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