Receiving BLE events in multiple tasks

⚠️
Hi there.. thanks for coming to the forums. Exciting news! we’re now in the process of moving to our new forum platform that will offer better functionality and is contained within the main Dialog website. All posts and accounts have been migrated. We’re now accepting traffic on the new forum only - please POST any new threads at//www.xmece.com/support. We’ll be fixing bugs / optimising the searching and tagging over the coming days.
6 posts / 0 new
Last post
firebird
Offline
Last seen:1 month 6 hours ago
Joined:2019-07-12 09:48
Receiving BLE events in multiple tasks

Hi teams,

I questioned for doing separated scan in MESH application, as the below thread.

https://support.dialog-semiconductor.com/forums/post/dialog-smartbond-bluetooth-low-energy-%E2%80%93-software/separate-scan-mesh-application

but when I called ble_gap_scan_start() and get BLE events with ble_get_event() in separate task, console says another scan was already running, and not only BLE_EVT_GAP_ADV_REPORT/BLE_EVT_GAP_SCAN_COMPLETED, but other events also be received in the same task.

And eventually, MESH application did not work.

How can I separate BLE event handling among multiple tasks?

Device:
PM_Dialog
Offline
Last seen:22 hours 59 min ago
Staff
Joined:2018-02-08 11:03
Hi firebird.

Hi firebird.

I assume that you are working on ble_mesh project. Otherwise, please indicate where you are working on. Since you have modified it, can you please share what modifications we have done and which your requirement is? Have you added a custom task?

Thanks, PM_Dialog

firebird
Offline
Last seen:1 month 6 hours ago
Joined:2019-07-12 09:48
Hi,

Hi,

Yes, I am using ble_mesh example with MESH SDK 1.6.1.

I added the following task and started it after device has been provisioned.

Currently, this task just start another scan and check RSSI strength. I will add some codes for identifing specific devices later.

/** **************************************************************************************** * * @file scanner_task.c * * @brief Main task + BLE * * Copyright (C) 2019 INFOMARK, Co., Ltd. * This computer program includes Confidential, Proprietary Information * of INFOMARK, Co., Ltd. All Rights Reserved. * **************************************************************************************** */ #include  #include  #include "osal.h" #include "ad_ble.h" #include "ble_common.h" #include "ble_gap.h" #include "ble_bufops.h" #include "ble_uuid.h" #include "sys_watchdog.h" static void start_scan(void) { ble_error_t status; status = ble_gap_scan_start(GAP_SCAN_PASSIVE, GAP_SCAN_GEN_DISC_MODE, BLE_SCAN_INTERVAL_FROM_MS(30), BLE_SCAN_WINDOW_FROM_MS(30), false, false); if (status != BLE_STATUS_OK) { printf("ERROR: application already scanning\r\n"); } printf("SCAN STARTED\r\n"); } static void handle_evt_gap_adv_report(const ble_evt_gap_adv_report_t *evt) { const uint8_t *p; uint8_t ad_len, ad_type; const char *dev_name = NULL; size_t dev_name_len = 0; printf("Device found: %s %s RSSI=%d\r\n", evt->address.addr_type == PUBLIC_ADDRESS ? "public " : "private", ble_address_to_string(&evt->address), (int8_t)evt->rssi); for (p = evt->data; p < evt->data + evt->length; p += ad_len) { ad_len = (*p++) - 1; /* ad_len is length of value only, without type */ ad_type = *p++; /* Device not found so we look for UUID */ if (ad_type == GAP_DATA_TYPE_UUID16_LIST || ad_type == GAP_DATA_TYPE_UUID16_LIST_INC) { size_t idx; for (idx = 0; idx < ad_len; idx += sizeof(uint16_t)) { printf("\tUUID16: %04X\r\n", get_u16(p + idx)); } } /* Look for name and store it to use later, if proper UUID is found */ if (ad_type == GAP_DATA_TYPE_SHORT_LOCAL_NAME || ad_type == GAP_DATA_TYPE_LOCAL_NAME) { dev_name = (const char*)p; dev_name_len = ad_len; printf("\tNAME: %.*s\r\n", dev_name_len, dev_name); } } } static void handle_evt_gap_scan_completed(const ble_evt_gap_scan_completed_t *evt) { printf("SCAN STOPPED\r\n"); start_scan(); } void scanner_main_task(void *pvParameters) { /* Just remove compiler warnings about the unused parameter */ (void)pvParameters; start_scan(); for (;;) { ble_evt_hdr_t *hdr; /* * Wait for a BLE event - this task will block * indefinitely until something is received. */ hdr = ble_get_event(true); if (!hdr) { continue; } switch (hdr->evt_code) { case BLE_EVT_GAP_ADV_REPORT: handle_evt_gap_adv_report((ble_evt_gap_adv_report_t *) hdr); break; case BLE_EVT_GAP_SCAN_COMPLETED: handle_evt_gap_scan_completed((ble_evt_gap_scan_completed_t *) hdr); break; default: printf("Event: %d\r\n", hdr->evt_code); ble_handle_event_default(hdr); break; } /* Free event buffer (it's not needed anymore) */ OS_FREE(hdr); } }

PM_Dialog
Offline
Last seen:22 hours 59 min ago
Staff
Joined:2018-02-08 11:03
Hi firebird,

Hi firebird,

Can you please run your code in debug mode? Does it get stuck anywhere?

Thanks, PM_Dialog

firebird
Offline
Last seen:1 month 6 hours ago
Joined:2019-07-12 09:48
This code runs for a while

This code runs for a while and randomly stuck.

Every time it stuck, the position is different, so can't figure out the cause.

And some events like BLE_EVT_GAP_ADV_COMPLETED, which is not intended to receive (related with MESH function) was received.

I want to know the followings:

1. Is this approach correct? i.e. Can I get BLE events in multiple tasks without affecting each other?

2. If so, can you please review my code and advice what is wrong.

3.如果没有,回到最初的问题,如何I execute separate scan in MESH application?

firebird
Offline
Last seen:1 month 6 hours ago
Joined:2019-07-12 09:48
Any update?

Any update?