From 6fd4893f4206f6179f20ec6c2ed78d38027d5745 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 14 Dec 2021 11:59:11 +0000 Subject: [PATCH 1/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/include/sensor_napi_utils.h | 12 +++++++++--- interfaces/plugin/src/sensor_js.cpp | 15 +++++++++------ interfaces/plugin/src/sensor_napi_utils.cpp | 14 ++++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index 22b06da0..b7a377d5 100755 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -21,14 +21,20 @@ #define EVENT_INVALID_PARAMETER (-1); #define EVENT_OK 0; + +struct SensorData { + int32_t sensorTypeId; + int32_t dataLength; + float data[16]; + int64_t timestamp; +}; + struct AsyncCallbackInfo { napi_env env; napi_async_work asyncWork; napi_deferred deferred; napi_ref callback[1] = { 0 }; - int32_t sensorTypeId; - int32_t sensorDataLength; - float sensorData[16]; + SensorData sensorData; int32_t status; }; diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c60fc7b2..5e057195 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -49,9 +49,11 @@ static void DataCallbackImpl(SensorEvent *event) float *data = (float *)(event->data); for (auto &onCallbackInfo: g_onCallbackInfos) { if ((int32_t)onCallbackInfo.first == sensorTypeId) { - onCallbackInfo.second->sensorTypeId = sensorTypeId; - onCallbackInfo.second->sensorDataLength = event->dataLen; - if (memcpy_s(onCallbackInfo.second->sensorData, event->dataLen, data, event->dataLen) != EOK) { + onCallbackInfo.second->sensorData.sensorTypeId = sensorTypeId; + onCallbackInfo.second->sensorData.dataLength = event->dataLen; + onCallbackInfo.second->sensorData.timestamp = event->timestamp; + HiLog::Error(LABEL, "%{public}s event is null!, time: %{public}lld", __func__, event->timestamp); + if (memcpy_s(onCallbackInfo.second->sensorData.data, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; } @@ -64,9 +66,10 @@ static void DataCallbackImpl(SensorEvent *event) return; } struct AsyncCallbackInfo *onceCallbackInfo = g_onceCallbackInfos[sensorTypeId]; - onceCallbackInfo->sensorTypeId = sensorTypeId; - onceCallbackInfo->sensorDataLength = event->dataLen; - if (memcpy_s(onceCallbackInfo->sensorData, event->dataLen, data, event->dataLen) != EOK) { + onceCallbackInfo->sensorData.sensorTypeId = sensorTypeId; + onceCallbackInfo->sensorData.dataLength = event->dataLen; + onceCallbackInfo->sensorData.timestamp = event->timestamp; + if (memcpy_s(onceCallbackInfo->sensorData.data, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; } diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index fc88427b..2be018de 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -191,20 +191,22 @@ void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) 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) { + int32_t sensorTypeId = asyncCallbackInfo->sensorData.sensorTypeId; + if (g_sensorAttributeList.count(sensorTypeId) == 0 + || g_sensorAttributeList[sensorTypeId].size() != asyncCallbackInfo->sensorData.dataLength / 4) { HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); return; } std::vector sensorAttribute = g_sensorAttributeList[sensorTypeId]; napi_create_object(env, &result[1]); + napi_value message = nullptr; 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_create_double(env, asyncCallbackInfo->sensorData.data[i], &message); napi_set_named_property(env, result[1], sensorAttribute[i].c_str(), message); + message = nullptr; } + napi_create_int64(env, asyncCallbackInfo->sensorData.timestamp, &message); + napi_set_named_property(env, result[1], "timestamp", message); napi_get_undefined(env, &result[0]); } napi_call_function(env, undefined, callback, 2, result, &callResult); -- Gitee From 9669215c6191c2e2fbeefa7869eb0f2596d89d3e Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 14 Dec 2021 12:20:49 +0000 Subject: [PATCH 2/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/src/sensor_js.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 5e057195..a3537a8b 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -52,7 +52,6 @@ static void DataCallbackImpl(SensorEvent *event) onCallbackInfo.second->sensorData.sensorTypeId = sensorTypeId; onCallbackInfo.second->sensorData.dataLength = event->dataLen; onCallbackInfo.second->sensorData.timestamp = event->timestamp; - HiLog::Error(LABEL, "%{public}s event is null!, time: %{public}lld", __func__, event->timestamp); if (memcpy_s(onCallbackInfo.second->sensorData.data, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; -- Gitee From f65af657444fc5e6f819f35bedd16e84b341cdf6 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 14 Dec 2021 12:45:11 +0000 Subject: [PATCH 3/4] 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 2be018de..ef4eb361 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -192,8 +192,8 @@ void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) napi_get_undefined(env, &result[1]); } else { int32_t sensorTypeId = asyncCallbackInfo->sensorData.sensorTypeId; - if (g_sensorAttributeList.count(sensorTypeId) == 0 - || g_sensorAttributeList[sensorTypeId].size() != asyncCallbackInfo->sensorData.dataLength / 4) { + if ((g_sensorAttributeList.count(sensorTypeId)) == 0 || (g_sensorAttributeList[sensorTypeId].size() + != (asyncCallbackInfo->sensorData.dataLength / sizeof(uint8_t)))) { HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); return; } -- Gitee From c53754d87b3c9a46500481e07f8e48cfe299e433 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 14 Dec 2021 13:04:05 +0000 Subject: [PATCH 4/4] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- interfaces/plugin/src/sensor_napi_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index ef4eb361..2d66a145 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -193,7 +193,7 @@ void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) } else { int32_t sensorTypeId = asyncCallbackInfo->sensorData.sensorTypeId; if ((g_sensorAttributeList.count(sensorTypeId)) == 0 || (g_sensorAttributeList[sensorTypeId].size() - != (asyncCallbackInfo->sensorData.dataLength / sizeof(uint8_t)))) { + != (asyncCallbackInfo->sensorData.dataLength / sizeof(float)))) { HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); return; } -- Gitee