/******************************************************************************* * Function Name: StackEventHandler ******************************************************************************** * * Summary: * This is an event callback function to receive events from the BLE Component. * * Parameters: * uint8 event: Event from the CYBLE component * void* eventParams: A structure instance for corresponding event type. The * list of event structure is described in the component * datasheet. * * Return: * None * *******************************************************************************/ void StackEventHandler(uint32 event, void *eventParam) { switch(event) { /* Mandatory events to be handled by Find Me Target design */ case CYBLE_EVT_STACK_ON: case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: /* Start BLE advertisement for 30 seconds and update link * status on LEDs */ CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); Advertising_LED_Write(LED_ON); break; case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP: if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED) { /* Advertisement event timed out, go to low power * mode (Hibernate mode) and wait for an external * user event to wake up the device again */ Advertising_LED_Write(LED_OFF); Hibernate_LED_Write(LED_ON); Wakeup_SW_ClearInterrupt(); Wakeup_Interrupt_ClearPending(); Wakeup_Interrupt_Start(); CySysPmHibernate(); } break; default: break; } }
/******************************************************************************* * Function Name: AppCallBack() ******************************************************************************** * * Summary: * This is an event callback function to receive events from the BLE Component. * *******************************************************************************/ 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 is received when the 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: break; case CYBLE_EVT_HARDWARE_ERROR: /* This event indicates that some internal HW error has occurred. */ DBG_PRINTF("CYBLE_EVT_HARDWARE_ERROR \r\n"); break; /* This event will be triggered by host stack if BLE stack is busy or not busy. * Parameter corresponding to this event will be the state of BLE stack. * BLE stack busy = CYBLE_STACK_STATE_BUSY, * BLE stack not busy = CYBLE_STACK_STATE_FREE */ case CYBLE_EVT_STACK_BUSY_STATUS: DBG_PRINTF("CYBLE_EVT_STACK_BUSY_STATUS: %x\r\n", *(uint8 *)eventParam); 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 press 'p' to enter passkey \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_KEYINFO_EXCHNGE_CMPLT: DBG_PRINTF("CYBLE_EVT_GAP_KEYINFO_EXCHNGE_CMPLT \r\n"); 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_ADVERTISING, 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"); LED_BLU_Write(LED_OFF); LED_RED_Write(LED_ON); LED_GRN_Write(LED_OFF); SW2_ClearInterrupt(); Wakeup_Interrupt_ClearPending(); Wakeup_Interrupt_Start(); #if (DEBUG_UART_ENABLED == ENABLED) /* Wait until debug info is sent */ while((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) != 0); #endif /* (DEBUG_UART_ENABLED == ENABLED) */ CySysPmHibernate(); } break; case CYBLE_EVT_GAP_DEVICE_CONNECTED: DBG_PRINTF("CYBLE_EVT_GAP_DEVICE_CONNECTED \r\n"); LED_BLU_Write(LED_OFF); break; case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: DBG_PRINTF("CYBLE_EVT_GAP_DEVICE_DISCONNECTED\r\n"); apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { DBG_PRINTF("StartAdvertisement API Error: %d \r\n", apiResult); } break; case CYBLE_EVT_GATTS_XCNHG_MTU_REQ: { uint16 mtu; CyBle_GattGetMtuSize(&mtu); DBG_PRINTF("CYBLE_EVT_GATTS_XCNHG_MTU_REQ, final mtu= %d \r\n", mtu); } 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_GAP_ENCRYPT_CHANGE: DBG_PRINTF("CYBLE_EVT_GAP_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; /********************************************************** * 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); /* Register service specific callback functions */ HidsInit(); BasInit(); ScpsInit(); break; case CYBLE_EVT_GATT_DISCONNECT_IND: DBG_PRINTF("CYBLE_EVT_GATT_DISCONNECT_IND \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; } }
/******************************************************************************* * Function Name: StackEventHandler ******************************************************************************** * * Summary: * This is an event callback function to receive events from the BLE Component. * * Parameters: * uint8 event: Event from the CYBLE component * void* eventParams: A structure instance for corresponding event type. The * list of event structure is described in the component * datasheet. * * Return: * None * *******************************************************************************/ void StackEventHandler(uint32 event, void *eventParam) { char authFailReasonCode[3]; CYBLE_GAP_AUTH_FAILED_REASON_T *authFailReason; switch(event) { /* Mandatory events to be handled by Find Me Target design */ case CYBLE_EVT_STACK_ON: case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: /* Start BLE advertisement for 30 seconds and update link * status on LEDs */ CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); Advertising_LED_Write(LED_ON); PWM_WriteCompare(LED_NO_ALERT); break; case CYBLE_EVT_GAP_DEVICE_CONNECTED: UART_UartPutString("GAP Device Connected\r\n"); /* BLE link is established */ Advertising_LED_Write(LED_OFF); break; case CYBLE_EVT_TIMEOUT: if(*(uint8 *) eventParam == CYBLE_GAP_ADV_MODE_TO) { /* Advertisement event timed out, go to low power * mode (Hibernate mode) and wait for an external * user event to wake up the device again */ Advertising_LED_Write(LED_OFF); Hibernate_LED_Write(LED_ON); PWM_Stop(); Wakeup_SW_ClearInterrupt(); Wakeup_Interrupt_ClearPending(); Wakeup_Interrupt_Start(); CySysPmHibernate(); } break; /********************************************************** * GAP Events ***********************************************************/ case CYBLE_EVT_GAP_AUTH_REQ: UART_UartPutString("Authorization Requested\r\n"); break; case CYBLE_EVT_GAP_AUTH_COMPLETE: UART_UartPutString("Pairing is Successful!\r\n"); break; case CYBLE_EVT_GAP_AUTH_FAILED: authFailReason = ((CYBLE_GAP_AUTH_FAILED_REASON_T *)eventParam); UART_UartPutString("Authentication Failed with Reason Code: "); snprintf(authFailReasonCode, sizeof(authFailReasonCode), "%lu", (uint32)(*authFailReason)); UART_UartPutString(authFailReasonCode); UART_UartPutChar("\r\n"); break; /********************************************************** * GATT Events ***********************************************************/ case CYBLE_EVT_GATT_CONNECT_IND: UART_UartPutString("GATT Connection Indication\r\n"); /* Set OOB data after the connection indication but before the authorization * request is received. */ if(CyBle_GapSetOobData(cyBle_connHandle.bdHandle, CYBLE_GAP_OOB_ENABLE, securityKey, NULL, NULL) != CYBLE_ERROR_OK) { UART_UartPutString("Error in Setting OOB Data\r\n"); } else { UART_UartPutString("OOB Data is Set\r\n"); } break; default: break; } }
/******************************************************************************* * Function Name: AppCallBack ******************************************************************************** * * Summary: * This is an event callback function to receive events from the CYBLE Component. * * Parameters: * uint8 event: Event from the CYBLE component. * void* eventParams: A structure instance for corresponding event type. The * list of event structure is described in the component * datasheet. * * Return: * None * *******************************************************************************/ void AppCallBack(uint32 event, void* eventParam) { CYBLE_API_RESULT_T apiResult; CYBLE_GATTS_WRITE_REQ_PARAM_T *wrReqParam; switch (event) { /********************************************************** * General Events ***********************************************************/ case CYBLE_EVT_STACK_ON: /* This event is 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) { //ShowError(); } printf("CYBLE_EVT_STACK_ON\n"); break; case CYBLE_EVT_TIMEOUT: printf("CYBLE_EVT_TIMEOUT\n"); break; case CYBLE_EVT_HARDWARE_ERROR: /* This event indicates that some internal HW error has occurred. */ //ShowError(); break; /********************************************************** * GAP Events ***********************************************************/ case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP: printf("CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP\n"); 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 */ // Advertising_LED_Write(LED_OFF); // Disconnect_LED_Write(LED_ON); // SW2_ClearInterrupt(); // Wakeup_Interrupt_ClearPending(); // Wakeup_Interrupt_Start(); CySysPmHibernate(); } break; case CYBLE_EVT_GAP_DEVICE_CONNECTED: printf("CYBLE_EVT_GAP_DEVICE_CONNECTED\n"); // Disconnect_LED_Write(LED_OFF); // Advertising_LED_Write(LED_OFF); break; case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: /* Put the device to discoverable mode so that remote can search it. */ printf("CYBLE_EVT_GAP_DEVICE_DISCONNECTED\n"); apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { //ShowError(); } break; case CYBLE_EVT_GAPC_CONNECTION_UPDATE_COMPLETE: break; /********************************************************** * GATT Events ***********************************************************/ case CYBLE_EVT_GATT_CONNECT_IND: printf("CYBLE_EVT_GATT_CONNECT_IND\n"); break; case CYBLE_EVT_GATT_DISCONNECT_IND: printf("CYBLE_EVT_GATT_DISCONNECT_IND\n"); // Reset the rotation notification _rotationCCCDValue[0] = 0; _voltageCCCDValue[0] = 0; _drawCCCDValue[0] = 0; break; case CYBLE_EVT_GATTS_WRITE_REQ: printf("CYBLE_EVT_GATTS_WRITE_REQ\n"); wrReqParam = (CYBLE_GATTS_WRITE_REQ_PARAM_T *) eventParam; /* Write request for time/date */ if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_TIME_CHAR_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(CYBLE_GATT_ERR_NONE == CyBle_GattsWriteAttributeValue(&wrReqParam->handleValPair, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED)) { uint64_t datetime = wrReqParam->handleValPair.value.val[0]; datetime <<= 8; datetime |= wrReqParam->handleValPair.value.val[1]; datetime <<= 8; datetime |= wrReqParam->handleValPair.value.val[2]; datetime <<= 8; datetime |= wrReqParam->handleValPair.value.val[3]; printf("Date time EPOCH: %08x\n", (unsigned int)(datetime >> 32)); printf("Date time EPOCH: %08x\n", (unsigned int)datetime); RTC_SetUnixTime(datetime); } } /* Write request for time/date */ if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_FILTERGAIN_CHAR_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(CYBLE_GATT_ERR_NONE == CyBle_GattsWriteAttributeValue(&wrReqParam->handleValPair, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED)) { _rotationFilterGain = wrReqParam->handleValPair.value.val[0]; _rotationFilterGain <<= 8; _rotationFilterGain |= wrReqParam->handleValPair.value.val[1]; _rotationFilterGain <<= 8; _rotationFilterGain |= wrReqParam->handleValPair.value.val[2]; _rotationFilterGain <<= 8; _rotationFilterGain |= wrReqParam->handleValPair.value.val[3]; } } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_DRAWOFFSET_CHAR_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(CYBLE_GATT_ERR_NONE == CyBle_GattsWriteAttributeValue(&wrReqParam->handleValPair, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED)) { _drawOffset = wrReqParam->handleValPair.value.val[0]; _drawOffset <<= 8; _drawOffset |= wrReqParam->handleValPair.value.val[1]; } } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_ROTATIONSPEED_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(wrReqParam->handleValPair.value.val[CYBLE_POVDISPLAY_ROTATIONSPEED_CHARACTERISTIC_USER_DESCRIPTION_DESC_INDEX] == 1) { _rotationCCCDValue[0] = 1; } else { _rotationCCCDValue[0] = 0; } /* Update CCCD handle with notification status data*/ _rotationNotificationCCCDHandle.attrHandle = CYBLE_POVDISPLAY_ROTATIONSPEED_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE; _rotationNotificationCCCDHandle.value.val = _rotationCCCDValue; _rotationNotificationCCCDHandle.value.len = 2; /* Report data to BLE component for sending data when read by Central device */ CyBle_GattsWriteAttributeValue(&_rotationNotificationCCCDHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED); } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_VOLTAGE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(wrReqParam->handleValPair.value.val[CYBLE_POVDISPLAY_VOLTAGE_CHARACTERISTIC_USER_DESCRIPTION_DESC_INDEX] == 1) { _voltageCCCDValue[0] = 1; } else { _voltageCCCDValue[0] = 0; } /* Update CCCD handle with notification status data*/ _voltageNotificationCCCDHandle.attrHandle = CYBLE_POVDISPLAY_VOLTAGE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE; _voltageNotificationCCCDHandle.value.val = _voltageCCCDValue; _voltageNotificationCCCDHandle.value.len = 2; /* Report data to BLE component for sending data when read by Central device */ CyBle_GattsWriteAttributeValue(&_voltageNotificationCCCDHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED); } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_DRAWTIME_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(wrReqParam->handleValPair.value.val[CYBLE_POVDISPLAY_DRAWTIME_CHARACTERISTIC_USER_DESCRIPTION_DESC_INDEX] == 1) { _drawCCCDValue[0] = 1; } else { _drawCCCDValue[0] = 0; } /* Update CCCD handle with notification status data*/ _drawNotificationCCCDHandle.attrHandle = CYBLE_POVDISPLAY_DRAWTIME_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE; _drawNotificationCCCDHandle.value.val = _drawCCCDValue; _drawNotificationCCCDHandle.value.len = 2; /* Report data to BLE component for sending data when read by Central device */ CyBle_GattsWriteAttributeValue(&_drawNotificationCCCDHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED); } CyBle_GattsWriteRsp(cyBle_connHandle); break; /********************************************************** * Other Events ***********************************************************/ case CYBLE_EVT_STACK_BUSY_STATUS: /* This event is generated when the internal stack buffer is full and no more * data can be accepted or the stack has buffer available and can accept data. * This event is used by application to prevent pushing lot of data to stack. */ /* Extract the present stack status */ _busyStatus = * (uint8*)eventParam; break; default: break; }
/******************************************************************************* * Function Name: AppCallBack() ******************************************************************************** * * Summary: * This finction handles events that are generated by BLE stack. * * Parameters: * None * * Return: * None * *******************************************************************************/ void AppCallBack(uint32 event, void* eventParam) { CYBLE_API_RESULT_T apiResult; uint32 i = 0u; switch (event) { /********************************************************** * General Events ***********************************************************/ case CYBLE_EVT_STACK_ON: /* This event received when component is Started */ /* Enter in to discoverable mode so that remote can search it. */ apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { } break; case CYBLE_EVT_HARDWARE_ERROR: /* This event indicates that some internal HW error has occurred. */ DBG_PRINTF("CYBLE_EVT_HARDWARE_ERROR\r\n"); break; /********************************************************** * GAP Events ***********************************************************/ case CYBLE_EVT_GAP_AUTH_REQ: DBG_PRINTF("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("EVT_PASSKEY_ENTRY_REQUEST press 'p' to enter passkey \r\n"); break; case CYBLE_EVT_GAP_PASSKEY_DISPLAY_REQUEST: DBG_PRINTF("EVT_PASSKEY_DISPLAY_REQUEST %6.6ld \r\n", *(uint32 *)eventParam); break; case CYBLE_EVT_GAP_KEYINFO_EXCHNGE_CMPLT: DBG_PRINTF("EVT_GAP_KEYINFO_EXCHNGE_CMPLT \r\n"); break; case CYBLE_EVT_GAP_AUTH_COMPLETE: DBG_PRINTF("AUTH_COMPLETE"); break; case CYBLE_EVT_GAP_AUTH_FAILED: DBG_PRINTF("EVT_AUTH_FAILED: %x \r\n", *(uint8 *)eventParam); break; case CYBLE_EVT_GAP_DEVICE_CONNECTED: DBG_PRINTF("EVT_GAP_DEVICE_CONNECTED: %d \r\n", connHandle.bdHandle); LED_WRITE_MACRO(LED_OFF); break; case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: DBG_PRINTF("EVT_GAP_DEVICE_DISCONNECTED\r\n"); apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { DBG_PRINTF("StartAdvertisement API Error: %d \r\n", apiResult); } break; case CYBLE_EVT_GAP_ENCRYPT_CHANGE: DBG_PRINTF("EVT_GAP_ENCRYPT_CHANGE: %x \r\n", *(uint8 *)eventParam); break; case CYBLE_EVT_GAPC_CONNECTION_UPDATE_COMPLETE: DBG_PRINTF("EVT_CONNECTION_UPDATE_COMPLETE: %x \r\n", *(uint8 *)eventParam); break; case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP: 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("Entering low power mode...\r\n"); Bootloading_LED_Write(LED_ON); Advertising_LED_1_Write(LED_ON); Advertising_LED_2_Write(LED_ON); Bootloader_Service_Activation_ClearInterrupt(); Wakeup_Interrupt_ClearPending(); Wakeup_Interrupt_Start(); CySysPmHibernate(); } break; /********************************************************** * GATT Events ***********************************************************/ case CYBLE_EVT_GATTS_WRITE_REQ: DBG_PRINTF("EVT_GATT_WRITE_REQ: %x = ",((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.attrHandle); for(i = 0; i < ((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.value.len; i++) { DBG_PRINTF("%2.2x ", ((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.value.val[i]); } DBG_PRINTF("\r\n"); CyBle_GattsWriteAttributeValue(&((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair, 0u, \ &((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->connHandle, CYBLE_GATT_DB_PEER_INITIATED); (void)CyBle_GattsWriteRsp(((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->connHandle); break; case CYBLE_EVT_GATT_CONNECT_IND: connHandle = *(CYBLE_CONN_HANDLE_T *)eventParam; break; case CYBLE_EVT_GATT_DISCONNECT_IND: connHandle.bdHandle = 0; break; case CYBLE_EVT_GATTS_WRITE_CMD_REQ: /* Pass packet to bootloader emulator */ packetRXSize = ((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.value.len; memcpy(&packetRX[0], ((CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam)->handleValPair.value.val, packetRXSize); packetRXFlag = 1u; break; case CYBLE_EVT_GATTS_PREP_WRITE_REQ: (void)CyBle_GattsPrepWriteReqSupport(CYBLE_GATTS_PREP_WRITE_NOT_SUPPORT); break; case CYBLE_EVT_HCI_STATUS: DBG_PRINTF("CYBLE_EVT_HCI_STATUS\r\n"); default: break; } }
/******************************************************************************* * 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 - both deep sleep and hibernate. * *******************************************************************************/ int main() { CYBLE_GAP_BONDED_DEV_ADDR_LIST_T bondedDeviceList; /* Enable global interrupts */ CyGlobalIntEnable; /* Start a new UART session and clear screen */ UART_Start(); UART_UartPutChar(12); UART_UartPutString("=====ANCS Demo=====\n\r"); UART_UartPutString("\n\rPress 'R' at any time to clear the bonded device list. \n\r"); /* Initialize BLE component and ANCS related information */ CyBle_Start(StackEventHandler); Ancs_Reset(); /* Check whether we already have anything in the bonded device list */ CyBle_GapGetBondedDevicesList(&bondedDeviceList); if(bondedDeviceList.count != 0) { authState = AUTHENTICATION_BONDING_COMPLETE; } /* Custom service discovery is not implemented in the component yet. * So we need to enable all the events to come to the application * in order to process raw data for ANCS service discovery. */ cyBle_eventHandlerFlag |= CYBLE_ENABLE_ALL_EVENTS; /* Inifinite loop */ for(;;) { /* Process all the generated events. This includes the general events * for BLE initialization, advertisement, connection etc. It also * includes the events for service discovery and ANCS notifications. */ CyBle_ProcessEvents(); /* Handle the state machine for ANCS notifications */ Ancs_StateMachine(); /* Handle bonding of device information*/ BondingImplementation(); /* To achieve low power in the device */ LowPowerImplementation(); /* Enter hibernate mode when necessary. */ if(enterHibernateFlag == true) { WakeupPin_ClearInterrupt(); WakeupPinInterrupt_Start(); /* Wait for the UART to complete transfer */ while((UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) != 0u); CySysPmHibernate(); } } }