OpenCPN Partial API docs
comm_driver.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: Communication driver layer. Defines the generic driver model,
5  * messages sent to/from drivers and addresses. The driver layer
6  * is the lowest of the three layers drivers, raw messages (navmsg)
7  * and decoded application messages(appmsg).
8  * Author: David Register, Alec Leamas
9  *
10  ***************************************************************************
11  * Copyright (C) 2022 by David Register, Alec Leamas *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the *
25  * Free Software Foundation, Inc., *
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
27  **************************************************************************/
28 
29 #include <memory>
30 #include <string>
31 #include <unordered_map>
32 
33 #include "observable.h"
34 #include "comm_navmsg.h"
35 
36 #ifndef _DRIVER_API_H
37 #define _DRIVER_API_H
38 
39 enum class CommStatus { Ok, NotImplemented, NotSupported, NameInUse };
40 
41 class AbstractCommDriver; // forward
42 
48 public:
50  virtual void Notify(std::shared_ptr<const NavMsg> message) = 0;
51 
53  virtual void Notify(const AbstractCommDriver& driver) = 0;
54 };
55 
58  : public std::enable_shared_from_this<AbstractCommDriver> {
59 public:
60  AbstractCommDriver() : bus(NavAddr::Bus::Undef), iface("nil"){};
61 
62  virtual bool SendMessage(std::shared_ptr<const NavMsg> msg,
63  std::shared_ptr<const NavAddr> addr) = 0;
64 
66  virtual void Activate() = 0;
67 
72  virtual void SetListener(DriverListener& l) {}
73 
81  virtual std::pair<CommStatus, std::string> Clone() {
82  // FIXME(leamas): Requires unique interface support in DriverRegistry.
83  return std::pair<CommStatus, std::string>(CommStatus::NotImplemented, "");
84  }
85 
86  std::string Key() const { return NavAddr::BusToString(bus) + "!@!" + iface; }
87 
88  const NavAddr::Bus bus;
89  const std::string iface;
92  virtual std::unordered_map<std::string, std::string> GetAttributes() const { return attributes;}
93 
94  std::unordered_map<std::string, std::string> attributes;
95 
96 protected:
97  AbstractCommDriver(NavAddr::Bus b) : bus(b){
98  attributes["protocol"] = NavAddr::BusToString(bus);
99  };
100  AbstractCommDriver(NavAddr::Bus b, const std::string& s) : bus(b), iface(s){
101  attributes["protocol"] = NavAddr::BusToString(bus);
102  };
103 };
104 
105 #endif // DRIVER_API_H
Common interface for all drivers.
Definition: comm_driver.h:58
virtual std::pair< CommStatus, std::string > Clone()
Create a new virtual interface using a new instance of this driver.
Definition: comm_driver.h:81
virtual void SetListener(DriverListener &l)
Set the entity which will receive incoming data.
Definition: comm_driver.h:72
const std::string iface
Physical device for 0183, else a unique string.
Definition: comm_driver.h:89
virtual void Activate()=0
Register driver in the driver Registry.
Interface implemented by transport layer and possible other parties like test code which should handl...
Definition: comm_driver.h:47
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.