YARP
Yet Another Robot Platform
DeviceDriver.cpp
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 #include <cstdio>
11 #include <yarp/dev/DeviceDriver.h>
12 #include <yarp/os/Vocab.h>
13 #include <yarp/os/Value.h>
14 
15 using namespace yarp::dev;
16 using namespace yarp::os;
17 using namespace yarp::os::impl;
18 
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).asVocab()) {
41  case yarp::os::createVocab('h','e','l','p'):
42  if (examples.size()>=1) {
43  reply.add(Value::makeVocab("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  Bottle cmd, response;
64  if (!cmd.read(connection)) { return false; }
65  //printf("command received: %s\n", cmd.toString().c_str());
66  respond(cmd,response);
67  if (response.size()>=1) {
68  ConnectionWriter *writer = connection.getWriter();
69  if (writer!=nullptr) {
70  if (response.get(0).toString()=="many"&&writer->isTextMode()) {
71  for (size_t i=1; i<response.size(); i++) {
72  Value& v = response.get(i);
73  if (v.isList()) {
74  v.asList()->write(*writer);
75  } else {
76  Bottle b;
77  b.add(v);
78  b.write(*writer);
79  }
80  }
81  } else {
82  response.write(*writer);
83  }
84 
85  //printf("response sent: %s\n", response.toString().c_str());
86  }
87  } else {
88  ConnectionWriter *writer = connection.getWriter();
89  if (writer!=nullptr) {
90  response.clear();
91  response.addVocab(Vocab::encode("nak"));
92  response.write(*writer);
93  }
94  }
95  return true;
96 }
97 
98 
100  examples.clear();
101  explains.clear();
102  details.clear();
103  addUsage("[help]", "list usage");
104  addUsage("[help] [more]", "list usage with some comments");
105 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Value::asVocab
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
yarp::os::Bottle::clear
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
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::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
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::DeviceResponder::makeUsage
void makeUsage()
Regenerate usage information.
Definition: DeviceDriver.cpp:99
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::os::Bottle::write
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:233
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
yarp::os::ConnectionReader::getWriter
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
yarp::os::Bottle::addVocab
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
yarp::os::Value::isList
virtual bool isList() const
Checks if value is a list.
Definition: Value.cpp:165
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::os::ConnectionWriter::isTextMode
virtual bool isTextMode() const =0
Check if the connection is text mode.
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::dev::DeviceResponder::DeviceResponder
DeviceResponder()
Constructor.
Definition: DeviceDriver.cpp:21
Vocab.h
yarp::os::Bottle::read
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
yarp::os::Value::asList
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
yarp::os::Bottle::add
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:339
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::os::impl
The components from which ports and connections are built.
Value.h
yarp::os::Bottle::append
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Definition: Bottle.cpp:383
DeviceDriver.h