The CSP library provides us an easy and convenient way to communicate between both our ground station as well as our sub-modules. Using CSP means we have to standardise communication between all sub-modules. It uses a thread safe socket API with many features/benefits that can be found here:

Listening for Commands:

Every sub-module/subsystem that needs to listen for commands or data needs to bind itself to a socket.

Below is an example of using the CSP built-in csp_socket_t struct to bind our port to our socket and listen for any incoming connections:

// Bind port to socket
csp_socket_t *  server_scoket = csp_socket(CSP_SO_NONE);
csp_bind(server_socket, CSP_ANY);
// Set socket to listen for incoming connections
csp_listen(server_socket, 5);

csp_bind will bind the socket to the port (2nd argument). If CSP_ANY is used, it will listen to all ports.

Details can be found in app_task.c.

Sending a Command to a Socket

To send a request or command to a listening socket, we can use the CSP transaction command below. This will create the connection, send outbuf, wait for reply, copy reply to inbuf, and close the connection.

static inline int csp_transaction(uint8_t prio, uint8_t dest, uint8_t port, uint32_t timeout, void * outbuf, int outlen, void * inbuf, int inlen) {
	return csp_transaction_w_opts(prio, dest, port, timeout, outbuf, outlen, inbuf, inlen, 0);
}

Parameters and return are as follows:

@param[in] prio priority, see #csp_prio_t

@param[in] dest destination address

@param[in] port destination port

@param[in] timeout timeout in mS to wait for a reply