BLE_API wrapper library for STMicroelectronics' BlueNRG Bluetooth Low Energy expansion board shield (Component)

Dependents:   Nucleo_Zumo_BLE_IDB04A1 contest_IOT5 contest_IOT6 contest_IOT_10 ... more

Fork of X_NUCLEO_IDB0XA1 by ST Expansion SW Team

Arduino Connector Compatibility Warning

X-NUCLEO-IDB04A1 and X-NUCLEO-IDB05A1 are Arduino compatible with an exception: instead of using pin D13 for the SPI clock, they use pin D3. The default configuration for this library is having the SPI clock on pin D3.

To be fully Arduino compatible, X-NUCLEO-IDB04A1 and X-NUCLEO-IDB05A1 need a small HW patch.

For X-NUCLEO-IDB04A1 this patch consists in removing zero resistor R10 and instead soldering zero resistor R11. For X-NUCLEO-IDB05A1 this patch consists in removing zero resistor R4 and instead soldering zero resistor R6.

In case you patch your board, then you also have to configure this library to use pin D13 to drive the SPI clock (see macro IDB0XA1_D13_PATCH in file x_nucleo_idb0xa1_targets.h).

If you use pin D13 for the SPI clock, please be aware that on STM32 Nucleo boards you may not drive the LED, otherwise you will get a conflict: the LED on STM32 Nucleo boards is connected to pin D13.

Referring to the current list of tested platforms (see X-NUCLEO-IDB04A1 and X-NUCLEO-IDB05A1 pages), the patch is required by ST-Nucleo-F103RB; ST-Nucleo-F302R8; ST-Nucleo-F411RE; and ST-Nucleo-F446RE.

Revision:
252:0c2cb16a7166
Parent:
242:058b2e731adc
Parent:
251:86df2c289e7c
Child:
253:9665a6f8bbdb
--- a/source/BlueNRGGap.cpp	Tue Jun 21 17:00:00 2016 +0200
+++ b/source/BlueNRGGap.cpp	Mon Jun 27 15:51:20 2016 +0200
@@ -108,6 +108,7 @@
         PRINTF("advData.getPayloadLen() == 0\n\r");
         //return BLE_ERROR_PARAM_OUT_OF_RANGE;
         local_name_length = 0;
+        txPowLevSet = 0;
         servUuidlength = 0;
         AdvLen = 0;
     } else {
@@ -181,6 +182,7 @@
                 {
                 PRINTF("Advertising type: COMPLETE_LOCAL_NAME\n\r");
                 loadPtr.getUnitAtIndex(index).printDataAsString();
+                loadPtr.getUnitAtIndex(index).printDataAsHex();
                 local_name_length = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
                 // The total length should include the Data Type Value
                 if(local_name_length>ADV_DATA_MAX_SIZE-1) {
@@ -188,7 +190,7 @@
                 }
                 local_name[0] = (uint8_t)(*loadPtr.getUnitAtIndex(index).getAdTypePtr()); //Data Type Value
                 memcpy(local_name+1, (uint8_t*)loadPtr.getUnitAtIndex(index).getDataPtr(), local_name_length-1);
-                PRINTF("Advertising type: COMPLETE_LOCAL_NAME local_name=%s local_name_length=%d\n\r", local_name, local_name_length);
+                PRINTF("Advertising type: COMPLETE_LOCAL_NAME local_name=%s local_name_length=%d\n\r", local_name+1, local_name_length);
 
                 break;
                 }
@@ -197,13 +199,17 @@
                 PRINTF("Advertising type: TX_POWER_LEVEL\n\r");     
                 int8_t enHighPower = 0;
                 int8_t paLevel = 0;
-#ifdef DEBUG
+
                 int8_t dbm = *loadPtr.getUnitAtIndex(index).getDataPtr();
-                int8_t dbmActuallySet = getHighPowerAndPALevelValue(dbm, enHighPower, paLevel);
+                tBleStatus ret = getHighPowerAndPALevelValue(dbm, enHighPower, paLevel);
+#ifdef DEBUG
+                PRINTF("dbm=%d, ret=%d\n\r", dbm, ret);
+                PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
 #endif
-                PRINTF("dbm=%d, dbmActuallySet=%d\n\r", dbm, dbmActuallySet);
-                PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);                    
-                aci_hal_set_tx_power_level(enHighPower, paLevel);
+                if(ret == BLE_STATUS_SUCCESS) {
+                  aci_hal_set_tx_power_level(enHighPower, paLevel);
+                  txPowLevSet = 1;
+                }
                 break;
                 }
             case GapAdvertisingData::DEVICE_ID:                          /**< Device ID */
@@ -212,6 +218,11 @@
                 }
             case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE:    /**< Slave :Connection Interval Range */
                 {
+                PRINTF("Advertising type: SLAVE_CONNECTION_INTERVAL_RANGE\n\r");
+                uint8_t *ptr = loadPtr.getUnitAtIndex(index).getDataPtr();
+                slaveConnIntervMin = ptr[0]|ptr[1]<<8;
+                slaveConnIntervMax = ptr[2]|ptr[3]<<8;
+
                 break;
                 }
             case GapAdvertisingData::SERVICE_DATA:                       /**< Service Data */
