OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_util.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: Implement comm_util.h -- communication driver utilities
5 * Author: David Register, Alec Leamas
6 *
7 ***************************************************************************
8 * Copyright (C) 2022 by David Register, Alec Leamas *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25// For compilers that support precompilation, includes "wx.h".
26#include <wx/wxprec.h>
27
28#ifndef WX_PRECOMP
29#include <wx/wx.h>
30#endif // precompiled headers
31
32#include <vector>
33#include <string>
34
35#include "model/comm_util.h"
36#include "model/comm_drv_factory.h"
38#include "model/conn_params.h"
39
40void UpdateDatastreams() {
41 // Recreate datastreams that are new, or have been edited
42 std::vector<std::string> enabled_conns;
43
44 for (auto* cp : TheConnectionParams()) {
45 // Connection already setup?
46 if (cp->b_IsSetup) {
47 if (cp->bEnabled) {
48 enabled_conns.push_back(cp->GetStrippedDSPort());
49 }
50 continue;
51 }
52
53 // Check to see if this connection port has been
54 // already enabled in this loop.
55 // If so, then leave this connection alone.
56 // This will handle multiple connections with same port,
57 // but possibly different filters
58 // Also protect against some user config errors
59 if (std::find(enabled_conns.begin(), enabled_conns.end(),
60 cp->GetStrippedDSPort()) != enabled_conns.end()) {
61 continue;
62 }
63
64 // Terminate and remove any existing driver, if present in registry
65 StopAndRemoveCommDriver(cp->GetStrippedDSPort(), cp->GetCommProtocol());
66
67 // Stop and remove "previous" port, in case other params have changed.
68 StopAndRemoveCommDriver(cp->GetLastDSPort(), cp->GetLastCommProtocol());
69
70 // Internal BlueTooth driver stacks commonly need a time delay to purge
71 // their buffers, etc. before restating with new parameters...
72 if (cp->Type == INTERNAL_BT) wxSleep(1);
73
74 // Connection has been disabled
75 if (!cp->bEnabled) continue;
76
77 // Make any new or re-enabled drivers
78 MakeCommDriver(cp);
79 cp->b_IsSetup = TRUE;
80 enabled_conns.push_back(cp->GetStrippedDSPort());
81 }
82}
83
84bool StopAndRemoveCommDriver(std::string ident, NavAddr::Bus _bus) {
85 auto& registry = CommDriverRegistry::GetInstance();
86 const std::vector<DriverPtr>& drivers = registry.GetDrivers();
87 DriverPtr& target_driver = FindDriver(drivers, ident, _bus);
88
89 if (!target_driver) return false;
90
91 // Deactivate the driver, and the last reference in shared_ptr
92 // will be removed.
93 // The driver DTOR will be called in due course.
94 registry.Deactivate(target_driver);
95
96 return true;
97}
98
107wxString ProcessNMEA4Tags(const wxString& msg) {
108 int idxFirst = msg.Find('\\');
109
110 if (wxNOT_FOUND == idxFirst) return msg;
111
112 if (idxFirst < (int)msg.Length() - 1) {
113 int idxSecond = msg.Mid(idxFirst + 1).Find('\\') + 1;
114 if (wxNOT_FOUND != idxSecond) {
115 if (idxSecond < (int)msg.Length() - 1) {
116 // wxString tag = msg.Mid(idxFirst+1, (idxSecond - idxFirst) -1);
117 return msg.Mid(idxSecond + 1);
118 }
119 }
120 }
121
122 return msg;
123}
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.