diff --git a/bundle.json b/bundle.json index 5aa9dca91891fe2cbf00f8a33371e88c4c4acae4..fba1c3670be28e1fe8b5fa87f7487698b056d05e 100644 --- a/bundle.json +++ b/bundle.json @@ -45,7 +45,8 @@ "//base/sensors/miscdevice/frameworks/js/napi:vibrator_js_target", "//base/sensors/miscdevice/frameworks/native/vibrator:vibrator_target", "//base/sensors/miscdevice/frameworks/native/light:light_target", - "//base/sensors/miscdevice/utils:miscdevice_utils_target" + "//base/sensors/miscdevice/utils:miscdevice_utils_target", + "//base/sensors/miscdevice/frameworks/capi:vibrator_ndk" ], "service_group": [ "//base/sensors/miscdevice/services/miscdevice_service:miscdevice_service_target", @@ -72,6 +73,16 @@ ], "header_base": "//base/sensors/miscdevice/interfaces/inner_api/light" } + }, + { + "name": "//base/sensors/miscdevice/frameworks/capi:vibrator_ndk", + "header": { + "header_files": [ + "vibrator_type.h", + "vibrator.h" + ], + "header_base": "//base/sensors/miscdevice/interfaces/kits/c" + } } ], "test": [ @@ -79,6 +90,7 @@ "//base/sensors/miscdevice/test/fuzztest/vibrator:fuzztest", "//base/sensors/miscdevice/test/fuzztest/light:fuzztest", "//base/sensors/miscdevice/test/unittest/vibrator/native:unittest", + "//base/sensors/miscdevice/test/unittest/vibrator/capi:unittest", "//base/sensors/miscdevice/test/unittest/light:unittest", "//base/sensors/miscdevice/test/fuzztest/service:fuzztest" ] diff --git a/frameworks/capi/BUILD.gn b/frameworks/capi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..61418cfd3e96574cf99e4ba28fc1cff1566713b2 --- /dev/null +++ b/frameworks/capi/BUILD.gn @@ -0,0 +1,38 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("./../../miscdevice.gni") + +ohos_shared_library("vibrator_ndk") { + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/interfaces/inner_api/vibrator", + "$SUBSYSTEM_DIR/utils/include", + ] + sources = [ "vibrator.cpp" ] + + defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] + + deps = + [ "$SUBSYSTEM_DIR/frameworks/native/vibrator:vibrator_interface_native" ] + + external_deps = [ + #"c_utils:utils", + "hilog:libhilog", + ] + + relative_install_dir = "ndk" + part_name = "miscdevice" + subsystem_name = "sensors" +} diff --git a/frameworks/capi/vibrator.cpp b/frameworks/capi/vibrator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1f80c2771f55f439862db3bc068840185afef73 --- /dev/null +++ b/frameworks/capi/vibrator.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "vibrator.h" +#include "vibrator_type.h" +#include "vibrator_agent.h" +#include "miscdevice_log.h" + +namespace OHOS { +namespace Sensors { +using OHOS::HiviewDFX::HiLog; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VIBRATOR" }; +} // namespace + +int32_t OH_Vibrator_PlayVibration(int32_t duration, VibrateAttribute vibrateAttribute) +{ + if (duration <= 0) { + MISC_HILOGE("duration is invalid, duration is %{public}d", duration); + return PARAMETER_ERROR; + } + if (vibrateAttribute.usage < USAGE_UNKNOWN || vibrateAttribute.usage > USAGE_MAX) { + MISC_HILOGE("vibrate attribute value is invalid"); + return PARAMETER_ERROR; + } + if (!SetUsage(vibrateAttribute.usage)) { + MISC_HILOGE("SetUsage fail"); + return PARAMETER_ERROR; + } + int32_t ret = StartVibratorOnce(duration); + if (ret != OHOS::ERR_OK) { + MISC_HILOGE("start Vibrator failed, ret is %{public}d", ret); + } + return ret; +} + +int32_t OH_Vibrator_PlayVibrationCustom(VibrateFileDescription fileDescription, + VibrateAttribute vibrateAttribute) +{ + if (!IsSupportVibratorCustom()) { + MISC_HILOGD("feature is not supported"); + return IS_NOT_SUPPORTED; + } + if (fileDescription.fd < 0 || fileDescription.offset < 0 || fileDescription.length <= 0) { + MISC_HILOGE("fileDescription is invalid"); + return PARAMETER_ERROR; + } + if (vibrateAttribute.usage < USAGE_UNKNOWN || vibrateAttribute.usage > USAGE_MAX) { + MISC_HILOGE("vibrate attribute value is invalid"); + return PARAMETER_ERROR; + } + if (!SetUsage(vibrateAttribute.usage)) { + MISC_HILOGE("SetUsage fail"); + return PARAMETER_ERROR; + } + int32_t ret = PlayVibratorCustom(fileDescription.fd, fileDescription.offset, fileDescription.length); + if (ret != OHOS::ERR_OK) { + MISC_HILOGE("play vibrator custom failed, ret is %{public}d", ret); + } + return ret; +} + +int32_t OH_Vibrator_Cancel() +{ + int32_t ret = Cancel(); + if (ret != OHOS::ERR_OK) { + MISC_HILOGE("stop Vibrator failed, ret is %{public}d", ret); + } + return ret; +} +} +} \ No newline at end of file diff --git a/interfaces/inner_api/vibrator/vibrator_agent_type.h b/interfaces/inner_api/vibrator/vibrator_agent_type.h index 8db23c24a60f997bf343181e4b744f1ba9cfb2fe..9c34d77e61df9c393abe51f629c8ff5f10a89e40 100644 --- a/interfaces/inner_api/vibrator/vibrator_agent_type.h +++ b/interfaces/inner_api/vibrator/vibrator_agent_type.h @@ -25,56 +25,70 @@ extern "C" { * * @since 1 */ -const char *VIBRATOR_TYPE_CLOCK_TIMER = "haptic.clock.timer"; +static const char *VIBRATOR_TYPE_CLOCK_TIMER = "haptic.clock.timer"; /** * @brief Describes the vibration effect of the vibrator when authentication fails. * * @since 11 */ -const char *VIBRATOR_TYPE_FAIL = "haptic.fail"; +static const char *VIBRATOR_TYPE_FAIL = "haptic.fail"; /** * @brief Describes the vibration effect of the vibrator when charging. * * @since 11 */ -const char *VIBRATOR_TYPE_CHARGING = "haptic.charging"; +static const char *VIBRATOR_TYPE_CHARGING = "haptic.charging"; /** * @brief Describes the vibration effect of the vibrator when long pressed. * * @since 11 */ -const char *VIBRATOR_TYPE_LONG_PRESS_LIGHT = "haptic.long_press.light"; +static const char *VIBRATOR_TYPE_LONG_PRESS_LIGHT = "haptic.long_press.light"; /** * @brief Describes the vibration effect of the vibrator when long pressed. * * @since 11 */ -const char *VIBRATOR_TYPE_LONG_PRESS_MEDIUM = "haptic.long_press.medium"; +static const char *VIBRATOR_TYPE_LONG_PRESS_MEDIUM = "haptic.long_press.medium"; /** * @brief Describes the vibration effect of the vibrator when long pressed. * * @since 11 */ -const char *VIBRATOR_TYPE_LONG_PRESS_HEAVY = "haptic.long_press.heavy"; +static const char *VIBRATOR_TYPE_LONG_PRESS_HEAVY = "haptic.long_press.heavy"; /** * @brief Describes the vibration effect of the vibrator when slide. * * @since 11 */ -const char *VIBRATOR_TYPE_SLIDE_LIGHT = "haptic.slide.light"; +static const char *VIBRATOR_TYPE_SLIDE_LIGHT = "haptic.slide.light"; /** * @brief Describes the vibration effect of the vibrator when the threshold is reached. * * @since 11 */ -const char *VIBRATOR_TYPE_THRESHOID = "haptic.threshold"; +static const char *VIBRATOR_TYPE_THRESHOID = "haptic.threshold"; + +/** + * @brief Describes the vibration effect of the vibrator when slide. + * + * @since 11 + */ +static const char *VIBRATOR_TYPE_SLIDE_LIGHT = "haptic.slide.light"; + +/** + * @brief Describes the vibration effect of the vibrator when the threshold is reached. + * + * @since 11 + */ +static const char *VIBRATOR_TYPE_THRESHOID = "haptic.threshold"; /** * @brief Enumerates vibration usages. diff --git a/interfaces/kits/c/BUILD.gn b/interfaces/kits/c/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..08e3ff7b67fc9f211163de324df67b78ff40a006 --- /dev/null +++ b/interfaces/kits/c/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos/ndk/ndk.gni") +import("./../../../miscdevice.gni") + +ohos_ndk_headers("ndk_vibrator_header") { + dest_dir = "$ndk_headers_out_dir/database/vibrator/" + sources = [ + "$SUBSYSTEM_DIR/interfaces/inner_api/vibrator", + "./vibrator.h", + "./vibrator_type.h", + ] +} + +ohos_ndk_library("lib_vibrator_ndk") { + output_name = "vibrator_ndk" + system_capability = "SystemCapability.Sensors.MiscDevice" + ndk_description_file = "./lib_vibrator.ndk.json" + min_compact_version = "10" + system_capability_headers = [ + "$ndk_headers_out_dir/miscdevice/vibrator.h", + "$ndk_headers_out_dir/miscdevice/vibrator_type.h", + ] +} diff --git a/interfaces/kits/c/lib_vibrator.ndk.json b/interfaces/kits/c/lib_vibrator.ndk.json new file mode 100644 index 0000000000000000000000000000000000000000..0ac0e1c336d6c25f8a99f4490e6d6a33738772a6 --- /dev/null +++ b/interfaces/kits/c/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 diff --git a/interfaces/kits/c/vibrator.h b/interfaces/kits/c/vibrator.h new file mode 100644 index 0000000000000000000000000000000000000000..892ec4c3cbe5ce2b7a93a0fa0ea8864cfe9837cc --- /dev/null +++ b/interfaces/kits/c/vibrator.h @@ -0,0 +1,83 @@ +/* + * 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. + * @since 11 + */ + +#ifndef VIBRATOR_H +#include +#include "vibrator_type.h" +#define VIBRATOR_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 VibrateAttribute}. + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + * For details, see {@link VibratorErrorCode}. + * + * @since 11 + */ +int32_t OH_Vibrator_PlayVibration(int32_t duration, VibrateAttribute attribute); + +/** + * @brief Controls the vibrator to vibrate with the custom sequence. + * + * @param fileDescription File descriptor of the custom vibration effect. + * For details, see {@link VibrateFileDescription}. + * @param vibrateAttribute Vibration attribute. For details, see {@link VibrateAttribute}. + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + * For details, see {@link VibratorErrorCode}. + * + * @since 11 + */ +int32_t OH_Vibrator_PlayVibrationCustom(VibrateFileDescription fileDescription, + VibrateAttribute vibrateAttribute); + +/** + * @brief Stop the motor vibration according to the input mode. + * + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + * For details, see {@link VibratorErrorCode}. + * + * @since 11 + */ +int32_t OH_Vibrator_Cancel(); +} // namespace Sensors +} // namespace OHOS +#ifdef __cplusplus +}; +#endif +/** @} */ +#endif // endif VIBRATOR_H diff --git a/interfaces/kits/c/vibrator_type.h b/interfaces/kits/c/vibrator_type.h new file mode 100644 index 0000000000000000000000000000000000000000..85b9befb7bcba365ab8a724048d95fb151d2bc0e --- /dev/null +++ b/interfaces/kits/c/vibrator_type.h @@ -0,0 +1,72 @@ +/* + * 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 +#include "vibrator_agent_type.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief 为用户定义错误码。 + * + * @since 11 + */ +typedef enum VibratorErrorCode : int32_t { + /**< 权限校验失败。 */ + PERMISSION_DENIED = 201, + /**< 参数检查失败,包括必选参数没有传入,参数类型错误等。 */ + PARAMETER_ERROR = 401, + /**< 设备不支持该API,通常用于在设备已支持该针对其少量的API支持处理。 */ + IS_NOT_SUPPORTED = 801, + /**< 设备操作失败。 */ + DEVICE_OPERATION_FAILED = 14600101, +} VibratorErrorCode; + +/** + * @brief Defines the vibrator attribute. + * + * @since 11 + */ +typedef struct VibrateAttribute { + /**< Vibrator ID. */ + int32_t vibratorId; + /**< Vibration scenario. */ + VibratorUsage usage; +} VibrateAttribute; + +/** + * @brief Defines the vibration file description. + * + * @since 11 + */ +typedef struct VibrateFileDescription { + /**< 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; +} VibrateFileDescription; +/** @} */ +#ifdef __cplusplus +}; +#endif + +#endif // endif VIBRATOR_TYPE_H diff --git a/test/unittest/vibrator/capi/BUILD.gn b/test/unittest/vibrator/capi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..cfd3e1ec72f2d496d9b4df816acf2c3cc93e8ce1 --- /dev/null +++ b/test/unittest/vibrator/capi/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//build/test.gni") +import("./../../../../miscdevice.gni") + +module_output_path = "miscdevice/vibrator" + +############################################################################### + +ohos_unittest("VibratorCapiTest") { + module_out_path = module_output_path + + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/interfaces/inner_api/vibrator", + "$SUBSYSTEM_DIR/utils/include", + ] + + sources = [ + "$SUBSYSTEM_DIR/frameworks/capi/vibrator.cpp", + "vibrator_test.cpp", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/capi:vibrator_ndk", + "$SUBSYSTEM_DIR/frameworks/native/vibrator:vibrator_interface_native", + "$SUBSYSTEM_DIR/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/googletest:gtest_main", + ] + if (miscdevice_feature_vibrator_custom) { + resource_config_file = + "$SUBSYSTEM_DIR/frameworks/capi/test/unittest/resource/ohos_test.xml" + } +} + +############################################################################### +group("unittest") { + testonly = true + deps = [ ":VibratorCapiTest" ] +} diff --git a/test/unittest/vibrator/capi/resource/json_file/test_128_event.json b/test/unittest/vibrator/capi/resource/json_file/test_128_event.json new file mode 100644 index 0000000000000000000000000000000000000000..0334faf90e368db0c115ca3ec534f40179c22a7d --- /dev/null +++ b/test/unittest/vibrator/capi/resource/json_file/test_128_event.json @@ -0,0 +1,1377 @@ +{ + "MetaData": { + "Create": "2023-01-09", + "Description": "a haptic testcase", + "Version": 1.0, + "ChannelNumber": 1 + }, + "Channels": [ + { + "Parameters": { + "Index": 1 + }, + "Pattern": [ + { + "Event": { + "Type": "continuous", + "StartTime": 0, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 100, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 150, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 250, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 300, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 350, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 400, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 450, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 500, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 550, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 600, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 650, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 700, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 750, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 800, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 850, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 900, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 950, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1000, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1050, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1100, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1150, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1250, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1300, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1350, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1400, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1450, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1500, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1550, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1600, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1650, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1700, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1750, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1800, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1850, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 1900, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 1950, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2000, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2050, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2100, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2150, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2250, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2300, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2350, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2400, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2450, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2500, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2550, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2600, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2650, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2700, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2750, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2800, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2850, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 2900, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 2950, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3000, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3050, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3100, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3150, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3250, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3300, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3350, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3400, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3450, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3500, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3550, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3600, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3650, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3700, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3750, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3800, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3850, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 3900, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 3950, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4000, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4050, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4100, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4150, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4250, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4300, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4350, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4400, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4450, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4500, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4550, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4600, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4650, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4700, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4750, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4800, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4850, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 4900, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 4950, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5000, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5050, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5100, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5150, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5250, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5300, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5350, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5400, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5450, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5500, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5550, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5600, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5650, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5700, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5750, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5800, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5850, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 5900, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 5950, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 6000, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 6050, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 6100, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 6150, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 6200, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 6250, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "transient", + "StartTime": 6300, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + }, + { + "Event": { + "Type": "continuous", + "StartTime": 6350, + "Duration": 50, + "Parameters": { + "Intensity": 100, + "Frequency": 50 + } + } + } + ] + } + ] +} \ No newline at end of file diff --git a/test/unittest/vibrator/capi/resource/ohos_test.xml b/test/unittest/vibrator/capi/resource/ohos_test.xml new file mode 100644 index 0000000000000000000000000000000000000000..bcb79b15d889c9911c0138c799ae33c8c44ccc50 --- /dev/null +++ b/test/unittest/vibrator/capi/resource/ohos_test.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/test/unittest/vibrator/capi/vibrator_test.cpp b/test/unittest/vibrator/capi/vibrator_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d81cc9077ee2aa8b8ac4fb4066d1789d7129f3b --- /dev/null +++ b/test/unittest/vibrator/capi/vibrator_test.cpp @@ -0,0 +1,170 @@ +/* + * 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 + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "parameters.h" +#include "token_setproc.h" + +#include "sensors_errors.h" +#include "vibrator.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; + +namespace { +std::string g_deviceType; +constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "NativeVibratorTest" }; +PermissionDef infoManagerTestPermDef_ = { + .permissionName = "ohos.permission.VIBRATE", + .bundleName = "accesstoken_test", + .grantMode = 1, + .label = "label", + .labelId = 1, + .description = "native vibrator test", + .descriptionId = 1, + .availableLevel = APL_NORMAL +}; + +PermissionStateFull infoManagerTestState_ = { + .grantFlags = {1}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .isGeneral = true, + .permissionName = "ohos.permission.VIBRATE", + .resDeviceID = {"local"} +}; + +HapPolicyParams infoManagerTestPolicyPrams_ = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {infoManagerTestPermDef_}, + .permStateList = {infoManagerTestState_} +}; + +HapInfoParams infoManagerTestInfoParms_ = { + .bundleName = "vibratoragent_test", + .userID = 1, + .instIndex = 0, + .appIDDesc = "NativeVibratorTest" +}; +} // namespace + +class NativeVibratorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + +private: + static AccessTokenID tokenID_; +}; + +AccessTokenID NativeVibratorTest::tokenID_ = 0; + +void NativeVibratorTest::SetUpTestCase() +{ + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_); + tokenID_ = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(0, tokenID_); + ASSERT_EQ(0, SetSelfTokenID(tokenID_)); + g_deviceType = OHOS::system::GetDeviceType(); + MISC_HILOGI("deviceType:%{public}s", g_deviceType.c_str()); +} + +void NativeVibratorTest::TearDownTestCase() +{ + int32_t ret = AccessTokenKit::DeleteToken(tokenID_); + if (tokenID_ != 0) { + ASSERT_EQ(RET_SUCCESS, ret); + } +} + +void NativeVibratorTest::SetUp() +{} + +void NativeVibratorTest::TearDown() +{} + +HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_001, TestSize.Level1) +{ + CALL_LOG_ENTER; + VibrateAttribute vibrateAttribute; + vibrateAttribute.usage = USAGE_ALARM; + int32_t ret = OH_Vibrator_PlayVibration(3000, vibrateAttribute); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + MISC_HILOGI("ret is %d\n\t", ret); + ASSERT_EQ(ret, RET_SUCCESS); +} + +HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_002, TestSize.Level1) +{ + CALL_LOG_ENTER; + VibrateAttribute vibrateAttribute; + vibrateAttribute.usage = USAGE_ALARM; + int32_t ret = OH_Vibrator_PlayVibration(0, vibrateAttribute); + MISC_HILOGI("ret is %d\n\t", ret); + ASSERT_EQ(ret, PARAMETER_ERROR); +} + +HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_001, TestSize.Level1) +{ + CALL_LOG_ENTER; + int32_t fd = open("/data/test/vibrator/test_128_event.json", O_RDONLY); + MISC_HILOGD("Test fd:%{public}d", fd); + VibrateFileDescription fileDescription; + struct stat64 statbuf = { 0 }; + fileDescription.fd = fd; + fileDescription.offset = 0; + fileDescription.length = statbuf.st_size; + VibrateAttribute vibrateAttribute; + vibrateAttribute.usage = USAGE_RING; + + int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute); + MISC_HILOGI("ret is %d\n\t", ret); + ASSERT_EQ(ret, IS_NOT_SUPPORTED); +} + +HWTEST_F(NativeVibratorTest, OH_Vibrator_CancelTest_001, TestSize.Level1) +{ + CALL_LOG_ENTER; + VibrateAttribute vibrateAttribute; + vibrateAttribute.usage = USAGE_RING; + int32_t ret = OH_Vibrator_PlayVibration(3000, vibrateAttribute); + ASSERT_EQ(ret, RET_SUCCESS); + ret = OH_Vibrator_Cancel(); + MISC_HILOGI("ret is %d\n\t", ret); + ASSERT_EQ(ret, RET_SUCCESS); +} + +HWTEST_F(NativeVibratorTest, OH_Vibrator_CancelTest_002, TestSize.Level1) +{ + CALL_LOG_ENTER; + int32_t ret = OH_Vibrator_Cancel(); + MISC_HILOGI("ret is %d\n\t", ret); + ASSERT_NE(ret, RET_SUCCESS); +} + +} // namespace Sensors +} // namespace OHOS