Initial public release.

This commit is contained in:
Joe Kearney 2025-02-01 19:22:12 -06:00
parent 7b169e8116
commit dac4af8d25
255 changed files with 68595 additions and 2 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,79 @@
/** \dir "BLE"
*
* \brief This directory contains source code for managing Bluetooth Low Energy communications.
*
*/
/** \file
* \brief This file defines the interface to the Bluetooth Low Energy communications used by this software.
*
*/
#ifndef COMM_BLE_H
#define COMM_BLE_H
#ifdef __cplusplus
extern "C" {
#endif
/* Preprocessor and Type Definitions */
// Define this to print out BLE trace statements to the console.
//#define TRACE_BLE
//#define VERBOSE_BLE_TRACE
#define COMM_BLE_TASK_STACK_SIZE_in_bytes 4096
typedef enum
{
COMM_BLE_DEFAULT,
COMM_BLE_INITIALIZING,
COMM_BLE_IDLE,
COMM_BLE_SCANNING_FOR_KTAG_PACKETS,
COMM_BLE_ADVERTISING_AS_PERIPHERAL,
COMM_BLE_ADVERTISING_AS_BROADCASTER,
COMM_BLE_SCANNING_AND_ADVERTISING
} COMM_BLE_StateID_T;
typedef enum
{
COMM_BLE_COMMAND_NO_OP,
COMM_BLE_REQUEST_STATE_CHANGE,
COMM_BLE_PROCESS_BLE_EVENTS,
COMM_BLE_SCAN_FOR_KTAG_PACKETS,
COMM_BLE_ADVERTISE_AS_BROADCASTER,
COMM_BLE_ADVERTISE_AS_PERIPHERAL,
COMM_BLE_STOP_ADVERTISING,
COMM_BLE_SCAN_AND_ADVERTISE,
// COMM_BLE_COMMAND_IS_OUT_OF_RANGE is one more than the last valid command.
COMM_BLE_COMMAND_IS_OUT_OF_RANGE
} COMM_BLE_Command_ID_T;
typedef struct
{
COMM_BLE_Command_ID_T ID;
void * Data;
} COMM_BLE_Command_T;
/* Include Files */
/* Public Variables */
extern cy_stc_ble_conn_handle_t appConnHandle[CY_BLE_CONN_COUNT];
extern volatile uint8_t COMM_BLE_IASAlertLevel;
extern QueueHandle_t COMM_BLE_CommandQueue;
//! Handle of the COMM_BLE_Task() given when the task was created.
extern TaskHandle_t COMM_BLE_Task_Handle;
/* Public Functions */
void COMM_BLE_Init(void);
void COMM_BLE_Task(void * pvParameters);
void COMM_BLE_RequestState(COMM_BLE_StateID_T state);
#ifdef __cplusplus
}
#endif
#endif // COMM_BLE_H

View file

