From 0c97dfa26bbb2f0a95dc954109d29e85ad9dcc55 Mon Sep 17 00:00:00 2001 From: shenpeixing Date: Thu, 8 May 2025 17:21:41 +0800 Subject: [PATCH] =?UTF-8?q?sensor=E6=9C=8D=E5=8A=A1napi=E7=9A=84idl?= =?UTF-8?q?=E5=8C=96=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shenpeixing --- bundle.json | 4 + frameworks/ets/taihe/BUILD.gn | 111 ++ .../ets/taihe/author/src/ani_constructor.cpp | 31 + .../ets/taihe/author/src/ohos.sensor.impl.cpp | 1554 ++++++++++++++++ frameworks/ets/taihe/idl/ohos.sensor.taihe | 1561 +++++++++++++++++ frameworks/ets/taihe/user/main.ets | 190 ++ 6 files changed, 3451 insertions(+) create mode 100644 frameworks/ets/taihe/BUILD.gn create mode 100644 frameworks/ets/taihe/author/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/author/src/ohos.sensor.impl.cpp create mode 100644 frameworks/ets/taihe/idl/ohos.sensor.taihe create mode 100644 frameworks/ets/taihe/user/main.ets diff --git a/bundle.json b/bundle.json index be3c1b91..eb103113 100755 --- a/bundle.json +++ b/bundle.json @@ -47,6 +47,7 @@ "//base/sensors/sensor/frameworks/cj:cj_sensor_ffi", "//base/sensors/sensor/frameworks/native:sensor_target", "//base/sensors/sensor/frameworks/native:ohsensor", + "//base/sensors/sensor/frameworks/ets/taihe:sensor_taihe", "//base/sensors/sensor/utils:sensor_utils_target" ], "service_group": [ @@ -55,6 +56,9 @@ ] }, "inner_kits": [ + { + "name": "//base/sensors/sensor/frameworks/ets/taihe:copy_taihe" + }, { "name": "//base/sensors/sensor/frameworks/native:sensor_interface_native", "header": { diff --git a/frameworks/ets/taihe/BUILD.gn b/frameworks/ets/taihe/BUILD.gn new file mode 100644 index 00000000..5242833b --- /dev/null +++ b/frameworks/ets/taihe/BUILD.gn @@ -0,0 +1,111 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("./../../../sensor.gni") + +subsystem_name = "sensors" +part_name = "sensor" +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +copy_taihe_idl("copy_taihe") { + sources = [ "idl/ohos.sensor.taihe" ] + + external_deps = [] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":copy_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.sensor.abi.c", + "$taihe_generated_file_path/src/ohos.sensor.ani.cpp", + ] +} + +taihe_shared_library("sensor_taihe_native") { + branch_protector_ret = "pac_ret" + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + taihe_generated_file_path = "$taihe_generated_file_path" + subsystem_name = "$subsystem_name" + part_name = "$part_name" + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/frameworks/js/napi/include", + "$SUBSYSTEM_DIR/interfaces/inner_api", + "$SUBSYSTEM_DIR/utils/common/include", + ] + sources = get_target_outputs(":run_taihe") + sources += [ + "author/src/ani_constructor.cpp", + "author/src/ohos.sensor.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native" + ] + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + "samgr:samgr_proxy", + ] +} + +generate_static_abc("sensor_abc") { + base_url = "$taihe_generated_file_path" + files = [ + "$taihe_generated_file_path/@ohos.base.ets", + "$taihe_generated_file_path/@ohos.sensor.ets", + ] + is_boot_abc = "True" + device_dst_file = "/system/framework/sensor_abc.abc" + dependencies = [ ":run_taihe" ] +} + +generate_static_abc("sensor_test") { + base_url = "user" + files = [ "user/main.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/sensor_test.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("sensor_etc") { + source = "$target_out_dir/sensor_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ + ":sensor_abc", + # ":sensor_test" + ] +} + +group("sensor_taihe") { + deps = [ + ":sensor_etc", + ":sensor_taihe_native", + ] +} diff --git a/frameworks/ets/taihe/author/src/ani_constructor.cpp b/frameworks/ets/taihe/author/src/ani_constructor.cpp new file mode 100644 index 00000000..931fc92a --- /dev/null +++ b/frameworks/ets/taihe/author/src/ani_constructor.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ohos.sensor.ani.hpp" +#include "taihe/runtime.hpp" + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != ohos::sensor::ANIRegister(env)) { + std::cerr << "Error from ohos::sensor::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/frameworks/ets/taihe/author/src/ohos.sensor.impl.cpp b/frameworks/ets/taihe/author/src/ohos.sensor.impl.cpp new file mode 100644 index 00000000..13f7cdbf --- /dev/null +++ b/frameworks/ets/taihe/author/src/ohos.sensor.impl.cpp @@ -0,0 +1,1554 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ohos.sensor.impl.hpp" + +#include +#include +#include +#include +#include +#include + +#include "ohos.sensor.proj.hpp" +#include "refbase.h" +#include "securec.h" +#include "sensor_agent.h" +#include "sensor_agent_type.h" +#include "sensor_algorithm.h" +#include "sensor_errors.h" +#include "sensor_log.h" +#include "stdexcept" +#include "taihe/runtime.hpp" + +#undef LOG_TAG +#define LOG_TAG "OhosSensorImpl" + +using namespace taihe; +using namespace ohos::sensor; +using namespace OHOS::Sensors; +using namespace OHOS; + +using responseSensorData = std::variant; +using callbackType = std::variant, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback, taihe::callback, + taihe::callback>; + +struct CallbackObject : public RefBase { + CallbackObject(callbackType cb, ani_ref ref) : callback(cb), ref(ref) + { + } + ~CallbackObject() + { + } + callbackType callback; + ani_ref ref; +}; + +namespace { +constexpr int32_t ROTATION_VECTOR_LENGTH = 3; +constexpr int32_t REPORTING_INTERVAL = 200000000; +constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9; +constexpr int32_t CALLBACK_MAX_DATA_LENGTH = 16; + +void CallBackAccelermeter(std::map data, sptr callbackObject); +void CallBackGyroscope(std::map data, sptr callbackObject); +void CallBackAmbientLight(std::map data, sptr callbackObject); +void CallBackMagneticField(std::map data, sptr callbackObject); +void CallBackBarometer(std::map data, sptr callbackObject); +void CallBackHall(std::map data, sptr callbackObject); +void CallBackProximity(std::map data, sptr callbackObject); +void CallBackHumidity(std::map data, sptr callbackObject); +void CallBackOrientation(std::map data, sptr callbackObject); +void CallBackGravity(std::map data, sptr callbackObject); +void CallBackLinearAcceleration(std::map data, sptr callbackObject); +void CallBackRotationVector(std::map data, sptr callbackObject); +void CallBackAmbientTemperature(std::map data, sptr callbackObject); +void CallBackMagneticFieldUncalibrated(std::map data, + sptr callbackObject); +void CallBackGyroscopeUncalibrated(std::map data, sptr callbackObject); +void CallBackSignificantMotion(std::map data, sptr callbackObject); +void CallBackPedometerDetection(std::map data, sptr callbackObject); +void CallBackPedometer(std::map data, sptr callbackObject); +void CallBackHeartRate(std::map data, sptr callbackObject); +void CallBackWearDetection(std::map data, sptr callbackObject); +void CallBackAccelerometerUncalibrated(std::map data, + sptr callbackObject); +void CallBackColor(std::map data, sptr callbackObject); +void CallBackSar(std::map data, sptr callbackObject); +void EmitOnceCallback(SensorEvent *event); + +std::map g_samplingPeriod = { + { "normal", 200000000 }, + { "ui", 60000000 }, + { "game", 20000000 }, +}; + +std::map> g_sensorAttributeList = { + { 0, { "x" } }, + { SENSOR_TYPE_ID_ACCELEROMETER, { "x", "y", "z" } }, + { SENSOR_TYPE_ID_GYROSCOPE, { "x", "y", "z" } }, + { SENSOR_TYPE_ID_AMBIENT_LIGHT, { "intensity", "colorTemperature", "infraredLuminance" } }, + { SENSOR_TYPE_ID_MAGNETIC_FIELD, { "x", "y", "z" } }, + { SENSOR_TYPE_ID_BAROMETER, { "pressure" } }, + { SENSOR_TYPE_ID_HALL, { "status" } }, + { SENSOR_TYPE_ID_TEMPERATURE, { "temperature" } }, + { SENSOR_TYPE_ID_PROXIMITY, { "distance" } }, + { SENSOR_TYPE_ID_HUMIDITY, { "humidity" } }, + { SENSOR_TYPE_ID_ORIENTATION, { "alpha", "beta", "gamma" } }, + { SENSOR_TYPE_ID_GRAVITY, { "x", "y", "z" } }, + { SENSOR_TYPE_ID_LINEAR_ACCELERATION, { "x", "y", "z" } }, + { SENSOR_TYPE_ID_ROTATION_VECTOR, { "x", "y", "z", "w" } }, + { SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, { "temperature" } }, + { SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, { "x", "y", "z", "biasX", "biasY", "biasZ" } }, + { SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, { "x", "y", "z", "biasX", "biasY", "biasZ" } }, + { SENSOR_TYPE_ID_SIGNIFICANT_MOTION, { "scalar" } }, + { SENSOR_TYPE_ID_PEDOMETER_DETECTION, { "scalar" } }, + { SENSOR_TYPE_ID_PEDOMETER, { "steps" } }, + { SENSOR_TYPE_ID_HEART_RATE, { "heartRate" } }, + { SENSOR_TYPE_ID_WEAR_DETECTION, { "value" } }, + { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, { "x", "y", "z", "biasX", "biasY", "biasZ" } }, + { SENSOR_TYPE_ID_COLOR, { "lightIntensity", "colorTemperature" } }, + { SENSOR_TYPE_ID_SAR, { "absorptionRatio" } }, + { SENSOR_TYPE_ID_FUSION_PRESSURE, { "fusionPressure" } }, +}; + +std::map, sptr)>> + g_functionBySensorTypeIdMap = { + { SENSOR_TYPE_ID_ACCELEROMETER, CallBackAccelermeter }, + { SENSOR_TYPE_ID_GYROSCOPE, CallBackGyroscope }, + { SENSOR_TYPE_ID_AMBIENT_LIGHT, CallBackAmbientLight }, + { SENSOR_TYPE_ID_MAGNETIC_FIELD, CallBackMagneticField }, + { SENSOR_TYPE_ID_BAROMETER, CallBackBarometer }, + { SENSOR_TYPE_ID_HALL, CallBackHall }, + { SENSOR_TYPE_ID_PROXIMITY, CallBackProximity }, + { SENSOR_TYPE_ID_HUMIDITY, CallBackHumidity }, + { SENSOR_TYPE_ID_ORIENTATION, CallBackOrientation }, + { SENSOR_TYPE_ID_GRAVITY, CallBackGravity }, + { SENSOR_TYPE_ID_LINEAR_ACCELERATION, CallBackLinearAcceleration }, + { SENSOR_TYPE_ID_ROTATION_VECTOR, CallBackRotationVector }, + { SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, CallBackAmbientTemperature }, + { SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, CallBackMagneticFieldUncalibrated }, + { SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, CallBackGyroscopeUncalibrated }, + { SENSOR_TYPE_ID_SIGNIFICANT_MOTION, CallBackSignificantMotion }, + { SENSOR_TYPE_ID_PEDOMETER_DETECTION, CallBackPedometerDetection }, + { SENSOR_TYPE_ID_PEDOMETER, CallBackPedometer }, + { SENSOR_TYPE_ID_HEART_RATE, CallBackHeartRate }, + { SENSOR_TYPE_ID_WEAR_DETECTION, CallBackWearDetection }, + { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, CallBackAccelerometerUncalibrated }, + { SENSOR_TYPE_ID_COLOR, CallBackColor }, + { SENSOR_TYPE_ID_SAR, CallBackSar }, + }; + +std::mutex g_mutex; +std::mutex g_bodyMutex; +std::map>> g_subscribeCallbacks; +std::mutex g_onMutex; +std::mutex g_onceMutex; +std::map>> g_onceCallbackInfos; +std::map>> g_onCallbackInfos; +std::mutex g_sensorTaiheAttrListMutex; + +RotationMatrixResponse getRotationMatrixSyncGrav(array_view gravity, array_view geomagnetic) +{ + std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclination(THREE_DIMENSIONAL_MATRIX_LENGTH); + SensorAlgorithm sensorAlgorithm; + std::vector gravityVector(gravity.begin(), gravity.end()); + std::vector geomagneticVector(geomagnetic.begin(), geomagnetic.end()); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravityVector, geomagneticVector, rotation, inclination); + ohos::sensor::RotationMatrixResponse rsp = { + .rotation = array::make(THREE_DIMENSIONAL_MATRIX_LENGTH), + .inclination = array::make(THREE_DIMENSIONAL_MATRIX_LENGTH), + }; + if (ret != OHOS::ERR_OK) { + taihe::set_business_error(ret, "Create rotation and inclination matrix fail"); + return rsp; + } + for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + rsp.rotation[i] = rotation[i]; + rsp.inclination[i] = inclination[i]; + } + return rsp; +} + +array getOrientationSync(array_view rotationMatrix) +{ + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + SensorAlgorithm sensorAlgorithm; + std::vector rotationMatrixVector(rotationMatrix.begin(), rotationMatrix.end()); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrixVector, rotationAngle); + if (ret != OHOS::ERR_OK) { + taihe::set_business_error(ret, "Get direction fail"); + return taihe::array(rotationAngle); + } + return taihe::array(rotationAngle); +} + +array getRotationMatrixSync(array_view rotation) +{ + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + SensorAlgorithm sensorAlgorithm; + std::vector rotationVector(rotation.begin(), rotation.end()); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + if (ret != OHOS::ERR_OK) { + taihe::set_business_error(ret, "Create rotation matrix fail"); + return taihe::array(rotationMatrix); + } + return taihe::array(rotationMatrix); +} + +array getSensorListSync() +{ + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfos, &count); + std::vector<::ohos::sensor::Sensor> result; + if (ret != OHOS::ERR_OK) { + taihe::set_business_error(ret, "Get sensor list fail"); + return taihe::array<::ohos::sensor::Sensor>(result); + } + for (int32_t i = 0; i < count; ++i) { + if ((sensorInfos[i].sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT1) || + (sensorInfos[i].sensorTypeId == SENSOR_TYPE_ID_PROXIMITY1) || + (sensorInfos[i].sensorTypeId > GL_SENSOR_TYPE_PRIVATE_MIN_VALUE)) { + SEN_HILOGD("This sensor is secondary ambient light"); + continue; + } + ohos::sensor::Sensor sensorInfo = { + .sensorName = sensorInfos[i].sensorName, + .vendorName = sensorInfos[i].vendorName, + .firmwareVersion = sensorInfos[i].firmwareVersion, + .hardwareVersion = sensorInfos[i].hardwareVersion, + .sensorId = sensorInfos[i].sensorId, + .maxRange = sensorInfos[i].maxRange, + .minSamplePeriod = sensorInfos[i].minSamplePeriod, + .maxSamplePeriod = sensorInfos[i].maxSamplePeriod, + .precision = sensorInfos[i].precision, + .power = sensorInfos[i].power, + }; + result.push_back(sensorInfo); + } + return taihe::array<::ohos::sensor::Sensor>(result); +} + +bool GetInterval(const Options &options, int64_t &interval) +{ + if (options.interval.holds_interval_i64()) { + interval = *options.interval.get_interval_i64_ptr(); + } else if (options.interval.holds_interval_string()) { + taihe::string modeTemp = *options.interval.get_interval_string_ptr(); + auto iter = g_samplingPeriod.find(modeTemp); + if (iter == g_samplingPeriod.end()) { + SEN_HILOGE("Find interval mode failed"); + return false; + } + interval = static_cast(iter->second); + SEN_HILOGI("Get interval by mode, mode:%{public}s", modeTemp.c_str()); + } else { + SEN_HILOGE("Interval failed"); + return false; + } + return true; +} + +bool CheckSubscribe(int32_t sensorTypeId) +{ + std::lock_guard onCallbackLock(g_onMutex); + return g_onCallbackInfos.find(sensorTypeId) != g_onCallbackInfos.end(); +} + +void CallBackAccelermeter(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback accelerometerResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + AccelerometerResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackGyroscope(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback gyroscopeResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + GyroscopeResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackAmbientLight(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback lightResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + LightResponse responseData = { + .base = res, + .intensity = std::get(data["intensity"]), + .colorTemperature = taihe::optional(std::in_place_t{}, std::get(data["colorTemperature"])), + .infraredLuminance = taihe::optional(std::in_place_t{}, std::get(data["infraredLuminance"])), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackMagneticField(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback magneticFieldResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + MagneticFieldResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackBarometer(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback barometerResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + BarometerResponse responseData = { + .base = res, + .pressure = std::get(data["pressure"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackHall(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback hallResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + HallResponse responseData = { + .base = res, + .status = std::get(data["status"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackProximity(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback proximityResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + ProximityResponse responseData = { + .base = res, + .distance = std::get(data["distance"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackHumidity(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback humidityResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + HumidityResponse responseData = { + .base = res, + .humidity = std::get(data["humidity"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackOrientation(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback orientationResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + OrientationResponse responseData = { + .base = res, + .alpha = std::get(data["alpha"]), + .beta = std::get(data["beta"]), + .gamma = std::get(data["gamma"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackGravity(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback gravityResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + GravityResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackLinearAcceleration(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback linearAccelerometerResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + LinearAccelerometerResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackRotationVector(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback rotationVectorResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + RotationVectorResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + .w = std::get(data["w"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackAmbientTemperature(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback ambientTemperatureResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + AmbientTemperatureResponse responseData = { + .base = res, + .temperature = std::get(data["temperature"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackMagneticFieldUncalibrated(std::map data, + sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>( + callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback magneticFieldUncalibratedResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + MagneticFieldUncalibratedResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + .biasX = std::get(data["biasX"]), + .biasY = std::get(data["biasY"]), + .biasZ = std::get(data["biasZ"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackGyroscopeUncalibrated(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>( + callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback gyroscopeUncalibratedResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + GyroscopeUncalibratedResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + .biasX = std::get(data["biasX"]), + .biasY = std::get(data["biasY"]), + .biasZ = std::get(data["biasZ"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackSignificantMotion(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback significantMotionResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + SignificantMotionResponse responseData = { + .base = res, + .scalar = std::get(data["scalar"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackPedometerDetection(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback pedometerDetectionResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + PedometerDetectionResponse responseData = { + .base = res, + .scalar = std::get(data["scalar"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackPedometer(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback pedometerResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + PedometerResponse responseData = { + .base = res, + .steps = std::get(data["steps"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackHeartRate(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback heartRateResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + HeartRateResponse responseData = { + .base = res, + .heartRate = std::get(data["heartRate"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackWearDetection(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback wearDetectionResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + WearDetectionResponse responseData = { + .base = res, + .value = std::get(data["value"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackAccelerometerUncalibrated(std::map data, + sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>( + callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback accelerometerUncalibratedResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + AccelerometerUncalibratedResponse responseData = { + .base = res, + .x = std::get(data["x"]), + .y = std::get(data["y"]), + .z = std::get(data["z"]), + .biasX = std::get(data["biasX"]), + .biasY = std::get(data["biasY"]), + .biasZ = std::get(data["biasZ"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackColor(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback colorResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + ColorResponse responseData = { + .base = res, + .lightIntensity = std::get(data["lightIntensity"]), + .colorTemperature = std::get(data["colorTemperature"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallBackSar(std::map data, sptr callbackObject) +{ + if (callbackObject == nullptr) { + SEN_HILOGE("callbackObject is null"); + return; + } + if (!std::holds_alternative>(callbackObject->callback)) { + SEN_HILOGE("callbackObject is not of type callback sarResponse function"); + return; + } + ohos::sensor::Response res = { + .timestamp = std::get(data["timestamp"]), + .accuracy = std::get(data["accuracy"]), + }; + SarResponse responseData = { + .base = res, + .absorptionRatio = std::get(data["absorptionRatio"]), + }; + auto &func = std::get>(callbackObject->callback); + func(responseData); +} + +void CallbackDataBySensorTypeId(int32_t sensorTypeId, std::map data, + sptr callbackObject) +{ + if (g_functionBySensorTypeIdMap.find(sensorTypeId) == g_functionBySensorTypeIdMap.end()) { + SEN_HILOGE("SensorTypeId not exist, id:%{public}d", sensorTypeId); + return; + } + g_functionBySensorTypeIdMap[sensorTypeId](data, callbackObject); +} + +void CallbackSensorData(sptr callbackObject, SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + int32_t sensorTypeId = event->sensorTypeId; + std::lock_guard sensorTaiheAttrListLock(g_sensorTaiheAttrListMutex); + size_t size = g_sensorAttributeList[sensorTypeId].size(); + uint32_t dataLength = event->dataLen / sizeof(float); + if (size > dataLength) { + SEN_HILOGE("Data length mismatch"); + return; + } + auto sensorAttributes = g_sensorAttributeList[sensorTypeId]; + auto dataOrigin = reinterpret_cast(event->data); + float dataNow[CALLBACK_MAX_DATA_LENGTH] = { 0 }; + if (memcpy_s(dataNow, sizeof(dataNow), dataOrigin, event->dataLen) != EOK) { + SEN_HILOGE("Copy data failed"); + return; + } + std::map dataMap; + for (size_t i = 0; i < size; ++i) { + dataMap.emplace(sensorAttributes[i].c_str(), static_cast(dataNow[i])); + } + dataMap.emplace("timestamp", event->timestamp); + ohos::sensor::SensorAccuracy sensorAccuracyTemp(static_cast(event->option)); + dataMap.emplace("accuracy", sensorAccuracyTemp); + CallbackDataBySensorTypeId(sensorTypeId, dataMap, callbackObject); +} + +void EmitOnCallback(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + int32_t sensorTypeId = event->sensorTypeId; + if (!CheckSubscribe(sensorTypeId)) { + return; + } + std::lock_guard onCallbackLock(g_onMutex); + auto onCallbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto &onCallbackInfo : onCallbackInfos) { + CallbackSensorData(onCallbackInfo, event); + } +} + +bool CheckSystemSubscribe(int32_t sensorTypeId) +{ + std::lock_guard subscribeLock(g_mutex); + auto iter = g_subscribeCallbacks.find(sensorTypeId); + return iter != g_subscribeCallbacks.end(); +} + +void EmitSubscribeCallback(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + int32_t sensorTypeId = event->sensorTypeId; + if (!CheckSystemSubscribe(sensorTypeId)) { + return; + } + std::lock_guard subscribeLock(g_mutex); + auto callbacks = g_subscribeCallbacks[sensorTypeId]; + for (auto &callback : callbacks) { + CallbackSensorData(callback, event); + } +} + +void DataCallbackImpl(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + EmitOnCallback(event); + EmitSubscribeCallback(event); + EmitOnceCallback(event); +} + +const SensorUser user = { .callback = DataCallbackImpl }; + +int32_t UnsubscribeSensor(int32_t sensorTypeId) +{ + int32_t ret = DeactivateSensor(sensorTypeId, &user); + if (ret != ERR_OK) { + SEN_HILOGE("DeactivateSensor failed"); + return ret; + } + return UnsubscribeSensor(sensorTypeId, &user); +} + +void EmitOnceCallback(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + int32_t sensorTypeId = event->sensorTypeId; + std::lock_guard onceCallbackLock(g_onceMutex); + auto iter = g_onceCallbackInfos.find(sensorTypeId); + if (iter == g_onceCallbackInfos.end()) { + return; + } + auto &onceCallbackInfos = iter->second; + while (!onceCallbackInfos.empty()) { + auto onceCallbackInfo = onceCallbackInfos.front(); + auto beginIter = onceCallbackInfos.begin(); + onceCallbackInfos.erase(beginIter); + CallbackSensorData(onceCallbackInfo, event); + } + g_onceCallbackInfos.erase(sensorTypeId); + + CHKCV((!CheckSubscribe(sensorTypeId)), "Has client subscribe, not need cancel subscribe"); + CHKCV((!CheckSystemSubscribe(sensorTypeId)), "Has client subscribe system api, not need cancel subscribe"); + UnsubscribeSensor(sensorTypeId); +} + +int32_t SubscribeSensor(int32_t sensorTypeId, int64_t interval, RecordSensorCallback callback) +{ + int32_t ret = SubscribeSensor(sensorTypeId, &user); + if (ret != ERR_OK) { + SEN_HILOGE("SubscribeSensor failed"); + return ret; + } + ret = SetBatch(sensorTypeId, &user, interval, 0); + if (ret != ERR_OK) { + SEN_HILOGE("SetBatch failed"); + return ret; + } + return ActivateSensor(sensorTypeId, &user); +} + +void UpdateCallbackInfos(int32_t sensorTypeId, callbackType callback, uintptr_t opq) +{ + std::lock_guard onCallbackLock(g_onMutex); + ani_object callbackObj = reinterpret_cast(opq); + ani_ref callbackRef; + ani_env *env = taihe::get_env(); + if (env == nullptr || env->GlobalReference_Create(callbackObj, &callbackRef) != ANI_OK) { + SEN_HILOGE("Failed to create callbackRef, sensorTypeId:%{public}d", sensorTypeId); + return; + } + std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; + bool isSubscribedCallback = + std::any_of(callbackInfos.begin(), callbackInfos.end(), [env, callbackRef](const CallbackObject *obj) { + ani_boolean isEqual = false; + return (env->Reference_StrictEquals(callbackRef, obj->ref, &isEqual) == ANI_OK) && isEqual; + }); + if (isSubscribedCallback) { + env->GlobalReference_Delete(callbackRef); + SEN_HILOGE("Callback is already subscribed, sensorTypeId:%{public}d", sensorTypeId); + return; + } + sptr taiheCallbackInfo = new (std::nothrow) CallbackObject(callback, callbackRef); + if (taiheCallbackInfo == nullptr) { + SEN_HILOGE("taiheCallbackInfo is nullptr"); + return; + } + callbackInfos.push_back(taiheCallbackInfo); + g_onCallbackInfos[sensorTypeId] = callbackInfos; +} + +void OnCommon(int32_t sensorTypeId, callbackType cb, uintptr_t opq, optional_view options) +{ + int64_t interval = REPORTING_INTERVAL; + if (options.has_value() && !GetInterval(options.value(), interval)) { + SEN_HILOGW("Get interval failed"); + } + SEN_HILOGD("Interval is %{public}" PRId64, interval); + int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); + if (ret != ERR_OK) { + taihe::set_business_error(ret, "SubscribeSensor fail"); + return; + } + UpdateCallbackInfos(sensorTypeId, cb, opq); +} + +void OnWearDetection(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_WEAR_DETECTION, f, opq, options); +} + +void UpdateOnceCallback(int32_t sensorTypeId, callbackType cb, uintptr_t opq) +{ + std::lock_guard onceCallbackLock(g_onceMutex); + ani_object callbackObj = reinterpret_cast(opq); + ani_ref callbackRef; + ani_env *env = taihe::get_env(); + if (env == nullptr || env->GlobalReference_Create(callbackObj, &callbackRef) != ANI_OK) { + SEN_HILOGE("Failed to create callbackRef, sensorTypeId:%{public}d", sensorTypeId); + return; + } + std::vector> callbackInfos = g_onceCallbackInfos[sensorTypeId]; + bool isSubscribedCallback = + std::any_of(callbackInfos.begin(), callbackInfos.end(), [env, callbackRef](const CallbackObject *obj) { + ani_boolean isEqual = false; + return (env->Reference_StrictEquals(callbackRef, obj->ref, &isEqual) == ANI_OK) && isEqual; + }); + if (isSubscribedCallback) { + env->GlobalReference_Delete(callbackRef); + SEN_HILOGE("Callback is already subscribed, sensorTypeId:%{public}d", sensorTypeId); + return; + } + sptr taiheCallbackInfo = new (std::nothrow) CallbackObject(cb, callbackRef); + if (taiheCallbackInfo == nullptr) { + SEN_HILOGE("taiheCallbackInfo is nullptr"); + return; + } + callbackInfos.push_back(taiheCallbackInfo); + g_onceCallbackInfos[sensorTypeId] = callbackInfos; +} + +void OnceCommon(int32_t sensorTypeId, callbackType cb, uintptr_t opq) +{ + if (!CheckSubscribe(sensorTypeId)) { + SEN_HILOGD("No subscription to change sensor data, registration is required"); + int32_t ret = SubscribeSensor(sensorTypeId, REPORTING_INTERVAL, DataCallbackImpl); + if (ret != ERR_OK) { + taihe::set_business_error(ret, "SubscribeSensor fail"); + return; + } + } + UpdateOnceCallback(sensorTypeId, cb, opq); +} + +void OnceWearDetection(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_WEAR_DETECTION, f, opq); +} + +int32_t RemoveAllCallback(int32_t sensorTypeId) +{ + std::lock_guard onCallbackLock(g_onMutex); + std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { + CHKPC(*iter); + if (auto *env = taihe::get_env()) { + env->GlobalReference_Delete((*iter)->ref); + } + iter = callbackInfos.erase(iter); + } + if (callbackInfos.empty()) { + SEN_HILOGD("No subscription to change sensor data"); + g_onCallbackInfos.erase(sensorTypeId); + return 0; + } + g_onCallbackInfos[sensorTypeId] = callbackInfos; + return static_cast(callbackInfos.size()); +} + +int32_t RemoveCallback(int32_t sensorTypeId, uintptr_t opq) +{ + std::lock_guard onCallbackLock(g_onMutex); + ani_object callbackObj = reinterpret_cast(opq); + ani_ref callbackRef; + ani_env *env = taihe::get_env(); + if (env == nullptr || env->GlobalReference_Create(callbackObj, &callbackRef) != ANI_OK) { + SEN_HILOGE("Failed to create callbackRef, sensorTypeId:%{public}d", sensorTypeId); + return 0; + } + std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { + CHKPC(*iter); + ani_boolean isEqual = false; + if ((env->Reference_StrictEquals(callbackRef, (*iter)->ref, &isEqual) == ANI_OK) && isEqual) { + env->GlobalReference_Delete((*iter)->ref); + iter = callbackInfos.erase(iter); + SEN_HILOGD("Remove callback success"); + break; + } else { + ++iter; + } + } + env->GlobalReference_Delete(callbackRef); + if (callbackInfos.empty()) { + SEN_HILOGD("No subscription to change sensor data"); + g_onCallbackInfos.erase(sensorTypeId); + return 0; + } + g_onCallbackInfos[sensorTypeId] = callbackInfos; + return static_cast(callbackInfos.size()); +} + +void OffCommon(int32_t sensorTypeId, optional_view opq) +{ + int32_t subscribeSize = -1; + if (!opq.has_value()) { + subscribeSize = RemoveAllCallback(sensorTypeId); + } else { + subscribeSize = RemoveCallback(sensorTypeId, opq.value()); + } + if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) { + SEN_HILOGW("There are other client subscribe system js api as well, not need unsubscribe"); + return; + } + int32_t ret = UnsubscribeSensor(sensorTypeId); + if (ret != ERR_OK) { + SEN_HILOGE("UnsubscribeSensor fail, ret:%{public}d", ret); + taihe::set_business_error(ret, "UnsubscribeSensor fail"); + return; + } +} + +void OffWearDetection(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_WEAR_DETECTION, opq); +} + +void OnSignificantMotion(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_SIGNIFICANT_MOTION, f, opq, options); +} + +void OnceSignificantMotion(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_SIGNIFICANT_MOTION, f, opq); +} + +void OffSignificantMotion(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_SIGNIFICANT_MOTION, opq); +} + +void OnRotationVector(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_ROTATION_VECTOR, f, opq, options); +} + +void OnceRotationVector(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_ROTATION_VECTOR, f, opq); +} + +void OffRotationVector(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_ROTATION_VECTOR, opq); +} + +void OnProximity(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_PROXIMITY, f, opq, options); +} + +void OnceProximity(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_PROXIMITY, f, opq); +} + +void OffProximity(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_PROXIMITY, opq); +} + +void OnPedometerDetection(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_PEDOMETER_DETECTION, f, opq, options); +} + +void OncePedometerDetection(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_PEDOMETER_DETECTION, f, opq); +} + +void OffPedometerDetection(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_PEDOMETER_DETECTION, opq); +} + +void OnPedometer(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_PEDOMETER, f, opq, options); +} + +void OncePedometer(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_PEDOMETER, f, opq); +} + +void OffPedometer(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_PEDOMETER, opq); +} + +void OnOrientation(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_ORIENTATION, f, opq, options); +} + +void OnceOrientation(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_ORIENTATION, f, opq); +} + +void OffOrientation(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_ORIENTATION, opq); +} + +void OnMagneticFieldUncalibrated(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, f, opq, options); +} + +void OnceMagneticFieldUncalibrated(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, f, opq); +} + +void OffMagneticFieldUncalibrated(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, opq); +} + +void OnMagneticField(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_MAGNETIC_FIELD, f, opq, options); +} + +void OnceMagneticField(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_MAGNETIC_FIELD, f, opq); +} + +void OffMagneticField(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_MAGNETIC_FIELD, opq); +} + +void OnLinearAccelerometer(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_LINEAR_ACCELERATION, f, opq, options); +} + +void OnceLinearAccelerometer(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_LINEAR_ACCELERATION, f, opq); +} + +void OffLinearAccelerometer(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_LINEAR_ACCELERATION, opq); +} + +void OnHumidity(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_HUMIDITY, f, opq, options); +} + +void OnceHumidity(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_HUMIDITY, f, opq); +} + +void OffHumidity(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_HUMIDITY, opq); +} + +void OnHeartRate(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_HEART_RATE, f, opq, options); +} + +void OnceHeartRate(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_HEART_RATE, f, opq); +} + +void OffHeartRate(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_HEART_RATE, opq); +} + +void OnHall(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_HALL, f, opq, options); +} + +void OnceHall(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_HALL, f, opq); +} + +void OffHall(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_HALL, opq); +} + +void OnGyroscopeUncalibrated(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, f, opq, options); +} + +void OnceGyroscopeUncalibrated(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, f, opq); +} + +void OffGyroscopeUncalibrated(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, opq); +} + +void OnGyroscope(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_GYROSCOPE, f, opq, options); +} + +void OnceGyroscope(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_GYROSCOPE, f, opq); +} + +void OffGyroscope(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_GYROSCOPE, opq); +} + +void OnGravity(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_GRAVITY, f, opq, options); +} + +void OnceGravity(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_GRAVITY, f, opq); +} + +void OffGravity(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_GRAVITY, opq); +} + +void OnBarometer(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_BAROMETER, f, opq, options); +} + +void OnceBarometer(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_BAROMETER, f, opq); +} + +void OffBarometer(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_BAROMETER, opq); +} + +void OnAmbientTemperature(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, f, opq, options); +} + +void OnceAmbientTemperature(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, f, opq); +} + +void OffAmbientTemperature(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, opq); +} + +void OnAmbientLight(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_AMBIENT_LIGHT, f, opq, options); +} + +void OnceAmbientLight(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_AMBIENT_LIGHT, f, opq); +} + +void OffAmbientLight(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_AMBIENT_LIGHT, opq); +} + +void OnAccelerometerUncalibrated(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, f, opq, options); +} + +void OnceAccelerometerUncalibrated(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, f, opq); +} + +void OffAccelerometerUncalibrated(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, opq); +} + +void OnAccelerometer(callback_view f, uintptr_t opq, + optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_ACCELEROMETER, f, opq, options); +} + +void OnceAccelerometer(callback_view f, uintptr_t opq) +{ + OnceCommon(SENSOR_TYPE_ID_ACCELEROMETER, f, opq); +} + +void OffAccelerometer(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_ACCELEROMETER, opq); +} + +void OnSar(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_SAR, f, opq, options); +} + +void OffSar(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_SAR, opq); +} + +void OnColor(callback_view f, uintptr_t opq, optional_view options) +{ + OnCommon(SENSOR_TYPE_ID_COLOR, f, opq, options); +} + +void OffColor(optional_view opq) +{ + OffCommon(SENSOR_TYPE_ID_COLOR, opq); +} +} // namespace + +// Since there macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_getRotationMatrixSyncGrav(getRotationMatrixSyncGrav); +TH_EXPORT_CPP_API_getOrientationSync(getOrientationSync); +TH_EXPORT_CPP_API_getRotationMatrixSync(getRotationMatrixSync); +TH_EXPORT_CPP_API_getSensorListSync(getSensorListSync); +TH_EXPORT_CPP_API_OnWearDetection(OnWearDetection); +TH_EXPORT_CPP_API_OnceWearDetection(OnceWearDetection); +TH_EXPORT_CPP_API_OffWearDetection(OffWearDetection); +TH_EXPORT_CPP_API_OnSignificantMotion(OnSignificantMotion); +TH_EXPORT_CPP_API_OnceSignificantMotion(OnceSignificantMotion); +TH_EXPORT_CPP_API_OffSignificantMotion(OffSignificantMotion); +TH_EXPORT_CPP_API_OnRotationVector(OnRotationVector); +TH_EXPORT_CPP_API_OnceRotationVector(OnceRotationVector); +TH_EXPORT_CPP_API_OffRotationVector(OffRotationVector); +TH_EXPORT_CPP_API_OnProximity(OnProximity); +TH_EXPORT_CPP_API_OnceProximity(OnceProximity); +TH_EXPORT_CPP_API_OffProximity(OffProximity); +TH_EXPORT_CPP_API_OnPedometerDetection(OnPedometerDetection); +TH_EXPORT_CPP_API_OncePedometerDetection(OncePedometerDetection); +TH_EXPORT_CPP_API_OffPedometerDetection(OffPedometerDetection); +TH_EXPORT_CPP_API_OnPedometer(OnPedometer); +TH_EXPORT_CPP_API_OncePedometer(OncePedometer); +TH_EXPORT_CPP_API_OffPedometer(OffPedometer); +TH_EXPORT_CPP_API_OnOrientation(OnOrientation); +TH_EXPORT_CPP_API_OnceOrientation(OnceOrientation); +TH_EXPORT_CPP_API_OffOrientation(OffOrientation); +TH_EXPORT_CPP_API_OnMagneticFieldUncalibrated(OnMagneticFieldUncalibrated); +TH_EXPORT_CPP_API_OnceMagneticFieldUncalibrated(OnceMagneticFieldUncalibrated); +TH_EXPORT_CPP_API_OffMagneticFieldUncalibrated(OffMagneticFieldUncalibrated); +TH_EXPORT_CPP_API_OnMagneticField(OnMagneticField); +TH_EXPORT_CPP_API_OnceMagneticField(OnceMagneticField); +TH_EXPORT_CPP_API_OffMagneticField(OffMagneticField); +TH_EXPORT_CPP_API_OnLinearAccelerometer(OnLinearAccelerometer); +TH_EXPORT_CPP_API_OnceLinearAccelerometer(OnceLinearAccelerometer); +TH_EXPORT_CPP_API_OffLinearAccelerometer(OffLinearAccelerometer); +TH_EXPORT_CPP_API_OnHumidity(OnHumidity); +TH_EXPORT_CPP_API_OnceHumidity(OnceHumidity); +TH_EXPORT_CPP_API_OffHumidity(OffHumidity); +TH_EXPORT_CPP_API_OnHeartRate(OnHeartRate); +TH_EXPORT_CPP_API_OnceHeartRate(OnceHeartRate); +TH_EXPORT_CPP_API_OffHeartRate(OffHeartRate); +TH_EXPORT_CPP_API_OnHall(OnHall); +TH_EXPORT_CPP_API_OnceHall(OnceHall); +TH_EXPORT_CPP_API_OffHall(OffHall); +TH_EXPORT_CPP_API_OnGyroscopeUncalibrated(OnGyroscopeUncalibrated); +TH_EXPORT_CPP_API_OnceGyroscopeUncalibrated(OnceGyroscopeUncalibrated); +TH_EXPORT_CPP_API_OffGyroscopeUncalibrated(OffGyroscopeUncalibrated); +TH_EXPORT_CPP_API_OnGyroscope(OnGyroscope); +TH_EXPORT_CPP_API_OnceGyroscope(OnceGyroscope); +TH_EXPORT_CPP_API_OffGyroscope(OffGyroscope); +TH_EXPORT_CPP_API_OnGravity(OnGravity); +TH_EXPORT_CPP_API_OnceGravity(OnceGravity); +TH_EXPORT_CPP_API_OffGravity(OffGravity); +TH_EXPORT_CPP_API_OnBarometer(OnBarometer); +TH_EXPORT_CPP_API_OnceBarometer(OnceBarometer); +TH_EXPORT_CPP_API_OffBarometer(OffBarometer); +TH_EXPORT_CPP_API_OnAmbientTemperature(OnAmbientTemperature); +TH_EXPORT_CPP_API_OnceAmbientTemperature(OnceAmbientTemperature); +TH_EXPORT_CPP_API_OffAmbientTemperature(OffAmbientTemperature); +TH_EXPORT_CPP_API_OnAmbientLight(OnAmbientLight); +TH_EXPORT_CPP_API_OnceAmbientLight(OnceAmbientLight); +TH_EXPORT_CPP_API_OffAmbientLight(OffAmbientLight); +TH_EXPORT_CPP_API_OnAccelerometerUncalibrated(OnAccelerometerUncalibrated); +TH_EXPORT_CPP_API_OnceAccelerometerUncalibrated(OnceAccelerometerUncalibrated); +TH_EXPORT_CPP_API_OffAccelerometerUncalibrated(OffAccelerometerUncalibrated); +TH_EXPORT_CPP_API_OnAccelerometer(OnAccelerometer); +TH_EXPORT_CPP_API_OnceAccelerometer(OnceAccelerometer); +TH_EXPORT_CPP_API_OffAccelerometer(OffAccelerometer); +TH_EXPORT_CPP_API_OnSar(OnSar); +TH_EXPORT_CPP_API_OffSar(OffSar); +TH_EXPORT_CPP_API_OnColor(OnColor); +TH_EXPORT_CPP_API_OffColor(OffColor); +// NOLINTEND \ No newline at end of file diff --git a/frameworks/ets/taihe/idl/ohos.sensor.taihe b/frameworks/ets/taihe/idl/ohos.sensor.taihe new file mode 100644 index 00000000..becd5ce2 --- /dev/null +++ b/frameworks/ets/taihe/idl/ohos.sensor.taihe @@ -0,0 +1,1561 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@!namespace("@ohos.sensor", "sensor") + +/** + * Enum for obtain the type of sensor. + * @enum { i32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ +/** + * Enum for obtain the type of sensor. + * @enum { i32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +enum SensorId: i32 { + /** + * Acceleration sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + /** + * Acceleration sensor. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + ACCELEROMETER = 1, + + /** + * Gyroscope sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + /** + * Gyroscope sensor. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + GYROSCOPE = 2, + + /** + * Ambient light sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + AMBIENT_LIGHT = 5, + + /** + * Magnetic field sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + MAGNETIC_FIELD = 6, + + /** + * Barometric pressure sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + BAROMETER = 8, + + /** + * Hall effect sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + HALL = 10, + + /** + * Proximity sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + PROXIMITY = 12, + + /** + * Humidity sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + HUMIDITY = 13, + + /** + * Color sensor. + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ + COLOR = 14, + + /** + * Sar sensor. + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ + SAR = 15, + + /** + * Orientation sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + /** + * Orientation sensor. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + ORIENTATION = 256, + + /** + * Gravity sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + GRAVITY = 257, + + /** + * Linear acceleration sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + LINEAR_ACCELEROMETER = 258, + + /** + * Rotation vector sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + ROTATION_VECTOR = 259, + + /** + * Ambient temperature sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + AMBIENT_TEMPERATURE = 260, + + /** + * Uncalibrated magnetic field sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + MAGNETIC_FIELD_UNCALIBRATED = 261, + + /** + * Uncalibrated gyroscope sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + GYROSCOPE_UNCALIBRATED = 263, + + /** + * Significant motion sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + SIGNIFICANT_MOTION = 264, + + /** + * Pedometer detection sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + PEDOMETER_DETECTION = 265, + + /** + * Pedometer sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + PEDOMETER = 266, + + /** + * Heart rate sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + HEART_RATE = 278, + + /** + * Wear detection sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + WEAR_DETECTION = 280, + + /** + * Uncalibrated acceleration sensor. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + ACCELEROMETER_UNCALIBRATED = 281 +} + +/** + * Enumerates the accuracy levels of data reported by a sensor. + * @enum { i32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +enum SensorAccuracy: i32 { + /** + * The sensor data is unreliable. It is possible that the sensor does not contact with the device to measure. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + ACCURACY_UNRELIABLE = 0, + + /** + * The sensor data is at a low accuracy level. The data must be calibrated based on the environment before being used. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + 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. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + ACCURACY_MEDIUM = 2, + + /** + * The sensor data is at a high accuracy level. The data can be used directly. + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + ACCURACY_HIGH = 3 +} + +/** + * The basic data structure of the sensor event. + * @typedef Response + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +/** + * The basic data structure of the sensor event. + * @typedef Response + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +struct Response { + /** + * The timestamp of the reported sensor data. + * @type { i64 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * The timestamp of the reported sensor data. + * @type { i64 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + timestamp: i64; + + /** + * The accuracy levels of data reported by a sensor. + * @type { SensorAccuracy } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + accuracy: SensorAccuracy; +} + +/** + * Acceleration sensor event data. + * @typedef AccelerometerResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +/** + * Acceleration sensor event data. + * @typedef AccelerometerResponse + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +struct AccelerometerResponse { + + @extends base: Response; + + /** + * Acceleration x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Acceleration x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + x: f32; + + /** + * Acceleration y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Acceleration y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + y: f32; + + /** + * Acceleration z-axis component + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Acceleration z-axis component + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + z: f32; +} + +/** + * Linear acceleration sensor event data. + * @typedef LinearAccelerometerResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct LinearAccelerometerResponse { + + @extends base: Response; + + /** + * Linear acceleration x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Linear acceleration y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Linear acceleration z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; +} + +/** + * Acceleration uncalibrated sensor event data. + * @typedef AccelerometerUncalibratedResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct AccelerometerUncalibratedResponse { + + @extends base: Response; + + /** + * Acceleration uncalibrated x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Acceleration uncalibrated y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Acceleration uncalibrated z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; + + /** + * Acceleration uncalibrated x-axis offset. + * + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasX: f32; + + /** + * Acceleration uncalibrated y-axis offset. + * + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasY: f32; + + /** + * Acceleration uncalibrated z-axis offset. + * + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasZ: f32; +} + +/** + * Gravity sensor event data. + * @typedef GravityResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct GravityResponse { + + @extends base: Response; + + /** + * Gravity x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Gravity y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Gravity z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; +} + +/** + * Orientation sensor event data. + * @typedef OrientationResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +/** + * Orientation sensor event data. + * @typedef OrientationResponse + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +struct OrientationResponse { + + @extends base: Response; + + /** + * The device rotates at an angle around the Z axis. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * The device rotates at an angle around the Z axis. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + alpha: f32; + + /** + * The device rotates at an angle around the X axis. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * The device rotates at an angle around the X axis. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + beta: f32; + + /** + * The device rotates at an angle around the Y axis. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * The device rotates at an angle around the Y axis. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + gamma: f32; +} + +/** + * Rotation vector sensor event data. + * @typedef RotationVectorResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct RotationVectorResponse { + + @extends base: Response; + + /** + * Rotation vector x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Rotation vector y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Rotation vector z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; + + /** + * Scalar quantity. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + w: f32; +} + +/** + * Gyroscope sensor event data. + * @typedef GyroscopeResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +/** + * Gyroscope sensor event data. + * @typedef GyroscopeResponse + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +struct GyroscopeResponse { + + @extends base: Response; + + /** + * Gyroscope x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Gyroscope x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + x: f32; + + /** + * Gyroscope y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Gyroscope y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + y: f32; + + /** + * Gyroscope z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Gyroscope z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + z: f32; +} + +/** + * Gyroscope uncalibrated sensor event data. + * @typedef GyroscopeUncalibratedResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct GyroscopeUncalibratedResponse { + + @extends base: Response; + + /** + * Gyroscope uncalibrated x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Gyroscope uncalibrated y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Gyroscope uncalibrated z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; + + /** + * Gyroscope uncalibrated x-axis offset. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasX: f32; + + /** + * Gyroscope uncalibrated y-axis offset. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasY: f32; + + /** + * Gyroscope uncalibrated z-axis offset. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasZ: f32; +} + +/** + * Significant motion sensor event data. + * @typedef SignificantMotionResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct SignificantMotionResponse { + + @extends base: Response; + + /** + * The degree of significant motion. + * Whether the device has a significant motion. + * The value 1 means that the device has a significant motion, and 0 means the opposite. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + scalar: f32; +} + +/** + * Proximity sensor event data. + * @typedef ProximityResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct ProximityResponse { + + @extends base: Response; + + /** + * Indicates the degree of proximity, event 0 indicates proximity, and greater than 0 indicates distance. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + distance: f32; +} + +/** + * Light sensor event data. + * @typedef LightResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct LightResponse { + + @extends base: Response; + + /** + * Indicates light intensity, in lux. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + intensity: f32; + + /** + * Indicates color temperature, in kelvin. + * @type { ?f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 12 + */ + colorTemperature: Optional; + + /** + * Indicates infrared luminance, in cd/m2. + * @type { ?f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 12 + */ + infraredLuminance: Optional; +} + +/** + * Hall sensor event data. + * @typedef HallResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct HallResponse { + + @extends base: Response; + + /** + * Indicates hall status, 0 indicates open, and greater than 0 indicates suction. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + status: f32; +} + +/** + * Magnetic field sensor event data. + * @typedef MagneticFieldResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct MagneticFieldResponse { + + @extends base: Response; + + /** + * Magnetic field x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Magnetic field y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Magnetic field z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; +} + +/** + * Magnetic field uncalibrated sensor event data. + * @typedef MagneticFieldUncalibratedResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct MagneticFieldUncalibratedResponse { + + @extends base: Response; + + /** + * Magnetic field uncalibrated x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Magnetic field uncalibrated y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Magnetic field uncalibrated z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; + + /** + * Magnetic field uncalibrated x-axis offset. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasX: f32; + + /** + * Magnetic field uncalibrated y-axis offset. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasY: f32; + + /** + * Magnetic field uncalibrated z-axis offset. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + biasZ: f32; +} + +/** + * Pedometer sensor event data. + * @typedef PedometerResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct PedometerResponse { + + @extends base: Response; + + /** + * Indicates the number of steps. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + steps: f32; +} + +/** + * Pedometer detection sensor event data. + * @typedef PedometerDetectionResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct PedometerDetectionResponse { + + @extends base: Response; + + /** + * Indicates the pedometer detection status, 1 indicates that a walking action has occurred, + * and 0 indicates that no movement has occurred. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + scalar: f32; +} + +/** + * Heart rate sensor event data. + * @typedef HeartRateResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct HeartRateResponse { + + @extends base: Response; + + /** + * Indicates the number of heart rate. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + heartRate: f32; +} + +/** + * Barometer sensor event data. + * @typedef BarometerResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct BarometerResponse { + + @extends base: Response; + + /** + * Indicates the number of barometer, in hpa. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + pressure: f32; +} + +/** + * Wear detection sensor event data. + * @typedef WearDetectionResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct WearDetectionResponse { + + @extends base: Response; + + /** + * Indicates the status of wear detection, 1 for wearing, 0 for wearing not. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + value: f32; +} + +/** + * Humidity sensor event data. + * @typedef HumidityResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct HumidityResponse { + + @extends base: Response; + + /** + * Indicates the number of humidity. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + humidity: f32; +} + +/** + * Ambient temperature sensor event data. + * @typedef AmbientTemperatureResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct AmbientTemperatureResponse { + + @extends base: Response; + + /** + * Indicates ambient temperature, in celsius. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + temperature: f32; +} + +/** + * Sar sensor event data. + * @typedef SarResponse + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ +struct SarResponse { + + @extends base: Response; + + /** + * Indicates the specific absorption rate, in W/kg. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ + absorptionRatio: f32; +} + +/** + * Color sensor event data. + * @typedef ColorResponse + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ +struct ColorResponse { + + @extends base: Response; + + /** + * Indicates the intensity of light, in lux. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ + lightIntensity: f32; + /** + * Indicates the color temperature, in kelvin. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @systemapi + * @since 10 + */ + colorTemperature: f32; +} + +/** + * Indicates the response of rotation matrix. + * @typedef RotationMatrixResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct RotationMatrixResponse { + /** + * rotation matrix. + * @type { Array } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + rotation: Array; + + /** + * inclination matrix. + * @type { Array } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + inclination: Array; +} + +/** + * Indicates the axis of the new coordinate system that coincides with the XY axis of the original coordinate system. + * @typedef CoordinatesOptions + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct CoordinatesOptions { + /** Indicates the axis of the new coordinate system that coincides with the X axis of the original coordinate system. + * @type { i32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: i32; + + /** Indicates the axis of the new coordinate system that coincides with the Y axis of the original coordinate system. + * @type { i32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: i32; +} + +/** + * Indicates geomagnetic field data. + * @typedef GeomagneticResponse + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +struct GeomagneticResponse { + /** + * Geomagnetic x-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + x: f32; + + /** + * Geomagnetic y-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + y: f32; + + /** + * Geomagnetic z-axis component. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + z: f32; + + /** + * The Angle between the earth's magnetic field lines and the horizontal plane. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + geomagneticDip: f32; + + /** + * The Angle of magnetic north and true north on a horizontal plane. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + deflectionAngle: f32; + + /** + * The horizontal strength of the geomagnetic field. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + levelIntensity: f32; + + /** + * The total strength of the geomagnetic field. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + totalIntensity: f32; +} + +/** + * Indicates sensor information. + * @typedef Sensor + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ +struct Sensor { + /** + * Sensor name. + * @type { String } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + sensorName: String; + + /** + * Sensor vendor. + * @type { String } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + vendorName: String; + + /** + * Sensor firmware version. + * @type { String } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + firmwareVersion: String; + + /** + * Sensor hardware version. + * @type { String } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + hardwareVersion: String; + + /** + * Sensor type ID, {@code SensorType}. + * @type { i32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + sensorId: i32; + + /** + * Maximum measurement range of the sensor. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + maxRange: f32; + + /** + * Minimum sample period allowed, in ns. + * @type { i64 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + minSamplePeriod: i64; + + /** + * Maximum sample period allowed, in ns. + * @type { i64 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + maxSamplePeriod: i64; + + /** + * Sensor accuracy. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + precision: f32; + + /** + * Sensor power. + * @type { f32 } + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ + power: f32; +} + +/** + * The sensor reporting frequency is divided into three modes. + * @typedef {'game' | 'ui' | 'normal'} + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +@!sts_inject(""" + type SensorFrequency = 'game' | 'ui' | 'normal'; +""") + +union MapOption { + interval_i64: i64; + interval_string: String; +} + +/** + * Subscribe to the sensor's optional parameters. + * @typedef Options + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ +/** + * Subscribe to the sensor's optional parameters. + * @typedef Options + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ +struct Options { + /** + * Sensor event reporting event interval. + * @type { i64 } + * @syscap SystemCapability.Sensors.Sensor + * @since 8 + */ + /** + * Sensor event reporting event interval. + * @type { MapOption } + * @syscap SystemCapability.Sensors.Sensor + * @atomicservice + * @since 11 + */ + interval: MapOption; +} + +/** + * Calculate rotation matrix based on gravity vector and geomagnetic vector. + * @param { Array } gravity - gravity Indicates the gravity vector. + * @param { Array } geomagnetic - geomagnetic Indicates the geomagnetic vector. + * @param { AsyncCallback } callback - callback rotation matrix and inclination matrix. + * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; + *
2. Incorrect parameter types; 3. Parameter verification failed. + * @throws { BusinessError } 14500101 - Service exception. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ +@gen_async("getRotationMatrix") +@gen_promise("getRotationMatrix") +function getRotationMatrixSyncGrav(gravity: Array, geomagnetic: Array): RotationMatrixResponse; + +/** + * Computes the device's orientation based on the rotation matrix. + * @param { Array } rotationMatrix - rotationMatrix Indicates the rotation matrix. + * @param { AsyncCallback> } callback - callback the angle of rotation around the z, x, y axis. + * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; + *
2. Incorrect parameter types; 3. Parameter verification failed. + * @throws { BusinessError } 14500101 - Service exception. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ +@gen_async("getOrientation") +@gen_promise("getOrientation") +function getOrientationSync(rotationMatrix: Array): Array; + +/** + * Convert rotation vector to rotation matrix. + * @param { Array } rotationVector - rotationVector Indicates the rotation vector. + * @param { AsyncCallback> } callback - callback rotation matrix. + * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; + *
2. Incorrect parameter types; 3. Parameter verification failed. + * @throws { BusinessError } 14500101 - Service exception. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ +@gen_async("getRotationMatrix") +@gen_promise("getRotationMatrix") +function getRotationMatrixSync(rotationVector: Array): Array; + +/** + * Obtains all sensor information on the device. + * @param { AsyncCallback> } callback - callback sensor list. + * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; + *
2. Incorrect parameter types; 3. Parameter verification failed. + * @throws { BusinessError } 14500101 - Service exception. + * @syscap SystemCapability.Sensors.Sensor + * @since 9 + */ +@gen_async("getSensorList") +@gen_promise("getSensorList") +function getSensorListSync(): Array; + +@!sts_inject(""" + function on(type: SensorId, callback: (arg0 : Object) => void, options?: Options) { + switch(type) { + case SensorId.WEAR_DETECTION: return onWearDetection(callback as (info: WearDetectionResponse) => void, callback, options); + case SensorId.SIGNIFICANT_MOTION: return onSignificantMotion(callback as (info: SignificantMotionResponse) => void, callback, options); + case SensorId.ROTATION_VECTOR: return onRotationVector(callback as (info: RotationVectorResponse) => void, callback, options); + case SensorId.PROXIMITY: return onProximity(callback as (info: ProximityResponse) => void, callback, options); + case SensorId.PEDOMETER_DETECTION: return onPedometerDetection(callback as (info: PedometerDetectionResponse) => void, callback, options); + case SensorId.PEDOMETER: return onPedometer(callback as (info: PedometerResponse) => void, callback, options); + case SensorId.ORIENTATION: return onOrientation(callback as (info: OrientationResponse) => void, callback, options); + case SensorId.MAGNETIC_FIELD_UNCALIBRATED: return onMagneticFieldUncalibrated(callback as (info: MagneticFieldUncalibratedResponse) => void, callback, options); + case SensorId.MAGNETIC_FIELD: return onMagneticField(callback as (info: MagneticFieldResponse) => void, callback, options); + case SensorId.LINEAR_ACCELEROMETER: return onLinearAccelerometer(callback as (info: LinearAccelerometerResponse) => void, callback, options); + case SensorId.HUMIDITY: return onHumidity(callback as (info: HumidityResponse) => void, callback, options); + case SensorId.HEART_RATE: return onHeartRate(callback as (info: HeartRateResponse) => void, callback, options); + case SensorId.HALL: return onHall(callback as (info: HallResponse) => void, callback, options); + case SensorId.GYROSCOPE_UNCALIBRATED: return onGyroscopeUncalibrated(callback as (info: GyroscopeUncalibratedResponse) => void, callback, options); + case SensorId.GYROSCOPE: return onGyroscope(callback as (info: GyroscopeResponse) => void, callback, options); + case SensorId.GRAVITY: return onGravity(callback as (info: GravityResponse) => void, callback, options); + case SensorId.BAROMETER: return onBarometer(callback as (info: BarometerResponse) => void, callback, options); + case SensorId.AMBIENT_TEMPERATURE: return onAmbientTemperature(callback as (info: AmbientTemperatureResponse) => void, callback, options); + case SensorId.AMBIENT_LIGHT: return onAmbientLight(callback as (info: LightResponse) => void, callback, options); + case SensorId.ACCELEROMETER_UNCALIBRATED: return onAccelerometerUncalibrated(callback as (info: AccelerometerUncalibratedResponse) => void, callback, options); + case SensorId.ACCELEROMETER: return onAccelerometer(callback as (info: AccelerometerResponse) => void, callback, options); + case SensorId.SAR: return onSar(callback as (info: SarResponse) => void, callback, options); + case SensorId.COLOR: return onColor(callback as (info: ColorResponse) => void, callback, options); + default: throw new Error(`Unknown type: ${type}`); + } + } + function once(type: SensorId, callback: (arg0 : Object) => void) { + switch(type) { + case SensorId.WEAR_DETECTION: return onceWearDetection(callback as (info: WearDetectionResponse) => void, callback); + case SensorId.SIGNIFICANT_MOTION: return onceSignificantMotion(callback as (info: SignificantMotionResponse) => void, callback); + case SensorId.ROTATION_VECTOR: return onceRotationVector(callback as (info: RotationVectorResponse) => void, callback); + case SensorId.PROXIMITY: return onceProximity(callback as (info: ProximityResponse) => void, callback); + case SensorId.PEDOMETER_DETECTION: return oncePedometerDetection(callback as (info: PedometerDetectionResponse) => void, callback); + case SensorId.PEDOMETER: return oncePedometer(callback as (info: PedometerResponse) => void, callback); + case SensorId.ORIENTATION: return onceOrientation(callback as (info: OrientationResponse) => void, callback); + case SensorId.MAGNETIC_FIELD_UNCALIBRATED: return onceMagneticFieldUncalibrated(callback as (info: MagneticFieldUncalibratedResponse) => void, callback); + case SensorId.MAGNETIC_FIELD: return onceMagneticField(callback as (info: MagneticFieldResponse) => void, callback); + case SensorId.LINEAR_ACCELEROMETER: return onceLinearAccelerometer(callback as (info: LinearAccelerometerResponse) => void, callback); + case SensorId.HUMIDITY: return onceHumidity(callback as (info: HumidityResponse) => void, callback); + case SensorId.HEART_RATE: return onceHeartRate(callback as (info: HeartRateResponse) => void, callback); + case SensorId.HALL: return onceHall(callback as (info: HallResponse) => void, callback); + case SensorId.GYROSCOPE_UNCALIBRATED: return onceGyroscopeUncalibrated(callback as (info: GyroscopeUncalibratedResponse) => void, callback); + case SensorId.GYROSCOPE: return onceGyroscope(callback as (info: GyroscopeResponse) => void, callback); + case SensorId.GRAVITY: return onceGravity(callback as (info: GravityResponse) => void, callback); + case SensorId.BAROMETER: return onceBarometer(callback as (info: BarometerResponse) => void, callback); + case SensorId.AMBIENT_TEMPERATURE: return onceAmbientTemperature(callback as (info: AmbientTemperatureResponse) => void, callback); + case SensorId.AMBIENT_LIGHT: return onceAmbientLight(callback as (info: LightResponse) => void, callback); + case SensorId.ACCELEROMETER_UNCALIBRATED: return onceAccelerometerUncalibrated(callback as (info: AccelerometerUncalibratedResponse) => void, callback); + case SensorId.ACCELEROMETER: return onceAccelerometer(callback as (info: AccelerometerResponse) => void, callback); + default: throw new Error(`Unknown type: ${type}`); + } + } + function off(type: SensorId, callback?: (arg0 : Object) => void) { + switch(type) { + case SensorId.WEAR_DETECTION: return offWearDetection(callback); + case SensorId.SIGNIFICANT_MOTION: return offSignificantMotion(callback); + case SensorId.ROTATION_VECTOR: return offRotationVector(callback); + case SensorId.PROXIMITY: return offProximity(callback); + case SensorId.PEDOMETER_DETECTION: return offPedometerDetection(callback); + case SensorId.PEDOMETER: return offPedometer(callback); + case SensorId.ORIENTATION: return offOrientation(callback); + case SensorId.MAGNETIC_FIELD_UNCALIBRATED: return offMagneticFieldUncalibrated(callback); + case SensorId.MAGNETIC_FIELD: return offMagneticField(callback); + case SensorId.LINEAR_ACCELEROMETER: return offLinearAccelerometer(callback); + case SensorId.HUMIDITY: return offHumidity(callback); + case SensorId.HEART_RATE: return offHeartRate(callback); + case SensorId.HALL: return offHall(callback); + case SensorId.GYROSCOPE_UNCALIBRATED: return offGyroscopeUncalibrated(callback); + case SensorId.GYROSCOPE: return offGyroscope(callback); + case SensorId.GRAVITY: return offGravity(callback); + case SensorId.BAROMETER: return offBarometer(callback); + case SensorId.AMBIENT_TEMPERATURE: return offAmbientTemperature(callback); + case SensorId.AMBIENT_LIGHT: return offAmbientLight(callback); + case SensorId.ACCELEROMETER_UNCALIBRATED: return offAccelerometerUncalibrated(callback); + case SensorId.ACCELEROMETER: return offAccelerometer(callback); + case SensorId.SAR: return offSar(callback); + case SensorId.COLOR: return offColor(callback); + default: throw new Error(`Unknown type: ${type}`); + } + } +""") +function OnWearDetection(f: (info: WearDetectionResponse) => void, opq: Opaque, options: Optional); +function OnceWearDetection(f: (info: WearDetectionResponse) => void, opq: Opaque); +function OffWearDetection(opq: Optional); + +function OnSignificantMotion(f: (info: SignificantMotionResponse) => void, opq: Opaque, options: Optional); +function OnceSignificantMotion(f: (info: SignificantMotionResponse) => void, opq: Opaque); +function OffSignificantMotion(opq: Optional); + +function OnRotationVector(f: (info: RotationVectorResponse) => void, opq: Opaque, options: Optional); +function OnceRotationVector(f: (info: RotationVectorResponse) => void, opq: Opaque); +function OffRotationVector(opq: Optional); + +function OnProximity(f: (info: ProximityResponse) => void, opq: Opaque, options: Optional); +function OnceProximity(f: (info: ProximityResponse) => void, opq: Opaque); +function OffProximity(opq: Optional); + +function OnPedometerDetection(f: (info: PedometerDetectionResponse) => void, opq: Opaque, options: Optional); +function OncePedometerDetection(f: (info: PedometerDetectionResponse) => void, opq: Opaque); +function OffPedometerDetection(opq: Optional); + +function OnPedometer(f: (info: PedometerResponse) => void, opq: Opaque, options: Optional); +function OncePedometer(f: (info: PedometerResponse) => void, opq: Opaque); +function OffPedometer(opq: Optional); + +function OnOrientation(f: (info: OrientationResponse) => void, opq: Opaque, options: Optional); +function OnceOrientation(f: (info: OrientationResponse) => void, opq: Opaque); +function OffOrientation(opq: Optional); + +function OnMagneticFieldUncalibrated(f: (info: MagneticFieldUncalibratedResponse) => void, opq: Opaque, options: Optional); +function OnceMagneticFieldUncalibrated(f: (info: MagneticFieldUncalibratedResponse) => void, opq: Opaque); +function OffMagneticFieldUncalibrated(opq: Optional); + +function OnMagneticField(f: (info: MagneticFieldResponse) => void, opq: Opaque, options: Optional); +function OnceMagneticField(f: (info: MagneticFieldResponse) => void, opq: Opaque); +function OffMagneticField(opq: Optional); + +function OnLinearAccelerometer(f: (info: LinearAccelerometerResponse) => void, opq: Opaque, options: Optional); +function OnceLinearAccelerometer(f: (info: LinearAccelerometerResponse) => void, opq: Opaque); +function OffLinearAccelerometer(opq: Optional); + +function OnHumidity(f: (info: HumidityResponse) => void, opq: Opaque, options: Optional); +function OnceHumidity(f: (info: HumidityResponse) => void, opq: Opaque); +function OffHumidity(opq: Optional); + +function OnHeartRate(f: (info: HeartRateResponse) => void, opq: Opaque, options: Optional); +function OnceHeartRate(f: (info: HeartRateResponse) => void, opq: Opaque); +function OffHeartRate(opq: Optional); + +function OnHall(f: (info: HallResponse) => void, opq: Opaque, options: Optional); +function OnceHall(f: (info: HallResponse) => void, opq: Opaque); +function OffHall(opq: Optional); + +function OnGyroscopeUncalibrated(f: (info: GyroscopeUncalibratedResponse) => void, opq: Opaque, options: Optional); +function OnceGyroscopeUncalibrated(f: (info: GyroscopeUncalibratedResponse) => void, opq: Opaque); +function OffGyroscopeUncalibrated(opq: Optional); + +function OnGyroscope(f: (info: GyroscopeResponse) => void, opq: Opaque, options: Optional); +function OnceGyroscope(f: (info: GyroscopeResponse) => void, opq: Opaque); +function OffGyroscope(opq: Optional); + +function OnGravity(f: (info: GravityResponse) => void, opq: Opaque, options: Optional); +function OnceGravity(f: (info: GravityResponse) => void, opq: Opaque); +function OffGravity(opq: Optional); + +function OnBarometer(f: (info: BarometerResponse) => void, opq: Opaque, options: Optional); +function OnceBarometer(f: (info: BarometerResponse) => void, opq: Opaque); +function OffBarometer(opq: Optional); + +function OnAmbientTemperature(f: (info: AmbientTemperatureResponse) => void, opq: Opaque, options: Optional); +function OnceAmbientTemperature(f: (info: AmbientTemperatureResponse) => void, opq: Opaque); +function OffAmbientTemperature(opq: Optional); + +function OnAmbientLight(f: (info: LightResponse) => void, opq: Opaque, options: Optional); +function OnceAmbientLight(f: (info: LightResponse) => void, opq: Opaque); +function OffAmbientLight(opq: Optional); + +function OnAccelerometerUncalibrated(f: (info: AccelerometerUncalibratedResponse) => void, opq: Opaque, options: Optional); +function OnceAccelerometerUncalibrated(f: (info: AccelerometerUncalibratedResponse) => void, opq: Opaque); +function OffAccelerometerUncalibrated(opq: Optional); + +function OnAccelerometer(f: (info: AccelerometerResponse) => void, opq: Opaque, options: Optional); +function OnceAccelerometer(f: (info: AccelerometerResponse) => void, opq: Opaque); +function OffAccelerometer(opq: Optional); + +function OnSar(f: (info: SarResponse) => void, opq: Opaque, options: Optional); +function OffSar(opq: Optional); + +function OnColor(f: (info: ColorResponse) => void, opq: Opaque, options: Optional); +function OffColor(opq: Optional); \ No newline at end of file diff --git a/frameworks/ets/taihe/user/main.ets b/frameworks/ets/taihe/user/main.ets new file mode 100644 index 00000000..d2b07cab --- /dev/null +++ b/frameworks/ets/taihe/user/main.ets @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import sensor from "@ohos.sensor"; +import {BusinessError} from "@ohos.base"; + +loadLibrary("sensor_taihe_native.z"); + +function testGetRotationMatrix() { + console.log("taihetest testGetRotationMatrix begin"); + try { + let gravity: float[] = [-0.27775216, 0.5351276, 9.788099]; + let geomagnetic: float[] = [210.87253, -78.6096, -111.44444]; + sensor.getRotationMatrix(gravity, geomagnetic, (err: BusinessError, data: sensor.RotationMatrixResponse) => { + if (err.code != 0) { + console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); + return; + } + console.info("Succeeded in getting rotationMatrix: " + JSON.stringify(data)); + }) + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testGetRotationMatrix end"); +} + +function testGetOrientation() { + console.log("taihetest testGetOrientation begin"); + try { + let preRotationMatrix: float[] = [1, 0, 0, 0, 0.87, -0.50, 0 , 0.50, 0.87]; + sensor.getOrientation(preRotationMatrix, (err: BusinessError, data: Array) => { + if (err.code != 0) { + console.error(`Failed to get orientation. Code: ${err.code}, message: ${err.message}`); + return; + } + console.info("Succeeded in getting data: " + JSON.stringify(data)); + }) + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to get orientation. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testGetOrientation end"); +} + +function testGetRotationMatrixSync() { + console.log("taihetest testGetRotationMatrixSync begin"); + try { + let rotationVector: float[] = [0.20046076, 0.21907, 0.73978853, 0.60376877]; + sensor.getRotationMatrix(rotationVector, (err: BusinessError, data: Array) => { + if (err.code != 0) { + console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); + return; + } + console.info("GetRotationMatrixSync succeeded in getting data: " + JSON.stringify(data)); + }) + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testGetRotationMatrixSync end"); +} + +function testGetSensorList() { + console.log("taihetest testGetSensorList begin"); + try { + sensor.getSensorList((err: BusinessError, data: Array) => { + if (err.code != 0) { + console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); + return; + } + console.info("GetSensorList succeeded in getting data: " + JSON.stringify(data)); + }); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testGetSensorList end"); +} + +function testOnAccelerometer() { + console.log("taihetest testOnAccelerometer begin"); + try { + let intervalData: long = 100000000; + let optionTest: sensor.Options = { + interval: intervalData + }; + sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse): void => { + console.info('Succeeded in invoking on accelerometer data: ' + JSON.stringify(data)); + }, optionTest); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to invoke onAccelerometer. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testOnAccelerometer end"); +} + +function testOnceAccelerometer() { + console.log("taihetest testOnceAccelerometer begin"); + try { + sensor.once(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse): void => { + console.info('Succeeded in invoking once accelerometer data: ' + JSON.stringify(data)); + }); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to invoke onceAccelerometer. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testOnceAccelerometer end"); +} + +function testOffAccelerometer() { + console.log("taihetest testOffAccelerometer begin"); + try { + sensor.off(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse): void => { + console.info('Succeeded in invoking off accelerometer data: ' + JSON.stringify(data)); + }); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to invoke offAccelerometer. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testOffAccelerometer end"); +} + +function testOnAmbientLight() { + console.log("taihetest testOnAmbientLight begin"); + try { + let intervalData: long = 100000000; + let optionTest: sensor.Options = { + interval: intervalData + }; + sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse): void => { + console.info('Succeeded in invoking on onAmbientLight data: ' + JSON.stringify(data)); + }, optionTest); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to invoke onAmbientLight. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testOnAmbientLight end"); +} + +function testOnceAmbientLight() { + console.log("taihetest testOnceAmbientLight begin"); + try { + sensor.once(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse): void => { + console.info('Succeeded in invoking once onAmbientLight data: ' + JSON.stringify(data)); + }); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to invoke onAmbientLight. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testOnceAmbientLight end"); +} + +function testOffAmbientLight() { + console.log("taihetest testOffAmbientLight begin"); + try { + sensor.off(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse): void => { + console.info('Succeeded in invoking off onAmbientLight data: ' + JSON.stringify(data)); + }); + } catch (error) { + let e: BusinessError = error as BusinessError; + console.error(`Failed to invoke onAmbientLight. Code: ${e.code}, message: ${e.message}`); + } + console.log("taihetest testOffAmbientLight end"); +} + +function main() { + testGetRotationMatrix(); + testGetOrientation(); + testGetRotationMatrixSync(); + testGetSensorList(); + testOnAccelerometer(); + testOnceAccelerometer(); + testOffAccelerometer(); + testOnAmbientLight(); + testOnceAmbientLight(); + testOffAmbientLight(); +} \ No newline at end of file -- Gitee