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