Antonio Vilei / X_NUCLEO_IDB0XA1

Fork of X_NUCLEO_IDB0XA1 by ST Expansion SW Team

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 #include "mbed.h"
00039 #include "ble/blecommon.h"
00040 #include "btle.h"
00041 #include "ble/GapAdvertisingParams.h"
00042 #include "ble/GapAdvertisingData.h"
00043 #include "ble/Gap.h"
00044 
00045 #define BLE_CONN_HANDLE_INVALID 0x0
00046 #define BDADDR_SIZE 6
00047 
00048 #define BLUENRG_GAP_ADV_INTERVAL_MIN (0x0020)
00049 #define BLUENRG_GAP_ADV_INTERVAL_MAX (0x4000)
00050 #define BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN (0x00A0)
00051 
00052 // Scanning and Connection Params used by Central for creating connection
00053 #define GAP_OBSERVATION_PROC (0x80)
00054 
00055 #define SCAN_P         (0x0010)
00056 #define SCAN_L         (0x0010)
00057 #define SUPERV_TIMEOUT (0xC80)
00058 #define CONN_P(x)      ((int)((x)/1.25f))
00059 #define CONN_L(x)      ((int)((x)/0.625f))
00060 #define CONN_P1        ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
00061 #define CONN_P2        ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
00062 #define CONN_L1        (0x0008)
00063 #define CONN_L2        (0x0008)
00064 #define GUARD_INT      5 //msec
00065 #define MIN_INT_CONN   0x0006 //=>7.5msec
00066 #define MAX_INT_CONN   0x0C80 //=>4000msec
00067 #define DEF_INT_CONN   0x0140 //=>400msec (default value for connection interval)
00068 
00069 #define LOCAL_NAME_MAX_SIZE 9 //8 + 1(AD_DATA_TYPE)
00070 #define UUID_BUFFER_SIZE 17 //Either 8*2(16-bit UUIDs) or 4*4(32-bit UUIDs) or 1*16(128-bit UUIDs) +1(AD_DATA_TYPE)
00071 #define ADV_DATA_MAX_SIZE 31
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 Address_t address);
00094     virtual ble_error_t getAddress(addr_type_t *typeP, Address_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 private:
00157     uint16_t m_connectionHandle;
00158     AddressType_t addr_type;
00159     Address_t _peerAddr;
00160     AddressType_t _peerAddrType;
00161     uint8_t bdaddr[BDADDR_SIZE];
00162     bool _scanning;
00163     bool _connecting;
00164     bool isSetAddress;
00165     uint8_t deviceAppearance[2];
00166 
00167     uint8_t local_name_length;
00168     uint8_t local_name[LOCAL_NAME_MAX_SIZE];
00169 
00170     uint8_t servUuidlength;
00171     uint8_t servUuidData[UUID_BUFFER_SIZE];
00172 
00173     uint8_t AdvLen;
00174     uint8_t AdvData[ADV_DATA_MAX_SIZE];
00175     
00176     Timeout advTimeout;
00177     bool AdvToFlag;
00178 
00179     const uint8_t *scan_response_payload;
00180     uint8_t scan_rsp_length;
00181 
00182     static uint16_t SCAN_DURATION_UNITS_TO_MSEC(uint16_t duration) {
00183         return (duration * 625) / 1000;
00184     }
00185 
00186     uint16_t scanInterval;
00187     uint16_t scanWindow;
00188     uint16_t advInterval;
00189     uint16_t conn_min_interval;
00190     uint16_t conn_max_interval;
00191     void setAdvParameters(void);
00192     void setConnectionParameters(void);
00193 
00194     Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
00195     Gap::ScanningPolicyMode_t scanningPolicyMode;
00196 
00197     Whitelist_t whitelistAddresses;
00198 
00199     ble_error_t updateAdvertisingData(void);
00200 
00201     BlueNRGGap() {
00202         m_connectionHandle = BLE_CONN_HANDLE_INVALID;
00203         addr_type = BLEProtocol::AddressType::RANDOM_STATIC;
00204 
00205         /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
00206         advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
00207         scanningPolicyMode    = Gap::SCAN_POLICY_IGNORE_WHITELIST;
00208 
00209         isSetAddress = false;
00210         memset(deviceAppearance, 0, sizeof(deviceAppearance));
00211     }
00212 
00213     BlueNRGGap(BlueNRGGap const &);
00214     void operator=(BlueNRGGap const &);
00215 };
00216 
00217 #endif // ifndef __BLUENRG_GAP_H__