OpenCPN Partial API docs
Loading...
Searching...
No Matches
garmin_protocol_mgr.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2010 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#ifndef GARMINPROTOCOLHANDLER_H_
25#define GARMINPROTOCOLHANDLER_H_
26
27#include <atomic>
28#include <string>
29
30#ifndef __WXMSW__
31#include <sys/socket.h> // needed for (some) Mac builds
32#include <netinet/in.h>
33#else
34#include <winsock2.h>
35#include <windows.h>
36#include <dbt.h>
37#include <initguid.h>
38#endif
39
40#include <wx/wxprec.h>
41
42#ifndef WX_PRECOMP
43#include <wx/wx.h>
44#endif // precompiled header
45
46#include <wx/datetime.h>
47
48#ifdef __WXGTK__
49// newer versions of glib define its own GSocket but we unfortunately use this
50// name in our own (semi-)public header and so can't change it -- rename glib
51// one instead
52#define GSocket GlibGSocket
53#include <wx/socket.h>
54#undef GSocket
55#else
56#include <wx/socket.h>
57#endif
58
59#include "garminusb.h"
60#include "model/conn_params.h"
61#include "model/ds_porttype.h"
62
63//----------------------------------------------------------------------------
64// constants
65//----------------------------------------------------------------------------
66#ifndef PI
67#define PI 3.1415926535897931160E0 /* pi */
68#endif
69
70//----------------------------------------------------------------------------
71// Garmin Device Management
72// Handle USB and Serial Port Garmin PVT protocol data interface.
73//----------------------------------------------------------------------------
74
75//--------------------------------------------------------
76// Some Garmin Data Structures and Constants
77//--------------------------------------------------------
78#define GARMIN_USB_API_VERSION 1
79#define GARMIN_USB_MAX_BUFFER_SIZE 4096
80#define GARMIN_USB_INTERRUPT_DATA_SIZE 64
81
82#define IOCTL_GARMIN_USB_API_VERSION \
83 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
84#define IOCTL_GARMIN_USB_INTERRUPT_IN \
85 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x850, METHOD_BUFFERED, FILE_ANY_ACCESS)
86#define IOCTL_GARMIN_USB_BULK_OUT_PACKET_SIZE \
87 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x851, METHOD_BUFFERED, FILE_ANY_ACCESS)
88
89#ifdef __WXMSW__
90// {2C9C45C2-8E7D-4C08-A12D-816BBAE722C0}
91DEFINE_GUID(GARMIN_GUID, 0x2c9c45c2L, 0x8e7d, 0x4c08, 0xa1, 0x2d, 0x81, 0x6b,
92 0xba, 0xe7, 0x22, 0xc0);
93#endif
94
95/*
96 * New packet types in USB.
97 */
98#define GUSB_SESSION_START 5 /* We request units attention */
99#define GUSB_SESSION_ACK 6 /* Unit responds that we have its attention */
100#define GUSB_REQUEST_BULK 2 /* Unit requests we read from bulk pipe */
101
102#define GUSB_RESPONSE_PVT 51 /* PVT Data Packet */
103#define GUSB_RESPONSE_SDR 114 /* Satellite Data Record Packet */
104
105typedef struct unit_info_type_ {
106 unsigned long serial_number;
107 unsigned long unit_id;
108 unsigned long unit_version;
109 char *os_identifier; /* In case the OS has another name for it. */
110 char *product_identifier; /* From the hardware itself. */
112
113/* push current alignment to stack, set alignment to 1 byte boundary */
114#pragma pack(push, 1)
115
116typedef struct {
117 float alt;
118 float epe;
119 float eph;
120 float epv;
121 short fix;
122 double tow;
123 double lat;
124 double lon;
125 float east;
126 float north;
127 float up;
128 float msl_hght;
129 short leap_scnds;
130 long wn_days;
132
133#pragma pack(pop) /* restore original alignment from stack */
134
135typedef struct {
136 float alt;
137 float epe;
138 float eph;
139 float epv;
140 short fix;
141 double tow;
142 double lat;
143 double lon;
144 float east;
145 float north;
146 float up;
147 float msl_hght;
148 short leap_scnds;
149 long wn_days;
151
152/* Packet structure for Pkt_ID = 114 (Satellite Data Record) */
153typedef struct {
154 unsigned char svid; // space vehicle identification (1-32 and 33-64 for WAAS)
155 short snr; // signal-to-noise ratio
156 unsigned char elev; // satellite elevation in degrees
157 short azmth; // satellite azimuth in degrees
158 unsigned char status; // status bit-field
160
161/*
162 * The status bit field represents a set of booleans described below:
163 * Bit Meaning when bit is one (1)
164 * 0 The unit has ephemeris data for the specified
165 * satellite. 1 The unit has a differential correction for the specified
166 * satellite. 2 The unit is using this satellite in the solution.
167 */
168
169enum { rs_fromintr, rs_frombulk };
170
171using SendMsgFunc = std::function<void(const std::vector<unsigned char> &)>;
172
173#define TIMER_GARMIN1 7005
174
177
178class GarminProtocolHandler : public wxEvtHandler {
179public:
180 GarminProtocolHandler(wxString port, SendMsgFunc send_msg_func, bool sel_usb);
182
183 void Close(void);
184
185 void StopIOThread(bool b_pause);
186 void RestartIOThread(void);
187
188 void StopSerialThread(void);
189
190 void OnTimerGarmin1(wxTimerEvent &event);
191
192 bool FindGarminDeviceInterface();
193
194 SendMsgFunc m_send_msg_func;
195 void *m_pparent;
196
197 int m_max_tx_size;
198 int m_receive_state;
199 cpo_sat_data m_sat_data[12];
200 unit_info_type grmin_unit_info[2];
201 int m_nSats;
202 wxTimer TimerGarmin1;
203
204 std::atomic_int m_Thread_run_flag;
205 GARMIN_Serial_Thread *m_garmin_serial_thread;
206 GARMIN_USB_Thread *m_garmin_usb_thread;
207 bool m_bneed_int_reset;
208 int m_ndelay;
209 bool m_bOK;
210 bool m_busb;
211 wxString m_port;
212
213#ifdef __WXMSW__
214 HANDLE garmin_usb_start();
215 bool ResetGarminUSBDriver();
216 static bool IsGarminPlugged();
217 bool gusb_syncup(void);
218
219 int gusb_win_get(garmin_usb_packet *ibuf, size_t sz);
220 int gusb_win_get_bulk(garmin_usb_packet *ibuf, size_t sz);
221 int gusb_win_send(const garmin_usb_packet *opkt, size_t sz);
222
223 int gusb_cmd_send(const garmin_usb_packet *opkt, size_t sz);
224 int gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz);
225
226 HANDLE m_usb_handle;
227
228 WXLRESULT MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam);
229#endif
230
231 DECLARE_EVENT_TABLE()
232};
233
234//-------------------------------------------------------------------------------------------------------------
235//
236// Garmin Serial Port Worker Thread
237//
238// This thread manages reading the positioning data stream from the declared
239// Garmin GRMN Mode serial device
240//
241//-------------------------------------------------------------------------------------------------------------
242class GARMIN_Serial_Thread : public wxThread {
243public:
244 GARMIN_Serial_Thread(GarminProtocolHandler *parent, SendMsgFunc send_msg_func,
245 wxString port);
247 void *Entry();
248 void string(wxCharBuffer mb_str);
249
250private:
251 SendMsgFunc m_send_msg_func;
252 GarminProtocolHandler *m_parent;
253
254 wxString m_port;
255 bool m_bconnected;
256 bool m_bdetected;
257};
258
259//-------------------------------------------------------------------------------------------------------------
260//
261// Garmin USB Worker Thread
262//
263// This thread manages reading the positioning data stream from the declared
264// Garmin USB device
265//
266//-------------------------------------------------------------------------------------------------------------
267class GARMIN_USB_Thread : public wxThread {
268public:
269 GARMIN_USB_Thread(GarminProtocolHandler *parent, SendMsgFunc send_msg_func,
270 unsigned int device_handle, size_t max_tx_size);
271 ~GARMIN_USB_Thread(void);
272 void *Entry();
273
274private:
275 int gusb_win_get(garmin_usb_packet *ibuf, size_t sz);
276 int gusb_win_get_bulk(garmin_usb_packet *ibuf, size_t sz);
277 int gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz);
278
279 SendMsgFunc m_send_msg_func;
280 GarminProtocolHandler *m_parent;
281
282 int m_receive_state;
283 cpo_sat_data m_sat_data[12];
284 unit_info_type grmin_unit_info[2];
285 int m_nSats;
286 int m_max_tx_size;
287#ifdef __WXMSW__
288 HANDLE m_usb_handle;
289#endif
290};
291
292#endif // GARMINPROTOCOLHANDLER_H_
Connection parameters.
Port type definition and support.