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