sbgECom Library
4.0.1987-stable
C library to interface SBG Systems IMU/AHRS/INS
|
This file implements the base interface for all Serial and Ethernet ports. More...
#include <sbgCommon.h>
Go to the source code of this file.
Data Structures | |
struct | _SbgInterface |
Macros | |
#define | SBG_IF_NAME_MAX_SIZE (48) |
#define | SBG_IF_TYPE_UNKNOW (0) |
#define | SBG_IF_TYPE_SERIAL (1) |
#define | SBG_IF_TYPE_ETH_UDP (2) |
#define | SBG_IF_TYPE_ETH_TCP_IP (3) |
#define | SBG_IF_TYPE_FILE (4) |
#define | SBG_IF_TYPE_LAST_RESERVED (999) |
#define | SBG_IF_FLUSH_INPUT ((uint32_t)1 << 0) |
#define | SBG_IF_FLUSH_OUTPUT ((uint32_t)1 << 1) |
#define | SBG_IF_FLUSH_ALL (SBG_IF_FLUSH_INPUT | SBG_IF_FLUSH_OUTPUT) |
Typedefs | |
typedef struct _SbgInterface | SbgInterface |
typedef void * | SbgInterfaceHandle |
typedef SbgErrorCode(* | SbgInterfaceDestroyFunc) (SbgInterface *pInterface) |
typedef SbgErrorCode(* | SbgInterfaceWriteFunc) (SbgInterface *pInterface, const void *pBuffer, size_t bytesToWrite) |
typedef SbgErrorCode(* | SbgInterfaceReadFunc) (SbgInterface *pInterface, void *pBuffer, size_t *pReadBytes, size_t bytesToRead) |
typedef SbgErrorCode(* | SbgInterfaceFlushFunc) (SbgInterface *pInterface, uint32_t flags) |
typedef SbgErrorCode(* | SbgInterfaceSetSpeed) (SbgInterface *pInterface, uint32_t speed) |
typedef uint32_t(* | SbgInterfaceGetSpeed) (const SbgInterface *pInterface) |
typedef uint32_t(* | SbgInterfaceGetDelayFunc) (const SbgInterface *pInterface, size_t numBytes) |
This file implements the base interface for all Serial and Ethernet ports.
An interface is used to provide a common API for both serial and Ethernet ports. An interface can be opened/closed and some data can be written or read from it.
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_IF_NAME_MAX_SIZE (48) |
Maximum size in bytes for the interface name string
#define SBG_IF_TYPE_UNKNOW (0) |
Type values reserved for standard interface types. The interface type is not defined.
#define SBG_IF_TYPE_SERIAL (1) |
The interface is a serial com port.
#define SBG_IF_TYPE_ETH_UDP (2) |
The interface is an UDP one.
#define SBG_IF_TYPE_ETH_TCP_IP (3) |
The interface is an TCP/IP one.
#define SBG_IF_TYPE_FILE (4) |
The interface is a file.
#define SBG_IF_TYPE_LAST_RESERVED (999) |
Last reserved value for standard types.
#define SBG_IF_FLUSH_INPUT ((uint32_t)1 << 0) |
Flush input data flag.
#define SBG_IF_FLUSH_OUTPUT ((uint32_t)1 << 1) |
Flush output data flag.
#define SBG_IF_FLUSH_ALL (SBG_IF_FLUSH_INPUT | SBG_IF_FLUSH_OUTPUT) |
Flag combination to flush both input and output data.
typedef struct _SbgInterface SbgInterface |
Interface structure pre-definition.
typedef void* SbgInterfaceHandle |
Handle that stores the internal interface handle (ie Serial or Ethernet)
typedef SbgErrorCode(* SbgInterfaceDestroyFunc) (SbgInterface *pInterface) |
Method to implement that close and destroy an interface.
[in] | pInterface | Interface instance. |
typedef SbgErrorCode(* SbgInterfaceWriteFunc) (SbgInterface *pInterface, const void *pBuffer, size_t bytesToWrite) |
Method to implement to write a buffer to an interface.
This method should return an error only if all bytes were not written successfully. If you try to write zero byte, the method shouldn't return any error.
[in] | pInterface | Interface instance. |
[in] | pBuffer | Pointer on an allocated buffer that contains the data to write |
[in] | bytesToWrite | Number of bytes we would like to write (can be zero). |
typedef SbgErrorCode(* SbgInterfaceReadFunc) (SbgInterface *pInterface, void *pBuffer, size_t *pReadBytes, size_t bytesToRead) |
Method to implement to read data from an interface.
This method returns an error only if there is a 'low level' error on the interface. If no byte is read at all or less bytes than bytesToRead, this method returns SBG_NO_ERROR. You have to check pReadBytes field to know the number of bytes actually read.
[in] | pInterface | Interface instance. |
[in] | pBuffer | Pointer on an allocated buffer that can hold at least bytesToRead bytes of data. |
[out] | pReadBytes | Returns the number of bytes actually read (can be zero and up to bytesToRead). |
[in] | bytesToRead | Maximum number of bytes to try to read on the interface. |
typedef SbgErrorCode(* SbgInterfaceFlushFunc) (SbgInterface *pInterface, uint32_t flags) |
Make an interface flush pending input and/or output data.
If flags include SBG_IF_FLUSH_INPUT, all pending input data is discarded. If flags include SBG_IF_FLUSH_OUTPUT, the function blocks until all output data has been written out.
WARNING: The method has no action if not applicable for a type of interface
[in] | pInterface | Interface instance. |
[in] | flags | Combination of the SBG_IF_FLUSH_INPUT and SBG_IF_FLUSH_OUTPUT flags. |
typedef SbgErrorCode(* SbgInterfaceSetSpeed) (SbgInterface *pInterface, uint32_t speed) |
Change an interface input and output speed in bps (bit per second)
This method will try to change the speed immediately even if there are pending bytes in the send buffer.
If you would like to make sure that all bytes in the Tx buffer have been sent before changing the speed, please flush the interface before.
WARNING: The method has no action if not applicable for a type of interface
[in] | pInterface | Interface instance. |
[in] | speed | The new interface speed to set in bps. |
typedef uint32_t(* SbgInterfaceGetSpeed) (const SbgInterface *pInterface) |
Returns the current interface baud rate in bps (bit per second)
WARNING: The method will returns zero if not applicable for a type of interface
[in] | pInterface | Interface instance. |
typedef uint32_t(* SbgInterfaceGetDelayFunc) (const SbgInterface *pInterface, size_t numBytes) |
Compute and return the delay needed by the interface to transmit / receive X number of bytes.
WARNING: The method will returns zero if not applicable for a type of interface.
[in] | pInterface | Interface instance. |
[in] | numBytes | The number of bytes to transmit / receive to evaluate the needed delay. |
SBG_COMMON_LIB_API void sbgInterfaceZeroInit | ( | SbgInterface * | pInterface | ) |
Initialize an interface instance to zero.
[in] | pInterface | The interface instance. |
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceDestroy | ( | SbgInterface * | pInterface | ) |
Close and destroy the interface gracefully.
This method will call the specialized interface destructor if any.
[in] | pInterface | The interface instance. |
SBG_INLINE uint32_t sbgInterfaceTypeGet | ( | const SbgInterface * | pInterface | ) |
Returns the interface type.
[in] | pInterface | Interface instance |
References _SbgInterface::type.
SBG_COMMON_LIB_API const char* sbgInterfaceTypeGetAsString | ( | const SbgInterface * | pInterface | ) |
Returns the interface as string.
[in] | pInterface | Interface instance |
SBG_INLINE const char* sbgInterfaceNameGet | ( | const SbgInterface * | pInterface | ) |
Returns the interface name string.
[in] | pInterface | Interface instance |
References _SbgInterface::name.
SBG_COMMON_LIB_API void sbgInterfaceNameSet | ( | SbgInterface * | pInterface, |
const char * | pName | ||
) |
Define the interface name as a NULL terminated C string.
This method make sure that the provided string will always fit within the allocated name buffer.
If the interface name you would like to set is too long, only the end of the string will be kept.
[in] | pInterface | Interface instance |
[in] | pName | The interface name to set as a NULL terminated C string |
SBG_INLINE SbgErrorCode sbgInterfaceWrite | ( | SbgInterface * | pInterface, |
const void * | pBuffer, | ||
size_t | bytesToWrite | ||
) |
Write some data to an interface.
This method should return an error only if all bytes were not written successfully. If you try to write zero byte, the method shouldn't return any error.
[in] | pInterface | Interface instance. |
[in] | pBuffer | Pointer on an allocated buffer that contains the data to write |
[in] | bytesToWrite | Number of bytes we would like to write (can be zero). |
References _SbgInterface::pWriteFunc, and SBG_INVALID_PARAMETER.
SBG_INLINE SbgErrorCode sbgInterfaceRead | ( | SbgInterface * | pInterface, |
void * | pBuffer, | ||
size_t * | pReadBytes, | ||
size_t | bytesToRead | ||
) |
Try to read some data from an interface.
This method returns an error only if there is a 'low level' error on the interface. If no byte is read at all or less bytes than bytesToRead, this method returns SBG_NO_ERROR. You have to check pReadBytes field to know the number of bytes actually read.
[in] | pInterface | Interface instance. |
[in] | pBuffer | Pointer on an allocated buffer that can hold at least bytesToRead bytes of data. |
[out] | pReadBytes | Returns the number of bytes actually read (can be zero and up to bytesToRead). |
[in] | bytesToRead | Maximum number of bytes to try to read on the interface. |
References _SbgInterface::pReadFunc, and SBG_INVALID_PARAMETER.
SBG_INLINE SbgErrorCode sbgInterfaceFlush | ( | SbgInterface * | pInterface, |
uint32_t | flags | ||
) |
Make an interface flush pending input and/or output data.
If flags include SBG_IF_FLUSH_INPUT, all pending input data is discarded. If flags include SBG_IF_FLUSH_OUTPUT, the function blocks until all output data has been written out.
WARNING: The method has no action if not applicable for a type of interface
[in] | pInterface | Interface instance. |
[in] | flags | Combination of the SBG_IF_FLUSH_INPUT and SBG_IF_FLUSH_OUTPUT flags. |
References _SbgInterface::pFlushFunc, SBG_IF_FLUSH_ALL, and SBG_NO_ERROR.
SBG_INLINE SbgErrorCode sbgInterfaceSetSpeed | ( | SbgInterface * | pInterface, |
uint32_t | speed | ||
) |
Change an interface input and output speed in bps (bit per second)
This method will try to change the speed immediately even if there are pending bytes in the send buffer.
If you would like to make sure that all bytes in the Tx buffer have been sent before changing the speed, please flush the interface before.
WARNING: The method has no action if not applicable for a type of interface
[in] | pInterface | Interface instance. |
[in] | speed | The new interface speed to set in bps. |
References _SbgInterface::pSetSpeedFunc, and SBG_NO_ERROR.
SBG_INLINE uint32_t sbgInterfaceGetSpeed | ( | const SbgInterface * | pInterface | ) |
Returns the current interface baud rate in bps (bit per second)
WARNING: The method will returns zero if not applicable for a type of interface
[in] | pInterface | Interface instance. |
References _SbgInterface::pGetSpeedFunc.
SBG_INLINE uint32_t sbgInterfaceGetDelay | ( | const SbgInterface * | pInterface, |
size_t | numBytes | ||
) |
Compute and return the delay needed by the interface to transmit / receive X number of bytes.
WARNING: The method will returns zero if not applicable for a type of interface.
[in] | pInterface | Interface instance. |
[in] | numBytes | The number of bytes to transmit / receive to evaluate the needed delay. |
References _SbgInterface::pDelayFunc.