OpenCPN Partial API docs
Loading...
Searching...
No Matches
android_serial_io.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2024 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
26#include <string>
27
28// For compilers that support precompilation, includes "wx.h".
29#include <wx/wxprec.h>
30
31#ifndef WX_PRECOMP
32#include <wx/wx.h>
33#endif
34
35#include <wx/string.h>
36
37#include "androidUTIL.h"
38#include "model/serial_io.h"
40
45class AndroidSerialIo : public SerialIo {
46public:
47 AndroidSerialIo(SendMsgFunc send_func, const std::string& port, unsigned baud)
48 : SerialIo(send_func, port, baud) {
49 m_driver_stats.driver_bus = NavAddr::Bus::N0183;
50 m_driver_stats.driver_iface = "android";
51 }
52
53 DriverStats GetStats() const { return m_driver_stats; }
54 // FIXME (leamas) rx...
55
56 bool SetOutMsg(const wxString& msg) override {
57 if (msg.size() < 6 || (msg[0] != '$' && msg[0] != '!')) return false;
58 wxString payload = msg;
59 if (!msg.EndsWith("\r\n")) payload += "\r\n";
60 wxString port(m_portname); // Horrors due to missing const in library
61 androidWriteSerial(port, payload);
62 m_driver_stats.tx_count += m_portname.size();
63 return true;
64 }
65
66 void Start() override {
67 if (m_portname.empty()) return;
68 wxString port(m_portname); // Horrors due to missing const in library
69 androidStartUSBSerial(port, std::to_string(m_baud), m_send_msg_func);
70 m_driver_stats.available = true;
71 }
72
73 void RequestStop() override {
74 wxString port(m_portname); // Horrors due to missing const in library
75 androidStopUSBSerial(port);
77 ThreadCtrl::SignalExit(); // No need to wait for exiting thread doing this
78 m_driver_stats.available = false;
79 }
80
81private:
82 DriverStats m_driver_stats;
83};
84
85std::unique_ptr<SerialIo> SerialIo::Create(SendMsgFunc send_msg_func,
86 const std::string& port,
87 unsigned baud) {
88 return std::make_unique<AndroidSerialIo>(send_msg_func, port, baud);
89}
Android SerialIo synchronous implementation based on the native Android serial interface as exposed i...
bool SetOutMsg(const wxString &msg) override
Send a message to remote peer.
DriverStats GetStats() const
Retrieve updated driver statistics.
void RequestStop() override
Request that thread stops operation.
void Start() override
Start IO operations including input, possibly in separate thread.
Nmea0183 serial communications wrapper, possibly running a thread.
Definition serial_io.h:54
static std::unique_ptr< SerialIo > Create(SendMsgFunc send_msg_func, const std::string &port, unsigned baud)
Factory.
void SignalExit()
Signal that thread has exited.
virtual void RequestStop()
Request that thread stops operation.
Communication statistics infrastructure.
Abstract N0183 serial communications interface.
std::function< void(const std::vector< unsigned char > &)> SendMsgFunc
Forwards data from driver to local receivers (main code, plugins, etc).
Definition serial_io.h:51
Driver statistics report.
unsigned tx_count
Number of bytes sent since program start.