SCOM Serial Communication Protocol  0.5.2
scom.h
Go to the documentation of this file.
1 
10 #ifndef SCOM_H_
11 #define SCOM_H_
12 #define SCOM_VER_HI 0
13 #define SCOM_VER_LO 5
14 #ifndef SCOM_REVISION
15 #define SCOM_REVISION 0
16 #endif
17 
18 #if defined __cplusplus
19 extern "C" {
20 #endif
21 
22 
23 #include "hal.h"
24 #include "scom_frame_queue.h"
25 #include "scom_ack_frame.h"
26 #include <stdint.h>
27 #include <stdbool.h>
28 
37 #define SCOM_DECLARE_FRAME_BUFFER(name, txQueueSize, rxQueueSize) \
38  SCOMFrameQueueItem name##_scom_tx_frame_pool[txQueueSize]; \
39  SCOMFrameQueueItem name##_scom_rx_frame_pool[rxQueueSize]; \
40  SCOMFrameBuffer name = {{name##_scom_tx_frame_pool, NULL, txQueueSize, NULL}, \
41  {name##_scom_rx_frame_pool, NULL, rxQueueSize, NULL}}
42 
43 
50 #define SCOM_DECLARE_FRAME_BUFFER_USING_HEAP(name) \
51  SCOMFrameBuffer name = { {NULL, NULL, 0, NULL}, \
52  { NULL, NULL, 0, NULL}}
53 
54 
55 
56 
57 #if SCOM_MAX_FRAME_FILTERS > 0
58 typedef struct SCOMFilterFramePool SCOMFilterFramePool;
60 #endif
61 
63 typedef struct {
69 
70 
71 
72 #if 1 == defined HAL_ENABLE_OS
73 
74 typedef OSTime (*SCOMClockSource)(void);
75 #else
76 
77 typedef uint32_t (*SCOMClockSource)(void);
78 #endif
79 
81 typedef struct HAL_PACKED {
82  uint8_t verHi;
83  uint8_t verLo;
84  uint16_t revision;
85 } SCOMVersion;
86 
88 typedef struct HAL_PACKED {
90  uint32_t deviceClass;
91  uint8_t deviceUID[16];
92 #if SCOM_MAX_SERVICES > 0
93  uint8_t serviceID[SCOM_MAX_SERVICES];
94 #endif
95 } SCOMIdentity;
96 
97 #define SCOM_DEVICE_INFO_LEN (sizeof(((SCOMIdentity *)0)->version) + sizeof(((SCOMIdentity *)0)->deviceClass) + sizeof(((SCOMIdentity *)0)->deviceUID))
98 
100 typedef enum {
106 
108 typedef enum {
114 
116 typedef struct {
119 } SCOMACKState;
120 
122 typedef struct {
124  uint32_t framesSent;
126  uint32_t framesReceived;
132 
134 typedef struct SCOMDataLinkDesc {
135  IODevice iodevice;
137  uint32_t timeout;
139  uint8_t serviceCount;
142  bool monitorMode;
149  uint8_t lastFrameNumber;
150  uint16_t lastFrameCrc;
151  uint32_t sendTime;
152  uint32_t receptionTime;
153 #if defined SCOM_RECEPTION_TIMEOUT_AFTER_FRAME_START
154  uint32_t receptionStartTime;
155 #endif
156  uint32_t txCounter;
157  uint32_t multiCounter;
162  bool (*receptionHandler)(struct SCOMDataLinkDesc* scom, SCOMFrame *frame);
163  void (*ackHandler)(struct SCOMDataLinkDesc* scom, uint8_t frame_type);
165 #if SCOM_MAX_SERVICES > 0
166  struct {
169  uint8_t serviceID;
171  void (*handler)(struct SCOMDataLinkDesc* scom, SCOMFrame *frame);
172  } serviceHandler[SCOM_MAX_SERVICES];
173 #endif
174 #if SCOM_MAX_FRAME_FILTERS > 0
175  struct {
177  LLSTItemDesc pool[SCOM_MAX_FRAME_FILTERS];
178  LLSTListDesc list;
179  OSCntSem dataReady;
180  SCOMFilterFramePool *framePool;
181  } filters;
182 #if defined HAL_ENABLE_OS && (HAL_ENABLE_OS == 1)
183  OSTask filterTask;
184 #endif
185 #endif
186 
187 #if 1 == HAL_ENABLE_OS
188  OSMutex accessGuard;
189  OSTask procTask;
190 #endif
191 
193 
196 
202 typedef bool (*SCOMFrameReceptionHandler)(SCOMDataLink scom, SCOMFrame *frame);
203 
205 typedef void (*SCOMAckReceptionHandler)(SCOMDataLink scom, uint8_t frame_type);
206 
208 typedef void (*SCOMServiceHandler)(SCOMDataLinkDesc* scom, SCOMFrame *frame);
209 
211 typedef enum {
235 } SCOMResult;
236 
237 /* Possible errors reported by SCOM (via HAL DIAG error reporting mechanism) */
238 
239 
240 #define SCOM_ERR__COMMUNICATION_ERROR 1
241 #define SCOM_ERR__INTERNAL_ERROR 2
242 #define SCOM_ERR__DATA_TOO_LARGE 3
243 #define SCOM_ERR__TX_OVERLOAD 4
244 #define SCOM_ERR__SYNCHRONIZATION_ERROR 5
245 
246 
247 typedef struct {
248  uint32_t multi_id;
249  uint32_t progress;
251 
252 // -----------------------------------------------------------------------------
253 // SCOM API
254 // -----------------------------------------------------------------------------
255 
261 SCOMDataLink SCOM_Create(void);
262 
268 void SCOM_Destroy(SCOMDataLinkDesc* const scom);
269 
281 SCOMFrameBuffer* SCOM_CreateFrameBuffer(size_t rxBuffSize, size_t txBuffSize);
282 
288 void SCOM_DestroyFrameBuffer(SCOMFrameBuffer *frameBuffer);
289 
305 SCOMResult SCOM_Init(SCOMDataLink scom, IODevice iodevice, SCOMFrameBuffer *frameBuffer,
306  SCOMClockSource clockSource, uint32_t ackTimeout,
307  uint32_t deviceClass, uint8_t* deviceUID,
308  uint8_t numberOfServices, uint8_t* serviceIDs
309  );
310 
317 SCOMResult SCOM_Deinit(SCOMDataLink scom);
318 
332 SCOMResult SCOM_InitAsMonitor(SCOMDataLink scom, IODevice iodevice, SCOMFrameBuffer *frameBuffer, SCOMClockSource clockSource, uint32_t ackTimeout);
333 
341 SCOMResult SCOM_RunProcessingThread(SCOMDataLink scom);
342 
349 void SCOM_StopProcessingThread(SCOMDataLink scom);
350 
360 SCOMResult SCOM_EnableConnectivityCheck(SCOMDataLink scom, uint32_t checkPeriod);
368 SCOMResult SCOM_DisableConnectivityCheck(SCOMDataLink scom);
369 
380 
386 IODevice SCOM_GetIODevice(const SCOMDataLinkDesc* const scom);
387 
405 SCOMResult SCOM_SendFrame(SCOMDataLink scom, uint8_t framePriority, uint8_t frameType, const void* frameData, uint8_t frameDataSize);
406 
407 
427 SCOMResult SCOM_SendFrameWithoutAck(SCOMDataLink scom, uint8_t framePriority, uint8_t frameType, const void* frameData, uint8_t frameDataSize);
428 
429 
447 SCOMResult SCOM_SendMultiFrame(SCOMDataLink scom, uint8_t framePriority, uint8_t frameType,
448  const void* frameData, uint32_t frameDataSize, SCOMMultiFrameDesc* multiFrameDesc);
449 
463 SCOMResult SCOM_SendAllocatedMultiFrame(SCOMDataLink scom, SCOMFrame* frame, SCOMMultiFrameDesc* multiFrameDesc);
464 
477 SCOMResult SCOM_AllocFrame(SCOMDataLink scom, SCOMFrame** framePtr);
478 
491 SCOMResult SCOM_SendAllocatedFrame(SCOMDataLink scom, SCOMFrame* frame);
492 
505 SCOMResult SCOM_SendAllocatedFrameWithoutACK(SCOMDataLink scom, SCOMFrame* frame);
506 
517 SCOMResult SCOM_SetFrameReceptionHandlerFunc(SCOMDataLink scom, SCOMFrameReceptionHandler frameReceptionFunc);
518 
528 SCOMResult SCOM_SetAckReceptionHandlerFunc(SCOMDataLink scom, SCOMAckReceptionHandler ackReceptionFunc);
529 
541 SCOMResult SCOM_SetServiceHandler(SCOMDataLink scom, uint8_t serviceID, SCOMServiceHandler serviceHandlerFunc);
542 
554 SCOMResult SCOM_Proc(SCOMDataLink scom);
555 
564 SCOMFrame * SCOM_GetReceivedFrame(SCOMDataLink scom);
565 
573 void SCOM_UnlockFrame(SCOMDataLink scom, SCOMFrame *frame);
574 
582 
588 void SCOM_ResetStatistics(SCOMDataLink scom);
589 
590 
597 bool SCOM_IsBusy(SCOMDataLink scom);
598 
599 #if defined __cplusplus
600 } // extern "C"
601 #endif
602 
603 
604 #endif /* SCOM_H */
605 
Definition of multiframe descriptor structure.
Definition: scom.h:247
SCOMResult SCOM_InitAsMonitor(SCOMDataLink scom, IODevice iodevice, SCOMFrameBuffer *frameBuffer, SCOMClockSource clockSource, uint32_t ackTimeout)
Definition: scom.c:363
uint32_t multi_id
Definition: scom.h:248
SCOMFrameQueue rxFrameQueue
descriptor of the frame queue used for receiving
Definition: scom.h:67
void SCOM_DestroyFrameBuffer(SCOMFrameBuffer *frameBuffer)
Definition: scom.c:218
SCOMFrameQueue txFrameQueue
descriptor of the frame queue used for sending
Definition: scom.h:65
SCOMVersion version
SCOM version record.
Definition: scom.h:89
SCOM version record.
Definition: scom.h:81
SCOMResult SCOM_SetServiceHandler(SCOMDataLink scom, uint8_t serviceID, SCOMServiceHandler serviceHandlerFunc)
Definition: scom.c:841
SCOMResult SCOM_Proc(SCOMDataLink scom)
Definition: scom.c:859
Descriptor of the SCOM statistics.
Definition: scom.h:122
#define SCOM_MAX_SERVICES
Definition: doxygen.h:644
uint32_t progress
multiframe id
Definition: scom.h:249
SCOMResult SCOM_SendFrame(SCOMDataLink scom, uint8_t framePriority, uint8_t frameType, const void *frameData, uint8_t frameDataSize)
SCOMResult SCOM_Deinit(SCOMDataLink scom)
SCOMResult SCOM_EnableConnectivityCheck(SCOMDataLink scom, uint32_t checkPeriod)
SCOMAckFrame definition.
Frame queue.
uint8_t deviceUID[16]
Device unique identifier.
Definition: scom.h:91
Definition of a single SCOM frame queue item.
SCOMFrameState
SCOMFrame * SCOM_GetReceivedFrame(SCOMDataLink scom)
Definition: scom.c:1065
bool SCOM_IsBusy(SCOMDataLink scom)
Definition: scom.c:1044
SCOMResult SCOM_SetAckReceptionHandlerFunc(SCOMDataLink scom, SCOMAckReceptionHandler ackReceptionFunc)
uint32_t retransmissionsReceived
Number of retransmissions detected when receiving frames.
Definition: scom.h:130
SCOMResult SCOM_DisableConnectivityCheck(SCOMDataLink scom)
void SCOM_ResetStatistics(SCOMDataLink scom)
IODevice SCOM_GetIODevice(const SCOMDataLinkDesc *const scom)
Definition: scom.c:482
void(* SCOMAckReceptionHandler)(SCOMDataLink scom, uint8_t frame_type)
Definition: scom.h:205
SCOMDataLinkDesc * SCOMDataLink
Definition: scom.h:195
SCOMResult SCOM_SetFrameReceptionHandlerFunc(SCOMDataLink scom, SCOMFrameReceptionHandler frameReceptionFunc)
struct HAL_PACKED SCOMIdentity
Definition of a structure that identifies an SCOM device.
SCOMConnectionState
Possible SCOM connection states.
Definition: scom.h:108
void SCOM_StopProcessingThread(SCOMDataLink scom)
Definition: scom.c:420
SCOMReceiverState
Possible receiver states.
Definition: scom.h:100
SCOMResult
Definition: scom.h:211
struct SCOMDataLinkDesc SCOMDataLinkDesc
SCOM Data Link descriptor.
bool(* SCOMFrameReceptionHandler)(SCOMDataLink scom, SCOMFrame *frame)
Definition: scom.h:202
uint32_t retransmissionsSent
Number of retransmissions needed when sending frames.
Definition: scom.h:128
uint32_t deviceClass
Device class.
Definition: scom.h:90
SCOMResult SCOM_Init(SCOMDataLink scom, IODevice iodevice, SCOMFrameBuffer *frameBuffer, SCOMClockSource clockSource, uint32_t ackTimeout, uint32_t deviceClass, uint8_t *deviceUID, uint8_t numberOfServices, uint8_t *serviceIDs)
Definition: scom.c:230
SCOMAckFrame frame
Definition: scom.h:117
struct HAL_PACKED SCOMVersion
SCOM version record.
SCOMFrameState state
ACK frame.
Definition: scom.h:118
uint16_t revision
protocol revision
Definition: scom.h:84
void SCOM_UnlockFrame(SCOMDataLink scom, SCOMFrame *frame)
Definition: scom.c:1086
OSTime(* SCOMClockSource)(void)
Definition: scom.h:74
void SCOM_Destroy(SCOMDataLinkDesc *const scom)
Definition: scom.c:161
uint32_t framesReceived
Number of frames that were received since the SCOM connection was opened. In case of multi-frames...
Definition: scom.h:126
uint32_t framesSent
Number of frames that were sent since the SCOM connection was opened. In case of multi-frames, each sub-frame is counted.
Definition: scom.h:124
SCOMResult SCOM_SendAllocatedFrame(SCOMDataLink scom, SCOMFrame *frame)
Definition: scom.c:735
SCOMResult SCOM_RunProcessingThread(SCOMDataLink scom)
SCOMResult SCOM_SendFrameWithoutAck(SCOMDataLink scom, uint8_t framePriority, uint8_t frameType, const void *frameData, uint8_t frameDataSize)
Definition: scom.c:530
SCOMResult SCOM_SendAllocatedMultiFrame(SCOMDataLink scom, SCOMFrame *frame, SCOMMultiFrameDesc *multiFrameDesc)
Definition: scom.c:650
SCOMResult SCOM_SendMultiFrame(SCOMDataLink scom, uint8_t framePriority, uint8_t frameType, const void *frameData, uint32_t frameDataSize, SCOMMultiFrameDesc *multiFrameDesc)
Definition: scom.c:574
uint8_t verHi
version high number
Definition: scom.h:82
SCOMConnectionState SCOM_GetConnectionState(const SCOMDataLinkDesc *const scom)
Definition: scom.c:461
SCOMDataLink SCOM_Create(void)
Definition: scom.c:146
void(* SCOMServiceHandler)(SCOMDataLinkDesc *scom, SCOMFrame *frame)
Definition: scom.h:208
Definition of the frame buffer used by SCOM.
Definition: scom.h:63
Descriptor of the ACK state.
Definition: scom.h:116
Definition of a SCOM frame queue.
SCOMStatistics SCOM_GetStatistics(const SCOMDataLinkDesc *const scom)
Definition: scom.c:1109
uint8_t verLo
version low number
Definition: scom.h:83
#define SCOM_MAX_FRAME_FILTERS
Definition: doxygen.h:652
SCOMResult SCOM_SendAllocatedFrameWithoutACK(SCOMDataLink scom, SCOMFrame *frame)
Definition: scom.c:766
SCOMResult SCOM_AllocFrame(SCOMDataLink scom, SCOMFrame **framePtr)
Definition: scom.c:706
SCOMFrameBuffer * SCOM_CreateFrameBuffer(size_t rxBuffSize, size_t txBuffSize)
Definition: scom.c:174