1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "motion_if_service.h" 17094332d3Sopenharmony_ci#include <hdf_base.h> 18094332d3Sopenharmony_ci#include "hitrace_meter.h" 19094332d3Sopenharmony_ci 20094332d3Sopenharmony_ci#define HDF_LOG_TAG "uhdf_motion_service" 21094332d3Sopenharmony_ci#define HDF_MOTION_TYPE_SECTION 1000 22094332d3Sopenharmony_ci 23094332d3Sopenharmony_cinamespace OHOS { 24094332d3Sopenharmony_cinamespace HDI { 25094332d3Sopenharmony_cinamespace Motion { 26094332d3Sopenharmony_cinamespace V1_1 { 27094332d3Sopenharmony_ciMotionIfService::MotionIfService() 28094332d3Sopenharmony_ci{ 29094332d3Sopenharmony_ci int32_t ret = GetMotionVdiImpl(); 30094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 31094332d3Sopenharmony_ci HDF_LOGE("%{public}s get motion vdi impl failed!", __func__); 32094332d3Sopenharmony_ci } 33094332d3Sopenharmony_ci} 34094332d3Sopenharmony_ci 35094332d3Sopenharmony_ciMotionIfService::~MotionIfService() 36094332d3Sopenharmony_ci{ 37094332d3Sopenharmony_ci if (vdi_ != nullptr) { 38094332d3Sopenharmony_ci HdfCloseVdi(vdi_); 39094332d3Sopenharmony_ci } 40094332d3Sopenharmony_ci} 41094332d3Sopenharmony_ci 42094332d3Sopenharmony_ciint32_t MotionIfService::GetMotionVdiImpl() 43094332d3Sopenharmony_ci{ 44094332d3Sopenharmony_ci struct WrapperMotionVdi *wrapperMotionVdi = nullptr; 45094332d3Sopenharmony_ci uint32_t version = 0; 46094332d3Sopenharmony_ci vdi_ = HdfLoadVdi(HDI_MOTION_VDI_LIBNAME); 47094332d3Sopenharmony_ci if (vdi_ == nullptr || vdi_->vdiBase == nullptr) { 48094332d3Sopenharmony_ci HDF_LOGE("%{public}s load motion vdi failed!", __func__); 49094332d3Sopenharmony_ci return HDF_FAILURE; 50094332d3Sopenharmony_ci } 51094332d3Sopenharmony_ci 52094332d3Sopenharmony_ci version = HdfGetVdiVersion(vdi_); 53094332d3Sopenharmony_ci if (version != 1) { 54094332d3Sopenharmony_ci HDF_LOGE("%{public}s get motion vdi version failed!", __func__); 55094332d3Sopenharmony_ci return HDF_FAILURE; 56094332d3Sopenharmony_ci } 57094332d3Sopenharmony_ci 58094332d3Sopenharmony_ci wrapperMotionVdi = reinterpret_cast<struct WrapperMotionVdi *>(vdi_->vdiBase); 59094332d3Sopenharmony_ci motionVdiImpl_ = wrapperMotionVdi->motionModule; 60094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 61094332d3Sopenharmony_ci HDF_LOGE("%{public}s get motion impl failed!", __func__); 62094332d3Sopenharmony_ci return HDF_FAILURE; 63094332d3Sopenharmony_ci } 64094332d3Sopenharmony_ci 65094332d3Sopenharmony_ci return HDF_SUCCESS; 66094332d3Sopenharmony_ci} 67094332d3Sopenharmony_ciint32_t MotionIfService::Init() 68094332d3Sopenharmony_ci{ 69094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 70094332d3Sopenharmony_ci HDF_LOGE("%{public}s get motion vdi version failed!", __func__); 71094332d3Sopenharmony_ci return HDF_FAILURE; 72094332d3Sopenharmony_ci } 73094332d3Sopenharmony_ci 74094332d3Sopenharmony_ci int32_t ret = motionVdiImpl_->InitMotion(); 75094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 76094332d3Sopenharmony_ci HDF_LOGE("%{public}s impl init failed,error code is %{public}d", __func__, ret); 77094332d3Sopenharmony_ci } 78094332d3Sopenharmony_ci 79094332d3Sopenharmony_ci return ret; 80094332d3Sopenharmony_ci} 81094332d3Sopenharmony_ci 82094332d3Sopenharmony_ciint32_t MotionIfService::EnableMotion(int32_t motionType) 83094332d3Sopenharmony_ci{ 84094332d3Sopenharmony_ci HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType); 85094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 86094332d3Sopenharmony_ci HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__); 87094332d3Sopenharmony_ci return HDF_FAILURE; 88094332d3Sopenharmony_ci } 89094332d3Sopenharmony_ci 90094332d3Sopenharmony_ci int32_t checkResult = CheckMotionType(motionType); 91094332d3Sopenharmony_ci if (checkResult != HDF_SUCCESS) { 92094332d3Sopenharmony_ci return checkResult; 93094332d3Sopenharmony_ci } 94094332d3Sopenharmony_ci 95094332d3Sopenharmony_ci StartTrace(HITRACE_TAG_HDF, "EnableMotion"); 96094332d3Sopenharmony_ci int32_t ret = motionVdiImpl_->EnableMotion(motionType); 97094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 98094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Enable failed, error code is %{public}d", __func__, ret); 99094332d3Sopenharmony_ci } 100094332d3Sopenharmony_ci FinishTrace(HITRACE_TAG_HDF); 101094332d3Sopenharmony_ci 102094332d3Sopenharmony_ci return ret; 103094332d3Sopenharmony_ci} 104094332d3Sopenharmony_ci 105094332d3Sopenharmony_ciint32_t MotionIfService::DisableMotion(int32_t motionType) 106094332d3Sopenharmony_ci{ 107094332d3Sopenharmony_ci HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType); 108094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 109094332d3Sopenharmony_ci HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__); 110094332d3Sopenharmony_ci return HDF_FAILURE; 111094332d3Sopenharmony_ci } 112094332d3Sopenharmony_ci 113094332d3Sopenharmony_ci int32_t checkResult = CheckMotionType(motionType); 114094332d3Sopenharmony_ci if (checkResult != HDF_SUCCESS) { 115094332d3Sopenharmony_ci return checkResult; 116094332d3Sopenharmony_ci } 117094332d3Sopenharmony_ci 118094332d3Sopenharmony_ci StartTrace(HITRACE_TAG_HDF, "DisableMotion"); 119094332d3Sopenharmony_ci int32_t ret = motionVdiImpl_->DisableMotion(motionType); 120094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 121094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Disable failed, error code is %{public}d", __func__, ret); 122094332d3Sopenharmony_ci } 123094332d3Sopenharmony_ci FinishTrace(HITRACE_TAG_HDF); 124094332d3Sopenharmony_ci 125094332d3Sopenharmony_ci return ret; 126094332d3Sopenharmony_ci} 127094332d3Sopenharmony_ci 128094332d3Sopenharmony_ciint32_t MotionIfService::Register(const sptr<IMotionCallback> &callbackObj) 129094332d3Sopenharmony_ci{ 130094332d3Sopenharmony_ci HDF_LOGI("%{public}s", __func__); 131094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 132094332d3Sopenharmony_ci HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__); 133094332d3Sopenharmony_ci return HDF_FAILURE; 134094332d3Sopenharmony_ci } 135094332d3Sopenharmony_ci 136094332d3Sopenharmony_ci StartTrace(HITRACE_TAG_HDF, "Register"); 137094332d3Sopenharmony_ci sptr<MotionCallbackVdi> motionCb = new MotionCallbackVdi(callbackObj); 138094332d3Sopenharmony_ci int32_t ret = motionVdiImpl_->RegisterMotionCallback(motionCb); 139094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 140094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Register failed, error code is %{public}d", __func__, ret); 141094332d3Sopenharmony_ci } 142094332d3Sopenharmony_ci FinishTrace(HITRACE_TAG_HDF); 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_ci return ret; 145094332d3Sopenharmony_ci} 146094332d3Sopenharmony_ci 147094332d3Sopenharmony_ciint32_t MotionIfService::Unregister(const sptr<IMotionCallback> &callbackObj) 148094332d3Sopenharmony_ci{ 149094332d3Sopenharmony_ci HDF_LOGI("%{public}s", __func__); 150094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 151094332d3Sopenharmony_ci HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__); 152094332d3Sopenharmony_ci return HDF_FAILURE; 153094332d3Sopenharmony_ci } 154094332d3Sopenharmony_ci 155094332d3Sopenharmony_ci StartTrace(HITRACE_TAG_HDF, "Unregister"); 156094332d3Sopenharmony_ci sptr<MotionCallbackVdi> motionCb = new MotionCallbackVdi(callbackObj); 157094332d3Sopenharmony_ci int32_t ret = motionVdiImpl_->UnregisterMotionCallback(motionCb); 158094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 159094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Unregister failed, Unregistererror code is %{public}d", __func__, ret); 160094332d3Sopenharmony_ci } 161094332d3Sopenharmony_ci FinishTrace(HITRACE_TAG_HDF); 162094332d3Sopenharmony_ci 163094332d3Sopenharmony_ci return ret; 164094332d3Sopenharmony_ci} 165094332d3Sopenharmony_ci 166094332d3Sopenharmony_ciint32_t MotionIfService::SetMotionConfig(int32_t motionType, const std::vector<uint8_t>& data) 167094332d3Sopenharmony_ci{ 168094332d3Sopenharmony_ci HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType); 169094332d3Sopenharmony_ci if (motionVdiImpl_ == nullptr) { 170094332d3Sopenharmony_ci HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__); 171094332d3Sopenharmony_ci return HDF_FAILURE; 172094332d3Sopenharmony_ci } 173094332d3Sopenharmony_ci 174094332d3Sopenharmony_ci int32_t checkResult = CheckMotionType(motionType); 175094332d3Sopenharmony_ci if (checkResult != HDF_SUCCESS) { 176094332d3Sopenharmony_ci return checkResult; 177094332d3Sopenharmony_ci } 178094332d3Sopenharmony_ci 179094332d3Sopenharmony_ci StartTrace(HITRACE_TAG_HDF, "SetMotionConfig"); 180094332d3Sopenharmony_ci int32_t ret = motionVdiImpl_->SetMotionConfig(motionType, data); 181094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 182094332d3Sopenharmony_ci HDF_LOGE("%{public}s: SetMotionConfig failed, error code is %{public}d", __func__, ret); 183094332d3Sopenharmony_ci } 184094332d3Sopenharmony_ci FinishTrace(HITRACE_TAG_HDF); 185094332d3Sopenharmony_ci 186094332d3Sopenharmony_ci return ret; 187094332d3Sopenharmony_ci} 188094332d3Sopenharmony_ci 189094332d3Sopenharmony_ciint32_t MotionIfService::CheckMotionType(int32_t motionType) 190094332d3Sopenharmony_ci{ 191094332d3Sopenharmony_ci if ((motionType > HDF_MOTION_TYPE_SECTION)) { 192094332d3Sopenharmony_ci return HDF_SUCCESS; 193094332d3Sopenharmony_ci } 194094332d3Sopenharmony_ci if (motionType >= HDF_MOTION_TYPE_PICKUP && motionType < HDF_MOTION_TYPE_MAX) { 195094332d3Sopenharmony_ci return HDF_SUCCESS; 196094332d3Sopenharmony_ci } 197094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 198094332d3Sopenharmony_ci} 199094332d3Sopenharmony_ci 200094332d3Sopenharmony_ciextern "C" IMotionInterface *MotionInterfaceImplGetInstance(void) 201094332d3Sopenharmony_ci{ 202094332d3Sopenharmony_ci MotionIfService *impl = new (std::nothrow) MotionIfService(); 203094332d3Sopenharmony_ci if (impl == nullptr) { 204094332d3Sopenharmony_ci return nullptr; 205094332d3Sopenharmony_ci } 206094332d3Sopenharmony_ci 207094332d3Sopenharmony_ci int32_t ret = impl->Init(); 208094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 209094332d3Sopenharmony_ci HDF_LOGE("%{public}s service init failed, error code is %{public}d", __func__, ret); 210094332d3Sopenharmony_ci delete impl; 211094332d3Sopenharmony_ci return nullptr; 212094332d3Sopenharmony_ci } 213094332d3Sopenharmony_ci 214094332d3Sopenharmony_ci return impl; 215094332d3Sopenharmony_ci} 216094332d3Sopenharmony_ci} // V1_1 217094332d3Sopenharmony_ci} //Motion 218094332d3Sopenharmony_ci} //HDI 219094332d3Sopenharmony_ci} //OHOS 220