reset_window.cc
Go to the documentation of this file.
1 /*
2  * Roboception GmbH
3  * Munich, Germany
4  * www.roboception.com
5  *
6  * Copyright (c) 2024 Roboception GmbH
7  * All rights reserved
8  *
9  * Author: Heiko Hirschmueller
10  */
11 
12 #include "reset_window.h"
13 #include "help_window.h"
14 
15 #include "layout.h"
16 #include "label.h"
17 
18 #include "rcdiscover/wol.h"
21 
22 #include <FL/fl_ask.H>
23 
24 #include <sstream>
25 
26 namespace
27 {
28 
29 void changingDeviceCb(Fl_Widget *, void *user_data)
30 {
31  ResetWindow *win=reinterpret_cast<ResetWindow *>(user_data);
32  win->isChangingDevice();
33 }
34 
35 void macCb(Fl_Widget *, void *user_data)
36 {
37  ResetWindow *win=reinterpret_cast<ResetWindow *>(user_data);
38  win->update();
39 }
40 
41 void resetParametersCb(Fl_Widget *, void *user_data)
42 {
43  ResetWindow *win=reinterpret_cast<ResetWindow *>(user_data);
44  win->isResetParameters();
45 }
46 
47 void resetNetworkCb(Fl_Widget *, void *user_data)
48 {
49  ResetWindow *win=reinterpret_cast<ResetWindow *>(user_data);
50  win->isResetNetwork();
51 }
52 
53 void resetAllCb(Fl_Widget *, void *user_data)
54 {
55  ResetWindow *win=reinterpret_cast<ResetWindow *>(user_data);
56  win->isResetAll();
57 }
58 
59 void switchPartitionsCb(Fl_Widget *, void *user_data)
60 {
61  ResetWindow *win=reinterpret_cast<ResetWindow *>(user_data);
62  win->isSwitchPartitions();
63 }
64 
65 void helpCb(Fl_Widget *, void *)
66 {
67  HelpWindow::showWindow("reset");
68 }
69 
70 }
71 
72 namespace
73 {
74 
75 static ResetWindow *win=0;
76 
77 }
78 
80 {
81  if (!win)
82  {
83  win=new ResetWindow();
84  }
85 
86  win->show();
87 
88  return win;
89 }
90 
92 {
93  if (win)
94  {
95  win->hide();
96  }
97 }
98 
99 void ResetWindow::updateDevices(const std::vector<std::pair<std::string, std::string> > &list,
100  const std::string &sel_mac)
101 {
102  device->update(list, sel_mac);
104  update();
105 }
106 
108 {
109  std::string v=device->getMAC();
110 
111  if (v.size() == 0)
112  {
113  mac->activate();
114  }
115  else
116  {
117  mac->deactivate();
118  }
119 
120  if (mac->isValid())
121  {
122  reset_parameters->activate();
123  reset_network->activate();
124  reset_all->activate();
125  switch_partitions->activate();
126  }
127  else
128  {
129  reset_parameters->deactivate();
130  reset_network->deactivate();
131  reset_all->deactivate();
132  switch_partitions->deactivate();
133  }
134 }
135 
137 {
138  mac->value(device->getMAC().c_str());
139  update();
140 }
141 
142 namespace
143 {
144 
145 void sendUDPRequest(const char *mac_string, uint64_t mac_value, const char *func_name,
146  uint8_t func_id)
147 {
148  if (mac_value != 0)
149  {
150  try
151  {
152  std::array<uint8_t, 6> mac;
153 
154  mac[0]=static_cast<uint8_t>(mac_value>>40);
155  mac[1]=static_cast<uint8_t>(mac_value>>32);
156  mac[2]=static_cast<uint8_t>(mac_value>>24);
157  mac[3]=static_cast<uint8_t>(mac_value>>16);
158  mac[4]=static_cast<uint8_t>(mac_value>>8);
159  mac[5]=static_cast<uint8_t>(mac_value);
160 
161  rcdiscover::WOL wol(mac, 9);
162 
163  std::ostringstream reset_check_str;
164  reset_check_str << "Are you sure to " << func_name <<
165  " of rc_visard with MAC-address " << mac_string << "?";
166  int answer=fl_choice("%s", "No", "Yes", 0, reset_check_str.str().c_str());
167 
168  while (answer == 1)
169  {
170  wol.send({{0xEE, 0xEE, 0xEE, func_id}});
171  answer=fl_choice("Please check whether rc_visard's LED turned white and whether rc_visard is rebooting.",
172  "Close", "Try again", 0);
173  }
174  }
175  catch (const rcdiscover::OperationNotPermitted&)
176  {
177  fl_alert("rcdiscover probably requires root/admin privileges for this operation.");
178  }
179  catch (const std::runtime_error& ex)
180  {
181  fl_alert("%s", ex.what());
182  }
183  }
184 }
185 
186 }
187 
189 {
190  sendUDPRequest(mac->value(), mac->getMAC(), "reset parameters", 0xAA);
191  hide();
192 }
193 
195 {
196  sendUDPRequest(mac->value(), mac->getMAC(), "reset network parameters", 0xBB);
197  hide();
198 }
199 
201 {
202  sendUDPRequest(mac->value(), mac->getMAC(), "reset all", 0xFF);
203  hide();
204 }
205 
207 {
208  sendUDPRequest(mac->value(), mac->getMAC(), "switch partition", 0xCC);
209  hide();
210 }
211 
212 ResetWindow::ResetWindow() : Fl_Double_Window(628, 124, "Reset rc_visard")
213 {
214  int width=628-2*GAP_SIZE;
215  int row_height=28;
216 
217  new Label(ADD_BELOW_XY, 120, row_height, "rc_visard");
218  device=new DeviceChoice(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
219  device->add("<Custom>");
220  device->value(0);
221  device->callback(changingDeviceCb, this);
222 
223  new Label(ADD_BELOW_XY, 120, row_height, "MAC address");
224  mac=new InputMAC(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
225  mac->setChangeCallback(macCb, this);
226 
227  reset_parameters=new Button(ADD_BELOW_XY, 160, row_height, "Reset parameters");
228  reset_parameters->callback(resetParametersCb, this);
229 
230  reset_network=new Button(ADD_RIGHT_XY, 140, row_height, "Reset network");
231  reset_network->callback(resetNetworkCb, this);
232 
233  reset_all=new Button(ADD_RIGHT_XY, 100, row_height, "Reset all");
234  reset_all->callback(resetAllCb, this);
235 
236  switch_partitions=new Button(ADD_RIGHT_XY, 140, row_height, "Switch partitions");
237  switch_partitions->callback(switchPartitionsCb, this);
238 
239  help=new Button(ADD_RIGHT_XY, row_height, row_height, "?");
240  help->callback(helpCb, this);
241 
242  checkGroupSize(__func__);
243  end();
244 
245  size_range(w(), h(), w(), h());
246 
247  update();
248 }
ADD_BELOW_XY
#define ADD_BELOW_XY
Definition: layout.h:121
wol.h
ResetWindow::isResetParameters
void isResetParameters()
Definition: reset_window.cc:188
operation_not_permitted.h
ResetWindow::isSwitchPartitions
void isSwitchPartitions()
Definition: reset_window.cc:206
ResetWindow::mac
InputMAC * mac
Definition: reset_window.h:45
Button
Definition: button.h:18
ResetWindow::device
DeviceChoice * device
Definition: reset_window.h:44
ResetWindow::isResetNetwork
void isResetNetwork()
Definition: reset_window.cc:194
ResetWindow::showWindow
static ResetWindow * showWindow()
Definition: reset_window.cc:79
InputMAC::getMAC
uint64_t getMAC()
Definition: input_mac.cc:24
ResetWindow::reset_network
Button * reset_network
Definition: reset_window.h:47
rcdiscover::OperationNotPermitted
Exception representing an "operation not permitted" error.
Definition: operation_not_permitted.h:47
ResetWindow::isResetAll
void isResetAll()
Definition: reset_window.cc:200
ResetWindow::switch_partitions
Button * switch_partitions
Definition: reset_window.h:49
wol_exception.h
ADD_RIGHT_XY
#define ADD_RIGHT_XY
Definition: layout.h:120
ResetWindow::isChangingDevice
void isChangingDevice()
Definition: reset_window.cc:136
ResetWindow::updateDevices
void updateDevices(const std::vector< std::pair< std::string, std::string > > &list, const std::string &sel_mac)
Definition: reset_window.cc:99
reset_window.h
GAP_SIZE
#define GAP_SIZE
Definition: layout.h:21
InputMAC::isValid
bool isValid()
Definition: input_mac.h:30
ResetWindow::ResetWindow
ResetWindow()
Definition: reset_window.cc:212
InputMAC
Definition: input_mac.h:20
label.h
checkGroupSize
void checkGroupSize(const char *group_name)
The size of the current group must be set before adding all children, especially resizable ones.
Definition: layout.h:88
ResetWindow::update
void update()
Definition: reset_window.cc:107
help_window.h
ResetWindow::reset_parameters
Button * reset_parameters
Definition: reset_window.h:46
layout.h
Label
Definition: label.h:18
ResetWindow::reset_all
Button * reset_all
Definition: reset_window.h:48
ResetWindow::hideWindow
static void hideWindow()
Definition: reset_window.cc:91
ResetWindow::help
Button * help
Definition: reset_window.h:50
DeviceChoice::update
void update(const std::vector< std::pair< std::string, std::string > > &list, const std::string &sel_mac)
Definition: device_choice.cc:41
ResetWindow
Definition: reset_window.h:22
DeviceChoice
Definition: device_choice.h:22
rcdiscover::WOL
Class for Magic Packet (Wake-on-Lan (WOL)) reset of device.
Definition: wol.h:62
InputMAC::setChangeCallback
void setChangeCallback(Fl_Callback *_cb, void *p)
Definition: input_mac.h:26
DeviceChoice::getMAC
std::string getMAC()
Definition: device_choice.cc:20
HelpWindow::showWindow
static void showWindow(const char *target=0)
Definition: help_window.cc:28


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Thu Aug 1 2024 02:55:56