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 "codec_image_config.h"
21094332d3Sopenharmony_ci#include "codec_log_wrapper.h"
22094332d3Sopenharmony_ci#include "v2_0/codec_image_stub.h"
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ciusing namespace OHOS::HDI::Codec::Image::V2_0;
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistruct HdfCodecImageHost {
27094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
28094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
29094332d3Sopenharmony_ci};
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cistatic int32_t CodecImageDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
32094332d3Sopenharmony_ci    struct HdfSBuf *reply)
33094332d3Sopenharmony_ci{
34094332d3Sopenharmony_ci    auto *hdfCodecImageHost = CONTAINER_OF(client->device->service, struct HdfCodecImageHost, ioService);
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
37094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
38094332d3Sopenharmony_ci    OHOS::MessageOption option;
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
41094332d3Sopenharmony_ci        CODEC_LOGE("invalid data sbuf object to dispatch");
42094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
43094332d3Sopenharmony_ci    }
44094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
45094332d3Sopenharmony_ci        CODEC_LOGE("invalid reply sbuf object to dispatch");
46094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
47094332d3Sopenharmony_ci    }
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    return hdfCodecImageHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
50094332d3Sopenharmony_ci}
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_cistatic int HdfCodecImageDriverInit(struct HdfDeviceObject *deviceObject)
53094332d3Sopenharmony_ci{
54094332d3Sopenharmony_ci    HDF_LOGI("driver init start");
55094332d3Sopenharmony_ci    if (deviceObject->property == nullptr) {
56094332d3Sopenharmony_ci        CODEC_LOGE("fail to get deviceObject property");
57094332d3Sopenharmony_ci        return HDF_FAILURE;
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci    CodecImageConfig::GetInstance()->Init(*(deviceObject->property));
60094332d3Sopenharmony_ci    return HDF_SUCCESS;
61094332d3Sopenharmony_ci}
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_cistatic int HdfCodecImageDriverBind(struct HdfDeviceObject *deviceObject)
64094332d3Sopenharmony_ci{
65094332d3Sopenharmony_ci    HDF_LOGI("driver bind start");
66094332d3Sopenharmony_ci    auto *hdfCodecImageHost = new (std::nothrow) HdfCodecImageHost;
67094332d3Sopenharmony_ci    if (hdfCodecImageHost == nullptr) {
68094332d3Sopenharmony_ci        CODEC_LOGE("failed to create create HdfCodecImageHost object");
69094332d3Sopenharmony_ci        return HDF_FAILURE;
70094332d3Sopenharmony_ci    }
71094332d3Sopenharmony_ci
72094332d3Sopenharmony_ci    hdfCodecImageHost->ioService.Dispatch = CodecImageDriverDispatch;
73094332d3Sopenharmony_ci    hdfCodecImageHost->ioService.Open = NULL;
74094332d3Sopenharmony_ci    hdfCodecImageHost->ioService.Release = NULL;
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_ci    auto serviceImpl = OHOS::HDI::Codec::Image::V2_0::ICodecImage::Get(true);
77094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
78094332d3Sopenharmony_ci        CODEC_LOGE("failed to get of implement service");
79094332d3Sopenharmony_ci        delete hdfCodecImageHost;
80094332d3Sopenharmony_ci        return HDF_FAILURE;
81094332d3Sopenharmony_ci    }
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    hdfCodecImageHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
84094332d3Sopenharmony_ci        OHOS::HDI::Codec::Image::V2_0::ICodecImage::GetDescriptor());
85094332d3Sopenharmony_ci    if (hdfCodecImageHost->stub == nullptr) {
86094332d3Sopenharmony_ci        CODEC_LOGE("failed to get stub object");
87094332d3Sopenharmony_ci        delete hdfCodecImageHost;
88094332d3Sopenharmony_ci        return HDF_FAILURE;
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci
91094332d3Sopenharmony_ci    deviceObject->service = &hdfCodecImageHost->ioService;
92094332d3Sopenharmony_ci    return HDF_SUCCESS;
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_cistatic void HdfCodecImageDriverRelease(struct HdfDeviceObject *deviceObject)
96094332d3Sopenharmony_ci{
97094332d3Sopenharmony_ci    HDF_LOGI("driver release start");
98094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
99094332d3Sopenharmony_ci        return;
100094332d3Sopenharmony_ci    }
101094332d3Sopenharmony_ci
102094332d3Sopenharmony_ci    auto *hdfCodecImageHost = CONTAINER_OF(deviceObject->service, struct HdfCodecImageHost, ioService);
103094332d3Sopenharmony_ci    if (hdfCodecImageHost != nullptr) {
104094332d3Sopenharmony_ci        delete hdfCodecImageHost;
105094332d3Sopenharmony_ci    }
106094332d3Sopenharmony_ci}
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_cistatic struct HdfDriverEntry g_CodecImageDriverEntry = {
109094332d3Sopenharmony_ci    .moduleVersion = 1,
110094332d3Sopenharmony_ci    .moduleName = "libcodec_image_service",
111094332d3Sopenharmony_ci    .Bind = HdfCodecImageDriverBind,
112094332d3Sopenharmony_ci    .Init = HdfCodecImageDriverInit,
113094332d3Sopenharmony_ci    .Release = HdfCodecImageDriverRelease,
114094332d3Sopenharmony_ci};
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_ci#ifdef __cplusplus
117094332d3Sopenharmony_ciextern "C" {
118094332d3Sopenharmony_ci#endif /* __cplusplus */
119094332d3Sopenharmony_ciHDF_INIT(g_CodecImageDriverEntry);
120094332d3Sopenharmony_ci#ifdef __cplusplus
121094332d3Sopenharmony_ci}
122094332d3Sopenharmony_ci#endif /* __cplusplus */
123