OpenCPN Partial API docs
Loading...
Searching...
No Matches
serial_io.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 by Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 **************************************************************************/
20
26#ifndef _N0183_PROTOL_MGR__
27#define _N0183_PROTOL_MGR__
28
29#include "config.h"
30
31#include <atomic>
32#include <memory>
33#include <functional>
34
35#include <wx/string.h>
36
37#include "comm_buffers.h"
38#include "model/logger.h"
39#include "model/thread_ctrl.h"
40#include "model/ocpn_utils.h"
42
43using namespace std::literals::chrono_literals;
44
46static std::string NormalizePort(const std::string& port) {
47 return port.find("Serial:") == 0 ? port.substr(strlen("Serial:")) : port;
48}
49
51using SendMsgFunc = std::function<void(const std::vector<unsigned char>&)>;
52
54class SerialIo : public ThreadCtrl {
55public:
57 static std::unique_ptr<SerialIo> Create(SendMsgFunc send_msg_func,
58 const std::string& port,
59 unsigned baud);
60
61 virtual ~SerialIo() = default;
62
64 virtual void Start() = 0;
65
67 virtual bool SetOutMsg(const wxString& msg) = 0;
68
69 virtual void RequestStop() override = 0;
70
72 virtual DriverStats GetStats() const = 0;
73
74protected:
75 const wxString m_portname;
76 const unsigned m_baud;
77 const SendMsgFunc m_send_msg_func;
78 DriverStats m_stats;
79 mutable std::mutex m_stats_mutex;
80 TimedLogFilter m_open_log_filter;
81
82 SerialIo(SendMsgFunc send_msg_func, const std::string& port, unsigned baud)
83 : m_portname(NormalizePort(port)),
84 m_baud(baud),
85 m_send_msg_func(send_msg_func),
86 m_open_log_filter(5min) {}
87};
88
89#ifdef __clang__
90// As of 12.0.5, clang does not emit the SerialIo vtable and typeinfo linker
91// addresses without this hack (gcc does). Net result without hack is
92// undefined symbols like "typeinfo for SerialIo". Possible clang bug?
93
94__attribute__((used)) static SerialIo* force_clang_to_emit_typeinfo;
95#endif
96
97#endif // _N0183_PROTOL_MGR__
Nmea0183 serial communications wrapper, possibly running a thread.
Definition serial_io.h:54
virtual void Start()=0
Start IO operations including input, possibly in separate thread.
virtual DriverStats GetStats() const =0
Retrieve updated driver statistics.
static std::unique_ptr< SerialIo > Create(SendMsgFunc send_msg_func, const std::string &port, unsigned baud)
Factory.
virtual void RequestStop() override=0
Request that thread stops operation.
virtual bool SetOutMsg(const wxString &msg)=0
Send a message to remote peer.
Thread mixin providing a "stop thread"/"wait until stopped" interface.
Definition thread_ctrl.h:32
Filter logging repeated message with specified interval.
Definition logger.h:145
Line-oriented input/output buffers.
Communication statistics infrastructure.
Enhanced logging interface on top of wx/log.h.
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.
ThreadCtrl mixin class definition.