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  virtual ~DeviceDriver(){}
44 
58  bool open(yarp::os::Searchable& config) override { 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=0/*NULL*/);
154 
161  void addUsage(const yarp::os::Bottle& bot, const char *explain=0/*NULL*/);
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:72
yarp::os::TypedReader::setReplier
virtual void setReplier(PortReader &reader)=0
If a message is received that requires a reply, use this handler.
api.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:68
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
Definition: DeviceDriver.h:37
yarp::dev::DeviceResponder::addUsage
void addUsage(const char *txt, const char *explain=0)
Add information about a message that the respond() method understands.
Definition: DeviceDriver.cpp:25
yarp::os::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:30
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::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::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:27
Property.h
yarp::os::IConfig
An object that can be configured.
Definition: IConfig.h:26
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
yarp::dev::DeviceDriver::~DeviceDriver
virtual ~DeviceDriver()
Destructor.
Definition: DeviceDriver.h:43
BufferedPort.h
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:39
yarp::dev::DeviceDriver::close
bool close() override
Close the DeviceDriver.
Definition: DeviceDriver.h:64
yarp::dev::DeviceResponder
Definition: DeviceDriver.h:132
IConfig.h
yarp::dev::DeprecatedDeviceDriver
Definition: DeviceDriver.h:118
yarp
Definition: numeric.h:47
yarp::dev::DeviceResponder::DeviceResponder
DeviceResponder()
Constructor.
Definition: DeviceDriver.cpp:21
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
yarp::os::TypedReader::useCallback
virtual void useCallback(TypedReaderCallback< T > &callback)=0
Set an object whose onRead method will be called when data is available.
ConnectionReader.h