Skip to content

Commit

Permalink
sample: fix sidewalk_thread msgq
Browse files Browse the repository at this point in the history
KRKNWK-19262
by statically initializing the queue we prevent issues with events.

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Jul 22, 2024
1 parent 04288c8 commit 4e1a7b3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
11 changes: 4 additions & 7 deletions samples/sid_end_device/src/sidewalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ LOG_MODULE_REGISTER(sidewalk_app, CONFIG_SIDEWALK_LOG_LEVEL);
static struct k_thread sid_thread;
K_THREAD_STACK_DEFINE(sid_thread_stack, CONFIG_SIDEWALK_THREAD_STACK_SIZE);

static uint8_t __aligned(4)
sid_msgq_buff[CONFIG_SIDEWALK_THREAD_QUEUE_SIZE * sizeof(sidewalk_ctx_event_t)];
K_MSGQ_DEFINE(sidewalk_thread_msgq, sizeof(sidewalk_ctx_event_t), CONFIG_SIDEWALK_THREAD_QUEUE_SIZE,
4);

static struct k_msgq msgq;
K_SEM_DEFINE(sid_thread_started, 0, 1);
static void sid_thread_entry(void *context, void *unused, void *unused2)
{
Expand All @@ -26,12 +25,10 @@ static void sid_thread_entry(void *context, void *unused, void *unused2)
sidewalk_ctx_t *sid = (sidewalk_ctx_t *)context;
sidewalk_ctx_event_t event = {};

k_msgq_init(&msgq, (char *)sid_msgq_buff, sizeof(sidewalk_ctx_event_t),
CONFIG_SIDEWALK_THREAD_QUEUE_SIZE);
k_sem_give(&sid_thread_started);

while (1) {
int err = k_msgq_get(&msgq, &event, K_FOREVER);
int err = k_msgq_get(&sidewalk_thread_msgq, &event, K_FOREVER);
if (!err) {
if (event.handler) {
event.handler(sid, event.ctx);
Expand Down Expand Up @@ -72,7 +69,7 @@ int sidewalk_event_send(event_handler_t event, void *ctx, ctx_free free)
}
#endif /* CONFIG_SIDEWALK_THREAD_QUEUE_TIMEOUT */

const int result = k_msgq_put(&msgq, (void *)&ctx_event, timeout);
const int result = k_msgq_put(&sidewalk_thread_msgq, (void *)&ctx_event, timeout);
LOG_DBG("sidewalk_event_send event = %p, context = %p, k_msgq_put result %d", (void *)event,
ctx, result);

Expand Down
36 changes: 32 additions & 4 deletions samples/sid_end_device/src/sidewalk_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ sidewalk_msg_t *get_message_buffer(uint16_t message_id)
// private
void sidewalk_event_process(sidewalk_ctx_t *sid, void *ctx)
{
if (sid == NULL || sid->handle == NULL) {
LOG_INF("sidewalk need to be started first.");
return;
}
sid_error_t e = sid_process(sid->handle);
if (e) {
LOG_ERR("sid process err %d", (int)e);
Expand All @@ -81,18 +85,21 @@ void sidewalk_event_platform_init(sidewalk_ctx_t *sid, void *ctx)
LOG_ERR("Sidewalk Platform Init err: %d", e);
return;
}
}

void sidewalk_event_autostart(sidewalk_ctx_t *sid, void *ctx)
{
if (sid->handle != NULL) {
LOG_INF("Sidewlak is already running");
return;
}
if (app_mfg_cfg_is_valid()) {
LOG_ERR("The mfg.hex version mismatch");
LOG_ERR("Check if the file has been generated and flashed properly");
LOG_ERR("START ADDRESS: 0x%08x", APP_MFG_CFG_FLASH_START);
LOG_ERR("SIZE: 0x%08x", APP_MFG_CFG_FLASH_SIZE);
return;
}
}

void sidewalk_event_autostart(sidewalk_ctx_t *sid, void *ctx)
{
#ifdef CONFIG_SID_END_DEVICE_PERSISTENT_LINK_MASK
int err = settings_utils_link_mask_get(&sid->config.link_mask);
if (err <= 0) {
Expand Down Expand Up @@ -156,6 +163,10 @@ void sidewalk_event_autostart(sidewalk_ctx_t *sid, void *ctx)

void sidewalk_event_factory_reset(sidewalk_ctx_t *sid, void *ctx)
{
if (sid == NULL || sid->handle == NULL) {
LOG_INF("sidewalk need to be started first.");
return;
}
#ifdef CONFIG_SID_END_DEVICE_PERSISTENT_LINK_MASK
(void)settings_utils_link_mask_set(0);
#endif /* CONFIG_SIDEWALK_PERSISTENT_LINK_MASK */
Expand All @@ -177,6 +188,10 @@ void sidewalk_event_new_status(sidewalk_ctx_t *sid, void *ctx)
}
void sidewalk_event_send_msg(sidewalk_ctx_t *sid, void *ctx)
{
if (sid == NULL || sid->handle == NULL) {
LOG_INF("sidewalk need to be started first.");
return;
}
sidewalk_msg_t *p_msg = (sidewalk_msg_t *)ctx;
if (!p_msg) {
LOG_ERR("sid send msg is NULL");
Expand Down Expand Up @@ -218,6 +233,10 @@ void sidewalk_event_send_msg(sidewalk_ctx_t *sid, void *ctx)
}
void sidewalk_event_connect(sidewalk_ctx_t *sid, void *ctx)
{
if (sid == NULL || sid->handle == NULL) {
LOG_INF("sidewalk need to be started first.");
return;
}
if (!(sid->config.link_mask & SID_LINK_TYPE_1)) {
LOG_ERR("Can not request connection - BLE not enabled");
return;
Expand All @@ -229,6 +248,10 @@ void sidewalk_event_connect(sidewalk_ctx_t *sid, void *ctx)
}
void sidewalk_event_link_switch(sidewalk_ctx_t *sid, void *ctx)
{
if (sid == NULL || sid->handle == NULL) {
LOG_INF("Can not change link if sidewalk was not started yet");
return;
}
static uint32_t new_link_mask = DEFAULT_LM;

switch (sid->config.link_mask) {
Expand Down Expand Up @@ -305,6 +328,10 @@ void sidewalk_event_link_switch(sidewalk_ctx_t *sid, void *ctx)

void sidewalk_event_exit(sidewalk_ctx_t *sid, void *ctx)
{
if (sid == NULL || sid->handle == NULL) {
LOG_INF("Sidewalk is already deinitialized");
return;
}
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
app_file_transfer_demo_deinit(sid->handle);
#endif
Expand All @@ -326,6 +353,7 @@ void sidewalk_event_exit(sidewalk_ctx_t *sid, void *ctx)
list_element = sys_slist_get(&pending_message_list);
}
k_mutex_unlock(&pending_message_list_mutex);
sid->handle = NULL;
}
void sidewalk_event_reboot(sidewalk_ctx_t *sid, void *ctx)
{
Expand Down

0 comments on commit 4e1a7b3

Please sign in to comment.