/* 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 */
