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, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
28#ifndef _DRIVER_API_H
29#define _DRIVER_API_H
30
31#include <memory>
32#include <string>
33#include <unordered_map>
34
35#include "observable.h"
36#include "comm_navmsg.h"
37
38enum class CommStatus { Ok, NotImplemented, NotSupported, NameInUse };
39
40class AbstractCommDriver; // forward
41
51public:
55 virtual ~DriverListener() = default;
56
58 virtual void Notify(std::shared_ptr<const NavMsg> message) = 0;
59
61 virtual void Notify(const AbstractCommDriver& driver) = 0;
62};
63
66public:
67 AbstractCommDriver() : bus(NavAddr::Bus::Undef), iface("nil") {};
68
69 virtual ~AbstractCommDriver() = default;
70
71 virtual bool SendMessage(std::shared_ptr<const NavMsg> msg,
72 std::shared_ptr<const NavAddr> addr) = 0;
73
78 virtual void SetListener(DriverListener& l) {}
79
87 virtual std::pair<CommStatus, std::string> Clone() {
88 // FIXME(leamas): Requires unique interface support in DriverRegistry.
89 return std::pair<CommStatus, std::string>(CommStatus::NotImplemented, "");
90 }
91
92 std::string Key() const { return NavAddr::BusToString(bus) + "!@!" + iface; }
93
94 const NavAddr::Bus bus;
95 const std::string iface;
98 virtual std::unordered_map<std::string, std::string> GetAttributes() const {
99 return attributes;
100 }
101
102 std::unordered_map<std::string, std::string> attributes;
103
104protected:
105 AbstractCommDriver(NavAddr::Bus b) : bus(b) {
106 attributes["protocol"] = NavAddr::BusToString(bus);
107 };
108 AbstractCommDriver(NavAddr::Bus b, const std::string& s) : bus(b), iface(s) {
109 attributes["protocol"] = NavAddr::BusToString(bus);
110 };
111};
112
113#endif // DRIVER_API_H
Common interface for all drivers.
Definition comm_driver.h:65
virtual void SetListener(DriverListener &l)
Set the entity which will receive incoming data.
Definition comm_driver.h:78
const std::string iface
Physical device for 0183, else a unique string.
Definition comm_driver.h:95
virtual std::pair< CommStatus, std::string > Clone()
Create a new virtual interface using a new instance of this driver.
Definition comm_driver.h:87
Interface for handling incoming messages.
Definition comm_driver.h:50
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.