blob: b94f29c41218e93833d0210383eadb00d526fba3 [file] [log] [blame]
developer72fb0bb2023-01-11 09:46:29 +08001/*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2019 RDK Management
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18*/
19
20/*
21* Material from the TR181 data model is Copyright (c) 2010-2017, Broadband Forum
22* Licensed under the BSD-3 license
23*/
24
25/*
26* This file includes material that is Copyright (c) 2020, Plume Design Inc.
27* Licensed under the BSD-3 license
28*/
29
30/* Code in rxStatsInfo_callback and other callbacks is credited as follows:
developera3511852023-06-14 14:12:59 +080031Copyright (c) 2007, 2008 Johannes Berg
32Copyright (c) 2007 Andy Lutomirski
33Copyright (c) 2007 Mike Kershaw
34Copyright (c) 2008-2009 Luis R. Rodriguez
developer72fb0bb2023-01-11 09:46:29 +080035Licensed under the ISC license
36*/
37#define MTK_IMPL
38#define HAL_NETLINK_IMPL
39#define _GNU_SOURCE /* needed for strcasestr */
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <string.h>
45#include <fcntl.h>
46#include <stdbool.h>
47#include "wifi_hal.h"
48
49#ifdef HAL_NETLINK_IMPL
50#include <errno.h>
51#include <netlink/attr.h>
52#include <netlink/netlink.h>
53#include <netlink/genl/genl.h>
54#include <netlink/genl/family.h>
55#include <netlink/genl/ctrl.h>
56#include <linux/nl80211.h>
developer8dd72532023-05-17 19:58:35 +080057#include <net/if.h>
58#include <unl.h>
59#include "mtk_vendor_nl80211.h"
developer72fb0bb2023-01-11 09:46:29 +080060#endif
61
62#include <ev.h>
63#include <wpa_ctrl.h>
64#include <errno.h>
65#include <time.h>
developercc5cbfb2023-06-13 18:29:52 +080066
67#include <sys/socket.h>
68#include <sys/ioctl.h>
69#include <arpa/inet.h>
70#include <linux/if.h>
71#include <linux/if_bridge.h>
72#include <linux/sockios.h>
developer86035662023-06-28 19:21:12 +080073#include <errno.h>
74#include <limits.h>
developer33f13ba2023-07-12 16:19:06 +080075#include "secure_wrapper.h"
developercc5cbfb2023-06-13 18:29:52 +080076
developer72fb0bb2023-01-11 09:46:29 +080077#define MAC_ALEN 6
78
79#define MAX_BUF_SIZE 256
80#define MAX_CMD_SIZE 256
developerb149d9d2023-06-06 16:14:22 +080081#define MAX_SUB_CMD_SIZE 200
82
developer72fb0bb2023-01-11 09:46:29 +080083#define IF_NAME_SIZE 16
84#define CONFIG_PREFIX "/nvram/hostapd"
85#define ACL_PREFIX "/nvram/hostapd-acl"
86#define DENY_PREFIX "/nvram/hostapd-deny"
87//#define ACL_PREFIX "/tmp/wifi_acl_list" //RDKB convention
88#define SOCK_PREFIX "/var/run/hostapd/wifi"
89#define VAP_STATUS_FILE "/nvram/vap-status"
90#define ESSID_FILE "/tmp/essid"
91#define GUARD_INTERVAL_FILE "/nvram/guard-interval"
92#define CHANNEL_STATS_FILE "/tmp/channel_stats"
93#define DFS_ENABLE_FILE "/nvram/dfs_enable.txt"
94#define VLAN_FILE "/nvram/hostapd.vlan"
95#define PSK_FILE "/nvram/hostapd"
96#define MCS_FILE "/tmp/MCS"
developera1255e42023-05-13 17:45:02 +080097#define POWER_PERCENTAGE "/tmp/POWER"
98#define MGMT_POWER_CTRL "/tmp/mgmt_power_ctrl"
99/*LOGAN_DAT_FILE: may be different on customer's platform.*/
developerc7adba42023-07-14 10:58:45 +0800100#ifdef WIFI_7992
101#define LOGAN_DAT_FILE "/etc/wireless/mediatek/mt7992.b"
102#define ROM_LOGAN_DAT_FILE "/rom/etc/wireless/mediatek/mt7992.b"
103#else
developera1255e42023-05-13 17:45:02 +0800104#define LOGAN_DAT_FILE "/etc/wireless/mediatek/mt7990.b"
developerb2977562023-05-24 17:54:12 +0800105#define ROM_LOGAN_DAT_FILE "/rom/etc/wireless/mediatek/mt7990.b"
developerc7adba42023-07-14 10:58:45 +0800106#endif
developera1255e42023-05-13 17:45:02 +0800107
developer72fb0bb2023-01-11 09:46:29 +0800108#define NOACK_MAP_FILE "/tmp/NoAckMap"
developerfead3972023-05-25 20:15:02 +0800109#define RADIO_RESET_FILE "/nvram/radio_reset"
developerf6a87542023-05-16 15:47:28 +0800110
developer72fb0bb2023-01-11 09:46:29 +0800111#define BRIDGE_NAME "brlan0"
developerd1824452023-05-18 12:30:04 +0800112#define BASE_PHY_INDEX 1
113#define BASE_RADIO_INDEX 0
developer72fb0bb2023-01-11 09:46:29 +0800114
115/*
116 MAX_APS - Number of all AP available in system
117 2x Home AP
118 2x Backhaul AP
119 2x Guest AP
120 2x Secure Onboard AP
121 2x Service AP
122
123*/
124
125
126#define MAX_APS MAX_NUM_RADIOS*5
developer7e4a2a62023-04-06 19:56:03 +0800127
128#define PREFIX_WIFI2G "ra"
129#define PREFIX_WIFI5G "rai"
130#define PREFIX_WIFI6G "rax"
developer72fb0bb2023-01-11 09:46:29 +0800131
developer47cc27a2023-05-17 23:09:58 +0800132#define PREFIX_SSID_2G "RDKB_2G"
133#define PREFIX_SSID_5G "RDKB_5G"
134#define PREFIX_SSID_6G "RDKB_6G"
135
developer72fb0bb2023-01-11 09:46:29 +0800136#ifndef RADIO_PREFIX
137#define RADIO_PREFIX "wlan"
138#endif
139
140#define MAX_ASSOCIATED_STA_NUM 2007
141
142//Uncomment to enable debug logs
143//#define WIFI_DEBUG
developer49b17232023-05-19 16:35:19 +0800144enum {
145 DEBUG_OFF = 0,
146 DEBUG_ERROR = 1,
147 DEBUG_WARN = 2,
148 DEBUG_NOTICE = 3,
149 DEBUG_INFO = 4
150};
151int wifi_debug_level = DEBUG_NOTICE;
152#define wifi_debug(level, fmt, args...) \
153{ \
154 if (level <= wifi_debug_level) \
155 { \
developer2edaf012023-05-24 14:24:53 +0800156 printf("[%s][%d]"fmt"", __func__, __LINE__, ##args); \
developer49b17232023-05-19 16:35:19 +0800157 } \
158}
developer72fb0bb2023-01-11 09:46:29 +0800159
160#ifdef WIFI_DEBUG
161#define wifi_dbg_printf printf
162#define WIFI_ENTRY_EXIT_DEBUG printf
163#else
developer2f79c922023-06-02 17:33:42 +0800164#define wifi_dbg_printf(format, args...)
165#define WIFI_ENTRY_EXIT_DEBUG(format, args...)
developer72fb0bb2023-01-11 09:46:29 +0800166#endif
167
168#define HOSTAPD_CONF_0 "/nvram/hostapd0.conf" //private-wifi-2g
169#define HOSTAPD_CONF_1 "/nvram/hostapd1.conf" //private-wifi-5g
170#define HOSTAPD_CONF_4 "/nvram/hostapd4.conf" //public-wifi-2g
171#define HOSTAPD_CONF_5 "/nvram/hostapd5.conf" //public-wifi-5g
172#define DEF_HOSTAPD_CONF_0 "/usr/ccsp/wifi/hostapd0.conf"
173#define DEF_HOSTAPD_CONF_1 "/usr/ccsp/wifi/hostapd1.conf"
174#define DEF_HOSTAPD_CONF_4 "/usr/ccsp/wifi/hostapd4.conf"
175#define DEF_HOSTAPD_CONF_5 "/usr/ccsp/wifi/hostapd5.conf"
176#define DEF_RADIO_PARAM_CONF "/usr/ccsp/wifi/radio_param_def.cfg"
177#define LM_DHCP_CLIENT_FORMAT "%63d %17s %63s %63s"
178
179#define HOSTAPD_HT_CAPAB "[LDPC][SHORT-GI-20][SHORT-GI-40][MAX-AMSDU-7935]"
180
181#define BW_FNAME "/nvram/bw_file.txt"
182
183#define PS_MAX_TID 16
184
developer96b38512023-02-22 11:17:45 +0800185#define MAX_CARD_INDEX 3
186
developer72fb0bb2023-01-11 09:46:29 +0800187static wifi_radioQueueType_t _tid_ac_index_get[PS_MAX_TID] = {
developera3511852023-06-14 14:12:59 +0800188 WIFI_RADIO_QUEUE_TYPE_BE, /* 0 */
189 WIFI_RADIO_QUEUE_TYPE_BK, /* 1 */
190 WIFI_RADIO_QUEUE_TYPE_BK, /* 2 */
191 WIFI_RADIO_QUEUE_TYPE_BE, /* 3 */
192 WIFI_RADIO_QUEUE_TYPE_VI, /* 4 */
193 WIFI_RADIO_QUEUE_TYPE_VI, /* 5 */
194 WIFI_RADIO_QUEUE_TYPE_VO, /* 6 */
195 WIFI_RADIO_QUEUE_TYPE_VO, /* 7 */
196 WIFI_RADIO_QUEUE_TYPE_BE, /* 8 */
197 WIFI_RADIO_QUEUE_TYPE_BK, /* 9 */
198 WIFI_RADIO_QUEUE_TYPE_BK, /* 10 */
199 WIFI_RADIO_QUEUE_TYPE_BE, /* 11 */
200 WIFI_RADIO_QUEUE_TYPE_VI, /* 12 */
201 WIFI_RADIO_QUEUE_TYPE_VI, /* 13 */
202 WIFI_RADIO_QUEUE_TYPE_VO, /* 14 */
203 WIFI_RADIO_QUEUE_TYPE_VO, /* 15 */
developer72fb0bb2023-01-11 09:46:29 +0800204};
205
206typedef unsigned long long u64;
207
208/* Enum to define WiFi Bands */
209typedef enum
210{
developera3511852023-06-14 14:12:59 +0800211 band_invalid = -1,
212 band_2_4 = 0,
213 band_5 = 1,
214 band_6 = 2,
developer72fb0bb2023-01-11 09:46:29 +0800215} wifi_band;
216
developer17038e62023-03-02 14:43:43 +0800217char* wifi_band_str[] = {
developera3511852023-06-14 14:12:59 +0800218 "2G",
219 "5G",
220 "6G",
developer17038e62023-03-02 14:43:43 +0800221};
222
developer72fb0bb2023-01-11 09:46:29 +0800223typedef enum {
developera3511852023-06-14 14:12:59 +0800224 WIFI_MODE_A = 0x01,
225 WIFI_MODE_B = 0x02,
226 WIFI_MODE_G = 0x04,
227 WIFI_MODE_N = 0x08,
228 WIFI_MODE_AC = 0x10,
229 WIFI_MODE_AX = 0x20,
230 WIFI_MODE_BE = 0x40,
developer72fb0bb2023-01-11 09:46:29 +0800231} wifi_ieee80211_Mode;
232
developer2f79c922023-06-02 17:33:42 +0800233typedef enum {
developera3511852023-06-14 14:12:59 +0800234 HT_BW_20,
235 HT_BW_40,
developer2f79c922023-06-02 17:33:42 +0800236} ht_config_bw;
developerd1824452023-05-18 12:30:04 +0800237
developer2f79c922023-06-02 17:33:42 +0800238typedef enum {
developera3511852023-06-14 14:12:59 +0800239 VHT_BW_2040,
240 VHT_BW_80,
241 VHT_BW_160,
242 VHT_BW_8080,
developer2f79c922023-06-02 17:33:42 +0800243} vht_config_bw;
developerd1824452023-05-18 12:30:04 +0800244
developer2f79c922023-06-02 17:33:42 +0800245typedef enum {
developera3511852023-06-14 14:12:59 +0800246 EHT_BW_20,
247 EHT_BW_40,
248 EHT_BW_80,
249 EHT_BW_160,
250 EHT_BW_320,
developer2f79c922023-06-02 17:33:42 +0800251} eht_config_bw;
developerd1824452023-05-18 12:30:04 +0800252
developer72fb0bb2023-01-11 09:46:29 +0800253#ifdef WIFI_HAL_VERSION_3
254
255// Return number of elements in array
256#ifndef ARRAY_SIZE
developera3511852023-06-14 14:12:59 +0800257#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
developer72fb0bb2023-01-11 09:46:29 +0800258#endif /* ARRAY_SIZE */
259
260#ifndef ARRAY_AND_SIZE
developera3511852023-06-14 14:12:59 +0800261#define ARRAY_AND_SIZE(x) (x),ARRAY_SIZE(x)
developer72fb0bb2023-01-11 09:46:29 +0800262#endif /* ARRAY_AND_SIZE */
263
developera3511852023-06-14 14:12:59 +0800264#define WIFI_ITEM_STR(key, str) {0, sizeof(str)-1, (int)key, (intptr_t)str}
developer72fb0bb2023-01-11 09:46:29 +0800265
266typedef struct {
developera3511852023-06-14 14:12:59 +0800267 int32_t value;
268 int32_t param;
269 intptr_t key;
270 intptr_t data;
developer72fb0bb2023-01-11 09:46:29 +0800271} wifi_secur_list;
272
developer0155a502023-06-19 20:33:57 +0800273typedef struct GNU_PACKED _wdev_extended_ap_metrics {
274 unsigned int uc_tx;
275 unsigned int uc_rx;
276 unsigned int mc_tx;
277 unsigned int mc_rx;
278 unsigned int bc_tx;
279 unsigned int bc_rx;
280} wdev_extended_ap_metric;
281
282typedef struct GNU_PACKED _wdev_ap_metric {
283 unsigned char bssid[6];
284 unsigned char cu;
285 unsigned char ESPI_AC[4][3];
286 wdev_extended_ap_metric ext_ap_metric;
287} wdev_ap_metric;
288
289
developer72fb0bb2023-01-11 09:46:29 +0800290static int util_unii_5g_centerfreq(const char *ht_mode, int channel);
291static int util_unii_6g_centerfreq(const char *ht_mode, int channel);
developera3511852023-06-14 14:12:59 +0800292wifi_secur_list * wifi_get_item_by_key(wifi_secur_list *list, int list_sz, int key);
293wifi_secur_list * wifi_get_item_by_str(wifi_secur_list *list, int list_sz, const char *str);
294char * wifi_get_str_by_key(wifi_secur_list *list, int list_sz, int key);
developer72fb0bb2023-01-11 09:46:29 +0800295static int ieee80211_channel_to_frequency(int channel, int *freqMHz);
developerd14dff12023-06-28 22:47:44 +0800296static void wifi_PrepareDefaultHostapdConfigs(bool reset);
developer47cc27a2023-05-17 23:09:58 +0800297static void wifi_psk_file_reset();
developerb2977562023-05-24 17:54:12 +0800298static void wifi_dat_file_reset_by_radio(char radio_idx);
developer262f4cb2023-05-24 12:22:04 +0800299static int util_get_sec_chan_offset(int channel, const char* ht_mode);
developer2f79c922023-06-02 17:33:42 +0800300int hostapd_raw_add_bss(int apIndex);
301int hostapd_raw_remove_bss(int apIndex);
developerd14dff12023-06-28 22:47:44 +0800302INT wifi_getApDevicesAssociated(INT apIndex, CHAR *macArray, UINT buf_size);
303
developer86035662023-06-28 19:21:12 +0800304static inline int hal_strtol(char *src, int base, long int *out)
305{
306 long int res = 0;
307 char *end_ptr = NULL;
308
309 errno = 0;
310 res = strtol(src, &end_ptr, base);
311
312 if ((errno == ERANGE && (res == LONG_MIN || res == LONG_MAX))
developer5fbf2ff2023-06-30 10:51:56 +0800313 || (errno != 0 && res == 0) || /*ignore end_ptr!=0 error*/ /**end_ptr != '\0' ||*/src == end_ptr ) {
314 *out = res;
developer86035662023-06-28 19:21:12 +0800315 return -1;
developer5fbf2ff2023-06-30 10:51:56 +0800316 } else
developer86035662023-06-28 19:21:12 +0800317 *out = res;
318
319 return 0;
320}
321
322static inline int hal_strtoul(char *src, int base, unsigned long *out)
323{
324 unsigned long res = 0;
325 char *end_ptr = NULL;
326
327 errno = 0;
328 res = strtoul(src, &end_ptr, base);
329
330 if ((errno == ERANGE && res == ULONG_MAX)
developer5fbf2ff2023-06-30 10:51:56 +0800331 || (errno != 0 && res == 0) || /*ignore end_ptr!=0 error*/ /**end_ptr != '\0' ||*/src == end_ptr ) {
332 *out = res;
developer86035662023-06-28 19:21:12 +0800333 return -1;
developer5fbf2ff2023-06-30 10:51:56 +0800334 } else
developer86035662023-06-28 19:21:12 +0800335 *out = res;
336
337 return 0;
338}
developer47cc27a2023-05-17 23:09:58 +0800339
developercc5cbfb2023-06-13 18:29:52 +0800340static inline int os_snprintf_error(size_t size, int res)
341{
342 return res < 0 || (unsigned int) res >= size;
343}
344
developer49b17232023-05-19 16:35:19 +0800345/*type define the nl80211 call back func*/
346typedef int (*mtk_nl80211_cb) (struct nl_msg *, void *);
347
348/**
349*struct mtk_nl80211_param
350* init mtk nl80211 using parameters
351* @sub_cmd: the cmd define in the mtk_vendor_nl80211.h.
352* @if_type: now only support the NL80211_ATTR_IFINDEX/NL80211_ATTR_WIPHY.
353* @if_idx: the index should match the interface or wiphy.
354* Note: NA
355**/
356struct mtk_nl80211_param {
357 unsigned int sub_cmd;
358 int if_type;
359 int if_idx;
360};
361
362/**
developer121a8e72023-05-22 09:19:39 +0800363*struct mtk_nl80211_cb_data
364* init mtk nl80211 call back parameters
365* @out_buf: store the mtk vendor output msg for wifi hal buffer.
366* @out_len: the output buffer length.
367* Note: NA
368**/
369struct mtk_nl80211_cb_data {
370 char * out_buf;
371 unsigned int out_len;
372};
373
374/**
developer49b17232023-05-19 16:35:19 +0800375*mtk_nl80211_init
376* init mtk nl80211 netlink and init the vendor msg common part.
377* @nl: netlink, just init it.
378* @msg: netlink message will alloc it.
379* the msg send success/fails is not free by app
380* only the nla_put etc api fails should use nlmsg_free.
381* @msg_data: vendor data msg attr pointer.
382* @param: init using interface and sub_cmd parameter.
383*
384*init the netlink context and mtk netlink vendor msg.
385*
386*return:
387* 0: success
388* other: fail
389**/
390
391int mtk_nl80211_init(struct unl *nl, struct nl_msg **msg,
392 struct nlattr **msg_data, struct mtk_nl80211_param *param) {
393 /*sanity check here*/
394 if (!nl || !param) {
395 (void)fprintf(stderr,
developerdaf24792023-06-06 11:40:04 +0800396 "[%s][%d]:nl(%p) or param(%p) is null, error!\n",
developer49b17232023-05-19 16:35:19 +0800397 __func__, __LINE__, nl, param);
398 return -1;
399 }
400 /*if_type check*/
401 if ( param->if_type != NL80211_ATTR_IFINDEX && param->if_type != NL80211_ATTR_WIPHY) {
402 (void)fprintf(stderr,
403 "[%s][%d]:if_type(0x%x) is not supported, only 0x%x and 0x%x supported.\n",
404 __func__, __LINE__, param->if_type, NL80211_ATTR_IFINDEX, NL80211_ATTR_WIPHY);
405 return -1;
406 }
407 /*init the nl*/
408 if (unl_genl_init(nl, "nl80211") < 0) {
409 (void)fprintf(stderr, "[%s][%d]::Failed to connect to nl80211\n",
410 __func__, __LINE__);
411 return -1;
412 }
413 /*init the msg*/
414 *msg = unl_genl_msg(nl, NL80211_CMD_VENDOR, false);
415
416 if (nla_put_u32(*msg, param->if_type, param->if_idx) ||
developera3511852023-06-14 14:12:59 +0800417 nla_put_u32(*msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) ||
418 nla_put_u32(*msg, NL80211_ATTR_VENDOR_SUBCMD, param->sub_cmd)) {
developer49b17232023-05-19 16:35:19 +0800419 (void)fprintf(stderr,
420 "[%s][%d]:Nla put error: if_type: 0x%x, if_idx: 0x%x, sub_cmd: 0x%x\n",
421 __func__, __LINE__, param->if_type, param->if_idx, param->sub_cmd);
422 goto err;
423 }
424
425 *msg_data = nla_nest_start(*msg, NL80211_ATTR_VENDOR_DATA);
426 if (!*msg_data) {
427 (void)fprintf(stderr, "[%s][%d]:Nla put NL80211_ATTR_VENDOR_DATA start error\n",
428 __func__, __LINE__);
429 goto err;
430 }
431
432 return 0;
433err:
developer49b17232023-05-19 16:35:19 +0800434 nlmsg_free(*msg);
435 unl_free(nl);
436 return -1;
437}
438
439/**
440*mtk_nl80211_send
441* set the vendor cmd call back and sent the vendor msg.
442* @nl: netlink.
443* @msg: netlink message.
444* @msg_data: vendor data msg attr pointer.
445* @handler: if the msg have call back shoud add the call back func
446* the event msg will handle by the call back func(exp:get cmd)
447* other set it as NULL(exp:set cmd).
448* @arg:call back func arg parameter.
449*add end of the netlink msg, set the call back and send msg
450*
451*return:
452* 0: success
453* other: fail
454**/
455int mtk_nl80211_send(struct unl *nl, struct nl_msg *msg,
456 struct nlattr *msg_data, mtk_nl80211_cb handler, void *arg) {
457 int ret = 0;
458 /*sanity check*/
459 if (!nl || !msg || !msg_data) {
460 (void)fprintf(stderr,
developerdaf24792023-06-06 11:40:04 +0800461 "[%s][%d]:nl(%p),msg(%p) or msg_data(%p) is null, error!\n",
developer49b17232023-05-19 16:35:19 +0800462 __func__, __LINE__, nl, msg, msg_data);
463 return -1;
464 }
465 /*end the msg attr of vendor data*/
466 nla_nest_end(msg, msg_data);
467 /*send the msg and set call back */
468 ret = unl_genl_request(nl, msg, handler, arg);
469 if (ret)
470 (void)fprintf(stderr, "send nl80211 cmd fails\n");
471 return ret;
472}
473
474/**
475*mtk_nl80211_deint
developer2edaf012023-05-24 14:24:53 +0800476* deinit the netlink.
developer49b17232023-05-19 16:35:19 +0800477* @nl: netlink.
478*
developer2edaf012023-05-24 14:24:53 +0800479*free deinit the netlink.
developer49b17232023-05-19 16:35:19 +0800480*
481*return:
482* 0: success
483**/
484
485int mtk_nl80211_deint(struct unl *nl) {
486 unl_free(nl);
487 return 0;
488}
489
developer72fb0bb2023-01-11 09:46:29 +0800490wifi_secur_list * wifi_get_item_by_key(wifi_secur_list *list, int list_sz, int key)
491{
developera3511852023-06-14 14:12:59 +0800492 wifi_secur_list *item;
493 int i;
developer72fb0bb2023-01-11 09:46:29 +0800494
developera3511852023-06-14 14:12:59 +0800495 for (item = list,i = 0;i < list_sz; item++, i++) {
496 if ((int)(item->key) == key) {
497 return item;
498 }
499 }
developer72fb0bb2023-01-11 09:46:29 +0800500
developera3511852023-06-14 14:12:59 +0800501 return NULL;
developer72fb0bb2023-01-11 09:46:29 +0800502}
503
504char * wifi_get_str_by_key(wifi_secur_list *list, int list_sz, int key)
505{
developera3511852023-06-14 14:12:59 +0800506 wifi_secur_list *item = wifi_get_item_by_key(list, list_sz, key);
developer72fb0bb2023-01-11 09:46:29 +0800507
developera3511852023-06-14 14:12:59 +0800508 if (!item) {
509 return "";
510 }
developer72fb0bb2023-01-11 09:46:29 +0800511
developera3511852023-06-14 14:12:59 +0800512 return (char *)(item->data);
developer72fb0bb2023-01-11 09:46:29 +0800513}
514
515wifi_secur_list * wifi_get_item_by_str(wifi_secur_list *list, int list_sz, const char *str)
516{
developera3511852023-06-14 14:12:59 +0800517 wifi_secur_list *item;
518 int i;
developer72fb0bb2023-01-11 09:46:29 +0800519
developera3511852023-06-14 14:12:59 +0800520 for (item = list,i = 0;i < list_sz; item++, i++) {
521 if (strcmp((char *)(item->data), str) == 0) {
522 return item;
523 }
524 }
developer72fb0bb2023-01-11 09:46:29 +0800525
developera3511852023-06-14 14:12:59 +0800526 return NULL;
developer72fb0bb2023-01-11 09:46:29 +0800527}
528#endif /* WIFI_HAL_VERSION_3 */
529
developer96b38512023-02-22 11:17:45 +0800530
531static char l1profile[32] = "/etc/wireless/l1profile.dat";
developer17038e62023-03-02 14:43:43 +0800532char main_prefix[MAX_NUM_RADIOS][IFNAMSIZ];
533char ext_prefix[MAX_NUM_RADIOS][IFNAMSIZ];
534#define MAX_SSID_LEN 64
535char default_ssid[MAX_NUM_RADIOS][MAX_SSID_LEN];;
developer745f0bd2023-03-06 14:32:53 +0800536int radio_band[MAX_NUM_RADIOS];
developer17038e62023-03-02 14:43:43 +0800537
538static int array_index_to_vap_index(UINT radioIndex, int arrayIndex);
539static int vap_index_to_array_index(int vapIndex, int *radioIndex, int *arrayIndex);
540
developer96b38512023-02-22 11:17:45 +0800541
542static int
543get_value(const char *conf_file, const char *param, char *value, int len)
544{
developera3511852023-06-14 14:12:59 +0800545 FILE *fp;
546 int ret = -1;
547 int param_len = strlen(param);
548 int buf_len;
developer86035662023-06-28 19:21:12 +0800549 char buf[256] = {0};
developer96b38512023-02-22 11:17:45 +0800550
developera3511852023-06-14 14:12:59 +0800551 fp = fopen(conf_file, "r");
552 if (!fp) {
553 return -1;
554 }
developer96b38512023-02-22 11:17:45 +0800555
developera3511852023-06-14 14:12:59 +0800556 while (fgets(buf, sizeof(buf), fp)) {
557 buf_len = strlen(buf);
developer86035662023-06-28 19:21:12 +0800558 if (buf_len == 0) {
559 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +0800560 if (fclose(fp) != 0) {
561 wifi_debug(DEBUG_ERROR, "fclose fail\n");
562 }
developer86035662023-06-28 19:21:12 +0800563 return RETURN_ERR;
564 }
developera3511852023-06-14 14:12:59 +0800565 if (buf[buf_len - 1] == '\n') {
566 buf_len--;
567 buf[buf_len] = '\0';
568 }
569 if ((buf_len > param_len) &&
570 (strncmp(buf, param, param_len) == 0) &&
571 (buf[param_len] == '=')) {
developer96b38512023-02-22 11:17:45 +0800572
developera3511852023-06-14 14:12:59 +0800573 if (buf_len == (param_len + 1)) {
574 value[0] = '\0';
575 ret = 0;
576 } else {
577 ret = snprintf(value, len, "%s", buf + (param_len + 1));
developer75bd10c2023-06-27 11:34:08 +0800578 if (os_snprintf_error(len, ret)) {
579 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
580 }
developera3511852023-06-14 14:12:59 +0800581 }
developerd14dff12023-06-28 22:47:44 +0800582 if (fclose(fp) != 0) {
583 ret = -1;
584 wifi_debug(DEBUG_ERROR, "fclose fail\n");
585 }
developera3511852023-06-14 14:12:59 +0800586 return ret;
587 }
588 }
developer37646972023-06-29 10:58:43 +0800589 if (fclose(fp) == EOF){
590 wifi_debug(DEBUG_ERROR, "fclose fail\n");
591 return RETURN_ERR;
592 }
developera3511852023-06-14 14:12:59 +0800593 return -1;
developer96b38512023-02-22 11:17:45 +0800594}
595
596static int
597get_value_by_idx(const char *conf_file, const char *param, int idx, char *value, int len)
598{
developera3511852023-06-14 14:12:59 +0800599 char buf[256];
600 int ret;
601 char *save_ptr = NULL;
602 char *tok = NULL;
developer96b38512023-02-22 11:17:45 +0800603
developera3511852023-06-14 14:12:59 +0800604 ret = get_value(conf_file, param, buf, sizeof(buf));
605 if (ret < 0)
606 return ret;
developer96b38512023-02-22 11:17:45 +0800607
developera3511852023-06-14 14:12:59 +0800608 tok = strtok_r(buf, ";", &save_ptr);
609 do {
610 if (idx == 0 || tok == NULL)
611 break;
612 else
613 idx--;
developer96b38512023-02-22 11:17:45 +0800614
developera3511852023-06-14 14:12:59 +0800615 tok = strtok_r(NULL, ";", &save_ptr);
616 } while (tok != NULL);
developer96b38512023-02-22 11:17:45 +0800617
developera3511852023-06-14 14:12:59 +0800618 if (tok) {
619 ret = snprintf(value, len, "%s", tok);
developer75bd10c2023-06-27 11:34:08 +0800620 if (os_snprintf_error(len, ret)) {
621 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
622 return -1;
623 }
developera3511852023-06-14 14:12:59 +0800624 } else {
625 ret = 0;
626 value[0] = '\0';
627 }
developer96b38512023-02-22 11:17:45 +0800628
developera3511852023-06-14 14:12:59 +0800629 return ret;
developer96b38512023-02-22 11:17:45 +0800630}
631
632
developer72fb0bb2023-01-11 09:46:29 +0800633#ifdef HAL_NETLINK_IMPL
634typedef struct {
developera3511852023-06-14 14:12:59 +0800635 int id;
636 struct nl_sock* socket;
637 struct nl_cb* cb;
developer72fb0bb2023-01-11 09:46:29 +0800638} Netlink;
639
640static int mac_addr_aton(unsigned char *mac_addr, char *arg)
641{
developera3511852023-06-14 14:12:59 +0800642 unsigned char mac_addr_int[6]={};
developer75bd10c2023-06-27 11:34:08 +0800643 unsigned int recv;
644
645 recv = sscanf(arg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac_addr_int+0, mac_addr_int+1, mac_addr_int+2, mac_addr_int+3, mac_addr_int+4, mac_addr_int+5);
646
647 if (recv != 6) {
648 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
649 return -1;
650 }
developera3511852023-06-14 14:12:59 +0800651 mac_addr[0] = mac_addr_int[0];
652 mac_addr[1] = mac_addr_int[1];
653 mac_addr[2] = mac_addr_int[2];
654 mac_addr[3] = mac_addr_int[3];
655 mac_addr[4] = mac_addr_int[4];
656 mac_addr[5] = mac_addr_int[5];
657 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800658}
659
660static void mac_addr_ntoa(char *mac_addr, unsigned char *arg)
661{
developera3511852023-06-14 14:12:59 +0800662 unsigned int mac_addr_int[6]={};
developere40952c2023-06-15 18:46:43 +0800663 int res;
664
developera3511852023-06-14 14:12:59 +0800665 mac_addr_int[0] = arg[0];
666 mac_addr_int[1] = arg[1];
667 mac_addr_int[2] = arg[2];
668 mac_addr_int[3] = arg[3];
669 mac_addr_int[4] = arg[4];
670 mac_addr_int[5] = arg[5];
developere40952c2023-06-15 18:46:43 +0800671 res = snprintf(mac_addr, 20, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac_addr_int[0], mac_addr_int[1],mac_addr_int[2],mac_addr_int[3],mac_addr_int[4],mac_addr_int[5]);
672 if (os_snprintf_error(20, res)) {
673 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
674 }
developera3511852023-06-14 14:12:59 +0800675 return;
developer72fb0bb2023-01-11 09:46:29 +0800676}
677
678static int ieee80211_frequency_to_channel(int freq)
679{
developera3511852023-06-14 14:12:59 +0800680 /* see 802.11-2007 17.3.8.3.2 and Annex J */
681 if (freq == 2484)
682 return 14;
683 /* see 802.11ax D6.1 27.3.23.2 and Annex E */
684 else if (freq == 5935)
685 return 2;
686 else if (freq < 2484)
687 return (freq - 2407) / 5;
688 else if (freq >= 4910 && freq <= 4980)
689 return (freq - 4000) / 5;
690 else if (freq < 5950)
691 return (freq - 5000) / 5;
692 else if (freq <= 45000) /* DMG band lower limit */
693 /* see 802.11ax D6.1 27.3.23.2 */
694 return (freq - 5950) / 5;
695 else if (freq >= 58320 && freq <= 70200)
696 return (freq - 56160) / 2160;
697 else
698 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800699}
700
701static int initSock80211(Netlink* nl) {
developera3511852023-06-14 14:12:59 +0800702 nl->socket = nl_socket_alloc();
703 if (!nl->socket) {
developer75bd10c2023-06-27 11:34:08 +0800704 wifi_debug(DEBUG_ERROR, "Failing to allocate the sock\n");
developera3511852023-06-14 14:12:59 +0800705 return -ENOMEM;
706 }
developer72fb0bb2023-01-11 09:46:29 +0800707
developera3511852023-06-14 14:12:59 +0800708 nl_socket_set_buffer_size(nl->socket, 8192, 8192);
developer72fb0bb2023-01-11 09:46:29 +0800709
developera3511852023-06-14 14:12:59 +0800710 if (genl_connect(nl->socket)) {
developer75bd10c2023-06-27 11:34:08 +0800711 wifi_debug(DEBUG_ERROR, "Failed to connect\n");
developera3511852023-06-14 14:12:59 +0800712 nl_close(nl->socket);
713 nl_socket_free(nl->socket);
714 return -ENOLINK;
715 }
developer72fb0bb2023-01-11 09:46:29 +0800716
developera3511852023-06-14 14:12:59 +0800717 nl->id = genl_ctrl_resolve(nl->socket, "nl80211");
718 if (nl->id< 0) {
developer75bd10c2023-06-27 11:34:08 +0800719 wifi_debug(DEBUG_ERROR, "interface not found.\n");
developera3511852023-06-14 14:12:59 +0800720 nl_close(nl->socket);
721 nl_socket_free(nl->socket);
722 return -ENOENT;
723 }
developer72fb0bb2023-01-11 09:46:29 +0800724
developera3511852023-06-14 14:12:59 +0800725 nl->cb = nl_cb_alloc(NL_CB_DEFAULT);
726 if ((!nl->cb)) {
developer75bd10c2023-06-27 11:34:08 +0800727 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink callback.\n");
developera3511852023-06-14 14:12:59 +0800728 nl_close(nl->socket);
729 nl_socket_free(nl->socket);
730 return ENOMEM;
731 }
developer72fb0bb2023-01-11 09:46:29 +0800732
developera3511852023-06-14 14:12:59 +0800733 return nl->id;
developer72fb0bb2023-01-11 09:46:29 +0800734}
735
736static int nlfree(Netlink *nl)
737{
developera3511852023-06-14 14:12:59 +0800738 nl_cb_put(nl->cb);
739 nl_close(nl->socket);
740 nl_socket_free(nl->socket);
741 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800742}
743
744static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
developera3511852023-06-14 14:12:59 +0800745 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
746 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
747 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED }
developer72fb0bb2023-01-11 09:46:29 +0800748};
749
750static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
751};
752
753static struct nla_policy tid_policy[NL80211_TID_STATS_MAX + 1] = {
754};
755
756typedef struct _wifi_channelStats_loc {
developera3511852023-06-14 14:12:59 +0800757 INT array_size;
758 INT ch_number;
759 BOOL ch_in_pool;
760 INT ch_noise;
761 BOOL ch_radar_noise;
762 INT ch_max_80211_rssi;
763 INT ch_non_80211_noise;
764 INT ch_utilization;
765 ULLONG ch_utilization_total;
766 ULLONG ch_utilization_busy;
767 ULLONG ch_utilization_busy_tx;
768 ULLONG ch_utilization_busy_rx;
769 ULLONG ch_utilization_busy_self;
770 ULLONG ch_utilization_busy_ext;
developer72fb0bb2023-01-11 09:46:29 +0800771} wifi_channelStats_t_loc;
772
773typedef struct wifi_device_info {
developera3511852023-06-14 14:12:59 +0800774 INT wifi_devIndex;
775 UCHAR wifi_devMacAddress[6];
776 CHAR wifi_devIPAddress[64];
777 BOOL wifi_devAssociatedDeviceAuthentiationState;
778 INT wifi_devSignalStrength;
779 INT wifi_devTxRate;
780 INT wifi_devRxRate;
developer72fb0bb2023-01-11 09:46:29 +0800781} wifi_device_info_t;
782
783#endif
784
785//For 5g Alias Interfaces
developer72fb0bb2023-01-11 09:46:29 +0800786static BOOL Radio_flag = TRUE;
787//wifi_setApBeaconRate(1, beaconRate);
788
789BOOL multiple_set = FALSE;
790
791struct params
792{
developera3511852023-06-14 14:12:59 +0800793 char * name;
794 char * value;
developer72fb0bb2023-01-11 09:46:29 +0800795};
796
developer33f13ba2023-07-12 16:19:06 +0800797#define _syscmd_secure(retBuf, retBufSize, fmt, args...) \
798 ({ \
799 FILE *f; \
800 char *ptr = retBuf; \
801 int bufSize = retBufSize, bufbytes = 0, readbytes = 0, cmd_ret = -1; \
802 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__); \
803 f = v_secure_popen("r", fmt, ##args); \
804 if(f) { \
805 while(!feof(f)) \
806 { \
807 *ptr = 0; \
808 if(bufSize>=128) { \
809 bufbytes=128; \
810 } else { \
811 bufbytes=bufSize-1; \
812 } \
813 if (fgets(ptr,bufbytes,f) == NULL) \
814 break; \
815 readbytes=strlen(ptr); \
816 if(!readbytes) \
817 break; \
818 bufSize-=readbytes; \
819 ptr += readbytes; \
820 } \
821 cmd_ret = v_secure_pclose(f); \
822 retBuf[retBufSize-1]=0; \
823 } \
824 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__); \
825 cmd_ret; \
826 })
827
828
developer72fb0bb2023-01-11 09:46:29 +0800829static int _syscmd(char *cmd, char *retBuf, int retBufSize)
830{
developera3511852023-06-14 14:12:59 +0800831 FILE *f;
832 char *ptr = retBuf;
833 int bufSize=retBufSize, bufbytes=0, readbytes=0, cmd_ret=0;
developer72fb0bb2023-01-11 09:46:29 +0800834
developera3511852023-06-14 14:12:59 +0800835 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
836 if((f = popen(cmd, "r")) == NULL) {
developer75bd10c2023-06-27 11:34:08 +0800837 wifi_debug(DEBUG_ERROR, "\npopen %s error\n", cmd);
developera3511852023-06-14 14:12:59 +0800838 return RETURN_ERR;
839 }
developer72fb0bb2023-01-11 09:46:29 +0800840
developera3511852023-06-14 14:12:59 +0800841 while(!feof(f))
842 {
843 *ptr = 0;
844 if(bufSize>=128) {
845 bufbytes=128;
846 } else {
847 bufbytes=bufSize-1;
848 }
developer72fb0bb2023-01-11 09:46:29 +0800849
developerd14dff12023-06-28 22:47:44 +0800850 if (fgets(ptr,bufbytes,f) == NULL)
851 break;
developera3511852023-06-14 14:12:59 +0800852 readbytes=strlen(ptr);
developer72fb0bb2023-01-11 09:46:29 +0800853
developera3511852023-06-14 14:12:59 +0800854 if(!readbytes)
855 break;
developer72fb0bb2023-01-11 09:46:29 +0800856
developera3511852023-06-14 14:12:59 +0800857 bufSize-=readbytes;
858 ptr += readbytes;
859 }
860 cmd_ret = pclose(f);
861 retBuf[retBufSize-1]=0;
862 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800863
developera3511852023-06-14 14:12:59 +0800864 return cmd_ret >> 8;
developer72fb0bb2023-01-11 09:46:29 +0800865}
866
867INT radio_index_to_phy(int radioIndex)
868{
developera3511852023-06-14 14:12:59 +0800869 /* TODO */
870 return radioIndex;
developer72fb0bb2023-01-11 09:46:29 +0800871}
872
873INT wifi_getMaxRadioNumber(INT *max_radio_num)
874{
developera3511852023-06-14 14:12:59 +0800875 char buf[4] = {0};
developere40952c2023-06-15 18:46:43 +0800876 int res;
developerc14d83a2023-06-29 20:09:42 +0800877 unsigned long tmp;
developera3511852023-06-14 14:12:59 +0800878 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800879
developer33f13ba2023-07-12 16:19:06 +0800880 res = _syscmd_secure(buf, sizeof(buf), "iw list | grep Wiphy | wc -l | tr -d '\\n'");
881 if (res) {
882 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +0800883 }
developerc14d83a2023-06-29 20:09:42 +0800884 if (hal_strtoul(buf, 10, &tmp) < 0) {
885 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +0800886 }
developerc14d83a2023-06-29 20:09:42 +0800887 res = tmp;
developerd14dff12023-06-28 22:47:44 +0800888
889 *max_radio_num = res > MAX_NUM_RADIOS ? MAX_NUM_RADIOS:res;
developer72fb0bb2023-01-11 09:46:29 +0800890
developera3511852023-06-14 14:12:59 +0800891 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800892
developera3511852023-06-14 14:12:59 +0800893 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +0800894}
895
developer17038e62023-03-02 14:43:43 +0800896wifi_band radio_index_to_band(int radioIndex)
897{
developera3511852023-06-14 14:12:59 +0800898 return radio_band[radioIndex];
developer17038e62023-03-02 14:43:43 +0800899}
900
developer72fb0bb2023-01-11 09:46:29 +0800901wifi_band wifi_index_to_band(int apIndex)
902{
developera3511852023-06-14 14:12:59 +0800903 char buf[64] = {0};
developerc14d83a2023-06-29 20:09:42 +0800904 long int nl80211_band = 0;
developera3511852023-06-14 14:12:59 +0800905 int i = 0;
906 int phyIndex = 0;
907 int radioIndex = 0;
908 int max_radio_num = 0;
909 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +0800910 int res;
developera3511852023-06-14 14:12:59 +0800911 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800912
developera3511852023-06-14 14:12:59 +0800913 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +0800914 if(max_radio_num == 0){
915 return RETURN_ERR;
916 }
developera3511852023-06-14 14:12:59 +0800917 radioIndex = apIndex % max_radio_num;
918 phyIndex = radio_index_to_phy(radioIndex);
developer33f13ba2023-07-12 16:19:06 +0800919 while (i < 10) {
920 res = _syscmd_secure(buf, sizeof(buf),
921 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", phyIndex);
922
923 if (res) {
924 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +0800925 }
developer33f13ba2023-07-12 16:19:06 +0800926
developerc14d83a2023-06-29 20:09:42 +0800927 if (hal_strtol(buf, 16, &nl80211_band) < 0) {
928 wifi_debug(DEBUG_ERROR, "strtol fail\n");
929 }
developera3511852023-06-14 14:12:59 +0800930 if (nl80211_band == 1)
931 band = band_2_4;
932 else if (nl80211_band == 2)
933 band = band_5;
934 else if (nl80211_band == 4) // band == 3 is 60GHz
935 band = band_6;
developer72fb0bb2023-01-11 09:46:29 +0800936
developera3511852023-06-14 14:12:59 +0800937 if(band != band_invalid)
938 break;
developer69b61b02023-03-07 17:17:44 +0800939
developera3511852023-06-14 14:12:59 +0800940 i++;
941 sleep(1);
942 }
developer72fb0bb2023-01-11 09:46:29 +0800943
developera3511852023-06-14 14:12:59 +0800944 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
945 return band;
developer72fb0bb2023-01-11 09:46:29 +0800946}
947
948static int wifi_hostapdRead(char *conf_file, char *param, char *output, int output_size)
949{
developer7e4a2a62023-04-06 19:56:03 +0800950 char cmd[MAX_CMD_SIZE] = {0};
951 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +0800952 int res = 0;
developer72fb0bb2023-01-11 09:46:29 +0800953
developere40952c2023-06-15 18:46:43 +0800954 res = snprintf(cmd, MAX_CMD_SIZE, "cat %s 2> /dev/null | grep \"^%s=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developer7e4a2a62023-04-06 19:56:03 +0800955 conf_file, param);
developer72fb0bb2023-01-11 09:46:29 +0800956
developere40952c2023-06-15 18:46:43 +0800957 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
958 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
959 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +0800960 }
developerb758dfd2023-06-21 17:32:07 +0800961
developere40952c2023-06-15 18:46:43 +0800962 res = _syscmd(cmd, buf, sizeof(buf));
963 if ((res != 0) && (strlen(buf) == 0)) {
developer7e4a2a62023-04-06 19:56:03 +0800964 printf("%s: _syscmd error!", __func__);
965 return -1;
966 }
967
developere40952c2023-06-15 18:46:43 +0800968 res = snprintf(output, output_size, "%s", buf);
969 if (os_snprintf_error(output_size, res)) {
970 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
971 return RETURN_ERR;
972 }
developer7e4a2a62023-04-06 19:56:03 +0800973
974 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800975}
976
977static int wifi_hostapdWrite(char *conf_file, struct params *list, int item_count)
978{
developera3511852023-06-14 14:12:59 +0800979 char cmd[MAX_CMD_SIZE] = {0};
980 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +0800981 int res;
developer72fb0bb2023-01-11 09:46:29 +0800982
developera3511852023-06-14 14:12:59 +0800983 for (int i = 0; i < item_count; i++) {
984 wifi_hostapdRead(conf_file, list[i].name, buf, sizeof(buf));
985 if (strlen(buf) == 0) /*no such item, insert it*/
developere40952c2023-06-15 18:46:43 +0800986 res = snprintf(cmd, sizeof(cmd), "sed -i -e '$a %s=%s' %s", list[i].name, list[i].value, conf_file);
developera3511852023-06-14 14:12:59 +0800987 else /*find the item, update it*/
developere40952c2023-06-15 18:46:43 +0800988 res = snprintf(cmd, sizeof(cmd), "sed -i \"s/^%s=.*/%s=%s/\" %s", list[i].name, list[i].name, list[i].value, conf_file);
developera1255e42023-05-13 17:45:02 +0800989
developere40952c2023-06-15 18:46:43 +0800990 if (os_snprintf_error(sizeof(cmd), res)) {
991 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
992 return RETURN_ERR;
993 }
developera3511852023-06-14 14:12:59 +0800994 if(_syscmd(cmd, buf, sizeof(buf)))
995 return -1;
996 }
developer72fb0bb2023-01-11 09:46:29 +0800997
developera3511852023-06-14 14:12:59 +0800998 return 0;
developera1255e42023-05-13 17:45:02 +0800999}
developerfde01262023-05-22 15:15:24 +08001000
developera1255e42023-05-13 17:45:02 +08001001static int wifi_datfileRead(char *conf_file, char *param, char *output, int output_size)
1002{
developera3511852023-06-14 14:12:59 +08001003 char cmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001004 int res = 0;
developera3511852023-06-14 14:12:59 +08001005 int len;
developerfde01262023-05-22 15:15:24 +08001006
developere40952c2023-06-15 18:46:43 +08001007 res = snprintf(cmd, sizeof(cmd), "datconf -f %s get %s", conf_file, param);
1008 if (os_snprintf_error(sizeof(cmd), res)) {
1009 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1010 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08001011 }
developerfde01262023-05-22 15:15:24 +08001012
developere40952c2023-06-15 18:46:43 +08001013
1014 res = _syscmd(cmd, output, output_size);
1015 if ((res != 0) && (strlen(output) == 0)) {
developera3511852023-06-14 14:12:59 +08001016 printf("%s: _syscmd error!", __func__);
1017 return -1;
1018 }
developera1255e42023-05-13 17:45:02 +08001019
developera3511852023-06-14 14:12:59 +08001020 len = strlen(output);
1021 if ((len > 0) && (output[len - 1] == '\n')) {
1022 output[len - 1] = '\0';
1023 }
developerfde01262023-05-22 15:15:24 +08001024
developera3511852023-06-14 14:12:59 +08001025 return 0;
developerfde01262023-05-22 15:15:24 +08001026}
developera1255e42023-05-13 17:45:02 +08001027
developera1255e42023-05-13 17:45:02 +08001028static int wifi_datfileWrite(char *conf_file, struct params *list, int item_count)
1029{
developere40952c2023-06-15 18:46:43 +08001030 int res;
developera3511852023-06-14 14:12:59 +08001031 char cmd[MAX_CMD_SIZE] = {0};
1032 char buf[MAX_BUF_SIZE] = {0};
developera1255e42023-05-13 17:45:02 +08001033
developera3511852023-06-14 14:12:59 +08001034 for (int i = 0; i < item_count; i++) {
developere40952c2023-06-15 18:46:43 +08001035 res = snprintf(cmd, sizeof(cmd), "datconf -f %s set %s \"%s\"", conf_file, list[i].name, list[i].value);
1036 if (os_snprintf_error(sizeof(cmd), res)) {
1037 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1038 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08001039 }
developera1255e42023-05-13 17:45:02 +08001040
developera3511852023-06-14 14:12:59 +08001041 if(_syscmd(cmd, buf, sizeof(buf)))
1042 return -1;
1043 }
developera1255e42023-05-13 17:45:02 +08001044
developera3511852023-06-14 14:12:59 +08001045 return 0;
developera1255e42023-05-13 17:45:02 +08001046}
1047
developerfde01262023-05-22 15:15:24 +08001048static int wifi_l1ProfileRead(char *param, char *output, int output_size)
1049{
developera3511852023-06-14 14:12:59 +08001050 int ret;
developerfde01262023-05-22 15:15:24 +08001051
developera3511852023-06-14 14:12:59 +08001052 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1053 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001054 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001055 return RETURN_ERR;
1056 }
developerfde01262023-05-22 15:15:24 +08001057
developera3511852023-06-14 14:12:59 +08001058 ret = wifi_datfileRead(l1profile, param, output, output_size);
1059 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08001060 wifi_debug(DEBUG_ERROR, "wifi_datfileRead %s from %s failed, ret:%d", param, l1profile, ret);
developera3511852023-06-14 14:12:59 +08001061 return RETURN_ERR;
1062 }
developerfde01262023-05-22 15:15:24 +08001063
developera3511852023-06-14 14:12:59 +08001064 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1065 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001066}
1067
1068static int wifi_CardProfileRead(int card_idx, char *param, char *output, int output_size)
1069{
developera3511852023-06-14 14:12:59 +08001070 char option[64];
1071 char card_profile_path[64];
developere40952c2023-06-15 18:46:43 +08001072 int res;
developerfde01262023-05-22 15:15:24 +08001073
developera3511852023-06-14 14:12:59 +08001074 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developerfde01262023-05-22 15:15:24 +08001075
developera3511852023-06-14 14:12:59 +08001076 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001077 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001078 return RETURN_ERR;
1079 }
developerfde01262023-05-22 15:15:24 +08001080
developere40952c2023-06-15 18:46:43 +08001081 res = snprintf(option, sizeof(option), "INDEX%d_profile_path", card_idx);
1082 if (os_snprintf_error(sizeof(option), res)) {
1083 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developera3511852023-06-14 14:12:59 +08001084 return RETURN_ERR;
1085 }
developere40952c2023-06-15 18:46:43 +08001086 res = wifi_l1ProfileRead(option, card_profile_path, sizeof(card_profile_path));
1087 if (res != 0) {
developer75bd10c2023-06-27 11:34:08 +08001088 wifi_debug(DEBUG_ERROR, "wifi_l1ProfileRead %s failed, ret:%d", option, res);
developere40952c2023-06-15 18:46:43 +08001089 return RETURN_ERR;
1090 }
developerfde01262023-05-22 15:15:24 +08001091
developere40952c2023-06-15 18:46:43 +08001092 res = wifi_datfileRead(card_profile_path, param, output, output_size);
1093 if (res != 0) {
developer75bd10c2023-06-27 11:34:08 +08001094 wifi_debug(DEBUG_ERROR, "wifi_datfileRead %s from %s failed, ret:%d", param, card_profile_path, res);
developera3511852023-06-14 14:12:59 +08001095 return RETURN_ERR;
1096 }
developerfde01262023-05-22 15:15:24 +08001097
developera3511852023-06-14 14:12:59 +08001098 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1099 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001100}
1101
1102static int wifi_BandProfileRead(int card_idx,
developera3511852023-06-14 14:12:59 +08001103 int radio_idx,
1104 char *param,
1105 char *output,
1106 int output_size,
1107 char *default_value)
developerfde01262023-05-22 15:15:24 +08001108{
developera3511852023-06-14 14:12:59 +08001109 char option[64];
1110 char band_profile_path[64];
developere40952c2023-06-15 18:46:43 +08001111 int ret, res;
developerfde01262023-05-22 15:15:24 +08001112
developera3511852023-06-14 14:12:59 +08001113 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1114 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001115 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001116 return RETURN_ERR;
1117 }
developerfde01262023-05-22 15:15:24 +08001118
developere40952c2023-06-15 18:46:43 +08001119 res = snprintf(option, sizeof(option), "BN%d_profile_path", radio_idx);
1120 if (os_snprintf_error(sizeof(option), res)) {
1121 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1122 return RETURN_ERR;
1123 }
developera3511852023-06-14 14:12:59 +08001124 ret = wifi_CardProfileRead(card_idx, option, band_profile_path, sizeof(band_profile_path));
1125 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08001126 wifi_debug(DEBUG_ERROR, "wifi_CardProfileRead %s failed, ret:%d", option, ret);
developera3511852023-06-14 14:12:59 +08001127 return RETURN_ERR;
1128 }
developerfde01262023-05-22 15:15:24 +08001129
developera3511852023-06-14 14:12:59 +08001130 ret = wifi_datfileRead(band_profile_path, param, output, output_size);
1131 if (ret != 0) {
1132 if (default_value) {
developere40952c2023-06-15 18:46:43 +08001133 res = snprintf(output, output_size, "%s", default_value);
1134 if (os_snprintf_error(output_size, res)) {
1135 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1136 return RETURN_ERR;
1137 }
developera3511852023-06-14 14:12:59 +08001138 } else {
1139 output[0] = '\0';
1140 }
1141 }
developerfde01262023-05-22 15:15:24 +08001142
developera3511852023-06-14 14:12:59 +08001143 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1144 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001145}
1146
developer72fb0bb2023-01-11 09:46:29 +08001147//For Getting Current Interface Name from corresponding hostapd configuration
1148static int wifi_GetInterfaceName(int apIndex, char *interface_name)
1149{
developera3511852023-06-14 14:12:59 +08001150 char config_file[128] = {0};
developere40952c2023-06-15 18:46:43 +08001151 int res;
developer72fb0bb2023-01-11 09:46:29 +08001152
developera3511852023-06-14 14:12:59 +08001153 if (interface_name == NULL)
1154 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001155
developera3511852023-06-14 14:12:59 +08001156 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001157
developere40952c2023-06-15 18:46:43 +08001158 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
1159 if (os_snprintf_error(sizeof(config_file), res)) {
1160 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1161 return RETURN_ERR;
1162 }
developera3511852023-06-14 14:12:59 +08001163 wifi_hostapdRead(config_file, "interface", interface_name, 16);
1164 if (strlen(interface_name) == 0)
1165 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001166
developera3511852023-06-14 14:12:59 +08001167 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1168 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001169}
1170
developera1255e42023-05-13 17:45:02 +08001171static UCHAR get_bssnum_byindex(INT radio_index, UCHAR *bss_cnt)
1172{
developera3511852023-06-14 14:12:59 +08001173 char interface_name[IF_NAME_SIZE] = {0};
1174 char cmd[MAX_BUF_SIZE]={'\0'};
1175 char buf[MAX_CMD_SIZE]={'\0'};
1176 UCHAR channel = 0;
developere40952c2023-06-15 18:46:43 +08001177 int res;
developera1255e42023-05-13 17:45:02 +08001178
developera3511852023-06-14 14:12:59 +08001179 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
1180 return RETURN_ERR;
1181 /*interface name to channel number*/
developere40952c2023-06-15 18:46:43 +08001182 res = snprintf(cmd, sizeof(cmd), "iw dev %s info | grep -i 'channel' | cut -d ' ' -f2", interface_name);
1183 if (os_snprintf_error(sizeof(cmd), res)) {
1184 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1185 return RETURN_ERR;
1186 }
1187
developera3511852023-06-14 14:12:59 +08001188 _syscmd(cmd, buf, sizeof(buf));
1189 channel = atoi(buf);
1190 WIFI_ENTRY_EXIT_DEBUG("%s:channel=%d\n", __func__, channel);
developera1255e42023-05-13 17:45:02 +08001191 /*count dev number with the same channel*/
developere40952c2023-06-15 18:46:43 +08001192 res = snprintf(cmd, sizeof(cmd), "iw dev | grep -i 'channel %d' | wc -l", channel);
1193 if (os_snprintf_error(sizeof(cmd), res)) {
1194 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1195 return RETURN_ERR;
1196 }
1197
developera3511852023-06-14 14:12:59 +08001198 _syscmd(cmd, buf, sizeof(buf));
1199 *bss_cnt = atoi(buf) - 1;/*1 for apcli interface*/
1200 WIFI_ENTRY_EXIT_DEBUG("%s:bss_cnt=%d\n", __func__, *bss_cnt);
1201 return RETURN_OK;
developera1255e42023-05-13 17:45:02 +08001202}
developer72fb0bb2023-01-11 09:46:29 +08001203
1204static int wifi_hostapdProcessUpdate(int apIndex, struct params *list, int item_count)
1205{
developera3511852023-06-14 14:12:59 +08001206 char interface_name[16] = {0};
1207 if (multiple_set == TRUE)
1208 return RETURN_OK;
1209 char cmd[MAX_CMD_SIZE]="", output[32]="";
1210 FILE *fp;
developere40952c2023-06-15 18:46:43 +08001211 int i, res;
developera3511852023-06-14 14:12:59 +08001212 //NOTE RELOAD should be done in ApplySSIDSettings
1213 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1214 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08001215
1216 for (i=0; i<item_count; i++, list++) {
1217 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s SET %s %s", interface_name, list->name, list->value);
1218 if (os_snprintf_error(sizeof(cmd), res)) {
1219 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1220 return RETURN_ERR;
1221 }
1222 if ((fp = popen(cmd, "r"))==NULL) {
developera3511852023-06-14 14:12:59 +08001223 perror("popen failed");
1224 return -1;
1225 }
developere40952c2023-06-15 18:46:43 +08001226 if (!fgets(output, sizeof(output), fp) || strncmp(output, "OK", 2)) {
1227 pclose(fp);
developera3511852023-06-14 14:12:59 +08001228 perror("fgets failed");
1229 return -1;
1230 }
developere40952c2023-06-15 18:46:43 +08001231 pclose(fp);
developera3511852023-06-14 14:12:59 +08001232 }
1233 return 0;
developer72fb0bb2023-01-11 09:46:29 +08001234}
1235
developer7e4a2a62023-04-06 19:56:03 +08001236static int wifi_quick_reload_ap(int apIndex)
1237{
1238 char interface_name[IF_NAME_SIZE] = {0};
1239 char cmd[MAX_CMD_SIZE] = {0};
1240 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001241 int res;
developer7e4a2a62023-04-06 19:56:03 +08001242
1243 if (multiple_set == TRUE)
1244 return RETURN_OK;
1245
1246 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1247 return RETURN_ERR;
1248
developere40952c2023-06-15 18:46:43 +08001249 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s reload", interface_name);
1250 if (os_snprintf_error(sizeof(cmd), res)) {
1251 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1252 return RETURN_ERR;
1253 }
developer7e4a2a62023-04-06 19:56:03 +08001254 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1255 return RETURN_ERR;
1256
1257 return RETURN_OK;
1258}
1259
developer72fb0bb2023-01-11 09:46:29 +08001260static int wifi_reloadAp(int apIndex)
1261{
developera3511852023-06-14 14:12:59 +08001262 char interface_name[16] = {0};
developer22e0c672023-06-07 15:25:37 +08001263 int res;
1264
developera3511852023-06-14 14:12:59 +08001265 if (multiple_set == TRUE)
1266 return RETURN_OK;
1267 char cmd[MAX_CMD_SIZE]="";
1268 char buf[MAX_BUF_SIZE]="";
developer72fb0bb2023-01-11 09:46:29 +08001269
developera3511852023-06-14 14:12:59 +08001270 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1271 return RETURN_ERR;
1272 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s reload", interface_name);
developer22e0c672023-06-07 15:25:37 +08001273 if (os_snprintf_error(sizeof(cmd), res)) {
1274 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1275 return RETURN_ERR;
1276 }
developera3511852023-06-14 14:12:59 +08001277 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1278 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001279
developera3511852023-06-14 14:12:59 +08001280 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s disable", interface_name);
developer22e0c672023-06-07 15:25:37 +08001281 if (os_snprintf_error(sizeof(cmd), res)) {
1282 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1283 return RETURN_ERR;
1284 }
developera3511852023-06-14 14:12:59 +08001285 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1286 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001287
developera3511852023-06-14 14:12:59 +08001288 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s enable", interface_name);
developer22e0c672023-06-07 15:25:37 +08001289 if (os_snprintf_error(sizeof(cmd), res)) {
1290 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1291 return RETURN_ERR;
1292 }
developera3511852023-06-14 14:12:59 +08001293 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1294 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001295
developera3511852023-06-14 14:12:59 +08001296 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001297}
1298
1299INT File_Reading(CHAR *file, char *Value)
1300{
developera3511852023-06-14 14:12:59 +08001301 FILE *fp = NULL;
1302 char buf[MAX_CMD_SIZE] = {0}, copy_buf[MAX_CMD_SIZE] ={0};
1303 int count = 0;
developer72fb0bb2023-01-11 09:46:29 +08001304
developera3511852023-06-14 14:12:59 +08001305 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1306 fp = popen(file,"r");
1307 if(fp == NULL)
1308 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001309
developera3511852023-06-14 14:12:59 +08001310 if(fgets(buf,sizeof(buf) -1,fp) != NULL)
1311 {
1312 for(count=0;buf[count]!='\n';count++)
1313 copy_buf[count]=buf[count];
1314 copy_buf[count]='\0';
1315 }
developerc14d83a2023-06-29 20:09:42 +08001316 strncpy(Value, copy_buf, strlen(copy_buf));
developer12fb9f62023-06-30 15:26:27 +08001317 Value[strlen(copy_buf)] = '\0';
developera3511852023-06-14 14:12:59 +08001318 pclose(fp);
1319 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001320
developera3511852023-06-14 14:12:59 +08001321 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001322}
1323
1324void wifi_RestartHostapd_2G()
1325{
developera3511852023-06-14 14:12:59 +08001326 int Public2GApIndex = 4;
developer72fb0bb2023-01-11 09:46:29 +08001327
developera3511852023-06-14 14:12:59 +08001328 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1329 wifi_setApEnable(Public2GApIndex, FALSE);
1330 wifi_setApEnable(Public2GApIndex, TRUE);
1331 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001332}
1333
1334void wifi_RestartHostapd_5G()
1335{
developera3511852023-06-14 14:12:59 +08001336 int Public5GApIndex = 5;
developer72fb0bb2023-01-11 09:46:29 +08001337
developera3511852023-06-14 14:12:59 +08001338 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1339 wifi_setApEnable(Public5GApIndex, FALSE);
1340 wifi_setApEnable(Public5GApIndex, TRUE);
1341 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001342}
1343
1344void wifi_RestartPrivateWifi_2G()
1345{
developera3511852023-06-14 14:12:59 +08001346 int PrivateApIndex = 0;
developer72fb0bb2023-01-11 09:46:29 +08001347
developera3511852023-06-14 14:12:59 +08001348 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1349 wifi_setApEnable(PrivateApIndex, FALSE);
1350 wifi_setApEnable(PrivateApIndex, TRUE);
1351 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001352}
1353
1354void wifi_RestartPrivateWifi_5G()
1355{
developera3511852023-06-14 14:12:59 +08001356 int Private5GApIndex = 1;
developer72fb0bb2023-01-11 09:46:29 +08001357
developera3511852023-06-14 14:12:59 +08001358 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1359 wifi_setApEnable(Private5GApIndex, FALSE);
1360 wifi_setApEnable(Private5GApIndex, TRUE);
1361 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001362}
1363
1364static int writeBandWidth(int radioIndex,char *bw_value)
1365{
developera3511852023-06-14 14:12:59 +08001366 char buf[MAX_BUF_SIZE];
1367 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08001368 int res;
developer72fb0bb2023-01-11 09:46:29 +08001369
developere40952c2023-06-15 18:46:43 +08001370 res = snprintf(cmd, sizeof(cmd), "grep SET_BW%d %s", radioIndex, BW_FNAME);
1371 if (os_snprintf_error(sizeof(cmd), res)) {
1372 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1373 return RETURN_ERR;
1374 }
1375
1376 if (_syscmd(cmd, buf, sizeof(buf))) {
1377 res = snprintf(cmd, sizeof(cmd), "echo SET_BW%d=%s >> %s", radioIndex, bw_value, BW_FNAME);
1378 if (os_snprintf_error(sizeof(cmd), res)) {
1379 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1380 return RETURN_ERR;
1381 }
developera3511852023-06-14 14:12:59 +08001382 _syscmd(cmd, buf, sizeof(buf));
1383 return RETURN_OK;
1384 }
developer72fb0bb2023-01-11 09:46:29 +08001385
developer75bd10c2023-06-27 11:34:08 +08001386 res = snprintf(cmd, sizeof(cmd), "sed -i 's/^SET_BW%d=.*$/SET_BW%d=%s/' %s",radioIndex,radioIndex,bw_value,BW_FNAME);
1387 if (os_snprintf_error(sizeof(cmd), res)) {
1388 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1389 return RETURN_ERR;
1390 }
developera3511852023-06-14 14:12:59 +08001391 _syscmd(cmd,buf,sizeof(buf));
1392 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001393}
1394
1395// Input could be "1Mbps"; "5.5Mbps"; "6Mbps"; "2Mbps"; "11Mbps"; "12Mbps"; "24Mbps"
1396INT wifi_setApBeaconRate(INT radioIndex,CHAR *beaconRate)
1397{
developera3511852023-06-14 14:12:59 +08001398 struct params params={'\0'};
1399 char config_file[MAX_BUF_SIZE] = {0};
1400 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08001401 int res;
developer72fb0bb2023-01-11 09:46:29 +08001402
developera3511852023-06-14 14:12:59 +08001403 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1404 // Copy the numeric value
1405 if (strlen (beaconRate) >= 5) {
1406 strncpy(buf, beaconRate, strlen(beaconRate) - 4);
1407 buf[strlen(beaconRate) - 4] = '\0';
developer9ce44382023-06-28 11:09:37 +08001408 } else if (strlen(beaconRate) > 0){
1409 strncpy(buf, beaconRate,sizeof(buf) - 1);
1410 buf[sizeof(buf) - 1] = '\0';
1411 } else
developera3511852023-06-14 14:12:59 +08001412 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001413
developera3511852023-06-14 14:12:59 +08001414 params.name = "beacon_rate";
1415 // hostapd config unit is 100 kbps. To convert Mbps to 100kbps, the value need to multiply 10.
1416 if (strncmp(buf, "5.5", 3) == 0) {
developere40952c2023-06-15 18:46:43 +08001417 res = snprintf(buf, sizeof(buf), "55");
1418 if (os_snprintf_error(sizeof(buf), res)) {
1419 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1420 return RETURN_ERR;
1421 }
developera3511852023-06-14 14:12:59 +08001422 params.value = buf;
1423 } else {
developer32f2a182023-06-27 19:50:41 +08001424 if (strlen(buf) >= (MAX_BUF_SIZE - 1)) {
1425 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
1426 return RETURN_ERR;
1427 }
1428 strncat(buf, "0", sizeof(buf) - strlen(buf) - 1);
developera3511852023-06-14 14:12:59 +08001429 params.value = buf;
1430 }
developer72fb0bb2023-01-11 09:46:29 +08001431
developer32f2a182023-06-27 19:50:41 +08001432 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
1433 if (os_snprintf_error(sizeof(config_file), res)) {
1434 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1435 return RETURN_ERR;
1436 }
1437
developera3511852023-06-14 14:12:59 +08001438 wifi_hostapdWrite(config_file, &params, 1);
1439 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
1440 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001441
developera3511852023-06-14 14:12:59 +08001442 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001443}
1444
1445INT wifi_getApBeaconRate(INT radioIndex, CHAR *beaconRate)
1446{
developera3511852023-06-14 14:12:59 +08001447 char config_file[128] = {'\0'};
1448 char temp_output[MAX_BUF_SIZE] = {'\0'};
1449 char buf[128] = {'\0'};
1450 char cmd[128] = {'\0'};
developerc14d83a2023-06-29 20:09:42 +08001451 long int rate = 0;
developere40952c2023-06-15 18:46:43 +08001452 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08001453
developera3511852023-06-14 14:12:59 +08001454 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1455 if (NULL == beaconRate)
1456 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001457
developer75bd10c2023-06-27 11:34:08 +08001458 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
1459 if (os_snprintf_error(sizeof(config_file), res)) {
1460 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1461 return RETURN_ERR;
1462 }
developera3511852023-06-14 14:12:59 +08001463 wifi_hostapdRead(config_file, "beacon_rate", buf, sizeof(buf));
1464 phyId = radio_index_to_phy(radioIndex);
1465 // Hostapd unit is 100kbps. To convert to 100kbps to Mbps, the value need to divide 10.
1466 if(strlen(buf) > 0) {
1467 if (strncmp(buf, "55", 2) == 0)
developere40952c2023-06-15 18:46:43 +08001468 res = snprintf(temp_output, sizeof(temp_output), "5.5Mbps");
developera3511852023-06-14 14:12:59 +08001469 else {
developerc14d83a2023-06-29 20:09:42 +08001470 if (hal_strtol(buf, 10, &rate) < 0) {
1471 wifi_debug(DEBUG_ERROR, "strtol fail\n");
1472 }
developerb14b3462023-07-01 18:02:42 +08001473 res = snprintf(temp_output, sizeof(temp_output), "%ldMbps", rate/10);
developera3511852023-06-14 14:12:59 +08001474 }
developer75bd10c2023-06-27 11:34:08 +08001475 if (os_snprintf_error(sizeof(temp_output), res)) {
1476 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1477 return RETURN_ERR;
1478 }
developera3511852023-06-14 14:12:59 +08001479 } else {
1480 // config not set, so we would use lowest rate as default
developer75bd10c2023-06-27 11:34:08 +08001481 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep Bitrates -A1 | tail -n 1 | awk '{print $2}' | tr -d '.0\\n'", phyId);
1482 if (os_snprintf_error(sizeof(cmd), res)) {
1483 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1484 return RETURN_ERR;
1485 }
developera3511852023-06-14 14:12:59 +08001486 _syscmd(cmd, buf, sizeof(buf));
developere40952c2023-06-15 18:46:43 +08001487 res = snprintf(temp_output, sizeof(temp_output), "%sMbps", buf);
developer75bd10c2023-06-27 11:34:08 +08001488 if (os_snprintf_error(sizeof(temp_output), res)) {
1489 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1490 return RETURN_ERR;
1491 }
developera3511852023-06-14 14:12:59 +08001492 }
developer75bd10c2023-06-27 11:34:08 +08001493
developera3511852023-06-14 14:12:59 +08001494 strncpy(beaconRate, temp_output, strlen(temp_output));
1495 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001496
developera3511852023-06-14 14:12:59 +08001497 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001498}
1499
1500INT wifi_setLED(INT radioIndex, BOOL enable)
1501{
1502 return 0;
1503}
1504INT wifi_setRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG seconds)
1505{
1506 return RETURN_OK;
1507}
1508/**********************************************************************************
1509 *
developer69b61b02023-03-07 17:17:44 +08001510 * Wifi Subsystem level function prototypes
developer72fb0bb2023-01-11 09:46:29 +08001511 *
1512**********************************************************************************/
1513//---------------------------------------------------------------------------------------------------
1514//Wifi system api
1515//Get the wifi hal version in string, eg "2.0.0". WIFI_HAL_MAJOR_VERSION.WIFI_HAL_MINOR_VERSION.WIFI_HAL_MAINTENANCE_VERSION
developer69b61b02023-03-07 17:17:44 +08001516INT wifi_getHalVersion(CHAR *output_string) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08001517{
developere40952c2023-06-15 18:46:43 +08001518 int res;
1519
developera3511852023-06-14 14:12:59 +08001520 if(!output_string)
1521 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08001522 res = snprintf(output_string, 64, "%d.%d.%d", WIFI_HAL_MAJOR_VERSION, WIFI_HAL_MINOR_VERSION, WIFI_HAL_MAINTENANCE_VERSION);
1523 if (os_snprintf_error(64, res)) {
1524 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1525 return RETURN_ERR;
1526 }
developer72fb0bb2023-01-11 09:46:29 +08001527
developera3511852023-06-14 14:12:59 +08001528 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001529}
1530
1531
1532/* wifi_factoryReset() function */
1533/**
developer69b61b02023-03-07 17:17:44 +08001534* @description Clears internal variables to implement a factory reset of the Wi-Fi
developer72fb0bb2023-01-11 09:46:29 +08001535* subsystem. Resets Implementation specifics may dictate some functionality since different hardware implementations may have different requirements.
1536*
1537* @param None
1538*
1539* @return The status of the operation.
1540* @retval RETURN_OK if successful.
1541* @retval RETURN_ERR if any error is detected
1542*
1543* @execution Synchronous
1544* @sideeffect None
1545*
1546* @note This function must not suspend and must not invoke any blocking system
1547* calls. It should probably just send a message to a driver event handler task.
1548*
1549*/
1550INT wifi_factoryReset()
1551{
developer47cc27a2023-05-17 23:09:58 +08001552 char cmd[MAX_CMD_SIZE] = {0};
1553 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001554 int res;
developer72fb0bb2023-01-11 09:46:29 +08001555
developer47cc27a2023-05-17 23:09:58 +08001556 /*delete running hostapd conf files*/
1557 wifi_dbg_printf("\n[%s]: deleting hostapd conf file.", __func__);
developere40952c2023-06-15 18:46:43 +08001558 res = snprintf(cmd, MAX_CMD_SIZE, "rm -rf /nvram/*.conf");
1559 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1560 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1561 return RETURN_ERR;
1562 }
1563
developer47cc27a2023-05-17 23:09:58 +08001564 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08001565
developerd14dff12023-06-28 22:47:44 +08001566 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08001567 wifi_psk_file_reset();
1568
1569 memset(cmd, 0, MAX_CMD_SIZE);
1570 memset(buf, 0, MAX_BUF_SIZE);
1571
developere40952c2023-06-15 18:46:43 +08001572 res = snprintf(cmd, MAX_CMD_SIZE, "systemctl restart hostapd.service");
1573 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1574 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1575 return RETURN_ERR;
1576 }
1577
developer47cc27a2023-05-17 23:09:58 +08001578 _syscmd(cmd, buf, sizeof(buf));
1579
1580 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001581}
1582
1583/* wifi_factoryResetRadios() function */
1584/**
1585* @description Restore all radio parameters without touching access point parameters. Resets Implementation specifics may dictate some functionality since different hardware implementations may have different requirements.
1586*
1587* @param None
1588* @return The status of the operation
1589* @retval RETURN_OK if successful
1590* @retval RETURN_ERR if any error is detected
1591*
1592* @execution Synchronous
1593*
1594* @sideeffect None
1595*
1596* @note This function must not suspend and must not invoke any blocking system
1597* calls. It should probably just send a message to a driver event handler task.
1598*
1599*/
1600INT wifi_factoryResetRadios()
1601{
developera3511852023-06-14 14:12:59 +08001602 if((RETURN_OK == wifi_factoryResetRadio(0)) && (RETURN_OK == wifi_factoryResetRadio(1)))
1603 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001604
developera3511852023-06-14 14:12:59 +08001605 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001606}
1607
developerfead3972023-05-25 20:15:02 +08001608ULONG get_radio_reset_cnt(int radioIndex)
1609{
1610 char cmd[MAX_CMD_SIZE] = {0};
1611 char buf[MAX_BUF_SIZE] = {0};
1612 ULONG reset_count = 0;
developere40952c2023-06-15 18:46:43 +08001613 int res;
developerfead3972023-05-25 20:15:02 +08001614
developere40952c2023-06-15 18:46:43 +08001615 res = snprintf(cmd, MAX_CMD_SIZE, "cat %s 2> /dev/null | grep \"^reset%d=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developerfead3972023-05-25 20:15:02 +08001616 RADIO_RESET_FILE, radioIndex);
developere40952c2023-06-15 18:46:43 +08001617 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1618 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1619 return RETURN_ERR;
1620 }
1621
developerfead3972023-05-25 20:15:02 +08001622 _syscmd(cmd, buf, sizeof(buf));
1623
1624 if (strlen(buf) == 0)
1625 return 0;
1626 else {
1627 reset_count = atol(buf);
1628 return reset_count;
1629 }
1630}
1631void update_radio_reset_cnt(int radioIndex)
1632{
1633 char cmd[MAX_CMD_SIZE] = {0};
1634 char buf[MAX_BUF_SIZE] = {0};
1635 ULONG reset_count = 0;
developere40952c2023-06-15 18:46:43 +08001636 int res;
developerfead3972023-05-25 20:15:02 +08001637
developere40952c2023-06-15 18:46:43 +08001638 res = snprintf(cmd, MAX_CMD_SIZE, "cat %s 2> /dev/null | grep \"^reset%d=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developerfead3972023-05-25 20:15:02 +08001639 RADIO_RESET_FILE, radioIndex);
developere40952c2023-06-15 18:46:43 +08001640 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1641 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1642 return;
1643 }
1644
developerfead3972023-05-25 20:15:02 +08001645 _syscmd(cmd, buf, sizeof(buf));
1646
1647 if (strlen(buf) == 0)
developere40952c2023-06-15 18:46:43 +08001648 res = snprintf(cmd, sizeof(cmd), "sed -i -e '$a reset%d=1' %s", radioIndex, RADIO_RESET_FILE);
developerfead3972023-05-25 20:15:02 +08001649 else {
1650 reset_count = atol(buf);
1651 reset_count++;
developere40952c2023-06-15 18:46:43 +08001652 res = snprintf(cmd, sizeof(cmd), "sed -i \"s/^reset%d=.*/reset%d=%lu/\" %s", radioIndex, radioIndex, reset_count, RADIO_RESET_FILE);
1653 }
1654 if (os_snprintf_error(sizeof(cmd), res)) {
1655 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1656 return;
developerfead3972023-05-25 20:15:02 +08001657 }
1658 _syscmd(cmd, buf, sizeof(buf));
1659}
developer72fb0bb2023-01-11 09:46:29 +08001660
1661/* wifi_factoryResetRadio() function */
1662/**
1663* @description Restore selected radio parameters without touching access point parameters
1664*
1665* @param radioIndex - Index of Wi-Fi Radio channel
1666*
1667* @return The status of the operation.
1668* @retval RETURN_OK if successful.
1669* @retval RETURN_ERR if any error is detected
1670*
1671* @execution Synchronous.
1672* @sideeffect None.
1673*
1674* @note This function must not suspend and must not invoke any blocking system
1675* calls. It should probably just send a message to a driver event handler task.
1676*
1677*/
1678INT wifi_factoryResetRadio(int radioIndex) //RDKB
1679{
developer47cc27a2023-05-17 23:09:58 +08001680 char cmd[MAX_CMD_SIZE] = {0};
1681 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001682 int res;
developer72fb0bb2023-01-11 09:46:29 +08001683
developer47cc27a2023-05-17 23:09:58 +08001684 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001685
developerb2977562023-05-24 17:54:12 +08001686 wifi_dat_file_reset_by_radio(radioIndex);
developer47cc27a2023-05-17 23:09:58 +08001687
developerb2977562023-05-24 17:54:12 +08001688 /*reset gi setting*/
developere40952c2023-06-15 18:46:43 +08001689 res = snprintf(cmd, sizeof(cmd), "echo 'Auto' > %s%d.txt", GUARD_INTERVAL_FILE, radioIndex);
1690 if (os_snprintf_error(sizeof(cmd), res)) {
1691 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1692 return RETURN_ERR;
1693 }
1694
developer47cc27a2023-05-17 23:09:58 +08001695 _syscmd(cmd, buf, sizeof(buf));
1696
developerb2977562023-05-24 17:54:12 +08001697 /*TBD: check mbss issue*/
1698 wifi_factoryResetAP(radioIndex);
developerfead3972023-05-25 20:15:02 +08001699 update_radio_reset_cnt(radioIndex);
developerb2977562023-05-24 17:54:12 +08001700
developer47cc27a2023-05-17 23:09:58 +08001701 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
1702 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001703}
1704
1705/* wifi_initRadio() function */
1706/**
1707* Description: This function call initializes the specified radio.
developer69b61b02023-03-07 17:17:44 +08001708* Implementation specifics may dictate the functionality since
developer72fb0bb2023-01-11 09:46:29 +08001709* different hardware implementations may have different initilization requirements.
1710* Parameters : radioIndex - The index of the radio. First radio is index 0. 2nd radio is index 1 - type INT
1711*
1712* @return The status of the operation.
1713* @retval RETURN_OK if successful.
1714* @retval RETURN_ERR if any error is detected
1715*
1716* @execution Synchronous.
1717* @sideeffect None.
1718*
1719* @note This function must not suspend and must not invoke any blocking system
1720* calls. It should probably just send a message to a driver event handler task.
1721*
1722*/
1723INT wifi_initRadio(INT radioIndex)
1724{
developera3511852023-06-14 14:12:59 +08001725 //TODO: Initializes the wifi subsystem (for specified radio)
1726 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001727}
1728
developer17038e62023-03-02 14:43:43 +08001729static void
1730wifi_ParseProfile(void)
1731{
developere40952c2023-06-15 18:46:43 +08001732 int i, res;
developera3511852023-06-14 14:12:59 +08001733 int max_radio_num = 0;
1734 int card_idx;
1735 int band_idx;
1736 int phy_idx = 0;
1737 int wireless_mode = 0;
1738 char buf[MAX_BUF_SIZE] = {0};
1739 char chip_name[12];
1740 char card_profile[MAX_BUF_SIZE] = {0};
1741 char band_profile[MAX_BUF_SIZE] = {0};
developer17038e62023-03-02 14:43:43 +08001742
developera3511852023-06-14 14:12:59 +08001743 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001744
developera3511852023-06-14 14:12:59 +08001745 memset(main_prefix, 0, sizeof(main_prefix));
1746 memset(ext_prefix, 0, sizeof(ext_prefix));
1747 memset(default_ssid, 0, sizeof(default_ssid));
1748 for (i = 0; i < MAX_NUM_RADIOS; i++)
1749 radio_band[i] = band_invalid;
developer17038e62023-03-02 14:43:43 +08001750
developera3511852023-06-14 14:12:59 +08001751 if (wifi_getMaxRadioNumber(&max_radio_num) != RETURN_OK) {
1752 /* LOG */
developer17038e62023-03-02 14:43:43 +08001753 return;
developera3511852023-06-14 14:12:59 +08001754 }
developer17038e62023-03-02 14:43:43 +08001755
developera3511852023-06-14 14:12:59 +08001756 for (card_idx = 0; card_idx < 3; card_idx++) {
developere40952c2023-06-15 18:46:43 +08001757 res = snprintf(buf, sizeof(buf), "INDEX%d", card_idx);
1758 if (os_snprintf_error(sizeof(buf), res)) {
1759 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1760 return;
1761 }
developera3511852023-06-14 14:12:59 +08001762 if (get_value(l1profile, buf, chip_name, sizeof(chip_name)) < 0) {
1763 break;
1764 }
developere40952c2023-06-15 18:46:43 +08001765 res = snprintf(buf, sizeof(buf), "INDEX%d_profile_path", card_idx);
1766 if (os_snprintf_error(sizeof(buf), res)) {
1767 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1768 return;
1769 }
developera3511852023-06-14 14:12:59 +08001770 if (get_value(l1profile, buf, card_profile, sizeof(card_profile)) < 0) {
1771 break;
1772 }
1773 for (band_idx = 0; band_idx < 3; band_idx++) {
developere40952c2023-06-15 18:46:43 +08001774 res = snprintf(buf, sizeof(buf), "BN%d_profile_path", band_idx);
1775 if (os_snprintf_error(sizeof(buf), res)) {
1776 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1777 return;
1778 }
developera3511852023-06-14 14:12:59 +08001779 if (get_value(card_profile, buf, band_profile, sizeof(band_profile)) < 0) {
1780 /* LOG */
1781 break;
1782 }
developer17038e62023-03-02 14:43:43 +08001783
developere40952c2023-06-15 18:46:43 +08001784 res = snprintf(buf, sizeof(buf), "INDEX%d_main_ifname", card_idx);
1785 if (os_snprintf_error(sizeof(buf), res)) {
1786 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1787 return;
1788 }
developera3511852023-06-14 14:12:59 +08001789 if (get_value_by_idx(l1profile, buf, band_idx, main_prefix[phy_idx], IFNAMSIZ) < 0) {
1790 /* LOG */
1791 }
developer17038e62023-03-02 14:43:43 +08001792
developere40952c2023-06-15 18:46:43 +08001793 res = snprintf(buf, sizeof(buf), "INDEX%d_ext_ifname", card_idx);
1794 if (os_snprintf_error(sizeof(buf), res)) {
1795 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1796 return;
1797 }
developera3511852023-06-14 14:12:59 +08001798 if (get_value_by_idx(l1profile, buf, band_idx, ext_prefix[phy_idx], IFNAMSIZ) < 0) {
1799 /* LOG */
1800 }
developer17038e62023-03-02 14:43:43 +08001801
developera3511852023-06-14 14:12:59 +08001802 if (get_value(band_profile, "SSID1", default_ssid[phy_idx], sizeof(default_ssid[phy_idx])) < 0) {
1803 /* LOG */
1804 }
1805 if (get_value(band_profile, "WirelessMode", buf, sizeof(buf)) < 0) {
1806 /* LOG */
1807 }
developer745f0bd2023-03-06 14:32:53 +08001808
developera3511852023-06-14 14:12:59 +08001809 wireless_mode = atoi(buf);
1810 switch (wireless_mode) {
1811 case 22:
1812 case 16:
1813 case 6:
1814 case 4:
1815 case 1:
1816 radio_band[phy_idx] = band_2_4;
1817 break;
1818 case 23:
1819 case 17:
1820 case 14:
1821 case 11:
1822 case 2:
1823 radio_band[phy_idx] = band_5;
1824 break;
1825 case 24:
1826 case 18:
1827 radio_band[phy_idx] = band_6;
1828 break;
1829 }
1830 phy_idx++;
1831 }
1832 }
developer17038e62023-03-02 14:43:43 +08001833
developera3511852023-06-14 14:12:59 +08001834 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001835}
1836
1837static void
developerd14dff12023-06-28 22:47:44 +08001838wifi_PrepareDefaultHostapdConfigs(bool reset)
developer17038e62023-03-02 14:43:43 +08001839{
developere40952c2023-06-15 18:46:43 +08001840 int radio_idx, res;
developer0132ed92023-03-21 13:48:53 +08001841 int bss_idx;
1842 int ap_idx;
developer0132ed92023-03-21 13:48:53 +08001843 char buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08001844 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer0132ed92023-03-21 13:48:53 +08001845 char ssid[MAX_BUF_SIZE] = {0};
1846 char interface[32] = {0};
1847 char ret_buf[MAX_BUF_SIZE] = {0};
1848 char psk_file[64] = {0};
1849 struct params params[3];
developer17038e62023-03-02 14:43:43 +08001850
developer0132ed92023-03-21 13:48:53 +08001851 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1852 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
developer47cc27a2023-05-17 23:09:58 +08001853
developer0132ed92023-03-21 13:48:53 +08001854 for (bss_idx = 0; bss_idx < 5; bss_idx++) {
developer47cc27a2023-05-17 23:09:58 +08001855 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
developer0132ed92023-03-21 13:48:53 +08001856
developere40952c2023-06-15 18:46:43 +08001857 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_idx);
1858 if (os_snprintf_error(sizeof(config_file), res)) {
1859 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1860 return;
1861 }
developerd14dff12023-06-28 22:47:44 +08001862 if (access(config_file, F_OK) == 0 && reset == FALSE)
1863 continue;
developere40952c2023-06-15 18:46:43 +08001864 res = snprintf(buf, sizeof(buf), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], config_file);
1865 if (os_snprintf_error(sizeof(buf), res)) {
1866 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1867 return;
1868 }
developer47cc27a2023-05-17 23:09:58 +08001869 _syscmd(buf, ret_buf, sizeof(ret_buf));
developer17038e62023-03-02 14:43:43 +08001870
developer47cc27a2023-05-17 23:09:58 +08001871 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08001872 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
1873 if (os_snprintf_error(sizeof(ssid), res)) {
1874 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1875 return;
1876 }
1877 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
1878 if (os_snprintf_error(sizeof(interface), res)) {
1879 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1880 return;
1881 }
developer47cc27a2023-05-17 23:09:58 +08001882 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08001883 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
1884 if (os_snprintf_error(sizeof(ssid), res)) {
1885 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1886 return;
1887 }
1888 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
1889 if (os_snprintf_error(sizeof(interface), res)) {
1890 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1891 return;
1892 }
developer47cc27a2023-05-17 23:09:58 +08001893 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08001894 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
1895 if (os_snprintf_error(sizeof(ssid), res)) {
1896 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1897 return;
1898 }
1899 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
1900 if (os_snprintf_error(sizeof(interface), res)) {
1901 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1902 return;
1903 }
developer47cc27a2023-05-17 23:09:58 +08001904 }
developer17038e62023-03-02 14:43:43 +08001905
developer47cc27a2023-05-17 23:09:58 +08001906 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08001907 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", ap_idx);
1908 if (os_snprintf_error(sizeof(psk_file), res)) {
1909 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1910 return;
1911 }
developer17038e62023-03-02 14:43:43 +08001912
developer47cc27a2023-05-17 23:09:58 +08001913 params[0].name = "ssid";
1914 params[0].value = ssid;
1915 params[1].name = "interface";
1916 params[1].value = interface;
1917 params[2].name = "wpa_psk_file";
1918 params[2].value = psk_file;
developer17038e62023-03-02 14:43:43 +08001919
developer47cc27a2023-05-17 23:09:58 +08001920 wifi_hostapdWrite(config_file, params, 3);
developer0132ed92023-03-21 13:48:53 +08001921 }
1922 }
1923 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001924}
1925
1926static void
developer4fe22c62023-08-03 13:23:58 +08001927wifiBringUpInterfacesForRadio(int radio_idx)
1928{
1929 int bss_idx;
1930 int ap_idx;
1931 char cmd[MAX_CMD_SIZE] = {0};
1932 char config_file[MAX_SUB_CMD_SIZE] = {0};
1933 char ret_buf[MAX_BUF_SIZE] = {0};
1934 char inf_name[IF_NAME_SIZE] = {0};
1935 int res;
1936
1937 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1938
1939 bss_idx = 0;
1940 /*TBD: we need refine setup flow and mbss flow*/
1941// for (bss_idx = 0; bss_idx < 5; bss_idx++) {
1942 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
1943
1944 res = snprintf(cmd, sizeof(cmd), "touch %s%d.psk", PSK_FILE, ap_idx);
1945 if (os_snprintf_error(sizeof(cmd), res)) {
1946 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1947 return;
1948 }
1949 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1950
1951 memset(cmd, 0, MAX_CMD_SIZE);
1952 memset(ret_buf, 0, MAX_BUF_SIZE);
1953
1954 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_idx);
1955 if (os_snprintf_error(sizeof(cmd), res)) {
1956 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1957 return;
1958 }
1959 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw ADD bss_config=phy%d:%s", radio_idx, config_file);
1960 if (os_snprintf_error(sizeof(cmd), res)) {
1961 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1962 return;
1963 }
1964 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1965
1966 wifi_GetInterfaceName(ap_idx, inf_name);
1967
1968 memset(cmd, 0, MAX_CMD_SIZE);
1969 memset(ret_buf, 0, MAX_BUF_SIZE);
1970
1971 /* fix vap-status file */
1972 res = snprintf(cmd, sizeof(cmd), "sed -i \"s/^%s=.*/%s=1/\" %s", inf_name, inf_name, VAP_STATUS_FILE);
1973 if (os_snprintf_error(sizeof(cmd), res)) {
1974 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1975 return;
1976 }
1977 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1978// }
1979
1980 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1981}
1982
1983
1984static void
1985wifi_BringUpInterfaces(void)
1986{
1987 int radio_idx;
1988 int band_idx;
1989
1990 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1991 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
1992 band_idx = radio_index_to_band(radio_idx);
1993 if (band_idx < 0) {
1994 break;
1995 }
1996 wifiBringUpInterfacesForRadio(radio_idx);
1997 }
1998 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1999}
2000
2001static void
developer17038e62023-03-02 14:43:43 +08002002wifi_BringDownInterfacesForRadio(int radio_idx)
2003{
developera3511852023-06-14 14:12:59 +08002004 char cmd[MAX_BUF_SIZE] = {0};
2005 char ret_buf[MAX_BUF_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08002006 int res;
developerb758dfd2023-06-21 17:32:07 +08002007
developera3511852023-06-14 14:12:59 +08002008 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08002009
developere40952c2023-06-15 18:46:43 +08002010 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw REMOVE %s", main_prefix[radio_idx]);
2011 if (os_snprintf_error(sizeof(cmd), res)) {
2012 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2013 return;
2014 }
developer8a3bbbf2023-03-15 17:47:23 +08002015 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2016
developera3511852023-06-14 14:12:59 +08002017 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08002018}
2019
2020
2021static void
2022wifi_BringDownInterfaces(void)
2023{
developera3511852023-06-14 14:12:59 +08002024 int radio_idx;
2025 int band_idx;
developer17038e62023-03-02 14:43:43 +08002026
developera3511852023-06-14 14:12:59 +08002027 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
2028 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
2029 band_idx = radio_index_to_band(radio_idx);
2030 if (band_idx < 0) {
2031 break;
2032 }
2033 wifi_BringDownInterfacesForRadio(radio_idx);
2034 }
2035 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08002036}
2037
developerb2977562023-05-24 17:54:12 +08002038static void wifi_dat_file_reset_by_radio(char radio_idx)
2039{
developerb149d9d2023-06-06 16:14:22 +08002040 char cmd[MAX_CMD_SIZE * 2] = {0};
developerb2977562023-05-24 17:54:12 +08002041 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08002042 char rom_dat_file[MAX_SUB_CMD_SIZE]= {0};
2043 char dat_file[MAX_SUB_CMD_SIZE]= {0};
developere40952c2023-06-15 18:46:43 +08002044 int res;
developerb2977562023-05-24 17:54:12 +08002045
developere40952c2023-06-15 18:46:43 +08002046 res = snprintf(rom_dat_file, sizeof(rom_dat_file), "%s%d.dat", ROM_LOGAN_DAT_FILE, radio_idx);
2047 if (os_snprintf_error(sizeof(rom_dat_file), res)) {
2048 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2049 return;
2050 }
2051 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
2052 if (os_snprintf_error(sizeof(dat_file), res)) {
2053 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2054 return;
2055 }
2056 res = snprintf(cmd, (MAX_CMD_SIZE * 2), "cp -rf %s %s", rom_dat_file, dat_file);
2057 if (os_snprintf_error((MAX_CMD_SIZE * 2), res)) {
2058 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2059 return;
2060 }
developerb2977562023-05-24 17:54:12 +08002061 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2062
2063}
2064
developer47cc27a2023-05-17 23:09:58 +08002065static void wifi_psk_file_reset()
2066{
2067 char cmd[MAX_CMD_SIZE] = {0};
2068 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08002069 char psk_file[MAX_SUB_CMD_SIZE]= {0};
developer47cc27a2023-05-17 23:09:58 +08002070 char vap_idx = 0;
developere40952c2023-06-15 18:46:43 +08002071 int res;
developer47cc27a2023-05-17 23:09:58 +08002072
2073 for (vap_idx = 0; vap_idx < MAX_APS; vap_idx++) {
developere40952c2023-06-15 18:46:43 +08002074 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, vap_idx);
2075 if (os_snprintf_error(sizeof(psk_file), res)) {
2076 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2077 return;
2078 }
developer47cc27a2023-05-17 23:09:58 +08002079
2080 if (access(psk_file, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002081 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", psk_file);
2082 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2083 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2084 return;
2085 }
developer47cc27a2023-05-17 23:09:58 +08002086 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2087 } else {
developere40952c2023-06-15 18:46:43 +08002088 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", psk_file);
2089 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2090 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2091 return;
2092 }
2093
developer47cc27a2023-05-17 23:09:58 +08002094 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2095 }
2096 }
developerb2977562023-05-24 17:54:12 +08002097}
2098
developer8a3bbbf2023-03-15 17:47:23 +08002099static void wifi_vap_status_reset()
2100{
developera3511852023-06-14 14:12:59 +08002101 char cmd[MAX_CMD_SIZE] = {0};
2102 char ret_buf[MAX_BUF_SIZE] = {0};
developer863a4a62023-06-06 16:55:59 +08002103 int radio_idx = 0;
developer8a3bbbf2023-03-15 17:47:23 +08002104 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08002105 int res;
developer8666b312023-03-24 14:05:31 +08002106
developer8a3bbbf2023-03-15 17:47:23 +08002107 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002108 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", VAP_STATUS_FILE);
2109 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2110 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2111 return;
2112 }
developer8a3bbbf2023-03-15 17:47:23 +08002113 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2114 } else {
developere40952c2023-06-15 18:46:43 +08002115 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", VAP_STATUS_FILE);
2116 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2117 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2118 return;
2119 }
developer8a3bbbf2023-03-15 17:47:23 +08002120 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2121 }
2122
2123 memset(cmd, 0, MAX_CMD_SIZE);
2124 memset(ret_buf, 0, MAX_BUF_SIZE);
2125
2126 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++)
developera3511852023-06-14 14:12:59 +08002127 for (bss_idx = 0; bss_idx < 5; bss_idx++) {
developere40952c2023-06-15 18:46:43 +08002128 res = snprintf(cmd, MAX_CMD_SIZE, "echo %s%d=0 >> %s", ext_prefix[radio_idx], bss_idx, VAP_STATUS_FILE);
2129 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2130 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2131 return;
2132 }
developer8a3bbbf2023-03-15 17:47:23 +08002133 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2134 }
2135
developerfead3972023-05-25 20:15:02 +08002136}
2137
2138static void wifi_radio_reset_count_reset()
2139{
developera3511852023-06-14 14:12:59 +08002140 char cmd[MAX_CMD_SIZE] = {0};
2141 char ret_buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002142 int res;
developerfead3972023-05-25 20:15:02 +08002143
2144 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002145 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", RADIO_RESET_FILE);
2146 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2147 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2148 return;
2149 }
developerfead3972023-05-25 20:15:02 +08002150 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2151 } else {
developere40952c2023-06-15 18:46:43 +08002152 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", RADIO_RESET_FILE);
2153 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2154 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2155 return;
2156 }
developerfead3972023-05-25 20:15:02 +08002157 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2158 }
developer8a3bbbf2023-03-15 17:47:23 +08002159}
developer17038e62023-03-02 14:43:43 +08002160
developer72fb0bb2023-01-11 09:46:29 +08002161// Initializes the wifi subsystem (all radios)
developera3511852023-06-14 14:12:59 +08002162INT wifi_init() //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002163{
developera3511852023-06-14 14:12:59 +08002164 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developercc9a4f32023-05-30 17:40:02 +08002165 static int CallOnce = 1;
developera3511852023-06-14 14:12:59 +08002166 //Not intitializing macfilter for Turris-Omnia Platform for now
2167 //macfilter_init();
2168 if (CallOnce) {
developercc9a4f32023-05-30 17:40:02 +08002169 wifi_ParseProfile();
developerd14dff12023-06-28 22:47:44 +08002170 wifi_PrepareDefaultHostapdConfigs(FALSE);
developercc9a4f32023-05-30 17:40:02 +08002171 wifi_psk_file_reset();
developer33f13ba2023-07-12 16:19:06 +08002172 //v_secure_system("/usr/sbin/iw reg set US");
2173 v_secure_system("systemctl start hostapd.service");
developercc9a4f32023-05-30 17:40:02 +08002174 sleep(2);
developerc338aba2023-08-09 13:56:42 +08002175 setbuf(stdout, NULL);
developercc9a4f32023-05-30 17:40:02 +08002176 wifi_vap_status_reset();
2177 wifi_radio_reset_count_reset();
developer4fe22c62023-08-03 13:23:58 +08002178 wifi_BringUpInterfaces();
developercc9a4f32023-05-30 17:40:02 +08002179 CallOnce = 0;
developera3511852023-06-14 14:12:59 +08002180 }
developer96b38512023-02-22 11:17:45 +08002181
developera3511852023-06-14 14:12:59 +08002182 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002183
developera3511852023-06-14 14:12:59 +08002184 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002185}
2186
2187/* wifi_reset() function */
2188/**
2189* Description: Resets the Wifi subsystem. This includes reset of all AP varibles.
developer69b61b02023-03-07 17:17:44 +08002190* Implementation specifics may dictate what is actualy reset since
developer72fb0bb2023-01-11 09:46:29 +08002191* different hardware implementations may have different requirements.
2192* Parameters : None
2193*
2194* @return The status of the operation.
2195* @retval RETURN_OK if successful.
2196* @retval RETURN_ERR if any error is detected
2197*
2198* @execution Synchronous.
2199* @sideeffect None.
2200*
2201* @note This function must not suspend and must not invoke any blocking system
2202* calls. It should probably just send a message to a driver event handler task.
2203*
2204*/
2205INT wifi_reset()
2206{
developer17038e62023-03-02 14:43:43 +08002207
developera3511852023-06-14 14:12:59 +08002208 wifi_BringDownInterfaces();
2209 sleep(2);
developer17038e62023-03-02 14:43:43 +08002210
developera3511852023-06-14 14:12:59 +08002211 //TODO: resets the wifi subsystem, deletes all APs
developer33f13ba2023-07-12 16:19:06 +08002212 v_secure_system("systemctl stop hostapd.service");
developera3511852023-06-14 14:12:59 +08002213 sleep(2);
developer17038e62023-03-02 14:43:43 +08002214
developer33f13ba2023-07-12 16:19:06 +08002215 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +08002216 sleep(5);
developer17038e62023-03-02 14:43:43 +08002217
developerd14dff12023-06-28 22:47:44 +08002218 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08002219 wifi_psk_file_reset();
developer4fe22c62023-08-03 13:23:58 +08002220 wifi_BringUpInterfaces();
developera3511852023-06-14 14:12:59 +08002221 sleep(2);
developer8a3bbbf2023-03-15 17:47:23 +08002222
2223 wifi_vap_status_reset();
2224
developera3511852023-06-14 14:12:59 +08002225 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002226}
2227
2228/* wifi_down() function */
2229/**
2230* @description Turns off transmit power for the entire Wifi subsystem, for all radios.
developer69b61b02023-03-07 17:17:44 +08002231* Implementation specifics may dictate some functionality since
developer72fb0bb2023-01-11 09:46:29 +08002232* different hardware implementations may have different requirements.
2233*
2234* @param None
2235*
2236* @return The status of the operation
2237* @retval RETURN_OK if successful
2238* @retval RETURN_ERR if any error is detected
2239*
2240* @execution Synchronous
2241* @sideeffect None
2242*
2243* @note This function must not suspend and must not invoke any blocking system
2244* calls. It should probably just send a message to a driver event handler task.
2245*
2246*/
2247INT wifi_down()
2248{
developera3511852023-06-14 14:12:59 +08002249 //TODO: turns off transmit power for the entire Wifi subsystem, for all radios
2250 int max_num_radios = 0;
developerb2977562023-05-24 17:54:12 +08002251 wifi_getMaxRadioNumber(&max_num_radios);
developer17038e62023-03-02 14:43:43 +08002252
developerb2977562023-05-24 17:54:12 +08002253 for (int radioIndex = 0; radioIndex < max_num_radios; radioIndex++)
2254 wifi_setRadioEnable(radioIndex, FALSE);
2255
developera3511852023-06-14 14:12:59 +08002256 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002257}
2258
2259
2260/* wifi_createInitialConfigFiles() function */
2261/**
2262* @description This function creates wifi configuration files. The format
developer69b61b02023-03-07 17:17:44 +08002263* and content of these files are implementation dependent. This function call is
2264* used to trigger this task if necessary. Some implementations may not need this
2265* function. If an implementation does not need to create config files the function call can
developer72fb0bb2023-01-11 09:46:29 +08002266* do nothing and return RETURN_OK.
2267*
2268* @param None
2269*
2270* @return The status of the operation
2271* @retval RETURN_OK if successful
2272* @retval RETURN_ERR if any error is detected
2273*
2274* @execution Synchronous
2275* @sideeffect None
2276*
2277* @note This function must not suspend and must not invoke any blocking system
2278* calls. It should probably just send a message to a driver event handler task.
2279*
2280*/
2281INT wifi_createInitialConfigFiles()
2282{
developera3511852023-06-14 14:12:59 +08002283 //TODO: creates initial implementation dependent configuration files that are later used for variable storage. Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
2284 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002285}
2286
developer7e4a2a62023-04-06 19:56:03 +08002287/* outputs the country code to a max 64 character string */
developer72fb0bb2023-01-11 09:46:29 +08002288INT wifi_getRadioCountryCode(INT radioIndex, CHAR *output_string)
2289{
developera3511852023-06-14 14:12:59 +08002290 int ret;
developer72fb0bb2023-01-11 09:46:29 +08002291
developera3511852023-06-14 14:12:59 +08002292 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002293
developera3511852023-06-14 14:12:59 +08002294 ret = wifi_BandProfileRead(0, radioIndex, "CountryCode", output_string, 64, NULL);
2295 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08002296 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead CountryCode failed\n");
developera3511852023-06-14 14:12:59 +08002297 return RETURN_ERR;
2298 }
developer7e4a2a62023-04-06 19:56:03 +08002299
developera3511852023-06-14 14:12:59 +08002300 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002301
developera3511852023-06-14 14:12:59 +08002302 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002303}
2304
2305INT wifi_setRadioCountryCode(INT radioIndex, CHAR *CountryCode)
2306{
developer7e4a2a62023-04-06 19:56:03 +08002307 /*Set wifi config. Wait for wifi reset to apply*/
developer7e4a2a62023-04-06 19:56:03 +08002308 struct params params;
2309 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002310 int ret = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002311
developer7e4a2a62023-04-06 19:56:03 +08002312 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002313
developer7e4a2a62023-04-06 19:56:03 +08002314 if(NULL == CountryCode || strlen(CountryCode) >= 32 ) {
2315 printf("%s: input para error!!!\n", __func__);
2316 return RETURN_ERR;
2317 }
developer72fb0bb2023-01-11 09:46:29 +08002318
developerc79e9172023-06-06 19:48:03 +08002319 if (!strlen(CountryCode)) {
2320 memcpy(CountryCode, "US", strlen("US")); /*default set the code to US*/
2321 CountryCode[2] = '\0';
2322 }
developer72fb0bb2023-01-11 09:46:29 +08002323
developer7e4a2a62023-04-06 19:56:03 +08002324 params.name = "country_code";
2325 params.value = CountryCode;
developer72fb0bb2023-01-11 09:46:29 +08002326
developere40952c2023-06-15 18:46:43 +08002327 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, radioIndex);
2328 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
2329 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2330 return RETURN_ERR;
2331 }
developer7e4a2a62023-04-06 19:56:03 +08002332 ret = wifi_hostapdWrite(config_file, &params, 1);
2333
2334 if (ret) {
2335 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n",
2336 __func__, ret);
2337 }
2338
2339 ret = wifi_hostapdProcessUpdate(radioIndex, &params, 1);
2340
2341 if (ret) {
2342 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n",
2343 __func__, ret);
2344 }
2345
2346 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
2347
2348 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002349}
2350
2351INT wifi_getRadioChannelStats2(INT radioIndex, wifi_channelStats2_t *outputChannelStats2)
2352{
developera3511852023-06-14 14:12:59 +08002353 char interface_name[16] = {0};
2354 char channel_util_file[64] = {0};
2355 char cmd[128] = {0};
2356 char buf[128] = {0};
2357 char *line = NULL;
2358 char *param = NULL, *value = NULL;
developere40952c2023-06-15 18:46:43 +08002359 int read = 0, res;
developera3511852023-06-14 14:12:59 +08002360 unsigned int ActiveTime = 0, BusyTime = 0, TransmitTime = 0;
developer86035662023-06-28 19:21:12 +08002361 unsigned long preActiveTime = 0, preBusyTime = 0, preTransmitTime = 0;
developerc14d83a2023-06-29 20:09:42 +08002362 long int rssi;
2363 long int tmp_l;
2364 unsigned long tmp_ul;
developera3511852023-06-14 14:12:59 +08002365 size_t len = 0;
2366 FILE *f = NULL;
developer72fb0bb2023-01-11 09:46:29 +08002367
developera3511852023-06-14 14:12:59 +08002368 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002369
developera3511852023-06-14 14:12:59 +08002370 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2371 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002372 res = snprintf(cmd, sizeof(cmd), "iw %s scan | grep signal | awk '{print $2}' | sort -n | tail -n1", interface_name);
2373 if (os_snprintf_error(sizeof(cmd), res)) {
2374 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2375 return RETURN_ERR;
2376 }
2377
developera3511852023-06-14 14:12:59 +08002378 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08002379 if (hal_strtol(buf, 10, &rssi) < 0) {
2380 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2381 }
2382 outputChannelStats2->ch_Max80211Rssi = rssi;
developer72fb0bb2023-01-11 09:46:29 +08002383
developera3511852023-06-14 14:12:59 +08002384 memset(cmd, 0, sizeof(cmd));
2385 memset(buf, 0, sizeof(buf));
developere40952c2023-06-15 18:46:43 +08002386 res = snprintf(cmd, sizeof(cmd), "iw %s survey dump | grep 'in use' -A6", interface_name);
2387 if (os_snprintf_error(sizeof(cmd), res)) {
2388 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2389 return RETURN_ERR;
2390 }
developera3511852023-06-14 14:12:59 +08002391 if ((f = popen(cmd, "r")) == NULL) {
2392 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
2393 return RETURN_ERR;
2394 }
developer72fb0bb2023-01-11 09:46:29 +08002395
developera3511852023-06-14 14:12:59 +08002396 read = getline(&line, &len, f);
2397 while (read != -1) {
2398 param = strtok(line, ":\t");
developerc14d83a2023-06-29 20:09:42 +08002399 if (!param) {
2400 read = getline(&line, &len, f);
2401 continue;
2402 }
developera3511852023-06-14 14:12:59 +08002403 value = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +08002404 if (!value) {
developer37646972023-06-29 10:58:43 +08002405 read = getline(&line, &len, f);
2406 continue;
2407 }
developera3511852023-06-14 14:12:59 +08002408 if(strstr(param, "frequency") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002409 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2410 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2411 }
2412 outputChannelStats2->ch_Frequency = tmp_ul;
developera3511852023-06-14 14:12:59 +08002413 }
2414 if(strstr(param, "noise") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002415 if (hal_strtol(value, 10, &tmp_l) < 0) {
2416 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2417 }
2418 outputChannelStats2->ch_NoiseFloor = tmp_l;
developer37646972023-06-29 10:58:43 +08002419
developerc14d83a2023-06-29 20:09:42 +08002420 if (hal_strtol(value, 10, &tmp_l) < 0) {
2421 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2422 }
2423 outputChannelStats2->ch_Non80211Noise = tmp_l;
developera3511852023-06-14 14:12:59 +08002424 }
2425 if(strstr(param, "channel active time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002426 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2427 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2428 }
2429 ActiveTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002430 }
developer5b23cd02023-07-19 20:26:03 +08002431 if(strstr(param, "channel busy time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002432 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2433 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2434 }
2435 BusyTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002436 }
2437 if(strstr(param, "channel transmit time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002438 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2439 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2440 }
2441 TransmitTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002442 }
2443 read = getline(&line, &len, f);
2444 }
2445 pclose(f);
developer72fb0bb2023-01-11 09:46:29 +08002446
developera3511852023-06-14 14:12:59 +08002447 // The file should store the last active, busy and transmit time
developere40952c2023-06-15 18:46:43 +08002448 res = snprintf(channel_util_file, sizeof(channel_util_file), "%s%d.txt", CHANNEL_STATS_FILE, radioIndex);
2449 if (os_snprintf_error(sizeof(channel_util_file), res)) {
2450 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2451 return RETURN_ERR;
2452 }
2453
developera3511852023-06-14 14:12:59 +08002454 f = fopen(channel_util_file, "r");
2455 if (f != NULL) {
2456 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002457 if (hal_strtoul(line, 10, &preActiveTime) < 0) {
2458 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002459 }
developera3511852023-06-14 14:12:59 +08002460 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002461 if (hal_strtoul(line, 10, &preBusyTime) < 0) {
2462 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerb6f05602023-06-30 11:32:41 +08002463
developer86035662023-06-28 19:21:12 +08002464 }
developera3511852023-06-14 14:12:59 +08002465 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002466 if (hal_strtoul(line, 10, &preTransmitTime) < 0) {
2467 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002468 }
developer5b23cd02023-07-19 20:26:03 +08002469
developerc14d83a2023-06-29 20:09:42 +08002470 if (fclose(f) == EOF)
2471 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developer5b23cd02023-07-19 20:26:03 +08002472
developerc14d83a2023-06-29 20:09:42 +08002473 }
2474
developerb14b3462023-07-01 18:02:42 +08002475 /*if (ActiveTime == preActiveTime) {
developer5b23cd02023-07-19 20:26:03 +08002476 wifi_debug(DEBUG_ERROR, "error:ActiveTime[%d] == preActiveTime[%ld]\n",
developerb14b3462023-07-01 18:02:42 +08002477 ActiveTime, preActiveTime);
developerc14d83a2023-06-29 20:09:42 +08002478 return RETURN_ERR;
developerb14b3462023-07-01 18:02:42 +08002479 }*/
developer72fb0bb2023-01-11 09:46:29 +08002480
developera3511852023-06-14 14:12:59 +08002481 outputChannelStats2->ch_ObssUtil = (BusyTime - preBusyTime)*100/(ActiveTime - preActiveTime);
2482 outputChannelStats2->ch_SelfBssUtil = (TransmitTime - preTransmitTime)*100/(ActiveTime - preActiveTime);
developer72fb0bb2023-01-11 09:46:29 +08002483
developera3511852023-06-14 14:12:59 +08002484 f = fopen(channel_util_file, "w");
2485 if (f != NULL) {
developer86035662023-06-28 19:21:12 +08002486 if (fprintf(f, "%u\n%u\n%u\n", ActiveTime, BusyTime, TransmitTime) < 0) {
2487 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
2488 }
2489 if (fclose(f) != 0) {
2490 wifi_debug(DEBUG_ERROR, "fclose fail\n");
2491 }
developera3511852023-06-14 14:12:59 +08002492 }
2493 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2494 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002495}
2496
2497/**********************************************************************************
2498 *
2499 * Wifi radio level function prototypes
2500 *
2501**********************************************************************************/
2502
2503//Get the total number of radios in this wifi subsystem
2504INT wifi_getRadioNumberOfEntries(ULONG *output) //Tr181
2505{
developera3511852023-06-14 14:12:59 +08002506 if (NULL == output)
2507 return RETURN_ERR;
2508 *output = MAX_NUM_RADIOS;
developer72fb0bb2023-01-11 09:46:29 +08002509
developera3511852023-06-14 14:12:59 +08002510 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002511}
2512
developer69b61b02023-03-07 17:17:44 +08002513//Get the total number of SSID entries in this wifi subsystem
developer72fb0bb2023-01-11 09:46:29 +08002514INT wifi_getSSIDNumberOfEntries(ULONG *output) //Tr181
2515{
developera3511852023-06-14 14:12:59 +08002516 if (NULL == output)
2517 return RETURN_ERR;
2518 *output = MAX_APS;
developer72fb0bb2023-01-11 09:46:29 +08002519
developera3511852023-06-14 14:12:59 +08002520 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002521}
2522
2523//Get the Radio enable config parameter
developera3511852023-06-14 14:12:59 +08002524INT wifi_getRadioEnable(INT radioIndex, BOOL *output_bool) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002525{
developer56fbedb2023-05-30 16:47:05 +08002526 char interface_name[16] = {0};
2527 char buf[128] = {0}, cmd[128] = {0};
2528 int apIndex;
2529 int max_radio_num = 0;
developer75bd10c2023-06-27 11:34:08 +08002530 int res;
developer3a85ab82023-05-25 11:59:38 +08002531
developer56fbedb2023-05-30 16:47:05 +08002532 if (NULL == output_bool)
2533 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002534
developer56fbedb2023-05-30 16:47:05 +08002535 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08002536
developer56fbedb2023-05-30 16:47:05 +08002537 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002538
developer56fbedb2023-05-30 16:47:05 +08002539 if (radioIndex >= max_radio_num)
2540 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002541
developer56fbedb2023-05-30 16:47:05 +08002542 /* loop all interface in radio, if any is enable, reture true, else return false */
2543 for(apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num)
2544 {
2545 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2546 continue;
2547 memset(cmd, 0, sizeof(cmd));
developer75bd10c2023-06-27 11:34:08 +08002548 res = snprintf(cmd, sizeof(cmd), "ifconfig %s 2> /dev/null | grep UP", interface_name);
2549 if (os_snprintf_error(sizeof(cmd), res)) {
2550 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2551 return RETURN_ERR;
2552 }
developer56fbedb2023-05-30 16:47:05 +08002553 *output_bool = _syscmd(cmd, buf, sizeof(buf)) ? FALSE : TRUE;
2554 if (*output_bool == TRUE)
2555 break;
2556 }
2557
2558 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002559}
2560
developere82c0ca2023-05-10 16:25:35 +08002561typedef long time_t;
2562static time_t radio_up_time[MAX_NUM_RADIOS];
2563
developer72fb0bb2023-01-11 09:46:29 +08002564INT wifi_setRadioEnable(INT radioIndex, BOOL enable)
2565{
developera3511852023-06-14 14:12:59 +08002566 char interface_name[16] = {0};
2567 char cmd[MAX_CMD_SIZE] = {0};
2568 char buf[MAX_BUF_SIZE] = {0};
2569 int apIndex;
2570 int max_radio_num = 0;
developere40952c2023-06-15 18:46:43 +08002571 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002572
developera3511852023-06-14 14:12:59 +08002573 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002574
developera3511852023-06-14 14:12:59 +08002575 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08002576
developera3511852023-06-14 14:12:59 +08002577 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002578
developera3511852023-06-14 14:12:59 +08002579 if(enable == FALSE) {
developer47cc27a2023-05-17 23:09:58 +08002580
2581 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2582 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002583
2584 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw REMOVE %s", interface_name);
2585 if (os_snprintf_error(sizeof(cmd), res)) {
2586 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2587 return RETURN_ERR;
2588 }
developer47cc27a2023-05-17 23:09:58 +08002589
developer8a3bbbf2023-03-15 17:47:23 +08002590 _syscmd(cmd, buf, sizeof(buf));
developer56fbedb2023-05-30 16:47:05 +08002591 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08002592 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s down", interface_name);
2593 if (os_snprintf_error(sizeof(cmd), res)) {
2594 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2595 return RETURN_ERR;
2596 }
developer56fbedb2023-05-30 16:47:05 +08002597 _syscmd(cmd, buf, sizeof(buf));
developere82c0ca2023-05-10 16:25:35 +08002598 if(strncmp(buf, "OK", 2))
developer75bd10c2023-06-27 11:34:08 +08002599 wifi_debug(DEBUG_ERROR, "Could not detach %s from hostapd daemon", interface_name);
developer8a3bbbf2023-03-15 17:47:23 +08002600 } else {
developere82c0ca2023-05-10 16:25:35 +08002601 for (apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num) {
developera3511852023-06-14 14:12:59 +08002602 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2603 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002604
developer8a3bbbf2023-03-15 17:47:23 +08002605 memset(cmd, 0, MAX_CMD_SIZE);
2606 memset(buf, 0, MAX_BUF_SIZE);
2607
developere40952c2023-06-15 18:46:43 +08002608 res = snprintf(cmd, sizeof(cmd), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
2609 if (os_snprintf_error(sizeof(cmd), res)) {
2610 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2611 return RETURN_ERR;
2612 }
developera3511852023-06-14 14:12:59 +08002613 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002614
developera3511852023-06-14 14:12:59 +08002615 if(*buf == '1') {
developere40952c2023-06-15 18:46:43 +08002616 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s up", interface_name);
2617 if (os_snprintf_error(sizeof(cmd), res)) {
2618 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2619 return RETURN_ERR;
2620 }
developer56fbedb2023-05-30 16:47:05 +08002621 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002622
2623 memset(cmd, 0, MAX_CMD_SIZE);
2624 memset(buf, 0, MAX_BUF_SIZE);
2625
developere40952c2023-06-15 18:46:43 +08002626 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw ADD bss_config=phy%d:/nvram/hostapd%d.conf",
developera3511852023-06-14 14:12:59 +08002627 phyId, apIndex);
developere40952c2023-06-15 18:46:43 +08002628 if (os_snprintf_error(sizeof(cmd), res)) {
2629 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2630 return RETURN_ERR;
2631 }
developera3511852023-06-14 14:12:59 +08002632 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002633
developera3511852023-06-14 14:12:59 +08002634 }
2635 }
developere75ba632023-06-29 16:03:33 +08002636 if (time(&radio_up_time[radioIndex]) < 0) {
2637 wifi_debug(DEBUG_ERROR, "GET time fail\n");
2638 return RETURN_ERR;
2639 }
developera3511852023-06-14 14:12:59 +08002640 }
developer72fb0bb2023-01-11 09:46:29 +08002641
developera3511852023-06-14 14:12:59 +08002642 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2643 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002644}
2645
2646//Get the Radio enable status
2647INT wifi_getRadioStatus(INT radioIndex, BOOL *output_bool) //RDKB
2648{
developera3511852023-06-14 14:12:59 +08002649 if (NULL == output_bool)
2650 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002651
developera3511852023-06-14 14:12:59 +08002652 return wifi_getRadioEnable(radioIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08002653}
2654
2655//Get the Radio Interface name from platform, eg "wlan0"
2656INT wifi_getRadioIfName(INT radioIndex, CHAR *output_string) //Tr181
2657{
developera3511852023-06-14 14:12:59 +08002658 if (NULL == output_string || radioIndex>=MAX_NUM_RADIOS || radioIndex<0)
2659 return RETURN_ERR;
2660 return wifi_GetInterfaceName(radioIndex, output_string);
developer72fb0bb2023-01-11 09:46:29 +08002661}
2662
developer6e578302023-06-21 10:11:16 +08002663int mtk_get_vow_info_callback(struct nl_msg *msg, void *data)
2664{
2665 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2666 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX + 1];
2667 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2668 int err = 0;
2669 struct vow_info *vow_info = NULL;
2670 struct mtk_nl80211_cb_data *cb_data = data;
2671
2672 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2673 genlmsg_attrlen(gnlh, 0), NULL);
2674 if (err < 0) {
2675 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
2676 return err;
2677 }
2678
2679 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2680 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX,
2681 tb[NL80211_ATTR_VENDOR_DATA], NULL);
2682 if (err < 0){
2683 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX fails\n");
2684 return err;
2685 }
2686
2687 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]) {
2688 vow_info = (struct vow_info *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]);
2689 memmove(cb_data->out_buf, vow_info, sizeof(struct vow_info));
2690 }
2691 }
2692
2693 return 0;
2694}
2695
2696INT mtk_wifi_set_air_time_management(
2697 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
2698 char* data, INT len, void *output)
2699{
2700 char inf_name[IF_NAME_SIZE] = {0};
2701 unsigned int if_idx = 0;
2702 int ret = -1;
2703 struct unl unl_ins;
2704 struct nl_msg *msg = NULL;
2705 struct nlattr * msg_data = NULL;
2706 struct mtk_nl80211_param param;
2707
2708 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
2709 return RETURN_ERR;
2710 if_idx = if_nametoindex(inf_name);
2711 if (!if_idx) {
2712 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
2713 return RETURN_ERR;
2714 }
2715 /*init mtk nl80211 vendor cmd*/
2716 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_VOW;
2717 param.if_type = NL80211_ATTR_IFINDEX;
2718 param.if_idx = if_idx;
2719
2720 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
2721 if (ret) {
2722 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
2723 return RETURN_ERR;
2724 }
2725 /*add mtk vendor cmd data*/
2726 if (nla_put(msg, vendor_data_attr, len, data)) {
2727 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
2728 nlmsg_free(msg);
2729 goto err;
2730 }
2731
2732 /*send mtk nl80211 vendor msg*/
2733 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output);
2734 if (ret) {
2735 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
2736 goto err;
2737 }
2738 /*deinit mtk nl80211 vendor msg*/
2739 mtk_nl80211_deint(&unl_ins);
2740 wifi_debug(DEBUG_INFO, "send cmd success.\n");
2741
2742 return RETURN_OK;
2743err:
2744 mtk_nl80211_deint(&unl_ins);
2745 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
2746 return RETURN_ERR;
2747}
2748
2749//Get the ATM(Air Time Management) Capable.
2750INT wifi_getATMCapable(BOOL *output_bool)
2751{
2752 if (NULL == output_bool)
2753 return RETURN_ERR;
2754 *output_bool = TRUE;
2755
2756 return RETURN_OK;
2757}
2758
2759INT wifi_setATMEnable(BOOL enable)
2760{
2761 int max_radio_num = 0;
2762 int radio_idx = 0;
2763 char dat_file[MAX_BUF_SIZE] = {0};
2764 int res;
2765 struct params params[2];
2766
2767 wifi_getMaxRadioNumber(&max_radio_num);
2768 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2769 if (mtk_wifi_set_air_time_management
2770 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO,
2771 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2772 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2773 return RETURN_ERR;
2774 }
2775
2776 if (mtk_wifi_set_air_time_management
2777 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_BW_EN_INFO,
2778 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2779 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2780 return RETURN_ERR;
2781 }
2782
2783 params[0].name = "VOW_Airtime_Fairness_En";
2784 params[0].value = enable ? "1" : "0";
2785 params[1].name = "VOW_BW_Ctrl";
2786 params[1].value = enable ? "1" : "0";
2787
2788 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
2789 if (os_snprintf_error(sizeof(dat_file), res)) {
2790 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2791 return RETURN_ERR;
2792 }
2793 wifi_datfileWrite(dat_file, params, 2);
2794 }
2795
2796 return RETURN_OK;
2797}
2798
2799INT wifi_getATMEnable(BOOL *output_enable)
2800{
2801 int max_radio_num = 0;
2802 int radio_idx = 0;
2803 struct vow_info vow_info;
2804 struct vow_info get_vow_info;
2805 struct mtk_nl80211_cb_data cb_data;
2806
2807 if (output_enable == NULL)
2808 return RETURN_ERR;
2809
2810 wifi_getMaxRadioNumber(&max_radio_num);
2811
2812 *output_enable = FALSE;
2813
2814 memset(&vow_info, 0, sizeof(struct vow_info));
2815
2816 cb_data.out_buf = (char *)&vow_info;
2817 cb_data.out_len = sizeof(struct vow_info);
2818
2819 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2820 if (mtk_wifi_set_air_time_management
2821 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2822 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2823 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2824 return RETURN_ERR;
2825 }
2826
2827 if (vow_info.atf_en == TRUE || vow_info.bw_en == TRUE) {
2828 *output_enable = TRUE;
2829 break;
2830 }
2831 }
2832
2833 return RETURN_OK;
2834}
2835
2836UINT apidx_to_group(INT apIndex)
2837{
2838 int max_radio_num = 0;
2839 unsigned int group = 0;
2840
2841 wifi_getMaxRadioNumber(&max_radio_num);
developerc14d83a2023-06-29 20:09:42 +08002842 if (max_radio_num == 0) {
2843 wifi_debug(DEBUG_ERROR, "invalid max radio num\n");
2844 return 0;
2845 }
developer6e578302023-06-21 10:11:16 +08002846 group = apIndex / max_radio_num + 5 * (apIndex % max_radio_num);
2847
2848 return group;
2849}
2850
2851INT wifi_setApATMAirTimePercent(INT apIndex, UINT ap_AirTimePercent)
2852{
2853 struct vow_group_en_param atc_en_param;
2854 struct vow_ratio_param radio_param;
2855 unsigned int group = 0;
2856 //BOOL ATM_enable = FALSE;
2857
2858 if (ap_AirTimePercent < 5 || ap_AirTimePercent > 100) {
2859 wifi_debug(DEBUG_ERROR, "invalid ait time percent!\n");
2860 return RETURN_ERR;
2861 }
2862
2863 /* mt7990 support 15 group now*/
2864 group = apidx_to_group(apIndex);
2865
2866 if (group > 15) {
2867 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2868 return RETURN_ERR;
2869 }
2870
2871 atc_en_param.group = group;
2872 atc_en_param.en = 1;
2873 if (mtk_wifi_set_air_time_management
2874 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO,
2875 NULL, (char *)&atc_en_param, sizeof(struct vow_group_en_param), NULL)!= RETURN_OK) {
2876 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO cmd fails\n");
2877 return RETURN_ERR;
2878 }
2879
2880 radio_param.group = group;
2881 radio_param.ratio = ap_AirTimePercent;
2882 if (mtk_wifi_set_air_time_management
2883 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO,
2884 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2885 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO cmd fails\n");
2886 return RETURN_ERR;
2887 }
2888
2889 if (mtk_wifi_set_air_time_management
2890 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO,
2891 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2892 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO cmd fails\n");
2893 return RETURN_ERR;
2894 }
2895
2896 return RETURN_OK;
2897}
2898
2899INT wifi_getApATMAirTimePercent(INT apIndex, UINT *output_ap_AirTimePercent)
2900{
2901 unsigned int group = 0;
2902 struct vow_info get_vow_info, vow_info;
2903 struct mtk_nl80211_cb_data cb_data;
2904
2905 if (output_ap_AirTimePercent == NULL)
2906 return RETURN_ERR;
2907
2908 group = apidx_to_group(apIndex);
2909 if (group > 15) {
2910 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2911 return RETURN_ERR;
2912 }
2913
2914 memset(&vow_info, 0, sizeof(struct vow_info));
2915 memset(&get_vow_info, 0, sizeof(struct vow_info));
2916
2917 cb_data.out_buf = (char *)&vow_info;
2918 cb_data.out_len = sizeof(struct vow_info);
2919
2920 get_vow_info.group = group;
2921
2922 if (mtk_wifi_set_air_time_management
2923 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2924 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2925 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2926 return RETURN_ERR;
2927 }
2928
2929 *output_ap_AirTimePercent = vow_info.ratio;
2930
2931 return RETURN_ERR;
2932}
2933
developerd14dff12023-06-28 22:47:44 +08002934INT wifi_getApATMSta(INT apIndex, UCHAR *output_sta_MAC_ATM_array, UINT buf_size)
2935{
2936 ULONG dev_num = 0;
2937 struct vow_info vow_info;
2938 struct vow_info get_vow_info;
2939 struct mtk_nl80211_cb_data cb_data;
2940 unsigned int percent;
2941 char assocArray[MAX_BUF_SIZE] = {0};
2942 char *mac = NULL;
2943 unsigned char output_len = 0;
2944 int res;
2945 char buf[MAX_BUF_SIZE] = {0};
2946
2947 memset(&vow_info, 0, sizeof(struct vow_info));
2948 memset(&get_vow_info, 0, sizeof(struct vow_info));
2949
2950 cb_data.out_buf = (char *)&vow_info;
2951 cb_data.out_len = sizeof(struct vow_info);
2952
2953 if (mtk_wifi_set_air_time_management
2954 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2955 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2956 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2957 return RETURN_ERR;
2958 }
2959
2960 if (vow_info.atf_en == FALSE) {
2961 wifi_debug(DEBUG_ERROR, "ATF disable!\n");
2962 return RETURN_ERR;
2963 }
2964
2965 if (wifi_getApNumDevicesAssociated(apIndex, &dev_num) != RETURN_OK) {
2966 wifi_debug(DEBUG_ERROR, "get sta num fail!\n");
2967 return RETURN_ERR;;
2968 }
2969
2970 percent = 100 / dev_num;
2971
2972 if (wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray)) != RETURN_OK){
2973 wifi_debug(DEBUG_ERROR, "get sta mac fail!\n");
2974 return RETURN_ERR;;
2975 }
2976
2977 memset(output_sta_MAC_ATM_array, 0, MAX_BUF_SIZE);
2978
2979 mac = strtok(assocArray, "\n");
2980 while (mac != NULL) {
2981 if (strlen(mac) >= 17) {
2982 res = snprintf(buf, sizeof(buf), "%s %d|", mac, percent);
2983
2984 if (os_snprintf_error(sizeof(buf), res)) {
2985 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2986 return RETURN_ERR;
2987 }
2988
2989 if (output_len + strlen(buf) > buf_size)
2990 break;
2991
2992 strncat((char *)output_sta_MAC_ATM_array, buf, strlen(buf));
2993
2994 output_len += strlen(buf);
2995 }
2996
2997 mac = strtok(NULL, "\n");
2998 }
2999
3000 /* Remove the last | */
3001 if (strlen((char *)output_sta_MAC_ATM_array) != 0)
3002 output_sta_MAC_ATM_array[strlen((char *)output_sta_MAC_ATM_array)-1] = '\0';
3003
3004 return RETURN_OK;
3005}
3006
3007INT wifi_setApATMSta(INT apIndex, UCHAR *sta_MAC_ATM_array, UINT ap_AirTimePercent)
3008{
3009 return RETURN_ERR;
3010}
developer9ce44382023-06-28 11:09:37 +08003011
developer72fb0bb2023-01-11 09:46:29 +08003012//Get the maximum PHY bit rate supported by this interface. eg: "216.7 Mb/s", "1.3 Gb/s"
3013//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
3014INT wifi_getRadioMaxBitRate(INT radioIndex, CHAR *output_string) //RDKB
3015{
developera3511852023-06-14 14:12:59 +08003016 // The formula to coculate bit rate is "Subcarriers * Modulation * Coding rate * Spatial stream / (Data interval + Guard interval)"
3017 // For max bit rate, we should always choose the best MCS
3018 char mode[64] = {0};
3019 char channel_bandwidth_str[64] = {0};
3020 UINT mode_map = 0;
3021 UINT num_subcarrier = 0;
3022 UINT code_bits = 0;
3023 float code_rate = 0; // use max code rate
3024 int NSS = 0;
3025 UINT Symbol_duration = 0;
3026 UINT GI_duration = 0;
3027 wifi_guard_interval_t gi = wifi_guard_interval_auto;
3028 BOOL enable = FALSE;
3029 float bit_rate = 0;
developere40952c2023-06-15 18:46:43 +08003030 int ant_bitmap = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003031
developera3511852023-06-14 14:12:59 +08003032 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3033 if (NULL == output_string)
3034 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003035
developera3511852023-06-14 14:12:59 +08003036 wifi_getRadioEnable(radioIndex, &enable);
3037 if (enable == FALSE) {
developere40952c2023-06-15 18:46:43 +08003038 res = snprintf(output_string, 64, "0 Mb/s");
3039 if (os_snprintf_error(64, res)) {
3040 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3041 return RETURN_ERR;
3042 }
developera3511852023-06-14 14:12:59 +08003043 return RETURN_OK;
3044 }
developer72fb0bb2023-01-11 09:46:29 +08003045
developera3511852023-06-14 14:12:59 +08003046 if (wifi_getRadioMode(radioIndex, mode, &mode_map) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08003047 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +08003048 return RETURN_ERR;
3049 }
developer72fb0bb2023-01-11 09:46:29 +08003050
developera3511852023-06-14 14:12:59 +08003051 if (wifi_getGuardInterval(radioIndex, &gi) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08003052 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +08003053 return RETURN_ERR;
3054 }
developer72fb0bb2023-01-11 09:46:29 +08003055
developera3511852023-06-14 14:12:59 +08003056 if (gi == wifi_guard_interval_3200)
3057 GI_duration = 32;
3058 else if (gi == wifi_guard_interval_1600)
3059 GI_duration = 16;
3060 else if (gi == wifi_guard_interval_800)
3061 GI_duration = 8;
3062 else // auto, 400
3063 GI_duration = 4;
developer72fb0bb2023-01-11 09:46:29 +08003064
developera3511852023-06-14 14:12:59 +08003065 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, channel_bandwidth_str) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08003066 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error\n");
developera3511852023-06-14 14:12:59 +08003067 return RETURN_ERR;
3068 }
developer72fb0bb2023-01-11 09:46:29 +08003069
developera3511852023-06-14 14:12:59 +08003070 if (strstr(channel_bandwidth_str, "80+80") != NULL)
developer32f2a182023-06-27 19:50:41 +08003071 memcpy(channel_bandwidth_str, "160", strlen("160"));
developer72fb0bb2023-01-11 09:46:29 +08003072
developera3511852023-06-14 14:12:59 +08003073 if (mode_map & WIFI_MODE_AX) {
3074 if (strstr(channel_bandwidth_str, "160") != NULL)
3075 num_subcarrier = 1960;
3076 else if (strstr(channel_bandwidth_str, "80") != NULL)
3077 num_subcarrier = 980;
3078 else if (strstr(channel_bandwidth_str, "40") != NULL)
3079 num_subcarrier = 468;
3080 else if (strstr(channel_bandwidth_str, "20") != NULL)
3081 num_subcarrier = 234;
3082 code_bits = 10;
3083 code_rate = (float)5/6;
3084 Symbol_duration = 128;
3085 GI_duration = 8;/*HE no GI 400ns*/
3086 } else if (mode_map & WIFI_MODE_AC) {
3087 if (strstr(channel_bandwidth_str, "160") != NULL)
3088 num_subcarrier = 468;
3089 else if (strstr(channel_bandwidth_str, "80") != NULL)
3090 num_subcarrier = 234;
3091 else if (strstr(channel_bandwidth_str, "40") != NULL)
3092 num_subcarrier = 108;
3093 else if (strstr(channel_bandwidth_str, "20") != NULL)
3094 num_subcarrier = 52;
3095 code_bits = 8;
3096 code_rate = (float)5/6;
3097 Symbol_duration = 32;
3098 } else if (mode_map & WIFI_MODE_N) {
3099 if (strstr(channel_bandwidth_str, "160") != NULL)
3100 num_subcarrier = 468;
3101 else if (strstr(channel_bandwidth_str, "80") != NULL)
3102 num_subcarrier = 234;
3103 else if (strstr(channel_bandwidth_str, "40") != NULL)
3104 num_subcarrier = 108;
3105 else if (strstr(channel_bandwidth_str, "20") != NULL)
3106 num_subcarrier = 52;
3107 code_bits = 6;
3108 code_rate = (float)3/4;
3109 Symbol_duration = 32;
3110 } else if ((mode_map & WIFI_MODE_G || mode_map & WIFI_MODE_B) || mode_map & WIFI_MODE_A) {
3111 // mode b must run with mode g, so we output mode g bitrate in 2.4 G.
developere40952c2023-06-15 18:46:43 +08003112 res = snprintf(output_string, 64, "65 Mb/s");
3113 if (os_snprintf_error(64, res)) {
3114 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3115 return RETURN_ERR;
3116 }
developera3511852023-06-14 14:12:59 +08003117 return RETURN_OK;
3118 } else {
developere40952c2023-06-15 18:46:43 +08003119 res = snprintf(output_string, 64, "0 Mb/s");
3120 if (os_snprintf_error(64, res)) {
3121 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3122 return RETURN_ERR;
3123 }
developera3511852023-06-14 14:12:59 +08003124 return RETURN_OK;
3125 }
developer72fb0bb2023-01-11 09:46:29 +08003126
developera3511852023-06-14 14:12:59 +08003127 // Spatial streams
3128 if (wifi_getRadioTxChainMask(radioIndex, &ant_bitmap) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08003129 wifi_debug(DEBUG_ERROR, "wifi_getRadioTxChainMask return error\n");
developera3511852023-06-14 14:12:59 +08003130 return RETURN_ERR;
3131 }
3132 for (; ant_bitmap > 0; ant_bitmap >>= 1)
3133 NSS += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08003134
developera3511852023-06-14 14:12:59 +08003135 // multiple 10 is to align duration unit (0.1 us)
3136 bit_rate = (num_subcarrier * code_bits * code_rate * NSS) / (Symbol_duration + GI_duration) * 10;
developere40952c2023-06-15 18:46:43 +08003137 res = snprintf(output_string, 64, "%.1f Mb/s", bit_rate);
3138 if (os_snprintf_error(64, res)) {
3139 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3140 return RETURN_ERR;
3141 }
developera3511852023-06-14 14:12:59 +08003142 WIFI_ENTRY_EXIT_DEBUG("%s:num_subcarrier=%d, code_bits=%d, code_rate=%.3f, nss=%d, symbol time=%u, %.1f Mb/s\n",
3143 __func__, num_subcarrier, code_bits, code_rate, NSS, Symbol_duration + GI_duration, bit_rate);
3144 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003145
developera3511852023-06-14 14:12:59 +08003146 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003147}
developer72fb0bb2023-01-11 09:46:29 +08003148
3149//Get Supported frequency bands at which the radio can operate. eg: "2.4GHz,5GHz"
3150//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
3151INT wifi_getRadioSupportedFrequencyBands(INT radioIndex, CHAR *output_string) //RDKB
3152{
developera3511852023-06-14 14:12:59 +08003153 wifi_band band = band_invalid;
developer72fb0bb2023-01-11 09:46:29 +08003154
developera3511852023-06-14 14:12:59 +08003155 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3156 if (NULL == output_string)
3157 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003158
developera3511852023-06-14 14:12:59 +08003159 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003160
developera3511852023-06-14 14:12:59 +08003161 memset(output_string, 0, 10);
3162 if (band == band_2_4)
developer32f2a182023-06-27 19:50:41 +08003163 memcpy(output_string, "2.4GHz", strlen("2.4GHz"));
developera3511852023-06-14 14:12:59 +08003164 else if (band == band_5)
developer32f2a182023-06-27 19:50:41 +08003165 memcpy(output_string, "5GHz", strlen("5GHz"));
developera3511852023-06-14 14:12:59 +08003166 else if (band == band_6)
developer32f2a182023-06-27 19:50:41 +08003167 memcpy(output_string, "6GHz", strlen("6GHz"));
developera3511852023-06-14 14:12:59 +08003168 else
3169 return RETURN_ERR;
3170 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003171
developera3511852023-06-14 14:12:59 +08003172 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003173}
3174
3175//Get the frequency band at which the radio is operating, eg: "2.4GHz"
3176//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
3177INT wifi_getRadioOperatingFrequencyBand(INT radioIndex, CHAR *output_string) //Tr181
3178{
developera3511852023-06-14 14:12:59 +08003179 wifi_band band = band_invalid;
developer9ce44382023-06-28 11:09:37 +08003180 int res = -1;
developere40952c2023-06-15 18:46:43 +08003181
developera3511852023-06-14 14:12:59 +08003182 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3183 if (NULL == output_string)
3184 return RETURN_ERR;
3185 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003186
developera3511852023-06-14 14:12:59 +08003187 if (band == band_2_4)
developere40952c2023-06-15 18:46:43 +08003188 res = snprintf(output_string, 64, "2.4GHz");
developera3511852023-06-14 14:12:59 +08003189 else if (band == band_5)
developere40952c2023-06-15 18:46:43 +08003190 res = snprintf(output_string, 64, "5GHz");
developera3511852023-06-14 14:12:59 +08003191 else if (band == band_6)
developere40952c2023-06-15 18:46:43 +08003192 res = snprintf(output_string, 64, "6GHz");
developer72fb0bb2023-01-11 09:46:29 +08003193
developere40952c2023-06-15 18:46:43 +08003194 if (os_snprintf_error(64, res)) {
3195 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3196 return RETURN_ERR;
3197 }
developera3511852023-06-14 14:12:59 +08003198 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003199
developera3511852023-06-14 14:12:59 +08003200 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003201}
3202
3203//Get the Supported Radio Mode. eg: "b,g,n"; "n,ac"
3204//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
3205INT wifi_getRadioSupportedStandards(INT radioIndex, CHAR *output_string) //Tr181
3206{
developera3511852023-06-14 14:12:59 +08003207 char cmd[128]={0};
3208 char buf[128]={0};
3209 char temp_output[128] = {0};
3210 wifi_band band;
developere40952c2023-06-15 18:46:43 +08003211 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003212
developera3511852023-06-14 14:12:59 +08003213 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3214 if (NULL == output_string)
3215 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003216
developera3511852023-06-14 14:12:59 +08003217 band = wifi_index_to_band(radioIndex);
3218 if (band == band_2_4) {
developer32f2a182023-06-27 19:50:41 +08003219 strncat(temp_output, "b,g,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003220 } else if (band == band_5) {
developer32f2a182023-06-27 19:50:41 +08003221 strncat(temp_output, "a,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003222 }
3223 phyId = radio_index_to_phy(radioIndex);
3224 // ht capabilities
developere40952c2023-06-15 18:46:43 +08003225 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep '[^PHY|MAC|VHT].Capabilities' | head -n 1 | cut -d ':' -f2 | sed 's/^.//' | tr -d '\\n'", phyId);
3226 if (os_snprintf_error(sizeof(cmd), res)) {
3227 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3228 return RETURN_ERR;
3229 }
developera3511852023-06-14 14:12:59 +08003230 _syscmd(cmd, buf, sizeof(buf));
3231 if (strlen(buf) >= 4 && strncmp(buf, "0x00", 4) != 0) {
developer32f2a182023-06-27 19:50:41 +08003232 if (strlen(temp_output) >= sizeof(temp_output) - 2)
3233 return RETURN_ERR;
3234 strncat(temp_output, "n,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003235 }
developer72fb0bb2023-01-11 09:46:29 +08003236
developera3511852023-06-14 14:12:59 +08003237 // vht capabilities
3238 if (band == band_5) {
developere40952c2023-06-15 18:46:43 +08003239 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'VHT Capabilities' | cut -d '(' -f2 | cut -c1-10 | tr -d '\\n'", phyId);
3240 if (os_snprintf_error(sizeof(cmd), res)) {
3241 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3242 return RETURN_ERR;
3243 }
3244 _syscmd(cmd, buf, sizeof(buf));
3245 if (strlen(buf) >= 10 && strncmp(buf, "0x00000000", 10) != 0) {
developer32f2a182023-06-27 19:50:41 +08003246 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3247 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08003248 strncat(temp_output, "ac,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003249 }
3250 }
developer72fb0bb2023-01-11 09:46:29 +08003251
developera3511852023-06-14 14:12:59 +08003252 // he capabilities
developere40952c2023-06-15 18:46:43 +08003253 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'HE MAC Capabilities' | head -n 2 | tail -n 1 | cut -d '(' -f2 | cut -c1-6 | tr -d '\\n'", phyId);
3254 if (os_snprintf_error(sizeof(cmd), res)) {
3255 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3256 return RETURN_ERR;
3257 }
developera3511852023-06-14 14:12:59 +08003258 _syscmd(cmd, buf, sizeof(buf));
3259 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003260 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3261 return RETURN_ERR;
3262 strncat(temp_output, "ax,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003263 }
developer72fb0bb2023-01-11 09:46:29 +08003264
developere82c0ca2023-05-10 16:25:35 +08003265 // eht capabilities
developere40952c2023-06-15 18:46:43 +08003266 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'EHT MAC Capabilities' | head -n 2 | tail -n 1 | cut -d '(' -f2 | cut -c1-6 | tr -d '\\n'", phyId);
3267 if (os_snprintf_error(sizeof(cmd), res)) {
3268 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3269 return RETURN_ERR;
3270 }
developera3511852023-06-14 14:12:59 +08003271 _syscmd(cmd, buf, sizeof(buf));
3272 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003273 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3274 return RETURN_ERR;
3275 strncat(temp_output, "be,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003276 }
developere82c0ca2023-05-10 16:25:35 +08003277
developera3511852023-06-14 14:12:59 +08003278 // Remove the last comma
3279 if (strlen(temp_output) != 0)
3280 temp_output[strlen(temp_output)-1] = '\0';
3281 strncpy(output_string, temp_output, strlen(temp_output));
3282 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3283 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003284}
3285
3286//Get the radio operating mode, and pure mode flag. eg: "ac"
3287//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
3288INT wifi_getRadioStandard(INT radioIndex, CHAR *output_string, BOOL *gOnly, BOOL *nOnly, BOOL *acOnly) //RDKB
3289{
developere40952c2023-06-15 18:46:43 +08003290 int res;
3291
developera3511852023-06-14 14:12:59 +08003292 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3293 if (NULL == output_string)
3294 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003295
developera3511852023-06-14 14:12:59 +08003296 if (radioIndex == 0) {
developere40952c2023-06-15 18:46:43 +08003297 res = snprintf(output_string, 64, "n"); //"ht" needs to be translated to "n" or others
3298 if (os_snprintf_error(64, res)) {
3299 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3300 return RETURN_ERR;
3301 }
developera3511852023-06-14 14:12:59 +08003302 *gOnly = FALSE;
3303 *nOnly = TRUE;
3304 *acOnly = FALSE;
3305 } else {
developere40952c2023-06-15 18:46:43 +08003306 res = snprintf(output_string, 64, "ac"); //"vht" needs to be translated to "ac"
3307 if (os_snprintf_error(64, res)) {
3308 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3309 return RETURN_ERR;
3310 }
developera3511852023-06-14 14:12:59 +08003311 *gOnly = FALSE;
3312 *nOnly = FALSE;
3313 *acOnly = FALSE;
3314 }
3315 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003316
developera3511852023-06-14 14:12:59 +08003317 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003318}
3319
developer0f10c772023-05-16 21:43:39 +08003320enum WIFI_MODE {
3321 WMODE_INVALID = 0,
3322 WMODE_A = 1 << 0,
3323 WMODE_B = 1 << 1,
3324 WMODE_G = 1 << 2,
3325 WMODE_GN = 1 << 3,
3326 WMODE_AN = 1 << 4,
3327 WMODE_AC = 1 << 5,
3328 WMODE_AX_24G = 1 << 6,
3329 WMODE_AX_5G = 1 << 7,
3330 WMODE_AX_6G = 1 << 8,
3331 WMODE_BE_24G = 1 << 9,
3332 WMODE_BE_5G = 1 << 10,
3333 WMODE_BE_6G = 1 << 11,
3334 /*
3335 * total types of supported wireless mode,
3336 * add this value once yow add new type
3337 */
3338 WMODE_COMP = 12,
3339};
3340
3341#define RADIO_MODE_LEN 32
developerfead3972023-05-25 20:15:02 +08003342
3343int get_radio_mode_handler(struct nl_msg *msg, void *cb)
developer72fb0bb2023-01-11 09:46:29 +08003344{
developerfead3972023-05-25 20:15:02 +08003345 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3346 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08003347 struct genlmsghdr *gnlh;
developerfead3972023-05-25 20:15:02 +08003348 unsigned int *phymode;
3349 int err = 0;
3350 struct mtk_nl80211_cb_data *cb_data = cb;
developer72fb0bb2023-01-11 09:46:29 +08003351
developerfead3972023-05-25 20:15:02 +08003352 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08003353 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developerfead3972023-05-25 20:15:02 +08003354 return NL_SKIP;
3355 }
developerc14d83a2023-06-29 20:09:42 +08003356 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer72fb0bb2023-01-11 09:46:29 +08003357
developerfead3972023-05-25 20:15:02 +08003358 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3359 genlmsg_attrlen(gnlh, 0), NULL);
3360 if (err < 0) {
3361 wifi_debug(DEBUG_ERROR, "nla_parse radio nl80211 msg fails,error.\n");
3362 return NL_SKIP;
3363 }
developer0f10c772023-05-16 21:43:39 +08003364
developerfead3972023-05-25 20:15:02 +08003365 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3366 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
3367 tb[NL80211_ATTR_VENDOR_DATA], NULL);
3368 if (err < 0)
3369 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08003370
developerfead3972023-05-25 20:15:02 +08003371 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]) {
3372 phymode = (unsigned int *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]);
3373
3374 memset(cb_data->out_buf, 0, cb_data->out_len);
3375 memmove(cb_data->out_buf, phymode, sizeof(unsigned int));
3376 }
3377 } else
3378 wifi_debug(DEBUG_ERROR, "No Stats from driver.\n");
3379
3380 return NL_OK;
3381}
developer0f10c772023-05-16 21:43:39 +08003382
developerfead3972023-05-25 20:15:02 +08003383void phymode_to_puremode(INT radioIndex, CHAR *output_string, UINT *pureMode, UINT phymode)
3384{
3385 wifi_band band;
3386 unsigned char radio_mode_tem_len;
developere40952c2023-06-15 18:46:43 +08003387 int res;
developerfead3972023-05-25 20:15:02 +08003388
3389 band = wifi_index_to_band(radioIndex);
developer0f10c772023-05-16 21:43:39 +08003390 // puremode is a bit map
developera3511852023-06-14 14:12:59 +08003391 *pureMode = 0;
developer0f10c772023-05-16 21:43:39 +08003392 memset(output_string, 0, RADIO_MODE_LEN);
3393
3394 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3395
3396 switch (band) {
3397 case band_2_4:
3398 if (phymode & WMODE_B) {
developere40952c2023-06-15 18:46:43 +08003399 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "b,");
3400 if (os_snprintf_error(radio_mode_tem_len, res)) {
3401 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3402 return;
3403 }
developer0f10c772023-05-16 21:43:39 +08003404 *pureMode |= WIFI_MODE_B;
3405 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3406 }
3407 if (phymode & WMODE_G) {
developere40952c2023-06-15 18:46:43 +08003408 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "g,");
3409 if (os_snprintf_error(radio_mode_tem_len, res)) {
3410 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3411 return;
3412 }
developer0f10c772023-05-16 21:43:39 +08003413 *pureMode |= WIFI_MODE_G;
3414 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3415 }
3416 if (phymode & WMODE_GN) {
developere40952c2023-06-15 18:46:43 +08003417 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3418 if (os_snprintf_error(radio_mode_tem_len, res)) {
3419 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3420 return;
3421 }
developer0f10c772023-05-16 21:43:39 +08003422 *pureMode |= WIFI_MODE_N;
3423 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3424 }
3425 if (phymode & WMODE_AX_24G) {
developere40952c2023-06-15 18:46:43 +08003426 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3427 if (os_snprintf_error(radio_mode_tem_len, res)) {
3428 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3429 return;
3430 }
developer0f10c772023-05-16 21:43:39 +08003431 *pureMode |= WIFI_MODE_AX;
3432 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3433 }
3434 if (phymode & WMODE_BE_24G) {
developere40952c2023-06-15 18:46:43 +08003435 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3436 if (os_snprintf_error(radio_mode_tem_len, res)) {
3437 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3438 return;
3439 }
developer0f10c772023-05-16 21:43:39 +08003440 *pureMode |= WIFI_MODE_BE;
3441 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3442 }
3443 break;
3444 case band_5:
3445 if (phymode & WMODE_A) {
developere40952c2023-06-15 18:46:43 +08003446 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "a,");
3447 if (os_snprintf_error(radio_mode_tem_len, res)) {
3448 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3449 return;
3450 }
developer0f10c772023-05-16 21:43:39 +08003451 *pureMode |= WIFI_MODE_A;
3452 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3453 }
3454 if (phymode & WMODE_AN) {
developere40952c2023-06-15 18:46:43 +08003455 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3456 if (os_snprintf_error(radio_mode_tem_len, res)) {
3457 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3458 return;
3459 }
developer0f10c772023-05-16 21:43:39 +08003460 *pureMode |= WIFI_MODE_N;
3461 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3462 }
3463 if (phymode & WMODE_AC) {
developere40952c2023-06-15 18:46:43 +08003464 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ac,");
3465 if (os_snprintf_error(radio_mode_tem_len, res)) {
3466 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3467 return;
3468 }
developer0f10c772023-05-16 21:43:39 +08003469 *pureMode |= WIFI_MODE_AC;
3470 }
3471 if (phymode & WMODE_AX_5G) {
developere40952c2023-06-15 18:46:43 +08003472 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3473 if (os_snprintf_error(radio_mode_tem_len, res)) {
3474 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3475 return;
3476 }
developer0f10c772023-05-16 21:43:39 +08003477 *pureMode |= WIFI_MODE_AX;
3478 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3479 }
3480 if (phymode & WMODE_BE_5G) {
developere40952c2023-06-15 18:46:43 +08003481 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3482 if (os_snprintf_error(radio_mode_tem_len, res)) {
3483 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3484 return;
3485 }
developer0f10c772023-05-16 21:43:39 +08003486 *pureMode |= WIFI_MODE_BE;
3487 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3488 }
3489 break;
3490 case band_6:
3491 if (phymode & WMODE_AX_6G) {
developere40952c2023-06-15 18:46:43 +08003492 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3493 if (os_snprintf_error(radio_mode_tem_len, res)) {
3494 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3495 return;
3496 }
developer0f10c772023-05-16 21:43:39 +08003497 *pureMode |= WIFI_MODE_AX;
3498 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3499 }
3500 if (phymode & WMODE_BE_6G) {
developere40952c2023-06-15 18:46:43 +08003501 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3502 if (os_snprintf_error(radio_mode_tem_len, res)) {
3503 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3504 return;
3505 }
developer0f10c772023-05-16 21:43:39 +08003506 *pureMode |= WIFI_MODE_BE;
3507 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3508 }
3509 break;
3510 default:
developer86035662023-06-28 19:21:12 +08003511 wifi_debug(DEBUG_ERROR, "%s band_idx invalid\n", __func__);
developer0f10c772023-05-16 21:43:39 +08003512 break;
3513 }
3514
3515 /* Remove the last comma */
3516 if (strlen(output_string) != 0)
developera3511852023-06-14 14:12:59 +08003517 output_string[strlen(output_string)-1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +08003518
developerfead3972023-05-25 20:15:02 +08003519}
3520
3521INT wifi_getRadioMode(INT radioIndex, CHAR *output_string, UINT *pureMode)
3522{
3523 unsigned int phymode;
3524 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003525 int ret = -1;
3526 unsigned int if_idx = 0;
3527 struct unl unl_ins;
3528 struct nl_msg *msg = NULL;
3529 struct nlattr * msg_data = NULL;
3530 struct mtk_nl80211_param param;
3531 struct mtk_nl80211_cb_data cb_data;
3532
developera3511852023-06-14 14:12:59 +08003533 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3534 if (NULL == output_string || NULL == pureMode)
developerdaf24792023-06-06 11:40:04 +08003535 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003536
3537 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
3538 return RETURN_ERR;
3539
3540 if_idx = if_nametoindex(interface_name);
3541 if (!if_idx) {
3542 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
3543 return RETURN_ERR;
3544 }
3545 /*init mtk nl80211 vendor cmd*/
3546 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
3547 param.if_type = NL80211_ATTR_IFINDEX;
3548 param.if_idx = if_idx;
3549
3550 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3551 if (ret) {
3552 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3553 return RETURN_ERR;
3554 }
3555
3556 /*add mtk vendor cmd data*/
3557 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE, 0)) {
3558 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_WMODE attribute error\n");
3559 nlmsg_free(msg);
3560 goto err;
3561 }
3562
3563 /*send mtk nl80211 vendor msg*/
3564 cb_data.out_buf = (char *)&phymode;
3565 cb_data.out_len = sizeof(unsigned int);
3566
3567 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_radio_mode_handler, &cb_data);
3568
3569 if (ret) {
3570 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3571 goto err;
3572 }
3573 /*deinit mtk nl80211 vendor msg*/
3574 mtk_nl80211_deint(&unl_ins);
3575
3576 phymode_to_puremode(radioIndex, output_string, pureMode, phymode);
developer6e578302023-06-21 10:11:16 +08003577 wifi_debug(DEBUG_INFO,"send cmd success\n");
developerfead3972023-05-25 20:15:02 +08003578
developera3511852023-06-14 14:12:59 +08003579 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3580 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003581err:
3582 mtk_nl80211_deint(&unl_ins);
3583 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
3584 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003585}
3586
3587// Set the radio operating mode, and pure mode flag.
3588INT wifi_setRadioChannelMode(INT radioIndex, CHAR *channelMode, BOOL gOnlyFlag, BOOL nOnlyFlag, BOOL acOnlyFlag) //RDKB
3589{
developera3511852023-06-14 14:12:59 +08003590 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%s_%d_%d:%d\n",__func__,channelMode,nOnlyFlag,gOnlyFlag,__LINE__);
3591 if (strcmp (channelMode,"11A") == 0)
3592 {
3593 writeBandWidth(radioIndex,"20MHz");
3594 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3595 printf("\nChannel Mode is 802.11a (5GHz)\n");
3596 }
3597 else if (strcmp (channelMode,"11NAHT20") == 0)
3598 {
3599 writeBandWidth(radioIndex,"20MHz");
3600 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3601 printf("\nChannel Mode is 802.11n-20MHz(5GHz)\n");
3602 }
3603 else if (strcmp (channelMode,"11NAHT40PLUS") == 0)
3604 {
3605 writeBandWidth(radioIndex,"40MHz");
3606 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3607 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3608 }
3609 else if (strcmp (channelMode,"11NAHT40MINUS") == 0)
3610 {
3611 writeBandWidth(radioIndex,"40MHz");
3612 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3613 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3614 }
3615 else if (strcmp (channelMode,"11ACVHT20") == 0)
3616 {
3617 writeBandWidth(radioIndex,"20MHz");
3618 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3619 printf("\nChannel Mode is 802.11ac-20MHz(5GHz)\n");
3620 }
3621 else if (strcmp (channelMode,"11ACVHT40PLUS") == 0)
3622 {
3623 writeBandWidth(radioIndex,"40MHz");
3624 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3625 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3626 }
3627 else if (strcmp (channelMode,"11ACVHT40MINUS") == 0)
3628 {
3629 writeBandWidth(radioIndex,"40MHz");
3630 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3631 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3632 }
3633 else if (strcmp (channelMode,"11ACVHT80") == 0)
3634 {
3635 wifi_setRadioOperatingChannelBandwidth(radioIndex,"80MHz");
3636 printf("\nChannel Mode is 802.11ac-80MHz(5GHz)\n");
3637 }
3638 else if (strcmp (channelMode,"11ACVHT160") == 0)
3639 {
3640 wifi_setRadioOperatingChannelBandwidth(radioIndex,"160MHz");
3641 printf("\nChannel Mode is 802.11ac-160MHz(5GHz)\n");
3642 }
3643 else if (strcmp (channelMode,"11B") == 0)
3644 {
3645 writeBandWidth(radioIndex,"20MHz");
3646 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3647 printf("\nChannel Mode is 802.11b(2.4GHz)\n");
3648 }
3649 else if (strcmp (channelMode,"11G") == 0)
3650 {
3651 writeBandWidth(radioIndex,"20MHz");
3652 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3653 printf("\nChannel Mode is 802.11g(2.4GHz)\n");
3654 }
3655 else if (strcmp (channelMode,"11NGHT20") == 0)
3656 {
3657 writeBandWidth(radioIndex,"20MHz");
3658 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3659 printf("\nChannel Mode is 802.11n-20MHz(2.4GHz)\n");
3660 }
3661 else if (strcmp (channelMode,"11NGHT40PLUS") == 0)
3662 {
3663 writeBandWidth(radioIndex,"40MHz");
3664 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3665 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3666 }
3667 else if (strcmp (channelMode,"11NGHT40MINUS") == 0)
3668 {
3669 writeBandWidth(radioIndex,"40MHz");
3670 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3671 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3672 }
3673 else
3674 {
3675 return RETURN_ERR;
3676 }
3677 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003678
developera3511852023-06-14 14:12:59 +08003679 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003680}
3681
developer0f10c772023-05-16 21:43:39 +08003682typedef enum _RT_802_11_PHY_MODE {
3683 PHY_11BG_MIXED = 0,
3684 PHY_11B = 1,
3685 PHY_11A = 2,
3686 PHY_11ABG_MIXED = 3,
3687 PHY_11G = 4,
3688 PHY_11ABGN_MIXED = 5, /* both band 5 */
developera3511852023-06-14 14:12:59 +08003689 PHY_11N_2_4G = 6, /* 11n-only with 2.4G band 6 */
3690 PHY_11GN_MIXED = 7, /* 2.4G band 7 */
3691 PHY_11AN_MIXED = 8, /* 5G band 8 */
3692 PHY_11BGN_MIXED = 9, /* if check 802.11b. 9 */
3693 PHY_11AGN_MIXED = 10, /* if check 802.11b. 10 */
3694 PHY_11N_5G = 11, /* 11n-only with 5G band 11 */
developer0f10c772023-05-16 21:43:39 +08003695 PHY_11VHT_N_ABG_MIXED = 12, /* 12 -> AC/A/AN/B/G/GN mixed */
3696 PHY_11VHT_N_AG_MIXED = 13, /* 13 -> AC/A/AN/G/GN mixed */
3697 PHY_11VHT_N_A_MIXED = 14, /* 14 -> AC/AN/A mixed in 5G band */
3698 PHY_11VHT_N_MIXED = 15, /* 15 -> AC/AN mixed in 5G band */
3699 PHY_11AX_24G = 16,
3700 PHY_11AX_5G = 17,
3701 PHY_11AX_6G = 18,
3702 PHY_11AX_24G_6G = 19,
3703 PHY_11AX_5G_6G = 20,
3704 PHY_11AX_24G_5G_6G = 21,
3705 PHY_11BE_24G = 22,
3706 PHY_11BE_5G = 23,
3707 PHY_11BE_6G = 24,
3708 PHY_11BE_24G_6G = 25,
3709 PHY_11BE_5G_6G = 26,
3710 PHY_11BE_24G_5G_6G = 27,
3711 PHY_MODE_MAX,
3712} RT_802_11_PHY_MODE;
3713
3714unsigned int puremode_to_wireless_mode(INT radioIndex, UINT pureMode)
3715{
3716 int band_idx = 0;
developerfead3972023-05-25 20:15:02 +08003717 unsigned char wireless_mode = PHY_MODE_MAX;
developer0f10c772023-05-16 21:43:39 +08003718
3719 band_idx = radio_index_to_band(radioIndex);
3720
3721 switch (band_idx) {
3722 case band_2_4:
3723 if (pureMode == (WIFI_MODE_G | WIFI_MODE_N))
3724 wireless_mode = PHY_11GN_MIXED;
3725 if (pureMode == (WIFI_MODE_B | WIFI_MODE_G | WIFI_MODE_N))
3726 wireless_mode = PHY_11BGN_MIXED;
3727 if (pureMode & WIFI_MODE_AX)
3728 wireless_mode = PHY_11AX_24G;
3729 if (pureMode & WIFI_MODE_BE)
3730 wireless_mode = PHY_11BE_24G;
3731 break;
3732 case band_5:
3733 if (pureMode == WIFI_MODE_N)
3734 wireless_mode = PHY_11N_5G;
3735 if ((pureMode == WIFI_MODE_AC) || (pureMode == (WIFI_MODE_N | WIFI_MODE_AC)))
3736 wireless_mode = PHY_11VHT_N_MIXED;
3737 if (pureMode == (WIFI_MODE_A | WIFI_MODE_N | WIFI_MODE_AC))
3738 wireless_mode = PHY_11VHT_N_A_MIXED;
3739 if (pureMode & WIFI_MODE_AX)
3740 wireless_mode = PHY_11AX_5G;
3741 if (pureMode & WIFI_MODE_BE)
3742 wireless_mode = PHY_11BE_5G;
3743 break;
3744 case band_6:
3745 if (pureMode & WIFI_MODE_AX)
3746 wireless_mode = PHY_11AX_6G;
3747 if (pureMode & WIFI_MODE_BE)
3748 wireless_mode = PHY_11BE_6G;
3749 break;
3750 default:
developer37646972023-06-29 10:58:43 +08003751 if (fprintf(stderr, "%s band_idx invalid\n", __func__) < 0)
3752 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developer0f10c772023-05-16 21:43:39 +08003753 break;
3754 }
3755
3756 return wireless_mode;
3757}
3758
developer72fb0bb2023-01-11 09:46:29 +08003759// Set the radio operating mode, and pure mode flag.
3760INT wifi_setRadioMode(INT radioIndex, CHAR *channelMode, UINT pureMode)
3761{
developerfead3972023-05-25 20:15:02 +08003762 unsigned char wireless_mode = PHY_MODE_MAX;
developer69b61b02023-03-07 17:17:44 +08003763
developer0f10c772023-05-16 21:43:39 +08003764 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003765 int ret = -1;
3766 unsigned int if_idx = 0;
3767 struct unl unl_ins;
3768 struct nl_msg *msg = NULL;
3769 struct nlattr * msg_data = NULL;
3770 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +08003771
developer0f10c772023-05-16 21:43:39 +08003772 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, channelMode, pureMode, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003773
developer0f10c772023-05-16 21:43:39 +08003774 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
developer72fb0bb2023-01-11 09:46:29 +08003775
developera3511852023-06-14 14:12:59 +08003776 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003777 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003778 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003779 }
developer72fb0bb2023-01-11 09:46:29 +08003780
developer0f10c772023-05-16 21:43:39 +08003781 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08003782 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003783
3784 if_idx = if_nametoindex(interface_name);
3785 if (!if_idx) {
3786 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", interface_name);
3787 return RETURN_ERR;
3788 }
3789 /*init mtk nl80211 vendor cmd*/
3790 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
3791 param.if_type = NL80211_ATTR_IFINDEX;
3792 param.if_idx = if_idx;
3793
3794 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3795 if (ret) {
3796 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3797 return RETURN_ERR;
3798 }
3799
3800 /*add mtk vendor cmd data*/
3801 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_WIRELESS_MODE, wireless_mode)) {
3802 wifi_debug(DEBUG_ERROR, "Nla put AP_WIRELESS_MODE attribute error\n");
3803 nlmsg_free(msg);
3804 goto err;
3805 }
3806 /*send mtk nl80211 vendor msg*/
3807 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
3808 if (ret) {
3809 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3810 goto err;
3811 }
3812 /*deinit mtk nl80211 vendor msg*/
3813 mtk_nl80211_deint(&unl_ins);
3814 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08003815
developera3511852023-06-14 14:12:59 +08003816 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer0f10c772023-05-16 21:43:39 +08003817
developera3511852023-06-14 14:12:59 +08003818 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003819err:
3820 mtk_nl80211_deint(&unl_ins);
3821 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
3822 return RETURN_ERR;
developer0f10c772023-05-16 21:43:39 +08003823}
3824
3825INT wifi_setRadioMode_by_dat(INT radioIndex, UINT pureMode)
3826{
developerfead3972023-05-25 20:15:02 +08003827 unsigned char wireless_mode = PHY_MODE_MAX;
developera3511852023-06-14 14:12:59 +08003828 char buf[MAX_BUF_SIZE] = {0};
developer0f10c772023-05-16 21:43:39 +08003829 char dat_file[MAX_BUF_SIZE] = {0};
3830 struct params params={0};
developere40952c2023-06-15 18:46:43 +08003831 int res;
developer0f10c772023-05-16 21:43:39 +08003832
3833 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, pureMode, __LINE__);
3834
3835 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
3836
developera3511852023-06-14 14:12:59 +08003837 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003838 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003839 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003840 }
developer0f10c772023-05-16 21:43:39 +08003841
3842 params.name = "WirelessMode";
developere40952c2023-06-15 18:46:43 +08003843 res = snprintf(buf, sizeof(buf), "%d", wireless_mode);
3844 if (os_snprintf_error(sizeof(buf), res)) {
3845 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3846 return RETURN_ERR;
3847 }
3848
developera3511852023-06-14 14:12:59 +08003849 params.value = buf;
developer0f10c772023-05-16 21:43:39 +08003850
developere40952c2023-06-15 18:46:43 +08003851 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
3852 if (os_snprintf_error(sizeof(dat_file), res)) {
3853 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3854 return RETURN_ERR;
3855 }
developera3511852023-06-14 14:12:59 +08003856 wifi_datfileWrite(dat_file, &params, 1);
developer0f10c772023-05-16 21:43:39 +08003857
developera3511852023-06-14 14:12:59 +08003858 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003859
developera3511852023-06-14 14:12:59 +08003860 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003861}
3862
3863INT wifi_setRadioHwMode(INT radioIndex, CHAR *hw_mode) {
3864
developera3511852023-06-14 14:12:59 +08003865 char config_file[64] = {0};
3866 char buf[64] = {0};
3867 struct params params = {0};
3868 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08003869 int res;
developer72fb0bb2023-01-11 09:46:29 +08003870
developera3511852023-06-14 14:12:59 +08003871 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003872
developera3511852023-06-14 14:12:59 +08003873 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003874
developera3511852023-06-14 14:12:59 +08003875 if (strncmp(hw_mode, "a", 1) == 0 && (band != band_5 && band != band_6))
3876 return RETURN_ERR;
3877 else if ((strncmp(hw_mode, "b", 1) == 0 || strncmp(hw_mode, "g", 1) == 0) && band != band_2_4)
3878 return RETURN_ERR;
3879 else if ((strncmp(hw_mode, "a", 1) && strncmp(hw_mode, "b", 1) && strncmp(hw_mode, "g", 1)) || band == band_invalid)
3880 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003881
developer75bd10c2023-06-27 11:34:08 +08003882 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3883 if (os_snprintf_error(sizeof(config_file), res)) {
3884 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3885 return RETURN_ERR;
3886 }
developera3511852023-06-14 14:12:59 +08003887 params.name = "hw_mode";
3888 params.value = hw_mode;
3889 wifi_hostapdWrite(config_file, &params, 1);
3890 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003891
developera3511852023-06-14 14:12:59 +08003892 if (band == band_2_4) {
3893 if (strncmp(hw_mode, "b", 1) == 0) {
3894 wifi_setRadioMode(radioIndex, "20MHz", WIFI_MODE_B);
developere40952c2023-06-15 18:46:43 +08003895 res = snprintf(buf, sizeof(buf), "%s", "1,2,5.5,11");
3896 if (os_snprintf_error(sizeof(buf), res)) {
3897 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3898 return RETURN_ERR;
3899 }
developera3511852023-06-14 14:12:59 +08003900 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003901 res = snprintf(buf, sizeof(buf), "%s", "1,2");
3902 if (os_snprintf_error(sizeof(buf), res)) {
3903 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3904 return RETURN_ERR;
3905 }
developera3511852023-06-14 14:12:59 +08003906 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3907 } else {
3908 // We don't set mode here, because we don't know whitch mode should be set (g, n or ax?).
developer72fb0bb2023-01-11 09:46:29 +08003909
developere40952c2023-06-15 18:46:43 +08003910 res = snprintf(buf, sizeof(buf), "%s", "6,9,12,18,24,36,48,54");
3911 if (os_snprintf_error(sizeof(buf), res)) {
3912 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3913 return RETURN_ERR;
3914 }
developera3511852023-06-14 14:12:59 +08003915 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003916 res = snprintf(buf, sizeof(buf), "%s", "6,12,24");
3917 if (os_snprintf_error(sizeof(buf), res)) {
3918 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3919 return RETURN_ERR;
3920 }
developera3511852023-06-14 14:12:59 +08003921 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3922 }
3923 }
developer72fb0bb2023-01-11 09:46:29 +08003924
developera3511852023-06-14 14:12:59 +08003925 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3926 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003927}
3928
3929INT wifi_setNoscan(INT radioIndex, CHAR *noscan)
3930{
developera3511852023-06-14 14:12:59 +08003931 char config_file[64] = {0};
3932 struct params params = {0};
3933 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08003934 int res;
developer72fb0bb2023-01-11 09:46:29 +08003935
developera3511852023-06-14 14:12:59 +08003936 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003937
developera3511852023-06-14 14:12:59 +08003938 band = wifi_index_to_band(radioIndex);
3939 if (band != band_2_4)
3940 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003941
developer75bd10c2023-06-27 11:34:08 +08003942 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3943 if (os_snprintf_error(sizeof(config_file), res)) {
3944 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3945 return RETURN_ERR;
3946 }
developera3511852023-06-14 14:12:59 +08003947 params.name = "noscan";
3948 params.value = noscan;
3949 wifi_hostapdWrite(config_file, &params, 1);
3950 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003951
developera3511852023-06-14 14:12:59 +08003952 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3953 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003954}
3955
3956//Get the list of supported channel. eg: "1-11"
3957//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
3958INT wifi_getRadioPossibleChannels(INT radioIndex, CHAR *output_string) //RDKB
3959{
developera3511852023-06-14 14:12:59 +08003960 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3961 if (NULL == output_string)
3962 return RETURN_ERR;
3963 char cmd[256] = {0};
3964 char buf[128] = {0};
3965 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +08003966 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003967
developera3511852023-06-14 14:12:59 +08003968 // Parse possible channel number and separate them with commas.
3969 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
3970 phyId = radio_index_to_phy(radioIndex);
3971 // Channel 68 and 96 only allow bandwidth 20MHz, so we remove them with their frequency.
3972 if (dfs_enable)
developere40952c2023-06-15 18:46:43 +08003973 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep -e '\\*.*MHz .*dBm' | grep -v 'no IR\\|5340\\|5480' | cut -d '[' -f2 | cut -d ']' -f1 | tr '\\n' ',' | sed 's/.$//'", phyId);
developera3511852023-06-14 14:12:59 +08003974 else
developere40952c2023-06-15 18:46:43 +08003975 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep -e '\\*.*MHz .*dBm' | grep -v 'radar\\|no IR\\|5340\\|5480' | cut -d '[' -f2 | cut -d ']' -f1 | tr '\\n' ',' | sed 's/.$//'", phyId);
developer72fb0bb2023-01-11 09:46:29 +08003976
developere40952c2023-06-15 18:46:43 +08003977 if (os_snprintf_error(sizeof(cmd), res)) {
3978 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3979 return RETURN_ERR;
3980 }
developera3511852023-06-14 14:12:59 +08003981 _syscmd(cmd,buf,sizeof(buf));
3982 strncpy(output_string, buf, strlen(buf) < sizeof(buf) ? strlen(buf) : sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08003983
developera3511852023-06-14 14:12:59 +08003984 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3985 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003986}
developerd1824452023-05-18 12:30:04 +08003987//Getting current radio extension channel
3988INT wifi_halgetRadioExtChannel(CHAR *file,CHAR *Value)
3989{
developera3511852023-06-14 14:12:59 +08003990 CHAR buf[150] = {0};
developer32f2a182023-06-27 19:50:41 +08003991 int len;
developerd1824452023-05-18 12:30:04 +08003992
developera3511852023-06-14 14:12:59 +08003993 wifi_datfileRead(file, "HT_EXTCHA", buf, sizeof(buf));
developerb14b3462023-07-01 18:02:42 +08003994
3995 if (strncmp(buf, "0", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08003996 len = strlen("BelowControlChannel");
3997 memcpy(Value, "BelowControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08003998 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08003999 }
developerb14b3462023-07-01 18:02:42 +08004000 else if(strncmp(buf, "1", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08004001 len = strlen("AboveControlChannel");
4002 memcpy(Value, "AboveControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08004003 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08004004 }
developer5b23cd02023-07-19 20:26:03 +08004005
developera3511852023-06-14 14:12:59 +08004006 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +08004007}
developerf6a87542023-05-16 15:47:28 +08004008
developer72fb0bb2023-01-11 09:46:29 +08004009//Get the list for used channel. eg: "1,6,9,11"
4010//The output_string is a max length 256 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
4011INT wifi_getRadioChannelsInUse(INT radioIndex, CHAR *output_string) //RDKB
4012{
developera3511852023-06-14 14:12:59 +08004013 char interface_name[16] = {0};
4014 char cmd[128] = {0};
4015 char buf[128] = {0};
4016 char config_file[64] = {0};
4017 int channel = 0;
4018 int freq = 0;
4019 int bandwidth = 0;
4020 int center_freq = 0;
4021 int center_channel = 0;
4022 int channel_delta = 0;
4023 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004024 int res;
developer72fb0bb2023-01-11 09:46:29 +08004025
developera3511852023-06-14 14:12:59 +08004026 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004027
developera3511852023-06-14 14:12:59 +08004028 if (NULL == output_string)
4029 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004030
developera3511852023-06-14 14:12:59 +08004031 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4032 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08004033
4034 res = snprintf(cmd, sizeof(cmd), "iw %s info | grep channel | sed -e 's/[^0-9 ]//g'", interface_name);
4035 if (os_snprintf_error(sizeof(cmd), res)) {
4036 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4037 return RETURN_ERR;
4038 }
developera3511852023-06-14 14:12:59 +08004039 _syscmd(cmd, buf, sizeof(buf));
4040 if (strlen(buf) == 0) {
developer75bd10c2023-06-27 11:34:08 +08004041 wifi_debug(DEBUG_ERROR, "failed to get channel information from iw.\n");
developera3511852023-06-14 14:12:59 +08004042 return RETURN_ERR;
4043 }
developerd14dff12023-06-28 22:47:44 +08004044 if (sscanf(buf, "%d %d %d %*d %d", &channel, &freq, &bandwidth, &center_freq) != 4) {
4045 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
4046 return RETURN_ERR;
4047 }
developer72fb0bb2023-01-11 09:46:29 +08004048
developera3511852023-06-14 14:12:59 +08004049 if (bandwidth == 20) {
developere40952c2023-06-15 18:46:43 +08004050 res = snprintf(output_string, 256, "%d", channel);
4051 if (os_snprintf_error(256, res)) {
4052 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4053 return RETURN_ERR;
4054 }
developera3511852023-06-14 14:12:59 +08004055 return RETURN_OK;
4056 }
developer72fb0bb2023-01-11 09:46:29 +08004057
developera3511852023-06-14 14:12:59 +08004058 center_channel = ieee80211_frequency_to_channel(center_freq);
developer72fb0bb2023-01-11 09:46:29 +08004059
developera3511852023-06-14 14:12:59 +08004060 band = wifi_index_to_band(radioIndex);
4061 if (band == band_2_4 && bandwidth == 40) {
developer32f2a182023-06-27 19:50:41 +08004062 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4063 if (os_snprintf_error(sizeof(config_file), res)) {
4064 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4065 return RETURN_ERR;
4066 }
developera3511852023-06-14 14:12:59 +08004067 memset(buf, 0, sizeof(buf));
4068 wifi_halgetRadioExtChannel(config_file, buf); // read ht_capab for HT40+ or -
developer72fb0bb2023-01-11 09:46:29 +08004069
developera3511852023-06-14 14:12:59 +08004070 if (strncmp(buf, "AboveControlChannel", strlen("AboveControlChannel")) == 0 && channel < 10) {
developere40952c2023-06-15 18:46:43 +08004071 res = snprintf(output_string, 256, "%d,%d", channel, channel+4);
developera3511852023-06-14 14:12:59 +08004072 } else if (strncmp(buf, "BelowControlChannel", strlen("BelowControlChannel")) == 0 && channel > 4) {
developere40952c2023-06-15 18:46:43 +08004073 res = snprintf(output_string, 256, "%d,%d", channel-4, channel);
developera3511852023-06-14 14:12:59 +08004074 } else {
developer37646972023-06-29 10:58:43 +08004075 if (fprintf(stderr, "%s: invalid channel %d set with %s\n.", __func__, channel, buf) < 0)
4076 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08004077 return RETURN_ERR;
4078 }
developerb758dfd2023-06-21 17:32:07 +08004079
developere40952c2023-06-15 18:46:43 +08004080 if (os_snprintf_error(256, res)) {
4081 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4082 return RETURN_ERR;
4083 }
developera3511852023-06-14 14:12:59 +08004084 } else if (band == band_5 || band == band_6){
4085 // to minus 20 is an offset, because frequence of a channel have a range. We need to use offset to calculate correct channel.
4086 // example: bandwidth 80: center is 42 (5210), channels are "36,40,44,48" (5170-5250). The delta should be 6.
4087 channel_delta = (bandwidth-20)/10;
4088 memset(output_string, 0, 256);
4089 for (int i = center_channel-channel_delta; i <= center_channel+channel_delta; i+=4) {
4090 // If i is not the last channel, we add a comma.
developere40952c2023-06-15 18:46:43 +08004091 res = snprintf(buf, sizeof(buf), "%d%s", i, i==center_channel+channel_delta?"":",");
developer5b23cd02023-07-19 20:26:03 +08004092
developere40952c2023-06-15 18:46:43 +08004093 if (os_snprintf_error(sizeof(buf), res)) {
4094 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4095 return RETURN_ERR;
4096 }
developer32f2a182023-06-27 19:50:41 +08004097 strncat(output_string, buf, sizeof(output_string) - strlen(output_string) - 1);
developera3511852023-06-14 14:12:59 +08004098 }
4099 } else
4100 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004101
developera3511852023-06-14 14:12:59 +08004102 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4103 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004104}
4105
developer69b61b02023-03-07 17:17:44 +08004106//Get the running channel number
developerd1824452023-05-18 12:30:04 +08004107INT wifi_getRadioChannel(INT radioIndex, ULONG *output_ulong) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004108{
developera3511852023-06-14 14:12:59 +08004109 char channel_str[16] = {0};
4110 char config_file[128] = {0};
4111 char buf[MAX_BUF_SIZE] = {0};
4112 char cmd[MAX_CMD_SIZE] = {0};
4113 char interface_name[IF_NAME_SIZE] = {0};
4114 wifi_band band = band_invalid;
4115 ULONG iwChannel = 0;
developere40952c2023-06-15 18:46:43 +08004116 int res;
developer47a56bf2023-05-30 13:38:57 +08004117
developera3511852023-06-14 14:12:59 +08004118 if (output_ulong == NULL)
4119 return RETURN_ERR;
4120 band = wifi_index_to_band(radioIndex);
developerb758dfd2023-06-21 17:32:07 +08004121 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08004122 if (os_snprintf_error(sizeof(config_file), res)) {
4123 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4124 return RETURN_ERR;
4125 }
4126
developera3511852023-06-14 14:12:59 +08004127 wifi_datfileRead(config_file, "Channel", channel_str, sizeof(channel_str));
developerb14b3462023-07-01 18:02:42 +08004128 if (hal_strtoul(channel_str, 10, output_ulong) < 0) {
developerc14d83a2023-06-29 20:09:42 +08004129 wifi_debug(DEBUG_ERROR, "strtol fail\n");
4130 }
developera3511852023-06-14 14:12:59 +08004131 if (*output_ulong == 0) {
4132 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4133 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004134 res = snprintf(cmd, sizeof(cmd), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4135 if (os_snprintf_error(sizeof(cmd), res)) {
4136 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4137 return RETURN_ERR;
4138 }
4139
developera3511852023-06-14 14:12:59 +08004140 _syscmd(cmd,buf,sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08004141 if (sscanf(buf, "%lu", &iwChannel) != 1) {
4142 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
4143 return RETURN_ERR;
4144 }
developera3511852023-06-14 14:12:59 +08004145 *output_ulong = iwChannel;
4146 }
developer72fb0bb2023-01-11 09:46:29 +08004147
developera3511852023-06-14 14:12:59 +08004148 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004149}
4150
developer72fb0bb2023-01-11 09:46:29 +08004151INT wifi_getApChannel(INT apIndex,ULONG *output_ulong) //RDKB
4152{
developera3511852023-06-14 14:12:59 +08004153 char cmd[1024] = {0}, buf[5] = {0};
4154 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08004155 int res;
developer72fb0bb2023-01-11 09:46:29 +08004156
developera3511852023-06-14 14:12:59 +08004157 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4158 if (NULL == output_ulong)
4159 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004160
developera3511852023-06-14 14:12:59 +08004161 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
4162 return RETURN_ERR;
developer47a56bf2023-05-30 13:38:57 +08004163
developere40952c2023-06-15 18:46:43 +08004164 res = snprintf(cmd, sizeof(cmd), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4165 if (os_snprintf_error(sizeof(cmd), res)) {
4166 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4167 return RETURN_ERR;
4168 }
developera3511852023-06-14 14:12:59 +08004169 _syscmd(cmd,buf,sizeof(buf));
4170 *output_ulong = (strlen(buf) >= 1)? atol(buf): 0;
4171 if (*output_ulong == 0) {
4172 return RETURN_ERR;
4173 }
developer72fb0bb2023-01-11 09:46:29 +08004174
developera3511852023-06-14 14:12:59 +08004175 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4176 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004177}
developer72fb0bb2023-01-11 09:46:29 +08004178//Storing the previous channel value
4179INT wifi_storeprevchanval(INT radioIndex)
4180{
developera3511852023-06-14 14:12:59 +08004181 char output[4]={'\0'};
4182 char config_file[MAX_BUF_SIZE] = {0};
4183 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004184 int res;
developerd1824452023-05-18 12:30:04 +08004185
developera3511852023-06-14 14:12:59 +08004186 band = wifi_index_to_band(radioIndex);
4187 if (band == band_invalid) {
4188 return RETURN_ERR;
4189 wifi_dbg_printf("[%s]: Invalid radio index", __func__);
4190 }
developere40952c2023-06-15 18:46:43 +08004191 res = snprintf(config_file, sizeof(config_file), "%s%d.dat",LOGAN_DAT_FILE, band);
4192 if (os_snprintf_error(sizeof(config_file), res)) {
4193 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4194 return RETURN_ERR;
4195 }
4196
developera3511852023-06-14 14:12:59 +08004197 wifi_datfileRead(config_file, "Channel", output, sizeof(output));
developerd1824452023-05-18 12:30:04 +08004198
developera3511852023-06-14 14:12:59 +08004199 if(band == band_2_4)
developer33f13ba2023-07-12 16:19:06 +08004200 res = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004201 else if(band == band_5)
developer33f13ba2023-07-12 16:19:06 +08004202 res = v_secure_system("echo %s > /var/prevchanval5G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004203 else
developer33f13ba2023-07-12 16:19:06 +08004204 res = v_secure_system("echo %s > /var/prevchanval6G_AutoChannelEnable", output);
developer75bd10c2023-06-27 11:34:08 +08004205
developer33f13ba2023-07-12 16:19:06 +08004206 if (res) {
4207 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08004208 }
developera3511852023-06-14 14:12:59 +08004209 Radio_flag = FALSE;
4210 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004211}
4212
4213//Set the running channel number
4214INT wifi_setRadioChannel(INT radioIndex, ULONG channel) //RDKB //AP only
4215{
developera3511852023-06-14 14:12:59 +08004216 // We only write hostapd config here
4217 char str_channel[8]={0};
4218 char *list_channel;
4219 char possible_channels[256] = {0};
4220 char config_file_dat[128] = {0};
4221 struct params dat = {0};
4222 struct params acs = {0};
4223 wifi_band band = band_invalid;
4224 bool acs_channel = false;
developere40952c2023-06-15 18:46:43 +08004225 int res;
developer72fb0bb2023-01-11 09:46:29 +08004226
developera3511852023-06-14 14:12:59 +08004227 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004228
developera3511852023-06-14 14:12:59 +08004229 if (channel == 0)
4230 acs_channel = true;
4231 // Check valid
developer75bd10c2023-06-27 11:34:08 +08004232 res = snprintf(str_channel, sizeof(str_channel), "%lu", channel);
4233 if (os_snprintf_error(sizeof(str_channel), res)) {
4234 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4235 return RETURN_ERR;
4236 }
developer47a56bf2023-05-30 13:38:57 +08004237
developera3511852023-06-14 14:12:59 +08004238 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4239 list_channel = strtok(possible_channels, ",");
4240 while(true)
4241 {
4242 if(list_channel == NULL) { // input not in the list
developer75bd10c2023-06-27 11:34:08 +08004243 wifi_debug(DEBUG_ERROR, "Channel %s is not in possible list\n", str_channel);
developera3511852023-06-14 14:12:59 +08004244 return RETURN_ERR;
4245 }
4246 if (strncmp(str_channel, list_channel, strlen(list_channel)) == 0 || strncmp(str_channel, "0", 1) == 0)
4247 break;
4248 list_channel = strtok(NULL, ",");
4249 }
4250 /*
4251 list.name = "channel";
4252 list.value = str_channel;
4253 wifi_getMaxRadioNumber(&max_radio_num);
4254 for(int i=0; i<=MAX_APS/max_radio_num;i++)
4255 {
4256 sprintf(config_file, "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_radio_num*i));
4257 wifi_hostapdWrite(config_file, &list, 1);
4258 }
4259 */
4260 dat.name = "Channel";
4261 dat.value = str_channel;
4262 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004263 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4264 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4265 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4266 return RETURN_ERR;
4267 }
4268
developera3511852023-06-14 14:12:59 +08004269 wifi_datfileWrite(config_file_dat, &dat, 1);
4270 if (acs_channel == true) {
4271 acs.name = "AutoChannelSelect";
4272 acs.value = "3";
4273 } else {
4274 acs.name = "AutoChannelSelect";
4275 acs.value = "0";
4276 }
4277 wifi_datfileWrite(config_file_dat, &acs, 1);
developer5b23cd02023-07-19 20:26:03 +08004278 if (multiple_set == FALSE) {
4279 wifi_setRadioEnable(radioIndex, FALSE);
4280 wifi_setRadioEnable(radioIndex, TRUE);
4281 }
4282
developera3511852023-06-14 14:12:59 +08004283 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4284 return RETURN_OK;
4285}
4286
4287INT wifi_setRadioCenterChannel(INT radioIndex, ULONG channel)
4288{
4289 struct params list[2];
4290 char str_idx[16];
4291 char config_file[64];
developere40952c2023-06-15 18:46:43 +08004292 int max_num_radios = 0, res;
developera3511852023-06-14 14:12:59 +08004293 wifi_band band = band_invalid;
4294
4295 band = wifi_index_to_band(radioIndex);
4296 if (band == band_2_4)
4297 return RETURN_OK;
4298
developere40952c2023-06-15 18:46:43 +08004299 res = snprintf(str_idx, sizeof(str_idx), "%lu", channel);
4300 if (os_snprintf_error(sizeof(str_idx), res)) {
4301 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4302 return RETURN_ERR;
4303 }
4304
developera3511852023-06-14 14:12:59 +08004305 list[0].name = "vht_oper_centr_freq_seg0_idx";
4306 list[0].value = str_idx;
4307 list[1].name = "he_oper_centr_freq_seg0_idx";
4308 list[1].value = str_idx;
4309
4310 wifi_getMaxRadioNumber(&max_num_radios);
developer9ce44382023-06-28 11:09:37 +08004311 if(max_num_radios== 0){
4312 return RETURN_ERR;
4313 }
developera3511852023-06-14 14:12:59 +08004314 for(int i=0; i<=MAX_APS/max_num_radios; i++)
4315 {
developere40952c2023-06-15 18:46:43 +08004316 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_num_radios*i));
4317 if (os_snprintf_error(sizeof(config_file), res)) {
4318 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4319 return RETURN_ERR;
4320 }
developera3511852023-06-14 14:12:59 +08004321 if (band == band_6)
4322 wifi_hostapdWrite(config_file, &list[1], 1);
4323 else
4324 wifi_hostapdWrite(config_file, list, 2);
4325 }
4326
4327 return RETURN_OK;
4328}
4329
4330//Enables or disables a driver level variable to indicate if auto channel selection is enabled on this radio
4331//This "auto channel" means the auto channel selection when radio is up. (which is different from the dynamic channel/frequency selection (DFC/DCS))
4332INT wifi_setRadioAutoChannelEnable(INT radioIndex, BOOL enable) //RDKB
4333{
4334 //Set to wifi config only. Wait for wifi reset to apply.
4335 ULONG Value = 0;
4336 char config_file_dat[128] = {0};
4337 struct params acs = {0};
4338 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004339 int res;
developera3511852023-06-14 14:12:59 +08004340
4341 if(enable == TRUE) {
4342 wifi_setRadioChannel(radioIndex,Value);
4343 } else {
4344 acs.name = "AutoChannelSelect";
4345 acs.value = "0";
4346 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004347 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4348 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4350 return RETURN_ERR;
4351 }
developera3511852023-06-14 14:12:59 +08004352 wifi_datfileWrite(config_file_dat, &acs, 1);
4353 }
4354 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004355}
4356
4357INT wifi_getRadioAutoChannelSupported(INT radioIndex, BOOL *output_bool)
4358{
developera3511852023-06-14 14:12:59 +08004359 if (output_bool == NULL)
4360 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004361
developera3511852023-06-14 14:12:59 +08004362 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004363
developera3511852023-06-14 14:12:59 +08004364 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004365}
4366
4367INT wifi_getRadioDCSSupported(INT radioIndex, BOOL *output_bool) //RDKB
4368{
developera3511852023-06-14 14:12:59 +08004369 if (NULL == output_bool)
4370 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004371 *output_bool=TRUE;
developera3511852023-06-14 14:12:59 +08004372 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004373}
4374
4375INT wifi_getRadioDCSEnable(INT radioIndex, BOOL *output_bool) //RDKB
4376{
developer326d4232023-06-15 16:45:30 +08004377 unsigned long period = 0;
4378
developera3511852023-06-14 14:12:59 +08004379 if (NULL == output_bool)
4380 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004381
4382 if (wifi_getRadioAutoChannelRefreshPeriod(radioIndex, &period) != RETURN_OK)
developerb758dfd2023-06-21 17:32:07 +08004383 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004384
4385 *output_bool = (period > 0) ? TRUE : FALSE;
4386
developera3511852023-06-14 14:12:59 +08004387 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004388}
4389
developer326d4232023-06-15 16:45:30 +08004390INT wifi_setRadioDCSEnable(INT radioIndex, BOOL enable) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004391{
developer326d4232023-06-15 16:45:30 +08004392 ULONG period = 1800;
4393
4394 if (enable == TRUE) {
4395 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, period) != RETURN_OK)
4396 return RETURN_ERR;
4397 }
4398 else {
4399 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, 0) != RETURN_OK)
4400 return RETURN_ERR;
4401 }
developera3511852023-06-14 14:12:59 +08004402 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004403}
4404
4405INT wifi_setApEnableOnLine(ULONG wlanIndex,BOOL enable)
4406{
developera3511852023-06-14 14:12:59 +08004407 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004408}
4409
4410INT wifi_factoryResetAP(int apIndex)
4411{
developerb149d9d2023-06-06 16:14:22 +08004412 char ap_config_file[MAX_SUB_CMD_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08004413 char cmd[MAX_CMD_SIZE] = {0};
4414 char ret_buf[MAX_BUF_SIZE] = {0};
4415 int radio_idx = 0;
4416 int bss_idx = 0;
4417 char ssid[32] = {0};
4418 char interface[IF_NAME_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08004419 char psk_file[MAX_SUB_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08004420 struct params params[3] = {0};
developere40952c2023-06-15 18:46:43 +08004421 int res;
developer72fb0bb2023-01-11 09:46:29 +08004422
developera3511852023-06-14 14:12:59 +08004423 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004424
developer47cc27a2023-05-17 23:09:58 +08004425 /*del old config file*/
developer9ce44382023-06-28 11:09:37 +08004426 res = snprintf(ap_config_file, sizeof(ap_config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
developerd14dff12023-06-28 22:47:44 +08004427 if (os_snprintf_error(sizeof(ap_config_file), res)) {
developere40952c2023-06-15 18:46:43 +08004428 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4429 return RETURN_ERR;
4430 }
4431
4432 res = snprintf(cmd, MAX_CMD_SIZE, "rm %s", ap_config_file);
4433 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4434 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4435 return RETURN_ERR;
4436 }
4437
developer47cc27a2023-05-17 23:09:58 +08004438 _syscmd(cmd, ret_buf, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004439
developer47cc27a2023-05-17 23:09:58 +08004440 memset(cmd, 0, sizeof(cmd));
4441 memset(ret_buf, 0, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004442
developer47cc27a2023-05-17 23:09:58 +08004443 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
4444
4445 /*prepare new config file*/
developere40952c2023-06-15 18:46:43 +08004446 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], ap_config_file);
4447 if (os_snprintf_error(sizeof(cmd), res)) {
4448 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4449 return RETURN_ERR;
4450 }
4451
developer47cc27a2023-05-17 23:09:58 +08004452 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4453
4454 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08004455 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
4456 if (os_snprintf_error(sizeof(ssid), res)) {
4457 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4458 return RETURN_ERR;
4459 }
4460
4461 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
4462 if (os_snprintf_error(sizeof(interface), res)) {
4463 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4464 return RETURN_ERR;
4465 }
developer47cc27a2023-05-17 23:09:58 +08004466 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08004467 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
4468 if (os_snprintf_error(sizeof(ssid), res)) {
4469 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4470 return RETURN_ERR;
4471 }
4472
4473 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
4474 if (os_snprintf_error(sizeof(interface), res)) {
4475 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4476 return RETURN_ERR;
4477 }
developer47cc27a2023-05-17 23:09:58 +08004478 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08004479 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
4480 if (os_snprintf_error(sizeof(ssid), res)) {
4481 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4482 return RETURN_ERR;
4483 }
4484
4485 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
4486 if (os_snprintf_error(sizeof(interface), res)) {
4487 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4488 return RETURN_ERR;
4489 }
developer47cc27a2023-05-17 23:09:58 +08004490 }
4491
4492 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08004493 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", apIndex);
4494 if (os_snprintf_error(sizeof(psk_file), res)) {
4495 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4496 return RETURN_ERR;
4497 }
developer47cc27a2023-05-17 23:09:58 +08004498
4499 params[0].name = "ssid";
4500 params[0].value = ssid;
4501 params[1].name = "interface";
4502 params[1].value = interface;
4503 params[2].name = "wpa_psk_file";
4504 params[2].value = psk_file;
4505
4506 wifi_hostapdWrite(ap_config_file, params, 3);
4507
4508 /*clear psk file*/
4509 memset(cmd, 0, sizeof(cmd));
4510 memset(ret_buf, 0, sizeof(ret_buf));
4511
developere40952c2023-06-15 18:46:43 +08004512 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, apIndex);
4513 if (os_snprintf_error(sizeof(psk_file), res)) {
4514 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4515 return RETURN_ERR;
4516 }
developer47cc27a2023-05-17 23:09:58 +08004517
4518 if (access(psk_file, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08004519 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", psk_file);
4520 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4521 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4522 return RETURN_ERR;
4523 }
4524
developer47cc27a2023-05-17 23:09:58 +08004525 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4526 } else {
developere40952c2023-06-15 18:46:43 +08004527 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", psk_file);
4528 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4529 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4530 return RETURN_ERR;
4531 }
4532
developer47cc27a2023-05-17 23:09:58 +08004533 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4534 }
4535
developer429ba832023-05-31 11:03:35 +08004536 wifi_setApEnable(apIndex, FALSE);
4537 wifi_setApEnable(apIndex, TRUE);
developer47cc27a2023-05-17 23:09:58 +08004538 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4539
4540 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004541}
4542
developer72fb0bb2023-01-11 09:46:29 +08004543INT wifi_setBandSteeringApGroup(char *ApGroup)
4544{
developera3511852023-06-14 14:12:59 +08004545 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004546}
4547
4548INT wifi_getApDTIMInterval(INT apIndex, INT *dtimInterval)
4549{
developera3511852023-06-14 14:12:59 +08004550 char config_file[128] = {'\0'};
4551 char buf[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004552 int res;
developerc14d83a2023-06-29 20:09:42 +08004553 long int tmp;
developer5b23cd02023-07-19 20:26:03 +08004554
developera3511852023-06-14 14:12:59 +08004555 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4556 if (dtimInterval == NULL)
4557 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004558
4559 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4560 if (os_snprintf_error(sizeof(config_file), res)) {
4561 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4562 return RETURN_ERR;
4563 }
developer72fb0bb2023-01-11 09:46:29 +08004564
developera3511852023-06-14 14:12:59 +08004565 wifi_hostapdRead(config_file, "dtim_period", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08004566
developera3511852023-06-14 14:12:59 +08004567 if (strlen(buf) == 0) {
4568 *dtimInterval = 2;
4569 } else {
developerc14d83a2023-06-29 20:09:42 +08004570 if (hal_strtol(buf, 10, &tmp) < 0) {
4571 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08004572 }
developerc14d83a2023-06-29 20:09:42 +08004573 *dtimInterval = tmp;
developera3511852023-06-14 14:12:59 +08004574 }
developer72fb0bb2023-01-11 09:46:29 +08004575
developera3511852023-06-14 14:12:59 +08004576 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4577 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004578}
4579
4580INT wifi_setApDTIMInterval(INT apIndex, INT dtimInterval)
4581{
developera3511852023-06-14 14:12:59 +08004582 struct params params={0};
4583 char config_file[MAX_BUF_SIZE] = {'\0'};
4584 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004585 int res;
developer72fb0bb2023-01-11 09:46:29 +08004586
developera3511852023-06-14 14:12:59 +08004587 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4588 if (dtimInterval < 1 || dtimInterval > 255) {
4589 WIFI_ENTRY_EXIT_DEBUG("Invalid dtimInterval: %d\n", dtimInterval);
4590 return RETURN_ERR;
4591 }
developer69b61b02023-03-07 17:17:44 +08004592
developera3511852023-06-14 14:12:59 +08004593 params.name = "dtim_period";
developere40952c2023-06-15 18:46:43 +08004594 res = snprintf(buf, sizeof(buf), "%d", dtimInterval);
4595 if (os_snprintf_error(sizeof(buf), res)) {
4596 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4597 return RETURN_ERR;
4598 }
developera3511852023-06-14 14:12:59 +08004599 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08004600
developer75bd10c2023-06-27 11:34:08 +08004601 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4602 if (os_snprintf_error(sizeof(config_file), res)) {
4603 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4604 return RETURN_ERR;
4605 }
developera3511852023-06-14 14:12:59 +08004606 wifi_hostapdWrite(config_file, &params, 1);
4607 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08004608
developera3511852023-06-14 14:12:59 +08004609 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4610 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004611}
4612
4613//Check if the driver support the Dfs
4614INT wifi_getRadioDfsSupport(INT radioIndex, BOOL *output_bool) //Tr181
4615{
developera3511852023-06-14 14:12:59 +08004616 wifi_band band = band_invalid;
4617 if (NULL == output_bool)
4618 return RETURN_ERR;
4619 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +08004620
developera3511852023-06-14 14:12:59 +08004621 band = wifi_index_to_band(radioIndex);
4622 if (band == band_5)
4623 *output_bool = TRUE;
4624 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004625}
4626
4627//The output_string is a max length 256 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
4628//The value of this parameter is a comma seperated list of channel number
4629INT wifi_getRadioDCSChannelPool(INT radioIndex, CHAR *output_pool) //RDKB
4630{
developer326d4232023-06-15 16:45:30 +08004631
4632 #define CHANNEL_AVAILABLE 0
4633 #define CHANNEL_INVALID 1
4634 #define CHANNEL_LIST_MAX_LENGTH 256
4635 #define MAX_CHANNEL_NUMBER 255
4636
4637 char config_file[MAX_BUF_SIZE] = {0};
4638 char possible_channels[CHANNEL_LIST_MAX_LENGTH] = {0};
4639 char skip_list[CHANNEL_LIST_MAX_LENGTH] = {0};
4640 int skip_table[MAX_CHANNEL_NUMBER +1] = {0};
4641 wifi_band band = band_invalid;
4642 char *token_channel = NULL, *token_skip = NULL;
developer75bd10c2023-06-27 11:34:08 +08004643 int res;
developere40952c2023-06-15 18:46:43 +08004644
developera3511852023-06-14 14:12:59 +08004645 if (NULL == output_pool)
4646 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004647 // get skiplist, possible_channels list
4648 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4649 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004650 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4651 if (os_snprintf_error(sizeof(config_file), res)) {
4652 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4653 return RETURN_ERR;
4654 }
developer326d4232023-06-15 16:45:30 +08004655 wifi_datfileRead(config_file, "AutoChannelSkipList", skip_list, sizeof(skip_list));
4656
4657 if (skip_list[0] != '\0') {
4658 int len = strlen(skip_list);
4659 for (int i = 0; i < len; i++) {
4660 if (skip_list[i] == ';') {
4661 skip_list[i] = ',';
4662 }
4663 }
4664 // skip list
4665 token_skip = strtok(skip_list, ",");
4666 while (token_skip != NULL) {
4667 int channel = atoi(token_skip);
4668 if (channel <= MAX_CHANNEL_NUMBER && strstr(possible_channels, token_skip) != NULL)
4669 skip_table[atoi(token_skip)] = CHANNEL_INVALID;
4670 token_skip = strtok(NULL, ",");
4671 }
developere40952c2023-06-15 18:46:43 +08004672 }
developer72fb0bb2023-01-11 09:46:29 +08004673
developer326d4232023-06-15 16:45:30 +08004674 int count = 0;
4675 token_channel = strtok(possible_channels, ",");
4676 while (token_channel != NULL) {
4677 int channel = atoi(token_channel);
4678 if (channel <= MAX_CHANNEL_NUMBER && skip_table[channel] == CHANNEL_AVAILABLE) {
4679 count += snprintf(&output_pool[count], CHANNEL_LIST_MAX_LENGTH-count, "%d,", channel);
4680 if (count >= CHANNEL_LIST_MAX_LENGTH-1)
4681 break;
4682 }
4683 token_channel = strtok(NULL, ",");
4684 }
4685 //delete the last one ','
4686 if (count >0 && output_pool[count-1] == ',')
4687 output_pool[count-1] = '\0';
developera3511852023-06-14 14:12:59 +08004688 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004689}
4690
4691INT wifi_setRadioDCSChannelPool(INT radioIndex, CHAR *pool) //RDKB
4692{
developer326d4232023-06-15 16:45:30 +08004693 char config_file_dat[128] = {0};
4694 struct params dat = {0};
4695 wifi_band band = band_invalid;
4696 char new_pool[128] = {0};
developer75bd10c2023-06-27 11:34:08 +08004697 int res;
developer326d4232023-06-15 16:45:30 +08004698
4699 if (NULL == pool)
4700 return RETURN_ERR;
4701
developer9ce44382023-06-28 11:09:37 +08004702 strncpy(new_pool, pool, sizeof(new_pool) - 1);
4703 new_pool[sizeof(new_pool) - 1] = '\0';
developer326d4232023-06-15 16:45:30 +08004704 for (int i = 0; new_pool[i] != '\0'; i++) {
4705 if (new_pool[i] == ',')
4706 new_pool[i] = ';';
4707 }
4708
4709 dat.name = "AutoChannelSkipList";
4710 dat.value = new_pool;
4711 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004712 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4713 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4714 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4715 return RETURN_ERR;
4716 }
developer326d4232023-06-15 16:45:30 +08004717 if (wifi_datfileWrite(config_file_dat, &dat, 1) != 0)
4718 return RETURN_ERR;
4719 wifi_reloadAp(radioIndex);
4720
developera3511852023-06-14 14:12:59 +08004721 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004722}
4723
4724INT wifi_getRadioDCSScanTime(INT radioIndex, INT *output_interval_seconds, INT *output_dwell_milliseconds)
4725{
developera3511852023-06-14 14:12:59 +08004726 if (NULL == output_interval_seconds || NULL == output_dwell_milliseconds)
4727 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004728 //Should refresh period time be filled in here? output_interval_seconds is INT type
4729 //wifi_getRadioAutoChannelRefreshPeriod is Ulong type
4730 *output_interval_seconds = 1800;
4731 *output_dwell_milliseconds = 200;
developer72fb0bb2023-01-11 09:46:29 +08004732
developera3511852023-06-14 14:12:59 +08004733 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004734}
4735
4736INT wifi_setRadioDCSScanTime(INT radioIndex, INT interval_seconds, INT dwell_milliseconds)
4737{
developera3511852023-06-14 14:12:59 +08004738 //Set to wifi config. And apply instantly.
4739 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004740}
4741
4742INT wifi_getRadioDfsAtBootUpEnable(INT radioIndex, BOOL *output_bool) //Tr181
4743{
developera3511852023-06-14 14:12:59 +08004744 if (output_bool == NULL)
4745 return RETURN_ERR;
4746 *output_bool = true;
4747 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004748}
4749
4750INT wifi_setRadioDfsAtBootUpEnable(INT radioIndex, BOOL enable) //Tr181
4751{
developera3511852023-06-14 14:12:59 +08004752 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004753}
4754
4755//Get the Dfs enable status
4756INT wifi_getRadioDfsEnable(INT radioIndex, BOOL *output_bool) //Tr181
4757{
developera3511852023-06-14 14:12:59 +08004758 char buf[16] = {0};
4759 char config_file_dat[128] = {0};
4760 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004761 int res;
developer72fb0bb2023-01-11 09:46:29 +08004762
developera3511852023-06-14 14:12:59 +08004763 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004764
developera3511852023-06-14 14:12:59 +08004765 if (output_bool == NULL)
4766 return RETURN_ERR;
4767 *output_bool = TRUE; // default
4768 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004769 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4770 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4771 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4772 return RETURN_ERR;
4773 }
developerd1824452023-05-18 12:30:04 +08004774
developera3511852023-06-14 14:12:59 +08004775 wifi_datfileRead(config_file_dat, "DfsEnable", buf, sizeof(buf));
developerd1824452023-05-18 12:30:04 +08004776
developera3511852023-06-14 14:12:59 +08004777 if (strncmp(buf, "0", 1) == 0)
4778 *output_bool = FALSE;
4779 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4780 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004781}
4782
4783//Set the Dfs enable status
4784INT wifi_setRadioDfsEnable(INT radioIndex, BOOL enable) //Tr181
4785{
developera3511852023-06-14 14:12:59 +08004786 char config_dat_file[128] = {0};
4787 FILE *f = NULL;
4788 struct params dat = {0};
4789 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08004790 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +08004791
developera3511852023-06-14 14:12:59 +08004792 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004793
developera3511852023-06-14 14:12:59 +08004794 f = fopen(DFS_ENABLE_FILE, "w");
4795 if (f == NULL)
4796 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08004797 ret = fprintf(f, "%d", enable);
4798 if (ret < 0)
4799 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developerd14dff12023-06-28 22:47:44 +08004800 if (fclose(f) != 0) {
4801 wifi_debug(DEBUG_ERROR, "fclose fail\n");
4802 return RETURN_ERR;
4803 }
developer72fb0bb2023-01-11 09:46:29 +08004804
developera3511852023-06-14 14:12:59 +08004805 wifi_setRadioIEEE80211hEnabled(radioIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08004806
developera3511852023-06-14 14:12:59 +08004807 dat.name = "DfsEnable";
4808 dat.value = enable?"1":"0";
4809 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004810 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4811 if (os_snprintf_error(sizeof(config_dat_file), res)) {
4812 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4813 return RETURN_ERR;
4814 }
4815
developera3511852023-06-14 14:12:59 +08004816 wifi_datfileWrite(config_dat_file, &dat, 1);
4817 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4818 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004819}
4820
4821//Check if the driver support the AutoChannelRefreshPeriod
4822INT wifi_getRadioAutoChannelRefreshPeriodSupported(INT radioIndex, BOOL *output_bool) //Tr181
4823{
developera3511852023-06-14 14:12:59 +08004824 if (NULL == output_bool)
4825 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004826 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004827
developera3511852023-06-14 14:12:59 +08004828 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004829}
4830
developer326d4232023-06-15 16:45:30 +08004831
4832int get_ACS_RefreshPeriod_callback(struct nl_msg *msg, void *arg)
4833{
4834 ULONG *data = (ULONG *)arg;
4835 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4836 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
4837 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4838 int err = 0;
4839
4840 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4841 genlmsg_attrlen(gnlh, 0), NULL);
4842 if (err < 0)
4843 return NL_SKIP;
4844
4845 if (tb[NL80211_ATTR_VENDOR_DATA]) {
4846 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
4847 tb[NL80211_ATTR_VENDOR_DATA], NULL);
4848 if (err < 0)
4849 return NL_SKIP;
4850
4851 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]) {
4852 *data = nla_get_u32(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]);
4853 }
4854 }
4855
4856 return NL_OK;
4857}
4858
developer72fb0bb2023-01-11 09:46:29 +08004859//Get the ACS refresh period in seconds
4860INT wifi_getRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG *output_ulong) //Tr181
4861{
developer326d4232023-06-15 16:45:30 +08004862 char interface_name[IF_NAME_SIZE] = {0};
4863 int ret = -1;
4864 unsigned int if_idx = 0;
4865 struct unl unl_ins;
4866 struct nl_msg *msg = NULL;
4867 struct nlattr * msg_data = NULL;
4868 struct mtk_nl80211_param param;
4869 unsigned long checktime = 0;
4870
4871 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004872 if (NULL == output_ulong)
4873 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004874
4875 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4876 return RETURN_ERR;
4877
4878 if_idx = if_nametoindex(interface_name);
4879 if (!if_idx) {
4880 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4881 return RETURN_ERR;
4882 }
4883 /*init mtk nl80211 vendor cmd*/
4884 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
4885 param.if_type = NL80211_ATTR_IFINDEX;
4886 param.if_idx = if_idx;
developer72fb0bb2023-01-11 09:46:29 +08004887
developer326d4232023-06-15 16:45:30 +08004888 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4889 if (ret) {
4890 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4891 return RETURN_ERR;
4892 }
4893
4894 /*add mtk vendor cmd data*/
4895 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD, 0)) {
4896 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD attribute error\n");
4897 nlmsg_free(msg);
4898 goto err;
4899 }
4900
4901 /*send mtk nl80211 vendor msg*/
4902 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_ACS_RefreshPeriod_callback, &checktime);
4903
4904 if (ret) {
4905 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4906 goto err;
4907 }
4908 /*deinit mtk nl80211 vendor msg*/
4909 mtk_nl80211_deint(&unl_ins);
4910 *output_ulong = checktime;
4911 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4912
4913 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004914 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004915err:
4916 mtk_nl80211_deint(&unl_ins);
4917 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
4918 return RETURN_ERR;
4919
developer72fb0bb2023-01-11 09:46:29 +08004920}
4921
4922//Set the ACS refresh period in seconds
4923INT wifi_setRadioDfsRefreshPeriod(INT radioIndex, ULONG seconds) //Tr181
4924{
developer326d4232023-06-15 16:45:30 +08004925 char interface_name[IF_NAME_SIZE] = {0};
4926 int ret = -1;
4927 unsigned int if_idx = 0;
4928 struct unl unl_ins;
4929 struct nl_msg *msg = NULL;
4930 struct nlattr * msg_data = NULL;
4931 struct mtk_nl80211_param param;
4932
4933 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4934
4935 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4936 return RETURN_ERR;
4937
4938 if_idx = if_nametoindex(interface_name);
4939 if (!if_idx) {
4940 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4941 return RETURN_ERR;
4942 }
4943 /*init mtk nl80211 vendor cmd*/
4944 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AUTO_CH_SEL;
4945 param.if_type = NL80211_ATTR_IFINDEX;
4946 param.if_idx = if_idx;
4947
4948 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4949 if (ret) {
4950 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4951 return RETURN_ERR;
4952 }
4953
4954 /*add mtk vendor cmd data*/
4955 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME, seconds)) {
4956 wifi_debug(DEBUG_ERROR, "Nla put MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME attribute error\n");
4957 nlmsg_free(msg);
4958 goto err;
4959 }
4960
4961 /*send mtk nl80211 vendor msg*/
4962 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
4963
4964 if (ret) {
4965 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4966 goto err;
4967 }
4968 /*deinit mtk nl80211 vendor msg*/
4969 mtk_nl80211_deint(&unl_ins);
4970 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4971
4972 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4973 return RETURN_OK;
4974err:
4975 mtk_nl80211_deint(&unl_ins);
4976 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developera3511852023-06-14 14:12:59 +08004977 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004978}
4979
4980//Get the Operating Channel Bandwidth. eg "20MHz", "40MHz", "80MHz", "80+80", "160"
4981//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
4982INT wifi_getRadioOperatingChannelBandwidth(INT radioIndex, CHAR *output_string) //Tr181
4983{
developera3511852023-06-14 14:12:59 +08004984 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
4985 char extchannel[128] = {0};
4986 char interface_name[64] = {0};
developere40952c2023-06-15 18:46:43 +08004987 int ret = 0, len=0, res;
developera3511852023-06-14 14:12:59 +08004988 BOOL radio_enable = FALSE;
4989 wifi_band band;
developer72fb0bb2023-01-11 09:46:29 +08004990
developera3511852023-06-14 14:12:59 +08004991 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004992
developera3511852023-06-14 14:12:59 +08004993 if (NULL == output_string) {
4994 WIFI_ENTRY_EXIT_DEBUG("output_string is nuill %s: %d \n", __func__, __LINE__);
4995 return RETURN_ERR;
4996 }
4997 if (wifi_getRadioEnable(radioIndex, &radio_enable) == RETURN_ERR) {
4998 WIFI_ENTRY_EXIT_DEBUG("wifi_getRadioEnable failed %s: %d \n", __func__, __LINE__);
4999 return RETURN_ERR;
5000 }
5001 if (radio_enable != TRUE) {
5002 WIFI_ENTRY_EXIT_DEBUG("Radio %d is not enable failed %s: %d \n", radioIndex, __func__, __LINE__);
5003 return RETURN_OK;
5004 }
5005 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5006 return RETURN_ERR;
5007 /*IW command get BW320 to do*/
developerd1824452023-05-18 12:30:04 +08005008
developere40952c2023-06-15 18:46:43 +08005009 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'width' | cut -d ' ' -f6 | tr -d '\\n'", interface_name);
5010 if (os_snprintf_error(sizeof(cmd), res)) {
5011 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5012 return RETURN_ERR;
5013 }
developera3511852023-06-14 14:12:59 +08005014 ret = _syscmd(cmd, buf, sizeof(buf));
5015 len = strlen(buf);
5016 if((ret != 0) || (len == 0))
5017 {
5018 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
5019 return RETURN_ERR;
5020 }
developer8666b312023-03-24 14:05:31 +08005021
developera3511852023-06-14 14:12:59 +08005022 band = wifi_index_to_band(radioIndex);
5023 if (band == band_2_4 && strncmp(buf, "20", 2) == 0) {
5024 wifi_getRadioExtChannel(radioIndex, extchannel);
developere40952c2023-06-15 18:46:43 +08005025 if (strncmp(extchannel, "Auto", 4) != 0) {
5026 res = snprintf(buf, sizeof(buf), "40");
5027 if (os_snprintf_error(sizeof(buf), res)) {
5028 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5029 return RETURN_ERR;
5030 }
5031 }
5032 }
5033 res = snprintf(output_string, 64, "%sMHz", buf);
5034 if (os_snprintf_error(64, res)) {
5035 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5036 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08005037 }
developera3511852023-06-14 14:12:59 +08005038 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005039
developera3511852023-06-14 14:12:59 +08005040 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005041}
5042
5043enum mwctl_chan_width {
5044 MWCTL_CHAN_WIDTH_20,
5045 MWCTL_CHAN_WIDTH_40,
5046 MWCTL_CHAN_WIDTH_80,
5047 MWCTL_CHAN_WIDTH_160,
5048 MWCTL_CHAN_WIDTH_320,
5049};
5050
5051struct bw_option {
5052 unsigned int bandwith;
5053 enum mwctl_chan_width mode;
5054};
5055
5056struct bw_option bw_opt[] = {
5057 {20, MWCTL_CHAN_WIDTH_20},
5058 {40, MWCTL_CHAN_WIDTH_40},
5059 {80, MWCTL_CHAN_WIDTH_80},
5060 {160, MWCTL_CHAN_WIDTH_160},
5061 {320, MWCTL_CHAN_WIDTH_320},
5062};
5063
5064INT wifi_setChannel_netlink(INT radioIndex, UINT* channel, UINT *bandwidth)
5065{
5066 int ret = -1;
5067 int i;
5068 struct unl unl_ins;
5069 struct nl_msg *msg = NULL;
5070 struct nlattr * msg_data = NULL;
5071 struct mtk_nl80211_param param;
5072 bool b_match = FALSE;
5073
5074 /*init mtk nl80211 vendor cmd*/
5075 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_CHANNEL;
5076 param.if_type = NL80211_ATTR_WIPHY;
5077 param.if_idx = radio_index_to_phy(radioIndex);
5078
5079 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5080 if (ret) {
5081 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5082 return RETURN_ERR;
5083 }
5084
5085 /*add mtk vendor cmd data*/
5086 if (channel != NULL)
5087 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_NUM, *channel)) {
5088 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_NUM attribute error\n");
5089 nlmsg_free(msg);
5090 goto err;
5091 }
5092
5093 if (bandwidth != NULL) {
5094 for (i = 0; i < (sizeof(bw_opt)/sizeof(bw_opt[0])); i++) {
5095 if (bw_opt[i].bandwith == *bandwidth) {
5096 b_match = true;
5097 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_BW, bw_opt[i].mode)) {
5098 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_BW attribute error\n");
5099 nlmsg_free(msg);
5100 goto err;
5101 }
5102 break;
5103 }
5104 }
5105
5106 if (!b_match) {
5107 wifi_debug(DEBUG_ERROR, "Cannot find bandwith error\n");
5108 nlmsg_free(msg);
5109 goto err;
5110 }
5111 }
5112
5113 /*send mtk nl80211 vendor msg*/
5114 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5115 if (ret) {
5116 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5117 goto err;
5118 }
5119 /*deinit mtk nl80211 vendor msg*/
5120 mtk_nl80211_deint(&unl_ins);
5121 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developera3511852023-06-14 14:12:59 +08005122 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developerfead3972023-05-25 20:15:02 +08005123
developera3511852023-06-14 14:12:59 +08005124 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005125err:
5126 mtk_nl80211_deint(&unl_ins);
5127 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5128 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005129}
developerfead3972023-05-25 20:15:02 +08005130
developer72fb0bb2023-01-11 09:46:29 +08005131//Set the Operating Channel Bandwidth.
5132INT wifi_setRadioOperatingChannelBandwidth(INT radioIndex, CHAR *bandwidth) //Tr181 //AP only
5133{
developera3511852023-06-14 14:12:59 +08005134 char config_file[128];
5135 char ht_value[16];
5136 char vht_value[16];
5137 char eht_value[16];
5138 struct params dat[3];
5139 wifi_band band = band_invalid;
5140 unsigned int bw = 20;
developere40952c2023-06-15 18:46:43 +08005141 int ret = 0, res1, res2, res3;
developer72fb0bb2023-01-11 09:46:29 +08005142
developera3511852023-06-14 14:12:59 +08005143 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005144
developera3511852023-06-14 14:12:59 +08005145 if(NULL == bandwidth)
5146 return RETURN_ERR;
5147 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08005148
developera3511852023-06-14 14:12:59 +08005149 if(strstr(bandwidth,"320") != NULL) {
developere40952c2023-06-15 18:46:43 +08005150 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5151 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5152 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_320);
developera3511852023-06-14 14:12:59 +08005153 bw = 320;
5154 } else if(strstr(bandwidth,"160") != NULL) {
developere40952c2023-06-15 18:46:43 +08005155 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5156 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5157 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_160);
developera3511852023-06-14 14:12:59 +08005158 bw = 160;
5159 } else if(strstr(bandwidth,"80") != NULL) {
developere40952c2023-06-15 18:46:43 +08005160 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5161 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_80);
5162 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_80);
developera3511852023-06-14 14:12:59 +08005163 bw = 80;
5164 } else if(strstr(bandwidth,"40") != NULL) {
developere40952c2023-06-15 18:46:43 +08005165 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5166 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5167 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_40);
developera3511852023-06-14 14:12:59 +08005168 bw = 40;
5169 } else if(strstr(bandwidth,"20") != NULL) {
developere40952c2023-06-15 18:46:43 +08005170 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_20);
5171 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5172 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_20);
developera3511852023-06-14 14:12:59 +08005173 bw = 20;
5174 } else {
developer37646972023-06-29 10:58:43 +08005175 if (fprintf(stderr, "%s: Invalid Bandwidth %s\n", __func__, bandwidth) < 0)
5176 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005177 return RETURN_ERR;
5178 }
developer72fb0bb2023-01-11 09:46:29 +08005179
developer37646972023-06-29 10:58:43 +08005180 if (os_snprintf_error(sizeof(ht_value), res1) ||
5181 os_snprintf_error(sizeof(vht_value), res2) ||
5182 os_snprintf_error(sizeof(eht_value), res3)) {
developere40952c2023-06-15 18:46:43 +08005183 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5184 return RETURN_ERR;
5185 }
5186
5187 res1 = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5188 if (os_snprintf_error(sizeof(config_file), res1)) {
5189 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5190 return RETURN_ERR;
5191 }
developera3511852023-06-14 14:12:59 +08005192 dat[0].name = "HT_BW";
5193 dat[0].value = ht_value;
5194 dat[1].name = "VHT_BW";
5195 dat[1].value = vht_value;
5196 dat[2].name = "EHT_ApBw";
5197 dat[2].value = eht_value;
5198 wifi_datfileWrite(config_file, dat, 3);
5199 ret = wifi_setChannel_netlink(radioIndex, NULL, &bw);
developerfead3972023-05-25 20:15:02 +08005200 if (ret != RETURN_OK) {
developer37646972023-06-29 10:58:43 +08005201 if (fprintf(stderr, "%s: wifi_setChannel return error.\n", __func__) < 0)
5202 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005203 return RETURN_ERR;
5204 }
developer72fb0bb2023-01-11 09:46:29 +08005205
developera3511852023-06-14 14:12:59 +08005206 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5207 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005208}
5209
developer72fb0bb2023-01-11 09:46:29 +08005210//Get the secondary extension channel position, "AboveControlChannel" or "BelowControlChannel". (this is for 40MHz and 80MHz bandwith only)
5211//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
5212INT wifi_getRadioExtChannel(INT radioIndex, CHAR *output_string) //Tr181
5213{
developera3511852023-06-14 14:12:59 +08005214 char config_file[64] = {0};
5215 char config_dat_file[64] = {0};
5216 char mode_str[16] = {0};
5217 char buf[64] = {0};
5218 char cmd[MAX_CMD_SIZE] = {0};
5219 char interface_name[64] = {0};
5220 int ret = 0, len=0;
5221 wifi_band band;
5222 ULONG channel = 0;
5223 int centr_channel = 0;
5224 UINT mode_map = 0;
developere40952c2023-06-15 18:46:43 +08005225 int freq=0, res;
developer72fb0bb2023-01-11 09:46:29 +08005226
developera3511852023-06-14 14:12:59 +08005227 if (output_string == NULL)
5228 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005229
developer86035662023-06-28 19:21:12 +08005230 if (wifi_getRadioMode(radioIndex, mode_str, &mode_map) != RETURN_OK) {
5231 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode fail\n");
5232 }
developer72fb0bb2023-01-11 09:46:29 +08005233
developera3511852023-06-14 14:12:59 +08005234 band = wifi_index_to_band(radioIndex);
5235 if (band == band_invalid)
5236 return RETURN_ERR;
5237 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5238 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005239
developere40952c2023-06-15 18:46:43 +08005240 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5241 if (os_snprintf_error(sizeof(config_file), res)) {
5242 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5243 return RETURN_ERR;
5244 }
5245
5246 res = snprintf(output_string, 64, "Auto");
5247 if (os_snprintf_error(64, res)) {
5248 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5249 return RETURN_ERR;
5250 }
developer72fb0bb2023-01-11 09:46:29 +08005251
developera3511852023-06-14 14:12:59 +08005252 if (band == band_2_4 || (!(mode_map&WIFI_MODE_AC) && !(mode_map&WIFI_MODE_AX))) {
5253 // 2G band or ac and ax mode is disable, we will check HT_EXTCHA
developere40952c2023-06-15 18:46:43 +08005254 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5255 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5256 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5257 return RETURN_ERR;
5258 }
developerb758dfd2023-06-21 17:32:07 +08005259
developera3511852023-06-14 14:12:59 +08005260 wifi_halgetRadioExtChannel(config_dat_file, output_string);
developer37646972023-06-29 10:58:43 +08005261 if (!(mode_map&WIFI_MODE_N)) {
developere40952c2023-06-15 18:46:43 +08005262 res = snprintf(output_string, 64, "Auto");
developer37646972023-06-29 10:58:43 +08005263 if (os_snprintf_error(64, res)) {
5264 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5265 return RETURN_ERR;
5266 }
5267 }
developera3511852023-06-14 14:12:59 +08005268 } else {
5269 // 5G and 6G band with ac or ax mode.
5270 wifi_getRadioChannel(radioIndex, &channel);
developere40952c2023-06-15 18:46:43 +08005271 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'center1' | cut -d ' ' -f9 | tr -d '\\n'", interface_name);
5272 if (os_snprintf_error(sizeof(cmd), res)) {
5273 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5274 return RETURN_ERR;
5275 }
developerd1824452023-05-18 12:30:04 +08005276
developera3511852023-06-14 14:12:59 +08005277 ret = _syscmd(cmd, buf, sizeof(buf));
5278 len = strlen(buf);
5279 if((ret != 0) || (len == 0))
5280 {
5281 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
5282 return RETURN_ERR;
5283 }
5284 sscanf(buf, "%d", &freq);
5285 centr_channel = ieee80211_frequency_to_channel(freq);
5286 if (centr_channel > (int)channel)
developere40952c2023-06-15 18:46:43 +08005287 res = snprintf(output_string, 64, "AboveControlChannel");
developera3511852023-06-14 14:12:59 +08005288 else
developere40952c2023-06-15 18:46:43 +08005289 res = snprintf(output_string, 64, "BelowControlChannel");
5290
5291 if (os_snprintf_error(64, res)) {
5292 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5293 return RETURN_ERR;
5294 }
developera3511852023-06-14 14:12:59 +08005295 }
developer72fb0bb2023-01-11 09:46:29 +08005296
developera3511852023-06-14 14:12:59 +08005297 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005298}
5299
5300//Set the extension channel.
5301INT wifi_setRadioExtChannel(INT radioIndex, CHAR *string) //Tr181 //AP only
developer69b61b02023-03-07 17:17:44 +08005302{
developera3511852023-06-14 14:12:59 +08005303 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5304 struct params params={0};
5305 char config_file[64] = {0};
5306 char config_dat_file[64] = {0};
5307 char ext_channel[64] = {0};
5308 char buf[128] = {0};
5309 char cmd[128] = {0};
developerc14d83a2023-06-29 20:09:42 +08005310 int max_radio_num =0, ret = 0;
5311 long int bandwidth = 0;
developera3511852023-06-14 14:12:59 +08005312 unsigned long channel = 0;
5313 bool stbcEnable = FALSE;
5314 params.name = "ht_capab";
5315 wifi_band band;
developere40952c2023-06-15 18:46:43 +08005316 int res;
developer72fb0bb2023-01-11 09:46:29 +08005317
developer75bd10c2023-06-27 11:34:08 +08005318 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5319 if (os_snprintf_error(sizeof(config_file), res)) {
5320 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5321 return RETURN_ERR;
5322 }
5323
developere40952c2023-06-15 18:46:43 +08005324 res = snprintf(cmd, sizeof(cmd), "cat %s | grep STBC", config_file);
5325 if (os_snprintf_error(sizeof(cmd), res)) {
5326 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5327 return RETURN_ERR;
5328 }
developera3511852023-06-14 14:12:59 +08005329 _syscmd(cmd, buf, sizeof(buf));
5330 if (strlen(buf) != 0)
5331 stbcEnable = TRUE;
5332 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, buf) != RETURN_OK)
5333 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08005334 if (hal_strtol(buf, 10, &bandwidth) < 0) {
5335 wifi_debug(DEBUG_ERROR, "strtol fail\n");
5336 }
developera3511852023-06-14 14:12:59 +08005337 // TDK expected to get error with 20MHz
5338 // we handle 20MHz in function wifi_RemoveRadioExtChannel().
5339 if (bandwidth == 20 || strstr(buf, "80+80") != NULL)
5340 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005341
developera3511852023-06-14 14:12:59 +08005342 band = wifi_index_to_band(radioIndex);
5343 if (band == band_invalid)
5344 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005345
developera3511852023-06-14 14:12:59 +08005346 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK)
5347 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005348
developerc14d83a2023-06-29 20:09:42 +08005349 res = snprintf(buf, sizeof(buf), "HT%ld", bandwidth);
developere40952c2023-06-15 18:46:43 +08005350 if (os_snprintf_error(sizeof(buf), res)) {
5351 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5352 return RETURN_ERR;
5353 }
developera3511852023-06-14 14:12:59 +08005354 ret = util_get_sec_chan_offset(channel, buf);
5355 if (ret == -EINVAL)
5356 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005357
developera3511852023-06-14 14:12:59 +08005358 if(NULL!= strstr(string,"Above")) {
5359 if ((band == band_2_4 && channel > 9) || (band == band_5 && ret == -1))
5360 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005361 memcpy(ext_channel, "1", strlen("1"));
developera3511852023-06-14 14:12:59 +08005362 } else if(NULL!= strstr(string,"Below")) {
5363 if ((band == band_2_4 && channel < 5) || (band == band_5 && ret == -1))
5364 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005365 memcpy(ext_channel, "0", strlen("0"));
developera3511852023-06-14 14:12:59 +08005366 } else {
5367 printf("%s: invalid EXT_CHA:%s\n", __func__, string);
developer262f4cb2023-05-24 12:22:04 +08005368 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08005369 }
5370 params.name = "HT_EXTCHA";
5371 params.value = ext_channel;
developer72fb0bb2023-01-11 09:46:29 +08005372
developere75ba632023-06-29 16:03:33 +08005373 res = snprintf (config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08005374 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5375 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5376 return RETURN_ERR;
5377 }
developera3511852023-06-14 14:12:59 +08005378 wifi_datfileWrite(config_dat_file, &params, 1);
developerd1824452023-05-18 12:30:04 +08005379
developera3511852023-06-14 14:12:59 +08005380 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08005381 if(max_radio_num== 0){
5382 return RETURN_ERR;
5383 }
developera3511852023-06-14 14:12:59 +08005384 for(int i=0; i<=MAX_APS/max_radio_num; i++)
5385 {
developer32f2a182023-06-27 19:50:41 +08005386 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex+(max_radio_num*i));
5387 if (os_snprintf_error(sizeof(config_file), res)) {
5388 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5389 return RETURN_ERR;
5390 }
developera3511852023-06-14 14:12:59 +08005391 wifi_setRadioSTBCEnable(radioIndex+(max_radio_num*i), stbcEnable);
5392 }
developer72fb0bb2023-01-11 09:46:29 +08005393
developera3511852023-06-14 14:12:59 +08005394 //Set to wifi config only. Wait for wifi reset or wifi_pushRadioChannel to apply.
5395 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5396 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005397}
5398
5399//Get the guard interval value. eg "400nsec" or "800nsec"
5400//The output_string is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
5401INT wifi_getRadioGuardInterval(INT radioIndex, CHAR *output_string) //Tr181
5402{
developera3511852023-06-14 14:12:59 +08005403 wifi_guard_interval_t GI;
developer32f2a182023-06-27 19:50:41 +08005404 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08005405
developera3511852023-06-14 14:12:59 +08005406 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005407
developera3511852023-06-14 14:12:59 +08005408 if (output_string == NULL || wifi_getGuardInterval(radioIndex, &GI) == RETURN_ERR)
5409 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005410
developer32f2a182023-06-27 19:50:41 +08005411 if (GI == wifi_guard_interval_400) {
5412 len = strlen("400nsec");
5413 memcpy(output_string, "400nsec", len);
developer5b23cd02023-07-19 20:26:03 +08005414 } else if (GI == wifi_guard_interval_800) {
developer32f2a182023-06-27 19:50:41 +08005415 len = strlen("800nsec");
5416 memcpy(output_string, "800nsec", strlen("800nsec"));
5417 } else if (GI == wifi_guard_interval_1600) {
5418 len = strlen("1600nsec");
5419 memcpy(output_string, "1600nsec", strlen("1600nsec"));
5420 } else if (GI == wifi_guard_interval_3200) {
5421 len = strlen("3200nsec");
5422 memcpy(output_string, "3200nsec", strlen("3200nsec"));
5423 } else {
5424 len = strlen("Auto");
5425 memcpy(output_string, "Auto", strlen("Auto"));
5426 }
5427 output_string[len] = '\0';
developera3511852023-06-14 14:12:59 +08005428 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5429 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005430}
5431
5432//Set the guard interval value.
5433INT wifi_setRadioGuardInterval(INT radioIndex, CHAR *string) //Tr181
5434{
developera3511852023-06-14 14:12:59 +08005435 wifi_guard_interval_t GI;
5436 int ret = 0;
developer72fb0bb2023-01-11 09:46:29 +08005437
developera3511852023-06-14 14:12:59 +08005438 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005439
developera3511852023-06-14 14:12:59 +08005440 if (strcmp(string, "400nsec") == 0)
5441 GI = wifi_guard_interval_400;
5442 else if (strcmp(string , "800nsec") == 0)
5443 GI = wifi_guard_interval_800;
5444 else if (strcmp(string , "1600nsec") == 0)
5445 GI = wifi_guard_interval_1600;
5446 else if (strcmp(string , "3200nsec") == 0)
5447 GI = wifi_guard_interval_3200;
5448 else
5449 GI = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +08005450
developera3511852023-06-14 14:12:59 +08005451 ret = wifi_setGuardInterval(radioIndex, GI);
developer72fb0bb2023-01-11 09:46:29 +08005452
developera3511852023-06-14 14:12:59 +08005453 if (ret == RETURN_ERR) {
5454 wifi_dbg_printf("%s: wifi_setGuardInterval return error\n", __func__);
5455 return RETURN_ERR;
5456 }
developer72fb0bb2023-01-11 09:46:29 +08005457
developera3511852023-06-14 14:12:59 +08005458 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5459 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005460}
5461
5462//Get the Modulation Coding Scheme index, eg: "-1", "1", "15"
5463INT wifi_getRadioMCS(INT radioIndex, INT *output_int) //Tr181
5464{
developera3511852023-06-14 14:12:59 +08005465 char buf[32]={0};
5466 char mcs_file[64] = {0};
5467 char cmd[MAX_CMD_SIZE] = {0};
5468 UINT mode_bitmap = 0;
developere40952c2023-06-15 18:46:43 +08005469 int res;
developerc14d83a2023-06-29 20:09:42 +08005470 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08005471
developera3511852023-06-14 14:12:59 +08005472 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5473 if(output_int == NULL)
5474 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005475 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5476 if (os_snprintf_error(sizeof(mcs_file), res)) {
5477 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5478 return RETURN_ERR;
5479 }
5480
5481 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", mcs_file);
5482 if (os_snprintf_error(sizeof(cmd), res)) {
5483 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5484 return RETURN_ERR;
5485 }
developer72fb0bb2023-01-11 09:46:29 +08005486
developera3511852023-06-14 14:12:59 +08005487 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08005488 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005489 if (hal_strtol(buf, 10, &tmp) < 0) {
5490 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005491 }
developerc14d83a2023-06-29 20:09:42 +08005492 *output_int = tmp;
developerd14dff12023-06-28 22:47:44 +08005493 } else {
developera3511852023-06-14 14:12:59 +08005494 // output the max MCS for the current radio mode
5495 if (wifi_getRadioMode(radioIndex, buf, &mode_bitmap) == RETURN_ERR) {
5496 wifi_dbg_printf("%s: wifi_getradiomode return error.\n", __func__);
5497 return RETURN_ERR;
5498 }
5499 if (mode_bitmap & WIFI_MODE_AX) {
5500 *output_int = 11;
5501 } else if (mode_bitmap & WIFI_MODE_AC) {
5502 *output_int = 9;
5503 } else if (mode_bitmap & WIFI_MODE_N) {
5504 *output_int = 7;
5505 }
5506 }
5507 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005508
developera3511852023-06-14 14:12:59 +08005509 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005510}
5511
5512//Set the Modulation Coding Scheme index
5513INT wifi_setRadioMCS(INT radioIndex, INT MCS) //Tr181
5514{
developera3511852023-06-14 14:12:59 +08005515 /*Only HE mode can specify MCS capability. We don't support MCS in HT mode,
5516 because that would be ambiguous (MCS code 8~11 refer to 2 NSS in HT but 1 NSS in HE adn VHT).*/
5517 char config_file[64] = {0};
5518 char set_value[16] = {0};
5519 char mcs_file[32] = {0};
5520 struct params set_config = {0};
5521 FILE *f = NULL;
5522 INT nss = 0;
5523 int ant_bitmap = 0;
5524 unsigned short cal_value = 0;
5525 UCHAR tval = 0, i = 0;
developere40952c2023-06-15 18:46:43 +08005526 int res;
developer72fb0bb2023-01-11 09:46:29 +08005527
developera3511852023-06-14 14:12:59 +08005528 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005529
developere40952c2023-06-15 18:46:43 +08005530 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5531 if (os_snprintf_error(sizeof(config_file), res)) {
5532 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5533 return RETURN_ERR;
5534 }
developer72fb0bb2023-01-11 09:46:29 +08005535
developera3511852023-06-14 14:12:59 +08005536 // -1 means auto
5537 if (MCS > 15 || MCS < -1) {
developer75bd10c2023-06-27 11:34:08 +08005538 wifi_debug(DEBUG_ERROR, "invalid MCS %d\n", MCS);
developera3511852023-06-14 14:12:59 +08005539 return RETURN_ERR;
5540 }
5541 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);/*nss is a bit map value,1111*/
5542 for(; ant_bitmap > 0; ant_bitmap >>= 1)
5543 nss += 1;
5544 //printf("%s:nss = %d\n", __func__, nss);
5545 /*16-bit combination of 2-bit values of Max HE-MCS For 1..8 SS;each 2-bit value have following meaning:
5546 0 = HE-MCS 0-7, 1 = HE-MCS 0-9, 2 = HE-MCS 0-11, 3 = not supported*/
5547 if (MCS > 9 || MCS == -1)
5548 tval = 2;/*one stream value*/
5549 else if (MCS > 7)
5550 tval = 1;
5551 else
5552 tval = 0;
5553 for (i = 0; i < nss; i++)
5554 cal_value |= (tval << (2*i));
developere40952c2023-06-15 18:46:43 +08005555 res = snprintf(set_value, sizeof(set_value), "%x", cal_value);
5556 if (os_snprintf_error(sizeof(set_value), res)) {
5557 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5558 return RETURN_ERR;
5559 }
5560
developera3511852023-06-14 14:12:59 +08005561 WIFI_ENTRY_EXIT_DEBUG("%s:set=%s, cal=%x\n", __func__, set_value, cal_value);
5562 set_config.name = "he_basic_mcs_nss_set";/*He capability in beacon or response*/
5563 set_config.value = set_value;
developer72fb0bb2023-01-11 09:46:29 +08005564
developera3511852023-06-14 14:12:59 +08005565 wifi_hostapdWrite(config_file, &set_config, 1);
5566 wifi_hostapdProcessUpdate(radioIndex, &set_config, 1);
developer72fb0bb2023-01-11 09:46:29 +08005567
developera3511852023-06-14 14:12:59 +08005568 // For pass tdk test, we need to record last MCS setting. No matter whether it is effective or not.
developere40952c2023-06-15 18:46:43 +08005569 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5570 if (os_snprintf_error(sizeof(mcs_file), res)) {
5571 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5572 return RETURN_ERR;
5573 }
5574
developera3511852023-06-14 14:12:59 +08005575 f = fopen(mcs_file, "w");
5576 if (f == NULL) {
developere75ba632023-06-29 16:03:33 +08005577 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
5578 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005579 return RETURN_ERR;
5580 }
developere75ba632023-06-29 16:03:33 +08005581 if (fprintf(f, "%d", MCS) < 0)
5582 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
5583 if (fclose(f) == EOF) {
5584 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
5585 return RETURN_ERR;
5586 }
developer72fb0bb2023-01-11 09:46:29 +08005587
developera3511852023-06-14 14:12:59 +08005588 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5589 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005590}
5591
5592//Get supported Transmit Power list, eg : "0,25,50,75,100"
5593//The output_list is a max length 64 octet string that is allocated by the RDKB code. Implementations must ensure that strings are not longer than this.
5594INT wifi_getRadioTransmitPowerSupported(INT radioIndex, CHAR *output_list) //Tr181
5595{
developere40952c2023-06-15 18:46:43 +08005596 int res;
5597 if (NULL == output_list)
5598 return RETURN_ERR;
5599 res = snprintf(output_list, 64,"0,25,50,75,100");
5600 if (os_snprintf_error(64, res)) {
5601 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5602 return RETURN_ERR;
5603 }
5604
5605 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005606}
5607
5608//Get current Transmit Power in dBm units.
5609//The transmite power level is in units of full power for this radio.
5610INT wifi_getRadioTransmitPower(INT radioIndex, ULONG *output_ulong) //RDKB
5611{
developera3511852023-06-14 14:12:59 +08005612 char interface_name[16] = {0};
5613 char cmd[MAX_CMD_SIZE]={0};
5614 char buf[16]={0};
developera1255e42023-05-13 17:45:02 +08005615 char pwr_file[128]={0};
developere40952c2023-06-15 18:46:43 +08005616 int res;
developera1255e42023-05-13 17:45:02 +08005617
developera3511852023-06-14 14:12:59 +08005618 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005619
developera3511852023-06-14 14:12:59 +08005620 if(output_ulong == NULL)
5621 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005622
developera3511852023-06-14 14:12:59 +08005623 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5624 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005625 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5626 if (os_snprintf_error(sizeof(pwr_file), res)) {
5627 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5628 return RETURN_ERR;
5629 }
5630
5631 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", pwr_file);
5632 if (os_snprintf_error(sizeof(cmd), res)) {
5633 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5634 return RETURN_ERR;
5635 }
5636
developera1255e42023-05-13 17:45:02 +08005637 _syscmd(cmd, buf, sizeof(buf));
developer5b23cd02023-07-19 20:26:03 +08005638 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005639 if (hal_strtoul(buf, 10, output_ulong) < 0) {
5640 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005641 }
5642 } else
developera1255e42023-05-13 17:45:02 +08005643 *output_ulong = 100;
developera3511852023-06-14 14:12:59 +08005644 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5645 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005646}
5647
5648//Set Transmit Power
5649//The transmite power level is in units of full power for this radio.
5650INT wifi_setRadioTransmitPower(INT radioIndex, ULONG TransmitPower) //RDKB
5651{
developera3511852023-06-14 14:12:59 +08005652 char interface_name[16] = {0};
5653 char *support;
5654 char buf[128]={0};
5655 char txpower_str[64] = {0};
developera1255e42023-05-13 17:45:02 +08005656 char pwr_file[128]={0};
developera3511852023-06-14 14:12:59 +08005657 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +08005658 int if_idx, ret = 0;
5659 struct nl_msg *msg = NULL;
5660 struct nlattr * msg_data = NULL;
5661 struct mtk_nl80211_param param;
5662 struct unl unl_ins;
developere40952c2023-06-15 18:46:43 +08005663 int res;
developer72fb0bb2023-01-11 09:46:29 +08005664
developera3511852023-06-14 14:12:59 +08005665 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005666
developera3511852023-06-14 14:12:59 +08005667 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5668 return RETURN_ERR;
5669 // Get the Tx power supported list and check that is the input in the list
developere40952c2023-06-15 18:46:43 +08005670 res = snprintf(txpower_str, sizeof(txpower_str), "%lu", TransmitPower);
5671 if (os_snprintf_error(sizeof(txpower_str), res)) {
5672 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5673 return RETURN_ERR;
5674 }
developera3511852023-06-14 14:12:59 +08005675 wifi_getRadioTransmitPowerSupported(radioIndex, buf);
5676 support = strtok(buf, ",");
5677 while(true)
5678 {
5679 if(support == NULL) { // input not in the list
5680 wifi_dbg_printf("Input value is invalid.\n");
5681 return RETURN_ERR;
5682 }
5683 if (strncmp(txpower_str, support, strlen(support)) == 0) {
5684 break;
5685 }
5686 support = strtok(NULL, ",");
5687 }
developerfead3972023-05-25 20:15:02 +08005688
5689 if_idx = if_nametoindex(interface_name);
5690 /*init mtk nl80211 vendor cmd*/
5691 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
5692 param.if_type = NL80211_ATTR_IFINDEX;
5693 param.if_idx = if_idx;
5694 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5695 if (ret) {
5696 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5697 return RETURN_ERR;
5698 }
5699 /*add mtk vendor cmd data*/
5700 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_PERCENTAGE_EN, 1)) {
5701 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5702 nlmsg_free(msg);
5703 goto err;
5704 }
5705
5706 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_DROP_CTRL, TransmitPower)) {
5707 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5708 nlmsg_free(msg);
5709 goto err;
5710 }
5711
5712 /*send mtk nl80211 vendor msg*/
5713 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5714 if (ret) {
5715 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5716 goto err;
5717 }
5718 /*deinit mtk nl80211 vendor msg*/
5719 mtk_nl80211_deint(&unl_ins);
5720 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
5721
developere40952c2023-06-15 18:46:43 +08005722 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5723 if (os_snprintf_error(sizeof(pwr_file), res)) {
5724 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5725 return RETURN_ERR;
5726 }
5727
developera3511852023-06-14 14:12:59 +08005728 f = fopen(pwr_file, "w");
5729 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +08005730 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera3511852023-06-14 14:12:59 +08005731 return RETURN_ERR;
5732 }
5733 fprintf(f, "%lu", TransmitPower);
developerc14d83a2023-06-29 20:09:42 +08005734 if (fclose(f) == EOF)
5735 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera1255e42023-05-13 17:45:02 +08005736 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005737err:
5738 mtk_nl80211_deint(&unl_ins);
5739 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5740 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005741}
5742
5743//get 80211h Supported. 80211h solves interference with satellites and radar using the same 5 GHz frequency band
5744INT wifi_getRadioIEEE80211hSupported(INT radioIndex, BOOL *Supported) //Tr181
5745{
developera3511852023-06-14 14:12:59 +08005746 if (NULL == Supported)
5747 return RETURN_ERR;
5748 *Supported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08005749
developera3511852023-06-14 14:12:59 +08005750 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005751}
5752
5753//Get 80211h feature enable
5754INT wifi_getRadioIEEE80211hEnabled(INT radioIndex, BOOL *enable) //Tr181
5755{
developera3511852023-06-14 14:12:59 +08005756 char buf[64]={'\0'};
5757 char config_file[64] = {'\0'};
developer75bd10c2023-06-27 11:34:08 +08005758 int res;
developer72fb0bb2023-01-11 09:46:29 +08005759
developera3511852023-06-14 14:12:59 +08005760 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5761 if(enable == NULL)
5762 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005763
developer75bd10c2023-06-27 11:34:08 +08005764 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5765 if (os_snprintf_error(sizeof(config_file), res)) {
5766 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5767 return RETURN_ERR;
5768 }
developera3511852023-06-14 14:12:59 +08005769 /* wifi_hostapdRead(config_file, "ieee80211h", buf, sizeof(buf)); */
5770 wifi_datfileRead(config_file, "IEEE80211H", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08005771
developera3511852023-06-14 14:12:59 +08005772 if (strncmp(buf, "1", 1) == 0)
5773 *enable = TRUE;
5774 else
5775 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08005776
developera3511852023-06-14 14:12:59 +08005777 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5778 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005779}
5780
5781//Set 80211h feature enable
5782INT wifi_setRadioIEEE80211hEnabled(INT radioIndex, BOOL enable) //Tr181
5783{
developera3511852023-06-14 14:12:59 +08005784 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5785 struct params params={'\0'};
5786 struct params dat={0};
5787 char config_file[MAX_BUF_SIZE] = {0};
5788 char config_dat_file[MAX_BUF_SIZE] = {0};
5789 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08005790 int res;
developer72fb0bb2023-01-11 09:46:29 +08005791
developera3511852023-06-14 14:12:59 +08005792 params.name = "ieee80211h";
developer72fb0bb2023-01-11 09:46:29 +08005793
developera3511852023-06-14 14:12:59 +08005794 if (enable) {
5795 params.value = "1";
5796 } else {
5797 params.value = "0";
5798 }
developer72fb0bb2023-01-11 09:46:29 +08005799
developera3511852023-06-14 14:12:59 +08005800 dat.name = "IEEE80211H";
5801 dat.value = params.value;
developerd1824452023-05-18 12:30:04 +08005802
developera3511852023-06-14 14:12:59 +08005803 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08005804 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5805 if (os_snprintf_error(sizeof(config_file), res)) {
5806 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5807 return RETURN_ERR;
5808 }
5809
5810 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5811 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5812 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5813 return RETURN_ERR;
5814 }
developer69b61b02023-03-07 17:17:44 +08005815
developera3511852023-06-14 14:12:59 +08005816 wifi_hostapdWrite(config_file, &params, 1);
5817 wifi_datfileWrite(config_dat_file, &dat, 1);
5818 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5819 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5820 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005821}
5822
5823//Indicates the Carrier Sense ranges supported by the radio. It is measured in dBm. Refer section A.2.3.2 of CableLabs Wi-Fi MGMT Specification.
5824INT wifi_getRadioCarrierSenseThresholdRange(INT radioIndex, INT *output) //P3
5825{
developera3511852023-06-14 14:12:59 +08005826 if (NULL == output)
5827 return RETURN_ERR;
5828 *output=100;
developer72fb0bb2023-01-11 09:46:29 +08005829
developera3511852023-06-14 14:12:59 +08005830 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005831}
5832
5833//The RSSI signal level at which CS/CCA detects a busy condition. This attribute enables APs to increase minimum sensitivity to avoid detecting busy condition from multiple/weak Wi-Fi sources in dense Wi-Fi environments. It is measured in dBm. Refer section A.2.3.2 of CableLabs Wi-Fi MGMT Specification.
5834INT wifi_getRadioCarrierSenseThresholdInUse(INT radioIndex, INT *output) //P3
5835{
developera3511852023-06-14 14:12:59 +08005836 if (NULL == output)
5837 return RETURN_ERR;
5838 *output = -99;
developer72fb0bb2023-01-11 09:46:29 +08005839
developera3511852023-06-14 14:12:59 +08005840 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005841}
5842
5843INT wifi_setRadioCarrierSenseThresholdInUse(INT radioIndex, INT threshold) //P3
5844{
developera3511852023-06-14 14:12:59 +08005845 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005846}
5847
5848
5849//Time interval between transmitting beacons (expressed in milliseconds). This parameter is based ondot11BeaconPeriod from [802.11-2012].
5850INT wifi_getRadioBeaconPeriod(INT radioIndex, UINT *output)
5851{
developera3511852023-06-14 14:12:59 +08005852 char interface_name[16] = {0};
5853 char cmd[MAX_BUF_SIZE]={'\0'};
5854 char buf[MAX_CMD_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08005855 int res;
developer72fb0bb2023-01-11 09:46:29 +08005856
developera3511852023-06-14 14:12:59 +08005857 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5858 if(output == NULL)
5859 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005860
developera3511852023-06-14 14:12:59 +08005861 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5862 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005863 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s status | grep beacon_int | cut -d '=' -f2 | tr -d '\n'", interface_name);
5864 if (os_snprintf_error(sizeof(cmd), res)) {
5865 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5866 return RETURN_ERR;
5867 }
5868
developera3511852023-06-14 14:12:59 +08005869 _syscmd(cmd, buf, sizeof(buf));
5870 *output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +08005871
developera3511852023-06-14 14:12:59 +08005872 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5873 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005874}
developer69b61b02023-03-07 17:17:44 +08005875
developer72fb0bb2023-01-11 09:46:29 +08005876INT wifi_setRadioBeaconPeriod(INT radioIndex, UINT BeaconPeriod)
5877{
developera3511852023-06-14 14:12:59 +08005878 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5879 struct params params={'\0'};
5880 char buf[MAX_BUF_SIZE] = {'\0'};
5881 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08005882 int res;
developer72fb0bb2023-01-11 09:46:29 +08005883
developera3511852023-06-14 14:12:59 +08005884 if (BeaconPeriod < 15 || BeaconPeriod > 65535)
5885 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005886
developera3511852023-06-14 14:12:59 +08005887 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08005888 res = snprintf(buf, sizeof(buf), "%u", BeaconPeriod);
5889 if (os_snprintf_error(sizeof(buf), res)) {
5890 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5891 return RETURN_ERR;
5892 }
5893
developera3511852023-06-14 14:12:59 +08005894 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08005895
developer75bd10c2023-06-27 11:34:08 +08005896 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5897 if (os_snprintf_error(sizeof(config_file), res)) {
5898 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5899 return RETURN_ERR;
5900 }
developera3511852023-06-14 14:12:59 +08005901 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08005902
developera3511852023-06-14 14:12:59 +08005903 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5904 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5905 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005906}
5907
5908//Comma-separated list of strings. The set of data rates, in Mbps, that have to be supported by all stations that desire to join this BSS. The stations have to be able to receive and transmit at each of the data rates listed inBasicDataTransmitRates. For example, a value of "1,2", indicates that stations support 1 Mbps and 2 Mbps. Most control packets use a data rate in BasicDataTransmitRates.
5909INT wifi_getRadioBasicDataTransmitRates(INT radioIndex, CHAR *output)
5910{
developera3511852023-06-14 14:12:59 +08005911 //TODO: need to revisit below implementation
5912 char *temp;
5913 char temp_output[128] = {0};
5914 char temp_TransmitRates[64] = {0};
5915 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08005916 int res;
developer72fb0bb2023-01-11 09:46:29 +08005917
developera3511852023-06-14 14:12:59 +08005918 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5919 if (NULL == output)
5920 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08005921
5922 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5923 if (os_snprintf_error(sizeof(config_file), res)) {
5924 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5925 return RETURN_ERR;
5926 }
developera3511852023-06-14 14:12:59 +08005927 wifi_hostapdRead(config_file,"basic_rates",temp_TransmitRates,64);
developer69b61b02023-03-07 17:17:44 +08005928
developera3511852023-06-14 14:12:59 +08005929 if (strlen(temp_TransmitRates) == 0) { // config not set, use supported rate
5930 wifi_getRadioSupportedDataTransmitRates(radioIndex, output);
5931 } else {
5932 temp = strtok(temp_TransmitRates," ");
5933 while(temp!=NULL)
5934 {
5935 // Convert 100 kbps to Mbps
5936 temp[strlen(temp)-1]=0;
5937 if((temp[0]=='5') && (temp[1]=='\0'))
5938 {
5939 temp="5.5";
5940 }
developer32f2a182023-06-27 19:50:41 +08005941 if (strlen(temp) >= sizeof(temp_output))
5942 return RETURN_ERR;
5943 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005944 temp = strtok(NULL," ");
5945 if(temp!=NULL)
5946 {
developer32f2a182023-06-27 19:50:41 +08005947 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
5948 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005949 }
5950 }
developer32f2a182023-06-27 19:50:41 +08005951 memcpy(output, temp_output, strlen(temp_output));
5952 output[strlen(temp_output)] = '\0';
developera3511852023-06-14 14:12:59 +08005953 }
5954 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5955 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005956}
5957
5958INT wifi_setRadioBasicDataTransmitRates(INT radioIndex, CHAR *TransmitRates)
5959{
developera3511852023-06-14 14:12:59 +08005960 char *temp;
developer32f2a182023-06-27 19:50:41 +08005961 char temp1[128] = {0};
5962 char temp_output[128] = {0};
5963 char temp_TransmitRates[128] = {0};
5964 char set[128] = {0};
5965 char sub_set[128] = {0};
developera3511852023-06-14 14:12:59 +08005966 int set_count=0,subset_count=0;
5967 int set_index=0,subset_index=0;
5968 char *token;
5969 int flag=0, i=0;
5970 struct params params={'\0'};
5971 char config_file[MAX_BUF_SIZE] = {0};
5972 wifi_band band = wifi_index_to_band(radioIndex);
developer32f2a182023-06-27 19:50:41 +08005973 int res;
developer72fb0bb2023-01-11 09:46:29 +08005974
developera3511852023-06-14 14:12:59 +08005975 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5976 if(NULL == TransmitRates)
5977 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +08005978 if (strlen(TransmitRates) >= sizeof(sub_set))
5979 return RETURN_ERR;
5980
5981 memcpy(sub_set, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08005982
developera3511852023-06-14 14:12:59 +08005983 //Allow only supported Data transmit rate to be set
5984 wifi_getRadioSupportedDataTransmitRates(radioIndex,set);
5985 token = strtok(sub_set,",");
5986 while( token != NULL ) /* split the basic rate to be set, by comma */
5987 {
5988 sub_set[subset_count]=atoi(token);
5989 subset_count++;
5990 token=strtok(NULL,",");
5991 }
5992 token=strtok(set,",");
5993 while(token!=NULL) /* split the supported rate by comma */
5994 {
5995 set[set_count]=atoi(token);
5996 set_count++;
5997 token=strtok(NULL,",");
5998 }
5999 for(subset_index=0;subset_index < subset_count;subset_index++) /* Compare each element of subset and set */
6000 {
6001 for(set_index=0;set_index < set_count;set_index++)
6002 {
6003 flag=0;
6004 if(sub_set[subset_index]==set[set_index])
6005 break;
6006 else
6007 flag=1; /* No match found */
6008 }
6009 if(flag==1)
6010 return RETURN_ERR; //If value not found return Error
6011 }
developer5b23cd02023-07-19 20:26:03 +08006012
developer32f2a182023-06-27 19:50:41 +08006013 if (strlen(TransmitRates) >= sizeof(temp_TransmitRates))
6014 return RETURN_ERR;
6015
6016 memcpy(temp_TransmitRates, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08006017
developera3511852023-06-14 14:12:59 +08006018 for(i=0;i<strlen(temp_TransmitRates);i++)
6019 {
6020 //if (((temp_TransmitRates[i]>=48) && (temp_TransmitRates[i]<=57)) | (temp_TransmitRates[i]==32))
6021 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
6022 {
6023 continue;
6024 }
6025 else
6026 {
6027 return RETURN_ERR;
6028 }
6029 }
developera3511852023-06-14 14:12:59 +08006030 temp = strtok(temp_TransmitRates,",");
6031 while(temp!=NULL)
6032 {
developer32f2a182023-06-27 19:50:41 +08006033 if (strlen(temp) >= sizeof(temp1))
6034 return RETURN_ERR;
6035 strncpy(temp1, temp, strlen(temp));
developera3511852023-06-14 14:12:59 +08006036 if(band == band_5)
6037 {
6038 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
6039 {
6040 return RETURN_ERR;
6041 }
6042 }
developer72fb0bb2023-01-11 09:46:29 +08006043
developera3511852023-06-14 14:12:59 +08006044 if(strcmp(temp,"5.5")==0)
6045 {
developer32f2a182023-06-27 19:50:41 +08006046 memcpy(temp1, "55", 2);
developera3511852023-06-14 14:12:59 +08006047 }
6048 else
6049 {
developer32f2a182023-06-27 19:50:41 +08006050 if (strlen(temp1) >= (sizeof(temp1) - 1))
6051 return RETURN_ERR;
6052 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +08006053 }
developer32f2a182023-06-27 19:50:41 +08006054 if (strlen(temp1) >= (sizeof(temp_output) - strlen(temp_output)))
6055 return RETURN_ERR;
6056 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08006057 temp = strtok(NULL,",");
6058 if(temp!=NULL)
6059 {
developer32f2a182023-06-27 19:50:41 +08006060 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
6061 return RETURN_ERR;
6062 strncat(temp_output," ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08006063 }
6064 }
developer32f2a182023-06-27 19:50:41 +08006065 memcpy(TransmitRates, temp_output, strlen(temp_output));
6066 TransmitRates[strlen(temp_output)] = '\0';
developer5b23cd02023-07-19 20:26:03 +08006067
developera3511852023-06-14 14:12:59 +08006068 params.name= "basic_rates";
6069 params.value =TransmitRates;
developer72fb0bb2023-01-11 09:46:29 +08006070
developera3511852023-06-14 14:12:59 +08006071 wifi_dbg_printf("\n%s:",__func__);
6072 wifi_dbg_printf("\nparams.value=%s\n",params.value);
6073 wifi_dbg_printf("\n******************Transmit rates=%s\n",TransmitRates);
developer32f2a182023-06-27 19:50:41 +08006074 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex);
6075 if (os_snprintf_error(sizeof(config_file), res)) {
6076 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6077 return RETURN_ERR;
6078 }
developer5b23cd02023-07-19 20:26:03 +08006079
developera3511852023-06-14 14:12:59 +08006080 wifi_hostapdWrite(config_file,&params,1);
6081 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6082 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006083}
6084
developer72fb0bb2023-01-11 09:46:29 +08006085INT wifi_halGetIfStatsNull(wifi_radioTrafficStats2_t *output_struct)
6086{
developera3511852023-06-14 14:12:59 +08006087 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6088 output_struct->radio_BytesSent = 0;
6089 output_struct->radio_BytesReceived = 0;
6090 output_struct->radio_PacketsSent = 0;
6091 output_struct->radio_PacketsReceived = 0;
6092 output_struct->radio_ErrorsSent = 0;
6093 output_struct->radio_ErrorsReceived = 0;
6094 output_struct->radio_DiscardPacketsSent = 0;
6095 output_struct->radio_DiscardPacketsReceived = 0;
6096 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6097 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006098}
6099
6100
6101INT wifi_halGetIfStats(char *ifname, wifi_radioTrafficStats2_t *pStats)
6102{
developera3511852023-06-14 14:12:59 +08006103 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6104 CHAR buf[MAX_CMD_SIZE] = {0};
6105 CHAR Value[MAX_BUF_SIZE] = {0};
6106 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08006107 int res;
developer37646972023-06-29 10:58:43 +08006108 unsigned long ret;
developer72fb0bb2023-01-11 09:46:29 +08006109
developera3511852023-06-14 14:12:59 +08006110 if (ifname == NULL || strlen(ifname) <= 1)
6111 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006112
developer33f13ba2023-07-12 16:19:06 +08006113 res = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", ifname);
6114 if (res) {
6115 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08006116 return RETURN_ERR;
6117 }
6118
developera3511852023-06-14 14:12:59 +08006119 fp = fopen("/tmp/Radio_Stats.txt", "r");
6120 if(fp == NULL)
6121 {
6122 printf("/tmp/Radio_Stats.txt not exists \n");
6123 return RETURN_ERR;
6124 }
developerd14dff12023-06-28 22:47:44 +08006125 if (fclose(fp) != 0) {
6126 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6127 return RETURN_ERR;
6128 }
developer72fb0bb2023-01-11 09:46:29 +08006129
developer75bd10c2023-06-27 11:34:08 +08006130 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6131 if (os_snprintf_error(sizeof(buf), res)) {
6132 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6133 return RETURN_ERR;
6134 }
developera3511852023-06-14 14:12:59 +08006135 File_Reading(buf, Value);
developer5b23cd02023-07-19 20:26:03 +08006136
developerc14d83a2023-06-29 20:09:42 +08006137 if (hal_strtoul(Value, 10, &ret) < 0) {
developer37646972023-06-29 10:58:43 +08006138 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +08006139 }
developerc14d83a2023-06-29 20:09:42 +08006140
developer37646972023-06-29 10:58:43 +08006141 pStats->radio_PacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006142
developer86035662023-06-28 19:21:12 +08006143 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6144 if (os_snprintf_error(sizeof(buf), res)) {
6145 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6146 return RETURN_ERR;
6147 }
developera3511852023-06-14 14:12:59 +08006148 File_Reading(buf, Value);
developer5b23cd02023-07-19 20:26:03 +08006149
developerc14d83a2023-06-29 20:09:42 +08006150 if (hal_strtoul(Value, 10, &ret) < 0) {
6151 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006152 }
developer5b23cd02023-07-19 20:26:03 +08006153
developer37646972023-06-29 10:58:43 +08006154 pStats->radio_PacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006155
developer86035662023-06-28 19:21:12 +08006156 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX bytes' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6157 if (os_snprintf_error(sizeof(buf), res)) {
6158 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6159 return RETURN_ERR;
6160 }
developera3511852023-06-14 14:12:59 +08006161 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006162 if (hal_strtoul(Value, 10, &ret) < 0) {
6163 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006164 }
6165 pStats->radio_BytesReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006166
developer86035662023-06-28 19:21:12 +08006167 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX bytes' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6168 if (os_snprintf_error(sizeof(buf), res)) {
6169 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6170 return RETURN_ERR;
6171 }
developera3511852023-06-14 14:12:59 +08006172 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006173 if (hal_strtoul(Value, 10, &ret) < 0) {
6174 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006175 }
6176 pStats->radio_BytesSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006177
developer86035662023-06-28 19:21:12 +08006178 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6179 if (os_snprintf_error(sizeof(buf), res)) {
6180 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6181 return RETURN_ERR;
6182 }
developera3511852023-06-14 14:12:59 +08006183 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006184 if (hal_strtoul(Value, 10, &ret) < 0) {
6185 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006186 }
6187 pStats->radio_ErrorsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006188
developer86035662023-06-28 19:21:12 +08006189 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6190 if (os_snprintf_error(sizeof(buf), res)) {
6191 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6192 return RETURN_ERR;
6193 }
developera3511852023-06-14 14:12:59 +08006194 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006195 if (hal_strtoul(Value, 10, &ret) < 0) {
6196 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006197 }
6198 pStats->radio_ErrorsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006199
developer86035662023-06-28 19:21:12 +08006200 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6201 if (os_snprintf_error(sizeof(buf), res)) {
6202 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6203 return RETURN_ERR;
6204 }
developera3511852023-06-14 14:12:59 +08006205 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006206 if (hal_strtoul(Value, 10, &ret) < 0) {
6207 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006208 }
6209 pStats->radio_DiscardPacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006210
developer86035662023-06-28 19:21:12 +08006211 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6212 if (os_snprintf_error(sizeof(buf), res)) {
6213 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6214 return RETURN_ERR;
6215 }
developera3511852023-06-14 14:12:59 +08006216 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006217 if (hal_strtoul(Value, 10, &ret) < 0) {
6218 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006219 }
6220 pStats->radio_DiscardPacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006221
developera3511852023-06-14 14:12:59 +08006222 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6223 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006224}
6225
6226INT GetIfacestatus(CHAR *interface_name, CHAR *status)
6227{
developer7e4a2a62023-04-06 19:56:03 +08006228 CHAR buf[MAX_CMD_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006229 int res;
developer72fb0bb2023-01-11 09:46:29 +08006230
developer7e4a2a62023-04-06 19:56:03 +08006231 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6232
6233 if (interface_name != NULL && (strlen(interface_name) > 1) && status != NULL) {
developer75bd10c2023-06-27 11:34:08 +08006234 res = snprintf(buf, sizeof(buf), "%s%s%s%s%s", "ifconfig -a ",
6235 interface_name, " | grep ", interface_name, " | wc -l");
6236
6237 if (os_snprintf_error(sizeof(buf), res)) {
6238 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6239 return RETURN_ERR;
6240 }
developer7e4a2a62023-04-06 19:56:03 +08006241 File_Reading(buf, status);
6242 }
6243
6244 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6245 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006246}
6247
6248//Get detail radio traffic static info
6249INT wifi_getRadioTrafficStats2(INT radioIndex, wifi_radioTrafficStats2_t *output_struct) //Tr181
6250{
developera3511852023-06-14 14:12:59 +08006251 CHAR interface_name[64] = {0};
6252 BOOL iface_status = FALSE;
6253 wifi_radioTrafficStats2_t radioTrafficStats = {0};
developer72fb0bb2023-01-11 09:46:29 +08006254
developera3511852023-06-14 14:12:59 +08006255 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6256 if (NULL == output_struct)
6257 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006258
developera3511852023-06-14 14:12:59 +08006259 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6260 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006261
developera3511852023-06-14 14:12:59 +08006262 wifi_getApEnable(radioIndex, &iface_status);
developer72fb0bb2023-01-11 09:46:29 +08006263
developera3511852023-06-14 14:12:59 +08006264 if (iface_status == TRUE)
6265 wifi_halGetIfStats(interface_name, &radioTrafficStats);
6266 else
6267 wifi_halGetIfStatsNull(&radioTrafficStats); // just set some transmission statistic value to 0
developer72fb0bb2023-01-11 09:46:29 +08006268
developera3511852023-06-14 14:12:59 +08006269 output_struct->radio_BytesSent = radioTrafficStats.radio_BytesSent;
6270 output_struct->radio_BytesReceived = radioTrafficStats.radio_BytesReceived;
6271 output_struct->radio_PacketsSent = radioTrafficStats.radio_PacketsSent;
6272 output_struct->radio_PacketsReceived = radioTrafficStats.radio_PacketsReceived;
6273 output_struct->radio_ErrorsSent = radioTrafficStats.radio_ErrorsSent;
6274 output_struct->radio_ErrorsReceived = radioTrafficStats.radio_ErrorsReceived;
6275 output_struct->radio_DiscardPacketsSent = radioTrafficStats.radio_DiscardPacketsSent;
6276 output_struct->radio_DiscardPacketsReceived = radioTrafficStats.radio_DiscardPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +08006277
developera3511852023-06-14 14:12:59 +08006278 output_struct->radio_PLCPErrorCount = 0; //The number of packets that were received with a detected Physical Layer Convergence Protocol (PLCP) header error.
6279 output_struct->radio_FCSErrorCount = 0; //The number of packets that were received with a detected FCS error. This parameter is based on dot11FCSErrorCount from [Annex C/802.11-2012].
6280 output_struct->radio_InvalidMACCount = 0; //The number of packets that were received with a detected invalid MAC header error.
6281 output_struct->radio_PacketsOtherReceived = 0; //The number of packets that were received, but which were destined for a MAC address that is not associated with this interface.
6282 output_struct->radio_NoiseFloor = -99; //The noise floor for this radio channel where a recoverable signal can be obtained. Expressed as a signed integer in the range (-110:0). Measurement should capture all energy (in dBm) from sources other than Wi-Fi devices as well as interference from Wi-Fi devices too weak to be decoded. Measured in dBm
6283 output_struct->radio_ChannelUtilization = 35; //Percentage of time the channel was occupied by the radio\92s own activity (Activity Factor) or the activity of other radios. Channel utilization MUST cover all user traffic, management traffic, and time the radio was unavailable for CSMA activities, including DIFS intervals, etc. The metric is calculated and updated in this parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected from the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in Percentage
6284 output_struct->radio_ActivityFactor = 2; //Percentage of time that the radio was transmitting or receiving Wi-Fi packets to/from associated clients. Activity factor MUST include all traffic that deals with communication between the radio and clients associated to the radio as well as management overhead for the radio, including NAV timers, beacons, probe responses,time for receiving devices to send an ACK, SIFC intervals, etc. The metric is calculated and updated in this parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected from the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in Percentage
6285 output_struct->radio_CarrierSenseThreshold_Exceeded = 20; //Percentage of time that the radio was unable to transmit or receive Wi-Fi packets to/from associated clients due to energy detection (ED) on the channel or clear channel assessment (CCA). The metric is calculated and updated in this Parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected from the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in Percentage
6286 output_struct->radio_RetransmissionMetirc = 0; //Percentage of packets that had to be re-transmitted. Multiple re-transmissions of the same packet count as one. The metric is calculated and updated in this parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected from the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in percentage
developer72fb0bb2023-01-11 09:46:29 +08006287
developera3511852023-06-14 14:12:59 +08006288 output_struct->radio_MaximumNoiseFloorOnChannel = -1; //Maximum Noise on the channel during the measuring interval. The metric is updated in this parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected in the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in dBm
6289 output_struct->radio_MinimumNoiseFloorOnChannel = -1; //Minimum Noise on the channel. The metric is updated in this Parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected in the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in dBm
6290 output_struct->radio_MedianNoiseFloorOnChannel = -1; //Median Noise on the channel during the measuring interval. The metric is updated in this parameter at the end of the interval defined by "Radio Statistics Measuring Interval". The calculation of this metric MUST only use the data collected in the just completed interval. If this metric is queried before it has been updated with an initial calculation, it MUST return -1. Units in dBm
6291 output_struct->radio_StatisticsStartTime = 0; //The date and time at which the collection of the current set of statistics started. This time must be updated whenever the radio statistics are reset.
developer72fb0bb2023-01-11 09:46:29 +08006292
developera3511852023-06-14 14:12:59 +08006293 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006294
developera3511852023-06-14 14:12:59 +08006295 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006296}
6297
6298//Set radio traffic static Measureing rules
6299INT wifi_setRadioTrafficStatsMeasure(INT radioIndex, wifi_radioTrafficStatsMeasure_t *input_struct) //Tr181
6300{
developera39cfb22023-06-20 16:28:17 +08006301 char inf_name[IF_NAME_SIZE] = {0};
6302 unsigned int if_idx = 0;
6303 int ret = -1;
6304 struct unl unl_ins;
6305 struct nl_msg *msg = NULL;
6306 struct nlattr * msg_data = NULL;
6307 struct mtk_nl80211_param param;
6308
6309 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6310 return RETURN_ERR;
6311 if_idx = if_nametoindex(inf_name);
6312 if (!if_idx) {
6313 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6314 return RETURN_ERR;
6315 }
6316 /*init mtk nl80211 vendor cmd*/
6317 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6318 param.if_type = NL80211_ATTR_IFINDEX;
6319 param.if_idx = if_idx;
6320
6321 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6322 if (ret) {
6323 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6324 return RETURN_ERR;
6325 }
6326 /*add mtk vendor cmd data*/
6327 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD,
6328 sizeof(wifi_radioTrafficStatsMeasure_t), input_struct)) {
6329 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6330 MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD);
6331 nlmsg_free(msg);
6332 goto err;
6333 }
6334
6335 /*send mtk nl80211 vendor msg*/
6336 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6337 if (ret) {
6338 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6339 goto err;
6340 }
6341 /*deinit mtk nl80211 vendor msg*/
6342 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006343 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006344err:
6345 mtk_nl80211_deint(&unl_ins);
6346 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6347 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006348}
6349
6350//To start or stop RadioTrafficStats
6351INT wifi_setRadioTrafficStatsRadioStatisticsEnable(INT radioIndex, BOOL enable)
6352{
developera39cfb22023-06-20 16:28:17 +08006353 char inf_name[IF_NAME_SIZE] = {0};
6354 unsigned int if_idx = 0;
6355 int ret = -1;
6356 struct unl unl_ins;
6357 struct nl_msg *msg = NULL;
6358 struct nlattr * msg_data = NULL;
6359 struct mtk_nl80211_param param;
6360
6361 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6362 return RETURN_ERR;
6363 if_idx = if_nametoindex(inf_name);
6364 if (!if_idx) {
6365 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6366 return RETURN_ERR;
6367 }
6368 /*init mtk nl80211 vendor cmd*/
6369 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6370 param.if_type = NL80211_ATTR_IFINDEX;
6371 param.if_idx = if_idx;
6372
6373 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6374 if (ret) {
6375 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6376 return RETURN_ERR;
6377 }
6378 /*add mtk vendor cmd data*/
6379 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL, enable)) {
6380 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6381 MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL);
6382 nlmsg_free(msg);
6383 goto err;
6384 }
6385
6386 /*send mtk nl80211 vendor msg*/
6387 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6388 if (ret) {
6389 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6390 goto err;
6391 }
6392 /*deinit mtk nl80211 vendor msg*/
6393 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006394 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006395err:
6396 mtk_nl80211_deint(&unl_ins);
6397 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6398 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006399}
6400
6401//Clients associated with the AP over a specific interval. The histogram MUST have a range from -110to 0 dBm and MUST be divided in bins of 3 dBM, with bins aligning on the -110 dBm end of the range. Received signal levels equal to or greater than the smaller boundary of a bin and less than the larger boundary are included in the respective bin. The bin associated with the client?s current received signal level MUST be incremented when a client associates with the AP. Additionally, the respective bins associated with each connected client?s current received signal level MUST be incremented at the interval defined by "Radio Statistics Measuring Rate". The histogram?s bins MUST NOT be incremented at any other time. The histogram data collected during the interval MUST be published to the parameter only at the end of the interval defined by "Radio Statistics Measuring Interval". The underlying histogram data MUST be cleared at the start of each interval defined by "Radio Statistics Measuring Interval?. If any of the parameter's representing this histogram is queried before the histogram has been updated with an initial set of data, it MUST return -1. Units dBm
6402INT wifi_getRadioStatsReceivedSignalLevel(INT radioIndex, INT signalIndex, INT *SignalLevel) //Tr181
6403{
developera3511852023-06-14 14:12:59 +08006404 if (NULL == SignalLevel)
6405 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +08006406
developer9ce44382023-06-28 11:09:37 +08006407 *SignalLevel = -19;
developer72fb0bb2023-01-11 09:46:29 +08006408
developera3511852023-06-14 14:12:59 +08006409 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006410}
6411
6412//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6413INT wifi_applyRadioSettings(INT radioIndex)
6414{
developera3511852023-06-14 14:12:59 +08006415 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006416}
6417
6418//Get the radio index assocated with this SSID entry
6419INT wifi_getSSIDRadioIndex(INT ssidIndex, INT *radioIndex)
6420{
developera3511852023-06-14 14:12:59 +08006421 if(NULL == radioIndex)
6422 return RETURN_ERR;
6423 int max_radio_num = 0;
6424 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006425 if(max_radio_num == 0){
6426 return RETURN_ERR;
6427 }
developera3511852023-06-14 14:12:59 +08006428 *radioIndex = ssidIndex%max_radio_num;
6429 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006430}
6431
6432//Device.WiFi.SSID.{i}.Enable
6433//Get SSID enable configuration parameters (not the SSID enable status)
6434INT wifi_getSSIDEnable(INT ssidIndex, BOOL *output_bool) //Tr181
6435{
developera3511852023-06-14 14:12:59 +08006436 if (NULL == output_bool)
6437 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006438
developera3511852023-06-14 14:12:59 +08006439 return wifi_getApEnable(ssidIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08006440}
6441
6442//Device.WiFi.SSID.{i}.Enable
6443//Set SSID enable configuration parameters
6444INT wifi_setSSIDEnable(INT ssidIndex, BOOL enable) //Tr181
6445{
developera3511852023-06-14 14:12:59 +08006446 return wifi_setApEnable(ssidIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08006447}
6448
6449//Device.WiFi.SSID.{i}.Status
6450//Get the SSID enable status
6451INT wifi_getSSIDStatus(INT ssidIndex, CHAR *output_string) //Tr181
6452{
developer9ce44382023-06-28 11:09:37 +08006453 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +08006454 int res;
developer72fb0bb2023-01-11 09:46:29 +08006455
developera3511852023-06-14 14:12:59 +08006456 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6457 if (NULL == output_string)
6458 return RETURN_ERR;
developer69b61b02023-03-07 17:17:44 +08006459
developera3511852023-06-14 14:12:59 +08006460 wifi_getApEnable(ssidIndex,&output_bool);
developere40952c2023-06-15 18:46:43 +08006461 res = snprintf(output_string, 32, output_bool==1?"Enabled":"Disabled");
6462 if (os_snprintf_error(32, res)) {
6463 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6464 return RETURN_ERR;
6465 }
developer72fb0bb2023-01-11 09:46:29 +08006466
developera3511852023-06-14 14:12:59 +08006467 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6468 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006469}
6470
6471// Outputs a 32 byte or less string indicating the SSID name. Sring buffer must be preallocated by the caller.
6472INT wifi_getSSIDName(INT apIndex, CHAR *output)
6473{
developera3511852023-06-14 14:12:59 +08006474 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006475 int res;
developer72fb0bb2023-01-11 09:46:29 +08006476
developera3511852023-06-14 14:12:59 +08006477 if (NULL == output)
6478 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006479
developer75bd10c2023-06-27 11:34:08 +08006480 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6481 if (os_snprintf_error(sizeof(config_file), res)) {
6482 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6483 return RETURN_ERR;
6484 }
developera3511852023-06-14 14:12:59 +08006485 wifi_hostapdRead(config_file,"ssid",output,32);
developer72fb0bb2023-01-11 09:46:29 +08006486
developera3511852023-06-14 14:12:59 +08006487 wifi_dbg_printf("\n[%s]: SSID Name is : %s",__func__,output);
6488 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006489}
6490
developer69b61b02023-03-07 17:17:44 +08006491// Set a max 32 byte string and sets an internal variable to the SSID name
developer72fb0bb2023-01-11 09:46:29 +08006492INT wifi_setSSIDName(INT apIndex, CHAR *ssid_string)
6493{
developera3511852023-06-14 14:12:59 +08006494 struct params params;
6495 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006496 int res;
developer72fb0bb2023-01-11 09:46:29 +08006497
developera3511852023-06-14 14:12:59 +08006498 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6499 if(NULL == ssid_string || strlen(ssid_string) >= 32 || strlen(ssid_string) == 0 )
6500 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006501
developera3511852023-06-14 14:12:59 +08006502 params.name = "ssid";
6503 params.value = ssid_string;
developer75bd10c2023-06-27 11:34:08 +08006504
6505 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6506 if (os_snprintf_error(sizeof(config_file), res)) {
6507 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6508 return RETURN_ERR;
6509 }
6510
developera3511852023-06-14 14:12:59 +08006511 wifi_hostapdWrite(config_file, &params, 1);
6512 wifi_hostapdProcessUpdate(apIndex, &params, 1);
6513 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006514
developera3511852023-06-14 14:12:59 +08006515 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006516}
6517
6518//Get the BSSID
6519INT wifi_getBaseBSSID(INT ssidIndex, CHAR *output_string) //RDKB
6520{
developer7e4a2a62023-04-06 19:56:03 +08006521 char cmd[MAX_CMD_SIZE] = {0};
6522 char inf_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08006523 int res;
developer72fb0bb2023-01-11 09:46:29 +08006524
developera3511852023-06-14 14:12:59 +08006525 if (!output_string)
developerdaf24792023-06-06 11:40:04 +08006526 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006527
developer47cc27a2023-05-17 23:09:58 +08006528 if (wifi_GetInterfaceName(ssidIndex, inf_name) != RETURN_OK)
6529 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08006530
developer5b2f10c2023-05-25 17:02:21 +08006531 if (ssidIndex < 0 || ssidIndex > MAX_APS) {
6532 wifi_debug(DEBUG_ERROR, "innvalide ssidIdex(%d)\n", ssidIndex);
6533 strncpy(output_string, "\0", 1);
6534 return RETURN_ERR;
6535 }
developere40952c2023-06-15 18:46:43 +08006536 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep bssid | cut -d '=' -f2 | tr -d '\\n'", inf_name);
6537 if (os_snprintf_error(sizeof(cmd), res)) {
6538 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6539 return RETURN_ERR;
6540 }
6541
developer5b2f10c2023-05-25 17:02:21 +08006542 _syscmd(cmd, output_string, 64);
developer7e4a2a62023-04-06 19:56:03 +08006543
developer5b2f10c2023-05-25 17:02:21 +08006544 /* if hostapd does not control interface even if this interface has been brought up,
6545 * try to get its mac address by iw command.
6546 */
6547 if(strlen(output_string) == 0) {
6548 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08006549 res = snprintf(cmd, sizeof(cmd), "iw dev %s info | grep \"addr\" | awk \'{print $2}\'", inf_name);
6550 if (os_snprintf_error(sizeof(cmd), res)) {
6551 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6552 return RETURN_ERR;
6553 }
6554
developer5b2f10c2023-05-25 17:02:21 +08006555 _syscmd(cmd, output_string, 64);
6556 }
developer72fb0bb2023-01-11 09:46:29 +08006557
developer5b2f10c2023-05-25 17:02:21 +08006558 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006559}
6560
6561//Get the MAC address associated with this Wifi SSID
6562INT wifi_getSSIDMACAddress(INT ssidIndex, CHAR *output_string) //Tr181
6563{
developera3511852023-06-14 14:12:59 +08006564 wifi_getBaseBSSID(ssidIndex,output_string);
6565 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006566}
6567
6568//Get the basic SSID traffic static info
6569//Apply SSID and AP (in the case of Acess Point devices) to the hardware
6570//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6571INT wifi_applySSIDSettings(INT ssidIndex)
6572{
developera3511852023-06-14 14:12:59 +08006573 char interface_name[16] = {0};
6574 BOOL status = false;
6575 char cmd[MAX_CMD_SIZE] = {0};
6576 char buf[MAX_CMD_SIZE] = {0};
6577 int apIndex, ret;
6578 int max_radio_num = 0;
6579 int radioIndex = 0;
developere40952c2023-06-15 18:46:43 +08006580 int res;
developer72fb0bb2023-01-11 09:46:29 +08006581
developera3511852023-06-14 14:12:59 +08006582 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006583 if(max_radio_num == 0){
6584 return RETURN_ERR;
6585 }
developera3511852023-06-14 14:12:59 +08006586 radioIndex = ssidIndex % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +08006587
developera3511852023-06-14 14:12:59 +08006588 wifi_getApEnable(ssidIndex,&status);
6589 // Do not apply when ssid index is disabled
6590 if (status == false)
6591 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006592
developera3511852023-06-14 14:12:59 +08006593 /* Doing full remove and add for ssid Index
6594 * Not all hostapd options are supported with reload
6595 * for example macaddr_acl
6596 */
6597 if(wifi_setApEnable(ssidIndex,false) != RETURN_OK)
6598 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006599
developera3511852023-06-14 14:12:59 +08006600 ret = wifi_setApEnable(ssidIndex,true);
developer72fb0bb2023-01-11 09:46:29 +08006601
developera3511852023-06-14 14:12:59 +08006602 /* Workaround for hostapd issue with multiple bss definitions
6603 * when first created interface will be removed
6604 * then all vaps other vaps on same phy are removed
6605 * after calling setApEnable to false readd all enabled vaps */
6606 for(int i=0; i < MAX_APS/max_radio_num; i++) {
6607 apIndex = max_radio_num*i+radioIndex;
6608 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
6609 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006610 res = snprintf(cmd, sizeof(cmd), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
6611 if (os_snprintf_error(sizeof(cmd), res)) {
6612 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6613 return RETURN_ERR;
6614 }
6615
developera3511852023-06-14 14:12:59 +08006616 _syscmd(cmd, buf, sizeof(buf));
6617 if(*buf == '1')
6618 wifi_setApEnable(apIndex, true);
6619 }
developer72fb0bb2023-01-11 09:46:29 +08006620
developera3511852023-06-14 14:12:59 +08006621 return ret;
developer72fb0bb2023-01-11 09:46:29 +08006622}
6623
6624struct channels_noise {
developera3511852023-06-14 14:12:59 +08006625 int channel;
6626 int noise;
developer72fb0bb2023-01-11 09:46:29 +08006627};
6628
6629// Return noise array for each channel
6630int get_noise(int radioIndex, struct channels_noise *channels_noise_arr, int channels_num)
6631{
developera3511852023-06-14 14:12:59 +08006632 char interface_name[16] = {0};
6633 FILE *f = NULL;
6634 char cmd[128] = {0};
6635 char line[256] = {0};
developer75bd10c2023-06-27 11:34:08 +08006636 int tmp = 0, arr_index = -1, res;
developer72fb0bb2023-01-11 09:46:29 +08006637
developera3511852023-06-14 14:12:59 +08006638 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6639 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08006640
6641 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep 'frequency\\|noise' | awk '{print $2}'", interface_name);
6642 if (os_snprintf_error(sizeof(cmd), res)) {
6643 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6644 return RETURN_ERR;
6645 }
developer72fb0bb2023-01-11 09:46:29 +08006646
developera3511852023-06-14 14:12:59 +08006647 if ((f = popen(cmd, "r")) == NULL) {
6648 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
6649 return RETURN_ERR;
6650 }
developer69b61b02023-03-07 17:17:44 +08006651
developera3511852023-06-14 14:12:59 +08006652 while(fgets(line, sizeof(line), f) != NULL) {
6653 if(arr_index < channels_num){
developer37646972023-06-29 10:58:43 +08006654 if (sscanf(line, "%d", &tmp) == EOF)
6655 continue;
developera3511852023-06-14 14:12:59 +08006656 if (tmp > 0) { // channel frequency, the first line must be frequency
6657 arr_index++;
6658 channels_noise_arr[arr_index].channel = ieee80211_frequency_to_channel(tmp);
6659 } else { // noise
6660 channels_noise_arr[arr_index].noise = tmp;
6661 }
6662 }else{
6663 break;
6664 }
6665 }
6666 pclose(f);
6667 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006668}
6669
6670//Start the wifi scan and get the result into output buffer for RDKB to parser. The result will be used to manage endpoint list
6671//HAL funciton should allocate an data structure array, and return to caller with "neighbor_ap_array"
developer69b61b02023-03-07 17:17:44 +08006672INT wifi_getNeighboringWiFiDiagnosticResult2(INT radioIndex, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size) //Tr181
developer72fb0bb2023-01-11 09:46:29 +08006673{
developera3511852023-06-14 14:12:59 +08006674 int index = -1;
6675 wifi_neighbor_ap2_t *scan_array = NULL;
6676 char cmd[256]={0};
6677 char buf[128]={0};
6678 char file_name[32] = {0};
6679 char filter_SSID[32] = {0};
6680 char line[256] = {0};
6681 char interface_name[16] = {0};
6682 char *ret = NULL;
6683 int freq=0;
6684 FILE *f = NULL;
developerc14d83a2023-06-29 20:09:42 +08006685 unsigned long channels_num = 0;
developera3511852023-06-14 14:12:59 +08006686 int vht_channel_width = 0;
6687 int get_noise_ret = RETURN_ERR;
6688 bool filter_enable = false;
6689 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
developere40952c2023-06-15 18:46:43 +08006690 int phyId = 0, res;
developerc14d83a2023-06-29 20:09:42 +08006691 unsigned long len, tmp;
developer72fb0bb2023-01-11 09:46:29 +08006692
developera3511852023-06-14 14:12:59 +08006693 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006694
developera3511852023-06-14 14:12:59 +08006695 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6696 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006697
6698 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radioIndex);
6699 if (os_snprintf_error(sizeof(file_name), res)) {
6700 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6701 return RETURN_ERR;
6702 }
developer72fb0bb2023-01-11 09:46:29 +08006703
developera3511852023-06-14 14:12:59 +08006704 f = fopen(file_name, "r");
6705 if (f != NULL) {
developerd14dff12023-06-28 22:47:44 +08006706 if (fgets(buf, sizeof(file_name), f) == NULL)
6707 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006708 if ((strncmp(buf, "0", 1)) != 0) {
developerd14dff12023-06-28 22:47:44 +08006709 if (fgets(filter_SSID, sizeof(file_name), f) == NULL)
6710 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006711 if (strlen(filter_SSID) != 0)
6712 filter_enable = true;
6713 }
developerd14dff12023-06-28 22:47:44 +08006714 if (fclose(f) != 0) {
6715 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6716 return RETURN_ERR;
6717 }
developera3511852023-06-14 14:12:59 +08006718 }
developer72fb0bb2023-01-11 09:46:29 +08006719
developera3511852023-06-14 14:12:59 +08006720 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +08006721 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
6722 if (os_snprintf_error(sizeof(cmd), res)) {
6723 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6724 return RETURN_ERR;
6725 }
6726
developera3511852023-06-14 14:12:59 +08006727 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08006728 if (hal_strtoul(buf, 10, &tmp) < 0) {
6729 wifi_debug(DEBUG_ERROR, "strtol fail\n");
6730 }
developer72fb0bb2023-01-11 09:46:29 +08006731
developerc14d83a2023-06-29 20:09:42 +08006732 channels_num = tmp;
developer32f2a182023-06-27 19:50:41 +08006733 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan | grep '%s\\|SSID\\|freq\\|beacon interval\\|capabilities\\|signal\\|Supported rates\\|DTIM\\| \
developera3511852023-06-14 14:12:59 +08006734 // WPA\\|RSN\\|Group cipher\\|HT operation\\|secondary channel offset\\|channel width\\|HE.*GHz' | grep -v -e '*.*BSS'", interface_name, interface_name);
developer32f2a182023-06-27 19:50:41 +08006735 if (os_snprintf_error(sizeof(cmd), res)) {
6736 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6737 return RETURN_ERR;
6738 }
developer86035662023-06-28 19:21:12 +08006739 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developera3511852023-06-14 14:12:59 +08006740 if ((f = popen(cmd, "r")) == NULL) {
developerb14b3462023-07-01 18:02:42 +08006741 wifi_debug(DEBUG_ERROR, "popen error\n");
developera3511852023-06-14 14:12:59 +08006742 return RETURN_ERR;
6743 }
developer9ce44382023-06-28 11:09:37 +08006744 struct channels_noise *channels_noise_arr = NULL;
6745 if(channels_num > 0 && channels_num <= 243){
6746 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
6747 } else{
developerb14b3462023-07-01 18:02:42 +08006748 wifi_debug(DEBUG_ERROR, "channel num = %ld!!\n", channels_num);
developer9ce44382023-06-28 11:09:37 +08006749 }
developer5b23cd02023-07-19 20:26:03 +08006750
developer9ce44382023-06-28 11:09:37 +08006751 if(channels_noise_arr != NULL){
6752 get_noise_ret = get_noise(radioIndex, channels_noise_arr, channels_num);
6753 } else{
developerb14b3462023-07-01 18:02:42 +08006754 wifi_debug(DEBUG_ERROR, "channels_noise_arr is NULL!!\n");
developer9ce44382023-06-28 11:09:37 +08006755 }
developer5b23cd02023-07-19 20:26:03 +08006756
developer69b61b02023-03-07 17:17:44 +08006757
developera3511852023-06-14 14:12:59 +08006758 ret = fgets(line, sizeof(line), f);
6759 while (ret != NULL) {
6760 if(strstr(line, "BSS") != NULL) { // new neighbor info
6761 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
6762 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
6763 // If we don't want the BSS info, we don't realloc more space, and just clean the previous BSS.
developer72fb0bb2023-01-11 09:46:29 +08006764
developera3511852023-06-14 14:12:59 +08006765 if (!filter_BSS) {
6766 index++;
6767 wifi_neighbor_ap2_t *tmp;
6768 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
6769 if (tmp == NULL) { // no more memory to use
6770 index--;
6771 wifi_dbg_printf("%s: realloc failed\n", __func__);
6772 break;
6773 }
6774 scan_array = tmp;
6775 }
6776 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +08006777
developera3511852023-06-14 14:12:59 +08006778 filter_BSS = false;
developer86035662023-06-28 19:21:12 +08006779 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
6780 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6781 goto err;
6782 }
developerc79e9172023-06-06 19:48:03 +08006783 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +08006784 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +08006785 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +08006786 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +08006787 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +08006788 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
6789 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +08006790 if (sscanf(line," freq: %d", &freq) != 1) {
6791 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006792 }
developera3511852023-06-14 14:12:59 +08006793 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +08006794
developera3511852023-06-14 14:12:59 +08006795 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +08006796 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006797 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +08006798 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006799 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +08006800 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006801 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
6802 }
6803 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +08006804 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006805 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +08006806 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006807 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +08006808 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006809 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
6810 }
developer72fb0bb2023-01-11 09:46:29 +08006811
developera3511852023-06-14 14:12:59 +08006812 scan_array[index].ap_Noise = 0;
6813 if (get_noise_ret == RETURN_OK) {
6814 for (int i = 0; i < channels_num; i++) {
6815 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
6816 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
6817 break;
6818 }
6819 }
6820 }
6821 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +08006822 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
6823 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6824 goto err;
6825 }
developera3511852023-06-14 14:12:59 +08006826 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +08006827 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
6828 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6829 goto err;
6830 }
developera3511852023-06-14 14:12:59 +08006831 } else if (strstr(line,"SSID") != NULL) {
developer86035662023-06-28 19:21:12 +08006832 if (sscanf(line," SSID: %32s", scan_array[index].ap_SSID) != 1) {
6833 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +08006834 //goto err;
developer86035662023-06-28 19:21:12 +08006835 }
developera3511852023-06-14 14:12:59 +08006836 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) != 0) {
6837 filter_BSS = true;
6838 }
6839 } else if (strstr(line, "Supported rates") != NULL) {
6840 char SRate[80] = {0}, *tmp = NULL;
6841 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +08006842 if (strlen(line) >= sizeof(SRate))
developer86035662023-06-28 19:21:12 +08006843 goto err;
developer32f2a182023-06-27 19:50:41 +08006844 strncpy(SRate, line, strlen(line));
developera3511852023-06-14 14:12:59 +08006845 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +08006846 if (tmp == NULL)
6847 goto err;
developera3511852023-06-14 14:12:59 +08006848 tmp = strtok(NULL, ":");
developer86035662023-06-28 19:21:12 +08006849 if (tmp == NULL)
6850 goto err;
developer32f2a182023-06-27 19:50:41 +08006851 if (strlen(tmp) >= sizeof(buf))
developer86035662023-06-28 19:21:12 +08006852 goto err;
developer32f2a182023-06-27 19:50:41 +08006853 strncpy(buf, tmp, strlen(tmp));
developera3511852023-06-14 14:12:59 +08006854 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +08006855
developera3511852023-06-14 14:12:59 +08006856 tmp = strtok(buf, " \n");
6857 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +08006858 if (strlen(tmp) >= (sizeof(SRate) - strlen(SRate)))
developer86035662023-06-28 19:21:12 +08006859 goto err;
developer32f2a182023-06-27 19:50:41 +08006860 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +08006861 if (SRate[strlen(SRate) - 1] == '*') {
6862 SRate[strlen(SRate) - 1] = '\0';
6863 }
developer32f2a182023-06-27 19:50:41 +08006864 if (strlen(SRate) >= (sizeof(SRate) - 1))
developer86035662023-06-28 19:21:12 +08006865 goto err;
developer32f2a182023-06-27 19:50:41 +08006866 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +08006867
developera3511852023-06-14 14:12:59 +08006868 tmp = strtok(NULL, " \n");
6869 }
6870 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +08006871 if (sizeof(scan_array[index].ap_SupportedDataTransferRates) <= strlen(SRate))
developer86035662023-06-28 19:21:12 +08006872 goto err;
developer32f2a182023-06-27 19:50:41 +08006873 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, strlen(SRate));
developera3511852023-06-14 14:12:59 +08006874 } else if (strstr(line, "DTIM") != NULL) {
developer86035662023-06-28 19:21:12 +08006875 if (sscanf(line,"DTIM Period %u", &(scan_array[index].ap_DTIMPeriod)) != 1) {
6876 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006877 }
developera3511852023-06-14 14:12:59 +08006878 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006879 if (sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) <= 4)
developer86035662023-06-28 19:21:12 +08006880 goto err;
developer32f2a182023-06-27 19:50:41 +08006881 strncat(scan_array[index].ap_SupportedStandards, ",ac",
6882 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6883 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
6884 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +08006885 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006886 strncat(scan_array[index].ap_SupportedStandards, ",n", sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6887 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
6888 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +08006889 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006890 if (fgets(line, sizeof(line), f) == NULL) {
6891 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6892 goto err;
6893 }
6894 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
6895 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6896 goto err;
6897 }
developera3511852023-06-14 14:12:59 +08006898 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +08006899 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +08006900 } else {
developere40952c2023-06-15 18:46:43 +08006901 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +08006902 }
developere40952c2023-06-15 18:46:43 +08006903 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6904 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +08006905 goto err;
developere40952c2023-06-15 18:46:43 +08006906 }
6907
developera3511852023-06-14 14:12:59 +08006908 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6909 continue;
6910 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006911 if (fgets(line, sizeof(line), f) == NULL) {
6912 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6913 goto err;
6914 }
developerc14d83a2023-06-29 20:09:42 +08006915 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
developer86035662023-06-28 19:21:12 +08006916 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6917 goto err;
6918 }
developera3511852023-06-14 14:12:59 +08006919 if (!strcmp(buf, "above")) {
6920 //40Mhz +
developere40952c2023-06-15 18:46:43 +08006921 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT40PLUS", radioIndex%1 ? "A": "G");
developera3511852023-06-14 14:12:59 +08006922 }
6923 else if (!strcmp(buf, "below")) {
6924 //40Mhz -
developere40952c2023-06-15 18:46:43 +08006925 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT40MINUS", radioIndex%1 ? "A": "G");
developera3511852023-06-14 14:12:59 +08006926 } else {
6927 //20Mhz
developere40952c2023-06-15 18:46:43 +08006928 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT20", radioIndex%1 ? "A": "G");
6929 }
6930 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6931 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +08006932 goto err;
developera3511852023-06-14 14:12:59 +08006933 }
developere40952c2023-06-15 18:46:43 +08006934
developera3511852023-06-14 14:12:59 +08006935 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6936 continue;
6937 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006938 strncat(scan_array[index].ap_SupportedStandards, ",ax",
6939 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6940 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
6941 scan_array[index].ap_OperatingStandards[2] = '\0';
developer86035662023-06-28 19:21:12 +08006942 if (fgets(line, sizeof(line), f) == NULL) {
6943 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6944 goto err;
6945 }
developera3511852023-06-14 14:12:59 +08006946 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006947 if (strstr(line, "HE40/2.4GHz") != NULL) {
6948 len = strlen("11AXHE40PLUS");
6949 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
6950 } else {
6951 len = strlen("11AXHE20");
6952 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
6953 }
6954 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +08006955 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
6956 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006957 len = strlen("11AXHE80");
6958 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
6959 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developer86035662023-06-28 19:21:12 +08006960 if (fgets(line, sizeof(line), f) == NULL) {
6961 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6962 goto err;
6963 }
developera3511852023-06-14 14:12:59 +08006964 } else
6965 continue;
developer32f2a182023-06-27 19:50:41 +08006966 if (strstr(line, "HE160/5GHz") != NULL) {
6967 len = strlen("11AXHE160");
6968 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
6969 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
6970 }
developera3511852023-06-14 14:12:59 +08006971 }
6972 continue;
6973 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006974 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
6975 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006976 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006977 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
6978 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006979 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +08006980 if (sscanf(line, " * Group cipher: %64s", scan_array[index].ap_EncryptionMode) != 1) {
6981 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6982 goto err;
6983 }
developera3511852023-06-14 14:12:59 +08006984 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006985 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
6986 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +08006987 }
6988 }
developerb14b3462023-07-01 18:02:42 +08006989 ret = fgets(line, sizeof(line), f);
developera3511852023-06-14 14:12:59 +08006990 }
developer72fb0bb2023-01-11 09:46:29 +08006991
developera3511852023-06-14 14:12:59 +08006992 if (!filter_BSS) {
6993 *output_array_size = index + 1;
6994 } else {
6995 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
6996 *output_array_size = index;
6997 }
6998 *neighbor_ap_array = scan_array;
6999 pclose(f);
7000 free(channels_noise_arr);
7001 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7002 return RETURN_OK;
developer86035662023-06-28 19:21:12 +08007003err:
7004 pclose(f);
7005 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +08007006 if (scan_array)
7007 free(scan_array);
developer86035662023-06-28 19:21:12 +08007008 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7009 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007010}
7011
7012//>> Deprecated: used for old RDKB code.
7013INT wifi_getRadioWifiTrafficStats(INT radioIndex, wifi_radioTrafficStats_t *output_struct)
7014{
developera3511852023-06-14 14:12:59 +08007015 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007016
developera3511852023-06-14 14:12:59 +08007017 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7018 output_struct->wifi_PLCPErrorCount = 0;
7019 output_struct->wifi_FCSErrorCount = 0;
7020 output_struct->wifi_InvalidMACCount = 0;
7021 output_struct->wifi_PacketsOtherReceived = 0;
7022 output_struct->wifi_Noise = 0;
7023 status = RETURN_OK;
7024 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7025 return status;
developer72fb0bb2023-01-11 09:46:29 +08007026}
7027
7028INT wifi_getBasicTrafficStats(INT apIndex, wifi_basicTrafficStats_t *output_struct)
7029{
developera3511852023-06-14 14:12:59 +08007030 char interface_name[16] = {0};
7031 char cmd[128] = {0};
7032 char buf[1280] = {0};
7033 char *pos = NULL;
developere40952c2023-06-15 18:46:43 +08007034 int res;
developer72fb0bb2023-01-11 09:46:29 +08007035
developera3511852023-06-14 14:12:59 +08007036 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7037 if (NULL == output_struct)
7038 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007039
developera3511852023-06-14 14:12:59 +08007040 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
7041 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007042
developera3511852023-06-14 14:12:59 +08007043 memset(output_struct, 0, sizeof(wifi_basicTrafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08007044
developere40952c2023-06-15 18:46:43 +08007045 res = snprintf(cmd, sizeof(cmd), "ifconfig %s", interface_name);
7046 if (os_snprintf_error(sizeof(cmd), res)) {
7047 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7048 return RETURN_ERR;
7049 }
7050
developera3511852023-06-14 14:12:59 +08007051 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007052
developera3511852023-06-14 14:12:59 +08007053 pos = buf;
7054 if ((pos = strstr(pos, "RX packets:")) == NULL)
7055 return RETURN_ERR;
7056 output_struct->wifi_PacketsReceived = atoi(pos+strlen("RX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08007057
developera3511852023-06-14 14:12:59 +08007058 if ((pos = strstr(pos, "TX packets:")) == NULL)
7059 return RETURN_ERR;
7060 output_struct->wifi_PacketsSent = atoi(pos+strlen("TX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08007061
developera3511852023-06-14 14:12:59 +08007062 if ((pos = strstr(pos, "RX bytes:")) == NULL)
7063 return RETURN_ERR;
7064 output_struct->wifi_BytesReceived = atoi(pos+strlen("RX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08007065
developera3511852023-06-14 14:12:59 +08007066 if ((pos = strstr(pos, "TX bytes:")) == NULL)
7067 return RETURN_ERR;
7068 output_struct->wifi_BytesSent = atoi(pos+strlen("TX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08007069
developer37646972023-06-29 10:58:43 +08007070 res = snprintf(cmd, sizeof(cmd),
7071 "hostapd_cli -i %s list_sta | wc -l | tr -d '\n'", interface_name);
7072 if (os_snprintf_error(sizeof(cmd), res)) {
7073 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7074 return RETURN_ERR;
7075 }
developera3511852023-06-14 14:12:59 +08007076 _syscmd(cmd, buf, sizeof(buf));
developer37646972023-06-29 10:58:43 +08007077 if (sscanf(buf, "%lu", &output_struct->wifi_Associations) == EOF)
7078 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer72fb0bb2023-01-11 09:46:29 +08007079
developera3511852023-06-14 14:12:59 +08007080 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7081 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007082}
7083
7084INT wifi_getWifiTrafficStats(INT apIndex, wifi_trafficStats_t *output_struct)
7085{
developera3511852023-06-14 14:12:59 +08007086 char interface_name[IF_NAME_SIZE] = {0};
7087 char interface_status[MAX_BUF_SIZE] = {0};
7088 char Value[MAX_BUF_SIZE] = {0};
7089 char buf[MAX_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08007090 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08007091 int res;
developerc14d83a2023-06-29 20:09:42 +08007092 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +08007093
developera3511852023-06-14 14:12:59 +08007094 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7095 if (NULL == output_struct)
7096 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007097
developera3511852023-06-14 14:12:59 +08007098 memset(output_struct, 0, sizeof(wifi_trafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08007099
developera3511852023-06-14 14:12:59 +08007100 if (wifi_GetInterfaceName(apIndex,interface_name) != RETURN_OK)
7101 return RETURN_ERR;
7102 GetIfacestatus(interface_name, interface_status);
developer72fb0bb2023-01-11 09:46:29 +08007103
developera3511852023-06-14 14:12:59 +08007104 if(0 != strcmp(interface_status, "1"))
7105 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08007106
developer33f13ba2023-07-12 16:19:06 +08007107 res = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", interface_name);
7108 if (res) {
7109 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08007110 return RETURN_ERR;
7111 }
developer72fb0bb2023-01-11 09:46:29 +08007112
developera3511852023-06-14 14:12:59 +08007113 fp = fopen("/tmp/SSID_Stats.txt", "r");
7114 if(fp == NULL)
7115 {
7116 printf("/tmp/SSID_Stats.txt not exists \n");
7117 return RETURN_ERR;
7118 }
developer37646972023-06-29 10:58:43 +08007119 if (fclose(fp) == EOF) {
7120 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
7121 return RETURN_ERR;
7122 }
developer72fb0bb2023-01-11 09:46:29 +08007123
developer37646972023-06-29 10:58:43 +08007124 res = snprintf(buf, sizeof(buf),
7125 "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7126 if (os_snprintf_error(sizeof(buf), res)) {
7127 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7128 return RETURN_ERR;
7129 }
developera3511852023-06-14 14:12:59 +08007130 File_Reading(buf, Value);
developer5b23cd02023-07-19 20:26:03 +08007131
developerc14d83a2023-06-29 20:09:42 +08007132 if (hal_strtoul(Value, 10, &tmp) < 0) {
7133 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007134 }
developerc14d83a2023-06-29 20:09:42 +08007135 output_struct->wifi_ErrorsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007136
developer37646972023-06-29 10:58:43 +08007137 res = snprintf(buf, sizeof(buf),
7138 "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7139 if (os_snprintf_error(sizeof(buf), res)) {
7140 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7141 return RETURN_ERR;
7142 }
developera3511852023-06-14 14:12:59 +08007143 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007144
7145 if (hal_strtoul(Value, 10, &tmp) < 0) {
7146 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007147 }
developerc14d83a2023-06-29 20:09:42 +08007148 output_struct->wifi_ErrorsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007149
developer37646972023-06-29 10:58:43 +08007150 res = snprintf(buf, sizeof(buf),
7151 "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7152 if (os_snprintf_error(sizeof(buf), res)) {
7153 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7154 return RETURN_ERR;
7155 }
developera3511852023-06-14 14:12:59 +08007156 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007157
7158 if (hal_strtoul(Value, 10, &tmp) < 0) {
7159 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007160 }
developerc14d83a2023-06-29 20:09:42 +08007161 output_struct->wifi_DiscardedPacketsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007162
developer37646972023-06-29 10:58:43 +08007163 res = snprintf(buf, sizeof(buf),
7164 "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7165 if (os_snprintf_error(sizeof(buf), res)) {
7166 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7167 return RETURN_ERR;
7168 }
developera3511852023-06-14 14:12:59 +08007169 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007170
7171 if (hal_strtoul(Value, 10, &tmp) < 0) {
7172 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007173 }
developerc14d83a2023-06-29 20:09:42 +08007174 output_struct->wifi_DiscardedPacketsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007175
developera3511852023-06-14 14:12:59 +08007176 output_struct->wifi_UnicastPacketsSent = 0;
7177 output_struct->wifi_UnicastPacketsReceived = 0;
7178 output_struct->wifi_MulticastPacketsSent = 0;
7179 output_struct->wifi_MulticastPacketsReceived = 0;
7180 output_struct->wifi_BroadcastPacketsSent = 0;
7181 output_struct->wifi_BroadcastPacketsRecevied = 0;
7182 output_struct->wifi_UnknownPacketsReceived = 0;
developer72fb0bb2023-01-11 09:46:29 +08007183
developera3511852023-06-14 14:12:59 +08007184 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7185 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007186}
7187
7188INT wifi_getSSIDTrafficStats(INT apIndex, wifi_ssidTrafficStats_t *output_struct)
7189{
developera3511852023-06-14 14:12:59 +08007190 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007191
developera3511852023-06-14 14:12:59 +08007192 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7193 //Below values should get updated from hal
7194 output_struct->wifi_RetransCount=0;
7195 output_struct->wifi_FailedRetransCount=0;
7196 output_struct->wifi_RetryCount=0;
7197 output_struct->wifi_MultipleRetryCount=0;
7198 output_struct->wifi_ACKFailureCount=0;
7199 output_struct->wifi_AggregatedPacketCount=0;
developer72fb0bb2023-01-11 09:46:29 +08007200
developera3511852023-06-14 14:12:59 +08007201 status = RETURN_OK;
7202 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007203
developera3511852023-06-14 14:12:59 +08007204 return status;
developer72fb0bb2023-01-11 09:46:29 +08007205}
7206
7207INT wifi_getNeighboringWiFiDiagnosticResult(wifi_neighbor_ap_t **neighbor_ap_array, UINT *output_array_size)
7208{
developera3511852023-06-14 14:12:59 +08007209 INT status = RETURN_ERR;
7210 UINT index;
7211 wifi_neighbor_ap_t *pt=NULL;
developer72fb0bb2023-01-11 09:46:29 +08007212
developera3511852023-06-14 14:12:59 +08007213 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7214 *output_array_size=2;
7215 //zqiu: HAL alloc the array and return to caller. Caller response to free it.
7216 *neighbor_ap_array=(wifi_neighbor_ap_t *)calloc(sizeof(wifi_neighbor_ap_t), *output_array_size);
developer86035662023-06-28 19:21:12 +08007217 if (*neighbor_ap_array == NULL) {
7218 wifi_debug(DEBUG_ERROR, "calloc fail!\n");
7219 return RETURN_ERR;
7220 }
developera3511852023-06-14 14:12:59 +08007221 for (index = 0, pt=*neighbor_ap_array; index < *output_array_size; index++, pt++) {
developer32f2a182023-06-27 19:50:41 +08007222 pt->ap_Radio[0] = '\0';
7223 pt->ap_SSID[0] = '\0';
7224 pt->ap_BSSID[0] = '\0';
7225 pt->ap_Mode[0] = '\0';
developera3511852023-06-14 14:12:59 +08007226 pt->ap_Channel=1;
7227 pt->ap_SignalStrength=0;
developer32f2a182023-06-27 19:50:41 +08007228 pt->ap_SecurityModeEnabled[0] = '\0';
7229 pt->ap_EncryptionMode[0] = '\0';
7230 pt->ap_OperatingFrequencyBand[0] = '\0';
7231 pt->ap_SupportedStandards[0] = '\0';
7232 pt->ap_OperatingStandards[0] = '\0';
7233 pt->ap_OperatingChannelBandwidth[0] = '\0';
7234 pt->ap_BasicDataTransferRates[0] = '\0';
7235 pt->ap_SupportedDataTransferRates[0] = '\0';
developera3511852023-06-14 14:12:59 +08007236 pt->ap_BeaconPeriod=1;
7237 pt->ap_Noise=0;
developera3511852023-06-14 14:12:59 +08007238 pt->ap_DTIMPeriod=1;
7239 pt->ap_ChannelUtilization = 1;
7240 }
developer72fb0bb2023-01-11 09:46:29 +08007241
developera3511852023-06-14 14:12:59 +08007242 status = RETURN_OK;
7243 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007244
developera3511852023-06-14 14:12:59 +08007245 return status;
developer72fb0bb2023-01-11 09:46:29 +08007246}
7247
7248//----------------- AP HAL -------------------------------
7249
7250//>> Deprecated: used for old RDKB code.
7251INT wifi_getAllAssociatedDeviceDetail(INT apIndex, ULONG *output_ulong, wifi_device_t **output_struct)
7252{
developera3511852023-06-14 14:12:59 +08007253 if (NULL == output_ulong || NULL == output_struct)
7254 return RETURN_ERR;
7255 *output_ulong = 0;
7256 *output_struct = NULL;
7257 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007258}
7259
7260#ifdef HAL_NETLINK_IMPL
7261static int AssoDevInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +08007262 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7263 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +08007264 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
7265 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +08007266 char mac_addr[20];
7267 static int count=0;
7268 int rate=0;
developer72fb0bb2023-01-11 09:46:29 +08007269
developera3511852023-06-14 14:12:59 +08007270 wifi_device_info_t *out = (wifi_device_info_t*)arg;
developer72fb0bb2023-01-11 09:46:29 +08007271
developera3511852023-06-14 14:12:59 +08007272 nla_parse(tb,
7273 NL80211_ATTR_MAX,
7274 genlmsg_attrdata(gnlh, 0),
7275 genlmsg_attrlen(gnlh, 0),
7276 NULL);
developer72fb0bb2023-01-11 09:46:29 +08007277
developera3511852023-06-14 14:12:59 +08007278 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +08007279 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +08007280 return NL_SKIP;
7281 }
developer72fb0bb2023-01-11 09:46:29 +08007282
7283
developera3511852023-06-14 14:12:59 +08007284 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +08007285 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +08007286 return NL_SKIP;
7287 }
developer72fb0bb2023-01-11 09:46:29 +08007288
developera3511852023-06-14 14:12:59 +08007289 //devIndex starts from 1
7290 if( ++count == out->wifi_devIndex )
7291 {
7292 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
7293 //Getting the mac addrress
7294 mac_addr_aton(out->wifi_devMacAddress,mac_addr);
developer12fb9f62023-06-30 15:26:27 +08007295 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7296 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
7297 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7298 return NL_SKIP;
7299 }
developera3511852023-06-14 14:12:59 +08007300 }
developer72fb0bb2023-01-11 09:46:29 +08007301
developera3511852023-06-14 14:12:59 +08007302 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7303 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7304 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7305 out->wifi_devTxRate = rate/10;
7306 }
7307 }
developer72fb0bb2023-01-11 09:46:29 +08007308
developer12fb9f62023-06-30 15:26:27 +08007309 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7310 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) {
7311 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7312 return NL_SKIP;
7313 }
developera3511852023-06-14 14:12:59 +08007314 }
developera3511852023-06-14 14:12:59 +08007315 if(sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7316 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7317 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7318 out->wifi_devRxRate = rate/10;
7319 }
7320 }
7321 if(sinfo[NL80211_STA_INFO_SIGNAL_AVG])
7322 out->wifi_devSignalStrength = (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
developer72fb0bb2023-01-11 09:46:29 +08007323
developera3511852023-06-14 14:12:59 +08007324 out->wifi_devAssociatedDeviceAuthentiationState = 1;
7325 count = 0; //starts the count for next cycle
7326 return NL_STOP;
7327 }
developer72fb0bb2023-01-11 09:46:29 +08007328
developera3511852023-06-14 14:12:59 +08007329 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08007330
7331}
7332#endif
7333
7334INT wifi_getAssociatedDeviceDetail(INT apIndex, INT devIndex, wifi_device_t *output_struct)
7335{
developera3511852023-06-14 14:12:59 +08007336 Netlink nl = {0};
7337 char if_name[IF_NAME_SIZE] = {0};
7338 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08007339 int res;
developer72fb0bb2023-01-11 09:46:29 +08007340
developera3511852023-06-14 14:12:59 +08007341 wifi_device_info_t info = {0};
7342 info.wifi_devIndex = devIndex;
developer72fb0bb2023-01-11 09:46:29 +08007343
developera3511852023-06-14 14:12:59 +08007344 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
7345 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007346
developere40952c2023-06-15 18:46:43 +08007347 res = snprintf(if_name,sizeof(if_name),"%s", interface_name);
7348 if (os_snprintf_error(sizeof(if_name), res)) {
7349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7350 return RETURN_ERR;
7351 }
developer72fb0bb2023-01-11 09:46:29 +08007352
developera3511852023-06-14 14:12:59 +08007353 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007354
developera3511852023-06-14 14:12:59 +08007355 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +08007356 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +08007357 return -1;
7358 }
developer72fb0bb2023-01-11 09:46:29 +08007359
developera3511852023-06-14 14:12:59 +08007360 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +08007361
developera3511852023-06-14 14:12:59 +08007362 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +08007363 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +08007364 nlfree(&nl);
7365 return -2;
7366 }
developer72fb0bb2023-01-11 09:46:29 +08007367
developera3511852023-06-14 14:12:59 +08007368 genlmsg_put(msg,
7369 NL_AUTO_PID,
7370 NL_AUTO_SEQ,
7371 nl.id,
7372 0,
7373 NLM_F_DUMP,
7374 NL80211_CMD_GET_STATION,
7375 0);
developer72fb0bb2023-01-11 09:46:29 +08007376
developera3511852023-06-14 14:12:59 +08007377 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
7378 nl_send_auto_complete(nl.socket, msg);
7379 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,AssoDevInfo_callback,&info);
7380 nl_recvmsgs(nl.socket, nl.cb);
7381 nlmsg_free(msg);
7382 nlfree(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007383
developera3511852023-06-14 14:12:59 +08007384 output_struct->wifi_devAssociatedDeviceAuthentiationState = info.wifi_devAssociatedDeviceAuthentiationState;
7385 output_struct->wifi_devRxRate = info.wifi_devRxRate;
7386 output_struct->wifi_devTxRate = info.wifi_devTxRate;
7387 output_struct->wifi_devSignalStrength = info.wifi_devSignalStrength;
7388 memcpy(&output_struct->wifi_devMacAddress, &info.wifi_devMacAddress, sizeof(info.wifi_devMacAddress));
7389 return RETURN_OK;
7390}
developer72fb0bb2023-01-11 09:46:29 +08007391
developera3511852023-06-14 14:12:59 +08007392INT wifi_kickAssociatedDevice(INT apIndex, wifi_device_t *device)
7393{
7394 if (NULL == device)
7395 return RETURN_ERR;
7396 return RETURN_OK;
7397}
7398//<<
developer72fb0bb2023-01-11 09:46:29 +08007399
developer72fb0bb2023-01-11 09:46:29 +08007400
7401//--------------wifi_ap_hal-----------------------------
7402//enables CTS protection for the radio used by this AP
7403INT wifi_setRadioCtsProtectionEnable(INT apIndex, BOOL enable)
7404{
developera3511852023-06-14 14:12:59 +08007405 //save config and Apply instantly
7406 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007407}
7408
7409// enables OBSS Coexistence - fall back to 20MHz if necessary for the radio used by this ap
7410INT wifi_setRadioObssCoexistenceEnable(INT apIndex, BOOL enable)
7411{
developera3511852023-06-14 14:12:59 +08007412 char config_file[64] = {'\0'};
7413 char config_dat_file[64] = {'\0'};
7414 char buf[64] = {'\0'};
7415 struct params list = {0};
7416 struct params dat = {0};
7417 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007418 int res;
developer72fb0bb2023-01-11 09:46:29 +08007419
developera3511852023-06-14 14:12:59 +08007420 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7421 list.name = "ht_coex";
developere40952c2023-06-15 18:46:43 +08007422 res = snprintf(buf, sizeof(buf), "%d", enable);
7423 if (os_snprintf_error(sizeof(buf), res)) {
7424 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7425 return RETURN_ERR;
7426 }
7427
developera3511852023-06-14 14:12:59 +08007428 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007429
developera3511852023-06-14 14:12:59 +08007430 dat.name = "HT_BSSCoexistence";
7431 dat.value = buf;
developerd1824452023-05-18 12:30:04 +08007432
developera3511852023-06-14 14:12:59 +08007433 band = wifi_index_to_band(apIndex);
developere40952c2023-06-15 18:46:43 +08007434 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7435 if (os_snprintf_error(sizeof(config_file), res)) {
7436 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7437 return RETURN_ERR;
7438 }
7439
7440 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7441 if (os_snprintf_error(sizeof(config_dat_file), res)) {
7442 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7443 return RETURN_ERR;
7444 }
7445
developera3511852023-06-14 14:12:59 +08007446 wifi_hostapdWrite(config_file, &list, 1);
7447 wifi_datfileWrite(config_dat_file, &dat, 1);
7448 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007449
developera3511852023-06-14 14:12:59 +08007450 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007451
developera3511852023-06-14 14:12:59 +08007452 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007453}
7454
7455//P3 // sets the fragmentation threshold in bytes for the radio used by this ap
7456INT wifi_setRadioFragmentationThreshold(INT apIndex, UINT threshold)
7457{
developera3511852023-06-14 14:12:59 +08007458 char config_file[MAX_BUF_SIZE] = {'\0'};
7459 char buf[MAX_BUF_SIZE] = {'\0'};
7460 struct params list;
developere40952c2023-06-15 18:46:43 +08007461 int res;
developer72fb0bb2023-01-11 09:46:29 +08007462
developera3511852023-06-14 14:12:59 +08007463 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7464 if (threshold < 256 || threshold > 2346 )
7465 return RETURN_ERR;
7466 list.name = "fragm_threshold";
developere40952c2023-06-15 18:46:43 +08007467 res = snprintf(buf, sizeof(buf), "%d", threshold);
7468 if (os_snprintf_error(sizeof(buf), res)) {
7469 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7470 return RETURN_ERR;
7471 }
7472
developera3511852023-06-14 14:12:59 +08007473 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007474
developere40952c2023-06-15 18:46:43 +08007475 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7476 if (os_snprintf_error(sizeof(config_file), res)) {
7477 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7478 return RETURN_ERR;
7479 }
7480
developera3511852023-06-14 14:12:59 +08007481 wifi_hostapdWrite(config_file, &list, 1);
7482 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007483
developera3511852023-06-14 14:12:59 +08007484 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007485
developera3511852023-06-14 14:12:59 +08007486 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007487}
7488
7489// enable STBC mode in the hardwarwe, 0 == not enabled, 1 == enabled
7490INT wifi_setRadioSTBCEnable(INT radioIndex, BOOL STBC_Enable)
7491{
developera3511852023-06-14 14:12:59 +08007492 char config_file[64] = {'\0'};
7493 char cmd[512] = {'\0'};
7494 char buf[512] = {'\0'};
7495 char stbc_config[16] = {'\0'};
7496 wifi_band band;
7497 int iterator = 0;
7498 BOOL current_stbc = FALSE;
7499 int ant_count = 0;
7500 int ant_bitmap = 0;
7501 struct params list;
7502 char dat_file[64] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007503 int res;
developer72fb0bb2023-01-11 09:46:29 +08007504
developera3511852023-06-14 14:12:59 +08007505 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007506
developera3511852023-06-14 14:12:59 +08007507 band = wifi_index_to_band(radioIndex);
7508 if (band == band_invalid)
7509 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007510
developera3511852023-06-14 14:12:59 +08007511 if (band == band_2_4)
7512 iterator = 1;
7513 else if ((band == band_5) || (band == band_6))
7514 iterator = 2;
7515 else
7516 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007517
developera3511852023-06-14 14:12:59 +08007518 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);
7519 for (; ant_bitmap > 0; ant_bitmap >>= 1)
7520 ant_count += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08007521
developera3511852023-06-14 14:12:59 +08007522 if (ant_count == 1 && STBC_Enable == TRUE) {
developer75bd10c2023-06-27 11:34:08 +08007523 wifi_debug(DEBUG_ERROR, "can not enable STBC when using only one antenna\n");
developera3511852023-06-14 14:12:59 +08007524 return RETURN_OK;
7525 }
developer72fb0bb2023-01-11 09:46:29 +08007526
developere40952c2023-06-15 18:46:43 +08007527 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7528 if (os_snprintf_error(sizeof(config_file), res)) {
7529 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7530 return RETURN_ERR;
7531 }
developer72fb0bb2023-01-11 09:46:29 +08007532
developera3511852023-06-14 14:12:59 +08007533 // set ht and vht config
7534 for (int i = 0; i < iterator; i++) {
7535 memset(stbc_config, 0, sizeof(stbc_config));
7536 memset(cmd, 0, sizeof(cmd));
7537 memset(buf, 0, sizeof(buf));
7538 list.name = (i == 0)?"ht_capab":"vht_capab";
developere40952c2023-06-15 18:46:43 +08007539 res = snprintf(stbc_config, sizeof(stbc_config), "%s", list.name);
7540 if (os_snprintf_error(sizeof(stbc_config), res)) {
7541 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7542 return RETURN_ERR;
7543 }
7544
7545 res = snprintf(cmd, sizeof(cmd), "cat %s | grep -E '^%s' | grep 'STBC'", config_file, stbc_config);
7546 if (os_snprintf_error(sizeof(cmd), res)) {
7547 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7548 return RETURN_ERR;
7549 }
7550
developera3511852023-06-14 14:12:59 +08007551 _syscmd(cmd, buf, sizeof(buf));
7552 if (strlen(buf) != 0)
7553 current_stbc = TRUE;
7554 if (current_stbc == STBC_Enable)
7555 continue;
developer72fb0bb2023-01-11 09:46:29 +08007556
developera3511852023-06-14 14:12:59 +08007557 if (STBC_Enable == TRUE) {
7558 // Append the STBC flags in capab config
7559 memset(cmd, 0, sizeof(cmd));
7560 if (i == 0)
developere40952c2023-06-15 18:46:43 +08007561 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC][RX-STBC1]/' %s", config_file);
developera3511852023-06-14 14:12:59 +08007562 else
developere40952c2023-06-15 18:46:43 +08007563 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1][RX-STBC-1]/' %s", config_file);
7564 if (os_snprintf_error(sizeof(cmd), res)) {
7565 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7566 return RETURN_ERR;
7567 }
7568
developera3511852023-06-14 14:12:59 +08007569 _syscmd(cmd, buf, sizeof(buf));
7570 } else if (STBC_Enable == FALSE) {
7571 // Remove the STBC flags and remain other flags in capab
7572 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08007573 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7574 if (os_snprintf_error(sizeof(cmd), res)) {
7575 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7576 return RETURN_ERR;
7577 }
7578
developera3511852023-06-14 14:12:59 +08007579 _syscmd(cmd, buf, sizeof(buf));
7580 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08007581 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[RX-STBC-?[1-3]*\\]//' %s", config_file);
7582 if (os_snprintf_error(sizeof(cmd), res)) {
7583 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7584 return RETURN_ERR;
7585 }
7586
developera3511852023-06-14 14:12:59 +08007587 _syscmd(cmd, buf, sizeof(buf));
7588 }
7589 wifi_hostapdRead(config_file, list.name, buf, sizeof(buf));
7590 list.value = buf;
7591 wifi_hostapdProcessUpdate(radioIndex, &list, 1);
7592 }
developere40952c2023-06-15 18:46:43 +08007593 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7594 if (os_snprintf_error(sizeof(dat_file), res)) {
7595 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7596 return RETURN_ERR;
7597 }
7598
7599 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_STBC=.*/HT_STBC=%d/g' %s", STBC_Enable, dat_file);
7600 if (os_snprintf_error(sizeof(cmd), res)) {
7601 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7602 return RETURN_ERR;
7603 }
7604
developera1255e42023-05-13 17:45:02 +08007605 _syscmd(cmd, buf, sizeof(buf));
7606 if ((band == band_5) || (band == band_6)) {
developere40952c2023-06-15 18:46:43 +08007607 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^VHT_STBC=.*/VHT_STBC=%d/g' %s", STBC_Enable, dat_file);
7608 if (os_snprintf_error(sizeof(cmd), res)) {
7609 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7610 return RETURN_ERR;
7611 }
7612
developera1255e42023-05-13 17:45:02 +08007613 _syscmd(cmd, buf, sizeof(buf));
7614 }
developera3511852023-06-14 14:12:59 +08007615 /*wifi_reloadAp(radioIndex);
developera1255e42023-05-13 17:45:02 +08007616 the caller do this.*/
developer72fb0bb2023-01-11 09:46:29 +08007617
developera3511852023-06-14 14:12:59 +08007618 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7619 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007620}
7621
7622// outputs A-MSDU enable status, 0 == not enabled, 1 == enabled
7623INT wifi_getRadioAMSDUEnable(INT radioIndex, BOOL *output_bool)
7624{
developera3511852023-06-14 14:12:59 +08007625 char dat_file[128] = {0};
developer2c22d832023-05-18 17:46:26 +08007626 wifi_band band;
7627 char amdus_buff[8] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007628 int res;
developer72fb0bb2023-01-11 09:46:29 +08007629
developer2c22d832023-05-18 17:46:26 +08007630 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007631
developer2c22d832023-05-18 17:46:26 +08007632 band = wifi_index_to_band(radioIndex);
7633 if (band == band_invalid) {
7634 printf("%s:Band Error\n", __func__);
7635 return RETURN_ERR;
7636 }
developere40952c2023-06-15 18:46:43 +08007637 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7638 if (os_snprintf_error(sizeof(dat_file), res)) {
7639 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7640 return RETURN_ERR;
7641 }
7642
developer2c22d832023-05-18 17:46:26 +08007643 wifi_datfileRead(dat_file, "HT_AMSDU", amdus_buff, sizeof(amdus_buff));
7644 if (strncmp(amdus_buff, "1", 1) == 0)
developera3511852023-06-14 14:12:59 +08007645 *output_bool = TRUE;
developer2c22d832023-05-18 17:46:26 +08007646 else
7647 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08007648
developer2c22d832023-05-18 17:46:26 +08007649 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7650
7651 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007652}
7653
7654// enables A-MSDU in the hardware, 0 == not enabled, 1 == enabled
7655INT wifi_setRadioAMSDUEnable(INT radioIndex, BOOL amsduEnable)
7656{
developer2c22d832023-05-18 17:46:26 +08007657 char dat_file[128] = {0};
7658 BOOL enable;
7659 wifi_band band;
7660 char amdus_buff[8] = {'\0'};
7661 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08007662 int res;
developer72fb0bb2023-01-11 09:46:29 +08007663
developer2c22d832023-05-18 17:46:26 +08007664 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007665
developer2c22d832023-05-18 17:46:26 +08007666 band = wifi_index_to_band(radioIndex);
7667 if (band == band_invalid) {
7668 printf("%s:Band Error\n", __func__);
7669 return RETURN_ERR;
7670 }
developere40952c2023-06-15 18:46:43 +08007671 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7672 if (os_snprintf_error(sizeof(dat_file), res)) {
7673 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7674 return RETURN_ERR;
7675 }
7676
developer2c22d832023-05-18 17:46:26 +08007677 wifi_datfileRead(dat_file, "HT_AMSDU", amdus_buff, sizeof(amdus_buff));
7678 if (strncmp(amdus_buff, "1", 1) == 0)
developera3511852023-06-14 14:12:59 +08007679 enable = TRUE;
developer2c22d832023-05-18 17:46:26 +08007680 else
7681 enable = FALSE;
7682 if (amsduEnable == enable)
developera3511852023-06-14 14:12:59 +08007683 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007684
developer2c22d832023-05-18 17:46:26 +08007685 params.name = "HT_AMSDU";
7686 if (amsduEnable)
7687 params.value = "1";
7688 else
7689 params.value = "0";
7690 wifi_datfileWrite(dat_file, &params, 1);
7691 wifi_reloadAp(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08007692
developer2c22d832023-05-18 17:46:26 +08007693 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007694
developera3511852023-06-14 14:12:59 +08007695 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007696}
7697
7698//P2 // outputs the number of Tx streams
7699INT wifi_getRadioTxChainMask(INT radioIndex, INT *output_int)
7700{
developera3511852023-06-14 14:12:59 +08007701 char buf[8] = {0};
7702 char cmd[128] = {0};
7703 int phyId = 0;
developere40952c2023-06-15 18:46:43 +08007704 int res;
developerc14d83a2023-06-29 20:09:42 +08007705 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007706
developera3511852023-06-14 14:12:59 +08007707 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007708
developera3511852023-06-14 14:12:59 +08007709 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +08007710 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'Configured Antennas' | awk '{print $4}'", phyId);
7711 if (os_snprintf_error(sizeof(cmd), res)) {
7712 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7713 return RETURN_ERR;
7714 }
7715
developera3511852023-06-14 14:12:59 +08007716 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007717
developerc14d83a2023-06-29 20:09:42 +08007718 if (hal_strtol(buf, 16, &tmp) < 0) {
7719 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007720 }
developerc14d83a2023-06-29 20:09:42 +08007721 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007722
developera3511852023-06-14 14:12:59 +08007723 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007724
developera3511852023-06-14 14:12:59 +08007725 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007726}
7727
7728INT fitChainMask(INT radioIndex, int antcount)
7729{
developera3511852023-06-14 14:12:59 +08007730 char buf[128] = {0};
7731 char cmd[128] = {0};
7732 char config_file[64] = {0};
7733 wifi_band band;
7734 struct params list[2] = {0};
developere40952c2023-06-15 18:46:43 +08007735 int res;
developer72fb0bb2023-01-11 09:46:29 +08007736
developera3511852023-06-14 14:12:59 +08007737 band = wifi_index_to_band(radioIndex);
7738 if (band == band_invalid)
7739 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007740
developera3511852023-06-14 14:12:59 +08007741 list[0].name = "he_mu_beamformer";
7742 list[1].name = "he_su_beamformer";
developer72fb0bb2023-01-11 09:46:29 +08007743
developere40952c2023-06-15 18:46:43 +08007744 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7745 if (os_snprintf_error(sizeof(config_file), res)) {
7746 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7747 return RETURN_ERR;
7748 }
7749
developera3511852023-06-14 14:12:59 +08007750 if (antcount == 1) {
7751 // remove config about multiple antennas
developere40952c2023-06-15 18:46:43 +08007752 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7753 if (os_snprintf_error(sizeof(cmd), res)) {
7754 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7755 return RETURN_ERR;
7756 }
7757
developera3511852023-06-14 14:12:59 +08007758 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007759
developere40952c2023-06-15 18:46:43 +08007760 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SOUNDING-DIMENSION-.\\]//' %s", config_file);
7761 if (os_snprintf_error(sizeof(cmd), res)) {
7762 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7763 return RETURN_ERR;
7764 }
7765
developera3511852023-06-14 14:12:59 +08007766 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007767
developere40952c2023-06-15 18:46:43 +08007768 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SU-BEAMFORMER\\]//' %s", config_file);
7769 if (os_snprintf_error(sizeof(cmd), res)) {
7770 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7771 return RETURN_ERR;
7772 }
7773
developera3511852023-06-14 14:12:59 +08007774 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007775
developere40952c2023-06-15 18:46:43 +08007776 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[MU-BEAMFORMER\\]//' %s", config_file);
7777 if (os_snprintf_error(sizeof(cmd), res)) {
7778 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7779 return RETURN_ERR;
7780 }
7781
developera3511852023-06-14 14:12:59 +08007782 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007783
developera3511852023-06-14 14:12:59 +08007784 list[0].value = "0";
7785 list[1].value = "0";
7786 } else {
7787 // If we only set RX STBC means STBC is enable and TX STBC is disable when last time set one antenna. so we need to add it back.
7788 if (band == band_2_4 || band == band_5) {
developere40952c2023-06-15 18:46:43 +08007789 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '^ht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7790 if (os_snprintf_error(sizeof(cmd), res)) {
7791 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7792 return RETURN_ERR;
7793 }
7794
developera3511852023-06-14 14:12:59 +08007795 _syscmd(cmd, buf, sizeof(buf));
7796 if (strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +08007797 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC]/' %s", config_file);
7798 if (os_snprintf_error(sizeof(cmd), res)) {
7799 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7800 return RETURN_ERR;
7801 }
7802
developera3511852023-06-14 14:12:59 +08007803 _syscmd(cmd, buf, sizeof(buf));
7804 }
7805 }
7806 if (band == band_5) {
developere40952c2023-06-15 18:46:43 +08007807 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '^vht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7808 if (os_snprintf_error(sizeof(cmd), res)) {
7809 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7810 return RETURN_ERR;
7811 }
7812
developera3511852023-06-14 14:12:59 +08007813 _syscmd(cmd, buf, sizeof(buf));
7814 if (strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +08007815 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1]/' %s", config_file);
7816 if (os_snprintf_error(sizeof(cmd), res)) {
7817 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7818 return RETURN_ERR;
7819 }
7820
developera3511852023-06-14 14:12:59 +08007821 _syscmd(cmd, buf, sizeof(buf));
7822 }
7823 }
developer72fb0bb2023-01-11 09:46:29 +08007824
developere40952c2023-06-15 18:46:43 +08007825 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[SU-BEAMFORMER\\]'", config_file);
7826 if (os_snprintf_error(sizeof(cmd), res)) {
7827 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7828 return RETURN_ERR;
7829 }
7830
developera3511852023-06-14 14:12:59 +08007831 _syscmd(cmd, buf, sizeof(buf));
7832 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007833 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SU-BEAMFORMER]/' %s", config_file);
7834 if (os_snprintf_error(sizeof(cmd), res)) {
7835 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7836 return RETURN_ERR;
7837 }
7838
developera3511852023-06-14 14:12:59 +08007839 _syscmd(cmd, buf, sizeof(buf));
7840 }
developer72fb0bb2023-01-11 09:46:29 +08007841
developere40952c2023-06-15 18:46:43 +08007842 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[MU-BEAMFORMER\\]'", config_file);
developerc14d83a2023-06-29 20:09:42 +08007843 if (os_snprintf_error(sizeof(cmd), res)) {
7844 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7845 return RETURN_ERR;
7846 }
developera3511852023-06-14 14:12:59 +08007847 _syscmd(cmd, buf, sizeof(buf));
7848 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007849 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[MU-BEAMFORMER]/' %s", config_file);
7850 if (os_snprintf_error(sizeof(cmd), res)) {
7851 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7852 return RETURN_ERR;
7853 }
7854
developera3511852023-06-14 14:12:59 +08007855 _syscmd(cmd, buf, sizeof(buf));
7856 }
developer72fb0bb2023-01-11 09:46:29 +08007857
developere40952c2023-06-15 18:46:43 +08007858 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[SOUNDING-DIMENSION-.\\]'", config_file);
7859 if (os_snprintf_error(sizeof(cmd), res)) {
7860 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7861 return RETURN_ERR;
7862 }
7863
developera3511852023-06-14 14:12:59 +08007864 _syscmd(cmd, buf, sizeof(buf));
7865 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007866 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SOUNDING-DIMENSION-%d]/' %s", antcount, config_file);
developera3511852023-06-14 14:12:59 +08007867 } else {
developere40952c2023-06-15 18:46:43 +08007868 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/(SOUNDING-DIMENSION-)./\\1%d/' %s", antcount, config_file);
7869 }
7870 if (os_snprintf_error(sizeof(cmd), res)) {
7871 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7872 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08007873 }
developere40952c2023-06-15 18:46:43 +08007874
developera3511852023-06-14 14:12:59 +08007875 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007876
developera3511852023-06-14 14:12:59 +08007877 list[0].value = "1";
7878 list[1].value = "1";
7879 }
7880 wifi_hostapdWrite(config_file, list, 2);
developerdaf24792023-06-06 11:40:04 +08007881 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007882}
7883
7884//P2 // sets the number of Tx streams to an enviornment variable
7885INT wifi_setRadioTxChainMask(INT radioIndex, INT numStreams)
7886{
developera3511852023-06-14 14:12:59 +08007887 char cmd[128] = {0};
7888 char buf[128] = {0};
7889 int phyId = 0;
7890 int cur_mask = 0;
7891 int antcountmsk = 0;
developera1255e42023-05-13 17:45:02 +08007892 INT cur_nss = 0;
developer863a4a62023-06-06 16:55:59 +08007893 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +08007894 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007895 int res;
developer72fb0bb2023-01-11 09:46:29 +08007896
developera3511852023-06-14 14:12:59 +08007897 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007898
developera3511852023-06-14 14:12:59 +08007899 if (numStreams <= 0) {
developer75bd10c2023-06-27 11:34:08 +08007900 wifi_debug(DEBUG_ERROR, "chainmask is not supported %d.\n", numStreams);
developera3511852023-06-14 14:12:59 +08007901 return RETURN_ERR;
7902 }
developer72fb0bb2023-01-11 09:46:29 +08007903
developera3511852023-06-14 14:12:59 +08007904 wifi_getRadioTxChainMask(radioIndex, &cur_mask);//this is mask value
developera1255e42023-05-13 17:45:02 +08007905 for(; cur_mask > 0; cur_mask >>= 1)//convert to number of streams.
7906 cur_nss += 1;
7907 WIFI_ENTRY_EXIT_DEBUG("%s:cur_nss=%d, new_nss=%d\n", __func__, cur_nss, numStreams);
developera3511852023-06-14 14:12:59 +08007908 if (cur_nss == numStreams)
7909 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007910
developera3511852023-06-14 14:12:59 +08007911 wifi_setRadioEnable(radioIndex, FALSE);
developer72fb0bb2023-01-11 09:46:29 +08007912
developera3511852023-06-14 14:12:59 +08007913 phyId = radio_index_to_phy(radioIndex);
developera1255e42023-05-13 17:45:02 +08007914 //iw need mask value.
7915 for (;numStreams > 0; numStreams--)
7916 antcountmsk |= 0x1 << (numStreams - 1);
developere40952c2023-06-15 18:46:43 +08007917 res = snprintf(cmd, sizeof(cmd), "iw phy%d set antenna 0x%x 2>&1", phyId, antcountmsk);
7918 if (os_snprintf_error(sizeof(cmd), res)) {
7919 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7920 return RETURN_ERR;
7921 }
developerb758dfd2023-06-21 17:32:07 +08007922
developera3511852023-06-14 14:12:59 +08007923 _syscmd(cmd, buf, sizeof(buf));
7924 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007925 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007926 return RETURN_ERR;
7927 }
7928 band = wifi_index_to_band(radioIndex);
developera1255e42023-05-13 17:45:02 +08007929 if (band == band_invalid) {
7930 printf("%s:Band Error\n", __func__);
7931 return RETURN_ERR;
7932 }
developere40952c2023-06-15 18:46:43 +08007933 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7934 if (os_snprintf_error(sizeof(dat_file), res)) {
7935 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7936 return RETURN_ERR;
7937 }
developerb758dfd2023-06-21 17:32:07 +08007938
developere40952c2023-06-15 18:46:43 +08007939 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_TxStream=.*/HT_TxStream=%d/g' %s", numStreams, dat_file);
7940 if (os_snprintf_error(sizeof(cmd), res)) {
7941 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7942 return RETURN_ERR;
7943 }
developerb758dfd2023-06-21 17:32:07 +08007944
developera1255e42023-05-13 17:45:02 +08007945 _syscmd(cmd, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +08007946 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007947 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007948 return RETURN_ERR;
7949 }
developere40952c2023-06-15 18:46:43 +08007950 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_RxStream=.*/HT_RxStream=%d/g' %s", numStreams, dat_file);
7951 if (os_snprintf_error(sizeof(cmd), res)) {
7952 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7953 return RETURN_ERR;
7954 }
developerb758dfd2023-06-21 17:32:07 +08007955
developera1255e42023-05-13 17:45:02 +08007956 _syscmd(cmd, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +08007957 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007958 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007959 return RETURN_ERR;
7960 }
7961 fitChainMask(radioIndex, numStreams);
7962 wifi_setRadioEnable(radioIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +08007963
developera3511852023-06-14 14:12:59 +08007964 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7965 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007966}
7967
7968//P2 // outputs the number of Rx streams
7969INT wifi_getRadioRxChainMask(INT radioIndex, INT *output_int)
7970{
developera3511852023-06-14 14:12:59 +08007971 char buf[8] = {0};
7972 char cmd[128] = {0};
7973 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +08007974 int res;
developerc14d83a2023-06-29 20:09:42 +08007975 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007976
developera3511852023-06-14 14:12:59 +08007977 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007978
developera3511852023-06-14 14:12:59 +08007979 phyId = radio_index_to_phy(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08007980
7981 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'Configured Antennas' | awk '{print $6}'", phyId);
7982 if (os_snprintf_error(sizeof(cmd), res)) {
7983 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7984 return RETURN_ERR;
7985 }
developera3511852023-06-14 14:12:59 +08007986 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007987
developerc14d83a2023-06-29 20:09:42 +08007988 if (hal_strtol(buf, 16, &tmp) < 0) {
7989 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007990 }
developerc14d83a2023-06-29 20:09:42 +08007991 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007992
developera3511852023-06-14 14:12:59 +08007993 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007994
developera3511852023-06-14 14:12:59 +08007995 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007996}
7997
7998//P2 // sets the number of Rx streams to an enviornment variable
7999INT wifi_setRadioRxChainMask(INT radioIndex, INT numStreams)
8000{
developera3511852023-06-14 14:12:59 +08008001 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8002 if (wifi_setRadioTxChainMask(radioIndex, numStreams) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08008003 wifi_debug(DEBUG_ERROR, "wifi_setRadioTxChainMask return error.\n");
developera3511852023-06-14 14:12:59 +08008004 return RETURN_ERR;
8005 }
8006 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8007 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008008}
8009
8010//Get radio RDG enable setting
8011INT wifi_getRadioReverseDirectionGrantSupported(INT radioIndex, BOOL *output_bool)
8012{
developer47cc27a2023-05-17 23:09:58 +08008013 if (NULL == output_bool)
8014 return RETURN_ERR;
8015
8016 *output_bool = TRUE;
8017 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008018}
8019
8020//Get radio RDG enable setting
8021INT wifi_getRadioReverseDirectionGrantEnable(INT radioIndex, BOOL *output_bool)
8022{
developer47cc27a2023-05-17 23:09:58 +08008023 char rdg_status[2] = {0};
8024 char dat_file[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008025 int res;
developer47cc27a2023-05-17 23:09:58 +08008026
8027 if (NULL == output_bool)
8028 return RETURN_ERR;
8029
8030 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08008031 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
8032 if (os_snprintf_error(sizeof(dat_file), res)) {
8033 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8034 return RETURN_ERR;
8035 }
developer47cc27a2023-05-17 23:09:58 +08008036
8037 wifi_datfileRead(dat_file, "HT_RDG", rdg_status, sizeof(rdg_status));
8038 if (!strncmp(rdg_status, "1", sizeof(rdg_status)))
8039 *output_bool = TRUE;
8040 else
8041 *output_bool = FALSE;
8042
8043 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008044}
8045
8046//Set radio RDG enable setting
8047INT wifi_setRadioReverseDirectionGrantEnable(INT radioIndex, BOOL enable)
8048{
developer47cc27a2023-05-17 23:09:58 +08008049 char dat_file[MAX_CMD_SIZE] = {0};
8050 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08008051 int res;
developer47cc27a2023-05-17 23:09:58 +08008052
8053 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08008054 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
8055 if (os_snprintf_error(sizeof(dat_file), res)) {
8056 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8057 return RETURN_ERR;
8058 }
developer47cc27a2023-05-17 23:09:58 +08008059
8060 params.name = "HT_RDG";
8061
developera3511852023-06-14 14:12:59 +08008062 if (enable) {
8063 params.value = "1";
8064 } else {
8065 params.value = "0";
8066 }
developer47cc27a2023-05-17 23:09:58 +08008067
8068 wifi_datfileWrite(dat_file, &params, 1);
8069
8070 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008071}
8072
developer5cd4c862023-05-26 09:34:42 +08008073
8074int mtk_get_ba_auto_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08008075{
developer5cd4c862023-05-26 09:34:42 +08008076 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8077 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
8078 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8079 unsigned char status;
8080 unsigned char *out_status = data;
8081 int err = 0;
developer8e6583c2023-05-23 13:36:06 +08008082
developer5cd4c862023-05-26 09:34:42 +08008083 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8084 genlmsg_attrlen(gnlh, 0), NULL);
8085 if (err < 0){
8086 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8087 return err;
8088 }
developer8e6583c2023-05-23 13:36:06 +08008089
developer5cd4c862023-05-26 09:34:42 +08008090 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8091 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
8092 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8093 if (err < 0){
8094 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8095 return err;
8096 }
developer8e6583c2023-05-23 13:36:06 +08008097
developer5cd4c862023-05-26 09:34:42 +08008098 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]) {
8099 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]);
8100 if (status == 0) {
8101 wifi_debug(DEBUG_NOTICE, "disabled\n");
8102 } else {
8103 wifi_debug(DEBUG_NOTICE, "enabled\n");
8104 }
8105 *out_status = status;
8106 }
8107 }
developer8e6583c2023-05-23 13:36:06 +08008108
developer5cd4c862023-05-26 09:34:42 +08008109 return 0;
8110}
developer8e6583c2023-05-23 13:36:06 +08008111
developer5cd4c862023-05-26 09:34:42 +08008112int mtk_get_ba_decline_status_callback(struct nl_msg *msg, void *data)
8113{
8114 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8115 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
8116 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8117 unsigned char status;
8118 unsigned char *out_status = data;
8119 int err = 0;
8120
8121 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8122 genlmsg_attrlen(gnlh, 0), NULL);
8123 if (err < 0) {
8124 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8125 return err;
8126 }
8127
8128 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8129 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
8130 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8131 if (err < 0) {
8132 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8133 return err;
8134 }
8135
8136 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]) {
8137 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]);
8138 if (status == 0) {
8139 wifi_debug(DEBUG_NOTICE, "disabled\n");
8140 } else {
8141 wifi_debug(DEBUG_NOTICE, "enabled\n");
8142 }
8143 *out_status = status;
8144 }
8145 }
8146
8147 return NL_OK;
developer72fb0bb2023-01-11 09:46:29 +08008148}
8149
developer5cd4c862023-05-26 09:34:42 +08008150INT mtk_wifi_get_ba_decl_auto_status(
8151 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back, BOOL *output_bool)
8152{
8153 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008154 unsigned int if_idx = 0;
8155 int ret = -1;
8156 struct unl unl_ins;
8157 struct nl_msg *msg = NULL;
8158 struct nlattr * msg_data = NULL;
8159 struct mtk_nl80211_param param;
8160
8161 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8162 return RETURN_ERR;
8163 if_idx = if_nametoindex(inf_name);
8164 if (!if_idx) {
8165 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8166 return RETURN_ERR;
8167 }
8168 /*init mtk nl80211 vendor cmd*/
8169 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8170 param.if_type = NL80211_ATTR_IFINDEX;
8171 param.if_idx = if_idx;
8172
8173 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8174 if (ret) {
8175 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8176 return RETURN_ERR;
8177 }
8178 /*add mtk vendor cmd data*/
8179 if (nla_put_u8(msg, vendor_data_attr, 0xf)) {
8180 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8181 nlmsg_free(msg);
8182 goto err;
8183 }
8184
8185 /*send mtk nl80211 vendor msg*/
8186 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8187 if (ret) {
8188 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8189 goto err;
8190 }
8191 /*deinit mtk nl80211 vendor msg*/
8192 mtk_nl80211_deint(&unl_ins);
8193 wifi_debug(DEBUG_NOTICE,"send cmd success, get output_bool:%d\n", *output_bool);
8194 return RETURN_OK;
8195err:
8196 mtk_nl80211_deint(&unl_ins);
8197 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8198 return RETURN_ERR;
8199}
developere0ff7232023-06-08 16:33:14 +08008200
8201INT mtk_wifi_set_auto_ba_en(
8202 INT apIndex, INT vendor_data_attr, BOOL enable)
8203{
8204 char inf_name[IF_NAME_SIZE] = {0};
8205 unsigned int if_idx = 0;
8206 int ret = -1;
8207 struct unl unl_ins;
8208 struct nl_msg *msg = NULL;
8209 struct nlattr * msg_data = NULL;
8210 struct mtk_nl80211_param param;
8211
8212 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8213 return RETURN_ERR;
8214 if_idx = if_nametoindex(inf_name);
8215 if (!if_idx) {
8216 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8217 return RETURN_ERR;
8218 }
8219 /*init mtk nl80211 vendor cmd*/
8220 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8221 param.if_type = NL80211_ATTR_IFINDEX;
8222 param.if_idx = if_idx;
8223
8224 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8225 if (ret) {
8226 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8227 return RETURN_ERR;
8228 }
8229 /*add mtk vendor cmd data*/
8230 if (nla_put_u8(msg, vendor_data_attr, enable)) {
8231 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8232 nlmsg_free(msg);
8233 goto err;
8234 }
8235
8236 /*send mtk nl80211 vendor msg*/
8237 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8238 if (ret) {
8239 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8240 goto err;
8241 }
8242 /*deinit mtk nl80211 vendor msg*/
8243 mtk_nl80211_deint(&unl_ins);
8244 return RETURN_OK;
8245err:
8246 mtk_nl80211_deint(&unl_ins);
8247 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8248 return RETURN_ERR;
8249}
8250
developer5cd4c862023-05-26 09:34:42 +08008251//Get radio ADDBA enable setting
8252INT wifi_getRadioDeclineBARequestEnable(INT radioIndex, BOOL *output_bool)
8253{
8254 if (output_bool == NULL) {
8255 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8256 return RETURN_ERR;
8257 }
8258 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
8259 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO, mtk_get_ba_decline_status_callback, output_bool) != RETURN_OK) {
8260 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO(0x%x) fails\n",
8261 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO);
8262 return RETURN_ERR;
8263 }
8264 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8265 return RETURN_OK;
8266}
8267
developer72fb0bb2023-01-11 09:46:29 +08008268//Set radio ADDBA enable setting
8269INT wifi_setRadioDeclineBARequestEnable(INT radioIndex, BOOL enable)
8270{
developera3511852023-06-14 14:12:59 +08008271 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008272}
8273
8274//Get radio auto block ack enable setting
8275INT wifi_getRadioAutoBlockAckEnable(INT radioIndex, BOOL *output_bool)
8276{
developer5cd4c862023-05-26 09:34:42 +08008277 if (output_bool == NULL) {
8278 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8279 return RETURN_ERR;
8280 }
developer8e6583c2023-05-23 13:36:06 +08008281
developera3511852023-06-14 14:12:59 +08008282 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
developer5cd4c862023-05-26 09:34:42 +08008283 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO,
8284 mtk_get_ba_auto_status_callback, output_bool) != RETURN_OK) {
8285 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO(0x%x) fails\n",
8286 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO);
8287 return RETURN_ERR;
8288 }
8289 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8290 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008291}
8292
8293//Set radio auto block ack enable setting
8294INT wifi_setRadioAutoBlockAckEnable(INT radioIndex, BOOL enable)
8295{
developera3511852023-06-14 14:12:59 +08008296 if (mtk_wifi_set_auto_ba_en
developere0ff7232023-06-08 16:33:14 +08008297 (radioIndex, MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO, enable) != RETURN_OK) {
8298 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO cmd fails\n");
8299 return RETURN_ERR;
8300 }
8301 wifi_debug(DEBUG_ERROR, "send cmd success: set auto ba enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008302 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008303}
8304
8305//Get radio 11n pure mode enable support
8306INT wifi_getRadio11nGreenfieldSupported(INT radioIndex, BOOL *output_bool)
8307{
developera3511852023-06-14 14:12:59 +08008308 if (NULL == output_bool)
8309 return RETURN_ERR;
8310 *output_bool = TRUE;
8311 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008312}
8313
8314//Get radio 11n pure mode enable setting
8315INT wifi_getRadio11nGreenfieldEnable(INT radioIndex, BOOL *output_bool)
8316{
developera3511852023-06-14 14:12:59 +08008317 if (NULL == output_bool)
8318 return RETURN_ERR;
8319 *output_bool = TRUE;
8320 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008321}
8322
8323//Set radio 11n pure mode enable setting
developer86035662023-06-28 19:21:12 +08008324INT wifi_setRadio11nGreenfieldEnable(INT radioIndex, BOOL enable)
developer72fb0bb2023-01-11 09:46:29 +08008325{
developer82533be2023-06-28 17:21:01 +08008326 char interface_name[16] = {0};
8327 int if_idx, ret = 0;
8328 struct nl_msg *msg = NULL;
8329 struct nlattr * msg_data = NULL;
8330 struct mtk_nl80211_param param;
8331 struct unl unl_ins;
8332
8333 if (radioIndex > MAX_APS) {
8334 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", radioIndex);
8335 return RETURN_ERR;
8336 }
8337 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8338 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
8339 return RETURN_ERR;
developerd14dff12023-06-28 22:47:44 +08008340
developer82533be2023-06-28 17:21:01 +08008341 if_idx = if_nametoindex(interface_name);
8342 /*init mtk nl80211 vendor cmd*/
8343 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
8344 param.if_type = NL80211_ATTR_IFINDEX;
8345 param.if_idx = if_idx;
8346 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8347 if (ret) {
8348 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8349 return RETURN_ERR;
8350 }
8351 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_HT_OP_MODE, enable)) {
8352 printf("Nla put attribute error\n");
8353 nlmsg_free(msg);
8354 goto err;
8355 }
8356 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8357 if (ret) {
8358 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
8359 goto err;
8360 }
8361 mtk_nl80211_deint(&unl_ins);
8362 //wifi_debug(DEBUG_NOTICE, "set Gf cmd success.\n");
8363 printf("set gf=%d cmd success.\n", enable);
8364 return RETURN_OK;
8365err:
8366 mtk_nl80211_deint(&unl_ins);
8367 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08008368 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008369}
8370
developer5cd4c862023-05-26 09:34:42 +08008371int mtk_get_igmp_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08008372{
developer5cd4c862023-05-26 09:34:42 +08008373 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8374 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX + 1];
8375 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8376 unsigned char status = 0, *out_status = data;
8377 int err = 0;
developer72fb0bb2023-01-11 09:46:29 +08008378
developer5cd4c862023-05-26 09:34:42 +08008379 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8380 genlmsg_attrlen(gnlh, 0), NULL);
8381 if (err < 0) {
8382 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8383 return err;
8384 }
developer72fb0bb2023-01-11 09:46:29 +08008385
developer5cd4c862023-05-26 09:34:42 +08008386 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8387 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX,
8388 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8389 if (err < 0){
8390 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX fails\n");
8391 return err;
8392 }
developer72fb0bb2023-01-11 09:46:29 +08008393
developer5cd4c862023-05-26 09:34:42 +08008394 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]) {
8395 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]);
8396 if (status == 0) {
8397 wifi_debug(DEBUG_NOTICE, "disabled\n");
8398 } else {
8399 wifi_debug(DEBUG_NOTICE, "enabled\n");
8400 }
8401 *out_status = status;
8402 wifi_debug(DEBUG_NOTICE, "status: %d\n", *out_status);
8403 }
8404 }
8405
8406 return 0;
8407}
8408
8409INT mtk_wifi_set_igmp_en_status(
8410 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
8411 unsigned char in_en_stat, BOOL *output_bool)
8412{
8413 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008414 unsigned int if_idx = 0;
8415 int ret = -1;
8416 struct unl unl_ins;
8417 struct nl_msg *msg = NULL;
8418 struct nlattr * msg_data = NULL;
8419 struct mtk_nl80211_param param;
8420
8421 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8422 return RETURN_ERR;
8423 if_idx = if_nametoindex(inf_name);
8424 if (!if_idx) {
8425 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8426 return RETURN_ERR;
8427 }
8428 /*init mtk nl80211 vendor cmd*/
8429 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_MULTICAST_SNOOPING;
8430 param.if_type = NL80211_ATTR_IFINDEX;
8431 param.if_idx = if_idx;
8432
8433 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8434 if (ret) {
8435 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8436 return RETURN_ERR;
8437 }
8438 /*add mtk vendor cmd data*/
8439 if (nla_put_u8(msg, vendor_data_attr, in_en_stat)) {
8440 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8441 nlmsg_free(msg);
8442 goto err;
8443 }
8444
8445 /*send mtk nl80211 vendor msg*/
8446 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8447 if (ret) {
8448 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8449 goto err;
8450 }
8451 /*deinit mtk nl80211 vendor msg*/
8452 mtk_nl80211_deint(&unl_ins);
8453 if (output_bool) {
8454 wifi_debug(DEBUG_NOTICE, "send cmd success, get output_bool:%d\n", *output_bool);
8455 } else {
8456 wifi_debug(DEBUG_NOTICE, "send cmd success.\n");
8457 }
8458 return RETURN_OK;
8459err:
8460 mtk_nl80211_deint(&unl_ins);
8461 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8462 return RETURN_ERR;
8463}
8464
8465
8466//Get radio IGMP snooping enable setting
8467INT wifi_getRadioIGMPSnoopingEnable(INT radioIndex, BOOL *output_bool)
8468{
8469 if (output_bool == NULL) {
8470 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8471 return RETURN_ERR;
8472 }
developera3511852023-06-14 14:12:59 +08008473 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008474 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8475 mtk_get_igmp_status_callback, 0xf, output_bool)!= RETURN_OK) {
8476 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8477 return RETURN_ERR;
8478 }
8479 wifi_debug(DEBUG_ERROR, "send cmd success: get igmp status:(%d)\n", *output_bool);
developera3511852023-06-14 14:12:59 +08008480 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008481}
8482
8483//Set radio IGMP snooping enable setting
8484INT wifi_setRadioIGMPSnoopingEnable(INT radioIndex, BOOL enable)
8485{
developera3511852023-06-14 14:12:59 +08008486 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008487 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8488 NULL, enable, NULL) != RETURN_OK) {
8489 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8490 return RETURN_ERR;
8491 }
8492 wifi_debug(DEBUG_ERROR, "send cmd success: set igmp enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008493 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008494}
8495
8496//Get the Reset count of radio
developer69b61b02023-03-07 17:17:44 +08008497INT wifi_getRadioResetCount(INT radioIndex, ULONG *output_int)
developer72fb0bb2023-01-11 09:46:29 +08008498{
developera3511852023-06-14 14:12:59 +08008499 if (NULL == output_int)
8500 return RETURN_ERR;
8501 *output_int = get_radio_reset_cnt(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08008502
developera3511852023-06-14 14:12:59 +08008503 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008504}
8505
8506
8507//---------------------------------------------------------------------------------------------------
8508//
8509// Additional Wifi AP level APIs used for Access Point devices
8510//
8511//---------------------------------------------------------------------------------------------------
8512
8513// creates a new ap and pushes these parameters to the hardware
8514INT wifi_createAp(INT apIndex, INT radioIndex, CHAR *essid, BOOL hideSsid)
8515{
developera3511852023-06-14 14:12:59 +08008516 // Deprecated when use hal version 3, use wifi_createVap() instead.
8517 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008518}
8519
8520// deletes this ap entry on the hardware, clears all internal variables associaated with this ap
8521INT wifi_deleteAp(INT apIndex)
8522{
developer7e4a2a62023-04-06 19:56:03 +08008523 char interface_name[16] = {0};
8524 char buf[MAX_BUF_SIZE];
8525 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08008526 int res;
developer72fb0bb2023-01-11 09:46:29 +08008527
developer7e4a2a62023-04-06 19:56:03 +08008528 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
8529 return RETURN_ERR;
developer8a3bbbf2023-03-15 17:47:23 +08008530
developere40952c2023-06-15 18:46:43 +08008531 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
8532 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
8533 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8534 return RETURN_ERR;
8535 }
developer7e4a2a62023-04-06 19:56:03 +08008536 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008537
developer7e4a2a62023-04-06 19:56:03 +08008538 wifi_removeApSecVaribles(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008539
developer7e4a2a62023-04-06 19:56:03 +08008540 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008541}
8542
8543// Outputs a 16 byte or less name assocated with the AP. String buffer must be pre-allocated by the caller
8544INT wifi_getApName(INT apIndex, CHAR *output_string)
8545{
developer7e4a2a62023-04-06 19:56:03 +08008546 char interface_name[IF_NAME_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08008547 int radio_idx = 0;
8548 int bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008549 int res;
developer72fb0bb2023-01-11 09:46:29 +08008550
developer7e4a2a62023-04-06 19:56:03 +08008551 if(!output_string)
8552 return RETURN_ERR;
8553
8554 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
8555 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
8556
developere40952c2023-06-15 18:46:43 +08008557 res = snprintf(output_string, IF_NAME_SIZE, "%s%d", ext_prefix[radio_idx], bss_idx); // For wifiagent generating data model.
developer7e4a2a62023-04-06 19:56:03 +08008558 } else
developere40952c2023-06-15 18:46:43 +08008559 res = snprintf(output_string, IF_NAME_SIZE, "%s", interface_name);
8560
8561 if (os_snprintf_error(IF_NAME_SIZE, res)) {
8562 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8563 return RETURN_ERR;
8564 }
developer7e4a2a62023-04-06 19:56:03 +08008565
8566 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008567}
8568
8569// Outputs the index number in that corresponds to the SSID string
8570INT wifi_getIndexFromName(CHAR *inputSsidString, INT *output_int)
8571{
developer7e4a2a62023-04-06 19:56:03 +08008572 char cmd [128] = {0};
8573 char buf[32] = {0};
8574 char ap_idx = 0;
8575 char *apIndex_str = NULL;
8576 char radio_idx = 0;
8577 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008578 int res;
developerc14d83a2023-06-29 20:09:42 +08008579 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08008580
developere40952c2023-06-15 18:46:43 +08008581 res = snprintf(cmd, sizeof(cmd), "grep -rn ^interface=%s$ /nvram/hostapd*.conf | cut -d '.' -f1 | cut -d 'd' -f2 | tr -d '\\n'",
developer7e4a2a62023-04-06 19:56:03 +08008582 inputSsidString);
developere40952c2023-06-15 18:46:43 +08008583
8584 if (os_snprintf_error(sizeof(cmd), res)) {
8585 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8586 return RETURN_ERR;
8587 }
8588
developer7e4a2a62023-04-06 19:56:03 +08008589 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008590
developer7e4a2a62023-04-06 19:56:03 +08008591 if (strlen(buf)) {
8592 apIndex_str = strtok(buf, "\n");
developerd14dff12023-06-28 22:47:44 +08008593 if (apIndex_str == NULL) {
8594 wifi_debug(DEBUG_ERROR, "strtok fail\n");
8595 return RETURN_ERR;
8596 }
developerc14d83a2023-06-29 20:09:42 +08008597 if (hal_strtol(apIndex_str, 10, &tmp) < 0) {
8598 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08008599 }
developerc14d83a2023-06-29 20:09:42 +08008600 *output_int = tmp;
developer7e4a2a62023-04-06 19:56:03 +08008601 return RETURN_OK;
8602 }
developer72fb0bb2023-01-11 09:46:29 +08008603
developer7e4a2a62023-04-06 19:56:03 +08008604 /* If interface name is not in hostapd config, the caller maybe wifi agent to generate data model.*/
8605 if (strstr(inputSsidString, PREFIX_WIFI6G)) {
8606 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI6G));
8607 radio_idx = 2;
8608 } else if (strstr(inputSsidString, PREFIX_WIFI5G)) {
8609 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI5G));
8610 radio_idx = 1;
8611 } else if (strstr(inputSsidString, PREFIX_WIFI2G)) {
8612 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI2G));
8613 radio_idx = 0;
8614 } else {
8615 printf("%s: hostapd conf not find, unknow inf(%s), return ERROR!!!(%d).\n",
8616 __func__, inputSsidString, ap_idx);
developera3511852023-06-14 14:12:59 +08008617 *output_int = -1;
8618 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08008619 }
8620
8621 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
8622
8623 if (ap_idx >= 0 && ap_idx < MAX_VAP) {
8624 printf("%s: hostapd conf not find, inf(%s), use inf idx(%d).\n",
8625 __func__, inputSsidString, ap_idx);
8626 *output_int = ap_idx;
8627 return RETURN_OK;
8628 }
8629
8630 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008631}
8632
8633INT wifi_getApIndexFromName(CHAR *inputSsidString, INT *output_int)
8634{
developera3511852023-06-14 14:12:59 +08008635 return wifi_getIndexFromName(inputSsidString, output_int);
developer72fb0bb2023-01-11 09:46:29 +08008636}
8637
8638// Outputs a 32 byte or less string indicating the beacon type as "None", "Basic", "WPA", "11i", "WPAand11i"
8639INT wifi_getApBeaconType(INT apIndex, CHAR *output_string)
8640{
developera3511852023-06-14 14:12:59 +08008641 char buf[MAX_BUF_SIZE] = {0};
8642 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008643 int res;
developer72fb0bb2023-01-11 09:46:29 +08008644
developera3511852023-06-14 14:12:59 +08008645 if(NULL == output_string)
8646 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008647
developer32f2a182023-06-27 19:50:41 +08008648 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
8649 if (os_snprintf_error(sizeof(config_file), res)) {
8650 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8651 return RETURN_ERR;
8652 }
developera3511852023-06-14 14:12:59 +08008653 wifi_hostapdRead(config_file, "wpa", buf, sizeof(buf));
8654 if((strcmp(buf,"3")==0))
developere40952c2023-06-15 18:46:43 +08008655 res = snprintf(output_string, 32, "WPAand11i");
developera3511852023-06-14 14:12:59 +08008656 else if((strcmp(buf,"2")==0))
developere40952c2023-06-15 18:46:43 +08008657 res = snprintf(output_string, 32, "11i");
developera3511852023-06-14 14:12:59 +08008658 else if((strcmp(buf,"1")==0))
developere40952c2023-06-15 18:46:43 +08008659 res = snprintf(output_string, 32, "WPA");
developera3511852023-06-14 14:12:59 +08008660 else
developere40952c2023-06-15 18:46:43 +08008661 res = snprintf(output_string, 32, "None");
8662 if (os_snprintf_error(32, res)) {
8663 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8664 return RETURN_ERR;
8665 }
developer72fb0bb2023-01-11 09:46:29 +08008666
developera3511852023-06-14 14:12:59 +08008667 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008668}
8669
8670// Sets the beacon type enviornment variable. Allowed input strings are "None", "Basic", "WPA, "11i", "WPAand11i"
8671INT wifi_setApBeaconType(INT apIndex, CHAR *beaconTypeString)
8672{
developera3511852023-06-14 14:12:59 +08008673 char config_file[MAX_BUF_SIZE] = {0};
8674 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008675 int res;
developer72fb0bb2023-01-11 09:46:29 +08008676
developera3511852023-06-14 14:12:59 +08008677 if (NULL == beaconTypeString)
8678 return RETURN_ERR;
8679 list.name = "wpa";
8680 list.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008681
developera3511852023-06-14 14:12:59 +08008682 if((strcmp(beaconTypeString,"WPAand11i")==0))
8683 list.value="3";
8684 else if((strcmp(beaconTypeString,"11i")==0))
8685 list.value="2";
8686 else if((strcmp(beaconTypeString,"WPA")==0))
8687 list.value="1";
developer72fb0bb2023-01-11 09:46:29 +08008688
developer75bd10c2023-06-27 11:34:08 +08008689 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8690 if (os_snprintf_error(sizeof(config_file), res)) {
8691 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8692 return RETURN_ERR;
8693 }
developera3511852023-06-14 14:12:59 +08008694 wifi_hostapdWrite(config_file, &list, 1);
8695 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8696 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
8697 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008698}
8699
8700// sets the beacon interval on the hardware for this AP
8701INT wifi_setApBeaconInterval(INT apIndex, INT beaconInterval)
8702{
developera3511852023-06-14 14:12:59 +08008703 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8704 struct params params={'\0'};
8705 char buf[MAX_BUF_SIZE] = {'\0'};
8706 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08008707 int res;
developer72fb0bb2023-01-11 09:46:29 +08008708
developera3511852023-06-14 14:12:59 +08008709 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08008710 res = snprintf(buf, sizeof(buf), "%u", beaconInterval);
8711 if (os_snprintf_error(sizeof(buf), res)) {
8712 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8713 return RETURN_ERR;
8714 }
developera3511852023-06-14 14:12:59 +08008715 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08008716
developer75bd10c2023-06-27 11:34:08 +08008717 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8718 if (os_snprintf_error(sizeof(config_file), res)) {
8719 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8720 return RETURN_ERR;
8721 }
developera3511852023-06-14 14:12:59 +08008722 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08008723
developera3511852023-06-14 14:12:59 +08008724 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8725 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8726 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008727}
8728
8729INT wifi_setDTIMInterval(INT apIndex, INT dtimInterval)
8730{
developera3511852023-06-14 14:12:59 +08008731 if (wifi_setApDTIMInterval(apIndex, dtimInterval) != RETURN_OK)
8732 return RETURN_ERR;
8733 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008734}
8735
8736// Get the packet size threshold supported.
8737INT wifi_getApRtsThresholdSupported(INT apIndex, BOOL *output_bool)
8738{
developera3511852023-06-14 14:12:59 +08008739 //save config and apply instantly
8740 if (NULL == output_bool)
8741 return RETURN_ERR;
8742 *output_bool = TRUE;
8743 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008744}
8745
8746// sets the packet size threshold in bytes to apply RTS/CTS backoff rules.
8747INT wifi_setApRtsThreshold(INT apIndex, UINT threshold)
8748{
developera3511852023-06-14 14:12:59 +08008749 char buf[16] = {0};
8750 char config_file[128] = {0};
8751 struct params param = {0};
developere40952c2023-06-15 18:46:43 +08008752 int res;
developer72fb0bb2023-01-11 09:46:29 +08008753
developera3511852023-06-14 14:12:59 +08008754 if (threshold > 65535) {
developer75bd10c2023-06-27 11:34:08 +08008755 wifi_debug(DEBUG_ERROR, "rts threshold %u is too big.\n", threshold);
developera3511852023-06-14 14:12:59 +08008756 return RETURN_ERR;
8757 }
developer72fb0bb2023-01-11 09:46:29 +08008758
developere40952c2023-06-15 18:46:43 +08008759 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8760 if (os_snprintf_error(sizeof(config_file), res)) {
8761 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8762 return RETURN_ERR;
8763 }
8764
8765 res = snprintf(buf, sizeof(buf), "%u", threshold);
8766 if (os_snprintf_error(sizeof(buf), res)) {
8767 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8768 return RETURN_ERR;
8769 }
developera3511852023-06-14 14:12:59 +08008770 param.name = "rts_threshold";
8771 param.value = buf;
8772 wifi_hostapdWrite(config_file, &param, 1);
8773 wifi_hostapdProcessUpdate(apIndex, &param, 1);
8774 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008775
developera3511852023-06-14 14:12:59 +08008776 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008777}
8778
8779// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8780INT wifi_getApWpaEncryptoinMode(INT apIndex, CHAR *output_string)
8781{
developere40952c2023-06-15 18:46:43 +08008782 int res;
8783
developera3511852023-06-14 14:12:59 +08008784 if (NULL == output_string)
8785 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08008786 res = snprintf(output_string, 32, "TKIPandAESEncryption");
8787 if (os_snprintf_error(32, res)) {
8788 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8789 return RETURN_ERR;
8790 }
developera3511852023-06-14 14:12:59 +08008791 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008792
8793}
8794
8795// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8796INT wifi_getApWpaEncryptionMode(INT apIndex, CHAR *output_string)
8797{
developera3511852023-06-14 14:12:59 +08008798 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8799 char *param_name = NULL;
8800 char buf[32] = {0}, config_file[MAX_BUF_SIZE] = {0};
developerc79e9172023-06-06 19:48:03 +08008801 unsigned int len;
developere40952c2023-06-15 18:46:43 +08008802 int res;
developer72fb0bb2023-01-11 09:46:29 +08008803
developera3511852023-06-14 14:12:59 +08008804 if(NULL == output_string)
8805 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008806
developer75bd10c2023-06-27 11:34:08 +08008807 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8808 if (os_snprintf_error(sizeof(config_file), res)) {
8809 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8810 return RETURN_ERR;
8811 }
8812
developera3511852023-06-14 14:12:59 +08008813 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008814
developera3511852023-06-14 14:12:59 +08008815 if(strcmp(buf,"0")==0)
8816 {
8817 printf("%s: wpa_mode is %s ......... \n", __func__, buf);
developere40952c2023-06-15 18:46:43 +08008818 res = snprintf(output_string, 32, "None");
8819 if (os_snprintf_error(32, res)) {
8820 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8821 return RETURN_ERR;
8822 }
developera3511852023-06-14 14:12:59 +08008823 return RETURN_OK;
8824 }
8825 else if((strcmp(buf,"3")==0) || (strcmp(buf,"2")==0))
8826 param_name = "rsn_pairwise";
8827 else if((strcmp(buf,"1")==0))
8828 param_name = "wpa_pairwise";
8829 else
8830 return RETURN_ERR;
8831 memset(output_string,'\0',32);
8832 wifi_hostapdRead(config_file,param_name,output_string,32);
8833 if (strlen(output_string) == 0) { // rsn_pairwise is optional. When it is empty use wpa_pairwise instead.
8834 param_name = "wpa_pairwise";
8835 memset(output_string, '\0', 32);
8836 wifi_hostapdRead(config_file, param_name, output_string, 32);
8837 }
8838 wifi_dbg_printf("\n%s output_string=%s",__func__,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008839
developera3511852023-06-14 14:12:59 +08008840 if(strcmp(output_string,"TKIP CCMP") == 0) {
developerc79e9172023-06-06 19:48:03 +08008841 len = strlen("TKIPandAESEncryption");
8842 memcpy(output_string,"TKIPandAESEncryption", len);
8843 output_string[len] = '\0';
8844 } else if(strcmp(output_string,"TKIP") == 0) {
8845 len = strlen("TKIPEncryption");
8846 memcpy(output_string,"TKIPEncryption", len);
8847 output_string[len] = '\0';
8848 } else if(strcmp(output_string,"CCMP") == 0) {
8849 len = strlen("AESEncryption");
8850 memcpy(output_string,"AESEncryption", len);
8851 output_string[len] = '\0';
8852 }
developer72fb0bb2023-01-11 09:46:29 +08008853
developera3511852023-06-14 14:12:59 +08008854 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8855 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008856}
8857
8858// sets the encyption mode enviornment variable. Valid string format is "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8859INT wifi_setApWpaEncryptionMode(INT apIndex, CHAR *encMode)
8860{
developera3511852023-06-14 14:12:59 +08008861 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8862 struct params params={'\0'};
8863 char output_string[32];
8864 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08008865 int res;
developer72fb0bb2023-01-11 09:46:29 +08008866
developera3511852023-06-14 14:12:59 +08008867 memset(output_string,'\0',32);
8868 wifi_getApBeaconType(apIndex,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008869
developera3511852023-06-14 14:12:59 +08008870 if(strcmp(encMode, "TKIPEncryption") == 0)
8871 params.value = "TKIP";
8872 else if(strcmp(encMode,"AESEncryption") == 0)
8873 params.value = "CCMP";
8874 else if(strcmp(encMode,"TKIPandAESEncryption") == 0)
8875 params.value = "TKIP CCMP";
developer72fb0bb2023-01-11 09:46:29 +08008876
developera3511852023-06-14 14:12:59 +08008877 if((strcmp(output_string,"WPAand11i")==0))
8878 {
8879 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008880 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8881 if (os_snprintf_error(sizeof(config_file), res)) {
8882 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8883 return RETURN_ERR;
8884 }
developera3511852023-06-14 14:12:59 +08008885 wifi_hostapdWrite(config_file, &params, 1);
8886 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008887
developera3511852023-06-14 14:12:59 +08008888 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008889 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8890 if (os_snprintf_error(sizeof(config_file), res)) {
8891 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8892 return RETURN_ERR;
8893 }
developera3511852023-06-14 14:12:59 +08008894 wifi_hostapdWrite(config_file, &params, 1);
8895 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008896
developera3511852023-06-14 14:12:59 +08008897 return RETURN_OK;
8898 }
8899 else if((strcmp(output_string,"11i")==0))
8900 {
8901 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008902 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8903 if (os_snprintf_error(sizeof(config_file), res)) {
8904 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8905 return RETURN_ERR;
8906 }
developera3511852023-06-14 14:12:59 +08008907 wifi_hostapdWrite(config_file, &params, 1);
8908 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8909 return RETURN_OK;
8910 }
8911 else if((strcmp(output_string,"WPA")==0))
8912 {
8913 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008914 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8915 if (os_snprintf_error(sizeof(config_file), res)) {
8916 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8917 return RETURN_ERR;
8918 }
developera3511852023-06-14 14:12:59 +08008919 wifi_hostapdWrite(config_file, &params, 1);
8920 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8921 return RETURN_OK;
8922 }
developer72fb0bb2023-01-11 09:46:29 +08008923
developera3511852023-06-14 14:12:59 +08008924 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8925 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008926}
8927
8928// deletes internal security varable settings for this ap
8929INT wifi_removeApSecVaribles(INT apIndex)
8930{
developer0155a502023-06-19 20:33:57 +08008931 char config_file[MAX_BUF_SIZE] = {0};
8932 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008933 int res;
developer0155a502023-06-19 20:33:57 +08008934
8935 list.name = "wpa";
8936 list.value = "0";
8937
developer75bd10c2023-06-27 11:34:08 +08008938 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8939 if (os_snprintf_error(sizeof(config_file), res)) {
8940 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8941 return RETURN_ERR;
8942 }
developer0155a502023-06-19 20:33:57 +08008943 wifi_hostapdWrite(config_file, &list, 1);
8944
8945 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008946}
8947
8948// changes the hardware settings to disable encryption on this ap
8949INT wifi_disableApEncryption(INT apIndex)
8950{
developer0155a502023-06-19 20:33:57 +08008951 char config_file[MAX_BUF_SIZE] = {0};
8952 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008953 int res;
developer0155a502023-06-19 20:33:57 +08008954
8955 list.name = "wpa";
8956 list.value = "0";
8957
developer75bd10c2023-06-27 11:34:08 +08008958 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8959 if (os_snprintf_error(sizeof(config_file), res)) {
8960 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8961 return RETURN_ERR;
8962 }
developer0155a502023-06-19 20:33:57 +08008963 wifi_hostapdWrite(config_file, &list, 1);
8964 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8965 wifi_reloadAp(apIndex);
8966
8967 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008968}
8969
8970// set the authorization mode on this ap
8971// mode mapping as: 1: open, 2: shared, 4:auto
8972INT wifi_setApAuthMode(INT apIndex, INT mode)
8973{
developera3511852023-06-14 14:12:59 +08008974 struct params params={0};
8975 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08008976 int res;
developer72fb0bb2023-01-11 09:46:29 +08008977
developera3511852023-06-14 14:12:59 +08008978 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008979
developera3511852023-06-14 14:12:59 +08008980 wifi_dbg_printf("\n%s algo_mode=%d", __func__, mode);
8981 params.name = "auth_algs";
developer72fb0bb2023-01-11 09:46:29 +08008982
developere5750452023-05-15 16:46:42 +08008983 if ((mode & 1 && mode & 2) || mode & 4)
developera3511852023-06-14 14:12:59 +08008984 params.value = "3";
8985 else if (mode & 2)
8986 params.value = "2";
8987 else if (mode & 1)
8988 params.value = "1";
8989 else
8990 params.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008991
developer75bd10c2023-06-27 11:34:08 +08008992 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8993 if (os_snprintf_error(sizeof(config_file), res)) {
8994 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8995 return RETURN_ERR;
8996 }
developera3511852023-06-14 14:12:59 +08008997 wifi_hostapdWrite(config_file, &params, 1);
8998 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +08008999 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +08009000 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08009001
developera3511852023-06-14 14:12:59 +08009002 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009003}
9004
9005// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
9006INT wifi_setApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
9007{
developera3511852023-06-14 14:12:59 +08009008 //save to wifi config, and wait for wifi restart to apply
9009 struct params params={'\0'};
9010 char config_file[MAX_BUF_SIZE] = {0};
9011 int ret;
developer72fb0bb2023-01-11 09:46:29 +08009012
developera3511852023-06-14 14:12:59 +08009013 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9014 if(authMode == NULL)
9015 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009016
developera3511852023-06-14 14:12:59 +08009017 wifi_dbg_printf("\n%s AuthMode=%s",__func__,authMode);
9018 params.name = "wpa_key_mgmt";
developer72fb0bb2023-01-11 09:46:29 +08009019
developera3511852023-06-14 14:12:59 +08009020 if((strcmp(authMode,"PSKAuthentication") == 0) || (strcmp(authMode,"SharedAuthentication") == 0))
9021 params.value = "WPA-PSK";
9022 else if(strcmp(authMode,"EAPAuthentication") == 0)
9023 params.value = "WPA-EAP";
9024 else if (strcmp(authMode, "SAEAuthentication") == 0)
9025 params.value = "SAE";
9026 else if (strcmp(authMode, "EAP_192-bit_Authentication") == 0)
9027 params.value = "WPA-EAP-SUITE-B-192";
9028 else if (strcmp(authMode, "PSK-SAEAuthentication") == 0)
9029 params.value = "WPA-PSK WPA-PSK-SHA256 SAE";
9030 else if (strcmp(authMode, "Enhanced_Open") == 0)
9031 params.value = "OWE";
9032 else if(strcmp(authMode,"None") == 0) //Donot change in case the authMode is None
9033 return RETURN_OK; //This is taken careof in beaconType
developer72fb0bb2023-01-11 09:46:29 +08009034
developer32f2a182023-06-27 19:50:41 +08009035 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
9036 if (os_snprintf_error(sizeof(config_file), ret)) {
9037 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9038 return RETURN_ERR;
9039 }
developera3511852023-06-14 14:12:59 +08009040 ret=wifi_hostapdWrite(config_file,&params,1);
9041 if(!ret)
9042 ret=wifi_hostapdProcessUpdate(apIndex, &params, 1);
9043 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08009044
developera3511852023-06-14 14:12:59 +08009045 return ret;
developer72fb0bb2023-01-11 09:46:29 +08009046}
9047
9048// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
9049INT wifi_getApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
9050{
developera3511852023-06-14 14:12:59 +08009051 //save to wifi config, and wait for wifi restart to apply
9052 char BeaconType[50] = {0};
9053 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08009054 int res;
developer32f2a182023-06-27 19:50:41 +08009055 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08009056
developera3511852023-06-14 14:12:59 +08009057 *authMode = 0;
9058 wifi_getApBeaconType(apIndex,BeaconType);
9059 printf("%s____%s \n",__FUNCTION__,BeaconType);
developer72fb0bb2023-01-11 09:46:29 +08009060
developer32f2a182023-06-27 19:50:41 +08009061 if(strcmp(BeaconType,"None") == 0) {
9062 memcpy(authMode, "None", 4);
9063 authMode[4] = '\0';
9064 } else {
9065 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
developer75bd10c2023-06-27 11:34:08 +08009066 if (os_snprintf_error(sizeof(config_file), res)) {
9067 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9068 return RETURN_ERR;
9069 }
developera3511852023-06-14 14:12:59 +08009070 wifi_hostapdRead(config_file, "wpa_key_mgmt", authMode, 32);
9071 wifi_dbg_printf("\n[%s]: AuthMode Name is : %s",__func__,authMode);
developer32f2a182023-06-27 19:50:41 +08009072 if(strcmp(authMode,"WPA-PSK") == 0) {
9073 len = strlen("SharedAuthentication");
9074 memcpy(authMode, "SharedAuthentication", len);
9075 authMode[len] = '\0';
9076 } else if(strcmp(authMode,"WPA-EAP") == 0) {
9077 len = strlen("EAPAuthentication");
9078 memcpy(authMode, "EAPAuthentication", len);
9079 authMode[len] = '\0';
9080 }
developera3511852023-06-14 14:12:59 +08009081 }
developer72fb0bb2023-01-11 09:46:29 +08009082
developera3511852023-06-14 14:12:59 +08009083 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009084}
9085
9086// Outputs the number of stations associated per AP
9087INT wifi_getApNumDevicesAssociated(INT apIndex, ULONG *output_ulong)
9088{
developera3511852023-06-14 14:12:59 +08009089 char interface_name[16] = {0};
9090 char cmd[128]={0};
9091 char buf[128]={0};
9092 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +08009093 int res;
developer72fb0bb2023-01-11 09:46:29 +08009094
developera3511852023-06-14 14:12:59 +08009095 if(apIndex > MAX_APS)
9096 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009097
developera3511852023-06-14 14:12:59 +08009098 wifi_getApEnable(apIndex,&status);
9099 if (!status)
9100 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009101
developera3511852023-06-14 14:12:59 +08009102 //sprintf(cmd, "iw dev %s station dump | grep Station | wc -l", interface_name);//alternate method
9103 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9104 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08009105 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta | wc -l", interface_name);
9106 if (os_snprintf_error(sizeof(cmd), res)) {
9107 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9108 return RETURN_ERR;
9109 }
developera3511852023-06-14 14:12:59 +08009110 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08009111 if (sscanf(buf,"%lu", output_ulong) != 1) {
9112 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
9113 return RETURN_ERR;
9114 }
developer72fb0bb2023-01-11 09:46:29 +08009115
developera3511852023-06-14 14:12:59 +08009116 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009117}
9118
9119// manually removes any active wi-fi association with the device specified on this ap
9120INT wifi_kickApAssociatedDevice(INT apIndex, CHAR *client_mac)
9121{
developera3511852023-06-14 14:12:59 +08009122 char inf_name[16] = {0};
9123 char cmd[MAX_CMD_SIZE] = {0};
9124 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08009125 int res;
developer72fb0bb2023-01-11 09:46:29 +08009126
developera3511852023-06-14 14:12:59 +08009127 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9128 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08009129
9130 res = snprintf(cmd, sizeof(cmd),"hostapd_cli -i %s disassociate %s", inf_name, client_mac);
9131 if (os_snprintf_error(sizeof(cmd), res)) {
9132 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9133 return RETURN_ERR;
9134 }
developer7e4a2a62023-04-06 19:56:03 +08009135
developera3511852023-06-14 14:12:59 +08009136 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08009137
developera3511852023-06-14 14:12:59 +08009138 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009139}
9140
9141// outputs the radio index for the specified ap. similar as wifi_getSsidRadioIndex
9142INT wifi_getApRadioIndex(INT apIndex, INT *output_int)
9143{
developer7e4a2a62023-04-06 19:56:03 +08009144 int max_radio_num = 0;
9145
9146 if(NULL == output_int)
9147 return RETURN_ERR;
9148
9149 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08009150 if(max_radio_num == 0){
9151 return RETURN_ERR;
9152 }
developer7e4a2a62023-04-06 19:56:03 +08009153 *output_int = apIndex % max_radio_num;
9154
9155 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009156}
9157
9158// sets the radio index for the specific ap
9159INT wifi_setApRadioIndex(INT apIndex, INT radioIndex)
9160{
developera3511852023-06-14 14:12:59 +08009161 //set to config only and wait for wifi reset to apply settings
9162 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009163}
9164
developer0155a502023-06-19 20:33:57 +08009165int mtk_get_ap_metrics(struct nl_msg *msg, void *cb)
9166{
9167 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9168 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009169 struct genlmsghdr *gnlh;
developer0155a502023-06-19 20:33:57 +08009170 wdev_ap_metric ap_metric;
9171 wdev_ap_metric *p_ap_metric = &ap_metric;
9172 int err = 0;
9173 struct mtk_nl80211_cb_data *cb_data = cb;
9174
9175 if (!msg || !cb_data) {
developerc14d83a2023-06-29 20:09:42 +08009176 wifi_debug(DEBUG_ERROR, "msgor cb_data is null,error.\n");
developer0155a502023-06-19 20:33:57 +08009177 return NL_SKIP;
9178 }
developerc14d83a2023-06-29 20:09:42 +08009179 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer0155a502023-06-19 20:33:57 +08009180
9181 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9182 genlmsg_attrlen(gnlh, 0), NULL);
9183 if (err < 0) {
9184 wifi_debug(DEBUG_ERROR, "nla_parse ap_metrics nl80211 msg fails,error.\n");
9185 return err;
9186 }
9187
9188 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9189 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX,
9190 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9191 if (err < 0) {
9192 wifi_debug(DEBUG_ERROR, "GET_STATISTIC_MAX fails,error.\n");
9193 return err;
9194 }
9195
9196 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]) {
9197 p_ap_metric = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]);
9198 if (p_ap_metric) {
9199 memcpy(cb_data->out_buf , &p_ap_metric->cu, sizeof(unsigned char));
9200 }
9201 }
9202 }
9203
9204 return NL_OK;
9205}
9206
developer121a8e72023-05-22 09:19:39 +08009207
9208#define MAX_ACL_DUMP_LEN 4096
9209int mtk_acl_list_dump_callback(struct nl_msg *msg, void *cb)
9210{
9211 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9212 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009213 struct genlmsghdr *gnlh;
developer121a8e72023-05-22 09:19:39 +08009214 char *show_str = NULL;
developer2edaf012023-05-24 14:24:53 +08009215 int err = 0;
developer121a8e72023-05-22 09:19:39 +08009216 unsigned short acl_result_len = 0;
9217 struct mtk_nl80211_cb_data *cb_data = cb;
developer121a8e72023-05-22 09:19:39 +08009218 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08009219 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developer121a8e72023-05-22 09:19:39 +08009220 return NL_SKIP;
9221 }
developerc14d83a2023-06-29 20:09:42 +08009222
9223 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer121a8e72023-05-22 09:19:39 +08009224 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9225 genlmsg_attrlen(gnlh, 0), NULL);
9226 if (err < 0) {
developer2edaf012023-05-24 14:24:53 +08009227 wifi_debug(DEBUG_ERROR, "nla_parse acl list nl80211 msg fails,error.\n");
developer121a8e72023-05-22 09:19:39 +08009228 return NL_SKIP;
9229 }
developer121a8e72023-05-22 09:19:39 +08009230 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9231 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX,
9232 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9233 if (err < 0)
9234 return NL_SKIP;
9235 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]) {
9236 acl_result_len = nla_len(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9237 show_str = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9238 if (acl_result_len > MAX_ACL_DUMP_LEN) {
9239 wifi_debug(DEBUG_ERROR,"the scan result len is invalid !!!\n");
9240 return NL_SKIP;
9241 } else if (*(show_str + acl_result_len - 1) != '\0') {
9242 wifi_debug(DEBUG_INFO, "the result string is not ended with right terminator, handle it!!!\n");
9243 *(show_str + acl_result_len - 1) = '\0';
9244 }
9245 wifi_debug(DEBUG_INFO, "driver msg:%s\n", show_str);
developer2edaf012023-05-24 14:24:53 +08009246
9247 if (cb_data->out_len >= acl_result_len) {
9248 memset(cb_data->out_buf, 0, cb_data->out_len);
9249 /*skip the first line: 'policy=1\n' to find the acl mac addrs*/
9250 memmove(cb_data->out_buf, show_str, acl_result_len);
9251 wifi_debug(DEBUG_INFO, "out buff:%s\n", cb_data->out_buf);
9252 } else {
9253 memset(cb_data->out_buf, 0, cb_data->out_len);
developer121a8e72023-05-22 09:19:39 +08009254 }
developer121a8e72023-05-22 09:19:39 +08009255 } else
9256 wifi_debug(DEBUG_ERROR, "no acl result attr\n");
9257 } else
9258 wifi_debug(DEBUG_ERROR, "no any acl result from driver\n");
9259 return NL_OK;
9260}
developer72fb0bb2023-01-11 09:46:29 +08009261// Get the ACL MAC list per AP
developer2edaf012023-05-24 14:24:53 +08009262INT mtk_wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
developer72fb0bb2023-01-11 09:46:29 +08009263{
developer7e4a2a62023-04-06 19:56:03 +08009264 char inf_name[IF_NAME_SIZE] = {0};
developer121a8e72023-05-22 09:19:39 +08009265 unsigned int if_idx = 0;
9266 int ret = -1;
9267 struct unl unl_ins;
9268 struct nl_msg *msg = NULL;
9269 struct nlattr * msg_data = NULL;
9270 struct mtk_nl80211_param param;
9271 struct mtk_nl80211_cb_data cb_data;
developer7e4a2a62023-04-06 19:56:03 +08009272 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9273 return RETURN_ERR;
developer121a8e72023-05-22 09:19:39 +08009274 if_idx = if_nametoindex(inf_name);
9275 if (!if_idx) {
developer2edaf012023-05-24 14:24:53 +08009276 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
developer121a8e72023-05-22 09:19:39 +08009277 return RETURN_ERR;
9278 }
9279 /*init mtk nl80211 vendor cmd*/
9280 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9281 param.if_type = NL80211_ATTR_IFINDEX;
9282 param.if_idx = if_idx;
9283
9284 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9285 if (ret) {
9286 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9287 return RETURN_ERR;
9288 }
developer121a8e72023-05-22 09:19:39 +08009289 /*add mtk vendor cmd data*/
9290 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_SHOW_ALL)) {
developer2edaf012023-05-24 14:24:53 +08009291 wifi_debug(DEBUG_ERROR, "Nla put ACL_SHOW_ALL attribute error\n");
developer121a8e72023-05-22 09:19:39 +08009292 nlmsg_free(msg);
9293 goto err;
9294 }
developer72fb0bb2023-01-11 09:46:29 +08009295
developer121a8e72023-05-22 09:19:39 +08009296 /*send mtk nl80211 vendor msg*/
9297 cb_data.out_buf = macArray;
9298 cb_data.out_len = buf_size;
9299
9300 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_acl_list_dump_callback, &cb_data);
9301 if (ret) {
9302 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9303 goto err;
9304 }
9305 /*deinit mtk nl80211 vendor msg*/
9306 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009307 wifi_debug(DEBUG_NOTICE,"send cmd success, get out_buf:%s\n", macArray);
developera3511852023-06-14 14:12:59 +08009308 return RETURN_OK;
developer121a8e72023-05-22 09:19:39 +08009309err:
9310 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009311 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developer121a8e72023-05-22 09:19:39 +08009312 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009313}
9314
developer2edaf012023-05-24 14:24:53 +08009315INT wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9316{
9317 char *mac_arry_buf = NULL;
9318
9319 mac_arry_buf = malloc(buf_size);
9320 if (!mac_arry_buf) {
9321 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
9322 return RETURN_ERR;
9323 }
9324 memset(mac_arry_buf, 0, buf_size);
9325 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
9326 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
9327 free(mac_arry_buf);
9328 mac_arry_buf = NULL;
9329 return RETURN_ERR;
9330 }
9331 /*
9332 mtk format to wifi hal format:
9333 "policy=1
9334 00:11:22:33:44:55
9335 00:11:22:33:44:66
9336 "
9337 -->
9338 "00:11:22:33:44:55
9339 00:11:22:33:44:66
9340 "
9341 */
9342 memset(macArray, 0, buf_size);
9343 if (*mac_arry_buf != '\0' && strchr(mac_arry_buf,'\n')) {
9344 memmove(macArray, strchr(mac_arry_buf,'\n')+1, strlen(strchr(mac_arry_buf,'\n')+1)+1);
9345 wifi_debug(DEBUG_NOTICE,"macArray:\n%s\n", macArray);
9346 }
9347 free(mac_arry_buf);
9348 mac_arry_buf = NULL;
9349 return RETURN_OK;
9350}
9351
developer72fb0bb2023-01-11 09:46:29 +08009352INT wifi_getApDenyAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9353{
developer72fb0bb2023-01-11 09:46:29 +08009354
developer7e4a2a62023-04-06 19:56:03 +08009355 wifi_getApAclDevices(apIndex, macArray, buf_size);
developer72fb0bb2023-01-11 09:46:29 +08009356
developera3511852023-06-14 14:12:59 +08009357 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009358}
9359
9360
9361// Get the list of stations associated per AP
9362INT wifi_getApDevicesAssociated(INT apIndex, CHAR *macArray, UINT buf_size)
9363{
developer7e4a2a62023-04-06 19:56:03 +08009364 char interface_name[IF_NAME_SIZE] = {0};
9365 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08009366 int res;
developer72fb0bb2023-01-11 09:46:29 +08009367
developer7e4a2a62023-04-06 19:56:03 +08009368 if(apIndex > 3) //Currently supporting apIndex upto 3
developera3511852023-06-14 14:12:59 +08009369 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009370
developer7e4a2a62023-04-06 19:56:03 +08009371 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08009372 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009373
developere40952c2023-06-15 18:46:43 +08009374 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta", interface_name);
9375 if (os_snprintf_error(sizeof(cmd), res)) {
9376 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9377 return RETURN_ERR;
9378 }
developer7e4a2a62023-04-06 19:56:03 +08009379 _syscmd(cmd, macArray, buf_size);
9380 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009381}
9382
developer8dd72532023-05-17 19:58:35 +08009383int hex2num(char c)
9384{
9385 if (c >= '0' && c <= '9')
9386 return c - '0';
9387 if (c >= 'a' && c <= 'f')
9388 return c - 'a' + 10;
9389 if (c >= 'A' && c <= 'F')
9390 return c - 'A' + 10;
9391 return -1;
9392}
9393
9394/**
9395 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
9396 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
9397 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
9398 * Returns: Characters used (> 0) on success, -1 on failure
9399 */
9400int hwaddr_aton2(const char *txt, unsigned char *addr)
9401{
9402 int i;
9403 const char *pos = txt;
9404
9405 for (i = 0; i < 6; i++) {
9406 int a, b;
9407
9408 while (*pos == ':' || *pos == '.' || *pos == '-')
9409 pos++;
9410
9411 a = hex2num(*pos++);
9412 if (a < 0)
9413 return -1;
9414 b = hex2num(*pos++);
9415 if (b < 0)
9416 return -1;
9417 *addr++ = (a << 4) | b;
9418 }
9419
9420 return pos - txt;
9421}
9422
developer72fb0bb2023-01-11 09:46:29 +08009423// adds the mac address to the filter list
9424//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9425INT wifi_addApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9426{
developer7e4a2a62023-04-06 19:56:03 +08009427 char inf_name[IF_NAME_SIZE] = {0};
developer8dd72532023-05-17 19:58:35 +08009428 int if_idx, ret = 0;
developer49b17232023-05-19 16:35:19 +08009429 struct nl_msg *msg = NULL;
9430 struct nlattr * msg_data = NULL;
9431 struct mtk_nl80211_param param;
developer8dd72532023-05-17 19:58:35 +08009432 unsigned char mac[ETH_ALEN] = {0x00, 0x0c, 0x43, 0x11, 0x22, 0x33};
9433 struct unl unl_ins;
developer7e4a2a62023-04-06 19:56:03 +08009434 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9435 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009436 if (!DeviceMacAddress)
9437 return RETURN_ERR;
developer8dd72532023-05-17 19:58:35 +08009438 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
developer2edaf012023-05-24 14:24:53 +08009439 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
developer8dd72532023-05-17 19:58:35 +08009440 return RETURN_ERR;
9441 }
developer8dd72532023-05-17 19:58:35 +08009442 if_idx = if_nametoindex(inf_name);
developer2edaf012023-05-24 14:24:53 +08009443 if (!if_idx) {
9444 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9445 return RETURN_ERR;
9446 }
developer49b17232023-05-19 16:35:19 +08009447 /*init mtk nl80211 vendor cmd*/
9448 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9449 param.if_type = NL80211_ATTR_IFINDEX;
9450 param.if_idx = if_idx;
9451 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9452 if (ret) {
9453 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009454 return RETURN_ERR;
9455 }
developer49b17232023-05-19 16:35:19 +08009456 /*add mtk vendor cmd data*/
9457 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_ADD_MAC, ETH_ALEN, mac)) {
developer2edaf012023-05-24 14:24:53 +08009458 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
developer8dd72532023-05-17 19:58:35 +08009459 nlmsg_free(msg);
9460 goto err;
9461 }
developer49b17232023-05-19 16:35:19 +08009462 /*send mtk nl80211 vendor msg*/
9463 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9464 if (ret) {
9465 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009466 goto err;
9467 }
developer49b17232023-05-19 16:35:19 +08009468 /*deinit mtk nl80211 vendor msg*/
9469 mtk_nl80211_deint(&unl_ins);
9470 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer8dd72532023-05-17 19:58:35 +08009471 return RETURN_OK;
9472err:
developer49b17232023-05-19 16:35:19 +08009473 mtk_nl80211_deint(&unl_ins);
9474 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developer8dd72532023-05-17 19:58:35 +08009475 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009476}
9477
9478// deletes the mac address from the filter list
9479//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9480INT wifi_delApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9481{
developer2edaf012023-05-24 14:24:53 +08009482 struct unl unl_ins;
9483 int if_idx = 0, ret = 0;
9484 struct nl_msg *msg = NULL;
9485 struct nlattr * msg_data = NULL;
9486 struct mtk_nl80211_param param;
developer7e4a2a62023-04-06 19:56:03 +08009487 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +08009488 unsigned char mac[ETH_ALEN] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009489
developer7e4a2a62023-04-06 19:56:03 +08009490 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9491 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009492
developer7e4a2a62023-04-06 19:56:03 +08009493 if (!DeviceMacAddress)
9494 return RETURN_ERR;
9495
developer2edaf012023-05-24 14:24:53 +08009496 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
9497 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
9498 return RETURN_ERR;
9499 }
developer72fb0bb2023-01-11 09:46:29 +08009500
developer2edaf012023-05-24 14:24:53 +08009501 if_idx = if_nametoindex(inf_name);
9502 if (!if_idx) {
9503 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9504 return RETURN_ERR;
9505 }
9506 /*init mtk nl80211 vendor cmd*/
9507 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9508 param.if_type = NL80211_ATTR_IFINDEX;
9509 param.if_idx = if_idx;
9510 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9511 if (ret) {
9512 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9513 return RETURN_ERR;
9514 }
9515 /*add mtk vendor cmd data*/
9516 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_DEL_MAC, ETH_ALEN, mac)) {
9517 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9518 nlmsg_free(msg);
9519 goto err;
9520 }
9521 /*send mtk nl80211 vendor msg*/
9522 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9523 if (ret) {
9524 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9525 goto err;
9526 }
9527 /*deinit mtk nl80211 vendor msg*/
9528 mtk_nl80211_deint(&unl_ins);
9529 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
9530 return RETURN_OK;
9531err:
9532 mtk_nl80211_deint(&unl_ins);
9533 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9534 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009535}
9536
9537// outputs the number of devices in the filter list
9538INT wifi_getApAclDeviceNum(INT apIndex, UINT *output_uint)
9539{
developer2edaf012023-05-24 14:24:53 +08009540 char *mac_arry = NULL, *ptr = NULL, mac_str[18] = {0};
9541 UINT buf_size = 1024;
9542 UINT sta_num = 0;
9543 unsigned char mac[ETH_ALEN] = {0};
developera3511852023-06-14 14:12:59 +08009544 if(output_uint == NULL)
developerdaf24792023-06-06 11:40:04 +08009545 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009546
developer2edaf012023-05-24 14:24:53 +08009547 mac_arry = (char *)malloc(buf_size);
9548 if (mac_arry == NULL) {
9549 wifi_debug(DEBUG_ERROR, "malloc mac_arry fails\n");
developer7e4a2a62023-04-06 19:56:03 +08009550 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009551 }
developerdaf24792023-06-06 11:40:04 +08009552 memset(mac_arry, 0, buf_size);
developer2edaf012023-05-24 14:24:53 +08009553 /*mac_arry str format: 00:11:22:33:44:55\n00:11:22:33:44:66\0*/
9554 if (wifi_getApAclDevices(apIndex, mac_arry, buf_size)!= RETURN_OK) {
9555 wifi_debug(DEBUG_ERROR, "get acl list entries fails\n");
developer9ce44382023-06-28 11:09:37 +08009556 free(mac_arry);
developer2edaf012023-05-24 14:24:53 +08009557 return RETURN_ERR;
9558 }
9559 /*count the acl str nums:*/
9560 wifi_debug(DEBUG_NOTICE, "mac_arry: %s\n", mac_arry);
developer7e4a2a62023-04-06 19:56:03 +08009561
developer2edaf012023-05-24 14:24:53 +08009562 /*mac addr string format:
9563 exp1: 00:11:22:33:44:55\0
9564 exp2: 00:11:22:33:44:55\n00:11:22:33:44:66\0
9565 */
9566 ptr = mac_arry;
9567 while (sscanf(ptr, "%17s", mac_str) == 1) {
9568 if (hwaddr_aton2(mac_str, mac) >= 0)
9569 sta_num++;
9570 ptr = strstr(ptr, mac_str) + strlen(mac_str);
9571 }
9572 *output_uint = sta_num;
9573 wifi_debug(DEBUG_NOTICE, "output_uint: %d\n", *output_uint);
9574 free(mac_arry);
9575 mac_arry = NULL;
developer7e4a2a62023-04-06 19:56:03 +08009576 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009577}
9578
9579INT apply_rules(INT apIndex, CHAR *client_mac,CHAR *action,CHAR *interface)
9580{
developer75bd10c2023-06-27 11:34:08 +08009581 int res;
developer72fb0bb2023-01-11 09:46:29 +08009582
developera3511852023-06-14 14:12:59 +08009583 if(strcmp(action,"DENY")==0)
9584 {
developer33f13ba2023-07-12 16:19:06 +08009585 res = v_secure_system("iptables -A WifiServices%d -m physdev --physdev-in %s -m mac --mac-source %s -j DROP",
developer32f2a182023-06-27 19:50:41 +08009586 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009587 if (res) {
9588 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009589 return RETURN_ERR;
9590 }
developera3511852023-06-14 14:12:59 +08009591 return RETURN_OK;
9592 }
developer72fb0bb2023-01-11 09:46:29 +08009593
developera3511852023-06-14 14:12:59 +08009594 if(strcmp(action,"ALLOW")==0)
9595 {
developer33f13ba2023-07-12 16:19:06 +08009596 res = v_secure_system("iptables -I WifiServices%d -m physdev --physdev-in %s -m mac --mac-source %s -j RETURN",
developer32f2a182023-06-27 19:50:41 +08009597 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009598 if (res) {
9599 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009600 return RETURN_ERR;
9601 }
developera3511852023-06-14 14:12:59 +08009602 return RETURN_OK;
9603 }
developer72fb0bb2023-01-11 09:46:29 +08009604
developera3511852023-06-14 14:12:59 +08009605 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009606
9607}
9608
9609// enable kick for devices on acl black list
9610INT wifi_kickApAclAssociatedDevices(INT apIndex, BOOL enable)
9611{
developera3511852023-06-14 14:12:59 +08009612 char aclArray[MAX_BUF_SIZE] = {0}, *acl = NULL;
9613 char assocArray[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009614
developera3511852023-06-14 14:12:59 +08009615 wifi_getApDenyAclDevices(apIndex, aclArray, sizeof(aclArray));
9616 wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray));
developer72fb0bb2023-01-11 09:46:29 +08009617
developera3511852023-06-14 14:12:59 +08009618 /* if there are no devices connected there is nothing to do */
9619 if (strlen(assocArray) < 17)
9620 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009621
developera3511852023-06-14 14:12:59 +08009622 if (enable == TRUE) {
9623 /* kick off the MAC which is in ACL array (deny list) */
9624 acl = strtok(aclArray, "\n");
9625 while (acl != NULL) {
9626 if (strlen(acl) >= 17 && strcasestr(assocArray, acl))
9627 wifi_kickApAssociatedDevice(apIndex, acl);
developer72fb0bb2023-01-11 09:46:29 +08009628
developera3511852023-06-14 14:12:59 +08009629 acl = strtok(NULL, "\n");
9630 }
developer72fb0bb2023-01-11 09:46:29 +08009631 wifi_setApMacAddressControlMode(apIndex, 2);
developera3511852023-06-14 14:12:59 +08009632 } else
developer72fb0bb2023-01-11 09:46:29 +08009633 wifi_setApMacAddressControlMode(apIndex, 0);
developer72fb0bb2023-01-11 09:46:29 +08009634
developera3511852023-06-14 14:12:59 +08009635 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009636}
9637
9638INT wifi_setPreferPrivateConnection(BOOL enable)
9639{
developera3511852023-06-14 14:12:59 +08009640 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009641}
9642
9643// sets the mac address filter control mode. 0 == filter disabled, 1 == filter as whitelist, 2 == filter as blacklist
9644INT wifi_setApMacAddressControlMode(INT apIndex, INT filterMode)
9645{
developer2edaf012023-05-24 14:24:53 +08009646 int if_idx = 0, ret = 0;
9647 struct unl unl_ins;
9648 struct nl_msg *msg = NULL;
9649 struct nlattr * msg_data = NULL;
9650 struct mtk_nl80211_param param;
9651 int acl_policy = -1;
developer7e4a2a62023-04-06 19:56:03 +08009652 char inf_name[IF_NAME_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009653
developer7e4a2a62023-04-06 19:56:03 +08009654 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9655 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009656 if_idx = if_nametoindex(inf_name);
9657 if (!if_idx) {
9658 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9659 return RETURN_ERR;
9660 }
9661 /*init mtk nl80211 vendor cmd*/
9662 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9663 param.if_type = NL80211_ATTR_IFINDEX;
9664 param.if_idx = if_idx;
9665 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9666 if (ret) {
9667 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9668 return RETURN_ERR;
9669 }
9670 /*add mtk vendor cmd data*/
9671 if (filterMode == 0) {
9672 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_DISABLE;
9673 } else if (filterMode == 1) {
9674 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_WHITE_LIST;
9675 } else if (filterMode == 2) {
9676 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_BLACK_LIST;
9677 } else {
9678 wifi_debug(DEBUG_ERROR, "filtermode(%d) not support error\n", filterMode);
9679 nlmsg_free(msg);
9680 goto err;
9681 }
9682 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, acl_policy)) {
9683 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9684 nlmsg_free(msg);
9685 goto err;
9686 }
9687 /*send mtk nl80211 vendor msg*/
9688 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9689 if (ret) {
9690 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9691 goto err;
9692 }
9693 /*deinit mtk nl80211 vendor msg*/
9694 mtk_nl80211_deint(&unl_ins);
9695 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer7e4a2a62023-04-06 19:56:03 +08009696 return RETURN_OK;
developer2edaf012023-05-24 14:24:53 +08009697err:
9698 mtk_nl80211_deint(&unl_ins);
9699 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9700 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009701}
9702
9703// enables internal gateway VLAN mode. In this mode a Vlan tag is added to upstream (received) data packets before exiting the Wifi driver. VLAN tags in downstream data are stripped from data packets before transmission. Default is FALSE.
9704INT wifi_setApVlanEnable(INT apIndex, BOOL VlanEnabled)
9705{
developera3511852023-06-14 14:12:59 +08009706 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009707}
9708
9709// gets the vlan ID for this ap from an internal enviornment variable
9710INT wifi_getApVlanID(INT apIndex, INT *output_int)
9711{
developera3511852023-06-14 14:12:59 +08009712 if(apIndex==0)
9713 {
9714 *output_int=100;
9715 return RETURN_OK;
9716 }
developer72fb0bb2023-01-11 09:46:29 +08009717
developera3511852023-06-14 14:12:59 +08009718 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009719}
9720
9721// sets the vlan ID for this ap to an internal enviornment variable
9722INT wifi_setApVlanID(INT apIndex, INT vlanId)
9723{
developera3511852023-06-14 14:12:59 +08009724 //save the vlanID to config and wait for wifi reset to apply (wifi up module would read this parameters and tag the AP with vlan id)
developer82533be2023-06-28 17:21:01 +08009725 char interface_name[16] = {0};
9726 int if_idx, ret = 0;
9727 struct nl_msg *msg = NULL;
9728 struct nlattr * msg_data = NULL;
9729 struct mtk_nl80211_param param;
9730 struct unl unl_ins;
9731
9732 if (apIndex > MAX_APS) {
9733 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9734 return RETURN_ERR;
9735 }
9736 if (vlanId > 4095 || vlanId < 1) {
9737 wifi_debug(DEBUG_ERROR, "Invalid vlanId %d\n", vlanId);
9738 return RETURN_ERR;
9739 }
9740 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9741 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9742 return RETURN_ERR;
9743 /*step 1. mwctl dev %s set vlan_tag 0*/
9744 if_idx = if_nametoindex(interface_name);
9745 /*init mtk nl80211 vendor cmd*/
9746 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9747 param.if_type = NL80211_ATTR_IFINDEX;
9748 param.if_idx = if_idx;
9749 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9750 if (ret) {
9751 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9752 return RETURN_ERR;
9753 }
9754 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, vlanId)) {
9755 printf("Nla put attribute error\n");
9756 nlmsg_free(msg);
9757 goto err;
9758 }
9759 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9760 if (ret) {
9761 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9762 goto err;
9763 }
9764 mtk_nl80211_deint(&unl_ins);
9765 //wifi_debug(DEBUG_NOTICE, "set vlanId cmd success.\n", vlanId);
9766 printf("set vlanId=%d cmd success.\n", vlanId);
9767 return RETURN_OK;
9768err:
9769 mtk_nl80211_deint(&unl_ins);
9770 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08009771 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009772}
9773
developercc5cbfb2023-06-13 18:29:52 +08009774char br_name[IFNAMSIZ] = "brlan0";
9775
developer72fb0bb2023-01-11 09:46:29 +08009776// gets bridgeName, IP address and Subnet. bridgeName is a maximum of 32 characters,
9777INT wifi_getApBridgeInfo(INT index, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9778{
developercc5cbfb2023-06-13 18:29:52 +08009779 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9780 struct ifreq ifr;
9781 struct sockaddr_in *sin;
9782
9783 memcpy(bridgeName, br_name, strlen(br_name));
9784
9785 if (sock == -1) {
9786 wifi_debug(DEBUG_ERROR, "socket failed");
9787 return RETURN_ERR;
9788 }
9789
developerd14dff12023-06-28 22:47:44 +08009790 strncpy(ifr.ifr_name, br_name, strlen(br_name));
developercc5cbfb2023-06-13 18:29:52 +08009791 ifr.ifr_addr.sa_family = AF_INET;
9792 if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
9793 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFADDR) failed, %s, bridge_name=%s\n",
9794 strerror(errno), br_name);
developer9ce44382023-06-28 11:09:37 +08009795 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009796 return RETURN_ERR;
9797 }
9798
9799 sin = (struct sockaddr_in *)&ifr.ifr_addr;
9800 wifi_debug(DEBUG_ERROR, "Bridge device %s has IP address: %s\n", br_name, inet_ntoa(sin->sin_addr));
9801 memcpy(IP, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9802
9803 if (ioctl(sock, SIOCGIFNETMASK, &ifr) < 0) {
9804 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFNETMASK) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009805 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009806 return RETURN_ERR;
9807 }
9808
9809 wifi_debug(DEBUG_ERROR, "Bridge device %s has subnet mask: %s\n", br_name, inet_ntoa(sin->sin_addr));
9810 memcpy(subnet, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9811 close(sock);
developer72fb0bb2023-01-11 09:46:29 +08009812
developera3511852023-06-14 14:12:59 +08009813 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009814}
9815
9816//sets bridgeName, IP address and Subnet to internal enviornment variables. bridgeName is a maximum of 32 characters
9817INT wifi_setApBridgeInfo(INT apIndex, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9818{
developera3511852023-06-14 14:12:59 +08009819 //save settings, wait for wifi reset or wifi_pushBridgeInfo to apply.
developercc5cbfb2023-06-13 18:29:52 +08009820 struct ifreq ifr;
9821 struct sockaddr_in sin;
9822 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9823
developerc14d83a2023-06-29 20:09:42 +08009824 if(sock < 0) {
9825 wifi_debug(DEBUG_ERROR, "sock init fail\n");
9826 return RETURN_ERR;
9827 }
developer5b23cd02023-07-19 20:26:03 +08009828
developercc5cbfb2023-06-13 18:29:52 +08009829 if (strlen(bridgeName) >= IFNAMSIZ) {
9830 wifi_debug(DEBUG_ERROR, "invalide bridgeName length=%ld\n", strlen(bridgeName));
developer9ce44382023-06-28 11:09:37 +08009831 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009832 return RETURN_ERR;
9833 }
9834
9835 if (strlen(br_name) >= IFNAMSIZ) {
9836 wifi_debug(DEBUG_ERROR, "invalide br_name length=%ld in strorage\n", strlen(br_name));
developer9ce44382023-06-28 11:09:37 +08009837 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009838 return RETURN_ERR;
9839 }
9840
9841 if (sock == -1) {
developera3511852023-06-14 14:12:59 +08009842 wifi_debug(DEBUG_ERROR, "socket failed");
developercc5cbfb2023-06-13 18:29:52 +08009843 return RETURN_ERR;
9844 }
9845
9846 memset(&ifr, 0, sizeof(ifr));
9847 strncpy(ifr.ifr_name, br_name, strlen(br_name));
9848 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9849 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009850 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009851 return RETURN_ERR;
9852 }
9853
9854 ifr.ifr_flags = (short)(ifr.ifr_flags & ~IFF_UP);
9855 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9856 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009857 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009858 return RETURN_ERR;
9859 }
9860
9861 memset(&ifr, 0, sizeof(ifr));
9862 strncpy(ifr.ifr_name, br_name, IFNAMSIZ);
developerd14dff12023-06-28 22:47:44 +08009863 strncpy(ifr.ifr_newname, bridgeName, strlen(bridgeName));
developercc5cbfb2023-06-13 18:29:52 +08009864 if (ioctl(sock, SIOCSIFNAME, &ifr) < 0) {
9865 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNAME) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009866 close(sock);
developera3511852023-06-14 14:12:59 +08009867 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009868 }
9869
9870 memset(br_name, 0, sizeof(br_name));
9871 memcpy(br_name, bridgeName, strlen(bridgeName));
9872
9873 memset(&ifr, 0, sizeof(ifr));
9874 strncpy(ifr.ifr_name, bridgeName, IFNAMSIZ);
9875 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9876 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009877 close(sock);
developera3511852023-06-14 14:12:59 +08009878 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009879 }
9880 ifr.ifr_flags = (short)(ifr.ifr_flags | IFF_UP);
9881 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9882 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009883 close(sock);
developera3511852023-06-14 14:12:59 +08009884 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009885 }
9886
9887 memset(&ifr, 0, sizeof(ifr));
9888 memcpy(ifr.ifr_name, bridgeName, strlen(bridgeName));
9889
9890 memset(&sin, 0, sizeof(struct sockaddr_in));
9891 sin.sin_family = AF_INET;
9892 if (inet_aton(IP, &(sin.sin_addr)) == 0) {
9893 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developer9ce44382023-06-28 11:09:37 +08009894 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009895 return RETURN_ERR;
9896 }
9897 memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr_in));
9898 if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
9899 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFADDR) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009900 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009901 return RETURN_ERR;
9902 }
9903
9904 if (inet_aton(subnet, &((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr) == 0) {
9905 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developerc14d83a2023-06-29 20:09:42 +08009906 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009907 return RETURN_ERR;
9908 }
9909 if (ioctl(sock, SIOCSIFNETMASK, &ifr) < -1) {
9910 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNETMASK) failed, %s", strerror(errno));
developerc14d83a2023-06-29 20:09:42 +08009911 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009912 return RETURN_ERR;
9913 }
9914
9915 close(sock);
developera3511852023-06-14 14:12:59 +08009916 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009917}
9918
9919// reset the vlan configuration for this ap
9920INT wifi_resetApVlanCfg(INT apIndex)
9921{
developera1255e42023-05-13 17:45:02 +08009922 char interface_name[16] = {0};
developer2202b332023-05-24 16:23:22 +08009923 int if_idx, ret = 0;
9924 struct nl_msg *msg = NULL;
9925 struct nlattr * msg_data = NULL;
9926 struct mtk_nl80211_param param;
9927 struct unl unl_ins;
9928 struct vlan_policy_param vlan_param;
developer72fb0bb2023-01-11 09:46:29 +08009929
developer2202b332023-05-24 16:23:22 +08009930 if (apIndex > MAX_APS) {
9931 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9932 return RETURN_ERR;
9933 }
developer72fb0bb2023-01-11 09:46:29 +08009934
developer2202b332023-05-24 16:23:22 +08009935 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9936 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9937 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009938
developer2202b332023-05-24 16:23:22 +08009939 /*step 1. mwctl dev %s set vlan_tag 0*/
9940 if_idx = if_nametoindex(interface_name);
9941 /*init mtk nl80211 vendor cmd*/
9942 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9943 param.if_type = NL80211_ATTR_IFINDEX;
9944 param.if_idx = if_idx;
9945 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
developer72fb0bb2023-01-11 09:46:29 +08009946
developer2202b332023-05-24 16:23:22 +08009947 if (ret) {
9948 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9949 return RETURN_ERR;
9950 }
developer72fb0bb2023-01-11 09:46:29 +08009951
developer2202b332023-05-24 16:23:22 +08009952 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_TAG_INFO, 0)) {
9953 printf("Nla put attribute error\n");
9954 nlmsg_free(msg);
9955 goto err;
9956 }
developer72fb0bb2023-01-11 09:46:29 +08009957
developer2202b332023-05-24 16:23:22 +08009958 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9959 if (ret) {
9960 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9961 goto err;
9962 }
9963 mtk_nl80211_deint(&unl_ins);
9964 wifi_debug(DEBUG_NOTICE, "set vlan_tag 0 cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08009965
developer2202b332023-05-24 16:23:22 +08009966 /*step 2. mwctl dev %s set vlan_priority 0*/
9967 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9968 if (ret) {
9969 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9970 return RETURN_ERR;
9971 }
developer72fb0bb2023-01-11 09:46:29 +08009972
developer2202b332023-05-24 16:23:22 +08009973 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_PRIORITY_INFO, 0)) {
9974 printf("Nla put attribute error\n");
9975 nlmsg_free(msg);
9976 goto err;
9977 }
developer72fb0bb2023-01-11 09:46:29 +08009978
developer2202b332023-05-24 16:23:22 +08009979 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9980 if (ret) {
9981 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9982 goto err;
9983 }
9984 mtk_nl80211_deint(&unl_ins);
9985 wifi_debug(DEBUG_NOTICE, "set vlan_priority 0 cmd success.\n");
9986
9987 /*step 3. mwctl dev %s set vlan_id 0*/
9988 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9989 if (ret) {
9990 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developera1255e42023-05-13 17:45:02 +08009991 return RETURN_ERR;
developer2202b332023-05-24 16:23:22 +08009992 }
developer72fb0bb2023-01-11 09:46:29 +08009993
developer2202b332023-05-24 16:23:22 +08009994 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, 0)) {
9995 printf("Nla put attribute error\n");
9996 nlmsg_free(msg);
9997 goto err;
9998 }
9999
10000 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10001 if (ret) {
10002 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10003 goto err;
10004 }
10005 mtk_nl80211_deint(&unl_ins);
10006 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
10007
10008 /*step 4. mwctl dev %s set vlan_en 0*/
10009 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10010 if (ret) {
10011 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10012 return RETURN_ERR;
10013 }
10014
10015 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_EN_INFO, 0)) {
10016 printf("Nla put attribute error\n");
10017 nlmsg_free(msg);
10018 goto err;
10019 }
10020
10021 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10022 if (ret) {
10023 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10024 goto err;
10025 }
10026 mtk_nl80211_deint(&unl_ins);
10027 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
10028
10029 /*step 5. mwctl dev %s set vlan_policy 0:4*/
10030 vlan_param.direction = 0;
10031 vlan_param.policy = 4;
10032 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10033 if (ret) {
10034 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10035 return RETURN_ERR;
10036 }
10037 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
10038 printf("Nla put attribute error\n");
10039 nlmsg_free(msg);
10040 goto err;
10041 }
10042
10043 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10044 if (ret) {
10045 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10046 goto err;
10047 }
10048 mtk_nl80211_deint(&unl_ins);
10049 wifi_debug(DEBUG_NOTICE, "set vlan_policy 0:4 cmd success.\n");
10050
10051 /*step 6. mwctl dev %s set vlan_policy 1:0*/
10052 vlan_param.direction = 1;
10053 vlan_param.policy = 0;
10054 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10055 if (ret) {
10056 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10057 return RETURN_ERR;
10058 }
10059
10060 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
10061 printf("Nla put attribute error\n");
10062 nlmsg_free(msg);
10063 goto err;
10064 }
10065
10066 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10067 if (ret) {
10068 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10069 goto err;
10070 }
10071 /*deinit mtk nl80211 vendor msg*/
10072 mtk_nl80211_deint(&unl_ins);
10073 wifi_debug(DEBUG_NOTICE, "set vlan_policy 1:0 cmd success.\n");
10074
10075 /*TODO need to modify VLAN config in dat file*/
10076 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10077
10078 return RETURN_OK;
10079err:
10080 mtk_nl80211_deint(&unl_ins);
10081 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10082 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010083}
10084
10085// creates configuration variables needed for WPA/WPS. These variables are implementation dependent and in some implementations these variables are used by hostapd when it is started. Specific variables that are needed are dependent on the hostapd implementation. These variables are set by WPA/WPS security functions in this wifi HAL. If not needed for a particular implementation this function may simply return no error.
10086INT wifi_createHostApdConfig(INT apIndex, BOOL createWpsCfg)
10087{
developera3511852023-06-14 14:12:59 +080010088 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010089}
10090
10091// starts hostapd, uses the variables in the hostapd config with format compatible with the specific hostapd implementation
10092INT wifi_startHostApd()
10093{
developera3511852023-06-14 14:12:59 +080010094 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +080010095 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +080010096 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10097 return RETURN_OK;
10098 //sprintf(cmd, "hostapd -B `cat /tmp/conf_filename` -e /nvram/etc/wpa2/entropy -P /tmp/hostapd.pid 1>&2");
developer72fb0bb2023-01-11 09:46:29 +080010099}
10100
10101// stops hostapd
developer69b61b02023-03-07 17:17:44 +080010102INT wifi_stopHostApd()
developer72fb0bb2023-01-11 09:46:29 +080010103{
developera3511852023-06-14 14:12:59 +080010104 char cmd[128] = {0};
10105 char buf[128] = {0};
developer75bd10c2023-06-27 11:34:08 +080010106 int res;
developer72fb0bb2023-01-11 09:46:29 +080010107
developer75bd10c2023-06-27 11:34:08 +080010108 res = snprintf(cmd, sizeof(cmd), "systemctl stop hostapd");
10109 if (os_snprintf_error(sizeof(cmd), res)) {
10110 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10111 return RETURN_ERR;
10112 }
developera3511852023-06-14 14:12:59 +080010113 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010114
developera3511852023-06-14 14:12:59 +080010115 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010116}
10117
10118// restart hostapd dummy function
10119INT wifi_restartHostApd()
10120{
developera3511852023-06-14 14:12:59 +080010121 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +080010122 v_secure_system("systemctl restart hostapd-global");
developera3511852023-06-14 14:12:59 +080010123 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010124
developera3511852023-06-14 14:12:59 +080010125 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010126}
10127
10128// sets the AP enable status variable for the specified ap.
10129INT wifi_setApEnable(INT apIndex, BOOL enable)
10130{
developer7e4a2a62023-04-06 19:56:03 +080010131 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080010132 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +080010133 char cmd[MAX_CMD_SIZE] = {0};
10134 char buf[MAX_BUF_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +080010135 BOOL status = FALSE;
developer7e4a2a62023-04-06 19:56:03 +080010136 int max_radio_num = 0;
10137 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010138 int res;
developer72fb0bb2023-01-11 09:46:29 +080010139
developer7e4a2a62023-04-06 19:56:03 +080010140 wifi_getApEnable(apIndex, &status);
developer72fb0bb2023-01-11 09:46:29 +080010141
developer7e4a2a62023-04-06 19:56:03 +080010142 wifi_getMaxRadioNumber(&max_radio_num);
10143 if (enable == status)
10144 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010145
developer7e4a2a62023-04-06 19:56:03 +080010146 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10147 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010148
developer7e4a2a62023-04-06 19:56:03 +080010149 if (enable == TRUE) {
10150 int radioIndex = apIndex % max_radio_num;
10151 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080010152 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s up", interface_name);
10153 if (os_snprintf_error(sizeof(cmd), res)) {
10154 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10155 return RETURN_ERR;
10156 }
developerf3c7d292023-05-29 17:57:16 +080010157 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +080010158
developere40952c2023-06-15 18:46:43 +080010159 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
10160 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
10161 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10162 return RETURN_ERR;
10163 }
10164 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
10165 if (os_snprintf_error(sizeof(cmd), res)) {
10166 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10167 return RETURN_ERR;
10168 }
developer7e4a2a62023-04-06 19:56:03 +080010169 _syscmd(cmd, buf, sizeof(buf));
10170 } else {
developere40952c2023-06-15 18:46:43 +080010171 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
10172 if (os_snprintf_error(sizeof(cmd), res)) {
10173 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10174 return RETURN_ERR;
10175 }
developer7e4a2a62023-04-06 19:56:03 +080010176 _syscmd(cmd, buf, sizeof(buf));
developere40952c2023-06-15 18:46:43 +080010177 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s down", interface_name);
10178 if (os_snprintf_error(sizeof(cmd), res)) {
10179 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10180 return RETURN_ERR;
10181 }
developerf3c7d292023-05-29 17:57:16 +080010182 _syscmd(cmd, buf, sizeof(buf));
developer7e4a2a62023-04-06 19:56:03 +080010183 }
developere40952c2023-06-15 18:46:43 +080010184 res = snprintf(cmd, MAX_CMD_SIZE, "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer7e4a2a62023-04-06 19:56:03 +080010185 interface_name, interface_name, enable, VAP_STATUS_FILE);
developere40952c2023-06-15 18:46:43 +080010186 if (os_snprintf_error(sizeof(cmd), res)) {
10187 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10188 return RETURN_ERR;
10189 }
developer7e4a2a62023-04-06 19:56:03 +080010190 _syscmd(cmd, buf, sizeof(buf));
10191 //Wait for wifi up/down to apply
10192 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010193}
10194
10195// Outputs the setting of the internal variable that is set by wifi_setApEnable().
10196INT wifi_getApEnable(INT apIndex, BOOL *output_bool)
10197{
developer7e4a2a62023-04-06 19:56:03 +080010198 char interface_name[IF_NAME_SIZE] = {0};
10199 char cmd[MAX_CMD_SIZE] = {0};
developerc1aa6532023-06-09 09:37:01 +080010200 char buf[MAX_BUF_SIZE] = {0};
developerc338aba2023-08-09 13:56:42 +080010201 int res, len;
10202 char ctrl_interface[64] = {0};
10203 char config_file[128] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010204
developer7e4a2a62023-04-06 19:56:03 +080010205 if ((!output_bool) || (apIndex < 0) || (apIndex >= MAX_APS))
10206 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010207
developer7e4a2a62023-04-06 19:56:03 +080010208 *output_bool = 0;
developer72fb0bb2023-01-11 09:46:29 +080010209
developer7e4a2a62023-04-06 19:56:03 +080010210 if ((apIndex >= 0) && (apIndex < MAX_APS)) {
10211 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
10212 *output_bool = FALSE;
10213 return RETURN_OK;
10214 }
developerc338aba2023-08-09 13:56:42 +080010215 if (strlen(interface_name) == 0)
10216 return RETURN_ERR;
10217
10218 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10219 if (os_snprintf_error(sizeof(config_file), res)) {
10220 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10221 return RETURN_ERR;
10222 }
10223 if (wifi_hostapdRead(config_file, "ctrl_interface", ctrl_interface, sizeof(ctrl_interface))) {
10224 wifi_debug(DEBUG_ERROR, "ctrl_interface for %s not exist\n", interface_name);
10225 }
10226
10227 _syscmd_secure(buf, sizeof(buf), "ls %s | grep %s",
10228 strlen(ctrl_interface) == 0 ? "/var/run/hostapd/" : ctrl_interface, interface_name);
10229 len = strlen(buf) >= strlen(interface_name) ? strlen(interface_name) : strlen(buf);
10230
10231 if (len == 0 || strncmp(buf, interface_name, len)) {
10232 return RETURN_OK;
10233 }
10234 memset(buf, 0, sizeof(buf));
developerc1aa6532023-06-09 09:37:01 +080010235
developer75bd10c2023-06-27 11:34:08 +080010236 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s status | grep state | cut -d '=' -f2", interface_name);
10237 if (os_snprintf_error(sizeof(cmd), res)) {
10238 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10239 return RETURN_ERR;
10240 }
developerc1aa6532023-06-09 09:37:01 +080010241 _syscmd(cmd, buf, sizeof(buf));
10242
10243 if(strncmp(buf, "ENABLED", 7) == 0 || strncmp(buf, "ACS", 3) == 0 ||
10244 strncmp(buf, "HT_SCAN", 7) == 0 || strncmp(buf, "DFS", 3) == 0) {
10245 *output_bool = TRUE;
10246 }
developer7e4a2a62023-04-06 19:56:03 +080010247 }
developer72fb0bb2023-01-11 09:46:29 +080010248
developer7e4a2a62023-04-06 19:56:03 +080010249 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010250}
10251
developer69b61b02023-03-07 17:17:44 +080010252// Outputs the AP "Enabled" "Disabled" status from driver
10253INT wifi_getApStatus(INT apIndex, CHAR *output_string)
developer72fb0bb2023-01-11 09:46:29 +080010254{
developer9ce44382023-06-28 11:09:37 +080010255 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +080010256 int res;
developer72fb0bb2023-01-11 09:46:29 +080010257
developer7e4a2a62023-04-06 19:56:03 +080010258 if (!output_string) {
10259 printf("%s: null pointer!", __func__);
10260 return RETURN_ERR;
10261 }
developer72fb0bb2023-01-11 09:46:29 +080010262
developer7e4a2a62023-04-06 19:56:03 +080010263 wifi_getApEnable(apIndex, &output_bool);
developer72fb0bb2023-01-11 09:46:29 +080010264
developer7e4a2a62023-04-06 19:56:03 +080010265 if(output_bool == 1)
developere40952c2023-06-15 18:46:43 +080010266 res = snprintf(output_string, 32, "Up");
developer7e4a2a62023-04-06 19:56:03 +080010267 else
developere40952c2023-06-15 18:46:43 +080010268 res = snprintf(output_string, 32, "Disable");
10269 if (os_snprintf_error(32, res)) {
10270 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10271 return RETURN_ERR;
10272 }
developer7e4a2a62023-04-06 19:56:03 +080010273
10274 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010275}
10276
10277//Indicates whether or not beacons include the SSID name.
10278// outputs a 1 if SSID on the AP is enabled, else outputs 0
10279INT wifi_getApSsidAdvertisementEnable(INT apIndex, BOOL *output)
10280{
developera3511852023-06-14 14:12:59 +080010281 //get the running status
10282 char config_file[MAX_BUF_SIZE] = {0};
10283 char buf[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080010284 int res;
developer72fb0bb2023-01-11 09:46:29 +080010285
developera3511852023-06-14 14:12:59 +080010286 if (!output)
10287 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010288
developer32f2a182023-06-27 19:50:41 +080010289 res = snprintf(config_file, sizeof(config_file),
10290 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010291 if (os_snprintf_error(sizeof(config_file), res)) {
10292 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10293 return RETURN_ERR;
10294 }
developera3511852023-06-14 14:12:59 +080010295 wifi_hostapdRead(config_file, "ignore_broadcast_ssid", buf, sizeof(buf));
10296 // default is enable
10297 if (strlen(buf) == 0 || strncmp("0", buf, 1) == 0)
10298 *output = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080010299
developera3511852023-06-14 14:12:59 +080010300 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010301}
10302
10303// sets an internal variable for ssid advertisement. Set to 1 to enable, set to 0 to disable
10304INT wifi_setApSsidAdvertisementEnable(INT apIndex, BOOL enable)
10305{
developera3511852023-06-14 14:12:59 +080010306 //store the config, apply instantly
10307 char config_file[MAX_BUF_SIZE] = {0};
10308 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010309 int res;
developer72fb0bb2023-01-11 09:46:29 +080010310
developera3511852023-06-14 14:12:59 +080010311 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10312 list.name = "ignore_broadcast_ssid";
10313 list.value = enable?"0":"1";
developer72fb0bb2023-01-11 09:46:29 +080010314
developer32f2a182023-06-27 19:50:41 +080010315 res = snprintf(config_file, sizeof(config_file),
10316 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010317 if (os_snprintf_error(sizeof(config_file), res)) {
10318 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10319 return RETURN_ERR;
10320 }
developera3511852023-06-14 14:12:59 +080010321 wifi_hostapdWrite(config_file, &list, 1);
10322 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10323 //TODO: call hostapd_cli for dynamic_config_control
10324 wifi_reloadAp(apIndex);
10325 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010326
developera3511852023-06-14 14:12:59 +080010327 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010328}
10329
10330//The maximum number of retransmission for a packet. This corresponds to IEEE 802.11 parameter dot11ShortRetryLimit.
10331INT wifi_getApRetryLimit(INT apIndex, UINT *output_uint)
10332{
developer47cc27a2023-05-17 23:09:58 +080010333 /* get the running status */
10334 if(!output_uint)
developera3511852023-06-14 14:12:59 +080010335 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +080010336
10337 *output_uint = 15;
10338 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010339}
10340
developer47cc27a2023-05-17 23:09:58 +080010341/*Do not support AP retry limit fix*/
developer72fb0bb2023-01-11 09:46:29 +080010342INT wifi_setApRetryLimit(INT apIndex, UINT number)
10343{
developer47cc27a2023-05-17 23:09:58 +080010344 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010345}
10346
developer95c045d2023-05-24 19:26:28 +080010347int get_wmm_cap_status_callback(struct nl_msg *msg, void *data)
10348{
10349 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10350 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_WMM_ATTR_MAX + 1];
10351 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developer2f79c922023-06-02 17:33:42 +080010352 unsigned char *status = (unsigned char *)data;
developer95c045d2023-05-24 19:26:28 +080010353 int err = 0;
developer95c045d2023-05-24 19:26:28 +080010354
10355 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10356 genlmsg_attrlen(gnlh, 0), NULL);
10357 if (err < 0)
10358 return err;
10359
10360 if (tb[NL80211_ATTR_VENDOR_DATA]) {
10361 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_WMM_ATTR_MAX,
10362 tb[NL80211_ATTR_VENDOR_DATA], NULL);
10363 if (err < 0)
10364 return err;
10365
10366 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]) {
developer95c045d2023-05-24 19:26:28 +080010367 *status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]);
10368 }
10369 }
10370
10371 return 0;
10372}
10373
developer72fb0bb2023-01-11 09:46:29 +080010374//Indicates whether this access point supports WiFi Multimedia (WMM) Access Categories (AC).
10375INT wifi_getApWMMCapability(INT apIndex, BOOL *output)
10376{
developer95c045d2023-05-24 19:26:28 +080010377 int if_idx, ret = 0;
developera3511852023-06-14 14:12:59 +080010378 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010379 unsigned char status = 0;
10380 struct nl_msg *msg = NULL;
10381 struct nlattr * msg_data = NULL;
10382 struct mtk_nl80211_param param;
10383 struct unl unl_ins;
developer8e6583c2023-05-23 13:36:06 +080010384
developera3511852023-06-14 14:12:59 +080010385 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10386 if(!output)
developerdaf24792023-06-06 11:40:04 +080010387 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010388
developer95c045d2023-05-24 19:26:28 +080010389 if (apIndex > MAX_APS) {
10390 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10391 return RETURN_ERR;
10392 }
10393
developera3511852023-06-14 14:12:59 +080010394 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developerdaf24792023-06-06 11:40:04 +080010395 return RETURN_ERR;
developer95c045d2023-05-24 19:26:28 +080010396
10397 if_idx = if_nametoindex(interface_name);
10398 /*init mtk nl80211 vendor cmd*/
10399 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10400 param.if_type = NL80211_ATTR_IFINDEX;
10401 param.if_idx = if_idx;
10402 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10403
10404 if (ret) {
10405 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10406 return RETURN_ERR;
10407 }
10408
10409 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, 0xf)) {
10410 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10411 nlmsg_free(msg);
10412 goto err;
10413 }
10414
10415 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_wmm_cap_status_callback,
10416 (void *)&status);
10417 if (ret) {
10418 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10419 goto err;
10420 }
10421 mtk_nl80211_deint(&unl_ins);
10422
10423 *output = status == 0 ? FALSE : TRUE;
10424 wifi_debug(DEBUG_NOTICE, "wmm cap (%u).\n", (unsigned int)(*output));
developer8e6583c2023-05-23 13:36:06 +080010425
developera3511852023-06-14 14:12:59 +080010426 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10427 return RETURN_OK;
10428err:
developer95c045d2023-05-24 19:26:28 +080010429 mtk_nl80211_deint(&unl_ins);
10430 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10431 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010432}
10433
10434//Indicates whether this access point supports WMM Unscheduled Automatic Power Save Delivery (U-APSD). Note: U-APSD support implies WMM support.
10435INT wifi_getApUAPSDCapability(INT apIndex, BOOL *output)
10436{
developera3511852023-06-14 14:12:59 +080010437 //get the running status from driver
10438 char cmd[128] = {0};
10439 char buf[128] = {0};
10440 int max_radio_num = 0, radioIndex = 0;
10441 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010442 int res;
developer72fb0bb2023-01-11 09:46:29 +080010443
developera3511852023-06-14 14:12:59 +080010444 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010445
developera3511852023-06-14 14:12:59 +080010446 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080010447 if(max_radio_num == 0){
10448 return RETURN_ERR;
10449 }
developera3511852023-06-14 14:12:59 +080010450 radioIndex = apIndex % max_radio_num;
10451 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080010452 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep u-APSD", phyId);
10453 if (os_snprintf_error(sizeof(cmd), res)) {
10454 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10455 return RETURN_ERR;
10456 }
developera3511852023-06-14 14:12:59 +080010457 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010458
developera3511852023-06-14 14:12:59 +080010459 if (strlen(buf) > 0)
10460 *output = true;
developer72fb0bb2023-01-11 09:46:29 +080010461
developera3511852023-06-14 14:12:59 +080010462 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010463
developera3511852023-06-14 14:12:59 +080010464 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010465}
10466
10467//Whether WMM support is currently enabled. When enabled, this is indicated in beacon frames.
10468INT wifi_getApWmmEnable(INT apIndex, BOOL *output)
10469{
developera3511852023-06-14 14:12:59 +080010470 return wifi_getApWMMCapability(apIndex, output);
developer72fb0bb2023-01-11 09:46:29 +080010471}
10472
10473// enables/disables WMM on the hardwawre for this AP. enable==1, disable == 0
10474INT wifi_setApWmmEnable(INT apIndex, BOOL enable)
10475{
developer95c045d2023-05-24 19:26:28 +080010476 int if_idx, ret = 0;
10477 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010478 struct nl_msg *msg = NULL;
10479 struct nlattr * msg_data = NULL;
10480 struct mtk_nl80211_param param;
10481 struct unl unl_ins;
developer72fb0bb2023-01-11 09:46:29 +080010482
developer95c045d2023-05-24 19:26:28 +080010483 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010484
developer95c045d2023-05-24 19:26:28 +080010485 if (apIndex > MAX_APS) {
10486 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10487 return RETURN_ERR;
10488 }
developer72fb0bb2023-01-11 09:46:29 +080010489
developer95c045d2023-05-24 19:26:28 +080010490 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10491 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010492
developer95c045d2023-05-24 19:26:28 +080010493 if_idx = if_nametoindex(interface_name);
10494 /*init mtk nl80211 vendor cmd*/
10495 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10496 param.if_type = NL80211_ATTR_IFINDEX;
10497 param.if_idx = if_idx;
10498 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10499
10500 if (ret) {
10501 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10502 return RETURN_ERR;
10503 }
10504
10505 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, enable ? 1 : 0)) {
10506 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10507 nlmsg_free(msg);
10508 goto err;
10509 }
10510
10511 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10512 if (ret) {
10513 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10514 goto err;
10515 }
10516 mtk_nl80211_deint(&unl_ins);
10517
10518 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10519 return RETURN_OK;
10520err:
10521 mtk_nl80211_deint(&unl_ins);
10522 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10523 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010524}
10525
developer95c045d2023-05-24 19:26:28 +080010526
developer72fb0bb2023-01-11 09:46:29 +080010527//Whether U-APSD support is currently enabled. When enabled, this is indicated in beacon frames. Note: U-APSD can only be enabled if WMM is also enabled.
10528INT wifi_getApWmmUapsdEnable(INT apIndex, BOOL *output)
10529{
developer75bd10c2023-06-27 11:34:08 +080010530 int res;
10531
developera3511852023-06-14 14:12:59 +080010532 //get the running status from driver
10533 if(!output)
10534 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010535
developera3511852023-06-14 14:12:59 +080010536 char config_file[128] = {0};
10537 char buf[16] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010538
developer75bd10c2023-06-27 11:34:08 +080010539 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10540 if (os_snprintf_error(sizeof(config_file), res)) {
10541 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10542 return RETURN_ERR;
10543 }
developera3511852023-06-14 14:12:59 +080010544 wifi_hostapdRead(config_file, "uapsd_advertisement_enabled", buf, sizeof(buf));
10545 if (strlen(buf) == 0 || strncmp("1", buf, 1) == 0)
10546 *output = TRUE;
10547 else
10548 *output = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080010549
developera3511852023-06-14 14:12:59 +080010550 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010551}
10552
10553// enables/disables Automatic Power Save Delivery on the hardwarwe for this AP
10554INT wifi_setApWmmUapsdEnable(INT apIndex, BOOL enable)
10555{
developera3511852023-06-14 14:12:59 +080010556 //save config and apply instantly.
10557 char config_file[MAX_BUF_SIZE] = {0};
10558 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010559 int res;
developer72fb0bb2023-01-11 09:46:29 +080010560
developera3511852023-06-14 14:12:59 +080010561 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10562 list.name = "uapsd_advertisement_enabled";
10563 list.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080010564
developer75bd10c2023-06-27 11:34:08 +080010565 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10566 if (os_snprintf_error(sizeof(config_file), res)) {
10567 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10568 return RETURN_ERR;
10569 }
developera3511852023-06-14 14:12:59 +080010570 wifi_hostapdWrite(config_file, &list, 1);
10571 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10572 wifi_quick_reload_ap(apIndex);
10573 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010574
developera3511852023-06-14 14:12:59 +080010575 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010576}
10577
10578// Sets the WMM ACK policy on the hardware. AckPolicy false means do not acknowledge, true means acknowledge
10579INT wifi_setApWmmOgAckPolicy(INT apIndex, INT class, BOOL ackPolicy) //RDKB
10580{
developera3511852023-06-14 14:12:59 +080010581 char interface_name[16] = {0};
10582 // assume class 0->BE, 1->BK, 2->VI, 3->VO
10583 char cmd[MAX_CMD_SIZE] = {0};
10584 char buf[128] = {0};
10585 char ack_filepath[128] = {0};
10586 uint16_t bitmap = 0;
10587 uint16_t class_map[4] = {0x0009, 0x0006, 0x0030, 0x00C0};
10588 FILE *f = NULL;
developere40952c2023-06-15 18:46:43 +080010589 int res;
developerc14d83a2023-06-29 20:09:42 +080010590 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080010591
developera3511852023-06-14 14:12:59 +080010592 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010593
developera3511852023-06-14 14:12:59 +080010594 // Get current setting
developere40952c2023-06-15 18:46:43 +080010595 res = snprintf(ack_filepath, sizeof(ack_filepath), "%s%d.txt", NOACK_MAP_FILE, apIndex);
10596 if (os_snprintf_error(sizeof(ack_filepath), res)) {
10597 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10598 return RETURN_ERR;
10599 }
10600 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", ack_filepath);
10601 if (os_snprintf_error(sizeof(cmd), res)) {
10602 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10603 return RETURN_ERR;
10604 }
developera3511852023-06-14 14:12:59 +080010605 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +080010606 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080010607 if (hal_strtoul(buf, 10, &tmp) < 0) {
10608 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080010609 }
developerc14d83a2023-06-29 20:09:42 +080010610 bitmap = tmp;
developerd14dff12023-06-28 22:47:44 +080010611 }
developer72fb0bb2023-01-11 09:46:29 +080010612
developera3511852023-06-14 14:12:59 +080010613 if (ackPolicy == TRUE) { // True, unset this class
10614 bitmap &= ~class_map[class];
10615 } else { // False, set this class
10616 bitmap |= class_map[class];
10617 }
developer72fb0bb2023-01-11 09:46:29 +080010618
developera3511852023-06-14 14:12:59 +080010619 f = fopen(ack_filepath, "w");
10620 if (f == NULL) {
developer37646972023-06-29 10:58:43 +080010621 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
10622 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +080010623 return RETURN_ERR;
10624 }
developer37646972023-06-29 10:58:43 +080010625 if (fprintf(f, "%hu", bitmap) < 0)
10626 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
10627
10628 if (fclose(f) == EOF) {
10629 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
10630 return RETURN_ERR;
10631 }
developer72fb0bb2023-01-11 09:46:29 +080010632
developera3511852023-06-14 14:12:59 +080010633 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10634 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010635 res = snprintf(cmd, sizeof(cmd), "iw dev %s set noack_map 0x%04x\n", interface_name, bitmap);
10636 if (os_snprintf_error(sizeof(cmd), res)) {
10637 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10638 return RETURN_ERR;
10639 }
developera3511852023-06-14 14:12:59 +080010640 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010641
developera3511852023-06-14 14:12:59 +080010642 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
10643 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010644}
10645
10646//The maximum number of devices that can simultaneously be connected to the access point. A value of 0 means that there is no specific limit.
10647INT wifi_getApMaxAssociatedDevices(INT apIndex, UINT *output_uint)
10648{
developer75bd10c2023-06-27 11:34:08 +080010649 int res;
10650
developera3511852023-06-14 14:12:59 +080010651 //get the running status from driver
10652 if(!output_uint)
10653 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010654
developera3511852023-06-14 14:12:59 +080010655 char output[16]={'\0'};
10656 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010657
developer75bd10c2023-06-27 11:34:08 +080010658 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10659 if (os_snprintf_error(sizeof(config_file), res)) {
10660 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10661 return RETURN_ERR;
10662 }
developera3511852023-06-14 14:12:59 +080010663 wifi_hostapdRead(config_file, "max_num_sta", output, sizeof(output));
10664 if (strlen(output) == 0) *output_uint = MAX_ASSOCIATED_STA_NUM;
10665 else {
10666 int device_num = atoi(output);
10667 if (device_num > MAX_ASSOCIATED_STA_NUM || device_num < 0) {
10668 wifi_dbg_printf("\n[%s]: get max_num_sta error: %d", __func__, device_num);
10669 return RETURN_ERR;
10670 }
10671 else {
10672 *output_uint = device_num;
10673 }
10674 }
developer72fb0bb2023-01-11 09:46:29 +080010675
developera3511852023-06-14 14:12:59 +080010676 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010677}
10678
10679INT wifi_setApMaxAssociatedDevices(INT apIndex, UINT number)
10680{
developera3511852023-06-14 14:12:59 +080010681 //store to wifi config, apply instantly
10682 char str[MAX_BUF_SIZE]={'\0'};
10683 struct params params;
10684 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080010685 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080010686
developera3511852023-06-14 14:12:59 +080010687 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10688 if (number > MAX_ASSOCIATED_STA_NUM) {
10689 WIFI_ENTRY_EXIT_DEBUG("%s: Invalid input\n",__func__);
10690 return RETURN_ERR;
10691 }
developer75bd10c2023-06-27 11:34:08 +080010692 res = snprintf(str, sizeof(str), "%d", number);
10693 if (os_snprintf_error(sizeof(str), res)) {
10694 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10695 return RETURN_ERR;
10696 }
developera3511852023-06-14 14:12:59 +080010697 params.name = "max_num_sta";
10698 params.value = str;
developer72fb0bb2023-01-11 09:46:29 +080010699
developer32f2a182023-06-27 19:50:41 +080010700 res = snprintf(config_file,
10701 sizeof(config_file), "%s%d.conf",CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010702 if (os_snprintf_error(sizeof(config_file), res)) {
10703 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10704 return RETURN_ERR;
10705 }
developer32f2a182023-06-27 19:50:41 +080010706 ret = wifi_hostapdWrite(config_file, &params, 1);
developera3511852023-06-14 14:12:59 +080010707 if (ret) {
10708 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n"
10709 ,__func__, ret);
10710 }
developer72fb0bb2023-01-11 09:46:29 +080010711
developera3511852023-06-14 14:12:59 +080010712 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10713 if (ret) {
10714 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n"
10715 ,__func__, ret);
10716 }
10717 wifi_reloadAp(apIndex);
10718 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010719
developera3511852023-06-14 14:12:59 +080010720 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010721}
10722
10723//The HighWatermarkThreshold value that is lesser than or equal to MaxAssociatedDevices. Setting this parameter does not actually limit the number of clients that can associate with this access point as that is controlled by MaxAssociatedDevices. MaxAssociatedDevices or 50. The default value of this parameter should be equal to MaxAssociatedDevices. In case MaxAssociatedDevices is 0 (zero), the default value of this parameter should be 50. A value of 0 means that there is no specific limit and Watermark calculation algorithm should be turned off.
10724INT wifi_getApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT *output_uint)
10725{
developera3511852023-06-14 14:12:59 +080010726 //get the current threshold
10727 if(!output_uint)
10728 return RETURN_ERR;
10729 wifi_getApMaxAssociatedDevices(apIndex, output_uint);
10730 if (*output_uint == 0)
10731 *output_uint = 50;
10732 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010733}
10734
10735INT wifi_setApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT Threshold)
10736{
developera3511852023-06-14 14:12:59 +080010737 //store the config, reset threshold, reset AssociatedDevicesHighWatermarkThresholdReached, reset AssociatedDevicesHighWatermarkDate to current time
10738 if (!wifi_setApMaxAssociatedDevices(apIndex, Threshold))
10739 return RETURN_OK;
10740 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010741}
10742
10743//Number of times the current total number of associated device has reached the HighWatermarkThreshold value. This calculation can be based on the parameter AssociatedDeviceNumberOfEntries as well. Implementation specifics about this parameter are left to the product group and the device vendors. It can be updated whenever there is a new client association request to the access point.
10744INT wifi_getApAssociatedDevicesHighWatermarkThresholdReached(INT apIndex, UINT *output_uint)
10745{
developera3511852023-06-14 14:12:59 +080010746 if(!output_uint)
10747 return RETURN_ERR;
10748 *output_uint = 3;
10749 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010750}
10751
10752//Maximum number of associated devices that have ever associated with the access point concurrently since the last reset of the device or WiFi module.
10753INT wifi_getApAssociatedDevicesHighWatermark(INT apIndex, UINT *output_uint)
10754{
developera3511852023-06-14 14:12:59 +080010755 if(!output_uint)
10756 return RETURN_ERR;
10757 *output_uint = 3;
10758 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010759}
10760
10761//Date and Time at which the maximum number of associated devices ever associated with the access point concurrenlty since the last reset of the device or WiFi module (or in short when was X_COMCAST-COM_AssociatedDevicesHighWatermark updated). This dateTime value is in UTC.
10762INT wifi_getApAssociatedDevicesHighWatermarkDate(INT apIndex, ULONG *output_in_seconds)
10763{
developera3511852023-06-14 14:12:59 +080010764 if(!output_in_seconds)
10765 return RETURN_ERR;
10766 *output_in_seconds = 0;
10767 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010768}
10769
10770//Comma-separated list of strings. Indicates which security modes this AccessPoint instance is capable of supporting. Each list item is an enumeration of: None,WEP-64,WEP-128,WPA-Personal,WPA2-Personal,WPA-WPA2-Personal,WPA-Enterprise,WPA2-Enterprise,WPA-WPA2-Enterprise
10771INT wifi_getApSecurityModesSupported(INT apIndex, CHAR *output)
10772{
developere40952c2023-06-15 18:46:43 +080010773 int res;
10774
developera3511852023-06-14 14:12:59 +080010775 if(!output || apIndex>=MAX_APS)
10776 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010777 //res = snprintf(output, 128, "None,WPA-Personal,WPA2-Personal,WPA-WPA2-Personal,WPA-Enterprise,WPA2-Enterprise,WPA-WPA2-Enterprise");
10778 res = snprintf(output, 128, "None,WPA2-Personal,WPA-WPA2-Personal,WPA2-Enterprise,WPA-WPA2-Enterprise,WPA3-Personal,WPA3-Enterprise");
10779 if (os_snprintf_error(128, res)) {
10780 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10781 return RETURN_ERR;
10782 }
developera3511852023-06-14 14:12:59 +080010783 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010784}
developer72fb0bb2023-01-11 09:46:29 +080010785
10786//The value MUST be a member of the list reported by the ModesSupported parameter. Indicates which security mode is enabled.
10787INT wifi_getApSecurityModeEnabled(INT apIndex, CHAR *output)
10788{
developera3511852023-06-14 14:12:59 +080010789 char config_file[128] = {0};
10790 char wpa[16] = {0};
10791 char key_mgmt[64] = {0};
developer9ce44382023-06-28 11:09:37 +080010792 int res = -1;
developere40952c2023-06-15 18:46:43 +080010793
developera3511852023-06-14 14:12:59 +080010794 if (!output)
10795 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010796
developer75bd10c2023-06-27 11:34:08 +080010797 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10798 if (os_snprintf_error(sizeof(config_file), res)) {
10799 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10800 return RETURN_ERR;
10801 }
developera3511852023-06-14 14:12:59 +080010802 wifi_hostapdRead(config_file, "wpa", wpa, sizeof(wpa));
developer72fb0bb2023-01-11 09:46:29 +080010803
developer32f2a182023-06-27 19:50:41 +080010804 memcpy(output, "None", 4);//Copying "None" to output string for default case
10805 output[4] = '\0';
developera3511852023-06-14 14:12:59 +080010806 wifi_hostapdRead(config_file, "wpa_key_mgmt", key_mgmt, sizeof(key_mgmt));
10807 if (strstr(key_mgmt, "WPA-PSK") && strstr(key_mgmt, "SAE") == NULL) {
10808 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010809 res = snprintf(output, 32, "WPA-Personal");
developera3511852023-06-14 14:12:59 +080010810 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010811 res = snprintf(output, 32, "WPA2-Personal");
developera3511852023-06-14 14:12:59 +080010812 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010813 res = snprintf(output, 32, "WPA-WPA2-Personal");
developer72fb0bb2023-01-11 09:46:29 +080010814
developera3511852023-06-14 14:12:59 +080010815 } else if (strstr(key_mgmt, "WPA-EAP-SUITE-B-192")) {
developere40952c2023-06-15 18:46:43 +080010816 res = snprintf(output, 32, "WPA3-Enterprise");
developera3511852023-06-14 14:12:59 +080010817 } else if (strstr(key_mgmt, "WPA-EAP")) {
10818 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010819 res = snprintf(output, 32, "WPA-Enterprise");
developera3511852023-06-14 14:12:59 +080010820 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010821 res = snprintf(output, 32, "WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010822 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010823 res = snprintf(output, 32, "WPA-WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010824 } else if (strstr(key_mgmt, "SAE")) {
10825 if (strstr(key_mgmt, "WPA-PSK") == NULL)
developere40952c2023-06-15 18:46:43 +080010826 res = snprintf(output, 32, "WPA3-Personal");
developera3511852023-06-14 14:12:59 +080010827 else
developere40952c2023-06-15 18:46:43 +080010828 res = snprintf(output, 32, "WPA3-Personal-Transition");
10829 }
10830 if (os_snprintf_error(32, res)) {
10831 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10832 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080010833 }
developer72fb0bb2023-01-11 09:46:29 +080010834
developera3511852023-06-14 14:12:59 +080010835 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
10836 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010837}
developer69b61b02023-03-07 17:17:44 +080010838
developer72fb0bb2023-01-11 09:46:29 +080010839INT wifi_setApSecurityModeEnabled(INT apIndex, CHAR *encMode)
10840{
developer32f2a182023-06-27 19:50:41 +080010841 char securityType[32] = {0};
10842 char authMode[32] = {0};
10843 unsigned long len_sec, len_auth;
developera3511852023-06-14 14:12:59 +080010844 //store settings and wait for wifi up to apply
10845 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10846 if(!encMode)
10847 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010848
developera3511852023-06-14 14:12:59 +080010849 if (strcmp(encMode, "None")==0)
10850 {
developer32f2a182023-06-27 19:50:41 +080010851 len_sec = strlen("None");
10852 len_auth = strlen("None");
10853 memcpy(securityType, "None", len_sec);
10854 memcpy(authMode, "None", len_auth);
10855 } else if (strcmp(encMode, "WPA-WPA2-Personal")==0) {
10856 len_sec = strlen("WPAand11i");
10857 memcpy(securityType, "WPAand11i", len_sec);
10858 len_auth = strlen("PSKAuthentication");
10859 memcpy(authMode, "PSKAuthentication", len_auth);
10860 } else if (strcmp(encMode, "WPA-WPA2-Enterprise")==0) {
10861 len_sec = strlen("WPAand11i");
10862 memcpy(securityType, "WPAand11i", len_sec);
10863 len_auth = strlen("EAPAuthentication");
10864 memcpy(authMode, "EAPAuthentication", len_auth);
10865 } else if (strcmp(encMode, "WPA-Personal")==0) {
10866 len_sec = strlen("WPA");
10867 memcpy(securityType, "WPA", len_sec);
10868 len_auth = strlen("PSKAuthentication");
10869 memcpy(authMode, "PSKAuthentication", len_auth);
10870 } else if (strcmp(encMode, "WPA-Enterprise")==0) {
10871 len_sec = strlen("WPA");
10872 memcpy(securityType, "WPA", len_sec);
10873 len_auth = strlen("EAPAuthentication");
10874 memcpy(authMode, "EAPAuthentication", len_auth);
10875 } else if (strcmp(encMode, "WPA2-Personal")==0) {
10876 len_sec = strlen("11i");
10877 memcpy(securityType, "11i", len_sec);
10878 len_auth = strlen("PSKAuthentication");
10879 memcpy(authMode, "PSKAuthentication", len_auth);
10880 } else if (strcmp(encMode, "WPA2-Enterprise")==0) {
10881 len_sec = strlen("11i");
10882 memcpy(securityType, "11i", len_sec);
10883 len_auth = strlen("EAPAuthentication");
10884 memcpy(authMode, "EAPAuthentication", len_auth);
10885 } else if (strcmp(encMode, "WPA3-Personal") == 0) {
10886 len_sec = strlen("11i");
10887 memcpy(securityType, "11i", len_sec);
10888 len_auth = strlen("SAEAuthentication");
10889 memcpy(authMode, "SAEAuthentication", len_auth);
10890 } else if (strcmp(encMode, "WPA3-Personal-Transition") == 0) {
10891 len_sec = strlen("11i");
10892 memcpy(securityType, "11i", len_sec);
10893 len_auth = strlen("PSK-SAEAuthentication");
10894 memcpy(authMode, "PSK-SAEAuthentication", len_auth);
10895 } else if (strcmp(encMode, "WPA3-Enterprise") == 0) {
10896 len_sec = strlen("11i");
10897 memcpy(securityType, "11i", len_sec);
10898 len_auth = strlen("EAP_192-bit_Authentication");
10899 memcpy(authMode, "EAP_192-bit_Authentication", len_auth);
10900 } else if (strcmp(encMode, "OWE") == 0) {
10901 len_sec = strlen("11i");
10902 memcpy(securityType, "11i", len_sec);
10903 len_auth = strlen("Enhanced_Open");
10904 memcpy(authMode, "Enhanced_Open", len_auth);
10905 } else {
10906 len_sec = strlen("None");
10907 memcpy(securityType, "None", len_sec);
10908 len_auth = strlen("None");
10909 memcpy(authMode, "None", len_auth);
developera3511852023-06-14 14:12:59 +080010910 }
developer32f2a182023-06-27 19:50:41 +080010911 securityType[len_sec] = '\0';
10912 authMode[len_auth] = '\0';
developera3511852023-06-14 14:12:59 +080010913 wifi_setApBeaconType(apIndex, securityType);
10914 wifi_setApBasicAuthenticationMode(apIndex, authMode);
10915 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010916
developera3511852023-06-14 14:12:59 +080010917 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010918}
developer72fb0bb2023-01-11 09:46:29 +080010919
10920
10921//A literal PreSharedKey (PSK) expressed as a hexadecimal string.
10922// output_string must be pre-allocated as 64 character string by caller
10923// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10924INT wifi_getApSecurityPreSharedKey(INT apIndex, CHAR *output_string)
10925{
developera3511852023-06-14 14:12:59 +080010926 char buf[16] = {0};
10927 char config_file[MAX_BUF_SIZE] = {0};
10928 int res;
developer72fb0bb2023-01-11 09:46:29 +080010929
developera3511852023-06-14 14:12:59 +080010930 if(output_string==NULL)
10931 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010932
developera3511852023-06-14 14:12:59 +080010933 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10934 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010935 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10936 return RETURN_ERR;
10937 }
developera3511852023-06-14 14:12:59 +080010938 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010939
developera3511852023-06-14 14:12:59 +080010940 if(strcmp(buf,"0")==0)
10941 {
10942 printf("wpa_mode is %s ......... \n",buf);
10943 return RETURN_ERR;
10944 }
developer72fb0bb2023-01-11 09:46:29 +080010945
developera3511852023-06-14 14:12:59 +080010946 wifi_dbg_printf("\nFunc=%s\n",__func__);
10947 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10948 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010949 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10950 return RETURN_ERR;
10951 }
developere5750452023-05-15 16:46:42 +080010952 wifi_hostapdRead(config_file,"wpa_psk",output_string,65);
developera3511852023-06-14 14:12:59 +080010953 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010954
developera3511852023-06-14 14:12:59 +080010955 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010956}
10957
10958// sets an enviornment variable for the psk. Input string preSharedKey must be a maximum of 64 characters
10959// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10960INT wifi_setApSecurityPreSharedKey(INT apIndex, CHAR *preSharedKey)
10961{
developera3511852023-06-14 14:12:59 +080010962 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
10963 struct params params={'\0'};
developer32f2a182023-06-27 19:50:41 +080010964 int ret;
developera3511852023-06-14 14:12:59 +080010965 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010966
developera3511852023-06-14 14:12:59 +080010967 if(NULL == preSharedKey)
10968 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010969
developera3511852023-06-14 14:12:59 +080010970 params.name = "wpa_psk";
developer72fb0bb2023-01-11 09:46:29 +080010971
developera3511852023-06-14 14:12:59 +080010972 if(strlen(preSharedKey) != 64)
10973 {
10974 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 64 chars\n");
10975 return RETURN_ERR;
10976 }
10977 params.value = preSharedKey;
developer32f2a182023-06-27 19:50:41 +080010978 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10979 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080010980 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10981 return RETURN_ERR;
10982 }
developera3511852023-06-14 14:12:59 +080010983 ret = wifi_hostapdWrite(config_file, &params, 1);
10984 if(!ret) {
10985 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10986 wifi_reloadAp(apIndex);
10987 }
10988 return ret;
10989 //TODO: call hostapd_cli for dynamic_config_control
developer72fb0bb2023-01-11 09:46:29 +080010990}
10991
10992//A passphrase from which the PreSharedKey is to be generated, for WPA-Personal or WPA2-Personal or WPA-WPA2-Personal security modes.
10993// outputs the passphrase, maximum 63 characters
10994INT wifi_getApSecurityKeyPassphrase(INT apIndex, CHAR *output_string)
10995{
developera3511852023-06-14 14:12:59 +080010996 char config_file[MAX_BUF_SIZE] = {0}, buf[32] = {0};
developer75bd10c2023-06-27 11:34:08 +080010997 int res;
developer72fb0bb2023-01-11 09:46:29 +080010998
developera3511852023-06-14 14:12:59 +080010999 wifi_dbg_printf("\nFunc=%s\n",__func__);
11000 if (NULL == output_string)
11001 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011002
developer75bd10c2023-06-27 11:34:08 +080011003 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11004 if (os_snprintf_error(sizeof(config_file), res)) {
11005 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11006 return RETURN_ERR;
11007 }
11008
developera3511852023-06-14 14:12:59 +080011009 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
11010 if(strcmp(buf,"0")==0)
11011 {
11012 printf("wpa_mode is %s ......... \n",buf);
11013 return RETURN_ERR;
11014 }
developer72fb0bb2023-01-11 09:46:29 +080011015
developera3511852023-06-14 14:12:59 +080011016 wifi_hostapdRead(config_file,"wpa_passphrase",output_string,64);
11017 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080011018
developera3511852023-06-14 14:12:59 +080011019 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011020}
11021
11022// sets the passphrase enviornment variable, max 63 characters
11023INT wifi_setApSecurityKeyPassphrase(INT apIndex, CHAR *passPhrase)
11024{
developera3511852023-06-14 14:12:59 +080011025 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
11026 struct params params={'\0'};
11027 char config_file[MAX_BUF_SIZE] = {0};
11028 int ret;
developer72fb0bb2023-01-11 09:46:29 +080011029
developera3511852023-06-14 14:12:59 +080011030 if(NULL == passPhrase)
11031 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011032
developera3511852023-06-14 14:12:59 +080011033 if(strlen(passPhrase)<8 || strlen(passPhrase)>63)
11034 {
11035 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 8 to 63 chars\n");
11036 return RETURN_ERR;
11037 }
11038 params.name = "wpa_passphrase";
11039 params.value = passPhrase;
developer32f2a182023-06-27 19:50:41 +080011040 ret = snprintf(config_file, sizeof(config_file),
11041 "%s%d.conf", CONFIG_PREFIX, apIndex);
11042 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080011043 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11044 return RETURN_ERR;
11045 }
developera3511852023-06-14 14:12:59 +080011046 ret=wifi_hostapdWrite(config_file,&params,1);
11047 if(!ret) {
11048 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +080011049 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +080011050 }
developer72fb0bb2023-01-11 09:46:29 +080011051
developera3511852023-06-14 14:12:59 +080011052 return ret;
developer72fb0bb2023-01-11 09:46:29 +080011053}
11054
11055//When set to true, this AccessPoint instance's WiFi security settings are reset to their factory default values. The affected settings include ModeEnabled, WEPKey, PreSharedKey and KeyPassphrase.
11056INT wifi_setApSecurityReset(INT apIndex)
11057{
developera3511852023-06-14 14:12:59 +080011058 char original_config_file[64] = {0};
11059 char current_config_file[64] = {0};
11060 char buf[64] = {0};
11061 char cmd[64] = {0};
11062 char wpa[4] = {0};
11063 char wpa_psk[64] = {0};
11064 char wpa_passphrase[64] = {0};
11065 char wpa_psk_file[128] = {0};
11066 char wpa_key_mgmt[64] = {0};
11067 char wpa_pairwise[32] = {0};
11068 wifi_band band;
11069 struct params list[6];
developere40952c2023-06-15 18:46:43 +080011070 int res;
developer72fb0bb2023-01-11 09:46:29 +080011071
developera3511852023-06-14 14:12:59 +080011072 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011073
developera3511852023-06-14 14:12:59 +080011074 band = wifi_index_to_band(apIndex);
developer37646972023-06-29 10:58:43 +080011075 if (band == band_2_4) {
11076 res = snprintf(original_config_file, sizeof(original_config_file),
11077 "/etc/hostapd-2G.conf");
11078 if (os_snprintf_error(sizeof(original_config_file), res)) {
11079 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11080 return RETURN_ERR;
11081 }
11082 } else if (band == band_5) {
11083 res = snprintf(original_config_file, sizeof(original_config_file),
11084 "/etc/hostapd-5G.conf");
11085 if (os_snprintf_error(sizeof(original_config_file), res)) {
11086 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11087 return RETURN_ERR;
11088 }
11089 } else if (band == band_6) {
11090 res = snprintf(original_config_file, sizeof(original_config_file),
11091 "/etc/hostapd-6G.conf");
11092 if (os_snprintf_error(sizeof(original_config_file), res)) {
11093 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11094 return RETURN_ERR;
11095 }
11096 } else
developera3511852023-06-14 14:12:59 +080011097 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011098
developera3511852023-06-14 14:12:59 +080011099 wifi_hostapdRead(original_config_file, "wpa", wpa, sizeof(wpa));
11100 list[0].name = "wpa";
11101 list[0].value = wpa;
developer69b61b02023-03-07 17:17:44 +080011102
developera3511852023-06-14 14:12:59 +080011103 wifi_hostapdRead(original_config_file, "wpa_psk", wpa_psk, sizeof(wpa_psk));
11104 list[1].name = "wpa_psk";
11105 list[1].value = wpa_psk;
developer72fb0bb2023-01-11 09:46:29 +080011106
developera3511852023-06-14 14:12:59 +080011107 wifi_hostapdRead(original_config_file, "wpa_passphrase", wpa_passphrase, sizeof(wpa_passphrase));
11108 list[2].name = "wpa_passphrase";
11109 list[2].value = wpa_passphrase;
developer72fb0bb2023-01-11 09:46:29 +080011110
developera3511852023-06-14 14:12:59 +080011111 wifi_hostapdRead(original_config_file, "wpa_psk_file", wpa_psk_file, sizeof(wpa_psk_file));
developer72fb0bb2023-01-11 09:46:29 +080011112
developera3511852023-06-14 14:12:59 +080011113 if (strlen(wpa_psk_file) == 0)
developer32f2a182023-06-27 19:50:41 +080011114 memcpy(wpa_psk_file, PSK_FILE, strlen(PSK_FILE));
developer72fb0bb2023-01-11 09:46:29 +080011115
developera3511852023-06-14 14:12:59 +080011116 if (access(wpa_psk_file, F_OK) != 0) {
developerd14dff12023-06-28 22:47:44 +080011117 res = snprintf(cmd, sizeof(cmd), "touch %s", wpa_psk_file);
developere40952c2023-06-15 18:46:43 +080011118 if (os_snprintf_error(sizeof(cmd), res)) {
11119 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11120 return RETURN_ERR;
11121 }
developera3511852023-06-14 14:12:59 +080011122 _syscmd(cmd, buf, sizeof(buf));
11123 }
11124 list[3].name = "wpa_psk_file";
11125 list[3].value = wpa_psk_file;
developer72fb0bb2023-01-11 09:46:29 +080011126
developera3511852023-06-14 14:12:59 +080011127 wifi_hostapdRead(original_config_file, "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt));
11128 list[4].name = "wpa_key_mgmt";
11129 list[4].value = wpa_key_mgmt;
developer72fb0bb2023-01-11 09:46:29 +080011130
developera3511852023-06-14 14:12:59 +080011131 wifi_hostapdRead(original_config_file, "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise));
11132 list[5].name = "wpa_pairwise";
11133 list[5].value = wpa_pairwise;
developer72fb0bb2023-01-11 09:46:29 +080011134
developer32f2a182023-06-27 19:50:41 +080011135 res = snprintf(current_config_file, sizeof(current_config_file),
11136 "%s%d.conf", CONFIG_PREFIX, apIndex);
11137 if (os_snprintf_error(sizeof(current_config_file), res)) {
11138 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11139 return RETURN_ERR;
11140 }
developera3511852023-06-14 14:12:59 +080011141 wifi_hostapdWrite(current_config_file, list, 6);
developer72fb0bb2023-01-11 09:46:29 +080011142
developera3511852023-06-14 14:12:59 +080011143 wifi_setApEnable(apIndex, FALSE);
11144 wifi_setApEnable(apIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +080011145
developera3511852023-06-14 14:12:59 +080011146 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11147 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011148}
11149
11150//The IP Address and port number of the RADIUS server used for WLAN security. RadiusServerIPAddr is only applicable when ModeEnabled is an Enterprise type (i.e. WPA-Enterprise, WPA2-Enterprise or WPA-WPA2-Enterprise).
11151INT wifi_getApSecurityRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11152{
developera3511852023-06-14 14:12:59 +080011153 char config_file[64] = {0};
11154 char buf[64] = {0};
11155 char cmd[256] = {0};
developere40952c2023-06-15 18:46:43 +080011156 int res;
developer72fb0bb2023-01-11 09:46:29 +080011157
developera3511852023-06-14 14:12:59 +080011158 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011159
developera3511852023-06-14 14:12:59 +080011160 if(!IP_output || !Port_output || !RadiusSecret_output)
11161 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011162
developera3511852023-06-14 14:12:59 +080011163 // Read the first matched config
developere40952c2023-06-15 18:46:43 +080011164 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11165 if (os_snprintf_error(sizeof(config_file), res)) {
11166 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11167 return RETURN_ERR;
11168 }
developer32f2a182023-06-27 19:50:41 +080011169 res = snprintf(cmd, sizeof(cmd),
11170 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
developer75bd10c2023-06-27 11:34:08 +080011171 if (os_snprintf_error(sizeof(cmd), res)) {
11172 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11173 return RETURN_ERR;
11174 }
developera3511852023-06-14 14:12:59 +080011175 _syscmd(cmd, buf, sizeof(buf));
11176 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011177
developera3511852023-06-14 14:12:59 +080011178 memset(buf, 0, sizeof(buf));
developer75bd10c2023-06-27 11:34:08 +080011179 res = snprintf(cmd, sizeof(cmd), "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11180 if (os_snprintf_error(sizeof(cmd), res)) {
11181 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11182 return RETURN_ERR;
11183 }
developera3511852023-06-14 14:12:59 +080011184 _syscmd(cmd, buf, sizeof(buf));
11185 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011186
developera3511852023-06-14 14:12:59 +080011187 memset(buf, 0, sizeof(buf));
developer75bd10c2023-06-27 11:34:08 +080011188 res = snprintf(cmd, sizeof(cmd), "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11189 if (os_snprintf_error(sizeof(cmd), res)) {
11190 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11191 return RETURN_ERR;
11192 }
developera3511852023-06-14 14:12:59 +080011193 _syscmd(cmd, buf, sizeof(buf));
11194 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011195
developera3511852023-06-14 14:12:59 +080011196 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11197 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011198}
11199
11200INT wifi_setApSecurityRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11201{
developera3511852023-06-14 14:12:59 +080011202 char config_file[64] = {0};
11203 char port_str[8] = {0};
11204 char cmd[256] = {0};
11205 char buf[128] = {0};
11206 int res;
developer72fb0bb2023-01-11 09:46:29 +080011207
developera3511852023-06-14 14:12:59 +080011208 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011209
developere5750452023-05-15 16:46:42 +080011210 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011211 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011212
developera3511852023-06-14 14:12:59 +080011213 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11214 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011215
developera3511852023-06-14 14:12:59 +080011216 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11217 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011218 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11219 return RETURN_ERR;
11220 }
developer72fb0bb2023-01-11 09:46:29 +080011221
developera3511852023-06-14 14:12:59 +080011222 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 1'", config_file);
11223 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011224 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11225 return RETURN_ERR;
11226 }
developera3511852023-06-14 14:12:59 +080011227 _syscmd(cmd, buf, sizeof(buf));
11228 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011229
developere40952c2023-06-15 18:46:43 +080011230 res = snprintf(port_str, sizeof(port_str), "%d", port);
developer37646972023-06-29 10:58:43 +080011231 if (os_snprintf_error(sizeof(port_str), res)) {
11232 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11233 return RETURN_ERR;
11234 }
developera3511852023-06-14 14:12:59 +080011235 if (strlen(buf) == 0) {
11236 // Append
11237 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 1\\n"
11238 "auth_server_addr=%s\\n"
11239 "auth_server_port=%s\\n"
11240 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11241 if (os_snprintf_error(sizeof(cmd), res)) {
11242 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11243 return RETURN_ERR;
11244 }
11245 } else {
11246 // Delete the three lines setting after the "# radius 1" comment
11247 res = snprintf(cmd, sizeof(cmd), "sed -i '/# radius 1/{n;N;N;d}' %s", config_file);
11248 if (os_snprintf_error(sizeof(cmd), res)) {
11249 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11250 return RETURN_ERR;
11251 }
11252 _syscmd(cmd, buf, sizeof(buf));
11253 memset(cmd, 0, sizeof(cmd));
11254 // Use "# radius 1" comment to find the location to insert the radius setting
11255 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 1/"
11256 "# radius 1\\n"
11257 "auth_server_addr=%s\\n"
11258 "auth_server_port=%s\\n"
11259 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11260 if (os_snprintf_error(sizeof(cmd), res)) {
11261 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11262 return RETURN_ERR;
11263 }
11264 }
11265 if(_syscmd(cmd, buf, sizeof(buf))) {
11266 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11267 return RETURN_ERR;
11268 }
developer72fb0bb2023-01-11 09:46:29 +080011269
developera3511852023-06-14 14:12:59 +080011270 wifi_reloadAp(apIndex);
11271 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11272 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011273}
11274
11275INT wifi_getApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11276{
developera3511852023-06-14 14:12:59 +080011277 char config_file[64] = {0};
11278 char buf[64] = {0};
11279 char cmd[256] = {0};
developere40952c2023-06-15 18:46:43 +080011280 int res;
developer72fb0bb2023-01-11 09:46:29 +080011281
developera3511852023-06-14 14:12:59 +080011282 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011283
developera3511852023-06-14 14:12:59 +080011284 if(!IP_output || !Port_output || !RadiusSecret_output)
11285 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011286
developera3511852023-06-14 14:12:59 +080011287 // Read the second matched config
developere40952c2023-06-15 18:46:43 +080011288 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11289 if (os_snprintf_error(sizeof(config_file), res)) {
11290 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11291 return RETURN_ERR;
11292 }
11293
developer32f2a182023-06-27 19:50:41 +080011294 res = snprintf(cmd, sizeof(cmd),
11295 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11296 config_file);
developer75bd10c2023-06-27 11:34:08 +080011297 if (os_snprintf_error(sizeof(cmd), res)) {
11298 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11299 return RETURN_ERR;
11300 }
developera3511852023-06-14 14:12:59 +080011301 _syscmd(cmd, buf, sizeof(buf));
11302 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011303
developera3511852023-06-14 14:12:59 +080011304 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080011305 res = snprintf(cmd, sizeof(cmd),
11306 "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11307 config_file);
developer75bd10c2023-06-27 11:34:08 +080011308 if (os_snprintf_error(sizeof(cmd), res)) {
11309 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11310 return RETURN_ERR;
11311 }
developera3511852023-06-14 14:12:59 +080011312 _syscmd(cmd, buf, sizeof(buf));
11313 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011314
developera3511852023-06-14 14:12:59 +080011315 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080011316 res = snprintf(cmd, sizeof(cmd),
11317 "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11318 config_file);
developer75bd10c2023-06-27 11:34:08 +080011319 if (os_snprintf_error(sizeof(cmd), res)) {
11320 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11321 return RETURN_ERR;
11322 }
11323
developera3511852023-06-14 14:12:59 +080011324 _syscmd(cmd, buf, sizeof(buf));
11325 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011326
developera3511852023-06-14 14:12:59 +080011327 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11328 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011329}
11330
11331INT wifi_setApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11332{
developera3511852023-06-14 14:12:59 +080011333 char config_file[64] = {0};
11334 char port_str[8] = {0};
11335 char cmd[256] = {0};
11336 char buf[128] = {0};
11337 int res;
developer72fb0bb2023-01-11 09:46:29 +080011338
developera3511852023-06-14 14:12:59 +080011339 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011340
developere5750452023-05-15 16:46:42 +080011341 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011342 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011343
developera3511852023-06-14 14:12:59 +080011344 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11345 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011346
developera3511852023-06-14 14:12:59 +080011347 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11348 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11350 return RETURN_ERR;
11351 }
developer72fb0bb2023-01-11 09:46:29 +080011352
developera3511852023-06-14 14:12:59 +080011353 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 2'", config_file);
11354 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011355 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11356 return RETURN_ERR;
11357 }
developera3511852023-06-14 14:12:59 +080011358 _syscmd(cmd, buf, sizeof(buf));
11359 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011360
developera3511852023-06-14 14:12:59 +080011361 res = snprintf(port_str, sizeof(port_str), "%d", port);
11362 if (os_snprintf_error(sizeof(port_str), res)) {
developer46506162023-06-12 10:09:39 +080011363 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11364 return RETURN_ERR;
11365 }
developera3511852023-06-14 14:12:59 +080011366 if (strlen(buf) == 0) {
11367 // Append
11368 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 2\\n"
11369 "auth_server_addr=%s\\n"
11370 "auth_server_port=%s\\n"
11371 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11372 if (os_snprintf_error(sizeof(cmd), res)) {
11373 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11374 return RETURN_ERR;
11375 }
11376 } else {
11377 // Delete the three lines setting after the "# radius 2" comment
11378 res = snprintf(cmd, sizeof(cmd), "sed -i '/# radius 2/{n;N;N;d}' %s", config_file);
11379 if (os_snprintf_error(sizeof(cmd), res)) {
11380 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11381 return RETURN_ERR;
11382 }
11383 _syscmd(cmd, buf, sizeof(buf));
11384 memset(cmd, 0, sizeof(cmd));
11385 // Use "# radius 2" comment to find the location to insert the radius setting
11386 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 2/"
11387 "# radius 2\\n"
11388 "auth_server_addr=%s\\n"
11389 "auth_server_port=%s\\n"
11390 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11391 if (os_snprintf_error(sizeof(cmd), res)) {
11392 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11393 return RETURN_ERR;
11394 }
11395 }
11396 if(_syscmd(cmd, buf, sizeof(buf))) {
11397 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11398 return RETURN_ERR;
11399 }
developer72fb0bb2023-01-11 09:46:29 +080011400
developera3511852023-06-14 14:12:59 +080011401 wifi_reloadAp(apIndex);
11402 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11403 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011404}
11405
11406//RadiusSettings
11407INT wifi_getApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *output)
11408{
developera3511852023-06-14 14:12:59 +080011409 if(!output)
11410 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011411
developera3511852023-06-14 14:12:59 +080011412 output->RadiusServerRetries = 3; //Number of retries for Radius requests.
11413 output->RadiusServerRequestTimeout = 5; //Radius request timeout in seconds after which the request must be retransmitted for the # of retries available.
11414 output->PMKLifetime = 28800; //Default time in seconds after which a Wi-Fi client is forced to ReAuthenticate (def 8 hrs).
11415 output->PMKCaching = FALSE; //Enable or disable caching of PMK.
11416 output->PMKCacheInterval = 300; //Time interval in seconds after which the PMKSA (Pairwise Master Key Security Association) cache is purged (def 5 minutes).
11417 output->MaxAuthenticationAttempts = 3; //Indicates the # of time, a client can attempt to login with incorrect credentials. When this limit is reached, the client is blacklisted and not allowed to attempt loging into the network. Settings this parameter to 0 (zero) disables the blacklisting feature.
11418 output->BlacklistTableTimeout = 600; //Time interval in seconds for which a client will continue to be blacklisted once it is marked so.
11419 output->IdentityRequestRetryInterval = 5; //Time Interval in seconds between identity requests retries. A value of 0 (zero) disables it.
11420 output->QuietPeriodAfterFailedAuthentication = 5; //The enforced quiet period (time interval) in seconds following failed authentication. A value of 0 (zero) disables it.
developere40952c2023-06-15 18:46:43 +080011421 //res = snprintf(output->RadiusSecret, 64, "12345678"); //The secret used for handshaking with the RADIUS server [RFC2865]. When read, this parameter returns an empty string, regardless of the actual value.
developer72fb0bb2023-01-11 09:46:29 +080011422
developera3511852023-06-14 14:12:59 +080011423 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011424}
11425
11426INT wifi_setApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *input)
11427{
developera3511852023-06-14 14:12:59 +080011428 //store the paramters, and apply instantly
11429 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011430}
11431
11432//Device.WiFi.AccessPoint.{i}.WPS.Enable
11433//Enables or disables WPS functionality for this access point.
11434// outputs the WPS enable state of this ap in output_bool
11435INT wifi_getApWpsEnable(INT apIndex, BOOL *output_bool)
11436{
developera3511852023-06-14 14:12:59 +080011437 char interface_name[16] = {0};
11438 char buf[MAX_BUF_SIZE] = {0}, cmd[MAX_CMD_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080011439 int res;
11440
developera3511852023-06-14 14:12:59 +080011441 if(!output_bool)
11442 return RETURN_ERR;
11443 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11444 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080011445 res = snprintf(cmd, sizeof(cmd),
11446 "hostapd_cli -i %s get_config | grep wps_state | cut -d '=' -f2",
11447 interface_name);
developer75bd10c2023-06-27 11:34:08 +080011448 if (os_snprintf_error(sizeof(cmd), res)) {
11449 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11450 return RETURN_ERR;
11451 }
developera3511852023-06-14 14:12:59 +080011452 _syscmd(cmd, buf, sizeof(buf));
11453 if(strstr(buf, "configured"))
11454 *output_bool=TRUE;
11455 else
11456 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +080011457
developera3511852023-06-14 14:12:59 +080011458 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080011459}
developer72fb0bb2023-01-11 09:46:29 +080011460
11461//Device.WiFi.AccessPoint.{i}.WPS.Enable
11462// sets the WPS enable enviornment variable for this ap to the value of enableValue, 1==enabled, 0==disabled
11463INT wifi_setApWpsEnable(INT apIndex, BOOL enable)
11464{
developera3511852023-06-14 14:12:59 +080011465 char config_file[MAX_BUF_SIZE] = {0};
11466 char buf[128] = {0};
11467 struct params params;
developere40952c2023-06-15 18:46:43 +080011468 int res;
developer72fb0bb2023-01-11 09:46:29 +080011469
developera3511852023-06-14 14:12:59 +080011470 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11471 //store the paramters, and wait for wifi up to apply
11472 params.name = "wps_state";
11473 if (enable == TRUE) {
11474 wifi_getApBeaconType(apIndex, buf);
11475 if (strncmp(buf, "None", 4) == 0) // If ap didn't set encryption
11476 params.value = "1";
11477 else // If ap set encryption
11478 params.value = "2";
11479 } else {
11480 params.value = "0";
11481 }
developer72fb0bb2023-01-11 09:46:29 +080011482
developere40952c2023-06-15 18:46:43 +080011483 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11484 if (os_snprintf_error(sizeof(config_file), res)) {
11485 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11486 return RETURN_ERR;
11487 }
developera3511852023-06-14 14:12:59 +080011488 wifi_hostapdWrite(config_file, &params, 1);
11489 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11490 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080011491
developera3511852023-06-14 14:12:59 +080011492 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11493 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011494}
11495
11496//Comma-separated list of strings. Indicates WPS configuration methods supported by the device. Each list item is an enumeration of: USBFlashDrive,Ethernet,ExternalNFCToken,IntegratedNFCToken,NFCInterface,PushButton,PIN
11497INT wifi_getApWpsConfigMethodsSupported(INT apIndex, CHAR *output)
11498{
developere40952c2023-06-15 18:46:43 +080011499 int res;
developera3511852023-06-14 14:12:59 +080011500 if(!output)
11501 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011502 res = snprintf(output, 128, "PushButton,PIN");
11503 if (os_snprintf_error(128, res)) {
11504 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11505 return RETURN_ERR;
11506 }
11507
developera3511852023-06-14 14:12:59 +080011508 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011509}
11510
11511//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11512//Comma-separated list of strings. Each list item MUST be a member of the list reported by the ConfigMethodsSupported parameter. Indicates WPS configuration methods enabled on the device.
11513// Outputs a common separated list of the enabled WPS config methods, 64 bytes max
11514INT wifi_getApWpsConfigMethodsEnabled(INT apIndex, CHAR *output)
11515{
developere40952c2023-06-15 18:46:43 +080011516 int res;
developera3511852023-06-14 14:12:59 +080011517 if(!output)
11518 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011519 res = snprintf(output, 64, "PushButton,PIN");//Currently, supporting these two methods
11520 if (os_snprintf_error(64, res)) {
11521 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11522 return RETURN_ERR;
11523 }
developer72fb0bb2023-01-11 09:46:29 +080011524
developera3511852023-06-14 14:12:59 +080011525 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011526}
11527
11528//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11529// sets an enviornment variable that specifies the WPS configuration method(s). methodString is a comma separated list of methods USBFlashDrive,Ethernet,ExternalNFCToken,IntegratedNFCToken,NFCInterface,PushButton,PIN
11530INT wifi_setApWpsConfigMethodsEnabled(INT apIndex, CHAR *methodString)
11531{
developera3511852023-06-14 14:12:59 +080011532 //apply instantly. No setting need to be stored.
11533 char methods[MAX_BUF_SIZE], *token, *next_token;
11534 char config_file[MAX_BUF_SIZE], config_methods[MAX_BUF_SIZE] = {0};
11535 struct params params;
developere40952c2023-06-15 18:46:43 +080011536 int res;
developer72fb0bb2023-01-11 09:46:29 +080011537
developera3511852023-06-14 14:12:59 +080011538 if(!methodString)
11539 return RETURN_ERR;
11540 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11541 //store the paramters, and wait for wifi up to apply
developer72fb0bb2023-01-11 09:46:29 +080011542
developere40952c2023-06-15 18:46:43 +080011543 res = snprintf(methods, sizeof(methods), "%s", methodString);
11544 if (os_snprintf_error(sizeof(methods), res)) {
11545 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11546 return RETURN_ERR;
11547 }
developera3511852023-06-14 14:12:59 +080011548 for(token=methods; *token; token=next_token) {
11549 strtok_r(token, ",", &next_token);
11550 if(*token=='U' && !strcmp(methods, "USBFlashDrive"))
developere40952c2023-06-15 18:46:43 +080011551 res = snprintf(config_methods, sizeof(config_methods), "%s ", "usba");
developera3511852023-06-14 14:12:59 +080011552 else if(*token=='E')
11553 {
11554 if(!strcmp(methods, "Ethernet"))
developere40952c2023-06-15 18:46:43 +080011555 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ethernet");
developera3511852023-06-14 14:12:59 +080011556 else if(!strcmp(methods, "ExternalNFCToken"))
developere40952c2023-06-15 18:46:43 +080011557 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ext_nfc_token");
developera3511852023-06-14 14:12:59 +080011558 else
11559 printf("%s: Unknown WpsConfigMethod\n", __func__);
11560 }
11561 else if(*token=='I' && !strcmp(token, "IntegratedNFCToken"))
developere40952c2023-06-15 18:46:43 +080011562 res = snprintf(config_methods, sizeof(config_methods), "%s ", "int_nfc_token");
developera3511852023-06-14 14:12:59 +080011563 else if(*token=='N' && !strcmp(token, "NFCInterface"))
developere40952c2023-06-15 18:46:43 +080011564 res = snprintf(config_methods, sizeof(config_methods), "%s ", "nfc_interface");
developera3511852023-06-14 14:12:59 +080011565 else if(*token=='P' )
11566 {
11567 if(!strcmp(token, "PushButton"))
developere40952c2023-06-15 18:46:43 +080011568 res = snprintf(config_methods, sizeof(config_methods), "%s ", "push_button");
developera3511852023-06-14 14:12:59 +080011569 else if(!strcmp(token, "PIN"))
developere40952c2023-06-15 18:46:43 +080011570 res = snprintf(config_methods, sizeof(config_methods), "%s ", "keypad");
developera3511852023-06-14 14:12:59 +080011571 else
11572 printf("%s: Unknown WpsConfigMethod\n", __func__);
11573 }
11574 else
11575 printf("%s: Unknown WpsConfigMethod\n", __func__);
developere40952c2023-06-15 18:46:43 +080011576
developer37646972023-06-29 10:58:43 +080011577 if (os_snprintf_error(sizeof(config_methods), res)) {
11578 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11579 return RETURN_ERR;
11580 }
11581 }
developera3511852023-06-14 14:12:59 +080011582 params.name = "config_methods";
11583 params.value = config_methods;
developere40952c2023-06-15 18:46:43 +080011584 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11585 if (os_snprintf_error(sizeof(config_file), res)) {
11586 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11587 return RETURN_ERR;
11588 }
developera3511852023-06-14 14:12:59 +080011589 wifi_hostapdWrite(config_file, &params, 1);
11590 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11591 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011592
developera3511852023-06-14 14:12:59 +080011593 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011594}
11595
11596// outputs the pin value, ulong_pin must be allocated by the caller
11597INT wifi_getApWpsDevicePIN(INT apIndex, ULONG *output_ulong)
11598{
developera3511852023-06-14 14:12:59 +080011599 char buf[MAX_BUF_SIZE] = {0};
11600 char cmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080011601 int res;
developer72fb0bb2023-01-11 09:46:29 +080011602
developera3511852023-06-14 14:12:59 +080011603 if(!output_ulong)
11604 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011605 res = snprintf(cmd, sizeof(cmd), "cat %s%d.conf | grep ap_pin | cut -d '=' -f2", CONFIG_PREFIX, apIndex);
11606 if (os_snprintf_error(sizeof(cmd), res)) {
11607 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11608 return RETURN_ERR;
11609 }
developera3511852023-06-14 14:12:59 +080011610 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +080011611 if(strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080011612 if (hal_strtoul(buf, 10, output_ulong) < 0) {
11613 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080011614 }
11615 }
developer72fb0bb2023-01-11 09:46:29 +080011616
developera3511852023-06-14 14:12:59 +080011617 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011618}
11619
11620// set an enviornment variable for the WPS pin for the selected AP. Normally, Device PIN should not be changed.
11621INT wifi_setApWpsDevicePIN(INT apIndex, ULONG pin)
11622{
developera3511852023-06-14 14:12:59 +080011623 //set the pin to wifi config and hostpad config. wait for wifi reset or hostapd reset to apply
11624 char ap_pin[16] = {0};
11625 char config_file[MAX_BUF_SIZE] = {0};
11626 struct params params;
developere40952c2023-06-15 18:46:43 +080011627 int res;
developer72fb0bb2023-01-11 09:46:29 +080011628
developera3511852023-06-14 14:12:59 +080011629 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011630 res = snprintf(ap_pin, sizeof(ap_pin), "%lu", pin);
11631 if (os_snprintf_error(sizeof(ap_pin), res)) {
11632 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11633 return RETURN_ERR;
11634 }
developera3511852023-06-14 14:12:59 +080011635 params.name = "ap_pin";
11636 params.value = ap_pin;
developere40952c2023-06-15 18:46:43 +080011637 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11638 if (os_snprintf_error(sizeof(config_file), res)) {
11639 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11640 return RETURN_ERR;
11641 }
developera3511852023-06-14 14:12:59 +080011642 wifi_hostapdWrite(config_file, &params, 1);
11643 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11644 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011645
developera3511852023-06-14 14:12:59 +080011646 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011647}
11648
11649// Output string is either Not configured or Configured, max 32 characters
11650INT wifi_getApWpsConfigurationState(INT apIndex, CHAR *output_string)
11651{
developera3511852023-06-14 14:12:59 +080011652 char interface_name[16] = {0};
11653 char cmd[MAX_CMD_SIZE];
11654 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011655 int res;
developer72fb0bb2023-01-11 09:46:29 +080011656
developera3511852023-06-14 14:12:59 +080011657 if(!output_string)
11658 return RETURN_ERR;
11659 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011660 res = snprintf(output_string, 32, "Not configured");
11661 if (os_snprintf_error(32, res)) {
11662 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11663 return RETURN_ERR;
11664 }
developera3511852023-06-14 14:12:59 +080011665 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11666 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011667 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep wps_state | cut -d'=' -f2", interface_name);
11668 if (os_snprintf_error(sizeof(cmd), res)) {
11669 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11670 return RETURN_ERR;
11671 }
developera3511852023-06-14 14:12:59 +080011672 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011673
developerc14d83a2023-06-29 20:09:42 +080011674 if(!strncmp(buf, "configured", 10)) {
developere40952c2023-06-15 18:46:43 +080011675 res = snprintf(output_string, 32, "Configured");
developerc14d83a2023-06-29 20:09:42 +080011676 if (os_snprintf_error(32, res)) {
11677 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11678 return RETURN_ERR;
11679 }
11680 }
developera3511852023-06-14 14:12:59 +080011681 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011682
developera3511852023-06-14 14:12:59 +080011683 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011684}
11685
11686// sets the WPS pin for this AP
11687INT wifi_setApWpsEnrolleePin(INT apIndex, CHAR *pin)
11688{
developera3511852023-06-14 14:12:59 +080011689 char interface_name[16] = {0};
11690 char cmd[MAX_CMD_SIZE];
11691 char buf[MAX_BUF_SIZE]={0};
developer9ce44382023-06-28 11:09:37 +080011692 BOOL enable = 0;
developere40952c2023-06-15 18:46:43 +080011693 int res;
developer72fb0bb2023-01-11 09:46:29 +080011694
developera3511852023-06-14 14:12:59 +080011695 wifi_getApEnable(apIndex, &enable);
11696 if (!enable)
11697 return RETURN_ERR;
11698 wifi_getApWpsEnable(apIndex, &enable);
11699 if (!enable)
11700 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011701
developera3511852023-06-14 14:12:59 +080011702 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11703 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011704 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_pin any %s", interface_name, pin);
11705 if (os_snprintf_error(sizeof(cmd), res)) {
11706 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11707 return RETURN_ERR;
11708 }
developera3511852023-06-14 14:12:59 +080011709 _syscmd(cmd, buf, sizeof(buf));
11710 if((strstr(buf, "OK"))!=NULL)
11711 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011712
developera3511852023-06-14 14:12:59 +080011713 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011714}
11715
11716// This function is called when the WPS push button has been pressed for this AP
11717INT wifi_setApWpsButtonPush(INT apIndex)
11718{
developera3511852023-06-14 14:12:59 +080011719 char cmd[MAX_CMD_SIZE];
11720 char buf[MAX_BUF_SIZE]={0};
11721 char interface_name[16] = {0};
11722 BOOL enable=FALSE;
developere40952c2023-06-15 18:46:43 +080011723 int res;
developer72fb0bb2023-01-11 09:46:29 +080011724
developera3511852023-06-14 14:12:59 +080011725 wifi_getApEnable(apIndex, &enable);
11726 if (!enable)
11727 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011728
developera3511852023-06-14 14:12:59 +080011729 wifi_getApWpsEnable(apIndex, &enable);
11730 if (!enable)
11731 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011732
developera3511852023-06-14 14:12:59 +080011733 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11734 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011735
developere40952c2023-06-15 18:46:43 +080011736 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_cancel; hostapd_cli -i%s wps_pbc", interface_name, interface_name);
11737 if (os_snprintf_error(sizeof(cmd), res)) {
11738 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11739 return RETURN_ERR;
11740 }
developera3511852023-06-14 14:12:59 +080011741 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011742
developera3511852023-06-14 14:12:59 +080011743 if((strstr(buf, "OK"))!=NULL)
11744 return RETURN_OK;
11745 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011746}
11747
11748// cancels WPS mode for this AP
11749INT wifi_cancelApWPS(INT apIndex)
11750{
developera3511852023-06-14 14:12:59 +080011751 char interface_name[16] = {0};
11752 char cmd[MAX_CMD_SIZE];
11753 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011754 int res;
developer72fb0bb2023-01-11 09:46:29 +080011755
developera3511852023-06-14 14:12:59 +080011756 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11757 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011758 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_cancel", interface_name);
11759 if (os_snprintf_error(sizeof(cmd), res)) {
11760 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11761 return RETURN_ERR;
11762 }
developera3511852023-06-14 14:12:59 +080011763 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011764
developera3511852023-06-14 14:12:59 +080011765 if((strstr(buf, "OK"))!=NULL)
11766 return RETURN_OK;
11767 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011768}
11769
11770//Device.WiFi.AccessPoint.{i}.AssociatedDevice.*
11771//HAL funciton should allocate an data structure array, and return to caller with "associated_dev_array"
11772INT wifi_getApAssociatedDeviceDiagnosticResult(INT apIndex, wifi_associated_dev_t **associated_dev_array, UINT *output_array_size)
11773{
developera3511852023-06-14 14:12:59 +080011774 char interface_name[16] = {0};
11775 FILE *f = NULL;
11776 int read_flag=0, auth_temp=0, mac_temp=0;
11777 char cmd[256] = {0}, buf[2048] = {0};
11778 char *param = NULL, *value = NULL, *line=NULL;
11779 size_t len = 0;
11780 wifi_associated_dev_t *dev=NULL;
11781 int res;
developer72fb0bb2023-01-11 09:46:29 +080011782
developera3511852023-06-14 14:12:59 +080011783 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11784 *associated_dev_array = NULL;
11785 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11786 return RETURN_ERR;
11787 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
11788 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011789 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11790 return RETURN_ERR;
11791 }
developera3511852023-06-14 14:12:59 +080011792 _syscmd(cmd,buf,sizeof(buf));
11793 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011794
developera3511852023-06-14 14:12:59 +080011795 if (*output_array_size <= 0)
11796 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011797
developera3511852023-06-14 14:12:59 +080011798 dev=(wifi_associated_dev_t *) calloc (*output_array_size, sizeof(wifi_associated_dev_t));
developere75ba632023-06-29 16:03:33 +080011799 if (!dev) {
11800 wifi_debug(DEBUG_ERROR, "Unexpected calloc fail\n");
11801 return RETURN_ERR;
11802 }
developera3511852023-06-14 14:12:59 +080011803 *associated_dev_array = dev;
11804 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta > /tmp/connected_devices.txt" , interface_name);
11805 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011806 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11807 return RETURN_ERR;
11808 }
developera3511852023-06-14 14:12:59 +080011809 _syscmd(cmd,buf,sizeof(buf));
11810 f = fopen("/tmp/connected_devices.txt", "r");
11811 if (f==NULL)
11812 {
11813 *output_array_size=0;
11814 return RETURN_ERR;
11815 }
11816 while ((getline(&line, &len, f)) != -1)
11817 {
11818 param = strtok(line,"=");
developere75ba632023-06-29 16:03:33 +080011819 if (!param)
11820 continue;
developera3511852023-06-14 14:12:59 +080011821 value = strtok(NULL,"=");
developere75ba632023-06-29 16:03:33 +080011822 if (!value)
11823 continue;
developer72fb0bb2023-01-11 09:46:29 +080011824
developera3511852023-06-14 14:12:59 +080011825 if( strcmp("flags",param) == 0 )
11826 {
11827 value[strlen(value)-1]='\0';
11828 if(strstr (value,"AUTHORIZED") != NULL )
11829 {
11830 dev[auth_temp].cli_AuthenticationState = 1;
11831 dev[auth_temp].cli_Active = 1;
11832 auth_temp++;
11833 read_flag=1;
11834 }
11835 }
11836 if(read_flag==1)
11837 {
11838 if( strcmp("dot11RSNAStatsSTAAddress",param) == 0 )
11839 {
11840 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080011841 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
developera3511852023-06-14 14:12:59 +080011842 (unsigned int *)&dev[mac_temp].cli_MACAddress[0],
11843 (unsigned int *)&dev[mac_temp].cli_MACAddress[1],
11844 (unsigned int *)&dev[mac_temp].cli_MACAddress[2],
11845 (unsigned int *)&dev[mac_temp].cli_MACAddress[3],
11846 (unsigned int *)&dev[mac_temp].cli_MACAddress[4],
developere75ba632023-06-29 16:03:33 +080011847 (unsigned int *)&dev[mac_temp].cli_MACAddress[5] ) == EOF)
11848 continue;
developera3511852023-06-14 14:12:59 +080011849 mac_temp++;
11850 read_flag=0;
11851 }
11852 }
11853 }
11854 *output_array_size = auth_temp;
11855 auth_temp=0;
11856 mac_temp=0;
11857 free(line);
developere75ba632023-06-29 16:03:33 +080011858 if (fclose(f) == EOF) {
11859 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
11860 return RETURN_ERR;
11861 }
developera3511852023-06-14 14:12:59 +080011862 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11863 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011864}
11865
11866#define MACADDRESS_SIZE 6
11867
11868INT wifihal_AssociatedDevicesstats3(INT apIndex,CHAR *interface_name,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
11869{
developera3511852023-06-14 14:12:59 +080011870 FILE *fp = NULL;
11871 char str[MAX_BUF_SIZE] = {0};
11872 int wificlientindex = 0 ;
11873 int count = 0;
11874 int signalstrength = 0;
11875 int arr[MACADDRESS_SIZE] = {0};
11876 unsigned char mac[MACADDRESS_SIZE] = {0};
11877 UINT wifi_count = 0;
developere40952c2023-06-15 18:46:43 +080011878 int res;
developerc14d83a2023-06-29 20:09:42 +080011879 wifi_associated_dev3_t* temp = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011880
developera3511852023-06-14 14:12:59 +080011881 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11882 *output_array_size = 0;
11883 *associated_dev_array = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011884
developer33f13ba2023-07-12 16:19:06 +080011885 fp = v_secure_popen("r", "iw dev %s station dump | grep %s | wc -l",
11886 interface_name, interface_name);
developera3511852023-06-14 14:12:59 +080011887 if (fp == NULL)
11888 {
11889 printf("Failed to run command inside function %s\n",__FUNCTION__ );
11890 return RETURN_ERR;
11891 }
developer72fb0bb2023-01-11 09:46:29 +080011892
developera3511852023-06-14 14:12:59 +080011893 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080011894 if (fgets(str, sizeof(str)-1, fp) == NULL) {
11895 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080011896 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011897 return RETURN_ERR;
11898 }
developera3511852023-06-14 14:12:59 +080011899 wifi_count = (unsigned int) atoi ( str );
11900 *output_array_size = wifi_count;
11901 printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
developer33f13ba2023-07-12 16:19:06 +080011902 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080011903
developera3511852023-06-14 14:12:59 +080011904 if(wifi_count == 0)
11905 {
11906 return RETURN_OK;
11907 }
11908 else
11909 {
developer9ce44382023-06-28 11:09:37 +080011910 if(wifi_count <= 0 || wifi_count > MAX_ASSOCIATED_STA_NUM){
11911 return RETURN_ERR;
11912 }
developera3511852023-06-14 14:12:59 +080011913 temp = (wifi_associated_dev3_t*)calloc(1, sizeof(wifi_associated_dev3_t)*wifi_count) ;
11914 if(temp == NULL)
11915 {
11916 printf("Error Statement. Insufficient memory \n");
11917 return RETURN_ERR;
11918 }
developer72fb0bb2023-01-11 09:46:29 +080011919
developer33f13ba2023-07-12 16:19:06 +080011920 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
11921 if (res) {
11922 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011923 free(temp);
developere40952c2023-06-15 18:46:43 +080011924 return RETURN_ERR;
11925 }
developer33f13ba2023-07-12 16:19:06 +080011926
developera3511852023-06-14 14:12:59 +080011927 if(apIndex == 0)
developer33f13ba2023-07-12 16:19:06 +080011928 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_2G.txt", interface_name);
developera3511852023-06-14 14:12:59 +080011929 else if(apIndex == 1)
developer33f13ba2023-07-12 16:19:06 +080011930 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", interface_name);
11931 if (res) {
11932 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011933 free(temp);
developere40952c2023-06-15 18:46:43 +080011934 return RETURN_ERR;
11935 }
developer72fb0bb2023-01-11 09:46:29 +080011936
developera3511852023-06-14 14:12:59 +080011937 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
11938 if(fp == NULL)
11939 {
11940 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
11941 free(temp);
11942 return RETURN_ERR;
11943 }
developere75ba632023-06-29 16:03:33 +080011944 if (fclose(fp) == EOF) {
11945 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developerc14d83a2023-06-29 20:09:42 +080011946 free(temp);
developere75ba632023-06-29 16:03:33 +080011947 return RETURN_ERR;
11948 }
developer72fb0bb2023-01-11 09:46:29 +080011949
developer33f13ba2023-07-12 16:19:06 +080011950 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
developera3511852023-06-14 14:12:59 +080011951 if(fp)
11952 {
11953 for(count =0 ; count < wifi_count; count++)
11954 {
developer86035662023-06-28 19:21:12 +080011955 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11956 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011957 goto err;
developer86035662023-06-28 19:21:12 +080011958 }
developera3511852023-06-14 14:12:59 +080011959 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
11960 {
11961 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
11962 {
11963 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080011964
developera3511852023-06-14 14:12:59 +080011965 }
11966 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
11967 printf("MAC %d = %X:%X:%X:%X:%X:%X \n", count, temp[count].cli_MACAddress[0],temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2], temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4], temp[count].cli_MACAddress[5]);
11968 }
11969 temp[count].cli_AuthenticationState = 1; //TODO
11970 temp[count].cli_Active = 1; //TODO
11971 }
developer33f13ba2023-07-12 16:19:06 +080011972 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011973 }
11974
developer33f13ba2023-07-12 16:19:06 +080011975 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep signal | tr -s ' ' | cut -d ' ' -f 2 > /tmp/wifi_signalstrength.txt");
developera3511852023-06-14 14:12:59 +080011976 if(fp)
11977 {
developer33f13ba2023-07-12 16:19:06 +080011978 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011979 }
developer33f13ba2023-07-12 16:19:06 +080011980 fp = v_secure_popen("r", "cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011981 if(fp)
11982 {
11983 for(count =0 ; count < wifi_count ;count++)
11984 {
developer86035662023-06-28 19:21:12 +080011985 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11986 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011987 goto err;
developer86035662023-06-28 19:21:12 +080011988 }
developera3511852023-06-14 14:12:59 +080011989 signalstrength = atoi(str);
11990 temp[count].cli_SignalStrength = signalstrength;
11991 temp[count].cli_RSSI = signalstrength;
11992 temp[count].cli_SNR = signalstrength + 95;
11993 }
developer33f13ba2023-07-12 16:19:06 +080011994 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011995 }
developer72fb0bb2023-01-11 09:46:29 +080011996
11997
developera3511852023-06-14 14:12:59 +080011998 if((apIndex == 0) || (apIndex == 4))
11999 {
12000 for(count =0 ; count < wifi_count ;count++)
12001 {
developer32f2a182023-06-27 19:50:41 +080012002 memcpy(temp[count].cli_OperatingStandard,"g", 1);
12003 temp[count].cli_OperatingStandard[1] = '\0';
12004 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
12005 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080012006 }
developer72fb0bb2023-01-11 09:46:29 +080012007
developera3511852023-06-14 14:12:59 +080012008 //BytesSent
developer33f13ba2023-07-12 16:19:06 +080012009 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx bytes' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bytes_Send.txt");
developera3511852023-06-14 14:12:59 +080012010 if(fp)
12011 {
developer33f13ba2023-07-12 16:19:06 +080012012 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012013 }
developer33f13ba2023-07-12 16:19:06 +080012014 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012015 if(fp)
12016 {
12017 for (count = 0; count < wifi_count; count++)
12018 {
developer86035662023-06-28 19:21:12 +080012019 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12020 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080012021 v_secure_pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080012022 free(temp);
developer86035662023-06-28 19:21:12 +080012023 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080012024 }
developerc14d83a2023-06-29 20:09:42 +080012025 if (hal_strtoul(str, 10, &(temp[count].cli_BytesSent)) < 0) {
12026 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012027 }
developera3511852023-06-14 14:12:59 +080012028 }
developer33f13ba2023-07-12 16:19:06 +080012029 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012030 }
developer72fb0bb2023-01-11 09:46:29 +080012031
developera3511852023-06-14 14:12:59 +080012032 //BytesReceived
developer86035662023-06-28 19:21:12 +080012033
developer33f13ba2023-07-12 16:19:06 +080012034 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'rx bytes' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bytes_Received.txt");
developera3511852023-06-14 14:12:59 +080012035 if (fp)
12036 {
developer33f13ba2023-07-12 16:19:06 +080012037 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012038 }
developer33f13ba2023-07-12 16:19:06 +080012039 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012040 if (fp)
12041 {
12042 for (count = 0; count < wifi_count; count++)
12043 {
developer86035662023-06-28 19:21:12 +080012044 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12045 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012046 goto err;
12047 }
12048 if (hal_strtoul(str, 10, &(temp[count].cli_BytesReceived)) < 0) {
12049 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012050 }
developera3511852023-06-14 14:12:59 +080012051 }
developer33f13ba2023-07-12 16:19:06 +080012052 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012053 }
developer72fb0bb2023-01-11 09:46:29 +080012054
developera3511852023-06-14 14:12:59 +080012055 //PacketsSent
developer33f13ba2023-07-12 16:19:06 +080012056 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx packets' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Packets_Send.txt");
developera3511852023-06-14 14:12:59 +080012057 if (fp)
12058 {
developer33f13ba2023-07-12 16:19:06 +080012059 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012060 }
developer72fb0bb2023-01-11 09:46:29 +080012061
developer33f13ba2023-07-12 16:19:06 +080012062 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012063 if (fp)
12064 {
12065 for (count = 0; count < wifi_count; count++)
12066 {
developer86035662023-06-28 19:21:12 +080012067 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12068 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012069 goto err;
developer5b23cd02023-07-19 20:26:03 +080012070 }
developerc14d83a2023-06-29 20:09:42 +080012071 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsSent)) < 0) {
12072 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012073 }
developera3511852023-06-14 14:12:59 +080012074 }
developer33f13ba2023-07-12 16:19:06 +080012075 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012076 }
developer72fb0bb2023-01-11 09:46:29 +080012077
developera3511852023-06-14 14:12:59 +080012078 //PacketsReceived
developer33f13ba2023-07-12 16:19:06 +080012079 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'rx packets' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Packets_Received.txt");
developera3511852023-06-14 14:12:59 +080012080 if (fp)
12081 {
developer33f13ba2023-07-12 16:19:06 +080012082 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012083 }
developer33f13ba2023-07-12 16:19:06 +080012084 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012085 if (fp)
12086 {
12087 for (count = 0; count < wifi_count; count++)
12088 {
developer86035662023-06-28 19:21:12 +080012089 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12090 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012091 goto err;
developer5b23cd02023-07-19 20:26:03 +080012092 }
developerc14d83a2023-06-29 20:09:42 +080012093 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsReceived)) < 0) {
12094 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012095 }
developera3511852023-06-14 14:12:59 +080012096 }
developer33f13ba2023-07-12 16:19:06 +080012097 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012098 }
developer72fb0bb2023-01-11 09:46:29 +080012099
developera3511852023-06-14 14:12:59 +080012100 //ErrorsSent
developer33f13ba2023-07-12 16:19:06 +080012101 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx failed' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Tx_Failed.txt");
developera3511852023-06-14 14:12:59 +080012102 if (fp)
12103 {
developer33f13ba2023-07-12 16:19:06 +080012104 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012105 }
developer33f13ba2023-07-12 16:19:06 +080012106 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012107 if (fp)
12108 {
12109 for (count = 0; count < wifi_count; count++)
12110 {
developer86035662023-06-28 19:21:12 +080012111 if (fgets(str, MAX_BUF_SIZE, fp) == NULL){
12112 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012113 goto err;
developer5b23cd02023-07-19 20:26:03 +080012114 }
developerc14d83a2023-06-29 20:09:42 +080012115 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
12116 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012117 }
developera3511852023-06-14 14:12:59 +080012118 }
developer33f13ba2023-07-12 16:19:06 +080012119 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012120 }
developer72fb0bb2023-01-11 09:46:29 +080012121
developera3511852023-06-14 14:12:59 +080012122 //ErrorsSent
developer86035662023-06-28 19:21:12 +080012123
developer33f13ba2023-07-12 16:19:06 +080012124 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx failed' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Tx_Failed.txt");
developera3511852023-06-14 14:12:59 +080012125 if (fp)
12126 {
developer33f13ba2023-07-12 16:19:06 +080012127 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012128 }
developer33f13ba2023-07-12 16:19:06 +080012129 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012130 if (fp)
12131 {
12132 for (count = 0; count < wifi_count; count++)
12133 {
developer86035662023-06-28 19:21:12 +080012134 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12135 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012136 goto err;
developer5b23cd02023-07-19 20:26:03 +080012137 }
developerc14d83a2023-06-29 20:09:42 +080012138 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
12139 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012140 }
developera3511852023-06-14 14:12:59 +080012141 }
developer33f13ba2023-07-12 16:19:06 +080012142 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012143 }
developer72fb0bb2023-01-11 09:46:29 +080012144
developera3511852023-06-14 14:12:59 +080012145 //LastDataDownlinkRate
developer33f13ba2023-07-12 16:19:06 +080012146 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Send.txt");
developera3511852023-06-14 14:12:59 +080012147 if (fp)
12148 {
developer33f13ba2023-07-12 16:19:06 +080012149 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012150 }
developer33f13ba2023-07-12 16:19:06 +080012151 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012152 if (fp)
developer5b23cd02023-07-19 20:26:03 +080012153 {
developerc14d83a2023-06-29 20:09:42 +080012154 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012155 for (count = 0; count < wifi_count; count++)
12156 {
developer86035662023-06-28 19:21:12 +080012157 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12158 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012159 goto err;
12160 }
developer5b23cd02023-07-19 20:26:03 +080012161
developerc14d83a2023-06-29 20:09:42 +080012162 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12163 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012164 }
developerc14d83a2023-06-29 20:09:42 +080012165 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080012166 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
12167 }
developer33f13ba2023-07-12 16:19:06 +080012168 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012169 }
developer72fb0bb2023-01-11 09:46:29 +080012170
developera3511852023-06-14 14:12:59 +080012171 //LastDataUplinkRate
developer33f13ba2023-07-12 16:19:06 +080012172 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep 'rx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Received.txt");
developera3511852023-06-14 14:12:59 +080012173 if (fp)
12174 {
developer33f13ba2023-07-12 16:19:06 +080012175 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012176 }
developer33f13ba2023-07-12 16:19:06 +080012177 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012178 if (fp)
12179 {
developerc14d83a2023-06-29 20:09:42 +080012180 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012181 for (count = 0; count < wifi_count; count++)
12182 {
developer86035662023-06-28 19:21:12 +080012183 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12184 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012185 goto err;
12186 }
12187 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12188 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012189 }
developerc14d83a2023-06-29 20:09:42 +080012190 temp[count].cli_LastDataUplinkRate = tmp_u;
12191
developera3511852023-06-14 14:12:59 +080012192 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
12193 }
developer33f13ba2023-07-12 16:19:06 +080012194 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012195 }
developer72fb0bb2023-01-11 09:46:29 +080012196
developera3511852023-06-14 14:12:59 +080012197 }
12198 else if ((apIndex == 1) || (apIndex == 5))
12199 {
12200 for (count = 0; count < wifi_count; count++)
12201 {
developer32f2a182023-06-27 19:50:41 +080012202 memcpy(temp[count].cli_OperatingStandard, "a", 1);
12203 temp[count].cli_OperatingStandard[1] = '\0';
12204 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
12205 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080012206 temp[count].cli_BytesSent = 0;
12207 temp[count].cli_BytesReceived = 0;
12208 temp[count].cli_LastDataUplinkRate = 0;
12209 temp[count].cli_LastDataDownlinkRate = 0;
12210 temp[count].cli_PacketsSent = 0;
12211 temp[count].cli_PacketsReceived = 0;
12212 temp[count].cli_ErrorsSent = 0;
12213 }
12214 }
developer72fb0bb2023-01-11 09:46:29 +080012215
developera3511852023-06-14 14:12:59 +080012216 for (count = 0; count < wifi_count; count++)
12217 {
12218 temp[count].cli_Retransmissions = 0;
12219 temp[count].cli_DataFramesSentAck = 0;
12220 temp[count].cli_DataFramesSentNoAck = 0;
12221 temp[count].cli_MinRSSI = 0;
12222 temp[count].cli_MaxRSSI = 0;
12223 strncpy(temp[count].cli_InterferenceSources, "", 64);
12224 memset(temp[count].cli_IPAddress, 0, 64);
12225 temp[count].cli_RetransCount = 0;
12226 temp[count].cli_FailedRetransCount = 0;
12227 temp[count].cli_RetryCount = 0;
12228 temp[count].cli_MultipleRetryCount = 0;
12229 }
12230 *associated_dev_array = temp;
12231 }
12232 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12233 return RETURN_OK;
developerc14d83a2023-06-29 20:09:42 +080012234err:
12235 if (temp)
12236 free(temp);
developer12fb9f62023-06-30 15:26:27 +080012237 pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080012238 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012239}
12240
developer7e4a2a62023-04-06 19:56:03 +080012241int wifihal_interfacestatus(CHAR *wifi_status, CHAR *interface_name)
developer72fb0bb2023-01-11 09:46:29 +080012242{
developera3511852023-06-14 14:12:59 +080012243 char cmd[MAX_CMD_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +080012244 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080012245 int res;
developer32f2a182023-06-27 19:50:41 +080012246 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012247
developera3511852023-06-14 14:12:59 +080012248 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080012249
developere40952c2023-06-15 18:46:43 +080012250 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s | grep RUNNING | tr -s ' ' | cut -d ' ' -f4 | tr -d '\\n'",
developer7e4a2a62023-04-06 19:56:03 +080012251 interface_name);
developere40952c2023-06-15 18:46:43 +080012252 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
12253 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12254 return RETURN_ERR;
12255 }
12256
developera3511852023-06-14 14:12:59 +080012257 _syscmd(cmd, buf, MAX_BUF_SIZE);
developer7e4a2a62023-04-06 19:56:03 +080012258
developer32f2a182023-06-27 19:50:41 +080012259 len = strlen(buf);
12260 if (len >= sizeof(buf)) {
12261 wifi_debug(DEBUG_ERROR, "Unexpected buf size\n");
12262 return RETURN_ERR;
12263 }
12264 strncpy(wifi_status, buf, len); /* TBD: check wifi_status mem lenth and replace with strcpy later */
12265 wifi_status[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012266
developera3511852023-06-14 14:12:59 +080012267 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12268 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012269}
12270
developer72fb0bb2023-01-11 09:46:29 +080012271static const char *get_line_from_str_buf(const char *buf, char *line)
12272{
developera3511852023-06-14 14:12:59 +080012273 int i;
12274 int n = strlen(buf);
developer72fb0bb2023-01-11 09:46:29 +080012275
developera3511852023-06-14 14:12:59 +080012276 for (i = 0; i < n; i++) {
12277 line[i] = buf[i];
12278 if (buf[i] == '\n') {
12279 line[i] = '\0';
12280 return &buf[i + 1];
12281 }
12282 }
developer72fb0bb2023-01-11 09:46:29 +080012283
developera3511852023-06-14 14:12:59 +080012284 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080012285}
12286
12287INT wifi_getApAssociatedDeviceDiagnosticResult3(INT apIndex, wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12288{
developera3511852023-06-14 14:12:59 +080012289 char interface_name[16] = {0};
12290 FILE *f = NULL;
12291 int auth_temp= -1;
12292 char cmd[256] = {0}, buf[2048] = {0};
12293 char *param = NULL, *value = NULL, *line=NULL;
12294 size_t len = 0;
12295 wifi_associated_dev3_t *dev=NULL;
developer75bd10c2023-06-27 11:34:08 +080012296 int res;
developer72fb0bb2023-01-11 09:46:29 +080012297
developera3511852023-06-14 14:12:59 +080012298 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12299 *associated_dev_array = NULL;
12300 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12301 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080012302
12303 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
12304 if (os_snprintf_error(sizeof(cmd), res)) {
12305 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12306 return RETURN_ERR;
12307 }
12308
developera3511852023-06-14 14:12:59 +080012309 _syscmd(cmd, buf, sizeof(buf));
12310 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080012311
developera3511852023-06-14 14:12:59 +080012312 if (*output_array_size <= 0)
12313 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012314
developera3511852023-06-14 14:12:59 +080012315 dev=(wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
developer86035662023-06-28 19:21:12 +080012316
12317 if (dev == NULL) {
12318 wifi_debug(DEBUG_ERROR, "calloc fail\n");
12319 return RETURN_ERR;
12320 }
developera3511852023-06-14 14:12:59 +080012321 *associated_dev_array = dev;
developer32f2a182023-06-27 19:50:41 +080012322 res = snprintf(cmd, sizeof(cmd),
12323 "hostapd_cli -i%s all_sta > /tmp/diagnostic3_devices.txt" , interface_name);
developer75bd10c2023-06-27 11:34:08 +080012324 if (os_snprintf_error(sizeof(cmd), res)) {
12325 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12326 return RETURN_ERR;
12327 }
12328
developera3511852023-06-14 14:12:59 +080012329 _syscmd(cmd,buf,sizeof(buf));
12330 f = fopen("/tmp/diagnostic3_devices.txt", "r");
12331 if (f == NULL)
12332 {
12333 *output_array_size=0;
12334 return RETURN_ERR;
12335 }
12336 while ((getline(&line, &len, f)) != -1)
12337 {
12338 param = strtok(line, "=");
developere75ba632023-06-29 16:03:33 +080012339 if (!param)
12340 continue;
developera3511852023-06-14 14:12:59 +080012341 value = strtok(NULL, "=");
developere75ba632023-06-29 16:03:33 +080012342 if (!value)
12343 continue;
developer72fb0bb2023-01-11 09:46:29 +080012344
developera3511852023-06-14 14:12:59 +080012345 if( strcmp("flags",param) == 0 )
12346 {
12347 value[strlen(value)-1]='\0';
12348 if(strstr (value,"AUTHORIZED") != NULL )
12349 {
12350 auth_temp++;
12351 dev[auth_temp].cli_AuthenticationState = 1;
12352 dev[auth_temp].cli_Active = 1;
12353 }
12354 } else if (auth_temp < 0) {
12355 continue;
12356 } else if( strcmp("dot11RSNAStatsSTAAddress", param) == 0 )
12357 {
12358 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012359 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
12360 (unsigned int *)&dev[auth_temp].cli_MACAddress[0],
12361 (unsigned int *)&dev[auth_temp].cli_MACAddress[1],
12362 (unsigned int *)&dev[auth_temp].cli_MACAddress[2],
12363 (unsigned int *)&dev[auth_temp].cli_MACAddress[3],
12364 (unsigned int *)&dev[auth_temp].cli_MACAddress[4],
12365 (unsigned int *)&dev[auth_temp].cli_MACAddress[5]) == EOF)
12366 continue;
developera3511852023-06-14 14:12:59 +080012367 } else if (strcmp("signal", param) == 0) {
12368 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012369 if (sscanf(value, "%d", &dev[auth_temp].cli_RSSI) == EOF)
12370 continue;
developera3511852023-06-14 14:12:59 +080012371 dev[auth_temp].cli_SNR = 95 + dev[auth_temp].cli_RSSI;
12372 }
12373 }
developer0d26f2c2023-05-25 19:46:36 +080012374 if (line)
developera3511852023-06-14 14:12:59 +080012375 free(line);
developerc14d83a2023-06-29 20:09:42 +080012376
12377 if (fclose(f) != 0) {
12378 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12379 }
12380
developera3511852023-06-14 14:12:59 +080012381 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12382 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012383}
developer72fb0bb2023-01-11 09:46:29 +080012384
12385/* getIPAddress function */
12386/**
12387* @description Returning IpAddress of the Matched String
12388*
developer69b61b02023-03-07 17:17:44 +080012389* @param
developer72fb0bb2023-01-11 09:46:29 +080012390* @str Having MacAddress
developer69b61b02023-03-07 17:17:44 +080012391* @ipaddr Having ipaddr
developer72fb0bb2023-01-11 09:46:29 +080012392* @return The status of the operation
12393* @retval RETURN_OK if successful
12394* @retval RETURN_ERR if any error is detected
12395*
12396*/
12397
12398INT getIPAddress(char *str,char *ipaddr)
12399{
developera3511852023-06-14 14:12:59 +080012400 FILE *fp = NULL;
12401 char buf[1024] = {0},ipAddr[50] = {0},phyAddr[100] = {0},hostName[100] = {0};
12402 int LeaseTime = 0,ret = 0;
developer32f2a182023-06-27 19:50:41 +080012403 unsigned long len;
12404
developera3511852023-06-14 14:12:59 +080012405 if ( (fp=fopen("/nvram/dnsmasq.leases", "r")) == NULL )
12406 {
12407 return RETURN_ERR;
12408 }
developer72fb0bb2023-01-11 09:46:29 +080012409
developera3511852023-06-14 14:12:59 +080012410 while ( fgets(buf, sizeof(buf), fp)!= NULL )
12411 {
12412 /*
12413 Sample:sss
12414 1560336751 00:cd:fe:f3:25:e6 10.0.0.153 NallamousiPhone 01:00:cd:fe:f3:25:e6
12415 1560336751 12:34:56:78:9a:bc 10.0.0.154 NallamousiPhone 01:00:cd:fe:f3:25:e6
12416 */
12417 ret = sscanf(buf, LM_DHCP_CLIENT_FORMAT,
12418 &(LeaseTime),
12419 phyAddr,
12420 ipAddr,
12421 hostName
12422 );
12423 if(ret != 4)
12424 continue;
developer32f2a182023-06-27 19:50:41 +080012425 if (strcmp(str,phyAddr) == 0) {
12426 len = strlen(ipAddr);
12427 strncpy(ipaddr, ipAddr, len);
12428 ipaddr[len] = '\0';
12429 }
developera3511852023-06-14 14:12:59 +080012430 }
developer37646972023-06-29 10:58:43 +080012431 if (fclose(fp) == EOF) {
12432 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12433 return RETURN_ERR;
12434 }
developera3511852023-06-14 14:12:59 +080012435 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012436}
12437
12438/* wifi_getApInactiveAssociatedDeviceDiagnosticResult function */
12439/**
12440* @description Returning Inactive wireless connected clients informations
12441*
developer69b61b02023-03-07 17:17:44 +080012442* @param
developer72fb0bb2023-01-11 09:46:29 +080012443* @filename Holding private_wifi 2g/5g content files
12444* @associated_dev_array Having inactiv wireless clients informations
12445* @output_array_size Returning Inactive wireless counts
12446* @return The status of the operation
12447* @retval RETURN_OK if successful
12448* @retval RETURN_ERR if any error is detected
12449*
12450*/
12451
12452INT wifi_getApInactiveAssociatedDeviceDiagnosticResult(char *filename,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12453{
developera3511852023-06-14 14:12:59 +080012454 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12455 int count = 0,maccount = 0,i = 0,wificlientindex = 0;
12456 FILE *fp = NULL;
12457 int arr[MACADDRESS_SIZE] = {0};
12458 unsigned char mac[MACADDRESS_SIZE] = {0};
12459 char path[1024] = {0},str[1024] = {0},ipaddr[50] = {0},buf[512] = {0};
developer86035662023-06-28 19:21:12 +080012460 int res;
12461
12462 res = snprintf(buf, sizeof(buf), "cat %s | grep Station | sort | uniq | wc -l",filename);
12463 if (os_snprintf_error(sizeof(buf), res)) {
12464 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12465 return RETURN_ERR;
12466 }
developera3511852023-06-14 14:12:59 +080012467 fp = popen(buf,"r");
12468 if(fp == NULL)
12469 return RETURN_ERR;
12470 else
12471 {
developerd14dff12023-06-28 22:47:44 +080012472 if (fgets(path,sizeof(path),fp) == NULL) {
12473 wifi_debug(DEBUG_ERROR, "fgets fail\n");
12474 pclose(fp);
12475 return RETURN_ERR;
12476 }
developera3511852023-06-14 14:12:59 +080012477 maccount = atoi(path);
12478 }
12479 pclose(fp);
12480 *output_array_size = maccount;
12481 wifi_associated_dev3_t* temp = NULL;
developer9ce44382023-06-28 11:09:37 +080012482 if(*output_array_size > 0 && *output_array_size < MAX_ASSOCIATED_STA_NUM){
12483 temp = (wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
12484 } else {
12485 return RETURN_ERR;
12486 }
developer37646972023-06-29 10:58:43 +080012487
developera3511852023-06-14 14:12:59 +080012488 *associated_dev_array = temp;
12489 if(temp == NULL)
12490 {
12491 printf("Error Statement. Insufficient memory \n");
12492 return RETURN_ERR;
12493 }
12494 memset(buf,0,sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080012495 res = snprintf(buf, sizeof(buf),
12496 "cat %s | grep Station | cut -d ' ' -f2 | sort | uniq",filename);
12497 if (os_snprintf_error(sizeof(buf), res)) {
12498 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12499 return RETURN_ERR;
12500 }
developera3511852023-06-14 14:12:59 +080012501 fp = popen(buf,"r");
12502 if (fp == NULL) {
developer37646972023-06-29 10:58:43 +080012503 res = fprintf(stderr, "%s: failed pipe command %s.\n", __func__, buf);
12504 if (res < 0) {
12505 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12506 }
developera3511852023-06-14 14:12:59 +080012507 return RETURN_ERR;
12508 }
12509 for(count = 0; count < maccount ; count++)
12510 {
developer37646972023-06-29 10:58:43 +080012511 if (fgets(path,sizeof(path),fp) == NULL)
12512 continue;
developera3511852023-06-14 14:12:59 +080012513 for(i = 0; path[i]!='\n';i++)
12514 str[i]=path[i];
12515 str[i]='\0';
12516 getIPAddress(str,ipaddr);
developera3511852023-06-14 14:12:59 +080012517 if(strlen(ipaddr) > 0)
12518 {
developer33f13ba2023-07-12 16:19:06 +080012519 if (v_secure_system("ping -q -c 1 -W 1 \"%s\" > /dev/null 2>&1", ipaddr)) //InActive wireless clients info
developera3511852023-06-14 14:12:59 +080012520 {
12521 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12522 {
12523 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12524 {
12525 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012526
developera3511852023-06-14 14:12:59 +080012527 }
12528 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer37646972023-06-29 10:58:43 +080012529 if (fprintf(stderr,"%sMAC %d = %X:%X:%X:%X:%X:%X \n", __FUNCTION__,count, temp[count].cli_MACAddress[0],
12530 temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2],
12531 temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4],
12532 temp[count].cli_MACAddress[5]) < 0) {
12533 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12534 break;
12535 }
developera3511852023-06-14 14:12:59 +080012536 }
12537 temp[count].cli_AuthenticationState = 0; //TODO
12538 temp[count].cli_Active = 0; //TODO
12539 temp[count].cli_SignalStrength = 0;
12540 }
12541 else //Active wireless clients info
12542 {
12543 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12544 {
12545 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12546 {
12547 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012548
developera3511852023-06-14 14:12:59 +080012549 }
12550 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer12fb9f62023-06-30 15:26:27 +080012551 wifi_debug(DEBUG_ERROR, "%sMAC %d = %X:%X:%X:%X:%X:%X \n", __FUNCTION__,count, temp[count].cli_MACAddress[0],temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2], temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4], temp[count].cli_MACAddress[5]);
developera3511852023-06-14 14:12:59 +080012552 }
12553 temp[count].cli_Active = 1;
12554 }
12555 }
12556 memset(ipaddr,0,sizeof(ipaddr));
12557 }
12558 pclose(fp);
12559 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12560 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012561}
12562//Device.WiFi.X_RDKCENTRAL-COM_BandSteering object
12563//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Capability bool r/o
12564//To get Band Steering Capability
12565INT wifi_getBandSteeringCapability(BOOL *support)
12566{
developera3511852023-06-14 14:12:59 +080012567 *support = FALSE;
12568 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012569}
12570
12571
12572//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Enable bool r/w
12573//To get Band Steering enable status
12574INT wifi_getBandSteeringEnable(BOOL *enable)
12575{
developera3511852023-06-14 14:12:59 +080012576 *enable = FALSE;
12577 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012578}
12579
12580//To turn on/off Band steering
12581INT wifi_setBandSteeringEnable(BOOL enable)
12582{
developera3511852023-06-14 14:12:59 +080012583 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012584}
12585
12586//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.APGroup string r/w
12587//To get Band Steering AP group
12588INT wifi_getBandSteeringApGroup(char *output_ApGroup)
12589{
developera3511852023-06-14 14:12:59 +080012590 if (NULL == output_ApGroup)
12591 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012592
developer32f2a182023-06-27 19:50:41 +080012593 memcpy(output_ApGroup, "1,2", 3);
12594 output_ApGroup[3] = '\0';
developera3511852023-06-14 14:12:59 +080012595 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012596}
12597
12598//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.UtilizationThreshold int r/w
12599//to set and read the band steering BandUtilizationThreshold parameters
12600INT wifi_getBandSteeringBandUtilizationThreshold (INT radioIndex, INT *pBuThreshold)
12601{
developera3511852023-06-14 14:12:59 +080012602 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012603}
12604
12605INT wifi_setBandSteeringBandUtilizationThreshold (INT radioIndex, INT buThreshold)
12606{
developera3511852023-06-14 14:12:59 +080012607 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012608}
12609
12610//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.RSSIThreshold int r/w
12611//to set and read the band steering RSSIThreshold parameters
12612INT wifi_getBandSteeringRSSIThreshold (INT radioIndex, INT *pRssiThreshold)
12613{
developera3511852023-06-14 14:12:59 +080012614 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012615}
12616
12617INT wifi_setBandSteeringRSSIThreshold (INT radioIndex, INT rssiThreshold)
12618{
developera3511852023-06-14 14:12:59 +080012619 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012620}
12621
12622
12623//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.PhyRateThreshold int r/w
12624//to set and read the band steering physical modulation rate threshold parameters
12625INT wifi_getBandSteeringPhyRateThreshold (INT radioIndex, INT *pPrThreshold)
12626{
developera3511852023-06-14 14:12:59 +080012627 //If chip is not support, return -1
12628 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012629}
12630
12631INT wifi_setBandSteeringPhyRateThreshold (INT radioIndex, INT prThreshold)
12632{
developera3511852023-06-14 14:12:59 +080012633 //If chip is not support, return -1
12634 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012635}
12636
12637//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.OverloadInactiveTime int r/w
12638//to set and read the inactivity time (in seconds) for steering under overload condition
12639INT wifi_getBandSteeringOverloadInactiveTime(INT radioIndex, INT *pPrThreshold)
12640{
developera3511852023-06-14 14:12:59 +080012641 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012642}
12643
12644INT wifi_setBandSteeringOverloadInactiveTime(INT radioIndex, INT prThreshold)
12645{
developera3511852023-06-14 14:12:59 +080012646 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012647}
12648
12649//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.IdleInactiveTime int r/w
12650//to set and read the inactivity time (in seconds) for steering under Idle condition
12651INT wifi_getBandSteeringIdleInactiveTime(INT radioIndex, INT *pPrThreshold)
12652{
developera3511852023-06-14 14:12:59 +080012653 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012654}
12655
12656INT wifi_setBandSteeringIdleInactiveTime(INT radioIndex, INT prThreshold)
12657{
developera3511852023-06-14 14:12:59 +080012658 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012659}
12660
12661//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.History string r/o
12662//pClientMAC[64]
12663//pSourceSSIDIndex[64]
12664//pDestSSIDIndex[64]
12665//pSteeringReason[256]
12666INT wifi_getBandSteeringLog(INT record_index, ULONG *pSteeringTime, CHAR *pClientMAC, INT *pSourceSSIDIndex, INT *pDestSSIDIndex, INT *pSteeringReason)
12667{
developera3511852023-06-14 14:12:59 +080012668 //if no steering or redord_index is out of boundary, return -1. pSteeringTime returns the UTC time in seconds. pClientMAC is pre allocated as 64bytes. pSteeringReason returns the predefined steering trigger reason
developer12fb9f62023-06-30 15:26:27 +080012669 long int tim_tmp = time(NULL);
12670 if (tim_tmp < 0)
12671 return RETURN_ERR;
12672 *pSteeringTime = tim_tmp;
developera3511852023-06-14 14:12:59 +080012673 *pSteeringReason = 0; //TODO: need to assign correct steering reason (INT numeric, i suppose)
12674 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012675}
12676
12677INT wifi_ifConfigDown(INT apIndex)
12678{
developera3511852023-06-14 14:12:59 +080012679 INT status = RETURN_OK;
12680 char cmd[64];
developere40952c2023-06-15 18:46:43 +080012681 int res;
developer72fb0bb2023-01-11 09:46:29 +080012682
developer33f13ba2023-07-12 16:19:06 +080012683 res = v_secure_system("ifconfig ath%d down", apIndex);
12684 if (res) {
12685 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080012686 return RETURN_ERR;
12687 }
developera3511852023-06-14 14:12:59 +080012688 printf("%s: %s\n", __func__, cmd);
developer72fb0bb2023-01-11 09:46:29 +080012689
developera3511852023-06-14 14:12:59 +080012690 return status;
developer72fb0bb2023-01-11 09:46:29 +080012691}
12692
12693INT wifi_ifConfigUp(INT apIndex)
12694{
developera3511852023-06-14 14:12:59 +080012695 char interface_name[16] = {0};
12696 char cmd[128];
12697 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012698 int res;
developer72fb0bb2023-01-11 09:46:29 +080012699
developera3511852023-06-14 14:12:59 +080012700 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12701 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080012702 res = snprintf(cmd, sizeof(cmd), "ifconfig %s up 2>/dev/null", interface_name);
12703 if (os_snprintf_error(sizeof(cmd), res)) {
12704 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12705 return RETURN_ERR;
12706 }
developera3511852023-06-14 14:12:59 +080012707 _syscmd(cmd, buf, sizeof(buf));
12708 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012709}
12710
12711//>> Deprecated. Replace with wifi_applyRadioSettings
12712INT wifi_pushBridgeInfo(INT apIndex)
12713{
developerb2977562023-05-24 17:54:12 +080012714 char ip[32] = {0};
12715 char subnet[32] = {0};
12716 char bridge[32] = {0};
12717 char cmd[128] = {0};
12718 char buf[1024] = {0};
developere40952c2023-06-15 18:46:43 +080012719 int res;
developer72fb0bb2023-01-11 09:46:29 +080012720
developerb2977562023-05-24 17:54:12 +080012721 wifi_getApBridgeInfo(apIndex, bridge, ip, subnet);
developer72fb0bb2023-01-11 09:46:29 +080012722
developere40952c2023-06-15 18:46:43 +080012723 res = snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s ", bridge, ip, subnet);
12724 if (os_snprintf_error(sizeof(cmd), res)) {
12725 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12726 return RETURN_ERR;
12727 }
developerb2977562023-05-24 17:54:12 +080012728 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080012729
developerb2977562023-05-24 17:54:12 +080012730 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012731}
12732
12733INT wifi_pushChannel(INT radioIndex, UINT channel)
12734{
developera3511852023-06-14 14:12:59 +080012735 char interface_name[16] = {0};
12736 char cmd[128];
12737 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012738 int res;
developer72fb0bb2023-01-11 09:46:29 +080012739
developera3511852023-06-14 14:12:59 +080012740 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
12741 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080012742 res = snprintf(cmd, sizeof(cmd), "iwconfig %s freq %d",interface_name,channel);
12743 if (os_snprintf_error(sizeof(cmd), res)) {
12744 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12745 return RETURN_ERR;
12746 }
developera3511852023-06-14 14:12:59 +080012747 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080012748
developera3511852023-06-14 14:12:59 +080012749 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012750}
12751
12752INT wifi_pushChannelMode(INT radioIndex)
12753{
developera3511852023-06-14 14:12:59 +080012754 //Apply Channel mode, pure mode, etc that been set by wifi_setRadioChannelMode() instantly
12755 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012756}
12757
12758INT wifi_pushDefaultValues(INT radioIndex)
12759{
developera3511852023-06-14 14:12:59 +080012760 //Apply Comcast specified default radio settings instantly
12761 //AMPDU=1
12762 //AMPDUFrames=32
12763 //AMPDULim=50000
12764 //txqueuelen=1000
developer72fb0bb2023-01-11 09:46:29 +080012765
developera3511852023-06-14 14:12:59 +080012766 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012767}
12768
12769INT wifi_pushTxChainMask(INT radioIndex)
12770{
developera3511852023-06-14 14:12:59 +080012771 //Apply default TxChainMask instantly
12772 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012773}
12774
12775INT wifi_pushRxChainMask(INT radioIndex)
12776{
developera3511852023-06-14 14:12:59 +080012777 //Apply default RxChainMask instantly
12778 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012779}
12780
12781INT wifi_pushSSID(INT apIndex, CHAR *ssid)
12782{
developer7e4a2a62023-04-06 19:56:03 +080012783 INT status;
developer72fb0bb2023-01-11 09:46:29 +080012784
developer7e4a2a62023-04-06 19:56:03 +080012785 status = wifi_setSSIDName(apIndex, ssid);
12786 wifi_quick_reload_ap(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080012787
developer7e4a2a62023-04-06 19:56:03 +080012788 return status;
developer72fb0bb2023-01-11 09:46:29 +080012789}
12790
12791INT wifi_pushSsidAdvertisementEnable(INT apIndex, BOOL enable)
12792{
developera3511852023-06-14 14:12:59 +080012793 int ret;
developerc1aa6532023-06-09 09:37:01 +080012794 ret = wifi_setApSsidAdvertisementEnable(apIndex, enable);
12795
12796 return ret;
developer72fb0bb2023-01-11 09:46:29 +080012797}
12798
12799INT wifi_getRadioUpTime(INT radioIndex, ULONG *output)
12800{
developera3511852023-06-14 14:12:59 +080012801 time_t now;
developere82c0ca2023-05-10 16:25:35 +080012802
developerd14dff12023-06-28 22:47:44 +080012803 now = time(NULL);
12804 if (now < 0) {
12805 wifi_debug(DEBUG_ERROR, "GET time fail\n");
12806 return RETURN_ERR;
12807 }
developere82c0ca2023-05-10 16:25:35 +080012808 if (now > radio_up_time[radioIndex])
12809 *output = now - radio_up_time[radioIndex];
12810 else {
12811 *output = 0;
12812 return RETURN_ERR;
12813 }
12814
developera3511852023-06-14 14:12:59 +080012815 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012816}
12817
12818INT wifi_getApEnableOnLine(INT wlanIndex, BOOL *enabled)
12819{
developera3511852023-06-14 14:12:59 +080012820 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012821}
12822
12823INT wifi_getApSecurityWpaRekeyInterval(INT apIndex, INT *output_int)
12824{
developera3511852023-06-14 14:12:59 +080012825 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012826}
12827
12828//To-do
12829INT wifi_getApSecurityMFPConfig(INT apIndex, CHAR *output_string)
12830{
developera3511852023-06-14 14:12:59 +080012831 char output[16]={'\0'};
12832 char config_file[MAX_BUF_SIZE] = {0};
12833 int res;
developer72fb0bb2023-01-11 09:46:29 +080012834
developera3511852023-06-14 14:12:59 +080012835 if (!output_string)
12836 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012837
developera3511852023-06-14 14:12:59 +080012838 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12839 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012840 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12841 return RETURN_ERR;
12842 }
developera3511852023-06-14 14:12:59 +080012843 wifi_hostapdRead(config_file, "ieee80211w", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012844
developera3511852023-06-14 14:12:59 +080012845 if (strlen(output) == 0)
developere40952c2023-06-15 18:46:43 +080012846 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012847 else if (strncmp(output, "0", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012848 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012849 else if (strncmp(output, "1", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012850 res = snprintf(output_string, 64, "Optional");
developera3511852023-06-14 14:12:59 +080012851 else if (strncmp(output, "2", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012852 res = snprintf(output_string, 64, "Required");
developera3511852023-06-14 14:12:59 +080012853 else {
12854 wifi_dbg_printf("\n[%s]: Unexpected ieee80211w=%s", __func__, output);
12855 return RETURN_ERR;
12856 }
developere40952c2023-06-15 18:46:43 +080012857 if (os_snprintf_error(64, res)) {
12858 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12859 return RETURN_ERR;
12860 }
developer72fb0bb2023-01-11 09:46:29 +080012861
developera3511852023-06-14 14:12:59 +080012862 wifi_dbg_printf("\n[%s]: ieee80211w is : %s", __func__, output);
12863 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012864}
12865INT wifi_setApSecurityMFPConfig(INT apIndex, CHAR *MfpConfig)
12866{
developera3511852023-06-14 14:12:59 +080012867 struct params params;
12868 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080012869 int res;
developer72fb0bb2023-01-11 09:46:29 +080012870
developera3511852023-06-14 14:12:59 +080012871 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12872 if(NULL == MfpConfig || strlen(MfpConfig) >= 32 )
12873 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012874
developera3511852023-06-14 14:12:59 +080012875 params.name = "ieee80211w";
12876 if (strncmp(MfpConfig, "Disabled", strlen("Disabled")) == 0)
12877 params.value = "0";
12878 else if (strncmp(MfpConfig, "Optional", strlen("Optional")) == 0)
12879 params.value = "1";
12880 else if (strncmp(MfpConfig, "Required", strlen("Required")) == 0)
12881 params.value = "2";
12882 else{
12883 wifi_dbg_printf("%s: invalid MfpConfig. Input has to be Disabled, Optional or Required \n", __func__);
12884 return RETURN_ERR;
12885 }
developer75bd10c2023-06-27 11:34:08 +080012886
12887 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12888 if (os_snprintf_error(sizeof(config_file), res)) {
12889 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12890 return RETURN_ERR;
12891 }
12892
developera3511852023-06-14 14:12:59 +080012893 wifi_hostapdWrite(config_file, &params, 1);
12894 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12895 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012896}
12897INT wifi_getRadioAutoChannelEnable(INT radioIndex, BOOL *output_bool)
12898{
developera3511852023-06-14 14:12:59 +080012899 char output[16]={'\0'};
12900 char config_file[MAX_BUF_SIZE] = {0};
12901 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080012902 int res;
developer72fb0bb2023-01-11 09:46:29 +080012903
developera3511852023-06-14 14:12:59 +080012904 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12905 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080012906 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
12907 if (os_snprintf_error(sizeof(config_file), res)) {
12908 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12909 return RETURN_ERR;
12910 }
developera3511852023-06-14 14:12:59 +080012911 wifi_datfileRead(config_file, "AutoChannelSelect" , output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012912
developera3511852023-06-14 14:12:59 +080012913 if (strncmp(output, "0", 1) == 0)
12914 *output_bool = FALSE;
12915 else if (strncmp(output, "1", 1) == 0)
12916 *output_bool = TRUE;
12917 else if (strncmp(output, "2", 1) == 0)
12918 *output_bool = TRUE;
12919 else if (strncmp(output, "3", 1) == 0)
12920 *output_bool = TRUE;
12921 else
12922 *output_bool = FALSE;
12923 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012924
developera3511852023-06-14 14:12:59 +080012925 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012926}
12927
12928INT wifi_getRouterEnable(INT wlanIndex, BOOL *enabled)
12929{
developera3511852023-06-14 14:12:59 +080012930 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012931}
12932
12933INT wifi_setApSecurityWpaRekeyInterval(INT apIndex, INT *rekeyInterval)
12934{
developera3511852023-06-14 14:12:59 +080012935 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012936}
12937
12938INT wifi_setRouterEnable(INT wlanIndex, INT *RouterEnabled)
12939{
developera3511852023-06-14 14:12:59 +080012940 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012941}
12942
12943INT wifi_getRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
12944{
developera3511852023-06-14 14:12:59 +080012945 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12946 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080012947 char tmp_output[MAX_BUF_SIZE] = {0};
developera3511852023-06-14 14:12:59 +080012948 int res;
developer72fb0bb2023-01-11 09:46:29 +080012949
developera3511852023-06-14 14:12:59 +080012950 if (NULL == output)
12951 return RETURN_ERR;
12952 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12953 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012954 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12955 return RETURN_ERR;
12956 }
developera3511852023-06-14 14:12:59 +080012957 wifi_hostapdRead(config_file,"hw_mode",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012958
developer32f2a182023-06-27 19:50:41 +080012959 if(strcmp(output,"b")==0) {
12960 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,11");
12961 if (os_snprintf_error(sizeof(tmp_output), res)) {
12962 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12963 return RETURN_ERR;
12964 }
12965 } else if (strcmp(output,"a")==0) {
12966 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "6,9,11,12,18,24,36,48,54");
12967 if (os_snprintf_error(sizeof(tmp_output), res)) {
12968 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12969 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080012970 }
developer32f2a182023-06-27 19:50:41 +080012971 } else if ((strcmp(output,"n")==0) | (strcmp(output,"g")==0)) {
12972 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,6,9,11,12,18,24,36,48,54");
12973 if (os_snprintf_error(sizeof(tmp_output), res)) {
12974 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12975 return RETURN_ERR;
12976 }
developer75bd10c2023-06-27 11:34:08 +080012977 }
developer32f2a182023-06-27 19:50:41 +080012978 memcpy(output, tmp_output, strlen(tmp_output));
12979 output[strlen(tmp_output)] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012980
developera3511852023-06-14 14:12:59 +080012981 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12982 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012983}
12984
12985INT wifi_getRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
12986{
developera3511852023-06-14 14:12:59 +080012987 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12988 char *temp;
developer32f2a182023-06-27 19:50:41 +080012989 char temp_output[128] = {0};
12990 char temp_TransmitRates[128] = {0};
developera3511852023-06-14 14:12:59 +080012991 char config_file[MAX_BUF_SIZE] = {0};
12992 int res;
developer32f2a182023-06-27 19:50:41 +080012993 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012994
developera3511852023-06-14 14:12:59 +080012995 if (NULL == output)
12996 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012997
developera3511852023-06-14 14:12:59 +080012998 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12999 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080013000 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13001 return RETURN_ERR;
13002 }
developera3511852023-06-14 14:12:59 +080013003 wifi_hostapdRead(config_file,"supported_rates",output,64);
developer72fb0bb2023-01-11 09:46:29 +080013004
developera3511852023-06-14 14:12:59 +080013005 if (strlen(output) == 0) {
13006 wifi_getRadioSupportedDataTransmitRates(wlanIndex, output);
13007 return RETURN_OK;
13008 }
developer32f2a182023-06-27 19:50:41 +080013009 len = strlen(output);
13010 if (len >= sizeof(temp_TransmitRates)) {
13011 wifi_debug(DEBUG_ERROR, "Unexpected strlen(output)\n");
13012 return RETURN_ERR;
13013 }
13014 strncpy(temp_TransmitRates, output, len);
developera3511852023-06-14 14:12:59 +080013015 temp = strtok(temp_TransmitRates," ");
13016 while(temp!=NULL)
13017 {
13018 temp[strlen(temp)-1]=0;
13019 if((temp[0]=='5') && (temp[1]=='\0'))
13020 {
13021 temp="5.5";
13022 }
developer32f2a182023-06-27 19:50:41 +080013023 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp)) {
13024 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13025 return RETURN_ERR;
13026 }
13027 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013028 temp = strtok(NULL," ");
13029 if(temp!=NULL)
13030 {
developer32f2a182023-06-27 19:50:41 +080013031 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
13032 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13033 return RETURN_ERR;
13034 }
13035 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013036 }
13037 }
developer32f2a182023-06-27 19:50:41 +080013038 len = strlen(temp_output);
13039 strncpy(output, temp_output, len);
13040 output[len] = '\0';
developera3511852023-06-14 14:12:59 +080013041 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013042
developera3511852023-06-14 14:12:59 +080013043 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013044}
13045
13046INT wifi_setRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
13047{
developera3511852023-06-14 14:12:59 +080013048 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013049}
13050
13051
13052INT wifi_setRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
13053{
developera3511852023-06-14 14:12:59 +080013054 int i=0;
13055 char *temp;
13056 char temp1[128] = {0};
13057 char temp_output[128] = {0};
13058 char temp_TransmitRates[128] = {0};
13059 struct params params={'\0'};
13060 char config_file[MAX_BUF_SIZE] = {0};
13061 wifi_band band = wifi_index_to_band(wlanIndex);
developer32f2a182023-06-27 19:50:41 +080013062 unsigned long len;
13063 int res;
developer72fb0bb2023-01-11 09:46:29 +080013064
developera3511852023-06-14 14:12:59 +080013065 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
13066 if(NULL == output)
13067 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080013068
13069 len = strlen(output);
13070 if (len >= sizeof(temp_TransmitRates)) {
13071 wifi_debug(DEBUG_ERROR, "not enough room in temp_TransmitRates\n");
13072 return RETURN_ERR;
13073 }
13074 strncpy(temp_TransmitRates, output, len);
13075 temp_TransmitRates[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080013076
developera3511852023-06-14 14:12:59 +080013077 for(i=0;i<strlen(temp_TransmitRates);i++)
13078 {
13079 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
13080 {
13081 continue;
13082 }
13083 else
13084 {
13085 return RETURN_ERR;
13086 }
13087 }
developera3511852023-06-14 14:12:59 +080013088 temp = strtok(temp_TransmitRates,",");
13089 while(temp!=NULL)
13090 {
developer32f2a182023-06-27 19:50:41 +080013091 len = strlen(temp);
13092 if (len >= sizeof(temp1)) {
13093 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13094 return RETURN_ERR;
13095 }
13096 strncpy(temp1, temp, len);
13097 temp1[len] = '\0';
developera3511852023-06-14 14:12:59 +080013098 if(band == band_5)
13099 {
13100 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
13101 {
13102 return RETURN_ERR;
13103 }
13104 }
developer72fb0bb2023-01-11 09:46:29 +080013105
developer32f2a182023-06-27 19:50:41 +080013106 if(strcmp(temp,"5.5")==0) {
13107 strncpy(temp1, "55", 2);
13108 temp1[2] = '\0';
13109 } else {
13110 if ((sizeof(temp1) - strlen(temp1)) <= 1) {
13111 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13112 return RETURN_ERR;
13113 }
13114 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +080013115 }
developer5b23cd02023-07-19 20:26:03 +080013116
developer32f2a182023-06-27 19:50:41 +080013117 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp1)) {
13118 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13119 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080013120 }
developer32f2a182023-06-27 19:50:41 +080013121 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013122 temp = strtok(NULL,",");
13123 if(temp!=NULL)
13124 {
developer32f2a182023-06-27 19:50:41 +080013125 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
13126 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13127 return RETURN_ERR;
13128 }
13129 strncat(temp_output, " ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013130 }
13131 }
developer32f2a182023-06-27 19:50:41 +080013132 len = strlen(temp_output);
13133 strncpy(output, temp_output, len);
13134 output[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080013135
developera3511852023-06-14 14:12:59 +080013136 params.name = "supported_rates";
13137 params.value = output;
developer72fb0bb2023-01-11 09:46:29 +080013138
developera3511852023-06-14 14:12:59 +080013139 wifi_dbg_printf("\n%s:",__func__);
13140 wifi_dbg_printf("params.value=%s\n",params.value);
developer32f2a182023-06-27 19:50:41 +080013141 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
13142 if (os_snprintf_error(sizeof(config_file), res)) {
13143 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13144 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013145 }
developera3511852023-06-14 14:12:59 +080013146 wifi_hostapdWrite(config_file,&params,1);
13147 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013148
developera3511852023-06-14 14:12:59 +080013149 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013150}
13151
13152
13153static char *sncopy(char *dst, int dst_sz, const char *src)
13154{
developera3511852023-06-14 14:12:59 +080013155 if (src && dst && dst_sz > 0) {
13156 strncpy(dst, src, dst_sz);
13157 dst[dst_sz - 1] = '\0';
13158 }
13159 return dst;
developer72fb0bb2023-01-11 09:46:29 +080013160}
13161
13162static int util_get_sec_chan_offset(int channel, const char* ht_mode)
13163{
developera3511852023-06-14 14:12:59 +080013164 if (0 == strcmp(ht_mode, "HT40") ||
13165 0 == strcmp(ht_mode, "HT80") ||
13166 0 == strcmp(ht_mode, "HT160")) {
13167 switch (channel) {
13168 case 1 ... 7:
13169 case 36:
13170 case 44:
13171 case 52:
13172 case 60:
13173 case 100:
13174 case 108:
13175 case 116:
13176 case 124:
13177 case 132:
13178 case 140:
13179 case 149:
13180 case 157:
13181 return 1;
13182 case 8 ... 13:
13183 case 40:
13184 case 48:
13185 case 56:
13186 case 64:
13187 case 104:
13188 case 112:
13189 case 120:
13190 case 128:
13191 case 136:
13192 case 144:
13193 case 153:
13194 case 161:
13195 return -1;
13196 default:
13197 return -EINVAL;
13198 }
13199 }
developer72fb0bb2023-01-11 09:46:29 +080013200
developera3511852023-06-14 14:12:59 +080013201 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013202}
13203
13204static int util_get_6g_sec_chan_offset(int channel, const char* ht_mode)
13205{
developera3511852023-06-14 14:12:59 +080013206 int idx = channel%8;
13207 if (0 == strcmp(ht_mode, "HT40") ||
13208 0 == strcmp(ht_mode, "HT80") ||
13209 0 == strcmp(ht_mode, "HT160")) {
13210 switch (idx) {
13211 case 1:
13212 return 1;
13213 case 5:
13214 return -1;
13215 default:
13216 return -EINVAL;
13217 }
13218 }
developer72fb0bb2023-01-11 09:46:29 +080013219
developera3511852023-06-14 14:12:59 +080013220 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013221}
13222static void util_hw_mode_to_bw_mode(const char* hw_mode, char *bw_mode, int bw_mode_len)
13223{
developera3511852023-06-14 14:12:59 +080013224 if (NULL == hw_mode) return;
developer72fb0bb2023-01-11 09:46:29 +080013225
developera3511852023-06-14 14:12:59 +080013226 if (0 == strcmp(hw_mode, "ac"))
13227 sncopy(bw_mode, bw_mode_len, "ht vht");
developer72fb0bb2023-01-11 09:46:29 +080013228
developera3511852023-06-14 14:12:59 +080013229 if (0 == strcmp(hw_mode, "n"))
13230 sncopy(bw_mode, bw_mode_len, "ht");
developer72fb0bb2023-01-11 09:46:29 +080013231
developera3511852023-06-14 14:12:59 +080013232 return;
developer72fb0bb2023-01-11 09:46:29 +080013233}
13234
13235static int util_chan_to_freq(int chan)
13236{
developera3511852023-06-14 14:12:59 +080013237 if (chan == 14)
13238 return 2484;
13239 else if (chan < 14)
13240 return 2407 + chan * 5;
13241 else if (chan >= 182 && chan <= 196)
13242 return 4000 + chan * 5;
13243 else
13244 return 5000 + chan * 5;
13245 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013246}
13247
13248static int util_6G_chan_to_freq(int chan)
13249{
developera3511852023-06-14 14:12:59 +080013250 if (chan)
13251 return 5950 + chan * 5;
13252 else
13253 return 0;
developer69b61b02023-03-07 17:17:44 +080013254
developer72fb0bb2023-01-11 09:46:29 +080013255}
13256const int *util_unii_5g_chan2list(int chan, int width)
13257{
developera3511852023-06-14 14:12:59 +080013258 static const int lists[] = {
13259 // <width>, <chan1>, <chan2>..., 0,
13260 20, 36, 0,
13261 20, 40, 0,
13262 20, 44, 0,
13263 20, 48, 0,
13264 20, 52, 0,
13265 20, 56, 0,
13266 20, 60, 0,
13267 20, 64, 0,
13268 20, 100, 0,
13269 20, 104, 0,
13270 20, 108, 0,
13271 20, 112, 0,
13272 20, 116, 0,
13273 20, 120, 0,
13274 20, 124, 0,
13275 20, 128, 0,
13276 20, 132, 0,
13277 20, 136, 0,
13278 20, 140, 0,
13279 20, 144, 0,
13280 20, 149, 0,
13281 20, 153, 0,
13282 20, 157, 0,
13283 20, 161, 0,
13284 20, 165, 0,
13285 40, 36, 40, 0,
13286 40, 44, 48, 0,
13287 40, 52, 56, 0,
13288 40, 60, 64, 0,
13289 40, 100, 104, 0,
13290 40, 108, 112, 0,
13291 40, 116, 120, 0,
13292 40, 124, 128, 0,
13293 40, 132, 136, 0,
13294 40, 140, 144, 0,
13295 40, 149, 153, 0,
13296 40, 157, 161, 0,
13297 80, 36, 40, 44, 48, 0,
13298 80, 52, 56, 60, 64, 0,
13299 80, 100, 104, 108, 112, 0,
13300 80, 116, 120, 124, 128, 0,
13301 80, 132, 136, 140, 144, 0,
13302 80, 149, 153, 157, 161, 0,
13303 160, 36, 40, 44, 48, 52, 56, 60, 64, 0,
13304 160, 100, 104, 108, 112, 116, 120, 124, 128, 0,
13305 -1 // final delimiter
13306 };
13307 const int *start;
13308 const int *p;
developer72fb0bb2023-01-11 09:46:29 +080013309
developera3511852023-06-14 14:12:59 +080013310 for (p = lists; *p != -1; p++) {
13311 if (*p == width) {
13312 for (start = ++p; *p != 0; p++) {
13313 if (*p == chan)
13314 return start;
13315 }
13316 }
13317 // move to the end of channel list of given width
13318 while (*p != 0) {
13319 p++;
13320 }
13321 }
developer72fb0bb2023-01-11 09:46:29 +080013322
developera3511852023-06-14 14:12:59 +080013323 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080013324}
13325
13326static int util_unii_5g_centerfreq(const char *ht_mode, int channel)
13327{
developera3511852023-06-14 14:12:59 +080013328 if (NULL == ht_mode)
13329 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013330
developera3511852023-06-14 14:12:59 +080013331 const int width = atoi(strlen(ht_mode) > 2 ? ht_mode + 2 : "20");
13332 const int *chans = util_unii_5g_chan2list(channel, width);
13333 int sum = 0;
13334 int cnt = 0;
developer72fb0bb2023-01-11 09:46:29 +080013335
developera3511852023-06-14 14:12:59 +080013336 if (NULL == chans)
13337 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013338
developera3511852023-06-14 14:12:59 +080013339 while (*chans) {
13340 sum += *chans;
13341 cnt++;
13342 chans++;
13343 }
13344 if (cnt == 0)
13345 return 0;
13346 return sum / cnt;
developer72fb0bb2023-01-11 09:46:29 +080013347}
13348
13349static int util_unii_6g_centerfreq(const char *ht_mode, int channel)
13350{
developerc14d83a2023-06-29 20:09:42 +080013351 long int width;
developera3511852023-06-14 14:12:59 +080013352 int idx = 0 ;
13353 int centerchan = 0;
13354 int chan_ofs = 1;
developer72fb0bb2023-01-11 09:46:29 +080013355
developerc14d83a2023-06-29 20:09:42 +080013356 if (NULL == ht_mode)
13357 return 0;
13358
13359 if (hal_strtol((char *)(ht_mode + 2), 10, &width) < 0) {
13360 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13361 }
13362
developera3511852023-06-14 14:12:59 +080013363 if (width == 40){
13364 idx = ((channel/4) + chan_ofs)%2;
13365 switch (idx) {
13366 case 0:
13367 centerchan = (channel - 2);
13368 break;
13369 case 1:
13370 centerchan = (channel + 2);
13371 break;
13372 default:
13373 return -EINVAL;
13374 }
13375 }else if (width == 80){
13376 idx = ((channel/4) + chan_ofs)%4;
13377 switch (idx) {
13378 case 0:
13379 centerchan = (channel - 6);
13380 break;
13381 case 1:
13382 centerchan = (channel + 6);
13383 break;
13384 case 2:
13385 centerchan = (channel + 2);
13386 break;
13387 case 3:
13388 centerchan = (channel - 2);
13389 break;
13390 default:
13391 return -EINVAL;
13392 }
13393 }else if (width == 160){
13394 switch (channel) {
13395 case 1 ... 29:
13396 centerchan = 15;
13397 break;
13398 case 33 ... 61:
13399 centerchan = 47;
13400 break;
13401 case 65 ... 93:
13402 centerchan = 79;
13403 break;
13404 case 97 ... 125:
13405 centerchan = 111;
13406 break;
13407 case 129 ... 157:
13408 centerchan = 143;
13409 break;
13410 case 161 ... 189:
13411 centerchan = 175;
13412 break;
13413 case 193 ... 221:
13414 centerchan = 207;
13415 break;
13416 default:
13417 return -EINVAL;
13418 }
13419 }
13420 return centerchan;
developer72fb0bb2023-01-11 09:46:29 +080013421}
13422static int util_radio_get_hw_mode(int radioIndex, char *hw_mode, int hw_mode_size)
13423{
developera3511852023-06-14 14:12:59 +080013424 BOOL onlyG, onlyN, onlyA;
13425 CHAR tmp[64];
13426 int ret = wifi_getRadioStandard(radioIndex, tmp, &onlyG, &onlyN, &onlyA);
13427 if (ret == RETURN_OK) {
13428 sncopy(hw_mode, hw_mode_size, tmp);
13429 }
13430 return ret;
developer72fb0bb2023-01-11 09:46:29 +080013431}
13432
13433INT wifi_pushRadioChannel2(INT radioIndex, UINT channel, UINT channel_width_MHz, UINT csa_beacon_count)
13434{
developera3511852023-06-14 14:12:59 +080013435 // Sample commands:
13436 // hostapd_cli -i wifi1 chan_switch 30 5200 sec_channel_offset=-1 center_freq1=5190 bandwidth=40 ht vht
13437 // hostapd_cli -i wifi0 chan_switch 30 2437
13438 int ret = 0;
13439 char center_freq1_str[32] = ""; // center_freq1=%d
13440 char opt_chan_info_str[32] = ""; // bandwidth=%d ht vht
13441 char sec_chan_offset_str[32] = ""; // sec_channel_offset=%d
13442 char hw_mode[16] = ""; // n|ac
13443 char bw_mode[16] = ""; // ht|ht vht
13444 char ht_mode[16] = ""; // HT20|HT40|HT80|HT160
13445 char interface_name[16] = {0};
13446 int sec_chan_offset;
13447 int width;
13448 char config_file[64] = {0};
13449 char *ext_str = "None";
13450 wifi_band band = band_invalid;
13451 int center_chan = 0;
13452 int center_freq1 = 0;
developere40952c2023-06-15 18:46:43 +080013453 int res;
developer72fb0bb2023-01-11 09:46:29 +080013454
developere40952c2023-06-15 18:46:43 +080013455 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
13456 if (os_snprintf_error(sizeof(config_file), res)) {
13457 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13458 return RETURN_ERR;
13459 }
developer72fb0bb2023-01-11 09:46:29 +080013460
developera3511852023-06-14 14:12:59 +080013461 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
13462 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013463
developera3511852023-06-14 14:12:59 +080013464 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013465
developera3511852023-06-14 14:12:59 +080013466 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080013467
developera3511852023-06-14 14:12:59 +080013468 width = channel_width_MHz > 20 ? channel_width_MHz : 20;
developer72fb0bb2023-01-11 09:46:29 +080013469
developera3511852023-06-14 14:12:59 +080013470 // Get radio mode HT20|HT40|HT80 etc.
13471 if (channel){
developere40952c2023-06-15 18:46:43 +080013472 res = snprintf(ht_mode, sizeof(ht_mode), "HT%d", width);
13473 if (os_snprintf_error(sizeof(ht_mode), res)) {
13474 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13475 return RETURN_ERR;
13476 }
developer72fb0bb2023-01-11 09:46:29 +080013477
developera3511852023-06-14 14:12:59 +080013478 // Provide bandwith if specified
13479 if (channel_width_MHz > 20) {
13480 // Select bandwidth mode from hardware n --> ht | ac --> ht vht
13481 util_radio_get_hw_mode(radioIndex, hw_mode, sizeof(hw_mode));
13482 util_hw_mode_to_bw_mode(hw_mode, bw_mode, sizeof(bw_mode));
developer72fb0bb2023-01-11 09:46:29 +080013483
developere40952c2023-06-15 18:46:43 +080013484 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d %s", width, bw_mode);
developera3511852023-06-14 14:12:59 +080013485 }else if (channel_width_MHz == 20){
developere40952c2023-06-15 18:46:43 +080013486 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d ht", width);
developera3511852023-06-14 14:12:59 +080013487 }
developer72fb0bb2023-01-11 09:46:29 +080013488
developere40952c2023-06-15 18:46:43 +080013489 if (os_snprintf_error(sizeof(opt_chan_info_str), res)) {
13490 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13491 return RETURN_ERR;
13492 }
developer72fb0bb2023-01-11 09:46:29 +080013493
developera3511852023-06-14 14:12:59 +080013494 if (channel_width_MHz > 20) {
13495 if (band == band_6){
13496 center_chan = util_unii_6g_centerfreq(ht_mode, channel);
13497 if(center_chan){
13498 center_freq1 = util_6G_chan_to_freq(center_chan);
13499 }
13500 }else{
13501 center_chan = util_unii_5g_centerfreq(ht_mode, channel);
13502 if(center_chan){
13503 center_freq1 = util_chan_to_freq(center_chan);
13504 }
13505 }
developer69b61b02023-03-07 17:17:44 +080013506
developera3511852023-06-14 14:12:59 +080013507 if (center_freq1)
developere40952c2023-06-15 18:46:43 +080013508 res = snprintf(center_freq1_str, sizeof(center_freq1_str), "center_freq1=%d", center_freq1);
developer69b61b02023-03-07 17:17:44 +080013509
developera3511852023-06-14 14:12:59 +080013510 }
developere40952c2023-06-15 18:46:43 +080013511 if (os_snprintf_error(sizeof(center_freq1_str), res)) {
13512 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13513 return RETURN_ERR;
13514 }
developer72fb0bb2023-01-11 09:46:29 +080013515
developera3511852023-06-14 14:12:59 +080013516 // Find channel offset +1/-1 for wide modes (HT40|HT80|HT160)
13517 if (band == band_6){
13518 sec_chan_offset = util_get_6g_sec_chan_offset(channel, ht_mode);
13519 }else{
13520 sec_chan_offset = util_get_sec_chan_offset(channel, ht_mode);
13521 }
developere40952c2023-06-15 18:46:43 +080013522 if (sec_chan_offset != -EINVAL) {
13523 res = snprintf(sec_chan_offset_str, sizeof(sec_chan_offset_str), "sec_channel_offset=%d", sec_chan_offset);
13524 if (os_snprintf_error(sizeof(sec_chan_offset_str), res)) {
13525 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13526 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013527 }
developere40952c2023-06-15 18:46:43 +080013528 }
developera3511852023-06-14 14:12:59 +080013529 // Only the first AP, other are hanging on the same radio
13530 ret = wifi_setChannel_netlink(radioIndex, &channel, NULL);
13531 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013532 wifi_debug(DEBUG_ERROR,"wifi_setChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013533 return RETURN_ERR;
13534 }
13535 /* wifi_dbg_printf("execute: '%s'\n", cmd);
13536 ret = _syscmd(cmd, buf, sizeof(buf));
13537 wifi_reloadAp(radioIndex); */
developer72fb0bb2023-01-11 09:46:29 +080013538
developera3511852023-06-14 14:12:59 +080013539 ret = wifi_setRadioChannel(radioIndex, channel);
13540 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013541 wifi_debug(DEBUG_ERROR,"wifi_setRadioChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013542 return RETURN_ERR;
13543 }
developer72fb0bb2023-01-11 09:46:29 +080013544
developera3511852023-06-14 14:12:59 +080013545 if (sec_chan_offset == 1)
13546 ext_str = "Above";
13547 else if (sec_chan_offset == -1)
13548 ext_str = "Below";
developer72fb0bb2023-01-11 09:46:29 +080013549
developera3511852023-06-14 14:12:59 +080013550 /*wifi_setRadioCenterChannel(radioIndex, center_chan); */
developer72fb0bb2023-01-11 09:46:29 +080013551
developera3511852023-06-14 14:12:59 +080013552 } else {
13553 if (channel_width_MHz > 20)
13554 ext_str = "Above";
13555 }
developer72fb0bb2023-01-11 09:46:29 +080013556
developera3511852023-06-14 14:12:59 +080013557 wifi_setRadioExtChannel(radioIndex, ext_str);
developer72fb0bb2023-01-11 09:46:29 +080013558
developera3511852023-06-14 14:12:59 +080013559 char mhz_str[16];
developere40952c2023-06-15 18:46:43 +080013560 res = snprintf(mhz_str, sizeof(mhz_str), "%dMHz", width);
13561 if (os_snprintf_error(sizeof(mhz_str), res)) {
13562 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13563 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013564 }
developera3511852023-06-14 14:12:59 +080013565 wifi_setRadioOperatingChannelBandwidth(radioIndex, mhz_str);
developer72fb0bb2023-01-11 09:46:29 +080013566
developera3511852023-06-14 14:12:59 +080013567 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013568
developera3511852023-06-14 14:12:59 +080013569 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013570}
13571
13572INT wifi_getNeighboringWiFiStatus(INT radio_index, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size)
13573{
developera3511852023-06-14 14:12:59 +080013574 int index = -1;
13575 wifi_neighbor_ap2_t *scan_array = NULL;
13576 char cmd[256]={0};
13577 char buf[128]={0};
13578 char file_name[32] = {0};
13579 char filter_SSID[32] = {0};
13580 char line[256] = {0};
13581 char interface_name[16] = {0};
13582 char *ret = NULL;
13583 int freq=0;
13584 FILE *f = NULL;
developer86035662023-06-28 19:21:12 +080013585 long int channels_num = 0;
developera3511852023-06-14 14:12:59 +080013586 int vht_channel_width = 0;
13587 int get_noise_ret = RETURN_ERR;
13588 bool filter_enable = false;
13589 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
13590 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080013591 int res;
developer32f2a182023-06-27 19:50:41 +080013592 unsigned long len;
developerb14b3462023-07-01 18:02:42 +080013593 struct channels_noise *channels_noise_arr = NULL;
developer72fb0bb2023-01-11 09:46:29 +080013594
developera3511852023-06-14 14:12:59 +080013595 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013596
developere40952c2023-06-15 18:46:43 +080013597 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radio_index);
13598 if (os_snprintf_error(sizeof(file_name), res)) {
13599 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13600 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013601 }
developera3511852023-06-14 14:12:59 +080013602 f = fopen(file_name, "r");
13603 if (f != NULL) {
developer86035662023-06-28 19:21:12 +080013604 if (fgets(filter_SSID, sizeof(file_name), f) == NULL) {
13605 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080013606 if (fclose(f) != 0) {
13607 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13608 }
developer86035662023-06-28 19:21:12 +080013609 return RETURN_ERR;
13610 }
developera3511852023-06-14 14:12:59 +080013611 if (strlen(filter_SSID) != 0)
13612 filter_enable = true;
developerd14dff12023-06-28 22:47:44 +080013613 if (fclose(f) != 0) {
13614 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13615 return RETURN_ERR;
13616 }
developera3511852023-06-14 14:12:59 +080013617 }
developer72fb0bb2023-01-11 09:46:29 +080013618
developera3511852023-06-14 14:12:59 +080013619 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
13620 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013621
developera3511852023-06-14 14:12:59 +080013622 phyId = radio_index_to_phy(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080013623
developere40952c2023-06-15 18:46:43 +080013624 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
13625 if (os_snprintf_error(sizeof(cmd), res)) {
13626 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13627 return RETURN_ERR;
13628 }
developera3511852023-06-14 14:12:59 +080013629 _syscmd(cmd, buf, sizeof(buf));
developer86035662023-06-28 19:21:12 +080013630 if (hal_strtol(buf, 10, &channels_num) < 0) {
13631 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13632 return RETURN_ERR;
13633 }
developer72fb0bb2023-01-11 09:46:29 +080013634
developer5b23cd02023-07-19 20:26:03 +080013635 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan | grep '%s\\|SSID\\|freq\\|beacon interval\\|capabilities\\|signal\\|Supported rates\\|DTIM\\| \
developera3511852023-06-14 14:12:59 +080013636 // WPA\\|RSN\\|Group cipher\\|HT operation\\|secondary channel offset\\|channel width\\|HE.*GHz' | grep -v -e '*.*BSS'", interface_name, interface_name);
developer32f2a182023-06-27 19:50:41 +080013637 if (os_snprintf_error(sizeof(cmd), res)) {
13638 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13639 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013640 }
developer32f2a182023-06-27 19:50:41 +080013641
developer86035662023-06-28 19:21:12 +080013642 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developera3511852023-06-14 14:12:59 +080013643 if ((f = popen(cmd, "r")) == NULL) {
13644 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
13645 return RETURN_ERR;
13646 }
developer69b61b02023-03-07 17:17:44 +080013647
developerb14b3462023-07-01 18:02:42 +080013648 if (channels_num > 0 && channels_num <= 243) {
13649 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
developer86035662023-06-28 19:21:12 +080013650
developerb14b3462023-07-01 18:02:42 +080013651 if (channels_noise_arr == NULL) {
13652 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13653 goto err;
13654 }
13655 get_noise_ret = get_noise(radio_index, channels_noise_arr, channels_num);
developer9ce44382023-06-28 11:09:37 +080013656 }
developer86035662023-06-28 19:21:12 +080013657
developera3511852023-06-14 14:12:59 +080013658 ret = fgets(line, sizeof(line), f);
13659 while (ret != NULL) {
13660 if(strstr(line, "BSS") != NULL) { // new neighbor info
13661 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
13662 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
13663 // If we don't want the BSS info, we don't realloc more space, and just clean the previous BSS.
developer72fb0bb2023-01-11 09:46:29 +080013664
developera3511852023-06-14 14:12:59 +080013665 if (!filter_BSS) {
13666 index++;
developer5b23cd02023-07-19 20:26:03 +080013667 wifi_debug(DEBUG_OFF, "index=%d\n", index);
developera3511852023-06-14 14:12:59 +080013668 wifi_neighbor_ap2_t *tmp;
13669 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
13670 if (tmp == NULL) { // no more memory to use
13671 index--;
13672 wifi_dbg_printf("%s: realloc failed\n", __func__);
13673 break;
13674 }
13675 scan_array = tmp;
13676 }
13677 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +080013678
developera3511852023-06-14 14:12:59 +080013679 filter_BSS = false;
developer86035662023-06-28 19:21:12 +080013680 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
13681 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13682 goto err;
13683 }
developerc79e9172023-06-06 19:48:03 +080013684 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +080013685 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +080013686 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +080013687 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +080013688 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +080013689 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
13690 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +080013691 if (sscanf(line," freq: %d", &freq) != 1) {
13692 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +080013693 //goto err;
developer86035662023-06-28 19:21:12 +080013694 }
developera3511852023-06-14 14:12:59 +080013695 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080013696
developera3511852023-06-14 14:12:59 +080013697 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +080013698 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013699 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +080013700 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013701 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +080013702 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013703 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
13704 }
13705 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +080013706 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013707 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +080013708 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013709 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +080013710 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013711 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
13712 }
developer72fb0bb2023-01-11 09:46:29 +080013713
developera3511852023-06-14 14:12:59 +080013714 scan_array[index].ap_Noise = 0;
13715 if (get_noise_ret == RETURN_OK) {
13716 for (int i = 0; i < channels_num; i++) {
13717 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
13718 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
13719 break;
13720 }
13721 }
13722 }
13723 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +080013724 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
13725 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13726 goto err;
13727 }
developera3511852023-06-14 14:12:59 +080013728 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +080013729 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
13730 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13731 goto err;
13732 }
developer5b23cd02023-07-19 20:26:03 +080013733 } else if (strstr(line,"SSID:") != NULL) {
13734 int i;
13735 char *new_line = NULL;
13736
13737 for (i = 0; line[i] == '\t'; i++) {
13738 ;
developer86035662023-06-28 19:21:12 +080013739 }
developer5b23cd02023-07-19 20:26:03 +080013740 new_line = &line[i];
13741
13742 if (strncmp(new_line, "SSID: \n", strlen("SSID: \n")) == 0) {
13743 ret = fgets(line, sizeof(line), f);
13744 continue;
13745 }
13746 if (strstr(new_line, "HESSID") == NULL) {
13747 if (sscanf(new_line, "SSID: %63s", scan_array[index].ap_SSID) != 1) {
13748 wifi_debug(DEBUG_ERROR, "sscanf fail, index = %d\n", index);
13749 goto err;
13750 } else
13751 wifi_debug(DEBUG_ERROR, "index = %d, ssid=%s\n", index, scan_array[index].ap_SSID);
13752 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) == 0) {
13753 filter_BSS = true;
13754 }
developera3511852023-06-14 14:12:59 +080013755 }
13756 } else if (strstr(line, "Supported rates") != NULL) {
13757 char SRate[80] = {0}, *tmp = NULL;
13758 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080013759 len = strlen(line);
13760 if (len >= sizeof(SRate)) {
13761 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013762 goto err;
developer32f2a182023-06-27 19:50:41 +080013763 }
13764 strncpy(SRate, line, len);
developera3511852023-06-14 14:12:59 +080013765 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +080013766 if (tmp == NULL)
13767 goto err;
developera3511852023-06-14 14:12:59 +080013768 tmp = strtok(NULL, ":");
developer5b23cd02023-07-19 20:26:03 +080013769 if (tmp == NULL)
developer86035662023-06-28 19:21:12 +080013770 goto err;
developer5b23cd02023-07-19 20:26:03 +080013771
developer32f2a182023-06-27 19:50:41 +080013772 len = strlen(tmp);
13773 if (len >= sizeof(buf)) {
13774 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
developer86035662023-06-28 19:21:12 +080013775 goto err;
developer32f2a182023-06-27 19:50:41 +080013776 }
13777 strncpy(buf, tmp, len);
developera3511852023-06-14 14:12:59 +080013778 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +080013779
developera3511852023-06-14 14:12:59 +080013780 tmp = strtok(buf, " \n");
13781 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +080013782 if ((sizeof(SRate) - strlen(SRate)) <= strlen(tmp)) {
13783 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013784 goto err;
developer32f2a182023-06-27 19:50:41 +080013785 }
13786 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +080013787 if (SRate[strlen(SRate) - 1] == '*') {
13788 SRate[strlen(SRate) - 1] = '\0';
13789 }
developer32f2a182023-06-27 19:50:41 +080013790 if ((sizeof(SRate) - strlen(SRate)) <= 1) {
13791 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013792 goto err;
developer32f2a182023-06-27 19:50:41 +080013793 }
13794 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +080013795
developera3511852023-06-14 14:12:59 +080013796 tmp = strtok(NULL, " \n");
13797 }
13798 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +080013799 len = strlen(SRate);
13800 if (len >= sizeof(scan_array[index].ap_SupportedDataTransferRates)) {
13801 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedDataTransferRates\n");
developer86035662023-06-28 19:21:12 +080013802 goto err;
developer32f2a182023-06-27 19:50:41 +080013803 }
13804 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, len);
13805 scan_array[index].ap_SupportedDataTransferRates[len] = '\0';
developera3511852023-06-14 14:12:59 +080013806 } else if (strstr(line, "DTIM") != NULL) {
developer5b23cd02023-07-19 20:26:03 +080013807 if (sscanf(line," TIM: DTIM Count %*d DTIM Period %d %*s", &(scan_array[index].ap_DTIMPeriod)) != 1) {
developer86035662023-06-28 19:21:12 +080013808 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developer5b23cd02023-07-19 20:26:03 +080013809 goto err;
developer86035662023-06-28 19:21:12 +080013810 }
developera3511852023-06-14 14:12:59 +080013811 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013812 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13813 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013814 goto err;
developer32f2a182023-06-27 19:50:41 +080013815 }
13816 strncat(scan_array[index].ap_SupportedStandards, ",ac",
13817 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13818 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
13819 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +080013820 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013821 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 2) {
13822 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013823 goto err;
developer32f2a182023-06-27 19:50:41 +080013824 }
13825 strncat(scan_array[index].ap_SupportedStandards, ",n",
13826 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13827 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
13828 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +080013829 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013830 if (fgets(line, sizeof(line), f) == NULL) {
13831 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13832 goto err;
13833 }
13834 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
13835 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13836 goto err;
13837 }
developera3511852023-06-14 14:12:59 +080013838 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +080013839 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +080013840 } else {
developere40952c2023-06-15 18:46:43 +080013841 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +080013842 }
developere40952c2023-06-15 18:46:43 +080013843 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13844 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013845 goto err;
developer5b23cd02023-07-19 20:26:03 +080013846 }
developere40952c2023-06-15 18:46:43 +080013847
developera3511852023-06-14 14:12:59 +080013848 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
13849 continue;
13850 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013851 if (fgets(line, sizeof(line), f) == NULL) {
13852 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13853 goto err;
13854 }
developer86035662023-06-28 19:21:12 +080013855 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
13856 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13857 goto err;
13858 }
developera3511852023-06-14 14:12:59 +080013859 if (!strcmp(buf, "above")) {
13860 //40Mhz +
developere40952c2023-06-15 18:46:43 +080013861 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT40PLUS", radio_index%1 ? "A": "G");
developera3511852023-06-14 14:12:59 +080013862 }
13863 else if (!strcmp(buf, "below")) {
13864 //40Mhz -
developere40952c2023-06-15 18:46:43 +080013865 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT40MINUS", radio_index%1 ? "A": "G");
developera3511852023-06-14 14:12:59 +080013866 } else {
13867 //20Mhz
developere40952c2023-06-15 18:46:43 +080013868 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT20", radio_index%1 ? "A": "G");
developera3511852023-06-14 14:12:59 +080013869 }
developere40952c2023-06-15 18:46:43 +080013870 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13871 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013872 goto err;
developer5b23cd02023-07-19 20:26:03 +080013873 }
developere40952c2023-06-15 18:46:43 +080013874
developer5b23cd02023-07-19 20:26:03 +080013875 if (strstr(line, "BSS") != NULL) { // prevent to get the next neighbor information
13876 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013877 continue;
developer5b23cd02023-07-19 20:26:03 +080013878 }
developera3511852023-06-14 14:12:59 +080013879 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013880 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13881 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013882 goto err;
developer32f2a182023-06-27 19:50:41 +080013883 }
13884 strncat(scan_array[index].ap_SupportedStandards, ",ax",
13885 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13886 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
13887 scan_array[index].ap_OperatingStandards[2] = '\0';
developerc14d83a2023-06-29 20:09:42 +080013888 if (fgets(line, sizeof(line), f) == NULL) {
13889 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13890 goto err;
13891 }
developera3511852023-06-14 14:12:59 +080013892 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013893 if (strstr(line, "HE40/2.4GHz") != NULL) {
13894 len = strlen("11AXHE40PLUS");
13895 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
13896 } else {
13897 len = strlen("11AXHE20");
13898 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
13899 }
13900 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013901 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
13902 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013903 len = strlen("11AXHE80");
13904 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
13905 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013906 ret = fgets(line, sizeof(line), f);
developer5b23cd02023-07-19 20:26:03 +080013907 } else {
13908 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013909 continue;
developer5b23cd02023-07-19 20:26:03 +080013910 }
developer32f2a182023-06-27 19:50:41 +080013911 if (strstr(line, "HE160/5GHz") != NULL) {
13912 len = strlen("11AXHE160");
13913 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
13914 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
13915 }
developera3511852023-06-14 14:12:59 +080013916 }
developer5b23cd02023-07-19 20:26:03 +080013917 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013918 continue;
13919 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013920 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
13921 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013922 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013923 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
13924 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013925 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +080013926 if (sscanf(line, " * Group cipher: %63s", scan_array[index].ap_EncryptionMode) != 1) {
13927 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13928 goto err;
13929 }
developera3511852023-06-14 14:12:59 +080013930 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013931 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
13932 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +080013933 }
13934 }
13935 ret = fgets(line, sizeof(line), f);
13936 }
developer72fb0bb2023-01-11 09:46:29 +080013937
developera3511852023-06-14 14:12:59 +080013938 if (!filter_BSS) {
13939 *output_array_size = index + 1;
13940 } else {
13941 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
13942 *output_array_size = index;
13943 }
13944 *neighbor_ap_array = scan_array;
13945 pclose(f);
13946 free(channels_noise_arr);
13947 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
13948 return RETURN_OK;
developer86035662023-06-28 19:21:12 +080013949err:
developerc14d83a2023-06-29 20:09:42 +080013950 pclose(f);
developer86035662023-06-28 19:21:12 +080013951 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +080013952 if (scan_array)
13953 free(scan_array);
developer86035662023-06-28 19:21:12 +080013954 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013955}
13956
13957INT wifi_getApAssociatedDeviceStats(
developera3511852023-06-14 14:12:59 +080013958 INT apIndex,
13959 mac_address_t *clientMacAddress,
13960 wifi_associated_dev_stats_t *associated_dev_stats,
13961 u64 *handle)
developer72fb0bb2023-01-11 09:46:29 +080013962{
developera3511852023-06-14 14:12:59 +080013963 wifi_associated_dev_stats_t *dev_stats = associated_dev_stats;
13964 char interface_name[50] = {0};
13965 char cmd[1024] = {0};
13966 char mac_str[18] = {0};
13967 char *key = NULL;
13968 char *val = NULL;
13969 FILE *f = NULL;
13970 char *line = NULL;
13971 size_t len = 0;
developer75bd10c2023-06-27 11:34:08 +080013972 int res;
developer72fb0bb2023-01-11 09:46:29 +080013973
developera3511852023-06-14 14:12:59 +080013974 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
13975 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
13976 return RETURN_ERR;
13977 }
developer72fb0bb2023-01-11 09:46:29 +080013978
developer32f2a182023-06-27 19:50:41 +080013979 res = snprintf(mac_str, sizeof(mac_str), "%x:%x:%x:%x:%x:%x",
13980 (*clientMacAddress)[0], (*clientMacAddress)[1], (*clientMacAddress)[2],
13981 (*clientMacAddress)[3], (*clientMacAddress)[4], (*clientMacAddress)[5]);
developer75bd10c2023-06-27 11:34:08 +080013982 if (os_snprintf_error(sizeof(mac_str), res)) {
13983 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13984 return RETURN_ERR;
13985 }
13986
13987 res = snprintf(cmd, sizeof(cmd), "iw dev %s station get %s | grep 'rx\\|tx' | tr -d '\t'", interface_name, mac_str);
13988 if (os_snprintf_error(sizeof(cmd), res)) {
13989 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13990 return RETURN_ERR;
13991 }
13992
developera3511852023-06-14 14:12:59 +080013993 if((f = popen(cmd, "r")) == NULL) {
13994 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
13995 return RETURN_ERR;
13996 }
developer72fb0bb2023-01-11 09:46:29 +080013997
developera3511852023-06-14 14:12:59 +080013998 while ((getline(&line, &len, f)) != -1) {
13999 key = strtok(line,":");
developer37646972023-06-29 10:58:43 +080014000 if (key == NULL)
14001 continue;
developera3511852023-06-14 14:12:59 +080014002 val = strtok(NULL,":");
developer37646972023-06-29 10:58:43 +080014003 if (val == NULL)
14004 continue;
developer72fb0bb2023-01-11 09:46:29 +080014005
developerb61d3362023-06-29 14:10:19 +080014006 if(!strncmp(key,"rx bytes",8))
14007 if (sscanf(val, "%llu", &dev_stats->cli_rx_bytes) != 1) {
14008 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080014009 continue;
developerb61d3362023-06-29 14:10:19 +080014010 }
14011 if(!strncmp(key,"tx bytes",8))
14012 if (sscanf(val, "%llu", &dev_stats->cli_tx_bytes) != 1) {
14013 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080014014 continue;
developerb61d3362023-06-29 14:10:19 +080014015 }
developer37646972023-06-29 10:58:43 +080014016 if(!strncmp(key,"rx packets",10)) {
14017 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
14018 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14019 continue;
14020 }
14021 }
14022 if(!strncmp(key,"tx packets",10)) {
14023 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
14024 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14025 continue;
14026 }
14027 }
14028 if(!strncmp(key,"tx retries",10)) {
14029 if (sscanf(val, "%llu", &dev_stats->cli_tx_retries) == EOF) {
14030 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14031 continue;
14032 }
14033 }
14034 if(!strncmp(key,"tx failed",9)) {
14035 if (sscanf(val, "%llu", &dev_stats->cli_tx_errors) == EOF) {
14036 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14037 continue;
14038 }
14039 }
14040 if(!strncmp(key,"rx drop misc",13)) {
14041 if (sscanf(val, "%llu", &dev_stats->cli_rx_errors) == EOF) {
14042 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14043 continue;
14044 }
developerd14dff12023-06-28 22:47:44 +080014045 }
developera3511852023-06-14 14:12:59 +080014046 if(!strncmp(key,"rx bitrate",10)) {
14047 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080014048 if (val == NULL)
14049 continue;
developer37646972023-06-29 10:58:43 +080014050 if (sscanf(val, "%lf", &dev_stats->cli_rx_rate) == EOF) {
14051 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14052 continue;
14053 }
developera3511852023-06-14 14:12:59 +080014054 }
14055 if(!strncmp(key,"tx bitrate",10)) {
14056 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080014057 if (val == NULL)
14058 continue;
developer37646972023-06-29 10:58:43 +080014059 if (sscanf(val, "%lf", &dev_stats->cli_tx_rate) == EOF) {
14060 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14061 continue;
14062 }
developera3511852023-06-14 14:12:59 +080014063 }
14064 }
14065 free(line);
14066 pclose(f);
14067 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014068}
14069
14070INT wifi_getSSIDNameStatus(INT apIndex, CHAR *output_string)
14071{
developera3511852023-06-14 14:12:59 +080014072 char interface_name[IF_NAME_SIZE] = {0};
14073 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
developere40952c2023-06-15 18:46:43 +080014074 int res;
developer72fb0bb2023-01-11 09:46:29 +080014075
developera3511852023-06-14 14:12:59 +080014076 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080014077
developera3511852023-06-14 14:12:59 +080014078 if (NULL == output_string)
14079 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014080
developera3511852023-06-14 14:12:59 +080014081 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
14082 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +080014083
developere40952c2023-06-15 18:46:43 +080014084 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep ^ssid | cut -d '=' -f2 | tr -d '\\n'", interface_name);
14085 if (os_snprintf_error(sizeof(cmd), res)) {
14086 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14087 return RETURN_ERR;
14088 }
developera3511852023-06-14 14:12:59 +080014089 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080014090
developera3511852023-06-14 14:12:59 +080014091 //size of SSID name restricted to value less than 32 bytes
developere40952c2023-06-15 18:46:43 +080014092 res = snprintf(output_string, 32, "%s", buf);
14093 if (os_snprintf_error(32, res)) {
14094 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14095 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014096 }
developera3511852023-06-14 14:12:59 +080014097 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014098
developera3511852023-06-14 14:12:59 +080014099 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014100}
14101
14102INT wifi_getApMacAddressControlMode(INT apIndex, INT *output_filterMode)
14103{
developer2edaf012023-05-24 14:24:53 +080014104 char *mac_arry_buf = NULL;
14105 INT policy = -1;
14106 INT buf_size = 1024;
developer72fb0bb2023-01-11 09:46:29 +080014107
developer2edaf012023-05-24 14:24:53 +080014108 mac_arry_buf = malloc(buf_size);
14109 if (!mac_arry_buf) {
14110 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
developer7e4a2a62023-04-06 19:56:03 +080014111 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080014112 }
14113 memset(mac_arry_buf, 0, buf_size);
14114 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
14115 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
14116 goto err;
14117 }
14118 /*
14119 mtk format to get policy:
14120 "policy=1
14121 00:11:22:33:44:55
14122 00:11:22:33:44:66
14123 "
14124 */
14125 if (strlen(mac_arry_buf) < strlen("policy=1") || sscanf(mac_arry_buf, "policy=%01d", &policy) != 1) {
14126 wifi_debug(DEBUG_ERROR,"mac_arry_buf(%s) invalid\n", mac_arry_buf);
14127 goto err;
14128 }
14129 if (!(policy >=0 && policy <= 2)){
14130 wifi_debug(DEBUG_ERROR,"policy(%d) is invalid\n", policy);
14131 goto err;
14132 }
14133 *output_filterMode = policy;
14134 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), success\n", *output_filterMode);
14135 free(mac_arry_buf);
14136 mac_arry_buf = NULL;
14137 return RETURN_OK;
14138err:
14139 free(mac_arry_buf);
14140 mac_arry_buf = NULL;
14141 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), fails\n", *output_filterMode);
14142 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014143}
14144
developer2edaf012023-05-24 14:24:53 +080014145
developer72fb0bb2023-01-11 09:46:29 +080014146INT wifi_getApAssociatedDeviceDiagnosticResult2(INT apIndex,wifi_associated_dev2_t **associated_dev_array,UINT *output_array_size)
14147{
developera3511852023-06-14 14:12:59 +080014148 FILE *fp = NULL;
14149 char str[MAX_BUF_SIZE] = {0};
14150 int wificlientindex = 0 ;
14151 int count = 0;
14152 int signalstrength = 0;
14153 int arr[MACADDRESS_SIZE] = {0};
14154 unsigned char mac[MACADDRESS_SIZE] = {0};
14155 UINT wifi_count = 0;
14156 char pipeCmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014157 int res;
developer72fb0bb2023-01-11 09:46:29 +080014158
developera3511852023-06-14 14:12:59 +080014159 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14160 *output_array_size = 0;
14161 *associated_dev_array = NULL;
14162 char interface_name[50] = {0};
developer72fb0bb2023-01-11 09:46:29 +080014163
developera3511852023-06-14 14:12:59 +080014164 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
14165 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
14166 return RETURN_ERR;
14167 }
developer72fb0bb2023-01-11 09:46:29 +080014168
developer75bd10c2023-06-27 11:34:08 +080014169 res = snprintf(pipeCmd, sizeof(pipeCmd), "iw dev %s station dump | grep %s | wc -l", interface_name, interface_name);
14170 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14171 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14172 return RETURN_ERR;
14173 }
developera3511852023-06-14 14:12:59 +080014174 fp = popen(pipeCmd, "r");
14175 if (fp == NULL)
14176 {
14177 printf("Failed to run command inside function %s\n",__FUNCTION__ );
14178 return RETURN_ERR;
14179 }
developer72fb0bb2023-01-11 09:46:29 +080014180
developera3511852023-06-14 14:12:59 +080014181 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080014182 if (fgets(str, sizeof(str)-1, fp) == NULL) {
14183 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14184 pclose(fp);
14185 return RETURN_ERR;
14186 }
developera3511852023-06-14 14:12:59 +080014187 wifi_count = (unsigned int) atoi ( str );
14188 *output_array_size = wifi_count;
14189 wifi_dbg_printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
14190 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014191
developera3511852023-06-14 14:12:59 +080014192 if(wifi_count == 0)
14193 {
14194 return RETURN_OK;
14195 }
14196 else
14197 {
14198 wifi_associated_dev2_t* temp = NULL;
14199 temp = (wifi_associated_dev2_t*)calloc(wifi_count, sizeof(wifi_associated_dev2_t));
14200 *associated_dev_array = temp;
14201 if(temp == NULL)
14202 {
14203 printf("Error Statement. Insufficient memory \n");
14204 return RETURN_ERR;
14205 }
developer72fb0bb2023-01-11 09:46:29 +080014206
developer33f13ba2023-07-12 16:19:06 +080014207 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
14208 if (res) {
14209 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080014210 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014211 }
developere40952c2023-06-15 18:46:43 +080014212
developera3511852023-06-14 14:12:59 +080014213 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
14214 if(fp == NULL)
14215 {
14216 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
14217 return RETURN_ERR;
14218 }
developere75ba632023-06-29 16:03:33 +080014219 if (fclose(fp) == EOF) {
14220 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14221 return RETURN_ERR;
14222 }
developer72fb0bb2023-01-11 09:46:29 +080014223
developer86035662023-06-28 19:21:12 +080014224 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
14225 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14226 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14227 return RETURN_ERR;
14228 }
developera3511852023-06-14 14:12:59 +080014229 fp = popen(pipeCmd, "r");
14230 if(fp)
14231 {
14232 for(count =0 ; count < wifi_count; count++)
14233 {
developer86035662023-06-28 19:21:12 +080014234 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14235 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14236 pclose(fp);
14237 return RETURN_ERR;
14238 }
developera3511852023-06-14 14:12:59 +080014239 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
14240 {
14241 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
14242 {
14243 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080014244
developera3511852023-06-14 14:12:59 +080014245 }
14246 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
14247 wifi_dbg_printf("MAC %d = %X:%X:%X:%X:%X:%X \n", count, temp[count].cli_MACAddress[0],temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2], temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4], temp[count].cli_MACAddress[5]);
14248 }
14249 temp[count].cli_AuthenticationState = 1; //TODO
14250 temp[count].cli_Active = 1; //TODO
14251 }
14252 pclose(fp);
14253 }
developer72fb0bb2023-01-11 09:46:29 +080014254
developera3511852023-06-14 14:12:59 +080014255 //Updating RSSI per client
developer86035662023-06-28 19:21:12 +080014256 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep signal | tr -s ' ' | cut -d ' ' -f 2 > /tmp/wifi_signalstrength.txt");
14257 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14258 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14259 return RETURN_ERR;
14260 }
14261
developera3511852023-06-14 14:12:59 +080014262 fp = popen(pipeCmd, "r");
14263 if(fp)
14264 {
14265 pclose(fp);
14266 }
14267 fp = popen("cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2","r");
14268 if(fp)
14269 {
14270 for(count =0 ; count < wifi_count ;count++)
14271 {
developer86035662023-06-28 19:21:12 +080014272 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14273 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14274 pclose(fp);
14275 return RETURN_ERR;
14276 }
developera3511852023-06-14 14:12:59 +080014277 signalstrength = atoi(str);
14278 temp[count].cli_RSSI = signalstrength;
14279 }
14280 pclose(fp);
14281 }
developer72fb0bb2023-01-11 09:46:29 +080014282
14283
developera3511852023-06-14 14:12:59 +080014284 //LastDataDownlinkRate
developer86035662023-06-28 19:21:12 +080014285 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Send.txt");
14286 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14287 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14288 return RETURN_ERR;
14289 }
developera3511852023-06-14 14:12:59 +080014290 fp = popen(pipeCmd, "r");
14291 if (fp)
14292 {
14293 pclose(fp);
14294 }
14295 fp = popen("cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2", "r");
14296 if (fp)
14297 {
developerc14d83a2023-06-29 20:09:42 +080014298 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014299 for (count = 0; count < wifi_count; count++)
14300 {
developer86035662023-06-28 19:21:12 +080014301 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14302 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14303 pclose(fp);
14304 return RETURN_ERR;
14305 }
developerc14d83a2023-06-29 20:09:42 +080014306 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14307 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14308 }
14309 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014310 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
14311 }
14312 pclose(fp);
14313 }
developer72fb0bb2023-01-11 09:46:29 +080014314
developera3511852023-06-14 14:12:59 +080014315 //LastDataUplinkRate
developer86035662023-06-28 19:21:12 +080014316 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep 'rx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Received.txt");
14317 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14318 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14319 return RETURN_ERR;
14320 }
developera3511852023-06-14 14:12:59 +080014321 fp = popen(pipeCmd, "r");
14322 if (fp)
14323 {
14324 pclose(fp);
14325 }
14326 fp = popen("cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2", "r");
14327 if (fp)
14328 {
developerc14d83a2023-06-29 20:09:42 +080014329 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014330 for (count = 0; count < wifi_count; count++)
14331 {
developer86035662023-06-28 19:21:12 +080014332 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14333 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14334 pclose(fp);
14335 return RETURN_ERR;
14336 }
developerc14d83a2023-06-29 20:09:42 +080014337 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14338 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14339 }
14340 temp[count].cli_LastDataUplinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014341 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
14342 }
14343 pclose(fp);
14344 }
14345 }
14346 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14347 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014348
14349}
14350
14351INT wifi_getSSIDTrafficStats2(INT ssidIndex,wifi_ssidTrafficStats2_t *output_struct)
14352{
developera3511852023-06-14 14:12:59 +080014353 FILE *fp = NULL;
14354 char interface_name[50] = {0};
14355 char pipeCmd[128] = {0};
14356 char str[256] = {0};
14357 wifi_ssidTrafficStats2_t *out = output_struct;
developer75bd10c2023-06-27 11:34:08 +080014358 int res;
developerd14dff12023-06-28 22:47:44 +080014359 unsigned int recv;
developer72fb0bb2023-01-11 09:46:29 +080014360
developera3511852023-06-14 14:12:59 +080014361 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
14362 if (!output_struct)
14363 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014364
developera3511852023-06-14 14:12:59 +080014365 memset(out, 0, sizeof(wifi_ssidTrafficStats2_t));
14366 if (wifi_GetInterfaceName(ssidIndex, interface_name) != RETURN_OK)
14367 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080014368 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /proc/net/dev | grep %s", interface_name);
14369 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14370 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14371 return RETURN_ERR;
14372 }
developer72fb0bb2023-01-11 09:46:29 +080014373
developera3511852023-06-14 14:12:59 +080014374 fp = popen(pipeCmd, "r");
14375 if (fp == NULL) {
developer86035662023-06-28 19:21:12 +080014376 wifi_debug(DEBUG_ERROR, "%s: popen failed\n", __func__);
14377 return RETURN_ERR;
14378 }
14379 if (fgets(str, sizeof(str), fp) == NULL) {
14380 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14381 pclose(fp);
developera3511852023-06-14 14:12:59 +080014382 return RETURN_ERR;
14383 }
developer5b23cd02023-07-19 20:26:03 +080014384
developera3511852023-06-14 14:12:59 +080014385 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014386
developera3511852023-06-14 14:12:59 +080014387 if (strlen(str) == 0) // interface not exist
14388 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014389
developerd14dff12023-06-28 22:47:44 +080014390 recv = sscanf(str, "%*[^:]: %lu %lu %lu %lu %*d %*d %*d %*d %lu %lu %lu %lu", &out->ssid_BytesReceived, &out->ssid_PacketsReceived, &out->ssid_ErrorsReceived, \
developera3511852023-06-14 14:12:59 +080014391 &out->ssid_DiscardedPacketsReceived, &out->ssid_BytesSent, &out->ssid_PacketsSent, &out->ssid_ErrorsSent, &out->ssid_DiscardedPacketsSent);
developerd14dff12023-06-28 22:47:44 +080014392 if (recv != 8) {
14393 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
14394 return RETURN_ERR;
14395 }
developer72fb0bb2023-01-11 09:46:29 +080014396
developera3511852023-06-14 14:12:59 +080014397 memset(str, 0, sizeof(str));
developer75bd10c2023-06-27 11:34:08 +080014398
14399 res = snprintf(pipeCmd, sizeof(pipeCmd), "tail -n1 /proc/net/netstat");
14400 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14401 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14402 return RETURN_ERR;
14403 }
developera3511852023-06-14 14:12:59 +080014404 fp = popen(pipeCmd, "r");
14405 if (fp == NULL) {
developer75bd10c2023-06-27 11:34:08 +080014406 wifi_debug(DEBUG_ERROR, "popen failed\n");
developera3511852023-06-14 14:12:59 +080014407 return RETURN_ERR;
14408 }
developer86035662023-06-28 19:21:12 +080014409
14410 if (fgets(str, sizeof(str), fp) == NULL) {
14411 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14412 pclose(fp);
14413 return RETURN_ERR;
14414 }
developer72fb0bb2023-01-11 09:46:29 +080014415
developer37646972023-06-29 10:58:43 +080014416 if (sscanf(str, "%*[^:]: %lu %lu %lu %lu", &out->ssid_MulticastPacketsReceived, &out->ssid_MulticastPacketsSent, &out->ssid_BroadcastPacketsRecevied, \
14417 &out->ssid_BroadcastPacketsSent) == EOF)
14418 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developera3511852023-06-14 14:12:59 +080014419 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014420
developera3511852023-06-14 14:12:59 +080014421 out->ssid_UnicastPacketsSent = out->ssid_PacketsSent - out->ssid_MulticastPacketsSent - out->ssid_BroadcastPacketsSent - out->ssid_DiscardedPacketsSent;
14422 out->ssid_UnicastPacketsReceived = out->ssid_PacketsReceived - out->ssid_MulticastPacketsReceived - out->ssid_BroadcastPacketsRecevied - out->ssid_DiscardedPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +080014423
developera3511852023-06-14 14:12:59 +080014424 // Not supported
14425 output_struct->ssid_RetransCount = 0;
14426 output_struct->ssid_FailedRetransCount = 0;
14427 output_struct->ssid_RetryCount = 0;
14428 output_struct->ssid_MultipleRetryCount = 0;
14429 output_struct->ssid_ACKFailureCount = 0;
14430 output_struct->ssid_AggregatedPacketCount = 0;
developer72fb0bb2023-01-11 09:46:29 +080014431
developera3511852023-06-14 14:12:59 +080014432 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014433}
14434
14435//Enables or disables device isolation. A value of true means that the devices connected to the Access Point are isolated from all other devices within the home network (as is typically the case for a Wireless Hotspot).
14436INT wifi_getApIsolationEnable(INT apIndex, BOOL *output)
14437{
developera3511852023-06-14 14:12:59 +080014438 char output_val[16]={'\0'};
14439 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080014440 int res;
developer72fb0bb2023-01-11 09:46:29 +080014441
developera3511852023-06-14 14:12:59 +080014442 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14443 if (!output)
14444 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080014445
14446 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
14447 if (os_snprintf_error(sizeof(config_file), res)) {
14448 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14449 return RETURN_ERR;
14450 }
developera3511852023-06-14 14:12:59 +080014451 wifi_hostapdRead(config_file, "ap_isolate", output_val, sizeof(output_val));
developer72fb0bb2023-01-11 09:46:29 +080014452
developera3511852023-06-14 14:12:59 +080014453 if( strcmp(output_val,"1") == 0 )
14454 *output = TRUE;
14455 else
14456 *output = FALSE;
14457 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014458
developera3511852023-06-14 14:12:59 +080014459 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014460}
14461
14462INT wifi_setApIsolationEnable(INT apIndex, BOOL enable)
14463{
developera3511852023-06-14 14:12:59 +080014464 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14465 char string[MAX_BUF_SIZE]={'\0'};
14466 char config_file[MAX_BUF_SIZE] = {0};
14467 struct params params;
developer75bd10c2023-06-27 11:34:08 +080014468 int res;
developer72fb0bb2023-01-11 09:46:29 +080014469
developer32f2a182023-06-27 19:50:41 +080014470 string[0] = enable == TRUE ? '1' : '0';
developer72fb0bb2023-01-11 09:46:29 +080014471
developera3511852023-06-14 14:12:59 +080014472 params.name = "ap_isolate";
14473 params.value = string;
developer72fb0bb2023-01-11 09:46:29 +080014474
developer75bd10c2023-06-27 11:34:08 +080014475 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
14476 if (os_snprintf_error(sizeof(config_file), res)) {
14477 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14478 return RETURN_ERR;
14479 }
14480
developera3511852023-06-14 14:12:59 +080014481 wifi_hostapdWrite(config_file,&params,1);
14482 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014483
developera3511852023-06-14 14:12:59 +080014484 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014485}
14486
14487INT wifi_getApManagementFramePowerControl(INT apIndex, INT *output_dBm)
14488{
developera3511852023-06-14 14:12:59 +080014489 char mgmtpwr_file[32] = {0};
14490 char cmd[64] = {0};
14491 char buf[32]={0};
developere40952c2023-06-15 18:46:43 +080014492 int res;
developerc14d83a2023-06-29 20:09:42 +080014493 long int tmp;
developera1255e42023-05-13 17:45:02 +080014494
developera3511852023-06-14 14:12:59 +080014495 if (NULL == output_dBm)
14496 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080014497 res = snprintf(mgmtpwr_file, sizeof(mgmtpwr_file), "%s%d.txt", MGMT_POWER_CTRL, apIndex);
14498 if (os_snprintf_error(sizeof(mgmtpwr_file), res)) {
14499 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14500 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014501 }
developere40952c2023-06-15 18:46:43 +080014502
14503 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", mgmtpwr_file);
14504 if (os_snprintf_error(sizeof(cmd), res)) {
14505 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14506 return RETURN_ERR;
14507 }
developera3511852023-06-14 14:12:59 +080014508 _syscmd(cmd, buf, sizeof(buf));
developer5b23cd02023-07-19 20:26:03 +080014509 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080014510 if (hal_strtol(buf, 10, &tmp) < 0) {
14511 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080014512 }
developerc14d83a2023-06-29 20:09:42 +080014513 *output_dBm = tmp;
developerd14dff12023-06-28 22:47:44 +080014514 } else
developera1255e42023-05-13 17:45:02 +080014515 *output_dBm = 23;
developera3511852023-06-14 14:12:59 +080014516 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014517}
14518
14519INT wifi_setApManagementFramePowerControl(INT wlanIndex, INT dBm)
14520{
developera1255e42023-05-13 17:45:02 +080014521 char interface_name[16] = {0};
developera1255e42023-05-13 17:45:02 +080014522 char mgmt_pwr_file[128]={0};
14523 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +080014524 int if_idx, ret = 0;
14525 struct nl_msg *msg = NULL;
14526 struct nlattr * msg_data = NULL;
14527 struct mtk_nl80211_param param;
14528 struct unl unl_ins;
14529 char power[16] = {0};
developere40952c2023-06-15 18:46:43 +080014530 int res;
developera1255e42023-05-13 17:45:02 +080014531
14532 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14533
14534 if (wifi_GetInterfaceName(wlanIndex, interface_name) != RETURN_OK)
14535 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +080014536
14537 if_idx = if_nametoindex(interface_name);
14538 /*init mtk nl80211 vendor cmd*/
14539 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
14540 param.if_type = NL80211_ATTR_IFINDEX;
14541 param.if_idx = if_idx;
14542
14543 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14544 if (ret) {
14545 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14546 return RETURN_ERR;
14547 }
14548
14549 /*add mtk vendor cmd data*/
developere40952c2023-06-15 18:46:43 +080014550 res = snprintf(power, sizeof(power), "%d", dBm);
14551 if (os_snprintf_error(sizeof(power), res)) {
14552 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14553 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014554 }
developere40952c2023-06-15 18:46:43 +080014555
developerfead3972023-05-25 20:15:02 +080014556 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_MGMT, strlen(power), power)) {
14557 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14558 nlmsg_free(msg);
14559 goto err;
14560 }
14561
14562 /*send mtk nl80211 vendor msg*/
14563 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14564 if (ret) {
14565 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14566 goto err;
14567 }
14568
14569 /*deinit mtk nl80211 vendor msg*/
14570 mtk_nl80211_deint(&unl_ins);
14571 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14572
developere40952c2023-06-15 18:46:43 +080014573 res = snprintf(mgmt_pwr_file, sizeof(mgmt_pwr_file), "%s%d.txt", MGMT_POWER_CTRL, wlanIndex);
14574 if (os_snprintf_error(sizeof(mgmt_pwr_file), res)) {
14575 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14576 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014577 }
developere40952c2023-06-15 18:46:43 +080014578
developera1255e42023-05-13 17:45:02 +080014579 f = fopen(mgmt_pwr_file, "w");
14580 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +080014581 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera1255e42023-05-13 17:45:02 +080014582 return RETURN_ERR;
14583 }
14584 fprintf(f, "%d", dBm);
developerc14d83a2023-06-29 20:09:42 +080014585 if (fclose(f) == EOF)
14586 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14587
developer72fb0bb2023-01-11 09:46:29 +080014588 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +080014589err:
14590 mtk_nl80211_deint(&unl_ins);
14591 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14592 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014593}
14594INT wifi_getRadioDcsChannelMetrics(INT radioIndex,wifi_channelMetrics_t *input_output_channelMetrics_array,INT size)
14595{
developera3511852023-06-14 14:12:59 +080014596 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014597}
14598INT wifi_setRadioDcsDwelltime(INT radioIndex, INT ms)
14599{
developera3511852023-06-14 14:12:59 +080014600 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014601}
14602INT wifi_getRadioDcsDwelltime(INT radioIndex, INT *ms)
14603{
developera3511852023-06-14 14:12:59 +080014604 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014605}
14606INT wifi_setRadioDcsScanning(INT radioIndex, BOOL enable)
14607{
developera3511852023-06-14 14:12:59 +080014608 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014609}
14610INT wifi_setBSSTransitionActivation(UINT apIndex, BOOL activate)
14611{
developera3511852023-06-14 14:12:59 +080014612 char config_file[MAX_BUF_SIZE] = {0};
14613 struct params list;
developere40952c2023-06-15 18:46:43 +080014614 int res;
developer72fb0bb2023-01-11 09:46:29 +080014615
developera3511852023-06-14 14:12:59 +080014616 list.name = "bss_transition";
14617 list.value = activate?"1":"0";
developere40952c2023-06-15 18:46:43 +080014618 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
14619 if (os_snprintf_error(sizeof(config_file), res)) {
14620 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14621 return RETURN_ERR;
14622 }
developera3511852023-06-14 14:12:59 +080014623 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080014624
developera3511852023-06-14 14:12:59 +080014625 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014626}
14627wifi_apAuthEvent_callback apAuthEvent_cb = NULL;
14628
14629void wifi_apAuthEvent_callback_register(wifi_apAuthEvent_callback callback_proc)
14630{
developera3511852023-06-14 14:12:59 +080014631 return;
developer72fb0bb2023-01-11 09:46:29 +080014632}
14633
14634INT wifi_setApCsaDeauth(INT apIndex, INT mode)
14635{
developera3511852023-06-14 14:12:59 +080014636 // TODO Implement me!
14637 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014638}
14639
14640INT wifi_setApScanFilter(INT apIndex, INT mode, CHAR *essid)
14641{
developera3511852023-06-14 14:12:59 +080014642 char file_name[128] = {0};
14643 FILE *f = NULL;
14644 int max_num_radios = 0;
developer75bd10c2023-06-27 11:34:08 +080014645 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080014646
developera3511852023-06-14 14:12:59 +080014647 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014648
developera3511852023-06-14 14:12:59 +080014649 wifi_getMaxRadioNumber(&max_num_radios);
14650 if (essid == NULL || strlen(essid) == 0 || apIndex == -1) {
14651 for (int index = 0; index < max_num_radios; index++) {
developere40952c2023-06-15 18:46:43 +080014652 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, index);
14653 if (os_snprintf_error(sizeof(file_name), res)) {
14654 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14655 return RETURN_ERR;
14656 }
14657
developera3511852023-06-14 14:12:59 +080014658 f = fopen(file_name, "w");
14659 if (f == NULL)
14660 return RETURN_ERR;
14661 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014662 if (mode) {
14663 ret = fprintf(f, "%s", essid);
14664 if (ret < 0)
14665 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14666 }
developer72fb0bb2023-01-11 09:46:29 +080014667
developerd14dff12023-06-28 22:47:44 +080014668 if (fclose(f) != 0) {
14669 wifi_debug(DEBUG_ERROR, "fclose fail\n");
14670 return RETURN_ERR;
14671 }
developera3511852023-06-14 14:12:59 +080014672 }
14673 } else { // special case, need to set AP's SSID as filter for each radio.
developere40952c2023-06-15 18:46:43 +080014674 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, apIndex);
14675 if (os_snprintf_error(sizeof(file_name), res)) {
14676 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14677 return RETURN_ERR;
14678 }
14679
developera3511852023-06-14 14:12:59 +080014680 f = fopen(file_name, "w");
14681 if (f == NULL)
14682 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014683
developera3511852023-06-14 14:12:59 +080014684 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014685 if (mode) {
14686 ret = fprintf(f, "%s", essid);
14687 if (ret < 0)
14688 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14689 }
developer72fb0bb2023-01-11 09:46:29 +080014690
developer37646972023-06-29 10:58:43 +080014691 if (fclose(f) == EOF) {
14692 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14693 return RETURN_ERR;
14694 }
developera3511852023-06-14 14:12:59 +080014695 }
developer72fb0bb2023-01-11 09:46:29 +080014696
developera3511852023-06-14 14:12:59 +080014697 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14698 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014699}
14700
14701INT wifi_pushRadioChannel(INT radioIndex, UINT channel)
14702{
developera3511852023-06-14 14:12:59 +080014703 // TODO Implement me!
14704 //Apply wifi_pushRadioChannel() instantly
14705 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014706}
14707
14708INT wifi_setRadioStatsEnable(INT radioIndex, BOOL enable)
14709{
developera3511852023-06-14 14:12:59 +080014710 // TODO Implement me!
14711 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014712}
14713
developer72fb0bb2023-01-11 09:46:29 +080014714
developera3511852023-06-14 14:12:59 +080014715static int tidStats_callback(struct nl_msg *msg, void *arg) {
14716 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14717 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
14718 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
14719 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1],*tidattr;
14720 int rem , tid_index = 0;
developer72fb0bb2023-01-11 09:46:29 +080014721
developera3511852023-06-14 14:12:59 +080014722 wifi_associated_dev_tid_stats_t *out = (wifi_associated_dev_tid_stats_t*)arg;
14723 wifi_associated_dev_tid_entry_t *stats_entry;
developer72fb0bb2023-01-11 09:46:29 +080014724
developera3511852023-06-14 14:12:59 +080014725 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
14726 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
14727 };
14728 static struct nla_policy tid_policy[NL80211_TID_STATS_MAX + 1] = {
14729 [NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
14730 };
developer72fb0bb2023-01-11 09:46:29 +080014731
developera3511852023-06-14 14:12:59 +080014732 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
14733 genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080014734
developer72fb0bb2023-01-11 09:46:29 +080014735
developera3511852023-06-14 14:12:59 +080014736 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014737 wifi_debug(DEBUG_ERROR, "station stats missing!\n");
developera3511852023-06-14 14:12:59 +080014738 return NL_SKIP;
14739 }
developer72fb0bb2023-01-11 09:46:29 +080014740
developera3511852023-06-14 14:12:59 +080014741 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
14742 tb[NL80211_ATTR_STA_INFO],
14743 stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014744 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014745 return NL_SKIP;
14746 }
developer72fb0bb2023-01-11 09:46:29 +080014747
developera3511852023-06-14 14:12:59 +080014748 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14749 nla_for_each_nested(tidattr, sinfo[NL80211_STA_INFO_TID_STATS], rem)
14750 {
14751 stats_entry = &out->tid_array[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014752
developera3511852023-06-14 14:12:59 +080014753 stats_entry->tid = tid_index;
14754 stats_entry->ac = _tid_ac_index_get[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014755
developera3511852023-06-14 14:12:59 +080014756 if(sinfo[NL80211_STA_INFO_TID_STATS])
14757 {
14758 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,tidattr, tid_policy)) {
14759 printf("failed to parse nested stats attributes!");
14760 return NL_SKIP;
14761 }
14762 }
14763 if(stats_info[NL80211_TID_STATS_TX_MSDU])
14764 stats_entry->num_msdus = (unsigned long long)nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
developer72fb0bb2023-01-11 09:46:29 +080014765
developera3511852023-06-14 14:12:59 +080014766 if(tid_index < (PS_MAX_TID - 1))
14767 tid_index++;
14768 }
14769 }
14770 //ToDo: sum_time_ms, ewma_time_ms
14771 return NL_SKIP;
14772}
developer72fb0bb2023-01-11 09:46:29 +080014773
developera3511852023-06-14 14:12:59 +080014774INT wifi_getApAssociatedDeviceTidStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_tid_stats_t *tid_stats, ULLONG *handle)
14775{
14776 Netlink nl;
14777 char if_name[IF_NAME_SIZE];
14778 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014779 int res;
developer72fb0bb2023-01-11 09:46:29 +080014780
developera3511852023-06-14 14:12:59 +080014781 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
14782 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014783
developere40952c2023-06-15 18:46:43 +080014784 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
14785 if (os_snprintf_error(sizeof(if_name), res)) {
14786 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14787 return RETURN_ERR;
14788 }
developer72fb0bb2023-01-11 09:46:29 +080014789
developera3511852023-06-14 14:12:59 +080014790 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080014791
developera3511852023-06-14 14:12:59 +080014792 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080014793 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080014794 return -1;
14795 }
developer72fb0bb2023-01-11 09:46:29 +080014796
developera3511852023-06-14 14:12:59 +080014797 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080014798
developera3511852023-06-14 14:12:59 +080014799 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080014800 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080014801 nlfree(&nl);
14802 return -2;
14803 }
developer72fb0bb2023-01-11 09:46:29 +080014804
developera3511852023-06-14 14:12:59 +080014805 genlmsg_put(msg,
14806 NL_AUTO_PID,
14807 NL_AUTO_SEQ,
14808 nl.id,
14809 0,
14810 0,
14811 NL80211_CMD_GET_STATION,
14812 0);
developer72fb0bb2023-01-11 09:46:29 +080014813
developera3511852023-06-14 14:12:59 +080014814 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
14815 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
14816 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,tidStats_callback,tid_stats);
14817 nl_send_auto_complete(nl.socket, msg);
14818 nl_recvmsgs(nl.socket, nl.cb);
14819 nlmsg_free(msg);
14820 nlfree(&nl);
14821 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014822}
14823
14824
14825INT wifi_startNeighborScan(INT apIndex, wifi_neighborScanMode_t scan_mode, INT dwell_time, UINT chan_num, UINT *chan_list)
14826{
developera3511852023-06-14 14:12:59 +080014827 char interface_name[16] = {0};
14828 char cmd[128]={0};
14829 char buf[128]={0};
14830 int freq = 0;
developere40952c2023-06-15 18:46:43 +080014831 int res;
developer72fb0bb2023-01-11 09:46:29 +080014832
developera3511852023-06-14 14:12:59 +080014833 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014834
developera3511852023-06-14 14:12:59 +080014835 // full mode is used to scan all channels.
14836 // multiple channels is ambiguous, iw can not set multiple frequencies in one time.
14837 if (scan_mode != WIFI_RADIO_SCAN_MODE_FULL)
14838 ieee80211_channel_to_frequency(chan_list[0], &freq);
developer72fb0bb2023-01-11 09:46:29 +080014839
developera3511852023-06-14 14:12:59 +080014840 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
14841 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014842
developera3511852023-06-14 14:12:59 +080014843 if (freq)
developere40952c2023-06-15 18:46:43 +080014844 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan trigger duration %d freq %d", interface_name, dwell_time, freq);
developera3511852023-06-14 14:12:59 +080014845 else
developere40952c2023-06-15 18:46:43 +080014846 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan trigger duration %d", interface_name, dwell_time);
14847 if (os_snprintf_error(sizeof(cmd), res)) {
14848 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14849 return RETURN_ERR;
14850 }
developer72fb0bb2023-01-11 09:46:29 +080014851
developera3511852023-06-14 14:12:59 +080014852 _syscmd(cmd, buf, sizeof(buf));
14853 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014854
developera3511852023-06-14 14:12:59 +080014855 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014856}
14857
14858
14859INT wifi_steering_setGroup(UINT steeringgroupIndex, wifi_steering_apConfig_t *cfg_2, wifi_steering_apConfig_t *cfg_5)
14860{
developera3511852023-06-14 14:12:59 +080014861 // TODO Implement me!
14862 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014863}
14864
14865INT wifi_steering_clientSet(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_steering_clientConfig_t *config)
14866{
developera3511852023-06-14 14:12:59 +080014867 // TODO Implement me!
14868 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014869}
14870
14871INT wifi_steering_clientRemove(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14872{
developera3511852023-06-14 14:12:59 +080014873 // TODO Implement me!
14874 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014875}
14876
14877INT wifi_steering_clientMeasure(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14878{
developera3511852023-06-14 14:12:59 +080014879 // TODO Implement me!
14880 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014881}
14882
14883INT wifi_steering_clientDisconnect(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_disconnectType_t type, UINT reason)
14884{
developera3511852023-06-14 14:12:59 +080014885 // TODO Implement me!
14886 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014887}
14888
14889INT wifi_steering_eventRegister(wifi_steering_eventCB_t event_cb)
14890{
developera3511852023-06-14 14:12:59 +080014891 // TODO Implement me!
14892 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014893}
14894
14895INT wifi_steering_eventUnregister(void)
14896{
developera3511852023-06-14 14:12:59 +080014897 // TODO Implement me!
14898 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014899}
14900
14901INT wifi_delApAclDevices(INT apIndex)
14902{
developer7e4a2a62023-04-06 19:56:03 +080014903 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +080014904 struct unl unl_ins;
14905 int if_idx = 0, ret = 0;
14906 struct nl_msg *msg = NULL;
14907 struct nlattr * msg_data = NULL;
14908 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +080014909
developer7e4a2a62023-04-06 19:56:03 +080014910 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
14911 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080014912 if_idx = if_nametoindex(inf_name);
14913 if (!if_idx) {
14914 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
14915 return RETURN_ERR;
14916 }
14917 /*init mtk nl80211 vendor cmd*/
14918 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
14919 param.if_type = NL80211_ATTR_IFINDEX;
14920 param.if_idx = if_idx;
14921 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14922 if (ret) {
14923 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14924 return RETURN_ERR;
14925 }
14926 /*add mtk vendor cmd data*/
14927 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_CLEAR_ALL)) {
14928 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14929 nlmsg_free(msg);
14930 goto err;
14931 }
14932 /*send mtk nl80211 vendor msg*/
14933 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14934 if (ret) {
14935 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14936 goto err;
14937 }
14938 /*deinit mtk nl80211 vendor msg*/
14939 mtk_nl80211_deint(&unl_ins);
14940 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14941 return RETURN_OK;
14942err:
14943 mtk_nl80211_deint(&unl_ins);
14944 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14945 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014946
developera3511852023-06-14 14:12:59 +080014947 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014948}
14949
developer72fb0bb2023-01-11 09:46:29 +080014950static int rxStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080014951 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14952 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080014953 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
14954 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080014955 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
14956 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080014957
developera3511852023-06-14 14:12:59 +080014958 nla_parse(tb,
14959 NL80211_ATTR_MAX,
14960 genlmsg_attrdata(gnlh, 0),
14961 genlmsg_attrlen(gnlh, 0),
14962 NULL);
developer72fb0bb2023-01-11 09:46:29 +080014963
developera3511852023-06-14 14:12:59 +080014964 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014965 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080014966 return NL_SKIP;
14967 }
developer72fb0bb2023-01-11 09:46:29 +080014968
developera3511852023-06-14 14:12:59 +080014969 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014970 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014971 return NL_SKIP;
14972 }
14973 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080014974
developera3511852023-06-14 14:12:59 +080014975 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080014976
developera3511852023-06-14 14:12:59 +080014977 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
14978 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy )) {
developer75bd10c2023-06-27 11:34:08 +080014979 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080014980 return NL_SKIP;
14981 }
14982 }
developer72fb0bb2023-01-11 09:46:29 +080014983
developera3511852023-06-14 14:12:59 +080014984 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14985 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
14986 printf("failed to parse nested stats attributes!");
14987 return NL_SKIP;
14988 }
14989 }
14990 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
developer72fb0bb2023-01-11 09:46:29 +080014991
developera3511852023-06-14 14:12:59 +080014992 if( nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]) )
14993 {
14994 printf("Type is VHT\n");
14995 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
14996 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->nss = nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]);
developer72fb0bb2023-01-11 09:46:29 +080014997
developera3511852023-06-14 14:12:59 +080014998 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
14999 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 1;
15000 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
15001 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
15002 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
15003 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
15004 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
15005 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
15006 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]) )
15007 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
15008 } else {
15009 printf(" OFDM or CCK \n");
15010 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
15011 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->nss = 0;
15012 }
15013 }
developer72fb0bb2023-01-11 09:46:29 +080015014
developera3511852023-06-14 14:12:59 +080015015 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
developereff896f2023-05-29 14:52:55 +080015016 if(rinfo[NL80211_RATE_INFO_MCS])
15017 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
15018 }
developera3511852023-06-14 14:12:59 +080015019 if (sinfo[NL80211_STA_INFO_RX_BYTES64])
developereff896f2023-05-29 14:52:55 +080015020 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]);
15021 else if (sinfo[NL80211_STA_INFO_RX_BYTES])
15022 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
developer72fb0bb2023-01-11 09:46:29 +080015023
developera3511852023-06-14 14:12:59 +080015024 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
15025 if (stats_info[NL80211_TID_STATS_RX_MSDU])
developereff896f2023-05-29 14:52:55 +080015026 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_RX_MSDU]);
15027 }
developer72fb0bb2023-01-11 09:46:29 +080015028
developereff896f2023-05-29 14:52:55 +080015029 if (sinfo[NL80211_STA_INFO_SIGNAL])
15030 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->rssi_combined = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
15031 //Assigning 0 for RETRIES ,PPDUS and MPDUS as we dont have rx retries attribute in libnl_3.3.0
15032 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->retries = 0;
15033 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->ppdus = 0;
15034 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = 0;
15035 //rssi_array need to be filled
15036 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015037}
developer72fb0bb2023-01-11 09:46:29 +080015038
15039INT wifi_getApAssociatedDeviceRxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_rx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
15040{
developera3511852023-06-14 14:12:59 +080015041 Netlink nl;
15042 char if_name[32];
15043 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
15044 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015045
developera3511852023-06-14 14:12:59 +080015046 *output_array_size = sizeof(wifi_associated_dev_rate_info_rx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080015047
developera3511852023-06-14 14:12:59 +080015048 if (*output_array_size <= 0)
15049 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015050
developera3511852023-06-14 14:12:59 +080015051 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015052
developera3511852023-06-14 14:12:59 +080015053 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080015054 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015055 return 0;
15056 }
developer72fb0bb2023-01-11 09:46:29 +080015057
developera3511852023-06-14 14:12:59 +080015058 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015059
developera3511852023-06-14 14:12:59 +080015060 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080015061 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015062 nlfree(&nl);
15063 return 0;
15064 }
developer72fb0bb2023-01-11 09:46:29 +080015065
developera3511852023-06-14 14:12:59 +080015066 genlmsg_put(msg,
15067 NL_AUTO_PID,
15068 NL_AUTO_SEQ,
15069 nl.id,
15070 0,
15071 0,
15072 NL80211_CMD_GET_STATION,
15073 0);
developer72fb0bb2023-01-11 09:46:29 +080015074
developera3511852023-06-14 14:12:59 +080015075 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, *clientMacAddress);
15076 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15077 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, rxStatsInfo_callback, stats_array);
15078 nl_send_auto_complete(nl.socket, msg);
15079 nl_recvmsgs(nl.socket, nl.cb);
15080 nlmsg_free(msg);
15081 nlfree(&nl);
15082 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015083}
15084
developer72fb0bb2023-01-11 09:46:29 +080015085static int txStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080015086 struct nlattr *tb[NL80211_ATTR_MAX + 1];
15087 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080015088 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
15089 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080015090 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
15091 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080015092
developera3511852023-06-14 14:12:59 +080015093 nla_parse(tb,
15094 NL80211_ATTR_MAX,
15095 genlmsg_attrdata(gnlh, 0),
15096 genlmsg_attrlen(gnlh, 0),
15097 NULL);
developer72fb0bb2023-01-11 09:46:29 +080015098
developera3511852023-06-14 14:12:59 +080015099 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080015100 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080015101 return NL_SKIP;
15102 }
developer72fb0bb2023-01-11 09:46:29 +080015103
developera3511852023-06-14 14:12:59 +080015104 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080015105 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080015106 return NL_SKIP;
15107 }
developer72fb0bb2023-01-11 09:46:29 +080015108
developera3511852023-06-14 14:12:59 +080015109 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080015110
developera3511852023-06-14 14:12:59 +080015111 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080015112
developera3511852023-06-14 14:12:59 +080015113 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
15114 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
developer75bd10c2023-06-27 11:34:08 +080015115 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080015116 return NL_SKIP;
15117 }
15118 }
developer72fb0bb2023-01-11 09:46:29 +080015119
developera3511852023-06-14 14:12:59 +080015120 if(sinfo[NL80211_STA_INFO_TID_STATS])
15121 {
15122 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
15123 printf("failed to parse nested stats attributes!");
15124 return NL_SKIP;
15125 }
15126 }
15127 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
15128 if(nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]))
15129 {
15130 printf("Type is VHT\n");
15131 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
15132 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->nss = nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]);
developer72fb0bb2023-01-11 09:46:29 +080015133
developera3511852023-06-14 14:12:59 +080015134 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
15135 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 1;
15136 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
15137 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15138 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
15139 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15140 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
15141 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15142 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]))
15143 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15144 }
15145 else
15146 {
15147 printf(" OFDM or CCK \n");
15148 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15149 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->nss = 0;
15150 }
15151 }
developer72fb0bb2023-01-11 09:46:29 +080015152
developera3511852023-06-14 14:12:59 +080015153 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
15154 if(rinfo[NL80211_RATE_INFO_MCS])
15155 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
15156 }
developer72fb0bb2023-01-11 09:46:29 +080015157
developera3511852023-06-14 14:12:59 +080015158 if(sinfo[NL80211_STA_INFO_TX_BYTES64])
15159 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]);
15160 else if (sinfo[NL80211_STA_INFO_TX_BYTES])
15161 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
developer72fb0bb2023-01-11 09:46:29 +080015162
developera3511852023-06-14 14:12:59 +080015163 //Assigning 0 for mpdus and ppdus , as we do not have attributes in netlink
15164 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
15165 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
developer72fb0bb2023-01-11 09:46:29 +080015166
developera3511852023-06-14 14:12:59 +080015167 if(sinfo[NL80211_STA_INFO_TID_STATS]) {
15168 if(stats_info[NL80211_TID_STATS_TX_MSDU])
15169 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
15170 }
developer72fb0bb2023-01-11 09:46:29 +080015171
developera3511852023-06-14 14:12:59 +080015172 if(sinfo[NL80211_STA_INFO_TX_RETRIES])
15173 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
developer72fb0bb2023-01-11 09:46:29 +080015174
developera3511852023-06-14 14:12:59 +080015175 if(sinfo[NL80211_STA_INFO_TX_FAILED] && sinfo[NL80211_STA_INFO_TX_PACKETS])
15176 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->attempts = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]) + nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
developer72fb0bb2023-01-11 09:46:29 +080015177
developera3511852023-06-14 14:12:59 +080015178 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015179}
developer72fb0bb2023-01-11 09:46:29 +080015180
15181INT wifi_getApAssociatedDeviceTxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_tx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
15182{
developera3511852023-06-14 14:12:59 +080015183 Netlink nl;
15184 char if_name[IF_NAME_SIZE];
15185 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015186 int res;
15187
developera3511852023-06-14 14:12:59 +080015188 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
15189 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015190
developera3511852023-06-14 14:12:59 +080015191 *output_array_size = sizeof(wifi_associated_dev_rate_info_tx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080015192
developera3511852023-06-14 14:12:59 +080015193 if (*output_array_size <= 0)
15194 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015195
developere40952c2023-06-15 18:46:43 +080015196 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
15197 if (os_snprintf_error(sizeof(if_name), res)) {
15198 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15199 return RETURN_ERR;
15200 }
developer72fb0bb2023-01-11 09:46:29 +080015201
developera3511852023-06-14 14:12:59 +080015202 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015203
developera3511852023-06-14 14:12:59 +080015204 if(nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080015205 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015206 return 0;
15207 }
developer72fb0bb2023-01-11 09:46:29 +080015208
developera3511852023-06-14 14:12:59 +080015209 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015210
developera3511852023-06-14 14:12:59 +080015211 if(!msg) {
developer75bd10c2023-06-27 11:34:08 +080015212 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015213 nlfree(&nl);
15214 return 0;
15215 }
developer72fb0bb2023-01-11 09:46:29 +080015216
developera3511852023-06-14 14:12:59 +080015217 genlmsg_put(msg,
15218 NL_AUTO_PID,
15219 NL_AUTO_SEQ,
15220 nl.id,
15221 0,
15222 0,
15223 NL80211_CMD_GET_STATION,
15224 0);
developer72fb0bb2023-01-11 09:46:29 +080015225
developera3511852023-06-14 14:12:59 +080015226 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
15227 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15228 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, txStatsInfo_callback, stats_array);
15229 nl_send_auto_complete(nl.socket, msg);
15230 nl_recvmsgs(nl.socket, nl.cb);
15231 nlmsg_free(msg);
15232 nlfree(&nl);
15233 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015234}
15235
15236INT wifi_getBSSTransitionActivation(UINT apIndex, BOOL *activate)
15237{
developera3511852023-06-14 14:12:59 +080015238 // TODO Implement me!
15239 char buf[MAX_BUF_SIZE] = {0};
15240 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015241 int res;
developer72fb0bb2023-01-11 09:46:29 +080015242
developere40952c2023-06-15 18:46:43 +080015243 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15244 if (os_snprintf_error(sizeof(config_file), res)) {
15245 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15246 return RETURN_ERR;
15247 }
developera3511852023-06-14 14:12:59 +080015248 wifi_hostapdRead(config_file, "bss_transition", buf, sizeof(buf));
15249 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015250
developera3511852023-06-14 14:12:59 +080015251 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015252}
15253
15254INT wifi_setNeighborReportActivation(UINT apIndex, BOOL activate)
15255{
developera3511852023-06-14 14:12:59 +080015256 char config_file[MAX_BUF_SIZE] = {0};
15257 struct params list;
developer75bd10c2023-06-27 11:34:08 +080015258 int res;
developer72fb0bb2023-01-11 09:46:29 +080015259
developera3511852023-06-14 14:12:59 +080015260 list.name = "rrm_neighbor_report";
15261 list.value = activate?"1":"0";
developer75bd10c2023-06-27 11:34:08 +080015262 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15263 if (os_snprintf_error(sizeof(config_file), res)) {
15264 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15265 return RETURN_ERR;
15266 }
developera3511852023-06-14 14:12:59 +080015267 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080015268
developera3511852023-06-14 14:12:59 +080015269 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015270}
15271
15272INT wifi_getNeighborReportActivation(UINT apIndex, BOOL *activate)
15273{
developera3511852023-06-14 14:12:59 +080015274 char buf[32] = {0};
15275 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080015276 int res;
developer72fb0bb2023-01-11 09:46:29 +080015277
developer75bd10c2023-06-27 11:34:08 +080015278 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15279 if (os_snprintf_error(sizeof(config_file), res)) {
15280 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15281 return RETURN_ERR;
15282 }
developera3511852023-06-14 14:12:59 +080015283 wifi_hostapdRead(config_file, "rrm_neighbor_report", buf, sizeof(buf));
15284 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015285
developera3511852023-06-14 14:12:59 +080015286 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015287}
15288#undef HAL_NETLINK_IMPL
15289#ifdef HAL_NETLINK_IMPL
15290static int chanSurveyInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080015291 struct nlattr *tb[NL80211_ATTR_MAX + 1];
15292 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
15293 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
15294 char dev[20];
15295 int freq =0 ;
15296 static int i=0;
developer72fb0bb2023-01-11 09:46:29 +080015297
developera3511852023-06-14 14:12:59 +080015298 wifi_channelStats_t_loc *out = (wifi_channelStats_t_loc*)arg;
developer72fb0bb2023-01-11 09:46:29 +080015299
developera3511852023-06-14 14:12:59 +080015300 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
15301 };
developer72fb0bb2023-01-11 09:46:29 +080015302
developera3511852023-06-14 14:12:59 +080015303 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080015304
developera3511852023-06-14 14:12:59 +080015305 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080015306
developera3511852023-06-14 14:12:59 +080015307 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
15308 fprintf(stderr, "survey data missing!\n");
15309 return NL_SKIP;
15310 }
developer72fb0bb2023-01-11 09:46:29 +080015311
developera3511852023-06-14 14:12:59 +080015312 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,tb[NL80211_ATTR_SURVEY_INFO],survey_policy))
15313 {
15314 fprintf(stderr, "failed to parse nested attributes!\n");
15315 return NL_SKIP;
15316 }
developer72fb0bb2023-01-11 09:46:29 +080015317
15318
developera3511852023-06-14 14:12:59 +080015319 if(out[0].array_size == 1 )
15320 {
15321 if(sinfo[NL80211_SURVEY_INFO_IN_USE])
15322 {
15323 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15324 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15325 out[0].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015326
developera3511852023-06-14 14:12:59 +080015327 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15328 out[0].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15329 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15330 out[0].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15331 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15332 out[0].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15333 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15334 out[0].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15335 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15336 out[0].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15337 if (sinfo[NL80211_SURVEY_INFO_TIME])
15338 out[0].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15339 return NL_STOP;
15340 }
15341 } else {
15342 if ( i <= out[0].array_size ) {
15343 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15344 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15345 out[i].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015346
developera3511852023-06-14 14:12:59 +080015347 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15348 out[i].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15349 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15350 out[i].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15351 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15352 out[i].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15353 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15354 out[i].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15355 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15356 out[i].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15357 if (sinfo[NL80211_SURVEY_INFO_TIME])
15358 out[i].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15359 }
15360 }
developer72fb0bb2023-01-11 09:46:29 +080015361
developera3511852023-06-14 14:12:59 +080015362 i++;
15363 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015364}
15365#endif
15366
15367static int ieee80211_channel_to_frequency(int channel, int *freqMHz)
15368{
developera3511852023-06-14 14:12:59 +080015369 char command[MAX_CMD_SIZE], output[MAX_BUF_SIZE];
15370 FILE *fp;
developere40952c2023-06-15 18:46:43 +080015371 int res;
developer72fb0bb2023-01-11 09:46:29 +080015372
developera3511852023-06-14 14:12:59 +080015373 if(access("/tmp/freq-channel-map.txt", F_OK)==-1)
15374 {
15375 printf("Creating Frequency-Channel Map\n");
developer33f13ba2023-07-12 16:19:06 +080015376 v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
developera3511852023-06-14 14:12:59 +080015377 }
developere40952c2023-06-15 18:46:43 +080015378 res = snprintf(command, sizeof(command), "cat /tmp/freq-channel-map.txt | grep '\\[%d\\]$' | cut -d' ' -f1", channel);
15379 if (os_snprintf_error(sizeof(command), res)) {
15380 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15381 return RETURN_ERR;
15382 }
15383
developera3511852023-06-14 14:12:59 +080015384 if((fp = popen(command, "r")))
15385 {
developerc14d83a2023-06-29 20:09:42 +080015386 if (fgets(output, sizeof(output), fp) == NULL) {
15387 wifi_debug(DEBUG_ERROR, "fgets fail\n");
15388 pclose(fp);
15389 return RETURN_ERR;
15390 }
developera3511852023-06-14 14:12:59 +080015391 *freqMHz = atoi(output);
15392 pclose(fp);
15393 }
developer72fb0bb2023-01-11 09:46:29 +080015394
developera3511852023-06-14 14:12:59 +080015395 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015396}
15397
developer2f79c922023-06-02 17:33:42 +080015398static int get_survey_dump_buf(INT radioIndex, int channel, char *buf, size_t bufsz)
developer72fb0bb2023-01-11 09:46:29 +080015399{
developera3511852023-06-14 14:12:59 +080015400 int freqMHz = -1;
15401 char cmd[MAX_CMD_SIZE] = {'\0'};
15402 char interface_name[16] = {0};
developer32f2a182023-06-27 19:50:41 +080015403 int res;
developer72fb0bb2023-01-11 09:46:29 +080015404
developera3511852023-06-14 14:12:59 +080015405 ieee80211_channel_to_frequency(channel, &freqMHz);
15406 if (freqMHz == -1) {
15407 wifi_dbg_printf("%s: failed to get channel frequency for channel: %d\n", __func__, channel);
15408 return -1;
15409 }
developer72fb0bb2023-01-11 09:46:29 +080015410
developer86035662023-06-28 19:21:12 +080015411 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
15412 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
15413 }
developer32f2a182023-06-27 19:50:41 +080015414 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep -A5 %d | tr -d '\\t'", interface_name, freqMHz);
15415 if (os_snprintf_error(sizeof(cmd), res)) {
15416 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15417 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080015418 }
developer72fb0bb2023-01-11 09:46:29 +080015419
developera3511852023-06-14 14:12:59 +080015420 if (_syscmd(cmd, buf, bufsz) == RETURN_ERR) {
15421 wifi_dbg_printf("%s: failed to execute '%s' for radioIndex=%d\n", __FUNCTION__, cmd, radioIndex);
15422 return -1;
15423 }
developer72fb0bb2023-01-11 09:46:29 +080015424
developera3511852023-06-14 14:12:59 +080015425 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015426}
15427
15428static int fetch_survey_from_buf(INT radioIndex, const char *buf, wifi_channelStats_t *stats)
15429{
developera3511852023-06-14 14:12:59 +080015430 const char *ptr = buf;
15431 char *key = NULL;
15432 char *val = NULL;
15433 char line[256] = { '\0' };
developer72fb0bb2023-01-11 09:46:29 +080015434
developera3511852023-06-14 14:12:59 +080015435 while ((ptr = get_line_from_str_buf(ptr, line))) {
15436 if (strstr(line, "Frequency")) continue;
developer72fb0bb2023-01-11 09:46:29 +080015437
developera3511852023-06-14 14:12:59 +080015438 key = strtok(line, ":");
developerc14d83a2023-06-29 20:09:42 +080015439 if (key == NULL)
15440 continue;
developera3511852023-06-14 14:12:59 +080015441 val = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080015442 if (val == NULL)
15443 continue;
developera3511852023-06-14 14:12:59 +080015444 wifi_dbg_printf("%s: key='%s' val='%s'\n", __func__, key, val);
developer72fb0bb2023-01-11 09:46:29 +080015445
developera3511852023-06-14 14:12:59 +080015446 if (!strcmp(key, "noise")) {
developer37646972023-06-29 10:58:43 +080015447 if (sscanf(val, "%d", &stats->ch_noise) == EOF)
15448 continue;
developera3511852023-06-14 14:12:59 +080015449 if (stats->ch_noise == 0) {
15450 // Workaround for missing noise information.
15451 // Assume -95 for 2.4G and -103 for 5G
15452 if (radioIndex == 0) stats->ch_noise = -95;
15453 if (radioIndex == 1) stats->ch_noise = -103;
15454 }
15455 }
15456 else if (!strcmp(key, "channel active time")) {
developer37646972023-06-29 10:58:43 +080015457 if (sscanf(val, "%llu", &stats->ch_utilization_total) == EOF)
15458 continue;
developera3511852023-06-14 14:12:59 +080015459 }
15460 else if (!strcmp(key, "channel busy time")) {
developer37646972023-06-29 10:58:43 +080015461 if (sscanf(val, "%llu", &stats->ch_utilization_busy) == EOF)
15462 continue;
developera3511852023-06-14 14:12:59 +080015463 }
15464 else if (!strcmp(key, "channel receive time")) {
developer37646972023-06-29 10:58:43 +080015465 if (sscanf(val, "%llu", &stats->ch_utilization_busy_rx) == EOF)
15466 continue;
developera3511852023-06-14 14:12:59 +080015467 }
15468 else if (!strcmp(key, "channel transmit time")) {
developer37646972023-06-29 10:58:43 +080015469 if (sscanf(val, "%llu", &stats->ch_utilization_busy_tx) == EOF)
15470 continue;
developera3511852023-06-14 14:12:59 +080015471 }
15472 };
developer72fb0bb2023-01-11 09:46:29 +080015473
developera3511852023-06-14 14:12:59 +080015474 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015475}
15476
15477INT wifi_getRadioChannelStats(INT radioIndex,wifi_channelStats_t *input_output_channelStats_array,INT array_size)
15478{
developera3511852023-06-14 14:12:59 +080015479 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080015480#ifdef HAL_NETLINK_IMPL
developera3511852023-06-14 14:12:59 +080015481 Netlink nl;
15482 wifi_channelStats_t_loc local[array_size];
15483 char if_name[32];
developer72fb0bb2023-01-11 09:46:29 +080015484
developera3511852023-06-14 14:12:59 +080015485 local[0].array_size = array_size;
developer72fb0bb2023-01-11 09:46:29 +080015486
developera3511852023-06-14 14:12:59 +080015487 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
15488 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015489
developera3511852023-06-14 14:12:59 +080015490 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015491
developera3511852023-06-14 14:12:59 +080015492 if (nl.id < 0) {
developerc14d83a2023-06-29 20:09:42 +080015493 wifi_debug(DEBUG_ERROR,, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015494 return -1;
15495 }
developer72fb0bb2023-01-11 09:46:29 +080015496
developera3511852023-06-14 14:12:59 +080015497 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015498
developera3511852023-06-14 14:12:59 +080015499 if (!msg) {
developerc14d83a2023-06-29 20:09:42 +080015500 wifi_debug(DEBUG_ERROR,, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015501 nlfree(&nl);
15502 return -2;
15503 }
developer72fb0bb2023-01-11 09:46:29 +080015504
developera3511852023-06-14 14:12:59 +080015505 genlmsg_put(msg,
15506 NL_AUTO_PID,
15507 NL_AUTO_SEQ,
15508 nl.id,
15509 0,
15510 NLM_F_DUMP,
15511 NL80211_CMD_GET_SURVEY,
15512 0);
developer72fb0bb2023-01-11 09:46:29 +080015513
developera3511852023-06-14 14:12:59 +080015514 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15515 nl_send_auto_complete(nl.socket, msg);
15516 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,chanSurveyInfo_callback,local);
15517 nl_recvmsgs(nl.socket, nl.cb);
15518 nlmsg_free(msg);
15519 nlfree(&nl);
15520 //Copying the Values
15521 for(int i=0;i<array_size;i++)
15522 {
15523 input_output_channelStats_array[i].ch_number = local[i].ch_number;
15524 input_output_channelStats_array[i].ch_noise = local[i].ch_noise;
15525 input_output_channelStats_array[i].ch_utilization_busy_rx = local[i].ch_utilization_busy_rx;
15526 input_output_channelStats_array[i].ch_utilization_busy_tx = local[i].ch_utilization_busy_tx;
15527 input_output_channelStats_array[i].ch_utilization_busy = local[i].ch_utilization_busy;
15528 input_output_channelStats_array[i].ch_utilization_busy_ext = local[i].ch_utilization_busy_ext;
15529 input_output_channelStats_array[i].ch_utilization_total = local[i].ch_utilization_total;
15530 //TODO: ch_radar_noise, ch_max_80211_rssi, ch_non_80211_noise, ch_utilization_busy_self
15531 }
developer72fb0bb2023-01-11 09:46:29 +080015532#else
developera3511852023-06-14 14:12:59 +080015533 ULONG channel = 0;
15534 int i;
15535 int number_of_channels = array_size;
15536 char buf[512];
developer72fb0bb2023-01-11 09:46:29 +080015537
developera3511852023-06-14 14:12:59 +080015538 if (number_of_channels == 0) {
15539 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK) {
15540 wifi_dbg_printf("%s: cannot get current channel for radioIndex=%d\n", __func__, radioIndex);
15541 return RETURN_ERR;
15542 }
15543 number_of_channels = 1;
15544 input_output_channelStats_array[0].ch_number = channel;
15545 }
developer72fb0bb2023-01-11 09:46:29 +080015546
developera3511852023-06-14 14:12:59 +080015547 for (i = 0; i < number_of_channels; i++) {
developer72fb0bb2023-01-11 09:46:29 +080015548
developera3511852023-06-14 14:12:59 +080015549 input_output_channelStats_array[i].ch_noise = 0;
15550 input_output_channelStats_array[i].ch_utilization_busy_rx = 0;
15551 input_output_channelStats_array[i].ch_utilization_busy_tx = 0;
15552 input_output_channelStats_array[i].ch_utilization_busy = 0;
15553 input_output_channelStats_array[i].ch_utilization_busy_ext = 0; // XXX: unavailable
15554 input_output_channelStats_array[i].ch_utilization_total = 0;
developer72fb0bb2023-01-11 09:46:29 +080015555
developera3511852023-06-14 14:12:59 +080015556 memset(buf, 0, sizeof(buf));
15557 if (get_survey_dump_buf(radioIndex, input_output_channelStats_array[i].ch_number, buf, sizeof(buf))) {
15558 return RETURN_ERR;
15559 }
15560 if (fetch_survey_from_buf(radioIndex, buf, &input_output_channelStats_array[i])) {
15561 wifi_dbg_printf("%s: cannot fetch survey from buf for radioIndex=%d\n", __func__, radioIndex);
15562 return RETURN_ERR;
15563 }
developer72fb0bb2023-01-11 09:46:29 +080015564
developera3511852023-06-14 14:12:59 +080015565 // XXX: fake missing 'self' counter which is not available in iw survey output
15566 // the 'self' counter (a.k.a 'bss') requires Linux Kernel update
15567 input_output_channelStats_array[i].ch_utilization_busy_self = input_output_channelStats_array[i].ch_utilization_busy_rx / 8;
developer72fb0bb2023-01-11 09:46:29 +080015568
developera3511852023-06-14 14:12:59 +080015569 input_output_channelStats_array[i].ch_utilization_busy_rx *= 1000;
15570 input_output_channelStats_array[i].ch_utilization_busy_tx *= 1000;
15571 input_output_channelStats_array[i].ch_utilization_busy_self *= 1000;
15572 input_output_channelStats_array[i].ch_utilization_busy *= 1000;
15573 input_output_channelStats_array[i].ch_utilization_total *= 1000;
developer72fb0bb2023-01-11 09:46:29 +080015574
developera3511852023-06-14 14:12:59 +080015575 wifi_dbg_printf("%s: ch_number=%d ch_noise=%d total=%llu busy=%llu busy_rx=%llu busy_tx=%llu busy_self=%llu busy_ext=%llu\n",
15576 __func__,
15577 input_output_channelStats_array[i].ch_number,
15578 input_output_channelStats_array[i].ch_noise,
15579 input_output_channelStats_array[i].ch_utilization_total,
15580 input_output_channelStats_array[i].ch_utilization_busy,
15581 input_output_channelStats_array[i].ch_utilization_busy_rx,
15582 input_output_channelStats_array[i].ch_utilization_busy_tx,
15583 input_output_channelStats_array[i].ch_utilization_busy_self,
15584 input_output_channelStats_array[i].ch_utilization_busy_ext);
15585 }
developer72fb0bb2023-01-11 09:46:29 +080015586#endif
developera3511852023-06-14 14:12:59 +080015587 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
15588 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015589}
15590#define HAL_NETLINK_IMPL
15591
15592/* Hostapd events */
15593
15594#ifndef container_of
15595#define offset_of(st, m) ((size_t)&(((st *)0)->m))
15596#define container_of(ptr, type, member) \
developera3511852023-06-14 14:12:59 +080015597 ((type *)((char *)ptr - offset_of(type, member)))
developer72fb0bb2023-01-11 09:46:29 +080015598#endif /* container_of */
15599
15600struct ctrl {
developera3511852023-06-14 14:12:59 +080015601 char sockpath[128];
15602 char sockdir[128];
15603 char bss[IFNAMSIZ];
15604 char reply[4096];
15605 int ssid_index;
15606 void (*cb)(struct ctrl *ctrl, int level, const char *buf, size_t len);
15607 void (*overrun)(struct ctrl *ctrl);
15608 struct wpa_ctrl *wpa;
15609 unsigned int ovfl;
15610 size_t reply_len;
15611 int initialized;
15612 ev_timer retry;
15613 ev_timer watchdog;
15614 ev_stat stat;
15615 ev_io io;
developer72fb0bb2023-01-11 09:46:29 +080015616};
15617static wifi_newApAssociatedDevice_callback clients_connect_cb;
15618static wifi_apDisassociatedDevice_callback clients_disconnect_cb;
15619static struct ctrl wpa_ctrl[MAX_APS];
15620static int initialized;
15621
15622static unsigned int ctrl_get_drops(struct ctrl *ctrl)
15623{
developera3511852023-06-14 14:12:59 +080015624 char cbuf[256] = {};
15625 struct msghdr msg = { .msg_control = cbuf, .msg_controllen = sizeof(cbuf) };
15626 struct cmsghdr *cmsg;
15627 unsigned int ovfl = ctrl->ovfl;
developer86035662023-06-28 19:21:12 +080015628 unsigned int drop = 0;
developer72fb0bb2023-01-11 09:46:29 +080015629
developer86035662023-06-28 19:21:12 +080015630 if (recvmsg(ctrl->io.fd, &msg, MSG_DONTWAIT) < 0)
15631 return drop;
developera3511852023-06-14 14:12:59 +080015632 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
15633 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_RXQ_OVFL)
15634 ovfl = *(unsigned int *)CMSG_DATA(cmsg);
developer72fb0bb2023-01-11 09:46:29 +080015635
developera3511852023-06-14 14:12:59 +080015636 drop = ovfl - ctrl->ovfl;
15637 ctrl->ovfl = ovfl;
developer72fb0bb2023-01-11 09:46:29 +080015638
developera3511852023-06-14 14:12:59 +080015639 return drop;
developer72fb0bb2023-01-11 09:46:29 +080015640}
15641
15642static void ctrl_close(struct ctrl *ctrl)
15643{
developera3511852023-06-14 14:12:59 +080015644 if (ctrl->io.cb)
15645 ev_io_stop(EV_DEFAULT_ &ctrl->io);
15646 if (ctrl->retry.cb)
15647 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15648 if (!ctrl->wpa)
15649 return;
developer72fb0bb2023-01-11 09:46:29 +080015650
developera3511852023-06-14 14:12:59 +080015651 wpa_ctrl_detach(ctrl->wpa);
15652 wpa_ctrl_close(ctrl->wpa);
15653 ctrl->wpa = NULL;
15654 printf("WPA_CTRL: closed index=%d\n", ctrl->ssid_index);
developer72fb0bb2023-01-11 09:46:29 +080015655}
15656
15657static void ctrl_process(struct ctrl *ctrl)
15658{
developera3511852023-06-14 14:12:59 +080015659 const char *str;
15660 int drops;
15661 int level;
developer72fb0bb2023-01-11 09:46:29 +080015662
developera3511852023-06-14 14:12:59 +080015663 /* Example events:
15664 *
15665 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19
15666 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19 keyid=sample_keyid
15667 * <3>AP-STA-DISCONNECTED 60:b4:f7:f0:0a:19
15668 * <3>CTRL-EVENT-CONNECTED - Connection to 00:1d:73:73:88:ea completed [id=0 id_str=]
15669 * <3>CTRL-EVENT-DISCONNECTED bssid=00:1d:73:73:88:ea reason=3 locally_generated=1
15670 */
15671 if (!(str = index(ctrl->reply, '>')))
15672 return;
15673 if (sscanf(ctrl->reply, "<%d>", &level) != 1)
15674 return;
developer72fb0bb2023-01-11 09:46:29 +080015675
developera3511852023-06-14 14:12:59 +080015676 str++;
developer72fb0bb2023-01-11 09:46:29 +080015677
developera3511852023-06-14 14:12:59 +080015678 if (strncmp("AP-STA-CONNECTED ", str, 17) == 0) {
15679 if (!(str = index(ctrl->reply, ' ')))
15680 return;
15681 wifi_associated_dev_t sta;
15682 memset(&sta, 0, sizeof(sta));
developer72fb0bb2023-01-11 09:46:29 +080015683
developere75ba632023-06-29 16:03:33 +080015684 if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
15685 &sta.cli_MACAddress[0], &sta.cli_MACAddress[1], &sta.cli_MACAddress[2],
15686 &sta.cli_MACAddress[3], &sta.cli_MACAddress[4], &sta.cli_MACAddress[5]) == EOF) {
15687 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
15688 return;
15689 }
developer72fb0bb2023-01-11 09:46:29 +080015690
developera3511852023-06-14 14:12:59 +080015691 sta.cli_Active=true;
developer72fb0bb2023-01-11 09:46:29 +080015692
developera3511852023-06-14 14:12:59 +080015693 (clients_connect_cb)(ctrl->ssid_index, &sta);
15694 goto handled;
15695 }
developer72fb0bb2023-01-11 09:46:29 +080015696
developera3511852023-06-14 14:12:59 +080015697 if (strncmp("AP-STA-DISCONNECTED ", str, 20) == 0) {
15698 if (!(str = index(ctrl->reply, ' ')))
15699 return;
developer72fb0bb2023-01-11 09:46:29 +080015700
developera3511852023-06-14 14:12:59 +080015701 (clients_disconnect_cb)(ctrl->ssid_index, (char*)str, 0);
15702 goto handled;
15703 }
developer72fb0bb2023-01-11 09:46:29 +080015704
developera3511852023-06-14 14:12:59 +080015705 if (strncmp("CTRL-EVENT-TERMINATING", str, 22) == 0) {
15706 printf("CTRL_WPA: handle TERMINATING event\n");
15707 goto retry;
15708 }
developer72fb0bb2023-01-11 09:46:29 +080015709
developera3511852023-06-14 14:12:59 +080015710 if (strncmp("AP-DISABLED", str, 11) == 0) {
15711 printf("CTRL_WPA: handle AP-DISABLED\n");
15712 goto retry;
15713 }
developer72fb0bb2023-01-11 09:46:29 +080015714
developera3511852023-06-14 14:12:59 +080015715 printf("Event not supported!!\n");
developer72fb0bb2023-01-11 09:46:29 +080015716
15717handled:
15718
developera3511852023-06-14 14:12:59 +080015719 if ((drops = ctrl_get_drops(ctrl))) {
15720 printf("WPA_CTRL: dropped %d messages index=%d\n", drops, ctrl->ssid_index);
15721 if (ctrl->overrun)
15722 ctrl->overrun(ctrl);
15723 }
developer72fb0bb2023-01-11 09:46:29 +080015724
developera3511852023-06-14 14:12:59 +080015725 return;
developer72fb0bb2023-01-11 09:46:29 +080015726
15727retry:
developera3511852023-06-14 14:12:59 +080015728 printf("WPA_CTRL: closing\n");
15729 ctrl_close(ctrl);
15730 printf("WPA_CTRL: retrying from ctrl prcoess\n");
15731 ev_timer_again(EV_DEFAULT_ &ctrl->retry);
developer72fb0bb2023-01-11 09:46:29 +080015732}
15733
15734static void ctrl_ev_cb(EV_P_ struct ev_io *io, int events)
15735{
developera3511852023-06-14 14:12:59 +080015736 struct ctrl *ctrl = container_of(io, struct ctrl, io);
15737 int err;
developer72fb0bb2023-01-11 09:46:29 +080015738
developera3511852023-06-14 14:12:59 +080015739 memset(ctrl->reply, 0, sizeof(ctrl->reply));
15740 ctrl->reply_len = sizeof(ctrl->reply) - 1;
15741 err = wpa_ctrl_recv(ctrl->wpa, ctrl->reply, &ctrl->reply_len);
15742 ctrl->reply[ctrl->reply_len] = 0;
15743 if (err < 0) {
15744 if (errno == EAGAIN || errno == EWOULDBLOCK)
15745 return;
15746 ctrl_close(ctrl);
15747 ev_timer_again(EV_A_ &ctrl->retry);
15748 return;
15749 }
developer72fb0bb2023-01-11 09:46:29 +080015750
developera3511852023-06-14 14:12:59 +080015751 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015752}
15753
15754static int ctrl_open(struct ctrl *ctrl)
15755{
developera3511852023-06-14 14:12:59 +080015756 int fd;
developer72fb0bb2023-01-11 09:46:29 +080015757
developera3511852023-06-14 14:12:59 +080015758 if (ctrl->wpa)
15759 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015760
developera3511852023-06-14 14:12:59 +080015761 ctrl->wpa = wpa_ctrl_open(ctrl->sockpath);
15762 if (!ctrl->wpa)
15763 goto err;
developer72fb0bb2023-01-11 09:46:29 +080015764
developera3511852023-06-14 14:12:59 +080015765 if (wpa_ctrl_attach(ctrl->wpa) < 0)
15766 goto err_close;
developer72fb0bb2023-01-11 09:46:29 +080015767
developera3511852023-06-14 14:12:59 +080015768 fd = wpa_ctrl_get_fd(ctrl->wpa);
15769 if (fd < 0)
15770 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015771
developera3511852023-06-14 14:12:59 +080015772 if (setsockopt(fd, SOL_SOCKET, SO_RXQ_OVFL, (int[]){1}, sizeof(int)) < 0)
15773 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015774
developera3511852023-06-14 14:12:59 +080015775 ev_io_init(&ctrl->io, ctrl_ev_cb, fd, EV_READ);
15776 ev_io_start(EV_DEFAULT_ &ctrl->io);
developer72fb0bb2023-01-11 09:46:29 +080015777
developera3511852023-06-14 14:12:59 +080015778 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015779
15780err_detach:
developera3511852023-06-14 14:12:59 +080015781 wpa_ctrl_detach(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015782err_close:
developera3511852023-06-14 14:12:59 +080015783 wpa_ctrl_close(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015784err:
developera3511852023-06-14 14:12:59 +080015785 ctrl->wpa = NULL;
15786 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015787}
15788
15789static void ctrl_stat_cb(EV_P_ ev_stat *stat, int events)
15790{
developera3511852023-06-14 14:12:59 +080015791 struct ctrl *ctrl = container_of(stat, struct ctrl, stat);
developer72fb0bb2023-01-11 09:46:29 +080015792
developera3511852023-06-14 14:12:59 +080015793 printf("WPA_CTRL: index=%d file state changed\n", ctrl->ssid_index);
15794 ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015795}
15796
15797static void ctrl_retry_cb(EV_P_ ev_timer *timer, int events)
15798{
developera3511852023-06-14 14:12:59 +080015799 struct ctrl *ctrl = container_of(timer, struct ctrl, retry);
developer72fb0bb2023-01-11 09:46:29 +080015800
developera3511852023-06-14 14:12:59 +080015801 printf("WPA_CTRL: index=%d retrying\n", ctrl->ssid_index);
15802 if (ctrl_open(ctrl) == 0) {
15803 printf("WPA_CTRL: retry successful\n");
15804 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15805 }
developer72fb0bb2023-01-11 09:46:29 +080015806}
15807
15808int ctrl_enable(struct ctrl *ctrl)
15809{
developera3511852023-06-14 14:12:59 +080015810 if (ctrl->wpa)
15811 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015812
developera3511852023-06-14 14:12:59 +080015813 if (!ctrl->stat.cb) {
15814 ev_stat_init(&ctrl->stat, ctrl_stat_cb, ctrl->sockpath, 0.);
15815 ev_stat_start(EV_DEFAULT_ &ctrl->stat);
15816 }
developer72fb0bb2023-01-11 09:46:29 +080015817
developera3511852023-06-14 14:12:59 +080015818 if (!ctrl->retry.cb) {
15819 ev_timer_init(&ctrl->retry, ctrl_retry_cb, 0., 5.);
15820 }
developer72fb0bb2023-01-11 09:46:29 +080015821
developera3511852023-06-14 14:12:59 +080015822 return ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015823}
15824
15825static void
15826ctrl_msg_cb(char *buf, size_t len)
15827{
developera3511852023-06-14 14:12:59 +080015828 struct ctrl *ctrl = container_of(buf, struct ctrl, reply);
developer72fb0bb2023-01-11 09:46:29 +080015829
developera3511852023-06-14 14:12:59 +080015830 printf("WPA_CTRL: unsolicited message: index=%d len=%zu msg=%s", ctrl->ssid_index, len, buf);
15831 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015832}
15833
15834static int ctrl_request(struct ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len)
15835{
developera3511852023-06-14 14:12:59 +080015836 int err;
developer72fb0bb2023-01-11 09:46:29 +080015837
developera3511852023-06-14 14:12:59 +080015838 if (!ctrl->wpa)
15839 return -1;
15840 if (*reply_len < 2)
15841 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015842
developera3511852023-06-14 14:12:59 +080015843 (*reply_len)--;
15844 ctrl->reply_len = sizeof(ctrl->reply);
15845 err = wpa_ctrl_request(ctrl->wpa, cmd, cmd_len, ctrl->reply, &ctrl->reply_len, ctrl_msg_cb);
15846 printf("WPA_CTRL: index=%d cmd='%s' err=%d\n", ctrl->ssid_index, cmd, err);
15847 if (err < 0)
15848 return err;
developer72fb0bb2023-01-11 09:46:29 +080015849
developera3511852023-06-14 14:12:59 +080015850 if (ctrl->reply_len > *reply_len)
15851 ctrl->reply_len = *reply_len;
developer72fb0bb2023-01-11 09:46:29 +080015852
developera3511852023-06-14 14:12:59 +080015853 *reply_len = ctrl->reply_len;
15854 memcpy(reply, ctrl->reply, *reply_len);
15855 reply[*reply_len - 1] = 0;
15856 printf("WPA_CTRL: index=%d reply='%s'\n", ctrl->ssid_index, reply);
15857 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015858}
15859
15860static void ctrl_watchdog_cb(EV_P_ ev_timer *timer, int events)
15861{
developera3511852023-06-14 14:12:59 +080015862 const char *pong = "PONG";
15863 const char *ping = "PING";
15864 char reply[1024];
15865 size_t len = sizeof(reply);
15866 int err;
15867 ULONG s, snum;
15868 INT ret;
15869 BOOL status;
developer72fb0bb2023-01-11 09:46:29 +080015870
developera3511852023-06-14 14:12:59 +080015871 printf("WPA_CTRL: watchdog cb\n");
developer72fb0bb2023-01-11 09:46:29 +080015872
developera3511852023-06-14 14:12:59 +080015873 ret = wifi_getSSIDNumberOfEntries(&snum);
15874 if (ret != RETURN_OK) {
15875 printf("%s: failed to get SSID count", __func__);
15876 return;
15877 }
developer72fb0bb2023-01-11 09:46:29 +080015878
developera3511852023-06-14 14:12:59 +080015879 if (snum > MAX_APS) {
15880 printf("more ssid than supported! %lu\n", snum);
15881 return;
15882 }
developer72fb0bb2023-01-11 09:46:29 +080015883
developera3511852023-06-14 14:12:59 +080015884 for (s = 0; s < snum; s++) {
15885 if (wifi_getApEnable(s, &status) != RETURN_OK) {
15886 printf("%s: failed to get AP Enable for index: %lu\n", __func__, s);
15887 continue;
15888 }
15889 if (status == false) continue;
developer72fb0bb2023-01-11 09:46:29 +080015890
developera3511852023-06-14 14:12:59 +080015891 memset(reply, 0, sizeof(reply));
15892 len = sizeof(reply);
15893 printf("WPA_CTRL: pinging index=%d\n", wpa_ctrl[s].ssid_index);
15894 err = ctrl_request(&wpa_ctrl[s], ping, strlen(ping), reply, &len);
15895 if (err == 0 && len > strlen(pong) && !strncmp(reply, pong, strlen(pong)))
15896 continue;
developer72fb0bb2023-01-11 09:46:29 +080015897
developera3511852023-06-14 14:12:59 +080015898 printf("WPA_CTRL: ping timeout index=%d\n", wpa_ctrl[s].ssid_index);
15899 ctrl_close(&wpa_ctrl[s]);
15900 printf("WPA_CTRL: ev_timer_again %lu\n", s);
15901 ev_timer_again(EV_DEFAULT_ &wpa_ctrl[s].retry);
15902 }
developer72fb0bb2023-01-11 09:46:29 +080015903}
15904
15905static int init_wpa()
15906{
developer9ce44382023-06-28 11:09:37 +080015907 int ret = 0;
developera3511852023-06-14 14:12:59 +080015908 ULONG s, snum;
developer72fb0bb2023-01-11 09:46:29 +080015909
developera3511852023-06-14 14:12:59 +080015910 ret = wifi_getSSIDNumberOfEntries(&snum);
15911 if (ret != RETURN_OK) {
15912 printf("%s: failed to get SSID count", __func__);
15913 return RETURN_ERR;
15914 }
developer72fb0bb2023-01-11 09:46:29 +080015915
developera3511852023-06-14 14:12:59 +080015916 if (snum > MAX_APS) {
15917 printf("more ssid than supported! %lu\n", snum);
15918 return RETURN_ERR;
15919 }
developer72fb0bb2023-01-11 09:46:29 +080015920
developera3511852023-06-14 14:12:59 +080015921 for (s = 0; s < snum; s++) {
15922 memset(&wpa_ctrl[s], 0, sizeof(struct ctrl));
developer32f2a182023-06-27 19:50:41 +080015923 ret = snprintf(wpa_ctrl[s].sockpath, sizeof(wpa_ctrl[s].sockpath), "%s%lu", SOCK_PREFIX, s);
15924 if (os_snprintf_error(sizeof(wpa_ctrl[s].sockpath), ret)) {
15925 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15926 return RETURN_ERR;
15927 }
developera3511852023-06-14 14:12:59 +080015928 wpa_ctrl[s].ssid_index = s;
15929 ctrl_enable(&wpa_ctrl[s]);
15930 }
developer72fb0bb2023-01-11 09:46:29 +080015931
developera3511852023-06-14 14:12:59 +080015932 ev_timer_init(&wpa_ctrl->watchdog, ctrl_watchdog_cb, 0., 30.);
15933 ev_timer_again(EV_DEFAULT_ &wpa_ctrl->watchdog);
developer72fb0bb2023-01-11 09:46:29 +080015934
developera3511852023-06-14 14:12:59 +080015935 initialized = 1;
15936 printf("WPA_CTRL: initialized\n");
developer72fb0bb2023-01-11 09:46:29 +080015937
developera3511852023-06-14 14:12:59 +080015938 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015939}
15940
15941void wifi_newApAssociatedDevice_callback_register(wifi_newApAssociatedDevice_callback callback_proc)
15942{
developera3511852023-06-14 14:12:59 +080015943 clients_connect_cb = callback_proc;
15944 if (!initialized)
15945 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015946}
15947
15948void wifi_apDisassociatedDevice_callback_register(wifi_apDisassociatedDevice_callback callback_proc)
15949{
developera3511852023-06-14 14:12:59 +080015950 clients_disconnect_cb = callback_proc;
15951 if (!initialized)
15952 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015953}
15954
15955INT wifi_setBTMRequest(UINT apIndex, CHAR *peerMac, wifi_BTMRequest_t *request)
15956{
developera3511852023-06-14 14:12:59 +080015957 // TODO Implement me!
15958 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015959}
15960
15961INT wifi_setRMBeaconRequest(UINT apIndex, CHAR *peer, wifi_BeaconRequest_t *in_request, UCHAR *out_DialogToken)
15962{
developera3511852023-06-14 14:12:59 +080015963 // TODO Implement me!
15964 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015965}
15966
15967INT wifi_getRadioChannels(INT radioIndex, wifi_channelMap_t *outputMap, INT outputMapSize)
15968{
developera3511852023-06-14 14:12:59 +080015969 int i;
15970 int phyId = -1;
15971 char cmd[256] = {0};
15972 char channel_numbers_buf[256] = {0};
15973 char dfs_state_buf[256] = {0};
15974 char line[256] = {0};
15975 const char *ptr;
15976 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +080015977 int res;
developer72fb0bb2023-01-11 09:46:29 +080015978
developera3511852023-06-14 14:12:59 +080015979 memset(outputMap, 0, outputMapSize*sizeof(wifi_channelMap_t)); // all unused entries should be zero
developer72fb0bb2023-01-11 09:46:29 +080015980
developera3511852023-06-14 14:12:59 +080015981 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
15982 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080015983
developere40952c2023-06-15 18:46:43 +080015984 res = snprintf(cmd, sizeof (cmd), "iw phy phy%d info | grep -e '\\*.*MHz .*dBm' | grep -v '%sno IR\\|5340\\|5480' | awk '{print $4}' | tr -d '[]'", phyId, dfs_enable?"":"radar\\|");
15985 if (os_snprintf_error(sizeof(cmd), res)) {
15986 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15987 return RETURN_ERR;
15988 }
developer72fb0bb2023-01-11 09:46:29 +080015989
developera3511852023-06-14 14:12:59 +080015990 if (_syscmd(cmd, channel_numbers_buf, sizeof(channel_numbers_buf)) == RETURN_ERR) {
15991 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15992 return RETURN_ERR;
15993 }
developer72fb0bb2023-01-11 09:46:29 +080015994
developera3511852023-06-14 14:12:59 +080015995 ptr = channel_numbers_buf;
15996 i = 0;
15997 while ((ptr = get_line_from_str_buf(ptr, line))) {
15998 if (i >= outputMapSize) {
15999 wifi_dbg_printf("%s: DFS map size too small\n", __FUNCTION__);
16000 return RETURN_ERR;
16001 }
developerd14dff12023-06-28 22:47:44 +080016002 if (sscanf(line, "%d", &outputMap[i].ch_number) != 1) {
16003 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
16004 return RETURN_ERR;
16005 }
developerd1824452023-05-18 12:30:04 +080016006
developera3511852023-06-14 14:12:59 +080016007 memset(cmd, 0, sizeof(cmd));
16008 // Below command should fetch string for DFS state (usable, available or unavailable)
16009 // Example line: "DFS state: usable (for 78930 sec)"
16010 if (sprintf(cmd,"iw list | grep -A 2 '\\[%d\\]' | tr -d '\\t' | grep 'DFS state' | awk '{print $3}' | tr -d '\\n'", outputMap[i].ch_number) < 0) {
16011 wifi_dbg_printf("%s: failed to build dfs state command\n", __FUNCTION__);
16012 return RETURN_ERR;
16013 }
developer72fb0bb2023-01-11 09:46:29 +080016014
developera3511852023-06-14 14:12:59 +080016015 memset(dfs_state_buf, 0, sizeof(dfs_state_buf));
16016 if (_syscmd(cmd, dfs_state_buf, sizeof(dfs_state_buf)) == RETURN_ERR) {
16017 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
16018 return RETURN_ERR;
16019 }
developer72fb0bb2023-01-11 09:46:29 +080016020
developera3511852023-06-14 14:12:59 +080016021 wifi_dbg_printf("DFS state = '%s'\n", dfs_state_buf);
developer59fda4f2023-05-16 15:47:38 +080016022
developera3511852023-06-14 14:12:59 +080016023 if (!strcmp(dfs_state_buf, "usable")) {
16024 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_FINISHED;
16025 } else if (!strcmp(dfs_state_buf, "available")) {
16026 outputMap[i].ch_state = CHAN_STATE_DFS_CAC_COMPLETED;
16027 } else if (!strcmp(dfs_state_buf, "unavailable")) {
16028 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_START;
16029 } else {
16030 outputMap[i].ch_state = CHAN_STATE_AVAILABLE;
16031 }
16032 i++;
16033 }
developer40ba1762023-05-13 11:03:49 +080016034
developera3511852023-06-14 14:12:59 +080016035 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +080016036
developera3511852023-06-14 14:12:59 +080016037 wifi_dbg_printf("%s: wrong radio index (%d)\n", __FUNCTION__, radioIndex);
16038 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016039}
16040
16041INT wifi_chan_eventRegister(wifi_chan_eventCB_t eventCb)
16042{
developera3511852023-06-14 14:12:59 +080016043 // TODO Implement me!
16044 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016045}
16046
16047INT wifi_getRadioBandUtilization (INT radioIndex, INT *output_percentage)
16048{
developer0155a502023-06-19 20:33:57 +080016049 int ret = -1;
16050 char inf_name[IF_NAME_SIZE] = {0};
16051 int if_idx = 0;
16052 struct unl unl_ins;
16053 struct nl_msg *msg = NULL;
16054 struct nlattr * msg_data = NULL;
16055 struct mtk_nl80211_param param;
16056 struct mtk_nl80211_cb_data cb_data;
16057 wdev_ap_metric ap_metric;
16058
16059 /*init mtk nl80211 vendor cmd*/
16060
16061 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
16062 return RETURN_ERR;
16063 if_idx = if_nametoindex(inf_name);
16064 if (!if_idx) {
16065 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
16066 return RETURN_ERR;
16067 }
16068
16069 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_STATISTIC;
16070 param.if_type = NL80211_ATTR_IFINDEX;
16071 param.if_idx = if_idx;
16072
16073 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
16074 if (ret) {
16075 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
16076 return RETURN_ERR;
16077 }
16078
16079 /*add mtk vendor cmd data*/
16080
16081 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS, sizeof(wdev_ap_metric), (char *)&ap_metric)) {
16082 wifi_debug(DEBUG_ERROR, "Nla put GET_AP_METRICS attribute error\n");
16083 nlmsg_free(msg);
16084 goto err;
16085 }
16086
16087 /*send mtk nl80211 vendor msg*/
16088 cb_data.out_buf = (char *)output_percentage;
16089 cb_data.out_len = sizeof(wdev_ap_metric);
16090 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_get_ap_metrics, &cb_data);
16091 if (ret) {
16092 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
16093 goto err;
16094 }
16095
16096 /*deinit mtk nl80211 vendor msg*/
16097 mtk_nl80211_deint(&unl_ins);
16098 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
16099 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16100
developera3511852023-06-14 14:12:59 +080016101 return RETURN_OK;
developer0155a502023-06-19 20:33:57 +080016102err:
16103 mtk_nl80211_deint(&unl_ins);
16104 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
16105 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016106}
16107
developer0155a502023-06-19 20:33:57 +080016108
developer72fb0bb2023-01-11 09:46:29 +080016109INT wifi_getApAssociatedClientDiagnosticResult(INT apIndex, char *mac_addr, wifi_associated_dev3_t *dev_conn)
16110{
developera3511852023-06-14 14:12:59 +080016111 // TODO Implement me!
16112 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016113}
16114
16115INT wifi_switchBand(char *interface_name,INT radioIndex,char *freqBand)
16116{
developera3511852023-06-14 14:12:59 +080016117 // TODO API refrence Implementaion is present on RPI hal
16118 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016119}
16120
16121INT wifi_getRadioPercentageTransmitPower(INT apIndex, ULONG *txpwr_pcntg)
16122{
developera3511852023-06-14 14:12:59 +080016123 ULONG pwr_percentage = 0;
developer72fb0bb2023-01-11 09:46:29 +080016124
developera3511852023-06-14 14:12:59 +080016125 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16126 if(txpwr_pcntg == NULL)
developerdaf24792023-06-06 11:40:04 +080016127 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016128
developera1255e42023-05-13 17:45:02 +080016129 wifi_getRadioTransmitPower(apIndex, &pwr_percentage);
16130 *txpwr_pcntg = pwr_percentage;
developera3511852023-06-14 14:12:59 +080016131 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16132 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016133}
16134
16135INT wifi_setZeroDFSState(UINT radioIndex, BOOL enable, BOOL precac)
16136{
developera3511852023-06-14 14:12:59 +080016137 // TODO precac feature.
16138 struct params params[2] = {0};
16139 char config_file[128] = {0};
16140 BOOL dfs_enable = false;
16141 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016142 int res;
developer72fb0bb2023-01-11 09:46:29 +080016143
developera3511852023-06-14 14:12:59 +080016144 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16145 band = wifi_index_to_band(radioIndex);
16146 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
developer72fb0bb2023-01-11 09:46:29 +080016147
developera3511852023-06-14 14:12:59 +080016148 if (dfs_enable == false) {
16149 WIFI_ENTRY_EXIT_DEBUG("Please enable DFS firstly!: %s\n", __func__);
16150 return RETURN_ERR;
16151 }
16152 params[0].name = "DfsZeroWaitDefault";
16153 params[0].value = enable?"1":"0";
16154 params[1].name = "DfsDedicatedZeroWait";
16155 params[1].value = enable?"1":"0";
developere40952c2023-06-15 18:46:43 +080016156 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16157 if (os_snprintf_error(sizeof(config_file), res)) {
16158 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16159 return RETURN_ERR;
16160 }
developera3511852023-06-14 14:12:59 +080016161 wifi_datfileWrite(config_file, params, 2);
16162 wifi_reloadAp(radioIndex);
16163 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016164
developera3511852023-06-14 14:12:59 +080016165 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16166 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016167}
16168
16169INT wifi_getZeroDFSState(UINT radioIndex, BOOL *enable, BOOL *precac)
16170{
developera3511852023-06-14 14:12:59 +080016171 char config_file[128] = {0};
16172 char buf1[32] = {0};
16173 char buf2[32] = {0};
16174 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016175 int res;
developer72fb0bb2023-01-11 09:46:29 +080016176
developera3511852023-06-14 14:12:59 +080016177 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16178 if (NULL == enable || NULL == precac)
16179 return RETURN_ERR;
16180 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080016181 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16182 if (os_snprintf_error(sizeof(config_file), res)) {
16183 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16184 return RETURN_ERR;
16185 }
developera3511852023-06-14 14:12:59 +080016186 wifi_datfileRead(config_file, "DfsZeroWaitDefault", buf1, sizeof(buf1));
16187 wifi_datfileRead(config_file, "DfsDedicatedZeroWait", buf2, sizeof(buf2));
16188 if ((strncmp(buf1, "1", 1) == 0) && (strncmp(buf2, "1", 1) == 0))
16189 *enable = true;
16190 else
16191 *enable = false;
developer72fb0bb2023-01-11 09:46:29 +080016192
developera3511852023-06-14 14:12:59 +080016193 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016194
developera3511852023-06-14 14:12:59 +080016195 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16196 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016197}
16198
16199INT wifi_isZeroDFSSupported(UINT radioIndex, BOOL *supported)
16200{
developera3511852023-06-14 14:12:59 +080016201 *supported = TRUE;
16202 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016203}
16204
16205INT wifi_setDownlinkMuType(INT radio_index, wifi_dl_mu_type_t mu_type)
16206{
developer863a4a62023-06-06 16:55:59 +080016207 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016208 wifi_band band = band_invalid;
16209 char ofdmabuf[32] = {'\0'};
16210 char mimobuf[32] = {'\0'};
16211 char new_ofdmabuf[32] = {'\0'};
16212 char new_mimobuf[32] = {'\0'};
16213 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016214 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16215 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16216 UCHAR bss_cnt = 0;
16217 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016218 int res;
developer72fb0bb2023-01-11 09:46:29 +080016219
developera3511852023-06-14 14:12:59 +080016220 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera1255e42023-05-13 17:45:02 +080016221 if ((mu_type < WIFI_DL_MU_TYPE_NONE)
16222 || (mu_type > WIFI_DL_MU_TYPE_OFDMA_MIMO)) {
16223 printf("%s:mu_type input Error", __func__);
16224 return RETURN_ERR;
16225 }
developera3511852023-06-14 14:12:59 +080016226 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016227 if (band == band_invalid) {
16228 printf("%s:Band Error\n", __func__);
16229 return RETURN_ERR;
16230 }
developere40952c2023-06-15 18:46:43 +080016231 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16232 if (os_snprintf_error(sizeof(dat_file), res)) {
16233 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16234 return RETURN_ERR;
16235 }
16236
developera1255e42023-05-13 17:45:02 +080016237 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016238 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16239 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016240 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16241 get_bssnum_byindex(radio_index, &bss_cnt);
16242 val_cnt = 2*bss_cnt - 1;
16243 WIFI_ENTRY_EXIT_DEBUG("bss number: %d\n", bss_cnt);
16244 if ((val_cnt >= sizeof(new_ofdmabuf))
16245 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016246 printf("%s:bss cnt Error", __func__);
developera1255e42023-05-13 17:45:02 +080016247 return RETURN_ERR;
16248 }
16249 /*translate set value*/
16250 if (mu_type == WIFI_DL_MU_TYPE_NONE) {
16251 strncpy(new_ofdmabuf, str_zero, val_cnt);
16252 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016253 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA) {
developera1255e42023-05-13 17:45:02 +080016254 strncpy(new_ofdmabuf, str_one, val_cnt);
16255 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016256 } else if (mu_type == WIFI_DL_MU_TYPE_MIMO) {
developera1255e42023-05-13 17:45:02 +080016257 strncpy(new_ofdmabuf, str_zero, val_cnt);
16258 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016259 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA_MIMO) {
developera1255e42023-05-13 17:45:02 +080016260 strncpy(new_ofdmabuf, str_one, val_cnt);
16261 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016262 }
developera1255e42023-05-13 17:45:02 +080016263 WIFI_ENTRY_EXIT_DEBUG("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16264 /*same value, not operation*/
16265 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16266 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16267 printf("%s:Reduntant value\n", __func__);
16268 return RETURN_OK;
16269 }
16270 /*modify dat file to new file*/
16271 params[0].name="MuOfdmaDlEnable";
16272 params[0].value=new_ofdmabuf;
16273 params[1].name="MuMimoDlEnable";
16274 params[1].value=new_mimobuf;
16275 wifi_datfileWrite(dat_file, params, 2);
16276 /*hostapd control restarp ap to take effect on these new value*/
16277 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016278 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16279 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016280}
16281
16282INT wifi_getDownlinkMuType(INT radio_index, wifi_dl_mu_type_t *mu_type)
16283{
developer5a333cf2023-06-06 18:18:50 +080016284 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016285 wifi_band band = band_invalid;
16286 char ofdmabuf[32] = {'\0'};
16287 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016288 char *token = NULL;
developerc14d83a2023-06-29 20:09:42 +080016289 long int ofdma = 0;
16290 long int mimo = 0;
developere40952c2023-06-15 18:46:43 +080016291 int res;
developer72fb0bb2023-01-11 09:46:29 +080016292
developera3511852023-06-14 14:12:59 +080016293 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016294
developera3511852023-06-14 14:12:59 +080016295 if (mu_type == NULL)
16296 return RETURN_ERR;
16297 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016298 if (band == band_invalid) {
16299 printf("%s:Band Error\n", __func__);
16300 return RETURN_ERR;
16301 }
developere40952c2023-06-15 18:46:43 +080016302 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16303 if (os_snprintf_error(sizeof(dat_file), res)) {
16304 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16305 return RETURN_ERR;
16306 }
developera1255e42023-05-13 17:45:02 +080016307 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016308 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16309 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016310
developera1255e42023-05-13 17:45:02 +080016311 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016312 if (token == NULL) {
16313 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16314 return RETURN_ERR;
16315 }
developerc14d83a2023-06-29 20:09:42 +080016316 if (hal_strtol(token, 10, &ofdma) < 0) {
16317 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080016318 }
developerc14d83a2023-06-29 20:09:42 +080016319
developera1255e42023-05-13 17:45:02 +080016320 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016321 if (token == NULL) {
16322 wifi_debug(DEBUG_ERROR, "Unexpected strtok fail\n");
16323 return RETURN_ERR;
16324 }
developerc14d83a2023-06-29 20:09:42 +080016325
16326 if (hal_strtol(token, 10, &mimo) < 0) {
16327 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +080016328 }
developerc14d83a2023-06-29 20:09:42 +080016329
developera1255e42023-05-13 17:45:02 +080016330 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d,mimo=%d\n", __func__, ofdma, mimo);
16331 if ((ofdma == 1) && (mimo == 1))
16332 *mu_type = WIFI_DL_MU_TYPE_OFDMA_MIMO;
16333 else if ((ofdma == 0) && (mimo == 1))
16334 *mu_type = WIFI_DL_MU_TYPE_MIMO;
16335 else if ((ofdma == 1) && (mimo == 0))
16336 *mu_type = WIFI_DL_MU_TYPE_OFDMA;
16337 else
16338 *mu_type = WIFI_DL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016339 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16340 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016341}
16342
16343INT wifi_setUplinkMuType(INT radio_index, wifi_ul_mu_type_t mu_type)
16344{
developera3511852023-06-14 14:12:59 +080016345 // hemu onoff=<val> (bitmap- UL MU-MIMO(bit3), DL MU-MIMO(bit2), UL OFDMA(bit1), DL OFDMA(bit0))
developer863a4a62023-06-06 16:55:59 +080016346 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016347 wifi_band band = band_invalid;
16348 char ofdmabuf[32] = {'\0'};
16349 char mimobuf[32] = {'\0'};
16350 char new_ofdmabuf[32] = {'\0'};
16351 char new_mimobuf[32] = {'\0'};
16352 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016353 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16354 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16355 UCHAR bss_cnt = 0;
16356 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016357 int res;
developer72fb0bb2023-01-11 09:46:29 +080016358
developera3511852023-06-14 14:12:59 +080016359 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16360 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016361 if (band == band_invalid) {
16362 printf("%s:Band Error\n", __func__);
16363 return RETURN_ERR;
16364 }
16365 if ((mu_type < WIFI_UL_MU_TYPE_NONE)
16366 || (mu_type > WIFI_UL_MU_TYPE_OFDMA)) {
16367 printf("%s:mu_type input Error\n", __func__);
16368 return RETURN_ERR;
16369 }
developere40952c2023-06-15 18:46:43 +080016370 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16371 if (os_snprintf_error(sizeof(dat_file), res)) {
16372 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16373 return RETURN_ERR;
16374 }
developera1255e42023-05-13 17:45:02 +080016375 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016376 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16377 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016378 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16379 get_bssnum_byindex(radio_index, &bss_cnt);
16380 val_cnt = 2*bss_cnt - 1;
16381 printf("bssNumber:%d,ValCnt:%d\n", bss_cnt, val_cnt);
16382 if ((val_cnt >= sizeof(new_ofdmabuf))
16383 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016384 printf("%s:bss cnt Error\n", __func__);
developera1255e42023-05-13 17:45:02 +080016385 return RETURN_ERR;
16386 }
16387 /*translate set value*/
16388 if (mu_type == WIFI_UL_MU_TYPE_NONE) {
16389 strncpy(new_ofdmabuf, str_zero, val_cnt);
16390 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016391 }
developera1255e42023-05-13 17:45:02 +080016392 if (mu_type == WIFI_UL_MU_TYPE_OFDMA) {
16393 strncpy(new_ofdmabuf, str_one, val_cnt);
16394 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016395 }
developera1255e42023-05-13 17:45:02 +080016396 printf("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16397 /*same value, not operation*/
16398 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16399 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16400 printf("%s:Reduntant value\n", __func__);
16401 return RETURN_OK;
16402 }
16403 /*modify dat file to new file*/
16404 params[0].name="MuOfdmaUlEnable";
16405 params[0].value=new_ofdmabuf;
16406 params[1].name="MuMimoUlEnable";
16407 params[1].value=new_mimobuf;
16408 wifi_datfileWrite(dat_file, params, 2);
16409 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016410 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16411 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016412}
16413
16414INT wifi_getUplinkMuType(INT radio_index, wifi_ul_mu_type_t *mu_type)
16415{
developer863a4a62023-06-06 16:55:59 +080016416 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016417 wifi_band band = band_invalid;
16418 char ofdmabuf[32] = {'\0'};
16419 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016420 char *token = NULL;
16421 UCHAR ofdma = 0;
16422 UCHAR mimo = 0;
developere40952c2023-06-15 18:46:43 +080016423 int res;
developerc14d83a2023-06-29 20:09:42 +080016424 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016425
developera3511852023-06-14 14:12:59 +080016426 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016427
developera3511852023-06-14 14:12:59 +080016428 if (mu_type == NULL)
16429 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016430 band = wifi_index_to_band(radio_index);
16431 if (band == band_invalid) {
16432 printf("%s:Band Error", __func__);
16433 return RETURN_ERR;
16434 }
developere40952c2023-06-15 18:46:43 +080016435 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16436 if (os_snprintf_error(sizeof(dat_file), res)) {
16437 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16438 return RETURN_ERR;
16439 }
developera1255e42023-05-13 17:45:02 +080016440 /*get current value in dat file*/
16441 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16442 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016443
developera1255e42023-05-13 17:45:02 +080016444 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016445 if (token == NULL) {
16446 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16447 return RETURN_ERR;
16448 }
developerc14d83a2023-06-29 20:09:42 +080016449
16450 if (hal_strtoul(token, 10, &tmp) < 0) {
16451 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016452 }
developerc14d83a2023-06-29 20:09:42 +080016453 ofdma = tmp;
developera1255e42023-05-13 17:45:02 +080016454 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016455 if (token == NULL) {
16456 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16457 return RETURN_ERR;
16458 }
developer5b23cd02023-07-19 20:26:03 +080016459
developerc14d83a2023-06-29 20:09:42 +080016460 if (hal_strtoul(token, 10, &tmp) < 0) {
16461 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +080016462 }
developerc14d83a2023-06-29 20:09:42 +080016463 mimo = tmp;
16464
developera1255e42023-05-13 17:45:02 +080016465 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d, mimo=%d\n", __func__, ofdma, mimo);
16466 if ((ofdma == 1) && (mimo == 0))
16467 *mu_type = WIFI_UL_MU_TYPE_OFDMA;
16468 else
16469 *mu_type = WIFI_UL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016470 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16471 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016472}
16473
16474
16475INT wifi_setGuardInterval(INT radio_index, wifi_guard_interval_t guard_interval)
16476{
developera3511852023-06-14 14:12:59 +080016477 char cmd[128] = {0};
16478 char buf[256] = {0};
16479 char config_file[64] = {0};
16480 char GI[8] = {0};
16481 UINT mode_map = 0;
16482 FILE *f = NULL;
16483 wifi_band band = band_invalid;
16484 char dat_file[64] = {'\0'};
16485 struct params params[3];
developere40952c2023-06-15 18:46:43 +080016486 int res;
developer72fb0bb2023-01-11 09:46:29 +080016487
developera3511852023-06-14 14:12:59 +080016488 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016489
developera3511852023-06-14 14:12:59 +080016490 if (wifi_getRadioMode(radio_index, buf, &mode_map) == RETURN_ERR) {
16491 wifi_dbg_printf("%s: wifi_getRadioMode return error\n", __func__);
16492 return RETURN_ERR;
16493 }
developera1255e42023-05-13 17:45:02 +080016494 /*sanity check*/
16495 if (((guard_interval == wifi_guard_interval_1600)
16496 || (guard_interval == wifi_guard_interval_3200))
developerdaf24792023-06-06 11:40:04 +080016497 && ((mode_map & (WIFI_MODE_BE | WIFI_MODE_AX)) == 0)) {
developera3511852023-06-14 14:12:59 +080016498 wifi_dbg_printf("%s: N/AC Mode not support 1600/3200ns GI\n", __func__);
16499 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016500 }
developere40952c2023-06-15 18:46:43 +080016501 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16502 if (os_snprintf_error(sizeof(config_file), res)) {
16503 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16504 return RETURN_ERR;
16505 }
developera3511852023-06-14 14:12:59 +080016506 band = wifi_index_to_band(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080016507
developera3511852023-06-14 14:12:59 +080016508 // Hostapd are not supported HE mode GI 1600, 3200 ns.
16509 if (guard_interval == wifi_guard_interval_800) { // remove all capab about short GI
developere40952c2023-06-15 18:46:43 +080016510 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SHORT-GI-(.){1,2}0\\]//g' %s", config_file);
16511 if (os_snprintf_error(sizeof(cmd), res)) {
16512 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16513 return RETURN_ERR;
16514 }
developera3511852023-06-14 14:12:59 +080016515 _syscmd(cmd, buf, sizeof(buf));
16516 } else if (guard_interval == wifi_guard_interval_400 || guard_interval == wifi_guard_interval_auto){
16517 wifi_hostapdRead(config_file, "ht_capab", buf, sizeof(buf));
16518 if (strstr(buf, "[SHORT-GI-") == NULL) {
developere40952c2023-06-15 18:46:43 +080016519 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[SHORT-GI-20][SHORT-GI-40]/' %s", config_file);
16520 if (os_snprintf_error(sizeof(cmd), res)) {
16521 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16522 return RETURN_ERR;
16523 }
developera3511852023-06-14 14:12:59 +080016524 _syscmd(cmd, buf, sizeof(buf));
16525 }
16526 if (band == band_5) {
16527 wifi_hostapdRead(config_file, "vht_capab", buf, sizeof(buf));
16528 if (strstr(buf, "[SHORT-GI-") == NULL) {
developere40952c2023-06-15 18:46:43 +080016529 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SHORT-GI-80][SHORT-GI-160]/' %s", config_file);
16530 if (os_snprintf_error(sizeof(cmd), res)) {
16531 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16532 return RETURN_ERR;
16533 }
developera3511852023-06-14 14:12:59 +080016534 _syscmd(cmd, buf, sizeof(buf));
16535 }
16536 }
16537 }
16538 /*wifi_reloadAp(radio_index);
developera1255e42023-05-13 17:45:02 +080016539 caller "wifi_setRadioOperatingParameters" have done this step.
16540 */
developere40952c2023-06-15 18:46:43 +080016541 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16542 if (os_snprintf_error(sizeof(dat_file), res)) {
16543 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16544 return RETURN_ERR;
16545 }
developera3511852023-06-14 14:12:59 +080016546 if (guard_interval == wifi_guard_interval_400) {
developera1255e42023-05-13 17:45:02 +080016547 params[0].name = "HT_GI";
16548 params[0].value = "1";
16549 params[1].name = "VHT_SGI";
16550 params[1].value = "1";
16551 wifi_datfileWrite(dat_file, params, 2);
developer32f2a182023-06-27 19:50:41 +080016552 memcpy(GI, "0.4", 3);
developera1255e42023-05-13 17:45:02 +080016553 } else {
16554 params[0].name = "HT_GI";
16555 params[0].value = "0";
16556 params[1].name = "VHT_SGI";
16557 params[1].value = "0";
16558 /*should enable FIXED_HE_GI_SUPPORT in driver*/
16559 params[2].name = "FgiFltf";
16560 if (guard_interval == wifi_guard_interval_800) {
16561 params[2].value = "800";
developer32f2a182023-06-27 19:50:41 +080016562 memcpy(GI, "0.8", 3);
developera1255e42023-05-13 17:45:02 +080016563 } else if (guard_interval == wifi_guard_interval_1600) {
16564 params[2].value = "1600";
developer32f2a182023-06-27 19:50:41 +080016565 memcpy(GI, "1.6", 3);
developera1255e42023-05-13 17:45:02 +080016566 } else if (guard_interval == wifi_guard_interval_3200) {
16567 params[2].value = "3200";
developer32f2a182023-06-27 19:50:41 +080016568 memcpy(GI, "3.2", 3);
developera1255e42023-05-13 17:45:02 +080016569 } else if (guard_interval == wifi_guard_interval_auto) {
16570 params[2].value = "0";
developer32f2a182023-06-27 19:50:41 +080016571 memcpy(GI, "auto", 4);
developera1255e42023-05-13 17:45:02 +080016572 }
16573 wifi_datfileWrite(dat_file, params, 3);
16574 }
developera3511852023-06-14 14:12:59 +080016575 // Record GI for get GI function
developere40952c2023-06-15 18:46:43 +080016576 res = snprintf(buf, sizeof(buf), "%s%d.txt", GUARD_INTERVAL_FILE, radio_index);
16577 if (os_snprintf_error(sizeof(buf), res)) {
16578 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16579 return RETURN_ERR;
16580 }
developera3511852023-06-14 14:12:59 +080016581 f = fopen(buf, "w");
16582 if (f == NULL)
16583 return RETURN_ERR;
16584 fprintf(f, "%s", GI);
developerc14d83a2023-06-29 20:09:42 +080016585 if (fclose(f) == EOF)
16586 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016587 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16588 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016589}
16590
16591INT wifi_getGuardInterval(INT radio_index, wifi_guard_interval_t *guard_interval)
16592{
developera3511852023-06-14 14:12:59 +080016593 char buf[32] = {0};
16594 char cmd[64] = {0};
developere40952c2023-06-15 18:46:43 +080016595 int res;
developer72fb0bb2023-01-11 09:46:29 +080016596
developera3511852023-06-14 14:12:59 +080016597 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016598
developera3511852023-06-14 14:12:59 +080016599 if (guard_interval == NULL)
16600 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016601
developere40952c2023-06-15 18:46:43 +080016602 res = snprintf(cmd, sizeof(cmd), "cat %s%d.txt 2> /dev/null", GUARD_INTERVAL_FILE, radio_index);
16603 if (os_snprintf_error(sizeof(cmd), res)) {
16604 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16605 return RETURN_ERR;
16606 }
developera3511852023-06-14 14:12:59 +080016607 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016608
developera3511852023-06-14 14:12:59 +080016609 if (strncmp(buf, "0.4", 3) == 0)
16610 *guard_interval = wifi_guard_interval_400;
16611 else if (strncmp(buf, "0.8", 3) == 0)
16612 *guard_interval = wifi_guard_interval_800;
16613 else if (strncmp(buf, "1.6", 3) == 0)
16614 *guard_interval = wifi_guard_interval_1600;
16615 else if (strncmp(buf, "3.2", 3) == 0)
16616 *guard_interval = wifi_guard_interval_3200;
16617 else
16618 *guard_interval = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +080016619
developera3511852023-06-14 14:12:59 +080016620 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16621 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016622}
16623
16624INT wifi_setBSSColor(INT radio_index, UCHAR color)
16625{
developera3511852023-06-14 14:12:59 +080016626 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16627 struct params params = {0};
16628 char config_file[128] = {0};
16629 char bss_color[4] ={0};
developere40952c2023-06-15 18:46:43 +080016630 int res;
developer72fb0bb2023-01-11 09:46:29 +080016631
developera1255e42023-05-13 17:45:02 +080016632 if (color < 1 || color > 63) {
16633 wifi_dbg_printf("color value is err:%d.\n", color);
16634 return RETURN_ERR;
16635 }
developera3511852023-06-14 14:12:59 +080016636 params.name = "he_bss_color";
developere40952c2023-06-15 18:46:43 +080016637 res = snprintf(bss_color, sizeof(bss_color), "%hhu", color);
16638 if (os_snprintf_error(sizeof(bss_color), res)) {
16639 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16640 return RETURN_ERR;
16641 }
developera3511852023-06-14 14:12:59 +080016642 params.value = bss_color;
developer75bd10c2023-06-27 11:34:08 +080016643
16644 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16645 if (os_snprintf_error(sizeof(config_file), res)) {
16646 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16647 return RETURN_ERR;
16648 }
developera3511852023-06-14 14:12:59 +080016649 wifi_hostapdWrite(config_file, &params, 1);
16650 //wifi_hostapdProcessUpdate(radio_index, &params, 1);
developera1255e42023-05-13 17:45:02 +080016651 wifi_reloadAp(radio_index);
developer69b61b02023-03-07 17:17:44 +080016652
developera3511852023-06-14 14:12:59 +080016653 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16654 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016655}
16656
16657INT wifi_getBSSColor(INT radio_index, UCHAR *color)
16658{
developera3511852023-06-14 14:12:59 +080016659 char config_file[128] = {0};
16660 char buf[64] = {0};
16661 char temp_output[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +080016662 int res;
developerc14d83a2023-06-29 20:09:42 +080016663 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016664
developera3511852023-06-14 14:12:59 +080016665 wifi_dbg_printf("\nFunc=%s\n", __func__);
16666 if (NULL == color)
16667 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016668
developer75bd10c2023-06-27 11:34:08 +080016669 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16670 if (os_snprintf_error(sizeof(config_file), res)) {
16671 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16672 return RETURN_ERR;
16673 }
developera3511852023-06-14 14:12:59 +080016674 wifi_hostapdRead(config_file, "he_bss_color", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016675
developera3511852023-06-14 14:12:59 +080016676 if(strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +080016677 res = snprintf(temp_output, sizeof(temp_output), "%s", buf);
developera3511852023-06-14 14:12:59 +080016678 } else {
developere40952c2023-06-15 18:46:43 +080016679 res = snprintf(temp_output, sizeof(temp_output), "1"); // default value
16680 }
16681 if (os_snprintf_error(sizeof(temp_output), res)) {
16682 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16683 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080016684 }
developer72fb0bb2023-01-11 09:46:29 +080016685
developerc14d83a2023-06-29 20:09:42 +080016686 if (hal_strtoul(temp_output, 10, &tmp) < 0) {
16687 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016688 }
developerc14d83a2023-06-29 20:09:42 +080016689 *color = tmp;
developera3511852023-06-14 14:12:59 +080016690 wifi_dbg_printf("\noutput_string=%s\n", color);
developer72fb0bb2023-01-11 09:46:29 +080016691
developera3511852023-06-14 14:12:59 +080016692 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016693}
16694
16695/* multi-psk support */
16696INT wifi_getMultiPskClientKey(INT apIndex, mac_address_t mac, wifi_key_multi_psk_t *key)
16697{
developera3511852023-06-14 14:12:59 +080016698 char cmd[256];
16699 char interface_name[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080016700 int res;
developer72fb0bb2023-01-11 09:46:29 +080016701
developera3511852023-06-14 14:12:59 +080016702 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16703 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016704
developer75bd10c2023-06-27 11:34:08 +080016705 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s sta %x:%x:%x:%x:%x:%x |grep '^keyid' | cut -f 2 -d = | tr -d '\n'",
developera3511852023-06-14 14:12:59 +080016706 interface_name,
16707 mac[0],
16708 mac[1],
16709 mac[2],
16710 mac[3],
16711 mac[4],
16712 mac[5]
16713 );
developer75bd10c2023-06-27 11:34:08 +080016714 if (os_snprintf_error(sizeof(cmd), res)) {
16715 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16716 return RETURN_ERR;
16717 }
developera3511852023-06-14 14:12:59 +080016718 printf("DEBUG LOG wifi_getMultiPskClientKey(%s)\n",cmd);
16719 _syscmd(cmd, key->wifi_keyId, 64);
developer72fb0bb2023-01-11 09:46:29 +080016720
16721
developera3511852023-06-14 14:12:59 +080016722 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016723}
16724
16725INT wifi_pushMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16726{
developera3511852023-06-14 14:12:59 +080016727 char interface_name[16] = {0};
16728 FILE *fd = NULL;
16729 char fname[100];
16730 char cmd[128] = {0};
16731 char out[64] = {0};
16732 wifi_key_multi_psk_t * key = NULL;
developer75bd10c2023-06-27 11:34:08 +080016733 int res, ret;
developere40952c2023-06-15 18:46:43 +080016734
developera3511852023-06-14 14:12:59 +080016735 if(keysNumber < 0)
16736 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016737
developere40952c2023-06-15 18:46:43 +080016738 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16739 if (os_snprintf_error(sizeof(fname), res)) {
16740 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16741 return RETURN_ERR;
16742 }
developera3511852023-06-14 14:12:59 +080016743 fd = fopen(fname, "w");
16744 if (!fd) {
16745 return RETURN_ERR;
16746 }
16747 key= (wifi_key_multi_psk_t *) keys;
16748 for(int i=0; i<keysNumber; ++i, key++) {
developer75bd10c2023-06-27 11:34:08 +080016749 ret = fprintf(fd, "keyid=%s 00:00:00:00:00:00 %s\n", key->wifi_keyId, key->wifi_psk);
16750 if (ret < 0)
16751 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developera3511852023-06-14 14:12:59 +080016752 }
developerd14dff12023-06-28 22:47:44 +080016753 if (fclose(fd) != 0) {
16754 wifi_debug(DEBUG_ERROR, "fclose fail\n");
16755 return RETURN_ERR;
16756 }
developer72fb0bb2023-01-11 09:46:29 +080016757
developera3511852023-06-14 14:12:59 +080016758 //reload file
16759 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16760 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080016761 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s raw RELOAD_WPA_PSK", interface_name);
16762
16763 if (os_snprintf_error(sizeof(cmd), res)) {
16764 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16765 return RETURN_ERR;
16766 }
developera3511852023-06-14 14:12:59 +080016767 _syscmd(cmd, out, 64);
16768 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016769}
16770
16771INT wifi_getMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16772{
developera3511852023-06-14 14:12:59 +080016773 FILE *fd = NULL;
16774 char fname[100];
16775 char * line = NULL;
16776 char * pos = NULL;
16777 size_t len = 0;
16778 ssize_t read = 0;
16779 INT ret = RETURN_OK;
16780 wifi_key_multi_psk_t *keys_it = NULL;
developere40952c2023-06-15 18:46:43 +080016781 int res;
developer72fb0bb2023-01-11 09:46:29 +080016782
developera3511852023-06-14 14:12:59 +080016783 if (keysNumber < 1) {
16784 return RETURN_ERR;
16785 }
developer72fb0bb2023-01-11 09:46:29 +080016786
developere40952c2023-06-15 18:46:43 +080016787 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16788 if (os_snprintf_error(sizeof(fname), res)) {
16789 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16790 return RETURN_ERR;
16791 }
developera3511852023-06-14 14:12:59 +080016792 fd = fopen(fname, "r");
16793 if (!fd) {
16794 return RETURN_ERR;
16795 }
developer72fb0bb2023-01-11 09:46:29 +080016796
developera3511852023-06-14 14:12:59 +080016797 if (keys == NULL) {
16798 ret = RETURN_ERR;
16799 goto close;
16800 }
developer72fb0bb2023-01-11 09:46:29 +080016801
developera3511852023-06-14 14:12:59 +080016802 keys_it = keys;
16803 while ((read = getline(&line, &len, fd)) != -1) {
16804 //Strip trailing new line if present
16805 if (read > 0 && line[read-1] == '\n') {
16806 line[read-1] = '\0';
16807 }
developer72fb0bb2023-01-11 09:46:29 +080016808
developera3511852023-06-14 14:12:59 +080016809 if(strcmp(line,"keyid=")) {
developer37646972023-06-29 10:58:43 +080016810 if (sscanf(line, "keyid=%63s", keys_it->wifi_keyId) == EOF)
16811 continue;
developera3511852023-06-14 14:12:59 +080016812 if (!(pos = index(line, ' '))) {
16813 ret = RETURN_ERR;
16814 goto close;
16815 }
16816 pos++;
16817 //Here should be 00:00:00:00:00:00
16818 if (!(strcmp(pos,"00:00:00:00:00:00"))) {
16819 printf("Not supported MAC: %s\n", pos);
16820 }
16821 if (!(pos = index(pos, ' '))) {
16822 ret = RETURN_ERR;
16823 goto close;
16824 }
16825 pos++;
developer72fb0bb2023-01-11 09:46:29 +080016826
developera3511852023-06-14 14:12:59 +080016827 //The rest is PSK
developere40952c2023-06-15 18:46:43 +080016828 res = snprintf(&keys_it->wifi_psk[0], sizeof(keys_it->wifi_psk), "%s", pos);
16829 if (os_snprintf_error(sizeof(keys_it->wifi_psk), res)) {
16830 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +080016831 if (fclose(fd) == EOF)
16832 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developer5b23cd02023-07-19 20:26:03 +080016833
developere40952c2023-06-15 18:46:43 +080016834 return RETURN_ERR;
16835 }
16836
developera3511852023-06-14 14:12:59 +080016837 keys_it++;
developer72fb0bb2023-01-11 09:46:29 +080016838
developera3511852023-06-14 14:12:59 +080016839 if(--keysNumber <= 0)
developer72fb0bb2023-01-11 09:46:29 +080016840 break;
developera3511852023-06-14 14:12:59 +080016841 }
16842 }
developer72fb0bb2023-01-11 09:46:29 +080016843
16844close:
developera3511852023-06-14 14:12:59 +080016845 free(line);
developer37646972023-06-29 10:58:43 +080016846 if (fclose(fd) == EOF)
16847 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016848 return ret;
developer72fb0bb2023-01-11 09:46:29 +080016849}
16850/* end of multi-psk support */
16851
16852INT wifi_setNeighborReports(UINT apIndex,
developera3511852023-06-14 14:12:59 +080016853 UINT numNeighborReports,
16854 wifi_NeighborReport_t *neighborReports)
developer72fb0bb2023-01-11 09:46:29 +080016855{
developera3511852023-06-14 14:12:59 +080016856 char hex_bssid[13] = { 0 };
16857 char bssid[18] = { 0 };
16858 char nr[100] = { 0 };
16859 char ssid[32];
16860 char hex_ssid[32];
16861 char interface_name[16] = {0};
16862 INT ret;
developere40952c2023-06-15 18:46:43 +080016863 int res;
developerd14dff12023-06-28 22:47:44 +080016864 unsigned char hex_ssid_len;
developer72fb0bb2023-01-11 09:46:29 +080016865
developera3511852023-06-14 14:12:59 +080016866 /*rmeove all neighbors*/
16867 wifi_dbg_printf("\n[%s]: removing all neighbors from %s\n", __func__, interface_name);
16868 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16869 return RETURN_ERR;
developer33f13ba2023-07-12 16:19:06 +080016870
16871 res = v_secure_system("hostapd_cli show_neighbor -i %s | awk '{print $1 \" \" $2}' | xargs -n2 -r hostapd_cli remove_neighbor -i %s",
developer32f2a182023-06-27 19:50:41 +080016872 interface_name, interface_name);
16873
developer33f13ba2023-07-12 16:19:06 +080016874 if (res) {
16875 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +080016876 return RETURN_ERR;
16877 }
developer72fb0bb2023-01-11 09:46:29 +080016878
developera3511852023-06-14 14:12:59 +080016879 for(unsigned int i = 0; i < numNeighborReports; i++)
16880 {
16881 memset(ssid, 0, sizeof(ssid));
16882 ret = wifi_getSSIDName(apIndex, ssid);
16883 if (ret != RETURN_OK)
16884 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016885
developera3511852023-06-14 14:12:59 +080016886 memset(hex_ssid, 0, sizeof(hex_ssid));
developerd14dff12023-06-28 22:47:44 +080016887 hex_ssid_len = sizeof(hex_ssid);
16888 for(size_t j = 0,k = 0; ssid[j] != '\0' && k < sizeof(hex_ssid); j++,k+=2 ) {
16889 res = snprintf(hex_ssid + k, hex_ssid_len, "%02x", ssid[j]);
16890
16891 if (os_snprintf_error(hex_ssid_len, res)) {
16892 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16893 return RETURN_ERR;
16894 }
16895 hex_ssid_len = sizeof(hex_ssid) - strlen(hex_ssid);
16896 }
developer72fb0bb2023-01-11 09:46:29 +080016897
developere40952c2023-06-15 18:46:43 +080016898 res = snprintf(hex_bssid, sizeof(hex_bssid),
developera3511852023-06-14 14:12:59 +080016899 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
16900 neighborReports[i].bssid[0], neighborReports[i].bssid[1], neighborReports[i].bssid[2], neighborReports[i].bssid[3], neighborReports[i].bssid[4], neighborReports[i].bssid[5]);
developere40952c2023-06-15 18:46:43 +080016901 if (os_snprintf_error(sizeof(hex_bssid), res)) {
16902 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16903 return RETURN_ERR;
16904 }
16905 res = snprintf(bssid, sizeof(bssid),
developera3511852023-06-14 14:12:59 +080016906 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
16907 neighborReports[i].bssid[0], neighborReports[i].bssid[1], neighborReports[i].bssid[2], neighborReports[i].bssid[3], neighborReports[i].bssid[4], neighborReports[i].bssid[5]);
developere40952c2023-06-15 18:46:43 +080016908 if (os_snprintf_error(sizeof(bssid), res)) {
16909 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16910 return RETURN_ERR;
16911 }
developer72fb0bb2023-01-11 09:46:29 +080016912
developere40952c2023-06-15 18:46:43 +080016913 res = snprintf(nr, sizeof(nr),
developera3511852023-06-14 14:12:59 +080016914 "%s" // bssid
16915 "%02hhx%02hhx%02hhx%02hhx" // bssid_info
16916 "%02hhx" // operclass
16917 "%02hhx" // channel
16918 "%02hhx", // phy_mode
16919 hex_bssid,
16920 neighborReports[i].info & 0xff, (neighborReports[i].info >> 8) & 0xff,
16921 (neighborReports[i].info >> 16) & 0xff, (neighborReports[i].info >> 24) & 0xff,
16922 neighborReports[i].opClass,
16923 neighborReports[i].channel,
16924 neighborReports[i].phyTable);
developere40952c2023-06-15 18:46:43 +080016925 if (os_snprintf_error(sizeof(nr), res)) {
16926 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16927 return RETURN_ERR;
16928 }
developer72fb0bb2023-01-11 09:46:29 +080016929
developer33f13ba2023-07-12 16:19:06 +080016930 res = v_secure_system("hostapd_cli set_neighbor %s ssid=%s nr=%s -i %s",
16931 bssid, hex_ssid, nr, interface_name);
16932 if (res) {
16933 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080016934 return RETURN_ERR;
16935 }
developera3511852023-06-14 14:12:59 +080016936 }
developer72fb0bb2023-01-11 09:46:29 +080016937
developera3511852023-06-14 14:12:59 +080016938 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016939}
16940
16941INT wifi_getApInterworkingElement(INT apIndex, wifi_InterworkingElement_t *output_struct)
16942{
developera3511852023-06-14 14:12:59 +080016943 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016944}
16945
16946#ifdef _WIFI_HAL_TEST_
16947int main(int argc,char **argv)
16948{
developera3511852023-06-14 14:12:59 +080016949 int index;
16950 INT ret=0;
16951 char buf[1024]="";
developer72fb0bb2023-01-11 09:46:29 +080016952
developera3511852023-06-14 14:12:59 +080016953 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16954 if(argc<3)
16955 {
16956 if(argc==2)
16957 {
16958 if(!strcmp(argv[1], "init"))
16959 return wifi_init();
16960 if(!strcmp(argv[1], "reset"))
16961 return wifi_reset();
16962 if(!strcmp(argv[1], "wifi_getHalVersion"))
16963 {
16964 char buffer[64];
16965 if(wifi_getHalVersion(buffer)==RETURN_OK)
16966 printf("Version: %s\n", buffer);
16967 else
16968 printf("Error in wifi_getHalVersion\n");
16969 return RETURN_OK;
16970 }
16971 }
16972 printf("wifihal <API> <radioIndex> <arg1> <arg2> ...\n");
16973 exit(-1);
16974 }
developer72fb0bb2023-01-11 09:46:29 +080016975
developera3511852023-06-14 14:12:59 +080016976 index = atoi(argv[2]);
developer33f13ba2023-07-12 16:19:06 +080016977 if (strstr(argv[1], "test_system")!=NULL) {
16978 ret = v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
16979 printf("ret = %d\n", ret);
16980
16981 ret = v_secure_system("hostapd_cli show_neighbor -i %s | awk '{print $1 \" \" $2}' | xargs -n2 -r hostapd_cli remove_neighbor -i %s",
16982 "ra0", "ra0");
16983 printf("ret = %d\n", ret);
16984
16985 ret = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", "1,2,3");
16986 printf("ret = %d\n", ret);
16987
16988 ret = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", "rai0");
16989 printf("ret = %d\n", ret);
16990
16991 ret = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", "rax0");
16992 printf("ret = %d\n", ret);
16993
16994 ret = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", "ra0");
16995 printf("ret = %d\n", ret);
16996 ret = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", "ra0");
16997 printf("ret = %d\n", ret);
16998 ret = v_secure_system("ping -q -c 1 -W 1 \"%s\" > /dev/console 2>&1", "20.1.1.101");
16999 printf("ret = %d\n", ret);
17000
17001 return 0;
17002 }
17003
17004 if (strstr(argv[1], "test_popen")!=NULL) {
17005 FILE *fp = v_secure_popen("w", "cat");
17006 fprintf(fp, "popen write success\n");
17007 fclose(fp);
17008
17009 char buf[1024];
17010 memset(buf, 0, sizeof(buf));
17011 fp = v_secure_popen("r", "echo popen read success");
17012 if (fp == NULL) {
17013 printf("v_secure_popen failed\n");
17014 } else {
17015 if (fgets(buf, sizeof(buf), fp) == NULL) {
17016 printf("v_secure_popen read error\n");
17017 } else {
17018 printf("%s\n", buf);
17019 }
17020 v_secure_pclose(fp);
17021 }
17022
17023#if 0
17024 fp = v_secure_popen("r", "hostapd_cli -i %s reload", "ra0");
17025 if (fp == NULL) {
17026 printf("v_secure_popen failed\n");
17027 } else {
17028 if (fgets(buf, sizeof(buf), fp) == NULL) {
17029 printf("v_secure_popen read error\n");
17030 } else {
17031 printf("%s", buf);
17032 }
17033 v_secure_pclose(fp);
17034 }
17035#endif
17036
17037 long int band;
17038 memset(buf, 0, sizeof(buf));
17039 ret = _syscmd_secure(buf, sizeof(buf),
17040 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 0);
17041 if (hal_strtol(buf, 16, &band) < 0) {
17042 wifi_debug(DEBUG_ERROR, "strtol fail\n");
17043 }
17044 printf("ret = %d, band0=%ld\n", ret, band);
17045
17046 memset(buf, 0, sizeof(buf));
17047 ret = _syscmd_secure(buf, sizeof(buf),
17048 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 1);
17049 if (hal_strtol(buf, 16, &band) < 0) {
17050 wifi_debug(DEBUG_ERROR, "strtol fail\n");
17051 }
17052 printf("ret = %d, band1=%ld\n", ret, band);
17053
17054 memset(buf, 0, sizeof(buf));
17055 ret = _syscmd_secure(buf, sizeof(buf),
17056 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 2);
17057 if (hal_strtol(buf, 16, &band) < 0) {
17058 wifi_debug(DEBUG_ERROR, "strtol fail\n");
17059 }
17060 printf("ret = %d, band2=%ld\n", ret, band);
17061 }
17062
developera3511852023-06-14 14:12:59 +080017063 if(strstr(argv[1], "wifi_getApName")!=NULL)
17064 {
17065 wifi_getApName(index,buf);
17066 printf("Ap name is %s \n",buf);
17067 return 0;
17068 }
developerb14b3462023-07-01 18:02:42 +080017069 if(strstr(argv[1], "wifi_getRadioExtChannel")!=NULL)
17070 {
17071 wifi_getRadioExtChannel(index,buf);
17072 printf("extchannel is %s \n",buf);
17073 return 0;
17074 }
developerfead3972023-05-25 20:15:02 +080017075 if(strstr(argv[1], "wifi_setRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080017076 {
developerfead3972023-05-25 20:15:02 +080017077 UINT pureMode = atoi(argv[3]);
17078
developera3511852023-06-14 14:12:59 +080017079 wifi_setRadioMode(index, NULL, pureMode);
17080 printf("Ap SET Radio mode 0x%x\n", pureMode);
17081 return 0;
17082 }
17083 if (strstr(argv[1], "wifi_setRadioAutoBlockAckEnable") != NULL) {
17084 unsigned char enable = atoi(argv[3]);
17085 if (enable)
17086 wifi_setRadioAutoBlockAckEnable(index, TRUE);
17087 else
17088 wifi_setRadioAutoBlockAckEnable(index, FALSE);
17089 printf("%s handle wifi_setRadioAutoBlockAckEnable\n", __FUNCTION__);
17090 }
developera39cfb22023-06-20 16:28:17 +080017091 if(strstr(argv[1], "wifi_setRadioTrafficStatsRadioStatisticsEnable")!=NULL)
17092 {
17093 wifi_setRadioTrafficStatsRadioStatisticsEnable(index, TRUE);
17094 printf("Ap SET wifi_setRadioTrafficStatsRadioStatisticsEnable\n");
17095 return 0;
17096 }
17097 if(strstr(argv[1], "wifi_setRadioTrafficStatsMeasure")!=NULL)
17098 {
17099 wifi_radioTrafficStatsMeasure_t input = {30, 200};
17100
17101 wifi_setRadioTrafficStatsMeasure(index, &input);
17102 printf("Ap SET wifi_setRadioTrafficStatsMeasure\n");
17103 return 0;
17104 }
developerfead3972023-05-25 20:15:02 +080017105 if(strstr(argv[1], "wifi_setRadioTransmitPower")!=NULL)
developera3511852023-06-14 14:12:59 +080017106 {
developerfead3972023-05-25 20:15:02 +080017107 ULONG TransmitPower = atoi(argv[3]);
17108
developera3511852023-06-14 14:12:59 +080017109 wifi_setRadioTransmitPower(index, TransmitPower);
17110 printf("Ap SET TransmitPower %lu\n", TransmitPower);
17111 return 0;
17112 }
developerfead3972023-05-25 20:15:02 +080017113 if(strstr(argv[1], "wifi_setApManagementFramePowerControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017114 {
developerfead3972023-05-25 20:15:02 +080017115 INT TransmitPower = atoi(argv[3]);
17116
developera3511852023-06-14 14:12:59 +080017117 wifi_setApManagementFramePowerControl(index, TransmitPower);
17118 printf("Ap SET Mgnt TransmitPower %d\n", TransmitPower);
17119 return 0;
17120 }
developerfead3972023-05-25 20:15:02 +080017121 if(strstr(argv[1], "wifi_setRadioBW")!=NULL)
developera3511852023-06-14 14:12:59 +080017122 {
developerfead3972023-05-25 20:15:02 +080017123 CHAR *bandwith = argv[3];
17124
developera3511852023-06-14 14:12:59 +080017125 wifi_setRadioOperatingChannelBandwidth(index, bandwith);
17126 printf("Ap SET bw %s\n", bandwith);
17127 return 0;
17128 }
developerfead3972023-05-25 20:15:02 +080017129 if(strstr(argv[1], "wifi_factoryResetRadio")!=NULL)
developera3511852023-06-14 14:12:59 +080017130 {
17131 wifi_factoryResetRadio(index);
17132 printf("wifi_factoryResetRadio ok!\n");
17133 return 0;
17134 }
developerfead3972023-05-25 20:15:02 +080017135 if(strstr(argv[1], "wifi_getRadioResetCount")!=NULL)
developera3511852023-06-14 14:12:59 +080017136 {
17137 ULONG rst_cnt;
17138 wifi_getRadioResetCount(index, &rst_cnt);
17139 printf("wifi_factoryResetRadio rst_cnt = %lu\n", rst_cnt);
17140 return 0;
17141 }
developer2edaf012023-05-24 14:24:53 +080017142 if (strncmp(argv[1], "wifi_addApAclDevice", strlen(argv[1])) == 0) {
developer49b17232023-05-19 16:35:19 +080017143 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017144 {
17145 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17146 exit(-1);
17147 }
developer49b17232023-05-19 16:35:19 +080017148 wifi_addApAclDevice(index, argv[3]);
17149 return 0;
17150 }
developer2edaf012023-05-24 14:24:53 +080017151 if (strncmp(argv[1], "wifi_getApAclDevices", strlen(argv[1])) == 0) {
17152 wifi_getApAclDevices(index, buf, 1024);
17153 wifi_debug(DEBUG_NOTICE, "Ap acl Devices: %s\n", buf);
developer121a8e72023-05-22 09:19:39 +080017154 return 0;
17155 }
developer2edaf012023-05-24 14:24:53 +080017156 if (strncmp(argv[1], "wifi_delApAclDevice", strlen(argv[1])) == 0) {
17157 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017158 {
17159 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17160 exit(-1);
17161 }
developer2edaf012023-05-24 14:24:53 +080017162 wifi_delApAclDevice(index, argv[3]);
17163 return 0;
17164 }
17165 if (strncmp(argv[1], "wifi_delApAclDevices", strlen(argv[1])) == 0) {
17166 wifi_delApAclDevices(index);
17167 return 0;
17168 }
17169 if (strncmp(argv[1], "wifi_getApAclDeviceNum", strlen(argv[1])) == 0) {
developer863a4a62023-06-06 16:55:59 +080017170 UINT acl_num = 0;
developer2edaf012023-05-24 14:24:53 +080017171 wifi_getApAclDeviceNum(index, &acl_num);
17172 wifi_debug(DEBUG_NOTICE, "Ap acl numbers: %d\n", acl_num);
17173 return 0;
17174 }
17175 if (strncmp(argv[1], "wifi_getApDenyAclDevices", strlen(argv[1])) == 0) {
17176 wifi_getApDenyAclDevices(index, buf, 1024);
17177 wifi_debug(DEBUG_NOTICE, "Ap Deny Acl Devices: %s\n", buf);
17178 return 0;
17179 }
17180 if (strncmp(argv[1], "wifi_setApMacAddressControlMode", strlen(argv[1])) == 0) {
17181 int filter_mode = 0;
17182 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017183 {
17184 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17185 exit(-1);
17186 }
developer2edaf012023-05-24 14:24:53 +080017187 filter_mode = atoi(argv[3]);
17188 wifi_setApMacAddressControlMode(index,filter_mode);
17189 return 0;
17190 }
developer5cd4c862023-05-26 09:34:42 +080017191 if (strncmp(argv[1], "wifi_getRadioDeclineBARequestEnable", strlen(argv[1])) == 0) {
17192 BOOL output_bool = 0;
17193 wifi_getRadioDeclineBARequestEnable(index, &output_bool);
17194 wifi_debug(DEBUG_NOTICE, "Ap get radio ba decline enable: %d\n", output_bool);
17195 return 0;
17196 }
17197 if (strncmp(argv[1], "wifi_getRadioAutoBlockAckEnable", strlen(argv[1])) == 0) {
17198 BOOL output_bool = 0;
17199 wifi_getRadioAutoBlockAckEnable(index, &output_bool);
17200 wifi_debug(DEBUG_NOTICE, "Ap get radio auto_ba enable: %d\n", output_bool);
17201 return 0;
17202 }
17203
17204 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17205 int filter_mode = 0;
17206 wifi_getApMacAddressControlMode(index, &filter_mode);
17207 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17208 return 0;
17209 }
17210 if (strncmp(argv[1], "wifi_setRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17211 int enable = 0;
17212 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017213 {
17214 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17215 exit(-1);
17216 }
developer5cd4c862023-05-26 09:34:42 +080017217 enable = (BOOL)atoi(argv[3]);
17218 wifi_setRadioIGMPSnoopingEnable(index, enable);
17219 wifi_debug(DEBUG_NOTICE, "Ap set IGMP Snooping Enable: %d\n", enable);
17220 return 0;
17221 }
developer326d4232023-06-15 16:45:30 +080017222 if (strncmp(argv[1], "wifi_setRadioDCSEnable(", strlen(argv[1])) == 0) {
17223 int enable = 0;
17224 if(argc <= 3 )
17225 {
17226 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17227 exit(-1);
17228 }
17229 enable = (BOOL)atoi(argv[3]);
17230 wifi_setRadioDCSEnable(index, enable);
17231 wifi_debug(DEBUG_NOTICE, "Ap set DCS Enable: %d\n", enable);
17232 return 0;
17233 }
17234 if (strncmp(argv[1], "wifi_getRadioAutoChannelRefreshPeriod(", strlen(argv[1])) == 0) {
17235 ULONG period = 0;
developer5cd4c862023-05-26 09:34:42 +080017236
developer326d4232023-06-15 16:45:30 +080017237 wifi_getRadioAutoChannelRefreshPeriod(index, &period);
17238 wifi_debug(DEBUG_NOTICE, "Get RefreshPeriod: %ld\n", period);
17239 return 0;
17240 }
17241 if (strncmp(argv[1], "wifi_setRadioDfsRefreshPeriod(", strlen(argv[1])) == 0) {
17242 ULONG period = 0;
17243
17244 period = (ULONG)atoi(argv[3]);
17245 wifi_setRadioDfsRefreshPeriod(index, period);
17246 wifi_debug(DEBUG_NOTICE, "Set RefreshPeriod: %ld\n", period);
17247 return 0;
17248 }
17249 if (strncmp(argv[1], "wifi_setRadioDCSChannelPool(", strlen(argv[1])) == 0) {
17250 char pool[256] = {'\0'};
17251
developerc14d83a2023-06-29 20:09:42 +080017252 strncpy(pool, argv[3], strlen(argv[3]));
developer326d4232023-06-15 16:45:30 +080017253 wifi_setRadioDCSChannelPool(index, pool);
17254 wifi_debug(DEBUG_NOTICE, "Set DCSChannelPool: %s\n", pool);
17255 return 0;
17256 }
17257 if (strncmp(argv[1], "wifi_getRadioDCSChannelPool(", strlen(argv[1])) == 0) {
17258 char pool[256] = {'\0'};
17259
17260 wifi_getRadioDCSChannelPool(index, pool);
17261 wifi_debug(DEBUG_NOTICE, "Get DCSChannelPool: %s\n", pool);
17262 return 0;
17263 }
developer5cd4c862023-05-26 09:34:42 +080017264 if (strncmp(argv[1], "wifi_getRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17265 BOOL out_status = 0;
17266 wifi_getRadioIGMPSnoopingEnable(index, &out_status);
17267 wifi_debug(DEBUG_NOTICE, "Ap get IGMP Snooping Enable: %d\n", out_status);
17268 return 0;
17269 }
developer121a8e72023-05-22 09:19:39 +080017270
developer95c045d2023-05-24 19:26:28 +080017271 if (strncmp(argv[1], "wifi_setApWmmEnable", strlen(argv[1])) == 0) {
17272 int enable = 0;
17273 if(argc <= 3)
17274 {
17275 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17276 exit(-1);
17277 }
17278 enable = atoi(argv[3]);
17279 wifi_setApWmmEnable(index,enable);
17280 return 0;
17281 }
developerc1aa6532023-06-09 09:37:01 +080017282 if (strncmp(argv[1], "wifi_pushSsidAdvertisementEnable", strlen(argv[1])) == 0) {
17283 int enable = 0;
17284 if(argc <= 3)
17285 {
17286 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17287 exit(-1);
17288 }
17289 enable = atoi(argv[3]);
17290 wifi_pushSsidAdvertisementEnable(index,enable);
17291 return 0;
17292 }
developer56fbedb2023-05-30 16:47:05 +080017293 if (strncmp(argv[1], "wifi_down", strlen(argv[1])) == 0) {
17294 wifi_down();
17295 return 0;
17296 }
developer95c045d2023-05-24 19:26:28 +080017297
developer56fbedb2023-05-30 16:47:05 +080017298 if (strncmp(argv[1], "wifi_getRadioStatus", strlen(argv[1])) == 0) {
17299 BOOL enable = 0;
17300
17301 wifi_getRadioStatus(index, &enable);
17302 wifi_debug(DEBUG_NOTICE, "wifi_getRadioStatus enable: %d\n", (int)enable);
17303 return 0;
17304 }
developer333c1eb2023-05-31 14:59:39 +080017305
developer95c045d2023-05-24 19:26:28 +080017306 if (strncmp(argv[1], "wifi_getApWMMCapability", strlen(argv[1])) == 0) {
17307 BOOL enable = 0;
17308
17309 wifi_getApWMMCapability(index, &enable);
17310 wifi_debug(DEBUG_NOTICE, "wifi_getApWMMCapability enable: %d\n", (int)enable);
17311 return 0;
17312 }
17313
17314 if (strncmp(argv[1], "wifi_getApWmmEnable", strlen(argv[1])) == 0) {
17315 BOOL enable = 0;
17316
17317 wifi_getApWmmEnable(index, &enable);
17318 wifi_debug(DEBUG_NOTICE, "wifi_getApWmmEnable enable: %d\n", (int)enable);
17319 return 0;
17320 }
17321
developer2edaf012023-05-24 14:24:53 +080017322 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17323 int filter_mode = 0;
17324 wifi_getApMacAddressControlMode(index, &filter_mode);
17325 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17326 return 0;
17327 }
developer0f10c772023-05-16 21:43:39 +080017328 if(strstr(argv[1], "wifi_getRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080017329 {
developer863a4a62023-06-06 16:55:59 +080017330 UINT mode = 0;
developer0f10c772023-05-16 21:43:39 +080017331
developera3511852023-06-14 14:12:59 +080017332 wifi_getRadioMode(index, buf, &mode);
17333 printf("Ap Radio mode is %s , mode = 0x%x\n", buf, mode);
17334 return 0;
17335 }
17336 if(strstr(argv[1], "wifi_getRadioAutoChannelEnable")!=NULL)
17337 {
17338 BOOL b = FALSE;
17339 BOOL *output_bool = &b;
17340 wifi_getRadioAutoChannelEnable(index,output_bool);
17341 printf("Channel enabled = %d \n",b);
17342 return 0;
17343 }
17344 if(strstr(argv[1], "wifi_getApWpaEncryptionMode")!=NULL)
17345 {
17346 wifi_getApWpaEncryptionMode(index,buf);
17347 printf("encryption enabled = %s\n",buf);
17348 return 0;
17349 }
17350 if(strstr(argv[1], "wifi_getApSsidAdvertisementEnable")!=NULL)
17351 {
17352 BOOL b = FALSE;
17353 BOOL *output_bool = &b;
17354 wifi_getApSsidAdvertisementEnable(index,output_bool);
17355 printf("advertisment enabled = %d\n",b);
17356 return 0;
17357 }
17358 if(strstr(argv[1],"wifi_getApAssociatedDeviceTidStatsResult")!=NULL)
17359 {
17360 if(argc <= 3 )
17361 {
17362 printf("Insufficient arguments \n");
17363 exit(-1);
17364 }
developer72fb0bb2023-01-11 09:46:29 +080017365
developera3511852023-06-14 14:12:59 +080017366 char sta[20] = {'\0'};
17367 ULLONG handle= 0;
developerc14d83a2023-06-29 20:09:42 +080017368 strncpy(sta,argv[3], strlen(argv[3]));
developera3511852023-06-14 14:12:59 +080017369 mac_address_t st;
developer72fb0bb2023-01-11 09:46:29 +080017370 mac_addr_aton(st,sta);
17371
developera3511852023-06-14 14:12:59 +080017372 wifi_associated_dev_tid_stats_t tid_stats;
17373 wifi_getApAssociatedDeviceTidStatsResult(index,&st,&tid_stats,&handle);
17374 for(int tid_index=0; tid_index<PS_MAX_TID; tid_index++) //print tid stats
17375 printf(" tid=%d \t ac=%d \t num_msdus=%lld \n" ,tid_stats.tid_array[tid_index].tid,tid_stats.tid_array[tid_index].ac,tid_stats.tid_array[tid_index].num_msdus);
17376 }
developer72fb0bb2023-01-11 09:46:29 +080017377
developera3511852023-06-14 14:12:59 +080017378 if(strstr(argv[1], "getApEnable")!=NULL) {
17379 BOOL enable;
17380 ret=wifi_getApEnable(index, &enable);
17381 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17382 }
17383 else if(strstr(argv[1], "setApEnable")!=NULL) {
17384 BOOL enable = atoi(argv[3]);
17385 ret=wifi_setApEnable(index, enable);
17386 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17387 }
17388 else if(strstr(argv[1], "getApStatus")!=NULL) {
17389 char status[64];
17390 ret=wifi_getApStatus(index, status);
17391 printf("%s %d: %s, returns %d\n", argv[1], index, status, ret);
17392 }
17393 else if(strstr(argv[1], "wifi_getSSIDNameStatus")!=NULL)
17394 {
17395 wifi_getSSIDNameStatus(index,buf);
17396 printf("%s %d: active ssid : %s\n",argv[1], index,buf);
17397 return 0;
17398 } else if(strstr(argv[1], "wifi_resetApVlanCfg")!=NULL) {
17399 wifi_resetApVlanCfg(index);
17400 printf("%s %d: wifi_resetApVlanCfg : %s\n",argv[1], index,buf);
17401 return 0;
17402 }
17403 else if(strstr(argv[1], "getSSIDTrafficStats2")!=NULL) {
17404 wifi_ssidTrafficStats2_t stats={0};
17405 ret=wifi_getSSIDTrafficStats2(index, &stats); //Tr181
17406 printf("%s %d: returns %d\n", argv[1], index, ret);
17407 printf(" ssid_BytesSent =%lu\n", stats.ssid_BytesSent);
17408 printf(" ssid_BytesReceived =%lu\n", stats.ssid_BytesReceived);
17409 printf(" ssid_PacketsSent =%lu\n", stats.ssid_PacketsSent);
17410 printf(" ssid_PacketsReceived =%lu\n", stats.ssid_PacketsReceived);
17411 printf(" ssid_RetransCount =%lu\n", stats.ssid_RetransCount);
17412 printf(" ssid_FailedRetransCount =%lu\n", stats.ssid_FailedRetransCount);
17413 printf(" ssid_RetryCount =%lu\n", stats.ssid_RetryCount);
17414 printf(" ssid_MultipleRetryCount =%lu\n", stats.ssid_MultipleRetryCount);
17415 printf(" ssid_ACKFailureCount =%lu\n", stats.ssid_ACKFailureCount);
17416 printf(" ssid_AggregatedPacketCount =%lu\n", stats.ssid_AggregatedPacketCount);
17417 printf(" ssid_ErrorsSent =%lu\n", stats.ssid_ErrorsSent);
17418 printf(" ssid_ErrorsReceived =%lu\n", stats.ssid_ErrorsReceived);
17419 printf(" ssid_UnicastPacketsSent =%lu\n", stats.ssid_UnicastPacketsSent);
17420 printf(" ssid_UnicastPacketsReceived =%lu\n", stats.ssid_UnicastPacketsReceived);
17421 printf(" ssid_DiscardedPacketsSent =%lu\n", stats.ssid_DiscardedPacketsSent);
17422 printf(" ssid_DiscardedPacketsReceived =%lu\n", stats.ssid_DiscardedPacketsReceived);
17423 printf(" ssid_MulticastPacketsSent =%lu\n", stats.ssid_MulticastPacketsSent);
17424 printf(" ssid_MulticastPacketsReceived =%lu\n", stats.ssid_MulticastPacketsReceived);
17425 printf(" ssid_BroadcastPacketsSent =%lu\n", stats.ssid_BroadcastPacketsSent);
17426 printf(" ssid_BroadcastPacketsRecevied =%lu\n", stats.ssid_BroadcastPacketsRecevied);
17427 printf(" ssid_UnknownPacketsReceived =%lu\n", stats.ssid_UnknownPacketsReceived);
17428 }
17429 else if(strstr(argv[1], "getNeighboringWiFiDiagnosticResult2")!=NULL) {
17430 wifi_neighbor_ap2_t *neighbor_ap_array=NULL, *pt=NULL;
17431 UINT array_size=0;
17432 UINT i=0;
17433 ret=wifi_getNeighboringWiFiDiagnosticResult2(index, &neighbor_ap_array, &array_size);
17434 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17435 for(i=0, pt=neighbor_ap_array; i<array_size; i++, pt++) {
17436 printf(" neighbor %d:\n", i);
17437 printf(" ap_SSID =%s\n", pt->ap_SSID);
17438 printf(" ap_BSSID =%s\n", pt->ap_BSSID);
17439 printf(" ap_Mode =%s\n", pt->ap_Mode);
17440 printf(" ap_Channel =%d\n", pt->ap_Channel);
17441 printf(" ap_SignalStrength =%d\n", pt->ap_SignalStrength);
17442 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17443 printf(" ap_EncryptionMode =%s\n", pt->ap_EncryptionMode);
17444 printf(" ap_SupportedStandards =%s\n", pt->ap_SupportedStandards);
17445 printf(" ap_OperatingStandards =%s\n", pt->ap_OperatingStandards);
17446 printf(" ap_OperatingChannelBandwidth =%s\n", pt->ap_OperatingChannelBandwidth);
17447 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17448 printf(" ap_BeaconPeriod =%d\n", pt->ap_BeaconPeriod);
17449 printf(" ap_Noise =%d\n", pt->ap_Noise);
17450 printf(" ap_BasicDataTransferRates =%s\n", pt->ap_BasicDataTransferRates);
17451 printf(" ap_SupportedDataTransferRates =%s\n", pt->ap_SupportedDataTransferRates);
17452 printf(" ap_DTIMPeriod =%d\n", pt->ap_DTIMPeriod);
17453 printf(" ap_ChannelUtilization =%d\n", pt->ap_ChannelUtilization);
17454 }
17455 if(neighbor_ap_array)
17456 free(neighbor_ap_array); //make sure to free the list
17457 }
17458 else if(strstr(argv[1], "getApAssociatedDeviceDiagnosticResult")!=NULL) {
17459 wifi_associated_dev_t *associated_dev_array=NULL, *pt=NULL;
17460 UINT array_size=0;
17461 UINT i=0;
17462 ret=wifi_getApAssociatedDeviceDiagnosticResult(index, &associated_dev_array, &array_size);
17463 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17464 for(i=0, pt=associated_dev_array; i<array_size; i++, pt++) {
17465 printf(" associated_dev %d:\n", i);
17466 printf(" cli_OperatingStandard =%s\n", pt->cli_OperatingStandard);
17467 printf(" cli_OperatingChannelBandwidth =%s\n", pt->cli_OperatingChannelBandwidth);
17468 printf(" cli_SNR =%d\n", pt->cli_SNR);
17469 printf(" cli_InterferenceSources =%s\n", pt->cli_InterferenceSources);
17470 printf(" cli_DataFramesSentAck =%lu\n", pt->cli_DataFramesSentAck);
17471 printf(" cli_DataFramesSentNoAck =%lu\n", pt->cli_DataFramesSentNoAck);
17472 printf(" cli_BytesSent =%lu\n", pt->cli_BytesSent);
17473 printf(" cli_BytesReceived =%lu\n", pt->cli_BytesReceived);
17474 printf(" cli_RSSI =%d\n", pt->cli_RSSI);
17475 printf(" cli_MinRSSI =%d\n", pt->cli_MinRSSI);
17476 printf(" cli_MaxRSSI =%d\n", pt->cli_MaxRSSI);
17477 printf(" cli_Disassociations =%d\n", pt->cli_Disassociations);
17478 printf(" cli_AuthenticationFailures =%d\n", pt->cli_AuthenticationFailures);
17479 }
17480 if(associated_dev_array)
17481 free(associated_dev_array); //make sure to free the list
17482 }
developer72fb0bb2023-01-11 09:46:29 +080017483
developera3511852023-06-14 14:12:59 +080017484 if(strstr(argv[1],"wifi_getRadioChannelStats")!=NULL)
17485 {
developer72fb0bb2023-01-11 09:46:29 +080017486#define MAX_ARRAY_SIZE 64
developera3511852023-06-14 14:12:59 +080017487 int i, array_size;
17488 char *p, *ch_str;
17489 wifi_channelStats_t input_output_channelStats_array[MAX_ARRAY_SIZE];
developer72fb0bb2023-01-11 09:46:29 +080017490
developera3511852023-06-14 14:12:59 +080017491 if(argc != 5)
17492 {
17493 printf("Insufficient arguments, Usage: wifihal wifi_getRadioChannelStats <AP-Index> <Array-Size> <Comma-seperated-channel-numbers>\n");
17494 exit(-1);
17495 }
17496 memset(input_output_channelStats_array, 0, sizeof(input_output_channelStats_array));
developer72fb0bb2023-01-11 09:46:29 +080017497
developera3511852023-06-14 14:12:59 +080017498 for (i=0, array_size=atoi(argv[3]), ch_str=argv[4]; i<array_size; i++, ch_str=p)
17499 {
17500 strtok_r(ch_str, ",", &p);
17501 input_output_channelStats_array[i].ch_number = atoi(ch_str);
17502 }
17503 wifi_getRadioChannelStats(atoi(argv[2]), input_output_channelStats_array, array_size);
17504 if(!array_size)
17505 array_size=1;//Need to print current channel statistics
17506 for(i=0; i<array_size; i++)
17507 printf("chan num = %d \t, noise =%d\t ch_utilization_busy_rx = %lld \t,\
17508 ch_utilization_busy_tx = %lld \t,ch_utilization_busy = %lld \t,\
17509 ch_utilization_busy_ext = %lld \t, ch_utilization_total = %lld \t \n",\
17510 input_output_channelStats_array[i].ch_number,\
17511 input_output_channelStats_array[i].ch_noise,\
17512 input_output_channelStats_array[i].ch_utilization_busy_rx,\
17513 input_output_channelStats_array[i].ch_utilization_busy_tx,\
17514 input_output_channelStats_array[i].ch_utilization_busy,\
17515 input_output_channelStats_array[i].ch_utilization_busy_ext,\
17516 input_output_channelStats_array[i].ch_utilization_total);
17517 }
developer72fb0bb2023-01-11 09:46:29 +080017518
developera3511852023-06-14 14:12:59 +080017519 if(strstr(argv[1],"wifi_getAssociatedDeviceDetail")!=NULL)
17520 {
17521 if(argc <= 3 )
17522 {
17523 printf("Insufficient arguments \n");
17524 exit(-1);
17525 }
17526 char mac_addr[20] = {'\0'};
17527 wifi_device_t output_struct;
17528 int dev_index = atoi(argv[3]);
developer72fb0bb2023-01-11 09:46:29 +080017529
developera3511852023-06-14 14:12:59 +080017530 wifi_getAssociatedDeviceDetail(index,dev_index,&output_struct);
17531 mac_addr_ntoa(mac_addr,output_struct.wifi_devMacAddress);
17532 printf("wifi_devMacAddress=%s \t wifi_devAssociatedDeviceAuthentiationState=%d \t, wifi_devSignalStrength=%d \t,wifi_devTxRate=%d \t, wifi_devRxRate =%d \t\n ", mac_addr,output_struct.wifi_devAssociatedDeviceAuthentiationState,output_struct.wifi_devSignalStrength,output_struct.wifi_devTxRate,output_struct.wifi_devRxRate);
17533 }
developer72fb0bb2023-01-11 09:46:29 +080017534
developera3511852023-06-14 14:12:59 +080017535 if(strstr(argv[1],"wifi_setNeighborReports")!=NULL)
17536 {
17537 if (argc <= 3)
17538 {
17539 printf("Insufficient arguments\n");
17540 exit(-1);
17541 }
17542 char args[256];
17543 wifi_NeighborReport_t *neighborReports;
developer72fb0bb2023-01-11 09:46:29 +080017544
developera3511852023-06-14 14:12:59 +080017545 neighborReports = calloc(argc - 2, sizeof(neighborReports));
17546 if (!neighborReports)
17547 {
17548 printf("Failed to allocate memory");
17549 exit(-1);
17550 }
developer72fb0bb2023-01-11 09:46:29 +080017551
developera3511852023-06-14 14:12:59 +080017552 for (int i = 3; i < argc; ++i)
17553 {
17554 char *val;
17555 int j = 0;
developerc14d83a2023-06-29 20:09:42 +080017556 unsigned long tmp;
developera3511852023-06-14 14:12:59 +080017557 memset(args, 0, sizeof(args));
17558 strncpy(args, argv[i], sizeof(args));
17559 val = strtok(args, ";");
17560 while (val != NULL)
17561 {
17562 if (j == 0)
17563 {
17564 mac_addr_aton(neighborReports[i - 3].bssid, val);
17565 } else if (j == 1)
17566 {
developerc14d83a2023-06-29 20:09:42 +080017567 if (hal_strtoul(val, 16, &tmp) < 0) {
17568 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017569 }
17570 neighborReports[i - 3].info = tmp;
developera3511852023-06-14 14:12:59 +080017571 } else if (j == 2)
17572 {
developerc14d83a2023-06-29 20:09:42 +080017573 if (hal_strtoul(val, 16, &tmp) < 0) {
17574 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017575 }
17576 neighborReports[i - 3].opClass = tmp;
developer5b23cd02023-07-19 20:26:03 +080017577
developera3511852023-06-14 14:12:59 +080017578 } else if (j == 3)
17579 {
developerc14d83a2023-06-29 20:09:42 +080017580 if (hal_strtoul(val, 16, &tmp) < 0) {
17581 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017582 }
17583 neighborReports[i - 3].channel = tmp;
developera3511852023-06-14 14:12:59 +080017584 } else if (j == 4)
17585 {
developerc14d83a2023-06-29 20:09:42 +080017586 if (hal_strtoul(val, 16, &tmp) < 0) {
17587 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017588 }
17589 neighborReports[i - 3].phyTable = tmp;
developera3511852023-06-14 14:12:59 +080017590 } else {
17591 printf("Insufficient arguments]n\n");
17592 exit(-1);
17593 }
17594 val = strtok(NULL, ";");
17595 j++;
17596 }
17597 }
developer72fb0bb2023-01-11 09:46:29 +080017598
developera3511852023-06-14 14:12:59 +080017599 INT ret = wifi_setNeighborReports(index, argc - 3, neighborReports);
17600 if (ret != RETURN_OK)
17601 {
17602 printf("wifi_setNeighborReports ret = %d", ret);
17603 exit(-1);
17604 }
17605 }
17606 if(strstr(argv[1],"wifi_getRadioIfName")!=NULL)
17607 {
17608 if((ret=wifi_getRadioIfName(index, buf))==RETURN_OK)
17609 printf("%s.\n", buf);
17610 else
17611 printf("Error returned\n");
17612 }
17613 if(strstr(argv[1],"wifi_getApSecurityModesSupported")!=NULL)
17614 {
17615 if((ret=wifi_getApSecurityModesSupported(index, buf))==RETURN_OK)
17616 printf("%s.\n", buf);
17617 else
17618 printf("Error returned\n");
17619 }
17620 if(strstr(argv[1],"wifi_getRadioOperatingChannelBandwidth")!=NULL)
17621 {
17622 if (argc <= 2)
17623 {
17624 printf("Insufficient arguments\n");
17625 exit(-1);
17626 }
17627 char buf[64]= {'\0'};
17628 wifi_getRadioOperatingChannelBandwidth(index,buf);
17629 printf("Current bandwidth is %s \n",buf);
17630 return 0;
17631 }
17632 if(strstr(argv[1],"pushRadioChannel2")!=NULL)
17633 {
17634 if (argc <= 5)
17635 {
17636 printf("Insufficient arguments\n");
17637 exit(-1);
17638 }
17639 UINT channel = atoi(argv[3]);
17640 UINT width = atoi(argv[4]);
17641 UINT beacon = atoi(argv[5]);
17642 INT ret = wifi_pushRadioChannel2(index,channel,width,beacon);
17643 printf("Result = %d", ret);
17644 }
developercc5cbfb2023-06-13 18:29:52 +080017645 if(strstr(argv[1],"wifi_getApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017646 {
developercc5cbfb2023-06-13 18:29:52 +080017647 char br_name[64], ip[64], subset[64] = {0};
17648 wifi_getApBridgeInfo(0, br_name, ip, subset);
17649 printf("wifi_getApBridgeInfo br_name = %s, ip = %s, subset = %s\n", br_name, ip, subset);
developera3511852023-06-14 14:12:59 +080017650 }
developercc5cbfb2023-06-13 18:29:52 +080017651 if(strstr(argv[1],"wifi_enableGreylistAccessControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017652 {
developercc5cbfb2023-06-13 18:29:52 +080017653 int enable = atoi(argv[3]);
17654 wifi_enableGreylistAccessControl(enable == 0 ? FALSE : TRUE);
17655 printf("wifi_enableGreylistAccessControl enable=%d\n", enable);
developera3511852023-06-14 14:12:59 +080017656 }
developercc5cbfb2023-06-13 18:29:52 +080017657 if(strstr(argv[1],"wifi_setApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017658 {
developercc5cbfb2023-06-13 18:29:52 +080017659 wifi_setApBridgeInfo(0, argv[3], argv[4], argv[5]);
17660 printf("wifi_setApBridgeInfo br_name = %s, ip = %s, subset = %s\n", argv[3], argv[4], argv[5]);
developera3511852023-06-14 14:12:59 +080017661 }
developer72fb0bb2023-01-11 09:46:29 +080017662
developer6e578302023-06-21 10:11:16 +080017663 if(strstr(argv[1], "wifi_getATMCapable")!=NULL)
17664 {
17665 BOOL b = FALSE;
17666 BOOL *output_bool = &b;
17667 wifi_getATMCapable(output_bool);
17668 printf("ATM capable = %d \n",b);
17669 return 0;
17670 }
17671 if (strncmp(argv[1], "wifi_setATMEnable", strlen(argv[1])) == 0) {
17672 int enable = 0;
17673 if(argc <= 3)
17674 {
17675 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17676 exit(-1);
17677 }
17678 enable = atoi(argv[3]);
17679 wifi_setATMEnable(enable);
17680 return 0;
17681 }
17682 if (strncmp(argv[1], "wifi_getATMEnable", strlen(argv[1])) == 0) {
17683 BOOL b = FALSE;
17684 BOOL *output_bool = &b;
17685 wifi_getATMEnable(output_bool);
17686 printf("ATM enable = %d \n", b);
17687 return 0;
17688 }
17689 if (strncmp(argv[1], "wifi_setApATMAirTimePercent", strlen(argv[1])) == 0) {
17690 unsigned int percent = 0;
17691 if(argc <= 3)
17692 {
17693 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17694 exit(-1);
17695 }
17696 percent = atoi(argv[3]);
17697 wifi_setApATMAirTimePercent(index, percent);
17698 return 0;
17699 }
17700 if (strncmp(argv[1], "wifi_getApATMAirTimePercent", strlen(argv[1])) == 0) {
17701 unsigned int percent = 0;
17702 unsigned int *output = &percent;
17703
17704 wifi_getApATMAirTimePercent(index, output);
17705 printf("ATM percent = %d \n", percent);
17706 return 0;
17707 }
developer82533be2023-06-28 17:21:01 +080017708 if (strstr(argv[1],"setGF")!=NULL)
17709 {
17710 BOOL enable = atoi(argv[3]);
17711 if((ret=wifi_setRadio11nGreenfieldEnable(index, enable))==RETURN_OK)
17712 printf("wifi_setRadio11nGreenfieldEnable success\n");
17713 else
17714 printf("wifi_setRadio11nGreenfieldEnable Error\n");
17715 }
17716 if (strstr(argv[1],"setVID")!=NULL)
17717 {
17718 INT vid = atoi(argv[3]);
17719 if((ret=wifi_setApVlanID(index, vid))==RETURN_OK)
17720 printf("wifi_setApVlanID success.\n");
17721 else
17722 printf("wifi_setApVlanID Error\n");
developerd14dff12023-06-28 22:47:44 +080017723 }
17724 if (strncmp(argv[1], "wifi_getApATMSta", strlen(argv[1])) == 0) {
17725 UCHAR outbuf[256]={0};
17726
17727 wifi_getApATMSta(index, outbuf, sizeof(outbuf));
17728 printf("sta air time percent is %s \n", outbuf);
17729 return 0;
17730 }
developera3511852023-06-14 14:12:59 +080017731 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17732 return 0;
developer72fb0bb2023-01-11 09:46:29 +080017733}
17734
17735#endif
17736
17737#ifdef WIFI_HAL_VERSION_3
17738
developer32f2a182023-06-27 19:50:41 +080017739INT BitMapToTransmitRates(UINT bitMap, char *BasicRate, unsigned long size)
developer72fb0bb2023-01-11 09:46:29 +080017740{
developera3511852023-06-14 14:12:59 +080017741 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer32f2a182023-06-27 19:50:41 +080017742 if (bitMap & WIFI_BITRATE_1MBPS) {
17743 if ((size - strlen(BasicRate)) <= 2)
17744 return RETURN_ERR;
17745 strncat(BasicRate, "1,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17746 }
17747 if (bitMap & WIFI_BITRATE_2MBPS) {
17748 if ((size - strlen(BasicRate)) <= 2)
17749 return RETURN_ERR;
17750 strncat(BasicRate, "2,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17751 }
17752 if (bitMap & WIFI_BITRATE_5_5MBPS) {
17753 if ((size - strlen(BasicRate)) <= 4)
17754 return RETURN_ERR;
17755 strncat(BasicRate, "5.5,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17756 }
17757 if (bitMap & WIFI_BITRATE_6MBPS) {
17758 if ((size - strlen(BasicRate)) <= 2)
17759 return RETURN_ERR;
17760 strncat(BasicRate, "6,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17761 }
17762 if (bitMap & WIFI_BITRATE_9MBPS) {
17763 if ((size - strlen(BasicRate)) <= 2)
17764 return RETURN_ERR;
17765 strncat(BasicRate, "9,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17766 }
17767 if (bitMap & WIFI_BITRATE_11MBPS) {
17768 if ((size - strlen(BasicRate)) <= 3)
17769 return RETURN_ERR;
17770 strncat(BasicRate, "11,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17771 }
17772 if (bitMap & WIFI_BITRATE_12MBPS) {
17773 if ((size - strlen(BasicRate)) <= 3)
17774 return RETURN_ERR;
17775 strncat(BasicRate, "12,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17776 }
17777 if (bitMap & WIFI_BITRATE_18MBPS) {
17778 if ((size - strlen(BasicRate)) <= 3)
17779 return RETURN_ERR;
17780 strncat(BasicRate, "18,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17781 }
17782 if (bitMap & WIFI_BITRATE_24MBPS) {
17783 if ((size - strlen(BasicRate)) <= 3)
17784 return RETURN_ERR;
17785 strncat(BasicRate, "24,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17786 }
17787 if (bitMap & WIFI_BITRATE_36MBPS) {
17788 if ((size - strlen(BasicRate)) <= 3)
17789 return RETURN_ERR;
17790 strncat(BasicRate, "36,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17791 }
17792 if (bitMap & WIFI_BITRATE_48MBPS) {
17793 if ((size - strlen(BasicRate)) <= 3)
17794 return RETURN_ERR;
17795 strncat(BasicRate, "48,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17796 }
17797 if (bitMap & WIFI_BITRATE_54MBPS) {
17798 if ((size - strlen(BasicRate)) <= 3)
17799 return RETURN_ERR;
17800 strncat(BasicRate, "54,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17801 }
developera3511852023-06-14 14:12:59 +080017802 if (strlen(BasicRate) != 0) // remove last comma
17803 BasicRate[strlen(BasicRate) - 1] = '\0';
17804 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17805 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017806}
17807
17808INT TransmitRatesToBitMap (char *BasicRatesList, UINT *basicRateBitMap)
17809{
developera3511852023-06-14 14:12:59 +080017810 UINT BitMap = 0;
17811 char *rate;
developer72fb0bb2023-01-11 09:46:29 +080017812
developera3511852023-06-14 14:12:59 +080017813 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
17814 rate = strtok(BasicRatesList, ",");
17815 while(rate != NULL)
17816 {
17817 if (strcmp(rate, "1") == 0)
17818 BitMap |= WIFI_BITRATE_1MBPS;
17819 else if (strcmp(rate, "2") == 0)
17820 BitMap |= WIFI_BITRATE_2MBPS;
17821 else if (strcmp(rate, "5.5") == 0)
17822 BitMap |= WIFI_BITRATE_5_5MBPS;
17823 else if (strcmp(rate, "6") == 0)
17824 BitMap |= WIFI_BITRATE_6MBPS;
17825 else if (strcmp(rate, "9") == 0)
17826 BitMap |= WIFI_BITRATE_9MBPS;
17827 else if (strcmp(rate, "11") == 0)
17828 BitMap |= WIFI_BITRATE_11MBPS;
17829 else if (strcmp(rate, "12") == 0)
17830 BitMap |= WIFI_BITRATE_12MBPS;
17831 else if (strcmp(rate, "18") == 0)
17832 BitMap |= WIFI_BITRATE_18MBPS;
17833 else if (strcmp(rate, "24") == 0)
17834 BitMap |= WIFI_BITRATE_24MBPS;
17835 else if (strcmp(rate, "36") == 0)
17836 BitMap |= WIFI_BITRATE_36MBPS;
17837 else if (strcmp(rate, "48") == 0)
17838 BitMap |= WIFI_BITRATE_48MBPS;
17839 else if (strcmp(rate, "54") == 0)
17840 BitMap |= WIFI_BITRATE_54MBPS;
17841 rate = strtok(NULL, ",");
17842 }
17843 *basicRateBitMap = BitMap;
17844 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17845 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017846}
17847
17848// This API is used to configured all radio operation parameter in a single set. it includes channel number, channelWidth, mode and auto chammel configuration.
17849INT wifi_setRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17850{
developera3511852023-06-14 14:12:59 +080017851 char buf[128] = {0};
17852 int bandwidth = 20;
17853 int set_mode = 0;
developer56fbedb2023-05-30 16:47:05 +080017854 BOOL drv_dat_change = 0, hapd_conf_change = 0;
developera3511852023-06-14 14:12:59 +080017855 wifi_radio_operationParam_t current_param;
developer72fb0bb2023-01-11 09:46:29 +080017856
developera3511852023-06-14 14:12:59 +080017857 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017858
developera3511852023-06-14 14:12:59 +080017859 multiple_set = TRUE;
17860 if (wifi_getRadioOperatingParameters(index, &current_param) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017861 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingParameters return error.\n");
developera3511852023-06-14 14:12:59 +080017862 return RETURN_ERR;
17863 }
17864 if (current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17865 if (wifi_setRadioAutoChannelEnable(index, operationParam->autoChannelEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017866 wifi_debug(DEBUG_ERROR, "wifi_setRadioAutoChannelEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017867 return RETURN_ERR;
17868 }
17869 drv_dat_change = TRUE;
17870 }
17871 if (current_param.channelWidth != operationParam->channelWidth ||
17872 current_param.channel != operationParam->channel ||
17873 current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17874 if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_20MHZ)
17875 bandwidth = 20;
17876 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_40MHZ)
17877 bandwidth = 40;
17878 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80MHZ)
17879 bandwidth = 80;
17880 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_160MHZ || operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80_80MHZ)
17881 bandwidth = 160;
developer72fb0bb2023-01-11 09:46:29 +080017882
developera3511852023-06-14 14:12:59 +080017883 if (operationParam->autoChannelEnabled) {
17884 if (wifi_pushRadioChannel2(index, 0, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017885 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017886 return RETURN_ERR;
17887 }
17888 } else {
17889 if (wifi_pushRadioChannel2(index, operationParam->channel, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017890 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017891 return RETURN_ERR;
17892 }
17893 }
developer56fbedb2023-05-30 16:47:05 +080017894 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017895 }
17896 if (current_param.variant != operationParam->variant) {
17897 // Two different definition bit map, so need to check every bit.
17898 if (operationParam->variant & WIFI_80211_VARIANT_A)
17899 set_mode |= WIFI_MODE_A;
17900 if (operationParam->variant & WIFI_80211_VARIANT_B)
17901 set_mode |= WIFI_MODE_B;
17902 if (operationParam->variant & WIFI_80211_VARIANT_G)
17903 set_mode |= WIFI_MODE_G;
17904 if (operationParam->variant & WIFI_80211_VARIANT_N)
17905 set_mode |= WIFI_MODE_N;
17906 if (operationParam->variant & WIFI_80211_VARIANT_AC)
17907 set_mode |= WIFI_MODE_AC;
17908 if (operationParam->variant & WIFI_80211_VARIANT_AX)
17909 set_mode |= WIFI_MODE_AX;
17910 // Second parameter is to set channel band width, it is done by wifi_pushRadioChannel2 if changed.
17911 memset(buf, 0, sizeof(buf));
17912 drv_dat_change = TRUE;
17913 if (wifi_setRadioMode_by_dat(index, set_mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017914 wifi_debug(DEBUG_ERROR, "wifi_setRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080017915 return RETURN_ERR;
17916 }
17917 }
17918 if (current_param.dtimPeriod != operationParam->dtimPeriod) {
developer56fbedb2023-05-30 16:47:05 +080017919 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017920 if (wifi_setApDTIMInterval(index, operationParam->dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017921 wifi_debug(DEBUG_ERROR, "wifi_setApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017922 return RETURN_ERR;
17923 }
17924 }
17925 if (current_param.beaconInterval != operationParam->beaconInterval) {
developer56fbedb2023-05-30 16:47:05 +080017926 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017927 if (wifi_setRadioBeaconPeriod(index, operationParam->beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017928 wifi_debug(DEBUG_ERROR, "wifi_setRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080017929 return RETURN_ERR;
17930 }
17931 }
17932 if (current_param.operationalDataTransmitRates != operationParam->operationalDataTransmitRates) {
developer56fbedb2023-05-30 16:47:05 +080017933 hapd_conf_change = TRUE;
developer32f2a182023-06-27 19:50:41 +080017934 BitMapToTransmitRates(operationParam->operationalDataTransmitRates, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080017935 if (wifi_setRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017936 wifi_debug(DEBUG_ERROR, "wifi_setRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080017937 return RETURN_ERR;
17938 }
17939 }
17940 if (current_param.fragmentationThreshold != operationParam->fragmentationThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017941 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017942 if (wifi_setRadioFragmentationThreshold(index, operationParam->fragmentationThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017943 wifi_debug(DEBUG_ERROR, "wifi_setRadioFragmentationThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017944 return RETURN_ERR;
17945 }
17946 }
17947 if (current_param.guardInterval != operationParam->guardInterval) {
developer56fbedb2023-05-30 16:47:05 +080017948 hapd_conf_change = TRUE;
17949 drv_dat_change = TRUE;
17950 if (wifi_setGuardInterval(index, operationParam->guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017951 wifi_debug(DEBUG_ERROR, "wifi_setGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017952 return RETURN_ERR;
17953 }
17954 }
17955 if (current_param.transmitPower != operationParam->transmitPower) {
developer56fbedb2023-05-30 16:47:05 +080017956 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017957 if (wifi_setRadioTransmitPower(index, operationParam->transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017958 wifi_debug(DEBUG_ERROR, "wifi_setRadioTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080017959 return RETURN_ERR;
17960 }
17961 }
17962 if (current_param.rtsThreshold != operationParam->rtsThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017963 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017964 if (wifi_setApRtsThreshold(index, operationParam->rtsThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017965 wifi_debug(DEBUG_ERROR, "wifi_setApRtsThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017966 return RETURN_ERR;
17967 }
17968 }
17969 if (current_param.obssCoex != operationParam->obssCoex) {
developer56fbedb2023-05-30 16:47:05 +080017970 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017971 if (wifi_setRadioObssCoexistenceEnable(index, operationParam->obssCoex) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017972 wifi_debug(DEBUG_ERROR, "wifi_setRadioObssCoexistenceEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017973 return RETURN_ERR;
17974 }
17975 }
17976 if (current_param.stbcEnable != operationParam->stbcEnable) {
developer56fbedb2023-05-30 16:47:05 +080017977 hapd_conf_change = TRUE;
17978 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017979 if (wifi_setRadioSTBCEnable(index, operationParam->stbcEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017980 wifi_debug(DEBUG_ERROR, "wifi_setRadioSTBCEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017981 return RETURN_ERR;
17982 }
17983 }
17984 if (current_param.greenFieldEnable != operationParam->greenFieldEnable) {
17985 if (wifi_setRadio11nGreenfieldEnable(index, operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017986 wifi_debug(DEBUG_ERROR, "wifi_setRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017987 return RETURN_ERR;
17988 }
17989 }
developer72fb0bb2023-01-11 09:46:29 +080017990
developera3511852023-06-14 14:12:59 +080017991 /* only down/up interface when dat file has been changed,
17992 * if enable is true, then restart the radio.
17993 */
17994 if (drv_dat_change == TRUE) {
17995 wifi_setRadioEnable(index, FALSE);
developer56fbedb2023-05-30 16:47:05 +080017996 if (operationParam->enable == TRUE)
developera3511852023-06-14 14:12:59 +080017997 wifi_setRadioEnable(index, TRUE);
17998 } else if (hapd_conf_change == TRUE) {
17999 hostapd_raw_remove_bss(index);
18000 if (operationParam->enable == TRUE)
18001 hostapd_raw_add_bss(index);
18002 }
developer56fbedb2023-05-30 16:47:05 +080018003
developera3511852023-06-14 14:12:59 +080018004 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018005
developera3511852023-06-14 14:12:59 +080018006 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018007}
18008
18009INT wifi_getRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
18010{
developera3511852023-06-14 14:12:59 +080018011 char band[64] = {0};
18012 char buf[256] = {0};
18013 char config_file[64] = {0};
18014 char cmd[128] = {0};
18015 UINT mode = 0;
18016 BOOL enabled = FALSE;
developer863a4a62023-06-06 16:55:59 +080018017 int dtimPeriod;
developer2f79c922023-06-02 17:33:42 +080018018 UINT beaconInterval;
18019 UINT basicDataTransmitRates;
18020 UINT operationalDataTransmitRates;
18021 wifi_guard_interval_t guardInterval;
18022 UINT transmitPower;
developere40952c2023-06-15 18:46:43 +080018023 int res;
developerc14d83a2023-06-29 20:09:42 +080018024 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080018025
developera3511852023-06-14 14:12:59 +080018026 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18027 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018028
developera3511852023-06-14 14:12:59 +080018029 memset(operationParam, 0, sizeof(wifi_radio_operationParam_t));
developere40952c2023-06-15 18:46:43 +080018030 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, index);
18031 if (os_snprintf_error(sizeof(config_file), res)) {
18032 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18033 return RETURN_ERR;
18034 }
developera3511852023-06-14 14:12:59 +080018035 if (wifi_getRadioEnable(index, &enabled) != RETURN_OK)
18036 {
developer75bd10c2023-06-27 11:34:08 +080018037 wifi_debug(DEBUG_ERROR, "wifi_getRadioEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018038 return RETURN_ERR;
18039 }
18040 operationParam->enable = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018041
developera3511852023-06-14 14:12:59 +080018042 memset(band, 0, sizeof(band));
18043 if (wifi_getRadioOperatingFrequencyBand(index, band) != RETURN_OK)
18044 {
developer75bd10c2023-06-27 11:34:08 +080018045 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingFrequencyBand return error.\n");
developera3511852023-06-14 14:12:59 +080018046 return RETURN_ERR;
18047 }
developer72fb0bb2023-01-11 09:46:29 +080018048
developera3511852023-06-14 14:12:59 +080018049 if (!strcmp(band, "2.4GHz"))
18050 operationParam->band = WIFI_FREQUENCY_2_4_BAND;
18051 else if (!strcmp(band, "5GHz"))
18052 operationParam->band = WIFI_FREQUENCY_5_BAND;
18053 else if (!strcmp(band, "6GHz"))
18054 operationParam->band = WIFI_FREQUENCY_6_BAND;
18055 else
18056 {
developer75bd10c2023-06-27 11:34:08 +080018057 wifi_debug(DEBUG_ERROR, "cannot decode band for radio index %d ('%s')\n", index, band);
developera3511852023-06-14 14:12:59 +080018058 }
developer72fb0bb2023-01-11 09:46:29 +080018059
developera3511852023-06-14 14:12:59 +080018060 wifi_hostapdRead(config_file, "channel", buf, sizeof(buf));
18061 if (strcmp(buf, "0") == 0 || strcmp(buf, "acs_survey") == 0) {
18062 operationParam->channel = 0;
18063 operationParam->autoChannelEnabled = TRUE;
18064 } else {
developerc14d83a2023-06-29 20:09:42 +080018065 if (hal_strtoul(buf, 10, &tmp) < 0) {
18066 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080018067 }
18068 operationParam->channel = tmp;
developer5b23cd02023-07-19 20:26:03 +080018069
developera3511852023-06-14 14:12:59 +080018070 operationParam->autoChannelEnabled = FALSE;
18071 }
developer72fb0bb2023-01-11 09:46:29 +080018072
developera3511852023-06-14 14:12:59 +080018073 memset(buf, 0, sizeof(buf));
18074 if (wifi_getRadioOperatingChannelBandwidth(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018075 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error.\n");
developera3511852023-06-14 14:12:59 +080018076 return RETURN_ERR;
18077 }
18078 if (!strcmp(buf, "20MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
18079 else if (!strcmp(buf, "40MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
18080 else if (!strcmp(buf, "80MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
18081 else if (!strcmp(buf, "160MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
18082 else
18083 {
developer75bd10c2023-06-27 11:34:08 +080018084 wifi_debug(DEBUG_ERROR, "Unknown channel bandwidth: %s\n", buf);
developera3511852023-06-14 14:12:59 +080018085 return false;
18086 }
developer72fb0bb2023-01-11 09:46:29 +080018087
developera3511852023-06-14 14:12:59 +080018088 if (wifi_getRadioMode(index, buf, &mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018089 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080018090 return RETURN_ERR;
18091 }
18092 // Two different definition bit map, so need to check every bit.
18093 if (mode & WIFI_MODE_A)
18094 operationParam->variant |= WIFI_80211_VARIANT_A;
18095 if (mode & WIFI_MODE_B)
18096 operationParam->variant |= WIFI_80211_VARIANT_B;
18097 if (mode & WIFI_MODE_G)
18098 operationParam->variant |= WIFI_80211_VARIANT_G;
18099 if (mode & WIFI_MODE_N)
18100 operationParam->variant |= WIFI_80211_VARIANT_N;
18101 if (mode & WIFI_MODE_AC)
18102 operationParam->variant |= WIFI_80211_VARIANT_AC;
18103 if (mode & WIFI_MODE_AX)
18104 operationParam->variant |= WIFI_80211_VARIANT_AX;
18105 if (wifi_getRadioDCSEnable(index, &operationParam->DCSEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018106 wifi_debug(DEBUG_ERROR, "wifi_getRadioDCSEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018107 return RETURN_ERR;
18108 }
18109 if (wifi_getApDTIMInterval(index, &dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018110 wifi_debug(DEBUG_ERROR, "wifi_getApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018111 return RETURN_ERR;
18112 }
developer2f79c922023-06-02 17:33:42 +080018113 operationParam->dtimPeriod = dtimPeriod;
developera3511852023-06-14 14:12:59 +080018114 if (wifi_getRadioBeaconPeriod(index, &beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018115 wifi_debug(DEBUG_ERROR, "wifi_getRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080018116 return RETURN_ERR;
18117 }
developer2f79c922023-06-02 17:33:42 +080018118 operationParam->beaconInterval = beaconInterval;
developer72fb0bb2023-01-11 09:46:29 +080018119
developera3511852023-06-14 14:12:59 +080018120 memset(buf, 0, sizeof(buf));
18121 if (wifi_getRadioSupportedDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018122 wifi_debug(DEBUG_ERROR, "wifi_getRadioSupportedDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018123 return RETURN_ERR;
18124 }
18125 TransmitRatesToBitMap(buf, &basicDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018126 operationParam->basicDataTransmitRates = basicDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018127
developera3511852023-06-14 14:12:59 +080018128 memset(buf, 0, sizeof(buf));
18129 if (wifi_getRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018130 wifi_debug(DEBUG_ERROR, "wifi_getRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018131 return RETURN_ERR;
18132 }
18133 TransmitRatesToBitMap(buf, &operationalDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018134 operationParam->operationalDataTransmitRates = operationalDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018135
developera3511852023-06-14 14:12:59 +080018136 memset(buf, 0, sizeof(buf));
18137 wifi_hostapdRead(config_file, "fragm_threshold", buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080018138 if (hal_strtoul(buf, 10, &tmp) < 0) {
18139 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18140 }
18141 operationParam->fragmentationThreshold = tmp;
developer72fb0bb2023-01-11 09:46:29 +080018142
developera3511852023-06-14 14:12:59 +080018143 if (wifi_getGuardInterval(index, &guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018144 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018145 return RETURN_ERR;
18146 }
developer2f79c922023-06-02 17:33:42 +080018147 operationParam->guardInterval = guardInterval;
18148
developera3511852023-06-14 14:12:59 +080018149 if (wifi_getRadioPercentageTransmitPower(index, (ULONG *)&transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018150 wifi_debug(DEBUG_ERROR, "wifi_getRadioPercentageTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080018151 return RETURN_ERR;
18152 }
developer2f79c922023-06-02 17:33:42 +080018153 operationParam->transmitPower = transmitPower;
developer72fb0bb2023-01-11 09:46:29 +080018154
developera3511852023-06-14 14:12:59 +080018155 memset(buf, 0, sizeof(buf));
18156 wifi_hostapdRead(config_file, "rts_threshold", buf, sizeof(buf));
18157 if (strcmp(buf, "-1") == 0) {
18158 operationParam->rtsThreshold = (UINT)-1; // maxuimum unsigned integer value
18159 operationParam->ctsProtection = FALSE;
18160 } else {
developerc14d83a2023-06-29 20:09:42 +080018161 if (hal_strtoul(buf, 10, &tmp) < 0) {
18162 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18163 }
18164 operationParam->rtsThreshold = tmp;
developera3511852023-06-14 14:12:59 +080018165 operationParam->ctsProtection = TRUE;
18166 }
developer72fb0bb2023-01-11 09:46:29 +080018167
developera3511852023-06-14 14:12:59 +080018168 memset(buf, 0, sizeof(buf));
18169 wifi_hostapdRead(config_file, "ht_coex", buf, sizeof(buf));
18170 if (strcmp(buf, "0") == 0)
18171 operationParam->obssCoex = FALSE;
18172 else
18173 operationParam->obssCoex = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018174
developere40952c2023-06-15 18:46:43 +080018175 res = snprintf(cmd, sizeof(cmd), "cat %s | grep STBC", config_file);
18176 if (os_snprintf_error(sizeof(cmd), res)) {
18177 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18178 return RETURN_ERR;
18179 }
developera3511852023-06-14 14:12:59 +080018180 _syscmd(cmd, buf, sizeof(buf));
18181 if (strlen(buf) != 0)
18182 operationParam->stbcEnable = TRUE;
18183 else
18184 operationParam->stbcEnable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018185
developera3511852023-06-14 14:12:59 +080018186 if (wifi_getRadio11nGreenfieldEnable(index, &operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018187 wifi_debug(DEBUG_ERROR, "wifi_getRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018188 return RETURN_ERR;
18189 }
developer72fb0bb2023-01-11 09:46:29 +080018190
developera3511852023-06-14 14:12:59 +080018191 // Below value is hardcoded
developer72fb0bb2023-01-11 09:46:29 +080018192
developera3511852023-06-14 14:12:59 +080018193 operationParam->numSecondaryChannels = 0;
18194 for (int i = 0; i < MAXNUMSECONDARYCHANNELS; i++) {
18195 operationParam->channelSecondary[i] = 0;
18196 }
18197 operationParam->csa_beacon_count = 15;
18198 operationParam->countryCode = wifi_countrycode_US; // hard to convert string to corresponding enum
developer72fb0bb2023-01-11 09:46:29 +080018199
developera3511852023-06-14 14:12:59 +080018200 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18201 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018202}
18203
18204static int array_index_to_vap_index(UINT radioIndex, int arrayIndex)
18205{
developera3511852023-06-14 14:12:59 +080018206 int max_radio_num = 0;
developer72fb0bb2023-01-11 09:46:29 +080018207
developera3511852023-06-14 14:12:59 +080018208 wifi_getMaxRadioNumber(&max_radio_num);
18209 if (radioIndex >= max_radio_num) {
developer75bd10c2023-06-27 11:34:08 +080018210 wifi_debug(DEBUG_ERROR, "Wrong radio index (%d)\n", radioIndex);
developera3511852023-06-14 14:12:59 +080018211 return RETURN_ERR;
18212 }
developer72fb0bb2023-01-11 09:46:29 +080018213
developera3511852023-06-14 14:12:59 +080018214 return (arrayIndex * max_radio_num) + radioIndex;
developer72fb0bb2023-01-11 09:46:29 +080018215}
18216
developer96b38512023-02-22 11:17:45 +080018217static int vap_index_to_array_index(int vapIndex, int *radioIndex, int *arrayIndex)
18218{
developera3511852023-06-14 14:12:59 +080018219 int max_radio_num = 0;
developer96b38512023-02-22 11:17:45 +080018220
developera3511852023-06-14 14:12:59 +080018221 if ((vapIndex < 0) || (vapIndex > MAX_NUM_VAP_PER_RADIO*MAX_NUM_RADIOS))
developer96b38512023-02-22 11:17:45 +080018222 return -1;
18223
developera3511852023-06-14 14:12:59 +080018224 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080018225 if(max_radio_num == 0){
18226 return RETURN_ERR;
18227 }
developera3511852023-06-14 14:12:59 +080018228 (*radioIndex) = vapIndex % max_radio_num;
18229 (*arrayIndex) = vapIndex / max_radio_num;
developer96b38512023-02-22 11:17:45 +080018230
developera3511852023-06-14 14:12:59 +080018231 return 0;
developer96b38512023-02-22 11:17:45 +080018232}
18233
18234
developer72fb0bb2023-01-11 09:46:29 +080018235wifi_bitrate_t beaconRate_string_to_enum(char *beaconRate) {
developera3511852023-06-14 14:12:59 +080018236 if (strncmp(beaconRate, "1Mbps", 5) == 0)
18237 return WIFI_BITRATE_1MBPS;
18238 else if (strncmp(beaconRate, "2Mbps", 5) == 0)
18239 return WIFI_BITRATE_2MBPS;
18240 else if (strncmp(beaconRate, "5.5Mbps", 7) == 0)
18241 return WIFI_BITRATE_5_5MBPS;
18242 else if (strncmp(beaconRate, "6Mbps", 5) == 0)
18243 return WIFI_BITRATE_6MBPS;
18244 else if (strncmp(beaconRate, "9Mbps", 5) == 0)
18245 return WIFI_BITRATE_9MBPS;
18246 else if (strncmp(beaconRate, "11Mbps", 6) == 0)
18247 return WIFI_BITRATE_11MBPS;
18248 else if (strncmp(beaconRate, "12Mbps", 6) == 0)
18249 return WIFI_BITRATE_12MBPS;
18250 else if (strncmp(beaconRate, "18Mbps", 6) == 0)
18251 return WIFI_BITRATE_18MBPS;
18252 else if (strncmp(beaconRate, "24Mbps", 6) == 0)
18253 return WIFI_BITRATE_24MBPS;
18254 else if (strncmp(beaconRate, "36Mbps", 6) == 0)
18255 return WIFI_BITRATE_36MBPS;
18256 else if (strncmp(beaconRate, "48Mbps", 6) == 0)
18257 return WIFI_BITRATE_48MBPS;
18258 else if (strncmp(beaconRate, "54Mbps", 6) == 0)
18259 return WIFI_BITRATE_54MBPS;
18260 return WIFI_BITRATE_DEFAULT;
developer72fb0bb2023-01-11 09:46:29 +080018261}
18262
developer32f2a182023-06-27 19:50:41 +080018263struct beacon_rate_2_string {
18264 wifi_bitrate_t beacon;
18265 char beacon_str[8];
18266};
18267
18268struct beacon_rate_2_string br2str[12] = {
18269 {WIFI_BITRATE_1MBPS, "1Mbps"},
18270 {WIFI_BITRATE_2MBPS, "2Mbps"},
18271 {WIFI_BITRATE_5_5MBPS, "5.5Mbps"},
18272 {WIFI_BITRATE_6MBPS, "6Mbps"},
18273 {WIFI_BITRATE_9MBPS, "9Mbps"},
18274 {WIFI_BITRATE_11MBPS, "11Mbps"},
18275 {WIFI_BITRATE_12MBPS, "12Mbps"},
18276 {WIFI_BITRATE_18MBPS, "18Mbps"},
18277 {WIFI_BITRATE_24MBPS, "24Mbps"},
18278 {WIFI_BITRATE_36MBPS, "36Mbps"},
18279 {WIFI_BITRATE_48MBPS, "48Mbps"},
18280 {WIFI_BITRATE_54MBPS, "54Mbps"}
18281};
18282
18283INT beaconRate_enum_to_string(wifi_bitrate_t beacon, char *beacon_str, unsigned long str_size)
developer72fb0bb2023-01-11 09:46:29 +080018284{
developer32f2a182023-06-27 19:50:41 +080018285 int i;
18286 unsigned long len;
18287
18288 for (i = 0; i < (sizeof(br2str)/sizeof(br2str[0])); i++) {
18289 if (beacon == br2str[i].beacon) {
18290 len = strlen(br2str[i].beacon_str);
18291 if (len >= str_size)
18292 return RETURN_ERR;
18293 memcpy(beacon_str, br2str[i].beacon_str, len);
18294 beacon_str[len] = '\0';
18295 break;
18296 }
18297 }
developera3511852023-06-14 14:12:59 +080018298 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018299}
18300
18301INT wifi_getRadioVapInfoMap(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18302{
developera3511852023-06-14 14:12:59 +080018303 INT mode = 0;
18304 INT ret = -1;
18305 UINT output = 0;
18306 int i = 0;
18307 int vap_index = 0;
18308 BOOL enabled = FALSE;
18309 char buf[32] = {0};
18310 wifi_vap_security_t security = {0};
developere40952c2023-06-15 18:46:43 +080018311 int res;
developer72fb0bb2023-01-11 09:46:29 +080018312
developera3511852023-06-14 14:12:59 +080018313 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18314 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018315
developera3511852023-06-14 14:12:59 +080018316 ret = wifi_BandProfileRead(0, index, "BssidNum", buf, sizeof(buf), "0");
18317 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +080018318 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead BssidNum failed\n");
developera3511852023-06-14 14:12:59 +080018319 return RETURN_ERR;
18320 }
developerfde01262023-05-22 15:15:24 +080018321
developera3511852023-06-14 14:12:59 +080018322 map->num_vaps = atoi(buf);
18323 if (map->num_vaps <= 0) {
developer75bd10c2023-06-27 11:34:08 +080018324 wifi_debug(DEBUG_ERROR, "invalid BssidNum %s\n", buf);
developera3511852023-06-14 14:12:59 +080018325 return RETURN_ERR;
18326 }
developerfde01262023-05-22 15:15:24 +080018327
developera3511852023-06-14 14:12:59 +080018328 for (i = 0; i < map->num_vaps; i++)
18329 {
18330 map->vap_array[i].radio_index = index;
developer72fb0bb2023-01-11 09:46:29 +080018331
developera3511852023-06-14 14:12:59 +080018332 vap_index = array_index_to_vap_index(index, i);
18333 if (vap_index < 0)
18334 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080018335
developer9ce44382023-06-28 11:09:37 +080018336 strncpy(map->vap_array[i].bridge_name, BRIDGE_NAME,sizeof(map->vap_array[i].bridge_name) - 1);
18337 map->vap_array[i].bridge_name[sizeof(map->vap_array[i].bridge_name) - 1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080018338
developera3511852023-06-14 14:12:59 +080018339 map->vap_array[i].vap_index = vap_index;
developer72fb0bb2023-01-11 09:46:29 +080018340
developera3511852023-06-14 14:12:59 +080018341 memset(buf, 0, sizeof(buf));
18342 ret = wifi_getApName(vap_index, buf);
18343 if (ret != RETURN_OK) {
18344 printf("%s: wifi_getApName return error\n", __func__);
18345 return RETURN_ERR;
18346 }
developere40952c2023-06-15 18:46:43 +080018347 res = snprintf(map->vap_array[i].vap_name, sizeof(map->vap_array[i].vap_name), "%s", buf);
18348 if (os_snprintf_error(sizeof(map->vap_array[i].vap_name), res)) {
18349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18350 return RETURN_ERR;
18351 }
developer72fb0bb2023-01-11 09:46:29 +080018352
developera3511852023-06-14 14:12:59 +080018353 memset(buf, 0, sizeof(buf));
18354 ret = wifi_getSSIDName(vap_index, buf);
18355 if (ret != RETURN_OK) {
18356 printf("%s: wifi_getSSIDName return error\n", __func__);
18357 return RETURN_ERR;
18358 }
developere40952c2023-06-15 18:46:43 +080018359 res = snprintf(map->vap_array[i].u.bss_info.ssid, sizeof(map->vap_array[i].u.bss_info.ssid), "%s", buf);
18360 if (os_snprintf_error(sizeof(map->vap_array[i].u.bss_info.ssid), res)) {
18361 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18362 return RETURN_ERR;
18363 }
developer72fb0bb2023-01-11 09:46:29 +080018364
developera3511852023-06-14 14:12:59 +080018365 map->vap_array[i].u.bss_info.enabled = true;
developer72fb0bb2023-01-11 09:46:29 +080018366
developera3511852023-06-14 14:12:59 +080018367 ret = wifi_getApSsidAdvertisementEnable(vap_index, &enabled);
18368 if (ret != RETURN_OK) {
18369 printf("%s: wifi_getApSsidAdvertisementEnable return error\n", __func__);
18370 return RETURN_ERR;
18371 }
18372 map->vap_array[i].u.bss_info.showSsid = enabled;
developer69b61b02023-03-07 17:17:44 +080018373
developera3511852023-06-14 14:12:59 +080018374 ret = wifi_getApIsolationEnable(vap_index, &enabled);
18375 if (ret != RETURN_OK) {
18376 printf("%s: wifi_getApIsolationEnable return error\n", __func__);
18377 return RETURN_ERR;
18378 }
18379 map->vap_array[i].u.bss_info.isolation = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018380
developera3511852023-06-14 14:12:59 +080018381 ret = wifi_getApMaxAssociatedDevices(vap_index, &output);
18382 if (ret != RETURN_OK) {
18383 printf("%s: wifi_getApMaxAssociatedDevices return error\n", __func__);
18384 return RETURN_ERR;
18385 }
18386 map->vap_array[i].u.bss_info.bssMaxSta = output;
developer72fb0bb2023-01-11 09:46:29 +080018387
developera3511852023-06-14 14:12:59 +080018388 ret = wifi_getBSSTransitionActivation(vap_index, &enabled);
18389 if (ret != RETURN_OK) {
18390 printf("%s: wifi_getBSSTransitionActivation return error\n", __func__);
18391 return RETURN_ERR;
18392 }
18393 map->vap_array[i].u.bss_info.bssTransitionActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018394
developera3511852023-06-14 14:12:59 +080018395 ret = wifi_getNeighborReportActivation(vap_index, &enabled);
18396 if (ret != RETURN_OK) {
18397 printf("%s: wifi_getNeighborReportActivation return error\n", __func__);
18398 return RETURN_ERR;
18399 }
18400 map->vap_array[i].u.bss_info.nbrReportActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018401
developera3511852023-06-14 14:12:59 +080018402 ret = wifi_getApSecurity(vap_index, &security);
18403 if (ret != RETURN_OK) {
18404 printf("%s: wifi_getApSecurity return error\n", __func__);
18405 return RETURN_ERR;
18406 }
18407 map->vap_array[i].u.bss_info.security = security;
developer72fb0bb2023-01-11 09:46:29 +080018408
developera3511852023-06-14 14:12:59 +080018409 ret = wifi_getApMacAddressControlMode(vap_index, &mode);
18410 if (ret != RETURN_OK) {
18411 printf("%s: wifi_getApMacAddressControlMode return error\n", __func__);
18412 return RETURN_ERR;
18413 }
18414 if (mode == 0)
18415 map->vap_array[i].u.bss_info.mac_filter_enable = FALSE;
18416 else
18417 map->vap_array[i].u.bss_info.mac_filter_enable = TRUE;
18418 if (mode == 1)
18419 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
18420 else if (mode == 2)
18421 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
developer72fb0bb2023-01-11 09:46:29 +080018422
developera3511852023-06-14 14:12:59 +080018423 ret = wifi_getApWmmEnable(vap_index, &enabled);
18424 if (ret != RETURN_OK) {
18425 printf("%s: wifi_getApWmmEnable return error\n", __func__);
18426 return RETURN_ERR;
18427 }
18428 map->vap_array[i].u.bss_info.wmm_enabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018429
developera3511852023-06-14 14:12:59 +080018430 ret = wifi_getApUAPSDCapability(vap_index, &enabled);
18431 if (ret != RETURN_OK) {
18432 printf("%s: wifi_getApUAPSDCapability return error\n", __func__);
18433 return RETURN_ERR;
18434 }
18435 map->vap_array[i].u.bss_info.UAPSDEnabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018436
developera3511852023-06-14 14:12:59 +080018437 memset(buf, 0, sizeof(buf));
18438 ret = wifi_getApBeaconRate(map->vap_array[i].radio_index, buf);
18439 if (ret != RETURN_OK) {
18440 printf("%s: wifi_getApBeaconRate return error\n", __func__);
18441 return RETURN_ERR;
18442 }
18443 map->vap_array[i].u.bss_info.beaconRate = beaconRate_string_to_enum(buf);
developer72fb0bb2023-01-11 09:46:29 +080018444
developera3511852023-06-14 14:12:59 +080018445 memset(buf, 0, sizeof(buf));
18446 ret = wifi_getBaseBSSID(vap_index, buf);
18447 if (ret != RETURN_OK) {
18448 printf("%s: wifi_getBaseBSSID return error\n", __func__);
18449 return RETURN_ERR;
18450 }
developer5b2f10c2023-05-25 17:02:21 +080018451 if (hwaddr_aton2(buf, map->vap_array[i].u.bss_info.bssid) < 0) {
18452 printf("%s: hwaddr_aton2 fail\n", __func__);
developera3511852023-06-14 14:12:59 +080018453 return RETURN_ERR;
developer5b2f10c2023-05-25 17:02:21 +080018454 }
developer72fb0bb2023-01-11 09:46:29 +080018455
developera3511852023-06-14 14:12:59 +080018456 ret = wifi_getRadioIGMPSnoopingEnable(map->vap_array[i].radio_index, &enabled);
18457 if (ret != RETURN_OK) {
developerc14d83a2023-06-29 20:09:42 +080018458 wifi_debug(DEBUG_ERROR, "%s: wifi_getRadioIGMPSnoopingEnable\n", __func__);
developera3511852023-06-14 14:12:59 +080018459 return RETURN_ERR;
18460 }
18461 map->vap_array[i].u.bss_info.mcast2ucast = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018462
developera3511852023-06-14 14:12:59 +080018463 // TODO: wps, noack
18464 }
18465 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18466 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018467}
18468
developer47cc27a2023-05-17 23:09:58 +080018469void checkVapStatus(int apIndex, BOOL *enable)
developer72fb0bb2023-01-11 09:46:29 +080018470{
developera3511852023-06-14 14:12:59 +080018471 char if_name[16] = {0};
18472 char cmd[128] = {0};
18473 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080018474 int res;
developer72fb0bb2023-01-11 09:46:29 +080018475
developera3511852023-06-14 14:12:59 +080018476 *enable = FALSE;
18477 if (wifi_GetInterfaceName(apIndex, if_name) != RETURN_OK)
18478 return;
developer72fb0bb2023-01-11 09:46:29 +080018479
developere40952c2023-06-15 18:46:43 +080018480 res = snprintf(cmd, sizeof(cmd), "cat %s | grep ^%s=1", VAP_STATUS_FILE, if_name);
18481 if (os_snprintf_error(sizeof(cmd), res)) {
18482 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18483 return;
18484 }
developera3511852023-06-14 14:12:59 +080018485 _syscmd(cmd, buf, sizeof(buf));
18486 if (strlen(buf) > 0)
18487 *enable = TRUE;
18488 return;
developer72fb0bb2023-01-11 09:46:29 +080018489}
18490
developer56fbedb2023-05-30 16:47:05 +080018491int hostapd_manage_bss(INT apIndex, BOOL enable)
18492{
18493 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080018494 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer56fbedb2023-05-30 16:47:05 +080018495 char cmd[MAX_CMD_SIZE] = {0};
18496 char buf[MAX_BUF_SIZE] = {0};
18497 BOOL status = FALSE;
18498 int max_radio_num = 0;
18499 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080018500 int res;
developer56fbedb2023-05-30 16:47:05 +080018501
18502 wifi_getApEnable(apIndex, &status);
18503
18504 wifi_getMaxRadioNumber(&max_radio_num);
18505 if (enable == status)
18506 return RETURN_OK;
18507
18508 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
18509 return RETURN_ERR;
18510
18511 if (enable == TRUE) {
18512 int radioIndex = apIndex % max_radio_num;
18513 phyId = radio_index_to_phy(radioIndex);
developerc14d83a2023-06-29 20:09:42 +080018514 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080018515 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
18516 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18517 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18518 return RETURN_ERR;
18519 }
18520 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
18521 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18522 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18523 return RETURN_ERR;
18524 }
developer56fbedb2023-05-30 16:47:05 +080018525 _syscmd(cmd, buf, sizeof(buf));
18526 } else {
developerc14d83a2023-06-29 20:09:42 +080018527 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080018528 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
18529 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18530 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18531 return RETURN_ERR;
18532 }
developer56fbedb2023-05-30 16:47:05 +080018533 _syscmd(cmd, buf, sizeof(buf));
18534 }
developere40952c2023-06-15 18:46:43 +080018535 res = snprintf(cmd, MAX_CMD_SIZE, "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer56fbedb2023-05-30 16:47:05 +080018536 interface_name, interface_name, enable, VAP_STATUS_FILE);
developere40952c2023-06-15 18:46:43 +080018537 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18538 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18539 return RETURN_ERR;
18540 }
developer56fbedb2023-05-30 16:47:05 +080018541 _syscmd(cmd, buf, sizeof(buf));
18542 //Wait for wifi up/down to apply
18543 return RETURN_OK;
18544}
18545
18546int hostapd_raw_add_bss(int apIndex)
18547{
18548 return hostapd_manage_bss(apIndex, TRUE);
18549}
18550
18551int hostapd_raw_remove_bss(int apIndex)
18552{
18553 return hostapd_manage_bss(apIndex, FALSE);
18554}
18555
18556int hostapd_raw_restart_bss(int apIndex)
developer333c1eb2023-05-31 14:59:39 +080018557{
developerdaf24792023-06-06 11:40:04 +080018558 int ret = 0;
18559
18560 ret = hostapd_raw_remove_bss(apIndex);
18561 if(ret != RETURN_OK)
18562 return RETURN_ERR;
18563
18564 ret = hostapd_raw_add_bss(apIndex);
18565 if(ret != RETURN_OK)
18566 return RETURN_ERR;
18567
18568 return RETURN_OK;
developer56fbedb2023-05-30 16:47:05 +080018569}
18570
developer72fb0bb2023-01-11 09:46:29 +080018571INT wifi_createVAP(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18572{
developera3511852023-06-14 14:12:59 +080018573 unsigned int i;
18574 wifi_vap_info_t *vap_info = NULL;
18575 int acl_mode;
18576 int ret = 0;
18577 char buf[256] = {0};
18578 char cmd[128] = {0};
18579 char config_file[64] = {0};
18580 char psk_file[64] = {0};
18581 BOOL enable = FALSE;
18582 int band_idx;
developere40952c2023-06-15 18:46:43 +080018583 int res;
developere740c2a2023-05-23 18:34:32 +080018584
developer72fb0bb2023-01-11 09:46:29 +080018585
developera3511852023-06-14 14:12:59 +080018586 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18587 printf("Entering %s index = %d\n", __func__, (int)index);
18588 for (i = 0; i < map->num_vaps; i++)
18589 {
18590 multiple_set = TRUE;
18591 vap_info = &map->vap_array[i];
developer72fb0bb2023-01-11 09:46:29 +080018592
developera3511852023-06-14 14:12:59 +080018593 // Check vap status file to enable multiple ap if the system boot.
18594 checkVapStatus(vap_info->vap_index, &enable);
18595 if (vap_info->u.bss_info.enabled == FALSE && enable == FALSE)
18596 continue;
developer72fb0bb2023-01-11 09:46:29 +080018597
developer75bd10c2023-06-27 11:34:08 +080018598 wifi_debug(DEBUG_ERROR, "\nCreate VAP for ssid_index=%d (vap_num=%d)\n", vap_info->vap_index, i);
developer72fb0bb2023-01-11 09:46:29 +080018599
developera3511852023-06-14 14:12:59 +080018600 band_idx = radio_index_to_band(index);
developere40952c2023-06-15 18:46:43 +080018601 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18602 if (os_snprintf_error(sizeof(config_file), res)) {
18603 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18604 return RETURN_ERR;
18605 }
developer9ce44382023-06-28 11:09:37 +080018606 if(band_idx >= 0 && band_idx < sizeof(wifi_band_str)/sizeof(wifi_band_str[0])){
18607 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[band_idx], config_file);
18608 } else{
18609 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18610 return RETURN_ERR;
18611 }
developere40952c2023-06-15 18:46:43 +080018612 if (os_snprintf_error(sizeof(cmd), res)) {
18613 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18614 return RETURN_ERR;
18615 }
developera3511852023-06-14 14:12:59 +080018616 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080018617
developera3511852023-06-14 14:12:59 +080018618 struct params params[3];
18619 params[0].name = "interface";
18620 params[0].value = vap_info->vap_name;
developere40952c2023-06-15 18:46:43 +080018621 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", vap_info->vap_index);
18622 if (os_snprintf_error(sizeof(psk_file), res)) {
18623 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18624 return RETURN_ERR;
18625 }
developera3511852023-06-14 14:12:59 +080018626 params[1].name = "wpa_psk_file";
18627 params[1].value = psk_file;
18628 params[2].name = "ssid";
18629 params[2].value = vap_info->u.bss_info.ssid;
developer72fb0bb2023-01-11 09:46:29 +080018630
developer37646972023-06-29 10:58:43 +080018631 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18632 if (os_snprintf_error(sizeof(config_file), res)) {
18633 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18634 return RETURN_ERR;
18635 }
developera3511852023-06-14 14:12:59 +080018636 wifi_hostapdWrite(config_file, params, 3);
developer72fb0bb2023-01-11 09:46:29 +080018637
developere40952c2023-06-15 18:46:43 +080018638 res = snprintf(cmd, sizeof(cmd), "touch %s", psk_file);
18639 if (os_snprintf_error(sizeof(cmd), res)) {
18640 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18641 return RETURN_ERR;
18642 }
developera3511852023-06-14 14:12:59 +080018643 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080018644
developera3511852023-06-14 14:12:59 +080018645 ret = wifi_setSSIDName(vap_info->vap_index, vap_info->u.bss_info.ssid);
18646 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018647 wifi_debug(DEBUG_ERROR,"wifi_setSSIDName return error\n");
developera3511852023-06-14 14:12:59 +080018648 return RETURN_ERR;
18649 }
developer72fb0bb2023-01-11 09:46:29 +080018650
developera3511852023-06-14 14:12:59 +080018651 ret = wifi_setApSsidAdvertisementEnable(vap_info->vap_index, vap_info->u.bss_info.showSsid);
18652 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018653 wifi_debug(DEBUG_ERROR, "wifi_setApSsidAdvertisementEnable return error\n");
developera3511852023-06-14 14:12:59 +080018654 return RETURN_ERR;
18655 }
developer72fb0bb2023-01-11 09:46:29 +080018656
developera3511852023-06-14 14:12:59 +080018657 ret = wifi_setApIsolationEnable(vap_info->vap_index, vap_info->u.bss_info.isolation);
18658 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018659 wifi_debug(DEBUG_ERROR, "wifi_setApIsolationEnable return error\n");
developera3511852023-06-14 14:12:59 +080018660 return RETURN_ERR;
18661 }
developer72fb0bb2023-01-11 09:46:29 +080018662
developera3511852023-06-14 14:12:59 +080018663 ret = wifi_setApMaxAssociatedDevices(vap_info->vap_index, vap_info->u.bss_info.bssMaxSta);
18664 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018665 wifi_debug(DEBUG_ERROR, "wifi_setApMaxAssociatedDevices return error\n");
developera3511852023-06-14 14:12:59 +080018666 return RETURN_ERR;
18667 }
developer72fb0bb2023-01-11 09:46:29 +080018668
developera3511852023-06-14 14:12:59 +080018669 ret = wifi_setBSSTransitionActivation(vap_info->vap_index, vap_info->u.bss_info.bssTransitionActivated);
18670 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018671 wifi_debug(DEBUG_ERROR, "wifi_setBSSTransitionActivation return error\n");
developera3511852023-06-14 14:12:59 +080018672 return RETURN_ERR;
18673 }
developer72fb0bb2023-01-11 09:46:29 +080018674
developera3511852023-06-14 14:12:59 +080018675 ret = wifi_setNeighborReportActivation(vap_info->vap_index, vap_info->u.bss_info.nbrReportActivated);
18676 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018677 wifi_debug(DEBUG_ERROR, "wifi_setNeighborReportActivation return error\n");
developera3511852023-06-14 14:12:59 +080018678 return RETURN_ERR;
18679 }
developer72fb0bb2023-01-11 09:46:29 +080018680
developera3511852023-06-14 14:12:59 +080018681 if (vap_info->u.bss_info.mac_filter_enable == false){
18682 acl_mode = 0;
18683 }else {
18684 if (vap_info->u.bss_info.mac_filter_mode == wifi_mac_filter_mode_black_list){
18685 acl_mode = 2;
developere40952c2023-06-15 18:46:43 +080018686 res = snprintf(cmd, sizeof(cmd), "touch %s%d", DENY_PREFIX, vap_info->vap_index);
18687 if (os_snprintf_error(sizeof(cmd), res)) {
18688 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18689 return RETURN_ERR;
18690 }
developera3511852023-06-14 14:12:59 +080018691 _syscmd(cmd, buf, sizeof(buf));
18692 }else{
18693 acl_mode = 1;
18694 }
18695 }
developer72fb0bb2023-01-11 09:46:29 +080018696
developera3511852023-06-14 14:12:59 +080018697 ret = wifi_setApWmmEnable(vap_info->vap_index, vap_info->u.bss_info.wmm_enabled);
18698 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018699 wifi_debug(DEBUG_ERROR, "wifi_setApWmmEnable return error\n");
developera3511852023-06-14 14:12:59 +080018700 return RETURN_ERR;
18701 }
developer72fb0bb2023-01-11 09:46:29 +080018702
developera3511852023-06-14 14:12:59 +080018703 ret = wifi_setApWmmUapsdEnable(vap_info->vap_index, vap_info->u.bss_info.UAPSDEnabled);
18704 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018705 wifi_debug(DEBUG_ERROR, "wifi_setApWmmUapsdEnable return error\n");
developera3511852023-06-14 14:12:59 +080018706 return RETURN_ERR;
18707 }
developer72fb0bb2023-01-11 09:46:29 +080018708
developera3511852023-06-14 14:12:59 +080018709 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080018710 beaconRate_enum_to_string(vap_info->u.bss_info.beaconRate, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080018711 ret = wifi_setApBeaconRate(vap_info->radio_index, buf);
18712 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018713 wifi_debug(DEBUG_ERROR, "wifi_setApBeaconRate return error\n");
developera3511852023-06-14 14:12:59 +080018714 return RETURN_ERR;
18715 }
developer72fb0bb2023-01-11 09:46:29 +080018716
developera3511852023-06-14 14:12:59 +080018717 ret = wifi_setRadioIGMPSnoopingEnable(vap_info->radio_index, vap_info->u.bss_info.mcast2ucast);
18718 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018719 wifi_debug(DEBUG_ERROR, "wifi_setRadioIGMPSnoopingEnable\n");
developera3511852023-06-14 14:12:59 +080018720 return RETURN_ERR;
18721 }
developer72fb0bb2023-01-11 09:46:29 +080018722
developera3511852023-06-14 14:12:59 +080018723 ret = wifi_setApSecurity(vap_info->vap_index, &vap_info->u.bss_info.security);
18724 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018725 wifi_debug(DEBUG_ERROR, "wifi_setApSecurity return error\n");
developera3511852023-06-14 14:12:59 +080018726 return RETURN_ERR;
18727 }
developer333c1eb2023-05-31 14:59:39 +080018728
developer56fbedb2023-05-30 16:47:05 +080018729 hostapd_raw_restart_bss(vap_info->vap_index);
developer72fb0bb2023-01-11 09:46:29 +080018730
developera3511852023-06-14 14:12:59 +080018731 multiple_set = FALSE;
developer23e71282023-01-18 10:25:19 +080018732
developera3511852023-06-14 14:12:59 +080018733 // If config use hostapd_cli to set, we calling these type of functions after enable the ap.
18734 ret = wifi_setApMacAddressControlMode(vap_info->vap_index, acl_mode);
18735 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018736 wifi_debug(DEBUG_ERROR, "wifi_setApMacAddressControlMode return error\n");
developera3511852023-06-14 14:12:59 +080018737 return RETURN_ERR;
18738 }
developer72fb0bb2023-01-11 09:46:29 +080018739
developera3511852023-06-14 14:12:59 +080018740 // TODO mgmtPowerControl, interworking, wps
18741 }
18742 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18743 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018744}
18745
18746int parse_channel_list_int_arr(char *pchannels, wifi_channels_list_t* chlistptr)
18747{
developera3511852023-06-14 14:12:59 +080018748 char *token, *next;
18749 const char s[2] = ",";
18750 int count =0;
developer72fb0bb2023-01-11 09:46:29 +080018751
developera3511852023-06-14 14:12:59 +080018752 /* get the first token */
18753 token = strtok_r(pchannels, s, &next);
developer72fb0bb2023-01-11 09:46:29 +080018754
developera3511852023-06-14 14:12:59 +080018755 /* walk through other tokens */
18756 while( token != NULL && count < MAX_CHANNELS) {
18757 chlistptr->channels_list[count++] = atoi(token);
18758 token = strtok_r(NULL, s, &next);
18759 }
developer72fb0bb2023-01-11 09:46:29 +080018760
developera3511852023-06-14 14:12:59 +080018761 return count;
developer72fb0bb2023-01-11 09:46:29 +080018762}
18763
18764static int getRadioCapabilities(int radioIndex, wifi_radio_capabilities_t *rcap)
18765{
developera3511852023-06-14 14:12:59 +080018766 INT status;
18767 wifi_channels_list_t *chlistp;
18768 CHAR output_string[64];
18769 CHAR pchannels[128];
18770 CHAR interface_name[16] = {0};
18771 wifi_band band;
developere40952c2023-06-15 18:46:43 +080018772 int res;
developer72fb0bb2023-01-11 09:46:29 +080018773
developera3511852023-06-14 14:12:59 +080018774 if(rcap == NULL)
18775 {
18776 return RETURN_ERR;
18777 }
developer72fb0bb2023-01-11 09:46:29 +080018778
developera3511852023-06-14 14:12:59 +080018779 rcap->numSupportedFreqBand = 1;
18780 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080018781
developera3511852023-06-14 14:12:59 +080018782 if (band == band_2_4)
18783 rcap->band[0] = WIFI_FREQUENCY_2_4_BAND;
18784 else if (band == band_5)
18785 rcap->band[0] = WIFI_FREQUENCY_5_BAND;
18786 else if (band == band_6)
18787 rcap->band[0] = WIFI_FREQUENCY_6_BAND;
developer72fb0bb2023-01-11 09:46:29 +080018788
developera3511852023-06-14 14:12:59 +080018789 chlistp = &(rcap->channel_list[0]);
18790 memset(pchannels, 0, sizeof(pchannels));
developer72fb0bb2023-01-11 09:46:29 +080018791
developera3511852023-06-14 14:12:59 +080018792 /* possible number of radio channels */
18793 status = wifi_getRadioPossibleChannels(radioIndex, pchannels);
18794 {
18795 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, pchannels);
18796 }
18797 /* Number of channels and list*/
18798 chlistp->num_channels = parse_channel_list_int_arr(pchannels, chlistp);
developer72fb0bb2023-01-11 09:46:29 +080018799
developera3511852023-06-14 14:12:59 +080018800 /* autoChannelSupported */
18801 /* always ON with wifi_getRadioAutoChannelSupported */
18802 rcap->autoChannelSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018803
developera3511852023-06-14 14:12:59 +080018804 /* DCSSupported */
18805 /* always ON with wifi_getRadioDCSSupported */
18806 rcap->DCSSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018807
developera3511852023-06-14 14:12:59 +080018808 /* zeroDFSSupported - TBD */
18809 rcap->zeroDFSSupported = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018810
developera3511852023-06-14 14:12:59 +080018811 /* Supported Country List*/
18812 memset(output_string, 0, sizeof(output_string));
18813 status = wifi_getRadioCountryCode(radioIndex, output_string);
18814 if( status != 0 ) {
18815 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, output_string);
18816 return RETURN_ERR;
18817 } else {
18818 printf("[wifi_hal dbg] : func[%s] line[%d], output [%s]\n", __FUNCTION__, __LINE__, output_string);
18819 }
18820 if(!strcmp(output_string,"US")){
18821 rcap->countrySupported[0] = wifi_countrycode_US;
18822 rcap->countrySupported[1] = wifi_countrycode_CA;
18823 } else if (!strcmp(output_string,"CA")) {
18824 rcap->countrySupported[0] = wifi_countrycode_CA;
18825 rcap->countrySupported[1] = wifi_countrycode_US;
18826 } else {
18827 printf("[wifi_hal dbg] : func[%s] line[%d] radio_index[%d] Invalid Country [%s]\n", __FUNCTION__, __LINE__, radioIndex, output_string);
18828 }
developer72fb0bb2023-01-11 09:46:29 +080018829
developera3511852023-06-14 14:12:59 +080018830 rcap->numcountrySupported = 2;
developer72fb0bb2023-01-11 09:46:29 +080018831
developera3511852023-06-14 14:12:59 +080018832 /* csi */
18833 rcap->csi.maxDevices = 8;
18834 rcap->csi.soudingFrameSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018835
developer86035662023-06-28 19:21:12 +080018836 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
18837 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
18838 }
developere40952c2023-06-15 18:46:43 +080018839 res = snprintf(rcap->ifaceName, sizeof(interface_name), "%s",interface_name);
18840 if (os_snprintf_error(sizeof(interface_name), res)) {
18841 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18842 return RETURN_ERR;
18843 }
developer72fb0bb2023-01-11 09:46:29 +080018844
developera3511852023-06-14 14:12:59 +080018845 /* channelWidth - all supported bandwidths */
18846 int i=0;
18847 rcap->channelWidth[i] = 0;
18848 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18849 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18850 WIFI_CHANNELBANDWIDTH_40MHZ);
developer72fb0bb2023-01-11 09:46:29 +080018851
developera3511852023-06-14 14:12:59 +080018852 }
18853 else if (rcap->band[i] & (WIFI_FREQUENCY_5_BAND ) || rcap->band[i] & (WIFI_FREQUENCY_6_BAND)) {
18854 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18855 WIFI_CHANNELBANDWIDTH_40MHZ |
18856 WIFI_CHANNELBANDWIDTH_80MHZ | WIFI_CHANNELBANDWIDTH_160MHZ);
18857 }
developer72fb0bb2023-01-11 09:46:29 +080018858
18859
developera3511852023-06-14 14:12:59 +080018860 /* mode - all supported variants */
18861 // rcap->mode[i] = WIFI_80211_VARIANT_H;
18862 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) {
18863 rcap->mode[i] = ( WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX );
18864 }
18865 else if (rcap->band[i] & WIFI_FREQUENCY_5_BAND ) {
18866 rcap->mode[i] = ( WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX );
18867 }
18868 else if (rcap->band[i] & WIFI_FREQUENCY_6_BAND) {
18869 rcap->mode[i] = ( WIFI_80211_VARIANT_AX );
18870 }
18871 rcap->maxBitRate[i] = ( rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) ? 300 :
18872 ((rcap->band[i] & WIFI_FREQUENCY_5_BAND) ? 1734 : 0);
developer72fb0bb2023-01-11 09:46:29 +080018873
developera3511852023-06-14 14:12:59 +080018874 /* supportedBitRate - all supported bitrates */
18875 rcap->supportedBitRate[i] = 0;
18876 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18877 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18878 WIFI_BITRATE_11MBPS | WIFI_BITRATE_12MBPS);
18879 }
18880 else if ((rcap->band[i] & (WIFI_FREQUENCY_5_BAND )) || (rcap->band[i] & (WIFI_FREQUENCY_6_BAND))) {
18881 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18882 WIFI_BITRATE_12MBPS | WIFI_BITRATE_18MBPS | WIFI_BITRATE_24MBPS |
18883 WIFI_BITRATE_36MBPS | WIFI_BITRATE_48MBPS | WIFI_BITRATE_54MBPS);
18884 }
developer72fb0bb2023-01-11 09:46:29 +080018885
18886
developera3511852023-06-14 14:12:59 +080018887 rcap->transmitPowerSupported_list[i].numberOfElements = 5;
18888 rcap->transmitPowerSupported_list[i].transmitPowerSupported[0]=12;
18889 rcap->transmitPowerSupported_list[i].transmitPowerSupported[1]=25;
18890 rcap->transmitPowerSupported_list[i].transmitPowerSupported[2]=50;
18891 rcap->transmitPowerSupported_list[i].transmitPowerSupported[3]=75;
18892 rcap->transmitPowerSupported_list[i].transmitPowerSupported[4]=100;
18893 rcap->cipherSupported = 0;
18894 rcap->cipherSupported |= WIFI_CIPHER_CAPA_ENC_TKIP | WIFI_CIPHER_CAPA_ENC_CCMP;
18895 rcap->maxNumberVAPs = MAX_NUM_VAP_PER_RADIO;
developer72fb0bb2023-01-11 09:46:29 +080018896
developera3511852023-06-14 14:12:59 +080018897 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018898}
18899
18900INT wifi_getHalCapability(wifi_hal_capability_t *cap)
18901{
developera3511852023-06-14 14:12:59 +080018902 INT status = 0, radioIndex = 0;
18903 char output[MAX_BUF_SIZE] = {0};
18904 int iter = 0;
18905 unsigned int j = 0;
developer9ce44382023-06-28 11:09:37 +080018906 int max_num_radios = 0;
developera3511852023-06-14 14:12:59 +080018907 wifi_interface_name_idex_map_t *iface_info = NULL;
developer72fb0bb2023-01-11 09:46:29 +080018908
developera3511852023-06-14 14:12:59 +080018909 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018910
developera3511852023-06-14 14:12:59 +080018911 memset(cap, 0, sizeof(wifi_hal_capability_t));
developer72fb0bb2023-01-11 09:46:29 +080018912
developera3511852023-06-14 14:12:59 +080018913 /* version */
18914 cap->version.major = WIFI_HAL_MAJOR_VERSION;
18915 cap->version.minor = WIFI_HAL_MINOR_VERSION;
developer72fb0bb2023-01-11 09:46:29 +080018916
developera3511852023-06-14 14:12:59 +080018917 /* number of radios platform property */
18918 wifi_getMaxRadioNumber(&max_num_radios);
18919 cap->wifi_prop.numRadios = max_num_radios;
developer72fb0bb2023-01-11 09:46:29 +080018920
developera3511852023-06-14 14:12:59 +080018921 for(radioIndex=0; radioIndex < cap->wifi_prop.numRadios; radioIndex++)
18922 {
18923 status = getRadioCapabilities(radioIndex, &(cap->wifi_prop.radiocap[radioIndex]));
18924 if (status != 0) {
18925 printf("%s: getRadioCapabilities idx = %d\n", __FUNCTION__, radioIndex);
18926 return RETURN_ERR;
18927 }
developer72fb0bb2023-01-11 09:46:29 +080018928
developera3511852023-06-14 14:12:59 +080018929 for (j = 0; j < cap->wifi_prop.radiocap[radioIndex].maxNumberVAPs; j++)
18930 {
18931 if (iter >= MAX_NUM_RADIOS * MAX_NUM_VAP_PER_RADIO)
18932 {
18933 printf("%s: to many vaps for index map (%d)\n", __func__, iter);
18934 return RETURN_ERR;
18935 }
18936 iface_info = &cap->wifi_prop.interface_map[iter];
18937 iface_info->phy_index = radioIndex; // XXX: parse phyX index instead
18938 iface_info->rdk_radio_index = radioIndex;
18939 memset(output, 0, sizeof(output));
18940 if (wifi_getRadioIfName(radioIndex, output) == RETURN_OK)
18941 {
18942 strncpy(iface_info->interface_name, output, sizeof(iface_info->interface_name) - 1);
18943 }
18944 // TODO: bridge name
18945 // TODO: vlan id
18946 // TODO: primary
18947 iface_info->index = array_index_to_vap_index(radioIndex, j);
18948 memset(output, 0, sizeof(output));
18949 if (wifi_getApName(iface_info->index, output) == RETURN_OK)
18950 {
18951 strncpy(iface_info->vap_name, output, sizeof(iface_info->vap_name) - 1);
18952 }
18953 iter++;
18954 }
18955 }
developer72fb0bb2023-01-11 09:46:29 +080018956
developera3511852023-06-14 14:12:59 +080018957 cap->BandSteeringSupported = FALSE;
18958 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18959 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018960}
18961
18962INT wifi_setOpportunisticKeyCaching(int ap_index, BOOL okc_enable)
18963{
developera3511852023-06-14 14:12:59 +080018964 struct params h_config={0};
18965 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018966 int res;
developer72fb0bb2023-01-11 09:46:29 +080018967
developera3511852023-06-14 14:12:59 +080018968 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018969
developera3511852023-06-14 14:12:59 +080018970 h_config.name = "okc";
18971 h_config.value = okc_enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018972
developere40952c2023-06-15 18:46:43 +080018973 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18974 if (os_snprintf_error(sizeof(config_file), res)) {
18975 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18976 return RETURN_ERR;
18977 }
developera3511852023-06-14 14:12:59 +080018978 wifi_hostapdWrite(config_file, &h_config, 1);
18979 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018980
developera3511852023-06-14 14:12:59 +080018981 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18982 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018983}
18984
18985INT wifi_setSAEMFP(int ap_index, BOOL enable)
18986{
developera3511852023-06-14 14:12:59 +080018987 struct params h_config={0};
18988 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018989 int res;
developer72fb0bb2023-01-11 09:46:29 +080018990
developera3511852023-06-14 14:12:59 +080018991 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018992
developera3511852023-06-14 14:12:59 +080018993 h_config.name = "sae_require_mfp";
18994 h_config.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018995
developere40952c2023-06-15 18:46:43 +080018996 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18997 if (os_snprintf_error(sizeof(config_file), res)) {
18998 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18999 return RETURN_ERR;
19000 }
developera3511852023-06-14 14:12:59 +080019001 wifi_hostapdWrite(config_file, &h_config, 1);
19002 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019003
developera3511852023-06-14 14:12:59 +080019004 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19005 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019006}
19007
19008INT wifi_setSAEpwe(int ap_index, int sae_pwe)
19009{
developera3511852023-06-14 14:12:59 +080019010 struct params h_config={0};
19011 char config_file[64] = {0};
19012 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080019013 int res;
developer72fb0bb2023-01-11 09:46:29 +080019014
developera3511852023-06-14 14:12:59 +080019015 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019016
developera3511852023-06-14 14:12:59 +080019017 h_config.name = "sae_pwe";
developere40952c2023-06-15 18:46:43 +080019018 res = snprintf(buf, sizeof(buf), "%d", sae_pwe);
19019 if (os_snprintf_error(sizeof(buf), res)) {
19020 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19021 return RETURN_ERR;
19022 }
19023
developera3511852023-06-14 14:12:59 +080019024 h_config.value = buf;
developer72fb0bb2023-01-11 09:46:29 +080019025
developere40952c2023-06-15 18:46:43 +080019026 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19027 if (os_snprintf_error(sizeof(config_file), res)) {
19028 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19029 return RETURN_ERR;
19030 }
developera3511852023-06-14 14:12:59 +080019031 wifi_hostapdWrite(config_file, &h_config, 1);
19032 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019033
developera3511852023-06-14 14:12:59 +080019034 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19035 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019036}
19037
19038INT wifi_setDisable_EAPOL_retries(int ap_index, BOOL disable_EAPOL_retries)
19039{
developera3511852023-06-14 14:12:59 +080019040 // wpa3 use SAE instead of PSK, so we need to disable this feature when using wpa3.
19041 struct params h_config={0};
19042 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080019043 int res;
developer72fb0bb2023-01-11 09:46:29 +080019044
developera3511852023-06-14 14:12:59 +080019045 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019046
developera3511852023-06-14 14:12:59 +080019047 h_config.name = "wpa_disable_eapol_key_retries";
19048 h_config.value = disable_EAPOL_retries?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080019049
developere40952c2023-06-15 18:46:43 +080019050 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19051 if (os_snprintf_error(sizeof(config_file), res)) {
19052 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19053 return RETURN_ERR;
19054 }
developera3511852023-06-14 14:12:59 +080019055 wifi_hostapdWrite(config_file, &h_config, 1);
19056 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019057
developera3511852023-06-14 14:12:59 +080019058 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19059 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019060}
19061
19062INT wifi_setApSecurity(INT ap_index, wifi_vap_security_t *security)
19063{
developera3511852023-06-14 14:12:59 +080019064 char buf[128] = {0};
19065 char config_file[128] = {0};
19066 char cmd[MAX_CMD_SIZE] = {0};
19067 char password[65] = {0};
19068 char mfp[32] = {0};
19069 char wpa_mode[32] = {0};
19070 BOOL okc_enable = FALSE;
19071 BOOL sae_MFP = FALSE;
19072 BOOL disable_EAPOL_retries = TRUE;
19073 int sae_pwe = 0;
19074 struct params params = {0};
19075 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080019076 int res;
developer72fb0bb2023-01-11 09:46:29 +080019077
developera3511852023-06-14 14:12:59 +080019078 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019079
developera3511852023-06-14 14:12:59 +080019080 multiple_set = TRUE;
developer37646972023-06-29 10:58:43 +080019081 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19082 if (os_snprintf_error(sizeof(config_file), res)) {
19083 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19084 return RETURN_ERR;
19085 }
developera3511852023-06-14 14:12:59 +080019086 if (security->mode == wifi_security_mode_none) {
developer9ce44382023-06-28 11:09:37 +080019087 strncpy(wpa_mode, "None",sizeof(wpa_mode) - 1);
19088 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19089 } else if (security->mode == wifi_security_mode_wpa_personal) {
19090 strncpy(wpa_mode, "WPA-Personal",sizeof(wpa_mode) - 1);
19091 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19092 } else if (security->mode == wifi_security_mode_wpa2_personal){
19093 strncpy(wpa_mode, "WPA2-Personal",sizeof(wpa_mode) - 1);
19094 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19095 } else if (security->mode == wifi_security_mode_wpa_wpa2_personal){
19096 strncpy(wpa_mode, "WPA-WPA2-Personal",sizeof(wpa_mode) - 1);
19097 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19098 } else if (security->mode == wifi_security_mode_wpa_enterprise){
19099 strncpy(wpa_mode, "WPA-Enterprise",sizeof(wpa_mode) - 1);
19100 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19101 } else if (security->mode == wifi_security_mode_wpa2_enterprise){
19102 strncpy(wpa_mode, "WPA2-Enterprise",sizeof(wpa_mode) - 1);
19103 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19104 } else if (security->mode == wifi_security_mode_wpa_wpa2_enterprise){
19105 strncpy(wpa_mode, "WPA-WAP2-Enterprise",sizeof(wpa_mode) - 1);
19106 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19107 } else if (security->mode == wifi_security_mode_wpa3_personal) {
19108 strncpy(wpa_mode, "WPA3-Personal",sizeof(wpa_mode) - 1);
19109 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019110 okc_enable = TRUE;
19111 sae_MFP = TRUE;
19112 sae_pwe = 2;
19113 disable_EAPOL_retries = FALSE;
19114 } else if (security->mode == wifi_security_mode_wpa3_transition) {
developer9ce44382023-06-28 11:09:37 +080019115 strncpy(wpa_mode, "WPA3-Personal-Transition",sizeof(wpa_mode) - 1);
19116 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019117 okc_enable = TRUE;
19118 sae_MFP = TRUE;
19119 sae_pwe = 2;
19120 disable_EAPOL_retries = FALSE;
19121 } else if (security->mode == wifi_security_mode_wpa3_enterprise) {
developer9ce44382023-06-28 11:09:37 +080019122 strncpy(wpa_mode, "WPA3-Enterprise",sizeof(wpa_mode) - 1);
19123 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019124 sae_MFP = TRUE;
19125 sae_pwe = 2;
19126 disable_EAPOL_retries = FALSE;
19127 } else if (security->mode == wifi_security_mode_enhanced_open) {
developer9ce44382023-06-28 11:09:37 +080019128 strncpy(wpa_mode, "OWE",sizeof(wpa_mode) - 1);
19129 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019130 sae_MFP = TRUE;
19131 sae_pwe = 2;
19132 disable_EAPOL_retries = FALSE;
19133 }
developer72fb0bb2023-01-11 09:46:29 +080019134
developera3511852023-06-14 14:12:59 +080019135 band = wifi_index_to_band(ap_index);
19136 if (band == band_6 && strstr(wpa_mode, "WPA3") == NULL) {
developerc14d83a2023-06-29 20:09:42 +080019137 wifi_debug(DEBUG_ERROR, "%s: 6G band must set with wpa3.\n", __func__);
developera3511852023-06-14 14:12:59 +080019138 return RETURN_ERR;
19139 }
developer72fb0bb2023-01-11 09:46:29 +080019140
developera3511852023-06-14 14:12:59 +080019141 wifi_setApSecurityModeEnabled(ap_index, wpa_mode);
19142 wifi_setOpportunisticKeyCaching(ap_index, okc_enable);
19143 wifi_setSAEMFP(ap_index, sae_MFP);
19144 wifi_setSAEpwe(ap_index, sae_pwe);
19145 wifi_setDisable_EAPOL_retries(ap_index, disable_EAPOL_retries);
developer72fb0bb2023-01-11 09:46:29 +080019146
developera3511852023-06-14 14:12:59 +080019147 if (security->mode != wifi_security_mode_none && security->mode != wifi_security_mode_enhanced_open) {
19148 if (security->u.key.type == wifi_security_key_type_psk || security->u.key.type == wifi_security_key_type_pass || security->u.key.type == wifi_security_key_type_psk_sae) {
19149 int key_len = strlen(security->u.key.key);
19150 // wpa_psk and wpa_passphrase cann;t use at the same time, the command replace one with the other.
19151 if (key_len == 64) { // set wpa_psk
19152 strncpy(password, security->u.key.key, 64); // 64 characters
19153 password[64] = '\0';
19154 wifi_setApSecurityPreSharedKey(ap_index, password);
developere40952c2023-06-15 18:46:43 +080019155 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^wpa_passphrase=/!p' %s", config_file);
19156 if (os_snprintf_error(sizeof(cmd), res)) {
19157 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19158 return RETURN_ERR;
19159 }
developera3511852023-06-14 14:12:59 +080019160 } else if (key_len >= 8 && key_len < 64) { // set wpa_passphrase
19161 strncpy(password, security->u.key.key, 63);
19162 password[63] = '\0';
19163 wifi_setApSecurityKeyPassphrase(ap_index, password);
developere40952c2023-06-15 18:46:43 +080019164 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^wpa_psk=/!p' %s", config_file);
developer9ce44382023-06-28 11:09:37 +080019165 if (os_snprintf_error(sizeof(cmd), res)) {
19166 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19167 return RETURN_ERR;
19168 }
developera3511852023-06-14 14:12:59 +080019169 } else
19170 return RETURN_ERR;
19171 _syscmd(cmd, buf, sizeof(buf));
19172 }
19173 if (security->u.key.type == wifi_security_key_type_sae || security->u.key.type == wifi_security_key_type_psk_sae) {
19174 params.name = "sae_password";
19175 params.value = security->u.key.key;
19176 wifi_hostapdWrite(config_file, &params, 1);
19177 } else { // remove sae_password
developere40952c2023-06-15 18:46:43 +080019178 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^sae_password=/!p' %s", config_file);
19179 if (os_snprintf_error(sizeof(cmd), res)) {
19180 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19181 return RETURN_ERR;
19182 }
developera3511852023-06-14 14:12:59 +080019183 _syscmd(cmd, buf, sizeof(buf));
19184 }
19185 }
developer72fb0bb2023-01-11 09:46:29 +080019186
developera3511852023-06-14 14:12:59 +080019187 if (security->mode != wifi_security_mode_none) {
19188 memset(&params, 0, sizeof(params));
19189 params.name = "wpa_pairwise";
19190 if (security->encr == wifi_encryption_tkip)
19191 params.value = "TKIP";
19192 else if (security->encr == wifi_encryption_aes)
19193 params.value = "CCMP";
19194 else if (security->encr == wifi_encryption_aes_tkip)
19195 params.value = "TKIP CCMP";
19196 wifi_hostapdWrite(config_file, &params, 1);
19197 }
developer72fb0bb2023-01-11 09:46:29 +080019198
developer9ce44382023-06-28 11:09:37 +080019199 if (security->mfp == wifi_mfp_cfg_disabled){
19200 strncpy(mfp,"Disabled",sizeof(mfp)-1);
19201 mfp[sizeof(mfp)-1] = '\0';
19202 } else if (security->mfp == wifi_mfp_cfg_optional){
19203 strncpy(mfp,"Optional",sizeof(mfp)-1);
19204 mfp[sizeof(mfp)-1] = '\0';
19205 } else if (security->mfp == wifi_mfp_cfg_required){
19206 strncpy(mfp,"Required",sizeof(mfp)-1);
19207 mfp[sizeof(mfp)-1] = '\0';
19208 }
developera3511852023-06-14 14:12:59 +080019209 wifi_setApSecurityMFPConfig(ap_index, mfp);
developer72fb0bb2023-01-11 09:46:29 +080019210
developera3511852023-06-14 14:12:59 +080019211 memset(&params, 0, sizeof(params));
19212 params.name = "transition_disable";
19213 if (security->wpa3_transition_disable == TRUE)
19214 params.value = "0x01";
19215 else
19216 params.value = "0x00";
19217 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019218
developera3511852023-06-14 14:12:59 +080019219 memset(&params, 0, sizeof(params));
19220 params.name = "wpa_group_rekey";
developere40952c2023-06-15 18:46:43 +080019221 res = snprintf(buf, sizeof(buf), "%d", security->rekey_interval);
19222 if (os_snprintf_error(sizeof(buf), res)) {
19223 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19224 return RETURN_ERR;
19225 }
developera3511852023-06-14 14:12:59 +080019226 params.value = buf;
19227 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019228
developera3511852023-06-14 14:12:59 +080019229 memset(&params, 0, sizeof(params));
19230 params.name = "wpa_strict_rekey";
19231 params.value = security->strict_rekey?"1":"0";
19232 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019233
developera3511852023-06-14 14:12:59 +080019234 memset(&params, 0, sizeof(params));
19235 params.name = "wpa_pairwise_update_count";
19236 if (security->eapol_key_retries == 0)
19237 security->eapol_key_retries = 4; // 0 is invalid, set to default value.
developere40952c2023-06-15 18:46:43 +080019238 res = snprintf(buf, sizeof(buf), "%u", security->eapol_key_retries);
19239 if (os_snprintf_error(sizeof(buf), res)) {
19240 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19241 return RETURN_ERR;
19242 }
developera3511852023-06-14 14:12:59 +080019243 params.value = buf;
19244 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019245
developera3511852023-06-14 14:12:59 +080019246 memset(&params, 0, sizeof(params));
19247 params.name = "disable_pmksa_caching";
19248 params.value = security->disable_pmksa_caching?"1":"0";
19249 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019250
developera3511852023-06-14 14:12:59 +080019251 if (multiple_set == FALSE) {
19252 wifi_setApEnable(ap_index, FALSE);
19253 wifi_setApEnable(ap_index, TRUE);
19254 }
developer72fb0bb2023-01-11 09:46:29 +080019255
developera3511852023-06-14 14:12:59 +080019256 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019257
developera3511852023-06-14 14:12:59 +080019258 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019259}
19260
19261INT wifi_getApSecurity(INT ap_index, wifi_vap_security_t *security)
19262{
developera3511852023-06-14 14:12:59 +080019263 char buf[256] = {0};
19264 char config_file[128] = {0};
developer86035662023-06-28 19:21:12 +080019265 long int disable = 0;
19266 long int tmp;
developera3511852023-06-14 14:12:59 +080019267 bool set_sae = FALSE;
developere75ba632023-06-29 16:03:33 +080019268 int res;
developer72fb0bb2023-01-11 09:46:29 +080019269
developera3511852023-06-14 14:12:59 +080019270 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere75ba632023-06-29 16:03:33 +080019271 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19272 if (os_snprintf_error(sizeof(config_file), res)) {
19273 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19274 return RETURN_ERR;
19275 }
developera3511852023-06-14 14:12:59 +080019276 wifi_getApSecurityModeEnabled(ap_index, buf); // Get wpa config
19277 security->mode = wifi_security_mode_none;
19278 if (strlen(buf) != 0) {
19279 if (!strcmp(buf, "WPA-Personal"))
19280 security->mode = wifi_security_mode_wpa_personal;
19281 else if (!strcmp(buf, "WPA2-Personal"))
19282 security->mode = wifi_security_mode_wpa2_personal;
19283 else if (!strcmp(buf, "WPA-WPA2-Personal"))
19284 security->mode = wifi_security_mode_wpa_wpa2_personal;
19285 else if (!strcmp(buf, "WPA-Enterprise"))
19286 security->mode = wifi_security_mode_wpa_enterprise;
19287 else if (!strcmp(buf, "WPA2-Enterprise"))
19288 security->mode = wifi_security_mode_wpa2_enterprise;
19289 else if (!strcmp(buf, "WPA-WPA2-Enterprise"))
19290 security->mode = wifi_security_mode_wpa_wpa2_enterprise;
19291 else if (!strcmp(buf, "WPA3-Personal"))
19292 security->mode = wifi_security_mode_wpa3_personal;
19293 else if (!strcmp(buf, "WPA3-Personal-Transition"))
19294 security->mode = wifi_security_mode_wpa3_transition;
19295 else if (!strcmp(buf, "WPA3-Enterprise"))
19296 security->mode = wifi_security_mode_wpa3_enterprise;
19297 else if (!strcmp(buf, "OWE"))
19298 security->mode = wifi_security_mode_enhanced_open;
19299 }
developer72fb0bb2023-01-11 09:46:29 +080019300
developera3511852023-06-14 14:12:59 +080019301 wifi_hostapdRead(config_file,"wpa_pairwise",buf,sizeof(buf));
19302 if (security->mode == wifi_security_mode_none)
19303 security->encr = wifi_encryption_none;
19304 else {
19305 if (strcmp(buf, "TKIP") == 0)
19306 security->encr = wifi_encryption_tkip;
19307 else if (strcmp(buf, "CCMP") == 0)
19308 security->encr = wifi_encryption_aes;
19309 else
19310 security->encr = wifi_encryption_aes_tkip;
19311 }
developer72fb0bb2023-01-11 09:46:29 +080019312
developera3511852023-06-14 14:12:59 +080019313 if (security->mode != wifi_security_mode_none) {
19314 memset(buf, 0, sizeof(buf));
19315 // wpa3 can use one or both configs as password, so we check sae_password first.
19316 wifi_hostapdRead(config_file, "sae_password", buf, sizeof(buf));
19317 if (strlen(buf) != 0) {
19318 if (security->mode == wifi_security_mode_wpa3_personal || security->mode == wifi_security_mode_wpa3_transition)
19319 security->u.key.type = wifi_security_key_type_sae;
19320 set_sae = TRUE;
19321 strncpy(security->u.key.key, buf, sizeof(buf));
19322 }
19323 wifi_hostapdRead(config_file, "wpa_passphrase", buf, sizeof(buf));
19324 if (strlen(buf) != 0){
19325 if (set_sae == TRUE)
19326 security->u.key.type = wifi_security_key_type_psk_sae;
19327 else if (strlen(buf) == 64)
19328 security->u.key.type = wifi_security_key_type_psk;
19329 else
19330 security->u.key.type = wifi_security_key_type_pass;
19331 strncpy(security->u.key.key, buf, sizeof(security->u.key.key));
19332 }
19333 security->u.key.key[255] = '\0';
19334 }
developer72fb0bb2023-01-11 09:46:29 +080019335
developera3511852023-06-14 14:12:59 +080019336 memset(buf, 0, sizeof(buf));
19337 wifi_getApSecurityMFPConfig(ap_index, buf);
19338 if (strcmp(buf, "Disabled") == 0)
19339 security->mfp = wifi_mfp_cfg_disabled;
19340 else if (strcmp(buf, "Optional") == 0)
19341 security->mfp = wifi_mfp_cfg_optional;
19342 else if (strcmp(buf, "Required") == 0)
19343 security->mfp = wifi_mfp_cfg_required;
developer72fb0bb2023-01-11 09:46:29 +080019344
developera3511852023-06-14 14:12:59 +080019345 memset(buf, 0, sizeof(buf));
19346 security->wpa3_transition_disable = FALSE;
19347 wifi_hostapdRead(config_file, "transition_disable", buf, sizeof(buf));
developer3de255a2023-06-29 10:35:45 +080019348 if (strlen(buf) == 0)
19349 disable = 0;
19350 else {
19351 if (hal_strtol(buf, 16, &disable) < 0) {
19352 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19353 return RETURN_ERR;
19354 }
developer86035662023-06-28 19:21:12 +080019355 }
developera3511852023-06-14 14:12:59 +080019356 if (disable != 0)
19357 security->wpa3_transition_disable = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080019358
developera3511852023-06-14 14:12:59 +080019359 memset(buf, 0, sizeof(buf));
19360 wifi_hostapdRead(config_file, "wpa_group_rekey", buf, sizeof(buf));
19361 if (strlen(buf) == 0)
19362 security->rekey_interval = 86400;
developer86035662023-06-28 19:21:12 +080019363 else {
19364 if (hal_strtol(buf, 10, &tmp) < 0) {
19365 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19366 return RETURN_ERR;
19367 }
19368 security->rekey_interval = tmp;
19369 }
developer72fb0bb2023-01-11 09:46:29 +080019370
developera3511852023-06-14 14:12:59 +080019371 memset(buf, 0, sizeof(buf));
19372 wifi_hostapdRead(config_file, "wpa_strict_rekey", buf, sizeof(buf));
19373 if (strlen(buf) == 0)
19374 security->strict_rekey = 1;
developer86035662023-06-28 19:21:12 +080019375 else {
19376 if (hal_strtol(buf, 10, &tmp) < 0) {
19377 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19378 return RETURN_ERR;
19379 }
19380 security->strict_rekey = tmp;
19381 }
developer72fb0bb2023-01-11 09:46:29 +080019382
developera3511852023-06-14 14:12:59 +080019383 memset(buf, 0, sizeof(buf));
19384 wifi_hostapdRead(config_file, "wpa_pairwise_update_count", buf, sizeof(buf));
19385 if (strlen(buf) == 0)
19386 security->eapol_key_retries = 4;
developer86035662023-06-28 19:21:12 +080019387 else {
19388 if (hal_strtol(buf, 10, &tmp) < 0) {
19389 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19390 return RETURN_ERR;
19391 }
19392 security->eapol_key_retries = tmp;
19393 }
developer72fb0bb2023-01-11 09:46:29 +080019394
developera3511852023-06-14 14:12:59 +080019395 memset(buf, 0, sizeof(buf));
19396 wifi_hostapdRead(config_file, "disable_pmksa_caching", buf, sizeof(buf));
19397 if (strlen(buf) == 0)
19398 security->disable_pmksa_caching = FALSE;
developer86035662023-06-28 19:21:12 +080019399 else {
19400 if (hal_strtol(buf, 10, &(tmp)) < 0) {
19401 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19402 return RETURN_ERR;
19403 }
19404 security->disable_pmksa_caching = tmp ? TRUE : FALSE;
19405 }
developera3511852023-06-14 14:12:59 +080019406 /* TODO
19407 eapol_key_timeout, eap_identity_req_timeout, eap_identity_req_retries, eap_req_timeout, eap_req_retries
19408 */
19409 security->eapol_key_timeout = 1000; // Unit is ms. The default value in protocol.
19410 security->eap_identity_req_timeout = 0;
19411 security->eap_identity_req_retries = 0;
19412 security->eap_req_timeout = 0;
19413 security->eap_req_retries = 0;
19414 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19415 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019416}
19417
19418#endif /* WIFI_HAL_VERSION_3 */
19419
19420#ifdef WIFI_HAL_VERSION_3_PHASE2
19421INT wifi_getApAssociatedDevice(INT ap_index, mac_address_t *output_deviceMacAddressArray, UINT maxNumDevices, UINT *output_numDevices)
19422{
developera3511852023-06-14 14:12:59 +080019423 char interface_name[16] = {0};
19424 char cmd[128] = {0};
19425 char buf[128] = {0};
19426 char *mac_addr = NULL;
19427 BOOL status = FALSE;
19428 size_t len = 0;
developer72fb0bb2023-01-11 09:46:29 +080019429
developera3511852023-06-14 14:12:59 +080019430 if(ap_index > MAX_APS)
19431 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019432
developera3511852023-06-14 14:12:59 +080019433 *output_numDevices = 0;
19434 wifi_getApEnable(ap_index, &status);
19435 if (status == FALSE)
19436 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019437
developera3511852023-06-14 14:12:59 +080019438 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19439 return RETURN_ERR;
19440 sprintf(cmd, "hostapd_cli -i %s list_sta", interface_name);
19441 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080019442
developera3511852023-06-14 14:12:59 +080019443 mac_addr = strtok(buf, "\n");
19444 for (int i = 0; i < maxNumDevices && mac_addr != NULL; i++) {
19445 *output_numDevices = i + 1;
developerc14d83a2023-06-29 20:09:42 +080019446 wifi_debug(DEBUG_ERROR,, "mac_addr: %s\n", mac_addr);
developera3511852023-06-14 14:12:59 +080019447 addr_ptr = output_deviceMacAddressArray[i];
19448 mac_addr_aton(addr_ptr, mac_addr);
19449 mac_addr = strtok(NULL, "\n");
19450 }
developer72fb0bb2023-01-11 09:46:29 +080019451
developera3511852023-06-14 14:12:59 +080019452 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019453}
19454#else
19455INT wifi_getApAssociatedDevice(INT ap_index, CHAR *output_buf, INT output_buf_size)
19456{
developera3511852023-06-14 14:12:59 +080019457 char interface_name[16] = {0};
19458 char cmd[128];
19459 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +080019460 int res;
developer72fb0bb2023-01-11 09:46:29 +080019461
developera3511852023-06-14 14:12:59 +080019462 if(ap_index > MAX_APS || output_buf == NULL || output_buf_size <= 0)
19463 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019464
developera3511852023-06-14 14:12:59 +080019465 output_buf[0] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080019466
developera3511852023-06-14 14:12:59 +080019467 wifi_getApEnable(ap_index,&status);
19468 if (!status)
19469 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019470
developera3511852023-06-14 14:12:59 +080019471 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19472 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080019473 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta | tr '\\n' ',' | sed 's/.$//'", interface_name);
19474 if (os_snprintf_error(sizeof(cmd), res)) {
19475 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19476 return RETURN_ERR;
19477 }
developer5b23cd02023-07-19 20:26:03 +080019478
developera3511852023-06-14 14:12:59 +080019479 _syscmd(cmd, output_buf, output_buf_size);
developer69b61b02023-03-07 17:17:44 +080019480
developera3511852023-06-14 14:12:59 +080019481 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019482}
19483#endif
19484
19485INT wifi_getProxyArp(INT apIndex, BOOL *enable)
19486{
developera3511852023-06-14 14:12:59 +080019487 char output[16]={'\0'};
19488 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080019489 int res;
developer72fb0bb2023-01-11 09:46:29 +080019490
developera3511852023-06-14 14:12:59 +080019491 if (!enable)
19492 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019493
developer75bd10c2023-06-27 11:34:08 +080019494 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
19495 if (os_snprintf_error(sizeof(config_file), res)) {
19496 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19497 return RETURN_ERR;
19498 }
developera3511852023-06-14 14:12:59 +080019499 wifi_hostapdRead(config_file, "proxy_arp", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080019500
developera3511852023-06-14 14:12:59 +080019501 if (strlen(output) == 0)
19502 *enable = FALSE;
19503 else if (strncmp(output, "1", 1) == 0)
19504 *enable = TRUE;
19505 else
19506 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080019507
developera3511852023-06-14 14:12:59 +080019508 wifi_dbg_printf("\n[%s]: proxy_arp is : %s", __func__, output);
19509 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019510}
19511
19512INT wifi_getRadioStatsEnable(INT radioIndex, BOOL *output_enable)
19513{
developera3511852023-06-14 14:12:59 +080019514 if (NULL == output_enable || radioIndex >=MAX_NUM_RADIOS)
19515 return RETURN_ERR;
19516 *output_enable=TRUE;
19517 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019518}
19519
19520INT wifi_getTWTsessions(INT ap_index, UINT maxNumberSessions, wifi_twt_sessions_t *twtSessions, UINT *numSessionReturned)
19521{
developera3511852023-06-14 14:12:59 +080019522 char cmd[128] = {0};
19523 char buf[128] = {0};
19524 char line[128] = {0};
19525 FILE *f = NULL;
19526 int index = 0;
19527 int exp = 0;
19528 int mantissa = 0;
19529 int duration = 0;
19530 int radio_index = 0;
19531 int max_radio_num = 0;
19532 uint twt_wake_interval = 0;
19533 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +080019534 int res;
developerc14d83a2023-06-29 20:09:42 +080019535 unsigned long tmp_u, tmp_l;
developer75bd10c2023-06-27 11:34:08 +080019536
developera3511852023-06-14 14:12:59 +080019537 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019538
developera3511852023-06-14 14:12:59 +080019539 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080019540 if(max_radio_num == 0){
19541 return RETURN_ERR;
19542 }
developera3511852023-06-14 14:12:59 +080019543 radio_index = ap_index % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +080019544
developera3511852023-06-14 14:12:59 +080019545 phyId = radio_index_to_phy(radio_index);
developer75bd10c2023-06-27 11:34:08 +080019546
19547 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | wc -l", phyId);
19548 if (os_snprintf_error(sizeof(cmd), res)) {
19549 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19550 return RETURN_ERR;
19551 }
developera3511852023-06-14 14:12:59 +080019552 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080019553 if (hal_strtoul(buf, 10, &tmp_u) < 0) {
19554 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019555 }
19556 *numSessionReturned = tmp_u - 1;
developer5b23cd02023-07-19 20:26:03 +080019557
developera3511852023-06-14 14:12:59 +080019558 if (*numSessionReturned > maxNumberSessions)
19559 *numSessionReturned = maxNumberSessions;
19560 else if (*numSessionReturned < 1) {
19561 *numSessionReturned = 0;
19562 return RETURN_OK;
19563 }
developer72fb0bb2023-01-11 09:46:29 +080019564
developer75bd10c2023-06-27 11:34:08 +080019565 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | tail -n %d | tr '|' ' ' | tr -s ' '", phyId, *numSessionReturned);
19566 if (os_snprintf_error(sizeof(cmd), res)) {
19567 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19568 return RETURN_ERR;
19569 }
developera3511852023-06-14 14:12:59 +080019570 if ((f = popen(cmd, "r")) == NULL) {
19571 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
19572 return RETURN_ERR;
19573 }
developer72fb0bb2023-01-11 09:46:29 +080019574
developera3511852023-06-14 14:12:59 +080019575 // the format of each line is "[wcid] [id] [flags] [exp] [mantissa] [duration] [tsf]"
19576 while((fgets(line, sizeof(line), f)) != NULL) {
19577 char *tmp = NULL;
developer9ce44382023-06-28 11:09:37 +080019578 size_t len = strlen(line);
19579 strncpy(buf, line,len);
19580 buf[len] = '\0';
developera3511852023-06-14 14:12:59 +080019581 tmp = strtok(buf, " ");
developerd14dff12023-06-28 22:47:44 +080019582 if (tmp == NULL)
19583 break;
developer5b23cd02023-07-19 20:26:03 +080019584
developerc14d83a2023-06-29 20:09:42 +080019585 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19586 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19587 }
19588 twtSessions[index].numDevicesInSession = tmp_u;
developera3511852023-06-14 14:12:59 +080019589 tmp = strtok(NULL, " ");
developerd14dff12023-06-28 22:47:44 +080019590 if (tmp == NULL)
19591 break;
developer5b23cd02023-07-19 20:26:03 +080019592
developerc14d83a2023-06-29 20:09:42 +080019593 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19594 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19595 }
19596 twtSessions[index].twtParameters.operation.flowID = tmp_u;
developera3511852023-06-14 14:12:59 +080019597 tmp = strtok(NULL, " ");
19598 if (strstr(tmp, "t")) {
19599 twtSessions[index].twtParameters.operation.trigger_enabled = TRUE;
19600 }
19601 if (strstr(tmp, "a")) {
19602 twtSessions[index].twtParameters.operation.announced = TRUE;
19603 }
19604 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019605 if (tmp == NULL)
19606 continue;
developer5b23cd02023-07-19 20:26:03 +080019607
developerc14d83a2023-06-29 20:09:42 +080019608 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19609 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19610 }
19611 exp = tmp_l;
developer5b23cd02023-07-19 20:26:03 +080019612
developera3511852023-06-14 14:12:59 +080019613 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019614 if (tmp == NULL)
19615 continue;
developerc14d83a2023-06-29 20:09:42 +080019616 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19617 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019618 }
19619 mantissa = tmp_l;
developer5b23cd02023-07-19 20:26:03 +080019620
developera3511852023-06-14 14:12:59 +080019621 tmp = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +080019622
19623 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19624 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019625 }
19626 duration = tmp_l;
developer72fb0bb2023-01-11 09:46:29 +080019627
developera3511852023-06-14 14:12:59 +080019628 // only implicit supported
19629 twtSessions[index].twtParameters.operation.implicit = TRUE;
19630 // only individual agreement supported
19631 twtSessions[index].twtParameters.agreement = wifi_twt_agreement_type_individual;
developer72fb0bb2023-01-11 09:46:29 +080019632
developera3511852023-06-14 14:12:59 +080019633 // wakeInterval_uSec is a unsigned integer, but the maximum TWT wake interval could be 2^15 (mantissa) * 2^32 = 2^47.
19634 twt_wake_interval = mantissa * (1 << exp);
19635 if (mantissa == 0 || twt_wake_interval/mantissa != (1 << exp)) {
19636 // Overflow handling
19637 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = -1; // max unsigned int
19638 } else {
19639 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = twt_wake_interval;
19640 }
19641 twtSessions[index].twtParameters.params.individual.minWakeDuration_uSec = duration * 256;
19642 index++;
19643 }
developer72fb0bb2023-01-11 09:46:29 +080019644
developera3511852023-06-14 14:12:59 +080019645 pclose(f);
19646 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19647 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019648}
developercc5cbfb2023-06-13 18:29:52 +080019649
19650INT wifi_enableGreylistAccessControl(BOOL enable)
19651{
19652 char inf_name[IFNAMSIZ] = {0};
19653 int if_idx, ret = 0;
19654 struct nl_msg *msg = NULL;
19655 struct nlattr * msg_data = NULL;
19656 struct mtk_nl80211_param param;
19657 struct unl unl_ins;
19658 unsigned short apIndex = 0;
19659
19660 for (apIndex = 0; apIndex < MAX_APS; apIndex++) {
19661 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
19662 continue;
19663
19664 if_idx = if_nametoindex(inf_name);
19665 if (!if_idx) {
19666 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
19667 continue;
19668 }
19669
19670 /*init mtk nl80211 vendor cmd*/
19671 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
19672 param.if_type = NL80211_ATTR_IFINDEX;
19673 param.if_idx = if_idx;
19674 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
19675 if (ret) {
19676 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
19677 return RETURN_ERR;
19678 }
19679
19680 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, enable == FALSE ? 0 : 1)) {
19681 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
19682 nlmsg_free(msg);
19683 mtk_nl80211_deint(&unl_ins);
19684 continue;
19685 }
19686
19687 /*send mtk nl80211 vendor msg*/
19688 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
19689 if (ret) {
19690 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
19691 mtk_nl80211_deint(&unl_ins);
19692 continue;
19693 }
19694 /*deinit mtk nl80211 vendor msg*/
19695 mtk_nl80211_deint(&unl_ins);
19696 wifi_debug(DEBUG_NOTICE, " %s cmd success.\n", inf_name);
19697 }
19698
19699 return RETURN_OK;
19700}