diff options
author | Sven Gothel <[email protected]> | 2020-12-14 01:59:26 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-12-14 01:59:26 +0100 |
commit | d22915932120e4486ad09691ae5fb6c635b8c925 (patch) | |
tree | ee27b245b96a244ce9e2cbac03500a009c71dbdb /api | |
parent | cafd8d1f3689135eae36854a9cca4def690045fd (diff) |
smart_ptr-2: Handle MgmtEvent instances via std::unique_ptr instead of shared_ptr; MgmtEvent Callbacks use 'const MgmtEvent&' now.
smart_ptr change series, commit #2
This series started with commit cafd8d1f3689135eae36854a9cca4def690045fd,
see for details.
Due to using unique_ptr<MgmtEvent>, we cannot pass them to the user (loss of ownership via std::move).
Hence it must be enough to pass a reference to the direct constant type.
In case a user needs to cache the event for later, a copy must be created,
see 'template<class T> MgmtMsg::clone(const T& source) noexcept' for this purpose.
Diffstat (limited to 'api')
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 29 | ||||
-rw-r--r-- | api/direct_bt/DBTDevice.hpp | 4 | ||||
-rw-r--r-- | api/direct_bt/DBTManager.hpp | 14 | ||||
-rw-r--r-- | api/direct_bt/MgmtTypes.hpp | 22 |
4 files changed, 45 insertions, 24 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 66ec6d2d..c9cd3c8d 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -244,6 +244,7 @@ namespace direct_bt { friend std::shared_ptr<DBTDevice> DBTDevice::getSharedInstance() const noexcept; friend std::shared_ptr<ConnectionInfo> DBTDevice::getConnectionInfo() noexcept; + friend void DBTDevice::sendMgmtEvDeviceDisconnected(std::unique_ptr<MgmtEvent> evt) noexcept; friend HCIStatusCode DBTDevice::disconnect(const HCIStatusCode reason) noexcept; friend void DBTDevice::remove() noexcept; friend HCIStatusCode DBTDevice::connectLE(uint16_t interval, uint16_t window, @@ -251,8 +252,8 @@ namespace direct_bt { uint16_t latency, uint16_t supervision_timeout); friend HCIStatusCode DBTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_offset, const uint8_t role_switch); friend void DBTDevice::processL2CAPSetup(std::shared_ptr<DBTDevice> sthis); - friend bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, std::shared_ptr<MgmtEvent> evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept; friend void DBTDevice::hciSMPMsgCallback(std::shared_ptr<DBTDevice> sthis, std::shared_ptr<const SMPPDUMsg> msg, const HCIACLData::l2cap_frame& source) noexcept; + friend bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, const MgmtEvent& evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept; friend void DBTDevice::processDeviceReady(std::shared_ptr<DBTDevice> sthis, const uint64_t timestamp); friend std::vector<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() noexcept; @@ -275,13 +276,13 @@ namespace direct_bt { void removeSharedDevice(const DBTDevice & device) noexcept; std::shared_ptr<DBTDevice> findSharedDevice (const EUI48 & address, const BDAddressType addressType) noexcept; - bool mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvDeviceDiscoveringMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvLocalNameChangedMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvDeviceFoundHCI(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvDeviceDisconnectedMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvPairDeviceCompleteMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvNewLongTermKeyMgmt(std::shared_ptr<MgmtEvent> e) noexcept; + bool mgmtEvNewSettingsMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvDeviceDiscoveringMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvLocalNameChangedMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept; + bool mgmtEvDeviceDisconnectedMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvPairDeviceCompleteMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvNewLongTermKeyMgmt(const MgmtEvent& e) noexcept; bool mgmtEvDeviceDiscoveringHCI(std::shared_ptr<MgmtEvent> e) noexcept; bool mgmtEvDeviceConnectedHCI(std::shared_ptr<MgmtEvent> e) noexcept; @@ -292,13 +293,13 @@ namespace direct_bt { bool mgmtEvDeviceDisconnectedHCI(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvDeviceDiscoveringAny(std::shared_ptr<MgmtEvent> e, const bool hciSourced) noexcept; + bool mgmtEvDeviceDiscoveringAny(const MgmtEvent& e, const bool hciSourced) noexcept; - bool mgmtEvPinCodeRequestMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvUserConfirmRequestMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvUserPasskeyRequestMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvAuthFailedMgmt(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvDeviceUnpairedMgmt(std::shared_ptr<MgmtEvent> e) noexcept; + bool mgmtEvPinCodeRequestMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvUserConfirmRequestMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvUserPasskeyRequestMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvAuthFailedMgmt(const MgmtEvent& e) noexcept; + bool mgmtEvDeviceUnpairedMgmt(const MgmtEvent& e) noexcept; bool hciSMPMsgCallback(const BDAddressAndType & addressAndType, std::shared_ptr<const SMPPDUMsg> msg, const HCIACLData::l2cap_frame& source) noexcept; diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp index 4b504c56..b338e811 100644 --- a/api/direct_bt/DBTDevice.hpp +++ b/api/direct_bt/DBTDevice.hpp @@ -161,7 +161,7 @@ namespace direct_bt { bool checkPairingKeyDistributionComplete(const std::string& timestamp) const noexcept; - bool updatePairingState(std::shared_ptr<DBTDevice> sthis, std::shared_ptr<MgmtEvent> evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept; + bool updatePairingState(std::shared_ptr<DBTDevice> sthis, const MgmtEvent& evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept; /** * Forwarded from HCIHandler -> DBTAdapter -> this DBTDevice @@ -204,6 +204,8 @@ namespace direct_bt { void clearSMPStates(const bool connected) noexcept; + void sendMgmtEvDeviceDisconnected(std::unique_ptr<MgmtEvent> evt) noexcept; + public: const uint64_t ts_creation; /** Device's unique mac address and type tuple. */ diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp index 8bea2eef..5e5ddb1c 100644 --- a/api/direct_bt/DBTManager.hpp +++ b/api/direct_bt/DBTManager.hpp @@ -215,7 +215,7 @@ namespace direct_bt { POctets rbuffer; HCIComm comm; - jau::ringbuffer<std::shared_ptr<MgmtEvent>, nullptr, jau::nsize_t> mgmtEventRing; + jau::ringbuffer<std::unique_ptr<MgmtEvent>, nullptr, jau::nsize_t> mgmtEventRing; jau::sc_atomic_bool mgmtReaderShallStop; std::mutex mtx_mgmtReaderLifecycle; @@ -250,7 +250,7 @@ namespace direct_bt { * In case response size check or devID and optional opcode validation fails, * function returns NULL. */ - std::shared_ptr<MgmtEvent> sendWithReply(MgmtCommand &req) noexcept; + std::unique_ptr<MgmtEvent> sendWithReply(MgmtCommand &req) noexcept; bool send(MgmtCommand &req) noexcept; @@ -267,10 +267,10 @@ namespace direct_bt { std::shared_ptr<AdapterInfo> initAdapter(const uint16_t dev_id, const BTMode btMode) noexcept; void shutdownAdapter(const uint16_t dev_id) noexcept; - void processAdapterAdded(std::shared_ptr<MgmtEvent> e) noexcept; - void processAdapterRemoved(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEvNewSettingsCB(std::shared_ptr<MgmtEvent> e) noexcept; - bool mgmtEventAnyCB(std::shared_ptr<MgmtEvent> e) noexcept; + void processAdapterAdded(std::unique_ptr<MgmtEvent> e) noexcept; + void processAdapterRemoved(std::unique_ptr<MgmtEvent> e) noexcept; + bool mgmtEvNewSettingsCB(const MgmtEvent& e) noexcept; + bool mgmtEventAnyCB(const MgmtEvent& e) noexcept; int findAdapterInfoIndex(const uint16_t dev_id) const noexcept; @@ -474,7 +474,7 @@ namespace direct_bt { void clearAllCallbacks() noexcept; /** Manually send a MgmtEvent to all of its listeners. */ - void sendMgmtEvent(std::shared_ptr<MgmtEvent> event) noexcept; + void sendMgmtEvent(const MgmtEvent& event) noexcept; /** ChangedAdapterSetCallback handling */ diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp index 1bec74f4..ab0c83b0 100644 --- a/api/direct_bt/MgmtTypes.hpp +++ b/api/direct_bt/MgmtTypes.hpp @@ -335,6 +335,23 @@ namespace direct_bt { virtual ~MgmtMsg() {} + /** + * User utility clone template for convenience, based on derived class's copy-constructor.<br> + * MgmtEvent callback example: + * <pre> + * bool mgmtEvDeviceUnpairedMgmt(const MgmtEvent& e) noexcept { + * const MgmtEvtDeviceUnpaired &event = *static_cast<const MgmtEvtDeviceUnpaired *>(&e); + * MgmtMsg * b2 = MgmtMsg::clone(event); + * .. do something .. + * } + * </pre> + * @tparam T The derived definite class type, deduced by source + * @param source the source to be copied + * @return a new instance. + */ + template<class T> + static MgmtMsg* clone(const T& source) noexcept { return new T(source); } + uint64_t getTimestamp() const noexcept { return ts_creation; } jau::nsize_t getTotalSize() const noexcept { return pdu.getSize(); } @@ -1157,7 +1174,7 @@ namespace direct_bt { * Returned memory reference is managed by caller (delete etc) * </p> */ - static std::shared_ptr<MgmtEvent> getSpecialized(const uint8_t * buffer, jau::nsize_t const buffer_size) noexcept; + static std::unique_ptr<MgmtEvent> getSpecialized(const uint8_t * buffer, jau::nsize_t const buffer_size) noexcept; /** Persistent memory, w/ ownership ..*/ MgmtEvent(const uint8_t* buffer, const jau::nsize_t buffer_len, const jau::nsize_t exp_param_size) @@ -1187,6 +1204,7 @@ namespace direct_bt { memcpy(pdu.get_wptr_nc(MGMT_HEADER_SIZE), param, param_size); } } + virtual ~MgmtEvent() noexcept override {} jau::nsize_t getTotalSize() const noexcept { return pdu.getSize(); } @@ -2213,7 +2231,7 @@ namespace direct_bt { }; - typedef jau::FunctionDef<bool, std::shared_ptr<MgmtEvent>> MgmtEventCallback; + typedef jau::FunctionDef<bool, const MgmtEvent&> MgmtEventCallback; typedef jau::cow_vector<MgmtEventCallback> MgmtEventCallbackList; class MgmtAdapterEventCallback { |