sbgECom Library
4.0.1987-stable
C library to interface SBG Systems IMU/AHRS/INS
|
Implementation of the sbgECom binary communication protocol. More...
#include <sbgCommon.h>
#include <interfaces/sbgInterface.h>
#include <streamBuffer/sbgStreamBuffer.h>
Go to the source code of this file.
Data Structures | |
struct | _SbgEComProtocolPayload |
struct | _SbgEComProtocol |
Macros | |
#define | SBG_ECOM_MAX_BUFFER_SIZE (4096) |
#define | SBG_ECOM_MAX_PAYLOAD_SIZE (4086) |
#define | SBG_ECOM_MAX_EXTENDED_PAYLOAD_SIZE (4081) |
#define | SBG_ECOM_SYNC_1 (0xFF) |
#define | SBG_ECOM_SYNC_2 (0x5A) |
#define | SBG_ECOM_ETX (0x33) |
#define | SBG_ECOM_RX_TIME_OUT (450) |
Typedefs | |
typedef struct _SbgEComProtocol | SbgEComProtocol |
typedef void(* | SbgEComProtocolFrameCb) (SbgEComProtocol *pProtocol, uint8_t msgClass, uint8_t msgId, SbgStreamBuffer *pReceivedFrame, void *pUserArg) |
typedef struct _SbgEComProtocolPayload | SbgEComProtocolPayload |
Implementation of the sbgECom binary communication protocol.
You will find below, the frame definition used by Ekinox devices.
Fields | SYNC 1 | SYNC 2 | CMD | LEN | DATA | CRC | ETX |
Size in bytes | 1 | 1 | 2 | 2 | (0-4086) | 2 | 1 |
Value | 0xFF | 0x5A | ? | ? | ? | ? | 0x33 |
Size in bytes indicates the size of the data field.
The minimum frame size is 9 bytes and the maximum is 512 bytes.
The CRC is calculated on the whole frame without:
SYNC STX CRC and ETX fields.
The MIT license
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define SBG_ECOM_MAX_BUFFER_SIZE (4096) |
Maximum reception buffer size in bytes.
#define SBG_ECOM_MAX_PAYLOAD_SIZE (4086) |
Maximum payload size in bytes.
#define SBG_ECOM_MAX_EXTENDED_PAYLOAD_SIZE (4081) |
Maximum payload size in an extended frame, in bytes.
#define SBG_ECOM_SYNC_1 (0xFF) |
First synchronization char of the frame.
#define SBG_ECOM_SYNC_2 (0x5A) |
Second synchronization char of the frame.
#define SBG_ECOM_ETX (0x33) |
End of frame byte.
#define SBG_ECOM_RX_TIME_OUT (450) |
Default time out for new frame reception.
typedef struct _SbgEComProtocol SbgEComProtocol |
Forward declaration.
typedef void(* SbgEComProtocolFrameCb) (SbgEComProtocol *pProtocol, uint8_t msgClass, uint8_t msgId, SbgStreamBuffer *pReceivedFrame, void *pUserArg) |
Function called each time a valid sbgECom frame is received.
This callback is used to intercept a valid and full sbgECom frame to easily intercept and store raw stream.
[in] | pProtocol | sbgECom protocol handle instance. |
[in] | msgClass | Received frame message class. |
[in] | msgId | Received frame message id. |
[out] | pReceivedFrame | Stream buffer initialized for read operations on the whole frame data. |
[in] | pUserArg | Optional user supplied argument. |
typedef struct _SbgEComProtocolPayload SbgEComProtocolPayload |
Payload.
This class is used to abstract whether some payload is received from a single or multiple frames.
void sbgEComProtocolPayloadConstruct | ( | SbgEComProtocolPayload * | pPayload | ) |
Payload constructor.
[in] | pPayload | Payload. |
void sbgEComProtocolPayloadDestroy | ( | SbgEComProtocolPayload * | pPayload | ) |
Payload destructor.
[in] | pPayload | Payload. |
const void* sbgEComProtocolPayloadGetBuffer | ( | const SbgEComProtocolPayload * | pPayload | ) |
Get the buffer of a payload.
[in] | pPayload | Payload. |
size_t sbgEComProtocolPayloadGetSize | ( | const SbgEComProtocolPayload * | pPayload | ) |
Get the size of a payload buffer.
[in] | pPayload | Payload. |
void* sbgEComProtocolPayloadMoveBuffer | ( | SbgEComProtocolPayload * | pPayload | ) |
Move the buffer of a payload.
If successful, the ownership of the buffer is passed to the caller. Otherwise, the payload is unchanged.
The buffer must be released with free() once unused.
[in] | pPayload | Payload. |
SbgErrorCode sbgEComProtocolInit | ( | SbgEComProtocol * | pProtocol, |
SbgInterface * | pInterface | ||
) |
Initialize the protocol system used to communicate with the product and return the created handle.
[in] | pProtocol | Protocol instance to construct. |
[in] | pInterface | Interface to use for read/write operations. |
SbgErrorCode sbgEComProtocolClose | ( | SbgEComProtocol * | pProtocol | ) |
Close the protocol system.
[in] | pProtocol | A valid protocol instance. |
SbgErrorCode sbgEComProtocolPurgeIncoming | ( | SbgEComProtocol * | pProtocol | ) |
Purge the interface rx buffer as well as the sbgECom rx work buffer.
For example, if the program flow has been interrupted, this method can be helpful to discard all trash received data.
WARNING: This method is blocking for 100ms and actively tries to read incoming data.
[in] | pProtocol | A valid protocol instance. |
SbgErrorCode sbgEComProtocolSend | ( | SbgEComProtocol * | pProtocol, |
uint8_t | msgClass, | ||
uint8_t | msg, | ||
const void * | pData, | ||
size_t | size | ||
) |
Send data.
If the size is SBG_ECOM_MAX_PAYLOAD_SIZE or less, the data is sent in a single frame. Otherwise, is it fragmented into multiple extended frames, each sent in order, which may block.
[in] | pProtocol | A valid protocol instance. |
[in] | msgClass | Message class. |
[in] | msg | Message ID. |
[in] | pData | Data buffer. |
[in] | size | Data buffer size, in bytes. |
SbgErrorCode sbgEComProtocolReceive | ( | SbgEComProtocol * | pProtocol, |
uint8_t * | pMsgClass, | ||
uint8_t * | pMsgId, | ||
void * | pData, | ||
size_t * | pSize, | ||
size_t | maxSize | ||
) |
Receive a frame.
[in] | pProtocol | A valid protocol instance. |
[out] | pMsgClass | Message class, may be NULL. |
[out] | pMsgId | Message ID, may be NULL. |
[out] | pData | Data buffer. |
[out] | pSize | Number of bytes received. |
[in] | maxSize | Data buffer size, in bytes. |
SbgErrorCode sbgEComProtocolReceive2 | ( | SbgEComProtocol * | pProtocol, |
uint8_t * | pMsgClass, | ||
uint8_t * | pMsgId, | ||
SbgEComProtocolPayload * | pPayload | ||
) |
Receive a frame.
This function is equivalent to sbgEComProtocolReceive() with two exceptions :
Any allocated resource associated with the given payload is released when calling this function.
Because the payload buffer may directly refer to the protocol work buffer on return, it is only valid until the next attempt to receive a frame, with any of the receive functions.
[in] | pProtocol | A valid protocol instance. |
[out] | pMsgClass | Message class, may be NULL. |
[out] | pMsgId | Message ID, may be NULL. |
[out] | pPayload | Payload. |
void sbgEComProtocolSetOnFrameReceivedCb | ( | SbgEComProtocol * | pProtocol, |
SbgEComProtocolFrameCb | pOnFrameReceivedCb, | ||
void * | pUserArg | ||
) |
Define the optional function called each time a valid sbgECom frame is received.
This callback is useful to intercept raw sbgECom frames at a low level. You can call this method with NULL parameters to uninstall the callback.
EXPERIMENTAL: This handler is still experimental and API may change in next sbgECom versions.
[in] | pHandle | A valid protocol instance. |
[in] | pOnFrameReceivedCb | Function to call each time a valid sbgECom frame is received. |
[in] | pUserArg | Optional user argument that will be passed to the callback method. |
SbgErrorCode sbgEComStartFrameGeneration | ( | SbgStreamBuffer * | pOutputStream, |
uint8_t | msgClass, | ||
uint8_t | msg, | ||
size_t * | pStreamCursor | ||
) |
Initialize an output stream for an sbgECom frame generation.
This function is helpful to avoid memory copy compared to sbgEComProtocolSend one.
Only standard frames may be sent with this function.
[in] | pOutputStream | Pointer to an allocated and initialized output stream. |
[in] | msgClass | Message class. |
[in] | msg | Message ID. |
[out] | pStreamCursor | The initial output stream cursor that thus points to the beginning of the generated message. This value should be passed to sbgEComFinalizeFrameGeneration for correct operations. |
SbgErrorCode sbgEComFinalizeFrameGeneration | ( | SbgStreamBuffer * | pOutputStream, |
size_t | streamCursor | ||
) |
Finalize an output stream that has been initialized with sbgEComStartFrameGeneration.
At return, the output stream buffer should point at the end of the generated message. You can thus easily create consecutive SBG_ECOM_LOGS with these methods.
[in] | pOutputStream | Pointer to an allocated and initialized output stream. |
[in] | streamCursor | Position in the stream buffer of the generated message first byte. This value is returned by sbgEComStartFrameGeneration and is mandatory for correct operations. |