diff options
author | Sven Gothel <[email protected]> | 2020-06-24 06:06:46 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-24 06:06:46 +0200 |
commit | 50d04560b87f51a1dc2979b62cc48883c846d5bb (patch) | |
tree | 8e3423eacf9e28d42f2cc420727d7a7f5f9fe21e /src/direct_bt | |
parent | 9900ef4b93c191c0ac4fa8f941e06a5a8045257c (diff) |
HCIHandler: Use async event mechanism for delayed replies, commands shall return immediately (2/2)
Adjust using HCIHandler changes, see commit 9900ef4b93c191c0ac4fa8f941e06a5a8045257c
- DBTAdapter listens to HCIHandler callbacks: *connected* and *disconnected*
using same callback implementations as for DBTManager.
- DBTDevice::notifyConnect receives the actual connection handler
and will be called for all connected callbacks (DBTManager and HCIHandler)
- DBTDevice adjusted return values for *connect* and *disconnect*,
no more connection handle available immediately.
Further changes:
Diffstat (limited to 'src/direct_bt')
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 6 | ||||
-rw-r--r-- | src/direct_bt/DBTDevice.cpp | 60 |
2 files changed, 40 insertions, 26 deletions
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 16a44485..fb488408 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -198,6 +198,8 @@ std::shared_ptr<HCIHandler> DBTAdapter::openHCI() return nullptr; } hci = std::shared_ptr<HCIHandler>( s ); + hci->addMgmtEventCallback(MgmtEvent::Opcode::DEVICE_CONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceConnectedCB)); + hci->addMgmtEventCallback(MgmtEvent::Opcode::DEVICE_DISCONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDisconnectedCB)); return hci; } @@ -577,8 +579,10 @@ bool DBTAdapter::mgmtEvDeviceConnectedCB(std::shared_ptr<MgmtEvent> e) { } DBG_PRINT("DBTAdapter::EventCB:DeviceConnected(dev_id %d, new_connect %d, updated %s): %s,\n %s\n -> %s", dev_id, new_connect, getEIRDataMaskString(updateMask).c_str(), event.toString().c_str(), ad_report.toString().c_str(), device->toString().c_str()); + + device->notifyConnected(event.getHCIHandle()); + if( EIRDataType::NONE != updateMask || 0 < new_connect ) { - device->notifyConnected(); int i=0; for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp index 2721d256..0985aef7 100644 --- a/src/direct_bt/DBTDevice.cpp +++ b/src/direct_bt/DBTDevice.cpp @@ -240,25 +240,25 @@ std::shared_ptr<ConnectionInfo> DBTDevice::getConnectionInfo() { return connInfo; } -uint16_t DBTDevice::connectLE(HCIAddressType peer_mac_type, HCIAddressType own_mac_type, +bool DBTDevice::connectLE(HCIAddressType peer_mac_type, HCIAddressType own_mac_type, uint16_t le_scan_interval, uint16_t le_scan_window, uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout) { if( 0 < hciConnHandle ) { ERR_PRINT("DBTDevice::connectLE: Already connected: %s", toString().c_str()); - return 0; + return false; } const std::lock_guard<std::recursive_mutex> lock(adapter.mtx_hci); // RAII-style acquire and relinquish via destructor std::shared_ptr<HCIHandler> hci = adapter.openHCI(); if( nullptr == hci || !hci->isOpen() ) { ERR_PRINT("DBTDevice::connectLE: Opening adapter's HCI failed: %s", toString().c_str()); - return 0; + return false; } if( !isLEAddressType() ) { ERR_PRINT("DBTDevice::connectLE: Not a BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM address: %s", toString().c_str()); - return 0; + return false; } { @@ -266,7 +266,7 @@ uint16_t DBTDevice::connectLE(HCIAddressType peer_mac_type, HCIAddressType own_m DBTManager & mngr = adapter.getManager(); mngr.create_connection(adapter.dev_id, address, addressType); } - HCIStatusCode status = hci->le_create_conn(&hciConnHandle, address, + HCIStatusCode status = hci->le_create_conn(address, peer_mac_type, own_mac_type, le_scan_interval, le_scan_window, conn_interval_min, conn_interval_max, conn_latency, supervision_timeout); @@ -285,32 +285,31 @@ uint16_t DBTDevice::connectLE(HCIAddressType peer_mac_type, HCIAddressType own_m if( HCIStatusCode::COMMAND_DISALLOWED == status ) { WARN_PRINT("DBTDevice::connectLE: Could not yet create connection: status 0x%2.2X (%s), errno %d %s on %s", static_cast<uint8_t>(status), getHCIStatusCodeString(status).c_str(), errno, strerror(errno), toString().c_str()); - return 0; + return false; } if ( HCIStatusCode::SUCCESS != status ) { ERR_PRINT("DBTDevice::connectLE: Could not create connection: status 0x%2.2X (%s), errno %d %s on %s", static_cast<uint8_t>(status), getHCIStatusCodeString(status).c_str(), errno, strerror(errno), toString().c_str()); - return 0; + return false; } - - return hciConnHandle; + return true; } -uint16_t DBTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_offset, const uint8_t role_switch) +bool DBTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_offset, const uint8_t role_switch) { if( 0 < hciConnHandle ) { ERR_PRINT("DBTDevice::connectBREDR: Already connected: %s", toString().c_str()); - return 0; + return false; } const std::lock_guard<std::recursive_mutex> lock(adapter.mtx_hci); // RAII-style acquire and relinquish via destructor std::shared_ptr<HCIHandler> hci = adapter.openHCI(); if( nullptr == hci || !hci->isOpen() ) { ERR_PRINT("DBTDevice::connectBREDR: Opening adapter's HCI failed: %s", toString().c_str()); - return 0; + return false; } if( !isBREDRAddressType() ) { ERR_PRINT("DBTDevice::connectBREDR: Not a BDADDR_BREDR address: %s", toString().c_str()); - return 0; + return false; } { @@ -319,17 +318,16 @@ uint16_t DBTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_o mngr.create_connection(adapter.dev_id, address, addressType); } - HCIStatusCode status = hci->create_conn(&hciConnHandle, address, pkt_type, clock_offset, role_switch); + HCIStatusCode status = hci->create_conn(address, pkt_type, clock_offset, role_switch); if ( HCIStatusCode::SUCCESS != status ) { ERR_PRINT("DBTDevice::connectBREDR: Could not create connection: status 0x%2.2X (%s), errno %d %s on %s", static_cast<uint8_t>(status), getHCIStatusCodeString(status).c_str(), errno, strerror(errno), toString().c_str()); - return 0; + return false; } - - return hciConnHandle; + return true; } -uint16_t DBTDevice::connectDefault() +bool DBTDevice::connectDefault() { switch( addressType ) { case BDAddressType::BDADDR_LE_PUBLIC: @@ -340,13 +338,18 @@ uint16_t DBTDevice::connectDefault() return connectBREDR(); default: ERR_PRINT("DBTDevice::connectDefault: Not a valid address type: %s", toString().c_str()); - return 0; + return false; } } -void DBTDevice::notifyConnected() { - DBG_PRINT("DBTDevice::notifyConnected: %s", toString().c_str()); +void DBTDevice::notifyConnected(const uint16_t handle) { + DBG_PRINT("DBTDevice::notifyConnected: handle %s, %s", uint16HexString(handle).c_str(), toString().c_str()); isConnected = true; + if( 0 != handle ) { + hciConnHandle = handle; + } else { + hciConnHandle = 0xffff; // dummy, as handle is not supported by manager + } } void DBTDevice::notifyDisconnected() { @@ -360,12 +363,14 @@ void DBTDevice::notifyDisconnected() { isConnected = false; } -void DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause, const HCIStatusCode reason) { +bool DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause, const HCIStatusCode reason) { DBG_PRINT("DBTDevice::disconnect: isConnected %d, sentFromManager %d, ioError %d, reason 0x%X (%s), gattHandler %d, hciConnHandle %d", isConnected.load(), sentFromManager, ioErrorCause, static_cast<uint8_t>(reason), getHCIStatusCodeString(reason).c_str(), (nullptr != gattHandler), (0 != hciConnHandle)); disconnectGATT(); + bool res = false; + const std::lock_guard<std::recursive_mutex> lock(adapter.mtx_hci); // RAII-style acquire and relinquish via destructor std::shared_ptr<HCIHandler> hci = adapter.getHCI(); @@ -390,8 +395,10 @@ void DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause, goto skip_hci_disconnect; } - if( HCIStatusCode::SUCCESS != hci->disconnect(hciConnHandle, reason) ) { + if( HCIStatusCode::SUCCESS != hci->disconnect(hciConnHandle, address, addressType, reason) ) { DBG_PRINT("DBTDevice::disconnect: handle 0x%X, errno %d %s", hciConnHandle, errno, strerror(errno)); + } else { + res = true; } skip_hci_disconnect: @@ -401,11 +408,14 @@ skip_hci_disconnect: // Also issue mngr.disconnect on non-HCI connect (whitelist), // which will also send the DISCONNECT event. DBTManager & mngr = adapter.getManager(); - mngr.disconnect(ioErrorCause, adapter.dev_id, address, addressType, reason); + res = mngr.disconnect(ioErrorCause, adapter.dev_id, address, addressType, reason) || res; } exit: - adapter.removeConnectedDevice(*this); + if( res ) { + adapter.removeConnectedDevice(*this); + } + return res; } void DBTDevice::remove() { |