OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_n2k_net.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2023 by David Register *
3 * Copyright (C) 2023 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#ifndef _COMMDRIVERN2KNET_H
26#define _COMMDRIVERN2KNET_H
27
28#ifndef __WXMSW__
29#include <sys/socket.h> // needed for (some) Mac builds
30#include <netinet/in.h>
31#endif
32
33#include <wx/wxprec.h>
34#ifndef WX_PRECOMP
35#include <wx/wx.h>
36#endif
37
38#include <wx/datetime.h>
39
40#include "model/comm_can_util.h"
41#include "model/comm_drv_n2k.h"
44#include "model/conn_params.h"
45
46#ifdef __WXGTK__
47// newer versions of glib define its own GSocket but we unfortunately use this
48// name in our own (semi-)public header and so can't change it -- rename glib
49// one instead
50#define GSocket GlibGSocket
51#include <wx/socket.h>
52#undef GSocket
53#else
54#include <wx/socket.h>
55#endif
56
57#define RX_BUFFER_SIZE_NET 4096
58
59#define ESCAPE 0x10
60#define STARTOFTEXT 0x02
61#define ENDOFTEXT 0x03
62
63#define MsgTypeN2kData 0x93
64#define MsgTypeN2kRequest 0x94
65
66typedef enum {
67 N2KFormat_Undefined = 0,
68 N2KFormat_YD_RAW,
69 N2KFormat_Actisense_RAW_ASCII,
70 N2KFormat_Actisense_N2K_ASCII,
71 N2KFormat_Actisense_N2K,
72 N2KFormat_Actisense_RAW,
73 N2KFormat_Actisense_NGT,
74 N2KFormat_SeaSmart,
75 N2KFormat_MiniPlex
76} N2K_Format;
77
78typedef enum { TX_FORMAT_YDEN = 0, TX_FORMAT_ACTISENSE } GW_TX_FORMAT;
79
80class MrqContainer; // forward in .cpp file
81class CommDriverN2KNetEvent; // Internal
82
83class circular_buffer {
84public:
85 circular_buffer(size_t size);
86 void reset();
87 size_t capacity() const;
88 size_t size() const;
89 bool empty() const;
90 bool full() const;
91 void put(unsigned char item);
92 unsigned char get();
93
94private:
95 std::mutex mutex_;
96 std::unique_ptr<unsigned char[]> buf_;
97 size_t head_ = 0;
98 size_t tail_ = 0;
99 const size_t max_size_;
100 bool full_ = 0;
101};
102
104 public wxEvtHandler,
105 public DriverStatsProvider {
106public:
108 CommDriverN2KNet(const ConnectionParams* params, DriverListener& listener);
109
110 virtual ~CommDriverN2KNet();
111
112 DriverStats GetDriverStats() const override { return m_driver_stats; }
113
114 void SetListener(DriverListener& l) override {};
115
116 void Open();
117 void Close();
118 ConnectionParams GetParams() const { return m_params; }
119
120 bool SetOutputSocketOptions(wxSocketBase* tsock);
121 void OnServerSocketEvent(wxSocketEvent& event); // The listener
122 void OnTimerSocket(wxTimerEvent& event) { OnTimerSocket(); }
123 void OnTimerSocket();
124 void OnSocketEvent(wxSocketEvent& event);
125 void OpenNetworkGPSD();
126 void OpenNetworkTCP(unsigned int addr);
127 void OpenNetworkUDP(unsigned int addr);
128 void OnSocketReadWatchdogTimer(wxTimerEvent& event);
129 void HandleResume();
130
131 bool SendMessage(std::shared_ptr<const NavMsg> msg,
132 std::shared_ptr<const NavAddr> addr) override;
133 wxSocketBase* GetSock() const { return m_sock; }
134
135private:
136 ConnectionParams m_params;
137 DriverListener& m_listener;
138
139 void handle_N2K_MSG(CommDriverN2KNetEvent& event);
140 wxString GetNetPort() const { return m_net_port; }
141 wxIPV4address GetAddr() const { return m_addr; }
142 wxTimer* GetSocketThreadWatchdogTimer() {
143 return &m_socketread_watchdog_timer;
144 }
145 wxTimer* GetSocketTimer() { return &m_socket_timer; }
146 void SetSock(wxSocketBase* sock) { m_sock = sock; }
147 void SetTSock(wxSocketBase* sock) { m_tsock = sock; }
148 wxSocketBase* GetTSock() const { return m_tsock; }
149 void SetSockServer(wxSocketServer* sock) { m_socket_server = sock; }
150 wxSocketServer* GetSockServer() const { return m_socket_server; }
151 void SetMulticast(bool multicast) { m_is_multicast = multicast; }
152 bool GetMulticast() const { return m_is_multicast; }
153
154 NetworkProtocol GetProtocol() { return m_net_protocol; }
155 void SetBrxConnectEvent(bool event) { m_brx_connect_event = event; }
156 bool GetBrxConnectEvent() { return m_brx_connect_event; }
157
158 void SetConnectTime(wxDateTime time) { m_connect_time = time; }
159 wxDateTime GetConnectTime() { return m_connect_time; }
160
161 dsPortType GetPortType() const { return m_io_select; }
162 wxString GetPort() const { return m_portstring; }
163
164 std::vector<unsigned char> PushFastMsgFragment(const CanHeader& header,
165 int position);
166 std::vector<unsigned char> PushCompleteMsg(const CanHeader header,
167 int position,
168 const can_frame frame);
169
170 void HandleCanFrameInput(can_frame frame);
171
172 ConnectionType GetConnectionType() const { return m_connection_type; }
173
174 bool ChecksumOK(const std::string& sentence);
175 void SetOk(bool ok) { m_bok = ok; };
176
177 N2K_Format DetectFormat(std::vector<unsigned char> packet);
178 bool ProcessActisense_ASCII_RAW(std::vector<unsigned char> packet);
179 bool ProcessActisense_ASCII_N2K(std::vector<unsigned char> packet);
180 bool ProcessActisense_N2K(std::vector<unsigned char> packet);
181 bool ProcessActisense_RAW(std::vector<unsigned char> packet);
182 bool ProcessActisense_NGT(std::vector<unsigned char> packet);
183 bool ProcessSeaSmart(std::vector<unsigned char> packet);
184 bool ProcessMiniPlex(std::vector<unsigned char> packet);
185
186 bool SendN2KNetwork(std::shared_ptr<const Nmea2000Msg>& msg,
187 std::shared_ptr<const NavAddr2000> dest_addr);
188
189 std::vector<std::vector<unsigned char>> GetTxVector(
190 const std::shared_ptr<const Nmea2000Msg>& msg,
191 std::shared_ptr<const NavAddr2000> dest_addr);
192 bool SendSentenceNetwork(std::vector<std::vector<unsigned char>> payload);
193 bool HandleMgntMsg(uint64_t pgn, std::vector<unsigned char>& payload);
194 bool PrepareForTX();
195 std::vector<unsigned char> PrepareLogPayload(
196 std::shared_ptr<const Nmea2000Msg>& msg,
197 std::shared_ptr<const NavAddr2000> addr);
198 void OnProdInfoTimer(wxTimerEvent& ev);
199
200 StatsTimer m_stats_timer;
201 DriverStats m_driver_stats;
202
203 wxString m_net_port;
204 NetworkProtocol m_net_protocol;
205 wxIPV4address m_addr;
206 wxSocketBase* m_sock;
207 wxSocketBase* m_tsock;
208 wxSocketServer* m_socket_server;
209 bool m_is_multicast;
210 MrqContainer* m_mrq_container;
211
212 int m_txenter;
213 int m_dog_value;
214 std::string m_sock_buffer;
215 wxString m_portstring;
216 dsPortType m_io_select;
217 wxDateTime m_connect_time;
218 bool m_brx_connect_event;
219 bool m_bchecksumCheck;
220 ConnectionType m_connection_type;
221
222 wxTimer m_socket_timer;
223 wxTimer m_socketread_watchdog_timer;
224
225 bool m_bok;
226 int m_ib;
227 bool m_bInMsg, m_bGotESC, m_bGotSOT;
228
229 circular_buffer* m_circle;
230 unsigned char* rx_buffer;
231 std::string m_sentence;
232
233 FastMessageMap* fast_messages;
234 N2K_Format m_n2k_format;
235 uint8_t m_order;
236 char m_TX_flag;
237 bool m_TX_available;
238 wxTimer m_prodinfo_timer;
239
240 ObsListener resume_listener;
241
242 DECLARE_EVENT_TABLE()
243};
244
245#endif // guard
CAN v2.0 29 bit header as used by NMEA 2000.
void SetListener(DriverListener &l) override
Set the entity which will receive incoming data.
DriverStats GetDriverStats() const override
Get the Driver Statistics.
void OnSocketEvent(wxSocketEvent &event)
Interface for handling incoming messages.
Definition comm_driver.h:50
Driver interface providing driver statistics.
Track fast message fragments eventually forming complete messages.
Define an action to be performed when a KeyProvider is notified.
Definition observable.h:257
Continuously report driver stats to CommDrvRegistry.evt_driver_stats.
Low-level socketcan utility functions.
Nmea2000 driver.
Nmea2000 IP network driver.
Communication statistics infrastructure.
Connection parameters.
Driver statistics report.