gjhn

Dependents:   BLE_GENERALE

Fork of X_NUCLEO_IDB0XA1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlueNRGGap.h Source File

BlueNRGGap.h

Go to the documentation of this file.
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 /**
00018   ******************************************************************************
00019   * @file    BlueNRGGap.h
00020   * @author  STMicroelectronics
00021   * @brief   Header file for BlueNRG BLE_API Gap Class
00022   ******************************************************************************
00023   * @copy
00024   *
00025   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00026   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00027   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00028   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00029   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00030   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00031   *
00032   * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
00033   */
00034 
00035 #ifndef __BLUENRG_GAP_H__
00036 #define __BLUENRG_GAP_H__
00037 
00038 #ifdef YOTTA_CFG_MBED_OS
00039     #include "mbed-drivers/mbed.h"
00040 #else
00041     #include "mbed.h"
00042 #endif 
00043 #include "ble/blecommon.h"
00044 #include "btle.h"
00045 #include "ble/GapAdvertisingParams.h"
00046 #include "ble/GapAdvertisingData.h"
00047 #include "ble/Gap.h"
00048 
00049 #define BLE_CONN_HANDLE_INVALID 0x0
00050 #define BDADDR_SIZE 6
00051 
00052 #define BLUENRG_GAP_ADV_INTERVAL_MIN (0x0020)
00053 #define BLUENRG_GAP_ADV_INTERVAL_MAX (0x4000)
00054 #define BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN (0x00A0)
00055 
00056 // Scanning and Connection Params used by Central for creating connection
00057 #define GAP_OBSERVATION_PROC (0x80)
00058 
00059 #define SCAN_P         (0x0010)
00060 #define SCAN_L         (0x0010)
00061 #define SUPERV_TIMEOUT (0xC80)
00062 #define CONN_P(x)      ((int)((x)/1.25f))
00063 #define CONN_L(x)      ((int)((x)/0.625f))
00064 #define CONN_P1        ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
00065 #define CONN_P2        ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
00066 #define CONN_L1        (0x0008)
00067 #define CONN_L2        (0x0008)
00068 #define GUARD_INT      5 //msec
00069 #define MIN_INT_CONN   0x0006 //=>7.5msec
00070 #define MAX_INT_CONN   0x0C80 //=>4000msec
00071 #define DEF_INT_CONN   0x0140 //=>400msec (default value for connection interval)
00072 
00073 /**************************************************************************/
00074 /*!
00075     \brief
00076 
00077 */
00078 /**************************************************************************/
00079 class BlueNRGGap : public Gap
00080 {
00081 public:
00082     static BlueNRGGap &getInstance() {
00083         static BlueNRGGap m_instance;
00084         return m_instance;
00085     }
00086 
00087     enum Reason_t {
00088         DEVICE_FOUND,
00089         DISCOVERY_COMPLETE
00090     };
00091 
00092     /* Functions that must be implemented from Gap */
00093     virtual ble_error_t setAddress(addr_type_t type, const BLEProtocol::AddressBytes_t address);
00094     virtual ble_error_t getAddress(BLEProtocol::AddressType_t *typeP, BLEProtocol::AddressBytes_t address);
00095     virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
00096     virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
00097     virtual ble_error_t stopAdvertising(void);
00098     virtual ble_error_t stopScan();
00099     virtual uint16_t    getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_INTERVAL_MIN);}
00100     virtual uint16_t    getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN);}
00101     virtual uint16_t    getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_INTERVAL_MAX);}
00102     virtual ble_error_t disconnect(DisconnectionReason_t reason);
00103     virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
00104     virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
00105     virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
00106     virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
00107 
00108     virtual ble_error_t setDeviceName(const uint8_t *deviceName);
00109     virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
00110     virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
00111     virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
00112 
00113     virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
00114     virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
00115     virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
00116     virtual ScanningPolicyMode_t getScanningPolicyMode(void) const;
00117 
00118     virtual ble_error_t setTxPower(int8_t txPower);
00119     virtual void        getPermittedTxPowerValues(const int8_t **, size_t *);
00120 
00121     virtual ble_error_t connect(const Address_t peerAddr,
00122                                 Gap::AddressType_t peerAddrType,
00123                                 const ConnectionParams_t *connectionParams,
00124                                 const GapScanningParams *scanParams);
00125 
00126     virtual ble_error_t reset(void);
00127 
00128     void                Discovery_CB(Reason_t reason,
00129                                      uint8_t adv_type,
00130                                      uint8_t addr_type,
00131                                      uint8_t *addr,
00132                                      uint8_t *data_length,
00133                                      uint8_t *data,
00134                                      uint8_t *RSSI);
00135     ble_error_t         createConnection(void);
00136 
00137     void     setConnectionHandle(uint16_t con_handle);
00138     uint16_t getConnectionHandle(void);
00139 
00140     bool getIsSetAddress();
00141 
00142     Timeout getAdvTimeout(void) const {
00143         return advTimeout;
00144     }
00145     uint8_t getAdvToFlag(void) {
00146         return AdvToFlag;
00147     }
00148     void setAdvToFlag(void);
00149 
00150     void Process(void);
00151 
00152     GapScanningParams* getScanningParams(void);
00153 
00154     virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
00155 
00156     void setConnectionInterval(uint16_t interval);
00157     void setGapRole(Role_t role);
00158 
00159 private:
00160     uint16_t m_connectionHandle;
00161     Role_t gapRole;
00162     AddressType_t addr_type;
00163     Address_t _peerAddr;
00164     AddressType_t _peerAddrType;
00165     uint8_t bdaddr[BDADDR_SIZE];
00166     bool _scanning;
00167     bool _connecting;
00168     bool isSetAddress;
00169     uint8_t deviceAppearance[2];
00170 
00171     Timeout advTimeout;
00172     bool AdvToFlag;
00173 
00174     static uint16_t SCAN_DURATION_UNITS_TO_MSEC(uint16_t duration) {
00175         return (duration * 625) / 1000;
00176     }
00177 
00178     uint16_t scanInterval;
00179     uint16_t scanWindow;
00180     uint16_t advInterval;
00181     uint16_t slaveConnIntervMin;
00182     uint16_t slaveConnIntervMax;
00183     uint16_t conn_min_interval;
00184     uint16_t conn_max_interval;
00185     void setAdvParameters(void);
00186     void setConnectionParameters(void);
00187 
00188     Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
00189     Gap::ScanningPolicyMode_t scanningPolicyMode;
00190 
00191     Whitelist_t whitelistAddresses;
00192 
00193     ble_error_t updateAdvertisingData(void);
00194 
00195     BlueNRGGap() {
00196         m_connectionHandle = BLE_CONN_HANDLE_INVALID;
00197         addr_type = BLEProtocol::AddressType::RANDOM_STATIC;
00198 
00199         /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
00200         advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
00201         scanningPolicyMode    = Gap::SCAN_POLICY_IGNORE_WHITELIST;
00202 
00203         isSetAddress = false;
00204         memset(deviceAppearance, 0, sizeof(deviceAppearance));
00205     }
00206 
00207     BlueNRGGap(BlueNRGGap const &);
00208     void operator=(BlueNRGGap const &);
00209 
00210     GapAdvertisingData _advData;
00211     GapAdvertisingData _scanResponse;
00212 };
00213 
00214 #endif // ifndef __BLUENRG_GAP_H__