Beispiel #1
0
/*******************************************************************************
* Function Name: AppCallBack()
********************************************************************************
*
* Summary:
*   This is an event callback function to receive events from the BLE Component.
*
* Parameters:
*  event - the event code
*  *eventParam - the event parameters
*
*******************************************************************************/
void AppCallBack(uint32 event, void* eventParam)
{

#ifdef DEBUG_OUT
    DebugOut(event, eventParam);
#endif

    switch(event)
    {
    case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
        Disconnect_LED_Write(LED_ON);
        mainTimer = 0;
    case CYBLE_EVT_STACK_ON:
        heartRateSimulation = DISABLED;
        /* Put the device into discoverable mode so that remote can search it. */
        StartAdvertisement(ADV_STATE_GENERAL);
        Advertising_LED_Write(LED_ON);
        advState = ADV_STATE_GENERAL;
        break;

    case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
        if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED) /* ADV timed out */
        {
            if(advState == ADV_STATE_GENERAL)
            {
                Advertising_LED_Write(LED_OFF);
                CyDelay(100);
                StartAdvertisement(ADV_STATE_WHITELIST);
                Advertising_LED_Write(LED_ON);
                advState = ADV_STATE_WHITELIST;
            }
            else
            {
                /* No connectionestablished, shut down ADV */
                Advertising_LED_Write(LED_OFF);
            }
        }
        break;

    case CYBLE_EVT_GAP_DEVICE_CONNECTED:
        Disconnect_LED_Write(LED_OFF);
        Advertising_LED_Write(LED_OFF);
        break;

    default:
        break;
    }
}
Beispiel #2
0
/****************************************************************************** 
Function Name: main
*******************************************************************************

Summary:
 Main routine of this firmware. Initializes the components once and hanles the
 BLE events and UART data in a loop forever.

Parameters:
 None.

Return:
 None.

******************************************************************************/
int main()
{
    /* This start-up delay is for the Android app to open when the locator
     * device is plugged into the phone's USB port so that the first printf
     * message will not be missed. 
     */    
	CyDelay(3000);
	
    CyGlobalIntEnable;
    
    UART_DEB_Start();               /* Start communication component */
	
    printf("PSoC: BLE Find Me Locator is Connected\r\n");
    
    Disconnect_LED_Write(LED_OFF);
    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("PSoC: CyBle_Start API Error: 0x%x \r\n", apiResult);
    }
    
    while(1)
    {        
        /*******************************************************************
        *  Processes all pending BLE events in the stack
        *******************************************************************/        
        CyBle_ProcessEvents();
		
		/* Process the received UART data */
        ProcessUartData();
    }
}
Beispiel #3
0
void AppCallBack(uint32 event, void* eventParam)
{
#ifdef DEBUG_OUT
    DebugOut(event, eventParam);
#endif

    switch(event)
    {
    case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
        Disconnect_LED_Write(LED_ON);
    case CYBLE_EVT_STACK_ON:
        StartScan(CYBLE_UUID_HEART_RATE_SERVICE);
        break;

    case CYBLE_EVT_GAP_DEVICE_CONNECTED:
        /* Send authorization request. */
        apiResult = CyBle_GapAuthReq(cyBle_connHandle.bdHandle, &cyBle_authInfo);

        if(CYBLE_ERROR_OK != apiResult)
        {
            printf("CyBle_GapAuthReq API Error: %x \r\n", apiResult);
        }
        else
        {
            printf("Authentification request is sent \r\n");
        }

        Disconnect_LED_Write(LED_OFF);
        Scanning_LED_Write(LED_OFF);
        break;

    case CYBLE_EVT_GAP_AUTH_COMPLETE:
        hrsConnHandle.bdHandle = cyBle_connHandle.bdHandle;
        StartDiscovery();
        break;

    case CYBLE_EVT_GATTC_DISCOVERY_COMPLETE:
        /* Send request to read the body sensor location char. */
        apiResult = HrscReadBodySensorLocation();

        if(apiResult != CYBLE_ERROR_OK)
        {
            printf("HrscReadBodySensorLocation API Error: %x \r\n", apiResult);
        }
        else
        {
            printf("Body Sensor Location Read Request is sent \r\n");
        }
        break;

    case CYBLE_EVT_L2CAP_CONN_PARAM_UPDATE_REQ:
        printf("CYBLE_EVT_L2CAP_CONN_PARAM_UPDATE_REQ: %x, %x, %x, %x \r\n",
               ((CYBLE_GAP_CONN_UPDATE_PARAM_T *)eventParam)->connIntvMax,
               ((CYBLE_GAP_CONN_UPDATE_PARAM_T *)eventParam)->connIntvMin,
               ((CYBLE_GAP_CONN_UPDATE_PARAM_T *)eventParam)->connLatency,
               ((CYBLE_GAP_CONN_UPDATE_PARAM_T *)eventParam)->supervisionTO
              );
        /* Accepted = 0x0000, Rejected  = 0x0001 */
        CyBle_L2capLeConnectionParamUpdateResponse(cyBle_connHandle.bdHandle, 0u);
        break;

    default:
        break;
    }
}
Beispiel #4
0
int main()
{
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;
    CYBLE_STACK_LIB_VERSION_T stackVersion;

    CyGlobalIntEnable;

    UART_DEB_Start();               /* Start communication component */
    printf("BLE Heart Rate Collector Example Project \r\n");

    Disconnect_LED_Write(LED_OFF);
    Scanning_LED_Write(LED_OFF);
    Notification_LED_Write(LED_OFF);

    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_Start API Error: 0x%x \r\n", apiResult);
    }

    apiResult = CyBle_GetStackLibraryVersion(&stackVersion);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_GetStackLibraryVersion API Error: 0x%x \r\n", apiResult);
    }
    else
    {
        printf("Stack Version: %d.%d.%d.%d \r\n", stackVersion.majorVersion,
               stackVersion.minorVersion, stackVersion.patch, stackVersion.buildNumber);
    }

    CyBle_BasRegisterAttrCallback(BasCallBack);
    HrsInit();

    while(1)
    {
        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
        {
            /* Enter DeepSleep mode between connection intervals */
            lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            CyGlobalIntDisable;
            blessState = CyBle_GetBleSsState();

            if(lpMode == CYBLE_BLESS_DEEPSLEEP)
            {
                if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
                {
                    /* Put the device into the DeepSleep mode only when all debug information has been sent */
                    if((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)
                    {
                        CySysPmDeepSleep();
                    }
                    else
                    {
                        CySysPmSleep();
                    }
                }
            }
            else
            {
                if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
                {
                    CySysPmSleep();
                }
            }
            CyGlobalIntEnable;

            /* Handle advertising led blinking */
            HandleLeds();
        }

        /* Store bonding data to flash only when all debug information has been sent */
        if((cyBle_pendingFlashWrite != 0u) &&
                ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u))
        {
            apiResult = CyBle_StoreBondingData(0u);
            printf("Store bonding data, status: %x \r\n", apiResult);
        }


        /*******************************************************************
        *  Processes all pending BLE events in the stack
        *******************************************************************/
        CyBle_ProcessEvents();
    }
}
void DebugOut(uint32 event, void* eventParam)
{
    switch(event)
    {
        case CYBLE_EVT_STACK_ON:
            printf("EVT_STACK_ON \r\n");
            break;

        case CYBLE_EVT_TIMEOUT: /* 0x01 -> GAP limited discoverable mode timeout. */
                                /* 0x02 -> GAP pairing process timeout. */
                                /* 0x03 -> GATT response timeout. */
            printf("EVT_TIMEOUT: %d \r\n", *(uint8 *)eventParam);
            break;

        case CYBLE_EVT_HARDWARE_ERROR:    /* This event indicates that some internal HW error has occurred. */
            printf("EVT_HARDWARE_ERROR \r\n");
            break;

        case CYBLE_EVT_HCI_STATUS:
            printf("EVT_HCI_STATUS \r\n");
            break;

        /**********************************************************
        *                       GAP Events
        ***********************************************************/

        case CYBLE_EVT_GAP_AUTH_REQ:
            printf("EVT_GAP_AUTH_REQ");
            break;

        case CYBLE_EVT_GAP_PASSKEY_ENTRY_REQUEST:
            printf("EVT_GAP_PASSKEY_ENTRY_REQUEST");
            break;

        case CYBLE_EVT_GAP_PASSKEY_DISPLAY_REQUEST:
            printf("EVT_GAP_PASSKEY_DISPLAY_REQUEST: %d \r\n", (int) (*(CYBLE_GAP_PASSKEY_DISP_INFO_T *)eventParam).passkey);
            break;

        case CYBLE_EVT_GAP_AUTH_FAILED:
            printf("EVT_GAP_AUTH_FAILED: %x \r\n", *(uint8 *)eventParam);
            break;

        case CYBLE_EVT_GAP_DEVICE_CONNECTED:
            printf("EVT_GAP_DEVICE_CONNECTED: %x \r\n", cyBle_connHandle.bdHandle);
            break;

        case CYBLE_EVT_GAPC_CONNECTION_UPDATE_COMPLETE:
            printf("EVT_GAPC_CONNECTION_UPDATE_COMPLETE \r\n");
            break;

        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
            printf("EVT_GAP_DEVICE_DISCONNECTED \r\n");
            Disconnect_LED_Write(LED_ON);
            break;

        case CYBLE_EVT_GAP_AUTH_COMPLETE:
            printf("EVT_GAP_AUTH_COMPLETE: security:%x, bonding:%x, ekeySize:%x, authErr %x \r\n",
                        ((CYBLE_GAP_AUTH_INFO_T *)eventParam)->security,
                        ((CYBLE_GAP_AUTH_INFO_T *)eventParam)->bonding, 
                        ((CYBLE_GAP_AUTH_INFO_T *)eventParam)->ekeySize, 
                        ((CYBLE_GAP_AUTH_INFO_T *)eventParam)->authErr);
            break;

        case CYBLE_EVT_GAP_ENCRYPT_CHANGE:
            printf("EVT_GAP_ENCRYPT_CHANGE: %d \r\n", *(uint8 *)eventParam);
            break;
#if 0
        case CYBLE_EVT_GAP_RESOLVE_PVT_ADDR_VERIFY_CNF:
            printf("EVT_GAP_RESOLVE_PVT_ADDR_VERIFY_CNF \r\n");
            break;
#endif

        /**********************************************************
        *                       GATT Events
        ***********************************************************/
        case CYBLE_EVT_GATTC_ERROR_RSP:
            printf("EVT_GATTC_ERROR_RSP: opcode: ");
            switch(((CYBLE_GATTC_ERR_RSP_PARAM_T *)eventParam)->opCode)
            {
                case CYBLE_GATT_FIND_INFO_REQ:
                    printf("FIND_INFO_REQ");
                    break;

                case CYBLE_GATT_READ_BY_TYPE_REQ:
                    printf("READ_BY_TYPE_REQ");
                    break;

                case CYBLE_GATT_READ_BY_GROUP_REQ:
                    printf("READ_BY_GROUP_REQ");
                    break;

                default:
                    printf("%x", ((CYBLE_GATTC_ERR_RSP_PARAM_T *)eventParam)->opCode);
                    break;
            }
            printf(",  handle: %x,  ", ((CYBLE_GATTC_ERR_RSP_PARAM_T *)eventParam)->attrHandle);
            printf("errorcode: ");
            switch(((CYBLE_GATTC_ERR_RSP_PARAM_T *)eventParam)->errorCode)
            {
                case CYBLE_GATT_ERR_ATTRIBUTE_NOT_FOUND:
                    printf("ATTRIBUTE_NOT_FOUND");
                    break;

                default:
                    printf("%x", ((CYBLE_GATTC_ERR_RSP_PARAM_T *)eventParam)->errorCode);
                    break;
            }
            printf("\r\n");
            break;

        case CYBLE_EVT_GATT_CONNECT_IND:
            printf("EVT_GATT_CONNECT_IND: attId %x, bdHandle %x \r\n", 
                ((CYBLE_CONN_HANDLE_T *)eventParam)->attId, ((CYBLE_CONN_HANDLE_T *)eventParam)->bdHandle);
            break;

        case CYBLE_EVT_GATT_DISCONNECT_IND:
            printf("EVT_GATT_DISCONNECT_IND \r\n");
            break;


        /**********************************************************
        *                       L2CAP Events
        ***********************************************************/

        case CYBLE_EVT_L2CAP_CONN_PARAM_UPDATE_REQ:
            printf("EVT_L2CAP_CONN_PARAM_UPDATE_REQ \r\n");
            break;

        /**********************************************************
        *                       Debug Events
        ***********************************************************/

        case CYBLE_DEBUG_EVT_BLESS_INT:
            break;


        default:
        #if(CYBLE_GATT_ROLE_SERVER)
            ServerDebugOut(event, eventParam);
        #endif /* (CYBLE_GATT_ROLE_SERVER) */
        #if(CYBLE_GATT_ROLE_CLIENT)
            ClientDebugOut(event, eventParam);
        #endif /* (CYBLE_GATT_ROLE_SERVER) */
            break;
    }
}
Beispiel #6
0
/*******************************************************************************
* Function Name: AppCallBack()
********************************************************************************
*
* Summary:
*   This is an event callback function to receive events from the BLE Component.
*
* Parameters:
*  event - the event code
*  *eventParam - the event parameters
*
*******************************************************************************/
void AppCallback(uint32 event, void* eventParam)
{
    CYBLE_API_RESULT_T apiResult;
    CYBLE_GAP_BD_ADDR_T localAddr;
    CYBLE_GAP_AUTH_INFO_T *authInfo;
    uint8 i;

    switch (event)
	{
        /**********************************************************
        *                       General Events
        ***********************************************************/
		case CYBLE_EVT_STACK_ON: /* This event received when component is Started */
            /* Enter into discoverable mode so that remote can search it. */
            apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
            if(apiResult != CYBLE_ERROR_OK)
            {
                DBG_PRINTF("StartAdvertisement API Error: %d \r\n", apiResult);
            }
            DBG_PRINTF("Bluetooth On, StartAdvertisement with addr: ");
            localAddr.type = 0u;
            CyBle_GetDeviceAddress(&localAddr);
            for(i = CYBLE_GAP_BD_ADDR_SIZE; i > 0u; i--)
            {
                DBG_PRINTF("%2.2x", localAddr.bdAddr[i-1]);
            }
            DBG_PRINTF("\r\n");
            break;
		case CYBLE_EVT_TIMEOUT: 
            DBG_PRINTF("CYBLE_EVT_TIMEOUT: %x \r\n", *(CYBLE_TO_REASON_CODE_T *)eventParam);
			break;
		case CYBLE_EVT_HARDWARE_ERROR:    /* This event indicates that some internal HW error has occurred. */
            DBG_PRINTF("Hardware Error \r\n");
			break;
        case CYBLE_EVT_HCI_STATUS:
            DBG_PRINTF("CYBLE_EVT_HCI_STATUS: %x \r\n", *(uint8 *)eventParam);
			break;

        /**********************************************************
        *                       GAP Events
        ***********************************************************/
        case CYBLE_EVT_GAP_AUTH_REQ:
            DBG_PRINTF("CYBLE_EVT_AUTH_REQ: security=%x, bonding=%x, ekeySize=%x, err=%x \r\n", 
                (*(CYBLE_GAP_AUTH_INFO_T *)eventParam).security, 
                (*(CYBLE_GAP_AUTH_INFO_T *)eventParam).bonding, 
                (*(CYBLE_GAP_AUTH_INFO_T *)eventParam).ekeySize, 
                (*(CYBLE_GAP_AUTH_INFO_T *)eventParam).authErr);
            break;
        case CYBLE_EVT_GAP_PASSKEY_ENTRY_REQUEST:
            DBG_PRINTF("CYBLE_EVT_PASSKEY_ENTRY_REQUEST \r\n");
            break;
        case CYBLE_EVT_GAP_PASSKEY_DISPLAY_REQUEST:
            DBG_PRINTF("CYBLE_EVT_PASSKEY_DISPLAY_REQUEST %6.6ld \r\n", *(uint32 *)eventParam);
            break;
        case CYBLE_EVT_GAP_AUTH_COMPLETE:
            authInfo = (CYBLE_GAP_AUTH_INFO_T *)eventParam;
            (void)authInfo;
            DBG_PRINTF("AUTH_COMPLETE: security:%x, bonding:%x, ekeySize:%x, authErr %x \r\n", 
                                    authInfo->security, authInfo->bonding, authInfo->ekeySize, authInfo->authErr);
            break;
        case CYBLE_EVT_GAP_AUTH_FAILED:
            DBG_PRINTF("CYBLE_EVT_AUTH_FAILED: %x \r\n", *(uint8 *)eventParam);
            break;
        case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
            DBG_PRINTF("CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP, state: %x\r\n", CyBle_GetState());
            if(CYBLE_STATE_DISCONNECTED == CyBle_GetState())
            {   
                /* Fast and slow advertising period complete, go to low power  
                 * mode (Hibernate mode) and wait for an external
                 * user event to wake up the device again */
                DBG_PRINTF("Hibernate \r\n");
                Advertising_LED_Write(LED_OFF);
                Disconnect_LED_Write(LED_ON);
            #if (DEBUG_UART_ENABLED == ENABLED)
                while((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) != 0);
            #endif /* (DEBUG_UART_ENABLED == ENABLED) */
                SW2_ClearInterrupt();
                Wakeup_Interrupt_ClearPending();
                Wakeup_Interrupt_Start();
                CySysPmHibernate();
            }
            break;
        case CYBLE_EVT_GAP_DEVICE_CONNECTED:
            DBG_PRINTF("CYBLE_EVT_GAP_DEVICE_CONNECTED: %x \r\n", *(uint8 *)eventParam);
            Advertising_LED_Write(LED_OFF);
            break;
        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
            DBG_PRINTF("CYBLE_EVT_GAP_DEVICE_DISCONNECTED\r\n");
            /* Put the device to discoverable mode so that remote can search it. */
            apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
            if(apiResult != CYBLE_ERROR_OK)
            {
                DBG_PRINTF("StartAdvertisement API Error: %x \r\n", apiResult);
            }
            break;
        case CYBLE_EVT_GAP_ENCRYPT_CHANGE:
            DBG_PRINTF("ENCRYPT_CHANGE: %x \r\n", *(uint8 *)eventParam);
            break;
        case CYBLE_EVT_GAPC_CONNECTION_UPDATE_COMPLETE:
            DBG_PRINTF("CYBLE_EVT_CONNECTION_UPDATE_COMPLETE: %x \r\n", *(uint8 *)eventParam);
            break;
        case CYBLE_EVT_GAP_KEYINFO_EXCHNGE_CMPLT:
            DBG_PRINTF("CYBLE_EVT_GAP_KEYINFO_EXCHNGE_CMPLT \r\n");
            break;

        /**********************************************************
        *                       GATT Events
        ***********************************************************/
        case CYBLE_EVT_GATT_CONNECT_IND:
            DBG_PRINTF("CYBLE_EVT_GATT_CONNECT_IND: %x, %x \r\n", cyBle_connHandle.attId, cyBle_connHandle.bdHandle);
            break;
        case CYBLE_EVT_GATT_DISCONNECT_IND:
            DBG_PRINTF("CYBLE_EVT_GATT_DISCONNECT_IND \r\n");
            break;
        case CYBLE_EVT_GATTS_WRITE_REQ:
            DBG_PRINTF("CYBLE_EVT_GATT_WRITE_REQ: %x = ",((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.attrHandle);
            ShowValue(&((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.value);
            (void)CyBle_GattsWriteRsp(((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->connHandle);
            break;
        case CYBLE_EVT_GATTS_INDICATION_ENABLED:
            DBG_PRINTF("CYBLE_EVT_GATTS_INDICATION_ENABLED \r\n");
            break;
        case CYBLE_EVT_GATTS_INDICATION_DISABLED:
            DBG_PRINTF("CYBLE_EVT_GATTS_INDICATION_DISABLED \r\n");
            break;
        case CYBLE_EVT_GATTS_READ_CHAR_VAL_ACCESS_REQ:
            /* Triggered on server side when client sends read request and when
            * characteristic has CYBLE_GATT_DB_ATTR_CHAR_VAL_RD_EVENT property set.
            * This event could be ignored by application unless it need to response
            * by error response which needs to be set in gattErrorCode field of
            * event parameter. */
            DBG_PRINTF("CYBLE_EVT_GATTS_READ_CHAR_VAL_ACCESS_REQ: handle: %x \r\n", 
                ((CYBLE_GATTS_CHAR_VAL_READ_REQ_T *)eventParam)->attrHandle);
            break;

        /**********************************************************
        *                       Other Events
        ***********************************************************/
        case CYBLE_EVT_PENDING_FLASH_WRITE:
            /* Inform application that flash write is pending. Stack internal data 
            * structures are modified and require to be stored in Flash using 
            * CyBle_StoreBondingData() */
            DBG_PRINTF("CYBLE_EVT_PENDING_FLASH_WRITE\r\n");
            break;

		default:
            DBG_PRINTF("OTHER event: %lx \r\n", event);
			break;
	}
}
Beispiel #7
0
/*******************************************************************************
* Function Name: main()
********************************************************************************
* Summary:
*  Main function for the project.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  The function starts BLE and UART components.
*  This function process all BLE events and also implements the low power 
*  functionality.
*
*******************************************************************************/
int main()
{
    CyGlobalIntEnable; 

#if (DEBUG_UART_ENABLED == ENABLED)
    UART_DEB_Start();
#endif /* (DEBUG_UART_ENABLED == ENABLED) */ 
    DBG_PRINTF("BLE Cycling Sensor Example Project \r\n");
    Disconnect_LED_Write(LED_OFF);
    Advertising_LED_Write(LED_OFF);

    CyBle_Start(AppCallback);

    /* Start CYBLE component and register generic event handler */
    CyBle_Start(AppCallback);
    /* Register service specific callback functions */
    CscsInit();
    CpsInit();
    WDT_Start();

    /***************************************************************************
    * Main polling loop
    ***************************************************************************/
	while(1) 
    {   
        /* CyBle_ProcessEvents() allows BLE stack to process pending events */
        CyBle_ProcessEvents();

        /* To achieve low power in the device */
        LowPowerImplementation();

        /***********************************************************************
        * Wait for connection established with Central device
        ***********************************************************************/
        if(CyBle_GetState() == CYBLE_STATE_CONNECTED)
        {
            /*******************************************************************
            *  Periodically simulate Cycling characteristics and send 
            *  results to the Client
            *******************************************************************/        
            if(mainTimer != 0u)
            {
                mainTimer = 0u;

                SimulateCyclingPower();

                CyBle_ProcessEvents();

                SimulateCyclingSpeed();
            }

            /* Store bounding data to flash only when all debug information has been sent */
        #if (DEBUG_UART_ENABLED == ENABLED)
            if((cyBle_pendingFlashWrite != 0u) &&
               ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u))
        #else
            if(cyBle_pendingFlashWrite != 0u)
        #endif /* (DEBUG_UART_ENABLED == ENABLED) */
            {
                CYBLE_API_RESULT_T apiResult;

                apiResult = CyBle_StoreBondingData(0u);
                (void)apiResult;
                DBG_PRINTF("Store bonding data, status: %x \r\n", apiResult);
            }
        }
	}   
}
Beispiel #8
0
/****************************************************************************** 
Function Name: AppCallBack
*******************************************************************************

Summary:
 Handles the events from the BLE stack

Parameters:
 eventCode:    the event code
 *eventParam:  the event parameters

Return:
 None.

******************************************************************************/
void AppCallBack(uint32 event, void* eventParam)
{
	int i;
	
    switch(event)
    {
        case CYBLE_EVT_STACK_ON:
        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
            Disconnect_LED_Write(LED_ON);
            break;

        case CYBLE_EVT_GAP_DEVICE_CONNECTED:
			
            /* Indicate to stack that the OOB data is available. OOB data needs
             * to be set before initiating the auth (pair) request. 
             */
			if(CyBle_GapSetOobData(cyBle_connHandle.bdHandle, CYBLE_GAP_OOB_ENABLE, securityKey, NULL, NULL)  != CYBLE_ERROR_OK)
			{
				printf("PSoC: Error in Setting OOB Data \r\n");
			}
			else
			{
				printf("PSoC: OOB Data is set with key:");
				for(i = 0; i < 16; i++)
				{
					printf("%2.2x", securityKey[i]);
				}
				printf("\r\n");
			}

			/* Send authorization request. */
            apiResult = CyBle_GapAuthReq(cyBle_connHandle.bdHandle, &cyBle_authInfo);
            
            if(CYBLE_ERROR_OK != apiResult)
        	{
        		printf("PSoC: CyBle_GapAuthReq API Error: %x \r\n", apiResult);
        	}
        	else
        	{
        		printf("PSoC: Pairing is initiated \r\n");
                Disconnect_LED_Write(LED_OFF);
        	}
            break;
            
        case CYBLE_EVT_GAP_AUTH_COMPLETE:
            printf("PSoC: ***** Pairing is Successful! ***** \r\n");
            break;
			
		case CYBLE_EVT_GAP_AUTH_FAILED:
        printf("PSoC: EVT_GAP_AUTH_FAILED, reason: ");
        switch(*(CYBLE_GAP_AUTH_FAILED_REASON_T *)eventParam)
        {
            case CYBLE_GAP_AUTH_ERROR_CONFIRM_VALUE_NOT_MATCH:
                printf("CONFIRM_VALUE_NOT_MATCH\r\n");
                break;
                
            case CYBLE_GAP_AUTH_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
                printf("INSUFFICIENT_ENCRYPTION_KEY_SIZE\r\n");
                break;
            
            case CYBLE_GAP_AUTH_ERROR_UNSPECIFIED_REASON:
                printf("UNSPECIFIED_REASON\r\n");
                break;
                
            case CYBLE_GAP_AUTH_ERROR_AUTHENTICATION_TIMEOUT:
                printf("AUTHENTICATION_TIMEOUT\r\n");
                break;
                
            default:
                printf("0x%x  \r\n", *(CYBLE_GAP_AUTH_FAILED_REASON_T *)eventParam);
                break;
        }
        break;

        case CYBLE_EVT_L2CAP_CONN_PARAM_UPDATE_REQ:
            /* Accepted = 0x0000, Rejected  = 0x0001 */
            CyBle_L2capLeConnectionParamUpdateResponse(cyBle_connHandle.bdHandle, 0u); 
            break;

        default:
            break;
    }
}
Beispiel #9
0
*******************************************************************************/int main()
{
    CyGlobalIntEnable;  

#if (DEBUG_UART_ENABLED == ENABLED)
    UART_DEB_Start();
#endif /* (DEBUG_UART_ENABLED == ENABLED) */
    DBG_PRINTF("BLE HID Keyboard Example Project \r\n");

    Disconnect_LED_Write(LED_OFF);
    Advertising_LED_Write(LED_OFF);
    CapsLock_LED_Write(LED_OFF);

    /* Start CYBLE component and register generic event handler */
    CyBle_Start(AppCallBack);
    WDT_Start();

#if (BAS_MEASURE_ENABLE != 0)
    ADC_Start();
#endif /* BAS_MEASURE_ENABLE != 0 */

    while(1) 
    {           
        /* CyBle_ProcessEvents() allows BLE stack to process pending events */
        CyBle_ProcessEvents();

        /* To achieve low power in the device */
        LowPowerImplementation();

        if((CyBle_GetState() == CYBLE_STATE_CONNECTED) && (suspend != CYBLE_HIDS_CP_SUSPEND))
        {
            if(mainTimer != 0u)
            {
                mainTimer = 0u;                
            #if (BAS_SIMULATE_ENABLE != 0)
                SimulateBattery();
                CyBle_ProcessEvents();
            #endif /* BAS_SIMULATE_ENABLE != 0 */    
            #if (BAS_MEASURE_ENABLE != 0)
                MeasureBattery();
                CyBle_ProcessEvents();
            #endif /* BAS_MEASURE_ENABLE != 0 */
                if(keyboardSimulation == ENABLED)
                {
                    SimulateKeyboard();
                }
            }
            /* Store bonding data to flash only when all debug information has been sent */
        #if(CYBLE_BONDING_REQUIREMENT == CYBLE_BONDING_YES)
        #if (DEBUG_UART_ENABLED == ENABLED)
            if((cyBle_pendingFlashWrite != 0u) &&
               ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u))
            
        #else
            if(cyBle_pendingFlashWrite != 0u)
        #endif /* (DEBUG_UART_ENABLED == ENABLED) */
            {
                CYBLE_API_RESULT_T apiResult;
                
                apiResult = CyBle_StoreBondingData(0u);
                (void)apiResult;
                DBG_PRINTF("Store bonding data, status: %x \r\n", apiResult);
            }
        #endif /* CYBLE_BONDING_REQUIREMENT == CYBLE_BONDING_YES */    
        }
	}   
}  
Beispiel #10
0
int main()
{
    CYBLE_STACK_LIB_VERSION_T stackVersion;

    CyGlobalIntEnable;
    
    UART_DEB_Start();               /* Start communication component */
    printf("BLE Heart Rate Sensor Example Project \r\n");
    
    Disconnect_LED_Write(LED_OFF);
    Advertising_LED_Write(LED_OFF);

    /* Start CYBLE component and register generic event handler */
    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_Start API Error: %x \r\n", apiResult);
    }
    
    apiResult = CyBle_GetStackLibraryVersion(&stackVersion);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_GetStackLibraryVersion API Error: 0x%x \r\n", apiResult);
    }
    else
    {
        printf("Stack Version: %d.%d.%d.%d \r\n", stackVersion.majorVersion, 
            stackVersion.minorVersion, stackVersion.patch, stackVersion.buildNumber);
    }
    
    /* Services initialization */
    HrsInit();
    
    WDT_Start();
    
    /***************************************************************************
    * Main polling loop
    ***************************************************************************/
    while(1)
    {        
        /***********************************************************************
        * Wait for connection established with Central device
        ***********************************************************************/
        if(CyBle_GetState() == CYBLE_STATE_CONNECTED)
        {
            /*******************************************************************
            *  Periodically simulate heart rate and measure a battery level 
            *  and send results to the Client
            *******************************************************************/        
            if(mainTimer != 0u)
            {
                mainTimer = 0u;
                if(heartRateSimulation == ENABLED)
                {
                    SimulateHeartRate();
                }
            }
        }
        
        /*******************************************************************
        *  Process all pending BLE events in the stack
        *******************************************************************/
        CyBle_ProcessEvents();
    }
}
Beispiel #11
0
int main()
{
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;

    CyGlobalIntEnable;

    UART_DEB_Start();               /* Start communication component */
    printf("BLE Secure Connection Example Project \r\n");

    Disconnect_LED_Write(LED_OFF);
    Advertising_LED_Write(LED_OFF);

    /* Start CYBLE component and register generic event handler */
    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_Start API Error: %x \r\n", apiResult);
    }

    /* Services initialization */
    HrsInit();

    /***************************************************************************
    * Main polling loop
    ***************************************************************************/
    while(1)
    {
        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
        {
            /* Enter DeepSleep mode between connection intervals */
            lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            CyGlobalIntDisable;
            blessState = CyBle_GetBleSsState();

            if(lpMode == CYBLE_BLESS_DEEPSLEEP)
            {
                if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
                {
                    /* Put the device into the Deep Sleep mode only when all debug information has been sent */
                    if((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)
                    {
                        CySysPmDeepSleep();
                    }
                    else
                    {
                        CySysPmSleep();
                    }
                }
            }
            else
            {
                if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
                {
                    CySysPmSleep();
                }
            }
            CyGlobalIntEnable;
        }


        /***********************************************************************
        * Wait for connection established with Central device
        ***********************************************************************/
        if(CyBle_GetState() == CYBLE_STATE_CONNECTED)
        {
            /*******************************************************************
            *  Periodically simulates heart beat and sends the results to the Client
            *******************************************************************/
            mainTimer++;

            if(mainTimer == MAIN_LOOP_SIMULATION_THRESHOLD)
            {
                mainTimer = 0u;
                if(heartRateSimulation == ENABLED)
                {
                    SimulateHeartRate();
                    CyBle_ProcessEvents();
                }
            }
            else if((cyBle_pendingFlashWrite != 0u) &&
                    ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u))
            {
                apiResult = CyBle_StoreBondingData(0u);
                printf("Store bonding data, status: %x \r\n", apiResult);
            }
            else
            {
                /* nothing else */
            }
        }

        /*******************************************************************
        *  Process all pending BLE events in the stack
        *******************************************************************/
        CyBle_ProcessEvents();
    }
}