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 #include <securec.h>
16 #include <hdf_base.h>
17 #include <hdf_log.h>
18 #include <hdf_load_vdi.h>
19 #include <osal_time.h>
20 #include <osal_mem.h>
21 #include "v1_3/iwlan_interface.h"
22 #include "wifi_hal.h"
23 #include "wlan_common_cmd.h"
24 #include "wlan_extend_cmd_vdi.h"
25 
26 #define VDI_VERSION_ONE 1
27 
28 struct WlanExtendInterfaceVdi *g_wlanExtendVdiImpl = NULL;
29 struct HdfVdiObject *g_vdi = NULL;
30 
CloseVdi(void)31 static void CloseVdi(void)
32 {
33     if (g_vdi != NULL) {
34         HdfCloseVdi(g_vdi);
35         g_vdi = NULL;
36     }
37 }
38 
InitWlanExtendVdiImpl()39 static int32_t InitWlanExtendVdiImpl()
40 {
41     uint32_t version = 0;
42     g_vdi = HdfLoadVdi(WLAN_EXTEND_VDI_LIBNAME);
43     if (g_vdi == NULL || g_vdi->vdiBase == NULL) {
44         HDF_LOGE("%{public}s: load wlan extend vdi failed", __func__);
45         return HDF_FAILURE;
46     }
47 
48     version = HdfGetVdiVersion(g_vdi);
49     if (version != VDI_VERSION_ONE) {
50         HDF_LOGE("%{public}s: get wlan extend vdi version failed", __func__);
51         CloseVdi();
52         return HDF_FAILURE;
53     }
54 
55     struct VdiWrapperWlanExtend *vdiWrapperWlanExtend = NULL;
56     vdiWrapperWlanExtend = (struct VdiWrapperWlanExtend *)(g_vdi->vdiBase);
57     g_wlanExtendVdiImpl = vdiWrapperWlanExtend->wlanExtendModule;
58     if (g_wlanExtendVdiImpl == NULL) {
59         HDF_LOGE("%{public}s: get vibrator impl failed", __func__);
60         CloseVdi();
61         return HDF_FAILURE;
62     }
63 
64     return HDF_SUCCESS;
65 }
66 
WlanInterfaceStartChannelMeas(struct IWlanInterface * self,const char * ifName,const struct MeasChannelParam * measChannelParam)67 int32_t WlanInterfaceStartChannelMeas(struct IWlanInterface *self, const char *ifName,
68     const struct MeasChannelParam *measChannelParam)
69 {
70     int32_t ret;
71 
72     (void)self;
73     if (ifName == NULL || measChannelParam == NULL) {
74         HDF_LOGE("%{public}s input parameter invalid!", __func__);
75         return HDF_ERR_INVALID_PARAM;
76     }
77     if (g_wlanExtendVdiImpl == NULL) {
78         HDF_LOGE("%{public}s g_wlanExtendVdiImpl is NULL!", __func__);
79         return HDF_FAILURE;
80     }
81     ret = g_wlanExtendVdiImpl->startChannelMeas(self, ifName, measChannelParam);
82     if (ret != HDF_SUCCESS) {
83         HDF_LOGE("%{public}s: start channel meas failed!, error code: %{public}d", __func__, ret);
84     }
85     return ret;
86 }
87 
WlanInterfaceGetChannelMeasResult(struct IWlanInterface * self,const char * ifName,struct MeasChannelResult * measChannelResult)88 int32_t WlanInterfaceGetChannelMeasResult(struct IWlanInterface *self, const char *ifName,
89     struct MeasChannelResult *measChannelResult)
90 {
91     int32_t ret;
92 
93     (void)self;
94     if (ifName == NULL || measChannelResult == NULL) {
95         HDF_LOGE("%{public}s input parameter invalid!", __func__);
96         return HDF_ERR_INVALID_PARAM;
97     }
98     if (g_wlanExtendVdiImpl == NULL) {
99         HDF_LOGE("%{public}s g_wlanExtendVdiImplis NULL!", __func__);
100         return HDF_FAILURE;
101     }
102     ret = g_wlanExtendVdiImpl->getChannelMeasResult(self, ifName, measChannelResult);
103     if (ret != HDF_SUCCESS) {
104         HDF_LOGE("%{public}s: get channel meas result failed!, error code: %{public}d", __func__, ret);
105     }
106     return ret;
107 }
108 
WlanInterfaceWifiSendCmdIoctl(struct IWlanInterface * self,const char * ifName,int32_t cmdId,const int8_t * paramBuf,uint32_t paramBufLen)109 int32_t WlanInterfaceWifiSendCmdIoctl(struct IWlanInterface *self, const char *ifName, int32_t cmdId,
110     const int8_t *paramBuf, uint32_t paramBufLen)
111 {
112     int32_t ret;
113 
114     (void)self;
115     if (ifName == NULL || paramBuf == NULL) {
116         HDF_LOGE("%{public}s input parameter invalid!", __func__);
117         return HDF_ERR_INVALID_PARAM;
118     }
119     if (g_wlanExtendVdiImpl == NULL) {
120         HDF_LOGE("%{public}s g_wlanExtendVdiImpl is NULL!", __func__);
121         return HDF_FAILURE;
122     }
123     ret = g_wlanExtendVdiImpl->sendCmdIoctl(self, ifName, cmdId, paramBuf, paramBufLen);
124     if (ret != HDF_SUCCESS) {
125         HDF_LOGE("%{public}s: send ioctl command failed!, error code: %{public}d", __func__, ret);
126     }
127     return ret;
128 }
129 
WlanInterfaceGetCoexChannelList(struct IWlanInterface * self,const char * ifName,uint8_t * paramBuf,uint32_t * paramBufLen)130 int32_t WlanInterfaceGetCoexChannelList(struct IWlanInterface *self, const char *ifName,
131     uint8_t *paramBuf, uint32_t *paramBufLen)
132 {
133     int32_t ret;
134     HDF_LOGI("%{public}s enter WlanInterfaceGetCoexChannelList", __func__);
135     (void)self;
136     if (ifName == NULL || paramBuf == NULL) {
137         HDF_LOGE("%{public}s input parameter invalid!", __func__);
138         return HDF_ERR_INVALID_PARAM;
139     }
140     if (g_wlanExtendVdiImpl == NULL) {
141         HDF_LOGE("%{public}s g_wlanExtendVdiImpl is NULL!", __func__);
142         return HDF_FAILURE;
143     }
144     ret = g_wlanExtendVdiImpl->getCoexChannelList(self, ifName, paramBuf, paramBufLen);
145     if (ret != HDF_SUCCESS) {
146         HDF_LOGE("%{public}s: get coex channellist failed!, error code: %{public}d", __func__, ret);
147     }
148     return ret;
149 }
150 
WlanInterfaceRegisterHid2dCallback(Hid2dCallbackFunc func,const char * ifName)151 int32_t WlanInterfaceRegisterHid2dCallback(Hid2dCallbackFunc func, const char *ifName)
152 {
153     int ret;
154 
155     if (func == NULL || ifName == NULL) {
156         HDF_LOGE("%{public}s input parameter invalid!", __func__);
157         return HDF_ERR_INVALID_PARAM;
158     }
159     if (g_wlanExtendVdiImpl == NULL) {
160         HDF_LOGE("%{public}s g_wlanExtendVdiImpl is NULL!", __func__);
161         return HDF_FAILURE;
162     }
163     ret = g_wlanExtendVdiImpl->registerHid2dCallback(func, ifName);
164     if (ret != HDF_SUCCESS) {
165         HDF_LOGE("%{public}s: Register hid2d callback failed!, error code: %{public}d", __func__, ret);
166     }
167     return ret;
168 }
169 
WlanInterfaceUnregisterHid2dCallback(Hid2dCallbackFunc func,const char * ifName)170 int32_t WlanInterfaceUnregisterHid2dCallback(Hid2dCallbackFunc func, const char *ifName)
171 {
172     int ret;
173 
174     if (func == NULL || ifName == NULL) {
175         HDF_LOGE("%{public}s input parameter invalid!", __func__);
176         return HDF_ERR_INVALID_PARAM;
177     }
178     if (g_wlanExtendVdiImpl == NULL) {
179         HDF_LOGE("%{public}s g_wlanExtendVdiImpl is NULL!", __func__);
180         return HDF_FAILURE;
181     }
182     ret = g_wlanExtendVdiImpl->unregisterHid2dCallback(func, ifName);
183     if (ret != HDF_SUCCESS) {
184         HDF_LOGE("%{public}s: Unregister hid2d callback failed!, error code: %{public}d", __func__, ret);
185     }
186     return ret;
187 }
188 
WlanExtendInterfaceWifiConstruct(void)189 int32_t WlanExtendInterfaceWifiConstruct(void)
190 {
191     if (g_wlanExtendVdiImpl != NULL) {
192         HDF_LOGI("%{public}s wlanExtendVdiImpl is not NULL!", __func__);
193         return HDF_SUCCESS;
194     }
195 
196     int32_t ret;
197     ret = InitWlanExtendVdiImpl();
198     if (ret != HDF_SUCCESS) {
199         HDF_LOGE("%{public}s construct WiFi failed! error code: %{public}d", __func__, ret);
200         return HDF_FAILURE;
201     }
202 
203     if (g_wlanExtendVdiImpl == NULL) {
204         HDF_LOGE("%{public}s wlanExtendVdiImpl init failed!", __func__);
205         CloseVdi();
206         return HDF_FAILURE;
207     }
208     ret = g_wlanExtendVdiImpl->wifiConstruct();
209     if (ret != HDF_SUCCESS) {
210         HDF_LOGE("%{public}s construct WiFi failed! error code: %{public}d", __func__, ret);
211         CloseVdi();
212     }
213     return ret;
214 }
215 
WlanExtendInterfaceWifiDestruct(void)216 int32_t WlanExtendInterfaceWifiDestruct(void)
217 {
218     if (g_wlanExtendVdiImpl == NULL) {
219         HDF_LOGI("%{public}s wlanExtendVdiImpl is NULL!", __func__);
220         return HDF_SUCCESS;
221     }
222 
223     int32_t ret;
224     ret = g_wlanExtendVdiImpl->wifiDestruct();
225     if (ret != HDF_SUCCESS) {
226         HDF_LOGE("%{public}s destruct WiFi failed! error code: %{public}d", __func__, ret);
227     }
228     CloseVdi();
229     g_wlanExtendVdiImpl = NULL;
230     return ret;
231 }
232