Working Code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light_Demo by HM_IOT_Demo

Committer:
hmiot
Date:
Wed Feb 01 12:38:31 2017 +0000
Revision:
13:9b2a587ee7ac
Parent:
12:cd54180657ee
Child:
14:f84a9745c10a
Added restart condition for out of memory issues.

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