Fixed ble issue
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Central_Light_Demo_23 by
Revision 18:ef42e2579463, committed 2017-11-08
- Comitter:
- hmiot
- Date:
- Wed Nov 08 12:07:38 2017 +0000
- Parent:
- 17:0b0a7dccf151
- Commit message:
- Updated for Zigo Demo
Changed in this revision
Light_Header/hm_config.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0b0a7dccf151 -r ef42e2579463 Light_Header/hm_config.h --- a/Light_Header/hm_config.h Mon Oct 09 09:39:46 2017 +0000 +++ b/Light_Header/hm_config.h Wed Nov 08 12:07:38 2017 +0000 @@ -11,24 +11,39 @@ #define MAX_SERVICES 4 //class Magic_Light{ +bool write_charDiscover = false; +bool notify_charDiscover = false; +bool notify_write = true; + +bool startFlag = false; +bool endFlag = false; +bool respFlag = false; + static bool connect_status; bool charDiscover; uint8_t pre_write_data[16]; static ble_error_t error_status; bool serviceDiscover = true; bool first_on=true; -int spiRX[8]; +int spiRX[16]; uint8_t bufferSize; uint8_t write_data[16]; //[8] = {0x00,};//={0x56,0x00,0xff,0x00,0x00,0xf0,0xaa}; +int deviceInfo[16] = {0x05,0xCE, 0x01, 0x03, 0xEF, 0x01, 0x77 }; +uint8_t response_data[16]; +int resp_BuffSize = 0; void AddCharNodes(DiscoveredCharacteristic light_Characteristic); void light_actions(int service_char[]); +void write_notifyCharacterstics(); +void processSPIPacket(int SPIData[]); +void responsePacket(const uint8_t *paramsData, int length); //public: typedef enum light_action_e{ TURN_OFF=0, TURN_ON, COLOR, + NOTIFY, INVAILD }light_action;
diff -r 0b0a7dccf151 -r ef42e2579463 main.cpp --- a/main.cpp Mon Oct 09 09:39:46 2017 +0000 +++ b/main.cpp Wed Nov 08 12:07:38 2017 +0000 @@ -6,6 +6,7 @@ #include "hm_config.h" DiscoveredCharacteristic lightCharacteristic; +DiscoveredCharacteristic notifyCharacteristic; static s_serviceInfo lightChar = {NULL,NULL,NULL,NULL}; Serial pc(USBTX, USBRX); void bleint(); @@ -60,11 +61,27 @@ printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); AddCharNodes(lightCharacteristic); - charDiscover = 1; - serviceDiscover = true; - }else { + write_charDiscover = true; + + } + else if(characteristicP->getUUID().getShortUUID() == 0xffe4) + { + notifyCharacteristic = *characteristicP; + notify_charDiscover = true; + pc.printf("Matched char UUID\r\n"); + printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), notifyCharacteristic.getConnectionHandle()); + printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),notifyCharacteristic.getValueHandle()); + + } + else { printf("Not Matched char UUID\r\n"); - } + } + + if(notify_charDiscover && write_charDiscover) + { + charDiscover = 1; + serviceDiscover = true; + } } void AddCharNodes(DiscoveredCharacteristic light_Characteristic) @@ -110,7 +127,7 @@ if (params->role == Gap::CENTRAL) { pc.printf("Service and characterstics discovery callback\r\n"); BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); - BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9); + BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xffe5, 0xffe9*/); // 0xffe5 --> Services UUID // 0xffe9 --> Characteristics UUID } @@ -121,6 +138,16 @@ BLE::Instance().gap().startScan(advertisementCallback); } +void hvxCallback(const GattHVXCallbackParams *params) { + printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); + for (unsigned index = 0; index < params->len; index++) { + printf(" %02x", params->data[index]); + } + strncpy((char *)response_data, (char *)params->data, params->len); + resp_BuffSize = params->len; + + printf("\r\n"); +} /** * Callback triggered when the ble initialization process has finished @@ -139,17 +166,18 @@ ble.gap().onDisconnection(disconnectionCallback); ble.gap().setScanParams(1000, 800); ble.gap().startScan(advertisementCallback); - + + ble.gattClient().onHVX(hvxCallback); } -void light_actions(int * service_char){ +void light_actions(int service_char[]){ lightCharacteristic_t *ptr_tmp = NULL; ptr_tmp = lightChar.services_Char[0]; uint8_t numCount; if(connect_status) { - switch(service_char[2]) + switch(service_char[1]) { case TURN_OFF: // off write_data[0]=0x56; @@ -178,9 +206,9 @@ case COLOR: // Color write_data[0]=0x56; - write_data[1]=lightColor[service_char[3]][0];//0xff - write_data[2]=lightColor[service_char[3]][1];//0x00; - write_data[3]=lightColor[service_char[3]][2];//0x00; + write_data[1]=lightColor[service_char[2]][0];//0xff + write_data[2]=lightColor[service_char[2]][1];//0x00; + write_data[3]=lightColor[service_char[2]][2];//0x00; write_data[4]=0x00; write_data[5]=0xf0; write_data[6]=0xaa; @@ -191,7 +219,16 @@ error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); BLE::Instance().processEvents(); break; - + + case NOTIFY: + //[0xEF, 0x01, 0x77] Notify Value + write_data[0]=0xEF; + write_data[1]=0x01; + write_data[2]=0x77; + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); + BLE::Instance().processEvents(); + break; + default: printf("Invalid Options\r\n"); } @@ -200,6 +237,21 @@ } +void write_notifyCharacterstics() +{ + printf("write_notifyCharacterstics \n\r"); + uint16_t value = BLE_HVX_NOTIFICATION; + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_REQ, + notifyCharacteristic.getConnectionHandle(), + notifyCharacteristic.getValueHandle(), /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */ + sizeof(uint16_t), /* HACK Alert! size should be made into a BLE_API constant. */ + reinterpret_cast<const uint8_t *>(&value)); + printf("Conn Handle = %d \r\n",notifyCharacteristic.getConnectionHandle()); + printf("Value Handle = %d \r\n",notifyCharacteristic.getValueHandle()); + + printf("write_notifyCharacterstics %d \n\r", error_status); + BLE::Instance().processEvents(); +} void datancpy(char pre_write[], char writeData[], int data_size){ int numCount; for(numCount=0; numCount <= data_size; numCount++){ @@ -226,39 +278,60 @@ statusWR=1; bleint(); bufferSize = 0; + int temp = 0; + //int index=0; + + //int temp = 0x00; + //int color = 0x00; + //int i=0; while(1){ //printf("loop\r\n"); if(!(connect_status && charDiscover)) waitBleEvent(); + if(charDiscover && connect_status && notify_write ) //&& notify_write + { + printf("Write Notify \n\r"); + write_notifyCharacterstics(); + notify_write = false; + + } + if(spiSlave.receive()){ // printf("Slave Read loop1\r\n"); - spiRX[bufferSize] = spiSlave.read(); - spiSlave.reply(spiRX[bufferSize]); - bufferSize++; + + temp = spiSlave.read(); - if(bufferSize >= 5){ - bufferSize=0; - light_actions(spiRX); - spiSlave.reply(spiRX[2]); //(0xAA); - /* - if(statusWR >=3) - { - wait_ms(500); - BLE::Instance().shutdown(); - statusWR =1; - connect_status =0; - charDiscover =0; - serviceDiscover = true; - bleint(); - } + if(temp == 0x86) + { + bufferSize = 0; + startFlag = true; + endFlag = false; + spiSlave.reply(0x86); - statusWR++; - */ + }else if(temp == 0x88) + { + endFlag= true; + spiSlave.reply(0x88); + + }else if(startFlag == true && endFlag == false) + { + spiRX[bufferSize] = temp; + spiSlave.reply(temp); + bufferSize++; } + if(startFlag && endFlag) + { + processSPIPacket(spiRX); + startFlag = false; + endFlag = false; + spiSlave.reply(0xAA); + } + + } - + //wait_ms(1000); } @@ -272,3 +345,27 @@ } } +void processSPIPacket(int SPIData[]) +{ + int start_index = 2; + int temp_buff[8]; + for(int k =0; k < SPIData[0]; k++, start_index++) + { + temp_buff[k] = SPIData[start_index]; + + } + switch(SPIData[1]) + { + case 0xCE: + { + light_actions(temp_buff); + } + break; + + default: + printf("Unknown data \r\n"); + break; + } + +} +