1/*
2 * Copyright (c) 2021 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 <hdf_log.h>
19#include <hdf_sbuf_ipc.h>
20#include "v1_0/camera_host_stub.h"
21
22using namespace OHOS::HDI::Camera::V1_0;
23
24struct HdfCameraHostHost {
25    struct IDeviceIoService ioService;
26    OHOS::sptr<OHOS::IRemoteObject> stub;
27};
28
29static int32_t CameraHostDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
30    struct HdfSBuf *reply)
31{
32    auto *hdfCameraHostHost = CONTAINER_OF(client->device->service, struct HdfCameraHostHost, 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    return hdfCameraHostHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
48}
49
50static int HdfCameraHostDriverInit(struct HdfDeviceObject *deviceObject)
51{
52    HDF_LOGI("HdfCameraHostDriverInit enter");
53    (void)deviceObject;
54    return HDF_SUCCESS;
55}
56
57extern "C" ICameraHost *CameraHostServiceGetInstance(void);
58
59static int HdfCameraHostDriverBind(struct HdfDeviceObject *deviceObject)
60{
61    HDF_LOGI("HdfCameraHostDriverBind enter");
62    if (deviceObject == nullptr) {
63        HDF_LOGE("%{public}s: device object is NULL!", __func__);
64        return HDF_FAILURE;
65    }
66
67    auto *hdfCameraHostHost = new (std::nothrow) HdfCameraHostHost;
68    if (hdfCameraHostHost == nullptr) {
69        HDF_LOGE("%{public}s: failed to create create HdfCameraHostHost object", __func__);
70        return HDF_FAILURE;
71    }
72
73    hdfCameraHostHost->ioService.Dispatch = CameraHostDriverDispatch;
74    hdfCameraHostHost->ioService.Open = NULL;
75    hdfCameraHostHost->ioService.Release = NULL;
76
77    OHOS::sptr<ICameraHost> serviceImpl {CameraHostServiceGetInstance()};
78    if (serviceImpl == nullptr) {
79        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
80        delete hdfCameraHostHost;
81        return HDF_FAILURE;
82    }
83
84    hdfCameraHostHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
85        ICameraHost::GetDescriptor());
86    if (hdfCameraHostHost->stub == nullptr) {
87        HDF_LOGE("%{public}s: failed to get stub object", __func__);
88        delete hdfCameraHostHost;
89        return HDF_FAILURE;
90    }
91
92    deviceObject->service = &hdfCameraHostHost->ioService;
93    return HDF_SUCCESS;
94}
95
96static void HdfCameraHostDriverRelease(struct HdfDeviceObject *deviceObject)
97{
98    HDF_LOGI("HdfCameraHostDriverRelease enter");
99    if (deviceObject == nullptr) {
100        HDF_LOGE("%{public}s: device object is NULL!", __func__);
101        return;
102    }
103
104    if (deviceObject->service == nullptr) {
105        HDF_LOGE("HdfCameraHostDriverRelease not initted");
106        return;
107    }
108
109    auto *hdfCameraHostHost = CONTAINER_OF(deviceObject->service, struct HdfCameraHostHost, ioService);
110    delete hdfCameraHostHost;
111}
112
113static struct HdfDriverEntry g_camerahostDriverEntry = {
114    .moduleVersion = 1,
115    .moduleName = "camera_service",
116    .Bind = HdfCameraHostDriverBind,
117    .Init = HdfCameraHostDriverInit,
118    .Release = HdfCameraHostDriverRelease,
119};
120
121#ifndef __cplusplus
122extern "C" {
123#endif
124HDF_INIT(g_camerahostDriverEntry);
125#ifndef __cplusplus
126}
127#endif
128