OpenCPN Partial API docs
Loading...
Searching...
No Matches
conn_edit_commons.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 by David S. Register *
3 * Copyright (C) 2062 Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
24#include <set>
25#include <stdexcept>
26#include <string>
27#include <vector>
28
29#include "conn_edit_commons.h"
30
31#include <wx/button.h>
32#include <wx/checkbox.h>
33#include <wx/combobox.h>
34#include <wx/log.h>
35#include <wx/msgdlg.h>
36#include <wx/radiobut.h>
37#include <wx/sizer.h>
38#include <wx/stattext.h>
39#include <wx/textctrl.h>
40#include <wx/tokenzr.h>
41
42#include "libgui/expand_panel.h"
43
44#include "model/ocpn_utils.h"
45#include "model/ser_ports.h"
46#include "model/sys_events.h"
47
48#include "filter_sentence_dlg.h"
49
50// Make _() return std::string instead of wxString;
51#undef _
52#if wxCHECK_VERSION(3, 2, 0)
53#define _(s) wxGetTranslation(wxASCII_STR(s)).ToStdString()
54#else
55#define _(s) wxGetTranslation((s)).ToStdString()
56#endif
57
58namespace conn_edit {
59
60static const std::string kAddressDefaultHelp =
61 _("Enter IP address or hostname");
62static const std::string kAddressUdpHelp =
63 _("IP address or hostname, often 255.255.255.255");
64static const std::string kAddressMcastHelp =
65 _("Group address, usually 224.0.2.0 - 224.0.255.255");
66static const std::string kMcastPortHelp =
67 _("Port number, usually 49152 - 65535");
68static const std::string kTcpDevice = _("Device using TCP");
69static const std::string kUdpReceive = _("Device sending UDP");
70static const std::string kUdpOutput = _("UDP send");
71static const std::string kGpsdDevice = _("Gpsd server");
72static const std::string kSignalkDevice = _("SignalK server");
73static const std::string kTcpClient = _("TCP client");
74static const std::string kUdpSend = _("Device(s) receiving UDP");
75static const std::string kGpsdClient = _("Gpsd client");
76static const std::string kSignalkClient = _("SignalK client");
77static const std::string kTcpServer = _("TCP Server");
78static const std::string kUdpInput = _("UDP Receive");
79static const std::string kUdpInputOutput = _("UDP Receive and Send");
80static const std::string kMulticastServer = _("UDP Multicast Receive and Send");
81static const std::string kMulticastClient = _("UDP Multicast Send");
82static const std::string kDefaultGpsdPort = "2947";
83static const std::string kDefaultSignalkPort = "3000";
84
85const std::vector<std::string> kBasicNetViews = {
86 kTcpDevice, kUdpReceive, kUdpSend, kGpsdDevice, kSignalkDevice};
87
88const std::vector<std::string> kAdvancedNetViews = {
89 // First items matches kBasicNetViews
90 kTcpClient, kUdpInput, kUdpOutput,
91 kGpsdClient, kSignalkClient, kTcpServer,
92 kUdpInputOutput, kMulticastClient, kMulticastServer};
93
94void LoadSerialPorts(wxComboBox* box) {
96 class PortSorter {
97 private:
98 [[nodiscard]] static std::string GetKey(const std::string& s) {
99 if (s.find("->") == std::string::npos) return s;
100 return ocpn::trim(ocpn::split(s, "->")[1]) + " link";
101 }
102
103 public:
104 bool operator()(const std::string& lhs, const std::string& rhs) const {
105 return GetKey(lhs) < GetKey(rhs);
106 }
107 } port_sorter;
108
109 std::set<std::string, PortSorter> sorted_ports(port_sorter);
110 std::unique_ptr<wxArrayString> ports(EnumerateSerialPorts());
111 for (size_t i = 0; i < ports->GetCount(); i++)
112 sorted_ports.insert((*ports)[i].ToStdString());
113
114 auto value = box->GetValue();
115 box->Clear();
116 for (auto& p : sorted_ports) box->Append(p);
117 if (!value.empty()) box->SetValue(value);
118}
119
120bool CheckPort(wxWindow* parent, const TextCtrlWithHelp& ctrl) {
121 if (ctrl.IsPristine() || ctrl.GetValue().empty()) {
122 auto dlg = wxMessageDialog(parent, _("Required field port is missing"),
123 _("OpenCPN error"), wxOK | wxICON_ERROR);
124 dlg.ShowModal();
125 return false;
126 }
127 int port;
128 try {
129 port = std::stoi(ctrl.GetValue().ToStdString());
130 } catch (std::logic_error&) {
131 auto dlg = wxMessageDialog(parent, _("Invalid port number"),
132 _("OpenCPN error"), wxOK | wxICON_ERROR);
133 dlg.ShowModal();
134 return false;
135 }
136 if (port < 1024) {
137 static const std::string kMsg =
138 _(R"(Port numbers smaller than 1024 are reserved for use by the
139operating system and should normally not be used by OpenCPN)");
140 auto dlg = wxMessageDialog(parent, kMsg, _("OpenCPN warning"),
141 wxOK | wxICON_WARNING);
142 dlg.ShowModal();
143 return true;
144 }
145 return true;
146}
147
148bool CheckAddress(wxWindow* parent, const TextCtrlWithHelp& ctrl) {
149 if (ctrl.IsPristine() || ctrl.GetValue().empty()) {
150 auto dlg = wxMessageDialog(parent, _("Required field address is missing"),
151 _("OpenCPN error"), wxOK | wxICON_ERROR);
152 dlg.ShowModal();
153 return false;
154 }
155 // Checking the address requires using gethostbyname() or so since it
156 // could be a hostname. Not worthwhile in this context.
157 return true;
158}
159
160bool IsAddressMultiCast(const wxString& ip) {
161 wxArrayString bytes = wxSplit(ip, '.');
162 if (bytes.size() != 4) {
163 return false;
164 }
165 unsigned long ip_num = (wxAtoi(bytes[0]) << 24) + (wxAtoi(bytes[1]) << 16) +
166 (wxAtoi(bytes[2]) << 8) + wxAtoi(bytes[3]);
167 unsigned long multicast_start = (224 << 24);
168 unsigned long multicast_end = (239 << 24) + (255 << 16) + (255 << 8) + 255;
169 return ip_num >= multicast_start && ip_num <= multicast_end;
170}
171
172static wxString StringArrayToString(const wxArrayString& arr) {
173 wxString ret;
174 for (size_t i = 0; i < arr.Count(); i++) {
175 if (i > 0) ret.Append(",");
176 ret.Append(arr[i]);
177 }
178 return ret;
179}
181class ProtocolChoiceImpl : public ProtocolChoice {
182 friend ProtocolChoice* ProtocolChoice::Create(wxWindow*, ConnectionParams&,
183 std::function<void()>);
184
185public:
186 void Reload() override {
187 Clear();
188 Append("NMEA 0183");
189 Append("NMEA 2000");
190 switch (m_params.data_protocol) {
191 case PROTO_NMEA0183:
192 SetSelection(0);
193 break;
194 case PROTO_NMEA2000:
195 SetSelection(1);
196 break;
197 default:
198 assert(false && "Illegal SignalK protocol");
199 break;
200 }
201 Enable();
202 }
203
204protected:
206 wxWindow* parent, ConnectionParams& params,
207 std::function<void()> on_change = [] {})
208 : ProtocolChoice(parent),
209 m_params(params),
210 m_on_change(std::move(on_change)) {
212 Bind(wxEVT_CHOICE, [&](wxCommandEvent& e) { OnChoice(e); });
213 }
214
215private:
216 ConnectionParams& m_params;
217 std::function<void()> m_on_change;
218
219 void OnChoice(wxCommandEvent& e) {
220 switch (GetSelection()) {
221 case 0:
222 m_params.data_protocol = PROTO_NMEA0183;
223 break;
224 case 1:
225 m_params.data_protocol = PROTO_NMEA2000;
226 break;
227 default:
228 assert(false && "Illegal protocol combobox value");
229 }
230 m_on_change();
231 e.Skip();
232 }
233}; // ProtocolChoiceImpl
234
235ProtocolChoice* ProtocolChoice::Create(wxWindow* parent,
236 ConnectionParams& params,
237 std::function<void()> on_change) {
238 return new ProtocolChoiceImpl(parent, params, std::move(on_change));
239}
241class PortTextCtrlImpl : public PortTextCtrl {
242 friend PortTextCtrl* PortTextCtrl::Create(wxWindow*, ConnectionParams&);
243
244public:
245 void Reload() override {
246 auto string = std::to_string(m_params.network_port);
247 ChangeValue(string == "0" ? "" : string);
248 if (GetValue().empty()) RestoreHelp();
249 }
250
251 void ChangeValue(const wxString& value) override {
253 OnChange();
254 }
255
256protected:
257 PortTextCtrlImpl(wxWindow* parent, ConnectionParams& params)
258 : PortTextCtrl(parent), m_params(params) {
260 Bind(wxEVT_TEXT, [&](wxCommandEvent& e) { OnChange(e); });
261 }
262
263private:
264 ConnectionParams& m_params;
265
266 void OnChange() {
267 if (GetValue().empty()) return;
268 try {
269 m_params.network_port = std::stoi(GetValue().ToStdString());
270 } catch (std::logic_error& e) {
271 wxLogWarning("Cannot parse configuration network port: %s", e.what());
272 m_params.network_port = 99999; // Illegal for ipv4
273 }
274 }
275
276 void OnChange(wxCommandEvent& event) {
277 OnChange();
278 event.Skip();
279 }
280}; // PortTextCtrlImpl
281
282PortTextCtrl* PortTextCtrl::Create(wxWindow* parent, ConnectionParams& params) {
283 return new PortTextCtrlImpl(parent, params);
284}
286class InputOutputPanelImpl : public InputOutputPanel {
287 friend InputOutputPanel* InputOutputPanel::Create(wxWindow*,
289
290 class InputCheckBox : public wxCheckBox, public Reloadable<wxCheckBox> {
291 public:
292 InputCheckBox(wxWindow* parent, ConnectionParams& params,
293 std::function<void()> on_change)
294 : wxCheckBox(parent, wxID_ANY, _("Receive Input on this Port")),
295 m_params(params),
296 m_on_change(std::move(on_change)) {
298 Bind(wxEVT_CHECKBOX, [&](wxCommandEvent& ev) {
299 m_on_change();
300 ev.Skip();
301 });
302 }
303
304 void Reload() override {
305 SetValue(m_params.direction != PortDirection::kOutput);
306 }
307
308 private:
309 ConnectionParams& m_params;
310 std::function<void()> m_on_change;
311 };
312
313 class OutputCheckBox : public wxCheckBox, public Reloadable<wxCheckBox> {
314 public:
315 OutputCheckBox(wxWindow* parent, ConnectionParams& params,
316 std::function<void()> on_change)
317 : wxCheckBox(parent, wxID_ANY,
318 _("Output on this port as autopilot or NMEA repeater")),
319 m_params(params),
320 m_on_change(std::move(on_change)) {
322 Bind(wxEVT_CHECKBOX, [&](wxCommandEvent& ev) {
323 m_on_change();
324 ev.Skip();
325 });
326 };
327
328 void Reload() override {
329 SetValue(m_params.direction != PortDirection::kInput);
330 }
331
332 private:
333 ConnectionParams& m_params;
334 std::function<void()> m_on_change;
335 };
336
337public:
338 void EnableOutput(bool enabled) override {
339 m_output_box->Enable(enabled);
340 OnChange();
341 }
342
343 void EnableInput(bool enabled) override {
344 m_input_box->Enable(enabled);
345 OnChange();
346 }
347
348 void SetOutput(bool value) override {
349 m_output_box->SetValue(value);
350 OnChange();
351 }
352
353 void SetInput(bool value) override {
354 m_input_box->SetValue(value);
355 OnChange();
356 }
357
358protected:
359 InputOutputPanelImpl(wxWindow* parent, ConnectionParams& params)
360 : InputOutputPanel(parent), m_params(params) {
361 auto vbox = new wxBoxSizer(wxVERTICAL);
362 auto flags = wxSizerFlags().Border();
363 m_input_box = new InputCheckBox(this, m_params, [&] { OnChange(); });
364 m_output_box = new OutputCheckBox(this, m_params, [&] { OnChange(); });
365 vbox->Add(m_input_box, flags);
366 vbox->Add(m_output_box, flags);
367 SetSizer(vbox);
368 wxWindow::Layout();
369 }
370
371private:
372 ConnectionParams& m_params;
373 wxCheckBox* m_input_box;
374 wxCheckBox* m_output_box;
375
376 void OnChange() {
377 if (m_input_box->GetValue() && m_output_box->GetValue())
378 m_params.direction = PortDirection::kInOut;
379 else if (m_input_box->GetValue())
380 m_params.direction = PortDirection::kInput;
381 else if (m_output_box->GetValue())
382 m_params.direction = PortDirection::kOutput;
383 else
384 wxLogWarning("Inconsistent GUI direction state");
385 }
386}; // InputOutputPanelImpl
387
388InputOutputPanel* InputOutputPanel::Create(wxWindow* parent,
389 ConnectionParams& params) {
390 return new InputOutputPanelImpl(parent, params);
391}
392
393/** Configure input filters: accept or ignore list of sentences. */
394class FilterConfigPanelImpl : public FilterConfigPanel {
395 friend FilterConfigPanel* FilterConfigPanel::Create(wxWindow*,
397
399 class Statbox : public wxStaticBoxSizer {
400 public:
401 Statbox(wxWindow* parent, ListType& type, wxArrayString& list,
402 const std::string& label)
403 : wxStaticBoxSizer(wxVERTICAL, parent, label),
404 m_parent(parent),
405 m_type(type),
406 m_list(list) {
407 auto radio_btn_box = new wxBoxSizer(wxHORIZONTAL);
408 m_accept_btn =
409 new wxRadioButton(parent, wxID_ANY, _("Accept only sentences"),
410 wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
411 m_ignore_btn = new wxRadioButton(parent, wxID_ANY, _("Ignore sentences"));
412 radio_btn_box->Add(m_accept_btn, wxSizerFlags().Border());
413 radio_btn_box->Add(m_ignore_btn, wxSizerFlags().Border());
414
415 auto setup_box = new wxBoxSizer(wxHORIZONTAL);
416 auto flags1 = wxSizerFlags(1);
417 auto flags0 = wxSizerFlags(0);
418 m_sentences_text =
419 new wxStaticText(parent, wxID_ANY, "", wxDefaultPosition,
420 wxDefaultSize, wxST_ELLIPSIZE_END);
421 m_edit_btn = new wxButton(parent, wxID_ANY, "...");
422 m_edit_btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent& e) { OnEdit(e); });
423 m_edit_btn->SetMaxSize(wxSize(parent->GetCharWidth() * 4, -1));
424 setup_box->Add(m_sentences_text, flags1.Expand());
425 setup_box->Add(m_edit_btn, flags0.Border(wxDOWN | wxRIGHT | wxLEFT));
426
427 auto vbox = new wxBoxSizer(wxVERTICAL);
428 vbox->Add(radio_btn_box, wxSizerFlags());
429 vbox->Add(setup_box, flags1.DoubleBorder(wxLEFT | wxRIGHT | wxDOWN));
430 Add(vbox);
431 wxSizer::Layout();
432
433 Statbox::Reload();
434 m_ignore_btn->Bind(wxEVT_RADIOBUTTON,
435 [&](wxCommandEvent& e) { OnDirectionChange(e); });
436 m_accept_btn->Bind(wxEVT_RADIOBUTTON,
437 [&](wxCommandEvent& e) { OnDirectionChange(e); });
438 }
439
440 void Reload() {
441 m_accept_btn->SetValue(m_type == ListType::WHITELIST);
442 m_ignore_btn->SetValue(m_type == ListType::BLACKLIST);
443 auto text = StringArrayToString(m_list);
444 text = text.empty() ? _("No sentences selected") : text;
445 m_sentences_text->SetLabel(text);
446 m_parent->GetGrandParent()->Layout();
447 }
448
449 private:
450 wxWindow* m_parent;
451 ListType& m_type;
452 wxArrayString& m_list;
453 wxRadioButton* m_accept_btn;
454 wxRadioButton* m_ignore_btn;
455 wxStaticText* m_sentences_text;
456 wxButton* m_edit_btn;
457
458 void OnDirectionChange(wxCommandEvent& e) {
459 if (m_accept_btn->GetValue()) m_type = ListType::WHITELIST;
460 if (m_ignore_btn->GetValue()) m_type = ListType::BLACKLIST;
461 e.Skip();
462 }
463
464 void OnEdit(wxCommandEvent&) {
465 static const std::string kNothingHere = _("No sentences selected");
466 if (m_sentences_text->GetLabel() == kNothingHere)
467 m_sentences_text->SetLabel("");
468 const wxArrayString list =
469 wxStringTokenize(m_sentences_text->GetLabel(), ",");
470 FilterSentenceDlg dlg(m_parent, FILTER_INPUT, m_type, list);
471 if (dlg.ShowModal() != wxID_OK) return;
472
473 std::string text = dlg.GetSentences().ToStdString();
474 m_list = wxStringTokenize(text, ",");
475 if (text.empty()) text = kNothingHere;
476 m_sentences_text->SetLabel(text);
477 m_parent->Layout();
478 }
479 }; // Statbox
480
481public:
482 void Reload() override {
483 m_output_filter->Reload();
484 m_input_filter->Reload();
485 }
486
487protected:
488 FilterConfigPanelImpl(wxWindow* parent, ConnectionParams& params)
489 : FilterConfigPanel(parent), m_params(params) {
490 auto* vbox = new wxBoxSizer(wxVERTICAL);
491 auto flags = wxSizerFlags().Border();
492 m_input_filter =
493 new Statbox(this, m_params.input_sentence_list_type,
494 m_params.input_sentence_list, _("Input filtering"));
495 vbox->Add(m_input_filter, flags);
496 m_output_filter =
497 new Statbox(this, m_params.output_sentence_list_type,
498 m_params.output_sentence_list, _("Output filtering"));
499 vbox->Add(m_output_filter, flags);
500 SetSizer(vbox);
501 wxWindowBase::Layout();
502 }
503
504private:
505 Statbox* m_input_filter;
506 Statbox* m_output_filter;
507 ConnectionParams& m_params;
508}; // FilterConfigPanelImpl
509
510FilterConfigPanel* FilterConfigPanel::Create(wxWindow* parent,
511 ConnectionParams& params) {
512 return new FilterConfigPanelImpl(parent, params);
513}
515class PortComboImpl : public PortCombo {
516 friend PortCombo* PortCombo::Create(wxWindow*, wxString&);
517
518public:
519 void Reload() override { SetValue(m_port); }
520
521protected:
522 PortComboImpl(wxWindow* parent, wxString& port)
523 : PortCombo(parent), m_port(port) {
524 PortComboImpl::LoadSerialPorts();
525 Bind(wxEVT_COMBOBOX, [&](wxCommandEvent& e) {
526 std::string value = GetValue().ToStdString();
527 size_t pos = value.find(' '); // Strip away informational info
528 m_port = pos == std::string::npos ? value : value.substr(0, pos);
529 e.Skip();
530 });
531 m_device_list_listener.Init(
532 SystemEvents::GetInstance().evt_dev_change,
533 [&](ObservedEvt&) { conn_edit::LoadSerialPorts(this); });
534 }
535
536private:
537 wxString& m_port;
538 obs::Listener m_device_list_listener;
539
540 virtual void LoadSerialPorts() {
542 class PortSorter {
543 private:
544 [[nodiscard]] static std::string GetKey(const std::string& s) {
545 if (s.find("->") == std::string::npos) return s;
546 return ocpn::trim(ocpn::split(s, "->")[1]) + " link";
547 }
548
549 public:
550 bool operator()(const std::string& lhs, const std::string& rhs) const {
551 return GetKey(lhs) < GetKey(rhs);
552 }
553 } port_sorter;
554
555 std::set<std::string, PortSorter> sorted_ports(port_sorter);
556 std::unique_ptr<wxArrayString> ports(EnumerateSerialPorts());
557 for (size_t i = 0; i < ports->GetCount(); i++)
558 sorted_ports.insert((*ports)[i].ToStdString());
559
560 auto value = GetValue();
561 Clear();
562 for (auto& p : sorted_ports) Append(p);
563 if (!value.empty())
564 SetValue(value);
565 else if (FindString(m_port) != wxNOT_FOUND)
566 Select(FindString(m_port));
567 else if (!sorted_ports.empty())
568 SetSelection(0);
569 }
570}; // PortComboImpl
571
572PortCombo* PortCombo::Create(wxWindow* parent, wxString& port) {
573 return new PortComboImpl(parent, port);
574}
576class NetConfigPanelImpl : public NetConfigPanel {
577 friend NetConfigPanel* NetConfigPanel::Create(wxWindow*, ConnectionParams&);
578
579 class ExpertModeCheckBox : public wxCheckBox {
580 public:
581 explicit ExpertModeCheckBox(wxWindow* parent)
582 : wxCheckBox(parent, wxID_ANY, "") {}
583 };
584
586 class ConnectionTypeChoice : public wxChoice {
587 public:
588 ConnectionTypeChoice(wxWindow* parent, std::function<void()> on_choice)
589 : wxChoice(parent, wxID_ANY), m_on_choice(std::move(on_choice)) {
590 for (const auto& view : kBasicNetViews) Append(view); // FIXME
591 Append(kBasicNetViews[0]);
592 wxChoice::SetSelection(0); // until OnConnectionTypeChanged()
593 Bind(wxEVT_CHOICE, [&](wxCommandEvent e) {
594 m_on_choice();
595 e.Skip();
596 });
597 }
598
599 private:
600 std::function<void()> m_on_choice;
601 };
602
604 class AddressTextCtrl : public TextCtrlWithHelp,
605 public Reloadable<TextCtrlWithHelp> {
606 public:
607 AddressTextCtrl(wxWindow* parent, ConnectionParams& params)
608 : TextCtrlWithHelp(parent,
609 "IP address or hostname, often 255.255.255.255"),
610 m_params(params) {
612 Bind(wxEVT_TEXT, [&](wxCommandEvent& e) {
613 m_params.network_address = GetValue();
614 e.Skip();
615 });
616 wxWindowBase::SetMinSize(wxSize(60 * wxWindow::GetCharWidth(), -1));
617 }
618
619 void Reload() override {
620 TextCtrlWithHelp::ChangeValue(m_params.network_address);
621 if (GetValue().empty()) RestoreHelp();
622 }
623
624 void ChangeValue(const wxString& text) override {
626 m_params.network_address = text;
627 }
628
629 void SetClientMode() {
630 if (GetValue() == "0.0.0.0") ChangeValue("");
631 if (IsPristine() || GetValue().empty()) SetHelp(kAddressDefaultHelp);
632 Enable();
633 }
634
635 void SetServerMode() {
636 ChangeValue("0.0.0.0");
637 m_params.network_address = "0.0.0.0";
638 Disable();
639 }
640
641 void SetClientMode(const std::string& help) {
642 SetClientMode();
643 SetHelp(help);
644 if (IsPristine()) RestoreHelp();
645 }
646
647 private:
648 ConnectionParams& m_params;
649 };
650
652 class CommentTextCtrl : public wxTextCtrl, public Reloadable<wxTextCtrl> {
653 public:
654 CommentTextCtrl(wxWindow* parent, ConnectionParams& params)
655 : wxTextCtrl(parent, wxID_ANY), m_params(params) {
657 Bind(wxEVT_TEXT, [&](wxCommandEvent& e) {
658 m_params.user_comment = GetValue();
659 e.Skip();
660 });
661 }
662
663 void Reload() override { ChangeValue(m_params.user_comment); }
664
665 private:
666 ConnectionParams& m_params;
667 };
668
670 class NetGrid : public wxFlexGridSizer {
671 public:
672 NetGrid(wxWindow* parent, ConnectionParams& params,
673 InputOutputPanel* in_out_panel,
674 std::function<void(DataProtocol)> on_protocol_change)
675 : wxFlexGridSizer(2),
676 m_parent(parent),
677 m_params(params),
678 m_in_out_panel(in_out_panel),
679 m_on_protocol_change(std::move(on_protocol_change)) {
680
681 // Longest help text in address and port:
682 static const wxSize kHelpSize = parent->GetTextExtent(kAddressMcastHelp);
683 AddGrowableCol(1);
684 auto flags = wxSizerFlags().Border();
685 auto label_flags = wxSizerFlags().Border();
686#if wxCHECK_VERSION(3, 2, 0)
687 label_flags = label_flags.CentreVertical();
688#endif
689 m_expert_mode_checkbox = new ExpertModeCheckBox(parent);
690 m_expert_mode_checkbox->Bind(wxEVT_CHECKBOX, [&](wxCommandEvent& e) {
691 OnExpertModeChange();
692 e.Skip();
693 });
694 m_expert_mode_label =
695 new wxStaticText(parent, wxID_ANY, _("Expert mode"));
696 Add(m_expert_mode_label, label_flags);
697 Add(m_expert_mode_checkbox, flags);
698
699 m_connection_type_label =
700 new wxStaticText(parent, wxID_ANY, _("Connection type"));
701 m_connection_type_choice =
702 new ConnectionTypeChoice(parent, [&] { OnConnectionTypeChange(); });
703 Add(m_connection_type_label, label_flags);
704 Add(m_connection_type_choice, flags);
705
706 m_protocol_label = new wxStaticText(parent, wxID_ANY, _("Data Protocol"));
707 m_protocol_choice = ProtocolChoice::Create(parent, m_params, [&] {
708 m_on_protocol_change(m_params.data_protocol);
709 });
710 Add(m_protocol_label, label_flags);
711 Add(m_protocol_choice, flags);
712
713 m_address_label = new wxStaticText(parent, wxID_ANY, _("Server address"));
714 m_address_textctrl = new AddressTextCtrl(parent, m_params);
715 m_address_textctrl->SetMinSize(wxSize(kHelpSize.x, -1));
716 m_address_textctrl->Bind(wxEVT_KILL_FOCUS,
717 [&](wxFocusEvent& ev) { OnAddressChange(ev); });
718 Add(m_address_label, label_flags);
719 Add(m_address_textctrl, wxSizerFlags().Border());
720
721 m_port_label = new wxStaticText(parent, wxID_ANY, _("Data Port"));
722 m_port_textctrl = conn_edit::PortTextCtrl::Create(parent, m_params);
723 m_port_textctrl->SetMinSize(wxSize(kHelpSize.x, -1));
724 Add(m_port_label, label_flags);
725 Add(m_port_textctrl, wxSizerFlags().Border());
726
727 m_comment_label = new wxStaticText(parent, wxID_ANY, _("User comment"));
728 m_comment_textctrl = new CommentTextCtrl(parent, m_params);
729 Add(m_comment_label, label_flags);
730 Add(m_comment_textctrl, wxSizerFlags(1).Expand().Border());
731 OnExpertModeChange();
732 ConfigureControlsForView(kTcpDevice);
733 wxSizer::Layout();
734 }
735
736 [[nodiscard]] bool CheckStatus() const {
737 bool ok = CheckAddress(m_parent, *m_address_textctrl);
738 if (ok) ok = CheckPort(m_parent, *m_port_textctrl);
739 return ok;
740 }
741
743 void Reload() {
744 if (m_params.type != ConnectionType::NETWORK) return;
745
746 std::string view;
747 bool is_expert;
748 if (IsDefaultParams(&m_params)) {
749 view = kTcpDevice;
750 is_expert = false;
751 } else {
752 view = NetViewByConnection(&m_params);
753 auto found =
754 std::find(kBasicNetViews.begin(), kBasicNetViews.end(), view);
755 is_expert = found == kBasicNetViews.end();
756 }
757 m_expert_mode_checkbox->SetValue(is_expert);
758 OnExpertModeChange();
759
760 auto item = m_connection_type_choice->FindString(view);
761 if (item != wxNOT_FOUND) m_connection_type_choice->SetSelection(item);
762 OnConnectionTypeChange();
763 }
764
765 private:
766 wxWindow* m_parent;
767 ConnectionParams& m_params;
768 InputOutputPanel* m_in_out_panel;
769 std::function<void(DataProtocol)> m_on_protocol_change;
770 wxStaticText* m_expert_mode_label;
771 ExpertModeCheckBox* m_expert_mode_checkbox;
772 wxStaticText* m_connection_type_label;
773 wxChoice* m_connection_type_choice;
774 wxStaticText* m_protocol_label;
775 ProtocolChoice* m_protocol_choice;
776 wxStaticText* m_address_label;
777 AddressTextCtrl* m_address_textctrl;
778 wxStaticText* m_port_label;
779 PortTextCtrl* m_port_textctrl;
780 wxStaticText* m_comment_label;
781 wxTextCtrl* m_comment_textctrl;
782
783 void OnExpertModeChange() {
784 bool is_expert = m_expert_mode_checkbox->GetValue();
785 int view = m_connection_type_choice->GetSelection();
786 m_connection_type_choice->Clear();
787 if (is_expert) {
788 for (const auto& choice : kAdvancedNetViews)
789 m_connection_type_choice->Append(choice);
790 m_connection_type_label->SetLabel(_("Connection type"));
791 if (m_comment_label) m_comment_label->Show();
792 if (m_comment_textctrl) m_comment_textctrl->Show();
793 } else {
794 if (view != wxNOT_FOUND) {
795 if (static_cast<size_t>(view) >= kBasicNetViews.size()) view = 0;
796 }
797 for (const auto& choice : kBasicNetViews)
798 m_connection_type_choice->Append(choice);
799 m_connection_type_label->SetLabel(_("Connect to"));
800 if (m_comment_label) m_comment_label->Hide();
801 if (m_comment_textctrl) m_comment_textctrl->Hide();
802 }
803 m_connection_type_choice->SetSelection(view);
804 auto view_str =
805 m_connection_type_choice->GetStringSelection().ToStdString();
806 if (!view_str.empty()) ConfigureControlsForView(view_str);
807 m_parent->GetParent()->Layout();
808 m_parent->FitInside();
809 }
810
811 void OnAddressChange(wxFocusEvent& ev) const {
812 int selection = m_connection_type_choice->GetSelection();
813 if (selection != wxNOT_FOUND) {
814 std::string type =
815 m_connection_type_choice->GetString(selection).ToStdString();
816 if (type == kMulticastClient || type == kMulticastServer) {
817 auto address = m_address_textctrl->GetValue().ToStdString();
818 if (!IsAddressMultiCast(address)) {
819 auto dlg =
820 wxMessageDialog(m_parent, _("Illegal multicast address"),
821 _("OpenCPN warning"), wxOK | wxICON_WARNING);
822 dlg.ShowModal();
823 }
824 }
825 }
826 ev.Skip();
827 }
828
829 void OnConnectionTypeChange() {
830 if (m_params.type != ConnectionType::NETWORK) return;
831 int selection = m_connection_type_choice->GetSelection();
832 if (selection == wxNOT_FOUND) return;
833 std::string view =
834 m_connection_type_choice->GetStringSelection().ToStdString();
835 if (view == kTcpDevice || view == kTcpClient || view == kTcpServer)
836 m_params.net_protocol = NetworkProtocol::TCP;
837 else if (view == kSignalkClient || view == kSignalkDevice)
838 m_params.net_protocol = NetworkProtocol::SIGNALK;
839 else if (view == kGpsdClient || view == kGpsdDevice)
840 m_params.net_protocol = NetworkProtocol::GPSD;
841 else if (view == kUdpReceive || view == kUdpInput || view == kUdpSend ||
842 view == kUdpOutput || view == kMulticastClient ||
843 view == kMulticastServer || view == kUdpInputOutput)
844 m_params.net_protocol = NetworkProtocol::UDP;
845 else
846 assert(false && "Cannot handle view");
847 if (view == kTcpServer || view == kUdpReceive || view == kUdpInput ||
848 view == kMulticastServer)
849 m_params.network_address = "0.0.0.0";
850 ConfigureControlsForView(view);
851 OnExpertModeChange();
852 }
853
859 static std::string NetViewByConnection(const ConnectionParams* cp) {
860 auto address = cp->network_address.ToStdString();
861 bool is_server = address.empty() || address == "0.0.0.0";
862 if (IsAddressMultiCast(cp->network_address))
863 return is_server ? kMulticastServer : kMulticastClient;
864 switch (cp->net_protocol) {
865 case NetworkProtocol::GPSD:
866 return kGpsdDevice;
867 case NetworkProtocol::SIGNALK:
868 return kSignalkDevice;
869 case NetworkProtocol::UDP:
870 if (cp->direction == PortDirection::kInOut) return kUdpInputOutput;
871 if (!is_server) return kUdpSend;
872 return kUdpReceive;
873 case NetworkProtocol::TCP:
874 return is_server ? kTcpServer : kTcpDevice;
875 default:
876 wxLogWarning("Cannot deduce connection params view type");
877 return "";
878 }
879 return ""; // for the compiler
880 }
881
882 void ConfigureControlsForView(const std::string& view) {
883 if (m_params.type != ConnectionType::NETWORK) {
884 m_connection_type_choice->SetSelection(0);
885 return;
886 }
887 int selection = m_connection_type_choice->GetSelection();
888 assert(selection != wxNOT_FOUND);
889 auto found =
890 std::find(kBasicNetViews.begin(), kBasicNetViews.end(), view);
891 m_expert_mode_checkbox->SetValue(found == kBasicNetViews.end());
892 m_address_textctrl->Reload();
893 m_address_textctrl->SetClientMode();
894 m_address_textctrl->Show();
895 m_address_label->SetLabel(_("Server address"));
896 m_address_label->Show();
897 m_in_out_panel->EnableOutput(false);
898 m_in_out_panel->EnableInput(false);
899 m_port_textctrl->Reload();
900 auto port = m_port_textctrl->GetValue();
901 if (port == kDefaultGpsdPort || port == kDefaultSignalkPort ||
902 port.empty()) {
903 m_port_textctrl->RestoreHelp();
904 }
905 m_protocol_choice->Reload();
906 m_protocol_choice->Enable();
907 if (view == kTcpDevice || view == kTcpClient) {
908 m_address_label->Show();
909 m_address_textctrl->SetClientMode();
910 if (m_port_textctrl->IsPristine()) m_port_textctrl->SetHelp(kPortHelp);
911 m_in_out_panel->EnableInput(true);
912 m_in_out_panel->EnableOutput(true);
913 } else if (view == kUdpReceive || view == kUdpInput ||
914 view == kUdpInputOutput) {
915 m_address_label->Show(view == kUdpInputOutput);
916 if (view != kUdpInputOutput) m_address_textctrl->SetServerMode();
917 m_address_textctrl->Show(view == kUdpInputOutput);
918 if (m_port_textctrl->IsPristine()) m_port_textctrl->SetHelp(kPortHelp);
919 m_in_out_panel->SetOutput(view == kUdpInputOutput);
920 m_in_out_panel->SetInput(true);
921 } else if (view == kUdpSend || view == kUdpOutput) {
922 m_address_textctrl->SetClientMode(kAddressUdpHelp);
923 m_in_out_panel->SetInput(false);
924 m_in_out_panel->SetOutput(true);
925 } else if (view == kGpsdClient || view == kGpsdDevice) {
926 m_protocol_choice->Clear();
927 m_protocol_choice->Append("gpsd");
928 m_protocol_choice->SetSelection(0);
929 m_protocol_choice->Disable();
930 m_address_textctrl->SetClientMode();
931 if (m_port_textctrl->IsPristine())
932 m_port_textctrl->ChangeValue(kDefaultGpsdPort);
933 m_in_out_panel->SetInput(true);
934 m_in_out_panel->SetOutput(false);
935 } else if (view == kSignalkClient || view == kSignalkDevice) {
936 m_protocol_choice->Clear();
937 m_protocol_choice->Append("SignalK");
938 m_protocol_choice->SetSelection(0);
939 m_protocol_choice->Disable();
940 m_address_textctrl->SetClientMode(kAddressUdpHelp);
941 if (m_port_textctrl->IsPristine())
942 m_port_textctrl->ChangeValue(kDefaultSignalkPort);
943 m_in_out_panel->SetInput(true);
944 m_in_out_panel->SetOutput(false);
945 } else if (view == kTcpServer) {
946 m_address_label->SetLabel(_("Interface"));
947 m_address_textctrl->SetServerMode();
948 m_in_out_panel->EnableInput(true);
949 m_in_out_panel->EnableOutput(true);
950 } else if (view == kMulticastClient || view == kMulticastServer) {
951 auto address = m_address_textctrl->GetValue();
952 if (address.empty() || address == "0.0.0.0")
953 m_address_textctrl->SetHelp(kAddressMcastHelp);
954 m_address_label->SetLabel(_("Multicast group"));
955 if (m_port_textctrl->IsPristine())
956 m_port_textctrl->SetHelp(kMcastPortHelp);
957 if (m_address_textctrl->IsPristine())
958 m_address_textctrl->SetHelp(kAddressMcastHelp);
959 if (view == kMulticastClient) {
960 m_in_out_panel->SetInput(false);
961 m_in_out_panel->SetOutput(true);
962 } else {
963 m_in_out_panel->SetInput(true);
964 m_in_out_panel->EnableOutput(true);
965 }
966 } else {
967 wxLogError("Illegal view: %s", view.c_str());
968 assert(false && "Illegal view");
969 }
970 }
971
972 static bool IsDefaultParams(const ConnectionParams* params) {
973 static const ConnectionParams default_params;
974 return ConnectionType::NETWORK == params->type &&
975 default_params.net_protocol == params->net_protocol &&
976 default_params.network_port == params->network_port &&
977 default_params.network_address == params->network_address;
978 }
979 }; // NetGrid
980
981 class NetGridPanel : public wxPanel, public Reloadable<wxPanel> {
982 public:
983 NetGridPanel(wxWindow* parent, ConnectionParams& params,
984 InputOutputPanel* in_out_panel,
985 std::function<void(DataProtocol)> on_protocol_change)
986 : wxPanel(parent),
987 m_net_grid(new NetGrid(this, params, in_out_panel,
988 std::move(on_protocol_change))) {
989 SetSizer(m_net_grid);
990 wxWindowBase::Layout();
991 }
992
993 bool CheckStatus() { return m_net_grid->CheckStatus(); }
994
995 void Reload() override { m_net_grid->Reload(); }
996
997 private:
998 NetGrid* m_net_grid;
999 };
1000
1001public:
1007 [[nodiscard]] bool CheckStatus() const override {
1008 if (m_params.type != ConnectionType::NETWORK) return true;
1009 return m_grid_panel->CheckStatus();
1010 }
1011
1012protected:
1013 NetConfigPanelImpl(wxWindow* parent, ConnectionParams& params)
1014 : NetConfigPanel(parent), m_params(params), m_grid_panel(nullptr) {
1015 auto flags = wxSizerFlags().Border();
1016 auto vbox = new wxBoxSizer(wxVERTICAL);
1017 auto input_output_panel = InputOutputPanel::Create(this, m_params);
1018 m_filter_panel = FilterConfigPanel::Create(this, m_params);
1019 m_advanced_panel = new ExpandablePanel(this, m_filter_panel, [&] {
1020 GetParent()->Layout();
1021 FitInside();
1022 });
1023 auto on_proto_change = [&](DataProtocol p) {
1024 m_advanced_panel->EnableChild(p == DataProtocol::PROTO_NMEA0183);
1025 };
1026 m_grid_panel =
1027 new NetGridPanel(this, m_params, input_output_panel, on_proto_change);
1028 vbox->Add(m_grid_panel, flags.Expand());
1029 vbox->Add(input_output_panel, flags);
1030
1031 auto hbox = new wxBoxSizer(wxHORIZONTAL);
1032 hbox->Add(new wxStaticText(this, wxID_ANY, _("Advanced")));
1033 hbox->Add(m_advanced_panel);
1034 vbox->Add(hbox, flags.Expand());
1035 vbox->Add(m_filter_panel, flags.Expand());
1036
1037 SetSizer(vbox);
1038 wxWindow::Layout();
1039 wxWindow::Show();
1040 }
1041
1042private:
1043 ConnectionParams& m_params;
1044 NetGridPanel* m_grid_panel;
1045 FilterConfigPanel* m_filter_panel;
1046 ExpandablePanel* m_advanced_panel;
1047}; // NetConfigPanelImpl
1048
1049NetConfigPanel* NetConfigPanel::Create(wxWindow* parent,
1050 ConnectionParams& params) {
1051 return new NetConfigPanelImpl(parent, params);
1052}
1053
1054} // namespace conn_edit
Connection data container close to a POD struct.
Definition conn_params.h:76
An ExpandIcon together with a child window.
void EnableChild(bool enable)
Enable/disable showing the child.
Filter list editing dialog.
Custom event class for OpenCPN's notification system.
A wxTextCtrl with an initial italics help text, removed when user starts typing.
void SetHelp(const std::string &help_text)
Set help text, enter pristine state.
void RestoreHelp()
Restore help text to initial value, enter pristine state.
bool IsPristine() const
Return true if user has not entered anything.
void ChangeValue(const wxString &value) override
Falsify pristine state.
Configure input filters: accept or ignore list of sentences.
void Reload() override
Synchronize widget value(s) with parameter(s).
Configure n0183 input and output filters.
Two checkboxes for enabling port input and/or output.
bool CheckStatus() const override
Check that user data is sane, possibly run dialogs for missing or bad input.
Main network configuration: TCP/UDP, client/server, etc.
void Reload() override
Synchronize widget value(s) with parameter(s).
Select serial port combobox.
void Reload() override
Synchronize widget value(s) with parameter(s).
Entry handling "Data port" setting.
void Reload() override
Synchronize widget value(s) with parameter(s).
NMEA2000/NMEA0183 user choice.
Interface implemented by widgets reflecting a ConnectionParams parameter.
void Init(const KeyProvider &kp, const std::function< void(ObservedEvt &ev)> &action)
Initiate an object yet not listening so it invokes action when the kp key is notified.
Definition observable.h:338
void LoadSerialPorts(wxComboBox *box)
connection_edit.cpp internal support.
std::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
std::string trim(std::string s)
Strip possibly trailing and/or leading space characters in s.
Miscellaneous utilities, many of which string related.
Serial ports support, notably enumeration.
wxArrayString * EnumerateSerialPorts(void)
Enumerate all serial ports.
Suspend/resume and new devices events exchange point.