OpenCPN Partial API docs
Loading...
Searching...
No Matches
garmin_protocol_mgr.h
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: NMEA Data Object
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2010 by David S. Register *
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 *
26 *
27 *
28 *
29 */
30
31#ifndef _GARMINPROTOCOLHANDLER_H__
32#define _GARMINPROTOCOLHANDLER_H__
33
34#include <string>
35
36#ifndef __WXMSW__
37#include <sys/socket.h> // needed for (some) Mac builds
38#include <netinet/in.h>
39#else
40#include <windows.h>
41#include <dbt.h>
42#include <initguid.h>
43#endif
44
45
46#include <wx/wxprec.h>
47
48#ifndef WX_PRECOMP
49#include <wx/wx.h>
50#endif // precompiled header
51
52#include <wx/datetime.h>
53
54#ifdef __WXGTK__
55// newer versions of glib define its own GSocket but we unfortunately use this
56// name in our own (semi-)public header and so can't change it -- rename glib
57// one instead
58//#include <gtk/gtk.h>
59#define GSocket GlibGSocket
60#include <wx/socket.h>
61#undef GSocket
62#else
63#include <wx/socket.h>
64#endif
65
66
67#include "conn_params.h"
68#include "dsPortType.h"
69
70//----------------------------------------------------------------------------
71// constants
72//----------------------------------------------------------------------------
73#ifndef PI
74#define PI 3.1415926535897931160E0 /* pi */
75#endif
76
77#define TIMER_SOCKET 7006
78
79//----------------------------------------------------------------------------
80// Garmin Device Management
81// Handle USB and Serial Port Garmin PVT protocol data interface.
82//----------------------------------------------------------------------------
83
84//--------------------------------------------------------
85// Some Garmin Data Structures and Constants
86//--------------------------------------------------------
87#define GARMIN_USB_API_VERSION 1
88#define GARMIN_USB_MAX_BUFFER_SIZE 4096
89#define GARMIN_USB_INTERRUPT_DATA_SIZE 64
90
91#define IOCTL_GARMIN_USB_API_VERSION \
92 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
93#define IOCTL_GARMIN_USB_INTERRUPT_IN \
94 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x850, METHOD_BUFFERED, FILE_ANY_ACCESS)
95#define IOCTL_GARMIN_USB_BULK_OUT_PACKET_SIZE \
96 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x851, METHOD_BUFFERED, FILE_ANY_ACCESS)
97
98#ifdef __WXMSW__
99// {2C9C45C2-8E7D-4C08-A12D-816BBAE722C0}
100DEFINE_GUID(GARMIN_GUID, 0x2c9c45c2L, 0x8e7d, 0x4c08, 0xa1, 0x2d, 0x81, 0x6b,
101 0xba, 0xe7, 0x22, 0xc0);
102#endif
103
104/*
105 * New packet types in USB.
106 */
107#define GUSB_SESSION_START 5 /* We request units attention */
108#define GUSB_SESSION_ACK 6 /* Unit responds that we have its attention */
109#define GUSB_REQUEST_BULK 2 /* Unit requests we read from bulk pipe */
110
111#define GUSB_RESPONSE_PVT 51 /* PVT Data Packet */
112#define GUSB_RESPONSE_SDR 114 /* Satellite Data Record Packet */
113
114typedef union {
115 struct {
116 unsigned char type;
117 unsigned char reserved1;
118 unsigned char reserved2;
119 unsigned char reserved3;
120 unsigned char pkt_id[2];
121 unsigned char reserved6;
122 unsigned char reserved7;
123 unsigned char datasz[4];
124 unsigned char databuf[5]; /* actually a variable length array... */
125 } gusb_pkt;
126 unsigned char dbuf[1024];
128
129typedef struct garmin_unit_info {
130 unsigned long serial_number;
131 unsigned long unit_id;
132 unsigned long unit_version;
133 char *os_identifier; /* In case the OS has another name for it. */
134 char *product_identifier; /* From the hardware itself. */
136
137/* Packet structure for Pkt_ID = 51 (PVT Data Record) */
138//#pragma pack(push) /* push current alignment to stack */
139//#pragma pack(1) /* set alignment to 1 byte boundary */
140#pragma pack( \
141 push, \
142 1) /* push current alignment to stack, set alignment to 1 byte boundary */
143
144typedef struct {
145 float alt;
146 float epe;
147 float eph;
148 float epv;
149 short fix;
150 double tow;
151 double lat;
152 double lon;
153 float east;
154 float north;
155 float up;
156 float msl_hght;
157 short leap_scnds;
158 long wn_days;
160
161#pragma pack(pop) /* restore original alignment from stack */
162
163typedef struct {
164 float alt;
165 float epe;
166 float eph;
167 float epv;
168 short fix;
169 double tow;
170 double lat;
171 double lon;
172 float east;
173 float north;
174 float up;
175 float msl_hght;
176 short leap_scnds;
177 long wn_days;
179
180/* Packet structure for Pkt_ID = 114 (Satellite Data Record) */
181typedef struct {
182 unsigned char svid; // space vehicle identification (1-32 and 33-64 for WAAS)
183 short snr; // signal-to-noise ratio
184 unsigned char elev; // satellite elevation in degrees
185 short azmth; // satellite azimuth in degrees
186 unsigned char status; // status bit-field
188
189/*
190 * The status bit field represents a set of booleans described below:
191 * Bit Meaning when bit is one (1)
192 * 0 The unit has ephemeris data for the specified
193 * satellite. 1 The unit has a differential correction for the specified
194 * satellite. 2 The unit is using this satellite in the solution.
195 */
196
197enum { rs_fromintr, rs_frombulk };
198
199#define TIMER_GARMIN1 7005
200
203
204class GarminProtocolHandler : public wxEvtHandler {
205public:
206 GarminProtocolHandler(/*FIXME dave DataStream*/void *parent, wxEvtHandler *MessageTarget,
207 bool bsel_usb);
209
210 void Close(void);
211
212 void StopIOThread(bool b_pause);
213 void RestartIOThread(void);
214
215 void StopSerialThread(void);
216
217 void OnTimerGarmin1(wxTimerEvent &event);
218
219 bool FindGarminDeviceInterface();
220
221 wxEvtHandler *m_pMainEventHandler;
222 void *m_pparent;
223
224 int m_max_tx_size;
225 int m_receive_state;
226 cpo_sat_data m_sat_data[12];
227 unit_info_type grmin_unit_info[2];
228 int m_nSats;
229 wxTimer TimerGarmin1;
230
231 int m_Thread_run_flag;
232 GARMIN_Serial_Thread *m_garmin_serial_thread;
233 GARMIN_USB_Thread *m_garmin_usb_thread;
234 bool m_bneed_int_reset;
235 int m_ndelay;
236 bool m_bOK;
237 bool m_busb;
238 wxString m_port;
239
240#ifdef __WXMSW__
241 HANDLE garmin_usb_start();
242 bool ResetGarminUSBDriver();
243 static bool IsGarminPlugged();
244 bool gusb_syncup(void);
245
246 int gusb_win_get(garmin_usb_packet *ibuf, size_t sz);
247 int gusb_win_get_bulk(garmin_usb_packet *ibuf, size_t sz);
248 int gusb_win_send(const garmin_usb_packet *opkt, size_t sz);
249
250 int gusb_cmd_send(const garmin_usb_packet *opkt, size_t sz);
251 int gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz);
252
253 HANDLE m_usb_handle;
254
255 WXLRESULT MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam);
256#endif
257
258 DECLARE_EVENT_TABLE()
259};
260
261//-------------------------------------------------------------------------------------------------------------
262//
263// Garmin Serial Port Worker Thread
264//
265// This thread manages reading the positioning data stream from the declared
266// Garmin GRMN Mode serial device
267//
268//-------------------------------------------------------------------------------------------------------------
269class GARMIN_Serial_Thread : public wxThread {
270public:
271 GARMIN_Serial_Thread(GarminProtocolHandler *parent, void *GParentStream,
272 wxEvtHandler *MessageTarget, wxString port);
274 void *Entry();
275 void string(wxCharBuffer mb_str);
276
277private:
278 wxEvtHandler *m_pMessageTarget;
279 GarminProtocolHandler *m_parent;
280 void *m_parent_stream;
281
282 wxString m_port;
283 bool m_bconnected;
284 bool m_bdetected;
285};
286
287//-------------------------------------------------------------------------------------------------------------
288//
289// Garmin USB Worker Thread
290//
291// This thread manages reading the positioning data stream from the declared
292// Garmin USB device
293//
294//-------------------------------------------------------------------------------------------------------------
295class GARMIN_USB_Thread : public wxThread {
296public:
297 GARMIN_USB_Thread(GarminProtocolHandler *parent, void *GParentStream,
298 wxEvtHandler *MessageTarget, unsigned int device_handle,
299 size_t max_tx_size);
300 ~GARMIN_USB_Thread(void);
301 void *Entry();
302
303private:
304 void *m_parent_stream;
305
306 int gusb_win_get(garmin_usb_packet *ibuf, size_t sz);
307 int gusb_win_get_bulk(garmin_usb_packet *ibuf, size_t sz);
308 int gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz);
309
310 wxEvtHandler *m_pMessageTarget;
311 GarminProtocolHandler *m_parent;
312
313 int m_receive_state;
314 cpo_sat_data m_sat_data[12];
315 unit_info_type grmin_unit_info[2];
316 int m_nSats;
317 int m_max_tx_size;
318#ifdef __WXMSW__
319 HANDLE m_usb_handle;
320#endif
321};
322
323#endif // __GARMINPROTOCOLHANDLER_H__