puzzlebox
|
I²C over TCP. More...
This library includes protocol (de)serialization functions for sending and receiving I²C messages over TCP. These functions are used by the main controller and the puzzle box client (pbc). This library does not include any puzzle bus specific code.
mpack is used for the actual (de)serialization, and the functions in this library act as helpers for parsing from chunked data streams.
To use these functions, include the following statement in your CMakeLists.txt:
Classes | |
struct | i2ctcp_msg_t |
I²C over TCP message (v1) More... | |
Functions | |
int | i2ctcp_read (i2ctcp_msg_t *target, const char *buf, size_t buf_sz) |
Read chunk of input stream, and store resulting message in target . | |
void | i2ctcp_read_reset (i2ctcp_msg_t *target) |
Reset the remaining message data counter. | |
bool | i2ctcp_write (const i2ctcp_msg_t *target, char **buf, size_t *buf_sz) |
Allocate and write a msgpack-formatted message to buf . | |
int i2ctcp_read | ( | i2ctcp_msg_t * | target, |
const char * | buf, | ||
size_t | buf_sz ) |
This function is called for each chunk of data from an input stream, and will parse the next puzzle bus message into target
. The input stream is assumed to only contain messages encoded by i2ctcp_write()
target | Pointer to struct that will contain the finished message data |
buf | Pointer to input stream data chunk |
buf_sz | Size of buf |
target
is complete.target->data
will automatically be allocated by this function, even if the message is not fully parsed. This variable must be free'd by the caller after each complete message to prevent memory leaks. void i2ctcp_read_reset | ( | i2ctcp_msg_t * | target | ) |
Calling this function has the effect of forcing i2ctcp_read()
to parse the next buffer chunk as the start of a new message. This function may be called before reading a TCP frame's data to mitigate any synchronization issues arising from earlier corrupt or otherwise malformed messages.
bool i2ctcp_write | ( | const i2ctcp_msg_t * | target, |
char ** | buf, | ||
size_t * | buf_sz ) |
This function allocates a buffer large enough to fit the message specified in target
, and encodes the data in target
in a format that can be decoded later using i2ctcp_read()
.
target | Pointer to struct that contains the message data |
buf | Pointer to char* that will contain the formatted message |
buf_sz | Pointer to size_t that will represent the final size of buf |
true
if a the message could be encoded successfully, false
if there was some kind of errorbuf
must be free'd by the caller afterwards