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