set_tmp_ip_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 "set_tmp_ip_window.h"
13 #include "help_window.h"
14 
15 #include "layout.h"
16 #include "label.h"
17 
18 #include "rcdiscover/force_ip.h"
19 
20 #include <FL/fl_ask.H>
21 
22 #include <stdexcept>
23 
24 namespace
25 {
26 
27 void changingDeviceCb(Fl_Widget *, void *user_data)
28 {
29  SetTmpIPWindow *win=reinterpret_cast<SetTmpIPWindow *>(user_data);
30  win->isChangingDevice();
31 }
32 
33 void ipCb(Fl_Widget *, void *user_data)
34 {
35  SetTmpIPWindow *win=reinterpret_cast<SetTmpIPWindow *>(user_data);
36  win->doIP();
37 }
38 
39 void subnetMaskCb(Fl_Widget *, void *user_data)
40 {
41  SetTmpIPWindow *win=reinterpret_cast<SetTmpIPWindow *>(user_data);
42  win->doSubnetMask();
43 }
44 
45 void updateCb(Fl_Widget *, void *user_data)
46 {
47  SetTmpIPWindow *win=reinterpret_cast<SetTmpIPWindow *>(user_data);
48  win->update();
49 }
50 
51 void setCb(Fl_Widget *, void *user_data)
52 {
53  SetTmpIPWindow *win=reinterpret_cast<SetTmpIPWindow *>(user_data);
54  win->isSet();
55 }
56 
57 void clearCb(Fl_Widget *, void *user_data)
58 {
59  SetTmpIPWindow *win=reinterpret_cast<SetTmpIPWindow *>(user_data);
60  win->isClear();
61 }
62 
63 void helpCb(Fl_Widget *, void *)
64 {
65  HelpWindow::showWindow("forceip");
66 }
67 
68 }
69 
70 namespace
71 {
72 
73 static SetTmpIPWindow *win=0;
74 
75 }
76 
78 {
79  if (!win)
80  {
81  win=new SetTmpIPWindow();
82  }
83 
84  win->show();
85 
86  return win;
87 }
88 
90 {
91  if (win)
92  {
93  win->hide();
94  }
95 }
96 
97 void SetTmpIPWindow::updateDevices(const std::vector<std::pair<std::string, std::string> > &list,
98  const std::string &sel_mac)
99 {
100  device->update(list, sel_mac);
102  update();
103 }
104 
106 {
107  std::string v=device->getMAC();
108 
109  if (v.size() == 0)
110  {
111  mac->activate();
112  }
113  else
114  {
115  mac->deactivate();
116  }
117 
118  if (mac->isValid() && ip->isValid() && subnet_mask->isValid() && default_gateway->isValid())
119  {
120  set_ip->activate();
121  }
122  else
123  {
124  set_ip->deactivate();
125  }
126 }
127 
129 {
130  mac->value(device->getMAC().c_str());
131  update();
132 }
133 
135 {
136  if (ip->isValid() && !subnet_mask->isValid())
137  {
138  uint32_t v=ip->getIP();
139 
140  if ((v>>24) == 10) // 10.0.0.0/8 addresses
141  {
142  subnet_mask->value("255.0.0.0");
143  }
144 
145  if ((v>>24) == 172 && (static_cast<uint8_t>(v>>16)&16) != 0) // 172.16.0.0/12 addresses
146  {
147  subnet_mask->value("255.240.0.0");
148  }
149 
150  if ((v>>24) == 192 && static_cast<uint8_t>(v>>16) == 168) // 192.168.0.0/16 addresses
151  {
152  subnet_mask->value("255.255.0.0");
153  }
154 
155  if ((v>>24) == 169 && static_cast<uint8_t>(v>>16) == 254) // 169.254.0.0/16 addresses
156  {
157  subnet_mask->value("255.255.0.0");
158  }
159  }
160 
161  doSubnetMask();
162 }
163 
165 {
166  if (ip->isValid() && subnet_mask->isValid())
167  {
168  default_gateway->setIP((ip->getIP() & subnet_mask->getIP()) | 0x1);
169  }
170 
171  update();
172 }
173 
175 {
176  if (mac->isValid() && ip->isValid() && subnet_mask->isValid() && default_gateway->isValid())
177  {
178  try
179  {
180  if ((ip->getIP() & subnet_mask->getIP()) != (default_gateway->getIP() & subnet_mask->getIP()))
181  {
182  if (fl_choice("IP address and gateway appear to be in different subnets. ",
183  "Cancel", "Proceed", 0) != 1)
184  {
185  return;
186  }
187  }
188 
189  rcdiscover::ForceIP force_ip;
190 
191  if (fl_choice("Are you sure to set the IP address of the device with MAC-address %s?",
192  "No", "Yes", 0, mac->value()) == 1)
193  {
194  force_ip.sendCommand(mac->getMAC(), ip->getIP(), subnet_mask->getIP(),
196 
197  hide();
198  }
199  }
200  catch (const std::runtime_error &ex)
201  {
202  fl_alert("%s", ex.what());
203  }
204  }
205 }
206 
208 {
209  ip->value("");
210  subnet_mask->value("");
211  default_gateway->value("");
212 
213  update();
214 }
215 
216 SetTmpIPWindow::SetTmpIPWindow() : Fl_Double_Window(480, 238, "Set temporary IP address")
217 {
218  int width=480-2*GAP_SIZE;
219  int row_height=28;
220 
221  new Label(ADD_BELOW_XY, 120, row_height, "rc_visard");
222  device=new DeviceChoice(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
223  device->add("<Custom>");
224  device->callback(changingDeviceCb, this);
225 
226  new Label(ADD_BELOW_XY, 120, row_height, "MAC address");
227  mac=new InputMAC(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
228  mac->setChangeCallback(updateCb, this);
229 
230  new Label(ADD_BELOW_XY, 120, row_height, "IP address");
231  ip=new InputIP(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
232  ip->setChangeCallback(ipCb, this);
233 
234  new Label(ADD_BELOW_XY, 120, row_height, "Subnet mask");
235  subnet_mask=new InputIP(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
236  subnet_mask->setChangeCallback(subnetMaskCb, this);
237 
238  new Label(ADD_BELOW_XY, 120, row_height, "Default gateway");
239  default_gateway=new InputIP(ADD_RIGHT_XY, width-GAP_SIZE-120, row_height, 0);
240  default_gateway->setChangeCallback(updateCb, this);
241 
242  set_ip=new Button(ADD_BELOW_XY, 200, row_height, "Set temporary IP address");
243  set_ip->callback(setCb, this);
244 
245  clear_form=new Button(ADD_RIGHT_XY, 100, row_height, "Clear form");
246  clear_form->callback(clearCb, this);
247 
248  help=new Button(width+GAP_SIZE-row_height, addRightY(), row_height, row_height, "?");
249  help->callback(helpCb, this);
250 
251  checkGroupSize(__func__);
252  end();
253 
254  size_range(w(), h(), w(), h());
255 }
ADD_BELOW_XY
#define ADD_BELOW_XY
Definition: layout.h:121
SetTmpIPWindow::mac
InputMAC * mac
Definition: set_tmp_ip_window.h:49
rcdiscover::ForceIP
Class for sending GigE Vision FORCEIP_CMD to camera.
Definition: force_ip.h:26
SetTmpIPWindow::device
DeviceChoice * device
Definition: set_tmp_ip_window.h:48
Button
Definition: button.h:18
InputIP::isValid
bool isValid()
Definition: input_ip.h:31
SetTmpIPWindow::doSubnetMask
void doSubnetMask()
Definition: set_tmp_ip_window.cc:164
SetTmpIPWindow::doIP
void doIP()
Definition: set_tmp_ip_window.cc:134
InputIP::setChangeCallback
void setChangeCallback(Fl_Callback *_cb, void *p)
Definition: input_ip.h:26
SetTmpIPWindow::isSet
void isSet()
Definition: set_tmp_ip_window.cc:174
InputMAC::getMAC
uint64_t getMAC()
Definition: input_mac.cc:24
SetTmpIPWindow::ip
InputIP * ip
Definition: set_tmp_ip_window.h:50
SetTmpIPWindow
Definition: set_tmp_ip_window.h:26
SetTmpIPWindow::subnet_mask
InputIP * subnet_mask
Definition: set_tmp_ip_window.h:51
InputIP
Definition: input_ip.h:20
ADD_RIGHT_XY
#define ADD_RIGHT_XY
Definition: layout.h:120
SetTmpIPWindow::default_gateway
InputIP * default_gateway
Definition: set_tmp_ip_window.h:52
SetTmpIPWindow::help
Button * help
Definition: set_tmp_ip_window.h:55
GAP_SIZE
#define GAP_SIZE
Definition: layout.h:21
SetTmpIPWindow::updateDevices
void updateDevices(const std::vector< std::pair< std::string, std::string > > &list, const std::string &sel_mac)
Definition: set_tmp_ip_window.cc:97
SetTmpIPWindow::isChangingDevice
void isChangingDevice()
Definition: set_tmp_ip_window.cc:128
InputMAC::isValid
bool isValid()
Definition: input_mac.h:30
addRightY
int addRightY(int gap=GAP_SIZE)
Computes the y coordinate of a widget to the right of the last added widget.
Definition: layout.h:43
SetTmpIPWindow::set_ip
Button * set_ip
Definition: set_tmp_ip_window.h:53
InputMAC
Definition: input_mac.h:20
label.h
InputIP::setIP
void setIP(uint32_t v)
Definition: input_ip.cc:60
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
rcdiscover::ForceIP::sendCommand
void sendCommand(std::uint64_t mac, std::uint32_t ip, std::uint32_t subnet, std::uint32_t gateway)
Send FORCEIP_CMD.
Definition: force_ip.cc:29
SetTmpIPWindow::clear_form
Button * clear_form
Definition: set_tmp_ip_window.h:54
help_window.h
layout.h
Label
Definition: label.h:18
SetTmpIPWindow::showWindow
static SetTmpIPWindow * showWindow()
Definition: set_tmp_ip_window.cc:77
InputIP::getIP
uint32_t getIP()
Definition: input_ip.cc:24
SetTmpIPWindow::update
void update()
Definition: set_tmp_ip_window.cc:105
DeviceChoice::update
void update(const std::vector< std::pair< std::string, std::string > > &list, const std::string &sel_mac)
Definition: device_choice.cc:41
force_ip.h
DeviceChoice
Definition: device_choice.h:22
SetTmpIPWindow::hideWindow
static void hideWindow()
Definition: set_tmp_ip_window.cc:89
InputMAC::setChangeCallback
void setChangeCallback(Fl_Callback *_cb, void *p)
Definition: input_mac.h:26
DeviceChoice::getMAC
std::string getMAC()
Definition: device_choice.cc:20
set_tmp_ip_window.h
SetTmpIPWindow::SetTmpIPWindow
SetTmpIPWindow()
Definition: set_tmp_ip_window.cc:216
HelpWindow::showWindow
static void showWindow(const char *target=0)
Definition: help_window.cc:28
SetTmpIPWindow::isClear
void isClear()
Definition: set_tmp_ip_window.cc:207


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