diff --git a/components/BLE/BLE.c b/components/BLE/BLE.c index a729f96..44d5bef 100644 --- a/components/BLE/BLE.c +++ b/components/BLE/BLE.c @@ -13,6 +13,11 @@ static const char *TAG = "BLE"; static bool Host_And_Controller_Synced = false; static bool Is_Scanning = false; static bool Is_Advertising = false; +static bool Is_Quiet = false; +static bool Was_Advertising = false; + +static TimerHandle_t BLEUnquietTimer = NULL; +static StaticTimer_t xBLEUnquietTimerBuffer; static uint8_t Advertising_Data[BLE_KTAG_PACKET_TOTAL_SIZE] = {0x1E, 0xFF, 0xFF, 0xFF, 'K', 'T', 'a', 'g'}; @@ -271,8 +276,34 @@ SystemKResult_T BLE_ScanAndAdvertise(void) } if (Is_Advertising == false) { - ble_advertise(); - Is_Advertising = true; + if (Is_Quiet == false) + { + ble_advertise(); + Is_Advertising = true; + } + else + { + // If we are quiet, we don't want to advertise yet, + // but we will remember that we were asked to advertise. + Was_Advertising = true; + } + } + result = SYSTEMK_RESULT_SUCCESS; + } + + return result; +} + +SystemKResult_T BLE_StopScanning(void) +{ + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; + + if (Host_And_Controller_Synced == true) + { + if (Is_Scanning == true) + { + ble_gap_disc_cancel(); + Is_Scanning = false; } result = SYSTEMK_RESULT_SUCCESS; } @@ -294,5 +325,132 @@ SystemKResult_T BLE_StopAdvertising(void) result = SYSTEMK_RESULT_SUCCESS; } + return result; +} + +static void Unquiet_Timer_Callback(TimerHandle_t xTimer) +{ + KLOG_INFO(TAG, "Quiet time expired after %lu ms.", (xTimerGetPeriod(xTimer) * portTICK_PERIOD_MS)); + BLE_Unquiet(); +} + +SystemKResult_T BLE_Quiet(uint32_t duration_ms) +{ + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; + + if (Is_Quiet == true) + { + // Already quiet; reset timer with new duration if duration_ms > 0. + if (duration_ms > 0) + { + // Stop the timer and change the period if it is already running. + if (BLEUnquietTimer != NULL) + { + xTimerStop(BLEUnquietTimer, 0); + xTimerChangePeriod(BLEUnquietTimer, pdMS_TO_TICKS(duration_ms), 0); + xTimerStart(BLEUnquietTimer, 0); + } + else + { + // There should already be a timer, but just in case... + BLEUnquietTimer = xTimerCreateStatic( + "BLEUnquietTimer", + pdMS_TO_TICKS(duration_ms), + pdFALSE, + (void *)0, + Unquiet_Timer_Callback, + &xBLEUnquietTimerBuffer); + + if (BLEUnquietTimer == NULL) + { + KLOG_ERROR(TAG, "Couldn't create the BLEUnquietTimer!"); + result = SYSTEMK_RESULT_FAILED_TO_CREATE_RTOS_TIMER; + } + else + { + if (xTimerStart(BLEUnquietTimer, 0) != pdPASS) + { + KLOG_ERROR(TAG, "Couldn't start the BLEUnquietTimer!"); + result = SYSTEMK_RESULT_FAILED_TO_START_RTOS_TIMER; + } + } + } + } + else + { + // If duration_ms is 0, stop the timer to stay quiet indefinitely. + if (BLEUnquietTimer != NULL) + { + xTimerStop(BLEUnquietTimer, 0); + } + } + result = SYSTEMK_RESULT_SUCCESS; + } + else if (Host_And_Controller_Synced == true) + { + Is_Quiet = true; + + if (Is_Advertising == true) + { + ble_gap_adv_stop(); + Is_Advertising = false; + Was_Advertising = true; + } + + if (duration_ms > 0) + { + // Set a timer to unquiet after the specified duration. + BLEUnquietTimer = xTimerCreateStatic( + "BLEUnquietTimer", + pdMS_TO_TICKS(duration_ms), + pdFALSE, + (void *)0, + Unquiet_Timer_Callback, + &xBLEUnquietTimerBuffer); + + if (BLEUnquietTimer == NULL) + { + KLOG_ERROR(TAG, "Couldn't create the BLEUnquietTimer!"); + result = SYSTEMK_RESULT_FAILED_TO_CREATE_RTOS_TIMER; + } + else + { + if (xTimerStart(BLEUnquietTimer, 0) != pdPASS) + { + KLOG_ERROR(TAG, "Couldn't start the BLEUnquietTimer!"); + result = SYSTEMK_RESULT_FAILED_TO_START_RTOS_TIMER; + } + } + } + result = SYSTEMK_RESULT_SUCCESS; + } + + return result; +} + +SystemKResult_T BLE_Unquiet(void) +{ + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; + + KLOG_INFO(TAG, "BLE_Unquiet()"); + + if (Is_Quiet == false) + { + // Already unquiet; no action needed. + return SYSTEMK_RESULT_SUCCESS; + } + + if (Host_And_Controller_Synced == true) + { + Is_Quiet = false; + if (Was_Advertising == true) + { + ble_advertise(); + Is_Advertising = true; + Was_Advertising = false; + } + result = SYSTEMK_RESULT_SUCCESS; + } + return result; } \ No newline at end of file diff --git a/components/SystemK b/components/SystemK index 7aa8275..430aec5 160000 --- a/components/SystemK +++ b/components/SystemK @@ -1 +1 @@ -Subproject commit 7aa827563a00b9bfa74e89be60f8529259ddac99 +Subproject commit 430aec54b8d6edde82c3bc9240e34b6a9e31aa09 diff --git a/main/Version.h b/main/Version.h index 9e8eb16..af1bea1 100644 --- a/main/Version.h +++ b/main/Version.h @@ -24,7 +24,7 @@ #define VERSION_H #define VERSION_MAJOR 00 -#define VERSION_MINOR 41 +#define VERSION_MINOR 42 #define STRINGIFY(number) #number #define VERSION_STRING(major, minor) STRINGIFY(major) "." STRINGIFY(minor) diff --git a/spiffs_image/boot_message.txt b/spiffs_image/boot_message.txt index ed41c0c..b389ce5 100644 --- a/spiffs_image/boot_message.txt +++ b/spiffs_image/boot_message.txt @@ -1 +1 @@ -Welcome to KTag! SPIFFS version 00.41 \ No newline at end of file +Welcome to KTag! SPIFFS version 00.42 \ No newline at end of file