@ -0,0 +1,252 @@
/* Include Files */
#include "KTag.h"
/* Local Definitions and Constants */
/* Public Variables */
/* Private Variables */
static bool removeBondListFlag = false;
/* Private Function Prototypes */
/* Public Functions */
/*******************************************************************************
* Function Name: App_DisplayBondList()
********************************************************************************
*
* Summary:
* This function displays the bond list.
*
*******************************************************************************/
void App_DisplayBondList(void)
{
#ifdef TRACE_BLE
cy_en_ble_api_result_t apiResult;
cy_stc_ble_gap_peer_addr_info_t bondedDeviceInfo[CY_BLE_MAX_BONDED_DEVICES];
cy_stc_ble_gap_bonded_device_list_info_t bondedDeviceList =
{
.bdHandleAddrList = bondedDeviceInfo
};
uint8_t deviceCount;
/* Find out whether the device has bonded information stored already or not */
apiResult = Cy_BLE_GAP_GetBondList(&bondedDeviceList);
if (apiResult != CY_BLE_SUCCESS)
{
COMM_Console_Print_String("[BLE] Cy_BLE_GAP_GetBondList API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
}
else
{
deviceCount = bondedDeviceList.noOfDevices;
if(deviceCount != 0u)
{
uint8_t counter;
COMM_Console_Print_String("[BLE] Bond list:\n");
do
{
COMM_Console_Print_String(" ");
COMM_Console_Print_UInt8(deviceCount);
COMM_Console_Print_String(". ");
deviceCount--;
if(bondedDeviceList.bdHandleAddrList[deviceCount].bdAddr.type == CY_BLE_GAP_ADDR_TYPE_RANDOM)
{
COMM_Console_Print_String("Peer Random Address:");
}
else
{
COMM_Console_Print_String("Peer Public Address:");
}
for (counter = CY_BLE_GAP_BD_ADDR_SIZE; counter > 0u; counter--)
{
COMM_Console_Print_String(" ");
COMM_Console_Print_UInt8AsHex(bondedDeviceList.bdHandleAddrList[deviceCount].bdAddr.bdAddr[counter - 1u]);
}
COMM_Console_Print_String(", bdHandle: 0x");
COMM_Console_Print_UInt8AsHex(bondedDeviceList.bdHandleAddrList[deviceCount].bdHandle);
COMM_Console_Print_String("\n");
} while (deviceCount != 0u);
COMM_Console_Print_String("\n");
}
}
#endif // TRACE_BLE
}
/*******************************************************************************
* Function Name: App_RemoveDevidesFromBondList
********************************************************************************
*
* Summary:
* Remove devices from the bond list.
*
*******************************************************************************/
void App_RemoveDevicesFromBondList(void)
{
#if(CY_BLE_BONDING_REQUIREMENT == CY_BLE_BONDING_YES)
cy_en_ble_api_result_t apiResult;
cy_stc_ble_gap_bd_addr_t peerBdAddr = { .type = 0u };
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cleaning Bond List...\n\n");
#endif // TRACE_BLE
/* Remove all bonded devices in the list */
apiResult = Cy_BLE_GAP_RemoveBondedDevice(&peerBdAddr);
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAP_RemoveBondedDevice API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
else
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAP_RemoveBondedDevice complete.\n\n");
#endif // TRACE_BLE
}
#else
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Bonding is disabled...no need to remove bonded devices.\n\n");
#endif // TRACE_BLE
#endif /* (CY_BLE_BONDING_REQUIREMENT == CY_BLE_BONDING_YES) */
/* Clean flag */
removeBondListFlag = false;
}
/*******************************************************************************
* Function Name: App_GetCountOfBondedDevices()
********************************************************************************
*
* Summary:
* This function returns the count of bonded devices
*
* Return:
* uint32_t The count of bonded devices
*
*******************************************************************************/
uint32_t App_GetCountOfBondedDevices(void)
{
cy_en_ble_api_result_t apiResult;
cy_stc_ble_gap_peer_addr_info_t bondedDeviceInfo[CY_BLE_MAX_BONDED_DEVICES];
cy_stc_ble_gap_bonded_device_list_info_t bondedDeviceList =
{
.bdHandleAddrList = bondedDeviceInfo
};
uint32_t deviceCount = 0u;
/* Find out whether the device has bonded information stored already or not */
apiResult = Cy_BLE_GAP_GetBondList(&bondedDeviceList);
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAP_GetBondList API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
else
{
deviceCount = bondedDeviceList.noOfDevices;
}
return (deviceCount);
}
/*******************************************************************************
* Function Name: App_IsDeviceInBondList()
********************************************************************************
*
* Summary:
* This function check if device with bdHandle is in the bond list
*
* Parameters:
* bdHandle - bond device handler
*
* Return:
* bool - true value when bdHandle exists in bond list
*
*******************************************************************************/
bool App_IsDeviceInBondList(uint32_t bdHandle)
{
cy_en_ble_api_result_t apiResult;
cy_stc_ble_gap_peer_addr_info_t bondedDeviceInfo[CY_BLE_MAX_BONDED_DEVICES];
cy_stc_ble_gap_bonded_device_list_info_t bondedDeviceList =
{
.bdHandleAddrList = bondedDeviceInfo
};
bool deviceIsDetected = false;
uint32_t deviceCount;
/* Find out whether the device has bonding information stored already or not */
apiResult = Cy_BLE_GAP_GetBondList(&bondedDeviceList);
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAP_GetBondList API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
else
{
deviceCount = bondedDeviceList.noOfDevices;
if(deviceCount != 0u)
{
do
{
deviceCount--;
if(bdHandle == bondedDeviceList.bdHandleAddrList[deviceCount].bdHandle)
{
deviceIsDetected = 1u;
}
} while(deviceCount != 0u);
}
}
return(deviceIsDetected);
}
/*******************************************************************************
* Function Name: App_SetRemoveBondListFlag()
********************************************************************************
* Summary:
* Set flag for removing bond list
*
*******************************************************************************/
void App_SetRemoveBondListFlag(void)
{
removeBondListFlag = true;
}
/*******************************************************************************
* Function Name: App_IsRemoveBondListFlag()
********************************************************************************
* Summary:
* Get value of remove bond list flag
*
* Return:
* true - remove bond list flag is set
* false - remove bond list flag is clear
*
*******************************************************************************/
bool App_IsRemoveBondListFlag(void)
{
return ((removeBondListFlag == true) ? true : false);
}
/* Private Functions */

