YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
10#include <yarp/os/LogStream.h>
11
12#include <algorithm>
13#include <iostream>
14#include <map>
15#include <cassert>
16
17using namespace yarp::os;
18using namespace yarp::dev;
19using namespace yarp::sig;
20
21namespace {
22YARP_LOG_COMPONENT(REMOTECONTROLBOARDREMAPPER, "yarp.device.remotecontrolboardremapper")
23}
24
25
26void RemoteControlBoardRemapper::closeAllRemoteControlBoards()
27{
28 for(auto& m_remoteControlBoardDevice : m_remoteControlBoardDevices)
29 {
31 {
33 }
34 }
35
36 m_remoteControlBoardDevices.resize(0);
37}
38
39
41{
42 bool ret = true;
43
45
46 ret = ret && ok;
47
49
50 ret = ret && ok;
51
52 closeAllRemoteControlBoards();
53
54 return ret;
55}
56
58{
59 Property prop;
60 prop.fromString(config.toString());
61
62 std::string localPortPrefix;
63 std::vector<std::string> remoteControlBoardsPorts;
64
65 // Check if the required parameters are found
66 if( prop.check("localPortPrefix") && prop.find("localPortPrefix").isString() )
67 {
68 localPortPrefix = prop.find("localPortPrefix").asString();
69 }
70 else
71 {
72 yCError(REMOTECONTROLBOARDREMAPPER) << "Parsing parameters: \"localPortPrefix\" should be a string.";
73 return false;
74 }
75
76 Bottle *remoteControlBoards=prop.find("remoteControlBoards").asList();
77 if(remoteControlBoards==nullptr)
78 {
79 yCError(REMOTECONTROLBOARDREMAPPER) << "Parsing parameters: \"remoteControlBoards\" should be followed by a list.";
80 return false;
81 }
82
84 for(size_t ax=0; ax < remoteControlBoards->size(); ax++)
85 {
87 }
88
89 // Load the REMOTE_CONTROLBOARD_OPTIONS, containing any additional option to pass to the remote control boards
91
92 Bottle & optionsGroupBot = prop.findGroup("REMOTE_CONTROLBOARD_OPTIONS");
93 if( !(optionsGroupBot.isNull()) )
94 {
95 remoteControlBoardsOptions.fromString(optionsGroupBot.toString());
96 }
97
98 // Parameters loaded, open all the remote controlboards
99
100 m_remoteControlBoardDevices.resize(remoteControlBoardsPorts.size());
101
103
104 for(size_t ctrlBrd=0; ctrlBrd < remoteControlBoardsPorts.size(); ctrlBrd++ )
105 {
106 std::string remote = remoteControlBoardsPorts[ctrlBrd];
107 // Note: as local parameter we use localPortPrefix+remoteOfTheReportControlBoard
108 std::string local = localPortPrefix+remote;
109
111 options.put("device", "controlBoard_nwc_yarp");
112 options.put("local", local);
113 options.put("remote", remote);
114
115 m_remoteControlBoardDevices[ctrlBrd] = std::make_unique<PolyDriver>();
116
117 bool ok = m_remoteControlBoardDevices[ctrlBrd]->open(options);
118
119 if( !ok || !(m_remoteControlBoardDevices[ctrlBrd]->isValid()) )
120 {
121 yCError(REMOTECONTROLBOARDREMAPPER) << "Opening controlBoard_nwc_yarp with remote \"" << remote << "\", opening the device failed.";
122 closeAllRemoteControlBoards();
123 return false;
124 }
125
126 // We use the remote name of the controlBoard_nwc_yarp as the key for it, in absence of anything better
127 remoteControlBoardsList.push((m_remoteControlBoardDevices[ctrlBrd].get()),remote.c_str());
128 }
129
130 // Device opened, now we open the ControlBoardRemapper and then we call attachAll
131 bool ok = ControlBoardRemapper::open(prop);
132
133 if( !ok )
134 {
135 yCError(REMOTECONTROLBOARDREMAPPER) << "Opening the controlboardremapper device, opening the device failed.";
137 closeAllRemoteControlBoards();
138 return false;
139 }
140
141 // If open went ok, we now call attachAll
143
144 if( !ok )
145 {
146 yCError(REMOTECONTROLBOARDREMAPPER) << "Calling attachAll in the controlboardremapper device, opening the device failed.";
148 closeAllRemoteControlBoards();
149 return false;
150 }
151
152 // All went ok, return true
153 // TODO: close devices that are not actually used by the remapper
154 return true;
155}
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.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
A base class for nested structures that can be searched.
Definition Searchable.h:31
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:252
virtual std::string asString() const
Get string value.
Definition Value.cpp:246
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13