OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_factory.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 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, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 **************************************************************************/
20
26// FIXME Why is this needed?
27#ifdef __MSVC__
28#include <winsock2.h>
29#include <wx/msw/winundef.h>
30#endif
31
32#include <wx/wxprec.h>
33
34#ifndef WX_PRECOMP
35#include <wx/wx.h>
36#endif // precompiled headers
37
38#include "model/comm_util.h"
39#include "model/comm_drv_loopback.h"
40#include "model/comm_drv_n2k_net.h"
41#include "model/comm_drv_n2k_serial.h"
44#include "model/comm_drv_signalk_net.h"
45#include "model/comm_drv_n0183_android_int.h"
46#include "model/comm_drv_n0183_android_bt.h"
49
50#if defined(__linux__) && !defined(__ANDROID__) && !defined(__WXOSX__)
51#include "model/comm_drv_n2k_socketcan.h"
52#endif
53
55public:
56 N0183Listener() = default;
57
59 void Notify(const AbstractCommDriver& driver) override {}
60
61 void Notify(std::shared_ptr<const NavMsg> message) override {
62 switch (message->state) {
63 case NavMsg::State::kCannotParse:
64 case NavMsg::State::kFiltered:
65 case NavMsg::State::kBadChecksum:
66 CommDriverRegistry::GetInstance().evt_dropped_msg.Notify(message);
67 break;
68 default:
69 NavMsgBus::GetInstance().Notify(message);
70 break;
71 }
72 }
73};
74
76 auto driver = std::make_unique<LoopbackDriver>(NavMsgBus::GetInstance());
77 CommDriverRegistry::GetInstance().Activate(std::move(driver));
78}
79
80void MakeCommDriver(const ConnectionParams* params) {
81 static N0183Listener listener;
82
83 wxLogMessage("MakeCommDriver: %s", params->GetDSPort().c_str());
84
85 auto& msgbus = NavMsgBus::GetInstance();
86 auto& registry = CommDriverRegistry::GetInstance();
87 switch (params->Type) {
88 case SERIAL:
89 switch (params->Protocol) {
90 case PROTO_NMEA2000: {
91 auto driver = std::make_unique<CommDriverN2KSerial>(params, msgbus);
92 registry.Activate(std::move(driver));
93 break;
94 }
95 default: {
96 auto driver =
97 std::make_unique<CommDriverN0183Serial>(params, listener);
98 registry.Activate(std::move(driver));
99 break;
100 }
101 }
102 break;
103 case NETWORK:
104 switch (params->NetProtocol) {
105 case SIGNALK: {
106 auto driver = std::make_unique<CommDriverSignalKNet>(params, msgbus);
107 registry.Activate(std::move(driver));
108 break;
109 }
110 default: {
111 switch (params->Protocol) {
112 case PROTO_NMEA0183: {
113 auto driver =
114 std::make_unique<CommDriverN0183Net>(params, listener);
115 registry.Activate(std::move(driver));
116 break;
117 }
118 case PROTO_NMEA2000: {
119 auto driver = std::make_unique<CommDriverN2KNet>(params, msgbus);
120 registry.Activate(std::move(driver));
121 break;
122 }
123 default:
124 break;
125 }
126 break;
127 }
128 }
129
130 break;
131#if defined(__linux__) && !defined(__ANDROID__) && !defined(__WXOSX__)
132 case SOCKETCAN: {
133 auto driver = CommDriverN2KSocketCAN::Create(params, msgbus);
134 registry.Activate(std::move(driver));
135 break;
136 }
137#endif
138
139#ifdef __ANDROID__
140 case INTERNAL_GPS: {
141 auto driver = std::make_unique<CommDriverN0183AndroidInt>(params, msgbus);
142 registry.Activate(std::move(driver));
143 break;
144 }
145
146 case INTERNAL_BT: {
147 auto driver = std::make_unique<CommDriverN0183AndroidBT>(params, msgbus);
148 registry.Activate(std::move(driver));
149 break;
150 }
151#endif
152
153 default:
154 break;
155 }
156};
157
158void initIXNetSystem() { CommDriverSignalKNet::initIXNetSystem(); };
159
160void uninitIXNetSystem() { CommDriverSignalKNet::uninitIXNetSystem(); };
Common interface for all drivers.
Definition comm_driver.h:67
EventVar evt_dropped_msg
Updated by drivers with a shared Navmsg pointer when receiving data otherwise dropped,...
void Activate(DriverPtr driver)
Add driver to list of active drivers.
Interface for handling incoming messages.
Definition comm_driver.h:52
void Notify() override
Notify all listeners, no data supplied.
void Notify(std::shared_ptr< const NavMsg > message) override
Handle a received message.
void Notify(const AbstractCommDriver &driver) override
Handle driver status change.
void Notify(std::shared_ptr< const NavMsg > message)
Accept message received by driver, make it available for upper layers.
void MakeLoopbackDriver()
Create and register the loopback driver.
void MakeCommDriver(const ConnectionParams *params)
Create and register a driver for given connection.
NMEA0183 over IP driver.
NMEA0183 serial driver.
Driver registration container, a singleton.
Raw messages layer, supports sending and recieving navmsg messages.