diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 6ec13fc50c5e946c41b822bda8de0ed2c3686477..d6c5629a316f59859c04562febf21ab20035c6bd 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 42bb53f88fa8a76ff46a77d76914b2a1ed6174ad..93ed023cdeabfccfc36a47a96d85ffbb87bf41af 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 6c25ebf01c0c6b100437b442d7ee59cf24ad078a..cdddb94bf06707cc5d81ea5562d967926100a7fb 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 0bb66359f0a768be6ae7a33195a0ec76dd0a0ddc..0b3b279b4f47ab08231f09aee6610b0c8ed350f2 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 2a09d45da7a93e47099e381e338ba874526eba64..8a9ade39f009f7c4c39d41efa88b0d7cfe7b5546 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 ec5840b607b7b3f6410ff2de5d8ae0c678e83107..55f2de2277514f4d070d7e74224809998cd7a0f1 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 cf6751ff6f3c50fb07f98ead0f82db0781336eb9..51bcbacacc98e71d5b7dd346623dc6f12310cac9 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 */