![](/media/cache/profiles/profile2.jpg.50x50_q85.jpg)
This software setup a central node of a star topology network
Dependencies: MQTT target_st_bluenrg
Fork of ble-star-mbed by
Diff: source/BleMasterService.cpp
- Revision:
- 1:110b5e896bc9
- Parent:
- 0:1902469bdd2d
- Child:
- 2:1525f4848cf2
--- a/source/BleMasterService.cpp Tue Feb 20 11:21:41 2018 +0000 +++ b/source/BleMasterService.cpp Mon Feb 26 10:10:37 2018 +0000 @@ -46,6 +46,14 @@ uint8_t notificationPending = 0; + +/* Flag to indicate if the ble stack is busy + * 1 busy + * 0 or anything else not busy */ +uint8_t stackBusy = 0; + + + /* Header pointer of the DiscoveredCharacteristic list */ DiscoveredCharacteristicNode * headCharacteristic[MAX_NUM_OF_NODES]; DiscoveredCharacteristicNode * tmp[MAX_NUM_OF_NODES]; @@ -204,7 +212,8 @@ if ( (perDevs.connDevices < MAX_NUM_OF_NODES) && (perDevs.discovery_enabled) ) { //printf("\r\nIF 1\n");//DEBUG /* Start discovery of new peripheral nodes and connect them */ - startDiscovery(); + //startDiscovery(); + eventQ.call(startDiscovery); } else { perDevs.status = DEVICE_CONNECTED; @@ -355,7 +364,7 @@ role = 0; - e1 = ble.gap().connect(perDevs.devInfo[index].bdaddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); + e1 = ble.gap().connect(perDevs.devInfo[index].bdaddr, BLEProtocol::AddressType::RANDOM_STATIC, NULL, NULL); if (e1 != BLE_ERROR_NONE){ @@ -419,9 +428,11 @@ readCharacteristicCallback(nullGattReadCallbackP(perDevs.connection_handle[i])); - - notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, - slaveDev.notification_data.attribute_handle); + if (writeDescriptorCompleted == 1){ + writeDescriptorCompleted =0; + notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, + slaveDev.notification_data.attribute_handle); + } perDevs.status = CONN_INIT; @@ -629,8 +640,6 @@ }//stopAdv #endif - - /* MASTER ROLE */ perDevs.status = START_DEVICE_DISCOVERY; printf("\r\nStart Device Discovery (%d)\n", perDevs.status); @@ -640,9 +649,9 @@ if ( e1 == BLE_ERROR_NONE ) { //printf("\r\nScan started correctly\n");//DEBUG }else { - printf("\r\nERROR starting scan (err: %d stat: %d)\n", e1, perDevs.status);//DEBUG - //doing something - return; + //printf("\r\nERROR starting scan (err: %d stat: %d)\n", e1, perDevs.status);//DEBUG + perDevs.status = DEVICE_DISCOVERY_COMPLETE; + discoveryCompleted = 1; } }//if-MAIN @@ -765,7 +774,7 @@ perDevs.status = DEVICE_FOUND; - printf("\r\nPeripheral %d inserted (%02x%02x%02x%02x%02x%02x) \n", perDevs.connDevices+1, + printf("\r\nPeripheral (MOTENV) %d inserted [%02x %02x %02x %02x %02x %02x] \n", perDevs.connDevices+1, tempAddr[5], tempAddr[4], tempAddr[3], tempAddr[2], tempAddr[1], tempAddr[0]); }//if-alreadyIn @@ -842,7 +851,7 @@ perDevs.status = DEVICE_FOUND; - printf("\r\nPeripheral %d inserted (%02x%02x%02x%02x%02x%02x) \n", perDevs.connDevices+1, + printf("\r\nPeripheral (FLIGHT) %d inserted [%02x %02x %02x %02x %02x %02x] \n", perDevs.connDevices+1, tempAddr[5], tempAddr[4], tempAddr[3], tempAddr[2], tempAddr[1], tempAddr[0]); }//if-alreadyIn @@ -926,7 +935,7 @@ perDevs.status = DEVICE_FOUND; - printf("\r\nPeripheral %d inserted (%02x%02x%02x%02x%02x%02x) \n", perDevs.connDevices+1, + printf("\r\nPeripheral (ALLMEMS) %d inserted [%02x %02x %02x %02x %02x %02x] \n", perDevs.connDevices+1, tempAddr[5], tempAddr[4], tempAddr[3], tempAddr[2], tempAddr[1], tempAddr[0]); }//if-alreadyIn @@ -1461,7 +1470,15 @@ if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify)) { perDevs.status = ALL_DATA_READ; - notifyMaster(ATTR_HEAD_LEN+WUP_DATA_LEN, star_attr_value, slave_attr_handle); + + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+WUP_DATA_LEN, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } + } @@ -1489,7 +1506,14 @@ if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify)) { perDevs.status = ALL_DATA_READ; - notifyMaster(ATTR_HEAD_LEN+MIC_DATA_LEN, star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + + writeDescriptorCompleted = 0; + notifyMaster(ATTR_HEAD_LEN+MIC_DATA_LEN, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } } @@ -1511,7 +1535,13 @@ if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify)) { perDevs.status = ALL_DATA_READ; - notifyMaster(ATTR_HEAD_LEN+PRX_DATA_LEN, star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+PRX_DATA_LEN, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } } @@ -1553,7 +1583,13 @@ if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify) && (perDevs.agm_on[index])) { perDevs.status = NOTIFICATIONS_DATA_READ; - notifyMaster(ATTR_HEAD_LEN+data_len, star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+data_len, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } } else { perDevs.status = ALL_DATA_READ; @@ -1583,7 +1619,13 @@ if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify) && (perDevs.sfusion_on[index])) { perDevs.status = NOTIFICATIONS_DATA_READ; - notifyMaster(ATTR_HEAD_LEN+SFUSION_DATA_LEN, star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+SFUSION_DATA_LEN, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } perDevs.sfusion_char_read[index] = 1; } else { @@ -1617,8 +1659,14 @@ Create_New_Attr_Value(attr_value, devAddr, PRESS_TYPE_ID, attr_value+TSTAMP_LEN, attr_len-TSTAMP_LEN); if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify)) { - notifyMaster(ATTR_HEAD_LEN+PRESS_DATA_LEN+(3*TYPE_ID_LEN)+HUM_DATA_LEN+(2*TEMP_DATA_LEN), - star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+PRESS_DATA_LEN+(3*TYPE_ID_LEN)+HUM_DATA_LEN+(2*TEMP_DATA_LEN), + star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } } @@ -1642,7 +1690,13 @@ if (perDevs.led_char_read[index]==1) { perDevs.status = ALL_DATA_READ; } - notifyMaster(ATTR_HEAD_LEN+LED_DATA_LEN, star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+LED_DATA_LEN, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } } @@ -1663,7 +1717,13 @@ Create_New_Attr_Value(attr_value, devAddr, LUX_TYPE_ID, attr_value+TSTAMP_LEN, LUX_DATA_LEN); if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify)) { - notifyMaster(ATTR_HEAD_LEN+LUX_DATA_LEN, star_attr_value, slave_attr_handle); + if ((stackBusy == 0) && (writeDescriptorCompleted ==1)){ + writeDescriptorCompleted=0; + notifyMaster(ATTR_HEAD_LEN+LUX_DATA_LEN, star_attr_value, slave_attr_handle); + }else { + //disableAllNotifications(); + return; + } } @@ -1721,7 +1781,7 @@ { event_type = "WUPEVT"; - // Enable/Disable the WakeUp notifications on the Motenv1 + //Enable/Disable the WakeUp notifications on the Motenv1 setNotificationProperty(conn_handle, index, FEATURE_MASK_WAKEUP_EVENTS, WAKEUP_NOTIFICATION_CMD, set_mode); attr_handle = perDevs.wup_char_handle[index] + 2; @@ -1756,9 +1816,10 @@ /* Method called after a periph descriptor is written */ void perDescriptorWrittenCallback(const GattWriteCallbackParams* event){ //printf("\r\nperDescriptorWrittenCallback\n");//DEBUG - - setNewStatus(); + writeDescriptorCompleted = 1; + eventQ.call(setNewStatus); + //writeDescriptorCompleted = 1; } /*----------------------------------------------------------------------------*/ @@ -2202,12 +2263,6 @@ tmp = 0; /* fake value used to just notify the prx presence */ sprintf((char *)wifi_data, "\"PRX_0x%02x%02x\":%ld", devAddr[NODE_ID_B2], devAddr[NODE_ID_B1], tmp); -// if (attr_value == NULL){ -// printf("\r\nattr_value == NULL\n"); -// }else{ -// printf("\r\nattr_value != NULL\n"); -// } - attribute_handle = slaveDev.star_data_char_handle; if ((slaveDev.is_connected) && (slaveDev.star_data_char_notify)) { uint8_t val[PRX_DATA_LEN]; @@ -2327,11 +2382,10 @@ if (notificationPending == 1){ readCompleted = 1; Change_Notification_Status(notifyP->att_data, notifyP->attr_value, notifyP->conn_handle, notifyP->i, notifyP->feature_mask, notifyP->frequency); - notificationPending = 0; + }else { + readCompleted = 1; + setNewStatus(); } - - setNewStatus(); - readCompleted = 1; } /*----------------------------------------------------------------------------*/ @@ -2407,6 +2461,44 @@ if ((perDevs.mic_event_enabled) || (perDevs.prx_event_enabled) || (perDevs.agm_event_enabled) || (perDevs.sfusion_event_enabled)){ //printf("\r\nNotification event enabled, skip reading\n");//DEBUG + + if (stackBusy == 1){ + uint8_t j; + uint16_t attr_handle; + uint8_t value_len; + uint8_t attr_value[2]; + char* feat = NULL; + + for (j=0; j<(perDevs.connDevices+perDevs.discDevices); j++) { + + if ((perDevs.prx_event[j]) && (perDevs.prx_event_enabled)) { + feat = "PRX"; + perDevs.prx_event_enabled = 0; + perDevs.prx_on[j] = 0; + attr_handle = perDevs.prx_char_handle[j] + 2; + + } else { + continue; + } + + value_len = 2; + attr_value[0] = DISABLE; + + + if (writeDescriptorCompleted == 1){ + writeDescriptorCompleted=0; + ble_error_t disableErr = writeCharacDescriptorWithError(perDevs.connection_handle[j], attr_handle, value_len, attr_value); + printf("\r\nSet OFF the %s notifications on node %d\n", feat, j); + + if (disableErr != BLE_ERROR_NONE){ + printf("\r\n(readingProcess) Write charac descriptor failed (err: %d, stat: %d)\n", disableErr, perDevs.status); + writeDescriptorCompleted=1; + }//if-error + } + + }//for + }//if-stackBusy + } else { perDevs.status = READING_ENVIRONMENTAL; } @@ -2427,7 +2519,7 @@ //ENVIRONMENTAL - if ((perDevs.status == READING_ENVIRONMENTAL) && (readCompleted == 1)) { + if ((perDevs.status == READING_ENVIRONMENTAL) && (readCompleted == 1) && (writeDescriptorCompleted ==1)) { //reading is starting readCompleted = 0; //fetch the characteristic declHandle @@ -2447,8 +2539,9 @@ //NOTIFY_ENV_TO_CLIENT - if ((perDevs.status == NOTIFY_ENV_TO_CLIENT) && (writeDescriptorCompleted ==1)) { + if ((perDevs.status == NOTIFY_ENV_TO_CLIENT) && (readCompleted == 1) && (writeDescriptorCompleted ==1)) { //printf("\r\nNOTIFY_ENV_TO_CLIENT\n");//DEBUG + writeDescriptorCompleted = 0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2461,7 +2554,7 @@ //READING_LED - if ((perDevs.status == READING_LED) && (readCompleted == 1)) { + if ((perDevs.status == READING_LED) && (readCompleted == 1) && (writeDescriptorCompleted ==1)) { //reading is starting readCompleted = 0; //fetch the characteristic declHandle @@ -2478,7 +2571,8 @@ //NOTIFY_LED_TO_CLIENT - if ((perDevs.status == NOTIFY_LED_TO_CLIENT) && (writeDescriptorCompleted ==1)) { + if ((perDevs.status == NOTIFY_LED_TO_CLIENT) && (readCompleted == 1) && (writeDescriptorCompleted ==1)) { + writeDescriptorCompleted = 0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2490,7 +2584,7 @@ //READING_LUX - if ((perDevs.status == READING_LUX) && (readCompleted == 1)) { + if ((perDevs.status == READING_LUX) && (readCompleted == 1) && (writeDescriptorCompleted ==1)) { //reading is starting readCompleted = 0; //fetch the characteristic declHandle @@ -2507,7 +2601,8 @@ //NOTIFY_LUX_TO_CLIENT - if ((perDevs.status == NOTIFY_LUX_TO_CLIENT) && (writeDescriptorCompleted ==1)) { + if ((perDevs.status == NOTIFY_LUX_TO_CLIENT) && (readCompleted == 1) && (writeDescriptorCompleted ==1)) { + writeDescriptorCompleted = 0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2520,7 +2615,7 @@ //READING_MIC - if ((perDevs.status == READING_MIC) && (readCompleted == 1)){ + if ((perDevs.status == READING_MIC) && (readCompleted == 1) && (writeDescriptorCompleted ==1)){ //reading is starting readCompleted = 0; HAL_Delay(300); @@ -2528,6 +2623,8 @@ readCharacteristicCallback(nullGattReadCallbackP(connHandle)); perDevs.status = NOTIFY_MIC_TO_CLIENT; + + writeDescriptorCompleted=0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2547,6 +2644,7 @@ readCharacteristicCallback(nullGattReadCallbackP(connHandle)); perDevs.status = NOTIFY_PRX_TO_CLIENT; + writeDescriptorCompleted =0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2567,6 +2665,7 @@ readCharacteristicCallback(nullGattReadCallbackP(connHandle)); perDevs.status = NOTIFY_AGM_TO_CLIENT; + writeDescriptorCompleted=0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2586,6 +2685,7 @@ readCharacteristicCallback(nullGattReadCallbackP(connHandle)); perDevs.status = NOTIFY_SFUSION_TO_CLIENT; + writeDescriptorCompleted=0; notifyMaster(slaveDev.notification_data.data_length, slaveDev.notification_data.attribute_value, slaveDev.notification_data.attribute_handle); eventQ.call(setNewStatus); @@ -2668,7 +2768,7 @@ error = characteristic.read(); if (error != BLE_ERROR_NONE){ - printf("\r\n\nUnable to read data from periph %d (err %d, cHndl 0x%04x - dHdl 0x%04x)\n", index+1, + printf("\r\nUnable to read data from periph %d (err %d, cHndl 0x%04x - dHdl 0x%04x)\n", index+1, error, connection_handle, characteristic.getDeclHandle()); eventQ.call(setNewStatus);