Pulled in the latest SystemK (with BLE v0.12) (#5)

This change refactors the BLE subsystem to support the changes introduced in the [KTag Beacon Specification v0.12](https://ktag.clubk.club/Technology/BLE/KTag%20Beacon%20Specification%20v0.12.pdf).

SystemK changes: Software/SystemK#5

Co-authored-by: Joe Kearney <joe@clubk.club>
Reviewed-on: #5
This commit is contained in:
Joe Kearney 2025-09-01 18:05:44 +00:00
parent 1f927b805e
commit e632408cb3
4 changed files with 163 additions and 5 deletions

View file

@ -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;
}

@ -1 +1 @@
Subproject commit 7aa827563a00b9bfa74e89be60f8529259ddac99
Subproject commit 430aec54b8d6edde82c3bc9240e34b6a9e31aa09

View file

@ -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)

View file

@ -1 +1 @@
Welcome to KTag! SPIFFS version 00.41
Welcome to KTag! SPIFFS version 00.42