project

Fork of X_NUCLEO_IDB0XA1 by ST

Revision:
191:d22cc4715b0c
Parent:
177:65d9b1b75fca
Parent:
189:30fa96fa8ec4
Child:
203:5b9c78101d45
--- a/source/BlueNRGGap.cpp	Wed Nov 18 15:17:24 2015 +0000
+++ b/source/BlueNRGGap.cpp	Fri Nov 20 16:59:49 2015 +0100
@@ -134,13 +134,15 @@
                 }
             case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS:  /**< Incomplete list of 16-bit Service IDs */
             case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS:    /**< Complete list of 16-bit Service IDs */
+            case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */
+            case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS:   /**< Complete list of 128-bit Service IDs */
                 {
-                PRINTF("Advertising type: INCOMPLETE_LIST_16BIT_SERVICE_IDS/COMPLETE_LIST_16BIT_SERVICE_IDS\n\r");
+                PRINTF("Advertising type: INCOMPLETE_LIST SERVICE_IDS/COMPLETE_LIST SERVICE_IDS\n\r");
                 
                 uint8_t buffSize = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
                 // The total lenght should include the Data Type Value
                 if(buffSize>UUID_BUFFER_SIZE-1) {
-                    return BLE_ERROR_PARAM_OUT_OF_RANGE;
+                    return BLE_ERROR_INVALID_PARAM;
                 }
                 
                 servUuidlength = buffSize+1; // +1 to include the Data Type Value
@@ -149,7 +151,7 @@
                 PRINTF("servUuidlength=%d servUuidData[0]=%d buffSize=%d\n\r", servUuidlength, servUuidData[0], buffSize);
                 // Save the Service UUID list just after the Data Type Value field
                 memcpy(servUuidData+1, loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
-                
+#ifdef DEBUG
                 for(unsigned i=0; i<servUuidlength; i++) {
                     PRINTF("servUuidData[%d] = 0x%x\n\r", i, servUuidData[i]);
                 }
@@ -157,7 +159,7 @@
                 for(unsigned i=0; i<buffSize; i++) {
                     PRINTF("loadPtr.getUnitAtIndex(index).getDataPtr()[%d] = 0x%x\n\r", i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
                 }
-
+#endif /* DEBUG */
                 break;
                 }
             case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS:  /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
@@ -170,16 +172,6 @@
                 PRINTF("Advertising type: COMPLETE_LIST_32BIT_SERVICE_IDS\n\r");
                 return BLE_ERROR_NOT_IMPLEMENTED;
                 }
-            case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */
-                {
-                PRINTF("Advertising type: INCOMPLETE_LIST_128BIT_SERVICE_IDS\n\r");
-                return BLE_ERROR_NOT_IMPLEMENTED;
-                }
-            case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS:   /**< Complete list of 128-bit Service IDs */
-                {
-                PRINTF("Advertising type: COMPLETE_LIST_128BIT_SERVICE_IDS\n\r");
-                return BLE_ERROR_NOT_IMPLEMENTED;
-                }
             case GapAdvertisingData::SHORTENED_LOCAL_NAME:               /**< Shortened Local Name */
                 {
                 break;
@@ -264,8 +256,25 @@
                 PRINTF("advtInterval=%d\n\r", (int)advtInterval);
                 break;
                 }
-            case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA:        /**< Manufacturer Specific Data */                                
+            case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA:        /**< Manufacturer Specific Data */                             
                 {
+                PRINTF("Advertising type: MANUFACTURER_SPECIFIC_DATA\n\r");
+                uint8_t buffSize = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
+                PRINTF("Advertising type: MANUFACTURER_SPECIFIC_DATA (buffSize=%d)\n\r", buffSize);
+                // the total ADV DATA LEN should include two more bytes:
+		// the buffer size byte;
+		// and the Manufacturer Specific Data Type Value byte
+                if(buffSize>ADV_DATA_MAX_SIZE-2) {
+                    return BLE_ERROR_PARAM_OUT_OF_RANGE;
+                }
+                for(int i=0; i<buffSize+1; i++) {
+                    PRINTF("Advertising type: MANUFACTURER_SPECIFIC_DATA loadPtr.getUnitAtIndex(index).getDataPtr()[%d] = 0x%x\n\r",
+				i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
+                }
+                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);
                 break;
                 }
                 
@@ -343,7 +352,9 @@
 /**************************************************************************/
 
 ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams &params)
