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 <securec.h>
18 #include <string>
19 #include <hdf_base.h>
20 #include "vibrator_uhdf_log.h"
21 #include "vibrator_if.h"
22
23 #define HDF_LOG_TAG uhdf_vibrator_service
24
25 constexpr int32_t VIBRATOR_INTENSITY_STOP = 0;
26
27 namespace OHOS {
28 namespace HDI {
29 namespace Vibrator {
30 namespace V1_1 {
31
Init()32 int32_t VibratorInterfaceImpl::Init()
33 {
34 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
35 if (vibratorInterface == nullptr) {
36 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
37 return HDF_FAILURE;
38 }
39
40 return HDF_SUCCESS;
41 }
42
StartOnce(uint32_t duration)43 int32_t VibratorInterfaceImpl::StartOnce(uint32_t duration)
44 {
45 HDF_LOGI("%{public}s: Enter the StartOnce function, duration is %{public}u", __func__, duration);
46 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
47 if (vibratorInterface == nullptr || vibratorInterface->StartOnce == nullptr) {
48 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
49 return HDF_FAILURE;
50 }
51 int32_t ret = vibratorInterface->StartOnce(duration);
52 if (ret != HDF_SUCCESS) {
53 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
54 }
55 return ret;
56 }
57
Start(const std::string & effectType)58 int32_t VibratorInterfaceImpl::Start(const std::string &effectType)
59 {
60 HDF_LOGI("%{public}s: Enter the Start function", __func__);
61 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
62 if (vibratorInterface == nullptr || vibratorInterface->Start == nullptr) {
63 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
64 return HDF_FAILURE;
65 }
66 int32_t ret = vibratorInterface->Start(effectType.c_str());
67 if (ret != HDF_SUCCESS) {
68 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
69 }
70 return ret;
71 }
72
Stop(HdfVibratorModeVdi mode)73 int32_t VibratorInterfaceImpl::Stop(HdfVibratorModeVdi mode)
74 {
75 HDF_LOGI("%{public}s: Enter the Stop function, mode is %{public}u", __func__, mode);
76 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
77 if (vibratorInterface == nullptr || vibratorInterface->Stop == nullptr) {
78 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
79 return HDF_FAILURE;
80 }
81
82 VibratorMode tmp;
83 if (mode == VDI_VIBRATOR_MODE_ONCE) {
84 tmp = VIBRATOR_MODE_ONCE;
85 } else if (mode == VDI_VIBRATOR_MODE_PRESET) {
86 tmp = VIBRATOR_MODE_PRESET;
87 } else if (mode == VDI_VIBRATOR_MODE_HDHAPTIC) {
88 tmp = VIBRATOR_MODE_HDHAPTIC;
89 } else if (mode == VDI_VIBRATOR_MODE_BUTT) {
90 tmp = VIBRATOR_MODE_BUTT;
91 } else {
92 HDF_LOGE("%{public}s: invalid param", __func__);
93 return HDF_FAILURE;
94 }
95
96 int32_t ret = vibratorInterface->Stop(tmp);
97 if (ret != HDF_SUCCESS) {
98 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
99 }
100 return ret;
101 }
102
GetVibratorInfo(std::vector<HdfVibratorInfoVdi> & vibratorInfo)103 int32_t VibratorInterfaceImpl::GetVibratorInfo(std::vector<HdfVibratorInfoVdi> &vibratorInfo)
104 {
105 HDF_LOGI("%{public}s: Enter the GetVibratorInfo function.", __func__);
106 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
107 if (vibratorInterface == nullptr || vibratorInterface->GetVibratorInfo == nullptr) {
108 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
109 return HDF_FAILURE;
110 }
111 HdfVibratorInfoVdi hdfVibratorInfo;
112 struct VibratorInfo *tmp = nullptr;
113
114 int32_t ret = vibratorInterface->GetVibratorInfo(&tmp);
115 if (ret != HDF_SUCCESS) {
116 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
117 return ret;
118 }
119
120 if (tmp == nullptr) {
121 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
122 return HDF_FAILURE;
123 }
124
125 hdfVibratorInfo.isSupportFrequency = tmp->isSupportFrequency;
126 hdfVibratorInfo.frequencyMaxValue = tmp->frequencyMaxValue;
127 hdfVibratorInfo.frequencyMinValue = tmp->frequencyMinValue;
128 hdfVibratorInfo.isSupportIntensity = tmp->isSupportIntensity;
129 hdfVibratorInfo.intensityMaxValue = tmp->intensityMaxValue;
130 hdfVibratorInfo.intensityMinValue = tmp->intensityMinValue;
131 vibratorInfo.push_back(std::move(hdfVibratorInfo));
132
133 return HDF_SUCCESS;
134 }
135
EnableVibratorModulation(uint32_t duration,uint16_t intensity,int16_t frequency)136 int32_t VibratorInterfaceImpl::EnableVibratorModulation(uint32_t duration, uint16_t intensity, int16_t frequency)
137 {
138 HDF_LOGI("%{public}s: duration is %{public}u, intensity is %{public}u, frequency is %{public}d.",
139 __func__, duration, intensity, frequency);
140 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
141 if (vibratorInterface == nullptr || vibratorInterface->EnableVibratorModulation == nullptr) {
142 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
143 return HDF_FAILURE;
144 }
145
146 int32_t ret = vibratorInterface->EnableVibratorModulation(duration, intensity, frequency);
147 if (ret != HDF_SUCCESS) {
148 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
149 }
150 return ret;
151 }
152
EnableCompositeEffect(const HdfCompositeEffectVdi & effect)153 int32_t VibratorInterfaceImpl::EnableCompositeEffect(const HdfCompositeEffectVdi &effect)
154 {
155 HDF_LOGI("%{public}s: Enter the EnableCompositeEffect function.", __func__);
156 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
157 if (vibratorInterface == nullptr || vibratorInterface->EnableCompositeEffect == nullptr) {
158 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
159 return HDF_FAILURE;
160 }
161
162 return HDF_SUCCESS;
163 }
164
GetEffectInfo(const std::string & effectType,HdfEffectInfoVdi & effectInfo)165 int32_t VibratorInterfaceImpl::GetEffectInfo(const std::string &effectType, HdfEffectInfoVdi &effectInfo)
166 {
167 HDF_LOGI("%{public}s: Enter the GetEffectInfo function.", __func__);
168 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
169 if (vibratorInterface == nullptr || vibratorInterface->GetEffectInfo == nullptr) {
170 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
171 return HDF_FAILURE;
172 }
173
174 EffectInfo info;
175 int32_t ret = vibratorInterface->GetEffectInfo(effectType.c_str(), &info);
176 if (ret != HDF_SUCCESS) {
177 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
178 }
179
180 effectInfo.isSupportEffect = info.isSupportEffect;
181 effectInfo.duration = info.duration;
182
183 return ret;
184 }
185
IsVibratorRunning(bool & state)186 int32_t VibratorInterfaceImpl::IsVibratorRunning(bool& state)
187 {
188 HDF_LOGI("%{public}s: Enter the IsVibratorRunning function, state = %{public}d\n", __func__, state);
189 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
190 if (vibratorInterface == nullptr || vibratorInterface->IsVibratorRunning == nullptr) {
191 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
192 return HDF_FAILURE;
193 }
194
195 bool statePointer = false;
196 int32_t ret = vibratorInterface->IsVibratorRunning(&statePointer);
197 if (ret != HDF_SUCCESS) {
198 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
199 }
200 state = statePointer;
201
202 return ret;
203 }
204
PlayHapticPattern(const HapticPaketVdi & pkgVdi)205 int32_t VibratorInterfaceImpl::PlayHapticPattern(const HapticPaketVdi& pkgVdi)
206 {
207 HDF_LOGI("%{public}s: Enter the PlayHapticPattern function\n", __func__);
208 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
209 if (vibratorInterface == nullptr || vibratorInterface->PlayHapticPattern == nullptr) {
210 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
211 return HDF_FAILURE;
212 }
213
214 return HDF_SUCCESS;
215 }
216
GetHapticCapacity(HapticCapacityVdi & hapticCapacityVdi)217 int32_t VibratorInterfaceImpl::GetHapticCapacity(HapticCapacityVdi& hapticCapacityVdi)
218 {
219 HDF_LOGI("%{public}s: Enter the GetHapticCapacity function\n", __func__);
220 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
221 if (vibratorInterface == nullptr || vibratorInterface->GetHapticCapacity == nullptr) {
222 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
223 return HDF_FAILURE;
224 }
225 struct HapticCapacity hapticCapacity;
226 int32_t ret = vibratorInterface->GetHapticCapacity(&hapticCapacity);
227 if (ret != HDF_SUCCESS) {
228 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
229 return ret;
230 }
231 hapticCapacityVdi.isSupportHdHaptic = hapticCapacity.isSupportHdHaptic;
232 hapticCapacityVdi.isSupportPresetMapping = hapticCapacity.isSupportPresetMapping;
233 hapticCapacityVdi.isSupportTimeDelay = hapticCapacity.isSupportTimeDelay;
234
235 return HDF_SUCCESS;
236 }
237
GetHapticStartUpTime(int32_t mode,int32_t & startUpTime)238 int32_t VibratorInterfaceImpl::GetHapticStartUpTime(int32_t mode, int32_t& startUpTime)
239 {
240 HDF_LOGI("%{public}s: Enter the GetHapticStartUpTime function\n", __func__);
241 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
242 if (vibratorInterface == nullptr || vibratorInterface->GetHapticStartUpTime == nullptr) {
243 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
244 return HDF_FAILURE;
245 }
246 int32_t ret = vibratorInterface->GetHapticStartUpTime(mode, &startUpTime);
247 if (ret != HDF_SUCCESS) {
248 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
249 }
250
251 return ret;
252 }
253
StartByIntensity(const std::string & effectType,uint16_t intensity)254 int32_t VibratorInterfaceImpl::StartByIntensity(const std::string& effectType, uint16_t intensity)
255 {
256 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance();
257 if (vibratorInterface == nullptr) {
258 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__);
259 return HDF_FAILURE;
260 }
261 if (intensity == VIBRATOR_INTENSITY_STOP) {
262 return HDF_SUCCESS;
263 }
264
265 int32_t ret = vibratorInterface->Start(effectType.c_str());
266 if (ret != HDF_SUCCESS) {
267 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
268 }
269
270 return ret;
271 }
272
CreateLightVdiInstance(struct HdfVdiBase * vdiBase)273 static int32_t CreateLightVdiInstance(struct HdfVdiBase *vdiBase)
274 {
275 HDF_LOGI("%{public}s: Enter the CreateLightVdiInstance function", __func__);
276 if (vdiBase == nullptr) {
277 HDF_LOGE("%{public}s parameter vdiBase is NULL", __func__);
278 return HDF_FAILURE;
279 }
280
281 struct VdiWrapperVibrator *vibratorVdi = reinterpret_cast<VdiWrapperVibrator *>(vdiBase);
282 vibratorVdi->vibratorModule = new VibratorInterfaceImpl();
283 if (vibratorVdi->vibratorModule == nullptr) {
284 HDF_LOGI("%{public}s: new vibratorModule failed!", __func__);
285 return HDF_FAILURE;
286 }
287 return HDF_SUCCESS;
288 }
289
DestoryLightVdiInstance(struct HdfVdiBase * vdiBase)290 static int32_t DestoryLightVdiInstance(struct HdfVdiBase *vdiBase)
291 {
292 HDF_LOGI("%{public}s: Enter the DestoryLightVdiInstance function", __func__);
293 if (vdiBase == nullptr) {
294 HDF_LOGE("%{public}s parameter vdiBase is NULL", __func__);
295 return HDF_FAILURE;
296 }
297
298 struct VdiWrapperVibrator *vibratorVdi = reinterpret_cast<VdiWrapperVibrator *>(vdiBase);
299 VibratorInterfaceImpl *vibratorImpl = reinterpret_cast<VibratorInterfaceImpl *>(vibratorVdi->vibratorModule);
300 if (vibratorImpl != nullptr) {
301 delete vibratorImpl;
302 vibratorVdi->vibratorModule = nullptr;
303 }
304 return HDF_SUCCESS;
305 }
306
307 static struct VdiWrapperVibrator g_vibratorVdi = {
308 .base = {
309 .moduleVersion = 1,
310 .moduleName = "vibrator_service",
311 .CreateVdiInstance = CreateLightVdiInstance,
312 .DestoryVdiInstance = DestoryLightVdiInstance,
313 },
314 .vibratorModule = nullptr,
315 };
316
317 extern "C" HDF_VDI_INIT(g_vibratorVdi);
318
319 } // V1_1
320 } // Vibrator
321 } // HDI
322 } // OHOS
323