YARP
Yet Another Robot Platform
DeviceDriver.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_DEV_DEVICEDRIVER_H
11 #define YARP_DEV_DEVICEDRIVER_H
12 
15 #include <yarp/os/BufferedPort.h>
16 #include <yarp/os/Property.h>
17 #include <yarp/os/IConfig.h>
18 
19 #include <yarp/dev/api.h>
20 
21 namespace yarp {
25  namespace dev {
26  class DeviceDriver;
27  class DeprecatedDeviceDriver;
28  class DeviceResponder;
29  }
30 }
31 
38 {
39 public:
43  ~DeviceDriver() override = default;
44 
58  bool open(yarp::os::Searchable& config) override { YARP_UNUSED(config); return true; }
59 
64  bool close() override { return true; }
65 
66 
76  template <class T>
77  bool view(T *&x) {
78  x = nullptr;
79 
80  // This is not super-portable; and it requires RTTI compiled
81  // in. For systems on which this is a problem, suggest:
82  // either replace it with a regular cast (and warn user) or
83  // implement own method for checking interface support.
84  T *v = dynamic_cast<T *>(getImplementation());
85 
86  if (v != nullptr) {
87  x = v;
88  return true;
89  }
90 
91  return false;
92  }
93 
102  return this;
103  }
104 };
105 
119 {
120 };
121 
122 
133  public yarp::os::TypedReaderCallback<yarp::os::Bottle>
134 {
135 
136 private:
137  yarp::os::Bottle examples;
138  yarp::os::Bottle explains;
139  yarp::os::Bottle details;
140 
141 public:
145  DeviceResponder();
146 
153  void addUsage(const char *txt, const char *explain = nullptr);
154 
161  void addUsage(const yarp::os::Bottle& bot, const char *explain = nullptr);
162 
169  virtual bool respond(const yarp::os::Bottle& command,
170  yarp::os::Bottle& reply);
171 
178  bool read(yarp::os::ConnectionReader& connection) override;
179 
187  void onRead(yarp::os::Bottle& v) override {
188  yarp::os::Bottle reply;
189  respond(v,reply);
190  }
191 
195  void makeUsage();
196 
203  source.useCallback(*this);
204  source.setReplier(*this);
205  }
206 };
207 
208 
209 #endif // YARP_DEV_DEVICEDRIVER_H
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::TypedReader::setReplier
virtual void setReplier(PortReader &reader)=0
If a message is received that requires a reply, use this handler.
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::os::TypedReader< yarp::os::Bottle >
yarp::dev::DeviceDriver::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: DeviceDriver.h:58
ConnectionWriter.h
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
yarp::os::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:31
yarp::dev::DeviceResponder::respond
virtual bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
Respond to a message.
Definition: DeviceDriver.cpp:39
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::dev::DeviceDriver::view
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
yarp::dev::DeviceDriver::getImplementation
virtual DeviceDriver * getImplementation()
Some drivers are bureaucrats, pointing at others.
Definition: DeviceDriver.h:101
yarp::dev::DeviceResponder::addUsage
void addUsage(const char *txt, const char *explain=nullptr)
Add information about a message that the respond() method understands.
Definition: DeviceDriver.cpp:25
yarp::dev::DeviceResponder::attach
void attach(yarp::os::TypedReader< yarp::os::Bottle > &source)
Attach this object to a source of messages.
Definition: DeviceDriver.h:202
yarp::dev::DeviceResponder::makeUsage
void makeUsage()
Regenerate usage information.
Definition: DeviceDriver.cpp:99
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
Property.h
yarp::os::IConfig
An object that can be configured.
Definition: IConfig.h:27
yarp::dev::DeviceResponder::onRead
void onRead(yarp::os::Bottle &v) override
Alternative handler for reading messages from the network, and passing them on to the respond() metho...
Definition: DeviceDriver.h:187
BufferedPort.h
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
yarp::dev::DeviceResponder::read
bool read(yarp::os::ConnectionReader &connection) override
Handler for reading messages from the network, and passing them on to the respond() method.
Definition: DeviceDriver.cpp:62
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::dev::DeviceDriver::close
bool close() override
Close the DeviceDriver.
Definition: DeviceDriver.h:64
yarp::dev::DeviceResponder
A cheap and cheerful framework for human readable/writable forms of messages to devices.
Definition: DeviceDriver.h:134
IConfig.h
yarp::dev::DeprecatedDeviceDriver
Interface implemented by deprecated device drivers.
Definition: DeviceDriver.h:119
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::dev::DeviceResponder::DeviceResponder
DeviceResponder()
Constructor.
Definition: DeviceDriver.cpp:21
yarp::os::TypedReader::useCallback
virtual void useCallback(TypedReaderCallback< T > &callback)=0
Set an object whose onRead method will be called when data is available.
api.h
yarp::dev::DeviceDriver::~DeviceDriver
~DeviceDriver() override=default
Destructor.
ConnectionReader.h