OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_util.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 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
25#include <vector>
26#include <string>
27
28// For compilers that support precompilation, includes "wx.h".
29#include <wx/wxprec.h>
30#ifndef WX_PRECOMP
31#include <wx/wx.h>
32#endif
33
34#include "model/comm_util.h"
37#include "model/conn_params.h"
38
39void UpdateDatastreams() {
40 // Recreate datastreams that are new, or have been edited
41 std::vector<std::string> enabled_conns;
42
43 for (auto* cp : TheConnectionParams()) {
44 // Connection already setup?
45 if (cp->b_IsSetup) {
46 if (cp->bEnabled) {
47 enabled_conns.push_back(cp->GetStrippedDSPort());
48 }
49 continue;
50 }
51
52 // Check to see if this connection port has been
53 // already enabled in this loop.
54 // If so, then leave this connection alone.
55 // This will handle multiple connections with same port,
56 // but possibly different filters
57 // Also protect against some user config errors
58 if (std::find(enabled_conns.begin(), enabled_conns.end(),
59 cp->GetStrippedDSPort()) != enabled_conns.end()) {
60 continue;
61 }
62
63 // Terminate and remove any existing driver, if present in registry
64 StopAndRemoveCommDriver(cp->GetStrippedDSPort(), cp->GetCommProtocol());
65
66 // Stop and remove "previous" port, in case other params have changed.
67 StopAndRemoveCommDriver(cp->GetLastDSPort(), cp->GetLastCommProtocol());
68
69 // Internal BlueTooth driver stacks commonly need a time delay to purge
70 // their buffers, etc. before restating with new parameters...
71 if (cp->Type == INTERNAL_BT) wxSleep(1);
72
73 // Connection has been disabled
74 if (!cp->bEnabled) continue;
75
76 // Make any new or re-enabled drivers
78 cp->b_IsSetup = TRUE;
79 enabled_conns.push_back(cp->GetStrippedDSPort());
80 }
81}
82
83bool StopAndRemoveCommDriver(std::string ident, NavAddr::Bus _bus) {
84 auto& registry = CommDriverRegistry::GetInstance();
85 const std::vector<DriverPtr>& drivers = registry.GetDrivers();
86 DriverPtr& target_driver = FindDriver(drivers, ident, _bus);
87
88 if (!target_driver) return false;
89
90 // Deactivate the driver, and the last reference in shared_ptr
91 // will be removed.
92 // The driver DTOR will be called in due course.
93 registry.Deactivate(target_driver);
94
95 return true;
96}
97
106wxString ProcessNMEA4Tags(const wxString& msg) {
107 int idxFirst = msg.Find('\\');
108
109 if (wxNOT_FOUND == idxFirst) return msg;
110
111 if (idxFirst < (int)msg.Length() - 1) {
112 int idxSecond = msg.Mid(idxFirst + 1).Find('\\') + 1;
113 if (wxNOT_FOUND != idxSecond) {
114 if (idxSecond < (int)msg.Length() - 1) {
115 // wxString tag = msg.Mid(idxFirst+1, (idxSecond - idxFirst) -1);
116 return msg.Mid(idxSecond + 1);
117 }
118 }
119 }
120
121 return msg;
122}
void MakeCommDriver(const ConnectionParams *params)
Create and register a driver for given connection.
Communication drivers factory and support.
DriverPtr & FindDriver(const std::vector< DriverPtr > &drivers, const std::string &iface, const NavAddr::Bus _bus)
Search list of drivers for a driver with given interface string.
Driver registration container, a singleton.
wxString ProcessNMEA4Tags(const wxString &msg)
Strip NMEA V4 tag blocks from NMEA0183 message.
Misc driver utilities.
Connection parameters.