156class TypeButtons :
public wxPanel,
public Reloadable<wxPanel> {
158 TypeButtons(wxWindow* parent, std::function<
void(ConnectionType)> on_click,
160 : wxPanel(parent, wxID_ANY),
161 m_serial_btn(
new wxRadioButton(
this, wxID_ANY, _(
"Serial"))),
162 m_net_btn(
new wxRadioButton(
this, wxID_ANY, _(
"Network"))),
164 m_canable_btn(
nullptr),
165 m_internal_gps_btn(
nullptr),
166 m_internal_bt_btn(
nullptr),
167 m_on_click(std::move(on_click)),
172 auto wbox =
new wxBoxSizer(wxHORIZONTAL);
174 auto wbox =
new wxWrapSizer(wxHORIZONTAL);
176 auto on_clicked = [&](wxCommandEvent& e) { OnClick(e); };
177 m_serial_btn->Bind(wxEVT_RADIOBUTTON, on_clicked);
178 wbox->Add(m_serial_btn, wxSizerFlags().Border());
180 m_net_btn->Bind(wxEVT_RADIOBUTTON, on_clicked);
181 wbox->Add(m_net_btn, wxSizerFlags().Border());
184 m_can_btn =
new wxRadioButton(
this, wxID_ANY, _(
"socketCAN"));
185 m_can_btn->Bind(wxEVT_RADIOBUTTON, on_clicked);
186 wbox->Add(m_can_btn, wxSizerFlags().Border());
189 m_canable_btn =
new wxRadioButton(
this, wxID_ANY, _(
"Canable (SLCAN)"));
190 m_canable_btn->Bind(wxEVT_RADIOBUTTON, on_clicked);
191 wbox->Add(m_canable_btn, wxSizerFlags().Border());
193 if (OCPNPlatform::hasInternalGPS()) {
194 m_internal_gps_btn =
new wxRadioButton(
this, wxID_ANY, _(
"Internal GPS"));
195 m_internal_gps_btn->Bind(wxEVT_RADIOBUTTON, on_clicked);
196 wbox->Add(m_internal_gps_btn, wxSizerFlags().Border());
198 if (OCPNPlatform::hasInternalBT()) {
200 new wxRadioButton(
this, wxID_ANY, _(
"Internal Bluetooth"));
201 m_internal_bt_btn->Bind(wxEVT_RADIOBUTTON, on_clicked);
202 wbox->Add(m_internal_bt_btn, wxSizerFlags().Border());
204 SetSelectedType(ConnectionType::SERIAL);
210 void Reload()
override { SetSelectedType(m_params.type); }
212 void SetSelectedType(ConnectionType type) {
214 case ConnectionType::NETWORK:
215 m_net_btn->SetValue(
true);
217 case ConnectionType::SERIAL:
218 m_serial_btn->SetValue(
true);
220 case ConnectionType::SOCKETCAN:
221 m_can_btn->SetValue(
true);
223 case ConnectionType::CANABLE:
224 if (m_canable_btn) m_canable_btn->SetValue(
true);
226 case ConnectionType::INTERNAL_GPS:
227 if (m_internal_gps_btn) m_internal_gps_btn->SetValue(
true);
229 case ConnectionType::INTERNAL_BT:
230 if (m_internal_bt_btn) m_internal_bt_btn->SetValue(
true);
233 assert(
false &&
"Illegal connection type");
238 wxRadioButton* m_serial_btn;
239 wxRadioButton* m_net_btn;
240 wxRadioButton* m_can_btn;
241 wxRadioButton* m_canable_btn;
242 wxRadioButton* m_internal_gps_btn;
243 wxRadioButton* m_internal_bt_btn;
244 std::function<void(ConnectionType)> m_on_click;
245 ConnectionParams& m_params;
247 [[nodiscard]] ConnectionType GetSelectedType()
const {
248 if (m_serial_btn->GetValue())
return ConnectionType::SERIAL;
249 if (m_net_btn->GetValue())
return ConnectionType::NETWORK;
250 if (m_can_btn && m_can_btn->GetValue())
return ConnectionType::SOCKETCAN;
251 if (m_canable_btn && m_canable_btn->GetValue())
252 return ConnectionType::CANABLE;
253 if (m_internal_gps_btn && m_internal_gps_btn->GetValue())
254 return ConnectionType::INTERNAL_GPS;
255 if (m_internal_bt_btn && m_internal_bt_btn->GetValue())
256 return ConnectionType::INTERNAL_BT;
257 assert(
false &&
"No connection type radiobutton active(!)");
258 return ConnectionType::UNKNOWN;
261 void OnClick(wxCommandEvent& e) {
262 auto type = GetSelectedType();
263 m_params.type = type;
270class InternalBtConfigPanel :
public wxPanel,
public Reloadable<wxPanel> {
271 class ScanTimer :
public wxTimer {
273 ScanTimer() : m_owner(
nullptr) {}
275 void SetOwner(InternalBtConfigPanel* owner) { m_owner = owner; }
277 void Notify()
override {
278 if (m_owner.get()) m_owner->OnTimer();
282 wxWeakRef<InternalBtConfigPanel> m_owner;
285 class AdvancedPanel :
public wxPanel {
288 : wxPanel(parent, wxID_ANY), m_params(params) {
289 auto vbox =
new wxBoxSizer(wxVERTICAL);
290 auto flags = wxSizerFlags();
292 vbox->Add(FilterConfigPanel::Create(
this, m_params), flags);
294 wxWindowBase::Layout();
303 : wxPanel(parent, wxID_ANY),
306 m_bt_no_change_counter(0),
307 m_bt_last_result_count(0) {
308 auto vbox =
new wxBoxSizer(wxVERTICAL);
309 auto flags = wxSizerFlags().Border();
311 vbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"Bluetooth Data Sources")),
314 auto src_box =
new wxBoxSizer(wxHORIZONTAL);
315 m_src_choice =
new wxChoice(
this, wxID_ANY);
316 m_src_choice->Append(
"unscanned");
317 m_src_choice->SetSelection(0);
318 m_scan_btn =
new wxButton(
this, wxID_ANY, _(
"Scan"));
319 m_scan_btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent& e) { OnClick(e); });
320 src_box->Add(m_src_choice, flags);
321 src_box->Add(m_scan_btn, flags);
322 vbox->Add(src_box, flags);
323 vbox->Add(conn_edit::InputOutputPanel::Create(
this, m_params));
325 auto advanced_panel =
new AdvancedPanel(
this, m_params);
326 auto adv_box =
new wxBoxSizer(wxHORIZONTAL);
328 adv_box->Add(
new wxStaticText(
this, wxID_ANY, _(
"Advanced")), flags);
329 auto on_expand_click = [&] {
333 adv_box->Add(
new ExpandablePanel(
this, advanced_panel, on_expand_click),
335 vbox->Add(adv_box, flags);
336 vbox->Add(advanced_panel, flags);
339 m_scan_timer.SetOwner(
this);
342 ~InternalBtConfigPanel()
override { m_scan_timer.Stop(); }
345 if (m_params.type == ConnectionType::INTERNAL_BT) SetFixedParams();
349 ScanTimer m_scan_timer;
351 wxButton* m_scan_btn;
352 wxChoice* m_src_choice;
354 int m_bt_no_change_counter;
355 wxArrayString m_bt_scan_results;
356 int m_bt_last_result_count;
359 void SetFixedParams() {
360 m_params.direction = PortDirection::kInOut;
361 m_params.network_address =
"";
362 m_params.network_port = 0;
363 m_params.net_protocol = PROTO_UNDEFINED;
364 m_params.baudrate = 0;
367 void OnClick(wxCommandEvent&) {
371 m_bt_no_change_counter = 0;
372 m_bt_last_result_count = 0;
373 m_scan_timer.Start(1000, wxTIMER_CONTINUOUS);
374 g_Platform->startBluetoothScan();
377 m_scan_btn->SetLabel(_(
"Stop Scan"));
383 if (!m_bt_scanning)
return;
385 m_bt_scan_results =
g_Platform->getBluetoothScanResults();
387 m_src_choice->Clear();
388 m_src_choice->Append(m_bt_scan_results[0]);
391 while ((i + 1) < m_bt_scan_results.GetCount()) {
392 wxString item1 = m_bt_scan_results[i] +
";";
393 wxString item2 = m_bt_scan_results.Item(i + 1);
394 m_src_choice->Append(item1 + item2);
398 if (m_bt_scan_results.GetCount() > 1) {
399 m_src_choice->SetSelection(1);
404 if (m_bt_no_change_counter > 5) StopScan();
405 int count =
static_cast<int>(m_bt_scan_results.GetCount());
406 if (count == m_bt_last_result_count)
407 m_bt_no_change_counter++;
409 m_bt_no_change_counter = 0;
410 m_bt_last_result_count =
static_cast<int>(m_bt_scan_results.GetCount());
413 if (m_bt_scanning >= 15) {
423 m_scan_btn->SetLabel(_(
"BT Scan"));
424 m_scan_btn->Enable();
464class CanableConfigPanel :
public wxPanel,
public Reloadable<wxPanel> {
468 params.data_protocol = PROTO_NMEA2000;
469 params.direction = PortDirection::kInOut;
470 params.network_address =
"";
471 params.network_port = 0;
472 params.net_protocol = PROTO_UNDEFINED;
478 : wxPanel(parent, wxID_ANY), m_params(params) {
479 auto* grid_sizer =
new wxFlexGridSizer(2, wxWindow::GetCharHeight() / 2, 0);
480 auto label_flags = wxSizerFlags().Border();
481#if wxCHECK_VERSION(3, 2, 0)
482 label_flags = label_flags.CentreVertical();
484 auto ctrl_flags = wxSizerFlags(1).Expand();
485 grid_sizer->Add(
new wxStaticText(
this, wxID_ANY, _(
"Data source")),
487 auto port_text_ctrl = PortCombo::Create(
this, m_params.serial_port);
488 port_text_ctrl->SetMinSize(wxSize(50 * wxWindow::GetCharWidth(), -1));
489 grid_sizer->Add(port_text_ctrl, ctrl_flags);
490 grid_sizer->Add(
new wxStaticText(
this, wxID_ANY,
"User comment"),
492 grid_sizer->Add(
new CommentText(
this, m_params.user_comment), ctrl_flags);
494 auto vbox =
new wxBoxSizer(wxVERTICAL);
495 vbox->Add(grid_sizer, wxSizerFlags().Border());
498 wxWindowBase::Layout();
502 if (m_params.type == ConnectionType::CANABLE) SetFixedParams(m_params);
511class SerialConfigPanel :
public wxPanel,
public Reloadable<wxPanel> {
514 class BaudChoice :
public wxComboBox,
public Reloadable {
516 BaudChoice(wxWindow* parent,
int& baudrate)
517 : wxComboBox(parent, wxID_ANY), m_baudrate(baudrate) {
519 Bind(wxEVT_COMBOBOX, [&](wxCommandEvent& e) { OnChange(e); });
522 void Reload()
override { LoadBaudrates(); }
527 void LoadBaudrates() {
528 static const std::vector<std::string> speeds = {
529 "150",
"300",
"600",
"1200",
"2400",
"4800",
"9600",
530 "19200",
"38400",
"57600",
"115200",
"230400",
"460800",
"921600"};
532 for (
const auto& speed : speeds) Append(speed);
533 auto found = FindString(std::to_string(m_baudrate));
534 if (found != wxNOT_FOUND)
540 void OnChange(wxCommandEvent& e) {
541 std::string speed = GetStringSelection().ToStdString();
543 m_baudrate = std::stoi(speed);
544 }
catch (std::logic_error&) {
545 assert(
false &&
"Cannot parse preconfigured baudrate");
551 class SerialParamsGrid :
public wxFlexGridSizer {
554 : wxFlexGridSizer(2), m_params(params) {
556 auto flags = wxSizerFlags().Border();
557 auto text_flags = wxSizerFlags().Border();
558#if wxCHECK_VERSION(3, 2, 0)
559 text_flags = text_flags.CentreVertical();
561 Add(
new wxStaticText(parent, wxID_ANY, _(
"Data port")), text_flags);
562 Add(PortCombo::Create(parent, m_params.serial_port), flags);
563 Add(
new wxStaticText(parent, wxID_ANY, _(
"Baudrate")), text_flags);
564 Add(
new BaudChoice(parent, m_params.baudrate), flags);
565 Add(
new wxStaticText(parent, wxID_ANY, _(
"Protocol")), text_flags);
566 Add(ProtocolChoice::Create(parent, m_params), flags);
567 Add(
new wxStaticText(parent, wxID_ANY, _(
"User Comment")), text_flags);
568 Add(
new CommentText(parent, m_params.user_comment),
569 wxSizerFlags(1).Border().Expand());
576 class GridPanel :
public wxPanel {
579 : wxPanel(parent, wxID_ANY),
581 m_grid(
new SerialParamsGrid(
this, m_params)) {
588 SerialParamsGrid* m_grid;
592 class AdvancedPanel :
public wxPanel {
595 : wxPanel(parent, wxID_ANY), m_params(params) {
596 auto vbox =
new wxBoxSizer(wxVERTICAL);
597 auto flags = wxSizerFlags().Border();
599 vbox->Add(FilterConfigPanel::Create(
this, m_params), flags);
601 wxWindowBase::Layout();
610 : wxPanel(parent), m_params(params) {
611 auto flags = wxSizerFlags().Border();
612 m_grid =
new GridPanel(
this, params);
614 m_advanced_panel =
new AdvancedPanel(
this, params);
616 GetParent()->Layout();
619 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
621 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"Advanced")), flags);
622 hbox->Add(advanced_ctrl, flags);
624 auto vbox =
new wxBoxSizer(wxVERTICAL);
625 vbox->Add(m_grid, wxSizerFlags().Border().Expand());
626 vbox->Add(InputOutputPanel::Create(
this, params), flags);
627 vbox->Add(hbox, flags);
628 vbox->Add(m_advanced_panel, flags);
635 if (m_params.type != ConnectionType::SERIAL)
return;
636 if (m_params.net_protocol != NetworkProtocol::PROTO_UNDEFINED)
return;
638 m_params.net_protocol = NetworkProtocol::TCP;
639 m_params.data_protocol = PROTO_NMEA0183;
640 m_params.direction = PortDirection::kInput;
641 m_params.network_address =
"";
642 m_params.network_port = 0;
656 wxWindow* m_advanced_panel;
661class CanConfigPanel :
public wxPanel {
662 class CanChoice :
public wxChoice,
public Reloadable<wxChoice> {
665 : wxChoice(parent, wxID_ANY), m_params(params) {
667 Bind(wxEVT_CHOICE, [&](wxCommandEvent& e) {
668 m_params.socket_can_port = GetStringSelection();
676 for (
const auto& port : GetAvailableSocketCanInterfaces()) Append(port);
677 if (GetCount() == 0) {
678 Append(
"No source available");
682 int found = FindString(m_params.socket_can_port);
683 SetSelection(found != wxNOT_FOUND ? found : 0);
691#if !defined(__linux__) || defined(__ANDROID__)
692 std::vector<std::string> GetAvailableSocketCanInterfaces() {
696 static int print_intf(
const struct intf_entry* entry,
void*) {
697 std::string iface = entry->intf_name;
698 if (entry->intf_type == 1 && iface.find(
"can") != std::string::npos) {
699 can_if_candidates.emplace_back(entry->intf_name);
704 static std::vector<std::string> GetAvailableSocketCanInterfaces() {
705 std::vector<std::string> rv;
706 can_if_candidates.clear();
708 if ((intf = intf_open()) ==
nullptr) {
709 wxLogWarning(
"Error opening interface list");
712 if (intf_loop(intf, print_intf,
nullptr) < 0) {
713 wxLogWarning(
"Error looping over interface list");
717 for (
const auto& iface : can_if_candidates) {
718 int sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
719 if (sock < 0)
continue;
722 ifreq if_request = {{0}};
723 strcpy(if_request.ifr_name, iface.c_str());
724 if (ioctl(sock, SIOCGIFINDEX, &if_request) < 0)
continue;
727 sockaddr_can can_address = {0};
728 can_address.can_family = AF_CAN;
729 can_address.can_ifindex = if_request.ifr_ifindex;
730 if (ioctl(sock, SIOCGIFFLAGS, &if_request) < 0)
continue;
732 if (if_request.ifr_flags & IFF_UP) rv.push_back(iface);
741 : wxPanel(parent), m_params(params) {
742 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
743 auto flags = wxSizerFlags().Border();
744 hbox->Add(
new wxStaticText(
this, wxID_ANY,
"Socketcan source"), flags);
745 m_choice =
new CanChoice(
this, m_params);
746 m_choice->Append(
"foobar");
747 hbox->Add(m_choice, wxSizerFlags().Border());
758class ConfigPanel :
public wxScrolledWindow {
761 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
762 wxVSCROLL | wxHSCROLL),
764 auto flags = wxSizerFlags(0).Expand();
765 auto vbox =
new wxBoxSizer(wxVERTICAL);
767 vbox->Add(m_serial_config_panel, flags);
768 m_net_config_panel = conn_edit::NetConfigPanel::Create(
this, m_params);
769 vbox->Add(m_net_config_panel, flags);
771 vbox->Add(m_can_config_panel, flags);
773 vbox->Add(m_canable_config_panel, flags);
775 vbox->Add(m_internal_gps_config_panel, flags);
777 vbox->Add(m_internal_bt_config_panel, flags);
779 vbox->Show(m_serial_config_panel,
false);
780 vbox->Show(m_net_config_panel,
false);
781 vbox->Show(m_can_config_panel,
false);
782 vbox->Show(m_canable_config_panel,
false);
783 vbox->Show(m_internal_gps_config_panel,
false);
784 vbox->Show(m_internal_bt_config_panel,
false);
787 SetScrollRate(10, 10);
788 SetActivePanel(ConnectionType::SERIAL);
789 wxScrolled<wxPanel>::Layout();
790 wxWindowBase::FitInside();
793 ~ConfigPanel()
override =
default;
795 void SetActivePanel(ConnectionType type) {
796 m_net_config_panel->Show(type == ConnectionType::NETWORK);
797 m_can_config_panel->Show(type == ConnectionType::SOCKETCAN);
798 m_serial_config_panel->Show(type == ConnectionType::SERIAL);
799 m_canable_config_panel->Show(type == ConnectionType::CANABLE);
800 m_internal_gps_config_panel->Show(type == ConnectionType::INTERNAL_GPS);
801 m_internal_bt_config_panel->Show(type == ConnectionType::INTERNAL_BT);
807 if (m_serial_config_panel->IsShown())
808 return m_serial_config_panel->CheckStatus();
809 if (m_net_config_panel->IsShown())
return m_net_config_panel->CheckStatus();
825class ConnectionEditDlg :
public AbstractConnectionEditDlg {
826 class ButtonSizer :
public wxStdDialogButtonSizer {
828 ButtonSizer(wxWindow* parent, std::function<
void(wxCommandEvent&)> on_ok,
829 std::function<
void(wxCommandEvent&)> on_cancel)
830 : wxStdDialogButtonSizer(),
831 m_on_ok(std::move(on_ok)),
832 m_on_cancel(std::move(on_cancel)) {
833 auto ok_btn =
new wxButton(parent, wxID_OK);
834 ok_btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent& ev) { m_on_ok(ev); });
836 auto cancel_btn =
new wxButton(parent, wxID_CANCEL);
837 cancel_btn->Bind(wxEVT_BUTTON,
838 [&](wxCommandEvent& ev) { m_on_cancel(ev); });
839 AddButton(cancel_btn);
840 SetAffirmativeButton(ok_btn);
845 std::function<void(wxCommandEvent&)> m_on_ok;
846 std::function<void(wxCommandEvent&)> m_on_cancel;
850 explicit ConnectionEditDlg(wxWindow* parent, std::function<
void()> on_cancel,
852 : AbstractConnectionEditDlg(parent),
853 m_on_ok(std::move(on_ok)),
854 m_on_cancel(std::move(on_cancel)),
855 m_params(GetDefaultParams()),
857 auto vbox =
new wxBoxSizer(wxVERTICAL);
858 auto statbox =
new wxStaticBoxSizer(wxVERTICAL,
this, _(
"Edit connection"));
859 auto on_type_btn_click = [&](ConnectionType ct) { OnTypeBtnClick(ct); };
860 auto flags = wxSizerFlags().Border();
863 statbox->Add(m_type_buttons, wxSizerFlags().Border());
864 statbox->Add(
new wxStaticLine(
this), wxSizerFlags().Expand());
866 statbox->Add(m_config_panel, wxSizerFlags(1).Expand());
867 vbox->Add(statbox, wxSizerFlags(1).Expand());
869 auto on_ok_ = [&](wxCommandEvent& ev) { OnOkCancel(ev,
true); };
870 auto on_cancel_ = [&](wxCommandEvent& ev) { OnOkCancel(ev,
false); };
871 auto button_sizer =
new ButtonSizer(
this, on_ok_, on_cancel_);
872 vbox->Add(button_sizer, flags.Expand());
873 m_type_buttons->SetSelectedType(ConnectionType::NETWORK);
874 OnTypeBtnClick(ConnectionType::NETWORK);
880 void Scroll(
int hscroll,
int vscroll)
override {
881 m_config_panel->Scroll(hscroll, vscroll);
884 void SetParams(std::unique_ptr<ConnectionParams> p)
override {
885 m_is_new = !
static_cast<bool>(p);
886 m_params = p ? *p : GetDefaultParams();
887 OnTypeBtnClick(m_params.type);
895 std::function<void()> m_on_cancel;
902 static void DoReload(wxWindow* root) {
903 for (wxWindow* child : root->GetChildren()) {
910 void Reload() { DoReload(
this); }
912 void OnTypeBtnClick(ConnectionType ct) {
914 m_config_panel->SetActivePanel(ct);
919 void OnOkCancel(wxCommandEvent&,
bool ok) {
922 }
else if (m_config_panel->CheckStatus()) {
923 m_on_ok(std::make_unique<ConnectionParams>(m_params), m_is_new);
927 static ConnectionParams GetDefaultParams() {
929 cp.type = ConnectionType::NETWORK;