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