YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
DeviceBundler.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "DeviceBundler.h"
7
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12namespace {
13YARP_LOG_COMPONENT(DEVICEBUNDLER, "yarp.device.DeviceBundler")
14}
15
19
23
25{
26 if (!this->parseParams(config)) {return false;}
27
28 std::string s = config.toString();
29
30 bool ret = true;
31
32 //open wrapper device
33 yCInfo(DEVICEBUNDLER, "Opening device: %s", m_wrapper_device.c_str());
34 yarp::os::Property config_wrap (config.toString().c_str());
35 config_wrap.unput("device");
36 config_wrap.unput("wrapper_device");
37 config_wrap.unput("attached_device");
38 config_wrap.unput("wrapping_enabled");
39 config_wrap.put("device", m_wrapper_device);
40 std::string sw = config_wrap.toString();
41 ret = m_pdev_wrapper.open(config_wrap);
42 if (!ret || !m_pdev_wrapper.isValid())
43 {
44 yCError(DEVICEBUNDLER, "Unable to open device:%s", m_wrapper_device.c_str());
45 return false;
46 }
47
48 //open secondary device
49 yCInfo(DEVICEBUNDLER, "Opening device: %s", m_attached_device.c_str());
50 yarp::os::Property config_sub(config.toString().c_str());
51 config_sub.unput("device");
52 config_sub.unput("wrapper_device");
53 config_sub.unput("attached_device");
54 config_sub.unput("wrapping_enabled");
55 config_sub.put("device", m_attached_device);
56 std::string ss = config_sub.toString();
57 ret = m_pdev_subdevice.open(config_sub);
58 if (!ret || !m_pdev_subdevice.isValid())
59 {
60 yCError(DEVICEBUNDLER, "Unable to open subdevice:%s", m_attached_device.c_str());
61 return false;
62 }
63
64 if (m_doNotAttach)
65 {
66 yCInfo(DEVICEBUNDLER, "doNotAttach option found. Device will not be attached.");
67 return true;
68 }
69
70 //Attach operations below
71 yCInfo(DEVICEBUNDLER, "Attaching devices %s and %s.", m_wrapper_device.c_str(), m_attached_device.c_str());
72 ret = m_pdev_wrapper.view(m_iWrapper);
73 if (!ret)
74 {
75 yCError(DEVICEBUNDLER, "Unable to open iWrapper interface. Maybe %s is not a wrapper device?", m_wrapper_device.c_str());
76 return false;
77 }
78
79 ret = m_iWrapper->attach(&m_pdev_subdevice);
80 if (!ret)
81 {
82 yCError(DEVICEBUNDLER, "Unable to attach the two devices: %s and %s", m_wrapper_device.c_str(), m_attached_device.c_str());
83 return false;
84 }
85
86 yCInfo(DEVICEBUNDLER, "Attach operation between %s and %s completed.", m_wrapper_device.c_str(), m_attached_device.c_str());
87 return true;
88}
89
91{
92 if (m_iWrapper)
93 {
94 m_iWrapper->detach();
95 m_iWrapper = nullptr;
96 }
97
98 if (m_pdev_wrapper.isValid())
99 {
100 m_pdev_wrapper.close();
101 }
102
103 if (m_pdev_subdevice.isValid())
104 {
105 m_pdev_subdevice.close();
106 }
107
108 return true;
109}
bool ret
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
~DeviceBundler() override
bool view(T *&x)
Get an interface to the device driver.
virtual bool detach()=0
Detach the object (you must have first called attach).
virtual bool attach(PolyDriver *driver)=0
Attach to another object.
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
bool open(const std::string &txt)
Construct and configure a device by its common name.
A class for storing options and configuration information.
Definition Property.h:33
std::string toString() const override
Return a standard text representation of the content of the object.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
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.
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)