YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SerialPort_nws_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
9#include <yarp/os/Os.h>
10
11namespace {
12YARP_LOG_COMPONENT(SERIAL_NWS, "yarp.devices.serialPort_nws_yarp")
13}
14
19
21{
22 return closeMain();
23}
24
26{
27 if (!parseParams(config)) { return false; }
28
29 std::string rootName = m_name;
30
31
32 toDevice.open(rootName+"/in");
33 fromDevice.open(rootName+"/out");
34
35 if (!m_rpcPort.open(rootName+"/rpc"))
36 {
37 yCError(SERIAL_NWS, "Failed to open rpc port");
38 return false;
39 }
40 m_rpcPort.setReader(*this);
41
42 yCInfo(SERIAL_NWS, "Device waiting for attach...");
43 return true;
44}
45
47{
48 yCInfo(SERIAL_NWS, "Server Serial starting");
49
50 //double before, now;
51 while (!isStopping())
52 {
53 //before = SystemClock::nowSystem();
54 Bottle& b = fromDevice.prepare();
55 b.clear();
56 receive( b );
57 /*if(b.size() > 0)*/ /* this line was creating a memory leak !! */
58 fromDevice.write();
59 //now = SystemClock::nowSystem();
60 // give other threads the chance to run
62 }
63 yCInfo(SERIAL_NWS, "Server Serial stopping");
64}
65
66bool SerialPort_nws_yarp::attach(yarp::dev::PolyDriver* deviceToAttach)
67{
68 if (deviceToAttach->isValid())
69 {
70 deviceToAttach->view(m_iserial);
71 }
72
73 if (nullptr == m_iserial)
74 {
75 yCError(SERIAL_NWS, "Subdevice passed to attach method is invalid");
76 return false;
77 }
78
79 yCInfo(SERIAL_NWS, "Attach done");
80
81 callback_impl = std::make_unique<SerialPort_CallbackHelper>(m_iserial);
82 toDevice.useCallback(*callback_impl);
83 m_rpc.setInterfaces(m_iserial);
84
85 start();
86 return true;
87}
88
89bool SerialPort_nws_yarp::detach()
90{
91 m_iserial = nullptr;
92 return true;
93}
94
95bool SerialPort_nws_yarp::receive(Bottle& msg)
96{
97 if (m_iserial != nullptr)
98 {
99 m_iserial->receive(msg);
100 return true;
101 }
102 else
103 {
104 return false;
105 }
106}
107
108int SerialPort_nws_yarp::receiveChar(char& c)
109{
110 if (m_iserial != nullptr)
111 {
112 return m_iserial->receiveChar(c);
113 }
114 else
115 {
116 return -1;
117 }
118}
119
120bool SerialPort_nws_yarp::closeMain()
121{
122 if (Thread::isRunning()) {
123 Thread::stop();
124 }
125 //close the port connection here
126 toDevice.disableCallback();
127 toDevice.close();
128 fromDevice.close();
129 m_rpcPort.close();
130 return true;
131}
132
134{
135 bool b = m_rpc.read(connection);
136 if (b)
137 {
138 return true;
139 }
140 else
141 {
142 yCError(SERIAL_NWS, "read() Command failed");
143 return false;
144 }
145}
146
147//--------------------------------------------------
148// RPC methods
149bool ISerialMsgsd::setDTR(bool enable)
150{
151 std::lock_guard <std::mutex> lg(m_mutex);
152 if (m_iser)
153 {
154 return m_iser->setDTR(enable);
155 }
156 yCError(SERIAL_NWS, "ISerialDevice interface was not set");
157 return false;
158}
159
161{
162 std::lock_guard <std::mutex> lg(m_mutex);
163 if (m_iser)
164 {
165 return m_iser->flush();
166 }
167 yCError(SERIAL_NWS, "ISerialDevice interface was not set");
168 return false;
169}
170
171//--------------------------------------------------
172// ImplementCallbackHelper class.
174{
175 if (x ==nullptr)
176 {
177 yCError(SERIAL_NWS, "Could not get ISerialDevice interface");
178 std::exit(1);
179 }
180 m_iser= x;
181}
182
184{
185 if (m_iser)
186 {
187 bool ok = m_iser->send(b);
188 if (!ok)
189 {
190 yCError(SERIAL_NWS, "Problems while trying to send data");
191 }
192 }
193 else
194 {
195 yCError(SERIAL_NWS, "ISerialDevice interface was not set");
196 }
197}
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
void setInterfaces(yarp::dev::ISerialDevice *iser)
int flush() override
bool setDTR(bool enable) override
void onRead(Bottle &b) override
yarp::dev::ISerialDevice * m_iser
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
virtual ~SerialPort_nws_yarp() override
void run() override
Main body of the new thread.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
A generic interface to serial port devices.
virtual bool setDTR(bool enable)=0
Enable/Disable DTR protocol.
virtual int flush()=0
Flushes the internal buffer.
virtual bool send(const yarp::os::Bottle &msg)=0
Sends a string of chars to the serial communications channel.
virtual int receiveChar(char &chr)=0
Gets one single char from the receive queue.
virtual bool receive(yarp::os::Bottle &msg)=0
Gets the existing chars in the receive queue.
A container for a device driver.
Definition PolyDriver.h:23
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
void disableCallback() override
Remove a callback set up with useCallback()
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
An interface for reading from a network connection.
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:470
void close() override
Stop port activity.
Definition Port.cpp:330
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
static void delaySystem(double seconds)
bool stop()
Stop the thread.
Definition Thread.cpp:81
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition Thread.cpp:99
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition Thread.cpp:105
bool start()
Start the new thread running.
Definition Thread.cpp:93
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)