File: GarminProtocolHandler.h

package info (click to toggle)
opencpn 5.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 101,540 kB
  • sloc: ansic: 414,598; cpp: 253,008; xml: 83,748; sh: 409; python: 353; makefile: 110; javascript: 87; perl: 83
file content (357 lines) | stat: -rw-r--r-- 10,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/******************************************************************************
 *
 * Project:  OpenCPN
 * Purpose:  NMEA Data Object
 * Author:   David Register
 *
 ***************************************************************************
 *   Copyright (C) 2010 by David S. Register                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
 ***************************************************************************
 *
 *
 *
 *
 */


#ifndef __GARMINPROTOCOLHANDLER_H__
#define __GARMINPROTOCOLHANDLER_H__

#include "wx/wxprec.h"

#ifndef  WX_PRECOMP
#include "wx/wx.h"
#endif //precompiled header


#include <wx/datetime.h>


#ifdef __WXGTK__
// newer versions of glib define its own GSocket but we unfortunately use this
// name in our own (semi-)public header and so can't change it -- rename glib
// one instead
//#include <gtk/gtk.h>
#define GSocket GlibGSocket
#include "wx/socket.h"
#undef GSocket
#else
#include "wx/socket.h"
#endif

#ifndef __WXMSW__
#include <sys/socket.h>                 // needed for (some) Mac builds
#include <netinet/in.h>
#endif

#ifdef __WXMSW__
#include <windows.h>
#include <dbt.h>
#include <initguid.h>
#endif
#include <string>
#include "ConnectionParams.h"
#include "dsPortType.h"
#include "datastream.h"

//----------------------------------------------------------------------------
//   constants
//----------------------------------------------------------------------------
#ifndef PI
#define PI        3.1415926535897931160E0      /* pi */
#endif

#define TIMER_SOCKET   7006



//----------------------------------------------------------------------------
// Garmin Device Management
// Handle USB and Serial Port Garmin PVT protocol data interface.
//----------------------------------------------------------------------------


//--------------------------------------------------------
//              Some Garmin Data Structures and Constants
//--------------------------------------------------------
#define GARMIN_USB_API_VERSION 1
#define GARMIN_USB_MAX_BUFFER_SIZE 4096
#define GARMIN_USB_INTERRUPT_DATA_SIZE 64

