diff --git a/2020TPCApp1.cydsn/2020TPCApp1.cyprj b/2020TPCApp1.cydsn/2020TPCApp1.cyprj index cc78e20..3c25635 100644 --- a/2020TPCApp1.cydsn/2020TPCApp1.cyprj +++ b/2020TPCApp1.cydsn/2020TPCApp1.cyprj @@ -5976,7 +5976,7 @@ - + diff --git a/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c b/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c index 6321035..eef6641 100644 --- a/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c +++ b/2020TPCApp1.cydsn/COMM/BLE/COMM_BLE.c @@ -411,6 +411,10 @@ SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR) SystemKResult_T BLE_ScanAndAdvertise(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_ScanAndAdvertise()"); +#endif // TRACE_BLE + if (Is_Quiet == false) { COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 }; @@ -428,6 +432,10 @@ SystemKResult_T BLE_ScanAndAdvertise(void) SystemKResult_T BLE_StopAdvertising(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_StopAdvertising()"); +#endif // TRACE_BLE + COMM_BLE_Command_T command = { .ID = COMM_BLE_STOP_ADVERTISING, .Data = (void *)0x00 }; xQueueSend(COMM_BLE_CommandQueue, &command, 0); @@ -436,6 +444,10 @@ SystemKResult_T BLE_StopAdvertising(void) SystemKResult_T BLE_StopScanning(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_StopScanning()"); +#endif // TRACE_BLE + COMM_BLE_Command_T command = { .ID = COMM_BLE_STOP_SCANNING, .Data = (void *)0x00 }; xQueueSend(COMM_BLE_CommandQueue, &command, 0); @@ -450,12 +462,59 @@ static void Unquiet_Timer_Callback(TimerHandle_t xTimer) SystemKResult_T BLE_Quiet(uint32_t duration_ms) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_Quiet(%lu)", duration_ms); +#endif // TRACE_BLE + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; if (Is_Quiet == true) { - // Already quiet; no action needed. - return SYSTEMK_RESULT_SUCCESS; + // Already quiet; reset timer with new duration if duration_ms > 0. + if (duration_ms > 0) + { + // Stop and delete existing timer if it exists. + if (BLEUnquietTimer != NULL) + { + xTimerStop(BLEUnquietTimer, 0); + xTimerDelete(BLEUnquietTimer, 0); + BLEUnquietTimer = NULL; + } + + // Create and start new timer with the new 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; + } + } + } + else + { + // If duration_ms is 0, stop and delete the timer to stay quiet indefinitely. + if (BLEUnquietTimer != NULL) + { + xTimerStop(BLEUnquietTimer, 0); + xTimerDelete(BLEUnquietTimer, 0); + BLEUnquietTimer = NULL; + } + } + result = SYSTEMK_RESULT_SUCCESS; } else { @@ -482,12 +541,14 @@ SystemKResult_T BLE_Quiet(uint32_t duration_ms) 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; } } } @@ -499,6 +560,10 @@ SystemKResult_T BLE_Quiet(uint32_t duration_ms) SystemKResult_T BLE_Unquiet(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_Unquiet()"); +#endif // TRACE_BLE + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; if (Is_Quiet == false) @@ -544,6 +609,10 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data) Advertising_Data.advDataLen = BLE_KTAG_PACKET_TOTAL_SIZE; memcpy(Advertising_Data.advData, data, BLE_KTAG_PACKET_TOTAL_SIZE); +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "Setting advertising data for packet type %u", data->data[8]); +#endif // TRACE_BLE + cy_en_ble_api_result_t result = Cy_BLE_GAPP_UpdateAdvScanData(&Advertising_Info); if (result != CY_BLE_SUCCESS) @@ -795,7 +864,7 @@ static void BLE_EventHandler(uint32_t event, void * eventParam) break; default: - COMM_Console_Print_String(" Unknown KTag packet found!"); + KLOG_ERROR(TAG, " Unknown KTag packet found: 0x%02X", packet->Generic.type); break; } diff --git a/2020TPCApp1.cydsn/SystemK b/2020TPCApp1.cydsn/SystemK index bb91512..88c81dc 160000 --- a/2020TPCApp1.cydsn/SystemK +++ b/2020TPCApp1.cydsn/SystemK @@ -1 +1 @@ -Subproject commit bb915121e8e9c8b2679397f4554fa9bc2d836b67 +Subproject commit 88c81dc405ef5d10fcf67d80d349bc1a7924e6f9 diff --git a/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c b/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c index 6321035..eef6641 100644 --- a/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c +++ b/2020TPCAppNoDFU.cydsn/COMM/BLE/COMM_BLE.c @@ -411,6 +411,10 @@ SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR) SystemKResult_T BLE_ScanAndAdvertise(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_ScanAndAdvertise()"); +#endif // TRACE_BLE + if (Is_Quiet == false) { COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 }; @@ -428,6 +432,10 @@ SystemKResult_T BLE_ScanAndAdvertise(void) SystemKResult_T BLE_StopAdvertising(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_StopAdvertising()"); +#endif // TRACE_BLE + COMM_BLE_Command_T command = { .ID = COMM_BLE_STOP_ADVERTISING, .Data = (void *)0x00 }; xQueueSend(COMM_BLE_CommandQueue, &command, 0); @@ -436,6 +444,10 @@ SystemKResult_T BLE_StopAdvertising(void) SystemKResult_T BLE_StopScanning(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_StopScanning()"); +#endif // TRACE_BLE + COMM_BLE_Command_T command = { .ID = COMM_BLE_STOP_SCANNING, .Data = (void *)0x00 }; xQueueSend(COMM_BLE_CommandQueue, &command, 0); @@ -450,12 +462,59 @@ static void Unquiet_Timer_Callback(TimerHandle_t xTimer) SystemKResult_T BLE_Quiet(uint32_t duration_ms) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_Quiet(%lu)", duration_ms); +#endif // TRACE_BLE + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; if (Is_Quiet == true) { - // Already quiet; no action needed. - return SYSTEMK_RESULT_SUCCESS; + // Already quiet; reset timer with new duration if duration_ms > 0. + if (duration_ms > 0) + { + // Stop and delete existing timer if it exists. + if (BLEUnquietTimer != NULL) + { + xTimerStop(BLEUnquietTimer, 0); + xTimerDelete(BLEUnquietTimer, 0); + BLEUnquietTimer = NULL; + } + + // Create and start new timer with the new 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; + } + } + } + else + { + // If duration_ms is 0, stop and delete the timer to stay quiet indefinitely. + if (BLEUnquietTimer != NULL) + { + xTimerStop(BLEUnquietTimer, 0); + xTimerDelete(BLEUnquietTimer, 0); + BLEUnquietTimer = NULL; + } + } + result = SYSTEMK_RESULT_SUCCESS; } else { @@ -482,12 +541,14 @@ SystemKResult_T BLE_Quiet(uint32_t duration_ms) 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; } } } @@ -499,6 +560,10 @@ SystemKResult_T BLE_Quiet(uint32_t duration_ms) SystemKResult_T BLE_Unquiet(void) { +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "BLE_Unquiet()"); +#endif // TRACE_BLE + SystemKResult_T result = SYSTEMK_RESULT_NOT_READY; if (Is_Quiet == false) @@ -544,6 +609,10 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data) Advertising_Data.advDataLen = BLE_KTAG_PACKET_TOTAL_SIZE; memcpy(Advertising_Data.advData, data, BLE_KTAG_PACKET_TOTAL_SIZE); +#ifdef TRACE_BLE + KLOG_TRACE(TAG, "Setting advertising data for packet type %u", data->data[8]); +#endif // TRACE_BLE + cy_en_ble_api_result_t result = Cy_BLE_GAPP_UpdateAdvScanData(&Advertising_Info); if (result != CY_BLE_SUCCESS) @@ -795,7 +864,7 @@ static void BLE_EventHandler(uint32_t event, void * eventParam) break; default: - COMM_Console_Print_String(" Unknown KTag packet found!"); + KLOG_ERROR(TAG, " Unknown KTag packet found: 0x%02X", packet->Generic.type); break; } diff --git a/2020TPCAppNoDFU.cydsn/SystemK b/2020TPCAppNoDFU.cydsn/SystemK index bb91512..88c81dc 160000 --- a/2020TPCAppNoDFU.cydsn/SystemK +++ b/2020TPCAppNoDFU.cydsn/SystemK @@ -1 +1 @@ -Subproject commit bb915121e8e9c8b2679397f4554fa9bc2d836b67 +Subproject commit 88c81dc405ef5d10fcf67d80d349bc1a7924e6f9