-{      
+{
+    tBleStatus ret;
+
     /* Make sure we support the advertising type */
     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
         /* ToDo: This requires a propery security implementation, etc. */
@@ -377,22 +388,30 @@
         return BLE_ERROR_PARAM_OUT_OF_RANGE;
     }
 
-    //tBleStatus ret;
-    //const LongUUIDBytes_t HRM_SERVICE_UUID_128 = {0x18, 0x0D};
     /* set scan response data */
-    hci_le_set_scan_resp_data(scan_rsp_length, scan_response_payload); /*int hci_le_set_scan_resp_data(uint8_t length, const uint8_t data[]);*/
+    ret = hci_le_set_scan_resp_data(scan_rsp_length, scan_response_payload);
+    if(BLE_STATUS_SUCCESS!=ret) {
+        PRINTF(" error while setting scan response data (ret=0x%x)\n", ret);
+        switch (ret) {
+          case BLE_STATUS_TIMEOUT:
+            return BLE_STACK_BUSY;
+          default:
+            return BLE_ERROR_UNSPECIFIED;
+        }
+    }
 
     /*aci_gap_set_discoverable(Advertising_Event_Type, Adv_min_intvl, Adv_Max_Intvl, Addr_Type, Adv_Filter_Policy,
                         Local_Name_Length, local_name, service_uuid_length, service_uuid_list, Slave_conn_intvl_min, Slave_conn_intvl_max);*/
     /*LINK_LAYER.H DESCRIBES THE ADVERTISING TYPES*/ 
 
+    // Enable the else branch if you want to set default device name
     char* name = NULL;
     uint8_t nameLen = 0; 
     if(local_name!=NULL) {
         name = (char*)local_name;
         PRINTF("name=%s\n\r", name); 
         nameLen = local_name_length;
-    } else {
+    } /*else {
         char str[] = "ST_BLE_DEV";
         name = new char[strlen(str)+1];
         name[0] = AD_TYPE_COMPLETE_LOCAL_NAME;
@@ -400,7 +419,7 @@
         nameLen = strlen(name);
         PRINTF("nameLen=%d\n\r", nameLen);
         PRINTF("name=%s\n\r", name);      
-    }  
+    }*/
 
     advtInterval = params.getIntervalInADVUnits(); // set advtInterval in case it is not already set by user application  
     ret = aci_gap_set_discoverable(params.getAdvertisingType(), // Advertising_Event_Type                                
@@ -419,7 +438,18 @@
     PRINTF("!!!setting discoverable (servUuidlength=0x%x)\n", servUuidlength);
     if(BLE_STATUS_SUCCESS!=ret) {
        PRINTF("error occurred while setting discoverable (ret=0x%x)\n", ret);
-       return BLE_ERROR_PARAM_OUT_OF_RANGE;  // no other suitable error code is available
+       switch (ret) {
+         case BLE_STATUS_INVALID_PARAMS:
+           return BLE_ERROR_INVALID_PARAM;
+         case ERR_COMMAND_DISALLOWED:
+           return BLE_ERROR_OPERATION_NOT_PERMITTED;
+         case ERR_UNSUPPORTED_FEATURE:
+           return BLE_ERROR_NOT_IMPLEMENTED;
+         case BLE_STATUS_TIMEOUT:
+           return BLE_STACK_BUSY;
+         default:
+           return BLE_ERROR_UNSPECIFIED;
+       }
     }
 
     // Before updating the ADV data, delete COMPLETE_LOCAL_NAME and TX_POWER_LEVEL fields (if present)
@@ -427,28 +457,45 @@
       if(name!=NULL) {
         PRINTF("!!!calling aci_gap_delete_ad_type AD_TYPE_COMPLETE_LOCAL_NAME!!!\n");
         ret = aci_gap_delete_ad_type(AD_TYPE_COMPLETE_LOCAL_NAME);
-        if (ret != BLE_STATUS_SUCCESS){
+        if (BLE_STATUS_SUCCESS!=ret){
           PRINTF("aci_gap_delete_ad_type failed return=%d\n", ret);
-          return BLE_ERROR_PARAM_OUT_OF_RANGE;
+          switch (ret) {
+            case BLE_STATUS_TIMEOUT:
+              return BLE_STACK_BUSY;
+            default:
+              return BLE_ERROR_UNSPECIFIED;
+          }
         }
       }
 
-      if(txPowerAdType) {
+      // If ADV Data Type is MANUFACTURER SPECIFIC, then the TxP is set implicitly
+      // (i.e., w/o calling setTxPower()
+      if(txPowerAdType || AdvData[1]==AD_TYPE_MANUFACTURER_SPECIFIC_DATA) {
         PRINTF("!!!calling aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL)!!!\n");
         ret = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
-        if (ret != BLE_STATUS_SUCCESS){
+        if (BLE_STATUS_SUCCESS!=ret){
           PRINTF("aci_gap_delete_ad_type failed return=%d\n", ret);
-          return BLE_ERROR_PARAM_OUT_OF_RANGE;
+          switch (ret) {
+            case BLE_STATUS_TIMEOUT:
+              return BLE_STACK_BUSY;
+            default:
+              return BLE_ERROR_UNSPECIFIED;
+          }
         }
       }
    
       ret = aci_gap_update_adv_data(AdvLen, AdvData);
       if(BLE_STATUS_SUCCESS!=ret) {
         PRINTF("error occurred while adding adv data (ret=0x%x)\n", ret);
-        return BLE_ERROR_PARAM_OUT_OF_RANGE;  // no other suitable error code is available
+          switch (ret) {
+            case BLE_STATUS_TIMEOUT:
+              return BLE_STACK_BUSY;
+            default:
+              return BLE_ERROR_UNSPECIFIED;
+          }
       }
       
-    }
+    } // AdvLen>0
 
     state.advertising = 1;
 
@@ -490,10 +537,16 @@
         //Set non-discoverable to stop advertising
         ret = aci_gap_set_non_discoverable();
         
-        if (ret != BLE_STATUS_SUCCESS){
+        if (BLE_STATUS_SUCCESS!=ret){
             PRINTF("Error in stopping advertisement (ret=0x%x)!!\n\r", ret) ;
-            return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value 
-            //FIXME: Define Error values equivalent to BlueNRG Error Codes.
+            switch (ret) {
+              case ERR_COMMAND_DISALLOWED:
+                return BLE_ERROR_OPERATION_NOT_PERMITTED;
+              case BLE_STATUS_TIMEOUT:
+                return BLE_STACK_BUSY;
+              default:
+                return BLE_ERROR_UNSPECIFIED;
+            }
         }
         PRINTF("Advertisement stopped!!\n\r") ;
         //Set GapState_t::advertising state
@@ -533,10 +586,16 @@
     if(m_connectionHandle != BLE_CONN_HANDLE_INVALID) {
         ret = aci_gap_terminate(m_connectionHandle, 0x16);//0x16 Connection Terminated by Local Host. 
 
-        if (ret != BLE_STATUS_SUCCESS){
-            PRINTF("Error in GAP termination!!\n\r") ;
-            return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value 
-            //FIXME: Define Error values equivalent to BlueNRG Error Codes.
+        if (BLE_STATUS_SUCCESS != ret){
+            PRINTF("Error in GAP termination (ret=0x%x)!!\n\r", ret) ;
+            switch (ret) {
+              case ERR_COMMAND_DISALLOWED:
+                return BLE_ERROR_OPERATION_NOT_PERMITTED;
+              case BLE_STATUS_TIMEOUT:
+                return BLE_STACK_BUSY;
+              default:
+                return BLE_ERROR_UNSPECIFIED;
+            }
         }
         
         //PRINTF("Disconnected from localhost!!\n\r") ;
@@ -576,10 +635,16 @@
     if(connectionHandle != BLE_CONN_HANDLE_INVALID) {
         ret = aci_gap_terminate(connectionHandle, 0x16);//0x16 Connection Terminated by Local Host. 
 
-        if (ret != BLE_STATUS_SUCCESS){
-            PRINTF("Error in GAP termination!!\n\r") ;
-            return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value 
-            //FIXME: Define Error values equivalent to BlueNRG Error Codes.
+        if (BLE_STATUS_SUCCESS != ret){
+            PRINTF("Error in GAP termination (ret=0x%x)!!\n\r", ret) ;
+            switch (ret) {
+              case ERR_COMMAND_DISALLOWED:
+                return BLE_ERROR_OPERATION_NOT_PERMITTED;
+              case BLE_STATUS_TIMEOUT:
+                return BLE_STACK_BUSY;
+              default:
+                return BLE_ERROR_UNSPECIFIED;
+            }
         }
         
         //PRINTF("Disconnected from localhost!!\n\r") ;
@@ -790,7 +855,7 @@
 /**************************************************************************/
 ble_error_t BlueNRGGap::setDeviceName(const uint8_t *deviceName) 
 {
-    int ret;
+    tBleStatus ret;
     uint8_t nameLen = 0;     
     
     DeviceName = (uint8_t *)deviceName;
@@ -804,10 +869,20 @@
     0, 
     nameLen, 
     (uint8_t *)DeviceName);
-    
-    if(ret){
-        PRINTF("device set name failed\n\r");            
-        return BLE_ERROR_PARAM_OUT_OF_RANGE;//TODO:Wrong error code
+
+    if (BLE_STATUS_SUCCESS != ret){
+        PRINTF("device set name failed (ret=0x%x)!!\n\r", ret) ;
+        switch (ret) {
+          case BLE_STATUS_INVALID_HANDLE:
+          case BLE_STATUS_INVALID_PARAMETER:
+            return BLE_ERROR_INVALID_PARAM;
+          case BLE_STATUS_INSUFFICIENT_RESOURCES:
+            return BLE_ERROR_NO_MEM;
+          case BLE_STATUS_TIMEOUT:
+            return BLE_STACK_BUSY;
+          default:
+            return BLE_ERROR_UNSPECIFIED;
+        }
     }
 
     return BLE_ERROR_NONE;
@@ -837,10 +912,8 @@
 /**************************************************************************/
 ble_error_t BlueNRGGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP) 
 {   
-    //int ret;
-    
     if(DeviceName==NULL) 
-    return BLE_ERROR_PARAM_OUT_OF_RANGE;
+      return BLE_ERROR_UNSPECIFIED;
     
     strcpy((char*)deviceName, (const char*)DeviceName);
     //PRINTF("GetDeviceName=%s\n\r", deviceName);
@@ -872,6 +945,8 @@
 /**************************************************************************/
 ble_error_t BlueNRGGap::setAppearance(GapAdvertisingData::Appearance appearance)
 {
+    tBleStatus ret;
+
     /* 
     Tested with GapAdvertisingData::GENERIC_PHONE. 
     for other appearances BLE Scanner android app is not behaving properly 
@@ -880,9 +955,23 @@
     STORE_LE_16(deviceAppearance, appearance);                 
     PRINTF("input: incoming = %d deviceAppearance= 0x%x 0x%x\n\r", appearance, deviceAppearance[1], deviceAppearance[0]);
     
-    aci_gatt_update_char_value(g_gap_service_handle, g_appearance_char_handle, 0, 2, (uint8_t *)deviceAppearance);
-    
-    return BLE_ERROR_NONE;    
+    ret = aci_gatt_update_char_value(g_gap_service_handle, g_appearance_char_handle, 0, 2, (uint8_t *)deviceAppearance);
+    if (BLE_STATUS_SUCCESS == ret){
+        return BLE_ERROR_NONE;
+    }
+
+    PRINTF("setAppearance failed (ret=0x%x)!!\n\r", ret) ;
+    switch (ret) {
+      case BLE_STATUS_INVALID_HANDLE:
+      case BLE_STATUS_INVALID_PARAMETER:
+        return BLE_ERROR_INVALID_PARAM;
+      case BLE_STATUS_INSUFFICIENT_RESOURCES:
+        return BLE_ERROR_NO_MEM;
+      case BLE_STATUS_TIMEOUT:
+        return BLE_STACK_BUSY;
+      default:
+        return BLE_ERROR_UNSPECIFIED;
+    }
 }
 
 /**************************************************************************/