Working Code
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Central_Light_Demo by
Diff: main.cpp
- Revision:
- 8:285ebd0e83fb
- Parent:
- 7:66586d2d7cb5
- Child:
- 9:b967a01810e1
--- a/main.cpp Fri Jan 20 17:59:26 2017 +0000 +++ b/main.cpp Wed Jan 25 08:08:20 2017 +0000 @@ -5,52 +5,42 @@ #include "hm_config.h" -static const uint16_t UNIT_1_25_MS = 1250; -static const uint16_t UNIT_10_MS = 10000; -#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) -#define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) -#define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS) -#define SLAVE_LATENCY 4 -#define SUPERVISION_TIMEOUT MSEC_TO_UNITS(6000, UNIT_10_MS) - -typedef unsigned char byte_char; - DiscoveredCharacteristic lightCharacteristic; - -static const Gap::ConnectionParams_t m_conn_params ={ - (uint16_t)MIN_CONNECTION_INTERVAL, - (uint16_t)MAX_CONNECTION_INTERVAL, - (uint16_t)SLAVE_LATENCY, - (uint16_t)SUPERVISION_TIMEOUT -}; - -GapScanningParams m_scan_params; +s_serviceInfo lightChar = {NULL,NULL,NULL,NULL}; Serial pc(USBTX, USBRX); -static ble_error_t error_status; + void bleint(); +bool serviceDiscover = true; +void readCharacteristics(); +void spiRead(); +void testaction(uint8_t options); +bool isint = true; +SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10); + +int spiRX[4]; + // uint8_t spiTX[4]; +uint8_t bufferSize; void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { uint8_t con_status =0; // uint8_t i; - Gap::GapState_t con_state; //0x51 for Magic light //0x5A - if (params->peerAddr[0] != 0x51) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ - return; - } - pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", + if ((params->peerAddr[0] == 0x51) || (params->peerAddr[0] == 0x5A)) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ + + pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type); pc.printf("Data Length : %d\r\n",params->advertisingDataLen); - - con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); - pc.printf("Connection Status : %d\r\n",con_status); - con_state = BLE::Instance().gap().getState(); - pc.printf("Connection state : %d\r\n",con_state); - - // } - BLE::Instance().gap().stopScan(); -} + con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); + pc.printf("Connection Status : %d\r\n",con_status); + connect_status = 1; + BLE::Instance().gap().stopScan(); + }else{ + printf("Not Matched your device\r\n"); + return; + } + } void serviceDiscoveryCallback(const DiscoveredService *service) { pc.printf("Service Discovery Callback\r\n"); @@ -68,20 +58,65 @@ void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); - uint8_t write_data []={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa}; - if (characteristicP->getUUID().getShortUUID() == 0xffe9) { /* !ALERT! Alter this filter to suit your device. */ - lightCharacteristic = *characteristicP; + + if (characteristicP->getUUID().getShortUUID() == 0xffe9 ) { /* !ALERT! Alter this filter to suit your device. */ + lightCharacteristic = *characteristicP; pc.printf("Matched char UUID\r\n"); - printf("Value Handle = %d\r\n",characteristicP->getDeclHandle()); - printf("Value Handle = %d and data size : %d\r\n",characteristicP->getValueHandle(),sizeof(write_data)); - lightCharacteristic.writeWoResponse(sizeof(write_data), write_data); - //error_status = characteristicP->write(sizeof(write_data), write_data); - printf("Write Status : %d\r\n",error_status); + printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); + printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); + // lightCharacteristic.writeWoResponse(sizeof(write_data), write_data); + charDiscover = 1; + serviceDiscover = true; + AddCharNodes(lightCharacteristic); + //light_actions(1); + + //BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,characteristicP->getConnectionHandle(),characteristicP->getValueHandle(),sizeof(write_data),write_data); + + }else { + printf("Not Matched char UUID\r\n"); } } +void AddCharNodes(DiscoveredCharacteristic light_Characteristic) +{ + printf("Add Char Nodes\r\n"); + lightCharacteristic_t *ptr_temp_char = NULL; + + printf("\n\rAdding Node..."); + + ptr_temp_char = lightChar.services_Char[0]; + + if(NULL == ptr_temp_char) + { + ptr_temp_char = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t)); + lightChar.services_Char[0] = ptr_temp_char; + } + else + { + while(NULL != ptr_temp_char->nextChar) + { + ptr_temp_char = ptr_temp_char->nextChar; + } + + /* add a new node */ + ptr_temp_char->nextChar = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t)); + ptr_temp_char = ptr_temp_char->nextChar; + } + + /* assign data to the node */ + ptr_temp_char->u_characteristicID = 1; + printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); + printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); + ptr_temp_char->connHandle = light_Characteristic.getConnectionHandle(); + ptr_temp_char->valueHandle = light_Characteristic.getValueHandle(); + printf("After Conn Handle = %d\r\n",ptr_temp_char->connHandle); + printf("After Value Handle = %d\r\n",ptr_temp_char->valueHandle); + ptr_temp_char->nextChar = NULL; +} + void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { pc.printf("terminated SD for handle %u\r\n", connectionHandle); + serviceDiscover = false; } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { @@ -95,6 +130,7 @@ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { pc.printf("disconnected\r\n"); + BLE::Instance().gap().startScan(advertisementCallback); } @@ -104,29 +140,189 @@ void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; - + pc.printf("Ble Init\n\r"); - /* Ensure that it is the default instance of BLE */ + /* Ensure that it is the default instance of BLE if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; - } + }*/ - ble.gap().onConnection(connectionCallback); - ble.gap().onDisconnection(disconnectionCallback); + // ble.gap().onConnection(connectionCallback); +// ble.gap().onDisconnection(disconnectionCallback); - ble.gap().setScanParams(1000, 800); - ble.gap().startScan(advertisementCallback); + // ble.gap().setScanParams(1000, 800); + // ble.gap().startScan(advertisementCallback); +} + +void light_actions(int service_char[]){ + uint8_t *write_data=NULL; + //={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa}; + write_data = new uint8_t[7]; + lightCharacteristic_t *ptr_tmp = NULL; + // uint8_t op; + ptr_tmp = lightChar.services_Char[service_char[0]-1]; + printf("Conn Handle = %d\r\n",ptr_tmp->connHandle); + printf("Value Handle = %d\r\n",ptr_tmp->valueHandle); + if(connect_status) + { + //scanf("%d", &op); + printf("options : %d\n",service_char[1]); + switch(service_char[1]) + { + case TURN_OFF: // off + write_data[0]=0x56; + write_data[1]=0x00; + write_data[2]=0x00; + write_data[3]=0x00; + write_data[4]=0x00; + write_data[5]=0xf0; + write_data[6]=0xaa; + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); + break; + + case TURN_ON: //on + write_data[0]=0x56; + write_data[1]=0xff; + write_data[2]=0x00; + write_data[3]=0xff; + write_data[4]=0x00; + write_data[5]=0xf0; + write_data[6]=0xaa; + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); + break; + + case COLOR: // Color + write_data[0]=0x56; + write_data[1]=0xff; //lightColor[service_char[2]][0];//0xff + write_data[2]=0x00;//lightColor[service_char[2]][1];//0x00; + write_data[3]=0x00;//lightColor[service_char[2]][2];//0x00; + write_data[4]=0x00; + write_data[5]=0xf0; + write_data[6]=0xaa; + //datancpy(pre_write_data, (char *)write_data,6); + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); + break; + + default: + printf("Invalid Options\r\n"); + + } + printf("Write Status : %d\r\n",error_status); + delete [] write_data; + } +} + +void readCharacteristics(){ + printf("Read Characteristics \r\n"); + lightCharacteristic_t *ptr_tmp = NULL; + ptr_tmp = lightChar.services_Char[1]; + uint16_t read_data; + error_status = BLE::Instance().gattClient().read(ptr_tmp->connHandle,ptr_tmp->valueHandle,read_data); + printf("Read Data : %d\r\n",read_data); +} + +void datancpy(char *pre_write, char *writeData, int data_size){ + int numCount; + printf("length %d\r\n",sizeof(writeData)); + for(numCount=0; numCount <= data_size; numCount++){ + pre_write[numCount] = writeData[numCount]; + // printf("write Value = %02x\r\n",writeData[numCount]); + //printf("Copy Value = %2x %d \r\n",pre_write[numCount],pre_write[numCount]); + } + } -int main(void) { +void bleint() +{ + printf("Init\r\n"); + if(isint){ + BLE::Instance().init(bleInitComplete); + isint=false; + } + +} + + +int main() +{ + // Serial port configuration pc.baud(9600); wait(8.0); + spiSlave.reply(191); pc.printf("Start\n\r"); BLE &ble = BLE::Instance(); - - ble.init(bleInitComplete); - - while (true) { - ble.waitForEvent(); - } + //ble.init(bleInitComplete); + //bleint(); + + //ble.waitForEvent(); + while(true) { + printf("loop\r\n"); + printf("Status : %d\r\n",BLE::Instance().gattClient().isServiceDiscoveryActive()); + + // if(connect_status && charDiscover){ + if(spiSlave.receive()) + { + spiRX[bufferSize] = spiSlave.read(); + spiSlave.reply(spiRX[bufferSize]); + bufferSize++; + if(bufferSize>=4){ + bufferSize=0; + + // light_actions(spiRX); + + } + } + bleint(); + // } + if (serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){ + ble.waitForEvent(); + //serviceDiscover = false; + } + } } +/* +void testaction(uint8_t options) +{ + int tx_buf[4]; + + switch(options) + { + case 1: //off + tx_buf[0] = 0x1; + tx_buf[1] = 0x0; + tx_buf[2] = 0x0; + tx_buf[3] = 0x0; + light_actions(tx_buf); + break; + case 2: //on + tx_buf[0] = 0x1; + tx_buf[1] = 0x1; + tx_buf[2] = 0x0; + tx_buf[3] = 0x0; + light_actions(tx_buf); + break; + case 3: //Red + tx_buf[0] = 0x1; + tx_buf[1] = 0x2; + tx_buf[2] = 0x0; + tx_buf[3] = 0x0; + light_actions(tx_buf); + break; + case 4: //Green + tx_buf[0] = 0x1; + tx_buf[1] = 0x2; + tx_buf[2] = 0x1; + tx_buf[3] = 0x0; + light_actions(tx_buf); + break; + case 5: //Blue + tx_buf[0] = 0x1; + tx_buf[1] = 0x2; + tx_buf[2] = 0x2; + tx_buf[3] = 0x0; + light_actions(tx_buf); + break; */ + /* default: + printf("Invalid \r\n"); + } +}*/ \ No newline at end of file