MIP_SDK  v3.0.0-736-g212583cf
MicroStrain Communications Library for embedded systems
Macros | Functions
serialization.c File Reference
#include "serialization.h"

Macros

#define INSERT_MACRO(name, type)
 
#define EXTRACT_MACRO(name, type)
 

Functions

void microstrain_serializer_init_insertion (microstrain_serializer *serializer, uint8_t *buffer, size_t buffer_size)
 Initialize a serialization struct for insertion into a buffer. More...
 
void microstrain_serializer_init_extraction (microstrain_serializer *serializer, const uint8_t *buffer, size_t buffer_size)
 Initialize a serialization struct for extraction from a buffer. More...
 
size_t microstrain_serializer_capacity (const microstrain_serializer *serializer)
 Determines the total length the buffer. More...
 
size_t microstrain_serializer_length (const microstrain_serializer *serializer)
 Determines the length of the data in the buffer. More...
 
int microstrain_serializer_remaining (const microstrain_serializer *serializer)
 Determines the difference between the length and buffer size. More...
 
bool microstrain_serializer_is_ok (const microstrain_serializer *serializer)
 Determines if the data read/written is less than the buffer size. More...
 
bool microstrain_serializer_is_complete (const microstrain_serializer *serializer)
 Determines if the number of remaining bytes is 0. More...
 
static void pack (uint8_t *buffer, const void *value, size_t size)
 
static void unpack (const uint8_t *buffer, void *value, size_t size)
 
void microstrain_extract_count (microstrain_serializer *serializer, uint8_t *count_out, uint8_t max_count)
 Similar to microstrain_extract_u8 but allows a maximum value to be specified. More...
 

Macro Definition Documentation

◆ INSERT_MACRO

#define INSERT_MACRO (   name,
  type 
)
Value:
void microstrain_insert_##name(microstrain_serializer* serializer, type value) \
{ \
const size_t offset = serializer->_offset + sizeof(type); \
if( offset <= serializer->_buffer_size ) \
pack(&serializer->_buffer[serializer->_offset], &value, sizeof(type)); \
serializer->_offset = offset; \
}

◆ EXTRACT_MACRO

#define EXTRACT_MACRO (   name,
  type 
)
Value:
void microstrain_extract_##name(microstrain_serializer* serializer, type* value) \
{ \
const size_t offset = serializer->_offset + sizeof(type); \
if( offset <= serializer->_buffer_size ) \
unpack(&serializer->_buffer[serializer->_offset], value, sizeof(type)); \
serializer->_offset = offset; \
}

Function Documentation

◆ microstrain_serializer_init_insertion()

void microstrain_serializer_init_insertion ( microstrain_serializer *  serializer,
uint8_t *  buffer,
size_t  buffer_size 
)
Parameters
serializer
bufferBuffer into which data will be written. Can be NULL if buffer_size==0.
buffer_sizeSize of the buffer. Data will not be written beyond this size.

◆ microstrain_serializer_init_extraction()

void microstrain_serializer_init_extraction ( microstrain_serializer *  serializer,
const uint8_t *  buffer,
size_t  buffer_size 
)
Parameters
serializer
bufferA pointer from which data will be read.
buffer_sizeMaximum number of bytes to be read from the buffer.

◆ microstrain_serializer_capacity()

size_t microstrain_serializer_capacity ( const microstrain_serializer *  serializer)
Parameters
serializer
Returns
The buffer size.

◆ microstrain_serializer_length()

size_t microstrain_serializer_length ( const microstrain_serializer *  serializer)
Parameters
serializerFor insertion, returns how many bytes have been written. For extraction, returns how many bytes have been read.
Note
This may exceed the buffer size. Check microstrain_serializer_is_ok() before using the data.

◆ microstrain_serializer_remaining()

int microstrain_serializer_remaining ( const microstrain_serializer *  serializer)
Parameters
serializerFor insertion, returns how many unwritten bytes remain in the buffer. For extraction, returns how many bytes have not been read.
Note
This can be a negative number if the application attempted to write or read more data than contained in the buffer. This is not a bug and it can be detected with the microstrain_serializer_is_ok() function.

◆ microstrain_serializer_is_ok()

bool microstrain_serializer_is_ok ( const microstrain_serializer *  serializer)

If the application attempts to read or write beyond the end of the buffer (as defined by the buffer_size passed to the init function), the read or write will be a no-op but the offset will still be advanced. This allows the condition to be detected.

Parameters
serializer
Returns
true if microstrain_serializer_remaining() >= 0.

◆ microstrain_serializer_is_complete()

bool microstrain_serializer_is_complete ( const microstrain_serializer *  serializer)

Use this to determine if the entire buffer has been extracted. It is not particularly useful for insertion.

Parameters
serializer
Returns
true if microstrain_serializer_remaining() == 0.

◆ pack()

static void pack ( uint8_t *  buffer,
const void *  value,
size_t  size 
)
static

◆ unpack()

static void unpack ( const uint8_t *  buffer,
void *  value,
size_t  size 
)
static

◆ microstrain_extract_count()

void microstrain_extract_count ( microstrain_serializer *  serializer,
uint8_t *  count_out,
uint8_t  max_count 
)

If the maximum count would be exceeded, an error is generated which causes further extraction to fail.

Parameters
serializer
count_outThe counter value read from the buffer.
max_countThe maximum value of the counter. If the count exceeds this, it is set to 0 and the serializer is insert into an error state.
microstrain::C::microstrain_serializer
struct microstrain::C::microstrain_serializer microstrain_serializer
Structure used for serialization.