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 <fcntl.h>
17 #include <hdf_base.h>
18 #include <hdf_device_desc.h>
19 #include <hdf_device_object.h>
20 #include <hdf_log.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <osal_mem.h>
24 #include <stub_collector.h>
25 #include "v1_0/ihostapd_interface.h"
26 #include "hostapd_impl.h"
27
28 struct HdfHostapdInterfaceHost {
29 struct IDeviceIoService ioService;
30 struct IHostapdInterface *service;
31 struct HdfRemoteService **stubObject;
32 };
33
HostapdInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)34 static int32_t HostapdInterfaceDriverDispatch(
35 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
36 {
37 HDF_LOGI("HostapdInterfaceDriverDispatch enter.");
38 struct HdfHostapdInterfaceHost *hostapdinterfaceHost = CONTAINER_OF(
39 client->device->service, struct HdfHostapdInterfaceHost, ioService);
40 if (hostapdinterfaceHost->service == NULL || hostapdinterfaceHost->stubObject == NULL) {
41 HDF_LOGE("%{public}s: invalid service obj", __func__);
42 return HDF_ERR_INVALID_OBJECT;
43 }
44
45 struct HdfRemoteService *stubObj = *hostapdinterfaceHost->stubObject;
46 if (stubObj == NULL || stubObj->dispatcher == NULL || stubObj->dispatcher->Dispatch == NULL) {
47 return HDF_ERR_INVALID_OBJECT;
48 }
49
50 return stubObj->dispatcher->Dispatch((struct HdfRemoteService *)stubObj->target, cmdId, data, reply);
51 }
52
HdfHostapdInterfaceDriverInit(struct HdfDeviceObject * deviceObject)53 static int HdfHostapdInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
54 {
55 int32_t ret;
56 HDF_LOGI("HdfHostapdInterfaceDriverInit enter.");
57 struct HdfHostapdStubData *stubData = HdfHostapdStubDriver();
58 DListHeadInit(&stubData->remoteListHead);
59 ret = OsalMutexInit(&stubData->mutex);
60 if (ret != HDF_SUCCESS) {
61 HDF_LOGE("%{public}s: Mutex init failed, error code: %{public}d", __func__, ret);
62 return HDF_FAILURE;
63 }
64 return HDF_SUCCESS;
65 }
66
HdfHostapdInterfaceDriverBind(struct HdfDeviceObject * deviceObject)67 static int HdfHostapdInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
68 {
69 HDF_LOGI("HdfHostapdInterfaceDriverBind enter.");
70
71 int32_t ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, IHOSTAPDINTERFACE_INTERFACE_DESC);
72 if (ret != HDF_SUCCESS) {
73 HDF_LOGE("Failed to set interface descriptor of device object");
74 return ret;
75 }
76
77 struct HdfHostapdInterfaceHost *hostapdinterfaceHost =
78 (struct HdfHostapdInterfaceHost *)OsalMemAlloc(sizeof(struct HdfHostapdInterfaceHost));
79 if (hostapdinterfaceHost == NULL) {
80 HDF_LOGE("HdfHostapdInterfaceDriverBind OsalMemAlloc HdfHostapdInterfaceHost failed!");
81 return HDF_FAILURE;
82 }
83
84 struct IHostapdInterface *serviceImpl = IHostapdInterfaceGet(true);
85 struct HdfRemoteService **stubObj = StubCollectorGetOrNewObject(IHOSTAPDINTERFACE_INTERFACE_DESC, serviceImpl);
86 if (stubObj == NULL) {
87 OsalMemFree(hostapdinterfaceHost);
88 IHostapdInterfaceRelease(serviceImpl, true);
89 return HDF_FAILURE;
90 }
91
92 hostapdinterfaceHost->ioService.Dispatch = HostapdInterfaceDriverDispatch;
93 hostapdinterfaceHost->ioService.Open = NULL;
94 hostapdinterfaceHost->ioService.Release = NULL;
95 hostapdinterfaceHost->service = serviceImpl;
96 hostapdinterfaceHost->stubObject = stubObj;
97 deviceObject->service = &hostapdinterfaceHost->ioService;
98 return HDF_SUCCESS;
99 }
100
HdfHostapdInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)101 static void HdfHostapdInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
102 {
103 HDF_LOGI("HdfHostapdInterfaceDriverRelease enter.");
104 struct HdfHostapdRemoteNode *pos = NULL;
105 struct HdfHostapdRemoteNode *tmp = NULL;
106 struct HdfHostapdStubData *stubData = HdfHostapdStubDriver();
107 if (stubData == NULL) {
108 HDF_LOGE("%{public}s: stubData is NUll!", __func__);
109 return;
110 }
111
112 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &stubData->remoteListHead, struct HdfHostapdRemoteNode, node) {
113 DListRemove(&(pos->node));
114 OsalMemFree(pos);
115 }
116
117 OsalMutexDestroy(&stubData->mutex);
118 struct HdfHostapdInterfaceHost *hostapdinterfaceHost = CONTAINER_OF(
119 deviceObject->service, struct HdfHostapdInterfaceHost, ioService);
120 StubCollectorRemoveObject(IHOSTAPDINTERFACE_INTERFACE_DESC, hostapdinterfaceHost->service);
121 IHostapdInterfaceRelease(hostapdinterfaceHost->service, true);
122 OsalMemFree(hostapdinterfaceHost);
123 }
124
125 struct HdfDriverEntry g_hostapdinterfaceDriverEntry = {
126 .moduleVersion = 1,
127 .moduleName = "hostapd_service",
128 .Bind = HdfHostapdInterfaceDriverBind,
129 .Init = HdfHostapdInterfaceDriverInit,
130 .Release = HdfHostapdInterfaceDriverRelease,
131 };
132
133 HDF_INIT(g_hostapdinterfaceDriverEntry);
134