puzzlebox
Loading...
Searching...
No Matches
i2ctcp

I²C over TCP. More...

Detailed Description

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:

# include pbdrv
add_subdirectory(lib/i2ctcp)
# link with executable
target_link_libraries(main i2ctcp)

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.
 

Function Documentation

◆ i2ctcp_read()

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()

Parameters
targetPointer to struct that will contain the finished message data
bufPointer to input stream data chunk
buf_szSize of buf
Returns
Integer representing amount of bytes required to finish message, or -1 if the message header could not be read. If this function returns 0, the message in target is complete.
Note
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.

◆ i2ctcp_read_reset()

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.

◆ i2ctcp_write()

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().

Parameters
targetPointer to struct that contains the message data
bufPointer to char* that will contain the formatted message
buf_szPointer to size_t that will represent the final size of buf
Returns
Boolean true if a the message could be encoded successfully, false if there was some kind of error
Note
The pointer stored in buf must be free'd by the caller afterwards