diff options
author | Sven Gothel <[email protected]> | 2020-06-01 01:37:57 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-01 01:37:57 +0200 |
commit | 728cbb0c31ac8cab0cc6e7551c78adab5e2a4a4d (patch) | |
tree | 4df09a49017cce5759e60c6eabc171c8d82d9f6a /api | |
parent | 9ae37c02afdcd6344f32e823e40f9ee8c4a9ae0a (diff) |
Complete using definite tyep HCIErrorCode for disconnect reason; Add Mgmt DisconnectReason <-> HCIErrorCode mapping
BlueZ Kernel Mgmt uses its own disconnect reason code in its implementation, well ...
We add mapping in both directions, Mgmt DisconnectReason <-> HCIErrorCode mapping,
while exposing HCIErrorCode in the front API only as we might move to HCI at one point.
Diffstat (limited to 'api')
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 4 | ||||
-rw-r--r-- | api/direct_bt/DBTDevice.hpp | 4 | ||||
-rw-r--r-- | api/direct_bt/DBTManager.hpp | 2 | ||||
-rw-r--r-- | api/direct_bt/HCIComm.hpp | 6 | ||||
-rw-r--r-- | api/direct_bt/MgmtTypes.hpp | 49 |
5 files changed, 52 insertions, 13 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 857e9643..3813fd5d 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -174,7 +174,7 @@ namespace direct_bt { friend std::shared_ptr<DBTDevice> DBTDevice::getSharedInstance() const; friend void DBTDevice::releaseSharedInstance() const; friend std::shared_ptr<ConnectionInfo> DBTDevice::getConnectionInfo(); - friend void DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause,const uint8_t reason); + friend void DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause,const HCIErrorCode reason); friend uint16_t DBTDevice::connectLE(HCIAddressType peer_mac_type, HCIAddressType own_mac_type, uint16_t interval, uint16_t window, uint16_t min_interval, uint16_t max_interval, @@ -184,7 +184,7 @@ namespace direct_bt { bool addConnectedDevice(const std::shared_ptr<DBTDevice> & device); bool removeConnectedDevice(const DBTDevice & device); - int disconnectAllDevices(const uint8_t reason=0x13 /* HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION */); + int disconnectAllDevices(const HCIErrorCode reason=HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION ); std::shared_ptr<DBTDevice> findConnectedDevice (EUI48 const & mac) const; bool addDiscoveredDevice(std::shared_ptr<DBTDevice> const &device); diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp index 16634f42..e5f86818 100644 --- a/api/direct_bt/DBTDevice.hpp +++ b/api/direct_bt/DBTDevice.hpp @@ -87,7 +87,7 @@ namespace direct_bt { void notifyConnected(); void disconnect(const bool sentFromManager, const bool ioErrorCause, - const uint8_t reason=0x13 /* HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION */); + const HCIErrorCode reason=HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION ); public: const uint64_t ts_creation; @@ -227,7 +227,7 @@ namespace direct_bt { * An open GATTHandler will also be closed via disconnectGATT() * </p> */ - void disconnect(const uint8_t reason=static_cast<uint8_t>(HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION) ) { + void disconnect(const HCIErrorCode reason=HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION ) { disconnect(false /* sentFromManager */, false /* ioErrorCause */, reason); } diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp index 6dfc261a..5ebcf925 100644 --- a/api/direct_bt/DBTManager.hpp +++ b/api/direct_bt/DBTManager.hpp @@ -282,7 +282,7 @@ namespace direct_bt { bool disconnect(const bool ioErrorCause, const int dev_id, const EUI48 &peer_bdaddr, const BDAddressType peer_mac_type, - const uint8_t reason=0x13 /* HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION */); + const HCIErrorCode reason=HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION ); std::shared_ptr<ConnectionInfo> getConnectionInfo(const int dev_id, const EUI48 &address, const BDAddressType address_type); std::shared_ptr<NameAndShortName> setLocalName(const int dev_id, const std::string & name, const std::string & short_name); diff --git a/api/direct_bt/HCIComm.hpp b/api/direct_bt/HCIComm.hpp index 94f348b4..f437e9fc 100644 --- a/api/direct_bt/HCIComm.hpp +++ b/api/direct_bt/HCIComm.hpp @@ -130,7 +130,9 @@ namespace direct_bt { LIMIT_REACHED = 0x43, OPERATION_CANCELLED_BY_HOST = 0x44, PACKET_TOO_LONG = 0x45, - INTERNAL_FAILURE = 0xff + + INTERNAL_FAILURE = 0xfe, + UNKNOWN = 0xff }; std::string getHCIErrorCodeString(const HCIErrorCode ec); @@ -232,7 +234,7 @@ namespace direct_bt { /** * Disconnect an established connection. */ - bool disconnect(const uint16_t conn_handle, const uint8_t reason=static_cast<uint8_t>(HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION)); + bool disconnect(const uint16_t conn_handle, const HCIErrorCode reason=HCIErrorCode::REMOTE_USER_TERMINATED_CONNECTION); private: static inline void set_bit(int nr, void *addr) diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp index 5d77ffd5..3947505c 100644 --- a/api/direct_bt/MgmtTypes.hpp +++ b/api/direct_bt/MgmtTypes.hpp @@ -914,10 +914,35 @@ namespace direct_bt { class MgmtEvtDeviceDisconnected : public MgmtEvent { public: + enum class DisconnectReason : uint8_t { + UNKNOWN = 0x00, + TIMEOUT = 0x01, + LOCAL_HOST = 0x02, + REMOTE = 0x03, + AUTH_FAILURE = 0x04 + }; + static std::string getDisconnectReasonString(DisconnectReason mgmtReason); + + /** + * BlueZ Kernel Mgmt has reduced information by HCIErrorCode -> DisconnectReason, + * now the inverse surely can't repair this loss. + * <p> + * See getDisconnectReason(HCIErrorCode) below for the mentioned mapping. + * </p> + */ + static HCIErrorCode getHCIReason(DisconnectReason mgmtReason); + + /** + * BlueZ Kernel Mgmt mapping of HCI disconnect reason, which reduces some information. + */ + static DisconnectReason getDisconnectReason(HCIErrorCode hciReason); + + private: + const HCIErrorCode hciRootReason; protected: std::string baseString() const override { - const HCIErrorCode reason = getReason(); + const HCIErrorCode reason = getHCIReason(); return MgmtEvent::baseString()+", address="+getAddress().toString()+ ", addressType "+getBDAddressTypeString(getAddressType())+ ", reason "+uint8HexString(static_cast<uint8_t>(reason))+" ("+getHCIErrorCodeString(reason)+")"; @@ -925,22 +950,34 @@ namespace direct_bt { public: MgmtEvtDeviceDisconnected(const uint8_t* buffer, const int buffer_len) - : MgmtEvent(buffer, buffer_len) + : MgmtEvent(buffer, buffer_len), hciRootReason(HCIErrorCode::UNKNOWN) { checkOpcode(getOpcode(), Opcode::DEVICE_DISCONNECTED); } - MgmtEvtDeviceDisconnected(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, uint8_t reason) - : MgmtEvent(Opcode::DEVICE_DISCONNECTED, dev_id, 6+1+1) + MgmtEvtDeviceDisconnected(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, HCIErrorCode hciRootReason) + : MgmtEvent(Opcode::DEVICE_DISCONNECTED, dev_id, 6+1+1), hciRootReason(hciRootReason) { + DisconnectReason disconnectReason = getDisconnectReason(hciRootReason); pdu.put_eui48(MGMT_HEADER_SIZE, address); pdu.put_uint8(MGMT_HEADER_SIZE+6, addressType); - pdu.put_uint8(MGMT_HEADER_SIZE+6+1, reason); + pdu.put_uint8(MGMT_HEADER_SIZE+6+1, static_cast<uint8_t>(disconnectReason)); } const EUI48 getAddress() const { return EUI48(pdu.get_ptr(MGMT_HEADER_SIZE)); } // mgmt_addr_info BDAddressType getAddressType() const { return static_cast<BDAddressType>(pdu.get_uint8(MGMT_HEADER_SIZE+6)); } // mgmt_addr_info - HCIErrorCode getReason() const { return static_cast<HCIErrorCode>(pdu.get_uint8(MGMT_HEADER_SIZE+7)); } + DisconnectReason getReason() const { return static_cast<DisconnectReason>(pdu.get_uint8(MGMT_HEADER_SIZE+7)); } + + /** Return the root reason in non reduced HCIErrorCode space, if available. Otherwise this value will be HCIErrorCode::UNKNOWN. */ + HCIErrorCode getHCIRootReason() const { return hciRootReason; } + + /** Returns either the getHCIRootReason() if not HCIErrorCode::UNKNOWN, or the translated DisconnectReason. */ + HCIErrorCode getHCIReason() const { + if( HCIErrorCode::UNKNOWN != hciRootReason ) { + return hciRootReason; + } + return getHCIReason(getReason()); + } int getDataOffset() const override { return MGMT_HEADER_SIZE+8; } int getDataSize() const override { return getParamSize()-8; } |