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