SCOM Serial Communication Protocol  0.5.2
scom_frame.h
Go to the documentation of this file.
1 
10 #ifndef SCOM_FRAME_H_
11 #define SCOM_FRAME_H_
12 
13 
14 #include "hal.h"
15 #include <stdbool.h>
16 #include <stdint.h>
17 
18 
19 #ifndef SCOM_MAX_DATA_LENGTH
20 
28 #define SCOM_MAX_DATA_LENGTH UINT8_C(255)
29 #else
30  #if defined __arm__
31  #warning custom SCOM_MAX_DATA_LENGTH defined.
32  #else
33  #pragma message "warning: custom SCOM_MAX_DATA_LENGTH defined"
34  #endif
35 #endif
36 
37 
38 #define SCOM_SOF_CHARACTER UINT8_C(0x3a)
39 #define SCOM_FRAME_CTRL_FRAME_NUMBER_MASK UINT8_C(0x07)
40 #define SCOM_FRAME_CTRL_NOACK_MASK UINT8_C(0x08)
41 #define SCOM_FRAME_CTRL_FRAME_PRIORITY_MASK UINT8_C(0x70)
42 #define SCOM_FRAME_CTRL_MULTI_ID_MASK UINT8_C(0x80)
43 
44 
46 typedef enum {
51 
52 
54 typedef struct HAL_PACKED {
55  uint8_t sync;
56  uint8_t sof;
57  uint8_t ctrl;
58  uint8_t type;
59  uint8_t size;
60  uint8_t null;//only for alignment
62  uint16_t crc;
63  union{
66  struct {
68  } singleframe;
70  struct {
71  uint32_t multi_id;
72  uint32_t offset;
73  uint32_t total_size;
74  uint8_t data[SCOM_MAX_DATA_LENGTH - 12];
75  } multiframe;
77  struct {
78  uint8_t primitive_id;
79  uint8_t data[SCOM_MAX_DATA_LENGTH - 1];
80  } serviceframe;
82  struct {
83  uint32_t multi_id;
84  uint32_t offset;
85  uint32_t total_size;
86  uint8_t primitive_id;
87  uint8_t data[SCOM_MAX_DATA_LENGTH - 13];
89  } payload;
90 
91 #if true == SCOM_RECEIVED_FRAME_TIMESTAMP
92  uint32_t received_timestamp;
94 #endif
95 } SCOMFrame;
96 
97 
104 uint16_t SCOMFrame_GetTotalFrameSize(const SCOMFrame* const frame);
105 
109 static HAL_INLINE uint8_t SCOMFrame_GetPacketControlSize(void) {
110  SCOMFrame *frame;
111  (void)frame;
112  return sizeof(frame->ctrl) + sizeof(frame->type) + sizeof(frame->size)
113  + sizeof(frame->crc);
114 }
115 
123 bool SCOMFrame_SetType(SCOMFrame* const frame, const uint8_t type);
124 
132 uint8_t SCOMFrame_GetType(const SCOMFrame* const frame);
133 
134 
143 bool SCOMFrame_SetPriority(SCOMFrame* const frame, const uint8_t priority);
144 
153 uint8_t SCOMFrame_GetPriority(const SCOMFrame* const frame);
154 
163 bool SCOMFrame_SetFrameNumber(SCOMFrame* const frame, const uint8_t number);
164 
171 uint8_t SCOMFrame_GetFrameNumber(const SCOMFrame * const frame);
172 
181 bool SCOMFrame_SetPayloadSize(SCOMFrame* const frame, const uint8_t size);
182 
190 uint8_t SCOMFrame_GetPayloadSize(const SCOMFrame* const frame);
191 
198 uint8_t* SCOMFrame_GetPayloadPtr(const SCOMFrame* const frame);
199 
200 
210 uint32_t SCOMFrame_GetTotalPayloadSize(const SCOMFrame* const frame);
211 
219 uint32_t SCOMFrame_GetMultiId(const SCOMFrame* const frame);
220 
228 uint32_t SCOMFrame_GetMultiOffset(const SCOMFrame* const frame);
229 
238 bool SCOMFrame_SetAcknowledgement(SCOMFrame* const frame, const bool ack);
239 
246 bool SCOMFrame_IsMultiframe(const SCOMFrame* const frame);
247 
254 bool SCOMFrame_IsAcknowledged(const SCOMFrame* const frame);
255 
262 uint16_t SCOMFrame_CalculateCRC(const SCOMFrame* const frame);
263 
269 static HAL_INLINE size_t SCOMFrame_GetMaxSingleframePayloadSize(void) {
270  return SCOM_MAX_DATA_LENGTH;
271 }
272 
278 static HAL_INLINE size_t SCOMFrame_GetMaxMultiframePayloadSize(void) {
279  return SCOM_MAX_DATA_LENGTH - 12;
280 }
281 
287 static HAL_INLINE size_t SCOMFrame_GetMaxServiceMultiframePayloadSize(void) {
288  return SCOM_MAX_DATA_LENGTH - 13;
289 }
290 
291 
292 #endif /* SCOM_FRAME_H */
#define SCOM_MAX_DATA_LENGTH
Definition: scom_frame.h:28
uint8_t null
TODO: XOR from sof, ctrl, type, size.
Definition: scom_frame.h:61
uint8_t sync
synchronization field: always 0xFF
Packet type definition: IDENTIFY.
Definition: scom_frame.h:49
uint8_t primitive_id
total size of a multiframe
Definition: scom_frame.h:78
struct HAL_PACKED::@1::@4 serviceframe
payload definition for service frame
SCOM version record.
Definition: scom.h:81
uint32_t offset
multiframe packet id
Definition: scom_frame.h:72
bool SCOMFrame_SetPayloadSize(SCOMFrame *const frame, const uint8_t size)
Definition: scom_frame.c:119
uint8_t SCOMFrame_GetPayloadSize(const SCOMFrame *const frame)
Definition: scom_frame.c:133
uint8_t SCOMFrame_GetFrameNumber(const SCOMFrame *const frame)
Definition: scom_frame.c:108
uint8_t data[UINT8_C(255)]
total size of a multiframe
Definition: scom_frame.h:67
bool SCOMFrame_SetAcknowledgement(SCOMFrame *const frame, const bool ack)
Definition: scom_frame.c:192
uint32_t SCOMFrame_GetMultiId(const SCOMFrame *const frame)
Definition: scom_frame.c:166
uint16_t SCOMFrame_CalculateCRC(const SCOMFrame *const frame)
Definition: scom_frame.c:234
uint8_t type
frame type field
uint32_t received_timestamp
time stamp of received frame - value should be set as soon as possible
Definition: scom_frame.h:93
uint8_t SCOMFrame_GetPriority(const SCOMFrame *const frame)
Definition: scom_frame.c:82
uint8_t size
frame type field
Definition: scom_frame.h:59
struct HAL_PACKED::@1::@3 multiframe
payload definition for a multi frame
uint32_t total_size
multiframe current frame offset (denotes progress)
Definition: scom_frame.h:73
uint8_t SCOMFrame_GetType(const SCOMFrame *const frame)
Definition: scom_frame.c:56
uint8_t ctrl
frame control field
bool SCOMFrame_IsMultiframe(const SCOMFrame *const frame)
Definition: scom_frame.c:208
Packet type definition: PING.
Definition: scom_frame.h:47
uint16_t crc
crc field
bool SCOMFrame_SetType(SCOMFrame *const frame, const uint8_t type)
Definition: scom_frame.c:42
bool SCOMFrame_IsAcknowledged(const SCOMFrame *const frame)
Definition: scom_frame.c:221
Packet type definition: PONG.
Definition: scom_frame.h:48
bool SCOMFrame_SetPriority(SCOMFrame *const frame, const uint8_t priority)
Definition: scom_frame.c:67
union HAL_PACKED::@1 payload
payload field
struct HAL_PACKED::@1::@2 singleframe
payload definition for a single frame
struct HAL_PACKED SCOMFrame
Definition of an SCOM frame.
uint8_t sof
synchronization field: always 0xFF
Definition: scom_frame.h:56
uint32_t SCOMFrame_GetTotalPayloadSize(const SCOMFrame *const frame)
Definition: scom_frame.c:155
uint32_t SCOMFrame_GetMultiOffset(const SCOMFrame *const frame)
Definition: scom_frame.c:179
SCOMFrameType
SCOM native frame types.
Definition: scom_frame.h:46
uint8_t * SCOMFrame_GetPayloadPtr(const SCOMFrame *const frame)
Definition: scom_frame.c:144
struct HAL_PACKED::@1::@5 servicemultiframe
payload definition for a service multi frame
uint32_t multi_id
Definition: scom_frame.h:71
bool SCOMFrame_SetFrameNumber(SCOMFrame *const frame, const uint8_t number)
Definition: scom_frame.c:93
uint16_t SCOMFrame_GetTotalFrameSize(const SCOMFrame *const frame)
Definition: scom_frame.c:20