From d51c4c4b7a79c5206c88abf3d8c4854660e76dca Mon Sep 17 00:00:00 2001 From: h00514358 Date: Sat, 13 Nov 2021 06:57:27 +0000 Subject: [PATCH 1/3] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/src/sensor_js.cpp | 5 +- interfaces/plugin/src/sensor_napi_utils.cpp | 102 ++++++++++++++------ 2 files changed, 77 insertions(+), 30 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 8e52bd8d..c60fc7b2 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -56,7 +56,7 @@ static void DataCallbackImpl(SensorEvent *event) return; } onCallbackInfo.second->status = 1; - EmitAsyncCallbackWork((struct AsyncCallbackInfo *)(onCallbackInfo.second)); + EmitUvEventLoop((struct AsyncCallbackInfo *)(onCallbackInfo.second)); } } if (g_onceCallbackInfos.find(sensorTypeId) == g_onceCallbackInfos.end()) { @@ -71,7 +71,7 @@ static void DataCallbackImpl(SensorEvent *event) return; } onceCallbackInfo->status = 2; - EmitAsyncCallbackWork((struct AsyncCallbackInfo *)(onceCallbackInfo)); + EmitUvEventLoop((struct AsyncCallbackInfo *)(onceCallbackInfo)); if (g_onCallbackInfos.find(sensorTypeId) == g_onCallbackInfos.end()) { HiLog::Debug(LABEL, "%{public}s no subscription to change sensor data, need to cancel registration", __func__); UnsubscribeSensor(sensorTypeId); @@ -250,7 +250,6 @@ static napi_value Off(napi_env env, napi_callback_info info) asyncCallbackInfo->status = 0; if (g_onCallbackInfos.find(sensorTypeId) != g_onCallbackInfos.end()) { napi_delete_reference(env, g_onCallbackInfos[sensorTypeId]->callback[0]); - napi_delete_async_work(env, g_onCallbackInfos[sensorTypeId]->asyncWork); delete g_onCallbackInfos[sensorTypeId]; g_onCallbackInfos[sensorTypeId] = nullptr; g_onCallbackInfos.erase(sensorTypeId); diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index c5f611d8..8d7415c7 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -133,39 +133,87 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) napi_create_error(env, code, message, &result[0]); napi_get_undefined(env, &result[1]); } else if (asyncCallbackInfo->status == 0) { - napi_create_object(env, &result[1]); - napi_value message = nullptr; - napi_create_int32(env, asyncCallbackInfo->status, &message); - napi_set_named_property(env, result[1], "code", message); - napi_get_undefined(env, &result[0]); - } else { - int32_t sensorTypeId = asyncCallbackInfo->sensorTypeId; - if (g_sensorAttributeList.count(sensorTypeId) == 0) { - HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); - return; - } - std::vector sensorAttribute = g_sensorAttributeList[sensorTypeId]; - napi_create_object(env, &result[1]); - for (size_t i = 0; i < sensorAttribute.size(); i++) { - napi_value message = nullptr; - double a = asyncCallbackInfo->sensorData[i]; - HiLog::Info(LABEL, "%{public}s data id %{public}f", __func__, a); - napi_create_double(env, a, &message); - napi_set_named_property(env, result[1], sensorAttribute[i].c_str(), message); - } + napi_get_undefined(env, &result[1]); napi_get_undefined(env, &result[0]); } napi_call_function(env, nullptr, callback, 2, result, &callResult); - if (asyncCallbackInfo->status != 1) { - HiLog::Debug(LABEL, "%{public}s not a continuous callback, need to release asyncCallbackInfo", __func__); - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - } + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; HiLog::Debug(LABEL, "%{public}s napi_create_async_work left", __func__); }, asyncCallbackInfo, &asyncCallbackInfo->asyncWork); napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork); HiLog::Debug(LABEL, "%{public}s end", __func__); } + + +void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) { + uv_loop_s *loop(nullptr); + HiLog::Error(LABEL, "%{public}s env: %{public}p", __func__, asyncCallbackInfo->env); + napi_get_uv_event_loop(asyncCallbackInfo->env, &loop); + if (loop == nullptr) { + HiLog::Error(LABEL, "%{public}s loop is null", __func__); + return; + } + + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + HiLog::Error(LABEL, "%{public}s work is null", __func__); + return; + } + + work->data = reinterpret_cast(asyncCallbackInfo); + uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { + AsyncCallbackInfo *asyncCallbackInfo = reinterpret_cast(work->data); + if (asyncCallbackInfo == nullptr) { + HiLog::Error(LABEL, "%{public}s asyncCallbackInfo is null", __func__); + return; + } + napi_env env = asyncCallbackInfo->env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (asyncCallbackInfo->callback[0] == nullptr) { + HiLog::Error(LABEL, "%{public}s callback is null", __func__); + return; + } + napi_value callback; + napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + napi_value callResult = nullptr; + napi_value result[2] = {0}; + if (asyncCallbackInfo->status < 0) { + HiLog::Debug(LABEL, "%{public}s status < 0 in", __func__); + napi_value code = nullptr; + napi_value message = nullptr; + napi_create_string_utf8(env, "-1", NAPI_AUTO_LENGTH, &code); + napi_create_string_utf8(env, "failed", NAPI_AUTO_LENGTH, &message); + napi_create_error(env, code, message, &result[0]); + napi_get_undefined(env, &result[1]); + } else { + int32_t sensorTypeId = asyncCallbackInfo->sensorTypeId; + if (g_sensorAttributeList.count(sensorTypeId) == 0) { + HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); + return; + } + std::vector sensorAttribute = g_sensorAttributeList[sensorTypeId]; + napi_create_object(env, &result[1]); + for (size_t i = 0; i < sensorAttribute.size(); i++) { + napi_value message = nullptr; + double a = asyncCallbackInfo->sensorData[i]; + HiLog::Info(LABEL, "%{public}s data id %{public}f", __func__, a); + napi_create_double(env, a, &message); + napi_set_named_property(env, result[1], sensorAttribute[i].c_str(), message); + } + napi_get_undefined(env, &result[0]); + } + napi_call_function(env, undefined, callback, 2, result, &callResult); + if (asyncCallbackInfo->status != 1) { + napi_delete_reference(env, asyncCallbackInfo->callback[0]); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + delete work; + work = nullptr; + }); +} -- Gitee From 8c930021deef5bfffbeb286ea609cdb208e91c5d Mon Sep 17 00:00:00 2001 From: h00514358 Date: Sat, 13 Nov 2021 07:11:06 +0000 Subject: [PATCH 2/3] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/include/sensor_js.h | 1 - interfaces/plugin/include/sensor_napi_utils.h | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/plugin/include/sensor_js.h b/interfaces/plugin/include/sensor_js.h index 4d8720d2..a361705c 100755 --- a/interfaces/plugin/include/sensor_js.h +++ b/interfaces/plugin/include/sensor_js.h @@ -17,4 +17,3 @@ static int32_t UnsubscribeSensor(int32_t sensorTypeId); static void DataCallbackImpl(SensorEvent *event); static int32_t SubscribeSensor(int32_t sensorTypeId, int64_t interval, RecordSensorCallback callback); - diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index cc2d8728..22b06da0 100755 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -15,6 +15,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" +#include #include "sensor_agent.h" #include @@ -36,6 +37,7 @@ napi_value GetNapiInt32(int32_t number, napi_env env); int32_t GetCppInt32(napi_value value, napi_env env); bool GetCppBool(napi_value value, napi_env env); void EmitAsyncCallbackWork(AsyncCallbackInfo *async_callback_info); +void EmitUvEventLoop(AsyncCallbackInfo *async_callback_info); int64_t GetCppInt64(napi_value value, napi_env env); napi_value NapiGetNamedProperty(napi_value jsonObject, std::string name, napi_env env); napi_value GetUndefined(napi_env env); \ No newline at end of file -- Gitee From 02028153f6e2613e358180eea9c2728622e097ef Mon Sep 17 00:00:00 2001 From: h00514358 Date: Sat, 13 Nov 2021 07:39:11 +0000 Subject: [PATCH 3/3] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/src/sensor_napi_utils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 8d7415c7..fc88427b 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -148,8 +148,8 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) HiLog::Debug(LABEL, "%{public}s end", __func__); } - -void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) { +void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) +{ uv_loop_s *loop(nullptr); HiLog::Error(LABEL, "%{public}s env: %{public}p", __func__, asyncCallbackInfo->env); napi_get_uv_event_loop(asyncCallbackInfo->env, &loop); -- Gitee