From 22bcc4ad3065ce3d3f17951745703435081d35d0 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 03:30:42 +0000 Subject: [PATCH 01/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I653df0a75965349b252775ffa17f36fa30cc01c6 --- interfaces/kits/c/BUILD.gn | 36 ++ interfaces/kits/c/libsensor_capi.ndk.json | 11 + interfaces/kits/c/sensor_capi.h | 89 ++++ interfaces/kits/c/sensor_capi_base.h | 471 ++++++++++++++++++ interfaces/native/BUILD.gn | 7 +- interfaces/native/src/sensor_capi.cpp | 76 +++ interfaces/native/test/BUILD.gn | 32 ++ .../native/test/unittest/sensor_capi_test.cpp | 204 ++++++++ 8 files changed, 925 insertions(+), 1 deletion(-) create mode 100755 interfaces/kits/c/BUILD.gn create mode 100755 interfaces/kits/c/libsensor_capi.ndk.json create mode 100755 interfaces/kits/c/sensor_capi.h create mode 100755 interfaces/kits/c/sensor_capi_base.h mode change 100644 => 100755 interfaces/native/BUILD.gn create mode 100755 interfaces/native/src/sensor_capi.cpp mode change 100644 => 100755 interfaces/native/test/BUILD.gn create mode 100755 interfaces/native/test/unittest/sensor_capi_test.cpp diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn new file mode 100755 index 00000000..964d5d24 --- /dev/null +++ b/interfaces/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("./../../../sensor.gni") +ohos_ndk_headers("sensor_capi_header") { + dest_dir = "$ndk_headers_out_dir/sensor_capi" + sources = + [ + "./sensor_capi.h", + "./sensor_capi_base.h" + ] +} + +ohos_ndk_library("libsensor_capi_ndk") { + ndk_description_file = "./libsensor_capi.ndk.json" + output_name = "libsensor_capi_ndk" + output_extension = "so" + + system_capability = "SystemCapability.Sensors.Sensor" + system_capability_headers = + [ + "sensor/sensor_capi_base.h", + "sensor/sensor_capi.h" + ] +} diff --git a/interfaces/kits/c/libsensor_capi.ndk.json b/interfaces/kits/c/libsensor_capi.ndk.json new file mode 100755 index 00000000..dc53b9d0 --- /dev/null +++ b/interfaces/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/interfaces/kits/c/sensor_capi.h b/interfaces/kits/c/sensor_capi.h new file mode 100755 index 00000000..917a344b --- /dev/null +++ b/interfaces/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 **sensorInfo, int32_t *count); + +/** + * @brief 订阅传感器数据,系统将按照用户指定的上报频率向用户报告传感器数据。 + * + * @param sensorTypeId 传感器类型, 详情请参见{@link Sensor_SensorTypeId}。 + * @param attribute 订阅属性,用于指定传感器的上报频率,详情请参见{@Link Sensor_SubscribeAttribute}。 + * @param user 订阅者信息,指定传感器数据回调函数,详情请参见{@Link Sensor_SensorUser}。 + * @return 订阅传感器成功返回SENSOR_SUCCESS,否则返回对应的错误码, + * 详情请参见{@Link Sensor_Result}。 + * + * @since 11 + */ +Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SensorTypeId sensorTypeId, Sensor_SubscribeAttribute attribute, + const Sensor_SensorUser *user); + +/** + * @brief 取消订阅传感器数据。 + * + * @param sensorTypeId 传感器类型, 详情请参见{@link Sensor_SensorTypeId}。 + * @param user 订阅者信息,指定传感器数据回调函数, + * 详情请参见{@Link Sensor_SensorUser}。 + * @return 取消订阅传感器成功返回SENSOR_SUCCESS,否则返回对应的错误码, + * 详情请参见{@Link Sensor_Result}。 + * + * @since 11 + */ +Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SensorTypeId sensorTypeId, const Sensor_SensorUser *user); + +#ifdef __cplusplus +} +#endif +#endif // SENSOR_CAPI_H \ No newline at end of file diff --git a/interfaces/kits/c/sensor_capi_base.h b/interfaces/kits/c/sensor_capi_base.h new file mode 100755 index 00000000..57837206 --- /dev/null +++ b/interfaces/kits/c/sensor_capi_base.h @@ -0,0 +1,471 @@ +/* + * 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 */ + +/** + * @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 */ + 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; /**< 传感器类型 ID */ + int32_t version; /**< 传感器算法版本 */ + int64_t timestamp; /**< 报告传感器数据的时间 */ + uint32_t option; /**< 传感器数据选项,包括测量范围和精度 */ + int32_t mode; /**< 传感器数据上报方式 (详细参考{@link Sensor_SensorMode}) */ + uint8_t *data = nullptr; /**< 传感器数据 */ + uint32_t dataLen; /**< 传感器数据长度 */ +} Sensor_SensorEvent; + +/** + * @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_SensorUser { + char name[NAME_MAX_LEN]; /**< 传感器数据订阅者的名称 */ + Sensor_RecordSensorCallback callback; /**< 用于报告传感器数据的回调 */ + Sensor_UserData *userData = nullptr; /**< 传感器数据订阅者的保留字段 */ +} Sensor_SensorUser; + +/** + * @brief 加速度传感器数据格式, + * 设备在m/s2的三个物理轴(x、y、z)上。 + * + * @since 11 + */ +typedef struct Sensor_AccelerometerData { + float x; /**< 施加在设备x轴的加速度,单位 : m/s² */ + float y; /**< 施加在设备y轴的加速度,单位 : m/s² */ + float z; /**< 施加在设备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/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn old mode 100644 new mode 100755 index 09be0a46..53621cc4 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -24,12 +24,16 @@ config("sensor_private_config") { include_dirs = [ "$SUBSYSTEM_DIR/frameworks/native/sensor/include", "$SUBSYSTEM_DIR/interfaces/native/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", "$SUBSYSTEM_DIR/utils/common/include", ] } config("sensor_public_config") { - include_dirs = [ "include" ] + include_dirs = [ + "include" , + "$SUBSYSTEM_DIR/interfaces/kits/c", + ] } ohos_shared_library("sensor_interface_native") { @@ -38,6 +42,7 @@ ohos_shared_library("sensor_interface_native") { "src/geomagnetic_field.cpp", "src/sensor_agent.cpp", "src/sensor_algorithm.cpp", + "src/sensor_capi.cpp", ] configs = [ ":sensor_private_config" ] diff --git a/interfaces/native/src/sensor_capi.cpp b/interfaces/native/src/sensor_capi.cpp new file mode 100755 index 00000000..0d8e3587 --- /dev/null +++ b/interfaces/native/src/sensor_capi.cpp @@ -0,0 +1,76 @@ +/* + * 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 + +#include +#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 **sensorInfo, int32_t *count) +{ + Sensor_Result ret = static_cast(GetAllSensors((SensorInfo **)sensorInfo, count)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("Get sensor list failed, ret is %{public}d", ret); + } + return ret; +} + +bool CheckSensorTypeId(int32_t sensorTypeId) +{ + int32_t count = -1; + Sensor_SensorInfo *sensorInfo = nullptr; + int32_t ret = OH_Sensor_GetAllSensors(&sensorInfo, &count); + if (ret != 0) { + return false; + } + for (int32_t i = 0; i < count; i++) + { + if ((sensorInfo + i)->sensorTypeId == sensorTypeId) { + return true; + } + } + return false; +} + +Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SensorTypeId sensorTypeId, Sensor_SubscribeAttribute attribute, + const Sensor_SensorUser *user) +{ + if (!CheckSensorTypeId(sensorTypeId) || attribute.samplingInterval < 0 || attribute.reportInterval < 0) { + SEN_HILOGE("Sensor attribute value is invalid"); + return SENSOR_PARAMETER_ERROR; + } + Sensor_Result ret = static_cast(SubscribeSensor(sensorTypeId, (SensorUser *)user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SubscribeSensor failed, ret is %{public}d", ret); + } + return ret; +} + +Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SensorTypeId sensorTypeId, const Sensor_SensorUser *user) +{ + if (!CheckSensorTypeId(sensorTypeId)) { + SEN_HILOGE("sensorTypeId is invalid"); + return SENSOR_PARAMETER_ERROR; + } + Sensor_Result ret = static_cast(UnsubscribeSensor(sensorTypeId, (SensorUser *)user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("UnsubscribeSensor failed, ret is %{public}d", ret); + } + return ret; +} diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn old mode 100644 new mode 100755 index 727f68b5..26edd433 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -24,6 +24,38 @@ ohos_unittest("SensorAgentTest") { include_dirs = [ "$SUBSYSTEM_DIR/interfaces/native/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/test/unittest/common/include", + "$SUBSYSTEM_DIR/utils/common/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_ndk_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", + ] +} + +ohos_unittest("SensorCapiTest") { + module_out_path = "sensors/sensor/interfaces" + + sources = [ + "$SUBSYSTEM_DIR/interfaces/native/test/unittest/sensor_capi_test.cpp", + ] + + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/native/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", "$SUBSYSTEM_DIR/test/unittest/common/include", "$SUBSYSTEM_DIR/utils/common/include", ] diff --git a/interfaces/native/test/unittest/sensor_capi_test.cpp b/interfaces/native/test/unittest/sensor_capi_test.cpp new file mode 100755 index 00000000..96787e6f --- /dev/null +++ b/interfaces/native/test/unittest/sensor_capi_test.cpp @@ -0,0 +1,204 @@ +/* + * 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" }; +Sensor_SensorTypeId SENSOR_ID = ACCELEROMETER; +Sensor_SensorTypeId INVALID_VALUE = INVALID; + +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("Sensor_SensorEvent is null"); + return; + } + float *sensorData = (float *)event[0].data; + SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}d, data:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + +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)); + 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_SensorUser user; + user.callback = nullptr; + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(SENSOR_ID, attribute, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscribeSensor_002 in"); + Sensor_SensorUser user; + user.callback = SensorDataCallbackImpl; + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(INVALID_VALUE, attribute, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscribeSensor_003 in"); + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(SENSOR_ID, attribute, nullptr)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_001 in"); + Sensor_SensorUser user; + user.callback = nullptr; + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(SENSOR_ID, attribute, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); + ret = static_cast(OH_Sensor_UnsubscribeSensor(SENSOR_ID, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_002 in"); + Sensor_SensorUser user; + user.callback = SensorDataCallbackImpl; + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(INVALID_VALUE, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensor_003 in"); + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(SENSOR_ID, nullptr)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} +} // namespace Sensors +} // namespace OHOS -- Gitee From 0dcc04a6f89d8e96c343123757c880948fda94b7 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:11:02 +0000 Subject: [PATCH 02/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I31985490c35c39945c796012a4acc34cead122c4 --- interfaces/kits/c/BUILD.gn | 68 ++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index 964d5d24..01dbab39 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -1,36 +1,34 @@ -# 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 +# 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("./../../../sensor.gni") -ohos_ndk_headers("sensor_capi_header") { - dest_dir = "$ndk_headers_out_dir/sensor_capi" - sources = - [ - "./sensor_capi.h", - "./sensor_capi_base.h" - ] -} - -ohos_ndk_library("libsensor_capi_ndk") { - ndk_description_file = "./libsensor_capi.ndk.json" - output_name = "libsensor_capi_ndk" - output_extension = "so" - - system_capability = "SystemCapability.Sensors.Sensor" - system_capability_headers = - [ - "sensor/sensor_capi_base.h", - "sensor/sensor_capi.h" - ] -} + +import("//build/ohos.gni") +import("./../../../sensor.gni") +ohos_ndk_headers("sensor_capi_header") { + dest_dir = "$ndk_headers_out_dir/sensor_capi" + sources = [ + "./sensor_capi.h", + "./sensor_capi_base.h", + ] +} + +ohos_ndk_library("libsensor_capi_ndk") { + ndk_description_file = "./libsensor_capi.ndk.json" + output_name = "libsensor_capi_ndk" + output_extension = "so" + + system_capability = "SystemCapability.Sensors.Sensor" + system_capability_headers = [ + "sensor/sensor_capi_base.h", + "sensor/sensor_capi.h", + ] +} -- Gitee From a441ece501a08c89e75856364e9813c8767813a3 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:21:20 +0000 Subject: [PATCH 03/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: Ia52e34ff01f97b9d47f54ac5b5e0d156718a369f --- interfaces/kits/c/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index 01dbab39..a310cac2 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -9,7 +9,7 @@ # 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. +# limitations under the License. import("//build/ohos.gni") import("./../../../sensor.gni") @@ -31,4 +31,4 @@ ohos_ndk_library("libsensor_capi_ndk") { "sensor/sensor_capi_base.h", "sensor/sensor_capi.h", ] -} +} \ No newline at end of file -- Gitee From eae787304b216bd818f0bfab1623ce6ec0cf8914 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:36:53 +0000 Subject: [PATCH 04/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I503a67397b27ffdce4c61d4b11a772d26823510d --- interfaces/kits/c/BUILD.gn | 2 +- interfaces/kits/c/sensor_capi.h | 1 - interfaces/kits/c/sensor_capi_base.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index a310cac2..b4ad0c94 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -23,7 +23,7 @@ ohos_ndk_headers("sensor_capi_header") { ohos_ndk_library("libsensor_capi_ndk") { ndk_description_file = "./libsensor_capi.ndk.json" - output_name = "libsensor_capi_ndk" + output_name = "sensor_capi_ndk" output_extension = "so" system_capability = "SystemCapability.Sensors.Sensor" diff --git a/interfaces/kits/c/sensor_capi.h b/interfaces/kits/c/sensor_capi.h index 917a344b..7ea3b750 100755 --- a/interfaces/kits/c/sensor_capi.h +++ b/interfaces/kits/c/sensor_capi.h @@ -27,7 +27,6 @@ * @file sensor_capi.h * * @brief 声明操作传感器的常用api,用于获取传感器信息、订阅或取消订阅传感器数据等。 - * @library 引用sensor_capi_base.h,链接生成libsensor_capi_ndk.so。 * @syscap SystemCapability.Sensors.Sensor * @since 11 */ diff --git a/interfaces/kits/c/sensor_capi_base.h b/interfaces/kits/c/sensor_capi_base.h index 57837206..b3b63637 100755 --- a/interfaces/kits/c/sensor_capi_base.h +++ b/interfaces/kits/c/sensor_capi_base.h @@ -26,7 +26,6 @@ * @file sensor_capi_base.h * * @brief 定义常用传感器属性。 - * @library 需要链接生成libsensor_capi_ndk.so。 * @syscap SystemCapability.Sensors.Sensor * * @since 11 -- Gitee From 7b38f1538c6fe2fc0189f13ac65efdcbc8fad15a Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:38:36 +0000 Subject: [PATCH 05/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I37e8a80e7ef9ecca877686b6be1f882ec83a167d --- interfaces/kits/c/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index b4ad0c94..7acd8d44 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -31,4 +31,4 @@ ohos_ndk_library("libsensor_capi_ndk") { "sensor/sensor_capi_base.h", "sensor/sensor_capi.h", ] -} \ No newline at end of file +} -- Gitee From 54fb716a6d948fa639f6c4e47dca271a38dcabe2 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:52:24 +0000 Subject: [PATCH 06/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I69235eee9faf700154ddfb8acf0750557b00e6f4 --- interfaces/native/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 53621cc4..e60be8cf 100755 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -31,9 +31,9 @@ config("sensor_private_config") { config("sensor_public_config") { include_dirs = [ - "include" , + "include", "$SUBSYSTEM_DIR/interfaces/kits/c", - ] + ] } ohos_shared_library("sensor_interface_native") { -- Gitee From 7f9af7c9b6dae4b38a2fe51655c34f067a13be4f Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:55:00 +0000 Subject: [PATCH 07/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: Ie9bfc25d52c36404bb0879bcd74d429f02c74e99 --- interfaces/kits/c/BUILD.gn | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index 7acd8d44..19ca05f7 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -12,6 +12,7 @@ # 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" @@ -23,7 +24,7 @@ ohos_ndk_headers("sensor_capi_header") { ohos_ndk_library("libsensor_capi_ndk") { ndk_description_file = "./libsensor_capi.ndk.json" - output_name = "sensor_capi_ndk" + output_name = "libsensor_capi_ndk" output_extension = "so" system_capability = "SystemCapability.Sensors.Sensor" -- Gitee From 653467e0ba04c8a6b308b853cdb5f92558ccdf9f Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 06:57:43 +0000 Subject: [PATCH 08/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I402200a3ba55bf52cf997bf624318a6f4e08a9d9 --- interfaces/kits/c/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index 19ca05f7..1c2803f0 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -32,4 +32,4 @@ ohos_ndk_library("libsensor_capi_ndk") { "sensor/sensor_capi_base.h", "sensor/sensor_capi.h", ] -} +} \ No newline at end of file -- Gitee From 6cd0c25fe69f8933d2be4b1516b52cc7d6d331e3 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 12 Sep 2023 07:03:47 +0000 Subject: [PATCH 09/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: Ie6b8de80b2b4e120b517ca2da24063609f967e44 --- interfaces/kits/c/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index 1c2803f0..19ca05f7 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -32,4 +32,4 @@ ohos_ndk_library("libsensor_capi_ndk") { "sensor/sensor_capi_base.h", "sensor/sensor_capi.h", ] -} \ No newline at end of file +} -- Gitee From 2bef610060109b7d77ea7c9ef5bce95cda6498d4 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Wed, 13 Sep 2023 06:15:02 +0000 Subject: [PATCH 10/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I3874c94352f8c98d18cf030e7262eef40c4b3c27 --- interfaces/native/test/BUILD.gn | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 26edd433..f2381693 100755 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -49,9 +49,8 @@ ohos_unittest("SensorAgentTest") { ohos_unittest("SensorCapiTest") { module_out_path = "sensors/sensor/interfaces" - sources = [ - "$SUBSYSTEM_DIR/interfaces/native/test/unittest/sensor_capi_test.cpp", - ] + sources = + [ "$SUBSYSTEM_DIR/interfaces/native/test/unittest/sensor_capi_test.cpp" ] include_dirs = [ "$SUBSYSTEM_DIR/interfaces/native/include", -- Gitee From 25d2b7788fcc596df0a10acbd957b0523e40d441 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 14 Sep 2023 09:19:27 +0000 Subject: [PATCH 11/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I6d422cdf3fff5eab67b462c8d17e2cc886b8060a --- bundle.json | 10 ++++++++++ interfaces/kits/c/BUILD.gn | 9 +++++---- interfaces/native/BUILD.gn | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/bundle.json b/bundle.json index 0ce724b0..96641fb9 100755 --- a/bundle.json +++ b/bundle.json @@ -58,6 +58,16 @@ ], "header_base": "//base/sensors/sensor/interfaces/native/include" } + }, + { + "name": "//base/sensors/sensor/interfaces/native:sensor_ndk", + "header": { + "header_files": [ + "sensor_capi_base.h", + "sensor_capi.h" + ], + "header_base": "//base/sensors/sensor/interfaces/kits/c" + } } ], "test": [ diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index 19ca05f7..bf610356 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -17,6 +17,7 @@ import("./../../../sensor.gni") ohos_ndk_headers("sensor_capi_header") { dest_dir = "$ndk_headers_out_dir/sensor_capi" sources = [ + "$SUBSYSTEM_DIR/interfaces/native", "./sensor_capi.h", "./sensor_capi_base.h", ] @@ -24,12 +25,12 @@ ohos_ndk_headers("sensor_capi_header") { ohos_ndk_library("libsensor_capi_ndk") { ndk_description_file = "./libsensor_capi.ndk.json" - output_name = "libsensor_capi_ndk" + output_name = "sensor_capi_ndk" output_extension = "so" - + min_compact_version = "10" system_capability = "SystemCapability.Sensors.Sensor" system_capability_headers = [ - "sensor/sensor_capi_base.h", - "sensor/sensor_capi.h", + "$ndk_headers_out_dir/sensor/sensor_capi_base.h", + "$ndk_headers_out_dir/sensor/sensor_capi.h", ] } diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index e60be8cf..83aa657a 100755 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -41,8 +41,7 @@ ohos_shared_library("sensor_interface_native") { sources = [ "src/geomagnetic_field.cpp", "src/sensor_agent.cpp", - "src/sensor_algorithm.cpp", - "src/sensor_capi.cpp", + "src/sensor_algorithm.cpp" ] configs = [ ":sensor_private_config" ] @@ -66,6 +65,37 @@ ohos_shared_library("sensor_interface_native") { subsystem_name = "sensors" } +ohos_shared_library("sensor_ndk") { + + sources = [ "src/sensor_capi.cpp" ] + + defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] + + configs = [ ":sensor_private_config" ] + public_configs = [ ":sensor_public_config" ] + + deps =[ + "$SUBSYSTEM_DIR/frameworks/native/sensor:libsensor_native", + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/interfaces/native:libsensor_ndk", + ] + + 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_ndk_target") { - deps = [ ":sensor_interface_native" ] + deps = [ + ":sensor_interface_native", + ":sensor_ndk" + ] } -- Gitee From 7822776fd8feef3f35e22e5351f4abbcff0cc80c Mon Sep 17 00:00:00 2001 From: cff-gite Date: Fri, 15 Sep 2023 01:06:26 +0000 Subject: [PATCH 12/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I6bff7907b5cdf0739eb6120ccc2e79b2870289f5 --- interfaces/native/BUILD.gn | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 83aa657a..edbebd3e 100755 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -36,13 +36,10 @@ config("sensor_public_config") { ] } -ohos_shared_library("sensor_interface_native") { - output_name = "sensor_agent" - sources = [ - "src/geomagnetic_field.cpp", - "src/sensor_agent.cpp", - "src/sensor_algorithm.cpp" - ] +ohos_shared_library("sensor_ndk") { + sources = [ "src/sensor_capi.cpp" ] + + defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] configs = [ ":sensor_private_config" ] public_configs = [ ":sensor_public_config" ] @@ -50,6 +47,7 @@ ohos_shared_library("sensor_interface_native") { deps = [ "$SUBSYSTEM_DIR/frameworks/native/sensor:libsensor_native", "$SUBSYSTEM_DIR/interfaces/native:libsensor_ndk", + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", ] external_deps = [ @@ -60,23 +58,24 @@ ohos_shared_library("sensor_interface_native") { "safwk:system_ability_fwk", ] + relative_install_dir = "ndk" part_name = "sensor" - innerapi_tags = [ "platformsdk" ] subsystem_name = "sensors" } -ohos_shared_library("sensor_ndk") { - - sources = [ "src/sensor_capi.cpp" ] - - defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] +ohos_shared_library("sensor_interface_native") { + output_name = "sensor_agent" + sources = [ + "src/geomagnetic_field.cpp", + "src/sensor_agent.cpp", + "src/sensor_algorithm.cpp", + ] configs = [ ":sensor_private_config" ] public_configs = [ ":sensor_public_config" ] - deps =[ + deps = [ "$SUBSYSTEM_DIR/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", "$SUBSYSTEM_DIR/interfaces/native:libsensor_ndk", ] @@ -88,14 +87,14 @@ ohos_shared_library("sensor_ndk") { "safwk:system_ability_fwk", ] - relative_install_dir = "ndk" part_name = "sensor" + innerapi_tags = [ "platformsdk" ] subsystem_name = "sensors" } group("sensor_ndk_target") { deps = [ ":sensor_interface_native", - ":sensor_ndk" + ":sensor_ndk", ] } -- Gitee From e902e662c434e504074486a1db31d2e8dc6c1d0b Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 21 Sep 2023 01:25:44 +0000 Subject: [PATCH 13/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: Ib898aceabf78580b5e91df093834060d1aa67fe5 --- bundle.json | 12 +- interfaces/capi/BUILD.gn | 62 ++++++ interfaces/capi/sensor_capi.cpp | 83 +++++++ interfaces/capi/test/unittest/BUILD.gn | 53 +++++ .../capi/test/unittest/sensor_capi_test.cpp | 208 ++++++++++++++++++ interfaces/native/BUILD.gn | 38 +--- interfaces/native/test/BUILD.gn | 31 --- 7 files changed, 415 insertions(+), 72 deletions(-) create mode 100755 interfaces/capi/BUILD.gn create mode 100755 interfaces/capi/sensor_capi.cpp create mode 100755 interfaces/capi/test/unittest/BUILD.gn create mode 100755 interfaces/capi/test/unittest/sensor_capi_test.cpp diff --git a/bundle.json b/bundle.json index 96641fb9..b6bd07d0 100755 --- a/bundle.json +++ b/bundle.json @@ -40,6 +40,7 @@ "fwk_group": [ "//base/sensors/sensor/interfaces/plugin:sensor_js_target", "//base/sensors/sensor/interfaces/native:sensor_ndk_target", + "//base/sensors/sensor/interfaces/capi:sensor_capi_ndk_target", "//base/sensors/sensor/frameworks/native/sensor:sensor_native_target", "//base/sensors/sensor/utils:sensor_utils_target" ], @@ -60,7 +61,7 @@ } }, { - "name": "//base/sensors/sensor/interfaces/native:sensor_ndk", + "name": "//base/sensors/sensor/interfaces/capi:sensor_capi_ndk_target", "header": { "header_files": [ "sensor_capi_base.h", @@ -71,10 +72,11 @@ } ], "test": [ - "//base/sensors/sensor/interfaces/plugin/test/unittest:unittest", - "//base/sensors/sensor/interfaces/native/test/fuzztest:fuzztest", - "//base/sensors/sensor/interfaces/native/test:unittest", - "//base/sensors/sensor/services/sensor/test/fuzztest:fuzztest" + "//base/sensors/sensor/interfaces/plugin/test/unittest:unittest", + "//base/sensors/sensor/interfaces/native/test/fuzztest:fuzztest", + "//base/sensors/sensor/interfaces/capi/test/unittest:unittest", + "//base/sensors/sensor/interfaces/native/test:unittest", + "//base/sensors/sensor/services/sensor/test/fuzztest:fuzztest" ] } } diff --git a/interfaces/capi/BUILD.gn b/interfaces/capi/BUILD.gn new file mode 100755 index 00000000..b1baaa13 --- /dev/null +++ b/interfaces/capi/BUILD.gn @@ -0,0 +1,62 @@ +# 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/sensor/include", + "$SUBSYSTEM_DIR/interfaces/native/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/utils/common/include", + ] +} + +config("sensor_public_config") { + include_dirs = [ + "include", + "$SUBSYSTEM_DIR/interfaces/kits/c", + ] +} + +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/sensor:libsensor_native", + "$SUBSYSTEM_DIR/interfaces/native:libsensor_ndk", + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + ] + + 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_ndk_target") { + deps = [ ":sensor_capi_ndk" ] +} diff --git a/interfaces/capi/sensor_capi.cpp b/interfaces/capi/sensor_capi.cpp new file mode 100755 index 00000000..0fda0516 --- /dev/null +++ b/interfaces/capi/sensor_capi.cpp @@ -0,0 +1,83 @@ +/* + * 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 + +#include +#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 **sensorInfo, int32_t *count) +{ + SEN_HILOGD("cff1 Get sensor list in"); + Sensor_Result ret = static_cast(GetAllSensors((SensorInfo **)sensorInfo, count)); + 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); + if (ret != 0) { + return false; + } + for (int32_t i = 0; i < count; i++) + { + if ((sensorInfo + i)->sensorTypeId == subscribeId.sensorTypeId) { + return true; + } + } + return false; +} + +Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SubscribeId subscribeId, Sensor_SubscribeAttribute attribute, + const Sensor_SubscribeUser *user) +{ + SEN_HILOGD("cff2 OH_Sensor_SubscribeSensor in"); + if (!CheckSensorSubscribeId(subscribeId) || attribute.samplingInterval < 0 || attribute.reportInterval < 0) { + SEN_HILOGE("Sensor attribute value is invalid"); + return SENSOR_PARAMETER_ERROR; + } + SEN_HILOGD("cff3 SubscribeSensor in"); + int32_t sensorTypeId = 0; + Sensor_Result ret = static_cast(SubscribeSensor(sensorTypeId, (SensorUser *)user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("SubscribeSensor failed, ret is %{public}d", ret); + } + return ret; +} + +Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SubscribeId subscribeId, const Sensor_SubscribeUser *user) +{ + SEN_HILOGD("cff4 OH_Sensor_UnsubscribeSensor in"); + if (!CheckSensorSubscribeId(subscribeId)) { + SEN_HILOGE("subscribeId is invalid"); + return SENSOR_PARAMETER_ERROR; + } + SEN_HILOGD("cff5 UnubscribeSensor in"); + int32_t sensorTypeId = 0; + Sensor_Result ret = static_cast(UnsubscribeSensor(sensorTypeId, (SensorUser *)user)); + if (ret != SENSOR_SUCCESS) { + SEN_HILOGE("UnsubscribeSensor failed, ret is %{public}d", ret); + } + return ret; +} diff --git a/interfaces/capi/test/unittest/BUILD.gn b/interfaces/capi/test/unittest/BUILD.gn new file mode 100755 index 00000000..41078dae --- /dev/null +++ b/interfaces/capi/test/unittest/BUILD.gn @@ -0,0 +1,53 @@ +# 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/interfaces" + + sources = + [ "$SUBSYSTEM_DIR/interfaces/capi/test/unittest/sensor_capi_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/native/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/test/unittest/common/include", + "$SUBSYSTEM_DIR/utils/common/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_ndk_target", + "$SUBSYSTEM_DIR/interfaces/capi:sensor_capi_ndk_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/interfaces/capi/test/unittest/sensor_capi_test.cpp b/interfaces/capi/test/unittest/sensor_capi_test.cpp new file mode 100755 index 00000000..a3f9345d --- /dev/null +++ b/interfaces/capi/test/unittest/sensor_capi_test.cpp @@ -0,0 +1,208 @@ +/* + * 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" }; +// Sensor_SubscribeId SENSOR_ID = 1; +// Sensor_SubscribeId INVALID_VALUE = -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("Sensor_SensorEvent is null"); + return; + } + float *sensorData = (float *)event[0].data; + SEN_HILOGI("sensorId:%{public}d, version:%{public}d, dataLen:%{public}d, data:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + +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)); + 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 user; + user.callback = nullptr; + Sensor_SubscribeId subscribeId; + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +// HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_002, TestSize.Level1) +// { +// SEN_HILOGI("OH_Sensor_SubscribeSensor_002 in"); +// Sensor_SubscribeUser user; +// user.callback = SensorDataCallbackImpl; +// struct Sensor_SubscribeAttribute attribute; +// Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(INVALID_VALUE, attribute, &user)); +// ASSERT_NE(ret, SENSOR_SUCCESS); +// } + +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscribeSensor_003 in"); + struct Sensor_SubscribeAttribute attribute; + Sensor_SubscribeId subscribeId; + 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 user; + user.callback = nullptr; + struct Sensor_SubscribeAttribute attribute; + Sensor_SubscribeId subscribeId; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); + ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} + +// HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_002, TestSize.Level1) +// { +// SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_002 in"); +// Sensor_SubscribeUser user; +// user.callback = SensorDataCallbackImpl; +// Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(INVALID_VALUE, &user)); +// ASSERT_NE(ret, SENSOR_SUCCESS); +// } + +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensor_003 in"); + Sensor_SubscribeId subscribeId; + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, nullptr)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} +} // namespace Sensors +} // namespace OHOS diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index edbebd3e..09be0a46 100755 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -24,43 +24,12 @@ config("sensor_private_config") { include_dirs = [ "$SUBSYSTEM_DIR/frameworks/native/sensor/include", "$SUBSYSTEM_DIR/interfaces/native/include", - "$SUBSYSTEM_DIR/interfaces/kits/c", "$SUBSYSTEM_DIR/utils/common/include", ] } config("sensor_public_config") { - include_dirs = [ - "include", - "$SUBSYSTEM_DIR/interfaces/kits/c", - ] -} - -ohos_shared_library("sensor_ndk") { - sources = [ "src/sensor_capi.cpp" ] - - defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] - - configs = [ ":sensor_private_config" ] - public_configs = [ ":sensor_public_config" ] - - deps = [ - "$SUBSYSTEM_DIR/frameworks/native/sensor:libsensor_native", - "$SUBSYSTEM_DIR/interfaces/native:libsensor_ndk", - "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", - ] - - 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" + include_dirs = [ "include" ] } ohos_shared_library("sensor_interface_native") { @@ -93,8 +62,5 @@ ohos_shared_library("sensor_interface_native") { } group("sensor_ndk_target") { - deps = [ - ":sensor_interface_native", - ":sensor_ndk", - ] + deps = [ ":sensor_interface_native" ] } diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index f2381693..727f68b5 100755 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -24,37 +24,6 @@ ohos_unittest("SensorAgentTest") { include_dirs = [ "$SUBSYSTEM_DIR/interfaces/native/include", - "$SUBSYSTEM_DIR/interfaces/kits/c", - "$SUBSYSTEM_DIR/test/unittest/common/include", - "$SUBSYSTEM_DIR/utils/common/include", - ] - - deps = [ - "$SUBSYSTEM_DIR/interfaces/native:sensor_ndk_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", - ] -} - -ohos_unittest("SensorCapiTest") { - module_out_path = "sensors/sensor/interfaces" - - sources = - [ "$SUBSYSTEM_DIR/interfaces/native/test/unittest/sensor_capi_test.cpp" ] - - include_dirs = [ - "$SUBSYSTEM_DIR/interfaces/native/include", - "$SUBSYSTEM_DIR/interfaces/kits/c", "$SUBSYSTEM_DIR/test/unittest/common/include", "$SUBSYSTEM_DIR/utils/common/include", ] -- Gitee From cf6d80ea2412c67a24ad44068bf45a7a06fb3098 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 21 Sep 2023 02:06:39 +0000 Subject: [PATCH 14/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I0cd4b2b102c5f7263f043baecfd618161eecf28d --- interfaces/capi/BUILD.gn | 0 interfaces/capi/sensor_capi.cpp | 0 interfaces/capi/test/unittest/BUILD.gn | 0 interfaces/capi/test/unittest/sensor_capi_test.cpp | 0 4 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 interfaces/capi/BUILD.gn mode change 100755 => 100644 interfaces/capi/sensor_capi.cpp mode change 100755 => 100644 interfaces/capi/test/unittest/BUILD.gn mode change 100755 => 100644 interfaces/capi/test/unittest/sensor_capi_test.cpp diff --git a/interfaces/capi/BUILD.gn b/interfaces/capi/BUILD.gn old mode 100755 new mode 100644 diff --git a/interfaces/capi/sensor_capi.cpp b/interfaces/capi/sensor_capi.cpp old mode 100755 new mode 100644 diff --git a/interfaces/capi/test/unittest/BUILD.gn b/interfaces/capi/test/unittest/BUILD.gn old mode 100755 new mode 100644 diff --git a/interfaces/capi/test/unittest/sensor_capi_test.cpp b/interfaces/capi/test/unittest/sensor_capi_test.cpp old mode 100755 new mode 100644 -- Gitee From dc0b622fd2f68e307f961b301e2c74e6faee100f Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 21 Sep 2023 02:11:55 +0000 Subject: [PATCH 15/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: I12956705bfcda695b3e3c19dcd16e4eabc883173 --- interfaces/capi/sensor_capi.cpp | 10 ++-- .../capi/test/unittest/sensor_capi_test.cpp | 56 +++++++++---------- 2 files changed, 31 insertions(+), 35 deletions(-) mode change 100644 => 100755 interfaces/capi/sensor_capi.cpp mode change 100644 => 100755 interfaces/capi/test/unittest/sensor_capi_test.cpp diff --git a/interfaces/capi/sensor_capi.cpp b/interfaces/capi/sensor_capi.cpp old mode 100644 new mode 100755 index 0fda0516..67234d96 --- a/interfaces/capi/sensor_capi.cpp +++ b/interfaces/capi/sensor_capi.cpp @@ -32,7 +32,7 @@ Sensor_Result OH_Sensor_GetAllSensors(Sensor_SensorInfo **sensorInfo, int32_t *c return ret; } -bool CheckSensorSubscribeId(Sensor_SubscribeId subscribeId) +bool CheckSensorSubscribeId(int32_t sensorTypeId) { int32_t count = -1; Sensor_SensorInfo *sensorInfo = nullptr; @@ -42,15 +42,15 @@ bool CheckSensorSubscribeId(Sensor_SubscribeId subscribeId) } for (int32_t i = 0; i < count; i++) { - if ((sensorInfo + i)->sensorTypeId == subscribeId.sensorTypeId) { + if ((sensorInfo + i)->sensorTypeId == sensorTypeId) { return true; } } return false; } -Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SubscribeId subscribeId, Sensor_SubscribeAttribute attribute, - const Sensor_SubscribeUser *user) +Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SensorTypeId sensorTypeId, Sensor_SubscribeAttribute attribute, + const Sensor_SensorUser *user) { SEN_HILOGD("cff2 OH_Sensor_SubscribeSensor in"); if (!CheckSensorSubscribeId(subscribeId) || attribute.samplingInterval < 0 || attribute.reportInterval < 0) { @@ -66,7 +66,7 @@ Sensor_Result OH_Sensor_SubscribeSensor(Sensor_SubscribeId subscribeId, Sensor_S return ret; } -Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SubscribeId subscribeId, const Sensor_SubscribeUser *user) +Sensor_Result OH_Sensor_UnsubscribeSensor(Sensor_SensorTypeId sensorTypeId, const Sensor_SensorUser *user) { SEN_HILOGD("cff4 OH_Sensor_UnsubscribeSensor in"); if (!CheckSensorSubscribeId(subscribeId)) { diff --git a/interfaces/capi/test/unittest/sensor_capi_test.cpp b/interfaces/capi/test/unittest/sensor_capi_test.cpp old mode 100644 new mode 100755 index a3f9345d..96787e6f --- a/interfaces/capi/test/unittest/sensor_capi_test.cpp +++ b/interfaces/capi/test/unittest/sensor_capi_test.cpp @@ -35,8 +35,8 @@ using Security::AccessToken::AccessTokenID; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorCapiTest" }; -// Sensor_SubscribeId SENSOR_ID = 1; -// Sensor_SubscribeId INVALID_VALUE = -1; +Sensor_SensorTypeId SENSOR_ID = ACCELEROMETER; +Sensor_SensorTypeId INVALID_VALUE = INVALID; PermissionDef g_infoManagerTestPermDef = { .permissionName = "ohos.permission.ACCELEROMETER", @@ -148,60 +148,56 @@ HWTEST_F(SensorCapiTest, OH_Sensor_GetAllSensorsTest_003, TestSize.Level1) HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_001, TestSize.Level1) { SEN_HILOGI("OH_Sensor_GetAllSensors_001 in"); - Sensor_SubscribeUser user; + Sensor_SensorUser user; user.callback = nullptr; - Sensor_SubscribeId subscribeId; struct Sensor_SubscribeAttribute attribute; - Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &user)); + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(SENSOR_ID, attribute, &user)); ASSERT_NE(ret, SENSOR_SUCCESS); } -// HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_002, TestSize.Level1) -// { -// SEN_HILOGI("OH_Sensor_SubscribeSensor_002 in"); -// Sensor_SubscribeUser user; -// user.callback = SensorDataCallbackImpl; -// struct Sensor_SubscribeAttribute attribute; -// Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(INVALID_VALUE, attribute, &user)); -// ASSERT_NE(ret, SENSOR_SUCCESS); -// } +HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_SubscribeSensor_002 in"); + Sensor_SensorUser user; + user.callback = SensorDataCallbackImpl; + struct Sensor_SubscribeAttribute attribute; + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(INVALID_VALUE, attribute, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} HWTEST_F(SensorCapiTest, OH_Sensor_SubscribeSensorTest_003, TestSize.Level1) { SEN_HILOGI("OH_Sensor_SubscribeSensor_003 in"); struct Sensor_SubscribeAttribute attribute; - Sensor_SubscribeId subscribeId; - Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, nullptr)); + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(SENSOR_ID, 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 user; + Sensor_SensorUser user; user.callback = nullptr; struct Sensor_SubscribeAttribute attribute; - Sensor_SubscribeId subscribeId; - Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(subscribeId, attribute, &user)); + Sensor_Result ret = static_cast(OH_Sensor_SubscribeSensor(SENSOR_ID, attribute, &user)); ASSERT_NE(ret, SENSOR_SUCCESS); - ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, &user)); + ret = static_cast(OH_Sensor_UnsubscribeSensor(SENSOR_ID, &user)); ASSERT_NE(ret, SENSOR_SUCCESS); } -// HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_002, TestSize.Level1) -// { -// SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_002 in"); -// Sensor_SubscribeUser user; -// user.callback = SensorDataCallbackImpl; -// Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(INVALID_VALUE, &user)); -// ASSERT_NE(ret, SENSOR_SUCCESS); -// } +HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("OH_Sensor_UnsubscribeSensorTest_002 in"); + Sensor_SensorUser user; + user.callback = SensorDataCallbackImpl; + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(INVALID_VALUE, &user)); + ASSERT_NE(ret, SENSOR_SUCCESS); +} HWTEST_F(SensorCapiTest, OH_Sensor_UnsubscribeSensorTest_003, TestSize.Level1) { SEN_HILOGI("OH_Sensor_UnsubscribeSensor_003 in"); - Sensor_SubscribeId subscribeId; - Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(subscribeId, nullptr)); + Sensor_Result ret = static_cast(OH_Sensor_UnsubscribeSensor(SENSOR_ID, nullptr)); ASSERT_NE(ret, SENSOR_SUCCESS); } } // namespace Sensors -- Gitee From e3e197561cce96f9969935f383500ad1d5976d2b Mon Sep 17 00:00:00 2001 From: cff-gite Date: Wed, 27 Sep 2023 06:46:31 +0000 Subject: [PATCH 16/16] =?UTF-8?q?sensor=20Capi=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite Change-Id: Ib141f01e21a11ed23fc8ee6a8f165ca4542130bd --- interfaces/kits/c/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn index bf610356..ced5c841 100755 --- a/interfaces/kits/c/BUILD.gn +++ b/interfaces/kits/c/BUILD.gn @@ -26,7 +26,6 @@ ohos_ndk_headers("sensor_capi_header") { ohos_ndk_library("libsensor_capi_ndk") { ndk_description_file = "./libsensor_capi.ndk.json" output_name = "sensor_capi_ndk" - output_extension = "so" min_compact_version = "10" system_capability = "SystemCapability.Sensors.Sensor" system_capability_headers = [ -- Gitee