YARP
Yet Another Robot Platform
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>
10 #include <yarp/os/LogComponent.h>
11 #include <yarp/os/Value.h>
12 
13 using namespace yarp::dev;
14 using namespace yarp::os;
15 using namespace yarp::os::impl;
16 
17 namespace {
18  YARP_LOG_COMPONENT(DEVICERESPONDER, "yarp.dev.DeviceResponder")
19 }
20 
22  makeUsage();
23 }
24 
25 void DeviceResponder::addUsage(const char *txt, const char *explain) {
26  examples.addString(txt); //Value::makeList(txt));
27  explains.addString((explain!=nullptr)?explain:"");
28  details.add(Value::makeList(txt));
29  std::string more = std::string(" ") + ((explain != nullptr) ? explain : "");
30  details.addString(more.c_str());
31 }
32 
33 
34 void DeviceResponder::addUsage(const Bottle& bot, const char *explain) {
35  addUsage(bot.toString().c_str(),explain);
36 }
37 
38 
39 bool DeviceResponder::respond(const Bottle& command, Bottle& reply) {
40  switch (command.get(0).asVocab32()) {
41  case yarp::os::createVocab32('h','e','l','p'):
42  if (examples.size()>=1) {
43  reply.add(Value::makeVocab32("many"));
44  if (command.get(1).toString()=="more") {
45  reply.append(details);
46  } else {
47  reply.append(examples);
48  }
49  return true;
50  } else {
51  reply.addString("no documentation available");
52  return false;
53  }
54  break;
55  default:
56  reply.addString("command not recognized");
57  return false;
58  }
59  return false;
60 }
61 
63 {
64  Bottle cmd;
65  Bottle response;
66  if (!cmd.read(connection)) {
67  return false;
68  }
69  yCTrace(DEVICERESPONDER, "Command received: %s", cmd.toString().c_str());
70  respond(cmd, response);
71  if (response.size() >= 1) {
72  ConnectionWriter* writer = connection.getWriter();
73  if (writer != nullptr) {
74  if (response.get(0).toString() == "many" && writer->isTextMode()) {
75  for (size_t i = 1; i < response.size(); i++) {
76  Value& v = response.get(i);
77  if (v.isList()) {
78  v.asList()->write(*writer);
79  } else {
80  Bottle b;
81  b.add(v);
82  b.write(*writer);
83  }
84  }
85  } else {
86  response.write(*writer);
87  }
88 
89  yCTrace(DEVICERESPONDER, "Response sent: %s", response.toString().c_str());
90  }
91  } else {
92  ConnectionWriter* writer = connection.getWriter();
93  if (writer != nullptr) {
94  response.clear();
95  response.addVocab32("nak");
96  response.write(*writer);
97  }
98  }
99  return true;
100 }
101 
102 
104  examples.clear();
105  explains.clear();
106  details.clear();
107  addUsage("[help]", "list usage");
108  addUsage("[help] [more]", "list usage with some comments");
109 }
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:74
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:336
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:380
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
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:45
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
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,...)
Definition: LogComponent.h:85
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
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:28