From f5939155bc0b5f8b675feb15f896c2ccf57ee5ae Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 8 Mar 2022 11:57:13 +0800 Subject: [PATCH 1/5] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- interfaces/plugin/include/sensor_napi_utils.h | 2 + interfaces/plugin/src/sensor_js.cpp | 144 ++++++++++++------ interfaces/plugin/src/sensor_napi_utils.cpp | 9 +- .../adapter/src/compatible_connection.cpp | 6 +- .../adapter/src/sensor_event_callback.cpp | 20 ++- .../interface/src/sensor_hdi_connection.cpp | 2 +- services/sensor/src/sensor_data_processer.cpp | 3 +- utils/include/sensor_basic_data_channel.h | 12 +- utils/src/permission_util.cpp | 10 +- utils/src/report_data_callback.cpp | 7 +- 10 files changed, 143 insertions(+), 72 deletions(-) mode change 100755 => 100644 utils/include/sensor_basic_data_channel.h diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index ecc8ad7f..80612bf1 100644 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -100,6 +100,8 @@ struct AsyncCallbackInfo { using ConvertDataFunc = void(*)(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]); +bool IsNapiValueSame(napi_env env, napi_value lhs, napi_value rhs); + bool IsMatchType(napi_env env, napi_value value, napi_valuetype type); napi_value GetNapiInt32(int32_t number, napi_env env); diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c5c54884..8745db3f 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -40,7 +40,7 @@ using namespace OHOS::HiviewDFX; static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002708, "SensorJsAPI"}; static std::map g_onceCallbackInfos; -static std::map g_onCallbackInfos; +static std::map> g_onCallbackInfos; static void DataCallbackImpl(SensorEvent *event) { @@ -52,17 +52,19 @@ static void DataCallbackImpl(SensorEvent *event) int32_t sensorTypeId = event->sensorTypeId; float *data = (float *)(event->data); if (g_onCallbackInfos.find(sensorTypeId) != g_onCallbackInfos.end()) { - struct AsyncCallbackInfo *onCallbackInfo = g_onCallbackInfos[sensorTypeId]; - onCallbackInfo->data.sensorData.sensorTypeId = sensorTypeId; - onCallbackInfo->data.sensorData.dataLength = event->dataLen; - onCallbackInfo->data.sensorData.timestamp = event->timestamp; - errno_t ret = memcpy_s(onCallbackInfo->data.sensorData.data, event->dataLen, data, event->dataLen); - if (ret != EOK) { - HiLog::Error(LABEL, "%{public}s copy data failed", __func__); - return; + for (uint32_t i = 0; i < g_onCallbackInfos[sensorTypeId].size(); ++i) { + g_onCallbackInfos[sensorTypeId][i]->data.sensorData.sensorTypeId = sensorTypeId; + g_onCallbackInfos[sensorTypeId][i]->data.sensorData.dataLength = event->dataLen; + g_onCallbackInfos[sensorTypeId][i]->data.sensorData.timestamp = event->timestamp; + errno_t ret = memcpy_s(g_onCallbackInfos[sensorTypeId][i]->data.sensorData.data, + event->dataLen, data, event->dataLen); + if (ret != EOK) { + HiLog::Error(LABEL, "%{public}s copy data failed", __func__); + return; + } + g_onCallbackInfos[sensorTypeId][i]->type = ON_CALLBACK; + EmitUvEventLoop(&g_onCallbackInfos[sensorTypeId][i]); } - onCallbackInfo->type = ON_CALLBACK; - EmitUvEventLoop(&g_onCallbackInfos[sensorTypeId]); } if (g_onceCallbackInfos.find(sensorTypeId) == g_onceCallbackInfos.end()) { @@ -170,9 +172,42 @@ static napi_value Once(napi_env env, napi_callback_info info) return nullptr; } +bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback) +{ + if (g_onCallbackInfos.find(sensorTypeId) == g_onCallbackInfos.end()) { + return false; + } + std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto callbackInfo : callbackInfos) { + napi_value sensorCallback = nullptr; + napi_get_reference_value(env, callbackInfo->callback[0], &sensorCallback); + if (IsNapiValueSame(env, callback, sensorCallback)) { + return true; + } + } + return false; +} + +void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value callback) +{ + if (IsSubscribed(env, sensorTypeId, callback)) { + HiLog::Debug(LABEL, "%{public}s the callback has been subscribed", __func__); + return; + } + AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; + napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]); + std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; + callbackInfos.push_back(asyncCallbackInfo); + g_onCallbackInfos[sensorTypeId] = callbackInfos; +} + static napi_value On(napi_env env, napi_callback_info info) { - HiLog::Info(LABEL, "%{public}s in", __func__); + HiLog::Debug(LABEL, "%{public}s in", __func__); size_t argc = 3; napi_value args[3] = { 0 }; napi_value thisVar = nullptr; @@ -188,33 +223,58 @@ static napi_value On(napi_env env, napi_callback_info info) int32_t sensorTypeId = GetCppInt32(args[0], env); int64_t interval = 200000000; if (argc == 3) { - HiLog::Info(LABEL, "%{public}s argc = 3!", __func__); napi_value value = NapiGetNamedProperty(args[2], "interval", env); if (!IsMatchType(env, value, napi_number)) { HiLog::Error(LABEL, "%{public}s argument should be napi_number type!", __func__); return nullptr; } interval = GetCppInt64(value, env); + HiLog::Debug(LABEL, "%{public}s interval is %{public}lld", __func__, interval); } - AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - }; - napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); - g_onCallbackInfos[sensorTypeId] = asyncCallbackInfo; int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); if (ret < 0) { - HiLog::Error(LABEL, "%{public}s subscribeSensor failed", __func__); - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; - EmitAsyncCallbackWork(asyncCallbackInfo); - g_onCallbackInfos.erase(sensorTypeId); + HiLog::Error(LABEL, "%{public}s subscribeSensor failed", __func__); + return nullptr; } - HiLog::Info(LABEL, "%{public}s out", __func__); + UpdateCallbackInfos(env, sensorTypeId, args[1]); + HiLog::Debug(LABEL, "%{public}s end", __func__); return nullptr; } +void RemoveAllCallback(napi_env env, int32_t sensorTypeId) +{ + std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto callbackInfo : callbackInfos) { + napi_delete_reference(env, callbackInfo->callback[0]); + callbackInfo->callback[0] = nullptr; + delete callbackInfo; + callbackInfo = nullptr; + } + g_onCallbackInfos.erase(sensorTypeId); +} + +void RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) +{ + std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; + std::vector::iterator iter; + for (iter = callbackInfos.begin(); iter != callbackInfos.end(); iter++) { + napi_value sensorCallback = nullptr; + napi_get_reference_value(env, (*iter)->callback[0], &sensorCallback); + if (IsNapiValueSame(env, callback, sensorCallback)) { + napi_delete_reference(env, (*iter)->callback[0]); + (*iter)->callback[0] = nullptr; + delete *iter; + *iter = nullptr; + callbackInfos.erase(iter); + if (callbackInfos.empty()) { + g_onCallbackInfos.erase(sensorTypeId); + return; + } + g_onCallbackInfos[sensorTypeId] = callbackInfos; + } + } +} + static napi_value Off(napi_env env, napi_callback_info info) { HiLog::Info(LABEL, "%{public}s in", __func__); @@ -222,38 +282,26 @@ static napi_value Off(napi_env env, napi_callback_info info) napi_value args[2] = { 0 }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, NULL)); - - if (argc < 1) { + if (argc < 1 || argc > 2 || !IsMatchType(env, args[0], napi_number)) { HiLog::Error(LABEL, "%{public}s Invalid input.", __func__); return nullptr; } - if (!IsMatchType(env, args[0], napi_number) || !IsMatchType(env, args[1], napi_function)) { - HiLog::Error(LABEL, "%{public}s argument is invalid", __func__); + int32_t sensorTypeId = GetCppInt32(args[0], env); + if (g_onCallbackInfos.find(sensorTypeId) == g_onCallbackInfos.end() + || (argc == 2 && !IsSubscribed(env, sensorTypeId, args[1]))) { + HiLog::Error(LABEL, "%{public}s should subscribe first", __func__); return nullptr; } - int32_t sensorTypeId = GetCppInt32(args[0], env); - AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - }; - napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); int32_t ret = UnsubscribeSensor(sensorTypeId); if (ret < 0) { - asyncCallbackInfo->type = FAIL; - asyncCallbackInfo->error.code = ret; HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); + return nullptr; + } + if (argc == 1) { + RemoveAllCallback(env, sensorTypeId); } else { - HiLog::Error(LABEL, "%{public}s UnsubscribeSensor success", __func__); - asyncCallbackInfo->type = OFF_CALLBACK; - if (g_onCallbackInfos.find(sensorTypeId) != g_onCallbackInfos.end()) { - napi_delete_reference(env, g_onCallbackInfos[sensorTypeId]->callback[0]); - delete g_onCallbackInfos[sensorTypeId]; - g_onCallbackInfos[sensorTypeId] = nullptr; - g_onCallbackInfos.erase(sensorTypeId); - } + RemoveCallback(env, sensorTypeId, args[1]); } - EmitAsyncCallbackWork(asyncCallbackInfo); return nullptr; } diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 047a15cc..c942707d 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -25,6 +25,13 @@ using namespace OHOS::HiviewDFX; static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002708, "SensorJsAPI"}; +bool IsNapiValueSame(napi_env env, napi_value lhs, napi_value rhs) +{ + bool result = false; + napi_strict_equals(env, lhs, rhs, &result); + return result; +} + bool IsMatchType(napi_env env, napi_value value, napi_valuetype type) { napi_valuetype paramType = napi_undefined; @@ -443,7 +450,7 @@ void EmitUvEventLoop(AsyncCallbackInfo **asyncCallbackInfo) return; } g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); - napi_call_function(env, undefined, callback, 2, result, &callResult); + napi_call_function(env, undefined, callback, 1, &result[1], &callResult); if (asyncCallbackInfo->type != ON_CALLBACK) { napi_delete_reference(env, asyncCallbackInfo->callback[0]); delete asyncCallbackInfo; diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index add28f3a..6a8ca61a 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -123,13 +123,11 @@ int32_t CompatibleConnection::SetOption(int32_t sensorId, int32_t option) int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *event) { - HiLog::Debug(LABEL, "%{public}s begin", __func__); if ((event == nullptr) || (event->dataLen == 0)) { HiLog::Error(LABEL, "%{public}s event is NULL", __func__); return ERR_INVALID_VALUE; } - - if (reportDataCb_ == nullptr) { + if (reportDataCb_ == nullptr || reportDataCallback_) { HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); return ERR_NO_INIT; } @@ -146,9 +144,9 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even errno_t ret = memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen); if (ret != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); + delete[] sensorEvent.data; return COPY_ERR; } - (void)(reportDataCallback_->*reportDataCb_)(&sensorEvent, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; diff --git a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index ce7036e7..0c3d7c88 100644 --- a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -27,7 +27,17 @@ std::unique_ptr HdiConnection_ = std::make_unique( } int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) { - HiLog::Debug(LABEL, "%{public}s begin", __func__); + ZReportDataCb reportDataCb_ = HdiConnection_->getReportDataCb(); + sptr reportDataCallback_ = HdiConnection_->getReportDataCallback(); + if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ or reportDataCallback_ cannot be null", __func__); + return ERR_NO_INIT; + } + int32_t dataSize = static_cast(event.data.size()); + if (dataSize == 0) { + HiLog::Error(LABEL, "%{public}s data is empty", __func__); + return ERR_INVALID_VALUE; + } struct SensorEvent sensorEvent = { .sensorTypeId = event.sensorId, .version = event.version, @@ -37,15 +47,9 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) .dataLen = event.dataLen }; sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; - for (int32_t i = 0; i < static_cast(event.data.size()); i++) { + for (int32_t i = 0; i < static_cast(dataSize); i++) { sensorEvent.data[i] = event.data[i]; } - ZReportDataCb reportDataCb_ = HdiConnection_->getReportDataCb(); - sptr reportDataCallback_ = HdiConnection_->getReportDataCallback(); - if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { - HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); - return ERR_NO_INIT; - } (void)(reportDataCallback_->*(reportDataCb_))(&sensorEvent, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 2384713d..78e63162 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -138,7 +138,7 @@ int32_t SensorHdiConnection::DestroyHdiConnection() { int32_t ret = iSensorHdiConnection_->DestroyHdiConnection(); if (ret != 0) { - HiLog::Info(LABEL, "%{public}s destroy hdi connectionr failed", __func__); + HiLog::Info(LABEL, "%{public}s destroy hdi connection failed", __func__); return DEVICE_ERR; } return ret; diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index e60040d7..044b7364 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -373,8 +373,7 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback int32_t eventNum = eventsBuf.eventNum; for (int32_t i = 0; i < eventNum; i++) { EventFilter(eventsBuf); - delete eventsBuf.circularBuf[eventsBuf.readPosition].data; - eventsBuf.circularBuf[eventsBuf.readPosition].data = nullptr; + delete[] eventsBuf.circularBuf[eventsBuf.readPosition].data; eventsBuf.readPosition++; if (eventsBuf.readPosition == CIRCULAR_BUF_LEN) { eventsBuf.readPosition = 0; diff --git a/utils/include/sensor_basic_data_channel.h b/utils/include/sensor_basic_data_channel.h old mode 100755 new mode 100644 index 2507a8fb..47c9f99b --- a/utils/include/sensor_basic_data_channel.h +++ b/utils/include/sensor_basic_data_channel.h @@ -28,12 +28,12 @@ namespace Sensors { constexpr int32_t INVALID_FD = -1; constexpr int32_t SENSOR_MAX_LENGTH = 64; struct TransferSensorEvents { - uint32_t sensorTypeId; /**< Sensor type ID */ - int32_t version; /**< Sensor algorithm version */ - int64_t timestamp; /**< Time when sensor data was reported */ - int32_t option; /**< Sensor data options, including the measurement range and accuracy */ - int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ - uint32_t dataLen; /**< Sensor data length */ + uint32_t sensorTypeId; + int32_t version; + int64_t timestamp; + int32_t option; + int32_t mode; + uint32_t dataLen; uint8_t data[SENSOR_MAX_LENGTH]; }; class SensorBasicDataChannel : public RefBase { diff --git a/utils/src/permission_util.cpp b/utils/src/permission_util.cpp index 3bdf81c6..c547ba46 100644 --- a/utils/src/permission_util.cpp +++ b/utils/src/permission_util.cpp @@ -48,8 +48,16 @@ bool PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t se if (sensorPermissions_.find(sensorTypeId) == sensorPermissions_.end()) { return true; } + int32_t result = -1; std::string permissionName = sensorPermissions_[sensorTypeId]; - int32_t result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); + if (AccessTokenKit::GetTokenTypeFlag(callerToken) == TOKEN_NATIVE) { + result = AccessTokenKit::VerifyNativeToken(callerToken, permissionName); + } else if (AccessTokenKit::GetTokenTypeFlag(callerToken) == TOKEN_HAP) { + result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); + } else { + HiLog::Error(LABEL, "%{public}s tokenId invalid", __func__); + return false; + } if (result != PERMISSION_GRANTED) { HiLog::Error(LABEL, "%{public}s sensorId: %{public}d grant failed, result: %{public}d", __func__, sensorTypeId, result); diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 513de8e9..719e76cc 100644 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -53,8 +53,13 @@ ReportDataCallback::~ReportDataCallback() int32_t ReportDataCallback::ZReportDataCallback(const struct SensorEvent* event, sptr cb) { - if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr || event == nullptr) { + if (event == nullptr) { + HiLog::Error(LABEL, "%{public}s sensor data is null", __func__); + return ERROR; + } + if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { HiLog::Error(LABEL, "%{public}s callback or circularBuf or event cannot be null", __func__); + delete[] event->data; return ERROR; } int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum; -- Gitee From 786542f66c45e6b2837616bfe8cd91504d078a32 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 8 Mar 2022 12:07:34 +0800 Subject: [PATCH 2/5] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- interfaces/plugin/src/sensor_js.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 8745db3f..29d8c062 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -172,7 +172,7 @@ static napi_value Once(napi_env env, napi_callback_info info) return nullptr; } -bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback) +static bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback) { if (g_onCallbackInfos.find(sensorTypeId) == g_onCallbackInfos.end()) { return false; @@ -188,7 +188,7 @@ bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback) return false; } -void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value callback) +static void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value callback) { if (IsSubscribed(env, sensorTypeId, callback)) { HiLog::Debug(LABEL, "%{public}s the callback has been subscribed", __func__); @@ -241,7 +241,7 @@ static napi_value On(napi_env env, napi_callback_info info) return nullptr; } -void RemoveAllCallback(napi_env env, int32_t sensorTypeId) +static void RemoveAllCallback(napi_env env, int32_t sensorTypeId) { std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto callbackInfo : callbackInfos) { @@ -253,7 +253,7 @@ void RemoveAllCallback(napi_env env, int32_t sensorTypeId) g_onCallbackInfos.erase(sensorTypeId); } -void RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) +static void RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) { std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; std::vector::iterator iter; -- Gitee From 003858ab082632f5dc2e571a3ac586f5db257ac9 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 10 Mar 2022 11:38:13 +0800 Subject: [PATCH 3/5] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- interfaces/plugin/src/sensor_js.cpp | 8 +++++--- .../sensor/hdi_connection/adapter/src/hdi_connection.cpp | 1 + utils/src/permission_util.cpp | 9 +-------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 29d8c062..61b90105 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -198,6 +198,7 @@ static void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value c .env = env, .asyncWork = nullptr, .deferred = nullptr, + .type = ON_CALLBACK, }; napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]); std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; @@ -253,7 +254,7 @@ static void RemoveAllCallback(napi_env env, int32_t sensorTypeId) g_onCallbackInfos.erase(sensorTypeId); } -static void RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) +static uint32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) { std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; std::vector::iterator iter; @@ -268,11 +269,12 @@ static void RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callba callbackInfos.erase(iter); if (callbackInfos.empty()) { g_onCallbackInfos.erase(sensorTypeId); - return; + return 0; } - g_onCallbackInfos[sensorTypeId] = callbackInfos; } } + g_onCallbackInfos[sensorTypeId] = callbackInfos; + return callbackInfos.size(); } static napi_value Off(napi_env env, napi_callback_info info) diff --git a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index 4315873b..bc9c43cc 100644 --- a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp @@ -248,6 +248,7 @@ void HdiConnection::setSensorBasicInfoState(int32_t sensorId, SensorState state) auto it = sensorBasicInfoMap_.find(sensorId); if (it == sensorBasicInfoMap_.end()) { HiLog::Warn(LABEL, "%{public}s should set batch first", __func__); + return; } sensorBasicInfoMap_[sensorId].SetSensorState(state); } diff --git a/utils/src/permission_util.cpp b/utils/src/permission_util.cpp index c547ba46..bc0141c7 100644 --- a/utils/src/permission_util.cpp +++ b/utils/src/permission_util.cpp @@ -50,14 +50,7 @@ bool PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t se } int32_t result = -1; std::string permissionName = sensorPermissions_[sensorTypeId]; - if (AccessTokenKit::GetTokenTypeFlag(callerToken) == TOKEN_NATIVE) { - result = AccessTokenKit::VerifyNativeToken(callerToken, permissionName); - } else if (AccessTokenKit::GetTokenTypeFlag(callerToken) == TOKEN_HAP) { - result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); - } else { - HiLog::Error(LABEL, "%{public}s tokenId invalid", __func__); - return false; - } + int32_t result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); if (result != PERMISSION_GRANTED) { HiLog::Error(LABEL, "%{public}s sensorId: %{public}d grant failed, result: %{public}d", __func__, sensorTypeId, result); -- Gitee From f80b3edd7412a984f2e2e9258dc41ad12a4a4a93 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 10 Mar 2022 11:52:30 +0800 Subject: [PATCH 4/5] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- interfaces/plugin/src/sensor_js.cpp | 11 ++++++----- .../adapter/src/compatible_connection.cpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 61b90105..e675dc0e 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -294,15 +294,16 @@ static napi_value Off(napi_env env, napi_callback_info info) HiLog::Error(LABEL, "%{public}s should subscribe first", __func__); return nullptr; } - int32_t ret = UnsubscribeSensor(sensorTypeId); - if (ret < 0) { - HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); + if (argc == 2 && RemoveCallback(env, sensorTypeId, args[1]) != 0) { + HiLog::Debug(LABEL, "%{public}s there are other client registrations as well", __func__); return nullptr; } if (argc == 1) { RemoveAllCallback(env, sensorTypeId); - } else { - RemoveCallback(env, sensorTypeId, args[1]); + } + int32_t ret = UnsubscribeSensor(sensorTypeId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); } return nullptr; } diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 6a8ca61a..a3d95b9d 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -127,7 +127,7 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even HiLog::Error(LABEL, "%{public}s event is NULL", __func__); return ERR_INVALID_VALUE; } - if (reportDataCb_ == nullptr || reportDataCallback_) { + if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); return ERR_NO_INIT; } -- Gitee From a783f019c0d4d9f023976f8dd319c2ad282a762f Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 10 Mar 2022 12:02:15 +0800 Subject: [PATCH 5/5] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- utils/src/permission_util.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/src/permission_util.cpp b/utils/src/permission_util.cpp index bc0141c7..3bdf81c6 100644 --- a/utils/src/permission_util.cpp +++ b/utils/src/permission_util.cpp @@ -48,7 +48,6 @@ bool PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t se if (sensorPermissions_.find(sensorTypeId) == sensorPermissions_.end()) { return true; } - int32_t result = -1; std::string permissionName = sensorPermissions_[sensorTypeId]; int32_t result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); if (result != PERMISSION_GRANTED) { -- Gitee