• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "p2p.h"
10 #include "message/message_router.h"
11 #include "message/sidecar.h"
12 #include "wifi_base.h"
13 #include "wifi_mac80211_ops.h"
14 #include "hdf_wlan_services.h"
15 #include "hdf_wlan_utils.h"
16 
17 #define HDF_LOG_TAG HDF_WIFI_CORE
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
RemainOnChannel(struct NetDevice * netdev,WifiOnChannel * onChannel)22 static int32_t RemainOnChannel(struct NetDevice *netdev, WifiOnChannel *onChannel)
23 {
24     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
25     if (chipDriver == NULL) {
26         HDF_LOGE("%s:bad net device found!", __func__);
27         return HDF_FAILURE;
28     }
29 
30     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, RemainOnChannel);
31     return chipDriver->p2pOps->RemainOnChannel(netdev, onChannel);
32 }
33 
WifiCmdRemainOnChannel(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)34 static int32_t  WifiCmdRemainOnChannel(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
35 {
36     int32_t ret;
37     struct NetDevice *netdev = NULL;
38     const char *ifName = NULL;
39     WifiOnChannel wifiOnChannel = {0};
40 
41     (void)context;
42     if (reqData == NULL || rspData == NULL) {
43         return HDF_ERR_INVALID_PARAM;
44     }
45     ifName = HdfSbufReadString(reqData);
46     if (ifName == NULL) {
47         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
48         return HDF_FAILURE;
49     }
50     netdev = NetDeviceGetInstByName(ifName);
51     if (netdev == NULL) {
52         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
53         return HDF_FAILURE;
54     }
55     if (!HdfSbufReadUint32(reqData, &(wifiOnChannel.freq))) {
56         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "freq");
57         return HDF_FAILURE;
58     }
59     if (!HdfSbufReadUint32(reqData, &(wifiOnChannel.duration))) {
60         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "duration");
61         return HDF_FAILURE;
62     }
63     ret = RemainOnChannel(netdev, &wifiOnChannel);
64     if (ret != HDF_SUCCESS) {
65         HDF_LOGE("%s: fail to remain on channel,%d", __func__, ret);
66     }
67     return ret;
68 }
69 
ProbeReqReport(struct NetDevice * netdev,int32_t report)70 static int32_t ProbeReqReport(struct NetDevice *netdev, int32_t report)
71 {
72     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
73     if (chipDriver == NULL) {
74         HDF_LOGE("%s:bad net device found!", __func__);
75         return HDF_FAILURE;
76     }
77 
78     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, ProbeReqReport);
79     return chipDriver->p2pOps->ProbeReqReport(netdev, report);
80 }
81 
WifiCmdProbeReqReport(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)82 static int32_t WifiCmdProbeReqReport(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
83 {
84     int32_t ret;
85     struct NetDevice *netdev = NULL;
86     const char *ifName = NULL;
87     int32_t report;
88 
89     (void)context;
90     if (reqData == NULL || rspData == NULL) {
91         return HDF_ERR_INVALID_PARAM;
92     }
93     ifName = HdfSbufReadString(reqData);
94     if (ifName == NULL) {
95         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
96         return HDF_FAILURE;
97     }
98     netdev = NetDeviceGetInstByName(ifName);
99     if (netdev == NULL) {
100         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
101         return HDF_FAILURE;
102     }
103     if (!HdfSbufReadInt32(reqData, &(report))) {
104         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "report");
105         return HDF_FAILURE;
106     }
107 
108     ret = ProbeReqReport(netdev, report);
109     if (ret != HDF_SUCCESS) {
110         HDF_LOGE("%s: fail to probe req report,%d", __func__, ret);
111     }
112     return ret;
113 }
114 
CancelRemainOnChannel(struct NetDevice * netdev)115 static int32_t CancelRemainOnChannel(struct NetDevice *netdev)
116 {
117     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
118     if (chipDriver == NULL) {
119         HDF_LOGE("%s:bad net device found!", __func__);
120         return HDF_FAILURE;
121     }
122 
123     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, CancelRemainOnChannel);
124     return chipDriver->p2pOps->CancelRemainOnChannel(netdev);
125 }
126 
WifiCmdCancelRemainOnChannel(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)127 static int32_t WifiCmdCancelRemainOnChannel(const RequestContext *context, struct HdfSBuf *reqData,
128     struct HdfSBuf *rspData)
129 {
130     int32_t ret;
131     struct NetDevice *netdev = NULL;
132     const char *ifName = NULL;
133 
134     (void)context;
135     if (reqData == NULL || rspData == NULL) {
136         return HDF_ERR_INVALID_PARAM;
137     }
138     ifName = HdfSbufReadString(reqData);
139     if (ifName == NULL) {
140         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
141         return HDF_FAILURE;
142     }
143     netdev = NetDeviceGetInstByName(ifName);
144     if (netdev == NULL) {
145         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
146         return HDF_FAILURE;
147     }
148 
149     ret = CancelRemainOnChannel(netdev);
150     if (ret != HDF_SUCCESS) {
151         HDF_LOGE("%s: fail to cancel remain on channel,%d", __func__, ret);
152     }
153     return ret;
154 }
155 
AddIf(struct NetDevice * netdev,WifiIfAdd * ifAdd)156 static int32_t AddIf(struct NetDevice *netdev, WifiIfAdd *ifAdd)
157 {
158     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
159     if (chipDriver == NULL) {
160         HDF_LOGE("%s:bad net device found!", __func__);
161         return HDF_FAILURE;
162     }
163 
164     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, AddIf);
165     return chipDriver->p2pOps->AddIf(netdev, ifAdd);
166 }
167 
WifiCmdAddIf(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)168 static int32_t WifiCmdAddIf(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
169 {
170     int32_t ret;
171     struct NetDevice *netdev = NULL;
172     const char *ifName = NULL;
173     WifiIfAdd ifAdd = {0};
174 
175     (void)context;
176     if (reqData == NULL || rspData == NULL) {
177         return HDF_ERR_INVALID_PARAM;
178     }
179     ifName = HdfSbufReadString(reqData);
180     if (ifName == NULL) {
181         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
182         return HDF_FAILURE;
183     }
184     netdev = NetDeviceGetInstByName(ifName);
185     if (netdev == NULL) {
186         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
187         return HDF_FAILURE;
188     }
189 
190     if (!HdfSbufReadUint8(reqData, &(ifAdd.type))) {
191         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "type");
192         return HDF_FAILURE;
193     }
194 
195     ret = AddIf(netdev, &ifAdd);
196     if (ret != HDF_SUCCESS) {
197         HDF_LOGE("%s: fail to cancel remain on channel,%d", __func__, ret);
198     }
199     return ret;
200 }
201 
RemoveIf(struct NetDevice * netdev,WifiIfRemove * ifRemove)202 static int32_t RemoveIf(struct NetDevice *netdev, WifiIfRemove *ifRemove)
203 {
204     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
205     if (chipDriver == NULL) {
206         HDF_LOGE("%s:bad net device found!", __func__);
207         return HDF_FAILURE;
208     }
209 
210     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, RemoveIf);
211     return chipDriver->p2pOps->RemoveIf(netdev, ifRemove);
212 }
213 
WifiCmdRemoveIf(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)214 static int32_t WifiCmdRemoveIf(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
215 {
216     int32_t ret;
217     struct NetDevice *netdev = NULL;
218     const char *ifName = NULL;
219     WifiIfRemove *ifRemove = NULL;
220     uint32_t dataSize;
221     (void)context;
222     if (reqData == NULL || rspData == NULL) {
223         return HDF_ERR_INVALID_PARAM;
224     }
225     ifName = HdfSbufReadString(reqData);
226     if (ifName == NULL) {
227         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
228         return HDF_FAILURE;
229     }
230     netdev = NetDeviceGetInstByName(ifName);
231     if (netdev == NULL) {
232         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
233         return HDF_FAILURE;
234     }
235 
236     dataSize = 0;
237     if (!HdfSbufReadBuffer(reqData, (const void **)&(ifRemove), &dataSize) || dataSize != sizeof(WifiIfRemove)) {
238         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
239         return HDF_FAILURE;
240     }
241     ret = RemoveIf(netdev, ifRemove);
242     if (ret != HDF_SUCCESS) {
243         HDF_LOGE("%s: fail to remove interface,%d", __func__, ret);
244     }
245     return ret;
246 }
247 
SetApWpsP2pIe(struct NetDevice * netdev,WifiAppIe * appIe)248 static int32_t SetApWpsP2pIe(struct NetDevice *netdev, WifiAppIe *appIe)
249 {
250     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
251     if (chipDriver == NULL) {
252         HDF_LOGE("%s:bad net device found!", __func__);
253         return HDF_FAILURE;
254     }
255 
256     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, SetApWpsP2pIe);
257     return chipDriver->p2pOps->SetApWpsP2pIe(netdev, appIe);
258 }
259 
WifiCmdSetApWpsP2pIe(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)260 static int32_t WifiCmdSetApWpsP2pIe(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
261 {
262     int32_t ret;
263     struct NetDevice *netdev = NULL;
264     const char *ifName = NULL;
265     WifiAppIe appIe = {0};
266 
267     (void)context;
268     if (reqData == NULL || rspData == NULL) {
269         return HDF_ERR_INVALID_PARAM;
270     }
271     ifName = HdfSbufReadString(reqData);
272     if (ifName == NULL) {
273         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
274         return HDF_FAILURE;
275     }
276     netdev = NetDeviceGetInstByName(ifName);
277     if (netdev == NULL) {
278         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
279         return HDF_FAILURE;
280     }
281 
282     if (!HdfSbufReadUint32(reqData, &(appIe.ieLen))) {
283         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ieLen");
284         return HDF_FAILURE;
285     }
286     if (!HdfSbufReadUint8(reqData, &(appIe.appIeType))) {
287         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "appIeType");
288         return HDF_FAILURE;
289     }
290     if (!HdfSbufReadBuffer(reqData, (const void**)&(appIe.ie), &(appIe.ieLen))) {
291         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "appIeType");
292         return HDF_FAILURE;
293     }
294     ret = SetApWpsP2pIe(netdev, &appIe);
295     if (ret != HDF_SUCCESS) {
296         HDF_LOGE("%s: fail to setapwpsp2pie,%d", __func__, ret);
297     }
298     return ret;
299 }
300 
GetDriverFlag(struct NetDevice * netdev,WifiGetDrvFlags ** params)301 int32_t GetDriverFlag (struct NetDevice *netdev, WifiGetDrvFlags **params)
302 {
303     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
304     if (chipDriver == NULL) {
305         HDF_LOGE("%s:bad net device found!", __func__);
306         return HDF_FAILURE;
307     }
308 
309     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->p2pOps, GetDriverFlag);
310     return chipDriver->p2pOps->GetDriverFlag(netdev, params);
311 }
312 
WifiCmdGetDriverFlag(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)313 static int32_t WifiCmdGetDriverFlag(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
314 {
315     int32_t ret;
316     struct NetDevice *netdev = NULL;
317     const char *ifName = NULL;
318     WifiGetDrvFlags *params = NULL;
319 
320     (void)context;
321     if (reqData == NULL || rspData == NULL) {
322         return HDF_ERR_INVALID_PARAM;
323     }
324     ifName = HdfSbufReadString(reqData);
325     if (ifName == NULL) {
326         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
327         return HDF_FAILURE;
328     }
329     netdev = NetDeviceGetInstByName(ifName);
330     if (netdev == NULL) {
331         HDF_LOGE("%s: netdev not found!ifName=%s", __func__, ifName);
332         return HDF_FAILURE;
333     }
334 
335     ret = GetDriverFlag(netdev, &params);
336     if (ret != HDF_SUCCESS) {
337         HDF_LOGE("%s: fail to getdriverflag,%d", __func__, ret);
338         return HDF_FAILURE;
339     }
340 
341     if (!HdfSbufWriteUint64(rspData, params->drvFlags)) {
342         HDF_LOGE("%s:%s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
343         ret = HDF_ERR_IO;
344     }
345 
346     HDF_LOGE("WifiCmdGetDriverFlag:%llx", params->drvFlags);
347     return ret;
348 }
349 
350 static struct MessageDef g_wifiP2pFeatureCmds[] = {
351     DUEMessage(CMD_P2P_PROBE_REQ_REPORT, WifiCmdProbeReqReport, 0),
352     DUEMessage(CMD_P2P_REMAIN_ON_CHANNEL, WifiCmdRemainOnChannel, 0),
353     DUEMessage(CMD_P2P_CANCEL_REMAIN_ON_CHANNEL, WifiCmdCancelRemainOnChannel, 0),
354     DUEMessage(CMD_P2P_ADD_IF, WifiCmdAddIf, 0),
355     DUEMessage(CMD_P2P_REMOVE_IF, WifiCmdRemoveIf, 0),
356     DUEMessage(CMD_P2P_SET_AP_WPS_P2P_IE, WifiCmdSetApWpsP2pIe, 0),
357     DUEMessage(CMD_P2P_GET_DRIVER_FLAGS, WifiCmdGetDriverFlag, 0),
358 };
359 ServiceDefine(P2PService, P2P_SERVICE_ID, g_wifiP2pFeatureCmds);
360 
361 static Service *g_p2pService = NULL;
362 
P2pInit(struct WifiFeature * feature)363 int32_t P2pInit(struct WifiFeature *feature)
364 {
365     (void)feature;
366     if (g_p2pService == NULL) {
367         ServiceCfg cfg = {
368             .dispatcherId = DEFAULT_DISPATCHER_ID
369         };
370         g_p2pService = CreateService(P2PService, &cfg);
371         if (g_p2pService == NULL) {
372             return HDF_FAILURE;
373         }
374     }
375     return HDF_SUCCESS;
376 }
377 
P2pDeinit(struct WifiFeature * feature)378 int32_t P2pDeinit(struct WifiFeature *feature)
379 {
380     (void)feature;
381     if (g_p2pService != NULL && g_p2pService->Destroy != NULL) {
382         g_p2pService->Destroy(g_p2pService);
383         g_p2pService = NULL;
384     }
385     return HDF_SUCCESS;
386 }
387 
388 struct WifiFeature g_wifiP2PFeature = {
389     .name = "p2p",
390     .init = P2pInit,
391     .deInit = P2pDeinit
392 };
393 
GetWifiP2pFeature(void)394 struct WifiFeature *GetWifiP2pFeature(void)
395 {
396     return &g_wifiP2PFeature;
397 }
398 
399 #ifdef __cplusplus
400 }
401 #endif
402