View file

@ -0,0 +1,32 @@
/** \file
* \brief This file declares Bluetooth Low Energy bond list helper functions.
*
*/
#ifndef COMM_BLE_BOND_H
#define COMM_BLE_BOND_H
#ifdef __cplusplus
extern "C" {
#endif
/* Preprocessor and Type Definitions */
/* Include Files */
/* Public Variables */
/* Public Functions */
void App_DisplayBondList(void);
void App_RemoveDevicesFromBondListBySW2Press(uint32_t seconds);
void App_RemoveDevicesFromBondList(void);
void App_SetRemoveBondListFlag(void);
bool App_IsRemoveBondListFlag(void);
bool App_IsDeviceInBondList(uint32_t bdHandle);
uint32_t App_GetCountOfBondedDevices(void);
#ifdef __cplusplus
}
#endif
#endif // COMM_BLE_BOND_H

View file

@ -0,0 +1,177 @@
/* Include Files */
#include "KTag.h"
/* Local Definitions and Constants */
#define UART_CIRCULAR_BUFFER_SIZE 1024
#define BLE_UART_BUFFER_CHARACTERISTIC_SIZE 20
/* Public Variables */
/* Private Variables */
static uint_fast16_t UART_Tx_Notifications_Enabled = 0;
static uint8_t UART_Tx_Buffer_Storage[UART_CIRCULAR_BUFFER_SIZE];
static UTIL_CircularBuffer_T UART_Tx_Buffer;
static uint8_t BLE_UART_Tx_Buffer[BLE_UART_BUFFER_CHARACTERISTIC_SIZE];
static uint8_t Rx_Buffer[BLE_UART_BUFFER_CHARACTERISTIC_SIZE + 1];
/* Private Function Prototypes */
/* Public Functions */
void COMM_BLE_UART_Init(void)
{
UTIL_InitCircularBuffer(&UART_Tx_Buffer, UART_Tx_Buffer_Storage, UART_CIRCULAR_BUFFER_SIZE);
}
//! Sends a message over the BLE UART.
void COMM_BLE_UART_PutString(const char8 * string, uint16_t length)
{
for (uint8_t i = 0; i < length; i++)
{
(void) UTIL_PushToCircularBuffer(&UART_Tx_Buffer, *string++);
}
}
//! Sends a single character over the BLE UART.
void COMM_BLE_UART_PutChar(char8 character)
{
(void) UTIL_PushToCircularBuffer(&UART_Tx_Buffer, character);
}
void COMM_BLE_UART_MaybeSendData(void)
{
int8_t length = 0;
if (UTIL_IsCircularBufferEmpty(&UART_Tx_Buffer) == false)
{
while ((length < BLE_UART_BUFFER_CHARACTERISTIC_SIZE) && (UTIL_IsCircularBufferEmpty(&UART_Tx_Buffer) == false))
{
uint8_t value;
if (UTIL_PopFromCircularBuffer(&UART_Tx_Buffer, &value) == UTIL_CIRCULARBUFFERRESULT_SUCCESS)
{
BLE_UART_Tx_Buffer[length] = value;
length++;
}
}
}
if (length > 0)
{
for (uint_fast8_t i = 0; i < CY_BLE_CONN_COUNT; i++)
{
if (Cy_BLE_GetConnectionState(appConnHandle[i]) >= CY_BLE_CONN_STATE_CONNECTED)
{
cy_stc_ble_gatt_handle_value_pair_t tempHandle;
tempHandle.attrHandle = CY_BLE_NORDIC_UART_SERVICE_TX_CHAR_HANDLE;
tempHandle.value.val = (uint8 *) BLE_UART_Tx_Buffer;
tempHandle.value.actualLen = length;
tempHandle.value.len = length;
Cy_BLE_GATTS_WriteAttributeValueLocal(&tempHandle);
}
}
// Send notification to each client that has TX notifications enabled.
for (uint_fast8_t i = 0; i < CY_BLE_CONN_COUNT; i++)
{
if ((Cy_BLE_GetConnectionState(appConnHandle[i]) >= CY_BLE_CONN_STATE_CONNECTED) &&
Cy_BLE_GATTS_IsNotificationEnabled(&appConnHandle[i],
CY_BLE_NORDIC_UART_SERVICE_TX_TXCCCD_DESC_HANDLE))
{
cy_stc_ble_gatt_handle_value_pair_t tempHandle;
tempHandle.attrHandle = CY_BLE_NORDIC_UART_SERVICE_TX_CHAR_HANDLE;
tempHandle.value.val = (uint8 *) BLE_UART_Tx_Buffer;
tempHandle.value.actualLen = length;
tempHandle.value.len = length;
Cy_BLE_GATTS_SendNotification(&appConnHandle[i], &tempHandle);
}
}
}
}
//! BLE event handler for the BLE UART feature.
/*!
* This function should be called *before* events are handled in the event handler passed to
* Cy_BLE_Start(). If it returns `false`, then the rest of the event handler should proceed.
*
* \param event BLE stack event code received from the BLE middleware (one of cy_en_ble_event_t).
* \param eventParam pointer to an event-specific data structure containing the relevant event information.
* \return true if this handler has completely handled the event, and no further
* handling is necessary; false otherwise.
*/
bool COMM_BLE_UART_HandleEvent(uint32 event, void * eventParam)
{
static cy_stc_ble_gatts_write_cmd_req_param_t * writeReqParameter;
bool handled = false;
/* Take an action based on the current event */
switch ((cy_en_ble_event_t)event)
{
// Handle a write request.
case CY_BLE_EVT_GATTS_WRITE_REQ:
writeReqParameter = (cy_stc_ble_gatts_write_cmd_req_param_t*)eventParam;
// Request to write the UART.
// https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.2.0%2Fble_sdk_app_nus_eval.html&cp=5_5_0_4_1_2_24
if (writeReqParameter->handleValPair.attrHandle == CY_BLE_NORDIC_UART_SERVICE_RX_CHAR_HANDLE)
{
// Only update the value and write the response if the requested write is allowed.
if (CY_BLE_GATT_ERR_NONE == Cy_BLE_GATTS_WriteAttributeValuePeer(&writeReqParameter->connHandle, &writeReqParameter->handleValPair))
{
uint16_t i;
for (i = 0; (i < BLE_UART_BUFFER_CHARACTERISTIC_SIZE) && (i < writeReqParameter->handleValPair.value.len); i++)
{
Rx_Buffer[i] = writeReqParameter->handleValPair.value.val[i];
}
// NULL-terminate the buffer.
Rx_Buffer[i] = 0x00;
Cy_BLE_GATTS_WriteRsp(writeReqParameter->connHandle);
COMM_Console_Execute_Internal_Command(Rx_Buffer);
}
handled = true;
}
// Request for UART Tx notifications.
if (writeReqParameter->handleValPair.attrHandle == CY_BLE_NORDIC_UART_SERVICE_TX_TXCCCD_DESC_HANDLE)
{
if (CY_BLE_GATT_ERR_NONE == Cy_BLE_GATTS_WriteAttributeValuePeer(&writeReqParameter->connHandle, &writeReqParameter->handleValPair))
{
UART_Tx_Notifications_Enabled = writeReqParameter->handleValPair.value.val[0] & 0x01;
if (UART_Tx_Notifications_Enabled)
{
COMM_Console_Print_String("[BLE] UART Tx notifications enabled.\n");
}
else
{
COMM_Console_Print_String("[BLE] UART Tx notifications disabled.\n");
}
Cy_BLE_GATTS_WriteRsp(writeReqParameter->connHandle);
}
handled = true;
}
break;
default:
// (`handled` is already set to false.)
break;
}
return handled;
}
/* Private Functions */

View file

@ -0,0 +1,30 @@
/** \file
* \brief This file declares interface functions to a BLE UART implementation.
*
*/
#ifndef COMM_BLE_UART_H
#define COMM_BLE_UART_H
#ifdef __cplusplus
extern "C" {
#endif
/* Preprocessor and Type Definitions */
/* Include Files */
/* Public Variables */
/* Public Functions */
void COMM_BLE_UART_Init(void);
void COMM_BLE_UART_PutString(const char8 * string, uint16_t length);
void COMM_BLE_UART_PutChar(char8 character);
void COMM_BLE_UART_MaybeSendData(void);
bool COMM_BLE_UART_HandleEvent(uint32 event, void * eventParam);
#ifdef __cplusplus
}
#endif
#endif // COMM_BLE_UART_H