36#include <wx/checklst.h>
37#include <wx/combobox.h>
40#include <wx/statline.h>
41#include <wx/tokenzr.h>
43#include "conn_params.h"
45#include "ocpn_frame.h"
46#include "ocpn_plugin.h"
49#if !wxUSE_XLOCALE && wxCHECK_VERSION(3, 0, 0)
50#define wxAtoi(arg) atoi(arg)
53static wxArrayOfConnPrm* the_connection_params = 0;
55wxArrayOfConnPrm* TheConnectionParams() {
56 if (the_connection_params == 0)
57 the_connection_params =
new wxArrayOfConnPrm();
58 return the_connection_params;
61ConnectionParams::ConnectionParams(
const wxString &configStr) {
62 m_optionsPanel = NULL;
63 Deserialize(configStr);
66void ConnectionParams::Deserialize(
const wxString &configStr) {
68 wxArrayString prms = wxStringTokenize(configStr, _T(
";"));
69 if (prms.Count() < 18) {
74 Type = (ConnectionType)wxAtoi(prms[0]);
75 NetProtocol = (NetworkProtocol)wxAtoi(prms[1]);
76 NetworkAddress = prms[2];
77 NetworkPort = (ConnectionType)wxAtoi(prms[3]);
78 Protocol = (DataProtocol)wxAtoi(prms[4]);
80 Baudrate = wxAtoi(prms[6]);
81 ChecksumCheck = wxAtoi(prms[7]);
82 int iotval = wxAtoi(prms[8]);
83 IOSelect = ((iotval <= 2) ? static_cast<dsPortType>(iotval) : DS_TYPE_INPUT);
84 InputSentenceListType = (ListType)wxAtoi(prms[9]);
85 InputSentenceList = wxStringTokenize(prms[10], _T(
","));
86 OutputSentenceListType = (ListType)wxAtoi(prms[11]);
87 OutputSentenceList = wxStringTokenize(prms[12], _T(
","));
88 Priority = wxAtoi(prms[13]);
89 Garmin = !!wxAtoi(prms[14]);
90 GarminUpload = !!wxAtoi(prms[15]);
91 FurunoGP3X = !!wxAtoi(prms[16]);
96 if (prms.Count() >= 18) {
97 bEnabled = !!wxAtoi(prms[17]);
99 if (prms.Count() >= 19) {
100 UserComment = prms[18];
102 if (prms.Count() >= 20) {
103 AutoSKDiscover = !!wxAtoi(prms[19]);
105 if (prms.Count() >= 21) {
106 socketCAN_port = prms[20];
110wxString ConnectionParams::Serialize()
const {
112 for (
size_t i = 0; i < InputSentenceList.Count(); i++) {
113 if (i > 0) istcs.Append(_T(
","));
114 istcs.Append(InputSentenceList[i]);
117 for (
size_t i = 0; i < OutputSentenceList.Count(); i++) {
118 if (i > 0) ostcs.Append(_T(
","));
119 ostcs.Append(OutputSentenceList[i]);
121 wxString ret = wxString::Format(
122 _T(
"%d;%d;%s;%d;%d;%s;%d;%d;%d;%d;%s;%d;%s;%d;%d;%d;%d;%d;%s;%d;%s"), Type,
123 NetProtocol, NetworkAddress.c_str(), NetworkPort, Protocol, Port.c_str(),
124 Baudrate, ChecksumCheck, IOSelect, InputSentenceListType, istcs.c_str(),
125 OutputSentenceListType, ostcs.c_str(), Priority, Garmin, GarminUpload,
126 FurunoGP3X, bEnabled, UserComment.c_str(), AutoSKDiscover, socketCAN_port.c_str());
131ConnectionParams::ConnectionParams() {
134 NetworkAddress = wxEmptyString;
136 Protocol = PROTO_NMEA0183;
137 Port = wxEmptyString;
139 ChecksumCheck =
true;
142 IOSelect = DS_TYPE_INPUT;
143 InputSentenceListType = WHITELIST;
144 OutputSentenceListType = WHITELIST;
149 m_optionsPanel = NULL;
150 AutoSKDiscover =
false;
153ConnectionParams::~ConnectionParams() {
157wxString ConnectionParams::GetSourceTypeStr()
const {
172wxString ConnectionParams::GetAddressStr()
const {
174 return wxString::Format(_T(
"%s"), Port.c_str());
175 else if (Type == NETWORK)
176 return wxString::Format(_T(
"%s:%d"), NetworkAddress.c_str(), NetworkPort);
177 else if (Type == INTERNAL_GPS)
178 return _(
"Internal");
179 else if (Type == INTERNAL_BT)
180 return NetworkAddress;
186static wxString NetworkProtocolToString(NetworkProtocol NetProtocol) {
187 switch (NetProtocol) {
195 return _(
"Signal K");
197 return _(
"Undefined");
201wxString ConnectionParams::GetParametersStr()
const {
204 return wxString::Format(_T(
"%d"), Baudrate);
206 return NetworkProtocolToString(NetProtocol);
216wxString ConnectionParams::GetIOTypeValueStr()
const {
217 if (IOSelect == DS_TYPE_INPUT)
219 else if (IOSelect == DS_TYPE_OUTPUT)
225wxString ConnectionParams::FilterTypeToStr(ListType type,
226 FilterDirection dir)
const {
227 if (dir == FILTER_INPUT) {
228 if (type == BLACKLIST)
233 if (type == BLACKLIST)
240wxString ConnectionParams::GetFiltersStr()
const {
242 for (
size_t i = 0; i < InputSentenceList.Count(); i++) {
243 if (i > 0) istcs.Append(_T(
","));
244 istcs.Append(InputSentenceList[i]);
247 for (
size_t i = 0; i < OutputSentenceList.Count(); i++) {
248 if (i > 0) ostcs.Append(_T(
","));
249 ostcs.Append(OutputSentenceList[i]);
251 wxString ret = wxEmptyString;
252 if (istcs.Len() > 0) {
254 ret.Append(wxString::Format(
256 FilterTypeToStr(InputSentenceListType, FILTER_INPUT).c_str(),
259 ret.Append(_(
"In: None"));
261 if (ostcs.Len() > 0) {
262 ret.Append(_T(
", "));
263 ret.Append(_(
"Out"));
264 ret.Append(wxString::Format(
266 FilterTypeToStr(OutputSentenceListType, FILTER_OUTPUT).c_str(),
269 ret.Append(_(
", Out: None"));
273wxString ConnectionParams::GetDSPort()
const {
275 return wxString::Format(_T(
"Serial:%s"), Port.c_str());
276 else if (Type == NETWORK) {
277 wxString proto = NetworkProtocolToString(NetProtocol);
278 return wxString::Format(_T(
"%s:%s:%d"), proto.c_str(),
279 NetworkAddress.c_str(), NetworkPort);
280 }
else if (Type == INTERNAL_BT) {
286std::string ConnectionParams::GetStrippedDSPort() {
288 wxString t = wxString::Format(_T(
"Serial:%s"), Port.c_str());
289 wxString comx = t.AfterFirst(
':').BeforeFirst(
' ');
290 return comx.ToStdString();
292 else if (Type == NETWORK) {
293 wxString proto = NetworkProtocolToString(NetProtocol);
294 wxString t = wxString::Format(_T(
"%s:%s:%d"), proto.c_str(),
295 NetworkAddress.c_str(), NetworkPort);
296 return t.ToStdString();
298 }
else if (Type == SOCKETCAN) {
306 }
else if (Type == INTERNAL_BT) {
307 return Port.ToStdString();
312std::string ConnectionParams::GetLastDSPort()
const {
314 wxString sp = wxString::Format(_T(
"Serial:%s"), Port.c_str());
315 return sp.ToStdString();
318 wxString proto = NetworkProtocolToString(LastNetProtocol);
319 wxString sp = wxString::Format(_T(
"%s:%s:%d"), proto.c_str(),
320 LastNetworkAddress.c_str(), LastNetworkPort);
321 return sp.ToStdString();
325bool ConnectionParams::SentencePassesFilter(
const wxString& sentence, FilterDirection direction)
327 wxArrayString filter;
328 bool listype =
false;
330 if (direction == FILTER_INPUT)
332 filter = InputSentenceList;
333 if (InputSentenceListType == WHITELIST)
338 filter = OutputSentenceList;
339 if (OutputSentenceListType == WHITELIST)
342 if (filter.Count() == 0)
346 for (
size_t i = 0; i < filter.Count(); i++)
352 if (fs == sentence.Mid(1, 2))
356 if (fs == sentence.Mid(3, 3))
360 if (fs == sentence.Mid(1, 5))
368 if (re.Matches(sentence.Mid(0, 8)))
378NavAddr::Bus ConnectionParams::GetCommProtocol(){
379 if (Type == NETWORK){
380 if (NetProtocol == SIGNALK)
381 return NavAddr::Bus::Signalk;
382 else if (NetProtocol == UDP)
383 return NavAddr::Bus::N0183;
384 else if (NetProtocol == TCP)
385 return NavAddr::Bus::N0183;
386 else if (NetProtocol == GPSD)
387 return NavAddr::Bus::N0183;
392 return NavAddr::Bus::N0183;
394 return NavAddr::Bus::N2000;
396 return NavAddr::Bus::Undef;
400NavAddr::Bus ConnectionParams::GetLastCommProtocol(){
401 if (Type == NETWORK){
402 if (LastNetProtocol == SIGNALK)
403 return NavAddr::Bus::Signalk;
404 else if (LastNetProtocol == UDP)
405 return NavAddr::Bus::N0183;
406 else if (LastNetProtocol == TCP)
407 return NavAddr::Bus::N0183;
408 else if (LastNetProtocol == GPSD)
409 return NavAddr::Bus::N0183;
412 switch (LastDataProtocol){
414 return NavAddr::Bus::N0183;
416 return NavAddr::Bus::N2000;
418 return NavAddr::Bus::Undef;