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