reset-dialog.cc
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 // placed here to make sure to include winsock2.h before windows.h
37 #include "rcdiscover/wol.h"
38 
39 #include "reset-dialog.h"
40 
41 #include "event-ids.h"
42 #include "resources.h"
43 
46 
47 #include <thread>
48 #include <sstream>
49 
50 #include <wx/dialog.h>
51 #include <wx/sizer.h>
52 #include <wx/button.h>
53 #include <wx/valgen.h>
54 #include <wx/msgdlg.h>
55 #include <wx/panel.h>
56 #include <wx/cshelp.h>
57 
58 ResetDialog::ResetDialog(wxHtmlHelpController *help_ctrl,
59  wxWindow *parent, wxWindowID id,
60  const wxPoint &pos,
61  long style,
62  const wxString &name) :
63  SensorCommandDialog(help_ctrl, parent, id, "Reset rc_visard", 0, pos,
64  style, name)
65 {
66  const auto panel = getPanel();
67  const auto vbox = getVerticalBox();
68 
69  auto *button_box = new wxBoxSizer(wxHORIZONTAL);
70  auto *reset_param_button = new wxButton(panel, ID_Reset_Params,
71  "Reset Parameters", wxDefaultPosition,
72  wxDefaultSize, wxBU_EXACTFIT);
73  button_box->Add(reset_param_button, 0, wxEXPAND);
74  auto *reset_gige_button = new wxButton(panel, ID_Reset_GigE,
75  "Reset Network", wxDefaultPosition,
76  wxDefaultSize, wxBU_EXACTFIT);
77  button_box->Add(reset_gige_button, 0, wxEXPAND);
78  auto *reset_all_button = new wxButton(panel, ID_Reset_All,
79  "Reset All", wxDefaultPosition,
80  wxDefaultSize, wxBU_EXACTFIT);
81  button_box->Add(reset_all_button, 0, wxEXPAND);
82  auto *switch_part_button = new wxButton(panel, ID_Switch_Partition,
83  "Switch Partitions",
84  wxDefaultPosition, wxDefaultSize,
85  wxBU_EXACTFIT);
86  button_box->Add(switch_part_button, 0, wxEXPAND);
87 
88  button_box->AddSpacer(20);
89  int w, h;
90  switch_part_button->GetSize(&w, &h);
91  auto *help_button = new wxContextHelpButton(panel, ID_Help_Reset,
92  wxDefaultPosition, wxSize(h,h));
93  button_box->Add(help_button, 0, wxEXPAND);
94 
95  vbox->Add(button_box, 0, wxLEFT | wxRIGHT | wxBOTTOM, 15);
96  vbox->Fit(this);
97 
98  Connect(ID_Reset_Params,
99  wxEVT_BUTTON,
100  wxCommandEventHandler(ResetDialog::onResetButton));
101  Connect(ID_Reset_GigE,
102  wxEVT_BUTTON,
103  wxCommandEventHandler(ResetDialog::onResetButton));
104  Connect(ID_Reset_All,
105  wxEVT_BUTTON,
106  wxCommandEventHandler(ResetDialog::onResetButton));
107  Connect(ID_Switch_Partition,
108  wxEVT_BUTTON,
109  wxCommandEventHandler(ResetDialog::onResetButton));
110  Connect(ID_Help_Reset,
111  wxEVT_BUTTON,
112  wxCommandEventHandler(ResetDialog::onHelpButton));
113 
114  Centre();
115 }
116 
117 void ResetDialog::onResetButton(wxCommandEvent &event)
118 {
119  try
120  {
121  std::string func_name("");
122  uint8_t func_id(0);
123 
124  switch(event.GetId())
125  {
126  case ID_Reset_Params:
127  {
128  func_name = "reset parameters";
129  func_id = 0xAA;
130  }
131  break;
132 
133  case ID_Reset_GigE:
134  {
135  func_name = "reset network parameters";
136  func_id = 0xBB;
137  }
138  break;
139 
140  case ID_Reset_All:
141  {
142  func_name = "reset all";
143  func_id = 0xFF;
144  }
145  break;
146 
147  case ID_Switch_Partition:
148  {
149  func_name = "switch partition";
150  func_id = 0xCC;
151  }
152  break;
153 
154  default:
155  throw std::runtime_error("Unknown event ID");
156  }
157 
158  try
159  {
160  std::array<uint8_t, 6> mac = getMac();
161  std::string mac_string = getMacString();
162 
163  rcdiscover::WOL wol(mac, 9);
164 
165  std::ostringstream reset_check_str;
166  reset_check_str << "Are you sure to " << func_name <<
167  " of rc_visard with MAC-address " << mac_string << "?";
168  const int reset_check_answer =
169  wxMessageBox(reset_check_str.str(), "", wxYES_NO);
170 
171  if (reset_check_answer == wxYES)
172  {
173  bool try_again = false;
174  do
175  {
176  wol.send({{0xEE, 0xEE, 0xEE, func_id}});
177 
178  auto sent_dialog = new wxMessageDialog(
179  this,
180  "Please check whether rc_visard's LED turned white and whether rc_visard is rebooting.",
181  "Command sent", wxOK | wxCANCEL | wxCANCEL_DEFAULT | wxCENTRE);
182  sent_dialog->SetOKCancelLabels("Try again", "Done");
183  const int sent_answer = sent_dialog->ShowModal();
184  try_again = sent_answer == wxID_OK;
185  }
186  while (try_again);
187  }
188 
189  Hide();
190  }
191  catch(const std::runtime_error& ex)
192  {
193  wxMessageBox(ex.what(), "Error", wxOK | wxICON_ERROR);
194  }
195  }
197  {
198  wxMessageBox(std::string("rcdiscover probably requires root/admin ") +
199  "privileges for this operation.",
200  "Operation not permitted",
201  wxOK | wxICON_ERROR);
202  }
203 }
204 
205 void ResetDialog::onHelpButton(wxCommandEvent &)
206 {
207  displayHelp("reset");
208 }
209 
210 BEGIN_EVENT_TABLE(ResetDialog, SensorCommandDialog)
211 END_EVENT_TABLE()
std::string getMacString() const
Dialog for Magic Packets reset of rc_visard.
Definition: reset-dialog.h:48
Class for Magic Packet (Wake-on-Lan (WOL)) reset of device.
Definition: wol.h:62
ResetDialog()=default
Exception representing an "operation not permitted" error.
Base class for dialogs for sending commands to a camera.
void displayHelp(const std::string &section)
void onResetButton(wxCommandEvent &event)
Event handler for click on one of the four reset buttons.
std::array< uint8_t, 6 > getMac() const
void send() const
Send Magic Packet without any data ("password").
Definition: wol.cc:59
void onHelpButton(wxCommandEvent &)
Event handler for help button.


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32