From de8e68b9ecc2abb0661ab0e88bb84e48ffe94dab Mon Sep 17 00:00:00 2001 From: cff-gite Date: Mon, 20 Nov 2023 03:14:18 +0000 Subject: [PATCH] Add sensor_capi Signed-off-by: cff-gite Change-Id: I229ac36745f8f5b61b7c3d0e3db7a0475d040c52 --- bundle.json | 12 + frameworks/capi/BUILD.gn | 67 +++ frameworks/capi/sensor_capi.cpp | 106 ++++ frameworks/kits/c/BUILD.gn | 36 ++ frameworks/kits/c/libsensor_capi.ndk.json | 11 + frameworks/kits/c/sensor_capi.h | 89 ++++ frameworks/kits/c/sensor_capi_base.h | 502 ++++++++++++++++++ test/unittest/interfaces/capi/BUILD.gn | 51 ++ .../interfaces/capi/sensor_capi_test.cpp | 235 ++++++++ 9 files changed, 1109 insertions(+) mode change 100755 => 100644 bundle.json create mode 100644 frameworks/capi/BUILD.gn create mode 100644 frameworks/capi/sensor_capi.cpp create mode 100644 frameworks/kits/c/BUILD.gn create mode 100644 frameworks/kits/c/libsensor_capi.ndk.json create mode 100644 frameworks/kits/c/sensor_capi.h create mode 100644 frameworks/kits/c/sensor_capi_base.h create mode 100644 test/unittest/interfaces/capi/BUILD.gn create mode 100644 test/unittest/interfaces/capi/sensor_capi_test.cpp diff --git a/bundle.json b/bundle.json old mode 100755 new mode 100644 index 94ec4a33..b7a41c04 --- a/bundle.json +++ b/bundle.json @@ -39,6 +39,7 @@ "base_group": [], "fwk_group": [ "//base/sensors/sensor/frameworks/js/napi:sensor_js_target", + "//base/sensors/sensor/frameworks/capi:sensor_capi_target", "//base/sensors/sensor/frameworks/native:sensor_target", "//base/sensors/sensor/utils:sensor_utils_target" ], @@ -57,11 +58,22 @@ ], "header_base": "//base/sensors/sensor/interfaces/inner_api" } + }, + { + "name": "//base/sensors/sensor/frameworks/capi:sensor_capi_target", + "header": { + "header_files": [ + "sensor_capi_base.h", + "sensor_capi.h" + ], + "header_base": "//base/sensors/sensor/frameworks/kits/c" + } } ], "test": [ "//base/sensors/sensor/test/unittest/interfaces/js:unittest", "//base/sensors/sensor/test/fuzztest/interfaces:fuzztest", + "//base/sensors/sensor/test/unittest/interfaces/capi:unittest", "//base/sensors/sensor/test/unittest/interfaces/inner_api:unittest", "//base/sensors/sensor/test/fuzztest/services:fuzztest" ] diff --git a/frameworks/capi/BUILD.gn b/frameworks/capi/BUILD.gn new file mode 100644 index 00000000..0acad004 --- /dev/null +++ b/frameworks/capi/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (c) 2021-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("./../../sensor.gni") + +config("sensor_private_config") { + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/interfaces/native/inner_api", + "$SUBSYSTEM_DIR/frameworks/kits/c", + "$SUBSYSTEM_DIR/utils/ipc/include", + "$SUBSYSTEM_DIR/utils/common/include", + ] +} + +config("sensor_public_config") { + include_dirs = [ + "include", + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/frameworks/native/inner_api", + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] +} + +ohos_shared_library("sensor_capi_ndk") { + sources = [ "sensor_capi.cpp" ] + + defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] + + configs = [ ":sensor_private_config" ] + public_configs = [ ":sensor_public_config" ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:libsensor_native", + "$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_capi_target") { + deps = [ ":sensor_capi_ndk" ] +} diff --git a/frameworks/capi/sensor_capi.cpp b/frameworks/capi/sensor_capi.cpp new file mode 100644 index 00000000..6840ced4 --- /dev/null +++ b/frameworks/capi/sensor_capi.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021-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 "sensor_capi.h" + +#include "sensor_agent.h" +#include "i_sensor_service.h" +#include "sensor.h" +#include "sensor_service_client.h" +#include "sensor_errors.h" + +using OHOS::HiviewDFX::HiLog; +using OHOS::HiviewDFX::HiLogLabel; +static const HiLogLabel LABEL = {LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorCapiAPI"}; + +Sensor_Result OH_Sensor_GetAllSensors(Sensor_SensorInfo **sensorMessage, int32_t *count) +{ + SensorInfo **sensorInfo = reinterpret_cast(const_cast(sensorMessage)); + if (count == nullptr) { + SEN_HILOGD("Get count fail"); + } + Sensor_Result ret = static_cast(GetAllSensors(sensorInfo, count)); + if (count == nullptr) { + SEN_HILOGD("Get count fail"); + } + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("Get sensor list failed, ret is %{public}d", ret); + } + return ret; +} + +bool CheckSensorSubscribeId(Sensor_SubscribeId subscribeId) +{ + int32_t count = -1; + Sensor_SensorInfo *sensorInfo = nullptr; + int32_t ret = OH_Sensor_GetAllSensors(&sensorInfo, &count); + SEN_HILOGD("Get sensor sensorInfo %{public}s, %{public}d, %{public}d", sensorInfo->sensorName, + sensorInfo->sensorTypeId, sensorInfo->sensorId); + SEN_HILOGD("Get sensor sensorInfo%{public}d", count); + if (ret != 0) { + return false; + } + for (int32_t i = 0; i < count; i++) + { + if ((sensorInfo + i)->sensorTypeId == subscribeId.sensorTypeId) { + SEN_HILOGD("Get sensor sensorTypeId:%{public}d", subscribeId.sensorTypeId); + return true; + } + } + return false; +} + +Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SubscribeId subscribeId, Sensor_SubscribeAttribute attribute, + const Sensor_SubscribeUser *cb) +{ + if (!CheckSensorSubscribeId(subscribeId) || attribute.samplingInterval < 0 || attribute.reportInterval < 0) { + SEN_HILOGE("Sensor attribute value is invalid"); + return SENSOR_PARAMETER_ERROR; + } + SEN_HILOGD("SubscribeSensor in"); + SensorUser *user = reinterpret_cast(const_cast(cb)); + Sensor_Result ret = static_cast(SubscribeSensor(subscribeId.sensorTypeId, user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SubscribeSensor failed, ret is %{public}d", ret); + } + ret = static_cast(SetBatch(subscribeId.sensorTypeId, user, + attribute.samplingInterval, attribute.reportInterval)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SetBatch failed, ret is %{public}d", ret); + } + ret = static_cast(ActivateSensor(subscribeId.sensorTypeId, user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("ActivateSensor failed, ret is %{public}d", ret); + } + return ret; +} + +Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SubscribeId subscribeId, const Sensor_SubscribeUser *cb) +{ + if (!CheckSensorSubscribeId(subscribeId)) { + SEN_HILOGE("subscribeId is invalid"); + return SENSOR_PARAMETER_ERROR; + } + SensorUser *user = reinterpret_cast(const_cast(cb)); + Sensor_Result ret = static_cast(DeactivateSensor(subscribeId.sensorTypeId, user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("DeactivateSensor failed, ret is %{public}d", ret); + } + ret = static_cast(UnsubscribeSensor(subscribeId.sensorTypeId, user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("UnsubscribeSensor failed, ret is %{public}d", ret); + } + return ret; +} diff --git a/frameworks/kits/c/BUILD.gn b/frameworks/kits/c/BUILD.gn new file mode 100644 index 00000000..b75e76e5 --- /dev/null +++ b/frameworks/kits/c/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (C) 2022 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") +import("./../../../sensor.gni") + +ohos_ndk_headers("sensor_capi_header") { + dest_dir = "$ndk_headers_out_dir/sensor_capi" + sources = [ + "$SUBSYSTEM_DIR/interfaces/inner_api", + "./sensor_capi.h", + "./sensor_capi_base.h", + ] +} + +ohos_ndk_library("libsensor_capi_ndk") { + ndk_description_file = "./libsensor_capi.ndk.json" + output_name = "sensor_capi_ndk" + min_compact_version = "10" + system_capability = "SystemCapability.Sensors.Sensor" + system_capability_headers = [ + "$ndk_headers_out_dir/sensor/sensor_capi_base.h", + "$ndk_headers_out_dir/sensor/sensor_capi.h", + ] +} diff --git a/frameworks/kits/c/libsensor_capi.ndk.json b/frameworks/kits/c/libsensor_capi.ndk.json new file mode 100644 index 00000000..dc53b9d0 --- /dev/null +++ b/frameworks/kits/c/libsensor_capi.ndk.json @@ -0,0 +1,11 @@ +[ + { + "name": "OH_Sensor_GetAllSensors" + }, + { + "name": "OH_Sensor_SubscribeSensor" + }, + { + "name": "OH_Sensor_UnsubscribeSensor" + } +] \ No newline at end of file diff --git a/frameworks/kits/c/sensor_capi.h b/frameworks/kits/c/sensor_capi.h new file mode 100644 index 00000000..db1eb3d7 --- /dev/null +++ b/frameworks/kits/c/sensor_capi.h @@ -0,0 +1,89 @@ +/* + * 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 传感器 + * @{ + * + * @brief 为您提供标准的开放api,以使用传感器的常用功能。 + * + * 例如,您可以调用这些api来获取传感器属性信息、订阅或取消订阅传感器数据等。 + * @since 11 + */ + +/** + * @file sensor_capi.h + * + * @brief 声明操作传感器的常用api,用于获取传感器信息、订阅或取消订阅传感器数据等。 + * @library 引用sensor_capi_base.h,链接生成libsensor_capi_ndk.so。 + * @syscap SystemCapability.Sensors.Sensor + * @since 11 + */ + +#ifndef SENSOR_CAPI_H +#define SENSOR_CAPI_H + +#include + +#include "sensor_capi_base.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief 获取设备上的所有传感器信息。 + * + * @param sensorInfo 设备上的所有传感器信息, + * 详情请参见{@Link Sensor_SensorInfo}。 + * @param count 设备上的传感器数量。 + * @return 获取传感器信息成功返回SENSOR_SUCCESS,否则返回对应的错误码, + * 详情请参见{@Link Sensor_Result}。 + * + * @since 11 + */ +Sensor_Result OH_Sensor_GetAllSensors(Sensor_SensorInfo **sensorMessage, int32_t *count); + +/** + * @brief 订阅传感器数据,系统将按照用户指定的上报频率向用户报告传感器数据。 + * + * @param id 传感器类型, 详情请参见{@link Sensor_SubscribeId}。 + * @param attribute 订阅属性,用于指定传感器的上报频率,详情请参见{@Link Sensor_SubscribeAttribute}。 + * @param user 订阅者信息,指定传感器数据回调函数,详情请参见{@Link Sensor_SubscribeUser}。 + * @return 订阅传感器成功返回SENSOR_SUCCESS,否则返回对应的错误码, + * 详情请参见{@Link Sensor_Result}。 + * + * @since 11 + */ +Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SubscribeId subscribeId, Sensor_SubscribeAttribute attribute, + const Sensor_SubscribeUser *cb); + +/** + * @brief 取消订阅传感器数据。 + * + * @param id 传感器类型, 详情请参见{@link Sensor_SubscribeId}。 + * @param user 订阅者信息,指定传感器数据回调函数, + * 详情请参见{@Link Sensor_SubscribeUser}。 + * @return 取消订阅传感器成功返回SENSOR_SUCCESS,否则返回对应的错误码, + * 详情请参见{@Link Sensor_Result}。 + * + * @since 11 + */ +Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SubscribeId subscribeId, const Sensor_SubscribeUser *cb); + +#ifdef __cplusplus +} +#endif +#endif // SENSOR_CAPI_H \ No newline at end of file diff --git a/frameworks/kits/c/sensor_capi_base.h b/frameworks/kits/c/sensor_capi_base.h new file mode 100644 index 00000000..4d6dd8fb --- /dev/null +++ b/frameworks/kits/c/sensor_capi_base.h @@ -0,0 +1,502 @@ +/* + * 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 传感器 + * @{ + * + * @brief 为您提供标准的开放api,定义常用传感器属性。 + * + * @since 11 + */ + +/** + * @file sensor_capi_base.h + * + * @brief 定义常用传感器属性。 + * @library 需要链接生成libsensor_capi_ndk.so。 + * @syscap SystemCapability.Sensors.Sensor + * + * @since 11 + */ +#ifndef SENSOR_CAPI_BASE_H +#define SENSOR_CAPI_BASE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif +/** 传感器名称的最大长度 */ +#ifndef NAME_MAX_LEN +#define NAME_MAX_LEN 128 +#endif /* NAME_MAX_LEN */ +/** 传感器数据大小 */ +#ifndef SENSOR_USER_DATA_SIZE +#define SENSOR_USER_DATA_SIZE 104 +#endif /* SENSOR_USER_DATA_SIZE */ +/** 传感器版本的最大长度 */ +#ifndef VERSION_MAX_LEN +#define VERSION_MAX_LEN 16 +#endif /* SENSOR_USER_DATA_SIZE */ +/** 分布式组网最大长度 */ +#ifndef NETWORK_ID_MAX_LEN +#define NETWORK_ID_MAX_LEN 96 +#endif /* NETWORK_ID_MAX_LEN */ +/** + * @brief 传感器类型。 + * + * @since 11 + */ +typedef enum Sensor_SensorTypeId { + INVALID = -1, + ACCELEROMETER = 1, /**< 加速度传感器 */ + GYROSCOPE = 2, /**< 陀螺仪传感测器 */ + AMBIENT_LIGHT = 5, /**< 环境光传感器 */ + MAGNETIC_FIELD = 6, /**< 地磁传感器 */ + BAROMETER = 8, /**< 气压计传感器 */ + HALL = 10, /**< 霍尔传感器 */ + PROXIMITY = 12, /**< 接近光传感器 */ + HUMIDITY = 13, /**< 湿度传感器 */ + COLOR = 14, /**< 颜色传感器 */ + SAR = 15, /**< 吸收比率传感器 */ + ORIENTATION = 256, /**< 方向传感器 */ + GRAVITY = 257, /**< 重力传感器 */ + LINEAR_ACCELEROMETER = 258, /**< 线性加速度传感器 */ + ROTATION_VECTOR = 259, /**< 旋转矢量传感器 */ + AMBIENT_TEMPERATURE = 260, /**< 温度传感器 */ + MAGNETIC_FIELD_UNCALIBRATED = 261, /**< 未校准地磁传感器 */ + GYROSCOPE_UNCALIBRATED = 263, /**< 未校准陀螺仪传感器 */ + SIGNIFICANT_MOTION = 264, /**< 大幅动作检测传感器 */ + PEDOMETER_DETECTION = 265, /**< 计步器检测传感器 */ + PEDOMETER = 266, /**< 计步器传感器 */ + HEART_RATE = 278, /**< 心率传感器 */ + WEAR_DETECTION = 280, /**< 佩戴检测传感器 */ + ACCELEROMETER_UNCALIBRATED = 281, /**< 未校准加速度传感器 */ +} Sensor_SensorTypeId; + +/** + * @brief 定义传感器操作错误码。 + * + * @since 11 + */ +typedef enum Sensor_Result { + SENSOR_SUCCESS = 0, /**< 操作成功时返回该值 */ + SENSOR_PERMISSION_DENIED = 201, /**< 当权限被拒绝时使用此错误代码 */ + SENSOR_PARAMETER_ERROR = 401, /**< 当输入参数类型或范围不匹配时使用此错误代码 */ + SENSOR_SERVICE_EXCEPTION = 14500101, /**< 当服务异常时使用此错误代码 */ +} Sensor_Result; + +/** + * @brief 传感器属性信息。 + * + * @since 11 + */ +typedef struct Sensor_SensorInfo { + char sensorName[NAME_MAX_LEN]; /**< 传感器名字 */ + char vendorName[NAME_MAX_LEN]; /**< 传感器供应商 */ + char firmwareVersion[VERSION_MAX_LEN]; /**< 传感器固件版本 */ + char hardwareVersion[VERSION_MAX_LEN]; /**< 传感器硬件版本 */ + int32_t sensorTypeId; /**< 传感器类型 ID */ + int32_t sensorId; /**< 传感器 ID */ + int32_t index; /**< 传感器编号,表示同类传感器的第几个器件,从0开始,默认是0, 0表示默认器件(主传感器)*/ + float maxRange; /**< 传感器的最大测量范围 */ + float precision; /**< 传感器精度 */ + float power; /**< 传感器功率 */ + int64_t minSamplePeriod; /**< 允许的最小采样周期,以纳秒为单位 */ + int64_t maxSamplePeriod; /**< 允许的最大采样周期,以纳秒为单位 */ +} Sensor_SensorInfo; + +/** + * @brief 列举传感器的数据报告模式。 + * + * @since 11 + */ +typedef enum Sensor_SensorMode { + DEFAULT_MODE = 0, /**< 默认数据报告模式 */ + REALTIME_MODE = 1, /**< 实时数据上报模式,每次上报一组数据 */ + ON_CHANGE = 2, /**< 数据实时上报模式,在状态发生变化时上报数据 */ + ONE_SHOT = 3, /**< 数据实时上报模式,只上报一次数据 */ + FIFO_MODE = 4, /**< 基于fifo的数据上报模式,根据BatchCnt设置上报数据 */ + MODE_MAX2, /**< 最大传感器数据上报模式 */ +} Sensor_SensorMode; + +/** + * @brief 上报的传感器数据信息。 + * + * @since 11 + */ +typedef struct Sensor_SensorEvent { + int32_t sensorTypeId = -1; /**< 传感器类型 ID */ + int32_t version = -1; /**< 传感器算法版本 */ + int64_t timestamp = -1; /**< 报告传感器数据的时间 */ + int32_t option = -1; /**< 传感器数据选项,包括测量范围和精度 */ + int32_t mode = -1; /**< 传感器数据上报方式 (详细参考{@link Sensor_SensorMode}) */ + uint8_t *data = nullptr; /**< 传感器数据 */ + uint32_t dataLen = 0; /**< 传感器数据长度 */ +} Sensor_SensorEvent; + +/** + * @brief 列举传感器的数据报告模式。 + * + * @since 11 + */ +typedef struct Sensor_SubscribeId { + Sensor_SensorTypeId sensorTypeId; /**< 用于报告传感器的类型 */ + int32_t sensorIndex; // 表示同类型的第几个传感器,0表示默认值 + int32_t sensorId; // 表示同类型的第几个传感器,0表示默认值 + char networkId[NETWORK_ID_MAX_LEN]; // 分布式设备的networkId,默认值 "local",用户什么都不传或者用户主动传了"local",都都表示本地设备 + // 只有用户主动传其他设备的networkId,才订阅分布式设备 + //暂时的实现上,我们在cpp里直接把这个字段里先忽略掉 + // 上接口时,在注释上得说明一下,networkID是预留字段,暂无实现 +} Sensor_SubscribeId; + +/** + * @brief 传感器订阅属性。 + * + * @since 11 + */ +typedef struct Sensor_SubscribeAttribute { + int64_t samplingInterval; /**< 传感器采样周期 */ + int64_t reportInterval; /**< 传感器上报频率 */ +} Sensor_SubscribeAttribute; + +/** + * @brief 定义传感器代理报告数据的回调。 + * + * @since 11 + */ +typedef void (*Sensor_RecordSensorCallback)(Sensor_SensorEvent *event); + +/** + * @brief 为传感器数据订阅者定义保留字段。 + * + * @since 11 + */ +typedef struct Sensor_UserData { + char userData[SENSOR_USER_DATA_SIZE]; /**< 为传感器数据订阅者保留 */ +} Sensor_UserData; + +/** + * @brief 定义有关传感器数据订阅者的信息。 + * + * @since 11 + */ +typedef struct Sensor_SubscribeUser { + char name[NAME_MAX_LEN]; /**< 传感器数据订阅者的名称 */ + Sensor_RecordSensorCallback callback; /**< 用于报告传感器数据的回调 */ + Sensor_UserData *userData = nullptr; /**< 传感器数据订阅者的保留字段 */ +} Sensor_SubscribeUser; + +/** + * @brief 定义有关传感器数据精度的信息。 + * + * @since 11 + */ +typedef enum Sensor_SensorAccuracy { + UNRELIABLE = 0, /**< 传感器数据不可信 */ + LOW = 1, /**< 传感器数据精度较低 */ + MEDIUM = 2, /**< 传感器数据精度中等 */ + HIGH = 3, /**< 传感器数据高 */ +} Sensor_SensorAccuracy; + +/** + * @brief 加速度传感器数据格式, + * 设备在m/s2的三个物理轴(x、y、z)上。 + * + * @since 11 + */ +typedef struct Sensor_AccelerometerData { + float x = 0.0; /**< 施加在设备x轴的加速度,单位 : m/s² */ + float y = 0.0; /**< 施加在设备y轴的加速度,单位 : m/s² */ + float z = 0.0; /**< 施加在设备z轴的加速度,单位 : m/s² */ +} Sensor_AccelerometerData; + +/** + * @brief 定义线性加速度计的数据结构。测量施加于的线性加速度 + * 设备在m/s2的三个物理轴(x、y、z)上。 + * + * @since 11 + */ +typedef struct Sensor_LinearAccelData { + float x; /**< 施加在设备x轴的线性加速度,单位 : m/s² */ + float y; /**< 施加在设备y轴的线性加速度,单位 : m/s² */ + float z; /**< 施加在设备z轴的线性加速度,单位 : m/s² */ +} Sensor_LinearAccelData; + +/** + * @brief 定义陀螺仪传感器数据结构。测量物体的旋转角速度 + * 三个物理轴(x, y和z)上的设备,单位为rad/s。 + * + * @since 11 + */ +typedef struct Sensor_GyroscopeData { + float x; /**< 设备x轴的旋转角速度,单位rad/s */ + float y; /**< 设备y轴的旋转角速度,单位rad/s */ + float z; /**< 设备z轴的旋转角速度,单位rad/s */ +} Sensor_GyroscopeData; + +/** + * @brief 定义重力传感器数据结构。测量重力加速度 + * 在m/s2的三个物理轴(x, y和z)上到设备。 + * + * @since 11 + */ +typedef struct Sensor_GravityData { + float x; /**< 施加在设备x轴的重力加速度,单位 : m/s² */ + float y; /**< 施加在设备y轴的重力加速度,单位 : m/s² */ + float z; /**< 施加在设备z轴的重力加速度,单位 : m/s² */ +} Sensor_GravityData; + +/** + * @brief 定义未校准加速度计数据结构。测量应用于的未校准加速度计 + * 设备在m/s2的三个物理轴(x、y、z)上。 + * + * @since 11 + */ +typedef struct Sensor_AccelUncalibratedData { + float x; /**< 施加在设备x轴未校准的加速度,单位 : m/s² */ + float y; /**< 施加在设备y轴未校准的加速度,单位 : m/s² */ + float z; /**< 施加在设备z轴未校准的加速度,单位 : m/s² */ + float biasX; /**< 施加在设备x轴未校准的加速度偏量,单位 : m/s² */ + float biasY; /**< 施加在设备上y轴未校准的加速度偏量,单位 : m/s² */ + float biasZ; /**< 施加在设备z轴未校准的加速度偏量,单位 : m/s² */ +} Sensor_AccelUncalibratedData; + +/** + * @brief 定义未校准陀螺仪传感器数据结构。测量未校准的旋转角速度 + * 三个物理轴(x, y和z)上的设备,单位为rad/s。 + * + * @since 11 + */ +typedef struct Sensor_GyroUncalibratedData { + float x; /**< 设备x轴未校准的旋转角速度,单位rad/s */ + float y; /**< 设备y轴未校准的旋转角速度,单位rad/s */ + float z; /**< 设备z轴未校准的旋转角速度,单位rad/s */ + float biasX; /**< 设备x轴未校准的旋转角速度偏量,单位rad/s */ + float biasY; /**< 设备y轴未校准的旋转角速度偏量,单位rad/s */ + float biasZ; /**< 设备z轴未校准的旋转角速度偏量,单位rad/s */ +} Sensor_GyroUncalibratedData; + +/** + * @brief 定义大幅动作检测传感器数据结构。测量设备上是否有实质性的运动 + * 三个物理轴(x, y和z);值为1表示存在较大的运动;0表示这个值没有大的变动。 + * + * @since 11 + */ +typedef struct Sensor_SignificantMotionData { + float scalar; /**< 表示剧烈运动程度。如果取值为1则代表存在大幅度运动,取值为0则代表没有大幅度运动 */ +} Sensor_SignificantMotionData; + +/** + * @brief 定义计步器检测传感器数据结构。检测用户的计步动作;如果值为1, + * 它表示用户产生了计数行走的动作; 如果该值为0,则表示用户没有移动。 + * + * @since 11 + */ +typedef struct Sensor_PedometerDetectData { + float scalar; /**< 计步器检测状态,1表示有行走动作,0表示没有动作 */ +} Sensor_PedometerDetectData; + +/** + * @brief 定义计步器传感器数据结构。统计用户走过的步数。 + * + * @since 11 + */ +typedef struct Sensor_PedometerData { + float steps; /**< 用户的行走步数 */ +} Sensor_PedometerData; + +/** + * @brief 定义温度传感器数据结构。测量环境温度,单位为摄氏度。 + * + * @since 11 + */ +typedef struct Sensor_AmbientTemperatureData { + float temperature; /**< 环境温度,单位为摄氏度 */ +} Sensor_AmbientTemperatureData; + +/** + * @brief 定义颜色传感器数据结构。 + * + * @since 11 + */ +typedef struct Sensor_ColorData { + float lightIntensity; /**< 表示光的强度,单位 : 勒克斯 */ + float colorTemperature; /**< 表示色温,单位是开尔文(k) */ +} Sensor_ColorData; + +/** + * @brief 定义吸收比率传感器数据结构。 + * + * @since 11 + */ +typedef struct Sensor_SarData { + float absorptionRatio; /**< 表示具体的吸收率,单位 : W/kg */ +} Sensor_SarData; + +/** + * @brief 定义湿度传感器数据结构。测量环境的相对湿度,以百分比(%)表示。 + * + * @since 11 + */ +typedef struct Sensor_HumidityData { + float humidity; /**< 湿度值。测量环境的相对湿度,以百分比 (%) 表示 */ +} Sensor_HumidityData; + +/** + * @brief 定义温度传感器数据结构。测量环境的相对温度,单位为摄氏度。 + * + * @since 11 + */ +typedef struct Sensor_TemperatureData { + float temperature; /**< 显示环境温度,单位为摄氏度 */ +} Sensor_TemperatureData; + +/** + * @brief 定义地磁传感器数据结构。 + * 三次测量周围的地磁场物理轴(x, y, z),单位为μT。 + * + * @since 11 + */ +typedef struct Sensor_MagneticFieldData { + float x; /**< x轴环境磁场强度,单位 : μT */ + float y; /**< y轴环境磁场强度,单位 : μT */ + float z; /**< z轴环境磁场强度,单位 : μT */ +} Sensor_MagneticFieldData; + +/** + * @brief 义未校准地磁传感器数据结构。 + * 测量未校准的环境地磁场在三个物理轴(x, y, z)上以μT为单位。 + * + * @since 11 + */ +typedef struct Sensor_MagneticFieldUncalibratedData { + float x; /**< x轴未校准环境磁场强度,单位 : μT */ + float y; /**< y轴未校准环境磁场强度,单位 : μT */ + float z; /**< z轴未校准环境磁场强度,单位 : μT */ + float biasX; /**< x轴未校准环境磁场强度偏量,单位 : μT */ + float biasY; /**< y轴未校准环境磁场强度偏量,单位 : μT */ + float biasZ; /**< z轴未校准环境磁场强度偏量,单位 : μT */ +} Sensor_MagneticFieldUncalibratedData; + +/** + * @brief 定义气压计传感器数据结构。测量环境气压,单位:hPa或mbar。 + * + * @since 11 + */ +typedef struct Sensor_BarometerData { + float pressure; /**< 压力值,单位:帕斯卡 */ +} Sensor_BarometerData; + +/** + * @brief 定义设备方向传感器数据结构。测量设备的旋转方向,单位为rad。 + * + * @since 11 + */ +typedef struct Sensor_DeviceOrientationData { + float scalar; /**< 表示设备的方向 */ +} Sensor_DeviceOrientationData; + +/** + * @brief 定义方向传感器数据结构。 + * 测量设备围绕所有三个物理轴(z, x, y)旋转的角度值,单位为rad。 + * + * @since 11 + */ +typedef struct Sensor_OrientationData { + float alpha; /**< 设备围绕Z轴的旋转角度,单位:度 */ + float beta; /**< 设备围绕X轴的旋转角度,单位:度 */ + float gamma; /**< 设备围绕Y轴的旋转角度,单位:度 */ +} Sensor_OrientationData; + +/** + * @brief 定义旋转矢量传感器数据结构。测量设备游戏旋转矢量,复合传感器: + * 由加速度传感器、陀螺仪传感器合成。 + * + * @since 11 + */ +typedef struct Sensor_RotationVectorData { + float x; /**< 旋转矢量x轴分量 */ + float y; /**< 旋转矢量y轴分量 */ + float z; /**< 旋转矢量z轴分量 */ + float w; /**< 标量 */ +} Sensor_RotationVectorData; + +/** + * @brief 定义了地磁旋转矢量传感器的数据结构。 测量装置地磁旋转矢量,复合传感器: + * 由加速度传感器和磁场传感器合成。 + * + * @since 11 + */ +typedef struct Sensor_GeomagneticRotaVectorData { + float x; /**< 地磁旋转矢量x轴分量 */ + float y; /**< 地磁旋转矢量y轴分量 */ + float z; /**< 地磁旋转矢量z轴分量 */ + float w; /**< 标量 */ +} Sensor_GeomagneticRotaVectorData; + +/** + * @brief 定义接近光传感器数据结构。 测量相对于设备显示的可见物体的接近度或距离; + * 其中0表示接近,1表示距离。 + * + * @since 11 + */ +typedef struct Sensor_ProximityData { + float distance; /**< 可见物体与设备显示器的接近程度。0表示接近,1表示远离 */ +} Sensor_ProximityData; + +/** + * @brief 定义环境光传感器数据结构。以勒克斯为单位测量设备周围的光强度。 + * + * @since 11 + */ +typedef struct Sensor_AmbientLightData { + float intensity; /**< 表示光强度,单位为:勒克斯 */ +} Sensor_AmbientLightData; + +/** + * @brief 定义霍尔传感器数据结构。测量设备周围是否有磁力, + * 0表示没有磁力,1表示有磁力。 + * + * @since 11 + */ +typedef struct Sensor_HallData { + float status; /**< 显示霍尔状态。测量设备周围是否存在磁力吸引,0表示没有,大于0表示有 */ +} Sensor_HallData; + +/** + * @brief 定义心率传感器数据结构。测量用户的心率,以bpm为单位。 + * + * @since 11 + */ +typedef struct Sensor_HeartRateData { + float heartRate; /**< 心率值。测量用户的心率数值,单位:bpm */ +} Sensor_HeartRateData; + +/** + * @brief 定义佩戴检测传感器数据结构。表示设备是否被穿戴, + * 1表示已穿戴,0表示未穿戴。 + * + * @since 11 + */ +typedef struct Sensor_WearDetectionData { + float value; /**< 佩戴检测状态,穿戴为1,不穿戴为0 */ +} Sensor_WearDetectionData; + +#ifdef __cplusplus +} +#endif +#endif // SENSOR_CAPI_BASE_H \ No newline at end of file diff --git a/test/unittest/interfaces/capi/BUILD.gn b/test/unittest/interfaces/capi/BUILD.gn new file mode 100644 index 00000000..d80eea28 --- /dev/null +++ b/test/unittest/interfaces/capi/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (c) 2021-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("SensorCapiTest") { + module_out_path = "sensors/sensor/frameworks" + + sources = + [ "$SUBSYSTEM_DIR/test/unittest/interfaces/capi/sensor_capi_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/inner_api", + "$SUBSYSTEM_DIR/frameworks/kits/c", + "$SUBSYSTEM_DIR/test/unittest/common/include", + "$SUBSYSTEM_DIR/utils/common/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/capi:sensor_capi_target", + "$SUBSYSTEM_DIR/frameworks/native:sensor_target", + "$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 = [ ":SensorCapiTest" ] +} diff --git a/test/unittest/interfaces/capi/sensor_capi_test.cpp b/test/unittest/interfaces/capi/sensor_capi_test.cpp new file mode 100644 index 00000000..da47e6a0 --- /dev/null +++ b/test/unittest/interfaces/capi/sensor_capi_test.cpp @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2021 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 "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +// #include "sensor_agent.h" +#include "sensor_capi.h" +#include "sensor_errors.h" +#include "system_info.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, "SensorCapiTest" }; +// constexpr int32_t ACCELEROMETER { 1 }; +// constexpr int32_t INVALID { -1 }; + +PermissionDef g_infoManagerTestPermDef = { + .permissionName = "ohos.permission.ACCELEROMETER", + .bundleName = "accesstoken_test", + .grantMode = 1, + .label = "label", + .labelId = 1, + .description = "test sensor capi", + .descriptionId = 1, + .availableLevel = APL_NORMAL +}; + +PermissionStateFull g_infoManagerTestState = { + .grantFlags = {1}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .isGeneral = true, + .permissionName = "ohos.permission.ACCELEROMETER", + .resDeviceID = {"local"} +}; + +HapPolicyParams g_infoManagerTestPolicyPrams = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {g_infoManagerTestPermDef}, + .permStateList = {g_infoManagerTestState} +}; + +HapInfoParams g_infoManagerTestInfoParms = { + .bundleName = "sensorcapi_test", + .userID = 1, + .instIndex = 0, + .appIDDesc = "sensorCapiTest" +}; +} // namespace + +class SensorCapiTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +private: + static AccessTokenID tokenID_; +}; + +AccessTokenID SensorCapiTest::tokenID_ = 0; + +void SensorCapiTest::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 SensorCapiTest::TearDownTestCase() +{ + int32_t ret = AccessTokenKit::DeleteToken(tokenID_); + if (tokenID_ != 0) { + ASSERT_EQ(RET_SUCCESS, ret); + } +} + +void SensorCapiTest::SetUp() +{} + +void SensorCapiTest::TearDown() +{} + +void SensorDataCallbackImpl(Sensor_SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("event is null"); + return; + } + if (event[0].data == nullptr) { + SEN_HILOGE("event[0].data is nullptr"); + return; + } + if (event[0].dataLen < sizeof(Sensor_AccelerometerData)) { + SEN_HILOGE("Event dataLen less than acc data size, event.dataLen:%{public}u", event[0].dataLen); + return; + } + Sensor_AccelerometerData *accelData = reinterpret_cast(event[0].data); + SEN_HILOGD("ensorId:%{public}d, version:%{public}d, dataLen:%{public}d," + "x:%{public}f, y:%{public}f,z:%{public}f, option:%{public}d", + event[0].sensorTypeId, event[0].version, event[0].dataLen, + accelData->x, accelData->y, accelData->z, event[0].option); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_GetAllSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_GetAllSensors_001 in"); + Sensor_SensorInfo *sensorInfos { nullptr }; + int32_t count { 0 }; + Sensor_Result ret = static_cast(OH_Sensor_GetAllSensors(&sensorInfos, &count)); + SEN_HILOGI("sensorInfos.sensorTypeId:%{public}d, count:%{public}d,", sensorInfos->sensorTypeId, count); + ASSERT_EQ(ret, 0); + ASSERT_NE(count, 0); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_GetAllSensorsTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_GetAllSensors_002 in"); + int32_t count = 0; + Sensor_Result ret = static_cast(OH_Sensor_GetAllSensors(nullptr, &count)); + ASSERT_NE(ret, SENSOR_SUCCESS); + ASSERT_EQ(count, 0); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_GetAllSensorsTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_GetAllSensors_003 in"); + Sensor_SensorInfo *sensorInfos { nullptr }; + Sensor_Result ret = static_cast(OH_Sensor_GetAllSensors(&sensorInfos, nullptr)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_GetAllSensors_001 in"); + Sensor_SubscribeUser cb; + cb.callback = SensorDataCallbackImpl; + Sensor_SubscribeId subscribeId; + subscribeId.sensorTypeId = ACCELEROMETER; + Sensor_SubscribeAttribute attribute; + attribute.samplingInterval = 100000000; + attribute.reportInterval = 100000000; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &cb)); + ASSERT_EQ(ret, 0); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscribeSensor_002 in"); + Sensor_SubscribeUser cb; + cb.callback = SensorDataCallbackImpl; + Sensor_SubscribeId subscribeId; + subscribeId.sensorId = INVALID; + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &cb)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscribeSensor_003 in"); + Sensor_SubscribeAttribute attribute; + Sensor_SubscribeId subscribeId; + subscribeId.sensorTypeId = ACCELEROMETER; + attribute.samplingInterval = 100000000; + attribute.reportInterval = 100000000; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, nullptr)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_001 in"); + Sensor_SubscribeUser cb; + cb.callback = SensorDataCallbackImpl; + Sensor_SubscribeId subscribeId; + subscribeId.sensorTypeId = ACCELEROMETER; + Sensor_SubscribeAttribute attribute; + attribute.samplingInterval = 100000000; + attribute.reportInterval = 100000000; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &cb)); + ASSERT_EQ(ret, 0); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, &cb)); + ASSERT_EQ(ret, 0); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_002 in"); + Sensor_SubscribeUser cb; + cb.callback = SensorDataCallbackImpl; + Sensor_SubscribeId subscribeId; + subscribeId.sensorId = INVALID; + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, &cb)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensor_003 in"); + Sensor_SubscribeId subscribeId; + subscribeId.sensorTypeId = ACCELEROMETER; + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, nullptr)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} +} // namespace Sensors +} // namespace OHOS -- Gitee