blob: d5b85d1998e0ed965ebbebaf2950c9f2117357b9 [file] [log] [blame]
developer72fb0bb2023-01-11 09:46:29 +08001/*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2019 RDK Management
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18*/
19
20/*
21* Material from the TR181 data model is Copyright (c) 2010-2017, Broadband Forum
22* Licensed under the BSD-3 license
23*/
24
25/*
26* This file includes material that is Copyright (c) 2020, Plume Design Inc.
27* Licensed under the BSD-3 license
28*/
29
30/* Code in rxStatsInfo_callback and other callbacks is credited as follows:
developera3511852023-06-14 14:12:59 +080031Copyright (c) 2007, 2008 Johannes Berg
32Copyright (c) 2007 Andy Lutomirski
33Copyright (c) 2007 Mike Kershaw
34Copyright (c) 2008-2009 Luis R. Rodriguez
developer72fb0bb2023-01-11 09:46:29 +080035Licensed under the ISC license
36*/
37#define MTK_IMPL
38#define HAL_NETLINK_IMPL
39#define _GNU_SOURCE /* needed for strcasestr */
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <string.h>
45#include <fcntl.h>
46#include <stdbool.h>
47#include "wifi_hal.h"
48
49#ifdef HAL_NETLINK_IMPL
50#include <errno.h>
51#include <netlink/attr.h>
52#include <netlink/netlink.h>
53#include <netlink/genl/genl.h>
54#include <netlink/genl/family.h>
55#include <netlink/genl/ctrl.h>
56#include <linux/nl80211.h>
developer8dd72532023-05-17 19:58:35 +080057#include <net/if.h>
58#include <unl.h>
59#include "mtk_vendor_nl80211.h"
developer72fb0bb2023-01-11 09:46:29 +080060#endif
61
62#include <ev.h>
63#include <wpa_ctrl.h>
64#include <errno.h>
65#include <time.h>
developercc5cbfb2023-06-13 18:29:52 +080066
67#include <sys/socket.h>
68#include <sys/ioctl.h>
69#include <arpa/inet.h>
70#include <linux/if.h>
71#include <linux/if_bridge.h>
72#include <linux/sockios.h>
developer86035662023-06-28 19:21:12 +080073#include <errno.h>
74#include <limits.h>
developer33f13ba2023-07-12 16:19:06 +080075#include "secure_wrapper.h"
developercc5cbfb2023-06-13 18:29:52 +080076
developer72fb0bb2023-01-11 09:46:29 +080077#define MAC_ALEN 6
78
79#define MAX_BUF_SIZE 256
80#define MAX_CMD_SIZE 256
developerb149d9d2023-06-06 16:14:22 +080081#define MAX_SUB_CMD_SIZE 200
82
developer72fb0bb2023-01-11 09:46:29 +080083#define IF_NAME_SIZE 16
84#define CONFIG_PREFIX "/nvram/hostapd"
85#define ACL_PREFIX "/nvram/hostapd-acl"
86#define DENY_PREFIX "/nvram/hostapd-deny"
87//#define ACL_PREFIX "/tmp/wifi_acl_list" //RDKB convention
88#define SOCK_PREFIX "/var/run/hostapd/wifi"
89#define VAP_STATUS_FILE "/nvram/vap-status"
90#define ESSID_FILE "/tmp/essid"
91#define GUARD_INTERVAL_FILE "/nvram/guard-interval"
92#define CHANNEL_STATS_FILE "/tmp/channel_stats"
93#define DFS_ENABLE_FILE "/nvram/dfs_enable.txt"
94#define VLAN_FILE "/nvram/hostapd.vlan"
95#define PSK_FILE "/nvram/hostapd"
96#define MCS_FILE "/tmp/MCS"
developera1255e42023-05-13 17:45:02 +080097#define POWER_PERCENTAGE "/tmp/POWER"
98#define MGMT_POWER_CTRL "/tmp/mgmt_power_ctrl"
99/*LOGAN_DAT_FILE: may be different on customer's platform.*/
developerc7adba42023-07-14 10:58:45 +0800100#ifdef WIFI_7992
101#define LOGAN_DAT_FILE "/etc/wireless/mediatek/mt7992.b"
102#define ROM_LOGAN_DAT_FILE "/rom/etc/wireless/mediatek/mt7992.b"
103#else
developera1255e42023-05-13 17:45:02 +0800104#define LOGAN_DAT_FILE "/etc/wireless/mediatek/mt7990.b"
developerb2977562023-05-24 17:54:12 +0800105#define ROM_LOGAN_DAT_FILE "/rom/etc/wireless/mediatek/mt7990.b"
developerc7adba42023-07-14 10:58:45 +0800106#endif
developera1255e42023-05-13 17:45:02 +0800107
developer72fb0bb2023-01-11 09:46:29 +0800108#define NOACK_MAP_FILE "/tmp/NoAckMap"
developerfead3972023-05-25 20:15:02 +0800109#define RADIO_RESET_FILE "/nvram/radio_reset"
developerf6a87542023-05-16 15:47:28 +0800110
developer72fb0bb2023-01-11 09:46:29 +0800111#define BRIDGE_NAME "brlan0"
developerd1824452023-05-18 12:30:04 +0800112#define BASE_PHY_INDEX 1
113#define BASE_RADIO_INDEX 0
developer72fb0bb2023-01-11 09:46:29 +0800114
115/*
116 MAX_APS - Number of all AP available in system
117 2x Home AP
118 2x Backhaul AP
119 2x Guest AP
120 2x Secure Onboard AP
121 2x Service AP
122
123*/
124
125
126#define MAX_APS MAX_NUM_RADIOS*5
developer7e4a2a62023-04-06 19:56:03 +0800127
128#define PREFIX_WIFI2G "ra"
129#define PREFIX_WIFI5G "rai"
130#define PREFIX_WIFI6G "rax"
developer72fb0bb2023-01-11 09:46:29 +0800131
developer47cc27a2023-05-17 23:09:58 +0800132#define PREFIX_SSID_2G "RDKB_2G"
133#define PREFIX_SSID_5G "RDKB_5G"
134#define PREFIX_SSID_6G "RDKB_6G"
135
developer72fb0bb2023-01-11 09:46:29 +0800136#ifndef RADIO_PREFIX
137#define RADIO_PREFIX "wlan"
138#endif
139
140#define MAX_ASSOCIATED_STA_NUM 2007
141
142//Uncomment to enable debug logs
143//#define WIFI_DEBUG
developer49b17232023-05-19 16:35:19 +0800144enum {
145 DEBUG_OFF = 0,
146 DEBUG_ERROR = 1,
147 DEBUG_WARN = 2,
148 DEBUG_NOTICE = 3,
149 DEBUG_INFO = 4
150};
151int wifi_debug_level = DEBUG_NOTICE;
152#define wifi_debug(level, fmt, args...) \
153{ \
154 if (level <= wifi_debug_level) \
155 { \
developer2edaf012023-05-24 14:24:53 +0800156 printf("[%s][%d]"fmt"", __func__, __LINE__, ##args); \
developer49b17232023-05-19 16:35:19 +0800157 } \
158}
developer72fb0bb2023-01-11 09:46:29 +0800159
160#ifdef WIFI_DEBUG
161#define wifi_dbg_printf printf
162#define WIFI_ENTRY_EXIT_DEBUG printf
163#else
developer2f79c922023-06-02 17:33:42 +0800164#define wifi_dbg_printf(format, args...)
165#define WIFI_ENTRY_EXIT_DEBUG(format, args...)
developer72fb0bb2023-01-11 09:46:29 +0800166#endif
167
168#define HOSTAPD_CONF_0 "/nvram/hostapd0.conf" //private-wifi-2g
169#define HOSTAPD_CONF_1 "/nvram/hostapd1.conf" //private-wifi-5g
170#define HOSTAPD_CONF_4 "/nvram/hostapd4.conf" //public-wifi-2g
171#define HOSTAPD_CONF_5 "/nvram/hostapd5.conf" //public-wifi-5g
172#define DEF_HOSTAPD_CONF_0 "/usr/ccsp/wifi/hostapd0.conf"
173#define DEF_HOSTAPD_CONF_1 "/usr/ccsp/wifi/hostapd1.conf"
174#define DEF_HOSTAPD_CONF_4 "/usr/ccsp/wifi/hostapd4.conf"
175#define DEF_HOSTAPD_CONF_5 "/usr/ccsp/wifi/hostapd5.conf"
176#define DEF_RADIO_PARAM_CONF "/usr/ccsp/wifi/radio_param_def.cfg"
177#define LM_DHCP_CLIENT_FORMAT "%63d %17s %63s %63s"
178
179#define HOSTAPD_HT_CAPAB "[LDPC][SHORT-GI-20][SHORT-GI-40][MAX-AMSDU-7935]"
180
181#define BW_FNAME "/nvram/bw_file.txt"
182
183#define PS_MAX_TID 16
184
developer96b38512023-02-22 11:17:45 +0800185#define MAX_CARD_INDEX 3
186
developer72fb0bb2023-01-11 09:46:29 +0800187static wifi_radioQueueType_t _tid_ac_index_get[PS_MAX_TID] = {
developera3511852023-06-14 14:12:59 +0800188 WIFI_RADIO_QUEUE_TYPE_BE, /* 0 */
189 WIFI_RADIO_QUEUE_TYPE_BK, /* 1 */
190 WIFI_RADIO_QUEUE_TYPE_BK, /* 2 */
191 WIFI_RADIO_QUEUE_TYPE_BE, /* 3 */
192 WIFI_RADIO_QUEUE_TYPE_VI, /* 4 */
193 WIFI_RADIO_QUEUE_TYPE_VI, /* 5 */
194 WIFI_RADIO_QUEUE_TYPE_VO, /* 6 */
195 WIFI_RADIO_QUEUE_TYPE_VO, /* 7 */
196 WIFI_RADIO_QUEUE_TYPE_BE, /* 8 */
197 WIFI_RADIO_QUEUE_TYPE_BK, /* 9 */
198 WIFI_RADIO_QUEUE_TYPE_BK, /* 10 */
199 WIFI_RADIO_QUEUE_TYPE_BE, /* 11 */
200 WIFI_RADIO_QUEUE_TYPE_VI, /* 12 */
201 WIFI_RADIO_QUEUE_TYPE_VI, /* 13 */
202 WIFI_RADIO_QUEUE_TYPE_VO, /* 14 */
203 WIFI_RADIO_QUEUE_TYPE_VO, /* 15 */
developer72fb0bb2023-01-11 09:46:29 +0800204};
205
206typedef unsigned long long u64;
207
208/* Enum to define WiFi Bands */
209typedef enum
210{
developera3511852023-06-14 14:12:59 +0800211 band_invalid = -1,
212 band_2_4 = 0,
213 band_5 = 1,
214 band_6 = 2,
developer72fb0bb2023-01-11 09:46:29 +0800215} wifi_band;
216
developer17038e62023-03-02 14:43:43 +0800217char* wifi_band_str[] = {
developera3511852023-06-14 14:12:59 +0800218 "2G",
219 "5G",
220 "6G",
developer17038e62023-03-02 14:43:43 +0800221};
222
developer72fb0bb2023-01-11 09:46:29 +0800223typedef enum {
developera3511852023-06-14 14:12:59 +0800224 WIFI_MODE_A = 0x01,
225 WIFI_MODE_B = 0x02,
226 WIFI_MODE_G = 0x04,
227 WIFI_MODE_N = 0x08,
228 WIFI_MODE_AC = 0x10,
229 WIFI_MODE_AX = 0x20,
230 WIFI_MODE_BE = 0x40,
developer72fb0bb2023-01-11 09:46:29 +0800231} wifi_ieee80211_Mode;
232
developer2f79c922023-06-02 17:33:42 +0800233typedef enum {
developera3511852023-06-14 14:12:59 +0800234 HT_BW_20,
235 HT_BW_40,
developer2f79c922023-06-02 17:33:42 +0800236} ht_config_bw;
developerd1824452023-05-18 12:30:04 +0800237
developer2f79c922023-06-02 17:33:42 +0800238typedef enum {
developera3511852023-06-14 14:12:59 +0800239 VHT_BW_2040,
240 VHT_BW_80,
241 VHT_BW_160,
242 VHT_BW_8080,
developer2f79c922023-06-02 17:33:42 +0800243} vht_config_bw;
developerd1824452023-05-18 12:30:04 +0800244
developer2f79c922023-06-02 17:33:42 +0800245typedef enum {
developera3511852023-06-14 14:12:59 +0800246 EHT_BW_20,
247 EHT_BW_40,
248 EHT_BW_80,
249 EHT_BW_160,
250 EHT_BW_320,
developer2f79c922023-06-02 17:33:42 +0800251} eht_config_bw;
developerd1824452023-05-18 12:30:04 +0800252
developer72fb0bb2023-01-11 09:46:29 +0800253#ifdef WIFI_HAL_VERSION_3
254
255// Return number of elements in array
256#ifndef ARRAY_SIZE
developera3511852023-06-14 14:12:59 +0800257#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
developer72fb0bb2023-01-11 09:46:29 +0800258#endif /* ARRAY_SIZE */
259
260#ifndef ARRAY_AND_SIZE
developera3511852023-06-14 14:12:59 +0800261#define ARRAY_AND_SIZE(x) (x),ARRAY_SIZE(x)
developer72fb0bb2023-01-11 09:46:29 +0800262#endif /* ARRAY_AND_SIZE */
263
developera3511852023-06-14 14:12:59 +0800264#define WIFI_ITEM_STR(key, str) {0, sizeof(str)-1, (int)key, (intptr_t)str}
developer72fb0bb2023-01-11 09:46:29 +0800265
266typedef struct {
developera3511852023-06-14 14:12:59 +0800267 int32_t value;
268 int32_t param;
269 intptr_t key;
270 intptr_t data;
developer72fb0bb2023-01-11 09:46:29 +0800271} wifi_secur_list;
272
developer0155a502023-06-19 20:33:57 +0800273typedef struct GNU_PACKED _wdev_extended_ap_metrics {
274 unsigned int uc_tx;
275 unsigned int uc_rx;
276 unsigned int mc_tx;
277 unsigned int mc_rx;
278 unsigned int bc_tx;
279 unsigned int bc_rx;
280} wdev_extended_ap_metric;
281
282typedef struct GNU_PACKED _wdev_ap_metric {
283 unsigned char bssid[6];
284 unsigned char cu;
285 unsigned char ESPI_AC[4][3];
286 wdev_extended_ap_metric ext_ap_metric;
287} wdev_ap_metric;
288
289
developer72fb0bb2023-01-11 09:46:29 +0800290static int util_unii_5g_centerfreq(const char *ht_mode, int channel);
291static int util_unii_6g_centerfreq(const char *ht_mode, int channel);
developera3511852023-06-14 14:12:59 +0800292wifi_secur_list * wifi_get_item_by_key(wifi_secur_list *list, int list_sz, int key);
293wifi_secur_list * wifi_get_item_by_str(wifi_secur_list *list, int list_sz, const char *str);
294char * wifi_get_str_by_key(wifi_secur_list *list, int list_sz, int key);
developer72fb0bb2023-01-11 09:46:29 +0800295static int ieee80211_channel_to_frequency(int channel, int *freqMHz);
developerd14dff12023-06-28 22:47:44 +0800296static void wifi_PrepareDefaultHostapdConfigs(bool reset);
developer47cc27a2023-05-17 23:09:58 +0800297static void wifi_psk_file_reset();
developerb2977562023-05-24 17:54:12 +0800298static void wifi_dat_file_reset_by_radio(char radio_idx);
developer262f4cb2023-05-24 12:22:04 +0800299static int util_get_sec_chan_offset(int channel, const char* ht_mode);
developer2f79c922023-06-02 17:33:42 +0800300int hostapd_raw_add_bss(int apIndex);
301int hostapd_raw_remove_bss(int apIndex);
developerd14dff12023-06-28 22:47:44 +0800302INT wifi_getApDevicesAssociated(INT apIndex, CHAR *macArray, UINT buf_size);
303
developer86035662023-06-28 19:21:12 +0800304static inline int hal_strtol(char *src, int base, long int *out)
305{
306 long int res = 0;
307 char *end_ptr = NULL;
308
309 errno = 0;
310 res = strtol(src, &end_ptr, base);
311
312 if ((errno == ERANGE && (res == LONG_MIN || res == LONG_MAX))
developer5fbf2ff2023-06-30 10:51:56 +0800313 || (errno != 0 && res == 0) || /*ignore end_ptr!=0 error*/ /**end_ptr != '\0' ||*/src == end_ptr ) {
314 *out = res;
developer86035662023-06-28 19:21:12 +0800315 return -1;
developer5fbf2ff2023-06-30 10:51:56 +0800316 } else
developer86035662023-06-28 19:21:12 +0800317 *out = res;
318
319 return 0;
320}
321
322static inline int hal_strtoul(char *src, int base, unsigned long *out)
323{
324 unsigned long res = 0;
325 char *end_ptr = NULL;
326
327 errno = 0;
328 res = strtoul(src, &end_ptr, base);
329
330 if ((errno == ERANGE && res == ULONG_MAX)
developer5fbf2ff2023-06-30 10:51:56 +0800331 || (errno != 0 && res == 0) || /*ignore end_ptr!=0 error*/ /**end_ptr != '\0' ||*/src == end_ptr ) {
332 *out = res;
developer86035662023-06-28 19:21:12 +0800333 return -1;
developer5fbf2ff2023-06-30 10:51:56 +0800334 } else
developer86035662023-06-28 19:21:12 +0800335 *out = res;
336
337 return 0;
338}
developer47cc27a2023-05-17 23:09:58 +0800339
developercc5cbfb2023-06-13 18:29:52 +0800340static inline int os_snprintf_error(size_t size, int res)
341{
342 return res < 0 || (unsigned int) res >= size;
343}
344
developer49b17232023-05-19 16:35:19 +0800345/*type define the nl80211 call back func*/
346typedef int (*mtk_nl80211_cb) (struct nl_msg *, void *);
347
348/**
349*struct mtk_nl80211_param
350* init mtk nl80211 using parameters
351* @sub_cmd: the cmd define in the mtk_vendor_nl80211.h.
352* @if_type: now only support the NL80211_ATTR_IFINDEX/NL80211_ATTR_WIPHY.
353* @if_idx: the index should match the interface or wiphy.
354* Note: NA
355**/
356struct mtk_nl80211_param {
357 unsigned int sub_cmd;
358 int if_type;
359 int if_idx;
360};
361
362/**
developer121a8e72023-05-22 09:19:39 +0800363*struct mtk_nl80211_cb_data
364* init mtk nl80211 call back parameters
365* @out_buf: store the mtk vendor output msg for wifi hal buffer.
366* @out_len: the output buffer length.
367* Note: NA
368**/
369struct mtk_nl80211_cb_data {
370 char * out_buf;
371 unsigned int out_len;
372};
373
374/**
developer49b17232023-05-19 16:35:19 +0800375*mtk_nl80211_init
376* init mtk nl80211 netlink and init the vendor msg common part.
377* @nl: netlink, just init it.
378* @msg: netlink message will alloc it.
379* the msg send success/fails is not free by app
380* only the nla_put etc api fails should use nlmsg_free.
381* @msg_data: vendor data msg attr pointer.
382* @param: init using interface and sub_cmd parameter.
383*
384*init the netlink context and mtk netlink vendor msg.
385*
386*return:
387* 0: success
388* other: fail
389**/
390
391int mtk_nl80211_init(struct unl *nl, struct nl_msg **msg,
392 struct nlattr **msg_data, struct mtk_nl80211_param *param) {
393 /*sanity check here*/
394 if (!nl || !param) {
395 (void)fprintf(stderr,
developerdaf24792023-06-06 11:40:04 +0800396 "[%s][%d]:nl(%p) or param(%p) is null, error!\n",
developer49b17232023-05-19 16:35:19 +0800397 __func__, __LINE__, nl, param);
398 return -1;
399 }
400 /*if_type check*/
401 if ( param->if_type != NL80211_ATTR_IFINDEX && param->if_type != NL80211_ATTR_WIPHY) {
402 (void)fprintf(stderr,
403 "[%s][%d]:if_type(0x%x) is not supported, only 0x%x and 0x%x supported.\n",
404 __func__, __LINE__, param->if_type, NL80211_ATTR_IFINDEX, NL80211_ATTR_WIPHY);
405 return -1;
406 }
407 /*init the nl*/
408 if (unl_genl_init(nl, "nl80211") < 0) {
409 (void)fprintf(stderr, "[%s][%d]::Failed to connect to nl80211\n",
410 __func__, __LINE__);
411 return -1;
412 }
413 /*init the msg*/
414 *msg = unl_genl_msg(nl, NL80211_CMD_VENDOR, false);
415
416 if (nla_put_u32(*msg, param->if_type, param->if_idx) ||
developera3511852023-06-14 14:12:59 +0800417 nla_put_u32(*msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) ||
418 nla_put_u32(*msg, NL80211_ATTR_VENDOR_SUBCMD, param->sub_cmd)) {
developer49b17232023-05-19 16:35:19 +0800419 (void)fprintf(stderr,
420 "[%s][%d]:Nla put error: if_type: 0x%x, if_idx: 0x%x, sub_cmd: 0x%x\n",
421 __func__, __LINE__, param->if_type, param->if_idx, param->sub_cmd);
422 goto err;
423 }
424
425 *msg_data = nla_nest_start(*msg, NL80211_ATTR_VENDOR_DATA);
426 if (!*msg_data) {
427 (void)fprintf(stderr, "[%s][%d]:Nla put NL80211_ATTR_VENDOR_DATA start error\n",
428 __func__, __LINE__);
429 goto err;
430 }
431
432 return 0;
433err:
developer49b17232023-05-19 16:35:19 +0800434 nlmsg_free(*msg);
435 unl_free(nl);
436 return -1;
437}
438
439/**
440*mtk_nl80211_send
441* set the vendor cmd call back and sent the vendor msg.
442* @nl: netlink.
443* @msg: netlink message.
444* @msg_data: vendor data msg attr pointer.
445* @handler: if the msg have call back shoud add the call back func
446* the event msg will handle by the call back func(exp:get cmd)
447* other set it as NULL(exp:set cmd).
448* @arg:call back func arg parameter.
449*add end of the netlink msg, set the call back and send msg
450*
451*return:
452* 0: success
453* other: fail
454**/
455int mtk_nl80211_send(struct unl *nl, struct nl_msg *msg,
456 struct nlattr *msg_data, mtk_nl80211_cb handler, void *arg) {
457 int ret = 0;
458 /*sanity check*/
459 if (!nl || !msg || !msg_data) {
460 (void)fprintf(stderr,
developerdaf24792023-06-06 11:40:04 +0800461 "[%s][%d]:nl(%p),msg(%p) or msg_data(%p) is null, error!\n",
developer49b17232023-05-19 16:35:19 +0800462 __func__, __LINE__, nl, msg, msg_data);
463 return -1;
464 }
465 /*end the msg attr of vendor data*/
466 nla_nest_end(msg, msg_data);
467 /*send the msg and set call back */
468 ret = unl_genl_request(nl, msg, handler, arg);
469 if (ret)
470 (void)fprintf(stderr, "send nl80211 cmd fails\n");
471 return ret;
472}
473
474/**
475*mtk_nl80211_deint
developer2edaf012023-05-24 14:24:53 +0800476* deinit the netlink.
developer49b17232023-05-19 16:35:19 +0800477* @nl: netlink.
478*
developer2edaf012023-05-24 14:24:53 +0800479*free deinit the netlink.
developer49b17232023-05-19 16:35:19 +0800480*
481*return:
482* 0: success
483**/
484
485int mtk_nl80211_deint(struct unl *nl) {
486 unl_free(nl);
487 return 0;
488}
489
developer72fb0bb2023-01-11 09:46:29 +0800490wifi_secur_list * wifi_get_item_by_key(wifi_secur_list *list, int list_sz, int key)
491{
developera3511852023-06-14 14:12:59 +0800492 wifi_secur_list *item;
493 int i;
developer72fb0bb2023-01-11 09:46:29 +0800494
developera3511852023-06-14 14:12:59 +0800495 for (item = list,i = 0;i < list_sz; item++, i++) {
496 if ((int)(item->key) == key) {
497 return item;
498 }
499 }
developer72fb0bb2023-01-11 09:46:29 +0800500
developera3511852023-06-14 14:12:59 +0800501 return NULL;
developer72fb0bb2023-01-11 09:46:29 +0800502}
503
504char * wifi_get_str_by_key(wifi_secur_list *list, int list_sz, int key)
505{
developera3511852023-06-14 14:12:59 +0800506 wifi_secur_list *item = wifi_get_item_by_key(list, list_sz, key);
developer72fb0bb2023-01-11 09:46:29 +0800507
developera3511852023-06-14 14:12:59 +0800508 if (!item) {
509 return "";
510 }
developer72fb0bb2023-01-11 09:46:29 +0800511
developera3511852023-06-14 14:12:59 +0800512 return (char *)(item->data);
developer72fb0bb2023-01-11 09:46:29 +0800513}
514
515wifi_secur_list * wifi_get_item_by_str(wifi_secur_list *list, int list_sz, const char *str)
516{
developera3511852023-06-14 14:12:59 +0800517 wifi_secur_list *item;
518 int i;
developer72fb0bb2023-01-11 09:46:29 +0800519
developera3511852023-06-14 14:12:59 +0800520 for (item = list,i = 0;i < list_sz; item++, i++) {
521 if (strcmp((char *)(item->data), str) == 0) {
522 return item;
523 }
524 }
developer72fb0bb2023-01-11 09:46:29 +0800525
developera3511852023-06-14 14:12:59 +0800526 return NULL;
developer72fb0bb2023-01-11 09:46:29 +0800527}
528#endif /* WIFI_HAL_VERSION_3 */
529
developer96b38512023-02-22 11:17:45 +0800530
531static char l1profile[32] = "/etc/wireless/l1profile.dat";
developer17038e62023-03-02 14:43:43 +0800532char main_prefix[MAX_NUM_RADIOS][IFNAMSIZ];
533char ext_prefix[MAX_NUM_RADIOS][IFNAMSIZ];
534#define MAX_SSID_LEN 64
535char default_ssid[MAX_NUM_RADIOS][MAX_SSID_LEN];;
developer745f0bd2023-03-06 14:32:53 +0800536int radio_band[MAX_NUM_RADIOS];
developer17038e62023-03-02 14:43:43 +0800537
538static int array_index_to_vap_index(UINT radioIndex, int arrayIndex);
539static int vap_index_to_array_index(int vapIndex, int *radioIndex, int *arrayIndex);
540
developer96b38512023-02-22 11:17:45 +0800541
542static int
543get_value(const char *conf_file, const char *param, char *value, int len)
544{
developera3511852023-06-14 14:12:59 +0800545 FILE *fp;
546 int ret = -1;
547 int param_len = strlen(param);
548 int buf_len;
developer86035662023-06-28 19:21:12 +0800549 char buf[256] = {0};
developer96b38512023-02-22 11:17:45 +0800550
developera3511852023-06-14 14:12:59 +0800551 fp = fopen(conf_file, "r");
552 if (!fp) {
553 return -1;
554 }
developer96b38512023-02-22 11:17:45 +0800555
developera3511852023-06-14 14:12:59 +0800556 while (fgets(buf, sizeof(buf), fp)) {
557 buf_len = strlen(buf);
developer86035662023-06-28 19:21:12 +0800558 if (buf_len == 0) {
559 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +0800560 if (fclose(fp) != 0) {
561 wifi_debug(DEBUG_ERROR, "fclose fail\n");
562 }
developer86035662023-06-28 19:21:12 +0800563 return RETURN_ERR;
564 }
developera3511852023-06-14 14:12:59 +0800565 if (buf[buf_len - 1] == '\n') {
566 buf_len--;
567 buf[buf_len] = '\0';
568 }
569 if ((buf_len > param_len) &&
570 (strncmp(buf, param, param_len) == 0) &&
571 (buf[param_len] == '=')) {
developer96b38512023-02-22 11:17:45 +0800572
developera3511852023-06-14 14:12:59 +0800573 if (buf_len == (param_len + 1)) {
574 value[0] = '\0';
575 ret = 0;
576 } else {
577 ret = snprintf(value, len, "%s", buf + (param_len + 1));
developer75bd10c2023-06-27 11:34:08 +0800578 if (os_snprintf_error(len, ret)) {
579 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
580 }
developera3511852023-06-14 14:12:59 +0800581 }
developerd14dff12023-06-28 22:47:44 +0800582 if (fclose(fp) != 0) {
583 ret = -1;
584 wifi_debug(DEBUG_ERROR, "fclose fail\n");
585 }
developera3511852023-06-14 14:12:59 +0800586 return ret;
587 }
588 }
developer37646972023-06-29 10:58:43 +0800589 if (fclose(fp) == EOF){
590 wifi_debug(DEBUG_ERROR, "fclose fail\n");
591 return RETURN_ERR;
592 }
developera3511852023-06-14 14:12:59 +0800593 return -1;
developer96b38512023-02-22 11:17:45 +0800594}
595
596static int
597get_value_by_idx(const char *conf_file, const char *param, int idx, char *value, int len)
598{
developera3511852023-06-14 14:12:59 +0800599 char buf[256];
600 int ret;
601 char *save_ptr = NULL;
602 char *tok = NULL;
developer96b38512023-02-22 11:17:45 +0800603
developera3511852023-06-14 14:12:59 +0800604 ret = get_value(conf_file, param, buf, sizeof(buf));
605 if (ret < 0)
606 return ret;
developer96b38512023-02-22 11:17:45 +0800607
developera3511852023-06-14 14:12:59 +0800608 tok = strtok_r(buf, ";", &save_ptr);
609 do {
610 if (idx == 0 || tok == NULL)
611 break;
612 else
613 idx--;
developer96b38512023-02-22 11:17:45 +0800614
developera3511852023-06-14 14:12:59 +0800615 tok = strtok_r(NULL, ";", &save_ptr);
616 } while (tok != NULL);
developer96b38512023-02-22 11:17:45 +0800617
developera3511852023-06-14 14:12:59 +0800618 if (tok) {
619 ret = snprintf(value, len, "%s", tok);
developer75bd10c2023-06-27 11:34:08 +0800620 if (os_snprintf_error(len, ret)) {
621 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
622 return -1;
623 }
developera3511852023-06-14 14:12:59 +0800624 } else {
625 ret = 0;
626 value[0] = '\0';
627 }
developer96b38512023-02-22 11:17:45 +0800628
developera3511852023-06-14 14:12:59 +0800629 return ret;
developer96b38512023-02-22 11:17:45 +0800630}
631
632
developer72fb0bb2023-01-11 09:46:29 +0800633#ifdef HAL_NETLINK_IMPL
634typedef struct {
developera3511852023-06-14 14:12:59 +0800635 int id;
636 struct nl_sock* socket;
637 struct nl_cb* cb;
developer72fb0bb2023-01-11 09:46:29 +0800638} Netlink;
639
640static int mac_addr_aton(unsigned char *mac_addr, char *arg)
641{
developera3511852023-06-14 14:12:59 +0800642 unsigned char mac_addr_int[6]={};
developer75bd10c2023-06-27 11:34:08 +0800643 unsigned int recv;
644
645 recv = sscanf(arg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac_addr_int+0, mac_addr_int+1, mac_addr_int+2, mac_addr_int+3, mac_addr_int+4, mac_addr_int+5);
646
647 if (recv != 6) {
648 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
649 return -1;
650 }
developera3511852023-06-14 14:12:59 +0800651 mac_addr[0] = mac_addr_int[0];
652 mac_addr[1] = mac_addr_int[1];
653 mac_addr[2] = mac_addr_int[2];
654 mac_addr[3] = mac_addr_int[3];
655 mac_addr[4] = mac_addr_int[4];
656 mac_addr[5] = mac_addr_int[5];
657 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800658}
659
660static void mac_addr_ntoa(char *mac_addr, unsigned char *arg)
661{
developera3511852023-06-14 14:12:59 +0800662 unsigned int mac_addr_int[6]={};
developere40952c2023-06-15 18:46:43 +0800663 int res;
664
developera3511852023-06-14 14:12:59 +0800665 mac_addr_int[0] = arg[0];
666 mac_addr_int[1] = arg[1];
667 mac_addr_int[2] = arg[2];
668 mac_addr_int[3] = arg[3];
669 mac_addr_int[4] = arg[4];
670 mac_addr_int[5] = arg[5];
developere40952c2023-06-15 18:46:43 +0800671 res = snprintf(mac_addr, 20, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac_addr_int[0], mac_addr_int[1],mac_addr_int[2],mac_addr_int[3],mac_addr_int[4],mac_addr_int[5]);
672 if (os_snprintf_error(20, res)) {
673 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
674 }
developera3511852023-06-14 14:12:59 +0800675 return;
developer72fb0bb2023-01-11 09:46:29 +0800676}
677
678static int ieee80211_frequency_to_channel(int freq)
679{
developera3511852023-06-14 14:12:59 +0800680 /* see 802.11-2007 17.3.8.3.2 and Annex J */
681 if (freq == 2484)
682 return 14;
683 /* see 802.11ax D6.1 27.3.23.2 and Annex E */
684 else if (freq == 5935)
685 return 2;
686 else if (freq < 2484)
687 return (freq - 2407) / 5;
688 else if (freq >= 4910 && freq <= 4980)
689 return (freq - 4000) / 5;
690 else if (freq < 5950)
691 return (freq - 5000) / 5;
692 else if (freq <= 45000) /* DMG band lower limit */
693 /* see 802.11ax D6.1 27.3.23.2 */
694 return (freq - 5950) / 5;
695 else if (freq >= 58320 && freq <= 70200)
696 return (freq - 56160) / 2160;
697 else
698 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800699}
700
701static int initSock80211(Netlink* nl) {
developera3511852023-06-14 14:12:59 +0800702 nl->socket = nl_socket_alloc();
703 if (!nl->socket) {
developer75bd10c2023-06-27 11:34:08 +0800704 wifi_debug(DEBUG_ERROR, "Failing to allocate the sock\n");
developera3511852023-06-14 14:12:59 +0800705 return -ENOMEM;
706 }
developer72fb0bb2023-01-11 09:46:29 +0800707
developera3511852023-06-14 14:12:59 +0800708 nl_socket_set_buffer_size(nl->socket, 8192, 8192);
developer72fb0bb2023-01-11 09:46:29 +0800709
developera3511852023-06-14 14:12:59 +0800710 if (genl_connect(nl->socket)) {
developer75bd10c2023-06-27 11:34:08 +0800711 wifi_debug(DEBUG_ERROR, "Failed to connect\n");
developera3511852023-06-14 14:12:59 +0800712 nl_close(nl->socket);
713 nl_socket_free(nl->socket);
714 return -ENOLINK;
715 }
developer72fb0bb2023-01-11 09:46:29 +0800716
developera3511852023-06-14 14:12:59 +0800717 nl->id = genl_ctrl_resolve(nl->socket, "nl80211");
718 if (nl->id< 0) {
developer75bd10c2023-06-27 11:34:08 +0800719 wifi_debug(DEBUG_ERROR, "interface not found.\n");
developera3511852023-06-14 14:12:59 +0800720 nl_close(nl->socket);
721 nl_socket_free(nl->socket);
722 return -ENOENT;
723 }
developer72fb0bb2023-01-11 09:46:29 +0800724
developera3511852023-06-14 14:12:59 +0800725 nl->cb = nl_cb_alloc(NL_CB_DEFAULT);
726 if ((!nl->cb)) {
developer75bd10c2023-06-27 11:34:08 +0800727 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink callback.\n");
developera3511852023-06-14 14:12:59 +0800728 nl_close(nl->socket);
729 nl_socket_free(nl->socket);
730 return ENOMEM;
731 }
developer72fb0bb2023-01-11 09:46:29 +0800732
developera3511852023-06-14 14:12:59 +0800733 return nl->id;
developer72fb0bb2023-01-11 09:46:29 +0800734}
735
736static int nlfree(Netlink *nl)
737{
developera3511852023-06-14 14:12:59 +0800738 nl_cb_put(nl->cb);
739 nl_close(nl->socket);
740 nl_socket_free(nl->socket);
741 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800742}
743
744static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
developera3511852023-06-14 14:12:59 +0800745 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
746 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
747 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED }
developer72fb0bb2023-01-11 09:46:29 +0800748};
749
750static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
751};
752
753static struct nla_policy tid_policy[NL80211_TID_STATS_MAX + 1] = {
754};
755
756typedef struct _wifi_channelStats_loc {
developera3511852023-06-14 14:12:59 +0800757 INT array_size;
758 INT ch_number;
759 BOOL ch_in_pool;
760 INT ch_noise;
761 BOOL ch_radar_noise;
762 INT ch_max_80211_rssi;
763 INT ch_non_80211_noise;
764 INT ch_utilization;
765 ULLONG ch_utilization_total;
766 ULLONG ch_utilization_busy;
767 ULLONG ch_utilization_busy_tx;
768 ULLONG ch_utilization_busy_rx;
769 ULLONG ch_utilization_busy_self;
770 ULLONG ch_utilization_busy_ext;
developer72fb0bb2023-01-11 09:46:29 +0800771} wifi_channelStats_t_loc;
772
773typedef struct wifi_device_info {
developera3511852023-06-14 14:12:59 +0800774 INT wifi_devIndex;
775 UCHAR wifi_devMacAddress[6];
776 CHAR wifi_devIPAddress[64];
777 BOOL wifi_devAssociatedDeviceAuthentiationState;
778 INT wifi_devSignalStrength;
779 INT wifi_devTxRate;
780 INT wifi_devRxRate;
developer72fb0bb2023-01-11 09:46:29 +0800781} wifi_device_info_t;
782
783#endif
784
785//For 5g Alias Interfaces
developer72fb0bb2023-01-11 09:46:29 +0800786static BOOL Radio_flag = TRUE;
787//wifi_setApBeaconRate(1, beaconRate);
788
789BOOL multiple_set = FALSE;
790
791struct params
792{
developera3511852023-06-14 14:12:59 +0800793 char * name;
794 char * value;
developer72fb0bb2023-01-11 09:46:29 +0800795};
796
developer33f13ba2023-07-12 16:19:06 +0800797#define _syscmd_secure(retBuf, retBufSize, fmt, args...) \
798 ({ \
799 FILE *f; \
800 char *ptr = retBuf; \
801 int bufSize = retBufSize, bufbytes = 0, readbytes = 0, cmd_ret = -1; \
802 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__); \
803 f = v_secure_popen("r", fmt, ##args); \
804 if(f) { \
805 while(!feof(f)) \
806 { \
807 *ptr = 0; \
808 if(bufSize>=128) { \
809 bufbytes=128; \
810 } else { \
811 bufbytes=bufSize-1; \
812 } \
813 if (fgets(ptr,bufbytes,f) == NULL) \
814 break; \
815 readbytes=strlen(ptr); \
816 if(!readbytes) \
817 break; \
818 bufSize-=readbytes; \
819 ptr += readbytes; \
820 } \
821 cmd_ret = v_secure_pclose(f); \
822 retBuf[retBufSize-1]=0; \
823 } \
824 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__); \
825 cmd_ret; \
826 })
827
828
developer72fb0bb2023-01-11 09:46:29 +0800829static int _syscmd(char *cmd, char *retBuf, int retBufSize)
830{
developera3511852023-06-14 14:12:59 +0800831 FILE *f;
832 char *ptr = retBuf;
833 int bufSize=retBufSize, bufbytes=0, readbytes=0, cmd_ret=0;
developer72fb0bb2023-01-11 09:46:29 +0800834
developera3511852023-06-14 14:12:59 +0800835 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
836 if((f = popen(cmd, "r")) == NULL) {
developer75bd10c2023-06-27 11:34:08 +0800837 wifi_debug(DEBUG_ERROR, "\npopen %s error\n", cmd);
developera3511852023-06-14 14:12:59 +0800838 return RETURN_ERR;
839 }
developer72fb0bb2023-01-11 09:46:29 +0800840
developera3511852023-06-14 14:12:59 +0800841 while(!feof(f))
842 {
843 *ptr = 0;
844 if(bufSize>=128) {
845 bufbytes=128;
846 } else {
847 bufbytes=bufSize-1;
848 }
developer72fb0bb2023-01-11 09:46:29 +0800849
developerd14dff12023-06-28 22:47:44 +0800850 if (fgets(ptr,bufbytes,f) == NULL)
851 break;
developera3511852023-06-14 14:12:59 +0800852 readbytes=strlen(ptr);
developer72fb0bb2023-01-11 09:46:29 +0800853
developera3511852023-06-14 14:12:59 +0800854 if(!readbytes)
855 break;
developer72fb0bb2023-01-11 09:46:29 +0800856
developera3511852023-06-14 14:12:59 +0800857 bufSize-=readbytes;
858 ptr += readbytes;
859 }
860 cmd_ret = pclose(f);
861 retBuf[retBufSize-1]=0;
862 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800863
developera3511852023-06-14 14:12:59 +0800864 return cmd_ret >> 8;
developer72fb0bb2023-01-11 09:46:29 +0800865}
866
867INT radio_index_to_phy(int radioIndex)
868{
developera3511852023-06-14 14:12:59 +0800869 /* TODO */
870 return radioIndex;
developer72fb0bb2023-01-11 09:46:29 +0800871}
872
873INT wifi_getMaxRadioNumber(INT *max_radio_num)
874{
developera3511852023-06-14 14:12:59 +0800875 char buf[4] = {0};
developere40952c2023-06-15 18:46:43 +0800876 int res;
developerc14d83a2023-06-29 20:09:42 +0800877 unsigned long tmp;
developera3511852023-06-14 14:12:59 +0800878 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800879
developer33f13ba2023-07-12 16:19:06 +0800880 res = _syscmd_secure(buf, sizeof(buf), "iw list | grep Wiphy | wc -l | tr -d '\\n'");
881 if (res) {
882 wifi_debug(DEBUG_ERROR, "_syscmd_secure fail\n");
developere40952c2023-06-15 18:46:43 +0800883 }
developerc14d83a2023-06-29 20:09:42 +0800884 if (hal_strtoul(buf, 10, &tmp) < 0) {
885 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +0800886 }
developerc14d83a2023-06-29 20:09:42 +0800887 res = tmp;
developerd14dff12023-06-28 22:47:44 +0800888
889 *max_radio_num = res > MAX_NUM_RADIOS ? MAX_NUM_RADIOS:res;
developer72fb0bb2023-01-11 09:46:29 +0800890
developera3511852023-06-14 14:12:59 +0800891 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +0800892
developera3511852023-06-14 14:12:59 +0800893 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +0800894}
895
developer17038e62023-03-02 14:43:43 +0800896wifi_band radio_index_to_band(int radioIndex)
897{
developera3511852023-06-14 14:12:59 +0800898 return radio_band[radioIndex];
developer17038e62023-03-02 14:43:43 +0800899}
900
developer72fb0bb2023-01-11 09:46:29 +0800901wifi_band wifi_index_to_band(int apIndex)
902{
developera3511852023-06-14 14:12:59 +0800903 char buf[64] = {0};
developerc14d83a2023-06-29 20:09:42 +0800904 long int nl80211_band = 0;
developera3511852023-06-14 14:12:59 +0800905 int i = 0;
906 int phyIndex = 0;
907 int radioIndex = 0;
908 int max_radio_num = 0;
909 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +0800910 int res;
developer72fb0bb2023-01-11 09:46:29 +0800911
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{
developer7e4a2a62023-04-06 19:56:03 +0800951 char cmd[MAX_CMD_SIZE] = {0};
952 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +0800953 int res = 0;
developer72fb0bb2023-01-11 09:46:29 +0800954
developere40952c2023-06-15 18:46:43 +0800955 res = snprintf(cmd, MAX_CMD_SIZE, "cat %s 2> /dev/null | grep \"^%s=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developer7e4a2a62023-04-06 19:56:03 +0800956 conf_file, param);
developer72fb0bb2023-01-11 09:46:29 +0800957
developere40952c2023-06-15 18:46:43 +0800958 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
959 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
960 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +0800961 }
developerb758dfd2023-06-21 17:32:07 +0800962
developere40952c2023-06-15 18:46:43 +0800963 res = _syscmd(cmd, buf, sizeof(buf));
964 if ((res != 0) && (strlen(buf) == 0)) {
developer7e4a2a62023-04-06 19:56:03 +0800965 printf("%s: _syscmd error!", __func__);
966 return -1;
967 }
968
developere40952c2023-06-15 18:46:43 +0800969 res = snprintf(output, output_size, "%s", buf);
970 if (os_snprintf_error(output_size, res)) {
971 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
972 return RETURN_ERR;
973 }
developer7e4a2a62023-04-06 19:56:03 +0800974
975 return 0;
developer72fb0bb2023-01-11 09:46:29 +0800976}
977
978static int wifi_hostapdWrite(char *conf_file, struct params *list, int item_count)
979{
developera3511852023-06-14 14:12:59 +0800980 char cmd[MAX_CMD_SIZE] = {0};
981 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +0800982 int res;
developer72fb0bb2023-01-11 09:46:29 +0800983
developera3511852023-06-14 14:12:59 +0800984 for (int i = 0; i < item_count; i++) {
985 wifi_hostapdRead(conf_file, list[i].name, buf, sizeof(buf));
986 if (strlen(buf) == 0) /*no such item, insert it*/
developere40952c2023-06-15 18:46:43 +0800987 res = snprintf(cmd, sizeof(cmd), "sed -i -e '$a %s=%s' %s", list[i].name, list[i].value, conf_file);
developera3511852023-06-14 14:12:59 +0800988 else /*find the item, update it*/
developere40952c2023-06-15 18:46:43 +0800989 res = snprintf(cmd, sizeof(cmd), "sed -i \"s/^%s=.*/%s=%s/\" %s", list[i].name, list[i].name, list[i].value, conf_file);
developera1255e42023-05-13 17:45:02 +0800990
developere40952c2023-06-15 18:46:43 +0800991 if (os_snprintf_error(sizeof(cmd), res)) {
992 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
993 return RETURN_ERR;
994 }
developera3511852023-06-14 14:12:59 +0800995 if(_syscmd(cmd, buf, sizeof(buf)))
996 return -1;
997 }
developer72fb0bb2023-01-11 09:46:29 +0800998
developera3511852023-06-14 14:12:59 +0800999 return 0;
developera1255e42023-05-13 17:45:02 +08001000}
developerfde01262023-05-22 15:15:24 +08001001
developera1255e42023-05-13 17:45:02 +08001002static int wifi_datfileRead(char *conf_file, char *param, char *output, int output_size)
1003{
developera3511852023-06-14 14:12:59 +08001004 char cmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001005 int res = 0;
developera3511852023-06-14 14:12:59 +08001006 int len;
developerfde01262023-05-22 15:15:24 +08001007
developere40952c2023-06-15 18:46:43 +08001008 res = snprintf(cmd, sizeof(cmd), "datconf -f %s get %s", conf_file, param);
1009 if (os_snprintf_error(sizeof(cmd), res)) {
1010 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1011 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08001012 }
developerfde01262023-05-22 15:15:24 +08001013
developere40952c2023-06-15 18:46:43 +08001014
1015 res = _syscmd(cmd, output, output_size);
1016 if ((res != 0) && (strlen(output) == 0)) {
developera3511852023-06-14 14:12:59 +08001017 printf("%s: _syscmd error!", __func__);
1018 return -1;
1019 }
developera1255e42023-05-13 17:45:02 +08001020
developera3511852023-06-14 14:12:59 +08001021 len = strlen(output);
1022 if ((len > 0) && (output[len - 1] == '\n')) {
1023 output[len - 1] = '\0';
1024 }
developerfde01262023-05-22 15:15:24 +08001025
developera3511852023-06-14 14:12:59 +08001026 return 0;
developerfde01262023-05-22 15:15:24 +08001027}
developera1255e42023-05-13 17:45:02 +08001028
developera1255e42023-05-13 17:45:02 +08001029static int wifi_datfileWrite(char *conf_file, struct params *list, int item_count)
1030{
developere40952c2023-06-15 18:46:43 +08001031 int res;
developera3511852023-06-14 14:12:59 +08001032 char cmd[MAX_CMD_SIZE] = {0};
1033 char buf[MAX_BUF_SIZE] = {0};
developera1255e42023-05-13 17:45:02 +08001034
developera3511852023-06-14 14:12:59 +08001035 for (int i = 0; i < item_count; i++) {
developere40952c2023-06-15 18:46:43 +08001036 res = snprintf(cmd, sizeof(cmd), "datconf -f %s set %s \"%s\"", conf_file, list[i].name, list[i].value);
1037 if (os_snprintf_error(sizeof(cmd), res)) {
1038 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1039 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08001040 }
developera1255e42023-05-13 17:45:02 +08001041
developera3511852023-06-14 14:12:59 +08001042 if(_syscmd(cmd, buf, sizeof(buf)))
1043 return -1;
1044 }
developera1255e42023-05-13 17:45:02 +08001045
developera3511852023-06-14 14:12:59 +08001046 return 0;
developera1255e42023-05-13 17:45:02 +08001047}
1048
developerfde01262023-05-22 15:15:24 +08001049static int wifi_l1ProfileRead(char *param, char *output, int output_size)
1050{
developera3511852023-06-14 14:12:59 +08001051 int ret;
developerfde01262023-05-22 15:15:24 +08001052
developera3511852023-06-14 14:12:59 +08001053 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1054 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001055 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001056 return RETURN_ERR;
1057 }
developerfde01262023-05-22 15:15:24 +08001058
developera3511852023-06-14 14:12:59 +08001059 ret = wifi_datfileRead(l1profile, param, output, output_size);
1060 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08001061 wifi_debug(DEBUG_ERROR, "wifi_datfileRead %s from %s failed, ret:%d", param, l1profile, ret);
developera3511852023-06-14 14:12:59 +08001062 return RETURN_ERR;
1063 }
developerfde01262023-05-22 15:15:24 +08001064
developera3511852023-06-14 14:12:59 +08001065 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1066 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001067}
1068
1069static int wifi_CardProfileRead(int card_idx, char *param, char *output, int output_size)
1070{
developera3511852023-06-14 14:12:59 +08001071 char option[64];
1072 char card_profile_path[64];
developere40952c2023-06-15 18:46:43 +08001073 int res;
developerfde01262023-05-22 15:15:24 +08001074
developera3511852023-06-14 14:12:59 +08001075 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developerfde01262023-05-22 15:15:24 +08001076
developera3511852023-06-14 14:12:59 +08001077 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001078 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001079 return RETURN_ERR;
1080 }
developerfde01262023-05-22 15:15:24 +08001081
developere40952c2023-06-15 18:46:43 +08001082 res = snprintf(option, sizeof(option), "INDEX%d_profile_path", card_idx);
1083 if (os_snprintf_error(sizeof(option), res)) {
1084 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developera3511852023-06-14 14:12:59 +08001085 return RETURN_ERR;
1086 }
developere40952c2023-06-15 18:46:43 +08001087 res = wifi_l1ProfileRead(option, card_profile_path, sizeof(card_profile_path));
1088 if (res != 0) {
developer75bd10c2023-06-27 11:34:08 +08001089 wifi_debug(DEBUG_ERROR, "wifi_l1ProfileRead %s failed, ret:%d", option, res);
developere40952c2023-06-15 18:46:43 +08001090 return RETURN_ERR;
1091 }
developerfde01262023-05-22 15:15:24 +08001092
developere40952c2023-06-15 18:46:43 +08001093 res = wifi_datfileRead(card_profile_path, param, output, output_size);
1094 if (res != 0) {
developer75bd10c2023-06-27 11:34:08 +08001095 wifi_debug(DEBUG_ERROR, "wifi_datfileRead %s from %s failed, ret:%d", param, card_profile_path, res);
developera3511852023-06-14 14:12:59 +08001096 return RETURN_ERR;
1097 }
developerfde01262023-05-22 15:15:24 +08001098
developera3511852023-06-14 14:12:59 +08001099 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1100 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001101}
1102
1103static int wifi_BandProfileRead(int card_idx,
developera3511852023-06-14 14:12:59 +08001104 int radio_idx,
1105 char *param,
1106 char *output,
1107 int output_size,
1108 char *default_value)
developerfde01262023-05-22 15:15:24 +08001109{
developera3511852023-06-14 14:12:59 +08001110 char option[64];
1111 char band_profile_path[64];
developere40952c2023-06-15 18:46:43 +08001112 int ret, res;
developerfde01262023-05-22 15:15:24 +08001113
developera3511852023-06-14 14:12:59 +08001114 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1115 if (!param || !output || (output_size <= 0)) {
developer75bd10c2023-06-27 11:34:08 +08001116 wifi_debug(DEBUG_ERROR, "invalid parameters");
developera3511852023-06-14 14:12:59 +08001117 return RETURN_ERR;
1118 }
developerfde01262023-05-22 15:15:24 +08001119
developere40952c2023-06-15 18:46:43 +08001120 res = snprintf(option, sizeof(option), "BN%d_profile_path", radio_idx);
1121 if (os_snprintf_error(sizeof(option), res)) {
1122 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1123 return RETURN_ERR;
1124 }
developera3511852023-06-14 14:12:59 +08001125 ret = wifi_CardProfileRead(card_idx, option, band_profile_path, sizeof(band_profile_path));
1126 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08001127 wifi_debug(DEBUG_ERROR, "wifi_CardProfileRead %s failed, ret:%d", option, ret);
developera3511852023-06-14 14:12:59 +08001128 return RETURN_ERR;
1129 }
developerfde01262023-05-22 15:15:24 +08001130
developera3511852023-06-14 14:12:59 +08001131 ret = wifi_datfileRead(band_profile_path, param, output, output_size);
1132 if (ret != 0) {
1133 if (default_value) {
developere40952c2023-06-15 18:46:43 +08001134 res = snprintf(output, output_size, "%s", default_value);
1135 if (os_snprintf_error(output_size, res)) {
1136 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1137 return RETURN_ERR;
1138 }
developera3511852023-06-14 14:12:59 +08001139 } else {
1140 output[0] = '\0';
1141 }
1142 }
developerfde01262023-05-22 15:15:24 +08001143
developera3511852023-06-14 14:12:59 +08001144 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1145 return RETURN_OK;
developerfde01262023-05-22 15:15:24 +08001146}
1147
developer72fb0bb2023-01-11 09:46:29 +08001148//For Getting Current Interface Name from corresponding hostapd configuration
1149static int wifi_GetInterfaceName(int apIndex, char *interface_name)
1150{
developera3511852023-06-14 14:12:59 +08001151 char config_file[128] = {0};
developere40952c2023-06-15 18:46:43 +08001152 int res;
developer72fb0bb2023-01-11 09:46:29 +08001153
developera3511852023-06-14 14:12:59 +08001154 if (interface_name == NULL)
1155 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001156
developera3511852023-06-14 14:12:59 +08001157 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001158
developere40952c2023-06-15 18:46:43 +08001159 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
1160 if (os_snprintf_error(sizeof(config_file), res)) {
1161 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1162 return RETURN_ERR;
1163 }
developera3511852023-06-14 14:12:59 +08001164 wifi_hostapdRead(config_file, "interface", interface_name, 16);
1165 if (strlen(interface_name) == 0)
1166 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001167
developera3511852023-06-14 14:12:59 +08001168 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1169 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001170}
1171
developera1255e42023-05-13 17:45:02 +08001172static UCHAR get_bssnum_byindex(INT radio_index, UCHAR *bss_cnt)
1173{
developera3511852023-06-14 14:12:59 +08001174 char interface_name[IF_NAME_SIZE] = {0};
1175 char cmd[MAX_BUF_SIZE]={'\0'};
1176 char buf[MAX_CMD_SIZE]={'\0'};
1177 UCHAR channel = 0;
developere40952c2023-06-15 18:46:43 +08001178 int res;
developera1255e42023-05-13 17:45:02 +08001179
developera3511852023-06-14 14:12:59 +08001180 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
1181 return RETURN_ERR;
1182 /*interface name to channel number*/
developere40952c2023-06-15 18:46:43 +08001183 res = snprintf(cmd, sizeof(cmd), "iw dev %s info | grep -i 'channel' | cut -d ' ' -f2", interface_name);
1184 if (os_snprintf_error(sizeof(cmd), res)) {
1185 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1186 return RETURN_ERR;
1187 }
1188
developera3511852023-06-14 14:12:59 +08001189 _syscmd(cmd, buf, sizeof(buf));
1190 channel = atoi(buf);
1191 WIFI_ENTRY_EXIT_DEBUG("%s:channel=%d\n", __func__, channel);
developera1255e42023-05-13 17:45:02 +08001192 /*count dev number with the same channel*/
developere40952c2023-06-15 18:46:43 +08001193 res = snprintf(cmd, sizeof(cmd), "iw dev | grep -i 'channel %d' | wc -l", channel);
1194 if (os_snprintf_error(sizeof(cmd), res)) {
1195 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1196 return RETURN_ERR;
1197 }
1198
developera3511852023-06-14 14:12:59 +08001199 _syscmd(cmd, buf, sizeof(buf));
1200 *bss_cnt = atoi(buf) - 1;/*1 for apcli interface*/
1201 WIFI_ENTRY_EXIT_DEBUG("%s:bss_cnt=%d\n", __func__, *bss_cnt);
1202 return RETURN_OK;
developera1255e42023-05-13 17:45:02 +08001203}
developer72fb0bb2023-01-11 09:46:29 +08001204
1205static int wifi_hostapdProcessUpdate(int apIndex, struct params *list, int item_count)
1206{
developera3511852023-06-14 14:12:59 +08001207 char interface_name[16] = {0};
1208 if (multiple_set == TRUE)
1209 return RETURN_OK;
1210 char cmd[MAX_CMD_SIZE]="", output[32]="";
1211 FILE *fp;
developere40952c2023-06-15 18:46:43 +08001212 int i, res;
developera3511852023-06-14 14:12:59 +08001213 //NOTE RELOAD should be done in ApplySSIDSettings
1214 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1215 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08001216
1217 for (i=0; i<item_count; i++, list++) {
1218 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s SET %s %s", interface_name, list->name, list->value);
1219 if (os_snprintf_error(sizeof(cmd), res)) {
1220 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1221 return RETURN_ERR;
1222 }
1223 if ((fp = popen(cmd, "r"))==NULL) {
developera3511852023-06-14 14:12:59 +08001224 perror("popen failed");
1225 return -1;
1226 }
developere40952c2023-06-15 18:46:43 +08001227 if (!fgets(output, sizeof(output), fp) || strncmp(output, "OK", 2)) {
1228 pclose(fp);
developera3511852023-06-14 14:12:59 +08001229 perror("fgets failed");
1230 return -1;
1231 }
developere40952c2023-06-15 18:46:43 +08001232 pclose(fp);
developera3511852023-06-14 14:12:59 +08001233 }
1234 return 0;
developer72fb0bb2023-01-11 09:46:29 +08001235}
1236
developer7e4a2a62023-04-06 19:56:03 +08001237static int wifi_quick_reload_ap(int apIndex)
1238{
1239 char interface_name[IF_NAME_SIZE] = {0};
1240 char cmd[MAX_CMD_SIZE] = {0};
1241 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001242 int res;
developer7e4a2a62023-04-06 19:56:03 +08001243
1244 if (multiple_set == TRUE)
1245 return RETURN_OK;
1246
1247 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1248 return RETURN_ERR;
1249
developere40952c2023-06-15 18:46:43 +08001250 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s reload", interface_name);
1251 if (os_snprintf_error(sizeof(cmd), res)) {
1252 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1253 return RETURN_ERR;
1254 }
developer7e4a2a62023-04-06 19:56:03 +08001255 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1256 return RETURN_ERR;
1257
1258 return RETURN_OK;
1259}
1260
developer72fb0bb2023-01-11 09:46:29 +08001261static int wifi_reloadAp(int apIndex)
1262{
developera3511852023-06-14 14:12:59 +08001263 char interface_name[16] = {0};
developer22e0c672023-06-07 15:25:37 +08001264 int res;
1265
developera3511852023-06-14 14:12:59 +08001266 if (multiple_set == TRUE)
1267 return RETURN_OK;
1268 char cmd[MAX_CMD_SIZE]="";
1269 char buf[MAX_BUF_SIZE]="";
developer72fb0bb2023-01-11 09:46:29 +08001270
developera3511852023-06-14 14:12:59 +08001271 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
1272 return RETURN_ERR;
1273 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s reload", interface_name);
developer22e0c672023-06-07 15:25:37 +08001274 if (os_snprintf_error(sizeof(cmd), res)) {
1275 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1276 return RETURN_ERR;
1277 }
developera3511852023-06-14 14:12:59 +08001278 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1279 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001280
developera3511852023-06-14 14:12:59 +08001281 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s disable", interface_name);
developer22e0c672023-06-07 15:25:37 +08001282 if (os_snprintf_error(sizeof(cmd), res)) {
1283 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1284 return RETURN_ERR;
1285 }
developera3511852023-06-14 14:12:59 +08001286 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1287 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001288
developera3511852023-06-14 14:12:59 +08001289 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s enable", interface_name);
developer22e0c672023-06-07 15:25:37 +08001290 if (os_snprintf_error(sizeof(cmd), res)) {
1291 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1292 return RETURN_ERR;
1293 }
developera3511852023-06-14 14:12:59 +08001294 if (_syscmd(cmd, buf, sizeof(buf)) == RETURN_ERR)
1295 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001296
developera3511852023-06-14 14:12:59 +08001297 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001298}
1299
1300INT File_Reading(CHAR *file, char *Value)
1301{
developera3511852023-06-14 14:12:59 +08001302 FILE *fp = NULL;
1303 char buf[MAX_CMD_SIZE] = {0}, copy_buf[MAX_CMD_SIZE] ={0};
1304 int count = 0;
developer72fb0bb2023-01-11 09:46:29 +08001305
developera3511852023-06-14 14:12:59 +08001306 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1307 fp = popen(file,"r");
1308 if(fp == NULL)
1309 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001310
developera3511852023-06-14 14:12:59 +08001311 if(fgets(buf,sizeof(buf) -1,fp) != NULL)
1312 {
1313 for(count=0;buf[count]!='\n';count++)
1314 copy_buf[count]=buf[count];
1315 copy_buf[count]='\0';
1316 }
developerc14d83a2023-06-29 20:09:42 +08001317 strncpy(Value, copy_buf, strlen(copy_buf));
developer12fb9f62023-06-30 15:26:27 +08001318 Value[strlen(copy_buf)] = '\0';
developera3511852023-06-14 14:12:59 +08001319 pclose(fp);
1320 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001321
developera3511852023-06-14 14:12:59 +08001322 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001323}
1324
1325void wifi_RestartHostapd_2G()
1326{
developera3511852023-06-14 14:12:59 +08001327 int Public2GApIndex = 4;
developer72fb0bb2023-01-11 09:46:29 +08001328
developera3511852023-06-14 14:12:59 +08001329 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1330 wifi_setApEnable(Public2GApIndex, FALSE);
1331 wifi_setApEnable(Public2GApIndex, TRUE);
1332 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001333}
1334
1335void wifi_RestartHostapd_5G()
1336{
developera3511852023-06-14 14:12:59 +08001337 int Public5GApIndex = 5;
developer72fb0bb2023-01-11 09:46:29 +08001338
developera3511852023-06-14 14:12:59 +08001339 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1340 wifi_setApEnable(Public5GApIndex, FALSE);
1341 wifi_setApEnable(Public5GApIndex, TRUE);
1342 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001343}
1344
1345void wifi_RestartPrivateWifi_2G()
1346{
developera3511852023-06-14 14:12:59 +08001347 int PrivateApIndex = 0;
developer72fb0bb2023-01-11 09:46:29 +08001348
developera3511852023-06-14 14:12:59 +08001349 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1350 wifi_setApEnable(PrivateApIndex, FALSE);
1351 wifi_setApEnable(PrivateApIndex, TRUE);
1352 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001353}
1354
1355void wifi_RestartPrivateWifi_5G()
1356{
developera3511852023-06-14 14:12:59 +08001357 int Private5GApIndex = 1;
developer72fb0bb2023-01-11 09:46:29 +08001358
developera3511852023-06-14 14:12:59 +08001359 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1360 wifi_setApEnable(Private5GApIndex, FALSE);
1361 wifi_setApEnable(Private5GApIndex, TRUE);
1362 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001363}
1364
1365static int writeBandWidth(int radioIndex,char *bw_value)
1366{
developera3511852023-06-14 14:12:59 +08001367 char buf[MAX_BUF_SIZE];
1368 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08001369 int res;
developer72fb0bb2023-01-11 09:46:29 +08001370
developere40952c2023-06-15 18:46:43 +08001371 res = snprintf(cmd, sizeof(cmd), "grep SET_BW%d %s", radioIndex, BW_FNAME);
1372 if (os_snprintf_error(sizeof(cmd), res)) {
1373 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1374 return RETURN_ERR;
1375 }
1376
1377 if (_syscmd(cmd, buf, sizeof(buf))) {
1378 res = snprintf(cmd, sizeof(cmd), "echo SET_BW%d=%s >> %s", radioIndex, bw_value, BW_FNAME);
1379 if (os_snprintf_error(sizeof(cmd), res)) {
1380 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1381 return RETURN_ERR;
1382 }
developera3511852023-06-14 14:12:59 +08001383 _syscmd(cmd, buf, sizeof(buf));
1384 return RETURN_OK;
1385 }
developer72fb0bb2023-01-11 09:46:29 +08001386
developer75bd10c2023-06-27 11:34:08 +08001387 res = snprintf(cmd, sizeof(cmd), "sed -i 's/^SET_BW%d=.*$/SET_BW%d=%s/' %s",radioIndex,radioIndex,bw_value,BW_FNAME);
1388 if (os_snprintf_error(sizeof(cmd), res)) {
1389 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1390 return RETURN_ERR;
1391 }
developera3511852023-06-14 14:12:59 +08001392 _syscmd(cmd,buf,sizeof(buf));
1393 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001394}
1395
1396// Input could be "1Mbps"; "5.5Mbps"; "6Mbps"; "2Mbps"; "11Mbps"; "12Mbps"; "24Mbps"
1397INT wifi_setApBeaconRate(INT radioIndex,CHAR *beaconRate)
1398{
developera3511852023-06-14 14:12:59 +08001399 struct params params={'\0'};
1400 char config_file[MAX_BUF_SIZE] = {0};
1401 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08001402 int res;
developer72fb0bb2023-01-11 09:46:29 +08001403
developera3511852023-06-14 14:12:59 +08001404 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1405 // Copy the numeric value
1406 if (strlen (beaconRate) >= 5) {
1407 strncpy(buf, beaconRate, strlen(beaconRate) - 4);
1408 buf[strlen(beaconRate) - 4] = '\0';
developer9ce44382023-06-28 11:09:37 +08001409 } else if (strlen(beaconRate) > 0){
1410 strncpy(buf, beaconRate,sizeof(buf) - 1);
1411 buf[sizeof(buf) - 1] = '\0';
1412 } else
developera3511852023-06-14 14:12:59 +08001413 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001414
developera3511852023-06-14 14:12:59 +08001415 params.name = "beacon_rate";
1416 // hostapd config unit is 100 kbps. To convert Mbps to 100kbps, the value need to multiply 10.
1417 if (strncmp(buf, "5.5", 3) == 0) {
developere40952c2023-06-15 18:46:43 +08001418 res = snprintf(buf, sizeof(buf), "55");
1419 if (os_snprintf_error(sizeof(buf), res)) {
1420 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1421 return RETURN_ERR;
1422 }
developera3511852023-06-14 14:12:59 +08001423 params.value = buf;
1424 } else {
developer32f2a182023-06-27 19:50:41 +08001425 if (strlen(buf) >= (MAX_BUF_SIZE - 1)) {
1426 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
1427 return RETURN_ERR;
1428 }
1429 strncat(buf, "0", sizeof(buf) - strlen(buf) - 1);
developera3511852023-06-14 14:12:59 +08001430 params.value = buf;
1431 }
developer72fb0bb2023-01-11 09:46:29 +08001432
developer32f2a182023-06-27 19:50:41 +08001433 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
1434 if (os_snprintf_error(sizeof(config_file), res)) {
1435 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1436 return RETURN_ERR;
1437 }
1438
developera3511852023-06-14 14:12:59 +08001439 wifi_hostapdWrite(config_file, &params, 1);
1440 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
1441 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001442
developera3511852023-06-14 14:12:59 +08001443 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001444}
1445
1446INT wifi_getApBeaconRate(INT radioIndex, CHAR *beaconRate)
1447{
developera3511852023-06-14 14:12:59 +08001448 char config_file[128] = {'\0'};
1449 char temp_output[MAX_BUF_SIZE] = {'\0'};
1450 char buf[128] = {'\0'};
1451 char cmd[128] = {'\0'};
developerc14d83a2023-06-29 20:09:42 +08001452 long int rate = 0;
developere40952c2023-06-15 18:46:43 +08001453 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08001454
developera3511852023-06-14 14:12:59 +08001455 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1456 if (NULL == beaconRate)
1457 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001458
developer75bd10c2023-06-27 11:34:08 +08001459 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
1460 if (os_snprintf_error(sizeof(config_file), res)) {
1461 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1462 return RETURN_ERR;
1463 }
developera3511852023-06-14 14:12:59 +08001464 wifi_hostapdRead(config_file, "beacon_rate", buf, sizeof(buf));
1465 phyId = radio_index_to_phy(radioIndex);
1466 // Hostapd unit is 100kbps. To convert to 100kbps to Mbps, the value need to divide 10.
1467 if(strlen(buf) > 0) {
1468 if (strncmp(buf, "55", 2) == 0)
developere40952c2023-06-15 18:46:43 +08001469 res = snprintf(temp_output, sizeof(temp_output), "5.5Mbps");
developera3511852023-06-14 14:12:59 +08001470 else {
developerc14d83a2023-06-29 20:09:42 +08001471 if (hal_strtol(buf, 10, &rate) < 0) {
1472 wifi_debug(DEBUG_ERROR, "strtol fail\n");
1473 }
developerb14b3462023-07-01 18:02:42 +08001474 res = snprintf(temp_output, sizeof(temp_output), "%ldMbps", rate/10);
developera3511852023-06-14 14:12:59 +08001475 }
developer75bd10c2023-06-27 11:34:08 +08001476 if (os_snprintf_error(sizeof(temp_output), res)) {
1477 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1478 return RETURN_ERR;
1479 }
developera3511852023-06-14 14:12:59 +08001480 } else {
1481 // config not set, so we would use lowest rate as default
developer75bd10c2023-06-27 11:34:08 +08001482 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep Bitrates -A1 | tail -n 1 | awk '{print $2}' | tr -d '.0\\n'", phyId);
1483 if (os_snprintf_error(sizeof(cmd), res)) {
1484 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1485 return RETURN_ERR;
1486 }
developera3511852023-06-14 14:12:59 +08001487 _syscmd(cmd, buf, sizeof(buf));
developere40952c2023-06-15 18:46:43 +08001488 res = snprintf(temp_output, sizeof(temp_output), "%sMbps", buf);
developer75bd10c2023-06-27 11:34:08 +08001489 if (os_snprintf_error(sizeof(temp_output), res)) {
1490 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1491 return RETURN_ERR;
1492 }
developera3511852023-06-14 14:12:59 +08001493 }
developer75bd10c2023-06-27 11:34:08 +08001494
developera3511852023-06-14 14:12:59 +08001495 strncpy(beaconRate, temp_output, strlen(temp_output));
1496 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001497
developera3511852023-06-14 14:12:59 +08001498 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001499}
1500
1501INT wifi_setLED(INT radioIndex, BOOL enable)
1502{
1503 return 0;
1504}
1505INT wifi_setRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG seconds)
1506{
1507 return RETURN_OK;
1508}
1509/**********************************************************************************
1510 *
developer69b61b02023-03-07 17:17:44 +08001511 * Wifi Subsystem level function prototypes
developer72fb0bb2023-01-11 09:46:29 +08001512 *
1513**********************************************************************************/
1514//---------------------------------------------------------------------------------------------------
1515//Wifi system api
1516//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 +08001517INT wifi_getHalVersion(CHAR *output_string) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08001518{
developere40952c2023-06-15 18:46:43 +08001519 int res;
1520
developera3511852023-06-14 14:12:59 +08001521 if(!output_string)
1522 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08001523 res = snprintf(output_string, 64, "%d.%d.%d", WIFI_HAL_MAJOR_VERSION, WIFI_HAL_MINOR_VERSION, WIFI_HAL_MAINTENANCE_VERSION);
1524 if (os_snprintf_error(64, res)) {
1525 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1526 return RETURN_ERR;
1527 }
developer72fb0bb2023-01-11 09:46:29 +08001528
developera3511852023-06-14 14:12:59 +08001529 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001530}
1531
1532
1533/* wifi_factoryReset() function */
1534/**
developer69b61b02023-03-07 17:17:44 +08001535* @description Clears internal variables to implement a factory reset of the Wi-Fi
developer72fb0bb2023-01-11 09:46:29 +08001536* subsystem. Resets Implementation specifics may dictate some functionality since different hardware implementations may have different requirements.
1537*
1538* @param None
1539*
1540* @return The status of the operation.
1541* @retval RETURN_OK if successful.
1542* @retval RETURN_ERR if any error is detected
1543*
1544* @execution Synchronous
1545* @sideeffect None
1546*
1547* @note This function must not suspend and must not invoke any blocking system
1548* calls. It should probably just send a message to a driver event handler task.
1549*
1550*/
1551INT wifi_factoryReset()
1552{
developer47cc27a2023-05-17 23:09:58 +08001553 char cmd[MAX_CMD_SIZE] = {0};
1554 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001555 int res;
developer72fb0bb2023-01-11 09:46:29 +08001556
developer47cc27a2023-05-17 23:09:58 +08001557 /*delete running hostapd conf files*/
1558 wifi_dbg_printf("\n[%s]: deleting hostapd conf file.", __func__);
developere40952c2023-06-15 18:46:43 +08001559 res = snprintf(cmd, MAX_CMD_SIZE, "rm -rf /nvram/*.conf");
1560 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1561 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1562 return RETURN_ERR;
1563 }
1564
developer47cc27a2023-05-17 23:09:58 +08001565 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08001566
developerd14dff12023-06-28 22:47:44 +08001567 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08001568 wifi_psk_file_reset();
1569
1570 memset(cmd, 0, MAX_CMD_SIZE);
1571 memset(buf, 0, MAX_BUF_SIZE);
1572
developere40952c2023-06-15 18:46:43 +08001573 res = snprintf(cmd, MAX_CMD_SIZE, "systemctl restart hostapd.service");
1574 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1575 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1576 return RETURN_ERR;
1577 }
1578
developer47cc27a2023-05-17 23:09:58 +08001579 _syscmd(cmd, buf, sizeof(buf));
1580
1581 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001582}
1583
1584/* wifi_factoryResetRadios() function */
1585/**
1586* @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.
1587*
1588* @param None
1589* @return The status of the operation
1590* @retval RETURN_OK if successful
1591* @retval RETURN_ERR if any error is detected
1592*
1593* @execution Synchronous
1594*
1595* @sideeffect None
1596*
1597* @note This function must not suspend and must not invoke any blocking system
1598* calls. It should probably just send a message to a driver event handler task.
1599*
1600*/
1601INT wifi_factoryResetRadios()
1602{
developera3511852023-06-14 14:12:59 +08001603 if((RETURN_OK == wifi_factoryResetRadio(0)) && (RETURN_OK == wifi_factoryResetRadio(1)))
1604 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001605
developera3511852023-06-14 14:12:59 +08001606 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08001607}
1608
developerfead3972023-05-25 20:15:02 +08001609ULONG get_radio_reset_cnt(int radioIndex)
1610{
1611 char cmd[MAX_CMD_SIZE] = {0};
1612 char buf[MAX_BUF_SIZE] = {0};
1613 ULONG reset_count = 0;
developere40952c2023-06-15 18:46:43 +08001614 int res;
developerfead3972023-05-25 20:15:02 +08001615
developere40952c2023-06-15 18:46:43 +08001616 res = snprintf(cmd, MAX_CMD_SIZE, "cat %s 2> /dev/null | grep \"^reset%d=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developerfead3972023-05-25 20:15:02 +08001617 RADIO_RESET_FILE, radioIndex);
developere40952c2023-06-15 18:46:43 +08001618 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1619 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1620 return RETURN_ERR;
1621 }
1622
developerfead3972023-05-25 20:15:02 +08001623 _syscmd(cmd, buf, sizeof(buf));
1624
1625 if (strlen(buf) == 0)
1626 return 0;
1627 else {
1628 reset_count = atol(buf);
1629 return reset_count;
1630 }
1631}
1632void update_radio_reset_cnt(int radioIndex)
1633{
1634 char cmd[MAX_CMD_SIZE] = {0};
1635 char buf[MAX_BUF_SIZE] = {0};
1636 ULONG reset_count = 0;
developere40952c2023-06-15 18:46:43 +08001637 int res;
developerfead3972023-05-25 20:15:02 +08001638
developere40952c2023-06-15 18:46:43 +08001639 res = snprintf(cmd, MAX_CMD_SIZE, "cat %s 2> /dev/null | grep \"^reset%d=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"",
developerfead3972023-05-25 20:15:02 +08001640 RADIO_RESET_FILE, radioIndex);
developere40952c2023-06-15 18:46:43 +08001641 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
1642 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1643 return;
1644 }
1645
developerfead3972023-05-25 20:15:02 +08001646 _syscmd(cmd, buf, sizeof(buf));
1647
1648 if (strlen(buf) == 0)
developere40952c2023-06-15 18:46:43 +08001649 res = snprintf(cmd, sizeof(cmd), "sed -i -e '$a reset%d=1' %s", radioIndex, RADIO_RESET_FILE);
developerfead3972023-05-25 20:15:02 +08001650 else {
1651 reset_count = atol(buf);
1652 reset_count++;
developere40952c2023-06-15 18:46:43 +08001653 res = snprintf(cmd, sizeof(cmd), "sed -i \"s/^reset%d=.*/reset%d=%lu/\" %s", radioIndex, radioIndex, reset_count, RADIO_RESET_FILE);
1654 }
1655 if (os_snprintf_error(sizeof(cmd), res)) {
1656 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1657 return;
developerfead3972023-05-25 20:15:02 +08001658 }
1659 _syscmd(cmd, buf, sizeof(buf));
1660}
developer72fb0bb2023-01-11 09:46:29 +08001661
1662/* wifi_factoryResetRadio() function */
1663/**
1664* @description Restore selected radio parameters without touching access point parameters
1665*
1666* @param radioIndex - Index of Wi-Fi Radio channel
1667*
1668* @return The status of the operation.
1669* @retval RETURN_OK if successful.
1670* @retval RETURN_ERR if any error is detected
1671*
1672* @execution Synchronous.
1673* @sideeffect None.
1674*
1675* @note This function must not suspend and must not invoke any blocking system
1676* calls. It should probably just send a message to a driver event handler task.
1677*
1678*/
1679INT wifi_factoryResetRadio(int radioIndex) //RDKB
1680{
developer47cc27a2023-05-17 23:09:58 +08001681 char cmd[MAX_CMD_SIZE] = {0};
1682 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08001683 int res;
developer72fb0bb2023-01-11 09:46:29 +08001684
developer47cc27a2023-05-17 23:09:58 +08001685 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08001686
developerb2977562023-05-24 17:54:12 +08001687 wifi_dat_file_reset_by_radio(radioIndex);
developer47cc27a2023-05-17 23:09:58 +08001688
developerb2977562023-05-24 17:54:12 +08001689 /*reset gi setting*/
developere40952c2023-06-15 18:46:43 +08001690 res = snprintf(cmd, sizeof(cmd), "echo 'Auto' > %s%d.txt", GUARD_INTERVAL_FILE, radioIndex);
1691 if (os_snprintf_error(sizeof(cmd), res)) {
1692 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1693 return RETURN_ERR;
1694 }
1695
developer47cc27a2023-05-17 23:09:58 +08001696 _syscmd(cmd, buf, sizeof(buf));
1697
developerb2977562023-05-24 17:54:12 +08001698 /*TBD: check mbss issue*/
1699 wifi_factoryResetAP(radioIndex);
developerfead3972023-05-25 20:15:02 +08001700 update_radio_reset_cnt(radioIndex);
developerb2977562023-05-24 17:54:12 +08001701
developer47cc27a2023-05-17 23:09:58 +08001702 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
1703 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001704}
1705
1706/* wifi_initRadio() function */
1707/**
1708* Description: This function call initializes the specified radio.
developer69b61b02023-03-07 17:17:44 +08001709* Implementation specifics may dictate the functionality since
developer72fb0bb2023-01-11 09:46:29 +08001710* different hardware implementations may have different initilization requirements.
1711* Parameters : radioIndex - The index of the radio. First radio is index 0. 2nd radio is index 1 - type INT
1712*
1713* @return The status of the operation.
1714* @retval RETURN_OK if successful.
1715* @retval RETURN_ERR if any error is detected
1716*
1717* @execution Synchronous.
1718* @sideeffect None.
1719*
1720* @note This function must not suspend and must not invoke any blocking system
1721* calls. It should probably just send a message to a driver event handler task.
1722*
1723*/
1724INT wifi_initRadio(INT radioIndex)
1725{
developera3511852023-06-14 14:12:59 +08001726 //TODO: Initializes the wifi subsystem (for specified radio)
1727 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08001728}
1729
developer17038e62023-03-02 14:43:43 +08001730static void
1731wifi_ParseProfile(void)
1732{
developere40952c2023-06-15 18:46:43 +08001733 int i, res;
developera3511852023-06-14 14:12:59 +08001734 int max_radio_num = 0;
1735 int card_idx;
1736 int band_idx;
1737 int phy_idx = 0;
1738 int wireless_mode = 0;
1739 char buf[MAX_BUF_SIZE] = {0};
1740 char chip_name[12];
1741 char card_profile[MAX_BUF_SIZE] = {0};
1742 char band_profile[MAX_BUF_SIZE] = {0};
developer17038e62023-03-02 14:43:43 +08001743
developera3511852023-06-14 14:12:59 +08001744 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001745
developera3511852023-06-14 14:12:59 +08001746 memset(main_prefix, 0, sizeof(main_prefix));
1747 memset(ext_prefix, 0, sizeof(ext_prefix));
1748 memset(default_ssid, 0, sizeof(default_ssid));
1749 for (i = 0; i < MAX_NUM_RADIOS; i++)
1750 radio_band[i] = band_invalid;
developer17038e62023-03-02 14:43:43 +08001751
developera3511852023-06-14 14:12:59 +08001752 if (wifi_getMaxRadioNumber(&max_radio_num) != RETURN_OK) {
1753 /* LOG */
developer17038e62023-03-02 14:43:43 +08001754 return;
developera3511852023-06-14 14:12:59 +08001755 }
developer17038e62023-03-02 14:43:43 +08001756
developera3511852023-06-14 14:12:59 +08001757 for (card_idx = 0; card_idx < 3; card_idx++) {
developere40952c2023-06-15 18:46:43 +08001758 res = snprintf(buf, sizeof(buf), "INDEX%d", card_idx);
1759 if (os_snprintf_error(sizeof(buf), res)) {
1760 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1761 return;
1762 }
developera3511852023-06-14 14:12:59 +08001763 if (get_value(l1profile, buf, chip_name, sizeof(chip_name)) < 0) {
1764 break;
1765 }
developere40952c2023-06-15 18:46:43 +08001766 res = snprintf(buf, sizeof(buf), "INDEX%d_profile_path", card_idx);
1767 if (os_snprintf_error(sizeof(buf), res)) {
1768 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1769 return;
1770 }
developera3511852023-06-14 14:12:59 +08001771 if (get_value(l1profile, buf, card_profile, sizeof(card_profile)) < 0) {
1772 break;
1773 }
1774 for (band_idx = 0; band_idx < 3; band_idx++) {
developere40952c2023-06-15 18:46:43 +08001775 res = snprintf(buf, sizeof(buf), "BN%d_profile_path", band_idx);
1776 if (os_snprintf_error(sizeof(buf), res)) {
1777 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1778 return;
1779 }
developera3511852023-06-14 14:12:59 +08001780 if (get_value(card_profile, buf, band_profile, sizeof(band_profile)) < 0) {
1781 /* LOG */
1782 break;
1783 }
developer17038e62023-03-02 14:43:43 +08001784
developere40952c2023-06-15 18:46:43 +08001785 res = snprintf(buf, sizeof(buf), "INDEX%d_main_ifname", card_idx);
1786 if (os_snprintf_error(sizeof(buf), res)) {
1787 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1788 return;
1789 }
developera3511852023-06-14 14:12:59 +08001790 if (get_value_by_idx(l1profile, buf, band_idx, main_prefix[phy_idx], IFNAMSIZ) < 0) {
1791 /* LOG */
1792 }
developer17038e62023-03-02 14:43:43 +08001793
developere40952c2023-06-15 18:46:43 +08001794 res = snprintf(buf, sizeof(buf), "INDEX%d_ext_ifname", card_idx);
1795 if (os_snprintf_error(sizeof(buf), res)) {
1796 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1797 return;
1798 }
developera3511852023-06-14 14:12:59 +08001799 if (get_value_by_idx(l1profile, buf, band_idx, ext_prefix[phy_idx], IFNAMSIZ) < 0) {
1800 /* LOG */
1801 }
developer17038e62023-03-02 14:43:43 +08001802
developera3511852023-06-14 14:12:59 +08001803 if (get_value(band_profile, "SSID1", default_ssid[phy_idx], sizeof(default_ssid[phy_idx])) < 0) {
1804 /* LOG */
1805 }
1806 if (get_value(band_profile, "WirelessMode", buf, sizeof(buf)) < 0) {
1807 /* LOG */
1808 }
developer745f0bd2023-03-06 14:32:53 +08001809
developera3511852023-06-14 14:12:59 +08001810 wireless_mode = atoi(buf);
1811 switch (wireless_mode) {
1812 case 22:
1813 case 16:
1814 case 6:
1815 case 4:
1816 case 1:
1817 radio_band[phy_idx] = band_2_4;
1818 break;
1819 case 23:
1820 case 17:
1821 case 14:
1822 case 11:
1823 case 2:
1824 radio_band[phy_idx] = band_5;
1825 break;
1826 case 24:
1827 case 18:
1828 radio_band[phy_idx] = band_6;
1829 break;
1830 }
1831 phy_idx++;
1832 }
1833 }
developer17038e62023-03-02 14:43:43 +08001834
developera3511852023-06-14 14:12:59 +08001835 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001836}
1837
1838static void
developerd14dff12023-06-28 22:47:44 +08001839wifi_PrepareDefaultHostapdConfigs(bool reset)
developer17038e62023-03-02 14:43:43 +08001840{
developere40952c2023-06-15 18:46:43 +08001841 int radio_idx, res;
developer0132ed92023-03-21 13:48:53 +08001842 int bss_idx;
1843 int ap_idx;
developer0132ed92023-03-21 13:48:53 +08001844 char buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08001845 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer0132ed92023-03-21 13:48:53 +08001846 char ssid[MAX_BUF_SIZE] = {0};
1847 char interface[32] = {0};
1848 char ret_buf[MAX_BUF_SIZE] = {0};
1849 char psk_file[64] = {0};
1850 struct params params[3];
developer17038e62023-03-02 14:43:43 +08001851
developer0132ed92023-03-21 13:48:53 +08001852 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1853 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
developer47cc27a2023-05-17 23:09:58 +08001854
developer0132ed92023-03-21 13:48:53 +08001855 for (bss_idx = 0; bss_idx < 5; bss_idx++) {
developer47cc27a2023-05-17 23:09:58 +08001856 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
developer0132ed92023-03-21 13:48:53 +08001857
developere40952c2023-06-15 18:46:43 +08001858 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_idx);
1859 if (os_snprintf_error(sizeof(config_file), res)) {
1860 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1861 return;
1862 }
developerd14dff12023-06-28 22:47:44 +08001863 if (access(config_file, F_OK) == 0 && reset == FALSE)
1864 continue;
developere40952c2023-06-15 18:46:43 +08001865 res = snprintf(buf, sizeof(buf), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], config_file);
1866 if (os_snprintf_error(sizeof(buf), res)) {
1867 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1868 return;
1869 }
developer47cc27a2023-05-17 23:09:58 +08001870 _syscmd(buf, ret_buf, sizeof(ret_buf));
developer17038e62023-03-02 14:43:43 +08001871
developer47cc27a2023-05-17 23:09:58 +08001872 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08001873 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
1874 if (os_snprintf_error(sizeof(ssid), res)) {
1875 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1876 return;
1877 }
1878 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
1879 if (os_snprintf_error(sizeof(interface), res)) {
1880 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1881 return;
1882 }
developer47cc27a2023-05-17 23:09:58 +08001883 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08001884 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
1885 if (os_snprintf_error(sizeof(ssid), res)) {
1886 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1887 return;
1888 }
1889 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
1890 if (os_snprintf_error(sizeof(interface), res)) {
1891 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1892 return;
1893 }
developer47cc27a2023-05-17 23:09:58 +08001894 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08001895 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
1896 if (os_snprintf_error(sizeof(ssid), res)) {
1897 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1898 return;
1899 }
1900 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
1901 if (os_snprintf_error(sizeof(interface), res)) {
1902 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1903 return;
1904 }
developer47cc27a2023-05-17 23:09:58 +08001905 }
developer17038e62023-03-02 14:43:43 +08001906
developer47cc27a2023-05-17 23:09:58 +08001907 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08001908 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", ap_idx);
1909 if (os_snprintf_error(sizeof(psk_file), res)) {
1910 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1911 return;
1912 }
developer17038e62023-03-02 14:43:43 +08001913
developer47cc27a2023-05-17 23:09:58 +08001914 params[0].name = "ssid";
1915 params[0].value = ssid;
1916 params[1].name = "interface";
1917 params[1].value = interface;
1918 params[2].name = "wpa_psk_file";
1919 params[2].value = psk_file;
developer17038e62023-03-02 14:43:43 +08001920
developer47cc27a2023-05-17 23:09:58 +08001921 wifi_hostapdWrite(config_file, params, 3);
developer0132ed92023-03-21 13:48:53 +08001922 }
1923 }
1924 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001925}
1926
1927static void
developer4fe22c62023-08-03 13:23:58 +08001928wifiBringUpInterfacesForRadio(int radio_idx)
1929{
1930 int bss_idx;
1931 int ap_idx;
1932 char cmd[MAX_CMD_SIZE] = {0};
1933 char config_file[MAX_SUB_CMD_SIZE] = {0};
1934 char ret_buf[MAX_BUF_SIZE] = {0};
1935 char inf_name[IF_NAME_SIZE] = {0};
1936 int res;
1937
1938 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1939
1940 bss_idx = 0;
1941 /*TBD: we need refine setup flow and mbss flow*/
1942// for (bss_idx = 0; bss_idx < 5; bss_idx++) {
1943 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
1944
1945 res = snprintf(cmd, sizeof(cmd), "touch %s%d.psk", PSK_FILE, ap_idx);
1946 if (os_snprintf_error(sizeof(cmd), res)) {
1947 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1948 return;
1949 }
1950 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1951
1952 memset(cmd, 0, MAX_CMD_SIZE);
1953 memset(ret_buf, 0, MAX_BUF_SIZE);
1954
1955 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_idx);
1956 if (os_snprintf_error(sizeof(cmd), res)) {
1957 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1958 return;
1959 }
1960 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw ADD bss_config=phy%d:%s", radio_idx, config_file);
1961 if (os_snprintf_error(sizeof(cmd), res)) {
1962 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1963 return;
1964 }
1965 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1966
1967 wifi_GetInterfaceName(ap_idx, inf_name);
1968
1969 memset(cmd, 0, MAX_CMD_SIZE);
1970 memset(ret_buf, 0, MAX_BUF_SIZE);
1971
1972 /* fix vap-status file */
1973 res = snprintf(cmd, sizeof(cmd), "sed -i \"s/^%s=.*/%s=1/\" %s", inf_name, inf_name, VAP_STATUS_FILE);
1974 if (os_snprintf_error(sizeof(cmd), res)) {
1975 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1976 return;
1977 }
1978 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1979// }
1980
1981 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
1982}
1983
1984
1985static void
1986wifi_BringUpInterfaces(void)
1987{
1988 int radio_idx;
1989 int band_idx;
1990
1991 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1992 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
1993 band_idx = radio_index_to_band(radio_idx);
1994 if (band_idx < 0) {
1995 break;
1996 }
1997 wifiBringUpInterfacesForRadio(radio_idx);
1998 }
1999 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2000}
2001
2002static void
developer17038e62023-03-02 14:43:43 +08002003wifi_BringDownInterfacesForRadio(int radio_idx)
2004{
developera3511852023-06-14 14:12:59 +08002005 char cmd[MAX_BUF_SIZE] = {0};
2006 char ret_buf[MAX_BUF_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08002007 int res;
developerb758dfd2023-06-21 17:32:07 +08002008
developera3511852023-06-14 14:12:59 +08002009 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08002010
developere40952c2023-06-15 18:46:43 +08002011 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw REMOVE %s", main_prefix[radio_idx]);
2012 if (os_snprintf_error(sizeof(cmd), res)) {
2013 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2014 return;
2015 }
developer8a3bbbf2023-03-15 17:47:23 +08002016 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2017
developera3511852023-06-14 14:12:59 +08002018 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08002019}
2020
2021
2022static void
2023wifi_BringDownInterfaces(void)
2024{
developera3511852023-06-14 14:12:59 +08002025 int radio_idx;
2026 int band_idx;
developer17038e62023-03-02 14:43:43 +08002027
developera3511852023-06-14 14:12:59 +08002028 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
2029 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
2030 band_idx = radio_index_to_band(radio_idx);
2031 if (band_idx < 0) {
2032 break;
2033 }
2034 wifi_BringDownInterfacesForRadio(radio_idx);
2035 }
2036 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08002037}
2038
developerb2977562023-05-24 17:54:12 +08002039static void wifi_dat_file_reset_by_radio(char radio_idx)
2040{
developerb149d9d2023-06-06 16:14:22 +08002041 char cmd[MAX_CMD_SIZE * 2] = {0};
developerb2977562023-05-24 17:54:12 +08002042 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08002043 char rom_dat_file[MAX_SUB_CMD_SIZE]= {0};
2044 char dat_file[MAX_SUB_CMD_SIZE]= {0};
developere40952c2023-06-15 18:46:43 +08002045 int res;
developerb2977562023-05-24 17:54:12 +08002046
developere40952c2023-06-15 18:46:43 +08002047 res = snprintf(rom_dat_file, sizeof(rom_dat_file), "%s%d.dat", ROM_LOGAN_DAT_FILE, radio_idx);
2048 if (os_snprintf_error(sizeof(rom_dat_file), res)) {
2049 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2050 return;
2051 }
2052 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
2053 if (os_snprintf_error(sizeof(dat_file), res)) {
2054 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2055 return;
2056 }
2057 res = snprintf(cmd, (MAX_CMD_SIZE * 2), "cp -rf %s %s", rom_dat_file, dat_file);
2058 if (os_snprintf_error((MAX_CMD_SIZE * 2), res)) {
2059 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2060 return;
2061 }
developerb2977562023-05-24 17:54:12 +08002062 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2063
2064}
2065
developer47cc27a2023-05-17 23:09:58 +08002066static void wifi_psk_file_reset()
2067{
2068 char cmd[MAX_CMD_SIZE] = {0};
2069 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08002070 char psk_file[MAX_SUB_CMD_SIZE]= {0};
developer47cc27a2023-05-17 23:09:58 +08002071 char vap_idx = 0;
developere40952c2023-06-15 18:46:43 +08002072 int res;
developer47cc27a2023-05-17 23:09:58 +08002073
2074 for (vap_idx = 0; vap_idx < MAX_APS; vap_idx++) {
developere40952c2023-06-15 18:46:43 +08002075 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, vap_idx);
2076 if (os_snprintf_error(sizeof(psk_file), res)) {
2077 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2078 return;
2079 }
developer47cc27a2023-05-17 23:09:58 +08002080
2081 if (access(psk_file, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002082 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", psk_file);
2083 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2084 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2085 return;
2086 }
developer47cc27a2023-05-17 23:09:58 +08002087 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2088 } else {
developere40952c2023-06-15 18:46:43 +08002089 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", psk_file);
2090 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2091 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2092 return;
2093 }
2094
developer47cc27a2023-05-17 23:09:58 +08002095 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2096 }
2097 }
developerb2977562023-05-24 17:54:12 +08002098}
2099
developer8a3bbbf2023-03-15 17:47:23 +08002100static void wifi_vap_status_reset()
2101{
developera3511852023-06-14 14:12:59 +08002102 char cmd[MAX_CMD_SIZE] = {0};
2103 char ret_buf[MAX_BUF_SIZE] = {0};
developer863a4a62023-06-06 16:55:59 +08002104 int radio_idx = 0;
developer8a3bbbf2023-03-15 17:47:23 +08002105 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08002106 int res;
developer8666b312023-03-24 14:05:31 +08002107
developer8a3bbbf2023-03-15 17:47:23 +08002108 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002109 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", VAP_STATUS_FILE);
2110 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2111 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2112 return;
2113 }
developer8a3bbbf2023-03-15 17:47:23 +08002114 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2115 } else {
developere40952c2023-06-15 18:46:43 +08002116 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", VAP_STATUS_FILE);
2117 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2118 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2119 return;
2120 }
developer8a3bbbf2023-03-15 17:47:23 +08002121 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2122 }
2123
2124 memset(cmd, 0, MAX_CMD_SIZE);
2125 memset(ret_buf, 0, MAX_BUF_SIZE);
2126
2127 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++)
developera3511852023-06-14 14:12:59 +08002128 for (bss_idx = 0; bss_idx < 5; bss_idx++) {
developere40952c2023-06-15 18:46:43 +08002129 res = snprintf(cmd, MAX_CMD_SIZE, "echo %s%d=0 >> %s", ext_prefix[radio_idx], bss_idx, VAP_STATUS_FILE);
2130 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2131 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2132 return;
2133 }
developer8a3bbbf2023-03-15 17:47:23 +08002134 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2135 }
2136
developerfead3972023-05-25 20:15:02 +08002137}
2138
2139static void wifi_radio_reset_count_reset()
2140{
developera3511852023-06-14 14:12:59 +08002141 char cmd[MAX_CMD_SIZE] = {0};
2142 char ret_buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002143 int res;
developerfead3972023-05-25 20:15:02 +08002144
2145 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002146 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", RADIO_RESET_FILE);
2147 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2148 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2149 return;
2150 }
developerfead3972023-05-25 20:15:02 +08002151 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2152 } else {
developere40952c2023-06-15 18:46:43 +08002153 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", RADIO_RESET_FILE);
2154 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2155 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2156 return;
2157 }
developerfead3972023-05-25 20:15:02 +08002158 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2159 }
developer8a3bbbf2023-03-15 17:47:23 +08002160}
developer17038e62023-03-02 14:43:43 +08002161
developer72fb0bb2023-01-11 09:46:29 +08002162// Initializes the wifi subsystem (all radios)
developera3511852023-06-14 14:12:59 +08002163INT wifi_init() //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002164{
developera3511852023-06-14 14:12:59 +08002165 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developercc9a4f32023-05-30 17:40:02 +08002166 static int CallOnce = 1;
developera3511852023-06-14 14:12:59 +08002167 //Not intitializing macfilter for Turris-Omnia Platform for now
2168 //macfilter_init();
2169 if (CallOnce) {
developercc9a4f32023-05-30 17:40:02 +08002170 wifi_ParseProfile();
developerd14dff12023-06-28 22:47:44 +08002171 wifi_PrepareDefaultHostapdConfigs(FALSE);
developercc9a4f32023-05-30 17:40:02 +08002172 wifi_psk_file_reset();
developer33f13ba2023-07-12 16:19:06 +08002173 //v_secure_system("/usr/sbin/iw reg set US");
2174 v_secure_system("systemctl start hostapd.service");
developercc9a4f32023-05-30 17:40:02 +08002175 sleep(2);
developer8a3bbbf2023-03-15 17:47:23 +08002176
developercc9a4f32023-05-30 17:40:02 +08002177 wifi_vap_status_reset();
2178 wifi_radio_reset_count_reset();
developer4fe22c62023-08-03 13:23:58 +08002179 wifi_BringUpInterfaces();
developercc9a4f32023-05-30 17:40:02 +08002180 CallOnce = 0;
developera3511852023-06-14 14:12:59 +08002181 }
developer96b38512023-02-22 11:17:45 +08002182
developera3511852023-06-14 14:12:59 +08002183 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002184
developera3511852023-06-14 14:12:59 +08002185 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002186}
2187
2188/* wifi_reset() function */
2189/**
2190* Description: Resets the Wifi subsystem. This includes reset of all AP varibles.
developer69b61b02023-03-07 17:17:44 +08002191* Implementation specifics may dictate what is actualy reset since
developer72fb0bb2023-01-11 09:46:29 +08002192* different hardware implementations may have different requirements.
2193* Parameters : None
2194*
2195* @return The status of the operation.
2196* @retval RETURN_OK if successful.
2197* @retval RETURN_ERR if any error is detected
2198*
2199* @execution Synchronous.
2200* @sideeffect None.
2201*
2202* @note This function must not suspend and must not invoke any blocking system
2203* calls. It should probably just send a message to a driver event handler task.
2204*
2205*/
2206INT wifi_reset()
2207{
developer17038e62023-03-02 14:43:43 +08002208
developera3511852023-06-14 14:12:59 +08002209 wifi_BringDownInterfaces();
2210 sleep(2);
developer17038e62023-03-02 14:43:43 +08002211
developera3511852023-06-14 14:12:59 +08002212 //TODO: resets the wifi subsystem, deletes all APs
developer33f13ba2023-07-12 16:19:06 +08002213 v_secure_system("systemctl stop hostapd.service");
developera3511852023-06-14 14:12:59 +08002214 sleep(2);
developer17038e62023-03-02 14:43:43 +08002215
developer33f13ba2023-07-12 16:19:06 +08002216 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +08002217 sleep(5);
developer17038e62023-03-02 14:43:43 +08002218
developerd14dff12023-06-28 22:47:44 +08002219 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08002220 wifi_psk_file_reset();
developer4fe22c62023-08-03 13:23:58 +08002221 wifi_BringUpInterfaces();
developera3511852023-06-14 14:12:59 +08002222 sleep(2);
developer8a3bbbf2023-03-15 17:47:23 +08002223
2224 wifi_vap_status_reset();
2225
developera3511852023-06-14 14:12:59 +08002226 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002227}
2228
2229/* wifi_down() function */
2230/**
2231* @description Turns off transmit power for the entire Wifi subsystem, for all radios.
developer69b61b02023-03-07 17:17:44 +08002232* Implementation specifics may dictate some functionality since
developer72fb0bb2023-01-11 09:46:29 +08002233* different hardware implementations may have different requirements.
2234*
2235* @param None
2236*
2237* @return The status of the operation
2238* @retval RETURN_OK if successful
2239* @retval RETURN_ERR if any error is detected
2240*
2241* @execution Synchronous
2242* @sideeffect None
2243*
2244* @note This function must not suspend and must not invoke any blocking system
2245* calls. It should probably just send a message to a driver event handler task.
2246*
2247*/
2248INT wifi_down()
2249{
developera3511852023-06-14 14:12:59 +08002250 //TODO: turns off transmit power for the entire Wifi subsystem, for all radios
2251 int max_num_radios = 0;
developerb2977562023-05-24 17:54:12 +08002252 wifi_getMaxRadioNumber(&max_num_radios);
developer17038e62023-03-02 14:43:43 +08002253
developerb2977562023-05-24 17:54:12 +08002254 for (int radioIndex = 0; radioIndex < max_num_radios; radioIndex++)
2255 wifi_setRadioEnable(radioIndex, FALSE);
2256
developera3511852023-06-14 14:12:59 +08002257 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002258}
2259
2260
2261/* wifi_createInitialConfigFiles() function */
2262/**
2263* @description This function creates wifi configuration files. The format
developer69b61b02023-03-07 17:17:44 +08002264* and content of these files are implementation dependent. This function call is
2265* used to trigger this task if necessary. Some implementations may not need this
2266* function. If an implementation does not need to create config files the function call can
developer72fb0bb2023-01-11 09:46:29 +08002267* do nothing and return RETURN_OK.
2268*
2269* @param None
2270*
2271* @return The status of the operation
2272* @retval RETURN_OK if successful
2273* @retval RETURN_ERR if any error is detected
2274*
2275* @execution Synchronous
2276* @sideeffect None
2277*
2278* @note This function must not suspend and must not invoke any blocking system
2279* calls. It should probably just send a message to a driver event handler task.
2280*
2281*/
2282INT wifi_createInitialConfigFiles()
2283{
developera3511852023-06-14 14:12:59 +08002284 //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)
2285 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002286}
2287
developer7e4a2a62023-04-06 19:56:03 +08002288/* outputs the country code to a max 64 character string */
developer72fb0bb2023-01-11 09:46:29 +08002289INT wifi_getRadioCountryCode(INT radioIndex, CHAR *output_string)
2290{
developera3511852023-06-14 14:12:59 +08002291 int ret;
developer72fb0bb2023-01-11 09:46:29 +08002292
developera3511852023-06-14 14:12:59 +08002293 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002294
developera3511852023-06-14 14:12:59 +08002295 ret = wifi_BandProfileRead(0, radioIndex, "CountryCode", output_string, 64, NULL);
2296 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08002297 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead CountryCode failed\n");
developera3511852023-06-14 14:12:59 +08002298 return RETURN_ERR;
2299 }
developer7e4a2a62023-04-06 19:56:03 +08002300
developera3511852023-06-14 14:12:59 +08002301 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002302
developera3511852023-06-14 14:12:59 +08002303 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002304}
2305
2306INT wifi_setRadioCountryCode(INT radioIndex, CHAR *CountryCode)
2307{
developer7e4a2a62023-04-06 19:56:03 +08002308 /*Set wifi config. Wait for wifi reset to apply*/
developer7e4a2a62023-04-06 19:56:03 +08002309 struct params params;
2310 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002311 int ret = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002312
developer7e4a2a62023-04-06 19:56:03 +08002313 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002314
developer7e4a2a62023-04-06 19:56:03 +08002315 if(NULL == CountryCode || strlen(CountryCode) >= 32 ) {
2316 printf("%s: input para error!!!\n", __func__);
2317 return RETURN_ERR;
2318 }
developer72fb0bb2023-01-11 09:46:29 +08002319
developerc79e9172023-06-06 19:48:03 +08002320 if (!strlen(CountryCode)) {
2321 memcpy(CountryCode, "US", strlen("US")); /*default set the code to US*/
2322 CountryCode[2] = '\0';
2323 }
developer72fb0bb2023-01-11 09:46:29 +08002324
developer7e4a2a62023-04-06 19:56:03 +08002325 params.name = "country_code";
2326 params.value = CountryCode;
developer72fb0bb2023-01-11 09:46:29 +08002327
developere40952c2023-06-15 18:46:43 +08002328 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, radioIndex);
2329 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
2330 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2331 return RETURN_ERR;
2332 }
developer7e4a2a62023-04-06 19:56:03 +08002333 ret = wifi_hostapdWrite(config_file, &params, 1);
2334
2335 if (ret) {
2336 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n",
2337 __func__, ret);
2338 }
2339
2340 ret = wifi_hostapdProcessUpdate(radioIndex, &params, 1);
2341
2342 if (ret) {
2343 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n",
2344 __func__, ret);
2345 }
2346
2347 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
2348
2349 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002350}
2351
2352INT wifi_getRadioChannelStats2(INT radioIndex, wifi_channelStats2_t *outputChannelStats2)
2353{
developera3511852023-06-14 14:12:59 +08002354 char interface_name[16] = {0};
2355 char channel_util_file[64] = {0};
2356 char cmd[128] = {0};
2357 char buf[128] = {0};
2358 char *line = NULL;
2359 char *param = NULL, *value = NULL;
developere40952c2023-06-15 18:46:43 +08002360 int read = 0, res;
developera3511852023-06-14 14:12:59 +08002361 unsigned int ActiveTime = 0, BusyTime = 0, TransmitTime = 0;
developer86035662023-06-28 19:21:12 +08002362 unsigned long preActiveTime = 0, preBusyTime = 0, preTransmitTime = 0;
developerc14d83a2023-06-29 20:09:42 +08002363 long int rssi;
2364 long int tmp_l;
2365 unsigned long tmp_ul;
developera3511852023-06-14 14:12:59 +08002366 size_t len = 0;
2367 FILE *f = NULL;
developer72fb0bb2023-01-11 09:46:29 +08002368
developera3511852023-06-14 14:12:59 +08002369 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002370
developera3511852023-06-14 14:12:59 +08002371 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2372 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002373 res = snprintf(cmd, sizeof(cmd), "iw %s scan | grep signal | awk '{print $2}' | sort -n | tail -n1", interface_name);
2374 if (os_snprintf_error(sizeof(cmd), res)) {
2375 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2376 return RETURN_ERR;
2377 }
2378
developera3511852023-06-14 14:12:59 +08002379 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08002380 if (hal_strtol(buf, 10, &rssi) < 0) {
2381 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2382 }
2383 outputChannelStats2->ch_Max80211Rssi = rssi;
developer72fb0bb2023-01-11 09:46:29 +08002384
developera3511852023-06-14 14:12:59 +08002385 memset(cmd, 0, sizeof(cmd));
2386 memset(buf, 0, sizeof(buf));
developere40952c2023-06-15 18:46:43 +08002387 res = snprintf(cmd, sizeof(cmd), "iw %s survey dump | grep 'in use' -A6", interface_name);
2388 if (os_snprintf_error(sizeof(cmd), res)) {
2389 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2390 return RETURN_ERR;
2391 }
developera3511852023-06-14 14:12:59 +08002392 if ((f = popen(cmd, "r")) == NULL) {
2393 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
2394 return RETURN_ERR;
2395 }
developer72fb0bb2023-01-11 09:46:29 +08002396
developera3511852023-06-14 14:12:59 +08002397 read = getline(&line, &len, f);
2398 while (read != -1) {
2399 param = strtok(line, ":\t");
developerc14d83a2023-06-29 20:09:42 +08002400 if (!param) {
2401 read = getline(&line, &len, f);
2402 continue;
2403 }
developera3511852023-06-14 14:12:59 +08002404 value = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +08002405 if (!value) {
developer37646972023-06-29 10:58:43 +08002406 read = getline(&line, &len, f);
2407 continue;
2408 }
developera3511852023-06-14 14:12:59 +08002409 if(strstr(param, "frequency") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002410 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2411 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2412 }
2413 outputChannelStats2->ch_Frequency = tmp_ul;
developera3511852023-06-14 14:12:59 +08002414 }
2415 if(strstr(param, "noise") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002416 if (hal_strtol(value, 10, &tmp_l) < 0) {
2417 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2418 }
2419 outputChannelStats2->ch_NoiseFloor = tmp_l;
developer37646972023-06-29 10:58:43 +08002420
developerc14d83a2023-06-29 20:09:42 +08002421 if (hal_strtol(value, 10, &tmp_l) < 0) {
2422 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2423 }
2424 outputChannelStats2->ch_Non80211Noise = tmp_l;
developera3511852023-06-14 14:12:59 +08002425 }
2426 if(strstr(param, "channel active time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002427 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2428 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2429 }
2430 ActiveTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002431 }
developer5b23cd02023-07-19 20:26:03 +08002432 if(strstr(param, "channel busy time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002433 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2434 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2435 }
2436 BusyTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002437 }
2438 if(strstr(param, "channel transmit time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002439 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2440 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2441 }
2442 TransmitTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002443 }
2444 read = getline(&line, &len, f);
2445 }
2446 pclose(f);
developer72fb0bb2023-01-11 09:46:29 +08002447
developera3511852023-06-14 14:12:59 +08002448 // The file should store the last active, busy and transmit time
developere40952c2023-06-15 18:46:43 +08002449 res = snprintf(channel_util_file, sizeof(channel_util_file), "%s%d.txt", CHANNEL_STATS_FILE, radioIndex);
2450 if (os_snprintf_error(sizeof(channel_util_file), res)) {
2451 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2452 return RETURN_ERR;
2453 }
2454
developera3511852023-06-14 14:12:59 +08002455 f = fopen(channel_util_file, "r");
2456 if (f != NULL) {
2457 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002458 if (hal_strtoul(line, 10, &preActiveTime) < 0) {
2459 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002460 }
developera3511852023-06-14 14:12:59 +08002461 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002462 if (hal_strtoul(line, 10, &preBusyTime) < 0) {
2463 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerb6f05602023-06-30 11:32:41 +08002464
developer86035662023-06-28 19:21:12 +08002465 }
developera3511852023-06-14 14:12:59 +08002466 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002467 if (hal_strtoul(line, 10, &preTransmitTime) < 0) {
2468 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002469 }
developer5b23cd02023-07-19 20:26:03 +08002470
developerc14d83a2023-06-29 20:09:42 +08002471 if (fclose(f) == EOF)
2472 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developer5b23cd02023-07-19 20:26:03 +08002473
developerc14d83a2023-06-29 20:09:42 +08002474 }
2475
developerb14b3462023-07-01 18:02:42 +08002476 /*if (ActiveTime == preActiveTime) {
developer5b23cd02023-07-19 20:26:03 +08002477 wifi_debug(DEBUG_ERROR, "error:ActiveTime[%d] == preActiveTime[%ld]\n",
developerb14b3462023-07-01 18:02:42 +08002478 ActiveTime, preActiveTime);
developerc14d83a2023-06-29 20:09:42 +08002479 return RETURN_ERR;
developerb14b3462023-07-01 18:02:42 +08002480 }*/
developer72fb0bb2023-01-11 09:46:29 +08002481
developera3511852023-06-14 14:12:59 +08002482 outputChannelStats2->ch_ObssUtil = (BusyTime - preBusyTime)*100/(ActiveTime - preActiveTime);
2483 outputChannelStats2->ch_SelfBssUtil = (TransmitTime - preTransmitTime)*100/(ActiveTime - preActiveTime);
developer72fb0bb2023-01-11 09:46:29 +08002484
developera3511852023-06-14 14:12:59 +08002485 f = fopen(channel_util_file, "w");
2486 if (f != NULL) {
developer86035662023-06-28 19:21:12 +08002487 if (fprintf(f, "%u\n%u\n%u\n", ActiveTime, BusyTime, TransmitTime) < 0) {
2488 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
2489 }
2490 if (fclose(f) != 0) {
2491 wifi_debug(DEBUG_ERROR, "fclose fail\n");
2492 }
developera3511852023-06-14 14:12:59 +08002493 }
2494 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2495 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002496}
2497
2498/**********************************************************************************
2499 *
2500 * Wifi radio level function prototypes
2501 *
2502**********************************************************************************/
2503
2504//Get the total number of radios in this wifi subsystem
2505INT wifi_getRadioNumberOfEntries(ULONG *output) //Tr181
2506{
developera3511852023-06-14 14:12:59 +08002507 if (NULL == output)
2508 return RETURN_ERR;
2509 *output = MAX_NUM_RADIOS;
developer72fb0bb2023-01-11 09:46:29 +08002510
developera3511852023-06-14 14:12:59 +08002511 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002512}
2513
developer69b61b02023-03-07 17:17:44 +08002514//Get the total number of SSID entries in this wifi subsystem
developer72fb0bb2023-01-11 09:46:29 +08002515INT wifi_getSSIDNumberOfEntries(ULONG *output) //Tr181
2516{
developera3511852023-06-14 14:12:59 +08002517 if (NULL == output)
2518 return RETURN_ERR;
2519 *output = MAX_APS;
developer72fb0bb2023-01-11 09:46:29 +08002520
developera3511852023-06-14 14:12:59 +08002521 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002522}
2523
2524//Get the Radio enable config parameter
developera3511852023-06-14 14:12:59 +08002525INT wifi_getRadioEnable(INT radioIndex, BOOL *output_bool) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002526{
developer56fbedb2023-05-30 16:47:05 +08002527 char interface_name[16] = {0};
2528 char buf[128] = {0}, cmd[128] = {0};
2529 int apIndex;
2530 int max_radio_num = 0;
developer75bd10c2023-06-27 11:34:08 +08002531 int res;
developer3a85ab82023-05-25 11:59:38 +08002532
developer56fbedb2023-05-30 16:47:05 +08002533 if (NULL == output_bool)
2534 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002535
developer56fbedb2023-05-30 16:47:05 +08002536 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08002537
developer56fbedb2023-05-30 16:47:05 +08002538 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002539
developer56fbedb2023-05-30 16:47:05 +08002540 if (radioIndex >= max_radio_num)
2541 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002542
developer56fbedb2023-05-30 16:47:05 +08002543 /* loop all interface in radio, if any is enable, reture true, else return false */
2544 for(apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num)
2545 {
2546 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2547 continue;
2548 memset(cmd, 0, sizeof(cmd));
developer75bd10c2023-06-27 11:34:08 +08002549 res = snprintf(cmd, sizeof(cmd), "ifconfig %s 2> /dev/null | grep UP", interface_name);
2550 if (os_snprintf_error(sizeof(cmd), res)) {
2551 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2552 return RETURN_ERR;
2553 }
developer56fbedb2023-05-30 16:47:05 +08002554 *output_bool = _syscmd(cmd, buf, sizeof(buf)) ? FALSE : TRUE;
2555 if (*output_bool == TRUE)
2556 break;
2557 }
2558
2559 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002560}
2561
developere82c0ca2023-05-10 16:25:35 +08002562typedef long time_t;
2563static time_t radio_up_time[MAX_NUM_RADIOS];
2564
developer72fb0bb2023-01-11 09:46:29 +08002565INT wifi_setRadioEnable(INT radioIndex, BOOL enable)
2566{
developera3511852023-06-14 14:12:59 +08002567 char interface_name[16] = {0};
2568 char cmd[MAX_CMD_SIZE] = {0};
2569 char buf[MAX_BUF_SIZE] = {0};
2570 int apIndex;
2571 int max_radio_num = 0;
developere40952c2023-06-15 18:46:43 +08002572 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002573
developera3511852023-06-14 14:12:59 +08002574 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002575
developera3511852023-06-14 14:12:59 +08002576 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08002577
developera3511852023-06-14 14:12:59 +08002578 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002579
developera3511852023-06-14 14:12:59 +08002580 if(enable == FALSE) {
developer47cc27a2023-05-17 23:09:58 +08002581
2582 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2583 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002584
2585 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw REMOVE %s", interface_name);
2586 if (os_snprintf_error(sizeof(cmd), res)) {
2587 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2588 return RETURN_ERR;
2589 }
developer47cc27a2023-05-17 23:09:58 +08002590
developer8a3bbbf2023-03-15 17:47:23 +08002591 _syscmd(cmd, buf, sizeof(buf));
developer56fbedb2023-05-30 16:47:05 +08002592 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08002593 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s down", interface_name);
2594 if (os_snprintf_error(sizeof(cmd), res)) {
2595 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2596 return RETURN_ERR;
2597 }
developer56fbedb2023-05-30 16:47:05 +08002598 _syscmd(cmd, buf, sizeof(buf));
developere82c0ca2023-05-10 16:25:35 +08002599 if(strncmp(buf, "OK", 2))
developer75bd10c2023-06-27 11:34:08 +08002600 wifi_debug(DEBUG_ERROR, "Could not detach %s from hostapd daemon", interface_name);
developer8a3bbbf2023-03-15 17:47:23 +08002601 } else {
developere82c0ca2023-05-10 16:25:35 +08002602 for (apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num) {
developera3511852023-06-14 14:12:59 +08002603 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2604 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002605
developer8a3bbbf2023-03-15 17:47:23 +08002606 memset(cmd, 0, MAX_CMD_SIZE);
2607 memset(buf, 0, MAX_BUF_SIZE);
2608
developere40952c2023-06-15 18:46:43 +08002609 res = snprintf(cmd, sizeof(cmd), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
2610 if (os_snprintf_error(sizeof(cmd), res)) {
2611 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2612 return RETURN_ERR;
2613 }
developera3511852023-06-14 14:12:59 +08002614 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002615
developera3511852023-06-14 14:12:59 +08002616 if(*buf == '1') {
developere40952c2023-06-15 18:46:43 +08002617 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s up", interface_name);
2618 if (os_snprintf_error(sizeof(cmd), res)) {
2619 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2620 return RETURN_ERR;
2621 }
developer56fbedb2023-05-30 16:47:05 +08002622 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002623
2624 memset(cmd, 0, MAX_CMD_SIZE);
2625 memset(buf, 0, MAX_BUF_SIZE);
2626
developere40952c2023-06-15 18:46:43 +08002627 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw ADD bss_config=phy%d:/nvram/hostapd%d.conf",
developera3511852023-06-14 14:12:59 +08002628 phyId, apIndex);
developere40952c2023-06-15 18:46:43 +08002629 if (os_snprintf_error(sizeof(cmd), res)) {
2630 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2631 return RETURN_ERR;
2632 }
developera3511852023-06-14 14:12:59 +08002633 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002634
developera3511852023-06-14 14:12:59 +08002635 }
2636 }
developere75ba632023-06-29 16:03:33 +08002637 if (time(&radio_up_time[radioIndex]) < 0) {
2638 wifi_debug(DEBUG_ERROR, "GET time fail\n");
2639 return RETURN_ERR;
2640 }
developera3511852023-06-14 14:12:59 +08002641 }
developer72fb0bb2023-01-11 09:46:29 +08002642
developera3511852023-06-14 14:12:59 +08002643 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2644 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002645}
2646
2647//Get the Radio enable status
2648INT wifi_getRadioStatus(INT radioIndex, BOOL *output_bool) //RDKB
2649{
developera3511852023-06-14 14:12:59 +08002650 if (NULL == output_bool)
2651 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002652
developera3511852023-06-14 14:12:59 +08002653 return wifi_getRadioEnable(radioIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08002654}
2655
2656//Get the Radio Interface name from platform, eg "wlan0"
2657INT wifi_getRadioIfName(INT radioIndex, CHAR *output_string) //Tr181
2658{
developera3511852023-06-14 14:12:59 +08002659 if (NULL == output_string || radioIndex>=MAX_NUM_RADIOS || radioIndex<0)
2660 return RETURN_ERR;
2661 return wifi_GetInterfaceName(radioIndex, output_string);
developer72fb0bb2023-01-11 09:46:29 +08002662}
2663
developer6e578302023-06-21 10:11:16 +08002664int mtk_get_vow_info_callback(struct nl_msg *msg, void *data)
2665{
2666 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2667 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX + 1];
2668 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2669 int err = 0;
2670 struct vow_info *vow_info = NULL;
2671 struct mtk_nl80211_cb_data *cb_data = data;
2672
2673 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2674 genlmsg_attrlen(gnlh, 0), NULL);
2675 if (err < 0) {
2676 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
2677 return err;
2678 }
2679
2680 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2681 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX,
2682 tb[NL80211_ATTR_VENDOR_DATA], NULL);
2683 if (err < 0){
2684 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX fails\n");
2685 return err;
2686 }
2687
2688 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]) {
2689 vow_info = (struct vow_info *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]);
2690 memmove(cb_data->out_buf, vow_info, sizeof(struct vow_info));
2691 }
2692 }
2693
2694 return 0;
2695}
2696
2697INT mtk_wifi_set_air_time_management(
2698 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
2699 char* data, INT len, void *output)
2700{
2701 char inf_name[IF_NAME_SIZE] = {0};
2702 unsigned int if_idx = 0;
2703 int ret = -1;
2704 struct unl unl_ins;
2705 struct nl_msg *msg = NULL;
2706 struct nlattr * msg_data = NULL;
2707 struct mtk_nl80211_param param;
2708
2709 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
2710 return RETURN_ERR;
2711 if_idx = if_nametoindex(inf_name);
2712 if (!if_idx) {
2713 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
2714 return RETURN_ERR;
2715 }
2716 /*init mtk nl80211 vendor cmd*/
2717 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_VOW;
2718 param.if_type = NL80211_ATTR_IFINDEX;
2719 param.if_idx = if_idx;
2720
2721 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
2722 if (ret) {
2723 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
2724 return RETURN_ERR;
2725 }
2726 /*add mtk vendor cmd data*/
2727 if (nla_put(msg, vendor_data_attr, len, data)) {
2728 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
2729 nlmsg_free(msg);
2730 goto err;
2731 }
2732
2733 /*send mtk nl80211 vendor msg*/
2734 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output);
2735 if (ret) {
2736 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
2737 goto err;
2738 }
2739 /*deinit mtk nl80211 vendor msg*/
2740 mtk_nl80211_deint(&unl_ins);
2741 wifi_debug(DEBUG_INFO, "send cmd success.\n");
2742
2743 return RETURN_OK;
2744err:
2745 mtk_nl80211_deint(&unl_ins);
2746 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
2747 return RETURN_ERR;
2748}
2749
2750//Get the ATM(Air Time Management) Capable.
2751INT wifi_getATMCapable(BOOL *output_bool)
2752{
2753 if (NULL == output_bool)
2754 return RETURN_ERR;
2755 *output_bool = TRUE;
2756
2757 return RETURN_OK;
2758}
2759
2760INT wifi_setATMEnable(BOOL enable)
2761{
2762 int max_radio_num = 0;
2763 int radio_idx = 0;
2764 char dat_file[MAX_BUF_SIZE] = {0};
2765 int res;
2766 struct params params[2];
2767
2768 wifi_getMaxRadioNumber(&max_radio_num);
2769 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2770 if (mtk_wifi_set_air_time_management
2771 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO,
2772 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2773 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2774 return RETURN_ERR;
2775 }
2776
2777 if (mtk_wifi_set_air_time_management
2778 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_BW_EN_INFO,
2779 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2780 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2781 return RETURN_ERR;
2782 }
2783
2784 params[0].name = "VOW_Airtime_Fairness_En";
2785 params[0].value = enable ? "1" : "0";
2786 params[1].name = "VOW_BW_Ctrl";
2787 params[1].value = enable ? "1" : "0";
2788
2789 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
2790 if (os_snprintf_error(sizeof(dat_file), res)) {
2791 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2792 return RETURN_ERR;
2793 }
2794 wifi_datfileWrite(dat_file, params, 2);
2795 }
2796
2797 return RETURN_OK;
2798}
2799
2800INT wifi_getATMEnable(BOOL *output_enable)
2801{
2802 int max_radio_num = 0;
2803 int radio_idx = 0;
2804 struct vow_info vow_info;
2805 struct vow_info get_vow_info;
2806 struct mtk_nl80211_cb_data cb_data;
2807
2808 if (output_enable == NULL)
2809 return RETURN_ERR;
2810
2811 wifi_getMaxRadioNumber(&max_radio_num);
2812
2813 *output_enable = FALSE;
2814
2815 memset(&vow_info, 0, sizeof(struct vow_info));
2816
2817 cb_data.out_buf = (char *)&vow_info;
2818 cb_data.out_len = sizeof(struct vow_info);
2819
2820 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2821 if (mtk_wifi_set_air_time_management
2822 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2823 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2824 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2825 return RETURN_ERR;
2826 }
2827
2828 if (vow_info.atf_en == TRUE || vow_info.bw_en == TRUE) {
2829 *output_enable = TRUE;
2830 break;
2831 }
2832 }
2833
2834 return RETURN_OK;
2835}
2836
2837UINT apidx_to_group(INT apIndex)
2838{
2839 int max_radio_num = 0;
2840 unsigned int group = 0;
2841
2842 wifi_getMaxRadioNumber(&max_radio_num);
developerc14d83a2023-06-29 20:09:42 +08002843 if (max_radio_num == 0) {
2844 wifi_debug(DEBUG_ERROR, "invalid max radio num\n");
2845 return 0;
2846 }
developer6e578302023-06-21 10:11:16 +08002847 group = apIndex / max_radio_num + 5 * (apIndex % max_radio_num);
2848
2849 return group;
2850}
2851
2852INT wifi_setApATMAirTimePercent(INT apIndex, UINT ap_AirTimePercent)
2853{
2854 struct vow_group_en_param atc_en_param;
2855 struct vow_ratio_param radio_param;
2856 unsigned int group = 0;
2857 //BOOL ATM_enable = FALSE;
2858
2859 if (ap_AirTimePercent < 5 || ap_AirTimePercent > 100) {
2860 wifi_debug(DEBUG_ERROR, "invalid ait time percent!\n");
2861 return RETURN_ERR;
2862 }
2863
2864 /* mt7990 support 15 group now*/
2865 group = apidx_to_group(apIndex);
2866
2867 if (group > 15) {
2868 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2869 return RETURN_ERR;
2870 }
2871
2872 atc_en_param.group = group;
2873 atc_en_param.en = 1;
2874 if (mtk_wifi_set_air_time_management
2875 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO,
2876 NULL, (char *)&atc_en_param, sizeof(struct vow_group_en_param), NULL)!= RETURN_OK) {
2877 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO cmd fails\n");
2878 return RETURN_ERR;
2879 }
2880
2881 radio_param.group = group;
2882 radio_param.ratio = ap_AirTimePercent;
2883 if (mtk_wifi_set_air_time_management
2884 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO,
2885 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2886 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO cmd fails\n");
2887 return RETURN_ERR;
2888 }
2889
2890 if (mtk_wifi_set_air_time_management
2891 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO,
2892 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2893 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO cmd fails\n");
2894 return RETURN_ERR;
2895 }
2896
2897 return RETURN_OK;
2898}
2899
2900INT wifi_getApATMAirTimePercent(INT apIndex, UINT *output_ap_AirTimePercent)
2901{
2902 unsigned int group = 0;
2903 struct vow_info get_vow_info, vow_info;
2904 struct mtk_nl80211_cb_data cb_data;
2905
2906 if (output_ap_AirTimePercent == NULL)
2907 return RETURN_ERR;
2908
2909 group = apidx_to_group(apIndex);
2910 if (group > 15) {
2911 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2912 return RETURN_ERR;
2913 }
2914
2915 memset(&vow_info, 0, sizeof(struct vow_info));
2916 memset(&get_vow_info, 0, sizeof(struct vow_info));
2917
2918 cb_data.out_buf = (char *)&vow_info;
2919 cb_data.out_len = sizeof(struct vow_info);
2920
2921 get_vow_info.group = group;
2922
2923 if (mtk_wifi_set_air_time_management
2924 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2925 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2926 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2927 return RETURN_ERR;
2928 }
2929
2930 *output_ap_AirTimePercent = vow_info.ratio;
2931
2932 return RETURN_ERR;
2933}
2934
developerd14dff12023-06-28 22:47:44 +08002935INT wifi_getApATMSta(INT apIndex, UCHAR *output_sta_MAC_ATM_array, UINT buf_size)
2936{
2937 ULONG dev_num = 0;
2938 struct vow_info vow_info;
2939 struct vow_info get_vow_info;
2940 struct mtk_nl80211_cb_data cb_data;
2941 unsigned int percent;
2942 char assocArray[MAX_BUF_SIZE] = {0};
2943 char *mac = NULL;
2944 unsigned char output_len = 0;
2945 int res;
2946 char buf[MAX_BUF_SIZE] = {0};
2947
2948 memset(&vow_info, 0, sizeof(struct vow_info));
2949 memset(&get_vow_info, 0, sizeof(struct vow_info));
2950
2951 cb_data.out_buf = (char *)&vow_info;
2952 cb_data.out_len = sizeof(struct vow_info);
2953
2954 if (mtk_wifi_set_air_time_management
2955 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2956 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2957 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2958 return RETURN_ERR;
2959 }
2960
2961 if (vow_info.atf_en == FALSE) {
2962 wifi_debug(DEBUG_ERROR, "ATF disable!\n");
2963 return RETURN_ERR;
2964 }
2965
2966 if (wifi_getApNumDevicesAssociated(apIndex, &dev_num) != RETURN_OK) {
2967 wifi_debug(DEBUG_ERROR, "get sta num fail!\n");
2968 return RETURN_ERR;;
2969 }
2970
2971 percent = 100 / dev_num;
2972
2973 if (wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray)) != RETURN_OK){
2974 wifi_debug(DEBUG_ERROR, "get sta mac fail!\n");
2975 return RETURN_ERR;;
2976 }
2977
2978 memset(output_sta_MAC_ATM_array, 0, MAX_BUF_SIZE);
2979
2980 mac = strtok(assocArray, "\n");
2981 while (mac != NULL) {
2982 if (strlen(mac) >= 17) {
2983 res = snprintf(buf, sizeof(buf), "%s %d|", mac, percent);
2984
2985 if (os_snprintf_error(sizeof(buf), res)) {
2986 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2987 return RETURN_ERR;
2988 }
2989
2990 if (output_len + strlen(buf) > buf_size)
2991 break;
2992
2993 strncat((char *)output_sta_MAC_ATM_array, buf, strlen(buf));
2994
2995 output_len += strlen(buf);
2996 }
2997
2998 mac = strtok(NULL, "\n");
2999 }
3000
3001 /* Remove the last | */
3002 if (strlen((char *)output_sta_MAC_ATM_array) != 0)
3003 output_sta_MAC_ATM_array[strlen((char *)output_sta_MAC_ATM_array)-1] = '\0';
3004
3005 return RETURN_OK;
3006}
3007
3008INT wifi_setApATMSta(INT apIndex, UCHAR *sta_MAC_ATM_array, UINT ap_AirTimePercent)
3009{
3010 return RETURN_ERR;
3011}
developer9ce44382023-06-28 11:09:37 +08003012
developer72fb0bb2023-01-11 09:46:29 +08003013//Get the maximum PHY bit rate supported by this interface. eg: "216.7 Mb/s", "1.3 Gb/s"
3014//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.
3015INT wifi_getRadioMaxBitRate(INT radioIndex, CHAR *output_string) //RDKB
3016{
developera3511852023-06-14 14:12:59 +08003017 // The formula to coculate bit rate is "Subcarriers * Modulation * Coding rate * Spatial stream / (Data interval + Guard interval)"
3018 // For max bit rate, we should always choose the best MCS
3019 char mode[64] = {0};
3020 char channel_bandwidth_str[64] = {0};
3021 UINT mode_map = 0;
3022 UINT num_subcarrier = 0;
3023 UINT code_bits = 0;
3024 float code_rate = 0; // use max code rate
3025 int NSS = 0;
3026 UINT Symbol_duration = 0;
3027 UINT GI_duration = 0;
3028 wifi_guard_interval_t gi = wifi_guard_interval_auto;
3029 BOOL enable = FALSE;
3030 float bit_rate = 0;
developere40952c2023-06-15 18:46:43 +08003031 int ant_bitmap = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003032
developera3511852023-06-14 14:12:59 +08003033 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3034 if (NULL == output_string)
3035 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003036
developera3511852023-06-14 14:12:59 +08003037 wifi_getRadioEnable(radioIndex, &enable);
3038 if (enable == FALSE) {
developere40952c2023-06-15 18:46:43 +08003039 res = snprintf(output_string, 64, "0 Mb/s");
3040 if (os_snprintf_error(64, res)) {
3041 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3042 return RETURN_ERR;
3043 }
developera3511852023-06-14 14:12:59 +08003044 return RETURN_OK;
3045 }
developer72fb0bb2023-01-11 09:46:29 +08003046
developera3511852023-06-14 14:12:59 +08003047 if (wifi_getRadioMode(radioIndex, mode, &mode_map) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08003048 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +08003049 return RETURN_ERR;
3050 }
developer72fb0bb2023-01-11 09:46:29 +08003051
developera3511852023-06-14 14:12:59 +08003052 if (wifi_getGuardInterval(radioIndex, &gi) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08003053 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +08003054 return RETURN_ERR;
3055 }
developer72fb0bb2023-01-11 09:46:29 +08003056
developera3511852023-06-14 14:12:59 +08003057 if (gi == wifi_guard_interval_3200)
3058 GI_duration = 32;
3059 else if (gi == wifi_guard_interval_1600)
3060 GI_duration = 16;
3061 else if (gi == wifi_guard_interval_800)
3062 GI_duration = 8;
3063 else // auto, 400
3064 GI_duration = 4;
developer72fb0bb2023-01-11 09:46:29 +08003065
developera3511852023-06-14 14:12:59 +08003066 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, channel_bandwidth_str) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08003067 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error\n");
developera3511852023-06-14 14:12:59 +08003068 return RETURN_ERR;
3069 }
developer72fb0bb2023-01-11 09:46:29 +08003070
developera3511852023-06-14 14:12:59 +08003071 if (strstr(channel_bandwidth_str, "80+80") != NULL)
developer32f2a182023-06-27 19:50:41 +08003072 memcpy(channel_bandwidth_str, "160", strlen("160"));
developer72fb0bb2023-01-11 09:46:29 +08003073
developera3511852023-06-14 14:12:59 +08003074 if (mode_map & WIFI_MODE_AX) {
3075 if (strstr(channel_bandwidth_str, "160") != NULL)
3076 num_subcarrier = 1960;
3077 else if (strstr(channel_bandwidth_str, "80") != NULL)
3078 num_subcarrier = 980;
3079 else if (strstr(channel_bandwidth_str, "40") != NULL)
3080 num_subcarrier = 468;
3081 else if (strstr(channel_bandwidth_str, "20") != NULL)
3082 num_subcarrier = 234;
3083 code_bits = 10;
3084 code_rate = (float)5/6;
3085 Symbol_duration = 128;
3086 GI_duration = 8;/*HE no GI 400ns*/
3087 } else if (mode_map & WIFI_MODE_AC) {
3088 if (strstr(channel_bandwidth_str, "160") != NULL)
3089 num_subcarrier = 468;
3090 else if (strstr(channel_bandwidth_str, "80") != NULL)
3091 num_subcarrier = 234;
3092 else if (strstr(channel_bandwidth_str, "40") != NULL)
3093 num_subcarrier = 108;
3094 else if (strstr(channel_bandwidth_str, "20") != NULL)
3095 num_subcarrier = 52;
3096 code_bits = 8;
3097 code_rate = (float)5/6;
3098 Symbol_duration = 32;
3099 } else if (mode_map & WIFI_MODE_N) {
3100 if (strstr(channel_bandwidth_str, "160") != NULL)
3101 num_subcarrier = 468;
3102 else if (strstr(channel_bandwidth_str, "80") != NULL)
3103 num_subcarrier = 234;
3104 else if (strstr(channel_bandwidth_str, "40") != NULL)
3105 num_subcarrier = 108;
3106 else if (strstr(channel_bandwidth_str, "20") != NULL)
3107 num_subcarrier = 52;
3108 code_bits = 6;
3109 code_rate = (float)3/4;
3110 Symbol_duration = 32;
3111 } else if ((mode_map & WIFI_MODE_G || mode_map & WIFI_MODE_B) || mode_map & WIFI_MODE_A) {
3112 // mode b must run with mode g, so we output mode g bitrate in 2.4 G.
developere40952c2023-06-15 18:46:43 +08003113 res = snprintf(output_string, 64, "65 Mb/s");
3114 if (os_snprintf_error(64, res)) {
3115 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3116 return RETURN_ERR;
3117 }
developera3511852023-06-14 14:12:59 +08003118 return RETURN_OK;
3119 } else {
developere40952c2023-06-15 18:46:43 +08003120 res = snprintf(output_string, 64, "0 Mb/s");
3121 if (os_snprintf_error(64, res)) {
3122 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3123 return RETURN_ERR;
3124 }
developera3511852023-06-14 14:12:59 +08003125 return RETURN_OK;
3126 }
developer72fb0bb2023-01-11 09:46:29 +08003127
developera3511852023-06-14 14:12:59 +08003128 // Spatial streams
3129 if (wifi_getRadioTxChainMask(radioIndex, &ant_bitmap) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08003130 wifi_debug(DEBUG_ERROR, "wifi_getRadioTxChainMask return error\n");
developera3511852023-06-14 14:12:59 +08003131 return RETURN_ERR;
3132 }
3133 for (; ant_bitmap > 0; ant_bitmap >>= 1)
3134 NSS += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08003135
developera3511852023-06-14 14:12:59 +08003136 // multiple 10 is to align duration unit (0.1 us)
3137 bit_rate = (num_subcarrier * code_bits * code_rate * NSS) / (Symbol_duration + GI_duration) * 10;
developere40952c2023-06-15 18:46:43 +08003138 res = snprintf(output_string, 64, "%.1f Mb/s", bit_rate);
3139 if (os_snprintf_error(64, res)) {
3140 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3141 return RETURN_ERR;
3142 }
developera3511852023-06-14 14:12:59 +08003143 WIFI_ENTRY_EXIT_DEBUG("%s:num_subcarrier=%d, code_bits=%d, code_rate=%.3f, nss=%d, symbol time=%u, %.1f Mb/s\n",
3144 __func__, num_subcarrier, code_bits, code_rate, NSS, Symbol_duration + GI_duration, bit_rate);
3145 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003146
developera3511852023-06-14 14:12:59 +08003147 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003148}
developer72fb0bb2023-01-11 09:46:29 +08003149
3150//Get Supported frequency bands at which the radio can operate. eg: "2.4GHz,5GHz"
3151//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.
3152INT wifi_getRadioSupportedFrequencyBands(INT radioIndex, CHAR *output_string) //RDKB
3153{
developera3511852023-06-14 14:12:59 +08003154 wifi_band band = band_invalid;
developer72fb0bb2023-01-11 09:46:29 +08003155
developera3511852023-06-14 14:12:59 +08003156 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3157 if (NULL == output_string)
3158 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003159
developera3511852023-06-14 14:12:59 +08003160 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003161
developera3511852023-06-14 14:12:59 +08003162 memset(output_string, 0, 10);
3163 if (band == band_2_4)
developer32f2a182023-06-27 19:50:41 +08003164 memcpy(output_string, "2.4GHz", strlen("2.4GHz"));
developera3511852023-06-14 14:12:59 +08003165 else if (band == band_5)
developer32f2a182023-06-27 19:50:41 +08003166 memcpy(output_string, "5GHz", strlen("5GHz"));
developera3511852023-06-14 14:12:59 +08003167 else if (band == band_6)
developer32f2a182023-06-27 19:50:41 +08003168 memcpy(output_string, "6GHz", strlen("6GHz"));
developera3511852023-06-14 14:12:59 +08003169 else
3170 return RETURN_ERR;
3171 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003172
developera3511852023-06-14 14:12:59 +08003173 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003174}
3175
3176//Get the frequency band at which the radio is operating, eg: "2.4GHz"
3177//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.
3178INT wifi_getRadioOperatingFrequencyBand(INT radioIndex, CHAR *output_string) //Tr181
3179{
developera3511852023-06-14 14:12:59 +08003180 wifi_band band = band_invalid;
developer9ce44382023-06-28 11:09:37 +08003181 int res = -1;
developere40952c2023-06-15 18:46:43 +08003182
developera3511852023-06-14 14:12:59 +08003183 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3184 if (NULL == output_string)
3185 return RETURN_ERR;
3186 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003187
developera3511852023-06-14 14:12:59 +08003188 if (band == band_2_4)
developere40952c2023-06-15 18:46:43 +08003189 res = snprintf(output_string, 64, "2.4GHz");
developera3511852023-06-14 14:12:59 +08003190 else if (band == band_5)
developere40952c2023-06-15 18:46:43 +08003191 res = snprintf(output_string, 64, "5GHz");
developera3511852023-06-14 14:12:59 +08003192 else if (band == band_6)
developere40952c2023-06-15 18:46:43 +08003193 res = snprintf(output_string, 64, "6GHz");
developer72fb0bb2023-01-11 09:46:29 +08003194
developere40952c2023-06-15 18:46:43 +08003195 if (os_snprintf_error(64, res)) {
3196 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3197 return RETURN_ERR;
3198 }
developera3511852023-06-14 14:12:59 +08003199 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003200
developera3511852023-06-14 14:12:59 +08003201 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003202}
3203
3204//Get the Supported Radio Mode. eg: "b,g,n"; "n,ac"
3205//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.
3206INT wifi_getRadioSupportedStandards(INT radioIndex, CHAR *output_string) //Tr181
3207{
developera3511852023-06-14 14:12:59 +08003208 char cmd[128]={0};
3209 char buf[128]={0};
3210 char temp_output[128] = {0};
3211 wifi_band band;
developere40952c2023-06-15 18:46:43 +08003212 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003213
developera3511852023-06-14 14:12:59 +08003214 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3215 if (NULL == output_string)
3216 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003217
developera3511852023-06-14 14:12:59 +08003218 band = wifi_index_to_band(radioIndex);
3219 if (band == band_2_4) {
developer32f2a182023-06-27 19:50:41 +08003220 strncat(temp_output, "b,g,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003221 } else if (band == band_5) {
developer32f2a182023-06-27 19:50:41 +08003222 strncat(temp_output, "a,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003223 }
3224 phyId = radio_index_to_phy(radioIndex);
3225 // ht capabilities
developere40952c2023-06-15 18:46:43 +08003226 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep '[^PHY|MAC|VHT].Capabilities' | head -n 1 | cut -d ':' -f2 | sed 's/^.//' | tr -d '\\n'", phyId);
3227 if (os_snprintf_error(sizeof(cmd), res)) {
3228 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3229 return RETURN_ERR;
3230 }
developera3511852023-06-14 14:12:59 +08003231 _syscmd(cmd, buf, sizeof(buf));
3232 if (strlen(buf) >= 4 && strncmp(buf, "0x00", 4) != 0) {
developer32f2a182023-06-27 19:50:41 +08003233 if (strlen(temp_output) >= sizeof(temp_output) - 2)
3234 return RETURN_ERR;
3235 strncat(temp_output, "n,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003236 }
developer72fb0bb2023-01-11 09:46:29 +08003237
developera3511852023-06-14 14:12:59 +08003238 // vht capabilities
3239 if (band == band_5) {
developere40952c2023-06-15 18:46:43 +08003240 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'VHT Capabilities' | cut -d '(' -f2 | cut -c1-10 | tr -d '\\n'", phyId);
3241 if (os_snprintf_error(sizeof(cmd), res)) {
3242 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3243 return RETURN_ERR;
3244 }
3245 _syscmd(cmd, buf, sizeof(buf));
3246 if (strlen(buf) >= 10 && strncmp(buf, "0x00000000", 10) != 0) {
developer32f2a182023-06-27 19:50:41 +08003247 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3248 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08003249 strncat(temp_output, "ac,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003250 }
3251 }
developer72fb0bb2023-01-11 09:46:29 +08003252
developera3511852023-06-14 14:12:59 +08003253 // he capabilities
developere40952c2023-06-15 18:46:43 +08003254 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'HE MAC Capabilities' | head -n 2 | tail -n 1 | cut -d '(' -f2 | cut -c1-6 | tr -d '\\n'", phyId);
3255 if (os_snprintf_error(sizeof(cmd), res)) {
3256 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3257 return RETURN_ERR;
3258 }
developera3511852023-06-14 14:12:59 +08003259 _syscmd(cmd, buf, sizeof(buf));
3260 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003261 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3262 return RETURN_ERR;
3263 strncat(temp_output, "ax,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003264 }
developer72fb0bb2023-01-11 09:46:29 +08003265
developere82c0ca2023-05-10 16:25:35 +08003266 // eht capabilities
developere40952c2023-06-15 18:46:43 +08003267 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'EHT MAC Capabilities' | head -n 2 | tail -n 1 | cut -d '(' -f2 | cut -c1-6 | tr -d '\\n'", phyId);
3268 if (os_snprintf_error(sizeof(cmd), res)) {
3269 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3270 return RETURN_ERR;
3271 }
developera3511852023-06-14 14:12:59 +08003272 _syscmd(cmd, buf, sizeof(buf));
3273 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003274 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3275 return RETURN_ERR;
3276 strncat(temp_output, "be,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003277 }
developere82c0ca2023-05-10 16:25:35 +08003278
developera3511852023-06-14 14:12:59 +08003279 // Remove the last comma
3280 if (strlen(temp_output) != 0)
3281 temp_output[strlen(temp_output)-1] = '\0';
3282 strncpy(output_string, temp_output, strlen(temp_output));
3283 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3284 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003285}
3286
3287//Get the radio operating mode, and pure mode flag. eg: "ac"
3288//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.
3289INT wifi_getRadioStandard(INT radioIndex, CHAR *output_string, BOOL *gOnly, BOOL *nOnly, BOOL *acOnly) //RDKB
3290{
developere40952c2023-06-15 18:46:43 +08003291 int res;
3292
developera3511852023-06-14 14:12:59 +08003293 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3294 if (NULL == output_string)
3295 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003296
developera3511852023-06-14 14:12:59 +08003297 if (radioIndex == 0) {
developere40952c2023-06-15 18:46:43 +08003298 res = snprintf(output_string, 64, "n"); //"ht" needs to be translated to "n" or others
3299 if (os_snprintf_error(64, res)) {
3300 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3301 return RETURN_ERR;
3302 }
developera3511852023-06-14 14:12:59 +08003303 *gOnly = FALSE;
3304 *nOnly = TRUE;
3305 *acOnly = FALSE;
3306 } else {
developere40952c2023-06-15 18:46:43 +08003307 res = snprintf(output_string, 64, "ac"); //"vht" needs to be translated to "ac"
3308 if (os_snprintf_error(64, res)) {
3309 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3310 return RETURN_ERR;
3311 }
developera3511852023-06-14 14:12:59 +08003312 *gOnly = FALSE;
3313 *nOnly = FALSE;
3314 *acOnly = FALSE;
3315 }
3316 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003317
developera3511852023-06-14 14:12:59 +08003318 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003319}
3320
developer0f10c772023-05-16 21:43:39 +08003321enum WIFI_MODE {
3322 WMODE_INVALID = 0,
3323 WMODE_A = 1 << 0,
3324 WMODE_B = 1 << 1,
3325 WMODE_G = 1 << 2,
3326 WMODE_GN = 1 << 3,
3327 WMODE_AN = 1 << 4,
3328 WMODE_AC = 1 << 5,
3329 WMODE_AX_24G = 1 << 6,
3330 WMODE_AX_5G = 1 << 7,
3331 WMODE_AX_6G = 1 << 8,
3332 WMODE_BE_24G = 1 << 9,
3333 WMODE_BE_5G = 1 << 10,
3334 WMODE_BE_6G = 1 << 11,
3335 /*
3336 * total types of supported wireless mode,
3337 * add this value once yow add new type
3338 */
3339 WMODE_COMP = 12,
3340};
3341
3342#define RADIO_MODE_LEN 32
developerfead3972023-05-25 20:15:02 +08003343
3344int get_radio_mode_handler(struct nl_msg *msg, void *cb)
developer72fb0bb2023-01-11 09:46:29 +08003345{
developerfead3972023-05-25 20:15:02 +08003346 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3347 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08003348 struct genlmsghdr *gnlh;
developerfead3972023-05-25 20:15:02 +08003349 unsigned int *phymode;
3350 int err = 0;
3351 struct mtk_nl80211_cb_data *cb_data = cb;
developer72fb0bb2023-01-11 09:46:29 +08003352
developerfead3972023-05-25 20:15:02 +08003353 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08003354 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developerfead3972023-05-25 20:15:02 +08003355 return NL_SKIP;
3356 }
developerc14d83a2023-06-29 20:09:42 +08003357 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer72fb0bb2023-01-11 09:46:29 +08003358
developerfead3972023-05-25 20:15:02 +08003359 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3360 genlmsg_attrlen(gnlh, 0), NULL);
3361 if (err < 0) {
3362 wifi_debug(DEBUG_ERROR, "nla_parse radio nl80211 msg fails,error.\n");
3363 return NL_SKIP;
3364 }
developer0f10c772023-05-16 21:43:39 +08003365
developerfead3972023-05-25 20:15:02 +08003366 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3367 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
3368 tb[NL80211_ATTR_VENDOR_DATA], NULL);
3369 if (err < 0)
3370 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08003371
developerfead3972023-05-25 20:15:02 +08003372 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]) {
3373 phymode = (unsigned int *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]);
3374
3375 memset(cb_data->out_buf, 0, cb_data->out_len);
3376 memmove(cb_data->out_buf, phymode, sizeof(unsigned int));
3377 }
3378 } else
3379 wifi_debug(DEBUG_ERROR, "No Stats from driver.\n");
3380
3381 return NL_OK;
3382}
developer0f10c772023-05-16 21:43:39 +08003383
developerfead3972023-05-25 20:15:02 +08003384void phymode_to_puremode(INT radioIndex, CHAR *output_string, UINT *pureMode, UINT phymode)
3385{
3386 wifi_band band;
3387 unsigned char radio_mode_tem_len;
developere40952c2023-06-15 18:46:43 +08003388 int res;
developerfead3972023-05-25 20:15:02 +08003389
3390 band = wifi_index_to_band(radioIndex);
developer0f10c772023-05-16 21:43:39 +08003391 // puremode is a bit map
developera3511852023-06-14 14:12:59 +08003392 *pureMode = 0;
developer0f10c772023-05-16 21:43:39 +08003393 memset(output_string, 0, RADIO_MODE_LEN);
3394
3395 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3396
3397 switch (band) {
3398 case band_2_4:
3399 if (phymode & WMODE_B) {
developere40952c2023-06-15 18:46:43 +08003400 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "b,");
3401 if (os_snprintf_error(radio_mode_tem_len, res)) {
3402 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3403 return;
3404 }
developer0f10c772023-05-16 21:43:39 +08003405 *pureMode |= WIFI_MODE_B;
3406 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3407 }
3408 if (phymode & WMODE_G) {
developere40952c2023-06-15 18:46:43 +08003409 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "g,");
3410 if (os_snprintf_error(radio_mode_tem_len, res)) {
3411 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3412 return;
3413 }
developer0f10c772023-05-16 21:43:39 +08003414 *pureMode |= WIFI_MODE_G;
3415 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3416 }
3417 if (phymode & WMODE_GN) {
developere40952c2023-06-15 18:46:43 +08003418 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3419 if (os_snprintf_error(radio_mode_tem_len, res)) {
3420 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3421 return;
3422 }
developer0f10c772023-05-16 21:43:39 +08003423 *pureMode |= WIFI_MODE_N;
3424 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3425 }
3426 if (phymode & WMODE_AX_24G) {
developere40952c2023-06-15 18:46:43 +08003427 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3428 if (os_snprintf_error(radio_mode_tem_len, res)) {
3429 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3430 return;
3431 }
developer0f10c772023-05-16 21:43:39 +08003432 *pureMode |= WIFI_MODE_AX;
3433 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3434 }
3435 if (phymode & WMODE_BE_24G) {
developere40952c2023-06-15 18:46:43 +08003436 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3437 if (os_snprintf_error(radio_mode_tem_len, res)) {
3438 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3439 return;
3440 }
developer0f10c772023-05-16 21:43:39 +08003441 *pureMode |= WIFI_MODE_BE;
3442 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3443 }
3444 break;
3445 case band_5:
3446 if (phymode & WMODE_A) {
developere40952c2023-06-15 18:46:43 +08003447 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "a,");
3448 if (os_snprintf_error(radio_mode_tem_len, res)) {
3449 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3450 return;
3451 }
developer0f10c772023-05-16 21:43:39 +08003452 *pureMode |= WIFI_MODE_A;
3453 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3454 }
3455 if (phymode & WMODE_AN) {
developere40952c2023-06-15 18:46:43 +08003456 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3457 if (os_snprintf_error(radio_mode_tem_len, res)) {
3458 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3459 return;
3460 }
developer0f10c772023-05-16 21:43:39 +08003461 *pureMode |= WIFI_MODE_N;
3462 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3463 }
3464 if (phymode & WMODE_AC) {
developere40952c2023-06-15 18:46:43 +08003465 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ac,");
3466 if (os_snprintf_error(radio_mode_tem_len, res)) {
3467 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3468 return;
3469 }
developer0f10c772023-05-16 21:43:39 +08003470 *pureMode |= WIFI_MODE_AC;
3471 }
3472 if (phymode & WMODE_AX_5G) {
developere40952c2023-06-15 18:46:43 +08003473 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3474 if (os_snprintf_error(radio_mode_tem_len, res)) {
3475 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3476 return;
3477 }
developer0f10c772023-05-16 21:43:39 +08003478 *pureMode |= WIFI_MODE_AX;
3479 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3480 }
3481 if (phymode & WMODE_BE_5G) {
developere40952c2023-06-15 18:46:43 +08003482 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3483 if (os_snprintf_error(radio_mode_tem_len, res)) {
3484 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3485 return;
3486 }
developer0f10c772023-05-16 21:43:39 +08003487 *pureMode |= WIFI_MODE_BE;
3488 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3489 }
3490 break;
3491 case band_6:
3492 if (phymode & WMODE_AX_6G) {
developere40952c2023-06-15 18:46:43 +08003493 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3494 if (os_snprintf_error(radio_mode_tem_len, res)) {
3495 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3496 return;
3497 }
developer0f10c772023-05-16 21:43:39 +08003498 *pureMode |= WIFI_MODE_AX;
3499 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3500 }
3501 if (phymode & WMODE_BE_6G) {
developere40952c2023-06-15 18:46:43 +08003502 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3503 if (os_snprintf_error(radio_mode_tem_len, res)) {
3504 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3505 return;
3506 }
developer0f10c772023-05-16 21:43:39 +08003507 *pureMode |= WIFI_MODE_BE;
3508 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3509 }
3510 break;
3511 default:
developer86035662023-06-28 19:21:12 +08003512 wifi_debug(DEBUG_ERROR, "%s band_idx invalid\n", __func__);
developer0f10c772023-05-16 21:43:39 +08003513 break;
3514 }
3515
3516 /* Remove the last comma */
3517 if (strlen(output_string) != 0)
developera3511852023-06-14 14:12:59 +08003518 output_string[strlen(output_string)-1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +08003519
developerfead3972023-05-25 20:15:02 +08003520}
3521
3522INT wifi_getRadioMode(INT radioIndex, CHAR *output_string, UINT *pureMode)
3523{
3524 unsigned int phymode;
3525 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003526 int ret = -1;
3527 unsigned int if_idx = 0;
3528 struct unl unl_ins;
3529 struct nl_msg *msg = NULL;
3530 struct nlattr * msg_data = NULL;
3531 struct mtk_nl80211_param param;
3532 struct mtk_nl80211_cb_data cb_data;
3533
developera3511852023-06-14 14:12:59 +08003534 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3535 if (NULL == output_string || NULL == pureMode)
developerdaf24792023-06-06 11:40:04 +08003536 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003537
3538 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
3539 return RETURN_ERR;
3540
3541 if_idx = if_nametoindex(interface_name);
3542 if (!if_idx) {
3543 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
3544 return RETURN_ERR;
3545 }
3546 /*init mtk nl80211 vendor cmd*/
3547 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
3548 param.if_type = NL80211_ATTR_IFINDEX;
3549 param.if_idx = if_idx;
3550
3551 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3552 if (ret) {
3553 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3554 return RETURN_ERR;
3555 }
3556
3557 /*add mtk vendor cmd data*/
3558 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE, 0)) {
3559 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_WMODE attribute error\n");
3560 nlmsg_free(msg);
3561 goto err;
3562 }
3563
3564 /*send mtk nl80211 vendor msg*/
3565 cb_data.out_buf = (char *)&phymode;
3566 cb_data.out_len = sizeof(unsigned int);
3567
3568 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_radio_mode_handler, &cb_data);
3569
3570 if (ret) {
3571 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3572 goto err;
3573 }
3574 /*deinit mtk nl80211 vendor msg*/
3575 mtk_nl80211_deint(&unl_ins);
3576
3577 phymode_to_puremode(radioIndex, output_string, pureMode, phymode);
developer6e578302023-06-21 10:11:16 +08003578 wifi_debug(DEBUG_INFO,"send cmd success\n");
developerfead3972023-05-25 20:15:02 +08003579
developera3511852023-06-14 14:12:59 +08003580 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3581 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003582err:
3583 mtk_nl80211_deint(&unl_ins);
3584 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
3585 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003586}
3587
3588// Set the radio operating mode, and pure mode flag.
3589INT wifi_setRadioChannelMode(INT radioIndex, CHAR *channelMode, BOOL gOnlyFlag, BOOL nOnlyFlag, BOOL acOnlyFlag) //RDKB
3590{
developera3511852023-06-14 14:12:59 +08003591 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%s_%d_%d:%d\n",__func__,channelMode,nOnlyFlag,gOnlyFlag,__LINE__);
3592 if (strcmp (channelMode,"11A") == 0)
3593 {
3594 writeBandWidth(radioIndex,"20MHz");
3595 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3596 printf("\nChannel Mode is 802.11a (5GHz)\n");
3597 }
3598 else if (strcmp (channelMode,"11NAHT20") == 0)
3599 {
3600 writeBandWidth(radioIndex,"20MHz");
3601 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3602 printf("\nChannel Mode is 802.11n-20MHz(5GHz)\n");
3603 }
3604 else if (strcmp (channelMode,"11NAHT40PLUS") == 0)
3605 {
3606 writeBandWidth(radioIndex,"40MHz");
3607 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3608 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3609 }
3610 else if (strcmp (channelMode,"11NAHT40MINUS") == 0)
3611 {
3612 writeBandWidth(radioIndex,"40MHz");
3613 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3614 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3615 }
3616 else if (strcmp (channelMode,"11ACVHT20") == 0)
3617 {
3618 writeBandWidth(radioIndex,"20MHz");
3619 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3620 printf("\nChannel Mode is 802.11ac-20MHz(5GHz)\n");
3621 }
3622 else if (strcmp (channelMode,"11ACVHT40PLUS") == 0)
3623 {
3624 writeBandWidth(radioIndex,"40MHz");
3625 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3626 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3627 }
3628 else if (strcmp (channelMode,"11ACVHT40MINUS") == 0)
3629 {
3630 writeBandWidth(radioIndex,"40MHz");
3631 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3632 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3633 }
3634 else if (strcmp (channelMode,"11ACVHT80") == 0)
3635 {
3636 wifi_setRadioOperatingChannelBandwidth(radioIndex,"80MHz");
3637 printf("\nChannel Mode is 802.11ac-80MHz(5GHz)\n");
3638 }
3639 else if (strcmp (channelMode,"11ACVHT160") == 0)
3640 {
3641 wifi_setRadioOperatingChannelBandwidth(radioIndex,"160MHz");
3642 printf("\nChannel Mode is 802.11ac-160MHz(5GHz)\n");
3643 }
3644 else if (strcmp (channelMode,"11B") == 0)
3645 {
3646 writeBandWidth(radioIndex,"20MHz");
3647 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3648 printf("\nChannel Mode is 802.11b(2.4GHz)\n");
3649 }
3650 else if (strcmp (channelMode,"11G") == 0)
3651 {
3652 writeBandWidth(radioIndex,"20MHz");
3653 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3654 printf("\nChannel Mode is 802.11g(2.4GHz)\n");
3655 }
3656 else if (strcmp (channelMode,"11NGHT20") == 0)
3657 {
3658 writeBandWidth(radioIndex,"20MHz");
3659 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3660 printf("\nChannel Mode is 802.11n-20MHz(2.4GHz)\n");
3661 }
3662 else if (strcmp (channelMode,"11NGHT40PLUS") == 0)
3663 {
3664 writeBandWidth(radioIndex,"40MHz");
3665 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3666 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3667 }
3668 else if (strcmp (channelMode,"11NGHT40MINUS") == 0)
3669 {
3670 writeBandWidth(radioIndex,"40MHz");
3671 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3672 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3673 }
3674 else
3675 {
3676 return RETURN_ERR;
3677 }
3678 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003679
developera3511852023-06-14 14:12:59 +08003680 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003681}
3682
developer0f10c772023-05-16 21:43:39 +08003683typedef enum _RT_802_11_PHY_MODE {
3684 PHY_11BG_MIXED = 0,
3685 PHY_11B = 1,
3686 PHY_11A = 2,
3687 PHY_11ABG_MIXED = 3,
3688 PHY_11G = 4,
3689 PHY_11ABGN_MIXED = 5, /* both band 5 */
developera3511852023-06-14 14:12:59 +08003690 PHY_11N_2_4G = 6, /* 11n-only with 2.4G band 6 */
3691 PHY_11GN_MIXED = 7, /* 2.4G band 7 */
3692 PHY_11AN_MIXED = 8, /* 5G band 8 */
3693 PHY_11BGN_MIXED = 9, /* if check 802.11b. 9 */
3694 PHY_11AGN_MIXED = 10, /* if check 802.11b. 10 */
3695 PHY_11N_5G = 11, /* 11n-only with 5G band 11 */
developer0f10c772023-05-16 21:43:39 +08003696 PHY_11VHT_N_ABG_MIXED = 12, /* 12 -> AC/A/AN/B/G/GN mixed */
3697 PHY_11VHT_N_AG_MIXED = 13, /* 13 -> AC/A/AN/G/GN mixed */
3698 PHY_11VHT_N_A_MIXED = 14, /* 14 -> AC/AN/A mixed in 5G band */
3699 PHY_11VHT_N_MIXED = 15, /* 15 -> AC/AN mixed in 5G band */
3700 PHY_11AX_24G = 16,
3701 PHY_11AX_5G = 17,
3702 PHY_11AX_6G = 18,
3703 PHY_11AX_24G_6G = 19,
3704 PHY_11AX_5G_6G = 20,
3705 PHY_11AX_24G_5G_6G = 21,
3706 PHY_11BE_24G = 22,
3707 PHY_11BE_5G = 23,
3708 PHY_11BE_6G = 24,
3709 PHY_11BE_24G_6G = 25,
3710 PHY_11BE_5G_6G = 26,
3711 PHY_11BE_24G_5G_6G = 27,
3712 PHY_MODE_MAX,
3713} RT_802_11_PHY_MODE;
3714
3715unsigned int puremode_to_wireless_mode(INT radioIndex, UINT pureMode)
3716{
3717 int band_idx = 0;
developerfead3972023-05-25 20:15:02 +08003718 unsigned char wireless_mode = PHY_MODE_MAX;
developer0f10c772023-05-16 21:43:39 +08003719
3720 band_idx = radio_index_to_band(radioIndex);
3721
3722 switch (band_idx) {
3723 case band_2_4:
3724 if (pureMode == (WIFI_MODE_G | WIFI_MODE_N))
3725 wireless_mode = PHY_11GN_MIXED;
3726 if (pureMode == (WIFI_MODE_B | WIFI_MODE_G | WIFI_MODE_N))
3727 wireless_mode = PHY_11BGN_MIXED;
3728 if (pureMode & WIFI_MODE_AX)
3729 wireless_mode = PHY_11AX_24G;
3730 if (pureMode & WIFI_MODE_BE)
3731 wireless_mode = PHY_11BE_24G;
3732 break;
3733 case band_5:
3734 if (pureMode == WIFI_MODE_N)
3735 wireless_mode = PHY_11N_5G;
3736 if ((pureMode == WIFI_MODE_AC) || (pureMode == (WIFI_MODE_N | WIFI_MODE_AC)))
3737 wireless_mode = PHY_11VHT_N_MIXED;
3738 if (pureMode == (WIFI_MODE_A | WIFI_MODE_N | WIFI_MODE_AC))
3739 wireless_mode = PHY_11VHT_N_A_MIXED;
3740 if (pureMode & WIFI_MODE_AX)
3741 wireless_mode = PHY_11AX_5G;
3742 if (pureMode & WIFI_MODE_BE)
3743 wireless_mode = PHY_11BE_5G;
3744 break;
3745 case band_6:
3746 if (pureMode & WIFI_MODE_AX)
3747 wireless_mode = PHY_11AX_6G;
3748 if (pureMode & WIFI_MODE_BE)
3749 wireless_mode = PHY_11BE_6G;
3750 break;
3751 default:
developer37646972023-06-29 10:58:43 +08003752 if (fprintf(stderr, "%s band_idx invalid\n", __func__) < 0)
3753 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developer0f10c772023-05-16 21:43:39 +08003754 break;
3755 }
3756
3757 return wireless_mode;
3758}
3759
developer72fb0bb2023-01-11 09:46:29 +08003760// Set the radio operating mode, and pure mode flag.
3761INT wifi_setRadioMode(INT radioIndex, CHAR *channelMode, UINT pureMode)
3762{
developerfead3972023-05-25 20:15:02 +08003763 unsigned char wireless_mode = PHY_MODE_MAX;
developer69b61b02023-03-07 17:17:44 +08003764
developer0f10c772023-05-16 21:43:39 +08003765 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003766 int ret = -1;
3767 unsigned int if_idx = 0;
3768 struct unl unl_ins;
3769 struct nl_msg *msg = NULL;
3770 struct nlattr * msg_data = NULL;
3771 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +08003772
developer0f10c772023-05-16 21:43:39 +08003773 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, channelMode, pureMode, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003774
developer0f10c772023-05-16 21:43:39 +08003775 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
developer72fb0bb2023-01-11 09:46:29 +08003776
developera3511852023-06-14 14:12:59 +08003777 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003778 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003779 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003780 }
developer72fb0bb2023-01-11 09:46:29 +08003781
developer0f10c772023-05-16 21:43:39 +08003782 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08003783 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003784
3785 if_idx = if_nametoindex(interface_name);
3786 if (!if_idx) {
3787 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", interface_name);
3788 return RETURN_ERR;
3789 }
3790 /*init mtk nl80211 vendor cmd*/
3791 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
3792 param.if_type = NL80211_ATTR_IFINDEX;
3793 param.if_idx = if_idx;
3794
3795 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3796 if (ret) {
3797 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3798 return RETURN_ERR;
3799 }
3800
3801 /*add mtk vendor cmd data*/
3802 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_WIRELESS_MODE, wireless_mode)) {
3803 wifi_debug(DEBUG_ERROR, "Nla put AP_WIRELESS_MODE attribute error\n");
3804 nlmsg_free(msg);
3805 goto err;
3806 }
3807 /*send mtk nl80211 vendor msg*/
3808 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
3809 if (ret) {
3810 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3811 goto err;
3812 }
3813 /*deinit mtk nl80211 vendor msg*/
3814 mtk_nl80211_deint(&unl_ins);
3815 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08003816
developera3511852023-06-14 14:12:59 +08003817 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer0f10c772023-05-16 21:43:39 +08003818
developera3511852023-06-14 14:12:59 +08003819 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003820err:
3821 mtk_nl80211_deint(&unl_ins);
3822 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
3823 return RETURN_ERR;
developer0f10c772023-05-16 21:43:39 +08003824}
3825
3826INT wifi_setRadioMode_by_dat(INT radioIndex, UINT pureMode)
3827{
developerfead3972023-05-25 20:15:02 +08003828 unsigned char wireless_mode = PHY_MODE_MAX;
developera3511852023-06-14 14:12:59 +08003829 char buf[MAX_BUF_SIZE] = {0};
developer0f10c772023-05-16 21:43:39 +08003830 char dat_file[MAX_BUF_SIZE] = {0};
3831 struct params params={0};
developere40952c2023-06-15 18:46:43 +08003832 int res;
developer0f10c772023-05-16 21:43:39 +08003833
3834 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, pureMode, __LINE__);
3835
3836 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
3837
developera3511852023-06-14 14:12:59 +08003838 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003839 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003840 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003841 }
developer0f10c772023-05-16 21:43:39 +08003842
3843 params.name = "WirelessMode";
developere40952c2023-06-15 18:46:43 +08003844 res = snprintf(buf, sizeof(buf), "%d", wireless_mode);
3845 if (os_snprintf_error(sizeof(buf), res)) {
3846 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3847 return RETURN_ERR;
3848 }
3849
developera3511852023-06-14 14:12:59 +08003850 params.value = buf;
developer0f10c772023-05-16 21:43:39 +08003851
developere40952c2023-06-15 18:46:43 +08003852 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
3853 if (os_snprintf_error(sizeof(dat_file), res)) {
3854 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3855 return RETURN_ERR;
3856 }
developera3511852023-06-14 14:12:59 +08003857 wifi_datfileWrite(dat_file, &params, 1);
developer0f10c772023-05-16 21:43:39 +08003858
developera3511852023-06-14 14:12:59 +08003859 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003860
developera3511852023-06-14 14:12:59 +08003861 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003862}
3863
3864INT wifi_setRadioHwMode(INT radioIndex, CHAR *hw_mode) {
3865
developera3511852023-06-14 14:12:59 +08003866 char config_file[64] = {0};
3867 char buf[64] = {0};
3868 struct params params = {0};
3869 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08003870 int res;
developer72fb0bb2023-01-11 09:46:29 +08003871
developera3511852023-06-14 14:12:59 +08003872 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003873
developera3511852023-06-14 14:12:59 +08003874 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003875
developera3511852023-06-14 14:12:59 +08003876 if (strncmp(hw_mode, "a", 1) == 0 && (band != band_5 && band != band_6))
3877 return RETURN_ERR;
3878 else if ((strncmp(hw_mode, "b", 1) == 0 || strncmp(hw_mode, "g", 1) == 0) && band != band_2_4)
3879 return RETURN_ERR;
3880 else if ((strncmp(hw_mode, "a", 1) && strncmp(hw_mode, "b", 1) && strncmp(hw_mode, "g", 1)) || band == band_invalid)
3881 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003882
developer75bd10c2023-06-27 11:34:08 +08003883 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3884 if (os_snprintf_error(sizeof(config_file), res)) {
3885 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3886 return RETURN_ERR;
3887 }
developera3511852023-06-14 14:12:59 +08003888 params.name = "hw_mode";
3889 params.value = hw_mode;
3890 wifi_hostapdWrite(config_file, &params, 1);
3891 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003892
developera3511852023-06-14 14:12:59 +08003893 if (band == band_2_4) {
3894 if (strncmp(hw_mode, "b", 1) == 0) {
3895 wifi_setRadioMode(radioIndex, "20MHz", WIFI_MODE_B);
developere40952c2023-06-15 18:46:43 +08003896 res = snprintf(buf, sizeof(buf), "%s", "1,2,5.5,11");
3897 if (os_snprintf_error(sizeof(buf), res)) {
3898 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3899 return RETURN_ERR;
3900 }
developera3511852023-06-14 14:12:59 +08003901 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003902 res = snprintf(buf, sizeof(buf), "%s", "1,2");
3903 if (os_snprintf_error(sizeof(buf), res)) {
3904 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3905 return RETURN_ERR;
3906 }
developera3511852023-06-14 14:12:59 +08003907 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3908 } else {
3909 // 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 +08003910
developere40952c2023-06-15 18:46:43 +08003911 res = snprintf(buf, sizeof(buf), "%s", "6,9,12,18,24,36,48,54");
3912 if (os_snprintf_error(sizeof(buf), res)) {
3913 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3914 return RETURN_ERR;
3915 }
developera3511852023-06-14 14:12:59 +08003916 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003917 res = snprintf(buf, sizeof(buf), "%s", "6,12,24");
3918 if (os_snprintf_error(sizeof(buf), res)) {
3919 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3920 return RETURN_ERR;
3921 }
developera3511852023-06-14 14:12:59 +08003922 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3923 }
3924 }
developer72fb0bb2023-01-11 09:46:29 +08003925
developera3511852023-06-14 14:12:59 +08003926 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3927 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003928}
3929
3930INT wifi_setNoscan(INT radioIndex, CHAR *noscan)
3931{
developera3511852023-06-14 14:12:59 +08003932 char config_file[64] = {0};
3933 struct params params = {0};
3934 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08003935 int res;
developer72fb0bb2023-01-11 09:46:29 +08003936
developera3511852023-06-14 14:12:59 +08003937 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003938
developera3511852023-06-14 14:12:59 +08003939 band = wifi_index_to_band(radioIndex);
3940 if (band != band_2_4)
3941 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003942
developer75bd10c2023-06-27 11:34:08 +08003943 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3944 if (os_snprintf_error(sizeof(config_file), res)) {
3945 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3946 return RETURN_ERR;
3947 }
developera3511852023-06-14 14:12:59 +08003948 params.name = "noscan";
3949 params.value = noscan;
3950 wifi_hostapdWrite(config_file, &params, 1);
3951 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003952
developera3511852023-06-14 14:12:59 +08003953 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3954 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003955}
3956
3957//Get the list of supported channel. eg: "1-11"
3958//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.
3959INT wifi_getRadioPossibleChannels(INT radioIndex, CHAR *output_string) //RDKB
3960{
developera3511852023-06-14 14:12:59 +08003961 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3962 if (NULL == output_string)
3963 return RETURN_ERR;
3964 char cmd[256] = {0};
3965 char buf[128] = {0};
3966 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +08003967 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003968
developera3511852023-06-14 14:12:59 +08003969 // Parse possible channel number and separate them with commas.
3970 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
3971 phyId = radio_index_to_phy(radioIndex);
3972 // Channel 68 and 96 only allow bandwidth 20MHz, so we remove them with their frequency.
3973 if (dfs_enable)
developere40952c2023-06-15 18:46:43 +08003974 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep -e '\\*.*MHz .*dBm' | grep -v 'no IR\\|5340\\|5480' | cut -d '[' -f2 | cut -d ']' -f1 | tr '\\n' ',' | sed 's/.$//'", phyId);
developera3511852023-06-14 14:12:59 +08003975 else
developere40952c2023-06-15 18:46:43 +08003976 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep -e '\\*.*MHz .*dBm' | grep -v 'radar\\|no IR\\|5340\\|5480' | cut -d '[' -f2 | cut -d ']' -f1 | tr '\\n' ',' | sed 's/.$//'", phyId);
developer72fb0bb2023-01-11 09:46:29 +08003977
developere40952c2023-06-15 18:46:43 +08003978 if (os_snprintf_error(sizeof(cmd), res)) {
3979 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3980 return RETURN_ERR;
3981 }
developera3511852023-06-14 14:12:59 +08003982 _syscmd(cmd,buf,sizeof(buf));
3983 strncpy(output_string, buf, strlen(buf) < sizeof(buf) ? strlen(buf) : sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08003984
developera3511852023-06-14 14:12:59 +08003985 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3986 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003987}
developerd1824452023-05-18 12:30:04 +08003988//Getting current radio extension channel
3989INT wifi_halgetRadioExtChannel(CHAR *file,CHAR *Value)
3990{
developera3511852023-06-14 14:12:59 +08003991 CHAR buf[150] = {0};
developer32f2a182023-06-27 19:50:41 +08003992 int len;
developerd1824452023-05-18 12:30:04 +08003993
developera3511852023-06-14 14:12:59 +08003994 wifi_datfileRead(file, "HT_EXTCHA", buf, sizeof(buf));
developerb14b3462023-07-01 18:02:42 +08003995
3996 if (strncmp(buf, "0", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08003997 len = strlen("BelowControlChannel");
3998 memcpy(Value, "BelowControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08003999 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08004000 }
developerb14b3462023-07-01 18:02:42 +08004001 else if(strncmp(buf, "1", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08004002 len = strlen("AboveControlChannel");
4003 memcpy(Value, "AboveControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08004004 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08004005 }
developer5b23cd02023-07-19 20:26:03 +08004006
developera3511852023-06-14 14:12:59 +08004007 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +08004008}
developerf6a87542023-05-16 15:47:28 +08004009
developer72fb0bb2023-01-11 09:46:29 +08004010//Get the list for used channel. eg: "1,6,9,11"
4011//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.
4012INT wifi_getRadioChannelsInUse(INT radioIndex, CHAR *output_string) //RDKB
4013{
developera3511852023-06-14 14:12:59 +08004014 char interface_name[16] = {0};
4015 char cmd[128] = {0};
4016 char buf[128] = {0};
4017 char config_file[64] = {0};
4018 int channel = 0;
4019 int freq = 0;
4020 int bandwidth = 0;
4021 int center_freq = 0;
4022 int center_channel = 0;
4023 int channel_delta = 0;
4024 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004025 int res;
developer72fb0bb2023-01-11 09:46:29 +08004026
developera3511852023-06-14 14:12:59 +08004027 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004028
developera3511852023-06-14 14:12:59 +08004029 if (NULL == output_string)
4030 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004031
developera3511852023-06-14 14:12:59 +08004032 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4033 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08004034
4035 res = snprintf(cmd, sizeof(cmd), "iw %s info | grep channel | sed -e 's/[^0-9 ]//g'", interface_name);
4036 if (os_snprintf_error(sizeof(cmd), res)) {
4037 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4038 return RETURN_ERR;
4039 }
developera3511852023-06-14 14:12:59 +08004040 _syscmd(cmd, buf, sizeof(buf));
4041 if (strlen(buf) == 0) {
developer75bd10c2023-06-27 11:34:08 +08004042 wifi_debug(DEBUG_ERROR, "failed to get channel information from iw.\n");
developera3511852023-06-14 14:12:59 +08004043 return RETURN_ERR;
4044 }
developerd14dff12023-06-28 22:47:44 +08004045 if (sscanf(buf, "%d %d %d %*d %d", &channel, &freq, &bandwidth, &center_freq) != 4) {
4046 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
4047 return RETURN_ERR;
4048 }
developer72fb0bb2023-01-11 09:46:29 +08004049
developera3511852023-06-14 14:12:59 +08004050 if (bandwidth == 20) {
developere40952c2023-06-15 18:46:43 +08004051 res = snprintf(output_string, 256, "%d", channel);
4052 if (os_snprintf_error(256, res)) {
4053 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4054 return RETURN_ERR;
4055 }
developera3511852023-06-14 14:12:59 +08004056 return RETURN_OK;
4057 }
developer72fb0bb2023-01-11 09:46:29 +08004058
developera3511852023-06-14 14:12:59 +08004059 center_channel = ieee80211_frequency_to_channel(center_freq);
developer72fb0bb2023-01-11 09:46:29 +08004060
developera3511852023-06-14 14:12:59 +08004061 band = wifi_index_to_band(radioIndex);
4062 if (band == band_2_4 && bandwidth == 40) {
developer32f2a182023-06-27 19:50:41 +08004063 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4064 if (os_snprintf_error(sizeof(config_file), res)) {
4065 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4066 return RETURN_ERR;
4067 }
developera3511852023-06-14 14:12:59 +08004068 memset(buf, 0, sizeof(buf));
4069 wifi_halgetRadioExtChannel(config_file, buf); // read ht_capab for HT40+ or -
developer72fb0bb2023-01-11 09:46:29 +08004070
developera3511852023-06-14 14:12:59 +08004071 if (strncmp(buf, "AboveControlChannel", strlen("AboveControlChannel")) == 0 && channel < 10) {
developere40952c2023-06-15 18:46:43 +08004072 res = snprintf(output_string, 256, "%d,%d", channel, channel+4);
developera3511852023-06-14 14:12:59 +08004073 } else if (strncmp(buf, "BelowControlChannel", strlen("BelowControlChannel")) == 0 && channel > 4) {
developere40952c2023-06-15 18:46:43 +08004074 res = snprintf(output_string, 256, "%d,%d", channel-4, channel);
developera3511852023-06-14 14:12:59 +08004075 } else {
developer37646972023-06-29 10:58:43 +08004076 if (fprintf(stderr, "%s: invalid channel %d set with %s\n.", __func__, channel, buf) < 0)
4077 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08004078 return RETURN_ERR;
4079 }
developerb758dfd2023-06-21 17:32:07 +08004080
developere40952c2023-06-15 18:46:43 +08004081 if (os_snprintf_error(256, res)) {
4082 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4083 return RETURN_ERR;
4084 }
developera3511852023-06-14 14:12:59 +08004085 } else if (band == band_5 || band == band_6){
4086 // to minus 20 is an offset, because frequence of a channel have a range. We need to use offset to calculate correct channel.
4087 // example: bandwidth 80: center is 42 (5210), channels are "36,40,44,48" (5170-5250). The delta should be 6.
4088 channel_delta = (bandwidth-20)/10;
4089 memset(output_string, 0, 256);
4090 for (int i = center_channel-channel_delta; i <= center_channel+channel_delta; i+=4) {
4091 // If i is not the last channel, we add a comma.
developere40952c2023-06-15 18:46:43 +08004092 res = snprintf(buf, sizeof(buf), "%d%s", i, i==center_channel+channel_delta?"":",");
developer5b23cd02023-07-19 20:26:03 +08004093
developere40952c2023-06-15 18:46:43 +08004094 if (os_snprintf_error(sizeof(buf), res)) {
4095 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4096 return RETURN_ERR;
4097 }
developer32f2a182023-06-27 19:50:41 +08004098 strncat(output_string, buf, sizeof(output_string) - strlen(output_string) - 1);
developera3511852023-06-14 14:12:59 +08004099 }
4100 } else
4101 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004102
developera3511852023-06-14 14:12:59 +08004103 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4104 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004105}
4106
developer69b61b02023-03-07 17:17:44 +08004107//Get the running channel number
developerd1824452023-05-18 12:30:04 +08004108INT wifi_getRadioChannel(INT radioIndex, ULONG *output_ulong) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004109{
developera3511852023-06-14 14:12:59 +08004110 char channel_str[16] = {0};
4111 char config_file[128] = {0};
4112 char buf[MAX_BUF_SIZE] = {0};
4113 char cmd[MAX_CMD_SIZE] = {0};
4114 char interface_name[IF_NAME_SIZE] = {0};
4115 wifi_band band = band_invalid;
4116 ULONG iwChannel = 0;
developere40952c2023-06-15 18:46:43 +08004117 int res;
developer47a56bf2023-05-30 13:38:57 +08004118
developera3511852023-06-14 14:12:59 +08004119 if (output_ulong == NULL)
4120 return RETURN_ERR;
4121 band = wifi_index_to_band(radioIndex);
developerb758dfd2023-06-21 17:32:07 +08004122 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08004123 if (os_snprintf_error(sizeof(config_file), res)) {
4124 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4125 return RETURN_ERR;
4126 }
4127
developera3511852023-06-14 14:12:59 +08004128 wifi_datfileRead(config_file, "Channel", channel_str, sizeof(channel_str));
developerb14b3462023-07-01 18:02:42 +08004129 if (hal_strtoul(channel_str, 10, output_ulong) < 0) {
developerc14d83a2023-06-29 20:09:42 +08004130 wifi_debug(DEBUG_ERROR, "strtol fail\n");
4131 }
developera3511852023-06-14 14:12:59 +08004132 if (*output_ulong == 0) {
4133 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4134 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004135 res = snprintf(cmd, sizeof(cmd), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4136 if (os_snprintf_error(sizeof(cmd), res)) {
4137 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4138 return RETURN_ERR;
4139 }
4140
developera3511852023-06-14 14:12:59 +08004141 _syscmd(cmd,buf,sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08004142 if (sscanf(buf, "%lu", &iwChannel) != 1) {
4143 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
4144 return RETURN_ERR;
4145 }
developera3511852023-06-14 14:12:59 +08004146 *output_ulong = iwChannel;
4147 }
developer72fb0bb2023-01-11 09:46:29 +08004148
developera3511852023-06-14 14:12:59 +08004149 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004150}
4151
developer72fb0bb2023-01-11 09:46:29 +08004152INT wifi_getApChannel(INT apIndex,ULONG *output_ulong) //RDKB
4153{
developera3511852023-06-14 14:12:59 +08004154 char cmd[1024] = {0}, buf[5] = {0};
4155 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08004156 int res;
developer72fb0bb2023-01-11 09:46:29 +08004157
developera3511852023-06-14 14:12:59 +08004158 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4159 if (NULL == output_ulong)
4160 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004161
developera3511852023-06-14 14:12:59 +08004162 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
4163 return RETURN_ERR;
developer47a56bf2023-05-30 13:38:57 +08004164
developere40952c2023-06-15 18:46:43 +08004165 res = snprintf(cmd, sizeof(cmd), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4166 if (os_snprintf_error(sizeof(cmd), res)) {
4167 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4168 return RETURN_ERR;
4169 }
developera3511852023-06-14 14:12:59 +08004170 _syscmd(cmd,buf,sizeof(buf));
4171 *output_ulong = (strlen(buf) >= 1)? atol(buf): 0;
4172 if (*output_ulong == 0) {
4173 return RETURN_ERR;
4174 }
developer72fb0bb2023-01-11 09:46:29 +08004175
developera3511852023-06-14 14:12:59 +08004176 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4177 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004178}
developer72fb0bb2023-01-11 09:46:29 +08004179//Storing the previous channel value
4180INT wifi_storeprevchanval(INT radioIndex)
4181{
developera3511852023-06-14 14:12:59 +08004182 char output[4]={'\0'};
4183 char config_file[MAX_BUF_SIZE] = {0};
4184 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004185 int res;
developerd1824452023-05-18 12:30:04 +08004186
developera3511852023-06-14 14:12:59 +08004187 band = wifi_index_to_band(radioIndex);
4188 if (band == band_invalid) {
4189 return RETURN_ERR;
4190 wifi_dbg_printf("[%s]: Invalid radio index", __func__);
4191 }
developere40952c2023-06-15 18:46:43 +08004192 res = snprintf(config_file, sizeof(config_file), "%s%d.dat",LOGAN_DAT_FILE, band);
4193 if (os_snprintf_error(sizeof(config_file), res)) {
4194 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4195 return RETURN_ERR;
4196 }
4197
developera3511852023-06-14 14:12:59 +08004198 wifi_datfileRead(config_file, "Channel", output, sizeof(output));
developerd1824452023-05-18 12:30:04 +08004199
developera3511852023-06-14 14:12:59 +08004200 if(band == band_2_4)
developer33f13ba2023-07-12 16:19:06 +08004201 res = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004202 else if(band == band_5)
developer33f13ba2023-07-12 16:19:06 +08004203 res = v_secure_system("echo %s > /var/prevchanval5G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004204 else
developer33f13ba2023-07-12 16:19:06 +08004205 res = v_secure_system("echo %s > /var/prevchanval6G_AutoChannelEnable", output);
developer75bd10c2023-06-27 11:34:08 +08004206
developer33f13ba2023-07-12 16:19:06 +08004207 if (res) {
4208 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08004209 }
developera3511852023-06-14 14:12:59 +08004210 Radio_flag = FALSE;
4211 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004212}
4213
4214//Set the running channel number
4215INT wifi_setRadioChannel(INT radioIndex, ULONG channel) //RDKB //AP only
4216{
developera3511852023-06-14 14:12:59 +08004217 // We only write hostapd config here
4218 char str_channel[8]={0};
4219 char *list_channel;
4220 char possible_channels[256] = {0};
4221 char config_file_dat[128] = {0};
4222 struct params dat = {0};
4223 struct params acs = {0};
4224 wifi_band band = band_invalid;
4225 bool acs_channel = false;
developere40952c2023-06-15 18:46:43 +08004226 int res;
developer72fb0bb2023-01-11 09:46:29 +08004227
developera3511852023-06-14 14:12:59 +08004228 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004229
developera3511852023-06-14 14:12:59 +08004230 if (channel == 0)
4231 acs_channel = true;
4232 // Check valid
developer75bd10c2023-06-27 11:34:08 +08004233 res = snprintf(str_channel, sizeof(str_channel), "%lu", channel);
4234 if (os_snprintf_error(sizeof(str_channel), res)) {
4235 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4236 return RETURN_ERR;
4237 }
developer47a56bf2023-05-30 13:38:57 +08004238
developera3511852023-06-14 14:12:59 +08004239 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4240 list_channel = strtok(possible_channels, ",");
4241 while(true)
4242 {
4243 if(list_channel == NULL) { // input not in the list
developer75bd10c2023-06-27 11:34:08 +08004244 wifi_debug(DEBUG_ERROR, "Channel %s is not in possible list\n", str_channel);
developera3511852023-06-14 14:12:59 +08004245 return RETURN_ERR;
4246 }
4247 if (strncmp(str_channel, list_channel, strlen(list_channel)) == 0 || strncmp(str_channel, "0", 1) == 0)
4248 break;
4249 list_channel = strtok(NULL, ",");
4250 }
4251 /*
4252 list.name = "channel";
4253 list.value = str_channel;
4254 wifi_getMaxRadioNumber(&max_radio_num);
4255 for(int i=0; i<=MAX_APS/max_radio_num;i++)
4256 {
4257 sprintf(config_file, "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_radio_num*i));
4258 wifi_hostapdWrite(config_file, &list, 1);
4259 }
4260 */
4261 dat.name = "Channel";
4262 dat.value = str_channel;
4263 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004264 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4265 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4266 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4267 return RETURN_ERR;
4268 }
4269
developera3511852023-06-14 14:12:59 +08004270 wifi_datfileWrite(config_file_dat, &dat, 1);
4271 if (acs_channel == true) {
4272 acs.name = "AutoChannelSelect";
4273 acs.value = "3";
4274 } else {
4275 acs.name = "AutoChannelSelect";
4276 acs.value = "0";
4277 }
4278 wifi_datfileWrite(config_file_dat, &acs, 1);
developer5b23cd02023-07-19 20:26:03 +08004279 if (multiple_set == FALSE) {
4280 wifi_setRadioEnable(radioIndex, FALSE);
4281 wifi_setRadioEnable(radioIndex, TRUE);
4282 }
4283
developera3511852023-06-14 14:12:59 +08004284 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4285 return RETURN_OK;
4286}
4287
4288INT wifi_setRadioCenterChannel(INT radioIndex, ULONG channel)
4289{
4290 struct params list[2];
4291 char str_idx[16];
4292 char config_file[64];
developere40952c2023-06-15 18:46:43 +08004293 int max_num_radios = 0, res;
developera3511852023-06-14 14:12:59 +08004294 wifi_band band = band_invalid;
4295
4296 band = wifi_index_to_band(radioIndex);
4297 if (band == band_2_4)
4298 return RETURN_OK;
4299
developere40952c2023-06-15 18:46:43 +08004300 res = snprintf(str_idx, sizeof(str_idx), "%lu", channel);
4301 if (os_snprintf_error(sizeof(str_idx), res)) {
4302 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4303 return RETURN_ERR;
4304 }
4305
developera3511852023-06-14 14:12:59 +08004306 list[0].name = "vht_oper_centr_freq_seg0_idx";
4307 list[0].value = str_idx;
4308 list[1].name = "he_oper_centr_freq_seg0_idx";
4309 list[1].value = str_idx;
4310
4311 wifi_getMaxRadioNumber(&max_num_radios);
developer9ce44382023-06-28 11:09:37 +08004312 if(max_num_radios== 0){
4313 return RETURN_ERR;
4314 }
developera3511852023-06-14 14:12:59 +08004315 for(int i=0; i<=MAX_APS/max_num_radios; i++)
4316 {
developere40952c2023-06-15 18:46:43 +08004317 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_num_radios*i));
4318 if (os_snprintf_error(sizeof(config_file), res)) {
4319 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4320 return RETURN_ERR;
4321 }
developera3511852023-06-14 14:12:59 +08004322 if (band == band_6)
4323 wifi_hostapdWrite(config_file, &list[1], 1);
4324 else
4325 wifi_hostapdWrite(config_file, list, 2);
4326 }
4327
4328 return RETURN_OK;
4329}
4330
4331//Enables or disables a driver level variable to indicate if auto channel selection is enabled on this radio
4332//This "auto channel" means the auto channel selection when radio is up. (which is different from the dynamic channel/frequency selection (DFC/DCS))
4333INT wifi_setRadioAutoChannelEnable(INT radioIndex, BOOL enable) //RDKB
4334{
4335 //Set to wifi config only. Wait for wifi reset to apply.
4336 ULONG Value = 0;
4337 char config_file_dat[128] = {0};
4338 struct params acs = {0};
4339 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004340 int res;
developera3511852023-06-14 14:12:59 +08004341
4342 if(enable == TRUE) {
4343 wifi_setRadioChannel(radioIndex,Value);
4344 } else {
4345 acs.name = "AutoChannelSelect";
4346 acs.value = "0";
4347 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004348 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4349 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4350 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4351 return RETURN_ERR;
4352 }
developera3511852023-06-14 14:12:59 +08004353 wifi_datfileWrite(config_file_dat, &acs, 1);
4354 }
4355 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004356}
4357
4358INT wifi_getRadioAutoChannelSupported(INT radioIndex, BOOL *output_bool)
4359{
developera3511852023-06-14 14:12:59 +08004360 if (output_bool == NULL)
4361 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004362
developera3511852023-06-14 14:12:59 +08004363 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004364
developera3511852023-06-14 14:12:59 +08004365 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004366}
4367
4368INT wifi_getRadioDCSSupported(INT radioIndex, BOOL *output_bool) //RDKB
4369{
developera3511852023-06-14 14:12:59 +08004370 if (NULL == output_bool)
4371 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004372 *output_bool=TRUE;
developera3511852023-06-14 14:12:59 +08004373 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004374}
4375
4376INT wifi_getRadioDCSEnable(INT radioIndex, BOOL *output_bool) //RDKB
4377{
developer326d4232023-06-15 16:45:30 +08004378 unsigned long period = 0;
4379
developera3511852023-06-14 14:12:59 +08004380 if (NULL == output_bool)
4381 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004382
4383 if (wifi_getRadioAutoChannelRefreshPeriod(radioIndex, &period) != RETURN_OK)
developerb758dfd2023-06-21 17:32:07 +08004384 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004385
4386 *output_bool = (period > 0) ? TRUE : FALSE;
4387
developera3511852023-06-14 14:12:59 +08004388 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004389}
4390
developer326d4232023-06-15 16:45:30 +08004391INT wifi_setRadioDCSEnable(INT radioIndex, BOOL enable) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004392{
developer326d4232023-06-15 16:45:30 +08004393 ULONG period = 1800;
4394
4395 if (enable == TRUE) {
4396 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, period) != RETURN_OK)
4397 return RETURN_ERR;
4398 }
4399 else {
4400 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, 0) != RETURN_OK)
4401 return RETURN_ERR;
4402 }
developera3511852023-06-14 14:12:59 +08004403 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004404}
4405
4406INT wifi_setApEnableOnLine(ULONG wlanIndex,BOOL enable)
4407{
developera3511852023-06-14 14:12:59 +08004408 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004409}
4410
4411INT wifi_factoryResetAP(int apIndex)
4412{
developerb149d9d2023-06-06 16:14:22 +08004413 char ap_config_file[MAX_SUB_CMD_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08004414 char cmd[MAX_CMD_SIZE] = {0};
4415 char ret_buf[MAX_BUF_SIZE] = {0};
4416 int radio_idx = 0;
4417 int bss_idx = 0;
4418 char ssid[32] = {0};
4419 char interface[IF_NAME_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08004420 char psk_file[MAX_SUB_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08004421 struct params params[3] = {0};
developere40952c2023-06-15 18:46:43 +08004422 int res;
developer72fb0bb2023-01-11 09:46:29 +08004423
developera3511852023-06-14 14:12:59 +08004424 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004425
developer47cc27a2023-05-17 23:09:58 +08004426 /*del old config file*/
developer9ce44382023-06-28 11:09:37 +08004427 res = snprintf(ap_config_file, sizeof(ap_config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
developerd14dff12023-06-28 22:47:44 +08004428 if (os_snprintf_error(sizeof(ap_config_file), res)) {
developere40952c2023-06-15 18:46:43 +08004429 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4430 return RETURN_ERR;
4431 }
4432
4433 res = snprintf(cmd, MAX_CMD_SIZE, "rm %s", ap_config_file);
4434 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4435 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4436 return RETURN_ERR;
4437 }
4438
developer47cc27a2023-05-17 23:09:58 +08004439 _syscmd(cmd, ret_buf, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004440
developer47cc27a2023-05-17 23:09:58 +08004441 memset(cmd, 0, sizeof(cmd));
4442 memset(ret_buf, 0, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004443
developer47cc27a2023-05-17 23:09:58 +08004444 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
4445
4446 /*prepare new config file*/
developere40952c2023-06-15 18:46:43 +08004447 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], ap_config_file);
4448 if (os_snprintf_error(sizeof(cmd), res)) {
4449 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4450 return RETURN_ERR;
4451 }
4452
developer47cc27a2023-05-17 23:09:58 +08004453 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4454
4455 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08004456 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
4457 if (os_snprintf_error(sizeof(ssid), res)) {
4458 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4459 return RETURN_ERR;
4460 }
4461
4462 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
4463 if (os_snprintf_error(sizeof(interface), res)) {
4464 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4465 return RETURN_ERR;
4466 }
developer47cc27a2023-05-17 23:09:58 +08004467 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08004468 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
4469 if (os_snprintf_error(sizeof(ssid), res)) {
4470 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4471 return RETURN_ERR;
4472 }
4473
4474 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
4475 if (os_snprintf_error(sizeof(interface), res)) {
4476 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4477 return RETURN_ERR;
4478 }
developer47cc27a2023-05-17 23:09:58 +08004479 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08004480 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
4481 if (os_snprintf_error(sizeof(ssid), res)) {
4482 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4483 return RETURN_ERR;
4484 }
4485
4486 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
4487 if (os_snprintf_error(sizeof(interface), res)) {
4488 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4489 return RETURN_ERR;
4490 }
developer47cc27a2023-05-17 23:09:58 +08004491 }
4492
4493 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08004494 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", apIndex);
4495 if (os_snprintf_error(sizeof(psk_file), res)) {
4496 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4497 return RETURN_ERR;
4498 }
developer47cc27a2023-05-17 23:09:58 +08004499
4500 params[0].name = "ssid";
4501 params[0].value = ssid;
4502 params[1].name = "interface";
4503 params[1].value = interface;
4504 params[2].name = "wpa_psk_file";
4505 params[2].value = psk_file;
4506
4507 wifi_hostapdWrite(ap_config_file, params, 3);
4508
4509 /*clear psk file*/
4510 memset(cmd, 0, sizeof(cmd));
4511 memset(ret_buf, 0, sizeof(ret_buf));
4512
developere40952c2023-06-15 18:46:43 +08004513 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, apIndex);
4514 if (os_snprintf_error(sizeof(psk_file), res)) {
4515 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4516 return RETURN_ERR;
4517 }
developer47cc27a2023-05-17 23:09:58 +08004518
4519 if (access(psk_file, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08004520 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", psk_file);
4521 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4522 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4523 return RETURN_ERR;
4524 }
4525
developer47cc27a2023-05-17 23:09:58 +08004526 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4527 } else {
developere40952c2023-06-15 18:46:43 +08004528 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", psk_file);
4529 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4530 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4531 return RETURN_ERR;
4532 }
4533
developer47cc27a2023-05-17 23:09:58 +08004534 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4535 }
4536
developer429ba832023-05-31 11:03:35 +08004537 wifi_setApEnable(apIndex, FALSE);
4538 wifi_setApEnable(apIndex, TRUE);
developer47cc27a2023-05-17 23:09:58 +08004539 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4540
4541 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004542}
4543
developer72fb0bb2023-01-11 09:46:29 +08004544INT wifi_setBandSteeringApGroup(char *ApGroup)
4545{
developera3511852023-06-14 14:12:59 +08004546 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004547}
4548
4549INT wifi_getApDTIMInterval(INT apIndex, INT *dtimInterval)
4550{
developera3511852023-06-14 14:12:59 +08004551 char config_file[128] = {'\0'};
4552 char buf[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004553 int res;
developerc14d83a2023-06-29 20:09:42 +08004554 long int tmp;
developer5b23cd02023-07-19 20:26:03 +08004555
developera3511852023-06-14 14:12:59 +08004556 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4557 if (dtimInterval == NULL)
4558 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004559
4560 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4561 if (os_snprintf_error(sizeof(config_file), res)) {
4562 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4563 return RETURN_ERR;
4564 }
developer72fb0bb2023-01-11 09:46:29 +08004565
developera3511852023-06-14 14:12:59 +08004566 wifi_hostapdRead(config_file, "dtim_period", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08004567
developera3511852023-06-14 14:12:59 +08004568 if (strlen(buf) == 0) {
4569 *dtimInterval = 2;
4570 } else {
developerc14d83a2023-06-29 20:09:42 +08004571 if (hal_strtol(buf, 10, &tmp) < 0) {
4572 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08004573 }
developerc14d83a2023-06-29 20:09:42 +08004574 *dtimInterval = tmp;
developera3511852023-06-14 14:12:59 +08004575 }
developer72fb0bb2023-01-11 09:46:29 +08004576
developera3511852023-06-14 14:12:59 +08004577 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4578 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004579}
4580
4581INT wifi_setApDTIMInterval(INT apIndex, INT dtimInterval)
4582{
developera3511852023-06-14 14:12:59 +08004583 struct params params={0};
4584 char config_file[MAX_BUF_SIZE] = {'\0'};
4585 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004586 int res;
developer72fb0bb2023-01-11 09:46:29 +08004587
developera3511852023-06-14 14:12:59 +08004588 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4589 if (dtimInterval < 1 || dtimInterval > 255) {
4590 WIFI_ENTRY_EXIT_DEBUG("Invalid dtimInterval: %d\n", dtimInterval);
4591 return RETURN_ERR;
4592 }
developer69b61b02023-03-07 17:17:44 +08004593
developera3511852023-06-14 14:12:59 +08004594 params.name = "dtim_period";
developere40952c2023-06-15 18:46:43 +08004595 res = snprintf(buf, sizeof(buf), "%d", dtimInterval);
4596 if (os_snprintf_error(sizeof(buf), res)) {
4597 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4598 return RETURN_ERR;
4599 }
developera3511852023-06-14 14:12:59 +08004600 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08004601
developer75bd10c2023-06-27 11:34:08 +08004602 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4603 if (os_snprintf_error(sizeof(config_file), res)) {
4604 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4605 return RETURN_ERR;
4606 }
developera3511852023-06-14 14:12:59 +08004607 wifi_hostapdWrite(config_file, &params, 1);
4608 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08004609
developera3511852023-06-14 14:12:59 +08004610 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4611 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004612}
4613
4614//Check if the driver support the Dfs
4615INT wifi_getRadioDfsSupport(INT radioIndex, BOOL *output_bool) //Tr181
4616{
developera3511852023-06-14 14:12:59 +08004617 wifi_band band = band_invalid;
4618 if (NULL == output_bool)
4619 return RETURN_ERR;
4620 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +08004621
developera3511852023-06-14 14:12:59 +08004622 band = wifi_index_to_band(radioIndex);
4623 if (band == band_5)
4624 *output_bool = TRUE;
4625 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004626}
4627
4628//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.
4629//The value of this parameter is a comma seperated list of channel number
4630INT wifi_getRadioDCSChannelPool(INT radioIndex, CHAR *output_pool) //RDKB
4631{
developer326d4232023-06-15 16:45:30 +08004632
4633 #define CHANNEL_AVAILABLE 0
4634 #define CHANNEL_INVALID 1
4635 #define CHANNEL_LIST_MAX_LENGTH 256
4636 #define MAX_CHANNEL_NUMBER 255
4637
4638 char config_file[MAX_BUF_SIZE] = {0};
4639 char possible_channels[CHANNEL_LIST_MAX_LENGTH] = {0};
4640 char skip_list[CHANNEL_LIST_MAX_LENGTH] = {0};
4641 int skip_table[MAX_CHANNEL_NUMBER +1] = {0};
4642 wifi_band band = band_invalid;
4643 char *token_channel = NULL, *token_skip = NULL;
developer75bd10c2023-06-27 11:34:08 +08004644 int res;
developere40952c2023-06-15 18:46:43 +08004645
developera3511852023-06-14 14:12:59 +08004646 if (NULL == output_pool)
4647 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004648 // get skiplist, possible_channels list
4649 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4650 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004651 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4652 if (os_snprintf_error(sizeof(config_file), res)) {
4653 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4654 return RETURN_ERR;
4655 }
developer326d4232023-06-15 16:45:30 +08004656 wifi_datfileRead(config_file, "AutoChannelSkipList", skip_list, sizeof(skip_list));
4657
4658 if (skip_list[0] != '\0') {
4659 int len = strlen(skip_list);
4660 for (int i = 0; i < len; i++) {
4661 if (skip_list[i] == ';') {
4662 skip_list[i] = ',';
4663 }
4664 }
4665 // skip list
4666 token_skip = strtok(skip_list, ",");
4667 while (token_skip != NULL) {
4668 int channel = atoi(token_skip);
4669 if (channel <= MAX_CHANNEL_NUMBER && strstr(possible_channels, token_skip) != NULL)
4670 skip_table[atoi(token_skip)] = CHANNEL_INVALID;
4671 token_skip = strtok(NULL, ",");
4672 }
developere40952c2023-06-15 18:46:43 +08004673 }
developer72fb0bb2023-01-11 09:46:29 +08004674
developer326d4232023-06-15 16:45:30 +08004675 int count = 0;
4676 token_channel = strtok(possible_channels, ",");
4677 while (token_channel != NULL) {
4678 int channel = atoi(token_channel);
4679 if (channel <= MAX_CHANNEL_NUMBER && skip_table[channel] == CHANNEL_AVAILABLE) {
4680 count += snprintf(&output_pool[count], CHANNEL_LIST_MAX_LENGTH-count, "%d,", channel);
4681 if (count >= CHANNEL_LIST_MAX_LENGTH-1)
4682 break;
4683 }
4684 token_channel = strtok(NULL, ",");
4685 }
4686 //delete the last one ','
4687 if (count >0 && output_pool[count-1] == ',')
4688 output_pool[count-1] = '\0';
developera3511852023-06-14 14:12:59 +08004689 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004690}
4691
4692INT wifi_setRadioDCSChannelPool(INT radioIndex, CHAR *pool) //RDKB
4693{
developer326d4232023-06-15 16:45:30 +08004694 char config_file_dat[128] = {0};
4695 struct params dat = {0};
4696 wifi_band band = band_invalid;
4697 char new_pool[128] = {0};
developer75bd10c2023-06-27 11:34:08 +08004698 int res;
developer326d4232023-06-15 16:45:30 +08004699
4700 if (NULL == pool)
4701 return RETURN_ERR;
4702
developer9ce44382023-06-28 11:09:37 +08004703 strncpy(new_pool, pool, sizeof(new_pool) - 1);
4704 new_pool[sizeof(new_pool) - 1] = '\0';
developer326d4232023-06-15 16:45:30 +08004705 for (int i = 0; new_pool[i] != '\0'; i++) {
4706 if (new_pool[i] == ',')
4707 new_pool[i] = ';';
4708 }
4709
4710 dat.name = "AutoChannelSkipList";
4711 dat.value = new_pool;
4712 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004713 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4714 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4715 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4716 return RETURN_ERR;
4717 }
developer326d4232023-06-15 16:45:30 +08004718 if (wifi_datfileWrite(config_file_dat, &dat, 1) != 0)
4719 return RETURN_ERR;
4720 wifi_reloadAp(radioIndex);
4721
developera3511852023-06-14 14:12:59 +08004722 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004723}
4724
4725INT wifi_getRadioDCSScanTime(INT radioIndex, INT *output_interval_seconds, INT *output_dwell_milliseconds)
4726{
developera3511852023-06-14 14:12:59 +08004727 if (NULL == output_interval_seconds || NULL == output_dwell_milliseconds)
4728 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004729 //Should refresh period time be filled in here? output_interval_seconds is INT type
4730 //wifi_getRadioAutoChannelRefreshPeriod is Ulong type
4731 *output_interval_seconds = 1800;
4732 *output_dwell_milliseconds = 200;
developer72fb0bb2023-01-11 09:46:29 +08004733
developera3511852023-06-14 14:12:59 +08004734 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004735}
4736
4737INT wifi_setRadioDCSScanTime(INT radioIndex, INT interval_seconds, INT dwell_milliseconds)
4738{
developera3511852023-06-14 14:12:59 +08004739 //Set to wifi config. And apply instantly.
4740 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004741}
4742
4743INT wifi_getRadioDfsAtBootUpEnable(INT radioIndex, BOOL *output_bool) //Tr181
4744{
developera3511852023-06-14 14:12:59 +08004745 if (output_bool == NULL)
4746 return RETURN_ERR;
4747 *output_bool = true;
4748 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004749}
4750
4751INT wifi_setRadioDfsAtBootUpEnable(INT radioIndex, BOOL enable) //Tr181
4752{
developera3511852023-06-14 14:12:59 +08004753 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004754}
4755
4756//Get the Dfs enable status
4757INT wifi_getRadioDfsEnable(INT radioIndex, BOOL *output_bool) //Tr181
4758{
developera3511852023-06-14 14:12:59 +08004759 char buf[16] = {0};
4760 char config_file_dat[128] = {0};
4761 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004762 int res;
developer72fb0bb2023-01-11 09:46:29 +08004763
developera3511852023-06-14 14:12:59 +08004764 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004765
developera3511852023-06-14 14:12:59 +08004766 if (output_bool == NULL)
4767 return RETURN_ERR;
4768 *output_bool = TRUE; // default
4769 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004770 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4771 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4772 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4773 return RETURN_ERR;
4774 }
developerd1824452023-05-18 12:30:04 +08004775
developera3511852023-06-14 14:12:59 +08004776 wifi_datfileRead(config_file_dat, "DfsEnable", buf, sizeof(buf));
developerd1824452023-05-18 12:30:04 +08004777
developera3511852023-06-14 14:12:59 +08004778 if (strncmp(buf, "0", 1) == 0)
4779 *output_bool = FALSE;
4780 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4781 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004782}
4783
4784//Set the Dfs enable status
4785INT wifi_setRadioDfsEnable(INT radioIndex, BOOL enable) //Tr181
4786{
developera3511852023-06-14 14:12:59 +08004787 char config_dat_file[128] = {0};
4788 FILE *f = NULL;
4789 struct params dat = {0};
4790 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08004791 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +08004792
developera3511852023-06-14 14:12:59 +08004793 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004794
developera3511852023-06-14 14:12:59 +08004795 f = fopen(DFS_ENABLE_FILE, "w");
4796 if (f == NULL)
4797 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08004798 ret = fprintf(f, "%d", enable);
4799 if (ret < 0)
4800 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developerd14dff12023-06-28 22:47:44 +08004801 if (fclose(f) != 0) {
4802 wifi_debug(DEBUG_ERROR, "fclose fail\n");
4803 return RETURN_ERR;
4804 }
developer72fb0bb2023-01-11 09:46:29 +08004805
developera3511852023-06-14 14:12:59 +08004806 wifi_setRadioIEEE80211hEnabled(radioIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08004807
developera3511852023-06-14 14:12:59 +08004808 dat.name = "DfsEnable";
4809 dat.value = enable?"1":"0";
4810 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004811 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4812 if (os_snprintf_error(sizeof(config_dat_file), res)) {
4813 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4814 return RETURN_ERR;
4815 }
4816
developera3511852023-06-14 14:12:59 +08004817 wifi_datfileWrite(config_dat_file, &dat, 1);
4818 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4819 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004820}
4821
4822//Check if the driver support the AutoChannelRefreshPeriod
4823INT wifi_getRadioAutoChannelRefreshPeriodSupported(INT radioIndex, BOOL *output_bool) //Tr181
4824{
developera3511852023-06-14 14:12:59 +08004825 if (NULL == output_bool)
4826 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004827 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004828
developera3511852023-06-14 14:12:59 +08004829 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004830}
4831
developer326d4232023-06-15 16:45:30 +08004832
4833int get_ACS_RefreshPeriod_callback(struct nl_msg *msg, void *arg)
4834{
4835 ULONG *data = (ULONG *)arg;
4836 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4837 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
4838 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4839 int err = 0;
4840
4841 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4842 genlmsg_attrlen(gnlh, 0), NULL);
4843 if (err < 0)
4844 return NL_SKIP;
4845
4846 if (tb[NL80211_ATTR_VENDOR_DATA]) {
4847 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
4848 tb[NL80211_ATTR_VENDOR_DATA], NULL);
4849 if (err < 0)
4850 return NL_SKIP;
4851
4852 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]) {
4853 *data = nla_get_u32(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]);
4854 }
4855 }
4856
4857 return NL_OK;
4858}
4859
developer72fb0bb2023-01-11 09:46:29 +08004860//Get the ACS refresh period in seconds
4861INT wifi_getRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG *output_ulong) //Tr181
4862{
developer326d4232023-06-15 16:45:30 +08004863 char interface_name[IF_NAME_SIZE] = {0};
4864 int ret = -1;
4865 unsigned int if_idx = 0;
4866 struct unl unl_ins;
4867 struct nl_msg *msg = NULL;
4868 struct nlattr * msg_data = NULL;
4869 struct mtk_nl80211_param param;
4870 unsigned long checktime = 0;
4871
4872 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004873 if (NULL == output_ulong)
4874 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004875
4876 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4877 return RETURN_ERR;
4878
4879 if_idx = if_nametoindex(interface_name);
4880 if (!if_idx) {
4881 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4882 return RETURN_ERR;
4883 }
4884 /*init mtk nl80211 vendor cmd*/
4885 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
4886 param.if_type = NL80211_ATTR_IFINDEX;
4887 param.if_idx = if_idx;
developer72fb0bb2023-01-11 09:46:29 +08004888
developer326d4232023-06-15 16:45:30 +08004889 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4890 if (ret) {
4891 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4892 return RETURN_ERR;
4893 }
4894
4895 /*add mtk vendor cmd data*/
4896 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD, 0)) {
4897 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD attribute error\n");
4898 nlmsg_free(msg);
4899 goto err;
4900 }
4901
4902 /*send mtk nl80211 vendor msg*/
4903 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_ACS_RefreshPeriod_callback, &checktime);
4904
4905 if (ret) {
4906 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4907 goto err;
4908 }
4909 /*deinit mtk nl80211 vendor msg*/
4910 mtk_nl80211_deint(&unl_ins);
4911 *output_ulong = checktime;
4912 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4913
4914 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004915 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004916err:
4917 mtk_nl80211_deint(&unl_ins);
4918 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
4919 return RETURN_ERR;
4920
developer72fb0bb2023-01-11 09:46:29 +08004921}
4922
4923//Set the ACS refresh period in seconds
4924INT wifi_setRadioDfsRefreshPeriod(INT radioIndex, ULONG seconds) //Tr181
4925{
developer326d4232023-06-15 16:45:30 +08004926 char interface_name[IF_NAME_SIZE] = {0};
4927 int ret = -1;
4928 unsigned int if_idx = 0;
4929 struct unl unl_ins;
4930 struct nl_msg *msg = NULL;
4931 struct nlattr * msg_data = NULL;
4932 struct mtk_nl80211_param param;
4933
4934 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4935
4936 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4937 return RETURN_ERR;
4938
4939 if_idx = if_nametoindex(interface_name);
4940 if (!if_idx) {
4941 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4942 return RETURN_ERR;
4943 }
4944 /*init mtk nl80211 vendor cmd*/
4945 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AUTO_CH_SEL;
4946 param.if_type = NL80211_ATTR_IFINDEX;
4947 param.if_idx = if_idx;
4948
4949 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4950 if (ret) {
4951 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4952 return RETURN_ERR;
4953 }
4954
4955 /*add mtk vendor cmd data*/
4956 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME, seconds)) {
4957 wifi_debug(DEBUG_ERROR, "Nla put MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME attribute error\n");
4958 nlmsg_free(msg);
4959 goto err;
4960 }
4961
4962 /*send mtk nl80211 vendor msg*/
4963 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
4964
4965 if (ret) {
4966 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4967 goto err;
4968 }
4969 /*deinit mtk nl80211 vendor msg*/
4970 mtk_nl80211_deint(&unl_ins);
4971 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4972
4973 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4974 return RETURN_OK;
4975err:
4976 mtk_nl80211_deint(&unl_ins);
4977 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developera3511852023-06-14 14:12:59 +08004978 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004979}
4980
4981//Get the Operating Channel Bandwidth. eg "20MHz", "40MHz", "80MHz", "80+80", "160"
4982//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.
4983INT wifi_getRadioOperatingChannelBandwidth(INT radioIndex, CHAR *output_string) //Tr181
4984{
developera3511852023-06-14 14:12:59 +08004985 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
4986 char extchannel[128] = {0};
4987 char interface_name[64] = {0};
developere40952c2023-06-15 18:46:43 +08004988 int ret = 0, len=0, res;
developera3511852023-06-14 14:12:59 +08004989 BOOL radio_enable = FALSE;
4990 wifi_band band;
developer72fb0bb2023-01-11 09:46:29 +08004991
developera3511852023-06-14 14:12:59 +08004992 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004993
developera3511852023-06-14 14:12:59 +08004994 if (NULL == output_string) {
4995 WIFI_ENTRY_EXIT_DEBUG("output_string is nuill %s: %d \n", __func__, __LINE__);
4996 return RETURN_ERR;
4997 }
4998 if (wifi_getRadioEnable(radioIndex, &radio_enable) == RETURN_ERR) {
4999 WIFI_ENTRY_EXIT_DEBUG("wifi_getRadioEnable failed %s: %d \n", __func__, __LINE__);
5000 return RETURN_ERR;
5001 }
5002 if (radio_enable != TRUE) {
5003 WIFI_ENTRY_EXIT_DEBUG("Radio %d is not enable failed %s: %d \n", radioIndex, __func__, __LINE__);
5004 return RETURN_OK;
5005 }
5006 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5007 return RETURN_ERR;
5008 /*IW command get BW320 to do*/
developerd1824452023-05-18 12:30:04 +08005009
developere40952c2023-06-15 18:46:43 +08005010 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'width' | cut -d ' ' -f6 | tr -d '\\n'", interface_name);
5011 if (os_snprintf_error(sizeof(cmd), res)) {
5012 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5013 return RETURN_ERR;
5014 }
developera3511852023-06-14 14:12:59 +08005015 ret = _syscmd(cmd, buf, sizeof(buf));
5016 len = strlen(buf);
5017 if((ret != 0) || (len == 0))
5018 {
5019 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
5020 return RETURN_ERR;
5021 }
developer8666b312023-03-24 14:05:31 +08005022
developera3511852023-06-14 14:12:59 +08005023 band = wifi_index_to_band(radioIndex);
5024 if (band == band_2_4 && strncmp(buf, "20", 2) == 0) {
5025 wifi_getRadioExtChannel(radioIndex, extchannel);
developere40952c2023-06-15 18:46:43 +08005026 if (strncmp(extchannel, "Auto", 4) != 0) {
5027 res = snprintf(buf, sizeof(buf), "40");
5028 if (os_snprintf_error(sizeof(buf), res)) {
5029 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5030 return RETURN_ERR;
5031 }
5032 }
5033 }
5034 res = snprintf(output_string, 64, "%sMHz", buf);
5035 if (os_snprintf_error(64, res)) {
5036 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5037 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08005038 }
developera3511852023-06-14 14:12:59 +08005039 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005040
developera3511852023-06-14 14:12:59 +08005041 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005042}
5043
5044enum mwctl_chan_width {
5045 MWCTL_CHAN_WIDTH_20,
5046 MWCTL_CHAN_WIDTH_40,
5047 MWCTL_CHAN_WIDTH_80,
5048 MWCTL_CHAN_WIDTH_160,
5049 MWCTL_CHAN_WIDTH_320,
5050};
5051
5052struct bw_option {
5053 unsigned int bandwith;
5054 enum mwctl_chan_width mode;
5055};
5056
5057struct bw_option bw_opt[] = {
5058 {20, MWCTL_CHAN_WIDTH_20},
5059 {40, MWCTL_CHAN_WIDTH_40},
5060 {80, MWCTL_CHAN_WIDTH_80},
5061 {160, MWCTL_CHAN_WIDTH_160},
5062 {320, MWCTL_CHAN_WIDTH_320},
5063};
5064
5065INT wifi_setChannel_netlink(INT radioIndex, UINT* channel, UINT *bandwidth)
5066{
5067 int ret = -1;
5068 int i;
5069 struct unl unl_ins;
5070 struct nl_msg *msg = NULL;
5071 struct nlattr * msg_data = NULL;
5072 struct mtk_nl80211_param param;
5073 bool b_match = FALSE;
5074
5075 /*init mtk nl80211 vendor cmd*/
5076 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_CHANNEL;
5077 param.if_type = NL80211_ATTR_WIPHY;
5078 param.if_idx = radio_index_to_phy(radioIndex);
5079
5080 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5081 if (ret) {
5082 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5083 return RETURN_ERR;
5084 }
5085
5086 /*add mtk vendor cmd data*/
5087 if (channel != NULL)
5088 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_NUM, *channel)) {
5089 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_NUM attribute error\n");
5090 nlmsg_free(msg);
5091 goto err;
5092 }
5093
5094 if (bandwidth != NULL) {
5095 for (i = 0; i < (sizeof(bw_opt)/sizeof(bw_opt[0])); i++) {
5096 if (bw_opt[i].bandwith == *bandwidth) {
5097 b_match = true;
5098 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_BW, bw_opt[i].mode)) {
5099 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_BW attribute error\n");
5100 nlmsg_free(msg);
5101 goto err;
5102 }
5103 break;
5104 }
5105 }
5106
5107 if (!b_match) {
5108 wifi_debug(DEBUG_ERROR, "Cannot find bandwith error\n");
5109 nlmsg_free(msg);
5110 goto err;
5111 }
5112 }
5113
5114 /*send mtk nl80211 vendor msg*/
5115 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5116 if (ret) {
5117 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5118 goto err;
5119 }
5120 /*deinit mtk nl80211 vendor msg*/
5121 mtk_nl80211_deint(&unl_ins);
5122 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developera3511852023-06-14 14:12:59 +08005123 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developerfead3972023-05-25 20:15:02 +08005124
developera3511852023-06-14 14:12:59 +08005125 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005126err:
5127 mtk_nl80211_deint(&unl_ins);
5128 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5129 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005130}
developerfead3972023-05-25 20:15:02 +08005131
developer72fb0bb2023-01-11 09:46:29 +08005132//Set the Operating Channel Bandwidth.
5133INT wifi_setRadioOperatingChannelBandwidth(INT radioIndex, CHAR *bandwidth) //Tr181 //AP only
5134{
developera3511852023-06-14 14:12:59 +08005135 char config_file[128];
5136 char ht_value[16];
5137 char vht_value[16];
5138 char eht_value[16];
5139 struct params dat[3];
5140 wifi_band band = band_invalid;
5141 unsigned int bw = 20;
developere40952c2023-06-15 18:46:43 +08005142 int ret = 0, res1, res2, res3;
developer72fb0bb2023-01-11 09:46:29 +08005143
developera3511852023-06-14 14:12:59 +08005144 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005145
developera3511852023-06-14 14:12:59 +08005146 if(NULL == bandwidth)
5147 return RETURN_ERR;
5148 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08005149
developera3511852023-06-14 14:12:59 +08005150 if(strstr(bandwidth,"320") != NULL) {
developere40952c2023-06-15 18:46:43 +08005151 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5152 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5153 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_320);
developera3511852023-06-14 14:12:59 +08005154 bw = 320;
5155 } else if(strstr(bandwidth,"160") != NULL) {
developere40952c2023-06-15 18:46:43 +08005156 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5157 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5158 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_160);
developera3511852023-06-14 14:12:59 +08005159 bw = 160;
5160 } else if(strstr(bandwidth,"80") != NULL) {
developere40952c2023-06-15 18:46:43 +08005161 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5162 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_80);
5163 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_80);
developera3511852023-06-14 14:12:59 +08005164 bw = 80;
5165 } else if(strstr(bandwidth,"40") != NULL) {
developere40952c2023-06-15 18:46:43 +08005166 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5167 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5168 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_40);
developera3511852023-06-14 14:12:59 +08005169 bw = 40;
5170 } else if(strstr(bandwidth,"20") != NULL) {
developere40952c2023-06-15 18:46:43 +08005171 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_20);
5172 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5173 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_20);
developera3511852023-06-14 14:12:59 +08005174 bw = 20;
5175 } else {
developer37646972023-06-29 10:58:43 +08005176 if (fprintf(stderr, "%s: Invalid Bandwidth %s\n", __func__, bandwidth) < 0)
5177 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005178 return RETURN_ERR;
5179 }
developer72fb0bb2023-01-11 09:46:29 +08005180
developer37646972023-06-29 10:58:43 +08005181 if (os_snprintf_error(sizeof(ht_value), res1) ||
5182 os_snprintf_error(sizeof(vht_value), res2) ||
5183 os_snprintf_error(sizeof(eht_value), res3)) {
developere40952c2023-06-15 18:46:43 +08005184 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5185 return RETURN_ERR;
5186 }
5187
5188 res1 = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5189 if (os_snprintf_error(sizeof(config_file), res1)) {
5190 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5191 return RETURN_ERR;
5192 }
developera3511852023-06-14 14:12:59 +08005193 dat[0].name = "HT_BW";
5194 dat[0].value = ht_value;
5195 dat[1].name = "VHT_BW";
5196 dat[1].value = vht_value;
5197 dat[2].name = "EHT_ApBw";
5198 dat[2].value = eht_value;
5199 wifi_datfileWrite(config_file, dat, 3);
5200 ret = wifi_setChannel_netlink(radioIndex, NULL, &bw);
developerfead3972023-05-25 20:15:02 +08005201 if (ret != RETURN_OK) {
developer37646972023-06-29 10:58:43 +08005202 if (fprintf(stderr, "%s: wifi_setChannel return error.\n", __func__) < 0)
5203 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005204 return RETURN_ERR;
5205 }
developer72fb0bb2023-01-11 09:46:29 +08005206
developera3511852023-06-14 14:12:59 +08005207 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5208 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005209}
5210
developer72fb0bb2023-01-11 09:46:29 +08005211//Get the secondary extension channel position, "AboveControlChannel" or "BelowControlChannel". (this is for 40MHz and 80MHz bandwith only)
5212//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.
5213INT wifi_getRadioExtChannel(INT radioIndex, CHAR *output_string) //Tr181
5214{
developera3511852023-06-14 14:12:59 +08005215 char config_file[64] = {0};
5216 char config_dat_file[64] = {0};
5217 char mode_str[16] = {0};
5218 char buf[64] = {0};
5219 char cmd[MAX_CMD_SIZE] = {0};
5220 char interface_name[64] = {0};
5221 int ret = 0, len=0;
5222 wifi_band band;
5223 ULONG channel = 0;
5224 int centr_channel = 0;
5225 UINT mode_map = 0;
developere40952c2023-06-15 18:46:43 +08005226 int freq=0, res;
developer72fb0bb2023-01-11 09:46:29 +08005227
developera3511852023-06-14 14:12:59 +08005228 if (output_string == NULL)
5229 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005230
developer86035662023-06-28 19:21:12 +08005231 if (wifi_getRadioMode(radioIndex, mode_str, &mode_map) != RETURN_OK) {
5232 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode fail\n");
5233 }
developer72fb0bb2023-01-11 09:46:29 +08005234
developera3511852023-06-14 14:12:59 +08005235 band = wifi_index_to_band(radioIndex);
5236 if (band == band_invalid)
5237 return RETURN_ERR;
5238 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5239 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005240
developere40952c2023-06-15 18:46:43 +08005241 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5242 if (os_snprintf_error(sizeof(config_file), res)) {
5243 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5244 return RETURN_ERR;
5245 }
5246
5247 res = snprintf(output_string, 64, "Auto");
5248 if (os_snprintf_error(64, res)) {
5249 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5250 return RETURN_ERR;
5251 }
developer72fb0bb2023-01-11 09:46:29 +08005252
developera3511852023-06-14 14:12:59 +08005253 if (band == band_2_4 || (!(mode_map&WIFI_MODE_AC) && !(mode_map&WIFI_MODE_AX))) {
5254 // 2G band or ac and ax mode is disable, we will check HT_EXTCHA
developere40952c2023-06-15 18:46:43 +08005255 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5256 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5257 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5258 return RETURN_ERR;
5259 }
developerb758dfd2023-06-21 17:32:07 +08005260
developera3511852023-06-14 14:12:59 +08005261 wifi_halgetRadioExtChannel(config_dat_file, output_string);
developer37646972023-06-29 10:58:43 +08005262 if (!(mode_map&WIFI_MODE_N)) {
developere40952c2023-06-15 18:46:43 +08005263 res = snprintf(output_string, 64, "Auto");
developer37646972023-06-29 10:58:43 +08005264 if (os_snprintf_error(64, res)) {
5265 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5266 return RETURN_ERR;
5267 }
5268 }
developera3511852023-06-14 14:12:59 +08005269 } else {
5270 // 5G and 6G band with ac or ax mode.
5271 wifi_getRadioChannel(radioIndex, &channel);
developere40952c2023-06-15 18:46:43 +08005272 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'center1' | cut -d ' ' -f9 | tr -d '\\n'", interface_name);
5273 if (os_snprintf_error(sizeof(cmd), res)) {
5274 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5275 return RETURN_ERR;
5276 }
developerd1824452023-05-18 12:30:04 +08005277
developera3511852023-06-14 14:12:59 +08005278 ret = _syscmd(cmd, buf, sizeof(buf));
5279 len = strlen(buf);
5280 if((ret != 0) || (len == 0))
5281 {
5282 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
5283 return RETURN_ERR;
5284 }
5285 sscanf(buf, "%d", &freq);
5286 centr_channel = ieee80211_frequency_to_channel(freq);
5287 if (centr_channel > (int)channel)
developere40952c2023-06-15 18:46:43 +08005288 res = snprintf(output_string, 64, "AboveControlChannel");
developera3511852023-06-14 14:12:59 +08005289 else
developere40952c2023-06-15 18:46:43 +08005290 res = snprintf(output_string, 64, "BelowControlChannel");
5291
5292 if (os_snprintf_error(64, res)) {
5293 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5294 return RETURN_ERR;
5295 }
developera3511852023-06-14 14:12:59 +08005296 }
developer72fb0bb2023-01-11 09:46:29 +08005297
developera3511852023-06-14 14:12:59 +08005298 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005299}
5300
5301//Set the extension channel.
5302INT wifi_setRadioExtChannel(INT radioIndex, CHAR *string) //Tr181 //AP only
developer69b61b02023-03-07 17:17:44 +08005303{
developera3511852023-06-14 14:12:59 +08005304 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5305 struct params params={0};
5306 char config_file[64] = {0};
5307 char config_dat_file[64] = {0};
5308 char ext_channel[64] = {0};
5309 char buf[128] = {0};
5310 char cmd[128] = {0};
developerc14d83a2023-06-29 20:09:42 +08005311 int max_radio_num =0, ret = 0;
5312 long int bandwidth = 0;
developera3511852023-06-14 14:12:59 +08005313 unsigned long channel = 0;
5314 bool stbcEnable = FALSE;
5315 params.name = "ht_capab";
5316 wifi_band band;
developere40952c2023-06-15 18:46:43 +08005317 int res;
developer72fb0bb2023-01-11 09:46:29 +08005318
developer75bd10c2023-06-27 11:34:08 +08005319 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5320 if (os_snprintf_error(sizeof(config_file), res)) {
5321 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5322 return RETURN_ERR;
5323 }
5324
developere40952c2023-06-15 18:46:43 +08005325 res = snprintf(cmd, sizeof(cmd), "cat %s | grep STBC", config_file);
5326 if (os_snprintf_error(sizeof(cmd), res)) {
5327 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5328 return RETURN_ERR;
5329 }
developera3511852023-06-14 14:12:59 +08005330 _syscmd(cmd, buf, sizeof(buf));
5331 if (strlen(buf) != 0)
5332 stbcEnable = TRUE;
5333 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, buf) != RETURN_OK)
5334 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08005335 if (hal_strtol(buf, 10, &bandwidth) < 0) {
5336 wifi_debug(DEBUG_ERROR, "strtol fail\n");
5337 }
developera3511852023-06-14 14:12:59 +08005338 // TDK expected to get error with 20MHz
5339 // we handle 20MHz in function wifi_RemoveRadioExtChannel().
5340 if (bandwidth == 20 || strstr(buf, "80+80") != NULL)
5341 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005342
developera3511852023-06-14 14:12:59 +08005343 band = wifi_index_to_band(radioIndex);
5344 if (band == band_invalid)
5345 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005346
developera3511852023-06-14 14:12:59 +08005347 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK)
5348 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005349
developerc14d83a2023-06-29 20:09:42 +08005350 res = snprintf(buf, sizeof(buf), "HT%ld", bandwidth);
developere40952c2023-06-15 18:46:43 +08005351 if (os_snprintf_error(sizeof(buf), res)) {
5352 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5353 return RETURN_ERR;
5354 }
developera3511852023-06-14 14:12:59 +08005355 ret = util_get_sec_chan_offset(channel, buf);
5356 if (ret == -EINVAL)
5357 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005358
developera3511852023-06-14 14:12:59 +08005359 if(NULL!= strstr(string,"Above")) {
5360 if ((band == band_2_4 && channel > 9) || (band == band_5 && ret == -1))
5361 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005362 memcpy(ext_channel, "1", strlen("1"));
developera3511852023-06-14 14:12:59 +08005363 } else if(NULL!= strstr(string,"Below")) {
5364 if ((band == band_2_4 && channel < 5) || (band == band_5 && ret == -1))
5365 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005366 memcpy(ext_channel, "0", strlen("0"));
developera3511852023-06-14 14:12:59 +08005367 } else {
5368 printf("%s: invalid EXT_CHA:%s\n", __func__, string);
developer262f4cb2023-05-24 12:22:04 +08005369 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08005370 }
5371 params.name = "HT_EXTCHA";
5372 params.value = ext_channel;
developer72fb0bb2023-01-11 09:46:29 +08005373
developere75ba632023-06-29 16:03:33 +08005374 res = snprintf (config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08005375 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5376 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5377 return RETURN_ERR;
5378 }
developera3511852023-06-14 14:12:59 +08005379 wifi_datfileWrite(config_dat_file, &params, 1);
developerd1824452023-05-18 12:30:04 +08005380
developera3511852023-06-14 14:12:59 +08005381 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08005382 if(max_radio_num== 0){
5383 return RETURN_ERR;
5384 }
developera3511852023-06-14 14:12:59 +08005385 for(int i=0; i<=MAX_APS/max_radio_num; i++)
5386 {
developer32f2a182023-06-27 19:50:41 +08005387 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex+(max_radio_num*i));
5388 if (os_snprintf_error(sizeof(config_file), res)) {
5389 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5390 return RETURN_ERR;
5391 }
developera3511852023-06-14 14:12:59 +08005392 wifi_setRadioSTBCEnable(radioIndex+(max_radio_num*i), stbcEnable);
5393 }
developer72fb0bb2023-01-11 09:46:29 +08005394
developera3511852023-06-14 14:12:59 +08005395 //Set to wifi config only. Wait for wifi reset or wifi_pushRadioChannel to apply.
5396 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5397 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005398}
5399
5400//Get the guard interval value. eg "400nsec" or "800nsec"
5401//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.
5402INT wifi_getRadioGuardInterval(INT radioIndex, CHAR *output_string) //Tr181
5403{
developera3511852023-06-14 14:12:59 +08005404 wifi_guard_interval_t GI;
developer32f2a182023-06-27 19:50:41 +08005405 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08005406
developera3511852023-06-14 14:12:59 +08005407 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005408
developera3511852023-06-14 14:12:59 +08005409 if (output_string == NULL || wifi_getGuardInterval(radioIndex, &GI) == RETURN_ERR)
5410 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005411
developer32f2a182023-06-27 19:50:41 +08005412 if (GI == wifi_guard_interval_400) {
5413 len = strlen("400nsec");
5414 memcpy(output_string, "400nsec", len);
developer5b23cd02023-07-19 20:26:03 +08005415 } else if (GI == wifi_guard_interval_800) {
developer32f2a182023-06-27 19:50:41 +08005416 len = strlen("800nsec");
5417 memcpy(output_string, "800nsec", strlen("800nsec"));
5418 } else if (GI == wifi_guard_interval_1600) {
5419 len = strlen("1600nsec");
5420 memcpy(output_string, "1600nsec", strlen("1600nsec"));
5421 } else if (GI == wifi_guard_interval_3200) {
5422 len = strlen("3200nsec");
5423 memcpy(output_string, "3200nsec", strlen("3200nsec"));
5424 } else {
5425 len = strlen("Auto");
5426 memcpy(output_string, "Auto", strlen("Auto"));
5427 }
5428 output_string[len] = '\0';
developera3511852023-06-14 14:12:59 +08005429 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5430 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005431}
5432
5433//Set the guard interval value.
5434INT wifi_setRadioGuardInterval(INT radioIndex, CHAR *string) //Tr181
5435{
developera3511852023-06-14 14:12:59 +08005436 wifi_guard_interval_t GI;
5437 int ret = 0;
developer72fb0bb2023-01-11 09:46:29 +08005438
developera3511852023-06-14 14:12:59 +08005439 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005440
developera3511852023-06-14 14:12:59 +08005441 if (strcmp(string, "400nsec") == 0)
5442 GI = wifi_guard_interval_400;
5443 else if (strcmp(string , "800nsec") == 0)
5444 GI = wifi_guard_interval_800;
5445 else if (strcmp(string , "1600nsec") == 0)
5446 GI = wifi_guard_interval_1600;
5447 else if (strcmp(string , "3200nsec") == 0)
5448 GI = wifi_guard_interval_3200;
5449 else
5450 GI = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +08005451
developera3511852023-06-14 14:12:59 +08005452 ret = wifi_setGuardInterval(radioIndex, GI);
developer72fb0bb2023-01-11 09:46:29 +08005453
developera3511852023-06-14 14:12:59 +08005454 if (ret == RETURN_ERR) {
5455 wifi_dbg_printf("%s: wifi_setGuardInterval return error\n", __func__);
5456 return RETURN_ERR;
5457 }
developer72fb0bb2023-01-11 09:46:29 +08005458
developera3511852023-06-14 14:12:59 +08005459 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5460 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005461}
5462
5463//Get the Modulation Coding Scheme index, eg: "-1", "1", "15"
5464INT wifi_getRadioMCS(INT radioIndex, INT *output_int) //Tr181
5465{
developera3511852023-06-14 14:12:59 +08005466 char buf[32]={0};
5467 char mcs_file[64] = {0};
5468 char cmd[MAX_CMD_SIZE] = {0};
5469 UINT mode_bitmap = 0;
developere40952c2023-06-15 18:46:43 +08005470 int res;
developerc14d83a2023-06-29 20:09:42 +08005471 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08005472
developera3511852023-06-14 14:12:59 +08005473 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5474 if(output_int == NULL)
5475 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005476 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5477 if (os_snprintf_error(sizeof(mcs_file), res)) {
5478 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5479 return RETURN_ERR;
5480 }
5481
5482 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", mcs_file);
5483 if (os_snprintf_error(sizeof(cmd), res)) {
5484 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5485 return RETURN_ERR;
5486 }
developer72fb0bb2023-01-11 09:46:29 +08005487
developera3511852023-06-14 14:12:59 +08005488 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08005489 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005490 if (hal_strtol(buf, 10, &tmp) < 0) {
5491 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005492 }
developerc14d83a2023-06-29 20:09:42 +08005493 *output_int = tmp;
developerd14dff12023-06-28 22:47:44 +08005494 } else {
developera3511852023-06-14 14:12:59 +08005495 // output the max MCS for the current radio mode
5496 if (wifi_getRadioMode(radioIndex, buf, &mode_bitmap) == RETURN_ERR) {
5497 wifi_dbg_printf("%s: wifi_getradiomode return error.\n", __func__);
5498 return RETURN_ERR;
5499 }
5500 if (mode_bitmap & WIFI_MODE_AX) {
5501 *output_int = 11;
5502 } else if (mode_bitmap & WIFI_MODE_AC) {
5503 *output_int = 9;
5504 } else if (mode_bitmap & WIFI_MODE_N) {
5505 *output_int = 7;
5506 }
5507 }
5508 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005509
developera3511852023-06-14 14:12:59 +08005510 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005511}
5512
5513//Set the Modulation Coding Scheme index
5514INT wifi_setRadioMCS(INT radioIndex, INT MCS) //Tr181
5515{
developera3511852023-06-14 14:12:59 +08005516 /*Only HE mode can specify MCS capability. We don't support MCS in HT mode,
5517 because that would be ambiguous (MCS code 8~11 refer to 2 NSS in HT but 1 NSS in HE adn VHT).*/
5518 char config_file[64] = {0};
5519 char set_value[16] = {0};
5520 char mcs_file[32] = {0};
5521 struct params set_config = {0};
5522 FILE *f = NULL;
5523 INT nss = 0;
5524 int ant_bitmap = 0;
5525 unsigned short cal_value = 0;
5526 UCHAR tval = 0, i = 0;
developere40952c2023-06-15 18:46:43 +08005527 int res;
developer72fb0bb2023-01-11 09:46:29 +08005528
developera3511852023-06-14 14:12:59 +08005529 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005530
developere40952c2023-06-15 18:46:43 +08005531 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5532 if (os_snprintf_error(sizeof(config_file), res)) {
5533 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5534 return RETURN_ERR;
5535 }
developer72fb0bb2023-01-11 09:46:29 +08005536
developera3511852023-06-14 14:12:59 +08005537 // -1 means auto
5538 if (MCS > 15 || MCS < -1) {
developer75bd10c2023-06-27 11:34:08 +08005539 wifi_debug(DEBUG_ERROR, "invalid MCS %d\n", MCS);
developera3511852023-06-14 14:12:59 +08005540 return RETURN_ERR;
5541 }
5542 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);/*nss is a bit map value,1111*/
5543 for(; ant_bitmap > 0; ant_bitmap >>= 1)
5544 nss += 1;
5545 //printf("%s:nss = %d\n", __func__, nss);
5546 /*16-bit combination of 2-bit values of Max HE-MCS For 1..8 SS;each 2-bit value have following meaning:
5547 0 = HE-MCS 0-7, 1 = HE-MCS 0-9, 2 = HE-MCS 0-11, 3 = not supported*/
5548 if (MCS > 9 || MCS == -1)
5549 tval = 2;/*one stream value*/
5550 else if (MCS > 7)
5551 tval = 1;
5552 else
5553 tval = 0;
5554 for (i = 0; i < nss; i++)
5555 cal_value |= (tval << (2*i));
developere40952c2023-06-15 18:46:43 +08005556 res = snprintf(set_value, sizeof(set_value), "%x", cal_value);
5557 if (os_snprintf_error(sizeof(set_value), res)) {
5558 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5559 return RETURN_ERR;
5560 }
5561
developera3511852023-06-14 14:12:59 +08005562 WIFI_ENTRY_EXIT_DEBUG("%s:set=%s, cal=%x\n", __func__, set_value, cal_value);
5563 set_config.name = "he_basic_mcs_nss_set";/*He capability in beacon or response*/
5564 set_config.value = set_value;
developer72fb0bb2023-01-11 09:46:29 +08005565
developera3511852023-06-14 14:12:59 +08005566 wifi_hostapdWrite(config_file, &set_config, 1);
5567 wifi_hostapdProcessUpdate(radioIndex, &set_config, 1);
developer72fb0bb2023-01-11 09:46:29 +08005568
developera3511852023-06-14 14:12:59 +08005569 // 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 +08005570 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5571 if (os_snprintf_error(sizeof(mcs_file), res)) {
5572 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5573 return RETURN_ERR;
5574 }
5575
developera3511852023-06-14 14:12:59 +08005576 f = fopen(mcs_file, "w");
5577 if (f == NULL) {
developere75ba632023-06-29 16:03:33 +08005578 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
5579 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005580 return RETURN_ERR;
5581 }
developere75ba632023-06-29 16:03:33 +08005582 if (fprintf(f, "%d", MCS) < 0)
5583 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
5584 if (fclose(f) == EOF) {
5585 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
5586 return RETURN_ERR;
5587 }
developer72fb0bb2023-01-11 09:46:29 +08005588
developera3511852023-06-14 14:12:59 +08005589 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5590 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005591}
5592
5593//Get supported Transmit Power list, eg : "0,25,50,75,100"
5594//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.
5595INT wifi_getRadioTransmitPowerSupported(INT radioIndex, CHAR *output_list) //Tr181
5596{
developere40952c2023-06-15 18:46:43 +08005597 int res;
5598 if (NULL == output_list)
5599 return RETURN_ERR;
5600 res = snprintf(output_list, 64,"0,25,50,75,100");
5601 if (os_snprintf_error(64, res)) {
5602 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5603 return RETURN_ERR;
5604 }
5605
5606 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005607}
5608
5609//Get current Transmit Power in dBm units.
5610//The transmite power level is in units of full power for this radio.
5611INT wifi_getRadioTransmitPower(INT radioIndex, ULONG *output_ulong) //RDKB
5612{
developera3511852023-06-14 14:12:59 +08005613 char interface_name[16] = {0};
5614 char cmd[MAX_CMD_SIZE]={0};
5615 char buf[16]={0};
developera1255e42023-05-13 17:45:02 +08005616 char pwr_file[128]={0};
developere40952c2023-06-15 18:46:43 +08005617 int res;
developera1255e42023-05-13 17:45:02 +08005618
developera3511852023-06-14 14:12:59 +08005619 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005620
developera3511852023-06-14 14:12:59 +08005621 if(output_ulong == NULL)
5622 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005623
developera3511852023-06-14 14:12:59 +08005624 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5625 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005626 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5627 if (os_snprintf_error(sizeof(pwr_file), res)) {
5628 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5629 return RETURN_ERR;
5630 }
5631
5632 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", pwr_file);
5633 if (os_snprintf_error(sizeof(cmd), res)) {
5634 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5635 return RETURN_ERR;
5636 }
5637
developera1255e42023-05-13 17:45:02 +08005638 _syscmd(cmd, buf, sizeof(buf));
developer5b23cd02023-07-19 20:26:03 +08005639 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005640 if (hal_strtoul(buf, 10, output_ulong) < 0) {
5641 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005642 }
5643 } else
developera1255e42023-05-13 17:45:02 +08005644 *output_ulong = 100;
developera3511852023-06-14 14:12:59 +08005645 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5646 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005647}
5648
5649//Set Transmit Power
5650//The transmite power level is in units of full power for this radio.
5651INT wifi_setRadioTransmitPower(INT radioIndex, ULONG TransmitPower) //RDKB
5652{
developera3511852023-06-14 14:12:59 +08005653 char interface_name[16] = {0};
5654 char *support;
5655 char buf[128]={0};
5656 char txpower_str[64] = {0};
developera1255e42023-05-13 17:45:02 +08005657 char pwr_file[128]={0};
developera3511852023-06-14 14:12:59 +08005658 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +08005659 int if_idx, ret = 0;
5660 struct nl_msg *msg = NULL;
5661 struct nlattr * msg_data = NULL;
5662 struct mtk_nl80211_param param;
5663 struct unl unl_ins;
developere40952c2023-06-15 18:46:43 +08005664 int res;
developer72fb0bb2023-01-11 09:46:29 +08005665
developera3511852023-06-14 14:12:59 +08005666 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005667
developera3511852023-06-14 14:12:59 +08005668 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5669 return RETURN_ERR;
5670 // Get the Tx power supported list and check that is the input in the list
developere40952c2023-06-15 18:46:43 +08005671 res = snprintf(txpower_str, sizeof(txpower_str), "%lu", TransmitPower);
5672 if (os_snprintf_error(sizeof(txpower_str), res)) {
5673 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5674 return RETURN_ERR;
5675 }
developera3511852023-06-14 14:12:59 +08005676 wifi_getRadioTransmitPowerSupported(radioIndex, buf);
5677 support = strtok(buf, ",");
5678 while(true)
5679 {
5680 if(support == NULL) { // input not in the list
5681 wifi_dbg_printf("Input value is invalid.\n");
5682 return RETURN_ERR;
5683 }
5684 if (strncmp(txpower_str, support, strlen(support)) == 0) {
5685 break;
5686 }
5687 support = strtok(NULL, ",");
5688 }
developerfead3972023-05-25 20:15:02 +08005689
5690 if_idx = if_nametoindex(interface_name);
5691 /*init mtk nl80211 vendor cmd*/
5692 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
5693 param.if_type = NL80211_ATTR_IFINDEX;
5694 param.if_idx = if_idx;
5695 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5696 if (ret) {
5697 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5698 return RETURN_ERR;
5699 }
5700 /*add mtk vendor cmd data*/
5701 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_PERCENTAGE_EN, 1)) {
5702 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5703 nlmsg_free(msg);
5704 goto err;
5705 }
5706
5707 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_DROP_CTRL, TransmitPower)) {
5708 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5709 nlmsg_free(msg);
5710 goto err;
5711 }
5712
5713 /*send mtk nl80211 vendor msg*/
5714 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5715 if (ret) {
5716 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5717 goto err;
5718 }
5719 /*deinit mtk nl80211 vendor msg*/
5720 mtk_nl80211_deint(&unl_ins);
5721 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
5722
developere40952c2023-06-15 18:46:43 +08005723 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5724 if (os_snprintf_error(sizeof(pwr_file), res)) {
5725 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5726 return RETURN_ERR;
5727 }
5728
developera3511852023-06-14 14:12:59 +08005729 f = fopen(pwr_file, "w");
5730 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +08005731 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera3511852023-06-14 14:12:59 +08005732 return RETURN_ERR;
5733 }
5734 fprintf(f, "%lu", TransmitPower);
developerc14d83a2023-06-29 20:09:42 +08005735 if (fclose(f) == EOF)
5736 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera1255e42023-05-13 17:45:02 +08005737 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005738err:
5739 mtk_nl80211_deint(&unl_ins);
5740 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5741 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005742}
5743
5744//get 80211h Supported. 80211h solves interference with satellites and radar using the same 5 GHz frequency band
5745INT wifi_getRadioIEEE80211hSupported(INT radioIndex, BOOL *Supported) //Tr181
5746{
developera3511852023-06-14 14:12:59 +08005747 if (NULL == Supported)
5748 return RETURN_ERR;
5749 *Supported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08005750
developera3511852023-06-14 14:12:59 +08005751 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005752}
5753
5754//Get 80211h feature enable
5755INT wifi_getRadioIEEE80211hEnabled(INT radioIndex, BOOL *enable) //Tr181
5756{
developera3511852023-06-14 14:12:59 +08005757 char buf[64]={'\0'};
5758 char config_file[64] = {'\0'};
developer75bd10c2023-06-27 11:34:08 +08005759 int res;
developer72fb0bb2023-01-11 09:46:29 +08005760
developera3511852023-06-14 14:12:59 +08005761 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5762 if(enable == NULL)
5763 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005764
developer75bd10c2023-06-27 11:34:08 +08005765 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5766 if (os_snprintf_error(sizeof(config_file), res)) {
5767 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5768 return RETURN_ERR;
5769 }
developera3511852023-06-14 14:12:59 +08005770 /* wifi_hostapdRead(config_file, "ieee80211h", buf, sizeof(buf)); */
5771 wifi_datfileRead(config_file, "IEEE80211H", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08005772
developera3511852023-06-14 14:12:59 +08005773 if (strncmp(buf, "1", 1) == 0)
5774 *enable = TRUE;
5775 else
5776 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08005777
developera3511852023-06-14 14:12:59 +08005778 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5779 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005780}
5781
5782//Set 80211h feature enable
5783INT wifi_setRadioIEEE80211hEnabled(INT radioIndex, BOOL enable) //Tr181
5784{
developera3511852023-06-14 14:12:59 +08005785 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5786 struct params params={'\0'};
5787 struct params dat={0};
5788 char config_file[MAX_BUF_SIZE] = {0};
5789 char config_dat_file[MAX_BUF_SIZE] = {0};
5790 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08005791 int res;
developer72fb0bb2023-01-11 09:46:29 +08005792
developera3511852023-06-14 14:12:59 +08005793 params.name = "ieee80211h";
developer72fb0bb2023-01-11 09:46:29 +08005794
developera3511852023-06-14 14:12:59 +08005795 if (enable) {
5796 params.value = "1";
5797 } else {
5798 params.value = "0";
5799 }
developer72fb0bb2023-01-11 09:46:29 +08005800
developera3511852023-06-14 14:12:59 +08005801 dat.name = "IEEE80211H";
5802 dat.value = params.value;
developerd1824452023-05-18 12:30:04 +08005803
developera3511852023-06-14 14:12:59 +08005804 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08005805 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5806 if (os_snprintf_error(sizeof(config_file), res)) {
5807 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5808 return RETURN_ERR;
5809 }
5810
5811 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5812 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5813 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5814 return RETURN_ERR;
5815 }
developer69b61b02023-03-07 17:17:44 +08005816
developera3511852023-06-14 14:12:59 +08005817 wifi_hostapdWrite(config_file, &params, 1);
5818 wifi_datfileWrite(config_dat_file, &dat, 1);
5819 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5820 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5821 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005822}
5823
5824//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.
5825INT wifi_getRadioCarrierSenseThresholdRange(INT radioIndex, INT *output) //P3
5826{
developera3511852023-06-14 14:12:59 +08005827 if (NULL == output)
5828 return RETURN_ERR;
5829 *output=100;
developer72fb0bb2023-01-11 09:46:29 +08005830
developera3511852023-06-14 14:12:59 +08005831 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005832}
5833
5834//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.
5835INT wifi_getRadioCarrierSenseThresholdInUse(INT radioIndex, INT *output) //P3
5836{
developera3511852023-06-14 14:12:59 +08005837 if (NULL == output)
5838 return RETURN_ERR;
5839 *output = -99;
developer72fb0bb2023-01-11 09:46:29 +08005840
developera3511852023-06-14 14:12:59 +08005841 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005842}
5843
5844INT wifi_setRadioCarrierSenseThresholdInUse(INT radioIndex, INT threshold) //P3
5845{
developera3511852023-06-14 14:12:59 +08005846 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005847}
5848
5849
5850//Time interval between transmitting beacons (expressed in milliseconds). This parameter is based ondot11BeaconPeriod from [802.11-2012].
5851INT wifi_getRadioBeaconPeriod(INT radioIndex, UINT *output)
5852{
developera3511852023-06-14 14:12:59 +08005853 char interface_name[16] = {0};
5854 char cmd[MAX_BUF_SIZE]={'\0'};
5855 char buf[MAX_CMD_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08005856 int res;
developer72fb0bb2023-01-11 09:46:29 +08005857
developera3511852023-06-14 14:12:59 +08005858 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5859 if(output == NULL)
5860 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005861
developera3511852023-06-14 14:12:59 +08005862 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5863 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005864 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s status | grep beacon_int | cut -d '=' -f2 | tr -d '\n'", interface_name);
5865 if (os_snprintf_error(sizeof(cmd), res)) {
5866 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5867 return RETURN_ERR;
5868 }
5869
developera3511852023-06-14 14:12:59 +08005870 _syscmd(cmd, buf, sizeof(buf));
5871 *output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +08005872
developera3511852023-06-14 14:12:59 +08005873 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5874 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005875}
developer69b61b02023-03-07 17:17:44 +08005876
developer72fb0bb2023-01-11 09:46:29 +08005877INT wifi_setRadioBeaconPeriod(INT radioIndex, UINT BeaconPeriod)
5878{
developera3511852023-06-14 14:12:59 +08005879 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5880 struct params params={'\0'};
5881 char buf[MAX_BUF_SIZE] = {'\0'};
5882 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08005883 int res;
developer72fb0bb2023-01-11 09:46:29 +08005884
developera3511852023-06-14 14:12:59 +08005885 if (BeaconPeriod < 15 || BeaconPeriod > 65535)
5886 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005887
developera3511852023-06-14 14:12:59 +08005888 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08005889 res = snprintf(buf, sizeof(buf), "%u", BeaconPeriod);
5890 if (os_snprintf_error(sizeof(buf), res)) {
5891 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5892 return RETURN_ERR;
5893 }
5894
developera3511852023-06-14 14:12:59 +08005895 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08005896
developer75bd10c2023-06-27 11:34:08 +08005897 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5898 if (os_snprintf_error(sizeof(config_file), res)) {
5899 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5900 return RETURN_ERR;
5901 }
developera3511852023-06-14 14:12:59 +08005902 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08005903
developera3511852023-06-14 14:12:59 +08005904 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5905 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5906 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005907}
5908
5909//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.
5910INT wifi_getRadioBasicDataTransmitRates(INT radioIndex, CHAR *output)
5911{
developera3511852023-06-14 14:12:59 +08005912 //TODO: need to revisit below implementation
5913 char *temp;
5914 char temp_output[128] = {0};
5915 char temp_TransmitRates[64] = {0};
5916 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08005917 int res;
developer72fb0bb2023-01-11 09:46:29 +08005918
developera3511852023-06-14 14:12:59 +08005919 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5920 if (NULL == output)
5921 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08005922
5923 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5924 if (os_snprintf_error(sizeof(config_file), res)) {
5925 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5926 return RETURN_ERR;
5927 }
developera3511852023-06-14 14:12:59 +08005928 wifi_hostapdRead(config_file,"basic_rates",temp_TransmitRates,64);
developer69b61b02023-03-07 17:17:44 +08005929
developera3511852023-06-14 14:12:59 +08005930 if (strlen(temp_TransmitRates) == 0) { // config not set, use supported rate
5931 wifi_getRadioSupportedDataTransmitRates(radioIndex, output);
5932 } else {
5933 temp = strtok(temp_TransmitRates," ");
5934 while(temp!=NULL)
5935 {
5936 // Convert 100 kbps to Mbps
5937 temp[strlen(temp)-1]=0;
5938 if((temp[0]=='5') && (temp[1]=='\0'))
5939 {
5940 temp="5.5";
5941 }
developer32f2a182023-06-27 19:50:41 +08005942 if (strlen(temp) >= sizeof(temp_output))
5943 return RETURN_ERR;
5944 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005945 temp = strtok(NULL," ");
5946 if(temp!=NULL)
5947 {
developer32f2a182023-06-27 19:50:41 +08005948 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
5949 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005950 }
5951 }
developer32f2a182023-06-27 19:50:41 +08005952 memcpy(output, temp_output, strlen(temp_output));
5953 output[strlen(temp_output)] = '\0';
developera3511852023-06-14 14:12:59 +08005954 }
5955 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5956 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005957}
5958
5959INT wifi_setRadioBasicDataTransmitRates(INT radioIndex, CHAR *TransmitRates)
5960{
developera3511852023-06-14 14:12:59 +08005961 char *temp;
developer32f2a182023-06-27 19:50:41 +08005962 char temp1[128] = {0};
5963 char temp_output[128] = {0};
5964 char temp_TransmitRates[128] = {0};
5965 char set[128] = {0};
5966 char sub_set[128] = {0};
developera3511852023-06-14 14:12:59 +08005967 int set_count=0,subset_count=0;
5968 int set_index=0,subset_index=0;
5969 char *token;
5970 int flag=0, i=0;
5971 struct params params={'\0'};
5972 char config_file[MAX_BUF_SIZE] = {0};
5973 wifi_band band = wifi_index_to_band(radioIndex);
developer32f2a182023-06-27 19:50:41 +08005974 int res;
developer72fb0bb2023-01-11 09:46:29 +08005975
developera3511852023-06-14 14:12:59 +08005976 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5977 if(NULL == TransmitRates)
5978 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +08005979 if (strlen(TransmitRates) >= sizeof(sub_set))
5980 return RETURN_ERR;
5981
5982 memcpy(sub_set, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08005983
developera3511852023-06-14 14:12:59 +08005984 //Allow only supported Data transmit rate to be set
5985 wifi_getRadioSupportedDataTransmitRates(radioIndex,set);
5986 token = strtok(sub_set,",");
5987 while( token != NULL ) /* split the basic rate to be set, by comma */
5988 {
5989 sub_set[subset_count]=atoi(token);
5990 subset_count++;
5991 token=strtok(NULL,",");
5992 }
5993 token=strtok(set,",");
5994 while(token!=NULL) /* split the supported rate by comma */
5995 {
5996 set[set_count]=atoi(token);
5997 set_count++;
5998 token=strtok(NULL,",");
5999 }
6000 for(subset_index=0;subset_index < subset_count;subset_index++) /* Compare each element of subset and set */
6001 {
6002 for(set_index=0;set_index < set_count;set_index++)
6003 {
6004 flag=0;
6005 if(sub_set[subset_index]==set[set_index])
6006 break;
6007 else
6008 flag=1; /* No match found */
6009 }
6010 if(flag==1)
6011 return RETURN_ERR; //If value not found return Error
6012 }
developer5b23cd02023-07-19 20:26:03 +08006013
developer32f2a182023-06-27 19:50:41 +08006014 if (strlen(TransmitRates) >= sizeof(temp_TransmitRates))
6015 return RETURN_ERR;
6016
6017 memcpy(temp_TransmitRates, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08006018
developera3511852023-06-14 14:12:59 +08006019 for(i=0;i<strlen(temp_TransmitRates);i++)
6020 {
6021 //if (((temp_TransmitRates[i]>=48) && (temp_TransmitRates[i]<=57)) | (temp_TransmitRates[i]==32))
6022 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
6023 {
6024 continue;
6025 }
6026 else
6027 {
6028 return RETURN_ERR;
6029 }
6030 }
developera3511852023-06-14 14:12:59 +08006031 temp = strtok(temp_TransmitRates,",");
6032 while(temp!=NULL)
6033 {
developer32f2a182023-06-27 19:50:41 +08006034 if (strlen(temp) >= sizeof(temp1))
6035 return RETURN_ERR;
6036 strncpy(temp1, temp, strlen(temp));
developera3511852023-06-14 14:12:59 +08006037 if(band == band_5)
6038 {
6039 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
6040 {
6041 return RETURN_ERR;
6042 }
6043 }
developer72fb0bb2023-01-11 09:46:29 +08006044
developera3511852023-06-14 14:12:59 +08006045 if(strcmp(temp,"5.5")==0)
6046 {
developer32f2a182023-06-27 19:50:41 +08006047 memcpy(temp1, "55", 2);
developera3511852023-06-14 14:12:59 +08006048 }
6049 else
6050 {
developer32f2a182023-06-27 19:50:41 +08006051 if (strlen(temp1) >= (sizeof(temp1) - 1))
6052 return RETURN_ERR;
6053 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +08006054 }
developer32f2a182023-06-27 19:50:41 +08006055 if (strlen(temp1) >= (sizeof(temp_output) - strlen(temp_output)))
6056 return RETURN_ERR;
6057 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08006058 temp = strtok(NULL,",");
6059 if(temp!=NULL)
6060 {
developer32f2a182023-06-27 19:50:41 +08006061 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
6062 return RETURN_ERR;
6063 strncat(temp_output," ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08006064 }
6065 }
developer32f2a182023-06-27 19:50:41 +08006066 memcpy(TransmitRates, temp_output, strlen(temp_output));
6067 TransmitRates[strlen(temp_output)] = '\0';
developer5b23cd02023-07-19 20:26:03 +08006068
developera3511852023-06-14 14:12:59 +08006069 params.name= "basic_rates";
6070 params.value =TransmitRates;
developer72fb0bb2023-01-11 09:46:29 +08006071
developera3511852023-06-14 14:12:59 +08006072 wifi_dbg_printf("\n%s:",__func__);
6073 wifi_dbg_printf("\nparams.value=%s\n",params.value);
6074 wifi_dbg_printf("\n******************Transmit rates=%s\n",TransmitRates);
developer32f2a182023-06-27 19:50:41 +08006075 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex);
6076 if (os_snprintf_error(sizeof(config_file), res)) {
6077 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6078 return RETURN_ERR;
6079 }
developer5b23cd02023-07-19 20:26:03 +08006080
developera3511852023-06-14 14:12:59 +08006081 wifi_hostapdWrite(config_file,&params,1);
6082 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6083 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006084}
6085
developer72fb0bb2023-01-11 09:46:29 +08006086INT wifi_halGetIfStatsNull(wifi_radioTrafficStats2_t *output_struct)
6087{
developera3511852023-06-14 14:12:59 +08006088 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6089 output_struct->radio_BytesSent = 0;
6090 output_struct->radio_BytesReceived = 0;
6091 output_struct->radio_PacketsSent = 0;
6092 output_struct->radio_PacketsReceived = 0;
6093 output_struct->radio_ErrorsSent = 0;
6094 output_struct->radio_ErrorsReceived = 0;
6095 output_struct->radio_DiscardPacketsSent = 0;
6096 output_struct->radio_DiscardPacketsReceived = 0;
6097 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6098 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006099}
6100
6101
6102INT wifi_halGetIfStats(char *ifname, wifi_radioTrafficStats2_t *pStats)
6103{
developera3511852023-06-14 14:12:59 +08006104 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6105 CHAR buf[MAX_CMD_SIZE] = {0};
6106 CHAR Value[MAX_BUF_SIZE] = {0};
6107 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08006108 int res;
developer37646972023-06-29 10:58:43 +08006109 unsigned long ret;
developer72fb0bb2023-01-11 09:46:29 +08006110
developera3511852023-06-14 14:12:59 +08006111 if (ifname == NULL || strlen(ifname) <= 1)
6112 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006113
developer33f13ba2023-07-12 16:19:06 +08006114 res = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", ifname);
6115 if (res) {
6116 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08006117 return RETURN_ERR;
6118 }
6119
developera3511852023-06-14 14:12:59 +08006120 fp = fopen("/tmp/Radio_Stats.txt", "r");
6121 if(fp == NULL)
6122 {
6123 printf("/tmp/Radio_Stats.txt not exists \n");
6124 return RETURN_ERR;
6125 }
developerd14dff12023-06-28 22:47:44 +08006126 if (fclose(fp) != 0) {
6127 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6128 return RETURN_ERR;
6129 }
developer72fb0bb2023-01-11 09:46:29 +08006130
developer75bd10c2023-06-27 11:34:08 +08006131 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6132 if (os_snprintf_error(sizeof(buf), res)) {
6133 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6134 return RETURN_ERR;
6135 }
developera3511852023-06-14 14:12:59 +08006136 File_Reading(buf, Value);
developer5b23cd02023-07-19 20:26:03 +08006137
developerc14d83a2023-06-29 20:09:42 +08006138 if (hal_strtoul(Value, 10, &ret) < 0) {
developer37646972023-06-29 10:58:43 +08006139 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +08006140 }
developerc14d83a2023-06-29 20:09:42 +08006141
developer37646972023-06-29 10:58:43 +08006142 pStats->radio_PacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006143
developer86035662023-06-28 19:21:12 +08006144 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6145 if (os_snprintf_error(sizeof(buf), res)) {
6146 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6147 return RETURN_ERR;
6148 }
developera3511852023-06-14 14:12:59 +08006149 File_Reading(buf, Value);
developer5b23cd02023-07-19 20:26:03 +08006150
developerc14d83a2023-06-29 20:09:42 +08006151 if (hal_strtoul(Value, 10, &ret) < 0) {
6152 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006153 }
developer5b23cd02023-07-19 20:26:03 +08006154
developer37646972023-06-29 10:58:43 +08006155 pStats->radio_PacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006156
developer86035662023-06-28 19:21:12 +08006157 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX bytes' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6158 if (os_snprintf_error(sizeof(buf), res)) {
6159 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6160 return RETURN_ERR;
6161 }
developera3511852023-06-14 14:12:59 +08006162 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006163 if (hal_strtoul(Value, 10, &ret) < 0) {
6164 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006165 }
6166 pStats->radio_BytesReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006167
developer86035662023-06-28 19:21:12 +08006168 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX bytes' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6169 if (os_snprintf_error(sizeof(buf), res)) {
6170 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6171 return RETURN_ERR;
6172 }
developera3511852023-06-14 14:12:59 +08006173 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006174 if (hal_strtoul(Value, 10, &ret) < 0) {
6175 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006176 }
6177 pStats->radio_BytesSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006178
developer86035662023-06-28 19:21:12 +08006179 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6180 if (os_snprintf_error(sizeof(buf), res)) {
6181 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6182 return RETURN_ERR;
6183 }
developera3511852023-06-14 14:12:59 +08006184 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006185 if (hal_strtoul(Value, 10, &ret) < 0) {
6186 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006187 }
6188 pStats->radio_ErrorsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006189
developer86035662023-06-28 19:21:12 +08006190 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6191 if (os_snprintf_error(sizeof(buf), res)) {
6192 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6193 return RETURN_ERR;
6194 }
developera3511852023-06-14 14:12:59 +08006195 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006196 if (hal_strtoul(Value, 10, &ret) < 0) {
6197 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006198 }
6199 pStats->radio_ErrorsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006200
developer86035662023-06-28 19:21:12 +08006201 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6202 if (os_snprintf_error(sizeof(buf), res)) {
6203 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6204 return RETURN_ERR;
6205 }
developera3511852023-06-14 14:12:59 +08006206 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006207 if (hal_strtoul(Value, 10, &ret) < 0) {
6208 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006209 }
6210 pStats->radio_DiscardPacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006211
developer86035662023-06-28 19:21:12 +08006212 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6213 if (os_snprintf_error(sizeof(buf), res)) {
6214 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6215 return RETURN_ERR;
6216 }
developera3511852023-06-14 14:12:59 +08006217 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006218 if (hal_strtoul(Value, 10, &ret) < 0) {
6219 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006220 }
6221 pStats->radio_DiscardPacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006222
developera3511852023-06-14 14:12:59 +08006223 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6224 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006225}
6226
6227INT GetIfacestatus(CHAR *interface_name, CHAR *status)
6228{
developer7e4a2a62023-04-06 19:56:03 +08006229 CHAR buf[MAX_CMD_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006230 int res;
developer72fb0bb2023-01-11 09:46:29 +08006231
developer7e4a2a62023-04-06 19:56:03 +08006232 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6233
6234 if (interface_name != NULL && (strlen(interface_name) > 1) && status != NULL) {
developer75bd10c2023-06-27 11:34:08 +08006235 res = snprintf(buf, sizeof(buf), "%s%s%s%s%s", "ifconfig -a ",
6236 interface_name, " | grep ", interface_name, " | wc -l");
6237
6238 if (os_snprintf_error(sizeof(buf), res)) {
6239 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6240 return RETURN_ERR;
6241 }
developer7e4a2a62023-04-06 19:56:03 +08006242 File_Reading(buf, status);
6243 }
6244
6245 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6246 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006247}
6248
6249//Get detail radio traffic static info
6250INT wifi_getRadioTrafficStats2(INT radioIndex, wifi_radioTrafficStats2_t *output_struct) //Tr181
6251{
developera3511852023-06-14 14:12:59 +08006252 CHAR interface_name[64] = {0};
6253 BOOL iface_status = FALSE;
6254 wifi_radioTrafficStats2_t radioTrafficStats = {0};
developer72fb0bb2023-01-11 09:46:29 +08006255
developera3511852023-06-14 14:12:59 +08006256 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6257 if (NULL == output_struct)
6258 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006259
developera3511852023-06-14 14:12:59 +08006260 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6261 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006262
developera3511852023-06-14 14:12:59 +08006263 wifi_getApEnable(radioIndex, &iface_status);
developer72fb0bb2023-01-11 09:46:29 +08006264
developera3511852023-06-14 14:12:59 +08006265 if (iface_status == TRUE)
6266 wifi_halGetIfStats(interface_name, &radioTrafficStats);
6267 else
6268 wifi_halGetIfStatsNull(&radioTrafficStats); // just set some transmission statistic value to 0
developer72fb0bb2023-01-11 09:46:29 +08006269
developera3511852023-06-14 14:12:59 +08006270 output_struct->radio_BytesSent = radioTrafficStats.radio_BytesSent;
6271 output_struct->radio_BytesReceived = radioTrafficStats.radio_BytesReceived;
6272 output_struct->radio_PacketsSent = radioTrafficStats.radio_PacketsSent;
6273 output_struct->radio_PacketsReceived = radioTrafficStats.radio_PacketsReceived;
6274 output_struct->radio_ErrorsSent = radioTrafficStats.radio_ErrorsSent;
6275 output_struct->radio_ErrorsReceived = radioTrafficStats.radio_ErrorsReceived;
6276 output_struct->radio_DiscardPacketsSent = radioTrafficStats.radio_DiscardPacketsSent;
6277 output_struct->radio_DiscardPacketsReceived = radioTrafficStats.radio_DiscardPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +08006278
developera3511852023-06-14 14:12:59 +08006279 output_struct->radio_PLCPErrorCount = 0; //The number of packets that were received with a detected Physical Layer Convergence Protocol (PLCP) header error.
6280 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].
6281 output_struct->radio_InvalidMACCount = 0; //The number of packets that were received with a detected invalid MAC header error.
6282 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.
6283 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
6284 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
6285 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
6286 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
6287 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 +08006288
developera3511852023-06-14 14:12:59 +08006289 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
6290 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
6291 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
6292 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 +08006293
developera3511852023-06-14 14:12:59 +08006294 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
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//Set radio traffic static Measureing rules
6300INT wifi_setRadioTrafficStatsMeasure(INT radioIndex, wifi_radioTrafficStatsMeasure_t *input_struct) //Tr181
6301{
developera39cfb22023-06-20 16:28:17 +08006302 char inf_name[IF_NAME_SIZE] = {0};
6303 unsigned int if_idx = 0;
6304 int ret = -1;
6305 struct unl unl_ins;
6306 struct nl_msg *msg = NULL;
6307 struct nlattr * msg_data = NULL;
6308 struct mtk_nl80211_param param;
6309
6310 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6311 return RETURN_ERR;
6312 if_idx = if_nametoindex(inf_name);
6313 if (!if_idx) {
6314 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6315 return RETURN_ERR;
6316 }
6317 /*init mtk nl80211 vendor cmd*/
6318 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6319 param.if_type = NL80211_ATTR_IFINDEX;
6320 param.if_idx = if_idx;
6321
6322 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6323 if (ret) {
6324 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6325 return RETURN_ERR;
6326 }
6327 /*add mtk vendor cmd data*/
6328 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD,
6329 sizeof(wifi_radioTrafficStatsMeasure_t), input_struct)) {
6330 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6331 MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD);
6332 nlmsg_free(msg);
6333 goto err;
6334 }
6335
6336 /*send mtk nl80211 vendor msg*/
6337 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6338 if (ret) {
6339 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6340 goto err;
6341 }
6342 /*deinit mtk nl80211 vendor msg*/
6343 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006344 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006345err:
6346 mtk_nl80211_deint(&unl_ins);
6347 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6348 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006349}
6350
6351//To start or stop RadioTrafficStats
6352INT wifi_setRadioTrafficStatsRadioStatisticsEnable(INT radioIndex, BOOL enable)
6353{
developera39cfb22023-06-20 16:28:17 +08006354 char inf_name[IF_NAME_SIZE] = {0};
6355 unsigned int if_idx = 0;
6356 int ret = -1;
6357 struct unl unl_ins;
6358 struct nl_msg *msg = NULL;
6359 struct nlattr * msg_data = NULL;
6360 struct mtk_nl80211_param param;
6361
6362 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6363 return RETURN_ERR;
6364 if_idx = if_nametoindex(inf_name);
6365 if (!if_idx) {
6366 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6367 return RETURN_ERR;
6368 }
6369 /*init mtk nl80211 vendor cmd*/
6370 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6371 param.if_type = NL80211_ATTR_IFINDEX;
6372 param.if_idx = if_idx;
6373
6374 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6375 if (ret) {
6376 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6377 return RETURN_ERR;
6378 }
6379 /*add mtk vendor cmd data*/
6380 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL, enable)) {
6381 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6382 MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL);
6383 nlmsg_free(msg);
6384 goto err;
6385 }
6386
6387 /*send mtk nl80211 vendor msg*/
6388 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6389 if (ret) {
6390 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6391 goto err;
6392 }
6393 /*deinit mtk nl80211 vendor msg*/
6394 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006395 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006396err:
6397 mtk_nl80211_deint(&unl_ins);
6398 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6399 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006400}
6401
6402//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
6403INT wifi_getRadioStatsReceivedSignalLevel(INT radioIndex, INT signalIndex, INT *SignalLevel) //Tr181
6404{
developera3511852023-06-14 14:12:59 +08006405 if (NULL == SignalLevel)
6406 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +08006407
developer9ce44382023-06-28 11:09:37 +08006408 *SignalLevel = -19;
developer72fb0bb2023-01-11 09:46:29 +08006409
developera3511852023-06-14 14:12:59 +08006410 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006411}
6412
6413//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6414INT wifi_applyRadioSettings(INT radioIndex)
6415{
developera3511852023-06-14 14:12:59 +08006416 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006417}
6418
6419//Get the radio index assocated with this SSID entry
6420INT wifi_getSSIDRadioIndex(INT ssidIndex, INT *radioIndex)
6421{
developera3511852023-06-14 14:12:59 +08006422 if(NULL == radioIndex)
6423 return RETURN_ERR;
6424 int max_radio_num = 0;
6425 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006426 if(max_radio_num == 0){
6427 return RETURN_ERR;
6428 }
developera3511852023-06-14 14:12:59 +08006429 *radioIndex = ssidIndex%max_radio_num;
6430 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006431}
6432
6433//Device.WiFi.SSID.{i}.Enable
6434//Get SSID enable configuration parameters (not the SSID enable status)
6435INT wifi_getSSIDEnable(INT ssidIndex, BOOL *output_bool) //Tr181
6436{
developera3511852023-06-14 14:12:59 +08006437 if (NULL == output_bool)
6438 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006439
developera3511852023-06-14 14:12:59 +08006440 return wifi_getApEnable(ssidIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08006441}
6442
6443//Device.WiFi.SSID.{i}.Enable
6444//Set SSID enable configuration parameters
6445INT wifi_setSSIDEnable(INT ssidIndex, BOOL enable) //Tr181
6446{
developera3511852023-06-14 14:12:59 +08006447 return wifi_setApEnable(ssidIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08006448}
6449
6450//Device.WiFi.SSID.{i}.Status
6451//Get the SSID enable status
6452INT wifi_getSSIDStatus(INT ssidIndex, CHAR *output_string) //Tr181
6453{
developer9ce44382023-06-28 11:09:37 +08006454 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +08006455 int res;
developer72fb0bb2023-01-11 09:46:29 +08006456
developera3511852023-06-14 14:12:59 +08006457 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6458 if (NULL == output_string)
6459 return RETURN_ERR;
developer69b61b02023-03-07 17:17:44 +08006460
developera3511852023-06-14 14:12:59 +08006461 wifi_getApEnable(ssidIndex,&output_bool);
developere40952c2023-06-15 18:46:43 +08006462 res = snprintf(output_string, 32, output_bool==1?"Enabled":"Disabled");
6463 if (os_snprintf_error(32, res)) {
6464 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6465 return RETURN_ERR;
6466 }
developer72fb0bb2023-01-11 09:46:29 +08006467
developera3511852023-06-14 14:12:59 +08006468 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6469 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006470}
6471
6472// Outputs a 32 byte or less string indicating the SSID name. Sring buffer must be preallocated by the caller.
6473INT wifi_getSSIDName(INT apIndex, CHAR *output)
6474{
developera3511852023-06-14 14:12:59 +08006475 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006476 int res;
developer72fb0bb2023-01-11 09:46:29 +08006477
developera3511852023-06-14 14:12:59 +08006478 if (NULL == output)
6479 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006480
developer75bd10c2023-06-27 11:34:08 +08006481 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6482 if (os_snprintf_error(sizeof(config_file), res)) {
6483 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6484 return RETURN_ERR;
6485 }
developera3511852023-06-14 14:12:59 +08006486 wifi_hostapdRead(config_file,"ssid",output,32);
developer72fb0bb2023-01-11 09:46:29 +08006487
developera3511852023-06-14 14:12:59 +08006488 wifi_dbg_printf("\n[%s]: SSID Name is : %s",__func__,output);
6489 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006490}
6491
developer69b61b02023-03-07 17:17:44 +08006492// Set a max 32 byte string and sets an internal variable to the SSID name
developer72fb0bb2023-01-11 09:46:29 +08006493INT wifi_setSSIDName(INT apIndex, CHAR *ssid_string)
6494{
developera3511852023-06-14 14:12:59 +08006495 struct params params;
6496 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006497 int res;
developer72fb0bb2023-01-11 09:46:29 +08006498
developera3511852023-06-14 14:12:59 +08006499 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6500 if(NULL == ssid_string || strlen(ssid_string) >= 32 || strlen(ssid_string) == 0 )
6501 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006502
developera3511852023-06-14 14:12:59 +08006503 params.name = "ssid";
6504 params.value = ssid_string;
developer75bd10c2023-06-27 11:34:08 +08006505
6506 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6507 if (os_snprintf_error(sizeof(config_file), res)) {
6508 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6509 return RETURN_ERR;
6510 }
6511
developera3511852023-06-14 14:12:59 +08006512 wifi_hostapdWrite(config_file, &params, 1);
6513 wifi_hostapdProcessUpdate(apIndex, &params, 1);
6514 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006515
developera3511852023-06-14 14:12:59 +08006516 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006517}
6518
6519//Get the BSSID
6520INT wifi_getBaseBSSID(INT ssidIndex, CHAR *output_string) //RDKB
6521{
developer7e4a2a62023-04-06 19:56:03 +08006522 char cmd[MAX_CMD_SIZE] = {0};
6523 char inf_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08006524 int res;
developer72fb0bb2023-01-11 09:46:29 +08006525
developera3511852023-06-14 14:12:59 +08006526 if (!output_string)
developerdaf24792023-06-06 11:40:04 +08006527 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006528
developer47cc27a2023-05-17 23:09:58 +08006529 if (wifi_GetInterfaceName(ssidIndex, inf_name) != RETURN_OK)
6530 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08006531
developer5b2f10c2023-05-25 17:02:21 +08006532 if (ssidIndex < 0 || ssidIndex > MAX_APS) {
6533 wifi_debug(DEBUG_ERROR, "innvalide ssidIdex(%d)\n", ssidIndex);
6534 strncpy(output_string, "\0", 1);
6535 return RETURN_ERR;
6536 }
developere40952c2023-06-15 18:46:43 +08006537 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep bssid | cut -d '=' -f2 | tr -d '\\n'", inf_name);
6538 if (os_snprintf_error(sizeof(cmd), res)) {
6539 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6540 return RETURN_ERR;
6541 }
6542
developer5b2f10c2023-05-25 17:02:21 +08006543 _syscmd(cmd, output_string, 64);
developer7e4a2a62023-04-06 19:56:03 +08006544
developer5b2f10c2023-05-25 17:02:21 +08006545 /* if hostapd does not control interface even if this interface has been brought up,
6546 * try to get its mac address by iw command.
6547 */
6548 if(strlen(output_string) == 0) {
6549 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08006550 res = snprintf(cmd, sizeof(cmd), "iw dev %s info | grep \"addr\" | awk \'{print $2}\'", inf_name);
6551 if (os_snprintf_error(sizeof(cmd), res)) {
6552 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6553 return RETURN_ERR;
6554 }
6555
developer5b2f10c2023-05-25 17:02:21 +08006556 _syscmd(cmd, output_string, 64);
6557 }
developer72fb0bb2023-01-11 09:46:29 +08006558
developer5b2f10c2023-05-25 17:02:21 +08006559 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006560}
6561
6562//Get the MAC address associated with this Wifi SSID
6563INT wifi_getSSIDMACAddress(INT ssidIndex, CHAR *output_string) //Tr181
6564{
developera3511852023-06-14 14:12:59 +08006565 wifi_getBaseBSSID(ssidIndex,output_string);
6566 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006567}
6568
6569//Get the basic SSID traffic static info
6570//Apply SSID and AP (in the case of Acess Point devices) to the hardware
6571//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6572INT wifi_applySSIDSettings(INT ssidIndex)
6573{
developera3511852023-06-14 14:12:59 +08006574 char interface_name[16] = {0};
6575 BOOL status = false;
6576 char cmd[MAX_CMD_SIZE] = {0};
6577 char buf[MAX_CMD_SIZE] = {0};
6578 int apIndex, ret;
6579 int max_radio_num = 0;
6580 int radioIndex = 0;
developere40952c2023-06-15 18:46:43 +08006581 int res;
developer72fb0bb2023-01-11 09:46:29 +08006582
developera3511852023-06-14 14:12:59 +08006583 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006584 if(max_radio_num == 0){
6585 return RETURN_ERR;
6586 }
developera3511852023-06-14 14:12:59 +08006587 radioIndex = ssidIndex % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +08006588
developera3511852023-06-14 14:12:59 +08006589 wifi_getApEnable(ssidIndex,&status);
6590 // Do not apply when ssid index is disabled
6591 if (status == false)
6592 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006593
developera3511852023-06-14 14:12:59 +08006594 /* Doing full remove and add for ssid Index
6595 * Not all hostapd options are supported with reload
6596 * for example macaddr_acl
6597 */
6598 if(wifi_setApEnable(ssidIndex,false) != RETURN_OK)
6599 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006600
developera3511852023-06-14 14:12:59 +08006601 ret = wifi_setApEnable(ssidIndex,true);
developer72fb0bb2023-01-11 09:46:29 +08006602
developera3511852023-06-14 14:12:59 +08006603 /* Workaround for hostapd issue with multiple bss definitions
6604 * when first created interface will be removed
6605 * then all vaps other vaps on same phy are removed
6606 * after calling setApEnable to false readd all enabled vaps */
6607 for(int i=0; i < MAX_APS/max_radio_num; i++) {
6608 apIndex = max_radio_num*i+radioIndex;
6609 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
6610 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006611 res = snprintf(cmd, sizeof(cmd), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
6612 if (os_snprintf_error(sizeof(cmd), res)) {
6613 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6614 return RETURN_ERR;
6615 }
6616
developera3511852023-06-14 14:12:59 +08006617 _syscmd(cmd, buf, sizeof(buf));
6618 if(*buf == '1')
6619 wifi_setApEnable(apIndex, true);
6620 }
developer72fb0bb2023-01-11 09:46:29 +08006621
developera3511852023-06-14 14:12:59 +08006622 return ret;
developer72fb0bb2023-01-11 09:46:29 +08006623}
6624
6625struct channels_noise {
developera3511852023-06-14 14:12:59 +08006626 int channel;
6627 int noise;
developer72fb0bb2023-01-11 09:46:29 +08006628};
6629
6630// Return noise array for each channel
6631int get_noise(int radioIndex, struct channels_noise *channels_noise_arr, int channels_num)
6632{
developera3511852023-06-14 14:12:59 +08006633 char interface_name[16] = {0};
6634 FILE *f = NULL;
6635 char cmd[128] = {0};
6636 char line[256] = {0};
developer75bd10c2023-06-27 11:34:08 +08006637 int tmp = 0, arr_index = -1, res;
developer72fb0bb2023-01-11 09:46:29 +08006638
developera3511852023-06-14 14:12:59 +08006639 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6640 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08006641
6642 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep 'frequency\\|noise' | awk '{print $2}'", interface_name);
6643 if (os_snprintf_error(sizeof(cmd), res)) {
6644 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6645 return RETURN_ERR;
6646 }
developer72fb0bb2023-01-11 09:46:29 +08006647
developera3511852023-06-14 14:12:59 +08006648 if ((f = popen(cmd, "r")) == NULL) {
6649 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
6650 return RETURN_ERR;
6651 }
developer69b61b02023-03-07 17:17:44 +08006652
developera3511852023-06-14 14:12:59 +08006653 while(fgets(line, sizeof(line), f) != NULL) {
6654 if(arr_index < channels_num){
developer37646972023-06-29 10:58:43 +08006655 if (sscanf(line, "%d", &tmp) == EOF)
6656 continue;
developera3511852023-06-14 14:12:59 +08006657 if (tmp > 0) { // channel frequency, the first line must be frequency
6658 arr_index++;
6659 channels_noise_arr[arr_index].channel = ieee80211_frequency_to_channel(tmp);
6660 } else { // noise
6661 channels_noise_arr[arr_index].noise = tmp;
6662 }
6663 }else{
6664 break;
6665 }
6666 }
6667 pclose(f);
6668 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006669}
6670
6671//Start the wifi scan and get the result into output buffer for RDKB to parser. The result will be used to manage endpoint list
6672//HAL funciton should allocate an data structure array, and return to caller with "neighbor_ap_array"
developer69b61b02023-03-07 17:17:44 +08006673INT wifi_getNeighboringWiFiDiagnosticResult2(INT radioIndex, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size) //Tr181
developer72fb0bb2023-01-11 09:46:29 +08006674{
developera3511852023-06-14 14:12:59 +08006675 int index = -1;
6676 wifi_neighbor_ap2_t *scan_array = NULL;
6677 char cmd[256]={0};
6678 char buf[128]={0};
6679 char file_name[32] = {0};
6680 char filter_SSID[32] = {0};
6681 char line[256] = {0};
6682 char interface_name[16] = {0};
6683 char *ret = NULL;
6684 int freq=0;
6685 FILE *f = NULL;
developerc14d83a2023-06-29 20:09:42 +08006686 unsigned long channels_num = 0;
developera3511852023-06-14 14:12:59 +08006687 int vht_channel_width = 0;
6688 int get_noise_ret = RETURN_ERR;
6689 bool filter_enable = false;
6690 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
developere40952c2023-06-15 18:46:43 +08006691 int phyId = 0, res;
developerc14d83a2023-06-29 20:09:42 +08006692 unsigned long len, tmp;
developer72fb0bb2023-01-11 09:46:29 +08006693
developera3511852023-06-14 14:12:59 +08006694 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006695
developera3511852023-06-14 14:12:59 +08006696 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6697 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006698
6699 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radioIndex);
6700 if (os_snprintf_error(sizeof(file_name), res)) {
6701 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6702 return RETURN_ERR;
6703 }
developer72fb0bb2023-01-11 09:46:29 +08006704
developera3511852023-06-14 14:12:59 +08006705 f = fopen(file_name, "r");
6706 if (f != NULL) {
developerd14dff12023-06-28 22:47:44 +08006707 if (fgets(buf, sizeof(file_name), f) == NULL)
6708 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006709 if ((strncmp(buf, "0", 1)) != 0) {
developerd14dff12023-06-28 22:47:44 +08006710 if (fgets(filter_SSID, sizeof(file_name), f) == NULL)
6711 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006712 if (strlen(filter_SSID) != 0)
6713 filter_enable = true;
6714 }
developerd14dff12023-06-28 22:47:44 +08006715 if (fclose(f) != 0) {
6716 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6717 return RETURN_ERR;
6718 }
developera3511852023-06-14 14:12:59 +08006719 }
developer72fb0bb2023-01-11 09:46:29 +08006720
developera3511852023-06-14 14:12:59 +08006721 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +08006722 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
6723 if (os_snprintf_error(sizeof(cmd), res)) {
6724 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6725 return RETURN_ERR;
6726 }
6727
developera3511852023-06-14 14:12:59 +08006728 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08006729 if (hal_strtoul(buf, 10, &tmp) < 0) {
6730 wifi_debug(DEBUG_ERROR, "strtol fail\n");
6731 }
developer72fb0bb2023-01-11 09:46:29 +08006732
developerc14d83a2023-06-29 20:09:42 +08006733 channels_num = tmp;
developer32f2a182023-06-27 19:50:41 +08006734 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 +08006735 // 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 +08006736 if (os_snprintf_error(sizeof(cmd), res)) {
6737 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6738 return RETURN_ERR;
6739 }
developer86035662023-06-28 19:21:12 +08006740 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developera3511852023-06-14 14:12:59 +08006741 if ((f = popen(cmd, "r")) == NULL) {
developerb14b3462023-07-01 18:02:42 +08006742 wifi_debug(DEBUG_ERROR, "popen error\n");
developera3511852023-06-14 14:12:59 +08006743 return RETURN_ERR;
6744 }
developer9ce44382023-06-28 11:09:37 +08006745 struct channels_noise *channels_noise_arr = NULL;
6746 if(channels_num > 0 && channels_num <= 243){
6747 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
6748 } else{
developerb14b3462023-07-01 18:02:42 +08006749 wifi_debug(DEBUG_ERROR, "channel num = %ld!!\n", channels_num);
developer9ce44382023-06-28 11:09:37 +08006750 }
developer5b23cd02023-07-19 20:26:03 +08006751
developer9ce44382023-06-28 11:09:37 +08006752 if(channels_noise_arr != NULL){
6753 get_noise_ret = get_noise(radioIndex, channels_noise_arr, channels_num);
6754 } else{
developerb14b3462023-07-01 18:02:42 +08006755 wifi_debug(DEBUG_ERROR, "channels_noise_arr is NULL!!\n");
developer9ce44382023-06-28 11:09:37 +08006756 }
developer5b23cd02023-07-19 20:26:03 +08006757
developer69b61b02023-03-07 17:17:44 +08006758
developera3511852023-06-14 14:12:59 +08006759 ret = fgets(line, sizeof(line), f);
6760 while (ret != NULL) {
6761 if(strstr(line, "BSS") != NULL) { // new neighbor info
6762 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
6763 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
6764 // 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 +08006765
developera3511852023-06-14 14:12:59 +08006766 if (!filter_BSS) {
6767 index++;
6768 wifi_neighbor_ap2_t *tmp;
6769 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
6770 if (tmp == NULL) { // no more memory to use
6771 index--;
6772 wifi_dbg_printf("%s: realloc failed\n", __func__);
6773 break;
6774 }
6775 scan_array = tmp;
6776 }
6777 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +08006778
developera3511852023-06-14 14:12:59 +08006779 filter_BSS = false;
developer86035662023-06-28 19:21:12 +08006780 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
6781 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6782 goto err;
6783 }
developerc79e9172023-06-06 19:48:03 +08006784 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +08006785 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +08006786 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +08006787 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +08006788 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +08006789 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
6790 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +08006791 if (sscanf(line," freq: %d", &freq) != 1) {
6792 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006793 }
developera3511852023-06-14 14:12:59 +08006794 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +08006795
developera3511852023-06-14 14:12:59 +08006796 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +08006797 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006798 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +08006799 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006800 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +08006801 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006802 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
6803 }
6804 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +08006805 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006806 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +08006807 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006808 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +08006809 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006810 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
6811 }
developer72fb0bb2023-01-11 09:46:29 +08006812
developera3511852023-06-14 14:12:59 +08006813 scan_array[index].ap_Noise = 0;
6814 if (get_noise_ret == RETURN_OK) {
6815 for (int i = 0; i < channels_num; i++) {
6816 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
6817 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
6818 break;
6819 }
6820 }
6821 }
6822 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +08006823 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
6824 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6825 goto err;
6826 }
developera3511852023-06-14 14:12:59 +08006827 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +08006828 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
6829 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6830 goto err;
6831 }
developera3511852023-06-14 14:12:59 +08006832 } else if (strstr(line,"SSID") != NULL) {
developer86035662023-06-28 19:21:12 +08006833 if (sscanf(line," SSID: %32s", scan_array[index].ap_SSID) != 1) {
6834 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +08006835 //goto err;
developer86035662023-06-28 19:21:12 +08006836 }
developera3511852023-06-14 14:12:59 +08006837 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) != 0) {
6838 filter_BSS = true;
6839 }
6840 } else if (strstr(line, "Supported rates") != NULL) {
6841 char SRate[80] = {0}, *tmp = NULL;
6842 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +08006843 if (strlen(line) >= sizeof(SRate))
developer86035662023-06-28 19:21:12 +08006844 goto err;
developer32f2a182023-06-27 19:50:41 +08006845 strncpy(SRate, line, strlen(line));
developera3511852023-06-14 14:12:59 +08006846 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +08006847 if (tmp == NULL)
6848 goto err;
developera3511852023-06-14 14:12:59 +08006849 tmp = strtok(NULL, ":");
developer86035662023-06-28 19:21:12 +08006850 if (tmp == NULL)
6851 goto err;
developer32f2a182023-06-27 19:50:41 +08006852 if (strlen(tmp) >= sizeof(buf))
developer86035662023-06-28 19:21:12 +08006853 goto err;
developer32f2a182023-06-27 19:50:41 +08006854 strncpy(buf, tmp, strlen(tmp));
developera3511852023-06-14 14:12:59 +08006855 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +08006856
developera3511852023-06-14 14:12:59 +08006857 tmp = strtok(buf, " \n");
6858 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +08006859 if (strlen(tmp) >= (sizeof(SRate) - strlen(SRate)))
developer86035662023-06-28 19:21:12 +08006860 goto err;
developer32f2a182023-06-27 19:50:41 +08006861 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +08006862 if (SRate[strlen(SRate) - 1] == '*') {
6863 SRate[strlen(SRate) - 1] = '\0';
6864 }
developer32f2a182023-06-27 19:50:41 +08006865 if (strlen(SRate) >= (sizeof(SRate) - 1))
developer86035662023-06-28 19:21:12 +08006866 goto err;
developer32f2a182023-06-27 19:50:41 +08006867 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +08006868
developera3511852023-06-14 14:12:59 +08006869 tmp = strtok(NULL, " \n");
6870 }
6871 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +08006872 if (sizeof(scan_array[index].ap_SupportedDataTransferRates) <= strlen(SRate))
developer86035662023-06-28 19:21:12 +08006873 goto err;
developer32f2a182023-06-27 19:50:41 +08006874 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, strlen(SRate));
developera3511852023-06-14 14:12:59 +08006875 } else if (strstr(line, "DTIM") != NULL) {
developer86035662023-06-28 19:21:12 +08006876 if (sscanf(line,"DTIM Period %u", &(scan_array[index].ap_DTIMPeriod)) != 1) {
6877 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006878 }
developera3511852023-06-14 14:12:59 +08006879 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006880 if (sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) <= 4)
developer86035662023-06-28 19:21:12 +08006881 goto err;
developer32f2a182023-06-27 19:50:41 +08006882 strncat(scan_array[index].ap_SupportedStandards, ",ac",
6883 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6884 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
6885 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +08006886 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006887 strncat(scan_array[index].ap_SupportedStandards, ",n", sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6888 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
6889 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +08006890 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006891 if (fgets(line, sizeof(line), f) == NULL) {
6892 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6893 goto err;
6894 }
6895 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
6896 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6897 goto err;
6898 }
developera3511852023-06-14 14:12:59 +08006899 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +08006900 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +08006901 } else {
developere40952c2023-06-15 18:46:43 +08006902 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +08006903 }
developere40952c2023-06-15 18:46:43 +08006904 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6905 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +08006906 goto err;
developere40952c2023-06-15 18:46:43 +08006907 }
6908
developera3511852023-06-14 14:12:59 +08006909 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6910 continue;
6911 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006912 if (fgets(line, sizeof(line), f) == NULL) {
6913 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6914 goto err;
6915 }
developerc14d83a2023-06-29 20:09:42 +08006916 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
developer86035662023-06-28 19:21:12 +08006917 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6918 goto err;
6919 }
developera3511852023-06-14 14:12:59 +08006920 if (!strcmp(buf, "above")) {
6921 //40Mhz +
developere40952c2023-06-15 18:46:43 +08006922 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 +08006923 }
6924 else if (!strcmp(buf, "below")) {
6925 //40Mhz -
developere40952c2023-06-15 18:46:43 +08006926 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 +08006927 } else {
6928 //20Mhz
developere40952c2023-06-15 18:46:43 +08006929 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT20", radioIndex%1 ? "A": "G");
6930 }
6931 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6932 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +08006933 goto err;
developera3511852023-06-14 14:12:59 +08006934 }
developere40952c2023-06-15 18:46:43 +08006935
developera3511852023-06-14 14:12:59 +08006936 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6937 continue;
6938 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006939 strncat(scan_array[index].ap_SupportedStandards, ",ax",
6940 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6941 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
6942 scan_array[index].ap_OperatingStandards[2] = '\0';
developer86035662023-06-28 19:21:12 +08006943 if (fgets(line, sizeof(line), f) == NULL) {
6944 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6945 goto err;
6946 }
developera3511852023-06-14 14:12:59 +08006947 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006948 if (strstr(line, "HE40/2.4GHz") != NULL) {
6949 len = strlen("11AXHE40PLUS");
6950 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
6951 } else {
6952 len = strlen("11AXHE20");
6953 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
6954 }
6955 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +08006956 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
6957 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006958 len = strlen("11AXHE80");
6959 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
6960 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developer86035662023-06-28 19:21:12 +08006961 if (fgets(line, sizeof(line), f) == NULL) {
6962 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6963 goto err;
6964 }
developera3511852023-06-14 14:12:59 +08006965 } else
6966 continue;
developer32f2a182023-06-27 19:50:41 +08006967 if (strstr(line, "HE160/5GHz") != NULL) {
6968 len = strlen("11AXHE160");
6969 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
6970 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
6971 }
developera3511852023-06-14 14:12:59 +08006972 }
6973 continue;
6974 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006975 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
6976 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006977 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006978 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
6979 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006980 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +08006981 if (sscanf(line, " * Group cipher: %64s", scan_array[index].ap_EncryptionMode) != 1) {
6982 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6983 goto err;
6984 }
developera3511852023-06-14 14:12:59 +08006985 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006986 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
6987 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +08006988 }
6989 }
developerb14b3462023-07-01 18:02:42 +08006990 ret = fgets(line, sizeof(line), f);
developera3511852023-06-14 14:12:59 +08006991 }
developer72fb0bb2023-01-11 09:46:29 +08006992
developera3511852023-06-14 14:12:59 +08006993 if (!filter_BSS) {
6994 *output_array_size = index + 1;
6995 } else {
6996 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
6997 *output_array_size = index;
6998 }
6999 *neighbor_ap_array = scan_array;
7000 pclose(f);
7001 free(channels_noise_arr);
7002 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7003 return RETURN_OK;
developer86035662023-06-28 19:21:12 +08007004err:
7005 pclose(f);
7006 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +08007007 if (scan_array)
7008 free(scan_array);
developer86035662023-06-28 19:21:12 +08007009 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7010 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007011}
7012
7013//>> Deprecated: used for old RDKB code.
7014INT wifi_getRadioWifiTrafficStats(INT radioIndex, wifi_radioTrafficStats_t *output_struct)
7015{
developera3511852023-06-14 14:12:59 +08007016 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007017
developera3511852023-06-14 14:12:59 +08007018 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7019 output_struct->wifi_PLCPErrorCount = 0;
7020 output_struct->wifi_FCSErrorCount = 0;
7021 output_struct->wifi_InvalidMACCount = 0;
7022 output_struct->wifi_PacketsOtherReceived = 0;
7023 output_struct->wifi_Noise = 0;
7024 status = RETURN_OK;
7025 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7026 return status;
developer72fb0bb2023-01-11 09:46:29 +08007027}
7028
7029INT wifi_getBasicTrafficStats(INT apIndex, wifi_basicTrafficStats_t *output_struct)
7030{
developera3511852023-06-14 14:12:59 +08007031 char interface_name[16] = {0};
7032 char cmd[128] = {0};
7033 char buf[1280] = {0};
7034 char *pos = NULL;
developere40952c2023-06-15 18:46:43 +08007035 int res;
developer72fb0bb2023-01-11 09:46:29 +08007036
developera3511852023-06-14 14:12:59 +08007037 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7038 if (NULL == output_struct)
7039 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007040
developera3511852023-06-14 14:12:59 +08007041 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
7042 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007043
developera3511852023-06-14 14:12:59 +08007044 memset(output_struct, 0, sizeof(wifi_basicTrafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08007045
developere40952c2023-06-15 18:46:43 +08007046 res = snprintf(cmd, sizeof(cmd), "ifconfig %s", interface_name);
7047 if (os_snprintf_error(sizeof(cmd), res)) {
7048 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7049 return RETURN_ERR;
7050 }
7051
developera3511852023-06-14 14:12:59 +08007052 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007053
developera3511852023-06-14 14:12:59 +08007054 pos = buf;
7055 if ((pos = strstr(pos, "RX packets:")) == NULL)
7056 return RETURN_ERR;
7057 output_struct->wifi_PacketsReceived = atoi(pos+strlen("RX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08007058
developera3511852023-06-14 14:12:59 +08007059 if ((pos = strstr(pos, "TX packets:")) == NULL)
7060 return RETURN_ERR;
7061 output_struct->wifi_PacketsSent = atoi(pos+strlen("TX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08007062
developera3511852023-06-14 14:12:59 +08007063 if ((pos = strstr(pos, "RX bytes:")) == NULL)
7064 return RETURN_ERR;
7065 output_struct->wifi_BytesReceived = atoi(pos+strlen("RX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08007066
developera3511852023-06-14 14:12:59 +08007067 if ((pos = strstr(pos, "TX bytes:")) == NULL)
7068 return RETURN_ERR;
7069 output_struct->wifi_BytesSent = atoi(pos+strlen("TX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08007070
developer37646972023-06-29 10:58:43 +08007071 res = snprintf(cmd, sizeof(cmd),
7072 "hostapd_cli -i %s list_sta | wc -l | tr -d '\n'", interface_name);
7073 if (os_snprintf_error(sizeof(cmd), res)) {
7074 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7075 return RETURN_ERR;
7076 }
developera3511852023-06-14 14:12:59 +08007077 _syscmd(cmd, buf, sizeof(buf));
developer37646972023-06-29 10:58:43 +08007078 if (sscanf(buf, "%lu", &output_struct->wifi_Associations) == EOF)
7079 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer72fb0bb2023-01-11 09:46:29 +08007080
developera3511852023-06-14 14:12:59 +08007081 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7082 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007083}
7084
7085INT wifi_getWifiTrafficStats(INT apIndex, wifi_trafficStats_t *output_struct)
7086{
developera3511852023-06-14 14:12:59 +08007087 char interface_name[IF_NAME_SIZE] = {0};
7088 char interface_status[MAX_BUF_SIZE] = {0};
7089 char Value[MAX_BUF_SIZE] = {0};
7090 char buf[MAX_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08007091 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08007092 int res;
developerc14d83a2023-06-29 20:09:42 +08007093 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +08007094
developera3511852023-06-14 14:12:59 +08007095 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7096 if (NULL == output_struct)
7097 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007098
developera3511852023-06-14 14:12:59 +08007099 memset(output_struct, 0, sizeof(wifi_trafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08007100
developera3511852023-06-14 14:12:59 +08007101 if (wifi_GetInterfaceName(apIndex,interface_name) != RETURN_OK)
7102 return RETURN_ERR;
7103 GetIfacestatus(interface_name, interface_status);
developer72fb0bb2023-01-11 09:46:29 +08007104
developera3511852023-06-14 14:12:59 +08007105 if(0 != strcmp(interface_status, "1"))
7106 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08007107
developer33f13ba2023-07-12 16:19:06 +08007108 res = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", interface_name);
7109 if (res) {
7110 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08007111 return RETURN_ERR;
7112 }
developer72fb0bb2023-01-11 09:46:29 +08007113
developera3511852023-06-14 14:12:59 +08007114 fp = fopen("/tmp/SSID_Stats.txt", "r");
7115 if(fp == NULL)
7116 {
7117 printf("/tmp/SSID_Stats.txt not exists \n");
7118 return RETURN_ERR;
7119 }
developer37646972023-06-29 10:58:43 +08007120 if (fclose(fp) == EOF) {
7121 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
7122 return RETURN_ERR;
7123 }
developer72fb0bb2023-01-11 09:46:29 +08007124
developer37646972023-06-29 10:58:43 +08007125 res = snprintf(buf, sizeof(buf),
7126 "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7127 if (os_snprintf_error(sizeof(buf), res)) {
7128 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7129 return RETURN_ERR;
7130 }
developera3511852023-06-14 14:12:59 +08007131 File_Reading(buf, Value);
developer5b23cd02023-07-19 20:26:03 +08007132
developerc14d83a2023-06-29 20:09:42 +08007133 if (hal_strtoul(Value, 10, &tmp) < 0) {
7134 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007135 }
developerc14d83a2023-06-29 20:09:42 +08007136 output_struct->wifi_ErrorsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007137
developer37646972023-06-29 10:58:43 +08007138 res = snprintf(buf, sizeof(buf),
7139 "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7140 if (os_snprintf_error(sizeof(buf), res)) {
7141 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7142 return RETURN_ERR;
7143 }
developera3511852023-06-14 14:12:59 +08007144 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007145
7146 if (hal_strtoul(Value, 10, &tmp) < 0) {
7147 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007148 }
developerc14d83a2023-06-29 20:09:42 +08007149 output_struct->wifi_ErrorsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007150
developer37646972023-06-29 10:58:43 +08007151 res = snprintf(buf, sizeof(buf),
7152 "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7153 if (os_snprintf_error(sizeof(buf), res)) {
7154 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7155 return RETURN_ERR;
7156 }
developera3511852023-06-14 14:12:59 +08007157 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007158
7159 if (hal_strtoul(Value, 10, &tmp) < 0) {
7160 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007161 }
developerc14d83a2023-06-29 20:09:42 +08007162 output_struct->wifi_DiscardedPacketsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007163
developer37646972023-06-29 10:58:43 +08007164 res = snprintf(buf, sizeof(buf),
7165 "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7166 if (os_snprintf_error(sizeof(buf), res)) {
7167 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7168 return RETURN_ERR;
7169 }
developera3511852023-06-14 14:12:59 +08007170 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007171
7172 if (hal_strtoul(Value, 10, &tmp) < 0) {
7173 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007174 }
developerc14d83a2023-06-29 20:09:42 +08007175 output_struct->wifi_DiscardedPacketsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007176
developera3511852023-06-14 14:12:59 +08007177 output_struct->wifi_UnicastPacketsSent = 0;
7178 output_struct->wifi_UnicastPacketsReceived = 0;
7179 output_struct->wifi_MulticastPacketsSent = 0;
7180 output_struct->wifi_MulticastPacketsReceived = 0;
7181 output_struct->wifi_BroadcastPacketsSent = 0;
7182 output_struct->wifi_BroadcastPacketsRecevied = 0;
7183 output_struct->wifi_UnknownPacketsReceived = 0;
developer72fb0bb2023-01-11 09:46:29 +08007184
developera3511852023-06-14 14:12:59 +08007185 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7186 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007187}
7188
7189INT wifi_getSSIDTrafficStats(INT apIndex, wifi_ssidTrafficStats_t *output_struct)
7190{
developera3511852023-06-14 14:12:59 +08007191 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007192
developera3511852023-06-14 14:12:59 +08007193 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7194 //Below values should get updated from hal
7195 output_struct->wifi_RetransCount=0;
7196 output_struct->wifi_FailedRetransCount=0;
7197 output_struct->wifi_RetryCount=0;
7198 output_struct->wifi_MultipleRetryCount=0;
7199 output_struct->wifi_ACKFailureCount=0;
7200 output_struct->wifi_AggregatedPacketCount=0;
developer72fb0bb2023-01-11 09:46:29 +08007201
developera3511852023-06-14 14:12:59 +08007202 status = RETURN_OK;
7203 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007204
developera3511852023-06-14 14:12:59 +08007205 return status;
developer72fb0bb2023-01-11 09:46:29 +08007206}
7207
7208INT wifi_getNeighboringWiFiDiagnosticResult(wifi_neighbor_ap_t **neighbor_ap_array, UINT *output_array_size)
7209{
developera3511852023-06-14 14:12:59 +08007210 INT status = RETURN_ERR;
7211 UINT index;
7212 wifi_neighbor_ap_t *pt=NULL;
developer72fb0bb2023-01-11 09:46:29 +08007213
developera3511852023-06-14 14:12:59 +08007214 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7215 *output_array_size=2;
7216 //zqiu: HAL alloc the array and return to caller. Caller response to free it.
7217 *neighbor_ap_array=(wifi_neighbor_ap_t *)calloc(sizeof(wifi_neighbor_ap_t), *output_array_size);
developer86035662023-06-28 19:21:12 +08007218 if (*neighbor_ap_array == NULL) {
7219 wifi_debug(DEBUG_ERROR, "calloc fail!\n");
7220 return RETURN_ERR;
7221 }
developera3511852023-06-14 14:12:59 +08007222 for (index = 0, pt=*neighbor_ap_array; index < *output_array_size; index++, pt++) {
developer32f2a182023-06-27 19:50:41 +08007223 pt->ap_Radio[0] = '\0';
7224 pt->ap_SSID[0] = '\0';
7225 pt->ap_BSSID[0] = '\0';
7226 pt->ap_Mode[0] = '\0';
developera3511852023-06-14 14:12:59 +08007227 pt->ap_Channel=1;
7228 pt->ap_SignalStrength=0;
developer32f2a182023-06-27 19:50:41 +08007229 pt->ap_SecurityModeEnabled[0] = '\0';
7230 pt->ap_EncryptionMode[0] = '\0';
7231 pt->ap_OperatingFrequencyBand[0] = '\0';
7232 pt->ap_SupportedStandards[0] = '\0';
7233 pt->ap_OperatingStandards[0] = '\0';
7234 pt->ap_OperatingChannelBandwidth[0] = '\0';
7235 pt->ap_BasicDataTransferRates[0] = '\0';
7236 pt->ap_SupportedDataTransferRates[0] = '\0';
developera3511852023-06-14 14:12:59 +08007237 pt->ap_BeaconPeriod=1;
7238 pt->ap_Noise=0;
developera3511852023-06-14 14:12:59 +08007239 pt->ap_DTIMPeriod=1;
7240 pt->ap_ChannelUtilization = 1;
7241 }
developer72fb0bb2023-01-11 09:46:29 +08007242
developera3511852023-06-14 14:12:59 +08007243 status = RETURN_OK;
7244 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007245
developera3511852023-06-14 14:12:59 +08007246 return status;
developer72fb0bb2023-01-11 09:46:29 +08007247}
7248
7249//----------------- AP HAL -------------------------------
7250
7251//>> Deprecated: used for old RDKB code.
7252INT wifi_getAllAssociatedDeviceDetail(INT apIndex, ULONG *output_ulong, wifi_device_t **output_struct)
7253{
developera3511852023-06-14 14:12:59 +08007254 if (NULL == output_ulong || NULL == output_struct)
7255 return RETURN_ERR;
7256 *output_ulong = 0;
7257 *output_struct = NULL;
7258 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007259}
7260
7261#ifdef HAL_NETLINK_IMPL
7262static int AssoDevInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +08007263 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7264 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +08007265 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
7266 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +08007267 char mac_addr[20];
7268 static int count=0;
7269 int rate=0;
developer72fb0bb2023-01-11 09:46:29 +08007270
developera3511852023-06-14 14:12:59 +08007271 wifi_device_info_t *out = (wifi_device_info_t*)arg;
developer72fb0bb2023-01-11 09:46:29 +08007272
developera3511852023-06-14 14:12:59 +08007273 nla_parse(tb,
7274 NL80211_ATTR_MAX,
7275 genlmsg_attrdata(gnlh, 0),
7276 genlmsg_attrlen(gnlh, 0),
7277 NULL);
developer72fb0bb2023-01-11 09:46:29 +08007278
developera3511852023-06-14 14:12:59 +08007279 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +08007280 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +08007281 return NL_SKIP;
7282 }
developer72fb0bb2023-01-11 09:46:29 +08007283
7284
developera3511852023-06-14 14:12:59 +08007285 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +08007286 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +08007287 return NL_SKIP;
7288 }
developer72fb0bb2023-01-11 09:46:29 +08007289
developera3511852023-06-14 14:12:59 +08007290 //devIndex starts from 1
7291 if( ++count == out->wifi_devIndex )
7292 {
7293 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
7294 //Getting the mac addrress
7295 mac_addr_aton(out->wifi_devMacAddress,mac_addr);
developer12fb9f62023-06-30 15:26:27 +08007296 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7297 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
7298 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7299 return NL_SKIP;
7300 }
developera3511852023-06-14 14:12:59 +08007301 }
developer72fb0bb2023-01-11 09:46:29 +08007302
developera3511852023-06-14 14:12:59 +08007303 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7304 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7305 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7306 out->wifi_devTxRate = rate/10;
7307 }
7308 }
developer72fb0bb2023-01-11 09:46:29 +08007309
developer12fb9f62023-06-30 15:26:27 +08007310 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7311 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) {
7312 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7313 return NL_SKIP;
7314 }
developera3511852023-06-14 14:12:59 +08007315 }
developera3511852023-06-14 14:12:59 +08007316 if(sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7317 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7318 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7319 out->wifi_devRxRate = rate/10;
7320 }
7321 }
7322 if(sinfo[NL80211_STA_INFO_SIGNAL_AVG])
7323 out->wifi_devSignalStrength = (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
developer72fb0bb2023-01-11 09:46:29 +08007324
developera3511852023-06-14 14:12:59 +08007325 out->wifi_devAssociatedDeviceAuthentiationState = 1;
7326 count = 0; //starts the count for next cycle
7327 return NL_STOP;
7328 }
developer72fb0bb2023-01-11 09:46:29 +08007329
developera3511852023-06-14 14:12:59 +08007330 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08007331
7332}
7333#endif
7334
7335INT wifi_getAssociatedDeviceDetail(INT apIndex, INT devIndex, wifi_device_t *output_struct)
7336{
developera3511852023-06-14 14:12:59 +08007337 Netlink nl = {0};
7338 char if_name[IF_NAME_SIZE] = {0};
7339 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08007340 int res;
developer72fb0bb2023-01-11 09:46:29 +08007341
developera3511852023-06-14 14:12:59 +08007342 wifi_device_info_t info = {0};
7343 info.wifi_devIndex = devIndex;
developer72fb0bb2023-01-11 09:46:29 +08007344
developera3511852023-06-14 14:12:59 +08007345 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
7346 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007347
developere40952c2023-06-15 18:46:43 +08007348 res = snprintf(if_name,sizeof(if_name),"%s", interface_name);
7349 if (os_snprintf_error(sizeof(if_name), res)) {
7350 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7351 return RETURN_ERR;
7352 }
developer72fb0bb2023-01-11 09:46:29 +08007353
developera3511852023-06-14 14:12:59 +08007354 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007355
developera3511852023-06-14 14:12:59 +08007356 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +08007357 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +08007358 return -1;
7359 }
developer72fb0bb2023-01-11 09:46:29 +08007360
developera3511852023-06-14 14:12:59 +08007361 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +08007362
developera3511852023-06-14 14:12:59 +08007363 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +08007364 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +08007365 nlfree(&nl);
7366 return -2;
7367 }
developer72fb0bb2023-01-11 09:46:29 +08007368
developera3511852023-06-14 14:12:59 +08007369 genlmsg_put(msg,
7370 NL_AUTO_PID,
7371 NL_AUTO_SEQ,
7372 nl.id,
7373 0,
7374 NLM_F_DUMP,
7375 NL80211_CMD_GET_STATION,
7376 0);
developer72fb0bb2023-01-11 09:46:29 +08007377
developera3511852023-06-14 14:12:59 +08007378 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
7379 nl_send_auto_complete(nl.socket, msg);
7380 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,AssoDevInfo_callback,&info);
7381 nl_recvmsgs(nl.socket, nl.cb);
7382 nlmsg_free(msg);
7383 nlfree(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007384
developera3511852023-06-14 14:12:59 +08007385 output_struct->wifi_devAssociatedDeviceAuthentiationState = info.wifi_devAssociatedDeviceAuthentiationState;
7386 output_struct->wifi_devRxRate = info.wifi_devRxRate;
7387 output_struct->wifi_devTxRate = info.wifi_devTxRate;
7388 output_struct->wifi_devSignalStrength = info.wifi_devSignalStrength;
7389 memcpy(&output_struct->wifi_devMacAddress, &info.wifi_devMacAddress, sizeof(info.wifi_devMacAddress));
7390 return RETURN_OK;
7391}
developer72fb0bb2023-01-11 09:46:29 +08007392
developera3511852023-06-14 14:12:59 +08007393INT wifi_kickAssociatedDevice(INT apIndex, wifi_device_t *device)
7394{
7395 if (NULL == device)
7396 return RETURN_ERR;
7397 return RETURN_OK;
7398}
7399//<<
developer72fb0bb2023-01-11 09:46:29 +08007400
developer72fb0bb2023-01-11 09:46:29 +08007401
7402//--------------wifi_ap_hal-----------------------------
7403//enables CTS protection for the radio used by this AP
7404INT wifi_setRadioCtsProtectionEnable(INT apIndex, BOOL enable)
7405{
developera3511852023-06-14 14:12:59 +08007406 //save config and Apply instantly
7407 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007408}
7409
7410// enables OBSS Coexistence - fall back to 20MHz if necessary for the radio used by this ap
7411INT wifi_setRadioObssCoexistenceEnable(INT apIndex, BOOL enable)
7412{
developera3511852023-06-14 14:12:59 +08007413 char config_file[64] = {'\0'};
7414 char config_dat_file[64] = {'\0'};
7415 char buf[64] = {'\0'};
7416 struct params list = {0};
7417 struct params dat = {0};
7418 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007419 int res;
developer72fb0bb2023-01-11 09:46:29 +08007420
developera3511852023-06-14 14:12:59 +08007421 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7422 list.name = "ht_coex";
developere40952c2023-06-15 18:46:43 +08007423 res = snprintf(buf, sizeof(buf), "%d", enable);
7424 if (os_snprintf_error(sizeof(buf), res)) {
7425 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7426 return RETURN_ERR;
7427 }
7428
developera3511852023-06-14 14:12:59 +08007429 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007430
developera3511852023-06-14 14:12:59 +08007431 dat.name = "HT_BSSCoexistence";
7432 dat.value = buf;
developerd1824452023-05-18 12:30:04 +08007433
developera3511852023-06-14 14:12:59 +08007434 band = wifi_index_to_band(apIndex);
developere40952c2023-06-15 18:46:43 +08007435 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7436 if (os_snprintf_error(sizeof(config_file), res)) {
7437 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7438 return RETURN_ERR;
7439 }
7440
7441 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7442 if (os_snprintf_error(sizeof(config_dat_file), res)) {
7443 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7444 return RETURN_ERR;
7445 }
7446
developera3511852023-06-14 14:12:59 +08007447 wifi_hostapdWrite(config_file, &list, 1);
7448 wifi_datfileWrite(config_dat_file, &dat, 1);
7449 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007450
developera3511852023-06-14 14:12:59 +08007451 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007452
developera3511852023-06-14 14:12:59 +08007453 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007454}
7455
7456//P3 // sets the fragmentation threshold in bytes for the radio used by this ap
7457INT wifi_setRadioFragmentationThreshold(INT apIndex, UINT threshold)
7458{
developera3511852023-06-14 14:12:59 +08007459 char config_file[MAX_BUF_SIZE] = {'\0'};
7460 char buf[MAX_BUF_SIZE] = {'\0'};
7461 struct params list;
developere40952c2023-06-15 18:46:43 +08007462 int res;
developer72fb0bb2023-01-11 09:46:29 +08007463
developera3511852023-06-14 14:12:59 +08007464 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7465 if (threshold < 256 || threshold > 2346 )
7466 return RETURN_ERR;
7467 list.name = "fragm_threshold";
developere40952c2023-06-15 18:46:43 +08007468 res = snprintf(buf, sizeof(buf), "%d", threshold);
7469 if (os_snprintf_error(sizeof(buf), res)) {
7470 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7471 return RETURN_ERR;
7472 }
7473
developera3511852023-06-14 14:12:59 +08007474 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007475
developere40952c2023-06-15 18:46:43 +08007476 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7477 if (os_snprintf_error(sizeof(config_file), res)) {
7478 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7479 return RETURN_ERR;
7480 }
7481
developera3511852023-06-14 14:12:59 +08007482 wifi_hostapdWrite(config_file, &list, 1);
7483 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007484
developera3511852023-06-14 14:12:59 +08007485 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007486
developera3511852023-06-14 14:12:59 +08007487 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007488}
7489
7490// enable STBC mode in the hardwarwe, 0 == not enabled, 1 == enabled
7491INT wifi_setRadioSTBCEnable(INT radioIndex, BOOL STBC_Enable)
7492{
developera3511852023-06-14 14:12:59 +08007493 char config_file[64] = {'\0'};
7494 char cmd[512] = {'\0'};
7495 char buf[512] = {'\0'};
7496 char stbc_config[16] = {'\0'};
7497 wifi_band band;
7498 int iterator = 0;
7499 BOOL current_stbc = FALSE;
7500 int ant_count = 0;
7501 int ant_bitmap = 0;
7502 struct params list;
7503 char dat_file[64] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007504 int res;
developer72fb0bb2023-01-11 09:46:29 +08007505
developera3511852023-06-14 14:12:59 +08007506 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007507
developera3511852023-06-14 14:12:59 +08007508 band = wifi_index_to_band(radioIndex);
7509 if (band == band_invalid)
7510 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007511
developera3511852023-06-14 14:12:59 +08007512 if (band == band_2_4)
7513 iterator = 1;
7514 else if ((band == band_5) || (band == band_6))
7515 iterator = 2;
7516 else
7517 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007518
developera3511852023-06-14 14:12:59 +08007519 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);
7520 for (; ant_bitmap > 0; ant_bitmap >>= 1)
7521 ant_count += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08007522
developera3511852023-06-14 14:12:59 +08007523 if (ant_count == 1 && STBC_Enable == TRUE) {
developer75bd10c2023-06-27 11:34:08 +08007524 wifi_debug(DEBUG_ERROR, "can not enable STBC when using only one antenna\n");
developera3511852023-06-14 14:12:59 +08007525 return RETURN_OK;
7526 }
developer72fb0bb2023-01-11 09:46:29 +08007527
developere40952c2023-06-15 18:46:43 +08007528 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7529 if (os_snprintf_error(sizeof(config_file), res)) {
7530 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7531 return RETURN_ERR;
7532 }
developer72fb0bb2023-01-11 09:46:29 +08007533
developera3511852023-06-14 14:12:59 +08007534 // set ht and vht config
7535 for (int i = 0; i < iterator; i++) {
7536 memset(stbc_config, 0, sizeof(stbc_config));
7537 memset(cmd, 0, sizeof(cmd));
7538 memset(buf, 0, sizeof(buf));
7539 list.name = (i == 0)?"ht_capab":"vht_capab";
developere40952c2023-06-15 18:46:43 +08007540 res = snprintf(stbc_config, sizeof(stbc_config), "%s", list.name);
7541 if (os_snprintf_error(sizeof(stbc_config), res)) {
7542 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7543 return RETURN_ERR;
7544 }
7545
7546 res = snprintf(cmd, sizeof(cmd), "cat %s | grep -E '^%s' | grep 'STBC'", config_file, stbc_config);
7547 if (os_snprintf_error(sizeof(cmd), res)) {
7548 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7549 return RETURN_ERR;
7550 }
7551
developera3511852023-06-14 14:12:59 +08007552 _syscmd(cmd, buf, sizeof(buf));
7553 if (strlen(buf) != 0)
7554 current_stbc = TRUE;
7555 if (current_stbc == STBC_Enable)
7556 continue;
developer72fb0bb2023-01-11 09:46:29 +08007557
developera3511852023-06-14 14:12:59 +08007558 if (STBC_Enable == TRUE) {
7559 // Append the STBC flags in capab config
7560 memset(cmd, 0, sizeof(cmd));
7561 if (i == 0)
developere40952c2023-06-15 18:46:43 +08007562 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC][RX-STBC1]/' %s", config_file);
developera3511852023-06-14 14:12:59 +08007563 else
developere40952c2023-06-15 18:46:43 +08007564 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1][RX-STBC-1]/' %s", config_file);
7565 if (os_snprintf_error(sizeof(cmd), res)) {
7566 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7567 return RETURN_ERR;
7568 }
7569
developera3511852023-06-14 14:12:59 +08007570 _syscmd(cmd, buf, sizeof(buf));
7571 } else if (STBC_Enable == FALSE) {
7572 // Remove the STBC flags and remain other flags in capab
7573 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08007574 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7575 if (os_snprintf_error(sizeof(cmd), res)) {
7576 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7577 return RETURN_ERR;
7578 }
7579
developera3511852023-06-14 14:12:59 +08007580 _syscmd(cmd, buf, sizeof(buf));
7581 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08007582 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[RX-STBC-?[1-3]*\\]//' %s", config_file);
7583 if (os_snprintf_error(sizeof(cmd), res)) {
7584 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7585 return RETURN_ERR;
7586 }
7587
developera3511852023-06-14 14:12:59 +08007588 _syscmd(cmd, buf, sizeof(buf));
7589 }
7590 wifi_hostapdRead(config_file, list.name, buf, sizeof(buf));
7591 list.value = buf;
7592 wifi_hostapdProcessUpdate(radioIndex, &list, 1);
7593 }
developere40952c2023-06-15 18:46:43 +08007594 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7595 if (os_snprintf_error(sizeof(dat_file), res)) {
7596 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7597 return RETURN_ERR;
7598 }
7599
7600 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_STBC=.*/HT_STBC=%d/g' %s", STBC_Enable, dat_file);
7601 if (os_snprintf_error(sizeof(cmd), res)) {
7602 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7603 return RETURN_ERR;
7604 }
7605
developera1255e42023-05-13 17:45:02 +08007606 _syscmd(cmd, buf, sizeof(buf));
7607 if ((band == band_5) || (band == band_6)) {
developere40952c2023-06-15 18:46:43 +08007608 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^VHT_STBC=.*/VHT_STBC=%d/g' %s", STBC_Enable, dat_file);
7609 if (os_snprintf_error(sizeof(cmd), res)) {
7610 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7611 return RETURN_ERR;
7612 }
7613
developera1255e42023-05-13 17:45:02 +08007614 _syscmd(cmd, buf, sizeof(buf));
7615 }
developera3511852023-06-14 14:12:59 +08007616 /*wifi_reloadAp(radioIndex);
developera1255e42023-05-13 17:45:02 +08007617 the caller do this.*/
developer72fb0bb2023-01-11 09:46:29 +08007618
developera3511852023-06-14 14:12:59 +08007619 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7620 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007621}
7622
7623// outputs A-MSDU enable status, 0 == not enabled, 1 == enabled
7624INT wifi_getRadioAMSDUEnable(INT radioIndex, BOOL *output_bool)
7625{
developera3511852023-06-14 14:12:59 +08007626 char dat_file[128] = {0};
developer2c22d832023-05-18 17:46:26 +08007627 wifi_band band;
7628 char amdus_buff[8] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007629 int res;
developer72fb0bb2023-01-11 09:46:29 +08007630
developer2c22d832023-05-18 17:46:26 +08007631 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007632
developer2c22d832023-05-18 17:46:26 +08007633 band = wifi_index_to_band(radioIndex);
7634 if (band == band_invalid) {
7635 printf("%s:Band Error\n", __func__);
7636 return RETURN_ERR;
7637 }
developere40952c2023-06-15 18:46:43 +08007638 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7639 if (os_snprintf_error(sizeof(dat_file), res)) {
7640 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7641 return RETURN_ERR;
7642 }
7643
developer2c22d832023-05-18 17:46:26 +08007644 wifi_datfileRead(dat_file, "HT_AMSDU", amdus_buff, sizeof(amdus_buff));
7645 if (strncmp(amdus_buff, "1", 1) == 0)
developera3511852023-06-14 14:12:59 +08007646 *output_bool = TRUE;
developer2c22d832023-05-18 17:46:26 +08007647 else
7648 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08007649
developer2c22d832023-05-18 17:46:26 +08007650 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7651
7652 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007653}
7654
7655// enables A-MSDU in the hardware, 0 == not enabled, 1 == enabled
7656INT wifi_setRadioAMSDUEnable(INT radioIndex, BOOL amsduEnable)
7657{
developer2c22d832023-05-18 17:46:26 +08007658 char dat_file[128] = {0};
7659 BOOL enable;
7660 wifi_band band;
7661 char amdus_buff[8] = {'\0'};
7662 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08007663 int res;
developer72fb0bb2023-01-11 09:46:29 +08007664
developer2c22d832023-05-18 17:46:26 +08007665 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007666
developer2c22d832023-05-18 17:46:26 +08007667 band = wifi_index_to_band(radioIndex);
7668 if (band == band_invalid) {
7669 printf("%s:Band Error\n", __func__);
7670 return RETURN_ERR;
7671 }
developere40952c2023-06-15 18:46:43 +08007672 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7673 if (os_snprintf_error(sizeof(dat_file), res)) {
7674 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7675 return RETURN_ERR;
7676 }
7677
developer2c22d832023-05-18 17:46:26 +08007678 wifi_datfileRead(dat_file, "HT_AMSDU", amdus_buff, sizeof(amdus_buff));
7679 if (strncmp(amdus_buff, "1", 1) == 0)
developera3511852023-06-14 14:12:59 +08007680 enable = TRUE;
developer2c22d832023-05-18 17:46:26 +08007681 else
7682 enable = FALSE;
7683 if (amsduEnable == enable)
developera3511852023-06-14 14:12:59 +08007684 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007685
developer2c22d832023-05-18 17:46:26 +08007686 params.name = "HT_AMSDU";
7687 if (amsduEnable)
7688 params.value = "1";
7689 else
7690 params.value = "0";
7691 wifi_datfileWrite(dat_file, &params, 1);
7692 wifi_reloadAp(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08007693
developer2c22d832023-05-18 17:46:26 +08007694 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007695
developera3511852023-06-14 14:12:59 +08007696 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007697}
7698
7699//P2 // outputs the number of Tx streams
7700INT wifi_getRadioTxChainMask(INT radioIndex, INT *output_int)
7701{
developera3511852023-06-14 14:12:59 +08007702 char buf[8] = {0};
7703 char cmd[128] = {0};
7704 int phyId = 0;
developere40952c2023-06-15 18:46:43 +08007705 int res;
developerc14d83a2023-06-29 20:09:42 +08007706 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007707
developera3511852023-06-14 14:12:59 +08007708 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007709
developera3511852023-06-14 14:12:59 +08007710 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +08007711 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'Configured Antennas' | awk '{print $4}'", phyId);
7712 if (os_snprintf_error(sizeof(cmd), res)) {
7713 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7714 return RETURN_ERR;
7715 }
7716
developera3511852023-06-14 14:12:59 +08007717 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007718
developerc14d83a2023-06-29 20:09:42 +08007719 if (hal_strtol(buf, 16, &tmp) < 0) {
7720 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007721 }
developerc14d83a2023-06-29 20:09:42 +08007722 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007723
developera3511852023-06-14 14:12:59 +08007724 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007725
developera3511852023-06-14 14:12:59 +08007726 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007727}
7728
7729INT fitChainMask(INT radioIndex, int antcount)
7730{
developera3511852023-06-14 14:12:59 +08007731 char buf[128] = {0};
7732 char cmd[128] = {0};
7733 char config_file[64] = {0};
7734 wifi_band band;
7735 struct params list[2] = {0};
developere40952c2023-06-15 18:46:43 +08007736 int res;
developer72fb0bb2023-01-11 09:46:29 +08007737
developera3511852023-06-14 14:12:59 +08007738 band = wifi_index_to_band(radioIndex);
7739 if (band == band_invalid)
7740 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007741
developera3511852023-06-14 14:12:59 +08007742 list[0].name = "he_mu_beamformer";
7743 list[1].name = "he_su_beamformer";
developer72fb0bb2023-01-11 09:46:29 +08007744
developere40952c2023-06-15 18:46:43 +08007745 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7746 if (os_snprintf_error(sizeof(config_file), res)) {
7747 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7748 return RETURN_ERR;
7749 }
7750
developera3511852023-06-14 14:12:59 +08007751 if (antcount == 1) {
7752 // remove config about multiple antennas
developere40952c2023-06-15 18:46:43 +08007753 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7754 if (os_snprintf_error(sizeof(cmd), res)) {
7755 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7756 return RETURN_ERR;
7757 }
7758
developera3511852023-06-14 14:12:59 +08007759 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007760
developere40952c2023-06-15 18:46:43 +08007761 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SOUNDING-DIMENSION-.\\]//' %s", config_file);
7762 if (os_snprintf_error(sizeof(cmd), res)) {
7763 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7764 return RETURN_ERR;
7765 }
7766
developera3511852023-06-14 14:12:59 +08007767 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007768
developere40952c2023-06-15 18:46:43 +08007769 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SU-BEAMFORMER\\]//' %s", config_file);
7770 if (os_snprintf_error(sizeof(cmd), res)) {
7771 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7772 return RETURN_ERR;
7773 }
7774
developera3511852023-06-14 14:12:59 +08007775 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007776
developere40952c2023-06-15 18:46:43 +08007777 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[MU-BEAMFORMER\\]//' %s", config_file);
7778 if (os_snprintf_error(sizeof(cmd), res)) {
7779 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7780 return RETURN_ERR;
7781 }
7782
developera3511852023-06-14 14:12:59 +08007783 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007784
developera3511852023-06-14 14:12:59 +08007785 list[0].value = "0";
7786 list[1].value = "0";
7787 } else {
7788 // 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.
7789 if (band == band_2_4 || band == band_5) {
developere40952c2023-06-15 18:46:43 +08007790 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '^ht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7791 if (os_snprintf_error(sizeof(cmd), res)) {
7792 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7793 return RETURN_ERR;
7794 }
7795
developera3511852023-06-14 14:12:59 +08007796 _syscmd(cmd, buf, sizeof(buf));
7797 if (strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +08007798 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC]/' %s", config_file);
7799 if (os_snprintf_error(sizeof(cmd), res)) {
7800 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7801 return RETURN_ERR;
7802 }
7803
developera3511852023-06-14 14:12:59 +08007804 _syscmd(cmd, buf, sizeof(buf));
7805 }
7806 }
7807 if (band == band_5) {
developere40952c2023-06-15 18:46:43 +08007808 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '^vht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7809 if (os_snprintf_error(sizeof(cmd), res)) {
7810 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7811 return RETURN_ERR;
7812 }
7813
developera3511852023-06-14 14:12:59 +08007814 _syscmd(cmd, buf, sizeof(buf));
7815 if (strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +08007816 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1]/' %s", config_file);
7817 if (os_snprintf_error(sizeof(cmd), res)) {
7818 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7819 return RETURN_ERR;
7820 }
7821
developera3511852023-06-14 14:12:59 +08007822 _syscmd(cmd, buf, sizeof(buf));
7823 }
7824 }
developer72fb0bb2023-01-11 09:46:29 +08007825
developere40952c2023-06-15 18:46:43 +08007826 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[SU-BEAMFORMER\\]'", config_file);
7827 if (os_snprintf_error(sizeof(cmd), res)) {
7828 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7829 return RETURN_ERR;
7830 }
7831
developera3511852023-06-14 14:12:59 +08007832 _syscmd(cmd, buf, sizeof(buf));
7833 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007834 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SU-BEAMFORMER]/' %s", config_file);
7835 if (os_snprintf_error(sizeof(cmd), res)) {
7836 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7837 return RETURN_ERR;
7838 }
7839
developera3511852023-06-14 14:12:59 +08007840 _syscmd(cmd, buf, sizeof(buf));
7841 }
developer72fb0bb2023-01-11 09:46:29 +08007842
developere40952c2023-06-15 18:46:43 +08007843 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[MU-BEAMFORMER\\]'", config_file);
developerc14d83a2023-06-29 20:09:42 +08007844 if (os_snprintf_error(sizeof(cmd), res)) {
7845 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7846 return RETURN_ERR;
7847 }
developera3511852023-06-14 14:12:59 +08007848 _syscmd(cmd, buf, sizeof(buf));
7849 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007850 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[MU-BEAMFORMER]/' %s", config_file);
7851 if (os_snprintf_error(sizeof(cmd), res)) {
7852 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7853 return RETURN_ERR;
7854 }
7855
developera3511852023-06-14 14:12:59 +08007856 _syscmd(cmd, buf, sizeof(buf));
7857 }
developer72fb0bb2023-01-11 09:46:29 +08007858
developere40952c2023-06-15 18:46:43 +08007859 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[SOUNDING-DIMENSION-.\\]'", config_file);
7860 if (os_snprintf_error(sizeof(cmd), res)) {
7861 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7862 return RETURN_ERR;
7863 }
7864
developera3511852023-06-14 14:12:59 +08007865 _syscmd(cmd, buf, sizeof(buf));
7866 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007867 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SOUNDING-DIMENSION-%d]/' %s", antcount, config_file);
developera3511852023-06-14 14:12:59 +08007868 } else {
developere40952c2023-06-15 18:46:43 +08007869 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/(SOUNDING-DIMENSION-)./\\1%d/' %s", antcount, config_file);
7870 }
7871 if (os_snprintf_error(sizeof(cmd), res)) {
7872 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7873 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08007874 }
developere40952c2023-06-15 18:46:43 +08007875
developera3511852023-06-14 14:12:59 +08007876 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007877
developera3511852023-06-14 14:12:59 +08007878 list[0].value = "1";
7879 list[1].value = "1";
7880 }
7881 wifi_hostapdWrite(config_file, list, 2);
developerdaf24792023-06-06 11:40:04 +08007882 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007883}
7884
7885//P2 // sets the number of Tx streams to an enviornment variable
7886INT wifi_setRadioTxChainMask(INT radioIndex, INT numStreams)
7887{
developera3511852023-06-14 14:12:59 +08007888 char cmd[128] = {0};
7889 char buf[128] = {0};
7890 int phyId = 0;
7891 int cur_mask = 0;
7892 int antcountmsk = 0;
developera1255e42023-05-13 17:45:02 +08007893 INT cur_nss = 0;
developer863a4a62023-06-06 16:55:59 +08007894 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +08007895 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007896 int res;
developer72fb0bb2023-01-11 09:46:29 +08007897
developera3511852023-06-14 14:12:59 +08007898 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007899
developera3511852023-06-14 14:12:59 +08007900 if (numStreams <= 0) {
developer75bd10c2023-06-27 11:34:08 +08007901 wifi_debug(DEBUG_ERROR, "chainmask is not supported %d.\n", numStreams);
developera3511852023-06-14 14:12:59 +08007902 return RETURN_ERR;
7903 }
developer72fb0bb2023-01-11 09:46:29 +08007904
developera3511852023-06-14 14:12:59 +08007905 wifi_getRadioTxChainMask(radioIndex, &cur_mask);//this is mask value
developera1255e42023-05-13 17:45:02 +08007906 for(; cur_mask > 0; cur_mask >>= 1)//convert to number of streams.
7907 cur_nss += 1;
7908 WIFI_ENTRY_EXIT_DEBUG("%s:cur_nss=%d, new_nss=%d\n", __func__, cur_nss, numStreams);
developera3511852023-06-14 14:12:59 +08007909 if (cur_nss == numStreams)
7910 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007911
developera3511852023-06-14 14:12:59 +08007912 wifi_setRadioEnable(radioIndex, FALSE);
developer72fb0bb2023-01-11 09:46:29 +08007913
developera3511852023-06-14 14:12:59 +08007914 phyId = radio_index_to_phy(radioIndex);
developera1255e42023-05-13 17:45:02 +08007915 //iw need mask value.
7916 for (;numStreams > 0; numStreams--)
7917 antcountmsk |= 0x1 << (numStreams - 1);
developere40952c2023-06-15 18:46:43 +08007918 res = snprintf(cmd, sizeof(cmd), "iw phy%d set antenna 0x%x 2>&1", phyId, antcountmsk);
7919 if (os_snprintf_error(sizeof(cmd), res)) {
7920 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7921 return RETURN_ERR;
7922 }
developerb758dfd2023-06-21 17:32:07 +08007923
developera3511852023-06-14 14:12:59 +08007924 _syscmd(cmd, buf, sizeof(buf));
7925 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007926 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007927 return RETURN_ERR;
7928 }
7929 band = wifi_index_to_band(radioIndex);
developera1255e42023-05-13 17:45:02 +08007930 if (band == band_invalid) {
7931 printf("%s:Band Error\n", __func__);
7932 return RETURN_ERR;
7933 }
developere40952c2023-06-15 18:46:43 +08007934 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7935 if (os_snprintf_error(sizeof(dat_file), res)) {
7936 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7937 return RETURN_ERR;
7938 }
developerb758dfd2023-06-21 17:32:07 +08007939
developere40952c2023-06-15 18:46:43 +08007940 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_TxStream=.*/HT_TxStream=%d/g' %s", numStreams, dat_file);
7941 if (os_snprintf_error(sizeof(cmd), res)) {
7942 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7943 return RETURN_ERR;
7944 }
developerb758dfd2023-06-21 17:32:07 +08007945
developera1255e42023-05-13 17:45:02 +08007946 _syscmd(cmd, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +08007947 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007948 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007949 return RETURN_ERR;
7950 }
developere40952c2023-06-15 18:46:43 +08007951 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_RxStream=.*/HT_RxStream=%d/g' %s", numStreams, dat_file);
7952 if (os_snprintf_error(sizeof(cmd), res)) {
7953 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7954 return RETURN_ERR;
7955 }
developerb758dfd2023-06-21 17:32:07 +08007956
developera1255e42023-05-13 17:45:02 +08007957 _syscmd(cmd, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +08007958 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007959 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007960 return RETURN_ERR;
7961 }
7962 fitChainMask(radioIndex, numStreams);
7963 wifi_setRadioEnable(radioIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +08007964
developera3511852023-06-14 14:12:59 +08007965 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7966 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007967}
7968
7969//P2 // outputs the number of Rx streams
7970INT wifi_getRadioRxChainMask(INT radioIndex, INT *output_int)
7971{
developera3511852023-06-14 14:12:59 +08007972 char buf[8] = {0};
7973 char cmd[128] = {0};
7974 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +08007975 int res;
developerc14d83a2023-06-29 20:09:42 +08007976 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007977
developera3511852023-06-14 14:12:59 +08007978 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007979
developera3511852023-06-14 14:12:59 +08007980 phyId = radio_index_to_phy(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08007981
7982 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'Configured Antennas' | awk '{print $6}'", phyId);
7983 if (os_snprintf_error(sizeof(cmd), res)) {
7984 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7985 return RETURN_ERR;
7986 }
developera3511852023-06-14 14:12:59 +08007987 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007988
developerc14d83a2023-06-29 20:09:42 +08007989 if (hal_strtol(buf, 16, &tmp) < 0) {
7990 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007991 }
developerc14d83a2023-06-29 20:09:42 +08007992 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007993
developera3511852023-06-14 14:12:59 +08007994 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007995
developera3511852023-06-14 14:12:59 +08007996 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007997}
7998
7999//P2 // sets the number of Rx streams to an enviornment variable
8000INT wifi_setRadioRxChainMask(INT radioIndex, INT numStreams)
8001{
developera3511852023-06-14 14:12:59 +08008002 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8003 if (wifi_setRadioTxChainMask(radioIndex, numStreams) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08008004 wifi_debug(DEBUG_ERROR, "wifi_setRadioTxChainMask return error.\n");
developera3511852023-06-14 14:12:59 +08008005 return RETURN_ERR;
8006 }
8007 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8008 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008009}
8010
8011//Get radio RDG enable setting
8012INT wifi_getRadioReverseDirectionGrantSupported(INT radioIndex, BOOL *output_bool)
8013{
developer47cc27a2023-05-17 23:09:58 +08008014 if (NULL == output_bool)
8015 return RETURN_ERR;
8016
8017 *output_bool = TRUE;
8018 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008019}
8020
8021//Get radio RDG enable setting
8022INT wifi_getRadioReverseDirectionGrantEnable(INT radioIndex, BOOL *output_bool)
8023{
developer47cc27a2023-05-17 23:09:58 +08008024 char rdg_status[2] = {0};
8025 char dat_file[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008026 int res;
developer47cc27a2023-05-17 23:09:58 +08008027
8028 if (NULL == output_bool)
8029 return RETURN_ERR;
8030
8031 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08008032 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
8033 if (os_snprintf_error(sizeof(dat_file), res)) {
8034 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8035 return RETURN_ERR;
8036 }
developer47cc27a2023-05-17 23:09:58 +08008037
8038 wifi_datfileRead(dat_file, "HT_RDG", rdg_status, sizeof(rdg_status));
8039 if (!strncmp(rdg_status, "1", sizeof(rdg_status)))
8040 *output_bool = TRUE;
8041 else
8042 *output_bool = FALSE;
8043
8044 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008045}
8046
8047//Set radio RDG enable setting
8048INT wifi_setRadioReverseDirectionGrantEnable(INT radioIndex, BOOL enable)
8049{
developer47cc27a2023-05-17 23:09:58 +08008050 char dat_file[MAX_CMD_SIZE] = {0};
8051 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08008052 int res;
developer47cc27a2023-05-17 23:09:58 +08008053
8054 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08008055 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
8056 if (os_snprintf_error(sizeof(dat_file), res)) {
8057 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8058 return RETURN_ERR;
8059 }
developer47cc27a2023-05-17 23:09:58 +08008060
8061 params.name = "HT_RDG";
8062
developera3511852023-06-14 14:12:59 +08008063 if (enable) {
8064 params.value = "1";
8065 } else {
8066 params.value = "0";
8067 }
developer47cc27a2023-05-17 23:09:58 +08008068
8069 wifi_datfileWrite(dat_file, &params, 1);
8070
8071 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008072}
8073
developer5cd4c862023-05-26 09:34:42 +08008074
8075int mtk_get_ba_auto_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08008076{
developer5cd4c862023-05-26 09:34:42 +08008077 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8078 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
8079 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8080 unsigned char status;
8081 unsigned char *out_status = data;
8082 int err = 0;
developer8e6583c2023-05-23 13:36:06 +08008083
developer5cd4c862023-05-26 09:34:42 +08008084 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8085 genlmsg_attrlen(gnlh, 0), NULL);
8086 if (err < 0){
8087 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8088 return err;
8089 }
developer8e6583c2023-05-23 13:36:06 +08008090
developer5cd4c862023-05-26 09:34:42 +08008091 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8092 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
8093 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8094 if (err < 0){
8095 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8096 return err;
8097 }
developer8e6583c2023-05-23 13:36:06 +08008098
developer5cd4c862023-05-26 09:34:42 +08008099 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]) {
8100 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]);
8101 if (status == 0) {
8102 wifi_debug(DEBUG_NOTICE, "disabled\n");
8103 } else {
8104 wifi_debug(DEBUG_NOTICE, "enabled\n");
8105 }
8106 *out_status = status;
8107 }
8108 }
developer8e6583c2023-05-23 13:36:06 +08008109
developer5cd4c862023-05-26 09:34:42 +08008110 return 0;
8111}
developer8e6583c2023-05-23 13:36:06 +08008112
developer5cd4c862023-05-26 09:34:42 +08008113int mtk_get_ba_decline_status_callback(struct nl_msg *msg, void *data)
8114{
8115 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8116 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
8117 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8118 unsigned char status;
8119 unsigned char *out_status = data;
8120 int err = 0;
8121
8122 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8123 genlmsg_attrlen(gnlh, 0), NULL);
8124 if (err < 0) {
8125 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8126 return err;
8127 }
8128
8129 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8130 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
8131 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8132 if (err < 0) {
8133 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8134 return err;
8135 }
8136
8137 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]) {
8138 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]);
8139 if (status == 0) {
8140 wifi_debug(DEBUG_NOTICE, "disabled\n");
8141 } else {
8142 wifi_debug(DEBUG_NOTICE, "enabled\n");
8143 }
8144 *out_status = status;
8145 }
8146 }
8147
8148 return NL_OK;
developer72fb0bb2023-01-11 09:46:29 +08008149}
8150
developer5cd4c862023-05-26 09:34:42 +08008151INT mtk_wifi_get_ba_decl_auto_status(
8152 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back, BOOL *output_bool)
8153{
8154 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008155 unsigned int if_idx = 0;
8156 int ret = -1;
8157 struct unl unl_ins;
8158 struct nl_msg *msg = NULL;
8159 struct nlattr * msg_data = NULL;
8160 struct mtk_nl80211_param param;
8161
8162 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8163 return RETURN_ERR;
8164 if_idx = if_nametoindex(inf_name);
8165 if (!if_idx) {
8166 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8167 return RETURN_ERR;
8168 }
8169 /*init mtk nl80211 vendor cmd*/
8170 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8171 param.if_type = NL80211_ATTR_IFINDEX;
8172 param.if_idx = if_idx;
8173
8174 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8175 if (ret) {
8176 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8177 return RETURN_ERR;
8178 }
8179 /*add mtk vendor cmd data*/
8180 if (nla_put_u8(msg, vendor_data_attr, 0xf)) {
8181 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8182 nlmsg_free(msg);
8183 goto err;
8184 }
8185
8186 /*send mtk nl80211 vendor msg*/
8187 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8188 if (ret) {
8189 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8190 goto err;
8191 }
8192 /*deinit mtk nl80211 vendor msg*/
8193 mtk_nl80211_deint(&unl_ins);
8194 wifi_debug(DEBUG_NOTICE,"send cmd success, get output_bool:%d\n", *output_bool);
8195 return RETURN_OK;
8196err:
8197 mtk_nl80211_deint(&unl_ins);
8198 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8199 return RETURN_ERR;
8200}
developere0ff7232023-06-08 16:33:14 +08008201
8202INT mtk_wifi_set_auto_ba_en(
8203 INT apIndex, INT vendor_data_attr, BOOL enable)
8204{
8205 char inf_name[IF_NAME_SIZE] = {0};
8206 unsigned int if_idx = 0;
8207 int ret = -1;
8208 struct unl unl_ins;
8209 struct nl_msg *msg = NULL;
8210 struct nlattr * msg_data = NULL;
8211 struct mtk_nl80211_param param;
8212
8213 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8214 return RETURN_ERR;
8215 if_idx = if_nametoindex(inf_name);
8216 if (!if_idx) {
8217 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8218 return RETURN_ERR;
8219 }
8220 /*init mtk nl80211 vendor cmd*/
8221 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8222 param.if_type = NL80211_ATTR_IFINDEX;
8223 param.if_idx = if_idx;
8224
8225 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8226 if (ret) {
8227 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8228 return RETURN_ERR;
8229 }
8230 /*add mtk vendor cmd data*/
8231 if (nla_put_u8(msg, vendor_data_attr, enable)) {
8232 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8233 nlmsg_free(msg);
8234 goto err;
8235 }
8236
8237 /*send mtk nl80211 vendor msg*/
8238 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8239 if (ret) {
8240 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8241 goto err;
8242 }
8243 /*deinit mtk nl80211 vendor msg*/
8244 mtk_nl80211_deint(&unl_ins);
8245 return RETURN_OK;
8246err:
8247 mtk_nl80211_deint(&unl_ins);
8248 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8249 return RETURN_ERR;
8250}
8251
developer5cd4c862023-05-26 09:34:42 +08008252//Get radio ADDBA enable setting
8253INT wifi_getRadioDeclineBARequestEnable(INT radioIndex, BOOL *output_bool)
8254{
8255 if (output_bool == NULL) {
8256 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8257 return RETURN_ERR;
8258 }
8259 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
8260 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO, mtk_get_ba_decline_status_callback, output_bool) != RETURN_OK) {
8261 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO(0x%x) fails\n",
8262 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO);
8263 return RETURN_ERR;
8264 }
8265 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8266 return RETURN_OK;
8267}
8268
developer72fb0bb2023-01-11 09:46:29 +08008269//Set radio ADDBA enable setting
8270INT wifi_setRadioDeclineBARequestEnable(INT radioIndex, BOOL enable)
8271{
developera3511852023-06-14 14:12:59 +08008272 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008273}
8274
8275//Get radio auto block ack enable setting
8276INT wifi_getRadioAutoBlockAckEnable(INT radioIndex, BOOL *output_bool)
8277{
developer5cd4c862023-05-26 09:34:42 +08008278 if (output_bool == NULL) {
8279 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8280 return RETURN_ERR;
8281 }
developer8e6583c2023-05-23 13:36:06 +08008282
developera3511852023-06-14 14:12:59 +08008283 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
developer5cd4c862023-05-26 09:34:42 +08008284 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO,
8285 mtk_get_ba_auto_status_callback, output_bool) != RETURN_OK) {
8286 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO(0x%x) fails\n",
8287 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO);
8288 return RETURN_ERR;
8289 }
8290 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8291 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008292}
8293
8294//Set radio auto block ack enable setting
8295INT wifi_setRadioAutoBlockAckEnable(INT radioIndex, BOOL enable)
8296{
developera3511852023-06-14 14:12:59 +08008297 if (mtk_wifi_set_auto_ba_en
developere0ff7232023-06-08 16:33:14 +08008298 (radioIndex, MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO, enable) != RETURN_OK) {
8299 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO cmd fails\n");
8300 return RETURN_ERR;
8301 }
8302 wifi_debug(DEBUG_ERROR, "send cmd success: set auto ba enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008303 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008304}
8305
8306//Get radio 11n pure mode enable support
8307INT wifi_getRadio11nGreenfieldSupported(INT radioIndex, BOOL *output_bool)
8308{
developera3511852023-06-14 14:12:59 +08008309 if (NULL == output_bool)
8310 return RETURN_ERR;
8311 *output_bool = TRUE;
8312 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008313}
8314
8315//Get radio 11n pure mode enable setting
8316INT wifi_getRadio11nGreenfieldEnable(INT radioIndex, BOOL *output_bool)
8317{
developera3511852023-06-14 14:12:59 +08008318 if (NULL == output_bool)
8319 return RETURN_ERR;
8320 *output_bool = TRUE;
8321 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008322}
8323
8324//Set radio 11n pure mode enable setting
developer86035662023-06-28 19:21:12 +08008325INT wifi_setRadio11nGreenfieldEnable(INT radioIndex, BOOL enable)
developer72fb0bb2023-01-11 09:46:29 +08008326{
developer82533be2023-06-28 17:21:01 +08008327 char interface_name[16] = {0};
8328 int if_idx, ret = 0;
8329 struct nl_msg *msg = NULL;
8330 struct nlattr * msg_data = NULL;
8331 struct mtk_nl80211_param param;
8332 struct unl unl_ins;
8333
8334 if (radioIndex > MAX_APS) {
8335 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", radioIndex);
8336 return RETURN_ERR;
8337 }
8338 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8339 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
8340 return RETURN_ERR;
developerd14dff12023-06-28 22:47:44 +08008341
developer82533be2023-06-28 17:21:01 +08008342 if_idx = if_nametoindex(interface_name);
8343 /*init mtk nl80211 vendor cmd*/
8344 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
8345 param.if_type = NL80211_ATTR_IFINDEX;
8346 param.if_idx = if_idx;
8347 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8348 if (ret) {
8349 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8350 return RETURN_ERR;
8351 }
8352 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_HT_OP_MODE, enable)) {
8353 printf("Nla put attribute error\n");
8354 nlmsg_free(msg);
8355 goto err;
8356 }
8357 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8358 if (ret) {
8359 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
8360 goto err;
8361 }
8362 mtk_nl80211_deint(&unl_ins);
8363 //wifi_debug(DEBUG_NOTICE, "set Gf cmd success.\n");
8364 printf("set gf=%d cmd success.\n", enable);
8365 return RETURN_OK;
8366err:
8367 mtk_nl80211_deint(&unl_ins);
8368 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08008369 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008370}
8371
developer5cd4c862023-05-26 09:34:42 +08008372int mtk_get_igmp_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08008373{
developer5cd4c862023-05-26 09:34:42 +08008374 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8375 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX + 1];
8376 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8377 unsigned char status = 0, *out_status = data;
8378 int err = 0;
developer72fb0bb2023-01-11 09:46:29 +08008379
developer5cd4c862023-05-26 09:34:42 +08008380 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8381 genlmsg_attrlen(gnlh, 0), NULL);
8382 if (err < 0) {
8383 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8384 return err;
8385 }
developer72fb0bb2023-01-11 09:46:29 +08008386
developer5cd4c862023-05-26 09:34:42 +08008387 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8388 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX,
8389 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8390 if (err < 0){
8391 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX fails\n");
8392 return err;
8393 }
developer72fb0bb2023-01-11 09:46:29 +08008394
developer5cd4c862023-05-26 09:34:42 +08008395 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]) {
8396 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]);
8397 if (status == 0) {
8398 wifi_debug(DEBUG_NOTICE, "disabled\n");
8399 } else {
8400 wifi_debug(DEBUG_NOTICE, "enabled\n");
8401 }
8402 *out_status = status;
8403 wifi_debug(DEBUG_NOTICE, "status: %d\n", *out_status);
8404 }
8405 }
8406
8407 return 0;
8408}
8409
8410INT mtk_wifi_set_igmp_en_status(
8411 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
8412 unsigned char in_en_stat, BOOL *output_bool)
8413{
8414 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008415 unsigned int if_idx = 0;
8416 int ret = -1;
8417 struct unl unl_ins;
8418 struct nl_msg *msg = NULL;
8419 struct nlattr * msg_data = NULL;
8420 struct mtk_nl80211_param param;
8421
8422 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8423 return RETURN_ERR;
8424 if_idx = if_nametoindex(inf_name);
8425 if (!if_idx) {
8426 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8427 return RETURN_ERR;
8428 }
8429 /*init mtk nl80211 vendor cmd*/
8430 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_MULTICAST_SNOOPING;
8431 param.if_type = NL80211_ATTR_IFINDEX;
8432 param.if_idx = if_idx;
8433
8434 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8435 if (ret) {
8436 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8437 return RETURN_ERR;
8438 }
8439 /*add mtk vendor cmd data*/
8440 if (nla_put_u8(msg, vendor_data_attr, in_en_stat)) {
8441 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8442 nlmsg_free(msg);
8443 goto err;
8444 }
8445
8446 /*send mtk nl80211 vendor msg*/
8447 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8448 if (ret) {
8449 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8450 goto err;
8451 }
8452 /*deinit mtk nl80211 vendor msg*/
8453 mtk_nl80211_deint(&unl_ins);
8454 if (output_bool) {
8455 wifi_debug(DEBUG_NOTICE, "send cmd success, get output_bool:%d\n", *output_bool);
8456 } else {
8457 wifi_debug(DEBUG_NOTICE, "send cmd success.\n");
8458 }
8459 return RETURN_OK;
8460err:
8461 mtk_nl80211_deint(&unl_ins);
8462 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8463 return RETURN_ERR;
8464}
8465
8466
8467//Get radio IGMP snooping enable setting
8468INT wifi_getRadioIGMPSnoopingEnable(INT radioIndex, BOOL *output_bool)
8469{
8470 if (output_bool == NULL) {
8471 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8472 return RETURN_ERR;
8473 }
developera3511852023-06-14 14:12:59 +08008474 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008475 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8476 mtk_get_igmp_status_callback, 0xf, output_bool)!= RETURN_OK) {
8477 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8478 return RETURN_ERR;
8479 }
8480 wifi_debug(DEBUG_ERROR, "send cmd success: get igmp status:(%d)\n", *output_bool);
developera3511852023-06-14 14:12:59 +08008481 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008482}
8483
8484//Set radio IGMP snooping enable setting
8485INT wifi_setRadioIGMPSnoopingEnable(INT radioIndex, BOOL enable)
8486{
developera3511852023-06-14 14:12:59 +08008487 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008488 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8489 NULL, enable, NULL) != RETURN_OK) {
8490 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8491 return RETURN_ERR;
8492 }
8493 wifi_debug(DEBUG_ERROR, "send cmd success: set igmp enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008494 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008495}
8496
8497//Get the Reset count of radio
developer69b61b02023-03-07 17:17:44 +08008498INT wifi_getRadioResetCount(INT radioIndex, ULONG *output_int)
developer72fb0bb2023-01-11 09:46:29 +08008499{
developera3511852023-06-14 14:12:59 +08008500 if (NULL == output_int)
8501 return RETURN_ERR;
8502 *output_int = get_radio_reset_cnt(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08008503
developera3511852023-06-14 14:12:59 +08008504 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008505}
8506
8507
8508//---------------------------------------------------------------------------------------------------
8509//
8510// Additional Wifi AP level APIs used for Access Point devices
8511//
8512//---------------------------------------------------------------------------------------------------
8513
8514// creates a new ap and pushes these parameters to the hardware
8515INT wifi_createAp(INT apIndex, INT radioIndex, CHAR *essid, BOOL hideSsid)
8516{
developera3511852023-06-14 14:12:59 +08008517 // Deprecated when use hal version 3, use wifi_createVap() instead.
8518 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008519}
8520
8521// deletes this ap entry on the hardware, clears all internal variables associaated with this ap
8522INT wifi_deleteAp(INT apIndex)
8523{
developer7e4a2a62023-04-06 19:56:03 +08008524 char interface_name[16] = {0};
8525 char buf[MAX_BUF_SIZE];
8526 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08008527 int res;
developer72fb0bb2023-01-11 09:46:29 +08008528
developer7e4a2a62023-04-06 19:56:03 +08008529 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
8530 return RETURN_ERR;
developer8a3bbbf2023-03-15 17:47:23 +08008531
developere40952c2023-06-15 18:46:43 +08008532 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
8533 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
8534 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8535 return RETURN_ERR;
8536 }
developer7e4a2a62023-04-06 19:56:03 +08008537 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008538
developer7e4a2a62023-04-06 19:56:03 +08008539 wifi_removeApSecVaribles(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008540
developer7e4a2a62023-04-06 19:56:03 +08008541 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008542}
8543
8544// Outputs a 16 byte or less name assocated with the AP. String buffer must be pre-allocated by the caller
8545INT wifi_getApName(INT apIndex, CHAR *output_string)
8546{
developer7e4a2a62023-04-06 19:56:03 +08008547 char interface_name[IF_NAME_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08008548 int radio_idx = 0;
8549 int bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008550 int res;
developer72fb0bb2023-01-11 09:46:29 +08008551
developer7e4a2a62023-04-06 19:56:03 +08008552 if(!output_string)
8553 return RETURN_ERR;
8554
8555 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
8556 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
8557
developere40952c2023-06-15 18:46:43 +08008558 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 +08008559 } else
developere40952c2023-06-15 18:46:43 +08008560 res = snprintf(output_string, IF_NAME_SIZE, "%s", interface_name);
8561
8562 if (os_snprintf_error(IF_NAME_SIZE, res)) {
8563 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8564 return RETURN_ERR;
8565 }
developer7e4a2a62023-04-06 19:56:03 +08008566
8567 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008568}
8569
8570// Outputs the index number in that corresponds to the SSID string
8571INT wifi_getIndexFromName(CHAR *inputSsidString, INT *output_int)
8572{
developer7e4a2a62023-04-06 19:56:03 +08008573 char cmd [128] = {0};
8574 char buf[32] = {0};
8575 char ap_idx = 0;
8576 char *apIndex_str = NULL;
8577 char radio_idx = 0;
8578 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008579 int res;
developerc14d83a2023-06-29 20:09:42 +08008580 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08008581
developere40952c2023-06-15 18:46:43 +08008582 res = snprintf(cmd, sizeof(cmd), "grep -rn ^interface=%s$ /nvram/hostapd*.conf | cut -d '.' -f1 | cut -d 'd' -f2 | tr -d '\\n'",
developer7e4a2a62023-04-06 19:56:03 +08008583 inputSsidString);
developere40952c2023-06-15 18:46:43 +08008584
8585 if (os_snprintf_error(sizeof(cmd), res)) {
8586 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8587 return RETURN_ERR;
8588 }
8589
developer7e4a2a62023-04-06 19:56:03 +08008590 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008591
developer7e4a2a62023-04-06 19:56:03 +08008592 if (strlen(buf)) {
8593 apIndex_str = strtok(buf, "\n");
developerd14dff12023-06-28 22:47:44 +08008594 if (apIndex_str == NULL) {
8595 wifi_debug(DEBUG_ERROR, "strtok fail\n");
8596 return RETURN_ERR;
8597 }
developerc14d83a2023-06-29 20:09:42 +08008598 if (hal_strtol(apIndex_str, 10, &tmp) < 0) {
8599 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08008600 }
developerc14d83a2023-06-29 20:09:42 +08008601 *output_int = tmp;
developer7e4a2a62023-04-06 19:56:03 +08008602 return RETURN_OK;
8603 }
developer72fb0bb2023-01-11 09:46:29 +08008604
developer7e4a2a62023-04-06 19:56:03 +08008605 /* If interface name is not in hostapd config, the caller maybe wifi agent to generate data model.*/
8606 if (strstr(inputSsidString, PREFIX_WIFI6G)) {
8607 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI6G));
8608 radio_idx = 2;
8609 } else if (strstr(inputSsidString, PREFIX_WIFI5G)) {
8610 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI5G));
8611 radio_idx = 1;
8612 } else if (strstr(inputSsidString, PREFIX_WIFI2G)) {
8613 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI2G));
8614 radio_idx = 0;
8615 } else {
8616 printf("%s: hostapd conf not find, unknow inf(%s), return ERROR!!!(%d).\n",
8617 __func__, inputSsidString, ap_idx);
developera3511852023-06-14 14:12:59 +08008618 *output_int = -1;
8619 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08008620 }
8621
8622 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
8623
8624 if (ap_idx >= 0 && ap_idx < MAX_VAP) {
8625 printf("%s: hostapd conf not find, inf(%s), use inf idx(%d).\n",
8626 __func__, inputSsidString, ap_idx);
8627 *output_int = ap_idx;
8628 return RETURN_OK;
8629 }
8630
8631 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008632}
8633
8634INT wifi_getApIndexFromName(CHAR *inputSsidString, INT *output_int)
8635{
developera3511852023-06-14 14:12:59 +08008636 return wifi_getIndexFromName(inputSsidString, output_int);
developer72fb0bb2023-01-11 09:46:29 +08008637}
8638
8639// Outputs a 32 byte or less string indicating the beacon type as "None", "Basic", "WPA", "11i", "WPAand11i"
8640INT wifi_getApBeaconType(INT apIndex, CHAR *output_string)
8641{
developera3511852023-06-14 14:12:59 +08008642 char buf[MAX_BUF_SIZE] = {0};
8643 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008644 int res;
developer72fb0bb2023-01-11 09:46:29 +08008645
developera3511852023-06-14 14:12:59 +08008646 if(NULL == output_string)
8647 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008648
developer32f2a182023-06-27 19:50:41 +08008649 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
8650 if (os_snprintf_error(sizeof(config_file), res)) {
8651 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8652 return RETURN_ERR;
8653 }
developera3511852023-06-14 14:12:59 +08008654 wifi_hostapdRead(config_file, "wpa", buf, sizeof(buf));
8655 if((strcmp(buf,"3")==0))
developere40952c2023-06-15 18:46:43 +08008656 res = snprintf(output_string, 32, "WPAand11i");
developera3511852023-06-14 14:12:59 +08008657 else if((strcmp(buf,"2")==0))
developere40952c2023-06-15 18:46:43 +08008658 res = snprintf(output_string, 32, "11i");
developera3511852023-06-14 14:12:59 +08008659 else if((strcmp(buf,"1")==0))
developere40952c2023-06-15 18:46:43 +08008660 res = snprintf(output_string, 32, "WPA");
developera3511852023-06-14 14:12:59 +08008661 else
developere40952c2023-06-15 18:46:43 +08008662 res = snprintf(output_string, 32, "None");
8663 if (os_snprintf_error(32, res)) {
8664 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8665 return RETURN_ERR;
8666 }
developer72fb0bb2023-01-11 09:46:29 +08008667
developera3511852023-06-14 14:12:59 +08008668 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008669}
8670
8671// Sets the beacon type enviornment variable. Allowed input strings are "None", "Basic", "WPA, "11i", "WPAand11i"
8672INT wifi_setApBeaconType(INT apIndex, CHAR *beaconTypeString)
8673{
developera3511852023-06-14 14:12:59 +08008674 char config_file[MAX_BUF_SIZE] = {0};
8675 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008676 int res;
developer72fb0bb2023-01-11 09:46:29 +08008677
developera3511852023-06-14 14:12:59 +08008678 if (NULL == beaconTypeString)
8679 return RETURN_ERR;
8680 list.name = "wpa";
8681 list.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008682
developera3511852023-06-14 14:12:59 +08008683 if((strcmp(beaconTypeString,"WPAand11i")==0))
8684 list.value="3";
8685 else if((strcmp(beaconTypeString,"11i")==0))
8686 list.value="2";
8687 else if((strcmp(beaconTypeString,"WPA")==0))
8688 list.value="1";
developer72fb0bb2023-01-11 09:46:29 +08008689
developer75bd10c2023-06-27 11:34:08 +08008690 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8691 if (os_snprintf_error(sizeof(config_file), res)) {
8692 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8693 return RETURN_ERR;
8694 }
developera3511852023-06-14 14:12:59 +08008695 wifi_hostapdWrite(config_file, &list, 1);
8696 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8697 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
8698 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008699}
8700
8701// sets the beacon interval on the hardware for this AP
8702INT wifi_setApBeaconInterval(INT apIndex, INT beaconInterval)
8703{
developera3511852023-06-14 14:12:59 +08008704 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8705 struct params params={'\0'};
8706 char buf[MAX_BUF_SIZE] = {'\0'};
8707 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08008708 int res;
developer72fb0bb2023-01-11 09:46:29 +08008709
developera3511852023-06-14 14:12:59 +08008710 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08008711 res = snprintf(buf, sizeof(buf), "%u", beaconInterval);
8712 if (os_snprintf_error(sizeof(buf), res)) {
8713 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8714 return RETURN_ERR;
8715 }
developera3511852023-06-14 14:12:59 +08008716 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08008717
developer75bd10c2023-06-27 11:34:08 +08008718 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8719 if (os_snprintf_error(sizeof(config_file), res)) {
8720 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8721 return RETURN_ERR;
8722 }
developera3511852023-06-14 14:12:59 +08008723 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08008724
developera3511852023-06-14 14:12:59 +08008725 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8726 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8727 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008728}
8729
8730INT wifi_setDTIMInterval(INT apIndex, INT dtimInterval)
8731{
developera3511852023-06-14 14:12:59 +08008732 if (wifi_setApDTIMInterval(apIndex, dtimInterval) != RETURN_OK)
8733 return RETURN_ERR;
8734 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008735}
8736
8737// Get the packet size threshold supported.
8738INT wifi_getApRtsThresholdSupported(INT apIndex, BOOL *output_bool)
8739{
developera3511852023-06-14 14:12:59 +08008740 //save config and apply instantly
8741 if (NULL == output_bool)
8742 return RETURN_ERR;
8743 *output_bool = TRUE;
8744 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008745}
8746
8747// sets the packet size threshold in bytes to apply RTS/CTS backoff rules.
8748INT wifi_setApRtsThreshold(INT apIndex, UINT threshold)
8749{
developera3511852023-06-14 14:12:59 +08008750 char buf[16] = {0};
8751 char config_file[128] = {0};
8752 struct params param = {0};
developere40952c2023-06-15 18:46:43 +08008753 int res;
developer72fb0bb2023-01-11 09:46:29 +08008754
developera3511852023-06-14 14:12:59 +08008755 if (threshold > 65535) {
developer75bd10c2023-06-27 11:34:08 +08008756 wifi_debug(DEBUG_ERROR, "rts threshold %u is too big.\n", threshold);
developera3511852023-06-14 14:12:59 +08008757 return RETURN_ERR;
8758 }
developer72fb0bb2023-01-11 09:46:29 +08008759
developere40952c2023-06-15 18:46:43 +08008760 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8761 if (os_snprintf_error(sizeof(config_file), res)) {
8762 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8763 return RETURN_ERR;
8764 }
8765
8766 res = snprintf(buf, sizeof(buf), "%u", threshold);
8767 if (os_snprintf_error(sizeof(buf), res)) {
8768 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8769 return RETURN_ERR;
8770 }
developera3511852023-06-14 14:12:59 +08008771 param.name = "rts_threshold";
8772 param.value = buf;
8773 wifi_hostapdWrite(config_file, &param, 1);
8774 wifi_hostapdProcessUpdate(apIndex, &param, 1);
8775 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008776
developera3511852023-06-14 14:12:59 +08008777 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008778}
8779
8780// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8781INT wifi_getApWpaEncryptoinMode(INT apIndex, CHAR *output_string)
8782{
developere40952c2023-06-15 18:46:43 +08008783 int res;
8784
developera3511852023-06-14 14:12:59 +08008785 if (NULL == output_string)
8786 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08008787 res = snprintf(output_string, 32, "TKIPandAESEncryption");
8788 if (os_snprintf_error(32, res)) {
8789 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8790 return RETURN_ERR;
8791 }
developera3511852023-06-14 14:12:59 +08008792 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008793
8794}
8795
8796// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8797INT wifi_getApWpaEncryptionMode(INT apIndex, CHAR *output_string)
8798{
developera3511852023-06-14 14:12:59 +08008799 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8800 char *param_name = NULL;
8801 char buf[32] = {0}, config_file[MAX_BUF_SIZE] = {0};
developerc79e9172023-06-06 19:48:03 +08008802 unsigned int len;
developere40952c2023-06-15 18:46:43 +08008803 int res;
developer72fb0bb2023-01-11 09:46:29 +08008804
developera3511852023-06-14 14:12:59 +08008805 if(NULL == output_string)
8806 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008807
developer75bd10c2023-06-27 11:34:08 +08008808 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8809 if (os_snprintf_error(sizeof(config_file), res)) {
8810 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8811 return RETURN_ERR;
8812 }
8813
developera3511852023-06-14 14:12:59 +08008814 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008815
developera3511852023-06-14 14:12:59 +08008816 if(strcmp(buf,"0")==0)
8817 {
8818 printf("%s: wpa_mode is %s ......... \n", __func__, buf);
developere40952c2023-06-15 18:46:43 +08008819 res = snprintf(output_string, 32, "None");
8820 if (os_snprintf_error(32, res)) {
8821 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8822 return RETURN_ERR;
8823 }
developera3511852023-06-14 14:12:59 +08008824 return RETURN_OK;
8825 }
8826 else if((strcmp(buf,"3")==0) || (strcmp(buf,"2")==0))
8827 param_name = "rsn_pairwise";
8828 else if((strcmp(buf,"1")==0))
8829 param_name = "wpa_pairwise";
8830 else
8831 return RETURN_ERR;
8832 memset(output_string,'\0',32);
8833 wifi_hostapdRead(config_file,param_name,output_string,32);
8834 if (strlen(output_string) == 0) { // rsn_pairwise is optional. When it is empty use wpa_pairwise instead.
8835 param_name = "wpa_pairwise";
8836 memset(output_string, '\0', 32);
8837 wifi_hostapdRead(config_file, param_name, output_string, 32);
8838 }
8839 wifi_dbg_printf("\n%s output_string=%s",__func__,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008840
developera3511852023-06-14 14:12:59 +08008841 if(strcmp(output_string,"TKIP CCMP") == 0) {
developerc79e9172023-06-06 19:48:03 +08008842 len = strlen("TKIPandAESEncryption");
8843 memcpy(output_string,"TKIPandAESEncryption", len);
8844 output_string[len] = '\0';
8845 } else if(strcmp(output_string,"TKIP") == 0) {
8846 len = strlen("TKIPEncryption");
8847 memcpy(output_string,"TKIPEncryption", len);
8848 output_string[len] = '\0';
8849 } else if(strcmp(output_string,"CCMP") == 0) {
8850 len = strlen("AESEncryption");
8851 memcpy(output_string,"AESEncryption", len);
8852 output_string[len] = '\0';
8853 }
developer72fb0bb2023-01-11 09:46:29 +08008854
developera3511852023-06-14 14:12:59 +08008855 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8856 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008857}
8858
8859// sets the encyption mode enviornment variable. Valid string format is "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8860INT wifi_setApWpaEncryptionMode(INT apIndex, CHAR *encMode)
8861{
developera3511852023-06-14 14:12:59 +08008862 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8863 struct params params={'\0'};
8864 char output_string[32];
8865 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08008866 int res;
developer72fb0bb2023-01-11 09:46:29 +08008867
developera3511852023-06-14 14:12:59 +08008868 memset(output_string,'\0',32);
8869 wifi_getApBeaconType(apIndex,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008870
developera3511852023-06-14 14:12:59 +08008871 if(strcmp(encMode, "TKIPEncryption") == 0)
8872 params.value = "TKIP";
8873 else if(strcmp(encMode,"AESEncryption") == 0)
8874 params.value = "CCMP";
8875 else if(strcmp(encMode,"TKIPandAESEncryption") == 0)
8876 params.value = "TKIP CCMP";
developer72fb0bb2023-01-11 09:46:29 +08008877
developera3511852023-06-14 14:12:59 +08008878 if((strcmp(output_string,"WPAand11i")==0))
8879 {
8880 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008881 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8882 if (os_snprintf_error(sizeof(config_file), res)) {
8883 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8884 return RETURN_ERR;
8885 }
developera3511852023-06-14 14:12:59 +08008886 wifi_hostapdWrite(config_file, &params, 1);
8887 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008888
developera3511852023-06-14 14:12:59 +08008889 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008890 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8891 if (os_snprintf_error(sizeof(config_file), res)) {
8892 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8893 return RETURN_ERR;
8894 }
developera3511852023-06-14 14:12:59 +08008895 wifi_hostapdWrite(config_file, &params, 1);
8896 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008897
developera3511852023-06-14 14:12:59 +08008898 return RETURN_OK;
8899 }
8900 else if((strcmp(output_string,"11i")==0))
8901 {
8902 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008903 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8904 if (os_snprintf_error(sizeof(config_file), res)) {
8905 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8906 return RETURN_ERR;
8907 }
developera3511852023-06-14 14:12:59 +08008908 wifi_hostapdWrite(config_file, &params, 1);
8909 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8910 return RETURN_OK;
8911 }
8912 else if((strcmp(output_string,"WPA")==0))
8913 {
8914 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008915 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8916 if (os_snprintf_error(sizeof(config_file), res)) {
8917 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8918 return RETURN_ERR;
8919 }
developera3511852023-06-14 14:12:59 +08008920 wifi_hostapdWrite(config_file, &params, 1);
8921 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8922 return RETURN_OK;
8923 }
developer72fb0bb2023-01-11 09:46:29 +08008924
developera3511852023-06-14 14:12:59 +08008925 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8926 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008927}
8928
8929// deletes internal security varable settings for this ap
8930INT wifi_removeApSecVaribles(INT apIndex)
8931{
developer0155a502023-06-19 20:33:57 +08008932 char config_file[MAX_BUF_SIZE] = {0};
8933 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008934 int res;
developer0155a502023-06-19 20:33:57 +08008935
8936 list.name = "wpa";
8937 list.value = "0";
8938
developer75bd10c2023-06-27 11:34:08 +08008939 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8940 if (os_snprintf_error(sizeof(config_file), res)) {
8941 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8942 return RETURN_ERR;
8943 }
developer0155a502023-06-19 20:33:57 +08008944 wifi_hostapdWrite(config_file, &list, 1);
8945
8946 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008947}
8948
8949// changes the hardware settings to disable encryption on this ap
8950INT wifi_disableApEncryption(INT apIndex)
8951{
developer0155a502023-06-19 20:33:57 +08008952 char config_file[MAX_BUF_SIZE] = {0};
8953 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008954 int res;
developer0155a502023-06-19 20:33:57 +08008955
8956 list.name = "wpa";
8957 list.value = "0";
8958
developer75bd10c2023-06-27 11:34:08 +08008959 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8960 if (os_snprintf_error(sizeof(config_file), res)) {
8961 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8962 return RETURN_ERR;
8963 }
developer0155a502023-06-19 20:33:57 +08008964 wifi_hostapdWrite(config_file, &list, 1);
8965 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8966 wifi_reloadAp(apIndex);
8967
8968 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008969}
8970
8971// set the authorization mode on this ap
8972// mode mapping as: 1: open, 2: shared, 4:auto
8973INT wifi_setApAuthMode(INT apIndex, INT mode)
8974{
developera3511852023-06-14 14:12:59 +08008975 struct params params={0};
8976 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08008977 int res;
developer72fb0bb2023-01-11 09:46:29 +08008978
developera3511852023-06-14 14:12:59 +08008979 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008980
developera3511852023-06-14 14:12:59 +08008981 wifi_dbg_printf("\n%s algo_mode=%d", __func__, mode);
8982 params.name = "auth_algs";
developer72fb0bb2023-01-11 09:46:29 +08008983
developere5750452023-05-15 16:46:42 +08008984 if ((mode & 1 && mode & 2) || mode & 4)
developera3511852023-06-14 14:12:59 +08008985 params.value = "3";
8986 else if (mode & 2)
8987 params.value = "2";
8988 else if (mode & 1)
8989 params.value = "1";
8990 else
8991 params.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008992
developer75bd10c2023-06-27 11:34:08 +08008993 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8994 if (os_snprintf_error(sizeof(config_file), res)) {
8995 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8996 return RETURN_ERR;
8997 }
developera3511852023-06-14 14:12:59 +08008998 wifi_hostapdWrite(config_file, &params, 1);
8999 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +08009000 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +08009001 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08009002
developera3511852023-06-14 14:12:59 +08009003 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009004}
9005
9006// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
9007INT wifi_setApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
9008{
developera3511852023-06-14 14:12:59 +08009009 //save to wifi config, and wait for wifi restart to apply
9010 struct params params={'\0'};
9011 char config_file[MAX_BUF_SIZE] = {0};
9012 int ret;
developer72fb0bb2023-01-11 09:46:29 +08009013
developera3511852023-06-14 14:12:59 +08009014 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9015 if(authMode == NULL)
9016 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009017
developera3511852023-06-14 14:12:59 +08009018 wifi_dbg_printf("\n%s AuthMode=%s",__func__,authMode);
9019 params.name = "wpa_key_mgmt";
developer72fb0bb2023-01-11 09:46:29 +08009020
developera3511852023-06-14 14:12:59 +08009021 if((strcmp(authMode,"PSKAuthentication") == 0) || (strcmp(authMode,"SharedAuthentication") == 0))
9022 params.value = "WPA-PSK";
9023 else if(strcmp(authMode,"EAPAuthentication") == 0)
9024 params.value = "WPA-EAP";
9025 else if (strcmp(authMode, "SAEAuthentication") == 0)
9026 params.value = "SAE";
9027 else if (strcmp(authMode, "EAP_192-bit_Authentication") == 0)
9028 params.value = "WPA-EAP-SUITE-B-192";
9029 else if (strcmp(authMode, "PSK-SAEAuthentication") == 0)
9030 params.value = "WPA-PSK WPA-PSK-SHA256 SAE";
9031 else if (strcmp(authMode, "Enhanced_Open") == 0)
9032 params.value = "OWE";
9033 else if(strcmp(authMode,"None") == 0) //Donot change in case the authMode is None
9034 return RETURN_OK; //This is taken careof in beaconType
developer72fb0bb2023-01-11 09:46:29 +08009035
developer32f2a182023-06-27 19:50:41 +08009036 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
9037 if (os_snprintf_error(sizeof(config_file), ret)) {
9038 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9039 return RETURN_ERR;
9040 }
developera3511852023-06-14 14:12:59 +08009041 ret=wifi_hostapdWrite(config_file,&params,1);
9042 if(!ret)
9043 ret=wifi_hostapdProcessUpdate(apIndex, &params, 1);
9044 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08009045
developera3511852023-06-14 14:12:59 +08009046 return ret;
developer72fb0bb2023-01-11 09:46:29 +08009047}
9048
9049// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
9050INT wifi_getApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
9051{
developera3511852023-06-14 14:12:59 +08009052 //save to wifi config, and wait for wifi restart to apply
9053 char BeaconType[50] = {0};
9054 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08009055 int res;
developer32f2a182023-06-27 19:50:41 +08009056 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08009057
developera3511852023-06-14 14:12:59 +08009058 *authMode = 0;
9059 wifi_getApBeaconType(apIndex,BeaconType);
9060 printf("%s____%s \n",__FUNCTION__,BeaconType);
developer72fb0bb2023-01-11 09:46:29 +08009061
developer32f2a182023-06-27 19:50:41 +08009062 if(strcmp(BeaconType,"None") == 0) {
9063 memcpy(authMode, "None", 4);
9064 authMode[4] = '\0';
9065 } else {
9066 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
developer75bd10c2023-06-27 11:34:08 +08009067 if (os_snprintf_error(sizeof(config_file), res)) {
9068 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9069 return RETURN_ERR;
9070 }
developera3511852023-06-14 14:12:59 +08009071 wifi_hostapdRead(config_file, "wpa_key_mgmt", authMode, 32);
9072 wifi_dbg_printf("\n[%s]: AuthMode Name is : %s",__func__,authMode);
developer32f2a182023-06-27 19:50:41 +08009073 if(strcmp(authMode,"WPA-PSK") == 0) {
9074 len = strlen("SharedAuthentication");
9075 memcpy(authMode, "SharedAuthentication", len);
9076 authMode[len] = '\0';
9077 } else if(strcmp(authMode,"WPA-EAP") == 0) {
9078 len = strlen("EAPAuthentication");
9079 memcpy(authMode, "EAPAuthentication", len);
9080 authMode[len] = '\0';
9081 }
developera3511852023-06-14 14:12:59 +08009082 }
developer72fb0bb2023-01-11 09:46:29 +08009083
developera3511852023-06-14 14:12:59 +08009084 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009085}
9086
9087// Outputs the number of stations associated per AP
9088INT wifi_getApNumDevicesAssociated(INT apIndex, ULONG *output_ulong)
9089{
developera3511852023-06-14 14:12:59 +08009090 char interface_name[16] = {0};
9091 char cmd[128]={0};
9092 char buf[128]={0};
9093 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +08009094 int res;
developer72fb0bb2023-01-11 09:46:29 +08009095
developera3511852023-06-14 14:12:59 +08009096 if(apIndex > MAX_APS)
9097 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009098
developera3511852023-06-14 14:12:59 +08009099 wifi_getApEnable(apIndex,&status);
9100 if (!status)
9101 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009102
developera3511852023-06-14 14:12:59 +08009103 //sprintf(cmd, "iw dev %s station dump | grep Station | wc -l", interface_name);//alternate method
9104 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9105 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08009106 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta | wc -l", interface_name);
9107 if (os_snprintf_error(sizeof(cmd), res)) {
9108 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9109 return RETURN_ERR;
9110 }
developera3511852023-06-14 14:12:59 +08009111 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08009112 if (sscanf(buf,"%lu", output_ulong) != 1) {
9113 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
9114 return RETURN_ERR;
9115 }
developer72fb0bb2023-01-11 09:46:29 +08009116
developera3511852023-06-14 14:12:59 +08009117 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009118}
9119
9120// manually removes any active wi-fi association with the device specified on this ap
9121INT wifi_kickApAssociatedDevice(INT apIndex, CHAR *client_mac)
9122{
developera3511852023-06-14 14:12:59 +08009123 char inf_name[16] = {0};
9124 char cmd[MAX_CMD_SIZE] = {0};
9125 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08009126 int res;
developer72fb0bb2023-01-11 09:46:29 +08009127
developera3511852023-06-14 14:12:59 +08009128 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9129 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08009130
9131 res = snprintf(cmd, sizeof(cmd),"hostapd_cli -i %s disassociate %s", inf_name, client_mac);
9132 if (os_snprintf_error(sizeof(cmd), res)) {
9133 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9134 return RETURN_ERR;
9135 }
developer7e4a2a62023-04-06 19:56:03 +08009136
developera3511852023-06-14 14:12:59 +08009137 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08009138
developera3511852023-06-14 14:12:59 +08009139 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009140}
9141
9142// outputs the radio index for the specified ap. similar as wifi_getSsidRadioIndex
9143INT wifi_getApRadioIndex(INT apIndex, INT *output_int)
9144{
developer7e4a2a62023-04-06 19:56:03 +08009145 int max_radio_num = 0;
9146
9147 if(NULL == output_int)
9148 return RETURN_ERR;
9149
9150 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08009151 if(max_radio_num == 0){
9152 return RETURN_ERR;
9153 }
developer7e4a2a62023-04-06 19:56:03 +08009154 *output_int = apIndex % max_radio_num;
9155
9156 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009157}
9158
9159// sets the radio index for the specific ap
9160INT wifi_setApRadioIndex(INT apIndex, INT radioIndex)
9161{
developera3511852023-06-14 14:12:59 +08009162 //set to config only and wait for wifi reset to apply settings
9163 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009164}
9165
developer0155a502023-06-19 20:33:57 +08009166int mtk_get_ap_metrics(struct nl_msg *msg, void *cb)
9167{
9168 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9169 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009170 struct genlmsghdr *gnlh;
developer0155a502023-06-19 20:33:57 +08009171 wdev_ap_metric ap_metric;
9172 wdev_ap_metric *p_ap_metric = &ap_metric;
9173 int err = 0;
9174 struct mtk_nl80211_cb_data *cb_data = cb;
9175
9176 if (!msg || !cb_data) {
developerc14d83a2023-06-29 20:09:42 +08009177 wifi_debug(DEBUG_ERROR, "msgor cb_data is null,error.\n");
developer0155a502023-06-19 20:33:57 +08009178 return NL_SKIP;
9179 }
developerc14d83a2023-06-29 20:09:42 +08009180 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer0155a502023-06-19 20:33:57 +08009181
9182 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9183 genlmsg_attrlen(gnlh, 0), NULL);
9184 if (err < 0) {
9185 wifi_debug(DEBUG_ERROR, "nla_parse ap_metrics nl80211 msg fails,error.\n");
9186 return err;
9187 }
9188
9189 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9190 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX,
9191 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9192 if (err < 0) {
9193 wifi_debug(DEBUG_ERROR, "GET_STATISTIC_MAX fails,error.\n");
9194 return err;
9195 }
9196
9197 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]) {
9198 p_ap_metric = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]);
9199 if (p_ap_metric) {
9200 memcpy(cb_data->out_buf , &p_ap_metric->cu, sizeof(unsigned char));
9201 }
9202 }
9203 }
9204
9205 return NL_OK;
9206}
9207
developer121a8e72023-05-22 09:19:39 +08009208
9209#define MAX_ACL_DUMP_LEN 4096
9210int mtk_acl_list_dump_callback(struct nl_msg *msg, void *cb)
9211{
9212 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9213 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009214 struct genlmsghdr *gnlh;
developer121a8e72023-05-22 09:19:39 +08009215 char *show_str = NULL;
developer2edaf012023-05-24 14:24:53 +08009216 int err = 0;
developer121a8e72023-05-22 09:19:39 +08009217 unsigned short acl_result_len = 0;
9218 struct mtk_nl80211_cb_data *cb_data = cb;
developer121a8e72023-05-22 09:19:39 +08009219 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08009220 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developer121a8e72023-05-22 09:19:39 +08009221 return NL_SKIP;
9222 }
developerc14d83a2023-06-29 20:09:42 +08009223
9224 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer121a8e72023-05-22 09:19:39 +08009225 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9226 genlmsg_attrlen(gnlh, 0), NULL);
9227 if (err < 0) {
developer2edaf012023-05-24 14:24:53 +08009228 wifi_debug(DEBUG_ERROR, "nla_parse acl list nl80211 msg fails,error.\n");
developer121a8e72023-05-22 09:19:39 +08009229 return NL_SKIP;
9230 }
developer121a8e72023-05-22 09:19:39 +08009231 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9232 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX,
9233 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9234 if (err < 0)
9235 return NL_SKIP;
9236 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]) {
9237 acl_result_len = nla_len(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9238 show_str = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9239 if (acl_result_len > MAX_ACL_DUMP_LEN) {
9240 wifi_debug(DEBUG_ERROR,"the scan result len is invalid !!!\n");
9241 return NL_SKIP;
9242 } else if (*(show_str + acl_result_len - 1) != '\0') {
9243 wifi_debug(DEBUG_INFO, "the result string is not ended with right terminator, handle it!!!\n");
9244 *(show_str + acl_result_len - 1) = '\0';
9245 }
9246 wifi_debug(DEBUG_INFO, "driver msg:%s\n", show_str);
developer2edaf012023-05-24 14:24:53 +08009247
9248 if (cb_data->out_len >= acl_result_len) {
9249 memset(cb_data->out_buf, 0, cb_data->out_len);
9250 /*skip the first line: 'policy=1\n' to find the acl mac addrs*/
9251 memmove(cb_data->out_buf, show_str, acl_result_len);
9252 wifi_debug(DEBUG_INFO, "out buff:%s\n", cb_data->out_buf);
9253 } else {
9254 memset(cb_data->out_buf, 0, cb_data->out_len);
developer121a8e72023-05-22 09:19:39 +08009255 }
developer121a8e72023-05-22 09:19:39 +08009256 } else
9257 wifi_debug(DEBUG_ERROR, "no acl result attr\n");
9258 } else
9259 wifi_debug(DEBUG_ERROR, "no any acl result from driver\n");
9260 return NL_OK;
9261}
developer72fb0bb2023-01-11 09:46:29 +08009262// Get the ACL MAC list per AP
developer2edaf012023-05-24 14:24:53 +08009263INT mtk_wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
developer72fb0bb2023-01-11 09:46:29 +08009264{
developer7e4a2a62023-04-06 19:56:03 +08009265 char inf_name[IF_NAME_SIZE] = {0};
developer121a8e72023-05-22 09:19:39 +08009266 unsigned int if_idx = 0;
9267 int ret = -1;
9268 struct unl unl_ins;
9269 struct nl_msg *msg = NULL;
9270 struct nlattr * msg_data = NULL;
9271 struct mtk_nl80211_param param;
9272 struct mtk_nl80211_cb_data cb_data;
developer7e4a2a62023-04-06 19:56:03 +08009273 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9274 return RETURN_ERR;
developer121a8e72023-05-22 09:19:39 +08009275 if_idx = if_nametoindex(inf_name);
9276 if (!if_idx) {
developer2edaf012023-05-24 14:24:53 +08009277 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
developer121a8e72023-05-22 09:19:39 +08009278 return RETURN_ERR;
9279 }
9280 /*init mtk nl80211 vendor cmd*/
9281 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9282 param.if_type = NL80211_ATTR_IFINDEX;
9283 param.if_idx = if_idx;
9284
9285 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9286 if (ret) {
9287 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9288 return RETURN_ERR;
9289 }
developer121a8e72023-05-22 09:19:39 +08009290 /*add mtk vendor cmd data*/
9291 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_SHOW_ALL)) {
developer2edaf012023-05-24 14:24:53 +08009292 wifi_debug(DEBUG_ERROR, "Nla put ACL_SHOW_ALL attribute error\n");
developer121a8e72023-05-22 09:19:39 +08009293 nlmsg_free(msg);
9294 goto err;
9295 }
developer72fb0bb2023-01-11 09:46:29 +08009296
developer121a8e72023-05-22 09:19:39 +08009297 /*send mtk nl80211 vendor msg*/
9298 cb_data.out_buf = macArray;
9299 cb_data.out_len = buf_size;
9300
9301 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_acl_list_dump_callback, &cb_data);
9302 if (ret) {
9303 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9304 goto err;
9305 }
9306 /*deinit mtk nl80211 vendor msg*/
9307 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009308 wifi_debug(DEBUG_NOTICE,"send cmd success, get out_buf:%s\n", macArray);
developera3511852023-06-14 14:12:59 +08009309 return RETURN_OK;
developer121a8e72023-05-22 09:19:39 +08009310err:
9311 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009312 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developer121a8e72023-05-22 09:19:39 +08009313 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009314}
9315
developer2edaf012023-05-24 14:24:53 +08009316INT wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9317{
9318 char *mac_arry_buf = NULL;
9319
9320 mac_arry_buf = malloc(buf_size);
9321 if (!mac_arry_buf) {
9322 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
9323 return RETURN_ERR;
9324 }
9325 memset(mac_arry_buf, 0, buf_size);
9326 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
9327 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
9328 free(mac_arry_buf);
9329 mac_arry_buf = NULL;
9330 return RETURN_ERR;
9331 }
9332 /*
9333 mtk format to wifi hal format:
9334 "policy=1
9335 00:11:22:33:44:55
9336 00:11:22:33:44:66
9337 "
9338 -->
9339 "00:11:22:33:44:55
9340 00:11:22:33:44:66
9341 "
9342 */
9343 memset(macArray, 0, buf_size);
9344 if (*mac_arry_buf != '\0' && strchr(mac_arry_buf,'\n')) {
9345 memmove(macArray, strchr(mac_arry_buf,'\n')+1, strlen(strchr(mac_arry_buf,'\n')+1)+1);
9346 wifi_debug(DEBUG_NOTICE,"macArray:\n%s\n", macArray);
9347 }
9348 free(mac_arry_buf);
9349 mac_arry_buf = NULL;
9350 return RETURN_OK;
9351}
9352
developer72fb0bb2023-01-11 09:46:29 +08009353INT wifi_getApDenyAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9354{
developer72fb0bb2023-01-11 09:46:29 +08009355
developer7e4a2a62023-04-06 19:56:03 +08009356 wifi_getApAclDevices(apIndex, macArray, buf_size);
developer72fb0bb2023-01-11 09:46:29 +08009357
developera3511852023-06-14 14:12:59 +08009358 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009359}
9360
9361
9362// Get the list of stations associated per AP
9363INT wifi_getApDevicesAssociated(INT apIndex, CHAR *macArray, UINT buf_size)
9364{
developer7e4a2a62023-04-06 19:56:03 +08009365 char interface_name[IF_NAME_SIZE] = {0};
9366 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08009367 int res;
developer72fb0bb2023-01-11 09:46:29 +08009368
developer7e4a2a62023-04-06 19:56:03 +08009369 if(apIndex > 3) //Currently supporting apIndex upto 3
developera3511852023-06-14 14:12:59 +08009370 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009371
developer7e4a2a62023-04-06 19:56:03 +08009372 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08009373 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009374
developere40952c2023-06-15 18:46:43 +08009375 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta", interface_name);
9376 if (os_snprintf_error(sizeof(cmd), res)) {
9377 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9378 return RETURN_ERR;
9379 }
developer7e4a2a62023-04-06 19:56:03 +08009380 _syscmd(cmd, macArray, buf_size);
9381 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009382}
9383
developer8dd72532023-05-17 19:58:35 +08009384int hex2num(char c)
9385{
9386 if (c >= '0' && c <= '9')
9387 return c - '0';
9388 if (c >= 'a' && c <= 'f')
9389 return c - 'a' + 10;
9390 if (c >= 'A' && c <= 'F')
9391 return c - 'A' + 10;
9392 return -1;
9393}
9394
9395/**
9396 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
9397 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
9398 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
9399 * Returns: Characters used (> 0) on success, -1 on failure
9400 */
9401int hwaddr_aton2(const char *txt, unsigned char *addr)
9402{
9403 int i;
9404 const char *pos = txt;
9405
9406 for (i = 0; i < 6; i++) {
9407 int a, b;
9408
9409 while (*pos == ':' || *pos == '.' || *pos == '-')
9410 pos++;
9411
9412 a = hex2num(*pos++);
9413 if (a < 0)
9414 return -1;
9415 b = hex2num(*pos++);
9416 if (b < 0)
9417 return -1;
9418 *addr++ = (a << 4) | b;
9419 }
9420
9421 return pos - txt;
9422}
9423
developer72fb0bb2023-01-11 09:46:29 +08009424// adds the mac address to the filter list
9425//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9426INT wifi_addApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9427{
developer7e4a2a62023-04-06 19:56:03 +08009428 char inf_name[IF_NAME_SIZE] = {0};
developer8dd72532023-05-17 19:58:35 +08009429 int if_idx, ret = 0;
developer49b17232023-05-19 16:35:19 +08009430 struct nl_msg *msg = NULL;
9431 struct nlattr * msg_data = NULL;
9432 struct mtk_nl80211_param param;
developer8dd72532023-05-17 19:58:35 +08009433 unsigned char mac[ETH_ALEN] = {0x00, 0x0c, 0x43, 0x11, 0x22, 0x33};
9434 struct unl unl_ins;
developer7e4a2a62023-04-06 19:56:03 +08009435 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9436 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009437 if (!DeviceMacAddress)
9438 return RETURN_ERR;
developer8dd72532023-05-17 19:58:35 +08009439 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
developer2edaf012023-05-24 14:24:53 +08009440 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
developer8dd72532023-05-17 19:58:35 +08009441 return RETURN_ERR;
9442 }
developer8dd72532023-05-17 19:58:35 +08009443 if_idx = if_nametoindex(inf_name);
developer2edaf012023-05-24 14:24:53 +08009444 if (!if_idx) {
9445 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9446 return RETURN_ERR;
9447 }
developer49b17232023-05-19 16:35:19 +08009448 /*init mtk nl80211 vendor cmd*/
9449 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9450 param.if_type = NL80211_ATTR_IFINDEX;
9451 param.if_idx = if_idx;
9452 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9453 if (ret) {
9454 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009455 return RETURN_ERR;
9456 }
developer49b17232023-05-19 16:35:19 +08009457 /*add mtk vendor cmd data*/
9458 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_ADD_MAC, ETH_ALEN, mac)) {
developer2edaf012023-05-24 14:24:53 +08009459 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
developer8dd72532023-05-17 19:58:35 +08009460 nlmsg_free(msg);
9461 goto err;
9462 }
developer49b17232023-05-19 16:35:19 +08009463 /*send mtk nl80211 vendor msg*/
9464 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9465 if (ret) {
9466 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009467 goto err;
9468 }
developer49b17232023-05-19 16:35:19 +08009469 /*deinit mtk nl80211 vendor msg*/
9470 mtk_nl80211_deint(&unl_ins);
9471 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer8dd72532023-05-17 19:58:35 +08009472 return RETURN_OK;
9473err:
developer49b17232023-05-19 16:35:19 +08009474 mtk_nl80211_deint(&unl_ins);
9475 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developer8dd72532023-05-17 19:58:35 +08009476 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009477}
9478
9479// deletes the mac address from the filter list
9480//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9481INT wifi_delApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9482{
developer2edaf012023-05-24 14:24:53 +08009483 struct unl unl_ins;
9484 int if_idx = 0, ret = 0;
9485 struct nl_msg *msg = NULL;
9486 struct nlattr * msg_data = NULL;
9487 struct mtk_nl80211_param param;
developer7e4a2a62023-04-06 19:56:03 +08009488 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +08009489 unsigned char mac[ETH_ALEN] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009490
developer7e4a2a62023-04-06 19:56:03 +08009491 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9492 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009493
developer7e4a2a62023-04-06 19:56:03 +08009494 if (!DeviceMacAddress)
9495 return RETURN_ERR;
9496
developer2edaf012023-05-24 14:24:53 +08009497 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
9498 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
9499 return RETURN_ERR;
9500 }
developer72fb0bb2023-01-11 09:46:29 +08009501
developer2edaf012023-05-24 14:24:53 +08009502 if_idx = if_nametoindex(inf_name);
9503 if (!if_idx) {
9504 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9505 return RETURN_ERR;
9506 }
9507 /*init mtk nl80211 vendor cmd*/
9508 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9509 param.if_type = NL80211_ATTR_IFINDEX;
9510 param.if_idx = if_idx;
9511 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9512 if (ret) {
9513 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9514 return RETURN_ERR;
9515 }
9516 /*add mtk vendor cmd data*/
9517 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_DEL_MAC, ETH_ALEN, mac)) {
9518 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9519 nlmsg_free(msg);
9520 goto err;
9521 }
9522 /*send mtk nl80211 vendor msg*/
9523 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9524 if (ret) {
9525 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9526 goto err;
9527 }
9528 /*deinit mtk nl80211 vendor msg*/
9529 mtk_nl80211_deint(&unl_ins);
9530 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
9531 return RETURN_OK;
9532err:
9533 mtk_nl80211_deint(&unl_ins);
9534 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9535 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009536}
9537
9538// outputs the number of devices in the filter list
9539INT wifi_getApAclDeviceNum(INT apIndex, UINT *output_uint)
9540{
developer2edaf012023-05-24 14:24:53 +08009541 char *mac_arry = NULL, *ptr = NULL, mac_str[18] = {0};
9542 UINT buf_size = 1024;
9543 UINT sta_num = 0;
9544 unsigned char mac[ETH_ALEN] = {0};
developera3511852023-06-14 14:12:59 +08009545 if(output_uint == NULL)
developerdaf24792023-06-06 11:40:04 +08009546 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009547
developer2edaf012023-05-24 14:24:53 +08009548 mac_arry = (char *)malloc(buf_size);
9549 if (mac_arry == NULL) {
9550 wifi_debug(DEBUG_ERROR, "malloc mac_arry fails\n");
developer7e4a2a62023-04-06 19:56:03 +08009551 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009552 }
developerdaf24792023-06-06 11:40:04 +08009553 memset(mac_arry, 0, buf_size);
developer2edaf012023-05-24 14:24:53 +08009554 /*mac_arry str format: 00:11:22:33:44:55\n00:11:22:33:44:66\0*/
9555 if (wifi_getApAclDevices(apIndex, mac_arry, buf_size)!= RETURN_OK) {
9556 wifi_debug(DEBUG_ERROR, "get acl list entries fails\n");
developer9ce44382023-06-28 11:09:37 +08009557 free(mac_arry);
developer2edaf012023-05-24 14:24:53 +08009558 return RETURN_ERR;
9559 }
9560 /*count the acl str nums:*/
9561 wifi_debug(DEBUG_NOTICE, "mac_arry: %s\n", mac_arry);
developer7e4a2a62023-04-06 19:56:03 +08009562
developer2edaf012023-05-24 14:24:53 +08009563 /*mac addr string format:
9564 exp1: 00:11:22:33:44:55\0
9565 exp2: 00:11:22:33:44:55\n00:11:22:33:44:66\0
9566 */
9567 ptr = mac_arry;
9568 while (sscanf(ptr, "%17s", mac_str) == 1) {
9569 if (hwaddr_aton2(mac_str, mac) >= 0)
9570 sta_num++;
9571 ptr = strstr(ptr, mac_str) + strlen(mac_str);
9572 }
9573 *output_uint = sta_num;
9574 wifi_debug(DEBUG_NOTICE, "output_uint: %d\n", *output_uint);
9575 free(mac_arry);
9576 mac_arry = NULL;
developer7e4a2a62023-04-06 19:56:03 +08009577 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009578}
9579
9580INT apply_rules(INT apIndex, CHAR *client_mac,CHAR *action,CHAR *interface)
9581{
developer75bd10c2023-06-27 11:34:08 +08009582 int res;
developer72fb0bb2023-01-11 09:46:29 +08009583
developera3511852023-06-14 14:12:59 +08009584 if(strcmp(action,"DENY")==0)
9585 {
developer33f13ba2023-07-12 16:19:06 +08009586 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 +08009587 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009588 if (res) {
9589 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009590 return RETURN_ERR;
9591 }
developera3511852023-06-14 14:12:59 +08009592 return RETURN_OK;
9593 }
developer72fb0bb2023-01-11 09:46:29 +08009594
developera3511852023-06-14 14:12:59 +08009595 if(strcmp(action,"ALLOW")==0)
9596 {
developer33f13ba2023-07-12 16:19:06 +08009597 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 +08009598 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009599 if (res) {
9600 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009601 return RETURN_ERR;
9602 }
developera3511852023-06-14 14:12:59 +08009603 return RETURN_OK;
9604 }
developer72fb0bb2023-01-11 09:46:29 +08009605
developera3511852023-06-14 14:12:59 +08009606 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009607
9608}
9609
9610// enable kick for devices on acl black list
9611INT wifi_kickApAclAssociatedDevices(INT apIndex, BOOL enable)
9612{
developera3511852023-06-14 14:12:59 +08009613 char aclArray[MAX_BUF_SIZE] = {0}, *acl = NULL;
9614 char assocArray[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009615
developera3511852023-06-14 14:12:59 +08009616 wifi_getApDenyAclDevices(apIndex, aclArray, sizeof(aclArray));
9617 wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray));
developer72fb0bb2023-01-11 09:46:29 +08009618
developera3511852023-06-14 14:12:59 +08009619 /* if there are no devices connected there is nothing to do */
9620 if (strlen(assocArray) < 17)
9621 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009622
developera3511852023-06-14 14:12:59 +08009623 if (enable == TRUE) {
9624 /* kick off the MAC which is in ACL array (deny list) */
9625 acl = strtok(aclArray, "\n");
9626 while (acl != NULL) {
9627 if (strlen(acl) >= 17 && strcasestr(assocArray, acl))
9628 wifi_kickApAssociatedDevice(apIndex, acl);
developer72fb0bb2023-01-11 09:46:29 +08009629
developera3511852023-06-14 14:12:59 +08009630 acl = strtok(NULL, "\n");
9631 }
developer72fb0bb2023-01-11 09:46:29 +08009632 wifi_setApMacAddressControlMode(apIndex, 2);
developera3511852023-06-14 14:12:59 +08009633 } else
developer72fb0bb2023-01-11 09:46:29 +08009634 wifi_setApMacAddressControlMode(apIndex, 0);
developer72fb0bb2023-01-11 09:46:29 +08009635
developera3511852023-06-14 14:12:59 +08009636 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009637}
9638
9639INT wifi_setPreferPrivateConnection(BOOL enable)
9640{
developera3511852023-06-14 14:12:59 +08009641 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009642}
9643
9644// sets the mac address filter control mode. 0 == filter disabled, 1 == filter as whitelist, 2 == filter as blacklist
9645INT wifi_setApMacAddressControlMode(INT apIndex, INT filterMode)
9646{
developer2edaf012023-05-24 14:24:53 +08009647 int if_idx = 0, ret = 0;
9648 struct unl unl_ins;
9649 struct nl_msg *msg = NULL;
9650 struct nlattr * msg_data = NULL;
9651 struct mtk_nl80211_param param;
9652 int acl_policy = -1;
developer7e4a2a62023-04-06 19:56:03 +08009653 char inf_name[IF_NAME_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009654
developer7e4a2a62023-04-06 19:56:03 +08009655 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9656 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009657 if_idx = if_nametoindex(inf_name);
9658 if (!if_idx) {
9659 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9660 return RETURN_ERR;
9661 }
9662 /*init mtk nl80211 vendor cmd*/
9663 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9664 param.if_type = NL80211_ATTR_IFINDEX;
9665 param.if_idx = if_idx;
9666 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9667 if (ret) {
9668 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9669 return RETURN_ERR;
9670 }
9671 /*add mtk vendor cmd data*/
9672 if (filterMode == 0) {
9673 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_DISABLE;
9674 } else if (filterMode == 1) {
9675 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_WHITE_LIST;
9676 } else if (filterMode == 2) {
9677 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_BLACK_LIST;
9678 } else {
9679 wifi_debug(DEBUG_ERROR, "filtermode(%d) not support error\n", filterMode);
9680 nlmsg_free(msg);
9681 goto err;
9682 }
9683 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, acl_policy)) {
9684 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9685 nlmsg_free(msg);
9686 goto err;
9687 }
9688 /*send mtk nl80211 vendor msg*/
9689 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9690 if (ret) {
9691 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9692 goto err;
9693 }
9694 /*deinit mtk nl80211 vendor msg*/
9695 mtk_nl80211_deint(&unl_ins);
9696 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer7e4a2a62023-04-06 19:56:03 +08009697 return RETURN_OK;
developer2edaf012023-05-24 14:24:53 +08009698err:
9699 mtk_nl80211_deint(&unl_ins);
9700 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9701 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009702}
9703
9704// 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.
9705INT wifi_setApVlanEnable(INT apIndex, BOOL VlanEnabled)
9706{
developera3511852023-06-14 14:12:59 +08009707 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009708}
9709
9710// gets the vlan ID for this ap from an internal enviornment variable
9711INT wifi_getApVlanID(INT apIndex, INT *output_int)
9712{
developera3511852023-06-14 14:12:59 +08009713 if(apIndex==0)
9714 {
9715 *output_int=100;
9716 return RETURN_OK;
9717 }
developer72fb0bb2023-01-11 09:46:29 +08009718
developera3511852023-06-14 14:12:59 +08009719 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009720}
9721
9722// sets the vlan ID for this ap to an internal enviornment variable
9723INT wifi_setApVlanID(INT apIndex, INT vlanId)
9724{
developera3511852023-06-14 14:12:59 +08009725 //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 +08009726 char interface_name[16] = {0};
9727 int if_idx, ret = 0;
9728 struct nl_msg *msg = NULL;
9729 struct nlattr * msg_data = NULL;
9730 struct mtk_nl80211_param param;
9731 struct unl unl_ins;
9732
9733 if (apIndex > MAX_APS) {
9734 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9735 return RETURN_ERR;
9736 }
9737 if (vlanId > 4095 || vlanId < 1) {
9738 wifi_debug(DEBUG_ERROR, "Invalid vlanId %d\n", vlanId);
9739 return RETURN_ERR;
9740 }
9741 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9742 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9743 return RETURN_ERR;
9744 /*step 1. mwctl dev %s set vlan_tag 0*/
9745 if_idx = if_nametoindex(interface_name);
9746 /*init mtk nl80211 vendor cmd*/
9747 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9748 param.if_type = NL80211_ATTR_IFINDEX;
9749 param.if_idx = if_idx;
9750 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9751 if (ret) {
9752 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9753 return RETURN_ERR;
9754 }
9755 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, vlanId)) {
9756 printf("Nla put attribute error\n");
9757 nlmsg_free(msg);
9758 goto err;
9759 }
9760 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9761 if (ret) {
9762 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9763 goto err;
9764 }
9765 mtk_nl80211_deint(&unl_ins);
9766 //wifi_debug(DEBUG_NOTICE, "set vlanId cmd success.\n", vlanId);
9767 printf("set vlanId=%d cmd success.\n", vlanId);
9768 return RETURN_OK;
9769err:
9770 mtk_nl80211_deint(&unl_ins);
9771 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08009772 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009773}
9774
developercc5cbfb2023-06-13 18:29:52 +08009775char br_name[IFNAMSIZ] = "brlan0";
9776
developer72fb0bb2023-01-11 09:46:29 +08009777// gets bridgeName, IP address and Subnet. bridgeName is a maximum of 32 characters,
9778INT wifi_getApBridgeInfo(INT index, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9779{
developercc5cbfb2023-06-13 18:29:52 +08009780 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9781 struct ifreq ifr;
9782 struct sockaddr_in *sin;
9783
9784 memcpy(bridgeName, br_name, strlen(br_name));
9785
9786 if (sock == -1) {
9787 wifi_debug(DEBUG_ERROR, "socket failed");
9788 return RETURN_ERR;
9789 }
9790
developerd14dff12023-06-28 22:47:44 +08009791 strncpy(ifr.ifr_name, br_name, strlen(br_name));
developercc5cbfb2023-06-13 18:29:52 +08009792 ifr.ifr_addr.sa_family = AF_INET;
9793 if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
9794 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFADDR) failed, %s, bridge_name=%s\n",
9795 strerror(errno), br_name);
developer9ce44382023-06-28 11:09:37 +08009796 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009797 return RETURN_ERR;
9798 }
9799
9800 sin = (struct sockaddr_in *)&ifr.ifr_addr;
9801 wifi_debug(DEBUG_ERROR, "Bridge device %s has IP address: %s\n", br_name, inet_ntoa(sin->sin_addr));
9802 memcpy(IP, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9803
9804 if (ioctl(sock, SIOCGIFNETMASK, &ifr) < 0) {
9805 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFNETMASK) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009806 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009807 return RETURN_ERR;
9808 }
9809
9810 wifi_debug(DEBUG_ERROR, "Bridge device %s has subnet mask: %s\n", br_name, inet_ntoa(sin->sin_addr));
9811 memcpy(subnet, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9812 close(sock);
developer72fb0bb2023-01-11 09:46:29 +08009813
developera3511852023-06-14 14:12:59 +08009814 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009815}
9816
9817//sets bridgeName, IP address and Subnet to internal enviornment variables. bridgeName is a maximum of 32 characters
9818INT wifi_setApBridgeInfo(INT apIndex, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9819{
developera3511852023-06-14 14:12:59 +08009820 //save settings, wait for wifi reset or wifi_pushBridgeInfo to apply.
developercc5cbfb2023-06-13 18:29:52 +08009821 struct ifreq ifr;
9822 struct sockaddr_in sin;
9823 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9824
developerc14d83a2023-06-29 20:09:42 +08009825 if(sock < 0) {
9826 wifi_debug(DEBUG_ERROR, "sock init fail\n");
9827 return RETURN_ERR;
9828 }
developer5b23cd02023-07-19 20:26:03 +08009829
developercc5cbfb2023-06-13 18:29:52 +08009830 if (strlen(bridgeName) >= IFNAMSIZ) {
9831 wifi_debug(DEBUG_ERROR, "invalide bridgeName length=%ld\n", strlen(bridgeName));
developer9ce44382023-06-28 11:09:37 +08009832 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009833 return RETURN_ERR;
9834 }
9835
9836 if (strlen(br_name) >= IFNAMSIZ) {
9837 wifi_debug(DEBUG_ERROR, "invalide br_name length=%ld in strorage\n", strlen(br_name));
developer9ce44382023-06-28 11:09:37 +08009838 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009839 return RETURN_ERR;
9840 }
9841
9842 if (sock == -1) {
developera3511852023-06-14 14:12:59 +08009843 wifi_debug(DEBUG_ERROR, "socket failed");
developercc5cbfb2023-06-13 18:29:52 +08009844 return RETURN_ERR;
9845 }
9846
9847 memset(&ifr, 0, sizeof(ifr));
9848 strncpy(ifr.ifr_name, br_name, strlen(br_name));
9849 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9850 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009851 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009852 return RETURN_ERR;
9853 }
9854
9855 ifr.ifr_flags = (short)(ifr.ifr_flags & ~IFF_UP);
9856 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9857 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009858 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009859 return RETURN_ERR;
9860 }
9861
9862 memset(&ifr, 0, sizeof(ifr));
9863 strncpy(ifr.ifr_name, br_name, IFNAMSIZ);
developerd14dff12023-06-28 22:47:44 +08009864 strncpy(ifr.ifr_newname, bridgeName, strlen(bridgeName));
developercc5cbfb2023-06-13 18:29:52 +08009865 if (ioctl(sock, SIOCSIFNAME, &ifr) < 0) {
9866 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNAME) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009867 close(sock);
developera3511852023-06-14 14:12:59 +08009868 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009869 }
9870
9871 memset(br_name, 0, sizeof(br_name));
9872 memcpy(br_name, bridgeName, strlen(bridgeName));
9873
9874 memset(&ifr, 0, sizeof(ifr));
9875 strncpy(ifr.ifr_name, bridgeName, IFNAMSIZ);
9876 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9877 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009878 close(sock);
developera3511852023-06-14 14:12:59 +08009879 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009880 }
9881 ifr.ifr_flags = (short)(ifr.ifr_flags | IFF_UP);
9882 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9883 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009884 close(sock);
developera3511852023-06-14 14:12:59 +08009885 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009886 }
9887
9888 memset(&ifr, 0, sizeof(ifr));
9889 memcpy(ifr.ifr_name, bridgeName, strlen(bridgeName));
9890
9891 memset(&sin, 0, sizeof(struct sockaddr_in));
9892 sin.sin_family = AF_INET;
9893 if (inet_aton(IP, &(sin.sin_addr)) == 0) {
9894 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developer9ce44382023-06-28 11:09:37 +08009895 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009896 return RETURN_ERR;
9897 }
9898 memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr_in));
9899 if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
9900 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFADDR) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009901 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009902 return RETURN_ERR;
9903 }
9904
9905 if (inet_aton(subnet, &((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr) == 0) {
9906 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developerc14d83a2023-06-29 20:09:42 +08009907 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009908 return RETURN_ERR;
9909 }
9910 if (ioctl(sock, SIOCSIFNETMASK, &ifr) < -1) {
9911 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNETMASK) failed, %s", strerror(errno));
developerc14d83a2023-06-29 20:09:42 +08009912 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009913 return RETURN_ERR;
9914 }
9915
9916 close(sock);
developera3511852023-06-14 14:12:59 +08009917 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009918}
9919
9920// reset the vlan configuration for this ap
9921INT wifi_resetApVlanCfg(INT apIndex)
9922{
developera1255e42023-05-13 17:45:02 +08009923 char interface_name[16] = {0};
developer2202b332023-05-24 16:23:22 +08009924 int if_idx, ret = 0;
9925 struct nl_msg *msg = NULL;
9926 struct nlattr * msg_data = NULL;
9927 struct mtk_nl80211_param param;
9928 struct unl unl_ins;
9929 struct vlan_policy_param vlan_param;
developer72fb0bb2023-01-11 09:46:29 +08009930
developer2202b332023-05-24 16:23:22 +08009931 if (apIndex > MAX_APS) {
9932 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9933 return RETURN_ERR;
9934 }
developer72fb0bb2023-01-11 09:46:29 +08009935
developer2202b332023-05-24 16:23:22 +08009936 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9937 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9938 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009939
developer2202b332023-05-24 16:23:22 +08009940 /*step 1. mwctl dev %s set vlan_tag 0*/
9941 if_idx = if_nametoindex(interface_name);
9942 /*init mtk nl80211 vendor cmd*/
9943 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9944 param.if_type = NL80211_ATTR_IFINDEX;
9945 param.if_idx = if_idx;
9946 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
developer72fb0bb2023-01-11 09:46:29 +08009947
developer2202b332023-05-24 16:23:22 +08009948 if (ret) {
9949 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9950 return RETURN_ERR;
9951 }
developer72fb0bb2023-01-11 09:46:29 +08009952
developer2202b332023-05-24 16:23:22 +08009953 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_TAG_INFO, 0)) {
9954 printf("Nla put attribute error\n");
9955 nlmsg_free(msg);
9956 goto err;
9957 }
developer72fb0bb2023-01-11 09:46:29 +08009958
developer2202b332023-05-24 16:23:22 +08009959 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9960 if (ret) {
9961 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9962 goto err;
9963 }
9964 mtk_nl80211_deint(&unl_ins);
9965 wifi_debug(DEBUG_NOTICE, "set vlan_tag 0 cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08009966
developer2202b332023-05-24 16:23:22 +08009967 /*step 2. mwctl dev %s set vlan_priority 0*/
9968 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9969 if (ret) {
9970 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9971 return RETURN_ERR;
9972 }
developer72fb0bb2023-01-11 09:46:29 +08009973
developer2202b332023-05-24 16:23:22 +08009974 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_PRIORITY_INFO, 0)) {
9975 printf("Nla put attribute error\n");
9976 nlmsg_free(msg);
9977 goto err;
9978 }
developer72fb0bb2023-01-11 09:46:29 +08009979
developer2202b332023-05-24 16:23:22 +08009980 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9981 if (ret) {
9982 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9983 goto err;
9984 }
9985 mtk_nl80211_deint(&unl_ins);
9986 wifi_debug(DEBUG_NOTICE, "set vlan_priority 0 cmd success.\n");
9987
9988 /*step 3. mwctl dev %s set vlan_id 0*/
9989 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9990 if (ret) {
9991 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developera1255e42023-05-13 17:45:02 +08009992 return RETURN_ERR;
developer2202b332023-05-24 16:23:22 +08009993 }
developer72fb0bb2023-01-11 09:46:29 +08009994
developer2202b332023-05-24 16:23:22 +08009995 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, 0)) {
9996 printf("Nla put attribute error\n");
9997 nlmsg_free(msg);
9998 goto err;
9999 }
10000
10001 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10002 if (ret) {
10003 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10004 goto err;
10005 }
10006 mtk_nl80211_deint(&unl_ins);
10007 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
10008
10009 /*step 4. mwctl dev %s set vlan_en 0*/
10010 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10011 if (ret) {
10012 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10013 return RETURN_ERR;
10014 }
10015
10016 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_EN_INFO, 0)) {
10017 printf("Nla put attribute error\n");
10018 nlmsg_free(msg);
10019 goto err;
10020 }
10021
10022 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10023 if (ret) {
10024 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10025 goto err;
10026 }
10027 mtk_nl80211_deint(&unl_ins);
10028 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
10029
10030 /*step 5. mwctl dev %s set vlan_policy 0:4*/
10031 vlan_param.direction = 0;
10032 vlan_param.policy = 4;
10033 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10034 if (ret) {
10035 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10036 return RETURN_ERR;
10037 }
10038 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
10039 printf("Nla put attribute error\n");
10040 nlmsg_free(msg);
10041 goto err;
10042 }
10043
10044 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10045 if (ret) {
10046 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10047 goto err;
10048 }
10049 mtk_nl80211_deint(&unl_ins);
10050 wifi_debug(DEBUG_NOTICE, "set vlan_policy 0:4 cmd success.\n");
10051
10052 /*step 6. mwctl dev %s set vlan_policy 1:0*/
10053 vlan_param.direction = 1;
10054 vlan_param.policy = 0;
10055 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10056 if (ret) {
10057 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10058 return RETURN_ERR;
10059 }
10060
10061 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
10062 printf("Nla put attribute error\n");
10063 nlmsg_free(msg);
10064 goto err;
10065 }
10066
10067 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10068 if (ret) {
10069 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10070 goto err;
10071 }
10072 /*deinit mtk nl80211 vendor msg*/
10073 mtk_nl80211_deint(&unl_ins);
10074 wifi_debug(DEBUG_NOTICE, "set vlan_policy 1:0 cmd success.\n");
10075
10076 /*TODO need to modify VLAN config in dat file*/
10077 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10078
10079 return RETURN_OK;
10080err:
10081 mtk_nl80211_deint(&unl_ins);
10082 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10083 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010084}
10085
10086// 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.
10087INT wifi_createHostApdConfig(INT apIndex, BOOL createWpsCfg)
10088{
developera3511852023-06-14 14:12:59 +080010089 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010090}
10091
10092// starts hostapd, uses the variables in the hostapd config with format compatible with the specific hostapd implementation
10093INT wifi_startHostApd()
10094{
developera3511852023-06-14 14:12:59 +080010095 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +080010096 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +080010097 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10098 return RETURN_OK;
10099 //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 +080010100}
10101
10102// stops hostapd
developer69b61b02023-03-07 17:17:44 +080010103INT wifi_stopHostApd()
developer72fb0bb2023-01-11 09:46:29 +080010104{
developera3511852023-06-14 14:12:59 +080010105 char cmd[128] = {0};
10106 char buf[128] = {0};
developer75bd10c2023-06-27 11:34:08 +080010107 int res;
developer72fb0bb2023-01-11 09:46:29 +080010108
developer75bd10c2023-06-27 11:34:08 +080010109 res = snprintf(cmd, sizeof(cmd), "systemctl stop hostapd");
10110 if (os_snprintf_error(sizeof(cmd), res)) {
10111 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10112 return RETURN_ERR;
10113 }
developera3511852023-06-14 14:12:59 +080010114 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010115
developera3511852023-06-14 14:12:59 +080010116 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010117}
10118
10119// restart hostapd dummy function
10120INT wifi_restartHostApd()
10121{
developera3511852023-06-14 14:12:59 +080010122 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +080010123 v_secure_system("systemctl restart hostapd-global");
developera3511852023-06-14 14:12:59 +080010124 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010125
developera3511852023-06-14 14:12:59 +080010126 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010127}
10128
10129// sets the AP enable status variable for the specified ap.
10130INT wifi_setApEnable(INT apIndex, BOOL enable)
10131{
developer7e4a2a62023-04-06 19:56:03 +080010132 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080010133 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +080010134 char cmd[MAX_CMD_SIZE] = {0};
10135 char buf[MAX_BUF_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +080010136 BOOL status = FALSE;
developer7e4a2a62023-04-06 19:56:03 +080010137 int max_radio_num = 0;
10138 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010139 int res;
developer72fb0bb2023-01-11 09:46:29 +080010140
developer7e4a2a62023-04-06 19:56:03 +080010141 wifi_getApEnable(apIndex, &status);
developer72fb0bb2023-01-11 09:46:29 +080010142
developer7e4a2a62023-04-06 19:56:03 +080010143 wifi_getMaxRadioNumber(&max_radio_num);
10144 if (enable == status)
10145 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010146
developer7e4a2a62023-04-06 19:56:03 +080010147 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10148 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010149
developer7e4a2a62023-04-06 19:56:03 +080010150 if (enable == TRUE) {
10151 int radioIndex = apIndex % max_radio_num;
10152 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080010153 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s up", interface_name);
10154 if (os_snprintf_error(sizeof(cmd), res)) {
10155 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10156 return RETURN_ERR;
10157 }
developerf3c7d292023-05-29 17:57:16 +080010158 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +080010159
developere40952c2023-06-15 18:46:43 +080010160 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
10161 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
10162 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10163 return RETURN_ERR;
10164 }
10165 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
10166 if (os_snprintf_error(sizeof(cmd), res)) {
10167 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10168 return RETURN_ERR;
10169 }
developer7e4a2a62023-04-06 19:56:03 +080010170 _syscmd(cmd, buf, sizeof(buf));
10171 } else {
developere40952c2023-06-15 18:46:43 +080010172 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
10173 if (os_snprintf_error(sizeof(cmd), res)) {
10174 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10175 return RETURN_ERR;
10176 }
developer7e4a2a62023-04-06 19:56:03 +080010177 _syscmd(cmd, buf, sizeof(buf));
developere40952c2023-06-15 18:46:43 +080010178 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s down", interface_name);
10179 if (os_snprintf_error(sizeof(cmd), res)) {
10180 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10181 return RETURN_ERR;
10182 }
developerf3c7d292023-05-29 17:57:16 +080010183 _syscmd(cmd, buf, sizeof(buf));
developer7e4a2a62023-04-06 19:56:03 +080010184 }
developere40952c2023-06-15 18:46:43 +080010185 res = snprintf(cmd, MAX_CMD_SIZE, "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer7e4a2a62023-04-06 19:56:03 +080010186 interface_name, interface_name, enable, VAP_STATUS_FILE);
developere40952c2023-06-15 18:46:43 +080010187 if (os_snprintf_error(sizeof(cmd), res)) {
10188 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10189 return RETURN_ERR;
10190 }
developer7e4a2a62023-04-06 19:56:03 +080010191 _syscmd(cmd, buf, sizeof(buf));
10192 //Wait for wifi up/down to apply
10193 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010194}
10195
10196// Outputs the setting of the internal variable that is set by wifi_setApEnable().
10197INT wifi_getApEnable(INT apIndex, BOOL *output_bool)
10198{
developer7e4a2a62023-04-06 19:56:03 +080010199 char interface_name[IF_NAME_SIZE] = {0};
10200 char cmd[MAX_CMD_SIZE] = {0};
developerc1aa6532023-06-09 09:37:01 +080010201 char buf[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080010202 int res;
developer72fb0bb2023-01-11 09:46:29 +080010203
developer7e4a2a62023-04-06 19:56:03 +080010204 if ((!output_bool) || (apIndex < 0) || (apIndex >= MAX_APS))
10205 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010206
developer7e4a2a62023-04-06 19:56:03 +080010207 *output_bool = 0;
developer72fb0bb2023-01-11 09:46:29 +080010208
developer7e4a2a62023-04-06 19:56:03 +080010209 if ((apIndex >= 0) && (apIndex < MAX_APS)) {
10210 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
10211 *output_bool = FALSE;
10212 return RETURN_OK;
10213 }
developerc1aa6532023-06-09 09:37:01 +080010214
developer75bd10c2023-06-27 11:34:08 +080010215 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s status | grep state | cut -d '=' -f2", interface_name);
10216 if (os_snprintf_error(sizeof(cmd), res)) {
10217 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10218 return RETURN_ERR;
10219 }
developerc1aa6532023-06-09 09:37:01 +080010220 _syscmd(cmd, buf, sizeof(buf));
10221
10222 if(strncmp(buf, "ENABLED", 7) == 0 || strncmp(buf, "ACS", 3) == 0 ||
10223 strncmp(buf, "HT_SCAN", 7) == 0 || strncmp(buf, "DFS", 3) == 0) {
10224 *output_bool = TRUE;
10225 }
developer7e4a2a62023-04-06 19:56:03 +080010226 }
developer72fb0bb2023-01-11 09:46:29 +080010227
developer7e4a2a62023-04-06 19:56:03 +080010228 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010229}
10230
developer69b61b02023-03-07 17:17:44 +080010231// Outputs the AP "Enabled" "Disabled" status from driver
10232INT wifi_getApStatus(INT apIndex, CHAR *output_string)
developer72fb0bb2023-01-11 09:46:29 +080010233{
developer9ce44382023-06-28 11:09:37 +080010234 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +080010235 int res;
developer72fb0bb2023-01-11 09:46:29 +080010236
developer7e4a2a62023-04-06 19:56:03 +080010237 if (!output_string) {
10238 printf("%s: null pointer!", __func__);
10239 return RETURN_ERR;
10240 }
developer72fb0bb2023-01-11 09:46:29 +080010241
developer7e4a2a62023-04-06 19:56:03 +080010242 wifi_getApEnable(apIndex, &output_bool);
developer72fb0bb2023-01-11 09:46:29 +080010243
developer7e4a2a62023-04-06 19:56:03 +080010244 if(output_bool == 1)
developere40952c2023-06-15 18:46:43 +080010245 res = snprintf(output_string, 32, "Up");
developer7e4a2a62023-04-06 19:56:03 +080010246 else
developere40952c2023-06-15 18:46:43 +080010247 res = snprintf(output_string, 32, "Disable");
10248 if (os_snprintf_error(32, res)) {
10249 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10250 return RETURN_ERR;
10251 }
developer7e4a2a62023-04-06 19:56:03 +080010252
10253 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010254}
10255
10256//Indicates whether or not beacons include the SSID name.
10257// outputs a 1 if SSID on the AP is enabled, else outputs 0
10258INT wifi_getApSsidAdvertisementEnable(INT apIndex, BOOL *output)
10259{
developera3511852023-06-14 14:12:59 +080010260 //get the running status
10261 char config_file[MAX_BUF_SIZE] = {0};
10262 char buf[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080010263 int res;
developer72fb0bb2023-01-11 09:46:29 +080010264
developera3511852023-06-14 14:12:59 +080010265 if (!output)
10266 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010267
developer32f2a182023-06-27 19:50:41 +080010268 res = snprintf(config_file, sizeof(config_file),
10269 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010270 if (os_snprintf_error(sizeof(config_file), res)) {
10271 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10272 return RETURN_ERR;
10273 }
developera3511852023-06-14 14:12:59 +080010274 wifi_hostapdRead(config_file, "ignore_broadcast_ssid", buf, sizeof(buf));
10275 // default is enable
10276 if (strlen(buf) == 0 || strncmp("0", buf, 1) == 0)
10277 *output = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080010278
developera3511852023-06-14 14:12:59 +080010279 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010280}
10281
10282// sets an internal variable for ssid advertisement. Set to 1 to enable, set to 0 to disable
10283INT wifi_setApSsidAdvertisementEnable(INT apIndex, BOOL enable)
10284{
developera3511852023-06-14 14:12:59 +080010285 //store the config, apply instantly
10286 char config_file[MAX_BUF_SIZE] = {0};
10287 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010288 int res;
developer72fb0bb2023-01-11 09:46:29 +080010289
developera3511852023-06-14 14:12:59 +080010290 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10291 list.name = "ignore_broadcast_ssid";
10292 list.value = enable?"0":"1";
developer72fb0bb2023-01-11 09:46:29 +080010293
developer32f2a182023-06-27 19:50:41 +080010294 res = snprintf(config_file, sizeof(config_file),
10295 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010296 if (os_snprintf_error(sizeof(config_file), res)) {
10297 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10298 return RETURN_ERR;
10299 }
developera3511852023-06-14 14:12:59 +080010300 wifi_hostapdWrite(config_file, &list, 1);
10301 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10302 //TODO: call hostapd_cli for dynamic_config_control
10303 wifi_reloadAp(apIndex);
10304 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010305
developera3511852023-06-14 14:12:59 +080010306 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010307}
10308
10309//The maximum number of retransmission for a packet. This corresponds to IEEE 802.11 parameter dot11ShortRetryLimit.
10310INT wifi_getApRetryLimit(INT apIndex, UINT *output_uint)
10311{
developer47cc27a2023-05-17 23:09:58 +080010312 /* get the running status */
10313 if(!output_uint)
developera3511852023-06-14 14:12:59 +080010314 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +080010315
10316 *output_uint = 15;
10317 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010318}
10319
developer47cc27a2023-05-17 23:09:58 +080010320/*Do not support AP retry limit fix*/
developer72fb0bb2023-01-11 09:46:29 +080010321INT wifi_setApRetryLimit(INT apIndex, UINT number)
10322{
developer47cc27a2023-05-17 23:09:58 +080010323 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010324}
10325
developer95c045d2023-05-24 19:26:28 +080010326int get_wmm_cap_status_callback(struct nl_msg *msg, void *data)
10327{
10328 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10329 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_WMM_ATTR_MAX + 1];
10330 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developer2f79c922023-06-02 17:33:42 +080010331 unsigned char *status = (unsigned char *)data;
developer95c045d2023-05-24 19:26:28 +080010332 int err = 0;
developer95c045d2023-05-24 19:26:28 +080010333
10334 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10335 genlmsg_attrlen(gnlh, 0), NULL);
10336 if (err < 0)
10337 return err;
10338
10339 if (tb[NL80211_ATTR_VENDOR_DATA]) {
10340 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_WMM_ATTR_MAX,
10341 tb[NL80211_ATTR_VENDOR_DATA], NULL);
10342 if (err < 0)
10343 return err;
10344
10345 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]) {
developer95c045d2023-05-24 19:26:28 +080010346 *status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]);
10347 }
10348 }
10349
10350 return 0;
10351}
10352
developer72fb0bb2023-01-11 09:46:29 +080010353//Indicates whether this access point supports WiFi Multimedia (WMM) Access Categories (AC).
10354INT wifi_getApWMMCapability(INT apIndex, BOOL *output)
10355{
developer95c045d2023-05-24 19:26:28 +080010356 int if_idx, ret = 0;
developera3511852023-06-14 14:12:59 +080010357 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010358 unsigned char status = 0;
10359 struct nl_msg *msg = NULL;
10360 struct nlattr * msg_data = NULL;
10361 struct mtk_nl80211_param param;
10362 struct unl unl_ins;
developer8e6583c2023-05-23 13:36:06 +080010363
developera3511852023-06-14 14:12:59 +080010364 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10365 if(!output)
developerdaf24792023-06-06 11:40:04 +080010366 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010367
developer95c045d2023-05-24 19:26:28 +080010368 if (apIndex > MAX_APS) {
10369 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10370 return RETURN_ERR;
10371 }
10372
developera3511852023-06-14 14:12:59 +080010373 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developerdaf24792023-06-06 11:40:04 +080010374 return RETURN_ERR;
developer95c045d2023-05-24 19:26:28 +080010375
10376 if_idx = if_nametoindex(interface_name);
10377 /*init mtk nl80211 vendor cmd*/
10378 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10379 param.if_type = NL80211_ATTR_IFINDEX;
10380 param.if_idx = if_idx;
10381 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10382
10383 if (ret) {
10384 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10385 return RETURN_ERR;
10386 }
10387
10388 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, 0xf)) {
10389 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10390 nlmsg_free(msg);
10391 goto err;
10392 }
10393
10394 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_wmm_cap_status_callback,
10395 (void *)&status);
10396 if (ret) {
10397 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10398 goto err;
10399 }
10400 mtk_nl80211_deint(&unl_ins);
10401
10402 *output = status == 0 ? FALSE : TRUE;
10403 wifi_debug(DEBUG_NOTICE, "wmm cap (%u).\n", (unsigned int)(*output));
developer8e6583c2023-05-23 13:36:06 +080010404
developera3511852023-06-14 14:12:59 +080010405 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10406 return RETURN_OK;
10407err:
developer95c045d2023-05-24 19:26:28 +080010408 mtk_nl80211_deint(&unl_ins);
10409 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10410 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010411}
10412
10413//Indicates whether this access point supports WMM Unscheduled Automatic Power Save Delivery (U-APSD). Note: U-APSD support implies WMM support.
10414INT wifi_getApUAPSDCapability(INT apIndex, BOOL *output)
10415{
developera3511852023-06-14 14:12:59 +080010416 //get the running status from driver
10417 char cmd[128] = {0};
10418 char buf[128] = {0};
10419 int max_radio_num = 0, radioIndex = 0;
10420 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010421 int res;
developer72fb0bb2023-01-11 09:46:29 +080010422
developera3511852023-06-14 14:12:59 +080010423 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010424
developera3511852023-06-14 14:12:59 +080010425 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080010426 if(max_radio_num == 0){
10427 return RETURN_ERR;
10428 }
developera3511852023-06-14 14:12:59 +080010429 radioIndex = apIndex % max_radio_num;
10430 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080010431 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep u-APSD", phyId);
10432 if (os_snprintf_error(sizeof(cmd), res)) {
10433 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10434 return RETURN_ERR;
10435 }
developera3511852023-06-14 14:12:59 +080010436 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010437
developera3511852023-06-14 14:12:59 +080010438 if (strlen(buf) > 0)
10439 *output = true;
developer72fb0bb2023-01-11 09:46:29 +080010440
developera3511852023-06-14 14:12:59 +080010441 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010442
developera3511852023-06-14 14:12:59 +080010443 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010444}
10445
10446//Whether WMM support is currently enabled. When enabled, this is indicated in beacon frames.
10447INT wifi_getApWmmEnable(INT apIndex, BOOL *output)
10448{
developera3511852023-06-14 14:12:59 +080010449 return wifi_getApWMMCapability(apIndex, output);
developer72fb0bb2023-01-11 09:46:29 +080010450}
10451
10452// enables/disables WMM on the hardwawre for this AP. enable==1, disable == 0
10453INT wifi_setApWmmEnable(INT apIndex, BOOL enable)
10454{
developer95c045d2023-05-24 19:26:28 +080010455 int if_idx, ret = 0;
10456 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010457 struct nl_msg *msg = NULL;
10458 struct nlattr * msg_data = NULL;
10459 struct mtk_nl80211_param param;
10460 struct unl unl_ins;
developer72fb0bb2023-01-11 09:46:29 +080010461
developer95c045d2023-05-24 19:26:28 +080010462 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010463
developer95c045d2023-05-24 19:26:28 +080010464 if (apIndex > MAX_APS) {
10465 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10466 return RETURN_ERR;
10467 }
developer72fb0bb2023-01-11 09:46:29 +080010468
developer95c045d2023-05-24 19:26:28 +080010469 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10470 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010471
developer95c045d2023-05-24 19:26:28 +080010472 if_idx = if_nametoindex(interface_name);
10473 /*init mtk nl80211 vendor cmd*/
10474 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10475 param.if_type = NL80211_ATTR_IFINDEX;
10476 param.if_idx = if_idx;
10477 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10478
10479 if (ret) {
10480 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10481 return RETURN_ERR;
10482 }
10483
10484 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, enable ? 1 : 0)) {
10485 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10486 nlmsg_free(msg);
10487 goto err;
10488 }
10489
10490 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10491 if (ret) {
10492 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10493 goto err;
10494 }
10495 mtk_nl80211_deint(&unl_ins);
10496
10497 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10498 return RETURN_OK;
10499err:
10500 mtk_nl80211_deint(&unl_ins);
10501 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10502 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010503}
10504
developer95c045d2023-05-24 19:26:28 +080010505
developer72fb0bb2023-01-11 09:46:29 +080010506//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.
10507INT wifi_getApWmmUapsdEnable(INT apIndex, BOOL *output)
10508{
developer75bd10c2023-06-27 11:34:08 +080010509 int res;
10510
developera3511852023-06-14 14:12:59 +080010511 //get the running status from driver
10512 if(!output)
10513 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010514
developera3511852023-06-14 14:12:59 +080010515 char config_file[128] = {0};
10516 char buf[16] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010517
developer75bd10c2023-06-27 11:34:08 +080010518 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10519 if (os_snprintf_error(sizeof(config_file), res)) {
10520 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10521 return RETURN_ERR;
10522 }
developera3511852023-06-14 14:12:59 +080010523 wifi_hostapdRead(config_file, "uapsd_advertisement_enabled", buf, sizeof(buf));
10524 if (strlen(buf) == 0 || strncmp("1", buf, 1) == 0)
10525 *output = TRUE;
10526 else
10527 *output = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080010528
developera3511852023-06-14 14:12:59 +080010529 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010530}
10531
10532// enables/disables Automatic Power Save Delivery on the hardwarwe for this AP
10533INT wifi_setApWmmUapsdEnable(INT apIndex, BOOL enable)
10534{
developera3511852023-06-14 14:12:59 +080010535 //save config and apply instantly.
10536 char config_file[MAX_BUF_SIZE] = {0};
10537 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010538 int res;
developer72fb0bb2023-01-11 09:46:29 +080010539
developera3511852023-06-14 14:12:59 +080010540 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10541 list.name = "uapsd_advertisement_enabled";
10542 list.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080010543
developer75bd10c2023-06-27 11:34:08 +080010544 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10545 if (os_snprintf_error(sizeof(config_file), res)) {
10546 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10547 return RETURN_ERR;
10548 }
developera3511852023-06-14 14:12:59 +080010549 wifi_hostapdWrite(config_file, &list, 1);
10550 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10551 wifi_quick_reload_ap(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// Sets the WMM ACK policy on the hardware. AckPolicy false means do not acknowledge, true means acknowledge
10558INT wifi_setApWmmOgAckPolicy(INT apIndex, INT class, BOOL ackPolicy) //RDKB
10559{
developera3511852023-06-14 14:12:59 +080010560 char interface_name[16] = {0};
10561 // assume class 0->BE, 1->BK, 2->VI, 3->VO
10562 char cmd[MAX_CMD_SIZE] = {0};
10563 char buf[128] = {0};
10564 char ack_filepath[128] = {0};
10565 uint16_t bitmap = 0;
10566 uint16_t class_map[4] = {0x0009, 0x0006, 0x0030, 0x00C0};
10567 FILE *f = NULL;
developere40952c2023-06-15 18:46:43 +080010568 int res;
developerc14d83a2023-06-29 20:09:42 +080010569 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080010570
developera3511852023-06-14 14:12:59 +080010571 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010572
developera3511852023-06-14 14:12:59 +080010573 // Get current setting
developere40952c2023-06-15 18:46:43 +080010574 res = snprintf(ack_filepath, sizeof(ack_filepath), "%s%d.txt", NOACK_MAP_FILE, apIndex);
10575 if (os_snprintf_error(sizeof(ack_filepath), res)) {
10576 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10577 return RETURN_ERR;
10578 }
10579 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", ack_filepath);
10580 if (os_snprintf_error(sizeof(cmd), res)) {
10581 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10582 return RETURN_ERR;
10583 }
developera3511852023-06-14 14:12:59 +080010584 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +080010585 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080010586 if (hal_strtoul(buf, 10, &tmp) < 0) {
10587 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080010588 }
developerc14d83a2023-06-29 20:09:42 +080010589 bitmap = tmp;
developerd14dff12023-06-28 22:47:44 +080010590 }
developer72fb0bb2023-01-11 09:46:29 +080010591
developera3511852023-06-14 14:12:59 +080010592 if (ackPolicy == TRUE) { // True, unset this class
10593 bitmap &= ~class_map[class];
10594 } else { // False, set this class
10595 bitmap |= class_map[class];
10596 }
developer72fb0bb2023-01-11 09:46:29 +080010597
developera3511852023-06-14 14:12:59 +080010598 f = fopen(ack_filepath, "w");
10599 if (f == NULL) {
developer37646972023-06-29 10:58:43 +080010600 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
10601 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +080010602 return RETURN_ERR;
10603 }
developer37646972023-06-29 10:58:43 +080010604 if (fprintf(f, "%hu", bitmap) < 0)
10605 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
10606
10607 if (fclose(f) == EOF) {
10608 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
10609 return RETURN_ERR;
10610 }
developer72fb0bb2023-01-11 09:46:29 +080010611
developera3511852023-06-14 14:12:59 +080010612 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10613 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010614 res = snprintf(cmd, sizeof(cmd), "iw dev %s set noack_map 0x%04x\n", interface_name, bitmap);
10615 if (os_snprintf_error(sizeof(cmd), res)) {
10616 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10617 return RETURN_ERR;
10618 }
developera3511852023-06-14 14:12:59 +080010619 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010620
developera3511852023-06-14 14:12:59 +080010621 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
10622 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010623}
10624
10625//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.
10626INT wifi_getApMaxAssociatedDevices(INT apIndex, UINT *output_uint)
10627{
developer75bd10c2023-06-27 11:34:08 +080010628 int res;
10629
developera3511852023-06-14 14:12:59 +080010630 //get the running status from driver
10631 if(!output_uint)
10632 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010633
developera3511852023-06-14 14:12:59 +080010634 char output[16]={'\0'};
10635 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010636
developer75bd10c2023-06-27 11:34:08 +080010637 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10638 if (os_snprintf_error(sizeof(config_file), res)) {
10639 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10640 return RETURN_ERR;
10641 }
developera3511852023-06-14 14:12:59 +080010642 wifi_hostapdRead(config_file, "max_num_sta", output, sizeof(output));
10643 if (strlen(output) == 0) *output_uint = MAX_ASSOCIATED_STA_NUM;
10644 else {
10645 int device_num = atoi(output);
10646 if (device_num > MAX_ASSOCIATED_STA_NUM || device_num < 0) {
10647 wifi_dbg_printf("\n[%s]: get max_num_sta error: %d", __func__, device_num);
10648 return RETURN_ERR;
10649 }
10650 else {
10651 *output_uint = device_num;
10652 }
10653 }
developer72fb0bb2023-01-11 09:46:29 +080010654
developera3511852023-06-14 14:12:59 +080010655 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010656}
10657
10658INT wifi_setApMaxAssociatedDevices(INT apIndex, UINT number)
10659{
developera3511852023-06-14 14:12:59 +080010660 //store to wifi config, apply instantly
10661 char str[MAX_BUF_SIZE]={'\0'};
10662 struct params params;
10663 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080010664 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080010665
developera3511852023-06-14 14:12:59 +080010666 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10667 if (number > MAX_ASSOCIATED_STA_NUM) {
10668 WIFI_ENTRY_EXIT_DEBUG("%s: Invalid input\n",__func__);
10669 return RETURN_ERR;
10670 }
developer75bd10c2023-06-27 11:34:08 +080010671 res = snprintf(str, sizeof(str), "%d", number);
10672 if (os_snprintf_error(sizeof(str), res)) {
10673 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10674 return RETURN_ERR;
10675 }
developera3511852023-06-14 14:12:59 +080010676 params.name = "max_num_sta";
10677 params.value = str;
developer72fb0bb2023-01-11 09:46:29 +080010678
developer32f2a182023-06-27 19:50:41 +080010679 res = snprintf(config_file,
10680 sizeof(config_file), "%s%d.conf",CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010681 if (os_snprintf_error(sizeof(config_file), res)) {
10682 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10683 return RETURN_ERR;
10684 }
developer32f2a182023-06-27 19:50:41 +080010685 ret = wifi_hostapdWrite(config_file, &params, 1);
developera3511852023-06-14 14:12:59 +080010686 if (ret) {
10687 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n"
10688 ,__func__, ret);
10689 }
developer72fb0bb2023-01-11 09:46:29 +080010690
developera3511852023-06-14 14:12:59 +080010691 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10692 if (ret) {
10693 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n"
10694 ,__func__, ret);
10695 }
10696 wifi_reloadAp(apIndex);
10697 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010698
developera3511852023-06-14 14:12:59 +080010699 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010700}
10701
10702//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.
10703INT wifi_getApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT *output_uint)
10704{
developera3511852023-06-14 14:12:59 +080010705 //get the current threshold
10706 if(!output_uint)
10707 return RETURN_ERR;
10708 wifi_getApMaxAssociatedDevices(apIndex, output_uint);
10709 if (*output_uint == 0)
10710 *output_uint = 50;
10711 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010712}
10713
10714INT wifi_setApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT Threshold)
10715{
developera3511852023-06-14 14:12:59 +080010716 //store the config, reset threshold, reset AssociatedDevicesHighWatermarkThresholdReached, reset AssociatedDevicesHighWatermarkDate to current time
10717 if (!wifi_setApMaxAssociatedDevices(apIndex, Threshold))
10718 return RETURN_OK;
10719 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010720}
10721
10722//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.
10723INT wifi_getApAssociatedDevicesHighWatermarkThresholdReached(INT apIndex, UINT *output_uint)
10724{
developera3511852023-06-14 14:12:59 +080010725 if(!output_uint)
10726 return RETURN_ERR;
10727 *output_uint = 3;
10728 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010729}
10730
10731//Maximum number of associated devices that have ever associated with the access point concurrently since the last reset of the device or WiFi module.
10732INT wifi_getApAssociatedDevicesHighWatermark(INT apIndex, UINT *output_uint)
10733{
developera3511852023-06-14 14:12:59 +080010734 if(!output_uint)
10735 return RETURN_ERR;
10736 *output_uint = 3;
10737 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010738}
10739
10740//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.
10741INT wifi_getApAssociatedDevicesHighWatermarkDate(INT apIndex, ULONG *output_in_seconds)
10742{
developera3511852023-06-14 14:12:59 +080010743 if(!output_in_seconds)
10744 return RETURN_ERR;
10745 *output_in_seconds = 0;
10746 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010747}
10748
10749//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
10750INT wifi_getApSecurityModesSupported(INT apIndex, CHAR *output)
10751{
developere40952c2023-06-15 18:46:43 +080010752 int res;
10753
developera3511852023-06-14 14:12:59 +080010754 if(!output || apIndex>=MAX_APS)
10755 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010756 //res = snprintf(output, 128, "None,WPA-Personal,WPA2-Personal,WPA-WPA2-Personal,WPA-Enterprise,WPA2-Enterprise,WPA-WPA2-Enterprise");
10757 res = snprintf(output, 128, "None,WPA2-Personal,WPA-WPA2-Personal,WPA2-Enterprise,WPA-WPA2-Enterprise,WPA3-Personal,WPA3-Enterprise");
10758 if (os_snprintf_error(128, res)) {
10759 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10760 return RETURN_ERR;
10761 }
developera3511852023-06-14 14:12:59 +080010762 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010763}
developer72fb0bb2023-01-11 09:46:29 +080010764
10765//The value MUST be a member of the list reported by the ModesSupported parameter. Indicates which security mode is enabled.
10766INT wifi_getApSecurityModeEnabled(INT apIndex, CHAR *output)
10767{
developera3511852023-06-14 14:12:59 +080010768 char config_file[128] = {0};
10769 char wpa[16] = {0};
10770 char key_mgmt[64] = {0};
developer9ce44382023-06-28 11:09:37 +080010771 int res = -1;
developere40952c2023-06-15 18:46:43 +080010772
developera3511852023-06-14 14:12:59 +080010773 if (!output)
10774 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010775
developer75bd10c2023-06-27 11:34:08 +080010776 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10777 if (os_snprintf_error(sizeof(config_file), res)) {
10778 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10779 return RETURN_ERR;
10780 }
developera3511852023-06-14 14:12:59 +080010781 wifi_hostapdRead(config_file, "wpa", wpa, sizeof(wpa));
developer72fb0bb2023-01-11 09:46:29 +080010782
developer32f2a182023-06-27 19:50:41 +080010783 memcpy(output, "None", 4);//Copying "None" to output string for default case
10784 output[4] = '\0';
developera3511852023-06-14 14:12:59 +080010785 wifi_hostapdRead(config_file, "wpa_key_mgmt", key_mgmt, sizeof(key_mgmt));
10786 if (strstr(key_mgmt, "WPA-PSK") && strstr(key_mgmt, "SAE") == NULL) {
10787 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010788 res = snprintf(output, 32, "WPA-Personal");
developera3511852023-06-14 14:12:59 +080010789 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010790 res = snprintf(output, 32, "WPA2-Personal");
developera3511852023-06-14 14:12:59 +080010791 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010792 res = snprintf(output, 32, "WPA-WPA2-Personal");
developer72fb0bb2023-01-11 09:46:29 +080010793
developera3511852023-06-14 14:12:59 +080010794 } else if (strstr(key_mgmt, "WPA-EAP-SUITE-B-192")) {
developere40952c2023-06-15 18:46:43 +080010795 res = snprintf(output, 32, "WPA3-Enterprise");
developera3511852023-06-14 14:12:59 +080010796 } else if (strstr(key_mgmt, "WPA-EAP")) {
10797 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010798 res = snprintf(output, 32, "WPA-Enterprise");
developera3511852023-06-14 14:12:59 +080010799 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010800 res = snprintf(output, 32, "WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010801 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010802 res = snprintf(output, 32, "WPA-WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010803 } else if (strstr(key_mgmt, "SAE")) {
10804 if (strstr(key_mgmt, "WPA-PSK") == NULL)
developere40952c2023-06-15 18:46:43 +080010805 res = snprintf(output, 32, "WPA3-Personal");
developera3511852023-06-14 14:12:59 +080010806 else
developere40952c2023-06-15 18:46:43 +080010807 res = snprintf(output, 32, "WPA3-Personal-Transition");
10808 }
10809 if (os_snprintf_error(32, res)) {
10810 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10811 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080010812 }
developer72fb0bb2023-01-11 09:46:29 +080010813
developera3511852023-06-14 14:12:59 +080010814 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
10815 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010816}
developer69b61b02023-03-07 17:17:44 +080010817
developer72fb0bb2023-01-11 09:46:29 +080010818INT wifi_setApSecurityModeEnabled(INT apIndex, CHAR *encMode)
10819{
developer32f2a182023-06-27 19:50:41 +080010820 char securityType[32] = {0};
10821 char authMode[32] = {0};
10822 unsigned long len_sec, len_auth;
developera3511852023-06-14 14:12:59 +080010823 //store settings and wait for wifi up to apply
10824 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10825 if(!encMode)
10826 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010827
developera3511852023-06-14 14:12:59 +080010828 if (strcmp(encMode, "None")==0)
10829 {
developer32f2a182023-06-27 19:50:41 +080010830 len_sec = strlen("None");
10831 len_auth = strlen("None");
10832 memcpy(securityType, "None", len_sec);
10833 memcpy(authMode, "None", len_auth);
10834 } else if (strcmp(encMode, "WPA-WPA2-Personal")==0) {
10835 len_sec = strlen("WPAand11i");
10836 memcpy(securityType, "WPAand11i", len_sec);
10837 len_auth = strlen("PSKAuthentication");
10838 memcpy(authMode, "PSKAuthentication", len_auth);
10839 } else if (strcmp(encMode, "WPA-WPA2-Enterprise")==0) {
10840 len_sec = strlen("WPAand11i");
10841 memcpy(securityType, "WPAand11i", len_sec);
10842 len_auth = strlen("EAPAuthentication");
10843 memcpy(authMode, "EAPAuthentication", len_auth);
10844 } else if (strcmp(encMode, "WPA-Personal")==0) {
10845 len_sec = strlen("WPA");
10846 memcpy(securityType, "WPA", len_sec);
10847 len_auth = strlen("PSKAuthentication");
10848 memcpy(authMode, "PSKAuthentication", len_auth);
10849 } else if (strcmp(encMode, "WPA-Enterprise")==0) {
10850 len_sec = strlen("WPA");
10851 memcpy(securityType, "WPA", len_sec);
10852 len_auth = strlen("EAPAuthentication");
10853 memcpy(authMode, "EAPAuthentication", len_auth);
10854 } else if (strcmp(encMode, "WPA2-Personal")==0) {
10855 len_sec = strlen("11i");
10856 memcpy(securityType, "11i", len_sec);
10857 len_auth = strlen("PSKAuthentication");
10858 memcpy(authMode, "PSKAuthentication", len_auth);
10859 } else if (strcmp(encMode, "WPA2-Enterprise")==0) {
10860 len_sec = strlen("11i");
10861 memcpy(securityType, "11i", len_sec);
10862 len_auth = strlen("EAPAuthentication");
10863 memcpy(authMode, "EAPAuthentication", len_auth);
10864 } else if (strcmp(encMode, "WPA3-Personal") == 0) {
10865 len_sec = strlen("11i");
10866 memcpy(securityType, "11i", len_sec);
10867 len_auth = strlen("SAEAuthentication");
10868 memcpy(authMode, "SAEAuthentication", len_auth);
10869 } else if (strcmp(encMode, "WPA3-Personal-Transition") == 0) {
10870 len_sec = strlen("11i");
10871 memcpy(securityType, "11i", len_sec);
10872 len_auth = strlen("PSK-SAEAuthentication");
10873 memcpy(authMode, "PSK-SAEAuthentication", len_auth);
10874 } else if (strcmp(encMode, "WPA3-Enterprise") == 0) {
10875 len_sec = strlen("11i");
10876 memcpy(securityType, "11i", len_sec);
10877 len_auth = strlen("EAP_192-bit_Authentication");
10878 memcpy(authMode, "EAP_192-bit_Authentication", len_auth);
10879 } else if (strcmp(encMode, "OWE") == 0) {
10880 len_sec = strlen("11i");
10881 memcpy(securityType, "11i", len_sec);
10882 len_auth = strlen("Enhanced_Open");
10883 memcpy(authMode, "Enhanced_Open", len_auth);
10884 } else {
10885 len_sec = strlen("None");
10886 memcpy(securityType, "None", len_sec);
10887 len_auth = strlen("None");
10888 memcpy(authMode, "None", len_auth);
developera3511852023-06-14 14:12:59 +080010889 }
developer32f2a182023-06-27 19:50:41 +080010890 securityType[len_sec] = '\0';
10891 authMode[len_auth] = '\0';
developera3511852023-06-14 14:12:59 +080010892 wifi_setApBeaconType(apIndex, securityType);
10893 wifi_setApBasicAuthenticationMode(apIndex, authMode);
10894 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010895
developera3511852023-06-14 14:12:59 +080010896 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010897}
developer72fb0bb2023-01-11 09:46:29 +080010898
10899
10900//A literal PreSharedKey (PSK) expressed as a hexadecimal string.
10901// output_string must be pre-allocated as 64 character string by caller
10902// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10903INT wifi_getApSecurityPreSharedKey(INT apIndex, CHAR *output_string)
10904{
developera3511852023-06-14 14:12:59 +080010905 char buf[16] = {0};
10906 char config_file[MAX_BUF_SIZE] = {0};
10907 int res;
developer72fb0bb2023-01-11 09:46:29 +080010908
developera3511852023-06-14 14:12:59 +080010909 if(output_string==NULL)
10910 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010911
developera3511852023-06-14 14:12:59 +080010912 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10913 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010914 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10915 return RETURN_ERR;
10916 }
developera3511852023-06-14 14:12:59 +080010917 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010918
developera3511852023-06-14 14:12:59 +080010919 if(strcmp(buf,"0")==0)
10920 {
10921 printf("wpa_mode is %s ......... \n",buf);
10922 return RETURN_ERR;
10923 }
developer72fb0bb2023-01-11 09:46:29 +080010924
developera3511852023-06-14 14:12:59 +080010925 wifi_dbg_printf("\nFunc=%s\n",__func__);
10926 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10927 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010928 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10929 return RETURN_ERR;
10930 }
developere5750452023-05-15 16:46:42 +080010931 wifi_hostapdRead(config_file,"wpa_psk",output_string,65);
developera3511852023-06-14 14:12:59 +080010932 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010933
developera3511852023-06-14 14:12:59 +080010934 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010935}
10936
10937// sets an enviornment variable for the psk. Input string preSharedKey must be a maximum of 64 characters
10938// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10939INT wifi_setApSecurityPreSharedKey(INT apIndex, CHAR *preSharedKey)
10940{
developera3511852023-06-14 14:12:59 +080010941 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
10942 struct params params={'\0'};
developer32f2a182023-06-27 19:50:41 +080010943 int ret;
developera3511852023-06-14 14:12:59 +080010944 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010945
developera3511852023-06-14 14:12:59 +080010946 if(NULL == preSharedKey)
10947 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010948
developera3511852023-06-14 14:12:59 +080010949 params.name = "wpa_psk";
developer72fb0bb2023-01-11 09:46:29 +080010950
developera3511852023-06-14 14:12:59 +080010951 if(strlen(preSharedKey) != 64)
10952 {
10953 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 64 chars\n");
10954 return RETURN_ERR;
10955 }
10956 params.value = preSharedKey;
developer32f2a182023-06-27 19:50:41 +080010957 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10958 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080010959 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10960 return RETURN_ERR;
10961 }
developera3511852023-06-14 14:12:59 +080010962 ret = wifi_hostapdWrite(config_file, &params, 1);
10963 if(!ret) {
10964 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10965 wifi_reloadAp(apIndex);
10966 }
10967 return ret;
10968 //TODO: call hostapd_cli for dynamic_config_control
developer72fb0bb2023-01-11 09:46:29 +080010969}
10970
10971//A passphrase from which the PreSharedKey is to be generated, for WPA-Personal or WPA2-Personal or WPA-WPA2-Personal security modes.
10972// outputs the passphrase, maximum 63 characters
10973INT wifi_getApSecurityKeyPassphrase(INT apIndex, CHAR *output_string)
10974{
developera3511852023-06-14 14:12:59 +080010975 char config_file[MAX_BUF_SIZE] = {0}, buf[32] = {0};
developer75bd10c2023-06-27 11:34:08 +080010976 int res;
developer72fb0bb2023-01-11 09:46:29 +080010977
developera3511852023-06-14 14:12:59 +080010978 wifi_dbg_printf("\nFunc=%s\n",__func__);
10979 if (NULL == output_string)
10980 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010981
developer75bd10c2023-06-27 11:34:08 +080010982 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10983 if (os_snprintf_error(sizeof(config_file), res)) {
10984 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10985 return RETURN_ERR;
10986 }
10987
developera3511852023-06-14 14:12:59 +080010988 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
10989 if(strcmp(buf,"0")==0)
10990 {
10991 printf("wpa_mode is %s ......... \n",buf);
10992 return RETURN_ERR;
10993 }
developer72fb0bb2023-01-11 09:46:29 +080010994
developera3511852023-06-14 14:12:59 +080010995 wifi_hostapdRead(config_file,"wpa_passphrase",output_string,64);
10996 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010997
developera3511852023-06-14 14:12:59 +080010998 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010999}
11000
11001// sets the passphrase enviornment variable, max 63 characters
11002INT wifi_setApSecurityKeyPassphrase(INT apIndex, CHAR *passPhrase)
11003{
developera3511852023-06-14 14:12:59 +080011004 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
11005 struct params params={'\0'};
11006 char config_file[MAX_BUF_SIZE] = {0};
11007 int ret;
developer72fb0bb2023-01-11 09:46:29 +080011008
developera3511852023-06-14 14:12:59 +080011009 if(NULL == passPhrase)
11010 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011011
developera3511852023-06-14 14:12:59 +080011012 if(strlen(passPhrase)<8 || strlen(passPhrase)>63)
11013 {
11014 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 8 to 63 chars\n");
11015 return RETURN_ERR;
11016 }
11017 params.name = "wpa_passphrase";
11018 params.value = passPhrase;
developer32f2a182023-06-27 19:50:41 +080011019 ret = snprintf(config_file, sizeof(config_file),
11020 "%s%d.conf", CONFIG_PREFIX, apIndex);
11021 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080011022 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11023 return RETURN_ERR;
11024 }
developera3511852023-06-14 14:12:59 +080011025 ret=wifi_hostapdWrite(config_file,&params,1);
11026 if(!ret) {
11027 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +080011028 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +080011029 }
developer72fb0bb2023-01-11 09:46:29 +080011030
developera3511852023-06-14 14:12:59 +080011031 return ret;
developer72fb0bb2023-01-11 09:46:29 +080011032}
11033
11034//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.
11035INT wifi_setApSecurityReset(INT apIndex)
11036{
developera3511852023-06-14 14:12:59 +080011037 char original_config_file[64] = {0};
11038 char current_config_file[64] = {0};
11039 char buf[64] = {0};
11040 char cmd[64] = {0};
11041 char wpa[4] = {0};
11042 char wpa_psk[64] = {0};
11043 char wpa_passphrase[64] = {0};
11044 char wpa_psk_file[128] = {0};
11045 char wpa_key_mgmt[64] = {0};
11046 char wpa_pairwise[32] = {0};
11047 wifi_band band;
11048 struct params list[6];
developere40952c2023-06-15 18:46:43 +080011049 int res;
developer72fb0bb2023-01-11 09:46:29 +080011050
developera3511852023-06-14 14:12:59 +080011051 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011052
developera3511852023-06-14 14:12:59 +080011053 band = wifi_index_to_band(apIndex);
developer37646972023-06-29 10:58:43 +080011054 if (band == band_2_4) {
11055 res = snprintf(original_config_file, sizeof(original_config_file),
11056 "/etc/hostapd-2G.conf");
11057 if (os_snprintf_error(sizeof(original_config_file), res)) {
11058 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11059 return RETURN_ERR;
11060 }
11061 } else if (band == band_5) {
11062 res = snprintf(original_config_file, sizeof(original_config_file),
11063 "/etc/hostapd-5G.conf");
11064 if (os_snprintf_error(sizeof(original_config_file), res)) {
11065 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11066 return RETURN_ERR;
11067 }
11068 } else if (band == band_6) {
11069 res = snprintf(original_config_file, sizeof(original_config_file),
11070 "/etc/hostapd-6G.conf");
11071 if (os_snprintf_error(sizeof(original_config_file), res)) {
11072 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11073 return RETURN_ERR;
11074 }
11075 } else
developera3511852023-06-14 14:12:59 +080011076 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011077
developera3511852023-06-14 14:12:59 +080011078 wifi_hostapdRead(original_config_file, "wpa", wpa, sizeof(wpa));
11079 list[0].name = "wpa";
11080 list[0].value = wpa;
developer69b61b02023-03-07 17:17:44 +080011081
developera3511852023-06-14 14:12:59 +080011082 wifi_hostapdRead(original_config_file, "wpa_psk", wpa_psk, sizeof(wpa_psk));
11083 list[1].name = "wpa_psk";
11084 list[1].value = wpa_psk;
developer72fb0bb2023-01-11 09:46:29 +080011085
developera3511852023-06-14 14:12:59 +080011086 wifi_hostapdRead(original_config_file, "wpa_passphrase", wpa_passphrase, sizeof(wpa_passphrase));
11087 list[2].name = "wpa_passphrase";
11088 list[2].value = wpa_passphrase;
developer72fb0bb2023-01-11 09:46:29 +080011089
developera3511852023-06-14 14:12:59 +080011090 wifi_hostapdRead(original_config_file, "wpa_psk_file", wpa_psk_file, sizeof(wpa_psk_file));
developer72fb0bb2023-01-11 09:46:29 +080011091
developera3511852023-06-14 14:12:59 +080011092 if (strlen(wpa_psk_file) == 0)
developer32f2a182023-06-27 19:50:41 +080011093 memcpy(wpa_psk_file, PSK_FILE, strlen(PSK_FILE));
developer72fb0bb2023-01-11 09:46:29 +080011094
developera3511852023-06-14 14:12:59 +080011095 if (access(wpa_psk_file, F_OK) != 0) {
developerd14dff12023-06-28 22:47:44 +080011096 res = snprintf(cmd, sizeof(cmd), "touch %s", wpa_psk_file);
developere40952c2023-06-15 18:46:43 +080011097 if (os_snprintf_error(sizeof(cmd), res)) {
11098 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11099 return RETURN_ERR;
11100 }
developera3511852023-06-14 14:12:59 +080011101 _syscmd(cmd, buf, sizeof(buf));
11102 }
11103 list[3].name = "wpa_psk_file";
11104 list[3].value = wpa_psk_file;
developer72fb0bb2023-01-11 09:46:29 +080011105
developera3511852023-06-14 14:12:59 +080011106 wifi_hostapdRead(original_config_file, "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt));
11107 list[4].name = "wpa_key_mgmt";
11108 list[4].value = wpa_key_mgmt;
developer72fb0bb2023-01-11 09:46:29 +080011109
developera3511852023-06-14 14:12:59 +080011110 wifi_hostapdRead(original_config_file, "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise));
11111 list[5].name = "wpa_pairwise";
11112 list[5].value = wpa_pairwise;
developer72fb0bb2023-01-11 09:46:29 +080011113
developer32f2a182023-06-27 19:50:41 +080011114 res = snprintf(current_config_file, sizeof(current_config_file),
11115 "%s%d.conf", CONFIG_PREFIX, apIndex);
11116 if (os_snprintf_error(sizeof(current_config_file), res)) {
11117 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11118 return RETURN_ERR;
11119 }
developera3511852023-06-14 14:12:59 +080011120 wifi_hostapdWrite(current_config_file, list, 6);
developer72fb0bb2023-01-11 09:46:29 +080011121
developera3511852023-06-14 14:12:59 +080011122 wifi_setApEnable(apIndex, FALSE);
11123 wifi_setApEnable(apIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +080011124
developera3511852023-06-14 14:12:59 +080011125 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11126 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011127}
11128
11129//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).
11130INT wifi_getApSecurityRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11131{
developera3511852023-06-14 14:12:59 +080011132 char config_file[64] = {0};
11133 char buf[64] = {0};
11134 char cmd[256] = {0};
developere40952c2023-06-15 18:46:43 +080011135 int res;
developer72fb0bb2023-01-11 09:46:29 +080011136
developera3511852023-06-14 14:12:59 +080011137 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011138
developera3511852023-06-14 14:12:59 +080011139 if(!IP_output || !Port_output || !RadiusSecret_output)
11140 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011141
developera3511852023-06-14 14:12:59 +080011142 // Read the first matched config
developere40952c2023-06-15 18:46:43 +080011143 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11144 if (os_snprintf_error(sizeof(config_file), res)) {
11145 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11146 return RETURN_ERR;
11147 }
developer32f2a182023-06-27 19:50:41 +080011148 res = snprintf(cmd, sizeof(cmd),
11149 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
developer75bd10c2023-06-27 11:34:08 +080011150 if (os_snprintf_error(sizeof(cmd), res)) {
11151 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11152 return RETURN_ERR;
11153 }
developera3511852023-06-14 14:12:59 +080011154 _syscmd(cmd, buf, sizeof(buf));
11155 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011156
developera3511852023-06-14 14:12:59 +080011157 memset(buf, 0, sizeof(buf));
developer75bd10c2023-06-27 11:34:08 +080011158 res = snprintf(cmd, sizeof(cmd), "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11159 if (os_snprintf_error(sizeof(cmd), res)) {
11160 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11161 return RETURN_ERR;
11162 }
developera3511852023-06-14 14:12:59 +080011163 _syscmd(cmd, buf, sizeof(buf));
11164 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011165
developera3511852023-06-14 14:12:59 +080011166 memset(buf, 0, sizeof(buf));
developer75bd10c2023-06-27 11:34:08 +080011167 res = snprintf(cmd, sizeof(cmd), "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11168 if (os_snprintf_error(sizeof(cmd), res)) {
11169 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11170 return RETURN_ERR;
11171 }
developera3511852023-06-14 14:12:59 +080011172 _syscmd(cmd, buf, sizeof(buf));
11173 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011174
developera3511852023-06-14 14:12:59 +080011175 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11176 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011177}
11178
11179INT wifi_setApSecurityRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11180{
developera3511852023-06-14 14:12:59 +080011181 char config_file[64] = {0};
11182 char port_str[8] = {0};
11183 char cmd[256] = {0};
11184 char buf[128] = {0};
11185 int res;
developer72fb0bb2023-01-11 09:46:29 +080011186
developera3511852023-06-14 14:12:59 +080011187 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011188
developere5750452023-05-15 16:46:42 +080011189 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011190 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011191
developera3511852023-06-14 14:12:59 +080011192 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11193 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011194
developera3511852023-06-14 14:12:59 +080011195 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11196 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011197 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11198 return RETURN_ERR;
11199 }
developer72fb0bb2023-01-11 09:46:29 +080011200
developera3511852023-06-14 14:12:59 +080011201 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 1'", config_file);
11202 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011203 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11204 return RETURN_ERR;
11205 }
developera3511852023-06-14 14:12:59 +080011206 _syscmd(cmd, buf, sizeof(buf));
11207 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011208
developere40952c2023-06-15 18:46:43 +080011209 res = snprintf(port_str, sizeof(port_str), "%d", port);
developer37646972023-06-29 10:58:43 +080011210 if (os_snprintf_error(sizeof(port_str), res)) {
11211 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11212 return RETURN_ERR;
11213 }
developera3511852023-06-14 14:12:59 +080011214 if (strlen(buf) == 0) {
11215 // Append
11216 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 1\\n"
11217 "auth_server_addr=%s\\n"
11218 "auth_server_port=%s\\n"
11219 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11220 if (os_snprintf_error(sizeof(cmd), res)) {
11221 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11222 return RETURN_ERR;
11223 }
11224 } else {
11225 // Delete the three lines setting after the "# radius 1" comment
11226 res = snprintf(cmd, sizeof(cmd), "sed -i '/# radius 1/{n;N;N;d}' %s", config_file);
11227 if (os_snprintf_error(sizeof(cmd), res)) {
11228 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11229 return RETURN_ERR;
11230 }
11231 _syscmd(cmd, buf, sizeof(buf));
11232 memset(cmd, 0, sizeof(cmd));
11233 // Use "# radius 1" comment to find the location to insert the radius setting
11234 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 1/"
11235 "# radius 1\\n"
11236 "auth_server_addr=%s\\n"
11237 "auth_server_port=%s\\n"
11238 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11239 if (os_snprintf_error(sizeof(cmd), res)) {
11240 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11241 return RETURN_ERR;
11242 }
11243 }
11244 if(_syscmd(cmd, buf, sizeof(buf))) {
11245 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11246 return RETURN_ERR;
11247 }
developer72fb0bb2023-01-11 09:46:29 +080011248
developera3511852023-06-14 14:12:59 +080011249 wifi_reloadAp(apIndex);
11250 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11251 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011252}
11253
11254INT wifi_getApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11255{
developera3511852023-06-14 14:12:59 +080011256 char config_file[64] = {0};
11257 char buf[64] = {0};
11258 char cmd[256] = {0};
developere40952c2023-06-15 18:46:43 +080011259 int res;
developer72fb0bb2023-01-11 09:46:29 +080011260
developera3511852023-06-14 14:12:59 +080011261 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011262
developera3511852023-06-14 14:12:59 +080011263 if(!IP_output || !Port_output || !RadiusSecret_output)
11264 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011265
developera3511852023-06-14 14:12:59 +080011266 // Read the second matched config
developere40952c2023-06-15 18:46:43 +080011267 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11268 if (os_snprintf_error(sizeof(config_file), res)) {
11269 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11270 return RETURN_ERR;
11271 }
11272
developer32f2a182023-06-27 19:50:41 +080011273 res = snprintf(cmd, sizeof(cmd),
11274 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11275 config_file);
developer75bd10c2023-06-27 11:34:08 +080011276 if (os_snprintf_error(sizeof(cmd), res)) {
11277 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11278 return RETURN_ERR;
11279 }
developera3511852023-06-14 14:12:59 +080011280 _syscmd(cmd, buf, sizeof(buf));
11281 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011282
developera3511852023-06-14 14:12:59 +080011283 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080011284 res = snprintf(cmd, sizeof(cmd),
11285 "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11286 config_file);
developer75bd10c2023-06-27 11:34:08 +080011287 if (os_snprintf_error(sizeof(cmd), res)) {
11288 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11289 return RETURN_ERR;
11290 }
developera3511852023-06-14 14:12:59 +080011291 _syscmd(cmd, buf, sizeof(buf));
11292 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011293
developera3511852023-06-14 14:12:59 +080011294 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080011295 res = snprintf(cmd, sizeof(cmd),
11296 "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11297 config_file);
developer75bd10c2023-06-27 11:34:08 +080011298 if (os_snprintf_error(sizeof(cmd), res)) {
11299 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11300 return RETURN_ERR;
11301 }
11302
developera3511852023-06-14 14:12:59 +080011303 _syscmd(cmd, buf, sizeof(buf));
11304 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011305
developera3511852023-06-14 14:12:59 +080011306 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11307 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011308}
11309
11310INT wifi_setApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11311{
developera3511852023-06-14 14:12:59 +080011312 char config_file[64] = {0};
11313 char port_str[8] = {0};
11314 char cmd[256] = {0};
11315 char buf[128] = {0};
11316 int res;
developer72fb0bb2023-01-11 09:46:29 +080011317
developera3511852023-06-14 14:12:59 +080011318 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011319
developere5750452023-05-15 16:46:42 +080011320 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011321 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011322
developera3511852023-06-14 14:12:59 +080011323 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11324 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011325
developera3511852023-06-14 14:12:59 +080011326 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11327 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011328 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11329 return RETURN_ERR;
11330 }
developer72fb0bb2023-01-11 09:46:29 +080011331
developera3511852023-06-14 14:12:59 +080011332 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 2'", config_file);
11333 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011334 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11335 return RETURN_ERR;
11336 }
developera3511852023-06-14 14:12:59 +080011337 _syscmd(cmd, buf, sizeof(buf));
11338 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011339
developera3511852023-06-14 14:12:59 +080011340 res = snprintf(port_str, sizeof(port_str), "%d", port);
11341 if (os_snprintf_error(sizeof(port_str), res)) {
developer46506162023-06-12 10:09:39 +080011342 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11343 return RETURN_ERR;
11344 }
developera3511852023-06-14 14:12:59 +080011345 if (strlen(buf) == 0) {
11346 // Append
11347 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 2\\n"
11348 "auth_server_addr=%s\\n"
11349 "auth_server_port=%s\\n"
11350 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11351 if (os_snprintf_error(sizeof(cmd), res)) {
11352 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11353 return RETURN_ERR;
11354 }
11355 } else {
11356 // Delete the three lines setting after the "# radius 2" comment
11357 res = snprintf(cmd, sizeof(cmd), "sed -i '/# radius 2/{n;N;N;d}' %s", config_file);
11358 if (os_snprintf_error(sizeof(cmd), res)) {
11359 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11360 return RETURN_ERR;
11361 }
11362 _syscmd(cmd, buf, sizeof(buf));
11363 memset(cmd, 0, sizeof(cmd));
11364 // Use "# radius 2" comment to find the location to insert the radius setting
11365 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 2/"
11366 "# radius 2\\n"
11367 "auth_server_addr=%s\\n"
11368 "auth_server_port=%s\\n"
11369 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11370 if (os_snprintf_error(sizeof(cmd), res)) {
11371 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11372 return RETURN_ERR;
11373 }
11374 }
11375 if(_syscmd(cmd, buf, sizeof(buf))) {
11376 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11377 return RETURN_ERR;
11378 }
developer72fb0bb2023-01-11 09:46:29 +080011379
developera3511852023-06-14 14:12:59 +080011380 wifi_reloadAp(apIndex);
11381 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11382 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011383}
11384
11385//RadiusSettings
11386INT wifi_getApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *output)
11387{
developera3511852023-06-14 14:12:59 +080011388 if(!output)
11389 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011390
developera3511852023-06-14 14:12:59 +080011391 output->RadiusServerRetries = 3; //Number of retries for Radius requests.
11392 output->RadiusServerRequestTimeout = 5; //Radius request timeout in seconds after which the request must be retransmitted for the # of retries available.
11393 output->PMKLifetime = 28800; //Default time in seconds after which a Wi-Fi client is forced to ReAuthenticate (def 8 hrs).
11394 output->PMKCaching = FALSE; //Enable or disable caching of PMK.
11395 output->PMKCacheInterval = 300; //Time interval in seconds after which the PMKSA (Pairwise Master Key Security Association) cache is purged (def 5 minutes).
11396 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.
11397 output->BlacklistTableTimeout = 600; //Time interval in seconds for which a client will continue to be blacklisted once it is marked so.
11398 output->IdentityRequestRetryInterval = 5; //Time Interval in seconds between identity requests retries. A value of 0 (zero) disables it.
11399 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 +080011400 //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 +080011401
developera3511852023-06-14 14:12:59 +080011402 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011403}
11404
11405INT wifi_setApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *input)
11406{
developera3511852023-06-14 14:12:59 +080011407 //store the paramters, and apply instantly
11408 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011409}
11410
11411//Device.WiFi.AccessPoint.{i}.WPS.Enable
11412//Enables or disables WPS functionality for this access point.
11413// outputs the WPS enable state of this ap in output_bool
11414INT wifi_getApWpsEnable(INT apIndex, BOOL *output_bool)
11415{
developera3511852023-06-14 14:12:59 +080011416 char interface_name[16] = {0};
11417 char buf[MAX_BUF_SIZE] = {0}, cmd[MAX_CMD_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080011418 int res;
11419
developera3511852023-06-14 14:12:59 +080011420 if(!output_bool)
11421 return RETURN_ERR;
11422 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11423 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080011424 res = snprintf(cmd, sizeof(cmd),
11425 "hostapd_cli -i %s get_config | grep wps_state | cut -d '=' -f2",
11426 interface_name);
developer75bd10c2023-06-27 11:34:08 +080011427 if (os_snprintf_error(sizeof(cmd), res)) {
11428 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11429 return RETURN_ERR;
11430 }
developera3511852023-06-14 14:12:59 +080011431 _syscmd(cmd, buf, sizeof(buf));
11432 if(strstr(buf, "configured"))
11433 *output_bool=TRUE;
11434 else
11435 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +080011436
developera3511852023-06-14 14:12:59 +080011437 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080011438}
developer72fb0bb2023-01-11 09:46:29 +080011439
11440//Device.WiFi.AccessPoint.{i}.WPS.Enable
11441// sets the WPS enable enviornment variable for this ap to the value of enableValue, 1==enabled, 0==disabled
11442INT wifi_setApWpsEnable(INT apIndex, BOOL enable)
11443{
developera3511852023-06-14 14:12:59 +080011444 char config_file[MAX_BUF_SIZE] = {0};
11445 char buf[128] = {0};
11446 struct params params;
developere40952c2023-06-15 18:46:43 +080011447 int res;
developer72fb0bb2023-01-11 09:46:29 +080011448
developera3511852023-06-14 14:12:59 +080011449 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11450 //store the paramters, and wait for wifi up to apply
11451 params.name = "wps_state";
11452 if (enable == TRUE) {
11453 wifi_getApBeaconType(apIndex, buf);
11454 if (strncmp(buf, "None", 4) == 0) // If ap didn't set encryption
11455 params.value = "1";
11456 else // If ap set encryption
11457 params.value = "2";
11458 } else {
11459 params.value = "0";
11460 }
developer72fb0bb2023-01-11 09:46:29 +080011461
developere40952c2023-06-15 18:46:43 +080011462 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11463 if (os_snprintf_error(sizeof(config_file), res)) {
11464 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11465 return RETURN_ERR;
11466 }
developera3511852023-06-14 14:12:59 +080011467 wifi_hostapdWrite(config_file, &params, 1);
11468 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11469 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080011470
developera3511852023-06-14 14:12:59 +080011471 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11472 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011473}
11474
11475//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
11476INT wifi_getApWpsConfigMethodsSupported(INT apIndex, CHAR *output)
11477{
developere40952c2023-06-15 18:46:43 +080011478 int res;
developera3511852023-06-14 14:12:59 +080011479 if(!output)
11480 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011481 res = snprintf(output, 128, "PushButton,PIN");
11482 if (os_snprintf_error(128, res)) {
11483 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11484 return RETURN_ERR;
11485 }
11486
developera3511852023-06-14 14:12:59 +080011487 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011488}
11489
11490//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11491//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.
11492// Outputs a common separated list of the enabled WPS config methods, 64 bytes max
11493INT wifi_getApWpsConfigMethodsEnabled(INT apIndex, CHAR *output)
11494{
developere40952c2023-06-15 18:46:43 +080011495 int res;
developera3511852023-06-14 14:12:59 +080011496 if(!output)
11497 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011498 res = snprintf(output, 64, "PushButton,PIN");//Currently, supporting these two methods
11499 if (os_snprintf_error(64, res)) {
11500 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11501 return RETURN_ERR;
11502 }
developer72fb0bb2023-01-11 09:46:29 +080011503
developera3511852023-06-14 14:12:59 +080011504 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011505}
11506
11507//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11508// 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
11509INT wifi_setApWpsConfigMethodsEnabled(INT apIndex, CHAR *methodString)
11510{
developera3511852023-06-14 14:12:59 +080011511 //apply instantly. No setting need to be stored.
11512 char methods[MAX_BUF_SIZE], *token, *next_token;
11513 char config_file[MAX_BUF_SIZE], config_methods[MAX_BUF_SIZE] = {0};
11514 struct params params;
developere40952c2023-06-15 18:46:43 +080011515 int res;
developer72fb0bb2023-01-11 09:46:29 +080011516
developera3511852023-06-14 14:12:59 +080011517 if(!methodString)
11518 return RETURN_ERR;
11519 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11520 //store the paramters, and wait for wifi up to apply
developer72fb0bb2023-01-11 09:46:29 +080011521
developere40952c2023-06-15 18:46:43 +080011522 res = snprintf(methods, sizeof(methods), "%s", methodString);
11523 if (os_snprintf_error(sizeof(methods), res)) {
11524 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11525 return RETURN_ERR;
11526 }
developera3511852023-06-14 14:12:59 +080011527 for(token=methods; *token; token=next_token) {
11528 strtok_r(token, ",", &next_token);
11529 if(*token=='U' && !strcmp(methods, "USBFlashDrive"))
developere40952c2023-06-15 18:46:43 +080011530 res = snprintf(config_methods, sizeof(config_methods), "%s ", "usba");
developera3511852023-06-14 14:12:59 +080011531 else if(*token=='E')
11532 {
11533 if(!strcmp(methods, "Ethernet"))
developere40952c2023-06-15 18:46:43 +080011534 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ethernet");
developera3511852023-06-14 14:12:59 +080011535 else if(!strcmp(methods, "ExternalNFCToken"))
developere40952c2023-06-15 18:46:43 +080011536 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ext_nfc_token");
developera3511852023-06-14 14:12:59 +080011537 else
11538 printf("%s: Unknown WpsConfigMethod\n", __func__);
11539 }
11540 else if(*token=='I' && !strcmp(token, "IntegratedNFCToken"))
developere40952c2023-06-15 18:46:43 +080011541 res = snprintf(config_methods, sizeof(config_methods), "%s ", "int_nfc_token");
developera3511852023-06-14 14:12:59 +080011542 else if(*token=='N' && !strcmp(token, "NFCInterface"))
developere40952c2023-06-15 18:46:43 +080011543 res = snprintf(config_methods, sizeof(config_methods), "%s ", "nfc_interface");
developera3511852023-06-14 14:12:59 +080011544 else if(*token=='P' )
11545 {
11546 if(!strcmp(token, "PushButton"))
developere40952c2023-06-15 18:46:43 +080011547 res = snprintf(config_methods, sizeof(config_methods), "%s ", "push_button");
developera3511852023-06-14 14:12:59 +080011548 else if(!strcmp(token, "PIN"))
developere40952c2023-06-15 18:46:43 +080011549 res = snprintf(config_methods, sizeof(config_methods), "%s ", "keypad");
developera3511852023-06-14 14:12:59 +080011550 else
11551 printf("%s: Unknown WpsConfigMethod\n", __func__);
11552 }
11553 else
11554 printf("%s: Unknown WpsConfigMethod\n", __func__);
developere40952c2023-06-15 18:46:43 +080011555
developer37646972023-06-29 10:58:43 +080011556 if (os_snprintf_error(sizeof(config_methods), res)) {
11557 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11558 return RETURN_ERR;
11559 }
11560 }
developera3511852023-06-14 14:12:59 +080011561 params.name = "config_methods";
11562 params.value = config_methods;
developere40952c2023-06-15 18:46:43 +080011563 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11564 if (os_snprintf_error(sizeof(config_file), res)) {
11565 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11566 return RETURN_ERR;
11567 }
developera3511852023-06-14 14:12:59 +080011568 wifi_hostapdWrite(config_file, &params, 1);
11569 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11570 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011571
developera3511852023-06-14 14:12:59 +080011572 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011573}
11574
11575// outputs the pin value, ulong_pin must be allocated by the caller
11576INT wifi_getApWpsDevicePIN(INT apIndex, ULONG *output_ulong)
11577{
developera3511852023-06-14 14:12:59 +080011578 char buf[MAX_BUF_SIZE] = {0};
11579 char cmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080011580 int res;
developer72fb0bb2023-01-11 09:46:29 +080011581
developera3511852023-06-14 14:12:59 +080011582 if(!output_ulong)
11583 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011584 res = snprintf(cmd, sizeof(cmd), "cat %s%d.conf | grep ap_pin | cut -d '=' -f2", CONFIG_PREFIX, apIndex);
11585 if (os_snprintf_error(sizeof(cmd), res)) {
11586 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11587 return RETURN_ERR;
11588 }
developera3511852023-06-14 14:12:59 +080011589 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +080011590 if(strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080011591 if (hal_strtoul(buf, 10, output_ulong) < 0) {
11592 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080011593 }
11594 }
developer72fb0bb2023-01-11 09:46:29 +080011595
developera3511852023-06-14 14:12:59 +080011596 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011597}
11598
11599// set an enviornment variable for the WPS pin for the selected AP. Normally, Device PIN should not be changed.
11600INT wifi_setApWpsDevicePIN(INT apIndex, ULONG pin)
11601{
developera3511852023-06-14 14:12:59 +080011602 //set the pin to wifi config and hostpad config. wait for wifi reset or hostapd reset to apply
11603 char ap_pin[16] = {0};
11604 char config_file[MAX_BUF_SIZE] = {0};
11605 struct params params;
developere40952c2023-06-15 18:46:43 +080011606 int res;
developer72fb0bb2023-01-11 09:46:29 +080011607
developera3511852023-06-14 14:12:59 +080011608 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011609 res = snprintf(ap_pin, sizeof(ap_pin), "%lu", pin);
11610 if (os_snprintf_error(sizeof(ap_pin), res)) {
11611 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11612 return RETURN_ERR;
11613 }
developera3511852023-06-14 14:12:59 +080011614 params.name = "ap_pin";
11615 params.value = ap_pin;
developere40952c2023-06-15 18:46:43 +080011616 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11617 if (os_snprintf_error(sizeof(config_file), res)) {
11618 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11619 return RETURN_ERR;
11620 }
developera3511852023-06-14 14:12:59 +080011621 wifi_hostapdWrite(config_file, &params, 1);
11622 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11623 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011624
developera3511852023-06-14 14:12:59 +080011625 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011626}
11627
11628// Output string is either Not configured or Configured, max 32 characters
11629INT wifi_getApWpsConfigurationState(INT apIndex, CHAR *output_string)
11630{
developera3511852023-06-14 14:12:59 +080011631 char interface_name[16] = {0};
11632 char cmd[MAX_CMD_SIZE];
11633 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011634 int res;
developer72fb0bb2023-01-11 09:46:29 +080011635
developera3511852023-06-14 14:12:59 +080011636 if(!output_string)
11637 return RETURN_ERR;
11638 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011639 res = snprintf(output_string, 32, "Not configured");
11640 if (os_snprintf_error(32, res)) {
11641 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11642 return RETURN_ERR;
11643 }
developera3511852023-06-14 14:12:59 +080011644 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11645 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011646 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep wps_state | cut -d'=' -f2", interface_name);
11647 if (os_snprintf_error(sizeof(cmd), res)) {
11648 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11649 return RETURN_ERR;
11650 }
developera3511852023-06-14 14:12:59 +080011651 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011652
developerc14d83a2023-06-29 20:09:42 +080011653 if(!strncmp(buf, "configured", 10)) {
developere40952c2023-06-15 18:46:43 +080011654 res = snprintf(output_string, 32, "Configured");
developerc14d83a2023-06-29 20:09:42 +080011655 if (os_snprintf_error(32, res)) {
11656 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11657 return RETURN_ERR;
11658 }
11659 }
developera3511852023-06-14 14:12:59 +080011660 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011661
developera3511852023-06-14 14:12:59 +080011662 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011663}
11664
11665// sets the WPS pin for this AP
11666INT wifi_setApWpsEnrolleePin(INT apIndex, CHAR *pin)
11667{
developera3511852023-06-14 14:12:59 +080011668 char interface_name[16] = {0};
11669 char cmd[MAX_CMD_SIZE];
11670 char buf[MAX_BUF_SIZE]={0};
developer9ce44382023-06-28 11:09:37 +080011671 BOOL enable = 0;
developere40952c2023-06-15 18:46:43 +080011672 int res;
developer72fb0bb2023-01-11 09:46:29 +080011673
developera3511852023-06-14 14:12:59 +080011674 wifi_getApEnable(apIndex, &enable);
11675 if (!enable)
11676 return RETURN_ERR;
11677 wifi_getApWpsEnable(apIndex, &enable);
11678 if (!enable)
11679 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011680
developera3511852023-06-14 14:12:59 +080011681 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11682 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011683 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_pin any %s", interface_name, pin);
11684 if (os_snprintf_error(sizeof(cmd), res)) {
11685 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11686 return RETURN_ERR;
11687 }
developera3511852023-06-14 14:12:59 +080011688 _syscmd(cmd, buf, sizeof(buf));
11689 if((strstr(buf, "OK"))!=NULL)
11690 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011691
developera3511852023-06-14 14:12:59 +080011692 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011693}
11694
11695// This function is called when the WPS push button has been pressed for this AP
11696INT wifi_setApWpsButtonPush(INT apIndex)
11697{
developera3511852023-06-14 14:12:59 +080011698 char cmd[MAX_CMD_SIZE];
11699 char buf[MAX_BUF_SIZE]={0};
11700 char interface_name[16] = {0};
11701 BOOL enable=FALSE;
developere40952c2023-06-15 18:46:43 +080011702 int res;
developer72fb0bb2023-01-11 09:46:29 +080011703
developera3511852023-06-14 14:12:59 +080011704 wifi_getApEnable(apIndex, &enable);
11705 if (!enable)
11706 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011707
developera3511852023-06-14 14:12:59 +080011708 wifi_getApWpsEnable(apIndex, &enable);
11709 if (!enable)
11710 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011711
developera3511852023-06-14 14:12:59 +080011712 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11713 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011714
developere40952c2023-06-15 18:46:43 +080011715 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_cancel; hostapd_cli -i%s wps_pbc", interface_name, interface_name);
11716 if (os_snprintf_error(sizeof(cmd), res)) {
11717 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11718 return RETURN_ERR;
11719 }
developera3511852023-06-14 14:12:59 +080011720 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011721
developera3511852023-06-14 14:12:59 +080011722 if((strstr(buf, "OK"))!=NULL)
11723 return RETURN_OK;
11724 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011725}
11726
11727// cancels WPS mode for this AP
11728INT wifi_cancelApWPS(INT apIndex)
11729{
developera3511852023-06-14 14:12:59 +080011730 char interface_name[16] = {0};
11731 char cmd[MAX_CMD_SIZE];
11732 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011733 int res;
developer72fb0bb2023-01-11 09:46:29 +080011734
developera3511852023-06-14 14:12:59 +080011735 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11736 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011737 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_cancel", interface_name);
11738 if (os_snprintf_error(sizeof(cmd), res)) {
11739 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11740 return RETURN_ERR;
11741 }
developera3511852023-06-14 14:12:59 +080011742 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011743
developera3511852023-06-14 14:12:59 +080011744 if((strstr(buf, "OK"))!=NULL)
11745 return RETURN_OK;
11746 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011747}
11748
11749//Device.WiFi.AccessPoint.{i}.AssociatedDevice.*
11750//HAL funciton should allocate an data structure array, and return to caller with "associated_dev_array"
11751INT wifi_getApAssociatedDeviceDiagnosticResult(INT apIndex, wifi_associated_dev_t **associated_dev_array, UINT *output_array_size)
11752{
developera3511852023-06-14 14:12:59 +080011753 char interface_name[16] = {0};
11754 FILE *f = NULL;
11755 int read_flag=0, auth_temp=0, mac_temp=0;
11756 char cmd[256] = {0}, buf[2048] = {0};
11757 char *param = NULL, *value = NULL, *line=NULL;
11758 size_t len = 0;
11759 wifi_associated_dev_t *dev=NULL;
11760 int res;
developer72fb0bb2023-01-11 09:46:29 +080011761
developera3511852023-06-14 14:12:59 +080011762 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11763 *associated_dev_array = NULL;
11764 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11765 return RETURN_ERR;
11766 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
11767 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011768 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11769 return RETURN_ERR;
11770 }
developera3511852023-06-14 14:12:59 +080011771 _syscmd(cmd,buf,sizeof(buf));
11772 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011773
developera3511852023-06-14 14:12:59 +080011774 if (*output_array_size <= 0)
11775 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011776
developera3511852023-06-14 14:12:59 +080011777 dev=(wifi_associated_dev_t *) calloc (*output_array_size, sizeof(wifi_associated_dev_t));
developere75ba632023-06-29 16:03:33 +080011778 if (!dev) {
11779 wifi_debug(DEBUG_ERROR, "Unexpected calloc fail\n");
11780 return RETURN_ERR;
11781 }
developera3511852023-06-14 14:12:59 +080011782 *associated_dev_array = dev;
11783 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta > /tmp/connected_devices.txt" , interface_name);
11784 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011785 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11786 return RETURN_ERR;
11787 }
developera3511852023-06-14 14:12:59 +080011788 _syscmd(cmd,buf,sizeof(buf));
11789 f = fopen("/tmp/connected_devices.txt", "r");
11790 if (f==NULL)
11791 {
11792 *output_array_size=0;
11793 return RETURN_ERR;
11794 }
11795 while ((getline(&line, &len, f)) != -1)
11796 {
11797 param = strtok(line,"=");
developere75ba632023-06-29 16:03:33 +080011798 if (!param)
11799 continue;
developera3511852023-06-14 14:12:59 +080011800 value = strtok(NULL,"=");
developere75ba632023-06-29 16:03:33 +080011801 if (!value)
11802 continue;
developer72fb0bb2023-01-11 09:46:29 +080011803
developera3511852023-06-14 14:12:59 +080011804 if( strcmp("flags",param) == 0 )
11805 {
11806 value[strlen(value)-1]='\0';
11807 if(strstr (value,"AUTHORIZED") != NULL )
11808 {
11809 dev[auth_temp].cli_AuthenticationState = 1;
11810 dev[auth_temp].cli_Active = 1;
11811 auth_temp++;
11812 read_flag=1;
11813 }
11814 }
11815 if(read_flag==1)
11816 {
11817 if( strcmp("dot11RSNAStatsSTAAddress",param) == 0 )
11818 {
11819 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080011820 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
developera3511852023-06-14 14:12:59 +080011821 (unsigned int *)&dev[mac_temp].cli_MACAddress[0],
11822 (unsigned int *)&dev[mac_temp].cli_MACAddress[1],
11823 (unsigned int *)&dev[mac_temp].cli_MACAddress[2],
11824 (unsigned int *)&dev[mac_temp].cli_MACAddress[3],
11825 (unsigned int *)&dev[mac_temp].cli_MACAddress[4],
developere75ba632023-06-29 16:03:33 +080011826 (unsigned int *)&dev[mac_temp].cli_MACAddress[5] ) == EOF)
11827 continue;
developera3511852023-06-14 14:12:59 +080011828 mac_temp++;
11829 read_flag=0;
11830 }
11831 }
11832 }
11833 *output_array_size = auth_temp;
11834 auth_temp=0;
11835 mac_temp=0;
11836 free(line);
developere75ba632023-06-29 16:03:33 +080011837 if (fclose(f) == EOF) {
11838 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
11839 return RETURN_ERR;
11840 }
developera3511852023-06-14 14:12:59 +080011841 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11842 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011843}
11844
11845#define MACADDRESS_SIZE 6
11846
11847INT wifihal_AssociatedDevicesstats3(INT apIndex,CHAR *interface_name,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
11848{
developera3511852023-06-14 14:12:59 +080011849 FILE *fp = NULL;
11850 char str[MAX_BUF_SIZE] = {0};
11851 int wificlientindex = 0 ;
11852 int count = 0;
11853 int signalstrength = 0;
11854 int arr[MACADDRESS_SIZE] = {0};
11855 unsigned char mac[MACADDRESS_SIZE] = {0};
11856 UINT wifi_count = 0;
developere40952c2023-06-15 18:46:43 +080011857 int res;
developerc14d83a2023-06-29 20:09:42 +080011858 wifi_associated_dev3_t* temp = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011859
developera3511852023-06-14 14:12:59 +080011860 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11861 *output_array_size = 0;
11862 *associated_dev_array = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011863
developer33f13ba2023-07-12 16:19:06 +080011864 fp = v_secure_popen("r", "iw dev %s station dump | grep %s | wc -l",
11865 interface_name, interface_name);
developera3511852023-06-14 14:12:59 +080011866 if (fp == NULL)
11867 {
11868 printf("Failed to run command inside function %s\n",__FUNCTION__ );
11869 return RETURN_ERR;
11870 }
developer72fb0bb2023-01-11 09:46:29 +080011871
developera3511852023-06-14 14:12:59 +080011872 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080011873 if (fgets(str, sizeof(str)-1, fp) == NULL) {
11874 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080011875 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011876 return RETURN_ERR;
11877 }
developera3511852023-06-14 14:12:59 +080011878 wifi_count = (unsigned int) atoi ( str );
11879 *output_array_size = wifi_count;
11880 printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
developer33f13ba2023-07-12 16:19:06 +080011881 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080011882
developera3511852023-06-14 14:12:59 +080011883 if(wifi_count == 0)
11884 {
11885 return RETURN_OK;
11886 }
11887 else
11888 {
developer9ce44382023-06-28 11:09:37 +080011889 if(wifi_count <= 0 || wifi_count > MAX_ASSOCIATED_STA_NUM){
11890 return RETURN_ERR;
11891 }
developera3511852023-06-14 14:12:59 +080011892 temp = (wifi_associated_dev3_t*)calloc(1, sizeof(wifi_associated_dev3_t)*wifi_count) ;
11893 if(temp == NULL)
11894 {
11895 printf("Error Statement. Insufficient memory \n");
11896 return RETURN_ERR;
11897 }
developer72fb0bb2023-01-11 09:46:29 +080011898
developer33f13ba2023-07-12 16:19:06 +080011899 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
11900 if (res) {
11901 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011902 free(temp);
developere40952c2023-06-15 18:46:43 +080011903 return RETURN_ERR;
11904 }
developer33f13ba2023-07-12 16:19:06 +080011905
developera3511852023-06-14 14:12:59 +080011906 if(apIndex == 0)
developer33f13ba2023-07-12 16:19:06 +080011907 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_2G.txt", interface_name);
developera3511852023-06-14 14:12:59 +080011908 else if(apIndex == 1)
developer33f13ba2023-07-12 16:19:06 +080011909 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", interface_name);
11910 if (res) {
11911 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011912 free(temp);
developere40952c2023-06-15 18:46:43 +080011913 return RETURN_ERR;
11914 }
developer72fb0bb2023-01-11 09:46:29 +080011915
developera3511852023-06-14 14:12:59 +080011916 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
11917 if(fp == NULL)
11918 {
11919 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
11920 free(temp);
11921 return RETURN_ERR;
11922 }
developere75ba632023-06-29 16:03:33 +080011923 if (fclose(fp) == EOF) {
11924 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developerc14d83a2023-06-29 20:09:42 +080011925 free(temp);
developere75ba632023-06-29 16:03:33 +080011926 return RETURN_ERR;
11927 }
developer72fb0bb2023-01-11 09:46:29 +080011928
developer33f13ba2023-07-12 16:19:06 +080011929 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
developera3511852023-06-14 14:12:59 +080011930 if(fp)
11931 {
11932 for(count =0 ; count < wifi_count; count++)
11933 {
developer86035662023-06-28 19:21:12 +080011934 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11935 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011936 goto err;
developer86035662023-06-28 19:21:12 +080011937 }
developera3511852023-06-14 14:12:59 +080011938 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
11939 {
11940 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
11941 {
11942 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080011943
developera3511852023-06-14 14:12:59 +080011944 }
11945 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
11946 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]);
11947 }
11948 temp[count].cli_AuthenticationState = 1; //TODO
11949 temp[count].cli_Active = 1; //TODO
11950 }
developer33f13ba2023-07-12 16:19:06 +080011951 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011952 }
11953
developer33f13ba2023-07-12 16:19:06 +080011954 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 +080011955 if(fp)
11956 {
developer33f13ba2023-07-12 16:19:06 +080011957 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011958 }
developer33f13ba2023-07-12 16:19:06 +080011959 fp = v_secure_popen("r", "cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011960 if(fp)
11961 {
11962 for(count =0 ; count < wifi_count ;count++)
11963 {
developer86035662023-06-28 19:21:12 +080011964 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11965 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011966 goto err;
developer86035662023-06-28 19:21:12 +080011967 }
developera3511852023-06-14 14:12:59 +080011968 signalstrength = atoi(str);
11969 temp[count].cli_SignalStrength = signalstrength;
11970 temp[count].cli_RSSI = signalstrength;
11971 temp[count].cli_SNR = signalstrength + 95;
11972 }
developer33f13ba2023-07-12 16:19:06 +080011973 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011974 }
developer72fb0bb2023-01-11 09:46:29 +080011975
11976
developera3511852023-06-14 14:12:59 +080011977 if((apIndex == 0) || (apIndex == 4))
11978 {
11979 for(count =0 ; count < wifi_count ;count++)
11980 {
developer32f2a182023-06-27 19:50:41 +080011981 memcpy(temp[count].cli_OperatingStandard,"g", 1);
11982 temp[count].cli_OperatingStandard[1] = '\0';
11983 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
11984 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080011985 }
developer72fb0bb2023-01-11 09:46:29 +080011986
developera3511852023-06-14 14:12:59 +080011987 //BytesSent
developer33f13ba2023-07-12 16:19:06 +080011988 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 +080011989 if(fp)
11990 {
developer33f13ba2023-07-12 16:19:06 +080011991 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011992 }
developer33f13ba2023-07-12 16:19:06 +080011993 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011994 if(fp)
11995 {
11996 for (count = 0; count < wifi_count; count++)
11997 {
developer86035662023-06-28 19:21:12 +080011998 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11999 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080012000 v_secure_pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080012001 free(temp);
developer86035662023-06-28 19:21:12 +080012002 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080012003 }
developerc14d83a2023-06-29 20:09:42 +080012004 if (hal_strtoul(str, 10, &(temp[count].cli_BytesSent)) < 0) {
12005 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012006 }
developera3511852023-06-14 14:12:59 +080012007 }
developer33f13ba2023-07-12 16:19:06 +080012008 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012009 }
developer72fb0bb2023-01-11 09:46:29 +080012010
developera3511852023-06-14 14:12:59 +080012011 //BytesReceived
developer86035662023-06-28 19:21:12 +080012012
developer33f13ba2023-07-12 16:19:06 +080012013 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 +080012014 if (fp)
12015 {
developer33f13ba2023-07-12 16:19:06 +080012016 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012017 }
developer33f13ba2023-07-12 16:19:06 +080012018 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012019 if (fp)
12020 {
12021 for (count = 0; count < wifi_count; count++)
12022 {
developer86035662023-06-28 19:21:12 +080012023 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12024 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012025 goto err;
12026 }
12027 if (hal_strtoul(str, 10, &(temp[count].cli_BytesReceived)) < 0) {
12028 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012029 }
developera3511852023-06-14 14:12:59 +080012030 }
developer33f13ba2023-07-12 16:19:06 +080012031 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012032 }
developer72fb0bb2023-01-11 09:46:29 +080012033
developera3511852023-06-14 14:12:59 +080012034 //PacketsSent
developer33f13ba2023-07-12 16:19:06 +080012035 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 +080012036 if (fp)
12037 {
developer33f13ba2023-07-12 16:19:06 +080012038 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012039 }
developer72fb0bb2023-01-11 09:46:29 +080012040
developer33f13ba2023-07-12 16:19:06 +080012041 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012042 if (fp)
12043 {
12044 for (count = 0; count < wifi_count; count++)
12045 {
developer86035662023-06-28 19:21:12 +080012046 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12047 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012048 goto err;
developer5b23cd02023-07-19 20:26:03 +080012049 }
developerc14d83a2023-06-29 20:09:42 +080012050 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsSent)) < 0) {
12051 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012052 }
developera3511852023-06-14 14:12:59 +080012053 }
developer33f13ba2023-07-12 16:19:06 +080012054 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012055 }
developer72fb0bb2023-01-11 09:46:29 +080012056
developera3511852023-06-14 14:12:59 +080012057 //PacketsReceived
developer33f13ba2023-07-12 16:19:06 +080012058 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 +080012059 if (fp)
12060 {
developer33f13ba2023-07-12 16:19:06 +080012061 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012062 }
developer33f13ba2023-07-12 16:19:06 +080012063 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012064 if (fp)
12065 {
12066 for (count = 0; count < wifi_count; count++)
12067 {
developer86035662023-06-28 19:21:12 +080012068 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12069 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012070 goto err;
developer5b23cd02023-07-19 20:26:03 +080012071 }
developerc14d83a2023-06-29 20:09:42 +080012072 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsReceived)) < 0) {
12073 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012074 }
developera3511852023-06-14 14:12:59 +080012075 }
developer33f13ba2023-07-12 16:19:06 +080012076 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012077 }
developer72fb0bb2023-01-11 09:46:29 +080012078
developera3511852023-06-14 14:12:59 +080012079 //ErrorsSent
developer33f13ba2023-07-12 16:19:06 +080012080 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 +080012081 if (fp)
12082 {
developer33f13ba2023-07-12 16:19:06 +080012083 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012084 }
developer33f13ba2023-07-12 16:19:06 +080012085 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012086 if (fp)
12087 {
12088 for (count = 0; count < wifi_count; count++)
12089 {
developer86035662023-06-28 19:21:12 +080012090 if (fgets(str, MAX_BUF_SIZE, fp) == NULL){
12091 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012092 goto err;
developer5b23cd02023-07-19 20:26:03 +080012093 }
developerc14d83a2023-06-29 20:09:42 +080012094 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
12095 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012096 }
developera3511852023-06-14 14:12:59 +080012097 }
developer33f13ba2023-07-12 16:19:06 +080012098 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012099 }
developer72fb0bb2023-01-11 09:46:29 +080012100
developera3511852023-06-14 14:12:59 +080012101 //ErrorsSent
developer86035662023-06-28 19:21:12 +080012102
developer33f13ba2023-07-12 16:19:06 +080012103 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 +080012104 if (fp)
12105 {
developer33f13ba2023-07-12 16:19:06 +080012106 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012107 }
developer33f13ba2023-07-12 16:19:06 +080012108 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012109 if (fp)
12110 {
12111 for (count = 0; count < wifi_count; count++)
12112 {
developer86035662023-06-28 19:21:12 +080012113 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12114 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012115 goto err;
developer5b23cd02023-07-19 20:26:03 +080012116 }
developerc14d83a2023-06-29 20:09:42 +080012117 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
12118 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012119 }
developera3511852023-06-14 14:12:59 +080012120 }
developer33f13ba2023-07-12 16:19:06 +080012121 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012122 }
developer72fb0bb2023-01-11 09:46:29 +080012123
developera3511852023-06-14 14:12:59 +080012124 //LastDataDownlinkRate
developer33f13ba2023-07-12 16:19:06 +080012125 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 +080012126 if (fp)
12127 {
developer33f13ba2023-07-12 16:19:06 +080012128 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012129 }
developer33f13ba2023-07-12 16:19:06 +080012130 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012131 if (fp)
developer5b23cd02023-07-19 20:26:03 +080012132 {
developerc14d83a2023-06-29 20:09:42 +080012133 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012134 for (count = 0; count < wifi_count; count++)
12135 {
developer86035662023-06-28 19:21:12 +080012136 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12137 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012138 goto err;
12139 }
developer5b23cd02023-07-19 20:26:03 +080012140
developerc14d83a2023-06-29 20:09:42 +080012141 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12142 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012143 }
developerc14d83a2023-06-29 20:09:42 +080012144 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080012145 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
12146 }
developer33f13ba2023-07-12 16:19:06 +080012147 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012148 }
developer72fb0bb2023-01-11 09:46:29 +080012149
developera3511852023-06-14 14:12:59 +080012150 //LastDataUplinkRate
developer33f13ba2023-07-12 16:19:06 +080012151 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 +080012152 if (fp)
12153 {
developer33f13ba2023-07-12 16:19:06 +080012154 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012155 }
developer33f13ba2023-07-12 16:19:06 +080012156 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012157 if (fp)
12158 {
developerc14d83a2023-06-29 20:09:42 +080012159 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012160 for (count = 0; count < wifi_count; count++)
12161 {
developer86035662023-06-28 19:21:12 +080012162 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12163 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012164 goto err;
12165 }
12166 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12167 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012168 }
developerc14d83a2023-06-29 20:09:42 +080012169 temp[count].cli_LastDataUplinkRate = tmp_u;
12170
developera3511852023-06-14 14:12:59 +080012171 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
12172 }
developer33f13ba2023-07-12 16:19:06 +080012173 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012174 }
developer72fb0bb2023-01-11 09:46:29 +080012175
developera3511852023-06-14 14:12:59 +080012176 }
12177 else if ((apIndex == 1) || (apIndex == 5))
12178 {
12179 for (count = 0; count < wifi_count; count++)
12180 {
developer32f2a182023-06-27 19:50:41 +080012181 memcpy(temp[count].cli_OperatingStandard, "a", 1);
12182 temp[count].cli_OperatingStandard[1] = '\0';
12183 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
12184 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080012185 temp[count].cli_BytesSent = 0;
12186 temp[count].cli_BytesReceived = 0;
12187 temp[count].cli_LastDataUplinkRate = 0;
12188 temp[count].cli_LastDataDownlinkRate = 0;
12189 temp[count].cli_PacketsSent = 0;
12190 temp[count].cli_PacketsReceived = 0;
12191 temp[count].cli_ErrorsSent = 0;
12192 }
12193 }
developer72fb0bb2023-01-11 09:46:29 +080012194
developera3511852023-06-14 14:12:59 +080012195 for (count = 0; count < wifi_count; count++)
12196 {
12197 temp[count].cli_Retransmissions = 0;
12198 temp[count].cli_DataFramesSentAck = 0;
12199 temp[count].cli_DataFramesSentNoAck = 0;
12200 temp[count].cli_MinRSSI = 0;
12201 temp[count].cli_MaxRSSI = 0;
12202 strncpy(temp[count].cli_InterferenceSources, "", 64);
12203 memset(temp[count].cli_IPAddress, 0, 64);
12204 temp[count].cli_RetransCount = 0;
12205 temp[count].cli_FailedRetransCount = 0;
12206 temp[count].cli_RetryCount = 0;
12207 temp[count].cli_MultipleRetryCount = 0;
12208 }
12209 *associated_dev_array = temp;
12210 }
12211 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12212 return RETURN_OK;
developerc14d83a2023-06-29 20:09:42 +080012213err:
12214 if (temp)
12215 free(temp);
developer12fb9f62023-06-30 15:26:27 +080012216 pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080012217 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012218}
12219
developer7e4a2a62023-04-06 19:56:03 +080012220int wifihal_interfacestatus(CHAR *wifi_status, CHAR *interface_name)
developer72fb0bb2023-01-11 09:46:29 +080012221{
developera3511852023-06-14 14:12:59 +080012222 char cmd[MAX_CMD_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +080012223 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080012224 int res;
developer32f2a182023-06-27 19:50:41 +080012225 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012226
developera3511852023-06-14 14:12:59 +080012227 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080012228
developere40952c2023-06-15 18:46:43 +080012229 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s | grep RUNNING | tr -s ' ' | cut -d ' ' -f4 | tr -d '\\n'",
developer7e4a2a62023-04-06 19:56:03 +080012230 interface_name);
developere40952c2023-06-15 18:46:43 +080012231 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
12232 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12233 return RETURN_ERR;
12234 }
12235
developera3511852023-06-14 14:12:59 +080012236 _syscmd(cmd, buf, MAX_BUF_SIZE);
developer7e4a2a62023-04-06 19:56:03 +080012237
developer32f2a182023-06-27 19:50:41 +080012238 len = strlen(buf);
12239 if (len >= sizeof(buf)) {
12240 wifi_debug(DEBUG_ERROR, "Unexpected buf size\n");
12241 return RETURN_ERR;
12242 }
12243 strncpy(wifi_status, buf, len); /* TBD: check wifi_status mem lenth and replace with strcpy later */
12244 wifi_status[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012245
developera3511852023-06-14 14:12:59 +080012246 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12247 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012248}
12249
developer72fb0bb2023-01-11 09:46:29 +080012250static const char *get_line_from_str_buf(const char *buf, char *line)
12251{
developera3511852023-06-14 14:12:59 +080012252 int i;
12253 int n = strlen(buf);
developer72fb0bb2023-01-11 09:46:29 +080012254
developera3511852023-06-14 14:12:59 +080012255 for (i = 0; i < n; i++) {
12256 line[i] = buf[i];
12257 if (buf[i] == '\n') {
12258 line[i] = '\0';
12259 return &buf[i + 1];
12260 }
12261 }
developer72fb0bb2023-01-11 09:46:29 +080012262
developera3511852023-06-14 14:12:59 +080012263 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080012264}
12265
12266INT wifi_getApAssociatedDeviceDiagnosticResult3(INT apIndex, wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12267{
developera3511852023-06-14 14:12:59 +080012268 char interface_name[16] = {0};
12269 FILE *f = NULL;
12270 int auth_temp= -1;
12271 char cmd[256] = {0}, buf[2048] = {0};
12272 char *param = NULL, *value = NULL, *line=NULL;
12273 size_t len = 0;
12274 wifi_associated_dev3_t *dev=NULL;
developer75bd10c2023-06-27 11:34:08 +080012275 int res;
developer72fb0bb2023-01-11 09:46:29 +080012276
developera3511852023-06-14 14:12:59 +080012277 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12278 *associated_dev_array = NULL;
12279 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12280 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080012281
12282 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
12283 if (os_snprintf_error(sizeof(cmd), res)) {
12284 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12285 return RETURN_ERR;
12286 }
12287
developera3511852023-06-14 14:12:59 +080012288 _syscmd(cmd, buf, sizeof(buf));
12289 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080012290
developera3511852023-06-14 14:12:59 +080012291 if (*output_array_size <= 0)
12292 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012293
developera3511852023-06-14 14:12:59 +080012294 dev=(wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
developer86035662023-06-28 19:21:12 +080012295
12296 if (dev == NULL) {
12297 wifi_debug(DEBUG_ERROR, "calloc fail\n");
12298 return RETURN_ERR;
12299 }
developera3511852023-06-14 14:12:59 +080012300 *associated_dev_array = dev;
developer32f2a182023-06-27 19:50:41 +080012301 res = snprintf(cmd, sizeof(cmd),
12302 "hostapd_cli -i%s all_sta > /tmp/diagnostic3_devices.txt" , interface_name);
developer75bd10c2023-06-27 11:34:08 +080012303 if (os_snprintf_error(sizeof(cmd), res)) {
12304 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12305 return RETURN_ERR;
12306 }
12307
developera3511852023-06-14 14:12:59 +080012308 _syscmd(cmd,buf,sizeof(buf));
12309 f = fopen("/tmp/diagnostic3_devices.txt", "r");
12310 if (f == NULL)
12311 {
12312 *output_array_size=0;
12313 return RETURN_ERR;
12314 }
12315 while ((getline(&line, &len, f)) != -1)
12316 {
12317 param = strtok(line, "=");
developere75ba632023-06-29 16:03:33 +080012318 if (!param)
12319 continue;
developera3511852023-06-14 14:12:59 +080012320 value = strtok(NULL, "=");
developere75ba632023-06-29 16:03:33 +080012321 if (!value)
12322 continue;
developer72fb0bb2023-01-11 09:46:29 +080012323
developera3511852023-06-14 14:12:59 +080012324 if( strcmp("flags",param) == 0 )
12325 {
12326 value[strlen(value)-1]='\0';
12327 if(strstr (value,"AUTHORIZED") != NULL )
12328 {
12329 auth_temp++;
12330 dev[auth_temp].cli_AuthenticationState = 1;
12331 dev[auth_temp].cli_Active = 1;
12332 }
12333 } else if (auth_temp < 0) {
12334 continue;
12335 } else if( strcmp("dot11RSNAStatsSTAAddress", param) == 0 )
12336 {
12337 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012338 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
12339 (unsigned int *)&dev[auth_temp].cli_MACAddress[0],
12340 (unsigned int *)&dev[auth_temp].cli_MACAddress[1],
12341 (unsigned int *)&dev[auth_temp].cli_MACAddress[2],
12342 (unsigned int *)&dev[auth_temp].cli_MACAddress[3],
12343 (unsigned int *)&dev[auth_temp].cli_MACAddress[4],
12344 (unsigned int *)&dev[auth_temp].cli_MACAddress[5]) == EOF)
12345 continue;
developera3511852023-06-14 14:12:59 +080012346 } else if (strcmp("signal", param) == 0) {
12347 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012348 if (sscanf(value, "%d", &dev[auth_temp].cli_RSSI) == EOF)
12349 continue;
developera3511852023-06-14 14:12:59 +080012350 dev[auth_temp].cli_SNR = 95 + dev[auth_temp].cli_RSSI;
12351 }
12352 }
developer0d26f2c2023-05-25 19:46:36 +080012353 if (line)
developera3511852023-06-14 14:12:59 +080012354 free(line);
developerc14d83a2023-06-29 20:09:42 +080012355
12356 if (fclose(f) != 0) {
12357 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12358 }
12359
developera3511852023-06-14 14:12:59 +080012360 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12361 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012362}
developer72fb0bb2023-01-11 09:46:29 +080012363
12364/* getIPAddress function */
12365/**
12366* @description Returning IpAddress of the Matched String
12367*
developer69b61b02023-03-07 17:17:44 +080012368* @param
developer72fb0bb2023-01-11 09:46:29 +080012369* @str Having MacAddress
developer69b61b02023-03-07 17:17:44 +080012370* @ipaddr Having ipaddr
developer72fb0bb2023-01-11 09:46:29 +080012371* @return The status of the operation
12372* @retval RETURN_OK if successful
12373* @retval RETURN_ERR if any error is detected
12374*
12375*/
12376
12377INT getIPAddress(char *str,char *ipaddr)
12378{
developera3511852023-06-14 14:12:59 +080012379 FILE *fp = NULL;
12380 char buf[1024] = {0},ipAddr[50] = {0},phyAddr[100] = {0},hostName[100] = {0};
12381 int LeaseTime = 0,ret = 0;
developer32f2a182023-06-27 19:50:41 +080012382 unsigned long len;
12383
developera3511852023-06-14 14:12:59 +080012384 if ( (fp=fopen("/nvram/dnsmasq.leases", "r")) == NULL )
12385 {
12386 return RETURN_ERR;
12387 }
developer72fb0bb2023-01-11 09:46:29 +080012388
developera3511852023-06-14 14:12:59 +080012389 while ( fgets(buf, sizeof(buf), fp)!= NULL )
12390 {
12391 /*
12392 Sample:sss
12393 1560336751 00:cd:fe:f3:25:e6 10.0.0.153 NallamousiPhone 01:00:cd:fe:f3:25:e6
12394 1560336751 12:34:56:78:9a:bc 10.0.0.154 NallamousiPhone 01:00:cd:fe:f3:25:e6
12395 */
12396 ret = sscanf(buf, LM_DHCP_CLIENT_FORMAT,
12397 &(LeaseTime),
12398 phyAddr,
12399 ipAddr,
12400 hostName
12401 );
12402 if(ret != 4)
12403 continue;
developer32f2a182023-06-27 19:50:41 +080012404 if (strcmp(str,phyAddr) == 0) {
12405 len = strlen(ipAddr);
12406 strncpy(ipaddr, ipAddr, len);
12407 ipaddr[len] = '\0';
12408 }
developera3511852023-06-14 14:12:59 +080012409 }
developer37646972023-06-29 10:58:43 +080012410 if (fclose(fp) == EOF) {
12411 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12412 return RETURN_ERR;
12413 }
developera3511852023-06-14 14:12:59 +080012414 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012415}
12416
12417/* wifi_getApInactiveAssociatedDeviceDiagnosticResult function */
12418/**
12419* @description Returning Inactive wireless connected clients informations
12420*
developer69b61b02023-03-07 17:17:44 +080012421* @param
developer72fb0bb2023-01-11 09:46:29 +080012422* @filename Holding private_wifi 2g/5g content files
12423* @associated_dev_array Having inactiv wireless clients informations
12424* @output_array_size Returning Inactive wireless counts
12425* @return The status of the operation
12426* @retval RETURN_OK if successful
12427* @retval RETURN_ERR if any error is detected
12428*
12429*/
12430
12431INT wifi_getApInactiveAssociatedDeviceDiagnosticResult(char *filename,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12432{
developera3511852023-06-14 14:12:59 +080012433 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12434 int count = 0,maccount = 0,i = 0,wificlientindex = 0;
12435 FILE *fp = NULL;
12436 int arr[MACADDRESS_SIZE] = {0};
12437 unsigned char mac[MACADDRESS_SIZE] = {0};
12438 char path[1024] = {0},str[1024] = {0},ipaddr[50] = {0},buf[512] = {0};
developer86035662023-06-28 19:21:12 +080012439 int res;
12440
12441 res = snprintf(buf, sizeof(buf), "cat %s | grep Station | sort | uniq | wc -l",filename);
12442 if (os_snprintf_error(sizeof(buf), res)) {
12443 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12444 return RETURN_ERR;
12445 }
developera3511852023-06-14 14:12:59 +080012446 fp = popen(buf,"r");
12447 if(fp == NULL)
12448 return RETURN_ERR;
12449 else
12450 {
developerd14dff12023-06-28 22:47:44 +080012451 if (fgets(path,sizeof(path),fp) == NULL) {
12452 wifi_debug(DEBUG_ERROR, "fgets fail\n");
12453 pclose(fp);
12454 return RETURN_ERR;
12455 }
developera3511852023-06-14 14:12:59 +080012456 maccount = atoi(path);
12457 }
12458 pclose(fp);
12459 *output_array_size = maccount;
12460 wifi_associated_dev3_t* temp = NULL;
developer9ce44382023-06-28 11:09:37 +080012461 if(*output_array_size > 0 && *output_array_size < MAX_ASSOCIATED_STA_NUM){
12462 temp = (wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
12463 } else {
12464 return RETURN_ERR;
12465 }
developer37646972023-06-29 10:58:43 +080012466
developera3511852023-06-14 14:12:59 +080012467 *associated_dev_array = temp;
12468 if(temp == NULL)
12469 {
12470 printf("Error Statement. Insufficient memory \n");
12471 return RETURN_ERR;
12472 }
12473 memset(buf,0,sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080012474 res = snprintf(buf, sizeof(buf),
12475 "cat %s | grep Station | cut -d ' ' -f2 | sort | uniq",filename);
12476 if (os_snprintf_error(sizeof(buf), res)) {
12477 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12478 return RETURN_ERR;
12479 }
developera3511852023-06-14 14:12:59 +080012480 fp = popen(buf,"r");
12481 if (fp == NULL) {
developer37646972023-06-29 10:58:43 +080012482 res = fprintf(stderr, "%s: failed pipe command %s.\n", __func__, buf);
12483 if (res < 0) {
12484 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12485 }
developera3511852023-06-14 14:12:59 +080012486 return RETURN_ERR;
12487 }
12488 for(count = 0; count < maccount ; count++)
12489 {
developer37646972023-06-29 10:58:43 +080012490 if (fgets(path,sizeof(path),fp) == NULL)
12491 continue;
developera3511852023-06-14 14:12:59 +080012492 for(i = 0; path[i]!='\n';i++)
12493 str[i]=path[i];
12494 str[i]='\0';
12495 getIPAddress(str,ipaddr);
developera3511852023-06-14 14:12:59 +080012496 if(strlen(ipaddr) > 0)
12497 {
developer33f13ba2023-07-12 16:19:06 +080012498 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 +080012499 {
12500 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12501 {
12502 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12503 {
12504 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012505
developera3511852023-06-14 14:12:59 +080012506 }
12507 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer37646972023-06-29 10:58:43 +080012508 if (fprintf(stderr,"%sMAC %d = %X:%X:%X:%X:%X:%X \n", __FUNCTION__,count, temp[count].cli_MACAddress[0],
12509 temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2],
12510 temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4],
12511 temp[count].cli_MACAddress[5]) < 0) {
12512 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12513 break;
12514 }
developera3511852023-06-14 14:12:59 +080012515 }
12516 temp[count].cli_AuthenticationState = 0; //TODO
12517 temp[count].cli_Active = 0; //TODO
12518 temp[count].cli_SignalStrength = 0;
12519 }
12520 else //Active wireless clients info
12521 {
12522 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12523 {
12524 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12525 {
12526 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012527
developera3511852023-06-14 14:12:59 +080012528 }
12529 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer12fb9f62023-06-30 15:26:27 +080012530 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 +080012531 }
12532 temp[count].cli_Active = 1;
12533 }
12534 }
12535 memset(ipaddr,0,sizeof(ipaddr));
12536 }
12537 pclose(fp);
12538 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12539 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012540}
12541//Device.WiFi.X_RDKCENTRAL-COM_BandSteering object
12542//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Capability bool r/o
12543//To get Band Steering Capability
12544INT wifi_getBandSteeringCapability(BOOL *support)
12545{
developera3511852023-06-14 14:12:59 +080012546 *support = FALSE;
12547 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012548}
12549
12550
12551//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Enable bool r/w
12552//To get Band Steering enable status
12553INT wifi_getBandSteeringEnable(BOOL *enable)
12554{
developera3511852023-06-14 14:12:59 +080012555 *enable = FALSE;
12556 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012557}
12558
12559//To turn on/off Band steering
12560INT wifi_setBandSteeringEnable(BOOL enable)
12561{
developera3511852023-06-14 14:12:59 +080012562 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012563}
12564
12565//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.APGroup string r/w
12566//To get Band Steering AP group
12567INT wifi_getBandSteeringApGroup(char *output_ApGroup)
12568{
developera3511852023-06-14 14:12:59 +080012569 if (NULL == output_ApGroup)
12570 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012571
developer32f2a182023-06-27 19:50:41 +080012572 memcpy(output_ApGroup, "1,2", 3);
12573 output_ApGroup[3] = '\0';
developera3511852023-06-14 14:12:59 +080012574 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012575}
12576
12577//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.UtilizationThreshold int r/w
12578//to set and read the band steering BandUtilizationThreshold parameters
12579INT wifi_getBandSteeringBandUtilizationThreshold (INT radioIndex, INT *pBuThreshold)
12580{
developera3511852023-06-14 14:12:59 +080012581 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012582}
12583
12584INT wifi_setBandSteeringBandUtilizationThreshold (INT radioIndex, INT buThreshold)
12585{
developera3511852023-06-14 14:12:59 +080012586 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012587}
12588
12589//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.RSSIThreshold int r/w
12590//to set and read the band steering RSSIThreshold parameters
12591INT wifi_getBandSteeringRSSIThreshold (INT radioIndex, INT *pRssiThreshold)
12592{
developera3511852023-06-14 14:12:59 +080012593 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012594}
12595
12596INT wifi_setBandSteeringRSSIThreshold (INT radioIndex, INT rssiThreshold)
12597{
developera3511852023-06-14 14:12:59 +080012598 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012599}
12600
12601
12602//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.PhyRateThreshold int r/w
12603//to set and read the band steering physical modulation rate threshold parameters
12604INT wifi_getBandSteeringPhyRateThreshold (INT radioIndex, INT *pPrThreshold)
12605{
developera3511852023-06-14 14:12:59 +080012606 //If chip is not support, return -1
12607 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012608}
12609
12610INT wifi_setBandSteeringPhyRateThreshold (INT radioIndex, INT prThreshold)
12611{
developera3511852023-06-14 14:12:59 +080012612 //If chip is not support, return -1
12613 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012614}
12615
12616//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.OverloadInactiveTime int r/w
12617//to set and read the inactivity time (in seconds) for steering under overload condition
12618INT wifi_getBandSteeringOverloadInactiveTime(INT radioIndex, INT *pPrThreshold)
12619{
developera3511852023-06-14 14:12:59 +080012620 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012621}
12622
12623INT wifi_setBandSteeringOverloadInactiveTime(INT radioIndex, INT prThreshold)
12624{
developera3511852023-06-14 14:12:59 +080012625 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012626}
12627
12628//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.IdleInactiveTime int r/w
12629//to set and read the inactivity time (in seconds) for steering under Idle condition
12630INT wifi_getBandSteeringIdleInactiveTime(INT radioIndex, INT *pPrThreshold)
12631{
developera3511852023-06-14 14:12:59 +080012632 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012633}
12634
12635INT wifi_setBandSteeringIdleInactiveTime(INT radioIndex, INT prThreshold)
12636{
developera3511852023-06-14 14:12:59 +080012637 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012638}
12639
12640//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.History string r/o
12641//pClientMAC[64]
12642//pSourceSSIDIndex[64]
12643//pDestSSIDIndex[64]
12644//pSteeringReason[256]
12645INT wifi_getBandSteeringLog(INT record_index, ULONG *pSteeringTime, CHAR *pClientMAC, INT *pSourceSSIDIndex, INT *pDestSSIDIndex, INT *pSteeringReason)
12646{
developera3511852023-06-14 14:12:59 +080012647 //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 +080012648 long int tim_tmp = time(NULL);
12649 if (tim_tmp < 0)
12650 return RETURN_ERR;
12651 *pSteeringTime = tim_tmp;
developera3511852023-06-14 14:12:59 +080012652 *pSteeringReason = 0; //TODO: need to assign correct steering reason (INT numeric, i suppose)
12653 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012654}
12655
12656INT wifi_ifConfigDown(INT apIndex)
12657{
developera3511852023-06-14 14:12:59 +080012658 INT status = RETURN_OK;
12659 char cmd[64];
developere40952c2023-06-15 18:46:43 +080012660 int res;
developer72fb0bb2023-01-11 09:46:29 +080012661
developer33f13ba2023-07-12 16:19:06 +080012662 res = v_secure_system("ifconfig ath%d down", apIndex);
12663 if (res) {
12664 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080012665 return RETURN_ERR;
12666 }
developera3511852023-06-14 14:12:59 +080012667 printf("%s: %s\n", __func__, cmd);
developer72fb0bb2023-01-11 09:46:29 +080012668
developera3511852023-06-14 14:12:59 +080012669 return status;
developer72fb0bb2023-01-11 09:46:29 +080012670}
12671
12672INT wifi_ifConfigUp(INT apIndex)
12673{
developera3511852023-06-14 14:12:59 +080012674 char interface_name[16] = {0};
12675 char cmd[128];
12676 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012677 int res;
developer72fb0bb2023-01-11 09:46:29 +080012678
developera3511852023-06-14 14:12:59 +080012679 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12680 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080012681 res = snprintf(cmd, sizeof(cmd), "ifconfig %s up 2>/dev/null", interface_name);
12682 if (os_snprintf_error(sizeof(cmd), res)) {
12683 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12684 return RETURN_ERR;
12685 }
developera3511852023-06-14 14:12:59 +080012686 _syscmd(cmd, buf, sizeof(buf));
12687 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012688}
12689
12690//>> Deprecated. Replace with wifi_applyRadioSettings
12691INT wifi_pushBridgeInfo(INT apIndex)
12692{
developerb2977562023-05-24 17:54:12 +080012693 char ip[32] = {0};
12694 char subnet[32] = {0};
12695 char bridge[32] = {0};
12696 char cmd[128] = {0};
12697 char buf[1024] = {0};
developere40952c2023-06-15 18:46:43 +080012698 int res;
developer72fb0bb2023-01-11 09:46:29 +080012699
developerb2977562023-05-24 17:54:12 +080012700 wifi_getApBridgeInfo(apIndex, bridge, ip, subnet);
developer72fb0bb2023-01-11 09:46:29 +080012701
developere40952c2023-06-15 18:46:43 +080012702 res = snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s ", bridge, ip, subnet);
12703 if (os_snprintf_error(sizeof(cmd), res)) {
12704 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12705 return RETURN_ERR;
12706 }
developerb2977562023-05-24 17:54:12 +080012707 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080012708
developerb2977562023-05-24 17:54:12 +080012709 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012710}
12711
12712INT wifi_pushChannel(INT radioIndex, UINT channel)
12713{
developera3511852023-06-14 14:12:59 +080012714 char interface_name[16] = {0};
12715 char cmd[128];
12716 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012717 int res;
developer72fb0bb2023-01-11 09:46:29 +080012718
developera3511852023-06-14 14:12:59 +080012719 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
12720 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080012721 res = snprintf(cmd, sizeof(cmd), "iwconfig %s freq %d",interface_name,channel);
12722 if (os_snprintf_error(sizeof(cmd), res)) {
12723 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12724 return RETURN_ERR;
12725 }
developera3511852023-06-14 14:12:59 +080012726 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080012727
developera3511852023-06-14 14:12:59 +080012728 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012729}
12730
12731INT wifi_pushChannelMode(INT radioIndex)
12732{
developera3511852023-06-14 14:12:59 +080012733 //Apply Channel mode, pure mode, etc that been set by wifi_setRadioChannelMode() instantly
12734 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012735}
12736
12737INT wifi_pushDefaultValues(INT radioIndex)
12738{
developera3511852023-06-14 14:12:59 +080012739 //Apply Comcast specified default radio settings instantly
12740 //AMPDU=1
12741 //AMPDUFrames=32
12742 //AMPDULim=50000
12743 //txqueuelen=1000
developer72fb0bb2023-01-11 09:46:29 +080012744
developera3511852023-06-14 14:12:59 +080012745 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012746}
12747
12748INT wifi_pushTxChainMask(INT radioIndex)
12749{
developera3511852023-06-14 14:12:59 +080012750 //Apply default TxChainMask instantly
12751 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012752}
12753
12754INT wifi_pushRxChainMask(INT radioIndex)
12755{
developera3511852023-06-14 14:12:59 +080012756 //Apply default RxChainMask instantly
12757 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012758}
12759
12760INT wifi_pushSSID(INT apIndex, CHAR *ssid)
12761{
developer7e4a2a62023-04-06 19:56:03 +080012762 INT status;
developer72fb0bb2023-01-11 09:46:29 +080012763
developer7e4a2a62023-04-06 19:56:03 +080012764 status = wifi_setSSIDName(apIndex, ssid);
12765 wifi_quick_reload_ap(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080012766
developer7e4a2a62023-04-06 19:56:03 +080012767 return status;
developer72fb0bb2023-01-11 09:46:29 +080012768}
12769
12770INT wifi_pushSsidAdvertisementEnable(INT apIndex, BOOL enable)
12771{
developera3511852023-06-14 14:12:59 +080012772 int ret;
developerc1aa6532023-06-09 09:37:01 +080012773 ret = wifi_setApSsidAdvertisementEnable(apIndex, enable);
12774
12775 return ret;
developer72fb0bb2023-01-11 09:46:29 +080012776}
12777
12778INT wifi_getRadioUpTime(INT radioIndex, ULONG *output)
12779{
developera3511852023-06-14 14:12:59 +080012780 time_t now;
developere82c0ca2023-05-10 16:25:35 +080012781
developerd14dff12023-06-28 22:47:44 +080012782 now = time(NULL);
12783 if (now < 0) {
12784 wifi_debug(DEBUG_ERROR, "GET time fail\n");
12785 return RETURN_ERR;
12786 }
developere82c0ca2023-05-10 16:25:35 +080012787 if (now > radio_up_time[radioIndex])
12788 *output = now - radio_up_time[radioIndex];
12789 else {
12790 *output = 0;
12791 return RETURN_ERR;
12792 }
12793
developera3511852023-06-14 14:12:59 +080012794 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012795}
12796
12797INT wifi_getApEnableOnLine(INT wlanIndex, BOOL *enabled)
12798{
developera3511852023-06-14 14:12:59 +080012799 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012800}
12801
12802INT wifi_getApSecurityWpaRekeyInterval(INT apIndex, INT *output_int)
12803{
developera3511852023-06-14 14:12:59 +080012804 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012805}
12806
12807//To-do
12808INT wifi_getApSecurityMFPConfig(INT apIndex, CHAR *output_string)
12809{
developera3511852023-06-14 14:12:59 +080012810 char output[16]={'\0'};
12811 char config_file[MAX_BUF_SIZE] = {0};
12812 int res;
developer72fb0bb2023-01-11 09:46:29 +080012813
developera3511852023-06-14 14:12:59 +080012814 if (!output_string)
12815 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012816
developera3511852023-06-14 14:12:59 +080012817 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12818 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012819 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12820 return RETURN_ERR;
12821 }
developera3511852023-06-14 14:12:59 +080012822 wifi_hostapdRead(config_file, "ieee80211w", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012823
developera3511852023-06-14 14:12:59 +080012824 if (strlen(output) == 0)
developere40952c2023-06-15 18:46:43 +080012825 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012826 else if (strncmp(output, "0", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012827 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012828 else if (strncmp(output, "1", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012829 res = snprintf(output_string, 64, "Optional");
developera3511852023-06-14 14:12:59 +080012830 else if (strncmp(output, "2", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012831 res = snprintf(output_string, 64, "Required");
developera3511852023-06-14 14:12:59 +080012832 else {
12833 wifi_dbg_printf("\n[%s]: Unexpected ieee80211w=%s", __func__, output);
12834 return RETURN_ERR;
12835 }
developere40952c2023-06-15 18:46:43 +080012836 if (os_snprintf_error(64, res)) {
12837 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12838 return RETURN_ERR;
12839 }
developer72fb0bb2023-01-11 09:46:29 +080012840
developera3511852023-06-14 14:12:59 +080012841 wifi_dbg_printf("\n[%s]: ieee80211w is : %s", __func__, output);
12842 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012843}
12844INT wifi_setApSecurityMFPConfig(INT apIndex, CHAR *MfpConfig)
12845{
developera3511852023-06-14 14:12:59 +080012846 struct params params;
12847 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080012848 int res;
developer72fb0bb2023-01-11 09:46:29 +080012849
developera3511852023-06-14 14:12:59 +080012850 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12851 if(NULL == MfpConfig || strlen(MfpConfig) >= 32 )
12852 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012853
developera3511852023-06-14 14:12:59 +080012854 params.name = "ieee80211w";
12855 if (strncmp(MfpConfig, "Disabled", strlen("Disabled")) == 0)
12856 params.value = "0";
12857 else if (strncmp(MfpConfig, "Optional", strlen("Optional")) == 0)
12858 params.value = "1";
12859 else if (strncmp(MfpConfig, "Required", strlen("Required")) == 0)
12860 params.value = "2";
12861 else{
12862 wifi_dbg_printf("%s: invalid MfpConfig. Input has to be Disabled, Optional or Required \n", __func__);
12863 return RETURN_ERR;
12864 }
developer75bd10c2023-06-27 11:34:08 +080012865
12866 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12867 if (os_snprintf_error(sizeof(config_file), res)) {
12868 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12869 return RETURN_ERR;
12870 }
12871
developera3511852023-06-14 14:12:59 +080012872 wifi_hostapdWrite(config_file, &params, 1);
12873 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12874 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012875}
12876INT wifi_getRadioAutoChannelEnable(INT radioIndex, BOOL *output_bool)
12877{
developera3511852023-06-14 14:12:59 +080012878 char output[16]={'\0'};
12879 char config_file[MAX_BUF_SIZE] = {0};
12880 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080012881 int res;
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 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080012885 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
12886 if (os_snprintf_error(sizeof(config_file), res)) {
12887 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12888 return RETURN_ERR;
12889 }
developera3511852023-06-14 14:12:59 +080012890 wifi_datfileRead(config_file, "AutoChannelSelect" , output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012891
developera3511852023-06-14 14:12:59 +080012892 if (strncmp(output, "0", 1) == 0)
12893 *output_bool = FALSE;
12894 else if (strncmp(output, "1", 1) == 0)
12895 *output_bool = TRUE;
12896 else if (strncmp(output, "2", 1) == 0)
12897 *output_bool = TRUE;
12898 else if (strncmp(output, "3", 1) == 0)
12899 *output_bool = TRUE;
12900 else
12901 *output_bool = FALSE;
12902 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012903
developera3511852023-06-14 14:12:59 +080012904 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012905}
12906
12907INT wifi_getRouterEnable(INT wlanIndex, BOOL *enabled)
12908{
developera3511852023-06-14 14:12:59 +080012909 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012910}
12911
12912INT wifi_setApSecurityWpaRekeyInterval(INT apIndex, INT *rekeyInterval)
12913{
developera3511852023-06-14 14:12:59 +080012914 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012915}
12916
12917INT wifi_setRouterEnable(INT wlanIndex, INT *RouterEnabled)
12918{
developera3511852023-06-14 14:12:59 +080012919 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012920}
12921
12922INT wifi_getRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
12923{
developera3511852023-06-14 14:12:59 +080012924 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12925 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080012926 char tmp_output[MAX_BUF_SIZE] = {0};
developera3511852023-06-14 14:12:59 +080012927 int res;
developer72fb0bb2023-01-11 09:46:29 +080012928
developera3511852023-06-14 14:12:59 +080012929 if (NULL == output)
12930 return RETURN_ERR;
12931 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12932 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012933 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12934 return RETURN_ERR;
12935 }
developera3511852023-06-14 14:12:59 +080012936 wifi_hostapdRead(config_file,"hw_mode",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012937
developer32f2a182023-06-27 19:50:41 +080012938 if(strcmp(output,"b")==0) {
12939 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,11");
12940 if (os_snprintf_error(sizeof(tmp_output), res)) {
12941 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12942 return RETURN_ERR;
12943 }
12944 } else if (strcmp(output,"a")==0) {
12945 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "6,9,11,12,18,24,36,48,54");
12946 if (os_snprintf_error(sizeof(tmp_output), res)) {
12947 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12948 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080012949 }
developer32f2a182023-06-27 19:50:41 +080012950 } else if ((strcmp(output,"n")==0) | (strcmp(output,"g")==0)) {
12951 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,6,9,11,12,18,24,36,48,54");
12952 if (os_snprintf_error(sizeof(tmp_output), res)) {
12953 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12954 return RETURN_ERR;
12955 }
developer75bd10c2023-06-27 11:34:08 +080012956 }
developer32f2a182023-06-27 19:50:41 +080012957 memcpy(output, tmp_output, strlen(tmp_output));
12958 output[strlen(tmp_output)] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012959
developera3511852023-06-14 14:12:59 +080012960 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12961 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012962}
12963
12964INT wifi_getRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
12965{
developera3511852023-06-14 14:12:59 +080012966 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12967 char *temp;
developer32f2a182023-06-27 19:50:41 +080012968 char temp_output[128] = {0};
12969 char temp_TransmitRates[128] = {0};
developera3511852023-06-14 14:12:59 +080012970 char config_file[MAX_BUF_SIZE] = {0};
12971 int res;
developer32f2a182023-06-27 19:50:41 +080012972 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012973
developera3511852023-06-14 14:12:59 +080012974 if (NULL == output)
12975 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012976
developera3511852023-06-14 14:12:59 +080012977 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12978 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012979 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12980 return RETURN_ERR;
12981 }
developera3511852023-06-14 14:12:59 +080012982 wifi_hostapdRead(config_file,"supported_rates",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012983
developera3511852023-06-14 14:12:59 +080012984 if (strlen(output) == 0) {
12985 wifi_getRadioSupportedDataTransmitRates(wlanIndex, output);
12986 return RETURN_OK;
12987 }
developer32f2a182023-06-27 19:50:41 +080012988 len = strlen(output);
12989 if (len >= sizeof(temp_TransmitRates)) {
12990 wifi_debug(DEBUG_ERROR, "Unexpected strlen(output)\n");
12991 return RETURN_ERR;
12992 }
12993 strncpy(temp_TransmitRates, output, len);
developera3511852023-06-14 14:12:59 +080012994 temp = strtok(temp_TransmitRates," ");
12995 while(temp!=NULL)
12996 {
12997 temp[strlen(temp)-1]=0;
12998 if((temp[0]=='5') && (temp[1]=='\0'))
12999 {
13000 temp="5.5";
13001 }
developer32f2a182023-06-27 19:50:41 +080013002 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp)) {
13003 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13004 return RETURN_ERR;
13005 }
13006 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013007 temp = strtok(NULL," ");
13008 if(temp!=NULL)
13009 {
developer32f2a182023-06-27 19:50:41 +080013010 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
13011 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13012 return RETURN_ERR;
13013 }
13014 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013015 }
13016 }
developer32f2a182023-06-27 19:50:41 +080013017 len = strlen(temp_output);
13018 strncpy(output, temp_output, len);
13019 output[len] = '\0';
developera3511852023-06-14 14:12:59 +080013020 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013021
developera3511852023-06-14 14:12:59 +080013022 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013023}
13024
13025INT wifi_setRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
13026{
developera3511852023-06-14 14:12:59 +080013027 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013028}
13029
13030
13031INT wifi_setRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
13032{
developera3511852023-06-14 14:12:59 +080013033 int i=0;
13034 char *temp;
13035 char temp1[128] = {0};
13036 char temp_output[128] = {0};
13037 char temp_TransmitRates[128] = {0};
13038 struct params params={'\0'};
13039 char config_file[MAX_BUF_SIZE] = {0};
13040 wifi_band band = wifi_index_to_band(wlanIndex);
developer32f2a182023-06-27 19:50:41 +080013041 unsigned long len;
13042 int res;
developer72fb0bb2023-01-11 09:46:29 +080013043
developera3511852023-06-14 14:12:59 +080013044 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
13045 if(NULL == output)
13046 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080013047
13048 len = strlen(output);
13049 if (len >= sizeof(temp_TransmitRates)) {
13050 wifi_debug(DEBUG_ERROR, "not enough room in temp_TransmitRates\n");
13051 return RETURN_ERR;
13052 }
13053 strncpy(temp_TransmitRates, output, len);
13054 temp_TransmitRates[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080013055
developera3511852023-06-14 14:12:59 +080013056 for(i=0;i<strlen(temp_TransmitRates);i++)
13057 {
13058 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
13059 {
13060 continue;
13061 }
13062 else
13063 {
13064 return RETURN_ERR;
13065 }
13066 }
developera3511852023-06-14 14:12:59 +080013067 temp = strtok(temp_TransmitRates,",");
13068 while(temp!=NULL)
13069 {
developer32f2a182023-06-27 19:50:41 +080013070 len = strlen(temp);
13071 if (len >= sizeof(temp1)) {
13072 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13073 return RETURN_ERR;
13074 }
13075 strncpy(temp1, temp, len);
13076 temp1[len] = '\0';
developera3511852023-06-14 14:12:59 +080013077 if(band == band_5)
13078 {
13079 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
13080 {
13081 return RETURN_ERR;
13082 }
13083 }
developer72fb0bb2023-01-11 09:46:29 +080013084
developer32f2a182023-06-27 19:50:41 +080013085 if(strcmp(temp,"5.5")==0) {
13086 strncpy(temp1, "55", 2);
13087 temp1[2] = '\0';
13088 } else {
13089 if ((sizeof(temp1) - strlen(temp1)) <= 1) {
13090 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13091 return RETURN_ERR;
13092 }
13093 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +080013094 }
developer5b23cd02023-07-19 20:26:03 +080013095
developer32f2a182023-06-27 19:50:41 +080013096 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp1)) {
13097 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13098 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080013099 }
developer32f2a182023-06-27 19:50:41 +080013100 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013101 temp = strtok(NULL,",");
13102 if(temp!=NULL)
13103 {
developer32f2a182023-06-27 19:50:41 +080013104 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
13105 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13106 return RETURN_ERR;
13107 }
13108 strncat(temp_output, " ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013109 }
13110 }
developer32f2a182023-06-27 19:50:41 +080013111 len = strlen(temp_output);
13112 strncpy(output, temp_output, len);
13113 output[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080013114
developera3511852023-06-14 14:12:59 +080013115 params.name = "supported_rates";
13116 params.value = output;
developer72fb0bb2023-01-11 09:46:29 +080013117
developera3511852023-06-14 14:12:59 +080013118 wifi_dbg_printf("\n%s:",__func__);
13119 wifi_dbg_printf("params.value=%s\n",params.value);
developer32f2a182023-06-27 19:50:41 +080013120 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
13121 if (os_snprintf_error(sizeof(config_file), res)) {
13122 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13123 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013124 }
developera3511852023-06-14 14:12:59 +080013125 wifi_hostapdWrite(config_file,&params,1);
13126 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013127
developera3511852023-06-14 14:12:59 +080013128 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013129}
13130
13131
13132static char *sncopy(char *dst, int dst_sz, const char *src)
13133{
developera3511852023-06-14 14:12:59 +080013134 if (src && dst && dst_sz > 0) {
13135 strncpy(dst, src, dst_sz);
13136 dst[dst_sz - 1] = '\0';
13137 }
13138 return dst;
developer72fb0bb2023-01-11 09:46:29 +080013139}
13140
13141static int util_get_sec_chan_offset(int channel, const char* ht_mode)
13142{
developera3511852023-06-14 14:12:59 +080013143 if (0 == strcmp(ht_mode, "HT40") ||
13144 0 == strcmp(ht_mode, "HT80") ||
13145 0 == strcmp(ht_mode, "HT160")) {
13146 switch (channel) {
13147 case 1 ... 7:
13148 case 36:
13149 case 44:
13150 case 52:
13151 case 60:
13152 case 100:
13153 case 108:
13154 case 116:
13155 case 124:
13156 case 132:
13157 case 140:
13158 case 149:
13159 case 157:
13160 return 1;
13161 case 8 ... 13:
13162 case 40:
13163 case 48:
13164 case 56:
13165 case 64:
13166 case 104:
13167 case 112:
13168 case 120:
13169 case 128:
13170 case 136:
13171 case 144:
13172 case 153:
13173 case 161:
13174 return -1;
13175 default:
13176 return -EINVAL;
13177 }
13178 }
developer72fb0bb2023-01-11 09:46:29 +080013179
developera3511852023-06-14 14:12:59 +080013180 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013181}
13182
13183static int util_get_6g_sec_chan_offset(int channel, const char* ht_mode)
13184{
developera3511852023-06-14 14:12:59 +080013185 int idx = channel%8;
13186 if (0 == strcmp(ht_mode, "HT40") ||
13187 0 == strcmp(ht_mode, "HT80") ||
13188 0 == strcmp(ht_mode, "HT160")) {
13189 switch (idx) {
13190 case 1:
13191 return 1;
13192 case 5:
13193 return -1;
13194 default:
13195 return -EINVAL;
13196 }
13197 }
developer72fb0bb2023-01-11 09:46:29 +080013198
developera3511852023-06-14 14:12:59 +080013199 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013200}
13201static void util_hw_mode_to_bw_mode(const char* hw_mode, char *bw_mode, int bw_mode_len)
13202{
developera3511852023-06-14 14:12:59 +080013203 if (NULL == hw_mode) return;
developer72fb0bb2023-01-11 09:46:29 +080013204
developera3511852023-06-14 14:12:59 +080013205 if (0 == strcmp(hw_mode, "ac"))
13206 sncopy(bw_mode, bw_mode_len, "ht vht");
developer72fb0bb2023-01-11 09:46:29 +080013207
developera3511852023-06-14 14:12:59 +080013208 if (0 == strcmp(hw_mode, "n"))
13209 sncopy(bw_mode, bw_mode_len, "ht");
developer72fb0bb2023-01-11 09:46:29 +080013210
developera3511852023-06-14 14:12:59 +080013211 return;
developer72fb0bb2023-01-11 09:46:29 +080013212}
13213
13214static int util_chan_to_freq(int chan)
13215{
developera3511852023-06-14 14:12:59 +080013216 if (chan == 14)
13217 return 2484;
13218 else if (chan < 14)
13219 return 2407 + chan * 5;
13220 else if (chan >= 182 && chan <= 196)
13221 return 4000 + chan * 5;
13222 else
13223 return 5000 + chan * 5;
13224 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013225}
13226
13227static int util_6G_chan_to_freq(int chan)
13228{
developera3511852023-06-14 14:12:59 +080013229 if (chan)
13230 return 5950 + chan * 5;
13231 else
13232 return 0;
developer69b61b02023-03-07 17:17:44 +080013233
developer72fb0bb2023-01-11 09:46:29 +080013234}
13235const int *util_unii_5g_chan2list(int chan, int width)
13236{
developera3511852023-06-14 14:12:59 +080013237 static const int lists[] = {
13238 // <width>, <chan1>, <chan2>..., 0,
13239 20, 36, 0,
13240 20, 40, 0,
13241 20, 44, 0,
13242 20, 48, 0,
13243 20, 52, 0,
13244 20, 56, 0,
13245 20, 60, 0,
13246 20, 64, 0,
13247 20, 100, 0,
13248 20, 104, 0,
13249 20, 108, 0,
13250 20, 112, 0,
13251 20, 116, 0,
13252 20, 120, 0,
13253 20, 124, 0,
13254 20, 128, 0,
13255 20, 132, 0,
13256 20, 136, 0,
13257 20, 140, 0,
13258 20, 144, 0,
13259 20, 149, 0,
13260 20, 153, 0,
13261 20, 157, 0,
13262 20, 161, 0,
13263 20, 165, 0,
13264 40, 36, 40, 0,
13265 40, 44, 48, 0,
13266 40, 52, 56, 0,
13267 40, 60, 64, 0,
13268 40, 100, 104, 0,
13269 40, 108, 112, 0,
13270 40, 116, 120, 0,
13271 40, 124, 128, 0,
13272 40, 132, 136, 0,
13273 40, 140, 144, 0,
13274 40, 149, 153, 0,
13275 40, 157, 161, 0,
13276 80, 36, 40, 44, 48, 0,
13277 80, 52, 56, 60, 64, 0,
13278 80, 100, 104, 108, 112, 0,
13279 80, 116, 120, 124, 128, 0,
13280 80, 132, 136, 140, 144, 0,
13281 80, 149, 153, 157, 161, 0,
13282 160, 36, 40, 44, 48, 52, 56, 60, 64, 0,
13283 160, 100, 104, 108, 112, 116, 120, 124, 128, 0,
13284 -1 // final delimiter
13285 };
13286 const int *start;
13287 const int *p;
developer72fb0bb2023-01-11 09:46:29 +080013288
developera3511852023-06-14 14:12:59 +080013289 for (p = lists; *p != -1; p++) {
13290 if (*p == width) {
13291 for (start = ++p; *p != 0; p++) {
13292 if (*p == chan)
13293 return start;
13294 }
13295 }
13296 // move to the end of channel list of given width
13297 while (*p != 0) {
13298 p++;
13299 }
13300 }
developer72fb0bb2023-01-11 09:46:29 +080013301
developera3511852023-06-14 14:12:59 +080013302 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080013303}
13304
13305static int util_unii_5g_centerfreq(const char *ht_mode, int channel)
13306{
developera3511852023-06-14 14:12:59 +080013307 if (NULL == ht_mode)
13308 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013309
developera3511852023-06-14 14:12:59 +080013310 const int width = atoi(strlen(ht_mode) > 2 ? ht_mode + 2 : "20");
13311 const int *chans = util_unii_5g_chan2list(channel, width);
13312 int sum = 0;
13313 int cnt = 0;
developer72fb0bb2023-01-11 09:46:29 +080013314
developera3511852023-06-14 14:12:59 +080013315 if (NULL == chans)
13316 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013317
developera3511852023-06-14 14:12:59 +080013318 while (*chans) {
13319 sum += *chans;
13320 cnt++;
13321 chans++;
13322 }
13323 if (cnt == 0)
13324 return 0;
13325 return sum / cnt;
developer72fb0bb2023-01-11 09:46:29 +080013326}
13327
13328static int util_unii_6g_centerfreq(const char *ht_mode, int channel)
13329{
developerc14d83a2023-06-29 20:09:42 +080013330 long int width;
developera3511852023-06-14 14:12:59 +080013331 int idx = 0 ;
13332 int centerchan = 0;
13333 int chan_ofs = 1;
developer72fb0bb2023-01-11 09:46:29 +080013334
developerc14d83a2023-06-29 20:09:42 +080013335 if (NULL == ht_mode)
13336 return 0;
13337
13338 if (hal_strtol((char *)(ht_mode + 2), 10, &width) < 0) {
13339 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13340 }
13341
developera3511852023-06-14 14:12:59 +080013342 if (width == 40){
13343 idx = ((channel/4) + chan_ofs)%2;
13344 switch (idx) {
13345 case 0:
13346 centerchan = (channel - 2);
13347 break;
13348 case 1:
13349 centerchan = (channel + 2);
13350 break;
13351 default:
13352 return -EINVAL;
13353 }
13354 }else if (width == 80){
13355 idx = ((channel/4) + chan_ofs)%4;
13356 switch (idx) {
13357 case 0:
13358 centerchan = (channel - 6);
13359 break;
13360 case 1:
13361 centerchan = (channel + 6);
13362 break;
13363 case 2:
13364 centerchan = (channel + 2);
13365 break;
13366 case 3:
13367 centerchan = (channel - 2);
13368 break;
13369 default:
13370 return -EINVAL;
13371 }
13372 }else if (width == 160){
13373 switch (channel) {
13374 case 1 ... 29:
13375 centerchan = 15;
13376 break;
13377 case 33 ... 61:
13378 centerchan = 47;
13379 break;
13380 case 65 ... 93:
13381 centerchan = 79;
13382 break;
13383 case 97 ... 125:
13384 centerchan = 111;
13385 break;
13386 case 129 ... 157:
13387 centerchan = 143;
13388 break;
13389 case 161 ... 189:
13390 centerchan = 175;
13391 break;
13392 case 193 ... 221:
13393 centerchan = 207;
13394 break;
13395 default:
13396 return -EINVAL;
13397 }
13398 }
13399 return centerchan;
developer72fb0bb2023-01-11 09:46:29 +080013400}
13401static int util_radio_get_hw_mode(int radioIndex, char *hw_mode, int hw_mode_size)
13402{
developera3511852023-06-14 14:12:59 +080013403 BOOL onlyG, onlyN, onlyA;
13404 CHAR tmp[64];
13405 int ret = wifi_getRadioStandard(radioIndex, tmp, &onlyG, &onlyN, &onlyA);
13406 if (ret == RETURN_OK) {
13407 sncopy(hw_mode, hw_mode_size, tmp);
13408 }
13409 return ret;
developer72fb0bb2023-01-11 09:46:29 +080013410}
13411
13412INT wifi_pushRadioChannel2(INT radioIndex, UINT channel, UINT channel_width_MHz, UINT csa_beacon_count)
13413{
developera3511852023-06-14 14:12:59 +080013414 // Sample commands:
13415 // hostapd_cli -i wifi1 chan_switch 30 5200 sec_channel_offset=-1 center_freq1=5190 bandwidth=40 ht vht
13416 // hostapd_cli -i wifi0 chan_switch 30 2437
13417 int ret = 0;
13418 char center_freq1_str[32] = ""; // center_freq1=%d
13419 char opt_chan_info_str[32] = ""; // bandwidth=%d ht vht
13420 char sec_chan_offset_str[32] = ""; // sec_channel_offset=%d
13421 char hw_mode[16] = ""; // n|ac
13422 char bw_mode[16] = ""; // ht|ht vht
13423 char ht_mode[16] = ""; // HT20|HT40|HT80|HT160
13424 char interface_name[16] = {0};
13425 int sec_chan_offset;
13426 int width;
13427 char config_file[64] = {0};
13428 char *ext_str = "None";
13429 wifi_band band = band_invalid;
13430 int center_chan = 0;
13431 int center_freq1 = 0;
developere40952c2023-06-15 18:46:43 +080013432 int res;
developer72fb0bb2023-01-11 09:46:29 +080013433
developere40952c2023-06-15 18:46:43 +080013434 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
13435 if (os_snprintf_error(sizeof(config_file), res)) {
13436 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13437 return RETURN_ERR;
13438 }
developer72fb0bb2023-01-11 09:46:29 +080013439
developera3511852023-06-14 14:12:59 +080013440 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
13441 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013442
developera3511852023-06-14 14:12:59 +080013443 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013444
developera3511852023-06-14 14:12:59 +080013445 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080013446
developera3511852023-06-14 14:12:59 +080013447 width = channel_width_MHz > 20 ? channel_width_MHz : 20;
developer72fb0bb2023-01-11 09:46:29 +080013448
developera3511852023-06-14 14:12:59 +080013449 // Get radio mode HT20|HT40|HT80 etc.
13450 if (channel){
developere40952c2023-06-15 18:46:43 +080013451 res = snprintf(ht_mode, sizeof(ht_mode), "HT%d", width);
13452 if (os_snprintf_error(sizeof(ht_mode), res)) {
13453 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13454 return RETURN_ERR;
13455 }
developer72fb0bb2023-01-11 09:46:29 +080013456
developera3511852023-06-14 14:12:59 +080013457 // Provide bandwith if specified
13458 if (channel_width_MHz > 20) {
13459 // Select bandwidth mode from hardware n --> ht | ac --> ht vht
13460 util_radio_get_hw_mode(radioIndex, hw_mode, sizeof(hw_mode));
13461 util_hw_mode_to_bw_mode(hw_mode, bw_mode, sizeof(bw_mode));
developer72fb0bb2023-01-11 09:46:29 +080013462
developere40952c2023-06-15 18:46:43 +080013463 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d %s", width, bw_mode);
developera3511852023-06-14 14:12:59 +080013464 }else if (channel_width_MHz == 20){
developere40952c2023-06-15 18:46:43 +080013465 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d ht", width);
developera3511852023-06-14 14:12:59 +080013466 }
developer72fb0bb2023-01-11 09:46:29 +080013467
developere40952c2023-06-15 18:46:43 +080013468 if (os_snprintf_error(sizeof(opt_chan_info_str), res)) {
13469 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13470 return RETURN_ERR;
13471 }
developer72fb0bb2023-01-11 09:46:29 +080013472
developera3511852023-06-14 14:12:59 +080013473 if (channel_width_MHz > 20) {
13474 if (band == band_6){
13475 center_chan = util_unii_6g_centerfreq(ht_mode, channel);
13476 if(center_chan){
13477 center_freq1 = util_6G_chan_to_freq(center_chan);
13478 }
13479 }else{
13480 center_chan = util_unii_5g_centerfreq(ht_mode, channel);
13481 if(center_chan){
13482 center_freq1 = util_chan_to_freq(center_chan);
13483 }
13484 }
developer69b61b02023-03-07 17:17:44 +080013485
developera3511852023-06-14 14:12:59 +080013486 if (center_freq1)
developere40952c2023-06-15 18:46:43 +080013487 res = snprintf(center_freq1_str, sizeof(center_freq1_str), "center_freq1=%d", center_freq1);
developer69b61b02023-03-07 17:17:44 +080013488
developera3511852023-06-14 14:12:59 +080013489 }
developere40952c2023-06-15 18:46:43 +080013490 if (os_snprintf_error(sizeof(center_freq1_str), res)) {
13491 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13492 return RETURN_ERR;
13493 }
developer72fb0bb2023-01-11 09:46:29 +080013494
developera3511852023-06-14 14:12:59 +080013495 // Find channel offset +1/-1 for wide modes (HT40|HT80|HT160)
13496 if (band == band_6){
13497 sec_chan_offset = util_get_6g_sec_chan_offset(channel, ht_mode);
13498 }else{
13499 sec_chan_offset = util_get_sec_chan_offset(channel, ht_mode);
13500 }
developere40952c2023-06-15 18:46:43 +080013501 if (sec_chan_offset != -EINVAL) {
13502 res = snprintf(sec_chan_offset_str, sizeof(sec_chan_offset_str), "sec_channel_offset=%d", sec_chan_offset);
13503 if (os_snprintf_error(sizeof(sec_chan_offset_str), res)) {
13504 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13505 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013506 }
developere40952c2023-06-15 18:46:43 +080013507 }
developera3511852023-06-14 14:12:59 +080013508 // Only the first AP, other are hanging on the same radio
13509 ret = wifi_setChannel_netlink(radioIndex, &channel, NULL);
13510 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013511 wifi_debug(DEBUG_ERROR,"wifi_setChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013512 return RETURN_ERR;
13513 }
13514 /* wifi_dbg_printf("execute: '%s'\n", cmd);
13515 ret = _syscmd(cmd, buf, sizeof(buf));
13516 wifi_reloadAp(radioIndex); */
developer72fb0bb2023-01-11 09:46:29 +080013517
developera3511852023-06-14 14:12:59 +080013518 ret = wifi_setRadioChannel(radioIndex, channel);
13519 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013520 wifi_debug(DEBUG_ERROR,"wifi_setRadioChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013521 return RETURN_ERR;
13522 }
developer72fb0bb2023-01-11 09:46:29 +080013523
developera3511852023-06-14 14:12:59 +080013524 if (sec_chan_offset == 1)
13525 ext_str = "Above";
13526 else if (sec_chan_offset == -1)
13527 ext_str = "Below";
developer72fb0bb2023-01-11 09:46:29 +080013528
developera3511852023-06-14 14:12:59 +080013529 /*wifi_setRadioCenterChannel(radioIndex, center_chan); */
developer72fb0bb2023-01-11 09:46:29 +080013530
developera3511852023-06-14 14:12:59 +080013531 } else {
13532 if (channel_width_MHz > 20)
13533 ext_str = "Above";
13534 }
developer72fb0bb2023-01-11 09:46:29 +080013535
developera3511852023-06-14 14:12:59 +080013536 wifi_setRadioExtChannel(radioIndex, ext_str);
developer72fb0bb2023-01-11 09:46:29 +080013537
developera3511852023-06-14 14:12:59 +080013538 char mhz_str[16];
developere40952c2023-06-15 18:46:43 +080013539 res = snprintf(mhz_str, sizeof(mhz_str), "%dMHz", width);
13540 if (os_snprintf_error(sizeof(mhz_str), res)) {
13541 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13542 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013543 }
developera3511852023-06-14 14:12:59 +080013544 wifi_setRadioOperatingChannelBandwidth(radioIndex, mhz_str);
developer72fb0bb2023-01-11 09:46:29 +080013545
developera3511852023-06-14 14:12:59 +080013546 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013547
developera3511852023-06-14 14:12:59 +080013548 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013549}
13550
13551INT wifi_getNeighboringWiFiStatus(INT radio_index, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size)
13552{
developera3511852023-06-14 14:12:59 +080013553 int index = -1;
13554 wifi_neighbor_ap2_t *scan_array = NULL;
13555 char cmd[256]={0};
13556 char buf[128]={0};
13557 char file_name[32] = {0};
13558 char filter_SSID[32] = {0};
13559 char line[256] = {0};
13560 char interface_name[16] = {0};
13561 char *ret = NULL;
13562 int freq=0;
13563 FILE *f = NULL;
developer86035662023-06-28 19:21:12 +080013564 long int channels_num = 0;
developera3511852023-06-14 14:12:59 +080013565 int vht_channel_width = 0;
13566 int get_noise_ret = RETURN_ERR;
13567 bool filter_enable = false;
13568 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
13569 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080013570 int res;
developer32f2a182023-06-27 19:50:41 +080013571 unsigned long len;
developerb14b3462023-07-01 18:02:42 +080013572 struct channels_noise *channels_noise_arr = NULL;
developer72fb0bb2023-01-11 09:46:29 +080013573
developera3511852023-06-14 14:12:59 +080013574 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013575
developere40952c2023-06-15 18:46:43 +080013576 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radio_index);
13577 if (os_snprintf_error(sizeof(file_name), res)) {
13578 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13579 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013580 }
developera3511852023-06-14 14:12:59 +080013581 f = fopen(file_name, "r");
13582 if (f != NULL) {
developer86035662023-06-28 19:21:12 +080013583 if (fgets(filter_SSID, sizeof(file_name), f) == NULL) {
13584 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080013585 if (fclose(f) != 0) {
13586 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13587 }
developer86035662023-06-28 19:21:12 +080013588 return RETURN_ERR;
13589 }
developera3511852023-06-14 14:12:59 +080013590 if (strlen(filter_SSID) != 0)
13591 filter_enable = true;
developerd14dff12023-06-28 22:47:44 +080013592 if (fclose(f) != 0) {
13593 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13594 return RETURN_ERR;
13595 }
developera3511852023-06-14 14:12:59 +080013596 }
developer72fb0bb2023-01-11 09:46:29 +080013597
developera3511852023-06-14 14:12:59 +080013598 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
13599 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013600
developera3511852023-06-14 14:12:59 +080013601 phyId = radio_index_to_phy(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080013602
developere40952c2023-06-15 18:46:43 +080013603 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
13604 if (os_snprintf_error(sizeof(cmd), res)) {
13605 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13606 return RETURN_ERR;
13607 }
developera3511852023-06-14 14:12:59 +080013608 _syscmd(cmd, buf, sizeof(buf));
developer86035662023-06-28 19:21:12 +080013609 if (hal_strtol(buf, 10, &channels_num) < 0) {
13610 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13611 return RETURN_ERR;
13612 }
developer72fb0bb2023-01-11 09:46:29 +080013613
developer5b23cd02023-07-19 20:26:03 +080013614 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 +080013615 // 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 +080013616 if (os_snprintf_error(sizeof(cmd), res)) {
13617 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13618 return RETURN_ERR;
developer5b23cd02023-07-19 20:26:03 +080013619 }
developer32f2a182023-06-27 19:50:41 +080013620
developer86035662023-06-28 19:21:12 +080013621 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developera3511852023-06-14 14:12:59 +080013622 if ((f = popen(cmd, "r")) == NULL) {
13623 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
13624 return RETURN_ERR;
13625 }
developer69b61b02023-03-07 17:17:44 +080013626
developerb14b3462023-07-01 18:02:42 +080013627 if (channels_num > 0 && channels_num <= 243) {
13628 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
developer86035662023-06-28 19:21:12 +080013629
developerb14b3462023-07-01 18:02:42 +080013630 if (channels_noise_arr == NULL) {
13631 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13632 goto err;
13633 }
13634 get_noise_ret = get_noise(radio_index, channels_noise_arr, channels_num);
developer9ce44382023-06-28 11:09:37 +080013635 }
developer86035662023-06-28 19:21:12 +080013636
developera3511852023-06-14 14:12:59 +080013637 ret = fgets(line, sizeof(line), f);
13638 while (ret != NULL) {
13639 if(strstr(line, "BSS") != NULL) { // new neighbor info
13640 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
13641 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
13642 // 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 +080013643
developera3511852023-06-14 14:12:59 +080013644 if (!filter_BSS) {
13645 index++;
developer5b23cd02023-07-19 20:26:03 +080013646 wifi_debug(DEBUG_OFF, "index=%d\n", index);
developera3511852023-06-14 14:12:59 +080013647 wifi_neighbor_ap2_t *tmp;
13648 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
13649 if (tmp == NULL) { // no more memory to use
13650 index--;
13651 wifi_dbg_printf("%s: realloc failed\n", __func__);
13652 break;
13653 }
13654 scan_array = tmp;
13655 }
13656 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +080013657
developera3511852023-06-14 14:12:59 +080013658 filter_BSS = false;
developer86035662023-06-28 19:21:12 +080013659 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
13660 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13661 goto err;
13662 }
developerc79e9172023-06-06 19:48:03 +080013663 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +080013664 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +080013665 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +080013666 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +080013667 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +080013668 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
13669 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +080013670 if (sscanf(line," freq: %d", &freq) != 1) {
13671 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +080013672 //goto err;
developer86035662023-06-28 19:21:12 +080013673 }
developera3511852023-06-14 14:12:59 +080013674 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080013675
developera3511852023-06-14 14:12:59 +080013676 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +080013677 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013678 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +080013679 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013680 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +080013681 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013682 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
13683 }
13684 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +080013685 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013686 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +080013687 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013688 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +080013689 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013690 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
13691 }
developer72fb0bb2023-01-11 09:46:29 +080013692
developera3511852023-06-14 14:12:59 +080013693 scan_array[index].ap_Noise = 0;
13694 if (get_noise_ret == RETURN_OK) {
13695 for (int i = 0; i < channels_num; i++) {
13696 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
13697 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
13698 break;
13699 }
13700 }
13701 }
13702 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +080013703 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
13704 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13705 goto err;
13706 }
developera3511852023-06-14 14:12:59 +080013707 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +080013708 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
13709 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13710 goto err;
13711 }
developer5b23cd02023-07-19 20:26:03 +080013712 } else if (strstr(line,"SSID:") != NULL) {
13713 int i;
13714 char *new_line = NULL;
13715
13716 for (i = 0; line[i] == '\t'; i++) {
13717 ;
developer86035662023-06-28 19:21:12 +080013718 }
developer5b23cd02023-07-19 20:26:03 +080013719 new_line = &line[i];
13720
13721 if (strncmp(new_line, "SSID: \n", strlen("SSID: \n")) == 0) {
13722 ret = fgets(line, sizeof(line), f);
13723 continue;
13724 }
13725 if (strstr(new_line, "HESSID") == NULL) {
13726 if (sscanf(new_line, "SSID: %63s", scan_array[index].ap_SSID) != 1) {
13727 wifi_debug(DEBUG_ERROR, "sscanf fail, index = %d\n", index);
13728 goto err;
13729 } else
13730 wifi_debug(DEBUG_ERROR, "index = %d, ssid=%s\n", index, scan_array[index].ap_SSID);
13731 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) == 0) {
13732 filter_BSS = true;
13733 }
developera3511852023-06-14 14:12:59 +080013734 }
13735 } else if (strstr(line, "Supported rates") != NULL) {
13736 char SRate[80] = {0}, *tmp = NULL;
13737 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080013738 len = strlen(line);
13739 if (len >= sizeof(SRate)) {
13740 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013741 goto err;
developer32f2a182023-06-27 19:50:41 +080013742 }
13743 strncpy(SRate, line, len);
developera3511852023-06-14 14:12:59 +080013744 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +080013745 if (tmp == NULL)
13746 goto err;
developera3511852023-06-14 14:12:59 +080013747 tmp = strtok(NULL, ":");
developer5b23cd02023-07-19 20:26:03 +080013748 if (tmp == NULL)
developer86035662023-06-28 19:21:12 +080013749 goto err;
developer5b23cd02023-07-19 20:26:03 +080013750
developer32f2a182023-06-27 19:50:41 +080013751 len = strlen(tmp);
13752 if (len >= sizeof(buf)) {
13753 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
developer86035662023-06-28 19:21:12 +080013754 goto err;
developer32f2a182023-06-27 19:50:41 +080013755 }
13756 strncpy(buf, tmp, len);
developera3511852023-06-14 14:12:59 +080013757 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +080013758
developera3511852023-06-14 14:12:59 +080013759 tmp = strtok(buf, " \n");
13760 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +080013761 if ((sizeof(SRate) - strlen(SRate)) <= strlen(tmp)) {
13762 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013763 goto err;
developer32f2a182023-06-27 19:50:41 +080013764 }
13765 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +080013766 if (SRate[strlen(SRate) - 1] == '*') {
13767 SRate[strlen(SRate) - 1] = '\0';
13768 }
developer32f2a182023-06-27 19:50:41 +080013769 if ((sizeof(SRate) - strlen(SRate)) <= 1) {
13770 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013771 goto err;
developer32f2a182023-06-27 19:50:41 +080013772 }
13773 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +080013774
developera3511852023-06-14 14:12:59 +080013775 tmp = strtok(NULL, " \n");
13776 }
13777 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +080013778 len = strlen(SRate);
13779 if (len >= sizeof(scan_array[index].ap_SupportedDataTransferRates)) {
13780 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedDataTransferRates\n");
developer86035662023-06-28 19:21:12 +080013781 goto err;
developer32f2a182023-06-27 19:50:41 +080013782 }
13783 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, len);
13784 scan_array[index].ap_SupportedDataTransferRates[len] = '\0';
developera3511852023-06-14 14:12:59 +080013785 } else if (strstr(line, "DTIM") != NULL) {
developer5b23cd02023-07-19 20:26:03 +080013786 if (sscanf(line," TIM: DTIM Count %*d DTIM Period %d %*s", &(scan_array[index].ap_DTIMPeriod)) != 1) {
developer86035662023-06-28 19:21:12 +080013787 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developer5b23cd02023-07-19 20:26:03 +080013788 goto err;
developer86035662023-06-28 19:21:12 +080013789 }
developera3511852023-06-14 14:12:59 +080013790 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013791 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13792 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013793 goto err;
developer32f2a182023-06-27 19:50:41 +080013794 }
13795 strncat(scan_array[index].ap_SupportedStandards, ",ac",
13796 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13797 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
13798 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +080013799 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013800 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 2) {
13801 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013802 goto err;
developer32f2a182023-06-27 19:50:41 +080013803 }
13804 strncat(scan_array[index].ap_SupportedStandards, ",n",
13805 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13806 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
13807 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +080013808 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013809 if (fgets(line, sizeof(line), f) == NULL) {
13810 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13811 goto err;
13812 }
13813 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
13814 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13815 goto err;
13816 }
developera3511852023-06-14 14:12:59 +080013817 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +080013818 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +080013819 } else {
developere40952c2023-06-15 18:46:43 +080013820 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +080013821 }
developere40952c2023-06-15 18:46:43 +080013822 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13823 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013824 goto err;
developer5b23cd02023-07-19 20:26:03 +080013825 }
developere40952c2023-06-15 18:46:43 +080013826
developera3511852023-06-14 14:12:59 +080013827 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
13828 continue;
13829 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013830 if (fgets(line, sizeof(line), f) == NULL) {
13831 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13832 goto err;
13833 }
developer86035662023-06-28 19:21:12 +080013834 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
13835 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13836 goto err;
13837 }
developera3511852023-06-14 14:12:59 +080013838 if (!strcmp(buf, "above")) {
13839 //40Mhz +
developere40952c2023-06-15 18:46:43 +080013840 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 +080013841 }
13842 else if (!strcmp(buf, "below")) {
13843 //40Mhz -
developere40952c2023-06-15 18:46:43 +080013844 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 +080013845 } else {
13846 //20Mhz
developere40952c2023-06-15 18:46:43 +080013847 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 +080013848 }
developere40952c2023-06-15 18:46:43 +080013849 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13850 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013851 goto err;
developer5b23cd02023-07-19 20:26:03 +080013852 }
developere40952c2023-06-15 18:46:43 +080013853
developer5b23cd02023-07-19 20:26:03 +080013854 if (strstr(line, "BSS") != NULL) { // prevent to get the next neighbor information
13855 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013856 continue;
developer5b23cd02023-07-19 20:26:03 +080013857 }
developera3511852023-06-14 14:12:59 +080013858 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013859 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13860 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013861 goto err;
developer32f2a182023-06-27 19:50:41 +080013862 }
13863 strncat(scan_array[index].ap_SupportedStandards, ",ax",
13864 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13865 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
13866 scan_array[index].ap_OperatingStandards[2] = '\0';
developerc14d83a2023-06-29 20:09:42 +080013867 if (fgets(line, sizeof(line), f) == NULL) {
13868 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13869 goto err;
13870 }
developera3511852023-06-14 14:12:59 +080013871 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013872 if (strstr(line, "HE40/2.4GHz") != NULL) {
13873 len = strlen("11AXHE40PLUS");
13874 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
13875 } else {
13876 len = strlen("11AXHE20");
13877 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
13878 }
13879 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013880 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
13881 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013882 len = strlen("11AXHE80");
13883 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
13884 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013885 ret = fgets(line, sizeof(line), f);
developer5b23cd02023-07-19 20:26:03 +080013886 } else {
13887 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013888 continue;
developer5b23cd02023-07-19 20:26:03 +080013889 }
developer32f2a182023-06-27 19:50:41 +080013890 if (strstr(line, "HE160/5GHz") != NULL) {
13891 len = strlen("11AXHE160");
13892 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
13893 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
13894 }
developera3511852023-06-14 14:12:59 +080013895 }
developer5b23cd02023-07-19 20:26:03 +080013896 wifi_debug(DEBUG_OFF, "continue\n");
developera3511852023-06-14 14:12:59 +080013897 continue;
13898 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013899 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
13900 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013901 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013902 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
13903 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013904 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +080013905 if (sscanf(line, " * Group cipher: %63s", scan_array[index].ap_EncryptionMode) != 1) {
13906 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13907 goto err;
13908 }
developera3511852023-06-14 14:12:59 +080013909 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013910 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
13911 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +080013912 }
13913 }
13914 ret = fgets(line, sizeof(line), f);
13915 }
developer72fb0bb2023-01-11 09:46:29 +080013916
developera3511852023-06-14 14:12:59 +080013917 if (!filter_BSS) {
13918 *output_array_size = index + 1;
13919 } else {
13920 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
13921 *output_array_size = index;
13922 }
13923 *neighbor_ap_array = scan_array;
13924 pclose(f);
13925 free(channels_noise_arr);
13926 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
13927 return RETURN_OK;
developer86035662023-06-28 19:21:12 +080013928err:
developerc14d83a2023-06-29 20:09:42 +080013929 pclose(f);
developer86035662023-06-28 19:21:12 +080013930 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +080013931 if (scan_array)
13932 free(scan_array);
developer86035662023-06-28 19:21:12 +080013933 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013934}
13935
13936INT wifi_getApAssociatedDeviceStats(
developera3511852023-06-14 14:12:59 +080013937 INT apIndex,
13938 mac_address_t *clientMacAddress,
13939 wifi_associated_dev_stats_t *associated_dev_stats,
13940 u64 *handle)
developer72fb0bb2023-01-11 09:46:29 +080013941{
developera3511852023-06-14 14:12:59 +080013942 wifi_associated_dev_stats_t *dev_stats = associated_dev_stats;
13943 char interface_name[50] = {0};
13944 char cmd[1024] = {0};
13945 char mac_str[18] = {0};
13946 char *key = NULL;
13947 char *val = NULL;
13948 FILE *f = NULL;
13949 char *line = NULL;
13950 size_t len = 0;
developer75bd10c2023-06-27 11:34:08 +080013951 int res;
developer72fb0bb2023-01-11 09:46:29 +080013952
developera3511852023-06-14 14:12:59 +080013953 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
13954 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
13955 return RETURN_ERR;
13956 }
developer72fb0bb2023-01-11 09:46:29 +080013957
developer32f2a182023-06-27 19:50:41 +080013958 res = snprintf(mac_str, sizeof(mac_str), "%x:%x:%x:%x:%x:%x",
13959 (*clientMacAddress)[0], (*clientMacAddress)[1], (*clientMacAddress)[2],
13960 (*clientMacAddress)[3], (*clientMacAddress)[4], (*clientMacAddress)[5]);
developer75bd10c2023-06-27 11:34:08 +080013961 if (os_snprintf_error(sizeof(mac_str), res)) {
13962 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13963 return RETURN_ERR;
13964 }
13965
13966 res = snprintf(cmd, sizeof(cmd), "iw dev %s station get %s | grep 'rx\\|tx' | tr -d '\t'", interface_name, mac_str);
13967 if (os_snprintf_error(sizeof(cmd), res)) {
13968 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13969 return RETURN_ERR;
13970 }
13971
developera3511852023-06-14 14:12:59 +080013972 if((f = popen(cmd, "r")) == NULL) {
13973 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
13974 return RETURN_ERR;
13975 }
developer72fb0bb2023-01-11 09:46:29 +080013976
developera3511852023-06-14 14:12:59 +080013977 while ((getline(&line, &len, f)) != -1) {
13978 key = strtok(line,":");
developer37646972023-06-29 10:58:43 +080013979 if (key == NULL)
13980 continue;
developera3511852023-06-14 14:12:59 +080013981 val = strtok(NULL,":");
developer37646972023-06-29 10:58:43 +080013982 if (val == NULL)
13983 continue;
developer72fb0bb2023-01-11 09:46:29 +080013984
developerb61d3362023-06-29 14:10:19 +080013985 if(!strncmp(key,"rx bytes",8))
13986 if (sscanf(val, "%llu", &dev_stats->cli_rx_bytes) != 1) {
13987 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080013988 continue;
developerb61d3362023-06-29 14:10:19 +080013989 }
13990 if(!strncmp(key,"tx bytes",8))
13991 if (sscanf(val, "%llu", &dev_stats->cli_tx_bytes) != 1) {
13992 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080013993 continue;
developerb61d3362023-06-29 14:10:19 +080013994 }
developer37646972023-06-29 10:58:43 +080013995 if(!strncmp(key,"rx packets",10)) {
13996 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
13997 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13998 continue;
13999 }
14000 }
14001 if(!strncmp(key,"tx packets",10)) {
14002 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
14003 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14004 continue;
14005 }
14006 }
14007 if(!strncmp(key,"tx retries",10)) {
14008 if (sscanf(val, "%llu", &dev_stats->cli_tx_retries) == EOF) {
14009 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14010 continue;
14011 }
14012 }
14013 if(!strncmp(key,"tx failed",9)) {
14014 if (sscanf(val, "%llu", &dev_stats->cli_tx_errors) == EOF) {
14015 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14016 continue;
14017 }
14018 }
14019 if(!strncmp(key,"rx drop misc",13)) {
14020 if (sscanf(val, "%llu", &dev_stats->cli_rx_errors) == EOF) {
14021 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14022 continue;
14023 }
developerd14dff12023-06-28 22:47:44 +080014024 }
developera3511852023-06-14 14:12:59 +080014025 if(!strncmp(key,"rx bitrate",10)) {
14026 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080014027 if (val == NULL)
14028 continue;
developer37646972023-06-29 10:58:43 +080014029 if (sscanf(val, "%lf", &dev_stats->cli_rx_rate) == EOF) {
14030 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14031 continue;
14032 }
developera3511852023-06-14 14:12:59 +080014033 }
14034 if(!strncmp(key,"tx bitrate",10)) {
14035 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080014036 if (val == NULL)
14037 continue;
developer37646972023-06-29 10:58:43 +080014038 if (sscanf(val, "%lf", &dev_stats->cli_tx_rate) == EOF) {
14039 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
14040 continue;
14041 }
developera3511852023-06-14 14:12:59 +080014042 }
14043 }
14044 free(line);
14045 pclose(f);
14046 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014047}
14048
14049INT wifi_getSSIDNameStatus(INT apIndex, CHAR *output_string)
14050{
developera3511852023-06-14 14:12:59 +080014051 char interface_name[IF_NAME_SIZE] = {0};
14052 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
developere40952c2023-06-15 18:46:43 +080014053 int res;
developer72fb0bb2023-01-11 09:46:29 +080014054
developera3511852023-06-14 14:12:59 +080014055 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080014056
developera3511852023-06-14 14:12:59 +080014057 if (NULL == output_string)
14058 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014059
developera3511852023-06-14 14:12:59 +080014060 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
14061 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +080014062
developere40952c2023-06-15 18:46:43 +080014063 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep ^ssid | cut -d '=' -f2 | tr -d '\\n'", interface_name);
14064 if (os_snprintf_error(sizeof(cmd), res)) {
14065 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14066 return RETURN_ERR;
14067 }
developera3511852023-06-14 14:12:59 +080014068 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080014069
developera3511852023-06-14 14:12:59 +080014070 //size of SSID name restricted to value less than 32 bytes
developere40952c2023-06-15 18:46:43 +080014071 res = snprintf(output_string, 32, "%s", buf);
14072 if (os_snprintf_error(32, res)) {
14073 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14074 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014075 }
developera3511852023-06-14 14:12:59 +080014076 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014077
developera3511852023-06-14 14:12:59 +080014078 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014079}
14080
14081INT wifi_getApMacAddressControlMode(INT apIndex, INT *output_filterMode)
14082{
developer2edaf012023-05-24 14:24:53 +080014083 char *mac_arry_buf = NULL;
14084 INT policy = -1;
14085 INT buf_size = 1024;
developer72fb0bb2023-01-11 09:46:29 +080014086
developer2edaf012023-05-24 14:24:53 +080014087 mac_arry_buf = malloc(buf_size);
14088 if (!mac_arry_buf) {
14089 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
developer7e4a2a62023-04-06 19:56:03 +080014090 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080014091 }
14092 memset(mac_arry_buf, 0, buf_size);
14093 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
14094 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
14095 goto err;
14096 }
14097 /*
14098 mtk format to get policy:
14099 "policy=1
14100 00:11:22:33:44:55
14101 00:11:22:33:44:66
14102 "
14103 */
14104 if (strlen(mac_arry_buf) < strlen("policy=1") || sscanf(mac_arry_buf, "policy=%01d", &policy) != 1) {
14105 wifi_debug(DEBUG_ERROR,"mac_arry_buf(%s) invalid\n", mac_arry_buf);
14106 goto err;
14107 }
14108 if (!(policy >=0 && policy <= 2)){
14109 wifi_debug(DEBUG_ERROR,"policy(%d) is invalid\n", policy);
14110 goto err;
14111 }
14112 *output_filterMode = policy;
14113 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), success\n", *output_filterMode);
14114 free(mac_arry_buf);
14115 mac_arry_buf = NULL;
14116 return RETURN_OK;
14117err:
14118 free(mac_arry_buf);
14119 mac_arry_buf = NULL;
14120 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), fails\n", *output_filterMode);
14121 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014122}
14123
developer2edaf012023-05-24 14:24:53 +080014124
developer72fb0bb2023-01-11 09:46:29 +080014125INT wifi_getApAssociatedDeviceDiagnosticResult2(INT apIndex,wifi_associated_dev2_t **associated_dev_array,UINT *output_array_size)
14126{
developera3511852023-06-14 14:12:59 +080014127 FILE *fp = NULL;
14128 char str[MAX_BUF_SIZE] = {0};
14129 int wificlientindex = 0 ;
14130 int count = 0;
14131 int signalstrength = 0;
14132 int arr[MACADDRESS_SIZE] = {0};
14133 unsigned char mac[MACADDRESS_SIZE] = {0};
14134 UINT wifi_count = 0;
14135 char pipeCmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014136 int res;
developer72fb0bb2023-01-11 09:46:29 +080014137
developera3511852023-06-14 14:12:59 +080014138 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14139 *output_array_size = 0;
14140 *associated_dev_array = NULL;
14141 char interface_name[50] = {0};
developer72fb0bb2023-01-11 09:46:29 +080014142
developera3511852023-06-14 14:12:59 +080014143 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
14144 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
14145 return RETURN_ERR;
14146 }
developer72fb0bb2023-01-11 09:46:29 +080014147
developer75bd10c2023-06-27 11:34:08 +080014148 res = snprintf(pipeCmd, sizeof(pipeCmd), "iw dev %s station dump | grep %s | wc -l", interface_name, interface_name);
14149 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14150 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14151 return RETURN_ERR;
14152 }
developera3511852023-06-14 14:12:59 +080014153 fp = popen(pipeCmd, "r");
14154 if (fp == NULL)
14155 {
14156 printf("Failed to run command inside function %s\n",__FUNCTION__ );
14157 return RETURN_ERR;
14158 }
developer72fb0bb2023-01-11 09:46:29 +080014159
developera3511852023-06-14 14:12:59 +080014160 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080014161 if (fgets(str, sizeof(str)-1, fp) == NULL) {
14162 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14163 pclose(fp);
14164 return RETURN_ERR;
14165 }
developera3511852023-06-14 14:12:59 +080014166 wifi_count = (unsigned int) atoi ( str );
14167 *output_array_size = wifi_count;
14168 wifi_dbg_printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
14169 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014170
developera3511852023-06-14 14:12:59 +080014171 if(wifi_count == 0)
14172 {
14173 return RETURN_OK;
14174 }
14175 else
14176 {
14177 wifi_associated_dev2_t* temp = NULL;
14178 temp = (wifi_associated_dev2_t*)calloc(wifi_count, sizeof(wifi_associated_dev2_t));
14179 *associated_dev_array = temp;
14180 if(temp == NULL)
14181 {
14182 printf("Error Statement. Insufficient memory \n");
14183 return RETURN_ERR;
14184 }
developer72fb0bb2023-01-11 09:46:29 +080014185
developer33f13ba2023-07-12 16:19:06 +080014186 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
14187 if (res) {
14188 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080014189 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014190 }
developere40952c2023-06-15 18:46:43 +080014191
developera3511852023-06-14 14:12:59 +080014192 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
14193 if(fp == NULL)
14194 {
14195 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
14196 return RETURN_ERR;
14197 }
developere75ba632023-06-29 16:03:33 +080014198 if (fclose(fp) == EOF) {
14199 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14200 return RETURN_ERR;
14201 }
developer72fb0bb2023-01-11 09:46:29 +080014202
developer86035662023-06-28 19:21:12 +080014203 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
14204 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14205 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14206 return RETURN_ERR;
14207 }
developera3511852023-06-14 14:12:59 +080014208 fp = popen(pipeCmd, "r");
14209 if(fp)
14210 {
14211 for(count =0 ; count < wifi_count; count++)
14212 {
developer86035662023-06-28 19:21:12 +080014213 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14214 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14215 pclose(fp);
14216 return RETURN_ERR;
14217 }
developera3511852023-06-14 14:12:59 +080014218 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
14219 {
14220 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
14221 {
14222 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080014223
developera3511852023-06-14 14:12:59 +080014224 }
14225 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
14226 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]);
14227 }
14228 temp[count].cli_AuthenticationState = 1; //TODO
14229 temp[count].cli_Active = 1; //TODO
14230 }
14231 pclose(fp);
14232 }
developer72fb0bb2023-01-11 09:46:29 +080014233
developera3511852023-06-14 14:12:59 +080014234 //Updating RSSI per client
developer86035662023-06-28 19:21:12 +080014235 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep signal | tr -s ' ' | cut -d ' ' -f 2 > /tmp/wifi_signalstrength.txt");
14236 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14237 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14238 return RETURN_ERR;
14239 }
14240
developera3511852023-06-14 14:12:59 +080014241 fp = popen(pipeCmd, "r");
14242 if(fp)
14243 {
14244 pclose(fp);
14245 }
14246 fp = popen("cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2","r");
14247 if(fp)
14248 {
14249 for(count =0 ; count < wifi_count ;count++)
14250 {
developer86035662023-06-28 19:21:12 +080014251 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14252 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14253 pclose(fp);
14254 return RETURN_ERR;
14255 }
developera3511852023-06-14 14:12:59 +080014256 signalstrength = atoi(str);
14257 temp[count].cli_RSSI = signalstrength;
14258 }
14259 pclose(fp);
14260 }
developer72fb0bb2023-01-11 09:46:29 +080014261
14262
developera3511852023-06-14 14:12:59 +080014263 //LastDataDownlinkRate
developer86035662023-06-28 19:21:12 +080014264 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Send.txt");
14265 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14266 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14267 return RETURN_ERR;
14268 }
developera3511852023-06-14 14:12:59 +080014269 fp = popen(pipeCmd, "r");
14270 if (fp)
14271 {
14272 pclose(fp);
14273 }
14274 fp = popen("cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2", "r");
14275 if (fp)
14276 {
developerc14d83a2023-06-29 20:09:42 +080014277 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014278 for (count = 0; count < wifi_count; count++)
14279 {
developer86035662023-06-28 19:21:12 +080014280 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14281 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14282 pclose(fp);
14283 return RETURN_ERR;
14284 }
developerc14d83a2023-06-29 20:09:42 +080014285 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14286 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14287 }
14288 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014289 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
14290 }
14291 pclose(fp);
14292 }
developer72fb0bb2023-01-11 09:46:29 +080014293
developera3511852023-06-14 14:12:59 +080014294 //LastDataUplinkRate
developer86035662023-06-28 19:21:12 +080014295 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep 'rx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Received.txt");
14296 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14297 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14298 return RETURN_ERR;
14299 }
developera3511852023-06-14 14:12:59 +080014300 fp = popen(pipeCmd, "r");
14301 if (fp)
14302 {
14303 pclose(fp);
14304 }
14305 fp = popen("cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2", "r");
14306 if (fp)
14307 {
developerc14d83a2023-06-29 20:09:42 +080014308 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014309 for (count = 0; count < wifi_count; count++)
14310 {
developer86035662023-06-28 19:21:12 +080014311 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14312 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14313 pclose(fp);
14314 return RETURN_ERR;
14315 }
developerc14d83a2023-06-29 20:09:42 +080014316 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14317 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14318 }
14319 temp[count].cli_LastDataUplinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014320 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
14321 }
14322 pclose(fp);
14323 }
14324 }
14325 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14326 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014327
14328}
14329
14330INT wifi_getSSIDTrafficStats2(INT ssidIndex,wifi_ssidTrafficStats2_t *output_struct)
14331{
developera3511852023-06-14 14:12:59 +080014332 FILE *fp = NULL;
14333 char interface_name[50] = {0};
14334 char pipeCmd[128] = {0};
14335 char str[256] = {0};
14336 wifi_ssidTrafficStats2_t *out = output_struct;
developer75bd10c2023-06-27 11:34:08 +080014337 int res;
developerd14dff12023-06-28 22:47:44 +080014338 unsigned int recv;
developer72fb0bb2023-01-11 09:46:29 +080014339
developera3511852023-06-14 14:12:59 +080014340 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
14341 if (!output_struct)
14342 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014343
developera3511852023-06-14 14:12:59 +080014344 memset(out, 0, sizeof(wifi_ssidTrafficStats2_t));
14345 if (wifi_GetInterfaceName(ssidIndex, interface_name) != RETURN_OK)
14346 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080014347 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /proc/net/dev | grep %s", interface_name);
14348 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14349 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14350 return RETURN_ERR;
14351 }
developer72fb0bb2023-01-11 09:46:29 +080014352
developera3511852023-06-14 14:12:59 +080014353 fp = popen(pipeCmd, "r");
14354 if (fp == NULL) {
developer86035662023-06-28 19:21:12 +080014355 wifi_debug(DEBUG_ERROR, "%s: popen failed\n", __func__);
14356 return RETURN_ERR;
14357 }
14358 if (fgets(str, sizeof(str), fp) == NULL) {
14359 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14360 pclose(fp);
developera3511852023-06-14 14:12:59 +080014361 return RETURN_ERR;
14362 }
developer5b23cd02023-07-19 20:26:03 +080014363
developera3511852023-06-14 14:12:59 +080014364 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014365
developera3511852023-06-14 14:12:59 +080014366 if (strlen(str) == 0) // interface not exist
14367 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014368
developerd14dff12023-06-28 22:47:44 +080014369 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 +080014370 &out->ssid_DiscardedPacketsReceived, &out->ssid_BytesSent, &out->ssid_PacketsSent, &out->ssid_ErrorsSent, &out->ssid_DiscardedPacketsSent);
developerd14dff12023-06-28 22:47:44 +080014371 if (recv != 8) {
14372 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
14373 return RETURN_ERR;
14374 }
developer72fb0bb2023-01-11 09:46:29 +080014375
developera3511852023-06-14 14:12:59 +080014376 memset(str, 0, sizeof(str));
developer75bd10c2023-06-27 11:34:08 +080014377
14378 res = snprintf(pipeCmd, sizeof(pipeCmd), "tail -n1 /proc/net/netstat");
14379 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14380 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14381 return RETURN_ERR;
14382 }
developera3511852023-06-14 14:12:59 +080014383 fp = popen(pipeCmd, "r");
14384 if (fp == NULL) {
developer75bd10c2023-06-27 11:34:08 +080014385 wifi_debug(DEBUG_ERROR, "popen failed\n");
developera3511852023-06-14 14:12:59 +080014386 return RETURN_ERR;
14387 }
developer86035662023-06-28 19:21:12 +080014388
14389 if (fgets(str, sizeof(str), fp) == NULL) {
14390 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14391 pclose(fp);
14392 return RETURN_ERR;
14393 }
developer72fb0bb2023-01-11 09:46:29 +080014394
developer37646972023-06-29 10:58:43 +080014395 if (sscanf(str, "%*[^:]: %lu %lu %lu %lu", &out->ssid_MulticastPacketsReceived, &out->ssid_MulticastPacketsSent, &out->ssid_BroadcastPacketsRecevied, \
14396 &out->ssid_BroadcastPacketsSent) == EOF)
14397 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developera3511852023-06-14 14:12:59 +080014398 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014399
developera3511852023-06-14 14:12:59 +080014400 out->ssid_UnicastPacketsSent = out->ssid_PacketsSent - out->ssid_MulticastPacketsSent - out->ssid_BroadcastPacketsSent - out->ssid_DiscardedPacketsSent;
14401 out->ssid_UnicastPacketsReceived = out->ssid_PacketsReceived - out->ssid_MulticastPacketsReceived - out->ssid_BroadcastPacketsRecevied - out->ssid_DiscardedPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +080014402
developera3511852023-06-14 14:12:59 +080014403 // Not supported
14404 output_struct->ssid_RetransCount = 0;
14405 output_struct->ssid_FailedRetransCount = 0;
14406 output_struct->ssid_RetryCount = 0;
14407 output_struct->ssid_MultipleRetryCount = 0;
14408 output_struct->ssid_ACKFailureCount = 0;
14409 output_struct->ssid_AggregatedPacketCount = 0;
developer72fb0bb2023-01-11 09:46:29 +080014410
developera3511852023-06-14 14:12:59 +080014411 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014412}
14413
14414//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).
14415INT wifi_getApIsolationEnable(INT apIndex, BOOL *output)
14416{
developera3511852023-06-14 14:12:59 +080014417 char output_val[16]={'\0'};
14418 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080014419 int res;
developer72fb0bb2023-01-11 09:46:29 +080014420
developera3511852023-06-14 14:12:59 +080014421 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14422 if (!output)
14423 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080014424
14425 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
14426 if (os_snprintf_error(sizeof(config_file), res)) {
14427 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14428 return RETURN_ERR;
14429 }
developera3511852023-06-14 14:12:59 +080014430 wifi_hostapdRead(config_file, "ap_isolate", output_val, sizeof(output_val));
developer72fb0bb2023-01-11 09:46:29 +080014431
developera3511852023-06-14 14:12:59 +080014432 if( strcmp(output_val,"1") == 0 )
14433 *output = TRUE;
14434 else
14435 *output = FALSE;
14436 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014437
developera3511852023-06-14 14:12:59 +080014438 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014439}
14440
14441INT wifi_setApIsolationEnable(INT apIndex, BOOL enable)
14442{
developera3511852023-06-14 14:12:59 +080014443 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14444 char string[MAX_BUF_SIZE]={'\0'};
14445 char config_file[MAX_BUF_SIZE] = {0};
14446 struct params params;
developer75bd10c2023-06-27 11:34:08 +080014447 int res;
developer72fb0bb2023-01-11 09:46:29 +080014448
developer32f2a182023-06-27 19:50:41 +080014449 string[0] = enable == TRUE ? '1' : '0';
developer72fb0bb2023-01-11 09:46:29 +080014450
developera3511852023-06-14 14:12:59 +080014451 params.name = "ap_isolate";
14452 params.value = string;
developer72fb0bb2023-01-11 09:46:29 +080014453
developer75bd10c2023-06-27 11:34:08 +080014454 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
14455 if (os_snprintf_error(sizeof(config_file), res)) {
14456 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14457 return RETURN_ERR;
14458 }
14459
developera3511852023-06-14 14:12:59 +080014460 wifi_hostapdWrite(config_file,&params,1);
14461 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014462
developera3511852023-06-14 14:12:59 +080014463 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014464}
14465
14466INT wifi_getApManagementFramePowerControl(INT apIndex, INT *output_dBm)
14467{
developera3511852023-06-14 14:12:59 +080014468 char mgmtpwr_file[32] = {0};
14469 char cmd[64] = {0};
14470 char buf[32]={0};
developere40952c2023-06-15 18:46:43 +080014471 int res;
developerc14d83a2023-06-29 20:09:42 +080014472 long int tmp;
developera1255e42023-05-13 17:45:02 +080014473
developera3511852023-06-14 14:12:59 +080014474 if (NULL == output_dBm)
14475 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080014476 res = snprintf(mgmtpwr_file, sizeof(mgmtpwr_file), "%s%d.txt", MGMT_POWER_CTRL, apIndex);
14477 if (os_snprintf_error(sizeof(mgmtpwr_file), res)) {
14478 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14479 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014480 }
developere40952c2023-06-15 18:46:43 +080014481
14482 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", mgmtpwr_file);
14483 if (os_snprintf_error(sizeof(cmd), res)) {
14484 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14485 return RETURN_ERR;
14486 }
developera3511852023-06-14 14:12:59 +080014487 _syscmd(cmd, buf, sizeof(buf));
developer5b23cd02023-07-19 20:26:03 +080014488 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080014489 if (hal_strtol(buf, 10, &tmp) < 0) {
14490 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080014491 }
developerc14d83a2023-06-29 20:09:42 +080014492 *output_dBm = tmp;
developerd14dff12023-06-28 22:47:44 +080014493 } else
developera1255e42023-05-13 17:45:02 +080014494 *output_dBm = 23;
developera3511852023-06-14 14:12:59 +080014495 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014496}
14497
14498INT wifi_setApManagementFramePowerControl(INT wlanIndex, INT dBm)
14499{
developera1255e42023-05-13 17:45:02 +080014500 char interface_name[16] = {0};
developera1255e42023-05-13 17:45:02 +080014501 char mgmt_pwr_file[128]={0};
14502 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +080014503 int if_idx, ret = 0;
14504 struct nl_msg *msg = NULL;
14505 struct nlattr * msg_data = NULL;
14506 struct mtk_nl80211_param param;
14507 struct unl unl_ins;
14508 char power[16] = {0};
developere40952c2023-06-15 18:46:43 +080014509 int res;
developera1255e42023-05-13 17:45:02 +080014510
14511 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14512
14513 if (wifi_GetInterfaceName(wlanIndex, interface_name) != RETURN_OK)
14514 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +080014515
14516 if_idx = if_nametoindex(interface_name);
14517 /*init mtk nl80211 vendor cmd*/
14518 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
14519 param.if_type = NL80211_ATTR_IFINDEX;
14520 param.if_idx = if_idx;
14521
14522 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14523 if (ret) {
14524 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14525 return RETURN_ERR;
14526 }
14527
14528 /*add mtk vendor cmd data*/
developere40952c2023-06-15 18:46:43 +080014529 res = snprintf(power, sizeof(power), "%d", dBm);
14530 if (os_snprintf_error(sizeof(power), res)) {
14531 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14532 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014533 }
developere40952c2023-06-15 18:46:43 +080014534
developerfead3972023-05-25 20:15:02 +080014535 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_MGMT, strlen(power), power)) {
14536 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14537 nlmsg_free(msg);
14538 goto err;
14539 }
14540
14541 /*send mtk nl80211 vendor msg*/
14542 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14543 if (ret) {
14544 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14545 goto err;
14546 }
14547
14548 /*deinit mtk nl80211 vendor msg*/
14549 mtk_nl80211_deint(&unl_ins);
14550 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14551
developere40952c2023-06-15 18:46:43 +080014552 res = snprintf(mgmt_pwr_file, sizeof(mgmt_pwr_file), "%s%d.txt", MGMT_POWER_CTRL, wlanIndex);
14553 if (os_snprintf_error(sizeof(mgmt_pwr_file), res)) {
14554 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14555 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014556 }
developere40952c2023-06-15 18:46:43 +080014557
developera1255e42023-05-13 17:45:02 +080014558 f = fopen(mgmt_pwr_file, "w");
14559 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +080014560 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera1255e42023-05-13 17:45:02 +080014561 return RETURN_ERR;
14562 }
14563 fprintf(f, "%d", dBm);
developerc14d83a2023-06-29 20:09:42 +080014564 if (fclose(f) == EOF)
14565 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14566
developer72fb0bb2023-01-11 09:46:29 +080014567 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +080014568err:
14569 mtk_nl80211_deint(&unl_ins);
14570 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14571 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014572}
14573INT wifi_getRadioDcsChannelMetrics(INT radioIndex,wifi_channelMetrics_t *input_output_channelMetrics_array,INT size)
14574{
developera3511852023-06-14 14:12:59 +080014575 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014576}
14577INT wifi_setRadioDcsDwelltime(INT radioIndex, INT ms)
14578{
developera3511852023-06-14 14:12:59 +080014579 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014580}
14581INT wifi_getRadioDcsDwelltime(INT radioIndex, INT *ms)
14582{
developera3511852023-06-14 14:12:59 +080014583 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014584}
14585INT wifi_setRadioDcsScanning(INT radioIndex, BOOL enable)
14586{
developera3511852023-06-14 14:12:59 +080014587 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014588}
14589INT wifi_setBSSTransitionActivation(UINT apIndex, BOOL activate)
14590{
developera3511852023-06-14 14:12:59 +080014591 char config_file[MAX_BUF_SIZE] = {0};
14592 struct params list;
developere40952c2023-06-15 18:46:43 +080014593 int res;
developer72fb0bb2023-01-11 09:46:29 +080014594
developera3511852023-06-14 14:12:59 +080014595 list.name = "bss_transition";
14596 list.value = activate?"1":"0";
developere40952c2023-06-15 18:46:43 +080014597 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
14598 if (os_snprintf_error(sizeof(config_file), res)) {
14599 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14600 return RETURN_ERR;
14601 }
developera3511852023-06-14 14:12:59 +080014602 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080014603
developera3511852023-06-14 14:12:59 +080014604 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014605}
14606wifi_apAuthEvent_callback apAuthEvent_cb = NULL;
14607
14608void wifi_apAuthEvent_callback_register(wifi_apAuthEvent_callback callback_proc)
14609{
developera3511852023-06-14 14:12:59 +080014610 return;
developer72fb0bb2023-01-11 09:46:29 +080014611}
14612
14613INT wifi_setApCsaDeauth(INT apIndex, INT mode)
14614{
developera3511852023-06-14 14:12:59 +080014615 // TODO Implement me!
14616 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014617}
14618
14619INT wifi_setApScanFilter(INT apIndex, INT mode, CHAR *essid)
14620{
developera3511852023-06-14 14:12:59 +080014621 char file_name[128] = {0};
14622 FILE *f = NULL;
14623 int max_num_radios = 0;
developer75bd10c2023-06-27 11:34:08 +080014624 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080014625
developera3511852023-06-14 14:12:59 +080014626 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014627
developera3511852023-06-14 14:12:59 +080014628 wifi_getMaxRadioNumber(&max_num_radios);
14629 if (essid == NULL || strlen(essid) == 0 || apIndex == -1) {
14630 for (int index = 0; index < max_num_radios; index++) {
developere40952c2023-06-15 18:46:43 +080014631 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, index);
14632 if (os_snprintf_error(sizeof(file_name), res)) {
14633 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14634 return RETURN_ERR;
14635 }
14636
developera3511852023-06-14 14:12:59 +080014637 f = fopen(file_name, "w");
14638 if (f == NULL)
14639 return RETURN_ERR;
14640 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014641 if (mode) {
14642 ret = fprintf(f, "%s", essid);
14643 if (ret < 0)
14644 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14645 }
developer72fb0bb2023-01-11 09:46:29 +080014646
developerd14dff12023-06-28 22:47:44 +080014647 if (fclose(f) != 0) {
14648 wifi_debug(DEBUG_ERROR, "fclose fail\n");
14649 return RETURN_ERR;
14650 }
developera3511852023-06-14 14:12:59 +080014651 }
14652 } else { // special case, need to set AP's SSID as filter for each radio.
developere40952c2023-06-15 18:46:43 +080014653 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, apIndex);
14654 if (os_snprintf_error(sizeof(file_name), res)) {
14655 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14656 return RETURN_ERR;
14657 }
14658
developera3511852023-06-14 14:12:59 +080014659 f = fopen(file_name, "w");
14660 if (f == NULL)
14661 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014662
developera3511852023-06-14 14:12:59 +080014663 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014664 if (mode) {
14665 ret = fprintf(f, "%s", essid);
14666 if (ret < 0)
14667 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14668 }
developer72fb0bb2023-01-11 09:46:29 +080014669
developer37646972023-06-29 10:58:43 +080014670 if (fclose(f) == EOF) {
14671 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14672 return RETURN_ERR;
14673 }
developera3511852023-06-14 14:12:59 +080014674 }
developer72fb0bb2023-01-11 09:46:29 +080014675
developera3511852023-06-14 14:12:59 +080014676 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14677 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014678}
14679
14680INT wifi_pushRadioChannel(INT radioIndex, UINT channel)
14681{
developera3511852023-06-14 14:12:59 +080014682 // TODO Implement me!
14683 //Apply wifi_pushRadioChannel() instantly
14684 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014685}
14686
14687INT wifi_setRadioStatsEnable(INT radioIndex, BOOL enable)
14688{
developera3511852023-06-14 14:12:59 +080014689 // TODO Implement me!
14690 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014691}
14692
developer72fb0bb2023-01-11 09:46:29 +080014693
developera3511852023-06-14 14:12:59 +080014694static int tidStats_callback(struct nl_msg *msg, void *arg) {
14695 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14696 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
14697 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
14698 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1],*tidattr;
14699 int rem , tid_index = 0;
developer72fb0bb2023-01-11 09:46:29 +080014700
developera3511852023-06-14 14:12:59 +080014701 wifi_associated_dev_tid_stats_t *out = (wifi_associated_dev_tid_stats_t*)arg;
14702 wifi_associated_dev_tid_entry_t *stats_entry;
developer72fb0bb2023-01-11 09:46:29 +080014703
developera3511852023-06-14 14:12:59 +080014704 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
14705 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
14706 };
14707 static struct nla_policy tid_policy[NL80211_TID_STATS_MAX + 1] = {
14708 [NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
14709 };
developer72fb0bb2023-01-11 09:46:29 +080014710
developera3511852023-06-14 14:12:59 +080014711 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
14712 genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080014713
developer72fb0bb2023-01-11 09:46:29 +080014714
developera3511852023-06-14 14:12:59 +080014715 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014716 wifi_debug(DEBUG_ERROR, "station stats missing!\n");
developera3511852023-06-14 14:12:59 +080014717 return NL_SKIP;
14718 }
developer72fb0bb2023-01-11 09:46:29 +080014719
developera3511852023-06-14 14:12:59 +080014720 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
14721 tb[NL80211_ATTR_STA_INFO],
14722 stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014723 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014724 return NL_SKIP;
14725 }
developer72fb0bb2023-01-11 09:46:29 +080014726
developera3511852023-06-14 14:12:59 +080014727 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14728 nla_for_each_nested(tidattr, sinfo[NL80211_STA_INFO_TID_STATS], rem)
14729 {
14730 stats_entry = &out->tid_array[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014731
developera3511852023-06-14 14:12:59 +080014732 stats_entry->tid = tid_index;
14733 stats_entry->ac = _tid_ac_index_get[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014734
developera3511852023-06-14 14:12:59 +080014735 if(sinfo[NL80211_STA_INFO_TID_STATS])
14736 {
14737 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,tidattr, tid_policy)) {
14738 printf("failed to parse nested stats attributes!");
14739 return NL_SKIP;
14740 }
14741 }
14742 if(stats_info[NL80211_TID_STATS_TX_MSDU])
14743 stats_entry->num_msdus = (unsigned long long)nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
developer72fb0bb2023-01-11 09:46:29 +080014744
developera3511852023-06-14 14:12:59 +080014745 if(tid_index < (PS_MAX_TID - 1))
14746 tid_index++;
14747 }
14748 }
14749 //ToDo: sum_time_ms, ewma_time_ms
14750 return NL_SKIP;
14751}
developer72fb0bb2023-01-11 09:46:29 +080014752
developera3511852023-06-14 14:12:59 +080014753INT wifi_getApAssociatedDeviceTidStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_tid_stats_t *tid_stats, ULLONG *handle)
14754{
14755 Netlink nl;
14756 char if_name[IF_NAME_SIZE];
14757 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014758 int res;
developer72fb0bb2023-01-11 09:46:29 +080014759
developera3511852023-06-14 14:12:59 +080014760 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
14761 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014762
developere40952c2023-06-15 18:46:43 +080014763 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
14764 if (os_snprintf_error(sizeof(if_name), res)) {
14765 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14766 return RETURN_ERR;
14767 }
developer72fb0bb2023-01-11 09:46:29 +080014768
developera3511852023-06-14 14:12:59 +080014769 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080014770
developera3511852023-06-14 14:12:59 +080014771 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080014772 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080014773 return -1;
14774 }
developer72fb0bb2023-01-11 09:46:29 +080014775
developera3511852023-06-14 14:12:59 +080014776 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080014777
developera3511852023-06-14 14:12:59 +080014778 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080014779 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080014780 nlfree(&nl);
14781 return -2;
14782 }
developer72fb0bb2023-01-11 09:46:29 +080014783
developera3511852023-06-14 14:12:59 +080014784 genlmsg_put(msg,
14785 NL_AUTO_PID,
14786 NL_AUTO_SEQ,
14787 nl.id,
14788 0,
14789 0,
14790 NL80211_CMD_GET_STATION,
14791 0);
developer72fb0bb2023-01-11 09:46:29 +080014792
developera3511852023-06-14 14:12:59 +080014793 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
14794 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
14795 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,tidStats_callback,tid_stats);
14796 nl_send_auto_complete(nl.socket, msg);
14797 nl_recvmsgs(nl.socket, nl.cb);
14798 nlmsg_free(msg);
14799 nlfree(&nl);
14800 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014801}
14802
14803
14804INT wifi_startNeighborScan(INT apIndex, wifi_neighborScanMode_t scan_mode, INT dwell_time, UINT chan_num, UINT *chan_list)
14805{
developera3511852023-06-14 14:12:59 +080014806 char interface_name[16] = {0};
14807 char cmd[128]={0};
14808 char buf[128]={0};
14809 int freq = 0;
developere40952c2023-06-15 18:46:43 +080014810 int res;
developer72fb0bb2023-01-11 09:46:29 +080014811
developera3511852023-06-14 14:12:59 +080014812 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014813
developera3511852023-06-14 14:12:59 +080014814 // full mode is used to scan all channels.
14815 // multiple channels is ambiguous, iw can not set multiple frequencies in one time.
14816 if (scan_mode != WIFI_RADIO_SCAN_MODE_FULL)
14817 ieee80211_channel_to_frequency(chan_list[0], &freq);
developer72fb0bb2023-01-11 09:46:29 +080014818
developera3511852023-06-14 14:12:59 +080014819 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
14820 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014821
developera3511852023-06-14 14:12:59 +080014822 if (freq)
developere40952c2023-06-15 18:46:43 +080014823 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan trigger duration %d freq %d", interface_name, dwell_time, freq);
developera3511852023-06-14 14:12:59 +080014824 else
developere40952c2023-06-15 18:46:43 +080014825 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan trigger duration %d", interface_name, dwell_time);
14826 if (os_snprintf_error(sizeof(cmd), res)) {
14827 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14828 return RETURN_ERR;
14829 }
developer72fb0bb2023-01-11 09:46:29 +080014830
developera3511852023-06-14 14:12:59 +080014831 _syscmd(cmd, buf, sizeof(buf));
14832 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014833
developera3511852023-06-14 14:12:59 +080014834 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014835}
14836
14837
14838INT wifi_steering_setGroup(UINT steeringgroupIndex, wifi_steering_apConfig_t *cfg_2, wifi_steering_apConfig_t *cfg_5)
14839{
developera3511852023-06-14 14:12:59 +080014840 // TODO Implement me!
14841 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014842}
14843
14844INT wifi_steering_clientSet(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_steering_clientConfig_t *config)
14845{
developera3511852023-06-14 14:12:59 +080014846 // TODO Implement me!
14847 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014848}
14849
14850INT wifi_steering_clientRemove(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14851{
developera3511852023-06-14 14:12:59 +080014852 // TODO Implement me!
14853 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014854}
14855
14856INT wifi_steering_clientMeasure(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14857{
developera3511852023-06-14 14:12:59 +080014858 // TODO Implement me!
14859 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014860}
14861
14862INT wifi_steering_clientDisconnect(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_disconnectType_t type, UINT reason)
14863{
developera3511852023-06-14 14:12:59 +080014864 // TODO Implement me!
14865 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014866}
14867
14868INT wifi_steering_eventRegister(wifi_steering_eventCB_t event_cb)
14869{
developera3511852023-06-14 14:12:59 +080014870 // TODO Implement me!
14871 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014872}
14873
14874INT wifi_steering_eventUnregister(void)
14875{
developera3511852023-06-14 14:12:59 +080014876 // TODO Implement me!
14877 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014878}
14879
14880INT wifi_delApAclDevices(INT apIndex)
14881{
developer7e4a2a62023-04-06 19:56:03 +080014882 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +080014883 struct unl unl_ins;
14884 int if_idx = 0, ret = 0;
14885 struct nl_msg *msg = NULL;
14886 struct nlattr * msg_data = NULL;
14887 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +080014888
developer7e4a2a62023-04-06 19:56:03 +080014889 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
14890 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080014891 if_idx = if_nametoindex(inf_name);
14892 if (!if_idx) {
14893 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
14894 return RETURN_ERR;
14895 }
14896 /*init mtk nl80211 vendor cmd*/
14897 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
14898 param.if_type = NL80211_ATTR_IFINDEX;
14899 param.if_idx = if_idx;
14900 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14901 if (ret) {
14902 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14903 return RETURN_ERR;
14904 }
14905 /*add mtk vendor cmd data*/
14906 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_CLEAR_ALL)) {
14907 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14908 nlmsg_free(msg);
14909 goto err;
14910 }
14911 /*send mtk nl80211 vendor msg*/
14912 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14913 if (ret) {
14914 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14915 goto err;
14916 }
14917 /*deinit mtk nl80211 vendor msg*/
14918 mtk_nl80211_deint(&unl_ins);
14919 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14920 return RETURN_OK;
14921err:
14922 mtk_nl80211_deint(&unl_ins);
14923 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14924 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014925
developera3511852023-06-14 14:12:59 +080014926 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014927}
14928
developer72fb0bb2023-01-11 09:46:29 +080014929static int rxStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080014930 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14931 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080014932 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
14933 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080014934 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
14935 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080014936
developera3511852023-06-14 14:12:59 +080014937 nla_parse(tb,
14938 NL80211_ATTR_MAX,
14939 genlmsg_attrdata(gnlh, 0),
14940 genlmsg_attrlen(gnlh, 0),
14941 NULL);
developer72fb0bb2023-01-11 09:46:29 +080014942
developera3511852023-06-14 14:12:59 +080014943 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014944 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080014945 return NL_SKIP;
14946 }
developer72fb0bb2023-01-11 09:46:29 +080014947
developera3511852023-06-14 14:12:59 +080014948 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014949 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014950 return NL_SKIP;
14951 }
14952 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080014953
developera3511852023-06-14 14:12:59 +080014954 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080014955
developera3511852023-06-14 14:12:59 +080014956 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
14957 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy )) {
developer75bd10c2023-06-27 11:34:08 +080014958 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080014959 return NL_SKIP;
14960 }
14961 }
developer72fb0bb2023-01-11 09:46:29 +080014962
developera3511852023-06-14 14:12:59 +080014963 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14964 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
14965 printf("failed to parse nested stats attributes!");
14966 return NL_SKIP;
14967 }
14968 }
14969 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
developer72fb0bb2023-01-11 09:46:29 +080014970
developera3511852023-06-14 14:12:59 +080014971 if( nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]) )
14972 {
14973 printf("Type is VHT\n");
14974 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
14975 ((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 +080014976
developera3511852023-06-14 14:12:59 +080014977 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
14978 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 1;
14979 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
14980 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14981 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
14982 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14983 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
14984 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14985 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]) )
14986 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
14987 } else {
14988 printf(" OFDM or CCK \n");
14989 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
14990 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->nss = 0;
14991 }
14992 }
developer72fb0bb2023-01-11 09:46:29 +080014993
developera3511852023-06-14 14:12:59 +080014994 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
developereff896f2023-05-29 14:52:55 +080014995 if(rinfo[NL80211_RATE_INFO_MCS])
14996 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
14997 }
developera3511852023-06-14 14:12:59 +080014998 if (sinfo[NL80211_STA_INFO_RX_BYTES64])
developereff896f2023-05-29 14:52:55 +080014999 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]);
15000 else if (sinfo[NL80211_STA_INFO_RX_BYTES])
15001 ((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 +080015002
developera3511852023-06-14 14:12:59 +080015003 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
15004 if (stats_info[NL80211_TID_STATS_RX_MSDU])
developereff896f2023-05-29 14:52:55 +080015005 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_RX_MSDU]);
15006 }
developer72fb0bb2023-01-11 09:46:29 +080015007
developereff896f2023-05-29 14:52:55 +080015008 if (sinfo[NL80211_STA_INFO_SIGNAL])
15009 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->rssi_combined = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
15010 //Assigning 0 for RETRIES ,PPDUS and MPDUS as we dont have rx retries attribute in libnl_3.3.0
15011 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->retries = 0;
15012 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->ppdus = 0;
15013 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = 0;
15014 //rssi_array need to be filled
15015 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015016}
developer72fb0bb2023-01-11 09:46:29 +080015017
15018INT wifi_getApAssociatedDeviceRxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_rx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
15019{
developera3511852023-06-14 14:12:59 +080015020 Netlink nl;
15021 char if_name[32];
15022 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
15023 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015024
developera3511852023-06-14 14:12:59 +080015025 *output_array_size = sizeof(wifi_associated_dev_rate_info_rx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080015026
developera3511852023-06-14 14:12:59 +080015027 if (*output_array_size <= 0)
15028 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015029
developera3511852023-06-14 14:12:59 +080015030 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015031
developera3511852023-06-14 14:12:59 +080015032 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080015033 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015034 return 0;
15035 }
developer72fb0bb2023-01-11 09:46:29 +080015036
developera3511852023-06-14 14:12:59 +080015037 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015038
developera3511852023-06-14 14:12:59 +080015039 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080015040 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015041 nlfree(&nl);
15042 return 0;
15043 }
developer72fb0bb2023-01-11 09:46:29 +080015044
developera3511852023-06-14 14:12:59 +080015045 genlmsg_put(msg,
15046 NL_AUTO_PID,
15047 NL_AUTO_SEQ,
15048 nl.id,
15049 0,
15050 0,
15051 NL80211_CMD_GET_STATION,
15052 0);
developer72fb0bb2023-01-11 09:46:29 +080015053
developera3511852023-06-14 14:12:59 +080015054 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, *clientMacAddress);
15055 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15056 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, rxStatsInfo_callback, stats_array);
15057 nl_send_auto_complete(nl.socket, msg);
15058 nl_recvmsgs(nl.socket, nl.cb);
15059 nlmsg_free(msg);
15060 nlfree(&nl);
15061 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015062}
15063
developer72fb0bb2023-01-11 09:46:29 +080015064static int txStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080015065 struct nlattr *tb[NL80211_ATTR_MAX + 1];
15066 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080015067 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
15068 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080015069 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
15070 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080015071
developera3511852023-06-14 14:12:59 +080015072 nla_parse(tb,
15073 NL80211_ATTR_MAX,
15074 genlmsg_attrdata(gnlh, 0),
15075 genlmsg_attrlen(gnlh, 0),
15076 NULL);
developer72fb0bb2023-01-11 09:46:29 +080015077
developera3511852023-06-14 14:12:59 +080015078 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080015079 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080015080 return NL_SKIP;
15081 }
developer72fb0bb2023-01-11 09:46:29 +080015082
developera3511852023-06-14 14:12:59 +080015083 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080015084 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080015085 return NL_SKIP;
15086 }
developer72fb0bb2023-01-11 09:46:29 +080015087
developera3511852023-06-14 14:12:59 +080015088 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080015089
developera3511852023-06-14 14:12:59 +080015090 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080015091
developera3511852023-06-14 14:12:59 +080015092 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
15093 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
developer75bd10c2023-06-27 11:34:08 +080015094 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080015095 return NL_SKIP;
15096 }
15097 }
developer72fb0bb2023-01-11 09:46:29 +080015098
developera3511852023-06-14 14:12:59 +080015099 if(sinfo[NL80211_STA_INFO_TID_STATS])
15100 {
15101 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
15102 printf("failed to parse nested stats attributes!");
15103 return NL_SKIP;
15104 }
15105 }
15106 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
15107 if(nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]))
15108 {
15109 printf("Type is VHT\n");
15110 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
15111 ((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 +080015112
developera3511852023-06-14 14:12:59 +080015113 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
15114 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 1;
15115 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
15116 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15117 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
15118 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15119 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
15120 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15121 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]))
15122 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15123 }
15124 else
15125 {
15126 printf(" OFDM or CCK \n");
15127 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15128 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->nss = 0;
15129 }
15130 }
developer72fb0bb2023-01-11 09:46:29 +080015131
developera3511852023-06-14 14:12:59 +080015132 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
15133 if(rinfo[NL80211_RATE_INFO_MCS])
15134 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
15135 }
developer72fb0bb2023-01-11 09:46:29 +080015136
developera3511852023-06-14 14:12:59 +080015137 if(sinfo[NL80211_STA_INFO_TX_BYTES64])
15138 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]);
15139 else if (sinfo[NL80211_STA_INFO_TX_BYTES])
15140 ((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 +080015141
developera3511852023-06-14 14:12:59 +080015142 //Assigning 0 for mpdus and ppdus , as we do not have attributes in netlink
15143 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
15144 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
developer72fb0bb2023-01-11 09:46:29 +080015145
developera3511852023-06-14 14:12:59 +080015146 if(sinfo[NL80211_STA_INFO_TID_STATS]) {
15147 if(stats_info[NL80211_TID_STATS_TX_MSDU])
15148 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
15149 }
developer72fb0bb2023-01-11 09:46:29 +080015150
developera3511852023-06-14 14:12:59 +080015151 if(sinfo[NL80211_STA_INFO_TX_RETRIES])
15152 ((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 +080015153
developera3511852023-06-14 14:12:59 +080015154 if(sinfo[NL80211_STA_INFO_TX_FAILED] && sinfo[NL80211_STA_INFO_TX_PACKETS])
15155 ((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 +080015156
developera3511852023-06-14 14:12:59 +080015157 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015158}
developer72fb0bb2023-01-11 09:46:29 +080015159
15160INT wifi_getApAssociatedDeviceTxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_tx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
15161{
developera3511852023-06-14 14:12:59 +080015162 Netlink nl;
15163 char if_name[IF_NAME_SIZE];
15164 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015165 int res;
15166
developera3511852023-06-14 14:12:59 +080015167 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
15168 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015169
developera3511852023-06-14 14:12:59 +080015170 *output_array_size = sizeof(wifi_associated_dev_rate_info_tx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080015171
developera3511852023-06-14 14:12:59 +080015172 if (*output_array_size <= 0)
15173 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015174
developere40952c2023-06-15 18:46:43 +080015175 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
15176 if (os_snprintf_error(sizeof(if_name), res)) {
15177 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15178 return RETURN_ERR;
15179 }
developer72fb0bb2023-01-11 09:46:29 +080015180
developera3511852023-06-14 14:12:59 +080015181 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015182
developera3511852023-06-14 14:12:59 +080015183 if(nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080015184 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015185 return 0;
15186 }
developer72fb0bb2023-01-11 09:46:29 +080015187
developera3511852023-06-14 14:12:59 +080015188 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015189
developera3511852023-06-14 14:12:59 +080015190 if(!msg) {
developer75bd10c2023-06-27 11:34:08 +080015191 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015192 nlfree(&nl);
15193 return 0;
15194 }
developer72fb0bb2023-01-11 09:46:29 +080015195
developera3511852023-06-14 14:12:59 +080015196 genlmsg_put(msg,
15197 NL_AUTO_PID,
15198 NL_AUTO_SEQ,
15199 nl.id,
15200 0,
15201 0,
15202 NL80211_CMD_GET_STATION,
15203 0);
developer72fb0bb2023-01-11 09:46:29 +080015204
developera3511852023-06-14 14:12:59 +080015205 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
15206 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15207 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, txStatsInfo_callback, stats_array);
15208 nl_send_auto_complete(nl.socket, msg);
15209 nl_recvmsgs(nl.socket, nl.cb);
15210 nlmsg_free(msg);
15211 nlfree(&nl);
15212 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015213}
15214
15215INT wifi_getBSSTransitionActivation(UINT apIndex, BOOL *activate)
15216{
developera3511852023-06-14 14:12:59 +080015217 // TODO Implement me!
15218 char buf[MAX_BUF_SIZE] = {0};
15219 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015220 int res;
developer72fb0bb2023-01-11 09:46:29 +080015221
developere40952c2023-06-15 18:46:43 +080015222 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15223 if (os_snprintf_error(sizeof(config_file), res)) {
15224 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15225 return RETURN_ERR;
15226 }
developera3511852023-06-14 14:12:59 +080015227 wifi_hostapdRead(config_file, "bss_transition", buf, sizeof(buf));
15228 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015229
developera3511852023-06-14 14:12:59 +080015230 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015231}
15232
15233INT wifi_setNeighborReportActivation(UINT apIndex, BOOL activate)
15234{
developera3511852023-06-14 14:12:59 +080015235 char config_file[MAX_BUF_SIZE] = {0};
15236 struct params list;
developer75bd10c2023-06-27 11:34:08 +080015237 int res;
developer72fb0bb2023-01-11 09:46:29 +080015238
developera3511852023-06-14 14:12:59 +080015239 list.name = "rrm_neighbor_report";
15240 list.value = activate?"1":"0";
developer75bd10c2023-06-27 11:34:08 +080015241 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15242 if (os_snprintf_error(sizeof(config_file), res)) {
15243 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15244 return RETURN_ERR;
15245 }
developera3511852023-06-14 14:12:59 +080015246 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080015247
developera3511852023-06-14 14:12:59 +080015248 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015249}
15250
15251INT wifi_getNeighborReportActivation(UINT apIndex, BOOL *activate)
15252{
developera3511852023-06-14 14:12:59 +080015253 char buf[32] = {0};
15254 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080015255 int res;
developer72fb0bb2023-01-11 09:46:29 +080015256
developer75bd10c2023-06-27 11:34:08 +080015257 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15258 if (os_snprintf_error(sizeof(config_file), res)) {
15259 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15260 return RETURN_ERR;
15261 }
developera3511852023-06-14 14:12:59 +080015262 wifi_hostapdRead(config_file, "rrm_neighbor_report", buf, sizeof(buf));
15263 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015264
developera3511852023-06-14 14:12:59 +080015265 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015266}
15267#undef HAL_NETLINK_IMPL
15268#ifdef HAL_NETLINK_IMPL
15269static int chanSurveyInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080015270 struct nlattr *tb[NL80211_ATTR_MAX + 1];
15271 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
15272 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
15273 char dev[20];
15274 int freq =0 ;
15275 static int i=0;
developer72fb0bb2023-01-11 09:46:29 +080015276
developera3511852023-06-14 14:12:59 +080015277 wifi_channelStats_t_loc *out = (wifi_channelStats_t_loc*)arg;
developer72fb0bb2023-01-11 09:46:29 +080015278
developera3511852023-06-14 14:12:59 +080015279 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
15280 };
developer72fb0bb2023-01-11 09:46:29 +080015281
developera3511852023-06-14 14:12:59 +080015282 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080015283
developera3511852023-06-14 14:12:59 +080015284 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080015285
developera3511852023-06-14 14:12:59 +080015286 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
15287 fprintf(stderr, "survey data missing!\n");
15288 return NL_SKIP;
15289 }
developer72fb0bb2023-01-11 09:46:29 +080015290
developera3511852023-06-14 14:12:59 +080015291 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,tb[NL80211_ATTR_SURVEY_INFO],survey_policy))
15292 {
15293 fprintf(stderr, "failed to parse nested attributes!\n");
15294 return NL_SKIP;
15295 }
developer72fb0bb2023-01-11 09:46:29 +080015296
15297
developera3511852023-06-14 14:12:59 +080015298 if(out[0].array_size == 1 )
15299 {
15300 if(sinfo[NL80211_SURVEY_INFO_IN_USE])
15301 {
15302 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15303 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15304 out[0].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015305
developera3511852023-06-14 14:12:59 +080015306 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15307 out[0].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15308 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15309 out[0].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15310 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15311 out[0].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15312 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15313 out[0].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15314 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15315 out[0].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15316 if (sinfo[NL80211_SURVEY_INFO_TIME])
15317 out[0].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15318 return NL_STOP;
15319 }
15320 } else {
15321 if ( i <= out[0].array_size ) {
15322 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15323 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15324 out[i].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015325
developera3511852023-06-14 14:12:59 +080015326 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15327 out[i].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15328 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15329 out[i].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15330 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15331 out[i].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15332 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15333 out[i].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15334 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15335 out[i].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15336 if (sinfo[NL80211_SURVEY_INFO_TIME])
15337 out[i].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15338 }
15339 }
developer72fb0bb2023-01-11 09:46:29 +080015340
developera3511852023-06-14 14:12:59 +080015341 i++;
15342 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015343}
15344#endif
15345
15346static int ieee80211_channel_to_frequency(int channel, int *freqMHz)
15347{
developera3511852023-06-14 14:12:59 +080015348 char command[MAX_CMD_SIZE], output[MAX_BUF_SIZE];
15349 FILE *fp;
developere40952c2023-06-15 18:46:43 +080015350 int res;
developer72fb0bb2023-01-11 09:46:29 +080015351
developera3511852023-06-14 14:12:59 +080015352 if(access("/tmp/freq-channel-map.txt", F_OK)==-1)
15353 {
15354 printf("Creating Frequency-Channel Map\n");
developer33f13ba2023-07-12 16:19:06 +080015355 v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
developera3511852023-06-14 14:12:59 +080015356 }
developere40952c2023-06-15 18:46:43 +080015357 res = snprintf(command, sizeof(command), "cat /tmp/freq-channel-map.txt | grep '\\[%d\\]$' | cut -d' ' -f1", channel);
15358 if (os_snprintf_error(sizeof(command), res)) {
15359 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15360 return RETURN_ERR;
15361 }
15362
developera3511852023-06-14 14:12:59 +080015363 if((fp = popen(command, "r")))
15364 {
developerc14d83a2023-06-29 20:09:42 +080015365 if (fgets(output, sizeof(output), fp) == NULL) {
15366 wifi_debug(DEBUG_ERROR, "fgets fail\n");
15367 pclose(fp);
15368 return RETURN_ERR;
15369 }
developera3511852023-06-14 14:12:59 +080015370 *freqMHz = atoi(output);
15371 pclose(fp);
15372 }
developer72fb0bb2023-01-11 09:46:29 +080015373
developera3511852023-06-14 14:12:59 +080015374 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015375}
15376
developer2f79c922023-06-02 17:33:42 +080015377static int get_survey_dump_buf(INT radioIndex, int channel, char *buf, size_t bufsz)
developer72fb0bb2023-01-11 09:46:29 +080015378{
developera3511852023-06-14 14:12:59 +080015379 int freqMHz = -1;
15380 char cmd[MAX_CMD_SIZE] = {'\0'};
15381 char interface_name[16] = {0};
developer32f2a182023-06-27 19:50:41 +080015382 int res;
developer72fb0bb2023-01-11 09:46:29 +080015383
developera3511852023-06-14 14:12:59 +080015384 ieee80211_channel_to_frequency(channel, &freqMHz);
15385 if (freqMHz == -1) {
15386 wifi_dbg_printf("%s: failed to get channel frequency for channel: %d\n", __func__, channel);
15387 return -1;
15388 }
developer72fb0bb2023-01-11 09:46:29 +080015389
developer86035662023-06-28 19:21:12 +080015390 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
15391 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
15392 }
developer32f2a182023-06-27 19:50:41 +080015393 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep -A5 %d | tr -d '\\t'", interface_name, freqMHz);
15394 if (os_snprintf_error(sizeof(cmd), res)) {
15395 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15396 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080015397 }
developer72fb0bb2023-01-11 09:46:29 +080015398
developera3511852023-06-14 14:12:59 +080015399 if (_syscmd(cmd, buf, bufsz) == RETURN_ERR) {
15400 wifi_dbg_printf("%s: failed to execute '%s' for radioIndex=%d\n", __FUNCTION__, cmd, radioIndex);
15401 return -1;
15402 }
developer72fb0bb2023-01-11 09:46:29 +080015403
developera3511852023-06-14 14:12:59 +080015404 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015405}
15406
15407static int fetch_survey_from_buf(INT radioIndex, const char *buf, wifi_channelStats_t *stats)
15408{
developera3511852023-06-14 14:12:59 +080015409 const char *ptr = buf;
15410 char *key = NULL;
15411 char *val = NULL;
15412 char line[256] = { '\0' };
developer72fb0bb2023-01-11 09:46:29 +080015413
developera3511852023-06-14 14:12:59 +080015414 while ((ptr = get_line_from_str_buf(ptr, line))) {
15415 if (strstr(line, "Frequency")) continue;
developer72fb0bb2023-01-11 09:46:29 +080015416
developera3511852023-06-14 14:12:59 +080015417 key = strtok(line, ":");
developerc14d83a2023-06-29 20:09:42 +080015418 if (key == NULL)
15419 continue;
developera3511852023-06-14 14:12:59 +080015420 val = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080015421 if (val == NULL)
15422 continue;
developera3511852023-06-14 14:12:59 +080015423 wifi_dbg_printf("%s: key='%s' val='%s'\n", __func__, key, val);
developer72fb0bb2023-01-11 09:46:29 +080015424
developera3511852023-06-14 14:12:59 +080015425 if (!strcmp(key, "noise")) {
developer37646972023-06-29 10:58:43 +080015426 if (sscanf(val, "%d", &stats->ch_noise) == EOF)
15427 continue;
developera3511852023-06-14 14:12:59 +080015428 if (stats->ch_noise == 0) {
15429 // Workaround for missing noise information.
15430 // Assume -95 for 2.4G and -103 for 5G
15431 if (radioIndex == 0) stats->ch_noise = -95;
15432 if (radioIndex == 1) stats->ch_noise = -103;
15433 }
15434 }
15435 else if (!strcmp(key, "channel active time")) {
developer37646972023-06-29 10:58:43 +080015436 if (sscanf(val, "%llu", &stats->ch_utilization_total) == EOF)
15437 continue;
developera3511852023-06-14 14:12:59 +080015438 }
15439 else if (!strcmp(key, "channel busy time")) {
developer37646972023-06-29 10:58:43 +080015440 if (sscanf(val, "%llu", &stats->ch_utilization_busy) == EOF)
15441 continue;
developera3511852023-06-14 14:12:59 +080015442 }
15443 else if (!strcmp(key, "channel receive time")) {
developer37646972023-06-29 10:58:43 +080015444 if (sscanf(val, "%llu", &stats->ch_utilization_busy_rx) == EOF)
15445 continue;
developera3511852023-06-14 14:12:59 +080015446 }
15447 else if (!strcmp(key, "channel transmit time")) {
developer37646972023-06-29 10:58:43 +080015448 if (sscanf(val, "%llu", &stats->ch_utilization_busy_tx) == EOF)
15449 continue;
developera3511852023-06-14 14:12:59 +080015450 }
15451 };
developer72fb0bb2023-01-11 09:46:29 +080015452
developera3511852023-06-14 14:12:59 +080015453 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015454}
15455
15456INT wifi_getRadioChannelStats(INT radioIndex,wifi_channelStats_t *input_output_channelStats_array,INT array_size)
15457{
developera3511852023-06-14 14:12:59 +080015458 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080015459#ifdef HAL_NETLINK_IMPL
developera3511852023-06-14 14:12:59 +080015460 Netlink nl;
15461 wifi_channelStats_t_loc local[array_size];
15462 char if_name[32];
developer72fb0bb2023-01-11 09:46:29 +080015463
developera3511852023-06-14 14:12:59 +080015464 local[0].array_size = array_size;
developer72fb0bb2023-01-11 09:46:29 +080015465
developera3511852023-06-14 14:12:59 +080015466 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
15467 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015468
developera3511852023-06-14 14:12:59 +080015469 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015470
developera3511852023-06-14 14:12:59 +080015471 if (nl.id < 0) {
developerc14d83a2023-06-29 20:09:42 +080015472 wifi_debug(DEBUG_ERROR,, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015473 return -1;
15474 }
developer72fb0bb2023-01-11 09:46:29 +080015475
developera3511852023-06-14 14:12:59 +080015476 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015477
developera3511852023-06-14 14:12:59 +080015478 if (!msg) {
developerc14d83a2023-06-29 20:09:42 +080015479 wifi_debug(DEBUG_ERROR,, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015480 nlfree(&nl);
15481 return -2;
15482 }
developer72fb0bb2023-01-11 09:46:29 +080015483
developera3511852023-06-14 14:12:59 +080015484 genlmsg_put(msg,
15485 NL_AUTO_PID,
15486 NL_AUTO_SEQ,
15487 nl.id,
15488 0,
15489 NLM_F_DUMP,
15490 NL80211_CMD_GET_SURVEY,
15491 0);
developer72fb0bb2023-01-11 09:46:29 +080015492
developera3511852023-06-14 14:12:59 +080015493 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15494 nl_send_auto_complete(nl.socket, msg);
15495 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,chanSurveyInfo_callback,local);
15496 nl_recvmsgs(nl.socket, nl.cb);
15497 nlmsg_free(msg);
15498 nlfree(&nl);
15499 //Copying the Values
15500 for(int i=0;i<array_size;i++)
15501 {
15502 input_output_channelStats_array[i].ch_number = local[i].ch_number;
15503 input_output_channelStats_array[i].ch_noise = local[i].ch_noise;
15504 input_output_channelStats_array[i].ch_utilization_busy_rx = local[i].ch_utilization_busy_rx;
15505 input_output_channelStats_array[i].ch_utilization_busy_tx = local[i].ch_utilization_busy_tx;
15506 input_output_channelStats_array[i].ch_utilization_busy = local[i].ch_utilization_busy;
15507 input_output_channelStats_array[i].ch_utilization_busy_ext = local[i].ch_utilization_busy_ext;
15508 input_output_channelStats_array[i].ch_utilization_total = local[i].ch_utilization_total;
15509 //TODO: ch_radar_noise, ch_max_80211_rssi, ch_non_80211_noise, ch_utilization_busy_self
15510 }
developer72fb0bb2023-01-11 09:46:29 +080015511#else
developera3511852023-06-14 14:12:59 +080015512 ULONG channel = 0;
15513 int i;
15514 int number_of_channels = array_size;
15515 char buf[512];
developer72fb0bb2023-01-11 09:46:29 +080015516
developera3511852023-06-14 14:12:59 +080015517 if (number_of_channels == 0) {
15518 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK) {
15519 wifi_dbg_printf("%s: cannot get current channel for radioIndex=%d\n", __func__, radioIndex);
15520 return RETURN_ERR;
15521 }
15522 number_of_channels = 1;
15523 input_output_channelStats_array[0].ch_number = channel;
15524 }
developer72fb0bb2023-01-11 09:46:29 +080015525
developera3511852023-06-14 14:12:59 +080015526 for (i = 0; i < number_of_channels; i++) {
developer72fb0bb2023-01-11 09:46:29 +080015527
developera3511852023-06-14 14:12:59 +080015528 input_output_channelStats_array[i].ch_noise = 0;
15529 input_output_channelStats_array[i].ch_utilization_busy_rx = 0;
15530 input_output_channelStats_array[i].ch_utilization_busy_tx = 0;
15531 input_output_channelStats_array[i].ch_utilization_busy = 0;
15532 input_output_channelStats_array[i].ch_utilization_busy_ext = 0; // XXX: unavailable
15533 input_output_channelStats_array[i].ch_utilization_total = 0;
developer72fb0bb2023-01-11 09:46:29 +080015534
developera3511852023-06-14 14:12:59 +080015535 memset(buf, 0, sizeof(buf));
15536 if (get_survey_dump_buf(radioIndex, input_output_channelStats_array[i].ch_number, buf, sizeof(buf))) {
15537 return RETURN_ERR;
15538 }
15539 if (fetch_survey_from_buf(radioIndex, buf, &input_output_channelStats_array[i])) {
15540 wifi_dbg_printf("%s: cannot fetch survey from buf for radioIndex=%d\n", __func__, radioIndex);
15541 return RETURN_ERR;
15542 }
developer72fb0bb2023-01-11 09:46:29 +080015543
developera3511852023-06-14 14:12:59 +080015544 // XXX: fake missing 'self' counter which is not available in iw survey output
15545 // the 'self' counter (a.k.a 'bss') requires Linux Kernel update
15546 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 +080015547
developera3511852023-06-14 14:12:59 +080015548 input_output_channelStats_array[i].ch_utilization_busy_rx *= 1000;
15549 input_output_channelStats_array[i].ch_utilization_busy_tx *= 1000;
15550 input_output_channelStats_array[i].ch_utilization_busy_self *= 1000;
15551 input_output_channelStats_array[i].ch_utilization_busy *= 1000;
15552 input_output_channelStats_array[i].ch_utilization_total *= 1000;
developer72fb0bb2023-01-11 09:46:29 +080015553
developera3511852023-06-14 14:12:59 +080015554 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",
15555 __func__,
15556 input_output_channelStats_array[i].ch_number,
15557 input_output_channelStats_array[i].ch_noise,
15558 input_output_channelStats_array[i].ch_utilization_total,
15559 input_output_channelStats_array[i].ch_utilization_busy,
15560 input_output_channelStats_array[i].ch_utilization_busy_rx,
15561 input_output_channelStats_array[i].ch_utilization_busy_tx,
15562 input_output_channelStats_array[i].ch_utilization_busy_self,
15563 input_output_channelStats_array[i].ch_utilization_busy_ext);
15564 }
developer72fb0bb2023-01-11 09:46:29 +080015565#endif
developera3511852023-06-14 14:12:59 +080015566 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
15567 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015568}
15569#define HAL_NETLINK_IMPL
15570
15571/* Hostapd events */
15572
15573#ifndef container_of
15574#define offset_of(st, m) ((size_t)&(((st *)0)->m))
15575#define container_of(ptr, type, member) \
developera3511852023-06-14 14:12:59 +080015576 ((type *)((char *)ptr - offset_of(type, member)))
developer72fb0bb2023-01-11 09:46:29 +080015577#endif /* container_of */
15578
15579struct ctrl {
developera3511852023-06-14 14:12:59 +080015580 char sockpath[128];
15581 char sockdir[128];
15582 char bss[IFNAMSIZ];
15583 char reply[4096];
15584 int ssid_index;
15585 void (*cb)(struct ctrl *ctrl, int level, const char *buf, size_t len);
15586 void (*overrun)(struct ctrl *ctrl);
15587 struct wpa_ctrl *wpa;
15588 unsigned int ovfl;
15589 size_t reply_len;
15590 int initialized;
15591 ev_timer retry;
15592 ev_timer watchdog;
15593 ev_stat stat;
15594 ev_io io;
developer72fb0bb2023-01-11 09:46:29 +080015595};
15596static wifi_newApAssociatedDevice_callback clients_connect_cb;
15597static wifi_apDisassociatedDevice_callback clients_disconnect_cb;
15598static struct ctrl wpa_ctrl[MAX_APS];
15599static int initialized;
15600
15601static unsigned int ctrl_get_drops(struct ctrl *ctrl)
15602{
developera3511852023-06-14 14:12:59 +080015603 char cbuf[256] = {};
15604 struct msghdr msg = { .msg_control = cbuf, .msg_controllen = sizeof(cbuf) };
15605 struct cmsghdr *cmsg;
15606 unsigned int ovfl = ctrl->ovfl;
developer86035662023-06-28 19:21:12 +080015607 unsigned int drop = 0;
developer72fb0bb2023-01-11 09:46:29 +080015608
developer86035662023-06-28 19:21:12 +080015609 if (recvmsg(ctrl->io.fd, &msg, MSG_DONTWAIT) < 0)
15610 return drop;
developera3511852023-06-14 14:12:59 +080015611 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
15612 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_RXQ_OVFL)
15613 ovfl = *(unsigned int *)CMSG_DATA(cmsg);
developer72fb0bb2023-01-11 09:46:29 +080015614
developera3511852023-06-14 14:12:59 +080015615 drop = ovfl - ctrl->ovfl;
15616 ctrl->ovfl = ovfl;
developer72fb0bb2023-01-11 09:46:29 +080015617
developera3511852023-06-14 14:12:59 +080015618 return drop;
developer72fb0bb2023-01-11 09:46:29 +080015619}
15620
15621static void ctrl_close(struct ctrl *ctrl)
15622{
developera3511852023-06-14 14:12:59 +080015623 if (ctrl->io.cb)
15624 ev_io_stop(EV_DEFAULT_ &ctrl->io);
15625 if (ctrl->retry.cb)
15626 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15627 if (!ctrl->wpa)
15628 return;
developer72fb0bb2023-01-11 09:46:29 +080015629
developera3511852023-06-14 14:12:59 +080015630 wpa_ctrl_detach(ctrl->wpa);
15631 wpa_ctrl_close(ctrl->wpa);
15632 ctrl->wpa = NULL;
15633 printf("WPA_CTRL: closed index=%d\n", ctrl->ssid_index);
developer72fb0bb2023-01-11 09:46:29 +080015634}
15635
15636static void ctrl_process(struct ctrl *ctrl)
15637{
developera3511852023-06-14 14:12:59 +080015638 const char *str;
15639 int drops;
15640 int level;
developer72fb0bb2023-01-11 09:46:29 +080015641
developera3511852023-06-14 14:12:59 +080015642 /* Example events:
15643 *
15644 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19
15645 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19 keyid=sample_keyid
15646 * <3>AP-STA-DISCONNECTED 60:b4:f7:f0:0a:19
15647 * <3>CTRL-EVENT-CONNECTED - Connection to 00:1d:73:73:88:ea completed [id=0 id_str=]
15648 * <3>CTRL-EVENT-DISCONNECTED bssid=00:1d:73:73:88:ea reason=3 locally_generated=1
15649 */
15650 if (!(str = index(ctrl->reply, '>')))
15651 return;
15652 if (sscanf(ctrl->reply, "<%d>", &level) != 1)
15653 return;
developer72fb0bb2023-01-11 09:46:29 +080015654
developera3511852023-06-14 14:12:59 +080015655 str++;
developer72fb0bb2023-01-11 09:46:29 +080015656
developera3511852023-06-14 14:12:59 +080015657 if (strncmp("AP-STA-CONNECTED ", str, 17) == 0) {
15658 if (!(str = index(ctrl->reply, ' ')))
15659 return;
15660 wifi_associated_dev_t sta;
15661 memset(&sta, 0, sizeof(sta));
developer72fb0bb2023-01-11 09:46:29 +080015662
developere75ba632023-06-29 16:03:33 +080015663 if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
15664 &sta.cli_MACAddress[0], &sta.cli_MACAddress[1], &sta.cli_MACAddress[2],
15665 &sta.cli_MACAddress[3], &sta.cli_MACAddress[4], &sta.cli_MACAddress[5]) == EOF) {
15666 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
15667 return;
15668 }
developer72fb0bb2023-01-11 09:46:29 +080015669
developera3511852023-06-14 14:12:59 +080015670 sta.cli_Active=true;
developer72fb0bb2023-01-11 09:46:29 +080015671
developera3511852023-06-14 14:12:59 +080015672 (clients_connect_cb)(ctrl->ssid_index, &sta);
15673 goto handled;
15674 }
developer72fb0bb2023-01-11 09:46:29 +080015675
developera3511852023-06-14 14:12:59 +080015676 if (strncmp("AP-STA-DISCONNECTED ", str, 20) == 0) {
15677 if (!(str = index(ctrl->reply, ' ')))
15678 return;
developer72fb0bb2023-01-11 09:46:29 +080015679
developera3511852023-06-14 14:12:59 +080015680 (clients_disconnect_cb)(ctrl->ssid_index, (char*)str, 0);
15681 goto handled;
15682 }
developer72fb0bb2023-01-11 09:46:29 +080015683
developera3511852023-06-14 14:12:59 +080015684 if (strncmp("CTRL-EVENT-TERMINATING", str, 22) == 0) {
15685 printf("CTRL_WPA: handle TERMINATING event\n");
15686 goto retry;
15687 }
developer72fb0bb2023-01-11 09:46:29 +080015688
developera3511852023-06-14 14:12:59 +080015689 if (strncmp("AP-DISABLED", str, 11) == 0) {
15690 printf("CTRL_WPA: handle AP-DISABLED\n");
15691 goto retry;
15692 }
developer72fb0bb2023-01-11 09:46:29 +080015693
developera3511852023-06-14 14:12:59 +080015694 printf("Event not supported!!\n");
developer72fb0bb2023-01-11 09:46:29 +080015695
15696handled:
15697
developera3511852023-06-14 14:12:59 +080015698 if ((drops = ctrl_get_drops(ctrl))) {
15699 printf("WPA_CTRL: dropped %d messages index=%d\n", drops, ctrl->ssid_index);
15700 if (ctrl->overrun)
15701 ctrl->overrun(ctrl);
15702 }
developer72fb0bb2023-01-11 09:46:29 +080015703
developera3511852023-06-14 14:12:59 +080015704 return;
developer72fb0bb2023-01-11 09:46:29 +080015705
15706retry:
developera3511852023-06-14 14:12:59 +080015707 printf("WPA_CTRL: closing\n");
15708 ctrl_close(ctrl);
15709 printf("WPA_CTRL: retrying from ctrl prcoess\n");
15710 ev_timer_again(EV_DEFAULT_ &ctrl->retry);
developer72fb0bb2023-01-11 09:46:29 +080015711}
15712
15713static void ctrl_ev_cb(EV_P_ struct ev_io *io, int events)
15714{
developera3511852023-06-14 14:12:59 +080015715 struct ctrl *ctrl = container_of(io, struct ctrl, io);
15716 int err;
developer72fb0bb2023-01-11 09:46:29 +080015717
developera3511852023-06-14 14:12:59 +080015718 memset(ctrl->reply, 0, sizeof(ctrl->reply));
15719 ctrl->reply_len = sizeof(ctrl->reply) - 1;
15720 err = wpa_ctrl_recv(ctrl->wpa, ctrl->reply, &ctrl->reply_len);
15721 ctrl->reply[ctrl->reply_len] = 0;
15722 if (err < 0) {
15723 if (errno == EAGAIN || errno == EWOULDBLOCK)
15724 return;
15725 ctrl_close(ctrl);
15726 ev_timer_again(EV_A_ &ctrl->retry);
15727 return;
15728 }
developer72fb0bb2023-01-11 09:46:29 +080015729
developera3511852023-06-14 14:12:59 +080015730 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015731}
15732
15733static int ctrl_open(struct ctrl *ctrl)
15734{
developera3511852023-06-14 14:12:59 +080015735 int fd;
developer72fb0bb2023-01-11 09:46:29 +080015736
developera3511852023-06-14 14:12:59 +080015737 if (ctrl->wpa)
15738 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015739
developera3511852023-06-14 14:12:59 +080015740 ctrl->wpa = wpa_ctrl_open(ctrl->sockpath);
15741 if (!ctrl->wpa)
15742 goto err;
developer72fb0bb2023-01-11 09:46:29 +080015743
developera3511852023-06-14 14:12:59 +080015744 if (wpa_ctrl_attach(ctrl->wpa) < 0)
15745 goto err_close;
developer72fb0bb2023-01-11 09:46:29 +080015746
developera3511852023-06-14 14:12:59 +080015747 fd = wpa_ctrl_get_fd(ctrl->wpa);
15748 if (fd < 0)
15749 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015750
developera3511852023-06-14 14:12:59 +080015751 if (setsockopt(fd, SOL_SOCKET, SO_RXQ_OVFL, (int[]){1}, sizeof(int)) < 0)
15752 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015753
developera3511852023-06-14 14:12:59 +080015754 ev_io_init(&ctrl->io, ctrl_ev_cb, fd, EV_READ);
15755 ev_io_start(EV_DEFAULT_ &ctrl->io);
developer72fb0bb2023-01-11 09:46:29 +080015756
developera3511852023-06-14 14:12:59 +080015757 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015758
15759err_detach:
developera3511852023-06-14 14:12:59 +080015760 wpa_ctrl_detach(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015761err_close:
developera3511852023-06-14 14:12:59 +080015762 wpa_ctrl_close(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015763err:
developera3511852023-06-14 14:12:59 +080015764 ctrl->wpa = NULL;
15765 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015766}
15767
15768static void ctrl_stat_cb(EV_P_ ev_stat *stat, int events)
15769{
developera3511852023-06-14 14:12:59 +080015770 struct ctrl *ctrl = container_of(stat, struct ctrl, stat);
developer72fb0bb2023-01-11 09:46:29 +080015771
developera3511852023-06-14 14:12:59 +080015772 printf("WPA_CTRL: index=%d file state changed\n", ctrl->ssid_index);
15773 ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015774}
15775
15776static void ctrl_retry_cb(EV_P_ ev_timer *timer, int events)
15777{
developera3511852023-06-14 14:12:59 +080015778 struct ctrl *ctrl = container_of(timer, struct ctrl, retry);
developer72fb0bb2023-01-11 09:46:29 +080015779
developera3511852023-06-14 14:12:59 +080015780 printf("WPA_CTRL: index=%d retrying\n", ctrl->ssid_index);
15781 if (ctrl_open(ctrl) == 0) {
15782 printf("WPA_CTRL: retry successful\n");
15783 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15784 }
developer72fb0bb2023-01-11 09:46:29 +080015785}
15786
15787int ctrl_enable(struct ctrl *ctrl)
15788{
developera3511852023-06-14 14:12:59 +080015789 if (ctrl->wpa)
15790 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015791
developera3511852023-06-14 14:12:59 +080015792 if (!ctrl->stat.cb) {
15793 ev_stat_init(&ctrl->stat, ctrl_stat_cb, ctrl->sockpath, 0.);
15794 ev_stat_start(EV_DEFAULT_ &ctrl->stat);
15795 }
developer72fb0bb2023-01-11 09:46:29 +080015796
developera3511852023-06-14 14:12:59 +080015797 if (!ctrl->retry.cb) {
15798 ev_timer_init(&ctrl->retry, ctrl_retry_cb, 0., 5.);
15799 }
developer72fb0bb2023-01-11 09:46:29 +080015800
developera3511852023-06-14 14:12:59 +080015801 return ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015802}
15803
15804static void
15805ctrl_msg_cb(char *buf, size_t len)
15806{
developera3511852023-06-14 14:12:59 +080015807 struct ctrl *ctrl = container_of(buf, struct ctrl, reply);
developer72fb0bb2023-01-11 09:46:29 +080015808
developera3511852023-06-14 14:12:59 +080015809 printf("WPA_CTRL: unsolicited message: index=%d len=%zu msg=%s", ctrl->ssid_index, len, buf);
15810 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015811}
15812
15813static int ctrl_request(struct ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len)
15814{
developera3511852023-06-14 14:12:59 +080015815 int err;
developer72fb0bb2023-01-11 09:46:29 +080015816
developera3511852023-06-14 14:12:59 +080015817 if (!ctrl->wpa)
15818 return -1;
15819 if (*reply_len < 2)
15820 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015821
developera3511852023-06-14 14:12:59 +080015822 (*reply_len)--;
15823 ctrl->reply_len = sizeof(ctrl->reply);
15824 err = wpa_ctrl_request(ctrl->wpa, cmd, cmd_len, ctrl->reply, &ctrl->reply_len, ctrl_msg_cb);
15825 printf("WPA_CTRL: index=%d cmd='%s' err=%d\n", ctrl->ssid_index, cmd, err);
15826 if (err < 0)
15827 return err;
developer72fb0bb2023-01-11 09:46:29 +080015828
developera3511852023-06-14 14:12:59 +080015829 if (ctrl->reply_len > *reply_len)
15830 ctrl->reply_len = *reply_len;
developer72fb0bb2023-01-11 09:46:29 +080015831
developera3511852023-06-14 14:12:59 +080015832 *reply_len = ctrl->reply_len;
15833 memcpy(reply, ctrl->reply, *reply_len);
15834 reply[*reply_len - 1] = 0;
15835 printf("WPA_CTRL: index=%d reply='%s'\n", ctrl->ssid_index, reply);
15836 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015837}
15838
15839static void ctrl_watchdog_cb(EV_P_ ev_timer *timer, int events)
15840{
developera3511852023-06-14 14:12:59 +080015841 const char *pong = "PONG";
15842 const char *ping = "PING";
15843 char reply[1024];
15844 size_t len = sizeof(reply);
15845 int err;
15846 ULONG s, snum;
15847 INT ret;
15848 BOOL status;
developer72fb0bb2023-01-11 09:46:29 +080015849
developera3511852023-06-14 14:12:59 +080015850 printf("WPA_CTRL: watchdog cb\n");
developer72fb0bb2023-01-11 09:46:29 +080015851
developera3511852023-06-14 14:12:59 +080015852 ret = wifi_getSSIDNumberOfEntries(&snum);
15853 if (ret != RETURN_OK) {
15854 printf("%s: failed to get SSID count", __func__);
15855 return;
15856 }
developer72fb0bb2023-01-11 09:46:29 +080015857
developera3511852023-06-14 14:12:59 +080015858 if (snum > MAX_APS) {
15859 printf("more ssid than supported! %lu\n", snum);
15860 return;
15861 }
developer72fb0bb2023-01-11 09:46:29 +080015862
developera3511852023-06-14 14:12:59 +080015863 for (s = 0; s < snum; s++) {
15864 if (wifi_getApEnable(s, &status) != RETURN_OK) {
15865 printf("%s: failed to get AP Enable for index: %lu\n", __func__, s);
15866 continue;
15867 }
15868 if (status == false) continue;
developer72fb0bb2023-01-11 09:46:29 +080015869
developera3511852023-06-14 14:12:59 +080015870 memset(reply, 0, sizeof(reply));
15871 len = sizeof(reply);
15872 printf("WPA_CTRL: pinging index=%d\n", wpa_ctrl[s].ssid_index);
15873 err = ctrl_request(&wpa_ctrl[s], ping, strlen(ping), reply, &len);
15874 if (err == 0 && len > strlen(pong) && !strncmp(reply, pong, strlen(pong)))
15875 continue;
developer72fb0bb2023-01-11 09:46:29 +080015876
developera3511852023-06-14 14:12:59 +080015877 printf("WPA_CTRL: ping timeout index=%d\n", wpa_ctrl[s].ssid_index);
15878 ctrl_close(&wpa_ctrl[s]);
15879 printf("WPA_CTRL: ev_timer_again %lu\n", s);
15880 ev_timer_again(EV_DEFAULT_ &wpa_ctrl[s].retry);
15881 }
developer72fb0bb2023-01-11 09:46:29 +080015882}
15883
15884static int init_wpa()
15885{
developer9ce44382023-06-28 11:09:37 +080015886 int ret = 0;
developera3511852023-06-14 14:12:59 +080015887 ULONG s, snum;
developer72fb0bb2023-01-11 09:46:29 +080015888
developera3511852023-06-14 14:12:59 +080015889 ret = wifi_getSSIDNumberOfEntries(&snum);
15890 if (ret != RETURN_OK) {
15891 printf("%s: failed to get SSID count", __func__);
15892 return RETURN_ERR;
15893 }
developer72fb0bb2023-01-11 09:46:29 +080015894
developera3511852023-06-14 14:12:59 +080015895 if (snum > MAX_APS) {
15896 printf("more ssid than supported! %lu\n", snum);
15897 return RETURN_ERR;
15898 }
developer72fb0bb2023-01-11 09:46:29 +080015899
developera3511852023-06-14 14:12:59 +080015900 for (s = 0; s < snum; s++) {
15901 memset(&wpa_ctrl[s], 0, sizeof(struct ctrl));
developer32f2a182023-06-27 19:50:41 +080015902 ret = snprintf(wpa_ctrl[s].sockpath, sizeof(wpa_ctrl[s].sockpath), "%s%lu", SOCK_PREFIX, s);
15903 if (os_snprintf_error(sizeof(wpa_ctrl[s].sockpath), ret)) {
15904 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15905 return RETURN_ERR;
15906 }
developera3511852023-06-14 14:12:59 +080015907 wpa_ctrl[s].ssid_index = s;
15908 ctrl_enable(&wpa_ctrl[s]);
15909 }
developer72fb0bb2023-01-11 09:46:29 +080015910
developera3511852023-06-14 14:12:59 +080015911 ev_timer_init(&wpa_ctrl->watchdog, ctrl_watchdog_cb, 0., 30.);
15912 ev_timer_again(EV_DEFAULT_ &wpa_ctrl->watchdog);
developer72fb0bb2023-01-11 09:46:29 +080015913
developera3511852023-06-14 14:12:59 +080015914 initialized = 1;
15915 printf("WPA_CTRL: initialized\n");
developer72fb0bb2023-01-11 09:46:29 +080015916
developera3511852023-06-14 14:12:59 +080015917 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015918}
15919
15920void wifi_newApAssociatedDevice_callback_register(wifi_newApAssociatedDevice_callback callback_proc)
15921{
developera3511852023-06-14 14:12:59 +080015922 clients_connect_cb = callback_proc;
15923 if (!initialized)
15924 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015925}
15926
15927void wifi_apDisassociatedDevice_callback_register(wifi_apDisassociatedDevice_callback callback_proc)
15928{
developera3511852023-06-14 14:12:59 +080015929 clients_disconnect_cb = callback_proc;
15930 if (!initialized)
15931 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015932}
15933
15934INT wifi_setBTMRequest(UINT apIndex, CHAR *peerMac, wifi_BTMRequest_t *request)
15935{
developera3511852023-06-14 14:12:59 +080015936 // TODO Implement me!
15937 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015938}
15939
15940INT wifi_setRMBeaconRequest(UINT apIndex, CHAR *peer, wifi_BeaconRequest_t *in_request, UCHAR *out_DialogToken)
15941{
developera3511852023-06-14 14:12:59 +080015942 // TODO Implement me!
15943 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015944}
15945
15946INT wifi_getRadioChannels(INT radioIndex, wifi_channelMap_t *outputMap, INT outputMapSize)
15947{
developera3511852023-06-14 14:12:59 +080015948 int i;
15949 int phyId = -1;
15950 char cmd[256] = {0};
15951 char channel_numbers_buf[256] = {0};
15952 char dfs_state_buf[256] = {0};
15953 char line[256] = {0};
15954 const char *ptr;
15955 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +080015956 int res;
developer72fb0bb2023-01-11 09:46:29 +080015957
developera3511852023-06-14 14:12:59 +080015958 memset(outputMap, 0, outputMapSize*sizeof(wifi_channelMap_t)); // all unused entries should be zero
developer72fb0bb2023-01-11 09:46:29 +080015959
developera3511852023-06-14 14:12:59 +080015960 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
15961 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080015962
developere40952c2023-06-15 18:46:43 +080015963 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\\|");
15964 if (os_snprintf_error(sizeof(cmd), res)) {
15965 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15966 return RETURN_ERR;
15967 }
developer72fb0bb2023-01-11 09:46:29 +080015968
developera3511852023-06-14 14:12:59 +080015969 if (_syscmd(cmd, channel_numbers_buf, sizeof(channel_numbers_buf)) == RETURN_ERR) {
15970 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15971 return RETURN_ERR;
15972 }
developer72fb0bb2023-01-11 09:46:29 +080015973
developera3511852023-06-14 14:12:59 +080015974 ptr = channel_numbers_buf;
15975 i = 0;
15976 while ((ptr = get_line_from_str_buf(ptr, line))) {
15977 if (i >= outputMapSize) {
15978 wifi_dbg_printf("%s: DFS map size too small\n", __FUNCTION__);
15979 return RETURN_ERR;
15980 }
developerd14dff12023-06-28 22:47:44 +080015981 if (sscanf(line, "%d", &outputMap[i].ch_number) != 1) {
15982 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
15983 return RETURN_ERR;
15984 }
developerd1824452023-05-18 12:30:04 +080015985
developera3511852023-06-14 14:12:59 +080015986 memset(cmd, 0, sizeof(cmd));
15987 // Below command should fetch string for DFS state (usable, available or unavailable)
15988 // Example line: "DFS state: usable (for 78930 sec)"
15989 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) {
15990 wifi_dbg_printf("%s: failed to build dfs state command\n", __FUNCTION__);
15991 return RETURN_ERR;
15992 }
developer72fb0bb2023-01-11 09:46:29 +080015993
developera3511852023-06-14 14:12:59 +080015994 memset(dfs_state_buf, 0, sizeof(dfs_state_buf));
15995 if (_syscmd(cmd, dfs_state_buf, sizeof(dfs_state_buf)) == RETURN_ERR) {
15996 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15997 return RETURN_ERR;
15998 }
developer72fb0bb2023-01-11 09:46:29 +080015999
developera3511852023-06-14 14:12:59 +080016000 wifi_dbg_printf("DFS state = '%s'\n", dfs_state_buf);
developer59fda4f2023-05-16 15:47:38 +080016001
developera3511852023-06-14 14:12:59 +080016002 if (!strcmp(dfs_state_buf, "usable")) {
16003 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_FINISHED;
16004 } else if (!strcmp(dfs_state_buf, "available")) {
16005 outputMap[i].ch_state = CHAN_STATE_DFS_CAC_COMPLETED;
16006 } else if (!strcmp(dfs_state_buf, "unavailable")) {
16007 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_START;
16008 } else {
16009 outputMap[i].ch_state = CHAN_STATE_AVAILABLE;
16010 }
16011 i++;
16012 }
developer40ba1762023-05-13 11:03:49 +080016013
developera3511852023-06-14 14:12:59 +080016014 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +080016015
developera3511852023-06-14 14:12:59 +080016016 wifi_dbg_printf("%s: wrong radio index (%d)\n", __FUNCTION__, radioIndex);
16017 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016018}
16019
16020INT wifi_chan_eventRegister(wifi_chan_eventCB_t eventCb)
16021{
developera3511852023-06-14 14:12:59 +080016022 // TODO Implement me!
16023 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016024}
16025
16026INT wifi_getRadioBandUtilization (INT radioIndex, INT *output_percentage)
16027{
developer0155a502023-06-19 20:33:57 +080016028 int ret = -1;
16029 char inf_name[IF_NAME_SIZE] = {0};
16030 int if_idx = 0;
16031 struct unl unl_ins;
16032 struct nl_msg *msg = NULL;
16033 struct nlattr * msg_data = NULL;
16034 struct mtk_nl80211_param param;
16035 struct mtk_nl80211_cb_data cb_data;
16036 wdev_ap_metric ap_metric;
16037
16038 /*init mtk nl80211 vendor cmd*/
16039
16040 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
16041 return RETURN_ERR;
16042 if_idx = if_nametoindex(inf_name);
16043 if (!if_idx) {
16044 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
16045 return RETURN_ERR;
16046 }
16047
16048 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_STATISTIC;
16049 param.if_type = NL80211_ATTR_IFINDEX;
16050 param.if_idx = if_idx;
16051
16052 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
16053 if (ret) {
16054 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
16055 return RETURN_ERR;
16056 }
16057
16058 /*add mtk vendor cmd data*/
16059
16060 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS, sizeof(wdev_ap_metric), (char *)&ap_metric)) {
16061 wifi_debug(DEBUG_ERROR, "Nla put GET_AP_METRICS attribute error\n");
16062 nlmsg_free(msg);
16063 goto err;
16064 }
16065
16066 /*send mtk nl80211 vendor msg*/
16067 cb_data.out_buf = (char *)output_percentage;
16068 cb_data.out_len = sizeof(wdev_ap_metric);
16069 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_get_ap_metrics, &cb_data);
16070 if (ret) {
16071 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
16072 goto err;
16073 }
16074
16075 /*deinit mtk nl80211 vendor msg*/
16076 mtk_nl80211_deint(&unl_ins);
16077 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
16078 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16079
developera3511852023-06-14 14:12:59 +080016080 return RETURN_OK;
developer0155a502023-06-19 20:33:57 +080016081err:
16082 mtk_nl80211_deint(&unl_ins);
16083 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
16084 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016085}
16086
developer0155a502023-06-19 20:33:57 +080016087
developer72fb0bb2023-01-11 09:46:29 +080016088INT wifi_getApAssociatedClientDiagnosticResult(INT apIndex, char *mac_addr, wifi_associated_dev3_t *dev_conn)
16089{
developera3511852023-06-14 14:12:59 +080016090 // TODO Implement me!
16091 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016092}
16093
16094INT wifi_switchBand(char *interface_name,INT radioIndex,char *freqBand)
16095{
developera3511852023-06-14 14:12:59 +080016096 // TODO API refrence Implementaion is present on RPI hal
16097 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016098}
16099
16100INT wifi_getRadioPercentageTransmitPower(INT apIndex, ULONG *txpwr_pcntg)
16101{
developera3511852023-06-14 14:12:59 +080016102 ULONG pwr_percentage = 0;
developer72fb0bb2023-01-11 09:46:29 +080016103
developera3511852023-06-14 14:12:59 +080016104 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16105 if(txpwr_pcntg == NULL)
developerdaf24792023-06-06 11:40:04 +080016106 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016107
developera1255e42023-05-13 17:45:02 +080016108 wifi_getRadioTransmitPower(apIndex, &pwr_percentage);
16109 *txpwr_pcntg = pwr_percentage;
developera3511852023-06-14 14:12:59 +080016110 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16111 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016112}
16113
16114INT wifi_setZeroDFSState(UINT radioIndex, BOOL enable, BOOL precac)
16115{
developera3511852023-06-14 14:12:59 +080016116 // TODO precac feature.
16117 struct params params[2] = {0};
16118 char config_file[128] = {0};
16119 BOOL dfs_enable = false;
16120 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016121 int res;
developer72fb0bb2023-01-11 09:46:29 +080016122
developera3511852023-06-14 14:12:59 +080016123 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16124 band = wifi_index_to_band(radioIndex);
16125 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
developer72fb0bb2023-01-11 09:46:29 +080016126
developera3511852023-06-14 14:12:59 +080016127 if (dfs_enable == false) {
16128 WIFI_ENTRY_EXIT_DEBUG("Please enable DFS firstly!: %s\n", __func__);
16129 return RETURN_ERR;
16130 }
16131 params[0].name = "DfsZeroWaitDefault";
16132 params[0].value = enable?"1":"0";
16133 params[1].name = "DfsDedicatedZeroWait";
16134 params[1].value = enable?"1":"0";
developere40952c2023-06-15 18:46:43 +080016135 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16136 if (os_snprintf_error(sizeof(config_file), res)) {
16137 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16138 return RETURN_ERR;
16139 }
developera3511852023-06-14 14:12:59 +080016140 wifi_datfileWrite(config_file, params, 2);
16141 wifi_reloadAp(radioIndex);
16142 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016143
developera3511852023-06-14 14:12:59 +080016144 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16145 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016146}
16147
16148INT wifi_getZeroDFSState(UINT radioIndex, BOOL *enable, BOOL *precac)
16149{
developera3511852023-06-14 14:12:59 +080016150 char config_file[128] = {0};
16151 char buf1[32] = {0};
16152 char buf2[32] = {0};
16153 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016154 int res;
developer72fb0bb2023-01-11 09:46:29 +080016155
developera3511852023-06-14 14:12:59 +080016156 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16157 if (NULL == enable || NULL == precac)
16158 return RETURN_ERR;
16159 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080016160 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16161 if (os_snprintf_error(sizeof(config_file), res)) {
16162 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16163 return RETURN_ERR;
16164 }
developera3511852023-06-14 14:12:59 +080016165 wifi_datfileRead(config_file, "DfsZeroWaitDefault", buf1, sizeof(buf1));
16166 wifi_datfileRead(config_file, "DfsDedicatedZeroWait", buf2, sizeof(buf2));
16167 if ((strncmp(buf1, "1", 1) == 0) && (strncmp(buf2, "1", 1) == 0))
16168 *enable = true;
16169 else
16170 *enable = false;
developer72fb0bb2023-01-11 09:46:29 +080016171
developera3511852023-06-14 14:12:59 +080016172 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016173
developera3511852023-06-14 14:12:59 +080016174 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16175 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016176}
16177
16178INT wifi_isZeroDFSSupported(UINT radioIndex, BOOL *supported)
16179{
developera3511852023-06-14 14:12:59 +080016180 *supported = TRUE;
16181 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016182}
16183
16184INT wifi_setDownlinkMuType(INT radio_index, wifi_dl_mu_type_t mu_type)
16185{
developer863a4a62023-06-06 16:55:59 +080016186 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016187 wifi_band band = band_invalid;
16188 char ofdmabuf[32] = {'\0'};
16189 char mimobuf[32] = {'\0'};
16190 char new_ofdmabuf[32] = {'\0'};
16191 char new_mimobuf[32] = {'\0'};
16192 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016193 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16194 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16195 UCHAR bss_cnt = 0;
16196 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016197 int res;
developer72fb0bb2023-01-11 09:46:29 +080016198
developera3511852023-06-14 14:12:59 +080016199 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera1255e42023-05-13 17:45:02 +080016200 if ((mu_type < WIFI_DL_MU_TYPE_NONE)
16201 || (mu_type > WIFI_DL_MU_TYPE_OFDMA_MIMO)) {
16202 printf("%s:mu_type input Error", __func__);
16203 return RETURN_ERR;
16204 }
developera3511852023-06-14 14:12:59 +080016205 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016206 if (band == band_invalid) {
16207 printf("%s:Band Error\n", __func__);
16208 return RETURN_ERR;
16209 }
developere40952c2023-06-15 18:46:43 +080016210 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16211 if (os_snprintf_error(sizeof(dat_file), res)) {
16212 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16213 return RETURN_ERR;
16214 }
16215
developera1255e42023-05-13 17:45:02 +080016216 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016217 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16218 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016219 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16220 get_bssnum_byindex(radio_index, &bss_cnt);
16221 val_cnt = 2*bss_cnt - 1;
16222 WIFI_ENTRY_EXIT_DEBUG("bss number: %d\n", bss_cnt);
16223 if ((val_cnt >= sizeof(new_ofdmabuf))
16224 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016225 printf("%s:bss cnt Error", __func__);
developera1255e42023-05-13 17:45:02 +080016226 return RETURN_ERR;
16227 }
16228 /*translate set value*/
16229 if (mu_type == WIFI_DL_MU_TYPE_NONE) {
16230 strncpy(new_ofdmabuf, str_zero, val_cnt);
16231 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016232 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA) {
developera1255e42023-05-13 17:45:02 +080016233 strncpy(new_ofdmabuf, str_one, val_cnt);
16234 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016235 } else if (mu_type == WIFI_DL_MU_TYPE_MIMO) {
developera1255e42023-05-13 17:45:02 +080016236 strncpy(new_ofdmabuf, str_zero, val_cnt);
16237 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016238 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA_MIMO) {
developera1255e42023-05-13 17:45:02 +080016239 strncpy(new_ofdmabuf, str_one, val_cnt);
16240 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016241 }
developera1255e42023-05-13 17:45:02 +080016242 WIFI_ENTRY_EXIT_DEBUG("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16243 /*same value, not operation*/
16244 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16245 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16246 printf("%s:Reduntant value\n", __func__);
16247 return RETURN_OK;
16248 }
16249 /*modify dat file to new file*/
16250 params[0].name="MuOfdmaDlEnable";
16251 params[0].value=new_ofdmabuf;
16252 params[1].name="MuMimoDlEnable";
16253 params[1].value=new_mimobuf;
16254 wifi_datfileWrite(dat_file, params, 2);
16255 /*hostapd control restarp ap to take effect on these new value*/
16256 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016257 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16258 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016259}
16260
16261INT wifi_getDownlinkMuType(INT radio_index, wifi_dl_mu_type_t *mu_type)
16262{
developer5a333cf2023-06-06 18:18:50 +080016263 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016264 wifi_band band = band_invalid;
16265 char ofdmabuf[32] = {'\0'};
16266 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016267 char *token = NULL;
developerc14d83a2023-06-29 20:09:42 +080016268 long int ofdma = 0;
16269 long int mimo = 0;
developere40952c2023-06-15 18:46:43 +080016270 int res;
developer72fb0bb2023-01-11 09:46:29 +080016271
developera3511852023-06-14 14:12:59 +080016272 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016273
developera3511852023-06-14 14:12:59 +080016274 if (mu_type == NULL)
16275 return RETURN_ERR;
16276 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016277 if (band == band_invalid) {
16278 printf("%s:Band Error\n", __func__);
16279 return RETURN_ERR;
16280 }
developere40952c2023-06-15 18:46:43 +080016281 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16282 if (os_snprintf_error(sizeof(dat_file), res)) {
16283 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16284 return RETURN_ERR;
16285 }
developera1255e42023-05-13 17:45:02 +080016286 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016287 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16288 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016289
developera1255e42023-05-13 17:45:02 +080016290 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016291 if (token == NULL) {
16292 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16293 return RETURN_ERR;
16294 }
developerc14d83a2023-06-29 20:09:42 +080016295 if (hal_strtol(token, 10, &ofdma) < 0) {
16296 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080016297 }
developerc14d83a2023-06-29 20:09:42 +080016298
developera1255e42023-05-13 17:45:02 +080016299 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016300 if (token == NULL) {
16301 wifi_debug(DEBUG_ERROR, "Unexpected strtok fail\n");
16302 return RETURN_ERR;
16303 }
developerc14d83a2023-06-29 20:09:42 +080016304
16305 if (hal_strtol(token, 10, &mimo) < 0) {
16306 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +080016307 }
developerc14d83a2023-06-29 20:09:42 +080016308
developera1255e42023-05-13 17:45:02 +080016309 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d,mimo=%d\n", __func__, ofdma, mimo);
16310 if ((ofdma == 1) && (mimo == 1))
16311 *mu_type = WIFI_DL_MU_TYPE_OFDMA_MIMO;
16312 else if ((ofdma == 0) && (mimo == 1))
16313 *mu_type = WIFI_DL_MU_TYPE_MIMO;
16314 else if ((ofdma == 1) && (mimo == 0))
16315 *mu_type = WIFI_DL_MU_TYPE_OFDMA;
16316 else
16317 *mu_type = WIFI_DL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016318 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16319 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016320}
16321
16322INT wifi_setUplinkMuType(INT radio_index, wifi_ul_mu_type_t mu_type)
16323{
developera3511852023-06-14 14:12:59 +080016324 // hemu onoff=<val> (bitmap- UL MU-MIMO(bit3), DL MU-MIMO(bit2), UL OFDMA(bit1), DL OFDMA(bit0))
developer863a4a62023-06-06 16:55:59 +080016325 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016326 wifi_band band = band_invalid;
16327 char ofdmabuf[32] = {'\0'};
16328 char mimobuf[32] = {'\0'};
16329 char new_ofdmabuf[32] = {'\0'};
16330 char new_mimobuf[32] = {'\0'};
16331 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016332 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16333 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16334 UCHAR bss_cnt = 0;
16335 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016336 int res;
developer72fb0bb2023-01-11 09:46:29 +080016337
developera3511852023-06-14 14:12:59 +080016338 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16339 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016340 if (band == band_invalid) {
16341 printf("%s:Band Error\n", __func__);
16342 return RETURN_ERR;
16343 }
16344 if ((mu_type < WIFI_UL_MU_TYPE_NONE)
16345 || (mu_type > WIFI_UL_MU_TYPE_OFDMA)) {
16346 printf("%s:mu_type input Error\n", __func__);
16347 return RETURN_ERR;
16348 }
developere40952c2023-06-15 18:46:43 +080016349 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16350 if (os_snprintf_error(sizeof(dat_file), res)) {
16351 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16352 return RETURN_ERR;
16353 }
developera1255e42023-05-13 17:45:02 +080016354 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016355 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16356 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016357 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16358 get_bssnum_byindex(radio_index, &bss_cnt);
16359 val_cnt = 2*bss_cnt - 1;
16360 printf("bssNumber:%d,ValCnt:%d\n", bss_cnt, val_cnt);
16361 if ((val_cnt >= sizeof(new_ofdmabuf))
16362 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016363 printf("%s:bss cnt Error\n", __func__);
developera1255e42023-05-13 17:45:02 +080016364 return RETURN_ERR;
16365 }
16366 /*translate set value*/
16367 if (mu_type == WIFI_UL_MU_TYPE_NONE) {
16368 strncpy(new_ofdmabuf, str_zero, val_cnt);
16369 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016370 }
developera1255e42023-05-13 17:45:02 +080016371 if (mu_type == WIFI_UL_MU_TYPE_OFDMA) {
16372 strncpy(new_ofdmabuf, str_one, val_cnt);
16373 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016374 }
developera1255e42023-05-13 17:45:02 +080016375 printf("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16376 /*same value, not operation*/
16377 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16378 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16379 printf("%s:Reduntant value\n", __func__);
16380 return RETURN_OK;
16381 }
16382 /*modify dat file to new file*/
16383 params[0].name="MuOfdmaUlEnable";
16384 params[0].value=new_ofdmabuf;
16385 params[1].name="MuMimoUlEnable";
16386 params[1].value=new_mimobuf;
16387 wifi_datfileWrite(dat_file, params, 2);
16388 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016389 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16390 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016391}
16392
16393INT wifi_getUplinkMuType(INT radio_index, wifi_ul_mu_type_t *mu_type)
16394{
developer863a4a62023-06-06 16:55:59 +080016395 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016396 wifi_band band = band_invalid;
16397 char ofdmabuf[32] = {'\0'};
16398 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016399 char *token = NULL;
16400 UCHAR ofdma = 0;
16401 UCHAR mimo = 0;
developere40952c2023-06-15 18:46:43 +080016402 int res;
developerc14d83a2023-06-29 20:09:42 +080016403 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016404
developera3511852023-06-14 14:12:59 +080016405 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016406
developera3511852023-06-14 14:12:59 +080016407 if (mu_type == NULL)
16408 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016409 band = wifi_index_to_band(radio_index);
16410 if (band == band_invalid) {
16411 printf("%s:Band Error", __func__);
16412 return RETURN_ERR;
16413 }
developere40952c2023-06-15 18:46:43 +080016414 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16415 if (os_snprintf_error(sizeof(dat_file), res)) {
16416 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16417 return RETURN_ERR;
16418 }
developera1255e42023-05-13 17:45:02 +080016419 /*get current value in dat file*/
16420 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16421 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016422
developera1255e42023-05-13 17:45:02 +080016423 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016424 if (token == NULL) {
16425 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16426 return RETURN_ERR;
16427 }
developerc14d83a2023-06-29 20:09:42 +080016428
16429 if (hal_strtoul(token, 10, &tmp) < 0) {
16430 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016431 }
developerc14d83a2023-06-29 20:09:42 +080016432 ofdma = tmp;
developera1255e42023-05-13 17:45:02 +080016433 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016434 if (token == NULL) {
16435 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16436 return RETURN_ERR;
16437 }
developer5b23cd02023-07-19 20:26:03 +080016438
developerc14d83a2023-06-29 20:09:42 +080016439 if (hal_strtoul(token, 10, &tmp) < 0) {
16440 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +080016441 }
developerc14d83a2023-06-29 20:09:42 +080016442 mimo = tmp;
16443
developera1255e42023-05-13 17:45:02 +080016444 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d, mimo=%d\n", __func__, ofdma, mimo);
16445 if ((ofdma == 1) && (mimo == 0))
16446 *mu_type = WIFI_UL_MU_TYPE_OFDMA;
16447 else
16448 *mu_type = WIFI_UL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016449 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16450 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016451}
16452
16453
16454INT wifi_setGuardInterval(INT radio_index, wifi_guard_interval_t guard_interval)
16455{
developera3511852023-06-14 14:12:59 +080016456 char cmd[128] = {0};
16457 char buf[256] = {0};
16458 char config_file[64] = {0};
16459 char GI[8] = {0};
16460 UINT mode_map = 0;
16461 FILE *f = NULL;
16462 wifi_band band = band_invalid;
16463 char dat_file[64] = {'\0'};
16464 struct params params[3];
developere40952c2023-06-15 18:46:43 +080016465 int res;
developer72fb0bb2023-01-11 09:46:29 +080016466
developera3511852023-06-14 14:12:59 +080016467 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016468
developera3511852023-06-14 14:12:59 +080016469 if (wifi_getRadioMode(radio_index, buf, &mode_map) == RETURN_ERR) {
16470 wifi_dbg_printf("%s: wifi_getRadioMode return error\n", __func__);
16471 return RETURN_ERR;
16472 }
developera1255e42023-05-13 17:45:02 +080016473 /*sanity check*/
16474 if (((guard_interval == wifi_guard_interval_1600)
16475 || (guard_interval == wifi_guard_interval_3200))
developerdaf24792023-06-06 11:40:04 +080016476 && ((mode_map & (WIFI_MODE_BE | WIFI_MODE_AX)) == 0)) {
developera3511852023-06-14 14:12:59 +080016477 wifi_dbg_printf("%s: N/AC Mode not support 1600/3200ns GI\n", __func__);
16478 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016479 }
developere40952c2023-06-15 18:46:43 +080016480 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16481 if (os_snprintf_error(sizeof(config_file), res)) {
16482 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16483 return RETURN_ERR;
16484 }
developera3511852023-06-14 14:12:59 +080016485 band = wifi_index_to_band(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080016486
developera3511852023-06-14 14:12:59 +080016487 // Hostapd are not supported HE mode GI 1600, 3200 ns.
16488 if (guard_interval == wifi_guard_interval_800) { // remove all capab about short GI
developere40952c2023-06-15 18:46:43 +080016489 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SHORT-GI-(.){1,2}0\\]//g' %s", config_file);
16490 if (os_snprintf_error(sizeof(cmd), res)) {
16491 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16492 return RETURN_ERR;
16493 }
developera3511852023-06-14 14:12:59 +080016494 _syscmd(cmd, buf, sizeof(buf));
16495 } else if (guard_interval == wifi_guard_interval_400 || guard_interval == wifi_guard_interval_auto){
16496 wifi_hostapdRead(config_file, "ht_capab", buf, sizeof(buf));
16497 if (strstr(buf, "[SHORT-GI-") == NULL) {
developere40952c2023-06-15 18:46:43 +080016498 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[SHORT-GI-20][SHORT-GI-40]/' %s", config_file);
16499 if (os_snprintf_error(sizeof(cmd), res)) {
16500 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16501 return RETURN_ERR;
16502 }
developera3511852023-06-14 14:12:59 +080016503 _syscmd(cmd, buf, sizeof(buf));
16504 }
16505 if (band == band_5) {
16506 wifi_hostapdRead(config_file, "vht_capab", buf, sizeof(buf));
16507 if (strstr(buf, "[SHORT-GI-") == NULL) {
developere40952c2023-06-15 18:46:43 +080016508 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SHORT-GI-80][SHORT-GI-160]/' %s", config_file);
16509 if (os_snprintf_error(sizeof(cmd), res)) {
16510 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16511 return RETURN_ERR;
16512 }
developera3511852023-06-14 14:12:59 +080016513 _syscmd(cmd, buf, sizeof(buf));
16514 }
16515 }
16516 }
16517 /*wifi_reloadAp(radio_index);
developera1255e42023-05-13 17:45:02 +080016518 caller "wifi_setRadioOperatingParameters" have done this step.
16519 */
developere40952c2023-06-15 18:46:43 +080016520 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16521 if (os_snprintf_error(sizeof(dat_file), res)) {
16522 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16523 return RETURN_ERR;
16524 }
developera3511852023-06-14 14:12:59 +080016525 if (guard_interval == wifi_guard_interval_400) {
developera1255e42023-05-13 17:45:02 +080016526 params[0].name = "HT_GI";
16527 params[0].value = "1";
16528 params[1].name = "VHT_SGI";
16529 params[1].value = "1";
16530 wifi_datfileWrite(dat_file, params, 2);
developer32f2a182023-06-27 19:50:41 +080016531 memcpy(GI, "0.4", 3);
developera1255e42023-05-13 17:45:02 +080016532 } else {
16533 params[0].name = "HT_GI";
16534 params[0].value = "0";
16535 params[1].name = "VHT_SGI";
16536 params[1].value = "0";
16537 /*should enable FIXED_HE_GI_SUPPORT in driver*/
16538 params[2].name = "FgiFltf";
16539 if (guard_interval == wifi_guard_interval_800) {
16540 params[2].value = "800";
developer32f2a182023-06-27 19:50:41 +080016541 memcpy(GI, "0.8", 3);
developera1255e42023-05-13 17:45:02 +080016542 } else if (guard_interval == wifi_guard_interval_1600) {
16543 params[2].value = "1600";
developer32f2a182023-06-27 19:50:41 +080016544 memcpy(GI, "1.6", 3);
developera1255e42023-05-13 17:45:02 +080016545 } else if (guard_interval == wifi_guard_interval_3200) {
16546 params[2].value = "3200";
developer32f2a182023-06-27 19:50:41 +080016547 memcpy(GI, "3.2", 3);
developera1255e42023-05-13 17:45:02 +080016548 } else if (guard_interval == wifi_guard_interval_auto) {
16549 params[2].value = "0";
developer32f2a182023-06-27 19:50:41 +080016550 memcpy(GI, "auto", 4);
developera1255e42023-05-13 17:45:02 +080016551 }
16552 wifi_datfileWrite(dat_file, params, 3);
16553 }
developera3511852023-06-14 14:12:59 +080016554 // Record GI for get GI function
developere40952c2023-06-15 18:46:43 +080016555 res = snprintf(buf, sizeof(buf), "%s%d.txt", GUARD_INTERVAL_FILE, radio_index);
16556 if (os_snprintf_error(sizeof(buf), res)) {
16557 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16558 return RETURN_ERR;
16559 }
developera3511852023-06-14 14:12:59 +080016560 f = fopen(buf, "w");
16561 if (f == NULL)
16562 return RETURN_ERR;
16563 fprintf(f, "%s", GI);
developerc14d83a2023-06-29 20:09:42 +080016564 if (fclose(f) == EOF)
16565 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016566 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16567 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016568}
16569
16570INT wifi_getGuardInterval(INT radio_index, wifi_guard_interval_t *guard_interval)
16571{
developera3511852023-06-14 14:12:59 +080016572 char buf[32] = {0};
16573 char cmd[64] = {0};
developere40952c2023-06-15 18:46:43 +080016574 int res;
developer72fb0bb2023-01-11 09:46:29 +080016575
developera3511852023-06-14 14:12:59 +080016576 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016577
developera3511852023-06-14 14:12:59 +080016578 if (guard_interval == NULL)
16579 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016580
developere40952c2023-06-15 18:46:43 +080016581 res = snprintf(cmd, sizeof(cmd), "cat %s%d.txt 2> /dev/null", GUARD_INTERVAL_FILE, radio_index);
16582 if (os_snprintf_error(sizeof(cmd), res)) {
16583 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16584 return RETURN_ERR;
16585 }
developera3511852023-06-14 14:12:59 +080016586 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016587
developera3511852023-06-14 14:12:59 +080016588 if (strncmp(buf, "0.4", 3) == 0)
16589 *guard_interval = wifi_guard_interval_400;
16590 else if (strncmp(buf, "0.8", 3) == 0)
16591 *guard_interval = wifi_guard_interval_800;
16592 else if (strncmp(buf, "1.6", 3) == 0)
16593 *guard_interval = wifi_guard_interval_1600;
16594 else if (strncmp(buf, "3.2", 3) == 0)
16595 *guard_interval = wifi_guard_interval_3200;
16596 else
16597 *guard_interval = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +080016598
developera3511852023-06-14 14:12:59 +080016599 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16600 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016601}
16602
16603INT wifi_setBSSColor(INT radio_index, UCHAR color)
16604{
developera3511852023-06-14 14:12:59 +080016605 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16606 struct params params = {0};
16607 char config_file[128] = {0};
16608 char bss_color[4] ={0};
developere40952c2023-06-15 18:46:43 +080016609 int res;
developer72fb0bb2023-01-11 09:46:29 +080016610
developera1255e42023-05-13 17:45:02 +080016611 if (color < 1 || color > 63) {
16612 wifi_dbg_printf("color value is err:%d.\n", color);
16613 return RETURN_ERR;
16614 }
developera3511852023-06-14 14:12:59 +080016615 params.name = "he_bss_color";
developere40952c2023-06-15 18:46:43 +080016616 res = snprintf(bss_color, sizeof(bss_color), "%hhu", color);
16617 if (os_snprintf_error(sizeof(bss_color), res)) {
16618 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16619 return RETURN_ERR;
16620 }
developera3511852023-06-14 14:12:59 +080016621 params.value = bss_color;
developer75bd10c2023-06-27 11:34:08 +080016622
16623 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16624 if (os_snprintf_error(sizeof(config_file), res)) {
16625 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16626 return RETURN_ERR;
16627 }
developera3511852023-06-14 14:12:59 +080016628 wifi_hostapdWrite(config_file, &params, 1);
16629 //wifi_hostapdProcessUpdate(radio_index, &params, 1);
developera1255e42023-05-13 17:45:02 +080016630 wifi_reloadAp(radio_index);
developer69b61b02023-03-07 17:17:44 +080016631
developera3511852023-06-14 14:12:59 +080016632 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16633 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016634}
16635
16636INT wifi_getBSSColor(INT radio_index, UCHAR *color)
16637{
developera3511852023-06-14 14:12:59 +080016638 char config_file[128] = {0};
16639 char buf[64] = {0};
16640 char temp_output[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +080016641 int res;
developerc14d83a2023-06-29 20:09:42 +080016642 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016643
developera3511852023-06-14 14:12:59 +080016644 wifi_dbg_printf("\nFunc=%s\n", __func__);
16645 if (NULL == color)
16646 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016647
developer75bd10c2023-06-27 11:34:08 +080016648 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16649 if (os_snprintf_error(sizeof(config_file), res)) {
16650 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16651 return RETURN_ERR;
16652 }
developera3511852023-06-14 14:12:59 +080016653 wifi_hostapdRead(config_file, "he_bss_color", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016654
developera3511852023-06-14 14:12:59 +080016655 if(strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +080016656 res = snprintf(temp_output, sizeof(temp_output), "%s", buf);
developera3511852023-06-14 14:12:59 +080016657 } else {
developere40952c2023-06-15 18:46:43 +080016658 res = snprintf(temp_output, sizeof(temp_output), "1"); // default value
16659 }
16660 if (os_snprintf_error(sizeof(temp_output), res)) {
16661 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16662 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080016663 }
developer72fb0bb2023-01-11 09:46:29 +080016664
developerc14d83a2023-06-29 20:09:42 +080016665 if (hal_strtoul(temp_output, 10, &tmp) < 0) {
16666 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016667 }
developerc14d83a2023-06-29 20:09:42 +080016668 *color = tmp;
developera3511852023-06-14 14:12:59 +080016669 wifi_dbg_printf("\noutput_string=%s\n", color);
developer72fb0bb2023-01-11 09:46:29 +080016670
developera3511852023-06-14 14:12:59 +080016671 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016672}
16673
16674/* multi-psk support */
16675INT wifi_getMultiPskClientKey(INT apIndex, mac_address_t mac, wifi_key_multi_psk_t *key)
16676{
developera3511852023-06-14 14:12:59 +080016677 char cmd[256];
16678 char interface_name[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080016679 int res;
developer72fb0bb2023-01-11 09:46:29 +080016680
developera3511852023-06-14 14:12:59 +080016681 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16682 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016683
developer75bd10c2023-06-27 11:34:08 +080016684 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 +080016685 interface_name,
16686 mac[0],
16687 mac[1],
16688 mac[2],
16689 mac[3],
16690 mac[4],
16691 mac[5]
16692 );
developer75bd10c2023-06-27 11:34:08 +080016693 if (os_snprintf_error(sizeof(cmd), res)) {
16694 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16695 return RETURN_ERR;
16696 }
developera3511852023-06-14 14:12:59 +080016697 printf("DEBUG LOG wifi_getMultiPskClientKey(%s)\n",cmd);
16698 _syscmd(cmd, key->wifi_keyId, 64);
developer72fb0bb2023-01-11 09:46:29 +080016699
16700
developera3511852023-06-14 14:12:59 +080016701 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016702}
16703
16704INT wifi_pushMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16705{
developera3511852023-06-14 14:12:59 +080016706 char interface_name[16] = {0};
16707 FILE *fd = NULL;
16708 char fname[100];
16709 char cmd[128] = {0};
16710 char out[64] = {0};
16711 wifi_key_multi_psk_t * key = NULL;
developer75bd10c2023-06-27 11:34:08 +080016712 int res, ret;
developere40952c2023-06-15 18:46:43 +080016713
developera3511852023-06-14 14:12:59 +080016714 if(keysNumber < 0)
16715 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016716
developere40952c2023-06-15 18:46:43 +080016717 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16718 if (os_snprintf_error(sizeof(fname), res)) {
16719 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16720 return RETURN_ERR;
16721 }
developera3511852023-06-14 14:12:59 +080016722 fd = fopen(fname, "w");
16723 if (!fd) {
16724 return RETURN_ERR;
16725 }
16726 key= (wifi_key_multi_psk_t *) keys;
16727 for(int i=0; i<keysNumber; ++i, key++) {
developer75bd10c2023-06-27 11:34:08 +080016728 ret = fprintf(fd, "keyid=%s 00:00:00:00:00:00 %s\n", key->wifi_keyId, key->wifi_psk);
16729 if (ret < 0)
16730 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developera3511852023-06-14 14:12:59 +080016731 }
developerd14dff12023-06-28 22:47:44 +080016732 if (fclose(fd) != 0) {
16733 wifi_debug(DEBUG_ERROR, "fclose fail\n");
16734 return RETURN_ERR;
16735 }
developer72fb0bb2023-01-11 09:46:29 +080016736
developera3511852023-06-14 14:12:59 +080016737 //reload file
16738 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16739 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080016740 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s raw RELOAD_WPA_PSK", interface_name);
16741
16742 if (os_snprintf_error(sizeof(cmd), res)) {
16743 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16744 return RETURN_ERR;
16745 }
developera3511852023-06-14 14:12:59 +080016746 _syscmd(cmd, out, 64);
16747 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016748}
16749
16750INT wifi_getMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16751{
developera3511852023-06-14 14:12:59 +080016752 FILE *fd = NULL;
16753 char fname[100];
16754 char * line = NULL;
16755 char * pos = NULL;
16756 size_t len = 0;
16757 ssize_t read = 0;
16758 INT ret = RETURN_OK;
16759 wifi_key_multi_psk_t *keys_it = NULL;
developere40952c2023-06-15 18:46:43 +080016760 int res;
developer72fb0bb2023-01-11 09:46:29 +080016761
developera3511852023-06-14 14:12:59 +080016762 if (keysNumber < 1) {
16763 return RETURN_ERR;
16764 }
developer72fb0bb2023-01-11 09:46:29 +080016765
developere40952c2023-06-15 18:46:43 +080016766 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16767 if (os_snprintf_error(sizeof(fname), res)) {
16768 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16769 return RETURN_ERR;
16770 }
developera3511852023-06-14 14:12:59 +080016771 fd = fopen(fname, "r");
16772 if (!fd) {
16773 return RETURN_ERR;
16774 }
developer72fb0bb2023-01-11 09:46:29 +080016775
developera3511852023-06-14 14:12:59 +080016776 if (keys == NULL) {
16777 ret = RETURN_ERR;
16778 goto close;
16779 }
developer72fb0bb2023-01-11 09:46:29 +080016780
developera3511852023-06-14 14:12:59 +080016781 keys_it = keys;
16782 while ((read = getline(&line, &len, fd)) != -1) {
16783 //Strip trailing new line if present
16784 if (read > 0 && line[read-1] == '\n') {
16785 line[read-1] = '\0';
16786 }
developer72fb0bb2023-01-11 09:46:29 +080016787
developera3511852023-06-14 14:12:59 +080016788 if(strcmp(line,"keyid=")) {
developer37646972023-06-29 10:58:43 +080016789 if (sscanf(line, "keyid=%63s", keys_it->wifi_keyId) == EOF)
16790 continue;
developera3511852023-06-14 14:12:59 +080016791 if (!(pos = index(line, ' '))) {
16792 ret = RETURN_ERR;
16793 goto close;
16794 }
16795 pos++;
16796 //Here should be 00:00:00:00:00:00
16797 if (!(strcmp(pos,"00:00:00:00:00:00"))) {
16798 printf("Not supported MAC: %s\n", pos);
16799 }
16800 if (!(pos = index(pos, ' '))) {
16801 ret = RETURN_ERR;
16802 goto close;
16803 }
16804 pos++;
developer72fb0bb2023-01-11 09:46:29 +080016805
developera3511852023-06-14 14:12:59 +080016806 //The rest is PSK
developere40952c2023-06-15 18:46:43 +080016807 res = snprintf(&keys_it->wifi_psk[0], sizeof(keys_it->wifi_psk), "%s", pos);
16808 if (os_snprintf_error(sizeof(keys_it->wifi_psk), res)) {
16809 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +080016810 if (fclose(fd) == EOF)
16811 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developer5b23cd02023-07-19 20:26:03 +080016812
developere40952c2023-06-15 18:46:43 +080016813 return RETURN_ERR;
16814 }
16815
developera3511852023-06-14 14:12:59 +080016816 keys_it++;
developer72fb0bb2023-01-11 09:46:29 +080016817
developera3511852023-06-14 14:12:59 +080016818 if(--keysNumber <= 0)
developer72fb0bb2023-01-11 09:46:29 +080016819 break;
developera3511852023-06-14 14:12:59 +080016820 }
16821 }
developer72fb0bb2023-01-11 09:46:29 +080016822
16823close:
developera3511852023-06-14 14:12:59 +080016824 free(line);
developer37646972023-06-29 10:58:43 +080016825 if (fclose(fd) == EOF)
16826 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016827 return ret;
developer72fb0bb2023-01-11 09:46:29 +080016828}
16829/* end of multi-psk support */
16830
16831INT wifi_setNeighborReports(UINT apIndex,
developera3511852023-06-14 14:12:59 +080016832 UINT numNeighborReports,
16833 wifi_NeighborReport_t *neighborReports)
developer72fb0bb2023-01-11 09:46:29 +080016834{
developera3511852023-06-14 14:12:59 +080016835 char hex_bssid[13] = { 0 };
16836 char bssid[18] = { 0 };
16837 char nr[100] = { 0 };
16838 char ssid[32];
16839 char hex_ssid[32];
16840 char interface_name[16] = {0};
16841 INT ret;
developere40952c2023-06-15 18:46:43 +080016842 int res;
developerd14dff12023-06-28 22:47:44 +080016843 unsigned char hex_ssid_len;
developer72fb0bb2023-01-11 09:46:29 +080016844
developera3511852023-06-14 14:12:59 +080016845 /*rmeove all neighbors*/
16846 wifi_dbg_printf("\n[%s]: removing all neighbors from %s\n", __func__, interface_name);
16847 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16848 return RETURN_ERR;
developer33f13ba2023-07-12 16:19:06 +080016849
16850 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 +080016851 interface_name, interface_name);
16852
developer33f13ba2023-07-12 16:19:06 +080016853 if (res) {
16854 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +080016855 return RETURN_ERR;
16856 }
developer72fb0bb2023-01-11 09:46:29 +080016857
developera3511852023-06-14 14:12:59 +080016858 for(unsigned int i = 0; i < numNeighborReports; i++)
16859 {
16860 memset(ssid, 0, sizeof(ssid));
16861 ret = wifi_getSSIDName(apIndex, ssid);
16862 if (ret != RETURN_OK)
16863 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016864
developera3511852023-06-14 14:12:59 +080016865 memset(hex_ssid, 0, sizeof(hex_ssid));
developerd14dff12023-06-28 22:47:44 +080016866 hex_ssid_len = sizeof(hex_ssid);
16867 for(size_t j = 0,k = 0; ssid[j] != '\0' && k < sizeof(hex_ssid); j++,k+=2 ) {
16868 res = snprintf(hex_ssid + k, hex_ssid_len, "%02x", ssid[j]);
16869
16870 if (os_snprintf_error(hex_ssid_len, res)) {
16871 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16872 return RETURN_ERR;
16873 }
16874 hex_ssid_len = sizeof(hex_ssid) - strlen(hex_ssid);
16875 }
developer72fb0bb2023-01-11 09:46:29 +080016876
developere40952c2023-06-15 18:46:43 +080016877 res = snprintf(hex_bssid, sizeof(hex_bssid),
developera3511852023-06-14 14:12:59 +080016878 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
16879 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 +080016880 if (os_snprintf_error(sizeof(hex_bssid), res)) {
16881 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16882 return RETURN_ERR;
16883 }
16884 res = snprintf(bssid, sizeof(bssid),
developera3511852023-06-14 14:12:59 +080016885 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
16886 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 +080016887 if (os_snprintf_error(sizeof(bssid), res)) {
16888 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16889 return RETURN_ERR;
16890 }
developer72fb0bb2023-01-11 09:46:29 +080016891
developere40952c2023-06-15 18:46:43 +080016892 res = snprintf(nr, sizeof(nr),
developera3511852023-06-14 14:12:59 +080016893 "%s" // bssid
16894 "%02hhx%02hhx%02hhx%02hhx" // bssid_info
16895 "%02hhx" // operclass
16896 "%02hhx" // channel
16897 "%02hhx", // phy_mode
16898 hex_bssid,
16899 neighborReports[i].info & 0xff, (neighborReports[i].info >> 8) & 0xff,
16900 (neighborReports[i].info >> 16) & 0xff, (neighborReports[i].info >> 24) & 0xff,
16901 neighborReports[i].opClass,
16902 neighborReports[i].channel,
16903 neighborReports[i].phyTable);
developere40952c2023-06-15 18:46:43 +080016904 if (os_snprintf_error(sizeof(nr), res)) {
16905 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16906 return RETURN_ERR;
16907 }
developer72fb0bb2023-01-11 09:46:29 +080016908
developer33f13ba2023-07-12 16:19:06 +080016909 res = v_secure_system("hostapd_cli set_neighbor %s ssid=%s nr=%s -i %s",
16910 bssid, hex_ssid, nr, interface_name);
16911 if (res) {
16912 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080016913 return RETURN_ERR;
16914 }
developera3511852023-06-14 14:12:59 +080016915 }
developer72fb0bb2023-01-11 09:46:29 +080016916
developera3511852023-06-14 14:12:59 +080016917 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016918}
16919
16920INT wifi_getApInterworkingElement(INT apIndex, wifi_InterworkingElement_t *output_struct)
16921{
developera3511852023-06-14 14:12:59 +080016922 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016923}
16924
16925#ifdef _WIFI_HAL_TEST_
16926int main(int argc,char **argv)
16927{
developera3511852023-06-14 14:12:59 +080016928 int index;
16929 INT ret=0;
16930 char buf[1024]="";
developer72fb0bb2023-01-11 09:46:29 +080016931
developera3511852023-06-14 14:12:59 +080016932 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16933 if(argc<3)
16934 {
16935 if(argc==2)
16936 {
16937 if(!strcmp(argv[1], "init"))
16938 return wifi_init();
16939 if(!strcmp(argv[1], "reset"))
16940 return wifi_reset();
16941 if(!strcmp(argv[1], "wifi_getHalVersion"))
16942 {
16943 char buffer[64];
16944 if(wifi_getHalVersion(buffer)==RETURN_OK)
16945 printf("Version: %s\n", buffer);
16946 else
16947 printf("Error in wifi_getHalVersion\n");
16948 return RETURN_OK;
16949 }
16950 }
16951 printf("wifihal <API> <radioIndex> <arg1> <arg2> ...\n");
16952 exit(-1);
16953 }
developer72fb0bb2023-01-11 09:46:29 +080016954
developera3511852023-06-14 14:12:59 +080016955 index = atoi(argv[2]);
developer33f13ba2023-07-12 16:19:06 +080016956 if (strstr(argv[1], "test_system")!=NULL) {
16957 ret = v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
16958 printf("ret = %d\n", ret);
16959
16960 ret = v_secure_system("hostapd_cli show_neighbor -i %s | awk '{print $1 \" \" $2}' | xargs -n2 -r hostapd_cli remove_neighbor -i %s",
16961 "ra0", "ra0");
16962 printf("ret = %d\n", ret);
16963
16964 ret = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", "1,2,3");
16965 printf("ret = %d\n", ret);
16966
16967 ret = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", "rai0");
16968 printf("ret = %d\n", ret);
16969
16970 ret = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", "rax0");
16971 printf("ret = %d\n", ret);
16972
16973 ret = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", "ra0");
16974 printf("ret = %d\n", ret);
16975 ret = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", "ra0");
16976 printf("ret = %d\n", ret);
16977 ret = v_secure_system("ping -q -c 1 -W 1 \"%s\" > /dev/console 2>&1", "20.1.1.101");
16978 printf("ret = %d\n", ret);
16979
16980 return 0;
16981 }
16982
16983 if (strstr(argv[1], "test_popen")!=NULL) {
16984 FILE *fp = v_secure_popen("w", "cat");
16985 fprintf(fp, "popen write success\n");
16986 fclose(fp);
16987
16988 char buf[1024];
16989 memset(buf, 0, sizeof(buf));
16990 fp = v_secure_popen("r", "echo popen read success");
16991 if (fp == NULL) {
16992 printf("v_secure_popen failed\n");
16993 } else {
16994 if (fgets(buf, sizeof(buf), fp) == NULL) {
16995 printf("v_secure_popen read error\n");
16996 } else {
16997 printf("%s\n", buf);
16998 }
16999 v_secure_pclose(fp);
17000 }
17001
17002#if 0
17003 fp = v_secure_popen("r", "hostapd_cli -i %s reload", "ra0");
17004 if (fp == NULL) {
17005 printf("v_secure_popen failed\n");
17006 } else {
17007 if (fgets(buf, sizeof(buf), fp) == NULL) {
17008 printf("v_secure_popen read error\n");
17009 } else {
17010 printf("%s", buf);
17011 }
17012 v_secure_pclose(fp);
17013 }
17014#endif
17015
17016 long int band;
17017 memset(buf, 0, sizeof(buf));
17018 ret = _syscmd_secure(buf, sizeof(buf),
17019 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 0);
17020 if (hal_strtol(buf, 16, &band) < 0) {
17021 wifi_debug(DEBUG_ERROR, "strtol fail\n");
17022 }
17023 printf("ret = %d, band0=%ld\n", ret, band);
17024
17025 memset(buf, 0, sizeof(buf));
17026 ret = _syscmd_secure(buf, sizeof(buf),
17027 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 1);
17028 if (hal_strtol(buf, 16, &band) < 0) {
17029 wifi_debug(DEBUG_ERROR, "strtol fail\n");
17030 }
17031 printf("ret = %d, band1=%ld\n", ret, band);
17032
17033 memset(buf, 0, sizeof(buf));
17034 ret = _syscmd_secure(buf, sizeof(buf),
17035 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 2);
17036 if (hal_strtol(buf, 16, &band) < 0) {
17037 wifi_debug(DEBUG_ERROR, "strtol fail\n");
17038 }
17039 printf("ret = %d, band2=%ld\n", ret, band);
17040 }
17041
developera3511852023-06-14 14:12:59 +080017042 if(strstr(argv[1], "wifi_getApName")!=NULL)
17043 {
17044 wifi_getApName(index,buf);
17045 printf("Ap name is %s \n",buf);
17046 return 0;
17047 }
developerb14b3462023-07-01 18:02:42 +080017048 if(strstr(argv[1], "wifi_getRadioExtChannel")!=NULL)
17049 {
17050 wifi_getRadioExtChannel(index,buf);
17051 printf("extchannel is %s \n",buf);
17052 return 0;
17053 }
developerfead3972023-05-25 20:15:02 +080017054 if(strstr(argv[1], "wifi_setRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080017055 {
developerfead3972023-05-25 20:15:02 +080017056 UINT pureMode = atoi(argv[3]);
17057
developera3511852023-06-14 14:12:59 +080017058 wifi_setRadioMode(index, NULL, pureMode);
17059 printf("Ap SET Radio mode 0x%x\n", pureMode);
17060 return 0;
17061 }
17062 if (strstr(argv[1], "wifi_setRadioAutoBlockAckEnable") != NULL) {
17063 unsigned char enable = atoi(argv[3]);
17064 if (enable)
17065 wifi_setRadioAutoBlockAckEnable(index, TRUE);
17066 else
17067 wifi_setRadioAutoBlockAckEnable(index, FALSE);
17068 printf("%s handle wifi_setRadioAutoBlockAckEnable\n", __FUNCTION__);
17069 }
developera39cfb22023-06-20 16:28:17 +080017070 if(strstr(argv[1], "wifi_setRadioTrafficStatsRadioStatisticsEnable")!=NULL)
17071 {
17072 wifi_setRadioTrafficStatsRadioStatisticsEnable(index, TRUE);
17073 printf("Ap SET wifi_setRadioTrafficStatsRadioStatisticsEnable\n");
17074 return 0;
17075 }
17076 if(strstr(argv[1], "wifi_setRadioTrafficStatsMeasure")!=NULL)
17077 {
17078 wifi_radioTrafficStatsMeasure_t input = {30, 200};
17079
17080 wifi_setRadioTrafficStatsMeasure(index, &input);
17081 printf("Ap SET wifi_setRadioTrafficStatsMeasure\n");
17082 return 0;
17083 }
developerfead3972023-05-25 20:15:02 +080017084 if(strstr(argv[1], "wifi_setRadioTransmitPower")!=NULL)
developera3511852023-06-14 14:12:59 +080017085 {
developerfead3972023-05-25 20:15:02 +080017086 ULONG TransmitPower = atoi(argv[3]);
17087
developera3511852023-06-14 14:12:59 +080017088 wifi_setRadioTransmitPower(index, TransmitPower);
17089 printf("Ap SET TransmitPower %lu\n", TransmitPower);
17090 return 0;
17091 }
developerfead3972023-05-25 20:15:02 +080017092 if(strstr(argv[1], "wifi_setApManagementFramePowerControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017093 {
developerfead3972023-05-25 20:15:02 +080017094 INT TransmitPower = atoi(argv[3]);
17095
developera3511852023-06-14 14:12:59 +080017096 wifi_setApManagementFramePowerControl(index, TransmitPower);
17097 printf("Ap SET Mgnt TransmitPower %d\n", TransmitPower);
17098 return 0;
17099 }
developerfead3972023-05-25 20:15:02 +080017100 if(strstr(argv[1], "wifi_setRadioBW")!=NULL)
developera3511852023-06-14 14:12:59 +080017101 {
developerfead3972023-05-25 20:15:02 +080017102 CHAR *bandwith = argv[3];
17103
developera3511852023-06-14 14:12:59 +080017104 wifi_setRadioOperatingChannelBandwidth(index, bandwith);
17105 printf("Ap SET bw %s\n", bandwith);
17106 return 0;
17107 }
developerfead3972023-05-25 20:15:02 +080017108 if(strstr(argv[1], "wifi_factoryResetRadio")!=NULL)
developera3511852023-06-14 14:12:59 +080017109 {
17110 wifi_factoryResetRadio(index);
17111 printf("wifi_factoryResetRadio ok!\n");
17112 return 0;
17113 }
developerfead3972023-05-25 20:15:02 +080017114 if(strstr(argv[1], "wifi_getRadioResetCount")!=NULL)
developera3511852023-06-14 14:12:59 +080017115 {
17116 ULONG rst_cnt;
17117 wifi_getRadioResetCount(index, &rst_cnt);
17118 printf("wifi_factoryResetRadio rst_cnt = %lu\n", rst_cnt);
17119 return 0;
17120 }
developer2edaf012023-05-24 14:24:53 +080017121 if (strncmp(argv[1], "wifi_addApAclDevice", strlen(argv[1])) == 0) {
developer49b17232023-05-19 16:35:19 +080017122 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017123 {
17124 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17125 exit(-1);
17126 }
developer49b17232023-05-19 16:35:19 +080017127 wifi_addApAclDevice(index, argv[3]);
17128 return 0;
17129 }
developer2edaf012023-05-24 14:24:53 +080017130 if (strncmp(argv[1], "wifi_getApAclDevices", strlen(argv[1])) == 0) {
17131 wifi_getApAclDevices(index, buf, 1024);
17132 wifi_debug(DEBUG_NOTICE, "Ap acl Devices: %s\n", buf);
developer121a8e72023-05-22 09:19:39 +080017133 return 0;
17134 }
developer2edaf012023-05-24 14:24:53 +080017135 if (strncmp(argv[1], "wifi_delApAclDevice", strlen(argv[1])) == 0) {
17136 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017137 {
17138 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17139 exit(-1);
17140 }
developer2edaf012023-05-24 14:24:53 +080017141 wifi_delApAclDevice(index, argv[3]);
17142 return 0;
17143 }
17144 if (strncmp(argv[1], "wifi_delApAclDevices", strlen(argv[1])) == 0) {
17145 wifi_delApAclDevices(index);
17146 return 0;
17147 }
17148 if (strncmp(argv[1], "wifi_getApAclDeviceNum", strlen(argv[1])) == 0) {
developer863a4a62023-06-06 16:55:59 +080017149 UINT acl_num = 0;
developer2edaf012023-05-24 14:24:53 +080017150 wifi_getApAclDeviceNum(index, &acl_num);
17151 wifi_debug(DEBUG_NOTICE, "Ap acl numbers: %d\n", acl_num);
17152 return 0;
17153 }
17154 if (strncmp(argv[1], "wifi_getApDenyAclDevices", strlen(argv[1])) == 0) {
17155 wifi_getApDenyAclDevices(index, buf, 1024);
17156 wifi_debug(DEBUG_NOTICE, "Ap Deny Acl Devices: %s\n", buf);
17157 return 0;
17158 }
17159 if (strncmp(argv[1], "wifi_setApMacAddressControlMode", strlen(argv[1])) == 0) {
17160 int filter_mode = 0;
17161 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017162 {
17163 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17164 exit(-1);
17165 }
developer2edaf012023-05-24 14:24:53 +080017166 filter_mode = atoi(argv[3]);
17167 wifi_setApMacAddressControlMode(index,filter_mode);
17168 return 0;
17169 }
developer5cd4c862023-05-26 09:34:42 +080017170 if (strncmp(argv[1], "wifi_getRadioDeclineBARequestEnable", strlen(argv[1])) == 0) {
17171 BOOL output_bool = 0;
17172 wifi_getRadioDeclineBARequestEnable(index, &output_bool);
17173 wifi_debug(DEBUG_NOTICE, "Ap get radio ba decline enable: %d\n", output_bool);
17174 return 0;
17175 }
17176 if (strncmp(argv[1], "wifi_getRadioAutoBlockAckEnable", strlen(argv[1])) == 0) {
17177 BOOL output_bool = 0;
17178 wifi_getRadioAutoBlockAckEnable(index, &output_bool);
17179 wifi_debug(DEBUG_NOTICE, "Ap get radio auto_ba enable: %d\n", output_bool);
17180 return 0;
17181 }
17182
17183 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17184 int filter_mode = 0;
17185 wifi_getApMacAddressControlMode(index, &filter_mode);
17186 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17187 return 0;
17188 }
17189 if (strncmp(argv[1], "wifi_setRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17190 int enable = 0;
17191 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017192 {
17193 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17194 exit(-1);
17195 }
developer5cd4c862023-05-26 09:34:42 +080017196 enable = (BOOL)atoi(argv[3]);
17197 wifi_setRadioIGMPSnoopingEnable(index, enable);
17198 wifi_debug(DEBUG_NOTICE, "Ap set IGMP Snooping Enable: %d\n", enable);
17199 return 0;
17200 }
developer326d4232023-06-15 16:45:30 +080017201 if (strncmp(argv[1], "wifi_setRadioDCSEnable(", strlen(argv[1])) == 0) {
17202 int enable = 0;
17203 if(argc <= 3 )
17204 {
17205 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17206 exit(-1);
17207 }
17208 enable = (BOOL)atoi(argv[3]);
17209 wifi_setRadioDCSEnable(index, enable);
17210 wifi_debug(DEBUG_NOTICE, "Ap set DCS Enable: %d\n", enable);
17211 return 0;
17212 }
17213 if (strncmp(argv[1], "wifi_getRadioAutoChannelRefreshPeriod(", strlen(argv[1])) == 0) {
17214 ULONG period = 0;
developer5cd4c862023-05-26 09:34:42 +080017215
developer326d4232023-06-15 16:45:30 +080017216 wifi_getRadioAutoChannelRefreshPeriod(index, &period);
17217 wifi_debug(DEBUG_NOTICE, "Get RefreshPeriod: %ld\n", period);
17218 return 0;
17219 }
17220 if (strncmp(argv[1], "wifi_setRadioDfsRefreshPeriod(", strlen(argv[1])) == 0) {
17221 ULONG period = 0;
17222
17223 period = (ULONG)atoi(argv[3]);
17224 wifi_setRadioDfsRefreshPeriod(index, period);
17225 wifi_debug(DEBUG_NOTICE, "Set RefreshPeriod: %ld\n", period);
17226 return 0;
17227 }
17228 if (strncmp(argv[1], "wifi_setRadioDCSChannelPool(", strlen(argv[1])) == 0) {
17229 char pool[256] = {'\0'};
17230
developerc14d83a2023-06-29 20:09:42 +080017231 strncpy(pool, argv[3], strlen(argv[3]));
developer326d4232023-06-15 16:45:30 +080017232 wifi_setRadioDCSChannelPool(index, pool);
17233 wifi_debug(DEBUG_NOTICE, "Set DCSChannelPool: %s\n", pool);
17234 return 0;
17235 }
17236 if (strncmp(argv[1], "wifi_getRadioDCSChannelPool(", strlen(argv[1])) == 0) {
17237 char pool[256] = {'\0'};
17238
17239 wifi_getRadioDCSChannelPool(index, pool);
17240 wifi_debug(DEBUG_NOTICE, "Get DCSChannelPool: %s\n", pool);
17241 return 0;
17242 }
developer5cd4c862023-05-26 09:34:42 +080017243 if (strncmp(argv[1], "wifi_getRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17244 BOOL out_status = 0;
17245 wifi_getRadioIGMPSnoopingEnable(index, &out_status);
17246 wifi_debug(DEBUG_NOTICE, "Ap get IGMP Snooping Enable: %d\n", out_status);
17247 return 0;
17248 }
developer121a8e72023-05-22 09:19:39 +080017249
developer95c045d2023-05-24 19:26:28 +080017250 if (strncmp(argv[1], "wifi_setApWmmEnable", strlen(argv[1])) == 0) {
17251 int enable = 0;
17252 if(argc <= 3)
17253 {
17254 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17255 exit(-1);
17256 }
17257 enable = atoi(argv[3]);
17258 wifi_setApWmmEnable(index,enable);
17259 return 0;
17260 }
developerc1aa6532023-06-09 09:37:01 +080017261 if (strncmp(argv[1], "wifi_pushSsidAdvertisementEnable", strlen(argv[1])) == 0) {
17262 int enable = 0;
17263 if(argc <= 3)
17264 {
17265 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17266 exit(-1);
17267 }
17268 enable = atoi(argv[3]);
17269 wifi_pushSsidAdvertisementEnable(index,enable);
17270 return 0;
17271 }
developer56fbedb2023-05-30 16:47:05 +080017272 if (strncmp(argv[1], "wifi_down", strlen(argv[1])) == 0) {
17273 wifi_down();
17274 return 0;
17275 }
developer95c045d2023-05-24 19:26:28 +080017276
developer56fbedb2023-05-30 16:47:05 +080017277 if (strncmp(argv[1], "wifi_getRadioStatus", strlen(argv[1])) == 0) {
17278 BOOL enable = 0;
17279
17280 wifi_getRadioStatus(index, &enable);
17281 wifi_debug(DEBUG_NOTICE, "wifi_getRadioStatus enable: %d\n", (int)enable);
17282 return 0;
17283 }
developer333c1eb2023-05-31 14:59:39 +080017284
developer95c045d2023-05-24 19:26:28 +080017285 if (strncmp(argv[1], "wifi_getApWMMCapability", strlen(argv[1])) == 0) {
17286 BOOL enable = 0;
17287
17288 wifi_getApWMMCapability(index, &enable);
17289 wifi_debug(DEBUG_NOTICE, "wifi_getApWMMCapability enable: %d\n", (int)enable);
17290 return 0;
17291 }
17292
17293 if (strncmp(argv[1], "wifi_getApWmmEnable", strlen(argv[1])) == 0) {
17294 BOOL enable = 0;
17295
17296 wifi_getApWmmEnable(index, &enable);
17297 wifi_debug(DEBUG_NOTICE, "wifi_getApWmmEnable enable: %d\n", (int)enable);
17298 return 0;
17299 }
17300
developer2edaf012023-05-24 14:24:53 +080017301 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17302 int filter_mode = 0;
17303 wifi_getApMacAddressControlMode(index, &filter_mode);
17304 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17305 return 0;
17306 }
developer0f10c772023-05-16 21:43:39 +080017307 if(strstr(argv[1], "wifi_getRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080017308 {
developer863a4a62023-06-06 16:55:59 +080017309 UINT mode = 0;
developer0f10c772023-05-16 21:43:39 +080017310
developera3511852023-06-14 14:12:59 +080017311 wifi_getRadioMode(index, buf, &mode);
17312 printf("Ap Radio mode is %s , mode = 0x%x\n", buf, mode);
17313 return 0;
17314 }
17315 if(strstr(argv[1], "wifi_getRadioAutoChannelEnable")!=NULL)
17316 {
17317 BOOL b = FALSE;
17318 BOOL *output_bool = &b;
17319 wifi_getRadioAutoChannelEnable(index,output_bool);
17320 printf("Channel enabled = %d \n",b);
17321 return 0;
17322 }
17323 if(strstr(argv[1], "wifi_getApWpaEncryptionMode")!=NULL)
17324 {
17325 wifi_getApWpaEncryptionMode(index,buf);
17326 printf("encryption enabled = %s\n",buf);
17327 return 0;
17328 }
17329 if(strstr(argv[1], "wifi_getApSsidAdvertisementEnable")!=NULL)
17330 {
17331 BOOL b = FALSE;
17332 BOOL *output_bool = &b;
17333 wifi_getApSsidAdvertisementEnable(index,output_bool);
17334 printf("advertisment enabled = %d\n",b);
17335 return 0;
17336 }
17337 if(strstr(argv[1],"wifi_getApAssociatedDeviceTidStatsResult")!=NULL)
17338 {
17339 if(argc <= 3 )
17340 {
17341 printf("Insufficient arguments \n");
17342 exit(-1);
17343 }
developer72fb0bb2023-01-11 09:46:29 +080017344
developera3511852023-06-14 14:12:59 +080017345 char sta[20] = {'\0'};
17346 ULLONG handle= 0;
developerc14d83a2023-06-29 20:09:42 +080017347 strncpy(sta,argv[3], strlen(argv[3]));
developera3511852023-06-14 14:12:59 +080017348 mac_address_t st;
developer72fb0bb2023-01-11 09:46:29 +080017349 mac_addr_aton(st,sta);
17350
developera3511852023-06-14 14:12:59 +080017351 wifi_associated_dev_tid_stats_t tid_stats;
17352 wifi_getApAssociatedDeviceTidStatsResult(index,&st,&tid_stats,&handle);
17353 for(int tid_index=0; tid_index<PS_MAX_TID; tid_index++) //print tid stats
17354 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);
17355 }
developer72fb0bb2023-01-11 09:46:29 +080017356
developera3511852023-06-14 14:12:59 +080017357 if(strstr(argv[1], "getApEnable")!=NULL) {
17358 BOOL enable;
17359 ret=wifi_getApEnable(index, &enable);
17360 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17361 }
17362 else if(strstr(argv[1], "setApEnable")!=NULL) {
17363 BOOL enable = atoi(argv[3]);
17364 ret=wifi_setApEnable(index, enable);
17365 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17366 }
17367 else if(strstr(argv[1], "getApStatus")!=NULL) {
17368 char status[64];
17369 ret=wifi_getApStatus(index, status);
17370 printf("%s %d: %s, returns %d\n", argv[1], index, status, ret);
17371 }
17372 else if(strstr(argv[1], "wifi_getSSIDNameStatus")!=NULL)
17373 {
17374 wifi_getSSIDNameStatus(index,buf);
17375 printf("%s %d: active ssid : %s\n",argv[1], index,buf);
17376 return 0;
17377 } else if(strstr(argv[1], "wifi_resetApVlanCfg")!=NULL) {
17378 wifi_resetApVlanCfg(index);
17379 printf("%s %d: wifi_resetApVlanCfg : %s\n",argv[1], index,buf);
17380 return 0;
17381 }
17382 else if(strstr(argv[1], "getSSIDTrafficStats2")!=NULL) {
17383 wifi_ssidTrafficStats2_t stats={0};
17384 ret=wifi_getSSIDTrafficStats2(index, &stats); //Tr181
17385 printf("%s %d: returns %d\n", argv[1], index, ret);
17386 printf(" ssid_BytesSent =%lu\n", stats.ssid_BytesSent);
17387 printf(" ssid_BytesReceived =%lu\n", stats.ssid_BytesReceived);
17388 printf(" ssid_PacketsSent =%lu\n", stats.ssid_PacketsSent);
17389 printf(" ssid_PacketsReceived =%lu\n", stats.ssid_PacketsReceived);
17390 printf(" ssid_RetransCount =%lu\n", stats.ssid_RetransCount);
17391 printf(" ssid_FailedRetransCount =%lu\n", stats.ssid_FailedRetransCount);
17392 printf(" ssid_RetryCount =%lu\n", stats.ssid_RetryCount);
17393 printf(" ssid_MultipleRetryCount =%lu\n", stats.ssid_MultipleRetryCount);
17394 printf(" ssid_ACKFailureCount =%lu\n", stats.ssid_ACKFailureCount);
17395 printf(" ssid_AggregatedPacketCount =%lu\n", stats.ssid_AggregatedPacketCount);
17396 printf(" ssid_ErrorsSent =%lu\n", stats.ssid_ErrorsSent);
17397 printf(" ssid_ErrorsReceived =%lu\n", stats.ssid_ErrorsReceived);
17398 printf(" ssid_UnicastPacketsSent =%lu\n", stats.ssid_UnicastPacketsSent);
17399 printf(" ssid_UnicastPacketsReceived =%lu\n", stats.ssid_UnicastPacketsReceived);
17400 printf(" ssid_DiscardedPacketsSent =%lu\n", stats.ssid_DiscardedPacketsSent);
17401 printf(" ssid_DiscardedPacketsReceived =%lu\n", stats.ssid_DiscardedPacketsReceived);
17402 printf(" ssid_MulticastPacketsSent =%lu\n", stats.ssid_MulticastPacketsSent);
17403 printf(" ssid_MulticastPacketsReceived =%lu\n", stats.ssid_MulticastPacketsReceived);
17404 printf(" ssid_BroadcastPacketsSent =%lu\n", stats.ssid_BroadcastPacketsSent);
17405 printf(" ssid_BroadcastPacketsRecevied =%lu\n", stats.ssid_BroadcastPacketsRecevied);
17406 printf(" ssid_UnknownPacketsReceived =%lu\n", stats.ssid_UnknownPacketsReceived);
17407 }
17408 else if(strstr(argv[1], "getNeighboringWiFiDiagnosticResult2")!=NULL) {
17409 wifi_neighbor_ap2_t *neighbor_ap_array=NULL, *pt=NULL;
17410 UINT array_size=0;
17411 UINT i=0;
17412 ret=wifi_getNeighboringWiFiDiagnosticResult2(index, &neighbor_ap_array, &array_size);
17413 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17414 for(i=0, pt=neighbor_ap_array; i<array_size; i++, pt++) {
17415 printf(" neighbor %d:\n", i);
17416 printf(" ap_SSID =%s\n", pt->ap_SSID);
17417 printf(" ap_BSSID =%s\n", pt->ap_BSSID);
17418 printf(" ap_Mode =%s\n", pt->ap_Mode);
17419 printf(" ap_Channel =%d\n", pt->ap_Channel);
17420 printf(" ap_SignalStrength =%d\n", pt->ap_SignalStrength);
17421 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17422 printf(" ap_EncryptionMode =%s\n", pt->ap_EncryptionMode);
17423 printf(" ap_SupportedStandards =%s\n", pt->ap_SupportedStandards);
17424 printf(" ap_OperatingStandards =%s\n", pt->ap_OperatingStandards);
17425 printf(" ap_OperatingChannelBandwidth =%s\n", pt->ap_OperatingChannelBandwidth);
17426 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17427 printf(" ap_BeaconPeriod =%d\n", pt->ap_BeaconPeriod);
17428 printf(" ap_Noise =%d\n", pt->ap_Noise);
17429 printf(" ap_BasicDataTransferRates =%s\n", pt->ap_BasicDataTransferRates);
17430 printf(" ap_SupportedDataTransferRates =%s\n", pt->ap_SupportedDataTransferRates);
17431 printf(" ap_DTIMPeriod =%d\n", pt->ap_DTIMPeriod);
17432 printf(" ap_ChannelUtilization =%d\n", pt->ap_ChannelUtilization);
17433 }
17434 if(neighbor_ap_array)
17435 free(neighbor_ap_array); //make sure to free the list
17436 }
17437 else if(strstr(argv[1], "getApAssociatedDeviceDiagnosticResult")!=NULL) {
17438 wifi_associated_dev_t *associated_dev_array=NULL, *pt=NULL;
17439 UINT array_size=0;
17440 UINT i=0;
17441 ret=wifi_getApAssociatedDeviceDiagnosticResult(index, &associated_dev_array, &array_size);
17442 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17443 for(i=0, pt=associated_dev_array; i<array_size; i++, pt++) {
17444 printf(" associated_dev %d:\n", i);
17445 printf(" cli_OperatingStandard =%s\n", pt->cli_OperatingStandard);
17446 printf(" cli_OperatingChannelBandwidth =%s\n", pt->cli_OperatingChannelBandwidth);
17447 printf(" cli_SNR =%d\n", pt->cli_SNR);
17448 printf(" cli_InterferenceSources =%s\n", pt->cli_InterferenceSources);
17449 printf(" cli_DataFramesSentAck =%lu\n", pt->cli_DataFramesSentAck);
17450 printf(" cli_DataFramesSentNoAck =%lu\n", pt->cli_DataFramesSentNoAck);
17451 printf(" cli_BytesSent =%lu\n", pt->cli_BytesSent);
17452 printf(" cli_BytesReceived =%lu\n", pt->cli_BytesReceived);
17453 printf(" cli_RSSI =%d\n", pt->cli_RSSI);
17454 printf(" cli_MinRSSI =%d\n", pt->cli_MinRSSI);
17455 printf(" cli_MaxRSSI =%d\n", pt->cli_MaxRSSI);
17456 printf(" cli_Disassociations =%d\n", pt->cli_Disassociations);
17457 printf(" cli_AuthenticationFailures =%d\n", pt->cli_AuthenticationFailures);
17458 }
17459 if(associated_dev_array)
17460 free(associated_dev_array); //make sure to free the list
17461 }
developer72fb0bb2023-01-11 09:46:29 +080017462
developera3511852023-06-14 14:12:59 +080017463 if(strstr(argv[1],"wifi_getRadioChannelStats")!=NULL)
17464 {
developer72fb0bb2023-01-11 09:46:29 +080017465#define MAX_ARRAY_SIZE 64
developera3511852023-06-14 14:12:59 +080017466 int i, array_size;
17467 char *p, *ch_str;
17468 wifi_channelStats_t input_output_channelStats_array[MAX_ARRAY_SIZE];
developer72fb0bb2023-01-11 09:46:29 +080017469
developera3511852023-06-14 14:12:59 +080017470 if(argc != 5)
17471 {
17472 printf("Insufficient arguments, Usage: wifihal wifi_getRadioChannelStats <AP-Index> <Array-Size> <Comma-seperated-channel-numbers>\n");
17473 exit(-1);
17474 }
17475 memset(input_output_channelStats_array, 0, sizeof(input_output_channelStats_array));
developer72fb0bb2023-01-11 09:46:29 +080017476
developera3511852023-06-14 14:12:59 +080017477 for (i=0, array_size=atoi(argv[3]), ch_str=argv[4]; i<array_size; i++, ch_str=p)
17478 {
17479 strtok_r(ch_str, ",", &p);
17480 input_output_channelStats_array[i].ch_number = atoi(ch_str);
17481 }
17482 wifi_getRadioChannelStats(atoi(argv[2]), input_output_channelStats_array, array_size);
17483 if(!array_size)
17484 array_size=1;//Need to print current channel statistics
17485 for(i=0; i<array_size; i++)
17486 printf("chan num = %d \t, noise =%d\t ch_utilization_busy_rx = %lld \t,\
17487 ch_utilization_busy_tx = %lld \t,ch_utilization_busy = %lld \t,\
17488 ch_utilization_busy_ext = %lld \t, ch_utilization_total = %lld \t \n",\
17489 input_output_channelStats_array[i].ch_number,\
17490 input_output_channelStats_array[i].ch_noise,\
17491 input_output_channelStats_array[i].ch_utilization_busy_rx,\
17492 input_output_channelStats_array[i].ch_utilization_busy_tx,\
17493 input_output_channelStats_array[i].ch_utilization_busy,\
17494 input_output_channelStats_array[i].ch_utilization_busy_ext,\
17495 input_output_channelStats_array[i].ch_utilization_total);
17496 }
developer72fb0bb2023-01-11 09:46:29 +080017497
developera3511852023-06-14 14:12:59 +080017498 if(strstr(argv[1],"wifi_getAssociatedDeviceDetail")!=NULL)
17499 {
17500 if(argc <= 3 )
17501 {
17502 printf("Insufficient arguments \n");
17503 exit(-1);
17504 }
17505 char mac_addr[20] = {'\0'};
17506 wifi_device_t output_struct;
17507 int dev_index = atoi(argv[3]);
developer72fb0bb2023-01-11 09:46:29 +080017508
developera3511852023-06-14 14:12:59 +080017509 wifi_getAssociatedDeviceDetail(index,dev_index,&output_struct);
17510 mac_addr_ntoa(mac_addr,output_struct.wifi_devMacAddress);
17511 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);
17512 }
developer72fb0bb2023-01-11 09:46:29 +080017513
developera3511852023-06-14 14:12:59 +080017514 if(strstr(argv[1],"wifi_setNeighborReports")!=NULL)
17515 {
17516 if (argc <= 3)
17517 {
17518 printf("Insufficient arguments\n");
17519 exit(-1);
17520 }
17521 char args[256];
17522 wifi_NeighborReport_t *neighborReports;
developer72fb0bb2023-01-11 09:46:29 +080017523
developera3511852023-06-14 14:12:59 +080017524 neighborReports = calloc(argc - 2, sizeof(neighborReports));
17525 if (!neighborReports)
17526 {
17527 printf("Failed to allocate memory");
17528 exit(-1);
17529 }
developer72fb0bb2023-01-11 09:46:29 +080017530
developera3511852023-06-14 14:12:59 +080017531 for (int i = 3; i < argc; ++i)
17532 {
17533 char *val;
17534 int j = 0;
developerc14d83a2023-06-29 20:09:42 +080017535 unsigned long tmp;
developera3511852023-06-14 14:12:59 +080017536 memset(args, 0, sizeof(args));
17537 strncpy(args, argv[i], sizeof(args));
17538 val = strtok(args, ";");
17539 while (val != NULL)
17540 {
17541 if (j == 0)
17542 {
17543 mac_addr_aton(neighborReports[i - 3].bssid, val);
17544 } else if (j == 1)
17545 {
developerc14d83a2023-06-29 20:09:42 +080017546 if (hal_strtoul(val, 16, &tmp) < 0) {
17547 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017548 }
17549 neighborReports[i - 3].info = tmp;
developera3511852023-06-14 14:12:59 +080017550 } else if (j == 2)
17551 {
developerc14d83a2023-06-29 20:09:42 +080017552 if (hal_strtoul(val, 16, &tmp) < 0) {
17553 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017554 }
17555 neighborReports[i - 3].opClass = tmp;
developer5b23cd02023-07-19 20:26:03 +080017556
developera3511852023-06-14 14:12:59 +080017557 } else if (j == 3)
17558 {
developerc14d83a2023-06-29 20:09:42 +080017559 if (hal_strtoul(val, 16, &tmp) < 0) {
17560 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017561 }
17562 neighborReports[i - 3].channel = tmp;
developera3511852023-06-14 14:12:59 +080017563 } else if (j == 4)
17564 {
developerc14d83a2023-06-29 20:09:42 +080017565 if (hal_strtoul(val, 16, &tmp) < 0) {
17566 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017567 }
17568 neighborReports[i - 3].phyTable = tmp;
developera3511852023-06-14 14:12:59 +080017569 } else {
17570 printf("Insufficient arguments]n\n");
17571 exit(-1);
17572 }
17573 val = strtok(NULL, ";");
17574 j++;
17575 }
17576 }
developer72fb0bb2023-01-11 09:46:29 +080017577
developera3511852023-06-14 14:12:59 +080017578 INT ret = wifi_setNeighborReports(index, argc - 3, neighborReports);
17579 if (ret != RETURN_OK)
17580 {
17581 printf("wifi_setNeighborReports ret = %d", ret);
17582 exit(-1);
17583 }
17584 }
17585 if(strstr(argv[1],"wifi_getRadioIfName")!=NULL)
17586 {
17587 if((ret=wifi_getRadioIfName(index, buf))==RETURN_OK)
17588 printf("%s.\n", buf);
17589 else
17590 printf("Error returned\n");
17591 }
17592 if(strstr(argv[1],"wifi_getApSecurityModesSupported")!=NULL)
17593 {
17594 if((ret=wifi_getApSecurityModesSupported(index, buf))==RETURN_OK)
17595 printf("%s.\n", buf);
17596 else
17597 printf("Error returned\n");
17598 }
17599 if(strstr(argv[1],"wifi_getRadioOperatingChannelBandwidth")!=NULL)
17600 {
17601 if (argc <= 2)
17602 {
17603 printf("Insufficient arguments\n");
17604 exit(-1);
17605 }
17606 char buf[64]= {'\0'};
17607 wifi_getRadioOperatingChannelBandwidth(index,buf);
17608 printf("Current bandwidth is %s \n",buf);
17609 return 0;
17610 }
17611 if(strstr(argv[1],"pushRadioChannel2")!=NULL)
17612 {
17613 if (argc <= 5)
17614 {
17615 printf("Insufficient arguments\n");
17616 exit(-1);
17617 }
17618 UINT channel = atoi(argv[3]);
17619 UINT width = atoi(argv[4]);
17620 UINT beacon = atoi(argv[5]);
17621 INT ret = wifi_pushRadioChannel2(index,channel,width,beacon);
17622 printf("Result = %d", ret);
17623 }
developercc5cbfb2023-06-13 18:29:52 +080017624 if(strstr(argv[1],"wifi_getApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017625 {
developercc5cbfb2023-06-13 18:29:52 +080017626 char br_name[64], ip[64], subset[64] = {0};
17627 wifi_getApBridgeInfo(0, br_name, ip, subset);
17628 printf("wifi_getApBridgeInfo br_name = %s, ip = %s, subset = %s\n", br_name, ip, subset);
developera3511852023-06-14 14:12:59 +080017629 }
developercc5cbfb2023-06-13 18:29:52 +080017630 if(strstr(argv[1],"wifi_enableGreylistAccessControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017631 {
developercc5cbfb2023-06-13 18:29:52 +080017632 int enable = atoi(argv[3]);
17633 wifi_enableGreylistAccessControl(enable == 0 ? FALSE : TRUE);
17634 printf("wifi_enableGreylistAccessControl enable=%d\n", enable);
developera3511852023-06-14 14:12:59 +080017635 }
developercc5cbfb2023-06-13 18:29:52 +080017636 if(strstr(argv[1],"wifi_setApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017637 {
developercc5cbfb2023-06-13 18:29:52 +080017638 wifi_setApBridgeInfo(0, argv[3], argv[4], argv[5]);
17639 printf("wifi_setApBridgeInfo br_name = %s, ip = %s, subset = %s\n", argv[3], argv[4], argv[5]);
developera3511852023-06-14 14:12:59 +080017640 }
developer72fb0bb2023-01-11 09:46:29 +080017641
developer6e578302023-06-21 10:11:16 +080017642 if(strstr(argv[1], "wifi_getATMCapable")!=NULL)
17643 {
17644 BOOL b = FALSE;
17645 BOOL *output_bool = &b;
17646 wifi_getATMCapable(output_bool);
17647 printf("ATM capable = %d \n",b);
17648 return 0;
17649 }
17650 if (strncmp(argv[1], "wifi_setATMEnable", strlen(argv[1])) == 0) {
17651 int enable = 0;
17652 if(argc <= 3)
17653 {
17654 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17655 exit(-1);
17656 }
17657 enable = atoi(argv[3]);
17658 wifi_setATMEnable(enable);
17659 return 0;
17660 }
17661 if (strncmp(argv[1], "wifi_getATMEnable", strlen(argv[1])) == 0) {
17662 BOOL b = FALSE;
17663 BOOL *output_bool = &b;
17664 wifi_getATMEnable(output_bool);
17665 printf("ATM enable = %d \n", b);
17666 return 0;
17667 }
17668 if (strncmp(argv[1], "wifi_setApATMAirTimePercent", strlen(argv[1])) == 0) {
17669 unsigned int percent = 0;
17670 if(argc <= 3)
17671 {
17672 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17673 exit(-1);
17674 }
17675 percent = atoi(argv[3]);
17676 wifi_setApATMAirTimePercent(index, percent);
17677 return 0;
17678 }
17679 if (strncmp(argv[1], "wifi_getApATMAirTimePercent", strlen(argv[1])) == 0) {
17680 unsigned int percent = 0;
17681 unsigned int *output = &percent;
17682
17683 wifi_getApATMAirTimePercent(index, output);
17684 printf("ATM percent = %d \n", percent);
17685 return 0;
17686 }
developer82533be2023-06-28 17:21:01 +080017687 if (strstr(argv[1],"setGF")!=NULL)
17688 {
17689 BOOL enable = atoi(argv[3]);
17690 if((ret=wifi_setRadio11nGreenfieldEnable(index, enable))==RETURN_OK)
17691 printf("wifi_setRadio11nGreenfieldEnable success\n");
17692 else
17693 printf("wifi_setRadio11nGreenfieldEnable Error\n");
17694 }
17695 if (strstr(argv[1],"setVID")!=NULL)
17696 {
17697 INT vid = atoi(argv[3]);
17698 if((ret=wifi_setApVlanID(index, vid))==RETURN_OK)
17699 printf("wifi_setApVlanID success.\n");
17700 else
17701 printf("wifi_setApVlanID Error\n");
developerd14dff12023-06-28 22:47:44 +080017702 }
17703 if (strncmp(argv[1], "wifi_getApATMSta", strlen(argv[1])) == 0) {
17704 UCHAR outbuf[256]={0};
17705
17706 wifi_getApATMSta(index, outbuf, sizeof(outbuf));
17707 printf("sta air time percent is %s \n", outbuf);
17708 return 0;
17709 }
developera3511852023-06-14 14:12:59 +080017710 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17711 return 0;
developer72fb0bb2023-01-11 09:46:29 +080017712}
17713
17714#endif
17715
17716#ifdef WIFI_HAL_VERSION_3
17717
developer32f2a182023-06-27 19:50:41 +080017718INT BitMapToTransmitRates(UINT bitMap, char *BasicRate, unsigned long size)
developer72fb0bb2023-01-11 09:46:29 +080017719{
developera3511852023-06-14 14:12:59 +080017720 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer32f2a182023-06-27 19:50:41 +080017721 if (bitMap & WIFI_BITRATE_1MBPS) {
17722 if ((size - strlen(BasicRate)) <= 2)
17723 return RETURN_ERR;
17724 strncat(BasicRate, "1,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17725 }
17726 if (bitMap & WIFI_BITRATE_2MBPS) {
17727 if ((size - strlen(BasicRate)) <= 2)
17728 return RETURN_ERR;
17729 strncat(BasicRate, "2,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17730 }
17731 if (bitMap & WIFI_BITRATE_5_5MBPS) {
17732 if ((size - strlen(BasicRate)) <= 4)
17733 return RETURN_ERR;
17734 strncat(BasicRate, "5.5,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17735 }
17736 if (bitMap & WIFI_BITRATE_6MBPS) {
17737 if ((size - strlen(BasicRate)) <= 2)
17738 return RETURN_ERR;
17739 strncat(BasicRate, "6,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17740 }
17741 if (bitMap & WIFI_BITRATE_9MBPS) {
17742 if ((size - strlen(BasicRate)) <= 2)
17743 return RETURN_ERR;
17744 strncat(BasicRate, "9,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17745 }
17746 if (bitMap & WIFI_BITRATE_11MBPS) {
17747 if ((size - strlen(BasicRate)) <= 3)
17748 return RETURN_ERR;
17749 strncat(BasicRate, "11,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17750 }
17751 if (bitMap & WIFI_BITRATE_12MBPS) {
17752 if ((size - strlen(BasicRate)) <= 3)
17753 return RETURN_ERR;
17754 strncat(BasicRate, "12,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17755 }
17756 if (bitMap & WIFI_BITRATE_18MBPS) {
17757 if ((size - strlen(BasicRate)) <= 3)
17758 return RETURN_ERR;
17759 strncat(BasicRate, "18,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17760 }
17761 if (bitMap & WIFI_BITRATE_24MBPS) {
17762 if ((size - strlen(BasicRate)) <= 3)
17763 return RETURN_ERR;
17764 strncat(BasicRate, "24,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17765 }
17766 if (bitMap & WIFI_BITRATE_36MBPS) {
17767 if ((size - strlen(BasicRate)) <= 3)
17768 return RETURN_ERR;
17769 strncat(BasicRate, "36,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17770 }
17771 if (bitMap & WIFI_BITRATE_48MBPS) {
17772 if ((size - strlen(BasicRate)) <= 3)
17773 return RETURN_ERR;
17774 strncat(BasicRate, "48,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17775 }
17776 if (bitMap & WIFI_BITRATE_54MBPS) {
17777 if ((size - strlen(BasicRate)) <= 3)
17778 return RETURN_ERR;
17779 strncat(BasicRate, "54,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17780 }
developera3511852023-06-14 14:12:59 +080017781 if (strlen(BasicRate) != 0) // remove last comma
17782 BasicRate[strlen(BasicRate) - 1] = '\0';
17783 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17784 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017785}
17786
17787INT TransmitRatesToBitMap (char *BasicRatesList, UINT *basicRateBitMap)
17788{
developera3511852023-06-14 14:12:59 +080017789 UINT BitMap = 0;
17790 char *rate;
developer72fb0bb2023-01-11 09:46:29 +080017791
developera3511852023-06-14 14:12:59 +080017792 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
17793 rate = strtok(BasicRatesList, ",");
17794 while(rate != NULL)
17795 {
17796 if (strcmp(rate, "1") == 0)
17797 BitMap |= WIFI_BITRATE_1MBPS;
17798 else if (strcmp(rate, "2") == 0)
17799 BitMap |= WIFI_BITRATE_2MBPS;
17800 else if (strcmp(rate, "5.5") == 0)
17801 BitMap |= WIFI_BITRATE_5_5MBPS;
17802 else if (strcmp(rate, "6") == 0)
17803 BitMap |= WIFI_BITRATE_6MBPS;
17804 else if (strcmp(rate, "9") == 0)
17805 BitMap |= WIFI_BITRATE_9MBPS;
17806 else if (strcmp(rate, "11") == 0)
17807 BitMap |= WIFI_BITRATE_11MBPS;
17808 else if (strcmp(rate, "12") == 0)
17809 BitMap |= WIFI_BITRATE_12MBPS;
17810 else if (strcmp(rate, "18") == 0)
17811 BitMap |= WIFI_BITRATE_18MBPS;
17812 else if (strcmp(rate, "24") == 0)
17813 BitMap |= WIFI_BITRATE_24MBPS;
17814 else if (strcmp(rate, "36") == 0)
17815 BitMap |= WIFI_BITRATE_36MBPS;
17816 else if (strcmp(rate, "48") == 0)
17817 BitMap |= WIFI_BITRATE_48MBPS;
17818 else if (strcmp(rate, "54") == 0)
17819 BitMap |= WIFI_BITRATE_54MBPS;
17820 rate = strtok(NULL, ",");
17821 }
17822 *basicRateBitMap = BitMap;
17823 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17824 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017825}
17826
17827// This API is used to configured all radio operation parameter in a single set. it includes channel number, channelWidth, mode and auto chammel configuration.
17828INT wifi_setRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17829{
developera3511852023-06-14 14:12:59 +080017830 char buf[128] = {0};
17831 int bandwidth = 20;
17832 int set_mode = 0;
developer56fbedb2023-05-30 16:47:05 +080017833 BOOL drv_dat_change = 0, hapd_conf_change = 0;
developera3511852023-06-14 14:12:59 +080017834 wifi_radio_operationParam_t current_param;
developer72fb0bb2023-01-11 09:46:29 +080017835
developera3511852023-06-14 14:12:59 +080017836 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017837
developera3511852023-06-14 14:12:59 +080017838 multiple_set = TRUE;
17839 if (wifi_getRadioOperatingParameters(index, &current_param) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017840 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingParameters return error.\n");
developera3511852023-06-14 14:12:59 +080017841 return RETURN_ERR;
17842 }
17843 if (current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17844 if (wifi_setRadioAutoChannelEnable(index, operationParam->autoChannelEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017845 wifi_debug(DEBUG_ERROR, "wifi_setRadioAutoChannelEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017846 return RETURN_ERR;
17847 }
17848 drv_dat_change = TRUE;
17849 }
17850 if (current_param.channelWidth != operationParam->channelWidth ||
17851 current_param.channel != operationParam->channel ||
17852 current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17853 if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_20MHZ)
17854 bandwidth = 20;
17855 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_40MHZ)
17856 bandwidth = 40;
17857 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80MHZ)
17858 bandwidth = 80;
17859 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_160MHZ || operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80_80MHZ)
17860 bandwidth = 160;
developer72fb0bb2023-01-11 09:46:29 +080017861
developera3511852023-06-14 14:12:59 +080017862 if (operationParam->autoChannelEnabled) {
17863 if (wifi_pushRadioChannel2(index, 0, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017864 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017865 return RETURN_ERR;
17866 }
17867 } else {
17868 if (wifi_pushRadioChannel2(index, operationParam->channel, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017869 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017870 return RETURN_ERR;
17871 }
17872 }
developer56fbedb2023-05-30 16:47:05 +080017873 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017874 }
17875 if (current_param.variant != operationParam->variant) {
17876 // Two different definition bit map, so need to check every bit.
17877 if (operationParam->variant & WIFI_80211_VARIANT_A)
17878 set_mode |= WIFI_MODE_A;
17879 if (operationParam->variant & WIFI_80211_VARIANT_B)
17880 set_mode |= WIFI_MODE_B;
17881 if (operationParam->variant & WIFI_80211_VARIANT_G)
17882 set_mode |= WIFI_MODE_G;
17883 if (operationParam->variant & WIFI_80211_VARIANT_N)
17884 set_mode |= WIFI_MODE_N;
17885 if (operationParam->variant & WIFI_80211_VARIANT_AC)
17886 set_mode |= WIFI_MODE_AC;
17887 if (operationParam->variant & WIFI_80211_VARIANT_AX)
17888 set_mode |= WIFI_MODE_AX;
17889 // Second parameter is to set channel band width, it is done by wifi_pushRadioChannel2 if changed.
17890 memset(buf, 0, sizeof(buf));
17891 drv_dat_change = TRUE;
17892 if (wifi_setRadioMode_by_dat(index, set_mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017893 wifi_debug(DEBUG_ERROR, "wifi_setRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080017894 return RETURN_ERR;
17895 }
17896 }
17897 if (current_param.dtimPeriod != operationParam->dtimPeriod) {
developer56fbedb2023-05-30 16:47:05 +080017898 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017899 if (wifi_setApDTIMInterval(index, operationParam->dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017900 wifi_debug(DEBUG_ERROR, "wifi_setApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017901 return RETURN_ERR;
17902 }
17903 }
17904 if (current_param.beaconInterval != operationParam->beaconInterval) {
developer56fbedb2023-05-30 16:47:05 +080017905 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017906 if (wifi_setRadioBeaconPeriod(index, operationParam->beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017907 wifi_debug(DEBUG_ERROR, "wifi_setRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080017908 return RETURN_ERR;
17909 }
17910 }
17911 if (current_param.operationalDataTransmitRates != operationParam->operationalDataTransmitRates) {
developer56fbedb2023-05-30 16:47:05 +080017912 hapd_conf_change = TRUE;
developer32f2a182023-06-27 19:50:41 +080017913 BitMapToTransmitRates(operationParam->operationalDataTransmitRates, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080017914 if (wifi_setRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017915 wifi_debug(DEBUG_ERROR, "wifi_setRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080017916 return RETURN_ERR;
17917 }
17918 }
17919 if (current_param.fragmentationThreshold != operationParam->fragmentationThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017920 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017921 if (wifi_setRadioFragmentationThreshold(index, operationParam->fragmentationThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017922 wifi_debug(DEBUG_ERROR, "wifi_setRadioFragmentationThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017923 return RETURN_ERR;
17924 }
17925 }
17926 if (current_param.guardInterval != operationParam->guardInterval) {
developer56fbedb2023-05-30 16:47:05 +080017927 hapd_conf_change = TRUE;
17928 drv_dat_change = TRUE;
17929 if (wifi_setGuardInterval(index, operationParam->guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017930 wifi_debug(DEBUG_ERROR, "wifi_setGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017931 return RETURN_ERR;
17932 }
17933 }
17934 if (current_param.transmitPower != operationParam->transmitPower) {
developer56fbedb2023-05-30 16:47:05 +080017935 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017936 if (wifi_setRadioTransmitPower(index, operationParam->transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017937 wifi_debug(DEBUG_ERROR, "wifi_setRadioTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080017938 return RETURN_ERR;
17939 }
17940 }
17941 if (current_param.rtsThreshold != operationParam->rtsThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017942 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017943 if (wifi_setApRtsThreshold(index, operationParam->rtsThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017944 wifi_debug(DEBUG_ERROR, "wifi_setApRtsThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017945 return RETURN_ERR;
17946 }
17947 }
17948 if (current_param.obssCoex != operationParam->obssCoex) {
developer56fbedb2023-05-30 16:47:05 +080017949 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017950 if (wifi_setRadioObssCoexistenceEnable(index, operationParam->obssCoex) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017951 wifi_debug(DEBUG_ERROR, "wifi_setRadioObssCoexistenceEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017952 return RETURN_ERR;
17953 }
17954 }
17955 if (current_param.stbcEnable != operationParam->stbcEnable) {
developer56fbedb2023-05-30 16:47:05 +080017956 hapd_conf_change = TRUE;
17957 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017958 if (wifi_setRadioSTBCEnable(index, operationParam->stbcEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017959 wifi_debug(DEBUG_ERROR, "wifi_setRadioSTBCEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017960 return RETURN_ERR;
17961 }
17962 }
17963 if (current_param.greenFieldEnable != operationParam->greenFieldEnable) {
17964 if (wifi_setRadio11nGreenfieldEnable(index, operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017965 wifi_debug(DEBUG_ERROR, "wifi_setRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017966 return RETURN_ERR;
17967 }
17968 }
developer72fb0bb2023-01-11 09:46:29 +080017969
developera3511852023-06-14 14:12:59 +080017970 /* only down/up interface when dat file has been changed,
17971 * if enable is true, then restart the radio.
17972 */
17973 if (drv_dat_change == TRUE) {
17974 wifi_setRadioEnable(index, FALSE);
developer56fbedb2023-05-30 16:47:05 +080017975 if (operationParam->enable == TRUE)
developera3511852023-06-14 14:12:59 +080017976 wifi_setRadioEnable(index, TRUE);
17977 } else if (hapd_conf_change == TRUE) {
17978 hostapd_raw_remove_bss(index);
17979 if (operationParam->enable == TRUE)
17980 hostapd_raw_add_bss(index);
17981 }
developer56fbedb2023-05-30 16:47:05 +080017982
developera3511852023-06-14 14:12:59 +080017983 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017984
developera3511852023-06-14 14:12:59 +080017985 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017986}
17987
17988INT wifi_getRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17989{
developera3511852023-06-14 14:12:59 +080017990 char band[64] = {0};
17991 char buf[256] = {0};
17992 char config_file[64] = {0};
17993 char cmd[128] = {0};
17994 UINT mode = 0;
17995 BOOL enabled = FALSE;
developer863a4a62023-06-06 16:55:59 +080017996 int dtimPeriod;
developer2f79c922023-06-02 17:33:42 +080017997 UINT beaconInterval;
17998 UINT basicDataTransmitRates;
17999 UINT operationalDataTransmitRates;
18000 wifi_guard_interval_t guardInterval;
18001 UINT transmitPower;
developere40952c2023-06-15 18:46:43 +080018002 int res;
developerc14d83a2023-06-29 20:09:42 +080018003 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080018004
developera3511852023-06-14 14:12:59 +080018005 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18006 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018007
developera3511852023-06-14 14:12:59 +080018008 memset(operationParam, 0, sizeof(wifi_radio_operationParam_t));
developere40952c2023-06-15 18:46:43 +080018009 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, index);
18010 if (os_snprintf_error(sizeof(config_file), res)) {
18011 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18012 return RETURN_ERR;
18013 }
developera3511852023-06-14 14:12:59 +080018014 if (wifi_getRadioEnable(index, &enabled) != RETURN_OK)
18015 {
developer75bd10c2023-06-27 11:34:08 +080018016 wifi_debug(DEBUG_ERROR, "wifi_getRadioEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018017 return RETURN_ERR;
18018 }
18019 operationParam->enable = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018020
developera3511852023-06-14 14:12:59 +080018021 memset(band, 0, sizeof(band));
18022 if (wifi_getRadioOperatingFrequencyBand(index, band) != RETURN_OK)
18023 {
developer75bd10c2023-06-27 11:34:08 +080018024 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingFrequencyBand return error.\n");
developera3511852023-06-14 14:12:59 +080018025 return RETURN_ERR;
18026 }
developer72fb0bb2023-01-11 09:46:29 +080018027
developera3511852023-06-14 14:12:59 +080018028 if (!strcmp(band, "2.4GHz"))
18029 operationParam->band = WIFI_FREQUENCY_2_4_BAND;
18030 else if (!strcmp(band, "5GHz"))
18031 operationParam->band = WIFI_FREQUENCY_5_BAND;
18032 else if (!strcmp(band, "6GHz"))
18033 operationParam->band = WIFI_FREQUENCY_6_BAND;
18034 else
18035 {
developer75bd10c2023-06-27 11:34:08 +080018036 wifi_debug(DEBUG_ERROR, "cannot decode band for radio index %d ('%s')\n", index, band);
developera3511852023-06-14 14:12:59 +080018037 }
developer72fb0bb2023-01-11 09:46:29 +080018038
developera3511852023-06-14 14:12:59 +080018039 wifi_hostapdRead(config_file, "channel", buf, sizeof(buf));
18040 if (strcmp(buf, "0") == 0 || strcmp(buf, "acs_survey") == 0) {
18041 operationParam->channel = 0;
18042 operationParam->autoChannelEnabled = TRUE;
18043 } else {
developerc14d83a2023-06-29 20:09:42 +080018044 if (hal_strtoul(buf, 10, &tmp) < 0) {
18045 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080018046 }
18047 operationParam->channel = tmp;
developer5b23cd02023-07-19 20:26:03 +080018048
developera3511852023-06-14 14:12:59 +080018049 operationParam->autoChannelEnabled = FALSE;
18050 }
developer72fb0bb2023-01-11 09:46:29 +080018051
developera3511852023-06-14 14:12:59 +080018052 memset(buf, 0, sizeof(buf));
18053 if (wifi_getRadioOperatingChannelBandwidth(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018054 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error.\n");
developera3511852023-06-14 14:12:59 +080018055 return RETURN_ERR;
18056 }
18057 if (!strcmp(buf, "20MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
18058 else if (!strcmp(buf, "40MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
18059 else if (!strcmp(buf, "80MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
18060 else if (!strcmp(buf, "160MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
18061 else
18062 {
developer75bd10c2023-06-27 11:34:08 +080018063 wifi_debug(DEBUG_ERROR, "Unknown channel bandwidth: %s\n", buf);
developera3511852023-06-14 14:12:59 +080018064 return false;
18065 }
developer72fb0bb2023-01-11 09:46:29 +080018066
developera3511852023-06-14 14:12:59 +080018067 if (wifi_getRadioMode(index, buf, &mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018068 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080018069 return RETURN_ERR;
18070 }
18071 // Two different definition bit map, so need to check every bit.
18072 if (mode & WIFI_MODE_A)
18073 operationParam->variant |= WIFI_80211_VARIANT_A;
18074 if (mode & WIFI_MODE_B)
18075 operationParam->variant |= WIFI_80211_VARIANT_B;
18076 if (mode & WIFI_MODE_G)
18077 operationParam->variant |= WIFI_80211_VARIANT_G;
18078 if (mode & WIFI_MODE_N)
18079 operationParam->variant |= WIFI_80211_VARIANT_N;
18080 if (mode & WIFI_MODE_AC)
18081 operationParam->variant |= WIFI_80211_VARIANT_AC;
18082 if (mode & WIFI_MODE_AX)
18083 operationParam->variant |= WIFI_80211_VARIANT_AX;
18084 if (wifi_getRadioDCSEnable(index, &operationParam->DCSEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018085 wifi_debug(DEBUG_ERROR, "wifi_getRadioDCSEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018086 return RETURN_ERR;
18087 }
18088 if (wifi_getApDTIMInterval(index, &dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018089 wifi_debug(DEBUG_ERROR, "wifi_getApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018090 return RETURN_ERR;
18091 }
developer2f79c922023-06-02 17:33:42 +080018092 operationParam->dtimPeriod = dtimPeriod;
developera3511852023-06-14 14:12:59 +080018093 if (wifi_getRadioBeaconPeriod(index, &beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018094 wifi_debug(DEBUG_ERROR, "wifi_getRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080018095 return RETURN_ERR;
18096 }
developer2f79c922023-06-02 17:33:42 +080018097 operationParam->beaconInterval = beaconInterval;
developer72fb0bb2023-01-11 09:46:29 +080018098
developera3511852023-06-14 14:12:59 +080018099 memset(buf, 0, sizeof(buf));
18100 if (wifi_getRadioSupportedDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018101 wifi_debug(DEBUG_ERROR, "wifi_getRadioSupportedDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018102 return RETURN_ERR;
18103 }
18104 TransmitRatesToBitMap(buf, &basicDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018105 operationParam->basicDataTransmitRates = basicDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018106
developera3511852023-06-14 14:12:59 +080018107 memset(buf, 0, sizeof(buf));
18108 if (wifi_getRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018109 wifi_debug(DEBUG_ERROR, "wifi_getRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018110 return RETURN_ERR;
18111 }
18112 TransmitRatesToBitMap(buf, &operationalDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018113 operationParam->operationalDataTransmitRates = operationalDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018114
developera3511852023-06-14 14:12:59 +080018115 memset(buf, 0, sizeof(buf));
18116 wifi_hostapdRead(config_file, "fragm_threshold", buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080018117 if (hal_strtoul(buf, 10, &tmp) < 0) {
18118 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18119 }
18120 operationParam->fragmentationThreshold = tmp;
developer72fb0bb2023-01-11 09:46:29 +080018121
developera3511852023-06-14 14:12:59 +080018122 if (wifi_getGuardInterval(index, &guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018123 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018124 return RETURN_ERR;
18125 }
developer2f79c922023-06-02 17:33:42 +080018126 operationParam->guardInterval = guardInterval;
18127
developera3511852023-06-14 14:12:59 +080018128 if (wifi_getRadioPercentageTransmitPower(index, (ULONG *)&transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018129 wifi_debug(DEBUG_ERROR, "wifi_getRadioPercentageTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080018130 return RETURN_ERR;
18131 }
developer2f79c922023-06-02 17:33:42 +080018132 operationParam->transmitPower = transmitPower;
developer72fb0bb2023-01-11 09:46:29 +080018133
developera3511852023-06-14 14:12:59 +080018134 memset(buf, 0, sizeof(buf));
18135 wifi_hostapdRead(config_file, "rts_threshold", buf, sizeof(buf));
18136 if (strcmp(buf, "-1") == 0) {
18137 operationParam->rtsThreshold = (UINT)-1; // maxuimum unsigned integer value
18138 operationParam->ctsProtection = FALSE;
18139 } else {
developerc14d83a2023-06-29 20:09:42 +080018140 if (hal_strtoul(buf, 10, &tmp) < 0) {
18141 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18142 }
18143 operationParam->rtsThreshold = tmp;
developera3511852023-06-14 14:12:59 +080018144 operationParam->ctsProtection = TRUE;
18145 }
developer72fb0bb2023-01-11 09:46:29 +080018146
developera3511852023-06-14 14:12:59 +080018147 memset(buf, 0, sizeof(buf));
18148 wifi_hostapdRead(config_file, "ht_coex", buf, sizeof(buf));
18149 if (strcmp(buf, "0") == 0)
18150 operationParam->obssCoex = FALSE;
18151 else
18152 operationParam->obssCoex = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018153
developere40952c2023-06-15 18:46:43 +080018154 res = snprintf(cmd, sizeof(cmd), "cat %s | grep STBC", config_file);
18155 if (os_snprintf_error(sizeof(cmd), res)) {
18156 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18157 return RETURN_ERR;
18158 }
developera3511852023-06-14 14:12:59 +080018159 _syscmd(cmd, buf, sizeof(buf));
18160 if (strlen(buf) != 0)
18161 operationParam->stbcEnable = TRUE;
18162 else
18163 operationParam->stbcEnable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018164
developera3511852023-06-14 14:12:59 +080018165 if (wifi_getRadio11nGreenfieldEnable(index, &operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018166 wifi_debug(DEBUG_ERROR, "wifi_getRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018167 return RETURN_ERR;
18168 }
developer72fb0bb2023-01-11 09:46:29 +080018169
developera3511852023-06-14 14:12:59 +080018170 // Below value is hardcoded
developer72fb0bb2023-01-11 09:46:29 +080018171
developera3511852023-06-14 14:12:59 +080018172 operationParam->numSecondaryChannels = 0;
18173 for (int i = 0; i < MAXNUMSECONDARYCHANNELS; i++) {
18174 operationParam->channelSecondary[i] = 0;
18175 }
18176 operationParam->csa_beacon_count = 15;
18177 operationParam->countryCode = wifi_countrycode_US; // hard to convert string to corresponding enum
developer72fb0bb2023-01-11 09:46:29 +080018178
developera3511852023-06-14 14:12:59 +080018179 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18180 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018181}
18182
18183static int array_index_to_vap_index(UINT radioIndex, int arrayIndex)
18184{
developera3511852023-06-14 14:12:59 +080018185 int max_radio_num = 0;
developer72fb0bb2023-01-11 09:46:29 +080018186
developera3511852023-06-14 14:12:59 +080018187 wifi_getMaxRadioNumber(&max_radio_num);
18188 if (radioIndex >= max_radio_num) {
developer75bd10c2023-06-27 11:34:08 +080018189 wifi_debug(DEBUG_ERROR, "Wrong radio index (%d)\n", radioIndex);
developera3511852023-06-14 14:12:59 +080018190 return RETURN_ERR;
18191 }
developer72fb0bb2023-01-11 09:46:29 +080018192
developera3511852023-06-14 14:12:59 +080018193 return (arrayIndex * max_radio_num) + radioIndex;
developer72fb0bb2023-01-11 09:46:29 +080018194}
18195
developer96b38512023-02-22 11:17:45 +080018196static int vap_index_to_array_index(int vapIndex, int *radioIndex, int *arrayIndex)
18197{
developera3511852023-06-14 14:12:59 +080018198 int max_radio_num = 0;
developer96b38512023-02-22 11:17:45 +080018199
developera3511852023-06-14 14:12:59 +080018200 if ((vapIndex < 0) || (vapIndex > MAX_NUM_VAP_PER_RADIO*MAX_NUM_RADIOS))
developer96b38512023-02-22 11:17:45 +080018201 return -1;
18202
developera3511852023-06-14 14:12:59 +080018203 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080018204 if(max_radio_num == 0){
18205 return RETURN_ERR;
18206 }
developera3511852023-06-14 14:12:59 +080018207 (*radioIndex) = vapIndex % max_radio_num;
18208 (*arrayIndex) = vapIndex / max_radio_num;
developer96b38512023-02-22 11:17:45 +080018209
developera3511852023-06-14 14:12:59 +080018210 return 0;
developer96b38512023-02-22 11:17:45 +080018211}
18212
18213
developer72fb0bb2023-01-11 09:46:29 +080018214wifi_bitrate_t beaconRate_string_to_enum(char *beaconRate) {
developera3511852023-06-14 14:12:59 +080018215 if (strncmp(beaconRate, "1Mbps", 5) == 0)
18216 return WIFI_BITRATE_1MBPS;
18217 else if (strncmp(beaconRate, "2Mbps", 5) == 0)
18218 return WIFI_BITRATE_2MBPS;
18219 else if (strncmp(beaconRate, "5.5Mbps", 7) == 0)
18220 return WIFI_BITRATE_5_5MBPS;
18221 else if (strncmp(beaconRate, "6Mbps", 5) == 0)
18222 return WIFI_BITRATE_6MBPS;
18223 else if (strncmp(beaconRate, "9Mbps", 5) == 0)
18224 return WIFI_BITRATE_9MBPS;
18225 else if (strncmp(beaconRate, "11Mbps", 6) == 0)
18226 return WIFI_BITRATE_11MBPS;
18227 else if (strncmp(beaconRate, "12Mbps", 6) == 0)
18228 return WIFI_BITRATE_12MBPS;
18229 else if (strncmp(beaconRate, "18Mbps", 6) == 0)
18230 return WIFI_BITRATE_18MBPS;
18231 else if (strncmp(beaconRate, "24Mbps", 6) == 0)
18232 return WIFI_BITRATE_24MBPS;
18233 else if (strncmp(beaconRate, "36Mbps", 6) == 0)
18234 return WIFI_BITRATE_36MBPS;
18235 else if (strncmp(beaconRate, "48Mbps", 6) == 0)
18236 return WIFI_BITRATE_48MBPS;
18237 else if (strncmp(beaconRate, "54Mbps", 6) == 0)
18238 return WIFI_BITRATE_54MBPS;
18239 return WIFI_BITRATE_DEFAULT;
developer72fb0bb2023-01-11 09:46:29 +080018240}
18241
developer32f2a182023-06-27 19:50:41 +080018242struct beacon_rate_2_string {
18243 wifi_bitrate_t beacon;
18244 char beacon_str[8];
18245};
18246
18247struct beacon_rate_2_string br2str[12] = {
18248 {WIFI_BITRATE_1MBPS, "1Mbps"},
18249 {WIFI_BITRATE_2MBPS, "2Mbps"},
18250 {WIFI_BITRATE_5_5MBPS, "5.5Mbps"},
18251 {WIFI_BITRATE_6MBPS, "6Mbps"},
18252 {WIFI_BITRATE_9MBPS, "9Mbps"},
18253 {WIFI_BITRATE_11MBPS, "11Mbps"},
18254 {WIFI_BITRATE_12MBPS, "12Mbps"},
18255 {WIFI_BITRATE_18MBPS, "18Mbps"},
18256 {WIFI_BITRATE_24MBPS, "24Mbps"},
18257 {WIFI_BITRATE_36MBPS, "36Mbps"},
18258 {WIFI_BITRATE_48MBPS, "48Mbps"},
18259 {WIFI_BITRATE_54MBPS, "54Mbps"}
18260};
18261
18262INT beaconRate_enum_to_string(wifi_bitrate_t beacon, char *beacon_str, unsigned long str_size)
developer72fb0bb2023-01-11 09:46:29 +080018263{
developer32f2a182023-06-27 19:50:41 +080018264 int i;
18265 unsigned long len;
18266
18267 for (i = 0; i < (sizeof(br2str)/sizeof(br2str[0])); i++) {
18268 if (beacon == br2str[i].beacon) {
18269 len = strlen(br2str[i].beacon_str);
18270 if (len >= str_size)
18271 return RETURN_ERR;
18272 memcpy(beacon_str, br2str[i].beacon_str, len);
18273 beacon_str[len] = '\0';
18274 break;
18275 }
18276 }
developera3511852023-06-14 14:12:59 +080018277 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018278}
18279
18280INT wifi_getRadioVapInfoMap(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18281{
developera3511852023-06-14 14:12:59 +080018282 INT mode = 0;
18283 INT ret = -1;
18284 UINT output = 0;
18285 int i = 0;
18286 int vap_index = 0;
18287 BOOL enabled = FALSE;
18288 char buf[32] = {0};
18289 wifi_vap_security_t security = {0};
developere40952c2023-06-15 18:46:43 +080018290 int res;
developer72fb0bb2023-01-11 09:46:29 +080018291
developera3511852023-06-14 14:12:59 +080018292 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18293 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018294
developera3511852023-06-14 14:12:59 +080018295 ret = wifi_BandProfileRead(0, index, "BssidNum", buf, sizeof(buf), "0");
18296 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +080018297 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead BssidNum failed\n");
developera3511852023-06-14 14:12:59 +080018298 return RETURN_ERR;
18299 }
developerfde01262023-05-22 15:15:24 +080018300
developera3511852023-06-14 14:12:59 +080018301 map->num_vaps = atoi(buf);
18302 if (map->num_vaps <= 0) {
developer75bd10c2023-06-27 11:34:08 +080018303 wifi_debug(DEBUG_ERROR, "invalid BssidNum %s\n", buf);
developera3511852023-06-14 14:12:59 +080018304 return RETURN_ERR;
18305 }
developerfde01262023-05-22 15:15:24 +080018306
developera3511852023-06-14 14:12:59 +080018307 for (i = 0; i < map->num_vaps; i++)
18308 {
18309 map->vap_array[i].radio_index = index;
developer72fb0bb2023-01-11 09:46:29 +080018310
developera3511852023-06-14 14:12:59 +080018311 vap_index = array_index_to_vap_index(index, i);
18312 if (vap_index < 0)
18313 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080018314
developer9ce44382023-06-28 11:09:37 +080018315 strncpy(map->vap_array[i].bridge_name, BRIDGE_NAME,sizeof(map->vap_array[i].bridge_name) - 1);
18316 map->vap_array[i].bridge_name[sizeof(map->vap_array[i].bridge_name) - 1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080018317
developera3511852023-06-14 14:12:59 +080018318 map->vap_array[i].vap_index = vap_index;
developer72fb0bb2023-01-11 09:46:29 +080018319
developera3511852023-06-14 14:12:59 +080018320 memset(buf, 0, sizeof(buf));
18321 ret = wifi_getApName(vap_index, buf);
18322 if (ret != RETURN_OK) {
18323 printf("%s: wifi_getApName return error\n", __func__);
18324 return RETURN_ERR;
18325 }
developere40952c2023-06-15 18:46:43 +080018326 res = snprintf(map->vap_array[i].vap_name, sizeof(map->vap_array[i].vap_name), "%s", buf);
18327 if (os_snprintf_error(sizeof(map->vap_array[i].vap_name), res)) {
18328 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18329 return RETURN_ERR;
18330 }
developer72fb0bb2023-01-11 09:46:29 +080018331
developera3511852023-06-14 14:12:59 +080018332 memset(buf, 0, sizeof(buf));
18333 ret = wifi_getSSIDName(vap_index, buf);
18334 if (ret != RETURN_OK) {
18335 printf("%s: wifi_getSSIDName return error\n", __func__);
18336 return RETURN_ERR;
18337 }
developere40952c2023-06-15 18:46:43 +080018338 res = snprintf(map->vap_array[i].u.bss_info.ssid, sizeof(map->vap_array[i].u.bss_info.ssid), "%s", buf);
18339 if (os_snprintf_error(sizeof(map->vap_array[i].u.bss_info.ssid), res)) {
18340 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18341 return RETURN_ERR;
18342 }
developer72fb0bb2023-01-11 09:46:29 +080018343
developera3511852023-06-14 14:12:59 +080018344 map->vap_array[i].u.bss_info.enabled = true;
developer72fb0bb2023-01-11 09:46:29 +080018345
developera3511852023-06-14 14:12:59 +080018346 ret = wifi_getApSsidAdvertisementEnable(vap_index, &enabled);
18347 if (ret != RETURN_OK) {
18348 printf("%s: wifi_getApSsidAdvertisementEnable return error\n", __func__);
18349 return RETURN_ERR;
18350 }
18351 map->vap_array[i].u.bss_info.showSsid = enabled;
developer69b61b02023-03-07 17:17:44 +080018352
developera3511852023-06-14 14:12:59 +080018353 ret = wifi_getApIsolationEnable(vap_index, &enabled);
18354 if (ret != RETURN_OK) {
18355 printf("%s: wifi_getApIsolationEnable return error\n", __func__);
18356 return RETURN_ERR;
18357 }
18358 map->vap_array[i].u.bss_info.isolation = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018359
developera3511852023-06-14 14:12:59 +080018360 ret = wifi_getApMaxAssociatedDevices(vap_index, &output);
18361 if (ret != RETURN_OK) {
18362 printf("%s: wifi_getApMaxAssociatedDevices return error\n", __func__);
18363 return RETURN_ERR;
18364 }
18365 map->vap_array[i].u.bss_info.bssMaxSta = output;
developer72fb0bb2023-01-11 09:46:29 +080018366
developera3511852023-06-14 14:12:59 +080018367 ret = wifi_getBSSTransitionActivation(vap_index, &enabled);
18368 if (ret != RETURN_OK) {
18369 printf("%s: wifi_getBSSTransitionActivation return error\n", __func__);
18370 return RETURN_ERR;
18371 }
18372 map->vap_array[i].u.bss_info.bssTransitionActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018373
developera3511852023-06-14 14:12:59 +080018374 ret = wifi_getNeighborReportActivation(vap_index, &enabled);
18375 if (ret != RETURN_OK) {
18376 printf("%s: wifi_getNeighborReportActivation return error\n", __func__);
18377 return RETURN_ERR;
18378 }
18379 map->vap_array[i].u.bss_info.nbrReportActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018380
developera3511852023-06-14 14:12:59 +080018381 ret = wifi_getApSecurity(vap_index, &security);
18382 if (ret != RETURN_OK) {
18383 printf("%s: wifi_getApSecurity return error\n", __func__);
18384 return RETURN_ERR;
18385 }
18386 map->vap_array[i].u.bss_info.security = security;
developer72fb0bb2023-01-11 09:46:29 +080018387
developera3511852023-06-14 14:12:59 +080018388 ret = wifi_getApMacAddressControlMode(vap_index, &mode);
18389 if (ret != RETURN_OK) {
18390 printf("%s: wifi_getApMacAddressControlMode return error\n", __func__);
18391 return RETURN_ERR;
18392 }
18393 if (mode == 0)
18394 map->vap_array[i].u.bss_info.mac_filter_enable = FALSE;
18395 else
18396 map->vap_array[i].u.bss_info.mac_filter_enable = TRUE;
18397 if (mode == 1)
18398 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
18399 else if (mode == 2)
18400 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
developer72fb0bb2023-01-11 09:46:29 +080018401
developera3511852023-06-14 14:12:59 +080018402 ret = wifi_getApWmmEnable(vap_index, &enabled);
18403 if (ret != RETURN_OK) {
18404 printf("%s: wifi_getApWmmEnable return error\n", __func__);
18405 return RETURN_ERR;
18406 }
18407 map->vap_array[i].u.bss_info.wmm_enabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018408
developera3511852023-06-14 14:12:59 +080018409 ret = wifi_getApUAPSDCapability(vap_index, &enabled);
18410 if (ret != RETURN_OK) {
18411 printf("%s: wifi_getApUAPSDCapability return error\n", __func__);
18412 return RETURN_ERR;
18413 }
18414 map->vap_array[i].u.bss_info.UAPSDEnabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018415
developera3511852023-06-14 14:12:59 +080018416 memset(buf, 0, sizeof(buf));
18417 ret = wifi_getApBeaconRate(map->vap_array[i].radio_index, buf);
18418 if (ret != RETURN_OK) {
18419 printf("%s: wifi_getApBeaconRate return error\n", __func__);
18420 return RETURN_ERR;
18421 }
18422 map->vap_array[i].u.bss_info.beaconRate = beaconRate_string_to_enum(buf);
developer72fb0bb2023-01-11 09:46:29 +080018423
developera3511852023-06-14 14:12:59 +080018424 memset(buf, 0, sizeof(buf));
18425 ret = wifi_getBaseBSSID(vap_index, buf);
18426 if (ret != RETURN_OK) {
18427 printf("%s: wifi_getBaseBSSID return error\n", __func__);
18428 return RETURN_ERR;
18429 }
developer5b2f10c2023-05-25 17:02:21 +080018430 if (hwaddr_aton2(buf, map->vap_array[i].u.bss_info.bssid) < 0) {
18431 printf("%s: hwaddr_aton2 fail\n", __func__);
developera3511852023-06-14 14:12:59 +080018432 return RETURN_ERR;
developer5b2f10c2023-05-25 17:02:21 +080018433 }
developer72fb0bb2023-01-11 09:46:29 +080018434
developera3511852023-06-14 14:12:59 +080018435 ret = wifi_getRadioIGMPSnoopingEnable(map->vap_array[i].radio_index, &enabled);
18436 if (ret != RETURN_OK) {
developerc14d83a2023-06-29 20:09:42 +080018437 wifi_debug(DEBUG_ERROR, "%s: wifi_getRadioIGMPSnoopingEnable\n", __func__);
developera3511852023-06-14 14:12:59 +080018438 return RETURN_ERR;
18439 }
18440 map->vap_array[i].u.bss_info.mcast2ucast = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018441
developera3511852023-06-14 14:12:59 +080018442 // TODO: wps, noack
18443 }
18444 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18445 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018446}
18447
developer47cc27a2023-05-17 23:09:58 +080018448void checkVapStatus(int apIndex, BOOL *enable)
developer72fb0bb2023-01-11 09:46:29 +080018449{
developera3511852023-06-14 14:12:59 +080018450 char if_name[16] = {0};
18451 char cmd[128] = {0};
18452 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080018453 int res;
developer72fb0bb2023-01-11 09:46:29 +080018454
developera3511852023-06-14 14:12:59 +080018455 *enable = FALSE;
18456 if (wifi_GetInterfaceName(apIndex, if_name) != RETURN_OK)
18457 return;
developer72fb0bb2023-01-11 09:46:29 +080018458
developere40952c2023-06-15 18:46:43 +080018459 res = snprintf(cmd, sizeof(cmd), "cat %s | grep ^%s=1", VAP_STATUS_FILE, if_name);
18460 if (os_snprintf_error(sizeof(cmd), res)) {
18461 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18462 return;
18463 }
developera3511852023-06-14 14:12:59 +080018464 _syscmd(cmd, buf, sizeof(buf));
18465 if (strlen(buf) > 0)
18466 *enable = TRUE;
18467 return;
developer72fb0bb2023-01-11 09:46:29 +080018468}
18469
developer56fbedb2023-05-30 16:47:05 +080018470int hostapd_manage_bss(INT apIndex, BOOL enable)
18471{
18472 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080018473 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer56fbedb2023-05-30 16:47:05 +080018474 char cmd[MAX_CMD_SIZE] = {0};
18475 char buf[MAX_BUF_SIZE] = {0};
18476 BOOL status = FALSE;
18477 int max_radio_num = 0;
18478 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080018479 int res;
developer56fbedb2023-05-30 16:47:05 +080018480
18481 wifi_getApEnable(apIndex, &status);
18482
18483 wifi_getMaxRadioNumber(&max_radio_num);
18484 if (enable == status)
18485 return RETURN_OK;
18486
18487 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
18488 return RETURN_ERR;
18489
18490 if (enable == TRUE) {
18491 int radioIndex = apIndex % max_radio_num;
18492 phyId = radio_index_to_phy(radioIndex);
developerc14d83a2023-06-29 20:09:42 +080018493 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080018494 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
18495 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18496 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18497 return RETURN_ERR;
18498 }
18499 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
18500 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18501 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18502 return RETURN_ERR;
18503 }
developer56fbedb2023-05-30 16:47:05 +080018504 _syscmd(cmd, buf, sizeof(buf));
18505 } else {
developerc14d83a2023-06-29 20:09:42 +080018506 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080018507 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
18508 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18509 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18510 return RETURN_ERR;
18511 }
developer56fbedb2023-05-30 16:47:05 +080018512 _syscmd(cmd, buf, sizeof(buf));
18513 }
developere40952c2023-06-15 18:46:43 +080018514 res = snprintf(cmd, MAX_CMD_SIZE, "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer56fbedb2023-05-30 16:47:05 +080018515 interface_name, interface_name, enable, VAP_STATUS_FILE);
developere40952c2023-06-15 18:46:43 +080018516 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18517 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18518 return RETURN_ERR;
18519 }
developer56fbedb2023-05-30 16:47:05 +080018520 _syscmd(cmd, buf, sizeof(buf));
18521 //Wait for wifi up/down to apply
18522 return RETURN_OK;
18523}
18524
18525int hostapd_raw_add_bss(int apIndex)
18526{
18527 return hostapd_manage_bss(apIndex, TRUE);
18528}
18529
18530int hostapd_raw_remove_bss(int apIndex)
18531{
18532 return hostapd_manage_bss(apIndex, FALSE);
18533}
18534
18535int hostapd_raw_restart_bss(int apIndex)
developer333c1eb2023-05-31 14:59:39 +080018536{
developerdaf24792023-06-06 11:40:04 +080018537 int ret = 0;
18538
18539 ret = hostapd_raw_remove_bss(apIndex);
18540 if(ret != RETURN_OK)
18541 return RETURN_ERR;
18542
18543 ret = hostapd_raw_add_bss(apIndex);
18544 if(ret != RETURN_OK)
18545 return RETURN_ERR;
18546
18547 return RETURN_OK;
developer56fbedb2023-05-30 16:47:05 +080018548}
18549
developer72fb0bb2023-01-11 09:46:29 +080018550INT wifi_createVAP(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18551{
developera3511852023-06-14 14:12:59 +080018552 unsigned int i;
18553 wifi_vap_info_t *vap_info = NULL;
18554 int acl_mode;
18555 int ret = 0;
18556 char buf[256] = {0};
18557 char cmd[128] = {0};
18558 char config_file[64] = {0};
18559 char psk_file[64] = {0};
18560 BOOL enable = FALSE;
18561 int band_idx;
developere40952c2023-06-15 18:46:43 +080018562 int res;
developere740c2a2023-05-23 18:34:32 +080018563
developer72fb0bb2023-01-11 09:46:29 +080018564
developera3511852023-06-14 14:12:59 +080018565 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18566 printf("Entering %s index = %d\n", __func__, (int)index);
18567 for (i = 0; i < map->num_vaps; i++)
18568 {
18569 multiple_set = TRUE;
18570 vap_info = &map->vap_array[i];
developer72fb0bb2023-01-11 09:46:29 +080018571
developera3511852023-06-14 14:12:59 +080018572 // Check vap status file to enable multiple ap if the system boot.
18573 checkVapStatus(vap_info->vap_index, &enable);
18574 if (vap_info->u.bss_info.enabled == FALSE && enable == FALSE)
18575 continue;
developer72fb0bb2023-01-11 09:46:29 +080018576
developer75bd10c2023-06-27 11:34:08 +080018577 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 +080018578
developera3511852023-06-14 14:12:59 +080018579 band_idx = radio_index_to_band(index);
developere40952c2023-06-15 18:46:43 +080018580 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18581 if (os_snprintf_error(sizeof(config_file), res)) {
18582 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18583 return RETURN_ERR;
18584 }
developer9ce44382023-06-28 11:09:37 +080018585 if(band_idx >= 0 && band_idx < sizeof(wifi_band_str)/sizeof(wifi_band_str[0])){
18586 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[band_idx], config_file);
18587 } else{
18588 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18589 return RETURN_ERR;
18590 }
developere40952c2023-06-15 18:46:43 +080018591 if (os_snprintf_error(sizeof(cmd), res)) {
18592 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18593 return RETURN_ERR;
18594 }
developera3511852023-06-14 14:12:59 +080018595 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080018596
developera3511852023-06-14 14:12:59 +080018597 struct params params[3];
18598 params[0].name = "interface";
18599 params[0].value = vap_info->vap_name;
developere40952c2023-06-15 18:46:43 +080018600 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", vap_info->vap_index);
18601 if (os_snprintf_error(sizeof(psk_file), res)) {
18602 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18603 return RETURN_ERR;
18604 }
developera3511852023-06-14 14:12:59 +080018605 params[1].name = "wpa_psk_file";
18606 params[1].value = psk_file;
18607 params[2].name = "ssid";
18608 params[2].value = vap_info->u.bss_info.ssid;
developer72fb0bb2023-01-11 09:46:29 +080018609
developer37646972023-06-29 10:58:43 +080018610 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18611 if (os_snprintf_error(sizeof(config_file), res)) {
18612 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18613 return RETURN_ERR;
18614 }
developera3511852023-06-14 14:12:59 +080018615 wifi_hostapdWrite(config_file, params, 3);
developer72fb0bb2023-01-11 09:46:29 +080018616
developere40952c2023-06-15 18:46:43 +080018617 res = snprintf(cmd, sizeof(cmd), "touch %s", psk_file);
18618 if (os_snprintf_error(sizeof(cmd), res)) {
18619 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18620 return RETURN_ERR;
18621 }
developera3511852023-06-14 14:12:59 +080018622 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080018623
developera3511852023-06-14 14:12:59 +080018624 ret = wifi_setSSIDName(vap_info->vap_index, vap_info->u.bss_info.ssid);
18625 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018626 wifi_debug(DEBUG_ERROR,"wifi_setSSIDName return error\n");
developera3511852023-06-14 14:12:59 +080018627 return RETURN_ERR;
18628 }
developer72fb0bb2023-01-11 09:46:29 +080018629
developera3511852023-06-14 14:12:59 +080018630 ret = wifi_setApSsidAdvertisementEnable(vap_info->vap_index, vap_info->u.bss_info.showSsid);
18631 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018632 wifi_debug(DEBUG_ERROR, "wifi_setApSsidAdvertisementEnable return error\n");
developera3511852023-06-14 14:12:59 +080018633 return RETURN_ERR;
18634 }
developer72fb0bb2023-01-11 09:46:29 +080018635
developera3511852023-06-14 14:12:59 +080018636 ret = wifi_setApIsolationEnable(vap_info->vap_index, vap_info->u.bss_info.isolation);
18637 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018638 wifi_debug(DEBUG_ERROR, "wifi_setApIsolationEnable return error\n");
developera3511852023-06-14 14:12:59 +080018639 return RETURN_ERR;
18640 }
developer72fb0bb2023-01-11 09:46:29 +080018641
developera3511852023-06-14 14:12:59 +080018642 ret = wifi_setApMaxAssociatedDevices(vap_info->vap_index, vap_info->u.bss_info.bssMaxSta);
18643 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018644 wifi_debug(DEBUG_ERROR, "wifi_setApMaxAssociatedDevices return error\n");
developera3511852023-06-14 14:12:59 +080018645 return RETURN_ERR;
18646 }
developer72fb0bb2023-01-11 09:46:29 +080018647
developera3511852023-06-14 14:12:59 +080018648 ret = wifi_setBSSTransitionActivation(vap_info->vap_index, vap_info->u.bss_info.bssTransitionActivated);
18649 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018650 wifi_debug(DEBUG_ERROR, "wifi_setBSSTransitionActivation return error\n");
developera3511852023-06-14 14:12:59 +080018651 return RETURN_ERR;
18652 }
developer72fb0bb2023-01-11 09:46:29 +080018653
developera3511852023-06-14 14:12:59 +080018654 ret = wifi_setNeighborReportActivation(vap_info->vap_index, vap_info->u.bss_info.nbrReportActivated);
18655 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018656 wifi_debug(DEBUG_ERROR, "wifi_setNeighborReportActivation return error\n");
developera3511852023-06-14 14:12:59 +080018657 return RETURN_ERR;
18658 }
developer72fb0bb2023-01-11 09:46:29 +080018659
developera3511852023-06-14 14:12:59 +080018660 if (vap_info->u.bss_info.mac_filter_enable == false){
18661 acl_mode = 0;
18662 }else {
18663 if (vap_info->u.bss_info.mac_filter_mode == wifi_mac_filter_mode_black_list){
18664 acl_mode = 2;
developere40952c2023-06-15 18:46:43 +080018665 res = snprintf(cmd, sizeof(cmd), "touch %s%d", DENY_PREFIX, vap_info->vap_index);
18666 if (os_snprintf_error(sizeof(cmd), res)) {
18667 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18668 return RETURN_ERR;
18669 }
developera3511852023-06-14 14:12:59 +080018670 _syscmd(cmd, buf, sizeof(buf));
18671 }else{
18672 acl_mode = 1;
18673 }
18674 }
developer72fb0bb2023-01-11 09:46:29 +080018675
developera3511852023-06-14 14:12:59 +080018676 ret = wifi_setApWmmEnable(vap_info->vap_index, vap_info->u.bss_info.wmm_enabled);
18677 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018678 wifi_debug(DEBUG_ERROR, "wifi_setApWmmEnable return error\n");
developera3511852023-06-14 14:12:59 +080018679 return RETURN_ERR;
18680 }
developer72fb0bb2023-01-11 09:46:29 +080018681
developera3511852023-06-14 14:12:59 +080018682 ret = wifi_setApWmmUapsdEnable(vap_info->vap_index, vap_info->u.bss_info.UAPSDEnabled);
18683 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018684 wifi_debug(DEBUG_ERROR, "wifi_setApWmmUapsdEnable return error\n");
developera3511852023-06-14 14:12:59 +080018685 return RETURN_ERR;
18686 }
developer72fb0bb2023-01-11 09:46:29 +080018687
developera3511852023-06-14 14:12:59 +080018688 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080018689 beaconRate_enum_to_string(vap_info->u.bss_info.beaconRate, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080018690 ret = wifi_setApBeaconRate(vap_info->radio_index, buf);
18691 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018692 wifi_debug(DEBUG_ERROR, "wifi_setApBeaconRate return error\n");
developera3511852023-06-14 14:12:59 +080018693 return RETURN_ERR;
18694 }
developer72fb0bb2023-01-11 09:46:29 +080018695
developera3511852023-06-14 14:12:59 +080018696 ret = wifi_setRadioIGMPSnoopingEnable(vap_info->radio_index, vap_info->u.bss_info.mcast2ucast);
18697 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018698 wifi_debug(DEBUG_ERROR, "wifi_setRadioIGMPSnoopingEnable\n");
developera3511852023-06-14 14:12:59 +080018699 return RETURN_ERR;
18700 }
developer72fb0bb2023-01-11 09:46:29 +080018701
developera3511852023-06-14 14:12:59 +080018702 ret = wifi_setApSecurity(vap_info->vap_index, &vap_info->u.bss_info.security);
18703 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018704 wifi_debug(DEBUG_ERROR, "wifi_setApSecurity return error\n");
developera3511852023-06-14 14:12:59 +080018705 return RETURN_ERR;
18706 }
developer333c1eb2023-05-31 14:59:39 +080018707
developer56fbedb2023-05-30 16:47:05 +080018708 hostapd_raw_restart_bss(vap_info->vap_index);
developer72fb0bb2023-01-11 09:46:29 +080018709
developera3511852023-06-14 14:12:59 +080018710 multiple_set = FALSE;
developer23e71282023-01-18 10:25:19 +080018711
developera3511852023-06-14 14:12:59 +080018712 // If config use hostapd_cli to set, we calling these type of functions after enable the ap.
18713 ret = wifi_setApMacAddressControlMode(vap_info->vap_index, acl_mode);
18714 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018715 wifi_debug(DEBUG_ERROR, "wifi_setApMacAddressControlMode return error\n");
developera3511852023-06-14 14:12:59 +080018716 return RETURN_ERR;
18717 }
developer72fb0bb2023-01-11 09:46:29 +080018718
developera3511852023-06-14 14:12:59 +080018719 // TODO mgmtPowerControl, interworking, wps
18720 }
18721 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18722 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018723}
18724
18725int parse_channel_list_int_arr(char *pchannels, wifi_channels_list_t* chlistptr)
18726{
developera3511852023-06-14 14:12:59 +080018727 char *token, *next;
18728 const char s[2] = ",";
18729 int count =0;
developer72fb0bb2023-01-11 09:46:29 +080018730
developera3511852023-06-14 14:12:59 +080018731 /* get the first token */
18732 token = strtok_r(pchannels, s, &next);
developer72fb0bb2023-01-11 09:46:29 +080018733
developera3511852023-06-14 14:12:59 +080018734 /* walk through other tokens */
18735 while( token != NULL && count < MAX_CHANNELS) {
18736 chlistptr->channels_list[count++] = atoi(token);
18737 token = strtok_r(NULL, s, &next);
18738 }
developer72fb0bb2023-01-11 09:46:29 +080018739
developera3511852023-06-14 14:12:59 +080018740 return count;
developer72fb0bb2023-01-11 09:46:29 +080018741}
18742
18743static int getRadioCapabilities(int radioIndex, wifi_radio_capabilities_t *rcap)
18744{
developera3511852023-06-14 14:12:59 +080018745 INT status;
18746 wifi_channels_list_t *chlistp;
18747 CHAR output_string[64];
18748 CHAR pchannels[128];
18749 CHAR interface_name[16] = {0};
18750 wifi_band band;
developere40952c2023-06-15 18:46:43 +080018751 int res;
developer72fb0bb2023-01-11 09:46:29 +080018752
developera3511852023-06-14 14:12:59 +080018753 if(rcap == NULL)
18754 {
18755 return RETURN_ERR;
18756 }
developer72fb0bb2023-01-11 09:46:29 +080018757
developera3511852023-06-14 14:12:59 +080018758 rcap->numSupportedFreqBand = 1;
18759 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080018760
developera3511852023-06-14 14:12:59 +080018761 if (band == band_2_4)
18762 rcap->band[0] = WIFI_FREQUENCY_2_4_BAND;
18763 else if (band == band_5)
18764 rcap->band[0] = WIFI_FREQUENCY_5_BAND;
18765 else if (band == band_6)
18766 rcap->band[0] = WIFI_FREQUENCY_6_BAND;
developer72fb0bb2023-01-11 09:46:29 +080018767
developera3511852023-06-14 14:12:59 +080018768 chlistp = &(rcap->channel_list[0]);
18769 memset(pchannels, 0, sizeof(pchannels));
developer72fb0bb2023-01-11 09:46:29 +080018770
developera3511852023-06-14 14:12:59 +080018771 /* possible number of radio channels */
18772 status = wifi_getRadioPossibleChannels(radioIndex, pchannels);
18773 {
18774 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, pchannels);
18775 }
18776 /* Number of channels and list*/
18777 chlistp->num_channels = parse_channel_list_int_arr(pchannels, chlistp);
developer72fb0bb2023-01-11 09:46:29 +080018778
developera3511852023-06-14 14:12:59 +080018779 /* autoChannelSupported */
18780 /* always ON with wifi_getRadioAutoChannelSupported */
18781 rcap->autoChannelSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018782
developera3511852023-06-14 14:12:59 +080018783 /* DCSSupported */
18784 /* always ON with wifi_getRadioDCSSupported */
18785 rcap->DCSSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018786
developera3511852023-06-14 14:12:59 +080018787 /* zeroDFSSupported - TBD */
18788 rcap->zeroDFSSupported = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018789
developera3511852023-06-14 14:12:59 +080018790 /* Supported Country List*/
18791 memset(output_string, 0, sizeof(output_string));
18792 status = wifi_getRadioCountryCode(radioIndex, output_string);
18793 if( status != 0 ) {
18794 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, output_string);
18795 return RETURN_ERR;
18796 } else {
18797 printf("[wifi_hal dbg] : func[%s] line[%d], output [%s]\n", __FUNCTION__, __LINE__, output_string);
18798 }
18799 if(!strcmp(output_string,"US")){
18800 rcap->countrySupported[0] = wifi_countrycode_US;
18801 rcap->countrySupported[1] = wifi_countrycode_CA;
18802 } else if (!strcmp(output_string,"CA")) {
18803 rcap->countrySupported[0] = wifi_countrycode_CA;
18804 rcap->countrySupported[1] = wifi_countrycode_US;
18805 } else {
18806 printf("[wifi_hal dbg] : func[%s] line[%d] radio_index[%d] Invalid Country [%s]\n", __FUNCTION__, __LINE__, radioIndex, output_string);
18807 }
developer72fb0bb2023-01-11 09:46:29 +080018808
developera3511852023-06-14 14:12:59 +080018809 rcap->numcountrySupported = 2;
developer72fb0bb2023-01-11 09:46:29 +080018810
developera3511852023-06-14 14:12:59 +080018811 /* csi */
18812 rcap->csi.maxDevices = 8;
18813 rcap->csi.soudingFrameSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018814
developer86035662023-06-28 19:21:12 +080018815 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
18816 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
18817 }
developere40952c2023-06-15 18:46:43 +080018818 res = snprintf(rcap->ifaceName, sizeof(interface_name), "%s",interface_name);
18819 if (os_snprintf_error(sizeof(interface_name), res)) {
18820 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18821 return RETURN_ERR;
18822 }
developer72fb0bb2023-01-11 09:46:29 +080018823
developera3511852023-06-14 14:12:59 +080018824 /* channelWidth - all supported bandwidths */
18825 int i=0;
18826 rcap->channelWidth[i] = 0;
18827 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18828 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18829 WIFI_CHANNELBANDWIDTH_40MHZ);
developer72fb0bb2023-01-11 09:46:29 +080018830
developera3511852023-06-14 14:12:59 +080018831 }
18832 else if (rcap->band[i] & (WIFI_FREQUENCY_5_BAND ) || rcap->band[i] & (WIFI_FREQUENCY_6_BAND)) {
18833 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18834 WIFI_CHANNELBANDWIDTH_40MHZ |
18835 WIFI_CHANNELBANDWIDTH_80MHZ | WIFI_CHANNELBANDWIDTH_160MHZ);
18836 }
developer72fb0bb2023-01-11 09:46:29 +080018837
18838
developera3511852023-06-14 14:12:59 +080018839 /* mode - all supported variants */
18840 // rcap->mode[i] = WIFI_80211_VARIANT_H;
18841 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) {
18842 rcap->mode[i] = ( WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX );
18843 }
18844 else if (rcap->band[i] & WIFI_FREQUENCY_5_BAND ) {
18845 rcap->mode[i] = ( WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX );
18846 }
18847 else if (rcap->band[i] & WIFI_FREQUENCY_6_BAND) {
18848 rcap->mode[i] = ( WIFI_80211_VARIANT_AX );
18849 }
18850 rcap->maxBitRate[i] = ( rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) ? 300 :
18851 ((rcap->band[i] & WIFI_FREQUENCY_5_BAND) ? 1734 : 0);
developer72fb0bb2023-01-11 09:46:29 +080018852
developera3511852023-06-14 14:12:59 +080018853 /* supportedBitRate - all supported bitrates */
18854 rcap->supportedBitRate[i] = 0;
18855 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18856 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18857 WIFI_BITRATE_11MBPS | WIFI_BITRATE_12MBPS);
18858 }
18859 else if ((rcap->band[i] & (WIFI_FREQUENCY_5_BAND )) || (rcap->band[i] & (WIFI_FREQUENCY_6_BAND))) {
18860 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18861 WIFI_BITRATE_12MBPS | WIFI_BITRATE_18MBPS | WIFI_BITRATE_24MBPS |
18862 WIFI_BITRATE_36MBPS | WIFI_BITRATE_48MBPS | WIFI_BITRATE_54MBPS);
18863 }
developer72fb0bb2023-01-11 09:46:29 +080018864
18865
developera3511852023-06-14 14:12:59 +080018866 rcap->transmitPowerSupported_list[i].numberOfElements = 5;
18867 rcap->transmitPowerSupported_list[i].transmitPowerSupported[0]=12;
18868 rcap->transmitPowerSupported_list[i].transmitPowerSupported[1]=25;
18869 rcap->transmitPowerSupported_list[i].transmitPowerSupported[2]=50;
18870 rcap->transmitPowerSupported_list[i].transmitPowerSupported[3]=75;
18871 rcap->transmitPowerSupported_list[i].transmitPowerSupported[4]=100;
18872 rcap->cipherSupported = 0;
18873 rcap->cipherSupported |= WIFI_CIPHER_CAPA_ENC_TKIP | WIFI_CIPHER_CAPA_ENC_CCMP;
18874 rcap->maxNumberVAPs = MAX_NUM_VAP_PER_RADIO;
developer72fb0bb2023-01-11 09:46:29 +080018875
developera3511852023-06-14 14:12:59 +080018876 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018877}
18878
18879INT wifi_getHalCapability(wifi_hal_capability_t *cap)
18880{
developera3511852023-06-14 14:12:59 +080018881 INT status = 0, radioIndex = 0;
18882 char output[MAX_BUF_SIZE] = {0};
18883 int iter = 0;
18884 unsigned int j = 0;
developer9ce44382023-06-28 11:09:37 +080018885 int max_num_radios = 0;
developera3511852023-06-14 14:12:59 +080018886 wifi_interface_name_idex_map_t *iface_info = NULL;
developer72fb0bb2023-01-11 09:46:29 +080018887
developera3511852023-06-14 14:12:59 +080018888 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018889
developera3511852023-06-14 14:12:59 +080018890 memset(cap, 0, sizeof(wifi_hal_capability_t));
developer72fb0bb2023-01-11 09:46:29 +080018891
developera3511852023-06-14 14:12:59 +080018892 /* version */
18893 cap->version.major = WIFI_HAL_MAJOR_VERSION;
18894 cap->version.minor = WIFI_HAL_MINOR_VERSION;
developer72fb0bb2023-01-11 09:46:29 +080018895
developera3511852023-06-14 14:12:59 +080018896 /* number of radios platform property */
18897 wifi_getMaxRadioNumber(&max_num_radios);
18898 cap->wifi_prop.numRadios = max_num_radios;
developer72fb0bb2023-01-11 09:46:29 +080018899
developera3511852023-06-14 14:12:59 +080018900 for(radioIndex=0; radioIndex < cap->wifi_prop.numRadios; radioIndex++)
18901 {
18902 status = getRadioCapabilities(radioIndex, &(cap->wifi_prop.radiocap[radioIndex]));
18903 if (status != 0) {
18904 printf("%s: getRadioCapabilities idx = %d\n", __FUNCTION__, radioIndex);
18905 return RETURN_ERR;
18906 }
developer72fb0bb2023-01-11 09:46:29 +080018907
developera3511852023-06-14 14:12:59 +080018908 for (j = 0; j < cap->wifi_prop.radiocap[radioIndex].maxNumberVAPs; j++)
18909 {
18910 if (iter >= MAX_NUM_RADIOS * MAX_NUM_VAP_PER_RADIO)
18911 {
18912 printf("%s: to many vaps for index map (%d)\n", __func__, iter);
18913 return RETURN_ERR;
18914 }
18915 iface_info = &cap->wifi_prop.interface_map[iter];
18916 iface_info->phy_index = radioIndex; // XXX: parse phyX index instead
18917 iface_info->rdk_radio_index = radioIndex;
18918 memset(output, 0, sizeof(output));
18919 if (wifi_getRadioIfName(radioIndex, output) == RETURN_OK)
18920 {
18921 strncpy(iface_info->interface_name, output, sizeof(iface_info->interface_name) - 1);
18922 }
18923 // TODO: bridge name
18924 // TODO: vlan id
18925 // TODO: primary
18926 iface_info->index = array_index_to_vap_index(radioIndex, j);
18927 memset(output, 0, sizeof(output));
18928 if (wifi_getApName(iface_info->index, output) == RETURN_OK)
18929 {
18930 strncpy(iface_info->vap_name, output, sizeof(iface_info->vap_name) - 1);
18931 }
18932 iter++;
18933 }
18934 }
developer72fb0bb2023-01-11 09:46:29 +080018935
developera3511852023-06-14 14:12:59 +080018936 cap->BandSteeringSupported = FALSE;
18937 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18938 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018939}
18940
18941INT wifi_setOpportunisticKeyCaching(int ap_index, BOOL okc_enable)
18942{
developera3511852023-06-14 14:12:59 +080018943 struct params h_config={0};
18944 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018945 int res;
developer72fb0bb2023-01-11 09:46:29 +080018946
developera3511852023-06-14 14:12:59 +080018947 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018948
developera3511852023-06-14 14:12:59 +080018949 h_config.name = "okc";
18950 h_config.value = okc_enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018951
developere40952c2023-06-15 18:46:43 +080018952 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18953 if (os_snprintf_error(sizeof(config_file), res)) {
18954 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18955 return RETURN_ERR;
18956 }
developera3511852023-06-14 14:12:59 +080018957 wifi_hostapdWrite(config_file, &h_config, 1);
18958 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018959
developera3511852023-06-14 14:12:59 +080018960 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18961 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018962}
18963
18964INT wifi_setSAEMFP(int ap_index, BOOL enable)
18965{
developera3511852023-06-14 14:12:59 +080018966 struct params h_config={0};
18967 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018968 int res;
developer72fb0bb2023-01-11 09:46:29 +080018969
developera3511852023-06-14 14:12:59 +080018970 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018971
developera3511852023-06-14 14:12:59 +080018972 h_config.name = "sae_require_mfp";
18973 h_config.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018974
developere40952c2023-06-15 18:46:43 +080018975 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18976 if (os_snprintf_error(sizeof(config_file), res)) {
18977 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18978 return RETURN_ERR;
18979 }
developera3511852023-06-14 14:12:59 +080018980 wifi_hostapdWrite(config_file, &h_config, 1);
18981 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018982
developera3511852023-06-14 14:12:59 +080018983 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18984 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018985}
18986
18987INT wifi_setSAEpwe(int ap_index, int sae_pwe)
18988{
developera3511852023-06-14 14:12:59 +080018989 struct params h_config={0};
18990 char config_file[64] = {0};
18991 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080018992 int res;
developer72fb0bb2023-01-11 09:46:29 +080018993
developera3511852023-06-14 14:12:59 +080018994 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018995
developera3511852023-06-14 14:12:59 +080018996 h_config.name = "sae_pwe";
developere40952c2023-06-15 18:46:43 +080018997 res = snprintf(buf, sizeof(buf), "%d", sae_pwe);
18998 if (os_snprintf_error(sizeof(buf), res)) {
18999 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19000 return RETURN_ERR;
19001 }
19002
developera3511852023-06-14 14:12:59 +080019003 h_config.value = buf;
developer72fb0bb2023-01-11 09:46:29 +080019004
developere40952c2023-06-15 18:46:43 +080019005 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19006 if (os_snprintf_error(sizeof(config_file), res)) {
19007 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19008 return RETURN_ERR;
19009 }
developera3511852023-06-14 14:12:59 +080019010 wifi_hostapdWrite(config_file, &h_config, 1);
19011 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019012
developera3511852023-06-14 14:12:59 +080019013 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19014 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019015}
19016
19017INT wifi_setDisable_EAPOL_retries(int ap_index, BOOL disable_EAPOL_retries)
19018{
developera3511852023-06-14 14:12:59 +080019019 // wpa3 use SAE instead of PSK, so we need to disable this feature when using wpa3.
19020 struct params h_config={0};
19021 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080019022 int res;
developer72fb0bb2023-01-11 09:46:29 +080019023
developera3511852023-06-14 14:12:59 +080019024 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019025
developera3511852023-06-14 14:12:59 +080019026 h_config.name = "wpa_disable_eapol_key_retries";
19027 h_config.value = disable_EAPOL_retries?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080019028
developere40952c2023-06-15 18:46:43 +080019029 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19030 if (os_snprintf_error(sizeof(config_file), res)) {
19031 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19032 return RETURN_ERR;
19033 }
developera3511852023-06-14 14:12:59 +080019034 wifi_hostapdWrite(config_file, &h_config, 1);
19035 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080019036
developera3511852023-06-14 14:12:59 +080019037 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
19038 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019039}
19040
19041INT wifi_setApSecurity(INT ap_index, wifi_vap_security_t *security)
19042{
developera3511852023-06-14 14:12:59 +080019043 char buf[128] = {0};
19044 char config_file[128] = {0};
19045 char cmd[MAX_CMD_SIZE] = {0};
19046 char password[65] = {0};
19047 char mfp[32] = {0};
19048 char wpa_mode[32] = {0};
19049 BOOL okc_enable = FALSE;
19050 BOOL sae_MFP = FALSE;
19051 BOOL disable_EAPOL_retries = TRUE;
19052 int sae_pwe = 0;
19053 struct params params = {0};
19054 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080019055 int res;
developer72fb0bb2023-01-11 09:46:29 +080019056
developera3511852023-06-14 14:12:59 +080019057 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019058
developera3511852023-06-14 14:12:59 +080019059 multiple_set = TRUE;
developer37646972023-06-29 10:58:43 +080019060 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19061 if (os_snprintf_error(sizeof(config_file), res)) {
19062 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19063 return RETURN_ERR;
19064 }
developera3511852023-06-14 14:12:59 +080019065 if (security->mode == wifi_security_mode_none) {
developer9ce44382023-06-28 11:09:37 +080019066 strncpy(wpa_mode, "None",sizeof(wpa_mode) - 1);
19067 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19068 } else if (security->mode == wifi_security_mode_wpa_personal) {
19069 strncpy(wpa_mode, "WPA-Personal",sizeof(wpa_mode) - 1);
19070 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19071 } else if (security->mode == wifi_security_mode_wpa2_personal){
19072 strncpy(wpa_mode, "WPA2-Personal",sizeof(wpa_mode) - 1);
19073 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19074 } else if (security->mode == wifi_security_mode_wpa_wpa2_personal){
19075 strncpy(wpa_mode, "WPA-WPA2-Personal",sizeof(wpa_mode) - 1);
19076 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19077 } else if (security->mode == wifi_security_mode_wpa_enterprise){
19078 strncpy(wpa_mode, "WPA-Enterprise",sizeof(wpa_mode) - 1);
19079 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19080 } else if (security->mode == wifi_security_mode_wpa2_enterprise){
19081 strncpy(wpa_mode, "WPA2-Enterprise",sizeof(wpa_mode) - 1);
19082 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19083 } else if (security->mode == wifi_security_mode_wpa_wpa2_enterprise){
19084 strncpy(wpa_mode, "WPA-WAP2-Enterprise",sizeof(wpa_mode) - 1);
19085 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
19086 } else if (security->mode == wifi_security_mode_wpa3_personal) {
19087 strncpy(wpa_mode, "WPA3-Personal",sizeof(wpa_mode) - 1);
19088 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019089 okc_enable = TRUE;
19090 sae_MFP = TRUE;
19091 sae_pwe = 2;
19092 disable_EAPOL_retries = FALSE;
19093 } else if (security->mode == wifi_security_mode_wpa3_transition) {
developer9ce44382023-06-28 11:09:37 +080019094 strncpy(wpa_mode, "WPA3-Personal-Transition",sizeof(wpa_mode) - 1);
19095 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019096 okc_enable = TRUE;
19097 sae_MFP = TRUE;
19098 sae_pwe = 2;
19099 disable_EAPOL_retries = FALSE;
19100 } else if (security->mode == wifi_security_mode_wpa3_enterprise) {
developer9ce44382023-06-28 11:09:37 +080019101 strncpy(wpa_mode, "WPA3-Enterprise",sizeof(wpa_mode) - 1);
19102 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019103 sae_MFP = TRUE;
19104 sae_pwe = 2;
19105 disable_EAPOL_retries = FALSE;
19106 } else if (security->mode == wifi_security_mode_enhanced_open) {
developer9ce44382023-06-28 11:09:37 +080019107 strncpy(wpa_mode, "OWE",sizeof(wpa_mode) - 1);
19108 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019109 sae_MFP = TRUE;
19110 sae_pwe = 2;
19111 disable_EAPOL_retries = FALSE;
19112 }
developer72fb0bb2023-01-11 09:46:29 +080019113
developera3511852023-06-14 14:12:59 +080019114 band = wifi_index_to_band(ap_index);
19115 if (band == band_6 && strstr(wpa_mode, "WPA3") == NULL) {
developerc14d83a2023-06-29 20:09:42 +080019116 wifi_debug(DEBUG_ERROR, "%s: 6G band must set with wpa3.\n", __func__);
developera3511852023-06-14 14:12:59 +080019117 return RETURN_ERR;
19118 }
developer72fb0bb2023-01-11 09:46:29 +080019119
developera3511852023-06-14 14:12:59 +080019120 wifi_setApSecurityModeEnabled(ap_index, wpa_mode);
19121 wifi_setOpportunisticKeyCaching(ap_index, okc_enable);
19122 wifi_setSAEMFP(ap_index, sae_MFP);
19123 wifi_setSAEpwe(ap_index, sae_pwe);
19124 wifi_setDisable_EAPOL_retries(ap_index, disable_EAPOL_retries);
developer72fb0bb2023-01-11 09:46:29 +080019125
developera3511852023-06-14 14:12:59 +080019126 if (security->mode != wifi_security_mode_none && security->mode != wifi_security_mode_enhanced_open) {
19127 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) {
19128 int key_len = strlen(security->u.key.key);
19129 // wpa_psk and wpa_passphrase cann;t use at the same time, the command replace one with the other.
19130 if (key_len == 64) { // set wpa_psk
19131 strncpy(password, security->u.key.key, 64); // 64 characters
19132 password[64] = '\0';
19133 wifi_setApSecurityPreSharedKey(ap_index, password);
developere40952c2023-06-15 18:46:43 +080019134 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^wpa_passphrase=/!p' %s", config_file);
19135 if (os_snprintf_error(sizeof(cmd), res)) {
19136 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19137 return RETURN_ERR;
19138 }
developera3511852023-06-14 14:12:59 +080019139 } else if (key_len >= 8 && key_len < 64) { // set wpa_passphrase
19140 strncpy(password, security->u.key.key, 63);
19141 password[63] = '\0';
19142 wifi_setApSecurityKeyPassphrase(ap_index, password);
developere40952c2023-06-15 18:46:43 +080019143 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^wpa_psk=/!p' %s", config_file);
developer9ce44382023-06-28 11:09:37 +080019144 if (os_snprintf_error(sizeof(cmd), res)) {
19145 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19146 return RETURN_ERR;
19147 }
developera3511852023-06-14 14:12:59 +080019148 } else
19149 return RETURN_ERR;
19150 _syscmd(cmd, buf, sizeof(buf));
19151 }
19152 if (security->u.key.type == wifi_security_key_type_sae || security->u.key.type == wifi_security_key_type_psk_sae) {
19153 params.name = "sae_password";
19154 params.value = security->u.key.key;
19155 wifi_hostapdWrite(config_file, &params, 1);
19156 } else { // remove sae_password
developere40952c2023-06-15 18:46:43 +080019157 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^sae_password=/!p' %s", config_file);
19158 if (os_snprintf_error(sizeof(cmd), res)) {
19159 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19160 return RETURN_ERR;
19161 }
developera3511852023-06-14 14:12:59 +080019162 _syscmd(cmd, buf, sizeof(buf));
19163 }
19164 }
developer72fb0bb2023-01-11 09:46:29 +080019165
developera3511852023-06-14 14:12:59 +080019166 if (security->mode != wifi_security_mode_none) {
19167 memset(&params, 0, sizeof(params));
19168 params.name = "wpa_pairwise";
19169 if (security->encr == wifi_encryption_tkip)
19170 params.value = "TKIP";
19171 else if (security->encr == wifi_encryption_aes)
19172 params.value = "CCMP";
19173 else if (security->encr == wifi_encryption_aes_tkip)
19174 params.value = "TKIP CCMP";
19175 wifi_hostapdWrite(config_file, &params, 1);
19176 }
developer72fb0bb2023-01-11 09:46:29 +080019177
developer9ce44382023-06-28 11:09:37 +080019178 if (security->mfp == wifi_mfp_cfg_disabled){
19179 strncpy(mfp,"Disabled",sizeof(mfp)-1);
19180 mfp[sizeof(mfp)-1] = '\0';
19181 } else if (security->mfp == wifi_mfp_cfg_optional){
19182 strncpy(mfp,"Optional",sizeof(mfp)-1);
19183 mfp[sizeof(mfp)-1] = '\0';
19184 } else if (security->mfp == wifi_mfp_cfg_required){
19185 strncpy(mfp,"Required",sizeof(mfp)-1);
19186 mfp[sizeof(mfp)-1] = '\0';
19187 }
developera3511852023-06-14 14:12:59 +080019188 wifi_setApSecurityMFPConfig(ap_index, mfp);
developer72fb0bb2023-01-11 09:46:29 +080019189
developera3511852023-06-14 14:12:59 +080019190 memset(&params, 0, sizeof(params));
19191 params.name = "transition_disable";
19192 if (security->wpa3_transition_disable == TRUE)
19193 params.value = "0x01";
19194 else
19195 params.value = "0x00";
19196 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019197
developera3511852023-06-14 14:12:59 +080019198 memset(&params, 0, sizeof(params));
19199 params.name = "wpa_group_rekey";
developere40952c2023-06-15 18:46:43 +080019200 res = snprintf(buf, sizeof(buf), "%d", security->rekey_interval);
19201 if (os_snprintf_error(sizeof(buf), res)) {
19202 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19203 return RETURN_ERR;
19204 }
developera3511852023-06-14 14:12:59 +080019205 params.value = buf;
19206 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019207
developera3511852023-06-14 14:12:59 +080019208 memset(&params, 0, sizeof(params));
19209 params.name = "wpa_strict_rekey";
19210 params.value = security->strict_rekey?"1":"0";
19211 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019212
developera3511852023-06-14 14:12:59 +080019213 memset(&params, 0, sizeof(params));
19214 params.name = "wpa_pairwise_update_count";
19215 if (security->eapol_key_retries == 0)
19216 security->eapol_key_retries = 4; // 0 is invalid, set to default value.
developere40952c2023-06-15 18:46:43 +080019217 res = snprintf(buf, sizeof(buf), "%u", security->eapol_key_retries);
19218 if (os_snprintf_error(sizeof(buf), res)) {
19219 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19220 return RETURN_ERR;
19221 }
developera3511852023-06-14 14:12:59 +080019222 params.value = buf;
19223 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019224
developera3511852023-06-14 14:12:59 +080019225 memset(&params, 0, sizeof(params));
19226 params.name = "disable_pmksa_caching";
19227 params.value = security->disable_pmksa_caching?"1":"0";
19228 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019229
developera3511852023-06-14 14:12:59 +080019230 if (multiple_set == FALSE) {
19231 wifi_setApEnable(ap_index, FALSE);
19232 wifi_setApEnable(ap_index, TRUE);
19233 }
developer72fb0bb2023-01-11 09:46:29 +080019234
developera3511852023-06-14 14:12:59 +080019235 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019236
developera3511852023-06-14 14:12:59 +080019237 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019238}
19239
19240INT wifi_getApSecurity(INT ap_index, wifi_vap_security_t *security)
19241{
developera3511852023-06-14 14:12:59 +080019242 char buf[256] = {0};
19243 char config_file[128] = {0};
developer86035662023-06-28 19:21:12 +080019244 long int disable = 0;
19245 long int tmp;
developera3511852023-06-14 14:12:59 +080019246 bool set_sae = FALSE;
developere75ba632023-06-29 16:03:33 +080019247 int res;
developer72fb0bb2023-01-11 09:46:29 +080019248
developera3511852023-06-14 14:12:59 +080019249 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere75ba632023-06-29 16:03:33 +080019250 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19251 if (os_snprintf_error(sizeof(config_file), res)) {
19252 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19253 return RETURN_ERR;
19254 }
developera3511852023-06-14 14:12:59 +080019255 wifi_getApSecurityModeEnabled(ap_index, buf); // Get wpa config
19256 security->mode = wifi_security_mode_none;
19257 if (strlen(buf) != 0) {
19258 if (!strcmp(buf, "WPA-Personal"))
19259 security->mode = wifi_security_mode_wpa_personal;
19260 else if (!strcmp(buf, "WPA2-Personal"))
19261 security->mode = wifi_security_mode_wpa2_personal;
19262 else if (!strcmp(buf, "WPA-WPA2-Personal"))
19263 security->mode = wifi_security_mode_wpa_wpa2_personal;
19264 else if (!strcmp(buf, "WPA-Enterprise"))
19265 security->mode = wifi_security_mode_wpa_enterprise;
19266 else if (!strcmp(buf, "WPA2-Enterprise"))
19267 security->mode = wifi_security_mode_wpa2_enterprise;
19268 else if (!strcmp(buf, "WPA-WPA2-Enterprise"))
19269 security->mode = wifi_security_mode_wpa_wpa2_enterprise;
19270 else if (!strcmp(buf, "WPA3-Personal"))
19271 security->mode = wifi_security_mode_wpa3_personal;
19272 else if (!strcmp(buf, "WPA3-Personal-Transition"))
19273 security->mode = wifi_security_mode_wpa3_transition;
19274 else if (!strcmp(buf, "WPA3-Enterprise"))
19275 security->mode = wifi_security_mode_wpa3_enterprise;
19276 else if (!strcmp(buf, "OWE"))
19277 security->mode = wifi_security_mode_enhanced_open;
19278 }
developer72fb0bb2023-01-11 09:46:29 +080019279
developera3511852023-06-14 14:12:59 +080019280 wifi_hostapdRead(config_file,"wpa_pairwise",buf,sizeof(buf));
19281 if (security->mode == wifi_security_mode_none)
19282 security->encr = wifi_encryption_none;
19283 else {
19284 if (strcmp(buf, "TKIP") == 0)
19285 security->encr = wifi_encryption_tkip;
19286 else if (strcmp(buf, "CCMP") == 0)
19287 security->encr = wifi_encryption_aes;
19288 else
19289 security->encr = wifi_encryption_aes_tkip;
19290 }
developer72fb0bb2023-01-11 09:46:29 +080019291
developera3511852023-06-14 14:12:59 +080019292 if (security->mode != wifi_security_mode_none) {
19293 memset(buf, 0, sizeof(buf));
19294 // wpa3 can use one or both configs as password, so we check sae_password first.
19295 wifi_hostapdRead(config_file, "sae_password", buf, sizeof(buf));
19296 if (strlen(buf) != 0) {
19297 if (security->mode == wifi_security_mode_wpa3_personal || security->mode == wifi_security_mode_wpa3_transition)
19298 security->u.key.type = wifi_security_key_type_sae;
19299 set_sae = TRUE;
19300 strncpy(security->u.key.key, buf, sizeof(buf));
19301 }
19302 wifi_hostapdRead(config_file, "wpa_passphrase", buf, sizeof(buf));
19303 if (strlen(buf) != 0){
19304 if (set_sae == TRUE)
19305 security->u.key.type = wifi_security_key_type_psk_sae;
19306 else if (strlen(buf) == 64)
19307 security->u.key.type = wifi_security_key_type_psk;
19308 else
19309 security->u.key.type = wifi_security_key_type_pass;
19310 strncpy(security->u.key.key, buf, sizeof(security->u.key.key));
19311 }
19312 security->u.key.key[255] = '\0';
19313 }
developer72fb0bb2023-01-11 09:46:29 +080019314
developera3511852023-06-14 14:12:59 +080019315 memset(buf, 0, sizeof(buf));
19316 wifi_getApSecurityMFPConfig(ap_index, buf);
19317 if (strcmp(buf, "Disabled") == 0)
19318 security->mfp = wifi_mfp_cfg_disabled;
19319 else if (strcmp(buf, "Optional") == 0)
19320 security->mfp = wifi_mfp_cfg_optional;
19321 else if (strcmp(buf, "Required") == 0)
19322 security->mfp = wifi_mfp_cfg_required;
developer72fb0bb2023-01-11 09:46:29 +080019323
developera3511852023-06-14 14:12:59 +080019324 memset(buf, 0, sizeof(buf));
19325 security->wpa3_transition_disable = FALSE;
19326 wifi_hostapdRead(config_file, "transition_disable", buf, sizeof(buf));
developer3de255a2023-06-29 10:35:45 +080019327 if (strlen(buf) == 0)
19328 disable = 0;
19329 else {
19330 if (hal_strtol(buf, 16, &disable) < 0) {
19331 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19332 return RETURN_ERR;
19333 }
developer86035662023-06-28 19:21:12 +080019334 }
developera3511852023-06-14 14:12:59 +080019335 if (disable != 0)
19336 security->wpa3_transition_disable = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080019337
developera3511852023-06-14 14:12:59 +080019338 memset(buf, 0, sizeof(buf));
19339 wifi_hostapdRead(config_file, "wpa_group_rekey", buf, sizeof(buf));
19340 if (strlen(buf) == 0)
19341 security->rekey_interval = 86400;
developer86035662023-06-28 19:21:12 +080019342 else {
19343 if (hal_strtol(buf, 10, &tmp) < 0) {
19344 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19345 return RETURN_ERR;
19346 }
19347 security->rekey_interval = tmp;
19348 }
developer72fb0bb2023-01-11 09:46:29 +080019349
developera3511852023-06-14 14:12:59 +080019350 memset(buf, 0, sizeof(buf));
19351 wifi_hostapdRead(config_file, "wpa_strict_rekey", buf, sizeof(buf));
19352 if (strlen(buf) == 0)
19353 security->strict_rekey = 1;
developer86035662023-06-28 19:21:12 +080019354 else {
19355 if (hal_strtol(buf, 10, &tmp) < 0) {
19356 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19357 return RETURN_ERR;
19358 }
19359 security->strict_rekey = tmp;
19360 }
developer72fb0bb2023-01-11 09:46:29 +080019361
developera3511852023-06-14 14:12:59 +080019362 memset(buf, 0, sizeof(buf));
19363 wifi_hostapdRead(config_file, "wpa_pairwise_update_count", buf, sizeof(buf));
19364 if (strlen(buf) == 0)
19365 security->eapol_key_retries = 4;
developer86035662023-06-28 19:21:12 +080019366 else {
19367 if (hal_strtol(buf, 10, &tmp) < 0) {
19368 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19369 return RETURN_ERR;
19370 }
19371 security->eapol_key_retries = tmp;
19372 }
developer72fb0bb2023-01-11 09:46:29 +080019373
developera3511852023-06-14 14:12:59 +080019374 memset(buf, 0, sizeof(buf));
19375 wifi_hostapdRead(config_file, "disable_pmksa_caching", buf, sizeof(buf));
19376 if (strlen(buf) == 0)
19377 security->disable_pmksa_caching = FALSE;
developer86035662023-06-28 19:21:12 +080019378 else {
19379 if (hal_strtol(buf, 10, &(tmp)) < 0) {
19380 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19381 return RETURN_ERR;
19382 }
19383 security->disable_pmksa_caching = tmp ? TRUE : FALSE;
19384 }
developera3511852023-06-14 14:12:59 +080019385 /* TODO
19386 eapol_key_timeout, eap_identity_req_timeout, eap_identity_req_retries, eap_req_timeout, eap_req_retries
19387 */
19388 security->eapol_key_timeout = 1000; // Unit is ms. The default value in protocol.
19389 security->eap_identity_req_timeout = 0;
19390 security->eap_identity_req_retries = 0;
19391 security->eap_req_timeout = 0;
19392 security->eap_req_retries = 0;
19393 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19394 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019395}
19396
19397#endif /* WIFI_HAL_VERSION_3 */
19398
19399#ifdef WIFI_HAL_VERSION_3_PHASE2
19400INT wifi_getApAssociatedDevice(INT ap_index, mac_address_t *output_deviceMacAddressArray, UINT maxNumDevices, UINT *output_numDevices)
19401{
developera3511852023-06-14 14:12:59 +080019402 char interface_name[16] = {0};
19403 char cmd[128] = {0};
19404 char buf[128] = {0};
19405 char *mac_addr = NULL;
19406 BOOL status = FALSE;
19407 size_t len = 0;
developer72fb0bb2023-01-11 09:46:29 +080019408
developera3511852023-06-14 14:12:59 +080019409 if(ap_index > MAX_APS)
19410 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019411
developera3511852023-06-14 14:12:59 +080019412 *output_numDevices = 0;
19413 wifi_getApEnable(ap_index, &status);
19414 if (status == FALSE)
19415 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019416
developera3511852023-06-14 14:12:59 +080019417 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19418 return RETURN_ERR;
19419 sprintf(cmd, "hostapd_cli -i %s list_sta", interface_name);
19420 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080019421
developera3511852023-06-14 14:12:59 +080019422 mac_addr = strtok(buf, "\n");
19423 for (int i = 0; i < maxNumDevices && mac_addr != NULL; i++) {
19424 *output_numDevices = i + 1;
developerc14d83a2023-06-29 20:09:42 +080019425 wifi_debug(DEBUG_ERROR,, "mac_addr: %s\n", mac_addr);
developera3511852023-06-14 14:12:59 +080019426 addr_ptr = output_deviceMacAddressArray[i];
19427 mac_addr_aton(addr_ptr, mac_addr);
19428 mac_addr = strtok(NULL, "\n");
19429 }
developer72fb0bb2023-01-11 09:46:29 +080019430
developera3511852023-06-14 14:12:59 +080019431 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019432}
19433#else
19434INT wifi_getApAssociatedDevice(INT ap_index, CHAR *output_buf, INT output_buf_size)
19435{
developera3511852023-06-14 14:12:59 +080019436 char interface_name[16] = {0};
19437 char cmd[128];
19438 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +080019439 int res;
developer72fb0bb2023-01-11 09:46:29 +080019440
developera3511852023-06-14 14:12:59 +080019441 if(ap_index > MAX_APS || output_buf == NULL || output_buf_size <= 0)
19442 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019443
developera3511852023-06-14 14:12:59 +080019444 output_buf[0] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080019445
developera3511852023-06-14 14:12:59 +080019446 wifi_getApEnable(ap_index,&status);
19447 if (!status)
19448 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019449
developera3511852023-06-14 14:12:59 +080019450 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19451 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080019452 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta | tr '\\n' ',' | sed 's/.$//'", interface_name);
19453 if (os_snprintf_error(sizeof(cmd), res)) {
19454 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19455 return RETURN_ERR;
19456 }
developer5b23cd02023-07-19 20:26:03 +080019457
developera3511852023-06-14 14:12:59 +080019458 _syscmd(cmd, output_buf, output_buf_size);
developer69b61b02023-03-07 17:17:44 +080019459
developera3511852023-06-14 14:12:59 +080019460 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019461}
19462#endif
19463
19464INT wifi_getProxyArp(INT apIndex, BOOL *enable)
19465{
developera3511852023-06-14 14:12:59 +080019466 char output[16]={'\0'};
19467 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080019468 int res;
developer72fb0bb2023-01-11 09:46:29 +080019469
developera3511852023-06-14 14:12:59 +080019470 if (!enable)
19471 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019472
developer75bd10c2023-06-27 11:34:08 +080019473 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
19474 if (os_snprintf_error(sizeof(config_file), res)) {
19475 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19476 return RETURN_ERR;
19477 }
developera3511852023-06-14 14:12:59 +080019478 wifi_hostapdRead(config_file, "proxy_arp", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080019479
developera3511852023-06-14 14:12:59 +080019480 if (strlen(output) == 0)
19481 *enable = FALSE;
19482 else if (strncmp(output, "1", 1) == 0)
19483 *enable = TRUE;
19484 else
19485 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080019486
developera3511852023-06-14 14:12:59 +080019487 wifi_dbg_printf("\n[%s]: proxy_arp is : %s", __func__, output);
19488 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019489}
19490
19491INT wifi_getRadioStatsEnable(INT radioIndex, BOOL *output_enable)
19492{
developera3511852023-06-14 14:12:59 +080019493 if (NULL == output_enable || radioIndex >=MAX_NUM_RADIOS)
19494 return RETURN_ERR;
19495 *output_enable=TRUE;
19496 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019497}
19498
19499INT wifi_getTWTsessions(INT ap_index, UINT maxNumberSessions, wifi_twt_sessions_t *twtSessions, UINT *numSessionReturned)
19500{
developera3511852023-06-14 14:12:59 +080019501 char cmd[128] = {0};
19502 char buf[128] = {0};
19503 char line[128] = {0};
19504 FILE *f = NULL;
19505 int index = 0;
19506 int exp = 0;
19507 int mantissa = 0;
19508 int duration = 0;
19509 int radio_index = 0;
19510 int max_radio_num = 0;
19511 uint twt_wake_interval = 0;
19512 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +080019513 int res;
developerc14d83a2023-06-29 20:09:42 +080019514 unsigned long tmp_u, tmp_l;
developer75bd10c2023-06-27 11:34:08 +080019515
developera3511852023-06-14 14:12:59 +080019516 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019517
developera3511852023-06-14 14:12:59 +080019518 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080019519 if(max_radio_num == 0){
19520 return RETURN_ERR;
19521 }
developera3511852023-06-14 14:12:59 +080019522 radio_index = ap_index % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +080019523
developera3511852023-06-14 14:12:59 +080019524 phyId = radio_index_to_phy(radio_index);
developer75bd10c2023-06-27 11:34:08 +080019525
19526 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | wc -l", phyId);
19527 if (os_snprintf_error(sizeof(cmd), res)) {
19528 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19529 return RETURN_ERR;
19530 }
developera3511852023-06-14 14:12:59 +080019531 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080019532 if (hal_strtoul(buf, 10, &tmp_u) < 0) {
19533 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019534 }
19535 *numSessionReturned = tmp_u - 1;
developer5b23cd02023-07-19 20:26:03 +080019536
developera3511852023-06-14 14:12:59 +080019537 if (*numSessionReturned > maxNumberSessions)
19538 *numSessionReturned = maxNumberSessions;
19539 else if (*numSessionReturned < 1) {
19540 *numSessionReturned = 0;
19541 return RETURN_OK;
19542 }
developer72fb0bb2023-01-11 09:46:29 +080019543
developer75bd10c2023-06-27 11:34:08 +080019544 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | tail -n %d | tr '|' ' ' | tr -s ' '", phyId, *numSessionReturned);
19545 if (os_snprintf_error(sizeof(cmd), res)) {
19546 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19547 return RETURN_ERR;
19548 }
developera3511852023-06-14 14:12:59 +080019549 if ((f = popen(cmd, "r")) == NULL) {
19550 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
19551 return RETURN_ERR;
19552 }
developer72fb0bb2023-01-11 09:46:29 +080019553
developera3511852023-06-14 14:12:59 +080019554 // the format of each line is "[wcid] [id] [flags] [exp] [mantissa] [duration] [tsf]"
19555 while((fgets(line, sizeof(line), f)) != NULL) {
19556 char *tmp = NULL;
developer9ce44382023-06-28 11:09:37 +080019557 size_t len = strlen(line);
19558 strncpy(buf, line,len);
19559 buf[len] = '\0';
developera3511852023-06-14 14:12:59 +080019560 tmp = strtok(buf, " ");
developerd14dff12023-06-28 22:47:44 +080019561 if (tmp == NULL)
19562 break;
developer5b23cd02023-07-19 20:26:03 +080019563
developerc14d83a2023-06-29 20:09:42 +080019564 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19565 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19566 }
19567 twtSessions[index].numDevicesInSession = tmp_u;
developera3511852023-06-14 14:12:59 +080019568 tmp = strtok(NULL, " ");
developerd14dff12023-06-28 22:47:44 +080019569 if (tmp == NULL)
19570 break;
developer5b23cd02023-07-19 20:26:03 +080019571
developerc14d83a2023-06-29 20:09:42 +080019572 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19573 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19574 }
19575 twtSessions[index].twtParameters.operation.flowID = tmp_u;
developera3511852023-06-14 14:12:59 +080019576 tmp = strtok(NULL, " ");
19577 if (strstr(tmp, "t")) {
19578 twtSessions[index].twtParameters.operation.trigger_enabled = TRUE;
19579 }
19580 if (strstr(tmp, "a")) {
19581 twtSessions[index].twtParameters.operation.announced = TRUE;
19582 }
19583 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019584 if (tmp == NULL)
19585 continue;
developer5b23cd02023-07-19 20:26:03 +080019586
developerc14d83a2023-06-29 20:09:42 +080019587 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19588 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19589 }
19590 exp = tmp_l;
developer5b23cd02023-07-19 20:26:03 +080019591
developera3511852023-06-14 14:12:59 +080019592 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019593 if (tmp == NULL)
19594 continue;
developerc14d83a2023-06-29 20:09:42 +080019595 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19596 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019597 }
19598 mantissa = tmp_l;
developer5b23cd02023-07-19 20:26:03 +080019599
developera3511852023-06-14 14:12:59 +080019600 tmp = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +080019601
19602 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19603 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019604 }
19605 duration = tmp_l;
developer72fb0bb2023-01-11 09:46:29 +080019606
developera3511852023-06-14 14:12:59 +080019607 // only implicit supported
19608 twtSessions[index].twtParameters.operation.implicit = TRUE;
19609 // only individual agreement supported
19610 twtSessions[index].twtParameters.agreement = wifi_twt_agreement_type_individual;
developer72fb0bb2023-01-11 09:46:29 +080019611
developera3511852023-06-14 14:12:59 +080019612 // wakeInterval_uSec is a unsigned integer, but the maximum TWT wake interval could be 2^15 (mantissa) * 2^32 = 2^47.
19613 twt_wake_interval = mantissa * (1 << exp);
19614 if (mantissa == 0 || twt_wake_interval/mantissa != (1 << exp)) {
19615 // Overflow handling
19616 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = -1; // max unsigned int
19617 } else {
19618 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = twt_wake_interval;
19619 }
19620 twtSessions[index].twtParameters.params.individual.minWakeDuration_uSec = duration * 256;
19621 index++;
19622 }
developer72fb0bb2023-01-11 09:46:29 +080019623
developera3511852023-06-14 14:12:59 +080019624 pclose(f);
19625 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19626 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019627}
developercc5cbfb2023-06-13 18:29:52 +080019628
19629INT wifi_enableGreylistAccessControl(BOOL enable)
19630{
19631 char inf_name[IFNAMSIZ] = {0};
19632 int if_idx, ret = 0;
19633 struct nl_msg *msg = NULL;
19634 struct nlattr * msg_data = NULL;
19635 struct mtk_nl80211_param param;
19636 struct unl unl_ins;
19637 unsigned short apIndex = 0;
19638
19639 for (apIndex = 0; apIndex < MAX_APS; apIndex++) {
19640 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
19641 continue;
19642
19643 if_idx = if_nametoindex(inf_name);
19644 if (!if_idx) {
19645 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
19646 continue;
19647 }
19648
19649 /*init mtk nl80211 vendor cmd*/
19650 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
19651 param.if_type = NL80211_ATTR_IFINDEX;
19652 param.if_idx = if_idx;
19653 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
19654 if (ret) {
19655 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
19656 return RETURN_ERR;
19657 }
19658
19659 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, enable == FALSE ? 0 : 1)) {
19660 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
19661 nlmsg_free(msg);
19662 mtk_nl80211_deint(&unl_ins);
19663 continue;
19664 }
19665
19666 /*send mtk nl80211 vendor msg*/
19667 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
19668 if (ret) {
19669 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
19670 mtk_nl80211_deint(&unl_ins);
19671 continue;
19672 }
19673 /*deinit mtk nl80211 vendor msg*/
19674 mtk_nl80211_deint(&unl_ins);
19675 wifi_debug(DEBUG_NOTICE, " %s cmd success.\n", inf_name);
19676 }
19677
19678 return RETURN_OK;
19679}