@@ -229,17 +240,22 @@
                             i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
                 }
 #endif
-                AdvLen = buffSize+2; // the total ADV DATA LEN should include two more bytes: the buffer size byte; and the Service Data Type Value byte
-                AdvData[0] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
-                AdvData[1] = AD_TYPE_SERVICE_DATA;
-                memcpy(AdvData+2, loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
+                // the total ADV DATA LEN should include two more bytes: the buffer size byte; and the Service Data Type Value byte
+                AdvData[AdvLen++] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
+                AdvData[AdvLen++] = AD_TYPE_SERVICE_DATA;
+                memcpy(&AdvData[AdvLen], loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
+                AdvLen += buffSize;
                 break;
                 }
 
             case GapAdvertisingData::ADVERTISING_INTERVAL:               /**< Advertising Interval */
                 {
-                PRINTF("Advertising type: ADVERTISING_INTERVAL\n\r");
-                //uint32_t advInt = (uint32_t)(*loadPtr.getUnitAtIndex(index).getDataPtr());
+                printf("Advertising type: ADVERTISING_INTERVAL\n\r");
+                uint8_t buffSize = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
+                AdvData[AdvLen++] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
+                AdvData[AdvLen++] = AD_TYPE_ADVERTISING_INTERVAL;
+                memcpy(&AdvData[AdvLen], loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
+                AdvLen += buffSize;
                 break;
                 }
             case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA:        /**< Manufacturer Specific Data */                             
@@ -259,10 +275,11 @@
 				i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
                 }
 #endif
-                AdvLen = buffSize+2; // the total ADV DATA LEN should include two more bytes: the buffer size byte; and the Manufacturer Specific Data Type Value byte
-                AdvData[0] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
-                AdvData[1] = AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
-                memcpy(AdvData+2, loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
+                // the total ADV DATA LEN should include two more bytes: the buffer size byte; and the Manufacturer Specific Data Type Value byte
+                AdvData[AdvLen++] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
+                AdvData[AdvLen++] = AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
+                memcpy(&AdvData[AdvLen], loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
+                AdvLen += buffSize;
                 break;
                 }
             } // end switch
@@ -467,8 +484,8 @@
                                    (const char*)local_name,     // LocalName
                                    servUuidlength,              // ServiceUUIDLen
                                    servUuidData,                // ServiceUUIDList
-                                   0,                           // SlaveConnIntervMin
-                                   0);                          // SlaveConnIntervMax
+                                   slaveConnIntervMin,          // SlaveConnIntervMin
+                                   slaveConnIntervMax);         // SlaveConnIntervMax
 
     
     PRINTF("!!!setting discoverable (servUuidlength=0x%x)\n\r", servUuidlength);
@@ -476,6 +493,7 @@
        PRINTF("error occurred while setting discoverable (ret=0x%x)\n\r", ret);
        switch (ret) {
          case BLE_STATUS_INVALID_PARAMS:
+         case ERR_INVALID_HCI_CMD_PARAMS:
            return BLE_ERROR_INVALID_PARAM;
          case ERR_COMMAND_DISALLOWED:
            return BLE_ERROR_OPERATION_NOT_PERMITTED;
@@ -488,6 +506,13 @@
        }
     }
 
+    // Since AD_TYPE_TX_POWER_LEVEL has not been set by application, we delete it
+    if(!txPowLevSet) {
+      PRINTF("Deleting TX POW LEV\n");
+      aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
+      txPowLevSet = 0;
+    }
+
     // Stop Advertising if an error occurs while updating ADV data
     rc = updateAdvertisingData();
     if(rc != BLE_ERROR_NONE) {
@@ -514,7 +539,7 @@
 {
     tBleStatus ret;
 
-    // Before updating the ADV data, delete COMPLETE_LOCAL_NAME and TX_POWER_LEVEL fields (if present)
+    // Before updating the ADV data, delete COMPLETE_LOCAL_NAME field
     if(AdvLen > 0) {
       if(local_name_length > 0) {
         ret = aci_gap_delete_ad_type(AD_TYPE_COMPLETE_LOCAL_NAME);
@@ -533,9 +558,8 @@
         }
       }
 
-      // If ADV Data Type is SERVICE DATA or MANUFACTURER SPECIFIC DATA,
-      // we need to delete it to make the needed room in ADV payload
-      if(AdvData[1]==AD_TYPE_SERVICE_DATA || AdvData[1]==AD_TYPE_MANUFACTURER_SPECIFIC_DATA) {
+      // ...and TX_POWER_LEVEL field to make the needed room in ADV payload
+      if(txPowLevSet) {
         ret = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
         if (BLE_STATUS_SUCCESS!=ret){
           PRINTF("aci_gap_delete_ad_type failed return=%d\n", ret);