From 32da27683151639652496c185dacaec3c6b55274 Mon Sep 17 00:00:00 2001 From: corruptbear Date: Fri, 21 Jun 2024 12:25:14 -0700 Subject: [PATCH] add service to remotely change device mode --- software/firmware/src/app/app_config.h | 1 + .../firmware/src/tasks/app_task_ranging.c | 10 ++++++ software/firmware/src/tasks/app_tasks.h | 1 + .../bluetooth/live_stats_functionality.c | 4 +++ .../src/tasks/bluetooth/live_stats_service.c | 36 +++++++++++++++++++ .../src/tasks/bluetooth/live_stats_service.h | 5 +++ software/firmware/src/tasks/ranging_task.c | 1 + 7 files changed, 58 insertions(+) diff --git a/software/firmware/src/app/app_config.h b/software/firmware/src/app/app_config.h index 6f53d549..ddb108cc 100644 --- a/software/firmware/src/app/app_config.h +++ b/software/firmware/src/app/app_config.h @@ -96,6 +96,7 @@ typedef enum { BATTERY_EMPTY = 3500, BATTERY_CRITICAL = 3680, BATTERY_NOMINAL = #define BLE_MAINTENANCE_EXPERIMENT_CHAR 0x2e,0x5d,0x5e,0x39,0x31,0x52,0x45,0x0c,0x90,0xee,0x3f,0xa2,0x61,0x31,0x8c,0xd6 #define BLE_MAINTENANCE_COMMAND_CHAR 0x2e,0x5d,0x5e,0x39,0x31,0x52,0x45,0x0c,0x90,0xee,0x3f,0xa2,0x62,0x31,0x8c,0xd6 #define BLE_MAINTENANCE_DATA_CHAR 0x2e,0x5d,0x5e,0x39,0x31,0x52,0x45,0x0c,0x90,0xee,0x3f,0xa2,0x63,0x31,0x8c,0xd6 +#define BLE_MODE_SWITCH_CHAR 0x2e,0x5d,0x5e,0x39,0x31,0x52,0x45,0x0c,0x90,0xee,0x3f,0xa2,0x64,0x31,0x8c,0xd6 // Ranging Protocol Configuration -------------------------------------------------------------------------------------- diff --git a/software/firmware/src/tasks/app_task_ranging.c b/software/firmware/src/tasks/app_task_ranging.c index cb4109ad..e2dc9c42 100644 --- a/software/firmware/src/tasks/app_task_ranging.c +++ b/software/firmware/src/tasks/app_task_ranging.c @@ -268,6 +268,16 @@ void app_activate_find_my_tottag(uint32_t seconds_to_activate) app_maintenance_activate_find_my_tottag(seconds_to_activate); } +void app_switch_mode(uint8_t command) +{ + if (command==1) + { //enable data downloading from ranging mode + //disable storage writing + storage_disable(true); + storage_enter_maintenance_mode(); + } +} + void AppTaskRanging(void *uid) { // Store the UID and application task handle diff --git a/software/firmware/src/tasks/app_tasks.h b/software/firmware/src/tasks/app_tasks.h index 69dadc73..5175ddfa 100644 --- a/software/firmware/src/tasks/app_tasks.h +++ b/software/firmware/src/tasks/app_tasks.h @@ -34,6 +34,7 @@ typedef struct __attribute__ ((__packed__)) // Application Task Public Functions void app_notify(app_notification_t notification, bool from_isr); void app_activate_find_my_tottag(uint32_t seconds_to_activate); +void app_switch_mode(uint8_t command); uint32_t app_get_experiment_time(int32_t offset); uint32_t app_experiment_time_to_rtc_time(uint32_t experiment_time); diff --git a/software/firmware/src/tasks/bluetooth/live_stats_functionality.c b/software/firmware/src/tasks/bluetooth/live_stats_functionality.c index f2a45da4..10c6bbe4 100644 --- a/software/firmware/src/tasks/bluetooth/live_stats_functionality.c +++ b/software/firmware/src/tasks/bluetooth/live_stats_functionality.c @@ -29,6 +29,10 @@ uint8_t handleLiveStatsWrite(dmConnId_t connId, uint16_t handle, uint8_t operati app_activate_find_my_tottag(*(uint32_t*)pValue); else if (handle == TIMESTAMP_HANDLE) rtc_set_time_from_timestamp(*(uint32_t*)pValue); +#ifdef _REMOTE_MODE_SWITCH_ENABLED + else if (handle == APP_MODE_SWITCH_HANDLE) + app_switch_mode(*(uint8_t*)pValue); +#endif return ATT_SUCCESS; } diff --git a/software/firmware/src/tasks/bluetooth/live_stats_service.c b/software/firmware/src/tasks/bluetooth/live_stats_service.c index 3385581a..686949a2 100644 --- a/software/firmware/src/tasks/bluetooth/live_stats_service.c +++ b/software/firmware/src/tasks/bluetooth/live_stats_service.c @@ -33,6 +33,16 @@ static const uint16_t findMyTottagDurationLen = sizeof(findMyTottagDuration); static const uint8_t findMyTottagDesc[] = "FindMyTottagRequest"; static const uint16_t findMyTottagDescLen = sizeof(findMyTottagDesc); +#ifdef _REMOTE_MODE_SWITCH_ENABLED +static const uint8_t modeSwitchChUuid[] = { BLE_MODE_SWITCH_CHAR }; +static const uint8_t modeSwitchChar[] = { ATT_PROP_WRITE, UINT16_TO_BYTES(APP_MODE_SWITCH_HANDLE), BLE_MODE_SWITCH_CHAR }; +static const uint16_t modeSwitchCharLen = sizeof(modeSwitchChar); +static uint32_t operationMode = 0; +static const uint16_t operationModeLen = sizeof(operationMode); +static const uint8_t modeSwitchDesc[] = "AppModeSwitch"; +static const uint16_t modeSwitchDescLen = sizeof(modeSwitchDesc); +#endif + static const uint8_t rangingChUuid[] = { BLE_LIVE_STATS_RANGING_CHAR }; static const uint8_t rangesChar[] = { ATT_PROP_NOTIFY, UINT16_TO_BYTES(RANGES_HANDLE), BLE_LIVE_STATS_RANGING_CHAR }; static const uint16_t rangesCharLen = sizeof(rangesChar); @@ -137,6 +147,32 @@ static const attsAttr_t liveStatsList[] = 0, ATTS_PERMIT_READ }, +#ifdef _REMOTE_MODE_SWITCH_ENABLED + { + attChUuid, + (uint8_t*)modeSwitchChar, + (uint16_t*)&modeSwitchCharLen, + sizeof(modeSwitchChar), + 0, + ATTS_PERMIT_READ + }, + { + modeSwitchChUuid, + (uint8_t*)&operationMode, + (uint16_t*)&operationModeLen, + sizeof(operationMode), + (ATTS_SET_UUID_128 | ATTS_SET_WRITE_CBACK), + ATTS_PERMIT_WRITE + }, + { + attChUserDescUuid, + (uint8_t*)modeSwitchDesc, + (uint16_t*)&modeSwitchDescLen, + sizeof(modeSwitchDesc), + 0, + ATTS_PERMIT_READ + }, +#endif { attChUuid, (uint8_t*)rangesChar, diff --git a/software/firmware/src/tasks/bluetooth/live_stats_service.h b/software/firmware/src/tasks/bluetooth/live_stats_service.h index 7b2f5d4b..b74c5db0 100644 --- a/software/firmware/src/tasks/bluetooth/live_stats_service.h +++ b/software/firmware/src/tasks/bluetooth/live_stats_service.h @@ -15,6 +15,11 @@ enum FIND_MY_TOTTAG_CHAR_HANDLE, // FindMyTottag characteristic FIND_MY_TOTTAG_HANDLE, // FindMyTottag value FIND_MY_TOTTAG_DESC_HANDLE, // FindMyTottag description +#ifdef _REMOTE_MODE_SWITCH_ENABLED + APP_MODE_SWITCH_CHAR_HANDLE, // Mode switch characteristic + APP_MODE_SWITCH_HANDLE, // Mode switch value + APP_MODE_SWITCH_DESC_HANDLE, // Mode switch description +#endif RANGES_CHAR_HANDLE, // Current ranges characteristic RANGES_HANDLE, // Current ranges RANGES_DESC_HANDLE, // Current ranges description diff --git a/software/firmware/src/tasks/ranging_task.c b/software/firmware/src/tasks/ranging_task.c index 41609ffe..c34fc500 100644 --- a/software/firmware/src/tasks/ranging_task.c +++ b/software/firmware/src/tasks/ranging_task.c @@ -21,6 +21,7 @@ void ranging_begin(schedule_role_t role) xTaskNotify(ranging_task_handle, role, eSetValueWithOverwrite); } + bool ranging_active(void) { // Return whether actively ranging