From d758727ad96e2d790f416b74acb95944a985c257 Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:06:18 +0800 Subject: [PATCH 1/7] Description:nfc codex Feature or Bugfix:Feature Binary Source:No Signed-off-by: zhangjunsheng1002 --- .../hdi_service/connected_nfc_tag_impl.cpp | 96 +++++++++++++++++-- .../hdi_service/connected_nfc_tag_impl.h | 9 ++ .../hdi_service/remote_death_recipient.h | 52 ++++++++++ 3 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 connected_nfc_tag/hdi_service/remote_death_recipient.h diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp index b1b8378ac8..f9d770120a 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "v1_1/connected_nfc_tag_service.h" #include "connected_nfc_tag_vendor_adapter.h" @@ -32,12 +33,12 @@ namespace ConnectedNfcTag { namespace V1_1 { static sptr g_callbackV1_1 = nullptr; -static std::mutex g_callbackMutex; +static std::mutex g_callbackMutex {}; static const int MAX_NDEF_LEN = 256; static int EventCallback(uint8_t event, uint8_t *buff, uint32_t buffLen) { - std::lock_guard lock(g_callbackMutex); + std::lock_guard guard(g_callbackMutex); if (g_callbackV1_1 != nullptr && buff != nullptr && buffLen > 0) { std::vector data(buff, buff + buffLen); g_callbackV1_1->OnChipEvent((ConnectedNfcTagEvent)event, data); @@ -50,10 +51,20 @@ extern "C" IConnectedNfcTag *ConnectedNfcTagImplGetInstance(void) return new (std::nothrow) ConnectedNfcTagImpl(); } +ConnectedNfcTagImpl::ConnectedNfcTagImpl() +{ + remoteDeathRecipient = + new RemoteDeathRecipient(std::bind(&ConnectedNfcTagImpl::OnRemoteDied, this, std::placeholders::_1)); +} + ConnectedNfcTagImpl::~ConnectedNfcTagImpl() { - HDF_LOGI("%{public}s", __func__); - adapter.UnInit(); + std::lock_guard guard(g_callbackMutex); + if (callbackObj_ != nullptr) { + RemoveDeathRecipient(callbackObj_); + callbackObj_ = nullptr; + } + g_callbackV1_1 = nullptr; } int32_t ConnectedNfcTagImpl::RegisterCallBack( @@ -61,15 +72,24 @@ int32_t ConnectedNfcTagImpl::RegisterCallBack( { HDF_LOGI("%{public}s", __func__); - if (callbackObj == nullptr) { - HDF_LOGW("%{public}s: callbackObj NULL", __func__); - std::lock_guard lock(g_callbackMutex); - g_callbackV1_1 = nullptr; - return HDF_SUCCESS; - } { std::lock_guard lock(g_callbackMutex); + if (callbackObj != nullptr) { + RemoveDeathRecipient(callbackObj_); + callbackObj_ = nullptr; + g_callbackV1_1 = nullptr; + } + } + if (callbackObj == nullptr) { + HDF_LOGW("%{public}s: callbackObj NULL", __func__); + } else { + callbackObj_ = callbackObj; g_callbackV1_1 = callbackObj; + AddDeathRecipient(callbackObj_); + } + + if (callbackObj == nullptr) { + return adapter.RegisterCallBack(nullptr); } return adapter.RegisterCallBack(EventCallback); } @@ -117,6 +137,62 @@ int32_t ConnectedNfcTagImpl::WriteNdefTag(const std::string &ndefData) return -1; } +void ConnectedNfcTagImpl::OnRemoteDied(const wptr &object) +{ + HDF_LOGW("%{public}s: callback process died, cleaning up", __func__); + { + std::lock_guard guard(g_callbackMutex); + if (callbackObj_ != nullptr) { + RemoteDeathRecipient(callbackObj_); + callbackObj_ = nullptr; + } + g_callbackV1_1 == nullptr; + } + adapter.RegisterCallBack(nullptr); +} + +int32_t ConnectedNfcTagImpl::AddDeathRecipient( + const sptr& callbackObj) +{ + if (callbackObj == nullptr) { + HDF_LOGE("%{public}s: callbackObj nullptr", __func__); + return HDF_FAILURE; + } + const stpr &remote = + OHOS::HDI::hdi_objcast(callbackObj); + if (remote == nullptr) { + HDF_LOGE("%{public}s: remote nullptr", __func__); + return HDF_FAILURE; + } + bool result = remote->AddDeathRecipient(remoteDeathRecipient_); + if (!result) { + HDF_LOGE("%{public}s: AddDeathRecipient failed", __func__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +int32_t ConnectedNfcTagImpl::RemoveDeathRecipient( + const sptr& callbackObj) +{ + if (callbackObj == nullptr) { + HDF_LOGE("%{public}s: callbackObj nullptr", __func__); + return HDF_FAILURE; + } + const stpr &remote = + OHOS::HDI::hdi_objcast(callbackObj); + if (remote == nullptr) { + HDF_LOGE("%{public}s: remote nullptr", __func__); + return HDF_FAILURE; + } + bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_); + if (!result) { + HDF_LOGE("%{public}s: RemoveDeathRecipient failed", __func__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + } // namespace V1_1 } // namespace ConnectedNfcTag } // namespace HDI diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.h b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.h index 9a53c4c9c4..1104a9d3d3 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.h +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.h @@ -17,10 +17,12 @@ #define CONNECTED_NFC_TAG_IMPL_H #include +#include #include #include "v1_1/iconnected_nfc_tag.h" #include "connected_nfc_tag_vendor_adapter.h" +#include "remote_death_recipient.h" namespace OHOS { namespace HDI { @@ -40,8 +42,15 @@ public: int32_t ReadNdefTag(std::string &ndefData) override; int32_t WriteNdefTag(const std::string &ndefData) override; +private: + void OnRemoteDied(const wptr &object); + int32_t AddDeathRecipient(const sptr& callbackObj); + int32_t RemoveDeathRecipient(const sptr& callbackObj); + private: ConnectedNfcTagVendorAdapter adapter; + sptr callbackObj_ = nullptr; + sptr remote_death_recipient_ = nullptr; }; } // namespace V1_1 } // namespace ConnectedNfcTag diff --git a/connected_nfc_tag/hdi_service/remote_death_recipient.h b/connected_nfc_tag/hdi_service/remote_death_recipient.h new file mode 100644 index 0000000000..ceafe76074 --- /dev/null +++ b/connected_nfc_tag/hdi_service/remote_death_recipient.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_HDI_CONNECTED_NFC_TAG_REMOTE_DEATH_RECIPIENT_H +#define OHOS_HDI_CONNECTED_NFC_TAG_REMOTE_DEATH_RECIPIENT_H + +#include + +#include "iremote_object.h" +#include "refbase.h" + +namespace OHOS { +namespace HDI { +namespace ConnectedNfcTag { +class RemoteDeathRecipient : public IRemoteObject::DeathRecipient { +public: + explicit RemoteDeathRecipient(std::function &)> callback) + { + callback_ = callback; + } + ~RemoteDeathRecipient() + { + callback_ = nullptr; + } + + void OnRemoteDied(const wptr &object) + { + if (callback_ != nullptr) { + callback_(object); + } + } + +private: + std::function &)> callback_; +}; +} // namespace ConnectedNfcTag +} // namespace HDI +} // namespace OHOS + +#endif // OHOS_HDI_CONNECTED_NFC_TAG_REMOTE_DEATH_RECIPIENT_H From 1c39c4170097ac8170bceaba0713238477aa80c0 Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:08:47 +0800 Subject: [PATCH 2/7] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20connected=5Fnfc=5Ftag=5Fimpl.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangjunsheng1002 --- connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp index f9d770120a..db43b150ea 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp @@ -53,7 +53,7 @@ extern "C" IConnectedNfcTag *ConnectedNfcTagImplGetInstance(void) ConnectedNfcTagImpl::ConnectedNfcTagImpl() { - remoteDeathRecipient = + remoteDeathRecipient_ = new RemoteDeathRecipient(std::bind(&ConnectedNfcTagImpl::OnRemoteDied, this, std::placeholders::_1)); } From 700fad9415c019680e5addc120e89d43cb99a34d Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:14:03 +0800 Subject: [PATCH 3/7] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20connected=5Fnfc=5Ftag=5Fimpl.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangjunsheng1002 --- connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp index db43b150ea..35bb637c60 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp @@ -74,7 +74,7 @@ int32_t ConnectedNfcTagImpl::RegisterCallBack( { std::lock_guard lock(g_callbackMutex); - if (callbackObj != nullptr) { + if (callbackObj_ != nullptr) { RemoveDeathRecipient(callbackObj_); callbackObj_ = nullptr; g_callbackV1_1 = nullptr; From 4596a4e6a29318bb89ea0653a52fa220289dd4dc Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:15:12 +0800 Subject: [PATCH 4/7] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20connected=5Fnfc=5Ftag=5Fimpl.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangjunsheng1002 --- connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp index 35bb637c60..9b20176b0c 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp @@ -73,7 +73,7 @@ int32_t ConnectedNfcTagImpl::RegisterCallBack( HDF_LOGI("%{public}s", __func__); { - std::lock_guard lock(g_callbackMutex); + std::lock_guard guard(g_callbackMutex); if (callbackObj_ != nullptr) { RemoveDeathRecipient(callbackObj_); callbackObj_ = nullptr; From d4ff79482f2168bfc173c32fecb784dce0c99ea0 Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:17:14 +0800 Subject: [PATCH 5/7] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20connected=5Fnfc=5Ftag=5Fimpl.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangjunsheng1002 --- connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp index 9b20176b0c..819e0bf04b 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp @@ -146,7 +146,7 @@ void ConnectedNfcTagImpl::OnRemoteDied(const wptr &object) RemoteDeathRecipient(callbackObj_); callbackObj_ = nullptr; } - g_callbackV1_1 == nullptr; + g_callbackV1_1 = nullptr; } adapter.RegisterCallBack(nullptr); } From 942a29a88ca67f829288df19dc5860949a2886cc Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:22:47 +0800 Subject: [PATCH 6/7] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20remote=5Fdeath=5Frecipient.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangjunsheng1002 --- connected_nfc_tag/hdi_service/remote_death_recipient.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/connected_nfc_tag/hdi_service/remote_death_recipient.h b/connected_nfc_tag/hdi_service/remote_death_recipient.h index ceafe76074..3aa77459f4 100644 --- a/connected_nfc_tag/hdi_service/remote_death_recipient.h +++ b/connected_nfc_tag/hdi_service/remote_death_recipient.h @@ -45,8 +45,8 @@ public: private: std::function &)> callback_; }; -} // namespace ConnectedNfcTag -} // namespace HDI -} // namespace OHOS +} // ConnectedNfcTag +} // HDI +} // OHOS #endif // OHOS_HDI_CONNECTED_NFC_TAG_REMOTE_DEATH_RECIPIENT_H From c51b9d55234328ce3cefce9b8ad1be699eca3093 Mon Sep 17 00:00:00 2001 From: zhangjunsheng1002 Date: Tue, 4 Aug 2026 15:32:46 +0800 Subject: [PATCH 7/7] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20connected=5Fnfc=5Ftag=5Fimpl.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangjunsheng1002 --- connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp index 819e0bf04b..c054ebc911 100644 --- a/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp +++ b/connected_nfc_tag/hdi_service/connected_nfc_tag_impl.cpp @@ -190,7 +190,7 @@ int32_t ConnectedNfcTagImpl::RemoveDeathRecipient( HDF_LOGE("%{public}s: RemoveDeathRecipient failed", __func__); return HDF_FAILURE; } - return HDF_SUCCESS; + return HDF_SUCCESS; } } // namespace V1_1