diff --git a/bundle.json b/bundle.json index 94ec4a333dba8c650786756e9a00c3e795f127a4..c07c2ce8f7d3e422683c701ff0aefea5bb329457 100755 --- a/bundle.json +++ b/bundle.json @@ -40,6 +40,7 @@ "fwk_group": [ "//base/sensors/sensor/frameworks/js/napi:sensor_js_target", "//base/sensors/sensor/frameworks/native:sensor_target", + "//base/sensors/sensor/frameworks/native:ohsensor", "//base/sensors/sensor/utils:sensor_utils_target" ], "service_group": [ @@ -57,10 +58,21 @@ ], "header_base": "//base/sensors/sensor/interfaces/inner_api" } + }, + { + "name": "//base/sensors/sensor/interfaces/kits/c:libsensor_ndk", + "header": { + "header_files": [ + "native_sensor_type.h", + "native_sensor.h" + ], + "header_base": "//base/sensors/sensor/interfaces/kits/c" + } } ], "test": [ "//base/sensors/sensor/test/unittest/interfaces/js:unittest", + "//base/sensors/sensor/test/unittest/interfaces/kits:unittest", "//base/sensors/sensor/test/fuzztest/interfaces:fuzztest", "//base/sensors/sensor/test/unittest/interfaces/inner_api:unittest", "//base/sensors/sensor/test/fuzztest/services:fuzztest" diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index d93289d1325219874dc1e21c516cdf03987bb915..f77a183b111ac68452b1015d01da6ac2c936637d 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -14,7 +14,7 @@ import("//build/ohos.gni") import("./../../sensor.gni") -ohos_shared_library("libsensor_native") { +ohos_shared_library("libsensor_client") { sources = [ "src/fd_listener.cpp", "src/sensor_agent_proxy.cpp", @@ -102,7 +102,7 @@ ohos_shared_library("sensor_interface_native") { } deps = [ - "$SUBSYSTEM_DIR/frameworks/native:libsensor_native", + "$SUBSYSTEM_DIR/frameworks/native:libsensor_client", "$SUBSYSTEM_DIR/frameworks/native:libsensor_ndk", ] @@ -119,9 +119,48 @@ ohos_shared_library("sensor_interface_native") { subsystem_name = "sensors" } +config("ohsensor_public_config") { + include_dirs = [ "$SUBSYSTEM_DIR/interfaces/kits/c" ] +} + +ohos_shared_library("ohsensor") { + sources = [ "src/native_sensor.cpp" ] + + defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] + + configs = [ ":sensor_private_config" ] + public_configs = [ ":ohsensor_public_config" ] + + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:libsensor_client", + "$SUBSYSTEM_DIR/frameworks/native:libsensor_ndk", + "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] + + relative_install_dir = "ndk" + part_name = "sensor" + subsystem_name = "sensors" +} + group("sensor_target") { deps = [ - ":libsensor_native", + ":libsensor_client", ":sensor_interface_native", ] } diff --git a/frameworks/native/include/native_sensor_impl.h b/frameworks/native/include/native_sensor_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..22c90acbe7d70028f8c405dcd2b1816d9d08ae86 --- /dev/null +++ b/frameworks/native/include/native_sensor_impl.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef NATIVE_SENSOR_IMPL +#define NATIVE_SENSOR_IMPL + +#include "oh_sensor.h" +#include "sensor_agent_type.h" + +struct Sensor_Info { + char sensorName[NAME_MAX_LEN]; /**< Sensor name */ + char vendorName[NAME_MAX_LEN]; /**< Sensor vendor */ + char firmwareVersion[VERSION_MAX_LEN]; /**< Sensor firmware version */ + char hardwareVersion[VERSION_MAX_LEN]; /**< Sensor hardware version */ + int32_t sensorTypeId = -1; /**< Sensor type ID */ + int32_t sensorId = -1; /**< Sensor ID */ + float maxRange = 0.0; /**< Maximum measurement range of the sensor */ + float precision = 0.0; /**< Sensor accuracy */ + float power = 0.0; /**< Sensor power */ + int64_t minSamplePeriod = -1; /**< Minimum sample period allowed, in ns */ + int64_t maxSamplePeriod = -1; /**< Maximum sample period allowed, in ns */ +}; + +struct Sensor_SubscriptionAttribute { + int64_t samplingInterval = -1; + int64_t reportInterval = -1; +}; + +struct Sensor_SubscriptionId { + int32_t sensorType = -1; +}; + +struct Sensor_Subscriber { + char name[NAME_MAX_LEN]; + Sensor_EventCallback callback; + UserData *userData = nullptr; +}; + +struct Sensor_Event { + int32_t sensorTypeId = -1; + int32_t version = -1; + int64_t timestamp = -1; + int32_t option = -1; + int32_t mode = -1; + uint8_t *data = nullptr; + uint32_t dataLen = 0; +}; +#endif // NATIVE_SENSOR_IMPL + diff --git a/frameworks/native/src/native_sensor.cpp b/frameworks/native/src/native_sensor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22d2572f4a6717256c128f7ae0bcffbb4ae0347a --- /dev/null +++ b/frameworks/native/src/native_sensor.cpp @@ -0,0 +1,363 @@ +/* + * Copyright (c) 2023 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 "oh_sensor.h" + +#include "i_sensor_service.h" +#include "native_sensor_impl.h" +#include "securec.h" +#include "sensor_agent.h" +#include "sensor_errors.h" + +using OHOS::HiviewDFX::HiLog; +using OHOS::HiviewDFX::HiLogLabel; +namespace { +const HiLogLabel LABEL = {LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorCapiAPI"}; +const uint32_t FLOAT_SIZE = 4; +} + +Sensor_Result OH_Sensor_GetInfos(Sensor_Info **sensors, uint32_t *count) +{ + if (count == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + SensorInfo *sensorInfo = nullptr; + int32_t sensorCount = -1; + int32_t ret = GetAllSensors(&sensorInfo, &sensorCount); + if (ret != SENSOR_SUCCESS || sensorCount < 0) { + SEN_HILOGE("GetAllSensors fail"); + return SENSOR_SERVICE_EXCEPTION; + } + if (sensors == nullptr) { + *count = static_cast(sensorCount); + SEN_HILOGD("Sensor count: %{public}d", *count); + return SENSOR_SUCCESS; + } + if (static_cast(sensorCount) != *count) { + SEN_HILOGE("Count:%{public}d is invalid, should be:%{public}d", *count, sensorCount); + return SENSOR_PARAMETER_ERROR; + } + for (int32_t i = 0; i < sensorCount; ++i) { + if (sensors[i] == nullptr) { + SEN_HILOGE("Sensor is null, i:%{public}d", i); + return SENSOR_PARAMETER_ERROR; + } + errno_t result = strcpy_s(sensors[i]->sensorName, NAME_MAX_LEN, sensorInfo[i].sensorName); + CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION); + result = strcpy_s(sensors[i]->vendorName, NAME_MAX_LEN, sensorInfo[i].vendorName); + CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION); + result = strcpy_s(sensors[i]->firmwareVersion, VERSION_MAX_LEN, sensorInfo[i].firmwareVersion); + CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION); + result = strcpy_s(sensors[i]->hardwareVersion, VERSION_MAX_LEN, sensorInfo[i].hardwareVersion); + CHKCR(result == EOK, SENSOR_SERVICE_EXCEPTION); + sensors[i]->sensorTypeId = sensorInfo[i].sensorTypeId; + sensors[i]->sensorId = sensorInfo[i].sensorId; + sensors[i]->maxRange = sensorInfo[i].maxRange; + sensors[i]->precision = sensorInfo[i].precision; + sensors[i]->power = sensorInfo[i].power; + sensors[i]->minSamplePeriod = sensorInfo[i].minSamplePeriod; + sensors[i]->maxSamplePeriod = sensorInfo[i].maxSamplePeriod; + } + return SENSOR_SUCCESS; +} + +Sensor_Info **OH_Sensor_Info_Create(uint32_t count) +{ + auto sensors = new Sensor_Info *[count]; + for (uint32_t i = 0; i < count; ++i) { + sensors[i] = new Sensor_Info(); + } + return sensors; +} + +int32_t OH_Sensor_Info_Destroy(Sensor_Info **sensors, uint32_t count) +{ + for (uint32_t i = 0; i < count; ++i) { + delete sensors[i]; + } + delete[] sensors; + sensors = nullptr; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Info_GetName(Sensor_Info* sensor, char *sensorName, uint32_t *length) +{ + if (sensor == nullptr || sensorName == nullptr || length == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + uint32_t nameLen = strlen(sensor->sensorName); + if (nameLen == 0 || *length <= nameLen) { + SEN_HILOGE("Parameter error, length:%{public}d is small, should big than:%{public}d", *length, nameLen); + return SENSOR_PARAMETER_ERROR; + } + errno_t result = strcpy_s(sensorName, *length, sensor->sensorName); + if (result != EOK) { + SEN_HILOGE("strcpy_s failed, result is %{public}d", result); + return SENSOR_SERVICE_EXCEPTION; + } + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Info_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length) +{ + if (sensor == nullptr || vendorName == nullptr || length == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + uint32_t nameLen = strlen(sensor->vendorName); + if (nameLen == 0 || *length <= nameLen) { + SEN_HILOGE("Parameter error, length:%{public}d is small, should big than:%{public}d", *length, nameLen); + return SENSOR_PARAMETER_ERROR; + } + errno_t result = strcpy_s(vendorName, *length, sensor->vendorName); + if (result != EOK) { + SEN_HILOGE("strcpy_s failed, result is %{public}d", result); + return SENSOR_SERVICE_EXCEPTION; + } + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Info_GetType(Sensor_Info* sensor, Sensor_Type *sensorType) +{ + if (sensor == nullptr || sensorType == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *sensorType = static_cast(sensor->sensorTypeId); + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Info_GetResolution(Sensor_Info* sensor, float *resolution) +{ + if (sensor == nullptr || resolution == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *resolution = sensor->precision; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Info_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplePeriod) +{ + if (sensor == nullptr || minSamplePeriod == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *minSamplePeriod = sensor->minSamplePeriod; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Info_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplePeriod) +{ + if (sensor == nullptr || maxSamplePeriod == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *maxSamplePeriod = sensor->maxSamplePeriod; + return SENSOR_SUCCESS; +} + +Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, + const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *user) +{ + if (id == nullptr || attribute == nullptr || user == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + const SensorUser *sensorUser = reinterpret_cast(user); + int32_t sensorType = id->sensorType; + int32_t ret = SubscribeSensor(sensorType, sensorUser); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SubscribeSensor failed, %{public}d", ret); + return SENSOR_SERVICE_EXCEPTION; + } + int64_t samplingInterval = attribute->samplingInterval; + ret = SetBatch(sensorType, sensorUser, samplingInterval, samplingInterval); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SetBatch failed, %{public}d", ret); + return SENSOR_SERVICE_EXCEPTION; + } + return static_cast(ActivateSensor(sensorType, sensorUser)); +} + +Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, + const Sensor_Subscriber *user) +{ + if (id == nullptr || user == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + const SensorUser *sensorUser = reinterpret_cast(user); + int32_t sensorType = id->sensorType; + int32_t ret = DeactivateSensor(sensorType, sensorUser); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SetBatch failed, %{public}d", ret); + return SENSOR_SERVICE_EXCEPTION; + } + return static_cast(UnsubscribeSensor(sensorType, sensorUser)); +} + +int32_t OH_Sensor_Event_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType) +{ + if (sensorEvent == nullptr || sensorType == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *sensorType = static_cast(sensorEvent->sensorTypeId); + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Event_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp) +{ + if (sensorEvent == nullptr || timestamp == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *timestamp = sensorEvent->timestamp; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Event_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy) +{ + if (sensorEvent == nullptr || accuracy == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *accuracy = static_cast(sensorEvent->option); + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Event_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length) +{ + if (sensorEvent == nullptr || data == nullptr || length == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *data = reinterpret_cast(sensorEvent->data); + *length = sensorEvent->dataLen / FLOAT_SIZE; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_SubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType) +{ + if (id == nullptr || sensorType == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *sensorType = static_cast(id->sensorType); + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_SubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType) +{ + if (id == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + id->sensorType = sensorType; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_SubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, + const int64_t samplingInterval) +{ + if (attribute == nullptr || samplingInterval < 0) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + attribute->samplingInterval = samplingInterval; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_SubscriptionAttribute_GetSamplingInterval(Sensor_SubscriptionAttribute* attribute, + int64_t *samplingInterval) +{ + if (attribute == nullptr || samplingInterval == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *samplingInterval = attribute->samplingInterval; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Subscriber_SetCallback(Sensor_Subscriber* user, const Sensor_EventCallback callback) +{ + if (user == nullptr || callback == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + user->callback = callback; + return SENSOR_SUCCESS; +} + +int32_t OH_Sensor_Subscriber_GetCallback(Sensor_Subscriber* user, Sensor_EventCallback *callback) +{ + if (user == nullptr || callback == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + *callback = user->callback; + return SENSOR_SUCCESS; +} + +Sensor_SubscriptionId *OH_Sensor_SubscriptionId_Create() +{ + return new (std::nothrow) Sensor_SubscriptionId(); +} + +int32_t OH_Sensor_SubscriptionId_Destroy(Sensor_SubscriptionId *id) +{ + if (id == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + delete id; + id = nullptr; + return SENSOR_SUCCESS; +} + +Sensor_SubscriptionAttribute *OH_Sensor_SubscriptionAttribute_Create() +{ + return new (std::nothrow) Sensor_SubscriptionAttribute(); +} + +int32_t OH_Sensor_SubscriptionAttribute_Destroy(Sensor_SubscriptionAttribute *attribute) +{ + if (attribute == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + delete attribute; + attribute = nullptr; + return SENSOR_SUCCESS; +} + +Sensor_Subscriber *OH_Sensor_Subscriber_Create() +{ + return new (std::nothrow) Sensor_Subscriber(); +} + +int32_t OH_Sensor_Subscriber_Destroy(Sensor_Subscriber *user) +{ + if (user == nullptr) { + SEN_HILOGE("Parameter error"); + return SENSOR_PARAMETER_ERROR; + } + delete user; + user = nullptr; + return SENSOR_SUCCESS; +} \ No newline at end of file diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3629fa7aa581de4f1c012a9b1bfe52cc6a48497a --- /dev/null +++ b/interfaces/kits/c/BUILD.gn @@ -0,0 +1,29 @@ +# Copyright (C) 2023 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/ohos.gni") +import("//build/ohos/ndk/ndk.gni") + +ohos_ndk_headers("sensor_ndk_header") { + dest_dir = "$ndk_headers_out_dir/sensors/sensor" + sources = [ + "./oh_sensor.h", + "./oh_sensor_type.h", + ] +} + +ohos_ndk_library("libsensor_ndk") { + ndk_description_file = "./libsensor.ndk.json" + output_name = "ohsensor" + min_compact_version = "11" +} diff --git a/interfaces/kits/c/libsensor.ndk.json b/interfaces/kits/c/libsensor.ndk.json new file mode 100644 index 0000000000000000000000000000000000000000..eb6d417d96b20cdb3325b0020846087dc1f0b67f --- /dev/null +++ b/interfaces/kits/c/libsensor.ndk.json @@ -0,0 +1,83 @@ +[ + { + "name": "OH_Sensor_GetInfos" + }, + { + "name": "OH_Sensor_Subscribe" + }, + { + "name": "OH_Sensor_Unsubscribe" + }, + { + "name": "OH_Sensor_Info_GetName" + }, + { + "name": "OH_Sensor_Info_GetVendorName" + }, + { + "name": "OH_Sensor_Info_GetType" + }, + { + "name": "OH_Sensor_Info_GetResolution" + }, + { + "name": "OH_Sensor_Info_GetMinSamplingInterval" + }, + { + "name": "OH_Sensor_Info_GetMaxSamplingInterval" + }, + { + "name": "OH_Sensor_Event_GetType" + }, + { + "name": "OH_Sensor_Event_GetTimestamp" + }, + { + "name": "OH_Sensor_Event_GetAccuracy" + }, + { + "name": "OH_Sensor_Event_GetData" + }, + { + "name": "OH_Sensor_SubscriptionId_GetType" + }, + { + "name": "OH_Sensor_SubscriptionId_SetType" + }, + { + "name": "OH_Sensor_SubscriptionAttribute_SetSamplingInterval" + }, + { + "name": "OH_Sensor_SubscriptionAttribute_GetSamplingInterval" + }, + { + "name": "OH_Sensor_Subscriber_SetCallback" + }, + { + "name": "OH_Sensor_Subscriber_GetCallback" + }, + { + "name": "OH_Sensor_Info_Create" + }, + { + "name": "OH_Sensor_Info_Destroy" + }, + { + "name": "OH_Sensor_SubscriptionId_Create" + }, + { + "name": "OH_Sensor_SubscriptionId_Destroy" + }, + { + "name": "OH_Sensor_SubscriptionAttribute_Create" + }, + { + "name": "OH_Sensor_SubscriptionAttribute_Destroy" + }, + { + "name": "OH_Sensor_Subscriber_Create" + }, + { + "name": "OH_Sensor_Subscriber_Destroy" + } +] \ No newline at end of file diff --git a/interfaces/kits/c/oh_sensor.h b/interfaces/kits/c/oh_sensor.h new file mode 100644 index 0000000000000000000000000000000000000000..ee0d78c6a152dc60654614c57ea7a9937da96efc --- /dev/null +++ b/interfaces/kits/c/oh_sensor.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2023 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. + */ + +/** + * @addtogroup Sensor + * @{ + * + * @brief Provides APIs to use common sensor features. For example, you can call the APIs to obtain sensor information + * and subscribe to or unsubscribe from sensor data. + * @since 11 + */ +/** + * @file oh_sensor.h + * + * @brief Declares the APIs for operating sensors, including obtaining sensor information and subscribing to or + * unsubscribing from sensor data. + * @library libohsensor.so + * @syscap SystemCapability.Sensors.Sensor + * @since 11 + */ + +#ifndef OH_SENSOR_H +#define OH_SENSOR_H + +#include "oh_sensor_type.h" + +#ifdef __cplusplus +extern "C" { +#endif +/** + * @brief Obtains information about all sensors on the device. + * + * @param infos - Double pointer to the information about all sensors on the device. + * For details, see {@link Sensor_Info}. + * @param count - Pointer to the number of sensors on the device. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * + * @since 11 + */ +Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count); + +/** + * @brief Subscribes to sensor data. The system will report sensor data to the subscriber at the specified frequency. + * If you need to apply for the ohos.permission.ACCELEROMETER permission when subscribing to the accelerometer sensor, + * you need to apply for the ohos.permission.GYROSCOPE permission when subscribing to the gyroscope sensor, and you need + * to apply for the ohos.permission.ACTIVITY_MOTION permission when subscribing to the pedometer related sensor. Apply + * for ohos.permission.READ_HEALTH_DATA permission when subscribing to health-related sensors, such as heart rate + * sensors, otherwise the subscription fails. Other sensors do not require permissions. + * + * @param id - Pointer to the sensor subscription ID. For details, see {@link Sensor_SubscriptionId}. + * @param attribute - Pointer to the subscription attribute, which is used to specify the data reporting frequency. + * For details, see {@link Sensor_SubscriptionAttribute}. + * @param subscriber - Pointer to the subscriber information, which is used to specify the callback function for + * reporting the sensor data. For details, see {@link Sensor_Subscriber}. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @permission ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or + * ohos.permission.ACTIVITY_MOTION or ohos.permission.READ_HEALTH_DATA + * @since 11 + */ +Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, + const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber); + +/** + * @brief Unsubscribes from sensor data. + * If you need to apply for the ohos.permission.ACCELEROMETER permission to unsubscribe from the accelerometer sensor, + * you need to request the ohos.permission.GYROSCOPE permission to unsubscribe from the gyroscope sensor, and you need + * to request the ohos.permission.ACTIVITY_MOTION permission to unsubscribe from the pedometer-related sensor. When you + * unsubscribe from health-related sensors, such as heart rate sensors, apply for ohos.permission.READ_HEALTH_DATA + * permissions, otherwise the subscription will fail. Other sensors do not require permissions. + * + * @param id - Pointer to the sensor subscription ID. For details, see {@link Sensor_SubscriptionId}. + * @param subscriber - Pointer to the subscriber information, which is used to specify the callback function for + * reporting the sensor data. For details, see {@link Sensor_Subscriber}. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @permission ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or + * ohos.permission.ACTIVITY_MOTION or ohos.permission.READ_HEALTH_DATA + * + * @since 11 + */ +Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber); +#ifdef __cplusplus +} +#endif +#endif // OH_SENSOR_H \ No newline at end of file diff --git a/interfaces/kits/c/oh_sensor_type.h b/interfaces/kits/c/oh_sensor_type.h new file mode 100644 index 0000000000000000000000000000000000000000..61ae622c063855249ea4d4079f30df73c3a269e1 --- /dev/null +++ b/interfaces/kits/c/oh_sensor_type.h @@ -0,0 +1,496 @@ +/* + * Copyright (c) 2023 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. + */ + +/** + * @addtogroup Sensor + * @{ + * + * @brief Provides APIs to define common sensor attributes. + * + * @since 11 + */ + +/** + * @file oh_sensor_type.h + * + * @brief Declares the common sensor attributes. + * @library libohsensor.so + * @syscap SystemCapability.Sensors.Sensor + * @since 11 + */ + +#ifndef OH_SENSOR_TYPE_H +#define OH_SENSOR_TYPE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the sensor types. + * + * @since 11 + */ +typedef enum Sensor_Type { + /** + * Acceleration sensor. + * @since 11 + */ + SENSOR_TYPE_ACCELEROMETER = 1, + /** + * Gyroscope sensor. + * @since 11 + */ + SENSOR_TYPE_GYROSCOPE = 2, + /** + * Ambient light sensor. + * @since 11 + */ + SENSOR_TYPE_AMBIENT_LIGHT = 5, + /** + * Magnetic field sensor. + * @since 11 + */ + SENSOR_TYPE_MAGNETIC_FIELD = 6, + /** + * Barometer sensor. + * @since 11 + */ + SENSOR_TYPE_BAROMETER = 8, + /** + * Hall effect sensor. + * @since 11 + */ + SENSOR_TYPE_HALL = 10, + /** + * Proximity sensor. + * @since 11 + */ + SENSOR_TYPE_PROXIMITY = 12, + /** + * Orientation sensor. + * @since 11 + */ + SENSOR_TYPE_ORIENTATION = 256, + /** + * Gravity sensor. + * @since 11 + */ + SENSOR_TYPE_GRAVITY = 257, + /** + * Rotation vector sensor. + * @since 11 + */ + SENSOR_TYPE_ROTATION_VECTOR = 259, + /** + * Pedometer detection sensor. + * @since 11 + */ + SENSOR_TYPE_PEDOMETER_DETECTION = 265, + /** + * Pedometer sensor. + * @since 11 + */ + SENSOR_TYPE_PEDOMETER = 266, + /** + * Heart rate sensor. + * @since 11 + */ + SENSOR_TYPE_HEART_RATE = 278, +} Sensor_Type; + +/** + * @brief Enumerates the sensor result codes. + * + * @since 11 + */ +typedef enum Sensor_Result { + /** + * The operation is successful. + * @since 11 + */ + SENSOR_SUCCESS = 0, + /** + * Permission verification failed. + * @since 11 + */ + SENSOR_PERMISSION_DENIED = 201, + /** + * Parameter check failed. For example, a mandatory parameter is not passed in, + * or the parameter type passed in is incorrect. + * @since 11 + */ + SENSOR_PARAMETER_ERROR = 401, + /** + * The sensor service is abnormal. + * @since 11 + */ + SENSOR_SERVICE_EXCEPTION = 14500101, +} Sensor_Result; + +/** + * @brief Enumerates the accuracy levels of data reported by a sensor. + * + * @since 11 + */ +typedef enum Sensor_Accuracy { + /** + * The sensor data is unreliable. It is possible that the sensor does not contact with the device to measure. + * @since 11 + */ + SENSOR_ACCURACY_UNRELIABLE = 0, + /** + * The sensor data is at a low accuracy level. The data must be calibrated based on + * the environment before being used. + * @since 11 + */ + SENSOR_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. + * @since 11 + */ + SENSOR_ACCURACY_MEDIUM = 2, + /** + * The sensor data is at a high accuracy level. The data can be used directly. + * @since 11 + */ + SENSOR_ACCURACY_HIGH = 3 +} Sensor_Accuracy; + +/** + * @brief Defines the sensor information. + * @since 11 + */ +typedef struct Sensor_Info Sensor_Info; + +/** + * @brief Creates an array of {@link Sensor_Info} instances with the given number. + * + * @param count - Number of {@link Sensor_Info} instances to create. + * @return Returns the double pointer to the array of {@link Sensor_Info} instances + * if the operation is successful; + * returns NULL otherwise. + * @since 11 + */ +Sensor_Info **OH_Sensor_Info_Create(uint32_t count); + +/** + * @brief Destroys an array of {@link Sensor_Info} instances and reclaims memory. + * + * @param sensors - Double pointer to the array of {@link Sensor_Info} instances. + * @param count - Number of {@link Sensor_Info} instances to destroy. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_Destroy(Sensor_Info **sensors, uint32_t count); + +/** + * @brief Obtains the sensor name. + * + * @param sensor - Pointer to the sensor information. + * @param sensorName - Pointer to the sensor name. + * @param length - Pointer to the length, in bytes. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_GetName(Sensor_Info* sensor, char *sensorName, uint32_t *length); + +/** + * @brief Obtains the sensor's vendor name. + * + * @param sensor - Pointer to the sensor information. + * @param vendorName - Pointer to the vendor name. + * @param length - Pointer to the length, in bytes. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length); + +/** + * @brief Obtains the sensor type. + * + * @param sensor - Pointer to the sensor information. + * @param sensorType - Pointer to the sensor type. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_GetType(Sensor_Info* sensor, Sensor_Type *sensorType); + +/** + * @brief Obtains the sensor resolution. + * + * @param sensor - Pointer to the sensor information. + * @param resolution - Pointer to the sensor resolution. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_GetResolution(Sensor_Info* sensor, float *resolution); + +/** + * @brief Obtains the minimum data reporting interval of a sensor. + * + * @param sensor - Pointer to the sensor information. + * @param minSamplingInterval - Pointer to the minimum data reporting interval, in nanoseconds. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval); + +/** + * @brief Obtains the maximum data reporting interval of a sensor. + * + * @param sensor - Pointer to the sensor information. + * @param maxSamplingInterval - Pointer to the maximum data reporting interval, in nanoseconds. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Info_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval); + +/** + * @brief Defines the sensor data information. + * @since 11 + */ +typedef struct Sensor_Event Sensor_Event; + +/** + * @brief Obtains the sensor type. + * + * @param Sensor_Event - Pointer to the sensor data information. + * @param sensorType - Pointer to the sensor type. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Event_GetType(Sensor_Event* Sensor_Event, Sensor_Type *sensorType); + +/** + * @brief Obtains the timestamp of sensor data. + * + * @param Sensor_Event - Pointer to the sensor data information. + * @param timestamp - Pointer to the timestamp. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Event_GetTimestamp(Sensor_Event* Sensor_Event, int64_t *timestamp); + +/** + * @brief Obtains the accuracy of sensor data. + * + * @param Sensor_Event - Pointer to the sensor data information. + * @param accuracy - Pointer to the accuracy. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Event_GetAccuracy(Sensor_Event* Sensor_Event, Sensor_Accuracy *accuracy); + +/** + * @brief Obtains sensor data. The data length and content depend on the sensor type. + * The format of the sensor data reported is as follows: + * SENSOR_TYPE_ACCELEROMETER: data[0], data[1], and data[2], indicating the acceleration around + * the x, y, and z axes of the device, respectively, in m/s2. + * SENSOR_TYPE_GYROSCOPE: data[0], data[1], and data[2], indicating the angular velocity of rotation around + * the x, y, and z axes of the device, respectively, in rad/s. + * SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux. + * SENSOR_TYPE_MAGNETIC_FIELD: data[0], data[1], and data[2], indicating the magnetic field strength around + * the x, y, and z axes of the device, respectively, in μT. + * SENSOR_TYPE_BAROMETER: data[0], indicating the atmospheric pressure, in hPa. + * SENSOR_TYPE_HALL: data[0], indicating the opening/closing state of the flip cover. The value 0 means that + * the flip cover is opened, and a value greater than 0 means that the flip cover is closed. + * SENSOR_TYPE_PROXIMITY: data[0], indicates the approaching state. The value 0 means the two objects are close + * to each other, and a value greater than 0 means that they are far away from each other. + * SENSOR_TYPE_ORIENTATION: data[0], data[1], and data[2], indicating the rotation angles of a device around + * the z, x, and y axes, respectively, in degree. + * SENSOR_TYPE_GRAVITY: data[0], data[1], and data[2], indicating the gravitational acceleration around + * the x, y, and z axes of a device, respectively, in m/s2. + * SENSOR_TYPE_ROTATION_VECTOR: data[0], data[1] and data[2], indicating the rotation angles of a device around + * the x, y, and z axes, respectively, in degree. data[3] indicates the rotation vector. + * SENSOR_TYPE_PEDOMETER_DETECTION: data[0], indicating the pedometer detection status. + * The value 1 means that the number of detected steps changes. + * SENSOR_TYPE_PEDOMETER: data[0], indicating the number of steps a user has walked. + * SENSOR_TYPE_HEART_RATE: data[0], indicating the heart rate value. + * + * @param Sensor_Event - Pointer to the sensor data information. + * @param data - Double pointer to the sensor data. + * @param length - Pointer to the array length. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Event_GetData(Sensor_Event* Sensor_Event, float **data, uint32_t *length); + +/** + * @brief Defines the sensor subscription ID, which uniquely identifies a sensor. + * @since 11 + */ +typedef struct Sensor_SubscriptionId Sensor_SubscriptionId; + +/** + * @brief Creates a {@link Sensor_SubscriptionId} instance. + * + * @return Returns the pointer to the {@link Sensor_SubscriptionId} instance if the operation is successful; + * returns NULL otherwise. + * @since 11 + */ +Sensor_SubscriptionId *OH_Sensor_SubscriptionId_Create(void); + +/** + * @brief Destroys a {@link Sensor_SubscriptionId} instance and reclaims memory. + * + * @param id - Pointer to the {@link Sensor_SubscriptionId} instance. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_SubscriptionId_Destroy(Sensor_SubscriptionId *id); + +/** + * @brief Obtains the sensor type. + * + * @param id - Pointer to the sensor subscription ID. + * @param id - Pointer to the sensor type. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_SubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType); + +/** + * @brief Sets the sensor type. + * + * @param id - Pointer to the sensor subscription ID. + * @param sensorType - Sensor type to set. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_SubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType); + +/** + * @brief Defines the sensor subscription attribute. + * @since 11 + */ +typedef struct Sensor_SubscriptionAttribute Sensor_SubscriptionAttribute; + +/** + * @brief Creates a {@link Sensor_SubscriptionAttribute} instance. + * + * @return Returns the pointer to the {@link Sensor_SubscriptionAttribute} instance if the operation is successful; + * returns NULL otherwise. + * @since 11 + */ +Sensor_SubscriptionAttribute *OH_Sensor_SubscriptionAttribute_Create(void); + +/** + * @brief Destroys a {@link Sensor_SubscriptionAttribute} instance and reclaims memory. + * + * @param attribute - Pointer to the {@link Sensor_SubscriptionAttribute} instance. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_SubscriptionAttribute_Destroy(Sensor_SubscriptionAttribute *attribute); + +/** + * @brief Sets the sensor data reporting interval. + * + * @param attribute - Pointer to the sensor subscription attribute. + * @param samplingInterval - Data reporting interval to set, in nanoseconds. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_SubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, + const int64_t samplingInterval); + +/** + * @brief Obtains the sensor data reporting interval. + * + * @param attribute - Pointer to the sensor subscription attribute. + * @param samplingInterval - Pointer to the data reporting interval, in nanoseconds. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_SubscriptionAttribute_GetSamplingInterval(Sensor_SubscriptionAttribute* attribute, + int64_t *samplingInterval); + +/** + * @brief Defines the callback function used to report sensor data. + * @since 11 + */ +typedef void (*Sensor_EventCallback)(Sensor_Event *event); + +/** + * @brief Defines the sensor subscriber information. + * @since 11 + */ +typedef struct Sensor_Subscriber Sensor_Subscriber; + +/** + * @brief Creates a {@link Sensor_Subscriber} instance. + * + * @return Returns the pointer to the {@link Sensor_Subscriber} instance + * if the operation is successful; returns NULL otherwise. + * @since 11 + */ +Sensor_Subscriber *OH_Sensor_Subscriber_Create(void); + +/** + * @brief Destroys a {@link Sensor_Subscriber} instance and reclaims memory. + * + * @param subscriber - Pointer to the {@link Sensor_Subscriber} instance. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Subscriber_Destroy(Sensor_Subscriber *subscriber); + +/** + * @brief Sets a callback function to report sensor data. + * + * @param subscriber - Pointer to the sensor subscriber information. + * @param callback - Callback function to set. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Subscriber_SetCallback(Sensor_Subscriber* subscriber, const Sensor_EventCallback callback); + +/** + * @brief Obtains the callback function used to report sensor data. + * + * @param subscriber - Pointer to the sensor subscriber information. + * @param callback - Pointer to the callback function. + * @return Returns SENSOR_SUCCESS if the operation is successful; + * returns an error code defined in {@link Sensor_Result} otherwise. + * @since 11 + */ +int32_t OH_Sensor_Subscriber_GetCallback(Sensor_Subscriber* subscriber, Sensor_EventCallback *callback); +#ifdef __cplusplus +} +#endif +#endif // OH_SENSOR_TYPE_H \ No newline at end of file diff --git a/test/unittest/interfaces/kits/BUILD.gn b/test/unittest/interfaces/kits/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4ac9e119ba56c9f1d06d28c05a4fc76b0b50fc1b --- /dev/null +++ b/test/unittest/interfaces/kits/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2023 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/test.gni") +import("./../../../../sensor.gni") + +ohos_unittest("SensorNativeTest") { + module_out_path = "sensor/interfaces" + + sources = + [ "$SUBSYSTEM_DIR/test/unittest/interfaces/kits/sensor_native_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/frameworks/native/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:ohsensor", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] +} +group("unittest") { + testonly = true + deps = [ ":SensorNativeTest" ] +} diff --git a/test/unittest/interfaces/kits/sensor_native_test.cpp b/test/unittest/interfaces/kits/sensor_native_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..824c6b5ca06de8022ad86571d0bb17be0ea5eccd --- /dev/null +++ b/test/unittest/interfaces/kits/sensor_native_test.cpp @@ -0,0 +1,417 @@ +/* + * Copyright (c) 2023 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 +#include +#include +#include + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "oh_sensor.h" +#include "sensor_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorAgentTest" }; +constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_ACCELEROMETER }; +constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64; +constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000; +constexpr int32_t SLEEP_TIME_MS = 1000; +constexpr int64_t INVALID_VALUE = -1; +constexpr float INVALID_RESOLUTION = -1.0F; + +PermissionDef g_infoManagerTestPermDef = { + .permissionName = "ohos.permission.ACCELEROMETER", + .bundleName = "accesstoken_test", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "test sensor agent", + .descriptionId = 1 +}; + +PermissionStateFull g_infoManagerTestState = { + .permissionName = "ohos.permission.ACCELEROMETER", + .isGeneral = true, + .resDeviceID = {"local"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1} +}; + +HapPolicyParams g_infoManagerTestPolicyPrams = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {g_infoManagerTestPermDef}, + .permStateList = {g_infoManagerTestState} +}; + +HapInfoParams g_infoManagerTestInfoParms = { + .userID = 1, + .bundleName = "sensoragent_test", + .instIndex = 0, + .appIDDesc = "sensorAgentTest" +}; + +Sensor_Subscriber *g_user = nullptr; +} // namespace + +class SensorAgentTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +private: + static AccessTokenID tokenID_; +}; + +AccessTokenID SensorAgentTest::tokenID_ = 0; + +void SensorAgentTest::SetUpTestCase() +{ + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams); + tokenID_ = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(0, tokenID_); + ASSERT_EQ(0, SetSelfTokenID(tokenID_)); +} + +void SensorAgentTest::TearDownTestCase() +{ + int32_t ret = AccessTokenKit::DeleteToken(tokenID_); + if (tokenID_ != 0) { + ASSERT_EQ(RET_SUCCESS, ret); + } +} + +void SensorAgentTest::SetUp() +{} + +void SensorAgentTest::TearDown() +{} + +void SensorDataCallbackImpl(Sensor_Event *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + int64_t timestamp = INVALID_VALUE; + int32_t ret = OH_Sensor_Event_GetTimestamp(event, ×tamp); + ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_Type sensorType; + ret = OH_Sensor_Event_GetType(event, &sensorType); + ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE; + ret = OH_Sensor_Event_GetAccuracy(event, &accuracy); + ASSERT_EQ(ret, SENSOR_SUCCESS); + float *data = nullptr; + uint32_t length = 0; + ret = OH_Sensor_Event_GetData(event, &data, &length); + ASSERT_EQ(ret, SENSOR_SUCCESS); + SEN_HILOGI("sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d" + "x:%{public}f, y:%{public}f, z:%{public}f", sensorType, length, accuracy, + data[0], data[1], data[2]); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_GetInfos_001 in"); + uint32_t count = 0; + int32_t ret = OH_Sensor_GetInfos(nullptr, &count); + ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_Info **sensors = OH_Sensor_Info_Create(count); + ASSERT_NE(sensors, nullptr); + ret = OH_Sensor_GetInfos(sensors, &count); + ASSERT_EQ(ret, SENSOR_SUCCESS); + for (uint32_t i = 0; i < count; ++i) { + char sensorName[SENSOR_NAME_LENGTH_MAX] = {}; + uint32_t length = SENSOR_NAME_LENGTH_MAX; + ret = OH_Sensor_Info_GetName(sensors[i], sensorName, &length); + ASSERT_EQ(ret, SENSOR_SUCCESS); + char vendorName[SENSOR_NAME_LENGTH_MAX] = {}; + length = SENSOR_NAME_LENGTH_MAX; + ret = OH_Sensor_Info_GetVendorName(sensors[i], vendorName, &length); + ASSERT_EQ(ret, SENSOR_SUCCESS); + Sensor_Type sensorType; + ret = OH_Sensor_Info_GetType(sensors[i], &sensorType); + ASSERT_EQ(ret, SENSOR_SUCCESS); + float resolution = INVALID_RESOLUTION; + ret = OH_Sensor_Info_GetResolution(sensors[i], &resolution); + ASSERT_EQ(ret, SENSOR_SUCCESS); + int64_t minSamplePeriod = INVALID_VALUE; + ret = OH_Sensor_Info_GetMinSamplingInterval(sensors[i], &minSamplePeriod); + ASSERT_EQ(ret, SENSOR_SUCCESS); + int64_t maxSamplePeriod = INVALID_VALUE; + ret = OH_Sensor_Info_GetMaxSamplingInterval(sensors[i], &maxSamplePeriod); + ASSERT_EQ(ret, SENSOR_SUCCESS); + SEN_HILOGI("sensorType:%{public}d, sensorName:%{public}s, vendorName:%{public}s," + "resolution:%{public}f, minSamplePeriod:%{public}" PRId64 "maxSamplePeriod:%{public}" PRId64, + static_cast(sensorType), sensorName, vendorName, + resolution, minSamplePeriod, maxSamplePeriod); + } + ret = OH_Sensor_Info_Destroy(sensors, count); + ASSERT_EQ(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_GetInfos_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_GetInfos_003 in"); + Sensor_Info *sensors { nullptr }; + int32_t ret = OH_Sensor_GetInfos(&sensors, nullptr); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscribe_001 in"); + g_user = OH_Sensor_Subscriber_Create(); + int32_t ret = OH_Sensor_Subscriber_SetCallback(g_user, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + Sensor_SubscriptionId *id = OH_Sensor_SubscriptionId_Create(); + ret = OH_Sensor_SubscriptionId_SetType(id, SENSOR_ID); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + Sensor_SubscriptionAttribute *attr = OH_Sensor_SubscriptionAttribute_Create(); + ret = OH_Sensor_SubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + ret = OH_Sensor_Subscribe(id, attr, g_user); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); + ret = OH_Sensor_Unsubscribe(id, g_user); + ASSERT_EQ(ret, SENSOR_SUCCESS); + if (id != nullptr) { + OH_Sensor_SubscriptionId_Destroy(id); + } + if (attr != nullptr) { + OH_Sensor_SubscriptionAttribute_Destroy(attr); + } + if (g_user != nullptr) { + OH_Sensor_Subscriber_Destroy(g_user); + g_user = nullptr; + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscribe_002 in"); + Sensor_SubscriptionId *id = OH_Sensor_SubscriptionId_Create(); + int32_t ret = OH_Sensor_SubscriptionId_SetType(id, SENSOR_ID); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + Sensor_SubscriptionAttribute *attr = OH_Sensor_SubscriptionAttribute_Create(); + ret = OH_Sensor_SubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + ret = OH_Sensor_Subscribe(id, attr, nullptr); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (id != nullptr) { + OH_Sensor_SubscriptionId_Destroy(id); + } + if (attr != nullptr) { + OH_Sensor_SubscriptionAttribute_Destroy(attr); + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscribe_003 in"); + g_user = OH_Sensor_Subscriber_Create(); + int32_t ret = OH_Sensor_Subscriber_SetCallback(g_user, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + Sensor_SubscriptionAttribute *attr = OH_Sensor_SubscriptionAttribute_Create(); + ret = OH_Sensor_SubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + ret = OH_Sensor_Subscribe(nullptr, attr, g_user); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (attr != nullptr) { + OH_Sensor_SubscriptionAttribute_Destroy(attr); + } + if (g_user != nullptr) { + OH_Sensor_Subscriber_Destroy(g_user); + g_user = nullptr; + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscribe_004, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscribe_004 in"); + g_user = OH_Sensor_Subscriber_Create(); + int32_t ret = OH_Sensor_Subscriber_SetCallback(g_user, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + Sensor_SubscriptionId *id = OH_Sensor_SubscriptionId_Create(); + ret = OH_Sensor_SubscriptionId_SetType(id, SENSOR_ID); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + ret = OH_Sensor_Subscribe(id, nullptr, g_user); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (id != nullptr) { + OH_Sensor_SubscriptionId_Destroy(id); + } + if (g_user != nullptr) { + OH_Sensor_Subscriber_Destroy(g_user); + g_user = nullptr; + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Unsubscribe_001 in"); + g_user = OH_Sensor_Subscriber_Create(); + int32_t ret = OH_Sensor_Subscriber_SetCallback(g_user, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + ret = OH_Sensor_Unsubscribe(nullptr, g_user); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (g_user != nullptr) { + OH_Sensor_Subscriber_Destroy(g_user); + g_user = nullptr; + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Unsubscribe_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Unsubscribe_002 in"); + Sensor_SubscriptionId *id = OH_Sensor_SubscriptionId_Create(); + int32_t ret = OH_Sensor_SubscriptionId_SetType(id, SENSOR_ID); + ASSERT_EQ(ret, SENSOR_SUCCESS); + + ret = OH_Sensor_Unsubscribe(id, nullptr); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (id != nullptr) { + OH_Sensor_SubscriptionId_Destroy(id); + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionId_SetType_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionId_SetType_001 in"); + int32_t ret = OH_Sensor_SubscriptionId_SetType(nullptr, SENSOR_ID); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionId_GetType_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionId_GetType_001 in"); + Sensor_Type type; + int32_t ret = OH_Sensor_SubscriptionId_GetType(nullptr, &type); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionId_GetType_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionId_GetType_002 in"); + Sensor_SubscriptionId *id = OH_Sensor_SubscriptionId_Create(); + int32_t ret = OH_Sensor_SubscriptionId_GetType(id, nullptr); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (id != nullptr) { + OH_Sensor_SubscriptionId_Destroy(id); + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionAttribute_SetSamplingInterval_001, + TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionAttribute_SetSamplingInterval_001 in"); + int32_t ret = OH_Sensor_SubscriptionAttribute_SetSamplingInterval(nullptr, + SENSOR_SAMPLE_PERIOD); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionAttribute_SetSamplingInterval_002, + TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionAttribute_SetSamplingInterval_002 in"); + Sensor_SubscriptionAttribute *attr = OH_Sensor_SubscriptionAttribute_Create(); + int32_t ret = OH_Sensor_SubscriptionAttribute_SetSamplingInterval(attr, INVALID_VALUE); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (attr != nullptr) { + OH_Sensor_SubscriptionAttribute_Destroy(attr); + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionAttribute_GetSamplingInterval_001, + TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionAttribute_GetSamplingInterval_001 in"); + int64_t samplingInterval = 0; + int32_t ret = OH_Sensor_SubscriptionAttribute_GetSamplingInterval(nullptr, + &samplingInterval); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_SubscriptionAttribute_GetSamplingInterval_002, + TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscriptionAttribute_GetSamplingInterval_002 in"); + Sensor_SubscriptionAttribute *attr = OH_Sensor_SubscriptionAttribute_Create(); + int32_t ret = OH_Sensor_SubscriptionAttribute_GetSamplingInterval(attr, nullptr); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (attr != nullptr) { + OH_Sensor_SubscriptionAttribute_Destroy(attr); + } +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscriber_SetCallback_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscriber_SetCallback_001 in"); + int32_t ret = OH_Sensor_Subscriber_SetCallback(nullptr, SensorDataCallbackImpl); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscriber_SetCallback_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscriber_SetCallback_002 in"); + g_user = OH_Sensor_Subscriber_Create(); + int32_t ret = OH_Sensor_Subscriber_SetCallback(g_user, nullptr); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscriber_GetCallback_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscriber_GetCallback_001 in"); + Sensor_EventCallback callback; + int32_t ret = OH_Sensor_Subscriber_GetCallback(nullptr, &callback); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); +} + +HWTEST_F(SensorAgentTest, OH_Sensor_Subscriber_GetCallback_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_Subscriber_GetCallback_002 in"); + g_user = OH_Sensor_Subscriber_Create(); + int32_t ret = OH_Sensor_Subscriber_GetCallback(g_user, nullptr); + ASSERT_EQ(ret, SENSOR_PARAMETER_ERROR); + if (g_user != nullptr) { + OH_Sensor_Subscriber_Destroy(g_user); + } +} +} // namespace Sensors +} // namespace OHOS