#define IOCTL_GARMIN_USB_API_VERSION CTL_CODE \
(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GARMIN_USB_INTERRUPT_IN CTL_CODE \
(FILE_DEVICE_UNKNOWN, 0x850, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GARMIN_USB_BULK_OUT_PACKET_SIZE CTL_CODE \
(FILE_DEVICE_UNKNOWN, 0x851, METHOD_BUFFERED, FILE_ANY_ACCESS)

#ifdef __WXMSW__
// {2C9C45C2-8E7D-4C08-A12D-816BBAE722C0}
DEFINE_GUID(GARMIN_GUID, 0x2c9c45c2L, 0x8e7d, 0x4c08, 0xa1, 0x2d, 0x81, 0x6b, 0xba, 0xe7, 0x22, 0xc0);
#endif

/*
 * New packet types in USB.
 */
#define GUSB_SESSION_START 5    /* We request units attention */
#define GUSB_SESSION_ACK   6    /* Unit responds that we have its attention */
#define GUSB_REQUEST_BULK  2    /* Unit requests we read from bulk pipe */

#define GUSB_RESPONSE_PVT  51   /* PVT Data Packet */
#define GUSB_RESPONSE_SDR  114  /* Satellite Data Record Packet */


typedef
union {
    struct {
        unsigned char type;
        unsigned char reserved1;
        unsigned char reserved2;
        unsigned char reserved3;
        unsigned char pkt_id[2];
        unsigned char reserved6;
        unsigned char reserved7;
        unsigned char datasz[4];
        unsigned char databuf[5]; /* actually a variable length array... */
    } gusb_pkt;
    unsigned char dbuf[1024];
} garmin_usb_packet;


typedef struct garmin_unit_info {
    unsigned long serial_number;
    unsigned long unit_id;
    unsigned long unit_version;
    char *os_identifier; /* In case the OS has another name for it. */
    char *product_identifier; /* From the hardware itself. */
} unit_info_type;


/*              Packet structure for Pkt_ID = 51 (PVT Data Record)   */
//#pragma pack(push)  /* push current alignment to stack */
//#pragma pack(1)     /* set alignment to 1 byte boundary */
#pragma pack(push,1) /* push current alignment to stack, set alignment to 1 byte boundary */

typedef struct {
    float   alt;
    float   epe;
    float   eph;
    float   epv;
    short   fix;
    double  tow;
    double  lat;
    double  lon;
    float   east;
    float   north;
    float   up;
    float   msl_hght;
    short   leap_scnds;
    long    wn_days;
} D800_Pvt_Data_Type;

#pragma pack(pop)   /* restore original alignment from stack */


typedef struct {
    float   alt;
    float   epe;
    float   eph;
    float   epv;
    short   fix;
    double  tow;
    double  lat;
    double  lon;
    float   east;
    float   north;
    float   up;
    float   msl_hght;
    short   leap_scnds;
    long    wn_days;
} D800_Pvt_Data_Type_Aligned;



/*              Packet structure for Pkt_ID = 114 (Satellite Data Record)   */
typedef    struct
{
    unsigned char         svid;          //space vehicle identification (1-32 and 33-64 for WAAS)
    short                 snr;           //signal-to-noise ratio
    unsigned char         elev;          //satellite elevation in degrees
    short                 azmth;         //satellite azimuth in degrees
    unsigned char         status;        //status bit-field
} cpo_sat_data;

/*
 * The status bit field represents a set of booleans described below:
 *                     Bit      Meaning when bit is one (1)
 *                     0       The unit has ephemeris data for the specified satellite.
 *                     1       The unit has a differential correction for the specified satellite.
 *                     2       The unit is using this satellite in the solution.
 */


enum {
    rs_fromintr,
    rs_frombulk
};

#define TIMER_GARMIN1   7005

class GARMIN_Serial_Thread;
class GARMIN_USB_Thread;

class GarminProtocolHandler: public wxEvtHandler
{
public:
    GarminProtocolHandler(DataStream *parent, wxEvtHandler *MessageTarget,  bool bsel_usb);
    ~GarminProtocolHandler();

    void Close(void);


    void StopIOThread(bool b_pause);
    void RestartIOThread(void);

    void StopSerialThread(void);

    void OnTimerGarmin1(wxTimerEvent& event);

    bool FindGarminDeviceInterface();



    wxEvtHandler            *m_pMainEventHandler;
    DataStream              *m_pparent;

    int                     m_max_tx_size;
    int                     m_receive_state;
    cpo_sat_data            m_sat_data[12];
    unit_info_type          grmin_unit_info[2];
    int                     m_nSats;
    wxTimer                 TimerGarmin1;

    int                     m_Thread_run_flag;
    GARMIN_Serial_Thread    *m_garmin_serial_thread;
    GARMIN_USB_Thread       *m_garmin_usb_thread;
    bool                    m_bneed_int_reset;
    int                     m_ndelay;
    bool                    m_bOK;
    bool                    m_busb;
    wxString                m_port;

#ifdef __WXMSW__
    HANDLE garmin_usb_start();
    bool ResetGarminUSBDriver();
    static bool IsGarminPlugged();
    bool gusb_syncup(void);

    int gusb_win_get(garmin_usb_packet *ibuf, size_t sz);
    int gusb_win_get_bulk(garmin_usb_packet *ibuf, size_t sz);
    int gusb_win_send(const garmin_usb_packet *opkt, size_t sz);

    int gusb_cmd_send(const garmin_usb_packet *opkt, size_t sz);
    int gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz);

    HANDLE                  m_usb_handle;

    WXLRESULT MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam);
#endif

    DECLARE_EVENT_TABLE()
};


//-------------------------------------------------------------------------------------------------------------
//
//    Garmin Serial Port Worker Thread
//
//    This thread manages reading the positioning data stream from the declared Garmin GRMN Mode serial device
//
//-------------------------------------------------------------------------------------------------------------
class GARMIN_Serial_Thread: public wxThread
{

public:

    GARMIN_Serial_Thread(GarminProtocolHandler *parent,
                         DataStream *GParentStream,
                         wxEvtHandler *MessageTarget,
                         wxString port);
    ~GARMIN_Serial_Thread(void);
    void *Entry();
    void string(wxCharBuffer mb_str);


private:
    wxEvtHandler            *m_pMessageTarget;
    GarminProtocolHandler   *m_parent;
    DataStream              *m_parent_stream;


    wxString                m_port;
    bool                    m_bconnected;
    bool                    m_bdetected;

};



//-------------------------------------------------------------------------------------------------------------
//
//    Garmin USB Worker Thread
//
//    This thread manages reading the positioning data stream from the declared Garmin USB device
//
//-------------------------------------------------------------------------------------------------------------
class GARMIN_USB_Thread: public wxThread
{

public:

    GARMIN_USB_Thread(GarminProtocolHandler *parent,
                      DataStream *GParentStream,
                      wxEvtHandler *MessageTarget,
                      unsigned int device_handle,
                      size_t max_tx_size);
    ~GARMIN_USB_Thread(void);
    void *Entry();


private:
    DataStream *m_parent_stream;

    int gusb_win_get(garmin_usb_packet *ibuf, size_t sz);
    int gusb_win_get_bulk(garmin_usb_packet *ibuf, size_t sz);
    int gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz);

    wxEvtHandler            *m_pMessageTarget;
    GarminProtocolHandler   *m_parent;


    int                     m_receive_state;
    cpo_sat_data            m_sat_data[12];
    unit_info_type          grmin_unit_info[2];
    int                     m_nSats;
    int                     m_max_tx_size;
#ifdef __WXMSW__
    HANDLE                  m_usb_handle;
#endif

};

#endif // __GARMINPROTOCOLHANDLER_H__