summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--api/direct_bt/DBTAdapter.hpp29
-rw-r--r--api/direct_bt/DBTDevice.hpp4
-rw-r--r--api/direct_bt/DBTManager.hpp14
-rw-r--r--api/direct_bt/MgmtTypes.hpp22
-rw-r--r--java/jni/direct_bt/DBTDevice.cxx49
-rw-r--r--java/jni/direct_bt/DBTManager.cxx14
-rw-r--r--src/direct_bt/DBTAdapter.cpp91
-rw-r--r--src/direct_bt/DBTDevice.cpp26
-rw-r--r--src/direct_bt/DBTManager.cpp96
-rw-r--r--src/direct_bt/MgmtTypes.cpp60
10 files changed, 221 insertions, 184 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 {
diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx
index e447edec..55a2deae 100644
--- a/java/jni/direct_bt/DBTDevice.cxx
+++ b/java/jni/direct_bt/DBTDevice.cxx
@@ -800,6 +800,17 @@ struct BooleanDeviceCBContext {
JNIGlobalRef boolean_cls_ref;
jmethodID boolean_ctor;
+ BooleanDeviceCBContext(
+ BDAddressAndType addressAndType_,
+ JNIGlobalRef javaCallback_ref_,
+ jmethodID mRun_,
+ JNIGlobalRef boolean_cls_ref_,
+ jmethodID boolean_ctor_)
+ : addressAndType(addressAndType_), javaCallback_ref(javaCallback_ref_),
+ mRun(mRun_), boolean_cls_ref(boolean_cls_ref_), boolean_ctor(boolean_ctor_)
+ { }
+
+
bool operator==(const BooleanDeviceCBContext& rhs) const
{
if( &rhs == this ) {
@@ -822,10 +833,10 @@ typedef std::shared_ptr<BooleanDeviceCBContext> BooleanDeviceCBContextRef;
static void disableBlockedNotifications(JNIEnv *env, jobject obj, DBTManager &mgmt)
{
- InvocationFunc<bool, std::shared_ptr<MgmtEvent>> * funcptr =
- getObjectRef<InvocationFunc<bool, std::shared_ptr<MgmtEvent>>>(env, obj, "blockedNotificationRef");
+ InvocationFunc<bool, const MgmtEvent&> * funcptr =
+ getObjectRef<InvocationFunc<bool, const MgmtEvent&>>(env, obj, "blockedNotificationRef");
if( nullptr != funcptr ) {
- FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef( funcptr );
+ FunctionDef<bool, const MgmtEvent&> funcDef( funcptr );
funcptr = nullptr;
setObjectRef(env, obj, funcptr, "blockedNotificationRef"); // clear java ref
int count;
@@ -859,17 +870,17 @@ void Java_direct_1bt_tinyb_DBTDevice_enableBlockedNotificationsImpl(JNIEnv *env,
disableBlockedNotifications(env, obj, mgmt);
- bool(*nativeCallback)(BooleanDeviceCBContextRef&, std::shared_ptr<MgmtEvent>) =
- [](BooleanDeviceCBContextRef& ctx_ref, std::shared_ptr<MgmtEvent> e)->bool {
+ bool(*nativeCallback)(BooleanDeviceCBContextRef&, const MgmtEvent&) =
+ [](BooleanDeviceCBContextRef& ctx_ref, const MgmtEvent& e)->bool {
bool isBlocked = false;
- if( MgmtEvent::Opcode::DEVICE_BLOCKED == e->getOpcode() ) {
- const MgmtEvtDeviceBlocked &event = *static_cast<const MgmtEvtDeviceBlocked *>(e.get());
+ if( MgmtEvent::Opcode::DEVICE_BLOCKED == e.getOpcode() ) {
+ const MgmtEvtDeviceBlocked &event = *static_cast<const MgmtEvtDeviceBlocked *>(&e);
if( event.getAddress() != ctx_ref->addressAndType.address || event.getAddressType() != ctx_ref->addressAndType.type ) {
return false; // not this device
}
isBlocked = true;
- } else if( MgmtEvent::Opcode::DEVICE_UNBLOCKED == e->getOpcode() ) {
- const MgmtEvtDeviceUnblocked &event = *static_cast<const MgmtEvtDeviceUnblocked *>(e.get());
+ } else if( MgmtEvent::Opcode::DEVICE_UNBLOCKED == e.getOpcode() ) {
+ const MgmtEvtDeviceUnblocked &event = *static_cast<const MgmtEvtDeviceUnblocked *>(&e);
if( event.getAddress() != ctx_ref->addressAndType.address || event.getAddressType() != ctx_ref->addressAndType.type ) {
return false; // not this device
}
@@ -892,12 +903,12 @@ void Java_direct_1bt_tinyb_DBTDevice_enableBlockedNotificationsImpl(JNIEnv *env,
jmethodID boolean_ctor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
java_exception_check_and_throw(env, E_FILE_LINE);
- BooleanDeviceCBContext * ctx = new BooleanDeviceCBContext{
- device->getAddressAndType(), JNIGlobalRef(javaCallback), mRun, JNIGlobalRef(boolean_cls), boolean_ctor };
+ BooleanDeviceCBContextRef ctx_ref = std::make_shared<BooleanDeviceCBContext>(
+ device->getAddressAndType(), JNIGlobalRef(javaCallback), mRun, JNIGlobalRef(boolean_cls), boolean_ctor );
jni_env->DeleteLocalRef(boolean_cls);
// move BooleanDeviceCBContextRef into CaptureInvocationFunc and operator== includes javaCallback comparison
- FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef = bindCaptureFunc(BooleanDeviceCBContextRef(ctx), nativeCallback);
+ FunctionDef<bool, const MgmtEvent&> funcDef = bindCaptureFunc(ctx_ref, nativeCallback);
setObjectRef(env, obj, funcDef.cloneFunction(), "blockedNotificationRef"); // set java ref
mgmt.addMgmtEventCallback(adapter.dev_id, MgmtEvent::Opcode::DEVICE_BLOCKED, funcDef);
mgmt.addMgmtEventCallback(adapter.dev_id, MgmtEvent::Opcode::DEVICE_UNBLOCKED, funcDef);
@@ -912,10 +923,10 @@ void Java_direct_1bt_tinyb_DBTDevice_enableBlockedNotificationsImpl(JNIEnv *env,
static void disablePairedNotifications(JNIEnv *env, jobject obj, DBTManager &mgmt)
{
- InvocationFunc<bool, std::shared_ptr<MgmtEvent>> * funcptr =
- getObjectRef<InvocationFunc<bool, std::shared_ptr<MgmtEvent>>>(env, obj, "pairedNotificationRef");
+ InvocationFunc<bool, const MgmtEvent&> * funcptr =
+ getObjectRef<InvocationFunc<bool, const MgmtEvent&>>(env, obj, "pairedNotificationRef");
if( nullptr != funcptr ) {
- FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef( funcptr );
+ FunctionDef<bool, const MgmtEvent&> funcDef( funcptr );
funcptr = nullptr;
setObjectRef(env, obj, funcptr, "pairedNotificationRef"); // clear java ref
int count;
@@ -946,9 +957,9 @@ void Java_direct_1bt_tinyb_DBTDevice_enablePairedNotificationsImpl(JNIEnv *env,
disablePairedNotifications(env, obj, mgmt);
- bool(*nativeCallback)(BooleanDeviceCBContextRef&, std::shared_ptr<MgmtEvent>) =
- [](BooleanDeviceCBContextRef& ctx_ref, std::shared_ptr<MgmtEvent> e)->bool {
- const MgmtEvtDeviceUnpaired &event = *static_cast<const MgmtEvtDeviceUnpaired *>(e.get());
+ bool(*nativeCallback)(BooleanDeviceCBContextRef&, const MgmtEvent&) =
+ [](BooleanDeviceCBContextRef& ctx_ref, const MgmtEvent& e)->bool {
+ const MgmtEvtDeviceUnpaired &event = *static_cast<const MgmtEvtDeviceUnpaired *>(&e);
if( event.getAddress() != ctx_ref->addressAndType.address || event.getAddressType() != ctx_ref->addressAndType.type ) {
return false; // not this device
}
@@ -971,7 +982,7 @@ void Java_direct_1bt_tinyb_DBTDevice_enablePairedNotificationsImpl(JNIEnv *env,
jni_env->DeleteLocalRef(boolean_cls);
// move BooleanDeviceCBContextRef into CaptureInvocationFunc and operator== includes javaCallback comparison
- FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef = bindCaptureFunc(BooleanDeviceCBContextRef(ctx), nativeCallback);
+ FunctionDef<bool, const MgmtEvent&> funcDef = bindCaptureFunc(BooleanDeviceCBContextRef(ctx), nativeCallback);
setObjectRef(env, obj, funcDef.cloneFunction(), "pairedNotificationRef"); // set java ref
// Note that this is only called natively for unpaired, i.e. paired:=false. Using deviceConnected for paired:=true on Java side
mgmt.addMgmtEventCallback(adapter.dev_id, MgmtEvent::Opcode::DEVICE_UNPAIRED, funcDef);
diff --git a/java/jni/direct_bt/DBTManager.cxx b/java/jni/direct_bt/DBTManager.cxx
index 8a5e6e87..efc7a0a1 100644
--- a/java/jni/direct_bt/DBTManager.cxx
+++ b/java/jni/direct_bt/DBTManager.cxx
@@ -48,6 +48,9 @@ struct BooleanMgmtCBContext {
JNIGlobalRef jmgmtRef;
jmethodID mid;
+ BooleanMgmtCBContext(MgmtEvent::Opcode opc_, JNIGlobalRef jmgmtRef_, jmethodID mid_)
+ : opc(opc_), jmgmtRef(jmgmtRef_), mid(mid_) { }
+
bool operator==(const BooleanMgmtCBContext& rhs) const
{
if( &rhs == this ) {
@@ -67,17 +70,17 @@ static void _addMgmtCBOnce(JNIEnv *env, DBTManager & mgmt, JNIGlobalRef jmgmtRef
const std::string &jmethodName, const std::string &jmethodArgs)
{
try {
- bool(*nativeCallback)(BooleanMgmtCBContextRef&, std::shared_ptr<MgmtEvent>) =
- [](BooleanMgmtCBContextRef& ctx_ref, std::shared_ptr<MgmtEvent> e)->bool {
+ bool(*nativeCallback)(BooleanMgmtCBContextRef&, const MgmtEvent&) =
+ [](BooleanMgmtCBContextRef& ctx_ref, const MgmtEvent& e)->bool {
JNIEnv *env_ = *jni_env;
- const int dev_id = e->getDevID();
+ const int dev_id = e.getDevID();
if( MgmtEvent::Opcode::INDEX_REMOVED == ctx_ref->opc ) {
env_->CallVoidMethod(*(ctx_ref->jmgmtRef), ctx_ref->mid, dev_id, ctx_ref->opc); // remove
} else if( MgmtEvent::Opcode::INDEX_ADDED == ctx_ref->opc ) {
env_->CallVoidMethod(*(ctx_ref->jmgmtRef), ctx_ref->mid, dev_id, ctx_ref->opc); // updated
} else if( MgmtEvent::Opcode::NEW_SETTINGS == ctx_ref->opc ) {
- const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(e.get());
+ const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(&e);
if( isAdapterSettingBitSet(event.getSettings(), AdapterSetting::POWERED) ) {
// probably newly POWERED on
env_->CallVoidMethod(*(ctx_ref->jmgmtRef), ctx_ref->mid, dev_id, ctx_ref->opc); // updated
@@ -97,10 +100,9 @@ static void _addMgmtCBOnce(JNIEnv *env, DBTManager & mgmt, JNIGlobalRef jmgmtRef
if( nullptr == mid ) {
throw jau::InternalError("DBTManager has no "+jmethodName+"."+jmethodArgs+" method, for "+mgmt.toString(), E_FILE_LINE);
}
- BooleanMgmtCBContext * ctx = new BooleanMgmtCBContext{opc, jmgmtRef, mid };
// move BooleanDeviceCBContextRef into CaptureInvocationFunc and operator== includes javaCallback comparison
- FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef = bindCaptureFunc(BooleanMgmtCBContextRef(ctx), nativeCallback);
+ FunctionDef<bool, const MgmtEvent&> funcDef = bindCaptureFunc(std::make_shared<BooleanMgmtCBContext>(opc, jmgmtRef, mid), nativeCallback);
mgmt.addMgmtEventCallback(-1, opc, funcDef);
} catch(...) {
rethrow_and_raise_java_exception(env);
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index 85a71f51..733136ca 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -735,7 +735,8 @@ exit:
if( le_scan_temp_disabled || HCIStatusCode::SUCCESS != status ) {
// In case of discoveryTempDisabled, power-off, le_enable_scane failure
// or already closed HCIHandler, send the event directly.
- mgmtEvDeviceDiscoveringHCI( std::shared_ptr<MgmtEvent>( new MgmtEvtDiscovering(dev_id, ScanType::LE, false) ) );
+ const MgmtEvtDiscovering e(dev_id, ScanType::LE, false);
+ mgmtEvDeviceDiscoveringHCI( e );
}
DBG_PRINT("DBTAdapter::stopDiscovery: End: Result %s, keepAlive %d, currentScanType[native %s, meta %s], le_scan_temp_disabled %d ...",
getHCIStatusCodeString(status).c_str(), keep_le_scan_alive.load(),
@@ -901,17 +902,17 @@ void DBTAdapter::sendDeviceUpdated(std::string cause, std::shared_ptr<DBTDevice>
// *************************************************
-bool DBTAdapter::mgmtEvDeviceDiscoveringHCI(std::shared_ptr<MgmtEvent> e) noexcept {
+bool DBTAdapter::mgmtEvDeviceDiscoveringHCI(const MgmtEvent& e) noexcept {
return mgmtEvDeviceDiscoveringAny(e, true /* hciSourced */ );
}
-bool DBTAdapter::mgmtEvDeviceDiscoveringMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
+bool DBTAdapter::mgmtEvDeviceDiscoveringMgmt(const MgmtEvent& e) noexcept {
return mgmtEvDeviceDiscoveringAny(e, false /* hciSourced */ );
}
-bool DBTAdapter::mgmtEvDeviceDiscoveringAny(std::shared_ptr<MgmtEvent> e, const bool hciSourced) noexcept {
+bool DBTAdapter::mgmtEvDeviceDiscoveringAny(const MgmtEvent& e, const bool hciSourced) noexcept {
const std::string srctkn = hciSourced ? "hci" : "mgmt";
- const MgmtEvtDiscovering &event = *static_cast<const MgmtEvtDiscovering *>(e.get());
+ const MgmtEvtDiscovering &event = *static_cast<const MgmtEvtDiscovering *>(&e);
const ScanType eventScanType = event.getScanType();
const bool eventEnabled = event.getEnabled();
ScanType currentNativeScanType = hci.getCurrentScanType();
@@ -975,9 +976,9 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringAny(std::shared_ptr<MgmtEvent> e, const
return true;
}
-bool DBTAdapter::mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- COND_PRINT(debug_event, "DBTAdapter:mgmt:NewSettings: %s", e->toString().c_str());
- const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(e.get());
+bool DBTAdapter::mgmtEvNewSettingsMgmt(const MgmtEvent& e) noexcept {
+ COND_PRINT(debug_event, "DBTAdapter:mgmt:NewSettings: %s", e.toString().c_str());
+ const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(&e);
const AdapterSetting new_settings = adapterInfo->setCurrentSettingMask(event.getSettings()); // probably done by mgmt callback already
{
const BTMode _btMode = getAdapterSettingsBTMode(new_settings);
@@ -1017,9 +1018,9 @@ bool DBTAdapter::mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
return true;
}
-bool DBTAdapter::mgmtEvLocalNameChangedMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- COND_PRINT(debug_event, "DBTAdapter:mgmt:LocalNameChanged: %s", e->toString().c_str());
- const MgmtEvtLocalNameChanged &event = *static_cast<const MgmtEvtLocalNameChanged *>(e.get());
+bool DBTAdapter::mgmtEvLocalNameChangedMgmt(const MgmtEvent& e) noexcept {
+ COND_PRINT(debug_event, "DBTAdapter:mgmt:LocalNameChanged: %s", e.toString().c_str());
+ const MgmtEvtLocalNameChanged &event = *static_cast<const MgmtEvtLocalNameChanged *>(&e);
std::string old_name = localName.getName();
std::string old_shortName = localName.getShortName();
bool nameChanged = old_name != event.getName();
@@ -1038,9 +1039,9 @@ bool DBTAdapter::mgmtEvLocalNameChangedMgmt(std::shared_ptr<MgmtEvent> e) noexce
return true;
}
-bool DBTAdapter::mgmtEvDeviceConnectedHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- COND_PRINT(debug_event, "DBTAdapter:hci:DeviceConnected(dev_id %d): %s", dev_id, e->toString().c_str());
- const MgmtEvtDeviceConnected &event = *static_cast<const MgmtEvtDeviceConnected *>(e.get());
+bool DBTAdapter::mgmtEvDeviceConnectedHCI(const MgmtEvent& e) noexcept {
+ COND_PRINT(debug_event, "DBTAdapter:hci:DeviceConnected(dev_id %d): %s", dev_id, e.toString().c_str());
+ const MgmtEvtDeviceConnected &event = *static_cast<const MgmtEvtDeviceConnected *>(&e);
EInfoReport ad_report;
{
ad_report.setSource(EInfoReport::Source::EIR);
@@ -1118,9 +1119,9 @@ bool DBTAdapter::mgmtEvDeviceConnectedHCI(std::shared_ptr<MgmtEvent> e) noexcept
return true;
}
-bool DBTAdapter::mgmtEvConnectFailedHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- COND_PRINT(debug_event, "DBTAdapter::EventHCI:ConnectFailed: %s", e->toString().c_str());
- const MgmtEvtDeviceConnectFailed &event = *static_cast<const MgmtEvtDeviceConnectFailed *>(e.get());
+bool DBTAdapter::mgmtEvConnectFailedHCI(const MgmtEvent& e) noexcept {
+ COND_PRINT(debug_event, "DBTAdapter::EventHCI:ConnectFailed: %s", e.toString().c_str());
+ const MgmtEvtDeviceConnectFailed &event = *static_cast<const MgmtEvtDeviceConnectFailed *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr != device ) {
@@ -1154,8 +1155,8 @@ bool DBTAdapter::mgmtEvConnectFailedHCI(std::shared_ptr<MgmtEvent> e) noexcept {
return true;
}
-bool DBTAdapter::mgmtEvHCIEncryptionChangedHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtHCIEncryptionChanged &event = *static_cast<const MgmtEvtHCIEncryptionChanged *>(e.get());
+bool DBTAdapter::mgmtEvHCIEncryptionChangedHCI(const MgmtEvent& e) noexcept {
+ const MgmtEvtHCIEncryptionChanged &event = *static_cast<const MgmtEvtHCIEncryptionChanged *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr != device ) {
@@ -1170,8 +1171,8 @@ bool DBTAdapter::mgmtEvHCIEncryptionChangedHCI(std::shared_ptr<MgmtEvent> e) noe
}
return true;
}
-bool DBTAdapter::mgmtEvHCIEncryptionKeyRefreshCompleteHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtHCIEncryptionKeyRefreshComplete &event = *static_cast<const MgmtEvtHCIEncryptionKeyRefreshComplete *>(e.get());
+bool DBTAdapter::mgmtEvHCIEncryptionKeyRefreshCompleteHCI(const MgmtEvent& e) noexcept {
+ const MgmtEvtHCIEncryptionKeyRefreshComplete &event = *static_cast<const MgmtEvtHCIEncryptionKeyRefreshComplete *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr != device ) {
@@ -1188,8 +1189,8 @@ bool DBTAdapter::mgmtEvHCIEncryptionKeyRefreshCompleteHCI(std::shared_ptr<MgmtEv
return true;
}
-bool DBTAdapter::mgmtEvHCILERemoteUserFeaturesHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtHCILERemoteUserFeatures &event = *static_cast<const MgmtEvtHCILERemoteUserFeatures *>(e.get());
+bool DBTAdapter::mgmtEvHCILERemoteUserFeaturesHCI(const MgmtEvent& e) noexcept {
+ const MgmtEvtHCILERemoteUserFeatures &event = *static_cast<const MgmtEvtHCILERemoteUserFeatures *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr != device ) {
@@ -1205,8 +1206,8 @@ bool DBTAdapter::mgmtEvHCILERemoteUserFeaturesHCI(std::shared_ptr<MgmtEvent> e)
return true;
}
-bool DBTAdapter::mgmtEvDeviceDisconnectedHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtDeviceDisconnected &event = *static_cast<const MgmtEvtDeviceDisconnected *>(e.get());
+bool DBTAdapter::mgmtEvDeviceDisconnectedHCI(const MgmtEvent& e) noexcept {
+ const MgmtEvtDeviceDisconnected &event = *static_cast<const MgmtEvtDeviceDisconnected *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr != device ) {
@@ -1244,15 +1245,15 @@ bool DBTAdapter::mgmtEvDeviceDisconnectedHCI(std::shared_ptr<MgmtEvent> e) noexc
return true;
}
-bool DBTAdapter::mgmtEvDeviceDisconnectedMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- COND_PRINT(debug_event, "DBTAdapter:mgmt:DeviceDisconnected: %s", e->toString().c_str());
- const MgmtEvtDeviceDisconnected &event = *static_cast<const MgmtEvtDeviceDisconnected *>(e.get());
+bool DBTAdapter::mgmtEvDeviceDisconnectedMgmt(const MgmtEvent& e) noexcept {
+ COND_PRINT(debug_event, "DBTAdapter:mgmt:DeviceDisconnected: %s", e.toString().c_str());
+ const MgmtEvtDeviceDisconnected &event = *static_cast<const MgmtEvtDeviceDisconnected *>(&e);
(void)event;
return true;
}
-bool DBTAdapter::mgmtEvPairDeviceCompleteMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtPairDeviceComplete &event = *static_cast<const MgmtEvtPairDeviceComplete *>(e.get());
+bool DBTAdapter::mgmtEvPairDeviceCompleteMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtPairDeviceComplete &event = *static_cast<const MgmtEvtPairDeviceComplete *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr != device ) {
@@ -1267,8 +1268,8 @@ bool DBTAdapter::mgmtEvPairDeviceCompleteMgmt(std::shared_ptr<MgmtEvent> e) noex
return true;
}
-bool DBTAdapter::mgmtEvNewLongTermKeyMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtNewLongTermKey& event = *static_cast<const MgmtEvtNewLongTermKey *>(e.get());
+bool DBTAdapter::mgmtEvNewLongTermKeyMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtNewLongTermKey& event = *static_cast<const MgmtEvtNewLongTermKey *>(&e);
const MgmtLongTermKeyInfo& ltk_info = event.getLongTermKey();
std::shared_ptr<DBTDevice> device = findConnectedDevice(ltk_info.address, ltk_info.address_type);
if( nullptr != device ) {
@@ -1286,9 +1287,9 @@ bool DBTAdapter::mgmtEvNewLongTermKeyMgmt(std::shared_ptr<MgmtEvent> e) noexcept
return true;
}
-bool DBTAdapter::mgmtEvDeviceFoundHCI(std::shared_ptr<MgmtEvent> e) noexcept {
- COND_PRINT(debug_event, "DBTAdapter:hci:DeviceFound(dev_id %d): %s", dev_id, e->toString().c_str());
- const MgmtEvtDeviceFound &deviceFoundEvent = *static_cast<const MgmtEvtDeviceFound *>(e.get());
+bool DBTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
+ COND_PRINT(debug_event, "DBTAdapter:hci:DeviceFound(dev_id %d): %s", dev_id, e.toString().c_str());
+ const MgmtEvtDeviceFound &deviceFoundEvent = *static_cast<const MgmtEvtDeviceFound *>(&e);
std::shared_ptr<EInfoReport> eir = deviceFoundEvent.getEIR();
if( nullptr == eir ) {
@@ -1376,18 +1377,18 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(std::shared_ptr<MgmtEvent> e) noexcept {
return true;
}
-bool DBTAdapter::mgmtEvDeviceUnpairedMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtDeviceUnpaired &event = *static_cast<const MgmtEvtDeviceUnpaired *>(e.get());
+bool DBTAdapter::mgmtEvDeviceUnpairedMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtDeviceUnpaired &event = *static_cast<const MgmtEvtDeviceUnpaired *>(&e);
DBG_PRINT("DBTAdapter:mgmt:DeviceUnpaired: %s", event.toString().c_str());
return true;
}
-bool DBTAdapter::mgmtEvPinCodeRequestMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtPinCodeRequest &event = *static_cast<const MgmtEvtPinCodeRequest *>(e.get());
+bool DBTAdapter::mgmtEvPinCodeRequestMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtPinCodeRequest &event = *static_cast<const MgmtEvtPinCodeRequest *>(&e);
DBG_PRINT("DBTAdapter:mgmt:PinCodeRequest: %s", event.toString().c_str());
return true;
}
-bool DBTAdapter::mgmtEvAuthFailedMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtAuthFailed &event = *static_cast<const MgmtEvtAuthFailed *>(e.get());
+bool DBTAdapter::mgmtEvAuthFailedMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtAuthFailed &event = *static_cast<const MgmtEvtAuthFailed *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr == device ) {
@@ -1400,8 +1401,8 @@ bool DBTAdapter::mgmtEvAuthFailedMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
device->updatePairingState(device, e, evtStatus, SMPPairingState::FAILED);
return true;
}
-bool DBTAdapter::mgmtEvUserConfirmRequestMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtUserConfirmRequest &event = *static_cast<const MgmtEvtUserConfirmRequest *>(e.get());
+bool DBTAdapter::mgmtEvUserConfirmRequestMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtUserConfirmRequest &event = *static_cast<const MgmtEvtUserConfirmRequest *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr == device ) {
@@ -1414,8 +1415,8 @@ bool DBTAdapter::mgmtEvUserConfirmRequestMgmt(std::shared_ptr<MgmtEvent> e) noex
device->updatePairingState(device, e, HCIStatusCode::SUCCESS, SMPPairingState::NUMERIC_COMPARE_EXPECTED);
return true;
}
-bool DBTAdapter::mgmtEvUserPasskeyRequestMgmt(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtUserPasskeyRequest &event = *static_cast<const MgmtEvtUserPasskeyRequest *>(e.get());
+bool DBTAdapter::mgmtEvUserPasskeyRequestMgmt(const MgmtEvent& e) noexcept {
+ const MgmtEvtUserPasskeyRequest &event = *static_cast<const MgmtEvtUserPasskeyRequest *>(&e);
std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType());
if( nullptr == device ) {
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index 0dfd3715..ca700e76 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -557,11 +557,11 @@ bool DBTDevice::checkPairingKeyDistributionComplete(const std::string& timestamp
return res;
}
-bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, std::shared_ptr<MgmtEvent> evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept {
+bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, const MgmtEvent& evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept {
const std::lock_guard<std::mutex> lock(mtx_pairing); // RAII-style acquire and relinquish via destructor
jau::sc_atomic_critical sync(sync_pairing);
- const MgmtEvent::Opcode mgmtEvtOpcode = evt->getOpcode();
+ const MgmtEvent::Opcode mgmtEvtOpcode = evt.getOpcode();
PairingMode mode = pairing_data.mode;
bool is_device_ready = false;
@@ -608,11 +608,11 @@ bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, std::shared
SMPPairingState::KEY_DISTRIBUTION == pairing_data.state )
{
// SMP pairing has started, mngr issued new LTK key command
- const MgmtEvtNewLongTermKey& event = *static_cast<const MgmtEvtNewLongTermKey *>(evt.get());
+ const MgmtEvtNewLongTermKey& event = *static_cast<const MgmtEvtNewLongTermKey *>(&evt);
const MgmtLongTermKeyInfo& ltk_info = event.getLongTermKey();
const SMPLongTermKeyInfo smp_ltk = ltk_info.toSMPLongTermKeyInfo();
if( smp_ltk.isValid() ) {
- const std::string timestamp = jau::uint64DecString(jau::environment::getElapsedMillisecond(evt->getTimestamp()), ',', 9);
+ const std::string timestamp = jau::uint64DecString(jau::environment::getElapsedMillisecond(evt.getTimestamp()), ',', 9);
const bool responder = ( SMPLongTermKeyInfo::Property::RESPONDER & smp_ltk.properties ) != SMPLongTermKeyInfo::Property::NONE;
if( responder ) {
@@ -660,15 +660,15 @@ bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, std::shared
DBG_PRINT("DBTDevice::updatePairingState.0: state %s -> %s, mode %s -> %s, ready %d, %s",
getSMPPairingStateString(pairing_data.state).c_str(), getSMPPairingStateString(claimed_state).c_str(),
getPairingModeString(pairing_data.mode).c_str(), getPairingModeString(mode).c_str(),
- is_device_ready, evt->toString().c_str());
+ is_device_ready, evt.toString().c_str());
pairing_data.mode = mode;
pairing_data.state = claimed_state;
- adapter.sendDevicePairingState(sthis, claimed_state, mode, evt->getTimestamp());
+ adapter.sendDevicePairingState(sthis, claimed_state, mode, evt.getTimestamp());
if( is_device_ready ) {
- std::thread dc(&DBTDevice::processDeviceReady, this, sthis, evt->getTimestamp()); // @suppress("Invalid arguments")
+ std::thread dc(&DBTDevice::processDeviceReady, this, sthis, evt.getTimestamp()); // @suppress("Invalid arguments")
dc.detach();
}
DBG_PRINT("DBTDevice::updatePairingState.2: End Complete: state %s, %s",
@@ -677,7 +677,7 @@ bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, std::shared
} else {
DBG_PRINT("DBTDevice::updatePairingState.3: End Unchanged: state %s, %s, %s",
getSMPPairingStateString(pairing_data.state).c_str(),
- evt->toString().c_str(), toString(false).c_str());
+ evt.toString().c_str(), toString(false).c_str());
}
return false;
}
@@ -1437,6 +1437,10 @@ void DBTDevice::notifyDisconnected() noexcept {
l2cap_att.close();
}
+void DBTDevice::sendMgmtEvDeviceDisconnected(std::unique_ptr<MgmtEvent> evt) noexcept {
+ adapter.mgmtEvDeviceDisconnectedHCI(*evt);
+}
+
HCIStatusCode DBTDevice::disconnect(const HCIStatusCode reason) noexcept {
// Avoid disconnect re-entry lock-free
bool expConn = true; // C++11, exp as value since C++20
@@ -1495,10 +1499,10 @@ exit:
// or in case the hci->disconnect() itself fails,
// send the DISCONN_COMPLETE event directly.
// SEND_EVENT: Perform off-thread to avoid potential deadlock w/ application callbacks (similar when sent from HCIHandler's reader-thread)
- std::thread bg(&DBTAdapter::mgmtEvDeviceDisconnectedHCI, &adapter, std::shared_ptr<MgmtEvent>( // @suppress("Invalid arguments")
- new MgmtEvtDeviceDisconnected(adapter.dev_id, addressAndType, reason, hciConnHandle.load()) ) );
+ std::thread bg(&DBTDevice::sendMgmtEvDeviceDisconnected, this, // @suppress("Invalid arguments")
+ std::make_unique<MgmtEvtDeviceDisconnected>(adapter.dev_id, addressAndType, reason, hciConnHandle.load()) );
bg.detach();
- // adapter.mgmtEvDeviceDisconnectedHCI( std::shared_ptr<MgmtEvent>( new MgmtEvtDeviceDisconnected(adapter.dev_id, address, addressType, reason, hciConnHandle.load()) ) );
+ // adapter.mgmtEvDeviceDisconnectedHCI( std::unique_ptr<MgmtEvent>( new MgmtEvtDeviceDisconnected(adapter.dev_id, address, addressType, reason, hciConnHandle.load()) ) );
}
WORDY_PRINT("DBTDevice::disconnect: End: status %s, handle 0x%X, isConnected %d/%d on %s",
getHCIStatusCodeString(res).c_str(),
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index e442c660..e98b55d6 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -113,7 +113,7 @@ void DBTManager::mgmtReaderThreadImpl() noexcept {
WARN_PRINT("DBTManager::reader: length mismatch %zu < MGMT_HEADER_SIZE(%u) + %u", len2, MGMT_HEADER_SIZE, paramSize);
continue; // discard data
}
- std::shared_ptr<MgmtEvent> event = MgmtEvent::getSpecialized(rbuffer.get_ptr(), len2);
+ std::unique_ptr<MgmtEvent> event = MgmtEvent::getSpecialized(rbuffer.get_ptr(), len2);
const MgmtEvent::Opcode opc = event->getOpcode();
if( MgmtEvent::Opcode::CMD_COMPLETE == opc || MgmtEvent::Opcode::CMD_STATUS == opc ) {
COND_PRINT(env.DEBUG_EVENT, "DBTManager-IO RECV (CMD) %s", event->toString().c_str());
@@ -122,19 +122,19 @@ void DBTManager::mgmtReaderThreadImpl() noexcept {
mgmtEventRing.drop(dropCount);
WARN_PRINT("DBTManager-IO RECV Drop (%u oldest elements of %u capacity, ring full)", dropCount, mgmtEventRing.capacity());
}
- mgmtEventRing.putBlocking( event );
+ mgmtEventRing.putBlocking( std::move( event ) );
} else if( MgmtEvent::Opcode::INDEX_ADDED == opc ) {
COND_PRINT(env.DEBUG_EVENT, "DBTManager-IO RECV (ADD) %s", event->toString().c_str());
- std::thread adapterAddedThread(&DBTManager::processAdapterAdded, this, event); // @suppress("Invalid arguments")
+ std::thread adapterAddedThread(&DBTManager::processAdapterAdded, this, std::move( event) ); // @suppress("Invalid arguments")
adapterAddedThread.detach();
} else if( MgmtEvent::Opcode::INDEX_REMOVED == opc ) {
COND_PRINT(env.DEBUG_EVENT, "DBTManager-IO RECV (REM) %s", event->toString().c_str());
- std::thread adapterRemovedThread(&DBTManager::processAdapterRemoved, this, event); // @suppress("Invalid arguments")
+ std::thread adapterRemovedThread(&DBTManager::processAdapterRemoved, this, std::move( event ) ); // @suppress("Invalid arguments")
adapterRemovedThread.detach();
} else {
// issue a callback
COND_PRINT(env.DEBUG_EVENT, "DBTManager-IO RECV (CB) %s", event->toString().c_str());
- sendMgmtEvent(event);
+ sendMgmtEvent( *event );
}
} else if( ETIMEDOUT != errno && !mgmtReaderShallStop ) { // expected exits
ERR_PRINT("DBTManager::reader: HCIComm read error");
@@ -151,9 +151,9 @@ void DBTManager::mgmtReaderThreadImpl() noexcept {
}
-void DBTManager::sendMgmtEvent(std::shared_ptr<MgmtEvent> event) noexcept {
- const uint16_t dev_id = event->getDevID();
- MgmtAdapterEventCallbackList & mgmtEventCallbackList = mgmtAdapterEventCallbackLists[static_cast<uint16_t>(event->getOpcode())];
+void DBTManager::sendMgmtEvent(const MgmtEvent& event) noexcept {
+ const uint16_t dev_id = event.getDevID();
+ MgmtAdapterEventCallbackList & mgmtEventCallbackList = mgmtAdapterEventCallbackLists[static_cast<uint16_t>(event.getOpcode())];
int invokeCount = 0;
jau::for_each_cow(mgmtEventCallbackList, [&](MgmtAdapterEventCallback &cb) {
@@ -169,7 +169,7 @@ void DBTManager::sendMgmtEvent(std::shared_ptr<MgmtEvent> event) noexcept {
}
});
- COND_PRINT(env.DEBUG_EVENT, "DBTManager::sendMgmtEvent: Event %s -> %d/%zd callbacks", event->toString().c_str(), invokeCount, mgmtEventCallbackList.size());
+ COND_PRINT(env.DEBUG_EVENT, "DBTManager::sendMgmtEvent: Event %s -> %d/%zd callbacks", event.toString().c_str(), invokeCount, mgmtEventCallbackList.size());
(void)invokeCount;
}
@@ -211,7 +211,7 @@ bool DBTManager::send(MgmtCommand &req) noexcept {
return true;
}
-std::shared_ptr<MgmtEvent> DBTManager::sendWithReply(MgmtCommand &req) noexcept {
+std::unique_ptr<MgmtEvent> DBTManager::sendWithReply(MgmtCommand &req) noexcept {
const std::lock_guard<std::recursive_mutex> lock(mtx_sendReply); // RAII-style acquire and relinquish via destructor
if( !send(req) ) {
return nullptr;
@@ -220,8 +220,8 @@ std::shared_ptr<MgmtEvent> DBTManager::sendWithReply(MgmtCommand &req) noexcept
// Ringbuffer read is thread safe
int32_t retryCount = 0;
while( retryCount < env.MGMT_READ_PACKET_MAX_RETRY ) {
- std::shared_ptr<MgmtEvent> res = mgmtEventRing.getBlocking(env.MGMT_COMMAND_REPLY_TIMEOUT);
- // std::shared_ptr<MgmtEvent> res = receiveNext();
+ std::unique_ptr<MgmtEvent> res = mgmtEventRing.getBlocking(env.MGMT_COMMAND_REPLY_TIMEOUT);
+ // std::unique_ptr<MgmtEvent> res = receiveNext();
if( nullptr == res ) {
errno = ETIMEDOUT;
ERR_PRINT("DBTManager::sendWithReply.X: nullptr result (timeout -> abort): req %s", req.toString().c_str());
@@ -258,7 +258,7 @@ std::shared_ptr<AdapterInfo> DBTManager::initAdapter(const uint16_t dev_id, cons
AdapterSetting current_settings;
MgmtCommand req0(MgmtCommand::Opcode::READ_INFO, dev_id);
{
- std::shared_ptr<MgmtEvent> res = sendWithReply(req0);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req0);
if( nullptr == res ) {
goto fail;
}
@@ -333,7 +333,7 @@ std::shared_ptr<AdapterInfo> DBTManager::initAdapter(const uint16_t dev_id, cons
adapterInfo->setCurrentSettingMask(current_settings);
} else {
adapterInfo = nullptr; // flush
- std::shared_ptr<MgmtEvent> res = sendWithReply(req0);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req0);
if( nullptr == res ) {
goto fail;
}
@@ -415,7 +415,7 @@ DBTManager::DBTManager(const BTMode _defaultBTMode) noexcept
// Mandatory
{
MgmtCommand req0(MgmtCommand::Opcode::READ_VERSION, MgmtConstU16::MGMT_INDEX_NONE);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req0);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req0);
if( nullptr == res ) {
goto fail;
}
@@ -435,7 +435,7 @@ DBTManager::DBTManager(const BTMode _defaultBTMode) noexcept
// Optional
{
MgmtCommand req0(MgmtCommand::Opcode::READ_COMMANDS, MgmtConstU16::MGMT_INDEX_NONE);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req0);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req0);
if( nullptr == res ) {
goto next1;
}
@@ -460,7 +460,7 @@ next1:
// Mandatory
{
MgmtCommand req0(MgmtCommand::Opcode::READ_INDEX_LIST, MgmtConstU16::MGMT_INDEX_NONE);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req0);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req0);
if( nullptr == res ) {
goto fail;
}
@@ -754,7 +754,7 @@ SMPIOCapability DBTManager::getIOCapability(const uint16_t dev_id) const noexcep
bool DBTManager::setMode(const uint16_t dev_id, const MgmtCommand::Opcode opc, const uint8_t mode, AdapterSetting& current_settings) noexcept {
MgmtUint8Cmd req(opc, dev_id, mode);
- std::shared_ptr<MgmtEvent> reply = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> reply = sendWithReply(req);
MgmtStatus res;
if( nullptr != reply ) {
if( reply->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
@@ -780,7 +780,7 @@ bool DBTManager::setMode(const uint16_t dev_id, const MgmtCommand::Opcode opc, c
MgmtStatus DBTManager::setDiscoverable(const uint16_t dev_id, const uint8_t state, const uint16_t timeout_sec, AdapterSetting& current_settings) noexcept {
MgmtSetDiscoverableCmd req(dev_id, state, timeout_sec);
- std::shared_ptr<MgmtEvent> reply = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> reply = sendWithReply(req);
MgmtStatus res;
if( nullptr != reply ) {
if( reply->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
@@ -809,7 +809,7 @@ ScanType DBTManager::startDiscovery(const uint16_t dev_id, const BTMode btMode)
ScanType DBTManager::startDiscovery(const uint16_t dev_id, const ScanType scanType) noexcept {
MgmtUint8Cmd req(MgmtCommand::Opcode::START_DISCOVERY, dev_id, number(scanType));
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
ScanType type = ScanType::NONE;
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
@@ -826,7 +826,7 @@ ScanType DBTManager::startDiscovery(const uint16_t dev_id, const ScanType scanTy
}
bool DBTManager::stopDiscovery(const uint16_t dev_id, const ScanType type) noexcept {
MgmtUint8Cmd req(MgmtCommand::Opcode::STOP_DISCOVERY, dev_id, number(type));
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
return MgmtStatus::SUCCESS == res1.getStatus();
@@ -839,7 +839,7 @@ bool DBTManager::uploadConnParam(const uint16_t dev_id, const BDAddressAndType &
const uint16_t conn_latency, const uint16_t supervision_timeout) noexcept {
MgmtConnParam connParam{ addressAndType.address, addressAndType.type, conn_min_interval, conn_max_interval, conn_latency, supervision_timeout };
MgmtLoadConnParamCmd req(dev_id, connParam);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
return MgmtStatus::SUCCESS == res1.getStatus();
@@ -849,7 +849,7 @@ bool DBTManager::uploadConnParam(const uint16_t dev_id, const BDAddressAndType &
MgmtStatus DBTManager::uploadLinkKey(const uint16_t dev_id, const bool debug_keys, const MgmtLinkKeyInfo &key) noexcept {
MgmtLoadLinkKeyCmd req(dev_id, debug_keys, key);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
return res1.getStatus();
@@ -860,7 +860,7 @@ MgmtStatus DBTManager::uploadLinkKey(const uint16_t dev_id, const bool debug_key
HCIStatusCode DBTManager::uploadLongTermKey(const uint16_t dev_id, const MgmtLongTermKeyInfo &key) noexcept {
MgmtLoadLongTermKeyCmd req(dev_id, key);
HCIStatusCode res;
- std::shared_ptr<MgmtEvent> reply = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> reply = sendWithReply(req);
if( nullptr != reply ) {
if( reply->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
res = getHCIStatusCode( static_cast<const MgmtEvtCmdComplete *>(reply.get())->getStatus() );
@@ -884,7 +884,7 @@ HCIStatusCode DBTManager::uploadLongTermKeyInfo(const uint16_t dev_id, const BDA
const MgmtLongTermKeyInfo mgmt_ltk_info { addressAndType.address, addressAndType.type, key_type, responder, ltk.enc_size, ltk.ediv, ltk.rand, ltk.ltk };
MgmtLoadLongTermKeyCmd req(dev_id, mgmt_ltk_info);
HCIStatusCode res;
- std::shared_ptr<MgmtEvent> reply = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> reply = sendWithReply(req);
if( nullptr != reply ) {
if( reply->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
res = getHCIStatusCode( static_cast<const MgmtEvtCmdComplete *>(reply.get())->getStatus() );
@@ -903,7 +903,7 @@ HCIStatusCode DBTManager::uploadLongTermKeyInfo(const uint16_t dev_id, const BDA
MgmtStatus DBTManager::userPasskeyReply(const uint16_t dev_id, const BDAddressAndType & addressAndType, const uint32_t passkey) noexcept {
MgmtUserPasskeyReplyCmd cmd(dev_id, addressAndType, passkey);
- std::shared_ptr<MgmtEvent> res = sendWithReply(cmd);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(cmd);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
// FIXME: Analyze address + addressType result?
@@ -914,7 +914,7 @@ MgmtStatus DBTManager::userPasskeyReply(const uint16_t dev_id, const BDAddressAn
MgmtStatus DBTManager::userPasskeyNegativeReply(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept {
MgmtUserPasskeyNegativeReplyCmd cmd(dev_id, addressAndType);
- std::shared_ptr<MgmtEvent> res = sendWithReply(cmd);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(cmd);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
// FIXME: Analyze address + addressType result?
@@ -924,7 +924,7 @@ MgmtStatus DBTManager::userPasskeyNegativeReply(const uint16_t dev_id, const BDA
}
MgmtStatus DBTManager::userConfirmReply(const uint16_t dev_id, const BDAddressAndType & addressAndType, const bool positive) noexcept {
- std::shared_ptr<MgmtEvent> res;
+ std::unique_ptr<MgmtEvent> res;
if( positive ) {
MgmtUserConfirmReplyCmd cmd(dev_id, addressAndType);
res = sendWithReply(cmd);
@@ -947,7 +947,7 @@ bool DBTManager::pairDevice(const uint16_t dev_id, const BDAddressAndType & addr
MgmtStatus DBTManager::unpairDevice(const uint16_t dev_id, const BDAddressAndType & addressAndType, const bool disconnect) noexcept {
MgmtUnpairDeviceCmd cmd(dev_id, addressAndType, disconnect);
- std::shared_ptr<MgmtEvent> res = sendWithReply(cmd);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(cmd);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
@@ -977,7 +977,7 @@ bool DBTManager::addDeviceToWhitelist(const uint16_t dev_id, const BDAddressAndT
ERR_PRINT("DBTManager::addDeviceToWhitelist: Already in local whitelist, remove first: %s", req.toString().c_str());
return false;
}
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
if( MgmtStatus::SUCCESS == res1.getStatus() ) {
@@ -1029,7 +1029,7 @@ bool DBTManager::removeDeviceFromWhitelist(const uint16_t dev_id, const BDAddres
// Actual removal
MgmtRemoveDeviceFromWhitelistCmd req(dev_id, addressAndType);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
if( MgmtStatus::SUCCESS == res1.getStatus() ) {
@@ -1049,7 +1049,7 @@ bool DBTManager::disconnect(const bool ioErrorCause,
// This will always notify the adapter of a disconnected device.
{
MgmtDisconnectCmd req(dev_id, addressAndType);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
if( MgmtStatus::SUCCESS == res1.getStatus() ) {
@@ -1060,15 +1060,15 @@ bool DBTManager::disconnect(const bool ioErrorCause,
if( !ioErrorCause ) {
// In case of an ioError (lost-connection), don't wait for the lagging
// DISCONN_COMPLETE event but send it directly.
- MgmtEvtDeviceDisconnected *e = new MgmtEvtDeviceDisconnected(dev_id, addressAndType, reason, 0xffff);
- sendMgmtEvent(std::shared_ptr<MgmtEvent>(e));
+ const MgmtEvtDeviceDisconnected e(dev_id, addressAndType, reason, 0xffff);
+ sendMgmtEvent(e);
}
return bres;
}
std::shared_ptr<ConnectionInfo> DBTManager::getConnectionInfo(const uint16_t dev_id, const BDAddressAndType& addressAndType) noexcept {
MgmtGetConnectionInfoCmd req(dev_id, addressAndType);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
if( MgmtStatus::SUCCESS == res1.getStatus() ) {
@@ -1081,15 +1081,15 @@ std::shared_ptr<ConnectionInfo> DBTManager::getConnectionInfo(const uint16_t dev
std::shared_ptr<NameAndShortName> DBTManager::setLocalName(const uint16_t dev_id, const std::string & name, const std::string & short_name) noexcept {
MgmtSetLocalNameCmd req (static_cast<uint16_t>(dev_id), name, short_name);
- std::shared_ptr<MgmtEvent> res = sendWithReply(req);
+ std::unique_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
if( MgmtStatus::SUCCESS == res1.getStatus() ) {
std::shared_ptr<NameAndShortName> result = res1.toNameAndShortName();
// explicit LocalNameChanged event
- MgmtEvtLocalNameChanged * e = new MgmtEvtLocalNameChanged(static_cast<uint16_t>(dev_id), result->getName(), result->getShortName());
- sendMgmtEvent(std::shared_ptr<MgmtEvent>(e));
+ MgmtEvtLocalNameChanged e(static_cast<uint16_t>(dev_id), result->getName(), result->getShortName());
+ sendMgmtEvent(e);
return result;
}
}
@@ -1156,13 +1156,13 @@ void DBTManager::clearAllCallbacks() noexcept {
mgmtChangedAdapterSetCallbackList.clear();
}
-void DBTManager::processAdapterAdded(std::shared_ptr<MgmtEvent> e) noexcept {
+void DBTManager::processAdapterAdded(std::unique_ptr<MgmtEvent> e) noexcept {
const uint16_t dev_id = e->getDevID();
std::shared_ptr<AdapterInfo> ai = initAdapter(dev_id, defaultBTMode);
if( nullptr != ai ) {
const bool added = addAdapterInfo(ai);
DBG_PRINT("DBTManager::Adapter[%d] Added: Start %s, added %d", dev_id, ai->toString().c_str(), added);
- sendMgmtEvent(e);
+ sendMgmtEvent(*e);
DBG_PRINT("DBTManager::Adapter[%d] Added: User_ %s", dev_id, ai->toString().c_str());
jau::for_each_cow(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) {
cb.invoke(true /* added */, *ai);
@@ -1172,12 +1172,12 @@ void DBTManager::processAdapterAdded(std::shared_ptr<MgmtEvent> e) noexcept {
DBG_PRINT("DBTManager::Adapter[%d] Added: InitAI failed", dev_id);
}
}
-void DBTManager::processAdapterRemoved(std::shared_ptr<MgmtEvent> e) noexcept {
+void DBTManager::processAdapterRemoved(std::unique_ptr<MgmtEvent> e) noexcept {
const uint16_t dev_id = e->getDevID();
std::shared_ptr<AdapterInfo> ai = removeAdapterInfo(dev_id);
if( nullptr != ai ) {
DBG_PRINT("DBTManager::Adapter[%d] Removed: Start: %s", dev_id, ai->toString().c_str());
- sendMgmtEvent(e);
+ sendMgmtEvent(*e);
DBG_PRINT("DBTManager::Adapter[%d] Removed: User_: %s", dev_id, ai->toString().c_str());
jau::for_each_cow(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) {
cb.invoke(false /* added */, *ai);
@@ -1187,8 +1187,8 @@ void DBTManager::processAdapterRemoved(std::shared_ptr<MgmtEvent> e) noexcept {
DBG_PRINT("DBTManager::Adapter[%d] Removed: RemoveAI failed", dev_id);
}
}
-bool DBTManager::mgmtEvNewSettingsCB(std::shared_ptr<MgmtEvent> e) noexcept {
- const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(e.get());
+bool DBTManager::mgmtEvNewSettingsCB(const MgmtEvent& e) noexcept {
+ const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(&e);
std::shared_ptr<AdapterInfo> adapterInfo = getAdapterInfo(event.getDevID());
if( nullptr != adapterInfo ) {
const AdapterSetting old_settings = adapterInfo->getCurrentSettingMask();
@@ -1197,18 +1197,18 @@ bool DBTManager::mgmtEvNewSettingsCB(std::shared_ptr<MgmtEvent> e) noexcept {
event.getDevID(),
getAdapterSettingMaskString(old_settings).c_str(),
getAdapterSettingMaskString(new_settings).c_str(),
- e->toString().c_str());
+ e.toString().c_str());
} else {
DBG_PRINT("DBTManager:mgmt:NewSettings: Adapter[%d] %s -> adapter not present - %s",
event.getDevID(),
getAdapterSettingMaskString(event.getSettings()).c_str(),
- e->toString().c_str());
+ e.toString().c_str());
}
return true;
}
-bool DBTManager::mgmtEventAnyCB(std::shared_ptr<MgmtEvent> e) noexcept {
- DBG_PRINT("DBTManager:mgmt:Any: %s", e->toString().c_str());
+bool DBTManager::mgmtEventAnyCB(const MgmtEvent& e) noexcept {
+ DBG_PRINT("DBTManager:mgmt:Any: %s", e.toString().c_str());
(void)e;
return true;
}
diff --git a/src/direct_bt/MgmtTypes.cpp b/src/direct_bt/MgmtTypes.cpp
index 682b5ab8..d78130e5 100644
--- a/src/direct_bt/MgmtTypes.cpp
+++ b/src/direct_bt/MgmtTypes.cpp
@@ -319,73 +319,71 @@ std::string MgmtEvent::getOpcodeString(const Opcode opc) noexcept {
return "Unknown Opcode";
}
-std::shared_ptr<MgmtEvent> MgmtEvent::getSpecialized(const uint8_t * buffer, jau::nsize_t const buffer_size) noexcept {
+std::unique_ptr<MgmtEvent> MgmtEvent::getSpecialized(const uint8_t * buffer, jau::nsize_t const buffer_size) noexcept {
const MgmtEvent::Opcode opc = MgmtEvent::getOpcode(buffer);
- MgmtEvent * res;
switch( opc ) {
case MgmtEvent::Opcode::CMD_COMPLETE: {
const MgmtCommand::Opcode cmdOpcode = MgmtEvtCmdComplete::getCmdOpcode(buffer);
if( buffer_size >= MgmtEvtAdapterInfo::getRequiredTotalSize() &&
MgmtCommand::Opcode::READ_INFO == cmdOpcode ) {
- res = new MgmtEvtAdapterInfo(buffer, buffer_size);
+ return std::make_unique<MgmtEvtAdapterInfo>(buffer, buffer_size);
} else if( buffer_size >= MgmtEvtPairDeviceComplete::getRequiredTotalSize() &&
MgmtCommand::Opcode::PAIR_DEVICE == cmdOpcode ) {
- res = new MgmtEvtPairDeviceComplete(buffer, buffer_size);
+ return std::make_unique<MgmtEvtPairDeviceComplete>(buffer, buffer_size);
} else {
- res = new MgmtEvtCmdComplete(buffer, buffer_size);
+ return std::make_unique<MgmtEvtCmdComplete>(buffer, buffer_size);
}
- } break;
+ }
case MgmtEvent::Opcode::CMD_STATUS:
- res = new MgmtEvtCmdStatus(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtCmdStatus>(buffer, buffer_size);
case MgmtEvent::Opcode::CONTROLLER_ERROR:
- res = new MgmtEvtControllerError(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtControllerError>(buffer, buffer_size);
case MgmtEvent::Opcode::INDEX_ADDED:
- res = new MgmtEvent(buffer, buffer_size, 0); break;
+ return std::make_unique<MgmtEvent>(buffer, buffer_size, 0);
case MgmtEvent::Opcode::INDEX_REMOVED:
- res = new MgmtEvent(buffer, buffer_size, 0); break;
+ return std::make_unique<MgmtEvent>(buffer, buffer_size, 0);
case MgmtEvent::Opcode::NEW_SETTINGS:
- res = new MgmtEvtNewSettings(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtNewSettings>(buffer, buffer_size);
case MgmtEvent::Opcode::LOCAL_NAME_CHANGED:
- res = new MgmtEvtLocalNameChanged(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtLocalNameChanged>(buffer, buffer_size);
case Opcode::NEW_LINK_KEY:
- res = new MgmtEvtNewLinkKey(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtNewLinkKey>(buffer, buffer_size);
case Opcode::NEW_LONG_TERM_KEY:
- res = new MgmtEvtNewLongTermKey(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtNewLongTermKey>(buffer, buffer_size);
case MgmtEvent::Opcode::DEVICE_CONNECTED:
- res = new MgmtEvtDeviceConnected(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceConnected>(buffer, buffer_size);
case MgmtEvent::Opcode::DEVICE_DISCONNECTED:
- res = new MgmtEvtDeviceDisconnected(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceDisconnected>(buffer, buffer_size);
case MgmtEvent::Opcode::CONNECT_FAILED:
- res = new MgmtEvtDeviceConnectFailed(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceConnectFailed>(buffer, buffer_size);
case MgmtEvent::Opcode::PIN_CODE_REQUEST:
- res = new MgmtEvtPinCodeRequest(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtPinCodeRequest>(buffer, buffer_size);
case MgmtEvent::Opcode::USER_CONFIRM_REQUEST:
- res = new MgmtEvtUserConfirmRequest(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtUserConfirmRequest>(buffer, buffer_size);
case MgmtEvent::Opcode::USER_PASSKEY_REQUEST:
- res = new MgmtEvtUserPasskeyRequest(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtUserPasskeyRequest>(buffer, buffer_size);
case Opcode::AUTH_FAILED:
- res = new MgmtEvtAuthFailed(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtAuthFailed>(buffer, buffer_size);
case MgmtEvent::Opcode::DEVICE_FOUND:
- res = new MgmtEvtDeviceFound(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceFound>(buffer, buffer_size);
case MgmtEvent::Opcode::DISCOVERING:
- res = new MgmtEvtDiscovering(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDiscovering>(buffer, buffer_size);
case MgmtEvent::Opcode::DEVICE_UNPAIRED:
- res = new MgmtEvtDeviceUnpaired(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceUnpaired>(buffer, buffer_size);
case Opcode::NEW_IRK:
- res = new MgmtEvtNewIdentityResolvingKey(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtNewIdentityResolvingKey>(buffer, buffer_size);
case Opcode::NEW_CSRK:
- res = new MgmtEvtNewSignatureResolvingKey(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtNewSignatureResolvingKey>(buffer, buffer_size);
case MgmtEvent::Opcode::DEVICE_WHITELIST_ADDED:
- res = new MgmtEvtDeviceWhitelistAdded(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceWhitelistAdded>(buffer, buffer_size);
case MgmtEvent::Opcode::DEVICE_WHITELIST_REMOVED:
- res = new MgmtEvtDeviceWhitelistRemoved(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtDeviceWhitelistRemoved>(buffer, buffer_size);
case MgmtEvent::Opcode::NEW_CONN_PARAM:
- res = new MgmtEvtNewConnectionParam(buffer, buffer_size); break;
+ return std::make_unique<MgmtEvtNewConnectionParam>(buffer, buffer_size);
default:
- res = new MgmtEvent(buffer, buffer_size, 0); break;
+ return std::make_unique<MgmtEvent>(buffer, buffer_size, 0);
}
- return std::shared_ptr<MgmtEvent>( res );
}
// *************************************************