1/* 2 * Copyright (c) 2022 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 <hdf_base.h> 17#include <hdf_device_desc.h> 18#include "motion_uhdf_log.h" 19#include <hdf_sbuf_ipc.h> 20#include "v1_1/motion_interface_stub.h" 21 22using namespace OHOS::HDI::Motion::V1_1; 23 24struct HdfMotionInterfaceHost { 25 struct IDeviceIoService ioService; 26 OHOS::sptr<OHOS::IRemoteObject> stub; 27}; 28 29static int32_t MotionInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, 30 struct HdfSBuf *reply) 31{ 32 auto *hdfMotionInterfaceHost = CONTAINER_OF(client->device->service, struct HdfMotionInterfaceHost, ioService); 33 34 OHOS::MessageParcel *dataParcel = nullptr; 35 OHOS::MessageParcel *replyParcel = nullptr; 36 OHOS::MessageOption option; 37 38 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) { 39 HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__); 40 return HDF_ERR_INVALID_PARAM; 41 } 42 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) { 43 HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__); 44 return HDF_ERR_INVALID_PARAM; 45 } 46 47 if (hdfMotionInterfaceHost == nullptr) { 48 HDF_LOGE("%{public}s:hdfMotionInterfaceHost is nullptr", __func__); 49 return HDF_ERR_INVALID_PARAM; 50 } 51 if (hdfMotionInterfaceHost->stub == nullptr) { 52 HDF_LOGE("%{public}s:hdfMotionInterfaceHost->stub is nullptr", __func__); 53 return HDF_ERR_INVALID_PARAM; 54 } 55 56 return hdfMotionInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option); 57} 58 59static int32_t HdfMotionInterfaceDriverInit(struct HdfDeviceObject *deviceObject) 60{ 61 (void)deviceObject; 62 HDF_LOGI("HdfMotionInterfaceDriverInit enter"); 63 return HDF_SUCCESS; 64} 65 66static int32_t HdfMotionInterfaceDriverBind(struct HdfDeviceObject *deviceObject) 67{ 68 HDF_LOGI("HdfMotionInterfaceDriverBind enter"); 69 70 auto *hdfMotionInterfaceHost = new (std::nothrow) HdfMotionInterfaceHost; 71 if (hdfMotionInterfaceHost == nullptr) { 72 HDF_LOGE("%{public}s: failed to create create HdfMotionInterfaceHost object", __func__); 73 return HDF_FAILURE; 74 } 75 76 hdfMotionInterfaceHost->ioService.Dispatch = MotionInterfaceDriverDispatch; 77 hdfMotionInterfaceHost->ioService.Open = NULL; 78 hdfMotionInterfaceHost->ioService.Release = NULL; 79 80 auto serviceImpl = OHOS::HDI::Motion::V1_1::IMotionInterface::Get(true); 81 if (serviceImpl == nullptr) { 82 HDF_LOGE("%{public}s: failed to get of implement service", __func__); 83 delete hdfMotionInterfaceHost; 84 return HDF_FAILURE; 85 } 86 87 hdfMotionInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, 88 OHOS::HDI::Motion::V1_1::IMotionInterface::GetDescriptor()); 89 if (hdfMotionInterfaceHost->stub == nullptr) { 90 HDF_LOGE("%{public}s: failed to get stub object", __func__); 91 delete hdfMotionInterfaceHost; 92 return HDF_FAILURE; 93 } 94 95 deviceObject->service = &hdfMotionInterfaceHost->ioService; 96 return HDF_SUCCESS; 97} 98 99static void HdfMotionInterfaceDriverRelease(struct HdfDeviceObject *deviceObject) 100{ 101 HDF_LOGI("HdfMotionInterfaceDriverRelease enter"); 102 auto *hdfMotionInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfMotionInterfaceHost, ioService); 103 delete hdfMotionInterfaceHost; 104 hdfMotionInterfaceHost = nullptr; 105} 106 107static struct HdfDriverEntry g_motioninterfaceDriverEntry = { 108 .moduleVersion = 1, 109 .moduleName = "motion_service", 110 .Bind = HdfMotionInterfaceDriverBind, 111 .Init = HdfMotionInterfaceDriverInit, 112 .Release = HdfMotionInterfaceDriverRelease, 113}; 114 115#ifdef __cplusplus 116extern "C" { 117#endif /* __cplusplus */ 118HDF_INIT(g_motioninterfaceDriverEntry); 119#ifdef __cplusplus 120} 121#endif /* __cplusplus */ 122