-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy patherpc_framed_transport.h
More file actions
149 lines (127 loc) · 4.39 KB
/
Copy patherpc_framed_transport.h
File metadata and controls
149 lines (127 loc) · 4.39 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
* Copyright 2016-2020 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _EMBEDDED_RPC__FRAMED_TRANSPORT_H_
#define _EMBEDDED_RPC__FRAMED_TRANSPORT_H_
#include "erpc_config_internal.h"
#include "erpc_message_buffer.h"
#include "erpc_transport.h"
#include <cstring>
#if !ERPC_THREADS_IS(NONE)
#include "erpc_threading.h"
#endif
/*!
* @addtogroup infra_transport
* @{
* @file
*/
////////////////////////////////////////////////////////////////////////////////
// Classes
////////////////////////////////////////////////////////////////////////////////
namespace erpc {
/*! @brief Contents of the header that prefixes each message. */
struct Header
{
uint16_t m_messageSize; //!< Size in bytes of the message, excluding the header.
uint16_t m_crc; //!< CRC-16 over the message data.
};
/*!
* @brief Base class for framed transport layers.
*
* This class adds simple framing to the data transmitted and received on the
* communications channel. This allows the transport to perform reads and writes
* of a size known in advance. Subclasses must implement the underlyingSend() and
* underlyingReceive() methods to actually transmit and receive data.
*
* Frames have a maximum size of 64kB, as a 16-bit frame size is used.
*
* @note This implementation currently assumes both sides of the communications channel
* are the same endianness.
*
* The frame header includes a CRC-16 over the data for integrity checking. This class
* includes a default CRC-16 implementation that is optimized for code size, but is
* relatively slow. If a faster implementation is desired, you can pass the new CRC
* function to setCRCFunction().
*
* @ingroup infra_transport
*/
class FramedTransport : public Transport
{
public:
/*!
* @brief Constructor.
*/
FramedTransport(void);
/*!
* @brief Codec destructor
*/
virtual ~FramedTransport(void);
/*!
* @brief Receives an entire message.
*
* The frame header and message data are received. The CRC-16 in the frame header is
* compared with the computed CRC. If the received CRC is invalid, #kErpcStatus_Fail
* will be returned.
*
* The @a message is only filled with the message data, not the frame header.
*
* This function is blocking.
*
* @param[in] message Message buffer, to which will be stored incoming message.
*
* @retval kErpcStatus_Success When receiving was successful.
* @retval kErpcStatus_CrcCheckFailed When receiving failed.
* @retval other Subclass may return other errors from the underlyingReceive() method.
*/
virtual erpc_status_t receive(MessageBuffer *message) override;
/*!
* @brief Function to send prepared message.
*
* @param[in] message Pass message buffer to send.
*
* @retval kErpcStatus_Success When sending was successful.
* @retval other Subclass may return other errors from the underlyingSend() method.
*/
virtual erpc_status_t send(MessageBuffer *message) override;
/*!
* @brief This functions sets the CRC-16 implementation.
*
* @param[in] crcImpl Object containing crc-16 compute function.
*/
virtual void setCrc16(Crc16 *crcImpl) override;
protected:
Crc16 *m_crcImpl; /*!< CRC object. */
#if !ERPC_THREADS_IS(NONE)
Mutex m_sendLock; //!< Mutex protecting send.
Mutex m_receiveLock; //!< Mutex protecting receive.
#endif
/*!
* @brief Subclasses must implement this function to send data.
*
* @param[in] data Buffer to send.
* @param[in] size Size of data to send.
*
* @retval kErpcStatus_Success When data was written successfully.
* @retval kErpcStatus_Fail When writing data ends with error.
*/
virtual erpc_status_t underlyingSend(const uint8_t *data, uint32_t size) = 0;
/*!
* @brief Subclasses must implement this function to receive data.
*
* @param[inout] data Preallocated buffer for receiving data.
* @param[in] size Size of data to read.
*
* @retval kErpcStatus_Success When data was read successfully.
* @retval kErpcStatus_Fail When reading data ends with error.
*/
virtual erpc_status_t underlyingReceive(uint8_t *data, uint32_t size) = 0;
};
} // namespace erpc
/*! @} */
#endif // _EMBEDDED_RPC__FRAMED_TRANSPORT_H_