50 ts_creation(r.getTimestamp()),
54 ts_last_discovery = ts_creation;
58 allowDisconnect =
false;
59 clearSMPStates(
false );
86 adapter.removeAllStatusListener(*
this);
91 return adapter.getSharedDevice(*
this);
94 bool BTDevice::addAdvService(std::shared_ptr<uuid_t>
const &uuid) noexcept
96 if( 0 > findAdvService(uuid) ) {
97 advServices.push_back(uuid);
102 bool BTDevice::addAdvServices(
jau::darray<std::shared_ptr<uuid_t>>
const & services) noexcept
105 for(
size_t j=0; j<services.size(); j++) {
106 const std::shared_ptr<uuid_t> uuid = services.at(j);
107 res = addAdvService(uuid) || res;
112 int BTDevice::findAdvService(std::shared_ptr<uuid_t>
const &uuid)
const noexcept
114 const size_t size = advServices.size();
115 for (
size_t i = 0; i < size; i++) {
116 const std::shared_ptr<uuid_t> & e = advServices[i];
117 if (
nullptr != e && *uuid == *e ) {
126 std::string res = name;
132 std::shared_ptr<ManufactureSpecificData> res = advMSD;
145 std::string msdstr =
nullptr != advMSD ? advMSD->toString() :
"MSD[null]";
146 std::string out(
"Device["+addressAndType.toString()+
", name['"+name+
149 ", sec[lvl "+
to_string(pairing_data.sec_level_conn)+
", io "+
to_string(pairing_data.ioCap_conn)+
153 "), "+msdstr+
", "+javaObjectToString()+
"]");
154 if( includeDiscoveredServices ) {
156 if( _advServices.
size() > 0 ) {
158 const size_t size = _advServices.
size();
159 for (
size_t i = 0; i < size; i++) {
160 const std::shared_ptr<uuid_t> & e = _advServices[i];
172 const std::lock_guard<std::mutex> lock(mtx_data);
176 ts_last_update = data.getTimestamp();
178 if( data.getAddress() != this->addressAndType.address ) {
179 WARN_PRINT(
"BTDevice::update:: BDADDR update not supported: %s for %s",
180 data.toString().c_str(), this->toString().c_str());
184 if( data.getAddressType() != this->addressAndType.type ) {
185 WARN_PRINT(
"BTDevice::update:: BDADDR_TYPE update not supported: %s for %s",
186 data.toString().c_str(), this->toString().c_str());
190 if( 0 == name.length() || data.getName().length() > name.length() ) {
191 name = data.getName();
196 if( 0 == name.length() ) {
197 name = data.getShortName();
202 if( rssi != data.getRSSI() ) {
203 rssi = data.getRSSI();
208 if( tx_power != data.getTxPower() ) {
209 tx_power = data.getTxPower();
214 if( appearance != data.getAppearance() ) {
215 appearance = data.getAppearance();
220 if( advMSD != data.getManufactureSpecificData() ) {
221 advMSD = data.getManufactureSpecificData();
225 if( addAdvServices( data.getServices() ) ) {
232 const std::lock_guard<std::mutex> lock(mtx_data);
236 ts_last_update = timestamp;
237 if( 0 == name.length() || data.deviceName.length() > name.length() ) {
238 name = data.deviceName;
241 if( appearance != data.appearance ) {
242 appearance = data.appearance;
259 if(
nullptr != connInfo ) {
261 if( rssi != connInfo->
getRSSI() ) {
271 if(
nullptr == sharedInstance ) {
272 ERR_PRINT(
"BTDevice::getConnectionInfo: Device unknown to adapter and not tracked: %s",
toString().c_str());
284 uint16_t conn_interval_min, uint16_t conn_interval_max,
285 uint16_t conn_latency, uint16_t supervision_timeout) noexcept
287 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect);
288 if( !adapter.isPowered() ) {
289 WARN_PRINT(
"BTDevice::connectLE: Adapter not powered: %s, %s", adapter.toString().c_str(), toString().c_str());
295 switch( addressAndType.type ) {
302 switch( leRandomAddressType ) {
306 ERR_PRINT(
"LE Random address type '%s' not supported yet: %s",
307 to_string(leRandomAddressType).c_str(), toString().c_str());
312 ERR_PRINT(
"LE Random address type '%s' not supported yet: %s",
313 to_string(leRandomAddressType).c_str(), toString().c_str());
323 ERR_PRINT(
"Can't connectLE to LE Random address type '%s': %s",
324 to_string(leRandomAddressType).c_str(), toString().c_str());
330 ERR_PRINT(
"Can't connectLE to address type '%s': %s",
to_string(addressAndType.type).c_str(), toString().c_str());
336 ERR_PRINT(
"BTDevice::connectLE: Already connected: %s", toString().c_str());
342 ERR_PRINT(
"BTDevice::connectLE: HCI closed: %s", toString().c_str());
349 bool smp_auto_done = !smp_auto;
350 int smp_auto_count = 0;
358 if( !smp_auto_done ) {
363 io_cap = smp_auto_io_cap;
366 io_cap = smp_auto_io_cap;
371 smp_auto_done =
true;
379 smp_auto_done =
true;
381 pairing_data.ioCap_auto = smp_auto_io_cap;
382 pairing_data.sec_level_user = sec_level;
383 pairing_data.ioCap_user = io_cap;
384 DBG_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.1: lvl %s -> %s, io %s -> %s, %s", smp_auto_count,
392 if( !adapter.lockConnect(*
this,
true , pairing_data.ioCap_user) ) {
393 ERR_PRINT(
"BTDevice::connectLE: adapter::lockConnect() failed: %s", toString().c_str());
398 hci_peer_mac_type, hci_own_mac_type,
400 conn_latency, supervision_timeout);
401 allowDisconnect =
true;
403 WARN_PRINT(
"BTDevice::connectLE: Could not yet create connection: status 0x%2.2X (%s), errno %d, hci-atype[peer %s, own %s] %s on %s",
404 static_cast<uint8_t
>(statusConnect),
to_string(statusConnect).c_str(), errno, strerror(errno),
408 adapter.unlockConnect(*
this);
409 smp_auto_done =
true;
411 ERR_PRINT(
"BTDevice::connectLE: Could not create connection: status 0x%2.2X (%s), errno %d %s, hci-atype[peer %s, own %s] on %s",
412 static_cast<uint8_t
>(statusConnect),
to_string(statusConnect).c_str(), errno, strerror(errno),
416 adapter.unlockConnect(*
this);
417 smp_auto_done =
true;
418 }
else if( smp_auto ) {
420 bool pairing_timeout =
false;
422 std::unique_lock<std::mutex> lock(mtx_pairing);
426 std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
428 DBG_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.2c Wait for SMPPairing: state %s, %s",
429 smp_auto_count,
to_string(pairing_data.state).c_str(), toString().c_str());
432 ERR_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.X Timeout SMPPairing: Disconnecting %s", smp_auto_count, toString().c_str());
433 smp_auto_done =
true;
435 pairing_timeout =
true;
439 pstate = pairing_data.state;
440 DBG_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.2d Wait for SMPPairing: state %s, %s",
441 smp_auto_count,
to_string(pstate).c_str(), toString().c_str());
443 if( pairing_timeout ) {
447 adapter.unlockConnect(*
this);
448 smp_auto_done =
true;
450 DBG_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.X Done: %s", smp_auto_count, toString().c_str());
451 smp_auto_done =
true;
454 if( !smp_auto_done ) {
456 int32_t td_disconnect = 0;
457 DBG_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.3 Failed SMPPairing -> Disconnect: %s", smp_auto_count, toString().c_str());
468 ERR_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.4 Timeout Disconnect td_pairing %d ms: %s",
469 smp_auto_count, td_disconnect, toString().c_str());
472 adapter.unlockConnect(*
this);
473 smp_auto_done =
true;
478 }
while( !smp_auto_done );
482 ERR_PRINT(
"BTDevice::connectLE: SEC AUTO.%d.X Failed SMPPairing -> Disconnect: %s", smp_auto_count, toString().c_str());
489 return statusConnect;
494 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect);
495 if( !adapter.isPowered() ) {
496 WARN_PRINT(
"BTDevice::connectBREDR: Adapter not powered: %s, %s", adapter.toString().c_str(), toString().c_str());
501 ERR_PRINT(
"BTDevice::connectBREDR: Already connected: %s", toString().c_str());
504 if( !addressAndType.isBREDRAddress() ) {
505 ERR_PRINT(
"BTDevice::connectBREDR: Not a BDADDR_BREDR address: %s", toString().c_str());
511 ERR_PRINT(
"BTDevice::connectBREDR: HCI closed: %s", toString().c_str());
517 if( !adapter.lockConnect(*
this,
true , pairing_data.ioCap_user) ) {
518 ERR_PRINT(
"BTDevice::connectBREDR: adapter::lockConnect() failed: %s", toString().c_str());
523 allowDisconnect =
true;
525 ERR_PRINT(
"BTDevice::connectBREDR: Could not create connection: status 0x%2.2X (%s), errno %d %s on %s",
526 static_cast<uint8_t
>(status),
to_string(status).c_str(), errno, strerror(errno), toString().c_str());
527 adapter.unlockConnect(*
this);
542 ERR_PRINT(
"BTDevice::connectDefault: Not a valid address type: %s",
toString().c_str());
547 void BTDevice::notifyConnected(std::shared_ptr<BTDevice> sthis,
const uint16_t handle,
const SMPIOCapability io_cap) noexcept {
550 DBG_PRINT(
"BTDevice::notifyConnected: handle %s -> %s, io %s -> %s, %s",
554 clearSMPStates(
true );
555 allowDisconnect =
true;
557 hciConnHandle = handle;
559 pairing_data.ioCap_conn = io_cap;
564 void BTDevice::notifyLEFeatures(std::shared_ptr<BTDevice> sthis,
const LE_Features features) noexcept {
565 DBG_PRINT(
"BTDevice::notifyLEFeatures: %s, %s",
567 le_features = features;
569 if( addressAndType.isLEAddress() && !l2cap_att.isOpen() ) {
570 std::thread bg(&BTDevice::processL2CAPSetup,
this, sthis);
575 void BTDevice::processL2CAPSetup(std::shared_ptr<BTDevice> sthis) {
576 bool callProcessDeviceReady =
false;
579 const std::unique_lock<std::mutex> lock(mtx_pairing);
590 sec_level = sec_level_user;
596 }
else if( responderLikesEncryption ) {
602 pairing_data.sec_level_conn = sec_level;
603 DBG_PRINT(
"BTDevice::processL2CAPSetup: sec_level_user %s, io_cap_conn %s -> sec_level %s",
608 const bool l2cap_open = l2cap_att.
open(*
this, sec_level);
610 #if SMP_SUPPORTED_BY_OS
613 const bool smp_enc =
false;
615 DBG_PRINT(
"BTDevice::processL2CAPSetup: lvl %s, connect[smp_enc %d, l2cap[open %d, enc %d]]",
616 to_string(sec_level).c_str(), smp_enc, l2cap_open, l2cap_enc);
618 adapter.unlockConnect(*
this);
623 }
else if( !l2cap_enc ) {
624 callProcessDeviceReady =
true;
627 DBG_PRINT(
"BTDevice::processL2CAPSetup: Skipped (not LE) %s",
toString().c_str());
629 if( callProcessDeviceReady ) {
636 void BTDevice::processDeviceReady(std::shared_ptr<BTDevice> sthis,
const uint64_t timestamp) {
641 pmode = pairing_data.mode;
650 std::this_thread::sleep_for(std::chrono::milliseconds(100));
653 const bool res1 = connectGATT(sthis);
659 DBG_PRINT(
"BTDevice::processDeviceReady: ready[GATT %d, unpair %s], %s",
662 adapter.sendDeviceReady(sthis, timestamp);
670 bool BTDevice::checkPairingKeyDistributionComplete(
const std::string& timestamp)
const noexcept {
677 if( pairing_data.use_sc ) {
678 if( pairing_data.keys_init_has == ( pairing_data.keys_init_exp &
_key_mask_sc ) ) {
683 if( pairing_data.keys_init_has == ( pairing_data.keys_init_exp &
_key_mask_legacy ) ) {
690 jau::PLAIN_PRINT(
false,
"[%s] Debug: BTDevice:SMP:KEY_DISTRIBUTION: done %d, address%s",
691 timestamp.c_str(), res,
692 addressAndType.toString().c_str());
695 to_string(pairing_data.keys_init_has).c_str(),
696 to_string(pairing_data.keys_init_exp).c_str(),
697 to_string(pairing_data.keys_resp_has).c_str(),
698 to_string(pairing_data.keys_resp_exp).c_str());
706 const std::unique_lock<std::mutex> lock(mtx_pairing);
712 bool is_device_ready =
false;
714 if( pairing_data.state != claimed_state ) {
716 switch( claimed_state ) {
719 claimed_state = pairing_data.state;
730 DBG_PRINT(
"BTDevice::updatePairingState.1a: state %s [ignored %s, sending dummy reply], mode %s, %s",
732 to_string(pairing_data.mode).c_str(), evt.toString().c_str());
733 claimed_state = pairing_data.state;
744 DBG_PRINT(
"BTDevice::updatePairingState.1b: state %s [ignored %s, sending dummy reply], mode %s, %s",
746 to_string(pairing_data.mode).c_str(), evt.toString().c_str());
747 claimed_state = pairing_data.state;
763 is_device_ready =
true;
771 is_device_ready =
true;
786 jau::PLAIN_PRINT(
false,
"[%s] BTDevice::updatePairingState.2a: ENC_KEY responder set", timestamp.c_str());
787 jau::PLAIN_PRINT(
false,
"[%s] - old %s", timestamp.c_str(), pairing_data.ltk_resp.toString().c_str());
790 pairing_data.ltk_resp = smp_ltk;
792 if( checkPairingKeyDistributionComplete(timestamp) ) {
793 is_device_ready =
true;
799 jau::PLAIN_PRINT(
false,
"[%s] BTDevice::updatePairingState.2b: ENC_KEY initiator set", timestamp.c_str());
800 jau::PLAIN_PRINT(
false,
"[%s] - old %s", timestamp.c_str(), pairing_data.ltk_init.toString().c_str());
803 pairing_data.ltk_init = smp_ltk;
805 if( checkPairingKeyDistributionComplete(timestamp) ) {
806 is_device_ready =
true;
810 if( !is_device_ready ) {
811 claimed_state = pairing_data.state;
816 claimed_state = pairing_data.state;
824 if( pairing_data.state != claimed_state ) {
825 DBG_PRINT(
"BTDevice::updatePairingState.3: state %s -> %s, mode %s -> %s, ready %d, %s",
828 is_device_ready, evt.toString().c_str());
830 pairing_data.mode = mode;
831 pairing_data.state = claimed_state;
833 adapter.sendDevicePairingState(sthis, claimed_state, mode, evt.getTimestamp());
835 if( is_device_ready ) {
836 std::thread dc(&BTDevice::processDeviceReady,
this, sthis, evt.getTimestamp());
839 DBG_PRINT(
"BTDevice::updatePairingState.4: End Complete: state %s, %s",
840 to_string(claimed_state).c_str(), toString().c_str());
842 cv_pairing_state_changed.notify_all();
846 DBG_PRINT(
"BTDevice::updatePairingState.5: End Unchanged: state %s, %s, %s",
848 evt.toString().c_str(), toString().c_str());
854 const std::unique_lock<std::mutex> lock(mtx_pairing);
863 bool is_device_ready =
false;
868 addressAndType.toString().c_str());
869 jau::PLAIN_PRINT(
false,
"[%s] - %s", timestamp.c_str(), msg.toString().c_str());
870 jau::PLAIN_PRINT(
false,
"[%s] - %s", timestamp.c_str(), source.toString().c_str());
871 jau::PLAIN_PRINT(
false,
"[%s] - %s", timestamp.c_str(), toString().c_str());
882 pairing_data.res_requested_sec =
true;
910 pairing_data.use_sc = use_sc;
913 pairing_data.authReqs_init, pairing_data.ioCap_init, pairing_data.oobFlag_init,
914 pairing_data.authReqs_resp, pairing_data.ioCap_resp, pairing_data.oobFlag_resp);
920 jau::PLAIN_PRINT(
false,
"[%s] Debug: BTDevice:hci:SMP.2: address%s: State %s, Mode %s, using SC %d:", timestamp.c_str() ,
921 addressAndType.toString().c_str(),
932 jau::PLAIN_PRINT(
false,
"[%s] - encsz: init %d", timestamp.c_str(), (
int)pairing_data.maxEncsz_init);
933 jau::PLAIN_PRINT(
false,
"[%s] - encsz: resp %d", timestamp.c_str(), (
int)pairing_data.maxEncsz_resp);
964 pairing_data.ltk_resp.properties |= SMPLongTermKeyInfo::Property::RESPONDER;
968 if( pairing_data.use_sc ) {
971 pairing_data.ltk_resp.enc_size = pairing_data.maxEncsz_resp;
972 pairing_data.ltk_resp.ltk = msg1.
getLTK();
979 if( pairing_data.use_sc ) {
982 pairing_data.ltk_init.enc_size = pairing_data.maxEncsz_init;
983 pairing_data.ltk_init.ltk = msg1.
getLTK();
993 pairing_data.ltk_resp.ediv = msg1.
getEDIV();
994 pairing_data.ltk_resp.rand = msg1.
getRand();
998 pairing_data.ltk_init.ediv = msg1.
getEDIV();
999 pairing_data.ltk_init.rand = msg1.
getRand();
1008 pairing_data.irk_resp = msg1.
getIRK();
1011 pairing_data.irk_init = msg1.
getIRK();
1038 pairing_data.csrk_resp.properties |= SMPSignatureResolvingKeyInfo::Property::RESPONDER;
1042 pairing_data.csrk_resp.csrk = msg1.
getCSRK();
1051 pairing_data.csrk_init.csrk = msg1.
getCSRK();
1059 if( checkPairingKeyDistributionComplete(timestamp) ) {
1061 is_device_ready =
true;
1065 if( old_pstate == pstate ) {
1066 jau::PLAIN_PRINT(
false,
"[%s] Debug: BTDevice:hci:SMP.4: Unchanged: address%s",
1068 addressAndType.toString().c_str());
1070 jau::PLAIN_PRINT(
false,
"[%s] Debug: BTDevice:hci:SMP.5: Updated: address%s",
1072 addressAndType.toString().c_str());
1081 to_string(pairing_data.keys_init_has).c_str(),
1082 to_string(pairing_data.keys_init_exp).c_str(),
1083 to_string(pairing_data.keys_resp_has).c_str(),
1084 to_string(pairing_data.keys_resp_exp).c_str());
1087 if( old_pstate == pstate ) {
1091 pairing_data.mode = pmode;
1092 pairing_data.state = pstate;
1094 adapter.sendDevicePairingState(sthis, pstate, pmode, msg.getTimestamp());
1096 if( is_device_ready ) {
1097 std::thread dc(&BTDevice::processDeviceReady,
this, sthis, msg.getTimestamp());
1101 jau::PLAIN_PRINT(
false,
"[%s] Debug: BTDevice:hci:SMP.6: End", timestamp.c_str());
1102 jau::PLAIN_PRINT(
false,
"[%s] - %s", timestamp.c_str(), toString().c_str());
1104 cv_pairing_state_changed.notify_all();
1110 return pairing_data.keys_resp_has;
1112 return pairing_data.keys_init_has;
1118 return responder ? pairing_data.ltk_resp : pairing_data.ltk_init;
1123 ERR_PRINT(
"BTDevice::setLongTermKeyInfo: Already connected: %s", toString().c_str());
1126 const std::unique_lock<std::mutex> lock(mtx_pairing);
1128 if( ltk.isResponder() ) {
1129 pairing_data.ltk_resp = ltk;
1131 pairing_data.ltk_init = ltk;
1133 #if USE_LINUX_BT_SECURITY
1134 BTManager & mngr = adapter.getManager();
1137 #elif SMP_SUPPORTED_BY_OS
1146 return responder ? pairing_data.csrk_resp : pairing_data.csrk_init;
1161 #if USE_LINUX_BT_SECURITY
1164 DBG_PRINT(
"BTDevice::pairDevice: Start: io %s, %s",
to_string(io_cap).c_str(), toString().c_str());
1168 pairing_data.ioCap_conn = io_cap;
1169 const bool res = mngr.
pairDevice(adapter.dev_id, addressAndType, io_cap);
1173 DBG_PRINT(
"BTDevice::pairDevice: End: io %s, %s",
to_string(io_cap).c_str(), toString().c_str());
1175 #elif SMP_SUPPORTED_BY_OS
1184 DBG_PRINT(
"DBTAdapter::setConnSecurityLevel: lvl %s, invalid value.",
to_string(sec_level).c_str());
1188 if( !isValid() || isConnected || allowDisconnect ) {
1189 DBG_PRINT(
"BTDevice::setConnSecurityLevel: lvl %s failed, invalid state %s",
1190 to_string(sec_level).c_str(), toString().c_str());
1194 const bool res =
true;
1195 pairing_data.sec_level_user = sec_level;
1198 DBG_PRINT(
"BTDevice::setConnSecurityLevel: result %d: lvl %s, %s", res,
1200 toString().c_str());
1206 return pairing_data.sec_level_conn;
1211 DBG_PRINT(
"BTDevice::setConnIOCapability: io %s, invalid value.",
to_string(io_cap).c_str());
1215 if( !isValid() || isConnected || allowDisconnect ) {
1216 DBG_PRINT(
"BTDevice::setConnIOCapability: io %s failed, invalid state %s",
1217 to_string(io_cap).c_str(), toString().c_str());
1221 const bool res =
true;
1222 pairing_data.ioCap_user = io_cap;
1225 DBG_PRINT(
"BTDevice::setConnIOCapability: result %d: io %s, %s", res,
1227 toString().c_str());
1234 return pairing_data.ioCap_conn;
1238 if( !isValid() || isConnected || allowDisconnect ) {
1239 DBG_PRINT(
"BTDevice::setConnSecurity: lvl %s, io %s failed, invalid state %s",
1241 to_string(io_cap).c_str(), toString().c_str());
1245 const bool res =
true;
1246 pairing_data.ioCap_user = io_cap;
1247 pairing_data.sec_level_user = sec_level;
1250 DBG_PRINT(
"BTDevice::setConnSecurity: result %d: lvl %s, io %s, %s", res,
1253 toString().c_str());
1260 return setConnSecurity(sec_level, io_cap);
1265 return setConnSecurityLevel(sec_level);
1268 return setConnIOCapability(io_cap);
1275 if( !isValid() || isConnected || allowDisconnect ) {
1276 DBG_PRINT(
"BTDevice::setConnSecurityAuto: io %s failed, invalid state %s",
1277 to_string(iocap_auto).c_str(), toString().c_str());
1283 DBG_PRINT(
"BTDevice::setConnSecurityAuto: io %s failed, user connection sec_level %s or io %s set %s",
1285 to_string(pairing_data.sec_level_user).c_str(),
1286 to_string(pairing_data.ioCap_user).c_str(),
1287 toString().c_str());
1292 const bool res =
true;
1293 pairing_data.ioCap_auto = iocap_auto;
1294 DBG_PRINT(
"BTDevice::setConnSecurityAuto: result %d: io %s, %s", res,
1295 to_string(iocap_auto).c_str(), toString().c_str());
1305 const std::unique_lock<std::mutex> lock(mtx_pairing);
1309 #if USE_LINUX_BT_SECURITY
1312 DBG_PRINT(
"BTDevice:mgmt:SMP: PASSKEY '%d', state %s, result %s",
1315 #elif SMP_SUPPORTED_BY_OS
1321 ERR_PRINT(
"BTDevice:mgmt:SMP: PASSKEY '%d', state %s, SKIPPED (wrong state)",
1322 passkey,
to_string(pairing_data.state).c_str());
1328 const std::unique_lock<std::mutex> lock(mtx_pairing);
1332 #if USE_LINUX_BT_SECURITY
1335 DBG_PRINT(
"BTDevice:mgmt:SMP: PASSKEY NEGATIVE, state %s, result %s",
1338 #elif SMP_SUPPORTED_BY_OS
1344 ERR_PRINT(
"BTDevice:mgmt:SMP: PASSKEY NEGATIVE, state %s, SKIPPED (wrong state)",
1351 const std::unique_lock<std::mutex> lock(mtx_pairing);
1355 #if USE_LINUX_BT_SECURITY
1358 DBG_PRINT(
"BTDevice:mgmt:SMP: CONFIRM '%d', state %s, result %s",
1361 #elif SMP_SUPPORTED_BY_OS
1367 ERR_PRINT(
"BTDevice:mgmt:SMP: CONFIRM '%d', state %s, SKIPPED (wrong state)",
1368 positive,
to_string(pairing_data.state).c_str());
1375 return pairing_data.mode;
1380 return pairing_data.state;
1383 void BTDevice::clearSMPStates(
const bool connected) noexcept {
1384 const std::unique_lock<std::mutex> lock(mtx_pairing);
1387 DBG_PRINT(
"BTDevice::clearSMPStates: Had: %s", toString().c_str());
1400 pairing_data.res_requested_sec =
false;
1401 pairing_data.use_sc =
false;
1406 pairing_data.maxEncsz_resp = 0;
1409 pairing_data.ltk_resp.clear();
1410 pairing_data.irk_resp.clear();
1413 pairing_data.csrk_resp.clear();
1418 pairing_data.maxEncsz_init = 0;
1421 pairing_data.ltk_init.clear();
1422 pairing_data.irk_init.clear();
1423 pairing_data.csrk_init.clear();
1426 void BTDevice::disconnectSMP(
const int caller) noexcept {
1427 #if SMP_SUPPORTED_BY_OS
1428 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_smpHandler);
1429 if(
nullptr != smpHandler ) {
1430 DBG_PRINT(
"BTDevice::disconnectSMP: start (has smpHandler, caller %d)", caller);
1431 smpHandler->disconnect(
false ,
false );
1433 DBG_PRINT(
"BTDevice::disconnectSMP: start (nil smpHandler, caller %d)", caller);
1435 smpHandler =
nullptr;
1436 DBG_PRINT(
"BTDevice::disconnectSMP: end");
1442 bool BTDevice::connectSMP(std::shared_ptr<BTDevice> sthis,
const BTSecurityLevel sec_level) noexcept {
1443 #if SMP_SUPPORTED_BY_OS
1444 if( !isConnected || !allowDisconnect) {
1445 ERR_PRINT(
"BTDevice::connectSMP(%u): Device not connected: %s", sec_level, toString().c_str());
1450 DBG_PRINT(
"BTDevice::connectSMP(%u): SMP Not supported by OS (1): %s", sec_level, toString().c_str());
1458 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_gattHandler);
1459 if(
nullptr != smpHandler ) {
1460 if( smpHandler->isConnected() ) {
1461 return smpHandler->establishSecurity(sec_level);
1463 smpHandler =
nullptr;
1466 smpHandler = std::make_shared<SMPHandler>(sthis);
1467 if( !smpHandler->isConnected() ) {
1468 ERR_PRINT(
"BTDevice::connectSMP: Connection failed");
1469 smpHandler =
nullptr;
1472 return smpHandler->establishSecurity(sec_level);
1474 DBG_PRINT(
"BTDevice::connectSMP: SMP Not supported by OS (0): %s", toString().c_str());
1481 void BTDevice::disconnectGATT(
const int caller) noexcept {
1482 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_gattHandler);
1483 if(
nullptr != gattHandler ) {
1484 DBG_PRINT(
"BTDevice::disconnectGATT: start (has gattHandler, caller %d)", caller);
1485 gattHandler->disconnect(
false ,
false );
1487 DBG_PRINT(
"BTDevice::disconnectGATT: start (nil gattHandler, caller %d)", caller);
1489 gattHandler =
nullptr;
1490 DBG_PRINT(
"BTDevice::disconnectGATT: end");
1493 bool BTDevice::connectGATT(std::shared_ptr<BTDevice> sthis) noexcept {
1494 if( !isConnected || !allowDisconnect) {
1495 ERR_PRINT(
"BTDevice::connectGATT: Device not connected: %s", toString().c_str());
1498 if( !l2cap_att.isOpen() ) {
1499 ERR_PRINT(
"BTDevice::connectGATT: L2CAP not open: %s", toString().c_str());
1503 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_gattHandler);
1504 if(
nullptr != gattHandler ) {
1505 if( gattHandler->isConnected() ) {
1508 gattHandler =
nullptr;
1511 gattHandler = std::make_shared<BTGattHandler>(sthis, l2cap_att);
1512 if( !gattHandler->isConnected() ) {
1513 ERR_PRINT2(
"BTDevice::connectGATT: Connection failed");
1514 gattHandler =
nullptr;
1521 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_gattHandler);
1527 if(
nullptr == gh ) {
1528 ERR_PRINT(
"BTDevice::getGATTServices: GATTHandler nullptr");
1532 if( gattServices.
size() > 0 ) {
1533 return gattServices;
1537 if( gattServices.
size() == 0 ) {
1538 return gattServices;
1542 std::shared_ptr<GattGenericAccessSvc> gattGenericAccess = gh->
getGenericAccess();
1543 if(
nullptr != gattGenericAccess ) {
1545 EIRDataType updateMask = update(*gattGenericAccess, ts);
1546 DBG_PRINT(
"BTDevice::getGATTServices: updated %s:\n %s\n -> %s",
1550 if(
nullptr == sharedInstance ) {
1551 ERR_PRINT(
"BTDevice::getGATTServices: Device unknown to adapter and not tracked: %s",
toString().c_str());
1553 adapter.sendDeviceUpdated(
"getGATTServices", sharedInstance, ts, updateMask);
1557 }
catch (std::exception &e) {
1558 WARN_PRINT(
"BTDevice::getGATTServices: Caught exception: '%s' on %s", e.what(),
toString().c_str());
1560 return gattServices;
1565 const size_t size = gattServices.
size();
1566 for (
size_t i = 0; i < size; i++) {
1567 const std::shared_ptr<BTGattService> & e = gattServices[i];
1568 if (
nullptr != e && *uuid == *(e->
type) ) {
1584 }
catch (std::exception &e) {
1585 IRQ_PRINT(
"BTDevice::pingGATT: Potential disconnect, exception: '%s' on %s", e.what(),
toString().c_str());
1592 if(
nullptr == gh ) {
1593 ERR_PRINT(
"BTDevice::getGATTGenericAccess: GATTHandler nullptr");
1601 if(
nullptr == gatt ) {
1609 std::shared_ptr<BTGattHandler> gatt = getGattHandler();
1610 if(
nullptr == gatt ) {
1612 DBG_PRINT(
"Device's GATTHandle not connected: %s", toString().c_str());
1619 std::shared_ptr<BTGattHandler> gatt = getGattHandler();
1620 if(
nullptr == gatt ) {
1622 DBG_PRINT(
"Device's GATTHandle not connected: %s", toString().c_str());
1629 std::shared_ptr<BTGattHandler> gatt = getGattHandler();
1630 if(
nullptr == gatt ) {
1632 DBG_PRINT(
"Device's GATTHandle not connected: %s", toString().c_str());
1640 if(
nullptr == gatt ) {
1649 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect);
1654 if( !isConnected ) {
1659 if( 0 == hciConnHandle ) {
1664 if( !adapter.isPowered() ) {
1669 res = hci.
le_read_phy(hciConnHandle.load(), addressAndType, resRx, resTx);
1671 DBG_PRINT(
"BTDevice::getConnectedLE_PHY: status %s: RX %s, TX %s - %s",
1678 ERR_PRINT(
"BTDevice::getConnectedLE_PHY: status %s: RX %s, TX %s - %s",
1684 void BTDevice::notifyDisconnected() noexcept {
1686 DBG_PRINT(
"BTDevice::notifyDisconnected: handle %s -> zero, %s",
1688 clearSMPStates(
false );
1689 allowDisconnect =
false;
1690 isConnected =
false;
1697 void BTDevice::sendMgmtEvDeviceDisconnected(std::unique_ptr<MgmtEvent> evt) noexcept {
1698 adapter.mgmtEvDeviceDisconnectedHCI(*evt);
1703 bool expConn =
true;
1704 if( !allowDisconnect.compare_exchange_strong(expConn,
false) ) {
1706 DBG_PRINT(
"BTDevice::disconnect: Not connected: isConnected %d/%d, reason 0x%X (%s), gattHandler %d, hciConnHandle %s",
1707 allowDisconnect.load(), isConnected.load(),
1708 static_cast<uint8_t
>(reason),
to_string(reason).c_str(),
1712 if( !isConnected ) {
1713 WARN_PRINT(
"BTDevice::disconnect: allowConnect true -> false, but !isConnected on %s", toString().c_str());
1723 const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect);
1725 WORDY_PRINT(
"BTDevice::disconnect: Start: isConnected %d/%d, reason 0x%X (%s), gattHandler %d, hciConnHandle %s",
1726 allowDisconnect.load(), isConnected.load(),
1727 static_cast<uint8_t
>(reason),
to_string(reason).c_str(),
1733 if( 0 == hciConnHandle ) {
1738 if( !adapter.isPowered() ) {
1739 WARN_PRINT(
"BTDevice::disconnect: Adapter not powered: %s, %s", adapter.toString().c_str(), toString().c_str());
1744 res = hci.
disconnect(hciConnHandle.load(), addressAndType, reason);
1746 ERR_PRINT(
"BTDevice::disconnect: status %s, handle 0x%X, isConnected %d/%d: errno %d %s on %s",
1747 to_string(res).c_str(), hciConnHandle.load(),
1748 allowDisconnect.load(), isConnected.load(),
1749 errno, strerror(errno),
1750 toString().c_str());
1759 std::thread bg(&BTDevice::sendMgmtEvDeviceDisconnected,
this,
1760 std::make_unique<MgmtEvtDeviceDisconnected>(adapter.dev_id, addressAndType, reason, hciConnHandle.load()) );
1764 WORDY_PRINT(
"BTDevice::disconnect: End: status %s, handle 0x%X, isConnected %d/%d on %s",
1766 hciConnHandle.load(), allowDisconnect.load(), isConnected.load(),
1767 toString().c_str());
1773 #if USE_LINUX_BT_SECURITY
1775 clearSMPStates(
false );
1777 #elif SMP_SUPPORTED_BY_OS
1785 clearSMPStates(
false );
1786 adapter.removeDevice(*
this);