From e7ad85d9e94d08c3463be294cc158c57aa30e499 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 7 Nov 2023 09:05:35 +0000 Subject: [PATCH] add vibrator capi Signed-off-by: hellohyh001 --- sensors/miscdevice/vibrator/BUILD.gn | 28 ++++++ .../miscdevice/vibrator/include/vibrator.h | 89 ++++++++++++++++++ .../vibrator/include/vibrator_type.h | 91 +++++++++++++++++++ .../miscdevice/vibrator/lib_vibrator.ndk.json | 5 + 4 files changed, 213 insertions(+) create mode 100644 sensors/miscdevice/vibrator/BUILD.gn create mode 100644 sensors/miscdevice/vibrator/include/vibrator.h create mode 100644 sensors/miscdevice/vibrator/include/vibrator_type.h create mode 100644 sensors/miscdevice/vibrator/lib_vibrator.ndk.json diff --git a/sensors/miscdevice/vibrator/BUILD.gn b/sensors/miscdevice/vibrator/BUILD.gn new file mode 100644 index 000000000..ef533e246 --- /dev/null +++ b/sensors/miscdevice/vibrator/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_ndk_headers("ndk_vibrator_header") { + dest_dir = "$ndk_headers_out_dir/sensors/miscdevice/vibrator" + sources = [ + "./include/vibrator.h", + "./include/vibrator_type.h", + ] +} + +ohos_ndk_library("lib_vibrator_ndk") { + output_name = "ohvibrator" + ndk_description_file = "./lib_vibrator.ndk.json" + min_compact_version = "11" +} diff --git a/sensors/miscdevice/vibrator/include/vibrator.h b/sensors/miscdevice/vibrator/include/vibrator.h new file mode 100644 index 000000000..0dd94c320 --- /dev/null +++ b/sensors/miscdevice/vibrator/include/vibrator.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 Vibrator + * @{ + * + * @brief Provides APIs for vibrator services to access the vibrator driver. + * @since 11 + */ + +/** + * @file vibrator.h + * + * @brief Declares the APIs for starting or stopping vibration. + * @library libohvibrator.z.so + * @syscap SystemCapability.Sensors.MiscDevice + * @since 11 + */ + +#ifndef VIBRATOR_H +#define VIBRATOR_H + +#include "vibrator_type.h" + +#ifdef __cplusplus +extern "C" { +#endif + +namespace OHOS { +namespace Sensors { +/** + * @brief Controls the vibrator to vibrate continuously for a given duration. + * + * @param duration - Vibration duration, in milliseconds. + * @param attribute - Vibration attribute. For details, see {@link Vibrator_Attribute}. + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + * For details, see {@link Vibrator_ErrorCode}. + * @permission ohos.permission.VIBRATE + * + * @since 11 + */ +int32_t OH_Vibrator_PlayVibration(int32_t duration, Vibrator_Attribute attribute); + +/** + * @brief Controls the vibrator to vibrate with the custom sequence. + * + * @param fileDescription - File descriptor of the custom vibration effect. + * For details, see {@link Vibrator_FileDescription}. + * @param vibrateAttribute - Vibration attribute. For details, see {@link Vibrator_Attribute}. + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + * For details, see {@link Vibrator_ErrorCode}. + * @permission ohos.permission.VIBRATE + * + * @since 11 + */ +int32_t OH_Vibrator_PlayVibrationCustom(Vibrator_FileDescription fileDescription, + Vibrator_Attribute vibrateAttribute); + +/** + * @brief Stop the motor vibration according to the input mode. + * + * @permission ohos.permission.VIBRATE + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + * For details, see {@link Vibrator_ErrorCode}. + * @permission ohos.permission.VIBRATE + * + * @since 11 + */ +int32_t OH_Vibrator_Cancel(); +} // namespace Sensors +} // namespace OHOS +#ifdef __cplusplus +}; +#endif +/** @} */ +#endif // endif VIBRATOR_H \ No newline at end of file diff --git a/sensors/miscdevice/vibrator/include/vibrator_type.h b/sensors/miscdevice/vibrator/include/vibrator_type.h new file mode 100644 index 000000000..9be871278 --- /dev/null +++ b/sensors/miscdevice/vibrator/include/vibrator_type.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef VIBRATOR_TYPE_H +#define VIBRATOR_TYPE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* @brief Defines an enum that enumerates the error codes. +* +* @since 11 +*/ +typedef enum Vibrator_ErrorCode : int32_t { + /**< Permission verification failed. */ + PERMISSION_DENIED = 201, + /**< Parameter check failed. For example, a mandatory parameter is not passed in, + * or the parameter type passed in is incorrect. */ + PARAMETER_ERROR = 401, + /**< The API is not supported on the device. The device supports the corresponding SysCap, + * but does not support certain APIs in this SysCap. */ + UNSUPPORTED = 801, + /**< The operation on the device failed. */ + DEVICE_OPERATION_FAILED = 14600101, +} Vibrator_ErrorCode; + +/** + * @brief Enumerates vibration usages scenarios. + * + * @since 11 + */ +typedef enum Vibrator_Usage { + VIBRATOR_USAGE_UNKNOWN = 0, /**< Vibration is used for unknown, lowest priority */ + VIBRATOR_USAGE_ALARM = 1, /**< Vibration is used for alarm */ + VIBRATOR_USAGE_RING = 2, /**< Vibration is used for ring */ + VIBRATOR_USAGE_NOTIFICATION = 3, /**< Vibration is used for notification */ + VIBRATOR_USAGE_COMMUNICATION = 4, /**< Vibration is used for communication */ + VIBRATOR_USAGE_TOUCH = 5, /**< Vibration is used for touch */ + VIBRATOR_USAGE_MEDIA = 6, /**< Vibration is used for media */ + VIBRATOR_USAGE_PHYSICAL_FEEDBACK = 7, /**< Vibration is used for physical feedback */ + VIBRATOR_USAGE_SIMULATED_REALITY = 8, /**< Vibration is used for simulate reality */ + VIBRATOR_USAGE_MAX +} Vibrator_Usage; + +/** + * @brief Defines the vibrator attribute. + * + * @since 11 + */ +typedef struct Vibrator_Attribute { + /**< Vibrator ID. */ + int32_t vibratorId; + /**< Vibration scenario. */ + Vibrator_Usage usage; +} Vibrator_Attribute; + +/** + * @brief Defines the vibration file description. + * + * @since 11 + */ +typedef struct Vibrator_FileDescription { + /**< File handle of the custom vibration sequence. */ + int32_t fd; + /**< Offset address of the custom vibration sequence. */ + int64_t offset; + /**< Total length of the custom vibration sequence. */ + int64_t length; +} Vibrator_FileDescription; +/** @} */ +#ifdef __cplusplus +}; +#endif + +#endif // endif VIBRATOR_TYPE_H \ No newline at end of file diff --git a/sensors/miscdevice/vibrator/lib_vibrator.ndk.json b/sensors/miscdevice/vibrator/lib_vibrator.ndk.json new file mode 100644 index 000000000..0ac0e1c33 --- /dev/null +++ b/sensors/miscdevice/vibrator/lib_vibrator.ndk.json @@ -0,0 +1,5 @@ +[ + { "name": "OH_Vibrator_PlayVibration" }, + { "name": "OH_Vibrator_PlayVibrationCustom" }, + { "name": "OH_Vibrator_Cancel" } +] \ No newline at end of file -- Gitee