1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "vibrator_interface_impl.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include "vibrator_if.h"
20
21 #define HDF_LOG_TAG uhdf_vibrator_service
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Vibrator {
26 namespace V1_1 {
VibratorInterfaceImplGetInstance(void)27 extern "C" IVibratorInterface *VibratorInterfaceImplGetInstance(void)
28 {
29 return new (std::nothrow) VibratorInterfaceImpl();
30 }
31
StartOnce(uint32_t duration)32 int32_t VibratorInterfaceImpl::StartOnce(uint32_t duration)
33 {
34 HDF_LOGI("%{public}s: Enter the StartOnce function, duration is %{public}u", __func__, duration);
35 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
36 if (vibratorInterface == nullptr || vibratorInterface->StartOnce == nullptr) {
37 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
38 return HDF_FAILURE;
39 }
40 int32_t ret = vibratorInterface->StartOnce(duration);
41 if (ret != HDF_SUCCESS) {
42 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
43 }
44 return ret;
45 }
46
Start(const std::string & effectType)47 int32_t VibratorInterfaceImpl::Start(const std::string &effectType)
48 {
49 HDF_LOGI("%{public}s: Enter the Start function", __func__);
50 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
51 if (vibratorInterface == nullptr || vibratorInterface->Start == nullptr) {
52 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
53 return HDF_FAILURE;
54 }
55 int32_t ret = vibratorInterface->Start(effectType.c_str());
56 if (ret != HDF_SUCCESS) {
57 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
58 }
59 return ret;
60 }
61
Stop(HdfVibratorMode mode)62 int32_t VibratorInterfaceImpl::Stop(HdfVibratorMode mode)
63 {
64 HDF_LOGI("%{public}s: Enter the Stop function, mode is %{public}u", __func__, mode);
65 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
66 if (vibratorInterface == nullptr || vibratorInterface->Stop == nullptr) {
67 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
68 return HDF_FAILURE;
69 }
70
71 VibratorMode tmp;
72 if (mode == HDF_VIBRATOR_MODE_ONCE) {
73 tmp = VIBRATOR_MODE_ONCE;
74 } else if (mode == HDF_VIBRATOR_MODE_PRESET) {
75 tmp = VIBRATOR_MODE_PRESET;
76 } else if (mode == HDF_VIBRATOR_MODE_BUTT) {
77 tmp = VIBRATOR_MODE_BUTT;
78 } else {
79 HDF_LOGE("%{public}s: invalid param", __func__);
80 return HDF_FAILURE;
81 }
82
83 int32_t ret = vibratorInterface->Stop(tmp);
84 if (ret != HDF_SUCCESS) {
85 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
86 }
87 return ret;
88 }
89
GetVibratorInfo(std::vector<HdfVibratorInfo> & vibratorInfo)90 int32_t VibratorInterfaceImpl::GetVibratorInfo(std::vector<HdfVibratorInfo> &vibratorInfo)
91 {
92 HDF_LOGI("%{public}s: Enter the GetVibratorInfo function.", __func__);
93 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
94 if (vibratorInterface == nullptr || vibratorInterface->GetVibratorInfo == nullptr) {
95 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
96 return HDF_FAILURE;
97 }
98 HdfVibratorInfo hdfVibratorInfo;
99 struct VibratorInfo *tmp = nullptr;
100
101 int32_t ret = vibratorInterface->GetVibratorInfo(&tmp);
102 if (ret != HDF_SUCCESS) {
103 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
104 return ret;
105 }
106
107 if (tmp == nullptr) {
108 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
109 return HDF_FAILURE;
110 }
111
112 hdfVibratorInfo.isSupportFrequency = tmp->isSupportFrequency;
113 hdfVibratorInfo.frequencyMaxValue = tmp->frequencyMaxValue;
114 hdfVibratorInfo.frequencyMinValue = tmp->frequencyMinValue;
115 hdfVibratorInfo.isSupportIntensity = tmp->isSupportIntensity;
116 hdfVibratorInfo.intensityMaxValue = tmp->intensityMaxValue;
117 hdfVibratorInfo.intensityMinValue = tmp->intensityMinValue;
118 vibratorInfo.push_back(std::move(hdfVibratorInfo));
119
120 return HDF_SUCCESS;
121 }
122
EnableVibratorModulation(uint32_t duration,uint16_t intensity,int16_t frequency)123 int32_t VibratorInterfaceImpl::EnableVibratorModulation(uint32_t duration, uint16_t intensity, int16_t frequency)
124 {
125 HDF_LOGI("%{public}s: duration is %{public}u, intensity is %{public}u, frequency is %{public}d.",
126 __func__, duration, intensity, frequency);
127 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
128 if (vibratorInterface == nullptr || vibratorInterface->EnableVibratorModulation == nullptr) {
129 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
130 return HDF_FAILURE;
131 }
132
133 int32_t ret = vibratorInterface->EnableVibratorModulation(duration, intensity, frequency);
134 if (ret != HDF_SUCCESS) {
135 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
136 }
137 return ret;
138 }
139
EnableCompositeEffect(const HdfCompositeEffect & effect)140 int32_t VibratorInterfaceImpl::EnableCompositeEffect(const HdfCompositeEffect &effect)
141 {
142 HDF_LOGI("%{public}s: Enter the EnableCompositeEffect function.", __func__);
143 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
144 if (vibratorInterface == nullptr || vibratorInterface->EnableCompositeEffect == nullptr) {
145 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
146 return HDF_FAILURE;
147 }
148
149 return HDF_SUCCESS;
150 }
151
GetEffectInfo(const std::string & effectType,HdfEffectInfo & effectInfo)152 int32_t VibratorInterfaceImpl::GetEffectInfo(const std::string &effectType, HdfEffectInfo &effectInfo)
153 {
154 HDF_LOGI("%{public}s: Enter the GetEffectInfo function.", __func__);
155 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
156 if (vibratorInterface == nullptr || vibratorInterface->GetEffectInfo == nullptr) {
157 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
158 return HDF_FAILURE;
159 }
160
161 return HDF_SUCCESS;
162 }
163
IsVibratorRunning(bool & state)164 int32_t VibratorInterfaceImpl::IsVibratorRunning(bool& state)
165 {
166 HDF_LOGI("%{public}s: Enter the IsVibratorRunning function, state = %{public}d\n", __func__, state);
167 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
168 if (vibratorInterface == nullptr || vibratorInterface->IsVibratorRunning == nullptr) {
169 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
170 return HDF_FAILURE;
171 }
172
173 return HDF_SUCCESS;
174 }
175
176 } // V1_1
177 } // Vibrator
178 } // HDI
179 } // OHOS
180