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_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "v1_0/partition_slot_stub.h"
21 
22 using namespace OHOS::HDI::Partitionslot::V1_0;
23 
24 struct HdfPartitionSlotHost {
25     struct IDeviceIoService ioService;
26     OHOS::sptr<OHOS::IRemoteObject> stub;
27 };
28 
PartitionSlotDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)29 static int32_t PartitionSlotDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
30     struct HdfSBuf *reply)
31 {
32     auto *hdfPartitionSlotHost = CONTAINER_OF(client->device->service, struct HdfPartitionSlotHost, ioService);
33 
34     OHOS::MessageParcel *dataParcel = nullptr;
35     OHOS::MessageParcel *replyParcel = nullptr;
36     OHOS::MessageOption option;
37 
38     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
39         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
40         return HDF_ERR_INVALID_PARAM;
41     }
42     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
43         HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
44         return HDF_ERR_INVALID_PARAM;
45     }
46 
47     return hdfPartitionSlotHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
48 }
49 
HdfPartitionSlotDriverInit(struct HdfDeviceObject * deviceObject)50 static int HdfPartitionSlotDriverInit(struct HdfDeviceObject *deviceObject)
51 {
52     HDF_LOGI("HdfPartitionSlotDriverInit enter");
53     return HDF_SUCCESS;
54 }
55 
HdfPartitionSlotDriverBind(struct HdfDeviceObject * deviceObject)56 static int HdfPartitionSlotDriverBind(struct HdfDeviceObject *deviceObject)
57 {
58     HDF_LOGI("HdfPartitionSlotDriverBind enter");
59 
60     auto *hdfPartitionSlotHost = new (std::nothrow) HdfPartitionSlotHost;
61     if (hdfPartitionSlotHost == nullptr) {
62         HDF_LOGE("%{public}s: failed to create create HdfPartitionSlotHost object", __func__);
63         return HDF_FAILURE;
64     }
65 
66     hdfPartitionSlotHost->ioService.Dispatch = PartitionSlotDriverDispatch;
67     hdfPartitionSlotHost->ioService.Open = NULL;
68     hdfPartitionSlotHost->ioService.Release = NULL;
69 
70     auto serviceImpl = IPartitionSlot::Get(true);
71     if (serviceImpl == nullptr) {
72         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
73         delete hdfPartitionSlotHost;
74         return HDF_FAILURE;
75     }
76 
77     hdfPartitionSlotHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
78         IPartitionSlot::GetDescriptor());
79     if (hdfPartitionSlotHost->stub == nullptr) {
80         HDF_LOGE("%{public}s: failed to get stub object", __func__);
81         delete hdfPartitionSlotHost;
82         return HDF_FAILURE;
83     }
84 
85     deviceObject->service = &hdfPartitionSlotHost->ioService;
86     return HDF_SUCCESS;
87 }
88 
HdfPartitionSlotDriverRelease(struct HdfDeviceObject * deviceObject)89 static void HdfPartitionSlotDriverRelease(struct HdfDeviceObject *deviceObject)
90 {
91     HDF_LOGI("HdfPartitionSlotDriverRelease enter");
92     if (deviceObject->service == nullptr) {
93         HDF_LOGE("HdfPartitionSlotDriverRelease not initted");
94         return;
95     }
96 
97     auto *hdfPartitionSlotHost = CONTAINER_OF(deviceObject->service, struct HdfPartitionSlotHost, ioService);
98     delete hdfPartitionSlotHost;
99 }
100 
101 static struct HdfDriverEntry g_partitionslotDriverEntry = {
102     .moduleVersion = 1,
103     .moduleName = "partitionslot_interface_service",
104     .Bind = HdfPartitionSlotDriverBind,
105     .Init = HdfPartitionSlotDriverInit,
106     .Release = HdfPartitionSlotDriverRelease,
107 };
108 
109 #ifndef __cplusplus
110 extern "C" {
111 #endif
112 HDF_INIT(g_partitionslotDriverEntry);
113 #ifndef __cplusplus
114 }
115 #endif
116