YARP
Yet Another Robot Platform
ServerSerial.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006 Alexandre Bernardino
4  * SPDX-FileCopyrightText: 2006 Carlos Beltran-Gonzalez
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "ServerSerial.h"
9 
10 #include <yarp/os/LogComponent.h>
11 #include <yarp/os/Os.h>
12 
13 namespace {
14 YARP_LOG_COMPONENT(SERVERSERIAL, "yarp.devices.ServerSerial")
15 }
16 
18 {
19  closeMain();
20 }
21 
22 bool ServerSerial::send(const Bottle& msg)
23 {
24  if (verb) {
25  yCDebug(SERVERSERIAL, "std::string to send : %s", msg.toString().c_str());
26  }
27  if(serial != nullptr) {
28  serial->send(msg);
29  return true;
30  } else {
31  return false;
32  }
33 }
34 
35 bool ServerSerial::send(char *msg, size_t size)
36 {
37  if (verb) {
38  yCDebug(SERVERSERIAL, "std::string to send : %s", msg);
39  }
40  if(serial != nullptr) {
41  serial->send(msg, size);
42  return true;
43  } else {
44  return false;
45  }
46 }
47 
49 {
50  if(serial != nullptr) {
51  serial->receive(msg);
52  return true;
53  } else {
54  return false;
55  }
56 }
57 
59 {
60  if(serial != nullptr) {
61  return serial->receiveChar(c);
62  } else {
63  return -1;
64  }
65 }
66 
68 {
69  if(serial != nullptr) {
70  return serial->flush();
71  } else {
72  return -1;
73  }
74 }
75 
76 bool ServerSerial::setDTR(bool enable)
77 {
78  if (serial != nullptr) {
79  return serial->setDTR(enable);
80  } else {
81  return false;
82  }
83 }
84 
85 int ServerSerial::receiveLine(char* line, const int MaxLineLength)
86 {
87  if(serial != nullptr) {
88  return serial->receiveLine(line, MaxLineLength);
89  } else {
90  return -1;
91  }
92 }
93 
94 int ServerSerial::receiveBytes(unsigned char* bytes, const int size)
95 {
96  if (serial != nullptr) {
97  return serial->receiveBytes(bytes, size);
98  } else {
99  return -1;
100  }
101 }
102 
104  return false;
105 }
106 
108  return closeMain();
109 }
110 
112 {
113  verb = (prop.check("verbose",Value(0),"Specifies if the device is in verbose mode (0/1).").asInt32())>0;
114  if (verb) {
115  yCInfo(SERVERSERIAL, "running with verbose output");
116  }
117 
118  Value *name;
119  if (prop.check("subdevice",name,"name of specific control device to wrap")) {
120  yCDebug(SERVERSERIAL, "Subdevice %s", name->toString().c_str());
121  if (name->isString()) {
122  // maybe user isn't doing nested configuration
123  Property p;
124  p.setMonitor(prop.getMonitor(),
125  "subdevice"); // pass on any monitoring
126  p.fromString(prop.toString());
127  p.put("device",name->toString());
128  poly.open(p);
129  } else {
130  Bottle subdevice = prop.findGroup("subdevice").tail();
131  poly.open(subdevice);
132  }
133  if (!poly.isValid()) {
134  yCError(SERVERSERIAL, "cannot make <%s>", name->toString().c_str());
135  }
136  } else {
137  yCError(SERVERSERIAL, "\"--subdevice <name>\" not set for serial");
138  return false;
139  }
140 
141  if (!poly.isValid()) {
142  return false;
143  }
144 
145  std::string rootName =
146  prop.check("name",Value("/serial"),
147  "prefix for port names").asString();
148 
149  command_buffer.attach(toDevice);
150  reply_buffer.attach(fromDevice);
151 
152  command_buffer.useCallback(callback_impl);
153 
154  toDevice.open(rootName+"/in");
155  fromDevice.open(rootName+"/out");
156 
157 
158  if (poly.isValid()) {
159  poly.view(serial);
160  }
161 
162  if(serial != nullptr) {
163  start();
164  return true;
165  }
166 
167  yCError(SERVERSERIAL, "subdevice <%s> doesn't look like a serial port (no appropriate interfaces were acquired)",
168  name->toString().c_str());
169 
170  return false;
171 }
172 
174  yCInfo(SERVERSERIAL, "Server Serial starting");
175  //double before, now;
176  while (!isStopping()) {
177  //before = SystemClock::nowSystem();
178  Bottle& b = reply_buffer.get();
179  b.clear();
180  receive( b );
181  /*if(b.size() > 0)*/ /* this line was creating a memory leak !! */
182  reply_buffer.write();
183  //now = SystemClock::nowSystem();
184  // give other threads the chance to run
186  }
187  yCInfo(SERVERSERIAL, "Server Serial stopping");
188 }
189 
190 
191 // ImplementCallbackHelper class.
192 
193 
195 {
196  ser = dynamic_cast<yarp::dev::ISerialDevice *> (x);
197  if (ser==nullptr) {
198  yCError(SERVERSERIAL, "Could not get serial device");
199  std::exit(1);
200  }
201 
202 
203 }
204 
206 {
207  if (ser) {
208  bool ok = ser->send(b);
209  if (!ok) {
210  yCError(SERVERSERIAL, "Problems while trying to send data");
211  }
212  }
213 }
ImplementCallbackHelper2()
Constructor.
yarp::dev::ISerialDevice * ser
Definition: ServerSerial.h:38
void onRead(Bottle &b) override
Callback function.
serial: Export a serial sensor.
Definition: ServerSerial.h:72
void run() override
The thread main loop deals with writing on ports here.
~ServerSerial() override
bool close() override
Close the device driver by deallocating all resources and closing ports.
bool setDTR(bool enable) override
Enable/Disable DTR protocol.
virtual bool open()
Default open() method.
int receiveBytes(unsigned char *bytes, const int size) override
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
bool receive(Bottle &msg) override
Gets the existing chars in the receive queue.
int receiveChar(char &c) override
Gets one single char from the receive queue.
int receiveLine(char *line, const int MaxLineLength) override
Gets one line (a sequence of chars with a ending '\n' or '\r') from the receive queue.
bool send(const Bottle &msg) override
Sends a string of chars to the serial communications channel.
int flush() override
Flushes the internal buffer.
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
A generic interface to serial port devices.
Definition: ISerialDevice.h:25
virtual int receiveLine(char *line, const int MaxLineLength)=0
Gets one line (a sequence of chars with a ending '\n' or '\r') from the receive queue.
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.
virtual int receiveBytes(unsigned char *bytes, const int size)=0
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:196
bool open(const std::string &txt)
Construct and configure a device by its common name.
Definition: PolyDriver.cpp:140
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
Bottle tail() const
Get all but the first element of a bottle.
Definition: Bottle.cpp:388
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:121
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
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 write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
T & get()
A synonym of PortWriterBuffer::prepare.
void attach(Port &port)
Set the Port to which objects will be written.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
A class for storing options and configuration information.
Definition: Property.h:34
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1063
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
static void delaySystem(double seconds)
Definition: SystemClock.cpp:29
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition: Thread.cpp:99
bool start()
Start the new thread running.
Definition: Thread.cpp:93
A single value (typically within a Bottle).
Definition: Value.h:45
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:156
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:356
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCDebug(component,...)
Definition: LogComponent.h:109
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77