1 /*
2  * Copyright (c) 2022-2023 Shenzhen Kaihong DID 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_sbuf_ipc.h>
19 #include "codec_component_config.h"
20 #include "codec_log_wrapper.h"
21 #include "v3_0/codec_component_manager_stub.h"
22 #include "codec_dfx_service.h"
23 #include <devhost_dump_reg.h>
24 using namespace OHOS::HDI::Codec::V3_0;
25 namespace {
26     struct HdfCodecComponentManagerHost {
27         struct IDeviceIoService ioService;
28         OHOS::sptr<OHOS::IRemoteObject> stub;
29     };
30 }
31 
CodecComponentManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t CodecComponentManagerDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33                                                    struct HdfSBuf *reply)
34 {
35     auto *hdfCodecComponentManagerHost =
36         CONTAINER_OF(client->device->service, struct HdfCodecComponentManagerHost, ioService);
37 
38     OHOS::MessageParcel *dataParcel = nullptr;
39     OHOS::MessageParcel *replyParcel = nullptr;
40     OHOS::MessageOption option;
41 
42     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
43         CODEC_LOGE("invalid data sbuf object to dispatch");
44         return HDF_ERR_INVALID_PARAM;
45     }
46     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
47         CODEC_LOGE("invalid reply sbuf object to dispatch");
48         return HDF_ERR_INVALID_PARAM;
49     }
50 
51     return hdfCodecComponentManagerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
52 }
53 
HdfCodecComponentManagerDriverInit(struct HdfDeviceObject * deviceObject)54 static int HdfCodecComponentManagerDriverInit(struct HdfDeviceObject *deviceObject)
55 {
56     CODEC_LOGI("HdfCodecComponentManagerDriverInit enter");
57     if (DevHostRegisterDumpHost(CodecDfxService::DevCodecHostDump) != HDF_SUCCESS) {
58         CODEC_LOGE("DevHostRegisterDumpHost error!");
59     }
60     return HDF_SUCCESS;
61 }
62 
HdfCodecComponentManagerDriverBind(struct HdfDeviceObject * deviceObject)63 static int HdfCodecComponentManagerDriverBind(struct HdfDeviceObject *deviceObject)
64 {
65     CODEC_LOGI("HdfCodecComponentManagerDriverBind enter");
66 
67     auto *hdfCodecComponentManagerHost = new (std::nothrow) HdfCodecComponentManagerHost;
68     if (hdfCodecComponentManagerHost == nullptr) {
69         CODEC_LOGE("failed to create create HdfCodecComponentManagerHost object");
70         return HDF_FAILURE;
71     }
72 
73     hdfCodecComponentManagerHost->ioService.Dispatch = CodecComponentManagerDriverDispatch;
74     hdfCodecComponentManagerHost->ioService.Open = NULL;
75     hdfCodecComponentManagerHost->ioService.Release = NULL;
76 
77     auto serviceImpl = ICodecComponentManager::Get(true);
78     if (serviceImpl == nullptr) {
79         CODEC_LOGE("failed to get of implement service");
80         delete hdfCodecComponentManagerHost;
81         return HDF_FAILURE;
82     }
83 
84     hdfCodecComponentManagerHost->stub =
85         OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, ICodecComponentManager::GetDescriptor());
86     if (hdfCodecComponentManagerHost->stub == nullptr) {
87         CODEC_LOGE("failed to get stub object");
88         delete hdfCodecComponentManagerHost;
89         return HDF_FAILURE;
90     }
91 
92     deviceObject->service = &hdfCodecComponentManagerHost->ioService;
93     return HDF_SUCCESS;
94 }
95 
HdfCodecComponentManagerDriverRelease(struct HdfDeviceObject * deviceObject)96 static void HdfCodecComponentManagerDriverRelease(struct HdfDeviceObject *deviceObject)
97 {
98     CODEC_LOGI("HdfCodecComponentManagerDriverRelease enter");
99     if (deviceObject->service == nullptr) {
100         CODEC_LOGE("HdfCodecComponentManagerDriverRelease not initted");
101         return;
102     }
103 
104     auto *hdfCodecComponentManagerHost =
105         CONTAINER_OF(deviceObject->service, struct HdfCodecComponentManagerHost, ioService);
106     delete hdfCodecComponentManagerHost;
107 }
108 
109 static struct HdfDriverEntry g_codeccomponentmanagerDriverEntry = {
110     .moduleVersion = 1,
111     .moduleName = "codec_component_manager_service",
112     .Bind = HdfCodecComponentManagerDriverBind,
113     .Init = HdfCodecComponentManagerDriverInit,
114     .Release = HdfCodecComponentManagerDriverRelease,
115 };
116 
117 #ifndef __cplusplus
118 extern "C" {
119 #endif
120 HDF_INIT(g_codeccomponentmanagerDriverEntry);
121 #ifndef __cplusplus
122 }
123 #endif
124