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 std::string rootName =
28 prop.check("name",Value("/serial"),
29 "prefix for port names").asString();
30
31 command_buffer.attach(toDevice);
32 reply_buffer.attach(fromDevice);
33
34 toDevice.open(rootName+"/in");
35 fromDevice.open(rootName+"/out");
36
37 if (!m_rpcPort.open(rootName+"/rpc"))
38 {
39 yCError(SERIAL_NWS, "Failed to open rpc port");
40 return false;
41 }
42 m_rpcPort.setReader(*this);
43
44 yCInfo(SERIAL_NWS, "Device waiting for attach...");
45 return true;
46}
47
49{
50 yCInfo(SERIAL_NWS, "Server Serial starting");
51
52 //double before, now;
53 while (!isStopping())
54 {
55 //before = SystemClock::nowSystem();
56 Bottle& b = reply_buffer.get();
57 b.clear();
58 receive( b );
59 /*if(b.size() > 0)*/ /* this line was creating a memory leak !! */
60 reply_buffer.write();
61 //now = SystemClock::nowSystem();
62 // give other threads the chance to run
64 }
65 yCInfo(SERIAL_NWS, "Server Serial stopping");
66}
67
68bool SerialPort_nws_yarp::attach(yarp::dev::PolyDriver* deviceToAttach)
69{
70 if (deviceToAttach->isValid())
71 {
72 deviceToAttach->view(m_iserial);
73 }
74
75 if (nullptr == m_iserial)
76 {
77 yCError(SERIAL_NWS, "Subdevice passed to attach method is invalid");
78 return false;
79 }
80
81 yCInfo(SERIAL_NWS, "Attach done");
82
83 callback_impl = new ImplementCallbackHelper2(m_iserial);
84 command_buffer.useCallback(*callback_impl);
85 m_rpc.setInterfaces(m_iserial);
86
87 start();
88 return true;
89}
90
91bool SerialPort_nws_yarp::detach()
92{
93 m_iserial = nullptr;
94 return true;
95}
96
97bool SerialPort_nws_yarp::receive(Bottle& msg)
98{
99 if (m_iserial != nullptr)
100 {
101 m_iserial->receive(msg);
102 return true;
103 }
104 else
105 {
106 return false;
107 }
108}
109
110int SerialPort_nws_yarp::receiveChar(char& c)
111{
112 if (m_iserial != nullptr)
113 {
114 return m_iserial->receiveChar(c);
115 }
116 else
117 {
118 return -1;
119 }
120}
121
122bool SerialPort_nws_yarp::closeMain()
123{
124 if (Thread::isRunning()) {
125 Thread::stop();
126 }
127 //close the port connection here
128 command_buffer.disableCallback();
129 toDevice.close();
130 fromDevice.close();
131 m_rpcPort.close();
132 if (callback_impl) {delete callback_impl; callback_impl=nullptr;}
133 return true;
134}
135
137{
138 bool b = m_rpc.read(connection);
139 if (b)
140 {
141 return true;
142 }
143 else
144 {
145 yCError(SERIAL_NWS, "read() Command failed");
146 return false;
147 }
148}
149
150//--------------------------------------------------
151// RPC methods
152bool ISerialMsgsd::setDTR(bool enable)
153{
154 std::lock_guard <std::mutex> lg(m_mutex);
155 if (m_iser)
156 {
157 return m_iser->setDTR(enable);
158 }
159 yCError(SERIAL_NWS, "ISerialDevice interface was not set");
160 return false;
161}
162
164{
165 std::lock_guard <std::mutex> lg(m_mutex);
166 if (m_iser)
167 {
168 return m_iser->flush();
169 }
170 yCError(SERIAL_NWS, "ISerialDevice interface was not set");
171 return false;
172}
173
174//--------------------------------------------------
175// ImplementCallbackHelper class.
177{
178 if (x ==nullptr)
179 {
180 yCError(SERIAL_NWS, "Could not get ISerialDevice interface");
181 std::exit(1);
182 }
183 m_iser= x;
184}
185
187{
188 if (m_iser)
189 {
190 bool ok = m_iser->send(b);
191 if (!ok)
192 {
193 yCError(SERIAL_NWS, "Problems while trying to send data");
194 }
195 }
196 else
197 {
198 yCError(SERIAL_NWS, "ISerialDevice interface was not set");
199 }
200}
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
Callback implementation after buffered input.
yarp::dev::ISerialDevice * m_iser
void onRead(Bottle &b) override
Callback method.
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.
An interface for reading from a network connection.
void attach(Port &port)
Attach this buffer to a particular port.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void disableCallback() override
Remove a callback set up with useCallback()
T & get()
A synonym of PortWriterBuffer::prepare.
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
void attach(Port &port)
Set the Port to which objects will be written.
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:511
void close() override
Stop port activity.
Definition Port.cpp:363
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:56
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
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
A single value (typically within a Bottle).
Definition Value.h:43
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)