bluetooth control motor

Dependents:   BLE_LED_IDB0XA1_demo MOTOR_BLE_V2 Motor_Ble_v1 Motor_Ble_v10223 ... more

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
barry210110
Date:
Sun Feb 04 06:55:14 2018 +0000
Revision:
309:492635ae323d
Parent:
278:a5209d8cfd61
final

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 ******************************************************************************
Vincent Coubard 255:97adb68decd4 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>
Vincent Coubard 255:97adb68decd4 33 */
Vincent Coubard 255:97adb68decd4 34
Wolfgang Betz 130:770ce14d3d15 35 #ifndef __BLUENRG_GAP_H__
Wolfgang Betz 130:770ce14d3d15 36 #define __BLUENRG_GAP_H__
Wolfgang Betz 130:770ce14d3d15 37
Vincent Coubard 278:a5209d8cfd61 38 #ifdef YOTTA_CFG_MBED_OS
Vincent Coubard 278:a5209d8cfd61 39 #include "mbed-drivers/mbed.h"
Vincent Coubard 278:a5209d8cfd61 40 #else
Vincent Coubard 278:a5209d8cfd61 41 #include "mbed.h"
Vincent Coubard 278:a5209d8cfd61 42 #endif
Wolfgang Betz 131:e09947216ccb 43 #include "ble/blecommon.h"
Wolfgang Betz 130:770ce14d3d15 44 #include "btle.h"
Wolfgang Betz 131:e09947216ccb 45 #include "ble/GapAdvertisingParams.h"
Wolfgang Betz 131:e09947216ccb 46 #include "ble/GapAdvertisingData.h"
Wolfgang Betz 132:51056160fa4a 47 #include "ble/Gap.h"
Wolfgang Betz 130:770ce14d3d15 48
Wolfgang Betz 130:770ce14d3d15 49 #define BLE_CONN_HANDLE_INVALID 0x0
Wolfgang Betz 130:770ce14d3d15 50 #define BDADDR_SIZE 6
Wolfgang Betz 130:770ce14d3d15 51
Andrea Palmieri 224:d8669b6b3175 52 #define BLUENRG_GAP_ADV_INTERVAL_MIN (0x0020)
Andrea Palmieri 229:9981f62cdb1a 53 #define BLUENRG_GAP_ADV_INTERVAL_MAX (0x4000)
Andrea Palmieri 229:9981f62cdb1a 54 #define BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN (0x00A0)
Wolfgang Betz 130:770ce14d3d15 55
Wolfgang Betz 132:51056160fa4a 56 // Scanning and Connection Params used by Central for creating connection
Andrea Palmieri 217:23870c55b07e 57 #define GAP_OBSERVATION_PROC (0x80)
Wolfgang Betz 132:51056160fa4a 58
Andrea Palmieri 220:e02936f0d4c7 59 #define SCAN_P (0x0010)
Andrea Palmieri 220:e02936f0d4c7 60 #define SCAN_L (0x0010)
Andrea Palmieri 220:e02936f0d4c7 61 #define SUPERV_TIMEOUT (0xC80)
Wolfgang Betz 132:51056160fa4a 62 #define CONN_P(x) ((int)((x)/1.25f))
Wolfgang Betz 132:51056160fa4a 63 #define CONN_L(x) ((int)((x)/0.625f))
Andrea Palmieri 220:e02936f0d4c7 64 #define CONN_P1 ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
Andrea Palmieri 220:e02936f0d4c7 65 #define CONN_P2 ((int)(_advParams.getInterval()+5)/1.25f)//(0x4C)//(0x6C)
Andrea Palmieri 229:9981f62cdb1a 66 #define CONN_L1 (0x0008)
Andrea Palmieri 229:9981f62cdb1a 67 #define CONN_L2 (0x0008)
Andrea Palmieri 229:9981f62cdb1a 68 #define GUARD_INT 5 //msec
Andrea Palmieri 234:3f6bba52b307 69 #define MIN_INT_CONN 0x0006 //=>7.5msec
Andrea Palmieri 234:3f6bba52b307 70 #define MAX_INT_CONN 0x0C80 //=>4000msec
Andrea Palmieri 234:3f6bba52b307 71 #define DEF_INT_CONN 0x0140 //=>400msec (default value for connection interval)
Andrea Palmieri 189:30fa96fa8ec4 72
Wolfgang Betz 130:770ce14d3d15 73 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 74 /*!
Wolfgang Betz 130:770ce14d3d15 75 \brief
Wolfgang Betz 130:770ce14d3d15 76
Wolfgang Betz 130:770ce14d3d15 77 */
Wolfgang Betz 130:770ce14d3d15 78 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 79 class BlueNRGGap : public Gap
Wolfgang Betz 130:770ce14d3d15 80 {
Wolfgang Betz 130:770ce14d3d15 81 public:
Wolfgang Betz 130:770ce14d3d15 82 static BlueNRGGap &getInstance() {
Wolfgang Betz 130:770ce14d3d15 83 static BlueNRGGap m_instance;
Wolfgang Betz 130:770ce14d3d15 84 return m_instance;
Wolfgang Betz 130:770ce14d3d15 85 }
Wolfgang Betz 130:770ce14d3d15 86
Wolfgang Betz 132:51056160fa4a 87 enum Reason_t {
Wolfgang Betz 132:51056160fa4a 88 DEVICE_FOUND,
Wolfgang Betz 132:51056160fa4a 89 DISCOVERY_COMPLETE
Wolfgang Betz 132:51056160fa4a 90 };
Vincent Coubard 255:97adb68decd4 91
Wolfgang Betz 130:770ce14d3d15 92 /* Functions that must be implemented from Gap */
Vincent Coubard 275:cb9a052c769b 93 virtual ble_error_t setAddress(addr_type_t type, const BLEProtocol::AddressBytes_t address);
Vincent Coubard 265:46333d70cf81 94 virtual ble_error_t getAddress(BLEProtocol::AddressType_t *typeP, BLEProtocol::AddressBytes_t address);
Wolfgang Betz 130:770ce14d3d15 95 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
Wolfgang Betz 130:770ce14d3d15 96 virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
Wolfgang Betz 130:770ce14d3d15 97 virtual ble_error_t stopAdvertising(void);
Wolfgang Betz 130:770ce14d3d15 98 virtual ble_error_t stopScan();
Andrea Palmieri 224:d8669b6b3175 99 virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_INTERVAL_MIN);}
Andrea Palmieri 224:d8669b6b3175 100 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_NONCON_INTERVAL_MIN);}
Andrea Palmieri 224:d8669b6b3175 101 virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLUENRG_GAP_ADV_INTERVAL_MAX);}
Wolfgang Betz 130:770ce14d3d15 102 virtual ble_error_t disconnect(DisconnectionReason_t reason);
Wolfgang Betz 130:770ce14d3d15 103 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
Wolfgang Betz 130:770ce14d3d15 104 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
Wolfgang Betz 130:770ce14d3d15 105 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
Wolfgang Betz 130:770ce14d3d15 106 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
Wolfgang Betz 130:770ce14d3d15 107
Wolfgang Betz 130:770ce14d3d15 108 virtual ble_error_t setDeviceName(const uint8_t *deviceName);
Wolfgang Betz 130:770ce14d3d15 109 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
Wolfgang Betz 130:770ce14d3d15 110 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
Wolfgang Betz 130:770ce14d3d15 111 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
Andrea Palmieri 229:9981f62cdb1a 112
Andrea Palmieri 229:9981f62cdb1a 113 virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
Andrea Palmieri 229:9981f62cdb1a 114 virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
Andrea Palmieri 229:9981f62cdb1a 115 virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
Andrea Palmieri 229:9981f62cdb1a 116 virtual ScanningPolicyMode_t getScanningPolicyMode(void) const;
Andrea Palmieri 229:9981f62cdb1a 117
Wolfgang Betz 130:770ce14d3d15 118 virtual ble_error_t setTxPower(int8_t txPower);
Wolfgang Betz 130:770ce14d3d15 119 virtual void getPermittedTxPowerValues(const int8_t **, size_t *);
Andrea Palmieri 229:9981f62cdb1a 120
Wolfgang Betz 132:51056160fa4a 121 virtual ble_error_t connect(const Address_t peerAddr,
Wolfgang Betz 132:51056160fa4a 122 Gap::AddressType_t peerAddrType,
Wolfgang Betz 132:51056160fa4a 123 const ConnectionParams_t *connectionParams,
Wolfgang Betz 132:51056160fa4a 124 const GapScanningParams *scanParams);
Andrea Palmieri 229:9981f62cdb1a 125
Andrea Palmieri 229:9981f62cdb1a 126 virtual ble_error_t reset(void);
Wolfgang Betz 130:770ce14d3d15 127
Wolfgang Betz 132:51056160fa4a 128 void Discovery_CB(Reason_t reason,
Wolfgang Betz 132:51056160fa4a 129 uint8_t adv_type,
Andrea Palmieri 229:9981f62cdb1a 130 uint8_t addr_type,
Wolfgang Betz 132:51056160fa4a 131 uint8_t *addr,
Wolfgang Betz 132:51056160fa4a 132 uint8_t *data_length,
Wolfgang Betz 132:51056160fa4a 133 uint8_t *data,
Wolfgang Betz 132:51056160fa4a 134 uint8_t *RSSI);
Wolfgang Betz 165:3576598c0889 135 ble_error_t createConnection(void);
Wolfgang Betz 165:3576598c0889 136
Wolfgang Betz 130:770ce14d3d15 137 void setConnectionHandle(uint16_t con_handle);
Wolfgang Betz 130:770ce14d3d15 138 uint16_t getConnectionHandle(void);
Vincent Coubard 255:97adb68decd4 139
Wolfgang Betz 130:770ce14d3d15 140 bool getIsSetAddress();
Vincent Coubard 255:97adb68decd4 141
Wolfgang Betz 130:770ce14d3d15 142 Timeout getAdvTimeout(void) const {
Wolfgang Betz 130:770ce14d3d15 143 return advTimeout;
Wolfgang Betz 130:770ce14d3d15 144 }
Wolfgang Betz 130:770ce14d3d15 145 uint8_t getAdvToFlag(void) {
Wolfgang Betz 130:770ce14d3d15 146 return AdvToFlag;
Wolfgang Betz 130:770ce14d3d15 147 }
Wolfgang Betz 130:770ce14d3d15 148 void setAdvToFlag(void);
Vincent Coubard 255:97adb68decd4 149
Wolfgang Betz 130:770ce14d3d15 150 void Process(void);
Wolfgang Betz 130:770ce14d3d15 151
Andrea Palmieri 160:174387d53809 152 GapScanningParams* getScanningParams(void);
Andrea Palmieri 160:174387d53809 153
Wolfgang Betz 130:770ce14d3d15 154 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
Wolfgang Betz 130:770ce14d3d15 155
Vincent Coubard 256:146bb175ce80 156 void setConnectionInterval(uint16_t interval);
Vincent Coubard 272:4639da7705e1 157 void setGapRole(Role_t role);
Vincent Coubard 256:146bb175ce80 158
Wolfgang Betz 130:770ce14d3d15 159 private:
Wolfgang Betz 130:770ce14d3d15 160 uint16_t m_connectionHandle;
Vincent Coubard 272:4639da7705e1 161 Role_t gapRole;
Wolfgang Betz 132:51056160fa4a 162 AddressType_t addr_type;
Wolfgang Betz 132:51056160fa4a 163 Address_t _peerAddr;
Andrea Palmieri 229:9981f62cdb1a 164 AddressType_t _peerAddrType;
Wolfgang Betz 130:770ce14d3d15 165 uint8_t bdaddr[BDADDR_SIZE];
Wolfgang Betz 132:51056160fa4a 166 bool _scanning;
Wolfgang Betz 132:51056160fa4a 167 bool _connecting;
Wolfgang Betz 130:770ce14d3d15 168 bool isSetAddress;
Wolfgang Betz 130:770ce14d3d15 169 uint8_t deviceAppearance[2];
Wolfgang Betz 130:770ce14d3d15 170
Wolfgang Betz 130:770ce14d3d15 171 Timeout advTimeout;
Wolfgang Betz 130:770ce14d3d15 172 bool AdvToFlag;
Wolfgang Betz 130:770ce14d3d15 173
Andrea Palmieri 229:9981f62cdb1a 174 static uint16_t SCAN_DURATION_UNITS_TO_MSEC(uint16_t duration) {
Andrea Palmieri 229:9981f62cdb1a 175 return (duration * 625) / 1000;
Andrea Palmieri 229:9981f62cdb1a 176 }
Andrea Palmieri 229:9981f62cdb1a 177
Andrea Palmieri 229:9981f62cdb1a 178 uint16_t scanInterval;
Andrea Palmieri 229:9981f62cdb1a 179 uint16_t scanWindow;
Andrea Palmieri 232:aead10288880 180 uint16_t advInterval;
Andrea Palmieri 251:86df2c289e7c 181 uint16_t slaveConnIntervMin;
Andrea Palmieri 251:86df2c289e7c 182 uint16_t slaveConnIntervMax;
Andrea Palmieri 229:9981f62cdb1a 183 uint16_t conn_min_interval;
Andrea Palmieri 229:9981f62cdb1a 184 uint16_t conn_max_interval;
Andrea Palmieri 232:aead10288880 185 void setAdvParameters(void);
Andrea Palmieri 229:9981f62cdb1a 186 void setConnectionParameters(void);
Andrea Palmieri 229:9981f62cdb1a 187
Andrea Palmieri 229:9981f62cdb1a 188 Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
Andrea Palmieri 229:9981f62cdb1a 189 Gap::ScanningPolicyMode_t scanningPolicyMode;
Andrea Palmieri 229:9981f62cdb1a 190
Andrea Palmieri 229:9981f62cdb1a 191 Whitelist_t whitelistAddresses;
Andrea Palmieri 229:9981f62cdb1a 192
Andrea Palmieri 238:b6454c839f42 193 ble_error_t updateAdvertisingData(void);
Andrea Palmieri 238:b6454c839f42 194
Vincent Coubard 270:ca649990a830 195 BlueNRGGap() {
Wolfgang Betz 130:770ce14d3d15 196 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
Andrea Palmieri 228:25368f053411 197 addr_type = BLEProtocol::AddressType::RANDOM_STATIC;
Andrea Palmieri 229:9981f62cdb1a 198
Andrea Palmieri 229:9981f62cdb1a 199 /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
Andrea Palmieri 229:9981f62cdb1a 200 advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 230:6eb53072d82b 201 scanningPolicyMode = Gap::SCAN_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 229:9981f62cdb1a 202
Wolfgang Betz 130:770ce14d3d15 203 isSetAddress = false;
Andrea Palmieri 228:25368f053411 204 memset(deviceAppearance, 0, sizeof(deviceAppearance));
Wolfgang Betz 130:770ce14d3d15 205 }
Wolfgang Betz 130:770ce14d3d15 206
Wolfgang Betz 130:770ce14d3d15 207 BlueNRGGap(BlueNRGGap const &);
Wolfgang Betz 130:770ce14d3d15 208 void operator=(BlueNRGGap const &);
Vincent Coubard 255:97adb68decd4 209
Vincent Coubard 255:97adb68decd4 210 GapAdvertisingData _advData;
Vincent Coubard 270:ca649990a830 211 GapAdvertisingData _scanResponse;
Wolfgang Betz 130:770ce14d3d15 212 };
Wolfgang Betz 130:770ce14d3d15 213
Vincent Coubard 255:97adb68decd4 214 #endif // ifndef __BLUENRG_GAP_H__