BLE_API wrapper library for STMicroelectronics' BlueNRG Bluetooth Low Energy expansion board shield

Fork of X_NUCLEO_IDB0XA1 by ST Expansion SW Team

Committer:
Andrea Palmieri
Date:
Fri May 20 17:00:21 2016 +0200
Revision:
222:aead10288880
Parent:
220:6eb53072d82b
Child:
224:3f6bba52b307
Fix bug on intervals setting

Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 130:770ce14d3d15 1 /* mbed Microcontroller Library
Wolfgang Betz 130:770ce14d3d15 2 * Copyright (c) 2006-2013 ARM Limited
Wolfgang Betz 130:770ce14d3d15 3 *
Wolfgang Betz 130:770ce14d3d15 4 * Licensed under the Apache License, Version 2.0 (the "License");
Wolfgang Betz 130:770ce14d3d15 5 * you may not use this file except in compliance with the License.
Wolfgang Betz 130:770ce14d3d15 6 * You may obtain a copy of the License at
Wolfgang Betz 130:770ce14d3d15 7 *
Wolfgang Betz 130:770ce14d3d15 8 * http://www.apache.org/licenses/LICENSE-2.0
Wolfgang Betz 130:770ce14d3d15 9 *
Wolfgang Betz 130:770ce14d3d15 10 * Unless required by applicable law or agreed to in writing, software
Wolfgang Betz 130:770ce14d3d15 11 * distributed under the License is distributed on an "AS IS" BASIS,
Wolfgang Betz 130:770ce14d3d15 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Wolfgang Betz 130:770ce14d3d15 13 * See the License for the specific language governing permissions and
Wolfgang Betz 130:770ce14d3d15 14 * limitations under the License.
Wolfgang Betz 130:770ce14d3d15 15 */
Wolfgang Betz 130:770ce14d3d15 16
Wolfgang Betz 130:770ce14d3d15 17 /**
Wolfgang Betz 130:770ce14d3d15 18 ******************************************************************************
Wolfgang Betz 130:770ce14d3d15 19 * @file BlueNRGGap.h
Wolfgang Betz 130:770ce14d3d15 20 * @author STMicroelectronics
Wolfgang Betz 130:770ce14d3d15 21 * @brief Header file for BlueNRG BLE_API Gap Class
Wolfgang Betz 130:770ce14d3d15 22 ******************************************************************************
Wolfgang Betz 130:770ce14d3d15 23 * @copy
Wolfgang Betz 130:770ce14d3d15 24 *
Wolfgang Betz 130:770ce14d3d15 25 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
Wolfgang Betz 130:770ce14d3d15 26 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
Wolfgang Betz 130:770ce14d3d15 27 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
Wolfgang Betz 130:770ce14d3d15 28 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
Wolfgang Betz 130:770ce14d3d15 29 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
Wolfgang Betz 130:770ce14d3d15 30 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Wolfgang Betz 130:770ce14d3d15 31 *
Wolfgang Betz 130:770ce14d3d15 32 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
Wolfgang Betz 130:770ce14d3d15 33 */
Wolfgang Betz 130:770ce14d3d15 34
Wolfgang Betz 130:770ce14d3d15 35 #ifndef __BLUENRG_GAP_H__
Wolfgang Betz 130:770ce14d3d15 36 #define __BLUENRG_GAP_H__
Wolfgang Betz 130:770ce14d3d15 37
Wolfgang Betz 144:bdf5e8432131 38 #include "mbed-drivers/mbed.h"
Wolfgang Betz 131:e09947216ccb 39 #include "ble/blecommon.h"
Wolfgang Betz 130:770ce14d3d15 40 #include "btle.h"
Wolfgang Betz 131:e09947216ccb 41 #include "ble/GapAdvertisingParams.h"
Wolfgang Betz 131:e09947216ccb 42 #include "ble/GapAdvertisingData.h"
Wolfgang Betz 132:51056160fa4a 43 #include "ble/Gap.h"
Wolfgang Betz 130:770ce14d3d15 44
Wolfgang Betz 130:770ce14d3d15 45 #define BLE_CONN_HANDLE_INVALID 0x0
Wolfgang Betz 130:770ce14d3d15 46 #define BDADDR_SIZE 6
Wolfgang Betz 130:770ce14d3d15 47
Andrea Palmieri 214:d8669b6b3175 48 #define BLUENRG_GAP_ADV_INTERVAL_MIN (0x0020)
Andrea Palmieri 219:9981f62cdb1a 49 #define BLUENRG_GAP_ADV_INTERVAL_MAX (0x4000)
Andrea Palmieri 219:9981f62cdb1a 50 #define BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN (0x00A0)
Wolfgang Betz 130:770ce14d3d15 51
Wolfgang Betz 132:51056160fa4a 52 // Scanning and Connection Params used by Central for creating connection
Andrea Palmieri 207:23870c55b07e 53 #define GAP_OBSERVATION_PROC (0x80)
Wolfgang Betz 132:51056160fa4a 54
Andrea Palmieri 210:e02936f0d4c7 55 #define SCAN_P (0x0010)
Andrea Palmieri 210:e02936f0d4c7 56 #define SCAN_L (0x0010)
Andrea Palmieri 210:e02936f0d4c7 57 #define SUPERV_TIMEOUT (0xC80)
Wolfgang Betz 132:51056160fa4a 58 #define CONN_P(x) ((int)((x)/1.25f))
Wolfgang Betz 132:51056160fa4a 59 #define CONN_L(x) ((int)((x)/0.625f))
Andrea Palmieri 210:e02936f0d4c7 60 #define CONN_P1 ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
Andrea Palmieri 210:e02936f0d4c7 61 #define CONN_P2 ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
Andrea Palmieri 219:9981f62cdb1a 62 #define CONN_L1 (0x0008)
Andrea Palmieri 219:9981f62cdb1a 63 #define CONN_L2 (0x0008)
Andrea Palmieri 219:9981f62cdb1a 64 #define GUARD_INT 5 //msec
Andrea Palmieri 185:30fa96fa8ec4 65
Andrea Palmieri 212:37a7e289ee3d 66 #define LOCAL_NAME_MAX_SIZE 9 //8 + 1(AD_DATA_TYPE)
Andrea Palmieri 212:37a7e289ee3d 67 #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)
Wolfgang Betz 130:770ce14d3d15 68 #define ADV_DATA_MAX_SIZE 31
Wolfgang Betz 130:770ce14d3d15 69
Wolfgang Betz 130:770ce14d3d15 70 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 71 /*!
Wolfgang Betz 130:770ce14d3d15 72 \brief
Wolfgang Betz 130:770ce14d3d15 73
Wolfgang Betz 130:770ce14d3d15 74 */
Wolfgang Betz 130:770ce14d3d15 75 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 76 class BlueNRGGap : public Gap
Wolfgang Betz 130:770ce14d3d15 77 {
Wolfgang Betz 130:770ce14d3d15 78 public:
Wolfgang Betz 130:770ce14d3d15 79 static BlueNRGGap &getInstance() {
Wolfgang Betz 130:770ce14d3d15 80 static BlueNRGGap m_instance;
Wolfgang Betz 130:770ce14d3d15 81 return m_instance;
Wolfgang Betz 130:770ce14d3d15 82 }
Wolfgang Betz 130:770ce14d3d15 83
Wolfgang Betz 132:51056160fa4a 84 enum Reason_t {
Wolfgang Betz 132:51056160fa4a 85 DEVICE_FOUND,
Wolfgang Betz 132:51056160fa4a 86 DISCOVERY_COMPLETE
Wolfgang Betz 132:51056160fa4a 87 };
Wolfgang Betz 130:770ce14d3d15 88
Wolfgang Betz 130:770ce14d3d15 89 /* Functions that must be implemented from Gap */
Andrea Palmieri 219:9981f62cdb1a 90 virtual ble_error_t setAddress(addr_type_t type, const Address_t address);
Wolfgang Betz 130:770ce14d3d15 91 virtual ble_error_t getAddress(addr_type_t *typeP, Address_t address);
Wolfgang Betz 130:770ce14d3d15 92 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
Wolfgang Betz 130:770ce14d3d15 93 virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
Wolfgang Betz 130:770ce14d3d15 94 virtual ble_error_t stopAdvertising(void);
Wolfgang Betz 130:770ce14d3d15 95 virtual ble_error_t stopScan();
Andrea Palmieri 214:d8669b6b3175 96 virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_INTERVAL_MIN);}
Andrea Palmieri 214:d8669b6b3175 97 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN);}
Andrea Palmieri 214:d8669b6b3175 98 virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_INTERVAL_MAX);}
Wolfgang Betz 130:770ce14d3d15 99 virtual ble_error_t disconnect(DisconnectionReason_t reason);
Wolfgang Betz 130:770ce14d3d15 100 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
Wolfgang Betz 130:770ce14d3d15 101 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
Wolfgang Betz 130:770ce14d3d15 102 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
Wolfgang Betz 130:770ce14d3d15 103 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
Wolfgang Betz 130:770ce14d3d15 104
Wolfgang Betz 130:770ce14d3d15 105 virtual ble_error_t setDeviceName(const uint8_t *deviceName);
Wolfgang Betz 130:770ce14d3d15 106 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
Wolfgang Betz 130:770ce14d3d15 107 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
Wolfgang Betz 130:770ce14d3d15 108 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
Andrea Palmieri 219:9981f62cdb1a 109
Andrea Palmieri 219:9981f62cdb1a 110 virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
Andrea Palmieri 219:9981f62cdb1a 111 virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
Andrea Palmieri 219:9981f62cdb1a 112 virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
Andrea Palmieri 219:9981f62cdb1a 113 virtual ScanningPolicyMode_t getScanningPolicyMode(void) const;
Andrea Palmieri 219:9981f62cdb1a 114
Wolfgang Betz 130:770ce14d3d15 115 virtual ble_error_t setTxPower(int8_t txPower);
Wolfgang Betz 130:770ce14d3d15 116 virtual void getPermittedTxPowerValues(const int8_t **, size_t *);
Andrea Palmieri 219:9981f62cdb1a 117
Wolfgang Betz 132:51056160fa4a 118 virtual ble_error_t connect(const Address_t peerAddr,
Wolfgang Betz 132:51056160fa4a 119 Gap::AddressType_t peerAddrType,
Wolfgang Betz 132:51056160fa4a 120 const ConnectionParams_t *connectionParams,
Wolfgang Betz 132:51056160fa4a 121 const GapScanningParams *scanParams);
Andrea Palmieri 219:9981f62cdb1a 122
Andrea Palmieri 219:9981f62cdb1a 123 virtual ble_error_t reset(void);
Wolfgang Betz 130:770ce14d3d15 124
Wolfgang Betz 132:51056160fa4a 125 void Discovery_CB(Reason_t reason,
Wolfgang Betz 132:51056160fa4a 126 uint8_t adv_type,
Andrea Palmieri 219:9981f62cdb1a 127 uint8_t addr_type,
Wolfgang Betz 132:51056160fa4a 128 uint8_t *addr,
Wolfgang Betz 132:51056160fa4a 129 uint8_t *data_length,
Wolfgang Betz 132:51056160fa4a 130 uint8_t *data,
Wolfgang Betz 132:51056160fa4a 131 uint8_t *RSSI);
Wolfgang Betz 165:3576598c0889 132 ble_error_t createConnection(void);
Wolfgang Betz 165:3576598c0889 133
Wolfgang Betz 130:770ce14d3d15 134 void setConnectionHandle(uint16_t con_handle);
Wolfgang Betz 130:770ce14d3d15 135 uint16_t getConnectionHandle(void);
Wolfgang Betz 130:770ce14d3d15 136
Wolfgang Betz 130:770ce14d3d15 137 bool getIsSetAddress();
Wolfgang Betz 130:770ce14d3d15 138
Wolfgang Betz 130:770ce14d3d15 139 Timeout getAdvTimeout(void) const {
Wolfgang Betz 130:770ce14d3d15 140 return advTimeout;
Wolfgang Betz 130:770ce14d3d15 141 }
Wolfgang Betz 130:770ce14d3d15 142 uint8_t getAdvToFlag(void) {
Wolfgang Betz 130:770ce14d3d15 143 return AdvToFlag;
Wolfgang Betz 130:770ce14d3d15 144 }
Wolfgang Betz 130:770ce14d3d15 145 void setAdvToFlag(void);
Wolfgang Betz 130:770ce14d3d15 146
Wolfgang Betz 130:770ce14d3d15 147 void Process(void);
Wolfgang Betz 130:770ce14d3d15 148
Andrea Palmieri 160:174387d53809 149 GapScanningParams* getScanningParams(void);
Andrea Palmieri 160:174387d53809 150
Wolfgang Betz 130:770ce14d3d15 151 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
Wolfgang Betz 130:770ce14d3d15 152
Wolfgang Betz 130:770ce14d3d15 153 private:
Wolfgang Betz 130:770ce14d3d15 154 uint16_t m_connectionHandle;
Wolfgang Betz 132:51056160fa4a 155 AddressType_t addr_type;
Wolfgang Betz 132:51056160fa4a 156 Address_t _peerAddr;
Andrea Palmieri 219:9981f62cdb1a 157 AddressType_t _peerAddrType;
Wolfgang Betz 130:770ce14d3d15 158 uint8_t bdaddr[BDADDR_SIZE];
Wolfgang Betz 132:51056160fa4a 159 bool _scanning;
Wolfgang Betz 132:51056160fa4a 160 bool _connecting;
Wolfgang Betz 130:770ce14d3d15 161 bool isSetAddress;
Wolfgang Betz 130:770ce14d3d15 162 uint8_t deviceAppearance[2];
Wolfgang Betz 130:770ce14d3d15 163
Wolfgang Betz 130:770ce14d3d15 164 uint8_t local_name_length;
Andrea Palmieri 212:37a7e289ee3d 165 uint8_t local_name[LOCAL_NAME_MAX_SIZE];
Wolfgang Betz 130:770ce14d3d15 166
Wolfgang Betz 130:770ce14d3d15 167 uint8_t servUuidlength;
Wolfgang Betz 130:770ce14d3d15 168 uint8_t servUuidData[UUID_BUFFER_SIZE];
Wolfgang Betz 130:770ce14d3d15 169
Wolfgang Betz 130:770ce14d3d15 170 uint8_t AdvLen;
Wolfgang Betz 130:770ce14d3d15 171 uint8_t AdvData[ADV_DATA_MAX_SIZE];
Wolfgang Betz 130:770ce14d3d15 172
Wolfgang Betz 130:770ce14d3d15 173 Timeout advTimeout;
Wolfgang Betz 130:770ce14d3d15 174 bool AdvToFlag;
Wolfgang Betz 130:770ce14d3d15 175
Andrea Palmieri 219:9981f62cdb1a 176 static uint16_t SCAN_DURATION_UNITS_TO_MSEC(uint16_t duration) {
Andrea Palmieri 219:9981f62cdb1a 177 return (duration * 625) / 1000;
Andrea Palmieri 219:9981f62cdb1a 178 }
Andrea Palmieri 219:9981f62cdb1a 179
Andrea Palmieri 219:9981f62cdb1a 180 uint16_t scanInterval;
Andrea Palmieri 219:9981f62cdb1a 181 uint16_t scanWindow;
Andrea Palmieri 222:aead10288880 182 uint16_t advInterval;
Andrea Palmieri 219:9981f62cdb1a 183 uint16_t conn_min_interval;
Andrea Palmieri 219:9981f62cdb1a 184 uint16_t conn_max_interval;
Andrea Palmieri 222:aead10288880 185 void setAdvParameters(void);
Andrea Palmieri 219:9981f62cdb1a 186 void setConnectionParameters(void);
Andrea Palmieri 219:9981f62cdb1a 187
Andrea Palmieri 219:9981f62cdb1a 188 Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
Andrea Palmieri 219:9981f62cdb1a 189 Gap::ScanningPolicyMode_t scanningPolicyMode;
Andrea Palmieri 219:9981f62cdb1a 190
Andrea Palmieri 219:9981f62cdb1a 191 Whitelist_t whitelistAddresses;
Andrea Palmieri 219:9981f62cdb1a 192
Wolfgang Betz 130:770ce14d3d15 193 BlueNRGGap() {
Wolfgang Betz 130:770ce14d3d15 194 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
Andrea Palmieri 218:25368f053411 195 addr_type = BLEProtocol::AddressType::RANDOM_STATIC;
Andrea Palmieri 219:9981f62cdb1a 196
Andrea Palmieri 219:9981f62cdb1a 197 /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
Andrea Palmieri 219:9981f62cdb1a 198 advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 220:6eb53072d82b 199 scanningPolicyMode = Gap::SCAN_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 219:9981f62cdb1a 200
Wolfgang Betz 130:770ce14d3d15 201 isSetAddress = false;
Andrea Palmieri 218:25368f053411 202 memset(deviceAppearance, 0, sizeof(deviceAppearance));
Wolfgang Betz 130:770ce14d3d15 203 }
Wolfgang Betz 130:770ce14d3d15 204
Wolfgang Betz 130:770ce14d3d15 205 BlueNRGGap(BlueNRGGap const &);
Wolfgang Betz 130:770ce14d3d15 206 void operator=(BlueNRGGap const &);
Wolfgang Betz 130:770ce14d3d15 207 };
Wolfgang Betz 130:770ce14d3d15 208
Wolfgang Betz 130:770ce14d3d15 209 #endif // ifndef __BLUENRG_GAP_H__