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 25constexpr int32_t VIBRATOR_INTENSITY_STOP = 0; 26 27namespace OHOS { 28namespace HDI { 29namespace Vibrator { 30namespace V1_1 { 31 32int32_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 43int32_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 58int32_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 73int32_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 103int32_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 136int32_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 153int32_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 165int32_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 186int32_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 return HDF_SUCCESS; 196} 197 198int32_t VibratorInterfaceImpl::PlayHapticPattern(const HapticPaketVdi& pkgVdi) 199{ 200 HDF_LOGI("%{public}s: Enter the PlayHapticPattern function\n", __func__); 201 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance(); 202 if (vibratorInterface == nullptr || vibratorInterface->PlayHapticPattern == nullptr) { 203 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__); 204 return HDF_FAILURE; 205 } 206 207 return HDF_SUCCESS; 208} 209 210int32_t VibratorInterfaceImpl::GetHapticCapacity(HapticCapacityVdi& hapticCapacityVdi) 211{ 212 HDF_LOGI("%{public}s: Enter the GetHapticCapacity function\n", __func__); 213 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance(); 214 if (vibratorInterface == nullptr || vibratorInterface->GetHapticCapacity == nullptr) { 215 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__); 216 return HDF_FAILURE; 217 } 218 struct HapticCapacity hapticCapacity; 219 int32_t ret = vibratorInterface->GetHapticCapacity(&hapticCapacity); 220 if (ret != HDF_SUCCESS) { 221 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret); 222 return ret; 223 } 224 hapticCapacityVdi.isSupportHdHaptic = hapticCapacity.isSupportHdHaptic; 225 hapticCapacityVdi.isSupportPresetMapping = hapticCapacity.isSupportPresetMapping; 226 hapticCapacityVdi.isSupportTimeDelay = hapticCapacity.isSupportTimeDelay; 227 228 return HDF_SUCCESS; 229} 230 231int32_t VibratorInterfaceImpl::GetHapticStartUpTime(int32_t mode, int32_t& startUpTime) 232{ 233 HDF_LOGI("%{public}s: Enter the GetHapticStartUpTime function\n", __func__); 234 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance(); 235 if (vibratorInterface == nullptr || vibratorInterface->GetHapticStartUpTime == nullptr) { 236 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__); 237 return HDF_FAILURE; 238 } 239 int32_t ret = vibratorInterface->GetHapticStartUpTime(mode, &startUpTime); 240 if (ret != HDF_SUCCESS) { 241 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret); 242 } 243 244 return ret; 245} 246 247int32_t VibratorInterfaceImpl::StartByIntensity(const std::string& effectType, uint16_t intensity) 248{ 249 const struct VibratorInterface *vibratorInterface = NewVibratorInterfaceInstance(); 250 if (vibratorInterface == nullptr) { 251 HDF_LOGE("%{public}s: get vibrator Module instance failed", __func__); 252 return HDF_FAILURE; 253 } 254 if (intensity == VIBRATOR_INTENSITY_STOP) { 255 return HDF_SUCCESS; 256 } 257 258 int32_t ret = vibratorInterface->Start(effectType.c_str()); 259 if (ret != HDF_SUCCESS) { 260 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret); 261 } 262 263 return ret; 264} 265 266static int32_t CreateLightVdiInstance(struct HdfVdiBase *vdiBase) 267{ 268 HDF_LOGI("%{public}s: Enter the CreateLightVdiInstance function", __func__); 269 if (vdiBase == nullptr) { 270 HDF_LOGE("%{public}s parameter vdiBase is NULL", __func__); 271 return HDF_FAILURE; 272 } 273 274 struct VdiWrapperVibrator *vibratorVdi = reinterpret_cast<VdiWrapperVibrator *>(vdiBase); 275 vibratorVdi->vibratorModule = new VibratorInterfaceImpl(); 276 if (vibratorVdi->vibratorModule == nullptr) { 277 HDF_LOGI("%{public}s: new vibratorModule failed!", __func__); 278 return HDF_FAILURE; 279 } 280 return HDF_SUCCESS; 281} 282 283static int32_t DestoryLightVdiInstance(struct HdfVdiBase *vdiBase) 284{ 285 HDF_LOGI("%{public}s: Enter the DestoryLightVdiInstance function", __func__); 286 if (vdiBase == nullptr) { 287 HDF_LOGE("%{public}s parameter vdiBase is NULL", __func__); 288 return HDF_FAILURE; 289 } 290 291 struct VdiWrapperVibrator *vibratorVdi = reinterpret_cast<VdiWrapperVibrator *>(vdiBase); 292 VibratorInterfaceImpl *vibratorImpl = reinterpret_cast<VibratorInterfaceImpl *>(vibratorVdi->vibratorModule); 293 if (vibratorImpl != nullptr) { 294 delete vibratorImpl; 295 vibratorVdi->vibratorModule = nullptr; 296 } 297 return HDF_SUCCESS; 298} 299 300static struct VdiWrapperVibrator g_vibratorVdi = { 301 .base = { 302 .moduleVersion = 1, 303 .moduleName = "vibrator_service", 304 .CreateVdiInstance = CreateLightVdiInstance, 305 .DestoryVdiInstance = DestoryLightVdiInstance, 306 }, 307 .vibratorModule = nullptr, 308}; 309 310extern "C" HDF_VDI_INIT(g_vibratorVdi); 311 312} // V1_1 313} // Vibrator 314} // HDI 315} // OHOS 316