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 <hdf_base.h>
17094332d3Sopenharmony_ci#include <hdf_device_desc.h>
18094332d3Sopenharmony_ci#include <hdf_log.h>
19094332d3Sopenharmony_ci#include <hdf_sbuf_ipc.h>
20094332d3Sopenharmony_ci#include "v1_1/metadata_stub.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ci#undef LOG_TAG
23094332d3Sopenharmony_ci#define LOG_TAG "METADATA_DRV"
24094332d3Sopenharmony_ci#undef LOG_DOMAIN
25094332d3Sopenharmony_ci#define LOG_DOMAIN 0xD002515
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_ciusing namespace OHOS::HDI::Display::Buffer::V1_1;
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_cistruct HdfMetadataHost {
30094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
31094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
32094332d3Sopenharmony_ci};
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_cistatic int32_t MetadataDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
35094332d3Sopenharmony_ci    struct HdfSBuf *reply)
36094332d3Sopenharmony_ci{
37094332d3Sopenharmony_ci    auto *hdfMetadataHost = CONTAINER_OF(client->device->service, struct HdfMetadataHost, ioService);
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
40094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
41094332d3Sopenharmony_ci    OHOS::MessageOption option;
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
44094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
45094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
46094332d3Sopenharmony_ci    }
47094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
48094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
49094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
50094332d3Sopenharmony_ci    }
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    return hdfMetadataHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
53094332d3Sopenharmony_ci}
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_cistatic int HdfMetadataDriverInit(struct HdfDeviceObject *deviceObject)
56094332d3Sopenharmony_ci{
57094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: driver init start", __func__);
58094332d3Sopenharmony_ci    return HDF_SUCCESS;
59094332d3Sopenharmony_ci}
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_cistatic int HdfMetadataDriverBind(struct HdfDeviceObject *deviceObject)
62094332d3Sopenharmony_ci{
63094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: driver bind start", __func__);
64094332d3Sopenharmony_ci    auto *hdfMetadataHost = new (std::nothrow) HdfMetadataHost;
65094332d3Sopenharmony_ci    if (hdfMetadataHost == nullptr) {
66094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to create create HdfMetadataHost object", __func__);
67094332d3Sopenharmony_ci        return HDF_FAILURE;
68094332d3Sopenharmony_ci    }
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci    hdfMetadataHost->ioService.Dispatch = MetadataDriverDispatch;
71094332d3Sopenharmony_ci    hdfMetadataHost->ioService.Open = NULL;
72094332d3Sopenharmony_ci    hdfMetadataHost->ioService.Release = NULL;
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci    auto serviceImpl = OHOS::HDI::Display::Buffer::V1_1::IMetadata::Get(true);
75094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
76094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
77094332d3Sopenharmony_ci        delete hdfMetadataHost;
78094332d3Sopenharmony_ci        return HDF_FAILURE;
79094332d3Sopenharmony_ci    }
80094332d3Sopenharmony_ci
81094332d3Sopenharmony_ci    hdfMetadataHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
82094332d3Sopenharmony_ci        OHOS::HDI::Display::Buffer::V1_1::IMetadata::GetDescriptor());
83094332d3Sopenharmony_ci    if (hdfMetadataHost->stub == nullptr) {
84094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get stub object", __func__);
85094332d3Sopenharmony_ci        delete hdfMetadataHost;
86094332d3Sopenharmony_ci        return HDF_FAILURE;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    deviceObject->service = &hdfMetadataHost->ioService;
90094332d3Sopenharmony_ci    return HDF_SUCCESS;
91094332d3Sopenharmony_ci}
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_cistatic void HdfMetadataDriverRelease(struct HdfDeviceObject *deviceObject)
94094332d3Sopenharmony_ci{
95094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: driver release start", __func__);
96094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
97094332d3Sopenharmony_ci        return;
98094332d3Sopenharmony_ci    }
99094332d3Sopenharmony_ci
100094332d3Sopenharmony_ci    auto *hdfMetadataHost = CONTAINER_OF(deviceObject->service, struct HdfMetadataHost, ioService);
101094332d3Sopenharmony_ci    if (hdfMetadataHost != nullptr) {
102094332d3Sopenharmony_ci        delete hdfMetadataHost;
103094332d3Sopenharmony_ci    }
104094332d3Sopenharmony_ci}
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_cistatic struct HdfDriverEntry g_metadataDriverEntry = {
107094332d3Sopenharmony_ci    .moduleVersion = 1,
108094332d3Sopenharmony_ci    .moduleName = "display_buffer",
109094332d3Sopenharmony_ci    .Bind = HdfMetadataDriverBind,
110094332d3Sopenharmony_ci    .Init = HdfMetadataDriverInit,
111094332d3Sopenharmony_ci    .Release = HdfMetadataDriverRelease,
112094332d3Sopenharmony_ci};
113094332d3Sopenharmony_ci
114094332d3Sopenharmony_ci#ifdef __cplusplus
115094332d3Sopenharmony_ciextern "C" {
116094332d3Sopenharmony_ci#endif /* __cplusplus */
117094332d3Sopenharmony_ciHDF_INIT(g_metadataDriverEntry);
118094332d3Sopenharmony_ci#ifdef __cplusplus
119094332d3Sopenharmony_ci}
120094332d3Sopenharmony_ci#endif /* __cplusplus */
121