> For the complete documentation index, see [llms.txt](https://irnas.gitbook.io/opencollar/-MDJCI9OmpOqWrQzjNIR/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://irnas.gitbook.io/opencollar/-MDJCI9OmpOqWrQzjNIR/thread-communication.md).

# Thread communication

Between thread communication.

Zephyr solution with [message queue](https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.3.0/zephyr/reference/kernel/data_passing/message_queues.html) 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&#x20;

`#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.&#x20;

### 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.&#x20;

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
