• 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 "hdf_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 V1_1 {
VibratorIfService()27 VibratorIfService::VibratorIfService()
28 {
29     int32_t ret = GetVibratorVdiImpl();
30     if (ret != HDF_SUCCESS) {
31         HDF_LOGE("%{public}s: get vibrator vdi instance failed", __func__);
32     }
33 }
34 
~VibratorIfService()35 VibratorIfService::~VibratorIfService()
36 {
37     if (vdi_ != nullptr) {
38         HdfCloseVdi(vdi_);
39     }
40 }
41 
GetVibratorVdiImpl()42 int32_t VibratorIfService::GetVibratorVdiImpl()
43 {
44     struct VdiWrapperVibrator *vdiWrapperVibrator = nullptr;
45     uint32_t version = 0;
46     vdi_ = HdfLoadVdi(HDI_VIBRATOR_VDI_LIBNAME);
47     if (vdi_ == nullptr || vdi_->vdiBase == nullptr) {
48         HDF_LOGE("%{public}s: load vibrator vdi failed", __func__);
49         return HDF_FAILURE;
50     }
51 
52     version = HdfGetVdiVersion(vdi_);
53     if (version != 1) {
54         HDF_LOGE("%{public}s: get vibrator vdi version failed", __func__);
55         return HDF_FAILURE;
56     }
57 
58     vdiWrapperVibrator = reinterpret_cast<struct VdiWrapperVibrator *>(vdi_->vdiBase);
59     vibratorVdiImpl_ = vdiWrapperVibrator->vibratorModule;
60     if (vibratorVdiImpl_ == nullptr) {
61         HDF_LOGE("%{public}s: get vibrator impl failed", __func__);
62         return HDF_FAILURE;
63     }
64 
65     return HDF_SUCCESS;
66 }
67 
Init()68 int32_t VibratorIfService::Init()
69 {
70     if (vibratorVdiImpl_ == nullptr) {
71         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
72         return HDF_FAILURE;
73     }
74 
75     int32_t ret = vibratorVdiImpl_->Init();
76     if (ret != HDF_SUCCESS) {
77         HDF_LOGE("%{public}s Init failed, error code is %{public}d", __func__, ret);
78     }
79 
80     return ret;
81 }
82 
StartOnce(uint32_t duration)83 int32_t VibratorIfService::StartOnce(uint32_t duration)
84 {
85     HDF_LOGI("%{public}s: Enter the StartOnce function, duration is %{public}u", __func__, duration);
86     if (vibratorVdiImpl_ == nullptr) {
87         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
88         return HDF_FAILURE;
89     }
90 
91     StartTrace(HITRACE_TAG_HDF, "StartOnce");
92     int32_t ret = vibratorVdiImpl_->StartOnce(duration);
93     if (ret != HDF_SUCCESS) {
94         HDF_LOGE("%{public}s StartOnce failed, error code is %{public}d", __func__, ret);
95     }
96     FinishTrace(HITRACE_TAG_HDF);
97 
98     return ret;
99 }
100 
Start(const std::string & effectType)101 int32_t VibratorIfService::Start(const std::string &effectType)
102 {
103     HDF_LOGI("%{public}s: Enter the Start function", __func__);
104     if (vibratorVdiImpl_ == nullptr) {
105         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
106         return HDF_FAILURE;
107     }
108 
109     StartTrace(HITRACE_TAG_HDF, "Start");
110     int32_t ret = vibratorVdiImpl_->Start(effectType);
111     if (ret != HDF_SUCCESS) {
112         HDF_LOGE("%{public}s Start failed, error code is %{public}d", __func__, ret);
113     }
114     FinishTrace(HITRACE_TAG_HDF);
115 
116     return ret;
117 }
118 
Stop(HdfVibratorMode mode)119 int32_t VibratorIfService::Stop(HdfVibratorMode mode)
120 {
121     HDF_LOGI("%{public}s: Enter the Stop function, mode: %{public}d", __func__, mode);
122     if (vibratorVdiImpl_ == nullptr) {
123         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
124         return HDF_FAILURE;
125     }
126 
127     HdfVibratorModeVdi vibratorMode;
128     if (mode == HDF_VIBRATOR_MODE_ONCE) {
129         vibratorMode = VDI_VIBRATOR_MODE_ONCE;
130     } else if (mode == HDF_VIBRATOR_MODE_PRESET) {
131         vibratorMode = VDI_VIBRATOR_MODE_PRESET;
132     } else if (mode == HDF_VIBRATOR_MODE_BUTT) {
133         vibratorMode = VDI_VIBRATOR_MODE_BUTT;
134     } else {
135         HDF_LOGE("%{public}s: invalid param", __func__);
136         return HDF_FAILURE;
137     }
138 
139     StartTrace(HITRACE_TAG_HDF, "Stop");
140     int32_t ret = vibratorVdiImpl_->Stop(vibratorMode);
141     if (ret != HDF_SUCCESS) {
142         HDF_LOGE("%{public}s Stop failed, error code is %{public}d", __func__, ret);
143     }
144     FinishTrace(HITRACE_TAG_HDF);
145 
146     return ret;
147 }
148 
GetVibratorInfo(std::vector<HdfVibratorInfo> & vibratorInfo)149 int32_t VibratorIfService::GetVibratorInfo(std::vector<HdfVibratorInfo> &vibratorInfo)
150 {
151     HDF_LOGI("%{public}s: Enter the GetVibratorInfo function.", __func__);
152     if (vibratorVdiImpl_ == nullptr) {
153         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
154         return HDF_FAILURE;
155     }
156 
157     std::vector<HdfVibratorInfoVdi> vibratorInfoVdi;
158     StartTrace(HITRACE_TAG_HDF, "GetVibratorInfo");
159     int32_t ret = vibratorVdiImpl_->GetVibratorInfo(vibratorInfoVdi);
160     if (ret != HDF_SUCCESS) {
161         HDF_LOGE("%{public}s GetVibratorInfo failed, error code is %{public}d", __func__, ret);
162         return ret;
163     }
164     FinishTrace(HITRACE_TAG_HDF);
165 
166     if (vibratorInfoVdi.empty()) {
167         HDF_LOGE("%{public}s no vibrator info in list", __func__);
168         return HDF_FAILURE;
169     }
170     for (const auto &iter : vibratorInfoVdi) {
171         HdfVibratorInfo hdfVibratorInfo;
172         hdfVibratorInfo.isSupportIntensity = iter.isSupportIntensity;
173         hdfVibratorInfo.isSupportFrequency = iter.isSupportFrequency;
174         hdfVibratorInfo.intensityMaxValue = iter.intensityMaxValue;
175         hdfVibratorInfo.intensityMinValue = iter.intensityMinValue;
176         hdfVibratorInfo.frequencyMaxValue = iter.frequencyMaxValue;
177         hdfVibratorInfo.frequencyMinValue = iter.frequencyMinValue;
178         vibratorInfo.push_back(std::move(hdfVibratorInfo));
179     }
180 
181     return HDF_SUCCESS;
182 }
183 
EnableVibratorModulation(uint32_t duration,uint16_t intensity,int16_t frequency)184 int32_t VibratorIfService::EnableVibratorModulation(uint32_t duration, uint16_t intensity, int16_t frequency)
185 {
186     HDF_LOGI("%{public}s: duration is %{public}u, intensity is %{public}u, frequency is %{public}d.",
187         __func__, duration, intensity, frequency);
188     if (vibratorVdiImpl_ == nullptr) {
189         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
190         return HDF_FAILURE;
191     }
192 
193     StartTrace(HITRACE_TAG_HDF, "EnableVibratorModulation");
194     int32_t ret = vibratorVdiImpl_->EnableVibratorModulation(duration, intensity, frequency);
195     if (ret != HDF_SUCCESS) {
196         HDF_LOGE("%{public}s EnableVibratorModulation failed, error code is %{public}d", __func__, ret);
197     }
198     FinishTrace(HITRACE_TAG_HDF);
199 
200     return ret;
201 }
202 
EnableCompositeEffect(const HdfCompositeEffect & effect)203 int32_t VibratorIfService::EnableCompositeEffect(const HdfCompositeEffect &effect)
204 {
205     HDF_LOGI("%{public}s: Enter the EnableCompositeEffect function.", __func__);
206     if (vibratorVdiImpl_ == nullptr) {
207         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
208         return HDF_FAILURE;
209     }
210 
211     std::vector<HdfEffectVdi> effectVdi;
212     for (const auto &compositeEffects : effect.compositeEffects) {
213         HdfEffectVdi hdfEffectVdi;
214         if (effect.type == HDF_EFFECT_TYPE_TIME) {
215             hdfEffectVdi.timeEffect.delay = compositeEffects.timeEffect.delay;
216             hdfEffectVdi.timeEffect.time = compositeEffects.timeEffect.time;
217             hdfEffectVdi.timeEffect.intensity = compositeEffects.timeEffect.intensity;
218             hdfEffectVdi.timeEffect.frequency = compositeEffects.timeEffect.frequency;
219         } else if (effect.type == HDF_EFFECT_TYPE_PRIMITIVE) {
220             hdfEffectVdi.primitiveEffect.delay = compositeEffects.primitiveEffect.delay;
221             hdfEffectVdi.primitiveEffect.effectId = compositeEffects.primitiveEffect.effectId;
222             hdfEffectVdi.primitiveEffect.intensity = compositeEffects.primitiveEffect.intensity;
223         }
224         effectVdi.push_back(std::move(hdfEffectVdi));
225     }
226 
227     HdfCompositeEffectVdi compositeEffectVdi;
228     compositeEffectVdi.type = effect.type;
229     compositeEffectVdi.effects = effectVdi;
230     StartTrace(HITRACE_TAG_HDF, "EnableCompositeEffect");
231     int32_t ret = vibratorVdiImpl_->EnableCompositeEffect(compositeEffectVdi);
232     if (ret != HDF_SUCCESS) {
233         HDF_LOGE("%{public}s  EnableCompositeEffect failed, error code is %{public}d", __func__, ret);
234     }
235     FinishTrace(HITRACE_TAG_HDF);
236 
237     return ret;
238 }
239 
GetEffectInfo(const std::string & effectType,HdfEffectInfo & effectInfo)240 int32_t VibratorIfService::GetEffectInfo(const std::string &effectType, HdfEffectInfo &effectInfo)
241 {
242     HDF_LOGI("%{public}s: Enter the GetEffectInfo function.", __func__);
243     if (vibratorVdiImpl_ == nullptr) {
244         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
245         return HDF_FAILURE;
246     }
247 
248     HdfEffectInfoVdi effectInfoVdi;
249     StartTrace(HITRACE_TAG_HDF, "GetEffectInfo");
250     int32_t ret = vibratorVdiImpl_->GetEffectInfo(effectType, effectInfoVdi);
251     if (ret != HDF_SUCCESS) {
252         HDF_LOGE("%{public}s GetEffectInfo failed, error code is %{public}d", __func__, ret);
253     }
254     FinishTrace(HITRACE_TAG_HDF);
255 
256     effectInfo.isSupportEffect = effectInfoVdi.isSupportEffect;
257     effectInfo.duration = effectInfoVdi.duration;
258 
259     return ret;
260 }
261 
IsVibratorRunning(bool & state)262 int32_t VibratorIfService::IsVibratorRunning(bool& state)
263 {
264     HDF_LOGI("%{public}s: Enter the IsVibratorRunning function", __func__);
265     if (vibratorVdiImpl_ == nullptr) {
266         HDF_LOGE("%{public}s: vibratorVdiImpl_ is nullptr", __func__);
267         return HDF_FAILURE;
268     }
269 
270     StartTrace(HITRACE_TAG_HDF, "IsVibratorRunning");
271     int32_t ret = vibratorVdiImpl_->IsVibratorRunning(state);
272     if (ret != HDF_SUCCESS) {
273         HDF_LOGE("%{public}s IsVibratorRunning failed, error code is %{public}d", __func__, ret);
274     }
275     FinishTrace(HITRACE_TAG_HDF);
276 
277     return ret;
278 }
279 
VibratorInterfaceImplGetInstance(void)280 extern "C" IVibratorInterface *VibratorInterfaceImplGetInstance(void)
281 {
282     VibratorIfService *impl = new (std::nothrow) VibratorIfService();
283     if (impl == nullptr) {
284         return nullptr;
285     }
286 
287     int32_t ret = impl->Init();
288     if (ret != HDF_SUCCESS) {
289         HDF_LOGE("%{public}s: service init failed, error code is %{public}d", __func__, ret);
290         delete impl;
291         return nullptr;
292     }
293 
294     return impl;
295 }
296 } // V1_1
297 } // Vibrator
298 } // HDI
299 } // OHOS
300