• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_if_service.h"
17 #include <hdf_base.h>
18 #include "vibrator_uhdf_log.h"
19 #include "hitrace_meter.h"
20 
21 #define HDF_LOG_TAG    "uhdf_vibrator_service"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Vibrator {
26 namespace V2_0 {
27 #define DEFAULT_DEVICE_ID (-1)
28 #define DEFAULT_VIBRATOR_ID 1
29 #define DEFAULT_POSITION 0
30 #define DEFAULT_IS_LOCAL 1
31 
VibratorIfService()32 VibratorIfService::VibratorIfService()
33 {
34     int32_t ret = GetVibratorVdiImpl();
35     if (ret != HDF_SUCCESS) {
36         HDF_LOGE("%{public}s: get vibrator vdi instance failed", __func__);
37     }
38 }
39 
~VibratorIfService()40 VibratorIfService::~VibratorIfService()
41 {
42     if (vdi_ != nullptr) {
43         HdfCloseVdi(vdi_);
44     }
45 }
46 
47 
GetVibratorVdiImpl()48 int32_t VibratorIfService::GetVibratorVdiImpl()
49 {
50     struct OHOS::HDI::Vibrator::V1_1::VdiWrapperVibrator *vdiWrapperVibrator = nullptr;
51     uint32_t version = 0;
52     vdi_ = HdfLoadVdi(HDI_VIBRATOR_VDI_LIBNAME);
53     if (vdi_ == nullptr || vdi_->vdiBase == nullptr) {
54         HDF_LOGE("%{public}s: load vibrator vdi failed", __func__);
55         return HDF_FAILURE;
56     }
57 
58     version = HdfGetVdiVersion(vdi_);
59     if (version != 1) {
60         HDF_LOGE("%{public}s: get vibrator vdi version failed", __func__);
61         return HDF_FAILURE;
62     }
63 
64     vdiWrapperVibrator = reinterpret_cast<struct OHOS::HDI::Vibrator::V1_1::VdiWrapperVibrator *>(vdi_->vdiBase);
65     vibratorVdiImplV1_1_ = vdiWrapperVibrator->vibratorModule;
66     bool ret = vibratorVdiImplV1_1_ == nullptr;
67     if (ret) {
68         HDF_LOGE("%{public}s: get vibrator impl failed", __func__);
69         return HDF_FAILURE;
70     }
71 
72     return HDF_SUCCESS;
73 }
74 
Init()75 int32_t VibratorIfService::Init()
76 {
77     if (vibratorVdiImplV1_1_ == nullptr) {
78         HDF_LOGE("%{public}s: vibratorVdiImplV1_1_ is nullptr", __func__);
79         return HDF_FAILURE;
80     }
81     int32_t ret = vibratorVdiImplV1_1_->Init();
82     if (ret != HDF_SUCCESS) {
83         HDF_LOGE("%{public}s Init failed, error code is %{public}d", __func__, ret);
84     }
85 
86     return ret;
87 }
88 
89 //version 2.0 interface
StartOnce(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,uint32_t duration)90 int32_t VibratorIfService::StartOnce(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
91                                      uint32_t duration)
92 {
93     HDF_LOGD("%{public}s: Enter the StartOnce function duration is %{public}u", __func__, duration);
94 
95     StartTrace(HITRACE_TAG_HDF, "StartOnce");
96 #ifdef TV_FLAG
97     int32_t ret = vibratorVdiImplV1_1_->StartOnce(deviceVibratorInfo, duration);
98 #else
99     int32_t ret = vibratorVdiImplV1_1_->StartOnce(duration);
100 #endif
101     FinishTrace(HITRACE_TAG_HDF);
102 
103     if (ret != HDF_SUCCESS) {
104         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
105                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
106     }
107 
108     return ret;
109 }
110 
Start(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,const std::string & effectType)111 int32_t VibratorIfService::Start(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
112                                  const std::string &effectType)
113 {
114     HDF_LOGD("%{public}s: Enter the Start function", __func__);
115 
116     StartTrace(HITRACE_TAG_HDF, "Start");
117 #ifdef TV_FLAG
118     int32_t ret = vibratorVdiImplV1_1_->Start(deviceVibratorInfo, effectType);
119 #else
120     int32_t ret = vibratorVdiImplV1_1_->Start(effectType);
121 #endif
122     FinishTrace(HITRACE_TAG_HDF);
123 
124     if (ret != HDF_SUCCESS) {
125         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
126                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
127     }
128 
129     return ret;
130 }
131 
Stop(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,V2_0::HdfVibratorMode mode)132 int32_t VibratorIfService::Stop(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
133                                 V2_0::HdfVibratorMode mode)
134 {
135     HDF_LOGD("%{public}s: Enter the Stop function, mode: %{public}d", __func__, mode);
136 
137     HdfVibratorModeVdi vibratorMode;
138     if (mode == HdfVibratorMode::HDF_VIBRATOR_MODE_ONCE) {
139         vibratorMode = VDI_VIBRATOR_MODE_ONCE;
140     } else if (mode == HdfVibratorMode::HDF_VIBRATOR_MODE_PRESET) {
141         vibratorMode = VDI_VIBRATOR_MODE_PRESET;
142     } else if (mode == HdfVibratorMode::HDF_VIBRATOR_MODE_HDHAPTIC) {
143         vibratorMode = VDI_VIBRATOR_MODE_HDHAPTIC;
144     } else if (mode == HdfVibratorMode::HDF_VIBRATOR_MODE_BUTT) {
145         vibratorMode = VDI_VIBRATOR_MODE_BUTT;
146     } else {
147         HDF_LOGE("%{public}s: invalid param", __func__);
148         return HDF_FAILURE;
149     }
150 
151     StartTrace(HITRACE_TAG_HDF, "Stop");
152 #ifdef TV_FLAG
153     int32_t ret = vibratorVdiImplV1_1_->Stop(deviceVibratorInfo, vibratorMode);
154 #else
155     int32_t ret = vibratorVdiImplV1_1_->Stop(vibratorMode);
156 #endif
157     FinishTrace(HITRACE_TAG_HDF);
158 
159     if (ret != HDF_SUCCESS) {
160         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
161                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
162     }
163 
164     return ret;
165 }
166 
GetVibratorInfo(std::vector<V2_0::HdfVibratorInfo> & vibratorInfo)167 int32_t VibratorIfService::GetVibratorInfo(std::vector<V2_0::HdfVibratorInfo> &vibratorInfo)
168 {
169     HDF_LOGD("%{public}s: Enter the GetVibratorInfo function.", __func__);
170 
171     StartTrace(HITRACE_TAG_HDF, "GetVibratorInfo");
172     std::vector<OHOS::HDI::Vibrator::V1_1::HdfVibratorInfoVdi> vibratorInfoVdi;
173     int32_t ret = vibratorVdiImplV1_1_->GetVibratorInfo(vibratorInfoVdi);
174     FinishTrace(HITRACE_TAG_HDF);
175 
176     if (ret != HDF_SUCCESS) {
177         HDF_LOGE("%{public}s GetVibratorInfo failed, error code is %{public}d", __func__, ret);
178         return ret;
179     }
180 
181     if (vibratorInfoVdi.empty()) {
182         HDF_LOGE("%{public}s no vibrator info in list", __func__);
183         return HDF_SUCCESS;
184     }
185     for (const auto &iter : vibratorInfoVdi) {
186         HdfVibratorInfo hdfVibratorInfo;
187         hdfVibratorInfo.isSupportIntensity = iter.isSupportIntensity;
188         hdfVibratorInfo.isSupportFrequency = iter.isSupportFrequency;
189         hdfVibratorInfo.intensityMaxValue = iter.intensityMaxValue;
190         hdfVibratorInfo.intensityMinValue = iter.intensityMinValue;
191         hdfVibratorInfo.frequencyMaxValue = iter.frequencyMaxValue;
192         hdfVibratorInfo.frequencyMinValue = iter.frequencyMinValue;
193 #ifdef TV_FLAG
194         hdfVibratorInfo.deviceId = iter.deviceId;
195         hdfVibratorInfo.vibratorId = iter.vibratorId;
196         hdfVibratorInfo.position = iter.position;
197         hdfVibratorInfo.isLocal = iter.isLocal;
198 #else
199         hdfVibratorInfo.deviceId = DEFAULT_DEVICE_ID;
200         hdfVibratorInfo.vibratorId = DEFAULT_VIBRATOR_ID;
201         hdfVibratorInfo.position = DEFAULT_POSITION;
202         hdfVibratorInfo.isLocal = DEFAULT_IS_LOCAL;
203 #endif
204         vibratorInfo.push_back(std::move(hdfVibratorInfo));
205     }
206 
207     return HDF_SUCCESS;
208 }
209 
GetVibratorIdSingle(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,std::vector<HdfVibratorInfo> & vibratorInfo)210 int32_t VibratorIfService::GetVibratorIdSingle(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
211                                                std::vector<HdfVibratorInfo> &vibratorInfo)
212 {
213     HDF_LOGD("%{public}s: Enter the GetVibratorInfo function.", __func__);
214 
215     std::vector<OHOS::HDI::Vibrator::V1_1::HdfVibratorInfoVdi> vibratorInfoVdi;
216     int32_t ret = HDF_FAILURE;
217     StartTrace(HITRACE_TAG_HDF, "GetVibratorInfo");
218 #ifdef TV_FLAG
219     ret = vibratorVdiImplV1_1_->GetVibratorIdSingle(deviceVibratorInfo, vibratorInfoVdi);
220 #else
221     HDF_LOGI("%{public}s: sensorVdiImplV1_1_ not support", __func__);
222     ret =  HDF_SUCCESS;
223 #endif
224     FinishTrace(HITRACE_TAG_HDF);
225 
226     if (ret != HDF_SUCCESS) {
227         HDF_LOGE("%{public}s GetVibratorInfo failed, error code is %{public}d", __func__, ret);
228         return ret;
229     }
230 
231     if (vibratorInfoVdi.empty()) {
232         HDF_LOGE("%{public}s no vibrator info in list", __func__);
233         return HDF_SUCCESS;
234     }
235     for (const auto &iter : vibratorInfoVdi) {
236         HdfVibratorInfo hdfVibratorInfo;
237         hdfVibratorInfo.isSupportIntensity = iter.isSupportIntensity;
238         hdfVibratorInfo.isSupportFrequency = iter.isSupportFrequency;
239         hdfVibratorInfo.intensityMaxValue = iter.intensityMaxValue;
240         hdfVibratorInfo.intensityMinValue = iter.intensityMinValue;
241         hdfVibratorInfo.frequencyMaxValue = iter.frequencyMaxValue;
242         hdfVibratorInfo.frequencyMinValue = iter.frequencyMinValue;
243 #ifdef TV_FLAG
244         hdfVibratorInfo.deviceId = iter.deviceId;
245         hdfVibratorInfo.vibratorId = iter.vibratorId;
246         hdfVibratorInfo.position = iter.position;
247         hdfVibratorInfo.isLocal = iter.isLocal;
248 #else
249         hdfVibratorInfo.deviceId = DEFAULT_DEVICE_ID;
250         hdfVibratorInfo.vibratorId = DEFAULT_VIBRATOR_ID;
251         hdfVibratorInfo.position = DEFAULT_POSITION;
252         hdfVibratorInfo.isLocal = DEFAULT_IS_LOCAL;
253 #endif
254         vibratorInfo.push_back(std::move(hdfVibratorInfo));
255     }
256 
257     return HDF_SUCCESS;
258 }
259 
EnableVibratorModulation(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,uint32_t duration,uint16_t intensity,int16_t frequency)260 int32_t VibratorIfService::EnableVibratorModulation(
261     const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo, uint32_t duration, uint16_t intensity,
262     int16_t frequency)
263 {
264     HDF_LOGD("%{public}s: duration is %{public}u, intensity is %{public}u, frequency is %{public}d.",
265         __func__, duration, intensity, frequency);
266 
267     StartTrace(HITRACE_TAG_HDF, "EnableVibratorModulation");
268 #ifdef TV_FLAG
269     int32_t ret = vibratorVdiImplV1_1_->EnableVibratorModulation(deviceVibratorInfo, duration, intensity, frequency);
270 #else
271     int32_t ret = vibratorVdiImplV1_1_->EnableVibratorModulation(duration, intensity, frequency);
272 #endif
273     FinishTrace(HITRACE_TAG_HDF);
274     if (ret != HDF_SUCCESS) {
275         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
276                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
277     }
278 
279     return ret;
280 }
281 
EnableCompositeEffect(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,const V2_0::HdfCompositeEffect & effect)282 int32_t VibratorIfService::EnableCompositeEffect(
283     const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo, const V2_0::HdfCompositeEffect &effect)
284 {
285     HDF_LOGD("%{public}s: Enter the EnableCompositeEffect function.", __func__);
286 
287     std::vector<HdfEffectVdi> effectVdi;
288     for (const auto &compositeEffects : effect.compositeEffects) {
289         HdfEffectVdi hdfEffectVdi;
290         if (effect.type == HDF_EFFECT_TYPE_TIME) {
291             hdfEffectVdi.timeEffect.delay = compositeEffects.timeEffect.delay;
292             hdfEffectVdi.timeEffect.time = compositeEffects.timeEffect.time;
293             hdfEffectVdi.timeEffect.intensity = compositeEffects.timeEffect.intensity;
294             hdfEffectVdi.timeEffect.frequency = compositeEffects.timeEffect.frequency;
295         } else if (effect.type == HDF_EFFECT_TYPE_PRIMITIVE) {
296             hdfEffectVdi.primitiveEffect.delay = compositeEffects.primitiveEffect.delay;
297             hdfEffectVdi.primitiveEffect.effectId = compositeEffects.primitiveEffect.effectId;
298             hdfEffectVdi.primitiveEffect.intensity = compositeEffects.primitiveEffect.intensity;
299         }
300         effectVdi.push_back(std::move(hdfEffectVdi));
301     }
302 
303     HdfCompositeEffectVdi compositeEffectVdi;
304     compositeEffectVdi.type = effect.type;
305     compositeEffectVdi.effects = effectVdi;
306 
307     StartTrace(HITRACE_TAG_HDF, "EnableCompositeEffect");
308 #ifdef TV_FLAG
309     int32_t ret = vibratorVdiImplV1_1_->EnableCompositeEffect(deviceVibratorInfo, compositeEffectVdi);
310 #else
311     int32_t ret = vibratorVdiImplV1_1_->EnableCompositeEffect(compositeEffectVdi);
312 #endif
313     if (ret != HDF_SUCCESS) {
314         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
315                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
316     }
317     FinishTrace(HITRACE_TAG_HDF);
318 
319     return ret;
320 }
321 
GetEffectInfo(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,const std::string & effectType,HdfEffectInfo & effectInfo)322 int32_t VibratorIfService::GetEffectInfo(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
323     const std::string &effectType, HdfEffectInfo &effectInfo)
324 {
325     HDF_LOGD("%{public}s: Enter the GetEffectInfo function.", __func__);
326 
327     HdfEffectInfoVdi effectInfoVdi = {0, false};
328     StartTrace(HITRACE_TAG_HDF, "GetEffectInfo");
329 #ifdef TV_FLAG
330     int32_t ret = vibratorVdiImplV1_1_->GetEffectInfo(deviceVibratorInfo, effectType, effectInfoVdi);
331 #else
332     int32_t ret = vibratorVdiImplV1_1_->GetEffectInfo(effectType, effectInfoVdi);
333 #endif
334     FinishTrace(HITRACE_TAG_HDF);
335 
336     if (ret != HDF_SUCCESS) {
337         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
338                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
339     }
340 
341     effectInfo.isSupportEffect = effectInfoVdi.isSupportEffect;
342     effectInfo.duration = effectInfoVdi.duration;
343 
344     return ret;
345 }
346 
IsVibratorRunning(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,bool & state)347 int32_t VibratorIfService::IsVibratorRunning(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
348                                              bool& state)
349 {
350     HDF_LOGD("%{public}s: Enter the IsVibratorRunning function", __func__);
351 
352     StartTrace(HITRACE_TAG_HDF, "IsVibratorRunning");
353 #ifdef TV_FLAG
354     int32_t ret = vibratorVdiImplV1_1_->IsVibratorRunning(deviceVibratorInfo, state);
355 #else
356     int32_t ret = vibratorVdiImplV1_1_->IsVibratorRunning(state);
357 #endif
358     FinishTrace(HITRACE_TAG_HDF);
359 
360     if (ret != HDF_SUCCESS) {
361         HDF_LOGE("%{public}s IsVibratorRunning failed, error code is %{public}d", __func__, ret);
362     }
363     HDF_LOGD("%{public}s: state %{public}d", __func__, state);
364 
365     return ret;
366 }
367 
PlayHapticPattern(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,const HapticPaket & pkg)368 int32_t VibratorIfService::PlayHapticPattern(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
369                                              const HapticPaket& pkg)
370 {
371     HDF_LOGD("%{public}s: Enter the PlayHapticPattern function", __func__);
372 
373     HapticPaketVdi hapticPaketVdi;
374     hapticPaketVdi.time = pkg.time;
375     hapticPaketVdi.eventNum = pkg.eventNum;
376     for (const auto &event : pkg.events) {
377         HapticEventVdi hapticEventVdi;
378         if (event.type == CONTINUOUS) {
379             hapticEventVdi.type = VDI_CONTINUOUS;
380         } else if (event.type == TRANSIENT) {
381             hapticEventVdi.type = VDI_TRANSIENT;
382         }
383         hapticEventVdi.time = event.time;
384         hapticEventVdi.duration = event.duration;
385         hapticEventVdi.intensity = event.intensity;
386         hapticEventVdi.frequency = event.frequency;
387         hapticEventVdi.index = event.index;
388         hapticEventVdi.pointNum = event.pointNum;
389         for (const auto &point : event.points) {
390             CurvePointVdi curvePointVdip;
391             curvePointVdip.time = point.time;
392             curvePointVdip.intensity = point.intensity;
393             curvePointVdip.frequency = point.frequency;
394             hapticEventVdi.points.push_back(std::move(curvePointVdip));
395         }
396         hapticPaketVdi.events.push_back(std::move(hapticEventVdi));
397     }
398 
399     StartTrace(HITRACE_TAG_HDF, "PlayHapticPattern");
400 #ifdef TV_FLAG
401     int32_t ret = vibratorVdiImplV1_1_->PlayHapticPattern(deviceVibratorInfo, hapticPaketVdi);
402 #else
403     int32_t ret = vibratorVdiImplV1_1_->PlayHapticPattern(hapticPaketVdi);
404 #endif
405     FinishTrace(HITRACE_TAG_HDF);
406 
407     if (ret != HDF_SUCCESS) {
408         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
409                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
410     }
411 
412     return ret;
413 }
414 
GetHapticCapacity(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,V2_0::HapticCapacity & hapticCapacity)415 int32_t VibratorIfService::GetHapticCapacity(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
416                                              V2_0::HapticCapacity& hapticCapacity)
417 {
418     HDF_LOGD("%{public}s: Enter the GetHapticCapacity function", __func__);
419 
420     HapticCapacityVdi hapticCapacityVdi = {false, false, false, 0, false, 0};
421     StartTrace(HITRACE_TAG_HDF, "GetHapticCapacity");
422 #ifdef TV_FLAG
423     int32_t ret = vibratorVdiImplV1_1_->GetHapticCapacity(deviceVibratorInfo, hapticCapacityVdi);
424 #else
425     int32_t ret = vibratorVdiImplV1_1_->GetHapticCapacity(hapticCapacityVdi);
426 #endif
427     FinishTrace(HITRACE_TAG_HDF);
428 
429     if (ret != HDF_SUCCESS) {
430         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
431                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
432     }
433     hapticCapacity.isSupportHdHaptic = hapticCapacityVdi.isSupportHdHaptic;
434     hapticCapacity.isSupportPresetMapping = hapticCapacityVdi.isSupportPresetMapping;
435     hapticCapacity.isSupportTimeDelay = hapticCapacityVdi.isSupportTimeDelay;
436 
437     return ret;
438 }
439 
GetHapticStartUpTime(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,int32_t mode,int32_t & startUpTime)440 int32_t VibratorIfService::GetHapticStartUpTime(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
441                                                 int32_t mode, int32_t& startUpTime)
442 {
443     HDF_LOGD("%{public}s: Enter the GetHapticStartUpTime function", __func__);
444 
445     StartTrace(HITRACE_TAG_HDF, "GetHapticStartUpTime");
446 #ifdef TV_FLAG
447     int32_t ret = vibratorVdiImplV1_1_->GetHapticStartUpTime(deviceVibratorInfo, mode, startUpTime);
448 #else
449     int32_t ret = vibratorVdiImplV1_1_->GetHapticStartUpTime(mode, startUpTime);
450 #endif
451     FinishTrace(HITRACE_TAG_HDF);
452 
453     if (ret != HDF_SUCCESS) {
454         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
455                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
456     }
457 
458     return ret;
459 }
460 
StartByIntensity(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,const std::string & effectType,uint16_t intensity)461 int32_t VibratorIfService::StartByIntensity(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
462                                             const std::string& effectType, uint16_t intensity)
463 {
464     HDF_LOGD("%{public}s: Enter the StartByIntensity function", __func__);
465 
466     StartTrace(HITRACE_TAG_HDF, "StartByIntensity");
467 #ifdef TV_FLAG
468     int32_t ret = vibratorVdiImplV1_1_->StartByIntensity(deviceVibratorInfo, effectType, intensity);
469 #else
470     int32_t ret = vibratorVdiImplV1_1_->StartByIntensity(effectType, intensity);
471 #endif
472     FinishTrace(HITRACE_TAG_HDF);
473 
474     if (ret != HDF_SUCCESS) {
475         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
476                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
477     }
478 
479     return ret;
480 }
481 
GetAllWaveInfo(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,std::vector<OHOS::HDI::Vibrator::V2_0::HdfWaveInformation> & info)482 int32_t VibratorIfService::GetAllWaveInfo(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
483                                           std::vector<OHOS::HDI::Vibrator::V2_0::HdfWaveInformation> &info)
484 {
485     HDF_LOGD("%{public}s: Enter the GetAllWaveInfo function", __func__);
486 
487     StartTrace(HITRACE_TAG_HDF, "GetAllWaveInfo");
488 #ifdef TV_FLAG
489     int32_t ret = vibratorVdiImplV1_1_->GetAllWaveInfo(deviceVibratorInfo, info);
490 #else
491     int32_t ret = vibratorVdiImplV1_1_->GetAllWaveInfo(deviceVibratorInfo.vibratorId, info);
492 #endif
493     FinishTrace(HITRACE_TAG_HDF);
494     if (ret != HDF_SUCCESS) {
495         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
496                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
497     }
498 
499     return ret;
500 }
501 
GetDeviceVibratorInfo(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,std::vector<V2_0::HdfVibratorInfo> & vibratorInfo)502 int32_t VibratorIfService::GetDeviceVibratorInfo(
503     const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
504     std::vector<V2_0::HdfVibratorInfo> &vibratorInfo)
505 {
506     HDF_LOGD("%{public}s: Enter the GetDeviceVibratorInfo function.", __func__);
507 
508     std::vector<HdfVibratorInfoVdi> vibratorInfoVdi;
509     StartTrace(HITRACE_TAG_HDF, "GetDeviceVibratorInfo");
510 #ifdef TV_FLAG
511     int32_t ret = vibratorVdiImplV1_1_->GetDeviceVibratorInfo(deviceVibratorInfo, vibratorInfoVdi);
512 #else
513     int32_t ret = vibratorVdiImplV1_1_->GetDeviceVibratorInfo(vibratorInfoVdi);
514 #endif
515     FinishTrace(HITRACE_TAG_HDF);
516 
517     if (ret != HDF_SUCCESS) {
518         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
519                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
520         return ret;
521     }
522 
523     if (vibratorInfoVdi.empty()) {
524         HDF_LOGE("%{public}s no vibrator info in list", __func__);
525         return HDF_FAILURE;
526     }
527     for (const auto &iter : vibratorInfoVdi) {
528         HdfVibratorInfo hdfVibratorInfo;
529         hdfVibratorInfo.isSupportIntensity = iter.isSupportIntensity;
530         hdfVibratorInfo.isSupportFrequency = iter.isSupportFrequency;
531         hdfVibratorInfo.intensityMaxValue = iter.intensityMaxValue;
532         hdfVibratorInfo.intensityMinValue = iter.intensityMinValue;
533         hdfVibratorInfo.frequencyMaxValue = iter.frequencyMaxValue;
534         hdfVibratorInfo.frequencyMinValue = iter.frequencyMinValue;
535 #ifdef TV_FLAG
536         hdfVibratorInfo.deviceId = iter.deviceId;
537         hdfVibratorInfo.vibratorId = iter.vibratorId;
538         hdfVibratorInfo.position = iter.position;
539         hdfVibratorInfo.isLocal = iter.isLocal;
540 #else
541         hdfVibratorInfo.deviceId = DEFAULT_DEVICE_ID;
542         hdfVibratorInfo.vibratorId = DEFAULT_VIBRATOR_ID;
543         hdfVibratorInfo.position = DEFAULT_POSITION;
544         hdfVibratorInfo.isLocal = DEFAULT_IS_LOCAL;
545 #endif
546         vibratorInfo.push_back(std::move(hdfVibratorInfo));
547     }
548 
549     return HDF_SUCCESS;
550 }
551 
RegVibratorPlugCallback(const sptr<V2_0::IVibratorPlugCallback> & callbackObj)552 int32_t VibratorIfService::RegVibratorPlugCallback(const sptr<V2_0::IVibratorPlugCallback> &callbackObj)
553 {
554     HDF_LOGD("%{public}s: Enter the RegVibratorPlugCallback function", __func__);
555 
556     StartTrace(HITRACE_TAG_HDF, "RegVibratorPlugCallback");
557 #ifdef TV_FLAG
558     int32_t ret = vibratorVdiImplV1_1_->RegVibratorPlugCallback(callbackObj);
559 #else
560     int32_t ret = HDF_SUCCESS;
561 #endif
562     FinishTrace(HITRACE_TAG_HDF);
563     if (ret != HDF_SUCCESS) {
564         HDF_LOGE("%{public}s: RegVibratorPlugCallback failed, error code is %{public}d", __func__, ret);
565     }
566 
567     return ret;
568 }
569 
UnRegVibratorPlugCallback(const sptr<V2_0::IVibratorPlugCallback> & callbackObj)570 int32_t VibratorIfService::UnRegVibratorPlugCallback(const sptr<V2_0::IVibratorPlugCallback> &callbackObj)
571 {
572     HDF_LOGD("%{public}s: Enter the UnRegVibratorPlugCallback function", __func__);
573 
574     StartTrace(HITRACE_TAG_HDF, "UnRegVibratorPlugCallback");
575 #ifdef TV_FLAG
576     int32_t ret = vibratorVdiImplV1_1_->UnRegVibratorPlugCallback(callbackObj);
577 #else
578     int32_t ret = HDF_SUCCESS;
579 #endif
580     FinishTrace(HITRACE_TAG_HDF);
581     if (ret != HDF_SUCCESS) {
582         HDF_LOGE("%{public}s: UnRegVibratorPlugCallback failed, error code is %{public}d", __func__, ret);
583     }
584 
585     return ret;
586 }
587 
PlayPatternBySessionId(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,uint32_t sessionId,const OHOS::HDI::Vibrator::V2_0::HapticPaket & hapticPaket)588 int32_t VibratorIfService::PlayPatternBySessionId(
589     const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
590     uint32_t sessionId,
591     const OHOS::HDI::Vibrator::V2_0::HapticPaket& hapticPaket)
592 {
593     HDF_LOGI("%{public}s: Enter the PlayPatternBySessionId function", __func__);
594 
595     HapticPaketVdi hapticPaketVdi;
596     hapticPaketVdi.time = hapticPaket.time;
597     hapticPaketVdi.eventNum = hapticPaket.eventNum;
598     for (const auto &event : hapticPaket.events) {
599         HapticEventVdi hapticEventVdi;
600         if (event.type == CONTINUOUS) {
601             hapticEventVdi.type = VDI_CONTINUOUS;
602         } else if (event.type == TRANSIENT) {
603             hapticEventVdi.type = VDI_TRANSIENT;
604         } else {
605             hapticEventVdi.type = VDI_UNKNOWN;
606             HDF_LOGE("%{public}s: Unknown event type %{public}d", __func__, event.type);
607             continue;
608         }
609         hapticEventVdi.time = event.time;
610         hapticEventVdi.duration = event.duration;
611         hapticEventVdi.intensity = event.intensity;
612         hapticEventVdi.frequency = event.frequency;
613         hapticEventVdi.index = event.index;
614         hapticEventVdi.pointNum = event.pointNum;
615         for (const auto &point : event.points) {
616             CurvePointVdi curvePointVdip;
617             curvePointVdip.time = point.time;
618             curvePointVdip.intensity = point.intensity;
619             curvePointVdip.frequency = point.frequency;
620             hapticEventVdi.points.push_back(std::move(curvePointVdip));
621         }
622         hapticPaketVdi.events.push_back(std::move(hapticEventVdi));
623     }
624 
625     StartTrace(HITRACE_TAG_HDF, "PlayPatternBySessionId");
626     int32_t ret = vibratorVdiImplV1_1_->PlayPatternBySessionId(deviceVibratorInfo, sessionId, hapticPaketVdi);
627     FinishTrace(HITRACE_TAG_HDF);
628 
629     if (ret != HDF_SUCCESS) {
630         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
631                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
632     }
633 
634     return ret;
635 }
636 
PlayPackageBySession(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,uint32_t sessionId,const OHOS::HDI::Vibrator::V2_0::VibratorPackage & vibratorPackage)637 int32_t VibratorIfService::PlayPackageBySession(
638     const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
639     uint32_t sessionId,
640     const OHOS::HDI::Vibrator::V2_0::VibratorPackage& vibratorPackage)
641 {
642     HDF_LOGI("%{public}s: Enter the PlayPackageBySession function", __func__);
643 
644     VibratorPackageVdi vibratorPackageVdi;
645     vibratorPackageVdi.patternNum = vibratorPackage.patternNum;
646     vibratorPackageVdi.packageduration = vibratorPackage.packageduration;
647     for (const auto &pattern : vibratorPackage.patterns) {
648         HapticPaketVdi hapticPaketVdi;
649         hapticPaketVdi.time = pattern.time;
650         hapticPaketVdi.eventNum = pattern.eventNum;
651         for (const auto &event : pattern.events) {
652             HapticEventVdi hapticEventVdi;
653             switch (event.type) {
654                 case CONTINUOUS: hapticEventVdi.type = VDI_CONTINUOUS; break;
655                 case TRANSIENT: hapticEventVdi.type = VDI_TRANSIENT; break;
656                 default:
657                     hapticEventVdi.type = VDI_UNKNOWN;
658                     HDF_LOGE("%{public}s: unknown event type %{public}d", __func__, event.type);
659                     continue;
660             }
661             hapticEventVdi.time = event.time;
662             hapticEventVdi.duration = event.duration;
663             hapticEventVdi.intensity = event.intensity;
664             hapticEventVdi.frequency = event.frequency;
665             hapticEventVdi.index = event.index;
666             hapticEventVdi.pointNum = event.pointNum;
667             for (const auto &point : event.points) {
668                 CurvePointVdi curvePointVdip;
669                 curvePointVdip.time = point.time;
670                 curvePointVdip.intensity = point.intensity;
671                 curvePointVdip.frequency = point.frequency;
672                 hapticEventVdi.points.push_back(std::move(curvePointVdip));
673             }
674             hapticPaketVdi.events.push_back(std::move(hapticEventVdi));
675         }
676         vibratorPackageVdi.patterns.push_back(std::move(hapticPaketVdi));
677     }
678     StartTrace(HITRACE_TAG_HDF, "PlayPackageBySession");
679     int32_t ret = vibratorVdiImplV1_1_->PlayPackageBySession(deviceVibratorInfo, sessionId, vibratorPackageVdi);
680     FinishTrace(HITRACE_TAG_HDF);
681 
682     if (ret != HDF_SUCCESS) {
683         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
684                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
685     }
686 
687     return ret;
688 }
689 
StopVibrateBySessionId(const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo & deviceVibratorInfo,uint32_t sessionId)690 int32_t VibratorIfService::StopVibrateBySessionId(
691     const OHOS::HDI::Vibrator::V2_0::DeviceVibratorInfo& deviceVibratorInfo,
692     uint32_t sessionId)
693 {
694     HDF_LOGI("%{public}s: Enter the StopVibrateBySessionId function", __func__);
695 
696     StartTrace(HITRACE_TAG_HDF, "StopVibrateBySessionId");
697     int32_t ret = vibratorVdiImplV1_1_->StopVibrateBySessionId(deviceVibratorInfo, sessionId);
698     FinishTrace(HITRACE_TAG_HDF);
699 
700     if (ret != HDF_SUCCESS) {
701         HDF_LOGE("%{public}s: failed, deviceId %{public}d, vibratorId %{public}d, error code is %{public}d",
702                  __func__, deviceVibratorInfo.deviceId, deviceVibratorInfo.vibratorId, ret);
703     }
704 
705     return ret;
706 }
707 
VibratorInterfaceImplGetInstance(void)708 extern "C" IVibratorInterface *VibratorInterfaceImplGetInstance(void)
709 {
710     VibratorIfService *impl = new (std::nothrow) VibratorIfService();
711     if (impl == nullptr) {
712         return nullptr;
713     }
714 
715     int32_t ret = impl->Init();
716     if (ret != HDF_SUCCESS) {
717         HDF_LOGE("%{public}s: service init failed, error code is %{public}d", __func__, ret);
718         delete impl;
719         return nullptr;
720     }
721 
722     return impl;
723 }
724 } // V2_0
725 } // Vibrator
726 } // HDI
727 } // OHOS
728