YARP
Yet Another Robot Platform
RemoteControlBoardRemapper.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 
7 
8 #include <yarp/os/Log.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 
12 #include <algorithm>
13 #include <iostream>
14 #include <map>
15 #include <cassert>
16 
17 using namespace yarp::os;
18 using namespace yarp::dev;
19 using namespace yarp::sig;
20 
21 namespace {
22 YARP_LOG_COMPONENT(REMOTECONTROLBOARDREMAPPER, "yarp.device.remotecontrolboardremapper")
23 }
24 
25 
26 void RemoteControlBoardRemapper::closeAllRemoteControlBoards()
27 {
28  for(auto& m_remoteControlBoardDevice : m_remoteControlBoardDevices)
29  {
30  if( m_remoteControlBoardDevice )
31  {
32  m_remoteControlBoardDevice->close();
33  delete m_remoteControlBoardDevice;
34  m_remoteControlBoardDevice = nullptr;
35  }
36  }
37 
38  m_remoteControlBoardDevices.resize(0);
39 }
40 
41 
43 {
44  bool ret = true;
45 
47 
48  ret = ret && ok;
49 
51 
52  ret = ret && ok;
53 
54  closeAllRemoteControlBoards();
55 
56  return ret;
57 }
58 
60 {
61  Property prop;
62  prop.fromString(config.toString());
63 
64  std::string localPortPrefix;
65  std::vector<std::string> remoteControlBoardsPorts;
66 
67  // Check if the required parameters are found
68  if( prop.check("localPortPrefix") && prop.find("localPortPrefix").isString() )
69  {
70  localPortPrefix = prop.find("localPortPrefix").asString();
71  }
72  else
73  {
74  yCError(REMOTECONTROLBOARDREMAPPER) << "Parsing parameters: \"localPortPrefix\" should be a string.";
75  return false;
76  }
77 
78  Bottle *remoteControlBoards=prop.find("remoteControlBoards").asList();
79  if(remoteControlBoards==nullptr)
80  {
81  yCError(REMOTECONTROLBOARDREMAPPER) << "Parsing parameters: \"remoteControlBoards\" should be followed by a list.";
82  return false;
83  }
84 
85  remoteControlBoardsPorts.resize(remoteControlBoards->size());
86  for(size_t ax=0; ax < remoteControlBoards->size(); ax++)
87  {
88  remoteControlBoardsPorts[ax] = remoteControlBoards->get(ax).asString();
89  }
90 
91  // Load the REMOTE_CONTROLBOARD_OPTIONS, containing any additional option to pass to the remote control boards
92  Property remoteControlBoardsOptions;
93 
94  Bottle & optionsGroupBot = prop.findGroup("REMOTE_CONTROLBOARD_OPTIONS");
95  if( !(optionsGroupBot.isNull()) )
96  {
97  remoteControlBoardsOptions.fromString(optionsGroupBot.toString());
98  }
99 
100  // Parameters loaded, open all the remote controlboards
101 
102  m_remoteControlBoardDevices.resize(remoteControlBoardsPorts.size(),nullptr);
103 
104  PolyDriverList remoteControlBoardsList;
105 
106  for(size_t ctrlBrd=0; ctrlBrd < remoteControlBoardsPorts.size(); ctrlBrd++ )
107  {
108  std::string remote = remoteControlBoardsPorts[ctrlBrd];
109  // Note: as local parameter we use localPortPrefix+remoteOfTheReportControlBoard
110  std::string local = localPortPrefix+remote;
111 
112  Property options = remoteControlBoardsOptions;
113  options.put("device", "remote_controlboard");
114  options.put("local", local);
115  options.put("remote", remote);
116 
117  m_remoteControlBoardDevices[ctrlBrd] = new PolyDriver();
118 
119  bool ok = m_remoteControlBoardDevices[ctrlBrd]->open(options);
120 
121  if( !ok || !(m_remoteControlBoardDevices[ctrlBrd]->isValid()) )
122  {
123  yCError(REMOTECONTROLBOARDREMAPPER) << "Opening remote_controlboard with remote \"" << remote << "\", opening the device failed.";
124  closeAllRemoteControlBoards();
125  return false;
126  }
127 
128  // We use the remote name of the remote_controlboard as the key for it, in absence of anything better
129  remoteControlBoardsList.push((m_remoteControlBoardDevices[ctrlBrd]),remote.c_str());
130  }
131 
132  // Device opened, now we open the ControlBoardRemapper and then we call attachAll
133  bool ok = ControlBoardRemapper::open(prop);
134 
135  if( !ok )
136  {
137  yCError(REMOTECONTROLBOARDREMAPPER) << "Opening the controlboardremapper device, opening the device failed.";
139  closeAllRemoteControlBoards();
140  return false;
141  }
142 
143  // If open went ok, we now call attachAll
144  ok = ControlBoardRemapper::attachAll(remoteControlBoardsList);
145 
146  if( !ok )
147  {
148  yCError(REMOTECONTROLBOARDREMAPPER) << "Calling attachAll in the controlboardremapper device, opening the device failed.";
150  closeAllRemoteControlBoards();
151  return false;
152  }
153 
154  // All went ok, return true
155  // TODO: close devices that are not actually used by the remapper
156  return true;
157 }
bool ret
bool close() override
Close the device driver by deallocating all resources and closing ports.
bool open(yarp::os::Searchable &prop) override
Open the device driver.
bool detachAll() override
Detach the object (you must have first called attach).
bool attachAll(const yarp::dev::PolyDriverList &l) override
Attach to a list of objects.
bool open(yarp::os::Searchable &prop) override
Open the device driver.
bool close() override
Close the device driver by deallocating all resources and closing ports.
void push(PolyDriver *p, const char *k)
A container for a device driver.
Definition: PolyDriver.h:24
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
bool isNull() const override
Checks if the object is invalid.
Definition: Bottle.cpp:370
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
A class for storing options and configuration information.
Definition: Property.h:34
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1051
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1063
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1041
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Property.cpp:1142
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:156
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:240
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
#define yCError(component,...)
Definition: LogComponent.h:154
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:314
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22