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 = NULL;
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}
171
172ConnectionParams::~ConnectionParams() {
173 // delete m_optionsPanel;
174}
175
176wxString ConnectionParams::GetSourceTypeStr() const {
177 switch (Type) {
178 case SERIAL:
179 return _("Serial");
180 case NETWORK:
181 return _("Network");
182 case INTERNAL_GPS:
183 return _("GPS");
184 case INTERNAL_BT:
185 return _("BT");
186 default:
187 return "";
188 }
189}
190
191wxString ConnectionParams::GetAddressStr() const {
192 if (Type == SERIAL)
193 return wxString::Format("%s", Port.c_str());
194 else if (Type == NETWORK)
195 return wxString::Format("%s:%d", NetworkAddress.c_str(), NetworkPort);
196 else if (Type == INTERNAL_GPS)
197 return _("Internal");
198 else if (Type == INTERNAL_BT)
199 return NetworkAddress;
200 else
201 return "";
202}
203
204// TODO: Make part of NetworkProtocol interface
205static wxString NetworkProtocolToString(NetworkProtocol NetProtocol) {
206 switch (NetProtocol) {
207 case TCP:
208 return _("TCP");
209 case UDP:
210 return _("UDP");
211 case GPSD:
212 return _("GPSD");
213 case SIGNALK:
214 return _("Signal K");
215 default:
216 return _("Undefined");
217 }
218}
219
220wxString ConnectionParams::GetParametersStr() const {
221 switch (Type) {
222 case SERIAL:
223 return wxString::Format("%d", Baudrate);
224 case NETWORK:
225 return NetworkProtocolToString(NetProtocol);
226 case INTERNAL_GPS:
227 return "GPS";
228 case INTERNAL_BT:
229 return Port;
230 default:
231 return "";
232 }
233}
234
235wxString ConnectionParams::GetIOTypeValueStr() const {
236 if (IOSelect == DS_TYPE_INPUT)
237 return _("Input");
238 else if (IOSelect == DS_TYPE_OUTPUT)
239 return _("Output");
240 else
241 return _("In/Out");
242}
243
244wxString ConnectionParams::FilterTypeToStr(ListType type,
245 FilterDirection dir) const {
246 if (dir == FILTER_INPUT) {
247 if (type == BLACKLIST)
248 return _("Reject");
249 else
250 return _("Accept");
251 } else {
252 if (type == BLACKLIST)
253 return _("Drop");
254 else
255 return _("Send");
256 }
257}
258
259wxString ConnectionParams::GetFiltersStr() const {
260 wxString istcs;
261 for (size_t i = 0; i < InputSentenceList.Count(); i++) {
262 if (i > 0) istcs.Append(",");
263 istcs.Append(InputSentenceList[i]);
264 }
265 wxString ostcs;
266 for (size_t i = 0; i < OutputSentenceList.Count(); i++) {
267 if (i > 0) ostcs.Append(",");
268 ostcs.Append(OutputSentenceList[i]);
269 }
270 wxString ret = "";
271 if (istcs.Len() > 0) {
272 ret.Append(_("In"));
273 ret.Append(wxString::Format(
274 ": %s %s", FilterTypeToStr(InputSentenceListType, FILTER_INPUT).c_str(),
275 istcs.c_str()));
276 } else
277 ret.Append(_("In: None"));
278
279 if (ostcs.Len() > 0) {
280 ret.Append(", ");
281 ret.Append(_("Out"));
282 ret.Append(wxString::Format(
283 ": %s %s",
284 FilterTypeToStr(OutputSentenceListType, FILTER_OUTPUT).c_str(),
285 ostcs.c_str()));
286 } else
287 ret.Append(_(", Out: None"));
288 return ret;
289}
290
291wxString ConnectionParams::GetDSPort() const {
292 if (Type == SERIAL)
293 return wxString::Format("Serial:%s", Port.c_str());
294 else if (Type == NETWORK) {
295 wxString proto = NetworkProtocolToString(NetProtocol);
296 return wxString::Format("%s:%s:%d", proto.c_str(), NetworkAddress.c_str(),
297 NetworkPort);
298 } else if (Type == INTERNAL_BT) {
299 return Port; // mac
300 } else
301 return "";
302}
303
304bool ConnectionParams::GetValidPort() const {
305 if (Type == SERIAL && Port == "")
306 return false;
307 else if (Type == NETWORK && (NetworkAddress == "" || !NetworkPort))
308 return false;
309 else if (Type == INTERNAL_BT && Port == "")
310 return false;
311 else
312 return true;
313}
314
315std::string ConnectionParams::GetStrippedDSPort() const {
316 if (Type == SERIAL) {
317 wxString t = wxString::Format("Serial:%s", Port.c_str());
318 wxString comx = t.AfterFirst(':').BeforeFirst(' ');
319 return comx.ToStdString();
320 } else if (Type == NETWORK) {
321 wxString proto = NetworkProtocolToString(NetProtocol);
322 wxString t = wxString::Format("%s:%s:%d", proto.c_str(),
323 NetworkAddress.c_str(), NetworkPort);
324 return t.ToStdString();
325
326 } else if (Type == SOCKETCAN) {
327 std::string rv;
328 if (!socketCAN_port.ToStdString().empty())
329 rv += "socketCAN-" + socketCAN_port.ToStdString();
330 return rv;
331 } else if (Type == INTERNAL_BT) {
332 return Port.ToStdString();
333 } else if (Type == INTERNAL_GPS) {
334 return Port.ToStdString();
335 } else
336 return "";
337}
338
339std::string ConnectionParams::GetLastDSPort() const {
340 if (Type == SERIAL) {
341 wxString sp = wxString::Format("Serial:%s", Port.c_str());
342 return sp.ToStdString();
343 } else {
344 wxString proto = NetworkProtocolToString(LastNetProtocol);
345 wxString sp = wxString::Format("%s:%s:%d", proto.c_str(),
346 LastNetworkAddress.c_str(), LastNetworkPort);
347 return sp.ToStdString();
348 }
349}
350
351bool ConnectionParams::SentencePassesFilter(const wxString& sentence,
352 FilterDirection direction) const {
353 wxArrayString filter;
354 bool listype = false;
355
356 if (direction == FILTER_INPUT) {
357 filter = InputSentenceList;
358 if (InputSentenceListType == WHITELIST) listype = true;
359 } else {
360 filter = OutputSentenceList;
361 if (OutputSentenceListType == WHITELIST) listype = true;
362 }
363 if (filter.Count() == 0) // Empty list means everything passes
364 return true;
365
366 wxString fs;
367 for (size_t i = 0; i < filter.Count(); i++) {
368 fs = filter[i];
369 switch (fs.Length()) {
370 case 2:
371 if (fs == sentence.Mid(1, 2)) return listype;
372 break;
373 case 3:
374 if (fs == sentence.Mid(3, 3)) return listype;
375 break;
376 case 5:
377 if (fs == sentence.Mid(1, 5)) return listype;
378 break;
379 default:
380 // TODO: regex patterns like ".GPZ.." or 6-character patterns
381 // are rejected in the connection settings dialogue currently
382 // experts simply edit .opencpn/opncpn.config
383 wxRegEx re(fs);
384 if (re.Matches(sentence.Mid(0, 8))) {
385 return listype;
386 }
387 break;
388 }
389 }
390 return !listype;
391}
392
393NavAddr::Bus ConnectionParams::GetCommProtocol() const {
394 if (Type == NETWORK) {
395 if (NetProtocol == SIGNALK)
396 return NavAddr::Bus::Signalk;
397 else if (NetProtocol == GPSD)
398 return NavAddr::Bus::N0183;
399 }
400
401 switch (Protocol) {
402 case PROTO_NMEA0183:
403 return NavAddr::Bus::N0183;
404 case PROTO_NMEA2000:
405 return NavAddr::Bus::N2000;
406 default:
407 return NavAddr::Bus::Undef;
408 }
409}
410
411NavAddr::Bus ConnectionParams::GetLastCommProtocol() {
412 if (Type == NETWORK) {
413 if (LastNetProtocol == SIGNALK)
414 return NavAddr::Bus::Signalk;
415 else if (LastNetProtocol == GPSD)
416 return NavAddr::Bus::N0183;
417 }
418
419 switch (LastDataProtocol) {
420 case PROTO_NMEA0183:
421 return NavAddr::Bus::N0183;
422 case PROTO_NMEA2000:
423 return NavAddr::Bus::N2000;
424 default:
425 return NavAddr::Bus::Undef;
426 }
427}
std::string GetKey() const
Return string unique for each instance.
Connection parameters.
PlugIn Object Definition/API.