Thread communication

Between thread communication.

Zephyr solution with message queue objects is used for between thread communication. Communication interface is defined in thread_com.h and thread_com.c files.

Two queues are defined in .c file:

  • setting_que - handling setting message communication (received messages via BLE and LoRa from user, defining new settings or sending in command messages)

  • message_que - handling response messages from firmware to user (returning values and requested messages)

K_MSGQ_DEFINE(setting_que, sizeof(message_struct), 2, 4);
K_MSGQ_DEFINE(message_que, sizeof(message_struct), 4, 4);

Each buffer is equipped with put and get function. Each queue is defined to contain max of 2 messages, defined by a message struct

typedef struct {
    uint8_t len;
    uint8_t id;
    uint8_t data[MAX_MES_LENGTH_BYTES];
} message_struct;

providing receiving thread with message id, its actual length and byte array message. Max message length is determined by

#define MAX_MES_LENGTH_BYTES 255

Message id-s are defined in definitions.h file.

Put message

When new message is send by a thread, depending on used queue functions

int thread_put_setting(uint8_t setting_id, void *setting_data, size_t setting_data_size);
int thread_put_message(uint8_t setting_id, void *setting_data, size_t setting_data_size);

are used. Put function takes id, message and its length and copy them into message_struct, provided message is not to long. If message buffer is full of non-opened messages, function purges the oldest message from the buffer.

Get message

int thread_get_setting(uint8_t *setting_id, void *setting_data);
int thread_get_message(uint8_t *setting_id, void *setting_data);

Provided there is unread message, function copies its content and id to provided buffers and return message length. Message is deleted from queue.

Function is non/blocking and returns 0 if there is no message or negative error code on error. New message can be retrieved from the buffer by using get functions

Last updated