OpenCPN Partial API docs
comm_drv_n2k_serial.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose:
5  * Author: David Register, Alec Leamas
6  *
7  ***************************************************************************
8  * Copyright (C) 2022 by David Register, Alec Leamas *
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 #ifndef _COMMDRIVERN2KSERIAL_H
27 #define _COMMDRIVERN2KSERIAL_H
28 
29 #include <atomic>
30 
31 #include <wx/thread.h>
32 
33 #include "config.h"
34 #include "model/comm_drv_n2k.h"
35 #include "model/conn_params.h"
36 
37 #ifndef __ANDROID__
38 #include "serial/serial.h"
39 #endif
40 
41 #define OUT_QUEUE_LENGTH 20
42 #define MAX_OUT_QUEUE_MESSAGE_LENGTH 200
43 
44 #define ESCAPE 0x10
45 #define STARTOFTEXT 0x02
46 #define ENDOFTEXT 0x03
47 
48 #define MsgTypeN2kData 0x93
49 #define MsgTypeN2kRequest 0x94
50 
51 class CommDriverN2KSerialThread; // fwd
53 
54 class CommDriverN2KSerial : public CommDriverN2K, public wxEvtHandler {
55 public:
57  CommDriverN2KSerial(const ConnectionParams* params, DriverListener& listener);
58 
59  virtual ~CommDriverN2KSerial();
60 
62  void Activate() override;
63 
64  void SetListener(DriverListener& l) override{};
65 
66  bool Open();
67  void Close();
68 
69  bool SendMessage(std::shared_ptr<const NavMsg> msg,
70  std::shared_ptr<const NavAddr> addr) override;
71 
72  int SetTXPGN(int pgn) override;
73 
74  // Secondary thread life toggle
75  // Used to inform launching object (this) to determine if the thread can
76  // be safely called or polled, e.g. wxThread->Destroy();
77  void SetSecThreadActive(void) { m_bsec_thread_active = true; }
78  void SetSecThreadInActive(void) { m_bsec_thread_active = false; }
79  bool IsSecThreadActive() const { return m_bsec_thread_active; }
80 
81  void SetSecondaryThread(CommDriverN2KSerialThread* secondary_Thread) {
82  m_pSecondary_Thread = secondary_Thread;
83  }
84  CommDriverN2KSerialThread* GetSecondaryThread() {
85  return m_pSecondary_Thread;
86  }
87  void SetThreadRunFlag(int run) { m_Thread_run_flag = run; }
88 
89  void handle_N2K_SERIAL_RAW(CommDriverN2KSerialEvent& event);
90  int GetMfgCode();
91 
92  std::atomic_int m_Thread_run_flag;
93 
94 private:
95  void ProcessManagementPacket(std::vector<unsigned char> *payload);
96  int SendMgmtMsg( unsigned char *string, size_t string_size,
97  unsigned char cmd_code,
98  int timeout_msec, bool *response_flag);
99 
100  bool m_bok;
101  std::string m_portstring;
102  std::string m_BaudRate;
103  int m_handshake;
104 
105  CommDriverN2KSerialThread* m_pSecondary_Thread;
106  bool m_bsec_thread_active;
107 
108  ConnectionParams m_params;
109  DriverListener& m_listener;
110 
111  bool m_bmg47_resp;
112  bool m_bmg01_resp;
113  bool m_bmg4B_resp;
114  bool m_bmg41_resp;
115  bool m_bmg42_resp;
116 
117  std::string m_device_common_name;
118  uint64_t NAME;
119  int m_manufacturers_code;
120  bool m_got_mfg_code;
121 
122 };
123 
124 #endif // guard
void Activate() override
Register driver and possibly do other post-ctor steps.
void SetListener(DriverListener &l) override
Set the entity which will receive incoming data.
Interface implemented by transport layer and possible other parties like test code which should handl...
Definition: comm_driver.h:47