Hem Dutt Dabral / BLE_BlueNRG_fork

Fork of BLE_BlueNRG by Mridupawan Das

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlueNRGGap.cpp Source File

BlueNRGGap.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "BlueNRGDevice.h"
00018 #include "mbed.h"
00019 #include "Payload.h"
00020 
00021 //Local Variables
00022 const uint8_t *device_name;
00023 
00024 Serial pc1(USBTX, USBRX); // tx, rx
00025 
00026 /**************************************************************************/
00027 /*!
00028     @brief  Sets the advertising parameters and payload for the device
00029 
00030     @param[in]  params
00031                 Basic advertising details, including the advertising
00032                 delay, timeout and how the device should be advertised
00033     @params[in] advData
00034                 The primary advertising data payload
00035     @params[in] scanResponse
00036                 The optional Scan Response payload if the advertising
00037                 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
00038                 in \ref GapAdveritinngParams
00039 
00040     @returns    \ref ble_error_t
00041 
00042     @retval     BLE_ERROR_NONE
00043                 Everything executed properly
00044 
00045     @retval     BLE_ERROR_BUFFER_OVERFLOW
00046                 The proposed action would cause a buffer overflow.  All
00047                 advertising payloads must be <= 31 bytes, for example.
00048 
00049     @retval     BLE_ERROR_NOT_IMPLEMENTED
00050                 A feature was requested that is not yet supported in the
00051                 nRF51 firmware or hardware.
00052 
00053     @retval     BLE_ERROR_PARAM_OUT_OF_RANGE
00054                 One of the proposed values is outside the valid range.
00055 
00056     @section EXAMPLE
00057 
00058     @code
00059 
00060     @endcode
00061 */
00062 /**************************************************************************/
00063 ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
00064 {
00065     /* Make sure we don't exceed the advertising payload length */
00066     if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
00067         return BLE_ERROR_BUFFER_OVERFLOW;
00068     }
00069 
00070     /* Make sure we have a payload! */
00071     if (advData.getPayloadLen() <= 0) {
00072         return BLE_ERROR_PARAM_OUT_OF_RANGE;
00073     } else { //set the advData here in some local variable so that startAdvertising can use it.
00074         Payload load(advData.getPayload(), advData.getPayloadLen());
00075         
00076         for(uint8_t index=0; index<load.getPayloadUnitCount(); index++) {
00077             //UnitPayload unitLoad = load.getPayLoadAtIndex(index);
00078             switch(load.getIDAtIndex(index)) {
00079                 case GapAdvertisingData::FLAGS:                              /* ref *Flags */
00080                 break;
00081                 case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS:  /**< Incomplete list of 16-bit Service IDs */
00082                 break;
00083                 case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS:    /**< Complete list of 16-bit Service IDs */
00084                 break;
00085                 case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS:  /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
00086                 break;
00087                 case GapAdvertisingData::COMPLETE_LIST_32BIT_SERVICE_IDS:    /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
00088                 break;
00089                 case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */
00090                 break;
00091                 case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS:   /**< Complete list of 128-bit Service IDs */
00092                 break;
00093                 case GapAdvertisingData::SHORTENED_LOCAL_NAME:               /**< Shortened Local Name */
00094                 break;
00095                 case GapAdvertisingData::COMPLETE_LOCAL_NAME:                /**< Complete Local Name */
00096                 device_name = load.getDataAtIndex(index);
00097                 break;
00098                 case GapAdvertisingData::TX_POWER_LEVEL:                     /**< TX Power Level (in dBm) */
00099                 break;
00100                 case GapAdvertisingData::DEVICE_ID:                          /**< Device ID */
00101                 break;
00102                 case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE:    /**< Slave :Connection Interval Range */
00103                 break;
00104                 case GapAdvertisingData::SERVICE_DATA:                       /**< Service Data */
00105                 break;
00106                 case GapAdvertisingData::APPEARANCE:                         /**< \ref Appearance */
00107                 break;
00108                 case GapAdvertisingData::ADVERTISING_INTERVAL:               /**< Advertising Interval */
00109                 break;
00110                 case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA:        /**< Manufacturer Specific Data */                                
00111                 break;
00112                                 
00113             }
00114         }
00115         //const uint8_t *payload = advData.getPayload();
00116         
00117     }
00118     
00119     return BLE_ERROR_NONE;
00120 }
00121 
00122 /**************************************************************************/
00123 /*!
00124     @brief  Starts the BLE HW, initialising any services that were
00125             added before this function was called.
00126 
00127     @note   All services must be added before calling this function!
00128 
00129     @returns    ble_error_t
00130 
00131     @retval     BLE_ERROR_NONE
00132                 Everything executed properly
00133 
00134     @section EXAMPLE
00135 
00136     @code
00137 
00138     @endcode
00139 */
00140 /**************************************************************************/
00141 ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams &params)
00142 {
00143     /* Make sure we support the advertising type */
00144     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
00145         /* ToDo: This requires a propery security implementation, etc. */
00146         return BLE_ERROR_NOT_IMPLEMENTED;
00147     }
00148 
00149     /* Check interval range */
00150     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) {
00151         /* Min delay is slightly longer for unconnectable devices */
00152         if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
00153             (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) {
00154             return BLE_ERROR_PARAM_OUT_OF_RANGE;
00155         }
00156     } else {
00157         if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
00158             (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) {
00159             return BLE_ERROR_PARAM_OUT_OF_RANGE;
00160         }
00161     }
00162 
00163     /* Check timeout is zero for Connectable Directed */
00164     if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) {
00165         /* Timeout must be 0 with this type, although we'll never get here */
00166         /* since this isn't implemented yet anyway */
00167         return BLE_ERROR_PARAM_OUT_OF_RANGE;
00168     }
00169 
00170     /* Check timeout for other advertising types */
00171     if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
00172         (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) {
00173         return BLE_ERROR_PARAM_OUT_OF_RANGE;
00174     }
00175 
00176   tBleStatus ret;
00177   
00178   //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'};
00179   //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28],device_name[29],device_name[30], device_name[31],
00180    //                                                     device_name[32], device_name[33], device_name[34], device_name[35], device_name[36]};
00181   
00182   #if 0
00183   pc1.printf("Hello Teraterm\n");
00184   /*const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[13],device_name[14],device_name[15],device_name[16], device_name[17],
00185                                                        device_name[18], device_name[19], device_name[20], device_name[21], device_name[22]};*/
00186   const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'h', 'd', 'd'};
00187   #else
00188   
00189     char *namePtr = new char[1+sizeof(device_name)];
00190     namePtr[0] = AD_TYPE_COMPLETE_LOCAL_NAME;
00191     pc1.printf("now setting name to: %s...\n", device_name);
00192     pc1.printf("device_name length=%d", sizeof(namePtr));
00193     int i=0;
00194     while(device_name[i]!=0) {
00195         namePtr[i+1] = device_name[i];
00196         pc1.printf("%c\n", namePtr[i+1]);
00197         i++;
00198         }
00199     
00200     const char* local_name = (const char*)namePtr;   
00201   
00202   #endif
00203   
00204   //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28]};
00205   const LongUUID_t HRM_SERVICE_UUID_128 = {0x18, 0x0D};
00206   /* disable scan response */
00207   hci_le_set_scan_resp_data(0,NULL);
00208   
00209   /*aci_gap_set_discoverable(Advertising_Event_Type, Adv_min_intvl, Adv_Max_Intvl, Addr_Type, Adv_Filter_Policy,
00210                         Local_Name_Length, local_name, service_uuid_length, service_uuid_list, Slave_conn_intvl_min, Slave_conn_intvl_max);*/
00211   /*LINK_LAYER.H DESCRIBES THE ADVERTISING TYPES*/ 
00212                                 
00213   ret = aci_gap_set_discoverable(params.getAdvertisingType(), params.getInterval(), 0, PUBLIC_ADDR, NO_WHITE_LIST_USE,
00214                                  11 /*Length of the local_name[] array*/, local_name, 0, NULL, 0, 0);
00215   state.advertising = 1;
00216   
00217     return BLE_ERROR_NONE;
00218 }
00219 
00220 /**************************************************************************/
00221 /*!
00222     @brief  Stops the BLE HW and disconnects from any devices
00223 
00224     @returns    ble_error_t
00225 
00226     @retval     BLE_ERROR_NONE
00227                 Everything executed properly
00228 
00229     @section EXAMPLE
00230 
00231     @code
00232 
00233     @endcode
00234 */
00235 /**************************************************************************/
00236 ble_error_t BlueNRGGap::stopAdvertising(void)
00237 {
00238     
00239 
00240     return BLE_ERROR_NONE;
00241 }
00242 
00243 /**************************************************************************/
00244 /*!
00245     @brief  Disconnects if we are connected to a central device
00246 
00247     @returns    ble_error_t
00248 
00249     @retval     BLE_ERROR_NONE
00250                 Everything executed properly
00251 
00252     @section EXAMPLE
00253 
00254     @code
00255 
00256     @endcode
00257 */
00258 /**************************************************************************/
00259 ble_error_t BlueNRGGap::disconnect(void)
00260 {
00261     
00262     return BLE_ERROR_NONE;
00263 }
00264 
00265 /**************************************************************************/
00266 /*!
00267     @brief  Sets the 16-bit connection handle
00268 */
00269 /**************************************************************************/
00270 void BlueNRGGap::setConnectionHandle(uint16_t con_handle)
00271 {
00272     m_connectionHandle = con_handle;
00273 }
00274 
00275 /**************************************************************************/
00276 /*!
00277     @brief  Gets the 16-bit connection handle
00278 */
00279 /**************************************************************************/
00280 uint16_t BlueNRGGap::getConnectionHandle(void)
00281 {
00282     return m_connectionHandle;
00283 }
00284 
00285 /**************************************************************************/
00286 /*!
00287     @brief      Sets the BLE device address
00288 
00289     @returns    ble_error_t
00290 
00291     @section EXAMPLE
00292 
00293     @code
00294 
00295     @endcode
00296 */
00297 /**************************************************************************/
00298 ble_error_t BlueNRGGap::setAddress(addr_type_t type, const uint8_t address[6])
00299 {
00300     tBleStatus ret;
00301     
00302     if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) {
00303         return BLE_ERROR_PARAM_OUT_OF_RANGE;
00304     }
00305         
00306     ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, (const tHalUint8*)address);
00307     
00308     //if (ret==BLE_STATUS_SUCCESS)
00309     return BLE_ERROR_NONE;
00310     
00311     //return BLE_ERROR_PARAM_OUT_OF_RANGE;
00312 }