OpenCPN Partial API docs
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_registry.h"
37 
38 bool StopAndRemoveCommDriver(std::string ident, NavAddr::Bus _bus) {
39  auto& registry = CommDriverRegistry::GetInstance();
40  const std::vector<DriverPtr>& drivers = registry.GetDrivers();
41  DriverPtr target_driver = FindDriver(drivers, ident, _bus);
42 
43  if (!target_driver) return false;
44 
45  // Deactivate the driver, and the last reference in shared_ptr
46  // will be removed.
47  // The driver DTOR will be called in due course.
48  registry.Deactivate(target_driver);
49 
50  return true;
51 }
52 
53 //----------------------------------------------------------------------------------
54 // Strip NMEA V4 tags from NMEA0183 message
55 //----------------------------------------------------------------------------------
56 wxString ProcessNMEA4Tags(wxString& msg) {
57  int idxFirst = msg.Find('\\');
58 
59  if (wxNOT_FOUND == idxFirst) return msg;
60 
61  if (idxFirst < (int)msg.Length() - 1) {
62  int idxSecond = msg.Mid(idxFirst + 1).Find('\\') + 1;
63  if (wxNOT_FOUND != idxSecond) {
64  if (idxSecond < (int)msg.Length() - 1) {
65  // wxString tag = msg.Mid(idxFirst+1, (idxSecond - idxFirst) -1);
66  return msg.Mid(idxSecond + 1);
67  }
68  }
69  }
70 
71  return msg;
72 }