/******************************************************************************* * Function Name: HandleBleProcessing ******************************************************************************** * * Summary: * Handles the BLE state machine for intiating different procedures * during different states of BLESS. * * Parameters: * None. * * Return: * None. * *******************************************************************************/ void HandleBleProcessing(void) { CYBLE_API_RESULT_T cyble_api_result; switch (cyBle_state) { case CYBLE_STATE_SCANNING: if(peerDeviceFound) { CyBle_GapcStopScan(); } break; case CYBLE_STATE_CONNECTED: /* if Client does not has all the information about attribute handles * call procedure for getting it */ if((INFO_EXCHANGE_COMPLETE != infoExchangeState)) { attrHandleInit(); } /* enable notifications if not enabled already */ else if(false == notificationEnabled) { enableNotifications(); } /* if client has all required info and stack is free, handle UART traffic */ else if(CyBle_GattGetBusStatus() != CYBLE_STACK_STATE_BUSY) { HandleUartTxTraffic(); } break; case CYBLE_STATE_DISCONNECTED: { if(peerDeviceFound) { cyble_api_result = CyBle_GapcConnectDevice(&peerAddr); if(CYBLE_ERROR_OK == cyble_api_result) { peerDeviceFound = false; } } else { CyBle_GapcStartScan(CYBLE_SCANNING_FAST); } break; } default: break; } }
/******************************************************************************* * Function Name: ConnectToPeripheralDevice ******************************************************************************** * Summary: * Connects to Peripheral device with given BD Address. * * Parameters: * void * * Return: * void * *******************************************************************************/ void ConnectToPeripheralDevice(void) { CYBLE_API_RESULT_T apiResult; /* If flag has been set to connect to a device... */ if(clientConnectToDevice) { /* Process pending BLE events */ CyBle_ProcessEvents(); #if 0 if(CYBLE_STATE_SCANNING == CyBle_GetState()) { CyBle_GapcStopScan(); #ifdef DEBUG_ENABLED UART_UartPutString("Stop Scan called from clientConnectToDevice "); SendBLEStatetoUART(CyBle_GetState()); UART_UartPutCRLF(' '); #endif CyBle_ProcessEvents(); } #endif if(CYBLE_STATE_DISCONNECTED == CyBle_GetState()) { /* Reset the flag to connect to a device*/ clientConnectToDevice = FALSE; /* Connect to the device whose address has been saved as part of * potential node search */ apiResult = CyBle_GapcConnectDevice(&peripAddr); if(apiResult != CYBLE_ERROR_OK) { #ifdef DEBUG_ENABLED UART_UartPutString("Connect Request failed "); SendBLEStatetoUART(CyBle_GetState()); UART_UartPutCRLF(' '); #endif } else { #ifdef DEBUG_ENABLED UART_UartPutString("Connect Request Sent "); SendBLEStatetoUART(CyBle_GetState()); UART_UartPutCRLF(' '); #endif } /* Process pending BLE events */ CyBle_ProcessEvents(); } } }
void BleEventHandler(uint32 event, void *eventParam) { hkj_ble_events_log_add(event, eventParam); switch(event) { case CYBLE_EVT_STACK_ON: CyBle_GapcConnectDevice(&connectPeriphDevice); break; case CYBLE_EVT_GATT_CONNECT_IND: connHandle = *(CYBLE_CONN_HANDLE_T *)eventParam; break; default: break; } }
void BleEventHandler(uint32 event, void *eventParam) { switch(event) { case CYBLE_EVT_STACK_ON: CyBle_GapcConnectDevice(&connectPeriphDevice); break; case CYBLE_EVT_GATT_CONNECT_IND: connHandle = *(CYBLE_CONN_HANDLE_T *)eventParam; break; case CYBLE_EVT_GATTC_DISCOVERY_COMPLETE: ledStateHandle = cyBle_customCServ[CYBLE_CUSTOMC_LEDSERVICE_SERVICE_INDEX].\ customServChar[CYBLE_CUSTOMC_LEDSERVICE_LEDSTATE_CHAR_INDEX].customServCharHandle; break; default: break; } }
void AppCallBack(uint32 event, void* eventParam) { CYBLE_API_RESULT_T apiResult; CYBLE_GATTS_WRITE_REQ_PARAM_T *wrReqParam; uint8 len; #ifdef DEBUG_OUT DebugOut(event, eventParam); //printf("ACB evt: %ld \r\n",event); #endif switch(event) { case CYBLE_EVT_STACK_ON: printf("EVT_STACK_ON \r\n"); goto start_scan; case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: BleConnected = 0; printf("EVT_GAP_DEVICE_DISCONNECTED \r\n"); goto start_scan; case CYBLE_EVT_TIMEOUT: printf("EVT_TIMEOUT \r\n"); start_scan: if(BleConnected == 0) { StartScan(CYBLE_UUID_CUSTOM_UART_TX_SERVICE); //Scanning_LED_Write(LED_ON); ble_state = BLE_SCAN_STATE; } break; #ifndef DEBUG_OUT case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT: ScanProgressEventHandler((CYBLE_GAPC_ADV_REPORT_T *)eventParam); break; case CYBLE_EVT_GAPC_SCAN_START_STOP: printf("EVT_GAPC_SCAN_START_STOP \r\n"); if(0u != (flag & CONNECT)) { printf("Connect to the Device: %x \r\n", deviceN); /* Connect to selected device */ apiResult = CyBle_GapcConnectDevice(&peerAddr[deviceN]); if(apiResult != CYBLE_ERROR_OK) { printf("ConnectDevice API Error: %x \r\n", apiResult); } flag &= ~CONNECT; BleConnected = 1; } break; case CYBLE_EVT_GATTC_HANDLE_VALUE_NTF: wrReqParam = (CYBLE_GATTS_WRITE_REQ_PARAM_T *) eventParam; if(wrReqParam->handleValPair.attrHandle == UART_TX_HANDLE) { len = wrReqParam->handleValPair.value.len; Buffer[0] = len; memcpy(&Buffer[1],wrReqParam->handleValPair.value.val,len); uCommState.Bit.BLERxFinshed = ENABLED; //printf("len %d buf[0] %d \r\n", len,Buffer[0]); } break; case CYBLE_EVT_GATT_CONNECT_IND: printf("EVT_GATT_CONNECT_IND \r\n"); break; case CYBLE_EVT_GATTC_XCHNG_MTU_RSP: printf("CYBLE_EVT_GATTC_XCHNG_MTU_RSP \r\n"); break; #endif case CYBLE_EVT_GAP_DEVICE_CONNECTED: printf("EVT_GAP_DEVICE_CONNECTED \r\n"); apiResult = CyBle_GattcExchangeMtuReq(cyBle_connHandle,0x200); if(apiResult == CYBLE_ERROR_OK) { printf("zozo test exchg mtu req send\r\n"); } ble_state = BLE_CONNECTED_STATE; break; case CYBLE_EVT_GATTC_DISCOVERY_COMPLETE: break; default: break; } }
/****************************************************************************** Function Name: ProcessUartData ******************************************************************************* Summary: Parses the commands received through the UART and initiates actions based on the command. For example, this function initiates connection to a peer device upon receiving the connect command. Parameters: None. Return: None. ******************************************************************************/ void ProcessUartData(void) { uint32 data; uint32 length; uint32 i; CYBLE_GAP_BD_ADDR_T peerAddr; if(UART_DEB_SpiUartGetRxBufferSize() < MIN_UART_BYTES) {return;} data = UART_DEB_UartGetChar(); if(data == 0){return;} if(data == CMD_CONNECT) { length = UART_DEB_UartGetChar(); /* length */ if(length == 0){return;} i = 0; do { data = UART_DEB_UartGetChar(); if(data != 0x00) { tagData[i++] = data; } }while(data != 0x0D); if(length != CONNECT_DATA_LENGTH) { printf("PSoC: Length is not equal to %d \r\n", CONNECT_DATA_LENGTH); return; } else { printf("PSoC: CONNECT command is Received. \r\n"); tagData[length] = (uint8)'\r'; tagData[length + 1] = (uint8)'\n'; tagData[length + 2] = 0; printf("%s", tagData); } /* Convert Hex string into byte array */ HexStringToByteArray(tagData, length); for(i = 0; i < CYBLE_GAP_BD_ADDR_SIZE; i++) { peerAddr.bdAddr[i] = tagData[i]; } peerAddr.type = tagData[i]; for(i = 0; i < SECURITY_KEY_LENGTH; i++) { securityKey[i] = tagData[CYBLE_GAP_BD_ADDR_SIZE + 1 + i]; } printf("PSoC: Connection initiated to device address: "); for(i = 0; i < CYBLE_GAP_BD_ADDR_SIZE; i++) { printf("%2.2x", peerAddr.bdAddr[CYBLE_GAP_BD_ADDR_SIZE - i - 1]); } printf("\r\n"); /* Initiate Connection */ apiResult = CyBle_GapcConnectDevice(&peerAddr); if(apiResult != CYBLE_ERROR_OK) { printf("PSoC: Error in Connection Inititation \r\n"); } } }