25#define EXPANSION_PROTOCOL_DEFAULT_BAUD_RATE (9600UL)
30#define EXPANSION_PROTOCOL_MAX_DATA_SIZE (64U)
35#define EXPANSION_PROTOCOL_TIMEOUT_MS (250U)
40#define EXPANSION_PROTOCOL_BAUD_CHANGE_DT_MS (25U)
191static inline size_t expansion_frame_get_encoded_size(
const ExpansionFrame* frame) {
194 return sizeof(frame->
header);
219static inline bool expansion_frame_get_remaining_size(
221 size_t received_size,
222 size_t* remaining_size) {
260 if(content_size > received_content_size) {
261 *remaining_size = content_size - received_content_size;
289 expansion_protocol_get_checksum(
const uint8_t* data,
size_t data_size) {
291 for(
size_t i = 0; i < data_size; ++i) {
311 size_t total_size = 0;
312 size_t remaining_size;
315 if(!expansion_frame_get_remaining_size(frame, total_size, &remaining_size)) {
317 }
else if(remaining_size == 0) {
321 const size_t received_size =
322 receive((uint8_t*)frame + total_size, remaining_size, context);
324 if(received_size == 0) {
328 total_size += received_size;
332 const size_t received_size = receive(&checksum,
sizeof(checksum), context);
334 if(received_size !=
sizeof(checksum)) {
336 }
else if(checksum != expansion_protocol_get_checksum((
const uint8_t*)frame, total_size)) {
355 const size_t encoded_size = expansion_frame_get_encoded_size(frame);
356 if(encoded_size == 0) {
361 expansion_protocol_get_checksum((
const uint8_t*)frame, encoded_size);
363 if((send((
const uint8_t*)frame, encoded_size, context) != encoded_size) ||
364 (send(&checksum,
sizeof(checksum), context) !=
sizeof(checksum))) {
ExpansionFrameControlCommand
Enumeration of suported control commands.
Definition expansion_protocol.h:66
@ ExpansionFrameControlCommandStartRpc
Start an RPC session.
Definition expansion_protocol.h:71
@ ExpansionFrameControlCommandEnableOtg
Enable OTG (5V) on external GPIO.
Definition expansion_protocol.h:82
@ ExpansionFrameControlCommandStopRpc
Stop an open RPC session.
Definition expansion_protocol.h:76
@ ExpansionFrameControlCommandDisableOtg
Disable OTG (5V) on external GPIO.
Definition expansion_protocol.h:88
ExpansionProtocolStatus
Enumeration of protocol parser statuses.
Definition expansion_protocol.h:272
@ ExpansionProtocolStatusErrorFormat
Invalid frame type.
Definition expansion_protocol.h:274
@ ExpansionProtocolStatusOk
No error has occurred.
Definition expansion_protocol.h:273
@ ExpansionProtocolStatusErrorChecksum
Checksum mismatch.
Definition expansion_protocol.h:275
@ ExpansionProtocolStatusErrorCommunication
Input/output error.
Definition expansion_protocol.h:276
ExpansionFrameError
Enumeration of possible error types.
Definition expansion_protocol.h:57
@ ExpansionFrameErrorNone
No error occurred.
Definition expansion_protocol.h:58
@ ExpansionFrameErrorUnknown
An unknown error has occurred (generic response).
Definition expansion_protocol.h:59
@ ExpansionFrameErrorBaudRate
Requested baud rate is not supported.
Definition expansion_protocol.h:60
ExpansionFrameType
Enumeration of supported frame types.
Definition expansion_protocol.h:45
@ ExpansionFrameTypeData
Data frame.
Definition expansion_protocol.h:50
@ ExpansionFrameTypeHeartbeat
Heartbeat frame.
Definition expansion_protocol.h:46
@ ExpansionFrameTypeReserved
Special value.
Definition expansion_protocol.h:51
@ ExpansionFrameTypeControl
Control frame.
Definition expansion_protocol.h:49
@ ExpansionFrameTypeStatus
Status report frame.
Definition expansion_protocol.h:47
@ ExpansionFrameTypeBaudRate
Baud rate negotiation frame.
Definition expansion_protocol.h:48
#define EXPANSION_PROTOCOL_MAX_DATA_SIZE
Maximum data size per frame, in bytes.
Definition expansion_protocol.h:30
size_t(* ExpansionFrameReceiveCallback)(uint8_t *data, size_t data_size, void *context)
Receive function type declaration.
Definition expansion_protocol.h:169
size_t(* ExpansionFrameSendCallback)(const uint8_t *data, size_t data_size, void *context)
Send function type declaration.
Definition expansion_protocol.h:181
uint8_t ExpansionFrameChecksum
Expansion checksum type.
Definition expansion_protocol.h:157
Baud rate frame contents.
Definition expansion_protocol.h:117
uint32_t baud
Requested baud rate.
Definition expansion_protocol.h:118
Control frame contents.
Definition expansion_protocol.h:124
uint8_t command
Control command number.
Definition expansion_protocol.h:125
Data frame contents.
Definition expansion_protocol.h:131
uint8_t bytes[EXPANSION_PROTOCOL_MAX_DATA_SIZE]
Data bytes.
Definition expansion_protocol.h:135
uint8_t size
Size of the data.
Definition expansion_protocol.h:133
Heartbeat frame contents.
Definition expansion_protocol.h:103
Expansion protocol frame structure.
Definition expansion_protocol.h:141
ExpansionFrameStatus status
Status frame contents.
Definition expansion_protocol.h:145
ExpansionFrameData data
Data frame contents.
Definition expansion_protocol.h:148
ExpansionFrameHeader header
Header of the frame.
Definition expansion_protocol.h:142
ExpansionFrameBaudRate baud_rate
Baud rate frame contents.
Definition expansion_protocol.h:146
union ExpansionFrame::@4 content
Contents of the frame.
ExpansionFrameHeartbeat heartbeat
Heartbeat frame contents.
Definition expansion_protocol.h:144
ExpansionFrameControl control
Control frame contents.
Definition expansion_protocol.h:147
Status frame contents.
Definition expansion_protocol.h:110
uint8_t error
Reported error code.
Definition expansion_protocol.h:111