OpenCPN Partial API docs
Loading...
Searching...
No Matches
conn_params.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2013 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#ifdef __MINGW32__
25#undef IPV6STRICT // mingw FTBFS fix: missing struct ip_mreq
26#include <windows.h>
27#endif
28
29// For compilers that support precompilation, includes "wx.h".
30#include <wx/wxprec.h>
31#ifndef WX_PRECOMP
32#include <wx/wx.h>
33#endif // precompiled headers
34
35#include <wx/checklst.h>
36#include <wx/combobox.h>
37#include <wx/intl.h>
38#include <wx/regex.h>
39#include <wx/statline.h>
40#include <wx/tokenzr.h>
41
42#include "model/conn_params.h"
43
44#include "ocpn_plugin.h"
45
46#if !wxUSE_XLOCALE && wxCHECK_VERSION(3, 0, 0)
47#define wxAtoi(arg) atoi(arg)
48#endif
49
50static std::vector<ConnectionParams*> the_connection_params;
51
52std::vector<ConnectionParams*>& TheConnectionParams() {
53 return the_connection_params;
54}
55
56ConnectionParams::ConnectionParams(const wxString& configStr) {
57 m_optionsPanel = nullptr;
58 Deserialize(configStr);
59}
60
61void ConnectionParams::Deserialize(const wxString& configStr) {
62 Valid = true;
63 wxArrayString prms = wxStringTokenize(configStr, ";");
64 if (prms.Count() < 18) {
65 Valid = false;
66 return;
67 }
68
69 Type = (ConnectionType)wxAtoi(prms[0]);
70 NetProtocol = (NetworkProtocol)wxAtoi(prms[1]);
71 NetworkAddress = prms[2];
72 NetworkPort = (ConnectionType)wxAtoi(prms[3]);
73 Protocol = (DataProtocol)wxAtoi(prms[4]);
74 Port = prms[5];
75 Baudrate = wxAtoi(prms[6]);
76 ChecksumCheck = wxAtoi(prms[7]);
77 int iotval = wxAtoi(prms[8]);
78 IOSelect = ((iotval <= 2) ? static_cast<dsPortType>(iotval) : DS_TYPE_INPUT);
79 InputSentenceListType = (ListType)wxAtoi(prms[9]);
80 InputSentenceList = wxStringTokenize(prms[10], ",");
81 OutputSentenceListType = (ListType)wxAtoi(prms[11]);
82 OutputSentenceList = wxStringTokenize(prms[12], ",");
83 Garmin = !!wxAtoi(prms[14]);
84 GarminUpload = !!wxAtoi(prms[15]);
85 FurunoGP3X = !!wxAtoi(prms[16]);
86
87 bEnabled = true;
88 LastNetworkPort = 0;
89 b_IsSetup = false;
90 if (prms.Count() >= 18) {
91 bEnabled = !!wxAtoi(prms[17]);
92 }
93 if (prms.Count() >= 19) {
94 UserComment = prms[18];
95 }
96 if (prms.Count() >= 20) {
97 AutoSKDiscover = !!wxAtoi(prms[19]);
98 }
99 if (prms.Count() >= 21) {
100 socketCAN_port = prms[20];
101 }
102 if (prms.Count() >= 22) {
103 NoDataReconnect = wxAtoi(prms[21]);
104 }
105 if (prms.Count() >= 23) {
106 DisableEcho = wxAtoi(prms[22]);
107 }
108 if (prms.Count() >= 24) {
109 AuthToken = prms[23];
110 }
111}
112
113wxString ConnectionParams::Serialize() const {
114 wxString istcs;
115 for (size_t i = 0; i < InputSentenceList.Count(); i++) {
116 if (i > 0) istcs.Append(",");
117 istcs.Append(InputSentenceList[i]);
118 }
119 wxString ostcs;
120 for (size_t i = 0; i < OutputSentenceList.Count(); i++) {
121 if (i > 0) ostcs.Append(",");
122 ostcs.Append(OutputSentenceList[i]);
123 }
124 wxString ret = wxString::Format(
125 "%d;%d;%s;%d;%d;%s;%d;%d;%d;%d;%s;%d;%s;%d;%d;%d;%d;%d;%s;%d;%s;%d;%d;%s",
126 Type, NetProtocol, NetworkAddress.c_str(), NetworkPort, Protocol,
127 Port.c_str(), Baudrate, ChecksumCheck, IOSelect, InputSentenceListType,
128 istcs.c_str(), OutputSentenceListType, ostcs.c_str(), 0 /* Priority */,
129 Garmin, GarminUpload, FurunoGP3X, bEnabled, UserComment.c_str(),
130 AutoSKDiscover, socketCAN_port.c_str(), NoDataReconnect, DisableEcho,
131 AuthToken.c_str());
132
133 return ret;
134}
135
136std::string ConnectionParams::GetKey() const {
137 std::stringstream ss;
138 ss << Type << NetProtocol << NetworkAddress << NetworkPort << Protocol << Port
139 << Baudrate << ChecksumCheck << IOSelect << InputSentenceListType
140 << OutputSentenceListType << Garmin << GarminUpload << FurunoGP3X
141 << UserComment << AutoSKDiscover << socketCAN_port << NoDataReconnect
142 << DisableEcho << AuthToken;
143 for (const auto& sentence : OutputSentenceList) ss << sentence;
144 for (const auto& sentence : InputSentenceList) ss << sentence;
145 return ss.str();
146}
147
148ConnectionParams::ConnectionParams() {
149 Type = UNKNOWN;
150 NetProtocol = TCP;
151 NetworkAddress = "";
152 NetworkPort = 0;
153 Protocol = PROTO_NMEA0183;
154 Port = "";
155 Baudrate = 4800;
156 ChecksumCheck = true;
157 Garmin = false;
158 FurunoGP3X = false;
159 IOSelect = DS_TYPE_INPUT;
160 InputSentenceListType = WHITELIST;
161 OutputSentenceListType = WHITELIST;
162 Valid = true;
163 bEnabled = true;
164 b_IsSetup = false;
165 m_optionsPanel = NULL;
166 AutoSKDiscover = false;
167 NoDataReconnect = false;
168 DisableEcho = false;
169 AuthToken = "";
170 is_server = false;
171}
172
173ConnectionParams::~ConnectionParams() {
174 // delete m_optionsPanel;
175}
176
177wxString ConnectionParams::GetSourceTypeStr() const {
178 switch (Type) {
179 case SERIAL:
180 return _("Serial");
181 case NETWORK:
182 return _("Network");
183 case INTERNAL_GPS:
184 return _("GPS");
185 case INTERNAL_BT:
186 return _("BT");
187 default:
188 return "";
189 }
190}
191
192wxString ConnectionParams::GetAddressStr() const {
193 if (Type == SERIAL)
194 return wxString::Format("%s", Port.c_str());
195 else if (Type == NETWORK)
196 return wxString::Format("%s:%d", NetworkAddress.c_str(), NetworkPort);
197 else if (Type == INTERNAL_GPS)
198 return _("Internal");
199 else if (Type == INTERNAL_BT)
200 return NetworkAddress;
201 else
202 return "";
203}
204
205// TODO: Make part of NetworkProtocol interface
206static wxString NetworkProtocolToString(NetworkProtocol NetProtocol) {
207 switch (NetProtocol) {
208 case TCP:
209 return _("TCP");
210 case UDP:
211 return _("UDP");
212 case GPSD:
213 return _("GPSD");
214 case SIGNALK:
215 return _("Signal K");
216 default:
217 return _("Undefined");
218 }
219}
220
221wxString ConnectionParams::GetParametersStr() const {
222 switch (Type) {
223 case SERIAL:
224 return wxString::Format("%d", Baudrate);
225 case NETWORK:
226 return NetworkProtocolToString(NetProtocol);
227 case INTERNAL_GPS:
228 return "GPS";
229 case INTERNAL_BT:
230 return Port;
231 default:
232 return "";
233 }
234}
235
236wxString ConnectionParams::GetIOTypeValueStr() const {
237 if (IOSelect == DS_TYPE_INPUT)
238 return _("Input");
239 else if (IOSelect == DS_TYPE_OUTPUT)
240 return _("Output");
241 else
242 return _("In/Out");
243}
244
245wxString ConnectionParams::FilterTypeToStr(ListType type,
246 FilterDirection dir) const {
247 if (dir == FILTER_INPUT) {
248 if (type == BLACKLIST)
249 return _("Reject");
250 else
251 return _("Accept");
252 } else {
253 if (type == BLACKLIST)
254 return _("Drop");
255 else
256 return _("Send");
257 }
258}
259
260wxString ConnectionParams::GetFiltersStr() const {
261 wxString istcs;
262 for (size_t i = 0; i < InputSentenceList.Count(); i++) {
263 if (i > 0) istcs.Append(",");
264 istcs.Append(InputSentenceList[i]);
265 }
266 wxString ostcs;
267 for (size_t i = 0; i < OutputSentenceList.Count(); i++) {
268 if (i > 0) ostcs.Append(",");
269 ostcs.Append(OutputSentenceList[i]);
270 }
271 wxString ret = "";
272 if (istcs.Len() > 0) {
273 ret.Append(_("In"));
274 ret.Append(wxString::Format(
275 ": %s %s", FilterTypeToStr(InputSentenceListType, FILTER_INPUT).c_str(),
276 istcs.c_str()));
277 } else
278 ret.Append(_("In: None"));
279
280 if (ostcs.Len() > 0) {
281 ret.Append(", ");
282 ret.Append(_("Out"));
283 ret.Append(wxString::Format(
284 ": %s %s",
285 FilterTypeToStr(OutputSentenceListType, FILTER_OUTPUT).c_str(),
286 ostcs.c_str()));
287 } else
288 ret.Append(_(", Out: None"));
289 return ret;
290}
291
292wxString ConnectionParams::GetDSPort() const {
293 if (Type == SERIAL)
294 return wxString::Format("Serial:%s", Port.c_str());
295 else if (Type == NETWORK) {
296 wxString proto = NetworkProtocolToString(NetProtocol);
297 return wxString::Format("%s:%s:%d", proto.c_str(), NetworkAddress.c_str(),
298 NetworkPort);
299 } else if (Type == INTERNAL_BT) {
300 return Port; // mac
301 } else
302 return "";
303}
304
305bool ConnectionParams::GetValidPort() const {
306 if (Type == SERIAL && Port == "")
307 return false;
308 else if (Type == NETWORK && (NetworkAddress == "" || !NetworkPort))
309 return false;
310 else if (Type == INTERNAL_BT && Port == "")
311 return false;
312 else
313 return true;
314}
315
316std::string ConnectionParams::GetStrippedDSPort() const {
317 if (Type == SERIAL) {
318 wxString t = wxString::Format("Serial:%s", Port.c_str());
319 wxString comx = t.AfterFirst(':').BeforeFirst(' ');
320 return comx.ToStdString();
321 } else if (Type == NETWORK) {
322 wxString proto = NetworkProtocolToString(NetProtocol);
323 wxString t = wxString::Format("%s:%s:%d", proto.c_str(),
324 NetworkAddress.c_str(), NetworkPort);
325 return t.ToStdString();
326
327 } else if (Type == SOCKETCAN) {
328 std::string rv;
329 if (!socketCAN_port.ToStdString().empty())
330 rv += "socketCAN-" + socketCAN_port.ToStdString();
331 return rv;
332 } else if (Type == INTERNAL_BT) {
333 return Port.ToStdString();
334 } else if (Type == INTERNAL_GPS) {
335 return Port.ToStdString();
336 } else
337 return "";
338}
339
340std::string ConnectionParams::GetLastDSPort() const {
341 if (Type == SERIAL) {
342 wxString sp = wxString::Format("Serial:%s", Port.c_str());
343 return sp.ToStdString();
344 } else {
345 wxString proto = NetworkProtocolToString(LastNetProtocol);
346 wxString sp = wxString::Format("%s:%s:%d", proto.c_str(),
347 LastNetworkAddress.c_str(), LastNetworkPort);
348 return sp.ToStdString();
349 }
350}
351
352bool ConnectionParams::SentencePassesFilter(const wxString& sentence,
353 FilterDirection direction) const {
354 wxArrayString filter;
355 bool listype = false;
356
357 if (direction == FILTER_INPUT) {
358 filter = InputSentenceList;
359 if (InputSentenceListType == WHITELIST) listype = true;
360 } else {
361 filter = OutputSentenceList;
362 if (OutputSentenceListType == WHITELIST) listype = true;
363 }
364 if (filter.Count() == 0) // Empty list means everything passes
365 return true;
366
367 wxString fs;
368 for (size_t i = 0; i < filter.Count(); i++) {
369 fs = filter[i];
370 switch (fs.Length()) {
371 case 2:
372 if (fs == sentence.Mid(1, 2)) return listype;
373 break;
374 case 3:
375 if (fs == sentence.Mid(3, 3)) return listype;
376 break;
377 case 5:
378 if (fs == sentence.Mid(1, 5)) return listype;
379 break;
380 default:
381 // TODO: regex patterns like ".GPZ.." or 6-character patterns
382 // are rejected in the connection settings dialogue currently
383 // experts simply edit .opencpn/opncpn.config
384 wxRegEx re(fs);
385 if (re.Matches(sentence.Mid(0, 8))) {
386 return listype;
387 }
388 break;
389 }
390 }
391 return !listype;
392}
393
394NavAddr::Bus ConnectionParams::GetCommProtocol() const {
395 if (Type == NETWORK) {
396 if (NetProtocol == SIGNALK)
397 return NavAddr::Bus::Signalk;
398 else if (NetProtocol == GPSD)
399 return NavAddr::Bus::N0183;
400 }
401
402 switch (Protocol) {
403 case PROTO_NMEA0183:
404 return NavAddr::Bus::N0183;
405 case PROTO_NMEA2000:
406 return NavAddr::Bus::N2000;
407 default:
408 return NavAddr::Bus::Undef;
409 }
410}
411
412NavAddr::Bus ConnectionParams::GetLastCommProtocol() {
413 if (Type == NETWORK) {
414 if (LastNetProtocol == SIGNALK)
415 return NavAddr::Bus::Signalk;
416 else if (LastNetProtocol == GPSD)
417 return NavAddr::Bus::N0183;
418 }
419
420 switch (LastDataProtocol) {
421 case PROTO_NMEA0183:
422 return NavAddr::Bus::N0183;
423 case PROTO_NMEA2000:
424 return NavAddr::Bus::N2000;
425 default:
426 return NavAddr::Bus::Undef;
427 }
428}
std::string GetKey() const
Return string unique for each instance.
Connection parameters.
PlugIn Object Definition/API.