test

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF5xGap.h Source File

nRF5xGap.h

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 #ifndef __NRF5x_GAP_H__
00018 #define __NRF5x_GAP_H__
00019 
00020 #include "mbed.h"
00021 #include "ble/blecommon.h"
00022 #include "ble.h"
00023 #include "ble/GapAdvertisingParams.h"
00024 #include "ble/GapAdvertisingData.h"
00025 #include "ble/Gap.h"
00026 #include "ble/GapScanningParams.h"
00027 
00028 #include "nrf_soc.h"
00029 #include "ble_radio_notification.h "
00030 #include "btle_security.h"
00031 
00032 void radioNotificationStaticCallback(bool param);
00033 
00034 /**************************************************************************/
00035 /*!
00036     \brief
00037 
00038 */
00039 /**************************************************************************/
00040 class nRF5xGap : public Gap
00041 {
00042 public:
00043     static nRF5xGap &getInstance();
00044 
00045     /* Functions that must be implemented from Gap */
00046     virtual ble_error_t setAddress(AddressType_t  type,  const Address_t address);
00047     virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address);
00048     virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
00049 
00050     virtual uint16_t    getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
00051     virtual uint16_t    getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);}
00052     virtual uint16_t    getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);}
00053 
00054     virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
00055     virtual ble_error_t stopAdvertising(void);
00056     virtual ble_error_t connect(const Address_t, Gap::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
00057     virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
00058     virtual ble_error_t disconnect(DisconnectionReason_t reason);
00059 
00060     virtual ble_error_t setDeviceName(const uint8_t *deviceName);
00061     virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
00062     virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
00063     virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
00064 
00065     virtual ble_error_t setTxPower(int8_t txPower);
00066     virtual void        getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
00067 
00068     void     setConnectionHandle(uint16_t con_handle);
00069     uint16_t getConnectionHandle(void);
00070 
00071     virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
00072     virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
00073     virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
00074 
00075     virtual ble_error_t initRadioNotification(void) {
00076         if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
00077             return BLE_ERROR_NONE;
00078         }
00079 
00080         return BLE_ERROR_UNSPECIFIED;
00081     }
00082 
00083 /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
00084 #if !defined(MCU_NRF51_16K_S110) && !defined(MCU_NRF51_32K_S110)
00085     virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {
00086         ble_gap_scan_params_t scanParams = {
00087             .active      = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */
00088             .selective   = 0,    /**< If 1, ignore unknown devices (non whitelisted). */
00089             .p_whitelist = NULL, /**< Pointer to whitelist, NULL if none is given. */
00090             .interval    = scanningParams.getInterval(),  /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
00091             .window      = scanningParams.getWindow(),    /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
00092             .timeout     = scanningParams.getTimeout(),   /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
00093         };
00094 
00095         if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) {
00096             return BLE_ERROR_PARAM_OUT_OF_RANGE;
00097         }
00098 
00099         return BLE_ERROR_NONE;
00100     }
00101 
00102     virtual ble_error_t stopScan(void) {
00103         if (sd_ble_gap_scan_stop() == NRF_SUCCESS) {
00104             return BLE_ERROR_NONE;
00105         }
00106 
00107         return BLE_STACK_BUSY;
00108     }
00109 #endif
00110 
00111 private:
00112     /**
00113      * A helper function to process radio-notification events; to be called internally.
00114      * @param param [description]
00115      */
00116     void processRadioNotificationEvent(bool param) {
00117         radioNotificationCallback.call(param);
00118     }
00119     friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */
00120 
00121 private:
00122     uint16_t m_connectionHandle;
00123     nRF5xGap() {
00124         m_connectionHandle = BLE_CONN_HANDLE_INVALID;
00125     }
00126 
00127     nRF5xGap(nRF5xGap const &);
00128     void operator=(nRF5xGap const &);
00129 };
00130 
00131 #endif // ifndef __NRF5x_GAP_H__