YARP
Yet Another Robot Platform
RobotDescriptionServer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 #include "RobotDescriptionServer.h"
11 #include <yarp/os/Log.h>
12 #include <yarp/os/LogComponent.h>
13 #include <yarp/os/LogStream.h>
14 #include <mutex>
15 
18 using namespace yarp::dev;
19 using namespace yarp::os;
20 using namespace yarp::sig;
21 
22 namespace {
23 YARP_LOG_COMPONENT(ROBOTDESCRIPTIONSERVER, "yarp.device.robotDescriptionServer")
24 }
25 
26 //------------------------------------------------------------------------------------------------------------------------------
27 
29 {
30  m_local_name.clear();
31  m_local_name = config.find("local").asString();
32 
33  if (m_local_name == "")
34  {
35  yCError(ROBOTDESCRIPTIONSERVER, "open(): Invalid local name");
36  return false;
37  }
38 
39  std::string local_rpc = m_local_name;
40  local_rpc += "/rpc";
41 
42  if (!m_rpc_port.open(local_rpc))
43  {
44  yCError(ROBOTDESCRIPTIONSERVER, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
45  return false;
46  }
47 
48  m_rpc_port.setReader(*this);
49  return true;
50 }
51 
53 {
54  std::lock_guard<std::mutex> guard(m_external_mutex);
55  for (int i = 0; i < p.size(); i++)
56  {
57  yCTrace(ROBOTDESCRIPTIONSERVER) << p[i]->poly->getOptions().toString();
58  yCTrace(ROBOTDESCRIPTIONSERVER) << p[i]->poly->getValue("device").toString();
59  yCTrace(ROBOTDESCRIPTIONSERVER) << p[i]->poly->getValue("name").toString();
61  dev.device_name = p[i]->poly->getValue("name").toString();
62  dev.device_type = p[i]->poly->getValue("device").toString();
63  if (this->add_device(dev) == false)
64  {
65  yCError(ROBOTDESCRIPTIONSERVER) << "attachAll(): Something strange happened here";
66  //return false;
67  }
68  }
69  return true;
70 }
71 
73 {
74  std::lock_guard<std::mutex> guard(m_external_mutex);
75  m_robot_devices.clear();
76  return true;
77 }
78 
80 {
81  m_rpc_port.close();
82  return true;
83 }
84 
85 bool RobotDescriptionServer::add_device(DeviceDescription dev)
86 {
87  std::lock_guard<std::mutex> guard(m_internal_mutex);
88  for (auto& m_robot_device : m_robot_devices)
89  {
90  if (dev.device_name == m_robot_device.device_name)
91  {
92  yCWarning(ROBOTDESCRIPTIONSERVER) << "add_device(): Device" << dev.device_name << "already exists, skipping";
93  return false;
94  }
95  }
96  m_robot_devices.push_back(dev);
97  return true;
98 }
99 
100 bool RobotDescriptionServer::remove_device(DeviceDescription dev)
101 {
102  std::lock_guard<std::mutex> guard(m_internal_mutex);
103  for (auto it = m_robot_devices.begin(); it != m_robot_devices.end(); it++)
104  {
105  if (dev.device_name == it->device_name)
106  {
107  m_robot_devices.erase(it);
108  return true;
109  }
110  }
111  return false;
112 }
113 
115 {
116  std::lock_guard<std::mutex> guard(m_external_mutex);
117  yarp::os::Bottle in;
118  yarp::os::Bottle out;
119  bool ret;
120  int code;
121 
122  bool ok = in.read(connection);
123  if (!ok) return false;
124 
125  // parse in, prepare out
126  code = in.get(0).asVocab();
127  ret = false;
128 
129  if (code == VOCAB_IROBOT_DESCRIPTION)
130  {
131  int macro_cmd = in.get(1).asVocab();
132  if (macro_cmd == VOCAB_IROBOT_GET)
133  {
134  int cmd = in.get(2).asVocab();
135  if (cmd == VOCAB_IROBOT_ALL)
136  {
137  out.addVocab(VOCAB_OK);
138  Bottle& l = out.addList();
139  for (auto& m_robot_device : m_robot_devices)
140  {
141  l.addString(m_robot_device.device_name);
142  l.addString(m_robot_device.device_type);
143  }
144  ret = true;
145  }
146  else if (cmd == VOCAB_IROBOT_BY_TYPE)
147  {
148  std::string type = in.get(3).asString();
149  out.addVocab(VOCAB_OK);
150  Bottle& l = out.addList();
151  for (auto& m_robot_device : m_robot_devices)
152  {
153  if (m_robot_device.device_type == type)
154  {
155  l.addString(m_robot_device.device_name);
156  l.addString(m_robot_device.device_type);
157  }
158  }
159  ret = true;
160  }
161  else
162  {
163  ret = false;
164  yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
165  }
166 
167  }
168  else if (macro_cmd == VOCAB_IROBOT_DELETE)
169  {
170  int cmd = in.get(2).asVocab();
171  if (cmd == VOCAB_IROBOT_DEVICE)
172  {
173  std::string name = in.get(3).asString();
174  DeviceDescription dev;
175  dev.device_name = name;
176  bool b_rem = this->remove_device(dev);
177  if (b_rem == false)
178  {
179  yCError(ROBOTDESCRIPTIONSERVER) << "remove_device failed";
180  }
181  else
182  {
183  out.addVocab(VOCAB_OK);
184  ret = true;
185  }
186  }
187  else
188  {
189  ret = false;
190  yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
191  }
192  }
193  else if (macro_cmd == VOCAB_IROBOT_SET)
194  {
195  int cmd = in.get(2).asVocab();
196  if (cmd == VOCAB_IROBOT_DEVICE)
197  {
198  DeviceDescription desc;
199  desc.device_name = in.get(3).asString();
200  desc.device_type = in.get(4).asString();
201  bool b_add = this->add_device(desc);
202  if (b_add == false)
203  {
204  yCError(ROBOTDESCRIPTIONSERVER) << "add_device failed";
205  }
206  else
207  {
208  out.addVocab(VOCAB_OK);
209  ret = true;
210  }
211  }
212  else
213  {
214  ret = false;
215  yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
216  }
217  }
218  else
219  {
220  ret = false;
221  yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
222  }
223  }
224  else
225  {
226  ret = false;
227  yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
228  }
229 
230  if (!ret)
231  {
232  out.clear();
233  out.addVocab(VOCAB_FAILED);
234  }
235 
236  yarp::os::ConnectionWriter *returnToSender = connection.getWriter();
237 
238  if (returnToSender != nullptr)
239  {
240  out.write(*returnToSender);
241  }
242 
243  return true;
244 }
LogStream.h
VOCAB_IROBOT_BY_TYPE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_BY_TYPE
Definition: IRobotDescription.h:74
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::clear
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
RobotDescriptionServer::attachAll
bool attachAll(const yarp::dev::PolyDriverList &l) override
Attach to a list of objects.
Definition: RobotDescriptionServer.cpp:52
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig
Signal processing.
Definition: Image.h:25
VOCAB_IROBOT_DEVICE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DEVICE
Definition: IRobotDescription.h:73
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
yarp::dev::PolyDriverList::size
int size() const
Definition: PolyDriverList.cpp:39
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
RobotDescriptionServer::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: RobotDescriptionServer.cpp:114
yarp::dev::DeviceDescription::device_type
std::string device_type
Definition: IRobotDescription.h:23
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::dev::PolyDriverList
Definition: PolyDriverList.h:22
RobotDescriptionServer::detachAll
bool detachAll() override
Detach the object (you must have first called attach).
Definition: RobotDescriptionServer.cpp:72
RobotDescriptionServer.h
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
IRobotDescription.h
VOCAB_IROBOT_DESCRIPTION
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DESCRIPTION
Definition: IRobotDescription.h:68
Log.h
yarp::dev::DeviceDescription
Definition: IRobotDescription.h:21
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::addList
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:185
yarp::os::Bottle::write
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:233
VOCAB_IROBOT_SET
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_SET
Definition: IRobotDescription.h:70
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
VOCAB_FAILED
constexpr yarp::conf::vocab32_t VOCAB_FAILED
Definition: GenericVocabs.h:19
yarp::dev::DeviceDescription::device_name
std::string device_name
Definition: IRobotDescription.h:22
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
yarp::os::ConnectionReader::getWriter
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
RobotDescriptionServer::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: RobotDescriptionServer.cpp:28
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
VOCAB_IROBOT_DELETE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DELETE
Definition: IRobotDescription.h:71
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
LogComponent.h
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
VOCAB_IROBOT_GET
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_GET
Definition: IRobotDescription.h:69
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
RobotDescriptionServer::close
bool close() override
Close the DeviceDriver.
Definition: RobotDescriptionServer.cpp:79
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
VOCAB_IROBOT_ALL
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_ALL
Definition: IRobotDescription.h:72
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18