blob: 9192a2ee75c9de15d315c9da544896ab3da3855f [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
developerbb9b20f2023-10-17 18:46:37 +0800125#define LOGAN_MAX_NUM_VAP_PER_RADIO (MAX_NUM_VAP_PER_RADIO > 16 ? 16 : MAX_NUM_VAP_PER_RADIO)
126#define MAX_APS MAX_NUM_RADIOS * LOGAN_MAX_NUM_VAP_PER_RADIO
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
developer8078acf2023-08-04 18:52:48 +0800829/*static int _syscmd(char *cmd, char *retBuf, int retBufSize)
developer72fb0bb2023-01-11 09:46:29 +0800830{
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}
developer8078acf2023-08-04 18:52:48 +0800866*/
developer72fb0bb2023-01-11 09:46:29 +0800867
868INT radio_index_to_phy(int radioIndex)
869{
developera3511852023-06-14 14:12:59 +0800870 /* TODO */
871 return radioIndex;
developer72fb0bb2023-01-11 09:46:29 +0800872}
873
874INT wifi_getMaxRadioNumber(INT *max_radio_num)
875{
developera3511852023-06-14 14:12:59 +0800876 char buf[4] = {0};
developere40952c2023-06-15 18:46:43 +0800877 int res;
developerc14d83a2023-06-29 20:09:42 +0800878 unsigned long tmp;
developera3511852023-06-14 14:12:59 +0800879 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800880
developer33f13ba2023-07-12 16:19:06 +0800881 res = _syscmd_secure(buf, sizeof(buf), "iw list | grep Wiphy | wc -l | tr -d '\\n'");
882 if (res) {
883 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +0800884 }
developerc14d83a2023-06-29 20:09:42 +0800885 if (hal_strtoul(buf, 10, &tmp) < 0) {
886 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +0800887 }
developerc14d83a2023-06-29 20:09:42 +0800888 res = tmp;
developerd14dff12023-06-28 22:47:44 +0800889
890 *max_radio_num = res > MAX_NUM_RADIOS ? MAX_NUM_RADIOS:res;
developer72fb0bb2023-01-11 09:46:29 +0800891
developera3511852023-06-14 14:12:59 +0800892 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800893
developera3511852023-06-14 14:12:59 +0800894 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +0800895}
896
developer17038e62023-03-02 14:43:43 +0800897wifi_band radio_index_to_band(int radioIndex)
898{
developera3511852023-06-14 14:12:59 +0800899 return radio_band[radioIndex];
developer17038e62023-03-02 14:43:43 +0800900}
901
developer72fb0bb2023-01-11 09:46:29 +0800902wifi_band wifi_index_to_band(int apIndex)
903{
developera3511852023-06-14 14:12:59 +0800904 char buf[64] = {0};
developerc14d83a2023-06-29 20:09:42 +0800905 long int nl80211_band = 0;
developera3511852023-06-14 14:12:59 +0800906 int i = 0;
907 int phyIndex = 0;
908 int radioIndex = 0;
909 int max_radio_num = 0;
910 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +0800911 int res;
developera3511852023-06-14 14:12:59 +0800912 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800913
developera3511852023-06-14 14:12:59 +0800914 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +0800915 if(max_radio_num == 0){
916 return RETURN_ERR;
917 }
developera3511852023-06-14 14:12:59 +0800918 radioIndex = apIndex % max_radio_num;
919 phyIndex = radio_index_to_phy(radioIndex);
developer33f13ba2023-07-12 16:19:06 +0800920 while (i < 10) {
921 res = _syscmd_secure(buf, sizeof(buf),
922 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", phyIndex);
923
924 if (res) {
925 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +0800926 }
developer33f13ba2023-07-12 16:19:06 +0800927
developerc14d83a2023-06-29 20:09:42 +0800928 if (hal_strtol(buf, 16, &nl80211_band) < 0) {
929 wifi_debug(DEBUG_ERROR, "strtol fail\n");
930 }
developera3511852023-06-14 14:12:59 +0800931 if (nl80211_band == 1)
932 band = band_2_4;
933 else if (nl80211_band == 2)
934 band = band_5;
935 else if (nl80211_band == 4) // band == 3 is 60GHz
936 band = band_6;
developer72fb0bb2023-01-11 09:46:29 +0800937
developera3511852023-06-14 14:12:59 +0800938 if(band != band_invalid)
939 break;
developer69b61b02023-03-07 17:17:44 +0800940
developera3511852023-06-14 14:12:59 +0800941 i++;
942 sleep(1);
943 }
developer72fb0bb2023-01-11 09:46:29 +0800944
developera3511852023-06-14 14:12:59 +0800945 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
946 return band;
developer72fb0bb2023-01-11 09:46:29 +0800947}
948
949static int wifi_hostapdRead(char *conf_file, char *param, char *output, int output_size)
950{
developer8078acf2023-08-04 18:52:48 +0800951
developer7e4a2a62023-04-06 19:56:03 +0800952 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +0800953 int res = 0;
developer72fb0bb2023-01-11 09:46:29 +0800954
developer72fb0bb2023-01-11 09:46:29 +0800955
developer8078acf2023-08-04 18:52:48 +0800956 res = _syscmd_secure(buf, sizeof(buf), "cat %s 2> /dev/null | grep \"^%s=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",conf_file, param);
957 if (res) {
958 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer7e4a2a62023-04-06 19:56:03 +0800959 }
developerb758dfd2023-06-21 17:32:07 +0800960
developer7e4a2a62023-04-06 19:56:03 +0800961
developere40952c2023-06-15 18:46:43 +0800962 res = snprintf(output, output_size, "%s", buf);
963 if (os_snprintf_error(output_size, res)) {
964 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
965 return RETURN_ERR;
966 }
developer7e4a2a62023-04-06 19:56:03 +0800967
968 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800969}
970
971static int wifi_hostapdWrite(char *conf_file, struct params *list, int item_count)
972{
developer8078acf2023-08-04 18:52:48 +0800973
developera3511852023-06-14 14:12:59 +0800974 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +0800975 int res;
developer72fb0bb2023-01-11 09:46:29 +0800976
developera3511852023-06-14 14:12:59 +0800977 for (int i = 0; i < item_count; i++) {
978 wifi_hostapdRead(conf_file, list[i].name, buf, sizeof(buf));
979 if (strlen(buf) == 0) /*no such item, insert it*/
developer8078acf2023-08-04 18:52:48 +0800980 res = _syscmd_secure(buf, sizeof(buf), "sed -i -e '$a %s=%s' %s", list[i].name, list[i].value, conf_file);
developera3511852023-06-14 14:12:59 +0800981 else /*find the item, update it*/
developer8078acf2023-08-04 18:52:48 +0800982 res = _syscmd_secure(buf, sizeof(buf), "sed -i \"s/^%s=.*/%s=%s/\" %s", list[i].name, list[i].name, list[i].value, conf_file);
developera1255e42023-05-13 17:45:02 +0800983
developer8078acf2023-08-04 18:52:48 +0800984 if(res) {
985 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +0800986 }
developera3511852023-06-14 14:12:59 +0800987 }
developer72fb0bb2023-01-11 09:46:29 +0800988
developera3511852023-06-14 14:12:59 +0800989 return 0;
developera1255e42023-05-13 17:45:02 +0800990}
developerfde01262023-05-22 15:15:24 +0800991
developera1255e42023-05-13 17:45:02 +0800992static int wifi_datfileRead(char *conf_file, char *param, char *output, int output_size)
993{
developere40952c2023-06-15 18:46:43 +0800994 int res = 0;
developera3511852023-06-14 14:12:59 +0800995 int len;
developerfde01262023-05-22 15:15:24 +0800996
developer8078acf2023-08-04 18:52:48 +0800997 res = _syscmd_secure(output, output_size, "datconf -f %s get %s", conf_file, param);
developere40952c2023-06-15 18:46:43 +0800998
developer8078acf2023-08-04 18:52:48 +0800999 if (res) {
1000 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developera3511852023-06-14 14:12:59 +08001001 }
developera1255e42023-05-13 17:45:02 +08001002
developer8078acf2023-08-04 18:52:48 +08001003
developera3511852023-06-14 14:12:59 +08001004 len = strlen(output);
1005 if ((len > 0) && (output[len - 1] == '\n')) {
1006 output[len - 1] = '\0';
1007 }
developerfde01262023-05-22 15:15:24 +08001008
developera3511852023-06-14 14:12:59 +08001009 return 0;
developerfde01262023-05-22 15:15:24 +08001010}
developera1255e42023-05-13 17:45:02 +08001011
developera1255e42023-05-13 17:45:02 +08001012static int wifi_datfileWrite(char *conf_file, struct params *list, int item_count)
1013{
developere40952c2023-06-15 18:46:43 +08001014 int res;
developera3511852023-06-14 14:12:59 +08001015 char buf[MAX_BUF_SIZE] = {0};
developera1255e42023-05-13 17:45:02 +08001016
developera3511852023-06-14 14:12:59 +08001017 for (int i = 0; i < item_count; i++) {
developer8078acf2023-08-04 18:52:48 +08001018 res = _syscmd_secure(buf, sizeof(buf), "datconf -f %s set %s \"%s\"", conf_file, list[i].name, list[i].value);
developera1255e42023-05-13 17:45:02 +08001019
developer8078acf2023-08-04 18:52:48 +08001020 if (res) {
1021 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
1022 }
developera3511852023-06-14 14:12:59 +08001023 }
developera1255e42023-05-13 17:45:02 +08001024
developera3511852023-06-14 14:12:59 +08001025 return 0;
developera1255e42023-05-13 17:45:02 +08001026}
1027
developerfde01262023-05-22 15:15:24 +08001028static int wifi_l1ProfileRead(char *param, char *output, int output_size)
1029{
developera3511852023-06-14 14:12:59 +08001030 int ret;
developerfde01262023-05-22 15:15:24 +08001031
developera3511852023-06-14 14:12:59 +08001032 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1033 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001034 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001035 return RETURN_ERR;
1036 }
developerfde01262023-05-22 15:15:24 +08001037
developera3511852023-06-14 14:12:59 +08001038 ret = wifi_datfileRead(l1profile, param, output, output_size);
1039 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08001040 wifi_debug(DEBUG_ERROR, "wifi_datfileRead %s from %s failed, ret:%d", param, l1profile, ret);
developera3511852023-06-14 14:12:59 +08001041 return RETURN_ERR;
1042 }
developerfde01262023-05-22 15:15:24 +08001043
developera3511852023-06-14 14:12:59 +08001044 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1045 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001046}
1047
1048static int wifi_CardProfileRead(int card_idx, char *param, char *output, int output_size)
1049{
developera3511852023-06-14 14:12:59 +08001050 char option[64];
1051 char card_profile_path[64];
developere40952c2023-06-15 18:46:43 +08001052 int res;
developerfde01262023-05-22 15:15:24 +08001053
developera3511852023-06-14 14:12:59 +08001054 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developerfde01262023-05-22 15:15:24 +08001055
developera3511852023-06-14 14:12:59 +08001056 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001057 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001058 return RETURN_ERR;
1059 }
developerfde01262023-05-22 15:15:24 +08001060
developere40952c2023-06-15 18:46:43 +08001061 res = snprintf(option, sizeof(option), "INDEX%d_profile_path", card_idx);
1062 if (os_snprintf_error(sizeof(option), res)) {
1063 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developera3511852023-06-14 14:12:59 +08001064 return RETURN_ERR;
1065 }
developere40952c2023-06-15 18:46:43 +08001066 res = wifi_l1ProfileRead(option, card_profile_path, sizeof(card_profile_path));
1067 if (res != 0) {
developer75bd10c2023-06-27 11:34:08 +08001068 wifi_debug(DEBUG_ERROR, "wifi_l1ProfileRead %s failed, ret:%d", option, res);
developere40952c2023-06-15 18:46:43 +08001069 return RETURN_ERR;
1070 }
developerfde01262023-05-22 15:15:24 +08001071
developere40952c2023-06-15 18:46:43 +08001072 res = wifi_datfileRead(card_profile_path, param, output, output_size);
1073 if (res != 0) {
developer75bd10c2023-06-27 11:34:08 +08001074 wifi_debug(DEBUG_ERROR, "wifi_datfileRead %s from %s failed, ret:%d", param, card_profile_path, res);
developera3511852023-06-14 14:12:59 +08001075 return RETURN_ERR;
1076 }
developerfde01262023-05-22 15:15:24 +08001077
developera3511852023-06-14 14:12:59 +08001078 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1079 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001080}
1081
1082static int wifi_BandProfileRead(int card_idx,
developera3511852023-06-14 14:12:59 +08001083 int radio_idx,
1084 char *param,
1085 char *output,
1086 int output_size,
1087 char *default_value)
developerfde01262023-05-22 15:15:24 +08001088{
developera3511852023-06-14 14:12:59 +08001089 char option[64];
1090 char band_profile_path[64];
developere40952c2023-06-15 18:46:43 +08001091 int ret, res;
developerfde01262023-05-22 15:15:24 +08001092
developera3511852023-06-14 14:12:59 +08001093 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1094 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001095 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001096 return RETURN_ERR;
1097 }
developerfde01262023-05-22 15:15:24 +08001098
developere40952c2023-06-15 18:46:43 +08001099 res = snprintf(option, sizeof(option), "BN%d_profile_path", radio_idx);
1100 if (os_snprintf_error(sizeof(option), res)) {
1101 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1102 return RETURN_ERR;
1103 }
developera3511852023-06-14 14:12:59 +08001104 ret = wifi_CardProfileRead(card_idx, option, band_profile_path, sizeof(band_profile_path));
1105 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08001106 wifi_debug(DEBUG_ERROR, "wifi_CardProfileRead %s failed, ret:%d", option, ret);
developera3511852023-06-14 14:12:59 +08001107 return RETURN_ERR;
1108 }
developerfde01262023-05-22 15:15:24 +08001109
developera3511852023-06-14 14:12:59 +08001110 ret = wifi_datfileRead(band_profile_path, param, output, output_size);
1111 if (ret != 0) {
1112 if (default_value) {
developere40952c2023-06-15 18:46:43 +08001113 res = snprintf(output, output_size, "%s", default_value);
1114 if (os_snprintf_error(output_size, res)) {
1115 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1116 return RETURN_ERR;
1117 }
developera3511852023-06-14 14:12:59 +08001118 } else {
1119 output[0] = '\0';
1120 }
1121 }
developerfde01262023-05-22 15:15:24 +08001122
developera3511852023-06-14 14:12:59 +08001123 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1124 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001125}
1126
developer72fb0bb2023-01-11 09:46:29 +08001127//For Getting Current Interface Name from corresponding hostapd configuration
1128static int wifi_GetInterfaceName(int apIndex, char *interface_name)
1129{
developera3511852023-06-14 14:12:59 +08001130 char config_file[128] = {0};
developere40952c2023-06-15 18:46:43 +08001131 int res;
developer72fb0bb2023-01-11 09:46:29 +08001132
developera3511852023-06-14 14:12:59 +08001133 if (interface_name == NULL)
1134 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001135
developera3511852023-06-14 14:12:59 +08001136 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001137
developere40952c2023-06-15 18:46:43 +08001138 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
1139 if (os_snprintf_error(sizeof(config_file), res)) {
1140 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1141 return RETURN_ERR;
1142 }
developera3511852023-06-14 14:12:59 +08001143 wifi_hostapdRead(config_file, "interface", interface_name, 16);
1144 if (strlen(interface_name) == 0)
1145 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001146
developera3511852023-06-14 14:12:59 +08001147 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1148 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001149}
1150
developera1255e42023-05-13 17:45:02 +08001151static UCHAR get_bssnum_byindex(INT radio_index, UCHAR *bss_cnt)
1152{
developera3511852023-06-14 14:12:59 +08001153 char interface_name[IF_NAME_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08001154 char buf[MAX_CMD_SIZE]={'\0'};
1155 UCHAR channel = 0;
developere40952c2023-06-15 18:46:43 +08001156 int res;
developera1255e42023-05-13 17:45:02 +08001157
developera3511852023-06-14 14:12:59 +08001158 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
1159 return RETURN_ERR;
1160 /*interface name to channel number*/
developer8078acf2023-08-04 18:52:48 +08001161 res = _syscmd_secure(buf, sizeof(buf), "iw dev %s info | grep -i 'channel' | cut -d ' ' -f2", interface_name);
1162
1163 if (res) {
1164 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001165 }
1166
developera3511852023-06-14 14:12:59 +08001167 channel = atoi(buf);
1168 WIFI_ENTRY_EXIT_DEBUG("%s:channel=%d\n", __func__, channel);
developera1255e42023-05-13 17:45:02 +08001169 /*count dev number with the same channel*/
developer8078acf2023-08-04 18:52:48 +08001170 res = _syscmd_secure(buf, sizeof(buf), "iw dev | grep -i 'channel %d' | wc -l", channel);
1171
1172 if (res) {
1173 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001174 }
1175
developera3511852023-06-14 14:12:59 +08001176 *bss_cnt = atoi(buf) - 1;/*1 for apcli interface*/
1177 WIFI_ENTRY_EXIT_DEBUG("%s:bss_cnt=%d\n", __func__, *bss_cnt);
1178 return RETURN_OK;
developera1255e42023-05-13 17:45:02 +08001179}
developer72fb0bb2023-01-11 09:46:29 +08001180
1181static int wifi_hostapdProcessUpdate(int apIndex, struct params *list, int item_count)
1182{
developera3511852023-06-14 14:12:59 +08001183 char interface_name[16] = {0};
1184 if (multiple_set == TRUE)
1185 return RETURN_OK;
developer8078acf2023-08-04 18:52:48 +08001186 char output[32]="";
developera3511852023-06-14 14:12:59 +08001187 FILE *fp;
developer8078acf2023-08-04 18:52:48 +08001188 int i;
developera3511852023-06-14 14:12:59 +08001189 //NOTE RELOAD should be done in ApplySSIDSettings
1190 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1191 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08001192
1193 for (i=0; i<item_count; i++, list++) {
developer8078acf2023-08-04 18:52:48 +08001194 fp = v_secure_popen("r", "hostapd_cli -i%s SET %s %s", interface_name, list->name, list->value);
1195
developer8078acf2023-08-04 18:52:48 +08001196 if (fp == NULL) {
1197 perror("v_secure_popen failed");
developera3511852023-06-14 14:12:59 +08001198 return -1;
1199 }
developere40952c2023-06-15 18:46:43 +08001200 if (!fgets(output, sizeof(output), fp) || strncmp(output, "OK", 2)) {
developer8078acf2023-08-04 18:52:48 +08001201 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +08001202 perror("fgets failed");
1203 return -1;
1204 }
developer8078acf2023-08-04 18:52:48 +08001205 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +08001206 }
1207 return 0;
developer72fb0bb2023-01-11 09:46:29 +08001208}
1209
developer7e4a2a62023-04-06 19:56:03 +08001210static int wifi_quick_reload_ap(int apIndex)
1211{
1212 char interface_name[IF_NAME_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +08001213 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001214 int res;
developer7e4a2a62023-04-06 19:56:03 +08001215
1216 if (multiple_set == TRUE)
1217 return RETURN_OK;
1218
developer9f2358c2023-09-22 18:42:12 +08001219
developer7e4a2a62023-04-06 19:56:03 +08001220 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1221 return RETURN_ERR;
1222
developer8078acf2023-08-04 18:52:48 +08001223 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s reload", interface_name);
developer9f2358c2023-09-22 18:42:12 +08001224 if (res) {
developer8078acf2023-08-04 18:52:48 +08001225 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer7e4a2a62023-04-06 19:56:03 +08001226 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +08001227 }
developer7e4a2a62023-04-06 19:56:03 +08001228
1229 return RETURN_OK;
1230}
1231
developer72fb0bb2023-01-11 09:46:29 +08001232static int wifi_reloadAp(int apIndex)
1233{
developera3511852023-06-14 14:12:59 +08001234 char interface_name[16] = {0};
developer22e0c672023-06-07 15:25:37 +08001235 int res;
1236
developera3511852023-06-14 14:12:59 +08001237 if (multiple_set == TRUE)
1238 return RETURN_OK;
developera3511852023-06-14 14:12:59 +08001239 char buf[MAX_BUF_SIZE]="";
developer72fb0bb2023-01-11 09:46:29 +08001240
developera3511852023-06-14 14:12:59 +08001241 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1242 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001243
developer8078acf2023-08-04 18:52:48 +08001244 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s reload", interface_name);
1245 if (res) {
1246 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer22e0c672023-06-07 15:25:37 +08001247 return RETURN_ERR;
1248 }
developer72fb0bb2023-01-11 09:46:29 +08001249
developer8078acf2023-08-04 18:52:48 +08001250
1251 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s disable", interface_name);
1252 if (res) {
1253 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer22e0c672023-06-07 15:25:37 +08001254 return RETURN_ERR;
1255 }
developer72fb0bb2023-01-11 09:46:29 +08001256
developer8078acf2023-08-04 18:52:48 +08001257 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s enable", interface_name);
1258 if (res) {
1259 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developera3511852023-06-14 14:12:59 +08001260 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08001261 }
developer72fb0bb2023-01-11 09:46:29 +08001262
developera3511852023-06-14 14:12:59 +08001263 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001264}
1265
1266void wifi_RestartHostapd_2G()
1267{
developera3511852023-06-14 14:12:59 +08001268 int Public2GApIndex = 4;
developer72fb0bb2023-01-11 09:46:29 +08001269
developera3511852023-06-14 14:12:59 +08001270 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1271 wifi_setApEnable(Public2GApIndex, FALSE);
1272 wifi_setApEnable(Public2GApIndex, TRUE);
1273 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001274}
1275
1276void wifi_RestartHostapd_5G()
1277{
developerbb9b20f2023-10-17 18:46:37 +08001278 int Public5GApIndex = 1;
developer72fb0bb2023-01-11 09:46:29 +08001279
developera3511852023-06-14 14:12:59 +08001280 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1281 wifi_setApEnable(Public5GApIndex, FALSE);
1282 wifi_setApEnable(Public5GApIndex, TRUE);
1283 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001284}
1285
1286void wifi_RestartPrivateWifi_2G()
1287{
developera3511852023-06-14 14:12:59 +08001288 int PrivateApIndex = 0;
developer72fb0bb2023-01-11 09:46:29 +08001289
developera3511852023-06-14 14:12:59 +08001290 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1291 wifi_setApEnable(PrivateApIndex, FALSE);
1292 wifi_setApEnable(PrivateApIndex, TRUE);
1293 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001294}
1295
1296void wifi_RestartPrivateWifi_5G()
1297{
developera3511852023-06-14 14:12:59 +08001298 int Private5GApIndex = 1;
developer72fb0bb2023-01-11 09:46:29 +08001299
developera3511852023-06-14 14:12:59 +08001300 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1301 wifi_setApEnable(Private5GApIndex, FALSE);
1302 wifi_setApEnable(Private5GApIndex, TRUE);
1303 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001304}
1305
1306static int writeBandWidth(int radioIndex,char *bw_value)
1307{
developera3511852023-06-14 14:12:59 +08001308 char buf[MAX_BUF_SIZE];
developere40952c2023-06-15 18:46:43 +08001309 int res;
developer72fb0bb2023-01-11 09:46:29 +08001310
developere40952c2023-06-15 18:46:43 +08001311
developer8078acf2023-08-04 18:52:48 +08001312 if (_syscmd_secure(buf, sizeof(buf), "grep SET_BW%d %s", radioIndex, BW_FNAME)) {
developer82160f02023-08-19 15:30:44 +08001313 res = _syscmd_secure(buf, sizeof(buf), "echo SET_BW%d=%s >> /nvram/bw_file.txt", radioIndex, bw_value);
developer8078acf2023-08-04 18:52:48 +08001314 if (res) {
1315 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001316 }
developera3511852023-06-14 14:12:59 +08001317 return RETURN_OK;
1318 }
developer72fb0bb2023-01-11 09:46:29 +08001319
developer8078acf2023-08-04 18:52:48 +08001320
1321 res = _syscmd_secure(buf, sizeof(buf), "sed -i 's/^SET_BW%d=.*$/SET_BW%d=%s/' %s",radioIndex,radioIndex,bw_value,BW_FNAME);
1322 if (res) {
1323 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08001324 }
developera3511852023-06-14 14:12:59 +08001325 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001326}
1327
1328// Input could be "1Mbps"; "5.5Mbps"; "6Mbps"; "2Mbps"; "11Mbps"; "12Mbps"; "24Mbps"
1329INT wifi_setApBeaconRate(INT radioIndex,CHAR *beaconRate)
1330{
developera3511852023-06-14 14:12:59 +08001331 struct params params={'\0'};
1332 char config_file[MAX_BUF_SIZE] = {0};
1333 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08001334 int res;
developer72fb0bb2023-01-11 09:46:29 +08001335
developera3511852023-06-14 14:12:59 +08001336 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1337 // Copy the numeric value
1338 if (strlen (beaconRate) >= 5) {
1339 strncpy(buf, beaconRate, strlen(beaconRate) - 4);
1340 buf[strlen(beaconRate) - 4] = '\0';
developer9ce44382023-06-28 11:09:37 +08001341 } else if (strlen(beaconRate) > 0){
1342 strncpy(buf, beaconRate,sizeof(buf) - 1);
1343 buf[sizeof(buf) - 1] = '\0';
1344 } else
developera3511852023-06-14 14:12:59 +08001345 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001346
developera3511852023-06-14 14:12:59 +08001347 params.name = "beacon_rate";
1348 // hostapd config unit is 100 kbps. To convert Mbps to 100kbps, the value need to multiply 10.
1349 if (strncmp(buf, "5.5", 3) == 0) {
developere40952c2023-06-15 18:46:43 +08001350 res = snprintf(buf, sizeof(buf), "55");
1351 if (os_snprintf_error(sizeof(buf), res)) {
1352 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1353 return RETURN_ERR;
1354 }
developera3511852023-06-14 14:12:59 +08001355 params.value = buf;
1356 } else {
developer32f2a182023-06-27 19:50:41 +08001357 if (strlen(buf) >= (MAX_BUF_SIZE - 1)) {
1358 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
1359 return RETURN_ERR;
1360 }
1361 strncat(buf, "0", sizeof(buf) - strlen(buf) - 1);
developera3511852023-06-14 14:12:59 +08001362 params.value = buf;
1363 }
developer72fb0bb2023-01-11 09:46:29 +08001364
developer32f2a182023-06-27 19:50:41 +08001365 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
1366 if (os_snprintf_error(sizeof(config_file), res)) {
1367 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1368 return RETURN_ERR;
1369 }
1370
developera3511852023-06-14 14:12:59 +08001371 wifi_hostapdWrite(config_file, &params, 1);
1372 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
1373 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001374
developera3511852023-06-14 14:12:59 +08001375 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001376}
1377
1378INT wifi_getApBeaconRate(INT radioIndex, CHAR *beaconRate)
1379{
developera3511852023-06-14 14:12:59 +08001380 char config_file[128] = {'\0'};
1381 char temp_output[MAX_BUF_SIZE] = {'\0'};
1382 char buf[128] = {'\0'};
developer8078acf2023-08-04 18:52:48 +08001383
developerc14d83a2023-06-29 20:09:42 +08001384 long int rate = 0;
developere40952c2023-06-15 18:46:43 +08001385 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08001386
developera3511852023-06-14 14:12:59 +08001387 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1388 if (NULL == beaconRate)
1389 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001390
developer75bd10c2023-06-27 11:34:08 +08001391 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
1392 if (os_snprintf_error(sizeof(config_file), res)) {
1393 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1394 return RETURN_ERR;
1395 }
developera3511852023-06-14 14:12:59 +08001396 wifi_hostapdRead(config_file, "beacon_rate", buf, sizeof(buf));
1397 phyId = radio_index_to_phy(radioIndex);
1398 // Hostapd unit is 100kbps. To convert to 100kbps to Mbps, the value need to divide 10.
1399 if(strlen(buf) > 0) {
1400 if (strncmp(buf, "55", 2) == 0)
developere40952c2023-06-15 18:46:43 +08001401 res = snprintf(temp_output, sizeof(temp_output), "5.5Mbps");
developera3511852023-06-14 14:12:59 +08001402 else {
developerc14d83a2023-06-29 20:09:42 +08001403 if (hal_strtol(buf, 10, &rate) < 0) {
1404 wifi_debug(DEBUG_ERROR, "strtol fail\n");
1405 }
developerb14b3462023-07-01 18:02:42 +08001406 res = snprintf(temp_output, sizeof(temp_output), "%ldMbps", rate/10);
developera3511852023-06-14 14:12:59 +08001407 }
developer75bd10c2023-06-27 11:34:08 +08001408 if (os_snprintf_error(sizeof(temp_output), res)) {
1409 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1410 return RETURN_ERR;
1411 }
developera3511852023-06-14 14:12:59 +08001412 } else {
1413 // config not set, so we would use lowest rate as default
developer8078acf2023-08-04 18:52:48 +08001414 res = _syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep Bitrates -A1 | tail -n 1 | awk '{print $2}' | tr -d '.0\\n'", phyId);
1415 if (res) {
1416 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08001417 }
developer8078acf2023-08-04 18:52:48 +08001418
developere40952c2023-06-15 18:46:43 +08001419 res = snprintf(temp_output, sizeof(temp_output), "%sMbps", buf);
developer75bd10c2023-06-27 11:34:08 +08001420 if (os_snprintf_error(sizeof(temp_output), res)) {
1421 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1422 return RETURN_ERR;
1423 }
developera3511852023-06-14 14:12:59 +08001424 }
developer75bd10c2023-06-27 11:34:08 +08001425
developera3511852023-06-14 14:12:59 +08001426 strncpy(beaconRate, temp_output, strlen(temp_output));
1427 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001428
developera3511852023-06-14 14:12:59 +08001429 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001430}
1431
1432INT wifi_setLED(INT radioIndex, BOOL enable)
1433{
1434 return 0;
1435}
1436INT wifi_setRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG seconds)
1437{
1438 return RETURN_OK;
1439}
1440/**********************************************************************************
1441 *
developer69b61b02023-03-07 17:17:44 +08001442 * Wifi Subsystem level function prototypes
developer72fb0bb2023-01-11 09:46:29 +08001443 *
1444**********************************************************************************/
1445//---------------------------------------------------------------------------------------------------
1446//Wifi system api
1447//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 +08001448INT wifi_getHalVersion(CHAR *output_string) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08001449{
developere40952c2023-06-15 18:46:43 +08001450 int res;
1451
developera3511852023-06-14 14:12:59 +08001452 if(!output_string)
1453 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08001454 res = snprintf(output_string, 64, "%d.%d.%d", WIFI_HAL_MAJOR_VERSION, WIFI_HAL_MINOR_VERSION, WIFI_HAL_MAINTENANCE_VERSION);
1455 if (os_snprintf_error(64, res)) {
1456 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1457 return RETURN_ERR;
1458 }
developer72fb0bb2023-01-11 09:46:29 +08001459
developera3511852023-06-14 14:12:59 +08001460 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001461}
1462
1463
1464/* wifi_factoryReset() function */
1465/**
developer69b61b02023-03-07 17:17:44 +08001466* @description Clears internal variables to implement a factory reset of the Wi-Fi
developer72fb0bb2023-01-11 09:46:29 +08001467* subsystem. Resets Implementation specifics may dictate some functionality since different hardware implementations may have different requirements.
1468*
1469* @param None
1470*
1471* @return The status of the operation.
1472* @retval RETURN_OK if successful.
1473* @retval RETURN_ERR if any error is detected
1474*
1475* @execution Synchronous
1476* @sideeffect None
1477*
1478* @note This function must not suspend and must not invoke any blocking system
1479* calls. It should probably just send a message to a driver event handler task.
1480*
1481*/
1482INT wifi_factoryReset()
1483{
developer47cc27a2023-05-17 23:09:58 +08001484 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001485 int res;
developer72fb0bb2023-01-11 09:46:29 +08001486
developer47cc27a2023-05-17 23:09:58 +08001487 /*delete running hostapd conf files*/
1488 wifi_dbg_printf("\n[%s]: deleting hostapd conf file.", __func__);
developere40952c2023-06-15 18:46:43 +08001489
developer8078acf2023-08-04 18:52:48 +08001490 res = _syscmd_secure(buf, sizeof(buf), "rm -rf /nvram/*.conf");
1491 if (res) {
1492 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
1493 }
developer72fb0bb2023-01-11 09:46:29 +08001494
developerd14dff12023-06-28 22:47:44 +08001495 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08001496 wifi_psk_file_reset();
1497
developer8078acf2023-08-04 18:52:48 +08001498
developer47cc27a2023-05-17 23:09:58 +08001499 memset(buf, 0, MAX_BUF_SIZE);
1500
developer8078acf2023-08-04 18:52:48 +08001501 res = _syscmd_secure(buf, sizeof(buf), "systemctl restart hostapd.service");
1502 if (res) {
1503 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001504 }
1505
developer47cc27a2023-05-17 23:09:58 +08001506 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001507}
1508
1509/* wifi_factoryResetRadios() function */
1510/**
1511* @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.
1512*
1513* @param None
1514* @return The status of the operation
1515* @retval RETURN_OK if successful
1516* @retval RETURN_ERR if any error is detected
1517*
1518* @execution Synchronous
1519*
1520* @sideeffect None
1521*
1522* @note This function must not suspend and must not invoke any blocking system
1523* calls. It should probably just send a message to a driver event handler task.
1524*
1525*/
1526INT wifi_factoryResetRadios()
1527{
developera3511852023-06-14 14:12:59 +08001528 if((RETURN_OK == wifi_factoryResetRadio(0)) && (RETURN_OK == wifi_factoryResetRadio(1)))
1529 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001530
developera3511852023-06-14 14:12:59 +08001531 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001532}
1533
developerfead3972023-05-25 20:15:02 +08001534ULONG get_radio_reset_cnt(int radioIndex)
1535{
developerfead3972023-05-25 20:15:02 +08001536 char buf[MAX_BUF_SIZE] = {0};
1537 ULONG reset_count = 0;
developere40952c2023-06-15 18:46:43 +08001538 int res;
developerfead3972023-05-25 20:15:02 +08001539
developer8078acf2023-08-04 18:52:48 +08001540 res = _syscmd_secure(buf, sizeof(buf), "cat %s 2> /dev/null | grep \"^reset%d=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developerfead3972023-05-25 20:15:02 +08001541 RADIO_RESET_FILE, radioIndex);
developer8078acf2023-08-04 18:52:48 +08001542 if (res) {
1543 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001544 }
1545
developerfead3972023-05-25 20:15:02 +08001546 if (strlen(buf) == 0)
1547 return 0;
1548 else {
1549 reset_count = atol(buf);
1550 return reset_count;
1551 }
1552}
developerd08b7d52023-08-22 15:41:36 +08001553
1554void reset_guard_interval(int radioIndex)
1555{
1556 char buf[MAX_BUF_SIZE] = {0};
1557 int res;
1558 FILE *f = NULL;
1559
1560 res = snprintf(buf, sizeof(buf), "%s%d.txt", GUARD_INTERVAL_FILE, radioIndex);
1561 if (os_snprintf_error(sizeof(buf), res)) {
1562 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1563 return;
1564 }
1565 f = fopen(buf, "w");
1566 if (f == NULL)
1567 return;
1568 fprintf(f, "%s", "auto");
1569 if (fclose(f) == EOF)
1570 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
1571}
1572
developerfead3972023-05-25 20:15:02 +08001573void update_radio_reset_cnt(int radioIndex)
1574{
developerfead3972023-05-25 20:15:02 +08001575 char buf[MAX_BUF_SIZE] = {0};
1576 ULONG reset_count = 0;
developere40952c2023-06-15 18:46:43 +08001577 int res;
developerfead3972023-05-25 20:15:02 +08001578
developer8078acf2023-08-04 18:52:48 +08001579 res = _syscmd_secure(buf, sizeof(buf), "cat %s 2> /dev/null | grep \"^reset%d=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
1580 RADIO_RESET_FILE, radioIndex );
1581 if (res) {
1582 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001583 }
1584
developerfead3972023-05-25 20:15:02 +08001585 if (strlen(buf) == 0)
developer8078acf2023-08-04 18:52:48 +08001586 res =_syscmd_secure(buf, sizeof(buf), "sed -i -e '$a reset%d=1' %s", radioIndex, RADIO_RESET_FILE);
developerfead3972023-05-25 20:15:02 +08001587 else {
1588 reset_count = atol(buf);
1589 reset_count++;
developer8078acf2023-08-04 18:52:48 +08001590 res = _syscmd_secure(buf,sizeof(buf), "sed -i \"s/^reset%d=.*/reset%d=%lu/\" %s", radioIndex, radioIndex, reset_count, RADIO_RESET_FILE);
developere40952c2023-06-15 18:46:43 +08001591 }
developer8078acf2023-08-04 18:52:48 +08001592
1593 if (res) {
1594 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developerfead3972023-05-25 20:15:02 +08001595 }
developerfead3972023-05-25 20:15:02 +08001596}
developer72fb0bb2023-01-11 09:46:29 +08001597
1598/* wifi_factoryResetRadio() function */
1599/**
1600* @description Restore selected radio parameters without touching access point parameters
1601*
1602* @param radioIndex - Index of Wi-Fi Radio channel
1603*
1604* @return The status of the operation.
1605* @retval RETURN_OK if successful.
1606* @retval RETURN_ERR if any error is detected
1607*
1608* @execution Synchronous.
1609* @sideeffect None.
1610*
1611* @note This function must not suspend and must not invoke any blocking system
1612* calls. It should probably just send a message to a driver event handler task.
1613*
1614*/
1615INT wifi_factoryResetRadio(int radioIndex) //RDKB
1616{
developer47cc27a2023-05-17 23:09:58 +08001617 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001618
developerb2977562023-05-24 17:54:12 +08001619 wifi_dat_file_reset_by_radio(radioIndex);
developer47cc27a2023-05-17 23:09:58 +08001620
developerb2977562023-05-24 17:54:12 +08001621 /*reset gi setting*/
developerd08b7d52023-08-22 15:41:36 +08001622 reset_guard_interval(radioIndex);
1623
developerb2977562023-05-24 17:54:12 +08001624 /*TBD: check mbss issue*/
1625 wifi_factoryResetAP(radioIndex);
developerfead3972023-05-25 20:15:02 +08001626 update_radio_reset_cnt(radioIndex);
developerb2977562023-05-24 17:54:12 +08001627
developer47cc27a2023-05-17 23:09:58 +08001628 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
1629 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001630}
1631
1632/* wifi_initRadio() function */
1633/**
1634* Description: This function call initializes the specified radio.
developer69b61b02023-03-07 17:17:44 +08001635* Implementation specifics may dictate the functionality since
developer72fb0bb2023-01-11 09:46:29 +08001636* different hardware implementations may have different initilization requirements.
1637* Parameters : radioIndex - The index of the radio. First radio is index 0. 2nd radio is index 1 - type INT
1638*
1639* @return The status of the operation.
1640* @retval RETURN_OK if successful.
1641* @retval RETURN_ERR if any error is detected
1642*
1643* @execution Synchronous.
1644* @sideeffect None.
1645*
1646* @note This function must not suspend and must not invoke any blocking system
1647* calls. It should probably just send a message to a driver event handler task.
1648*
1649*/
1650INT wifi_initRadio(INT radioIndex)
1651{
developera3511852023-06-14 14:12:59 +08001652 //TODO: Initializes the wifi subsystem (for specified radio)
1653 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001654}
1655
developer17038e62023-03-02 14:43:43 +08001656static void
1657wifi_ParseProfile(void)
1658{
developere40952c2023-06-15 18:46:43 +08001659 int i, res;
developera3511852023-06-14 14:12:59 +08001660 int max_radio_num = 0;
1661 int card_idx;
1662 int band_idx;
1663 int phy_idx = 0;
1664 int wireless_mode = 0;
1665 char buf[MAX_BUF_SIZE] = {0};
1666 char chip_name[12];
1667 char card_profile[MAX_BUF_SIZE] = {0};
1668 char band_profile[MAX_BUF_SIZE] = {0};
developer17038e62023-03-02 14:43:43 +08001669
developera3511852023-06-14 14:12:59 +08001670 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001671
developera3511852023-06-14 14:12:59 +08001672 memset(main_prefix, 0, sizeof(main_prefix));
1673 memset(ext_prefix, 0, sizeof(ext_prefix));
1674 memset(default_ssid, 0, sizeof(default_ssid));
1675 for (i = 0; i < MAX_NUM_RADIOS; i++)
1676 radio_band[i] = band_invalid;
developer17038e62023-03-02 14:43:43 +08001677
developera3511852023-06-14 14:12:59 +08001678 if (wifi_getMaxRadioNumber(&max_radio_num) != RETURN_OK) {
1679 /* LOG */
developer17038e62023-03-02 14:43:43 +08001680 return;
developera3511852023-06-14 14:12:59 +08001681 }
developer17038e62023-03-02 14:43:43 +08001682
developera3511852023-06-14 14:12:59 +08001683 for (card_idx = 0; card_idx < 3; card_idx++) {
developere40952c2023-06-15 18:46:43 +08001684 res = snprintf(buf, sizeof(buf), "INDEX%d", card_idx);
1685 if (os_snprintf_error(sizeof(buf), res)) {
1686 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1687 return;
1688 }
developera3511852023-06-14 14:12:59 +08001689 if (get_value(l1profile, buf, chip_name, sizeof(chip_name)) < 0) {
1690 break;
1691 }
developere40952c2023-06-15 18:46:43 +08001692 res = snprintf(buf, sizeof(buf), "INDEX%d_profile_path", card_idx);
1693 if (os_snprintf_error(sizeof(buf), res)) {
1694 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1695 return;
1696 }
developera3511852023-06-14 14:12:59 +08001697 if (get_value(l1profile, buf, card_profile, sizeof(card_profile)) < 0) {
1698 break;
1699 }
1700 for (band_idx = 0; band_idx < 3; band_idx++) {
developere40952c2023-06-15 18:46:43 +08001701 res = snprintf(buf, sizeof(buf), "BN%d_profile_path", band_idx);
1702 if (os_snprintf_error(sizeof(buf), res)) {
1703 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1704 return;
1705 }
developera3511852023-06-14 14:12:59 +08001706 if (get_value(card_profile, buf, band_profile, sizeof(band_profile)) < 0) {
1707 /* LOG */
1708 break;
1709 }
developer17038e62023-03-02 14:43:43 +08001710
developere40952c2023-06-15 18:46:43 +08001711 res = snprintf(buf, sizeof(buf), "INDEX%d_main_ifname", card_idx);
1712 if (os_snprintf_error(sizeof(buf), res)) {
1713 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1714 return;
1715 }
developera3511852023-06-14 14:12:59 +08001716 if (get_value_by_idx(l1profile, buf, band_idx, main_prefix[phy_idx], IFNAMSIZ) < 0) {
1717 /* LOG */
1718 }
developer17038e62023-03-02 14:43:43 +08001719
developere40952c2023-06-15 18:46:43 +08001720 res = snprintf(buf, sizeof(buf), "INDEX%d_ext_ifname", card_idx);
1721 if (os_snprintf_error(sizeof(buf), res)) {
1722 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1723 return;
1724 }
developera3511852023-06-14 14:12:59 +08001725 if (get_value_by_idx(l1profile, buf, band_idx, ext_prefix[phy_idx], IFNAMSIZ) < 0) {
1726 /* LOG */
1727 }
developer17038e62023-03-02 14:43:43 +08001728
developera3511852023-06-14 14:12:59 +08001729 if (get_value(band_profile, "SSID1", default_ssid[phy_idx], sizeof(default_ssid[phy_idx])) < 0) {
1730 /* LOG */
1731 }
1732 if (get_value(band_profile, "WirelessMode", buf, sizeof(buf)) < 0) {
1733 /* LOG */
1734 }
developer745f0bd2023-03-06 14:32:53 +08001735
developera3511852023-06-14 14:12:59 +08001736 wireless_mode = atoi(buf);
1737 switch (wireless_mode) {
1738 case 22:
1739 case 16:
1740 case 6:
1741 case 4:
1742 case 1:
1743 radio_band[phy_idx] = band_2_4;
1744 break;
1745 case 23:
1746 case 17:
1747 case 14:
1748 case 11:
1749 case 2:
1750 radio_band[phy_idx] = band_5;
1751 break;
1752 case 24:
1753 case 18:
1754 radio_band[phy_idx] = band_6;
1755 break;
1756 }
1757 phy_idx++;
1758 }
1759 }
developer17038e62023-03-02 14:43:43 +08001760
developera3511852023-06-14 14:12:59 +08001761 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001762}
1763
1764static void
developerd14dff12023-06-28 22:47:44 +08001765wifi_PrepareDefaultHostapdConfigs(bool reset)
developer17038e62023-03-02 14:43:43 +08001766{
developere40952c2023-06-15 18:46:43 +08001767 int radio_idx, res;
developer0132ed92023-03-21 13:48:53 +08001768 int bss_idx;
1769 int ap_idx;
developer8078acf2023-08-04 18:52:48 +08001770
developerb149d9d2023-06-06 16:14:22 +08001771 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer0132ed92023-03-21 13:48:53 +08001772 char ssid[MAX_BUF_SIZE] = {0};
1773 char interface[32] = {0};
1774 char ret_buf[MAX_BUF_SIZE] = {0};
1775 char psk_file[64] = {0};
1776 struct params params[3];
developer17038e62023-03-02 14:43:43 +08001777
developer0132ed92023-03-21 13:48:53 +08001778 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1779 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
developer47cc27a2023-05-17 23:09:58 +08001780
developerbb9b20f2023-10-17 18:46:37 +08001781 for (bss_idx = 0; bss_idx < LOGAN_MAX_NUM_VAP_PER_RADIO; bss_idx++) {
developer47cc27a2023-05-17 23:09:58 +08001782 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
developer0132ed92023-03-21 13:48:53 +08001783
developere40952c2023-06-15 18:46:43 +08001784 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_idx);
1785 if (os_snprintf_error(sizeof(config_file), res)) {
1786 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1787 return;
1788 }
developerd14dff12023-06-28 22:47:44 +08001789 if (access(config_file, F_OK) == 0 && reset == FALSE)
1790 continue;
developer8078acf2023-08-04 18:52:48 +08001791 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], config_file);
1792 if (res) {
1793 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001794 }
developer8078acf2023-08-04 18:52:48 +08001795
1796
1797
1798
developer17038e62023-03-02 14:43:43 +08001799
developer47cc27a2023-05-17 23:09:58 +08001800 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08001801 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
1802 if (os_snprintf_error(sizeof(ssid), res)) {
1803 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1804 return;
1805 }
1806 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
1807 if (os_snprintf_error(sizeof(interface), res)) {
1808 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1809 return;
1810 }
developer47cc27a2023-05-17 23:09:58 +08001811 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08001812 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
1813 if (os_snprintf_error(sizeof(ssid), res)) {
1814 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1815 return;
1816 }
1817 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
1818 if (os_snprintf_error(sizeof(interface), res)) {
1819 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1820 return;
1821 }
developer47cc27a2023-05-17 23:09:58 +08001822 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08001823 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
1824 if (os_snprintf_error(sizeof(ssid), res)) {
1825 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1826 return;
1827 }
1828 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
1829 if (os_snprintf_error(sizeof(interface), res)) {
1830 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1831 return;
1832 }
developer47cc27a2023-05-17 23:09:58 +08001833 }
developer17038e62023-03-02 14:43:43 +08001834
developer47cc27a2023-05-17 23:09:58 +08001835 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08001836 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", ap_idx);
1837 if (os_snprintf_error(sizeof(psk_file), res)) {
1838 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1839 return;
1840 }
developer17038e62023-03-02 14:43:43 +08001841
developer47cc27a2023-05-17 23:09:58 +08001842 params[0].name = "ssid";
1843 params[0].value = ssid;
1844 params[1].name = "interface";
1845 params[1].value = interface;
1846 params[2].name = "wpa_psk_file";
1847 params[2].value = psk_file;
developer17038e62023-03-02 14:43:43 +08001848
developer47cc27a2023-05-17 23:09:58 +08001849 wifi_hostapdWrite(config_file, params, 3);
developer0132ed92023-03-21 13:48:53 +08001850 }
1851 }
1852 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001853}
1854
1855static void
developer4fe22c62023-08-03 13:23:58 +08001856wifiBringUpInterfacesForRadio(int radio_idx)
1857{
1858 int bss_idx;
1859 int ap_idx;
1860 char cmd[MAX_CMD_SIZE] = {0};
1861 char config_file[MAX_SUB_CMD_SIZE] = {0};
1862 char ret_buf[MAX_BUF_SIZE] = {0};
1863 char inf_name[IF_NAME_SIZE] = {0};
developer82160f02023-08-19 15:30:44 +08001864 int res, ret, bss_num;
developer4fe22c62023-08-03 13:23:58 +08001865
1866 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1867
1868 bss_idx = 0;
developer82160f02023-08-19 15:30:44 +08001869
1870 ret = wifi_BandProfileRead(0, radio_idx, "BssidNum", ret_buf, sizeof(ret_buf), "1");
1871 if (ret != 0) {
1872 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead BssidNum failed\n");
1873 return;
1874 }
1875
1876 bss_num = atoi(ret_buf);
1877 if (bss_num <= 0) {
1878 wifi_debug(DEBUG_ERROR, "invalid BssidNum %s\n", ret_buf);
1879 return ;
1880 }
developerbb9b20f2023-10-17 18:46:37 +08001881
1882 if (bss_num > LOGAN_MAX_NUM_VAP_PER_RADIO) {
1883 wifi_debug(DEBUG_ERROR, "bss_num is larger than %d, use %d\n", LOGAN_MAX_NUM_VAP_PER_RADIO, LOGAN_MAX_NUM_VAP_PER_RADIO);
1884 bss_num = LOGAN_MAX_NUM_VAP_PER_RADIO;
1885 }
1886
developer82160f02023-08-19 15:30:44 +08001887 wifi_debug(DEBUG_ERROR, "band %d BssidNum %d\n", radio_idx, bss_num);
developer4fe22c62023-08-03 13:23:58 +08001888 /*TBD: we need refine setup flow and mbss flow*/
developer82160f02023-08-19 15:30:44 +08001889 for (bss_idx = 0; bss_idx < bss_num; bss_idx++) {
developer4fe22c62023-08-03 13:23:58 +08001890 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
1891
developere7e0ae42023-08-17 09:29:49 +08001892 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "touch %s%d.psk", PSK_FILE, ap_idx);
1893 if (res) {
1894 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer4fe22c62023-08-03 13:23:58 +08001895 }
developer4fe22c62023-08-03 13:23:58 +08001896
1897 memset(cmd, 0, MAX_CMD_SIZE);
1898 memset(ret_buf, 0, MAX_BUF_SIZE);
1899
1900 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_idx);
1901 if (os_snprintf_error(sizeof(cmd), res)) {
1902 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1903 return;
1904 }
developer82160f02023-08-19 15:30:44 +08001905
developere7e0ae42023-08-17 09:29:49 +08001906 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "hostapd_cli -i global raw ADD bss_config=phy%d:%s", radio_idx, config_file);
1907 if (res) {
1908 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer4fe22c62023-08-03 13:23:58 +08001909 }
developer4fe22c62023-08-03 13:23:58 +08001910
1911 wifi_GetInterfaceName(ap_idx, inf_name);
developer82160f02023-08-19 15:30:44 +08001912 wifi_debug(DEBUG_ERROR, "bring up %s\n", inf_name);
developer4fe22c62023-08-03 13:23:58 +08001913
1914 memset(cmd, 0, MAX_CMD_SIZE);
1915 memset(ret_buf, 0, MAX_BUF_SIZE);
1916
1917 /* fix vap-status file */
developere7e0ae42023-08-17 09:29:49 +08001918 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "sed -i \"s/^%s=.*/%s=1/\" %s", inf_name, inf_name, VAP_STATUS_FILE);
1919 if (res) {
1920 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer4fe22c62023-08-03 13:23:58 +08001921 }
developere7e0ae42023-08-17 09:29:49 +08001922
developer82160f02023-08-19 15:30:44 +08001923 }
developer4fe22c62023-08-03 13:23:58 +08001924
1925 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1926}
1927
1928
1929static void
1930wifi_BringUpInterfaces(void)
1931{
1932 int radio_idx;
1933 int band_idx;
1934
1935 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1936 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
1937 band_idx = radio_index_to_band(radio_idx);
1938 if (band_idx < 0) {
1939 break;
1940 }
1941 wifiBringUpInterfacesForRadio(radio_idx);
1942 }
1943 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1944}
1945
1946static void
developer17038e62023-03-02 14:43:43 +08001947wifi_BringDownInterfacesForRadio(int radio_idx)
1948{
developer8078acf2023-08-04 18:52:48 +08001949
developera3511852023-06-14 14:12:59 +08001950 char ret_buf[MAX_BUF_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08001951 int res;
developerb758dfd2023-06-21 17:32:07 +08001952
developera3511852023-06-14 14:12:59 +08001953 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001954
developer8078acf2023-08-04 18:52:48 +08001955 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "hostapd_cli -i global raw REMOVE %s", main_prefix[radio_idx]);
1956 if (res) {
1957 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08001958 }
developer8a3bbbf2023-03-15 17:47:23 +08001959
developera3511852023-06-14 14:12:59 +08001960 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001961}
1962
1963
1964static void
1965wifi_BringDownInterfaces(void)
1966{
developera3511852023-06-14 14:12:59 +08001967 int radio_idx;
1968 int band_idx;
developer17038e62023-03-02 14:43:43 +08001969
developera3511852023-06-14 14:12:59 +08001970 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1971 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
1972 band_idx = radio_index_to_band(radio_idx);
1973 if (band_idx < 0) {
1974 break;
1975 }
1976 wifi_BringDownInterfacesForRadio(radio_idx);
1977 }
1978 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001979}
1980
developerb2977562023-05-24 17:54:12 +08001981static void wifi_dat_file_reset_by_radio(char radio_idx)
1982{
developer8078acf2023-08-04 18:52:48 +08001983
developerb2977562023-05-24 17:54:12 +08001984 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08001985 char rom_dat_file[MAX_SUB_CMD_SIZE]= {0};
1986 char dat_file[MAX_SUB_CMD_SIZE]= {0};
developere40952c2023-06-15 18:46:43 +08001987 int res;
developerb2977562023-05-24 17:54:12 +08001988
developere40952c2023-06-15 18:46:43 +08001989 res = snprintf(rom_dat_file, sizeof(rom_dat_file), "%s%d.dat", ROM_LOGAN_DAT_FILE, radio_idx);
1990 if (os_snprintf_error(sizeof(rom_dat_file), res)) {
1991 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1992 return;
1993 }
1994 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
1995 if (os_snprintf_error(sizeof(dat_file), res)) {
1996 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1997 return;
1998 }
developerb2977562023-05-24 17:54:12 +08001999
developer8078acf2023-08-04 18:52:48 +08002000 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "cp -rf %s %s", rom_dat_file, dat_file);
2001 if (res) {
2002 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
2003 }
developerb2977562023-05-24 17:54:12 +08002004}
2005
developer47cc27a2023-05-17 23:09:58 +08002006static void wifi_psk_file_reset()
2007{
developer47cc27a2023-05-17 23:09:58 +08002008 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08002009 char psk_file[MAX_SUB_CMD_SIZE]= {0};
developer47cc27a2023-05-17 23:09:58 +08002010 char vap_idx = 0;
developere40952c2023-06-15 18:46:43 +08002011 int res;
developer47cc27a2023-05-17 23:09:58 +08002012
2013 for (vap_idx = 0; vap_idx < MAX_APS; vap_idx++) {
developere40952c2023-06-15 18:46:43 +08002014 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, vap_idx);
2015 if (os_snprintf_error(sizeof(psk_file), res)) {
2016 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2017 return;
2018 }
developer47cc27a2023-05-17 23:09:58 +08002019
2020 if (access(psk_file, F_OK) != 0) {
developer8078acf2023-08-04 18:52:48 +08002021 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "touch %s", psk_file);
2022 if (res) {
2023 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002024 }
developer47cc27a2023-05-17 23:09:58 +08002025 } else {
developere40952c2023-06-15 18:46:43 +08002026
developer8078acf2023-08-04 18:52:48 +08002027 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "echo '' > %s", psk_file);
2028 if (res) {
2029 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
2030 }
developer47cc27a2023-05-17 23:09:58 +08002031 }
2032 }
developerb2977562023-05-24 17:54:12 +08002033}
2034
developer8a3bbbf2023-03-15 17:47:23 +08002035static void wifi_vap_status_reset()
2036{
developera3511852023-06-14 14:12:59 +08002037 char ret_buf[MAX_BUF_SIZE] = {0};
developer863a4a62023-06-06 16:55:59 +08002038 int radio_idx = 0;
developer8a3bbbf2023-03-15 17:47:23 +08002039 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08002040 int res;
developer8666b312023-03-24 14:05:31 +08002041
developer8a3bbbf2023-03-15 17:47:23 +08002042 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developer8078acf2023-08-04 18:52:48 +08002043 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "touch %s", VAP_STATUS_FILE);
2044 if (res) {
2045 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002046 }
developer8a3bbbf2023-03-15 17:47:23 +08002047 } else {
developer8078acf2023-08-04 18:52:48 +08002048 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "echo '' > %s", VAP_STATUS_FILE);
2049 if (res) {
2050 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002051 }
developer8a3bbbf2023-03-15 17:47:23 +08002052 }
2053
developer8a3bbbf2023-03-15 17:47:23 +08002054 memset(ret_buf, 0, MAX_BUF_SIZE);
2055
2056 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++)
developerbb9b20f2023-10-17 18:46:37 +08002057 for (bss_idx = 0; bss_idx < LOGAN_MAX_NUM_VAP_PER_RADIO; bss_idx++) {
developer82160f02023-08-19 15:30:44 +08002058 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "echo %s%d=0 >> /nvram/vap-status", ext_prefix[radio_idx], bss_idx);
developer8078acf2023-08-04 18:52:48 +08002059 if (res) {
2060 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002061 }
developer8a3bbbf2023-03-15 17:47:23 +08002062 }
2063
developerfead3972023-05-25 20:15:02 +08002064}
2065
2066static void wifi_radio_reset_count_reset()
2067{
developer8078acf2023-08-04 18:52:48 +08002068
developera3511852023-06-14 14:12:59 +08002069 char ret_buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002070 int res;
developerfead3972023-05-25 20:15:02 +08002071
2072 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developer8078acf2023-08-04 18:52:48 +08002073 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "touch %s", RADIO_RESET_FILE);
2074 if (res) {
2075 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002076 }
developerfead3972023-05-25 20:15:02 +08002077 } else {
developer82160f02023-08-19 15:30:44 +08002078 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "echo '' > /nvram/radio_reset");
developer8078acf2023-08-04 18:52:48 +08002079 if (res) {
2080 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002081 }
developerfead3972023-05-25 20:15:02 +08002082 }
developer8a3bbbf2023-03-15 17:47:23 +08002083}
developer17038e62023-03-02 14:43:43 +08002084
developer72fb0bb2023-01-11 09:46:29 +08002085// Initializes the wifi subsystem (all radios)
developera3511852023-06-14 14:12:59 +08002086INT wifi_init() //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002087{
developera3511852023-06-14 14:12:59 +08002088 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developercc9a4f32023-05-30 17:40:02 +08002089 static int CallOnce = 1;
developera3511852023-06-14 14:12:59 +08002090 //Not intitializing macfilter for Turris-Omnia Platform for now
2091 //macfilter_init();
2092 if (CallOnce) {
developercc9a4f32023-05-30 17:40:02 +08002093 wifi_ParseProfile();
developerd14dff12023-06-28 22:47:44 +08002094 wifi_PrepareDefaultHostapdConfigs(FALSE);
developercc9a4f32023-05-30 17:40:02 +08002095 wifi_psk_file_reset();
developer33f13ba2023-07-12 16:19:06 +08002096 //v_secure_system("/usr/sbin/iw reg set US");
2097 v_secure_system("systemctl start hostapd.service");
developercc9a4f32023-05-30 17:40:02 +08002098 sleep(2);
developerc338aba2023-08-09 13:56:42 +08002099 setbuf(stdout, NULL);
developercc9a4f32023-05-30 17:40:02 +08002100 wifi_vap_status_reset();
2101 wifi_radio_reset_count_reset();
developer4fe22c62023-08-03 13:23:58 +08002102 wifi_BringUpInterfaces();
developercc9a4f32023-05-30 17:40:02 +08002103 CallOnce = 0;
developera3511852023-06-14 14:12:59 +08002104 }
developer96b38512023-02-22 11:17:45 +08002105
developera3511852023-06-14 14:12:59 +08002106 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002107
developera3511852023-06-14 14:12:59 +08002108 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002109}
2110
2111/* wifi_reset() function */
2112/**
2113* Description: Resets the Wifi subsystem. This includes reset of all AP varibles.
developer69b61b02023-03-07 17:17:44 +08002114* Implementation specifics may dictate what is actualy reset since
developer72fb0bb2023-01-11 09:46:29 +08002115* different hardware implementations may have different requirements.
2116* Parameters : None
2117*
2118* @return The status of the operation.
2119* @retval RETURN_OK if successful.
2120* @retval RETURN_ERR if any error is detected
2121*
2122* @execution Synchronous.
2123* @sideeffect None.
2124*
2125* @note This function must not suspend and must not invoke any blocking system
2126* calls. It should probably just send a message to a driver event handler task.
2127*
2128*/
2129INT wifi_reset()
2130{
developer17038e62023-03-02 14:43:43 +08002131
developera3511852023-06-14 14:12:59 +08002132 wifi_BringDownInterfaces();
2133 sleep(2);
developer17038e62023-03-02 14:43:43 +08002134
developera3511852023-06-14 14:12:59 +08002135 //TODO: resets the wifi subsystem, deletes all APs
developer33f13ba2023-07-12 16:19:06 +08002136 v_secure_system("systemctl stop hostapd.service");
developera3511852023-06-14 14:12:59 +08002137 sleep(2);
developer17038e62023-03-02 14:43:43 +08002138
developer33f13ba2023-07-12 16:19:06 +08002139 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +08002140 sleep(5);
developer17038e62023-03-02 14:43:43 +08002141
developerd14dff12023-06-28 22:47:44 +08002142 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08002143 wifi_psk_file_reset();
developer4fe22c62023-08-03 13:23:58 +08002144 wifi_BringUpInterfaces();
developera3511852023-06-14 14:12:59 +08002145 sleep(2);
developer8a3bbbf2023-03-15 17:47:23 +08002146
2147 wifi_vap_status_reset();
2148
developera3511852023-06-14 14:12:59 +08002149 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002150}
2151
2152/* wifi_down() function */
2153/**
2154* @description Turns off transmit power for the entire Wifi subsystem, for all radios.
developer69b61b02023-03-07 17:17:44 +08002155* Implementation specifics may dictate some functionality since
developer72fb0bb2023-01-11 09:46:29 +08002156* different hardware implementations may have different requirements.
2157*
2158* @param None
2159*
2160* @return The status of the operation
2161* @retval RETURN_OK if successful
2162* @retval RETURN_ERR if any error is detected
2163*
2164* @execution Synchronous
2165* @sideeffect None
2166*
2167* @note This function must not suspend and must not invoke any blocking system
2168* calls. It should probably just send a message to a driver event handler task.
2169*
2170*/
2171INT wifi_down()
2172{
developera3511852023-06-14 14:12:59 +08002173 //TODO: turns off transmit power for the entire Wifi subsystem, for all radios
2174 int max_num_radios = 0;
developerb2977562023-05-24 17:54:12 +08002175 wifi_getMaxRadioNumber(&max_num_radios);
developer17038e62023-03-02 14:43:43 +08002176
developerb2977562023-05-24 17:54:12 +08002177 for (int radioIndex = 0; radioIndex < max_num_radios; radioIndex++)
2178 wifi_setRadioEnable(radioIndex, FALSE);
2179
developera3511852023-06-14 14:12:59 +08002180 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002181}
2182
2183
2184/* wifi_createInitialConfigFiles() function */
2185/**
2186* @description This function creates wifi configuration files. The format
developer69b61b02023-03-07 17:17:44 +08002187* and content of these files are implementation dependent. This function call is
2188* used to trigger this task if necessary. Some implementations may not need this
2189* function. If an implementation does not need to create config files the function call can
developer72fb0bb2023-01-11 09:46:29 +08002190* do nothing and return RETURN_OK.
2191*
2192* @param 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_createInitialConfigFiles()
2206{
developera3511852023-06-14 14:12:59 +08002207 //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)
2208 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002209}
2210
developer7e4a2a62023-04-06 19:56:03 +08002211/* outputs the country code to a max 64 character string */
developer72fb0bb2023-01-11 09:46:29 +08002212INT wifi_getRadioCountryCode(INT radioIndex, CHAR *output_string)
2213{
developera3511852023-06-14 14:12:59 +08002214 int ret;
developer72fb0bb2023-01-11 09:46:29 +08002215
developera3511852023-06-14 14:12:59 +08002216 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002217
developera3511852023-06-14 14:12:59 +08002218 ret = wifi_BandProfileRead(0, radioIndex, "CountryCode", output_string, 64, NULL);
2219 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08002220 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead CountryCode failed\n");
developera3511852023-06-14 14:12:59 +08002221 return RETURN_ERR;
2222 }
developer7e4a2a62023-04-06 19:56:03 +08002223
developera3511852023-06-14 14:12:59 +08002224 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002225
developera3511852023-06-14 14:12:59 +08002226 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002227}
2228
2229INT wifi_setRadioCountryCode(INT radioIndex, CHAR *CountryCode)
2230{
developer7e4a2a62023-04-06 19:56:03 +08002231 /*Set wifi config. Wait for wifi reset to apply*/
developer7e4a2a62023-04-06 19:56:03 +08002232 struct params params;
2233 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002234 int ret = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002235
developer7e4a2a62023-04-06 19:56:03 +08002236 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002237
developer7e4a2a62023-04-06 19:56:03 +08002238 if(NULL == CountryCode || strlen(CountryCode) >= 32 ) {
2239 printf("%s: input para error!!!\n", __func__);
2240 return RETURN_ERR;
2241 }
developer72fb0bb2023-01-11 09:46:29 +08002242
developerc79e9172023-06-06 19:48:03 +08002243 if (!strlen(CountryCode)) {
2244 memcpy(CountryCode, "US", strlen("US")); /*default set the code to US*/
2245 CountryCode[2] = '\0';
2246 }
developer72fb0bb2023-01-11 09:46:29 +08002247
developer7e4a2a62023-04-06 19:56:03 +08002248 params.name = "country_code";
2249 params.value = CountryCode;
developer72fb0bb2023-01-11 09:46:29 +08002250
developere40952c2023-06-15 18:46:43 +08002251 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, radioIndex);
2252 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
2253 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2254 return RETURN_ERR;
2255 }
developer7e4a2a62023-04-06 19:56:03 +08002256 ret = wifi_hostapdWrite(config_file, &params, 1);
2257
2258 if (ret) {
2259 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n",
2260 __func__, ret);
2261 }
2262
2263 ret = wifi_hostapdProcessUpdate(radioIndex, &params, 1);
2264
2265 if (ret) {
2266 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n",
2267 __func__, ret);
2268 }
2269
2270 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
2271
2272 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002273}
2274
2275INT wifi_getRadioChannelStats2(INT radioIndex, wifi_channelStats2_t *outputChannelStats2)
2276{
developera3511852023-06-14 14:12:59 +08002277 char interface_name[16] = {0};
2278 char channel_util_file[64] = {0};
2279 char cmd[128] = {0};
2280 char buf[128] = {0};
2281 char *line = NULL;
2282 char *param = NULL, *value = NULL;
developere40952c2023-06-15 18:46:43 +08002283 int read = 0, res;
developera3511852023-06-14 14:12:59 +08002284 unsigned int ActiveTime = 0, BusyTime = 0, TransmitTime = 0;
developer86035662023-06-28 19:21:12 +08002285 unsigned long preActiveTime = 0, preBusyTime = 0, preTransmitTime = 0;
developerc14d83a2023-06-29 20:09:42 +08002286 long int rssi;
2287 long int tmp_l;
2288 unsigned long tmp_ul;
developera3511852023-06-14 14:12:59 +08002289 size_t len = 0;
2290 FILE *f = NULL;
developer72fb0bb2023-01-11 09:46:29 +08002291
developera3511852023-06-14 14:12:59 +08002292 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002293
developera3511852023-06-14 14:12:59 +08002294 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2295 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +08002296
2297 res = _syscmd_secure(buf, sizeof(buf), "iw %s scan | grep signal | awk '{print $2}' | sort -n | tail -n1", interface_name);
2298 if (res) {
2299 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002300 }
2301
developerc14d83a2023-06-29 20:09:42 +08002302 if (hal_strtol(buf, 10, &rssi) < 0) {
2303 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2304 }
2305 outputChannelStats2->ch_Max80211Rssi = rssi;
developer72fb0bb2023-01-11 09:46:29 +08002306
developera3511852023-06-14 14:12:59 +08002307 memset(cmd, 0, sizeof(cmd));
2308 memset(buf, 0, sizeof(buf));
developere40952c2023-06-15 18:46:43 +08002309 res = snprintf(cmd, sizeof(cmd), "iw %s survey dump | grep 'in use' -A6", interface_name);
2310 if (os_snprintf_error(sizeof(cmd), res)) {
2311 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2312 return RETURN_ERR;
2313 }
developer8078acf2023-08-04 18:52:48 +08002314 f = v_secure_popen("r","iw %s survey dump | grep 'in use' -A6", interface_name);
2315 if (f == NULL) {
2316 wifi_dbg_printf("%s: v_secure_popen %s error\n", __func__, cmd);
developera3511852023-06-14 14:12:59 +08002317 return RETURN_ERR;
2318 }
developer72fb0bb2023-01-11 09:46:29 +08002319
developera3511852023-06-14 14:12:59 +08002320 read = getline(&line, &len, f);
2321 while (read != -1) {
2322 param = strtok(line, ":\t");
developerc14d83a2023-06-29 20:09:42 +08002323 if (!param) {
2324 read = getline(&line, &len, f);
2325 continue;
2326 }
developera3511852023-06-14 14:12:59 +08002327 value = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +08002328 if (!value) {
developer37646972023-06-29 10:58:43 +08002329 read = getline(&line, &len, f);
2330 continue;
2331 }
developera3511852023-06-14 14:12:59 +08002332 if(strstr(param, "frequency") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002333 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2334 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2335 }
2336 outputChannelStats2->ch_Frequency = tmp_ul;
developera3511852023-06-14 14:12:59 +08002337 }
2338 if(strstr(param, "noise") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002339 if (hal_strtol(value, 10, &tmp_l) < 0) {
2340 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2341 }
2342 outputChannelStats2->ch_NoiseFloor = tmp_l;
developer37646972023-06-29 10:58:43 +08002343
developerc14d83a2023-06-29 20:09:42 +08002344 if (hal_strtol(value, 10, &tmp_l) < 0) {
2345 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2346 }
2347 outputChannelStats2->ch_Non80211Noise = tmp_l;
developera3511852023-06-14 14:12:59 +08002348 }
2349 if(strstr(param, "channel active time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002350 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2351 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2352 }
2353 ActiveTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002354 }
developer5b23cd02023-07-19 20:26:03 +08002355 if(strstr(param, "channel busy time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002356 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2357 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2358 }
2359 BusyTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002360 }
2361 if(strstr(param, "channel transmit time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002362 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2363 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2364 }
2365 TransmitTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002366 }
2367 read = getline(&line, &len, f);
2368 }
developer8078acf2023-08-04 18:52:48 +08002369 v_secure_pclose(f);
developer72fb0bb2023-01-11 09:46:29 +08002370
developera3511852023-06-14 14:12:59 +08002371 // The file should store the last active, busy and transmit time
developere40952c2023-06-15 18:46:43 +08002372 res = snprintf(channel_util_file, sizeof(channel_util_file), "%s%d.txt", CHANNEL_STATS_FILE, radioIndex);
2373 if (os_snprintf_error(sizeof(channel_util_file), res)) {
2374 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2375 return RETURN_ERR;
2376 }
2377
developera3511852023-06-14 14:12:59 +08002378 f = fopen(channel_util_file, "r");
2379 if (f != NULL) {
2380 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002381 if (hal_strtoul(line, 10, &preActiveTime) < 0) {
2382 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002383 }
developera3511852023-06-14 14:12:59 +08002384 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002385 if (hal_strtoul(line, 10, &preBusyTime) < 0) {
2386 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerb6f05602023-06-30 11:32:41 +08002387
developer86035662023-06-28 19:21:12 +08002388 }
developera3511852023-06-14 14:12:59 +08002389 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002390 if (hal_strtoul(line, 10, &preTransmitTime) < 0) {
2391 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002392 }
developer5b23cd02023-07-19 20:26:03 +08002393
developerc14d83a2023-06-29 20:09:42 +08002394 if (fclose(f) == EOF)
2395 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developer5b23cd02023-07-19 20:26:03 +08002396
developerc14d83a2023-06-29 20:09:42 +08002397 }
2398
developerb14b3462023-07-01 18:02:42 +08002399 /*if (ActiveTime == preActiveTime) {
developer5b23cd02023-07-19 20:26:03 +08002400 wifi_debug(DEBUG_ERROR, "error:ActiveTime[%d] == preActiveTime[%ld]\n",
developerb14b3462023-07-01 18:02:42 +08002401 ActiveTime, preActiveTime);
developerc14d83a2023-06-29 20:09:42 +08002402 return RETURN_ERR;
developerb14b3462023-07-01 18:02:42 +08002403 }*/
developer72fb0bb2023-01-11 09:46:29 +08002404
developera3511852023-06-14 14:12:59 +08002405 outputChannelStats2->ch_ObssUtil = (BusyTime - preBusyTime)*100/(ActiveTime - preActiveTime);
2406 outputChannelStats2->ch_SelfBssUtil = (TransmitTime - preTransmitTime)*100/(ActiveTime - preActiveTime);
developer72fb0bb2023-01-11 09:46:29 +08002407
developera3511852023-06-14 14:12:59 +08002408 f = fopen(channel_util_file, "w");
2409 if (f != NULL) {
developer86035662023-06-28 19:21:12 +08002410 if (fprintf(f, "%u\n%u\n%u\n", ActiveTime, BusyTime, TransmitTime) < 0) {
2411 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
2412 }
2413 if (fclose(f) != 0) {
2414 wifi_debug(DEBUG_ERROR, "fclose fail\n");
2415 }
developera3511852023-06-14 14:12:59 +08002416 }
2417 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2418 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002419}
2420
2421/**********************************************************************************
2422 *
2423 * Wifi radio level function prototypes
2424 *
2425**********************************************************************************/
2426
2427//Get the total number of radios in this wifi subsystem
2428INT wifi_getRadioNumberOfEntries(ULONG *output) //Tr181
2429{
developera3511852023-06-14 14:12:59 +08002430 if (NULL == output)
2431 return RETURN_ERR;
2432 *output = MAX_NUM_RADIOS;
developer72fb0bb2023-01-11 09:46:29 +08002433
developera3511852023-06-14 14:12:59 +08002434 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002435}
2436
developer69b61b02023-03-07 17:17:44 +08002437//Get the total number of SSID entries in this wifi subsystem
developer72fb0bb2023-01-11 09:46:29 +08002438INT wifi_getSSIDNumberOfEntries(ULONG *output) //Tr181
2439{
developera3511852023-06-14 14:12:59 +08002440 if (NULL == output)
2441 return RETURN_ERR;
2442 *output = MAX_APS;
developer72fb0bb2023-01-11 09:46:29 +08002443
developera3511852023-06-14 14:12:59 +08002444 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002445}
2446
2447//Get the Radio enable config parameter
developera3511852023-06-14 14:12:59 +08002448INT wifi_getRadioEnable(INT radioIndex, BOOL *output_bool) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002449{
developer56fbedb2023-05-30 16:47:05 +08002450 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +08002451 char buf[128] = {0};
developer56fbedb2023-05-30 16:47:05 +08002452 int apIndex;
2453 int max_radio_num = 0;
developer3a85ab82023-05-25 11:59:38 +08002454
developer56fbedb2023-05-30 16:47:05 +08002455 if (NULL == output_bool)
2456 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002457
developer56fbedb2023-05-30 16:47:05 +08002458 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08002459
developer56fbedb2023-05-30 16:47:05 +08002460 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002461
developer56fbedb2023-05-30 16:47:05 +08002462 if (radioIndex >= max_radio_num)
2463 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002464
developer56fbedb2023-05-30 16:47:05 +08002465 /* loop all interface in radio, if any is enable, reture true, else return false */
2466 for(apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num)
2467 {
2468 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2469 continue;
developer8078acf2023-08-04 18:52:48 +08002470
developer8078acf2023-08-04 18:52:48 +08002471 *output_bool = _syscmd_secure(buf, sizeof(buf), "ifconfig %s 2> /dev/null | grep UP", interface_name) ? FALSE : TRUE;
developer56fbedb2023-05-30 16:47:05 +08002472 if (*output_bool == TRUE)
2473 break;
developer8078acf2023-08-04 18:52:48 +08002474
developer56fbedb2023-05-30 16:47:05 +08002475 }
2476
2477 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002478}
2479
developere82c0ca2023-05-10 16:25:35 +08002480typedef long time_t;
2481static time_t radio_up_time[MAX_NUM_RADIOS];
2482
developer72fb0bb2023-01-11 09:46:29 +08002483INT wifi_setRadioEnable(INT radioIndex, BOOL enable)
2484{
developera3511852023-06-14 14:12:59 +08002485 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +08002486 char buf[MAX_BUF_SIZE] = {0};
2487 int apIndex;
2488 int max_radio_num = 0;
developere40952c2023-06-15 18:46:43 +08002489 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002490
developera3511852023-06-14 14:12:59 +08002491 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002492
developera3511852023-06-14 14:12:59 +08002493 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08002494
developera3511852023-06-14 14:12:59 +08002495 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002496
developera3511852023-06-14 14:12:59 +08002497 if(enable == FALSE) {
developer47cc27a2023-05-17 23:09:58 +08002498
2499 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2500 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002501
developer8078acf2023-08-04 18:52:48 +08002502
2503 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw REMOVE %s", interface_name);
2504 if (res) {
2505 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002506 }
developer8078acf2023-08-04 18:52:48 +08002507 memset(buf, 0, MAX_BUF_SIZE);
developer47cc27a2023-05-17 23:09:58 +08002508
developer8078acf2023-08-04 18:52:48 +08002509 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s down", interface_name);
2510 if (res) {
2511 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002512 }
developer8078acf2023-08-04 18:52:48 +08002513
developere82c0ca2023-05-10 16:25:35 +08002514 if(strncmp(buf, "OK", 2))
developer75bd10c2023-06-27 11:34:08 +08002515 wifi_debug(DEBUG_ERROR, "Could not detach %s from hostapd daemon", interface_name);
developer8a3bbbf2023-03-15 17:47:23 +08002516 } else {
developere82c0ca2023-05-10 16:25:35 +08002517 for (apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num) {
developera3511852023-06-14 14:12:59 +08002518 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2519 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002520
developer8a3bbbf2023-03-15 17:47:23 +08002521 memset(buf, 0, MAX_BUF_SIZE);
2522
developer8078acf2023-08-04 18:52:48 +08002523
2524 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
2525 if (res) {
2526 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002527 }
developer8a3bbbf2023-03-15 17:47:23 +08002528
developera3511852023-06-14 14:12:59 +08002529 if(*buf == '1') {
developer8078acf2023-08-04 18:52:48 +08002530 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s up", interface_name);
2531 if (res) {
2532 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002533 }
developer8a3bbbf2023-03-15 17:47:23 +08002534
developer8a3bbbf2023-03-15 17:47:23 +08002535 memset(buf, 0, MAX_BUF_SIZE);
2536
developer8078acf2023-08-04 18:52:48 +08002537 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw ADD bss_config=phy%d:/nvram/hostapd%d.conf",phyId,apIndex);
2538 if (res) {
2539 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08002540 }
developera3511852023-06-14 14:12:59 +08002541 }
2542 }
developere75ba632023-06-29 16:03:33 +08002543 if (time(&radio_up_time[radioIndex]) < 0) {
2544 wifi_debug(DEBUG_ERROR, "GET time fail\n");
2545 return RETURN_ERR;
2546 }
developera3511852023-06-14 14:12:59 +08002547 }
developer72fb0bb2023-01-11 09:46:29 +08002548
developera3511852023-06-14 14:12:59 +08002549 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2550 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002551}
2552
2553//Get the Radio enable status
2554INT wifi_getRadioStatus(INT radioIndex, BOOL *output_bool) //RDKB
2555{
developera3511852023-06-14 14:12:59 +08002556 if (NULL == output_bool)
2557 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002558
developera3511852023-06-14 14:12:59 +08002559 return wifi_getRadioEnable(radioIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08002560}
2561
2562//Get the Radio Interface name from platform, eg "wlan0"
2563INT wifi_getRadioIfName(INT radioIndex, CHAR *output_string) //Tr181
2564{
developera3511852023-06-14 14:12:59 +08002565 if (NULL == output_string || radioIndex>=MAX_NUM_RADIOS || radioIndex<0)
2566 return RETURN_ERR;
2567 return wifi_GetInterfaceName(radioIndex, output_string);
developer72fb0bb2023-01-11 09:46:29 +08002568}
2569
developer6e578302023-06-21 10:11:16 +08002570int mtk_get_vow_info_callback(struct nl_msg *msg, void *data)
2571{
2572 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2573 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX + 1];
2574 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2575 int err = 0;
2576 struct vow_info *vow_info = NULL;
2577 struct mtk_nl80211_cb_data *cb_data = data;
2578
2579 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2580 genlmsg_attrlen(gnlh, 0), NULL);
2581 if (err < 0) {
2582 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
2583 return err;
2584 }
2585
2586 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2587 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX,
2588 tb[NL80211_ATTR_VENDOR_DATA], NULL);
2589 if (err < 0){
2590 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX fails\n");
2591 return err;
2592 }
2593
2594 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]) {
2595 vow_info = (struct vow_info *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]);
2596 memmove(cb_data->out_buf, vow_info, sizeof(struct vow_info));
2597 }
2598 }
2599
2600 return 0;
2601}
2602
2603INT mtk_wifi_set_air_time_management(
2604 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
2605 char* data, INT len, void *output)
2606{
2607 char inf_name[IF_NAME_SIZE] = {0};
2608 unsigned int if_idx = 0;
2609 int ret = -1;
2610 struct unl unl_ins;
2611 struct nl_msg *msg = NULL;
2612 struct nlattr * msg_data = NULL;
2613 struct mtk_nl80211_param param;
2614
2615 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
2616 return RETURN_ERR;
2617 if_idx = if_nametoindex(inf_name);
2618 if (!if_idx) {
2619 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
2620 return RETURN_ERR;
2621 }
2622 /*init mtk nl80211 vendor cmd*/
2623 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_VOW;
2624 param.if_type = NL80211_ATTR_IFINDEX;
2625 param.if_idx = if_idx;
2626
2627 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
2628 if (ret) {
2629 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
2630 return RETURN_ERR;
2631 }
2632 /*add mtk vendor cmd data*/
2633 if (nla_put(msg, vendor_data_attr, len, data)) {
2634 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
2635 nlmsg_free(msg);
2636 goto err;
2637 }
2638
2639 /*send mtk nl80211 vendor msg*/
2640 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output);
2641 if (ret) {
2642 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
2643 goto err;
2644 }
2645 /*deinit mtk nl80211 vendor msg*/
2646 mtk_nl80211_deint(&unl_ins);
2647 wifi_debug(DEBUG_INFO, "send cmd success.\n");
2648
2649 return RETURN_OK;
2650err:
2651 mtk_nl80211_deint(&unl_ins);
2652 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
2653 return RETURN_ERR;
2654}
2655
2656//Get the ATM(Air Time Management) Capable.
2657INT wifi_getATMCapable(BOOL *output_bool)
2658{
2659 if (NULL == output_bool)
2660 return RETURN_ERR;
2661 *output_bool = TRUE;
2662
2663 return RETURN_OK;
2664}
2665
2666INT wifi_setATMEnable(BOOL enable)
2667{
2668 int max_radio_num = 0;
2669 int radio_idx = 0;
2670 char dat_file[MAX_BUF_SIZE] = {0};
2671 int res;
2672 struct params params[2];
2673
2674 wifi_getMaxRadioNumber(&max_radio_num);
2675 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2676 if (mtk_wifi_set_air_time_management
2677 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO,
2678 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2679 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2680 return RETURN_ERR;
2681 }
2682
2683 if (mtk_wifi_set_air_time_management
2684 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_BW_EN_INFO,
2685 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2686 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2687 return RETURN_ERR;
2688 }
2689
2690 params[0].name = "VOW_Airtime_Fairness_En";
2691 params[0].value = enable ? "1" : "0";
2692 params[1].name = "VOW_BW_Ctrl";
2693 params[1].value = enable ? "1" : "0";
2694
2695 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
2696 if (os_snprintf_error(sizeof(dat_file), res)) {
2697 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2698 return RETURN_ERR;
2699 }
2700 wifi_datfileWrite(dat_file, params, 2);
2701 }
2702
2703 return RETURN_OK;
2704}
2705
2706INT wifi_getATMEnable(BOOL *output_enable)
2707{
2708 int max_radio_num = 0;
2709 int radio_idx = 0;
2710 struct vow_info vow_info;
2711 struct vow_info get_vow_info;
2712 struct mtk_nl80211_cb_data cb_data;
2713
2714 if (output_enable == NULL)
2715 return RETURN_ERR;
2716
2717 wifi_getMaxRadioNumber(&max_radio_num);
2718
2719 *output_enable = FALSE;
2720
2721 memset(&vow_info, 0, sizeof(struct vow_info));
2722
2723 cb_data.out_buf = (char *)&vow_info;
2724 cb_data.out_len = sizeof(struct vow_info);
2725
2726 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2727 if (mtk_wifi_set_air_time_management
2728 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2729 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2730 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2731 return RETURN_ERR;
2732 }
2733
2734 if (vow_info.atf_en == TRUE || vow_info.bw_en == TRUE) {
2735 *output_enable = TRUE;
2736 break;
2737 }
2738 }
2739
2740 return RETURN_OK;
2741}
2742
2743UINT apidx_to_group(INT apIndex)
2744{
2745 int max_radio_num = 0;
2746 unsigned int group = 0;
2747
2748 wifi_getMaxRadioNumber(&max_radio_num);
developerc14d83a2023-06-29 20:09:42 +08002749 if (max_radio_num == 0) {
2750 wifi_debug(DEBUG_ERROR, "invalid max radio num\n");
2751 return 0;
2752 }
developer6e578302023-06-21 10:11:16 +08002753 group = apIndex / max_radio_num + 5 * (apIndex % max_radio_num);
2754
2755 return group;
2756}
2757
2758INT wifi_setApATMAirTimePercent(INT apIndex, UINT ap_AirTimePercent)
2759{
2760 struct vow_group_en_param atc_en_param;
2761 struct vow_ratio_param radio_param;
2762 unsigned int group = 0;
2763 //BOOL ATM_enable = FALSE;
2764
2765 if (ap_AirTimePercent < 5 || ap_AirTimePercent > 100) {
2766 wifi_debug(DEBUG_ERROR, "invalid ait time percent!\n");
2767 return RETURN_ERR;
2768 }
2769
2770 /* mt7990 support 15 group now*/
2771 group = apidx_to_group(apIndex);
2772
2773 if (group > 15) {
2774 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2775 return RETURN_ERR;
2776 }
2777
2778 atc_en_param.group = group;
2779 atc_en_param.en = 1;
2780 if (mtk_wifi_set_air_time_management
2781 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO,
2782 NULL, (char *)&atc_en_param, sizeof(struct vow_group_en_param), NULL)!= RETURN_OK) {
2783 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO cmd fails\n");
2784 return RETURN_ERR;
2785 }
2786
2787 radio_param.group = group;
2788 radio_param.ratio = ap_AirTimePercent;
2789 if (mtk_wifi_set_air_time_management
2790 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO,
2791 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2792 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO cmd fails\n");
2793 return RETURN_ERR;
2794 }
2795
2796 if (mtk_wifi_set_air_time_management
2797 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO,
2798 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2799 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO cmd fails\n");
2800 return RETURN_ERR;
2801 }
2802
2803 return RETURN_OK;
2804}
2805
2806INT wifi_getApATMAirTimePercent(INT apIndex, UINT *output_ap_AirTimePercent)
2807{
2808 unsigned int group = 0;
2809 struct vow_info get_vow_info, vow_info;
2810 struct mtk_nl80211_cb_data cb_data;
2811
2812 if (output_ap_AirTimePercent == NULL)
2813 return RETURN_ERR;
2814
2815 group = apidx_to_group(apIndex);
2816 if (group > 15) {
2817 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2818 return RETURN_ERR;
2819 }
2820
2821 memset(&vow_info, 0, sizeof(struct vow_info));
2822 memset(&get_vow_info, 0, sizeof(struct vow_info));
2823
2824 cb_data.out_buf = (char *)&vow_info;
2825 cb_data.out_len = sizeof(struct vow_info);
2826
2827 get_vow_info.group = group;
2828
2829 if (mtk_wifi_set_air_time_management
2830 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2831 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2832 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2833 return RETURN_ERR;
2834 }
2835
2836 *output_ap_AirTimePercent = vow_info.ratio;
2837
2838 return RETURN_ERR;
2839}
2840
developerd14dff12023-06-28 22:47:44 +08002841INT wifi_getApATMSta(INT apIndex, UCHAR *output_sta_MAC_ATM_array, UINT buf_size)
2842{
2843 ULONG dev_num = 0;
2844 struct vow_info vow_info;
2845 struct vow_info get_vow_info;
2846 struct mtk_nl80211_cb_data cb_data;
2847 unsigned int percent;
2848 char assocArray[MAX_BUF_SIZE] = {0};
2849 char *mac = NULL;
2850 unsigned char output_len = 0;
2851 int res;
2852 char buf[MAX_BUF_SIZE] = {0};
2853
2854 memset(&vow_info, 0, sizeof(struct vow_info));
2855 memset(&get_vow_info, 0, sizeof(struct vow_info));
2856
2857 cb_data.out_buf = (char *)&vow_info;
2858 cb_data.out_len = sizeof(struct vow_info);
2859
2860 if (mtk_wifi_set_air_time_management
2861 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2862 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2863 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2864 return RETURN_ERR;
2865 }
2866
2867 if (vow_info.atf_en == FALSE) {
2868 wifi_debug(DEBUG_ERROR, "ATF disable!\n");
2869 return RETURN_ERR;
2870 }
2871
2872 if (wifi_getApNumDevicesAssociated(apIndex, &dev_num) != RETURN_OK) {
2873 wifi_debug(DEBUG_ERROR, "get sta num fail!\n");
2874 return RETURN_ERR;;
2875 }
2876
2877 percent = 100 / dev_num;
2878
2879 if (wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray)) != RETURN_OK){
2880 wifi_debug(DEBUG_ERROR, "get sta mac fail!\n");
2881 return RETURN_ERR;;
2882 }
2883
2884 memset(output_sta_MAC_ATM_array, 0, MAX_BUF_SIZE);
2885
2886 mac = strtok(assocArray, "\n");
2887 while (mac != NULL) {
2888 if (strlen(mac) >= 17) {
2889 res = snprintf(buf, sizeof(buf), "%s %d|", mac, percent);
2890
2891 if (os_snprintf_error(sizeof(buf), res)) {
2892 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2893 return RETURN_ERR;
2894 }
2895
2896 if (output_len + strlen(buf) > buf_size)
2897 break;
2898
2899 strncat((char *)output_sta_MAC_ATM_array, buf, strlen(buf));
2900
2901 output_len += strlen(buf);
2902 }
2903
2904 mac = strtok(NULL, "\n");
2905 }
2906
2907 /* Remove the last | */
2908 if (strlen((char *)output_sta_MAC_ATM_array) != 0)
2909 output_sta_MAC_ATM_array[strlen((char *)output_sta_MAC_ATM_array)-1] = '\0';
2910
2911 return RETURN_OK;
2912}
2913
2914INT wifi_setApATMSta(INT apIndex, UCHAR *sta_MAC_ATM_array, UINT ap_AirTimePercent)
2915{
2916 return RETURN_ERR;
2917}
developer9ce44382023-06-28 11:09:37 +08002918
developer72fb0bb2023-01-11 09:46:29 +08002919//Get the maximum PHY bit rate supported by this interface. eg: "216.7 Mb/s", "1.3 Gb/s"
2920//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.
2921INT wifi_getRadioMaxBitRate(INT radioIndex, CHAR *output_string) //RDKB
2922{
developera3511852023-06-14 14:12:59 +08002923 // The formula to coculate bit rate is "Subcarriers * Modulation * Coding rate * Spatial stream / (Data interval + Guard interval)"
2924 // For max bit rate, we should always choose the best MCS
2925 char mode[64] = {0};
2926 char channel_bandwidth_str[64] = {0};
2927 UINT mode_map = 0;
2928 UINT num_subcarrier = 0;
2929 UINT code_bits = 0;
2930 float code_rate = 0; // use max code rate
2931 int NSS = 0;
2932 UINT Symbol_duration = 0;
2933 UINT GI_duration = 0;
2934 wifi_guard_interval_t gi = wifi_guard_interval_auto;
2935 BOOL enable = FALSE;
2936 float bit_rate = 0;
developere40952c2023-06-15 18:46:43 +08002937 int ant_bitmap = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002938
developera3511852023-06-14 14:12:59 +08002939 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
2940 if (NULL == output_string)
2941 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002942
developera3511852023-06-14 14:12:59 +08002943 wifi_getRadioEnable(radioIndex, &enable);
2944 if (enable == FALSE) {
developere40952c2023-06-15 18:46:43 +08002945 res = snprintf(output_string, 64, "0 Mb/s");
2946 if (os_snprintf_error(64, res)) {
2947 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2948 return RETURN_ERR;
2949 }
developera3511852023-06-14 14:12:59 +08002950 return RETURN_OK;
2951 }
developer72fb0bb2023-01-11 09:46:29 +08002952
developera3511852023-06-14 14:12:59 +08002953 if (wifi_getRadioMode(radioIndex, mode, &mode_map) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08002954 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +08002955 return RETURN_ERR;
2956 }
developer72fb0bb2023-01-11 09:46:29 +08002957
developera3511852023-06-14 14:12:59 +08002958 if (wifi_getGuardInterval(radioIndex, &gi) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08002959 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +08002960 return RETURN_ERR;
2961 }
developer72fb0bb2023-01-11 09:46:29 +08002962
developera3511852023-06-14 14:12:59 +08002963 if (gi == wifi_guard_interval_3200)
2964 GI_duration = 32;
2965 else if (gi == wifi_guard_interval_1600)
2966 GI_duration = 16;
2967 else if (gi == wifi_guard_interval_800)
2968 GI_duration = 8;
2969 else // auto, 400
2970 GI_duration = 4;
developer72fb0bb2023-01-11 09:46:29 +08002971
developera3511852023-06-14 14:12:59 +08002972 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, channel_bandwidth_str) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08002973 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error\n");
developera3511852023-06-14 14:12:59 +08002974 return RETURN_ERR;
2975 }
developer72fb0bb2023-01-11 09:46:29 +08002976
developera3511852023-06-14 14:12:59 +08002977 if (strstr(channel_bandwidth_str, "80+80") != NULL)
developer32f2a182023-06-27 19:50:41 +08002978 memcpy(channel_bandwidth_str, "160", strlen("160"));
developer72fb0bb2023-01-11 09:46:29 +08002979
developera3511852023-06-14 14:12:59 +08002980 if (mode_map & WIFI_MODE_AX) {
2981 if (strstr(channel_bandwidth_str, "160") != NULL)
2982 num_subcarrier = 1960;
2983 else if (strstr(channel_bandwidth_str, "80") != NULL)
2984 num_subcarrier = 980;
2985 else if (strstr(channel_bandwidth_str, "40") != NULL)
2986 num_subcarrier = 468;
2987 else if (strstr(channel_bandwidth_str, "20") != NULL)
2988 num_subcarrier = 234;
2989 code_bits = 10;
2990 code_rate = (float)5/6;
2991 Symbol_duration = 128;
2992 GI_duration = 8;/*HE no GI 400ns*/
2993 } else if (mode_map & WIFI_MODE_AC) {
2994 if (strstr(channel_bandwidth_str, "160") != NULL)
2995 num_subcarrier = 468;
2996 else if (strstr(channel_bandwidth_str, "80") != NULL)
2997 num_subcarrier = 234;
2998 else if (strstr(channel_bandwidth_str, "40") != NULL)
2999 num_subcarrier = 108;
3000 else if (strstr(channel_bandwidth_str, "20") != NULL)
3001 num_subcarrier = 52;
3002 code_bits = 8;
3003 code_rate = (float)5/6;
3004 Symbol_duration = 32;
3005 } else if (mode_map & WIFI_MODE_N) {
3006 if (strstr(channel_bandwidth_str, "160") != NULL)
3007 num_subcarrier = 468;
3008 else if (strstr(channel_bandwidth_str, "80") != NULL)
3009 num_subcarrier = 234;
3010 else if (strstr(channel_bandwidth_str, "40") != NULL)
3011 num_subcarrier = 108;
3012 else if (strstr(channel_bandwidth_str, "20") != NULL)
3013 num_subcarrier = 52;
3014 code_bits = 6;
3015 code_rate = (float)3/4;
3016 Symbol_duration = 32;
3017 } else if ((mode_map & WIFI_MODE_G || mode_map & WIFI_MODE_B) || mode_map & WIFI_MODE_A) {
3018 // mode b must run with mode g, so we output mode g bitrate in 2.4 G.
developere40952c2023-06-15 18:46:43 +08003019 res = snprintf(output_string, 64, "65 Mb/s");
3020 if (os_snprintf_error(64, res)) {
3021 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3022 return RETURN_ERR;
3023 }
developera3511852023-06-14 14:12:59 +08003024 return RETURN_OK;
3025 } else {
developere40952c2023-06-15 18:46:43 +08003026 res = snprintf(output_string, 64, "0 Mb/s");
3027 if (os_snprintf_error(64, res)) {
3028 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3029 return RETURN_ERR;
3030 }
developera3511852023-06-14 14:12:59 +08003031 return RETURN_OK;
3032 }
developer72fb0bb2023-01-11 09:46:29 +08003033
developera3511852023-06-14 14:12:59 +08003034 // Spatial streams
3035 if (wifi_getRadioTxChainMask(radioIndex, &ant_bitmap) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08003036 wifi_debug(DEBUG_ERROR, "wifi_getRadioTxChainMask return error\n");
developera3511852023-06-14 14:12:59 +08003037 return RETURN_ERR;
3038 }
3039 for (; ant_bitmap > 0; ant_bitmap >>= 1)
3040 NSS += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08003041
developera3511852023-06-14 14:12:59 +08003042 // multiple 10 is to align duration unit (0.1 us)
3043 bit_rate = (num_subcarrier * code_bits * code_rate * NSS) / (Symbol_duration + GI_duration) * 10;
developere40952c2023-06-15 18:46:43 +08003044 res = snprintf(output_string, 64, "%.1f Mb/s", bit_rate);
3045 if (os_snprintf_error(64, res)) {
3046 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3047 return RETURN_ERR;
3048 }
developera3511852023-06-14 14:12:59 +08003049 WIFI_ENTRY_EXIT_DEBUG("%s:num_subcarrier=%d, code_bits=%d, code_rate=%.3f, nss=%d, symbol time=%u, %.1f Mb/s\n",
3050 __func__, num_subcarrier, code_bits, code_rate, NSS, Symbol_duration + GI_duration, bit_rate);
3051 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003052
developera3511852023-06-14 14:12:59 +08003053 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003054}
developer72fb0bb2023-01-11 09:46:29 +08003055
3056//Get Supported frequency bands at which the radio can operate. eg: "2.4GHz,5GHz"
3057//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.
3058INT wifi_getRadioSupportedFrequencyBands(INT radioIndex, CHAR *output_string) //RDKB
3059{
developera3511852023-06-14 14:12:59 +08003060 wifi_band band = band_invalid;
developer72fb0bb2023-01-11 09:46:29 +08003061
developera3511852023-06-14 14:12:59 +08003062 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3063 if (NULL == output_string)
3064 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003065
developera3511852023-06-14 14:12:59 +08003066 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003067
developera3511852023-06-14 14:12:59 +08003068 memset(output_string, 0, 10);
3069 if (band == band_2_4)
developer32f2a182023-06-27 19:50:41 +08003070 memcpy(output_string, "2.4GHz", strlen("2.4GHz"));
developera3511852023-06-14 14:12:59 +08003071 else if (band == band_5)
developer32f2a182023-06-27 19:50:41 +08003072 memcpy(output_string, "5GHz", strlen("5GHz"));
developera3511852023-06-14 14:12:59 +08003073 else if (band == band_6)
developer32f2a182023-06-27 19:50:41 +08003074 memcpy(output_string, "6GHz", strlen("6GHz"));
developera3511852023-06-14 14:12:59 +08003075 else
3076 return RETURN_ERR;
3077 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003078
developera3511852023-06-14 14:12:59 +08003079 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003080}
3081
3082//Get the frequency band at which the radio is operating, eg: "2.4GHz"
3083//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.
3084INT wifi_getRadioOperatingFrequencyBand(INT radioIndex, CHAR *output_string) //Tr181
3085{
developera3511852023-06-14 14:12:59 +08003086 wifi_band band = band_invalid;
developer9ce44382023-06-28 11:09:37 +08003087 int res = -1;
developere40952c2023-06-15 18:46:43 +08003088
developera3511852023-06-14 14:12:59 +08003089 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3090 if (NULL == output_string)
3091 return RETURN_ERR;
3092 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003093
developera3511852023-06-14 14:12:59 +08003094 if (band == band_2_4)
developere40952c2023-06-15 18:46:43 +08003095 res = snprintf(output_string, 64, "2.4GHz");
developera3511852023-06-14 14:12:59 +08003096 else if (band == band_5)
developere40952c2023-06-15 18:46:43 +08003097 res = snprintf(output_string, 64, "5GHz");
developera3511852023-06-14 14:12:59 +08003098 else if (band == band_6)
developere40952c2023-06-15 18:46:43 +08003099 res = snprintf(output_string, 64, "6GHz");
developer72fb0bb2023-01-11 09:46:29 +08003100
developere40952c2023-06-15 18:46:43 +08003101 if (os_snprintf_error(64, res)) {
3102 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3103 return RETURN_ERR;
3104 }
developera3511852023-06-14 14:12:59 +08003105 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003106
developera3511852023-06-14 14:12:59 +08003107 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003108}
3109
3110//Get the Supported Radio Mode. eg: "b,g,n"; "n,ac"
3111//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.
3112INT wifi_getRadioSupportedStandards(INT radioIndex, CHAR *output_string) //Tr181
3113{
developera3511852023-06-14 14:12:59 +08003114 char buf[128]={0};
3115 char temp_output[128] = {0};
3116 wifi_band band;
developere40952c2023-06-15 18:46:43 +08003117 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003118
developera3511852023-06-14 14:12:59 +08003119 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3120 if (NULL == output_string)
3121 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003122
developera3511852023-06-14 14:12:59 +08003123 band = wifi_index_to_band(radioIndex);
3124 if (band == band_2_4) {
developer32f2a182023-06-27 19:50:41 +08003125 strncat(temp_output, "b,g,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003126 } else if (band == band_5) {
developer32f2a182023-06-27 19:50:41 +08003127 strncat(temp_output, "a,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003128 }
3129 phyId = radio_index_to_phy(radioIndex);
3130 // ht capabilities
developer8078acf2023-08-04 18:52:48 +08003131
3132 res = _syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep '[^PHY|MAC|VHT].Capabilities' | head -n 1 | cut -d ':' -f2 | sed 's/^.//' | tr -d '\\n'", phyId);
3133 if (res) {
3134 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08003135 }
developer8078acf2023-08-04 18:52:48 +08003136
developera3511852023-06-14 14:12:59 +08003137 if (strlen(buf) >= 4 && strncmp(buf, "0x00", 4) != 0) {
developer32f2a182023-06-27 19:50:41 +08003138 if (strlen(temp_output) >= sizeof(temp_output) - 2)
3139 return RETURN_ERR;
3140 strncat(temp_output, "n,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003141 }
developer72fb0bb2023-01-11 09:46:29 +08003142
developera3511852023-06-14 14:12:59 +08003143 // vht capabilities
3144 if (band == band_5) {
developer8078acf2023-08-04 18:52:48 +08003145 res = _syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep 'VHT Capabilities' | cut -d '(' -f2 | cut -c1-10 | tr -d '\\n'", phyId);
3146 if (res) {
3147 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08003148 }
developere40952c2023-06-15 18:46:43 +08003149 if (strlen(buf) >= 10 && strncmp(buf, "0x00000000", 10) != 0) {
developer32f2a182023-06-27 19:50:41 +08003150 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3151 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08003152 strncat(temp_output, "ac,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003153 }
3154 }
developer72fb0bb2023-01-11 09:46:29 +08003155
developera3511852023-06-14 14:12:59 +08003156 // he capabilities
developer8078acf2023-08-04 18:52:48 +08003157 res = _syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep 'HE MAC Capabilities' | head -n 2 | tail -n 1 | cut -d '(' -f2 | cut -c1-6 | tr -d '\\n'", phyId);
3158 if (res) {
3159 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08003160 }
developer8078acf2023-08-04 18:52:48 +08003161
developera3511852023-06-14 14:12:59 +08003162 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003163 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3164 return RETURN_ERR;
3165 strncat(temp_output, "ax,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003166 }
developer72fb0bb2023-01-11 09:46:29 +08003167
developere82c0ca2023-05-10 16:25:35 +08003168 // eht capabilities
developer8078acf2023-08-04 18:52:48 +08003169 res = _syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep 'EHT MAC Capabilities' | head -n 2 | tail -n 1 | cut -d '(' -f2 | cut -c1-6 | tr -d '\\n'", phyId);
3170 if (res) {
3171 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08003172 }
developer8078acf2023-08-04 18:52:48 +08003173
developera3511852023-06-14 14:12:59 +08003174 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003175 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3176 return RETURN_ERR;
3177 strncat(temp_output, "be,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003178 }
developere82c0ca2023-05-10 16:25:35 +08003179
developera3511852023-06-14 14:12:59 +08003180 // Remove the last comma
3181 if (strlen(temp_output) != 0)
3182 temp_output[strlen(temp_output)-1] = '\0';
3183 strncpy(output_string, temp_output, strlen(temp_output));
3184 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3185 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003186}
3187
3188//Get the radio operating mode, and pure mode flag. eg: "ac"
3189//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.
3190INT wifi_getRadioStandard(INT radioIndex, CHAR *output_string, BOOL *gOnly, BOOL *nOnly, BOOL *acOnly) //RDKB
3191{
developere40952c2023-06-15 18:46:43 +08003192 int res;
3193
developera3511852023-06-14 14:12:59 +08003194 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3195 if (NULL == output_string)
3196 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003197
developera3511852023-06-14 14:12:59 +08003198 if (radioIndex == 0) {
developere40952c2023-06-15 18:46:43 +08003199 res = snprintf(output_string, 64, "n"); //"ht" needs to be translated to "n" or others
3200 if (os_snprintf_error(64, res)) {
3201 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3202 return RETURN_ERR;
3203 }
developera3511852023-06-14 14:12:59 +08003204 *gOnly = FALSE;
3205 *nOnly = TRUE;
3206 *acOnly = FALSE;
3207 } else {
developere40952c2023-06-15 18:46:43 +08003208 res = snprintf(output_string, 64, "ac"); //"vht" needs to be translated to "ac"
3209 if (os_snprintf_error(64, res)) {
3210 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3211 return RETURN_ERR;
3212 }
developera3511852023-06-14 14:12:59 +08003213 *gOnly = FALSE;
3214 *nOnly = FALSE;
3215 *acOnly = FALSE;
3216 }
3217 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003218
developera3511852023-06-14 14:12:59 +08003219 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003220}
3221
developer0f10c772023-05-16 21:43:39 +08003222enum WIFI_MODE {
3223 WMODE_INVALID = 0,
3224 WMODE_A = 1 << 0,
3225 WMODE_B = 1 << 1,
3226 WMODE_G = 1 << 2,
3227 WMODE_GN = 1 << 3,
3228 WMODE_AN = 1 << 4,
3229 WMODE_AC = 1 << 5,
3230 WMODE_AX_24G = 1 << 6,
3231 WMODE_AX_5G = 1 << 7,
3232 WMODE_AX_6G = 1 << 8,
3233 WMODE_BE_24G = 1 << 9,
3234 WMODE_BE_5G = 1 << 10,
3235 WMODE_BE_6G = 1 << 11,
3236 /*
3237 * total types of supported wireless mode,
3238 * add this value once yow add new type
3239 */
3240 WMODE_COMP = 12,
3241};
3242
3243#define RADIO_MODE_LEN 32
developerfead3972023-05-25 20:15:02 +08003244
3245int get_radio_mode_handler(struct nl_msg *msg, void *cb)
developer72fb0bb2023-01-11 09:46:29 +08003246{
developerfead3972023-05-25 20:15:02 +08003247 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3248 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08003249 struct genlmsghdr *gnlh;
developerfead3972023-05-25 20:15:02 +08003250 unsigned int *phymode;
3251 int err = 0;
3252 struct mtk_nl80211_cb_data *cb_data = cb;
developer72fb0bb2023-01-11 09:46:29 +08003253
developerfead3972023-05-25 20:15:02 +08003254 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08003255 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developerfead3972023-05-25 20:15:02 +08003256 return NL_SKIP;
3257 }
developerc14d83a2023-06-29 20:09:42 +08003258 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer72fb0bb2023-01-11 09:46:29 +08003259
developerfead3972023-05-25 20:15:02 +08003260 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3261 genlmsg_attrlen(gnlh, 0), NULL);
3262 if (err < 0) {
3263 wifi_debug(DEBUG_ERROR, "nla_parse radio nl80211 msg fails,error.\n");
3264 return NL_SKIP;
3265 }
developer0f10c772023-05-16 21:43:39 +08003266
developerfead3972023-05-25 20:15:02 +08003267 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3268 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
3269 tb[NL80211_ATTR_VENDOR_DATA], NULL);
3270 if (err < 0)
3271 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08003272
developerfead3972023-05-25 20:15:02 +08003273 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]) {
3274 phymode = (unsigned int *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]);
3275
3276 memset(cb_data->out_buf, 0, cb_data->out_len);
3277 memmove(cb_data->out_buf, phymode, sizeof(unsigned int));
3278 }
3279 } else
3280 wifi_debug(DEBUG_ERROR, "No Stats from driver.\n");
3281
3282 return NL_OK;
3283}
developer0f10c772023-05-16 21:43:39 +08003284
developerfead3972023-05-25 20:15:02 +08003285void phymode_to_puremode(INT radioIndex, CHAR *output_string, UINT *pureMode, UINT phymode)
3286{
3287 wifi_band band;
3288 unsigned char radio_mode_tem_len;
developere40952c2023-06-15 18:46:43 +08003289 int res;
developerfead3972023-05-25 20:15:02 +08003290
3291 band = wifi_index_to_band(radioIndex);
developer0f10c772023-05-16 21:43:39 +08003292 // puremode is a bit map
developera3511852023-06-14 14:12:59 +08003293 *pureMode = 0;
developer0f10c772023-05-16 21:43:39 +08003294 memset(output_string, 0, RADIO_MODE_LEN);
3295
3296 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3297
3298 switch (band) {
3299 case band_2_4:
3300 if (phymode & WMODE_B) {
developere40952c2023-06-15 18:46:43 +08003301 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "b,");
3302 if (os_snprintf_error(radio_mode_tem_len, res)) {
3303 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3304 return;
3305 }
developer0f10c772023-05-16 21:43:39 +08003306 *pureMode |= WIFI_MODE_B;
3307 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3308 }
3309 if (phymode & WMODE_G) {
developere40952c2023-06-15 18:46:43 +08003310 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "g,");
3311 if (os_snprintf_error(radio_mode_tem_len, res)) {
3312 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3313 return;
3314 }
developer0f10c772023-05-16 21:43:39 +08003315 *pureMode |= WIFI_MODE_G;
3316 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3317 }
3318 if (phymode & WMODE_GN) {
developere40952c2023-06-15 18:46:43 +08003319 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3320 if (os_snprintf_error(radio_mode_tem_len, res)) {
3321 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3322 return;
3323 }
developer0f10c772023-05-16 21:43:39 +08003324 *pureMode |= WIFI_MODE_N;
3325 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3326 }
3327 if (phymode & WMODE_AX_24G) {
developere40952c2023-06-15 18:46:43 +08003328 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3329 if (os_snprintf_error(radio_mode_tem_len, res)) {
3330 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3331 return;
3332 }
developer0f10c772023-05-16 21:43:39 +08003333 *pureMode |= WIFI_MODE_AX;
3334 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3335 }
3336 if (phymode & WMODE_BE_24G) {
developere40952c2023-06-15 18:46:43 +08003337 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3338 if (os_snprintf_error(radio_mode_tem_len, res)) {
3339 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3340 return;
3341 }
developer0f10c772023-05-16 21:43:39 +08003342 *pureMode |= WIFI_MODE_BE;
3343 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3344 }
3345 break;
3346 case band_5:
3347 if (phymode & WMODE_A) {
developere40952c2023-06-15 18:46:43 +08003348 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "a,");
3349 if (os_snprintf_error(radio_mode_tem_len, res)) {
3350 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3351 return;
3352 }
developer0f10c772023-05-16 21:43:39 +08003353 *pureMode |= WIFI_MODE_A;
3354 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3355 }
3356 if (phymode & WMODE_AN) {
developere40952c2023-06-15 18:46:43 +08003357 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3358 if (os_snprintf_error(radio_mode_tem_len, res)) {
3359 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3360 return;
3361 }
developer0f10c772023-05-16 21:43:39 +08003362 *pureMode |= WIFI_MODE_N;
3363 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3364 }
3365 if (phymode & WMODE_AC) {
developere40952c2023-06-15 18:46:43 +08003366 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ac,");
3367 if (os_snprintf_error(radio_mode_tem_len, res)) {
3368 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3369 return;
3370 }
developer0f10c772023-05-16 21:43:39 +08003371 *pureMode |= WIFI_MODE_AC;
3372 }
3373 if (phymode & WMODE_AX_5G) {
developere40952c2023-06-15 18:46:43 +08003374 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3375 if (os_snprintf_error(radio_mode_tem_len, res)) {
3376 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3377 return;
3378 }
developer0f10c772023-05-16 21:43:39 +08003379 *pureMode |= WIFI_MODE_AX;
3380 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3381 }
3382 if (phymode & WMODE_BE_5G) {
developere40952c2023-06-15 18:46:43 +08003383 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3384 if (os_snprintf_error(radio_mode_tem_len, res)) {
3385 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3386 return;
3387 }
developer0f10c772023-05-16 21:43:39 +08003388 *pureMode |= WIFI_MODE_BE;
3389 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3390 }
3391 break;
3392 case band_6:
3393 if (phymode & WMODE_AX_6G) {
developere40952c2023-06-15 18:46:43 +08003394 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3395 if (os_snprintf_error(radio_mode_tem_len, res)) {
3396 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3397 return;
3398 }
developer0f10c772023-05-16 21:43:39 +08003399 *pureMode |= WIFI_MODE_AX;
3400 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3401 }
3402 if (phymode & WMODE_BE_6G) {
developere40952c2023-06-15 18:46:43 +08003403 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3404 if (os_snprintf_error(radio_mode_tem_len, res)) {
3405 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3406 return;
3407 }
developer0f10c772023-05-16 21:43:39 +08003408 *pureMode |= WIFI_MODE_BE;
3409 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3410 }
3411 break;
3412 default:
developer86035662023-06-28 19:21:12 +08003413 wifi_debug(DEBUG_ERROR, "%s band_idx invalid\n", __func__);
developer0f10c772023-05-16 21:43:39 +08003414 break;
3415 }
3416
3417 /* Remove the last comma */
3418 if (strlen(output_string) != 0)
developera3511852023-06-14 14:12:59 +08003419 output_string[strlen(output_string)-1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +08003420
developerfead3972023-05-25 20:15:02 +08003421}
3422
3423INT wifi_getRadioMode(INT radioIndex, CHAR *output_string, UINT *pureMode)
3424{
3425 unsigned int phymode;
3426 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003427 int ret = -1;
3428 unsigned int if_idx = 0;
3429 struct unl unl_ins;
3430 struct nl_msg *msg = NULL;
3431 struct nlattr * msg_data = NULL;
3432 struct mtk_nl80211_param param;
3433 struct mtk_nl80211_cb_data cb_data;
3434
developera3511852023-06-14 14:12:59 +08003435 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3436 if (NULL == output_string || NULL == pureMode)
developerdaf24792023-06-06 11:40:04 +08003437 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003438
3439 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
3440 return RETURN_ERR;
3441
3442 if_idx = if_nametoindex(interface_name);
3443 if (!if_idx) {
3444 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
3445 return RETURN_ERR;
3446 }
3447 /*init mtk nl80211 vendor cmd*/
3448 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
3449 param.if_type = NL80211_ATTR_IFINDEX;
3450 param.if_idx = if_idx;
3451
3452 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3453 if (ret) {
3454 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3455 return RETURN_ERR;
3456 }
3457
3458 /*add mtk vendor cmd data*/
3459 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE, 0)) {
3460 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_WMODE attribute error\n");
3461 nlmsg_free(msg);
3462 goto err;
3463 }
3464
3465 /*send mtk nl80211 vendor msg*/
3466 cb_data.out_buf = (char *)&phymode;
3467 cb_data.out_len = sizeof(unsigned int);
3468
3469 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_radio_mode_handler, &cb_data);
3470
3471 if (ret) {
3472 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3473 goto err;
3474 }
3475 /*deinit mtk nl80211 vendor msg*/
3476 mtk_nl80211_deint(&unl_ins);
3477
3478 phymode_to_puremode(radioIndex, output_string, pureMode, phymode);
developer6e578302023-06-21 10:11:16 +08003479 wifi_debug(DEBUG_INFO,"send cmd success\n");
developerfead3972023-05-25 20:15:02 +08003480
developera3511852023-06-14 14:12:59 +08003481 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3482 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003483err:
3484 mtk_nl80211_deint(&unl_ins);
3485 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
3486 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003487}
3488
3489// Set the radio operating mode, and pure mode flag.
3490INT wifi_setRadioChannelMode(INT radioIndex, CHAR *channelMode, BOOL gOnlyFlag, BOOL nOnlyFlag, BOOL acOnlyFlag) //RDKB
3491{
developera3511852023-06-14 14:12:59 +08003492 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%s_%d_%d:%d\n",__func__,channelMode,nOnlyFlag,gOnlyFlag,__LINE__);
3493 if (strcmp (channelMode,"11A") == 0)
3494 {
3495 writeBandWidth(radioIndex,"20MHz");
3496 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3497 printf("\nChannel Mode is 802.11a (5GHz)\n");
3498 }
3499 else if (strcmp (channelMode,"11NAHT20") == 0)
3500 {
3501 writeBandWidth(radioIndex,"20MHz");
3502 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3503 printf("\nChannel Mode is 802.11n-20MHz(5GHz)\n");
3504 }
3505 else if (strcmp (channelMode,"11NAHT40PLUS") == 0)
3506 {
3507 writeBandWidth(radioIndex,"40MHz");
3508 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3509 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3510 }
3511 else if (strcmp (channelMode,"11NAHT40MINUS") == 0)
3512 {
3513 writeBandWidth(radioIndex,"40MHz");
3514 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3515 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3516 }
3517 else if (strcmp (channelMode,"11ACVHT20") == 0)
3518 {
3519 writeBandWidth(radioIndex,"20MHz");
3520 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3521 printf("\nChannel Mode is 802.11ac-20MHz(5GHz)\n");
3522 }
3523 else if (strcmp (channelMode,"11ACVHT40PLUS") == 0)
3524 {
3525 writeBandWidth(radioIndex,"40MHz");
3526 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3527 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3528 }
3529 else if (strcmp (channelMode,"11ACVHT40MINUS") == 0)
3530 {
3531 writeBandWidth(radioIndex,"40MHz");
3532 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3533 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3534 }
3535 else if (strcmp (channelMode,"11ACVHT80") == 0)
3536 {
3537 wifi_setRadioOperatingChannelBandwidth(radioIndex,"80MHz");
3538 printf("\nChannel Mode is 802.11ac-80MHz(5GHz)\n");
3539 }
3540 else if (strcmp (channelMode,"11ACVHT160") == 0)
3541 {
3542 wifi_setRadioOperatingChannelBandwidth(radioIndex,"160MHz");
3543 printf("\nChannel Mode is 802.11ac-160MHz(5GHz)\n");
3544 }
3545 else if (strcmp (channelMode,"11B") == 0)
3546 {
3547 writeBandWidth(radioIndex,"20MHz");
3548 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3549 printf("\nChannel Mode is 802.11b(2.4GHz)\n");
3550 }
3551 else if (strcmp (channelMode,"11G") == 0)
3552 {
3553 writeBandWidth(radioIndex,"20MHz");
3554 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3555 printf("\nChannel Mode is 802.11g(2.4GHz)\n");
3556 }
3557 else if (strcmp (channelMode,"11NGHT20") == 0)
3558 {
3559 writeBandWidth(radioIndex,"20MHz");
3560 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3561 printf("\nChannel Mode is 802.11n-20MHz(2.4GHz)\n");
3562 }
3563 else if (strcmp (channelMode,"11NGHT40PLUS") == 0)
3564 {
3565 writeBandWidth(radioIndex,"40MHz");
3566 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3567 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3568 }
3569 else if (strcmp (channelMode,"11NGHT40MINUS") == 0)
3570 {
3571 writeBandWidth(radioIndex,"40MHz");
3572 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3573 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3574 }
3575 else
3576 {
3577 return RETURN_ERR;
3578 }
3579 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003580
developera3511852023-06-14 14:12:59 +08003581 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003582}
3583
developer0f10c772023-05-16 21:43:39 +08003584typedef enum _RT_802_11_PHY_MODE {
3585 PHY_11BG_MIXED = 0,
3586 PHY_11B = 1,
3587 PHY_11A = 2,
3588 PHY_11ABG_MIXED = 3,
3589 PHY_11G = 4,
3590 PHY_11ABGN_MIXED = 5, /* both band 5 */
developera3511852023-06-14 14:12:59 +08003591 PHY_11N_2_4G = 6, /* 11n-only with 2.4G band 6 */
3592 PHY_11GN_MIXED = 7, /* 2.4G band 7 */
3593 PHY_11AN_MIXED = 8, /* 5G band 8 */
3594 PHY_11BGN_MIXED = 9, /* if check 802.11b. 9 */
3595 PHY_11AGN_MIXED = 10, /* if check 802.11b. 10 */
3596 PHY_11N_5G = 11, /* 11n-only with 5G band 11 */
developer0f10c772023-05-16 21:43:39 +08003597 PHY_11VHT_N_ABG_MIXED = 12, /* 12 -> AC/A/AN/B/G/GN mixed */
3598 PHY_11VHT_N_AG_MIXED = 13, /* 13 -> AC/A/AN/G/GN mixed */
3599 PHY_11VHT_N_A_MIXED = 14, /* 14 -> AC/AN/A mixed in 5G band */
3600 PHY_11VHT_N_MIXED = 15, /* 15 -> AC/AN mixed in 5G band */
3601 PHY_11AX_24G = 16,
3602 PHY_11AX_5G = 17,
3603 PHY_11AX_6G = 18,
3604 PHY_11AX_24G_6G = 19,
3605 PHY_11AX_5G_6G = 20,
3606 PHY_11AX_24G_5G_6G = 21,
3607 PHY_11BE_24G = 22,
3608 PHY_11BE_5G = 23,
3609 PHY_11BE_6G = 24,
3610 PHY_11BE_24G_6G = 25,
3611 PHY_11BE_5G_6G = 26,
3612 PHY_11BE_24G_5G_6G = 27,
3613 PHY_MODE_MAX,
3614} RT_802_11_PHY_MODE;
3615
3616unsigned int puremode_to_wireless_mode(INT radioIndex, UINT pureMode)
3617{
3618 int band_idx = 0;
developerfead3972023-05-25 20:15:02 +08003619 unsigned char wireless_mode = PHY_MODE_MAX;
developer0f10c772023-05-16 21:43:39 +08003620
3621 band_idx = radio_index_to_band(radioIndex);
3622
3623 switch (band_idx) {
3624 case band_2_4:
3625 if (pureMode == (WIFI_MODE_G | WIFI_MODE_N))
3626 wireless_mode = PHY_11GN_MIXED;
3627 if (pureMode == (WIFI_MODE_B | WIFI_MODE_G | WIFI_MODE_N))
3628 wireless_mode = PHY_11BGN_MIXED;
3629 if (pureMode & WIFI_MODE_AX)
3630 wireless_mode = PHY_11AX_24G;
3631 if (pureMode & WIFI_MODE_BE)
3632 wireless_mode = PHY_11BE_24G;
3633 break;
3634 case band_5:
3635 if (pureMode == WIFI_MODE_N)
3636 wireless_mode = PHY_11N_5G;
3637 if ((pureMode == WIFI_MODE_AC) || (pureMode == (WIFI_MODE_N | WIFI_MODE_AC)))
3638 wireless_mode = PHY_11VHT_N_MIXED;
3639 if (pureMode == (WIFI_MODE_A | WIFI_MODE_N | WIFI_MODE_AC))
3640 wireless_mode = PHY_11VHT_N_A_MIXED;
3641 if (pureMode & WIFI_MODE_AX)
3642 wireless_mode = PHY_11AX_5G;
3643 if (pureMode & WIFI_MODE_BE)
3644 wireless_mode = PHY_11BE_5G;
3645 break;
3646 case band_6:
3647 if (pureMode & WIFI_MODE_AX)
3648 wireless_mode = PHY_11AX_6G;
3649 if (pureMode & WIFI_MODE_BE)
3650 wireless_mode = PHY_11BE_6G;
3651 break;
3652 default:
developer37646972023-06-29 10:58:43 +08003653 if (fprintf(stderr, "%s band_idx invalid\n", __func__) < 0)
3654 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developer0f10c772023-05-16 21:43:39 +08003655 break;
3656 }
3657
3658 return wireless_mode;
3659}
3660
developer72fb0bb2023-01-11 09:46:29 +08003661// Set the radio operating mode, and pure mode flag.
3662INT wifi_setRadioMode(INT radioIndex, CHAR *channelMode, UINT pureMode)
3663{
developerfead3972023-05-25 20:15:02 +08003664 unsigned char wireless_mode = PHY_MODE_MAX;
developer69b61b02023-03-07 17:17:44 +08003665
developer0f10c772023-05-16 21:43:39 +08003666 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003667 int ret = -1;
3668 unsigned int if_idx = 0;
3669 struct unl unl_ins;
3670 struct nl_msg *msg = NULL;
3671 struct nlattr * msg_data = NULL;
3672 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +08003673
developer9f2358c2023-09-22 18:42:12 +08003674 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%s:%d_%d\n", __func__, channelMode, pureMode, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003675
developer0f10c772023-05-16 21:43:39 +08003676 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
developer72fb0bb2023-01-11 09:46:29 +08003677
developera3511852023-06-14 14:12:59 +08003678 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003679 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003680 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003681 }
developer72fb0bb2023-01-11 09:46:29 +08003682
developer0f10c772023-05-16 21:43:39 +08003683 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08003684 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003685
3686 if_idx = if_nametoindex(interface_name);
3687 if (!if_idx) {
3688 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", interface_name);
3689 return RETURN_ERR;
3690 }
3691 /*init mtk nl80211 vendor cmd*/
3692 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
3693 param.if_type = NL80211_ATTR_IFINDEX;
3694 param.if_idx = if_idx;
3695
3696 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3697 if (ret) {
3698 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3699 return RETURN_ERR;
3700 }
3701
3702 /*add mtk vendor cmd data*/
3703 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_WIRELESS_MODE, wireless_mode)) {
3704 wifi_debug(DEBUG_ERROR, "Nla put AP_WIRELESS_MODE attribute error\n");
3705 nlmsg_free(msg);
3706 goto err;
3707 }
3708 /*send mtk nl80211 vendor msg*/
3709 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
3710 if (ret) {
3711 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3712 goto err;
3713 }
3714 /*deinit mtk nl80211 vendor msg*/
3715 mtk_nl80211_deint(&unl_ins);
3716 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08003717
developera3511852023-06-14 14:12:59 +08003718 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer0f10c772023-05-16 21:43:39 +08003719
developera3511852023-06-14 14:12:59 +08003720 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003721err:
3722 mtk_nl80211_deint(&unl_ins);
3723 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
3724 return RETURN_ERR;
developer0f10c772023-05-16 21:43:39 +08003725}
3726
3727INT wifi_setRadioMode_by_dat(INT radioIndex, UINT pureMode)
3728{
developerfead3972023-05-25 20:15:02 +08003729 unsigned char wireless_mode = PHY_MODE_MAX;
developera3511852023-06-14 14:12:59 +08003730 char buf[MAX_BUF_SIZE] = {0};
developer0f10c772023-05-16 21:43:39 +08003731 char dat_file[MAX_BUF_SIZE] = {0};
3732 struct params params={0};
developere40952c2023-06-15 18:46:43 +08003733 int res;
developer0f10c772023-05-16 21:43:39 +08003734
3735 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, pureMode, __LINE__);
3736
3737 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
3738
developera3511852023-06-14 14:12:59 +08003739 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003740 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003741 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003742 }
developer0f10c772023-05-16 21:43:39 +08003743
3744 params.name = "WirelessMode";
developere40952c2023-06-15 18:46:43 +08003745 res = snprintf(buf, sizeof(buf), "%d", wireless_mode);
3746 if (os_snprintf_error(sizeof(buf), res)) {
3747 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3748 return RETURN_ERR;
3749 }
3750
developera3511852023-06-14 14:12:59 +08003751 params.value = buf;
developer0f10c772023-05-16 21:43:39 +08003752
developere40952c2023-06-15 18:46:43 +08003753 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
3754 if (os_snprintf_error(sizeof(dat_file), res)) {
3755 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3756 return RETURN_ERR;
3757 }
developera3511852023-06-14 14:12:59 +08003758 wifi_datfileWrite(dat_file, &params, 1);
developer0f10c772023-05-16 21:43:39 +08003759
developera3511852023-06-14 14:12:59 +08003760 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003761
developera3511852023-06-14 14:12:59 +08003762 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003763}
3764
3765INT wifi_setRadioHwMode(INT radioIndex, CHAR *hw_mode) {
3766
developera3511852023-06-14 14:12:59 +08003767 char config_file[64] = {0};
3768 char buf[64] = {0};
3769 struct params params = {0};
3770 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08003771 int res;
developer72fb0bb2023-01-11 09:46:29 +08003772
developera3511852023-06-14 14:12:59 +08003773 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003774
developera3511852023-06-14 14:12:59 +08003775 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003776
developera3511852023-06-14 14:12:59 +08003777 if (strncmp(hw_mode, "a", 1) == 0 && (band != band_5 && band != band_6))
3778 return RETURN_ERR;
3779 else if ((strncmp(hw_mode, "b", 1) == 0 || strncmp(hw_mode, "g", 1) == 0) && band != band_2_4)
3780 return RETURN_ERR;
3781 else if ((strncmp(hw_mode, "a", 1) && strncmp(hw_mode, "b", 1) && strncmp(hw_mode, "g", 1)) || band == band_invalid)
3782 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003783
developer75bd10c2023-06-27 11:34:08 +08003784 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3785 if (os_snprintf_error(sizeof(config_file), res)) {
3786 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3787 return RETURN_ERR;
3788 }
developera3511852023-06-14 14:12:59 +08003789 params.name = "hw_mode";
3790 params.value = hw_mode;
3791 wifi_hostapdWrite(config_file, &params, 1);
3792 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003793
developera3511852023-06-14 14:12:59 +08003794 if (band == band_2_4) {
3795 if (strncmp(hw_mode, "b", 1) == 0) {
3796 wifi_setRadioMode(radioIndex, "20MHz", WIFI_MODE_B);
developere40952c2023-06-15 18:46:43 +08003797 res = snprintf(buf, sizeof(buf), "%s", "1,2,5.5,11");
3798 if (os_snprintf_error(sizeof(buf), res)) {
3799 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3800 return RETURN_ERR;
3801 }
developera3511852023-06-14 14:12:59 +08003802 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003803 res = snprintf(buf, sizeof(buf), "%s", "1,2");
3804 if (os_snprintf_error(sizeof(buf), res)) {
3805 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3806 return RETURN_ERR;
3807 }
developera3511852023-06-14 14:12:59 +08003808 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3809 } else {
3810 // 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 +08003811
developere40952c2023-06-15 18:46:43 +08003812 res = snprintf(buf, sizeof(buf), "%s", "6,9,12,18,24,36,48,54");
3813 if (os_snprintf_error(sizeof(buf), res)) {
3814 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3815 return RETURN_ERR;
3816 }
developera3511852023-06-14 14:12:59 +08003817 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003818 res = snprintf(buf, sizeof(buf), "%s", "6,12,24");
3819 if (os_snprintf_error(sizeof(buf), res)) {
3820 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3821 return RETURN_ERR;
3822 }
developera3511852023-06-14 14:12:59 +08003823 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3824 }
3825 }
developer72fb0bb2023-01-11 09:46:29 +08003826
developera3511852023-06-14 14:12:59 +08003827 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3828 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003829}
3830
3831INT wifi_setNoscan(INT radioIndex, CHAR *noscan)
3832{
developera3511852023-06-14 14:12:59 +08003833 char config_file[64] = {0};
3834 struct params params = {0};
3835 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08003836 int res;
developer72fb0bb2023-01-11 09:46:29 +08003837
developera3511852023-06-14 14:12:59 +08003838 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003839
developera3511852023-06-14 14:12:59 +08003840 band = wifi_index_to_band(radioIndex);
3841 if (band != band_2_4)
3842 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003843
developer75bd10c2023-06-27 11:34:08 +08003844 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3845 if (os_snprintf_error(sizeof(config_file), res)) {
3846 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3847 return RETURN_ERR;
3848 }
developera3511852023-06-14 14:12:59 +08003849 params.name = "noscan";
3850 params.value = noscan;
3851 wifi_hostapdWrite(config_file, &params, 1);
3852 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003853
developera3511852023-06-14 14:12:59 +08003854 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3855 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003856}
3857
3858//Get the list of supported channel. eg: "1-11"
3859//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.
3860INT wifi_getRadioPossibleChannels(INT radioIndex, CHAR *output_string) //RDKB
3861{
developera3511852023-06-14 14:12:59 +08003862 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3863 if (NULL == output_string)
3864 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +08003865
developera3511852023-06-14 14:12:59 +08003866 char buf[128] = {0};
3867 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +08003868 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003869
developera3511852023-06-14 14:12:59 +08003870 // Parse possible channel number and separate them with commas.
3871 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
3872 phyId = radio_index_to_phy(radioIndex);
3873 // Channel 68 and 96 only allow bandwidth 20MHz, so we remove them with their frequency.
3874 if (dfs_enable)
developer8078acf2023-08-04 18:52:48 +08003875 res = _syscmd_secure(buf, sizeof(buf), "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 +08003876 else
developer8078acf2023-08-04 18:52:48 +08003877 res = _syscmd_secure(buf, sizeof(buf), "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 +08003878
developer8078acf2023-08-04 18:52:48 +08003879 if (res) {
3880 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08003881 }
developera3511852023-06-14 14:12:59 +08003882 strncpy(output_string, buf, strlen(buf) < sizeof(buf) ? strlen(buf) : sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08003883
developera3511852023-06-14 14:12:59 +08003884 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3885 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003886}
developerd1824452023-05-18 12:30:04 +08003887//Getting current radio extension channel
3888INT wifi_halgetRadioExtChannel(CHAR *file,CHAR *Value)
3889{
developera3511852023-06-14 14:12:59 +08003890 CHAR buf[150] = {0};
developer32f2a182023-06-27 19:50:41 +08003891 int len;
developerd1824452023-05-18 12:30:04 +08003892
developera3511852023-06-14 14:12:59 +08003893 wifi_datfileRead(file, "HT_EXTCHA", buf, sizeof(buf));
developerb14b3462023-07-01 18:02:42 +08003894
3895 if (strncmp(buf, "0", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08003896 len = strlen("BelowControlChannel");
3897 memcpy(Value, "BelowControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08003898 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08003899 }
developerb14b3462023-07-01 18:02:42 +08003900 else if(strncmp(buf, "1", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08003901 len = strlen("AboveControlChannel");
3902 memcpy(Value, "AboveControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08003903 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08003904 }
developer5b23cd02023-07-19 20:26:03 +08003905
developera3511852023-06-14 14:12:59 +08003906 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +08003907}
developerf6a87542023-05-16 15:47:28 +08003908
developer72fb0bb2023-01-11 09:46:29 +08003909//Get the list for used channel. eg: "1,6,9,11"
3910//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.
3911INT wifi_getRadioChannelsInUse(INT radioIndex, CHAR *output_string) //RDKB
3912{
developera3511852023-06-14 14:12:59 +08003913 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +08003914
developera3511852023-06-14 14:12:59 +08003915 char buf[128] = {0};
3916 char config_file[64] = {0};
3917 int channel = 0;
3918 int freq = 0;
3919 int bandwidth = 0;
3920 int center_freq = 0;
3921 int center_channel = 0;
3922 int channel_delta = 0;
3923 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08003924 int res;
developer72fb0bb2023-01-11 09:46:29 +08003925
developera3511852023-06-14 14:12:59 +08003926 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003927
developera3511852023-06-14 14:12:59 +08003928 if (NULL == output_string)
3929 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003930
developera3511852023-06-14 14:12:59 +08003931 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
3932 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08003933
developer8078acf2023-08-04 18:52:48 +08003934 res = _syscmd_secure(buf, sizeof(buf), "iw %s info | grep channel | sed -e 's/[^0-9 ]//g'", interface_name);
3935 if (res) {
3936 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08003937 }
developera3511852023-06-14 14:12:59 +08003938 if (strlen(buf) == 0) {
developer75bd10c2023-06-27 11:34:08 +08003939 wifi_debug(DEBUG_ERROR, "failed to get channel information from iw.\n");
developera3511852023-06-14 14:12:59 +08003940 return RETURN_ERR;
3941 }
developerd14dff12023-06-28 22:47:44 +08003942 if (sscanf(buf, "%d %d %d %*d %d", &channel, &freq, &bandwidth, &center_freq) != 4) {
3943 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
3944 return RETURN_ERR;
3945 }
developer72fb0bb2023-01-11 09:46:29 +08003946
developera3511852023-06-14 14:12:59 +08003947 if (bandwidth == 20) {
developere40952c2023-06-15 18:46:43 +08003948 res = snprintf(output_string, 256, "%d", channel);
3949 if (os_snprintf_error(256, res)) {
3950 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3951 return RETURN_ERR;
3952 }
developera3511852023-06-14 14:12:59 +08003953 return RETURN_OK;
3954 }
developer72fb0bb2023-01-11 09:46:29 +08003955
developera3511852023-06-14 14:12:59 +08003956 center_channel = ieee80211_frequency_to_channel(center_freq);
developer72fb0bb2023-01-11 09:46:29 +08003957
developera3511852023-06-14 14:12:59 +08003958 band = wifi_index_to_band(radioIndex);
3959 if (band == band_2_4 && bandwidth == 40) {
developer32f2a182023-06-27 19:50:41 +08003960 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
3961 if (os_snprintf_error(sizeof(config_file), res)) {
3962 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3963 return RETURN_ERR;
3964 }
developera3511852023-06-14 14:12:59 +08003965 memset(buf, 0, sizeof(buf));
3966 wifi_halgetRadioExtChannel(config_file, buf); // read ht_capab for HT40+ or -
developer72fb0bb2023-01-11 09:46:29 +08003967
developera3511852023-06-14 14:12:59 +08003968 if (strncmp(buf, "AboveControlChannel", strlen("AboveControlChannel")) == 0 && channel < 10) {
developere40952c2023-06-15 18:46:43 +08003969 res = snprintf(output_string, 256, "%d,%d", channel, channel+4);
developera3511852023-06-14 14:12:59 +08003970 } else if (strncmp(buf, "BelowControlChannel", strlen("BelowControlChannel")) == 0 && channel > 4) {
developere40952c2023-06-15 18:46:43 +08003971 res = snprintf(output_string, 256, "%d,%d", channel-4, channel);
developera3511852023-06-14 14:12:59 +08003972 } else {
developer37646972023-06-29 10:58:43 +08003973 if (fprintf(stderr, "%s: invalid channel %d set with %s\n.", __func__, channel, buf) < 0)
3974 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08003975 return RETURN_ERR;
3976 }
developerb758dfd2023-06-21 17:32:07 +08003977
developere40952c2023-06-15 18:46:43 +08003978 if (os_snprintf_error(256, res)) {
3979 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3980 return RETURN_ERR;
3981 }
developera3511852023-06-14 14:12:59 +08003982 } else if (band == band_5 || band == band_6){
3983 // to minus 20 is an offset, because frequence of a channel have a range. We need to use offset to calculate correct channel.
3984 // example: bandwidth 80: center is 42 (5210), channels are "36,40,44,48" (5170-5250). The delta should be 6.
3985 channel_delta = (bandwidth-20)/10;
3986 memset(output_string, 0, 256);
3987 for (int i = center_channel-channel_delta; i <= center_channel+channel_delta; i+=4) {
3988 // If i is not the last channel, we add a comma.
developere40952c2023-06-15 18:46:43 +08003989 res = snprintf(buf, sizeof(buf), "%d%s", i, i==center_channel+channel_delta?"":",");
developer5b23cd02023-07-19 20:26:03 +08003990
developere40952c2023-06-15 18:46:43 +08003991 if (os_snprintf_error(sizeof(buf), res)) {
3992 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3993 return RETURN_ERR;
3994 }
developer32f2a182023-06-27 19:50:41 +08003995 strncat(output_string, buf, sizeof(output_string) - strlen(output_string) - 1);
developera3511852023-06-14 14:12:59 +08003996 }
3997 } else
3998 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003999
developera3511852023-06-14 14:12:59 +08004000 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4001 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004002}
4003
developer69b61b02023-03-07 17:17:44 +08004004//Get the running channel number
developerd1824452023-05-18 12:30:04 +08004005INT wifi_getRadioChannel(INT radioIndex, ULONG *output_ulong) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004006{
developera3511852023-06-14 14:12:59 +08004007 char channel_str[16] = {0};
4008 char config_file[128] = {0};
4009 char buf[MAX_BUF_SIZE] = {0};
developer8078acf2023-08-04 18:52:48 +08004010
developera3511852023-06-14 14:12:59 +08004011 char interface_name[IF_NAME_SIZE] = {0};
4012 wifi_band band = band_invalid;
4013 ULONG iwChannel = 0;
developere40952c2023-06-15 18:46:43 +08004014 int res;
developer47a56bf2023-05-30 13:38:57 +08004015
developera3511852023-06-14 14:12:59 +08004016 if (output_ulong == NULL)
4017 return RETURN_ERR;
4018 band = wifi_index_to_band(radioIndex);
developerb758dfd2023-06-21 17:32:07 +08004019 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08004020 if (os_snprintf_error(sizeof(config_file), res)) {
4021 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4022 return RETURN_ERR;
4023 }
4024
developera3511852023-06-14 14:12:59 +08004025 wifi_datfileRead(config_file, "Channel", channel_str, sizeof(channel_str));
developerb14b3462023-07-01 18:02:42 +08004026 if (hal_strtoul(channel_str, 10, output_ulong) < 0) {
developerc14d83a2023-06-29 20:09:42 +08004027 wifi_debug(DEBUG_ERROR, "strtol fail\n");
4028 }
developera3511852023-06-14 14:12:59 +08004029 if (*output_ulong == 0) {
4030 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4031 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +08004032
4033 res = _syscmd_secure(buf, sizeof(buf), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4034 if (res) {
4035 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08004036 }
4037
developerd14dff12023-06-28 22:47:44 +08004038 if (sscanf(buf, "%lu", &iwChannel) != 1) {
4039 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
4040 return RETURN_ERR;
4041 }
developera3511852023-06-14 14:12:59 +08004042 *output_ulong = iwChannel;
4043 }
developer72fb0bb2023-01-11 09:46:29 +08004044
developera3511852023-06-14 14:12:59 +08004045 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004046}
4047
developer72fb0bb2023-01-11 09:46:29 +08004048INT wifi_getApChannel(INT apIndex,ULONG *output_ulong) //RDKB
4049{
developer8078acf2023-08-04 18:52:48 +08004050 char buf[5] = {0};
developera3511852023-06-14 14:12:59 +08004051 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08004052 int res;
developer72fb0bb2023-01-11 09:46:29 +08004053
developera3511852023-06-14 14:12:59 +08004054 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4055 if (NULL == output_ulong)
4056 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004057
developera3511852023-06-14 14:12:59 +08004058 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
4059 return RETURN_ERR;
developer47a56bf2023-05-30 13:38:57 +08004060
developer8078acf2023-08-04 18:52:48 +08004061
4062 res = _syscmd_secure(buf, sizeof(buf), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4063 if (res) {
4064 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
4065 }
developera3511852023-06-14 14:12:59 +08004066 *output_ulong = (strlen(buf) >= 1)? atol(buf): 0;
4067 if (*output_ulong == 0) {
4068 return RETURN_ERR;
4069 }
developer72fb0bb2023-01-11 09:46:29 +08004070
developera3511852023-06-14 14:12:59 +08004071 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4072 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004073}
developer72fb0bb2023-01-11 09:46:29 +08004074//Storing the previous channel value
4075INT wifi_storeprevchanval(INT radioIndex)
4076{
developera3511852023-06-14 14:12:59 +08004077 char output[4]={'\0'};
4078 char config_file[MAX_BUF_SIZE] = {0};
4079 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004080 int res;
developerd1824452023-05-18 12:30:04 +08004081
developera3511852023-06-14 14:12:59 +08004082 band = wifi_index_to_band(radioIndex);
4083 if (band == band_invalid) {
4084 return RETURN_ERR;
4085 wifi_dbg_printf("[%s]: Invalid radio index", __func__);
4086 }
developere40952c2023-06-15 18:46:43 +08004087 res = snprintf(config_file, sizeof(config_file), "%s%d.dat",LOGAN_DAT_FILE, band);
4088 if (os_snprintf_error(sizeof(config_file), res)) {
4089 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4090 return RETURN_ERR;
4091 }
4092
developera3511852023-06-14 14:12:59 +08004093 wifi_datfileRead(config_file, "Channel", output, sizeof(output));
developerd1824452023-05-18 12:30:04 +08004094
developera3511852023-06-14 14:12:59 +08004095 if(band == band_2_4)
developer33f13ba2023-07-12 16:19:06 +08004096 res = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004097 else if(band == band_5)
developer33f13ba2023-07-12 16:19:06 +08004098 res = v_secure_system("echo %s > /var/prevchanval5G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004099 else
developer33f13ba2023-07-12 16:19:06 +08004100 res = v_secure_system("echo %s > /var/prevchanval6G_AutoChannelEnable", output);
developer75bd10c2023-06-27 11:34:08 +08004101
developer33f13ba2023-07-12 16:19:06 +08004102 if (res) {
4103 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08004104 }
developera3511852023-06-14 14:12:59 +08004105 Radio_flag = FALSE;
4106 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004107}
4108
4109//Set the running channel number
4110INT wifi_setRadioChannel(INT radioIndex, ULONG channel) //RDKB //AP only
4111{
developera3511852023-06-14 14:12:59 +08004112 // We only write hostapd config here
4113 char str_channel[8]={0};
4114 char *list_channel;
4115 char possible_channels[256] = {0};
4116 char config_file_dat[128] = {0};
4117 struct params dat = {0};
4118 struct params acs = {0};
4119 wifi_band band = band_invalid;
4120 bool acs_channel = false;
developere40952c2023-06-15 18:46:43 +08004121 int res;
developer72fb0bb2023-01-11 09:46:29 +08004122
developera3511852023-06-14 14:12:59 +08004123 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004124
developera3511852023-06-14 14:12:59 +08004125 if (channel == 0)
4126 acs_channel = true;
4127 // Check valid
developer75bd10c2023-06-27 11:34:08 +08004128 res = snprintf(str_channel, sizeof(str_channel), "%lu", channel);
4129 if (os_snprintf_error(sizeof(str_channel), res)) {
4130 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4131 return RETURN_ERR;
4132 }
developer47a56bf2023-05-30 13:38:57 +08004133
developera3511852023-06-14 14:12:59 +08004134 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4135 list_channel = strtok(possible_channels, ",");
4136 while(true)
4137 {
4138 if(list_channel == NULL) { // input not in the list
developer75bd10c2023-06-27 11:34:08 +08004139 wifi_debug(DEBUG_ERROR, "Channel %s is not in possible list\n", str_channel);
developera3511852023-06-14 14:12:59 +08004140 return RETURN_ERR;
4141 }
4142 if (strncmp(str_channel, list_channel, strlen(list_channel)) == 0 || strncmp(str_channel, "0", 1) == 0)
4143 break;
4144 list_channel = strtok(NULL, ",");
4145 }
4146 /*
4147 list.name = "channel";
4148 list.value = str_channel;
4149 wifi_getMaxRadioNumber(&max_radio_num);
4150 for(int i=0; i<=MAX_APS/max_radio_num;i++)
4151 {
4152 sprintf(config_file, "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_radio_num*i));
4153 wifi_hostapdWrite(config_file, &list, 1);
4154 }
4155 */
4156 dat.name = "Channel";
4157 dat.value = str_channel;
4158 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004159 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4160 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4161 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4162 return RETURN_ERR;
4163 }
4164
developera3511852023-06-14 14:12:59 +08004165 wifi_datfileWrite(config_file_dat, &dat, 1);
4166 if (acs_channel == true) {
4167 acs.name = "AutoChannelSelect";
4168 acs.value = "3";
4169 } else {
4170 acs.name = "AutoChannelSelect";
4171 acs.value = "0";
4172 }
4173 wifi_datfileWrite(config_file_dat, &acs, 1);
developer5b23cd02023-07-19 20:26:03 +08004174 if (multiple_set == FALSE) {
4175 wifi_setRadioEnable(radioIndex, FALSE);
4176 wifi_setRadioEnable(radioIndex, TRUE);
4177 }
4178
developera3511852023-06-14 14:12:59 +08004179 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4180 return RETURN_OK;
4181}
4182
4183INT wifi_setRadioCenterChannel(INT radioIndex, ULONG channel)
4184{
4185 struct params list[2];
4186 char str_idx[16];
4187 char config_file[64];
developere40952c2023-06-15 18:46:43 +08004188 int max_num_radios = 0, res;
developera3511852023-06-14 14:12:59 +08004189 wifi_band band = band_invalid;
4190
4191 band = wifi_index_to_band(radioIndex);
4192 if (band == band_2_4)
4193 return RETURN_OK;
4194
developere40952c2023-06-15 18:46:43 +08004195 res = snprintf(str_idx, sizeof(str_idx), "%lu", channel);
4196 if (os_snprintf_error(sizeof(str_idx), res)) {
4197 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4198 return RETURN_ERR;
4199 }
4200
developera3511852023-06-14 14:12:59 +08004201 list[0].name = "vht_oper_centr_freq_seg0_idx";
4202 list[0].value = str_idx;
4203 list[1].name = "he_oper_centr_freq_seg0_idx";
4204 list[1].value = str_idx;
4205
4206 wifi_getMaxRadioNumber(&max_num_radios);
developer9ce44382023-06-28 11:09:37 +08004207 if(max_num_radios== 0){
4208 return RETURN_ERR;
4209 }
developera3511852023-06-14 14:12:59 +08004210 for(int i=0; i<=MAX_APS/max_num_radios; i++)
4211 {
developere40952c2023-06-15 18:46:43 +08004212 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_num_radios*i));
4213 if (os_snprintf_error(sizeof(config_file), res)) {
4214 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4215 return RETURN_ERR;
4216 }
developera3511852023-06-14 14:12:59 +08004217 if (band == band_6)
4218 wifi_hostapdWrite(config_file, &list[1], 1);
4219 else
4220 wifi_hostapdWrite(config_file, list, 2);
4221 }
4222
4223 return RETURN_OK;
4224}
4225
4226//Enables or disables a driver level variable to indicate if auto channel selection is enabled on this radio
4227//This "auto channel" means the auto channel selection when radio is up. (which is different from the dynamic channel/frequency selection (DFC/DCS))
4228INT wifi_setRadioAutoChannelEnable(INT radioIndex, BOOL enable) //RDKB
4229{
4230 //Set to wifi config only. Wait for wifi reset to apply.
4231 ULONG Value = 0;
4232 char config_file_dat[128] = {0};
4233 struct params acs = {0};
4234 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004235 int res;
developera3511852023-06-14 14:12:59 +08004236
4237 if(enable == TRUE) {
4238 wifi_setRadioChannel(radioIndex,Value);
4239 } else {
4240 acs.name = "AutoChannelSelect";
4241 acs.value = "0";
4242 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004243 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4244 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4245 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4246 return RETURN_ERR;
4247 }
developera3511852023-06-14 14:12:59 +08004248 wifi_datfileWrite(config_file_dat, &acs, 1);
4249 }
4250 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004251}
4252
4253INT wifi_getRadioAutoChannelSupported(INT radioIndex, BOOL *output_bool)
4254{
developera3511852023-06-14 14:12:59 +08004255 if (output_bool == NULL)
4256 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004257
developera3511852023-06-14 14:12:59 +08004258 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004259
developera3511852023-06-14 14:12:59 +08004260 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004261}
4262
4263INT wifi_getRadioDCSSupported(INT radioIndex, BOOL *output_bool) //RDKB
4264{
developera3511852023-06-14 14:12:59 +08004265 if (NULL == output_bool)
4266 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004267 *output_bool=TRUE;
developera3511852023-06-14 14:12:59 +08004268 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004269}
4270
4271INT wifi_getRadioDCSEnable(INT radioIndex, BOOL *output_bool) //RDKB
4272{
developer326d4232023-06-15 16:45:30 +08004273 unsigned long period = 0;
4274
developera3511852023-06-14 14:12:59 +08004275 if (NULL == output_bool)
4276 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004277
4278 if (wifi_getRadioAutoChannelRefreshPeriod(radioIndex, &period) != RETURN_OK)
developerb758dfd2023-06-21 17:32:07 +08004279 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004280
4281 *output_bool = (period > 0) ? TRUE : FALSE;
4282
developera3511852023-06-14 14:12:59 +08004283 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004284}
4285
developer326d4232023-06-15 16:45:30 +08004286INT wifi_setRadioDCSEnable(INT radioIndex, BOOL enable) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004287{
developer326d4232023-06-15 16:45:30 +08004288 ULONG period = 1800;
4289
4290 if (enable == TRUE) {
4291 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, period) != RETURN_OK)
4292 return RETURN_ERR;
4293 }
4294 else {
4295 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, 0) != RETURN_OK)
4296 return RETURN_ERR;
4297 }
developera3511852023-06-14 14:12:59 +08004298 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004299}
4300
4301INT wifi_setApEnableOnLine(ULONG wlanIndex,BOOL enable)
4302{
developera3511852023-06-14 14:12:59 +08004303 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004304}
4305
4306INT wifi_factoryResetAP(int apIndex)
4307{
developerb149d9d2023-06-06 16:14:22 +08004308 char ap_config_file[MAX_SUB_CMD_SIZE] = {0};
developer8078acf2023-08-04 18:52:48 +08004309
developer47cc27a2023-05-17 23:09:58 +08004310 char ret_buf[MAX_BUF_SIZE] = {0};
4311 int radio_idx = 0;
4312 int bss_idx = 0;
4313 char ssid[32] = {0};
4314 char interface[IF_NAME_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08004315 char psk_file[MAX_SUB_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08004316 struct params params[3] = {0};
developere40952c2023-06-15 18:46:43 +08004317 int res;
developer72fb0bb2023-01-11 09:46:29 +08004318
developera3511852023-06-14 14:12:59 +08004319 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004320
developer47cc27a2023-05-17 23:09:58 +08004321 /*del old config file*/
developer9ce44382023-06-28 11:09:37 +08004322 res = snprintf(ap_config_file, sizeof(ap_config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
developerd14dff12023-06-28 22:47:44 +08004323 if (os_snprintf_error(sizeof(ap_config_file), res)) {
developere40952c2023-06-15 18:46:43 +08004324 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4325 return RETURN_ERR;
4326 }
4327
developer8078acf2023-08-04 18:52:48 +08004328 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "rm %s", ap_config_file);
4329 if (res) {
4330 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08004331 }
4332
developer72fb0bb2023-01-11 09:46:29 +08004333
developer47cc27a2023-05-17 23:09:58 +08004334 memset(ret_buf, 0, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004335
developer47cc27a2023-05-17 23:09:58 +08004336 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
4337
4338 /*prepare new config file*/
developer8078acf2023-08-04 18:52:48 +08004339
4340 res = _syscmd_secure(ret_buf, sizeof(ret_buf), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], ap_config_file);
4341 if (res) {
4342 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08004343 }
4344
developer47cc27a2023-05-17 23:09:58 +08004345
4346 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08004347 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
4348 if (os_snprintf_error(sizeof(ssid), res)) {
4349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4350 return RETURN_ERR;
4351 }
4352
4353 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
4354 if (os_snprintf_error(sizeof(interface), res)) {
4355 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4356 return RETURN_ERR;
4357 }
developer47cc27a2023-05-17 23:09:58 +08004358 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08004359 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
4360 if (os_snprintf_error(sizeof(ssid), res)) {
4361 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4362 return RETURN_ERR;
4363 }
4364
4365 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
4366 if (os_snprintf_error(sizeof(interface), res)) {
4367 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4368 return RETURN_ERR;
4369 }
developer47cc27a2023-05-17 23:09:58 +08004370 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08004371 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
4372 if (os_snprintf_error(sizeof(ssid), res)) {
4373 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4374 return RETURN_ERR;
4375 }
4376
4377 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
4378 if (os_snprintf_error(sizeof(interface), res)) {
4379 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4380 return RETURN_ERR;
4381 }
developer47cc27a2023-05-17 23:09:58 +08004382 }
4383
4384 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08004385 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", apIndex);
4386 if (os_snprintf_error(sizeof(psk_file), res)) {
4387 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4388 return RETURN_ERR;
4389 }
developer47cc27a2023-05-17 23:09:58 +08004390
4391 params[0].name = "ssid";
4392 params[0].value = ssid;
4393 params[1].name = "interface";
4394 params[1].value = interface;
4395 params[2].name = "wpa_psk_file";
4396 params[2].value = psk_file;
4397
4398 wifi_hostapdWrite(ap_config_file, params, 3);
4399
4400 /*clear psk file*/
developer8078acf2023-08-04 18:52:48 +08004401
developer47cc27a2023-05-17 23:09:58 +08004402 memset(ret_buf, 0, sizeof(ret_buf));
4403
developere40952c2023-06-15 18:46:43 +08004404 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, apIndex);
4405 if (os_snprintf_error(sizeof(psk_file), res)) {
4406 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4407 return RETURN_ERR;
4408 }
developer47cc27a2023-05-17 23:09:58 +08004409
4410 if (access(psk_file, F_OK) != 0) {
developer8078acf2023-08-04 18:52:48 +08004411 res = _syscmd_secure(ret_buf,sizeof(ret_buf), "touch %s", psk_file);
4412 if (res) {
4413 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08004414 }
4415
developer47cc27a2023-05-17 23:09:58 +08004416 } else {
developer8078acf2023-08-04 18:52:48 +08004417
4418 res = _syscmd_secure(ret_buf,sizeof(ret_buf), "echo '' > %s", psk_file);
4419 if (res) {
4420 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08004421 }
4422
developer47cc27a2023-05-17 23:09:58 +08004423 }
4424
developer429ba832023-05-31 11:03:35 +08004425 wifi_setApEnable(apIndex, FALSE);
4426 wifi_setApEnable(apIndex, TRUE);
developer47cc27a2023-05-17 23:09:58 +08004427 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4428
4429 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004430}
4431
developer72fb0bb2023-01-11 09:46:29 +08004432INT wifi_setBandSteeringApGroup(char *ApGroup)
4433{
developera3511852023-06-14 14:12:59 +08004434 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004435}
4436
4437INT wifi_getApDTIMInterval(INT apIndex, INT *dtimInterval)
4438{
developera3511852023-06-14 14:12:59 +08004439 char config_file[128] = {'\0'};
4440 char buf[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004441 int res;
developerc14d83a2023-06-29 20:09:42 +08004442 long int tmp;
developer5b23cd02023-07-19 20:26:03 +08004443
developera3511852023-06-14 14:12:59 +08004444 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4445 if (dtimInterval == NULL)
4446 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004447
4448 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4449 if (os_snprintf_error(sizeof(config_file), res)) {
4450 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4451 return RETURN_ERR;
4452 }
developer72fb0bb2023-01-11 09:46:29 +08004453
developera3511852023-06-14 14:12:59 +08004454 wifi_hostapdRead(config_file, "dtim_period", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08004455
developera3511852023-06-14 14:12:59 +08004456 if (strlen(buf) == 0) {
4457 *dtimInterval = 2;
4458 } else {
developerc14d83a2023-06-29 20:09:42 +08004459 if (hal_strtol(buf, 10, &tmp) < 0) {
4460 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08004461 }
developerc14d83a2023-06-29 20:09:42 +08004462 *dtimInterval = tmp;
developera3511852023-06-14 14:12:59 +08004463 }
developer72fb0bb2023-01-11 09:46:29 +08004464
developera3511852023-06-14 14:12:59 +08004465 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4466 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004467}
4468
4469INT wifi_setApDTIMInterval(INT apIndex, INT dtimInterval)
4470{
developera3511852023-06-14 14:12:59 +08004471 struct params params={0};
4472 char config_file[MAX_BUF_SIZE] = {'\0'};
4473 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004474 int res;
developer72fb0bb2023-01-11 09:46:29 +08004475
developera3511852023-06-14 14:12:59 +08004476 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4477 if (dtimInterval < 1 || dtimInterval > 255) {
4478 WIFI_ENTRY_EXIT_DEBUG("Invalid dtimInterval: %d\n", dtimInterval);
4479 return RETURN_ERR;
4480 }
developer69b61b02023-03-07 17:17:44 +08004481
developera3511852023-06-14 14:12:59 +08004482 params.name = "dtim_period";
developere40952c2023-06-15 18:46:43 +08004483 res = snprintf(buf, sizeof(buf), "%d", dtimInterval);
4484 if (os_snprintf_error(sizeof(buf), res)) {
4485 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4486 return RETURN_ERR;
4487 }
developera3511852023-06-14 14:12:59 +08004488 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08004489
developer75bd10c2023-06-27 11:34:08 +08004490 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4491 if (os_snprintf_error(sizeof(config_file), res)) {
4492 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4493 return RETURN_ERR;
4494 }
developera3511852023-06-14 14:12:59 +08004495 wifi_hostapdWrite(config_file, &params, 1);
4496 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08004497
developera3511852023-06-14 14:12:59 +08004498 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4499 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004500}
4501
4502//Check if the driver support the Dfs
4503INT wifi_getRadioDfsSupport(INT radioIndex, BOOL *output_bool) //Tr181
4504{
developera3511852023-06-14 14:12:59 +08004505 wifi_band band = band_invalid;
4506 if (NULL == output_bool)
4507 return RETURN_ERR;
4508 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +08004509
developera3511852023-06-14 14:12:59 +08004510 band = wifi_index_to_band(radioIndex);
4511 if (band == band_5)
4512 *output_bool = TRUE;
4513 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004514}
4515
4516//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.
4517//The value of this parameter is a comma seperated list of channel number
4518INT wifi_getRadioDCSChannelPool(INT radioIndex, CHAR *output_pool) //RDKB
4519{
developer326d4232023-06-15 16:45:30 +08004520
4521 #define CHANNEL_AVAILABLE 0
4522 #define CHANNEL_INVALID 1
4523 #define CHANNEL_LIST_MAX_LENGTH 256
4524 #define MAX_CHANNEL_NUMBER 255
4525
4526 char config_file[MAX_BUF_SIZE] = {0};
4527 char possible_channels[CHANNEL_LIST_MAX_LENGTH] = {0};
4528 char skip_list[CHANNEL_LIST_MAX_LENGTH] = {0};
4529 int skip_table[MAX_CHANNEL_NUMBER +1] = {0};
4530 wifi_band band = band_invalid;
4531 char *token_channel = NULL, *token_skip = NULL;
developer75bd10c2023-06-27 11:34:08 +08004532 int res;
developere40952c2023-06-15 18:46:43 +08004533
developera3511852023-06-14 14:12:59 +08004534 if (NULL == output_pool)
4535 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004536 // get skiplist, possible_channels list
4537 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4538 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004539 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4540 if (os_snprintf_error(sizeof(config_file), res)) {
4541 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4542 return RETURN_ERR;
4543 }
developer326d4232023-06-15 16:45:30 +08004544 wifi_datfileRead(config_file, "AutoChannelSkipList", skip_list, sizeof(skip_list));
4545
4546 if (skip_list[0] != '\0') {
4547 int len = strlen(skip_list);
4548 for (int i = 0; i < len; i++) {
4549 if (skip_list[i] == ';') {
4550 skip_list[i] = ',';
4551 }
4552 }
4553 // skip list
4554 token_skip = strtok(skip_list, ",");
4555 while (token_skip != NULL) {
4556 int channel = atoi(token_skip);
4557 if (channel <= MAX_CHANNEL_NUMBER && strstr(possible_channels, token_skip) != NULL)
4558 skip_table[atoi(token_skip)] = CHANNEL_INVALID;
4559 token_skip = strtok(NULL, ",");
4560 }
developere40952c2023-06-15 18:46:43 +08004561 }
developer72fb0bb2023-01-11 09:46:29 +08004562
developer326d4232023-06-15 16:45:30 +08004563 int count = 0;
4564 token_channel = strtok(possible_channels, ",");
4565 while (token_channel != NULL) {
4566 int channel = atoi(token_channel);
4567 if (channel <= MAX_CHANNEL_NUMBER && skip_table[channel] == CHANNEL_AVAILABLE) {
4568 count += snprintf(&output_pool[count], CHANNEL_LIST_MAX_LENGTH-count, "%d,", channel);
4569 if (count >= CHANNEL_LIST_MAX_LENGTH-1)
4570 break;
4571 }
4572 token_channel = strtok(NULL, ",");
4573 }
4574 //delete the last one ','
4575 if (count >0 && output_pool[count-1] == ',')
4576 output_pool[count-1] = '\0';
developera3511852023-06-14 14:12:59 +08004577 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004578}
4579
4580INT wifi_setRadioDCSChannelPool(INT radioIndex, CHAR *pool) //RDKB
4581{
developer326d4232023-06-15 16:45:30 +08004582 char config_file_dat[128] = {0};
4583 struct params dat = {0};
4584 wifi_band band = band_invalid;
4585 char new_pool[128] = {0};
developer75bd10c2023-06-27 11:34:08 +08004586 int res;
developer326d4232023-06-15 16:45:30 +08004587
4588 if (NULL == pool)
4589 return RETURN_ERR;
4590
developer9ce44382023-06-28 11:09:37 +08004591 strncpy(new_pool, pool, sizeof(new_pool) - 1);
4592 new_pool[sizeof(new_pool) - 1] = '\0';
developer326d4232023-06-15 16:45:30 +08004593 for (int i = 0; new_pool[i] != '\0'; i++) {
4594 if (new_pool[i] == ',')
4595 new_pool[i] = ';';
4596 }
4597
4598 dat.name = "AutoChannelSkipList";
4599 dat.value = new_pool;
4600 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004601 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4602 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4603 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4604 return RETURN_ERR;
4605 }
developer326d4232023-06-15 16:45:30 +08004606 if (wifi_datfileWrite(config_file_dat, &dat, 1) != 0)
4607 return RETURN_ERR;
4608 wifi_reloadAp(radioIndex);
4609
developera3511852023-06-14 14:12:59 +08004610 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004611}
4612
4613INT wifi_getRadioDCSScanTime(INT radioIndex, INT *output_interval_seconds, INT *output_dwell_milliseconds)
4614{
developera3511852023-06-14 14:12:59 +08004615 if (NULL == output_interval_seconds || NULL == output_dwell_milliseconds)
4616 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004617 //Should refresh period time be filled in here? output_interval_seconds is INT type
4618 //wifi_getRadioAutoChannelRefreshPeriod is Ulong type
4619 *output_interval_seconds = 1800;
4620 *output_dwell_milliseconds = 200;
developer72fb0bb2023-01-11 09:46:29 +08004621
developera3511852023-06-14 14:12:59 +08004622 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004623}
4624
4625INT wifi_setRadioDCSScanTime(INT radioIndex, INT interval_seconds, INT dwell_milliseconds)
4626{
developera3511852023-06-14 14:12:59 +08004627 //Set to wifi config. And apply instantly.
4628 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004629}
4630
4631INT wifi_getRadioDfsAtBootUpEnable(INT radioIndex, BOOL *output_bool) //Tr181
4632{
developera3511852023-06-14 14:12:59 +08004633 if (output_bool == NULL)
4634 return RETURN_ERR;
4635 *output_bool = true;
4636 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004637}
4638
4639INT wifi_setRadioDfsAtBootUpEnable(INT radioIndex, BOOL enable) //Tr181
4640{
developera3511852023-06-14 14:12:59 +08004641 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004642}
4643
4644//Get the Dfs enable status
4645INT wifi_getRadioDfsEnable(INT radioIndex, BOOL *output_bool) //Tr181
4646{
developera3511852023-06-14 14:12:59 +08004647 char buf[16] = {0};
4648 char config_file_dat[128] = {0};
4649 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004650 int res;
developer72fb0bb2023-01-11 09:46:29 +08004651
developera3511852023-06-14 14:12:59 +08004652 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004653
developera3511852023-06-14 14:12:59 +08004654 if (output_bool == NULL)
4655 return RETURN_ERR;
4656 *output_bool = TRUE; // default
4657 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004658 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4659 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4660 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4661 return RETURN_ERR;
4662 }
developerd1824452023-05-18 12:30:04 +08004663
developera3511852023-06-14 14:12:59 +08004664 wifi_datfileRead(config_file_dat, "DfsEnable", buf, sizeof(buf));
developerd1824452023-05-18 12:30:04 +08004665
developera3511852023-06-14 14:12:59 +08004666 if (strncmp(buf, "0", 1) == 0)
4667 *output_bool = FALSE;
4668 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4669 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004670}
4671
4672//Set the Dfs enable status
4673INT wifi_setRadioDfsEnable(INT radioIndex, BOOL enable) //Tr181
4674{
developera3511852023-06-14 14:12:59 +08004675 char config_dat_file[128] = {0};
4676 FILE *f = NULL;
4677 struct params dat = {0};
4678 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08004679 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +08004680
developera3511852023-06-14 14:12:59 +08004681 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004682
developera3511852023-06-14 14:12:59 +08004683 f = fopen(DFS_ENABLE_FILE, "w");
4684 if (f == NULL)
4685 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08004686 ret = fprintf(f, "%d", enable);
4687 if (ret < 0)
4688 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developerd14dff12023-06-28 22:47:44 +08004689 if (fclose(f) != 0) {
4690 wifi_debug(DEBUG_ERROR, "fclose fail\n");
4691 return RETURN_ERR;
4692 }
developer72fb0bb2023-01-11 09:46:29 +08004693
developera3511852023-06-14 14:12:59 +08004694 wifi_setRadioIEEE80211hEnabled(radioIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08004695
developera3511852023-06-14 14:12:59 +08004696 dat.name = "DfsEnable";
4697 dat.value = enable?"1":"0";
4698 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004699 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4700 if (os_snprintf_error(sizeof(config_dat_file), res)) {
4701 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4702 return RETURN_ERR;
4703 }
4704
developera3511852023-06-14 14:12:59 +08004705 wifi_datfileWrite(config_dat_file, &dat, 1);
4706 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4707 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004708}
4709
4710//Check if the driver support the AutoChannelRefreshPeriod
4711INT wifi_getRadioAutoChannelRefreshPeriodSupported(INT radioIndex, BOOL *output_bool) //Tr181
4712{
developera3511852023-06-14 14:12:59 +08004713 if (NULL == output_bool)
4714 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004715 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004716
developera3511852023-06-14 14:12:59 +08004717 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004718}
4719
developer326d4232023-06-15 16:45:30 +08004720
4721int get_ACS_RefreshPeriod_callback(struct nl_msg *msg, void *arg)
4722{
4723 ULONG *data = (ULONG *)arg;
4724 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4725 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
4726 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4727 int err = 0;
4728
4729 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4730 genlmsg_attrlen(gnlh, 0), NULL);
4731 if (err < 0)
4732 return NL_SKIP;
4733
4734 if (tb[NL80211_ATTR_VENDOR_DATA]) {
4735 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
4736 tb[NL80211_ATTR_VENDOR_DATA], NULL);
4737 if (err < 0)
4738 return NL_SKIP;
4739
4740 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]) {
4741 *data = nla_get_u32(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]);
4742 }
4743 }
4744
4745 return NL_OK;
4746}
4747
developer72fb0bb2023-01-11 09:46:29 +08004748//Get the ACS refresh period in seconds
4749INT wifi_getRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG *output_ulong) //Tr181
4750{
developer326d4232023-06-15 16:45:30 +08004751 char interface_name[IF_NAME_SIZE] = {0};
4752 int ret = -1;
4753 unsigned int if_idx = 0;
4754 struct unl unl_ins;
4755 struct nl_msg *msg = NULL;
4756 struct nlattr * msg_data = NULL;
4757 struct mtk_nl80211_param param;
4758 unsigned long checktime = 0;
4759
4760 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004761 if (NULL == output_ulong)
4762 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004763
4764 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4765 return RETURN_ERR;
4766
4767 if_idx = if_nametoindex(interface_name);
4768 if (!if_idx) {
4769 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4770 return RETURN_ERR;
4771 }
4772 /*init mtk nl80211 vendor cmd*/
4773 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
4774 param.if_type = NL80211_ATTR_IFINDEX;
4775 param.if_idx = if_idx;
developer72fb0bb2023-01-11 09:46:29 +08004776
developer326d4232023-06-15 16:45:30 +08004777 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4778 if (ret) {
4779 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4780 return RETURN_ERR;
4781 }
4782
4783 /*add mtk vendor cmd data*/
4784 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD, 0)) {
4785 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD attribute error\n");
4786 nlmsg_free(msg);
4787 goto err;
4788 }
4789
4790 /*send mtk nl80211 vendor msg*/
4791 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_ACS_RefreshPeriod_callback, &checktime);
4792
4793 if (ret) {
4794 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4795 goto err;
4796 }
4797 /*deinit mtk nl80211 vendor msg*/
4798 mtk_nl80211_deint(&unl_ins);
4799 *output_ulong = checktime;
4800 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4801
4802 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004803 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004804err:
4805 mtk_nl80211_deint(&unl_ins);
4806 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
4807 return RETURN_ERR;
4808
developer72fb0bb2023-01-11 09:46:29 +08004809}
4810
4811//Set the ACS refresh period in seconds
4812INT wifi_setRadioDfsRefreshPeriod(INT radioIndex, ULONG seconds) //Tr181
4813{
developer326d4232023-06-15 16:45:30 +08004814 char interface_name[IF_NAME_SIZE] = {0};
4815 int ret = -1;
4816 unsigned int if_idx = 0;
4817 struct unl unl_ins;
4818 struct nl_msg *msg = NULL;
4819 struct nlattr * msg_data = NULL;
4820 struct mtk_nl80211_param param;
4821
4822 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4823
4824 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4825 return RETURN_ERR;
4826
4827 if_idx = if_nametoindex(interface_name);
4828 if (!if_idx) {
4829 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4830 return RETURN_ERR;
4831 }
4832 /*init mtk nl80211 vendor cmd*/
4833 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AUTO_CH_SEL;
4834 param.if_type = NL80211_ATTR_IFINDEX;
4835 param.if_idx = if_idx;
4836
4837 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4838 if (ret) {
4839 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4840 return RETURN_ERR;
4841 }
4842
4843 /*add mtk vendor cmd data*/
4844 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME, seconds)) {
4845 wifi_debug(DEBUG_ERROR, "Nla put MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME attribute error\n");
4846 nlmsg_free(msg);
4847 goto err;
4848 }
4849
4850 /*send mtk nl80211 vendor msg*/
4851 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
4852
4853 if (ret) {
4854 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4855 goto err;
4856 }
4857 /*deinit mtk nl80211 vendor msg*/
4858 mtk_nl80211_deint(&unl_ins);
4859 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4860
4861 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4862 return RETURN_OK;
4863err:
4864 mtk_nl80211_deint(&unl_ins);
4865 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developera3511852023-06-14 14:12:59 +08004866 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004867}
4868
4869//Get the Operating Channel Bandwidth. eg "20MHz", "40MHz", "80MHz", "80+80", "160"
4870//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.
4871INT wifi_getRadioOperatingChannelBandwidth(INT radioIndex, CHAR *output_string) //Tr181
4872{
developera3511852023-06-14 14:12:59 +08004873 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
4874 char extchannel[128] = {0};
4875 char interface_name[64] = {0};
developere40952c2023-06-15 18:46:43 +08004876 int ret = 0, len=0, res;
developera3511852023-06-14 14:12:59 +08004877 BOOL radio_enable = FALSE;
4878 wifi_band band;
developer72fb0bb2023-01-11 09:46:29 +08004879
developera3511852023-06-14 14:12:59 +08004880 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004881
developera3511852023-06-14 14:12:59 +08004882 if (NULL == output_string) {
4883 WIFI_ENTRY_EXIT_DEBUG("output_string is nuill %s: %d \n", __func__, __LINE__);
4884 return RETURN_ERR;
4885 }
4886 if (wifi_getRadioEnable(radioIndex, &radio_enable) == RETURN_ERR) {
4887 WIFI_ENTRY_EXIT_DEBUG("wifi_getRadioEnable failed %s: %d \n", __func__, __LINE__);
4888 return RETURN_ERR;
4889 }
4890 if (radio_enable != TRUE) {
4891 WIFI_ENTRY_EXIT_DEBUG("Radio %d is not enable failed %s: %d \n", radioIndex, __func__, __LINE__);
4892 return RETURN_OK;
4893 }
4894 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4895 return RETURN_ERR;
4896 /*IW command get BW320 to do*/
developerd1824452023-05-18 12:30:04 +08004897
developere40952c2023-06-15 18:46:43 +08004898 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'width' | cut -d ' ' -f6 | tr -d '\\n'", interface_name);
4899 if (os_snprintf_error(sizeof(cmd), res)) {
4900 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4901 return RETURN_ERR;
4902 }
developer8078acf2023-08-04 18:52:48 +08004903
4904 ret = _syscmd_secure(buf, sizeof(buf), "iw dev %s info | grep 'width' | cut -d ' ' -f6 | tr -d '\\n'", interface_name);
4905 if(ret) {
4906 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
4907 }
4908
developera3511852023-06-14 14:12:59 +08004909 len = strlen(buf);
4910 if((ret != 0) || (len == 0))
4911 {
4912 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
4913 return RETURN_ERR;
4914 }
developer8666b312023-03-24 14:05:31 +08004915
developera3511852023-06-14 14:12:59 +08004916 band = wifi_index_to_band(radioIndex);
4917 if (band == band_2_4 && strncmp(buf, "20", 2) == 0) {
4918 wifi_getRadioExtChannel(radioIndex, extchannel);
developere40952c2023-06-15 18:46:43 +08004919 if (strncmp(extchannel, "Auto", 4) != 0) {
4920 res = snprintf(buf, sizeof(buf), "40");
4921 if (os_snprintf_error(sizeof(buf), res)) {
4922 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4923 return RETURN_ERR;
4924 }
4925 }
4926 }
4927 res = snprintf(output_string, 64, "%sMHz", buf);
4928 if (os_snprintf_error(64, res)) {
4929 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4930 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08004931 }
developera3511852023-06-14 14:12:59 +08004932 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004933
developera3511852023-06-14 14:12:59 +08004934 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08004935}
4936
4937enum mwctl_chan_width {
4938 MWCTL_CHAN_WIDTH_20,
4939 MWCTL_CHAN_WIDTH_40,
4940 MWCTL_CHAN_WIDTH_80,
4941 MWCTL_CHAN_WIDTH_160,
4942 MWCTL_CHAN_WIDTH_320,
4943};
4944
4945struct bw_option {
4946 unsigned int bandwith;
4947 enum mwctl_chan_width mode;
4948};
4949
4950struct bw_option bw_opt[] = {
4951 {20, MWCTL_CHAN_WIDTH_20},
4952 {40, MWCTL_CHAN_WIDTH_40},
4953 {80, MWCTL_CHAN_WIDTH_80},
4954 {160, MWCTL_CHAN_WIDTH_160},
4955 {320, MWCTL_CHAN_WIDTH_320},
4956};
4957
4958INT wifi_setChannel_netlink(INT radioIndex, UINT* channel, UINT *bandwidth)
4959{
4960 int ret = -1;
4961 int i;
4962 struct unl unl_ins;
4963 struct nl_msg *msg = NULL;
4964 struct nlattr * msg_data = NULL;
4965 struct mtk_nl80211_param param;
4966 bool b_match = FALSE;
4967
4968 /*init mtk nl80211 vendor cmd*/
4969 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_CHANNEL;
4970 param.if_type = NL80211_ATTR_WIPHY;
4971 param.if_idx = radio_index_to_phy(radioIndex);
4972
4973 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4974 if (ret) {
4975 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4976 return RETURN_ERR;
4977 }
4978
4979 /*add mtk vendor cmd data*/
4980 if (channel != NULL)
4981 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_NUM, *channel)) {
4982 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_NUM attribute error\n");
4983 nlmsg_free(msg);
4984 goto err;
4985 }
4986
4987 if (bandwidth != NULL) {
4988 for (i = 0; i < (sizeof(bw_opt)/sizeof(bw_opt[0])); i++) {
4989 if (bw_opt[i].bandwith == *bandwidth) {
4990 b_match = true;
4991 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_BW, bw_opt[i].mode)) {
4992 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_BW attribute error\n");
4993 nlmsg_free(msg);
4994 goto err;
4995 }
4996 break;
4997 }
4998 }
4999
5000 if (!b_match) {
5001 wifi_debug(DEBUG_ERROR, "Cannot find bandwith error\n");
5002 nlmsg_free(msg);
5003 goto err;
5004 }
5005 }
5006
5007 /*send mtk nl80211 vendor msg*/
5008 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5009 if (ret) {
5010 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5011 goto err;
5012 }
5013 /*deinit mtk nl80211 vendor msg*/
5014 mtk_nl80211_deint(&unl_ins);
5015 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developera3511852023-06-14 14:12:59 +08005016 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developerfead3972023-05-25 20:15:02 +08005017
developera3511852023-06-14 14:12:59 +08005018 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005019err:
5020 mtk_nl80211_deint(&unl_ins);
5021 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5022 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005023}
developerfead3972023-05-25 20:15:02 +08005024
developer72fb0bb2023-01-11 09:46:29 +08005025//Set the Operating Channel Bandwidth.
5026INT wifi_setRadioOperatingChannelBandwidth(INT radioIndex, CHAR *bandwidth) //Tr181 //AP only
5027{
developera3511852023-06-14 14:12:59 +08005028 char config_file[128];
5029 char ht_value[16];
5030 char vht_value[16];
5031 char eht_value[16];
5032 struct params dat[3];
5033 wifi_band band = band_invalid;
5034 unsigned int bw = 20;
developere40952c2023-06-15 18:46:43 +08005035 int ret = 0, res1, res2, res3;
developer72fb0bb2023-01-11 09:46:29 +08005036
developera3511852023-06-14 14:12:59 +08005037 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005038
developera3511852023-06-14 14:12:59 +08005039 if(NULL == bandwidth)
5040 return RETURN_ERR;
5041 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08005042
developera3511852023-06-14 14:12:59 +08005043 if(strstr(bandwidth,"320") != NULL) {
developere40952c2023-06-15 18:46:43 +08005044 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5045 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5046 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_320);
developera3511852023-06-14 14:12:59 +08005047 bw = 320;
5048 } else if(strstr(bandwidth,"160") != NULL) {
developere40952c2023-06-15 18:46:43 +08005049 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5050 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5051 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_160);
developera3511852023-06-14 14:12:59 +08005052 bw = 160;
5053 } else if(strstr(bandwidth,"80") != NULL) {
developere40952c2023-06-15 18:46:43 +08005054 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5055 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_80);
5056 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_80);
developera3511852023-06-14 14:12:59 +08005057 bw = 80;
5058 } else if(strstr(bandwidth,"40") != NULL) {
developere40952c2023-06-15 18:46:43 +08005059 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5060 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5061 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_40);
developera3511852023-06-14 14:12:59 +08005062 bw = 40;
5063 } else if(strstr(bandwidth,"20") != NULL) {
developere40952c2023-06-15 18:46:43 +08005064 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_20);
5065 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5066 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_20);
developera3511852023-06-14 14:12:59 +08005067 bw = 20;
5068 } else {
developer37646972023-06-29 10:58:43 +08005069 if (fprintf(stderr, "%s: Invalid Bandwidth %s\n", __func__, bandwidth) < 0)
5070 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005071 return RETURN_ERR;
5072 }
developer72fb0bb2023-01-11 09:46:29 +08005073
developer37646972023-06-29 10:58:43 +08005074 if (os_snprintf_error(sizeof(ht_value), res1) ||
5075 os_snprintf_error(sizeof(vht_value), res2) ||
5076 os_snprintf_error(sizeof(eht_value), res3)) {
developere40952c2023-06-15 18:46:43 +08005077 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5078 return RETURN_ERR;
5079 }
5080
5081 res1 = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5082 if (os_snprintf_error(sizeof(config_file), res1)) {
5083 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5084 return RETURN_ERR;
5085 }
developera3511852023-06-14 14:12:59 +08005086 dat[0].name = "HT_BW";
5087 dat[0].value = ht_value;
5088 dat[1].name = "VHT_BW";
5089 dat[1].value = vht_value;
5090 dat[2].name = "EHT_ApBw";
5091 dat[2].value = eht_value;
5092 wifi_datfileWrite(config_file, dat, 3);
5093 ret = wifi_setChannel_netlink(radioIndex, NULL, &bw);
developerfead3972023-05-25 20:15:02 +08005094 if (ret != RETURN_OK) {
developer37646972023-06-29 10:58:43 +08005095 if (fprintf(stderr, "%s: wifi_setChannel return error.\n", __func__) < 0)
5096 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005097 return RETURN_ERR;
5098 }
developer72fb0bb2023-01-11 09:46:29 +08005099
developera3511852023-06-14 14:12:59 +08005100 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5101 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005102}
5103
developer72fb0bb2023-01-11 09:46:29 +08005104//Get the secondary extension channel position, "AboveControlChannel" or "BelowControlChannel". (this is for 40MHz and 80MHz bandwith only)
5105//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.
5106INT wifi_getRadioExtChannel(INT radioIndex, CHAR *output_string) //Tr181
5107{
developera3511852023-06-14 14:12:59 +08005108 char config_file[64] = {0};
5109 char config_dat_file[64] = {0};
5110 char mode_str[16] = {0};
5111 char buf[64] = {0};
5112 char cmd[MAX_CMD_SIZE] = {0};
5113 char interface_name[64] = {0};
5114 int ret = 0, len=0;
5115 wifi_band band;
5116 ULONG channel = 0;
5117 int centr_channel = 0;
5118 UINT mode_map = 0;
developere40952c2023-06-15 18:46:43 +08005119 int freq=0, res;
developer72fb0bb2023-01-11 09:46:29 +08005120
developera3511852023-06-14 14:12:59 +08005121 if (output_string == NULL)
5122 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005123
developer86035662023-06-28 19:21:12 +08005124 if (wifi_getRadioMode(radioIndex, mode_str, &mode_map) != RETURN_OK) {
5125 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode fail\n");
5126 }
developer72fb0bb2023-01-11 09:46:29 +08005127
developera3511852023-06-14 14:12:59 +08005128 band = wifi_index_to_band(radioIndex);
5129 if (band == band_invalid)
5130 return RETURN_ERR;
5131 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5132 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005133
developere40952c2023-06-15 18:46:43 +08005134 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5135 if (os_snprintf_error(sizeof(config_file), res)) {
5136 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5137 return RETURN_ERR;
5138 }
5139
5140 res = snprintf(output_string, 64, "Auto");
5141 if (os_snprintf_error(64, res)) {
5142 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5143 return RETURN_ERR;
5144 }
developer72fb0bb2023-01-11 09:46:29 +08005145
developera3511852023-06-14 14:12:59 +08005146 if (band == band_2_4 || (!(mode_map&WIFI_MODE_AC) && !(mode_map&WIFI_MODE_AX))) {
5147 // 2G band or ac and ax mode is disable, we will check HT_EXTCHA
developere40952c2023-06-15 18:46:43 +08005148 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5149 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5150 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5151 return RETURN_ERR;
5152 }
developerb758dfd2023-06-21 17:32:07 +08005153
developera3511852023-06-14 14:12:59 +08005154 wifi_halgetRadioExtChannel(config_dat_file, output_string);
developer37646972023-06-29 10:58:43 +08005155 if (!(mode_map&WIFI_MODE_N)) {
developere40952c2023-06-15 18:46:43 +08005156 res = snprintf(output_string, 64, "Auto");
developer37646972023-06-29 10:58:43 +08005157 if (os_snprintf_error(64, res)) {
5158 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5159 return RETURN_ERR;
5160 }
5161 }
developera3511852023-06-14 14:12:59 +08005162 } else {
5163 // 5G and 6G band with ac or ax mode.
5164 wifi_getRadioChannel(radioIndex, &channel);
developere40952c2023-06-15 18:46:43 +08005165 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'center1' | cut -d ' ' -f9 | tr -d '\\n'", interface_name);
5166 if (os_snprintf_error(sizeof(cmd), res)) {
5167 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5168 return RETURN_ERR;
5169 }
developerd1824452023-05-18 12:30:04 +08005170
developer8078acf2023-08-04 18:52:48 +08005171 ret = _syscmd_secure(buf, sizeof(buf), "iw dev %s info | grep 'center1' | cut -d ' ' -f9 | tr -d '\\n'", interface_name);
5172 if(ret) {
5173 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
5174 }
5175
developera3511852023-06-14 14:12:59 +08005176 len = strlen(buf);
5177 if((ret != 0) || (len == 0))
5178 {
5179 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
5180 return RETURN_ERR;
5181 }
5182 sscanf(buf, "%d", &freq);
5183 centr_channel = ieee80211_frequency_to_channel(freq);
5184 if (centr_channel > (int)channel)
developere40952c2023-06-15 18:46:43 +08005185 res = snprintf(output_string, 64, "AboveControlChannel");
developera3511852023-06-14 14:12:59 +08005186 else
developere40952c2023-06-15 18:46:43 +08005187 res = snprintf(output_string, 64, "BelowControlChannel");
5188
5189 if (os_snprintf_error(64, res)) {
5190 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5191 return RETURN_ERR;
5192 }
developera3511852023-06-14 14:12:59 +08005193 }
developer72fb0bb2023-01-11 09:46:29 +08005194
developera3511852023-06-14 14:12:59 +08005195 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005196}
5197
5198//Set the extension channel.
5199INT wifi_setRadioExtChannel(INT radioIndex, CHAR *string) //Tr181 //AP only
developer69b61b02023-03-07 17:17:44 +08005200{
developera3511852023-06-14 14:12:59 +08005201 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5202 struct params params={0};
5203 char config_file[64] = {0};
5204 char config_dat_file[64] = {0};
5205 char ext_channel[64] = {0};
5206 char buf[128] = {0};
developer8078acf2023-08-04 18:52:48 +08005207
developerc14d83a2023-06-29 20:09:42 +08005208 int max_radio_num =0, ret = 0;
5209 long int bandwidth = 0;
developera3511852023-06-14 14:12:59 +08005210 unsigned long channel = 0;
5211 bool stbcEnable = FALSE;
5212 params.name = "ht_capab";
5213 wifi_band band;
developere40952c2023-06-15 18:46:43 +08005214 int res;
developer72fb0bb2023-01-11 09:46:29 +08005215
developer75bd10c2023-06-27 11:34:08 +08005216 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5217 if (os_snprintf_error(sizeof(config_file), res)) {
5218 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5219 return RETURN_ERR;
5220 }
5221
developer8078acf2023-08-04 18:52:48 +08005222 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep STBC", config_file);
5223 if(res) {
5224 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08005225 }
developer8078acf2023-08-04 18:52:48 +08005226
developera3511852023-06-14 14:12:59 +08005227 if (strlen(buf) != 0)
5228 stbcEnable = TRUE;
5229 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, buf) != RETURN_OK)
5230 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08005231 if (hal_strtol(buf, 10, &bandwidth) < 0) {
5232 wifi_debug(DEBUG_ERROR, "strtol fail\n");
5233 }
developera3511852023-06-14 14:12:59 +08005234 // TDK expected to get error with 20MHz
5235 // we handle 20MHz in function wifi_RemoveRadioExtChannel().
5236 if (bandwidth == 20 || strstr(buf, "80+80") != NULL)
5237 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005238
developera3511852023-06-14 14:12:59 +08005239 band = wifi_index_to_band(radioIndex);
5240 if (band == band_invalid)
5241 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005242
developera3511852023-06-14 14:12:59 +08005243 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK)
5244 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005245
developerc14d83a2023-06-29 20:09:42 +08005246 res = snprintf(buf, sizeof(buf), "HT%ld", bandwidth);
developere40952c2023-06-15 18:46:43 +08005247 if (os_snprintf_error(sizeof(buf), res)) {
5248 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5249 return RETURN_ERR;
5250 }
developera3511852023-06-14 14:12:59 +08005251 ret = util_get_sec_chan_offset(channel, buf);
5252 if (ret == -EINVAL)
5253 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005254
developera3511852023-06-14 14:12:59 +08005255 if(NULL!= strstr(string,"Above")) {
5256 if ((band == band_2_4 && channel > 9) || (band == band_5 && ret == -1))
5257 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005258 memcpy(ext_channel, "1", strlen("1"));
developera3511852023-06-14 14:12:59 +08005259 } else if(NULL!= strstr(string,"Below")) {
5260 if ((band == band_2_4 && channel < 5) || (band == band_5 && ret == -1))
5261 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005262 memcpy(ext_channel, "0", strlen("0"));
developera3511852023-06-14 14:12:59 +08005263 } else {
5264 printf("%s: invalid EXT_CHA:%s\n", __func__, string);
developer262f4cb2023-05-24 12:22:04 +08005265 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08005266 }
5267 params.name = "HT_EXTCHA";
5268 params.value = ext_channel;
developer72fb0bb2023-01-11 09:46:29 +08005269
developere75ba632023-06-29 16:03:33 +08005270 res = snprintf (config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08005271 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5272 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5273 return RETURN_ERR;
5274 }
developera3511852023-06-14 14:12:59 +08005275 wifi_datfileWrite(config_dat_file, &params, 1);
developerd1824452023-05-18 12:30:04 +08005276
developera3511852023-06-14 14:12:59 +08005277 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08005278 if(max_radio_num== 0){
5279 return RETURN_ERR;
5280 }
developera3511852023-06-14 14:12:59 +08005281 for(int i=0; i<=MAX_APS/max_radio_num; i++)
5282 {
developer32f2a182023-06-27 19:50:41 +08005283 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex+(max_radio_num*i));
5284 if (os_snprintf_error(sizeof(config_file), res)) {
5285 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5286 return RETURN_ERR;
5287 }
developera3511852023-06-14 14:12:59 +08005288 wifi_setRadioSTBCEnable(radioIndex+(max_radio_num*i), stbcEnable);
5289 }
developer72fb0bb2023-01-11 09:46:29 +08005290
developera3511852023-06-14 14:12:59 +08005291 //Set to wifi config only. Wait for wifi reset or wifi_pushRadioChannel to apply.
5292 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5293 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005294}
5295
5296//Get the guard interval value. eg "400nsec" or "800nsec"
5297//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.
5298INT wifi_getRadioGuardInterval(INT radioIndex, CHAR *output_string) //Tr181
5299{
developera3511852023-06-14 14:12:59 +08005300 wifi_guard_interval_t GI;
developer32f2a182023-06-27 19:50:41 +08005301 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08005302
developera3511852023-06-14 14:12:59 +08005303 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005304
developera3511852023-06-14 14:12:59 +08005305 if (output_string == NULL || wifi_getGuardInterval(radioIndex, &GI) == RETURN_ERR)
5306 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005307
developer32f2a182023-06-27 19:50:41 +08005308 if (GI == wifi_guard_interval_400) {
5309 len = strlen("400nsec");
5310 memcpy(output_string, "400nsec", len);
developer5b23cd02023-07-19 20:26:03 +08005311 } else if (GI == wifi_guard_interval_800) {
developer32f2a182023-06-27 19:50:41 +08005312 len = strlen("800nsec");
5313 memcpy(output_string, "800nsec", strlen("800nsec"));
5314 } else if (GI == wifi_guard_interval_1600) {
5315 len = strlen("1600nsec");
5316 memcpy(output_string, "1600nsec", strlen("1600nsec"));
5317 } else if (GI == wifi_guard_interval_3200) {
5318 len = strlen("3200nsec");
5319 memcpy(output_string, "3200nsec", strlen("3200nsec"));
5320 } else {
5321 len = strlen("Auto");
5322 memcpy(output_string, "Auto", strlen("Auto"));
5323 }
5324 output_string[len] = '\0';
developera3511852023-06-14 14:12:59 +08005325 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5326 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005327}
5328
5329//Set the guard interval value.
5330INT wifi_setRadioGuardInterval(INT radioIndex, CHAR *string) //Tr181
5331{
developera3511852023-06-14 14:12:59 +08005332 wifi_guard_interval_t GI;
5333 int ret = 0;
developer72fb0bb2023-01-11 09:46:29 +08005334
developera3511852023-06-14 14:12:59 +08005335 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005336
developera3511852023-06-14 14:12:59 +08005337 if (strcmp(string, "400nsec") == 0)
5338 GI = wifi_guard_interval_400;
5339 else if (strcmp(string , "800nsec") == 0)
5340 GI = wifi_guard_interval_800;
5341 else if (strcmp(string , "1600nsec") == 0)
5342 GI = wifi_guard_interval_1600;
5343 else if (strcmp(string , "3200nsec") == 0)
5344 GI = wifi_guard_interval_3200;
5345 else
5346 GI = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +08005347
developera3511852023-06-14 14:12:59 +08005348 ret = wifi_setGuardInterval(radioIndex, GI);
developer72fb0bb2023-01-11 09:46:29 +08005349
developera3511852023-06-14 14:12:59 +08005350 if (ret == RETURN_ERR) {
5351 wifi_dbg_printf("%s: wifi_setGuardInterval return error\n", __func__);
5352 return RETURN_ERR;
5353 }
developer72fb0bb2023-01-11 09:46:29 +08005354
developera3511852023-06-14 14:12:59 +08005355 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5356 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005357}
5358
5359//Get the Modulation Coding Scheme index, eg: "-1", "1", "15"
5360INT wifi_getRadioMCS(INT radioIndex, INT *output_int) //Tr181
5361{
developera3511852023-06-14 14:12:59 +08005362 char buf[32]={0};
5363 char mcs_file[64] = {0};
developer8078acf2023-08-04 18:52:48 +08005364
developera3511852023-06-14 14:12:59 +08005365 UINT mode_bitmap = 0;
developere40952c2023-06-15 18:46:43 +08005366 int res;
developerc14d83a2023-06-29 20:09:42 +08005367 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08005368
developera3511852023-06-14 14:12:59 +08005369 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5370 if(output_int == NULL)
5371 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005372 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5373 if (os_snprintf_error(sizeof(mcs_file), res)) {
5374 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5375 return RETURN_ERR;
5376 }
5377
developer8078acf2023-08-04 18:52:48 +08005378 res = _syscmd_secure(buf, sizeof(buf), "cat %s 2> /dev/null", mcs_file);
5379 if (res) {
5380 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08005381 }
developer72fb0bb2023-01-11 09:46:29 +08005382
developerd14dff12023-06-28 22:47:44 +08005383 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005384 if (hal_strtol(buf, 10, &tmp) < 0) {
5385 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005386 }
developerc14d83a2023-06-29 20:09:42 +08005387 *output_int = tmp;
developerd14dff12023-06-28 22:47:44 +08005388 } else {
developera3511852023-06-14 14:12:59 +08005389 // output the max MCS for the current radio mode
5390 if (wifi_getRadioMode(radioIndex, buf, &mode_bitmap) == RETURN_ERR) {
5391 wifi_dbg_printf("%s: wifi_getradiomode return error.\n", __func__);
5392 return RETURN_ERR;
5393 }
5394 if (mode_bitmap & WIFI_MODE_AX) {
5395 *output_int = 11;
5396 } else if (mode_bitmap & WIFI_MODE_AC) {
5397 *output_int = 9;
5398 } else if (mode_bitmap & WIFI_MODE_N) {
5399 *output_int = 7;
5400 }
5401 }
5402 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005403
developera3511852023-06-14 14:12:59 +08005404 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005405}
5406
5407//Set the Modulation Coding Scheme index
5408INT wifi_setRadioMCS(INT radioIndex, INT MCS) //Tr181
5409{
developera3511852023-06-14 14:12:59 +08005410 /*Only HE mode can specify MCS capability. We don't support MCS in HT mode,
5411 because that would be ambiguous (MCS code 8~11 refer to 2 NSS in HT but 1 NSS in HE adn VHT).*/
5412 char config_file[64] = {0};
5413 char set_value[16] = {0};
5414 char mcs_file[32] = {0};
5415 struct params set_config = {0};
5416 FILE *f = NULL;
5417 INT nss = 0;
5418 int ant_bitmap = 0;
5419 unsigned short cal_value = 0;
5420 UCHAR tval = 0, i = 0;
developere40952c2023-06-15 18:46:43 +08005421 int res;
developer72fb0bb2023-01-11 09:46:29 +08005422
developera3511852023-06-14 14:12:59 +08005423 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005424
developere40952c2023-06-15 18:46:43 +08005425 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5426 if (os_snprintf_error(sizeof(config_file), res)) {
5427 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5428 return RETURN_ERR;
5429 }
developer72fb0bb2023-01-11 09:46:29 +08005430
developera3511852023-06-14 14:12:59 +08005431 // -1 means auto
5432 if (MCS > 15 || MCS < -1) {
developer75bd10c2023-06-27 11:34:08 +08005433 wifi_debug(DEBUG_ERROR, "invalid MCS %d\n", MCS);
developera3511852023-06-14 14:12:59 +08005434 return RETURN_ERR;
5435 }
5436 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);/*nss is a bit map value,1111*/
5437 for(; ant_bitmap > 0; ant_bitmap >>= 1)
5438 nss += 1;
5439 //printf("%s:nss = %d\n", __func__, nss);
5440 /*16-bit combination of 2-bit values of Max HE-MCS For 1..8 SS;each 2-bit value have following meaning:
5441 0 = HE-MCS 0-7, 1 = HE-MCS 0-9, 2 = HE-MCS 0-11, 3 = not supported*/
5442 if (MCS > 9 || MCS == -1)
5443 tval = 2;/*one stream value*/
5444 else if (MCS > 7)
5445 tval = 1;
5446 else
5447 tval = 0;
5448 for (i = 0; i < nss; i++)
5449 cal_value |= (tval << (2*i));
developere40952c2023-06-15 18:46:43 +08005450 res = snprintf(set_value, sizeof(set_value), "%x", cal_value);
5451 if (os_snprintf_error(sizeof(set_value), res)) {
5452 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5453 return RETURN_ERR;
5454 }
5455
developera3511852023-06-14 14:12:59 +08005456 WIFI_ENTRY_EXIT_DEBUG("%s:set=%s, cal=%x\n", __func__, set_value, cal_value);
5457 set_config.name = "he_basic_mcs_nss_set";/*He capability in beacon or response*/
5458 set_config.value = set_value;
developer72fb0bb2023-01-11 09:46:29 +08005459
developera3511852023-06-14 14:12:59 +08005460 wifi_hostapdWrite(config_file, &set_config, 1);
5461 wifi_hostapdProcessUpdate(radioIndex, &set_config, 1);
developer72fb0bb2023-01-11 09:46:29 +08005462
developera3511852023-06-14 14:12:59 +08005463 // 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 +08005464 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5465 if (os_snprintf_error(sizeof(mcs_file), res)) {
5466 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5467 return RETURN_ERR;
5468 }
5469
developera3511852023-06-14 14:12:59 +08005470 f = fopen(mcs_file, "w");
5471 if (f == NULL) {
developere75ba632023-06-29 16:03:33 +08005472 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
5473 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005474 return RETURN_ERR;
5475 }
developere75ba632023-06-29 16:03:33 +08005476 if (fprintf(f, "%d", MCS) < 0)
5477 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
5478 if (fclose(f) == EOF) {
5479 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
5480 return RETURN_ERR;
5481 }
developer72fb0bb2023-01-11 09:46:29 +08005482
developera3511852023-06-14 14:12:59 +08005483 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5484 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005485}
5486
5487//Get supported Transmit Power list, eg : "0,25,50,75,100"
5488//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.
5489INT wifi_getRadioTransmitPowerSupported(INT radioIndex, CHAR *output_list) //Tr181
5490{
developere40952c2023-06-15 18:46:43 +08005491 int res;
5492 if (NULL == output_list)
5493 return RETURN_ERR;
5494 res = snprintf(output_list, 64,"0,25,50,75,100");
5495 if (os_snprintf_error(64, res)) {
5496 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5497 return RETURN_ERR;
5498 }
5499
5500 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005501}
5502
5503//Get current Transmit Power in dBm units.
5504//The transmite power level is in units of full power for this radio.
5505INT wifi_getRadioTransmitPower(INT radioIndex, ULONG *output_ulong) //RDKB
5506{
developera3511852023-06-14 14:12:59 +08005507 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +08005508
developera3511852023-06-14 14:12:59 +08005509 char buf[16]={0};
developera1255e42023-05-13 17:45:02 +08005510 char pwr_file[128]={0};
developere40952c2023-06-15 18:46:43 +08005511 int res;
developera1255e42023-05-13 17:45:02 +08005512
developera3511852023-06-14 14:12:59 +08005513 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005514
developera3511852023-06-14 14:12:59 +08005515 if(output_ulong == NULL)
5516 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005517
developera3511852023-06-14 14:12:59 +08005518 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5519 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005520 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5521 if (os_snprintf_error(sizeof(pwr_file), res)) {
5522 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5523 return RETURN_ERR;
5524 }
5525
developer8078acf2023-08-04 18:52:48 +08005526 res = _syscmd_secure(buf, sizeof(buf),"cat %s 2> /dev/null", pwr_file);
5527 if(res) {
5528 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08005529 }
5530
developer5b23cd02023-07-19 20:26:03 +08005531 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005532 if (hal_strtoul(buf, 10, output_ulong) < 0) {
5533 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005534 }
5535 } else
developera1255e42023-05-13 17:45:02 +08005536 *output_ulong = 100;
developera3511852023-06-14 14:12:59 +08005537 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5538 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005539}
5540
5541//Set Transmit Power
5542//The transmite power level is in units of full power for this radio.
5543INT wifi_setRadioTransmitPower(INT radioIndex, ULONG TransmitPower) //RDKB
5544{
developera3511852023-06-14 14:12:59 +08005545 char interface_name[16] = {0};
5546 char *support;
5547 char buf[128]={0};
5548 char txpower_str[64] = {0};
developera1255e42023-05-13 17:45:02 +08005549 char pwr_file[128]={0};
developera3511852023-06-14 14:12:59 +08005550 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +08005551 int if_idx, ret = 0;
5552 struct nl_msg *msg = NULL;
5553 struct nlattr * msg_data = NULL;
5554 struct mtk_nl80211_param param;
5555 struct unl unl_ins;
developere40952c2023-06-15 18:46:43 +08005556 int res;
developer72fb0bb2023-01-11 09:46:29 +08005557
developera3511852023-06-14 14:12:59 +08005558 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005559
developera3511852023-06-14 14:12:59 +08005560 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5561 return RETURN_ERR;
5562 // Get the Tx power supported list and check that is the input in the list
developere40952c2023-06-15 18:46:43 +08005563 res = snprintf(txpower_str, sizeof(txpower_str), "%lu", TransmitPower);
5564 if (os_snprintf_error(sizeof(txpower_str), res)) {
5565 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5566 return RETURN_ERR;
5567 }
developera3511852023-06-14 14:12:59 +08005568 wifi_getRadioTransmitPowerSupported(radioIndex, buf);
5569 support = strtok(buf, ",");
5570 while(true)
5571 {
5572 if(support == NULL) { // input not in the list
5573 wifi_dbg_printf("Input value is invalid.\n");
5574 return RETURN_ERR;
5575 }
5576 if (strncmp(txpower_str, support, strlen(support)) == 0) {
5577 break;
5578 }
5579 support = strtok(NULL, ",");
5580 }
developerfead3972023-05-25 20:15:02 +08005581
5582 if_idx = if_nametoindex(interface_name);
5583 /*init mtk nl80211 vendor cmd*/
5584 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
5585 param.if_type = NL80211_ATTR_IFINDEX;
5586 param.if_idx = if_idx;
5587 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5588 if (ret) {
5589 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5590 return RETURN_ERR;
5591 }
5592 /*add mtk vendor cmd data*/
5593 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_PERCENTAGE_EN, 1)) {
5594 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5595 nlmsg_free(msg);
5596 goto err;
5597 }
5598
5599 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_DROP_CTRL, TransmitPower)) {
5600 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5601 nlmsg_free(msg);
5602 goto err;
5603 }
5604
5605 /*send mtk nl80211 vendor msg*/
5606 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5607 if (ret) {
5608 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5609 goto err;
5610 }
5611 /*deinit mtk nl80211 vendor msg*/
5612 mtk_nl80211_deint(&unl_ins);
5613 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
5614
developere40952c2023-06-15 18:46:43 +08005615 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5616 if (os_snprintf_error(sizeof(pwr_file), res)) {
5617 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5618 return RETURN_ERR;
5619 }
5620
developera3511852023-06-14 14:12:59 +08005621 f = fopen(pwr_file, "w");
5622 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +08005623 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera3511852023-06-14 14:12:59 +08005624 return RETURN_ERR;
5625 }
5626 fprintf(f, "%lu", TransmitPower);
developerc14d83a2023-06-29 20:09:42 +08005627 if (fclose(f) == EOF)
5628 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera1255e42023-05-13 17:45:02 +08005629 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005630err:
5631 mtk_nl80211_deint(&unl_ins);
5632 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5633 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005634}
5635
5636//get 80211h Supported. 80211h solves interference with satellites and radar using the same 5 GHz frequency band
5637INT wifi_getRadioIEEE80211hSupported(INT radioIndex, BOOL *Supported) //Tr181
5638{
developera3511852023-06-14 14:12:59 +08005639 if (NULL == Supported)
5640 return RETURN_ERR;
5641 *Supported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08005642
developera3511852023-06-14 14:12:59 +08005643 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005644}
5645
5646//Get 80211h feature enable
5647INT wifi_getRadioIEEE80211hEnabled(INT radioIndex, BOOL *enable) //Tr181
5648{
developera3511852023-06-14 14:12:59 +08005649 char buf[64]={'\0'};
5650 char config_file[64] = {'\0'};
developer75bd10c2023-06-27 11:34:08 +08005651 int res;
developer72fb0bb2023-01-11 09:46:29 +08005652
developera3511852023-06-14 14:12:59 +08005653 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5654 if(enable == NULL)
5655 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005656
developer75bd10c2023-06-27 11:34:08 +08005657 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5658 if (os_snprintf_error(sizeof(config_file), res)) {
5659 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5660 return RETURN_ERR;
5661 }
developera3511852023-06-14 14:12:59 +08005662 /* wifi_hostapdRead(config_file, "ieee80211h", buf, sizeof(buf)); */
5663 wifi_datfileRead(config_file, "IEEE80211H", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08005664
developera3511852023-06-14 14:12:59 +08005665 if (strncmp(buf, "1", 1) == 0)
5666 *enable = TRUE;
5667 else
5668 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08005669
developera3511852023-06-14 14:12:59 +08005670 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5671 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005672}
5673
5674//Set 80211h feature enable
5675INT wifi_setRadioIEEE80211hEnabled(INT radioIndex, BOOL enable) //Tr181
5676{
developera3511852023-06-14 14:12:59 +08005677 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5678 struct params params={'\0'};
5679 struct params dat={0};
5680 char config_file[MAX_BUF_SIZE] = {0};
5681 char config_dat_file[MAX_BUF_SIZE] = {0};
5682 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08005683 int res;
developer72fb0bb2023-01-11 09:46:29 +08005684
developera3511852023-06-14 14:12:59 +08005685 params.name = "ieee80211h";
developer72fb0bb2023-01-11 09:46:29 +08005686
developera3511852023-06-14 14:12:59 +08005687 if (enable) {
5688 params.value = "1";
5689 } else {
5690 params.value = "0";
5691 }
developer72fb0bb2023-01-11 09:46:29 +08005692
developera3511852023-06-14 14:12:59 +08005693 dat.name = "IEEE80211H";
5694 dat.value = params.value;
developerd1824452023-05-18 12:30:04 +08005695
developera3511852023-06-14 14:12:59 +08005696 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08005697 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5698 if (os_snprintf_error(sizeof(config_file), res)) {
5699 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5700 return RETURN_ERR;
5701 }
5702
5703 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5704 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5705 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5706 return RETURN_ERR;
5707 }
developer69b61b02023-03-07 17:17:44 +08005708
developera3511852023-06-14 14:12:59 +08005709 wifi_hostapdWrite(config_file, &params, 1);
5710 wifi_datfileWrite(config_dat_file, &dat, 1);
5711 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5712 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5713 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005714}
5715
5716//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.
5717INT wifi_getRadioCarrierSenseThresholdRange(INT radioIndex, INT *output) //P3
5718{
developera3511852023-06-14 14:12:59 +08005719 if (NULL == output)
5720 return RETURN_ERR;
5721 *output=100;
developer72fb0bb2023-01-11 09:46:29 +08005722
developera3511852023-06-14 14:12:59 +08005723 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005724}
5725
5726//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.
5727INT wifi_getRadioCarrierSenseThresholdInUse(INT radioIndex, INT *output) //P3
5728{
developera3511852023-06-14 14:12:59 +08005729 if (NULL == output)
5730 return RETURN_ERR;
5731 *output = -99;
developer72fb0bb2023-01-11 09:46:29 +08005732
developera3511852023-06-14 14:12:59 +08005733 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005734}
5735
5736INT wifi_setRadioCarrierSenseThresholdInUse(INT radioIndex, INT threshold) //P3
5737{
developera3511852023-06-14 14:12:59 +08005738 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005739}
5740
5741
5742//Time interval between transmitting beacons (expressed in milliseconds). This parameter is based ondot11BeaconPeriod from [802.11-2012].
5743INT wifi_getRadioBeaconPeriod(INT radioIndex, UINT *output)
5744{
developera3511852023-06-14 14:12:59 +08005745 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +08005746
developera3511852023-06-14 14:12:59 +08005747 char buf[MAX_CMD_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08005748 int res;
developer72fb0bb2023-01-11 09:46:29 +08005749
developera3511852023-06-14 14:12:59 +08005750 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5751 if(output == NULL)
5752 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005753
developera3511852023-06-14 14:12:59 +08005754 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5755 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +08005756
5757 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s status | grep beacon_int | cut -d '=' -f2 | tr -d '\n'", interface_name);
5758 if(res) {
5759 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08005760 }
5761
developera3511852023-06-14 14:12:59 +08005762 *output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +08005763
developera3511852023-06-14 14:12:59 +08005764 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5765 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005766}
developer69b61b02023-03-07 17:17:44 +08005767
developer72fb0bb2023-01-11 09:46:29 +08005768INT wifi_setRadioBeaconPeriod(INT radioIndex, UINT BeaconPeriod)
5769{
developera3511852023-06-14 14:12:59 +08005770 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5771 struct params params={'\0'};
5772 char buf[MAX_BUF_SIZE] = {'\0'};
5773 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08005774 int res;
developer72fb0bb2023-01-11 09:46:29 +08005775
developera3511852023-06-14 14:12:59 +08005776 if (BeaconPeriod < 15 || BeaconPeriod > 65535)
5777 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005778
developera3511852023-06-14 14:12:59 +08005779 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08005780 res = snprintf(buf, sizeof(buf), "%u", BeaconPeriod);
5781 if (os_snprintf_error(sizeof(buf), res)) {
5782 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5783 return RETURN_ERR;
5784 }
5785
developera3511852023-06-14 14:12:59 +08005786 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08005787
developer75bd10c2023-06-27 11:34:08 +08005788 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5789 if (os_snprintf_error(sizeof(config_file), res)) {
5790 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5791 return RETURN_ERR;
5792 }
developera3511852023-06-14 14:12:59 +08005793 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08005794
developera3511852023-06-14 14:12:59 +08005795 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5796 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5797 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005798}
5799
5800//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.
5801INT wifi_getRadioBasicDataTransmitRates(INT radioIndex, CHAR *output)
5802{
developera3511852023-06-14 14:12:59 +08005803 //TODO: need to revisit below implementation
5804 char *temp;
5805 char temp_output[128] = {0};
5806 char temp_TransmitRates[64] = {0};
5807 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08005808 int res;
developer72fb0bb2023-01-11 09:46:29 +08005809
developera3511852023-06-14 14:12:59 +08005810 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5811 if (NULL == output)
5812 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08005813
5814 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5815 if (os_snprintf_error(sizeof(config_file), res)) {
5816 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5817 return RETURN_ERR;
5818 }
developera3511852023-06-14 14:12:59 +08005819 wifi_hostapdRead(config_file,"basic_rates",temp_TransmitRates,64);
developer69b61b02023-03-07 17:17:44 +08005820
developera3511852023-06-14 14:12:59 +08005821 if (strlen(temp_TransmitRates) == 0) { // config not set, use supported rate
5822 wifi_getRadioSupportedDataTransmitRates(radioIndex, output);
5823 } else {
5824 temp = strtok(temp_TransmitRates," ");
5825 while(temp!=NULL)
5826 {
5827 // Convert 100 kbps to Mbps
5828 temp[strlen(temp)-1]=0;
5829 if((temp[0]=='5') && (temp[1]=='\0'))
5830 {
5831 temp="5.5";
5832 }
developer32f2a182023-06-27 19:50:41 +08005833 if (strlen(temp) >= sizeof(temp_output))
5834 return RETURN_ERR;
5835 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005836 temp = strtok(NULL," ");
5837 if(temp!=NULL)
5838 {
developer32f2a182023-06-27 19:50:41 +08005839 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
5840 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005841 }
5842 }
developer32f2a182023-06-27 19:50:41 +08005843 memcpy(output, temp_output, strlen(temp_output));
5844 output[strlen(temp_output)] = '\0';
developera3511852023-06-14 14:12:59 +08005845 }
5846 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5847 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005848}
5849
5850INT wifi_setRadioBasicDataTransmitRates(INT radioIndex, CHAR *TransmitRates)
5851{
developera3511852023-06-14 14:12:59 +08005852 char *temp;
developer32f2a182023-06-27 19:50:41 +08005853 char temp1[128] = {0};
5854 char temp_output[128] = {0};
5855 char temp_TransmitRates[128] = {0};
5856 char set[128] = {0};
5857 char sub_set[128] = {0};
developera3511852023-06-14 14:12:59 +08005858 int set_count=0,subset_count=0;
5859 int set_index=0,subset_index=0;
5860 char *token;
5861 int flag=0, i=0;
5862 struct params params={'\0'};
5863 char config_file[MAX_BUF_SIZE] = {0};
5864 wifi_band band = wifi_index_to_band(radioIndex);
developer32f2a182023-06-27 19:50:41 +08005865 int res;
developer9f2358c2023-09-22 18:42:12 +08005866 bool temp_multiple_set = multiple_set;
developer72fb0bb2023-01-11 09:46:29 +08005867
developera3511852023-06-14 14:12:59 +08005868 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer9f2358c2023-09-22 18:42:12 +08005869
developera3511852023-06-14 14:12:59 +08005870 if(NULL == TransmitRates)
5871 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +08005872 if (strlen(TransmitRates) >= sizeof(sub_set))
5873 return RETURN_ERR;
5874
5875 memcpy(sub_set, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08005876
developera3511852023-06-14 14:12:59 +08005877 //Allow only supported Data transmit rate to be set
5878 wifi_getRadioSupportedDataTransmitRates(radioIndex,set);
5879 token = strtok(sub_set,",");
5880 while( token != NULL ) /* split the basic rate to be set, by comma */
5881 {
5882 sub_set[subset_count]=atoi(token);
5883 subset_count++;
5884 token=strtok(NULL,",");
5885 }
5886 token=strtok(set,",");
5887 while(token!=NULL) /* split the supported rate by comma */
5888 {
5889 set[set_count]=atoi(token);
5890 set_count++;
5891 token=strtok(NULL,",");
5892 }
5893 for(subset_index=0;subset_index < subset_count;subset_index++) /* Compare each element of subset and set */
5894 {
5895 for(set_index=0;set_index < set_count;set_index++)
5896 {
5897 flag=0;
5898 if(sub_set[subset_index]==set[set_index])
5899 break;
5900 else
5901 flag=1; /* No match found */
5902 }
5903 if(flag==1)
5904 return RETURN_ERR; //If value not found return Error
5905 }
developer5b23cd02023-07-19 20:26:03 +08005906
developer32f2a182023-06-27 19:50:41 +08005907 if (strlen(TransmitRates) >= sizeof(temp_TransmitRates))
5908 return RETURN_ERR;
5909
5910 memcpy(temp_TransmitRates, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08005911
developera3511852023-06-14 14:12:59 +08005912 for(i=0;i<strlen(temp_TransmitRates);i++)
5913 {
5914 //if (((temp_TransmitRates[i]>=48) && (temp_TransmitRates[i]<=57)) | (temp_TransmitRates[i]==32))
5915 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
5916 {
5917 continue;
5918 }
5919 else
5920 {
5921 return RETURN_ERR;
5922 }
5923 }
developera3511852023-06-14 14:12:59 +08005924 temp = strtok(temp_TransmitRates,",");
5925 while(temp!=NULL)
5926 {
developer32f2a182023-06-27 19:50:41 +08005927 if (strlen(temp) >= sizeof(temp1))
5928 return RETURN_ERR;
5929 strncpy(temp1, temp, strlen(temp));
developera3511852023-06-14 14:12:59 +08005930 if(band == band_5)
5931 {
5932 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
5933 {
5934 return RETURN_ERR;
5935 }
5936 }
developer72fb0bb2023-01-11 09:46:29 +08005937
developera3511852023-06-14 14:12:59 +08005938 if(strcmp(temp,"5.5")==0)
5939 {
developer32f2a182023-06-27 19:50:41 +08005940 memcpy(temp1, "55", 2);
developera3511852023-06-14 14:12:59 +08005941 }
5942 else
5943 {
developer32f2a182023-06-27 19:50:41 +08005944 if (strlen(temp1) >= (sizeof(temp1) - 1))
5945 return RETURN_ERR;
5946 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +08005947 }
developer32f2a182023-06-27 19:50:41 +08005948 if (strlen(temp1) >= (sizeof(temp_output) - strlen(temp_output)))
5949 return RETURN_ERR;
5950 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005951 temp = strtok(NULL,",");
5952 if(temp!=NULL)
5953 {
developer32f2a182023-06-27 19:50:41 +08005954 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
5955 return RETURN_ERR;
5956 strncat(temp_output," ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005957 }
5958 }
developer32f2a182023-06-27 19:50:41 +08005959 memcpy(TransmitRates, temp_output, strlen(temp_output));
5960 TransmitRates[strlen(temp_output)] = '\0';
developer5b23cd02023-07-19 20:26:03 +08005961
developera3511852023-06-14 14:12:59 +08005962 params.name= "basic_rates";
5963 params.value =TransmitRates;
developer72fb0bb2023-01-11 09:46:29 +08005964
developera3511852023-06-14 14:12:59 +08005965 wifi_dbg_printf("\n%s:",__func__);
5966 wifi_dbg_printf("\nparams.value=%s\n",params.value);
5967 wifi_dbg_printf("\n******************Transmit rates=%s\n",TransmitRates);
developer32f2a182023-06-27 19:50:41 +08005968 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex);
5969 if (os_snprintf_error(sizeof(config_file), res)) {
5970 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5971 return RETURN_ERR;
5972 }
developer5b23cd02023-07-19 20:26:03 +08005973
developera3511852023-06-14 14:12:59 +08005974 wifi_hostapdWrite(config_file,&params,1);
developer9f2358c2023-09-22 18:42:12 +08005975 multiple_set = false;
5976 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5977 wifi_quick_reload_ap(radioIndex);
5978 multiple_set = temp_multiple_set;
5979
developera3511852023-06-14 14:12:59 +08005980 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5981 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005982}
5983
developer72fb0bb2023-01-11 09:46:29 +08005984INT wifi_halGetIfStatsNull(wifi_radioTrafficStats2_t *output_struct)
5985{
developera3511852023-06-14 14:12:59 +08005986 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
5987 output_struct->radio_BytesSent = 0;
5988 output_struct->radio_BytesReceived = 0;
5989 output_struct->radio_PacketsSent = 0;
5990 output_struct->radio_PacketsReceived = 0;
5991 output_struct->radio_ErrorsSent = 0;
5992 output_struct->radio_ErrorsReceived = 0;
5993 output_struct->radio_DiscardPacketsSent = 0;
5994 output_struct->radio_DiscardPacketsReceived = 0;
5995 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
5996 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005997}
5998
5999
6000INT wifi_halGetIfStats(char *ifname, wifi_radioTrafficStats2_t *pStats)
6001{
developera3511852023-06-14 14:12:59 +08006002 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer8078acf2023-08-04 18:52:48 +08006003
developera3511852023-06-14 14:12:59 +08006004 CHAR Value[MAX_BUF_SIZE] = {0};
6005 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08006006 int res;
developer37646972023-06-29 10:58:43 +08006007 unsigned long ret;
developer72fb0bb2023-01-11 09:46:29 +08006008
developera3511852023-06-14 14:12:59 +08006009 if (ifname == NULL || strlen(ifname) <= 1)
6010 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006011
developer33f13ba2023-07-12 16:19:06 +08006012 res = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", ifname);
6013 if (res) {
6014 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08006015 return RETURN_ERR;
6016 }
6017
developera3511852023-06-14 14:12:59 +08006018 fp = fopen("/tmp/Radio_Stats.txt", "r");
6019 if(fp == NULL)
6020 {
6021 printf("/tmp/Radio_Stats.txt not exists \n");
6022 return RETURN_ERR;
6023 }
developerd14dff12023-06-28 22:47:44 +08006024 if (fclose(fp) != 0) {
6025 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6026 return RETURN_ERR;
6027 }
developer72fb0bb2023-01-11 09:46:29 +08006028
developer8078acf2023-08-04 18:52:48 +08006029 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6030 if(res) {
6031 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08006032 }
developer5b23cd02023-07-19 20:26:03 +08006033
developerc14d83a2023-06-29 20:09:42 +08006034 if (hal_strtoul(Value, 10, &ret) < 0) {
developer37646972023-06-29 10:58:43 +08006035 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +08006036 }
developerc14d83a2023-06-29 20:09:42 +08006037
developer37646972023-06-29 10:58:43 +08006038 pStats->radio_PacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006039
developer8078acf2023-08-04 18:52:48 +08006040 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6041 if(res) {
6042 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006043 }
developer5b23cd02023-07-19 20:26:03 +08006044
developerc14d83a2023-06-29 20:09:42 +08006045 if (hal_strtoul(Value, 10, &ret) < 0) {
6046 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006047 }
developer5b23cd02023-07-19 20:26:03 +08006048
developer37646972023-06-29 10:58:43 +08006049 pStats->radio_PacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006050
developer8078acf2023-08-04 18:52:48 +08006051 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'RX bytes' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6052 if(res) {
6053 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006054 }
developer8078acf2023-08-04 18:52:48 +08006055
developerc14d83a2023-06-29 20:09:42 +08006056 if (hal_strtoul(Value, 10, &ret) < 0) {
6057 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006058 }
6059 pStats->radio_BytesReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006060
developer8078acf2023-08-04 18:52:48 +08006061 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'TX bytes' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6062 if(res) {
6063 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006064 }
developer8078acf2023-08-04 18:52:48 +08006065
developerc14d83a2023-06-29 20:09:42 +08006066 if (hal_strtoul(Value, 10, &ret) < 0) {
6067 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006068 }
6069 pStats->radio_BytesSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006070
developer8078acf2023-08-04 18:52:48 +08006071 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6072 if(res) {
6073 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006074 }
developer8078acf2023-08-04 18:52:48 +08006075
developerc14d83a2023-06-29 20:09:42 +08006076 if (hal_strtoul(Value, 10, &ret) < 0) {
6077 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006078 }
6079 pStats->radio_ErrorsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006080
developer8078acf2023-08-04 18:52:48 +08006081 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6082 if(res) {
6083 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006084 }
developer8078acf2023-08-04 18:52:48 +08006085
developerc14d83a2023-06-29 20:09:42 +08006086 if (hal_strtoul(Value, 10, &ret) < 0) {
6087 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006088 }
6089 pStats->radio_ErrorsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006090
developer8078acf2023-08-04 18:52:48 +08006091 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6092 if(res) {
6093 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006094 }
developer8078acf2023-08-04 18:52:48 +08006095
developerc14d83a2023-06-29 20:09:42 +08006096 if (hal_strtoul(Value, 10, &ret) < 0) {
6097 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006098 }
6099 pStats->radio_DiscardPacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006100
developer8078acf2023-08-04 18:52:48 +08006101 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6102 if(res) {
6103 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer86035662023-06-28 19:21:12 +08006104 }
developer8078acf2023-08-04 18:52:48 +08006105
developerc14d83a2023-06-29 20:09:42 +08006106 if (hal_strtoul(Value, 10, &ret) < 0) {
6107 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006108 }
6109 pStats->radio_DiscardPacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006110
developera3511852023-06-14 14:12:59 +08006111 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6112 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006113}
6114
6115INT GetIfacestatus(CHAR *interface_name, CHAR *status)
6116{
developer75bd10c2023-06-27 11:34:08 +08006117 int res;
developer72fb0bb2023-01-11 09:46:29 +08006118
developer7e4a2a62023-04-06 19:56:03 +08006119 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6120
6121 if (interface_name != NULL && (strlen(interface_name) > 1) && status != NULL) {
developer8078acf2023-08-04 18:52:48 +08006122
developerd08b7d52023-08-22 15:41:36 +08006123 res = _syscmd_secure(status, sizeof(status), "ifconfig -a %s | grep %s | wc -l",
6124 interface_name, interface_name);
6125 if(res) {
6126 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
6127 }
developer75bd10c2023-06-27 11:34:08 +08006128
developer7e4a2a62023-04-06 19:56:03 +08006129 }
6130
6131 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6132 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006133}
6134
6135//Get detail radio traffic static info
6136INT wifi_getRadioTrafficStats2(INT radioIndex, wifi_radioTrafficStats2_t *output_struct) //Tr181
6137{
developera3511852023-06-14 14:12:59 +08006138 CHAR interface_name[64] = {0};
6139 BOOL iface_status = FALSE;
6140 wifi_radioTrafficStats2_t radioTrafficStats = {0};
developer72fb0bb2023-01-11 09:46:29 +08006141
developera3511852023-06-14 14:12:59 +08006142 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6143 if (NULL == output_struct)
6144 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006145
developera3511852023-06-14 14:12:59 +08006146 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6147 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006148
developera3511852023-06-14 14:12:59 +08006149 wifi_getApEnable(radioIndex, &iface_status);
developer72fb0bb2023-01-11 09:46:29 +08006150
developera3511852023-06-14 14:12:59 +08006151 if (iface_status == TRUE)
6152 wifi_halGetIfStats(interface_name, &radioTrafficStats);
6153 else
6154 wifi_halGetIfStatsNull(&radioTrafficStats); // just set some transmission statistic value to 0
developer72fb0bb2023-01-11 09:46:29 +08006155
developera3511852023-06-14 14:12:59 +08006156 output_struct->radio_BytesSent = radioTrafficStats.radio_BytesSent;
6157 output_struct->radio_BytesReceived = radioTrafficStats.radio_BytesReceived;
6158 output_struct->radio_PacketsSent = radioTrafficStats.radio_PacketsSent;
6159 output_struct->radio_PacketsReceived = radioTrafficStats.radio_PacketsReceived;
6160 output_struct->radio_ErrorsSent = radioTrafficStats.radio_ErrorsSent;
6161 output_struct->radio_ErrorsReceived = radioTrafficStats.radio_ErrorsReceived;
6162 output_struct->radio_DiscardPacketsSent = radioTrafficStats.radio_DiscardPacketsSent;
6163 output_struct->radio_DiscardPacketsReceived = radioTrafficStats.radio_DiscardPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +08006164
developera3511852023-06-14 14:12:59 +08006165 output_struct->radio_PLCPErrorCount = 0; //The number of packets that were received with a detected Physical Layer Convergence Protocol (PLCP) header error.
6166 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].
6167 output_struct->radio_InvalidMACCount = 0; //The number of packets that were received with a detected invalid MAC header error.
6168 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.
6169 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
6170 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
6171 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
6172 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
6173 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 +08006174
developera3511852023-06-14 14:12:59 +08006175 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
6176 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
6177 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
6178 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 +08006179
developera3511852023-06-14 14:12:59 +08006180 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006181
developera3511852023-06-14 14:12:59 +08006182 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006183}
6184
6185//Set radio traffic static Measureing rules
6186INT wifi_setRadioTrafficStatsMeasure(INT radioIndex, wifi_radioTrafficStatsMeasure_t *input_struct) //Tr181
6187{
developera39cfb22023-06-20 16:28:17 +08006188 char inf_name[IF_NAME_SIZE] = {0};
6189 unsigned int if_idx = 0;
6190 int ret = -1;
6191 struct unl unl_ins;
6192 struct nl_msg *msg = NULL;
6193 struct nlattr * msg_data = NULL;
6194 struct mtk_nl80211_param param;
6195
6196 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6197 return RETURN_ERR;
6198 if_idx = if_nametoindex(inf_name);
6199 if (!if_idx) {
6200 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6201 return RETURN_ERR;
6202 }
6203 /*init mtk nl80211 vendor cmd*/
6204 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6205 param.if_type = NL80211_ATTR_IFINDEX;
6206 param.if_idx = if_idx;
6207
6208 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6209 if (ret) {
6210 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6211 return RETURN_ERR;
6212 }
6213 /*add mtk vendor cmd data*/
6214 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD,
6215 sizeof(wifi_radioTrafficStatsMeasure_t), input_struct)) {
6216 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6217 MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD);
6218 nlmsg_free(msg);
6219 goto err;
6220 }
6221
6222 /*send mtk nl80211 vendor msg*/
6223 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6224 if (ret) {
6225 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6226 goto err;
6227 }
6228 /*deinit mtk nl80211 vendor msg*/
6229 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006230 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006231err:
6232 mtk_nl80211_deint(&unl_ins);
6233 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6234 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006235}
6236
6237//To start or stop RadioTrafficStats
6238INT wifi_setRadioTrafficStatsRadioStatisticsEnable(INT radioIndex, BOOL enable)
6239{
developera39cfb22023-06-20 16:28:17 +08006240 char inf_name[IF_NAME_SIZE] = {0};
6241 unsigned int if_idx = 0;
6242 int ret = -1;
6243 struct unl unl_ins;
6244 struct nl_msg *msg = NULL;
6245 struct nlattr * msg_data = NULL;
6246 struct mtk_nl80211_param param;
6247
6248 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6249 return RETURN_ERR;
6250 if_idx = if_nametoindex(inf_name);
6251 if (!if_idx) {
6252 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6253 return RETURN_ERR;
6254 }
6255 /*init mtk nl80211 vendor cmd*/
6256 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6257 param.if_type = NL80211_ATTR_IFINDEX;
6258 param.if_idx = if_idx;
6259
6260 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6261 if (ret) {
6262 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6263 return RETURN_ERR;
6264 }
6265 /*add mtk vendor cmd data*/
6266 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL, enable)) {
6267 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6268 MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL);
6269 nlmsg_free(msg);
6270 goto err;
6271 }
6272
6273 /*send mtk nl80211 vendor msg*/
6274 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6275 if (ret) {
6276 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6277 goto err;
6278 }
6279 /*deinit mtk nl80211 vendor msg*/
6280 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006281 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006282err:
6283 mtk_nl80211_deint(&unl_ins);
6284 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6285 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006286}
6287
6288//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
6289INT wifi_getRadioStatsReceivedSignalLevel(INT radioIndex, INT signalIndex, INT *SignalLevel) //Tr181
6290{
developera3511852023-06-14 14:12:59 +08006291 if (NULL == SignalLevel)
6292 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +08006293
developer9ce44382023-06-28 11:09:37 +08006294 *SignalLevel = -19;
developer72fb0bb2023-01-11 09:46:29 +08006295
developera3511852023-06-14 14:12:59 +08006296 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006297}
6298
6299//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6300INT wifi_applyRadioSettings(INT radioIndex)
6301{
developera3511852023-06-14 14:12:59 +08006302 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006303}
6304
6305//Get the radio index assocated with this SSID entry
6306INT wifi_getSSIDRadioIndex(INT ssidIndex, INT *radioIndex)
6307{
developera3511852023-06-14 14:12:59 +08006308 if(NULL == radioIndex)
6309 return RETURN_ERR;
6310 int max_radio_num = 0;
6311 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006312 if(max_radio_num == 0){
6313 return RETURN_ERR;
6314 }
developera3511852023-06-14 14:12:59 +08006315 *radioIndex = ssidIndex%max_radio_num;
6316 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006317}
6318
6319//Device.WiFi.SSID.{i}.Enable
6320//Get SSID enable configuration parameters (not the SSID enable status)
6321INT wifi_getSSIDEnable(INT ssidIndex, BOOL *output_bool) //Tr181
6322{
developera3511852023-06-14 14:12:59 +08006323 if (NULL == output_bool)
6324 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006325
developera3511852023-06-14 14:12:59 +08006326 return wifi_getApEnable(ssidIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08006327}
6328
6329//Device.WiFi.SSID.{i}.Enable
6330//Set SSID enable configuration parameters
6331INT wifi_setSSIDEnable(INT ssidIndex, BOOL enable) //Tr181
6332{
developera3511852023-06-14 14:12:59 +08006333 return wifi_setApEnable(ssidIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08006334}
6335
6336//Device.WiFi.SSID.{i}.Status
6337//Get the SSID enable status
6338INT wifi_getSSIDStatus(INT ssidIndex, CHAR *output_string) //Tr181
6339{
developer9ce44382023-06-28 11:09:37 +08006340 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +08006341 int res;
developer72fb0bb2023-01-11 09:46:29 +08006342
developera3511852023-06-14 14:12:59 +08006343 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6344 if (NULL == output_string)
6345 return RETURN_ERR;
developer69b61b02023-03-07 17:17:44 +08006346
developera3511852023-06-14 14:12:59 +08006347 wifi_getApEnable(ssidIndex,&output_bool);
developere40952c2023-06-15 18:46:43 +08006348 res = snprintf(output_string, 32, output_bool==1?"Enabled":"Disabled");
6349 if (os_snprintf_error(32, res)) {
6350 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6351 return RETURN_ERR;
6352 }
developer72fb0bb2023-01-11 09:46:29 +08006353
developera3511852023-06-14 14:12:59 +08006354 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6355 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006356}
6357
6358// Outputs a 32 byte or less string indicating the SSID name. Sring buffer must be preallocated by the caller.
6359INT wifi_getSSIDName(INT apIndex, CHAR *output)
6360{
developera3511852023-06-14 14:12:59 +08006361 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006362 int res;
developer72fb0bb2023-01-11 09:46:29 +08006363
developera3511852023-06-14 14:12:59 +08006364 if (NULL == output)
6365 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006366
developer75bd10c2023-06-27 11:34:08 +08006367 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6368 if (os_snprintf_error(sizeof(config_file), res)) {
6369 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6370 return RETURN_ERR;
6371 }
developera3511852023-06-14 14:12:59 +08006372 wifi_hostapdRead(config_file,"ssid",output,32);
developer72fb0bb2023-01-11 09:46:29 +08006373
developera3511852023-06-14 14:12:59 +08006374 wifi_dbg_printf("\n[%s]: SSID Name is : %s",__func__,output);
6375 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006376}
6377
developer69b61b02023-03-07 17:17:44 +08006378// Set a max 32 byte string and sets an internal variable to the SSID name
developer72fb0bb2023-01-11 09:46:29 +08006379INT wifi_setSSIDName(INT apIndex, CHAR *ssid_string)
6380{
developera3511852023-06-14 14:12:59 +08006381 struct params params;
6382 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006383 int res;
developer72fb0bb2023-01-11 09:46:29 +08006384
developera3511852023-06-14 14:12:59 +08006385 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6386 if(NULL == ssid_string || strlen(ssid_string) >= 32 || strlen(ssid_string) == 0 )
6387 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006388
developera3511852023-06-14 14:12:59 +08006389 params.name = "ssid";
6390 params.value = ssid_string;
developer75bd10c2023-06-27 11:34:08 +08006391
6392 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6393 if (os_snprintf_error(sizeof(config_file), res)) {
6394 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6395 return RETURN_ERR;
6396 }
6397
developera3511852023-06-14 14:12:59 +08006398 wifi_hostapdWrite(config_file, &params, 1);
6399 wifi_hostapdProcessUpdate(apIndex, &params, 1);
6400 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006401
developera3511852023-06-14 14:12:59 +08006402 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006403}
6404
6405//Get the BSSID
6406INT wifi_getBaseBSSID(INT ssidIndex, CHAR *output_string) //RDKB
6407{
developer7e4a2a62023-04-06 19:56:03 +08006408 char inf_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08006409 int res;
developer72fb0bb2023-01-11 09:46:29 +08006410
developera3511852023-06-14 14:12:59 +08006411 if (!output_string)
developerdaf24792023-06-06 11:40:04 +08006412 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006413
developer47cc27a2023-05-17 23:09:58 +08006414 if (wifi_GetInterfaceName(ssidIndex, inf_name) != RETURN_OK)
6415 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08006416
developer5b2f10c2023-05-25 17:02:21 +08006417 if (ssidIndex < 0 || ssidIndex > MAX_APS) {
6418 wifi_debug(DEBUG_ERROR, "innvalide ssidIdex(%d)\n", ssidIndex);
6419 strncpy(output_string, "\0", 1);
6420 return RETURN_ERR;
6421 }
developere40952c2023-06-15 18:46:43 +08006422
developer8078acf2023-08-04 18:52:48 +08006423 res = _syscmd_secure(output_string,64,"hostapd_cli -i %s get_config | grep bssid | cut -d '=' -f2 | tr -d '\\n'", inf_name);
6424 if(res) {
6425 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
6426 }
developer7e4a2a62023-04-06 19:56:03 +08006427
developer5b2f10c2023-05-25 17:02:21 +08006428 /* if hostapd does not control interface even if this interface has been brought up,
6429 * try to get its mac address by iw command.
6430 */
6431 if(strlen(output_string) == 0) {
developer8078acf2023-08-04 18:52:48 +08006432 res = _syscmd_secure(output_string, 64, "iw dev %s info | grep \"addr\" | awk \'{print $2}\'", inf_name);
6433 if(res) {
6434 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08006435 }
developer5b2f10c2023-05-25 17:02:21 +08006436 }
developer72fb0bb2023-01-11 09:46:29 +08006437
developer5b2f10c2023-05-25 17:02:21 +08006438 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006439}
6440
6441//Get the MAC address associated with this Wifi SSID
6442INT wifi_getSSIDMACAddress(INT ssidIndex, CHAR *output_string) //Tr181
6443{
developera3511852023-06-14 14:12:59 +08006444 wifi_getBaseBSSID(ssidIndex,output_string);
6445 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006446}
6447
6448//Get the basic SSID traffic static info
6449//Apply SSID and AP (in the case of Acess Point devices) to the hardware
6450//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6451INT wifi_applySSIDSettings(INT ssidIndex)
6452{
developera3511852023-06-14 14:12:59 +08006453 char interface_name[16] = {0};
6454 BOOL status = false;
developera3511852023-06-14 14:12:59 +08006455 char buf[MAX_CMD_SIZE] = {0};
6456 int apIndex, ret;
6457 int max_radio_num = 0;
6458 int radioIndex = 0;
developere40952c2023-06-15 18:46:43 +08006459 int res;
developer72fb0bb2023-01-11 09:46:29 +08006460
developera3511852023-06-14 14:12:59 +08006461 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006462 if(max_radio_num == 0){
6463 return RETURN_ERR;
6464 }
developera3511852023-06-14 14:12:59 +08006465 radioIndex = ssidIndex % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +08006466
developera3511852023-06-14 14:12:59 +08006467 wifi_getApEnable(ssidIndex,&status);
6468 // Do not apply when ssid index is disabled
6469 if (status == false)
6470 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006471
developera3511852023-06-14 14:12:59 +08006472 /* Doing full remove and add for ssid Index
6473 * Not all hostapd options are supported with reload
6474 * for example macaddr_acl
6475 */
6476 if(wifi_setApEnable(ssidIndex,false) != RETURN_OK)
6477 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006478
developera3511852023-06-14 14:12:59 +08006479 ret = wifi_setApEnable(ssidIndex,true);
developer72fb0bb2023-01-11 09:46:29 +08006480
developera3511852023-06-14 14:12:59 +08006481 /* Workaround for hostapd issue with multiple bss definitions
6482 * when first created interface will be removed
6483 * then all vaps other vaps on same phy are removed
6484 * after calling setApEnable to false readd all enabled vaps */
6485 for(int i=0; i < MAX_APS/max_radio_num; i++) {
6486 apIndex = max_radio_num*i+radioIndex;
6487 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
6488 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006489
developer8078acf2023-08-04 18:52:48 +08006490 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
6491 if(res) {
6492 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
6493 }
developera3511852023-06-14 14:12:59 +08006494 if(*buf == '1')
6495 wifi_setApEnable(apIndex, true);
6496 }
developer72fb0bb2023-01-11 09:46:29 +08006497
developera3511852023-06-14 14:12:59 +08006498 return ret;
developer72fb0bb2023-01-11 09:46:29 +08006499}
6500
6501struct channels_noise {
developera3511852023-06-14 14:12:59 +08006502 int channel;
6503 int noise;
developer72fb0bb2023-01-11 09:46:29 +08006504};
6505
6506// Return noise array for each channel
6507int get_noise(int radioIndex, struct channels_noise *channels_noise_arr, int channels_num)
6508{
developera3511852023-06-14 14:12:59 +08006509 char interface_name[16] = {0};
6510 FILE *f = NULL;
6511 char cmd[128] = {0};
6512 char line[256] = {0};
developer75bd10c2023-06-27 11:34:08 +08006513 int tmp = 0, arr_index = -1, res;
developer72fb0bb2023-01-11 09:46:29 +08006514
developera3511852023-06-14 14:12:59 +08006515 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6516 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08006517
6518 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep 'frequency\\|noise' | awk '{print $2}'", interface_name);
6519 if (os_snprintf_error(sizeof(cmd), res)) {
6520 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6521 return RETURN_ERR;
6522 }
developer8078acf2023-08-04 18:52:48 +08006523 f = v_secure_popen("r", "iw dev %s survey dump | grep 'frequency\\|noise' | awk '{print $2}'", interface_name);
6524 if (f == NULL) {
6525 wifi_dbg_printf("%s: v_secure_popen %s error\n", __func__, cmd);
developera3511852023-06-14 14:12:59 +08006526 return RETURN_ERR;
6527 }
developer69b61b02023-03-07 17:17:44 +08006528
developera3511852023-06-14 14:12:59 +08006529 while(fgets(line, sizeof(line), f) != NULL) {
6530 if(arr_index < channels_num){
developer37646972023-06-29 10:58:43 +08006531 if (sscanf(line, "%d", &tmp) == EOF)
6532 continue;
developera3511852023-06-14 14:12:59 +08006533 if (tmp > 0) { // channel frequency, the first line must be frequency
6534 arr_index++;
6535 channels_noise_arr[arr_index].channel = ieee80211_frequency_to_channel(tmp);
6536 } else { // noise
6537 channels_noise_arr[arr_index].noise = tmp;
6538 }
6539 }else{
6540 break;
6541 }
6542 }
developer8078acf2023-08-04 18:52:48 +08006543 v_secure_pclose(f);
developera3511852023-06-14 14:12:59 +08006544 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006545}
6546
6547//Start the wifi scan and get the result into output buffer for RDKB to parser. The result will be used to manage endpoint list
6548//HAL funciton should allocate an data structure array, and return to caller with "neighbor_ap_array"
developer69b61b02023-03-07 17:17:44 +08006549INT wifi_getNeighboringWiFiDiagnosticResult2(INT radioIndex, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size) //Tr181
developer72fb0bb2023-01-11 09:46:29 +08006550{
developera3511852023-06-14 14:12:59 +08006551 int index = -1;
6552 wifi_neighbor_ap2_t *scan_array = NULL;
6553 char cmd[256]={0};
6554 char buf[128]={0};
6555 char file_name[32] = {0};
6556 char filter_SSID[32] = {0};
6557 char line[256] = {0};
6558 char interface_name[16] = {0};
6559 char *ret = NULL;
6560 int freq=0;
6561 FILE *f = NULL;
developerc14d83a2023-06-29 20:09:42 +08006562 unsigned long channels_num = 0;
developera3511852023-06-14 14:12:59 +08006563 int vht_channel_width = 0;
6564 int get_noise_ret = RETURN_ERR;
6565 bool filter_enable = false;
6566 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
developere40952c2023-06-15 18:46:43 +08006567 int phyId = 0, res;
developerc14d83a2023-06-29 20:09:42 +08006568 unsigned long len, tmp;
developer72fb0bb2023-01-11 09:46:29 +08006569
developera3511852023-06-14 14:12:59 +08006570 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006571
developera3511852023-06-14 14:12:59 +08006572 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6573 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006574
6575 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radioIndex);
6576 if (os_snprintf_error(sizeof(file_name), res)) {
6577 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6578 return RETURN_ERR;
6579 }
developer72fb0bb2023-01-11 09:46:29 +08006580
developera3511852023-06-14 14:12:59 +08006581 f = fopen(file_name, "r");
6582 if (f != NULL) {
developerd14dff12023-06-28 22:47:44 +08006583 if (fgets(buf, sizeof(file_name), f) == NULL)
6584 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006585 if ((strncmp(buf, "0", 1)) != 0) {
developerd14dff12023-06-28 22:47:44 +08006586 if (fgets(filter_SSID, sizeof(file_name), f) == NULL)
6587 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006588 if (strlen(filter_SSID) != 0)
6589 filter_enable = true;
6590 }
developerd14dff12023-06-28 22:47:44 +08006591 if (fclose(f) != 0) {
6592 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6593 return RETURN_ERR;
6594 }
developera3511852023-06-14 14:12:59 +08006595 }
developer72fb0bb2023-01-11 09:46:29 +08006596
developera3511852023-06-14 14:12:59 +08006597 phyId = radio_index_to_phy(radioIndex);
developer8078acf2023-08-04 18:52:48 +08006598
6599 res = _syscmd_secure(buf, sizeof(buf), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
6600 if (res) {
6601 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08006602 }
6603
developerc14d83a2023-06-29 20:09:42 +08006604 if (hal_strtoul(buf, 10, &tmp) < 0) {
6605 wifi_debug(DEBUG_ERROR, "strtol fail\n");
6606 }
developer72fb0bb2023-01-11 09:46:29 +08006607
developerc14d83a2023-06-29 20:09:42 +08006608 channels_num = tmp;
developer32f2a182023-06-27 19:50:41 +08006609 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 +08006610 // 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 +08006611 if (os_snprintf_error(sizeof(cmd), res)) {
6612 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6613 return RETURN_ERR;
6614 }
developer86035662023-06-28 19:21:12 +08006615 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developer8078acf2023-08-04 18:52:48 +08006616
6617 f = v_secure_popen("r", "iw dev %s scan | grep '%s\\|SSID\\|freq\\|beacon interval\\|capabilities\\|signal\\|Supported rates\\|DTIM\\| \
6618 // WPA\\|RSN\\|Group cipher\\|HT operation\\|secondary channel offset\\|channel width\\|HE.*GHz' | grep -v -e '*.*BSS'", interface_name, interface_name);
6619
6620 if (f == NULL) {
6621 wifi_debug(DEBUG_ERROR, "v_secure_popen error\n");
developera3511852023-06-14 14:12:59 +08006622 return RETURN_ERR;
6623 }
developer9ce44382023-06-28 11:09:37 +08006624 struct channels_noise *channels_noise_arr = NULL;
6625 if(channels_num > 0 && channels_num <= 243){
6626 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
6627 } else{
developerb14b3462023-07-01 18:02:42 +08006628 wifi_debug(DEBUG_ERROR, "channel num = %ld!!\n", channels_num);
developer9ce44382023-06-28 11:09:37 +08006629 }
developer5b23cd02023-07-19 20:26:03 +08006630
developer9ce44382023-06-28 11:09:37 +08006631 if(channels_noise_arr != NULL){
6632 get_noise_ret = get_noise(radioIndex, channels_noise_arr, channels_num);
6633 } else{
developerb14b3462023-07-01 18:02:42 +08006634 wifi_debug(DEBUG_ERROR, "channels_noise_arr is NULL!!\n");
developer9ce44382023-06-28 11:09:37 +08006635 }
developer5b23cd02023-07-19 20:26:03 +08006636
developer69b61b02023-03-07 17:17:44 +08006637
developera3511852023-06-14 14:12:59 +08006638 ret = fgets(line, sizeof(line), f);
6639 while (ret != NULL) {
6640 if(strstr(line, "BSS") != NULL) { // new neighbor info
6641 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
6642 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
6643 // 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 +08006644
developera3511852023-06-14 14:12:59 +08006645 if (!filter_BSS) {
6646 index++;
6647 wifi_neighbor_ap2_t *tmp;
6648 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
6649 if (tmp == NULL) { // no more memory to use
6650 index--;
6651 wifi_dbg_printf("%s: realloc failed\n", __func__);
6652 break;
6653 }
6654 scan_array = tmp;
6655 }
6656 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +08006657
developera3511852023-06-14 14:12:59 +08006658 filter_BSS = false;
developer86035662023-06-28 19:21:12 +08006659 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
6660 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6661 goto err;
6662 }
developerc79e9172023-06-06 19:48:03 +08006663 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +08006664 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +08006665 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +08006666 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +08006667 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +08006668 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
6669 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +08006670 if (sscanf(line," freq: %d", &freq) != 1) {
6671 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006672 }
developera3511852023-06-14 14:12:59 +08006673 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +08006674
developera3511852023-06-14 14:12:59 +08006675 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +08006676 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006677 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +08006678 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006679 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +08006680 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006681 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
6682 }
6683 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +08006684 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006685 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +08006686 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006687 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +08006688 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006689 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
6690 }
developer72fb0bb2023-01-11 09:46:29 +08006691
developera3511852023-06-14 14:12:59 +08006692 scan_array[index].ap_Noise = 0;
6693 if (get_noise_ret == RETURN_OK) {
6694 for (int i = 0; i < channels_num; i++) {
6695 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
6696 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
6697 break;
6698 }
6699 }
6700 }
6701 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +08006702 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
6703 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6704 goto err;
6705 }
developera3511852023-06-14 14:12:59 +08006706 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +08006707 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
6708 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6709 goto err;
6710 }
developera3511852023-06-14 14:12:59 +08006711 } else if (strstr(line,"SSID") != NULL) {
developer86035662023-06-28 19:21:12 +08006712 if (sscanf(line," SSID: %32s", scan_array[index].ap_SSID) != 1) {
6713 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +08006714 //goto err;
developer86035662023-06-28 19:21:12 +08006715 }
developera3511852023-06-14 14:12:59 +08006716 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) != 0) {
6717 filter_BSS = true;
6718 }
6719 } else if (strstr(line, "Supported rates") != NULL) {
6720 char SRate[80] = {0}, *tmp = NULL;
6721 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +08006722 if (strlen(line) >= sizeof(SRate))
developer86035662023-06-28 19:21:12 +08006723 goto err;
developer32f2a182023-06-27 19:50:41 +08006724 strncpy(SRate, line, strlen(line));
developera3511852023-06-14 14:12:59 +08006725 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +08006726 if (tmp == NULL)
6727 goto err;
developera3511852023-06-14 14:12:59 +08006728 tmp = strtok(NULL, ":");
developer86035662023-06-28 19:21:12 +08006729 if (tmp == NULL)
6730 goto err;
developer32f2a182023-06-27 19:50:41 +08006731 if (strlen(tmp) >= sizeof(buf))
developer86035662023-06-28 19:21:12 +08006732 goto err;
developer32f2a182023-06-27 19:50:41 +08006733 strncpy(buf, tmp, strlen(tmp));
developera3511852023-06-14 14:12:59 +08006734 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +08006735
developera3511852023-06-14 14:12:59 +08006736 tmp = strtok(buf, " \n");
6737 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +08006738 if (strlen(tmp) >= (sizeof(SRate) - strlen(SRate)))
developer86035662023-06-28 19:21:12 +08006739 goto err;
developer32f2a182023-06-27 19:50:41 +08006740 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +08006741 if (SRate[strlen(SRate) - 1] == '*') {
6742 SRate[strlen(SRate) - 1] = '\0';
6743 }
developer32f2a182023-06-27 19:50:41 +08006744 if (strlen(SRate) >= (sizeof(SRate) - 1))
developer86035662023-06-28 19:21:12 +08006745 goto err;
developer32f2a182023-06-27 19:50:41 +08006746 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +08006747
developera3511852023-06-14 14:12:59 +08006748 tmp = strtok(NULL, " \n");
6749 }
6750 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +08006751 if (sizeof(scan_array[index].ap_SupportedDataTransferRates) <= strlen(SRate))
developer86035662023-06-28 19:21:12 +08006752 goto err;
developer32f2a182023-06-27 19:50:41 +08006753 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, strlen(SRate));
developera3511852023-06-14 14:12:59 +08006754 } else if (strstr(line, "DTIM") != NULL) {
developer86035662023-06-28 19:21:12 +08006755 if (sscanf(line,"DTIM Period %u", &(scan_array[index].ap_DTIMPeriod)) != 1) {
6756 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006757 }
developera3511852023-06-14 14:12:59 +08006758 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006759 if (sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) <= 4)
developer86035662023-06-28 19:21:12 +08006760 goto err;
developer32f2a182023-06-27 19:50:41 +08006761 strncat(scan_array[index].ap_SupportedStandards, ",ac",
6762 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6763 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
6764 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +08006765 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006766 strncat(scan_array[index].ap_SupportedStandards, ",n", sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6767 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
6768 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +08006769 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006770 if (fgets(line, sizeof(line), f) == NULL) {
6771 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6772 goto err;
6773 }
6774 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
6775 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6776 goto err;
6777 }
developera3511852023-06-14 14:12:59 +08006778 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +08006779 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +08006780 } else {
developere40952c2023-06-15 18:46:43 +08006781 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +08006782 }
developere40952c2023-06-15 18:46:43 +08006783 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6784 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +08006785 goto err;
developere40952c2023-06-15 18:46:43 +08006786 }
6787
developera3511852023-06-14 14:12:59 +08006788 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6789 continue;
6790 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006791 if (fgets(line, sizeof(line), f) == NULL) {
6792 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6793 goto err;
6794 }
developerc14d83a2023-06-29 20:09:42 +08006795 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
developer86035662023-06-28 19:21:12 +08006796 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6797 goto err;
6798 }
developera3511852023-06-14 14:12:59 +08006799 if (!strcmp(buf, "above")) {
6800 //40Mhz +
developere40952c2023-06-15 18:46:43 +08006801 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 +08006802 }
6803 else if (!strcmp(buf, "below")) {
6804 //40Mhz -
developere40952c2023-06-15 18:46:43 +08006805 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 +08006806 } else {
6807 //20Mhz
developere40952c2023-06-15 18:46:43 +08006808 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT20", radioIndex%1 ? "A": "G");
6809 }
6810 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6811 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +08006812 goto err;
developera3511852023-06-14 14:12:59 +08006813 }
developere40952c2023-06-15 18:46:43 +08006814
developera3511852023-06-14 14:12:59 +08006815 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6816 continue;
6817 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006818 strncat(scan_array[index].ap_SupportedStandards, ",ax",
6819 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6820 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
6821 scan_array[index].ap_OperatingStandards[2] = '\0';
developer86035662023-06-28 19:21:12 +08006822 if (fgets(line, sizeof(line), f) == NULL) {
6823 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6824 goto err;
6825 }
developera3511852023-06-14 14:12:59 +08006826 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006827 if (strstr(line, "HE40/2.4GHz") != NULL) {
6828 len = strlen("11AXHE40PLUS");
6829 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
6830 } else {
6831 len = strlen("11AXHE20");
6832 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
6833 }
6834 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +08006835 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
6836 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006837 len = strlen("11AXHE80");
6838 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
6839 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developer86035662023-06-28 19:21:12 +08006840 if (fgets(line, sizeof(line), f) == NULL) {
6841 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6842 goto err;
6843 }
developera3511852023-06-14 14:12:59 +08006844 } else
6845 continue;
developer32f2a182023-06-27 19:50:41 +08006846 if (strstr(line, "HE160/5GHz") != NULL) {
6847 len = strlen("11AXHE160");
6848 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
6849 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
6850 }
developera3511852023-06-14 14:12:59 +08006851 }
6852 continue;
6853 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006854 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
6855 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006856 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006857 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
6858 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006859 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +08006860 if (sscanf(line, " * Group cipher: %64s", scan_array[index].ap_EncryptionMode) != 1) {
6861 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6862 goto err;
6863 }
developera3511852023-06-14 14:12:59 +08006864 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006865 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
6866 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +08006867 }
6868 }
developerb14b3462023-07-01 18:02:42 +08006869 ret = fgets(line, sizeof(line), f);
developera3511852023-06-14 14:12:59 +08006870 }
developer72fb0bb2023-01-11 09:46:29 +08006871
developera3511852023-06-14 14:12:59 +08006872 if (!filter_BSS) {
6873 *output_array_size = index + 1;
6874 } else {
6875 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
6876 *output_array_size = index;
6877 }
6878 *neighbor_ap_array = scan_array;
developer8078acf2023-08-04 18:52:48 +08006879 v_secure_pclose(f);
developera3511852023-06-14 14:12:59 +08006880 free(channels_noise_arr);
6881 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6882 return RETURN_OK;
developer86035662023-06-28 19:21:12 +08006883err:
developer8078acf2023-08-04 18:52:48 +08006884 v_secure_pclose(f);
developer86035662023-06-28 19:21:12 +08006885 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +08006886 if (scan_array)
6887 free(scan_array);
developer86035662023-06-28 19:21:12 +08006888 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6889 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006890}
6891
6892//>> Deprecated: used for old RDKB code.
6893INT wifi_getRadioWifiTrafficStats(INT radioIndex, wifi_radioTrafficStats_t *output_struct)
6894{
developera3511852023-06-14 14:12:59 +08006895 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006896
developera3511852023-06-14 14:12:59 +08006897 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6898 output_struct->wifi_PLCPErrorCount = 0;
6899 output_struct->wifi_FCSErrorCount = 0;
6900 output_struct->wifi_InvalidMACCount = 0;
6901 output_struct->wifi_PacketsOtherReceived = 0;
6902 output_struct->wifi_Noise = 0;
6903 status = RETURN_OK;
6904 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6905 return status;
developer72fb0bb2023-01-11 09:46:29 +08006906}
6907
6908INT wifi_getBasicTrafficStats(INT apIndex, wifi_basicTrafficStats_t *output_struct)
6909{
developera3511852023-06-14 14:12:59 +08006910 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +08006911
developera3511852023-06-14 14:12:59 +08006912 char buf[1280] = {0};
6913 char *pos = NULL;
developere40952c2023-06-15 18:46:43 +08006914 int res;
developer72fb0bb2023-01-11 09:46:29 +08006915
developera3511852023-06-14 14:12:59 +08006916 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6917 if (NULL == output_struct)
6918 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006919
developera3511852023-06-14 14:12:59 +08006920 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
6921 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006922
developera3511852023-06-14 14:12:59 +08006923 memset(output_struct, 0, sizeof(wifi_basicTrafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08006924
developer8078acf2023-08-04 18:52:48 +08006925 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s", interface_name);
6926 if (res) {
6927 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08006928 }
6929
developera3511852023-06-14 14:12:59 +08006930 pos = buf;
6931 if ((pos = strstr(pos, "RX packets:")) == NULL)
6932 return RETURN_ERR;
6933 output_struct->wifi_PacketsReceived = atoi(pos+strlen("RX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08006934
developera3511852023-06-14 14:12:59 +08006935 if ((pos = strstr(pos, "TX packets:")) == NULL)
6936 return RETURN_ERR;
6937 output_struct->wifi_PacketsSent = atoi(pos+strlen("TX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08006938
developera3511852023-06-14 14:12:59 +08006939 if ((pos = strstr(pos, "RX bytes:")) == NULL)
6940 return RETURN_ERR;
6941 output_struct->wifi_BytesReceived = atoi(pos+strlen("RX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08006942
developera3511852023-06-14 14:12:59 +08006943 if ((pos = strstr(pos, "TX bytes:")) == NULL)
6944 return RETURN_ERR;
6945 output_struct->wifi_BytesSent = atoi(pos+strlen("TX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08006946
developer8078acf2023-08-04 18:52:48 +08006947 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s list_sta | wc -l | tr -d '\n'", interface_name);
6948 if (res) {
6949 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer37646972023-06-29 10:58:43 +08006950 }
developer8078acf2023-08-04 18:52:48 +08006951
developer37646972023-06-29 10:58:43 +08006952 if (sscanf(buf, "%lu", &output_struct->wifi_Associations) == EOF)
6953 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer72fb0bb2023-01-11 09:46:29 +08006954
developera3511852023-06-14 14:12:59 +08006955 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6956 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006957}
6958
6959INT wifi_getWifiTrafficStats(INT apIndex, wifi_trafficStats_t *output_struct)
6960{
developera3511852023-06-14 14:12:59 +08006961 char interface_name[IF_NAME_SIZE] = {0};
6962 char interface_status[MAX_BUF_SIZE] = {0};
6963 char Value[MAX_BUF_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08006964 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08006965 int res;
developerc14d83a2023-06-29 20:09:42 +08006966 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +08006967
developera3511852023-06-14 14:12:59 +08006968 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6969 if (NULL == output_struct)
6970 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006971
developera3511852023-06-14 14:12:59 +08006972 memset(output_struct, 0, sizeof(wifi_trafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08006973
developera3511852023-06-14 14:12:59 +08006974 if (wifi_GetInterfaceName(apIndex,interface_name) != RETURN_OK)
6975 return RETURN_ERR;
6976 GetIfacestatus(interface_name, interface_status);
developer72fb0bb2023-01-11 09:46:29 +08006977
developerd08b7d52023-08-22 15:41:36 +08006978 if(0 != strncmp(interface_status, "1", 1))
developera3511852023-06-14 14:12:59 +08006979 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006980
developer33f13ba2023-07-12 16:19:06 +08006981 res = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", interface_name);
6982 if (res) {
6983 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08006984 return RETURN_ERR;
6985 }
developer72fb0bb2023-01-11 09:46:29 +08006986
developera3511852023-06-14 14:12:59 +08006987 fp = fopen("/tmp/SSID_Stats.txt", "r");
6988 if(fp == NULL)
6989 {
6990 printf("/tmp/SSID_Stats.txt not exists \n");
6991 return RETURN_ERR;
6992 }
developer37646972023-06-29 10:58:43 +08006993 if (fclose(fp) == EOF) {
6994 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
6995 return RETURN_ERR;
6996 }
developer72fb0bb2023-01-11 09:46:29 +08006997
developer8078acf2023-08-04 18:52:48 +08006998 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6999 if(res) {
7000 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer37646972023-06-29 10:58:43 +08007001 }
developer5b23cd02023-07-19 20:26:03 +08007002
developerc14d83a2023-06-29 20:09:42 +08007003 if (hal_strtoul(Value, 10, &tmp) < 0) {
7004 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007005 }
developerc14d83a2023-06-29 20:09:42 +08007006 output_struct->wifi_ErrorsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007007
developer8078acf2023-08-04 18:52:48 +08007008 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7009 if(res) {
7010 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer37646972023-06-29 10:58:43 +08007011 }
developerc14d83a2023-06-29 20:09:42 +08007012
7013 if (hal_strtoul(Value, 10, &tmp) < 0) {
7014 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007015 }
developerc14d83a2023-06-29 20:09:42 +08007016 output_struct->wifi_ErrorsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007017
developer8078acf2023-08-04 18:52:48 +08007018 res = _syscmd_secure(Value, sizeof(Value), "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7019 if(res) {
7020 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer37646972023-06-29 10:58:43 +08007021 }
developerc14d83a2023-06-29 20:09:42 +08007022
7023 if (hal_strtoul(Value, 10, &tmp) < 0) {
7024 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007025 }
developerc14d83a2023-06-29 20:09:42 +08007026 output_struct->wifi_DiscardedPacketsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007027
developer8078acf2023-08-04 18:52:48 +08007028 res = _syscmd_secure(Value, sizeof(Value),"cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7029 if(res) {
7030 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer37646972023-06-29 10:58:43 +08007031 }
developerc14d83a2023-06-29 20:09:42 +08007032
7033 if (hal_strtoul(Value, 10, &tmp) < 0) {
7034 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007035 }
developerc14d83a2023-06-29 20:09:42 +08007036 output_struct->wifi_DiscardedPacketsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007037
developera3511852023-06-14 14:12:59 +08007038 output_struct->wifi_UnicastPacketsSent = 0;
7039 output_struct->wifi_UnicastPacketsReceived = 0;
7040 output_struct->wifi_MulticastPacketsSent = 0;
7041 output_struct->wifi_MulticastPacketsReceived = 0;
7042 output_struct->wifi_BroadcastPacketsSent = 0;
7043 output_struct->wifi_BroadcastPacketsRecevied = 0;
7044 output_struct->wifi_UnknownPacketsReceived = 0;
developer72fb0bb2023-01-11 09:46:29 +08007045
developera3511852023-06-14 14:12:59 +08007046 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7047 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007048}
7049
7050INT wifi_getSSIDTrafficStats(INT apIndex, wifi_ssidTrafficStats_t *output_struct)
7051{
developera3511852023-06-14 14:12:59 +08007052 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007053
developera3511852023-06-14 14:12:59 +08007054 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7055 //Below values should get updated from hal
7056 output_struct->wifi_RetransCount=0;
7057 output_struct->wifi_FailedRetransCount=0;
7058 output_struct->wifi_RetryCount=0;
7059 output_struct->wifi_MultipleRetryCount=0;
7060 output_struct->wifi_ACKFailureCount=0;
7061 output_struct->wifi_AggregatedPacketCount=0;
developer72fb0bb2023-01-11 09:46:29 +08007062
developera3511852023-06-14 14:12:59 +08007063 status = RETURN_OK;
7064 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007065
developera3511852023-06-14 14:12:59 +08007066 return status;
developer72fb0bb2023-01-11 09:46:29 +08007067}
7068
7069INT wifi_getNeighboringWiFiDiagnosticResult(wifi_neighbor_ap_t **neighbor_ap_array, UINT *output_array_size)
7070{
developera3511852023-06-14 14:12:59 +08007071 INT status = RETURN_ERR;
7072 UINT index;
7073 wifi_neighbor_ap_t *pt=NULL;
developer72fb0bb2023-01-11 09:46:29 +08007074
developera3511852023-06-14 14:12:59 +08007075 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7076 *output_array_size=2;
7077 //zqiu: HAL alloc the array and return to caller. Caller response to free it.
7078 *neighbor_ap_array=(wifi_neighbor_ap_t *)calloc(sizeof(wifi_neighbor_ap_t), *output_array_size);
developer86035662023-06-28 19:21:12 +08007079 if (*neighbor_ap_array == NULL) {
7080 wifi_debug(DEBUG_ERROR, "calloc fail!\n");
7081 return RETURN_ERR;
7082 }
developera3511852023-06-14 14:12:59 +08007083 for (index = 0, pt=*neighbor_ap_array; index < *output_array_size; index++, pt++) {
developer32f2a182023-06-27 19:50:41 +08007084 pt->ap_Radio[0] = '\0';
7085 pt->ap_SSID[0] = '\0';
7086 pt->ap_BSSID[0] = '\0';
7087 pt->ap_Mode[0] = '\0';
developera3511852023-06-14 14:12:59 +08007088 pt->ap_Channel=1;
7089 pt->ap_SignalStrength=0;
developer32f2a182023-06-27 19:50:41 +08007090 pt->ap_SecurityModeEnabled[0] = '\0';
7091 pt->ap_EncryptionMode[0] = '\0';
7092 pt->ap_OperatingFrequencyBand[0] = '\0';
7093 pt->ap_SupportedStandards[0] = '\0';
7094 pt->ap_OperatingStandards[0] = '\0';
7095 pt->ap_OperatingChannelBandwidth[0] = '\0';
7096 pt->ap_BasicDataTransferRates[0] = '\0';
7097 pt->ap_SupportedDataTransferRates[0] = '\0';
developera3511852023-06-14 14:12:59 +08007098 pt->ap_BeaconPeriod=1;
7099 pt->ap_Noise=0;
developera3511852023-06-14 14:12:59 +08007100 pt->ap_DTIMPeriod=1;
7101 pt->ap_ChannelUtilization = 1;
7102 }
developer72fb0bb2023-01-11 09:46:29 +08007103
developera3511852023-06-14 14:12:59 +08007104 status = RETURN_OK;
7105 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007106
developera3511852023-06-14 14:12:59 +08007107 return status;
developer72fb0bb2023-01-11 09:46:29 +08007108}
7109
7110//----------------- AP HAL -------------------------------
7111
7112//>> Deprecated: used for old RDKB code.
7113INT wifi_getAllAssociatedDeviceDetail(INT apIndex, ULONG *output_ulong, wifi_device_t **output_struct)
7114{
developera3511852023-06-14 14:12:59 +08007115 if (NULL == output_ulong || NULL == output_struct)
7116 return RETURN_ERR;
7117 *output_ulong = 0;
7118 *output_struct = NULL;
7119 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007120}
7121
7122#ifdef HAL_NETLINK_IMPL
7123static int AssoDevInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +08007124 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7125 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +08007126 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
7127 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +08007128 char mac_addr[20];
7129 static int count=0;
7130 int rate=0;
developer72fb0bb2023-01-11 09:46:29 +08007131
developera3511852023-06-14 14:12:59 +08007132 wifi_device_info_t *out = (wifi_device_info_t*)arg;
developer72fb0bb2023-01-11 09:46:29 +08007133
developera3511852023-06-14 14:12:59 +08007134 nla_parse(tb,
7135 NL80211_ATTR_MAX,
7136 genlmsg_attrdata(gnlh, 0),
7137 genlmsg_attrlen(gnlh, 0),
7138 NULL);
developer72fb0bb2023-01-11 09:46:29 +08007139
developera3511852023-06-14 14:12:59 +08007140 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +08007141 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +08007142 return NL_SKIP;
7143 }
developer72fb0bb2023-01-11 09:46:29 +08007144
7145
developera3511852023-06-14 14:12:59 +08007146 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +08007147 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +08007148 return NL_SKIP;
7149 }
developer72fb0bb2023-01-11 09:46:29 +08007150
developera3511852023-06-14 14:12:59 +08007151 //devIndex starts from 1
7152 if( ++count == out->wifi_devIndex )
7153 {
7154 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
7155 //Getting the mac addrress
7156 mac_addr_aton(out->wifi_devMacAddress,mac_addr);
developer12fb9f62023-06-30 15:26:27 +08007157 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7158 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
7159 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7160 return NL_SKIP;
7161 }
developera3511852023-06-14 14:12:59 +08007162 }
developer72fb0bb2023-01-11 09:46:29 +08007163
developera3511852023-06-14 14:12:59 +08007164 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7165 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7166 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7167 out->wifi_devTxRate = rate/10;
7168 }
7169 }
developer72fb0bb2023-01-11 09:46:29 +08007170
developer12fb9f62023-06-30 15:26:27 +08007171 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7172 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) {
7173 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7174 return NL_SKIP;
7175 }
developera3511852023-06-14 14:12:59 +08007176 }
developera3511852023-06-14 14:12:59 +08007177 if(sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7178 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7179 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7180 out->wifi_devRxRate = rate/10;
7181 }
7182 }
7183 if(sinfo[NL80211_STA_INFO_SIGNAL_AVG])
7184 out->wifi_devSignalStrength = (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
developer72fb0bb2023-01-11 09:46:29 +08007185
developera3511852023-06-14 14:12:59 +08007186 out->wifi_devAssociatedDeviceAuthentiationState = 1;
7187 count = 0; //starts the count for next cycle
7188 return NL_STOP;
7189 }
developer72fb0bb2023-01-11 09:46:29 +08007190
developera3511852023-06-14 14:12:59 +08007191 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08007192
7193}
7194#endif
7195
7196INT wifi_getAssociatedDeviceDetail(INT apIndex, INT devIndex, wifi_device_t *output_struct)
7197{
developera3511852023-06-14 14:12:59 +08007198 Netlink nl = {0};
7199 char if_name[IF_NAME_SIZE] = {0};
7200 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08007201 int res;
developer72fb0bb2023-01-11 09:46:29 +08007202
developera3511852023-06-14 14:12:59 +08007203 wifi_device_info_t info = {0};
7204 info.wifi_devIndex = devIndex;
developer72fb0bb2023-01-11 09:46:29 +08007205
developera3511852023-06-14 14:12:59 +08007206 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
7207 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007208
developere40952c2023-06-15 18:46:43 +08007209 res = snprintf(if_name,sizeof(if_name),"%s", interface_name);
7210 if (os_snprintf_error(sizeof(if_name), res)) {
7211 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7212 return RETURN_ERR;
7213 }
developer72fb0bb2023-01-11 09:46:29 +08007214
developera3511852023-06-14 14:12:59 +08007215 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007216
developera3511852023-06-14 14:12:59 +08007217 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +08007218 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +08007219 return -1;
7220 }
developer72fb0bb2023-01-11 09:46:29 +08007221
developera3511852023-06-14 14:12:59 +08007222 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +08007223
developera3511852023-06-14 14:12:59 +08007224 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +08007225 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +08007226 nlfree(&nl);
7227 return -2;
7228 }
developer72fb0bb2023-01-11 09:46:29 +08007229
developera3511852023-06-14 14:12:59 +08007230 genlmsg_put(msg,
7231 NL_AUTO_PID,
7232 NL_AUTO_SEQ,
7233 nl.id,
7234 0,
7235 NLM_F_DUMP,
7236 NL80211_CMD_GET_STATION,
7237 0);
developer72fb0bb2023-01-11 09:46:29 +08007238
developera3511852023-06-14 14:12:59 +08007239 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
7240 nl_send_auto_complete(nl.socket, msg);
7241 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,AssoDevInfo_callback,&info);
7242 nl_recvmsgs(nl.socket, nl.cb);
7243 nlmsg_free(msg);
7244 nlfree(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007245
developera3511852023-06-14 14:12:59 +08007246 output_struct->wifi_devAssociatedDeviceAuthentiationState = info.wifi_devAssociatedDeviceAuthentiationState;
7247 output_struct->wifi_devRxRate = info.wifi_devRxRate;
7248 output_struct->wifi_devTxRate = info.wifi_devTxRate;
7249 output_struct->wifi_devSignalStrength = info.wifi_devSignalStrength;
7250 memcpy(&output_struct->wifi_devMacAddress, &info.wifi_devMacAddress, sizeof(info.wifi_devMacAddress));
7251 return RETURN_OK;
7252}
developer72fb0bb2023-01-11 09:46:29 +08007253
developera3511852023-06-14 14:12:59 +08007254INT wifi_kickAssociatedDevice(INT apIndex, wifi_device_t *device)
7255{
7256 if (NULL == device)
7257 return RETURN_ERR;
7258 return RETURN_OK;
7259}
7260//<<
developer72fb0bb2023-01-11 09:46:29 +08007261
developer72fb0bb2023-01-11 09:46:29 +08007262
7263//--------------wifi_ap_hal-----------------------------
7264//enables CTS protection for the radio used by this AP
7265INT wifi_setRadioCtsProtectionEnable(INT apIndex, BOOL enable)
7266{
developera3511852023-06-14 14:12:59 +08007267 //save config and Apply instantly
7268 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007269}
7270
7271// enables OBSS Coexistence - fall back to 20MHz if necessary for the radio used by this ap
7272INT wifi_setRadioObssCoexistenceEnable(INT apIndex, BOOL enable)
7273{
developera3511852023-06-14 14:12:59 +08007274 char config_file[64] = {'\0'};
7275 char config_dat_file[64] = {'\0'};
7276 char buf[64] = {'\0'};
7277 struct params list = {0};
7278 struct params dat = {0};
7279 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007280 int res;
developer72fb0bb2023-01-11 09:46:29 +08007281
developera3511852023-06-14 14:12:59 +08007282 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7283 list.name = "ht_coex";
developere40952c2023-06-15 18:46:43 +08007284 res = snprintf(buf, sizeof(buf), "%d", enable);
7285 if (os_snprintf_error(sizeof(buf), res)) {
7286 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7287 return RETURN_ERR;
7288 }
7289
developera3511852023-06-14 14:12:59 +08007290 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007291
developera3511852023-06-14 14:12:59 +08007292 dat.name = "HT_BSSCoexistence";
7293 dat.value = buf;
developerd1824452023-05-18 12:30:04 +08007294
developera3511852023-06-14 14:12:59 +08007295 band = wifi_index_to_band(apIndex);
developere40952c2023-06-15 18:46:43 +08007296 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7297 if (os_snprintf_error(sizeof(config_file), res)) {
7298 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7299 return RETURN_ERR;
7300 }
7301
7302 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7303 if (os_snprintf_error(sizeof(config_dat_file), res)) {
7304 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7305 return RETURN_ERR;
7306 }
7307
developera3511852023-06-14 14:12:59 +08007308 wifi_hostapdWrite(config_file, &list, 1);
7309 wifi_datfileWrite(config_dat_file, &dat, 1);
7310 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007311
developera3511852023-06-14 14:12:59 +08007312 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007313
developera3511852023-06-14 14:12:59 +08007314 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007315}
7316
7317//P3 // sets the fragmentation threshold in bytes for the radio used by this ap
7318INT wifi_setRadioFragmentationThreshold(INT apIndex, UINT threshold)
7319{
developera3511852023-06-14 14:12:59 +08007320 char config_file[MAX_BUF_SIZE] = {'\0'};
7321 char buf[MAX_BUF_SIZE] = {'\0'};
7322 struct params list;
developere40952c2023-06-15 18:46:43 +08007323 int res;
developer72fb0bb2023-01-11 09:46:29 +08007324
developera3511852023-06-14 14:12:59 +08007325 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7326 if (threshold < 256 || threshold > 2346 )
7327 return RETURN_ERR;
7328 list.name = "fragm_threshold";
developere40952c2023-06-15 18:46:43 +08007329 res = snprintf(buf, sizeof(buf), "%d", threshold);
7330 if (os_snprintf_error(sizeof(buf), res)) {
7331 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7332 return RETURN_ERR;
7333 }
7334
developera3511852023-06-14 14:12:59 +08007335 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007336
developere40952c2023-06-15 18:46:43 +08007337 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7338 if (os_snprintf_error(sizeof(config_file), res)) {
7339 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7340 return RETURN_ERR;
7341 }
7342
developera3511852023-06-14 14:12:59 +08007343 wifi_hostapdWrite(config_file, &list, 1);
7344 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007345
developera3511852023-06-14 14:12:59 +08007346 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007347
developera3511852023-06-14 14:12:59 +08007348 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007349}
7350
7351// enable STBC mode in the hardwarwe, 0 == not enabled, 1 == enabled
7352INT wifi_setRadioSTBCEnable(INT radioIndex, BOOL STBC_Enable)
7353{
developera3511852023-06-14 14:12:59 +08007354 char config_file[64] = {'\0'};
developer8078acf2023-08-04 18:52:48 +08007355
developera3511852023-06-14 14:12:59 +08007356 char buf[512] = {'\0'};
7357 char stbc_config[16] = {'\0'};
7358 wifi_band band;
7359 int iterator = 0;
7360 BOOL current_stbc = FALSE;
7361 int ant_count = 0;
7362 int ant_bitmap = 0;
7363 struct params list;
7364 char dat_file[64] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007365 int res;
developer72fb0bb2023-01-11 09:46:29 +08007366
developera3511852023-06-14 14:12:59 +08007367 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007368
developera3511852023-06-14 14:12:59 +08007369 band = wifi_index_to_band(radioIndex);
7370 if (band == band_invalid)
7371 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007372
developera3511852023-06-14 14:12:59 +08007373 if (band == band_2_4)
7374 iterator = 1;
7375 else if ((band == band_5) || (band == band_6))
7376 iterator = 2;
7377 else
7378 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007379
developera3511852023-06-14 14:12:59 +08007380 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);
7381 for (; ant_bitmap > 0; ant_bitmap >>= 1)
7382 ant_count += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08007383
developera3511852023-06-14 14:12:59 +08007384 if (ant_count == 1 && STBC_Enable == TRUE) {
developer75bd10c2023-06-27 11:34:08 +08007385 wifi_debug(DEBUG_ERROR, "can not enable STBC when using only one antenna\n");
developera3511852023-06-14 14:12:59 +08007386 return RETURN_OK;
7387 }
developer72fb0bb2023-01-11 09:46:29 +08007388
developere40952c2023-06-15 18:46:43 +08007389 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7390 if (os_snprintf_error(sizeof(config_file), res)) {
7391 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7392 return RETURN_ERR;
7393 }
developer72fb0bb2023-01-11 09:46:29 +08007394
developera3511852023-06-14 14:12:59 +08007395 // set ht and vht config
7396 for (int i = 0; i < iterator; i++) {
7397 memset(stbc_config, 0, sizeof(stbc_config));
developer8078acf2023-08-04 18:52:48 +08007398
developera3511852023-06-14 14:12:59 +08007399 memset(buf, 0, sizeof(buf));
7400 list.name = (i == 0)?"ht_capab":"vht_capab";
developere40952c2023-06-15 18:46:43 +08007401 res = snprintf(stbc_config, sizeof(stbc_config), "%s", list.name);
7402 if (os_snprintf_error(sizeof(stbc_config), res)) {
7403 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7404 return RETURN_ERR;
7405 }
7406
developer8078acf2023-08-04 18:52:48 +08007407 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep -E '^%s' | grep 'STBC'", config_file, stbc_config);
7408 if (res) {
7409 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007410 }
7411
developera3511852023-06-14 14:12:59 +08007412 if (strlen(buf) != 0)
7413 current_stbc = TRUE;
7414 if (current_stbc == STBC_Enable)
7415 continue;
developer72fb0bb2023-01-11 09:46:29 +08007416
developera3511852023-06-14 14:12:59 +08007417 if (STBC_Enable == TRUE) {
7418 // Append the STBC flags in capab config
developer8078acf2023-08-04 18:52:48 +08007419
developera3511852023-06-14 14:12:59 +08007420 if (i == 0)
developer8078acf2023-08-04 18:52:48 +08007421 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC][RX-STBC1]/' %s", config_file);
developera3511852023-06-14 14:12:59 +08007422 else
developer8078acf2023-08-04 18:52:48 +08007423 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1][RX-STBC-1]/' %s", config_file);
7424 if (res) {
7425 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007426 }
7427
developera3511852023-06-14 14:12:59 +08007428 } else if (STBC_Enable == FALSE) {
7429 // Remove the STBC flags and remain other flags in capab
developer8078acf2023-08-04 18:52:48 +08007430 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7431 if (res) {
7432 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007433 }
developer8078acf2023-08-04 18:52:48 +08007434 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[RX-STBC-?[1-3]*\\]//' %s", config_file);
7435 if (res) {
7436 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007437 }
7438
developera3511852023-06-14 14:12:59 +08007439 }
7440 wifi_hostapdRead(config_file, list.name, buf, sizeof(buf));
7441 list.value = buf;
7442 wifi_hostapdProcessUpdate(radioIndex, &list, 1);
7443 }
developere40952c2023-06-15 18:46:43 +08007444 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7445 if (os_snprintf_error(sizeof(dat_file), res)) {
7446 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7447 return RETURN_ERR;
7448 }
7449
developer8078acf2023-08-04 18:52:48 +08007450 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/^HT_STBC=.*/HT_STBC=%d/g' %s", STBC_Enable, dat_file);
7451 if (res) {
7452 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007453 }
7454
developera1255e42023-05-13 17:45:02 +08007455 if ((band == band_5) || (band == band_6)) {
developer8078acf2023-08-04 18:52:48 +08007456 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/^VHT_STBC=.*/VHT_STBC=%d/g' %s", STBC_Enable, dat_file);
7457 if (res) {
7458 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007459 }
7460
developera1255e42023-05-13 17:45:02 +08007461 }
developera3511852023-06-14 14:12:59 +08007462 /*wifi_reloadAp(radioIndex);
developera1255e42023-05-13 17:45:02 +08007463 the caller do this.*/
developer72fb0bb2023-01-11 09:46:29 +08007464
developera3511852023-06-14 14:12:59 +08007465 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7466 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007467}
7468
developer37ad6bf2023-10-09 11:31:05 +08007469int mtk_get_ap_amsdu_callback(struct nl_msg *msg, void *data) {
7470 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7471 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BSS_ATTR_MAX + 1];
7472 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7473 unsigned char status;
7474 unsigned char *out_status = data;
7475 int err = 0;
7476
7477 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7478 genlmsg_attrlen(gnlh, 0), NULL);
7479 if (err < 0){
7480 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
7481 return err;
7482 }
7483
7484 if (tb[NL80211_ATTR_VENDOR_DATA]) {
7485 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BSS_ATTR_MAX,
7486 tb[NL80211_ATTR_VENDOR_DATA], NULL);
7487 if (err < 0){
7488 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
7489 return err;
7490 }
7491
7492 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN]) {
7493 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN]);
7494 if (status == 0) {
7495 wifi_debug(DEBUG_INFO, "disabled\n");
7496 } else {
7497 wifi_debug(DEBUG_INFO, "enabled\n");
7498 }
7499 *out_status = status;
7500 }
7501 }
7502 return 0;
7503}
7504
developer72fb0bb2023-01-11 09:46:29 +08007505// outputs A-MSDU enable status, 0 == not enabled, 1 == enabled
7506INT wifi_getRadioAMSDUEnable(INT radioIndex, BOOL *output_bool)
7507{
developer37ad6bf2023-10-09 11:31:05 +08007508 char inf_name[IF_NAME_SIZE] = {0};
7509 unsigned int if_idx = 0;
7510 int ret = -1;
7511 struct unl unl_ins;
7512 struct nl_msg *msg = NULL;
7513 struct nlattr * msg_data = NULL;
7514 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +08007515
developer37ad6bf2023-10-09 11:31:05 +08007516 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
7517 return RETURN_ERR;
7518 if_idx = if_nametoindex(inf_name);
7519 /*init mtk nl80211 vendor cmd*/
7520 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
7521 param.if_type = NL80211_ATTR_IFINDEX;
7522 param.if_idx = if_idx;
developer72fb0bb2023-01-11 09:46:29 +08007523
developer37ad6bf2023-10-09 11:31:05 +08007524 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
7525 if (ret) {
7526 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developer2c22d832023-05-18 17:46:26 +08007527 return RETURN_ERR;
7528 }
developer37ad6bf2023-10-09 11:31:05 +08007529 /*add mtk vendor cmd data*/
7530 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN, 0xf)) {
7531 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN);
7532 nlmsg_free(msg);
7533 goto err;
developere40952c2023-06-15 18:46:43 +08007534 }
7535
developer37ad6bf2023-10-09 11:31:05 +08007536 /*send mtk nl80211 vendor msg*/
7537 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_get_ap_amsdu_callback, output_bool);
7538 if (ret) {
7539 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
7540 goto err;
7541 }
7542 /*deinit mtk nl80211 vendor msg*/
7543 mtk_nl80211_deint(&unl_ins);
7544 wifi_debug(DEBUG_INFO,"send cmd success, get output_bool:%d\n", *output_bool);
developer2c22d832023-05-18 17:46:26 +08007545 return RETURN_OK;
developer37ad6bf2023-10-09 11:31:05 +08007546err:
7547 mtk_nl80211_deint(&unl_ins);
7548 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
7549 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007550}
7551
7552// enables A-MSDU in the hardware, 0 == not enabled, 1 == enabled
7553INT wifi_setRadioAMSDUEnable(INT radioIndex, BOOL amsduEnable)
7554{
developer37ad6bf2023-10-09 11:31:05 +08007555 char inf_name[IF_NAME_SIZE] = {0};
7556 unsigned int if_idx = 0;
7557 struct unl unl_ins;
7558 struct nl_msg *msg = NULL;
7559 struct nlattr * msg_data = NULL;
7560 struct mtk_nl80211_param param;
7561 int ret = -1;
developer72fb0bb2023-01-11 09:46:29 +08007562
developer37ad6bf2023-10-09 11:31:05 +08007563 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
developer2c22d832023-05-18 17:46:26 +08007564 return RETURN_ERR;
developer37ad6bf2023-10-09 11:31:05 +08007565 if_idx = if_nametoindex(inf_name);
7566 /*init mtk nl80211 vendor cmd*/
7567 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
7568 param.if_type = NL80211_ATTR_IFINDEX;
7569 param.if_idx = if_idx;
7570 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
7571 if (ret) {
7572 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developere40952c2023-06-15 18:46:43 +08007573 return RETURN_ERR;
7574 }
developer37ad6bf2023-10-09 11:31:05 +08007575 /*add mtk vendor cmd data*/
7576 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN, amsduEnable)) {
7577 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN);
7578 nlmsg_free(msg);
7579 goto err;
7580 }
developer72fb0bb2023-01-11 09:46:29 +08007581
developer37ad6bf2023-10-09 11:31:05 +08007582 /*send mtk nl80211 vendor msg*/
7583 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
7584 if (ret) {
7585 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
7586 goto err;
7587 }
7588 /*deinit mtk nl80211 vendor msg*/
7589 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08007590 return RETURN_OK;
developer37ad6bf2023-10-09 11:31:05 +08007591err:
7592 mtk_nl80211_deint(&unl_ins);
7593 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
7594 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007595}
7596
7597//P2 // outputs the number of Tx streams
7598INT wifi_getRadioTxChainMask(INT radioIndex, INT *output_int)
7599{
developera3511852023-06-14 14:12:59 +08007600 char buf[8] = {0};
developera3511852023-06-14 14:12:59 +08007601 int phyId = 0;
developere40952c2023-06-15 18:46:43 +08007602 int res;
developerc14d83a2023-06-29 20:09:42 +08007603 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007604
developera3511852023-06-14 14:12:59 +08007605 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007606
developera3511852023-06-14 14:12:59 +08007607 phyId = radio_index_to_phy(radioIndex);
developer8078acf2023-08-04 18:52:48 +08007608
7609 res = _syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep 'Configured Antennas' | awk '{print $4}'", phyId);
7610 if (res) {
7611 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007612 }
7613
developer72fb0bb2023-01-11 09:46:29 +08007614
developerc14d83a2023-06-29 20:09:42 +08007615 if (hal_strtol(buf, 16, &tmp) < 0) {
7616 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007617 }
developerc14d83a2023-06-29 20:09:42 +08007618 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007619
developera3511852023-06-14 14:12:59 +08007620 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007621
developera3511852023-06-14 14:12:59 +08007622 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007623}
7624
7625INT fitChainMask(INT radioIndex, int antcount)
7626{
developera3511852023-06-14 14:12:59 +08007627 char buf[128] = {0};
developer8078acf2023-08-04 18:52:48 +08007628
developera3511852023-06-14 14:12:59 +08007629 char config_file[64] = {0};
7630 wifi_band band;
7631 struct params list[2] = {0};
developere40952c2023-06-15 18:46:43 +08007632 int res;
developer72fb0bb2023-01-11 09:46:29 +08007633
developera3511852023-06-14 14:12:59 +08007634 band = wifi_index_to_band(radioIndex);
7635 if (band == band_invalid)
7636 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007637
developera3511852023-06-14 14:12:59 +08007638 list[0].name = "he_mu_beamformer";
7639 list[1].name = "he_su_beamformer";
developer72fb0bb2023-01-11 09:46:29 +08007640
developere40952c2023-06-15 18:46:43 +08007641 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7642 if (os_snprintf_error(sizeof(config_file), res)) {
7643 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7644 return RETURN_ERR;
7645 }
7646
developera3511852023-06-14 14:12:59 +08007647 if (antcount == 1) {
7648 // remove config about multiple antennas
developer8078acf2023-08-04 18:52:48 +08007649 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7650 if (res) {
7651 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007652 }
7653
developer8078acf2023-08-04 18:52:48 +08007654 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[SOUNDING-DIMENSION-.\\]//' %s", config_file);
7655 if (res) {
7656 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007657 }
7658
developer8078acf2023-08-04 18:52:48 +08007659 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[SU-BEAMFORMER\\]//' %s", config_file);
7660 if (res) {
7661 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007662 }
7663
developer8078acf2023-08-04 18:52:48 +08007664 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[MU-BEAMFORMER\\]//' %s", config_file);
7665 if (res) {
7666 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007667 }
7668
developera3511852023-06-14 14:12:59 +08007669 list[0].value = "0";
7670 list[1].value = "0";
7671 } else {
7672 // 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.
7673 if (band == band_2_4 || band == band_5) {
developer8078acf2023-08-04 18:52:48 +08007674 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '^ht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7675 if (res) {
7676 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007677 }
7678
developera3511852023-06-14 14:12:59 +08007679 if (strlen(buf) > 0) {
developer8078acf2023-08-04 18:52:48 +08007680 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC]/' %s", config_file);
7681 if (res) {
7682 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007683 }
developera3511852023-06-14 14:12:59 +08007684 }
7685 }
7686 if (band == band_5) {
developer8078acf2023-08-04 18:52:48 +08007687 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '^vht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7688 if (res) {
7689 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007690 }
developera3511852023-06-14 14:12:59 +08007691 if (strlen(buf) > 0) {
developer8078acf2023-08-04 18:52:48 +08007692 res = _syscmd_secure(buf, sizeof(buf) , "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1]/' %s", config_file);
7693 if (res) {
7694 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007695 }
developera3511852023-06-14 14:12:59 +08007696 }
7697 }
developer72fb0bb2023-01-11 09:46:29 +08007698
developer8078acf2023-08-04 18:52:48 +08007699 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '\\[SU-BEAMFORMER\\]'", config_file);
7700 if (res) {
7701 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007702 }
7703
developera3511852023-06-14 14:12:59 +08007704 if (strlen(buf) == 0) {
developer8078acf2023-08-04 18:52:48 +08007705 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^vht_capab=.*/s/$/[SU-BEAMFORMER]/' %s", config_file);
7706 if (res) {
7707 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007708 }
developera3511852023-06-14 14:12:59 +08007709 }
developer72fb0bb2023-01-11 09:46:29 +08007710
developer8078acf2023-08-04 18:52:48 +08007711 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '\\[MU-BEAMFORMER\\]'", config_file);
7712 if (res) {
7713 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developerc14d83a2023-06-29 20:09:42 +08007714 }
developera3511852023-06-14 14:12:59 +08007715 if (strlen(buf) == 0) {
developer8078acf2023-08-04 18:52:48 +08007716 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^vht_capab=.*/s/$/[MU-BEAMFORMER]/' %s", config_file);
7717 if (res) {
7718 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08007719 }
developere40952c2023-06-15 18:46:43 +08007720 }
7721
developer8078acf2023-08-04 18:52:48 +08007722 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '\\[SOUNDING-DIMENSION-.\\]'", config_file);
7723 if (res) {
7724 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developera3511852023-06-14 14:12:59 +08007725 }
developera3511852023-06-14 14:12:59 +08007726 if (strlen(buf) == 0) {
developer8078acf2023-08-04 18:52:48 +08007727 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^vht_capab=.*/s/$/[SOUNDING-DIMENSION-%d]/' %s", antcount, config_file);
developera3511852023-06-14 14:12:59 +08007728 } else {
developer8078acf2023-08-04 18:52:48 +08007729 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/(SOUNDING-DIMENSION-)./\\1%d/' %s", antcount, config_file);
developere40952c2023-06-15 18:46:43 +08007730 }
developer8078acf2023-08-04 18:52:48 +08007731 if (res) {
7732 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developera3511852023-06-14 14:12:59 +08007733 }
developere40952c2023-06-15 18:46:43 +08007734
developera3511852023-06-14 14:12:59 +08007735 list[0].value = "1";
7736 list[1].value = "1";
7737 }
7738 wifi_hostapdWrite(config_file, list, 2);
developerdaf24792023-06-06 11:40:04 +08007739 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007740}
7741
7742//P2 // sets the number of Tx streams to an enviornment variable
7743INT wifi_setRadioTxChainMask(INT radioIndex, INT numStreams)
7744{
developera3511852023-06-14 14:12:59 +08007745 char cmd[128] = {0};
7746 char buf[128] = {0};
7747 int phyId = 0;
7748 int cur_mask = 0;
7749 int antcountmsk = 0;
developera1255e42023-05-13 17:45:02 +08007750 INT cur_nss = 0;
developer863a4a62023-06-06 16:55:59 +08007751 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +08007752 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007753 int res;
developer72fb0bb2023-01-11 09:46:29 +08007754
developera3511852023-06-14 14:12:59 +08007755 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007756
developera3511852023-06-14 14:12:59 +08007757 if (numStreams <= 0) {
developer75bd10c2023-06-27 11:34:08 +08007758 wifi_debug(DEBUG_ERROR, "chainmask is not supported %d.\n", numStreams);
developera3511852023-06-14 14:12:59 +08007759 return RETURN_ERR;
7760 }
developer72fb0bb2023-01-11 09:46:29 +08007761
developera3511852023-06-14 14:12:59 +08007762 wifi_getRadioTxChainMask(radioIndex, &cur_mask);//this is mask value
developera1255e42023-05-13 17:45:02 +08007763 for(; cur_mask > 0; cur_mask >>= 1)//convert to number of streams.
7764 cur_nss += 1;
7765 WIFI_ENTRY_EXIT_DEBUG("%s:cur_nss=%d, new_nss=%d\n", __func__, cur_nss, numStreams);
developera3511852023-06-14 14:12:59 +08007766 if (cur_nss == numStreams)
7767 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007768
developera3511852023-06-14 14:12:59 +08007769 wifi_setRadioEnable(radioIndex, FALSE);
developer72fb0bb2023-01-11 09:46:29 +08007770
developera3511852023-06-14 14:12:59 +08007771 phyId = radio_index_to_phy(radioIndex);
developera1255e42023-05-13 17:45:02 +08007772 //iw need mask value.
7773 for (;numStreams > 0; numStreams--)
7774 antcountmsk |= 0x1 << (numStreams - 1);
developere40952c2023-06-15 18:46:43 +08007775 res = snprintf(cmd, sizeof(cmd), "iw phy%d set antenna 0x%x 2>&1", phyId, antcountmsk);
7776 if (os_snprintf_error(sizeof(cmd), res)) {
7777 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7778 return RETURN_ERR;
7779 }
developerb758dfd2023-06-21 17:32:07 +08007780
developer8078acf2023-08-04 18:52:48 +08007781 res=_syscmd_secure(buf, sizeof(buf), "iw phy%d set antenna 0x%x 2>&1", phyId, antcountmsk);
7782 if (res) {
7783 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
7784 }
7785
developera3511852023-06-14 14:12:59 +08007786 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007787 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007788 return RETURN_ERR;
7789 }
7790 band = wifi_index_to_band(radioIndex);
developera1255e42023-05-13 17:45:02 +08007791 if (band == band_invalid) {
7792 printf("%s:Band Error\n", __func__);
7793 return RETURN_ERR;
7794 }
developere40952c2023-06-15 18:46:43 +08007795 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7796 if (os_snprintf_error(sizeof(dat_file), res)) {
7797 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7798 return RETURN_ERR;
7799 }
developerb758dfd2023-06-21 17:32:07 +08007800
developere40952c2023-06-15 18:46:43 +08007801 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_TxStream=.*/HT_TxStream=%d/g' %s", numStreams, dat_file);
7802 if (os_snprintf_error(sizeof(cmd), res)) {
7803 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7804 return RETURN_ERR;
7805 }
developerb758dfd2023-06-21 17:32:07 +08007806
developer8078acf2023-08-04 18:52:48 +08007807 res=_syscmd_secure(buf, sizeof(buf), "sed -r -i 's/^HT_TxStream=.*/HT_TxStream=%d/g' %s", numStreams, dat_file);
7808 if (res) {
7809 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
7810 }
7811
developera3511852023-06-14 14:12:59 +08007812 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007813 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007814 return RETURN_ERR;
7815 }
developer8078acf2023-08-04 18:52:48 +08007816
developere40952c2023-06-15 18:46:43 +08007817 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_RxStream=.*/HT_RxStream=%d/g' %s", numStreams, dat_file);
7818 if (os_snprintf_error(sizeof(cmd), res)) {
7819 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7820 return RETURN_ERR;
7821 }
developerb758dfd2023-06-21 17:32:07 +08007822
developer8078acf2023-08-04 18:52:48 +08007823 res=_syscmd_secure(buf, sizeof(buf), "sed -r -i 's/^HT_RxStream=.*/HT_RxStream=%d/g' %s", numStreams, dat_file);
7824 if (res) {
7825 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
7826 }
7827
developera3511852023-06-14 14:12:59 +08007828 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007829 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007830 return RETURN_ERR;
7831 }
7832 fitChainMask(radioIndex, numStreams);
7833 wifi_setRadioEnable(radioIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +08007834
developera3511852023-06-14 14:12:59 +08007835 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7836 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007837}
7838
7839//P2 // outputs the number of Rx streams
7840INT wifi_getRadioRxChainMask(INT radioIndex, INT *output_int)
7841{
developera3511852023-06-14 14:12:59 +08007842 char buf[8] = {0};
developera3511852023-06-14 14:12:59 +08007843 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +08007844 int res;
developerc14d83a2023-06-29 20:09:42 +08007845 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007846
developera3511852023-06-14 14:12:59 +08007847 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007848
developera3511852023-06-14 14:12:59 +08007849 phyId = radio_index_to_phy(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08007850
developer8078acf2023-08-04 18:52:48 +08007851 res=_syscmd_secure(buf, sizeof(buf), "iw phy%d info | grep 'Configured Antennas' | awk '{print $6}'", phyId);
7852 if (res) {
7853 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08007854 }
developer72fb0bb2023-01-11 09:46:29 +08007855
developerc14d83a2023-06-29 20:09:42 +08007856 if (hal_strtol(buf, 16, &tmp) < 0) {
7857 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007858 }
developerc14d83a2023-06-29 20:09:42 +08007859 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007860
developera3511852023-06-14 14:12:59 +08007861 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007862
developera3511852023-06-14 14:12:59 +08007863 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007864}
7865
7866//P2 // sets the number of Rx streams to an enviornment variable
7867INT wifi_setRadioRxChainMask(INT radioIndex, INT numStreams)
7868{
developera3511852023-06-14 14:12:59 +08007869 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7870 if (wifi_setRadioTxChainMask(radioIndex, numStreams) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08007871 wifi_debug(DEBUG_ERROR, "wifi_setRadioTxChainMask return error.\n");
developera3511852023-06-14 14:12:59 +08007872 return RETURN_ERR;
7873 }
7874 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7875 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007876}
7877
7878//Get radio RDG enable setting
7879INT wifi_getRadioReverseDirectionGrantSupported(INT radioIndex, BOOL *output_bool)
7880{
developer47cc27a2023-05-17 23:09:58 +08007881 if (NULL == output_bool)
7882 return RETURN_ERR;
7883
7884 *output_bool = TRUE;
7885 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007886}
7887
7888//Get radio RDG enable setting
7889INT wifi_getRadioReverseDirectionGrantEnable(INT radioIndex, BOOL *output_bool)
7890{
developer47cc27a2023-05-17 23:09:58 +08007891 char rdg_status[2] = {0};
7892 char dat_file[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08007893 int res;
developer47cc27a2023-05-17 23:09:58 +08007894
7895 if (NULL == output_bool)
7896 return RETURN_ERR;
7897
7898 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08007899 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
7900 if (os_snprintf_error(sizeof(dat_file), res)) {
7901 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7902 return RETURN_ERR;
7903 }
developer47cc27a2023-05-17 23:09:58 +08007904
7905 wifi_datfileRead(dat_file, "HT_RDG", rdg_status, sizeof(rdg_status));
7906 if (!strncmp(rdg_status, "1", sizeof(rdg_status)))
7907 *output_bool = TRUE;
7908 else
7909 *output_bool = FALSE;
7910
7911 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007912}
7913
7914//Set radio RDG enable setting
7915INT wifi_setRadioReverseDirectionGrantEnable(INT radioIndex, BOOL enable)
7916{
developer47cc27a2023-05-17 23:09:58 +08007917 char dat_file[MAX_CMD_SIZE] = {0};
7918 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08007919 int res;
developer47cc27a2023-05-17 23:09:58 +08007920
7921 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08007922 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
7923 if (os_snprintf_error(sizeof(dat_file), res)) {
7924 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7925 return RETURN_ERR;
7926 }
developer47cc27a2023-05-17 23:09:58 +08007927
7928 params.name = "HT_RDG";
7929
developera3511852023-06-14 14:12:59 +08007930 if (enable) {
7931 params.value = "1";
7932 } else {
7933 params.value = "0";
7934 }
developer47cc27a2023-05-17 23:09:58 +08007935
7936 wifi_datfileWrite(dat_file, &params, 1);
7937
7938 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007939}
7940
developer5cd4c862023-05-26 09:34:42 +08007941
7942int mtk_get_ba_auto_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08007943{
developer5cd4c862023-05-26 09:34:42 +08007944 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7945 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
7946 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7947 unsigned char status;
7948 unsigned char *out_status = data;
7949 int err = 0;
developer8e6583c2023-05-23 13:36:06 +08007950
developer5cd4c862023-05-26 09:34:42 +08007951 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7952 genlmsg_attrlen(gnlh, 0), NULL);
7953 if (err < 0){
7954 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
7955 return err;
7956 }
developer8e6583c2023-05-23 13:36:06 +08007957
developer5cd4c862023-05-26 09:34:42 +08007958 if (tb[NL80211_ATTR_VENDOR_DATA]) {
7959 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
7960 tb[NL80211_ATTR_VENDOR_DATA], NULL);
7961 if (err < 0){
7962 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
7963 return err;
7964 }
developer8e6583c2023-05-23 13:36:06 +08007965
developer5cd4c862023-05-26 09:34:42 +08007966 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]) {
7967 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]);
7968 if (status == 0) {
7969 wifi_debug(DEBUG_NOTICE, "disabled\n");
7970 } else {
7971 wifi_debug(DEBUG_NOTICE, "enabled\n");
7972 }
7973 *out_status = status;
7974 }
7975 }
developer8e6583c2023-05-23 13:36:06 +08007976
developer5cd4c862023-05-26 09:34:42 +08007977 return 0;
7978}
developer8e6583c2023-05-23 13:36:06 +08007979
developer5cd4c862023-05-26 09:34:42 +08007980int mtk_get_ba_decline_status_callback(struct nl_msg *msg, void *data)
7981{
7982 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7983 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
7984 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7985 unsigned char status;
7986 unsigned char *out_status = data;
7987 int err = 0;
7988
7989 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7990 genlmsg_attrlen(gnlh, 0), NULL);
7991 if (err < 0) {
7992 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
7993 return err;
7994 }
7995
7996 if (tb[NL80211_ATTR_VENDOR_DATA]) {
7997 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
7998 tb[NL80211_ATTR_VENDOR_DATA], NULL);
7999 if (err < 0) {
8000 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8001 return err;
8002 }
8003
8004 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]) {
8005 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]);
8006 if (status == 0) {
8007 wifi_debug(DEBUG_NOTICE, "disabled\n");
8008 } else {
8009 wifi_debug(DEBUG_NOTICE, "enabled\n");
8010 }
8011 *out_status = status;
8012 }
8013 }
8014
8015 return NL_OK;
developer72fb0bb2023-01-11 09:46:29 +08008016}
8017
developer5cd4c862023-05-26 09:34:42 +08008018INT mtk_wifi_get_ba_decl_auto_status(
8019 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back, BOOL *output_bool)
8020{
8021 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008022 unsigned int if_idx = 0;
8023 int ret = -1;
8024 struct unl unl_ins;
8025 struct nl_msg *msg = NULL;
8026 struct nlattr * msg_data = NULL;
8027 struct mtk_nl80211_param param;
8028
8029 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8030 return RETURN_ERR;
8031 if_idx = if_nametoindex(inf_name);
8032 if (!if_idx) {
8033 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8034 return RETURN_ERR;
8035 }
8036 /*init mtk nl80211 vendor cmd*/
8037 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8038 param.if_type = NL80211_ATTR_IFINDEX;
8039 param.if_idx = if_idx;
8040
8041 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8042 if (ret) {
8043 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8044 return RETURN_ERR;
8045 }
8046 /*add mtk vendor cmd data*/
8047 if (nla_put_u8(msg, vendor_data_attr, 0xf)) {
8048 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8049 nlmsg_free(msg);
8050 goto err;
8051 }
8052
8053 /*send mtk nl80211 vendor msg*/
8054 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8055 if (ret) {
8056 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8057 goto err;
8058 }
8059 /*deinit mtk nl80211 vendor msg*/
8060 mtk_nl80211_deint(&unl_ins);
8061 wifi_debug(DEBUG_NOTICE,"send cmd success, get output_bool:%d\n", *output_bool);
8062 return RETURN_OK;
8063err:
8064 mtk_nl80211_deint(&unl_ins);
8065 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8066 return RETURN_ERR;
8067}
developere0ff7232023-06-08 16:33:14 +08008068
8069INT mtk_wifi_set_auto_ba_en(
8070 INT apIndex, INT vendor_data_attr, BOOL enable)
8071{
8072 char inf_name[IF_NAME_SIZE] = {0};
8073 unsigned int if_idx = 0;
8074 int ret = -1;
8075 struct unl unl_ins;
8076 struct nl_msg *msg = NULL;
8077 struct nlattr * msg_data = NULL;
8078 struct mtk_nl80211_param param;
8079
8080 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8081 return RETURN_ERR;
8082 if_idx = if_nametoindex(inf_name);
8083 if (!if_idx) {
8084 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8085 return RETURN_ERR;
8086 }
8087 /*init mtk nl80211 vendor cmd*/
8088 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8089 param.if_type = NL80211_ATTR_IFINDEX;
8090 param.if_idx = if_idx;
8091
8092 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8093 if (ret) {
8094 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8095 return RETURN_ERR;
8096 }
8097 /*add mtk vendor cmd data*/
8098 if (nla_put_u8(msg, vendor_data_attr, enable)) {
8099 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8100 nlmsg_free(msg);
8101 goto err;
8102 }
8103
8104 /*send mtk nl80211 vendor msg*/
8105 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8106 if (ret) {
8107 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8108 goto err;
8109 }
8110 /*deinit mtk nl80211 vendor msg*/
8111 mtk_nl80211_deint(&unl_ins);
8112 return RETURN_OK;
8113err:
8114 mtk_nl80211_deint(&unl_ins);
8115 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8116 return RETURN_ERR;
8117}
8118
developer5cd4c862023-05-26 09:34:42 +08008119//Get radio ADDBA enable setting
8120INT wifi_getRadioDeclineBARequestEnable(INT radioIndex, BOOL *output_bool)
8121{
8122 if (output_bool == NULL) {
8123 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8124 return RETURN_ERR;
8125 }
8126 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
8127 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO, mtk_get_ba_decline_status_callback, output_bool) != RETURN_OK) {
8128 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO(0x%x) fails\n",
8129 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO);
8130 return RETURN_ERR;
8131 }
8132 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8133 return RETURN_OK;
8134}
8135
developer72fb0bb2023-01-11 09:46:29 +08008136//Set radio ADDBA enable setting
8137INT wifi_setRadioDeclineBARequestEnable(INT radioIndex, BOOL enable)
8138{
developera3511852023-06-14 14:12:59 +08008139 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008140}
8141
8142//Get radio auto block ack enable setting
8143INT wifi_getRadioAutoBlockAckEnable(INT radioIndex, BOOL *output_bool)
8144{
developer5cd4c862023-05-26 09:34:42 +08008145 if (output_bool == NULL) {
8146 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8147 return RETURN_ERR;
8148 }
developer8e6583c2023-05-23 13:36:06 +08008149
developera3511852023-06-14 14:12:59 +08008150 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
developer5cd4c862023-05-26 09:34:42 +08008151 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO,
8152 mtk_get_ba_auto_status_callback, output_bool) != RETURN_OK) {
8153 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO(0x%x) fails\n",
8154 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO);
8155 return RETURN_ERR;
8156 }
8157 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8158 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008159}
8160
8161//Set radio auto block ack enable setting
8162INT wifi_setRadioAutoBlockAckEnable(INT radioIndex, BOOL enable)
8163{
developera3511852023-06-14 14:12:59 +08008164 if (mtk_wifi_set_auto_ba_en
developere0ff7232023-06-08 16:33:14 +08008165 (radioIndex, MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO, enable) != RETURN_OK) {
8166 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO cmd fails\n");
8167 return RETURN_ERR;
8168 }
8169 wifi_debug(DEBUG_ERROR, "send cmd success: set auto ba enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008170 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008171}
8172
8173//Get radio 11n pure mode enable support
8174INT wifi_getRadio11nGreenfieldSupported(INT radioIndex, BOOL *output_bool)
8175{
developera3511852023-06-14 14:12:59 +08008176 if (NULL == output_bool)
8177 return RETURN_ERR;
8178 *output_bool = TRUE;
8179 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008180}
8181
8182//Get radio 11n pure mode enable setting
8183INT wifi_getRadio11nGreenfieldEnable(INT radioIndex, BOOL *output_bool)
8184{
developera3511852023-06-14 14:12:59 +08008185 if (NULL == output_bool)
8186 return RETURN_ERR;
8187 *output_bool = TRUE;
8188 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008189}
8190
8191//Set radio 11n pure mode enable setting
developer86035662023-06-28 19:21:12 +08008192INT wifi_setRadio11nGreenfieldEnable(INT radioIndex, BOOL enable)
developer72fb0bb2023-01-11 09:46:29 +08008193{
developer82533be2023-06-28 17:21:01 +08008194 char interface_name[16] = {0};
8195 int if_idx, ret = 0;
8196 struct nl_msg *msg = NULL;
8197 struct nlattr * msg_data = NULL;
8198 struct mtk_nl80211_param param;
8199 struct unl unl_ins;
8200
8201 if (radioIndex > MAX_APS) {
8202 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", radioIndex);
8203 return RETURN_ERR;
8204 }
8205 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8206 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
8207 return RETURN_ERR;
developerd14dff12023-06-28 22:47:44 +08008208
developer82533be2023-06-28 17:21:01 +08008209 if_idx = if_nametoindex(interface_name);
8210 /*init mtk nl80211 vendor cmd*/
8211 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
8212 param.if_type = NL80211_ATTR_IFINDEX;
8213 param.if_idx = if_idx;
8214 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8215 if (ret) {
8216 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8217 return RETURN_ERR;
8218 }
8219 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_HT_OP_MODE, enable)) {
8220 printf("Nla put attribute error\n");
8221 nlmsg_free(msg);
8222 goto err;
8223 }
8224 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8225 if (ret) {
8226 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
8227 goto err;
8228 }
8229 mtk_nl80211_deint(&unl_ins);
8230 //wifi_debug(DEBUG_NOTICE, "set Gf cmd success.\n");
8231 printf("set gf=%d cmd success.\n", enable);
8232 return RETURN_OK;
8233err:
8234 mtk_nl80211_deint(&unl_ins);
8235 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08008236 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008237}
8238
developer5cd4c862023-05-26 09:34:42 +08008239int mtk_get_igmp_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08008240{
developer5cd4c862023-05-26 09:34:42 +08008241 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8242 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX + 1];
8243 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8244 unsigned char status = 0, *out_status = data;
8245 int err = 0;
developer72fb0bb2023-01-11 09:46:29 +08008246
developer5cd4c862023-05-26 09:34:42 +08008247 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8248 genlmsg_attrlen(gnlh, 0), NULL);
8249 if (err < 0) {
8250 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8251 return err;
8252 }
developer72fb0bb2023-01-11 09:46:29 +08008253
developer5cd4c862023-05-26 09:34:42 +08008254 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8255 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX,
8256 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8257 if (err < 0){
8258 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX fails\n");
8259 return err;
8260 }
developer72fb0bb2023-01-11 09:46:29 +08008261
developer5cd4c862023-05-26 09:34:42 +08008262 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]) {
8263 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]);
8264 if (status == 0) {
8265 wifi_debug(DEBUG_NOTICE, "disabled\n");
8266 } else {
8267 wifi_debug(DEBUG_NOTICE, "enabled\n");
8268 }
8269 *out_status = status;
8270 wifi_debug(DEBUG_NOTICE, "status: %d\n", *out_status);
8271 }
8272 }
8273
8274 return 0;
8275}
8276
8277INT mtk_wifi_set_igmp_en_status(
8278 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
8279 unsigned char in_en_stat, BOOL *output_bool)
8280{
8281 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008282 unsigned int if_idx = 0;
8283 int ret = -1;
8284 struct unl unl_ins;
8285 struct nl_msg *msg = NULL;
8286 struct nlattr * msg_data = NULL;
8287 struct mtk_nl80211_param param;
8288
8289 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8290 return RETURN_ERR;
8291 if_idx = if_nametoindex(inf_name);
8292 if (!if_idx) {
8293 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8294 return RETURN_ERR;
8295 }
8296 /*init mtk nl80211 vendor cmd*/
8297 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_MULTICAST_SNOOPING;
8298 param.if_type = NL80211_ATTR_IFINDEX;
8299 param.if_idx = if_idx;
8300
8301 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8302 if (ret) {
8303 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8304 return RETURN_ERR;
8305 }
8306 /*add mtk vendor cmd data*/
8307 if (nla_put_u8(msg, vendor_data_attr, in_en_stat)) {
8308 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8309 nlmsg_free(msg);
8310 goto err;
8311 }
8312
8313 /*send mtk nl80211 vendor msg*/
8314 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8315 if (ret) {
8316 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8317 goto err;
8318 }
8319 /*deinit mtk nl80211 vendor msg*/
8320 mtk_nl80211_deint(&unl_ins);
8321 if (output_bool) {
8322 wifi_debug(DEBUG_NOTICE, "send cmd success, get output_bool:%d\n", *output_bool);
8323 } else {
8324 wifi_debug(DEBUG_NOTICE, "send cmd success.\n");
8325 }
8326 return RETURN_OK;
8327err:
8328 mtk_nl80211_deint(&unl_ins);
8329 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8330 return RETURN_ERR;
8331}
8332
8333
8334//Get radio IGMP snooping enable setting
8335INT wifi_getRadioIGMPSnoopingEnable(INT radioIndex, BOOL *output_bool)
8336{
8337 if (output_bool == NULL) {
8338 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8339 return RETURN_ERR;
8340 }
developera3511852023-06-14 14:12:59 +08008341 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008342 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8343 mtk_get_igmp_status_callback, 0xf, output_bool)!= RETURN_OK) {
8344 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8345 return RETURN_ERR;
8346 }
8347 wifi_debug(DEBUG_ERROR, "send cmd success: get igmp status:(%d)\n", *output_bool);
developera3511852023-06-14 14:12:59 +08008348 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008349}
8350
8351//Set radio IGMP snooping enable setting
8352INT wifi_setRadioIGMPSnoopingEnable(INT radioIndex, BOOL enable)
8353{
developera3511852023-06-14 14:12:59 +08008354 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008355 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8356 NULL, enable, NULL) != RETURN_OK) {
8357 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8358 return RETURN_ERR;
8359 }
8360 wifi_debug(DEBUG_ERROR, "send cmd success: set igmp enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008361 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008362}
8363
8364//Get the Reset count of radio
developer69b61b02023-03-07 17:17:44 +08008365INT wifi_getRadioResetCount(INT radioIndex, ULONG *output_int)
developer72fb0bb2023-01-11 09:46:29 +08008366{
developera3511852023-06-14 14:12:59 +08008367 if (NULL == output_int)
8368 return RETURN_ERR;
8369 *output_int = get_radio_reset_cnt(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08008370
developera3511852023-06-14 14:12:59 +08008371 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008372}
8373
8374
8375//---------------------------------------------------------------------------------------------------
8376//
8377// Additional Wifi AP level APIs used for Access Point devices
8378//
8379//---------------------------------------------------------------------------------------------------
8380
8381// creates a new ap and pushes these parameters to the hardware
8382INT wifi_createAp(INT apIndex, INT radioIndex, CHAR *essid, BOOL hideSsid)
8383{
developera3511852023-06-14 14:12:59 +08008384 // Deprecated when use hal version 3, use wifi_createVap() instead.
8385 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008386}
8387
8388// deletes this ap entry on the hardware, clears all internal variables associaated with this ap
8389INT wifi_deleteAp(INT apIndex)
8390{
developer7e4a2a62023-04-06 19:56:03 +08008391 char interface_name[16] = {0};
8392 char buf[MAX_BUF_SIZE];
developere40952c2023-06-15 18:46:43 +08008393 int res;
developer72fb0bb2023-01-11 09:46:29 +08008394
developer7e4a2a62023-04-06 19:56:03 +08008395 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
8396 return RETURN_ERR;
developer8a3bbbf2023-03-15 17:47:23 +08008397
developer8078acf2023-08-04 18:52:48 +08008398 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw REMOVE %s", interface_name);
8399 if (res) {
8400 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08008401 }
developer72fb0bb2023-01-11 09:46:29 +08008402
developer7e4a2a62023-04-06 19:56:03 +08008403 wifi_removeApSecVaribles(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008404
developer7e4a2a62023-04-06 19:56:03 +08008405 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008406}
8407
8408// Outputs a 16 byte or less name assocated with the AP. String buffer must be pre-allocated by the caller
8409INT wifi_getApName(INT apIndex, CHAR *output_string)
8410{
developer7e4a2a62023-04-06 19:56:03 +08008411 char interface_name[IF_NAME_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08008412 int radio_idx = 0;
8413 int bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008414 int res;
developer72fb0bb2023-01-11 09:46:29 +08008415
developer7e4a2a62023-04-06 19:56:03 +08008416 if(!output_string)
8417 return RETURN_ERR;
8418
8419 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
8420 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
8421
developere40952c2023-06-15 18:46:43 +08008422 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 +08008423 } else
developere40952c2023-06-15 18:46:43 +08008424 res = snprintf(output_string, IF_NAME_SIZE, "%s", interface_name);
8425
8426 if (os_snprintf_error(IF_NAME_SIZE, res)) {
8427 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8428 return RETURN_ERR;
8429 }
developer7e4a2a62023-04-06 19:56:03 +08008430
8431 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008432}
8433
8434// Outputs the index number in that corresponds to the SSID string
8435INT wifi_getIndexFromName(CHAR *inputSsidString, INT *output_int)
8436{
developer7e4a2a62023-04-06 19:56:03 +08008437 char buf[32] = {0};
8438 char ap_idx = 0;
8439 char *apIndex_str = NULL;
8440 char radio_idx = 0;
8441 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008442 int res;
developerc14d83a2023-06-29 20:09:42 +08008443 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08008444
developer8078acf2023-08-04 18:52:48 +08008445 res = _syscmd_secure(buf, sizeof(buf), "grep -rn ^interface=%s$ /nvram/hostapd*.conf | cut -d '.' -f1 | cut -d 'd' -f2 | tr -d '\\n'", inputSsidString);
8446 if (res) {
8447 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08008448 }
8449
developer7e4a2a62023-04-06 19:56:03 +08008450 if (strlen(buf)) {
8451 apIndex_str = strtok(buf, "\n");
developerd14dff12023-06-28 22:47:44 +08008452 if (apIndex_str == NULL) {
8453 wifi_debug(DEBUG_ERROR, "strtok fail\n");
8454 return RETURN_ERR;
8455 }
developerc14d83a2023-06-29 20:09:42 +08008456 if (hal_strtol(apIndex_str, 10, &tmp) < 0) {
8457 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08008458 }
developerc14d83a2023-06-29 20:09:42 +08008459 *output_int = tmp;
developer7e4a2a62023-04-06 19:56:03 +08008460 return RETURN_OK;
8461 }
developer72fb0bb2023-01-11 09:46:29 +08008462
developer7e4a2a62023-04-06 19:56:03 +08008463 /* If interface name is not in hostapd config, the caller maybe wifi agent to generate data model.*/
8464 if (strstr(inputSsidString, PREFIX_WIFI6G)) {
8465 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI6G));
8466 radio_idx = 2;
8467 } else if (strstr(inputSsidString, PREFIX_WIFI5G)) {
8468 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI5G));
8469 radio_idx = 1;
8470 } else if (strstr(inputSsidString, PREFIX_WIFI2G)) {
8471 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI2G));
8472 radio_idx = 0;
8473 } else {
8474 printf("%s: hostapd conf not find, unknow inf(%s), return ERROR!!!(%d).\n",
8475 __func__, inputSsidString, ap_idx);
developera3511852023-06-14 14:12:59 +08008476 *output_int = -1;
8477 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08008478 }
8479
8480 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
8481
8482 if (ap_idx >= 0 && ap_idx < MAX_VAP) {
8483 printf("%s: hostapd conf not find, inf(%s), use inf idx(%d).\n",
8484 __func__, inputSsidString, ap_idx);
8485 *output_int = ap_idx;
8486 return RETURN_OK;
8487 }
8488
8489 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008490}
8491
8492INT wifi_getApIndexFromName(CHAR *inputSsidString, INT *output_int)
8493{
developera3511852023-06-14 14:12:59 +08008494 return wifi_getIndexFromName(inputSsidString, output_int);
developer72fb0bb2023-01-11 09:46:29 +08008495}
8496
8497// Outputs a 32 byte or less string indicating the beacon type as "None", "Basic", "WPA", "11i", "WPAand11i"
8498INT wifi_getApBeaconType(INT apIndex, CHAR *output_string)
8499{
developera3511852023-06-14 14:12:59 +08008500 char buf[MAX_BUF_SIZE] = {0};
8501 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008502 int res;
developer72fb0bb2023-01-11 09:46:29 +08008503
developera3511852023-06-14 14:12:59 +08008504 if(NULL == output_string)
8505 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008506
developer32f2a182023-06-27 19:50:41 +08008507 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
8508 if (os_snprintf_error(sizeof(config_file), res)) {
8509 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8510 return RETURN_ERR;
8511 }
developera3511852023-06-14 14:12:59 +08008512 wifi_hostapdRead(config_file, "wpa", buf, sizeof(buf));
8513 if((strcmp(buf,"3")==0))
developere40952c2023-06-15 18:46:43 +08008514 res = snprintf(output_string, 32, "WPAand11i");
developera3511852023-06-14 14:12:59 +08008515 else if((strcmp(buf,"2")==0))
developere40952c2023-06-15 18:46:43 +08008516 res = snprintf(output_string, 32, "11i");
developera3511852023-06-14 14:12:59 +08008517 else if((strcmp(buf,"1")==0))
developere40952c2023-06-15 18:46:43 +08008518 res = snprintf(output_string, 32, "WPA");
developera3511852023-06-14 14:12:59 +08008519 else
developere40952c2023-06-15 18:46:43 +08008520 res = snprintf(output_string, 32, "None");
8521 if (os_snprintf_error(32, res)) {
8522 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8523 return RETURN_ERR;
8524 }
developer72fb0bb2023-01-11 09:46:29 +08008525
developera3511852023-06-14 14:12:59 +08008526 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008527}
8528
8529// Sets the beacon type enviornment variable. Allowed input strings are "None", "Basic", "WPA, "11i", "WPAand11i"
8530INT wifi_setApBeaconType(INT apIndex, CHAR *beaconTypeString)
8531{
developera3511852023-06-14 14:12:59 +08008532 char config_file[MAX_BUF_SIZE] = {0};
8533 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008534 int res;
developer72fb0bb2023-01-11 09:46:29 +08008535
developera3511852023-06-14 14:12:59 +08008536 if (NULL == beaconTypeString)
8537 return RETURN_ERR;
8538 list.name = "wpa";
8539 list.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008540
developera3511852023-06-14 14:12:59 +08008541 if((strcmp(beaconTypeString,"WPAand11i")==0))
8542 list.value="3";
8543 else if((strcmp(beaconTypeString,"11i")==0))
8544 list.value="2";
8545 else if((strcmp(beaconTypeString,"WPA")==0))
8546 list.value="1";
developer72fb0bb2023-01-11 09:46:29 +08008547
developer75bd10c2023-06-27 11:34:08 +08008548 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8549 if (os_snprintf_error(sizeof(config_file), res)) {
8550 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8551 return RETURN_ERR;
8552 }
developera3511852023-06-14 14:12:59 +08008553 wifi_hostapdWrite(config_file, &list, 1);
8554 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8555 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
8556 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008557}
8558
8559// sets the beacon interval on the hardware for this AP
8560INT wifi_setApBeaconInterval(INT apIndex, INT beaconInterval)
8561{
developera3511852023-06-14 14:12:59 +08008562 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8563 struct params params={'\0'};
8564 char buf[MAX_BUF_SIZE] = {'\0'};
8565 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08008566 int res;
developer72fb0bb2023-01-11 09:46:29 +08008567
developera3511852023-06-14 14:12:59 +08008568 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08008569 res = snprintf(buf, sizeof(buf), "%u", beaconInterval);
8570 if (os_snprintf_error(sizeof(buf), res)) {
8571 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8572 return RETURN_ERR;
8573 }
developera3511852023-06-14 14:12:59 +08008574 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08008575
developer75bd10c2023-06-27 11:34:08 +08008576 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8577 if (os_snprintf_error(sizeof(config_file), res)) {
8578 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8579 return RETURN_ERR;
8580 }
developera3511852023-06-14 14:12:59 +08008581 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08008582
developera3511852023-06-14 14:12:59 +08008583 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8584 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8585 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008586}
8587
8588INT wifi_setDTIMInterval(INT apIndex, INT dtimInterval)
8589{
developera3511852023-06-14 14:12:59 +08008590 if (wifi_setApDTIMInterval(apIndex, dtimInterval) != RETURN_OK)
8591 return RETURN_ERR;
8592 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008593}
8594
8595// Get the packet size threshold supported.
8596INT wifi_getApRtsThresholdSupported(INT apIndex, BOOL *output_bool)
8597{
developera3511852023-06-14 14:12:59 +08008598 //save config and apply instantly
8599 if (NULL == output_bool)
8600 return RETURN_ERR;
8601 *output_bool = TRUE;
8602 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008603}
8604
8605// sets the packet size threshold in bytes to apply RTS/CTS backoff rules.
8606INT wifi_setApRtsThreshold(INT apIndex, UINT threshold)
8607{
developera3511852023-06-14 14:12:59 +08008608 char buf[16] = {0};
8609 char config_file[128] = {0};
8610 struct params param = {0};
developere40952c2023-06-15 18:46:43 +08008611 int res;
developer72fb0bb2023-01-11 09:46:29 +08008612
developera3511852023-06-14 14:12:59 +08008613 if (threshold > 65535) {
developer75bd10c2023-06-27 11:34:08 +08008614 wifi_debug(DEBUG_ERROR, "rts threshold %u is too big.\n", threshold);
developera3511852023-06-14 14:12:59 +08008615 return RETURN_ERR;
8616 }
developer72fb0bb2023-01-11 09:46:29 +08008617
developere40952c2023-06-15 18:46:43 +08008618 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8619 if (os_snprintf_error(sizeof(config_file), res)) {
8620 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8621 return RETURN_ERR;
8622 }
8623
8624 res = snprintf(buf, sizeof(buf), "%u", threshold);
8625 if (os_snprintf_error(sizeof(buf), res)) {
8626 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8627 return RETURN_ERR;
8628 }
developera3511852023-06-14 14:12:59 +08008629 param.name = "rts_threshold";
8630 param.value = buf;
8631 wifi_hostapdWrite(config_file, &param, 1);
8632 wifi_hostapdProcessUpdate(apIndex, &param, 1);
8633 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008634
developera3511852023-06-14 14:12:59 +08008635 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008636}
8637
8638// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8639INT wifi_getApWpaEncryptoinMode(INT apIndex, CHAR *output_string)
8640{
developere40952c2023-06-15 18:46:43 +08008641 int res;
8642
developera3511852023-06-14 14:12:59 +08008643 if (NULL == output_string)
8644 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08008645 res = snprintf(output_string, 32, "TKIPandAESEncryption");
8646 if (os_snprintf_error(32, res)) {
8647 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8648 return RETURN_ERR;
8649 }
developera3511852023-06-14 14:12:59 +08008650 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008651
8652}
8653
8654// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8655INT wifi_getApWpaEncryptionMode(INT apIndex, CHAR *output_string)
8656{
developera3511852023-06-14 14:12:59 +08008657 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8658 char *param_name = NULL;
8659 char buf[32] = {0}, config_file[MAX_BUF_SIZE] = {0};
developerc79e9172023-06-06 19:48:03 +08008660 unsigned int len;
developere40952c2023-06-15 18:46:43 +08008661 int res;
developer72fb0bb2023-01-11 09:46:29 +08008662
developera3511852023-06-14 14:12:59 +08008663 if(NULL == output_string)
8664 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008665
developer75bd10c2023-06-27 11:34:08 +08008666 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8667 if (os_snprintf_error(sizeof(config_file), res)) {
8668 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8669 return RETURN_ERR;
8670 }
8671
developera3511852023-06-14 14:12:59 +08008672 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008673
developera3511852023-06-14 14:12:59 +08008674 if(strcmp(buf,"0")==0)
8675 {
8676 printf("%s: wpa_mode is %s ......... \n", __func__, buf);
developere40952c2023-06-15 18:46:43 +08008677 res = snprintf(output_string, 32, "None");
8678 if (os_snprintf_error(32, res)) {
8679 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8680 return RETURN_ERR;
8681 }
developera3511852023-06-14 14:12:59 +08008682 return RETURN_OK;
8683 }
8684 else if((strcmp(buf,"3")==0) || (strcmp(buf,"2")==0))
8685 param_name = "rsn_pairwise";
8686 else if((strcmp(buf,"1")==0))
8687 param_name = "wpa_pairwise";
8688 else
8689 return RETURN_ERR;
8690 memset(output_string,'\0',32);
8691 wifi_hostapdRead(config_file,param_name,output_string,32);
8692 if (strlen(output_string) == 0) { // rsn_pairwise is optional. When it is empty use wpa_pairwise instead.
8693 param_name = "wpa_pairwise";
8694 memset(output_string, '\0', 32);
8695 wifi_hostapdRead(config_file, param_name, output_string, 32);
8696 }
8697 wifi_dbg_printf("\n%s output_string=%s",__func__,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008698
developera3511852023-06-14 14:12:59 +08008699 if(strcmp(output_string,"TKIP CCMP") == 0) {
developerc79e9172023-06-06 19:48:03 +08008700 len = strlen("TKIPandAESEncryption");
8701 memcpy(output_string,"TKIPandAESEncryption", len);
8702 output_string[len] = '\0';
8703 } else if(strcmp(output_string,"TKIP") == 0) {
8704 len = strlen("TKIPEncryption");
8705 memcpy(output_string,"TKIPEncryption", len);
8706 output_string[len] = '\0';
8707 } else if(strcmp(output_string,"CCMP") == 0) {
8708 len = strlen("AESEncryption");
8709 memcpy(output_string,"AESEncryption", len);
8710 output_string[len] = '\0';
8711 }
developer72fb0bb2023-01-11 09:46:29 +08008712
developera3511852023-06-14 14:12:59 +08008713 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8714 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008715}
8716
8717// sets the encyption mode enviornment variable. Valid string format is "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8718INT wifi_setApWpaEncryptionMode(INT apIndex, CHAR *encMode)
8719{
developera3511852023-06-14 14:12:59 +08008720 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8721 struct params params={'\0'};
8722 char output_string[32];
8723 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08008724 int res;
developer72fb0bb2023-01-11 09:46:29 +08008725
developera3511852023-06-14 14:12:59 +08008726 memset(output_string,'\0',32);
8727 wifi_getApBeaconType(apIndex,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008728
developera3511852023-06-14 14:12:59 +08008729 if(strcmp(encMode, "TKIPEncryption") == 0)
8730 params.value = "TKIP";
8731 else if(strcmp(encMode,"AESEncryption") == 0)
8732 params.value = "CCMP";
8733 else if(strcmp(encMode,"TKIPandAESEncryption") == 0)
8734 params.value = "TKIP CCMP";
developer72fb0bb2023-01-11 09:46:29 +08008735
developera3511852023-06-14 14:12:59 +08008736 if((strcmp(output_string,"WPAand11i")==0))
8737 {
8738 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008739 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8740 if (os_snprintf_error(sizeof(config_file), res)) {
8741 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8742 return RETURN_ERR;
8743 }
developera3511852023-06-14 14:12:59 +08008744 wifi_hostapdWrite(config_file, &params, 1);
8745 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008746
developera3511852023-06-14 14:12:59 +08008747 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008748 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8749 if (os_snprintf_error(sizeof(config_file), res)) {
8750 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8751 return RETURN_ERR;
8752 }
developera3511852023-06-14 14:12:59 +08008753 wifi_hostapdWrite(config_file, &params, 1);
8754 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008755
developera3511852023-06-14 14:12:59 +08008756 return RETURN_OK;
8757 }
8758 else if((strcmp(output_string,"11i")==0))
8759 {
8760 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008761 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8762 if (os_snprintf_error(sizeof(config_file), res)) {
8763 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8764 return RETURN_ERR;
8765 }
developera3511852023-06-14 14:12:59 +08008766 wifi_hostapdWrite(config_file, &params, 1);
8767 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8768 return RETURN_OK;
8769 }
8770 else if((strcmp(output_string,"WPA")==0))
8771 {
8772 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008773 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8774 if (os_snprintf_error(sizeof(config_file), res)) {
8775 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8776 return RETURN_ERR;
8777 }
developera3511852023-06-14 14:12:59 +08008778 wifi_hostapdWrite(config_file, &params, 1);
8779 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8780 return RETURN_OK;
8781 }
developer72fb0bb2023-01-11 09:46:29 +08008782
developera3511852023-06-14 14:12:59 +08008783 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8784 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008785}
8786
8787// deletes internal security varable settings for this ap
8788INT wifi_removeApSecVaribles(INT apIndex)
8789{
developer0155a502023-06-19 20:33:57 +08008790 char config_file[MAX_BUF_SIZE] = {0};
8791 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008792 int res;
developer0155a502023-06-19 20:33:57 +08008793
8794 list.name = "wpa";
8795 list.value = "0";
8796
developer75bd10c2023-06-27 11:34:08 +08008797 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8798 if (os_snprintf_error(sizeof(config_file), res)) {
8799 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8800 return RETURN_ERR;
8801 }
developer0155a502023-06-19 20:33:57 +08008802 wifi_hostapdWrite(config_file, &list, 1);
8803
8804 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008805}
8806
8807// changes the hardware settings to disable encryption on this ap
8808INT wifi_disableApEncryption(INT apIndex)
8809{
developer0155a502023-06-19 20:33:57 +08008810 char config_file[MAX_BUF_SIZE] = {0};
8811 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008812 int res;
developer0155a502023-06-19 20:33:57 +08008813
8814 list.name = "wpa";
8815 list.value = "0";
8816
developer75bd10c2023-06-27 11:34:08 +08008817 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8818 if (os_snprintf_error(sizeof(config_file), res)) {
8819 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8820 return RETURN_ERR;
8821 }
developer0155a502023-06-19 20:33:57 +08008822 wifi_hostapdWrite(config_file, &list, 1);
8823 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8824 wifi_reloadAp(apIndex);
8825
8826 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008827}
8828
8829// set the authorization mode on this ap
8830// mode mapping as: 1: open, 2: shared, 4:auto
8831INT wifi_setApAuthMode(INT apIndex, INT mode)
8832{
developera3511852023-06-14 14:12:59 +08008833 struct params params={0};
8834 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08008835 int res;
developer72fb0bb2023-01-11 09:46:29 +08008836
developera3511852023-06-14 14:12:59 +08008837 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008838
developera3511852023-06-14 14:12:59 +08008839 wifi_dbg_printf("\n%s algo_mode=%d", __func__, mode);
8840 params.name = "auth_algs";
developer72fb0bb2023-01-11 09:46:29 +08008841
developere5750452023-05-15 16:46:42 +08008842 if ((mode & 1 && mode & 2) || mode & 4)
developera3511852023-06-14 14:12:59 +08008843 params.value = "3";
8844 else if (mode & 2)
8845 params.value = "2";
8846 else if (mode & 1)
8847 params.value = "1";
8848 else
8849 params.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008850
developer75bd10c2023-06-27 11:34:08 +08008851 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8852 if (os_snprintf_error(sizeof(config_file), res)) {
8853 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8854 return RETURN_ERR;
8855 }
developera3511852023-06-14 14:12:59 +08008856 wifi_hostapdWrite(config_file, &params, 1);
8857 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +08008858 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +08008859 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008860
developera3511852023-06-14 14:12:59 +08008861 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008862}
8863
8864// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
8865INT wifi_setApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
8866{
developera3511852023-06-14 14:12:59 +08008867 //save to wifi config, and wait for wifi restart to apply
8868 struct params params={'\0'};
8869 char config_file[MAX_BUF_SIZE] = {0};
8870 int ret;
developer72fb0bb2023-01-11 09:46:29 +08008871
developera3511852023-06-14 14:12:59 +08008872 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8873 if(authMode == NULL)
8874 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008875
developera3511852023-06-14 14:12:59 +08008876 wifi_dbg_printf("\n%s AuthMode=%s",__func__,authMode);
8877 params.name = "wpa_key_mgmt";
developer72fb0bb2023-01-11 09:46:29 +08008878
developera3511852023-06-14 14:12:59 +08008879 if((strcmp(authMode,"PSKAuthentication") == 0) || (strcmp(authMode,"SharedAuthentication") == 0))
8880 params.value = "WPA-PSK";
8881 else if(strcmp(authMode,"EAPAuthentication") == 0)
8882 params.value = "WPA-EAP";
8883 else if (strcmp(authMode, "SAEAuthentication") == 0)
8884 params.value = "SAE";
8885 else if (strcmp(authMode, "EAP_192-bit_Authentication") == 0)
8886 params.value = "WPA-EAP-SUITE-B-192";
8887 else if (strcmp(authMode, "PSK-SAEAuthentication") == 0)
8888 params.value = "WPA-PSK WPA-PSK-SHA256 SAE";
8889 else if (strcmp(authMode, "Enhanced_Open") == 0)
8890 params.value = "OWE";
8891 else if(strcmp(authMode,"None") == 0) //Donot change in case the authMode is None
8892 return RETURN_OK; //This is taken careof in beaconType
developer72fb0bb2023-01-11 09:46:29 +08008893
developer32f2a182023-06-27 19:50:41 +08008894 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
8895 if (os_snprintf_error(sizeof(config_file), ret)) {
8896 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8897 return RETURN_ERR;
8898 }
developera3511852023-06-14 14:12:59 +08008899 ret=wifi_hostapdWrite(config_file,&params,1);
8900 if(!ret)
8901 ret=wifi_hostapdProcessUpdate(apIndex, &params, 1);
8902 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008903
developera3511852023-06-14 14:12:59 +08008904 return ret;
developer72fb0bb2023-01-11 09:46:29 +08008905}
8906
8907// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
8908INT wifi_getApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
8909{
developera3511852023-06-14 14:12:59 +08008910 //save to wifi config, and wait for wifi restart to apply
8911 char BeaconType[50] = {0};
8912 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08008913 int res;
developer32f2a182023-06-27 19:50:41 +08008914 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08008915
developera3511852023-06-14 14:12:59 +08008916 *authMode = 0;
8917 wifi_getApBeaconType(apIndex,BeaconType);
8918 printf("%s____%s \n",__FUNCTION__,BeaconType);
developer72fb0bb2023-01-11 09:46:29 +08008919
developer32f2a182023-06-27 19:50:41 +08008920 if(strcmp(BeaconType,"None") == 0) {
8921 memcpy(authMode, "None", 4);
8922 authMode[4] = '\0';
8923 } else {
8924 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
developer75bd10c2023-06-27 11:34:08 +08008925 if (os_snprintf_error(sizeof(config_file), res)) {
8926 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8927 return RETURN_ERR;
8928 }
developera3511852023-06-14 14:12:59 +08008929 wifi_hostapdRead(config_file, "wpa_key_mgmt", authMode, 32);
8930 wifi_dbg_printf("\n[%s]: AuthMode Name is : %s",__func__,authMode);
developer32f2a182023-06-27 19:50:41 +08008931 if(strcmp(authMode,"WPA-PSK") == 0) {
8932 len = strlen("SharedAuthentication");
8933 memcpy(authMode, "SharedAuthentication", len);
8934 authMode[len] = '\0';
8935 } else if(strcmp(authMode,"WPA-EAP") == 0) {
8936 len = strlen("EAPAuthentication");
8937 memcpy(authMode, "EAPAuthentication", len);
8938 authMode[len] = '\0';
8939 }
developera3511852023-06-14 14:12:59 +08008940 }
developer72fb0bb2023-01-11 09:46:29 +08008941
developera3511852023-06-14 14:12:59 +08008942 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008943}
8944
8945// Outputs the number of stations associated per AP
8946INT wifi_getApNumDevicesAssociated(INT apIndex, ULONG *output_ulong)
8947{
developera3511852023-06-14 14:12:59 +08008948 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +08008949 char buf[128]={0};
8950 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +08008951 int res;
developer72fb0bb2023-01-11 09:46:29 +08008952
developera3511852023-06-14 14:12:59 +08008953 if(apIndex > MAX_APS)
8954 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008955
developera3511852023-06-14 14:12:59 +08008956 wifi_getApEnable(apIndex,&status);
8957 if (!status)
8958 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008959
developera3511852023-06-14 14:12:59 +08008960 //sprintf(cmd, "iw dev %s station dump | grep Station | wc -l", interface_name);//alternate method
8961 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
8962 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +08008963
8964 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s list_sta | wc -l", interface_name);
8965 if (res) {
8966 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08008967 }
developer8078acf2023-08-04 18:52:48 +08008968
8969
developerd14dff12023-06-28 22:47:44 +08008970 if (sscanf(buf,"%lu", output_ulong) != 1) {
8971 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
8972 return RETURN_ERR;
8973 }
developer72fb0bb2023-01-11 09:46:29 +08008974
developera3511852023-06-14 14:12:59 +08008975 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008976}
8977
8978// manually removes any active wi-fi association with the device specified on this ap
8979INT wifi_kickApAssociatedDevice(INT apIndex, CHAR *client_mac)
8980{
developera3511852023-06-14 14:12:59 +08008981 char inf_name[16] = {0};
developera3511852023-06-14 14:12:59 +08008982 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008983 int res;
developer72fb0bb2023-01-11 09:46:29 +08008984
developera3511852023-06-14 14:12:59 +08008985 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8986 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08008987
developer8078acf2023-08-04 18:52:48 +08008988 res = _syscmd_secure(buf, sizeof(buf),"hostapd_cli -i %s disassociate %s", inf_name, client_mac);
8989 if (res) {
8990 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +08008991 }
developer7e4a2a62023-04-06 19:56:03 +08008992
developera3511852023-06-14 14:12:59 +08008993 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008994}
8995
8996// outputs the radio index for the specified ap. similar as wifi_getSsidRadioIndex
8997INT wifi_getApRadioIndex(INT apIndex, INT *output_int)
8998{
developer7e4a2a62023-04-06 19:56:03 +08008999 int max_radio_num = 0;
9000
9001 if(NULL == output_int)
9002 return RETURN_ERR;
9003
9004 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08009005 if(max_radio_num == 0){
9006 return RETURN_ERR;
9007 }
developer7e4a2a62023-04-06 19:56:03 +08009008 *output_int = apIndex % max_radio_num;
9009
9010 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009011}
9012
9013// sets the radio index for the specific ap
9014INT wifi_setApRadioIndex(INT apIndex, INT radioIndex)
9015{
developera3511852023-06-14 14:12:59 +08009016 //set to config only and wait for wifi reset to apply settings
9017 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009018}
9019
developer0155a502023-06-19 20:33:57 +08009020int mtk_get_ap_metrics(struct nl_msg *msg, void *cb)
9021{
9022 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9023 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009024 struct genlmsghdr *gnlh;
developer0155a502023-06-19 20:33:57 +08009025 wdev_ap_metric ap_metric;
9026 wdev_ap_metric *p_ap_metric = &ap_metric;
9027 int err = 0;
9028 struct mtk_nl80211_cb_data *cb_data = cb;
9029
9030 if (!msg || !cb_data) {
developerc14d83a2023-06-29 20:09:42 +08009031 wifi_debug(DEBUG_ERROR, "msgor cb_data is null,error.\n");
developer0155a502023-06-19 20:33:57 +08009032 return NL_SKIP;
9033 }
developerc14d83a2023-06-29 20:09:42 +08009034 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer0155a502023-06-19 20:33:57 +08009035
9036 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9037 genlmsg_attrlen(gnlh, 0), NULL);
9038 if (err < 0) {
9039 wifi_debug(DEBUG_ERROR, "nla_parse ap_metrics nl80211 msg fails,error.\n");
9040 return err;
9041 }
9042
9043 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9044 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX,
9045 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9046 if (err < 0) {
9047 wifi_debug(DEBUG_ERROR, "GET_STATISTIC_MAX fails,error.\n");
9048 return err;
9049 }
9050
9051 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]) {
9052 p_ap_metric = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]);
9053 if (p_ap_metric) {
9054 memcpy(cb_data->out_buf , &p_ap_metric->cu, sizeof(unsigned char));
9055 }
9056 }
9057 }
9058
9059 return NL_OK;
9060}
9061
developer121a8e72023-05-22 09:19:39 +08009062
9063#define MAX_ACL_DUMP_LEN 4096
9064int mtk_acl_list_dump_callback(struct nl_msg *msg, void *cb)
9065{
9066 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9067 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009068 struct genlmsghdr *gnlh;
developer121a8e72023-05-22 09:19:39 +08009069 char *show_str = NULL;
developer2edaf012023-05-24 14:24:53 +08009070 int err = 0;
developer121a8e72023-05-22 09:19:39 +08009071 unsigned short acl_result_len = 0;
9072 struct mtk_nl80211_cb_data *cb_data = cb;
developer121a8e72023-05-22 09:19:39 +08009073 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08009074 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developer121a8e72023-05-22 09:19:39 +08009075 return NL_SKIP;
9076 }
developerc14d83a2023-06-29 20:09:42 +08009077
9078 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer121a8e72023-05-22 09:19:39 +08009079 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9080 genlmsg_attrlen(gnlh, 0), NULL);
9081 if (err < 0) {
developer2edaf012023-05-24 14:24:53 +08009082 wifi_debug(DEBUG_ERROR, "nla_parse acl list nl80211 msg fails,error.\n");
developer121a8e72023-05-22 09:19:39 +08009083 return NL_SKIP;
9084 }
developer121a8e72023-05-22 09:19:39 +08009085 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9086 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX,
9087 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9088 if (err < 0)
9089 return NL_SKIP;
9090 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]) {
9091 acl_result_len = nla_len(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9092 show_str = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9093 if (acl_result_len > MAX_ACL_DUMP_LEN) {
9094 wifi_debug(DEBUG_ERROR,"the scan result len is invalid !!!\n");
9095 return NL_SKIP;
9096 } else if (*(show_str + acl_result_len - 1) != '\0') {
9097 wifi_debug(DEBUG_INFO, "the result string is not ended with right terminator, handle it!!!\n");
9098 *(show_str + acl_result_len - 1) = '\0';
9099 }
9100 wifi_debug(DEBUG_INFO, "driver msg:%s\n", show_str);
developer2edaf012023-05-24 14:24:53 +08009101
9102 if (cb_data->out_len >= acl_result_len) {
9103 memset(cb_data->out_buf, 0, cb_data->out_len);
9104 /*skip the first line: 'policy=1\n' to find the acl mac addrs*/
9105 memmove(cb_data->out_buf, show_str, acl_result_len);
9106 wifi_debug(DEBUG_INFO, "out buff:%s\n", cb_data->out_buf);
9107 } else {
9108 memset(cb_data->out_buf, 0, cb_data->out_len);
developer121a8e72023-05-22 09:19:39 +08009109 }
developer121a8e72023-05-22 09:19:39 +08009110 } else
9111 wifi_debug(DEBUG_ERROR, "no acl result attr\n");
9112 } else
9113 wifi_debug(DEBUG_ERROR, "no any acl result from driver\n");
9114 return NL_OK;
9115}
developer72fb0bb2023-01-11 09:46:29 +08009116// Get the ACL MAC list per AP
developer2edaf012023-05-24 14:24:53 +08009117INT mtk_wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
developer72fb0bb2023-01-11 09:46:29 +08009118{
developer7e4a2a62023-04-06 19:56:03 +08009119 char inf_name[IF_NAME_SIZE] = {0};
developer121a8e72023-05-22 09:19:39 +08009120 unsigned int if_idx = 0;
9121 int ret = -1;
9122 struct unl unl_ins;
9123 struct nl_msg *msg = NULL;
9124 struct nlattr * msg_data = NULL;
9125 struct mtk_nl80211_param param;
9126 struct mtk_nl80211_cb_data cb_data;
developer7e4a2a62023-04-06 19:56:03 +08009127 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9128 return RETURN_ERR;
developer121a8e72023-05-22 09:19:39 +08009129 if_idx = if_nametoindex(inf_name);
9130 if (!if_idx) {
developer2edaf012023-05-24 14:24:53 +08009131 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
developer121a8e72023-05-22 09:19:39 +08009132 return RETURN_ERR;
9133 }
9134 /*init mtk nl80211 vendor cmd*/
9135 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9136 param.if_type = NL80211_ATTR_IFINDEX;
9137 param.if_idx = if_idx;
9138
9139 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9140 if (ret) {
9141 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9142 return RETURN_ERR;
9143 }
developer121a8e72023-05-22 09:19:39 +08009144 /*add mtk vendor cmd data*/
9145 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_SHOW_ALL)) {
developer2edaf012023-05-24 14:24:53 +08009146 wifi_debug(DEBUG_ERROR, "Nla put ACL_SHOW_ALL attribute error\n");
developer121a8e72023-05-22 09:19:39 +08009147 nlmsg_free(msg);
9148 goto err;
9149 }
developer72fb0bb2023-01-11 09:46:29 +08009150
developer121a8e72023-05-22 09:19:39 +08009151 /*send mtk nl80211 vendor msg*/
9152 cb_data.out_buf = macArray;
9153 cb_data.out_len = buf_size;
9154
9155 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_acl_list_dump_callback, &cb_data);
9156 if (ret) {
9157 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9158 goto err;
9159 }
9160 /*deinit mtk nl80211 vendor msg*/
9161 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009162 wifi_debug(DEBUG_NOTICE,"send cmd success, get out_buf:%s\n", macArray);
developera3511852023-06-14 14:12:59 +08009163 return RETURN_OK;
developer121a8e72023-05-22 09:19:39 +08009164err:
9165 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009166 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developer121a8e72023-05-22 09:19:39 +08009167 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009168}
9169
developer2edaf012023-05-24 14:24:53 +08009170INT wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9171{
9172 char *mac_arry_buf = NULL;
9173
9174 mac_arry_buf = malloc(buf_size);
9175 if (!mac_arry_buf) {
9176 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
9177 return RETURN_ERR;
9178 }
9179 memset(mac_arry_buf, 0, buf_size);
9180 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
9181 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
9182 free(mac_arry_buf);
9183 mac_arry_buf = NULL;
9184 return RETURN_ERR;
9185 }
9186 /*
9187 mtk format to wifi hal format:
9188 "policy=1
9189 00:11:22:33:44:55
9190 00:11:22:33:44:66
9191 "
9192 -->
9193 "00:11:22:33:44:55
9194 00:11:22:33:44:66
9195 "
9196 */
9197 memset(macArray, 0, buf_size);
9198 if (*mac_arry_buf != '\0' && strchr(mac_arry_buf,'\n')) {
9199 memmove(macArray, strchr(mac_arry_buf,'\n')+1, strlen(strchr(mac_arry_buf,'\n')+1)+1);
9200 wifi_debug(DEBUG_NOTICE,"macArray:\n%s\n", macArray);
9201 }
9202 free(mac_arry_buf);
9203 mac_arry_buf = NULL;
9204 return RETURN_OK;
9205}
9206
developer72fb0bb2023-01-11 09:46:29 +08009207INT wifi_getApDenyAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9208{
developer72fb0bb2023-01-11 09:46:29 +08009209
developer7e4a2a62023-04-06 19:56:03 +08009210 wifi_getApAclDevices(apIndex, macArray, buf_size);
developer72fb0bb2023-01-11 09:46:29 +08009211
developera3511852023-06-14 14:12:59 +08009212 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009213}
9214
9215
9216// Get the list of stations associated per AP
9217INT wifi_getApDevicesAssociated(INT apIndex, CHAR *macArray, UINT buf_size)
9218{
developer7e4a2a62023-04-06 19:56:03 +08009219 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08009220 int res;
developer72fb0bb2023-01-11 09:46:29 +08009221
developer7e4a2a62023-04-06 19:56:03 +08009222 if(apIndex > 3) //Currently supporting apIndex upto 3
developera3511852023-06-14 14:12:59 +08009223 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009224
developer7e4a2a62023-04-06 19:56:03 +08009225 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08009226 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009227
developer8078acf2023-08-04 18:52:48 +08009228 res = _syscmd_secure(macArray, buf_size, "hostapd_cli -i %s list_sta", interface_name);
9229 if (res) {
9230 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
9231 }
9232
developer7e4a2a62023-04-06 19:56:03 +08009233 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009234}
9235
developer8dd72532023-05-17 19:58:35 +08009236int hex2num(char c)
9237{
9238 if (c >= '0' && c <= '9')
9239 return c - '0';
9240 if (c >= 'a' && c <= 'f')
9241 return c - 'a' + 10;
9242 if (c >= 'A' && c <= 'F')
9243 return c - 'A' + 10;
9244 return -1;
9245}
9246
9247/**
9248 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
9249 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
9250 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
9251 * Returns: Characters used (> 0) on success, -1 on failure
9252 */
9253int hwaddr_aton2(const char *txt, unsigned char *addr)
9254{
9255 int i;
9256 const char *pos = txt;
9257
9258 for (i = 0; i < 6; i++) {
9259 int a, b;
9260
9261 while (*pos == ':' || *pos == '.' || *pos == '-')
9262 pos++;
9263
9264 a = hex2num(*pos++);
9265 if (a < 0)
9266 return -1;
9267 b = hex2num(*pos++);
9268 if (b < 0)
9269 return -1;
9270 *addr++ = (a << 4) | b;
9271 }
9272
9273 return pos - txt;
9274}
9275
developer72fb0bb2023-01-11 09:46:29 +08009276// adds the mac address to the filter list
9277//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9278INT wifi_addApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9279{
developer7e4a2a62023-04-06 19:56:03 +08009280 char inf_name[IF_NAME_SIZE] = {0};
developer8dd72532023-05-17 19:58:35 +08009281 int if_idx, ret = 0;
developer49b17232023-05-19 16:35:19 +08009282 struct nl_msg *msg = NULL;
9283 struct nlattr * msg_data = NULL;
9284 struct mtk_nl80211_param param;
developer8dd72532023-05-17 19:58:35 +08009285 unsigned char mac[ETH_ALEN] = {0x00, 0x0c, 0x43, 0x11, 0x22, 0x33};
9286 struct unl unl_ins;
developer7e4a2a62023-04-06 19:56:03 +08009287 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9288 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009289 if (!DeviceMacAddress)
9290 return RETURN_ERR;
developer8dd72532023-05-17 19:58:35 +08009291 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
developer2edaf012023-05-24 14:24:53 +08009292 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
developer8dd72532023-05-17 19:58:35 +08009293 return RETURN_ERR;
9294 }
developer8dd72532023-05-17 19:58:35 +08009295 if_idx = if_nametoindex(inf_name);
developer2edaf012023-05-24 14:24:53 +08009296 if (!if_idx) {
9297 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9298 return RETURN_ERR;
9299 }
developer49b17232023-05-19 16:35:19 +08009300 /*init mtk nl80211 vendor cmd*/
9301 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9302 param.if_type = NL80211_ATTR_IFINDEX;
9303 param.if_idx = if_idx;
9304 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9305 if (ret) {
9306 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009307 return RETURN_ERR;
9308 }
developer49b17232023-05-19 16:35:19 +08009309 /*add mtk vendor cmd data*/
9310 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_ADD_MAC, ETH_ALEN, mac)) {
developer2edaf012023-05-24 14:24:53 +08009311 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
developer8dd72532023-05-17 19:58:35 +08009312 nlmsg_free(msg);
9313 goto err;
9314 }
developer49b17232023-05-19 16:35:19 +08009315 /*send mtk nl80211 vendor msg*/
9316 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9317 if (ret) {
9318 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009319 goto err;
9320 }
developer49b17232023-05-19 16:35:19 +08009321 /*deinit mtk nl80211 vendor msg*/
9322 mtk_nl80211_deint(&unl_ins);
9323 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer8dd72532023-05-17 19:58:35 +08009324 return RETURN_OK;
9325err:
developer49b17232023-05-19 16:35:19 +08009326 mtk_nl80211_deint(&unl_ins);
9327 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developer8dd72532023-05-17 19:58:35 +08009328 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009329}
9330
9331// deletes the mac address from the filter list
9332//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9333INT wifi_delApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9334{
developer2edaf012023-05-24 14:24:53 +08009335 struct unl unl_ins;
9336 int if_idx = 0, ret = 0;
9337 struct nl_msg *msg = NULL;
9338 struct nlattr * msg_data = NULL;
9339 struct mtk_nl80211_param param;
developer7e4a2a62023-04-06 19:56:03 +08009340 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +08009341 unsigned char mac[ETH_ALEN] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009342
developer7e4a2a62023-04-06 19:56:03 +08009343 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9344 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009345
developer7e4a2a62023-04-06 19:56:03 +08009346 if (!DeviceMacAddress)
9347 return RETURN_ERR;
9348
developer2edaf012023-05-24 14:24:53 +08009349 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
9350 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
9351 return RETURN_ERR;
9352 }
developer72fb0bb2023-01-11 09:46:29 +08009353
developer2edaf012023-05-24 14:24:53 +08009354 if_idx = if_nametoindex(inf_name);
9355 if (!if_idx) {
9356 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9357 return RETURN_ERR;
9358 }
9359 /*init mtk nl80211 vendor cmd*/
9360 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9361 param.if_type = NL80211_ATTR_IFINDEX;
9362 param.if_idx = if_idx;
9363 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9364 if (ret) {
9365 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9366 return RETURN_ERR;
9367 }
9368 /*add mtk vendor cmd data*/
9369 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_DEL_MAC, ETH_ALEN, mac)) {
9370 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9371 nlmsg_free(msg);
9372 goto err;
9373 }
9374 /*send mtk nl80211 vendor msg*/
9375 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9376 if (ret) {
9377 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9378 goto err;
9379 }
9380 /*deinit mtk nl80211 vendor msg*/
9381 mtk_nl80211_deint(&unl_ins);
9382 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
9383 return RETURN_OK;
9384err:
9385 mtk_nl80211_deint(&unl_ins);
9386 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9387 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009388}
9389
9390// outputs the number of devices in the filter list
9391INT wifi_getApAclDeviceNum(INT apIndex, UINT *output_uint)
9392{
developer2edaf012023-05-24 14:24:53 +08009393 char *mac_arry = NULL, *ptr = NULL, mac_str[18] = {0};
9394 UINT buf_size = 1024;
9395 UINT sta_num = 0;
9396 unsigned char mac[ETH_ALEN] = {0};
developera3511852023-06-14 14:12:59 +08009397 if(output_uint == NULL)
developerdaf24792023-06-06 11:40:04 +08009398 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009399
developer2edaf012023-05-24 14:24:53 +08009400 mac_arry = (char *)malloc(buf_size);
9401 if (mac_arry == NULL) {
9402 wifi_debug(DEBUG_ERROR, "malloc mac_arry fails\n");
developer7e4a2a62023-04-06 19:56:03 +08009403 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009404 }
developerdaf24792023-06-06 11:40:04 +08009405 memset(mac_arry, 0, buf_size);
developer2edaf012023-05-24 14:24:53 +08009406 /*mac_arry str format: 00:11:22:33:44:55\n00:11:22:33:44:66\0*/
9407 if (wifi_getApAclDevices(apIndex, mac_arry, buf_size)!= RETURN_OK) {
9408 wifi_debug(DEBUG_ERROR, "get acl list entries fails\n");
developer9ce44382023-06-28 11:09:37 +08009409 free(mac_arry);
developer2edaf012023-05-24 14:24:53 +08009410 return RETURN_ERR;
9411 }
9412 /*count the acl str nums:*/
9413 wifi_debug(DEBUG_NOTICE, "mac_arry: %s\n", mac_arry);
developer7e4a2a62023-04-06 19:56:03 +08009414
developer2edaf012023-05-24 14:24:53 +08009415 /*mac addr string format:
9416 exp1: 00:11:22:33:44:55\0
9417 exp2: 00:11:22:33:44:55\n00:11:22:33:44:66\0
9418 */
9419 ptr = mac_arry;
9420 while (sscanf(ptr, "%17s", mac_str) == 1) {
9421 if (hwaddr_aton2(mac_str, mac) >= 0)
9422 sta_num++;
9423 ptr = strstr(ptr, mac_str) + strlen(mac_str);
9424 }
9425 *output_uint = sta_num;
9426 wifi_debug(DEBUG_NOTICE, "output_uint: %d\n", *output_uint);
9427 free(mac_arry);
9428 mac_arry = NULL;
developer7e4a2a62023-04-06 19:56:03 +08009429 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009430}
9431
9432INT apply_rules(INT apIndex, CHAR *client_mac,CHAR *action,CHAR *interface)
9433{
developer75bd10c2023-06-27 11:34:08 +08009434 int res;
developer72fb0bb2023-01-11 09:46:29 +08009435
developera3511852023-06-14 14:12:59 +08009436 if(strcmp(action,"DENY")==0)
9437 {
developer33f13ba2023-07-12 16:19:06 +08009438 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 +08009439 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009440 if (res) {
9441 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009442 return RETURN_ERR;
9443 }
developera3511852023-06-14 14:12:59 +08009444 return RETURN_OK;
9445 }
developer72fb0bb2023-01-11 09:46:29 +08009446
developera3511852023-06-14 14:12:59 +08009447 if(strcmp(action,"ALLOW")==0)
9448 {
developer33f13ba2023-07-12 16:19:06 +08009449 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 +08009450 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009451 if (res) {
9452 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009453 return RETURN_ERR;
9454 }
developera3511852023-06-14 14:12:59 +08009455 return RETURN_OK;
9456 }
developer72fb0bb2023-01-11 09:46:29 +08009457
developera3511852023-06-14 14:12:59 +08009458 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009459
9460}
9461
9462// enable kick for devices on acl black list
9463INT wifi_kickApAclAssociatedDevices(INT apIndex, BOOL enable)
9464{
developera3511852023-06-14 14:12:59 +08009465 char aclArray[MAX_BUF_SIZE] = {0}, *acl = NULL;
9466 char assocArray[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009467
developera3511852023-06-14 14:12:59 +08009468 wifi_getApDenyAclDevices(apIndex, aclArray, sizeof(aclArray));
9469 wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray));
developer72fb0bb2023-01-11 09:46:29 +08009470
developera3511852023-06-14 14:12:59 +08009471 /* if there are no devices connected there is nothing to do */
9472 if (strlen(assocArray) < 17)
9473 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009474
developera3511852023-06-14 14:12:59 +08009475 if (enable == TRUE) {
9476 /* kick off the MAC which is in ACL array (deny list) */
9477 acl = strtok(aclArray, "\n");
9478 while (acl != NULL) {
9479 if (strlen(acl) >= 17 && strcasestr(assocArray, acl))
9480 wifi_kickApAssociatedDevice(apIndex, acl);
developer72fb0bb2023-01-11 09:46:29 +08009481
developera3511852023-06-14 14:12:59 +08009482 acl = strtok(NULL, "\n");
9483 }
developer72fb0bb2023-01-11 09:46:29 +08009484 wifi_setApMacAddressControlMode(apIndex, 2);
developera3511852023-06-14 14:12:59 +08009485 } else
developer72fb0bb2023-01-11 09:46:29 +08009486 wifi_setApMacAddressControlMode(apIndex, 0);
developer72fb0bb2023-01-11 09:46:29 +08009487
developera3511852023-06-14 14:12:59 +08009488 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009489}
9490
9491INT wifi_setPreferPrivateConnection(BOOL enable)
9492{
developera3511852023-06-14 14:12:59 +08009493 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009494}
9495
9496// sets the mac address filter control mode. 0 == filter disabled, 1 == filter as whitelist, 2 == filter as blacklist
9497INT wifi_setApMacAddressControlMode(INT apIndex, INT filterMode)
9498{
developer2edaf012023-05-24 14:24:53 +08009499 int if_idx = 0, ret = 0;
9500 struct unl unl_ins;
9501 struct nl_msg *msg = NULL;
9502 struct nlattr * msg_data = NULL;
9503 struct mtk_nl80211_param param;
9504 int acl_policy = -1;
developer7e4a2a62023-04-06 19:56:03 +08009505 char inf_name[IF_NAME_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009506
developer7e4a2a62023-04-06 19:56:03 +08009507 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9508 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009509 if_idx = if_nametoindex(inf_name);
9510 if (!if_idx) {
9511 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9512 return RETURN_ERR;
9513 }
9514 /*init mtk nl80211 vendor cmd*/
9515 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9516 param.if_type = NL80211_ATTR_IFINDEX;
9517 param.if_idx = if_idx;
9518 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9519 if (ret) {
9520 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9521 return RETURN_ERR;
9522 }
9523 /*add mtk vendor cmd data*/
9524 if (filterMode == 0) {
9525 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_DISABLE;
9526 } else if (filterMode == 1) {
9527 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_WHITE_LIST;
9528 } else if (filterMode == 2) {
9529 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_BLACK_LIST;
9530 } else {
9531 wifi_debug(DEBUG_ERROR, "filtermode(%d) not support error\n", filterMode);
9532 nlmsg_free(msg);
9533 goto err;
9534 }
9535 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, acl_policy)) {
9536 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9537 nlmsg_free(msg);
9538 goto err;
9539 }
9540 /*send mtk nl80211 vendor msg*/
9541 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9542 if (ret) {
9543 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9544 goto err;
9545 }
9546 /*deinit mtk nl80211 vendor msg*/
9547 mtk_nl80211_deint(&unl_ins);
9548 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer7e4a2a62023-04-06 19:56:03 +08009549 return RETURN_OK;
developer2edaf012023-05-24 14:24:53 +08009550err:
9551 mtk_nl80211_deint(&unl_ins);
9552 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9553 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009554}
9555
9556// 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.
9557INT wifi_setApVlanEnable(INT apIndex, BOOL VlanEnabled)
9558{
developera3511852023-06-14 14:12:59 +08009559 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009560}
9561
9562// gets the vlan ID for this ap from an internal enviornment variable
9563INT wifi_getApVlanID(INT apIndex, INT *output_int)
9564{
developera3511852023-06-14 14:12:59 +08009565 if(apIndex==0)
9566 {
9567 *output_int=100;
9568 return RETURN_OK;
9569 }
developer72fb0bb2023-01-11 09:46:29 +08009570
developera3511852023-06-14 14:12:59 +08009571 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009572}
9573
9574// sets the vlan ID for this ap to an internal enviornment variable
9575INT wifi_setApVlanID(INT apIndex, INT vlanId)
9576{
developera3511852023-06-14 14:12:59 +08009577 //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 +08009578 char interface_name[16] = {0};
9579 int if_idx, ret = 0;
9580 struct nl_msg *msg = NULL;
9581 struct nlattr * msg_data = NULL;
9582 struct mtk_nl80211_param param;
9583 struct unl unl_ins;
9584
9585 if (apIndex > MAX_APS) {
9586 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9587 return RETURN_ERR;
9588 }
9589 if (vlanId > 4095 || vlanId < 1) {
9590 wifi_debug(DEBUG_ERROR, "Invalid vlanId %d\n", vlanId);
9591 return RETURN_ERR;
9592 }
9593 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9594 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9595 return RETURN_ERR;
9596 /*step 1. mwctl dev %s set vlan_tag 0*/
9597 if_idx = if_nametoindex(interface_name);
9598 /*init mtk nl80211 vendor cmd*/
9599 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9600 param.if_type = NL80211_ATTR_IFINDEX;
9601 param.if_idx = if_idx;
9602 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9603 if (ret) {
9604 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9605 return RETURN_ERR;
9606 }
9607 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, vlanId)) {
9608 printf("Nla put attribute error\n");
9609 nlmsg_free(msg);
9610 goto err;
9611 }
9612 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9613 if (ret) {
9614 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9615 goto err;
9616 }
9617 mtk_nl80211_deint(&unl_ins);
9618 //wifi_debug(DEBUG_NOTICE, "set vlanId cmd success.\n", vlanId);
9619 printf("set vlanId=%d cmd success.\n", vlanId);
9620 return RETURN_OK;
9621err:
9622 mtk_nl80211_deint(&unl_ins);
9623 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08009624 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009625}
9626
developercc5cbfb2023-06-13 18:29:52 +08009627char br_name[IFNAMSIZ] = "brlan0";
9628
developer72fb0bb2023-01-11 09:46:29 +08009629// gets bridgeName, IP address and Subnet. bridgeName is a maximum of 32 characters,
9630INT wifi_getApBridgeInfo(INT index, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9631{
developercc5cbfb2023-06-13 18:29:52 +08009632 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9633 struct ifreq ifr;
9634 struct sockaddr_in *sin;
9635
9636 memcpy(bridgeName, br_name, strlen(br_name));
9637
9638 if (sock == -1) {
9639 wifi_debug(DEBUG_ERROR, "socket failed");
9640 return RETURN_ERR;
9641 }
9642
developerd14dff12023-06-28 22:47:44 +08009643 strncpy(ifr.ifr_name, br_name, strlen(br_name));
developercc5cbfb2023-06-13 18:29:52 +08009644 ifr.ifr_addr.sa_family = AF_INET;
9645 if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
9646 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFADDR) failed, %s, bridge_name=%s\n",
9647 strerror(errno), br_name);
developer9ce44382023-06-28 11:09:37 +08009648 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009649 return RETURN_ERR;
9650 }
9651
9652 sin = (struct sockaddr_in *)&ifr.ifr_addr;
9653 wifi_debug(DEBUG_ERROR, "Bridge device %s has IP address: %s\n", br_name, inet_ntoa(sin->sin_addr));
9654 memcpy(IP, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9655
9656 if (ioctl(sock, SIOCGIFNETMASK, &ifr) < 0) {
9657 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFNETMASK) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009658 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009659 return RETURN_ERR;
9660 }
9661
9662 wifi_debug(DEBUG_ERROR, "Bridge device %s has subnet mask: %s\n", br_name, inet_ntoa(sin->sin_addr));
9663 memcpy(subnet, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9664 close(sock);
developer72fb0bb2023-01-11 09:46:29 +08009665
developera3511852023-06-14 14:12:59 +08009666 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009667}
9668
9669//sets bridgeName, IP address and Subnet to internal enviornment variables. bridgeName is a maximum of 32 characters
9670INT wifi_setApBridgeInfo(INT apIndex, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9671{
developera3511852023-06-14 14:12:59 +08009672 //save settings, wait for wifi reset or wifi_pushBridgeInfo to apply.
developercc5cbfb2023-06-13 18:29:52 +08009673 struct ifreq ifr;
9674 struct sockaddr_in sin;
9675 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9676
developerc14d83a2023-06-29 20:09:42 +08009677 if(sock < 0) {
9678 wifi_debug(DEBUG_ERROR, "sock init fail\n");
9679 return RETURN_ERR;
9680 }
developer5b23cd02023-07-19 20:26:03 +08009681
developercc5cbfb2023-06-13 18:29:52 +08009682 if (strlen(bridgeName) >= IFNAMSIZ) {
9683 wifi_debug(DEBUG_ERROR, "invalide bridgeName length=%ld\n", strlen(bridgeName));
developer9ce44382023-06-28 11:09:37 +08009684 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009685 return RETURN_ERR;
9686 }
9687
9688 if (strlen(br_name) >= IFNAMSIZ) {
9689 wifi_debug(DEBUG_ERROR, "invalide br_name length=%ld in strorage\n", strlen(br_name));
developer9ce44382023-06-28 11:09:37 +08009690 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009691 return RETURN_ERR;
9692 }
9693
9694 if (sock == -1) {
developera3511852023-06-14 14:12:59 +08009695 wifi_debug(DEBUG_ERROR, "socket failed");
developercc5cbfb2023-06-13 18:29:52 +08009696 return RETURN_ERR;
9697 }
9698
9699 memset(&ifr, 0, sizeof(ifr));
9700 strncpy(ifr.ifr_name, br_name, strlen(br_name));
9701 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9702 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009703 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009704 return RETURN_ERR;
9705 }
9706
9707 ifr.ifr_flags = (short)(ifr.ifr_flags & ~IFF_UP);
9708 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9709 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009710 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009711 return RETURN_ERR;
9712 }
9713
9714 memset(&ifr, 0, sizeof(ifr));
9715 strncpy(ifr.ifr_name, br_name, IFNAMSIZ);
developerd14dff12023-06-28 22:47:44 +08009716 strncpy(ifr.ifr_newname, bridgeName, strlen(bridgeName));
developercc5cbfb2023-06-13 18:29:52 +08009717 if (ioctl(sock, SIOCSIFNAME, &ifr) < 0) {
9718 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNAME) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009719 close(sock);
developera3511852023-06-14 14:12:59 +08009720 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009721 }
9722
9723 memset(br_name, 0, sizeof(br_name));
9724 memcpy(br_name, bridgeName, strlen(bridgeName));
9725
9726 memset(&ifr, 0, sizeof(ifr));
9727 strncpy(ifr.ifr_name, bridgeName, IFNAMSIZ);
9728 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9729 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009730 close(sock);
developera3511852023-06-14 14:12:59 +08009731 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009732 }
9733 ifr.ifr_flags = (short)(ifr.ifr_flags | IFF_UP);
9734 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9735 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009736 close(sock);
developera3511852023-06-14 14:12:59 +08009737 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009738 }
9739
9740 memset(&ifr, 0, sizeof(ifr));
9741 memcpy(ifr.ifr_name, bridgeName, strlen(bridgeName));
9742
9743 memset(&sin, 0, sizeof(struct sockaddr_in));
9744 sin.sin_family = AF_INET;
9745 if (inet_aton(IP, &(sin.sin_addr)) == 0) {
9746 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developer9ce44382023-06-28 11:09:37 +08009747 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009748 return RETURN_ERR;
9749 }
9750 memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr_in));
9751 if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
9752 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFADDR) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009753 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009754 return RETURN_ERR;
9755 }
9756
9757 if (inet_aton(subnet, &((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr) == 0) {
9758 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developerc14d83a2023-06-29 20:09:42 +08009759 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009760 return RETURN_ERR;
9761 }
9762 if (ioctl(sock, SIOCSIFNETMASK, &ifr) < -1) {
9763 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNETMASK) failed, %s", strerror(errno));
developerc14d83a2023-06-29 20:09:42 +08009764 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009765 return RETURN_ERR;
9766 }
9767
9768 close(sock);
developera3511852023-06-14 14:12:59 +08009769 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009770}
9771
9772// reset the vlan configuration for this ap
9773INT wifi_resetApVlanCfg(INT apIndex)
9774{
developera1255e42023-05-13 17:45:02 +08009775 char interface_name[16] = {0};
developer2202b332023-05-24 16:23:22 +08009776 int if_idx, ret = 0;
9777 struct nl_msg *msg = NULL;
9778 struct nlattr * msg_data = NULL;
9779 struct mtk_nl80211_param param;
9780 struct unl unl_ins;
9781 struct vlan_policy_param vlan_param;
developer72fb0bb2023-01-11 09:46:29 +08009782
developer2202b332023-05-24 16:23:22 +08009783 if (apIndex > MAX_APS) {
9784 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9785 return RETURN_ERR;
9786 }
developer72fb0bb2023-01-11 09:46:29 +08009787
developer2202b332023-05-24 16:23:22 +08009788 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9789 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9790 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009791
developer2202b332023-05-24 16:23:22 +08009792 /*step 1. mwctl dev %s set vlan_tag 0*/
9793 if_idx = if_nametoindex(interface_name);
9794 /*init mtk nl80211 vendor cmd*/
9795 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9796 param.if_type = NL80211_ATTR_IFINDEX;
9797 param.if_idx = if_idx;
9798 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
developer72fb0bb2023-01-11 09:46:29 +08009799
developer2202b332023-05-24 16:23:22 +08009800 if (ret) {
9801 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9802 return RETURN_ERR;
9803 }
developer72fb0bb2023-01-11 09:46:29 +08009804
developer2202b332023-05-24 16:23:22 +08009805 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_TAG_INFO, 0)) {
9806 printf("Nla put attribute error\n");
9807 nlmsg_free(msg);
9808 goto err;
9809 }
developer72fb0bb2023-01-11 09:46:29 +08009810
developer2202b332023-05-24 16:23:22 +08009811 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9812 if (ret) {
9813 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9814 goto err;
9815 }
9816 mtk_nl80211_deint(&unl_ins);
9817 wifi_debug(DEBUG_NOTICE, "set vlan_tag 0 cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08009818
developer2202b332023-05-24 16:23:22 +08009819 /*step 2. mwctl dev %s set vlan_priority 0*/
9820 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9821 if (ret) {
9822 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9823 return RETURN_ERR;
9824 }
developer72fb0bb2023-01-11 09:46:29 +08009825
developer2202b332023-05-24 16:23:22 +08009826 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_PRIORITY_INFO, 0)) {
9827 printf("Nla put attribute error\n");
9828 nlmsg_free(msg);
9829 goto err;
9830 }
developer72fb0bb2023-01-11 09:46:29 +08009831
developer2202b332023-05-24 16:23:22 +08009832 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9833 if (ret) {
9834 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9835 goto err;
9836 }
9837 mtk_nl80211_deint(&unl_ins);
9838 wifi_debug(DEBUG_NOTICE, "set vlan_priority 0 cmd success.\n");
9839
9840 /*step 3. mwctl dev %s set vlan_id 0*/
9841 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9842 if (ret) {
9843 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developera1255e42023-05-13 17:45:02 +08009844 return RETURN_ERR;
developer2202b332023-05-24 16:23:22 +08009845 }
developer72fb0bb2023-01-11 09:46:29 +08009846
developer2202b332023-05-24 16:23:22 +08009847 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, 0)) {
9848 printf("Nla put attribute error\n");
9849 nlmsg_free(msg);
9850 goto err;
9851 }
9852
9853 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9854 if (ret) {
9855 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9856 goto err;
9857 }
9858 mtk_nl80211_deint(&unl_ins);
9859 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
9860
9861 /*step 4. mwctl dev %s set vlan_en 0*/
9862 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9863 if (ret) {
9864 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9865 return RETURN_ERR;
9866 }
9867
9868 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_EN_INFO, 0)) {
9869 printf("Nla put attribute error\n");
9870 nlmsg_free(msg);
9871 goto err;
9872 }
9873
9874 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9875 if (ret) {
9876 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9877 goto err;
9878 }
9879 mtk_nl80211_deint(&unl_ins);
9880 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
9881
9882 /*step 5. mwctl dev %s set vlan_policy 0:4*/
9883 vlan_param.direction = 0;
9884 vlan_param.policy = 4;
9885 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9886 if (ret) {
9887 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9888 return RETURN_ERR;
9889 }
9890 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
9891 printf("Nla put attribute error\n");
9892 nlmsg_free(msg);
9893 goto err;
9894 }
9895
9896 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9897 if (ret) {
9898 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9899 goto err;
9900 }
9901 mtk_nl80211_deint(&unl_ins);
9902 wifi_debug(DEBUG_NOTICE, "set vlan_policy 0:4 cmd success.\n");
9903
9904 /*step 6. mwctl dev %s set vlan_policy 1:0*/
9905 vlan_param.direction = 1;
9906 vlan_param.policy = 0;
9907 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9908 if (ret) {
9909 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9910 return RETURN_ERR;
9911 }
9912
9913 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
9914 printf("Nla put attribute error\n");
9915 nlmsg_free(msg);
9916 goto err;
9917 }
9918
9919 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9920 if (ret) {
9921 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9922 goto err;
9923 }
9924 /*deinit mtk nl80211 vendor msg*/
9925 mtk_nl80211_deint(&unl_ins);
9926 wifi_debug(DEBUG_NOTICE, "set vlan_policy 1:0 cmd success.\n");
9927
9928 /*TODO need to modify VLAN config in dat file*/
9929 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
9930
9931 return RETURN_OK;
9932err:
9933 mtk_nl80211_deint(&unl_ins);
9934 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9935 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009936}
9937
9938// 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.
9939INT wifi_createHostApdConfig(INT apIndex, BOOL createWpsCfg)
9940{
developera3511852023-06-14 14:12:59 +08009941 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009942}
9943
9944// starts hostapd, uses the variables in the hostapd config with format compatible with the specific hostapd implementation
9945INT wifi_startHostApd()
9946{
developera3511852023-06-14 14:12:59 +08009947 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +08009948 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +08009949 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
9950 return RETURN_OK;
9951 //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 +08009952}
9953
9954// stops hostapd
developer69b61b02023-03-07 17:17:44 +08009955INT wifi_stopHostApd()
developer72fb0bb2023-01-11 09:46:29 +08009956{
developera3511852023-06-14 14:12:59 +08009957 char buf[128] = {0};
developer75bd10c2023-06-27 11:34:08 +08009958 int res;
developer72fb0bb2023-01-11 09:46:29 +08009959
developer8078acf2023-08-04 18:52:48 +08009960 res = _syscmd_secure(buf, sizeof(buf), "systemctl stop hostapd");
9961 if (res) {
9962 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +08009963 }
developer72fb0bb2023-01-11 09:46:29 +08009964
developera3511852023-06-14 14:12:59 +08009965 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009966}
9967
9968// restart hostapd dummy function
9969INT wifi_restartHostApd()
9970{
developera3511852023-06-14 14:12:59 +08009971 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +08009972 v_secure_system("systemctl restart hostapd-global");
developera3511852023-06-14 14:12:59 +08009973 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08009974
developera3511852023-06-14 14:12:59 +08009975 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009976}
9977
9978// sets the AP enable status variable for the specified ap.
9979INT wifi_setApEnable(INT apIndex, BOOL enable)
9980{
developer7e4a2a62023-04-06 19:56:03 +08009981 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +08009982 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer8078acf2023-08-04 18:52:48 +08009983
developer7e4a2a62023-04-06 19:56:03 +08009984 char buf[MAX_BUF_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08009985 BOOL status = FALSE;
developer7e4a2a62023-04-06 19:56:03 +08009986 int max_radio_num = 0;
9987 int phyId = 0;
developere40952c2023-06-15 18:46:43 +08009988 int res;
developer72fb0bb2023-01-11 09:46:29 +08009989
developer7e4a2a62023-04-06 19:56:03 +08009990 wifi_getApEnable(apIndex, &status);
developer72fb0bb2023-01-11 09:46:29 +08009991
developer7e4a2a62023-04-06 19:56:03 +08009992 wifi_getMaxRadioNumber(&max_radio_num);
9993 if (enable == status)
9994 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009995
developer7e4a2a62023-04-06 19:56:03 +08009996 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9997 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009998
developer7e4a2a62023-04-06 19:56:03 +08009999 if (enable == TRUE) {
10000 int radioIndex = apIndex % max_radio_num;
10001 phyId = radio_index_to_phy(radioIndex);
developer8078acf2023-08-04 18:52:48 +080010002
10003 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s up", interface_name);
10004 if (res) {
10005 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010006 }
developer8a3bbbf2023-03-15 17:47:23 +080010007
developere40952c2023-06-15 18:46:43 +080010008 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
10009 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
10010 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10011 return RETURN_ERR;
10012 }
developer8078acf2023-08-04 18:52:48 +080010013 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
10014 if (res) {
10015 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010016 }
developer7e4a2a62023-04-06 19:56:03 +080010017 } else {
developer8078acf2023-08-04 18:52:48 +080010018 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw REMOVE %s", interface_name);
10019 if (res) {
10020 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010021 }
developer8078acf2023-08-04 18:52:48 +080010022 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s down", interface_name);
10023 if (res) {
10024 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010025 }
developer7e4a2a62023-04-06 19:56:03 +080010026 }
developer8078acf2023-08-04 18:52:48 +080010027 res = _syscmd_secure(buf, sizeof(buf), "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer7e4a2a62023-04-06 19:56:03 +080010028 interface_name, interface_name, enable, VAP_STATUS_FILE);
developer8078acf2023-08-04 18:52:48 +080010029 if (res) {
10030 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010031 }
developer7e4a2a62023-04-06 19:56:03 +080010032 //Wait for wifi up/down to apply
10033 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010034}
10035
10036// Outputs the setting of the internal variable that is set by wifi_setApEnable().
10037INT wifi_getApEnable(INT apIndex, BOOL *output_bool)
10038{
developer7e4a2a62023-04-06 19:56:03 +080010039 char interface_name[IF_NAME_SIZE] = {0};
developerc1aa6532023-06-09 09:37:01 +080010040 char buf[MAX_BUF_SIZE] = {0};
developerc338aba2023-08-09 13:56:42 +080010041 int res, len;
10042 char ctrl_interface[64] = {0};
10043 char config_file[128] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010044
developer7e4a2a62023-04-06 19:56:03 +080010045 if ((!output_bool) || (apIndex < 0) || (apIndex >= MAX_APS))
10046 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010047
developer7e4a2a62023-04-06 19:56:03 +080010048 *output_bool = 0;
developer72fb0bb2023-01-11 09:46:29 +080010049
developer7e4a2a62023-04-06 19:56:03 +080010050 if ((apIndex >= 0) && (apIndex < MAX_APS)) {
10051 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
10052 *output_bool = FALSE;
10053 return RETURN_OK;
10054 }
developerc338aba2023-08-09 13:56:42 +080010055 if (strlen(interface_name) == 0)
10056 return RETURN_ERR;
10057
10058 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10059 if (os_snprintf_error(sizeof(config_file), res)) {
10060 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10061 return RETURN_ERR;
10062 }
10063 if (wifi_hostapdRead(config_file, "ctrl_interface", ctrl_interface, sizeof(ctrl_interface))) {
10064 wifi_debug(DEBUG_ERROR, "ctrl_interface for %s not exist\n", interface_name);
10065 }
10066
10067 _syscmd_secure(buf, sizeof(buf), "ls %s | grep %s",
10068 strlen(ctrl_interface) == 0 ? "/var/run/hostapd/" : ctrl_interface, interface_name);
10069 len = strlen(buf) >= strlen(interface_name) ? strlen(interface_name) : strlen(buf);
10070
10071 if (len == 0 || strncmp(buf, interface_name, len)) {
10072 return RETURN_OK;
10073 }
10074 memset(buf, 0, sizeof(buf));
developerc1aa6532023-06-09 09:37:01 +080010075
developer8078acf2023-08-04 18:52:48 +080010076 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s status | grep state | cut -d '=' -f2", interface_name);
10077 if (res) {
10078 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080010079 }
developerc1aa6532023-06-09 09:37:01 +080010080
10081 if(strncmp(buf, "ENABLED", 7) == 0 || strncmp(buf, "ACS", 3) == 0 ||
10082 strncmp(buf, "HT_SCAN", 7) == 0 || strncmp(buf, "DFS", 3) == 0) {
10083 *output_bool = TRUE;
10084 }
developer7e4a2a62023-04-06 19:56:03 +080010085 }
developer72fb0bb2023-01-11 09:46:29 +080010086
developer7e4a2a62023-04-06 19:56:03 +080010087 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010088}
10089
developer69b61b02023-03-07 17:17:44 +080010090// Outputs the AP "Enabled" "Disabled" status from driver
10091INT wifi_getApStatus(INT apIndex, CHAR *output_string)
developer72fb0bb2023-01-11 09:46:29 +080010092{
developer9ce44382023-06-28 11:09:37 +080010093 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +080010094 int res;
developer72fb0bb2023-01-11 09:46:29 +080010095
developer7e4a2a62023-04-06 19:56:03 +080010096 if (!output_string) {
10097 printf("%s: null pointer!", __func__);
10098 return RETURN_ERR;
10099 }
developer72fb0bb2023-01-11 09:46:29 +080010100
developer7e4a2a62023-04-06 19:56:03 +080010101 wifi_getApEnable(apIndex, &output_bool);
developer72fb0bb2023-01-11 09:46:29 +080010102
developer7e4a2a62023-04-06 19:56:03 +080010103 if(output_bool == 1)
developere40952c2023-06-15 18:46:43 +080010104 res = snprintf(output_string, 32, "Up");
developer7e4a2a62023-04-06 19:56:03 +080010105 else
developere40952c2023-06-15 18:46:43 +080010106 res = snprintf(output_string, 32, "Disable");
10107 if (os_snprintf_error(32, res)) {
10108 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10109 return RETURN_ERR;
10110 }
developer7e4a2a62023-04-06 19:56:03 +080010111
10112 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010113}
10114
10115//Indicates whether or not beacons include the SSID name.
10116// outputs a 1 if SSID on the AP is enabled, else outputs 0
10117INT wifi_getApSsidAdvertisementEnable(INT apIndex, BOOL *output)
10118{
developera3511852023-06-14 14:12:59 +080010119 //get the running status
10120 char config_file[MAX_BUF_SIZE] = {0};
10121 char buf[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080010122 int res;
developer72fb0bb2023-01-11 09:46:29 +080010123
developera3511852023-06-14 14:12:59 +080010124 if (!output)
10125 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010126
developer32f2a182023-06-27 19:50:41 +080010127 res = snprintf(config_file, sizeof(config_file),
10128 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010129 if (os_snprintf_error(sizeof(config_file), res)) {
10130 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10131 return RETURN_ERR;
10132 }
developera3511852023-06-14 14:12:59 +080010133 wifi_hostapdRead(config_file, "ignore_broadcast_ssid", buf, sizeof(buf));
10134 // default is enable
10135 if (strlen(buf) == 0 || strncmp("0", buf, 1) == 0)
10136 *output = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080010137
developera3511852023-06-14 14:12:59 +080010138 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010139}
10140
10141// sets an internal variable for ssid advertisement. Set to 1 to enable, set to 0 to disable
10142INT wifi_setApSsidAdvertisementEnable(INT apIndex, BOOL enable)
10143{
developera3511852023-06-14 14:12:59 +080010144 //store the config, apply instantly
10145 char config_file[MAX_BUF_SIZE] = {0};
10146 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010147 int res;
developer72fb0bb2023-01-11 09:46:29 +080010148
developera3511852023-06-14 14:12:59 +080010149 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10150 list.name = "ignore_broadcast_ssid";
10151 list.value = enable?"0":"1";
developer72fb0bb2023-01-11 09:46:29 +080010152
developer32f2a182023-06-27 19:50:41 +080010153 res = snprintf(config_file, sizeof(config_file),
10154 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010155 if (os_snprintf_error(sizeof(config_file), res)) {
10156 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10157 return RETURN_ERR;
10158 }
developera3511852023-06-14 14:12:59 +080010159 wifi_hostapdWrite(config_file, &list, 1);
10160 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10161 //TODO: call hostapd_cli for dynamic_config_control
10162 wifi_reloadAp(apIndex);
10163 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010164
developera3511852023-06-14 14:12:59 +080010165 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010166}
10167
10168//The maximum number of retransmission for a packet. This corresponds to IEEE 802.11 parameter dot11ShortRetryLimit.
10169INT wifi_getApRetryLimit(INT apIndex, UINT *output_uint)
10170{
developer47cc27a2023-05-17 23:09:58 +080010171 /* get the running status */
10172 if(!output_uint)
developera3511852023-06-14 14:12:59 +080010173 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +080010174
10175 *output_uint = 15;
10176 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010177}
10178
developer47cc27a2023-05-17 23:09:58 +080010179/*Do not support AP retry limit fix*/
developer72fb0bb2023-01-11 09:46:29 +080010180INT wifi_setApRetryLimit(INT apIndex, UINT number)
10181{
developer47cc27a2023-05-17 23:09:58 +080010182 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010183}
10184
developer95c045d2023-05-24 19:26:28 +080010185int get_wmm_cap_status_callback(struct nl_msg *msg, void *data)
10186{
10187 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10188 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_WMM_ATTR_MAX + 1];
10189 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developer2f79c922023-06-02 17:33:42 +080010190 unsigned char *status = (unsigned char *)data;
developer95c045d2023-05-24 19:26:28 +080010191 int err = 0;
developer95c045d2023-05-24 19:26:28 +080010192
10193 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10194 genlmsg_attrlen(gnlh, 0), NULL);
10195 if (err < 0)
10196 return err;
10197
10198 if (tb[NL80211_ATTR_VENDOR_DATA]) {
10199 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_WMM_ATTR_MAX,
10200 tb[NL80211_ATTR_VENDOR_DATA], NULL);
10201 if (err < 0)
10202 return err;
10203
10204 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]) {
developer95c045d2023-05-24 19:26:28 +080010205 *status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]);
10206 }
10207 }
10208
10209 return 0;
10210}
10211
developer72fb0bb2023-01-11 09:46:29 +080010212//Indicates whether this access point supports WiFi Multimedia (WMM) Access Categories (AC).
10213INT wifi_getApWMMCapability(INT apIndex, BOOL *output)
10214{
developer95c045d2023-05-24 19:26:28 +080010215 int if_idx, ret = 0;
developera3511852023-06-14 14:12:59 +080010216 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010217 unsigned char status = 0;
10218 struct nl_msg *msg = NULL;
10219 struct nlattr * msg_data = NULL;
10220 struct mtk_nl80211_param param;
10221 struct unl unl_ins;
developer8e6583c2023-05-23 13:36:06 +080010222
developera3511852023-06-14 14:12:59 +080010223 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10224 if(!output)
developerdaf24792023-06-06 11:40:04 +080010225 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010226
developer95c045d2023-05-24 19:26:28 +080010227 if (apIndex > MAX_APS) {
10228 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10229 return RETURN_ERR;
10230 }
10231
developera3511852023-06-14 14:12:59 +080010232 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developerdaf24792023-06-06 11:40:04 +080010233 return RETURN_ERR;
developer95c045d2023-05-24 19:26:28 +080010234
10235 if_idx = if_nametoindex(interface_name);
10236 /*init mtk nl80211 vendor cmd*/
10237 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10238 param.if_type = NL80211_ATTR_IFINDEX;
10239 param.if_idx = if_idx;
10240 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10241
10242 if (ret) {
10243 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10244 return RETURN_ERR;
10245 }
10246
10247 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, 0xf)) {
10248 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10249 nlmsg_free(msg);
10250 goto err;
10251 }
10252
10253 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_wmm_cap_status_callback,
10254 (void *)&status);
10255 if (ret) {
10256 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10257 goto err;
10258 }
10259 mtk_nl80211_deint(&unl_ins);
10260
10261 *output = status == 0 ? FALSE : TRUE;
10262 wifi_debug(DEBUG_NOTICE, "wmm cap (%u).\n", (unsigned int)(*output));
developer8e6583c2023-05-23 13:36:06 +080010263
developera3511852023-06-14 14:12:59 +080010264 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10265 return RETURN_OK;
10266err:
developer95c045d2023-05-24 19:26:28 +080010267 mtk_nl80211_deint(&unl_ins);
10268 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10269 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010270}
10271
10272//Indicates whether this access point supports WMM Unscheduled Automatic Power Save Delivery (U-APSD). Note: U-APSD support implies WMM support.
10273INT wifi_getApUAPSDCapability(INT apIndex, BOOL *output)
10274{
developera3511852023-06-14 14:12:59 +080010275 //get the running status from driver
developera3511852023-06-14 14:12:59 +080010276 char buf[128] = {0};
10277 int max_radio_num = 0, radioIndex = 0;
10278 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010279 int res;
developer72fb0bb2023-01-11 09:46:29 +080010280
developera3511852023-06-14 14:12:59 +080010281 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010282
developera3511852023-06-14 14:12:59 +080010283 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080010284 if(max_radio_num == 0){
10285 return RETURN_ERR;
10286 }
developera3511852023-06-14 14:12:59 +080010287 radioIndex = apIndex % max_radio_num;
10288 phyId = radio_index_to_phy(radioIndex);
developer8078acf2023-08-04 18:52:48 +080010289
10290 res = _syscmd_secure(buf, sizeof(buf), "iw phy phy%d info | grep u-APSD", phyId);
10291 if (res) {
10292 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010293 }
developer72fb0bb2023-01-11 09:46:29 +080010294
developera3511852023-06-14 14:12:59 +080010295 if (strlen(buf) > 0)
10296 *output = true;
developer72fb0bb2023-01-11 09:46:29 +080010297
developera3511852023-06-14 14:12:59 +080010298 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
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//Whether WMM support is currently enabled. When enabled, this is indicated in beacon frames.
10304INT wifi_getApWmmEnable(INT apIndex, BOOL *output)
10305{
developera3511852023-06-14 14:12:59 +080010306 return wifi_getApWMMCapability(apIndex, output);
developer72fb0bb2023-01-11 09:46:29 +080010307}
10308
10309// enables/disables WMM on the hardwawre for this AP. enable==1, disable == 0
10310INT wifi_setApWmmEnable(INT apIndex, BOOL enable)
10311{
developer95c045d2023-05-24 19:26:28 +080010312 int if_idx, ret = 0;
10313 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010314 struct nl_msg *msg = NULL;
10315 struct nlattr * msg_data = NULL;
10316 struct mtk_nl80211_param param;
10317 struct unl unl_ins;
developer72fb0bb2023-01-11 09:46:29 +080010318
developer95c045d2023-05-24 19:26:28 +080010319 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010320
developer95c045d2023-05-24 19:26:28 +080010321 if (apIndex > MAX_APS) {
10322 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10323 return RETURN_ERR;
10324 }
developer72fb0bb2023-01-11 09:46:29 +080010325
developer95c045d2023-05-24 19:26:28 +080010326 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10327 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010328
developer95c045d2023-05-24 19:26:28 +080010329 if_idx = if_nametoindex(interface_name);
10330 /*init mtk nl80211 vendor cmd*/
10331 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10332 param.if_type = NL80211_ATTR_IFINDEX;
10333 param.if_idx = if_idx;
10334 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10335
10336 if (ret) {
10337 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10338 return RETURN_ERR;
10339 }
10340
10341 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, enable ? 1 : 0)) {
10342 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10343 nlmsg_free(msg);
10344 goto err;
10345 }
10346
10347 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10348 if (ret) {
10349 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10350 goto err;
10351 }
10352 mtk_nl80211_deint(&unl_ins);
10353
10354 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10355 return RETURN_OK;
10356err:
10357 mtk_nl80211_deint(&unl_ins);
10358 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10359 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010360}
10361
developer95c045d2023-05-24 19:26:28 +080010362
developer72fb0bb2023-01-11 09:46:29 +080010363//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.
10364INT wifi_getApWmmUapsdEnable(INT apIndex, BOOL *output)
10365{
developer75bd10c2023-06-27 11:34:08 +080010366 int res;
10367
developera3511852023-06-14 14:12:59 +080010368 //get the running status from driver
10369 if(!output)
10370 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010371
developera3511852023-06-14 14:12:59 +080010372 char config_file[128] = {0};
10373 char buf[16] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010374
developer75bd10c2023-06-27 11:34:08 +080010375 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10376 if (os_snprintf_error(sizeof(config_file), res)) {
10377 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10378 return RETURN_ERR;
10379 }
developera3511852023-06-14 14:12:59 +080010380 wifi_hostapdRead(config_file, "uapsd_advertisement_enabled", buf, sizeof(buf));
10381 if (strlen(buf) == 0 || strncmp("1", buf, 1) == 0)
10382 *output = TRUE;
10383 else
10384 *output = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080010385
developera3511852023-06-14 14:12:59 +080010386 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010387}
10388
10389// enables/disables Automatic Power Save Delivery on the hardwarwe for this AP
10390INT wifi_setApWmmUapsdEnable(INT apIndex, BOOL enable)
10391{
developera3511852023-06-14 14:12:59 +080010392 //save config and apply instantly.
10393 char config_file[MAX_BUF_SIZE] = {0};
10394 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010395 int res;
developer72fb0bb2023-01-11 09:46:29 +080010396
developera3511852023-06-14 14:12:59 +080010397 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10398 list.name = "uapsd_advertisement_enabled";
10399 list.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080010400
developer75bd10c2023-06-27 11:34:08 +080010401 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10402 if (os_snprintf_error(sizeof(config_file), res)) {
10403 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10404 return RETURN_ERR;
10405 }
developera3511852023-06-14 14:12:59 +080010406 wifi_hostapdWrite(config_file, &list, 1);
10407 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10408 wifi_quick_reload_ap(apIndex);
10409 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010410
developera3511852023-06-14 14:12:59 +080010411 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010412}
10413
10414// Sets the WMM ACK policy on the hardware. AckPolicy false means do not acknowledge, true means acknowledge
10415INT wifi_setApWmmOgAckPolicy(INT apIndex, INT class, BOOL ackPolicy) //RDKB
10416{
developera3511852023-06-14 14:12:59 +080010417 char interface_name[16] = {0};
10418 // assume class 0->BE, 1->BK, 2->VI, 3->VO
developer8078acf2023-08-04 18:52:48 +080010419
developera3511852023-06-14 14:12:59 +080010420 char buf[128] = {0};
10421 char ack_filepath[128] = {0};
10422 uint16_t bitmap = 0;
10423 uint16_t class_map[4] = {0x0009, 0x0006, 0x0030, 0x00C0};
10424 FILE *f = NULL;
developere40952c2023-06-15 18:46:43 +080010425 int res;
developerc14d83a2023-06-29 20:09:42 +080010426 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080010427
developera3511852023-06-14 14:12:59 +080010428 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010429
developera3511852023-06-14 14:12:59 +080010430 // Get current setting
developere40952c2023-06-15 18:46:43 +080010431 res = snprintf(ack_filepath, sizeof(ack_filepath), "%s%d.txt", NOACK_MAP_FILE, apIndex);
10432 if (os_snprintf_error(sizeof(ack_filepath), res)) {
10433 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10434 return RETURN_ERR;
10435 }
developer8078acf2023-08-04 18:52:48 +080010436 res = _syscmd_secure(buf, sizeof(buf), "cat %s 2> /dev/null", ack_filepath);
10437 if (res) {
10438 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010439 }
developer8078acf2023-08-04 18:52:48 +080010440
developerd14dff12023-06-28 22:47:44 +080010441 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080010442 if (hal_strtoul(buf, 10, &tmp) < 0) {
10443 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080010444 }
developerc14d83a2023-06-29 20:09:42 +080010445 bitmap = tmp;
developerd14dff12023-06-28 22:47:44 +080010446 }
developer72fb0bb2023-01-11 09:46:29 +080010447
developera3511852023-06-14 14:12:59 +080010448 if (ackPolicy == TRUE) { // True, unset this class
10449 bitmap &= ~class_map[class];
10450 } else { // False, set this class
10451 bitmap |= class_map[class];
10452 }
developer72fb0bb2023-01-11 09:46:29 +080010453
developera3511852023-06-14 14:12:59 +080010454 f = fopen(ack_filepath, "w");
10455 if (f == NULL) {
developer37646972023-06-29 10:58:43 +080010456 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
10457 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +080010458 return RETURN_ERR;
10459 }
developer37646972023-06-29 10:58:43 +080010460 if (fprintf(f, "%hu", bitmap) < 0)
10461 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
10462
10463 if (fclose(f) == EOF) {
10464 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
10465 return RETURN_ERR;
10466 }
developer72fb0bb2023-01-11 09:46:29 +080010467
developera3511852023-06-14 14:12:59 +080010468 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10469 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080010470
10471 res = _syscmd_secure(buf, sizeof(buf), "iw dev %s set noack_map 0x%04x\n", interface_name, bitmap);
10472 if (res) {
10473 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010474 }
developer72fb0bb2023-01-11 09:46:29 +080010475
developera3511852023-06-14 14:12:59 +080010476 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
10477 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010478}
10479
10480//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.
10481INT wifi_getApMaxAssociatedDevices(INT apIndex, UINT *output_uint)
10482{
developer75bd10c2023-06-27 11:34:08 +080010483 int res;
10484
developera3511852023-06-14 14:12:59 +080010485 //get the running status from driver
10486 if(!output_uint)
10487 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010488
developera3511852023-06-14 14:12:59 +080010489 char output[16]={'\0'};
10490 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010491
developer75bd10c2023-06-27 11:34:08 +080010492 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10493 if (os_snprintf_error(sizeof(config_file), res)) {
10494 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10495 return RETURN_ERR;
10496 }
developera3511852023-06-14 14:12:59 +080010497 wifi_hostapdRead(config_file, "max_num_sta", output, sizeof(output));
10498 if (strlen(output) == 0) *output_uint = MAX_ASSOCIATED_STA_NUM;
10499 else {
10500 int device_num = atoi(output);
10501 if (device_num > MAX_ASSOCIATED_STA_NUM || device_num < 0) {
10502 wifi_dbg_printf("\n[%s]: get max_num_sta error: %d", __func__, device_num);
10503 return RETURN_ERR;
10504 }
10505 else {
10506 *output_uint = device_num;
10507 }
10508 }
developer72fb0bb2023-01-11 09:46:29 +080010509
developera3511852023-06-14 14:12:59 +080010510 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010511}
10512
10513INT wifi_setApMaxAssociatedDevices(INT apIndex, UINT number)
10514{
developera3511852023-06-14 14:12:59 +080010515 //store to wifi config, apply instantly
10516 char str[MAX_BUF_SIZE]={'\0'};
10517 struct params params;
10518 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080010519 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080010520
developera3511852023-06-14 14:12:59 +080010521 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10522 if (number > MAX_ASSOCIATED_STA_NUM) {
10523 WIFI_ENTRY_EXIT_DEBUG("%s: Invalid input\n",__func__);
10524 return RETURN_ERR;
10525 }
developer75bd10c2023-06-27 11:34:08 +080010526 res = snprintf(str, sizeof(str), "%d", number);
10527 if (os_snprintf_error(sizeof(str), res)) {
10528 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10529 return RETURN_ERR;
10530 }
developera3511852023-06-14 14:12:59 +080010531 params.name = "max_num_sta";
10532 params.value = str;
developer72fb0bb2023-01-11 09:46:29 +080010533
developer32f2a182023-06-27 19:50:41 +080010534 res = snprintf(config_file,
10535 sizeof(config_file), "%s%d.conf",CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010536 if (os_snprintf_error(sizeof(config_file), res)) {
10537 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10538 return RETURN_ERR;
10539 }
developer32f2a182023-06-27 19:50:41 +080010540 ret = wifi_hostapdWrite(config_file, &params, 1);
developera3511852023-06-14 14:12:59 +080010541 if (ret) {
10542 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n"
10543 ,__func__, ret);
10544 }
developer72fb0bb2023-01-11 09:46:29 +080010545
developera3511852023-06-14 14:12:59 +080010546 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10547 if (ret) {
10548 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n"
10549 ,__func__, ret);
10550 }
10551 wifi_reloadAp(apIndex);
10552 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010553
developera3511852023-06-14 14:12:59 +080010554 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010555}
10556
10557//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.
10558INT wifi_getApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT *output_uint)
10559{
developera3511852023-06-14 14:12:59 +080010560 //get the current threshold
10561 if(!output_uint)
10562 return RETURN_ERR;
10563 wifi_getApMaxAssociatedDevices(apIndex, output_uint);
10564 if (*output_uint == 0)
10565 *output_uint = 50;
10566 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010567}
10568
10569INT wifi_setApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT Threshold)
10570{
developera3511852023-06-14 14:12:59 +080010571 //store the config, reset threshold, reset AssociatedDevicesHighWatermarkThresholdReached, reset AssociatedDevicesHighWatermarkDate to current time
10572 if (!wifi_setApMaxAssociatedDevices(apIndex, Threshold))
10573 return RETURN_OK;
10574 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010575}
10576
10577//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.
10578INT wifi_getApAssociatedDevicesHighWatermarkThresholdReached(INT apIndex, UINT *output_uint)
10579{
developera3511852023-06-14 14:12:59 +080010580 if(!output_uint)
10581 return RETURN_ERR;
10582 *output_uint = 3;
10583 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010584}
10585
10586//Maximum number of associated devices that have ever associated with the access point concurrently since the last reset of the device or WiFi module.
10587INT wifi_getApAssociatedDevicesHighWatermark(INT apIndex, UINT *output_uint)
10588{
developera3511852023-06-14 14:12:59 +080010589 if(!output_uint)
10590 return RETURN_ERR;
10591 *output_uint = 3;
10592 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010593}
10594
10595//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.
10596INT wifi_getApAssociatedDevicesHighWatermarkDate(INT apIndex, ULONG *output_in_seconds)
10597{
developera3511852023-06-14 14:12:59 +080010598 if(!output_in_seconds)
10599 return RETURN_ERR;
10600 *output_in_seconds = 0;
10601 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010602}
10603
10604//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
10605INT wifi_getApSecurityModesSupported(INT apIndex, CHAR *output)
10606{
developere40952c2023-06-15 18:46:43 +080010607 int res;
10608
developera3511852023-06-14 14:12:59 +080010609 if(!output || apIndex>=MAX_APS)
10610 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010611 //res = snprintf(output, 128, "None,WPA-Personal,WPA2-Personal,WPA-WPA2-Personal,WPA-Enterprise,WPA2-Enterprise,WPA-WPA2-Enterprise");
10612 res = snprintf(output, 128, "None,WPA2-Personal,WPA-WPA2-Personal,WPA2-Enterprise,WPA-WPA2-Enterprise,WPA3-Personal,WPA3-Enterprise");
10613 if (os_snprintf_error(128, res)) {
10614 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10615 return RETURN_ERR;
10616 }
developera3511852023-06-14 14:12:59 +080010617 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010618}
developer72fb0bb2023-01-11 09:46:29 +080010619
10620//The value MUST be a member of the list reported by the ModesSupported parameter. Indicates which security mode is enabled.
10621INT wifi_getApSecurityModeEnabled(INT apIndex, CHAR *output)
10622{
developera3511852023-06-14 14:12:59 +080010623 char config_file[128] = {0};
10624 char wpa[16] = {0};
10625 char key_mgmt[64] = {0};
developer9ce44382023-06-28 11:09:37 +080010626 int res = -1;
developere40952c2023-06-15 18:46:43 +080010627
developera3511852023-06-14 14:12:59 +080010628 if (!output)
10629 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010630
developer75bd10c2023-06-27 11:34:08 +080010631 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10632 if (os_snprintf_error(sizeof(config_file), res)) {
10633 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10634 return RETURN_ERR;
10635 }
developera3511852023-06-14 14:12:59 +080010636 wifi_hostapdRead(config_file, "wpa", wpa, sizeof(wpa));
developer72fb0bb2023-01-11 09:46:29 +080010637
developer32f2a182023-06-27 19:50:41 +080010638 memcpy(output, "None", 4);//Copying "None" to output string for default case
10639 output[4] = '\0';
developera3511852023-06-14 14:12:59 +080010640 wifi_hostapdRead(config_file, "wpa_key_mgmt", key_mgmt, sizeof(key_mgmt));
10641 if (strstr(key_mgmt, "WPA-PSK") && strstr(key_mgmt, "SAE") == NULL) {
10642 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010643 res = snprintf(output, 32, "WPA-Personal");
developera3511852023-06-14 14:12:59 +080010644 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010645 res = snprintf(output, 32, "WPA2-Personal");
developera3511852023-06-14 14:12:59 +080010646 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010647 res = snprintf(output, 32, "WPA-WPA2-Personal");
developer72fb0bb2023-01-11 09:46:29 +080010648
developera3511852023-06-14 14:12:59 +080010649 } else if (strstr(key_mgmt, "WPA-EAP-SUITE-B-192")) {
developere40952c2023-06-15 18:46:43 +080010650 res = snprintf(output, 32, "WPA3-Enterprise");
developera3511852023-06-14 14:12:59 +080010651 } else if (strstr(key_mgmt, "WPA-EAP")) {
10652 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010653 res = snprintf(output, 32, "WPA-Enterprise");
developera3511852023-06-14 14:12:59 +080010654 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010655 res = snprintf(output, 32, "WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010656 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010657 res = snprintf(output, 32, "WPA-WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010658 } else if (strstr(key_mgmt, "SAE")) {
10659 if (strstr(key_mgmt, "WPA-PSK") == NULL)
developere40952c2023-06-15 18:46:43 +080010660 res = snprintf(output, 32, "WPA3-Personal");
developera3511852023-06-14 14:12:59 +080010661 else
developere40952c2023-06-15 18:46:43 +080010662 res = snprintf(output, 32, "WPA3-Personal-Transition");
10663 }
10664 if (os_snprintf_error(32, res)) {
10665 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10666 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080010667 }
developer72fb0bb2023-01-11 09:46:29 +080010668
developera3511852023-06-14 14:12:59 +080010669 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
10670 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010671}
developer69b61b02023-03-07 17:17:44 +080010672
developer72fb0bb2023-01-11 09:46:29 +080010673INT wifi_setApSecurityModeEnabled(INT apIndex, CHAR *encMode)
10674{
developer32f2a182023-06-27 19:50:41 +080010675 char securityType[32] = {0};
10676 char authMode[32] = {0};
10677 unsigned long len_sec, len_auth;
developera3511852023-06-14 14:12:59 +080010678 //store settings and wait for wifi up to apply
10679 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10680 if(!encMode)
10681 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010682
developera3511852023-06-14 14:12:59 +080010683 if (strcmp(encMode, "None")==0)
10684 {
developer32f2a182023-06-27 19:50:41 +080010685 len_sec = strlen("None");
10686 len_auth = strlen("None");
10687 memcpy(securityType, "None", len_sec);
10688 memcpy(authMode, "None", len_auth);
10689 } else if (strcmp(encMode, "WPA-WPA2-Personal")==0) {
10690 len_sec = strlen("WPAand11i");
10691 memcpy(securityType, "WPAand11i", len_sec);
10692 len_auth = strlen("PSKAuthentication");
10693 memcpy(authMode, "PSKAuthentication", len_auth);
10694 } else if (strcmp(encMode, "WPA-WPA2-Enterprise")==0) {
10695 len_sec = strlen("WPAand11i");
10696 memcpy(securityType, "WPAand11i", len_sec);
10697 len_auth = strlen("EAPAuthentication");
10698 memcpy(authMode, "EAPAuthentication", len_auth);
10699 } else if (strcmp(encMode, "WPA-Personal")==0) {
10700 len_sec = strlen("WPA");
10701 memcpy(securityType, "WPA", len_sec);
10702 len_auth = strlen("PSKAuthentication");
10703 memcpy(authMode, "PSKAuthentication", len_auth);
10704 } else if (strcmp(encMode, "WPA-Enterprise")==0) {
10705 len_sec = strlen("WPA");
10706 memcpy(securityType, "WPA", len_sec);
10707 len_auth = strlen("EAPAuthentication");
10708 memcpy(authMode, "EAPAuthentication", len_auth);
10709 } else if (strcmp(encMode, "WPA2-Personal")==0) {
10710 len_sec = strlen("11i");
10711 memcpy(securityType, "11i", len_sec);
10712 len_auth = strlen("PSKAuthentication");
10713 memcpy(authMode, "PSKAuthentication", len_auth);
10714 } else if (strcmp(encMode, "WPA2-Enterprise")==0) {
10715 len_sec = strlen("11i");
10716 memcpy(securityType, "11i", len_sec);
10717 len_auth = strlen("EAPAuthentication");
10718 memcpy(authMode, "EAPAuthentication", len_auth);
10719 } else if (strcmp(encMode, "WPA3-Personal") == 0) {
10720 len_sec = strlen("11i");
10721 memcpy(securityType, "11i", len_sec);
10722 len_auth = strlen("SAEAuthentication");
10723 memcpy(authMode, "SAEAuthentication", len_auth);
10724 } else if (strcmp(encMode, "WPA3-Personal-Transition") == 0) {
10725 len_sec = strlen("11i");
10726 memcpy(securityType, "11i", len_sec);
10727 len_auth = strlen("PSK-SAEAuthentication");
10728 memcpy(authMode, "PSK-SAEAuthentication", len_auth);
10729 } else if (strcmp(encMode, "WPA3-Enterprise") == 0) {
10730 len_sec = strlen("11i");
10731 memcpy(securityType, "11i", len_sec);
10732 len_auth = strlen("EAP_192-bit_Authentication");
10733 memcpy(authMode, "EAP_192-bit_Authentication", len_auth);
10734 } else if (strcmp(encMode, "OWE") == 0) {
10735 len_sec = strlen("11i");
10736 memcpy(securityType, "11i", len_sec);
10737 len_auth = strlen("Enhanced_Open");
10738 memcpy(authMode, "Enhanced_Open", len_auth);
10739 } else {
10740 len_sec = strlen("None");
10741 memcpy(securityType, "None", len_sec);
10742 len_auth = strlen("None");
10743 memcpy(authMode, "None", len_auth);
developera3511852023-06-14 14:12:59 +080010744 }
developer32f2a182023-06-27 19:50:41 +080010745 securityType[len_sec] = '\0';
10746 authMode[len_auth] = '\0';
developera3511852023-06-14 14:12:59 +080010747 wifi_setApBeaconType(apIndex, securityType);
10748 wifi_setApBasicAuthenticationMode(apIndex, authMode);
10749 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010750
developera3511852023-06-14 14:12:59 +080010751 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010752}
developer72fb0bb2023-01-11 09:46:29 +080010753
10754
10755//A literal PreSharedKey (PSK) expressed as a hexadecimal string.
10756// output_string must be pre-allocated as 64 character string by caller
10757// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10758INT wifi_getApSecurityPreSharedKey(INT apIndex, CHAR *output_string)
10759{
developera3511852023-06-14 14:12:59 +080010760 char buf[16] = {0};
10761 char config_file[MAX_BUF_SIZE] = {0};
10762 int res;
developer72fb0bb2023-01-11 09:46:29 +080010763
developera3511852023-06-14 14:12:59 +080010764 if(output_string==NULL)
10765 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010766
developera3511852023-06-14 14:12:59 +080010767 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10768 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010769 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10770 return RETURN_ERR;
10771 }
developera3511852023-06-14 14:12:59 +080010772 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010773
developera3511852023-06-14 14:12:59 +080010774 if(strcmp(buf,"0")==0)
10775 {
10776 printf("wpa_mode is %s ......... \n",buf);
10777 return RETURN_ERR;
10778 }
developer72fb0bb2023-01-11 09:46:29 +080010779
developera3511852023-06-14 14:12:59 +080010780 wifi_dbg_printf("\nFunc=%s\n",__func__);
10781 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10782 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010783 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10784 return RETURN_ERR;
10785 }
developere5750452023-05-15 16:46:42 +080010786 wifi_hostapdRead(config_file,"wpa_psk",output_string,65);
developera3511852023-06-14 14:12:59 +080010787 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010788
developera3511852023-06-14 14:12:59 +080010789 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010790}
10791
10792// sets an enviornment variable for the psk. Input string preSharedKey must be a maximum of 64 characters
10793// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10794INT wifi_setApSecurityPreSharedKey(INT apIndex, CHAR *preSharedKey)
10795{
developera3511852023-06-14 14:12:59 +080010796 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
10797 struct params params={'\0'};
developer32f2a182023-06-27 19:50:41 +080010798 int ret;
developera3511852023-06-14 14:12:59 +080010799 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010800
developera3511852023-06-14 14:12:59 +080010801 if(NULL == preSharedKey)
10802 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010803
developera3511852023-06-14 14:12:59 +080010804 params.name = "wpa_psk";
developer72fb0bb2023-01-11 09:46:29 +080010805
developera3511852023-06-14 14:12:59 +080010806 if(strlen(preSharedKey) != 64)
10807 {
10808 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 64 chars\n");
10809 return RETURN_ERR;
10810 }
10811 params.value = preSharedKey;
developer32f2a182023-06-27 19:50:41 +080010812 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10813 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080010814 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10815 return RETURN_ERR;
10816 }
developera3511852023-06-14 14:12:59 +080010817 ret = wifi_hostapdWrite(config_file, &params, 1);
10818 if(!ret) {
10819 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10820 wifi_reloadAp(apIndex);
10821 }
10822 return ret;
10823 //TODO: call hostapd_cli for dynamic_config_control
developer72fb0bb2023-01-11 09:46:29 +080010824}
10825
10826//A passphrase from which the PreSharedKey is to be generated, for WPA-Personal or WPA2-Personal or WPA-WPA2-Personal security modes.
10827// outputs the passphrase, maximum 63 characters
10828INT wifi_getApSecurityKeyPassphrase(INT apIndex, CHAR *output_string)
10829{
developera3511852023-06-14 14:12:59 +080010830 char config_file[MAX_BUF_SIZE] = {0}, buf[32] = {0};
developer75bd10c2023-06-27 11:34:08 +080010831 int res;
developer72fb0bb2023-01-11 09:46:29 +080010832
developera3511852023-06-14 14:12:59 +080010833 wifi_dbg_printf("\nFunc=%s\n",__func__);
10834 if (NULL == output_string)
10835 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010836
developer75bd10c2023-06-27 11:34:08 +080010837 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10838 if (os_snprintf_error(sizeof(config_file), res)) {
10839 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10840 return RETURN_ERR;
10841 }
10842
developera3511852023-06-14 14:12:59 +080010843 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
10844 if(strcmp(buf,"0")==0)
10845 {
10846 printf("wpa_mode is %s ......... \n",buf);
10847 return RETURN_ERR;
10848 }
developer72fb0bb2023-01-11 09:46:29 +080010849
developera3511852023-06-14 14:12:59 +080010850 wifi_hostapdRead(config_file,"wpa_passphrase",output_string,64);
10851 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010852
developera3511852023-06-14 14:12:59 +080010853 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010854}
10855
10856// sets the passphrase enviornment variable, max 63 characters
10857INT wifi_setApSecurityKeyPassphrase(INT apIndex, CHAR *passPhrase)
10858{
developera3511852023-06-14 14:12:59 +080010859 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
10860 struct params params={'\0'};
10861 char config_file[MAX_BUF_SIZE] = {0};
10862 int ret;
developer72fb0bb2023-01-11 09:46:29 +080010863
developera3511852023-06-14 14:12:59 +080010864 if(NULL == passPhrase)
10865 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010866
developera3511852023-06-14 14:12:59 +080010867 if(strlen(passPhrase)<8 || strlen(passPhrase)>63)
10868 {
10869 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 8 to 63 chars\n");
10870 return RETURN_ERR;
10871 }
10872 params.name = "wpa_passphrase";
10873 params.value = passPhrase;
developer32f2a182023-06-27 19:50:41 +080010874 ret = snprintf(config_file, sizeof(config_file),
10875 "%s%d.conf", CONFIG_PREFIX, apIndex);
10876 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080010877 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10878 return RETURN_ERR;
10879 }
developera3511852023-06-14 14:12:59 +080010880 ret=wifi_hostapdWrite(config_file,&params,1);
10881 if(!ret) {
10882 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +080010883 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +080010884 }
developer72fb0bb2023-01-11 09:46:29 +080010885
developera3511852023-06-14 14:12:59 +080010886 return ret;
developer72fb0bb2023-01-11 09:46:29 +080010887}
10888
10889//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.
10890INT wifi_setApSecurityReset(INT apIndex)
10891{
developera3511852023-06-14 14:12:59 +080010892 char original_config_file[64] = {0};
10893 char current_config_file[64] = {0};
10894 char buf[64] = {0};
developer8078acf2023-08-04 18:52:48 +080010895
developera3511852023-06-14 14:12:59 +080010896 char wpa[4] = {0};
10897 char wpa_psk[64] = {0};
10898 char wpa_passphrase[64] = {0};
10899 char wpa_psk_file[128] = {0};
10900 char wpa_key_mgmt[64] = {0};
10901 char wpa_pairwise[32] = {0};
10902 wifi_band band;
10903 struct params list[6];
developere40952c2023-06-15 18:46:43 +080010904 int res;
developer72fb0bb2023-01-11 09:46:29 +080010905
developera3511852023-06-14 14:12:59 +080010906 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010907
developera3511852023-06-14 14:12:59 +080010908 band = wifi_index_to_band(apIndex);
developer37646972023-06-29 10:58:43 +080010909 if (band == band_2_4) {
10910 res = snprintf(original_config_file, sizeof(original_config_file),
10911 "/etc/hostapd-2G.conf");
10912 if (os_snprintf_error(sizeof(original_config_file), res)) {
10913 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10914 return RETURN_ERR;
10915 }
developer8078acf2023-08-04 18:52:48 +080010916
developer37646972023-06-29 10:58:43 +080010917 } else if (band == band_5) {
10918 res = snprintf(original_config_file, sizeof(original_config_file),
10919 "/etc/hostapd-5G.conf");
10920 if (os_snprintf_error(sizeof(original_config_file), res)) {
10921 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10922 return RETURN_ERR;
10923 }
10924 } else if (band == band_6) {
10925 res = snprintf(original_config_file, sizeof(original_config_file),
10926 "/etc/hostapd-6G.conf");
10927 if (os_snprintf_error(sizeof(original_config_file), res)) {
10928 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10929 return RETURN_ERR;
10930 }
10931 } else
developera3511852023-06-14 14:12:59 +080010932 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010933
developera3511852023-06-14 14:12:59 +080010934 wifi_hostapdRead(original_config_file, "wpa", wpa, sizeof(wpa));
10935 list[0].name = "wpa";
10936 list[0].value = wpa;
developer69b61b02023-03-07 17:17:44 +080010937
developera3511852023-06-14 14:12:59 +080010938 wifi_hostapdRead(original_config_file, "wpa_psk", wpa_psk, sizeof(wpa_psk));
10939 list[1].name = "wpa_psk";
10940 list[1].value = wpa_psk;
developer72fb0bb2023-01-11 09:46:29 +080010941
developera3511852023-06-14 14:12:59 +080010942 wifi_hostapdRead(original_config_file, "wpa_passphrase", wpa_passphrase, sizeof(wpa_passphrase));
10943 list[2].name = "wpa_passphrase";
10944 list[2].value = wpa_passphrase;
developer72fb0bb2023-01-11 09:46:29 +080010945
developera3511852023-06-14 14:12:59 +080010946 wifi_hostapdRead(original_config_file, "wpa_psk_file", wpa_psk_file, sizeof(wpa_psk_file));
developer72fb0bb2023-01-11 09:46:29 +080010947
developera3511852023-06-14 14:12:59 +080010948 if (strlen(wpa_psk_file) == 0)
developer32f2a182023-06-27 19:50:41 +080010949 memcpy(wpa_psk_file, PSK_FILE, strlen(PSK_FILE));
developer72fb0bb2023-01-11 09:46:29 +080010950
developera3511852023-06-14 14:12:59 +080010951 if (access(wpa_psk_file, F_OK) != 0) {
developer8078acf2023-08-04 18:52:48 +080010952 res = _syscmd_secure(buf, sizeof(buf),"touch %s", wpa_psk_file);
10953 if (res) {
10954 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080010955 }
developera3511852023-06-14 14:12:59 +080010956 }
10957 list[3].name = "wpa_psk_file";
10958 list[3].value = wpa_psk_file;
developer72fb0bb2023-01-11 09:46:29 +080010959
developera3511852023-06-14 14:12:59 +080010960 wifi_hostapdRead(original_config_file, "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt));
10961 list[4].name = "wpa_key_mgmt";
10962 list[4].value = wpa_key_mgmt;
developer72fb0bb2023-01-11 09:46:29 +080010963
developera3511852023-06-14 14:12:59 +080010964 wifi_hostapdRead(original_config_file, "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise));
10965 list[5].name = "wpa_pairwise";
10966 list[5].value = wpa_pairwise;
developer72fb0bb2023-01-11 09:46:29 +080010967
developer32f2a182023-06-27 19:50:41 +080010968 res = snprintf(current_config_file, sizeof(current_config_file),
10969 "%s%d.conf", CONFIG_PREFIX, apIndex);
10970 if (os_snprintf_error(sizeof(current_config_file), res)) {
10971 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10972 return RETURN_ERR;
10973 }
developera3511852023-06-14 14:12:59 +080010974 wifi_hostapdWrite(current_config_file, list, 6);
developer72fb0bb2023-01-11 09:46:29 +080010975
developera3511852023-06-14 14:12:59 +080010976 wifi_setApEnable(apIndex, FALSE);
10977 wifi_setApEnable(apIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +080010978
developera3511852023-06-14 14:12:59 +080010979 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10980 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010981}
10982
10983//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).
10984INT wifi_getApSecurityRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
10985{
developera3511852023-06-14 14:12:59 +080010986 char config_file[64] = {0};
10987 char buf[64] = {0};
developer8078acf2023-08-04 18:52:48 +080010988
developere40952c2023-06-15 18:46:43 +080010989 int res;
developer72fb0bb2023-01-11 09:46:29 +080010990
developera3511852023-06-14 14:12:59 +080010991 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010992
developera3511852023-06-14 14:12:59 +080010993 if(!IP_output || !Port_output || !RadiusSecret_output)
10994 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010995
developera3511852023-06-14 14:12:59 +080010996 // Read the first matched config
developere40952c2023-06-15 18:46:43 +080010997 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10998 if (os_snprintf_error(sizeof(config_file), res)) {
10999 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11000 return RETURN_ERR;
11001 }
developer8078acf2023-08-04 18:52:48 +080011002 res = _syscmd_secure(buf, sizeof(buf),
developer32f2a182023-06-27 19:50:41 +080011003 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
developer8078acf2023-08-04 18:52:48 +080011004 if (res) {
11005 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011006 }
developera3511852023-06-14 14:12:59 +080011007 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011008
developera3511852023-06-14 14:12:59 +080011009 memset(buf, 0, sizeof(buf));
developer8078acf2023-08-04 18:52:48 +080011010 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11011 if (res) {
11012 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011013 }
developera3511852023-06-14 14:12:59 +080011014 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011015
developera3511852023-06-14 14:12:59 +080011016 memset(buf, 0, sizeof(buf));
developer8078acf2023-08-04 18:52:48 +080011017 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11018 if (res) {
11019 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011020 }
developera3511852023-06-14 14:12:59 +080011021 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011022
developera3511852023-06-14 14:12:59 +080011023 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11024 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011025}
11026
11027INT wifi_setApSecurityRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11028{
developera3511852023-06-14 14:12:59 +080011029 char config_file[64] = {0};
11030 char port_str[8] = {0};
11031 char cmd[256] = {0};
11032 char buf[128] = {0};
11033 int res;
developer72fb0bb2023-01-11 09:46:29 +080011034
developera3511852023-06-14 14:12:59 +080011035 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011036
developere5750452023-05-15 16:46:42 +080011037 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011038 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011039
developera3511852023-06-14 14:12:59 +080011040 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11041 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011042
developera3511852023-06-14 14:12:59 +080011043 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11044 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011045 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11046 return RETURN_ERR;
11047 }
developer72fb0bb2023-01-11 09:46:29 +080011048
developer8078acf2023-08-04 18:52:48 +080011049 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '# radius 1'", config_file);
11050 if (res) {
11051 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer46506162023-06-12 10:09:39 +080011052 }
developer8078acf2023-08-04 18:52:48 +080011053
developera3511852023-06-14 14:12:59 +080011054 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011055
developere40952c2023-06-15 18:46:43 +080011056 res = snprintf(port_str, sizeof(port_str), "%d", port);
developer37646972023-06-29 10:58:43 +080011057 if (os_snprintf_error(sizeof(port_str), res)) {
11058 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11059 return RETURN_ERR;
11060 }
developera3511852023-06-14 14:12:59 +080011061 if (strlen(buf) == 0) {
11062 // Append
11063 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 1\\n"
11064 "auth_server_addr=%s\\n"
11065 "auth_server_port=%s\\n"
11066 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11067 if (os_snprintf_error(sizeof(cmd), res)) {
11068 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11069 return RETURN_ERR;
11070 }
developer8078acf2023-08-04 18:52:48 +080011071
11072 if(_syscmd_secure(buf, sizeof(buf), "echo -e '# radius 1\\n"
11073 "auth_server_addr=%s\\n"
11074 "auth_server_port=%s\\n"
11075 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file)) {
11076 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11077 return RETURN_ERR;
11078 }
developera3511852023-06-14 14:12:59 +080011079 } else {
11080 // Delete the three lines setting after the "# radius 1" comment
developer8078acf2023-08-04 18:52:48 +080011081 res = _syscmd_secure(buf, sizeof(buf), "sed -i '/# radius 1/{n;N;N;d}' %s", config_file);
11082 if (res) {
11083 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developera3511852023-06-14 14:12:59 +080011084 }
developera3511852023-06-14 14:12:59 +080011085 memset(cmd, 0, sizeof(cmd));
11086 // Use "# radius 1" comment to find the location to insert the radius setting
11087 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 1/"
11088 "# radius 1\\n"
11089 "auth_server_addr=%s\\n"
11090 "auth_server_port=%s\\n"
11091 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11092 if (os_snprintf_error(sizeof(cmd), res)) {
11093 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11094 return RETURN_ERR;
11095 }
developer8078acf2023-08-04 18:52:48 +080011096
11097 if(_syscmd_secure(buf, sizeof(buf), "sed -i 's/# radius 1/"
11098 "# radius 1\\n"
11099 "auth_server_addr=%s\\n"
11100 "auth_server_port=%s\\n"
11101 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file)) {
11102 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11103 return RETURN_ERR;
11104 }
developera3511852023-06-14 14:12:59 +080011105 }
developer72fb0bb2023-01-11 09:46:29 +080011106
developera3511852023-06-14 14:12:59 +080011107 wifi_reloadAp(apIndex);
11108 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11109 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011110}
11111
11112INT wifi_getApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11113{
developera3511852023-06-14 14:12:59 +080011114 char config_file[64] = {0};
11115 char buf[64] = {0};
developer8078acf2023-08-04 18:52:48 +080011116
developere40952c2023-06-15 18:46:43 +080011117 int res;
developer72fb0bb2023-01-11 09:46:29 +080011118
developera3511852023-06-14 14:12:59 +080011119 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011120
developera3511852023-06-14 14:12:59 +080011121 if(!IP_output || !Port_output || !RadiusSecret_output)
11122 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011123
developera3511852023-06-14 14:12:59 +080011124 // Read the second matched config
developere40952c2023-06-15 18:46:43 +080011125 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11126 if (os_snprintf_error(sizeof(config_file), res)) {
11127 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11128 return RETURN_ERR;
11129 }
11130
developer8078acf2023-08-04 18:52:48 +080011131 res = _syscmd_secure(buf, sizeof(buf),
developer32f2a182023-06-27 19:50:41 +080011132 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11133 config_file);
developer8078acf2023-08-04 18:52:48 +080011134 if (res) {
11135 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011136 }
developer8078acf2023-08-04 18:52:48 +080011137
developera3511852023-06-14 14:12:59 +080011138 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011139
developera3511852023-06-14 14:12:59 +080011140 memset(buf, 0, sizeof(buf));
developer8078acf2023-08-04 18:52:48 +080011141 res = _syscmd_secure(buf, sizeof(buf),
developer32f2a182023-06-27 19:50:41 +080011142 "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11143 config_file);
developer8078acf2023-08-04 18:52:48 +080011144 if (res) {
11145 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011146 }
developer8078acf2023-08-04 18:52:48 +080011147
developera3511852023-06-14 14:12:59 +080011148 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011149
developera3511852023-06-14 14:12:59 +080011150 memset(buf, 0, sizeof(buf));
developer8078acf2023-08-04 18:52:48 +080011151 res = _syscmd_secure(buf, sizeof(buf),
developer32f2a182023-06-27 19:50:41 +080011152 "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11153 config_file);
developer8078acf2023-08-04 18:52:48 +080011154 if (res) {
11155 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011156 }
11157
developera3511852023-06-14 14:12:59 +080011158 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011159
developera3511852023-06-14 14:12:59 +080011160 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11161 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011162}
11163
11164INT wifi_setApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11165{
developera3511852023-06-14 14:12:59 +080011166 char config_file[64] = {0};
11167 char port_str[8] = {0};
11168 char cmd[256] = {0};
11169 char buf[128] = {0};
11170 int res;
developer72fb0bb2023-01-11 09:46:29 +080011171
developera3511852023-06-14 14:12:59 +080011172 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011173
developere5750452023-05-15 16:46:42 +080011174 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011175 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011176
developera3511852023-06-14 14:12:59 +080011177 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11178 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011179
developera3511852023-06-14 14:12:59 +080011180 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11181 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011182 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11183 return RETURN_ERR;
11184 }
developer72fb0bb2023-01-11 09:46:29 +080011185
developer8078acf2023-08-04 18:52:48 +080011186 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep '# radius 2'", config_file);
11187 if (res) {
11188 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer46506162023-06-12 10:09:39 +080011189 }
developer8078acf2023-08-04 18:52:48 +080011190
developera3511852023-06-14 14:12:59 +080011191 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011192
developera3511852023-06-14 14:12:59 +080011193 res = snprintf(port_str, sizeof(port_str), "%d", port);
11194 if (os_snprintf_error(sizeof(port_str), res)) {
developer46506162023-06-12 10:09:39 +080011195 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11196 return RETURN_ERR;
11197 }
developera3511852023-06-14 14:12:59 +080011198 if (strlen(buf) == 0) {
11199 // Append
11200 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 2\\n"
11201 "auth_server_addr=%s\\n"
11202 "auth_server_port=%s\\n"
11203 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11204 if (os_snprintf_error(sizeof(cmd), res)) {
11205 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11206 return RETURN_ERR;
11207 }
developer8078acf2023-08-04 18:52:48 +080011208
11209 if(_syscmd_secure(buf, sizeof(buf), "echo -e '# radius 2\\n"
11210 "auth_server_addr=%s\\n"
11211 "auth_server_port=%s\\n"
11212 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file)){
11213 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11214 return RETURN_ERR;
11215 }
developera3511852023-06-14 14:12:59 +080011216 } else {
11217 // Delete the three lines setting after the "# radius 2" comment
developer8078acf2023-08-04 18:52:48 +080011218 res = _syscmd_secure(buf, sizeof(buf), "sed -i '/# radius 2/{n;N;N;d}' %s", config_file);
11219 if (res) {
11220 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developera3511852023-06-14 14:12:59 +080011221 }
developer8078acf2023-08-04 18:52:48 +080011222
developera3511852023-06-14 14:12:59 +080011223 memset(cmd, 0, sizeof(cmd));
11224 // Use "# radius 2" comment to find the location to insert the radius setting
11225 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 2/"
11226 "# radius 2\\n"
11227 "auth_server_addr=%s\\n"
11228 "auth_server_port=%s\\n"
11229 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11230 if (os_snprintf_error(sizeof(cmd), res)) {
11231 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11232 return RETURN_ERR;
11233 }
developer8078acf2023-08-04 18:52:48 +080011234
11235 if(_syscmd_secure(buf, sizeof(buf), "sed -i 's/# radius 2/"
11236 "# radius 2\\n"
11237 "auth_server_addr=%s\\n"
11238 "auth_server_port=%s\\n"
11239 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file)){
11240 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11241 return RETURN_ERR;
11242 }
developera3511852023-06-14 14:12:59 +080011243 }
developer72fb0bb2023-01-11 09:46:29 +080011244
developera3511852023-06-14 14:12:59 +080011245 wifi_reloadAp(apIndex);
11246 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11247 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011248}
11249
11250//RadiusSettings
11251INT wifi_getApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *output)
11252{
developera3511852023-06-14 14:12:59 +080011253 if(!output)
11254 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011255
developera3511852023-06-14 14:12:59 +080011256 output->RadiusServerRetries = 3; //Number of retries for Radius requests.
11257 output->RadiusServerRequestTimeout = 5; //Radius request timeout in seconds after which the request must be retransmitted for the # of retries available.
11258 output->PMKLifetime = 28800; //Default time in seconds after which a Wi-Fi client is forced to ReAuthenticate (def 8 hrs).
11259 output->PMKCaching = FALSE; //Enable or disable caching of PMK.
11260 output->PMKCacheInterval = 300; //Time interval in seconds after which the PMKSA (Pairwise Master Key Security Association) cache is purged (def 5 minutes).
11261 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.
11262 output->BlacklistTableTimeout = 600; //Time interval in seconds for which a client will continue to be blacklisted once it is marked so.
11263 output->IdentityRequestRetryInterval = 5; //Time Interval in seconds between identity requests retries. A value of 0 (zero) disables it.
11264 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 +080011265 //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 +080011266
developera3511852023-06-14 14:12:59 +080011267 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011268}
11269
11270INT wifi_setApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *input)
11271{
developera3511852023-06-14 14:12:59 +080011272 //store the paramters, and apply instantly
11273 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011274}
11275
11276//Device.WiFi.AccessPoint.{i}.WPS.Enable
11277//Enables or disables WPS functionality for this access point.
11278// outputs the WPS enable state of this ap in output_bool
11279INT wifi_getApWpsEnable(INT apIndex, BOOL *output_bool)
11280{
developera3511852023-06-14 14:12:59 +080011281 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +080011282 char buf[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080011283 int res;
11284
developera3511852023-06-14 14:12:59 +080011285 if(!output_bool)
11286 return RETURN_ERR;
11287 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11288 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080011289 res = _syscmd_secure(buf, sizeof(buf),
developer32f2a182023-06-27 19:50:41 +080011290 "hostapd_cli -i %s get_config | grep wps_state | cut -d '=' -f2",
11291 interface_name);
developer8078acf2023-08-04 18:52:48 +080011292 if (res) {
11293 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080011294 }
developera3511852023-06-14 14:12:59 +080011295 if(strstr(buf, "configured"))
11296 *output_bool=TRUE;
11297 else
11298 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +080011299
developera3511852023-06-14 14:12:59 +080011300 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080011301}
developer72fb0bb2023-01-11 09:46:29 +080011302
11303//Device.WiFi.AccessPoint.{i}.WPS.Enable
11304// sets the WPS enable enviornment variable for this ap to the value of enableValue, 1==enabled, 0==disabled
11305INT wifi_setApWpsEnable(INT apIndex, BOOL enable)
11306{
developera3511852023-06-14 14:12:59 +080011307 char config_file[MAX_BUF_SIZE] = {0};
11308 char buf[128] = {0};
11309 struct params params;
developere40952c2023-06-15 18:46:43 +080011310 int res;
developer72fb0bb2023-01-11 09:46:29 +080011311
developera3511852023-06-14 14:12:59 +080011312 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11313 //store the paramters, and wait for wifi up to apply
11314 params.name = "wps_state";
11315 if (enable == TRUE) {
11316 wifi_getApBeaconType(apIndex, buf);
11317 if (strncmp(buf, "None", 4) == 0) // If ap didn't set encryption
11318 params.value = "1";
11319 else // If ap set encryption
11320 params.value = "2";
11321 } else {
11322 params.value = "0";
11323 }
developer72fb0bb2023-01-11 09:46:29 +080011324
developere40952c2023-06-15 18:46:43 +080011325 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11326 if (os_snprintf_error(sizeof(config_file), res)) {
11327 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11328 return RETURN_ERR;
11329 }
developera3511852023-06-14 14:12:59 +080011330 wifi_hostapdWrite(config_file, &params, 1);
11331 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11332 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080011333
developera3511852023-06-14 14:12:59 +080011334 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11335 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011336}
11337
11338//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
11339INT wifi_getApWpsConfigMethodsSupported(INT apIndex, CHAR *output)
11340{
developere40952c2023-06-15 18:46:43 +080011341 int res;
developera3511852023-06-14 14:12:59 +080011342 if(!output)
11343 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011344 res = snprintf(output, 128, "PushButton,PIN");
11345 if (os_snprintf_error(128, res)) {
11346 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11347 return RETURN_ERR;
11348 }
11349
developera3511852023-06-14 14:12:59 +080011350 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011351}
11352
11353//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11354//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.
11355// Outputs a common separated list of the enabled WPS config methods, 64 bytes max
11356INT wifi_getApWpsConfigMethodsEnabled(INT apIndex, CHAR *output)
11357{
developere40952c2023-06-15 18:46:43 +080011358 int res;
developera3511852023-06-14 14:12:59 +080011359 if(!output)
11360 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011361 res = snprintf(output, 64, "PushButton,PIN");//Currently, supporting these two methods
11362 if (os_snprintf_error(64, res)) {
11363 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11364 return RETURN_ERR;
11365 }
developer72fb0bb2023-01-11 09:46:29 +080011366
developera3511852023-06-14 14:12:59 +080011367 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011368}
11369
11370//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11371// 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
11372INT wifi_setApWpsConfigMethodsEnabled(INT apIndex, CHAR *methodString)
11373{
developera3511852023-06-14 14:12:59 +080011374 //apply instantly. No setting need to be stored.
11375 char methods[MAX_BUF_SIZE], *token, *next_token;
11376 char config_file[MAX_BUF_SIZE], config_methods[MAX_BUF_SIZE] = {0};
11377 struct params params;
developere40952c2023-06-15 18:46:43 +080011378 int res;
developer72fb0bb2023-01-11 09:46:29 +080011379
developera3511852023-06-14 14:12:59 +080011380 if(!methodString)
11381 return RETURN_ERR;
11382 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11383 //store the paramters, and wait for wifi up to apply
developer72fb0bb2023-01-11 09:46:29 +080011384
developere40952c2023-06-15 18:46:43 +080011385 res = snprintf(methods, sizeof(methods), "%s", methodString);
11386 if (os_snprintf_error(sizeof(methods), res)) {
11387 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11388 return RETURN_ERR;
11389 }
developera3511852023-06-14 14:12:59 +080011390 for(token=methods; *token; token=next_token) {
11391 strtok_r(token, ",", &next_token);
11392 if(*token=='U' && !strcmp(methods, "USBFlashDrive"))
developere40952c2023-06-15 18:46:43 +080011393 res = snprintf(config_methods, sizeof(config_methods), "%s ", "usba");
developera3511852023-06-14 14:12:59 +080011394 else if(*token=='E')
11395 {
11396 if(!strcmp(methods, "Ethernet"))
developere40952c2023-06-15 18:46:43 +080011397 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ethernet");
developera3511852023-06-14 14:12:59 +080011398 else if(!strcmp(methods, "ExternalNFCToken"))
developere40952c2023-06-15 18:46:43 +080011399 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ext_nfc_token");
developera3511852023-06-14 14:12:59 +080011400 else
11401 printf("%s: Unknown WpsConfigMethod\n", __func__);
11402 }
11403 else if(*token=='I' && !strcmp(token, "IntegratedNFCToken"))
developere40952c2023-06-15 18:46:43 +080011404 res = snprintf(config_methods, sizeof(config_methods), "%s ", "int_nfc_token");
developera3511852023-06-14 14:12:59 +080011405 else if(*token=='N' && !strcmp(token, "NFCInterface"))
developere40952c2023-06-15 18:46:43 +080011406 res = snprintf(config_methods, sizeof(config_methods), "%s ", "nfc_interface");
developera3511852023-06-14 14:12:59 +080011407 else if(*token=='P' )
11408 {
11409 if(!strcmp(token, "PushButton"))
developere40952c2023-06-15 18:46:43 +080011410 res = snprintf(config_methods, sizeof(config_methods), "%s ", "push_button");
developera3511852023-06-14 14:12:59 +080011411 else if(!strcmp(token, "PIN"))
developere40952c2023-06-15 18:46:43 +080011412 res = snprintf(config_methods, sizeof(config_methods), "%s ", "keypad");
developera3511852023-06-14 14:12:59 +080011413 else
11414 printf("%s: Unknown WpsConfigMethod\n", __func__);
11415 }
11416 else
11417 printf("%s: Unknown WpsConfigMethod\n", __func__);
developere40952c2023-06-15 18:46:43 +080011418
developer37646972023-06-29 10:58:43 +080011419 if (os_snprintf_error(sizeof(config_methods), res)) {
11420 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11421 return RETURN_ERR;
11422 }
11423 }
developera3511852023-06-14 14:12:59 +080011424 params.name = "config_methods";
11425 params.value = config_methods;
developere40952c2023-06-15 18:46:43 +080011426 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11427 if (os_snprintf_error(sizeof(config_file), res)) {
11428 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11429 return RETURN_ERR;
11430 }
developera3511852023-06-14 14:12:59 +080011431 wifi_hostapdWrite(config_file, &params, 1);
11432 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11433 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011434
developera3511852023-06-14 14:12:59 +080011435 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011436}
11437
11438// outputs the pin value, ulong_pin must be allocated by the caller
11439INT wifi_getApWpsDevicePIN(INT apIndex, ULONG *output_ulong)
11440{
developera3511852023-06-14 14:12:59 +080011441 char buf[MAX_BUF_SIZE] = {0};
developer8078acf2023-08-04 18:52:48 +080011442
developere40952c2023-06-15 18:46:43 +080011443 int res;
developer72fb0bb2023-01-11 09:46:29 +080011444
developera3511852023-06-14 14:12:59 +080011445 if(!output_ulong)
11446 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080011447 res = _syscmd_secure(buf, sizeof(buf), "cat %s%d.conf | grep ap_pin | cut -d '=' -f2", CONFIG_PREFIX, apIndex);
11448 if (res) {
11449 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080011450 }
developerd14dff12023-06-28 22:47:44 +080011451 if(strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080011452 if (hal_strtoul(buf, 10, output_ulong) < 0) {
11453 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080011454 }
11455 }
developer72fb0bb2023-01-11 09:46:29 +080011456
developera3511852023-06-14 14:12:59 +080011457 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011458}
11459
11460// set an enviornment variable for the WPS pin for the selected AP. Normally, Device PIN should not be changed.
11461INT wifi_setApWpsDevicePIN(INT apIndex, ULONG pin)
11462{
developera3511852023-06-14 14:12:59 +080011463 //set the pin to wifi config and hostpad config. wait for wifi reset or hostapd reset to apply
11464 char ap_pin[16] = {0};
11465 char config_file[MAX_BUF_SIZE] = {0};
11466 struct params params;
developere40952c2023-06-15 18:46:43 +080011467 int res;
developer72fb0bb2023-01-11 09:46:29 +080011468
developera3511852023-06-14 14:12:59 +080011469 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011470 res = snprintf(ap_pin, sizeof(ap_pin), "%lu", pin);
11471 if (os_snprintf_error(sizeof(ap_pin), res)) {
11472 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11473 return RETURN_ERR;
11474 }
developera3511852023-06-14 14:12:59 +080011475 params.name = "ap_pin";
11476 params.value = ap_pin;
developere40952c2023-06-15 18:46:43 +080011477 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11478 if (os_snprintf_error(sizeof(config_file), res)) {
11479 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11480 return RETURN_ERR;
11481 }
developera3511852023-06-14 14:12:59 +080011482 wifi_hostapdWrite(config_file, &params, 1);
11483 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11484 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011485
developera3511852023-06-14 14:12:59 +080011486 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011487}
11488
11489// Output string is either Not configured or Configured, max 32 characters
11490INT wifi_getApWpsConfigurationState(INT apIndex, CHAR *output_string)
11491{
developera3511852023-06-14 14:12:59 +080011492 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +080011493 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011494 int res;
developer72fb0bb2023-01-11 09:46:29 +080011495
developera3511852023-06-14 14:12:59 +080011496 if(!output_string)
11497 return RETURN_ERR;
11498 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011499 res = snprintf(output_string, 32, "Not configured");
11500 if (os_snprintf_error(32, res)) {
11501 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11502 return RETURN_ERR;
11503 }
developera3511852023-06-14 14:12:59 +080011504 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11505 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080011506 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s get_config | grep wps_state | cut -d'=' -f2", interface_name);
11507 if (res) {
11508 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080011509 }
developer72fb0bb2023-01-11 09:46:29 +080011510
developerc14d83a2023-06-29 20:09:42 +080011511 if(!strncmp(buf, "configured", 10)) {
developere40952c2023-06-15 18:46:43 +080011512 res = snprintf(output_string, 32, "Configured");
developerc14d83a2023-06-29 20:09:42 +080011513 if (os_snprintf_error(32, res)) {
11514 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11515 return RETURN_ERR;
11516 }
11517 }
developera3511852023-06-14 14:12:59 +080011518 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011519
developera3511852023-06-14 14:12:59 +080011520 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011521}
11522
11523// sets the WPS pin for this AP
11524INT wifi_setApWpsEnrolleePin(INT apIndex, CHAR *pin)
11525{
developera3511852023-06-14 14:12:59 +080011526 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +080011527 char buf[MAX_BUF_SIZE]={0};
developer9ce44382023-06-28 11:09:37 +080011528 BOOL enable = 0;
developere40952c2023-06-15 18:46:43 +080011529 int res;
developer72fb0bb2023-01-11 09:46:29 +080011530
developera3511852023-06-14 14:12:59 +080011531 wifi_getApEnable(apIndex, &enable);
11532 if (!enable)
11533 return RETURN_ERR;
11534 wifi_getApWpsEnable(apIndex, &enable);
11535 if (!enable)
11536 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011537
developera3511852023-06-14 14:12:59 +080011538 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11539 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080011540 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i%s wps_pin any %s", interface_name, pin);
11541 if (res) {
11542 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080011543 }
developer8078acf2023-08-04 18:52:48 +080011544
developera3511852023-06-14 14:12:59 +080011545 if((strstr(buf, "OK"))!=NULL)
11546 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011547
developera3511852023-06-14 14:12:59 +080011548 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011549}
11550
11551// This function is called when the WPS push button has been pressed for this AP
11552INT wifi_setApWpsButtonPush(INT apIndex)
11553{
developera3511852023-06-14 14:12:59 +080011554 char buf[MAX_BUF_SIZE]={0};
11555 char interface_name[16] = {0};
11556 BOOL enable=FALSE;
developere40952c2023-06-15 18:46:43 +080011557 int res;
developer72fb0bb2023-01-11 09:46:29 +080011558
developera3511852023-06-14 14:12:59 +080011559 wifi_getApEnable(apIndex, &enable);
11560 if (!enable)
11561 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011562
developera3511852023-06-14 14:12:59 +080011563 wifi_getApWpsEnable(apIndex, &enable);
11564 if (!enable)
11565 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011566
developera3511852023-06-14 14:12:59 +080011567 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11568 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011569
developer8078acf2023-08-04 18:52:48 +080011570 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i%s wps_cancel; hostapd_cli -i%s wps_pbc", interface_name, interface_name);
11571 if (res) {
11572 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080011573 }
developer72fb0bb2023-01-11 09:46:29 +080011574
developera3511852023-06-14 14:12:59 +080011575 if((strstr(buf, "OK"))!=NULL)
11576 return RETURN_OK;
11577 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011578}
11579
11580// cancels WPS mode for this AP
11581INT wifi_cancelApWPS(INT apIndex)
11582{
developera3511852023-06-14 14:12:59 +080011583 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +080011584 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011585 int res;
developer72fb0bb2023-01-11 09:46:29 +080011586
developera3511852023-06-14 14:12:59 +080011587 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11588 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080011589
11590 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i%s wps_cancel", interface_name);
11591 if (res) {
11592 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080011593 }
developer72fb0bb2023-01-11 09:46:29 +080011594
developera3511852023-06-14 14:12:59 +080011595 if((strstr(buf, "OK"))!=NULL)
11596 return RETURN_OK;
11597 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011598}
11599
11600//Device.WiFi.AccessPoint.{i}.AssociatedDevice.*
11601//HAL funciton should allocate an data structure array, and return to caller with "associated_dev_array"
11602INT wifi_getApAssociatedDeviceDiagnosticResult(INT apIndex, wifi_associated_dev_t **associated_dev_array, UINT *output_array_size)
11603{
developera3511852023-06-14 14:12:59 +080011604 char interface_name[16] = {0};
11605 FILE *f = NULL;
11606 int read_flag=0, auth_temp=0, mac_temp=0;
developer8078acf2023-08-04 18:52:48 +080011607 char buf[2048] = {0};
developera3511852023-06-14 14:12:59 +080011608 char *param = NULL, *value = NULL, *line=NULL;
11609 size_t len = 0;
11610 wifi_associated_dev_t *dev=NULL;
11611 int res;
developer72fb0bb2023-01-11 09:46:29 +080011612
developera3511852023-06-14 14:12:59 +080011613 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11614 *associated_dev_array = NULL;
11615 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11616 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080011617 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
11618 if (res) {
11619 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer46506162023-06-12 10:09:39 +080011620 }
developer8078acf2023-08-04 18:52:48 +080011621
developera3511852023-06-14 14:12:59 +080011622 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011623
developera3511852023-06-14 14:12:59 +080011624 if (*output_array_size <= 0)
11625 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011626
developera3511852023-06-14 14:12:59 +080011627 dev=(wifi_associated_dev_t *) calloc (*output_array_size, sizeof(wifi_associated_dev_t));
developere75ba632023-06-29 16:03:33 +080011628 if (!dev) {
11629 wifi_debug(DEBUG_ERROR, "Unexpected calloc fail\n");
11630 return RETURN_ERR;
11631 }
developera3511852023-06-14 14:12:59 +080011632 *associated_dev_array = dev;
developer8078acf2023-08-04 18:52:48 +080011633 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i%s all_sta > /tmp/connected_devices.txt" , interface_name);
11634 if (res) {
11635 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer46506162023-06-12 10:09:39 +080011636 }
developer8078acf2023-08-04 18:52:48 +080011637
developera3511852023-06-14 14:12:59 +080011638 f = fopen("/tmp/connected_devices.txt", "r");
11639 if (f==NULL)
11640 {
11641 *output_array_size=0;
11642 return RETURN_ERR;
11643 }
11644 while ((getline(&line, &len, f)) != -1)
11645 {
11646 param = strtok(line,"=");
developere75ba632023-06-29 16:03:33 +080011647 if (!param)
11648 continue;
developera3511852023-06-14 14:12:59 +080011649 value = strtok(NULL,"=");
developere75ba632023-06-29 16:03:33 +080011650 if (!value)
11651 continue;
developer72fb0bb2023-01-11 09:46:29 +080011652
developera3511852023-06-14 14:12:59 +080011653 if( strcmp("flags",param) == 0 )
11654 {
11655 value[strlen(value)-1]='\0';
11656 if(strstr (value,"AUTHORIZED") != NULL )
11657 {
11658 dev[auth_temp].cli_AuthenticationState = 1;
11659 dev[auth_temp].cli_Active = 1;
11660 auth_temp++;
11661 read_flag=1;
11662 }
11663 }
11664 if(read_flag==1)
11665 {
11666 if( strcmp("dot11RSNAStatsSTAAddress",param) == 0 )
11667 {
11668 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080011669 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
developera3511852023-06-14 14:12:59 +080011670 (unsigned int *)&dev[mac_temp].cli_MACAddress[0],
11671 (unsigned int *)&dev[mac_temp].cli_MACAddress[1],
11672 (unsigned int *)&dev[mac_temp].cli_MACAddress[2],
11673 (unsigned int *)&dev[mac_temp].cli_MACAddress[3],
11674 (unsigned int *)&dev[mac_temp].cli_MACAddress[4],
developere75ba632023-06-29 16:03:33 +080011675 (unsigned int *)&dev[mac_temp].cli_MACAddress[5] ) == EOF)
11676 continue;
developera3511852023-06-14 14:12:59 +080011677 mac_temp++;
11678 read_flag=0;
11679 }
11680 }
11681 }
11682 *output_array_size = auth_temp;
11683 auth_temp=0;
11684 mac_temp=0;
11685 free(line);
developere75ba632023-06-29 16:03:33 +080011686 if (fclose(f) == EOF) {
11687 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
11688 return RETURN_ERR;
11689 }
developera3511852023-06-14 14:12:59 +080011690 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11691 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011692}
11693
11694#define MACADDRESS_SIZE 6
11695
11696INT wifihal_AssociatedDevicesstats3(INT apIndex,CHAR *interface_name,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
11697{
developera3511852023-06-14 14:12:59 +080011698 FILE *fp = NULL;
11699 char str[MAX_BUF_SIZE] = {0};
11700 int wificlientindex = 0 ;
11701 int count = 0;
11702 int signalstrength = 0;
11703 int arr[MACADDRESS_SIZE] = {0};
11704 unsigned char mac[MACADDRESS_SIZE] = {0};
11705 UINT wifi_count = 0;
developere40952c2023-06-15 18:46:43 +080011706 int res;
developerc14d83a2023-06-29 20:09:42 +080011707 wifi_associated_dev3_t* temp = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011708
developera3511852023-06-14 14:12:59 +080011709 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11710 *output_array_size = 0;
11711 *associated_dev_array = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011712
developer33f13ba2023-07-12 16:19:06 +080011713 fp = v_secure_popen("r", "iw dev %s station dump | grep %s | wc -l",
11714 interface_name, interface_name);
developera3511852023-06-14 14:12:59 +080011715 if (fp == NULL)
11716 {
11717 printf("Failed to run command inside function %s\n",__FUNCTION__ );
11718 return RETURN_ERR;
11719 }
developer72fb0bb2023-01-11 09:46:29 +080011720
developera3511852023-06-14 14:12:59 +080011721 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080011722 if (fgets(str, sizeof(str)-1, fp) == NULL) {
11723 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080011724 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011725 return RETURN_ERR;
11726 }
developera3511852023-06-14 14:12:59 +080011727 wifi_count = (unsigned int) atoi ( str );
11728 *output_array_size = wifi_count;
11729 printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
developer33f13ba2023-07-12 16:19:06 +080011730 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080011731
developera3511852023-06-14 14:12:59 +080011732 if(wifi_count == 0)
11733 {
11734 return RETURN_OK;
11735 }
11736 else
11737 {
developer9ce44382023-06-28 11:09:37 +080011738 if(wifi_count <= 0 || wifi_count > MAX_ASSOCIATED_STA_NUM){
11739 return RETURN_ERR;
11740 }
developera3511852023-06-14 14:12:59 +080011741 temp = (wifi_associated_dev3_t*)calloc(1, sizeof(wifi_associated_dev3_t)*wifi_count) ;
11742 if(temp == NULL)
11743 {
11744 printf("Error Statement. Insufficient memory \n");
11745 return RETURN_ERR;
11746 }
developer72fb0bb2023-01-11 09:46:29 +080011747
developer33f13ba2023-07-12 16:19:06 +080011748 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
11749 if (res) {
11750 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011751 free(temp);
developere40952c2023-06-15 18:46:43 +080011752 return RETURN_ERR;
11753 }
developer33f13ba2023-07-12 16:19:06 +080011754
developera3511852023-06-14 14:12:59 +080011755 if(apIndex == 0)
developer33f13ba2023-07-12 16:19:06 +080011756 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_2G.txt", interface_name);
developera3511852023-06-14 14:12:59 +080011757 else if(apIndex == 1)
developer33f13ba2023-07-12 16:19:06 +080011758 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", interface_name);
11759 if (res) {
11760 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011761 free(temp);
developere40952c2023-06-15 18:46:43 +080011762 return RETURN_ERR;
11763 }
developer72fb0bb2023-01-11 09:46:29 +080011764
developera3511852023-06-14 14:12:59 +080011765 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
11766 if(fp == NULL)
11767 {
11768 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
11769 free(temp);
11770 return RETURN_ERR;
11771 }
developere75ba632023-06-29 16:03:33 +080011772 if (fclose(fp) == EOF) {
11773 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developerc14d83a2023-06-29 20:09:42 +080011774 free(temp);
developere75ba632023-06-29 16:03:33 +080011775 return RETURN_ERR;
11776 }
developer72fb0bb2023-01-11 09:46:29 +080011777
developer33f13ba2023-07-12 16:19:06 +080011778 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
developera3511852023-06-14 14:12:59 +080011779 if(fp)
11780 {
11781 for(count =0 ; count < wifi_count; count++)
11782 {
developer86035662023-06-28 19:21:12 +080011783 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11784 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011785 goto err;
developer86035662023-06-28 19:21:12 +080011786 }
developera3511852023-06-14 14:12:59 +080011787 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
11788 {
11789 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
11790 {
11791 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080011792
developera3511852023-06-14 14:12:59 +080011793 }
11794 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
11795 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]);
11796 }
11797 temp[count].cli_AuthenticationState = 1; //TODO
11798 temp[count].cli_Active = 1; //TODO
11799 }
developer33f13ba2023-07-12 16:19:06 +080011800 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011801 }
11802
developer33f13ba2023-07-12 16:19:06 +080011803 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 +080011804 if(fp)
11805 {
developer33f13ba2023-07-12 16:19:06 +080011806 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011807 }
developer33f13ba2023-07-12 16:19:06 +080011808 fp = v_secure_popen("r", "cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011809 if(fp)
11810 {
11811 for(count =0 ; count < wifi_count ;count++)
11812 {
developer86035662023-06-28 19:21:12 +080011813 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11814 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011815 goto err;
developer86035662023-06-28 19:21:12 +080011816 }
developera3511852023-06-14 14:12:59 +080011817 signalstrength = atoi(str);
11818 temp[count].cli_SignalStrength = signalstrength;
11819 temp[count].cli_RSSI = signalstrength;
11820 temp[count].cli_SNR = signalstrength + 95;
11821 }
developer33f13ba2023-07-12 16:19:06 +080011822 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011823 }
developer72fb0bb2023-01-11 09:46:29 +080011824
11825
developera3511852023-06-14 14:12:59 +080011826 if((apIndex == 0) || (apIndex == 4))
11827 {
11828 for(count =0 ; count < wifi_count ;count++)
11829 {
developer32f2a182023-06-27 19:50:41 +080011830 memcpy(temp[count].cli_OperatingStandard,"g", 1);
11831 temp[count].cli_OperatingStandard[1] = '\0';
11832 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
11833 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080011834 }
developer72fb0bb2023-01-11 09:46:29 +080011835
developera3511852023-06-14 14:12:59 +080011836 //BytesSent
developer33f13ba2023-07-12 16:19:06 +080011837 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 +080011838 if(fp)
11839 {
developer33f13ba2023-07-12 16:19:06 +080011840 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011841 }
developer33f13ba2023-07-12 16:19:06 +080011842 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011843 if(fp)
11844 {
11845 for (count = 0; count < wifi_count; count++)
11846 {
developer86035662023-06-28 19:21:12 +080011847 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11848 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080011849 v_secure_pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080011850 free(temp);
developer86035662023-06-28 19:21:12 +080011851 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080011852 }
developerc14d83a2023-06-29 20:09:42 +080011853 if (hal_strtoul(str, 10, &(temp[count].cli_BytesSent)) < 0) {
11854 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011855 }
developera3511852023-06-14 14:12:59 +080011856 }
developer33f13ba2023-07-12 16:19:06 +080011857 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011858 }
developer72fb0bb2023-01-11 09:46:29 +080011859
developera3511852023-06-14 14:12:59 +080011860 //BytesReceived
developer86035662023-06-28 19:21:12 +080011861
developer33f13ba2023-07-12 16:19:06 +080011862 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 +080011863 if (fp)
11864 {
developer33f13ba2023-07-12 16:19:06 +080011865 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011866 }
developer33f13ba2023-07-12 16:19:06 +080011867 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011868 if (fp)
11869 {
11870 for (count = 0; count < wifi_count; count++)
11871 {
developer86035662023-06-28 19:21:12 +080011872 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11873 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011874 goto err;
11875 }
11876 if (hal_strtoul(str, 10, &(temp[count].cli_BytesReceived)) < 0) {
11877 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011878 }
developera3511852023-06-14 14:12:59 +080011879 }
developer33f13ba2023-07-12 16:19:06 +080011880 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011881 }
developer72fb0bb2023-01-11 09:46:29 +080011882
developera3511852023-06-14 14:12:59 +080011883 //PacketsSent
developer33f13ba2023-07-12 16:19:06 +080011884 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 +080011885 if (fp)
11886 {
developer33f13ba2023-07-12 16:19:06 +080011887 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011888 }
developer72fb0bb2023-01-11 09:46:29 +080011889
developer33f13ba2023-07-12 16:19:06 +080011890 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011891 if (fp)
11892 {
11893 for (count = 0; count < wifi_count; count++)
11894 {
developer86035662023-06-28 19:21:12 +080011895 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11896 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011897 goto err;
developer5b23cd02023-07-19 20:26:03 +080011898 }
developerc14d83a2023-06-29 20:09:42 +080011899 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsSent)) < 0) {
11900 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011901 }
developera3511852023-06-14 14:12:59 +080011902 }
developer33f13ba2023-07-12 16:19:06 +080011903 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011904 }
developer72fb0bb2023-01-11 09:46:29 +080011905
developera3511852023-06-14 14:12:59 +080011906 //PacketsReceived
developer33f13ba2023-07-12 16:19:06 +080011907 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 +080011908 if (fp)
11909 {
developer33f13ba2023-07-12 16:19:06 +080011910 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011911 }
developer33f13ba2023-07-12 16:19:06 +080011912 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011913 if (fp)
11914 {
11915 for (count = 0; count < wifi_count; count++)
11916 {
developer86035662023-06-28 19:21:12 +080011917 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11918 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011919 goto err;
developer5b23cd02023-07-19 20:26:03 +080011920 }
developerc14d83a2023-06-29 20:09:42 +080011921 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsReceived)) < 0) {
11922 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011923 }
developera3511852023-06-14 14:12:59 +080011924 }
developer33f13ba2023-07-12 16:19:06 +080011925 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011926 }
developer72fb0bb2023-01-11 09:46:29 +080011927
developera3511852023-06-14 14:12:59 +080011928 //ErrorsSent
developer33f13ba2023-07-12 16:19:06 +080011929 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 +080011930 if (fp)
11931 {
developer33f13ba2023-07-12 16:19:06 +080011932 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011933 }
developer33f13ba2023-07-12 16:19:06 +080011934 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011935 if (fp)
11936 {
11937 for (count = 0; count < wifi_count; count++)
11938 {
developer86035662023-06-28 19:21:12 +080011939 if (fgets(str, MAX_BUF_SIZE, fp) == NULL){
11940 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011941 goto err;
developer5b23cd02023-07-19 20:26:03 +080011942 }
developerc14d83a2023-06-29 20:09:42 +080011943 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
11944 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011945 }
developera3511852023-06-14 14:12:59 +080011946 }
developer33f13ba2023-07-12 16:19:06 +080011947 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011948 }
developer72fb0bb2023-01-11 09:46:29 +080011949
developera3511852023-06-14 14:12:59 +080011950 //ErrorsSent
developer86035662023-06-28 19:21:12 +080011951
developer33f13ba2023-07-12 16:19:06 +080011952 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 +080011953 if (fp)
11954 {
developer33f13ba2023-07-12 16:19:06 +080011955 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011956 }
developer33f13ba2023-07-12 16:19:06 +080011957 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011958 if (fp)
11959 {
11960 for (count = 0; count < wifi_count; count++)
11961 {
developer86035662023-06-28 19:21:12 +080011962 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11963 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011964 goto err;
developer5b23cd02023-07-19 20:26:03 +080011965 }
developerc14d83a2023-06-29 20:09:42 +080011966 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
11967 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011968 }
developera3511852023-06-14 14:12:59 +080011969 }
developer33f13ba2023-07-12 16:19:06 +080011970 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011971 }
developer72fb0bb2023-01-11 09:46:29 +080011972
developera3511852023-06-14 14:12:59 +080011973 //LastDataDownlinkRate
developer33f13ba2023-07-12 16:19:06 +080011974 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 +080011975 if (fp)
11976 {
developer33f13ba2023-07-12 16:19:06 +080011977 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011978 }
developer33f13ba2023-07-12 16:19:06 +080011979 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011980 if (fp)
developer5b23cd02023-07-19 20:26:03 +080011981 {
developerc14d83a2023-06-29 20:09:42 +080011982 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080011983 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;
11988 }
developer5b23cd02023-07-19 20:26:03 +080011989
developerc14d83a2023-06-29 20:09:42 +080011990 if (hal_strtoul(str, 10, &tmp_u) < 0) {
11991 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011992 }
developerc14d83a2023-06-29 20:09:42 +080011993 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080011994 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
11995 }
developer33f13ba2023-07-12 16:19:06 +080011996 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011997 }
developer72fb0bb2023-01-11 09:46:29 +080011998
developera3511852023-06-14 14:12:59 +080011999 //LastDataUplinkRate
developer33f13ba2023-07-12 16:19:06 +080012000 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 +080012001 if (fp)
12002 {
developer33f13ba2023-07-12 16:19:06 +080012003 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012004 }
developer33f13ba2023-07-12 16:19:06 +080012005 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012006 if (fp)
12007 {
developerc14d83a2023-06-29 20:09:42 +080012008 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012009 for (count = 0; count < wifi_count; count++)
12010 {
developer86035662023-06-28 19:21:12 +080012011 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12012 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012013 goto err;
12014 }
12015 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12016 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012017 }
developerc14d83a2023-06-29 20:09:42 +080012018 temp[count].cli_LastDataUplinkRate = tmp_u;
12019
developera3511852023-06-14 14:12:59 +080012020 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
12021 }
developer33f13ba2023-07-12 16:19:06 +080012022 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012023 }
developer72fb0bb2023-01-11 09:46:29 +080012024
developera3511852023-06-14 14:12:59 +080012025 }
12026 else if ((apIndex == 1) || (apIndex == 5))
12027 {
12028 for (count = 0; count < wifi_count; count++)
12029 {
developer32f2a182023-06-27 19:50:41 +080012030 memcpy(temp[count].cli_OperatingStandard, "a", 1);
12031 temp[count].cli_OperatingStandard[1] = '\0';
12032 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
12033 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080012034 temp[count].cli_BytesSent = 0;
12035 temp[count].cli_BytesReceived = 0;
12036 temp[count].cli_LastDataUplinkRate = 0;
12037 temp[count].cli_LastDataDownlinkRate = 0;
12038 temp[count].cli_PacketsSent = 0;
12039 temp[count].cli_PacketsReceived = 0;
12040 temp[count].cli_ErrorsSent = 0;
12041 }
12042 }
developer72fb0bb2023-01-11 09:46:29 +080012043
developera3511852023-06-14 14:12:59 +080012044 for (count = 0; count < wifi_count; count++)
12045 {
12046 temp[count].cli_Retransmissions = 0;
12047 temp[count].cli_DataFramesSentAck = 0;
12048 temp[count].cli_DataFramesSentNoAck = 0;
12049 temp[count].cli_MinRSSI = 0;
12050 temp[count].cli_MaxRSSI = 0;
12051 strncpy(temp[count].cli_InterferenceSources, "", 64);
12052 memset(temp[count].cli_IPAddress, 0, 64);
12053 temp[count].cli_RetransCount = 0;
12054 temp[count].cli_FailedRetransCount = 0;
12055 temp[count].cli_RetryCount = 0;
12056 temp[count].cli_MultipleRetryCount = 0;
12057 }
12058 *associated_dev_array = temp;
12059 }
12060 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12061 return RETURN_OK;
developerc14d83a2023-06-29 20:09:42 +080012062err:
12063 if (temp)
12064 free(temp);
developer8078acf2023-08-04 18:52:48 +080012065 v_secure_pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080012066 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012067}
12068
developer7e4a2a62023-04-06 19:56:03 +080012069int wifihal_interfacestatus(CHAR *wifi_status, CHAR *interface_name)
developer72fb0bb2023-01-11 09:46:29 +080012070{
developer7e4a2a62023-04-06 19:56:03 +080012071 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080012072 int res;
developer32f2a182023-06-27 19:50:41 +080012073 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012074
developera3511852023-06-14 14:12:59 +080012075 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080012076
developer8078acf2023-08-04 18:52:48 +080012077 res = _syscmd_secure(buf, MAX_BUF_SIZE, "ifconfig %s | grep RUNNING | tr -s ' ' | cut -d ' ' -f4 | tr -d '\\n'",
developer7e4a2a62023-04-06 19:56:03 +080012078 interface_name);
developer8078acf2023-08-04 18:52:48 +080012079 if (res) {
12080 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080012081 }
12082
developer32f2a182023-06-27 19:50:41 +080012083 len = strlen(buf);
12084 if (len >= sizeof(buf)) {
12085 wifi_debug(DEBUG_ERROR, "Unexpected buf size\n");
12086 return RETURN_ERR;
12087 }
12088 strncpy(wifi_status, buf, len); /* TBD: check wifi_status mem lenth and replace with strcpy later */
12089 wifi_status[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012090
developera3511852023-06-14 14:12:59 +080012091 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12092 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012093}
12094
developer72fb0bb2023-01-11 09:46:29 +080012095static const char *get_line_from_str_buf(const char *buf, char *line)
12096{
developera3511852023-06-14 14:12:59 +080012097 int i;
12098 int n = strlen(buf);
developer72fb0bb2023-01-11 09:46:29 +080012099
developera3511852023-06-14 14:12:59 +080012100 for (i = 0; i < n; i++) {
12101 line[i] = buf[i];
12102 if (buf[i] == '\n') {
12103 line[i] = '\0';
12104 return &buf[i + 1];
12105 }
12106 }
developer72fb0bb2023-01-11 09:46:29 +080012107
developera3511852023-06-14 14:12:59 +080012108 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080012109}
12110
12111INT wifi_getApAssociatedDeviceDiagnosticResult3(INT apIndex, wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12112{
developera3511852023-06-14 14:12:59 +080012113 char interface_name[16] = {0};
12114 FILE *f = NULL;
12115 int auth_temp= -1;
developer8078acf2023-08-04 18:52:48 +080012116 char buf[2048] = {0};
developera3511852023-06-14 14:12:59 +080012117 char *param = NULL, *value = NULL, *line=NULL;
12118 size_t len = 0;
12119 wifi_associated_dev3_t *dev=NULL;
developer75bd10c2023-06-27 11:34:08 +080012120 int res;
developer72fb0bb2023-01-11 09:46:29 +080012121
developera3511852023-06-14 14:12:59 +080012122 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12123 *associated_dev_array = NULL;
12124 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12125 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080012126
developer8078acf2023-08-04 18:52:48 +080012127 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
12128 if (res) {
12129 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080012130 }
12131
developera3511852023-06-14 14:12:59 +080012132 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080012133
developera3511852023-06-14 14:12:59 +080012134 if (*output_array_size <= 0)
12135 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012136
developera3511852023-06-14 14:12:59 +080012137 dev=(wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
developer86035662023-06-28 19:21:12 +080012138
12139 if (dev == NULL) {
12140 wifi_debug(DEBUG_ERROR, "calloc fail\n");
12141 return RETURN_ERR;
12142 }
developera3511852023-06-14 14:12:59 +080012143 *associated_dev_array = dev;
developer8078acf2023-08-04 18:52:48 +080012144 res = _syscmd_secure(buf, sizeof(buf),
developer32f2a182023-06-27 19:50:41 +080012145 "hostapd_cli -i%s all_sta > /tmp/diagnostic3_devices.txt" , interface_name);
developer8078acf2023-08-04 18:52:48 +080012146 if (res) {
12147 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developer75bd10c2023-06-27 11:34:08 +080012148 }
12149
developera3511852023-06-14 14:12:59 +080012150 f = fopen("/tmp/diagnostic3_devices.txt", "r");
12151 if (f == NULL)
12152 {
12153 *output_array_size=0;
12154 return RETURN_ERR;
12155 }
12156 while ((getline(&line, &len, f)) != -1)
12157 {
12158 param = strtok(line, "=");
developere75ba632023-06-29 16:03:33 +080012159 if (!param)
12160 continue;
developera3511852023-06-14 14:12:59 +080012161 value = strtok(NULL, "=");
developere75ba632023-06-29 16:03:33 +080012162 if (!value)
12163 continue;
developer72fb0bb2023-01-11 09:46:29 +080012164
developera3511852023-06-14 14:12:59 +080012165 if( strcmp("flags",param) == 0 )
12166 {
12167 value[strlen(value)-1]='\0';
12168 if(strstr (value,"AUTHORIZED") != NULL )
12169 {
12170 auth_temp++;
12171 dev[auth_temp].cli_AuthenticationState = 1;
12172 dev[auth_temp].cli_Active = 1;
12173 }
12174 } else if (auth_temp < 0) {
12175 continue;
12176 } else if( strcmp("dot11RSNAStatsSTAAddress", param) == 0 )
12177 {
12178 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012179 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
12180 (unsigned int *)&dev[auth_temp].cli_MACAddress[0],
12181 (unsigned int *)&dev[auth_temp].cli_MACAddress[1],
12182 (unsigned int *)&dev[auth_temp].cli_MACAddress[2],
12183 (unsigned int *)&dev[auth_temp].cli_MACAddress[3],
12184 (unsigned int *)&dev[auth_temp].cli_MACAddress[4],
12185 (unsigned int *)&dev[auth_temp].cli_MACAddress[5]) == EOF)
12186 continue;
developera3511852023-06-14 14:12:59 +080012187 } else if (strcmp("signal", param) == 0) {
12188 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012189 if (sscanf(value, "%d", &dev[auth_temp].cli_RSSI) == EOF)
12190 continue;
developera3511852023-06-14 14:12:59 +080012191 dev[auth_temp].cli_SNR = 95 + dev[auth_temp].cli_RSSI;
12192 }
12193 }
developer0d26f2c2023-05-25 19:46:36 +080012194 if (line)
developera3511852023-06-14 14:12:59 +080012195 free(line);
developerc14d83a2023-06-29 20:09:42 +080012196
12197 if (fclose(f) != 0) {
12198 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12199 }
12200
developera3511852023-06-14 14:12:59 +080012201 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12202 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012203}
developer72fb0bb2023-01-11 09:46:29 +080012204
12205/* getIPAddress function */
12206/**
12207* @description Returning IpAddress of the Matched String
12208*
developer69b61b02023-03-07 17:17:44 +080012209* @param
developer72fb0bb2023-01-11 09:46:29 +080012210* @str Having MacAddress
developer69b61b02023-03-07 17:17:44 +080012211* @ipaddr Having ipaddr
developer72fb0bb2023-01-11 09:46:29 +080012212* @return The status of the operation
12213* @retval RETURN_OK if successful
12214* @retval RETURN_ERR if any error is detected
12215*
12216*/
12217
12218INT getIPAddress(char *str,char *ipaddr)
12219{
developera3511852023-06-14 14:12:59 +080012220 FILE *fp = NULL;
12221 char buf[1024] = {0},ipAddr[50] = {0},phyAddr[100] = {0},hostName[100] = {0};
12222 int LeaseTime = 0,ret = 0;
developer32f2a182023-06-27 19:50:41 +080012223 unsigned long len;
12224
developera3511852023-06-14 14:12:59 +080012225 if ( (fp=fopen("/nvram/dnsmasq.leases", "r")) == NULL )
12226 {
12227 return RETURN_ERR;
12228 }
developer72fb0bb2023-01-11 09:46:29 +080012229
developera3511852023-06-14 14:12:59 +080012230 while ( fgets(buf, sizeof(buf), fp)!= NULL )
12231 {
12232 /*
12233 Sample:sss
12234 1560336751 00:cd:fe:f3:25:e6 10.0.0.153 NallamousiPhone 01:00:cd:fe:f3:25:e6
12235 1560336751 12:34:56:78:9a:bc 10.0.0.154 NallamousiPhone 01:00:cd:fe:f3:25:e6
12236 */
12237 ret = sscanf(buf, LM_DHCP_CLIENT_FORMAT,
12238 &(LeaseTime),
12239 phyAddr,
12240 ipAddr,
12241 hostName
12242 );
12243 if(ret != 4)
12244 continue;
developer32f2a182023-06-27 19:50:41 +080012245 if (strcmp(str,phyAddr) == 0) {
12246 len = strlen(ipAddr);
12247 strncpy(ipaddr, ipAddr, len);
12248 ipaddr[len] = '\0';
12249 }
developera3511852023-06-14 14:12:59 +080012250 }
developer37646972023-06-29 10:58:43 +080012251 if (fclose(fp) == EOF) {
12252 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12253 return RETURN_ERR;
12254 }
developera3511852023-06-14 14:12:59 +080012255 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012256}
12257
12258/* wifi_getApInactiveAssociatedDeviceDiagnosticResult function */
12259/**
12260* @description Returning Inactive wireless connected clients informations
12261*
developer69b61b02023-03-07 17:17:44 +080012262* @param
developer72fb0bb2023-01-11 09:46:29 +080012263* @filename Holding private_wifi 2g/5g content files
12264* @associated_dev_array Having inactiv wireless clients informations
12265* @output_array_size Returning Inactive wireless counts
12266* @return The status of the operation
12267* @retval RETURN_OK if successful
12268* @retval RETURN_ERR if any error is detected
12269*
12270*/
12271
12272INT wifi_getApInactiveAssociatedDeviceDiagnosticResult(char *filename,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12273{
developera3511852023-06-14 14:12:59 +080012274 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12275 int count = 0,maccount = 0,i = 0,wificlientindex = 0;
12276 FILE *fp = NULL;
12277 int arr[MACADDRESS_SIZE] = {0};
12278 unsigned char mac[MACADDRESS_SIZE] = {0};
12279 char path[1024] = {0},str[1024] = {0},ipaddr[50] = {0},buf[512] = {0};
developer86035662023-06-28 19:21:12 +080012280 int res;
12281
developer8078acf2023-08-04 18:52:48 +080012282
12283
12284
12285
12286
12287 fp = v_secure_popen("r","cat %s | grep Station | sort | uniq | wc -l",filename);
developera3511852023-06-14 14:12:59 +080012288 if(fp == NULL)
12289 return RETURN_ERR;
12290 else
12291 {
developerd14dff12023-06-28 22:47:44 +080012292 if (fgets(path,sizeof(path),fp) == NULL) {
12293 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080012294 v_secure_pclose(fp);
developerd14dff12023-06-28 22:47:44 +080012295 return RETURN_ERR;
12296 }
developera3511852023-06-14 14:12:59 +080012297 maccount = atoi(path);
12298 }
developer8078acf2023-08-04 18:52:48 +080012299 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012300 *output_array_size = maccount;
12301 wifi_associated_dev3_t* temp = NULL;
developer9ce44382023-06-28 11:09:37 +080012302 if(*output_array_size > 0 && *output_array_size < MAX_ASSOCIATED_STA_NUM){
12303 temp = (wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
12304 } else {
12305 return RETURN_ERR;
12306 }
developer37646972023-06-29 10:58:43 +080012307
developera3511852023-06-14 14:12:59 +080012308 *associated_dev_array = temp;
12309 if(temp == NULL)
12310 {
12311 printf("Error Statement. Insufficient memory \n");
12312 return RETURN_ERR;
12313 }
12314 memset(buf,0,sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080012315 res = snprintf(buf, sizeof(buf),
12316 "cat %s | grep Station | cut -d ' ' -f2 | sort | uniq",filename);
12317 if (os_snprintf_error(sizeof(buf), res)) {
12318 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12319 return RETURN_ERR;
12320 }
developer8078acf2023-08-04 18:52:48 +080012321 fp = v_secure_popen("r", "cat %s | grep Station | cut -d ' ' -f2 | sort | uniq",filename);
developera3511852023-06-14 14:12:59 +080012322 if (fp == NULL) {
developer37646972023-06-29 10:58:43 +080012323 res = fprintf(stderr, "%s: failed pipe command %s.\n", __func__, buf);
12324 if (res < 0) {
12325 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12326 }
developera3511852023-06-14 14:12:59 +080012327 return RETURN_ERR;
12328 }
12329 for(count = 0; count < maccount ; count++)
12330 {
developer37646972023-06-29 10:58:43 +080012331 if (fgets(path,sizeof(path),fp) == NULL)
12332 continue;
developera3511852023-06-14 14:12:59 +080012333 for(i = 0; path[i]!='\n';i++)
12334 str[i]=path[i];
12335 str[i]='\0';
12336 getIPAddress(str,ipaddr);
developera3511852023-06-14 14:12:59 +080012337 if(strlen(ipaddr) > 0)
12338 {
developer33f13ba2023-07-12 16:19:06 +080012339 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 +080012340 {
12341 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12342 {
12343 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12344 {
12345 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012346
developera3511852023-06-14 14:12:59 +080012347 }
12348 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer37646972023-06-29 10:58:43 +080012349 if (fprintf(stderr,"%sMAC %d = %X:%X:%X:%X:%X:%X \n", __FUNCTION__,count, temp[count].cli_MACAddress[0],
12350 temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2],
12351 temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4],
12352 temp[count].cli_MACAddress[5]) < 0) {
12353 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12354 break;
12355 }
developera3511852023-06-14 14:12:59 +080012356 }
12357 temp[count].cli_AuthenticationState = 0; //TODO
12358 temp[count].cli_Active = 0; //TODO
12359 temp[count].cli_SignalStrength = 0;
12360 }
12361 else //Active wireless clients info
12362 {
12363 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12364 {
12365 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12366 {
12367 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012368
developera3511852023-06-14 14:12:59 +080012369 }
12370 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer12fb9f62023-06-30 15:26:27 +080012371 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 +080012372 }
12373 temp[count].cli_Active = 1;
12374 }
12375 }
12376 memset(ipaddr,0,sizeof(ipaddr));
12377 }
developer8078acf2023-08-04 18:52:48 +080012378 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012379 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12380 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012381}
12382//Device.WiFi.X_RDKCENTRAL-COM_BandSteering object
12383//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Capability bool r/o
12384//To get Band Steering Capability
12385INT wifi_getBandSteeringCapability(BOOL *support)
12386{
developera3511852023-06-14 14:12:59 +080012387 *support = FALSE;
12388 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012389}
12390
12391
12392//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Enable bool r/w
12393//To get Band Steering enable status
12394INT wifi_getBandSteeringEnable(BOOL *enable)
12395{
developera3511852023-06-14 14:12:59 +080012396 *enable = FALSE;
12397 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012398}
12399
12400//To turn on/off Band steering
12401INT wifi_setBandSteeringEnable(BOOL enable)
12402{
developera3511852023-06-14 14:12:59 +080012403 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012404}
12405
12406//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.APGroup string r/w
12407//To get Band Steering AP group
12408INT wifi_getBandSteeringApGroup(char *output_ApGroup)
12409{
developera3511852023-06-14 14:12:59 +080012410 if (NULL == output_ApGroup)
12411 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012412
developer32f2a182023-06-27 19:50:41 +080012413 memcpy(output_ApGroup, "1,2", 3);
12414 output_ApGroup[3] = '\0';
developera3511852023-06-14 14:12:59 +080012415 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012416}
12417
12418//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.UtilizationThreshold int r/w
12419//to set and read the band steering BandUtilizationThreshold parameters
12420INT wifi_getBandSteeringBandUtilizationThreshold (INT radioIndex, INT *pBuThreshold)
12421{
developera3511852023-06-14 14:12:59 +080012422 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012423}
12424
12425INT wifi_setBandSteeringBandUtilizationThreshold (INT radioIndex, INT buThreshold)
12426{
developera3511852023-06-14 14:12:59 +080012427 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012428}
12429
12430//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.RSSIThreshold int r/w
12431//to set and read the band steering RSSIThreshold parameters
12432INT wifi_getBandSteeringRSSIThreshold (INT radioIndex, INT *pRssiThreshold)
12433{
developera3511852023-06-14 14:12:59 +080012434 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012435}
12436
12437INT wifi_setBandSteeringRSSIThreshold (INT radioIndex, INT rssiThreshold)
12438{
developera3511852023-06-14 14:12:59 +080012439 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012440}
12441
12442
12443//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.PhyRateThreshold int r/w
12444//to set and read the band steering physical modulation rate threshold parameters
12445INT wifi_getBandSteeringPhyRateThreshold (INT radioIndex, INT *pPrThreshold)
12446{
developera3511852023-06-14 14:12:59 +080012447 //If chip is not support, return -1
12448 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012449}
12450
12451INT wifi_setBandSteeringPhyRateThreshold (INT radioIndex, INT prThreshold)
12452{
developera3511852023-06-14 14:12:59 +080012453 //If chip is not support, return -1
12454 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012455}
12456
12457//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.OverloadInactiveTime int r/w
12458//to set and read the inactivity time (in seconds) for steering under overload condition
12459INT wifi_getBandSteeringOverloadInactiveTime(INT radioIndex, INT *pPrThreshold)
12460{
developera3511852023-06-14 14:12:59 +080012461 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012462}
12463
12464INT wifi_setBandSteeringOverloadInactiveTime(INT radioIndex, INT prThreshold)
12465{
developera3511852023-06-14 14:12:59 +080012466 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012467}
12468
12469//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.IdleInactiveTime int r/w
12470//to set and read the inactivity time (in seconds) for steering under Idle condition
12471INT wifi_getBandSteeringIdleInactiveTime(INT radioIndex, INT *pPrThreshold)
12472{
developera3511852023-06-14 14:12:59 +080012473 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012474}
12475
12476INT wifi_setBandSteeringIdleInactiveTime(INT radioIndex, INT prThreshold)
12477{
developera3511852023-06-14 14:12:59 +080012478 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012479}
12480
12481//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.History string r/o
12482//pClientMAC[64]
12483//pSourceSSIDIndex[64]
12484//pDestSSIDIndex[64]
12485//pSteeringReason[256]
12486INT wifi_getBandSteeringLog(INT record_index, ULONG *pSteeringTime, CHAR *pClientMAC, INT *pSourceSSIDIndex, INT *pDestSSIDIndex, INT *pSteeringReason)
12487{
developera3511852023-06-14 14:12:59 +080012488 //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 +080012489 long int tim_tmp = time(NULL);
12490 if (tim_tmp < 0)
12491 return RETURN_ERR;
12492 *pSteeringTime = tim_tmp;
developera3511852023-06-14 14:12:59 +080012493 *pSteeringReason = 0; //TODO: need to assign correct steering reason (INT numeric, i suppose)
12494 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012495}
12496
12497INT wifi_ifConfigDown(INT apIndex)
12498{
developera3511852023-06-14 14:12:59 +080012499 INT status = RETURN_OK;
12500 char cmd[64];
developere40952c2023-06-15 18:46:43 +080012501 int res;
developer72fb0bb2023-01-11 09:46:29 +080012502
developer33f13ba2023-07-12 16:19:06 +080012503 res = v_secure_system("ifconfig ath%d down", apIndex);
12504 if (res) {
12505 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080012506 return RETURN_ERR;
12507 }
developera3511852023-06-14 14:12:59 +080012508 printf("%s: %s\n", __func__, cmd);
developer72fb0bb2023-01-11 09:46:29 +080012509
developera3511852023-06-14 14:12:59 +080012510 return status;
developer72fb0bb2023-01-11 09:46:29 +080012511}
12512
12513INT wifi_ifConfigUp(INT apIndex)
12514{
developera3511852023-06-14 14:12:59 +080012515 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +080012516 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012517 int res;
developer72fb0bb2023-01-11 09:46:29 +080012518
developera3511852023-06-14 14:12:59 +080012519 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12520 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080012521 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s up 2>/dev/null", interface_name);
12522 if (res) {
12523 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080012524 }
developera3511852023-06-14 14:12:59 +080012525 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012526}
12527
12528//>> Deprecated. Replace with wifi_applyRadioSettings
12529INT wifi_pushBridgeInfo(INT apIndex)
12530{
developerb2977562023-05-24 17:54:12 +080012531 char ip[32] = {0};
12532 char subnet[32] = {0};
12533 char bridge[32] = {0};
developerb2977562023-05-24 17:54:12 +080012534 char buf[1024] = {0};
developere40952c2023-06-15 18:46:43 +080012535 int res;
developer72fb0bb2023-01-11 09:46:29 +080012536
developerb2977562023-05-24 17:54:12 +080012537 wifi_getApBridgeInfo(apIndex, bridge, ip, subnet);
developer72fb0bb2023-01-11 09:46:29 +080012538
developer8078acf2023-08-04 18:52:48 +080012539 res = _syscmd_secure(buf, sizeof(buf), "ifconfig %s %s netmask %s ", bridge, ip, subnet);
12540 if (res) {
12541 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080012542 }
developer72fb0bb2023-01-11 09:46:29 +080012543
developerb2977562023-05-24 17:54:12 +080012544 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012545}
12546
12547INT wifi_pushChannel(INT radioIndex, UINT channel)
12548{
developera3511852023-06-14 14:12:59 +080012549 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +080012550 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012551 int res;
developer72fb0bb2023-01-11 09:46:29 +080012552
developera3511852023-06-14 14:12:59 +080012553 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
12554 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080012555 res = _syscmd_secure(buf, sizeof(buf), "iwconfig %s freq %d",interface_name,channel);
12556 if (res) {
12557 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080012558 }
developer72fb0bb2023-01-11 09:46:29 +080012559
developera3511852023-06-14 14:12:59 +080012560 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012561}
12562
12563INT wifi_pushChannelMode(INT radioIndex)
12564{
developera3511852023-06-14 14:12:59 +080012565 //Apply Channel mode, pure mode, etc that been set by wifi_setRadioChannelMode() instantly
12566 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012567}
12568
12569INT wifi_pushDefaultValues(INT radioIndex)
12570{
developera3511852023-06-14 14:12:59 +080012571 //Apply Comcast specified default radio settings instantly
12572 //AMPDU=1
12573 //AMPDUFrames=32
12574 //AMPDULim=50000
12575 //txqueuelen=1000
developer72fb0bb2023-01-11 09:46:29 +080012576
developera3511852023-06-14 14:12:59 +080012577 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012578}
12579
12580INT wifi_pushTxChainMask(INT radioIndex)
12581{
developera3511852023-06-14 14:12:59 +080012582 //Apply default TxChainMask instantly
12583 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012584}
12585
12586INT wifi_pushRxChainMask(INT radioIndex)
12587{
developera3511852023-06-14 14:12:59 +080012588 //Apply default RxChainMask instantly
12589 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012590}
12591
12592INT wifi_pushSSID(INT apIndex, CHAR *ssid)
12593{
developer7e4a2a62023-04-06 19:56:03 +080012594 INT status;
developer72fb0bb2023-01-11 09:46:29 +080012595
developer7e4a2a62023-04-06 19:56:03 +080012596 status = wifi_setSSIDName(apIndex, ssid);
12597 wifi_quick_reload_ap(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080012598
developer7e4a2a62023-04-06 19:56:03 +080012599 return status;
developer72fb0bb2023-01-11 09:46:29 +080012600}
12601
12602INT wifi_pushSsidAdvertisementEnable(INT apIndex, BOOL enable)
12603{
developera3511852023-06-14 14:12:59 +080012604 int ret;
developerc1aa6532023-06-09 09:37:01 +080012605 ret = wifi_setApSsidAdvertisementEnable(apIndex, enable);
12606
12607 return ret;
developer72fb0bb2023-01-11 09:46:29 +080012608}
12609
12610INT wifi_getRadioUpTime(INT radioIndex, ULONG *output)
12611{
developera3511852023-06-14 14:12:59 +080012612 time_t now;
developere82c0ca2023-05-10 16:25:35 +080012613
developerd14dff12023-06-28 22:47:44 +080012614 now = time(NULL);
12615 if (now < 0) {
12616 wifi_debug(DEBUG_ERROR, "GET time fail\n");
12617 return RETURN_ERR;
12618 }
developere82c0ca2023-05-10 16:25:35 +080012619 if (now > radio_up_time[radioIndex])
12620 *output = now - radio_up_time[radioIndex];
12621 else {
12622 *output = 0;
12623 return RETURN_ERR;
12624 }
12625
developera3511852023-06-14 14:12:59 +080012626 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012627}
12628
12629INT wifi_getApEnableOnLine(INT wlanIndex, BOOL *enabled)
12630{
developera3511852023-06-14 14:12:59 +080012631 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012632}
12633
12634INT wifi_getApSecurityWpaRekeyInterval(INT apIndex, INT *output_int)
12635{
developera3511852023-06-14 14:12:59 +080012636 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012637}
12638
12639//To-do
12640INT wifi_getApSecurityMFPConfig(INT apIndex, CHAR *output_string)
12641{
developera3511852023-06-14 14:12:59 +080012642 char output[16]={'\0'};
12643 char config_file[MAX_BUF_SIZE] = {0};
12644 int res;
developer72fb0bb2023-01-11 09:46:29 +080012645
developera3511852023-06-14 14:12:59 +080012646 if (!output_string)
12647 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012648
developera3511852023-06-14 14:12:59 +080012649 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12650 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012651 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12652 return RETURN_ERR;
12653 }
developera3511852023-06-14 14:12:59 +080012654 wifi_hostapdRead(config_file, "ieee80211w", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012655
developera3511852023-06-14 14:12:59 +080012656 if (strlen(output) == 0)
developere40952c2023-06-15 18:46:43 +080012657 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012658 else if (strncmp(output, "0", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012659 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012660 else if (strncmp(output, "1", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012661 res = snprintf(output_string, 64, "Optional");
developera3511852023-06-14 14:12:59 +080012662 else if (strncmp(output, "2", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012663 res = snprintf(output_string, 64, "Required");
developera3511852023-06-14 14:12:59 +080012664 else {
12665 wifi_dbg_printf("\n[%s]: Unexpected ieee80211w=%s", __func__, output);
12666 return RETURN_ERR;
12667 }
developere40952c2023-06-15 18:46:43 +080012668 if (os_snprintf_error(64, res)) {
12669 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12670 return RETURN_ERR;
12671 }
developer72fb0bb2023-01-11 09:46:29 +080012672
developera3511852023-06-14 14:12:59 +080012673 wifi_dbg_printf("\n[%s]: ieee80211w is : %s", __func__, output);
12674 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012675}
12676INT wifi_setApSecurityMFPConfig(INT apIndex, CHAR *MfpConfig)
12677{
developera3511852023-06-14 14:12:59 +080012678 struct params params;
12679 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080012680 int res;
developer9f2358c2023-09-22 18:42:12 +080012681 bool temp_multiple_set = multiple_set;
developer72fb0bb2023-01-11 09:46:29 +080012682
developera3511852023-06-14 14:12:59 +080012683 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12684 if(NULL == MfpConfig || strlen(MfpConfig) >= 32 )
12685 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012686
developera3511852023-06-14 14:12:59 +080012687 params.name = "ieee80211w";
12688 if (strncmp(MfpConfig, "Disabled", strlen("Disabled")) == 0)
12689 params.value = "0";
12690 else if (strncmp(MfpConfig, "Optional", strlen("Optional")) == 0)
12691 params.value = "1";
12692 else if (strncmp(MfpConfig, "Required", strlen("Required")) == 0)
12693 params.value = "2";
12694 else{
12695 wifi_dbg_printf("%s: invalid MfpConfig. Input has to be Disabled, Optional or Required \n", __func__);
12696 return RETURN_ERR;
12697 }
developer75bd10c2023-06-27 11:34:08 +080012698
12699 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12700 if (os_snprintf_error(sizeof(config_file), res)) {
12701 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12702 return RETURN_ERR;
12703 }
12704
developera3511852023-06-14 14:12:59 +080012705 wifi_hostapdWrite(config_file, &params, 1);
developer9f2358c2023-09-22 18:42:12 +080012706 multiple_set = false;
12707 wifi_hostapdProcessUpdate(apIndex, &params, 1);
12708 wifi_quick_reload_ap(apIndex);
12709 multiple_set = temp_multiple_set;
12710
developera3511852023-06-14 14:12:59 +080012711 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12712 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012713}
12714INT wifi_getRadioAutoChannelEnable(INT radioIndex, BOOL *output_bool)
12715{
developera3511852023-06-14 14:12:59 +080012716 char output[16]={'\0'};
12717 char config_file[MAX_BUF_SIZE] = {0};
12718 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080012719 int res;
developer72fb0bb2023-01-11 09:46:29 +080012720
developera3511852023-06-14 14:12:59 +080012721 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12722 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080012723 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
12724 if (os_snprintf_error(sizeof(config_file), res)) {
12725 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12726 return RETURN_ERR;
12727 }
developera3511852023-06-14 14:12:59 +080012728 wifi_datfileRead(config_file, "AutoChannelSelect" , output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012729
developera3511852023-06-14 14:12:59 +080012730 if (strncmp(output, "0", 1) == 0)
12731 *output_bool = FALSE;
12732 else if (strncmp(output, "1", 1) == 0)
12733 *output_bool = TRUE;
12734 else if (strncmp(output, "2", 1) == 0)
12735 *output_bool = TRUE;
12736 else if (strncmp(output, "3", 1) == 0)
12737 *output_bool = TRUE;
12738 else
12739 *output_bool = FALSE;
12740 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012741
developera3511852023-06-14 14:12:59 +080012742 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012743}
12744
12745INT wifi_getRouterEnable(INT wlanIndex, BOOL *enabled)
12746{
developera3511852023-06-14 14:12:59 +080012747 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012748}
12749
12750INT wifi_setApSecurityWpaRekeyInterval(INT apIndex, INT *rekeyInterval)
12751{
developera3511852023-06-14 14:12:59 +080012752 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012753}
12754
12755INT wifi_setRouterEnable(INT wlanIndex, INT *RouterEnabled)
12756{
developera3511852023-06-14 14:12:59 +080012757 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012758}
12759
12760INT wifi_getRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
12761{
developera3511852023-06-14 14:12:59 +080012762 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12763 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080012764 char tmp_output[MAX_BUF_SIZE] = {0};
developera3511852023-06-14 14:12:59 +080012765 int res;
developer72fb0bb2023-01-11 09:46:29 +080012766
developera3511852023-06-14 14:12:59 +080012767 if (NULL == output)
12768 return RETURN_ERR;
12769 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12770 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012771 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12772 return RETURN_ERR;
12773 }
developera3511852023-06-14 14:12:59 +080012774 wifi_hostapdRead(config_file,"hw_mode",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012775
developer32f2a182023-06-27 19:50:41 +080012776 if(strcmp(output,"b")==0) {
12777 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,11");
12778 if (os_snprintf_error(sizeof(tmp_output), res)) {
12779 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12780 return RETURN_ERR;
12781 }
12782 } else if (strcmp(output,"a")==0) {
12783 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "6,9,11,12,18,24,36,48,54");
12784 if (os_snprintf_error(sizeof(tmp_output), res)) {
12785 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12786 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080012787 }
developer32f2a182023-06-27 19:50:41 +080012788 } else if ((strcmp(output,"n")==0) | (strcmp(output,"g")==0)) {
12789 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,6,9,11,12,18,24,36,48,54");
12790 if (os_snprintf_error(sizeof(tmp_output), res)) {
12791 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12792 return RETURN_ERR;
12793 }
developer75bd10c2023-06-27 11:34:08 +080012794 }
developer32f2a182023-06-27 19:50:41 +080012795 memcpy(output, tmp_output, strlen(tmp_output));
12796 output[strlen(tmp_output)] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012797
developera3511852023-06-14 14:12:59 +080012798 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12799 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012800}
12801
12802INT wifi_getRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
12803{
developera3511852023-06-14 14:12:59 +080012804 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12805 char *temp;
developer32f2a182023-06-27 19:50:41 +080012806 char temp_output[128] = {0};
12807 char temp_TransmitRates[128] = {0};
developera3511852023-06-14 14:12:59 +080012808 char config_file[MAX_BUF_SIZE] = {0};
12809 int res;
developer32f2a182023-06-27 19:50:41 +080012810 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012811
developera3511852023-06-14 14:12:59 +080012812 if (NULL == output)
12813 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012814
developera3511852023-06-14 14:12:59 +080012815 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12816 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012817 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12818 return RETURN_ERR;
12819 }
developera3511852023-06-14 14:12:59 +080012820 wifi_hostapdRead(config_file,"supported_rates",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012821
developera3511852023-06-14 14:12:59 +080012822 if (strlen(output) == 0) {
12823 wifi_getRadioSupportedDataTransmitRates(wlanIndex, output);
12824 return RETURN_OK;
12825 }
developer32f2a182023-06-27 19:50:41 +080012826 len = strlen(output);
12827 if (len >= sizeof(temp_TransmitRates)) {
12828 wifi_debug(DEBUG_ERROR, "Unexpected strlen(output)\n");
12829 return RETURN_ERR;
12830 }
12831 strncpy(temp_TransmitRates, output, len);
developera3511852023-06-14 14:12:59 +080012832 temp = strtok(temp_TransmitRates," ");
12833 while(temp!=NULL)
12834 {
12835 temp[strlen(temp)-1]=0;
12836 if((temp[0]=='5') && (temp[1]=='\0'))
12837 {
12838 temp="5.5";
12839 }
developer32f2a182023-06-27 19:50:41 +080012840 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp)) {
12841 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
12842 return RETURN_ERR;
12843 }
12844 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080012845 temp = strtok(NULL," ");
12846 if(temp!=NULL)
12847 {
developer32f2a182023-06-27 19:50:41 +080012848 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
12849 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
12850 return RETURN_ERR;
12851 }
12852 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080012853 }
12854 }
developer32f2a182023-06-27 19:50:41 +080012855 len = strlen(temp_output);
12856 strncpy(output, temp_output, len);
12857 output[len] = '\0';
developera3511852023-06-14 14:12:59 +080012858 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012859
developera3511852023-06-14 14:12:59 +080012860 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012861}
12862
12863INT wifi_setRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
12864{
developera3511852023-06-14 14:12:59 +080012865 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012866}
12867
12868
12869INT wifi_setRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
12870{
developera3511852023-06-14 14:12:59 +080012871 int i=0;
12872 char *temp;
12873 char temp1[128] = {0};
12874 char temp_output[128] = {0};
12875 char temp_TransmitRates[128] = {0};
12876 struct params params={'\0'};
12877 char config_file[MAX_BUF_SIZE] = {0};
12878 wifi_band band = wifi_index_to_band(wlanIndex);
developer32f2a182023-06-27 19:50:41 +080012879 unsigned long len;
12880 int res;
developer9f2358c2023-09-22 18:42:12 +080012881 bool temp_multiple_set = multiple_set;
developer72fb0bb2023-01-11 09:46:29 +080012882
developera3511852023-06-14 14:12:59 +080012883 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12884 if(NULL == output)
12885 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080012886
12887 len = strlen(output);
12888 if (len >= sizeof(temp_TransmitRates)) {
12889 wifi_debug(DEBUG_ERROR, "not enough room in temp_TransmitRates\n");
12890 return RETURN_ERR;
12891 }
12892 strncpy(temp_TransmitRates, output, len);
12893 temp_TransmitRates[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012894
developera3511852023-06-14 14:12:59 +080012895 for(i=0;i<strlen(temp_TransmitRates);i++)
12896 {
12897 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
12898 {
12899 continue;
12900 }
12901 else
12902 {
12903 return RETURN_ERR;
12904 }
12905 }
developera3511852023-06-14 14:12:59 +080012906 temp = strtok(temp_TransmitRates,",");
12907 while(temp!=NULL)
12908 {
developer32f2a182023-06-27 19:50:41 +080012909 len = strlen(temp);
12910 if (len >= sizeof(temp1)) {
12911 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
12912 return RETURN_ERR;
12913 }
12914 strncpy(temp1, temp, len);
12915 temp1[len] = '\0';
developera3511852023-06-14 14:12:59 +080012916 if(band == band_5)
12917 {
12918 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
12919 {
12920 return RETURN_ERR;
12921 }
12922 }
developer72fb0bb2023-01-11 09:46:29 +080012923
developer32f2a182023-06-27 19:50:41 +080012924 if(strcmp(temp,"5.5")==0) {
12925 strncpy(temp1, "55", 2);
12926 temp1[2] = '\0';
12927 } else {
12928 if ((sizeof(temp1) - strlen(temp1)) <= 1) {
12929 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
12930 return RETURN_ERR;
12931 }
12932 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +080012933 }
developer5b23cd02023-07-19 20:26:03 +080012934
developer32f2a182023-06-27 19:50:41 +080012935 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp1)) {
12936 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
12937 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080012938 }
developer32f2a182023-06-27 19:50:41 +080012939 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080012940 temp = strtok(NULL,",");
12941 if(temp!=NULL)
12942 {
developer32f2a182023-06-27 19:50:41 +080012943 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
12944 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
12945 return RETURN_ERR;
12946 }
12947 strncat(temp_output, " ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080012948 }
12949 }
developer32f2a182023-06-27 19:50:41 +080012950 len = strlen(temp_output);
12951 strncpy(output, temp_output, len);
12952 output[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012953
developera3511852023-06-14 14:12:59 +080012954 params.name = "supported_rates";
12955 params.value = output;
developer72fb0bb2023-01-11 09:46:29 +080012956
developera3511852023-06-14 14:12:59 +080012957 wifi_dbg_printf("\n%s:",__func__);
12958 wifi_dbg_printf("params.value=%s\n",params.value);
developer32f2a182023-06-27 19:50:41 +080012959 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12960 if (os_snprintf_error(sizeof(config_file), res)) {
12961 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12962 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080012963 }
developer9f2358c2023-09-22 18:42:12 +080012964
developera3511852023-06-14 14:12:59 +080012965 wifi_hostapdWrite(config_file,&params,1);
developer9f2358c2023-09-22 18:42:12 +080012966 multiple_set = false;
12967 wifi_hostapdProcessUpdate(wlanIndex, &params, 1);
12968 wifi_quick_reload_ap(wlanIndex);
12969 multiple_set = temp_multiple_set;
12970
developera3511852023-06-14 14:12:59 +080012971 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012972
developera3511852023-06-14 14:12:59 +080012973 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012974}
12975
12976
12977static char *sncopy(char *dst, int dst_sz, const char *src)
12978{
developera3511852023-06-14 14:12:59 +080012979 if (src && dst && dst_sz > 0) {
12980 strncpy(dst, src, dst_sz);
12981 dst[dst_sz - 1] = '\0';
12982 }
12983 return dst;
developer72fb0bb2023-01-11 09:46:29 +080012984}
12985
12986static int util_get_sec_chan_offset(int channel, const char* ht_mode)
12987{
developera3511852023-06-14 14:12:59 +080012988 if (0 == strcmp(ht_mode, "HT40") ||
12989 0 == strcmp(ht_mode, "HT80") ||
12990 0 == strcmp(ht_mode, "HT160")) {
12991 switch (channel) {
12992 case 1 ... 7:
12993 case 36:
12994 case 44:
12995 case 52:
12996 case 60:
12997 case 100:
12998 case 108:
12999 case 116:
13000 case 124:
13001 case 132:
13002 case 140:
13003 case 149:
13004 case 157:
13005 return 1;
13006 case 8 ... 13:
13007 case 40:
13008 case 48:
13009 case 56:
13010 case 64:
13011 case 104:
13012 case 112:
13013 case 120:
13014 case 128:
13015 case 136:
13016 case 144:
13017 case 153:
13018 case 161:
13019 return -1;
13020 default:
13021 return -EINVAL;
13022 }
13023 }
developer72fb0bb2023-01-11 09:46:29 +080013024
developera3511852023-06-14 14:12:59 +080013025 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013026}
13027
13028static int util_get_6g_sec_chan_offset(int channel, const char* ht_mode)
13029{
developera3511852023-06-14 14:12:59 +080013030 int idx = channel%8;
13031 if (0 == strcmp(ht_mode, "HT40") ||
13032 0 == strcmp(ht_mode, "HT80") ||
13033 0 == strcmp(ht_mode, "HT160")) {
13034 switch (idx) {
13035 case 1:
13036 return 1;
13037 case 5:
13038 return -1;
13039 default:
13040 return -EINVAL;
13041 }
13042 }
developer72fb0bb2023-01-11 09:46:29 +080013043
developera3511852023-06-14 14:12:59 +080013044 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013045}
13046static void util_hw_mode_to_bw_mode(const char* hw_mode, char *bw_mode, int bw_mode_len)
13047{
developera3511852023-06-14 14:12:59 +080013048 if (NULL == hw_mode) return;
developer72fb0bb2023-01-11 09:46:29 +080013049
developera3511852023-06-14 14:12:59 +080013050 if (0 == strcmp(hw_mode, "ac"))
13051 sncopy(bw_mode, bw_mode_len, "ht vht");
developer72fb0bb2023-01-11 09:46:29 +080013052
developera3511852023-06-14 14:12:59 +080013053 if (0 == strcmp(hw_mode, "n"))
13054 sncopy(bw_mode, bw_mode_len, "ht");
developer72fb0bb2023-01-11 09:46:29 +080013055
developera3511852023-06-14 14:12:59 +080013056 return;
developer72fb0bb2023-01-11 09:46:29 +080013057}
13058
13059static int util_chan_to_freq(int chan)
13060{
developera3511852023-06-14 14:12:59 +080013061 if (chan == 14)
13062 return 2484;
13063 else if (chan < 14)
13064 return 2407 + chan * 5;
13065 else if (chan >= 182 && chan <= 196)
13066 return 4000 + chan * 5;
13067 else
13068 return 5000 + chan * 5;
13069 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013070}
13071
13072static int util_6G_chan_to_freq(int chan)
13073{
developera3511852023-06-14 14:12:59 +080013074 if (chan)
13075 return 5950 + chan * 5;
13076 else
13077 return 0;
developer69b61b02023-03-07 17:17:44 +080013078
developer72fb0bb2023-01-11 09:46:29 +080013079}
13080const int *util_unii_5g_chan2list(int chan, int width)
13081{
developera3511852023-06-14 14:12:59 +080013082 static const int lists[] = {
13083 // <width>, <chan1>, <chan2>..., 0,
13084 20, 36, 0,
13085 20, 40, 0,
13086 20, 44, 0,
13087 20, 48, 0,
13088 20, 52, 0,
13089 20, 56, 0,
13090 20, 60, 0,
13091 20, 64, 0,
13092 20, 100, 0,
13093 20, 104, 0,
13094 20, 108, 0,
13095 20, 112, 0,
13096 20, 116, 0,
13097 20, 120, 0,
13098 20, 124, 0,
13099 20, 128, 0,
13100 20, 132, 0,
13101 20, 136, 0,
13102 20, 140, 0,
13103 20, 144, 0,
13104 20, 149, 0,
13105 20, 153, 0,
13106 20, 157, 0,
13107 20, 161, 0,
13108 20, 165, 0,
13109 40, 36, 40, 0,
13110 40, 44, 48, 0,
13111 40, 52, 56, 0,
13112 40, 60, 64, 0,
13113 40, 100, 104, 0,
13114 40, 108, 112, 0,
13115 40, 116, 120, 0,
13116 40, 124, 128, 0,
13117 40, 132, 136, 0,
13118 40, 140, 144, 0,
13119 40, 149, 153, 0,
13120 40, 157, 161, 0,
13121 80, 36, 40, 44, 48, 0,
13122 80, 52, 56, 60, 64, 0,
13123 80, 100, 104, 108, 112, 0,
13124 80, 116, 120, 124, 128, 0,
13125 80, 132, 136, 140, 144, 0,
13126 80, 149, 153, 157, 161, 0,
13127 160, 36, 40, 44, 48, 52, 56, 60, 64, 0,
13128 160, 100, 104, 108, 112, 116, 120, 124, 128, 0,
13129 -1 // final delimiter
13130 };
13131 const int *start;
13132 const int *p;
developer72fb0bb2023-01-11 09:46:29 +080013133
developera3511852023-06-14 14:12:59 +080013134 for (p = lists; *p != -1; p++) {
13135 if (*p == width) {
13136 for (start = ++p; *p != 0; p++) {
13137 if (*p == chan)
13138 return start;
13139 }
13140 }
13141 // move to the end of channel list of given width
13142 while (*p != 0) {
13143 p++;
13144 }
13145 }
developer72fb0bb2023-01-11 09:46:29 +080013146
developera3511852023-06-14 14:12:59 +080013147 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080013148}
13149
13150static int util_unii_5g_centerfreq(const char *ht_mode, int channel)
13151{
developera3511852023-06-14 14:12:59 +080013152 if (NULL == ht_mode)
13153 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013154
developera3511852023-06-14 14:12:59 +080013155 const int width = atoi(strlen(ht_mode) > 2 ? ht_mode + 2 : "20");
13156 const int *chans = util_unii_5g_chan2list(channel, width);
13157 int sum = 0;
13158 int cnt = 0;
developer72fb0bb2023-01-11 09:46:29 +080013159
developera3511852023-06-14 14:12:59 +080013160 if (NULL == chans)
13161 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013162
developera3511852023-06-14 14:12:59 +080013163 while (*chans) {
13164 sum += *chans;
13165 cnt++;
13166 chans++;
13167 }
13168 if (cnt == 0)
13169 return 0;
13170 return sum / cnt;
developer72fb0bb2023-01-11 09:46:29 +080013171}
13172
13173static int util_unii_6g_centerfreq(const char *ht_mode, int channel)
13174{
developerc14d83a2023-06-29 20:09:42 +080013175 long int width;
developera3511852023-06-14 14:12:59 +080013176 int idx = 0 ;
13177 int centerchan = 0;
13178 int chan_ofs = 1;
developer72fb0bb2023-01-11 09:46:29 +080013179
developerc14d83a2023-06-29 20:09:42 +080013180 if (NULL == ht_mode)
13181 return 0;
13182
13183 if (hal_strtol((char *)(ht_mode + 2), 10, &width) < 0) {
13184 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13185 }
13186
developera3511852023-06-14 14:12:59 +080013187 if (width == 40){
13188 idx = ((channel/4) + chan_ofs)%2;
13189 switch (idx) {
13190 case 0:
13191 centerchan = (channel - 2);
13192 break;
13193 case 1:
13194 centerchan = (channel + 2);
13195 break;
13196 default:
13197 return -EINVAL;
13198 }
13199 }else if (width == 80){
13200 idx = ((channel/4) + chan_ofs)%4;
13201 switch (idx) {
13202 case 0:
13203 centerchan = (channel - 6);
13204 break;
13205 case 1:
13206 centerchan = (channel + 6);
13207 break;
13208 case 2:
13209 centerchan = (channel + 2);
13210 break;
13211 case 3:
13212 centerchan = (channel - 2);
13213 break;
13214 default:
13215 return -EINVAL;
13216 }
13217 }else if (width == 160){
13218 switch (channel) {
13219 case 1 ... 29:
13220 centerchan = 15;
13221 break;
13222 case 33 ... 61:
13223 centerchan = 47;
13224 break;
13225 case 65 ... 93:
13226 centerchan = 79;
13227 break;
13228 case 97 ... 125:
13229 centerchan = 111;
13230 break;
13231 case 129 ... 157:
13232 centerchan = 143;
13233 break;
13234 case 161 ... 189:
13235 centerchan = 175;
13236 break;
13237 case 193 ... 221:
13238 centerchan = 207;
13239 break;
13240 default:
13241 return -EINVAL;
13242 }
13243 }
13244 return centerchan;
developer72fb0bb2023-01-11 09:46:29 +080013245}
13246static int util_radio_get_hw_mode(int radioIndex, char *hw_mode, int hw_mode_size)
13247{
developera3511852023-06-14 14:12:59 +080013248 BOOL onlyG, onlyN, onlyA;
13249 CHAR tmp[64];
13250 int ret = wifi_getRadioStandard(radioIndex, tmp, &onlyG, &onlyN, &onlyA);
13251 if (ret == RETURN_OK) {
13252 sncopy(hw_mode, hw_mode_size, tmp);
13253 }
13254 return ret;
developer72fb0bb2023-01-11 09:46:29 +080013255}
13256
13257INT wifi_pushRadioChannel2(INT radioIndex, UINT channel, UINT channel_width_MHz, UINT csa_beacon_count)
13258{
developera3511852023-06-14 14:12:59 +080013259 // Sample commands:
13260 // hostapd_cli -i wifi1 chan_switch 30 5200 sec_channel_offset=-1 center_freq1=5190 bandwidth=40 ht vht
13261 // hostapd_cli -i wifi0 chan_switch 30 2437
13262 int ret = 0;
13263 char center_freq1_str[32] = ""; // center_freq1=%d
13264 char opt_chan_info_str[32] = ""; // bandwidth=%d ht vht
13265 char sec_chan_offset_str[32] = ""; // sec_channel_offset=%d
13266 char hw_mode[16] = ""; // n|ac
13267 char bw_mode[16] = ""; // ht|ht vht
13268 char ht_mode[16] = ""; // HT20|HT40|HT80|HT160
13269 char interface_name[16] = {0};
13270 int sec_chan_offset;
13271 int width;
13272 char config_file[64] = {0};
13273 char *ext_str = "None";
13274 wifi_band band = band_invalid;
13275 int center_chan = 0;
13276 int center_freq1 = 0;
developere40952c2023-06-15 18:46:43 +080013277 int res;
developer72fb0bb2023-01-11 09:46:29 +080013278
developere40952c2023-06-15 18:46:43 +080013279 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
13280 if (os_snprintf_error(sizeof(config_file), res)) {
13281 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13282 return RETURN_ERR;
13283 }
developer72fb0bb2023-01-11 09:46:29 +080013284
developera3511852023-06-14 14:12:59 +080013285 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
13286 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013287
developera3511852023-06-14 14:12:59 +080013288 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013289
developera3511852023-06-14 14:12:59 +080013290 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080013291
developera3511852023-06-14 14:12:59 +080013292 width = channel_width_MHz > 20 ? channel_width_MHz : 20;
developer72fb0bb2023-01-11 09:46:29 +080013293
developera3511852023-06-14 14:12:59 +080013294 // Get radio mode HT20|HT40|HT80 etc.
13295 if (channel){
developere40952c2023-06-15 18:46:43 +080013296 res = snprintf(ht_mode, sizeof(ht_mode), "HT%d", width);
13297 if (os_snprintf_error(sizeof(ht_mode), res)) {
13298 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13299 return RETURN_ERR;
13300 }
developer72fb0bb2023-01-11 09:46:29 +080013301
developera3511852023-06-14 14:12:59 +080013302 // Provide bandwith if specified
13303 if (channel_width_MHz > 20) {
13304 // Select bandwidth mode from hardware n --> ht | ac --> ht vht
13305 util_radio_get_hw_mode(radioIndex, hw_mode, sizeof(hw_mode));
13306 util_hw_mode_to_bw_mode(hw_mode, bw_mode, sizeof(bw_mode));
developer72fb0bb2023-01-11 09:46:29 +080013307
developere40952c2023-06-15 18:46:43 +080013308 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d %s", width, bw_mode);
developera3511852023-06-14 14:12:59 +080013309 }else if (channel_width_MHz == 20){
developere40952c2023-06-15 18:46:43 +080013310 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d ht", width);
developera3511852023-06-14 14:12:59 +080013311 }
developer72fb0bb2023-01-11 09:46:29 +080013312
developere40952c2023-06-15 18:46:43 +080013313 if (os_snprintf_error(sizeof(opt_chan_info_str), res)) {
13314 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13315 return RETURN_ERR;
13316 }
developer72fb0bb2023-01-11 09:46:29 +080013317
developera3511852023-06-14 14:12:59 +080013318 if (channel_width_MHz > 20) {
13319 if (band == band_6){
13320 center_chan = util_unii_6g_centerfreq(ht_mode, channel);
13321 if(center_chan){
13322 center_freq1 = util_6G_chan_to_freq(center_chan);
13323 }
13324 }else{
13325 center_chan = util_unii_5g_centerfreq(ht_mode, channel);
13326 if(center_chan){
13327 center_freq1 = util_chan_to_freq(center_chan);
13328 }
13329 }
developer69b61b02023-03-07 17:17:44 +080013330
developera3511852023-06-14 14:12:59 +080013331 if (center_freq1)
developere40952c2023-06-15 18:46:43 +080013332 res = snprintf(center_freq1_str, sizeof(center_freq1_str), "center_freq1=%d", center_freq1);
developer69b61b02023-03-07 17:17:44 +080013333
developera3511852023-06-14 14:12:59 +080013334 }
developere40952c2023-06-15 18:46:43 +080013335 if (os_snprintf_error(sizeof(center_freq1_str), res)) {
13336 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13337 return RETURN_ERR;
13338 }
developer72fb0bb2023-01-11 09:46:29 +080013339
developera3511852023-06-14 14:12:59 +080013340 // Find channel offset +1/-1 for wide modes (HT40|HT80|HT160)
13341 if (band == band_6){
13342 sec_chan_offset = util_get_6g_sec_chan_offset(channel, ht_mode);
13343 }else{
13344 sec_chan_offset = util_get_sec_chan_offset(channel, ht_mode);
13345 }
developere40952c2023-06-15 18:46:43 +080013346 if (sec_chan_offset != -EINVAL) {
13347 res = snprintf(sec_chan_offset_str, sizeof(sec_chan_offset_str), "sec_channel_offset=%d", sec_chan_offset);
13348 if (os_snprintf_error(sizeof(sec_chan_offset_str), res)) {
13349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13350 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013351 }
developere40952c2023-06-15 18:46:43 +080013352 }
developera3511852023-06-14 14:12:59 +080013353 // Only the first AP, other are hanging on the same radio
13354 ret = wifi_setChannel_netlink(radioIndex, &channel, NULL);
13355 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013356 wifi_debug(DEBUG_ERROR,"wifi_setChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013357 return RETURN_ERR;
13358 }
13359 /* wifi_dbg_printf("execute: '%s'\n", cmd);
13360 ret = _syscmd(cmd, buf, sizeof(buf));
13361 wifi_reloadAp(radioIndex); */
developer72fb0bb2023-01-11 09:46:29 +080013362
developera3511852023-06-14 14:12:59 +080013363 ret = wifi_setRadioChannel(radioIndex, channel);
13364 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013365 wifi_debug(DEBUG_ERROR,"wifi_setRadioChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013366 return RETURN_ERR;
13367 }
developer72fb0bb2023-01-11 09:46:29 +080013368
developera3511852023-06-14 14:12:59 +080013369 if (sec_chan_offset == 1)
13370 ext_str = "Above";
13371 else if (sec_chan_offset == -1)
13372 ext_str = "Below";
developer72fb0bb2023-01-11 09:46:29 +080013373
developera3511852023-06-14 14:12:59 +080013374 /*wifi_setRadioCenterChannel(radioIndex, center_chan); */
developer72fb0bb2023-01-11 09:46:29 +080013375
developera3511852023-06-14 14:12:59 +080013376 } else {
13377 if (channel_width_MHz > 20)
13378 ext_str = "Above";
13379 }
developer72fb0bb2023-01-11 09:46:29 +080013380
developera3511852023-06-14 14:12:59 +080013381 wifi_setRadioExtChannel(radioIndex, ext_str);
developer72fb0bb2023-01-11 09:46:29 +080013382
developera3511852023-06-14 14:12:59 +080013383 char mhz_str[16];
developere40952c2023-06-15 18:46:43 +080013384 res = snprintf(mhz_str, sizeof(mhz_str), "%dMHz", width);
13385 if (os_snprintf_error(sizeof(mhz_str), res)) {
13386 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13387 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013388 }
developera3511852023-06-14 14:12:59 +080013389 wifi_setRadioOperatingChannelBandwidth(radioIndex, mhz_str);
developer72fb0bb2023-01-11 09:46:29 +080013390
developera3511852023-06-14 14:12:59 +080013391 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013392
developera3511852023-06-14 14:12:59 +080013393 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013394}
13395
13396INT wifi_getNeighboringWiFiStatus(INT radio_index, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size)
13397{
developera3511852023-06-14 14:12:59 +080013398 int index = -1;
13399 wifi_neighbor_ap2_t *scan_array = NULL;
13400 char cmd[256]={0};
13401 char buf[128]={0};
13402 char file_name[32] = {0};
13403 char filter_SSID[32] = {0};
13404 char line[256] = {0};
13405 char interface_name[16] = {0};
13406 char *ret = NULL;
13407 int freq=0;
13408 FILE *f = NULL;
developer86035662023-06-28 19:21:12 +080013409 long int channels_num = 0;
developera3511852023-06-14 14:12:59 +080013410 int vht_channel_width = 0;
13411 int get_noise_ret = RETURN_ERR;
13412 bool filter_enable = false;
13413 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
13414 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080013415 int res;
developer32f2a182023-06-27 19:50:41 +080013416 unsigned long len;
developerb14b3462023-07-01 18:02:42 +080013417 struct channels_noise *channels_noise_arr = NULL;
developer72fb0bb2023-01-11 09:46:29 +080013418
developera3511852023-06-14 14:12:59 +080013419 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013420
developere40952c2023-06-15 18:46:43 +080013421 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radio_index);
13422 if (os_snprintf_error(sizeof(file_name), res)) {
13423 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13424 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013425 }
developera3511852023-06-14 14:12:59 +080013426 f = fopen(file_name, "r");
13427 if (f != NULL) {
developer86035662023-06-28 19:21:12 +080013428 if (fgets(filter_SSID, sizeof(file_name), f) == NULL) {
13429 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080013430 if (fclose(f) != 0) {
13431 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13432 }
developer86035662023-06-28 19:21:12 +080013433 return RETURN_ERR;
13434 }
developera3511852023-06-14 14:12:59 +080013435 if (strlen(filter_SSID) != 0)
13436 filter_enable = true;
developerd14dff12023-06-28 22:47:44 +080013437 if (fclose(f) != 0) {
13438 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13439 return RETURN_ERR;
13440 }
developera3511852023-06-14 14:12:59 +080013441 }
developer72fb0bb2023-01-11 09:46:29 +080013442
developera3511852023-06-14 14:12:59 +080013443 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
13444 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013445
developera3511852023-06-14 14:12:59 +080013446 phyId = radio_index_to_phy(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080013447
developer8078acf2023-08-04 18:52:48 +080013448 res = _syscmd_secure(buf, sizeof(buf), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
13449 if (res) {
13450 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080013451 }
developer86035662023-06-28 19:21:12 +080013452 if (hal_strtol(buf, 10, &channels_num) < 0) {
13453 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13454 return RETURN_ERR;
13455 }
developer72fb0bb2023-01-11 09:46:29 +080013456
developer5b23cd02023-07-19 20:26:03 +080013457 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 +080013458 // 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 +080013459 if (os_snprintf_error(sizeof(cmd), res)) {
13460 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13461 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013462 }
developer32f2a182023-06-27 19:50:41 +080013463
developer86035662023-06-28 19:21:12 +080013464 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developer8078acf2023-08-04 18:52:48 +080013465
13466 f = v_secure_popen("r", "iw dev %s scan | grep '%s\\|SSID\\|freq\\|beacon interval\\|capabilities\\|signal\\|Supported rates\\|DTIM\\| \
13467 // WPA\\|RSN\\|Group cipher\\|HT operation\\|secondary channel offset\\|channel width\\|HE.*GHz' | grep -v -e '*.*BSS'", interface_name, interface_name);
13468
13469 if (f == NULL) {
13470 wifi_dbg_printf("%s: v_secure_popen %s error\n", __func__, cmd);
developera3511852023-06-14 14:12:59 +080013471 return RETURN_ERR;
13472 }
developer69b61b02023-03-07 17:17:44 +080013473
developerb14b3462023-07-01 18:02:42 +080013474 if (channels_num > 0 && channels_num <= 243) {
13475 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
developer86035662023-06-28 19:21:12 +080013476
developerb14b3462023-07-01 18:02:42 +080013477 if (channels_noise_arr == NULL) {
13478 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13479 goto err;
13480 }
13481 get_noise_ret = get_noise(radio_index, channels_noise_arr, channels_num);
developer9ce44382023-06-28 11:09:37 +080013482 }
developer86035662023-06-28 19:21:12 +080013483
developera3511852023-06-14 14:12:59 +080013484 ret = fgets(line, sizeof(line), f);
13485 while (ret != NULL) {
13486 if(strstr(line, "BSS") != NULL) { // new neighbor info
13487 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
13488 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
13489 // 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 +080013490
developera3511852023-06-14 14:12:59 +080013491 if (!filter_BSS) {
13492 index++;
developer5b23cd02023-07-19 20:26:03 +080013493 wifi_debug(DEBUG_OFF, "index=%d\n", index);
developera3511852023-06-14 14:12:59 +080013494 wifi_neighbor_ap2_t *tmp;
13495 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
13496 if (tmp == NULL) { // no more memory to use
13497 index--;
13498 wifi_dbg_printf("%s: realloc failed\n", __func__);
13499 break;
13500 }
13501 scan_array = tmp;
13502 }
13503 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +080013504
developera3511852023-06-14 14:12:59 +080013505 filter_BSS = false;
developer86035662023-06-28 19:21:12 +080013506 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
13507 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13508 goto err;
13509 }
developerc79e9172023-06-06 19:48:03 +080013510 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +080013511 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +080013512 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +080013513 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +080013514 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +080013515 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
13516 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +080013517 if (sscanf(line," freq: %d", &freq) != 1) {
13518 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +080013519 //goto err;
developer86035662023-06-28 19:21:12 +080013520 }
developera3511852023-06-14 14:12:59 +080013521 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080013522
developera3511852023-06-14 14:12:59 +080013523 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +080013524 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013525 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +080013526 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013527 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +080013528 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013529 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
13530 }
13531 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +080013532 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013533 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +080013534 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013535 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +080013536 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013537 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
13538 }
developer72fb0bb2023-01-11 09:46:29 +080013539
developera3511852023-06-14 14:12:59 +080013540 scan_array[index].ap_Noise = 0;
13541 if (get_noise_ret == RETURN_OK) {
13542 for (int i = 0; i < channels_num; i++) {
13543 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
13544 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
13545 break;
13546 }
13547 }
13548 }
13549 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +080013550 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
13551 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13552 goto err;
13553 }
developera3511852023-06-14 14:12:59 +080013554 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +080013555 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
13556 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13557 goto err;
13558 }
developer5b23cd02023-07-19 20:26:03 +080013559 } else if (strstr(line,"SSID:") != NULL) {
13560 int i;
13561 char *new_line = NULL;
13562
13563 for (i = 0; line[i] == '\t'; i++) {
13564 ;
developer86035662023-06-28 19:21:12 +080013565 }
developer5b23cd02023-07-19 20:26:03 +080013566 new_line = &line[i];
13567
13568 if (strncmp(new_line, "SSID: \n", strlen("SSID: \n")) == 0) {
13569 ret = fgets(line, sizeof(line), f);
13570 continue;
13571 }
13572 if (strstr(new_line, "HESSID") == NULL) {
13573 if (sscanf(new_line, "SSID: %63s", scan_array[index].ap_SSID) != 1) {
13574 wifi_debug(DEBUG_ERROR, "sscanf fail, index = %d\n", index);
13575 goto err;
13576 } else
13577 wifi_debug(DEBUG_ERROR, "index = %d, ssid=%s\n", index, scan_array[index].ap_SSID);
13578 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) == 0) {
13579 filter_BSS = true;
13580 }
developera3511852023-06-14 14:12:59 +080013581 }
13582 } else if (strstr(line, "Supported rates") != NULL) {
13583 char SRate[80] = {0}, *tmp = NULL;
13584 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080013585 len = strlen(line);
13586 if (len >= sizeof(SRate)) {
13587 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013588 goto err;
developer32f2a182023-06-27 19:50:41 +080013589 }
13590 strncpy(SRate, line, len);
developera3511852023-06-14 14:12:59 +080013591 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +080013592 if (tmp == NULL)
13593 goto err;
developera3511852023-06-14 14:12:59 +080013594 tmp = strtok(NULL, ":");
developer5b23cd02023-07-19 20:26:03 +080013595 if (tmp == NULL)
developer86035662023-06-28 19:21:12 +080013596 goto err;
developer5b23cd02023-07-19 20:26:03 +080013597
developer32f2a182023-06-27 19:50:41 +080013598 len = strlen(tmp);
13599 if (len >= sizeof(buf)) {
13600 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
developer86035662023-06-28 19:21:12 +080013601 goto err;
developer32f2a182023-06-27 19:50:41 +080013602 }
13603 strncpy(buf, tmp, len);
developera3511852023-06-14 14:12:59 +080013604 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +080013605
developera3511852023-06-14 14:12:59 +080013606 tmp = strtok(buf, " \n");
13607 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +080013608 if ((sizeof(SRate) - strlen(SRate)) <= strlen(tmp)) {
13609 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013610 goto err;
developer32f2a182023-06-27 19:50:41 +080013611 }
13612 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +080013613 if (SRate[strlen(SRate) - 1] == '*') {
13614 SRate[strlen(SRate) - 1] = '\0';
13615 }
developer32f2a182023-06-27 19:50:41 +080013616 if ((sizeof(SRate) - strlen(SRate)) <= 1) {
13617 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013618 goto err;
developer32f2a182023-06-27 19:50:41 +080013619 }
13620 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +080013621
developera3511852023-06-14 14:12:59 +080013622 tmp = strtok(NULL, " \n");
13623 }
13624 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +080013625 len = strlen(SRate);
13626 if (len >= sizeof(scan_array[index].ap_SupportedDataTransferRates)) {
13627 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedDataTransferRates\n");
developer86035662023-06-28 19:21:12 +080013628 goto err;
developer32f2a182023-06-27 19:50:41 +080013629 }
13630 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, len);
13631 scan_array[index].ap_SupportedDataTransferRates[len] = '\0';
developera3511852023-06-14 14:12:59 +080013632 } else if (strstr(line, "DTIM") != NULL) {
developer5b23cd02023-07-19 20:26:03 +080013633 if (sscanf(line," TIM: DTIM Count %*d DTIM Period %d %*s", &(scan_array[index].ap_DTIMPeriod)) != 1) {
developer86035662023-06-28 19:21:12 +080013634 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developer5b23cd02023-07-19 20:26:03 +080013635 goto err;
developer86035662023-06-28 19:21:12 +080013636 }
developera3511852023-06-14 14:12:59 +080013637 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013638 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13639 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013640 goto err;
developer32f2a182023-06-27 19:50:41 +080013641 }
13642 strncat(scan_array[index].ap_SupportedStandards, ",ac",
13643 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13644 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
13645 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +080013646 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013647 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 2) {
13648 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013649 goto err;
developer32f2a182023-06-27 19:50:41 +080013650 }
13651 strncat(scan_array[index].ap_SupportedStandards, ",n",
13652 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13653 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
13654 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +080013655 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013656 if (fgets(line, sizeof(line), f) == NULL) {
13657 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13658 goto err;
13659 }
13660 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
13661 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13662 goto err;
13663 }
developera3511852023-06-14 14:12:59 +080013664 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +080013665 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +080013666 } else {
developere40952c2023-06-15 18:46:43 +080013667 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +080013668 }
developere40952c2023-06-15 18:46:43 +080013669 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13670 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013671 goto err;
developer5b23cd02023-07-19 20:26:03 +080013672 }
developere40952c2023-06-15 18:46:43 +080013673
developera3511852023-06-14 14:12:59 +080013674 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
13675 continue;
13676 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013677 if (fgets(line, sizeof(line), f) == NULL) {
13678 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13679 goto err;
13680 }
developer86035662023-06-28 19:21:12 +080013681 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
13682 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13683 goto err;
13684 }
developera3511852023-06-14 14:12:59 +080013685 if (!strcmp(buf, "above")) {
13686 //40Mhz +
developere40952c2023-06-15 18:46:43 +080013687 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 +080013688 }
13689 else if (!strcmp(buf, "below")) {
13690 //40Mhz -
developere40952c2023-06-15 18:46:43 +080013691 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 +080013692 } else {
13693 //20Mhz
developere40952c2023-06-15 18:46:43 +080013694 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 +080013695 }
developere40952c2023-06-15 18:46:43 +080013696 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13697 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013698 goto err;
developer5b23cd02023-07-19 20:26:03 +080013699 }
developere40952c2023-06-15 18:46:43 +080013700
developer5b23cd02023-07-19 20:26:03 +080013701 if (strstr(line, "BSS") != NULL) { // prevent to get the next neighbor information
13702 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013703 continue;
developer5b23cd02023-07-19 20:26:03 +080013704 }
developera3511852023-06-14 14:12:59 +080013705 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013706 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13707 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013708 goto err;
developer32f2a182023-06-27 19:50:41 +080013709 }
13710 strncat(scan_array[index].ap_SupportedStandards, ",ax",
13711 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13712 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
13713 scan_array[index].ap_OperatingStandards[2] = '\0';
developerc14d83a2023-06-29 20:09:42 +080013714 if (fgets(line, sizeof(line), f) == NULL) {
13715 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13716 goto err;
13717 }
developera3511852023-06-14 14:12:59 +080013718 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013719 if (strstr(line, "HE40/2.4GHz") != NULL) {
13720 len = strlen("11AXHE40PLUS");
13721 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
13722 } else {
13723 len = strlen("11AXHE20");
13724 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
13725 }
13726 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013727 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
13728 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013729 len = strlen("11AXHE80");
13730 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
13731 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013732 ret = fgets(line, sizeof(line), f);
developer5b23cd02023-07-19 20:26:03 +080013733 } else {
13734 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013735 continue;
developer5b23cd02023-07-19 20:26:03 +080013736 }
developer32f2a182023-06-27 19:50:41 +080013737 if (strstr(line, "HE160/5GHz") != NULL) {
13738 len = strlen("11AXHE160");
13739 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
13740 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
13741 }
developera3511852023-06-14 14:12:59 +080013742 }
developer5b23cd02023-07-19 20:26:03 +080013743 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013744 continue;
13745 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013746 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
13747 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013748 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013749 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
13750 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013751 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +080013752 if (sscanf(line, " * Group cipher: %63s", scan_array[index].ap_EncryptionMode) != 1) {
13753 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13754 goto err;
13755 }
developera3511852023-06-14 14:12:59 +080013756 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013757 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
13758 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +080013759 }
13760 }
13761 ret = fgets(line, sizeof(line), f);
13762 }
developer72fb0bb2023-01-11 09:46:29 +080013763
developera3511852023-06-14 14:12:59 +080013764 if (!filter_BSS) {
13765 *output_array_size = index + 1;
13766 } else {
13767 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
13768 *output_array_size = index;
13769 }
13770 *neighbor_ap_array = scan_array;
developer8078acf2023-08-04 18:52:48 +080013771 v_secure_pclose(f);
developera3511852023-06-14 14:12:59 +080013772 free(channels_noise_arr);
13773 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
13774 return RETURN_OK;
developer86035662023-06-28 19:21:12 +080013775err:
developer8078acf2023-08-04 18:52:48 +080013776 v_secure_pclose(f);
developer86035662023-06-28 19:21:12 +080013777 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +080013778 if (scan_array)
13779 free(scan_array);
developer86035662023-06-28 19:21:12 +080013780 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013781}
13782
13783INT wifi_getApAssociatedDeviceStats(
developera3511852023-06-14 14:12:59 +080013784 INT apIndex,
13785 mac_address_t *clientMacAddress,
13786 wifi_associated_dev_stats_t *associated_dev_stats,
13787 u64 *handle)
developer72fb0bb2023-01-11 09:46:29 +080013788{
developera3511852023-06-14 14:12:59 +080013789 wifi_associated_dev_stats_t *dev_stats = associated_dev_stats;
13790 char interface_name[50] = {0};
13791 char cmd[1024] = {0};
13792 char mac_str[18] = {0};
13793 char *key = NULL;
13794 char *val = NULL;
13795 FILE *f = NULL;
13796 char *line = NULL;
13797 size_t len = 0;
developer75bd10c2023-06-27 11:34:08 +080013798 int res;
developer72fb0bb2023-01-11 09:46:29 +080013799
developera3511852023-06-14 14:12:59 +080013800 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
13801 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
13802 return RETURN_ERR;
13803 }
developer72fb0bb2023-01-11 09:46:29 +080013804
developer32f2a182023-06-27 19:50:41 +080013805 res = snprintf(mac_str, sizeof(mac_str), "%x:%x:%x:%x:%x:%x",
13806 (*clientMacAddress)[0], (*clientMacAddress)[1], (*clientMacAddress)[2],
13807 (*clientMacAddress)[3], (*clientMacAddress)[4], (*clientMacAddress)[5]);
developer75bd10c2023-06-27 11:34:08 +080013808 if (os_snprintf_error(sizeof(mac_str), res)) {
13809 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13810 return RETURN_ERR;
13811 }
13812
13813 res = snprintf(cmd, sizeof(cmd), "iw dev %s station get %s | grep 'rx\\|tx' | tr -d '\t'", interface_name, mac_str);
13814 if (os_snprintf_error(sizeof(cmd), res)) {
13815 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13816 return RETURN_ERR;
13817 }
developer8078acf2023-08-04 18:52:48 +080013818 f = v_secure_popen("r", "iw dev %s station get %s | grep 'rx\\|tx' | tr -d '\t'", interface_name, mac_str);
13819 if(f == NULL) {
13820 wifi_dbg_printf("%s: v_secure_popen %s error\n", __func__, cmd);
developera3511852023-06-14 14:12:59 +080013821 return RETURN_ERR;
13822 }
developer72fb0bb2023-01-11 09:46:29 +080013823
developera3511852023-06-14 14:12:59 +080013824 while ((getline(&line, &len, f)) != -1) {
13825 key = strtok(line,":");
developer37646972023-06-29 10:58:43 +080013826 if (key == NULL)
13827 continue;
developera3511852023-06-14 14:12:59 +080013828 val = strtok(NULL,":");
developer37646972023-06-29 10:58:43 +080013829 if (val == NULL)
13830 continue;
developer72fb0bb2023-01-11 09:46:29 +080013831
developerb61d3362023-06-29 14:10:19 +080013832 if(!strncmp(key,"rx bytes",8))
13833 if (sscanf(val, "%llu", &dev_stats->cli_rx_bytes) != 1) {
13834 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080013835 continue;
developerb61d3362023-06-29 14:10:19 +080013836 }
13837 if(!strncmp(key,"tx bytes",8))
13838 if (sscanf(val, "%llu", &dev_stats->cli_tx_bytes) != 1) {
13839 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080013840 continue;
developerb61d3362023-06-29 14:10:19 +080013841 }
developer37646972023-06-29 10:58:43 +080013842 if(!strncmp(key,"rx packets",10)) {
13843 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
13844 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13845 continue;
13846 }
13847 }
13848 if(!strncmp(key,"tx packets",10)) {
13849 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
13850 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13851 continue;
13852 }
13853 }
13854 if(!strncmp(key,"tx retries",10)) {
13855 if (sscanf(val, "%llu", &dev_stats->cli_tx_retries) == EOF) {
13856 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13857 continue;
13858 }
13859 }
13860 if(!strncmp(key,"tx failed",9)) {
13861 if (sscanf(val, "%llu", &dev_stats->cli_tx_errors) == EOF) {
13862 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13863 continue;
13864 }
13865 }
13866 if(!strncmp(key,"rx drop misc",13)) {
13867 if (sscanf(val, "%llu", &dev_stats->cli_rx_errors) == EOF) {
13868 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13869 continue;
13870 }
developerd14dff12023-06-28 22:47:44 +080013871 }
developera3511852023-06-14 14:12:59 +080013872 if(!strncmp(key,"rx bitrate",10)) {
13873 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080013874 if (val == NULL)
13875 continue;
developer37646972023-06-29 10:58:43 +080013876 if (sscanf(val, "%lf", &dev_stats->cli_rx_rate) == EOF) {
13877 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13878 continue;
13879 }
developera3511852023-06-14 14:12:59 +080013880 }
13881 if(!strncmp(key,"tx bitrate",10)) {
13882 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080013883 if (val == NULL)
13884 continue;
developer37646972023-06-29 10:58:43 +080013885 if (sscanf(val, "%lf", &dev_stats->cli_tx_rate) == EOF) {
13886 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13887 continue;
13888 }
developera3511852023-06-14 14:12:59 +080013889 }
13890 }
13891 free(line);
developer8078acf2023-08-04 18:52:48 +080013892 v_secure_pclose(f);
developera3511852023-06-14 14:12:59 +080013893 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013894}
13895
13896INT wifi_getSSIDNameStatus(INT apIndex, CHAR *output_string)
13897{
developera3511852023-06-14 14:12:59 +080013898 char interface_name[IF_NAME_SIZE] = {0};
developer8078acf2023-08-04 18:52:48 +080013899 char buf[32] = {0};
developere40952c2023-06-15 18:46:43 +080013900 int res;
developer72fb0bb2023-01-11 09:46:29 +080013901
developera3511852023-06-14 14:12:59 +080013902 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080013903
developera3511852023-06-14 14:12:59 +080013904 if (NULL == output_string)
13905 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013906
developera3511852023-06-14 14:12:59 +080013907 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
13908 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +080013909
developer8078acf2023-08-04 18:52:48 +080013910 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s get_config | grep ^ssid | cut -d '=' -f2 | tr -d '\\n'", interface_name);
13911 if (res) {
13912 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080013913 }
developer72fb0bb2023-01-11 09:46:29 +080013914
developera3511852023-06-14 14:12:59 +080013915 //size of SSID name restricted to value less than 32 bytes
developere40952c2023-06-15 18:46:43 +080013916 res = snprintf(output_string, 32, "%s", buf);
13917 if (os_snprintf_error(32, res)) {
13918 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13919 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013920 }
developera3511852023-06-14 14:12:59 +080013921 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013922
developera3511852023-06-14 14:12:59 +080013923 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013924}
13925
13926INT wifi_getApMacAddressControlMode(INT apIndex, INT *output_filterMode)
13927{
developer2edaf012023-05-24 14:24:53 +080013928 char *mac_arry_buf = NULL;
13929 INT policy = -1;
13930 INT buf_size = 1024;
developer72fb0bb2023-01-11 09:46:29 +080013931
developer2edaf012023-05-24 14:24:53 +080013932 mac_arry_buf = malloc(buf_size);
13933 if (!mac_arry_buf) {
13934 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
developer7e4a2a62023-04-06 19:56:03 +080013935 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080013936 }
13937 memset(mac_arry_buf, 0, buf_size);
13938 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
13939 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
13940 goto err;
13941 }
13942 /*
13943 mtk format to get policy:
13944 "policy=1
13945 00:11:22:33:44:55
13946 00:11:22:33:44:66
13947 "
13948 */
13949 if (strlen(mac_arry_buf) < strlen("policy=1") || sscanf(mac_arry_buf, "policy=%01d", &policy) != 1) {
13950 wifi_debug(DEBUG_ERROR,"mac_arry_buf(%s) invalid\n", mac_arry_buf);
13951 goto err;
13952 }
13953 if (!(policy >=0 && policy <= 2)){
13954 wifi_debug(DEBUG_ERROR,"policy(%d) is invalid\n", policy);
13955 goto err;
13956 }
13957 *output_filterMode = policy;
13958 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), success\n", *output_filterMode);
13959 free(mac_arry_buf);
13960 mac_arry_buf = NULL;
13961 return RETURN_OK;
13962err:
13963 free(mac_arry_buf);
13964 mac_arry_buf = NULL;
13965 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), fails\n", *output_filterMode);
13966 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013967}
13968
developer2edaf012023-05-24 14:24:53 +080013969
developer72fb0bb2023-01-11 09:46:29 +080013970INT wifi_getApAssociatedDeviceDiagnosticResult2(INT apIndex,wifi_associated_dev2_t **associated_dev_array,UINT *output_array_size)
13971{
developera3511852023-06-14 14:12:59 +080013972 FILE *fp = NULL;
13973 char str[MAX_BUF_SIZE] = {0};
13974 int wificlientindex = 0 ;
13975 int count = 0;
13976 int signalstrength = 0;
13977 int arr[MACADDRESS_SIZE] = {0};
13978 unsigned char mac[MACADDRESS_SIZE] = {0};
13979 UINT wifi_count = 0;
developer8078acf2023-08-04 18:52:48 +080013980
developere40952c2023-06-15 18:46:43 +080013981 int res;
developer72fb0bb2023-01-11 09:46:29 +080013982
developera3511852023-06-14 14:12:59 +080013983 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
13984 *output_array_size = 0;
13985 *associated_dev_array = NULL;
13986 char interface_name[50] = {0};
developer72fb0bb2023-01-11 09:46:29 +080013987
developera3511852023-06-14 14:12:59 +080013988 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
13989 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
13990 return RETURN_ERR;
13991 }
developer72fb0bb2023-01-11 09:46:29 +080013992
developer8078acf2023-08-04 18:52:48 +080013993 fp = v_secure_popen( "r", "iw dev %s station dump | grep %s | wc -l", interface_name, interface_name);
developera3511852023-06-14 14:12:59 +080013994 if (fp == NULL)
13995 {
13996 printf("Failed to run command inside function %s\n",__FUNCTION__ );
13997 return RETURN_ERR;
13998 }
developer72fb0bb2023-01-11 09:46:29 +080013999
developera3511852023-06-14 14:12:59 +080014000 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080014001 if (fgets(str, sizeof(str)-1, fp) == NULL) {
14002 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014003 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080014004 return RETURN_ERR;
14005 }
developera3511852023-06-14 14:12:59 +080014006 wifi_count = (unsigned int) atoi ( str );
14007 *output_array_size = wifi_count;
14008 wifi_dbg_printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
developer8078acf2023-08-04 18:52:48 +080014009 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014010
developera3511852023-06-14 14:12:59 +080014011 if(wifi_count == 0)
14012 {
14013 return RETURN_OK;
14014 }
14015 else
14016 {
14017 wifi_associated_dev2_t* temp = NULL;
14018 temp = (wifi_associated_dev2_t*)calloc(wifi_count, sizeof(wifi_associated_dev2_t));
14019 *associated_dev_array = temp;
14020 if(temp == NULL)
14021 {
14022 printf("Error Statement. Insufficient memory \n");
14023 return RETURN_ERR;
14024 }
developer72fb0bb2023-01-11 09:46:29 +080014025
developer33f13ba2023-07-12 16:19:06 +080014026 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
14027 if (res) {
14028 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080014029 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014030 }
developere40952c2023-06-15 18:46:43 +080014031
developera3511852023-06-14 14:12:59 +080014032 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
14033 if(fp == NULL)
14034 {
14035 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
14036 return RETURN_ERR;
14037 }
developere75ba632023-06-29 16:03:33 +080014038 if (fclose(fp) == EOF) {
14039 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14040 return RETURN_ERR;
14041 }
developer72fb0bb2023-01-11 09:46:29 +080014042
developer8078acf2023-08-04 18:52:48 +080014043 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
developera3511852023-06-14 14:12:59 +080014044 if(fp)
14045 {
14046 for(count =0 ; count < wifi_count; count++)
14047 {
developer86035662023-06-28 19:21:12 +080014048 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14049 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014050 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080014051 return RETURN_ERR;
14052 }
developera3511852023-06-14 14:12:59 +080014053 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
14054 {
14055 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
14056 {
14057 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080014058
developera3511852023-06-14 14:12:59 +080014059 }
14060 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
14061 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]);
14062 }
14063 temp[count].cli_AuthenticationState = 1; //TODO
14064 temp[count].cli_Active = 1; //TODO
14065 }
developer8078acf2023-08-04 18:52:48 +080014066 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014067 }
developer72fb0bb2023-01-11 09:46:29 +080014068
developera3511852023-06-14 14:12:59 +080014069 //Updating RSSI per client
developer8078acf2023-08-04 18:52:48 +080014070 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 +080014071 if(fp)
14072 {
developer8078acf2023-08-04 18:52:48 +080014073 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014074 }
developer8078acf2023-08-04 18:52:48 +080014075 fp = v_secure_popen("r", "cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080014076 if(fp)
14077 {
14078 for(count =0 ; count < wifi_count ;count++)
14079 {
developer86035662023-06-28 19:21:12 +080014080 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14081 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014082 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080014083 return RETURN_ERR;
14084 }
developera3511852023-06-14 14:12:59 +080014085 signalstrength = atoi(str);
14086 temp[count].cli_RSSI = signalstrength;
14087 }
developer8078acf2023-08-04 18:52:48 +080014088 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014089 }
developer72fb0bb2023-01-11 09:46:29 +080014090
14091
developera3511852023-06-14 14:12:59 +080014092 //LastDataDownlinkRate
developer8078acf2023-08-04 18:52:48 +080014093 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 +080014094 if (fp)
14095 {
developer8078acf2023-08-04 18:52:48 +080014096 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014097 }
developer8078acf2023-08-04 18:52:48 +080014098 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080014099 if (fp)
14100 {
developerc14d83a2023-06-29 20:09:42 +080014101 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014102 for (count = 0; count < wifi_count; count++)
14103 {
developer86035662023-06-28 19:21:12 +080014104 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14105 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014106 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080014107 return RETURN_ERR;
14108 }
developerc14d83a2023-06-29 20:09:42 +080014109 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14110 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14111 }
14112 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014113 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
14114 }
developer8078acf2023-08-04 18:52:48 +080014115 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014116 }
developer72fb0bb2023-01-11 09:46:29 +080014117
developera3511852023-06-14 14:12:59 +080014118 //LastDataUplinkRate
developer8078acf2023-08-04 18:52:48 +080014119 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 +080014120 if (fp)
14121 {
developer8078acf2023-08-04 18:52:48 +080014122 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014123 }
developer8078acf2023-08-04 18:52:48 +080014124 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080014125 if (fp)
14126 {
developerc14d83a2023-06-29 20:09:42 +080014127 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014128 for (count = 0; count < wifi_count; count++)
14129 {
developer86035662023-06-28 19:21:12 +080014130 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14131 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014132 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080014133 return RETURN_ERR;
14134 }
developerc14d83a2023-06-29 20:09:42 +080014135 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14136 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14137 }
14138 temp[count].cli_LastDataUplinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014139 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
14140 }
developer8078acf2023-08-04 18:52:48 +080014141 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014142 }
14143 }
14144 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14145 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014146
14147}
14148
14149INT wifi_getSSIDTrafficStats2(INT ssidIndex,wifi_ssidTrafficStats2_t *output_struct)
14150{
developera3511852023-06-14 14:12:59 +080014151 FILE *fp = NULL;
14152 char interface_name[50] = {0};
developer8078acf2023-08-04 18:52:48 +080014153
developera3511852023-06-14 14:12:59 +080014154 char str[256] = {0};
14155 wifi_ssidTrafficStats2_t *out = output_struct;
developer8078acf2023-08-04 18:52:48 +080014156
developerd14dff12023-06-28 22:47:44 +080014157 unsigned int recv;
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 if (!output_struct)
14161 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014162
developera3511852023-06-14 14:12:59 +080014163 memset(out, 0, sizeof(wifi_ssidTrafficStats2_t));
14164 if (wifi_GetInterfaceName(ssidIndex, interface_name) != RETURN_OK)
14165 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080014166
14167
14168
14169
developer72fb0bb2023-01-11 09:46:29 +080014170
developer8078acf2023-08-04 18:52:48 +080014171
14172 fp = v_secure_popen("r", "cat /proc/net/dev | grep %s", interface_name);
developera3511852023-06-14 14:12:59 +080014173 if (fp == NULL) {
developer8078acf2023-08-04 18:52:48 +080014174 wifi_debug(DEBUG_ERROR, "%s: v_secure_popen failed\n", __func__);
developer86035662023-06-28 19:21:12 +080014175 return RETURN_ERR;
14176 }
14177 if (fgets(str, sizeof(str), fp) == NULL) {
14178 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014179 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080014180 return RETURN_ERR;
14181 }
developer5b23cd02023-07-19 20:26:03 +080014182
developer8078acf2023-08-04 18:52:48 +080014183 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014184
developera3511852023-06-14 14:12:59 +080014185 if (strlen(str) == 0) // interface not exist
14186 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014187
developerd14dff12023-06-28 22:47:44 +080014188 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 +080014189 &out->ssid_DiscardedPacketsReceived, &out->ssid_BytesSent, &out->ssid_PacketsSent, &out->ssid_ErrorsSent, &out->ssid_DiscardedPacketsSent);
developerd14dff12023-06-28 22:47:44 +080014190 if (recv != 8) {
14191 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
14192 return RETURN_ERR;
14193 }
developer72fb0bb2023-01-11 09:46:29 +080014194
developera3511852023-06-14 14:12:59 +080014195 memset(str, 0, sizeof(str));
developer75bd10c2023-06-27 11:34:08 +080014196
developer8078acf2023-08-04 18:52:48 +080014197
14198
14199
14200
14201
14202 fp = v_secure_popen("r", "tail -n1 /proc/net/netstat");
developera3511852023-06-14 14:12:59 +080014203 if (fp == NULL) {
developer8078acf2023-08-04 18:52:48 +080014204 wifi_debug(DEBUG_ERROR, "v_secure_popen failed\n");
developera3511852023-06-14 14:12:59 +080014205 return RETURN_ERR;
14206 }
developer86035662023-06-28 19:21:12 +080014207
14208 if (fgets(str, sizeof(str), fp) == NULL) {
14209 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080014210 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080014211 return RETURN_ERR;
14212 }
developer72fb0bb2023-01-11 09:46:29 +080014213
developer37646972023-06-29 10:58:43 +080014214 if (sscanf(str, "%*[^:]: %lu %lu %lu %lu", &out->ssid_MulticastPacketsReceived, &out->ssid_MulticastPacketsSent, &out->ssid_BroadcastPacketsRecevied, \
14215 &out->ssid_BroadcastPacketsSent) == EOF)
14216 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer8078acf2023-08-04 18:52:48 +080014217 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014218
developera3511852023-06-14 14:12:59 +080014219 out->ssid_UnicastPacketsSent = out->ssid_PacketsSent - out->ssid_MulticastPacketsSent - out->ssid_BroadcastPacketsSent - out->ssid_DiscardedPacketsSent;
14220 out->ssid_UnicastPacketsReceived = out->ssid_PacketsReceived - out->ssid_MulticastPacketsReceived - out->ssid_BroadcastPacketsRecevied - out->ssid_DiscardedPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +080014221
developera3511852023-06-14 14:12:59 +080014222 // Not supported
14223 output_struct->ssid_RetransCount = 0;
14224 output_struct->ssid_FailedRetransCount = 0;
14225 output_struct->ssid_RetryCount = 0;
14226 output_struct->ssid_MultipleRetryCount = 0;
14227 output_struct->ssid_ACKFailureCount = 0;
14228 output_struct->ssid_AggregatedPacketCount = 0;
developer72fb0bb2023-01-11 09:46:29 +080014229
developera3511852023-06-14 14:12:59 +080014230 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014231}
14232
developer37ad6bf2023-10-09 11:31:05 +080014233int mtk_get_ap_isolation_callback(struct nl_msg *msg, void *data) {
14234 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14235 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BSS_ATTR_MAX + 1];
14236 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
14237 unsigned char status;
14238 unsigned char *out_status = data;
14239 int err = 0;
14240
14241 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
14242 genlmsg_attrlen(gnlh, 0), NULL);
14243 if (err < 0){
14244 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
14245 return err;
14246 }
14247
14248 if (tb[NL80211_ATTR_VENDOR_DATA]) {
14249 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BSS_ATTR_MAX,
14250 tb[NL80211_ATTR_VENDOR_DATA], NULL);
14251 if (err < 0){
14252 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
14253 return err;
14254 }
14255
14256 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_ISOLATION]) {
14257 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_ISOLATION]);
14258 if (status == 0) {
14259 wifi_debug(DEBUG_INFO, "disabled\n");
14260 } else {
14261 wifi_debug(DEBUG_INFO, "enabled\n");
14262 }
14263 *out_status = status;
14264 }
14265 }
14266 return 0;
14267
14268}
14269
developer72fb0bb2023-01-11 09:46:29 +080014270//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).
14271INT wifi_getApIsolationEnable(INT apIndex, BOOL *output)
14272{
developer37ad6bf2023-10-09 11:31:05 +080014273 char inf_name[IF_NAME_SIZE] = {0};
14274 unsigned int if_idx = 0;
14275 int ret = -1;
14276 struct unl unl_ins;
14277 struct nl_msg *msg = NULL;
14278 struct nlattr * msg_data = NULL;
14279 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +080014280
developer37ad6bf2023-10-09 11:31:05 +080014281 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
14282 return RETURN_ERR;
14283 if_idx = if_nametoindex(inf_name);
developer75bd10c2023-06-27 11:34:08 +080014284
developer37ad6bf2023-10-09 11:31:05 +080014285 /*init mtk nl80211 vendor cmd*/
14286 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
14287 param.if_type = NL80211_ATTR_IFINDEX;
14288 param.if_idx = if_idx;
developer72fb0bb2023-01-11 09:46:29 +080014289
developer37ad6bf2023-10-09 11:31:05 +080014290 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14291 if (ret) {
14292 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14293 return RETURN_ERR;
14294 }
14295 /*add mtk vendor cmd data*/
14296 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_ISOLATION, 0xf)) {
14297 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", MTK_NL80211_VENDOR_ATTR_AP_AMSDU_EN);
14298 nlmsg_free(msg);
14299 goto err;
14300 }
developer72fb0bb2023-01-11 09:46:29 +080014301
developer37ad6bf2023-10-09 11:31:05 +080014302 /*send mtk nl80211 vendor msg*/
14303 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_get_ap_isolation_callback, output);
14304 if (ret) {
14305 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14306 goto err;
14307 }
14308 /*deinit mtk nl80211 vendor msg*/
14309 mtk_nl80211_deint(&unl_ins);
14310 wifi_debug(DEBUG_INFO,"send cmd success, get output_bool:%d\n", *output);
14311 return RETURN_OK;
14312err:
14313 mtk_nl80211_deint(&unl_ins);
14314 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
14315 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014316}
14317
14318INT wifi_setApIsolationEnable(INT apIndex, BOOL enable)
14319{
developer37ad6bf2023-10-09 11:31:05 +080014320 char inf_name[IF_NAME_SIZE] = {0};
14321 unsigned int if_idx = 0;
14322 int ret = -1;
14323 struct unl unl_ins;
14324 struct nl_msg *msg = NULL;
14325 struct nlattr * msg_data = NULL;
14326 struct mtk_nl80211_param nl_param;
developer72fb0bb2023-01-11 09:46:29 +080014327
developer37ad6bf2023-10-09 11:31:05 +080014328 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
14329 return RETURN_ERR;
14330 if_idx = if_nametoindex(inf_name);
developer72fb0bb2023-01-11 09:46:29 +080014331
developer37ad6bf2023-10-09 11:31:05 +080014332 nl_param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
14333 nl_param.if_type = NL80211_ATTR_IFINDEX;
14334 nl_param.if_idx = if_idx;
14335 /*init mtk nl80211 vendor cmd*/
14336 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &nl_param);
14337 if (ret) {
14338 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14339 return RETURN_ERR;
14340 }
14341 /*add mtk vendor cmd data*/
14342 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_ISOLATION, enable)) {
14343 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", MTK_NL80211_VENDOR_ATTR_AP_ISOLATION);
14344 nlmsg_free(msg);
14345 goto err;
14346 }
developer75bd10c2023-06-27 11:34:08 +080014347
developer37ad6bf2023-10-09 11:31:05 +080014348 /*send mtk nl80211 vendor msg*/
14349 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14350 if (ret) {
14351 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14352 goto err;
14353 }
14354 /*deinit mtk nl80211 vendor msg*/
14355 mtk_nl80211_deint(&unl_ins);
14356 return RETURN_OK;
14357err:
14358 mtk_nl80211_deint(&unl_ins);
14359 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
14360 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014361}
14362
14363INT wifi_getApManagementFramePowerControl(INT apIndex, INT *output_dBm)
14364{
developera3511852023-06-14 14:12:59 +080014365 char mgmtpwr_file[32] = {0};
developer8078acf2023-08-04 18:52:48 +080014366
developera3511852023-06-14 14:12:59 +080014367 char buf[32]={0};
developere40952c2023-06-15 18:46:43 +080014368 int res;
developerc14d83a2023-06-29 20:09:42 +080014369 long int tmp;
developera1255e42023-05-13 17:45:02 +080014370
developera3511852023-06-14 14:12:59 +080014371 if (NULL == output_dBm)
14372 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080014373 res = snprintf(mgmtpwr_file, sizeof(mgmtpwr_file), "%s%d.txt", MGMT_POWER_CTRL, apIndex);
14374 if (os_snprintf_error(sizeof(mgmtpwr_file), res)) {
14375 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14376 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014377 }
developere40952c2023-06-15 18:46:43 +080014378
developer8078acf2023-08-04 18:52:48 +080014379 res = _syscmd_secure(buf, sizeof(buf), "cat %s 2> /dev/null", mgmtpwr_file);
14380 if (res) {
14381 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080014382 }
developer5b23cd02023-07-19 20:26:03 +080014383 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080014384 if (hal_strtol(buf, 10, &tmp) < 0) {
14385 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080014386 }
developerc14d83a2023-06-29 20:09:42 +080014387 *output_dBm = tmp;
developerd14dff12023-06-28 22:47:44 +080014388 } else
developera1255e42023-05-13 17:45:02 +080014389 *output_dBm = 23;
developera3511852023-06-14 14:12:59 +080014390 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014391}
14392
14393INT wifi_setApManagementFramePowerControl(INT wlanIndex, INT dBm)
14394{
developera1255e42023-05-13 17:45:02 +080014395 char interface_name[16] = {0};
developera1255e42023-05-13 17:45:02 +080014396 char mgmt_pwr_file[128]={0};
14397 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +080014398 int if_idx, ret = 0;
14399 struct nl_msg *msg = NULL;
14400 struct nlattr * msg_data = NULL;
14401 struct mtk_nl80211_param param;
14402 struct unl unl_ins;
14403 char power[16] = {0};
developere40952c2023-06-15 18:46:43 +080014404 int res;
developera1255e42023-05-13 17:45:02 +080014405
14406 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14407
14408 if (wifi_GetInterfaceName(wlanIndex, interface_name) != RETURN_OK)
14409 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +080014410
14411 if_idx = if_nametoindex(interface_name);
14412 /*init mtk nl80211 vendor cmd*/
14413 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
14414 param.if_type = NL80211_ATTR_IFINDEX;
14415 param.if_idx = if_idx;
14416
14417 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14418 if (ret) {
14419 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14420 return RETURN_ERR;
14421 }
14422
14423 /*add mtk vendor cmd data*/
developere40952c2023-06-15 18:46:43 +080014424 res = snprintf(power, sizeof(power), "%d", dBm);
14425 if (os_snprintf_error(sizeof(power), res)) {
14426 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14427 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014428 }
developere40952c2023-06-15 18:46:43 +080014429
developerfead3972023-05-25 20:15:02 +080014430 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_MGMT, strlen(power), power)) {
14431 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14432 nlmsg_free(msg);
14433 goto err;
14434 }
14435
14436 /*send mtk nl80211 vendor msg*/
14437 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14438 if (ret) {
14439 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14440 goto err;
14441 }
14442
14443 /*deinit mtk nl80211 vendor msg*/
14444 mtk_nl80211_deint(&unl_ins);
14445 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14446
developere40952c2023-06-15 18:46:43 +080014447 res = snprintf(mgmt_pwr_file, sizeof(mgmt_pwr_file), "%s%d.txt", MGMT_POWER_CTRL, wlanIndex);
14448 if (os_snprintf_error(sizeof(mgmt_pwr_file), res)) {
14449 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14450 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014451 }
developere40952c2023-06-15 18:46:43 +080014452
developera1255e42023-05-13 17:45:02 +080014453 f = fopen(mgmt_pwr_file, "w");
14454 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +080014455 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera1255e42023-05-13 17:45:02 +080014456 return RETURN_ERR;
14457 }
14458 fprintf(f, "%d", dBm);
developerc14d83a2023-06-29 20:09:42 +080014459 if (fclose(f) == EOF)
14460 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14461
developer72fb0bb2023-01-11 09:46:29 +080014462 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +080014463err:
14464 mtk_nl80211_deint(&unl_ins);
14465 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14466 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014467}
14468INT wifi_getRadioDcsChannelMetrics(INT radioIndex,wifi_channelMetrics_t *input_output_channelMetrics_array,INT size)
14469{
developera3511852023-06-14 14:12:59 +080014470 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014471}
14472INT wifi_setRadioDcsDwelltime(INT radioIndex, INT ms)
14473{
developera3511852023-06-14 14:12:59 +080014474 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014475}
14476INT wifi_getRadioDcsDwelltime(INT radioIndex, INT *ms)
14477{
developera3511852023-06-14 14:12:59 +080014478 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014479}
14480INT wifi_setRadioDcsScanning(INT radioIndex, BOOL enable)
14481{
developera3511852023-06-14 14:12:59 +080014482 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014483}
developer9f2358c2023-09-22 18:42:12 +080014484
developer72fb0bb2023-01-11 09:46:29 +080014485INT wifi_setBSSTransitionActivation(UINT apIndex, BOOL activate)
14486{
developera3511852023-06-14 14:12:59 +080014487 char config_file[MAX_BUF_SIZE] = {0};
14488 struct params list;
developere40952c2023-06-15 18:46:43 +080014489 int res;
developer9f2358c2023-09-22 18:42:12 +080014490 bool temp_multiple_set = multiple_set;
developer72fb0bb2023-01-11 09:46:29 +080014491
developera3511852023-06-14 14:12:59 +080014492 list.name = "bss_transition";
14493 list.value = activate?"1":"0";
developere40952c2023-06-15 18:46:43 +080014494 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
14495 if (os_snprintf_error(sizeof(config_file), res)) {
14496 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14497 return RETURN_ERR;
14498 }
developera3511852023-06-14 14:12:59 +080014499 wifi_hostapdWrite(config_file, &list, 1);
developer9f2358c2023-09-22 18:42:12 +080014500 multiple_set = false;
14501 wifi_hostapdProcessUpdate(apIndex, &list, 1);
14502 wifi_quick_reload_ap(apIndex);
14503 multiple_set = temp_multiple_set;
developer72fb0bb2023-01-11 09:46:29 +080014504
developera3511852023-06-14 14:12:59 +080014505 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014506}
developer9f2358c2023-09-22 18:42:12 +080014507
14508
developer72fb0bb2023-01-11 09:46:29 +080014509wifi_apAuthEvent_callback apAuthEvent_cb = NULL;
14510
14511void wifi_apAuthEvent_callback_register(wifi_apAuthEvent_callback callback_proc)
14512{
developera3511852023-06-14 14:12:59 +080014513 return;
developer72fb0bb2023-01-11 09:46:29 +080014514}
14515
14516INT wifi_setApCsaDeauth(INT apIndex, INT mode)
14517{
developera3511852023-06-14 14:12:59 +080014518 // TODO Implement me!
14519 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014520}
14521
14522INT wifi_setApScanFilter(INT apIndex, INT mode, CHAR *essid)
14523{
developera3511852023-06-14 14:12:59 +080014524 char file_name[128] = {0};
14525 FILE *f = NULL;
14526 int max_num_radios = 0;
developer75bd10c2023-06-27 11:34:08 +080014527 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080014528
developera3511852023-06-14 14:12:59 +080014529 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014530
developera3511852023-06-14 14:12:59 +080014531 wifi_getMaxRadioNumber(&max_num_radios);
14532 if (essid == NULL || strlen(essid) == 0 || apIndex == -1) {
14533 for (int index = 0; index < max_num_radios; index++) {
developere40952c2023-06-15 18:46:43 +080014534 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, index);
14535 if (os_snprintf_error(sizeof(file_name), res)) {
14536 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14537 return RETURN_ERR;
14538 }
14539
developera3511852023-06-14 14:12:59 +080014540 f = fopen(file_name, "w");
14541 if (f == NULL)
14542 return RETURN_ERR;
14543 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014544 if (mode) {
14545 ret = fprintf(f, "%s", essid);
14546 if (ret < 0)
14547 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14548 }
developer72fb0bb2023-01-11 09:46:29 +080014549
developerd14dff12023-06-28 22:47:44 +080014550 if (fclose(f) != 0) {
14551 wifi_debug(DEBUG_ERROR, "fclose fail\n");
14552 return RETURN_ERR;
14553 }
developera3511852023-06-14 14:12:59 +080014554 }
14555 } else { // special case, need to set AP's SSID as filter for each radio.
developere40952c2023-06-15 18:46:43 +080014556 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, apIndex);
14557 if (os_snprintf_error(sizeof(file_name), res)) {
14558 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14559 return RETURN_ERR;
14560 }
14561
developera3511852023-06-14 14:12:59 +080014562 f = fopen(file_name, "w");
14563 if (f == NULL)
14564 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014565
developera3511852023-06-14 14:12:59 +080014566 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014567 if (mode) {
14568 ret = fprintf(f, "%s", essid);
14569 if (ret < 0)
14570 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14571 }
developer72fb0bb2023-01-11 09:46:29 +080014572
developer37646972023-06-29 10:58:43 +080014573 if (fclose(f) == EOF) {
14574 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14575 return RETURN_ERR;
14576 }
developera3511852023-06-14 14:12:59 +080014577 }
developer72fb0bb2023-01-11 09:46:29 +080014578
developera3511852023-06-14 14:12:59 +080014579 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14580 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014581}
14582
14583INT wifi_pushRadioChannel(INT radioIndex, UINT channel)
14584{
developera3511852023-06-14 14:12:59 +080014585 // TODO Implement me!
14586 //Apply wifi_pushRadioChannel() instantly
14587 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014588}
14589
14590INT wifi_setRadioStatsEnable(INT radioIndex, BOOL enable)
14591{
developera3511852023-06-14 14:12:59 +080014592 // TODO Implement me!
14593 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014594}
14595
developer72fb0bb2023-01-11 09:46:29 +080014596
developera3511852023-06-14 14:12:59 +080014597static int tidStats_callback(struct nl_msg *msg, void *arg) {
14598 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14599 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
14600 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
14601 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1],*tidattr;
14602 int rem , tid_index = 0;
developer72fb0bb2023-01-11 09:46:29 +080014603
developera3511852023-06-14 14:12:59 +080014604 wifi_associated_dev_tid_stats_t *out = (wifi_associated_dev_tid_stats_t*)arg;
14605 wifi_associated_dev_tid_entry_t *stats_entry;
developer72fb0bb2023-01-11 09:46:29 +080014606
developera3511852023-06-14 14:12:59 +080014607 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
14608 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
14609 };
14610 static struct nla_policy tid_policy[NL80211_TID_STATS_MAX + 1] = {
14611 [NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
14612 };
developer72fb0bb2023-01-11 09:46:29 +080014613
developera3511852023-06-14 14:12:59 +080014614 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
14615 genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080014616
developer72fb0bb2023-01-11 09:46:29 +080014617
developera3511852023-06-14 14:12:59 +080014618 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014619 wifi_debug(DEBUG_ERROR, "station stats missing!\n");
developera3511852023-06-14 14:12:59 +080014620 return NL_SKIP;
14621 }
developer72fb0bb2023-01-11 09:46:29 +080014622
developera3511852023-06-14 14:12:59 +080014623 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
14624 tb[NL80211_ATTR_STA_INFO],
14625 stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014626 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014627 return NL_SKIP;
14628 }
developer72fb0bb2023-01-11 09:46:29 +080014629
developera3511852023-06-14 14:12:59 +080014630 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14631 nla_for_each_nested(tidattr, sinfo[NL80211_STA_INFO_TID_STATS], rem)
14632 {
14633 stats_entry = &out->tid_array[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014634
developera3511852023-06-14 14:12:59 +080014635 stats_entry->tid = tid_index;
14636 stats_entry->ac = _tid_ac_index_get[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014637
developera3511852023-06-14 14:12:59 +080014638 if(sinfo[NL80211_STA_INFO_TID_STATS])
14639 {
14640 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,tidattr, tid_policy)) {
14641 printf("failed to parse nested stats attributes!");
14642 return NL_SKIP;
14643 }
14644 }
14645 if(stats_info[NL80211_TID_STATS_TX_MSDU])
14646 stats_entry->num_msdus = (unsigned long long)nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
developer72fb0bb2023-01-11 09:46:29 +080014647
developera3511852023-06-14 14:12:59 +080014648 if(tid_index < (PS_MAX_TID - 1))
14649 tid_index++;
14650 }
14651 }
14652 //ToDo: sum_time_ms, ewma_time_ms
14653 return NL_SKIP;
14654}
developer72fb0bb2023-01-11 09:46:29 +080014655
developera3511852023-06-14 14:12:59 +080014656INT wifi_getApAssociatedDeviceTidStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_tid_stats_t *tid_stats, ULLONG *handle)
14657{
14658 Netlink nl;
14659 char if_name[IF_NAME_SIZE];
14660 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014661 int res;
developer72fb0bb2023-01-11 09:46:29 +080014662
developera3511852023-06-14 14:12:59 +080014663 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
14664 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014665
developere40952c2023-06-15 18:46:43 +080014666 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
14667 if (os_snprintf_error(sizeof(if_name), res)) {
14668 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14669 return RETURN_ERR;
14670 }
developer72fb0bb2023-01-11 09:46:29 +080014671
developera3511852023-06-14 14:12:59 +080014672 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080014673
developera3511852023-06-14 14:12:59 +080014674 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080014675 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080014676 return -1;
14677 }
developer72fb0bb2023-01-11 09:46:29 +080014678
developera3511852023-06-14 14:12:59 +080014679 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080014680
developera3511852023-06-14 14:12:59 +080014681 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080014682 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080014683 nlfree(&nl);
14684 return -2;
14685 }
developer72fb0bb2023-01-11 09:46:29 +080014686
developera3511852023-06-14 14:12:59 +080014687 genlmsg_put(msg,
14688 NL_AUTO_PID,
14689 NL_AUTO_SEQ,
14690 nl.id,
14691 0,
14692 0,
14693 NL80211_CMD_GET_STATION,
14694 0);
developer72fb0bb2023-01-11 09:46:29 +080014695
developera3511852023-06-14 14:12:59 +080014696 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
14697 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
14698 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,tidStats_callback,tid_stats);
14699 nl_send_auto_complete(nl.socket, msg);
14700 nl_recvmsgs(nl.socket, nl.cb);
14701 nlmsg_free(msg);
14702 nlfree(&nl);
14703 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014704}
14705
14706
14707INT wifi_startNeighborScan(INT apIndex, wifi_neighborScanMode_t scan_mode, INT dwell_time, UINT chan_num, UINT *chan_list)
14708{
developera3511852023-06-14 14:12:59 +080014709 char interface_name[16] = {0};
developera3511852023-06-14 14:12:59 +080014710 char buf[128]={0};
14711 int freq = 0;
developere40952c2023-06-15 18:46:43 +080014712 int res;
developer72fb0bb2023-01-11 09:46:29 +080014713
developera3511852023-06-14 14:12:59 +080014714 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014715
developera3511852023-06-14 14:12:59 +080014716 // full mode is used to scan all channels.
14717 // multiple channels is ambiguous, iw can not set multiple frequencies in one time.
14718 if (scan_mode != WIFI_RADIO_SCAN_MODE_FULL)
14719 ieee80211_channel_to_frequency(chan_list[0], &freq);
developer72fb0bb2023-01-11 09:46:29 +080014720
developera3511852023-06-14 14:12:59 +080014721 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
14722 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014723
developera3511852023-06-14 14:12:59 +080014724 if (freq)
developer8078acf2023-08-04 18:52:48 +080014725 res = _syscmd_secure(buf, sizeof(buf), "iw dev %s scan trigger duration %d freq %d", interface_name, dwell_time, freq);
developera3511852023-06-14 14:12:59 +080014726 else
developer8078acf2023-08-04 18:52:48 +080014727 res = _syscmd_secure(buf, sizeof(buf), "iw dev %s scan trigger duration %d", interface_name, dwell_time);
14728 if (res) {
14729 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080014730 }
developer72fb0bb2023-01-11 09:46:29 +080014731
developera3511852023-06-14 14:12:59 +080014732 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014733
developera3511852023-06-14 14:12:59 +080014734 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014735}
14736
14737
14738INT wifi_steering_setGroup(UINT steeringgroupIndex, wifi_steering_apConfig_t *cfg_2, wifi_steering_apConfig_t *cfg_5)
14739{
developera3511852023-06-14 14:12:59 +080014740 // TODO Implement me!
14741 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014742}
14743
14744INT wifi_steering_clientSet(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_steering_clientConfig_t *config)
14745{
developera3511852023-06-14 14:12:59 +080014746 // TODO Implement me!
14747 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014748}
14749
14750INT wifi_steering_clientRemove(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14751{
developera3511852023-06-14 14:12:59 +080014752 // TODO Implement me!
14753 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014754}
14755
14756INT wifi_steering_clientMeasure(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14757{
developera3511852023-06-14 14:12:59 +080014758 // TODO Implement me!
14759 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014760}
14761
14762INT wifi_steering_clientDisconnect(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_disconnectType_t type, UINT reason)
14763{
developera3511852023-06-14 14:12:59 +080014764 // TODO Implement me!
14765 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014766}
14767
14768INT wifi_steering_eventRegister(wifi_steering_eventCB_t event_cb)
14769{
developera3511852023-06-14 14:12:59 +080014770 // TODO Implement me!
14771 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014772}
14773
14774INT wifi_steering_eventUnregister(void)
14775{
developera3511852023-06-14 14:12:59 +080014776 // TODO Implement me!
14777 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014778}
14779
14780INT wifi_delApAclDevices(INT apIndex)
14781{
developer7e4a2a62023-04-06 19:56:03 +080014782 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +080014783 struct unl unl_ins;
14784 int if_idx = 0, ret = 0;
14785 struct nl_msg *msg = NULL;
14786 struct nlattr * msg_data = NULL;
14787 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +080014788
developer7e4a2a62023-04-06 19:56:03 +080014789 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
14790 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080014791 if_idx = if_nametoindex(inf_name);
14792 if (!if_idx) {
14793 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
14794 return RETURN_ERR;
14795 }
14796 /*init mtk nl80211 vendor cmd*/
14797 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
14798 param.if_type = NL80211_ATTR_IFINDEX;
14799 param.if_idx = if_idx;
14800 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14801 if (ret) {
14802 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14803 return RETURN_ERR;
14804 }
14805 /*add mtk vendor cmd data*/
14806 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_CLEAR_ALL)) {
14807 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14808 nlmsg_free(msg);
14809 goto err;
14810 }
14811 /*send mtk nl80211 vendor msg*/
14812 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14813 if (ret) {
14814 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14815 goto err;
14816 }
14817 /*deinit mtk nl80211 vendor msg*/
14818 mtk_nl80211_deint(&unl_ins);
14819 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14820 return RETURN_OK;
14821err:
14822 mtk_nl80211_deint(&unl_ins);
14823 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14824 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014825
developera3511852023-06-14 14:12:59 +080014826 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014827}
14828
developer72fb0bb2023-01-11 09:46:29 +080014829static int rxStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080014830 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14831 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080014832 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
14833 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080014834 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
14835 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080014836
developera3511852023-06-14 14:12:59 +080014837 nla_parse(tb,
14838 NL80211_ATTR_MAX,
14839 genlmsg_attrdata(gnlh, 0),
14840 genlmsg_attrlen(gnlh, 0),
14841 NULL);
developer72fb0bb2023-01-11 09:46:29 +080014842
developera3511852023-06-14 14:12:59 +080014843 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014844 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080014845 return NL_SKIP;
14846 }
developer72fb0bb2023-01-11 09:46:29 +080014847
developera3511852023-06-14 14:12:59 +080014848 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014849 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014850 return NL_SKIP;
14851 }
14852 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080014853
developera3511852023-06-14 14:12:59 +080014854 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080014855
developera3511852023-06-14 14:12:59 +080014856 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
14857 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy )) {
developer75bd10c2023-06-27 11:34:08 +080014858 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080014859 return NL_SKIP;
14860 }
14861 }
developer72fb0bb2023-01-11 09:46:29 +080014862
developera3511852023-06-14 14:12:59 +080014863 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14864 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
14865 printf("failed to parse nested stats attributes!");
14866 return NL_SKIP;
14867 }
14868 }
14869 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
developer72fb0bb2023-01-11 09:46:29 +080014870
developera3511852023-06-14 14:12:59 +080014871 if( nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]) )
14872 {
14873 printf("Type is VHT\n");
14874 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
14875 ((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 +080014876
developera3511852023-06-14 14:12:59 +080014877 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
14878 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 1;
14879 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
14880 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14881 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
14882 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14883 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
14884 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14885 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]) )
14886 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
14887 } else {
14888 printf(" OFDM or CCK \n");
14889 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
14890 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->nss = 0;
14891 }
14892 }
developer72fb0bb2023-01-11 09:46:29 +080014893
developera3511852023-06-14 14:12:59 +080014894 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
developereff896f2023-05-29 14:52:55 +080014895 if(rinfo[NL80211_RATE_INFO_MCS])
14896 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
14897 }
developera3511852023-06-14 14:12:59 +080014898 if (sinfo[NL80211_STA_INFO_RX_BYTES64])
developereff896f2023-05-29 14:52:55 +080014899 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]);
14900 else if (sinfo[NL80211_STA_INFO_RX_BYTES])
14901 ((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 +080014902
developera3511852023-06-14 14:12:59 +080014903 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14904 if (stats_info[NL80211_TID_STATS_RX_MSDU])
developereff896f2023-05-29 14:52:55 +080014905 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_RX_MSDU]);
14906 }
developer72fb0bb2023-01-11 09:46:29 +080014907
developereff896f2023-05-29 14:52:55 +080014908 if (sinfo[NL80211_STA_INFO_SIGNAL])
14909 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->rssi_combined = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
14910 //Assigning 0 for RETRIES ,PPDUS and MPDUS as we dont have rx retries attribute in libnl_3.3.0
14911 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->retries = 0;
14912 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->ppdus = 0;
14913 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = 0;
14914 //rssi_array need to be filled
14915 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080014916}
developer72fb0bb2023-01-11 09:46:29 +080014917
14918INT wifi_getApAssociatedDeviceRxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_rx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
14919{
developera3511852023-06-14 14:12:59 +080014920 Netlink nl;
14921 char if_name[32];
14922 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
14923 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014924
developera3511852023-06-14 14:12:59 +080014925 *output_array_size = sizeof(wifi_associated_dev_rate_info_rx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080014926
developera3511852023-06-14 14:12:59 +080014927 if (*output_array_size <= 0)
14928 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014929
developera3511852023-06-14 14:12:59 +080014930 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080014931
developera3511852023-06-14 14:12:59 +080014932 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080014933 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080014934 return 0;
14935 }
developer72fb0bb2023-01-11 09:46:29 +080014936
developera3511852023-06-14 14:12:59 +080014937 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080014938
developera3511852023-06-14 14:12:59 +080014939 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080014940 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080014941 nlfree(&nl);
14942 return 0;
14943 }
developer72fb0bb2023-01-11 09:46:29 +080014944
developera3511852023-06-14 14:12:59 +080014945 genlmsg_put(msg,
14946 NL_AUTO_PID,
14947 NL_AUTO_SEQ,
14948 nl.id,
14949 0,
14950 0,
14951 NL80211_CMD_GET_STATION,
14952 0);
developer72fb0bb2023-01-11 09:46:29 +080014953
developera3511852023-06-14 14:12:59 +080014954 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, *clientMacAddress);
14955 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
14956 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, rxStatsInfo_callback, stats_array);
14957 nl_send_auto_complete(nl.socket, msg);
14958 nl_recvmsgs(nl.socket, nl.cb);
14959 nlmsg_free(msg);
14960 nlfree(&nl);
14961 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014962}
14963
developer72fb0bb2023-01-11 09:46:29 +080014964static int txStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080014965 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14966 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080014967 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
14968 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080014969 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
14970 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080014971
developera3511852023-06-14 14:12:59 +080014972 nla_parse(tb,
14973 NL80211_ATTR_MAX,
14974 genlmsg_attrdata(gnlh, 0),
14975 genlmsg_attrlen(gnlh, 0),
14976 NULL);
developer72fb0bb2023-01-11 09:46:29 +080014977
developera3511852023-06-14 14:12:59 +080014978 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014979 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080014980 return NL_SKIP;
14981 }
developer72fb0bb2023-01-11 09:46:29 +080014982
developera3511852023-06-14 14:12:59 +080014983 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014984 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014985 return NL_SKIP;
14986 }
developer72fb0bb2023-01-11 09:46:29 +080014987
developera3511852023-06-14 14:12:59 +080014988 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080014989
developera3511852023-06-14 14:12:59 +080014990 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080014991
developera3511852023-06-14 14:12:59 +080014992 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
14993 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014994 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080014995 return NL_SKIP;
14996 }
14997 }
developer72fb0bb2023-01-11 09:46:29 +080014998
developera3511852023-06-14 14:12:59 +080014999 if(sinfo[NL80211_STA_INFO_TID_STATS])
15000 {
15001 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
15002 printf("failed to parse nested stats attributes!");
15003 return NL_SKIP;
15004 }
15005 }
15006 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
15007 if(nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]))
15008 {
15009 printf("Type is VHT\n");
15010 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
15011 ((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 +080015012
developera3511852023-06-14 14:12:59 +080015013 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
15014 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 1;
15015 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
15016 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15017 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
15018 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15019 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
15020 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15021 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]))
15022 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15023 }
15024 else
15025 {
15026 printf(" OFDM or CCK \n");
15027 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15028 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->nss = 0;
15029 }
15030 }
developer72fb0bb2023-01-11 09:46:29 +080015031
developera3511852023-06-14 14:12:59 +080015032 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
15033 if(rinfo[NL80211_RATE_INFO_MCS])
15034 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
15035 }
developer72fb0bb2023-01-11 09:46:29 +080015036
developera3511852023-06-14 14:12:59 +080015037 if(sinfo[NL80211_STA_INFO_TX_BYTES64])
15038 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]);
15039 else if (sinfo[NL80211_STA_INFO_TX_BYTES])
15040 ((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 +080015041
developera3511852023-06-14 14:12:59 +080015042 //Assigning 0 for mpdus and ppdus , as we do not have attributes in netlink
15043 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
15044 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
developer72fb0bb2023-01-11 09:46:29 +080015045
developera3511852023-06-14 14:12:59 +080015046 if(sinfo[NL80211_STA_INFO_TID_STATS]) {
15047 if(stats_info[NL80211_TID_STATS_TX_MSDU])
15048 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
15049 }
developer72fb0bb2023-01-11 09:46:29 +080015050
developera3511852023-06-14 14:12:59 +080015051 if(sinfo[NL80211_STA_INFO_TX_RETRIES])
15052 ((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 +080015053
developera3511852023-06-14 14:12:59 +080015054 if(sinfo[NL80211_STA_INFO_TX_FAILED] && sinfo[NL80211_STA_INFO_TX_PACKETS])
15055 ((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 +080015056
developera3511852023-06-14 14:12:59 +080015057 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015058}
developer72fb0bb2023-01-11 09:46:29 +080015059
15060INT wifi_getApAssociatedDeviceTxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_tx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
15061{
developera3511852023-06-14 14:12:59 +080015062 Netlink nl;
15063 char if_name[IF_NAME_SIZE];
15064 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015065 int res;
15066
developera3511852023-06-14 14:12:59 +080015067 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
15068 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015069
developera3511852023-06-14 14:12:59 +080015070 *output_array_size = sizeof(wifi_associated_dev_rate_info_tx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080015071
developera3511852023-06-14 14:12:59 +080015072 if (*output_array_size <= 0)
15073 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015074
developere40952c2023-06-15 18:46:43 +080015075 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
15076 if (os_snprintf_error(sizeof(if_name), res)) {
15077 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15078 return RETURN_ERR;
15079 }
developer72fb0bb2023-01-11 09:46:29 +080015080
developera3511852023-06-14 14:12:59 +080015081 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015082
developera3511852023-06-14 14:12:59 +080015083 if(nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080015084 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015085 return 0;
15086 }
developer72fb0bb2023-01-11 09:46:29 +080015087
developera3511852023-06-14 14:12:59 +080015088 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015089
developera3511852023-06-14 14:12:59 +080015090 if(!msg) {
developer75bd10c2023-06-27 11:34:08 +080015091 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015092 nlfree(&nl);
15093 return 0;
15094 }
developer72fb0bb2023-01-11 09:46:29 +080015095
developera3511852023-06-14 14:12:59 +080015096 genlmsg_put(msg,
15097 NL_AUTO_PID,
15098 NL_AUTO_SEQ,
15099 nl.id,
15100 0,
15101 0,
15102 NL80211_CMD_GET_STATION,
15103 0);
developer72fb0bb2023-01-11 09:46:29 +080015104
developera3511852023-06-14 14:12:59 +080015105 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
15106 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15107 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, txStatsInfo_callback, stats_array);
15108 nl_send_auto_complete(nl.socket, msg);
15109 nl_recvmsgs(nl.socket, nl.cb);
15110 nlmsg_free(msg);
15111 nlfree(&nl);
15112 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015113}
15114
15115INT wifi_getBSSTransitionActivation(UINT apIndex, BOOL *activate)
15116{
developera3511852023-06-14 14:12:59 +080015117 // TODO Implement me!
15118 char buf[MAX_BUF_SIZE] = {0};
15119 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015120 int res;
developer72fb0bb2023-01-11 09:46:29 +080015121
developere40952c2023-06-15 18:46:43 +080015122 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15123 if (os_snprintf_error(sizeof(config_file), res)) {
15124 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15125 return RETURN_ERR;
15126 }
developera3511852023-06-14 14:12:59 +080015127 wifi_hostapdRead(config_file, "bss_transition", buf, sizeof(buf));
15128 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015129
developera3511852023-06-14 14:12:59 +080015130 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015131}
15132
15133INT wifi_setNeighborReportActivation(UINT apIndex, BOOL activate)
15134{
developera3511852023-06-14 14:12:59 +080015135 char config_file[MAX_BUF_SIZE] = {0};
15136 struct params list;
developer75bd10c2023-06-27 11:34:08 +080015137 int res;
developer9f2358c2023-09-22 18:42:12 +080015138 bool temp_multiple_set = multiple_set;
developer72fb0bb2023-01-11 09:46:29 +080015139
developera3511852023-06-14 14:12:59 +080015140 list.name = "rrm_neighbor_report";
15141 list.value = activate?"1":"0";
developer75bd10c2023-06-27 11:34:08 +080015142 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15143 if (os_snprintf_error(sizeof(config_file), res)) {
15144 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15145 return RETURN_ERR;
15146 }
developer9f2358c2023-09-22 18:42:12 +080015147
developera3511852023-06-14 14:12:59 +080015148 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080015149
developer9f2358c2023-09-22 18:42:12 +080015150 multiple_set = false;
15151 wifi_hostapdProcessUpdate(apIndex, &list, 1);
15152 wifi_quick_reload_ap(apIndex);
15153 multiple_set = temp_multiple_set;
15154
developera3511852023-06-14 14:12:59 +080015155 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015156}
15157
developer9f2358c2023-09-22 18:42:12 +080015158
developer72fb0bb2023-01-11 09:46:29 +080015159INT wifi_getNeighborReportActivation(UINT apIndex, BOOL *activate)
15160{
developera3511852023-06-14 14:12:59 +080015161 char buf[32] = {0};
15162 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080015163 int res;
developer72fb0bb2023-01-11 09:46:29 +080015164
developer75bd10c2023-06-27 11:34:08 +080015165 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15166 if (os_snprintf_error(sizeof(config_file), res)) {
15167 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15168 return RETURN_ERR;
15169 }
developera3511852023-06-14 14:12:59 +080015170 wifi_hostapdRead(config_file, "rrm_neighbor_report", buf, sizeof(buf));
15171 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015172
developera3511852023-06-14 14:12:59 +080015173 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015174}
15175#undef HAL_NETLINK_IMPL
15176#ifdef HAL_NETLINK_IMPL
15177static int chanSurveyInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080015178 struct nlattr *tb[NL80211_ATTR_MAX + 1];
15179 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
15180 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
15181 char dev[20];
15182 int freq =0 ;
15183 static int i=0;
developer72fb0bb2023-01-11 09:46:29 +080015184
developera3511852023-06-14 14:12:59 +080015185 wifi_channelStats_t_loc *out = (wifi_channelStats_t_loc*)arg;
developer72fb0bb2023-01-11 09:46:29 +080015186
developera3511852023-06-14 14:12:59 +080015187 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
15188 };
developer72fb0bb2023-01-11 09:46:29 +080015189
developera3511852023-06-14 14:12:59 +080015190 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080015191
developera3511852023-06-14 14:12:59 +080015192 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080015193
developera3511852023-06-14 14:12:59 +080015194 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
15195 fprintf(stderr, "survey data missing!\n");
15196 return NL_SKIP;
15197 }
developer72fb0bb2023-01-11 09:46:29 +080015198
developera3511852023-06-14 14:12:59 +080015199 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,tb[NL80211_ATTR_SURVEY_INFO],survey_policy))
15200 {
15201 fprintf(stderr, "failed to parse nested attributes!\n");
15202 return NL_SKIP;
15203 }
developer72fb0bb2023-01-11 09:46:29 +080015204
15205
developera3511852023-06-14 14:12:59 +080015206 if(out[0].array_size == 1 )
15207 {
15208 if(sinfo[NL80211_SURVEY_INFO_IN_USE])
15209 {
15210 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15211 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15212 out[0].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015213
developera3511852023-06-14 14:12:59 +080015214 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15215 out[0].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15216 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15217 out[0].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15218 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15219 out[0].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15220 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15221 out[0].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15222 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15223 out[0].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15224 if (sinfo[NL80211_SURVEY_INFO_TIME])
15225 out[0].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15226 return NL_STOP;
15227 }
15228 } else {
15229 if ( i <= out[0].array_size ) {
15230 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15231 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15232 out[i].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015233
developera3511852023-06-14 14:12:59 +080015234 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15235 out[i].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15236 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15237 out[i].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15238 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15239 out[i].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15240 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15241 out[i].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15242 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15243 out[i].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15244 if (sinfo[NL80211_SURVEY_INFO_TIME])
15245 out[i].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15246 }
15247 }
developer72fb0bb2023-01-11 09:46:29 +080015248
developera3511852023-06-14 14:12:59 +080015249 i++;
15250 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015251}
15252#endif
15253
15254static int ieee80211_channel_to_frequency(int channel, int *freqMHz)
15255{
developer8078acf2023-08-04 18:52:48 +080015256 char output[MAX_BUF_SIZE];
developera3511852023-06-14 14:12:59 +080015257 FILE *fp;
developer8078acf2023-08-04 18:52:48 +080015258
developer72fb0bb2023-01-11 09:46:29 +080015259
developera3511852023-06-14 14:12:59 +080015260 if(access("/tmp/freq-channel-map.txt", F_OK)==-1)
15261 {
15262 printf("Creating Frequency-Channel Map\n");
developer33f13ba2023-07-12 16:19:06 +080015263 v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
developera3511852023-06-14 14:12:59 +080015264 }
developere40952c2023-06-15 18:46:43 +080015265
developer8078acf2023-08-04 18:52:48 +080015266 if((fp = v_secure_popen("r", "cat /tmp/freq-channel-map.txt | grep '\\[%d\\]$' | cut -d' ' -f1", channel)))
developera3511852023-06-14 14:12:59 +080015267 {
developerc14d83a2023-06-29 20:09:42 +080015268 if (fgets(output, sizeof(output), fp) == NULL) {
15269 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer8078acf2023-08-04 18:52:48 +080015270 v_secure_pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080015271 return RETURN_ERR;
15272 }
developera3511852023-06-14 14:12:59 +080015273 *freqMHz = atoi(output);
developer8078acf2023-08-04 18:52:48 +080015274 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080015275 }
developer72fb0bb2023-01-11 09:46:29 +080015276
developera3511852023-06-14 14:12:59 +080015277 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015278}
15279
developer2f79c922023-06-02 17:33:42 +080015280static int get_survey_dump_buf(INT radioIndex, int channel, char *buf, size_t bufsz)
developer72fb0bb2023-01-11 09:46:29 +080015281{
developera3511852023-06-14 14:12:59 +080015282 int freqMHz = -1;
15283 char cmd[MAX_CMD_SIZE] = {'\0'};
15284 char interface_name[16] = {0};
developer32f2a182023-06-27 19:50:41 +080015285 int res;
developer72fb0bb2023-01-11 09:46:29 +080015286
developera3511852023-06-14 14:12:59 +080015287 ieee80211_channel_to_frequency(channel, &freqMHz);
15288 if (freqMHz == -1) {
15289 wifi_dbg_printf("%s: failed to get channel frequency for channel: %d\n", __func__, channel);
15290 return -1;
15291 }
developer72fb0bb2023-01-11 09:46:29 +080015292
developer86035662023-06-28 19:21:12 +080015293 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
15294 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
15295 }
developer32f2a182023-06-27 19:50:41 +080015296 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep -A5 %d | tr -d '\\t'", interface_name, freqMHz);
15297 if (os_snprintf_error(sizeof(cmd), res)) {
15298 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15299 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080015300 }
developer72fb0bb2023-01-11 09:46:29 +080015301
developer8078acf2023-08-04 18:52:48 +080015302 if (_syscmd_secure(buf, bufsz, "iw dev %s survey dump | grep -A5 %d | tr -d '\\t'", interface_name, freqMHz) == RETURN_ERR) {
developera3511852023-06-14 14:12:59 +080015303 wifi_dbg_printf("%s: failed to execute '%s' for radioIndex=%d\n", __FUNCTION__, cmd, radioIndex);
15304 return -1;
15305 }
developer72fb0bb2023-01-11 09:46:29 +080015306
developera3511852023-06-14 14:12:59 +080015307 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015308}
15309
15310static int fetch_survey_from_buf(INT radioIndex, const char *buf, wifi_channelStats_t *stats)
15311{
developera3511852023-06-14 14:12:59 +080015312 const char *ptr = buf;
15313 char *key = NULL;
15314 char *val = NULL;
15315 char line[256] = { '\0' };
developer72fb0bb2023-01-11 09:46:29 +080015316
developera3511852023-06-14 14:12:59 +080015317 while ((ptr = get_line_from_str_buf(ptr, line))) {
15318 if (strstr(line, "Frequency")) continue;
developer72fb0bb2023-01-11 09:46:29 +080015319
developera3511852023-06-14 14:12:59 +080015320 key = strtok(line, ":");
developerc14d83a2023-06-29 20:09:42 +080015321 if (key == NULL)
15322 continue;
developera3511852023-06-14 14:12:59 +080015323 val = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080015324 if (val == NULL)
15325 continue;
developera3511852023-06-14 14:12:59 +080015326 wifi_dbg_printf("%s: key='%s' val='%s'\n", __func__, key, val);
developer72fb0bb2023-01-11 09:46:29 +080015327
developera3511852023-06-14 14:12:59 +080015328 if (!strcmp(key, "noise")) {
developer37646972023-06-29 10:58:43 +080015329 if (sscanf(val, "%d", &stats->ch_noise) == EOF)
15330 continue;
developera3511852023-06-14 14:12:59 +080015331 if (stats->ch_noise == 0) {
15332 // Workaround for missing noise information.
15333 // Assume -95 for 2.4G and -103 for 5G
15334 if (radioIndex == 0) stats->ch_noise = -95;
15335 if (radioIndex == 1) stats->ch_noise = -103;
15336 }
15337 }
15338 else if (!strcmp(key, "channel active time")) {
developer37646972023-06-29 10:58:43 +080015339 if (sscanf(val, "%llu", &stats->ch_utilization_total) == EOF)
15340 continue;
developera3511852023-06-14 14:12:59 +080015341 }
15342 else if (!strcmp(key, "channel busy time")) {
developer37646972023-06-29 10:58:43 +080015343 if (sscanf(val, "%llu", &stats->ch_utilization_busy) == EOF)
15344 continue;
developera3511852023-06-14 14:12:59 +080015345 }
15346 else if (!strcmp(key, "channel receive time")) {
developer37646972023-06-29 10:58:43 +080015347 if (sscanf(val, "%llu", &stats->ch_utilization_busy_rx) == EOF)
15348 continue;
developera3511852023-06-14 14:12:59 +080015349 }
15350 else if (!strcmp(key, "channel transmit time")) {
developer37646972023-06-29 10:58:43 +080015351 if (sscanf(val, "%llu", &stats->ch_utilization_busy_tx) == EOF)
15352 continue;
developera3511852023-06-14 14:12:59 +080015353 }
15354 };
developer72fb0bb2023-01-11 09:46:29 +080015355
developera3511852023-06-14 14:12:59 +080015356 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015357}
15358
15359INT wifi_getRadioChannelStats(INT radioIndex,wifi_channelStats_t *input_output_channelStats_array,INT array_size)
15360{
developera3511852023-06-14 14:12:59 +080015361 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080015362#ifdef HAL_NETLINK_IMPL
developera3511852023-06-14 14:12:59 +080015363 Netlink nl;
15364 wifi_channelStats_t_loc local[array_size];
15365 char if_name[32];
developer72fb0bb2023-01-11 09:46:29 +080015366
developera3511852023-06-14 14:12:59 +080015367 local[0].array_size = array_size;
developer72fb0bb2023-01-11 09:46:29 +080015368
developera3511852023-06-14 14:12:59 +080015369 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
15370 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015371
developera3511852023-06-14 14:12:59 +080015372 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015373
developera3511852023-06-14 14:12:59 +080015374 if (nl.id < 0) {
developerc14d83a2023-06-29 20:09:42 +080015375 wifi_debug(DEBUG_ERROR,, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015376 return -1;
15377 }
developer72fb0bb2023-01-11 09:46:29 +080015378
developera3511852023-06-14 14:12:59 +080015379 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015380
developera3511852023-06-14 14:12:59 +080015381 if (!msg) {
developerc14d83a2023-06-29 20:09:42 +080015382 wifi_debug(DEBUG_ERROR,, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015383 nlfree(&nl);
15384 return -2;
15385 }
developer72fb0bb2023-01-11 09:46:29 +080015386
developera3511852023-06-14 14:12:59 +080015387 genlmsg_put(msg,
15388 NL_AUTO_PID,
15389 NL_AUTO_SEQ,
15390 nl.id,
15391 0,
15392 NLM_F_DUMP,
15393 NL80211_CMD_GET_SURVEY,
15394 0);
developer72fb0bb2023-01-11 09:46:29 +080015395
developera3511852023-06-14 14:12:59 +080015396 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15397 nl_send_auto_complete(nl.socket, msg);
15398 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,chanSurveyInfo_callback,local);
15399 nl_recvmsgs(nl.socket, nl.cb);
15400 nlmsg_free(msg);
15401 nlfree(&nl);
15402 //Copying the Values
15403 for(int i=0;i<array_size;i++)
15404 {
15405 input_output_channelStats_array[i].ch_number = local[i].ch_number;
15406 input_output_channelStats_array[i].ch_noise = local[i].ch_noise;
15407 input_output_channelStats_array[i].ch_utilization_busy_rx = local[i].ch_utilization_busy_rx;
15408 input_output_channelStats_array[i].ch_utilization_busy_tx = local[i].ch_utilization_busy_tx;
15409 input_output_channelStats_array[i].ch_utilization_busy = local[i].ch_utilization_busy;
15410 input_output_channelStats_array[i].ch_utilization_busy_ext = local[i].ch_utilization_busy_ext;
15411 input_output_channelStats_array[i].ch_utilization_total = local[i].ch_utilization_total;
15412 //TODO: ch_radar_noise, ch_max_80211_rssi, ch_non_80211_noise, ch_utilization_busy_self
15413 }
developer72fb0bb2023-01-11 09:46:29 +080015414#else
developera3511852023-06-14 14:12:59 +080015415 ULONG channel = 0;
15416 int i;
15417 int number_of_channels = array_size;
15418 char buf[512];
developer72fb0bb2023-01-11 09:46:29 +080015419
developera3511852023-06-14 14:12:59 +080015420 if (number_of_channels == 0) {
15421 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK) {
15422 wifi_dbg_printf("%s: cannot get current channel for radioIndex=%d\n", __func__, radioIndex);
15423 return RETURN_ERR;
15424 }
15425 number_of_channels = 1;
15426 input_output_channelStats_array[0].ch_number = channel;
15427 }
developer72fb0bb2023-01-11 09:46:29 +080015428
developera3511852023-06-14 14:12:59 +080015429 for (i = 0; i < number_of_channels; i++) {
developer72fb0bb2023-01-11 09:46:29 +080015430
developera3511852023-06-14 14:12:59 +080015431 input_output_channelStats_array[i].ch_noise = 0;
15432 input_output_channelStats_array[i].ch_utilization_busy_rx = 0;
15433 input_output_channelStats_array[i].ch_utilization_busy_tx = 0;
15434 input_output_channelStats_array[i].ch_utilization_busy = 0;
15435 input_output_channelStats_array[i].ch_utilization_busy_ext = 0; // XXX: unavailable
15436 input_output_channelStats_array[i].ch_utilization_total = 0;
developer72fb0bb2023-01-11 09:46:29 +080015437
developera3511852023-06-14 14:12:59 +080015438 memset(buf, 0, sizeof(buf));
15439 if (get_survey_dump_buf(radioIndex, input_output_channelStats_array[i].ch_number, buf, sizeof(buf))) {
15440 return RETURN_ERR;
15441 }
15442 if (fetch_survey_from_buf(radioIndex, buf, &input_output_channelStats_array[i])) {
15443 wifi_dbg_printf("%s: cannot fetch survey from buf for radioIndex=%d\n", __func__, radioIndex);
15444 return RETURN_ERR;
15445 }
developer72fb0bb2023-01-11 09:46:29 +080015446
developera3511852023-06-14 14:12:59 +080015447 // XXX: fake missing 'self' counter which is not available in iw survey output
15448 // the 'self' counter (a.k.a 'bss') requires Linux Kernel update
15449 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 +080015450
developera3511852023-06-14 14:12:59 +080015451 input_output_channelStats_array[i].ch_utilization_busy_rx *= 1000;
15452 input_output_channelStats_array[i].ch_utilization_busy_tx *= 1000;
15453 input_output_channelStats_array[i].ch_utilization_busy_self *= 1000;
15454 input_output_channelStats_array[i].ch_utilization_busy *= 1000;
15455 input_output_channelStats_array[i].ch_utilization_total *= 1000;
developer72fb0bb2023-01-11 09:46:29 +080015456
developera3511852023-06-14 14:12:59 +080015457 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",
15458 __func__,
15459 input_output_channelStats_array[i].ch_number,
15460 input_output_channelStats_array[i].ch_noise,
15461 input_output_channelStats_array[i].ch_utilization_total,
15462 input_output_channelStats_array[i].ch_utilization_busy,
15463 input_output_channelStats_array[i].ch_utilization_busy_rx,
15464 input_output_channelStats_array[i].ch_utilization_busy_tx,
15465 input_output_channelStats_array[i].ch_utilization_busy_self,
15466 input_output_channelStats_array[i].ch_utilization_busy_ext);
15467 }
developer72fb0bb2023-01-11 09:46:29 +080015468#endif
developera3511852023-06-14 14:12:59 +080015469 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
15470 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015471}
15472#define HAL_NETLINK_IMPL
15473
15474/* Hostapd events */
15475
15476#ifndef container_of
15477#define offset_of(st, m) ((size_t)&(((st *)0)->m))
15478#define container_of(ptr, type, member) \
developera3511852023-06-14 14:12:59 +080015479 ((type *)((char *)ptr - offset_of(type, member)))
developer72fb0bb2023-01-11 09:46:29 +080015480#endif /* container_of */
15481
15482struct ctrl {
developera3511852023-06-14 14:12:59 +080015483 char sockpath[128];
15484 char sockdir[128];
15485 char bss[IFNAMSIZ];
15486 char reply[4096];
15487 int ssid_index;
15488 void (*cb)(struct ctrl *ctrl, int level, const char *buf, size_t len);
15489 void (*overrun)(struct ctrl *ctrl);
15490 struct wpa_ctrl *wpa;
15491 unsigned int ovfl;
15492 size_t reply_len;
15493 int initialized;
15494 ev_timer retry;
15495 ev_timer watchdog;
15496 ev_stat stat;
15497 ev_io io;
developer72fb0bb2023-01-11 09:46:29 +080015498};
15499static wifi_newApAssociatedDevice_callback clients_connect_cb;
15500static wifi_apDisassociatedDevice_callback clients_disconnect_cb;
15501static struct ctrl wpa_ctrl[MAX_APS];
15502static int initialized;
15503
15504static unsigned int ctrl_get_drops(struct ctrl *ctrl)
15505{
developera3511852023-06-14 14:12:59 +080015506 char cbuf[256] = {};
15507 struct msghdr msg = { .msg_control = cbuf, .msg_controllen = sizeof(cbuf) };
15508 struct cmsghdr *cmsg;
15509 unsigned int ovfl = ctrl->ovfl;
developer86035662023-06-28 19:21:12 +080015510 unsigned int drop = 0;
developer72fb0bb2023-01-11 09:46:29 +080015511
developer86035662023-06-28 19:21:12 +080015512 if (recvmsg(ctrl->io.fd, &msg, MSG_DONTWAIT) < 0)
15513 return drop;
developera3511852023-06-14 14:12:59 +080015514 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
15515 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_RXQ_OVFL)
15516 ovfl = *(unsigned int *)CMSG_DATA(cmsg);
developer72fb0bb2023-01-11 09:46:29 +080015517
developera3511852023-06-14 14:12:59 +080015518 drop = ovfl - ctrl->ovfl;
15519 ctrl->ovfl = ovfl;
developer72fb0bb2023-01-11 09:46:29 +080015520
developera3511852023-06-14 14:12:59 +080015521 return drop;
developer72fb0bb2023-01-11 09:46:29 +080015522}
15523
15524static void ctrl_close(struct ctrl *ctrl)
15525{
developera3511852023-06-14 14:12:59 +080015526 if (ctrl->io.cb)
15527 ev_io_stop(EV_DEFAULT_ &ctrl->io);
15528 if (ctrl->retry.cb)
15529 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15530 if (!ctrl->wpa)
15531 return;
developer72fb0bb2023-01-11 09:46:29 +080015532
developera3511852023-06-14 14:12:59 +080015533 wpa_ctrl_detach(ctrl->wpa);
15534 wpa_ctrl_close(ctrl->wpa);
15535 ctrl->wpa = NULL;
15536 printf("WPA_CTRL: closed index=%d\n", ctrl->ssid_index);
developer72fb0bb2023-01-11 09:46:29 +080015537}
15538
15539static void ctrl_process(struct ctrl *ctrl)
15540{
developera3511852023-06-14 14:12:59 +080015541 const char *str;
15542 int drops;
15543 int level;
developer72fb0bb2023-01-11 09:46:29 +080015544
developera3511852023-06-14 14:12:59 +080015545 /* Example events:
15546 *
15547 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19
15548 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19 keyid=sample_keyid
15549 * <3>AP-STA-DISCONNECTED 60:b4:f7:f0:0a:19
15550 * <3>CTRL-EVENT-CONNECTED - Connection to 00:1d:73:73:88:ea completed [id=0 id_str=]
15551 * <3>CTRL-EVENT-DISCONNECTED bssid=00:1d:73:73:88:ea reason=3 locally_generated=1
15552 */
15553 if (!(str = index(ctrl->reply, '>')))
15554 return;
15555 if (sscanf(ctrl->reply, "<%d>", &level) != 1)
15556 return;
developer72fb0bb2023-01-11 09:46:29 +080015557
developera3511852023-06-14 14:12:59 +080015558 str++;
developer72fb0bb2023-01-11 09:46:29 +080015559
developera3511852023-06-14 14:12:59 +080015560 if (strncmp("AP-STA-CONNECTED ", str, 17) == 0) {
15561 if (!(str = index(ctrl->reply, ' ')))
15562 return;
15563 wifi_associated_dev_t sta;
15564 memset(&sta, 0, sizeof(sta));
developer72fb0bb2023-01-11 09:46:29 +080015565
developere75ba632023-06-29 16:03:33 +080015566 if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
15567 &sta.cli_MACAddress[0], &sta.cli_MACAddress[1], &sta.cli_MACAddress[2],
15568 &sta.cli_MACAddress[3], &sta.cli_MACAddress[4], &sta.cli_MACAddress[5]) == EOF) {
15569 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
15570 return;
15571 }
developer72fb0bb2023-01-11 09:46:29 +080015572
developera3511852023-06-14 14:12:59 +080015573 sta.cli_Active=true;
developer72fb0bb2023-01-11 09:46:29 +080015574
developera3511852023-06-14 14:12:59 +080015575 (clients_connect_cb)(ctrl->ssid_index, &sta);
15576 goto handled;
15577 }
developer72fb0bb2023-01-11 09:46:29 +080015578
developera3511852023-06-14 14:12:59 +080015579 if (strncmp("AP-STA-DISCONNECTED ", str, 20) == 0) {
15580 if (!(str = index(ctrl->reply, ' ')))
15581 return;
developer72fb0bb2023-01-11 09:46:29 +080015582
developera3511852023-06-14 14:12:59 +080015583 (clients_disconnect_cb)(ctrl->ssid_index, (char*)str, 0);
15584 goto handled;
15585 }
developer72fb0bb2023-01-11 09:46:29 +080015586
developera3511852023-06-14 14:12:59 +080015587 if (strncmp("CTRL-EVENT-TERMINATING", str, 22) == 0) {
15588 printf("CTRL_WPA: handle TERMINATING event\n");
15589 goto retry;
15590 }
developer72fb0bb2023-01-11 09:46:29 +080015591
developera3511852023-06-14 14:12:59 +080015592 if (strncmp("AP-DISABLED", str, 11) == 0) {
15593 printf("CTRL_WPA: handle AP-DISABLED\n");
15594 goto retry;
15595 }
developer72fb0bb2023-01-11 09:46:29 +080015596
developera3511852023-06-14 14:12:59 +080015597 printf("Event not supported!!\n");
developer72fb0bb2023-01-11 09:46:29 +080015598
15599handled:
15600
developera3511852023-06-14 14:12:59 +080015601 if ((drops = ctrl_get_drops(ctrl))) {
15602 printf("WPA_CTRL: dropped %d messages index=%d\n", drops, ctrl->ssid_index);
15603 if (ctrl->overrun)
15604 ctrl->overrun(ctrl);
15605 }
developer72fb0bb2023-01-11 09:46:29 +080015606
developera3511852023-06-14 14:12:59 +080015607 return;
developer72fb0bb2023-01-11 09:46:29 +080015608
15609retry:
developera3511852023-06-14 14:12:59 +080015610 printf("WPA_CTRL: closing\n");
15611 ctrl_close(ctrl);
15612 printf("WPA_CTRL: retrying from ctrl prcoess\n");
15613 ev_timer_again(EV_DEFAULT_ &ctrl->retry);
developer72fb0bb2023-01-11 09:46:29 +080015614}
15615
15616static void ctrl_ev_cb(EV_P_ struct ev_io *io, int events)
15617{
developera3511852023-06-14 14:12:59 +080015618 struct ctrl *ctrl = container_of(io, struct ctrl, io);
15619 int err;
developer72fb0bb2023-01-11 09:46:29 +080015620
developera3511852023-06-14 14:12:59 +080015621 memset(ctrl->reply, 0, sizeof(ctrl->reply));
15622 ctrl->reply_len = sizeof(ctrl->reply) - 1;
15623 err = wpa_ctrl_recv(ctrl->wpa, ctrl->reply, &ctrl->reply_len);
15624 ctrl->reply[ctrl->reply_len] = 0;
15625 if (err < 0) {
15626 if (errno == EAGAIN || errno == EWOULDBLOCK)
15627 return;
15628 ctrl_close(ctrl);
15629 ev_timer_again(EV_A_ &ctrl->retry);
15630 return;
15631 }
developer72fb0bb2023-01-11 09:46:29 +080015632
developera3511852023-06-14 14:12:59 +080015633 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015634}
15635
15636static int ctrl_open(struct ctrl *ctrl)
15637{
developera3511852023-06-14 14:12:59 +080015638 int fd;
developer72fb0bb2023-01-11 09:46:29 +080015639
developera3511852023-06-14 14:12:59 +080015640 if (ctrl->wpa)
15641 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015642
developera3511852023-06-14 14:12:59 +080015643 ctrl->wpa = wpa_ctrl_open(ctrl->sockpath);
15644 if (!ctrl->wpa)
15645 goto err;
developer72fb0bb2023-01-11 09:46:29 +080015646
developera3511852023-06-14 14:12:59 +080015647 if (wpa_ctrl_attach(ctrl->wpa) < 0)
15648 goto err_close;
developer72fb0bb2023-01-11 09:46:29 +080015649
developera3511852023-06-14 14:12:59 +080015650 fd = wpa_ctrl_get_fd(ctrl->wpa);
15651 if (fd < 0)
15652 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015653
developera3511852023-06-14 14:12:59 +080015654 if (setsockopt(fd, SOL_SOCKET, SO_RXQ_OVFL, (int[]){1}, sizeof(int)) < 0)
15655 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015656
developera3511852023-06-14 14:12:59 +080015657 ev_io_init(&ctrl->io, ctrl_ev_cb, fd, EV_READ);
15658 ev_io_start(EV_DEFAULT_ &ctrl->io);
developer72fb0bb2023-01-11 09:46:29 +080015659
developera3511852023-06-14 14:12:59 +080015660 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015661
15662err_detach:
developera3511852023-06-14 14:12:59 +080015663 wpa_ctrl_detach(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015664err_close:
developera3511852023-06-14 14:12:59 +080015665 wpa_ctrl_close(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015666err:
developera3511852023-06-14 14:12:59 +080015667 ctrl->wpa = NULL;
15668 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015669}
15670
15671static void ctrl_stat_cb(EV_P_ ev_stat *stat, int events)
15672{
developera3511852023-06-14 14:12:59 +080015673 struct ctrl *ctrl = container_of(stat, struct ctrl, stat);
developer72fb0bb2023-01-11 09:46:29 +080015674
developera3511852023-06-14 14:12:59 +080015675 printf("WPA_CTRL: index=%d file state changed\n", ctrl->ssid_index);
15676 ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015677}
15678
15679static void ctrl_retry_cb(EV_P_ ev_timer *timer, int events)
15680{
developera3511852023-06-14 14:12:59 +080015681 struct ctrl *ctrl = container_of(timer, struct ctrl, retry);
developer72fb0bb2023-01-11 09:46:29 +080015682
developera3511852023-06-14 14:12:59 +080015683 printf("WPA_CTRL: index=%d retrying\n", ctrl->ssid_index);
15684 if (ctrl_open(ctrl) == 0) {
15685 printf("WPA_CTRL: retry successful\n");
15686 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15687 }
developer72fb0bb2023-01-11 09:46:29 +080015688}
15689
15690int ctrl_enable(struct ctrl *ctrl)
15691{
developera3511852023-06-14 14:12:59 +080015692 if (ctrl->wpa)
15693 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015694
developera3511852023-06-14 14:12:59 +080015695 if (!ctrl->stat.cb) {
15696 ev_stat_init(&ctrl->stat, ctrl_stat_cb, ctrl->sockpath, 0.);
15697 ev_stat_start(EV_DEFAULT_ &ctrl->stat);
15698 }
developer72fb0bb2023-01-11 09:46:29 +080015699
developera3511852023-06-14 14:12:59 +080015700 if (!ctrl->retry.cb) {
15701 ev_timer_init(&ctrl->retry, ctrl_retry_cb, 0., 5.);
15702 }
developer72fb0bb2023-01-11 09:46:29 +080015703
developera3511852023-06-14 14:12:59 +080015704 return ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015705}
15706
15707static void
15708ctrl_msg_cb(char *buf, size_t len)
15709{
developera3511852023-06-14 14:12:59 +080015710 struct ctrl *ctrl = container_of(buf, struct ctrl, reply);
developer72fb0bb2023-01-11 09:46:29 +080015711
developera3511852023-06-14 14:12:59 +080015712 printf("WPA_CTRL: unsolicited message: index=%d len=%zu msg=%s", ctrl->ssid_index, len, buf);
15713 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015714}
15715
15716static int ctrl_request(struct ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len)
15717{
developera3511852023-06-14 14:12:59 +080015718 int err;
developer72fb0bb2023-01-11 09:46:29 +080015719
developera3511852023-06-14 14:12:59 +080015720 if (!ctrl->wpa)
15721 return -1;
15722 if (*reply_len < 2)
15723 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015724
developera3511852023-06-14 14:12:59 +080015725 (*reply_len)--;
15726 ctrl->reply_len = sizeof(ctrl->reply);
15727 err = wpa_ctrl_request(ctrl->wpa, cmd, cmd_len, ctrl->reply, &ctrl->reply_len, ctrl_msg_cb);
15728 printf("WPA_CTRL: index=%d cmd='%s' err=%d\n", ctrl->ssid_index, cmd, err);
15729 if (err < 0)
15730 return err;
developer72fb0bb2023-01-11 09:46:29 +080015731
developera3511852023-06-14 14:12:59 +080015732 if (ctrl->reply_len > *reply_len)
15733 ctrl->reply_len = *reply_len;
developer72fb0bb2023-01-11 09:46:29 +080015734
developera3511852023-06-14 14:12:59 +080015735 *reply_len = ctrl->reply_len;
15736 memcpy(reply, ctrl->reply, *reply_len);
15737 reply[*reply_len - 1] = 0;
15738 printf("WPA_CTRL: index=%d reply='%s'\n", ctrl->ssid_index, reply);
15739 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015740}
15741
15742static void ctrl_watchdog_cb(EV_P_ ev_timer *timer, int events)
15743{
developera3511852023-06-14 14:12:59 +080015744 const char *pong = "PONG";
15745 const char *ping = "PING";
15746 char reply[1024];
15747 size_t len = sizeof(reply);
15748 int err;
15749 ULONG s, snum;
15750 INT ret;
15751 BOOL status;
developer72fb0bb2023-01-11 09:46:29 +080015752
developera3511852023-06-14 14:12:59 +080015753 printf("WPA_CTRL: watchdog cb\n");
developer72fb0bb2023-01-11 09:46:29 +080015754
developera3511852023-06-14 14:12:59 +080015755 ret = wifi_getSSIDNumberOfEntries(&snum);
15756 if (ret != RETURN_OK) {
15757 printf("%s: failed to get SSID count", __func__);
15758 return;
15759 }
developer72fb0bb2023-01-11 09:46:29 +080015760
developera3511852023-06-14 14:12:59 +080015761 if (snum > MAX_APS) {
15762 printf("more ssid than supported! %lu\n", snum);
15763 return;
15764 }
developer72fb0bb2023-01-11 09:46:29 +080015765
developera3511852023-06-14 14:12:59 +080015766 for (s = 0; s < snum; s++) {
15767 if (wifi_getApEnable(s, &status) != RETURN_OK) {
15768 printf("%s: failed to get AP Enable for index: %lu\n", __func__, s);
15769 continue;
15770 }
15771 if (status == false) continue;
developer72fb0bb2023-01-11 09:46:29 +080015772
developera3511852023-06-14 14:12:59 +080015773 memset(reply, 0, sizeof(reply));
15774 len = sizeof(reply);
15775 printf("WPA_CTRL: pinging index=%d\n", wpa_ctrl[s].ssid_index);
15776 err = ctrl_request(&wpa_ctrl[s], ping, strlen(ping), reply, &len);
15777 if (err == 0 && len > strlen(pong) && !strncmp(reply, pong, strlen(pong)))
15778 continue;
developer72fb0bb2023-01-11 09:46:29 +080015779
developera3511852023-06-14 14:12:59 +080015780 printf("WPA_CTRL: ping timeout index=%d\n", wpa_ctrl[s].ssid_index);
15781 ctrl_close(&wpa_ctrl[s]);
15782 printf("WPA_CTRL: ev_timer_again %lu\n", s);
15783 ev_timer_again(EV_DEFAULT_ &wpa_ctrl[s].retry);
15784 }
developer72fb0bb2023-01-11 09:46:29 +080015785}
15786
15787static int init_wpa()
15788{
developer9ce44382023-06-28 11:09:37 +080015789 int ret = 0;
developera3511852023-06-14 14:12:59 +080015790 ULONG s, snum;
developer72fb0bb2023-01-11 09:46:29 +080015791
developera3511852023-06-14 14:12:59 +080015792 ret = wifi_getSSIDNumberOfEntries(&snum);
15793 if (ret != RETURN_OK) {
15794 printf("%s: failed to get SSID count", __func__);
15795 return RETURN_ERR;
15796 }
developer72fb0bb2023-01-11 09:46:29 +080015797
developera3511852023-06-14 14:12:59 +080015798 if (snum > MAX_APS) {
15799 printf("more ssid than supported! %lu\n", snum);
15800 return RETURN_ERR;
15801 }
developer72fb0bb2023-01-11 09:46:29 +080015802
developera3511852023-06-14 14:12:59 +080015803 for (s = 0; s < snum; s++) {
15804 memset(&wpa_ctrl[s], 0, sizeof(struct ctrl));
developer32f2a182023-06-27 19:50:41 +080015805 ret = snprintf(wpa_ctrl[s].sockpath, sizeof(wpa_ctrl[s].sockpath), "%s%lu", SOCK_PREFIX, s);
15806 if (os_snprintf_error(sizeof(wpa_ctrl[s].sockpath), ret)) {
15807 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15808 return RETURN_ERR;
15809 }
developera3511852023-06-14 14:12:59 +080015810 wpa_ctrl[s].ssid_index = s;
15811 ctrl_enable(&wpa_ctrl[s]);
15812 }
developer72fb0bb2023-01-11 09:46:29 +080015813
developera3511852023-06-14 14:12:59 +080015814 ev_timer_init(&wpa_ctrl->watchdog, ctrl_watchdog_cb, 0., 30.);
15815 ev_timer_again(EV_DEFAULT_ &wpa_ctrl->watchdog);
developer72fb0bb2023-01-11 09:46:29 +080015816
developera3511852023-06-14 14:12:59 +080015817 initialized = 1;
15818 printf("WPA_CTRL: initialized\n");
developer72fb0bb2023-01-11 09:46:29 +080015819
developera3511852023-06-14 14:12:59 +080015820 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015821}
15822
15823void wifi_newApAssociatedDevice_callback_register(wifi_newApAssociatedDevice_callback callback_proc)
15824{
developera3511852023-06-14 14:12:59 +080015825 clients_connect_cb = callback_proc;
15826 if (!initialized)
15827 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015828}
15829
15830void wifi_apDisassociatedDevice_callback_register(wifi_apDisassociatedDevice_callback callback_proc)
15831{
developera3511852023-06-14 14:12:59 +080015832 clients_disconnect_cb = callback_proc;
15833 if (!initialized)
15834 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015835}
15836
15837INT wifi_setBTMRequest(UINT apIndex, CHAR *peerMac, wifi_BTMRequest_t *request)
15838{
developera3511852023-06-14 14:12:59 +080015839 // TODO Implement me!
15840 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015841}
15842
15843INT wifi_setRMBeaconRequest(UINT apIndex, CHAR *peer, wifi_BeaconRequest_t *in_request, UCHAR *out_DialogToken)
15844{
developera3511852023-06-14 14:12:59 +080015845 // TODO Implement me!
15846 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015847}
15848
15849INT wifi_getRadioChannels(INT radioIndex, wifi_channelMap_t *outputMap, INT outputMapSize)
15850{
developera3511852023-06-14 14:12:59 +080015851 int i;
15852 int phyId = -1;
15853 char cmd[256] = {0};
15854 char channel_numbers_buf[256] = {0};
15855 char dfs_state_buf[256] = {0};
15856 char line[256] = {0};
15857 const char *ptr;
15858 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +080015859 int res;
developer72fb0bb2023-01-11 09:46:29 +080015860
developera3511852023-06-14 14:12:59 +080015861 memset(outputMap, 0, outputMapSize*sizeof(wifi_channelMap_t)); // all unused entries should be zero
developer72fb0bb2023-01-11 09:46:29 +080015862
developera3511852023-06-14 14:12:59 +080015863 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
15864 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080015865
developere40952c2023-06-15 18:46:43 +080015866 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\\|");
15867 if (os_snprintf_error(sizeof(cmd), res)) {
15868 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15869 return RETURN_ERR;
15870 }
developer72fb0bb2023-01-11 09:46:29 +080015871
developer8078acf2023-08-04 18:52:48 +080015872 if (_syscmd_secure(channel_numbers_buf, sizeof(channel_numbers_buf), "iw phy phy%d info | grep -e '\\*.*MHz .*dBm' | grep -v '%sno IR\\|5340\\|5480' | awk '{print $4}' | tr -d '[]'", phyId, dfs_enable?"":"radar\\|") == RETURN_ERR) {
developera3511852023-06-14 14:12:59 +080015873 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15874 return RETURN_ERR;
15875 }
developer72fb0bb2023-01-11 09:46:29 +080015876
developera3511852023-06-14 14:12:59 +080015877 ptr = channel_numbers_buf;
15878 i = 0;
15879 while ((ptr = get_line_from_str_buf(ptr, line))) {
15880 if (i >= outputMapSize) {
15881 wifi_dbg_printf("%s: DFS map size too small\n", __FUNCTION__);
15882 return RETURN_ERR;
15883 }
developerd14dff12023-06-28 22:47:44 +080015884 if (sscanf(line, "%d", &outputMap[i].ch_number) != 1) {
15885 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
15886 return RETURN_ERR;
15887 }
developerd1824452023-05-18 12:30:04 +080015888
developera3511852023-06-14 14:12:59 +080015889 memset(cmd, 0, sizeof(cmd));
15890 // Below command should fetch string for DFS state (usable, available or unavailable)
15891 // Example line: "DFS state: usable (for 78930 sec)"
15892 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) {
15893 wifi_dbg_printf("%s: failed to build dfs state command\n", __FUNCTION__);
15894 return RETURN_ERR;
15895 }
developer72fb0bb2023-01-11 09:46:29 +080015896
developera3511852023-06-14 14:12:59 +080015897 memset(dfs_state_buf, 0, sizeof(dfs_state_buf));
developer8078acf2023-08-04 18:52:48 +080015898 if (_syscmd_secure(dfs_state_buf, sizeof(dfs_state_buf),"iw list | grep -A 2 '\\[%d\\]' | tr -d '\\t' | grep 'DFS state' | awk '{print $3}' | tr -d '\\n'", outputMap[i].ch_number) == RETURN_ERR) {
developera3511852023-06-14 14:12:59 +080015899 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15900 return RETURN_ERR;
15901 }
developer72fb0bb2023-01-11 09:46:29 +080015902
developera3511852023-06-14 14:12:59 +080015903 wifi_dbg_printf("DFS state = '%s'\n", dfs_state_buf);
developer59fda4f2023-05-16 15:47:38 +080015904
developera3511852023-06-14 14:12:59 +080015905 if (!strcmp(dfs_state_buf, "usable")) {
15906 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_FINISHED;
15907 } else if (!strcmp(dfs_state_buf, "available")) {
15908 outputMap[i].ch_state = CHAN_STATE_DFS_CAC_COMPLETED;
15909 } else if (!strcmp(dfs_state_buf, "unavailable")) {
15910 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_START;
15911 } else {
15912 outputMap[i].ch_state = CHAN_STATE_AVAILABLE;
15913 }
15914 i++;
15915 }
developer40ba1762023-05-13 11:03:49 +080015916
developera3511852023-06-14 14:12:59 +080015917 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +080015918
developera3511852023-06-14 14:12:59 +080015919 wifi_dbg_printf("%s: wrong radio index (%d)\n", __FUNCTION__, radioIndex);
15920 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015921}
15922
15923INT wifi_chan_eventRegister(wifi_chan_eventCB_t eventCb)
15924{
developera3511852023-06-14 14:12:59 +080015925 // TODO Implement me!
15926 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015927}
15928
15929INT wifi_getRadioBandUtilization (INT radioIndex, INT *output_percentage)
15930{
developer0155a502023-06-19 20:33:57 +080015931 int ret = -1;
15932 char inf_name[IF_NAME_SIZE] = {0};
15933 int if_idx = 0;
15934 struct unl unl_ins;
15935 struct nl_msg *msg = NULL;
15936 struct nlattr * msg_data = NULL;
15937 struct mtk_nl80211_param param;
15938 struct mtk_nl80211_cb_data cb_data;
15939 wdev_ap_metric ap_metric;
15940
15941 /*init mtk nl80211 vendor cmd*/
15942
15943 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
15944 return RETURN_ERR;
15945 if_idx = if_nametoindex(inf_name);
15946 if (!if_idx) {
15947 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
15948 return RETURN_ERR;
15949 }
15950
15951 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_STATISTIC;
15952 param.if_type = NL80211_ATTR_IFINDEX;
15953 param.if_idx = if_idx;
15954
15955 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
15956 if (ret) {
15957 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
15958 return RETURN_ERR;
15959 }
15960
15961 /*add mtk vendor cmd data*/
15962
15963 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS, sizeof(wdev_ap_metric), (char *)&ap_metric)) {
15964 wifi_debug(DEBUG_ERROR, "Nla put GET_AP_METRICS attribute error\n");
15965 nlmsg_free(msg);
15966 goto err;
15967 }
15968
15969 /*send mtk nl80211 vendor msg*/
15970 cb_data.out_buf = (char *)output_percentage;
15971 cb_data.out_len = sizeof(wdev_ap_metric);
15972 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_get_ap_metrics, &cb_data);
15973 if (ret) {
15974 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
15975 goto err;
15976 }
15977
15978 /*deinit mtk nl80211 vendor msg*/
15979 mtk_nl80211_deint(&unl_ins);
15980 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
15981 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
15982
developera3511852023-06-14 14:12:59 +080015983 return RETURN_OK;
developer0155a502023-06-19 20:33:57 +080015984err:
15985 mtk_nl80211_deint(&unl_ins);
15986 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
15987 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015988}
15989
developer0155a502023-06-19 20:33:57 +080015990
developer72fb0bb2023-01-11 09:46:29 +080015991INT wifi_getApAssociatedClientDiagnosticResult(INT apIndex, char *mac_addr, wifi_associated_dev3_t *dev_conn)
15992{
developera3511852023-06-14 14:12:59 +080015993 // TODO Implement me!
15994 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015995}
15996
15997INT wifi_switchBand(char *interface_name,INT radioIndex,char *freqBand)
15998{
developera3511852023-06-14 14:12:59 +080015999 // TODO API refrence Implementaion is present on RPI hal
16000 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016001}
16002
16003INT wifi_getRadioPercentageTransmitPower(INT apIndex, ULONG *txpwr_pcntg)
16004{
developera3511852023-06-14 14:12:59 +080016005 ULONG pwr_percentage = 0;
developer72fb0bb2023-01-11 09:46:29 +080016006
developera3511852023-06-14 14:12:59 +080016007 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16008 if(txpwr_pcntg == NULL)
developerdaf24792023-06-06 11:40:04 +080016009 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016010
developera1255e42023-05-13 17:45:02 +080016011 wifi_getRadioTransmitPower(apIndex, &pwr_percentage);
16012 *txpwr_pcntg = pwr_percentage;
developera3511852023-06-14 14:12:59 +080016013 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16014 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016015}
16016
16017INT wifi_setZeroDFSState(UINT radioIndex, BOOL enable, BOOL precac)
16018{
developera3511852023-06-14 14:12:59 +080016019 // TODO precac feature.
16020 struct params params[2] = {0};
16021 char config_file[128] = {0};
16022 BOOL dfs_enable = false;
16023 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016024 int res;
developer72fb0bb2023-01-11 09:46:29 +080016025
developera3511852023-06-14 14:12:59 +080016026 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16027 band = wifi_index_to_band(radioIndex);
16028 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
developer72fb0bb2023-01-11 09:46:29 +080016029
developera3511852023-06-14 14:12:59 +080016030 if (dfs_enable == false) {
16031 WIFI_ENTRY_EXIT_DEBUG("Please enable DFS firstly!: %s\n", __func__);
16032 return RETURN_ERR;
16033 }
16034 params[0].name = "DfsZeroWaitDefault";
16035 params[0].value = enable?"1":"0";
16036 params[1].name = "DfsDedicatedZeroWait";
16037 params[1].value = enable?"1":"0";
developere40952c2023-06-15 18:46:43 +080016038 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16039 if (os_snprintf_error(sizeof(config_file), res)) {
16040 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16041 return RETURN_ERR;
16042 }
developera3511852023-06-14 14:12:59 +080016043 wifi_datfileWrite(config_file, params, 2);
16044 wifi_reloadAp(radioIndex);
16045 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016046
developera3511852023-06-14 14:12:59 +080016047 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16048 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016049}
16050
16051INT wifi_getZeroDFSState(UINT radioIndex, BOOL *enable, BOOL *precac)
16052{
developera3511852023-06-14 14:12:59 +080016053 char config_file[128] = {0};
16054 char buf1[32] = {0};
16055 char buf2[32] = {0};
16056 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016057 int res;
developer72fb0bb2023-01-11 09:46:29 +080016058
developera3511852023-06-14 14:12:59 +080016059 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16060 if (NULL == enable || NULL == precac)
16061 return RETURN_ERR;
16062 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080016063 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16064 if (os_snprintf_error(sizeof(config_file), res)) {
16065 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16066 return RETURN_ERR;
16067 }
developera3511852023-06-14 14:12:59 +080016068 wifi_datfileRead(config_file, "DfsZeroWaitDefault", buf1, sizeof(buf1));
16069 wifi_datfileRead(config_file, "DfsDedicatedZeroWait", buf2, sizeof(buf2));
16070 if ((strncmp(buf1, "1", 1) == 0) && (strncmp(buf2, "1", 1) == 0))
16071 *enable = true;
16072 else
16073 *enable = false;
developer72fb0bb2023-01-11 09:46:29 +080016074
developera3511852023-06-14 14:12:59 +080016075 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016076
developera3511852023-06-14 14:12:59 +080016077 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16078 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016079}
16080
16081INT wifi_isZeroDFSSupported(UINT radioIndex, BOOL *supported)
16082{
developera3511852023-06-14 14:12:59 +080016083 *supported = TRUE;
16084 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016085}
16086
16087INT wifi_setDownlinkMuType(INT radio_index, wifi_dl_mu_type_t mu_type)
16088{
developer863a4a62023-06-06 16:55:59 +080016089 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016090 wifi_band band = band_invalid;
16091 char ofdmabuf[32] = {'\0'};
16092 char mimobuf[32] = {'\0'};
16093 char new_ofdmabuf[32] = {'\0'};
16094 char new_mimobuf[32] = {'\0'};
16095 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016096 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16097 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16098 UCHAR bss_cnt = 0;
16099 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016100 int res;
developer72fb0bb2023-01-11 09:46:29 +080016101
developera3511852023-06-14 14:12:59 +080016102 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera1255e42023-05-13 17:45:02 +080016103 if ((mu_type < WIFI_DL_MU_TYPE_NONE)
16104 || (mu_type > WIFI_DL_MU_TYPE_OFDMA_MIMO)) {
16105 printf("%s:mu_type input Error", __func__);
16106 return RETURN_ERR;
16107 }
developera3511852023-06-14 14:12:59 +080016108 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016109 if (band == band_invalid) {
16110 printf("%s:Band Error\n", __func__);
16111 return RETURN_ERR;
16112 }
developere40952c2023-06-15 18:46:43 +080016113 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16114 if (os_snprintf_error(sizeof(dat_file), res)) {
16115 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16116 return RETURN_ERR;
16117 }
16118
developera1255e42023-05-13 17:45:02 +080016119 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016120 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16121 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016122 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16123 get_bssnum_byindex(radio_index, &bss_cnt);
16124 val_cnt = 2*bss_cnt - 1;
16125 WIFI_ENTRY_EXIT_DEBUG("bss number: %d\n", bss_cnt);
16126 if ((val_cnt >= sizeof(new_ofdmabuf))
16127 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016128 printf("%s:bss cnt Error", __func__);
developera1255e42023-05-13 17:45:02 +080016129 return RETURN_ERR;
16130 }
16131 /*translate set value*/
16132 if (mu_type == WIFI_DL_MU_TYPE_NONE) {
16133 strncpy(new_ofdmabuf, str_zero, val_cnt);
16134 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016135 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA) {
developera1255e42023-05-13 17:45:02 +080016136 strncpy(new_ofdmabuf, str_one, val_cnt);
16137 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016138 } else if (mu_type == WIFI_DL_MU_TYPE_MIMO) {
developera1255e42023-05-13 17:45:02 +080016139 strncpy(new_ofdmabuf, str_zero, val_cnt);
16140 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016141 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA_MIMO) {
developera1255e42023-05-13 17:45:02 +080016142 strncpy(new_ofdmabuf, str_one, val_cnt);
16143 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016144 }
developera1255e42023-05-13 17:45:02 +080016145 WIFI_ENTRY_EXIT_DEBUG("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16146 /*same value, not operation*/
16147 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16148 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16149 printf("%s:Reduntant value\n", __func__);
16150 return RETURN_OK;
16151 }
16152 /*modify dat file to new file*/
16153 params[0].name="MuOfdmaDlEnable";
16154 params[0].value=new_ofdmabuf;
16155 params[1].name="MuMimoDlEnable";
16156 params[1].value=new_mimobuf;
16157 wifi_datfileWrite(dat_file, params, 2);
16158 /*hostapd control restarp ap to take effect on these new value*/
16159 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016160 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16161 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016162}
16163
16164INT wifi_getDownlinkMuType(INT radio_index, wifi_dl_mu_type_t *mu_type)
16165{
developer5a333cf2023-06-06 18:18:50 +080016166 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016167 wifi_band band = band_invalid;
16168 char ofdmabuf[32] = {'\0'};
16169 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016170 char *token = NULL;
developerc14d83a2023-06-29 20:09:42 +080016171 long int ofdma = 0;
16172 long int mimo = 0;
developere40952c2023-06-15 18:46:43 +080016173 int res;
developer72fb0bb2023-01-11 09:46:29 +080016174
developera3511852023-06-14 14:12:59 +080016175 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016176
developera3511852023-06-14 14:12:59 +080016177 if (mu_type == NULL)
16178 return RETURN_ERR;
16179 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016180 if (band == band_invalid) {
16181 printf("%s:Band Error\n", __func__);
16182 return RETURN_ERR;
16183 }
developere40952c2023-06-15 18:46:43 +080016184 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16185 if (os_snprintf_error(sizeof(dat_file), res)) {
16186 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16187 return RETURN_ERR;
16188 }
developera1255e42023-05-13 17:45:02 +080016189 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016190 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16191 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016192
developera1255e42023-05-13 17:45:02 +080016193 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016194 if (token == NULL) {
16195 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16196 return RETURN_ERR;
16197 }
developerc14d83a2023-06-29 20:09:42 +080016198 if (hal_strtol(token, 10, &ofdma) < 0) {
16199 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080016200 }
developerc14d83a2023-06-29 20:09:42 +080016201
developera1255e42023-05-13 17:45:02 +080016202 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016203 if (token == NULL) {
16204 wifi_debug(DEBUG_ERROR, "Unexpected strtok fail\n");
16205 return RETURN_ERR;
16206 }
developerc14d83a2023-06-29 20:09:42 +080016207
16208 if (hal_strtol(token, 10, &mimo) < 0) {
16209 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +080016210 }
developerc14d83a2023-06-29 20:09:42 +080016211
developer9f2358c2023-09-22 18:42:12 +080016212 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%ld,mimo=%ld\n", __func__, ofdma, mimo);
developera1255e42023-05-13 17:45:02 +080016213 if ((ofdma == 1) && (mimo == 1))
16214 *mu_type = WIFI_DL_MU_TYPE_OFDMA_MIMO;
16215 else if ((ofdma == 0) && (mimo == 1))
16216 *mu_type = WIFI_DL_MU_TYPE_MIMO;
16217 else if ((ofdma == 1) && (mimo == 0))
16218 *mu_type = WIFI_DL_MU_TYPE_OFDMA;
16219 else
16220 *mu_type = WIFI_DL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016221 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16222 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016223}
16224
16225INT wifi_setUplinkMuType(INT radio_index, wifi_ul_mu_type_t mu_type)
16226{
developera3511852023-06-14 14:12:59 +080016227 // hemu onoff=<val> (bitmap- UL MU-MIMO(bit3), DL MU-MIMO(bit2), UL OFDMA(bit1), DL OFDMA(bit0))
developer863a4a62023-06-06 16:55:59 +080016228 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016229 wifi_band band = band_invalid;
16230 char ofdmabuf[32] = {'\0'};
16231 char mimobuf[32] = {'\0'};
16232 char new_ofdmabuf[32] = {'\0'};
16233 char new_mimobuf[32] = {'\0'};
16234 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016235 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16236 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16237 UCHAR bss_cnt = 0;
16238 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016239 int res;
developer72fb0bb2023-01-11 09:46:29 +080016240
developera3511852023-06-14 14:12:59 +080016241 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16242 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016243 if (band == band_invalid) {
16244 printf("%s:Band Error\n", __func__);
16245 return RETURN_ERR;
16246 }
16247 if ((mu_type < WIFI_UL_MU_TYPE_NONE)
16248 || (mu_type > WIFI_UL_MU_TYPE_OFDMA)) {
16249 printf("%s:mu_type input Error\n", __func__);
16250 return RETURN_ERR;
16251 }
developere40952c2023-06-15 18:46:43 +080016252 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16253 if (os_snprintf_error(sizeof(dat_file), res)) {
16254 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16255 return RETURN_ERR;
16256 }
developera1255e42023-05-13 17:45:02 +080016257 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016258 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16259 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016260 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16261 get_bssnum_byindex(radio_index, &bss_cnt);
16262 val_cnt = 2*bss_cnt - 1;
16263 printf("bssNumber:%d,ValCnt:%d\n", bss_cnt, val_cnt);
16264 if ((val_cnt >= sizeof(new_ofdmabuf))
16265 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016266 printf("%s:bss cnt Error\n", __func__);
developera1255e42023-05-13 17:45:02 +080016267 return RETURN_ERR;
16268 }
16269 /*translate set value*/
16270 if (mu_type == WIFI_UL_MU_TYPE_NONE) {
16271 strncpy(new_ofdmabuf, str_zero, val_cnt);
16272 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016273 }
developera1255e42023-05-13 17:45:02 +080016274 if (mu_type == WIFI_UL_MU_TYPE_OFDMA) {
16275 strncpy(new_ofdmabuf, str_one, val_cnt);
16276 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016277 }
developera1255e42023-05-13 17:45:02 +080016278 printf("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16279 /*same value, not operation*/
16280 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16281 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16282 printf("%s:Reduntant value\n", __func__);
16283 return RETURN_OK;
16284 }
16285 /*modify dat file to new file*/
16286 params[0].name="MuOfdmaUlEnable";
16287 params[0].value=new_ofdmabuf;
16288 params[1].name="MuMimoUlEnable";
16289 params[1].value=new_mimobuf;
16290 wifi_datfileWrite(dat_file, params, 2);
16291 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016292 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16293 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016294}
16295
16296INT wifi_getUplinkMuType(INT radio_index, wifi_ul_mu_type_t *mu_type)
16297{
developer863a4a62023-06-06 16:55:59 +080016298 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016299 wifi_band band = band_invalid;
16300 char ofdmabuf[32] = {'\0'};
16301 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016302 char *token = NULL;
16303 UCHAR ofdma = 0;
16304 UCHAR mimo = 0;
developere40952c2023-06-15 18:46:43 +080016305 int res;
developerc14d83a2023-06-29 20:09:42 +080016306 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016307
developera3511852023-06-14 14:12:59 +080016308 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016309
developera3511852023-06-14 14:12:59 +080016310 if (mu_type == NULL)
16311 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016312 band = wifi_index_to_band(radio_index);
16313 if (band == band_invalid) {
16314 printf("%s:Band Error", __func__);
16315 return RETURN_ERR;
16316 }
developere40952c2023-06-15 18:46:43 +080016317 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16318 if (os_snprintf_error(sizeof(dat_file), res)) {
16319 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16320 return RETURN_ERR;
16321 }
developera1255e42023-05-13 17:45:02 +080016322 /*get current value in dat file*/
16323 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16324 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016325
developera1255e42023-05-13 17:45:02 +080016326 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016327 if (token == NULL) {
16328 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16329 return RETURN_ERR;
16330 }
developerc14d83a2023-06-29 20:09:42 +080016331
16332 if (hal_strtoul(token, 10, &tmp) < 0) {
16333 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016334 }
developerc14d83a2023-06-29 20:09:42 +080016335 ofdma = tmp;
developera1255e42023-05-13 17:45:02 +080016336 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016337 if (token == NULL) {
16338 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16339 return RETURN_ERR;
16340 }
developer5b23cd02023-07-19 20:26:03 +080016341
developerc14d83a2023-06-29 20:09:42 +080016342 if (hal_strtoul(token, 10, &tmp) < 0) {
16343 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +080016344 }
developerc14d83a2023-06-29 20:09:42 +080016345 mimo = tmp;
16346
developera1255e42023-05-13 17:45:02 +080016347 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d, mimo=%d\n", __func__, ofdma, mimo);
16348 if ((ofdma == 1) && (mimo == 0))
16349 *mu_type = WIFI_UL_MU_TYPE_OFDMA;
16350 else
16351 *mu_type = WIFI_UL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016352 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16353 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016354}
16355
16356
16357INT wifi_setGuardInterval(INT radio_index, wifi_guard_interval_t guard_interval)
16358{
developer8078acf2023-08-04 18:52:48 +080016359
developera3511852023-06-14 14:12:59 +080016360 char buf[256] = {0};
16361 char config_file[64] = {0};
16362 char GI[8] = {0};
16363 UINT mode_map = 0;
16364 FILE *f = NULL;
16365 wifi_band band = band_invalid;
16366 char dat_file[64] = {'\0'};
16367 struct params params[3];
developere40952c2023-06-15 18:46:43 +080016368 int res;
developer72fb0bb2023-01-11 09:46:29 +080016369
developera3511852023-06-14 14:12:59 +080016370 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016371
developera3511852023-06-14 14:12:59 +080016372 if (wifi_getRadioMode(radio_index, buf, &mode_map) == RETURN_ERR) {
16373 wifi_dbg_printf("%s: wifi_getRadioMode return error\n", __func__);
16374 return RETURN_ERR;
16375 }
developera1255e42023-05-13 17:45:02 +080016376 /*sanity check*/
16377 if (((guard_interval == wifi_guard_interval_1600)
16378 || (guard_interval == wifi_guard_interval_3200))
developerdaf24792023-06-06 11:40:04 +080016379 && ((mode_map & (WIFI_MODE_BE | WIFI_MODE_AX)) == 0)) {
developera3511852023-06-14 14:12:59 +080016380 wifi_dbg_printf("%s: N/AC Mode not support 1600/3200ns GI\n", __func__);
16381 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016382 }
developere40952c2023-06-15 18:46:43 +080016383 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16384 if (os_snprintf_error(sizeof(config_file), res)) {
16385 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16386 return RETURN_ERR;
16387 }
developera3511852023-06-14 14:12:59 +080016388 band = wifi_index_to_band(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080016389
developera3511852023-06-14 14:12:59 +080016390 // Hostapd are not supported HE mode GI 1600, 3200 ns.
16391 if (guard_interval == wifi_guard_interval_800) { // remove all capab about short GI
developer8078acf2023-08-04 18:52:48 +080016392 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i 's/\\[SHORT-GI-(.){1,2}0\\]//g' %s", config_file);
16393 if (res) {
16394 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
16395
developere40952c2023-06-15 18:46:43 +080016396 }
developer8078acf2023-08-04 18:52:48 +080016397
developera3511852023-06-14 14:12:59 +080016398 } else if (guard_interval == wifi_guard_interval_400 || guard_interval == wifi_guard_interval_auto){
16399 wifi_hostapdRead(config_file, "ht_capab", buf, sizeof(buf));
16400 if (strstr(buf, "[SHORT-GI-") == NULL) {
developer8078acf2023-08-04 18:52:48 +080016401 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^ht_capab=.*/s/$/[SHORT-GI-20][SHORT-GI-40]/' %s", config_file);
16402 if (res) {
16403 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
16404
developere40952c2023-06-15 18:46:43 +080016405 }
developer8078acf2023-08-04 18:52:48 +080016406
developera3511852023-06-14 14:12:59 +080016407 }
16408 if (band == band_5) {
16409 wifi_hostapdRead(config_file, "vht_capab", buf, sizeof(buf));
16410 if (strstr(buf, "[SHORT-GI-") == NULL) {
developer8078acf2023-08-04 18:52:48 +080016411 res = _syscmd_secure(buf, sizeof(buf), "sed -r -i '/^vht_capab=.*/s/$/[SHORT-GI-80][SHORT-GI-160]/' %s", config_file);
16412 if (res) {
16413 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
16414
developere40952c2023-06-15 18:46:43 +080016415 }
developer8078acf2023-08-04 18:52:48 +080016416
developera3511852023-06-14 14:12:59 +080016417 }
16418 }
16419 }
16420 /*wifi_reloadAp(radio_index);
developera1255e42023-05-13 17:45:02 +080016421 caller "wifi_setRadioOperatingParameters" have done this step.
16422 */
developere40952c2023-06-15 18:46:43 +080016423 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16424 if (os_snprintf_error(sizeof(dat_file), res)) {
16425 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16426 return RETURN_ERR;
16427 }
developera3511852023-06-14 14:12:59 +080016428 if (guard_interval == wifi_guard_interval_400) {
developera1255e42023-05-13 17:45:02 +080016429 params[0].name = "HT_GI";
16430 params[0].value = "1";
16431 params[1].name = "VHT_SGI";
16432 params[1].value = "1";
16433 wifi_datfileWrite(dat_file, params, 2);
developer32f2a182023-06-27 19:50:41 +080016434 memcpy(GI, "0.4", 3);
developera1255e42023-05-13 17:45:02 +080016435 } else {
16436 params[0].name = "HT_GI";
16437 params[0].value = "0";
16438 params[1].name = "VHT_SGI";
16439 params[1].value = "0";
16440 /*should enable FIXED_HE_GI_SUPPORT in driver*/
16441 params[2].name = "FgiFltf";
16442 if (guard_interval == wifi_guard_interval_800) {
16443 params[2].value = "800";
developer32f2a182023-06-27 19:50:41 +080016444 memcpy(GI, "0.8", 3);
developera1255e42023-05-13 17:45:02 +080016445 } else if (guard_interval == wifi_guard_interval_1600) {
16446 params[2].value = "1600";
developer32f2a182023-06-27 19:50:41 +080016447 memcpy(GI, "1.6", 3);
developera1255e42023-05-13 17:45:02 +080016448 } else if (guard_interval == wifi_guard_interval_3200) {
16449 params[2].value = "3200";
developer32f2a182023-06-27 19:50:41 +080016450 memcpy(GI, "3.2", 3);
developera1255e42023-05-13 17:45:02 +080016451 } else if (guard_interval == wifi_guard_interval_auto) {
16452 params[2].value = "0";
developer32f2a182023-06-27 19:50:41 +080016453 memcpy(GI, "auto", 4);
developera1255e42023-05-13 17:45:02 +080016454 }
16455 wifi_datfileWrite(dat_file, params, 3);
16456 }
developera3511852023-06-14 14:12:59 +080016457 // Record GI for get GI function
developere40952c2023-06-15 18:46:43 +080016458 res = snprintf(buf, sizeof(buf), "%s%d.txt", GUARD_INTERVAL_FILE, radio_index);
16459 if (os_snprintf_error(sizeof(buf), res)) {
16460 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16461 return RETURN_ERR;
16462 }
developera3511852023-06-14 14:12:59 +080016463 f = fopen(buf, "w");
16464 if (f == NULL)
16465 return RETURN_ERR;
16466 fprintf(f, "%s", GI);
developerc14d83a2023-06-29 20:09:42 +080016467 if (fclose(f) == EOF)
16468 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016469 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16470 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016471}
16472
16473INT wifi_getGuardInterval(INT radio_index, wifi_guard_interval_t *guard_interval)
16474{
developera3511852023-06-14 14:12:59 +080016475 char buf[32] = {0};
developere40952c2023-06-15 18:46:43 +080016476 int res;
developer72fb0bb2023-01-11 09:46:29 +080016477
developera3511852023-06-14 14:12:59 +080016478 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016479
developera3511852023-06-14 14:12:59 +080016480 if (guard_interval == NULL)
16481 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016482
developer8078acf2023-08-04 18:52:48 +080016483 res = _syscmd_secure(buf, sizeof(buf), "cat %s%d.txt 2> /dev/null", GUARD_INTERVAL_FILE, radio_index);
16484 if (res) {
16485 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +080016486 }
developer72fb0bb2023-01-11 09:46:29 +080016487
developera3511852023-06-14 14:12:59 +080016488 if (strncmp(buf, "0.4", 3) == 0)
16489 *guard_interval = wifi_guard_interval_400;
16490 else if (strncmp(buf, "0.8", 3) == 0)
16491 *guard_interval = wifi_guard_interval_800;
16492 else if (strncmp(buf, "1.6", 3) == 0)
16493 *guard_interval = wifi_guard_interval_1600;
16494 else if (strncmp(buf, "3.2", 3) == 0)
16495 *guard_interval = wifi_guard_interval_3200;
16496 else
16497 *guard_interval = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +080016498
developera3511852023-06-14 14:12:59 +080016499 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16500 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016501}
16502
16503INT wifi_setBSSColor(INT radio_index, UCHAR color)
16504{
developera3511852023-06-14 14:12:59 +080016505 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16506 struct params params = {0};
16507 char config_file[128] = {0};
16508 char bss_color[4] ={0};
developere40952c2023-06-15 18:46:43 +080016509 int res;
developer72fb0bb2023-01-11 09:46:29 +080016510
developera1255e42023-05-13 17:45:02 +080016511 if (color < 1 || color > 63) {
16512 wifi_dbg_printf("color value is err:%d.\n", color);
16513 return RETURN_ERR;
16514 }
developera3511852023-06-14 14:12:59 +080016515 params.name = "he_bss_color";
developere40952c2023-06-15 18:46:43 +080016516 res = snprintf(bss_color, sizeof(bss_color), "%hhu", color);
16517 if (os_snprintf_error(sizeof(bss_color), res)) {
16518 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16519 return RETURN_ERR;
16520 }
developera3511852023-06-14 14:12:59 +080016521 params.value = bss_color;
developer75bd10c2023-06-27 11:34:08 +080016522
16523 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16524 if (os_snprintf_error(sizeof(config_file), res)) {
16525 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16526 return RETURN_ERR;
16527 }
developera3511852023-06-14 14:12:59 +080016528 wifi_hostapdWrite(config_file, &params, 1);
16529 //wifi_hostapdProcessUpdate(radio_index, &params, 1);
developera1255e42023-05-13 17:45:02 +080016530 wifi_reloadAp(radio_index);
developer69b61b02023-03-07 17:17:44 +080016531
developera3511852023-06-14 14:12:59 +080016532 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16533 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016534}
16535
16536INT wifi_getBSSColor(INT radio_index, UCHAR *color)
16537{
developera3511852023-06-14 14:12:59 +080016538 char config_file[128] = {0};
16539 char buf[64] = {0};
16540 char temp_output[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +080016541 int res;
developerc14d83a2023-06-29 20:09:42 +080016542 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016543
developera3511852023-06-14 14:12:59 +080016544 wifi_dbg_printf("\nFunc=%s\n", __func__);
16545 if (NULL == color)
16546 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016547
developer75bd10c2023-06-27 11:34:08 +080016548 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16549 if (os_snprintf_error(sizeof(config_file), res)) {
16550 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16551 return RETURN_ERR;
16552 }
developera3511852023-06-14 14:12:59 +080016553 wifi_hostapdRead(config_file, "he_bss_color", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016554
developera3511852023-06-14 14:12:59 +080016555 if(strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +080016556 res = snprintf(temp_output, sizeof(temp_output), "%s", buf);
developera3511852023-06-14 14:12:59 +080016557 } else {
developere40952c2023-06-15 18:46:43 +080016558 res = snprintf(temp_output, sizeof(temp_output), "1"); // default value
16559 }
16560 if (os_snprintf_error(sizeof(temp_output), res)) {
16561 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16562 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080016563 }
developer72fb0bb2023-01-11 09:46:29 +080016564
developerc14d83a2023-06-29 20:09:42 +080016565 if (hal_strtoul(temp_output, 10, &tmp) < 0) {
16566 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016567 }
developerc14d83a2023-06-29 20:09:42 +080016568 *color = tmp;
developera3511852023-06-14 14:12:59 +080016569 wifi_dbg_printf("\noutput_string=%s\n", color);
developer72fb0bb2023-01-11 09:46:29 +080016570
developera3511852023-06-14 14:12:59 +080016571 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016572}
16573
16574/* multi-psk support */
16575INT wifi_getMultiPskClientKey(INT apIndex, mac_address_t mac, wifi_key_multi_psk_t *key)
16576{
developera3511852023-06-14 14:12:59 +080016577 char cmd[256];
16578 char interface_name[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080016579 int res;
developer72fb0bb2023-01-11 09:46:29 +080016580
developera3511852023-06-14 14:12:59 +080016581 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16582 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016583
developer75bd10c2023-06-27 11:34:08 +080016584 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 +080016585 interface_name,
16586 mac[0],
16587 mac[1],
16588 mac[2],
16589 mac[3],
16590 mac[4],
16591 mac[5]
16592 );
developer75bd10c2023-06-27 11:34:08 +080016593 if (os_snprintf_error(sizeof(cmd), res)) {
16594 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16595 return RETURN_ERR;
16596 }
developera3511852023-06-14 14:12:59 +080016597 printf("DEBUG LOG wifi_getMultiPskClientKey(%s)\n",cmd);
developer8078acf2023-08-04 18:52:48 +080016598
16599 res = _syscmd_secure(key->wifi_keyId, 64, "hostapd_cli -i %s sta %x:%x:%x:%x:%x:%x |grep '^keyid' | cut -f 2 -d = | tr -d '\n'",interface_name,mac[0],mac[1],mac[2], mac[3], mac[4], mac[5]);
16600 if(res) {
16601 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
16602 }
developer72fb0bb2023-01-11 09:46:29 +080016603
16604
developera3511852023-06-14 14:12:59 +080016605 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016606}
16607
16608INT wifi_pushMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16609{
developera3511852023-06-14 14:12:59 +080016610 char interface_name[16] = {0};
16611 FILE *fd = NULL;
16612 char fname[100];
developer8078acf2023-08-04 18:52:48 +080016613
developera3511852023-06-14 14:12:59 +080016614 char out[64] = {0};
16615 wifi_key_multi_psk_t * key = NULL;
developer75bd10c2023-06-27 11:34:08 +080016616 int res, ret;
developere40952c2023-06-15 18:46:43 +080016617
developera3511852023-06-14 14:12:59 +080016618 if(keysNumber < 0)
16619 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016620
developere40952c2023-06-15 18:46:43 +080016621 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16622 if (os_snprintf_error(sizeof(fname), res)) {
16623 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16624 return RETURN_ERR;
16625 }
developera3511852023-06-14 14:12:59 +080016626 fd = fopen(fname, "w");
16627 if (!fd) {
16628 return RETURN_ERR;
16629 }
16630 key= (wifi_key_multi_psk_t *) keys;
16631 for(int i=0; i<keysNumber; ++i, key++) {
developer75bd10c2023-06-27 11:34:08 +080016632 ret = fprintf(fd, "keyid=%s 00:00:00:00:00:00 %s\n", key->wifi_keyId, key->wifi_psk);
16633 if (ret < 0)
16634 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developera3511852023-06-14 14:12:59 +080016635 }
developerd14dff12023-06-28 22:47:44 +080016636 if (fclose(fd) != 0) {
16637 wifi_debug(DEBUG_ERROR, "fclose fail\n");
16638 return RETURN_ERR;
16639 }
developer72fb0bb2023-01-11 09:46:29 +080016640
developera3511852023-06-14 14:12:59 +080016641 //reload file
16642 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16643 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080016644 res = _syscmd_secure(out, 64, "hostapd_cli -i%s raw RELOAD_WPA_PSK", interface_name);
developer32f2a182023-06-27 19:50:41 +080016645
developer8078acf2023-08-04 18:52:48 +080016646 if (res) {
16647 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
16648
developer32f2a182023-06-27 19:50:41 +080016649 }
developer8078acf2023-08-04 18:52:48 +080016650
developera3511852023-06-14 14:12:59 +080016651 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016652}
16653
16654INT wifi_getMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16655{
developera3511852023-06-14 14:12:59 +080016656 FILE *fd = NULL;
16657 char fname[100];
16658 char * line = NULL;
16659 char * pos = NULL;
16660 size_t len = 0;
16661 ssize_t read = 0;
16662 INT ret = RETURN_OK;
16663 wifi_key_multi_psk_t *keys_it = NULL;
developere40952c2023-06-15 18:46:43 +080016664 int res;
developer72fb0bb2023-01-11 09:46:29 +080016665
developera3511852023-06-14 14:12:59 +080016666 if (keysNumber < 1) {
16667 return RETURN_ERR;
16668 }
developer72fb0bb2023-01-11 09:46:29 +080016669
developere40952c2023-06-15 18:46:43 +080016670 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16671 if (os_snprintf_error(sizeof(fname), res)) {
16672 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16673 return RETURN_ERR;
16674 }
developera3511852023-06-14 14:12:59 +080016675 fd = fopen(fname, "r");
16676 if (!fd) {
16677 return RETURN_ERR;
16678 }
developer72fb0bb2023-01-11 09:46:29 +080016679
developera3511852023-06-14 14:12:59 +080016680 if (keys == NULL) {
16681 ret = RETURN_ERR;
16682 goto close;
16683 }
developer72fb0bb2023-01-11 09:46:29 +080016684
developera3511852023-06-14 14:12:59 +080016685 keys_it = keys;
16686 while ((read = getline(&line, &len, fd)) != -1) {
16687 //Strip trailing new line if present
16688 if (read > 0 && line[read-1] == '\n') {
16689 line[read-1] = '\0';
16690 }
developer72fb0bb2023-01-11 09:46:29 +080016691
developera3511852023-06-14 14:12:59 +080016692 if(strcmp(line,"keyid=")) {
developer37646972023-06-29 10:58:43 +080016693 if (sscanf(line, "keyid=%63s", keys_it->wifi_keyId) == EOF)
16694 continue;
developera3511852023-06-14 14:12:59 +080016695 if (!(pos = index(line, ' '))) {
16696 ret = RETURN_ERR;
16697 goto close;
16698 }
16699 pos++;
16700 //Here should be 00:00:00:00:00:00
16701 if (!(strcmp(pos,"00:00:00:00:00:00"))) {
16702 printf("Not supported MAC: %s\n", pos);
16703 }
16704 if (!(pos = index(pos, ' '))) {
16705 ret = RETURN_ERR;
16706 goto close;
16707 }
16708 pos++;
developer72fb0bb2023-01-11 09:46:29 +080016709
developera3511852023-06-14 14:12:59 +080016710 //The rest is PSK
developere40952c2023-06-15 18:46:43 +080016711 res = snprintf(&keys_it->wifi_psk[0], sizeof(keys_it->wifi_psk), "%s", pos);
16712 if (os_snprintf_error(sizeof(keys_it->wifi_psk), res)) {
16713 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +080016714 if (fclose(fd) == EOF)
16715 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developer5b23cd02023-07-19 20:26:03 +080016716
developere40952c2023-06-15 18:46:43 +080016717 return RETURN_ERR;
16718 }
16719
developera3511852023-06-14 14:12:59 +080016720 keys_it++;
developer72fb0bb2023-01-11 09:46:29 +080016721
developera3511852023-06-14 14:12:59 +080016722 if(--keysNumber <= 0)
developer72fb0bb2023-01-11 09:46:29 +080016723 break;
developera3511852023-06-14 14:12:59 +080016724 }
16725 }
developer72fb0bb2023-01-11 09:46:29 +080016726
16727close:
developera3511852023-06-14 14:12:59 +080016728 free(line);
developer37646972023-06-29 10:58:43 +080016729 if (fclose(fd) == EOF)
16730 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016731 return ret;
developer72fb0bb2023-01-11 09:46:29 +080016732}
16733/* end of multi-psk support */
16734
16735INT wifi_setNeighborReports(UINT apIndex,
developera3511852023-06-14 14:12:59 +080016736 UINT numNeighborReports,
16737 wifi_NeighborReport_t *neighborReports)
developer72fb0bb2023-01-11 09:46:29 +080016738{
developera3511852023-06-14 14:12:59 +080016739 char hex_bssid[13] = { 0 };
16740 char bssid[18] = { 0 };
16741 char nr[100] = { 0 };
16742 char ssid[32];
16743 char hex_ssid[32];
16744 char interface_name[16] = {0};
16745 INT ret;
developere40952c2023-06-15 18:46:43 +080016746 int res;
developerd14dff12023-06-28 22:47:44 +080016747 unsigned char hex_ssid_len;
developer72fb0bb2023-01-11 09:46:29 +080016748
developera3511852023-06-14 14:12:59 +080016749 /*rmeove all neighbors*/
16750 wifi_dbg_printf("\n[%s]: removing all neighbors from %s\n", __func__, interface_name);
16751 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16752 return RETURN_ERR;
developer33f13ba2023-07-12 16:19:06 +080016753
16754 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 +080016755 interface_name, interface_name);
16756
developer33f13ba2023-07-12 16:19:06 +080016757 if (res) {
16758 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +080016759 return RETURN_ERR;
16760 }
developer72fb0bb2023-01-11 09:46:29 +080016761
developera3511852023-06-14 14:12:59 +080016762 for(unsigned int i = 0; i < numNeighborReports; i++)
16763 {
16764 memset(ssid, 0, sizeof(ssid));
16765 ret = wifi_getSSIDName(apIndex, ssid);
16766 if (ret != RETURN_OK)
16767 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016768
developera3511852023-06-14 14:12:59 +080016769 memset(hex_ssid, 0, sizeof(hex_ssid));
developerd14dff12023-06-28 22:47:44 +080016770 hex_ssid_len = sizeof(hex_ssid);
16771 for(size_t j = 0,k = 0; ssid[j] != '\0' && k < sizeof(hex_ssid); j++,k+=2 ) {
16772 res = snprintf(hex_ssid + k, hex_ssid_len, "%02x", ssid[j]);
16773
16774 if (os_snprintf_error(hex_ssid_len, res)) {
16775 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16776 return RETURN_ERR;
16777 }
16778 hex_ssid_len = sizeof(hex_ssid) - strlen(hex_ssid);
16779 }
developer72fb0bb2023-01-11 09:46:29 +080016780
developere40952c2023-06-15 18:46:43 +080016781 res = snprintf(hex_bssid, sizeof(hex_bssid),
developera3511852023-06-14 14:12:59 +080016782 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
16783 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 +080016784 if (os_snprintf_error(sizeof(hex_bssid), res)) {
16785 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16786 return RETURN_ERR;
16787 }
16788 res = snprintf(bssid, sizeof(bssid),
developera3511852023-06-14 14:12:59 +080016789 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
16790 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 +080016791 if (os_snprintf_error(sizeof(bssid), res)) {
16792 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16793 return RETURN_ERR;
16794 }
developer72fb0bb2023-01-11 09:46:29 +080016795
developere40952c2023-06-15 18:46:43 +080016796 res = snprintf(nr, sizeof(nr),
developera3511852023-06-14 14:12:59 +080016797 "%s" // bssid
16798 "%02hhx%02hhx%02hhx%02hhx" // bssid_info
16799 "%02hhx" // operclass
16800 "%02hhx" // channel
16801 "%02hhx", // phy_mode
16802 hex_bssid,
16803 neighborReports[i].info & 0xff, (neighborReports[i].info >> 8) & 0xff,
16804 (neighborReports[i].info >> 16) & 0xff, (neighborReports[i].info >> 24) & 0xff,
16805 neighborReports[i].opClass,
16806 neighborReports[i].channel,
16807 neighborReports[i].phyTable);
developere40952c2023-06-15 18:46:43 +080016808 if (os_snprintf_error(sizeof(nr), res)) {
16809 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16810 return RETURN_ERR;
16811 }
developer72fb0bb2023-01-11 09:46:29 +080016812
developer33f13ba2023-07-12 16:19:06 +080016813 res = v_secure_system("hostapd_cli set_neighbor %s ssid=%s nr=%s -i %s",
16814 bssid, hex_ssid, nr, interface_name);
16815 if (res) {
16816 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080016817 return RETURN_ERR;
16818 }
developera3511852023-06-14 14:12:59 +080016819 }
developer72fb0bb2023-01-11 09:46:29 +080016820
developera3511852023-06-14 14:12:59 +080016821 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016822}
16823
16824INT wifi_getApInterworkingElement(INT apIndex, wifi_InterworkingElement_t *output_struct)
16825{
developera3511852023-06-14 14:12:59 +080016826 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016827}
16828
16829#ifdef _WIFI_HAL_TEST_
16830int main(int argc,char **argv)
16831{
developera3511852023-06-14 14:12:59 +080016832 int index;
16833 INT ret=0;
16834 char buf[1024]="";
developer72fb0bb2023-01-11 09:46:29 +080016835
developera3511852023-06-14 14:12:59 +080016836 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16837 if(argc<3)
16838 {
16839 if(argc==2)
16840 {
16841 if(!strcmp(argv[1], "init"))
16842 return wifi_init();
16843 if(!strcmp(argv[1], "reset"))
16844 return wifi_reset();
16845 if(!strcmp(argv[1], "wifi_getHalVersion"))
16846 {
16847 char buffer[64];
16848 if(wifi_getHalVersion(buffer)==RETURN_OK)
16849 printf("Version: %s\n", buffer);
16850 else
16851 printf("Error in wifi_getHalVersion\n");
16852 return RETURN_OK;
16853 }
16854 }
16855 printf("wifihal <API> <radioIndex> <arg1> <arg2> ...\n");
16856 exit(-1);
16857 }
developer72fb0bb2023-01-11 09:46:29 +080016858
developera3511852023-06-14 14:12:59 +080016859 index = atoi(argv[2]);
developer33f13ba2023-07-12 16:19:06 +080016860 if (strstr(argv[1], "test_system")!=NULL) {
16861 ret = v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
16862 printf("ret = %d\n", ret);
16863
16864 ret = v_secure_system("hostapd_cli show_neighbor -i %s | awk '{print $1 \" \" $2}' | xargs -n2 -r hostapd_cli remove_neighbor -i %s",
16865 "ra0", "ra0");
16866 printf("ret = %d\n", ret);
16867
16868 ret = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", "1,2,3");
16869 printf("ret = %d\n", ret);
16870
16871 ret = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", "rai0");
16872 printf("ret = %d\n", ret);
16873
16874 ret = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", "rax0");
16875 printf("ret = %d\n", ret);
16876
16877 ret = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", "ra0");
16878 printf("ret = %d\n", ret);
16879 ret = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", "ra0");
16880 printf("ret = %d\n", ret);
16881 ret = v_secure_system("ping -q -c 1 -W 1 \"%s\" > /dev/console 2>&1", "20.1.1.101");
16882 printf("ret = %d\n", ret);
16883
16884 return 0;
16885 }
16886
16887 if (strstr(argv[1], "test_popen")!=NULL) {
16888 FILE *fp = v_secure_popen("w", "cat");
16889 fprintf(fp, "popen write success\n");
16890 fclose(fp);
16891
16892 char buf[1024];
16893 memset(buf, 0, sizeof(buf));
16894 fp = v_secure_popen("r", "echo popen read success");
16895 if (fp == NULL) {
16896 printf("v_secure_popen failed\n");
16897 } else {
16898 if (fgets(buf, sizeof(buf), fp) == NULL) {
16899 printf("v_secure_popen read error\n");
16900 } else {
16901 printf("%s\n", buf);
16902 }
16903 v_secure_pclose(fp);
16904 }
16905
16906#if 0
16907 fp = v_secure_popen("r", "hostapd_cli -i %s reload", "ra0");
16908 if (fp == NULL) {
16909 printf("v_secure_popen failed\n");
16910 } else {
16911 if (fgets(buf, sizeof(buf), fp) == NULL) {
16912 printf("v_secure_popen read error\n");
16913 } else {
16914 printf("%s", buf);
16915 }
16916 v_secure_pclose(fp);
16917 }
16918#endif
16919
16920 long int band;
16921 memset(buf, 0, sizeof(buf));
16922 ret = _syscmd_secure(buf, sizeof(buf),
16923 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 0);
16924 if (hal_strtol(buf, 16, &band) < 0) {
16925 wifi_debug(DEBUG_ERROR, "strtol fail\n");
16926 }
16927 printf("ret = %d, band0=%ld\n", ret, band);
16928
16929 memset(buf, 0, sizeof(buf));
16930 ret = _syscmd_secure(buf, sizeof(buf),
16931 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 1);
16932 if (hal_strtol(buf, 16, &band) < 0) {
16933 wifi_debug(DEBUG_ERROR, "strtol fail\n");
16934 }
16935 printf("ret = %d, band1=%ld\n", ret, band);
16936
16937 memset(buf, 0, sizeof(buf));
16938 ret = _syscmd_secure(buf, sizeof(buf),
16939 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 2);
16940 if (hal_strtol(buf, 16, &band) < 0) {
16941 wifi_debug(DEBUG_ERROR, "strtol fail\n");
16942 }
16943 printf("ret = %d, band2=%ld\n", ret, band);
16944 }
16945
developera3511852023-06-14 14:12:59 +080016946 if(strstr(argv[1], "wifi_getApName")!=NULL)
16947 {
16948 wifi_getApName(index,buf);
16949 printf("Ap name is %s \n",buf);
16950 return 0;
16951 }
developerb14b3462023-07-01 18:02:42 +080016952 if(strstr(argv[1], "wifi_getRadioExtChannel")!=NULL)
16953 {
16954 wifi_getRadioExtChannel(index,buf);
16955 printf("extchannel is %s \n",buf);
16956 return 0;
16957 }
developer37ad6bf2023-10-09 11:31:05 +080016958 if (strstr(argv[1], "wifi_setRadioAMSDUEnable")!=NULL) {
16959 unsigned char enable = atoi(argv[3]);
16960 BOOL out_put;
16961 if (enable)
16962 wifi_setRadioAMSDUEnable(index, TRUE);
16963 else
16964 wifi_setRadioAMSDUEnable(index, FALSE);
16965 wifi_getRadioAMSDUEnable(index, &out_put);
16966 printf("amsdu = %d\n", out_put);
16967 }
16968 if (strstr(argv[1], "wifi_setApIsolationEnable")!=NULL) {
16969 unsigned char enable = atoi(argv[3]);
16970 BOOL out_put;
16971 if (enable)
16972 wifi_setApIsolationEnable(index, TRUE);
16973 else
16974 wifi_setApIsolationEnable(index, FALSE);
16975 wifi_getApIsolationEnable(index, &out_put);
16976 printf("isolation input=%d, output=%d\n", enable, out_put);
16977 }
developerfead3972023-05-25 20:15:02 +080016978 if(strstr(argv[1], "wifi_setRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080016979 {
developer2a6abc92023-10-17 17:19:14 +080016980 UINT pureMode;
16981 if(argc <= 3)
16982 {
16983 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
16984 exit(-1);
16985 }
developerfead3972023-05-25 20:15:02 +080016986
developer2a6abc92023-10-17 17:19:14 +080016987 pureMode = atoi(argv[3]);
developera3511852023-06-14 14:12:59 +080016988 wifi_setRadioMode(index, NULL, pureMode);
16989 printf("Ap SET Radio mode 0x%x\n", pureMode);
16990 return 0;
16991 }
16992 if (strstr(argv[1], "wifi_setRadioAutoBlockAckEnable") != NULL) {
developer2a6abc92023-10-17 17:19:14 +080016993 unsigned char enable;
16994 if(argc <= 3)
16995 {
16996 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
16997 exit(-1);
16998 }
16999 enable = atoi(argv[3]);
developera3511852023-06-14 14:12:59 +080017000 if (enable)
17001 wifi_setRadioAutoBlockAckEnable(index, TRUE);
17002 else
17003 wifi_setRadioAutoBlockAckEnable(index, FALSE);
17004 printf("%s handle wifi_setRadioAutoBlockAckEnable\n", __FUNCTION__);
17005 }
developera39cfb22023-06-20 16:28:17 +080017006 if(strstr(argv[1], "wifi_setRadioTrafficStatsRadioStatisticsEnable")!=NULL)
17007 {
17008 wifi_setRadioTrafficStatsRadioStatisticsEnable(index, TRUE);
17009 printf("Ap SET wifi_setRadioTrafficStatsRadioStatisticsEnable\n");
17010 return 0;
17011 }
17012 if(strstr(argv[1], "wifi_setRadioTrafficStatsMeasure")!=NULL)
17013 {
17014 wifi_radioTrafficStatsMeasure_t input = {30, 200};
17015
17016 wifi_setRadioTrafficStatsMeasure(index, &input);
17017 printf("Ap SET wifi_setRadioTrafficStatsMeasure\n");
17018 return 0;
17019 }
developerfead3972023-05-25 20:15:02 +080017020 if(strstr(argv[1], "wifi_setRadioTransmitPower")!=NULL)
developera3511852023-06-14 14:12:59 +080017021 {
developer2a6abc92023-10-17 17:19:14 +080017022 ULONG TransmitPower;
17023 if(argc <= 3)
17024 {
17025 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17026 exit(-1);
17027 }
17028 TransmitPower = atoi(argv[3]);
developera3511852023-06-14 14:12:59 +080017029 wifi_setRadioTransmitPower(index, TransmitPower);
17030 printf("Ap SET TransmitPower %lu\n", TransmitPower);
17031 return 0;
17032 }
developerfead3972023-05-25 20:15:02 +080017033 if(strstr(argv[1], "wifi_setApManagementFramePowerControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017034 {
developer2a6abc92023-10-17 17:19:14 +080017035 INT TransmitPower;
17036 if(argc <= 3)
17037 {
17038 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17039 exit(-1);
17040 }
17041 TransmitPower = atoi(argv[3]);
developera3511852023-06-14 14:12:59 +080017042 wifi_setApManagementFramePowerControl(index, TransmitPower);
17043 printf("Ap SET Mgnt TransmitPower %d\n", TransmitPower);
17044 return 0;
17045 }
developerfead3972023-05-25 20:15:02 +080017046 if(strstr(argv[1], "wifi_setRadioBW")!=NULL)
developera3511852023-06-14 14:12:59 +080017047 {
developer2a6abc92023-10-17 17:19:14 +080017048 if(argc <= 3)
17049 {
17050 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17051 exit(-1);
17052 }
developerfead3972023-05-25 20:15:02 +080017053
developer2a6abc92023-10-17 17:19:14 +080017054 wifi_setRadioOperatingChannelBandwidth(index, argv[3]);
17055 printf("Ap SET bw %s\n", argv[3]);
developera3511852023-06-14 14:12:59 +080017056 return 0;
17057 }
developerfead3972023-05-25 20:15:02 +080017058 if(strstr(argv[1], "wifi_factoryResetRadio")!=NULL)
developera3511852023-06-14 14:12:59 +080017059 {
17060 wifi_factoryResetRadio(index);
17061 printf("wifi_factoryResetRadio ok!\n");
17062 return 0;
17063 }
developerfead3972023-05-25 20:15:02 +080017064 if(strstr(argv[1], "wifi_getRadioResetCount")!=NULL)
developera3511852023-06-14 14:12:59 +080017065 {
17066 ULONG rst_cnt;
17067 wifi_getRadioResetCount(index, &rst_cnt);
17068 printf("wifi_factoryResetRadio rst_cnt = %lu\n", rst_cnt);
17069 return 0;
17070 }
developer2edaf012023-05-24 14:24:53 +080017071 if (strncmp(argv[1], "wifi_addApAclDevice", strlen(argv[1])) == 0) {
developer49b17232023-05-19 16:35:19 +080017072 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017073 {
17074 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17075 exit(-1);
17076 }
developer49b17232023-05-19 16:35:19 +080017077 wifi_addApAclDevice(index, argv[3]);
17078 return 0;
17079 }
developer2edaf012023-05-24 14:24:53 +080017080 if (strncmp(argv[1], "wifi_getApAclDevices", strlen(argv[1])) == 0) {
17081 wifi_getApAclDevices(index, buf, 1024);
17082 wifi_debug(DEBUG_NOTICE, "Ap acl Devices: %s\n", buf);
developer121a8e72023-05-22 09:19:39 +080017083 return 0;
17084 }
developer2edaf012023-05-24 14:24:53 +080017085 if (strncmp(argv[1], "wifi_delApAclDevice", strlen(argv[1])) == 0) {
17086 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017087 {
17088 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17089 exit(-1);
17090 }
developer2edaf012023-05-24 14:24:53 +080017091 wifi_delApAclDevice(index, argv[3]);
17092 return 0;
17093 }
17094 if (strncmp(argv[1], "wifi_delApAclDevices", strlen(argv[1])) == 0) {
17095 wifi_delApAclDevices(index);
17096 return 0;
17097 }
17098 if (strncmp(argv[1], "wifi_getApAclDeviceNum", strlen(argv[1])) == 0) {
developer863a4a62023-06-06 16:55:59 +080017099 UINT acl_num = 0;
developer2edaf012023-05-24 14:24:53 +080017100 wifi_getApAclDeviceNum(index, &acl_num);
17101 wifi_debug(DEBUG_NOTICE, "Ap acl numbers: %d\n", acl_num);
17102 return 0;
17103 }
17104 if (strncmp(argv[1], "wifi_getApDenyAclDevices", strlen(argv[1])) == 0) {
17105 wifi_getApDenyAclDevices(index, buf, 1024);
17106 wifi_debug(DEBUG_NOTICE, "Ap Deny Acl Devices: %s\n", buf);
17107 return 0;
17108 }
17109 if (strncmp(argv[1], "wifi_setApMacAddressControlMode", strlen(argv[1])) == 0) {
17110 int filter_mode = 0;
17111 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017112 {
17113 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17114 exit(-1);
17115 }
developer2edaf012023-05-24 14:24:53 +080017116 filter_mode = atoi(argv[3]);
17117 wifi_setApMacAddressControlMode(index,filter_mode);
17118 return 0;
17119 }
developer5cd4c862023-05-26 09:34:42 +080017120 if (strncmp(argv[1], "wifi_getRadioDeclineBARequestEnable", strlen(argv[1])) == 0) {
17121 BOOL output_bool = 0;
17122 wifi_getRadioDeclineBARequestEnable(index, &output_bool);
17123 wifi_debug(DEBUG_NOTICE, "Ap get radio ba decline enable: %d\n", output_bool);
17124 return 0;
17125 }
17126 if (strncmp(argv[1], "wifi_getRadioAutoBlockAckEnable", strlen(argv[1])) == 0) {
17127 BOOL output_bool = 0;
17128 wifi_getRadioAutoBlockAckEnable(index, &output_bool);
17129 wifi_debug(DEBUG_NOTICE, "Ap get radio auto_ba enable: %d\n", output_bool);
17130 return 0;
17131 }
17132
17133 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17134 int filter_mode = 0;
17135 wifi_getApMacAddressControlMode(index, &filter_mode);
17136 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17137 return 0;
17138 }
17139 if (strncmp(argv[1], "wifi_setRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17140 int enable = 0;
17141 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017142 {
17143 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17144 exit(-1);
17145 }
developer5cd4c862023-05-26 09:34:42 +080017146 enable = (BOOL)atoi(argv[3]);
17147 wifi_setRadioIGMPSnoopingEnable(index, enable);
17148 wifi_debug(DEBUG_NOTICE, "Ap set IGMP Snooping Enable: %d\n", enable);
17149 return 0;
17150 }
developer2a6abc92023-10-17 17:19:14 +080017151 if (strncmp(argv[1], "wifi_setRadioDCSEnable", strlen(argv[1])) == 0) {
developer326d4232023-06-15 16:45:30 +080017152 int enable = 0;
17153 if(argc <= 3 )
17154 {
17155 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17156 exit(-1);
17157 }
17158 enable = (BOOL)atoi(argv[3]);
17159 wifi_setRadioDCSEnable(index, enable);
17160 wifi_debug(DEBUG_NOTICE, "Ap set DCS Enable: %d\n", enable);
17161 return 0;
17162 }
developer2a6abc92023-10-17 17:19:14 +080017163 if (strncmp(argv[1], "wifi_getRadioAutoChannelRefreshPeriod", strlen(argv[1])) == 0) {
developer326d4232023-06-15 16:45:30 +080017164 ULONG period = 0;
developer5cd4c862023-05-26 09:34:42 +080017165
developer326d4232023-06-15 16:45:30 +080017166 wifi_getRadioAutoChannelRefreshPeriod(index, &period);
17167 wifi_debug(DEBUG_NOTICE, "Get RefreshPeriod: %ld\n", period);
17168 return 0;
17169 }
developer2a6abc92023-10-17 17:19:14 +080017170 if (strncmp(argv[1], "wifi_setRadioDfsRefreshPeriod", strlen(argv[1])) == 0) {
developer326d4232023-06-15 16:45:30 +080017171 ULONG period = 0;
developer2a6abc92023-10-17 17:19:14 +080017172 if(argc <= 3)
17173 {
17174 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17175 exit(-1);
17176 }
developer326d4232023-06-15 16:45:30 +080017177 period = (ULONG)atoi(argv[3]);
17178 wifi_setRadioDfsRefreshPeriod(index, period);
17179 wifi_debug(DEBUG_NOTICE, "Set RefreshPeriod: %ld\n", period);
17180 return 0;
17181 }
developer2a6abc92023-10-17 17:19:14 +080017182 if (strncmp(argv[1], "wifi_setRadioDCSChannelPool", strlen(argv[1])) == 0) {
developer326d4232023-06-15 16:45:30 +080017183 char pool[256] = {'\0'};
developer2a6abc92023-10-17 17:19:14 +080017184 if(argc <= 3)
17185 {
17186 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17187 exit(-1);
17188 }
developerc14d83a2023-06-29 20:09:42 +080017189 strncpy(pool, argv[3], strlen(argv[3]));
developer326d4232023-06-15 16:45:30 +080017190 wifi_setRadioDCSChannelPool(index, pool);
17191 wifi_debug(DEBUG_NOTICE, "Set DCSChannelPool: %s\n", pool);
17192 return 0;
17193 }
developer2a6abc92023-10-17 17:19:14 +080017194 if (strncmp(argv[1], "wifi_getRadioDCSChannelPool", strlen(argv[1])) == 0) {
developer326d4232023-06-15 16:45:30 +080017195 char pool[256] = {'\0'};
17196
17197 wifi_getRadioDCSChannelPool(index, pool);
17198 wifi_debug(DEBUG_NOTICE, "Get DCSChannelPool: %s\n", pool);
17199 return 0;
17200 }
developer5cd4c862023-05-26 09:34:42 +080017201 if (strncmp(argv[1], "wifi_getRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17202 BOOL out_status = 0;
17203 wifi_getRadioIGMPSnoopingEnable(index, &out_status);
17204 wifi_debug(DEBUG_NOTICE, "Ap get IGMP Snooping Enable: %d\n", out_status);
17205 return 0;
17206 }
developer121a8e72023-05-22 09:19:39 +080017207
developer95c045d2023-05-24 19:26:28 +080017208 if (strncmp(argv[1], "wifi_setApWmmEnable", strlen(argv[1])) == 0) {
17209 int enable = 0;
17210 if(argc <= 3)
17211 {
17212 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17213 exit(-1);
17214 }
17215 enable = atoi(argv[3]);
17216 wifi_setApWmmEnable(index,enable);
17217 return 0;
17218 }
developerc1aa6532023-06-09 09:37:01 +080017219 if (strncmp(argv[1], "wifi_pushSsidAdvertisementEnable", strlen(argv[1])) == 0) {
17220 int enable = 0;
17221 if(argc <= 3)
17222 {
17223 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17224 exit(-1);
17225 }
17226 enable = atoi(argv[3]);
17227 wifi_pushSsidAdvertisementEnable(index,enable);
17228 return 0;
17229 }
developer56fbedb2023-05-30 16:47:05 +080017230 if (strncmp(argv[1], "wifi_down", strlen(argv[1])) == 0) {
17231 wifi_down();
17232 return 0;
17233 }
developer95c045d2023-05-24 19:26:28 +080017234
developer56fbedb2023-05-30 16:47:05 +080017235 if (strncmp(argv[1], "wifi_getRadioStatus", strlen(argv[1])) == 0) {
17236 BOOL enable = 0;
17237
17238 wifi_getRadioStatus(index, &enable);
17239 wifi_debug(DEBUG_NOTICE, "wifi_getRadioStatus enable: %d\n", (int)enable);
17240 return 0;
17241 }
developer333c1eb2023-05-31 14:59:39 +080017242
developer95c045d2023-05-24 19:26:28 +080017243 if (strncmp(argv[1], "wifi_getApWMMCapability", strlen(argv[1])) == 0) {
17244 BOOL enable = 0;
17245
17246 wifi_getApWMMCapability(index, &enable);
17247 wifi_debug(DEBUG_NOTICE, "wifi_getApWMMCapability enable: %d\n", (int)enable);
17248 return 0;
17249 }
17250
17251 if (strncmp(argv[1], "wifi_getApWmmEnable", strlen(argv[1])) == 0) {
17252 BOOL enable = 0;
17253
17254 wifi_getApWmmEnable(index, &enable);
17255 wifi_debug(DEBUG_NOTICE, "wifi_getApWmmEnable enable: %d\n", (int)enable);
17256 return 0;
17257 }
17258
developer2edaf012023-05-24 14:24:53 +080017259 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17260 int filter_mode = 0;
17261 wifi_getApMacAddressControlMode(index, &filter_mode);
17262 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17263 return 0;
17264 }
developer0f10c772023-05-16 21:43:39 +080017265 if(strstr(argv[1], "wifi_getRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080017266 {
developer863a4a62023-06-06 16:55:59 +080017267 UINT mode = 0;
developer0f10c772023-05-16 21:43:39 +080017268
developera3511852023-06-14 14:12:59 +080017269 wifi_getRadioMode(index, buf, &mode);
17270 printf("Ap Radio mode is %s , mode = 0x%x\n", buf, mode);
17271 return 0;
17272 }
17273 if(strstr(argv[1], "wifi_getRadioAutoChannelEnable")!=NULL)
17274 {
17275 BOOL b = FALSE;
17276 BOOL *output_bool = &b;
17277 wifi_getRadioAutoChannelEnable(index,output_bool);
17278 printf("Channel enabled = %d \n",b);
17279 return 0;
17280 }
17281 if(strstr(argv[1], "wifi_getApWpaEncryptionMode")!=NULL)
17282 {
17283 wifi_getApWpaEncryptionMode(index,buf);
17284 printf("encryption enabled = %s\n",buf);
17285 return 0;
17286 }
17287 if(strstr(argv[1], "wifi_getApSsidAdvertisementEnable")!=NULL)
17288 {
17289 BOOL b = FALSE;
17290 BOOL *output_bool = &b;
17291 wifi_getApSsidAdvertisementEnable(index,output_bool);
17292 printf("advertisment enabled = %d\n",b);
17293 return 0;
17294 }
17295 if(strstr(argv[1],"wifi_getApAssociatedDeviceTidStatsResult")!=NULL)
17296 {
17297 if(argc <= 3 )
17298 {
17299 printf("Insufficient arguments \n");
17300 exit(-1);
17301 }
developer72fb0bb2023-01-11 09:46:29 +080017302
developera3511852023-06-14 14:12:59 +080017303 char sta[20] = {'\0'};
17304 ULLONG handle= 0;
developerc14d83a2023-06-29 20:09:42 +080017305 strncpy(sta,argv[3], strlen(argv[3]));
developera3511852023-06-14 14:12:59 +080017306 mac_address_t st;
developer72fb0bb2023-01-11 09:46:29 +080017307 mac_addr_aton(st,sta);
17308
developera3511852023-06-14 14:12:59 +080017309 wifi_associated_dev_tid_stats_t tid_stats;
17310 wifi_getApAssociatedDeviceTidStatsResult(index,&st,&tid_stats,&handle);
17311 for(int tid_index=0; tid_index<PS_MAX_TID; tid_index++) //print tid stats
17312 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);
17313 }
developer72fb0bb2023-01-11 09:46:29 +080017314
developera3511852023-06-14 14:12:59 +080017315 if(strstr(argv[1], "getApEnable")!=NULL) {
17316 BOOL enable;
17317 ret=wifi_getApEnable(index, &enable);
17318 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17319 }
17320 else if(strstr(argv[1], "setApEnable")!=NULL) {
developer2a6abc92023-10-17 17:19:14 +080017321 BOOL enable;
17322 if(argc <= 3)
17323 {
17324 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17325 exit(-1);
17326 }
17327 enable = atoi(argv[3]);
developera3511852023-06-14 14:12:59 +080017328 ret=wifi_setApEnable(index, enable);
17329 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17330 }
17331 else if(strstr(argv[1], "getApStatus")!=NULL) {
17332 char status[64];
17333 ret=wifi_getApStatus(index, status);
17334 printf("%s %d: %s, returns %d\n", argv[1], index, status, ret);
17335 }
17336 else if(strstr(argv[1], "wifi_getSSIDNameStatus")!=NULL)
17337 {
17338 wifi_getSSIDNameStatus(index,buf);
17339 printf("%s %d: active ssid : %s\n",argv[1], index,buf);
17340 return 0;
17341 } else if(strstr(argv[1], "wifi_resetApVlanCfg")!=NULL) {
17342 wifi_resetApVlanCfg(index);
17343 printf("%s %d: wifi_resetApVlanCfg : %s\n",argv[1], index,buf);
17344 return 0;
17345 }
17346 else if(strstr(argv[1], "getSSIDTrafficStats2")!=NULL) {
17347 wifi_ssidTrafficStats2_t stats={0};
17348 ret=wifi_getSSIDTrafficStats2(index, &stats); //Tr181
17349 printf("%s %d: returns %d\n", argv[1], index, ret);
17350 printf(" ssid_BytesSent =%lu\n", stats.ssid_BytesSent);
17351 printf(" ssid_BytesReceived =%lu\n", stats.ssid_BytesReceived);
17352 printf(" ssid_PacketsSent =%lu\n", stats.ssid_PacketsSent);
17353 printf(" ssid_PacketsReceived =%lu\n", stats.ssid_PacketsReceived);
17354 printf(" ssid_RetransCount =%lu\n", stats.ssid_RetransCount);
17355 printf(" ssid_FailedRetransCount =%lu\n", stats.ssid_FailedRetransCount);
17356 printf(" ssid_RetryCount =%lu\n", stats.ssid_RetryCount);
17357 printf(" ssid_MultipleRetryCount =%lu\n", stats.ssid_MultipleRetryCount);
17358 printf(" ssid_ACKFailureCount =%lu\n", stats.ssid_ACKFailureCount);
17359 printf(" ssid_AggregatedPacketCount =%lu\n", stats.ssid_AggregatedPacketCount);
17360 printf(" ssid_ErrorsSent =%lu\n", stats.ssid_ErrorsSent);
17361 printf(" ssid_ErrorsReceived =%lu\n", stats.ssid_ErrorsReceived);
17362 printf(" ssid_UnicastPacketsSent =%lu\n", stats.ssid_UnicastPacketsSent);
17363 printf(" ssid_UnicastPacketsReceived =%lu\n", stats.ssid_UnicastPacketsReceived);
17364 printf(" ssid_DiscardedPacketsSent =%lu\n", stats.ssid_DiscardedPacketsSent);
17365 printf(" ssid_DiscardedPacketsReceived =%lu\n", stats.ssid_DiscardedPacketsReceived);
17366 printf(" ssid_MulticastPacketsSent =%lu\n", stats.ssid_MulticastPacketsSent);
17367 printf(" ssid_MulticastPacketsReceived =%lu\n", stats.ssid_MulticastPacketsReceived);
17368 printf(" ssid_BroadcastPacketsSent =%lu\n", stats.ssid_BroadcastPacketsSent);
17369 printf(" ssid_BroadcastPacketsRecevied =%lu\n", stats.ssid_BroadcastPacketsRecevied);
17370 printf(" ssid_UnknownPacketsReceived =%lu\n", stats.ssid_UnknownPacketsReceived);
17371 }
17372 else if(strstr(argv[1], "getNeighboringWiFiDiagnosticResult2")!=NULL) {
17373 wifi_neighbor_ap2_t *neighbor_ap_array=NULL, *pt=NULL;
17374 UINT array_size=0;
17375 UINT i=0;
17376 ret=wifi_getNeighboringWiFiDiagnosticResult2(index, &neighbor_ap_array, &array_size);
17377 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17378 for(i=0, pt=neighbor_ap_array; i<array_size; i++, pt++) {
17379 printf(" neighbor %d:\n", i);
17380 printf(" ap_SSID =%s\n", pt->ap_SSID);
17381 printf(" ap_BSSID =%s\n", pt->ap_BSSID);
17382 printf(" ap_Mode =%s\n", pt->ap_Mode);
17383 printf(" ap_Channel =%d\n", pt->ap_Channel);
17384 printf(" ap_SignalStrength =%d\n", pt->ap_SignalStrength);
17385 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17386 printf(" ap_EncryptionMode =%s\n", pt->ap_EncryptionMode);
17387 printf(" ap_SupportedStandards =%s\n", pt->ap_SupportedStandards);
17388 printf(" ap_OperatingStandards =%s\n", pt->ap_OperatingStandards);
17389 printf(" ap_OperatingChannelBandwidth =%s\n", pt->ap_OperatingChannelBandwidth);
17390 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17391 printf(" ap_BeaconPeriod =%d\n", pt->ap_BeaconPeriod);
17392 printf(" ap_Noise =%d\n", pt->ap_Noise);
17393 printf(" ap_BasicDataTransferRates =%s\n", pt->ap_BasicDataTransferRates);
17394 printf(" ap_SupportedDataTransferRates =%s\n", pt->ap_SupportedDataTransferRates);
17395 printf(" ap_DTIMPeriod =%d\n", pt->ap_DTIMPeriod);
17396 printf(" ap_ChannelUtilization =%d\n", pt->ap_ChannelUtilization);
17397 }
17398 if(neighbor_ap_array)
17399 free(neighbor_ap_array); //make sure to free the list
17400 }
17401 else if(strstr(argv[1], "getApAssociatedDeviceDiagnosticResult")!=NULL) {
17402 wifi_associated_dev_t *associated_dev_array=NULL, *pt=NULL;
17403 UINT array_size=0;
17404 UINT i=0;
17405 ret=wifi_getApAssociatedDeviceDiagnosticResult(index, &associated_dev_array, &array_size);
17406 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17407 for(i=0, pt=associated_dev_array; i<array_size; i++, pt++) {
17408 printf(" associated_dev %d:\n", i);
17409 printf(" cli_OperatingStandard =%s\n", pt->cli_OperatingStandard);
17410 printf(" cli_OperatingChannelBandwidth =%s\n", pt->cli_OperatingChannelBandwidth);
17411 printf(" cli_SNR =%d\n", pt->cli_SNR);
17412 printf(" cli_InterferenceSources =%s\n", pt->cli_InterferenceSources);
17413 printf(" cli_DataFramesSentAck =%lu\n", pt->cli_DataFramesSentAck);
17414 printf(" cli_DataFramesSentNoAck =%lu\n", pt->cli_DataFramesSentNoAck);
17415 printf(" cli_BytesSent =%lu\n", pt->cli_BytesSent);
17416 printf(" cli_BytesReceived =%lu\n", pt->cli_BytesReceived);
17417 printf(" cli_RSSI =%d\n", pt->cli_RSSI);
17418 printf(" cli_MinRSSI =%d\n", pt->cli_MinRSSI);
17419 printf(" cli_MaxRSSI =%d\n", pt->cli_MaxRSSI);
17420 printf(" cli_Disassociations =%d\n", pt->cli_Disassociations);
17421 printf(" cli_AuthenticationFailures =%d\n", pt->cli_AuthenticationFailures);
17422 }
17423 if(associated_dev_array)
17424 free(associated_dev_array); //make sure to free the list
17425 }
developer72fb0bb2023-01-11 09:46:29 +080017426
developera3511852023-06-14 14:12:59 +080017427 if(strstr(argv[1],"wifi_getRadioChannelStats")!=NULL)
17428 {
developer72fb0bb2023-01-11 09:46:29 +080017429#define MAX_ARRAY_SIZE 64
developera3511852023-06-14 14:12:59 +080017430 int i, array_size;
17431 char *p, *ch_str;
17432 wifi_channelStats_t input_output_channelStats_array[MAX_ARRAY_SIZE];
developer72fb0bb2023-01-11 09:46:29 +080017433
developera3511852023-06-14 14:12:59 +080017434 if(argc != 5)
17435 {
17436 printf("Insufficient arguments, Usage: wifihal wifi_getRadioChannelStats <AP-Index> <Array-Size> <Comma-seperated-channel-numbers>\n");
17437 exit(-1);
17438 }
17439 memset(input_output_channelStats_array, 0, sizeof(input_output_channelStats_array));
developer72fb0bb2023-01-11 09:46:29 +080017440
developera3511852023-06-14 14:12:59 +080017441 for (i=0, array_size=atoi(argv[3]), ch_str=argv[4]; i<array_size; i++, ch_str=p)
17442 {
17443 strtok_r(ch_str, ",", &p);
17444 input_output_channelStats_array[i].ch_number = atoi(ch_str);
17445 }
17446 wifi_getRadioChannelStats(atoi(argv[2]), input_output_channelStats_array, array_size);
17447 if(!array_size)
17448 array_size=1;//Need to print current channel statistics
17449 for(i=0; i<array_size; i++)
17450 printf("chan num = %d \t, noise =%d\t ch_utilization_busy_rx = %lld \t,\
17451 ch_utilization_busy_tx = %lld \t,ch_utilization_busy = %lld \t,\
17452 ch_utilization_busy_ext = %lld \t, ch_utilization_total = %lld \t \n",\
17453 input_output_channelStats_array[i].ch_number,\
17454 input_output_channelStats_array[i].ch_noise,\
17455 input_output_channelStats_array[i].ch_utilization_busy_rx,\
17456 input_output_channelStats_array[i].ch_utilization_busy_tx,\
17457 input_output_channelStats_array[i].ch_utilization_busy,\
17458 input_output_channelStats_array[i].ch_utilization_busy_ext,\
17459 input_output_channelStats_array[i].ch_utilization_total);
17460 }
developer72fb0bb2023-01-11 09:46:29 +080017461
developera3511852023-06-14 14:12:59 +080017462 if(strstr(argv[1],"wifi_getAssociatedDeviceDetail")!=NULL)
17463 {
17464 if(argc <= 3 )
17465 {
17466 printf("Insufficient arguments \n");
17467 exit(-1);
17468 }
17469 char mac_addr[20] = {'\0'};
17470 wifi_device_t output_struct;
17471 int dev_index = atoi(argv[3]);
developer72fb0bb2023-01-11 09:46:29 +080017472
developera3511852023-06-14 14:12:59 +080017473 wifi_getAssociatedDeviceDetail(index,dev_index,&output_struct);
17474 mac_addr_ntoa(mac_addr,output_struct.wifi_devMacAddress);
17475 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);
17476 }
developer72fb0bb2023-01-11 09:46:29 +080017477
developera3511852023-06-14 14:12:59 +080017478 if(strstr(argv[1],"wifi_setNeighborReports")!=NULL)
17479 {
17480 if (argc <= 3)
17481 {
17482 printf("Insufficient arguments\n");
17483 exit(-1);
17484 }
17485 char args[256];
17486 wifi_NeighborReport_t *neighborReports;
developer72fb0bb2023-01-11 09:46:29 +080017487
developera3511852023-06-14 14:12:59 +080017488 neighborReports = calloc(argc - 2, sizeof(neighborReports));
17489 if (!neighborReports)
17490 {
17491 printf("Failed to allocate memory");
17492 exit(-1);
17493 }
developer72fb0bb2023-01-11 09:46:29 +080017494
developera3511852023-06-14 14:12:59 +080017495 for (int i = 3; i < argc; ++i)
17496 {
17497 char *val;
17498 int j = 0;
developerc14d83a2023-06-29 20:09:42 +080017499 unsigned long tmp;
developera3511852023-06-14 14:12:59 +080017500 memset(args, 0, sizeof(args));
17501 strncpy(args, argv[i], sizeof(args));
17502 val = strtok(args, ";");
17503 while (val != NULL)
17504 {
17505 if (j == 0)
17506 {
17507 mac_addr_aton(neighborReports[i - 3].bssid, val);
17508 } else if (j == 1)
17509 {
developerc14d83a2023-06-29 20:09:42 +080017510 if (hal_strtoul(val, 16, &tmp) < 0) {
17511 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017512 }
17513 neighborReports[i - 3].info = tmp;
developera3511852023-06-14 14:12:59 +080017514 } else if (j == 2)
17515 {
developerc14d83a2023-06-29 20:09:42 +080017516 if (hal_strtoul(val, 16, &tmp) < 0) {
17517 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017518 }
17519 neighborReports[i - 3].opClass = tmp;
developer5b23cd02023-07-19 20:26:03 +080017520
developera3511852023-06-14 14:12:59 +080017521 } else if (j == 3)
17522 {
developerc14d83a2023-06-29 20:09:42 +080017523 if (hal_strtoul(val, 16, &tmp) < 0) {
17524 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017525 }
17526 neighborReports[i - 3].channel = tmp;
developera3511852023-06-14 14:12:59 +080017527 } else if (j == 4)
17528 {
developerc14d83a2023-06-29 20:09:42 +080017529 if (hal_strtoul(val, 16, &tmp) < 0) {
17530 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017531 }
17532 neighborReports[i - 3].phyTable = tmp;
developera3511852023-06-14 14:12:59 +080017533 } else {
17534 printf("Insufficient arguments]n\n");
17535 exit(-1);
17536 }
17537 val = strtok(NULL, ";");
17538 j++;
17539 }
17540 }
developer72fb0bb2023-01-11 09:46:29 +080017541
developera3511852023-06-14 14:12:59 +080017542 INT ret = wifi_setNeighborReports(index, argc - 3, neighborReports);
17543 if (ret != RETURN_OK)
17544 {
17545 printf("wifi_setNeighborReports ret = %d", ret);
17546 exit(-1);
17547 }
17548 }
17549 if(strstr(argv[1],"wifi_getRadioIfName")!=NULL)
17550 {
17551 if((ret=wifi_getRadioIfName(index, buf))==RETURN_OK)
17552 printf("%s.\n", buf);
17553 else
17554 printf("Error returned\n");
17555 }
17556 if(strstr(argv[1],"wifi_getApSecurityModesSupported")!=NULL)
17557 {
17558 if((ret=wifi_getApSecurityModesSupported(index, buf))==RETURN_OK)
17559 printf("%s.\n", buf);
17560 else
17561 printf("Error returned\n");
17562 }
17563 if(strstr(argv[1],"wifi_getRadioOperatingChannelBandwidth")!=NULL)
17564 {
17565 if (argc <= 2)
17566 {
17567 printf("Insufficient arguments\n");
17568 exit(-1);
17569 }
17570 char buf[64]= {'\0'};
17571 wifi_getRadioOperatingChannelBandwidth(index,buf);
17572 printf("Current bandwidth is %s \n",buf);
17573 return 0;
17574 }
17575 if(strstr(argv[1],"pushRadioChannel2")!=NULL)
17576 {
17577 if (argc <= 5)
17578 {
17579 printf("Insufficient arguments\n");
17580 exit(-1);
17581 }
17582 UINT channel = atoi(argv[3]);
17583 UINT width = atoi(argv[4]);
17584 UINT beacon = atoi(argv[5]);
17585 INT ret = wifi_pushRadioChannel2(index,channel,width,beacon);
17586 printf("Result = %d", ret);
17587 }
developercc5cbfb2023-06-13 18:29:52 +080017588 if(strstr(argv[1],"wifi_getApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017589 {
developercc5cbfb2023-06-13 18:29:52 +080017590 char br_name[64], ip[64], subset[64] = {0};
17591 wifi_getApBridgeInfo(0, br_name, ip, subset);
17592 printf("wifi_getApBridgeInfo br_name = %s, ip = %s, subset = %s\n", br_name, ip, subset);
developera3511852023-06-14 14:12:59 +080017593 }
developercc5cbfb2023-06-13 18:29:52 +080017594 if(strstr(argv[1],"wifi_enableGreylistAccessControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017595 {
developer2a6abc92023-10-17 17:19:14 +080017596 int enable;
17597 if(argc <= 3)
17598 {
17599 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17600 exit(-1);
17601 }
17602 enable = atoi(argv[3]);
developercc5cbfb2023-06-13 18:29:52 +080017603 wifi_enableGreylistAccessControl(enable == 0 ? FALSE : TRUE);
17604 printf("wifi_enableGreylistAccessControl enable=%d\n", enable);
developera3511852023-06-14 14:12:59 +080017605 }
developercc5cbfb2023-06-13 18:29:52 +080017606 if(strstr(argv[1],"wifi_setApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017607 {
developer2a6abc92023-10-17 17:19:14 +080017608 if(argc <= 5)
17609 {
17610 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17611 exit(-1);
17612 }
developercc5cbfb2023-06-13 18:29:52 +080017613 wifi_setApBridgeInfo(0, argv[3], argv[4], argv[5]);
17614 printf("wifi_setApBridgeInfo br_name = %s, ip = %s, subset = %s\n", argv[3], argv[4], argv[5]);
developera3511852023-06-14 14:12:59 +080017615 }
developer72fb0bb2023-01-11 09:46:29 +080017616
developer6e578302023-06-21 10:11:16 +080017617 if(strstr(argv[1], "wifi_getATMCapable")!=NULL)
17618 {
17619 BOOL b = FALSE;
17620 BOOL *output_bool = &b;
17621 wifi_getATMCapable(output_bool);
17622 printf("ATM capable = %d \n",b);
17623 return 0;
17624 }
17625 if (strncmp(argv[1], "wifi_setATMEnable", strlen(argv[1])) == 0) {
17626 int enable = 0;
17627 if(argc <= 3)
17628 {
17629 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17630 exit(-1);
17631 }
17632 enable = atoi(argv[3]);
17633 wifi_setATMEnable(enable);
17634 return 0;
17635 }
17636 if (strncmp(argv[1], "wifi_getATMEnable", strlen(argv[1])) == 0) {
17637 BOOL b = FALSE;
17638 BOOL *output_bool = &b;
17639 wifi_getATMEnable(output_bool);
17640 printf("ATM enable = %d \n", b);
17641 return 0;
17642 }
17643 if (strncmp(argv[1], "wifi_setApATMAirTimePercent", strlen(argv[1])) == 0) {
17644 unsigned int percent = 0;
17645 if(argc <= 3)
17646 {
17647 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17648 exit(-1);
17649 }
17650 percent = atoi(argv[3]);
17651 wifi_setApATMAirTimePercent(index, percent);
17652 return 0;
17653 }
17654 if (strncmp(argv[1], "wifi_getApATMAirTimePercent", strlen(argv[1])) == 0) {
17655 unsigned int percent = 0;
17656 unsigned int *output = &percent;
17657
17658 wifi_getApATMAirTimePercent(index, output);
17659 printf("ATM percent = %d \n", percent);
17660 return 0;
17661 }
developer82533be2023-06-28 17:21:01 +080017662 if (strstr(argv[1],"setGF")!=NULL)
17663 {
developer2a6abc92023-10-17 17:19:14 +080017664 BOOL enable;
17665 if(argc <= 3)
17666 {
17667 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17668 exit(-1);
17669 }
17670 enable = atoi(argv[3]);
developer82533be2023-06-28 17:21:01 +080017671 if((ret=wifi_setRadio11nGreenfieldEnable(index, enable))==RETURN_OK)
17672 printf("wifi_setRadio11nGreenfieldEnable success\n");
17673 else
17674 printf("wifi_setRadio11nGreenfieldEnable Error\n");
17675 }
17676 if (strstr(argv[1],"setVID")!=NULL)
17677 {
developer2a6abc92023-10-17 17:19:14 +080017678 INT vid;
17679 if(argc <= 3)
17680 {
17681 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17682 exit(-1);
17683 }
17684 vid = atoi(argv[3]);
developer82533be2023-06-28 17:21:01 +080017685 if((ret=wifi_setApVlanID(index, vid))==RETURN_OK)
17686 printf("wifi_setApVlanID success.\n");
17687 else
17688 printf("wifi_setApVlanID Error\n");
developerd14dff12023-06-28 22:47:44 +080017689 }
17690 if (strncmp(argv[1], "wifi_getApATMSta", strlen(argv[1])) == 0) {
17691 UCHAR outbuf[256]={0};
17692
17693 wifi_getApATMSta(index, outbuf, sizeof(outbuf));
17694 printf("sta air time percent is %s \n", outbuf);
17695 return 0;
17696 }
developera3511852023-06-14 14:12:59 +080017697 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17698 return 0;
developer72fb0bb2023-01-11 09:46:29 +080017699}
17700
17701#endif
17702
17703#ifdef WIFI_HAL_VERSION_3
17704
developer32f2a182023-06-27 19:50:41 +080017705INT BitMapToTransmitRates(UINT bitMap, char *BasicRate, unsigned long size)
developer72fb0bb2023-01-11 09:46:29 +080017706{
developera3511852023-06-14 14:12:59 +080017707 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer32f2a182023-06-27 19:50:41 +080017708 if (bitMap & WIFI_BITRATE_1MBPS) {
17709 if ((size - strlen(BasicRate)) <= 2)
17710 return RETURN_ERR;
17711 strncat(BasicRate, "1,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17712 }
17713 if (bitMap & WIFI_BITRATE_2MBPS) {
17714 if ((size - strlen(BasicRate)) <= 2)
17715 return RETURN_ERR;
17716 strncat(BasicRate, "2,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17717 }
17718 if (bitMap & WIFI_BITRATE_5_5MBPS) {
17719 if ((size - strlen(BasicRate)) <= 4)
17720 return RETURN_ERR;
17721 strncat(BasicRate, "5.5,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17722 }
17723 if (bitMap & WIFI_BITRATE_6MBPS) {
17724 if ((size - strlen(BasicRate)) <= 2)
17725 return RETURN_ERR;
17726 strncat(BasicRate, "6,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17727 }
17728 if (bitMap & WIFI_BITRATE_9MBPS) {
17729 if ((size - strlen(BasicRate)) <= 2)
17730 return RETURN_ERR;
17731 strncat(BasicRate, "9,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17732 }
17733 if (bitMap & WIFI_BITRATE_11MBPS) {
17734 if ((size - strlen(BasicRate)) <= 3)
17735 return RETURN_ERR;
17736 strncat(BasicRate, "11,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17737 }
17738 if (bitMap & WIFI_BITRATE_12MBPS) {
17739 if ((size - strlen(BasicRate)) <= 3)
17740 return RETURN_ERR;
17741 strncat(BasicRate, "12,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17742 }
17743 if (bitMap & WIFI_BITRATE_18MBPS) {
17744 if ((size - strlen(BasicRate)) <= 3)
17745 return RETURN_ERR;
17746 strncat(BasicRate, "18,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17747 }
17748 if (bitMap & WIFI_BITRATE_24MBPS) {
17749 if ((size - strlen(BasicRate)) <= 3)
17750 return RETURN_ERR;
17751 strncat(BasicRate, "24,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17752 }
17753 if (bitMap & WIFI_BITRATE_36MBPS) {
17754 if ((size - strlen(BasicRate)) <= 3)
17755 return RETURN_ERR;
17756 strncat(BasicRate, "36,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17757 }
17758 if (bitMap & WIFI_BITRATE_48MBPS) {
17759 if ((size - strlen(BasicRate)) <= 3)
17760 return RETURN_ERR;
17761 strncat(BasicRate, "48,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17762 }
17763 if (bitMap & WIFI_BITRATE_54MBPS) {
17764 if ((size - strlen(BasicRate)) <= 3)
17765 return RETURN_ERR;
17766 strncat(BasicRate, "54,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17767 }
developera3511852023-06-14 14:12:59 +080017768 if (strlen(BasicRate) != 0) // remove last comma
17769 BasicRate[strlen(BasicRate) - 1] = '\0';
17770 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17771 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017772}
17773
17774INT TransmitRatesToBitMap (char *BasicRatesList, UINT *basicRateBitMap)
17775{
developera3511852023-06-14 14:12:59 +080017776 UINT BitMap = 0;
17777 char *rate;
developer72fb0bb2023-01-11 09:46:29 +080017778
developera3511852023-06-14 14:12:59 +080017779 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
17780 rate = strtok(BasicRatesList, ",");
17781 while(rate != NULL)
17782 {
17783 if (strcmp(rate, "1") == 0)
17784 BitMap |= WIFI_BITRATE_1MBPS;
17785 else if (strcmp(rate, "2") == 0)
17786 BitMap |= WIFI_BITRATE_2MBPS;
17787 else if (strcmp(rate, "5.5") == 0)
17788 BitMap |= WIFI_BITRATE_5_5MBPS;
17789 else if (strcmp(rate, "6") == 0)
17790 BitMap |= WIFI_BITRATE_6MBPS;
17791 else if (strcmp(rate, "9") == 0)
17792 BitMap |= WIFI_BITRATE_9MBPS;
17793 else if (strcmp(rate, "11") == 0)
17794 BitMap |= WIFI_BITRATE_11MBPS;
17795 else if (strcmp(rate, "12") == 0)
17796 BitMap |= WIFI_BITRATE_12MBPS;
17797 else if (strcmp(rate, "18") == 0)
17798 BitMap |= WIFI_BITRATE_18MBPS;
17799 else if (strcmp(rate, "24") == 0)
17800 BitMap |= WIFI_BITRATE_24MBPS;
17801 else if (strcmp(rate, "36") == 0)
17802 BitMap |= WIFI_BITRATE_36MBPS;
17803 else if (strcmp(rate, "48") == 0)
17804 BitMap |= WIFI_BITRATE_48MBPS;
17805 else if (strcmp(rate, "54") == 0)
17806 BitMap |= WIFI_BITRATE_54MBPS;
17807 rate = strtok(NULL, ",");
17808 }
17809 *basicRateBitMap = BitMap;
17810 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17811 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017812}
17813
17814// This API is used to configured all radio operation parameter in a single set. it includes channel number, channelWidth, mode and auto chammel configuration.
17815INT wifi_setRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17816{
developera3511852023-06-14 14:12:59 +080017817 char buf[128] = {0};
17818 int bandwidth = 20;
17819 int set_mode = 0;
developer56fbedb2023-05-30 16:47:05 +080017820 BOOL drv_dat_change = 0, hapd_conf_change = 0;
developera3511852023-06-14 14:12:59 +080017821 wifi_radio_operationParam_t current_param;
developer82160f02023-08-19 15:30:44 +080017822 int ApIndex;
developer72fb0bb2023-01-11 09:46:29 +080017823
developera3511852023-06-14 14:12:59 +080017824 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017825
developera3511852023-06-14 14:12:59 +080017826 multiple_set = TRUE;
17827 if (wifi_getRadioOperatingParameters(index, &current_param) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017828 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingParameters return error.\n");
developera3511852023-06-14 14:12:59 +080017829 return RETURN_ERR;
17830 }
17831 if (current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17832 if (wifi_setRadioAutoChannelEnable(index, operationParam->autoChannelEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017833 wifi_debug(DEBUG_ERROR, "wifi_setRadioAutoChannelEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017834 return RETURN_ERR;
17835 }
17836 drv_dat_change = TRUE;
17837 }
17838 if (current_param.channelWidth != operationParam->channelWidth ||
17839 current_param.channel != operationParam->channel ||
17840 current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17841 if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_20MHZ)
17842 bandwidth = 20;
17843 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_40MHZ)
17844 bandwidth = 40;
17845 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80MHZ)
17846 bandwidth = 80;
17847 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_160MHZ || operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80_80MHZ)
17848 bandwidth = 160;
developer72fb0bb2023-01-11 09:46:29 +080017849
developera3511852023-06-14 14:12:59 +080017850 if (operationParam->autoChannelEnabled) {
17851 if (wifi_pushRadioChannel2(index, 0, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017852 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017853 return RETURN_ERR;
17854 }
17855 } else {
17856 if (wifi_pushRadioChannel2(index, operationParam->channel, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017857 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017858 return RETURN_ERR;
17859 }
17860 }
developer56fbedb2023-05-30 16:47:05 +080017861 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017862 }
17863 if (current_param.variant != operationParam->variant) {
17864 // Two different definition bit map, so need to check every bit.
17865 if (operationParam->variant & WIFI_80211_VARIANT_A)
17866 set_mode |= WIFI_MODE_A;
17867 if (operationParam->variant & WIFI_80211_VARIANT_B)
17868 set_mode |= WIFI_MODE_B;
17869 if (operationParam->variant & WIFI_80211_VARIANT_G)
17870 set_mode |= WIFI_MODE_G;
17871 if (operationParam->variant & WIFI_80211_VARIANT_N)
17872 set_mode |= WIFI_MODE_N;
17873 if (operationParam->variant & WIFI_80211_VARIANT_AC)
17874 set_mode |= WIFI_MODE_AC;
17875 if (operationParam->variant & WIFI_80211_VARIANT_AX)
17876 set_mode |= WIFI_MODE_AX;
17877 // Second parameter is to set channel band width, it is done by wifi_pushRadioChannel2 if changed.
17878 memset(buf, 0, sizeof(buf));
17879 drv_dat_change = TRUE;
17880 if (wifi_setRadioMode_by_dat(index, set_mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017881 wifi_debug(DEBUG_ERROR, "wifi_setRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080017882 return RETURN_ERR;
17883 }
17884 }
17885 if (current_param.dtimPeriod != operationParam->dtimPeriod) {
developer56fbedb2023-05-30 16:47:05 +080017886 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017887 if (wifi_setApDTIMInterval(index, operationParam->dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017888 wifi_debug(DEBUG_ERROR, "wifi_setApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017889 return RETURN_ERR;
17890 }
17891 }
17892 if (current_param.beaconInterval != operationParam->beaconInterval) {
developer56fbedb2023-05-30 16:47:05 +080017893 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017894 if (wifi_setRadioBeaconPeriod(index, operationParam->beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017895 wifi_debug(DEBUG_ERROR, "wifi_setRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080017896 return RETURN_ERR;
17897 }
17898 }
17899 if (current_param.operationalDataTransmitRates != operationParam->operationalDataTransmitRates) {
developer56fbedb2023-05-30 16:47:05 +080017900 hapd_conf_change = TRUE;
developer32f2a182023-06-27 19:50:41 +080017901 BitMapToTransmitRates(operationParam->operationalDataTransmitRates, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080017902 if (wifi_setRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017903 wifi_debug(DEBUG_ERROR, "wifi_setRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080017904 return RETURN_ERR;
17905 }
17906 }
17907 if (current_param.fragmentationThreshold != operationParam->fragmentationThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017908 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017909 if (wifi_setRadioFragmentationThreshold(index, operationParam->fragmentationThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017910 wifi_debug(DEBUG_ERROR, "wifi_setRadioFragmentationThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017911 return RETURN_ERR;
17912 }
17913 }
17914 if (current_param.guardInterval != operationParam->guardInterval) {
developer56fbedb2023-05-30 16:47:05 +080017915 hapd_conf_change = TRUE;
17916 drv_dat_change = TRUE;
17917 if (wifi_setGuardInterval(index, operationParam->guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017918 wifi_debug(DEBUG_ERROR, "wifi_setGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017919 return RETURN_ERR;
17920 }
17921 }
17922 if (current_param.transmitPower != operationParam->transmitPower) {
developer56fbedb2023-05-30 16:47:05 +080017923 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017924 if (wifi_setRadioTransmitPower(index, operationParam->transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017925 wifi_debug(DEBUG_ERROR, "wifi_setRadioTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080017926 return RETURN_ERR;
17927 }
17928 }
17929 if (current_param.rtsThreshold != operationParam->rtsThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017930 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017931 if (wifi_setApRtsThreshold(index, operationParam->rtsThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017932 wifi_debug(DEBUG_ERROR, "wifi_setApRtsThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017933 return RETURN_ERR;
17934 }
17935 }
17936 if (current_param.obssCoex != operationParam->obssCoex) {
developer56fbedb2023-05-30 16:47:05 +080017937 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017938 if (wifi_setRadioObssCoexistenceEnable(index, operationParam->obssCoex) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017939 wifi_debug(DEBUG_ERROR, "wifi_setRadioObssCoexistenceEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017940 return RETURN_ERR;
17941 }
17942 }
17943 if (current_param.stbcEnable != operationParam->stbcEnable) {
developer56fbedb2023-05-30 16:47:05 +080017944 hapd_conf_change = TRUE;
17945 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017946 if (wifi_setRadioSTBCEnable(index, operationParam->stbcEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017947 wifi_debug(DEBUG_ERROR, "wifi_setRadioSTBCEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017948 return RETURN_ERR;
17949 }
17950 }
17951 if (current_param.greenFieldEnable != operationParam->greenFieldEnable) {
17952 if (wifi_setRadio11nGreenfieldEnable(index, operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017953 wifi_debug(DEBUG_ERROR, "wifi_setRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017954 return RETURN_ERR;
17955 }
17956 }
developer72fb0bb2023-01-11 09:46:29 +080017957
developera3511852023-06-14 14:12:59 +080017958 /* only down/up interface when dat file has been changed,
17959 * if enable is true, then restart the radio.
17960 */
17961 if (drv_dat_change == TRUE) {
17962 wifi_setRadioEnable(index, FALSE);
developer56fbedb2023-05-30 16:47:05 +080017963 if (operationParam->enable == TRUE)
developera3511852023-06-14 14:12:59 +080017964 wifi_setRadioEnable(index, TRUE);
17965 } else if (hapd_conf_change == TRUE) {
developer82160f02023-08-19 15:30:44 +080017966 int ret, bss_num, i;
17967 char ret_buf[MAX_BUF_SIZE] = {0};
17968
17969 ret = wifi_BandProfileRead(0, index, "BssidNum", ret_buf, sizeof(ret_buf), "1");
17970 if (ret != 0) {
17971 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead BssidNum failed\n");
17972 return RETURN_ERR;
17973 }
17974 bss_num = atoi(ret_buf);
17975 if (bss_num <= 0) {
17976 wifi_debug(DEBUG_ERROR, "invalid BssidNum %s\n", ret_buf);
17977 return RETURN_ERR;
17978 }
developerbb9b20f2023-10-17 18:46:37 +080017979 if (bss_num > LOGAN_MAX_NUM_VAP_PER_RADIO) {
17980 wifi_debug(DEBUG_ERROR, "bss_num is larger than %d, use %d\n", LOGAN_MAX_NUM_VAP_PER_RADIO, LOGAN_MAX_NUM_VAP_PER_RADIO);
17981 bss_num = LOGAN_MAX_NUM_VAP_PER_RADIO;
17982 }
developer82160f02023-08-19 15:30:44 +080017983 wifi_debug(DEBUG_ERROR, "band %d BssidNum %d\n", index, bss_num);
17984
17985 for (i = 0; i < bss_num; i++) {
17986 ApIndex = array_index_to_vap_index(index, i);
17987 hostapd_raw_remove_bss(ApIndex);
17988 if (operationParam->enable == TRUE)
17989 hostapd_raw_add_bss(ApIndex);
17990 }
developera3511852023-06-14 14:12:59 +080017991 }
developer56fbedb2023-05-30 16:47:05 +080017992
developera3511852023-06-14 14:12:59 +080017993 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017994
developera3511852023-06-14 14:12:59 +080017995 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017996}
17997
17998INT wifi_getRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17999{
developera3511852023-06-14 14:12:59 +080018000 char band[64] = {0};
18001 char buf[256] = {0};
18002 char config_file[64] = {0};
developer8078acf2023-08-04 18:52:48 +080018003
developera3511852023-06-14 14:12:59 +080018004 UINT mode = 0;
18005 BOOL enabled = FALSE;
developer863a4a62023-06-06 16:55:59 +080018006 int dtimPeriod;
developer2f79c922023-06-02 17:33:42 +080018007 UINT beaconInterval;
18008 UINT basicDataTransmitRates;
18009 UINT operationalDataTransmitRates;
18010 wifi_guard_interval_t guardInterval;
18011 UINT transmitPower;
developere40952c2023-06-15 18:46:43 +080018012 int res;
developerc14d83a2023-06-29 20:09:42 +080018013 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080018014
developera3511852023-06-14 14:12:59 +080018015 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18016 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018017
developera3511852023-06-14 14:12:59 +080018018 memset(operationParam, 0, sizeof(wifi_radio_operationParam_t));
developere40952c2023-06-15 18:46:43 +080018019 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, index);
18020 if (os_snprintf_error(sizeof(config_file), res)) {
18021 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18022 return RETURN_ERR;
18023 }
developera3511852023-06-14 14:12:59 +080018024 if (wifi_getRadioEnable(index, &enabled) != RETURN_OK)
18025 {
developer75bd10c2023-06-27 11:34:08 +080018026 wifi_debug(DEBUG_ERROR, "wifi_getRadioEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018027 return RETURN_ERR;
18028 }
18029 operationParam->enable = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018030
developera3511852023-06-14 14:12:59 +080018031 memset(band, 0, sizeof(band));
18032 if (wifi_getRadioOperatingFrequencyBand(index, band) != RETURN_OK)
18033 {
developer75bd10c2023-06-27 11:34:08 +080018034 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingFrequencyBand return error.\n");
developera3511852023-06-14 14:12:59 +080018035 return RETURN_ERR;
18036 }
developer72fb0bb2023-01-11 09:46:29 +080018037
developera3511852023-06-14 14:12:59 +080018038 if (!strcmp(band, "2.4GHz"))
18039 operationParam->band = WIFI_FREQUENCY_2_4_BAND;
18040 else if (!strcmp(band, "5GHz"))
18041 operationParam->band = WIFI_FREQUENCY_5_BAND;
18042 else if (!strcmp(band, "6GHz"))
18043 operationParam->band = WIFI_FREQUENCY_6_BAND;
18044 else
18045 {
developer75bd10c2023-06-27 11:34:08 +080018046 wifi_debug(DEBUG_ERROR, "cannot decode band for radio index %d ('%s')\n", index, band);
developera3511852023-06-14 14:12:59 +080018047 }
developer72fb0bb2023-01-11 09:46:29 +080018048
developera3511852023-06-14 14:12:59 +080018049 wifi_hostapdRead(config_file, "channel", buf, sizeof(buf));
18050 if (strcmp(buf, "0") == 0 || strcmp(buf, "acs_survey") == 0) {
18051 operationParam->channel = 0;
18052 operationParam->autoChannelEnabled = TRUE;
18053 } else {
developerc14d83a2023-06-29 20:09:42 +080018054 if (hal_strtoul(buf, 10, &tmp) < 0) {
18055 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080018056 }
18057 operationParam->channel = tmp;
developer5b23cd02023-07-19 20:26:03 +080018058
developera3511852023-06-14 14:12:59 +080018059 operationParam->autoChannelEnabled = FALSE;
18060 }
developer72fb0bb2023-01-11 09:46:29 +080018061
developera3511852023-06-14 14:12:59 +080018062 memset(buf, 0, sizeof(buf));
18063 if (wifi_getRadioOperatingChannelBandwidth(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018064 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error.\n");
developera3511852023-06-14 14:12:59 +080018065 return RETURN_ERR;
18066 }
18067 if (!strcmp(buf, "20MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
18068 else if (!strcmp(buf, "40MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
18069 else if (!strcmp(buf, "80MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
18070 else if (!strcmp(buf, "160MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
18071 else
18072 {
developer75bd10c2023-06-27 11:34:08 +080018073 wifi_debug(DEBUG_ERROR, "Unknown channel bandwidth: %s\n", buf);
developera3511852023-06-14 14:12:59 +080018074 return false;
18075 }
developer72fb0bb2023-01-11 09:46:29 +080018076
developera3511852023-06-14 14:12:59 +080018077 if (wifi_getRadioMode(index, buf, &mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018078 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080018079 return RETURN_ERR;
18080 }
18081 // Two different definition bit map, so need to check every bit.
18082 if (mode & WIFI_MODE_A)
18083 operationParam->variant |= WIFI_80211_VARIANT_A;
18084 if (mode & WIFI_MODE_B)
18085 operationParam->variant |= WIFI_80211_VARIANT_B;
18086 if (mode & WIFI_MODE_G)
18087 operationParam->variant |= WIFI_80211_VARIANT_G;
18088 if (mode & WIFI_MODE_N)
18089 operationParam->variant |= WIFI_80211_VARIANT_N;
18090 if (mode & WIFI_MODE_AC)
18091 operationParam->variant |= WIFI_80211_VARIANT_AC;
18092 if (mode & WIFI_MODE_AX)
18093 operationParam->variant |= WIFI_80211_VARIANT_AX;
18094 if (wifi_getRadioDCSEnable(index, &operationParam->DCSEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018095 wifi_debug(DEBUG_ERROR, "wifi_getRadioDCSEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018096 return RETURN_ERR;
18097 }
18098 if (wifi_getApDTIMInterval(index, &dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018099 wifi_debug(DEBUG_ERROR, "wifi_getApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018100 return RETURN_ERR;
18101 }
developer2f79c922023-06-02 17:33:42 +080018102 operationParam->dtimPeriod = dtimPeriod;
developera3511852023-06-14 14:12:59 +080018103 if (wifi_getRadioBeaconPeriod(index, &beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018104 wifi_debug(DEBUG_ERROR, "wifi_getRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080018105 return RETURN_ERR;
18106 }
developer2f79c922023-06-02 17:33:42 +080018107 operationParam->beaconInterval = beaconInterval;
developer72fb0bb2023-01-11 09:46:29 +080018108
developera3511852023-06-14 14:12:59 +080018109 memset(buf, 0, sizeof(buf));
18110 if (wifi_getRadioSupportedDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018111 wifi_debug(DEBUG_ERROR, "wifi_getRadioSupportedDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018112 return RETURN_ERR;
18113 }
18114 TransmitRatesToBitMap(buf, &basicDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018115 operationParam->basicDataTransmitRates = basicDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018116
developera3511852023-06-14 14:12:59 +080018117 memset(buf, 0, sizeof(buf));
18118 if (wifi_getRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018119 wifi_debug(DEBUG_ERROR, "wifi_getRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018120 return RETURN_ERR;
18121 }
18122 TransmitRatesToBitMap(buf, &operationalDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018123 operationParam->operationalDataTransmitRates = operationalDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018124
developera3511852023-06-14 14:12:59 +080018125 memset(buf, 0, sizeof(buf));
18126 wifi_hostapdRead(config_file, "fragm_threshold", buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080018127 if (hal_strtoul(buf, 10, &tmp) < 0) {
18128 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18129 }
18130 operationParam->fragmentationThreshold = tmp;
developer72fb0bb2023-01-11 09:46:29 +080018131
developera3511852023-06-14 14:12:59 +080018132 if (wifi_getGuardInterval(index, &guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018133 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018134 return RETURN_ERR;
18135 }
developer2f79c922023-06-02 17:33:42 +080018136 operationParam->guardInterval = guardInterval;
18137
developera3511852023-06-14 14:12:59 +080018138 if (wifi_getRadioPercentageTransmitPower(index, (ULONG *)&transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018139 wifi_debug(DEBUG_ERROR, "wifi_getRadioPercentageTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080018140 return RETURN_ERR;
18141 }
developer2f79c922023-06-02 17:33:42 +080018142 operationParam->transmitPower = transmitPower;
developer72fb0bb2023-01-11 09:46:29 +080018143
developera3511852023-06-14 14:12:59 +080018144 memset(buf, 0, sizeof(buf));
18145 wifi_hostapdRead(config_file, "rts_threshold", buf, sizeof(buf));
18146 if (strcmp(buf, "-1") == 0) {
18147 operationParam->rtsThreshold = (UINT)-1; // maxuimum unsigned integer value
18148 operationParam->ctsProtection = FALSE;
18149 } else {
developerc14d83a2023-06-29 20:09:42 +080018150 if (hal_strtoul(buf, 10, &tmp) < 0) {
18151 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18152 }
18153 operationParam->rtsThreshold = tmp;
developera3511852023-06-14 14:12:59 +080018154 operationParam->ctsProtection = TRUE;
18155 }
developer72fb0bb2023-01-11 09:46:29 +080018156
developera3511852023-06-14 14:12:59 +080018157 memset(buf, 0, sizeof(buf));
18158 wifi_hostapdRead(config_file, "ht_coex", buf, sizeof(buf));
18159 if (strcmp(buf, "0") == 0)
18160 operationParam->obssCoex = FALSE;
18161 else
18162 operationParam->obssCoex = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018163
developer8078acf2023-08-04 18:52:48 +080018164 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep STBC", config_file);
18165 if (res) {
18166 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18167
developere40952c2023-06-15 18:46:43 +080018168 }
developer8078acf2023-08-04 18:52:48 +080018169
developera3511852023-06-14 14:12:59 +080018170 if (strlen(buf) != 0)
18171 operationParam->stbcEnable = TRUE;
18172 else
18173 operationParam->stbcEnable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018174
developera3511852023-06-14 14:12:59 +080018175 if (wifi_getRadio11nGreenfieldEnable(index, &operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018176 wifi_debug(DEBUG_ERROR, "wifi_getRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018177 return RETURN_ERR;
18178 }
developer72fb0bb2023-01-11 09:46:29 +080018179
developera3511852023-06-14 14:12:59 +080018180 // Below value is hardcoded
developer72fb0bb2023-01-11 09:46:29 +080018181
developera3511852023-06-14 14:12:59 +080018182 operationParam->numSecondaryChannels = 0;
18183 for (int i = 0; i < MAXNUMSECONDARYCHANNELS; i++) {
18184 operationParam->channelSecondary[i] = 0;
18185 }
18186 operationParam->csa_beacon_count = 15;
18187 operationParam->countryCode = wifi_countrycode_US; // hard to convert string to corresponding enum
developer72fb0bb2023-01-11 09:46:29 +080018188
developera3511852023-06-14 14:12:59 +080018189 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18190 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018191}
18192
18193static int array_index_to_vap_index(UINT radioIndex, int arrayIndex)
18194{
developera3511852023-06-14 14:12:59 +080018195 int max_radio_num = 0;
developer72fb0bb2023-01-11 09:46:29 +080018196
developera3511852023-06-14 14:12:59 +080018197 wifi_getMaxRadioNumber(&max_radio_num);
18198 if (radioIndex >= max_radio_num) {
developer75bd10c2023-06-27 11:34:08 +080018199 wifi_debug(DEBUG_ERROR, "Wrong radio index (%d)\n", radioIndex);
developera3511852023-06-14 14:12:59 +080018200 return RETURN_ERR;
18201 }
developer72fb0bb2023-01-11 09:46:29 +080018202
developera3511852023-06-14 14:12:59 +080018203 return (arrayIndex * max_radio_num) + radioIndex;
developer72fb0bb2023-01-11 09:46:29 +080018204}
18205
developer96b38512023-02-22 11:17:45 +080018206static int vap_index_to_array_index(int vapIndex, int *radioIndex, int *arrayIndex)
18207{
developera3511852023-06-14 14:12:59 +080018208 int max_radio_num = 0;
developer96b38512023-02-22 11:17:45 +080018209
developera3511852023-06-14 14:12:59 +080018210 if ((vapIndex < 0) || (vapIndex > MAX_NUM_VAP_PER_RADIO*MAX_NUM_RADIOS))
developer96b38512023-02-22 11:17:45 +080018211 return -1;
18212
developera3511852023-06-14 14:12:59 +080018213 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080018214 if(max_radio_num == 0){
18215 return RETURN_ERR;
18216 }
developera3511852023-06-14 14:12:59 +080018217 (*radioIndex) = vapIndex % max_radio_num;
18218 (*arrayIndex) = vapIndex / max_radio_num;
developer96b38512023-02-22 11:17:45 +080018219
developera3511852023-06-14 14:12:59 +080018220 return 0;
developer96b38512023-02-22 11:17:45 +080018221}
18222
18223
developer72fb0bb2023-01-11 09:46:29 +080018224wifi_bitrate_t beaconRate_string_to_enum(char *beaconRate) {
developera3511852023-06-14 14:12:59 +080018225 if (strncmp(beaconRate, "1Mbps", 5) == 0)
18226 return WIFI_BITRATE_1MBPS;
18227 else if (strncmp(beaconRate, "2Mbps", 5) == 0)
18228 return WIFI_BITRATE_2MBPS;
18229 else if (strncmp(beaconRate, "5.5Mbps", 7) == 0)
18230 return WIFI_BITRATE_5_5MBPS;
18231 else if (strncmp(beaconRate, "6Mbps", 5) == 0)
18232 return WIFI_BITRATE_6MBPS;
18233 else if (strncmp(beaconRate, "9Mbps", 5) == 0)
18234 return WIFI_BITRATE_9MBPS;
18235 else if (strncmp(beaconRate, "11Mbps", 6) == 0)
18236 return WIFI_BITRATE_11MBPS;
18237 else if (strncmp(beaconRate, "12Mbps", 6) == 0)
18238 return WIFI_BITRATE_12MBPS;
18239 else if (strncmp(beaconRate, "18Mbps", 6) == 0)
18240 return WIFI_BITRATE_18MBPS;
18241 else if (strncmp(beaconRate, "24Mbps", 6) == 0)
18242 return WIFI_BITRATE_24MBPS;
18243 else if (strncmp(beaconRate, "36Mbps", 6) == 0)
18244 return WIFI_BITRATE_36MBPS;
18245 else if (strncmp(beaconRate, "48Mbps", 6) == 0)
18246 return WIFI_BITRATE_48MBPS;
18247 else if (strncmp(beaconRate, "54Mbps", 6) == 0)
18248 return WIFI_BITRATE_54MBPS;
18249 return WIFI_BITRATE_DEFAULT;
developer72fb0bb2023-01-11 09:46:29 +080018250}
18251
developer32f2a182023-06-27 19:50:41 +080018252struct beacon_rate_2_string {
18253 wifi_bitrate_t beacon;
18254 char beacon_str[8];
18255};
18256
18257struct beacon_rate_2_string br2str[12] = {
18258 {WIFI_BITRATE_1MBPS, "1Mbps"},
18259 {WIFI_BITRATE_2MBPS, "2Mbps"},
18260 {WIFI_BITRATE_5_5MBPS, "5.5Mbps"},
18261 {WIFI_BITRATE_6MBPS, "6Mbps"},
18262 {WIFI_BITRATE_9MBPS, "9Mbps"},
18263 {WIFI_BITRATE_11MBPS, "11Mbps"},
18264 {WIFI_BITRATE_12MBPS, "12Mbps"},
18265 {WIFI_BITRATE_18MBPS, "18Mbps"},
18266 {WIFI_BITRATE_24MBPS, "24Mbps"},
18267 {WIFI_BITRATE_36MBPS, "36Mbps"},
18268 {WIFI_BITRATE_48MBPS, "48Mbps"},
18269 {WIFI_BITRATE_54MBPS, "54Mbps"}
18270};
18271
18272INT beaconRate_enum_to_string(wifi_bitrate_t beacon, char *beacon_str, unsigned long str_size)
developer72fb0bb2023-01-11 09:46:29 +080018273{
developer32f2a182023-06-27 19:50:41 +080018274 int i;
18275 unsigned long len;
18276
18277 for (i = 0; i < (sizeof(br2str)/sizeof(br2str[0])); i++) {
18278 if (beacon == br2str[i].beacon) {
18279 len = strlen(br2str[i].beacon_str);
18280 if (len >= str_size)
18281 return RETURN_ERR;
18282 memcpy(beacon_str, br2str[i].beacon_str, len);
18283 beacon_str[len] = '\0';
18284 break;
18285 }
18286 }
developera3511852023-06-14 14:12:59 +080018287 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018288}
18289
18290INT wifi_getRadioVapInfoMap(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18291{
developera3511852023-06-14 14:12:59 +080018292 INT mode = 0;
18293 INT ret = -1;
18294 UINT output = 0;
18295 int i = 0;
18296 int vap_index = 0;
18297 BOOL enabled = FALSE;
18298 char buf[32] = {0};
18299 wifi_vap_security_t security = {0};
developerbb9b20f2023-10-17 18:46:37 +080018300 int res = RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018301
developera3511852023-06-14 14:12:59 +080018302 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18303 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018304
developerbb9b20f2023-10-17 18:46:37 +080018305 memset((void *)map, 0, sizeof(*map));
developera3511852023-06-14 14:12:59 +080018306 ret = wifi_BandProfileRead(0, index, "BssidNum", buf, sizeof(buf), "0");
18307 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +080018308 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead BssidNum failed\n");
developera3511852023-06-14 14:12:59 +080018309 return RETURN_ERR;
18310 }
developerfde01262023-05-22 15:15:24 +080018311
developera3511852023-06-14 14:12:59 +080018312 map->num_vaps = atoi(buf);
18313 if (map->num_vaps <= 0) {
developer75bd10c2023-06-27 11:34:08 +080018314 wifi_debug(DEBUG_ERROR, "invalid BssidNum %s\n", buf);
developera3511852023-06-14 14:12:59 +080018315 return RETURN_ERR;
18316 }
developerbb9b20f2023-10-17 18:46:37 +080018317 if (map->num_vaps > LOGAN_MAX_NUM_VAP_PER_RADIO) {
18318 wifi_debug(DEBUG_ERROR, "bss_num is larger than %d, use %d\n", LOGAN_MAX_NUM_VAP_PER_RADIO, LOGAN_MAX_NUM_VAP_PER_RADIO);
18319 map->num_vaps = LOGAN_MAX_NUM_VAP_PER_RADIO;
18320 }
developerfde01262023-05-22 15:15:24 +080018321
developera3511852023-06-14 14:12:59 +080018322 for (i = 0; i < map->num_vaps; i++)
18323 {
18324 map->vap_array[i].radio_index = index;
developer72fb0bb2023-01-11 09:46:29 +080018325
developera3511852023-06-14 14:12:59 +080018326 vap_index = array_index_to_vap_index(index, i);
developerbb9b20f2023-10-17 18:46:37 +080018327 if (vap_index < 0) {
18328 res = RETURN_ERR;
18329 continue;
18330 }
developer72fb0bb2023-01-11 09:46:29 +080018331
developer9ce44382023-06-28 11:09:37 +080018332 strncpy(map->vap_array[i].bridge_name, BRIDGE_NAME,sizeof(map->vap_array[i].bridge_name) - 1);
18333 map->vap_array[i].bridge_name[sizeof(map->vap_array[i].bridge_name) - 1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080018334
developera3511852023-06-14 14:12:59 +080018335 map->vap_array[i].vap_index = vap_index;
developer72fb0bb2023-01-11 09:46:29 +080018336
developera3511852023-06-14 14:12:59 +080018337 memset(buf, 0, sizeof(buf));
18338 ret = wifi_getApName(vap_index, buf);
18339 if (ret != RETURN_OK) {
18340 printf("%s: wifi_getApName return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018341 res = RETURN_ERR;
18342 continue;
developera3511852023-06-14 14:12:59 +080018343 }
developerbb9b20f2023-10-17 18:46:37 +080018344 ret = snprintf(map->vap_array[i].vap_name, sizeof(map->vap_array[i].vap_name), "%s", buf);
18345 if (os_snprintf_error(sizeof(map->vap_array[i].vap_name), ret)) {
developere40952c2023-06-15 18:46:43 +080018346 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerbb9b20f2023-10-17 18:46:37 +080018347 res = RETURN_ERR;
18348 continue;
developere40952c2023-06-15 18:46:43 +080018349 }
developer72fb0bb2023-01-11 09:46:29 +080018350
developera3511852023-06-14 14:12:59 +080018351 memset(buf, 0, sizeof(buf));
18352 ret = wifi_getSSIDName(vap_index, buf);
18353 if (ret != RETURN_OK) {
18354 printf("%s: wifi_getSSIDName return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018355 res = RETURN_ERR;
18356 continue;
developera3511852023-06-14 14:12:59 +080018357 }
developerbb9b20f2023-10-17 18:46:37 +080018358 ret = snprintf(map->vap_array[i].u.bss_info.ssid, sizeof(map->vap_array[i].u.bss_info.ssid), "%s", buf);
18359 if (os_snprintf_error(sizeof(map->vap_array[i].u.bss_info.ssid), ret)) {
developere40952c2023-06-15 18:46:43 +080018360 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerbb9b20f2023-10-17 18:46:37 +080018361 res = RETURN_ERR;
18362 continue;
developere40952c2023-06-15 18:46:43 +080018363 }
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__);
developerbb9b20f2023-10-17 18:46:37 +080018370 res = RETURN_ERR;
18371 continue;
developera3511852023-06-14 14:12:59 +080018372 }
18373 map->vap_array[i].u.bss_info.showSsid = enabled;
developer69b61b02023-03-07 17:17:44 +080018374
developera3511852023-06-14 14:12:59 +080018375 ret = wifi_getApIsolationEnable(vap_index, &enabled);
18376 if (ret != RETURN_OK) {
18377 printf("%s: wifi_getApIsolationEnable return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018378 res = RETURN_ERR;
18379 continue;
developera3511852023-06-14 14:12:59 +080018380 }
18381 map->vap_array[i].u.bss_info.isolation = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018382
developera3511852023-06-14 14:12:59 +080018383 ret = wifi_getApMaxAssociatedDevices(vap_index, &output);
18384 if (ret != RETURN_OK) {
18385 printf("%s: wifi_getApMaxAssociatedDevices return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018386 res = RETURN_ERR;
18387 continue;
developera3511852023-06-14 14:12:59 +080018388 }
18389 map->vap_array[i].u.bss_info.bssMaxSta = output;
developer72fb0bb2023-01-11 09:46:29 +080018390
developera3511852023-06-14 14:12:59 +080018391 ret = wifi_getBSSTransitionActivation(vap_index, &enabled);
18392 if (ret != RETURN_OK) {
18393 printf("%s: wifi_getBSSTransitionActivation return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018394 res = RETURN_ERR;
18395 continue;
developera3511852023-06-14 14:12:59 +080018396 }
18397 map->vap_array[i].u.bss_info.bssTransitionActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018398
developera3511852023-06-14 14:12:59 +080018399 ret = wifi_getNeighborReportActivation(vap_index, &enabled);
18400 if (ret != RETURN_OK) {
18401 printf("%s: wifi_getNeighborReportActivation return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018402 res = RETURN_ERR;
18403 continue;
developera3511852023-06-14 14:12:59 +080018404 }
18405 map->vap_array[i].u.bss_info.nbrReportActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018406
developera3511852023-06-14 14:12:59 +080018407 ret = wifi_getApSecurity(vap_index, &security);
18408 if (ret != RETURN_OK) {
18409 printf("%s: wifi_getApSecurity return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018410 res = RETURN_ERR;
18411 continue;
developera3511852023-06-14 14:12:59 +080018412 }
18413 map->vap_array[i].u.bss_info.security = security;
developer72fb0bb2023-01-11 09:46:29 +080018414
developera3511852023-06-14 14:12:59 +080018415 ret = wifi_getApMacAddressControlMode(vap_index, &mode);
18416 if (ret != RETURN_OK) {
18417 printf("%s: wifi_getApMacAddressControlMode return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018418 res = RETURN_ERR;
18419 continue;
developera3511852023-06-14 14:12:59 +080018420 }
18421 if (mode == 0)
18422 map->vap_array[i].u.bss_info.mac_filter_enable = FALSE;
18423 else
18424 map->vap_array[i].u.bss_info.mac_filter_enable = TRUE;
18425 if (mode == 1)
18426 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
18427 else if (mode == 2)
18428 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
developer72fb0bb2023-01-11 09:46:29 +080018429
developera3511852023-06-14 14:12:59 +080018430 ret = wifi_getApWmmEnable(vap_index, &enabled);
18431 if (ret != RETURN_OK) {
18432 printf("%s: wifi_getApWmmEnable return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018433 res = RETURN_ERR;
18434 continue;
developera3511852023-06-14 14:12:59 +080018435 }
18436 map->vap_array[i].u.bss_info.wmm_enabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018437
developera3511852023-06-14 14:12:59 +080018438 ret = wifi_getApUAPSDCapability(vap_index, &enabled);
18439 if (ret != RETURN_OK) {
18440 printf("%s: wifi_getApUAPSDCapability return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018441 res = RETURN_ERR;
18442 continue;
developera3511852023-06-14 14:12:59 +080018443 }
18444 map->vap_array[i].u.bss_info.UAPSDEnabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018445
developera3511852023-06-14 14:12:59 +080018446 memset(buf, 0, sizeof(buf));
18447 ret = wifi_getApBeaconRate(map->vap_array[i].radio_index, buf);
18448 if (ret != RETURN_OK) {
18449 printf("%s: wifi_getApBeaconRate return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018450 res = RETURN_ERR;
18451 continue;
developera3511852023-06-14 14:12:59 +080018452 }
18453 map->vap_array[i].u.bss_info.beaconRate = beaconRate_string_to_enum(buf);
developer72fb0bb2023-01-11 09:46:29 +080018454
developera3511852023-06-14 14:12:59 +080018455 memset(buf, 0, sizeof(buf));
18456 ret = wifi_getBaseBSSID(vap_index, buf);
18457 if (ret != RETURN_OK) {
18458 printf("%s: wifi_getBaseBSSID return error\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018459 res = RETURN_ERR;
18460 continue;
developera3511852023-06-14 14:12:59 +080018461 }
developer5b2f10c2023-05-25 17:02:21 +080018462 if (hwaddr_aton2(buf, map->vap_array[i].u.bss_info.bssid) < 0) {
18463 printf("%s: hwaddr_aton2 fail\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018464 res = RETURN_ERR;
18465 continue;
developer5b2f10c2023-05-25 17:02:21 +080018466 }
developer72fb0bb2023-01-11 09:46:29 +080018467
developera3511852023-06-14 14:12:59 +080018468 ret = wifi_getRadioIGMPSnoopingEnable(map->vap_array[i].radio_index, &enabled);
18469 if (ret != RETURN_OK) {
developerc14d83a2023-06-29 20:09:42 +080018470 wifi_debug(DEBUG_ERROR, "%s: wifi_getRadioIGMPSnoopingEnable\n", __func__);
developerbb9b20f2023-10-17 18:46:37 +080018471 res = RETURN_ERR;
18472 continue;
developera3511852023-06-14 14:12:59 +080018473 }
18474 map->vap_array[i].u.bss_info.mcast2ucast = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018475
developera3511852023-06-14 14:12:59 +080018476 // TODO: wps, noack
18477 }
18478 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developerbb9b20f2023-10-17 18:46:37 +080018479 return res;
developer72fb0bb2023-01-11 09:46:29 +080018480}
18481
developer47cc27a2023-05-17 23:09:58 +080018482void checkVapStatus(int apIndex, BOOL *enable)
developer72fb0bb2023-01-11 09:46:29 +080018483{
developera3511852023-06-14 14:12:59 +080018484 char if_name[16] = {0};
developera3511852023-06-14 14:12:59 +080018485 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080018486 int res;
developer72fb0bb2023-01-11 09:46:29 +080018487
developera3511852023-06-14 14:12:59 +080018488 *enable = FALSE;
18489 if (wifi_GetInterfaceName(apIndex, if_name) != RETURN_OK)
18490 return;
developer72fb0bb2023-01-11 09:46:29 +080018491
developer8078acf2023-08-04 18:52:48 +080018492 res = _syscmd_secure(buf, sizeof(buf), "cat %s | grep ^%s=1", VAP_STATUS_FILE, if_name);
18493 if (res) {
18494 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18495
developere40952c2023-06-15 18:46:43 +080018496 }
developer8078acf2023-08-04 18:52:48 +080018497
developera3511852023-06-14 14:12:59 +080018498 if (strlen(buf) > 0)
18499 *enable = TRUE;
18500 return;
developer72fb0bb2023-01-11 09:46:29 +080018501}
18502
developer56fbedb2023-05-30 16:47:05 +080018503int hostapd_manage_bss(INT apIndex, BOOL enable)
18504{
18505 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080018506 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer8078acf2023-08-04 18:52:48 +080018507
developer56fbedb2023-05-30 16:47:05 +080018508 char buf[MAX_BUF_SIZE] = {0};
18509 BOOL status = FALSE;
18510 int max_radio_num = 0;
18511 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080018512 int res;
developer56fbedb2023-05-30 16:47:05 +080018513
18514 wifi_getApEnable(apIndex, &status);
18515
18516 wifi_getMaxRadioNumber(&max_radio_num);
18517 if (enable == status)
18518 return RETURN_OK;
18519
18520 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
18521 return RETURN_ERR;
18522
18523 if (enable == TRUE) {
18524 int radioIndex = apIndex % max_radio_num;
18525 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080018526 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
18527 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18528 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18529 return RETURN_ERR;
18530 }
developer82160f02023-08-19 15:30:44 +080018531
18532 wifi_debug(DEBUG_ERROR, "raw ADD bss_config config_file=%s\n", config_file);
18533
developer8078acf2023-08-04 18:52:48 +080018534 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
18535 if (res) {
18536 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18537
developere40952c2023-06-15 18:46:43 +080018538 }
developer8078acf2023-08-04 18:52:48 +080018539
developer56fbedb2023-05-30 16:47:05 +080018540 } else {
developerc14d83a2023-06-29 20:09:42 +080018541 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developer82160f02023-08-19 15:30:44 +080018542
18543 wifi_debug(DEBUG_ERROR, "global raw REMOVE %s\n", interface_name);
developer8078acf2023-08-04 18:52:48 +080018544 res = _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i global raw REMOVE %s", interface_name);
18545 if (res) {
18546 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18547
developere40952c2023-06-15 18:46:43 +080018548 }
developer56fbedb2023-05-30 16:47:05 +080018549 }
developer8078acf2023-08-04 18:52:48 +080018550 res = _syscmd_secure(buf, sizeof(buf), "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer56fbedb2023-05-30 16:47:05 +080018551 interface_name, interface_name, enable, VAP_STATUS_FILE);
developer8078acf2023-08-04 18:52:48 +080018552 if (res) {
18553 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18554
developere40952c2023-06-15 18:46:43 +080018555 }
developer56fbedb2023-05-30 16:47:05 +080018556 //Wait for wifi up/down to apply
18557 return RETURN_OK;
18558}
18559
18560int hostapd_raw_add_bss(int apIndex)
18561{
18562 return hostapd_manage_bss(apIndex, TRUE);
18563}
18564
18565int hostapd_raw_remove_bss(int apIndex)
18566{
18567 return hostapd_manage_bss(apIndex, FALSE);
18568}
18569
18570int hostapd_raw_restart_bss(int apIndex)
developer333c1eb2023-05-31 14:59:39 +080018571{
developerdaf24792023-06-06 11:40:04 +080018572 int ret = 0;
18573
18574 ret = hostapd_raw_remove_bss(apIndex);
18575 if(ret != RETURN_OK)
18576 return RETURN_ERR;
18577
18578 ret = hostapd_raw_add_bss(apIndex);
18579 if(ret != RETURN_OK)
18580 return RETURN_ERR;
18581
18582 return RETURN_OK;
developer56fbedb2023-05-30 16:47:05 +080018583}
18584
developer72fb0bb2023-01-11 09:46:29 +080018585INT wifi_createVAP(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18586{
developera3511852023-06-14 14:12:59 +080018587 unsigned int i;
18588 wifi_vap_info_t *vap_info = NULL;
18589 int acl_mode;
18590 int ret = 0;
18591 char buf[256] = {0};
18592 char cmd[128] = {0};
18593 char config_file[64] = {0};
18594 char psk_file[64] = {0};
18595 BOOL enable = FALSE;
18596 int band_idx;
developere40952c2023-06-15 18:46:43 +080018597 int res;
developere740c2a2023-05-23 18:34:32 +080018598
developer72fb0bb2023-01-11 09:46:29 +080018599
developera3511852023-06-14 14:12:59 +080018600 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18601 printf("Entering %s index = %d\n", __func__, (int)index);
developer6bfd6462023-08-24 14:28:18 +080018602 for (i = 0; i < map->num_vaps; i++)
developera3511852023-06-14 14:12:59 +080018603 {
18604 multiple_set = TRUE;
18605 vap_info = &map->vap_array[i];
developer72fb0bb2023-01-11 09:46:29 +080018606
developera3511852023-06-14 14:12:59 +080018607 // Check vap status file to enable multiple ap if the system boot.
18608 checkVapStatus(vap_info->vap_index, &enable);
18609 if (vap_info->u.bss_info.enabled == FALSE && enable == FALSE)
18610 continue;
developer72fb0bb2023-01-11 09:46:29 +080018611
developer75bd10c2023-06-27 11:34:08 +080018612 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 +080018613
developera3511852023-06-14 14:12:59 +080018614 band_idx = radio_index_to_band(index);
developere40952c2023-06-15 18:46:43 +080018615 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18616 if (os_snprintf_error(sizeof(config_file), res)) {
18617 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18618 return RETURN_ERR;
18619 }
developer9ce44382023-06-28 11:09:37 +080018620 if(band_idx >= 0 && band_idx < sizeof(wifi_band_str)/sizeof(wifi_band_str[0])){
18621 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[band_idx], config_file);
18622 } else{
18623 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18624 return RETURN_ERR;
18625 }
developere40952c2023-06-15 18:46:43 +080018626 if (os_snprintf_error(sizeof(cmd), res)) {
18627 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18628 return RETURN_ERR;
18629 }
developer8078acf2023-08-04 18:52:48 +080018630
18631 res = _syscmd_secure(buf, sizeof(buf), "cp /etc/hostapd-%s.conf %s", wifi_band_str[band_idx], config_file);
18632 if (res) {
18633 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18634
18635 }
developer72fb0bb2023-01-11 09:46:29 +080018636
developera3511852023-06-14 14:12:59 +080018637 struct params params[3];
18638 params[0].name = "interface";
18639 params[0].value = vap_info->vap_name;
developere40952c2023-06-15 18:46:43 +080018640 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", vap_info->vap_index);
18641 if (os_snprintf_error(sizeof(psk_file), res)) {
18642 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18643 return RETURN_ERR;
18644 }
developera3511852023-06-14 14:12:59 +080018645 params[1].name = "wpa_psk_file";
18646 params[1].value = psk_file;
18647 params[2].name = "ssid";
18648 params[2].value = vap_info->u.bss_info.ssid;
developer72fb0bb2023-01-11 09:46:29 +080018649
developer37646972023-06-29 10:58:43 +080018650 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18651 if (os_snprintf_error(sizeof(config_file), res)) {
18652 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18653 return RETURN_ERR;
18654 }
developera3511852023-06-14 14:12:59 +080018655 wifi_hostapdWrite(config_file, params, 3);
developer72fb0bb2023-01-11 09:46:29 +080018656
developer8078acf2023-08-04 18:52:48 +080018657 res = _syscmd_secure(buf, sizeof(buf), "touch %s", psk_file);
18658 if (res) {
18659 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18660
developere40952c2023-06-15 18:46:43 +080018661 }
developer72fb0bb2023-01-11 09:46:29 +080018662
developera3511852023-06-14 14:12:59 +080018663 ret = wifi_setSSIDName(vap_info->vap_index, vap_info->u.bss_info.ssid);
18664 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018665 wifi_debug(DEBUG_ERROR,"wifi_setSSIDName 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_setApSsidAdvertisementEnable(vap_info->vap_index, vap_info->u.bss_info.showSsid);
18670 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018671 wifi_debug(DEBUG_ERROR, "wifi_setApSsidAdvertisementEnable 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_setApIsolationEnable(vap_info->vap_index, vap_info->u.bss_info.isolation);
18676 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018677 wifi_debug(DEBUG_ERROR, "wifi_setApIsolationEnable 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 ret = wifi_setApMaxAssociatedDevices(vap_info->vap_index, vap_info->u.bss_info.bssMaxSta);
18682 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018683 wifi_debug(DEBUG_ERROR, "wifi_setApMaxAssociatedDevices return error\n");
developera3511852023-06-14 14:12:59 +080018684 return RETURN_ERR;
18685 }
developer72fb0bb2023-01-11 09:46:29 +080018686
developera3511852023-06-14 14:12:59 +080018687 ret = wifi_setBSSTransitionActivation(vap_info->vap_index, vap_info->u.bss_info.bssTransitionActivated);
18688 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018689 wifi_debug(DEBUG_ERROR, "wifi_setBSSTransitionActivation return error\n");
developera3511852023-06-14 14:12:59 +080018690 return RETURN_ERR;
18691 }
developer72fb0bb2023-01-11 09:46:29 +080018692
developera3511852023-06-14 14:12:59 +080018693 ret = wifi_setNeighborReportActivation(vap_info->vap_index, vap_info->u.bss_info.nbrReportActivated);
18694 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018695 wifi_debug(DEBUG_ERROR, "wifi_setNeighborReportActivation return error\n");
developera3511852023-06-14 14:12:59 +080018696 return RETURN_ERR;
18697 }
developer72fb0bb2023-01-11 09:46:29 +080018698
developera3511852023-06-14 14:12:59 +080018699 if (vap_info->u.bss_info.mac_filter_enable == false){
18700 acl_mode = 0;
18701 }else {
18702 if (vap_info->u.bss_info.mac_filter_mode == wifi_mac_filter_mode_black_list){
18703 acl_mode = 2;
developer8078acf2023-08-04 18:52:48 +080018704 res = _syscmd_secure(buf, sizeof(buf), "touch %s%d", DENY_PREFIX, vap_info->vap_index);
18705 if (res) {
18706 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
18707
developere40952c2023-06-15 18:46:43 +080018708 }
developer8078acf2023-08-04 18:52:48 +080018709
developera3511852023-06-14 14:12:59 +080018710 }else{
18711 acl_mode = 1;
18712 }
18713 }
developer72fb0bb2023-01-11 09:46:29 +080018714
developera3511852023-06-14 14:12:59 +080018715 ret = wifi_setApWmmUapsdEnable(vap_info->vap_index, vap_info->u.bss_info.UAPSDEnabled);
18716 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018717 wifi_debug(DEBUG_ERROR, "wifi_setApWmmUapsdEnable return error\n");
developera3511852023-06-14 14:12:59 +080018718 return RETURN_ERR;
18719 }
developer72fb0bb2023-01-11 09:46:29 +080018720
developera3511852023-06-14 14:12:59 +080018721 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080018722 beaconRate_enum_to_string(vap_info->u.bss_info.beaconRate, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080018723 ret = wifi_setApBeaconRate(vap_info->radio_index, buf);
18724 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018725 wifi_debug(DEBUG_ERROR, "wifi_setApBeaconRate return error\n");
developera3511852023-06-14 14:12:59 +080018726 return RETURN_ERR;
18727 }
developer72fb0bb2023-01-11 09:46:29 +080018728
developera3511852023-06-14 14:12:59 +080018729 ret = wifi_setRadioIGMPSnoopingEnable(vap_info->radio_index, vap_info->u.bss_info.mcast2ucast);
18730 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018731 wifi_debug(DEBUG_ERROR, "wifi_setRadioIGMPSnoopingEnable\n");
developera3511852023-06-14 14:12:59 +080018732 return RETURN_ERR;
18733 }
developer72fb0bb2023-01-11 09:46:29 +080018734
developera3511852023-06-14 14:12:59 +080018735 ret = wifi_setApSecurity(vap_info->vap_index, &vap_info->u.bss_info.security);
18736 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018737 wifi_debug(DEBUG_ERROR, "wifi_setApSecurity return error\n");
developera3511852023-06-14 14:12:59 +080018738 return RETURN_ERR;
18739 }
developer333c1eb2023-05-31 14:59:39 +080018740
developer56fbedb2023-05-30 16:47:05 +080018741 hostapd_raw_restart_bss(vap_info->vap_index);
developer72fb0bb2023-01-11 09:46:29 +080018742
developera3511852023-06-14 14:12:59 +080018743 multiple_set = FALSE;
developer23e71282023-01-18 10:25:19 +080018744
developera3511852023-06-14 14:12:59 +080018745 // If config use hostapd_cli to set, we calling these type of functions after enable the ap.
18746 ret = wifi_setApMacAddressControlMode(vap_info->vap_index, acl_mode);
18747 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018748 wifi_debug(DEBUG_ERROR, "wifi_setApMacAddressControlMode return error\n");
developera3511852023-06-14 14:12:59 +080018749 return RETURN_ERR;
18750 }
developer72fb0bb2023-01-11 09:46:29 +080018751
developer6bfd6462023-08-24 14:28:18 +080018752 ret = wifi_setApWmmEnable(vap_info->vap_index, vap_info->u.bss_info.wmm_enabled);
18753 if (ret != RETURN_OK) {
18754 wifi_debug(DEBUG_ERROR, "wifi_setApWmmEnable return error\n");
18755 return RETURN_ERR;
18756 }
18757
developera3511852023-06-14 14:12:59 +080018758 // TODO mgmtPowerControl, interworking, wps
18759 }
18760 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18761 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018762}
18763
18764int parse_channel_list_int_arr(char *pchannels, wifi_channels_list_t* chlistptr)
18765{
developera3511852023-06-14 14:12:59 +080018766 char *token, *next;
18767 const char s[2] = ",";
18768 int count =0;
developer72fb0bb2023-01-11 09:46:29 +080018769
developera3511852023-06-14 14:12:59 +080018770 /* get the first token */
18771 token = strtok_r(pchannels, s, &next);
developer72fb0bb2023-01-11 09:46:29 +080018772
developera3511852023-06-14 14:12:59 +080018773 /* walk through other tokens */
18774 while( token != NULL && count < MAX_CHANNELS) {
18775 chlistptr->channels_list[count++] = atoi(token);
18776 token = strtok_r(NULL, s, &next);
18777 }
developer72fb0bb2023-01-11 09:46:29 +080018778
developera3511852023-06-14 14:12:59 +080018779 return count;
developer72fb0bb2023-01-11 09:46:29 +080018780}
18781
18782static int getRadioCapabilities(int radioIndex, wifi_radio_capabilities_t *rcap)
18783{
developera3511852023-06-14 14:12:59 +080018784 INT status;
18785 wifi_channels_list_t *chlistp;
18786 CHAR output_string[64];
18787 CHAR pchannels[128];
18788 CHAR interface_name[16] = {0};
18789 wifi_band band;
developere40952c2023-06-15 18:46:43 +080018790 int res;
developer72fb0bb2023-01-11 09:46:29 +080018791
developera3511852023-06-14 14:12:59 +080018792 if(rcap == NULL)
18793 {
18794 return RETURN_ERR;
18795 }
developer72fb0bb2023-01-11 09:46:29 +080018796
developera3511852023-06-14 14:12:59 +080018797 rcap->numSupportedFreqBand = 1;
18798 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080018799
developera3511852023-06-14 14:12:59 +080018800 if (band == band_2_4)
18801 rcap->band[0] = WIFI_FREQUENCY_2_4_BAND;
18802 else if (band == band_5)
18803 rcap->band[0] = WIFI_FREQUENCY_5_BAND;
18804 else if (band == band_6)
18805 rcap->band[0] = WIFI_FREQUENCY_6_BAND;
developer72fb0bb2023-01-11 09:46:29 +080018806
developera3511852023-06-14 14:12:59 +080018807 chlistp = &(rcap->channel_list[0]);
18808 memset(pchannels, 0, sizeof(pchannels));
developer72fb0bb2023-01-11 09:46:29 +080018809
developera3511852023-06-14 14:12:59 +080018810 /* possible number of radio channels */
18811 status = wifi_getRadioPossibleChannels(radioIndex, pchannels);
18812 {
18813 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, pchannels);
18814 }
18815 /* Number of channels and list*/
18816 chlistp->num_channels = parse_channel_list_int_arr(pchannels, chlistp);
developer72fb0bb2023-01-11 09:46:29 +080018817
developera3511852023-06-14 14:12:59 +080018818 /* autoChannelSupported */
18819 /* always ON with wifi_getRadioAutoChannelSupported */
18820 rcap->autoChannelSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018821
developera3511852023-06-14 14:12:59 +080018822 /* DCSSupported */
18823 /* always ON with wifi_getRadioDCSSupported */
18824 rcap->DCSSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018825
developera3511852023-06-14 14:12:59 +080018826 /* zeroDFSSupported - TBD */
18827 rcap->zeroDFSSupported = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018828
developera3511852023-06-14 14:12:59 +080018829 /* Supported Country List*/
18830 memset(output_string, 0, sizeof(output_string));
18831 status = wifi_getRadioCountryCode(radioIndex, output_string);
18832 if( status != 0 ) {
18833 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, output_string);
18834 return RETURN_ERR;
18835 } else {
18836 printf("[wifi_hal dbg] : func[%s] line[%d], output [%s]\n", __FUNCTION__, __LINE__, output_string);
18837 }
18838 if(!strcmp(output_string,"US")){
18839 rcap->countrySupported[0] = wifi_countrycode_US;
18840 rcap->countrySupported[1] = wifi_countrycode_CA;
18841 } else if (!strcmp(output_string,"CA")) {
18842 rcap->countrySupported[0] = wifi_countrycode_CA;
18843 rcap->countrySupported[1] = wifi_countrycode_US;
18844 } else {
18845 printf("[wifi_hal dbg] : func[%s] line[%d] radio_index[%d] Invalid Country [%s]\n", __FUNCTION__, __LINE__, radioIndex, output_string);
18846 }
developer72fb0bb2023-01-11 09:46:29 +080018847
developera3511852023-06-14 14:12:59 +080018848 rcap->numcountrySupported = 2;
developer72fb0bb2023-01-11 09:46:29 +080018849
developera3511852023-06-14 14:12:59 +080018850 /* csi */
18851 rcap->csi.maxDevices = 8;
18852 rcap->csi.soudingFrameSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018853
developer86035662023-06-28 19:21:12 +080018854 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
18855 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
18856 }
developere40952c2023-06-15 18:46:43 +080018857 res = snprintf(rcap->ifaceName, sizeof(interface_name), "%s",interface_name);
18858 if (os_snprintf_error(sizeof(interface_name), res)) {
18859 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18860 return RETURN_ERR;
18861 }
developer72fb0bb2023-01-11 09:46:29 +080018862
developera3511852023-06-14 14:12:59 +080018863 /* channelWidth - all supported bandwidths */
18864 int i=0;
18865 rcap->channelWidth[i] = 0;
18866 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18867 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18868 WIFI_CHANNELBANDWIDTH_40MHZ);
developer72fb0bb2023-01-11 09:46:29 +080018869
developera3511852023-06-14 14:12:59 +080018870 }
18871 else if (rcap->band[i] & (WIFI_FREQUENCY_5_BAND ) || rcap->band[i] & (WIFI_FREQUENCY_6_BAND)) {
18872 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18873 WIFI_CHANNELBANDWIDTH_40MHZ |
18874 WIFI_CHANNELBANDWIDTH_80MHZ | WIFI_CHANNELBANDWIDTH_160MHZ);
18875 }
developer72fb0bb2023-01-11 09:46:29 +080018876
18877
developera3511852023-06-14 14:12:59 +080018878 /* mode - all supported variants */
18879 // rcap->mode[i] = WIFI_80211_VARIANT_H;
18880 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) {
18881 rcap->mode[i] = ( WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX );
18882 }
18883 else if (rcap->band[i] & WIFI_FREQUENCY_5_BAND ) {
18884 rcap->mode[i] = ( WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX );
18885 }
18886 else if (rcap->band[i] & WIFI_FREQUENCY_6_BAND) {
18887 rcap->mode[i] = ( WIFI_80211_VARIANT_AX );
18888 }
18889 rcap->maxBitRate[i] = ( rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) ? 300 :
18890 ((rcap->band[i] & WIFI_FREQUENCY_5_BAND) ? 1734 : 0);
developer72fb0bb2023-01-11 09:46:29 +080018891
developera3511852023-06-14 14:12:59 +080018892 /* supportedBitRate - all supported bitrates */
18893 rcap->supportedBitRate[i] = 0;
18894 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18895 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18896 WIFI_BITRATE_11MBPS | WIFI_BITRATE_12MBPS);
18897 }
18898 else if ((rcap->band[i] & (WIFI_FREQUENCY_5_BAND )) || (rcap->band[i] & (WIFI_FREQUENCY_6_BAND))) {
18899 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18900 WIFI_BITRATE_12MBPS | WIFI_BITRATE_18MBPS | WIFI_BITRATE_24MBPS |
18901 WIFI_BITRATE_36MBPS | WIFI_BITRATE_48MBPS | WIFI_BITRATE_54MBPS);
18902 }
developer72fb0bb2023-01-11 09:46:29 +080018903
18904
developera3511852023-06-14 14:12:59 +080018905 rcap->transmitPowerSupported_list[i].numberOfElements = 5;
18906 rcap->transmitPowerSupported_list[i].transmitPowerSupported[0]=12;
18907 rcap->transmitPowerSupported_list[i].transmitPowerSupported[1]=25;
18908 rcap->transmitPowerSupported_list[i].transmitPowerSupported[2]=50;
18909 rcap->transmitPowerSupported_list[i].transmitPowerSupported[3]=75;
18910 rcap->transmitPowerSupported_list[i].transmitPowerSupported[4]=100;
18911 rcap->cipherSupported = 0;
18912 rcap->cipherSupported |= WIFI_CIPHER_CAPA_ENC_TKIP | WIFI_CIPHER_CAPA_ENC_CCMP;
18913 rcap->maxNumberVAPs = MAX_NUM_VAP_PER_RADIO;
developer72fb0bb2023-01-11 09:46:29 +080018914
developera3511852023-06-14 14:12:59 +080018915 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018916}
18917
18918INT wifi_getHalCapability(wifi_hal_capability_t *cap)
18919{
developera3511852023-06-14 14:12:59 +080018920 INT status = 0, radioIndex = 0;
18921 char output[MAX_BUF_SIZE] = {0};
18922 int iter = 0;
18923 unsigned int j = 0;
developer9ce44382023-06-28 11:09:37 +080018924 int max_num_radios = 0;
developera3511852023-06-14 14:12:59 +080018925 wifi_interface_name_idex_map_t *iface_info = NULL;
developer72fb0bb2023-01-11 09:46:29 +080018926
developera3511852023-06-14 14:12:59 +080018927 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018928
developera3511852023-06-14 14:12:59 +080018929 memset(cap, 0, sizeof(wifi_hal_capability_t));
developer72fb0bb2023-01-11 09:46:29 +080018930
developera3511852023-06-14 14:12:59 +080018931 /* version */
18932 cap->version.major = WIFI_HAL_MAJOR_VERSION;
18933 cap->version.minor = WIFI_HAL_MINOR_VERSION;
developer72fb0bb2023-01-11 09:46:29 +080018934
developera3511852023-06-14 14:12:59 +080018935 /* number of radios platform property */
18936 wifi_getMaxRadioNumber(&max_num_radios);
18937 cap->wifi_prop.numRadios = max_num_radios;
developer72fb0bb2023-01-11 09:46:29 +080018938
developera3511852023-06-14 14:12:59 +080018939 for(radioIndex=0; radioIndex < cap->wifi_prop.numRadios; radioIndex++)
18940 {
18941 status = getRadioCapabilities(radioIndex, &(cap->wifi_prop.radiocap[radioIndex]));
18942 if (status != 0) {
18943 printf("%s: getRadioCapabilities idx = %d\n", __FUNCTION__, radioIndex);
18944 return RETURN_ERR;
18945 }
developer72fb0bb2023-01-11 09:46:29 +080018946
developera3511852023-06-14 14:12:59 +080018947 for (j = 0; j < cap->wifi_prop.radiocap[radioIndex].maxNumberVAPs; j++)
18948 {
18949 if (iter >= MAX_NUM_RADIOS * MAX_NUM_VAP_PER_RADIO)
18950 {
18951 printf("%s: to many vaps for index map (%d)\n", __func__, iter);
18952 return RETURN_ERR;
18953 }
18954 iface_info = &cap->wifi_prop.interface_map[iter];
18955 iface_info->phy_index = radioIndex; // XXX: parse phyX index instead
18956 iface_info->rdk_radio_index = radioIndex;
18957 memset(output, 0, sizeof(output));
18958 if (wifi_getRadioIfName(radioIndex, output) == RETURN_OK)
18959 {
18960 strncpy(iface_info->interface_name, output, sizeof(iface_info->interface_name) - 1);
18961 }
18962 // TODO: bridge name
18963 // TODO: vlan id
18964 // TODO: primary
18965 iface_info->index = array_index_to_vap_index(radioIndex, j);
18966 memset(output, 0, sizeof(output));
18967 if (wifi_getApName(iface_info->index, output) == RETURN_OK)
18968 {
18969 strncpy(iface_info->vap_name, output, sizeof(iface_info->vap_name) - 1);
18970 }
18971 iter++;
18972 }
18973 }
developer72fb0bb2023-01-11 09:46:29 +080018974
developera3511852023-06-14 14:12:59 +080018975 cap->BandSteeringSupported = FALSE;
18976 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18977 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018978}
18979
18980INT wifi_setOpportunisticKeyCaching(int ap_index, BOOL okc_enable)
18981{
developera3511852023-06-14 14:12:59 +080018982 struct params h_config={0};
18983 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018984 int res;
developer72fb0bb2023-01-11 09:46:29 +080018985
developera3511852023-06-14 14:12:59 +080018986 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018987
developera3511852023-06-14 14:12:59 +080018988 h_config.name = "okc";
18989 h_config.value = okc_enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018990
developere40952c2023-06-15 18:46:43 +080018991 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18992 if (os_snprintf_error(sizeof(config_file), res)) {
18993 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18994 return RETURN_ERR;
18995 }
developera3511852023-06-14 14:12:59 +080018996 wifi_hostapdWrite(config_file, &h_config, 1);
18997 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018998
developera3511852023-06-14 14:12:59 +080018999 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19000 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019001}
19002
19003INT wifi_setSAEMFP(int ap_index, BOOL enable)
19004{
developera3511852023-06-14 14:12:59 +080019005 struct params h_config={0};
19006 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080019007 int res;
developer72fb0bb2023-01-11 09:46:29 +080019008
developera3511852023-06-14 14:12:59 +080019009 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019010
developera3511852023-06-14 14:12:59 +080019011 h_config.name = "sae_require_mfp";
19012 h_config.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080019013
developere40952c2023-06-15 18:46:43 +080019014 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19015 if (os_snprintf_error(sizeof(config_file), res)) {
19016 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19017 return RETURN_ERR;
19018 }
developera3511852023-06-14 14:12:59 +080019019 wifi_hostapdWrite(config_file, &h_config, 1);
19020 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019021
developera3511852023-06-14 14:12:59 +080019022 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19023 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019024}
19025
19026INT wifi_setSAEpwe(int ap_index, int sae_pwe)
19027{
developera3511852023-06-14 14:12:59 +080019028 struct params h_config={0};
19029 char config_file[64] = {0};
19030 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080019031 int res;
developer72fb0bb2023-01-11 09:46:29 +080019032
developera3511852023-06-14 14:12:59 +080019033 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019034
developera3511852023-06-14 14:12:59 +080019035 h_config.name = "sae_pwe";
developere40952c2023-06-15 18:46:43 +080019036 res = snprintf(buf, sizeof(buf), "%d", sae_pwe);
19037 if (os_snprintf_error(sizeof(buf), res)) {
19038 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19039 return RETURN_ERR;
19040 }
19041
developera3511852023-06-14 14:12:59 +080019042 h_config.value = buf;
developer72fb0bb2023-01-11 09:46:29 +080019043
developere40952c2023-06-15 18:46:43 +080019044 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19045 if (os_snprintf_error(sizeof(config_file), res)) {
19046 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19047 return RETURN_ERR;
19048 }
developera3511852023-06-14 14:12:59 +080019049 wifi_hostapdWrite(config_file, &h_config, 1);
19050 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019051
developera3511852023-06-14 14:12:59 +080019052 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19053 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019054}
19055
19056INT wifi_setDisable_EAPOL_retries(int ap_index, BOOL disable_EAPOL_retries)
19057{
developera3511852023-06-14 14:12:59 +080019058 // wpa3 use SAE instead of PSK, so we need to disable this feature when using wpa3.
19059 struct params h_config={0};
19060 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080019061 int res;
developer72fb0bb2023-01-11 09:46:29 +080019062
developera3511852023-06-14 14:12:59 +080019063 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019064
developera3511852023-06-14 14:12:59 +080019065 h_config.name = "wpa_disable_eapol_key_retries";
19066 h_config.value = disable_EAPOL_retries?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080019067
developere40952c2023-06-15 18:46:43 +080019068 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19069 if (os_snprintf_error(sizeof(config_file), res)) {
19070 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19071 return RETURN_ERR;
19072 }
developera3511852023-06-14 14:12:59 +080019073 wifi_hostapdWrite(config_file, &h_config, 1);
19074 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019075
developera3511852023-06-14 14:12:59 +080019076 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19077 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019078}
19079
19080INT wifi_setApSecurity(INT ap_index, wifi_vap_security_t *security)
19081{
developera3511852023-06-14 14:12:59 +080019082 char buf[128] = {0};
19083 char config_file[128] = {0};
developer8078acf2023-08-04 18:52:48 +080019084
developera3511852023-06-14 14:12:59 +080019085 char password[65] = {0};
19086 char mfp[32] = {0};
19087 char wpa_mode[32] = {0};
19088 BOOL okc_enable = FALSE;
19089 BOOL sae_MFP = FALSE;
19090 BOOL disable_EAPOL_retries = TRUE;
19091 int sae_pwe = 0;
19092 struct params params = {0};
19093 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080019094 int res;
developer72fb0bb2023-01-11 09:46:29 +080019095
developera3511852023-06-14 14:12:59 +080019096 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019097
developera3511852023-06-14 14:12:59 +080019098 multiple_set = TRUE;
developer37646972023-06-29 10:58:43 +080019099 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19100 if (os_snprintf_error(sizeof(config_file), res)) {
19101 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19102 return RETURN_ERR;
19103 }
developera3511852023-06-14 14:12:59 +080019104 if (security->mode == wifi_security_mode_none) {
developer9ce44382023-06-28 11:09:37 +080019105 strncpy(wpa_mode, "None",sizeof(wpa_mode) - 1);
19106 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19107 } else if (security->mode == wifi_security_mode_wpa_personal) {
19108 strncpy(wpa_mode, "WPA-Personal",sizeof(wpa_mode) - 1);
19109 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19110 } else if (security->mode == wifi_security_mode_wpa2_personal){
19111 strncpy(wpa_mode, "WPA2-Personal",sizeof(wpa_mode) - 1);
19112 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19113 } else if (security->mode == wifi_security_mode_wpa_wpa2_personal){
19114 strncpy(wpa_mode, "WPA-WPA2-Personal",sizeof(wpa_mode) - 1);
19115 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19116 } else if (security->mode == wifi_security_mode_wpa_enterprise){
19117 strncpy(wpa_mode, "WPA-Enterprise",sizeof(wpa_mode) - 1);
19118 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19119 } else if (security->mode == wifi_security_mode_wpa2_enterprise){
19120 strncpy(wpa_mode, "WPA2-Enterprise",sizeof(wpa_mode) - 1);
19121 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19122 } else if (security->mode == wifi_security_mode_wpa_wpa2_enterprise){
19123 strncpy(wpa_mode, "WPA-WAP2-Enterprise",sizeof(wpa_mode) - 1);
19124 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19125 } else if (security->mode == wifi_security_mode_wpa3_personal) {
19126 strncpy(wpa_mode, "WPA3-Personal",sizeof(wpa_mode) - 1);
19127 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019128 okc_enable = TRUE;
19129 sae_MFP = TRUE;
19130 sae_pwe = 2;
19131 disable_EAPOL_retries = FALSE;
19132 } else if (security->mode == wifi_security_mode_wpa3_transition) {
developer9ce44382023-06-28 11:09:37 +080019133 strncpy(wpa_mode, "WPA3-Personal-Transition",sizeof(wpa_mode) - 1);
19134 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019135 okc_enable = TRUE;
19136 sae_MFP = TRUE;
19137 sae_pwe = 2;
19138 disable_EAPOL_retries = FALSE;
19139 } else if (security->mode == wifi_security_mode_wpa3_enterprise) {
developer9ce44382023-06-28 11:09:37 +080019140 strncpy(wpa_mode, "WPA3-Enterprise",sizeof(wpa_mode) - 1);
19141 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019142 sae_MFP = TRUE;
19143 sae_pwe = 2;
19144 disable_EAPOL_retries = FALSE;
19145 } else if (security->mode == wifi_security_mode_enhanced_open) {
developer9ce44382023-06-28 11:09:37 +080019146 strncpy(wpa_mode, "OWE",sizeof(wpa_mode) - 1);
19147 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019148 sae_MFP = TRUE;
19149 sae_pwe = 2;
19150 disable_EAPOL_retries = FALSE;
19151 }
developer72fb0bb2023-01-11 09:46:29 +080019152
developera3511852023-06-14 14:12:59 +080019153 band = wifi_index_to_band(ap_index);
19154 if (band == band_6 && strstr(wpa_mode, "WPA3") == NULL) {
developerc14d83a2023-06-29 20:09:42 +080019155 wifi_debug(DEBUG_ERROR, "%s: 6G band must set with wpa3.\n", __func__);
developera3511852023-06-14 14:12:59 +080019156 return RETURN_ERR;
19157 }
developer72fb0bb2023-01-11 09:46:29 +080019158
developera3511852023-06-14 14:12:59 +080019159 wifi_setApSecurityModeEnabled(ap_index, wpa_mode);
19160 wifi_setOpportunisticKeyCaching(ap_index, okc_enable);
19161 wifi_setSAEMFP(ap_index, sae_MFP);
19162 wifi_setSAEpwe(ap_index, sae_pwe);
19163 wifi_setDisable_EAPOL_retries(ap_index, disable_EAPOL_retries);
developer72fb0bb2023-01-11 09:46:29 +080019164
developera3511852023-06-14 14:12:59 +080019165 if (security->mode != wifi_security_mode_none && security->mode != wifi_security_mode_enhanced_open) {
19166 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) {
19167 int key_len = strlen(security->u.key.key);
19168 // wpa_psk and wpa_passphrase cann;t use at the same time, the command replace one with the other.
19169 if (key_len == 64) { // set wpa_psk
19170 strncpy(password, security->u.key.key, 64); // 64 characters
19171 password[64] = '\0';
19172 wifi_setApSecurityPreSharedKey(ap_index, password);
developer8078acf2023-08-04 18:52:48 +080019173 res = _syscmd_secure(buf, sizeof(buf), "sed -i -n -e '/^wpa_passphrase=/!p' %s", config_file);
19174 if (res) {
19175 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
19176
developere40952c2023-06-15 18:46:43 +080019177 }
developera3511852023-06-14 14:12:59 +080019178 } else if (key_len >= 8 && key_len < 64) { // set wpa_passphrase
19179 strncpy(password, security->u.key.key, 63);
19180 password[63] = '\0';
19181 wifi_setApSecurityKeyPassphrase(ap_index, password);
developer8078acf2023-08-04 18:52:48 +080019182 res = _syscmd_secure(buf, sizeof(buf), "sed -i -n -e '/^wpa_psk=/!p' %s", config_file);
19183 if (res) {
19184 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
19185
developer9ce44382023-06-28 11:09:37 +080019186 }
developera3511852023-06-14 14:12:59 +080019187 } else
19188 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080019189
developera3511852023-06-14 14:12:59 +080019190 }
19191 if (security->u.key.type == wifi_security_key_type_sae || security->u.key.type == wifi_security_key_type_psk_sae) {
19192 params.name = "sae_password";
19193 params.value = security->u.key.key;
19194 wifi_hostapdWrite(config_file, &params, 1);
19195 } else { // remove sae_password
developer8078acf2023-08-04 18:52:48 +080019196 res = _syscmd_secure(buf, sizeof(buf), "sed -i -n -e '/^sae_password=/!p' %s", config_file);
19197 if (res) {
19198 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
19199
developere40952c2023-06-15 18:46:43 +080019200 }
developera3511852023-06-14 14:12:59 +080019201 }
19202 }
developer72fb0bb2023-01-11 09:46:29 +080019203
developera3511852023-06-14 14:12:59 +080019204 if (security->mode != wifi_security_mode_none) {
19205 memset(&params, 0, sizeof(params));
19206 params.name = "wpa_pairwise";
19207 if (security->encr == wifi_encryption_tkip)
19208 params.value = "TKIP";
19209 else if (security->encr == wifi_encryption_aes)
19210 params.value = "CCMP";
19211 else if (security->encr == wifi_encryption_aes_tkip)
19212 params.value = "TKIP CCMP";
19213 wifi_hostapdWrite(config_file, &params, 1);
19214 }
developer72fb0bb2023-01-11 09:46:29 +080019215
developer9ce44382023-06-28 11:09:37 +080019216 if (security->mfp == wifi_mfp_cfg_disabled){
19217 strncpy(mfp,"Disabled",sizeof(mfp)-1);
19218 mfp[sizeof(mfp)-1] = '\0';
19219 } else if (security->mfp == wifi_mfp_cfg_optional){
19220 strncpy(mfp,"Optional",sizeof(mfp)-1);
19221 mfp[sizeof(mfp)-1] = '\0';
19222 } else if (security->mfp == wifi_mfp_cfg_required){
19223 strncpy(mfp,"Required",sizeof(mfp)-1);
19224 mfp[sizeof(mfp)-1] = '\0';
19225 }
developera3511852023-06-14 14:12:59 +080019226 wifi_setApSecurityMFPConfig(ap_index, mfp);
developer72fb0bb2023-01-11 09:46:29 +080019227
developera3511852023-06-14 14:12:59 +080019228 memset(&params, 0, sizeof(params));
19229 params.name = "transition_disable";
19230 if (security->wpa3_transition_disable == TRUE)
19231 params.value = "0x01";
19232 else
19233 params.value = "0x00";
19234 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019235
developera3511852023-06-14 14:12:59 +080019236 memset(&params, 0, sizeof(params));
19237 params.name = "wpa_group_rekey";
developere40952c2023-06-15 18:46:43 +080019238 res = snprintf(buf, sizeof(buf), "%d", security->rekey_interval);
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 = "wpa_strict_rekey";
19248 params.value = security->strict_rekey?"1":"0";
19249 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019250
developera3511852023-06-14 14:12:59 +080019251 memset(&params, 0, sizeof(params));
19252 params.name = "wpa_pairwise_update_count";
19253 if (security->eapol_key_retries == 0)
19254 security->eapol_key_retries = 4; // 0 is invalid, set to default value.
developere40952c2023-06-15 18:46:43 +080019255 res = snprintf(buf, sizeof(buf), "%u", security->eapol_key_retries);
19256 if (os_snprintf_error(sizeof(buf), res)) {
19257 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19258 return RETURN_ERR;
19259 }
developera3511852023-06-14 14:12:59 +080019260 params.value = buf;
19261 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019262
developera3511852023-06-14 14:12:59 +080019263 memset(&params, 0, sizeof(params));
19264 params.name = "disable_pmksa_caching";
19265 params.value = security->disable_pmksa_caching?"1":"0";
19266 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019267
developera3511852023-06-14 14:12:59 +080019268 if (multiple_set == FALSE) {
19269 wifi_setApEnable(ap_index, FALSE);
19270 wifi_setApEnable(ap_index, TRUE);
19271 }
developer72fb0bb2023-01-11 09:46:29 +080019272
developera3511852023-06-14 14:12:59 +080019273 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019274
developera3511852023-06-14 14:12:59 +080019275 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019276}
19277
19278INT wifi_getApSecurity(INT ap_index, wifi_vap_security_t *security)
19279{
developera3511852023-06-14 14:12:59 +080019280 char buf[256] = {0};
19281 char config_file[128] = {0};
developer86035662023-06-28 19:21:12 +080019282 long int disable = 0;
19283 long int tmp;
developera3511852023-06-14 14:12:59 +080019284 bool set_sae = FALSE;
developere75ba632023-06-29 16:03:33 +080019285 int res;
developer72fb0bb2023-01-11 09:46:29 +080019286
developera3511852023-06-14 14:12:59 +080019287 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere75ba632023-06-29 16:03:33 +080019288 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19289 if (os_snprintf_error(sizeof(config_file), res)) {
19290 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19291 return RETURN_ERR;
19292 }
developera3511852023-06-14 14:12:59 +080019293 wifi_getApSecurityModeEnabled(ap_index, buf); // Get wpa config
19294 security->mode = wifi_security_mode_none;
19295 if (strlen(buf) != 0) {
19296 if (!strcmp(buf, "WPA-Personal"))
19297 security->mode = wifi_security_mode_wpa_personal;
19298 else if (!strcmp(buf, "WPA2-Personal"))
19299 security->mode = wifi_security_mode_wpa2_personal;
19300 else if (!strcmp(buf, "WPA-WPA2-Personal"))
19301 security->mode = wifi_security_mode_wpa_wpa2_personal;
19302 else if (!strcmp(buf, "WPA-Enterprise"))
19303 security->mode = wifi_security_mode_wpa_enterprise;
19304 else if (!strcmp(buf, "WPA2-Enterprise"))
19305 security->mode = wifi_security_mode_wpa2_enterprise;
19306 else if (!strcmp(buf, "WPA-WPA2-Enterprise"))
19307 security->mode = wifi_security_mode_wpa_wpa2_enterprise;
19308 else if (!strcmp(buf, "WPA3-Personal"))
19309 security->mode = wifi_security_mode_wpa3_personal;
19310 else if (!strcmp(buf, "WPA3-Personal-Transition"))
19311 security->mode = wifi_security_mode_wpa3_transition;
19312 else if (!strcmp(buf, "WPA3-Enterprise"))
19313 security->mode = wifi_security_mode_wpa3_enterprise;
19314 else if (!strcmp(buf, "OWE"))
19315 security->mode = wifi_security_mode_enhanced_open;
19316 }
developer72fb0bb2023-01-11 09:46:29 +080019317
developera3511852023-06-14 14:12:59 +080019318 wifi_hostapdRead(config_file,"wpa_pairwise",buf,sizeof(buf));
19319 if (security->mode == wifi_security_mode_none)
19320 security->encr = wifi_encryption_none;
19321 else {
19322 if (strcmp(buf, "TKIP") == 0)
19323 security->encr = wifi_encryption_tkip;
19324 else if (strcmp(buf, "CCMP") == 0)
19325 security->encr = wifi_encryption_aes;
19326 else
19327 security->encr = wifi_encryption_aes_tkip;
19328 }
developer72fb0bb2023-01-11 09:46:29 +080019329
developera3511852023-06-14 14:12:59 +080019330 if (security->mode != wifi_security_mode_none) {
19331 memset(buf, 0, sizeof(buf));
19332 // wpa3 can use one or both configs as password, so we check sae_password first.
19333 wifi_hostapdRead(config_file, "sae_password", buf, sizeof(buf));
19334 if (strlen(buf) != 0) {
19335 if (security->mode == wifi_security_mode_wpa3_personal || security->mode == wifi_security_mode_wpa3_transition)
19336 security->u.key.type = wifi_security_key_type_sae;
19337 set_sae = TRUE;
19338 strncpy(security->u.key.key, buf, sizeof(buf));
19339 }
19340 wifi_hostapdRead(config_file, "wpa_passphrase", buf, sizeof(buf));
19341 if (strlen(buf) != 0){
19342 if (set_sae == TRUE)
19343 security->u.key.type = wifi_security_key_type_psk_sae;
19344 else if (strlen(buf) == 64)
19345 security->u.key.type = wifi_security_key_type_psk;
19346 else
19347 security->u.key.type = wifi_security_key_type_pass;
19348 strncpy(security->u.key.key, buf, sizeof(security->u.key.key));
19349 }
19350 security->u.key.key[255] = '\0';
19351 }
developer72fb0bb2023-01-11 09:46:29 +080019352
developera3511852023-06-14 14:12:59 +080019353 memset(buf, 0, sizeof(buf));
19354 wifi_getApSecurityMFPConfig(ap_index, buf);
19355 if (strcmp(buf, "Disabled") == 0)
19356 security->mfp = wifi_mfp_cfg_disabled;
19357 else if (strcmp(buf, "Optional") == 0)
19358 security->mfp = wifi_mfp_cfg_optional;
19359 else if (strcmp(buf, "Required") == 0)
19360 security->mfp = wifi_mfp_cfg_required;
developer72fb0bb2023-01-11 09:46:29 +080019361
developera3511852023-06-14 14:12:59 +080019362 memset(buf, 0, sizeof(buf));
19363 security->wpa3_transition_disable = FALSE;
19364 wifi_hostapdRead(config_file, "transition_disable", buf, sizeof(buf));
developer3de255a2023-06-29 10:35:45 +080019365 if (strlen(buf) == 0)
19366 disable = 0;
19367 else {
19368 if (hal_strtol(buf, 16, &disable) < 0) {
19369 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19370 return RETURN_ERR;
19371 }
developer86035662023-06-28 19:21:12 +080019372 }
developera3511852023-06-14 14:12:59 +080019373 if (disable != 0)
19374 security->wpa3_transition_disable = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080019375
developera3511852023-06-14 14:12:59 +080019376 memset(buf, 0, sizeof(buf));
19377 wifi_hostapdRead(config_file, "wpa_group_rekey", buf, sizeof(buf));
19378 if (strlen(buf) == 0)
19379 security->rekey_interval = 86400;
developer86035662023-06-28 19:21:12 +080019380 else {
19381 if (hal_strtol(buf, 10, &tmp) < 0) {
19382 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19383 return RETURN_ERR;
19384 }
19385 security->rekey_interval = tmp;
19386 }
developer72fb0bb2023-01-11 09:46:29 +080019387
developera3511852023-06-14 14:12:59 +080019388 memset(buf, 0, sizeof(buf));
19389 wifi_hostapdRead(config_file, "wpa_strict_rekey", buf, sizeof(buf));
19390 if (strlen(buf) == 0)
19391 security->strict_rekey = 1;
developer86035662023-06-28 19:21:12 +080019392 else {
19393 if (hal_strtol(buf, 10, &tmp) < 0) {
19394 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19395 return RETURN_ERR;
19396 }
19397 security->strict_rekey = tmp;
19398 }
developer72fb0bb2023-01-11 09:46:29 +080019399
developera3511852023-06-14 14:12:59 +080019400 memset(buf, 0, sizeof(buf));
19401 wifi_hostapdRead(config_file, "wpa_pairwise_update_count", buf, sizeof(buf));
19402 if (strlen(buf) == 0)
19403 security->eapol_key_retries = 4;
developer86035662023-06-28 19:21:12 +080019404 else {
19405 if (hal_strtol(buf, 10, &tmp) < 0) {
19406 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19407 return RETURN_ERR;
19408 }
19409 security->eapol_key_retries = tmp;
19410 }
developer72fb0bb2023-01-11 09:46:29 +080019411
developera3511852023-06-14 14:12:59 +080019412 memset(buf, 0, sizeof(buf));
19413 wifi_hostapdRead(config_file, "disable_pmksa_caching", buf, sizeof(buf));
19414 if (strlen(buf) == 0)
19415 security->disable_pmksa_caching = FALSE;
developer86035662023-06-28 19:21:12 +080019416 else {
19417 if (hal_strtol(buf, 10, &(tmp)) < 0) {
19418 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19419 return RETURN_ERR;
19420 }
19421 security->disable_pmksa_caching = tmp ? TRUE : FALSE;
19422 }
developera3511852023-06-14 14:12:59 +080019423 /* TODO
19424 eapol_key_timeout, eap_identity_req_timeout, eap_identity_req_retries, eap_req_timeout, eap_req_retries
19425 */
19426 security->eapol_key_timeout = 1000; // Unit is ms. The default value in protocol.
19427 security->eap_identity_req_timeout = 0;
19428 security->eap_identity_req_retries = 0;
19429 security->eap_req_timeout = 0;
19430 security->eap_req_retries = 0;
19431 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19432 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019433}
19434
19435#endif /* WIFI_HAL_VERSION_3 */
19436
19437#ifdef WIFI_HAL_VERSION_3_PHASE2
19438INT wifi_getApAssociatedDevice(INT ap_index, mac_address_t *output_deviceMacAddressArray, UINT maxNumDevices, UINT *output_numDevices)
19439{
developera3511852023-06-14 14:12:59 +080019440 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +080019441
developera3511852023-06-14 14:12:59 +080019442 char buf[128] = {0};
19443 char *mac_addr = NULL;
19444 BOOL status = FALSE;
19445 size_t len = 0;
developer8078acf2023-08-04 18:52:48 +080019446 int res;
developer72fb0bb2023-01-11 09:46:29 +080019447
developera3511852023-06-14 14:12:59 +080019448 if(ap_index > MAX_APS)
19449 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019450
developera3511852023-06-14 14:12:59 +080019451 *output_numDevices = 0;
19452 wifi_getApEnable(ap_index, &status);
19453 if (status == FALSE)
19454 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019455
developera3511852023-06-14 14:12:59 +080019456 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19457 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080019458
19459 _syscmd_secure(buf, sizeof(buf), "hostapd_cli -i %s list_sta", interface_name);
19460 if (res) {
19461 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
19462 }
developer72fb0bb2023-01-11 09:46:29 +080019463
developera3511852023-06-14 14:12:59 +080019464 mac_addr = strtok(buf, "\n");
19465 for (int i = 0; i < maxNumDevices && mac_addr != NULL; i++) {
19466 *output_numDevices = i + 1;
developerc14d83a2023-06-29 20:09:42 +080019467 wifi_debug(DEBUG_ERROR,, "mac_addr: %s\n", mac_addr);
developera3511852023-06-14 14:12:59 +080019468 addr_ptr = output_deviceMacAddressArray[i];
19469 mac_addr_aton(addr_ptr, mac_addr);
19470 mac_addr = strtok(NULL, "\n");
19471 }
developer72fb0bb2023-01-11 09:46:29 +080019472
developera3511852023-06-14 14:12:59 +080019473 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019474}
19475#else
19476INT wifi_getApAssociatedDevice(INT ap_index, CHAR *output_buf, INT output_buf_size)
19477{
developera3511852023-06-14 14:12:59 +080019478 char interface_name[16] = {0};
developer8078acf2023-08-04 18:52:48 +080019479
developera3511852023-06-14 14:12:59 +080019480 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +080019481 int res;
developer72fb0bb2023-01-11 09:46:29 +080019482
developera3511852023-06-14 14:12:59 +080019483 if(ap_index > MAX_APS || output_buf == NULL || output_buf_size <= 0)
19484 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019485
developera3511852023-06-14 14:12:59 +080019486 output_buf[0] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080019487
developera3511852023-06-14 14:12:59 +080019488 wifi_getApEnable(ap_index,&status);
19489 if (!status)
19490 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019491
developera3511852023-06-14 14:12:59 +080019492 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19493 return RETURN_ERR;
developer8078acf2023-08-04 18:52:48 +080019494 res = _syscmd_secure(output_buf, output_buf_size, "hostapd_cli -i %s list_sta | tr '\\n' ',' | sed 's/.$//'", interface_name);
19495 if (res) {
19496 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
19497
developer75bd10c2023-06-27 11:34:08 +080019498 }
developer5b23cd02023-07-19 20:26:03 +080019499
developer8078acf2023-08-04 18:52:48 +080019500
developer69b61b02023-03-07 17:17:44 +080019501
developera3511852023-06-14 14:12:59 +080019502 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019503}
19504#endif
19505
19506INT wifi_getProxyArp(INT apIndex, BOOL *enable)
19507{
developera3511852023-06-14 14:12:59 +080019508 char output[16]={'\0'};
19509 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080019510 int res;
developer72fb0bb2023-01-11 09:46:29 +080019511
developera3511852023-06-14 14:12:59 +080019512 if (!enable)
19513 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019514
developer75bd10c2023-06-27 11:34:08 +080019515 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
19516 if (os_snprintf_error(sizeof(config_file), res)) {
19517 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19518 return RETURN_ERR;
19519 }
developera3511852023-06-14 14:12:59 +080019520 wifi_hostapdRead(config_file, "proxy_arp", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080019521
developera3511852023-06-14 14:12:59 +080019522 if (strlen(output) == 0)
19523 *enable = FALSE;
19524 else if (strncmp(output, "1", 1) == 0)
19525 *enable = TRUE;
19526 else
19527 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080019528
developera3511852023-06-14 14:12:59 +080019529 wifi_dbg_printf("\n[%s]: proxy_arp is : %s", __func__, output);
19530 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019531}
19532
19533INT wifi_getRadioStatsEnable(INT radioIndex, BOOL *output_enable)
19534{
developera3511852023-06-14 14:12:59 +080019535 if (NULL == output_enable || radioIndex >=MAX_NUM_RADIOS)
19536 return RETURN_ERR;
19537 *output_enable=TRUE;
19538 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019539}
19540
19541INT wifi_getTWTsessions(INT ap_index, UINT maxNumberSessions, wifi_twt_sessions_t *twtSessions, UINT *numSessionReturned)
19542{
developera3511852023-06-14 14:12:59 +080019543 char cmd[128] = {0};
19544 char buf[128] = {0};
19545 char line[128] = {0};
19546 FILE *f = NULL;
19547 int index = 0;
19548 int exp = 0;
19549 int mantissa = 0;
19550 int duration = 0;
19551 int radio_index = 0;
19552 int max_radio_num = 0;
19553 uint twt_wake_interval = 0;
19554 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +080019555 int res;
developerc14d83a2023-06-29 20:09:42 +080019556 unsigned long tmp_u, tmp_l;
developer75bd10c2023-06-27 11:34:08 +080019557
developera3511852023-06-14 14:12:59 +080019558 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019559
developera3511852023-06-14 14:12:59 +080019560 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080019561 if(max_radio_num == 0){
19562 return RETURN_ERR;
19563 }
developera3511852023-06-14 14:12:59 +080019564 radio_index = ap_index % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +080019565
developera3511852023-06-14 14:12:59 +080019566 phyId = radio_index_to_phy(radio_index);
developer75bd10c2023-06-27 11:34:08 +080019567
developer8078acf2023-08-04 18:52:48 +080019568 res = _syscmd_secure(buf, sizeof(buf),"cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | wc -l", phyId);
19569 if (res) {
19570 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
19571
developer75bd10c2023-06-27 11:34:08 +080019572 }
developer8078acf2023-08-04 18:52:48 +080019573
developerc14d83a2023-06-29 20:09:42 +080019574 if (hal_strtoul(buf, 10, &tmp_u) < 0) {
19575 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019576 }
19577 *numSessionReturned = tmp_u - 1;
developer5b23cd02023-07-19 20:26:03 +080019578
developera3511852023-06-14 14:12:59 +080019579 if (*numSessionReturned > maxNumberSessions)
19580 *numSessionReturned = maxNumberSessions;
19581 else if (*numSessionReturned < 1) {
19582 *numSessionReturned = 0;
19583 return RETURN_OK;
19584 }
developer72fb0bb2023-01-11 09:46:29 +080019585
developer75bd10c2023-06-27 11:34:08 +080019586 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | tail -n %d | tr '|' ' ' | tr -s ' '", phyId, *numSessionReturned);
19587 if (os_snprintf_error(sizeof(cmd), res)) {
19588 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19589 return RETURN_ERR;
19590 }
developer8078acf2023-08-04 18:52:48 +080019591 f = v_secure_popen("r", "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | tail -n %d | tr '|' ' ' | tr -s ' '", phyId, *numSessionReturned);
19592 if (f == NULL) {
19593 wifi_dbg_printf("%s: v_secure_popen %s error\n", __func__, cmd);
developera3511852023-06-14 14:12:59 +080019594 return RETURN_ERR;
19595 }
developer72fb0bb2023-01-11 09:46:29 +080019596
developera3511852023-06-14 14:12:59 +080019597 // the format of each line is "[wcid] [id] [flags] [exp] [mantissa] [duration] [tsf]"
19598 while((fgets(line, sizeof(line), f)) != NULL) {
19599 char *tmp = NULL;
developer9ce44382023-06-28 11:09:37 +080019600 size_t len = strlen(line);
19601 strncpy(buf, line,len);
19602 buf[len] = '\0';
developera3511852023-06-14 14:12:59 +080019603 tmp = strtok(buf, " ");
developerd14dff12023-06-28 22:47:44 +080019604 if (tmp == NULL)
19605 break;
developer5b23cd02023-07-19 20:26:03 +080019606
developerc14d83a2023-06-29 20:09:42 +080019607 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19608 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19609 }
19610 twtSessions[index].numDevicesInSession = tmp_u;
developera3511852023-06-14 14:12:59 +080019611 tmp = strtok(NULL, " ");
developerd14dff12023-06-28 22:47:44 +080019612 if (tmp == NULL)
19613 break;
developer5b23cd02023-07-19 20:26:03 +080019614
developerc14d83a2023-06-29 20:09:42 +080019615 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19616 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19617 }
19618 twtSessions[index].twtParameters.operation.flowID = tmp_u;
developera3511852023-06-14 14:12:59 +080019619 tmp = strtok(NULL, " ");
19620 if (strstr(tmp, "t")) {
19621 twtSessions[index].twtParameters.operation.trigger_enabled = TRUE;
19622 }
19623 if (strstr(tmp, "a")) {
19624 twtSessions[index].twtParameters.operation.announced = TRUE;
19625 }
19626 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019627 if (tmp == NULL)
19628 continue;
developer5b23cd02023-07-19 20:26:03 +080019629
developerc14d83a2023-06-29 20:09:42 +080019630 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19631 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19632 }
19633 exp = tmp_l;
developer5b23cd02023-07-19 20:26:03 +080019634
developera3511852023-06-14 14:12:59 +080019635 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019636 if (tmp == NULL)
19637 continue;
developerc14d83a2023-06-29 20:09:42 +080019638 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19639 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019640 }
19641 mantissa = tmp_l;
developer5b23cd02023-07-19 20:26:03 +080019642
developera3511852023-06-14 14:12:59 +080019643 tmp = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +080019644
19645 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19646 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019647 }
19648 duration = tmp_l;
developer72fb0bb2023-01-11 09:46:29 +080019649
developera3511852023-06-14 14:12:59 +080019650 // only implicit supported
19651 twtSessions[index].twtParameters.operation.implicit = TRUE;
19652 // only individual agreement supported
19653 twtSessions[index].twtParameters.agreement = wifi_twt_agreement_type_individual;
developer72fb0bb2023-01-11 09:46:29 +080019654
developera3511852023-06-14 14:12:59 +080019655 // wakeInterval_uSec is a unsigned integer, but the maximum TWT wake interval could be 2^15 (mantissa) * 2^32 = 2^47.
19656 twt_wake_interval = mantissa * (1 << exp);
19657 if (mantissa == 0 || twt_wake_interval/mantissa != (1 << exp)) {
19658 // Overflow handling
19659 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = -1; // max unsigned int
19660 } else {
19661 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = twt_wake_interval;
19662 }
19663 twtSessions[index].twtParameters.params.individual.minWakeDuration_uSec = duration * 256;
19664 index++;
19665 }
developer72fb0bb2023-01-11 09:46:29 +080019666
developer8078acf2023-08-04 18:52:48 +080019667 v_secure_pclose(f);
developera3511852023-06-14 14:12:59 +080019668 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19669 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019670}
developercc5cbfb2023-06-13 18:29:52 +080019671
19672INT wifi_enableGreylistAccessControl(BOOL enable)
19673{
19674 char inf_name[IFNAMSIZ] = {0};
19675 int if_idx, ret = 0;
19676 struct nl_msg *msg = NULL;
19677 struct nlattr * msg_data = NULL;
19678 struct mtk_nl80211_param param;
19679 struct unl unl_ins;
19680 unsigned short apIndex = 0;
19681
19682 for (apIndex = 0; apIndex < MAX_APS; apIndex++) {
19683 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
19684 continue;
19685
19686 if_idx = if_nametoindex(inf_name);
19687 if (!if_idx) {
19688 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
19689 continue;
19690 }
19691
19692 /*init mtk nl80211 vendor cmd*/
19693 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
19694 param.if_type = NL80211_ATTR_IFINDEX;
19695 param.if_idx = if_idx;
19696 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
19697 if (ret) {
19698 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
19699 return RETURN_ERR;
19700 }
19701
19702 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, enable == FALSE ? 0 : 1)) {
19703 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
19704 nlmsg_free(msg);
19705 mtk_nl80211_deint(&unl_ins);
19706 continue;
19707 }
19708
19709 /*send mtk nl80211 vendor msg*/
19710 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
19711 if (ret) {
19712 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
19713 mtk_nl80211_deint(&unl_ins);
19714 continue;
19715 }
19716 /*deinit mtk nl80211 vendor msg*/
19717 mtk_nl80211_deint(&unl_ins);
19718 wifi_debug(DEBUG_NOTICE, " %s cmd success.\n", inf_name);
19719 }
19720
19721 return RETURN_OK;
19722}