YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RobotDescriptionServer.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11#include <mutex>
12
15using namespace yarp::dev;
16using namespace yarp::os;
17using namespace yarp::sig;
18
19namespace {
20YARP_LOG_COMPONENT(ROBOTDESCRIPTIONSERVER, "yarp.device.robotDescriptionServer")
21}
22
23//------------------------------------------------------------------------------------------------------------------------------
24
26{
27 if (!this->parseParams(config)) { return false; }
28
30 {
31 yCError(ROBOTDESCRIPTIONSERVER, "open(): Could not open rpc port %s, check network", m_local.c_str());
32 return false;
33 }
34
35 m_rpc_port.setReader(*this);
36 return true;
37}
38
40{
41 std::lock_guard<std::mutex> guard(m_external_mutex);
42 for (int i = 0; i < p.size(); i++)
43 {
45
46 //BROKEN HERE. TO BE REIMPLEMENTED
47 //dev.device_name = p[i]->poly->getValue("name").toString();
48 //dev.device_type = p[i]->poly->getValue("device").toString();
49
50 if (this->add_device(dev) == false)
51 {
52 yCError(ROBOTDESCRIPTIONSERVER) << "attachAll(): Something strange happened here";
53 //return false;
54 }
55 }
56 return true;
57}
58
60{
61 std::lock_guard<std::mutex> guard(m_external_mutex);
62 m_robot_devices.clear();
63 return true;
64}
65
67{
69 return true;
70}
71
72bool RobotDescriptionServer::add_device(DeviceDescription dev)
73{
74 std::lock_guard<std::mutex> guard(m_internal_mutex);
76 {
77 if (dev.device_name == m_robot_device.device_name)
78 {
79 yCWarning(ROBOTDESCRIPTIONSERVER) << "add_device(): Device" << dev.device_name << "already exists, skipping";
80 return false;
81 }
82 }
83 m_robot_devices.push_back(dev);
84 return true;
85}
86
87bool RobotDescriptionServer::remove_device(DeviceDescription dev)
88{
89 std::lock_guard<std::mutex> guard(m_internal_mutex);
90 for (auto it = m_robot_devices.begin(); it != m_robot_devices.end(); it++)
91 {
92 if (dev.device_name == it->device_name)
93 {
94 m_robot_devices.erase(it);
95 return true;
96 }
97 }
98 return false;
99}
100
102{
103 std::lock_guard<std::mutex> guard(m_external_mutex);
106 bool ret;
107 int code;
108
109 bool ok = in.read(connection);
110 if (!ok) {
111 return false;
112 }
113
114 // parse in, prepare out
115 code = in.get(0).asVocab32();
116 ret = false;
117
118 if (code == VOCAB_IROBOT_DESCRIPTION)
119 {
120 int macro_cmd = in.get(1).asVocab32();
122 {
123 int cmd = in.get(2).asVocab32();
124 if (cmd == VOCAB_IROBOT_ALL)
125 {
126 out.addVocab32(VOCAB_OK);
127 Bottle& l = out.addList();
128 for (auto& m_robot_device : m_robot_devices)
129 {
130 l.addString(m_robot_device.device_name);
131 l.addString(m_robot_device.device_type);
132 }
133 ret = true;
134 }
135 else if (cmd == VOCAB_IROBOT_BY_TYPE)
136 {
137 std::string type = in.get(3).asString();
138 out.addVocab32(VOCAB_OK);
139 Bottle& l = out.addList();
140 for (auto& m_robot_device : m_robot_devices)
141 {
142 if (m_robot_device.device_type == type)
143 {
144 l.addString(m_robot_device.device_name);
145 l.addString(m_robot_device.device_type);
146 }
147 }
148 ret = true;
149 }
150 else
151 {
152 ret = false;
153 yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
154 }
155
156 }
157 else if (macro_cmd == VOCAB_IROBOT_DELETE)
158 {
159 int cmd = in.get(2).asVocab32();
160 if (cmd == VOCAB_IROBOT_DEVICE)
161 {
162 std::string name = in.get(3).asString();
164 dev.device_name = name;
165 bool b_rem = this->remove_device(dev);
166 if (b_rem == false)
167 {
168 yCError(ROBOTDESCRIPTIONSERVER) << "remove_device failed";
169 }
170 else
171 {
172 out.addVocab32(VOCAB_OK);
173 ret = true;
174 }
175 }
176 else
177 {
178 ret = false;
179 yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
180 }
181 }
182 else if (macro_cmd == VOCAB_IROBOT_SET)
183 {
184 int cmd = in.get(2).asVocab32();
185 if (cmd == VOCAB_IROBOT_DEVICE)
186 {
188 desc.device_name = in.get(3).asString();
189 desc.device_type = in.get(4).asString();
190 bool b_add = this->add_device(desc);
191 if (b_add == false)
192 {
193 yCError(ROBOTDESCRIPTIONSERVER) << "add_device failed";
194 }
195 else
196 {
197 out.addVocab32(VOCAB_OK);
198 ret = true;
199 }
200 }
201 else
202 {
203 ret = false;
204 yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
205 }
206 }
207 else
208 {
209 ret = false;
210 yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
211 }
212 }
213 else
214 {
215 ret = false;
216 yCError(ROBOTDESCRIPTIONSERVER, "Invalid vocab received");
217 }
218
219 if (!ret)
220 {
221 out.clear();
223 }
224
226
227 if (returnToSender != nullptr)
228 {
229 out.write(*returnToSender);
230 }
231
232 return true;
233}
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_FAILED
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_SET
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DEVICE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DELETE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DESCRIPTION
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_BY_TYPE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_ALL
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_GET
bool ret
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool detachAll() override
Detach the object (you must have first called attach).
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool attachAll(const yarp::dev::PolyDriverList &l) override
Attach to a list of objects.
bool close() override
Close the DeviceDriver.
std::vector< yarp::dev::DeviceDescription > m_robot_devices
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:164
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:182
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
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
An interface for writing to a network connection.
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:511
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition Value.cpp:228
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
An interface to the operating system, including Port based communication.