From fa89f091cc85618201e648e0787895fb185bd902 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 26 Sep 2023 02:15:28 +0000 Subject: [PATCH] SensorAccuracy Signed-off-by: hellohyh001 Change-Id: Ie5f98f068df01a8534efa7e915df5435ba0682cc --- interfaces/native/include/sensor_agent_type.h | 18 +++++- .../test/unittest/sensor_agent_test.cpp | 12 ++-- .../plugin/include/async_callback_info.h | 1 + interfaces/plugin/src/sensor_js.cpp | 18 ++++++ interfaces/plugin/src/sensor_napi_utils.cpp | 4 ++ .../unittest/sensor/ExampleJsunit.test.js | 63 ++++++++++++++++--- utils/common/include/sensor_data_event.h | 2 +- 7 files changed, 105 insertions(+), 13 deletions(-) diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 6ec13fc5..d6c5629a 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -118,6 +118,22 @@ typedef struct SensorInfo { int64_t maxSamplePeriod; /**< Maximum sample period allowed, in ns */ } SensorInfo; +/** + * @brief Enumerates the accuracy levels of data reported by a sensor. + * + * @since 11 + */ +typedef enum SensorAccuracy { + /**< The sensor data is unreliable. It is possible that the sensor does not contact with the device to measure. */ + ACCURACY_UNRELIABLE = 0, + /**< The sensor data is at a low accuracy level. You are required to calibrate the data based on the environment before using it. */ + ACCURACY_LOW = 1, + /**< The sensor data is at a medium accuracy level. You are advised to calibrate the data based on the environment before using it. */ + ACCURACY_MEDIUM = 2, + /**< The sensor data is at a high accuracy level. The data can be used directly. */ + ACCURACY_HIGH = 3, +} SensorAccuracy; + /** * @brief Defines the data reported by the sensor. * @@ -127,7 +143,7 @@ typedef struct SensorEvent { int32_t sensorTypeId; /**< Sensor type ID */ int32_t version; /**< Sensor algorithm version */ int64_t timestamp; /**< Time when sensor data was reported */ - uint32_t option; /**< Sensor data options, including the measurement range and accuracy */ + int32_t option; /**< Sensor data options, including the measurement range and accuracy */ int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ uint8_t *data = nullptr; /**< Sensor data */ uint32_t dataLen; /**< Sensor data length */ diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 42bb53f8..93ed023c 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -121,8 +121,10 @@ void SensorDataCallbackImpl(SensorEvent *event) return; } AccelData *accelData = reinterpret_cast(event[0].data); - SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}d, x:%{public}f, y:%{public}f, z:%{public}f", - event[0].sensorTypeId, event[0].version, event[0].dataLen, accelData->x, accelData->y, accelData->z); + SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}d," + "x:%{public}f, y:%{public}f,z:%{public}f, option:%{public}d", + event[0].sensorTypeId, event[0].version, event[0].dataLen, + accelData->x, accelData->y, accelData->z, event[0].option); } void SensorDataCallbackImpl2(SensorEvent *event) @@ -140,8 +142,10 @@ void SensorDataCallbackImpl2(SensorEvent *event) return; } AccelData *accelData = reinterpret_cast(event[0].data); - SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}d, x:%{public}f, y:%{public}f, z:%{public}f", - event[0].sensorTypeId, event[0].version, event[0].dataLen, accelData->x, accelData->y, accelData->z); + SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}d," + "x:%{public}f, y:%{public}f,z:%{public}f, option:%{public}d", + event[0].sensorTypeId, event[0].version, event[0].dataLen, + accelData->x, accelData->y, accelData->z, event[0].option); } HWTEST_F(SensorAgentTest, GetAllSensorsTest_001, TestSize.Level1) diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 6c25ebf0..cdddb94b 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -75,6 +75,7 @@ struct SensorData { uint32_t dataLength; float data[DATA_LENGTH]; int64_t timestamp; + int32_t sensorAccuracy; }; struct ReserveData { diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 0bb66359..0b3b279b 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -71,6 +71,7 @@ static bool copySensorData(sptr callbackInfo, SensorEvent *ev callbackInfo->data.sensorData.sensorTypeId = sensorTypeId; callbackInfo->data.sensorData.dataLength = event->dataLen; callbackInfo->data.sensorData.timestamp = event->timestamp; + callbackInfo->data.sensorData.sensorAccuracy = event->option; CHKPF(event->data); if (event->dataLen < sizeof(float)) { SEN_HILOGE("Event dataLen less than float size."); @@ -1214,6 +1215,22 @@ static napi_value CreateEnumSensorId(napi_env env, napi_value exports) return exports; } +static napi_value CreateEnumSensorAccuracy(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("ACCURACY_UNRELIABLE", GetNapiInt32(env, ACCURACY_UNRELIABLE)), + DECLARE_NAPI_STATIC_PROPERTY("ACCURACY_LOW", GetNapiInt32(env, ACCURACY_LOW)), + DECLARE_NAPI_STATIC_PROPERTY("ACCURACY_MEDIUM", GetNapiInt32(env, ACCURACY_MEDIUM)), + DECLARE_NAPI_STATIC_PROPERTY("ACCURACY_HIGH", GetNapiInt32(env, ACCURACY_HIGH)), + }; + napi_value result = nullptr; + CHKNRP(env, napi_define_class(env, "SensorAccuracy", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &result), "napi_define_class"); + CHKNRP(env, napi_set_named_property(env, exports, "SensorAccuracy", result), "napi_set_named_property fail"); + return exports; + +} + static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { @@ -1270,6 +1287,7 @@ static napi_value Init(napi_env env, napi_value exports) "napi_define_properties"); CHKCP(CreateEnumSensorType(env, exports), "Create enum sensor type fail"); CHKCP(CreateEnumSensorId(env, exports), "Create enum sensor id fail"); + CHKCP(CreateEnumSensorAccuracy(env, exports), "Create enum sensor accuracy fail"); return exports; } diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 2a09d45d..8a9ade39 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -337,6 +337,10 @@ bool ConvertToSensorData(const napi_env &env, sptr asyncCallb CHKNRF(env, napi_create_int64(env, asyncCallbackInfo->data.sensorData.timestamp, &message), "napi_create_int64"); CHKNRF(env, napi_set_named_property(env, result[1], "timestamp", message), "napi_set_named_property"); + message = nullptr; + CHKNRF(env, napi_create_int32(env, asyncCallbackInfo->data.sensorData.sensorAccuracy, &message), + "napi_create_int32"); + CHKNRF(env, napi_set_named_property(env, result[1], "accuracy", message), "napi_set_named_property"); return true; } diff --git a/interfaces/plugin/test/unittest/sensor/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/sensor/ExampleJsunit.test.js index ec5840b6..55f2de22 100755 --- a/interfaces/plugin/test/unittest/sensor/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/sensor/ExampleJsunit.test.js @@ -18,12 +18,26 @@ import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from ' describe("SensorJsTest", function () { function callback(data) { - console.info("callback" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('SensorJsTest callback accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest callback invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } expect(typeof(data.x)).assertEqual("number"); } function callback2() { - console.info("callback2" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('SensorJsTest callback2 accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('SensorJsTest callback2 invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } expect(typeof(data.x)).assertEqual("number"); } @@ -4533,7 +4547,14 @@ describe("SensorJsTest", function () { expect(typeof(data.x)).assertEqual("number"); expect(typeof(data.y)).assertEqual("number"); expect(typeof(data.z)).assertEqual("number"); - console.info("Sensor_SubscribeAccelerometer_001 success" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('Sensor_SubscribeAccelerometer_001 accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('Sensor_SubscribeAccelerometer_001 invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } }, fail: function(data, code) { expect(false).assertTrue(); @@ -4639,7 +4660,14 @@ describe("SensorJsTest", function () { expect(typeof(data.x)).assertEqual("number"); expect(typeof(data.y)).assertEqual("number"); expect(typeof(data.z)).assertEqual("number"); - console.info("Sensor_SubscribeAccelerometer_005 success" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('Sensor_SubscribeAccelerometer_005 accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('Sensor_SubscribeAccelerometer_005 invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } }, }, "abc"); setTimeout(() => { @@ -4670,7 +4698,14 @@ describe("SensorJsTest", function () { expect(typeof(data.x)).assertEqual("number"); expect(typeof(data.y)).assertEqual("number"); expect(typeof(data.z)).assertEqual("number"); - console.info("Sensor_SubscribeAccelerometer_006 success" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('Sensor_SubscribeAccelerometer_006 accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('Sensor_SubscribeAccelerometer_006 invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } }, fail:undefined, }); @@ -4702,7 +4737,14 @@ describe("SensorJsTest", function () { expect(typeof(data.x)).assertEqual("number"); expect(typeof(data.y)).assertEqual("number"); expect(typeof(data.z)).assertEqual("number"); - console.info("Sensor_SubscribeAccelerometer_007 success" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('Sensor_SubscribeAccelerometer_007 accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('Sensor_SubscribeAccelerometer_007 invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } }, fail:null, }); @@ -4734,7 +4776,14 @@ describe("SensorJsTest", function () { expect(typeof(data.x)).assertEqual("number"); expect(typeof(data.y)).assertEqual("number"); expect(typeof(data.z)).assertEqual("number"); - console.info("Sensor_SubscribeAccelerometer_008 success" + JSON.stringify(data)); + if (data.accuracy >= sensor.SensorAccuracy.ACCURACY_UNRELIABLE && data.accuracy <= + sensor.SensorAccuracy.ACCURACY_HIGH) { + console.info('Sensor_SubscribeAccelerometer_008 accuracy verified' + JSON.stringify(data)); + expect(true).assertTrue(); + } else { + console.info('Sensor_SubscribeAccelerometer_008 invalid accuracy encountered' + JSON.stringify(data)); + expect(false).assertTrue(); + } }, fail:"abc", }); diff --git a/utils/common/include/sensor_data_event.h b/utils/common/include/sensor_data_event.h index cf6751ff..51bcbaca 100755 --- a/utils/common/include/sensor_data_event.h +++ b/utils/common/include/sensor_data_event.h @@ -34,7 +34,7 @@ struct SensorData { int32_t sensorTypeId; /**< Sensor type ID */ int32_t version; /**< Sensor algorithm version */ int64_t timestamp; /**< Time when sensor data was reported */ - uint32_t option; /**< Sensor data options, including the measurement range and accuracy */ + int32_t option; /**< Sensor data options, including the measurement range and accuracy */ int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ uint8_t data[SENSOR_MAX_LENGTH]; /**< Sensor data */ uint32_t dataLen; /**< Sensor data length */ -- Gitee