From bce4260e60ca9259e04cd40e1925f2974ba6234b Mon Sep 17 00:00:00 2001 From: LDAB Date: Tue, 9 Jul 2024 20:05:40 +0200 Subject: [PATCH] check for duplicates --- .../lea_assistant/lea_assistant_app.c | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/src/applications/lea_assistant/lea_assistant_app.c b/app/src/applications/lea_assistant/lea_assistant_app.c index 63dd3140..6f3b4905 100644 --- a/app/src/applications/lea_assistant/lea_assistant_app.c +++ b/app/src/applications/lea_assistant/lea_assistant_app.c @@ -22,6 +22,13 @@ #include "message_handler.h" +#define MAX_DEVICES_NUM 16 + +typedef struct devices_list { + uint8_t num; + bt_addr_le_t addr[MAX_DEVICES_NUM]; +} devices_list_t; + LOG_MODULE_REGISTER(lea_assistant_app, CONFIG_ZSW_LEA_ASSISTANT_APP_LOG_LEVEL); // Functions needed for all applications @@ -37,6 +44,10 @@ static application_t app = { .stop_func = lea_assistant_app_stop }; +static devices_list_t devices_list = { + .num = 0, +}; + static void close_app(void) { zsw_app_manager_app_close_request(&app); @@ -55,6 +66,8 @@ static void on_sink_selected(lea_assistant_device_t *device) { LOG_DBG("Sink %s selected", device->name); + devices_list.num = 0; + message_handler(&(struct webusb_message ) { .sub_type = MESSAGE_SUBTYPE_STOP_SCAN }, 0); @@ -83,6 +96,8 @@ static void lea_assistant_app_stop(void) message_handler(&(struct webusb_message ) { .sub_type = MESSAGE_SUBTYPE_STOP_SCAN }, 0); + + devices_list.num = 0; } static int lea_assistant_app_add(void) @@ -94,6 +109,21 @@ static int lea_assistant_app_add(void) void lea_assistant_app_add_entry(lea_assistant_device_t *device) { + for (size_t i = 0; i < devices_list.num; i++) { + if (bt_addr_le_cmp(&device->addr, &devices_list.addr[i]) == 0) { + LOG_DBG("Device already added (%s)", device->name); + return; + } + } + + if (devices_list.num > MAX_DEVICES_NUM) { + LOG_WRN("MAX devices reached"); + return; + } + + bt_addr_le_copy(&devices_list.addr[devices_list.num], &device->addr); + devices_list.num++; + lea_assistant_ui_add_list_entry(device); }