/********************************************************************* * @fn simpleBLEGATTDiscoveryEvent * * @brief Process GATT discovery event * * @return none */ static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg ) { attReadByTypeReq_t req; if ( simpleBLEDiscState == BLE_DISC_STATE_SVC ) { // Service found, store handles if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->msg.findByTypeValueRsp.numInfo > 0 ) { simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle; simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle; } // If procedure complete if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->hdr.status == bleProcedureComplete ) || ( pMsg->method == ATT_ERROR_RSP ) ) { if ( simpleBLESvcStartHdl != 0 ) { // Discover characteristic simpleBLEDiscState = BLE_DISC_STATE_CHAR; req.startHandle = simpleBLESvcStartHdl; req.endHandle = simpleBLESvcEndHdl; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID); req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID); GATT_ReadUsingCharUUID( simpleBLEConnHandle, &req, simpleBLETaskId ); } } } else if ( simpleBLEDiscState == BLE_DISC_STATE_CHAR ) { // Characteristic found, store handle if ( pMsg->method == ATT_READ_BY_TYPE_RSP && pMsg->msg.readByTypeRsp.numPairs > 0 ) { simpleBLECharHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0], pMsg->msg.readByTypeRsp.dataList[1] ); LCD_WRITE_STRING( "Simple Svc Found", HAL_LCD_LINE_1 ); simpleBLEProcedureInProgress = FALSE; } simpleBLEDiscState = BLE_DISC_STATE_IDLE; } }
/********************************************************************* * @fn simpleBLEGATTDiscoveryEvent * * @brief Process GATT discovery event * * @return none */ static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg ) { attReadByTypeReq_t req; if ( simpleBLEDiscState == BLE_DISC_STATE_SVC ) { // Service found, store handles if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->msg.findByTypeValueRsp.numInfo > 0 ) { simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle; simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle; app_write_string("find service start is:"); app_write_string(uint16_to_string(simpleBLESvcStartHdl)); app_write_string("find service end is:"); app_write_string(uint16_to_string(simpleBLESvcEndHdl)); } // If procedure complete if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->hdr.status == bleProcedureComplete ) || ( pMsg->method == ATT_ERROR_RSP ) ) { if ( simpleBLESvcStartHdl != 0 ) { // Discover characteristic simpleBLEDiscState = BLE_DISC_STATE_CHAR; req.startHandle = simpleBLESvcStartHdl; req.endHandle = simpleBLESvcEndHdl; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(PARAM_BLE_CAR_IN_CHAR_UUID); req.type.uuid[1] = HI_UINT16(PARAM_BLE_CAR_IN_CHAR_UUID); GATT_ReadUsingCharUUID( simpleBLEConnHandle, &req, simpleBLETaskId ); app_write_string("start to read charac.\r\n"); } /****wkxboot***/ else { app_write_string("ATT_ERROR_RSP.\r\n"); } } } else if ( simpleBLEDiscState == BLE_DISC_STATE_CHAR ) { // Characteristic found, store handle if ( pMsg->method == ATT_READ_BY_TYPE_RSP && pMsg->msg.readByTypeRsp.numPairs > 0 ) { simpleBLECharHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0], pMsg->msg.readByTypeRsp.dataList[1] ); app_write_string("Simple Svc Found.char handle is:\r\n"); app_write_string(uint16_to_string(simpleBLECharHdl)); /***wkxboot***/ //LCD_WRITE_STRING( "Simple Svc Found", HAL_LCD_LINE_1 ); simpleBLEProcedureInProgress = FALSE; } /***wkxboot***/ simpleBLEDiscState = BLE_DISC_STATE_IDLE; /***wkxboot***/ } }
/********************************************************************* * @fn simpleTopology_processGATTDiscEvent * * @brief Process GATT discovery event * * @return none */ static void simpleTopology_processGATTDiscEvent(gattMsgEvent_t *pMsg) { if (pMsg->method == ATT_MTU_UPDATED_EVENT) { // MTU size updated LCD_WRITE_STRING_VALUE("MTU Size:", pMsg->msg.mtuEvt.MTU, 10, LCD_PAGE4); } else if (discState == BLE_DISC_STATE_MTU) { // MTU size response received, discover simple BLE service if (pMsg->method == ATT_EXCHANGE_MTU_RSP) { uint8_t uuid[ATT_BT_UUID_SIZE] = { LO_UINT16(SIMPLEPROFILE_SERV_UUID), HI_UINT16(SIMPLEPROFILE_SERV_UUID) }; discState = BLE_DISC_STATE_SVC; // Discovery simple BLE service VOID GATT_DiscPrimaryServiceByUUID(connHandle, uuid, ATT_BT_UUID_SIZE, selfEntity); } } else if (discState == BLE_DISC_STATE_SVC) { // Service found, store handles if (pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->msg.findByTypeValueRsp.numInfo > 0) { svcStartHdl = ATT_ATTR_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0); svcEndHdl = ATT_GRP_END_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0); } // If procedure complete if (((pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP) && (pMsg->hdr.status == bleProcedureComplete)) || (pMsg->method == ATT_ERROR_RSP)) { if (svcStartHdl != 0) { attReadByTypeReq_t req; // Discover characteristic discState = BLE_DISC_STATE_CHAR; req.startHandle = svcStartHdl; req.endHandle = svcEndHdl; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID); req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID); VOID GATT_ReadUsingCharUUID(connHandle, &req, selfEntity); } } } else if (discState == BLE_DISC_STATE_CHAR) { // Characteristic found, store handle if ((pMsg->method == ATT_READ_BY_TYPE_RSP) && (pMsg->msg.readByTypeRsp.numPairs > 0)) { //find index to store handle uint8_t connIndex = gapRoleInfo_Find(connHandle); charHdl[connIndex] = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[0], pMsg->msg.readByTypeRsp.pDataList[1]); LCD_WRITE_STRING("Simple Svc Found", LCD_PAGE6); } discState = BLE_DISC_STATE_IDLE; } }
/************************************************************************************************ * @fn simpleBLECentralProcessGATTMsg * * @brief Process GATT messages * * @return none ************************************************************************************************/ void simpleBLECentralProcessGATTMsg( gattMsgEvent_t *pMsg ){ switch(pMsg->method){ case ATT_READ_RSP:{ uint8 valueRead = pMsg->msg.readRsp.value[0]; LCDPrintText("Read rsp:",valueRead,PRINT_VALUE); //GAPCentralRole_TerminateLink( connHandle ); break; } case ATT_WRITE_RSP:{ if(CharSendingFlag ==1){ //to filt out the last rep message! Z_DelayMS(50); ClientWriteValue(); } break; } case ATT_FIND_BY_TYPE_VALUE_RSP:{ if(pMsg->msg.findByTypeValueRsp.numInfo > 0 ){ //LCDPrintText("Found group h",0,PRINT_STRING); HandleRangeStart = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle; //Found attribute handle HandleRangeEnd = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle; //Group end handle } if(pMsg->hdr.status == bleProcedureComplete) if( HandleRangeEnd != 0 ){ attReadByTypeReq_t req; req.startHandle = HandleRangeStart; req.endHandle = HandleRangeEnd; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(BLEChar.uuid); req.type.uuid[1] = HI_UINT16(BLEChar.uuid); uint8 return_status; //LCDPrintText("seach attrib h",0,PRINT_STRING); if(return_status = GATT_ReadUsingCharUUID(CurrentConnectionInfo.Handle, &req, MasterSlaveSwitchTaskID ))// Discover characteristic LCDPrintText("ReadUsingChar error",return_status,PRINT_VALUE); } else LCDPrintText("Handle range error",1,PRINT_STRING); break; } case ATT_READ_BY_TYPE_RSP:{ if(pMsg->msg.readByTypeRsp.numPairs > 0 ){ uint16 temp_handle = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0], pMsg->msg.readByTypeRsp.dataList[1] ); if((temp_handle == BLEChar.handle) && (BLEChar.handle != 0)) {CharHandleSearchFlag = 0; //HalLcdWriteStringValue("HANDLE SA",temp_handle,10,HAL_LCD_LINE_5); } else {BLEChar.handle= temp_handle; //HalLcdWriteStringValue("HANDLE DE",temp_handle,10,HAL_LCD_LINE_6); } } if(pMsg->hdr.status == bleProcedureComplete) if(BLEChar.handle != 0){ ClientWriteValue(); CharSendingFlag =1; //LCDPrintText("finished attrib h",0,PRINT_STRING); } else LCDPrintText("Handle value error",1,PRINT_STRING); break; } case ATT_HANDLE_VALUE_NOTI:{ //one transfer last 1s the most ,otherwise watchdog will reset the device //WD_KICK(); if(pMsg->msg.handleValueNoti.len != MAC_LEN){ LCDPrintText("error noti lenth:",pMsg->msg.handleValueNoti.len,PRINT_VALUE); break; } osal_memcpy(mac_buffer[Buffer_Top],pMsg->msg.handleValueNoti.value,pMsg->msg.handleValueNoti.len); if(TRUE == osal_memcmp(mac_buffer[Buffer_Top], mac_buffer[Buffer_Top+1] ,MAC_LEN)){ GAPCentralRole_TerminateLink( CurrentConnectionInfo.Handle ); uint32 timeout_value = 50; //osal_start_timerEx(MasterSlaveSwitchTaskID, CHECH_LINK_EVT, timeout_value); break; }//wait for conn timeout if(TRUE == MacBufferSearch(mac_buffer[Buffer_Top])){ //LCDPrintText("valid value:",mac_buffer[Buffer_Top][0],PRINT_VALUE); Buffer_Top++; } else osal_memset(mac_buffer[Buffer_Top], 0, MAC_LEN); break; } case ATT_ERROR_RSP:{ if(pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ){ uint8 status = pMsg->msg.errorRsp.errCode; LCDPrintText("Read Error",status,PRINT_VALUE); } else if(pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ){ uint8 status = pMsg->msg.errorRsp.errCode; LCDPrintText("Write Error",status,PRINT_VALUE); } else if(pMsg->msg.errorRsp.reqOpcode == ATT_FIND_BY_TYPE_VALUE_REQ) LCDPrintText("service find error",1,PRINT_STRING); else if(pMsg->msg.errorRsp.reqOpcode == ATT_READ_BY_TYPE_REQ) LCDPrintText("charac find error",1,PRINT_STRING); break; } default: LCDPrintText("unknow gatt message",1,PRINT_STRING); break; } }