!9228 merge master into master

Description:nfc app crash

Created-by: zhangjunsheng1002
Commit-by: zhangjunsheng1002
Merged-by: openharmony_ci
Description: ### 相关的Issue


### 原因(目的、解决的问题等)


### 描述(做了什么,变更了什么)


### 测试用例(新增、改动、可能影响的功能)






See merge request: openharmony/drivers_peripheral!9228
This commit is contained in:
openharmony_ci
2026-08-04 16:58:40 +08:00
3 changed files with 147 additions and 10 deletions
@@ -17,6 +17,7 @@
#include <mutex>
#include <hdf_base.h>
#include <hdf_log.h>
#include <iproxy_broker.h>
#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<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback> 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<std::mutex> lock(g_callbackMutex);
std::lock_guard<std::mutex> guard(g_callbackMutex);
if (g_callbackV1_1 != nullptr && buff != nullptr && buffLen > 0) {
std::vector<uint8_t> 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<std::mutex> 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__);
{
std::lock_guard<std::mutex> guard(g_callbackMutex);
if (callbackObj_ != nullptr) {
RemoveDeathRecipient(callbackObj_);
callbackObj_ = nullptr;
g_callbackV1_1 = nullptr;
}
}
if (callbackObj == nullptr) {
HDF_LOGW("%{public}s: callbackObj NULL", __func__);
std::lock_guard<std::mutex> lock(g_callbackMutex);
g_callbackV1_1 = nullptr;
return HDF_SUCCESS;
}
{
std::lock_guard<std::mutex> lock(g_callbackMutex);
} 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<IRemoteObject> &object)
{
HDF_LOGW("%{public}s: callback process died, cleaning up", __func__);
{
std::lock_guard<std::mutex> guard(g_callbackMutex);
if (callbackObj_ != nullptr) {
RemoteDeathRecipient(callbackObj_);
callbackObj_ = nullptr;
}
g_callbackV1_1 = nullptr;
}
adapter.RegisterCallBack(nullptr);
}
int32_t ConnectedNfcTagImpl::AddDeathRecipient(
const sptr<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback>& callbackObj)
{
if (callbackObj == nullptr) {
HDF_LOGE("%{public}s: callbackObj nullptr", __func__);
return HDF_FAILURE;
}
const stpr<IRemoteObject> &remote =
OHOS::HDI::hdi_objcast<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback>(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<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback>& callbackObj)
{
if (callbackObj == nullptr) {
HDF_LOGE("%{public}s: callbackObj nullptr", __func__);
return HDF_FAILURE;
}
const stpr<IRemoteObject> &remote =
OHOS::HDI::hdi_objcast<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback>(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
@@ -17,10 +17,12 @@
#define CONNECTED_NFC_TAG_IMPL_H
#include <cstdint>
#include <mutex>
#include <string>
#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<IRemoteObjects> &object);
int32_t AddDeathRecipient(const sptr<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback>& callbackObj);
int32_t RemoveDeathRecipient(const sptr<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback>& callbackObj);
private:
ConnectedNfcTagVendorAdapter adapter;
sptr<OHOS::HDI::ConnectedNfcTag::V1_1::IConnectedNfcTagCallback> callbackObj_ = nullptr;
sptr<RemoteDeathRecipient> remote_death_recipient_ = nullptr;
};
} // namespace V1_1
} // namespace ConnectedNfcTag
@@ -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 <functional>
#include "iremote_object.h"
#include "refbase.h"
namespace OHOS {
namespace HDI {
namespace ConnectedNfcTag {
class RemoteDeathRecipient : public IRemoteObject::DeathRecipient {
public:
explicit RemoteDeathRecipient(std::function<void(const wptr<IRemoteObject> &)> callback)
{
callback_ = callback;
}
~RemoteDeathRecipient()
{
callback_ = nullptr;
}
void OnRemoteDied(const wptr<IRemoteObject> &object)
{
if (callback_ != nullptr) {
callback_(object);
}
}
private:
std::function<void(const wptr<IRemoteObject> &)> callback_;
};
} // ConnectedNfcTag
} // HDI
} // OHOS
#endif // OHOS_HDI_CONNECTED_NFC_TAG_REMOTE_DEATH_RECIPIENT_H