Working Code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light_Demo by HM_IOT_Demo

Committer:
hmiot
Date:
Wed Jan 25 14:41:05 2017 +0000
Revision:
9:b967a01810e1
Parent:
8:285ebd0e83fb
Child:
10:5e5fa5bf77b5
Updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmiot 7:66586d2d7cb5 1 /************************************************************************
hmiot 7:66586d2d7cb5 2 * *
hmiot 7:66586d2d7cb5 3 * *
hmiot 7:66586d2d7cb5 4 ************************************************************************/
hmiot 6:71e7a446ae6a 5
hmiot 7:66586d2d7cb5 6 #include "hm_config.h"
hmiot 9:b967a01810e1 7 //#include "rtos.h"
hmiot 7:66586d2d7cb5 8
hmiot 6:71e7a446ae6a 9 DiscoveredCharacteristic lightCharacteristic;
hmiot 8:285ebd0e83fb 10 s_serviceInfo lightChar = {NULL,NULL,NULL,NULL};
hmiot 6:71e7a446ae6a 11 Serial pc(USBTX, USBRX);
hmiot 6:71e7a446ae6a 12
hmiot 9:b967a01810e1 13 void bleint();
hmiot 9:b967a01810e1 14 void SPI_Slave_read();
hmiot 9:b967a01810e1 15
hmiot 8:285ebd0e83fb 16 SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10);
hmiot 8:285ebd0e83fb 17
hmiot 9:b967a01810e1 18 bool serviceDiscover = true;
hmiot 8:285ebd0e83fb 19 int spiRX[4];
hmiot 8:285ebd0e83fb 20 // uint8_t spiTX[4];
hmiot 8:285ebd0e83fb 21 uint8_t bufferSize;
mbed_tw_hoehoe 3:d6f80e11a7f4 22
hmiot 6:71e7a446ae6a 23 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 24 uint8_t con_status =0;
hmiot 6:71e7a446ae6a 25 //0x51 for Magic light
hmiot 6:71e7a446ae6a 26 //0x5A
hmiot 9:b967a01810e1 27 if (params->peerAddr[0] == 0x51) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
hmiot 8:285ebd0e83fb 28
hmiot 8:285ebd0e83fb 29 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
mbed_tw_hoehoe 0:83b5c6efd8d7 30 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
mbed_tw_hoehoe 0:83b5c6efd8d7 31 params->rssi, params->isScanResponse, params->type);
hmiot 6:71e7a446ae6a 32 pc.printf("Data Length : %d\r\n",params->advertisingDataLen);
hmiot 8:285ebd0e83fb 33 con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL);
hmiot 8:285ebd0e83fb 34 pc.printf("Connection Status : %d\r\n",con_status);
hmiot 8:285ebd0e83fb 35 connect_status = 1;
hmiot 8:285ebd0e83fb 36 BLE::Instance().gap().stopScan();
hmiot 9:b967a01810e1 37
hmiot 8:285ebd0e83fb 38 }else{
hmiot 8:285ebd0e83fb 39 printf("Not Matched your device\r\n");
hmiot 9:b967a01810e1 40 return;
hmiot 8:285ebd0e83fb 41 }
hmiot 8:285ebd0e83fb 42 }
mbed_tw_hoehoe 0:83b5c6efd8d7 43
mbed_tw_hoehoe 0:83b5c6efd8d7 44 void serviceDiscoveryCallback(const DiscoveredService *service) {
hmiot 6:71e7a446ae6a 45 pc.printf("Service Discovery Callback\r\n");
hmiot 6:71e7a446ae6a 46 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
hmiot 6:71e7a446ae6a 47 pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 48 } else {
hmiot 6:71e7a446ae6a 49 pc.printf("S UUID-");
hmiot 6:71e7a446ae6a 50 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
hmiot 6:71e7a446ae6a 51 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
hmiot 6:71e7a446ae6a 52 printf("%02x", longUUIDBytes[i]);
hmiot 6:71e7a446ae6a 53 }
hmiot 6:71e7a446ae6a 54 pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 55 }
mbed_tw_hoehoe 0:83b5c6efd8d7 56 }
mbed_tw_hoehoe 0:83b5c6efd8d7 57
mbed_tw_hoehoe 0:83b5c6efd8d7 58 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
hmiot 6:71e7a446ae6a 59 pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
hmiot 8:285ebd0e83fb 60
hmiot 9:b967a01810e1 61 if (characteristicP->getUUID().getShortUUID() == 0xffe9) { /* !ALERT! Alter this filter to suit your device. */
hmiot 8:285ebd0e83fb 62 lightCharacteristic = *characteristicP;
hmiot 6:71e7a446ae6a 63 pc.printf("Matched char UUID\r\n");
hmiot 8:285ebd0e83fb 64 printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle());
hmiot 8:285ebd0e83fb 65 printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle());
hmiot 9:b967a01810e1 66 charDiscover = 1;
hmiot 9:b967a01810e1 67 serviceDiscover = true;
hmiot 9:b967a01810e1 68 AddCharNodes(lightCharacteristic);
hmiot 9:b967a01810e1 69 }else {
hmiot 8:285ebd0e83fb 70 printf("Not Matched char UUID\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 71 }
mbed_tw_hoehoe 0:83b5c6efd8d7 72 }
mbed_tw_hoehoe 0:83b5c6efd8d7 73
hmiot 8:285ebd0e83fb 74 void AddCharNodes(DiscoveredCharacteristic light_Characteristic)
hmiot 8:285ebd0e83fb 75 {
hmiot 8:285ebd0e83fb 76 printf("Add Char Nodes\r\n");
hmiot 8:285ebd0e83fb 77 lightCharacteristic_t *ptr_temp_char = NULL;
hmiot 8:285ebd0e83fb 78 ptr_temp_char = lightChar.services_Char[0];
hmiot 8:285ebd0e83fb 79
hmiot 8:285ebd0e83fb 80 if(NULL == ptr_temp_char)
hmiot 8:285ebd0e83fb 81 {
hmiot 8:285ebd0e83fb 82 ptr_temp_char = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t));
hmiot 8:285ebd0e83fb 83 lightChar.services_Char[0] = ptr_temp_char;
hmiot 8:285ebd0e83fb 84 }
hmiot 8:285ebd0e83fb 85 else
hmiot 8:285ebd0e83fb 86 {
hmiot 8:285ebd0e83fb 87 while(NULL != ptr_temp_char->nextChar)
hmiot 8:285ebd0e83fb 88 {
hmiot 8:285ebd0e83fb 89 ptr_temp_char = ptr_temp_char->nextChar;
hmiot 8:285ebd0e83fb 90 }
hmiot 8:285ebd0e83fb 91
hmiot 8:285ebd0e83fb 92 /* add a new node */
hmiot 8:285ebd0e83fb 93 ptr_temp_char->nextChar = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t));
hmiot 8:285ebd0e83fb 94 ptr_temp_char = ptr_temp_char->nextChar;
hmiot 8:285ebd0e83fb 95 }
hmiot 8:285ebd0e83fb 96
hmiot 8:285ebd0e83fb 97 /* assign data to the node */
hmiot 8:285ebd0e83fb 98 ptr_temp_char->u_characteristicID = 1;
hmiot 8:285ebd0e83fb 99 printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle());
hmiot 8:285ebd0e83fb 100 printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle());
hmiot 8:285ebd0e83fb 101 ptr_temp_char->connHandle = light_Characteristic.getConnectionHandle();
hmiot 8:285ebd0e83fb 102 ptr_temp_char->valueHandle = light_Characteristic.getValueHandle();
hmiot 8:285ebd0e83fb 103 ptr_temp_char->nextChar = NULL;
hmiot 8:285ebd0e83fb 104 }
hmiot 8:285ebd0e83fb 105
mbed_tw_hoehoe 0:83b5c6efd8d7 106 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 107 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
hmiot 8:285ebd0e83fb 108 serviceDiscover = false;
mbed_tw_hoehoe 0:83b5c6efd8d7 109 }
mbed_tw_hoehoe 0:83b5c6efd8d7 110
hmiot 6:71e7a446ae6a 111 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 112 pc.printf("Connection Callback\n\r");
hmiot 6:71e7a446ae6a 113 if (params->role == Gap::CENTRAL) {
hmiot 6:71e7a446ae6a 114 pc.printf("Service and characterstics discovery callback\r\n");
hmiot 6:71e7a446ae6a 115 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
hmiot 6:71e7a446ae6a 116 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9);
mbed_tw_hoehoe 0:83b5c6efd8d7 117 }
hmiot 6:71e7a446ae6a 118 }
hmiot 6:71e7a446ae6a 119
hmiot 6:71e7a446ae6a 120 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 121 pc.printf("disconnected\r\n");
hmiot 8:285ebd0e83fb 122 BLE::Instance().gap().startScan(advertisementCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 123 }
mbed_tw_hoehoe 0:83b5c6efd8d7 124
hmiot 6:71e7a446ae6a 125
hmiot 6:71e7a446ae6a 126 /**
hmiot 6:71e7a446ae6a 127 * Callback triggered when the ble initialization process has finished
hmiot 6:71e7a446ae6a 128 */
hmiot 6:71e7a446ae6a 129 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
hmiot 6:71e7a446ae6a 130 {
hmiot 6:71e7a446ae6a 131 BLE& ble = params->ble;
hmiot 8:285ebd0e83fb 132
hmiot 6:71e7a446ae6a 133 pc.printf("Ble Init\n\r");
hmiot 9:b967a01810e1 134 /* Ensure that it is the default instance of BLE */
hmiot 6:71e7a446ae6a 135 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
hmiot 6:71e7a446ae6a 136 return;
hmiot 9:b967a01810e1 137 }
hmiot 6:71e7a446ae6a 138
hmiot 9:b967a01810e1 139 ble.gap().onConnection(connectionCallback);
hmiot 9:b967a01810e1 140 ble.gap().onDisconnection(disconnectionCallback);
mbed_tw_hoehoe 3:d6f80e11a7f4 141
hmiot 9:b967a01810e1 142 ble.gap().setScanParams(1000, 800);
hmiot 9:b967a01810e1 143 ble.gap().startScan(advertisementCallback);
hmiot 8:285ebd0e83fb 144 }
hmiot 8:285ebd0e83fb 145
hmiot 8:285ebd0e83fb 146 void light_actions(int service_char[]){
hmiot 8:285ebd0e83fb 147 uint8_t *write_data=NULL;
hmiot 8:285ebd0e83fb 148 //={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa};
hmiot 8:285ebd0e83fb 149 write_data = new uint8_t[7];
hmiot 8:285ebd0e83fb 150 lightCharacteristic_t *ptr_tmp = NULL;
hmiot 9:b967a01810e1 151 ptr_tmp = lightChar.services_Char[service_char[1]-1];
hmiot 9:b967a01810e1 152 // printf("Conn Handle = %d\r\n",ptr_tmp->connHandle);
hmiot 9:b967a01810e1 153 // printf("Value Handle = %d\r\n",ptr_tmp->valueHandle);
hmiot 9:b967a01810e1 154 if(connect_status)
hmiot 8:285ebd0e83fb 155 {
hmiot 9:b967a01810e1 156
hmiot 9:b967a01810e1 157 switch(service_char[2])
hmiot 8:285ebd0e83fb 158 {
hmiot 8:285ebd0e83fb 159 case TURN_OFF: // off
hmiot 8:285ebd0e83fb 160 write_data[0]=0x56;
hmiot 8:285ebd0e83fb 161 write_data[1]=0x00;
hmiot 8:285ebd0e83fb 162 write_data[2]=0x00;
hmiot 8:285ebd0e83fb 163 write_data[3]=0x00;
hmiot 8:285ebd0e83fb 164 write_data[4]=0x00;
hmiot 8:285ebd0e83fb 165 write_data[5]=0xf0;
hmiot 8:285ebd0e83fb 166 write_data[6]=0xaa;
hmiot 8:285ebd0e83fb 167 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data);
hmiot 8:285ebd0e83fb 168 break;
hmiot 8:285ebd0e83fb 169
hmiot 8:285ebd0e83fb 170 case TURN_ON: //on
hmiot 8:285ebd0e83fb 171 write_data[0]=0x56;
hmiot 8:285ebd0e83fb 172 write_data[1]=0xff;
hmiot 9:b967a01810e1 173 write_data[2]=0xff;
hmiot 8:285ebd0e83fb 174 write_data[3]=0xff;
hmiot 8:285ebd0e83fb 175 write_data[4]=0x00;
hmiot 8:285ebd0e83fb 176 write_data[5]=0xf0;
hmiot 8:285ebd0e83fb 177 write_data[6]=0xaa;
hmiot 8:285ebd0e83fb 178 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data);
hmiot 8:285ebd0e83fb 179 break;
hmiot 8:285ebd0e83fb 180
hmiot 8:285ebd0e83fb 181 case COLOR: // Color
hmiot 8:285ebd0e83fb 182 write_data[0]=0x56;
hmiot 9:b967a01810e1 183 write_data[1]=lightColor[service_char[3]][0];//0xff
hmiot 9:b967a01810e1 184 write_data[2]=lightColor[service_char[3]][1];//0x00;
hmiot 9:b967a01810e1 185 write_data[3]=lightColor[service_char[3]][2];//0x00;
hmiot 8:285ebd0e83fb 186 write_data[4]=0x00;
hmiot 8:285ebd0e83fb 187 write_data[5]=0xf0;
hmiot 8:285ebd0e83fb 188 write_data[6]=0xaa;
hmiot 8:285ebd0e83fb 189 //datancpy(pre_write_data, (char *)write_data,6);
hmiot 8:285ebd0e83fb 190 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data);
hmiot 8:285ebd0e83fb 191 break;
hmiot 8:285ebd0e83fb 192
hmiot 8:285ebd0e83fb 193 default:
hmiot 9:b967a01810e1 194 printf("Invalid Options\r\n");
hmiot 8:285ebd0e83fb 195 }
hmiot 8:285ebd0e83fb 196 delete [] write_data;
hmiot 9:b967a01810e1 197 }
hmiot 8:285ebd0e83fb 198 }
hmiot 8:285ebd0e83fb 199
hmiot 8:285ebd0e83fb 200 void datancpy(char *pre_write, char *writeData, int data_size){
hmiot 8:285ebd0e83fb 201 int numCount;
hmiot 8:285ebd0e83fb 202 for(numCount=0; numCount <= data_size; numCount++){
hmiot 8:285ebd0e83fb 203 pre_write[numCount] = writeData[numCount];
hmiot 8:285ebd0e83fb 204 }
hmiot 8:285ebd0e83fb 205
mbed_tw_hoehoe 2:4b53d13d9851 206 }
mbed_tw_hoehoe 2:4b53d13d9851 207
hmiot 8:285ebd0e83fb 208 void bleint()
hmiot 8:285ebd0e83fb 209 {
hmiot 8:285ebd0e83fb 210 printf("Init\r\n");
hmiot 9:b967a01810e1 211 BLE &ble = BLE::Instance();
hmiot 8:285ebd0e83fb 212 BLE::Instance().init(bleInitComplete);
hmiot 8:285ebd0e83fb 213 }
hmiot 8:285ebd0e83fb 214
hmiot 8:285ebd0e83fb 215
hmiot 8:285ebd0e83fb 216 int main()
hmiot 8:285ebd0e83fb 217 {
hmiot 8:285ebd0e83fb 218 // Serial port configuration
mbed_tw_hoehoe 1:2f1203d70643 219 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 220 wait(8.0);
hmiot 9:b967a01810e1 221 pc.printf("Start\n\r");
hmiot 8:285ebd0e83fb 222 spiSlave.reply(191);
hmiot 9:b967a01810e1 223 // Initialize the Ble
hmiot 9:b967a01810e1 224 bleint();
hmiot 9:b967a01810e1 225 while(1){
hmiot 9:b967a01810e1 226 //printf("loop\r\n");
hmiot 9:b967a01810e1 227 if (serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){
hmiot 9:b967a01810e1 228 BLE::Instance().waitForEvent();
hmiot 9:b967a01810e1 229
hmiot 9:b967a01810e1 230 }else{
hmiot 9:b967a01810e1 231 // printf("Slave Read loop\r\n");
hmiot 9:b967a01810e1 232 SPI_Slave_read();
hmiot 8:285ebd0e83fb 233 }
hmiot 8:285ebd0e83fb 234
hmiot 9:b967a01810e1 235 }
hmiot 9:b967a01810e1 236
hmiot 9:b967a01810e1 237 }
hmiot 9:b967a01810e1 238
hmiot 9:b967a01810e1 239 void SPI_Slave_read(){
hmiot 9:b967a01810e1 240
hmiot 9:b967a01810e1 241 wait_us(2);
hmiot 9:b967a01810e1 242 if(spiSlave.receive()){
hmiot 9:b967a01810e1 243 // printf("Slave Read loop1\r\n");
hmiot 9:b967a01810e1 244 spiRX[bufferSize] = spiSlave.read();
hmiot 9:b967a01810e1 245 spiSlave.reply(spiRX[bufferSize]);
hmiot 9:b967a01810e1 246 bufferSize++;
hmiot 9:b967a01810e1 247 if(bufferSize>=4){
hmiot 9:b967a01810e1 248 bufferSize=0;
hmiot 9:b967a01810e1 249 light_actions(spiRX);
hmiot 9:b967a01810e1 250 // printf("Receive\n\r");
hmiot 8:285ebd0e83fb 251 }
hmiot 9:b967a01810e1 252 }
hmiot 9:b967a01810e1 253 }