Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Gap.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 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 #include "ble/Gap.h" 00018 00019 namespace { 00020 00021 ble_error_t convert_address_type( 00022 Gap::PeerAddressType_t input_type, 00023 const BLEProtocol::AddressBytes_t address, 00024 BLEProtocol::AddressType_t& output_type 00025 ) { 00026 typedef Gap::RandomAddressType_t RandomAddressType_t; 00027 typedef Gap::PeerAddressType_t PeerAddressType_t; 00028 typedef BLEProtocol::AddressType LegacyAddressType_t; 00029 00030 // best effort; peerAddrTypeIn should not be used when privacy is on. 00031 switch(input_type.value()) { 00032 case PeerAddressType_t::PUBLIC: 00033 case PeerAddressType_t::PUBLIC_IDENTITY: 00034 output_type = LegacyAddressType_t::PUBLIC; 00035 break; 00036 case PeerAddressType_t::RANDOM: { 00037 RandomAddressType_t random_address_type(RandomAddressType_t::STATIC); 00038 ble_error_t err = Gap::getRandomAddressType(address, &random_address_type); 00039 if (err) { 00040 return err; 00041 } 00042 switch (random_address_type.value()) { 00043 case RandomAddressType_t::STATIC: 00044 output_type = LegacyAddressType_t::RANDOM_STATIC; 00045 break; 00046 case RandomAddressType_t::NON_RESOLVABLE_PRIVATE: 00047 output_type = LegacyAddressType_t::RANDOM_PRIVATE_NON_RESOLVABLE; 00048 break; 00049 case RandomAddressType_t::RESOLVABLE_PRIVATE: 00050 output_type = LegacyAddressType_t::RANDOM_PRIVATE_RESOLVABLE; 00051 break; 00052 } 00053 break; 00054 } 00055 case PeerAddressType_t::RANDOM_STATIC_IDENTITY: 00056 output_type = LegacyAddressType_t::RANDOM_STATIC; 00057 break; 00058 } 00059 00060 return BLE_ERROR_NONE; 00061 } 00062 00063 Gap::PeerAddressType_t convert_legacy_address_type( 00064 BLEProtocol::AddressType_t legacy_address 00065 ) { 00066 if (legacy_address == BLEProtocol::AddressType::PUBLIC) { 00067 return Gap::PeerAddressType_t::PUBLIC; 00068 } else { 00069 return Gap::PeerAddressType_t::RANDOM; 00070 } 00071 } 00072 00073 } 00074 00075 const Gap::PeripheralPrivacyConfiguration_t Gap::default_peripheral_privacy_configuration = { 00076 /* use_non_resolvable_random_address */ false, 00077 /* resolution_strategy */ PeripheralPrivacyConfiguration_t::PERFORM_PAIRING_PROCEDURE 00078 }; 00079 00080 const Gap::CentralPrivacyConfiguration_t Gap::default_central_privacy_configuration = { 00081 /* use_non_resolvable_random_address */ false, 00082 /* resolution_strategy */ CentralPrivacyConfiguration_t::DO_NOT_RESOLVE 00083 }; 00084 00085 void Gap::processConnectionEvent( 00086 Handle_t handle, 00087 Role_t role, 00088 PeerAddressType_t peerAddrType, 00089 const BLEProtocol::AddressBytes_t peerAddr, 00090 BLEProtocol::AddressType_t ownAddrType, 00091 const BLEProtocol::AddressBytes_t ownAddr, 00092 const ConnectionParams_t *connectionParams, 00093 const uint8_t *peerResolvableAddr, 00094 const uint8_t *localResolvableAddr 00095 ) { 00096 /* Update Gap state */ 00097 state.advertising = 0; 00098 state.connected = 1; 00099 ++connectionCount; 00100 00101 ConnectionCallbackParams_t callbackParams( 00102 handle, 00103 role, 00104 peerAddrType, 00105 peerAddr, 00106 ownAddrType, 00107 ownAddr, 00108 connectionParams, 00109 peerResolvableAddr, 00110 localResolvableAddr 00111 ); 00112 00113 connectionCallChain.call(&callbackParams); 00114 } 00115 00116 void Gap::processAdvertisementReport( 00117 const BLEProtocol::AddressBytes_t peerAddr, 00118 int8_t rssi, 00119 bool isScanResponse, 00120 GapAdvertisingParams::AdvertisingType_t type, 00121 uint8_t advertisingDataLen, 00122 const uint8_t *advertisingData, 00123 PeerAddressType_t addressType 00124 ) { 00125 AdvertisementCallbackParams_t params; 00126 00127 memcpy(params.peerAddr, peerAddr, ADDR_LEN); 00128 params.rssi = rssi; 00129 params.isScanResponse = isScanResponse; 00130 params.type = type; 00131 params.advertisingDataLen = advertisingDataLen; 00132 params.advertisingData = advertisingData; 00133 params.peerAddrType = addressType; 00134 00135 convert_address_type( 00136 addressType, 00137 peerAddr, 00138 params.addressType 00139 ); 00140 00141 onAdvertisementReport.call(¶ms); 00142 } 00143 00144 00145 #if defined(__GNUC__) && !defined(__CC_ARM) 00146 #pragma GCC diagnostic push 00147 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00148 #elif defined(__CC_ARM) 00149 #pragma push 00150 #pragma diag_suppress 1361 00151 #endif 00152 00153 Gap::AdvertisementCallbackParams_t::AdvertisementCallbackParams_t() : 00154 peerAddr(), 00155 rssi(), 00156 isScanResponse(), 00157 type(), 00158 advertisingDataLen(0), 00159 advertisingData(NULL), 00160 addressType(), 00161 peerAddrType(PeerAddressType_t::PUBLIC) 00162 { 00163 } 00164 00165 ble_error_t Gap::getRandomAddressType( 00166 const BLEProtocol::AddressBytes_t address, 00167 RandomAddressType_t* type 00168 ) { 00169 // see section Device address in Bluetooth Link Layer specification 00170 // (Vol 6 - Part B) 00171 switch (address[5] >> 6) { 00172 case 0x03: 00173 *type = RandomAddressType_t::STATIC; 00174 return BLE_ERROR_NONE; 00175 case 0x00: 00176 *type = RandomAddressType_t::NON_RESOLVABLE_PRIVATE; 00177 return BLE_ERROR_NONE; 00178 case 0x01: 00179 *type = RandomAddressType_t::RESOLVABLE_PRIVATE; 00180 return BLE_ERROR_NONE; 00181 default: 00182 return BLE_ERROR_INVALID_PARAM; 00183 } 00184 } 00185 00186 Gap::ConnectionCallbackParams_t::ConnectionCallbackParams_t( 00187 Handle_t handleIn, 00188 Role_t roleIn, 00189 BLEProtocol::AddressType_t peerAddrTypeIn, 00190 const uint8_t *peerAddrIn, 00191 BLEProtocol::AddressType_t ownAddrTypeIn, 00192 const uint8_t *ownAddrIn, 00193 const ConnectionParams_t *connectionParamsIn, 00194 const uint8_t *peerResolvableAddrIn, 00195 const uint8_t *localResolvableAddrIn 00196 ) : handle(handleIn), 00197 role(roleIn), 00198 peerAddrType(peerAddrTypeIn), 00199 peerAddr(), 00200 ownAddrType(ownAddrTypeIn), 00201 ownAddr(), 00202 connectionParams(connectionParamsIn), 00203 peerResolvableAddr(), 00204 localResolvableAddr(), 00205 peerAddressType(convert_legacy_address_type(peerAddrTypeIn)) 00206 { 00207 constructor_helper( 00208 peerAddrIn, 00209 ownAddrIn, 00210 peerResolvableAddrIn, 00211 localResolvableAddrIn 00212 ); 00213 } 00214 00215 Gap::ConnectionCallbackParams_t::ConnectionCallbackParams_t( 00216 Handle_t handleIn, 00217 Role_t roleIn, 00218 PeerAddressType_t peerAddrTypeIn, 00219 const uint8_t *peerAddrIn, 00220 BLEProtocol::AddressType_t ownAddrTypeIn, 00221 const uint8_t *ownAddrIn, 00222 const ConnectionParams_t *connectionParamsIn, 00223 const uint8_t *peerResolvableAddrIn, 00224 const uint8_t *localResolvableAddrIn 00225 ) : handle(handleIn), 00226 role(roleIn), 00227 peerAddrType(), 00228 peerAddr(), 00229 ownAddrType(ownAddrTypeIn), 00230 ownAddr(), 00231 connectionParams(connectionParamsIn), 00232 peerResolvableAddr(), 00233 localResolvableAddr(), 00234 peerAddressType(peerAddrTypeIn) 00235 { 00236 constructor_helper( 00237 peerAddrIn, 00238 ownAddrIn, 00239 peerResolvableAddrIn, 00240 localResolvableAddrIn 00241 ); 00242 00243 convert_address_type(peerAddrTypeIn, peerAddrIn, peerAddrType); 00244 } 00245 00246 void Gap::ConnectionCallbackParams_t::constructor_helper( 00247 const uint8_t *peerAddrIn, 00248 const uint8_t *ownAddrIn, 00249 const uint8_t *peerResolvableAddrIn, 00250 const uint8_t *localResolvableAddrIn 00251 ) { 00252 memcpy(peerAddr, peerAddrIn, ADDR_LEN); 00253 00254 if (ownAddrIn) { 00255 memcpy(ownAddr, ownAddrIn, ADDR_LEN); 00256 } else { 00257 memset(ownAddr, 0, ADDR_LEN); 00258 } 00259 00260 if (peerResolvableAddrIn) { 00261 memcpy(peerResolvableAddr, peerResolvableAddrIn, ADDR_LEN); 00262 } else { 00263 memset(ownAddr, 0, ADDR_LEN); 00264 } 00265 00266 if (localResolvableAddrIn) { 00267 memcpy(localResolvableAddr, localResolvableAddrIn, ADDR_LEN); 00268 } else { 00269 memset(ownAddr, 0, ADDR_LEN); 00270 } 00271 } 00272 00273 ble_error_t Gap::connect( 00274 const BLEProtocol::AddressBytes_t peerAddr, 00275 DeprecatedAddressType_t peerAddrType, 00276 const ConnectionParams_t *connectionParams, 00277 const GapScanningParams *scanParams 00278 ) { 00279 return connect( 00280 peerAddr, 00281 (BLEProtocol::AddressType_t) peerAddrType, 00282 connectionParams, 00283 scanParams 00284 ); 00285 } 00286 00287 void Gap::processConnectionEvent( 00288 Handle_t handle, 00289 Role_t role, 00290 BLEProtocol::AddressType_t peerAddrType, 00291 const BLEProtocol::AddressBytes_t peerAddr, 00292 BLEProtocol::AddressType_t ownAddrType, 00293 const BLEProtocol::AddressBytes_t ownAddr, 00294 const ConnectionParams_t *connectionParams, 00295 const uint8_t *peerResolvableAddr, 00296 const uint8_t *localResolvableAddr 00297 ) { 00298 /* Update Gap state */ 00299 state.advertising = 0; 00300 state.connected = 1; 00301 ++connectionCount; 00302 00303 ConnectionCallbackParams_t callbackParams( 00304 handle, 00305 role, 00306 peerAddrType, 00307 peerAddr, 00308 ownAddrType, 00309 ownAddr, 00310 connectionParams, 00311 peerResolvableAddr, 00312 localResolvableAddr 00313 ); 00314 00315 connectionCallChain.call(&callbackParams); 00316 } 00317 00318 void Gap::processAdvertisementReport( 00319 const BLEProtocol::AddressBytes_t peerAddr, 00320 int8_t rssi, 00321 bool isScanResponse, 00322 GapAdvertisingParams::AdvertisingType_t type, 00323 uint8_t advertisingDataLen, 00324 const uint8_t *advertisingData, 00325 BLEProtocol::AddressType_t addressType 00326 ) { 00327 AdvertisementCallbackParams_t params; 00328 memcpy(params.peerAddr, peerAddr, ADDR_LEN); 00329 params.rssi = rssi; 00330 params.isScanResponse = isScanResponse; 00331 params.type = type; 00332 params.advertisingDataLen = advertisingDataLen; 00333 params.advertisingData = advertisingData; 00334 params.addressType = addressType; 00335 00336 params.peerAddrType = convert_legacy_address_type(addressType); 00337 onAdvertisementReport.call(¶ms); 00338 } 00339 00340 #if defined(__GNUC__) && !defined(__CC_ARM) 00341 #pragma GCC diagnostic pop 00342 #elif defined(__CC_ARM) 00343 #pragma pop 00344 #endif
Generated on Tue Jul 12 2022 12:44:14 by
