Fork of ble-x-nucleo-idb0xa1 with changes required by BleStarMbed

Dependents:   ble-star-mbed

Committer:
lorevee
Date:
Tue Feb 20 11:07:16 2018 +0000
Revision:
0:ac0b0725c6fa
Initial commit

Who changed what in which revision?

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