File: datastream.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 (289 lines) | stat: -rw-r--r-- 9,074 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
/******************************************************************************
 *
 * 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 __DATASTREAM_H__
#define __DATASTREAM_H__

#ifdef __MINGW32__
#undef IPV6STRICT    // mingw FTBS fix:  missing struct ip_mreq
#include <windows.h>
#endif

#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 <winioctl.h>
// #include <initguid.h>
#endif
#include <string>
#include "ConnectionParams.h"
#include "dsPortType.h"

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

#define TIMER_SOCKET   7006

//      Error codes, returned by GetLastError()
enum {
    DS_ERROR_PORTNOTFOUND
};


//      End-of-sentence types
enum {
    DS_EOS_CRLF,
    DS_EOS_LF,
    DS_EOS_CR
};

//      Serial port handshake type
enum {
    DS_HANDSHAKE_NONE,
    DS_HANDSHAKE_XON_XOFF
};

#define DS_SOCKET_ID             5001
#define DS_SERVERSOCKET_ID       5002
#define DS_ACTIVESERVERSOCKET_ID 5003

#define     MAX_RX_MESSSAGE_SIZE  4096
#define     RX_BUFFER_SIZE        4096


// Class declarations





// Class declarations
class OCP_DataStreamInput_Thread;
class DataStream;
class GarminProtocolHandler;

extern  const wxEventType wxEVT_OCPN_DATASTREAM;
extern  const wxEventType wxEVT_OCPN_THREADMSG;

bool CheckSumCheck(const std::string& sentence);

//----------------------------------------------------------------------------
// DataStream
//
//      Physical port is specified by a string in the class ctor.
//      Examples strings:
//              Serial:/dev/ttyS0               (Standard serial port)
//              Serial:COM4
//              TCP:192.168.1.1:5200            (TCP source, address and port specified)
//              GPSD:192.168.2.3:5400           (GPSD Wire protocol, address and port specified)
//
//----------------------------------------------------------------------------



class DataStream: public wxEvtHandler
{
protected:
    DataStream(wxEvtHandler *input_consumer,
               const ConnectionType conn_type,
               const wxString& Port,
               const wxString& BaudRate,
               dsPortType io_select,
               int priority,
               bool bGarmin,
               int EOS_type,
               int handshake_type);
    DataStream(wxEvtHandler *input_consumer, const ConnectionParams* params);

public:
    virtual ~DataStream();

    virtual void Close();

    bool IsOk() const { return m_bok; }
    wxString GetPort() const { return m_portstring; }
    dsPortType GetIoSelect() const { return m_io_select; }
    int GetPriority() const { return m_priority; }

    virtual bool SendSentence( const wxString &sentence );

    int GetLastError() const { return m_last_error; }

 //    Secondary thread life toggle
 //    Used to inform launching object (this) to determine if the thread can
 //    be safely called or polled, e.g. wxThread->Destroy();
    void SetSecThreadActive(void){m_bsec_thread_active = true;}
    void SetSecThreadInActive(void){m_bsec_thread_active = false;}
    bool IsSecThreadActive() const { return m_bsec_thread_active; }

    void SetChecksumCheck(bool check) { m_bchecksumCheck = check; }

    void SetInputFilter(wxArrayString filter) { m_input_filter = filter; }
    void SetInputFilterType(ListType filter_type) { m_input_filter_type = filter_type; }
    void SetOutputFilter(wxArrayString filter) { m_output_filter = filter; }
    void SetOutputFilterType(ListType filter_type) { m_output_filter_type = filter_type; }
    bool SentencePassesFilter(const wxString& sentence, FilterDirection direction);
    bool ChecksumOK(const std::string& sentence);
    bool GetGarminMode() const { return m_bGarmin_GRMN_mode; }

    wxString GetBaudRate() const { return m_BaudRate; }
    dsPortType GetPortType() const { return m_io_select; }
    wxArrayString GetInputSentenceList() const { return m_input_filter; }
    wxArrayString GetOutputSentenceList() const { return m_output_filter; }
    ListType GetInputSentenceListType() const { return m_input_filter_type; }
    ListType GetOutputSentenceListType() const { return m_output_filter_type; }
    bool GetChecksumCheck() const { return m_bchecksumCheck; }
    ConnectionType GetConnectionType() const { return m_connection_type; }
    const ConnectionParams* GetConnectionParams() const { return &m_params; }
    int                 m_Thread_run_flag;

protected:
    void SetOk(bool ok) { m_bok = ok; };

    void SetGarminProtocolHandler(GarminProtocolHandler *garminHandler) {
        m_GarminHandler = garminHandler;
    }

    void SetSecondaryThread(OCP_DataStreamInput_Thread *secondary_Thread) {
        m_pSecondary_Thread = secondary_Thread;
    }
    OCP_DataStreamInput_Thread* GetSecondaryThread() { return m_pSecondary_Thread; }
    void SetThreadRunFlag(int run) { m_Thread_run_flag = run; }

    wxEvtHandler* GetConsumer() { return m_consumer; }
    wxString GetPortString(){ return m_portstring; }
    
    void SetConnectTime(wxDateTime time) { m_connect_time = time; }
    wxDateTime GetConnectTime() { return m_connect_time; }


private:
    virtual void Open();


    bool                m_bok;
    wxEvtHandler        *m_consumer;
    wxString            m_portstring;
    wxString            m_BaudRate;
    dsPortType          m_io_select;
    int                 m_priority;
    int                 m_handshake;

    OCP_DataStreamInput_Thread *m_pSecondary_Thread;
    bool                m_bsec_thread_active;
    int                 m_last_error;

    ConnectionType      m_connection_type;

    bool                m_bchecksumCheck;
    wxArrayString       m_input_filter;
    ListType            m_input_filter_type;
    wxArrayString       m_output_filter;
    ListType            m_output_filter_type;

    bool                m_bGarmin_GRMN_mode;
    GarminProtocolHandler *m_GarminHandler;
    wxDateTime          m_connect_time;
    ConnectionParams    m_params;

};

class InternalGPSDataStream : public DataStream {
public:
    InternalGPSDataStream(wxEvtHandler *input_consumer,
                          const ConnectionParams *params) : DataStream(input_consumer, params) {
        Open();
    }

private:
    void Open();
};

class InternalBTDataStream : public DataStream {
public:
    InternalBTDataStream(wxEvtHandler *input_consumer,
                         const ConnectionParams *params) : DataStream(input_consumer, params) {
        Open();
    }

private:
    void Open();
    bool SendSentence( const wxString &sentence );

};

class NullDataStream : public DataStream {
public:
    NullDataStream(wxEvtHandler *input_consumer,
                   const ConnectionParams *params) : DataStream(input_consumer, params) {}

};

// Factory methods, preparation to split into subclasses with clearer
// Responsibilities

DataStream* makeDataStream(wxEvtHandler *input_consumer, const ConnectionParams* params);


#endif