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