1 /*
2  * Copyright (c) 2020-2022 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 "ap.h"
10 #include <osal_mem.h>
11 #include <securec.h>
12 #include "message/message_router.h"
13 #include "message/sidecar.h"
14 #include "wifi_base.h"
15 #include "wifi_mac80211_ops.h"
16 #include "hdf_wlan_services.h"
17 #include "hdf_wlan_utils.h"
18 
19 #define HDF_LOG_TAG HDF_WIFI_CORE
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
ChangeBeacon(struct NetDevice * netDev,WifiApSetting * apSettings)24 static uint32_t ChangeBeacon(struct NetDevice *netDev, WifiApSetting *apSettings)
25 {
26     struct WlanBeaconConf beaconConf = { 0 };
27     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
28     if (chipDriver == NULL) {
29         HDF_LOGE("%s:bad net device found!", __func__);
30         return HDF_FAILURE;
31     }
32     if (netDev == NULL || apSettings == NULL) {
33         HDF_LOGE("%s: parameter null", __func__);
34         return HDF_FAILURE;
35     }
36     beaconConf.interval = apSettings->beaconInterval;
37     beaconConf.DTIMPeriod = apSettings->dtimPeriod;
38     beaconConf.hiddenSSID = (apSettings->hiddenSsid == 1);
39     beaconConf.headIEs = apSettings->beaconData.head;
40     beaconConf.headIEsLength = apSettings->beaconData.headLen;
41     beaconConf.tailIEs = apSettings->beaconData.tail;
42     beaconConf.tailIEsLength = apSettings->beaconData.tailLen;
43     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, ConfigBeacon);
44     HDF_LOGD("%s: ChangeBeacon finished!", __func__);
45     return chipDriver->apOps->ConfigBeacon(netDev, &beaconConf);
46 }
47 
StartAp(struct NetDevice * netDev,WifiApSetting * apSettings)48 static int32_t StartAp(struct NetDevice *netDev, WifiApSetting *apSettings)
49 {
50     struct WlanAPConf apConf = { 0 };
51     int32_t ret;
52     struct HdfChipDriver *chipDriver = NULL;
53     errno_t err;
54     HDF_LOGI("%s: Starting ap...", __func__);
55     chipDriver = GetChipDriver(netDev);
56     if (chipDriver == NULL) {
57         HDF_LOGE("%s:bad net device found!", __func__);
58         return HDF_FAILURE;
59     }
60     err = memcpy_s(apConf.ssidConf.ssid, IEEE80211_MAX_SSID_LEN, apSettings->ssid, apSettings->ssidLen);
61     if (err != EOK) {
62         HDF_LOGE("%s: memcpy_s failed!ret=%d", __func__, err);
63         return HDF_FAILURE;
64     }
65     apConf.ssidConf.ssidLen = apSettings->ssidLen;
66     apConf.centerFreq1 = apSettings->freqParams.centerFreq1;
67     apConf.channel = apSettings->freqParams.channel;
68     apConf.width = apSettings->freqParams.bandwidth;
69     apConf.band = apSettings->freqParams.band;
70     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, ConfigAp);
71     ret = chipDriver->apOps->ConfigAp(netDev, &apConf);
72     if (ret != HDF_SUCCESS) {
73         HDF_LOGE("%s:ConfigAp failed!ret=%d", __func__, ret);
74         return HDF_FAILURE;
75     }
76     ret = ChangeBeacon(netDev, apSettings);
77     if (ret != HDF_SUCCESS) {
78         HDF_LOGE("%s:ChangeBeacon failed!ret=%d", __func__, ret);
79         return HDF_FAILURE;
80     }
81     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, StartAp);
82     ret = chipDriver->apOps->StartAp(netDev);
83     if (ret != HDF_SUCCESS) {
84         HDF_LOGE("%s:StartAp failed!ret=%d", __func__, ret);
85         return HDF_FAILURE;
86     }
87     HDF_LOGI("%s: Start ap finished!", __func__);
88     return NetIfSetStatus(netDev, NETIF_UP);
89 }
90 
StopAp(struct NetDevice * netDev)91 static uint32_t StopAp(struct NetDevice *netDev)
92 {
93     uint32_t ret;
94     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
95     if (chipDriver == NULL) {
96         HDF_LOGE("%s:bad net device found!", __func__);
97         return HDF_FAILURE;
98     }
99     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, StopAp);
100     ret = chipDriver->apOps->StopAp(netDev);
101     if (ret != HDF_SUCCESS) {
102         HDF_LOGE("StopAp:failed, error[%d]", ret);
103         return ret;
104     }
105     HDF_LOGI("%s: StopAp finished!", __func__);
106     return NetIfSetStatus(netDev, NETIF_DOWN);
107 }
108 
DelStation(struct NetDevice * netDev,struct StationDelParameters * params)109 static uint32_t DelStation(struct NetDevice *netDev, struct StationDelParameters *params)
110 {
111     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
112     if (chipDriver == NULL) {
113         HDF_LOGE("%s:bad net device found!", __func__);
114         return HDF_FAILURE;
115     }
116     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, DelStation);
117     return chipDriver->apOps->DelStation(netDev, params->mac);
118 }
119 
SetCountryCode(struct NetDevice * netDev,const char * code,uint32_t len)120 static uint32_t SetCountryCode(struct NetDevice *netDev, const char *code, uint32_t len)
121 {
122     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
123     if (chipDriver == NULL) {
124         HDF_LOGE("%s:bad net device found!", __func__);
125         return HDF_FAILURE;
126     }
127     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, SetCountryCode);
128     return chipDriver->apOps->SetCountryCode(netDev, code, len);
129 }
130 
GetAssociatedStasCount(struct NetDevice * netDev,uint32_t * num)131 static uint32_t GetAssociatedStasCount(struct NetDevice *netDev, uint32_t *num)
132 {
133     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
134     if (chipDriver == NULL) {
135         HDF_LOGE("%s:bad net device found!", __func__);
136         return HDF_FAILURE;
137     }
138     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, GetAssociatedStasCount);
139     return chipDriver->apOps->GetAssociatedStasCount(netDev, num);
140 }
141 
GetAssociatedStasInfo(struct NetDevice * netDev,WifiStaInfo * staInfo,uint32_t num)142 static uint32_t GetAssociatedStasInfo(struct NetDevice *netDev, WifiStaInfo *staInfo, uint32_t num)
143 {
144     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
145     if (chipDriver == NULL) {
146         HDF_LOGE("%s:bad net device found!", __func__);
147         return HDF_FAILURE;
148     }
149     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->apOps, GetAssociatedStasInfo);
150     return chipDriver->apOps->GetAssociatedStasInfo(netDev, staInfo, num);
151 }
152 
WifiFillApSettingsParams(struct HdfSBuf * reqData,WifiApSetting * apSettings)153 static int32_t WifiFillApSettingsParams(struct HdfSBuf *reqData,  WifiApSetting *apSettings)
154 {
155     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.mode))) {
156         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "mode");
157         return HDF_FAILURE;
158     }
159     HDF_LOGI("%s: ApSettings->freqParams.mode=%d", __func__, apSettings->freqParams.mode);
160     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.freq))) {
161         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "freq");
162         return HDF_FAILURE;
163     }
164     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.channel))) {
165         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "channel");
166         return HDF_FAILURE;
167     }
168     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.htEnabled))) {
169         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "htEnabled");
170         return HDF_FAILURE;
171     }
172     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.secChannelOffset))) {
173         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "secChannelOffset");
174         return HDF_FAILURE;
175     }
176     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.vhtEnabled))) {
177         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "vhtEnabled");
178         return HDF_FAILURE;
179     }
180     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.centerFreq1))) {
181         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "centerFreq1");
182         return HDF_FAILURE;
183     }
184     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.centerFreq2))) {
185         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "centerFreq2");
186         return HDF_FAILURE;
187     }
188     if (!HdfSbufReadInt32(reqData, &(apSettings->freqParams.bandwidth))) {
189         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "bandwidth");
190         return HDF_FAILURE;
191     }
192     if (!HdfSbufReadUint8(reqData, &(apSettings->freqParams.band))) {
193         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "band");
194         return HDF_FAILURE;
195     }
196     return HDF_SUCCESS;
197 }
198 
WifiCmdSetApInner(struct HdfSBuf * reqData,WifiApSetting * apSettings)199 static int32_t WifiCmdSetApInner(struct HdfSBuf *reqData,  WifiApSetting *apSettings)
200 {
201     if (WifiFillApSettingsParams(reqData, apSettings) != HDF_SUCCESS) {
202         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "apSettings");
203         return HDF_FAILURE;
204     }
205     if (!HdfSbufReadInt32(reqData, &(apSettings->beaconInterval))) {
206         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "beaconInterval");
207         return HDF_FAILURE;
208     }
209     if (!HdfSbufReadInt32(reqData, &(apSettings->dtimPeriod))) {
210         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "dtimPeriod");
211         return HDF_FAILURE;
212     }
213     if (!HdfSbufReadUint8(reqData, &(apSettings->hiddenSsid))) {
214         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "hiddenSsid");
215         return HDF_FAILURE;
216     }
217     if (!HdfSbufReadUint8(reqData, &(apSettings->authType))) {
218         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "authType");
219         return HDF_FAILURE;
220     }
221     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings->beaconData.head), &(apSettings->beaconData.headLen))) {
222         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "head");
223         return HDF_FAILURE;
224     }
225     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings->beaconData.tail), &(apSettings->beaconData.tailLen))) {
226         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "tail");
227         return HDF_FAILURE;
228     }
229     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings->ssid), &(apSettings->ssidLen))) {
230         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ssid");
231         return HDF_FAILURE;
232     }
233     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings->meshSsid), &(apSettings->meshSsidLen))) {
234         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "meshSsid");
235         return HDF_FAILURE;
236     }
237     return HDF_SUCCESS;
238 }
239 
WifiCmdSetAp(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)240 static int32_t WifiCmdSetAp(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
241 {
242     WifiApSetting apSettings;
243     const char *ifName = NULL;
244     struct NetDevice *netdev = NULL;
245     int32_t ret = 0;
246     (void)context;
247     (void)rspData;
248     if (reqData == NULL) {
249         HDF_LOGE("%s: reqData is NULL", __func__);
250         return HDF_ERR_INVALID_PARAM;
251     }
252 
253     ifName = HdfSbufReadString(reqData);
254     if (ifName == NULL) {
255         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
256         return HDF_FAILURE;
257     }
258 
259     if (WifiCmdSetApInner(reqData, &apSettings) != HDF_SUCCESS) {
260         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "apSettings");
261         return HDF_FAILURE;
262     }
263 
264     netdev = NetDeviceGetInstByName(ifName);
265     if (netdev == NULL) {
266         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
267         return HDF_FAILURE;
268     }
269     ret = StartAp(netdev, &apSettings);
270     HDF_LOGI("%s: WifiCmdSetAp finished!", __func__);
271     return ret;
272 }
273 
WifiCmdStopAp(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)274 static int32_t WifiCmdStopAp(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
275 {
276     const char *ifName = NULL;
277     struct NetDevice *netdev = NULL;
278     (void)context;
279     (void)rspData;
280     if (reqData == NULL) {
281         HDF_LOGE("%s: reqData is NULL", __func__);
282         return HDF_ERR_INVALID_PARAM;
283     }
284     ifName = HdfSbufReadString(reqData);
285     if (ifName == NULL) {
286         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
287         return HDF_FAILURE;
288     }
289     netdev = NetDeviceGetInstByName(ifName);
290     if (netdev == NULL) {
291         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
292         return HDF_FAILURE;
293     }
294     HDF_LOGI("%s:%s stopping AP ...", __func__, ifName);
295     return StopAp(netdev);
296 }
297 
WifiCmdChangeBeacon(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)298 static int32_t WifiCmdChangeBeacon(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
299 {
300     struct NetDevice *netdev = NULL;
301     WifiApSetting apSettings;
302     const char *ifName = NULL;
303     (void)context;
304     (void)rspData;
305     if (reqData == NULL) {
306         HDF_LOGE("%s: reqData is NULL", __func__);
307         return HDF_ERR_INVALID_PARAM;
308     }
309     ifName = HdfSbufReadString(reqData);
310     if (ifName == NULL) {
311         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
312         return HDF_FAILURE;
313     }
314     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings.beaconData.head), &(apSettings.beaconData.headLen))) {
315         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "head");
316         return HDF_FAILURE;
317     }
318     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings.beaconData.tail), &(apSettings.beaconData.tailLen))) {
319         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "tail");
320         return HDF_FAILURE;
321     }
322     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings.ssid), &(apSettings.ssidLen))) {
323         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ssid");
324         return HDF_FAILURE;
325     }
326     if (!HdfSbufReadBuffer(reqData, (const void **)&(apSettings.meshSsid), &(apSettings.meshSsidLen))) {
327         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "meshSsid");
328         return HDF_FAILURE;
329     }
330     netdev = NetDeviceGetInstByName(ifName);
331     if (netdev == NULL) {
332         HDF_LOGE("%s: Netdev not found!ifName=%s", __func__, ifName);
333         return HDF_FAILURE;
334     }
335     HDF_LOGI("%s: Wifi cmd changeBeacon finished!", __func__);
336     return ChangeBeacon(netdev, &apSettings);
337 }
338 
WifiCmdStaRemove(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)339 static int32_t WifiCmdStaRemove(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
340 {
341     struct NetDevice *netdev = NULL;
342     struct StationDelParameters params;
343     const char *ifName = NULL;
344     uint32_t dataSize = 0;
345     int32_t ret;
346     (void)context;
347     (void)rspData;
348     if (reqData == NULL) {
349         HDF_LOGE("%s: reqData is NULL", __func__);
350         return HDF_ERR_INVALID_PARAM;
351     }
352     ifName = HdfSbufReadString(reqData);
353     if (ifName == NULL) {
354         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
355         return HDF_FAILURE;
356     }
357     netdev = NetDeviceGetInstByName(ifName);
358     if (netdev == NULL) {
359         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
360         return HDF_FAILURE;
361     }
362     params.subtype = 0;
363     params.reasonCode = 0;
364     if (!HdfSbufReadBuffer(reqData, (const void **)&params.mac, &dataSize) || dataSize != ETH_ADDR_LEN) {
365         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "mac", dataSize);
366         return HDF_FAILURE;
367     }
368     ret = DelStation(netdev, &params);
369     HDF_LOGI("%s:del station XX:XX:XX:XX:XX:%02X ret=%d", __func__, params.mac[ETH_ADDR_LEN - 1], ret);
370     return ret;
371 }
372 
GetAssociatedStas(struct NetDevice * netdev,uint32_t num,struct HdfSBuf * rspData)373 static int32_t GetAssociatedStas(struct NetDevice *netdev, uint32_t num, struct HdfSBuf *rspData)
374 {
375     int32_t ret;
376     WifiStaInfo *staInfo = NULL;
377     staInfo = (WifiStaInfo *)OsalMemCalloc(sizeof(WifiStaInfo) * num);
378     if (staInfo == NULL) {
379         HDF_LOGE("%s: OsalMemCalloc failed!", __func__);
380         return HDF_FAILURE;
381     }
382     ret = GetAssociatedStasInfo(netdev, staInfo, num);
383     if (ret != HDF_SUCCESS) {
384         HDF_LOGE("%s: fail to get sta info,%d", __func__, ret);
385         OsalMemFree(staInfo);
386         return ret;
387     }
388     if (!HdfSbufWriteBuffer(rspData, staInfo, sizeof(WifiStaInfo) * num)) {
389         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
390         OsalMemFree(staInfo);
391         return HDF_FAILURE;
392     }
393     OsalMemFree(staInfo);
394     return HDF_SUCCESS;
395 }
396 
WifiCmdGetAssociatedStas(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)397 static int32_t WifiCmdGetAssociatedStas(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
398 {
399     int32_t ret;
400     struct NetDevice *netdev = NULL;
401     const char *ifName = NULL;
402     uint32_t num;
403     (void)context;
404     if (reqData == NULL || rspData == NULL) {
405         return HDF_ERR_INVALID_PARAM;
406     }
407     ifName = HdfSbufReadString(reqData);
408     if (ifName == NULL) {
409         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
410         return HDF_FAILURE;
411     }
412     netdev = NetDeviceGetInstByName(ifName);
413     if (netdev == NULL) {
414         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
415         return HDF_FAILURE;
416     }
417     ret = GetAssociatedStasCount(netdev, &num);
418     if (ret != HDF_SUCCESS) {
419         HDF_LOGE("%s: fail to get user num,ret=%d", __func__, ret);
420         return ret;
421     }
422     if (!HdfSbufWriteUint32(rspData, num)) {
423         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
424         return HDF_FAILURE;
425     }
426     if (num == 0) {
427         return HDF_SUCCESS;
428     }
429     ret = GetAssociatedStas(netdev, num, rspData);
430     if (ret != HDF_SUCCESS) {
431         HDF_LOGE("%s: fail to GetAssociatedStas,%d", __func__, ret);
432     }
433     return ret;
434 }
435 
WifiCmdSetCountryCode(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)436 static int32_t WifiCmdSetCountryCode(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
437 {
438     int32_t ret;
439     struct NetDevice *netdev = NULL;
440     const char *ifName = NULL;
441     const char *code = NULL;
442     uint32_t replayDataSize;
443     (void)context;
444     if (reqData == NULL || rspData == NULL) {
445         return HDF_ERR_INVALID_PARAM;
446     }
447     ifName = HdfSbufReadString(reqData);
448     if (ifName == NULL) {
449         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
450         return HDF_FAILURE;
451     }
452     netdev = NetDeviceGetInstByName(ifName);
453     if (netdev == NULL) {
454         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
455         return HDF_FAILURE;
456     }
457     if (!HdfSbufReadBuffer(reqData, (const void **)&code, &replayDataSize)) {
458         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "code");
459         return HDF_FAILURE;
460     }
461     ret = SetCountryCode(netdev, code, replayDataSize);
462     if (ret != HDF_SUCCESS) {
463         HDF_LOGE("%s: fail to set country code,%d", __func__, ret);
464     }
465     return ret;
466 }
467 
OpsGetApBandwidth(const char * ifName,uint8_t * bandwidth)468 static uint32_t OpsGetApBandwidth(const char *ifName, uint8_t *bandwidth)
469 {
470     struct NetDevice *netdev = NULL;
471 
472     if (ifName == NULL || bandwidth == NULL) {
473         HDF_LOGE("%s: Invalid parameter", __func__);
474         return HDF_FAILURE;
475     }
476     netdev = NetDeviceGetInstByName(ifName);
477     if (netdev == NULL) {
478         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
479         return HDF_FAILURE;
480     }
481     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
482     if (chipDriver == NULL) {
483         HDF_LOGE("%s:bad net device found!", __func__);
484         return HDF_FAILURE;
485     }
486     if (chipDriver->ops == NULL) {
487         return HDF_ERR_INVALID_OBJECT;
488     }
489     return HDF_ERR_NOT_SUPPORT;
490 }
491 
WifiCmdGetApBandwidth(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)492 static int32_t WifiCmdGetApBandwidth(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
493 {
494     int32_t ret;
495     const char *ifName = NULL;
496     uint8_t bandwidth;
497     (void)context;
498 
499     if (reqData == NULL || rspData == NULL) {
500         return HDF_ERR_INVALID_PARAM;
501     }
502     ifName = HdfSbufReadString(reqData);
503     if (ifName == NULL) {
504         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
505         return HDF_FAILURE;
506     }
507     ret = OpsGetApBandwidth(ifName, &bandwidth);
508     if (ret != HDF_SUCCESS) {
509         HDF_LOGE("%s: fail to get power mode, %d", __func__, ret);
510         return ret;
511     }
512     HDF_LOGI("%s: ap bandwidth: %d", __func__, bandwidth);
513     if (!HdfSbufWriteUint8(rspData, bandwidth)) {
514         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
515         return HDF_FAILURE;
516     }
517 
518     return HDF_SUCCESS;
519 }
520 
521 
522 static struct MessageDef g_wifiApFeatureCmds[] = {
523     DUEMessage(CMD_AP_START, WifiCmdSetAp, 0),
524     DUEMessage(CMD_AP_STOP, WifiCmdStopAp, 0),
525     DUEMessage(CMD_AP_CHANGE_BEACON, WifiCmdChangeBeacon, 0),
526     DUEMessage(CMD_AP_DEL_STATION, WifiCmdStaRemove, 0),
527     DUEMessage(CMD_AP_GET_ASSOC_STA, WifiCmdGetAssociatedStas, 0),
528     DUEMessage(CMD_AP_SET_COUNTRY_CODE, WifiCmdSetCountryCode, 0),
529     DUEMessage(CMD_AP_GET_BANDWIDTH, WifiCmdGetApBandwidth, 0)
530 };
531 ServiceDefine(APService, AP_SERVICE_ID, g_wifiApFeatureCmds);
532 
533 static Service *g_apService = NULL;
534 
ApInit(struct WifiFeature * feature)535 static int32_t ApInit(struct WifiFeature *feature)
536 {
537     (void)feature;
538     if (g_apService == NULL) {
539         ServiceCfg cfg = {
540             .dispatcherId = DEFAULT_DISPATCHER_ID
541         };
542         g_apService = CreateService(APService, &cfg);
543         if (g_apService == NULL) {
544             return HDF_FAILURE;
545         }
546     }
547     return HDF_SUCCESS;
548 }
549 
ApDeinit(struct WifiFeature * feature)550 static int32_t ApDeinit(struct WifiFeature *feature)
551 {
552     (void)feature;
553     if (g_apService != NULL && g_apService->Destroy != NULL) {
554         g_apService->Destroy(g_apService);
555         g_apService = NULL;
556     }
557     return HDF_SUCCESS;
558 }
559 
560 struct WifiFeature g_apFeature = {
561     .name = "ap",
562     .init = ApInit,
563     .deInit = ApDeinit
564 };
565 
GetWifiApFeature(void)566 struct WifiFeature *GetWifiApFeature(void)
567 {
568     return &g_apFeature;
569 }
570 
571 #ifdef __cplusplus
572 }
573 #endif
574