OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_driver.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 - 2024 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
29#include <memory>
30#include <string>
31#include <unordered_map>
32
33#include "observable.h"
34// #include "comm_drv_stats.h"
35#include "comm_navmsg.h"
36
37#ifndef _DRIVER_API_H
38#define _DRIVER_API_H
39
40enum class CommStatus { Ok, NotImplemented, NotSupported, NameInUse };
41
42class AbstractCommDriver; // forward
43
53public:
57 virtual ~DriverListener() = default;
58
60 virtual void Notify(std::shared_ptr<const NavMsg> message) = 0;
61
63 virtual void Notify(const AbstractCommDriver& driver) = 0;
64};
65
68public:
69 AbstractCommDriver() : bus(NavAddr::Bus::Undef), iface("nil") {};
70
71 virtual ~AbstractCommDriver() = default;
72
73 virtual bool SendMessage(std::shared_ptr<const NavMsg> msg,
74 std::shared_ptr<const NavAddr> addr) = 0;
75
80 virtual void SetListener(DriverListener& l) {}
81
89 virtual std::pair<CommStatus, std::string> Clone() {
90 // FIXME(leamas): Requires unique interface support in DriverRegistry.
91 return std::pair<CommStatus, std::string>(CommStatus::NotImplemented, "");
92 }
93
94 std::string Key() const { return NavAddr::BusToString(bus) + "!@!" + iface; }
95
96 const NavAddr::Bus bus;
97 const std::string iface;
100 virtual std::unordered_map<std::string, std::string> GetAttributes() const {
101 return attributes;
102 }
103
104 std::unordered_map<std::string, std::string> attributes;
105
106protected:
107 AbstractCommDriver(NavAddr::Bus b) : bus(b) {
108 attributes["protocol"] = NavAddr::BusToString(bus);
109 };
110 AbstractCommDriver(NavAddr::Bus b, const std::string& s) : bus(b), iface(s) {
111 attributes["protocol"] = NavAddr::BusToString(bus);
112 };
113};
114
115#endif // DRIVER_API_H
Common interface for all drivers.
Definition comm_driver.h:67
virtual void SetListener(DriverListener &l)
Set the entity which will receive incoming data.
Definition comm_driver.h:80
const std::string iface
Physical device for 0183, else a unique string.
Definition comm_driver.h:97
virtual std::pair< CommStatus, std::string > Clone()
Create a new virtual interface using a new instance of this driver.
Definition comm_driver.h:89
Interface for handling incoming messages.
Definition comm_driver.h:52
virtual ~DriverListener()=default
Destroy the Driver Listener object.
virtual void Notify(std::shared_ptr< const NavMsg > message)=0
Handle a received message.
virtual void Notify(const AbstractCommDriver &driver)=0
Handle driver status change.
Raw, undecoded messages definitions.
General observable implementation with several specializations.