changed low freq. clock source to IRC

Dependents:   BLE_ANCS_SDAPI_IRC

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF51Gap.cpp Source File

nRF51Gap.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 "nRF51Gap.h"
00018 #include "mbed.h"
00019 
00020 #include "common/common.h "
00021 #include "ble_advdata.h "
00022 #include "ble_hci.h"
00023 
00024 /**************************************************************************/
00025 /*!
00026     @brief  Sets the advertising parameters and payload for the device
00027 
00028     @param[in]  params
00029                 Basic advertising details, including the advertising
00030                 delay, timeout and how the device should be advertised
00031     @params[in] advData
00032                 The primary advertising data payload
00033     @params[in] scanResponse
00034                 The optional Scan Response payload if the advertising
00035                 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
00036                 in \ref GapAdveritinngParams
00037             
00038     @returns    \ref ble_error_t
00039     
00040     @retval     BLE_ERROR_NONE
00041                 Everything executed properly
00042 
00043     @retval     BLE_ERROR_BUFFER_OVERFLOW
00044                 The proposed action would cause a buffer overflow.  All
00045                 advertising payloads must be <= 31 bytes, for example.
00046                 
00047     @retval     BLE_ERROR_NOT_IMPLEMENTED
00048                 A feature was requested that is not yet supported in the
00049                 nRF51 firmware or hardware.
00050 
00051     @retval     BLE_ERROR_PARAM_OUT_OF_RANGE
00052                 One of the proposed values is outside the valid range.
00053 
00054     @section EXAMPLE
00055 
00056     @code
00057 
00058     @endcode
00059 */
00060 /**************************************************************************/
00061 ble_error_t nRF51Gap::setAdvertisingData(const GapAdvertisingData & advData, const GapAdvertisingData & scanResponse)
00062 {
00063     /* Make sure we don't exceed the advertising payload length */
00064     if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
00065     {
00066         return BLE_ERROR_BUFFER_OVERFLOW;
00067     }
00068     
00069     /* Make sure we have a payload! */
00070     if (advData.getPayloadLen() == 0)
00071     {
00072       return BLE_ERROR_PARAM_OUT_OF_RANGE;
00073     }
00074 
00075     /* Check the scan response payload limits */
00076     //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
00077     //{
00078     //    /* Check if we're within the upper limit */
00079     //    if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
00080     //    {
00081     //        return BLE_ERROR_BUFFER_OVERFLOW;
00082     //    }
00083     //    /* Make sure we have a payload! */
00084     //    if (advData.getPayloadLen() == 0)
00085     //    {
00086     //        return BLE_ERROR_PARAM_OUT_OF_RANGE;
00087     //    }
00088     //}
00089     
00090     /* Send advertising data! */
00091     ASSERT( ERROR_NONE == sd_ble_gap_adv_data_set(advData.getPayload(), advData.getPayloadLen(),
00092                                                   scanResponse.getPayload(), scanResponse.getPayloadLen()), BLE_ERROR_PARAM_OUT_OF_RANGE);
00093 
00094     /* Make sure the GAP Service appearance value is aligned with the appearance from GapAdvertisingData */ 
00095     ASSERT( ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), BLE_ERROR_PARAM_OUT_OF_RANGE);
00096 
00097     /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
00098     /* contains a flags AD type, etc. */
00099 
00100     return BLE_ERROR_NONE;
00101 }
00102 
00103 /**************************************************************************/
00104 /*!
00105     @brief  Starts the BLE HW, initialising any services that were
00106             added before this function was called.
00107             
00108     @note   All services must be added before calling this function!
00109             
00110     @returns    ble_error_t
00111     
00112     @retval     BLE_ERROR_NONE
00113                 Everything executed properly
00114                 
00115     @section EXAMPLE
00116 
00117     @code
00118 
00119     @endcode
00120 */
00121 /**************************************************************************/
00122 ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams & params)
00123 {
00124       /* Make sure we support the advertising type */
00125     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
00126     {
00127         /* ToDo: This requires a propery security implementation, etc. */
00128         return BLE_ERROR_NOT_IMPLEMENTED;
00129     }
00130 
00131     /* Check interval range */
00132     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED)
00133     {
00134         /* Min delay is slightly longer for unconnectable devices */
00135         if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
00136             (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
00137         {
00138             return BLE_ERROR_PARAM_OUT_OF_RANGE;
00139         }
00140     }
00141     else
00142     {
00143         if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
00144             (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
00145         {
00146             return BLE_ERROR_PARAM_OUT_OF_RANGE;
00147         }
00148     }
00149 
00150     /* Check timeout is zero for Connectable Directed */
00151     if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
00152         (params.getTimeout() != 0))
00153     {
00154         /* Timeout must be 0 with this type, although we'll never get here */
00155         /* since this isn't implemented yet anyway */
00156         return BLE_ERROR_PARAM_OUT_OF_RANGE;
00157     }
00158 
00159     /* Check timeout for other advertising types */
00160     if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
00161         (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX))
00162     {
00163         return BLE_ERROR_PARAM_OUT_OF_RANGE;
00164     }
00165 
00166     /* Start Advertising */
00167     ble_gap_adv_params_t adv_para = { 0 };
00168 
00169     adv_para.type        = params.getAdvertisingType() ;
00170     adv_para.p_peer_addr = NULL                        ; // Undirected advertisement
00171     adv_para.fp          = BLE_GAP_ADV_FP_ANY          ;
00172     adv_para.p_whitelist = NULL                        ;
00173     adv_para.interval    = params.getInterval()        ; // advertising interval (in units of 0.625 ms)
00174     adv_para.timeout     = params.getTimeout()         ;
00175 
00176     ASSERT( ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE);
00177         
00178     state.advertising = 1;
00179 
00180     return BLE_ERROR_NONE;
00181 }
00182 
00183 /**************************************************************************/
00184 /*!
00185     @brief  Stops the BLE HW and disconnects from any devices
00186             
00187     @returns    ble_error_t
00188     
00189     @retval     BLE_ERROR_NONE
00190                 Everything executed properly
00191                 
00192     @section EXAMPLE
00193 
00194     @code
00195 
00196     @endcode
00197 */
00198 /**************************************************************************/
00199 ble_error_t nRF51Gap::stopAdvertising(void)
00200 {
00201   /* Stop Advertising */
00202   ASSERT( ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
00203 
00204     state.advertising = 0;
00205 
00206   return BLE_ERROR_NONE;
00207 }
00208 
00209 /**************************************************************************/
00210 /*!
00211     @brief  Disconnects if we are connected to a central device
00212             
00213     @returns    ble_error_t
00214     
00215     @retval     BLE_ERROR_NONE
00216                 Everything executed properly
00217                 
00218     @section EXAMPLE
00219 
00220     @code
00221 
00222     @endcode
00223 */
00224 /**************************************************************************/
00225 ble_error_t nRF51Gap::disconnect(void)
00226 {
00227     state.advertising = 0;
00228   state.connected = 0;
00229     
00230     /* Disconnect if we are connected to a central device */
00231   ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(m_connectionHandle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION),
00232              BLE_ERROR_PARAM_OUT_OF_RANGE);
00233    
00234   return BLE_ERROR_NONE;
00235 }
00236 
00237 /**************************************************************************/
00238 /*!
00239     @brief  Sets the 16-bit connection handle
00240 */
00241 /**************************************************************************/
00242 void nRF51Gap::setConnectionHandle(uint16_t con_handle)
00243 {
00244   m_connectionHandle = con_handle;
00245 }
00246  
00247 /**************************************************************************/
00248 /*!
00249     @brief  Gets the 16-bit connection handle
00250 */
00251 /**************************************************************************/
00252 uint16_t nRF51Gap::getConnectionHandle(void)
00253 {
00254   return m_connectionHandle;
00255 }
00256  
00257 /**************************************************************************/
00258 /*!
00259     @brief      Sets the BLE device address
00260             
00261     @returns    ble_error_t
00262 
00263     @section EXAMPLE
00264 
00265     @code
00266 
00267     uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 };
00268     nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address);
00269 
00270     @endcode
00271 */
00272 /**************************************************************************/
00273 ble_error_t nRF51Gap::setAddress(addr_type_t type, const uint8_t address[6])
00274 {
00275   if ( type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) return BLE_ERROR_PARAM_OUT_OF_RANGE;
00276  
00277   ble_gap_addr_t dev_addr;
00278   dev_addr.addr_type = type;
00279   memcpy(dev_addr.addr, address, 6);
00280  
00281   ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(&dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE);
00282  
00283   return BLE_ERROR_NONE;
00284 }