Skip to content

Commit 39bd955

Browse files
committed
[SW] fix segfault
1 parent 142ef91 commit 39bd955

File tree

2 files changed

+68
-43
lines changed

2 files changed

+68
-43
lines changed

software/serialbridge/src/SerialPort.cpp

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ static std::map<enum SerialPort::eFlowControl, serial_port_base::flow_control> c
3737
struct SerialPort_Params
3838
{
3939
std::string device;
40-
uint32_t baudrate;
41-
enum SerialPort::eFlowControl flowControl;
40+
uint32_t baudrate = 115200;
41+
enum SerialPort::eFlowControl flowControl = SerialPort::eFlowControl::None;
4242
SerialPort::ISerialHandler* handler = nullptr;
4343

4444
SerialPort_Params(const std::string& device, uint32_t baudrate, enum SerialPort::eFlowControl flowControl)
@@ -140,7 +140,6 @@ struct SerialPort_Private
140140
//ExecuteCloseOperation(oError); //< todo: not sure if still necessary
141141
}
142142
}
143-
144143
}
145144

146145

@@ -248,7 +247,7 @@ static bool isSerialPortPresent(const std::string& deviceName)
248247
#else
249248
{
250249
using namespace boost;
251-
return (filesystem::exists(deviceName) && filesystem::is_regular_file(deviceName));
250+
return filesystem::exists(deviceName);
252251
}
253252
#endif
254253
}
@@ -276,6 +275,7 @@ SerialPort::~SerialPort() noexcept
276275
{
277276
this->close();
278277
m_private.reset();
278+
m_params.reset();
279279
}
280280
catch (...)
281281
{
@@ -287,41 +287,48 @@ SerialPort& SerialPort::operator=(const SerialPort& rhs)
287287
if (this != &rhs)
288288
{
289289
this->m_private = rhs.m_private;
290+
this->m_params = rhs.m_params;
290291
}
291292
return *this;
292293
}
293294

294295
void SerialPort::awaitConnection(uint16_t waitMs)
295296
{
296-
if (isSerialPortPresent(m_params->device))
297+
if (nullptr != m_params)
297298
{
298-
try
299+
if (isSerialPortPresent(m_params->device))
299300
{
300-
m_private = std::shared_ptr<SerialPort_Private>(new SerialPort_Private(*m_params));
301-
}
302-
catch (...)
303-
{
304-
throw "Failed to open serial port!";
305-
}
301+
try
302+
{
303+
m_private = std::shared_ptr<SerialPort_Private>(new SerialPort_Private(*m_params));
304+
}
305+
catch (...)
306+
{
307+
throw "Failed to open serial port!";
308+
}
306309

307-
if (nullptr != m_params->handler)
310+
if (nullptr != m_params->handler)
311+
{
312+
m_params->handler->onSerialConnected();
313+
}
314+
315+
std::cout << "Serial port connected" << std::endl;
316+
}
317+
else
308318
{
309-
m_params->handler->onSerialConnected();
319+
std::this_thread::sleep_for(std::chrono::milliseconds(waitMs));
310320
}
311-
312-
std::cout << "Serial port connected" << std::endl;
313-
}
314-
else
315-
{
316-
std::this_thread::sleep_for(std::chrono::milliseconds(waitMs));
317321
}
318322
}
319323

320324
bool SerialPort::start()
321325
{
322-
if (m_private->m_serialPort.is_open())
326+
if (nullptr != m_private)
323327
{
324-
return m_private->StartReading();
328+
if (m_private->m_serialPort.is_open())
329+
{
330+
return m_private->StartReading();
331+
}
325332
}
326333

327334
return false;
@@ -330,39 +337,48 @@ bool SerialPort::start()
330337

331338
void SerialPort::setHandler(ISerialHandler* const handler)
332339
{
333-
m_params->handler = handler;
340+
if (nullptr != m_params)
341+
{
342+
m_params->handler = handler;
343+
}
334344
}
335345

336346

337347
bool SerialPort::send(const char cMsg) noexcept
338348
{
339349
try
340350
{
341-
m_private->m_ioService.post(boost::bind(&SerialPort_Private::sendChar, m_private.get(), cMsg));
351+
if (nullptr != m_private)
352+
{
353+
m_private->m_ioService.post(boost::bind(&SerialPort_Private::sendChar, m_private.get(), cMsg));
354+
return true;
355+
}
342356
}
343357
catch (...)
344358
{
345-
return false;
346359
}
347360

348-
return true;
361+
return false;
349362
}
350363

351364

352365
bool SerialPort::send(const std::string& text)
353366
{
354367
try
355368
{
356-
m_private->m_ioService.post(boost::bind(&SerialPort_Private::sendText,
357-
m_private.get(),
358-
text));
369+
if (nullptr != m_private)
370+
{
371+
m_private->m_ioService.post(boost::bind(&SerialPort_Private::sendText,
372+
m_private.get(),
373+
text));
374+
return true;
375+
}
359376
}
360377
catch (...)
361378
{
362-
return false;
363379
}
364380

365-
return true;
381+
return false;
366382
}
367383

368384

@@ -371,40 +387,51 @@ bool SerialPort::send(const uint8_t* const data, size_t length)
371387
{
372388
try
373389
{
374-
m_private->m_ioService.post(boost::bind(&SerialPort_Private::sendBinary,
375-
m_private.get(),
376-
data,
377-
length));
390+
if (nullptr != m_private)
391+
{
392+
m_private->m_ioService.post(boost::bind(&SerialPort_Private::sendBinary,
393+
m_private.get(),
394+
data,
395+
length));
396+
return true;
397+
}
378398
}
379399
catch (...)
380400
{
381-
return false;
382401
}
383402

384-
return true;
403+
return false;
385404
}
386405

387406

388407
bool SerialPort::close() noexcept
389408
{
390409
try
391410
{
411+
if (nullptr != m_private)
412+
{
392413
m_private->m_ioService.post(boost::bind(&SerialPort_Private::close,
393414
m_private.get(),
394415
boost::system::error_code()));
416+
return true;
417+
}
395418
}
396419
catch (...)
397420
{
398-
return false;
399421
}
400422

401-
return true;
423+
return false;
402424
}
403425

404426

405427
bool SerialPort::isActive() const
406428
{
429+
if (nullptr != m_private)
430+
{
407431
return m_private->m_active;
432+
}
433+
434+
return false;
408435
}
409436

410437

software/serialbridge/src/main.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "TCPServer.h"
1919

2020
const char* HelloString = "SerialBridge\n\r";
21-
const char* ReadyString = "SerialBrigde ready\n\r";
2221

2322
/* creates a tcp server socket for bridging serial UART data into a tcp network */
2423
class SerialBridge : private SerialPort::ISerialHandler,
@@ -62,9 +61,10 @@ class SerialBridge : private SerialPort::ISerialHandler,
6261
{
6362
if (tcpConnected && serialConnected)
6463
{
64+
std::cout << "TCP + Serial ready" << std::endl;
6565
if (!readySent)
6666
{
67-
tcpServer.send(ReadyString);
67+
tcpServer.send(HelloString);
6868
readySent = true;
6969
}
7070
}
@@ -96,11 +96,9 @@ class SerialBridge : private SerialPort::ISerialHandler,
9696
void onSerialWriteComplete(const char* msg, size_t length) final
9797
{
9898
}
99-
10099

101100
void onTcpClientAccept() final
102101
{
103-
tcpServer.send(HelloString);
104102
tcpConnected = true;
105103

106104
std::cout << "Client Connect" << std::endl;

0 commit comments

Comments
 (0)