1 /*
2  * Copyright (c) 2022 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_sbuf_ipc.h>
19 #include <osal_mem.h>
20 #include "v1_1/thermal_interface_stub.h"
21 #include "thermal_log.h"
22 
23 
24 using namespace OHOS::HDI::Thermal::V1_1;
25 
26 namespace {
27 struct HdfThermalInterfaceHost {
28     struct IDeviceIoService ioService;
29     OHOS::sptr<OHOS::IRemoteObject> stub;
30 };
31 }
32 
ThermalInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)33 static int32_t ThermalInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
34     struct HdfSBuf *reply)
35 {
36     auto *hdfThermalInterfaceHost = CONTAINER_OF(client->device->service, struct HdfThermalInterfaceHost, 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         THERMAL_HILOGE(COMP_HDI, "invalid data sbuf object to dispatch");
44         return HDF_ERR_INVALID_PARAM;
45     }
46     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
47         THERMAL_HILOGE(COMP_HDI, "invalid reply sbuf object to dispatch");
48         return HDF_ERR_INVALID_PARAM;
49     }
50 
51     return hdfThermalInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
52 }
53 
54 static int32_t HdfThermalInterfaceDriverInit([[maybe_unused]] struct HdfDeviceObject *deviceObject)
55 {
56     THERMAL_HILOGD(COMP_HDI, "enter");
57     return HDF_SUCCESS;
58 }
59 
HdfThermalInterfaceDriverBind(struct HdfDeviceObject * deviceObject)60 static int32_t HdfThermalInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
61 {
62     THERMAL_HILOGD(COMP_HDI, "enter");
63 
64     auto *hdfThermalInterfaceHost = new (std::nothrow) HdfThermalInterfaceHost;
65     if (hdfThermalInterfaceHost == nullptr) {
66         THERMAL_HILOGE(COMP_HDI, "failed to create HdfThermalInterfaceHost object");
67         return HDF_FAILURE;
68     }
69 
70     hdfThermalInterfaceHost->ioService.Dispatch = ThermalInterfaceDriverDispatch;
71     hdfThermalInterfaceHost->ioService.Open = NULL;
72     hdfThermalInterfaceHost->ioService.Release = NULL;
73 
74     auto serviceImpl = IThermalInterface::Get(true);
75     if (serviceImpl == nullptr) {
76         THERMAL_HILOGE(COMP_HDI, "failed to get of implement service");
77         delete hdfThermalInterfaceHost;
78         return HDF_FAILURE;
79     }
80 
81     hdfThermalInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
82         IThermalInterface::GetDescriptor());
83     if (hdfThermalInterfaceHost->stub == nullptr) {
84         THERMAL_HILOGE(COMP_HDI, "failed to get stub object");
85         delete hdfThermalInterfaceHost;
86         return HDF_FAILURE;
87     }
88 
89     deviceObject->service = &hdfThermalInterfaceHost->ioService;
90     return HDF_SUCCESS;
91 }
92 
HdfThermalInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)93 static void HdfThermalInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
94 {
95     THERMAL_HILOGD(COMP_HDI, "enter");
96     if (deviceObject->service == nullptr) {
97         THERMAL_HILOGE(COMP_HDI, "HdfThermalInterfaceDriverRelease not initted");
98         return;
99     }
100 
101     auto *hdfThermalInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfThermalInterfaceHost, ioService);
102     delete hdfThermalInterfaceHost;
103 }
104 
105 static struct HdfDriverEntry g_thermalinterfaceDriverEntry = {
106     .moduleVersion = 1,
107     .moduleName = "thermal_interface_service",
108     .Bind = HdfThermalInterfaceDriverBind,
109     .Init = HdfThermalInterfaceDriverInit,
110     .Release = HdfThermalInterfaceDriverRelease,
111 };
112 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif /* __cplusplus */
116 HDF_INIT(g_thermalinterfaceDriverEntry);
117 #ifdef __cplusplus
118 }
119 #endif /* __cplusplus */
120