YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
DeviceDriver.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <cstdio>
9#include <yarp/os/Vocab.h>
11#include <yarp/os/Value.h>
12
13using namespace yarp::dev;
14using namespace yarp::os;
15using namespace yarp::os::impl;
16
17namespace {
18 YARP_LOG_COMPONENT(DEVICERESPONDER, "yarp.dev.DeviceResponder")
19}
20
21
23{
24public:
25 std::string device_id;
26};
27
32
34{
35 delete mPriv;
36}
37
38void DeviceDriver::setId(const std::string& id)
39{
40 mPriv->device_id = id;
41}
42
43std::string DeviceDriver::id() const
44{
45 return mPriv->device_id;
46}
47
51
52void DeviceResponder::addUsage(const char *txt, const char *explain) {
53 examples.addString(txt); //Value::makeList(txt));
54 explains.addString((explain!=nullptr)?explain:"");
55 details.add(Value::makeList(txt));
56 std::string more = std::string(" ") + ((explain != nullptr) ? explain : "");
57 details.addString(more.c_str());
58}
59
60
61void DeviceResponder::addUsage(const Bottle& bot, const char *explain) {
62 addUsage(bot.toString().c_str(),explain);
63}
64
65
66bool DeviceResponder::respond(const Bottle& command, Bottle& reply) {
67 switch (command.get(0).asVocab32()) {
68 case yarp::os::createVocab32('h','e','l','p'):
69 if (examples.size()>=1) {
70 reply.add(Value::makeVocab32("many"));
71 if (command.get(1).toString()=="more") {
72 reply.append(details);
73 } else {
74 reply.append(examples);
75 }
76 return true;
77 } else {
78 reply.addString("no documentation available");
79 return false;
80 }
81 break;
82 default:
83 reply.addString("command not recognized");
84 return false;
85 }
86 return false;
87}
88
90{
91 Bottle cmd;
92 Bottle response;
93 if (!cmd.read(connection)) {
94 return false;
95 }
96 yCTrace(DEVICERESPONDER, "Command received: %s", cmd.toString().c_str());
97 respond(cmd, response);
98 if (response.size() >= 1) {
99 ConnectionWriter* writer = connection.getWriter();
100 if (writer != nullptr) {
101 if (response.get(0).toString() == "many" && writer->isTextMode()) {
102 for (size_t i = 1; i < response.size(); i++) {
103 Value& v = response.get(i);
104 if (v.isList()) {
105 v.asList()->write(*writer);
106 } else {
107 Bottle b;
108 b.add(v);
109 b.write(*writer);
110 }
111 }
112 } else {
113 response.write(*writer);
114 }
115
116 yCTrace(DEVICERESPONDER, "Response sent: %s", response.toString().c_str());
117 }
118 } else {
119 ConnectionWriter* writer = connection.getWriter();
120 if (writer != nullptr) {
121 response.clear();
122 response.addVocab32("nak");
123 response.write(*writer);
124 }
125 }
126 return true;
127}
128
129
131 examples.clear();
132 explains.clear();
133 details.clear();
134 addUsage("[help]", "list usage");
135 addUsage("[help] [more]", "list usage with some comments");
136}
virtual void setId(const std::string &id)
Set the id for this device.
virtual std::string id() const
Return the id assigned to the PolyDriver.
virtual bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
Respond to a message.
void addUsage(const char *txt, const char *explain=nullptr)
Add information about a message that the respond() method understands.
void makeUsage()
Regenerate usage information.
bool read(yarp::os::ConnectionReader &connection) override
Handler for reading messages from the network, and passing them on to the respond() method.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:309
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:164
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Definition Bottle.cpp:353
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:251
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:240
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:230
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:211
A mini-server for performing network communication in the background.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
virtual bool isTextMode() const =0
Check if the connection is text mode.
A single value (typically within a Bottle).
Definition Value.h:43
static Value * makeList()
Create a list Value.
Definition Value.cpp:440
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition Value.cpp:228
static Value * makeVocab32(yarp::conf::vocab32_t v)
Create a vocabulary identifier Value.
Definition Value.cpp:427
virtual bool isList() const
Checks if value is a list.
Definition Value.cpp:162
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:240
std::string toString() const override
Return a standard text representation of the content of the object.
Definition Value.cpp:356
#define yCTrace(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition Vocab.h:27