diff --git a/components/BLE/BLE.c b/components/BLE/BLE.c index 90e7c06..00f40ca 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,17 @@ 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; } @@ -311,5 +325,86 @@ SystemKResult_T BLE_StopAdvertising(void) result = SYSTEMK_RESULT_SUCCESS; } + return result; +} + +static void Unquiet_Timer_Callback(TimerHandle_t xTimer) +{ + KLOG_INFO(TAG, "Unquiet timer 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; no action needed. + return SYSTEMK_RESULT_SUCCESS; + } + + 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!"); + } + else + { + if (xTimerStart(BLEUnquietTimer, 0) != pdPASS) + { + KLOG_ERROR(TAG, "Couldn't start the BLEUnquietTimer!"); + } + } + } + result = SYSTEMK_RESULT_SUCCESS; + } + + return result; +} + +SystemKResult_T BLE_Unquiet(void) +{ + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; + + 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 fb1a4ab..c514219 160000 --- a/components/SystemK +++ b/components/SystemK @@ -1 +1 @@ -Subproject commit fb1a4ab07b990fef8f957ef7c5f806649e126d0f +Subproject commit c514219ccc3978fa27bb7925a85ce826c37e6e63