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