diff --git a/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c b/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c index 1e61a04..6321035 100644 --- a/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c +++ b/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c @@ -22,11 +22,17 @@ TaskHandle_t COMM_BLE_Task_Handle; /* Private Variables */ +static const char *TAG = "BLE"; static const TickType_t BLE_Task_Delay = BLE_TASK_PERIOD_IN_ms / portTICK_PERIOD_MS; static COMM_BLE_StateID_T Current_State = COMM_BLE_DEFAULT; static COMM_BLE_StateID_T Next_State = COMM_BLE_INITIALIZING; static bool State_Changed = false; static TickType_t Time_At_State_Entry_In_Ticks; +static bool Is_Quiet = false; +static bool Was_Advertising = false; + +static TimerHandle_t BLEUnquietTimer = NULL; +static StaticTimer_t xBLEUnquietTimerBuffer; //! Immediate Alert Service alert level value. volatile uint8_t COMM_BLE_IASAlertLevel = 0; @@ -288,6 +294,69 @@ void COMM_BLE_Task(void * pvParameters) #endif // TRACE_BLE } } + + switch (command.ID) + { + case COMM_BLE_STOP_SCANNING: + { + cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + apiResult = Cy_BLE_GAPP_StopAdvertisement(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + COMM_BLE_RequestState(COMM_BLE_ADVERTISING_AS_BROADCASTER); + } + break; + + case COMM_BLE_STOP_ADVERTISING: + { + cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + apiResult = Cy_BLE_GAPP_StopAdvertisement(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + COMM_BLE_RequestState(COMM_BLE_SCANNING_FOR_KTAG_PACKETS); + } + break; + + default: + // All other commands are ignored in this state. + break; + } } break; } @@ -342,8 +411,17 @@ SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR) SystemKResult_T BLE_ScanAndAdvertise(void) { - COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 }; - xQueueSend(COMM_BLE_CommandQueue, &command, 0); + if (Is_Quiet == false) + { + COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 }; + xQueueSend(COMM_BLE_CommandQueue, &command, 0); + } + 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; + } return SYSTEMK_RESULT_SUCCESS; } @@ -364,6 +442,84 @@ SystemKResult_T BLE_StopScanning(void) return SYSTEMK_RESULT_SUCCESS; } +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; + } + else + { + Is_Quiet = true; + + if ((Current_State == COMM_BLE_SCANNING_AND_ADVERTISING) || + (Current_State == COMM_BLE_ADVERTISING_AS_BROADCASTER)) + { + result = BLE_StopAdvertising(); + 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; + } + else + { + Is_Quiet = false; + if (Was_Advertising == true) + { + BLE_ScanAndAdvertise(); + Was_Advertising = false; + } + result = SYSTEMK_RESULT_SUCCESS; + } + + return result; +} + void COMM_BLE_RequestState(COMM_BLE_StateID_T state) { Next_State = state; @@ -396,6 +552,8 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data) COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_UpdateAdvScanData Error: 0x"); COMM_Console_Print_UInt32AsHex(result); COMM_Console_Print_String("\n"); + + vTaskDelay(100); #endif // TRACE_BLE } } @@ -612,8 +770,8 @@ static void BLE_EventHandler(uint32_t event, void * eventParam) COMM_Console_Print_String(" KTag 'Instigate Game' packet found!"); break; - case BLE_PACKET_TYPE_JOIN_NOW: - COMM_Console_Print_String(" KTag 'Join Now' packet found!"); + case BLE_PACKET_TYPE_EVENT: + COMM_Console_Print_String(" KTag 'Event' packet found!"); break; case BLE_PACKET_TYPE_TAG: @@ -627,6 +785,14 @@ static void BLE_EventHandler(uint32_t event, void * eventParam) case BLE_PACKET_TYPE_STATUS: COMM_Console_Print_String(" KTag 'Status' packet found!"); break; + + case BLE_PACKET_TYPE_PARAMETERS: + COMM_Console_Print_String(" KTag 'Parameters' packet found!"); + break; + + case BLE_PACKET_TYPE_HELLO: + COMM_Console_Print_String(" KTag 'Hello' packet found!"); + break; default: COMM_Console_Print_String(" Unknown KTag packet found!"); diff --git a/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.h b/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.h index e4921c5..e358b48 100644 --- a/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.h +++ b/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.h @@ -22,7 +22,7 @@ extern "C" { //#define TRACE_BLE //#define VERBOSE_BLE_TRACE -#define COMM_BLE_TASK_STACK_SIZE_in_bytes 4096 +#define COMM_BLE_TASK_STACK_SIZE_in_bytes 6144 typedef enum { diff --git a/2020TPCApp1.cydsn/SystemK b/2020TPCApp1.cydsn/SystemK index fb1a4ab..bb91512 160000 --- a/2020TPCApp1.cydsn/SystemK +++ b/2020TPCApp1.cydsn/SystemK @@ -1 +1 @@ -Subproject commit fb1a4ab07b990fef8f957ef7c5f806649e126d0f +Subproject commit bb915121e8e9c8b2679397f4554fa9bc2d836b67 diff --git a/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c b/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c index 1e61a04..6321035 100644 --- a/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c +++ b/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c @@ -22,11 +22,17 @@ TaskHandle_t COMM_BLE_Task_Handle; /* Private Variables */ +static const char *TAG = "BLE"; static const TickType_t BLE_Task_Delay = BLE_TASK_PERIOD_IN_ms / portTICK_PERIOD_MS; static COMM_BLE_StateID_T Current_State = COMM_BLE_DEFAULT; static COMM_BLE_StateID_T Next_State = COMM_BLE_INITIALIZING; static bool State_Changed = false; static TickType_t Time_At_State_Entry_In_Ticks; +static bool Is_Quiet = false; +static bool Was_Advertising = false; + +static TimerHandle_t BLEUnquietTimer = NULL; +static StaticTimer_t xBLEUnquietTimerBuffer; //! Immediate Alert Service alert level value. volatile uint8_t COMM_BLE_IASAlertLevel = 0; @@ -288,6 +294,69 @@ void COMM_BLE_Task(void * pvParameters) #endif // TRACE_BLE } } + + switch (command.ID) + { + case COMM_BLE_STOP_SCANNING: + { + cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + apiResult = Cy_BLE_GAPP_StopAdvertisement(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + COMM_BLE_RequestState(COMM_BLE_ADVERTISING_AS_BROADCASTER); + } + break; + + case COMM_BLE_STOP_ADVERTISING: + { + cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + apiResult = Cy_BLE_GAPP_StopAdvertisement(); + + if (apiResult != CY_BLE_SUCCESS) + { +#ifdef TRACE_BLE + COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x"); + COMM_Console_Print_UInt32AsHex(apiResult); + COMM_Console_Print_String("\n"); +#endif // TRACE_BLE + } + + COMM_BLE_RequestState(COMM_BLE_SCANNING_FOR_KTAG_PACKETS); + } + break; + + default: + // All other commands are ignored in this state. + break; + } } break; } @@ -342,8 +411,17 @@ SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR) SystemKResult_T BLE_ScanAndAdvertise(void) { - COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 }; - xQueueSend(COMM_BLE_CommandQueue, &command, 0); + if (Is_Quiet == false) + { + COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 }; + xQueueSend(COMM_BLE_CommandQueue, &command, 0); + } + 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; + } return SYSTEMK_RESULT_SUCCESS; } @@ -364,6 +442,84 @@ SystemKResult_T BLE_StopScanning(void) return SYSTEMK_RESULT_SUCCESS; } +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; + } + else + { + Is_Quiet = true; + + if ((Current_State == COMM_BLE_SCANNING_AND_ADVERTISING) || + (Current_State == COMM_BLE_ADVERTISING_AS_BROADCASTER)) + { + result = BLE_StopAdvertising(); + 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; + } + else + { + Is_Quiet = false; + if (Was_Advertising == true) + { + BLE_ScanAndAdvertise(); + Was_Advertising = false; + } + result = SYSTEMK_RESULT_SUCCESS; + } + + return result; +} + void COMM_BLE_RequestState(COMM_BLE_StateID_T state) { Next_State = state; @@ -396,6 +552,8 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data) COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_UpdateAdvScanData Error: 0x"); COMM_Console_Print_UInt32AsHex(result); COMM_Console_Print_String("\n"); + + vTaskDelay(100); #endif // TRACE_BLE } } @@ -612,8 +770,8 @@ static void BLE_EventHandler(uint32_t event, void * eventParam) COMM_Console_Print_String(" KTag 'Instigate Game' packet found!"); break; - case BLE_PACKET_TYPE_JOIN_NOW: - COMM_Console_Print_String(" KTag 'Join Now' packet found!"); + case BLE_PACKET_TYPE_EVENT: + COMM_Console_Print_String(" KTag 'Event' packet found!"); break; case BLE_PACKET_TYPE_TAG: @@ -627,6 +785,14 @@ static void BLE_EventHandler(uint32_t event, void * eventParam) case BLE_PACKET_TYPE_STATUS: COMM_Console_Print_String(" KTag 'Status' packet found!"); break; + + case BLE_PACKET_TYPE_PARAMETERS: + COMM_Console_Print_String(" KTag 'Parameters' packet found!"); + break; + + case BLE_PACKET_TYPE_HELLO: + COMM_Console_Print_String(" KTag 'Hello' packet found!"); + break; default: COMM_Console_Print_String(" Unknown KTag packet found!"); diff --git a/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.h b/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.h index e4921c5..e358b48 100644 --- a/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.h +++ b/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.h @@ -22,7 +22,7 @@ extern "C" { //#define TRACE_BLE //#define VERBOSE_BLE_TRACE -#define COMM_BLE_TASK_STACK_SIZE_in_bytes 4096 +#define COMM_BLE_TASK_STACK_SIZE_in_bytes 6144 typedef enum { diff --git a/2020TPCAppNoDFU.cydsn/SystemK b/2020TPCAppNoDFU.cydsn/SystemK index fb1a4ab..bb91512 160000 --- a/2020TPCAppNoDFU.cydsn/SystemK +++ b/2020TPCAppNoDFU.cydsn/SystemK @@ -1 +1 @@ -Subproject commit fb1a4ab07b990fef8f957ef7c5f806649e126d0f +Subproject commit bb915121e8e9c8b2679397f4554fa9bc2d836b67