-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy patherpc_serial_transport.cpp
106 lines (91 loc) · 2.37 KB
/
erpc_serial_transport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (c) 2014, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_serial_transport.hpp"
#include "erpc_message_buffer.hpp"
#include "erpc_serial.h"
#include <cstdio>
#include <string>
extern "C" {
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#include <windows.h>
#else
#include <termios.h>
#endif
}
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
SerialTransport::SerialTransport(const char *portName, speed_t baudRate) :
m_serialHandle(0), m_portName(portName), m_baudRate(baudRate)
{
}
SerialTransport::~SerialTransport(void)
{
serial_close(m_serialHandle);
}
erpc_status_t SerialTransport::init(uint8_t vtime, uint8_t vmin)
{
erpc_status_t status = kErpcStatus_Success;
m_serialHandle = serial_open(m_portName);
if (-1 == m_serialHandle)
{
status = kErpcStatus_InitFailed;
}
#ifdef _WIN32
// TODO
#else
if (status == kErpcStatus_Success)
{
if (!isatty(m_serialHandle))
{
status = kErpcStatus_InitFailed;
}
}
#endif
if (status == kErpcStatus_Success)
{
if (-1 == serial_setup(m_serialHandle, m_baudRate))
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
if (-1 == serial_set_read_timeout(m_serialHandle, vtime, vmin))
{
status = kErpcStatus_InitFailed;
}
}
#ifdef _WIN32
// TODO
#else
if (status == kErpcStatus_Success)
{
if (-1 == tcflush(m_serialHandle, TCIOFLUSH))
{
status = kErpcStatus_InitFailed;
}
}
#endif
return status;
}
erpc_status_t SerialTransport::underlyingSend(const uint8_t *data, uint32_t size)
{
uint32_t bytesWritten = serial_write(m_serialHandle, reinterpret_cast<const char *>(data), size);
return (size != bytesWritten) ? kErpcStatus_SendFailed : kErpcStatus_Success;
}
erpc_status_t SerialTransport::underlyingReceive(uint8_t *data, uint32_t size)
{
uint32_t bytesRead = serial_read(m_serialHandle, reinterpret_cast<char *>(data), size);
return (size != bytesRead) ? kErpcStatus_ReceiveFailed : kErpcStatus_Success;
}