forked from OpenCPN/OpenCPN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm_drv_n0183_android_int.cpp
More file actions
242 lines (194 loc) · 6.83 KB
/
comm_drv_n0183_android_int.cpp
File metadata and controls
242 lines (194 loc) · 6.83 KB
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
/***************************************************************************
* Copyright (C) 2023 by David Register *
* Copyright (C) 2023 Alec Leamas *
* *
* 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, see <https://www.gnu.org/licenses/>. *
**************************************************************************/
/**
* \file
*
* Implement comm_drv_n0183_android_int.h -- Android internal Nmea 0183
* driver.
*/
#include <mutex>
#include <queue>
#include <vector>
// For compilers that support precompilation, includes "wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif // precompiled headers
#include <wx/event.h>
#include <wx/log.h>
#include <wx/string.h>
#include <wx/utils.h>
#include "config.h"
#include "model/comm_drv_n0183_android_int.h"
#include "model/comm_drv_registry.h"
#include "model/comm_navmsg_bus.h"
#ifdef __ANDROID__
#include "androidUTIL.h"
#endif
typedef enum DS_ENUM_BUFFER_STATE {
DS_RX_BUFFER_EMPTY,
DS_RX_BUFFER_FULL
} _DS_ENUM_BUFFER_STATE;
using namespace std::literals::chrono_literals;
class CommDriverN0183AndroidInt; // fwd
#define MAX_OUT_QUEUE_MESSAGE_LENGTH 100
template <typename T>
class n0183_atomic_queue {
public:
size_t size() {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queque.size();
}
bool empty() {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queque.empty();
}
const T& front() {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queque.front();
}
void push(const T& value) {
std::lock_guard<std::mutex> lock(m_mutex);
m_queque.push(value);
}
void pop() {
std::lock_guard<std::mutex> lock(m_mutex);
m_queque.pop();
}
private:
std::queue<T> m_queque;
mutable std::mutex m_mutex;
};
#define OUT_QUEUE_LENGTH 20
#define MAX_OUT_QUEUE_MESSAGE_LENGTH 100
wxDEFINE_EVENT(wxEVT_COMMDRIVER_N0183_ANDROID_INT,
CommDriverN0183AndroidIntEvent);
CommDriverN0183AndroidIntEvent::CommDriverN0183AndroidIntEvent(
wxEventType commandType, int id = 0)
: wxEvent(id, commandType) {};
CommDriverN0183AndroidIntEvent::~CommDriverN0183AndroidIntEvent() {};
void CommDriverN0183AndroidIntEvent::SetPayload(
std::shared_ptr<std::vector<unsigned char>> data) {
m_payload = data;
}
std::shared_ptr<std::vector<unsigned char>>
CommDriverN0183AndroidIntEvent::GetPayload() {
return m_payload;
}
// required for sending with wxPostEvent()
wxEvent* CommDriverN0183AndroidIntEvent::Clone() const {
CommDriverN0183AndroidIntEvent* newevent =
new CommDriverN0183AndroidIntEvent(*this);
newevent->m_payload = this->m_payload;
return newevent;
};
template <class T>
class circular_buffer {
public:
explicit circular_buffer(size_t size)
: buf_(std::unique_ptr<T[]>(new T[size])), max_size_(size) {}
void reset();
size_t capacity() const;
size_t size() const;
bool empty() const {
// if head and tail are equal, we are empty
return (!full_ && (head_ == tail_));
}
bool full() const {
// If tail is ahead the head by 1, we are full
return full_;
}
void put(T item) {
std::lock_guard<std::mutex> lock(mutex_);
buf_[head_] = item;
if (full_) tail_ = (tail_ + 1) % max_size_;
head_ = (head_ + 1) % max_size_;
full_ = head_ == tail_;
}
T get() {
std::lock_guard<std::mutex> lock(mutex_);
if (empty()) return T();
// Read data and advance the tail (we now have a free space)
auto val = buf_[tail_];
full_ = false;
tail_ = (tail_ + 1) % max_size_;
return val;
}
private:
std::mutex mutex_;
std::unique_ptr<T[]> buf_;
size_t head_ = 0;
size_t tail_ = 0;
const size_t max_size_;
bool full_ = 0;
};
CommDriverN0183AndroidInt::CommDriverN0183AndroidInt(
const ConnectionParams* params, DriverListener& listener)
: CommDriverN0183(NavAddr::Bus::N0183, params->GetStrippedDSPort()),
m_bok(false),
m_portstring(params->GetDSPort()),
m_stats_timer(*this, 2s),
m_params(*params),
m_listener(listener) {
this->attributes["commPort"] = params->Port.ToStdString();
this->attributes["userComment"] = params->UserComment.ToStdString();
this->attributes["ioDirection"] = DsPortTypeToString(params->IOSelect);
m_driver_stats.driver_bus = NavAddr::Bus::N0183;
m_driver_stats.driver_iface = params->GetStrippedDSPort();
// Prepare the wxEventHandler to accept events from the actual hardware thread
Bind(wxEVT_COMMDRIVER_N0183_ANDROID_INT,
&CommDriverN0183AndroidInt::handle_N0183_MSG, this);
Open();
}
CommDriverN0183AndroidInt::~CommDriverN0183AndroidInt() { Close(); }
bool CommDriverN0183AndroidInt::Open() {
androidStartGPS(this);
m_driver_stats.available = true;
return true;
}
void CommDriverN0183AndroidInt::Close() {
wxLogMessage(
wxString::Format("Closing NMEA Driver %s", m_portstring.c_str()));
m_stats_timer.Stop();
androidStopGPS();
m_driver_stats.available = false;
Unbind(wxEVT_COMMDRIVER_N0183_ANDROID_INT,
&CommDriverN0183AndroidInt::handle_N0183_MSG, this);
}
bool CommDriverN0183AndroidInt::SendMessage(
std::shared_ptr<const NavMsg> msg, std::shared_ptr<const NavAddr> addr) {
return false;
}
void CommDriverN0183AndroidInt::handle_N0183_MSG(
CommDriverN0183AndroidIntEvent& event) {
auto p = event.GetPayload();
std::vector<unsigned char>* payload = p.get();
m_driver_stats.rx_count += payload->size();
// Extract the NMEA0183 sentence
std::string full_sentence = std::string(payload->begin(), payload->end());
if ((full_sentence[0] == '$') || (full_sentence[0] == '!')) { // Sanity check
// notify message listener
if (m_params.SentencePassesFilter(full_sentence, FILTER_INPUT)) {
// We notify based on full message, including the Talker ID
std::string id = full_sentence.substr(1, 5);
auto msg =
std::make_shared<const Nmea0183Msg>(id, full_sentence, GetAddress());
m_listener.Notify(std::move(msg));
}
}
}