blob: e7d7137681c0412db59a725f7866611564bb6ef0 [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
developer17038e62023-03-02 14:43:43 +08001928wifi_BringDownInterfacesForRadio(int radio_idx)
1929{
developera3511852023-06-14 14:12:59 +08001930 char cmd[MAX_BUF_SIZE] = {0};
1931 char ret_buf[MAX_BUF_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08001932 int res;
developerb758dfd2023-06-21 17:32:07 +08001933
developera3511852023-06-14 14:12:59 +08001934 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001935
developere40952c2023-06-15 18:46:43 +08001936 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw REMOVE %s", main_prefix[radio_idx]);
1937 if (os_snprintf_error(sizeof(cmd), res)) {
1938 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1939 return;
1940 }
developer8a3bbbf2023-03-15 17:47:23 +08001941 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1942
developera3511852023-06-14 14:12:59 +08001943 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001944}
1945
1946
1947static void
1948wifi_BringDownInterfaces(void)
1949{
developera3511852023-06-14 14:12:59 +08001950 int radio_idx;
1951 int band_idx;
developer17038e62023-03-02 14:43:43 +08001952
developera3511852023-06-14 14:12:59 +08001953 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
1954 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++) {
1955 band_idx = radio_index_to_band(radio_idx);
1956 if (band_idx < 0) {
1957 break;
1958 }
1959 wifi_BringDownInterfacesForRadio(radio_idx);
1960 }
1961 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer17038e62023-03-02 14:43:43 +08001962}
1963
developerb2977562023-05-24 17:54:12 +08001964static void wifi_dat_file_reset_by_radio(char radio_idx)
1965{
developerb149d9d2023-06-06 16:14:22 +08001966 char cmd[MAX_CMD_SIZE * 2] = {0};
developerb2977562023-05-24 17:54:12 +08001967 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08001968 char rom_dat_file[MAX_SUB_CMD_SIZE]= {0};
1969 char dat_file[MAX_SUB_CMD_SIZE]= {0};
developere40952c2023-06-15 18:46:43 +08001970 int res;
developerb2977562023-05-24 17:54:12 +08001971
developere40952c2023-06-15 18:46:43 +08001972 res = snprintf(rom_dat_file, sizeof(rom_dat_file), "%s%d.dat", ROM_LOGAN_DAT_FILE, radio_idx);
1973 if (os_snprintf_error(sizeof(rom_dat_file), res)) {
1974 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1975 return;
1976 }
1977 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
1978 if (os_snprintf_error(sizeof(dat_file), res)) {
1979 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1980 return;
1981 }
1982 res = snprintf(cmd, (MAX_CMD_SIZE * 2), "cp -rf %s %s", rom_dat_file, dat_file);
1983 if (os_snprintf_error((MAX_CMD_SIZE * 2), res)) {
1984 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
1985 return;
1986 }
developerb2977562023-05-24 17:54:12 +08001987 _syscmd(cmd, ret_buf, sizeof(ret_buf));
1988
1989}
1990
developer47cc27a2023-05-17 23:09:58 +08001991static void wifi_psk_file_reset()
1992{
1993 char cmd[MAX_CMD_SIZE] = {0};
1994 char ret_buf[MAX_BUF_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08001995 char psk_file[MAX_SUB_CMD_SIZE]= {0};
developer47cc27a2023-05-17 23:09:58 +08001996 char vap_idx = 0;
developere40952c2023-06-15 18:46:43 +08001997 int res;
developer47cc27a2023-05-17 23:09:58 +08001998
1999 for (vap_idx = 0; vap_idx < MAX_APS; vap_idx++) {
developere40952c2023-06-15 18:46:43 +08002000 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, vap_idx);
2001 if (os_snprintf_error(sizeof(psk_file), res)) {
2002 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2003 return;
2004 }
developer47cc27a2023-05-17 23:09:58 +08002005
2006 if (access(psk_file, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002007 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", psk_file);
2008 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2009 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2010 return;
2011 }
developer47cc27a2023-05-17 23:09:58 +08002012 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2013 } else {
developere40952c2023-06-15 18:46:43 +08002014 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", psk_file);
2015 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2016 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2017 return;
2018 }
2019
developer47cc27a2023-05-17 23:09:58 +08002020 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2021 }
2022 }
developerb2977562023-05-24 17:54:12 +08002023}
2024
developer8a3bbbf2023-03-15 17:47:23 +08002025static void wifi_vap_status_reset()
2026{
developera3511852023-06-14 14:12:59 +08002027 char cmd[MAX_CMD_SIZE] = {0};
2028 char ret_buf[MAX_BUF_SIZE] = {0};
developer863a4a62023-06-06 16:55:59 +08002029 int radio_idx = 0;
developer8a3bbbf2023-03-15 17:47:23 +08002030 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08002031 int res;
developer8666b312023-03-24 14:05:31 +08002032
developer8a3bbbf2023-03-15 17:47:23 +08002033 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002034 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", VAP_STATUS_FILE);
2035 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2036 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2037 return;
2038 }
developer8a3bbbf2023-03-15 17:47:23 +08002039 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2040 } else {
developere40952c2023-06-15 18:46:43 +08002041 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", VAP_STATUS_FILE);
2042 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2043 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2044 return;
2045 }
developer8a3bbbf2023-03-15 17:47:23 +08002046 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2047 }
2048
2049 memset(cmd, 0, MAX_CMD_SIZE);
2050 memset(ret_buf, 0, MAX_BUF_SIZE);
2051
2052 for (radio_idx = 0; radio_idx < MAX_NUM_RADIOS; radio_idx++)
developera3511852023-06-14 14:12:59 +08002053 for (bss_idx = 0; bss_idx < 5; bss_idx++) {
developere40952c2023-06-15 18:46:43 +08002054 res = snprintf(cmd, MAX_CMD_SIZE, "echo %s%d=0 >> %s", ext_prefix[radio_idx], bss_idx, VAP_STATUS_FILE);
2055 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2056 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2057 return;
2058 }
developer8a3bbbf2023-03-15 17:47:23 +08002059 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2060 }
2061
developerfead3972023-05-25 20:15:02 +08002062}
2063
2064static void wifi_radio_reset_count_reset()
2065{
developera3511852023-06-14 14:12:59 +08002066 char cmd[MAX_CMD_SIZE] = {0};
2067 char ret_buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002068 int res;
developerfead3972023-05-25 20:15:02 +08002069
2070 if (access(VAP_STATUS_FILE, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08002071 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", RADIO_RESET_FILE);
2072 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2073 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2074 return;
2075 }
developerfead3972023-05-25 20:15:02 +08002076 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2077 } else {
developere40952c2023-06-15 18:46:43 +08002078 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", RADIO_RESET_FILE);
2079 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
2080 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2081 return;
2082 }
developerfead3972023-05-25 20:15:02 +08002083 _syscmd(cmd, ret_buf, sizeof(ret_buf));
2084 }
developer8a3bbbf2023-03-15 17:47:23 +08002085}
developer17038e62023-03-02 14:43:43 +08002086
developer72fb0bb2023-01-11 09:46:29 +08002087// Initializes the wifi subsystem (all radios)
developera3511852023-06-14 14:12:59 +08002088INT wifi_init() //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002089{
developera3511852023-06-14 14:12:59 +08002090 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developercc9a4f32023-05-30 17:40:02 +08002091 static int CallOnce = 1;
developera3511852023-06-14 14:12:59 +08002092 //Not intitializing macfilter for Turris-Omnia Platform for now
2093 //macfilter_init();
2094 if (CallOnce) {
developercc9a4f32023-05-30 17:40:02 +08002095 wifi_ParseProfile();
developerd14dff12023-06-28 22:47:44 +08002096 wifi_PrepareDefaultHostapdConfigs(FALSE);
developercc9a4f32023-05-30 17:40:02 +08002097 wifi_psk_file_reset();
developer33f13ba2023-07-12 16:19:06 +08002098 //v_secure_system("/usr/sbin/iw reg set US");
2099 v_secure_system("systemctl start hostapd.service");
developercc9a4f32023-05-30 17:40:02 +08002100 sleep(2);
developer8a3bbbf2023-03-15 17:47:23 +08002101
developercc9a4f32023-05-30 17:40:02 +08002102 wifi_vap_status_reset();
2103 wifi_radio_reset_count_reset();
2104 CallOnce = 0;
developera3511852023-06-14 14:12:59 +08002105 }
developer96b38512023-02-22 11:17:45 +08002106
developera3511852023-06-14 14:12:59 +08002107 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002108
developera3511852023-06-14 14:12:59 +08002109 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002110}
2111
2112/* wifi_reset() function */
2113/**
2114* Description: Resets the Wifi subsystem. This includes reset of all AP varibles.
developer69b61b02023-03-07 17:17:44 +08002115* Implementation specifics may dictate what is actualy reset since
developer72fb0bb2023-01-11 09:46:29 +08002116* different hardware implementations may have different requirements.
2117* Parameters : None
2118*
2119* @return The status of the operation.
2120* @retval RETURN_OK if successful.
2121* @retval RETURN_ERR if any error is detected
2122*
2123* @execution Synchronous.
2124* @sideeffect None.
2125*
2126* @note This function must not suspend and must not invoke any blocking system
2127* calls. It should probably just send a message to a driver event handler task.
2128*
2129*/
2130INT wifi_reset()
2131{
developer17038e62023-03-02 14:43:43 +08002132
developera3511852023-06-14 14:12:59 +08002133 wifi_BringDownInterfaces();
2134 sleep(2);
developer17038e62023-03-02 14:43:43 +08002135
developera3511852023-06-14 14:12:59 +08002136 //TODO: resets the wifi subsystem, deletes all APs
developer33f13ba2023-07-12 16:19:06 +08002137 v_secure_system("systemctl stop hostapd.service");
developera3511852023-06-14 14:12:59 +08002138 sleep(2);
developer17038e62023-03-02 14:43:43 +08002139
developer33f13ba2023-07-12 16:19:06 +08002140 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +08002141 sleep(5);
developer17038e62023-03-02 14:43:43 +08002142
developerd14dff12023-06-28 22:47:44 +08002143 wifi_PrepareDefaultHostapdConfigs(TRUE);
developer47cc27a2023-05-17 23:09:58 +08002144 wifi_psk_file_reset();
developera3511852023-06-14 14:12:59 +08002145 sleep(2);
developer8a3bbbf2023-03-15 17:47:23 +08002146
2147 wifi_vap_status_reset();
2148
developera3511852023-06-14 14:12:59 +08002149 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002150}
2151
2152/* wifi_down() function */
2153/**
2154* @description Turns off transmit power for the entire Wifi subsystem, for all radios.
developer69b61b02023-03-07 17:17:44 +08002155* Implementation specifics may dictate some functionality since
developer72fb0bb2023-01-11 09:46:29 +08002156* different hardware implementations may have different requirements.
2157*
2158* @param None
2159*
2160* @return The status of the operation
2161* @retval RETURN_OK if successful
2162* @retval RETURN_ERR if any error is detected
2163*
2164* @execution Synchronous
2165* @sideeffect None
2166*
2167* @note This function must not suspend and must not invoke any blocking system
2168* calls. It should probably just send a message to a driver event handler task.
2169*
2170*/
2171INT wifi_down()
2172{
developera3511852023-06-14 14:12:59 +08002173 //TODO: turns off transmit power for the entire Wifi subsystem, for all radios
2174 int max_num_radios = 0;
developerb2977562023-05-24 17:54:12 +08002175 wifi_getMaxRadioNumber(&max_num_radios);
developer17038e62023-03-02 14:43:43 +08002176
developerb2977562023-05-24 17:54:12 +08002177 for (int radioIndex = 0; radioIndex < max_num_radios; radioIndex++)
2178 wifi_setRadioEnable(radioIndex, FALSE);
2179
developera3511852023-06-14 14:12:59 +08002180 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002181}
2182
2183
2184/* wifi_createInitialConfigFiles() function */
2185/**
2186* @description This function creates wifi configuration files. The format
developer69b61b02023-03-07 17:17:44 +08002187* and content of these files are implementation dependent. This function call is
2188* used to trigger this task if necessary. Some implementations may not need this
2189* function. If an implementation does not need to create config files the function call can
developer72fb0bb2023-01-11 09:46:29 +08002190* do nothing and return RETURN_OK.
2191*
2192* @param None
2193*
2194* @return The status of the operation
2195* @retval RETURN_OK if successful
2196* @retval RETURN_ERR if any error is detected
2197*
2198* @execution Synchronous
2199* @sideeffect None
2200*
2201* @note This function must not suspend and must not invoke any blocking system
2202* calls. It should probably just send a message to a driver event handler task.
2203*
2204*/
2205INT wifi_createInitialConfigFiles()
2206{
developera3511852023-06-14 14:12:59 +08002207 //TODO: creates initial implementation dependent configuration files that are later used for variable storage. Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
2208 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002209}
2210
developer7e4a2a62023-04-06 19:56:03 +08002211/* outputs the country code to a max 64 character string */
developer72fb0bb2023-01-11 09:46:29 +08002212INT wifi_getRadioCountryCode(INT radioIndex, CHAR *output_string)
2213{
developera3511852023-06-14 14:12:59 +08002214 int ret;
developer72fb0bb2023-01-11 09:46:29 +08002215
developera3511852023-06-14 14:12:59 +08002216 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002217
developera3511852023-06-14 14:12:59 +08002218 ret = wifi_BandProfileRead(0, radioIndex, "CountryCode", output_string, 64, NULL);
2219 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +08002220 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead CountryCode failed\n");
developera3511852023-06-14 14:12:59 +08002221 return RETURN_ERR;
2222 }
developer7e4a2a62023-04-06 19:56:03 +08002223
developera3511852023-06-14 14:12:59 +08002224 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +08002225
developera3511852023-06-14 14:12:59 +08002226 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002227}
2228
2229INT wifi_setRadioCountryCode(INT radioIndex, CHAR *CountryCode)
2230{
developer7e4a2a62023-04-06 19:56:03 +08002231 /*Set wifi config. Wait for wifi reset to apply*/
developer7e4a2a62023-04-06 19:56:03 +08002232 struct params params;
2233 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08002234 int ret = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002235
developer7e4a2a62023-04-06 19:56:03 +08002236 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002237
developer7e4a2a62023-04-06 19:56:03 +08002238 if(NULL == CountryCode || strlen(CountryCode) >= 32 ) {
2239 printf("%s: input para error!!!\n", __func__);
2240 return RETURN_ERR;
2241 }
developer72fb0bb2023-01-11 09:46:29 +08002242
developerc79e9172023-06-06 19:48:03 +08002243 if (!strlen(CountryCode)) {
2244 memcpy(CountryCode, "US", strlen("US")); /*default set the code to US*/
2245 CountryCode[2] = '\0';
2246 }
developer72fb0bb2023-01-11 09:46:29 +08002247
developer7e4a2a62023-04-06 19:56:03 +08002248 params.name = "country_code";
2249 params.value = CountryCode;
developer72fb0bb2023-01-11 09:46:29 +08002250
developere40952c2023-06-15 18:46:43 +08002251 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, radioIndex);
2252 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
2253 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2254 return RETURN_ERR;
2255 }
developer7e4a2a62023-04-06 19:56:03 +08002256 ret = wifi_hostapdWrite(config_file, &params, 1);
2257
2258 if (ret) {
2259 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n",
2260 __func__, ret);
2261 }
2262
2263 ret = wifi_hostapdProcessUpdate(radioIndex, &params, 1);
2264
2265 if (ret) {
2266 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n",
2267 __func__, ret);
2268 }
2269
2270 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
2271
2272 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002273}
2274
2275INT wifi_getRadioChannelStats2(INT radioIndex, wifi_channelStats2_t *outputChannelStats2)
2276{
developera3511852023-06-14 14:12:59 +08002277 char interface_name[16] = {0};
2278 char channel_util_file[64] = {0};
2279 char cmd[128] = {0};
2280 char buf[128] = {0};
2281 char *line = NULL;
2282 char *param = NULL, *value = NULL;
developere40952c2023-06-15 18:46:43 +08002283 int read = 0, res;
developera3511852023-06-14 14:12:59 +08002284 unsigned int ActiveTime = 0, BusyTime = 0, TransmitTime = 0;
developer86035662023-06-28 19:21:12 +08002285 unsigned long preActiveTime = 0, preBusyTime = 0, preTransmitTime = 0;
developerc14d83a2023-06-29 20:09:42 +08002286 long int rssi;
2287 long int tmp_l;
2288 unsigned long tmp_ul;
developera3511852023-06-14 14:12:59 +08002289 size_t len = 0;
2290 FILE *f = NULL;
developer72fb0bb2023-01-11 09:46:29 +08002291
developera3511852023-06-14 14:12:59 +08002292 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002293
developera3511852023-06-14 14:12:59 +08002294 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2295 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002296 res = snprintf(cmd, sizeof(cmd), "iw %s scan | grep signal | awk '{print $2}' | sort -n | tail -n1", interface_name);
2297 if (os_snprintf_error(sizeof(cmd), res)) {
2298 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2299 return RETURN_ERR;
2300 }
2301
developera3511852023-06-14 14:12:59 +08002302 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08002303 if (hal_strtol(buf, 10, &rssi) < 0) {
2304 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2305 }
2306 outputChannelStats2->ch_Max80211Rssi = rssi;
developer72fb0bb2023-01-11 09:46:29 +08002307
developera3511852023-06-14 14:12:59 +08002308 memset(cmd, 0, sizeof(cmd));
2309 memset(buf, 0, sizeof(buf));
developere40952c2023-06-15 18:46:43 +08002310 res = snprintf(cmd, sizeof(cmd), "iw %s survey dump | grep 'in use' -A6", interface_name);
2311 if (os_snprintf_error(sizeof(cmd), res)) {
2312 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2313 return RETURN_ERR;
2314 }
developera3511852023-06-14 14:12:59 +08002315 if ((f = popen(cmd, "r")) == NULL) {
2316 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
2317 return RETURN_ERR;
2318 }
developer72fb0bb2023-01-11 09:46:29 +08002319
developera3511852023-06-14 14:12:59 +08002320 read = getline(&line, &len, f);
2321 while (read != -1) {
2322 param = strtok(line, ":\t");
developerc14d83a2023-06-29 20:09:42 +08002323 if (!param) {
2324 read = getline(&line, &len, f);
2325 continue;
2326 }
developera3511852023-06-14 14:12:59 +08002327 value = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +08002328 if (!value) {
developer37646972023-06-29 10:58:43 +08002329 read = getline(&line, &len, f);
2330 continue;
2331 }
developera3511852023-06-14 14:12:59 +08002332 if(strstr(param, "frequency") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002333 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2334 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2335 }
2336 outputChannelStats2->ch_Frequency = tmp_ul;
developera3511852023-06-14 14:12:59 +08002337 }
2338 if(strstr(param, "noise") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002339 if (hal_strtol(value, 10, &tmp_l) < 0) {
2340 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2341 }
2342 outputChannelStats2->ch_NoiseFloor = tmp_l;
developer37646972023-06-29 10:58:43 +08002343
developerc14d83a2023-06-29 20:09:42 +08002344 if (hal_strtol(value, 10, &tmp_l) < 0) {
2345 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2346 }
2347 outputChannelStats2->ch_Non80211Noise = tmp_l;
developera3511852023-06-14 14:12:59 +08002348 }
2349 if(strstr(param, "channel active time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002350 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2351 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2352 }
2353 ActiveTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002354 }
developerc14d83a2023-06-29 20:09:42 +08002355 if(strstr(param, "channel busy time") != NULL) {
2356 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2357 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2358 }
2359 BusyTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002360 }
2361 if(strstr(param, "channel transmit time") != NULL) {
developerc14d83a2023-06-29 20:09:42 +08002362 if (hal_strtoul(value, 10, &tmp_ul) < 0) {
2363 wifi_debug(DEBUG_ERROR, "strtol fail\n");
2364 }
2365 TransmitTime = tmp_ul;
developera3511852023-06-14 14:12:59 +08002366 }
2367 read = getline(&line, &len, f);
2368 }
2369 pclose(f);
developer72fb0bb2023-01-11 09:46:29 +08002370
developera3511852023-06-14 14:12:59 +08002371 // The file should store the last active, busy and transmit time
developere40952c2023-06-15 18:46:43 +08002372 res = snprintf(channel_util_file, sizeof(channel_util_file), "%s%d.txt", CHANNEL_STATS_FILE, radioIndex);
2373 if (os_snprintf_error(sizeof(channel_util_file), res)) {
2374 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2375 return RETURN_ERR;
2376 }
2377
developera3511852023-06-14 14:12:59 +08002378 f = fopen(channel_util_file, "r");
2379 if (f != NULL) {
2380 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002381 if (hal_strtoul(line, 10, &preActiveTime) < 0) {
2382 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002383 }
developera3511852023-06-14 14:12:59 +08002384 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002385 if (hal_strtoul(line, 10, &preBusyTime) < 0) {
2386 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerb6f05602023-06-30 11:32:41 +08002387
developer86035662023-06-28 19:21:12 +08002388 }
developera3511852023-06-14 14:12:59 +08002389 read = getline(&line, &len, f);
developer86035662023-06-28 19:21:12 +08002390 if (hal_strtoul(line, 10, &preTransmitTime) < 0) {
2391 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +08002392 }
developerc14d83a2023-06-29 20:09:42 +08002393
2394 if (fclose(f) == EOF)
2395 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
2396
2397 }
2398
developerb14b3462023-07-01 18:02:42 +08002399 /*if (ActiveTime == preActiveTime) {
2400 wifi_debug(DEBUG_ERROR, "error:ActiveTime[%d] == preActiveTime[%ld]\n",
2401 ActiveTime, preActiveTime);
developerc14d83a2023-06-29 20:09:42 +08002402 return RETURN_ERR;
developerb14b3462023-07-01 18:02:42 +08002403 }*/
developer72fb0bb2023-01-11 09:46:29 +08002404
developera3511852023-06-14 14:12:59 +08002405 outputChannelStats2->ch_ObssUtil = (BusyTime - preBusyTime)*100/(ActiveTime - preActiveTime);
2406 outputChannelStats2->ch_SelfBssUtil = (TransmitTime - preTransmitTime)*100/(ActiveTime - preActiveTime);
developer72fb0bb2023-01-11 09:46:29 +08002407
developera3511852023-06-14 14:12:59 +08002408 f = fopen(channel_util_file, "w");
2409 if (f != NULL) {
developer86035662023-06-28 19:21:12 +08002410 if (fprintf(f, "%u\n%u\n%u\n", ActiveTime, BusyTime, TransmitTime) < 0) {
2411 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
2412 }
2413 if (fclose(f) != 0) {
2414 wifi_debug(DEBUG_ERROR, "fclose fail\n");
2415 }
developera3511852023-06-14 14:12:59 +08002416 }
2417 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2418 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002419}
2420
2421/**********************************************************************************
2422 *
2423 * Wifi radio level function prototypes
2424 *
2425**********************************************************************************/
2426
2427//Get the total number of radios in this wifi subsystem
2428INT wifi_getRadioNumberOfEntries(ULONG *output) //Tr181
2429{
developera3511852023-06-14 14:12:59 +08002430 if (NULL == output)
2431 return RETURN_ERR;
2432 *output = MAX_NUM_RADIOS;
developer72fb0bb2023-01-11 09:46:29 +08002433
developera3511852023-06-14 14:12:59 +08002434 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002435}
2436
developer69b61b02023-03-07 17:17:44 +08002437//Get the total number of SSID entries in this wifi subsystem
developer72fb0bb2023-01-11 09:46:29 +08002438INT wifi_getSSIDNumberOfEntries(ULONG *output) //Tr181
2439{
developera3511852023-06-14 14:12:59 +08002440 if (NULL == output)
2441 return RETURN_ERR;
2442 *output = MAX_APS;
developer72fb0bb2023-01-11 09:46:29 +08002443
developera3511852023-06-14 14:12:59 +08002444 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002445}
2446
2447//Get the Radio enable config parameter
developera3511852023-06-14 14:12:59 +08002448INT wifi_getRadioEnable(INT radioIndex, BOOL *output_bool) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08002449{
developer56fbedb2023-05-30 16:47:05 +08002450 char interface_name[16] = {0};
2451 char buf[128] = {0}, cmd[128] = {0};
2452 int apIndex;
2453 int max_radio_num = 0;
developer75bd10c2023-06-27 11:34:08 +08002454 int res;
developer3a85ab82023-05-25 11:59:38 +08002455
developer56fbedb2023-05-30 16:47:05 +08002456 if (NULL == output_bool)
2457 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002458
developer56fbedb2023-05-30 16:47:05 +08002459 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08002460
developer56fbedb2023-05-30 16:47:05 +08002461 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002462
developer56fbedb2023-05-30 16:47:05 +08002463 if (radioIndex >= max_radio_num)
2464 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002465
developer56fbedb2023-05-30 16:47:05 +08002466 /* loop all interface in radio, if any is enable, reture true, else return false */
2467 for(apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num)
2468 {
2469 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2470 continue;
2471 memset(cmd, 0, sizeof(cmd));
developer75bd10c2023-06-27 11:34:08 +08002472 res = snprintf(cmd, sizeof(cmd), "ifconfig %s 2> /dev/null | grep UP", interface_name);
2473 if (os_snprintf_error(sizeof(cmd), res)) {
2474 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2475 return RETURN_ERR;
2476 }
developer56fbedb2023-05-30 16:47:05 +08002477 *output_bool = _syscmd(cmd, buf, sizeof(buf)) ? FALSE : TRUE;
2478 if (*output_bool == TRUE)
2479 break;
2480 }
2481
2482 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002483}
2484
developere82c0ca2023-05-10 16:25:35 +08002485typedef long time_t;
2486static time_t radio_up_time[MAX_NUM_RADIOS];
2487
developer72fb0bb2023-01-11 09:46:29 +08002488INT wifi_setRadioEnable(INT radioIndex, BOOL enable)
2489{
developera3511852023-06-14 14:12:59 +08002490 char interface_name[16] = {0};
2491 char cmd[MAX_CMD_SIZE] = {0};
2492 char buf[MAX_BUF_SIZE] = {0};
2493 int apIndex;
2494 int max_radio_num = 0;
developere40952c2023-06-15 18:46:43 +08002495 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002496
developera3511852023-06-14 14:12:59 +08002497 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08002498
developera3511852023-06-14 14:12:59 +08002499 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08002500
developera3511852023-06-14 14:12:59 +08002501 wifi_getMaxRadioNumber(&max_radio_num);
developer72fb0bb2023-01-11 09:46:29 +08002502
developera3511852023-06-14 14:12:59 +08002503 if(enable == FALSE) {
developer47cc27a2023-05-17 23:09:58 +08002504
2505 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
2506 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08002507
2508 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i global raw REMOVE %s", interface_name);
2509 if (os_snprintf_error(sizeof(cmd), res)) {
2510 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2511 return RETURN_ERR;
2512 }
developer47cc27a2023-05-17 23:09:58 +08002513
developer8a3bbbf2023-03-15 17:47:23 +08002514 _syscmd(cmd, buf, sizeof(buf));
developer56fbedb2023-05-30 16:47:05 +08002515 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08002516 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s down", interface_name);
2517 if (os_snprintf_error(sizeof(cmd), res)) {
2518 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2519 return RETURN_ERR;
2520 }
developer56fbedb2023-05-30 16:47:05 +08002521 _syscmd(cmd, buf, sizeof(buf));
developere82c0ca2023-05-10 16:25:35 +08002522 if(strncmp(buf, "OK", 2))
developer75bd10c2023-06-27 11:34:08 +08002523 wifi_debug(DEBUG_ERROR, "Could not detach %s from hostapd daemon", interface_name);
developer8a3bbbf2023-03-15 17:47:23 +08002524 } else {
developere82c0ca2023-05-10 16:25:35 +08002525 for (apIndex = radioIndex; apIndex < MAX_APS; apIndex += max_radio_num) {
developera3511852023-06-14 14:12:59 +08002526 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
2527 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002528
developer8a3bbbf2023-03-15 17:47:23 +08002529 memset(cmd, 0, MAX_CMD_SIZE);
2530 memset(buf, 0, MAX_BUF_SIZE);
2531
developere40952c2023-06-15 18:46:43 +08002532 res = snprintf(cmd, sizeof(cmd), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
2533 if (os_snprintf_error(sizeof(cmd), res)) {
2534 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2535 return RETURN_ERR;
2536 }
developera3511852023-06-14 14:12:59 +08002537 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002538
developera3511852023-06-14 14:12:59 +08002539 if(*buf == '1') {
developere40952c2023-06-15 18:46:43 +08002540 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s up", interface_name);
2541 if (os_snprintf_error(sizeof(cmd), res)) {
2542 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2543 return RETURN_ERR;
2544 }
developer56fbedb2023-05-30 16:47:05 +08002545 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002546
2547 memset(cmd, 0, MAX_CMD_SIZE);
2548 memset(buf, 0, MAX_BUF_SIZE);
2549
developere40952c2023-06-15 18:46:43 +08002550 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 +08002551 phyId, apIndex);
developere40952c2023-06-15 18:46:43 +08002552 if (os_snprintf_error(sizeof(cmd), res)) {
2553 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2554 return RETURN_ERR;
2555 }
developera3511852023-06-14 14:12:59 +08002556 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +08002557
developera3511852023-06-14 14:12:59 +08002558 }
2559 }
developere75ba632023-06-29 16:03:33 +08002560 if (time(&radio_up_time[radioIndex]) < 0) {
2561 wifi_debug(DEBUG_ERROR, "GET time fail\n");
2562 return RETURN_ERR;
2563 }
developera3511852023-06-14 14:12:59 +08002564 }
developer72fb0bb2023-01-11 09:46:29 +08002565
developera3511852023-06-14 14:12:59 +08002566 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
2567 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08002568}
2569
2570//Get the Radio enable status
2571INT wifi_getRadioStatus(INT radioIndex, BOOL *output_bool) //RDKB
2572{
developera3511852023-06-14 14:12:59 +08002573 if (NULL == output_bool)
2574 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002575
developera3511852023-06-14 14:12:59 +08002576 return wifi_getRadioEnable(radioIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08002577}
2578
2579//Get the Radio Interface name from platform, eg "wlan0"
2580INT wifi_getRadioIfName(INT radioIndex, CHAR *output_string) //Tr181
2581{
developera3511852023-06-14 14:12:59 +08002582 if (NULL == output_string || radioIndex>=MAX_NUM_RADIOS || radioIndex<0)
2583 return RETURN_ERR;
2584 return wifi_GetInterfaceName(radioIndex, output_string);
developer72fb0bb2023-01-11 09:46:29 +08002585}
2586
developer6e578302023-06-21 10:11:16 +08002587int mtk_get_vow_info_callback(struct nl_msg *msg, void *data)
2588{
2589 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2590 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX + 1];
2591 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2592 int err = 0;
2593 struct vow_info *vow_info = NULL;
2594 struct mtk_nl80211_cb_data *cb_data = data;
2595
2596 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2597 genlmsg_attrlen(gnlh, 0), NULL);
2598 if (err < 0) {
2599 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
2600 return err;
2601 }
2602
2603 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2604 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX,
2605 tb[NL80211_ATTR_VENDOR_DATA], NULL);
2606 if (err < 0){
2607 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_VOW_ATTR_MAX fails\n");
2608 return err;
2609 }
2610
2611 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]) {
2612 vow_info = (struct vow_info *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO]);
2613 memmove(cb_data->out_buf, vow_info, sizeof(struct vow_info));
2614 }
2615 }
2616
2617 return 0;
2618}
2619
2620INT mtk_wifi_set_air_time_management(
2621 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
2622 char* data, INT len, void *output)
2623{
2624 char inf_name[IF_NAME_SIZE] = {0};
2625 unsigned int if_idx = 0;
2626 int ret = -1;
2627 struct unl unl_ins;
2628 struct nl_msg *msg = NULL;
2629 struct nlattr * msg_data = NULL;
2630 struct mtk_nl80211_param param;
2631
2632 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
2633 return RETURN_ERR;
2634 if_idx = if_nametoindex(inf_name);
2635 if (!if_idx) {
2636 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
2637 return RETURN_ERR;
2638 }
2639 /*init mtk nl80211 vendor cmd*/
2640 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_VOW;
2641 param.if_type = NL80211_ATTR_IFINDEX;
2642 param.if_idx = if_idx;
2643
2644 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
2645 if (ret) {
2646 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
2647 return RETURN_ERR;
2648 }
2649 /*add mtk vendor cmd data*/
2650 if (nla_put(msg, vendor_data_attr, len, data)) {
2651 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
2652 nlmsg_free(msg);
2653 goto err;
2654 }
2655
2656 /*send mtk nl80211 vendor msg*/
2657 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output);
2658 if (ret) {
2659 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
2660 goto err;
2661 }
2662 /*deinit mtk nl80211 vendor msg*/
2663 mtk_nl80211_deint(&unl_ins);
2664 wifi_debug(DEBUG_INFO, "send cmd success.\n");
2665
2666 return RETURN_OK;
2667err:
2668 mtk_nl80211_deint(&unl_ins);
2669 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
2670 return RETURN_ERR;
2671}
2672
2673//Get the ATM(Air Time Management) Capable.
2674INT wifi_getATMCapable(BOOL *output_bool)
2675{
2676 if (NULL == output_bool)
2677 return RETURN_ERR;
2678 *output_bool = TRUE;
2679
2680 return RETURN_OK;
2681}
2682
2683INT wifi_setATMEnable(BOOL enable)
2684{
2685 int max_radio_num = 0;
2686 int radio_idx = 0;
2687 char dat_file[MAX_BUF_SIZE] = {0};
2688 int res;
2689 struct params params[2];
2690
2691 wifi_getMaxRadioNumber(&max_radio_num);
2692 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2693 if (mtk_wifi_set_air_time_management
2694 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO,
2695 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2696 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2697 return RETURN_ERR;
2698 }
2699
2700 if (mtk_wifi_set_air_time_management
2701 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_BW_EN_INFO,
2702 NULL, (char *)&enable, 1, NULL)!= RETURN_OK) {
2703 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATF_EN_INFO cmd fails\n");
2704 return RETURN_ERR;
2705 }
2706
2707 params[0].name = "VOW_Airtime_Fairness_En";
2708 params[0].value = enable ? "1" : "0";
2709 params[1].name = "VOW_BW_Ctrl";
2710 params[1].value = enable ? "1" : "0";
2711
2712 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radio_idx);
2713 if (os_snprintf_error(sizeof(dat_file), res)) {
2714 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2715 return RETURN_ERR;
2716 }
2717 wifi_datfileWrite(dat_file, params, 2);
2718 }
2719
2720 return RETURN_OK;
2721}
2722
2723INT wifi_getATMEnable(BOOL *output_enable)
2724{
2725 int max_radio_num = 0;
2726 int radio_idx = 0;
2727 struct vow_info vow_info;
2728 struct vow_info get_vow_info;
2729 struct mtk_nl80211_cb_data cb_data;
2730
2731 if (output_enable == NULL)
2732 return RETURN_ERR;
2733
2734 wifi_getMaxRadioNumber(&max_radio_num);
2735
2736 *output_enable = FALSE;
2737
2738 memset(&vow_info, 0, sizeof(struct vow_info));
2739
2740 cb_data.out_buf = (char *)&vow_info;
2741 cb_data.out_len = sizeof(struct vow_info);
2742
2743 for (radio_idx = 0; radio_idx < max_radio_num; radio_idx++) {
2744 if (mtk_wifi_set_air_time_management
2745 (radio_idx, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2746 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2747 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2748 return RETURN_ERR;
2749 }
2750
2751 if (vow_info.atf_en == TRUE || vow_info.bw_en == TRUE) {
2752 *output_enable = TRUE;
2753 break;
2754 }
2755 }
2756
2757 return RETURN_OK;
2758}
2759
2760UINT apidx_to_group(INT apIndex)
2761{
2762 int max_radio_num = 0;
2763 unsigned int group = 0;
2764
2765 wifi_getMaxRadioNumber(&max_radio_num);
developerc14d83a2023-06-29 20:09:42 +08002766 if (max_radio_num == 0) {
2767 wifi_debug(DEBUG_ERROR, "invalid max radio num\n");
2768 return 0;
2769 }
developer6e578302023-06-21 10:11:16 +08002770 group = apIndex / max_radio_num + 5 * (apIndex % max_radio_num);
2771
2772 return group;
2773}
2774
2775INT wifi_setApATMAirTimePercent(INT apIndex, UINT ap_AirTimePercent)
2776{
2777 struct vow_group_en_param atc_en_param;
2778 struct vow_ratio_param radio_param;
2779 unsigned int group = 0;
2780 //BOOL ATM_enable = FALSE;
2781
2782 if (ap_AirTimePercent < 5 || ap_AirTimePercent > 100) {
2783 wifi_debug(DEBUG_ERROR, "invalid ait time percent!\n");
2784 return RETURN_ERR;
2785 }
2786
2787 /* mt7990 support 15 group now*/
2788 group = apidx_to_group(apIndex);
2789
2790 if (group > 15) {
2791 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2792 return RETURN_ERR;
2793 }
2794
2795 atc_en_param.group = group;
2796 atc_en_param.en = 1;
2797 if (mtk_wifi_set_air_time_management
2798 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO,
2799 NULL, (char *)&atc_en_param, sizeof(struct vow_group_en_param), NULL)!= RETURN_OK) {
2800 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_ATC_EN_INFO cmd fails\n");
2801 return RETURN_ERR;
2802 }
2803
2804 radio_param.group = group;
2805 radio_param.ratio = ap_AirTimePercent;
2806 if (mtk_wifi_set_air_time_management
2807 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO,
2808 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2809 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MIN_RATIO_INFO cmd fails\n");
2810 return RETURN_ERR;
2811 }
2812
2813 if (mtk_wifi_set_air_time_management
2814 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO,
2815 NULL, (char *)&radio_param, sizeof(struct vow_ratio_param), NULL)!= RETURN_OK) {
2816 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_MAX_RATIO_INFO cmd fails\n");
2817 return RETURN_ERR;
2818 }
2819
2820 return RETURN_OK;
2821}
2822
2823INT wifi_getApATMAirTimePercent(INT apIndex, UINT *output_ap_AirTimePercent)
2824{
2825 unsigned int group = 0;
2826 struct vow_info get_vow_info, vow_info;
2827 struct mtk_nl80211_cb_data cb_data;
2828
2829 if (output_ap_AirTimePercent == NULL)
2830 return RETURN_ERR;
2831
2832 group = apidx_to_group(apIndex);
2833 if (group > 15) {
2834 wifi_debug(DEBUG_ERROR, "invalid group!\n");
2835 return RETURN_ERR;
2836 }
2837
2838 memset(&vow_info, 0, sizeof(struct vow_info));
2839 memset(&get_vow_info, 0, sizeof(struct vow_info));
2840
2841 cb_data.out_buf = (char *)&vow_info;
2842 cb_data.out_len = sizeof(struct vow_info);
2843
2844 get_vow_info.group = group;
2845
2846 if (mtk_wifi_set_air_time_management
2847 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2848 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2849 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2850 return RETURN_ERR;
2851 }
2852
2853 *output_ap_AirTimePercent = vow_info.ratio;
2854
2855 return RETURN_ERR;
2856}
2857
developerd14dff12023-06-28 22:47:44 +08002858INT wifi_getApATMSta(INT apIndex, UCHAR *output_sta_MAC_ATM_array, UINT buf_size)
2859{
2860 ULONG dev_num = 0;
2861 struct vow_info vow_info;
2862 struct vow_info get_vow_info;
2863 struct mtk_nl80211_cb_data cb_data;
2864 unsigned int percent;
2865 char assocArray[MAX_BUF_SIZE] = {0};
2866 char *mac = NULL;
2867 unsigned char output_len = 0;
2868 int res;
2869 char buf[MAX_BUF_SIZE] = {0};
2870
2871 memset(&vow_info, 0, sizeof(struct vow_info));
2872 memset(&get_vow_info, 0, sizeof(struct vow_info));
2873
2874 cb_data.out_buf = (char *)&vow_info;
2875 cb_data.out_len = sizeof(struct vow_info);
2876
2877 if (mtk_wifi_set_air_time_management
2878 (apIndex, MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO,
2879 mtk_get_vow_info_callback, (char *)&get_vow_info, sizeof(struct vow_info), &cb_data)!= RETURN_OK) {
2880 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_VOW_GET_INFO cmd fails\n");
2881 return RETURN_ERR;
2882 }
2883
2884 if (vow_info.atf_en == FALSE) {
2885 wifi_debug(DEBUG_ERROR, "ATF disable!\n");
2886 return RETURN_ERR;
2887 }
2888
2889 if (wifi_getApNumDevicesAssociated(apIndex, &dev_num) != RETURN_OK) {
2890 wifi_debug(DEBUG_ERROR, "get sta num fail!\n");
2891 return RETURN_ERR;;
2892 }
2893
2894 percent = 100 / dev_num;
2895
2896 if (wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray)) != RETURN_OK){
2897 wifi_debug(DEBUG_ERROR, "get sta mac fail!\n");
2898 return RETURN_ERR;;
2899 }
2900
2901 memset(output_sta_MAC_ATM_array, 0, MAX_BUF_SIZE);
2902
2903 mac = strtok(assocArray, "\n");
2904 while (mac != NULL) {
2905 if (strlen(mac) >= 17) {
2906 res = snprintf(buf, sizeof(buf), "%s %d|", mac, percent);
2907
2908 if (os_snprintf_error(sizeof(buf), res)) {
2909 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2910 return RETURN_ERR;
2911 }
2912
2913 if (output_len + strlen(buf) > buf_size)
2914 break;
2915
2916 strncat((char *)output_sta_MAC_ATM_array, buf, strlen(buf));
2917
2918 output_len += strlen(buf);
2919 }
2920
2921 mac = strtok(NULL, "\n");
2922 }
2923
2924 /* Remove the last | */
2925 if (strlen((char *)output_sta_MAC_ATM_array) != 0)
2926 output_sta_MAC_ATM_array[strlen((char *)output_sta_MAC_ATM_array)-1] = '\0';
2927
2928 return RETURN_OK;
2929}
2930
2931INT wifi_setApATMSta(INT apIndex, UCHAR *sta_MAC_ATM_array, UINT ap_AirTimePercent)
2932{
2933 return RETURN_ERR;
2934}
developer9ce44382023-06-28 11:09:37 +08002935
developer72fb0bb2023-01-11 09:46:29 +08002936//Get the maximum PHY bit rate supported by this interface. eg: "216.7 Mb/s", "1.3 Gb/s"
2937//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.
2938INT wifi_getRadioMaxBitRate(INT radioIndex, CHAR *output_string) //RDKB
2939{
developera3511852023-06-14 14:12:59 +08002940 // The formula to coculate bit rate is "Subcarriers * Modulation * Coding rate * Spatial stream / (Data interval + Guard interval)"
2941 // For max bit rate, we should always choose the best MCS
2942 char mode[64] = {0};
2943 char channel_bandwidth_str[64] = {0};
2944 UINT mode_map = 0;
2945 UINT num_subcarrier = 0;
2946 UINT code_bits = 0;
2947 float code_rate = 0; // use max code rate
2948 int NSS = 0;
2949 UINT Symbol_duration = 0;
2950 UINT GI_duration = 0;
2951 wifi_guard_interval_t gi = wifi_guard_interval_auto;
2952 BOOL enable = FALSE;
2953 float bit_rate = 0;
developere40952c2023-06-15 18:46:43 +08002954 int ant_bitmap = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08002955
developera3511852023-06-14 14:12:59 +08002956 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
2957 if (NULL == output_string)
2958 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08002959
developera3511852023-06-14 14:12:59 +08002960 wifi_getRadioEnable(radioIndex, &enable);
2961 if (enable == FALSE) {
developere40952c2023-06-15 18:46:43 +08002962 res = snprintf(output_string, 64, "0 Mb/s");
2963 if (os_snprintf_error(64, res)) {
2964 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
2965 return RETURN_ERR;
2966 }
developera3511852023-06-14 14:12:59 +08002967 return RETURN_OK;
2968 }
developer72fb0bb2023-01-11 09:46:29 +08002969
developera3511852023-06-14 14:12:59 +08002970 if (wifi_getRadioMode(radioIndex, mode, &mode_map) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08002971 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +08002972 return RETURN_ERR;
2973 }
developer72fb0bb2023-01-11 09:46:29 +08002974
developera3511852023-06-14 14:12:59 +08002975 if (wifi_getGuardInterval(radioIndex, &gi) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08002976 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +08002977 return RETURN_ERR;
2978 }
developer72fb0bb2023-01-11 09:46:29 +08002979
developera3511852023-06-14 14:12:59 +08002980 if (gi == wifi_guard_interval_3200)
2981 GI_duration = 32;
2982 else if (gi == wifi_guard_interval_1600)
2983 GI_duration = 16;
2984 else if (gi == wifi_guard_interval_800)
2985 GI_duration = 8;
2986 else // auto, 400
2987 GI_duration = 4;
developer72fb0bb2023-01-11 09:46:29 +08002988
developera3511852023-06-14 14:12:59 +08002989 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, channel_bandwidth_str) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08002990 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error\n");
developera3511852023-06-14 14:12:59 +08002991 return RETURN_ERR;
2992 }
developer72fb0bb2023-01-11 09:46:29 +08002993
developera3511852023-06-14 14:12:59 +08002994 if (strstr(channel_bandwidth_str, "80+80") != NULL)
developer32f2a182023-06-27 19:50:41 +08002995 memcpy(channel_bandwidth_str, "160", strlen("160"));
developer72fb0bb2023-01-11 09:46:29 +08002996
developera3511852023-06-14 14:12:59 +08002997 if (mode_map & WIFI_MODE_AX) {
2998 if (strstr(channel_bandwidth_str, "160") != NULL)
2999 num_subcarrier = 1960;
3000 else if (strstr(channel_bandwidth_str, "80") != NULL)
3001 num_subcarrier = 980;
3002 else if (strstr(channel_bandwidth_str, "40") != NULL)
3003 num_subcarrier = 468;
3004 else if (strstr(channel_bandwidth_str, "20") != NULL)
3005 num_subcarrier = 234;
3006 code_bits = 10;
3007 code_rate = (float)5/6;
3008 Symbol_duration = 128;
3009 GI_duration = 8;/*HE no GI 400ns*/
3010 } else if (mode_map & WIFI_MODE_AC) {
3011 if (strstr(channel_bandwidth_str, "160") != NULL)
3012 num_subcarrier = 468;
3013 else if (strstr(channel_bandwidth_str, "80") != NULL)
3014 num_subcarrier = 234;
3015 else if (strstr(channel_bandwidth_str, "40") != NULL)
3016 num_subcarrier = 108;
3017 else if (strstr(channel_bandwidth_str, "20") != NULL)
3018 num_subcarrier = 52;
3019 code_bits = 8;
3020 code_rate = (float)5/6;
3021 Symbol_duration = 32;
3022 } else if (mode_map & WIFI_MODE_N) {
3023 if (strstr(channel_bandwidth_str, "160") != NULL)
3024 num_subcarrier = 468;
3025 else if (strstr(channel_bandwidth_str, "80") != NULL)
3026 num_subcarrier = 234;
3027 else if (strstr(channel_bandwidth_str, "40") != NULL)
3028 num_subcarrier = 108;
3029 else if (strstr(channel_bandwidth_str, "20") != NULL)
3030 num_subcarrier = 52;
3031 code_bits = 6;
3032 code_rate = (float)3/4;
3033 Symbol_duration = 32;
3034 } else if ((mode_map & WIFI_MODE_G || mode_map & WIFI_MODE_B) || mode_map & WIFI_MODE_A) {
3035 // mode b must run with mode g, so we output mode g bitrate in 2.4 G.
developere40952c2023-06-15 18:46:43 +08003036 res = snprintf(output_string, 64, "65 Mb/s");
3037 if (os_snprintf_error(64, res)) {
3038 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3039 return RETURN_ERR;
3040 }
developera3511852023-06-14 14:12:59 +08003041 return RETURN_OK;
3042 } else {
developere40952c2023-06-15 18:46:43 +08003043 res = snprintf(output_string, 64, "0 Mb/s");
3044 if (os_snprintf_error(64, res)) {
3045 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3046 return RETURN_ERR;
3047 }
developera3511852023-06-14 14:12:59 +08003048 return RETURN_OK;
3049 }
developer72fb0bb2023-01-11 09:46:29 +08003050
developera3511852023-06-14 14:12:59 +08003051 // Spatial streams
3052 if (wifi_getRadioTxChainMask(radioIndex, &ant_bitmap) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +08003053 wifi_debug(DEBUG_ERROR, "wifi_getRadioTxChainMask return error\n");
developera3511852023-06-14 14:12:59 +08003054 return RETURN_ERR;
3055 }
3056 for (; ant_bitmap > 0; ant_bitmap >>= 1)
3057 NSS += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08003058
developera3511852023-06-14 14:12:59 +08003059 // multiple 10 is to align duration unit (0.1 us)
3060 bit_rate = (num_subcarrier * code_bits * code_rate * NSS) / (Symbol_duration + GI_duration) * 10;
developere40952c2023-06-15 18:46:43 +08003061 res = snprintf(output_string, 64, "%.1f Mb/s", bit_rate);
3062 if (os_snprintf_error(64, res)) {
3063 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3064 return RETURN_ERR;
3065 }
developera3511852023-06-14 14:12:59 +08003066 WIFI_ENTRY_EXIT_DEBUG("%s:num_subcarrier=%d, code_bits=%d, code_rate=%.3f, nss=%d, symbol time=%u, %.1f Mb/s\n",
3067 __func__, num_subcarrier, code_bits, code_rate, NSS, Symbol_duration + GI_duration, bit_rate);
3068 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003069
developera3511852023-06-14 14:12:59 +08003070 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003071}
developer72fb0bb2023-01-11 09:46:29 +08003072
3073//Get Supported frequency bands at which the radio can operate. eg: "2.4GHz,5GHz"
3074//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.
3075INT wifi_getRadioSupportedFrequencyBands(INT radioIndex, CHAR *output_string) //RDKB
3076{
developera3511852023-06-14 14:12:59 +08003077 wifi_band band = band_invalid;
developer72fb0bb2023-01-11 09:46:29 +08003078
developera3511852023-06-14 14:12:59 +08003079 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3080 if (NULL == output_string)
3081 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003082
developera3511852023-06-14 14:12:59 +08003083 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003084
developera3511852023-06-14 14:12:59 +08003085 memset(output_string, 0, 10);
3086 if (band == band_2_4)
developer32f2a182023-06-27 19:50:41 +08003087 memcpy(output_string, "2.4GHz", strlen("2.4GHz"));
developera3511852023-06-14 14:12:59 +08003088 else if (band == band_5)
developer32f2a182023-06-27 19:50:41 +08003089 memcpy(output_string, "5GHz", strlen("5GHz"));
developera3511852023-06-14 14:12:59 +08003090 else if (band == band_6)
developer32f2a182023-06-27 19:50:41 +08003091 memcpy(output_string, "6GHz", strlen("6GHz"));
developera3511852023-06-14 14:12:59 +08003092 else
3093 return RETURN_ERR;
3094 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003095
developera3511852023-06-14 14:12:59 +08003096 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003097}
3098
3099//Get the frequency band at which the radio is operating, eg: "2.4GHz"
3100//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.
3101INT wifi_getRadioOperatingFrequencyBand(INT radioIndex, CHAR *output_string) //Tr181
3102{
developera3511852023-06-14 14:12:59 +08003103 wifi_band band = band_invalid;
developer9ce44382023-06-28 11:09:37 +08003104 int res = -1;
developere40952c2023-06-15 18:46:43 +08003105
developera3511852023-06-14 14:12:59 +08003106 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3107 if (NULL == output_string)
3108 return RETURN_ERR;
3109 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003110
developera3511852023-06-14 14:12:59 +08003111 if (band == band_2_4)
developere40952c2023-06-15 18:46:43 +08003112 res = snprintf(output_string, 64, "2.4GHz");
developera3511852023-06-14 14:12:59 +08003113 else if (band == band_5)
developere40952c2023-06-15 18:46:43 +08003114 res = snprintf(output_string, 64, "5GHz");
developera3511852023-06-14 14:12:59 +08003115 else if (band == band_6)
developere40952c2023-06-15 18:46:43 +08003116 res = snprintf(output_string, 64, "6GHz");
developer72fb0bb2023-01-11 09:46:29 +08003117
developere40952c2023-06-15 18:46:43 +08003118 if (os_snprintf_error(64, res)) {
3119 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3120 return RETURN_ERR;
3121 }
developera3511852023-06-14 14:12:59 +08003122 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003123
developera3511852023-06-14 14:12:59 +08003124 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003125}
3126
3127//Get the Supported Radio Mode. eg: "b,g,n"; "n,ac"
3128//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.
3129INT wifi_getRadioSupportedStandards(INT radioIndex, CHAR *output_string) //Tr181
3130{
developera3511852023-06-14 14:12:59 +08003131 char cmd[128]={0};
3132 char buf[128]={0};
3133 char temp_output[128] = {0};
3134 wifi_band band;
developere40952c2023-06-15 18:46:43 +08003135 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003136
developera3511852023-06-14 14:12:59 +08003137 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3138 if (NULL == output_string)
3139 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003140
developera3511852023-06-14 14:12:59 +08003141 band = wifi_index_to_band(radioIndex);
3142 if (band == band_2_4) {
developer32f2a182023-06-27 19:50:41 +08003143 strncat(temp_output, "b,g,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003144 } else if (band == band_5) {
developer32f2a182023-06-27 19:50:41 +08003145 strncat(temp_output, "a,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003146 }
3147 phyId = radio_index_to_phy(radioIndex);
3148 // ht capabilities
developere40952c2023-06-15 18:46:43 +08003149 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);
3150 if (os_snprintf_error(sizeof(cmd), res)) {
3151 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3152 return RETURN_ERR;
3153 }
developera3511852023-06-14 14:12:59 +08003154 _syscmd(cmd, buf, sizeof(buf));
3155 if (strlen(buf) >= 4 && strncmp(buf, "0x00", 4) != 0) {
developer32f2a182023-06-27 19:50:41 +08003156 if (strlen(temp_output) >= sizeof(temp_output) - 2)
3157 return RETURN_ERR;
3158 strncat(temp_output, "n,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003159 }
developer72fb0bb2023-01-11 09:46:29 +08003160
developera3511852023-06-14 14:12:59 +08003161 // vht capabilities
3162 if (band == band_5) {
developere40952c2023-06-15 18:46:43 +08003163 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'VHT Capabilities' | cut -d '(' -f2 | cut -c1-10 | tr -d '\\n'", phyId);
3164 if (os_snprintf_error(sizeof(cmd), res)) {
3165 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3166 return RETURN_ERR;
3167 }
3168 _syscmd(cmd, buf, sizeof(buf));
3169 if (strlen(buf) >= 10 && strncmp(buf, "0x00000000", 10) != 0) {
developer32f2a182023-06-27 19:50:41 +08003170 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3171 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08003172 strncat(temp_output, "ac,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003173 }
3174 }
developer72fb0bb2023-01-11 09:46:29 +08003175
developera3511852023-06-14 14:12:59 +08003176 // he capabilities
developere40952c2023-06-15 18:46:43 +08003177 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);
3178 if (os_snprintf_error(sizeof(cmd), res)) {
3179 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3180 return RETURN_ERR;
3181 }
developera3511852023-06-14 14:12:59 +08003182 _syscmd(cmd, buf, sizeof(buf));
3183 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003184 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3185 return RETURN_ERR;
3186 strncat(temp_output, "ax,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003187 }
developer72fb0bb2023-01-11 09:46:29 +08003188
developere82c0ca2023-05-10 16:25:35 +08003189 // eht capabilities
developere40952c2023-06-15 18:46:43 +08003190 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);
3191 if (os_snprintf_error(sizeof(cmd), res)) {
3192 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3193 return RETURN_ERR;
3194 }
developera3511852023-06-14 14:12:59 +08003195 _syscmd(cmd, buf, sizeof(buf));
3196 if (strlen(buf) >= 6 && strncmp (buf, "0x0000", 6) != 0) {
developer32f2a182023-06-27 19:50:41 +08003197 if (strlen(temp_output) >= sizeof(temp_output) - 3)
3198 return RETURN_ERR;
3199 strncat(temp_output, "be,", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08003200 }
developere82c0ca2023-05-10 16:25:35 +08003201
developera3511852023-06-14 14:12:59 +08003202 // Remove the last comma
3203 if (strlen(temp_output) != 0)
3204 temp_output[strlen(temp_output)-1] = '\0';
3205 strncpy(output_string, temp_output, strlen(temp_output));
3206 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3207 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003208}
3209
3210//Get the radio operating mode, and pure mode flag. eg: "ac"
3211//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.
3212INT wifi_getRadioStandard(INT radioIndex, CHAR *output_string, BOOL *gOnly, BOOL *nOnly, BOOL *acOnly) //RDKB
3213{
developere40952c2023-06-15 18:46:43 +08003214 int res;
3215
developera3511852023-06-14 14:12:59 +08003216 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3217 if (NULL == output_string)
3218 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003219
developera3511852023-06-14 14:12:59 +08003220 if (radioIndex == 0) {
developere40952c2023-06-15 18:46:43 +08003221 res = snprintf(output_string, 64, "n"); //"ht" needs to be translated to "n" or others
3222 if (os_snprintf_error(64, res)) {
3223 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3224 return RETURN_ERR;
3225 }
developera3511852023-06-14 14:12:59 +08003226 *gOnly = FALSE;
3227 *nOnly = TRUE;
3228 *acOnly = FALSE;
3229 } else {
developere40952c2023-06-15 18:46:43 +08003230 res = snprintf(output_string, 64, "ac"); //"vht" needs to be translated to "ac"
3231 if (os_snprintf_error(64, res)) {
3232 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3233 return RETURN_ERR;
3234 }
developera3511852023-06-14 14:12:59 +08003235 *gOnly = FALSE;
3236 *nOnly = FALSE;
3237 *acOnly = FALSE;
3238 }
3239 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003240
developera3511852023-06-14 14:12:59 +08003241 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003242}
3243
developer0f10c772023-05-16 21:43:39 +08003244enum WIFI_MODE {
3245 WMODE_INVALID = 0,
3246 WMODE_A = 1 << 0,
3247 WMODE_B = 1 << 1,
3248 WMODE_G = 1 << 2,
3249 WMODE_GN = 1 << 3,
3250 WMODE_AN = 1 << 4,
3251 WMODE_AC = 1 << 5,
3252 WMODE_AX_24G = 1 << 6,
3253 WMODE_AX_5G = 1 << 7,
3254 WMODE_AX_6G = 1 << 8,
3255 WMODE_BE_24G = 1 << 9,
3256 WMODE_BE_5G = 1 << 10,
3257 WMODE_BE_6G = 1 << 11,
3258 /*
3259 * total types of supported wireless mode,
3260 * add this value once yow add new type
3261 */
3262 WMODE_COMP = 12,
3263};
3264
3265#define RADIO_MODE_LEN 32
developerfead3972023-05-25 20:15:02 +08003266
3267int get_radio_mode_handler(struct nl_msg *msg, void *cb)
developer72fb0bb2023-01-11 09:46:29 +08003268{
developerfead3972023-05-25 20:15:02 +08003269 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3270 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08003271 struct genlmsghdr *gnlh;
developerfead3972023-05-25 20:15:02 +08003272 unsigned int *phymode;
3273 int err = 0;
3274 struct mtk_nl80211_cb_data *cb_data = cb;
developer72fb0bb2023-01-11 09:46:29 +08003275
developerfead3972023-05-25 20:15:02 +08003276 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08003277 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developerfead3972023-05-25 20:15:02 +08003278 return NL_SKIP;
3279 }
developerc14d83a2023-06-29 20:09:42 +08003280 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer72fb0bb2023-01-11 09:46:29 +08003281
developerfead3972023-05-25 20:15:02 +08003282 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3283 genlmsg_attrlen(gnlh, 0), NULL);
3284 if (err < 0) {
3285 wifi_debug(DEBUG_ERROR, "nla_parse radio nl80211 msg fails,error.\n");
3286 return NL_SKIP;
3287 }
developer0f10c772023-05-16 21:43:39 +08003288
developerfead3972023-05-25 20:15:02 +08003289 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3290 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
3291 tb[NL80211_ATTR_VENDOR_DATA], NULL);
3292 if (err < 0)
3293 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08003294
developerfead3972023-05-25 20:15:02 +08003295 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]) {
3296 phymode = (unsigned int *)nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE]);
3297
3298 memset(cb_data->out_buf, 0, cb_data->out_len);
3299 memmove(cb_data->out_buf, phymode, sizeof(unsigned int));
3300 }
3301 } else
3302 wifi_debug(DEBUG_ERROR, "No Stats from driver.\n");
3303
3304 return NL_OK;
3305}
developer0f10c772023-05-16 21:43:39 +08003306
developerfead3972023-05-25 20:15:02 +08003307void phymode_to_puremode(INT radioIndex, CHAR *output_string, UINT *pureMode, UINT phymode)
3308{
3309 wifi_band band;
3310 unsigned char radio_mode_tem_len;
developere40952c2023-06-15 18:46:43 +08003311 int res;
developerfead3972023-05-25 20:15:02 +08003312
3313 band = wifi_index_to_band(radioIndex);
developer0f10c772023-05-16 21:43:39 +08003314 // puremode is a bit map
developera3511852023-06-14 14:12:59 +08003315 *pureMode = 0;
developer0f10c772023-05-16 21:43:39 +08003316 memset(output_string, 0, RADIO_MODE_LEN);
3317
3318 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3319
3320 switch (band) {
3321 case band_2_4:
3322 if (phymode & WMODE_B) {
developere40952c2023-06-15 18:46:43 +08003323 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "b,");
3324 if (os_snprintf_error(radio_mode_tem_len, res)) {
3325 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3326 return;
3327 }
developer0f10c772023-05-16 21:43:39 +08003328 *pureMode |= WIFI_MODE_B;
3329 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3330 }
3331 if (phymode & WMODE_G) {
developere40952c2023-06-15 18:46:43 +08003332 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "g,");
3333 if (os_snprintf_error(radio_mode_tem_len, res)) {
3334 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3335 return;
3336 }
developer0f10c772023-05-16 21:43:39 +08003337 *pureMode |= WIFI_MODE_G;
3338 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3339 }
3340 if (phymode & WMODE_GN) {
developere40952c2023-06-15 18:46:43 +08003341 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3342 if (os_snprintf_error(radio_mode_tem_len, res)) {
3343 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3344 return;
3345 }
developer0f10c772023-05-16 21:43:39 +08003346 *pureMode |= WIFI_MODE_N;
3347 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3348 }
3349 if (phymode & WMODE_AX_24G) {
developere40952c2023-06-15 18:46:43 +08003350 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3351 if (os_snprintf_error(radio_mode_tem_len, res)) {
3352 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3353 return;
3354 }
developer0f10c772023-05-16 21:43:39 +08003355 *pureMode |= WIFI_MODE_AX;
3356 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3357 }
3358 if (phymode & WMODE_BE_24G) {
developere40952c2023-06-15 18:46:43 +08003359 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3360 if (os_snprintf_error(radio_mode_tem_len, res)) {
3361 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3362 return;
3363 }
developer0f10c772023-05-16 21:43:39 +08003364 *pureMode |= WIFI_MODE_BE;
3365 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3366 }
3367 break;
3368 case band_5:
3369 if (phymode & WMODE_A) {
developere40952c2023-06-15 18:46:43 +08003370 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "a,");
3371 if (os_snprintf_error(radio_mode_tem_len, res)) {
3372 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3373 return;
3374 }
developer0f10c772023-05-16 21:43:39 +08003375 *pureMode |= WIFI_MODE_A;
3376 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3377 }
3378 if (phymode & WMODE_AN) {
developere40952c2023-06-15 18:46:43 +08003379 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "n,");
3380 if (os_snprintf_error(radio_mode_tem_len, res)) {
3381 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3382 return;
3383 }
developer0f10c772023-05-16 21:43:39 +08003384 *pureMode |= WIFI_MODE_N;
3385 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3386 }
3387 if (phymode & WMODE_AC) {
developere40952c2023-06-15 18:46:43 +08003388 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ac,");
3389 if (os_snprintf_error(radio_mode_tem_len, res)) {
3390 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3391 return;
3392 }
developer0f10c772023-05-16 21:43:39 +08003393 *pureMode |= WIFI_MODE_AC;
3394 }
3395 if (phymode & WMODE_AX_5G) {
developere40952c2023-06-15 18:46:43 +08003396 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3397 if (os_snprintf_error(radio_mode_tem_len, res)) {
3398 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3399 return;
3400 }
developer0f10c772023-05-16 21:43:39 +08003401 *pureMode |= WIFI_MODE_AX;
3402 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3403 }
3404 if (phymode & WMODE_BE_5G) {
developere40952c2023-06-15 18:46:43 +08003405 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3406 if (os_snprintf_error(radio_mode_tem_len, res)) {
3407 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3408 return;
3409 }
developer0f10c772023-05-16 21:43:39 +08003410 *pureMode |= WIFI_MODE_BE;
3411 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3412 }
3413 break;
3414 case band_6:
3415 if (phymode & WMODE_AX_6G) {
developere40952c2023-06-15 18:46:43 +08003416 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "ax,");
3417 if (os_snprintf_error(radio_mode_tem_len, res)) {
3418 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3419 return;
3420 }
developer0f10c772023-05-16 21:43:39 +08003421 *pureMode |= WIFI_MODE_AX;
3422 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3423 }
3424 if (phymode & WMODE_BE_6G) {
developere40952c2023-06-15 18:46:43 +08003425 res = snprintf(output_string + strlen(output_string), radio_mode_tem_len, "%s", "be,");
3426 if (os_snprintf_error(radio_mode_tem_len, res)) {
3427 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3428 return;
3429 }
developer0f10c772023-05-16 21:43:39 +08003430 *pureMode |= WIFI_MODE_BE;
3431 radio_mode_tem_len = RADIO_MODE_LEN - strlen(output_string);
3432 }
3433 break;
3434 default:
developer86035662023-06-28 19:21:12 +08003435 wifi_debug(DEBUG_ERROR, "%s band_idx invalid\n", __func__);
developer0f10c772023-05-16 21:43:39 +08003436 break;
3437 }
3438
3439 /* Remove the last comma */
3440 if (strlen(output_string) != 0)
developera3511852023-06-14 14:12:59 +08003441 output_string[strlen(output_string)-1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +08003442
developerfead3972023-05-25 20:15:02 +08003443}
3444
3445INT wifi_getRadioMode(INT radioIndex, CHAR *output_string, UINT *pureMode)
3446{
3447 unsigned int phymode;
3448 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003449 int ret = -1;
3450 unsigned int if_idx = 0;
3451 struct unl unl_ins;
3452 struct nl_msg *msg = NULL;
3453 struct nlattr * msg_data = NULL;
3454 struct mtk_nl80211_param param;
3455 struct mtk_nl80211_cb_data cb_data;
3456
developera3511852023-06-14 14:12:59 +08003457 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3458 if (NULL == output_string || NULL == pureMode)
developerdaf24792023-06-06 11:40:04 +08003459 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003460
3461 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
3462 return RETURN_ERR;
3463
3464 if_idx = if_nametoindex(interface_name);
3465 if (!if_idx) {
3466 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
3467 return RETURN_ERR;
3468 }
3469 /*init mtk nl80211 vendor cmd*/
3470 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
3471 param.if_type = NL80211_ATTR_IFINDEX;
3472 param.if_idx = if_idx;
3473
3474 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3475 if (ret) {
3476 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3477 return RETURN_ERR;
3478 }
3479
3480 /*add mtk vendor cmd data*/
3481 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_WMODE, 0)) {
3482 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_WMODE attribute error\n");
3483 nlmsg_free(msg);
3484 goto err;
3485 }
3486
3487 /*send mtk nl80211 vendor msg*/
3488 cb_data.out_buf = (char *)&phymode;
3489 cb_data.out_len = sizeof(unsigned int);
3490
3491 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_radio_mode_handler, &cb_data);
3492
3493 if (ret) {
3494 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3495 goto err;
3496 }
3497 /*deinit mtk nl80211 vendor msg*/
3498 mtk_nl80211_deint(&unl_ins);
3499
3500 phymode_to_puremode(radioIndex, output_string, pureMode, phymode);
developer6e578302023-06-21 10:11:16 +08003501 wifi_debug(DEBUG_INFO,"send cmd success\n");
developerfead3972023-05-25 20:15:02 +08003502
developera3511852023-06-14 14:12:59 +08003503 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3504 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003505err:
3506 mtk_nl80211_deint(&unl_ins);
3507 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
3508 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003509}
3510
3511// Set the radio operating mode, and pure mode flag.
3512INT wifi_setRadioChannelMode(INT radioIndex, CHAR *channelMode, BOOL gOnlyFlag, BOOL nOnlyFlag, BOOL acOnlyFlag) //RDKB
3513{
developera3511852023-06-14 14:12:59 +08003514 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%s_%d_%d:%d\n",__func__,channelMode,nOnlyFlag,gOnlyFlag,__LINE__);
3515 if (strcmp (channelMode,"11A") == 0)
3516 {
3517 writeBandWidth(radioIndex,"20MHz");
3518 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3519 printf("\nChannel Mode is 802.11a (5GHz)\n");
3520 }
3521 else if (strcmp (channelMode,"11NAHT20") == 0)
3522 {
3523 writeBandWidth(radioIndex,"20MHz");
3524 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3525 printf("\nChannel Mode is 802.11n-20MHz(5GHz)\n");
3526 }
3527 else if (strcmp (channelMode,"11NAHT40PLUS") == 0)
3528 {
3529 writeBandWidth(radioIndex,"40MHz");
3530 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3531 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3532 }
3533 else if (strcmp (channelMode,"11NAHT40MINUS") == 0)
3534 {
3535 writeBandWidth(radioIndex,"40MHz");
3536 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3537 printf("\nChannel Mode is 802.11n-40MHz(5GHz)\n");
3538 }
3539 else if (strcmp (channelMode,"11ACVHT20") == 0)
3540 {
3541 writeBandWidth(radioIndex,"20MHz");
3542 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3543 printf("\nChannel Mode is 802.11ac-20MHz(5GHz)\n");
3544 }
3545 else if (strcmp (channelMode,"11ACVHT40PLUS") == 0)
3546 {
3547 writeBandWidth(radioIndex,"40MHz");
3548 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3549 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3550 }
3551 else if (strcmp (channelMode,"11ACVHT40MINUS") == 0)
3552 {
3553 writeBandWidth(radioIndex,"40MHz");
3554 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3555 printf("\nChannel Mode is 802.11ac-40MHz(5GHz)\n");
3556 }
3557 else if (strcmp (channelMode,"11ACVHT80") == 0)
3558 {
3559 wifi_setRadioOperatingChannelBandwidth(radioIndex,"80MHz");
3560 printf("\nChannel Mode is 802.11ac-80MHz(5GHz)\n");
3561 }
3562 else if (strcmp (channelMode,"11ACVHT160") == 0)
3563 {
3564 wifi_setRadioOperatingChannelBandwidth(radioIndex,"160MHz");
3565 printf("\nChannel Mode is 802.11ac-160MHz(5GHz)\n");
3566 }
3567 else if (strcmp (channelMode,"11B") == 0)
3568 {
3569 writeBandWidth(radioIndex,"20MHz");
3570 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3571 printf("\nChannel Mode is 802.11b(2.4GHz)\n");
3572 }
3573 else if (strcmp (channelMode,"11G") == 0)
3574 {
3575 writeBandWidth(radioIndex,"20MHz");
3576 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3577 printf("\nChannel Mode is 802.11g(2.4GHz)\n");
3578 }
3579 else if (strcmp (channelMode,"11NGHT20") == 0)
3580 {
3581 writeBandWidth(radioIndex,"20MHz");
3582 wifi_setRadioOperatingChannelBandwidth(radioIndex,"20MHz");
3583 printf("\nChannel Mode is 802.11n-20MHz(2.4GHz)\n");
3584 }
3585 else if (strcmp (channelMode,"11NGHT40PLUS") == 0)
3586 {
3587 writeBandWidth(radioIndex,"40MHz");
3588 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3589 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3590 }
3591 else if (strcmp (channelMode,"11NGHT40MINUS") == 0)
3592 {
3593 writeBandWidth(radioIndex,"40MHz");
3594 wifi_setRadioOperatingChannelBandwidth(radioIndex,"40MHz");
3595 printf("\nChannel Mode is 802.11n-40MHz(2.4GHz)\n");
3596 }
3597 else
3598 {
3599 return RETURN_ERR;
3600 }
3601 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003602
developera3511852023-06-14 14:12:59 +08003603 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003604}
3605
developer0f10c772023-05-16 21:43:39 +08003606typedef enum _RT_802_11_PHY_MODE {
3607 PHY_11BG_MIXED = 0,
3608 PHY_11B = 1,
3609 PHY_11A = 2,
3610 PHY_11ABG_MIXED = 3,
3611 PHY_11G = 4,
3612 PHY_11ABGN_MIXED = 5, /* both band 5 */
developera3511852023-06-14 14:12:59 +08003613 PHY_11N_2_4G = 6, /* 11n-only with 2.4G band 6 */
3614 PHY_11GN_MIXED = 7, /* 2.4G band 7 */
3615 PHY_11AN_MIXED = 8, /* 5G band 8 */
3616 PHY_11BGN_MIXED = 9, /* if check 802.11b. 9 */
3617 PHY_11AGN_MIXED = 10, /* if check 802.11b. 10 */
3618 PHY_11N_5G = 11, /* 11n-only with 5G band 11 */
developer0f10c772023-05-16 21:43:39 +08003619 PHY_11VHT_N_ABG_MIXED = 12, /* 12 -> AC/A/AN/B/G/GN mixed */
3620 PHY_11VHT_N_AG_MIXED = 13, /* 13 -> AC/A/AN/G/GN mixed */
3621 PHY_11VHT_N_A_MIXED = 14, /* 14 -> AC/AN/A mixed in 5G band */
3622 PHY_11VHT_N_MIXED = 15, /* 15 -> AC/AN mixed in 5G band */
3623 PHY_11AX_24G = 16,
3624 PHY_11AX_5G = 17,
3625 PHY_11AX_6G = 18,
3626 PHY_11AX_24G_6G = 19,
3627 PHY_11AX_5G_6G = 20,
3628 PHY_11AX_24G_5G_6G = 21,
3629 PHY_11BE_24G = 22,
3630 PHY_11BE_5G = 23,
3631 PHY_11BE_6G = 24,
3632 PHY_11BE_24G_6G = 25,
3633 PHY_11BE_5G_6G = 26,
3634 PHY_11BE_24G_5G_6G = 27,
3635 PHY_MODE_MAX,
3636} RT_802_11_PHY_MODE;
3637
3638unsigned int puremode_to_wireless_mode(INT radioIndex, UINT pureMode)
3639{
3640 int band_idx = 0;
developerfead3972023-05-25 20:15:02 +08003641 unsigned char wireless_mode = PHY_MODE_MAX;
developer0f10c772023-05-16 21:43:39 +08003642
3643 band_idx = radio_index_to_band(radioIndex);
3644
3645 switch (band_idx) {
3646 case band_2_4:
3647 if (pureMode == (WIFI_MODE_G | WIFI_MODE_N))
3648 wireless_mode = PHY_11GN_MIXED;
3649 if (pureMode == (WIFI_MODE_B | WIFI_MODE_G | WIFI_MODE_N))
3650 wireless_mode = PHY_11BGN_MIXED;
3651 if (pureMode & WIFI_MODE_AX)
3652 wireless_mode = PHY_11AX_24G;
3653 if (pureMode & WIFI_MODE_BE)
3654 wireless_mode = PHY_11BE_24G;
3655 break;
3656 case band_5:
3657 if (pureMode == WIFI_MODE_N)
3658 wireless_mode = PHY_11N_5G;
3659 if ((pureMode == WIFI_MODE_AC) || (pureMode == (WIFI_MODE_N | WIFI_MODE_AC)))
3660 wireless_mode = PHY_11VHT_N_MIXED;
3661 if (pureMode == (WIFI_MODE_A | WIFI_MODE_N | WIFI_MODE_AC))
3662 wireless_mode = PHY_11VHT_N_A_MIXED;
3663 if (pureMode & WIFI_MODE_AX)
3664 wireless_mode = PHY_11AX_5G;
3665 if (pureMode & WIFI_MODE_BE)
3666 wireless_mode = PHY_11BE_5G;
3667 break;
3668 case band_6:
3669 if (pureMode & WIFI_MODE_AX)
3670 wireless_mode = PHY_11AX_6G;
3671 if (pureMode & WIFI_MODE_BE)
3672 wireless_mode = PHY_11BE_6G;
3673 break;
3674 default:
developer37646972023-06-29 10:58:43 +08003675 if (fprintf(stderr, "%s band_idx invalid\n", __func__) < 0)
3676 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developer0f10c772023-05-16 21:43:39 +08003677 break;
3678 }
3679
3680 return wireless_mode;
3681}
3682
developer72fb0bb2023-01-11 09:46:29 +08003683// Set the radio operating mode, and pure mode flag.
3684INT wifi_setRadioMode(INT radioIndex, CHAR *channelMode, UINT pureMode)
3685{
developerfead3972023-05-25 20:15:02 +08003686 unsigned char wireless_mode = PHY_MODE_MAX;
developer69b61b02023-03-07 17:17:44 +08003687
developer0f10c772023-05-16 21:43:39 +08003688 char interface_name[IF_NAME_SIZE] = {0};
developerfead3972023-05-25 20:15:02 +08003689 int ret = -1;
3690 unsigned int if_idx = 0;
3691 struct unl unl_ins;
3692 struct nl_msg *msg = NULL;
3693 struct nlattr * msg_data = NULL;
3694 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +08003695
developer0f10c772023-05-16 21:43:39 +08003696 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, channelMode, pureMode, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003697
developer0f10c772023-05-16 21:43:39 +08003698 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
developer72fb0bb2023-01-11 09:46:29 +08003699
developera3511852023-06-14 14:12:59 +08003700 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003701 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003702 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003703 }
developer72fb0bb2023-01-11 09:46:29 +08003704
developer0f10c772023-05-16 21:43:39 +08003705 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08003706 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +08003707
3708 if_idx = if_nametoindex(interface_name);
3709 if (!if_idx) {
3710 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", interface_name);
3711 return RETURN_ERR;
3712 }
3713 /*init mtk nl80211 vendor cmd*/
3714 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
3715 param.if_type = NL80211_ATTR_IFINDEX;
3716 param.if_idx = if_idx;
3717
3718 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
3719 if (ret) {
3720 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
3721 return RETURN_ERR;
3722 }
3723
3724 /*add mtk vendor cmd data*/
3725 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_WIRELESS_MODE, wireless_mode)) {
3726 wifi_debug(DEBUG_ERROR, "Nla put AP_WIRELESS_MODE attribute error\n");
3727 nlmsg_free(msg);
3728 goto err;
3729 }
3730 /*send mtk nl80211 vendor msg*/
3731 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
3732 if (ret) {
3733 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
3734 goto err;
3735 }
3736 /*deinit mtk nl80211 vendor msg*/
3737 mtk_nl80211_deint(&unl_ins);
3738 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08003739
developera3511852023-06-14 14:12:59 +08003740 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer0f10c772023-05-16 21:43:39 +08003741
developera3511852023-06-14 14:12:59 +08003742 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08003743err:
3744 mtk_nl80211_deint(&unl_ins);
3745 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
3746 return RETURN_ERR;
developer0f10c772023-05-16 21:43:39 +08003747}
3748
3749INT wifi_setRadioMode_by_dat(INT radioIndex, UINT pureMode)
3750{
developerfead3972023-05-25 20:15:02 +08003751 unsigned char wireless_mode = PHY_MODE_MAX;
developera3511852023-06-14 14:12:59 +08003752 char buf[MAX_BUF_SIZE] = {0};
developer0f10c772023-05-16 21:43:39 +08003753 char dat_file[MAX_BUF_SIZE] = {0};
3754 struct params params={0};
developere40952c2023-06-15 18:46:43 +08003755 int res;
developer0f10c772023-05-16 21:43:39 +08003756
3757 WIFI_ENTRY_EXIT_DEBUG("Inside %s_%d:%d\n", __func__, pureMode, __LINE__);
3758
3759 wireless_mode = puremode_to_wireless_mode(radioIndex, pureMode);
3760
developera3511852023-06-14 14:12:59 +08003761 if (wireless_mode == PHY_MODE_MAX) {
developer75bd10c2023-06-27 11:34:08 +08003762 wifi_debug(DEBUG_ERROR, "invalid pureMode = %x\n", pureMode);
developer0f10c772023-05-16 21:43:39 +08003763 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08003764 }
developer0f10c772023-05-16 21:43:39 +08003765
3766 params.name = "WirelessMode";
developere40952c2023-06-15 18:46:43 +08003767 res = snprintf(buf, sizeof(buf), "%d", wireless_mode);
3768 if (os_snprintf_error(sizeof(buf), res)) {
3769 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3770 return RETURN_ERR;
3771 }
3772
developera3511852023-06-14 14:12:59 +08003773 params.value = buf;
developer0f10c772023-05-16 21:43:39 +08003774
developere40952c2023-06-15 18:46:43 +08003775 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
3776 if (os_snprintf_error(sizeof(dat_file), res)) {
3777 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3778 return RETURN_ERR;
3779 }
developera3511852023-06-14 14:12:59 +08003780 wifi_datfileWrite(dat_file, &params, 1);
developer0f10c772023-05-16 21:43:39 +08003781
developera3511852023-06-14 14:12:59 +08003782 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003783
developera3511852023-06-14 14:12:59 +08003784 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003785}
3786
3787INT wifi_setRadioHwMode(INT radioIndex, CHAR *hw_mode) {
3788
developera3511852023-06-14 14:12:59 +08003789 char config_file[64] = {0};
3790 char buf[64] = {0};
3791 struct params params = {0};
3792 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08003793 int res;
developer72fb0bb2023-01-11 09:46:29 +08003794
developera3511852023-06-14 14:12:59 +08003795 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003796
developera3511852023-06-14 14:12:59 +08003797 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08003798
developera3511852023-06-14 14:12:59 +08003799 if (strncmp(hw_mode, "a", 1) == 0 && (band != band_5 && band != band_6))
3800 return RETURN_ERR;
3801 else if ((strncmp(hw_mode, "b", 1) == 0 || strncmp(hw_mode, "g", 1) == 0) && band != band_2_4)
3802 return RETURN_ERR;
3803 else if ((strncmp(hw_mode, "a", 1) && strncmp(hw_mode, "b", 1) && strncmp(hw_mode, "g", 1)) || band == band_invalid)
3804 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003805
developer75bd10c2023-06-27 11:34:08 +08003806 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3807 if (os_snprintf_error(sizeof(config_file), res)) {
3808 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3809 return RETURN_ERR;
3810 }
developera3511852023-06-14 14:12:59 +08003811 params.name = "hw_mode";
3812 params.value = hw_mode;
3813 wifi_hostapdWrite(config_file, &params, 1);
3814 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003815
developera3511852023-06-14 14:12:59 +08003816 if (band == band_2_4) {
3817 if (strncmp(hw_mode, "b", 1) == 0) {
3818 wifi_setRadioMode(radioIndex, "20MHz", WIFI_MODE_B);
developere40952c2023-06-15 18:46:43 +08003819 res = snprintf(buf, sizeof(buf), "%s", "1,2,5.5,11");
3820 if (os_snprintf_error(sizeof(buf), res)) {
3821 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3822 return RETURN_ERR;
3823 }
developera3511852023-06-14 14:12:59 +08003824 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003825 res = snprintf(buf, sizeof(buf), "%s", "1,2");
3826 if (os_snprintf_error(sizeof(buf), res)) {
3827 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3828 return RETURN_ERR;
3829 }
developera3511852023-06-14 14:12:59 +08003830 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3831 } else {
3832 // 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 +08003833
developere40952c2023-06-15 18:46:43 +08003834 res = snprintf(buf, sizeof(buf), "%s", "6,9,12,18,24,36,48,54");
3835 if (os_snprintf_error(sizeof(buf), res)) {
3836 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3837 return RETURN_ERR;
3838 }
developera3511852023-06-14 14:12:59 +08003839 wifi_setRadioOperationalDataTransmitRates(radioIndex, buf);
developere40952c2023-06-15 18:46:43 +08003840 res = snprintf(buf, sizeof(buf), "%s", "6,12,24");
3841 if (os_snprintf_error(sizeof(buf), res)) {
3842 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3843 return RETURN_ERR;
3844 }
developera3511852023-06-14 14:12:59 +08003845 wifi_setRadioBasicDataTransmitRates(radioIndex, buf);
3846 }
3847 }
developer72fb0bb2023-01-11 09:46:29 +08003848
developera3511852023-06-14 14:12:59 +08003849 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3850 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003851}
3852
3853INT wifi_setNoscan(INT radioIndex, CHAR *noscan)
3854{
developera3511852023-06-14 14:12:59 +08003855 char config_file[64] = {0};
3856 struct params params = {0};
3857 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08003858 int res;
developer72fb0bb2023-01-11 09:46:29 +08003859
developera3511852023-06-14 14:12:59 +08003860 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003861
developera3511852023-06-14 14:12:59 +08003862 band = wifi_index_to_band(radioIndex);
3863 if (band != band_2_4)
3864 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003865
developer75bd10c2023-06-27 11:34:08 +08003866 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
3867 if (os_snprintf_error(sizeof(config_file), res)) {
3868 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3869 return RETURN_ERR;
3870 }
developera3511852023-06-14 14:12:59 +08003871 params.name = "noscan";
3872 params.value = noscan;
3873 wifi_hostapdWrite(config_file, &params, 1);
3874 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08003875
developera3511852023-06-14 14:12:59 +08003876 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3877 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003878}
3879
3880//Get the list of supported channel. eg: "1-11"
3881//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.
3882INT wifi_getRadioPossibleChannels(INT radioIndex, CHAR *output_string) //RDKB
3883{
developera3511852023-06-14 14:12:59 +08003884 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
3885 if (NULL == output_string)
3886 return RETURN_ERR;
3887 char cmd[256] = {0};
3888 char buf[128] = {0};
3889 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +08003890 int phyId = 0, res;
developer72fb0bb2023-01-11 09:46:29 +08003891
developera3511852023-06-14 14:12:59 +08003892 // Parse possible channel number and separate them with commas.
3893 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
3894 phyId = radio_index_to_phy(radioIndex);
3895 // Channel 68 and 96 only allow bandwidth 20MHz, so we remove them with their frequency.
3896 if (dfs_enable)
developere40952c2023-06-15 18:46:43 +08003897 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 +08003898 else
developere40952c2023-06-15 18:46:43 +08003899 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 +08003900
developere40952c2023-06-15 18:46:43 +08003901 if (os_snprintf_error(sizeof(cmd), res)) {
3902 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3903 return RETURN_ERR;
3904 }
developera3511852023-06-14 14:12:59 +08003905 _syscmd(cmd,buf,sizeof(buf));
3906 strncpy(output_string, buf, strlen(buf) < sizeof(buf) ? strlen(buf) : sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08003907
developera3511852023-06-14 14:12:59 +08003908 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
3909 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08003910}
developerd1824452023-05-18 12:30:04 +08003911//Getting current radio extension channel
3912INT wifi_halgetRadioExtChannel(CHAR *file,CHAR *Value)
3913{
developera3511852023-06-14 14:12:59 +08003914 CHAR buf[150] = {0};
developer32f2a182023-06-27 19:50:41 +08003915 int len;
developerd1824452023-05-18 12:30:04 +08003916
developera3511852023-06-14 14:12:59 +08003917 wifi_datfileRead(file, "HT_EXTCHA", buf, sizeof(buf));
developerb14b3462023-07-01 18:02:42 +08003918
3919 if (strncmp(buf, "0", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08003920 len = strlen("BelowControlChannel");
3921 memcpy(Value, "BelowControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08003922 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08003923 }
developerb14b3462023-07-01 18:02:42 +08003924 else if(strncmp(buf, "1", 1) == 0) {
developer32f2a182023-06-27 19:50:41 +08003925 len = strlen("AboveControlChannel");
3926 memcpy(Value, "AboveControlChannel", len);
developerc14d83a2023-06-29 20:09:42 +08003927 Value[len] = '\0';
developer32f2a182023-06-27 19:50:41 +08003928 }
developerc14d83a2023-06-29 20:09:42 +08003929
developera3511852023-06-14 14:12:59 +08003930 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +08003931}
developerf6a87542023-05-16 15:47:28 +08003932
developer72fb0bb2023-01-11 09:46:29 +08003933//Get the list for used channel. eg: "1,6,9,11"
3934//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.
3935INT wifi_getRadioChannelsInUse(INT radioIndex, CHAR *output_string) //RDKB
3936{
developera3511852023-06-14 14:12:59 +08003937 char interface_name[16] = {0};
3938 char cmd[128] = {0};
3939 char buf[128] = {0};
3940 char config_file[64] = {0};
3941 int channel = 0;
3942 int freq = 0;
3943 int bandwidth = 0;
3944 int center_freq = 0;
3945 int center_channel = 0;
3946 int channel_delta = 0;
3947 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08003948 int res;
developer72fb0bb2023-01-11 09:46:29 +08003949
developera3511852023-06-14 14:12:59 +08003950 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08003951
developera3511852023-06-14 14:12:59 +08003952 if (NULL == output_string)
3953 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08003954
developera3511852023-06-14 14:12:59 +08003955 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
3956 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08003957
3958 res = snprintf(cmd, sizeof(cmd), "iw %s info | grep channel | sed -e 's/[^0-9 ]//g'", interface_name);
3959 if (os_snprintf_error(sizeof(cmd), res)) {
3960 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3961 return RETURN_ERR;
3962 }
developera3511852023-06-14 14:12:59 +08003963 _syscmd(cmd, buf, sizeof(buf));
3964 if (strlen(buf) == 0) {
developer75bd10c2023-06-27 11:34:08 +08003965 wifi_debug(DEBUG_ERROR, "failed to get channel information from iw.\n");
developera3511852023-06-14 14:12:59 +08003966 return RETURN_ERR;
3967 }
developerd14dff12023-06-28 22:47:44 +08003968 if (sscanf(buf, "%d %d %d %*d %d", &channel, &freq, &bandwidth, &center_freq) != 4) {
3969 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
3970 return RETURN_ERR;
3971 }
developer72fb0bb2023-01-11 09:46:29 +08003972
developera3511852023-06-14 14:12:59 +08003973 if (bandwidth == 20) {
developere40952c2023-06-15 18:46:43 +08003974 res = snprintf(output_string, 256, "%d", channel);
3975 if (os_snprintf_error(256, res)) {
3976 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3977 return RETURN_ERR;
3978 }
developera3511852023-06-14 14:12:59 +08003979 return RETURN_OK;
3980 }
developer72fb0bb2023-01-11 09:46:29 +08003981
developera3511852023-06-14 14:12:59 +08003982 center_channel = ieee80211_frequency_to_channel(center_freq);
developer72fb0bb2023-01-11 09:46:29 +08003983
developera3511852023-06-14 14:12:59 +08003984 band = wifi_index_to_band(radioIndex);
3985 if (band == band_2_4 && bandwidth == 40) {
developer32f2a182023-06-27 19:50:41 +08003986 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
3987 if (os_snprintf_error(sizeof(config_file), res)) {
3988 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
3989 return RETURN_ERR;
3990 }
developera3511852023-06-14 14:12:59 +08003991 memset(buf, 0, sizeof(buf));
3992 wifi_halgetRadioExtChannel(config_file, buf); // read ht_capab for HT40+ or -
developer72fb0bb2023-01-11 09:46:29 +08003993
developera3511852023-06-14 14:12:59 +08003994 if (strncmp(buf, "AboveControlChannel", strlen("AboveControlChannel")) == 0 && channel < 10) {
developere40952c2023-06-15 18:46:43 +08003995 res = snprintf(output_string, 256, "%d,%d", channel, channel+4);
developera3511852023-06-14 14:12:59 +08003996 } else if (strncmp(buf, "BelowControlChannel", strlen("BelowControlChannel")) == 0 && channel > 4) {
developere40952c2023-06-15 18:46:43 +08003997 res = snprintf(output_string, 256, "%d,%d", channel-4, channel);
developera3511852023-06-14 14:12:59 +08003998 } else {
developer37646972023-06-29 10:58:43 +08003999 if (fprintf(stderr, "%s: invalid channel %d set with %s\n.", __func__, channel, buf) < 0)
4000 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08004001 return RETURN_ERR;
4002 }
developerb758dfd2023-06-21 17:32:07 +08004003
developere40952c2023-06-15 18:46:43 +08004004 if (os_snprintf_error(256, res)) {
4005 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4006 return RETURN_ERR;
4007 }
developera3511852023-06-14 14:12:59 +08004008 } else if (band == band_5 || band == band_6){
4009 // to minus 20 is an offset, because frequence of a channel have a range. We need to use offset to calculate correct channel.
4010 // example: bandwidth 80: center is 42 (5210), channels are "36,40,44,48" (5170-5250). The delta should be 6.
4011 channel_delta = (bandwidth-20)/10;
4012 memset(output_string, 0, 256);
4013 for (int i = center_channel-channel_delta; i <= center_channel+channel_delta; i+=4) {
4014 // If i is not the last channel, we add a comma.
developere40952c2023-06-15 18:46:43 +08004015 res = snprintf(buf, sizeof(buf), "%d%s", i, i==center_channel+channel_delta?"":",");
developer32f2a182023-06-27 19:50:41 +08004016
developere40952c2023-06-15 18:46:43 +08004017 if (os_snprintf_error(sizeof(buf), res)) {
4018 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4019 return RETURN_ERR;
4020 }
developer32f2a182023-06-27 19:50:41 +08004021 strncat(output_string, buf, sizeof(output_string) - strlen(output_string) - 1);
developera3511852023-06-14 14:12:59 +08004022 }
4023 } else
4024 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004025
developera3511852023-06-14 14:12:59 +08004026 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4027 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004028}
4029
developer69b61b02023-03-07 17:17:44 +08004030//Get the running channel number
developerd1824452023-05-18 12:30:04 +08004031INT wifi_getRadioChannel(INT radioIndex, ULONG *output_ulong) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004032{
developera3511852023-06-14 14:12:59 +08004033 char channel_str[16] = {0};
4034 char config_file[128] = {0};
4035 char buf[MAX_BUF_SIZE] = {0};
4036 char cmd[MAX_CMD_SIZE] = {0};
4037 char interface_name[IF_NAME_SIZE] = {0};
4038 wifi_band band = band_invalid;
4039 ULONG iwChannel = 0;
developere40952c2023-06-15 18:46:43 +08004040 int res;
developer47a56bf2023-05-30 13:38:57 +08004041
developera3511852023-06-14 14:12:59 +08004042 if (output_ulong == NULL)
4043 return RETURN_ERR;
4044 band = wifi_index_to_band(radioIndex);
developerb758dfd2023-06-21 17:32:07 +08004045 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08004046 if (os_snprintf_error(sizeof(config_file), res)) {
4047 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4048 return RETURN_ERR;
4049 }
4050
developera3511852023-06-14 14:12:59 +08004051 wifi_datfileRead(config_file, "Channel", channel_str, sizeof(channel_str));
developerb14b3462023-07-01 18:02:42 +08004052 if (hal_strtoul(channel_str, 10, output_ulong) < 0) {
developerc14d83a2023-06-29 20:09:42 +08004053 wifi_debug(DEBUG_ERROR, "strtol fail\n");
4054 }
developera3511852023-06-14 14:12:59 +08004055 if (*output_ulong == 0) {
4056 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4057 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004058 res = snprintf(cmd, sizeof(cmd), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4059 if (os_snprintf_error(sizeof(cmd), res)) {
4060 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4061 return RETURN_ERR;
4062 }
4063
developera3511852023-06-14 14:12:59 +08004064 _syscmd(cmd,buf,sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08004065 if (sscanf(buf, "%lu", &iwChannel) != 1) {
4066 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
4067 return RETURN_ERR;
4068 }
developera3511852023-06-14 14:12:59 +08004069 *output_ulong = iwChannel;
4070 }
developer72fb0bb2023-01-11 09:46:29 +08004071
developera3511852023-06-14 14:12:59 +08004072 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004073}
4074
developer72fb0bb2023-01-11 09:46:29 +08004075INT wifi_getApChannel(INT apIndex,ULONG *output_ulong) //RDKB
4076{
developera3511852023-06-14 14:12:59 +08004077 char cmd[1024] = {0}, buf[5] = {0};
4078 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08004079 int res;
developer72fb0bb2023-01-11 09:46:29 +08004080
developera3511852023-06-14 14:12:59 +08004081 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4082 if (NULL == output_ulong)
4083 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004084
developera3511852023-06-14 14:12:59 +08004085 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
4086 return RETURN_ERR;
developer47a56bf2023-05-30 13:38:57 +08004087
developere40952c2023-06-15 18:46:43 +08004088 res = snprintf(cmd, sizeof(cmd), "iw dev %s info |grep channel | cut -d ' ' -f2", interface_name);
4089 if (os_snprintf_error(sizeof(cmd), res)) {
4090 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4091 return RETURN_ERR;
4092 }
developera3511852023-06-14 14:12:59 +08004093 _syscmd(cmd,buf,sizeof(buf));
4094 *output_ulong = (strlen(buf) >= 1)? atol(buf): 0;
4095 if (*output_ulong == 0) {
4096 return RETURN_ERR;
4097 }
developer72fb0bb2023-01-11 09:46:29 +08004098
developera3511852023-06-14 14:12:59 +08004099 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4100 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004101}
developer72fb0bb2023-01-11 09:46:29 +08004102//Storing the previous channel value
4103INT wifi_storeprevchanval(INT radioIndex)
4104{
developera3511852023-06-14 14:12:59 +08004105 char output[4]={'\0'};
4106 char config_file[MAX_BUF_SIZE] = {0};
4107 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004108 int res;
developerd1824452023-05-18 12:30:04 +08004109
developera3511852023-06-14 14:12:59 +08004110 band = wifi_index_to_band(radioIndex);
4111 if (band == band_invalid) {
4112 return RETURN_ERR;
4113 wifi_dbg_printf("[%s]: Invalid radio index", __func__);
4114 }
developere40952c2023-06-15 18:46:43 +08004115 res = snprintf(config_file, sizeof(config_file), "%s%d.dat",LOGAN_DAT_FILE, band);
4116 if (os_snprintf_error(sizeof(config_file), res)) {
4117 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4118 return RETURN_ERR;
4119 }
4120
developera3511852023-06-14 14:12:59 +08004121 wifi_datfileRead(config_file, "Channel", output, sizeof(output));
developerd1824452023-05-18 12:30:04 +08004122
developera3511852023-06-14 14:12:59 +08004123 if(band == band_2_4)
developer33f13ba2023-07-12 16:19:06 +08004124 res = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004125 else if(band == band_5)
developer33f13ba2023-07-12 16:19:06 +08004126 res = v_secure_system("echo %s > /var/prevchanval5G_AutoChannelEnable", output);
developera3511852023-06-14 14:12:59 +08004127 else
developer33f13ba2023-07-12 16:19:06 +08004128 res = v_secure_system("echo %s > /var/prevchanval6G_AutoChannelEnable", output);
developer75bd10c2023-06-27 11:34:08 +08004129
developer33f13ba2023-07-12 16:19:06 +08004130 if (res) {
4131 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08004132 }
developera3511852023-06-14 14:12:59 +08004133 Radio_flag = FALSE;
4134 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004135}
4136
4137//Set the running channel number
4138INT wifi_setRadioChannel(INT radioIndex, ULONG channel) //RDKB //AP only
4139{
developera3511852023-06-14 14:12:59 +08004140 // We only write hostapd config here
4141 char str_channel[8]={0};
4142 char *list_channel;
4143 char possible_channels[256] = {0};
4144 char config_file_dat[128] = {0};
4145 struct params dat = {0};
4146 struct params acs = {0};
4147 wifi_band band = band_invalid;
4148 bool acs_channel = false;
developere40952c2023-06-15 18:46:43 +08004149 int res;
developer72fb0bb2023-01-11 09:46:29 +08004150
developera3511852023-06-14 14:12:59 +08004151 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004152
developera3511852023-06-14 14:12:59 +08004153 if (channel == 0)
4154 acs_channel = true;
4155 // Check valid
developer75bd10c2023-06-27 11:34:08 +08004156 res = snprintf(str_channel, sizeof(str_channel), "%lu", channel);
4157 if (os_snprintf_error(sizeof(str_channel), res)) {
4158 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4159 return RETURN_ERR;
4160 }
developer47a56bf2023-05-30 13:38:57 +08004161
developera3511852023-06-14 14:12:59 +08004162 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4163 list_channel = strtok(possible_channels, ",");
4164 while(true)
4165 {
4166 if(list_channel == NULL) { // input not in the list
developer75bd10c2023-06-27 11:34:08 +08004167 wifi_debug(DEBUG_ERROR, "Channel %s is not in possible list\n", str_channel);
developera3511852023-06-14 14:12:59 +08004168 return RETURN_ERR;
4169 }
4170 if (strncmp(str_channel, list_channel, strlen(list_channel)) == 0 || strncmp(str_channel, "0", 1) == 0)
4171 break;
4172 list_channel = strtok(NULL, ",");
4173 }
4174 /*
4175 list.name = "channel";
4176 list.value = str_channel;
4177 wifi_getMaxRadioNumber(&max_radio_num);
4178 for(int i=0; i<=MAX_APS/max_radio_num;i++)
4179 {
4180 sprintf(config_file, "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_radio_num*i));
4181 wifi_hostapdWrite(config_file, &list, 1);
4182 }
4183 */
4184 dat.name = "Channel";
4185 dat.value = str_channel;
4186 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004187 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4188 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4189 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4190 return RETURN_ERR;
4191 }
4192
developera3511852023-06-14 14:12:59 +08004193 wifi_datfileWrite(config_file_dat, &dat, 1);
4194 if (acs_channel == true) {
4195 acs.name = "AutoChannelSelect";
4196 acs.value = "3";
4197 } else {
4198 acs.name = "AutoChannelSelect";
4199 acs.value = "0";
4200 }
4201 wifi_datfileWrite(config_file_dat, &acs, 1);
4202 wifi_reloadAp(radioIndex);
4203 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
4204 return RETURN_OK;
4205}
4206
4207INT wifi_setRadioCenterChannel(INT radioIndex, ULONG channel)
4208{
4209 struct params list[2];
4210 char str_idx[16];
4211 char config_file[64];
developere40952c2023-06-15 18:46:43 +08004212 int max_num_radios = 0, res;
developera3511852023-06-14 14:12:59 +08004213 wifi_band band = band_invalid;
4214
4215 band = wifi_index_to_band(radioIndex);
4216 if (band == band_2_4)
4217 return RETURN_OK;
4218
developere40952c2023-06-15 18:46:43 +08004219 res = snprintf(str_idx, sizeof(str_idx), "%lu", channel);
4220 if (os_snprintf_error(sizeof(str_idx), res)) {
4221 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4222 return RETURN_ERR;
4223 }
4224
developera3511852023-06-14 14:12:59 +08004225 list[0].name = "vht_oper_centr_freq_seg0_idx";
4226 list[0].value = str_idx;
4227 list[1].name = "he_oper_centr_freq_seg0_idx";
4228 list[1].value = str_idx;
4229
4230 wifi_getMaxRadioNumber(&max_num_radios);
developer9ce44382023-06-28 11:09:37 +08004231 if(max_num_radios== 0){
4232 return RETURN_ERR;
4233 }
developera3511852023-06-14 14:12:59 +08004234 for(int i=0; i<=MAX_APS/max_num_radios; i++)
4235 {
developere40952c2023-06-15 18:46:43 +08004236 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex+(max_num_radios*i));
4237 if (os_snprintf_error(sizeof(config_file), res)) {
4238 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4239 return RETURN_ERR;
4240 }
developera3511852023-06-14 14:12:59 +08004241 if (band == band_6)
4242 wifi_hostapdWrite(config_file, &list[1], 1);
4243 else
4244 wifi_hostapdWrite(config_file, list, 2);
4245 }
4246
4247 return RETURN_OK;
4248}
4249
4250//Enables or disables a driver level variable to indicate if auto channel selection is enabled on this radio
4251//This "auto channel" means the auto channel selection when radio is up. (which is different from the dynamic channel/frequency selection (DFC/DCS))
4252INT wifi_setRadioAutoChannelEnable(INT radioIndex, BOOL enable) //RDKB
4253{
4254 //Set to wifi config only. Wait for wifi reset to apply.
4255 ULONG Value = 0;
4256 char config_file_dat[128] = {0};
4257 struct params acs = {0};
4258 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004259 int res;
developera3511852023-06-14 14:12:59 +08004260
4261 if(enable == TRUE) {
4262 wifi_setRadioChannel(radioIndex,Value);
4263 } else {
4264 acs.name = "AutoChannelSelect";
4265 acs.value = "0";
4266 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004267 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4268 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4269 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4270 return RETURN_ERR;
4271 }
developera3511852023-06-14 14:12:59 +08004272 wifi_datfileWrite(config_file_dat, &acs, 1);
4273 }
4274 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004275}
4276
4277INT wifi_getRadioAutoChannelSupported(INT radioIndex, BOOL *output_bool)
4278{
developera3511852023-06-14 14:12:59 +08004279 if (output_bool == NULL)
4280 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004281
developera3511852023-06-14 14:12:59 +08004282 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004283
developera3511852023-06-14 14:12:59 +08004284 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004285}
4286
4287INT wifi_getRadioDCSSupported(INT radioIndex, BOOL *output_bool) //RDKB
4288{
developera3511852023-06-14 14:12:59 +08004289 if (NULL == output_bool)
4290 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004291 *output_bool=TRUE;
developera3511852023-06-14 14:12:59 +08004292 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004293}
4294
4295INT wifi_getRadioDCSEnable(INT radioIndex, BOOL *output_bool) //RDKB
4296{
developer326d4232023-06-15 16:45:30 +08004297 unsigned long period = 0;
4298
developera3511852023-06-14 14:12:59 +08004299 if (NULL == output_bool)
4300 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004301
4302 if (wifi_getRadioAutoChannelRefreshPeriod(radioIndex, &period) != RETURN_OK)
developerb758dfd2023-06-21 17:32:07 +08004303 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004304
4305 *output_bool = (period > 0) ? TRUE : FALSE;
4306
developera3511852023-06-14 14:12:59 +08004307 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004308}
4309
developer326d4232023-06-15 16:45:30 +08004310INT wifi_setRadioDCSEnable(INT radioIndex, BOOL enable) //RDKB
developer72fb0bb2023-01-11 09:46:29 +08004311{
developer326d4232023-06-15 16:45:30 +08004312 ULONG period = 1800;
4313
4314 if (enable == TRUE) {
4315 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, period) != RETURN_OK)
4316 return RETURN_ERR;
4317 }
4318 else {
4319 if (wifi_setRadioAutoChannelRefreshPeriod(radioIndex, 0) != RETURN_OK)
4320 return RETURN_ERR;
4321 }
developera3511852023-06-14 14:12:59 +08004322 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004323}
4324
4325INT wifi_setApEnableOnLine(ULONG wlanIndex,BOOL enable)
4326{
developera3511852023-06-14 14:12:59 +08004327 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004328}
4329
4330INT wifi_factoryResetAP(int apIndex)
4331{
developerb149d9d2023-06-06 16:14:22 +08004332 char ap_config_file[MAX_SUB_CMD_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08004333 char cmd[MAX_CMD_SIZE] = {0};
4334 char ret_buf[MAX_BUF_SIZE] = {0};
4335 int radio_idx = 0;
4336 int bss_idx = 0;
4337 char ssid[32] = {0};
4338 char interface[IF_NAME_SIZE] = {0};
developerb149d9d2023-06-06 16:14:22 +08004339 char psk_file[MAX_SUB_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08004340 struct params params[3] = {0};
developere40952c2023-06-15 18:46:43 +08004341 int res;
developer72fb0bb2023-01-11 09:46:29 +08004342
developera3511852023-06-14 14:12:59 +08004343 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004344
developer47cc27a2023-05-17 23:09:58 +08004345 /*del old config file*/
developer9ce44382023-06-28 11:09:37 +08004346 res = snprintf(ap_config_file, sizeof(ap_config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
developerd14dff12023-06-28 22:47:44 +08004347 if (os_snprintf_error(sizeof(ap_config_file), res)) {
developere40952c2023-06-15 18:46:43 +08004348 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4349 return RETURN_ERR;
4350 }
4351
4352 res = snprintf(cmd, MAX_CMD_SIZE, "rm %s", ap_config_file);
4353 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4354 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4355 return RETURN_ERR;
4356 }
4357
developer47cc27a2023-05-17 23:09:58 +08004358 _syscmd(cmd, ret_buf, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004359
developer47cc27a2023-05-17 23:09:58 +08004360 memset(cmd, 0, sizeof(cmd));
4361 memset(ret_buf, 0, sizeof(ret_buf));
developer72fb0bb2023-01-11 09:46:29 +08004362
developer47cc27a2023-05-17 23:09:58 +08004363 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
4364
4365 /*prepare new config file*/
developere40952c2023-06-15 18:46:43 +08004366 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[radio_idx], ap_config_file);
4367 if (os_snprintf_error(sizeof(cmd), res)) {
4368 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4369 return RETURN_ERR;
4370 }
4371
developer47cc27a2023-05-17 23:09:58 +08004372 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4373
4374 if (radio_idx == band_2_4) {
developere40952c2023-06-15 18:46:43 +08004375 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_2G, bss_idx);
4376 if (os_snprintf_error(sizeof(ssid), res)) {
4377 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4378 return RETURN_ERR;
4379 }
4380
4381 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI2G, bss_idx);
4382 if (os_snprintf_error(sizeof(interface), res)) {
4383 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4384 return RETURN_ERR;
4385 }
developer47cc27a2023-05-17 23:09:58 +08004386 } else if (radio_idx == band_5) {
developere40952c2023-06-15 18:46:43 +08004387 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_5G, bss_idx);
4388 if (os_snprintf_error(sizeof(ssid), res)) {
4389 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4390 return RETURN_ERR;
4391 }
4392
4393 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI5G, bss_idx);
4394 if (os_snprintf_error(sizeof(interface), res)) {
4395 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4396 return RETURN_ERR;
4397 }
developer47cc27a2023-05-17 23:09:58 +08004398 } else if (radio_idx == band_6) {
developere40952c2023-06-15 18:46:43 +08004399 res = snprintf(ssid, sizeof(ssid), "%s_%d", PREFIX_SSID_6G, bss_idx);
4400 if (os_snprintf_error(sizeof(ssid), res)) {
4401 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4402 return RETURN_ERR;
4403 }
4404
4405 res = snprintf(interface, sizeof(interface), "%s%d", PREFIX_WIFI6G, bss_idx);
4406 if (os_snprintf_error(sizeof(interface), res)) {
4407 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4408 return RETURN_ERR;
4409 }
developer47cc27a2023-05-17 23:09:58 +08004410 }
4411
4412 /* fix wpa_psk_file path */
developere40952c2023-06-15 18:46:43 +08004413 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", apIndex);
4414 if (os_snprintf_error(sizeof(psk_file), res)) {
4415 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4416 return RETURN_ERR;
4417 }
developer47cc27a2023-05-17 23:09:58 +08004418
4419 params[0].name = "ssid";
4420 params[0].value = ssid;
4421 params[1].name = "interface";
4422 params[1].value = interface;
4423 params[2].name = "wpa_psk_file";
4424 params[2].value = psk_file;
4425
4426 wifi_hostapdWrite(ap_config_file, params, 3);
4427
4428 /*clear psk file*/
4429 memset(cmd, 0, sizeof(cmd));
4430 memset(ret_buf, 0, sizeof(ret_buf));
4431
developere40952c2023-06-15 18:46:43 +08004432 res = snprintf(psk_file, sizeof(psk_file), "%s%d.psk", PSK_FILE, apIndex);
4433 if (os_snprintf_error(sizeof(psk_file), res)) {
4434 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4435 return RETURN_ERR;
4436 }
developer47cc27a2023-05-17 23:09:58 +08004437
4438 if (access(psk_file, F_OK) != 0) {
developere40952c2023-06-15 18:46:43 +08004439 res = snprintf(cmd, MAX_CMD_SIZE, "touch %s", psk_file);
4440 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
4441 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4442 return RETURN_ERR;
4443 }
4444
developer47cc27a2023-05-17 23:09:58 +08004445 _syscmd(cmd, ret_buf, sizeof(ret_buf));
4446 } else {
developere40952c2023-06-15 18:46:43 +08004447 res = snprintf(cmd, MAX_CMD_SIZE, "echo '' > %s", psk_file);
4448 if (os_snprintf_error(MAX_CMD_SIZE, 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
developer429ba832023-05-31 11:03:35 +08004456 wifi_setApEnable(apIndex, FALSE);
4457 wifi_setApEnable(apIndex, TRUE);
developer47cc27a2023-05-17 23:09:58 +08004458 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4459
4460 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004461}
4462
developer72fb0bb2023-01-11 09:46:29 +08004463INT wifi_setBandSteeringApGroup(char *ApGroup)
4464{
developera3511852023-06-14 14:12:59 +08004465 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004466}
4467
4468INT wifi_getApDTIMInterval(INT apIndex, INT *dtimInterval)
4469{
developera3511852023-06-14 14:12:59 +08004470 char config_file[128] = {'\0'};
4471 char buf[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004472 int res;
developerc14d83a2023-06-29 20:09:42 +08004473 long int tmp;
4474
developera3511852023-06-14 14:12:59 +08004475 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4476 if (dtimInterval == NULL)
4477 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08004478
4479 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4480 if (os_snprintf_error(sizeof(config_file), res)) {
4481 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4482 return RETURN_ERR;
4483 }
developer72fb0bb2023-01-11 09:46:29 +08004484
developera3511852023-06-14 14:12:59 +08004485 wifi_hostapdRead(config_file, "dtim_period", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08004486
developera3511852023-06-14 14:12:59 +08004487 if (strlen(buf) == 0) {
4488 *dtimInterval = 2;
4489 } else {
developerc14d83a2023-06-29 20:09:42 +08004490 if (hal_strtol(buf, 10, &tmp) < 0) {
4491 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08004492 }
developerc14d83a2023-06-29 20:09:42 +08004493 *dtimInterval = tmp;
developera3511852023-06-14 14:12:59 +08004494 }
developer72fb0bb2023-01-11 09:46:29 +08004495
developera3511852023-06-14 14:12:59 +08004496 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4497 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004498}
4499
4500INT wifi_setApDTIMInterval(INT apIndex, INT dtimInterval)
4501{
developera3511852023-06-14 14:12:59 +08004502 struct params params={0};
4503 char config_file[MAX_BUF_SIZE] = {'\0'};
4504 char buf[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08004505 int res;
developer72fb0bb2023-01-11 09:46:29 +08004506
developera3511852023-06-14 14:12:59 +08004507 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4508 if (dtimInterval < 1 || dtimInterval > 255) {
4509 WIFI_ENTRY_EXIT_DEBUG("Invalid dtimInterval: %d\n", dtimInterval);
4510 return RETURN_ERR;
4511 }
developer69b61b02023-03-07 17:17:44 +08004512
developera3511852023-06-14 14:12:59 +08004513 params.name = "dtim_period";
developere40952c2023-06-15 18:46:43 +08004514 res = snprintf(buf, sizeof(buf), "%d", dtimInterval);
4515 if (os_snprintf_error(sizeof(buf), res)) {
4516 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4517 return RETURN_ERR;
4518 }
developera3511852023-06-14 14:12:59 +08004519 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08004520
developer75bd10c2023-06-27 11:34:08 +08004521 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
4522 if (os_snprintf_error(sizeof(config_file), res)) {
4523 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4524 return RETURN_ERR;
4525 }
developera3511852023-06-14 14:12:59 +08004526 wifi_hostapdWrite(config_file, &params, 1);
4527 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08004528
developera3511852023-06-14 14:12:59 +08004529 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4530 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004531}
4532
4533//Check if the driver support the Dfs
4534INT wifi_getRadioDfsSupport(INT radioIndex, BOOL *output_bool) //Tr181
4535{
developera3511852023-06-14 14:12:59 +08004536 wifi_band band = band_invalid;
4537 if (NULL == output_bool)
4538 return RETURN_ERR;
4539 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +08004540
developera3511852023-06-14 14:12:59 +08004541 band = wifi_index_to_band(radioIndex);
4542 if (band == band_5)
4543 *output_bool = TRUE;
4544 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004545}
4546
4547//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.
4548//The value of this parameter is a comma seperated list of channel number
4549INT wifi_getRadioDCSChannelPool(INT radioIndex, CHAR *output_pool) //RDKB
4550{
developer326d4232023-06-15 16:45:30 +08004551
4552 #define CHANNEL_AVAILABLE 0
4553 #define CHANNEL_INVALID 1
4554 #define CHANNEL_LIST_MAX_LENGTH 256
4555 #define MAX_CHANNEL_NUMBER 255
4556
4557 char config_file[MAX_BUF_SIZE] = {0};
4558 char possible_channels[CHANNEL_LIST_MAX_LENGTH] = {0};
4559 char skip_list[CHANNEL_LIST_MAX_LENGTH] = {0};
4560 int skip_table[MAX_CHANNEL_NUMBER +1] = {0};
4561 wifi_band band = band_invalid;
4562 char *token_channel = NULL, *token_skip = NULL;
developer75bd10c2023-06-27 11:34:08 +08004563 int res;
developere40952c2023-06-15 18:46:43 +08004564
developera3511852023-06-14 14:12:59 +08004565 if (NULL == output_pool)
4566 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004567 // get skiplist, possible_channels list
4568 wifi_getRadioPossibleChannels(radioIndex, possible_channels);
4569 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004570 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4571 if (os_snprintf_error(sizeof(config_file), res)) {
4572 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4573 return RETURN_ERR;
4574 }
developer326d4232023-06-15 16:45:30 +08004575 wifi_datfileRead(config_file, "AutoChannelSkipList", skip_list, sizeof(skip_list));
4576
4577 if (skip_list[0] != '\0') {
4578 int len = strlen(skip_list);
4579 for (int i = 0; i < len; i++) {
4580 if (skip_list[i] == ';') {
4581 skip_list[i] = ',';
4582 }
4583 }
4584 // skip list
4585 token_skip = strtok(skip_list, ",");
4586 while (token_skip != NULL) {
4587 int channel = atoi(token_skip);
4588 if (channel <= MAX_CHANNEL_NUMBER && strstr(possible_channels, token_skip) != NULL)
4589 skip_table[atoi(token_skip)] = CHANNEL_INVALID;
4590 token_skip = strtok(NULL, ",");
4591 }
developere40952c2023-06-15 18:46:43 +08004592 }
developer72fb0bb2023-01-11 09:46:29 +08004593
developer326d4232023-06-15 16:45:30 +08004594 int count = 0;
4595 token_channel = strtok(possible_channels, ",");
4596 while (token_channel != NULL) {
4597 int channel = atoi(token_channel);
4598 if (channel <= MAX_CHANNEL_NUMBER && skip_table[channel] == CHANNEL_AVAILABLE) {
4599 count += snprintf(&output_pool[count], CHANNEL_LIST_MAX_LENGTH-count, "%d,", channel);
4600 if (count >= CHANNEL_LIST_MAX_LENGTH-1)
4601 break;
4602 }
4603 token_channel = strtok(NULL, ",");
4604 }
4605 //delete the last one ','
4606 if (count >0 && output_pool[count-1] == ',')
4607 output_pool[count-1] = '\0';
developera3511852023-06-14 14:12:59 +08004608 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004609}
4610
4611INT wifi_setRadioDCSChannelPool(INT radioIndex, CHAR *pool) //RDKB
4612{
developer326d4232023-06-15 16:45:30 +08004613 char config_file_dat[128] = {0};
4614 struct params dat = {0};
4615 wifi_band band = band_invalid;
4616 char new_pool[128] = {0};
developer75bd10c2023-06-27 11:34:08 +08004617 int res;
developer326d4232023-06-15 16:45:30 +08004618
4619 if (NULL == pool)
4620 return RETURN_ERR;
4621
developer9ce44382023-06-28 11:09:37 +08004622 strncpy(new_pool, pool, sizeof(new_pool) - 1);
4623 new_pool[sizeof(new_pool) - 1] = '\0';
developer326d4232023-06-15 16:45:30 +08004624 for (int i = 0; new_pool[i] != '\0'; i++) {
4625 if (new_pool[i] == ',')
4626 new_pool[i] = ';';
4627 }
4628
4629 dat.name = "AutoChannelSkipList";
4630 dat.value = new_pool;
4631 band = wifi_index_to_band(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08004632 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4633 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4634 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4635 return RETURN_ERR;
4636 }
developer326d4232023-06-15 16:45:30 +08004637 if (wifi_datfileWrite(config_file_dat, &dat, 1) != 0)
4638 return RETURN_ERR;
4639 wifi_reloadAp(radioIndex);
4640
developera3511852023-06-14 14:12:59 +08004641 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004642}
4643
4644INT wifi_getRadioDCSScanTime(INT radioIndex, INT *output_interval_seconds, INT *output_dwell_milliseconds)
4645{
developera3511852023-06-14 14:12:59 +08004646 if (NULL == output_interval_seconds || NULL == output_dwell_milliseconds)
4647 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004648 //Should refresh period time be filled in here? output_interval_seconds is INT type
4649 //wifi_getRadioAutoChannelRefreshPeriod is Ulong type
4650 *output_interval_seconds = 1800;
4651 *output_dwell_milliseconds = 200;
developer72fb0bb2023-01-11 09:46:29 +08004652
developera3511852023-06-14 14:12:59 +08004653 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004654}
4655
4656INT wifi_setRadioDCSScanTime(INT radioIndex, INT interval_seconds, INT dwell_milliseconds)
4657{
developera3511852023-06-14 14:12:59 +08004658 //Set to wifi config. And apply instantly.
4659 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004660}
4661
4662INT wifi_getRadioDfsAtBootUpEnable(INT radioIndex, BOOL *output_bool) //Tr181
4663{
developera3511852023-06-14 14:12:59 +08004664 if (output_bool == NULL)
4665 return RETURN_ERR;
4666 *output_bool = true;
4667 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004668}
4669
4670INT wifi_setRadioDfsAtBootUpEnable(INT radioIndex, BOOL enable) //Tr181
4671{
developera3511852023-06-14 14:12:59 +08004672 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004673}
4674
4675//Get the Dfs enable status
4676INT wifi_getRadioDfsEnable(INT radioIndex, BOOL *output_bool) //Tr181
4677{
developera3511852023-06-14 14:12:59 +08004678 char buf[16] = {0};
4679 char config_file_dat[128] = {0};
4680 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08004681 int res;
developer72fb0bb2023-01-11 09:46:29 +08004682
developera3511852023-06-14 14:12:59 +08004683 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004684
developera3511852023-06-14 14:12:59 +08004685 if (output_bool == NULL)
4686 return RETURN_ERR;
4687 *output_bool = TRUE; // default
4688 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004689 res = snprintf(config_file_dat, sizeof(config_file_dat), "%s%d.dat", LOGAN_DAT_FILE, band);
4690 if (os_snprintf_error(sizeof(config_file_dat), res)) {
4691 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4692 return RETURN_ERR;
4693 }
developerd1824452023-05-18 12:30:04 +08004694
developera3511852023-06-14 14:12:59 +08004695 wifi_datfileRead(config_file_dat, "DfsEnable", buf, sizeof(buf));
developerd1824452023-05-18 12:30:04 +08004696
developera3511852023-06-14 14:12:59 +08004697 if (strncmp(buf, "0", 1) == 0)
4698 *output_bool = FALSE;
4699 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4700 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004701}
4702
4703//Set the Dfs enable status
4704INT wifi_setRadioDfsEnable(INT radioIndex, BOOL enable) //Tr181
4705{
developera3511852023-06-14 14:12:59 +08004706 char config_dat_file[128] = {0};
4707 FILE *f = NULL;
4708 struct params dat = {0};
4709 wifi_band band = band_invalid;
developer75bd10c2023-06-27 11:34:08 +08004710 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +08004711
developera3511852023-06-14 14:12:59 +08004712 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004713
developera3511852023-06-14 14:12:59 +08004714 f = fopen(DFS_ENABLE_FILE, "w");
4715 if (f == NULL)
4716 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08004717 ret = fprintf(f, "%d", enable);
4718 if (ret < 0)
4719 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developerd14dff12023-06-28 22:47:44 +08004720 if (fclose(f) != 0) {
4721 wifi_debug(DEBUG_ERROR, "fclose fail\n");
4722 return RETURN_ERR;
4723 }
developer72fb0bb2023-01-11 09:46:29 +08004724
developera3511852023-06-14 14:12:59 +08004725 wifi_setRadioIEEE80211hEnabled(radioIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08004726
developera3511852023-06-14 14:12:59 +08004727 dat.name = "DfsEnable";
4728 dat.value = enable?"1":"0";
4729 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08004730 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
4731 if (os_snprintf_error(sizeof(config_dat_file), res)) {
4732 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4733 return RETURN_ERR;
4734 }
4735
developera3511852023-06-14 14:12:59 +08004736 wifi_datfileWrite(config_dat_file, &dat, 1);
4737 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4738 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004739}
4740
4741//Check if the driver support the AutoChannelRefreshPeriod
4742INT wifi_getRadioAutoChannelRefreshPeriodSupported(INT radioIndex, BOOL *output_bool) //Tr181
4743{
developera3511852023-06-14 14:12:59 +08004744 if (NULL == output_bool)
4745 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004746 *output_bool = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08004747
developera3511852023-06-14 14:12:59 +08004748 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08004749}
4750
developer326d4232023-06-15 16:45:30 +08004751
4752int get_ACS_RefreshPeriod_callback(struct nl_msg *msg, void *arg)
4753{
4754 ULONG *data = (ULONG *)arg;
4755 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4756 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX + 1];
4757 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4758 int err = 0;
4759
4760 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4761 genlmsg_attrlen(gnlh, 0), NULL);
4762 if (err < 0)
4763 return NL_SKIP;
4764
4765 if (tb[NL80211_ATTR_VENDOR_DATA]) {
4766 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_MAX,
4767 tb[NL80211_ATTR_VENDOR_DATA], NULL);
4768 if (err < 0)
4769 return NL_SKIP;
4770
4771 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]) {
4772 *data = nla_get_u32(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD]);
4773 }
4774 }
4775
4776 return NL_OK;
4777}
4778
developer72fb0bb2023-01-11 09:46:29 +08004779//Get the ACS refresh period in seconds
4780INT wifi_getRadioAutoChannelRefreshPeriod(INT radioIndex, ULONG *output_ulong) //Tr181
4781{
developer326d4232023-06-15 16:45:30 +08004782 char interface_name[IF_NAME_SIZE] = {0};
4783 int ret = -1;
4784 unsigned int if_idx = 0;
4785 struct unl unl_ins;
4786 struct nl_msg *msg = NULL;
4787 struct nlattr * msg_data = NULL;
4788 struct mtk_nl80211_param param;
4789 unsigned long checktime = 0;
4790
4791 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004792 if (NULL == output_ulong)
4793 return RETURN_ERR;
developer326d4232023-06-15 16:45:30 +08004794
4795 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4796 return RETURN_ERR;
4797
4798 if_idx = if_nametoindex(interface_name);
4799 if (!if_idx) {
4800 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4801 return RETURN_ERR;
4802 }
4803 /*init mtk nl80211 vendor cmd*/
4804 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_RUNTIME_INFO;
4805 param.if_type = NL80211_ATTR_IFINDEX;
4806 param.if_idx = if_idx;
developer72fb0bb2023-01-11 09:46:29 +08004807
developer326d4232023-06-15 16:45:30 +08004808 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4809 if (ret) {
4810 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4811 return RETURN_ERR;
4812 }
4813
4814 /*add mtk vendor cmd data*/
4815 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD, 0)) {
4816 wifi_debug(DEBUG_ERROR, "Nla put GET_RUNTIME_INFO_GET_ACS_REFRESH_PERIOD attribute error\n");
4817 nlmsg_free(msg);
4818 goto err;
4819 }
4820
4821 /*send mtk nl80211 vendor msg*/
4822 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_ACS_RefreshPeriod_callback, &checktime);
4823
4824 if (ret) {
4825 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4826 goto err;
4827 }
4828 /*deinit mtk nl80211 vendor msg*/
4829 mtk_nl80211_deint(&unl_ins);
4830 *output_ulong = checktime;
4831 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4832
4833 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developera3511852023-06-14 14:12:59 +08004834 return RETURN_OK;
developer326d4232023-06-15 16:45:30 +08004835err:
4836 mtk_nl80211_deint(&unl_ins);
4837 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
4838 return RETURN_ERR;
4839
developer72fb0bb2023-01-11 09:46:29 +08004840}
4841
4842//Set the ACS refresh period in seconds
4843INT wifi_setRadioDfsRefreshPeriod(INT radioIndex, ULONG seconds) //Tr181
4844{
developer326d4232023-06-15 16:45:30 +08004845 char interface_name[IF_NAME_SIZE] = {0};
4846 int ret = -1;
4847 unsigned int if_idx = 0;
4848 struct unl unl_ins;
4849 struct nl_msg *msg = NULL;
4850 struct nlattr * msg_data = NULL;
4851 struct mtk_nl80211_param param;
4852
4853 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
4854
4855 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4856 return RETURN_ERR;
4857
4858 if_idx = if_nametoindex(interface_name);
4859 if (!if_idx) {
4860 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", interface_name);
4861 return RETURN_ERR;
4862 }
4863 /*init mtk nl80211 vendor cmd*/
4864 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AUTO_CH_SEL;
4865 param.if_type = NL80211_ATTR_IFINDEX;
4866 param.if_idx = if_idx;
4867
4868 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
4869 if (ret) {
4870 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
4871 return RETURN_ERR;
4872 }
4873
4874 /*add mtk vendor cmd data*/
4875 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME, seconds)) {
4876 wifi_debug(DEBUG_ERROR, "Nla put MTK_NL80211_VENDOR_ATTR_AUTO_CH_CHECK_TIME attribute error\n");
4877 nlmsg_free(msg);
4878 goto err;
4879 }
4880
4881 /*send mtk nl80211 vendor msg*/
4882 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
4883
4884 if (ret) {
4885 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
4886 goto err;
4887 }
4888 /*deinit mtk nl80211 vendor msg*/
4889 mtk_nl80211_deint(&unl_ins);
4890 wifi_debug(DEBUG_NOTICE,"send cmd success\n");
4891
4892 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
4893 return RETURN_OK;
4894err:
4895 mtk_nl80211_deint(&unl_ins);
4896 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developera3511852023-06-14 14:12:59 +08004897 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08004898}
4899
4900//Get the Operating Channel Bandwidth. eg "20MHz", "40MHz", "80MHz", "80+80", "160"
4901//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.
4902INT wifi_getRadioOperatingChannelBandwidth(INT radioIndex, CHAR *output_string) //Tr181
4903{
developera3511852023-06-14 14:12:59 +08004904 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
4905 char extchannel[128] = {0};
4906 char interface_name[64] = {0};
developere40952c2023-06-15 18:46:43 +08004907 int ret = 0, len=0, res;
developera3511852023-06-14 14:12:59 +08004908 BOOL radio_enable = FALSE;
4909 wifi_band band;
developer72fb0bb2023-01-11 09:46:29 +08004910
developera3511852023-06-14 14:12:59 +08004911 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004912
developera3511852023-06-14 14:12:59 +08004913 if (NULL == output_string) {
4914 WIFI_ENTRY_EXIT_DEBUG("output_string is nuill %s: %d \n", __func__, __LINE__);
4915 return RETURN_ERR;
4916 }
4917 if (wifi_getRadioEnable(radioIndex, &radio_enable) == RETURN_ERR) {
4918 WIFI_ENTRY_EXIT_DEBUG("wifi_getRadioEnable failed %s: %d \n", __func__, __LINE__);
4919 return RETURN_ERR;
4920 }
4921 if (radio_enable != TRUE) {
4922 WIFI_ENTRY_EXIT_DEBUG("Radio %d is not enable failed %s: %d \n", radioIndex, __func__, __LINE__);
4923 return RETURN_OK;
4924 }
4925 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
4926 return RETURN_ERR;
4927 /*IW command get BW320 to do*/
developerd1824452023-05-18 12:30:04 +08004928
developere40952c2023-06-15 18:46:43 +08004929 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'width' | cut -d ' ' -f6 | tr -d '\\n'", interface_name);
4930 if (os_snprintf_error(sizeof(cmd), res)) {
4931 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4932 return RETURN_ERR;
4933 }
developera3511852023-06-14 14:12:59 +08004934 ret = _syscmd(cmd, buf, sizeof(buf));
4935 len = strlen(buf);
4936 if((ret != 0) || (len == 0))
4937 {
4938 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
4939 return RETURN_ERR;
4940 }
developer8666b312023-03-24 14:05:31 +08004941
developera3511852023-06-14 14:12:59 +08004942 band = wifi_index_to_band(radioIndex);
4943 if (band == band_2_4 && strncmp(buf, "20", 2) == 0) {
4944 wifi_getRadioExtChannel(radioIndex, extchannel);
developere40952c2023-06-15 18:46:43 +08004945 if (strncmp(extchannel, "Auto", 4) != 0) {
4946 res = snprintf(buf, sizeof(buf), "40");
4947 if (os_snprintf_error(sizeof(buf), res)) {
4948 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4949 return RETURN_ERR;
4950 }
4951 }
4952 }
4953 res = snprintf(output_string, 64, "%sMHz", buf);
4954 if (os_snprintf_error(64, res)) {
4955 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
4956 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08004957 }
developera3511852023-06-14 14:12:59 +08004958 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08004959
developera3511852023-06-14 14:12:59 +08004960 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08004961}
4962
4963enum mwctl_chan_width {
4964 MWCTL_CHAN_WIDTH_20,
4965 MWCTL_CHAN_WIDTH_40,
4966 MWCTL_CHAN_WIDTH_80,
4967 MWCTL_CHAN_WIDTH_160,
4968 MWCTL_CHAN_WIDTH_320,
4969};
4970
4971struct bw_option {
4972 unsigned int bandwith;
4973 enum mwctl_chan_width mode;
4974};
4975
4976struct bw_option bw_opt[] = {
4977 {20, MWCTL_CHAN_WIDTH_20},
4978 {40, MWCTL_CHAN_WIDTH_40},
4979 {80, MWCTL_CHAN_WIDTH_80},
4980 {160, MWCTL_CHAN_WIDTH_160},
4981 {320, MWCTL_CHAN_WIDTH_320},
4982};
4983
4984INT wifi_setChannel_netlink(INT radioIndex, UINT* channel, UINT *bandwidth)
4985{
4986 int ret = -1;
4987 int i;
4988 struct unl unl_ins;
4989 struct nl_msg *msg = NULL;
4990 struct nlattr * msg_data = NULL;
4991 struct mtk_nl80211_param param;
4992 bool b_match = FALSE;
4993
4994 /*init mtk nl80211 vendor cmd*/
4995 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_CHANNEL;
4996 param.if_type = NL80211_ATTR_WIPHY;
4997 param.if_idx = radio_index_to_phy(radioIndex);
4998
4999 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5000 if (ret) {
5001 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5002 return RETURN_ERR;
5003 }
5004
5005 /*add mtk vendor cmd data*/
5006 if (channel != NULL)
5007 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_NUM, *channel)) {
5008 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_NUM attribute error\n");
5009 nlmsg_free(msg);
5010 goto err;
5011 }
5012
5013 if (bandwidth != NULL) {
5014 for (i = 0; i < (sizeof(bw_opt)/sizeof(bw_opt[0])); i++) {
5015 if (bw_opt[i].bandwith == *bandwidth) {
5016 b_match = true;
5017 if (nla_put_u32(msg, MTK_NL80211_VENDOR_ATTR_CHAN_SET_BW, bw_opt[i].mode)) {
5018 wifi_debug(DEBUG_ERROR, "Nla put CHAN_SET_BW attribute error\n");
5019 nlmsg_free(msg);
5020 goto err;
5021 }
5022 break;
5023 }
5024 }
5025
5026 if (!b_match) {
5027 wifi_debug(DEBUG_ERROR, "Cannot find bandwith error\n");
5028 nlmsg_free(msg);
5029 goto err;
5030 }
5031 }
5032
5033 /*send mtk nl80211 vendor msg*/
5034 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5035 if (ret) {
5036 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5037 goto err;
5038 }
5039 /*deinit mtk nl80211 vendor msg*/
5040 mtk_nl80211_deint(&unl_ins);
5041 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developera3511852023-06-14 14:12:59 +08005042 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developerfead3972023-05-25 20:15:02 +08005043
developera3511852023-06-14 14:12:59 +08005044 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005045err:
5046 mtk_nl80211_deint(&unl_ins);
5047 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5048 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005049}
developerfead3972023-05-25 20:15:02 +08005050
developer72fb0bb2023-01-11 09:46:29 +08005051//Set the Operating Channel Bandwidth.
5052INT wifi_setRadioOperatingChannelBandwidth(INT radioIndex, CHAR *bandwidth) //Tr181 //AP only
5053{
developera3511852023-06-14 14:12:59 +08005054 char config_file[128];
5055 char ht_value[16];
5056 char vht_value[16];
5057 char eht_value[16];
5058 struct params dat[3];
5059 wifi_band band = band_invalid;
5060 unsigned int bw = 20;
developere40952c2023-06-15 18:46:43 +08005061 int ret = 0, res1, res2, res3;
developer72fb0bb2023-01-11 09:46:29 +08005062
developera3511852023-06-14 14:12:59 +08005063 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005064
developera3511852023-06-14 14:12:59 +08005065 if(NULL == bandwidth)
5066 return RETURN_ERR;
5067 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08005068
developera3511852023-06-14 14:12:59 +08005069 if(strstr(bandwidth,"320") != NULL) {
developere40952c2023-06-15 18:46:43 +08005070 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5071 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5072 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_320);
developera3511852023-06-14 14:12:59 +08005073 bw = 320;
5074 } else if(strstr(bandwidth,"160") != NULL) {
developere40952c2023-06-15 18:46:43 +08005075 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5076 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_160);
5077 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_160);
developera3511852023-06-14 14:12:59 +08005078 bw = 160;
5079 } else if(strstr(bandwidth,"80") != NULL) {
developere40952c2023-06-15 18:46:43 +08005080 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5081 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_80);
5082 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_80);
developera3511852023-06-14 14:12:59 +08005083 bw = 80;
5084 } else if(strstr(bandwidth,"40") != NULL) {
developere40952c2023-06-15 18:46:43 +08005085 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_40);
5086 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5087 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_40);
developera3511852023-06-14 14:12:59 +08005088 bw = 40;
5089 } else if(strstr(bandwidth,"20") != NULL) {
developere40952c2023-06-15 18:46:43 +08005090 res1 = snprintf(ht_value, sizeof(ht_value), "%d", HT_BW_20);
5091 res2 = snprintf(vht_value, sizeof(vht_value), "%d", VHT_BW_2040);
5092 res3 = snprintf(eht_value, sizeof(eht_value), "%d", EHT_BW_20);
developera3511852023-06-14 14:12:59 +08005093 bw = 20;
5094 } else {
developer37646972023-06-29 10:58:43 +08005095 if (fprintf(stderr, "%s: Invalid Bandwidth %s\n", __func__, bandwidth) < 0)
5096 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005097 return RETURN_ERR;
5098 }
developer72fb0bb2023-01-11 09:46:29 +08005099
developer37646972023-06-29 10:58:43 +08005100 if (os_snprintf_error(sizeof(ht_value), res1) ||
5101 os_snprintf_error(sizeof(vht_value), res2) ||
5102 os_snprintf_error(sizeof(eht_value), res3)) {
developere40952c2023-06-15 18:46:43 +08005103 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5104 return RETURN_ERR;
5105 }
5106
5107 res1 = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5108 if (os_snprintf_error(sizeof(config_file), res1)) {
5109 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5110 return RETURN_ERR;
5111 }
developera3511852023-06-14 14:12:59 +08005112 dat[0].name = "HT_BW";
5113 dat[0].value = ht_value;
5114 dat[1].name = "VHT_BW";
5115 dat[1].value = vht_value;
5116 dat[2].name = "EHT_ApBw";
5117 dat[2].value = eht_value;
5118 wifi_datfileWrite(config_file, dat, 3);
5119 ret = wifi_setChannel_netlink(radioIndex, NULL, &bw);
developerfead3972023-05-25 20:15:02 +08005120 if (ret != RETURN_OK) {
developer37646972023-06-29 10:58:43 +08005121 if (fprintf(stderr, "%s: wifi_setChannel return error.\n", __func__) < 0)
5122 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005123 return RETURN_ERR;
5124 }
developer72fb0bb2023-01-11 09:46:29 +08005125
developera3511852023-06-14 14:12:59 +08005126 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5127 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005128}
5129
developer72fb0bb2023-01-11 09:46:29 +08005130//Get the secondary extension channel position, "AboveControlChannel" or "BelowControlChannel". (this is for 40MHz and 80MHz bandwith only)
5131//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.
5132INT wifi_getRadioExtChannel(INT radioIndex, CHAR *output_string) //Tr181
5133{
developera3511852023-06-14 14:12:59 +08005134 char config_file[64] = {0};
5135 char config_dat_file[64] = {0};
5136 char mode_str[16] = {0};
5137 char buf[64] = {0};
5138 char cmd[MAX_CMD_SIZE] = {0};
5139 char interface_name[64] = {0};
5140 int ret = 0, len=0;
5141 wifi_band band;
5142 ULONG channel = 0;
5143 int centr_channel = 0;
5144 UINT mode_map = 0;
developere40952c2023-06-15 18:46:43 +08005145 int freq=0, res;
developer72fb0bb2023-01-11 09:46:29 +08005146
developera3511852023-06-14 14:12:59 +08005147 if (output_string == NULL)
5148 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005149
developer86035662023-06-28 19:21:12 +08005150 if (wifi_getRadioMode(radioIndex, mode_str, &mode_map) != RETURN_OK) {
5151 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode fail\n");
5152 }
developer72fb0bb2023-01-11 09:46:29 +08005153
developera3511852023-06-14 14:12:59 +08005154 band = wifi_index_to_band(radioIndex);
5155 if (band == band_invalid)
5156 return RETURN_ERR;
5157 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5158 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005159
developere40952c2023-06-15 18:46:43 +08005160 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5161 if (os_snprintf_error(sizeof(config_file), res)) {
5162 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5163 return RETURN_ERR;
5164 }
5165
5166 res = snprintf(output_string, 64, "Auto");
5167 if (os_snprintf_error(64, res)) {
5168 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5169 return RETURN_ERR;
5170 }
developer72fb0bb2023-01-11 09:46:29 +08005171
developera3511852023-06-14 14:12:59 +08005172 if (band == band_2_4 || (!(mode_map&WIFI_MODE_AC) && !(mode_map&WIFI_MODE_AX))) {
5173 // 2G band or ac and ax mode is disable, we will check HT_EXTCHA
developere40952c2023-06-15 18:46:43 +08005174 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5175 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5176 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5177 return RETURN_ERR;
5178 }
developerb758dfd2023-06-21 17:32:07 +08005179
developera3511852023-06-14 14:12:59 +08005180 wifi_halgetRadioExtChannel(config_dat_file, output_string);
developer37646972023-06-29 10:58:43 +08005181 if (!(mode_map&WIFI_MODE_N)) {
developere40952c2023-06-15 18:46:43 +08005182 res = snprintf(output_string, 64, "Auto");
developer37646972023-06-29 10:58:43 +08005183 if (os_snprintf_error(64, res)) {
5184 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5185 return RETURN_ERR;
5186 }
5187 }
developera3511852023-06-14 14:12:59 +08005188 } else {
5189 // 5G and 6G band with ac or ax mode.
5190 wifi_getRadioChannel(radioIndex, &channel);
developere40952c2023-06-15 18:46:43 +08005191 res = snprintf(cmd, sizeof(cmd),"iw dev %s info | grep 'center1' | cut -d ' ' -f9 | tr -d '\\n'", interface_name);
5192 if (os_snprintf_error(sizeof(cmd), res)) {
5193 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5194 return RETURN_ERR;
5195 }
developerd1824452023-05-18 12:30:04 +08005196
developera3511852023-06-14 14:12:59 +08005197 ret = _syscmd(cmd, buf, sizeof(buf));
5198 len = strlen(buf);
5199 if((ret != 0) || (len == 0))
5200 {
5201 WIFI_ENTRY_EXIT_DEBUG("failed with Command %s %s:%d\n",cmd,__func__, __LINE__);
5202 return RETURN_ERR;
5203 }
5204 sscanf(buf, "%d", &freq);
5205 centr_channel = ieee80211_frequency_to_channel(freq);
5206 if (centr_channel > (int)channel)
developere40952c2023-06-15 18:46:43 +08005207 res = snprintf(output_string, 64, "AboveControlChannel");
developera3511852023-06-14 14:12:59 +08005208 else
developere40952c2023-06-15 18:46:43 +08005209 res = snprintf(output_string, 64, "BelowControlChannel");
5210
5211 if (os_snprintf_error(64, res)) {
5212 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5213 return RETURN_ERR;
5214 }
developera3511852023-06-14 14:12:59 +08005215 }
developer72fb0bb2023-01-11 09:46:29 +08005216
developera3511852023-06-14 14:12:59 +08005217 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005218}
5219
5220//Set the extension channel.
5221INT wifi_setRadioExtChannel(INT radioIndex, CHAR *string) //Tr181 //AP only
developer69b61b02023-03-07 17:17:44 +08005222{
developera3511852023-06-14 14:12:59 +08005223 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5224 struct params params={0};
5225 char config_file[64] = {0};
5226 char config_dat_file[64] = {0};
5227 char ext_channel[64] = {0};
5228 char buf[128] = {0};
5229 char cmd[128] = {0};
developerc14d83a2023-06-29 20:09:42 +08005230 int max_radio_num =0, ret = 0;
5231 long int bandwidth = 0;
developera3511852023-06-14 14:12:59 +08005232 unsigned long channel = 0;
5233 bool stbcEnable = FALSE;
5234 params.name = "ht_capab";
5235 wifi_band band;
developere40952c2023-06-15 18:46:43 +08005236 int res;
developer72fb0bb2023-01-11 09:46:29 +08005237
developer75bd10c2023-06-27 11:34:08 +08005238 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5239 if (os_snprintf_error(sizeof(config_file), res)) {
5240 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5241 return RETURN_ERR;
5242 }
5243
developere40952c2023-06-15 18:46:43 +08005244 res = snprintf(cmd, sizeof(cmd), "cat %s | grep STBC", config_file);
5245 if (os_snprintf_error(sizeof(cmd), res)) {
5246 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5247 return RETURN_ERR;
5248 }
developera3511852023-06-14 14:12:59 +08005249 _syscmd(cmd, buf, sizeof(buf));
5250 if (strlen(buf) != 0)
5251 stbcEnable = TRUE;
5252 if (wifi_getRadioOperatingChannelBandwidth(radioIndex, buf) != RETURN_OK)
5253 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +08005254 if (hal_strtol(buf, 10, &bandwidth) < 0) {
5255 wifi_debug(DEBUG_ERROR, "strtol fail\n");
5256 }
developera3511852023-06-14 14:12:59 +08005257 // TDK expected to get error with 20MHz
5258 // we handle 20MHz in function wifi_RemoveRadioExtChannel().
5259 if (bandwidth == 20 || strstr(buf, "80+80") != NULL)
5260 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005261
developera3511852023-06-14 14:12:59 +08005262 band = wifi_index_to_band(radioIndex);
5263 if (band == band_invalid)
5264 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005265
developera3511852023-06-14 14:12:59 +08005266 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK)
5267 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005268
developerc14d83a2023-06-29 20:09:42 +08005269 res = snprintf(buf, sizeof(buf), "HT%ld", bandwidth);
developere40952c2023-06-15 18:46:43 +08005270 if (os_snprintf_error(sizeof(buf), res)) {
5271 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5272 return RETURN_ERR;
5273 }
developera3511852023-06-14 14:12:59 +08005274 ret = util_get_sec_chan_offset(channel, buf);
5275 if (ret == -EINVAL)
5276 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005277
developera3511852023-06-14 14:12:59 +08005278 if(NULL!= strstr(string,"Above")) {
5279 if ((band == band_2_4 && channel > 9) || (band == band_5 && ret == -1))
5280 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005281 memcpy(ext_channel, "1", strlen("1"));
developera3511852023-06-14 14:12:59 +08005282 } else if(NULL!= strstr(string,"Below")) {
5283 if ((band == band_2_4 && channel < 5) || (band == band_5 && ret == -1))
5284 return RETURN_OK;
developerb14b3462023-07-01 18:02:42 +08005285 memcpy(ext_channel, "0", strlen("0"));
developera3511852023-06-14 14:12:59 +08005286 } else {
5287 printf("%s: invalid EXT_CHA:%s\n", __func__, string);
developer262f4cb2023-05-24 12:22:04 +08005288 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08005289 }
5290 params.name = "HT_EXTCHA";
5291 params.value = ext_channel;
developer72fb0bb2023-01-11 09:46:29 +08005292
developere75ba632023-06-29 16:03:33 +08005293 res = snprintf (config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
developere40952c2023-06-15 18:46:43 +08005294 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5295 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5296 return RETURN_ERR;
5297 }
developera3511852023-06-14 14:12:59 +08005298 wifi_datfileWrite(config_dat_file, &params, 1);
developerd1824452023-05-18 12:30:04 +08005299
developera3511852023-06-14 14:12:59 +08005300 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08005301 if(max_radio_num== 0){
5302 return RETURN_ERR;
5303 }
developera3511852023-06-14 14:12:59 +08005304 for(int i=0; i<=MAX_APS/max_radio_num; i++)
5305 {
developer32f2a182023-06-27 19:50:41 +08005306 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex+(max_radio_num*i));
5307 if (os_snprintf_error(sizeof(config_file), res)) {
5308 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5309 return RETURN_ERR;
5310 }
developera3511852023-06-14 14:12:59 +08005311 wifi_setRadioSTBCEnable(radioIndex+(max_radio_num*i), stbcEnable);
5312 }
developer72fb0bb2023-01-11 09:46:29 +08005313
developera3511852023-06-14 14:12:59 +08005314 //Set to wifi config only. Wait for wifi reset or wifi_pushRadioChannel to apply.
5315 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5316 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005317}
5318
5319//Get the guard interval value. eg "400nsec" or "800nsec"
5320//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.
5321INT wifi_getRadioGuardInterval(INT radioIndex, CHAR *output_string) //Tr181
5322{
developera3511852023-06-14 14:12:59 +08005323 wifi_guard_interval_t GI;
developer32f2a182023-06-27 19:50:41 +08005324 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08005325
developera3511852023-06-14 14:12:59 +08005326 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005327
developera3511852023-06-14 14:12:59 +08005328 if (output_string == NULL || wifi_getGuardInterval(radioIndex, &GI) == RETURN_ERR)
5329 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005330
developer32f2a182023-06-27 19:50:41 +08005331 if (GI == wifi_guard_interval_400) {
5332 len = strlen("400nsec");
5333 memcpy(output_string, "400nsec", len);
5334 } else if (GI == wifi_guard_interval_800) {
5335 len = strlen("800nsec");
5336 memcpy(output_string, "800nsec", strlen("800nsec"));
5337 } else if (GI == wifi_guard_interval_1600) {
5338 len = strlen("1600nsec");
5339 memcpy(output_string, "1600nsec", strlen("1600nsec"));
5340 } else if (GI == wifi_guard_interval_3200) {
5341 len = strlen("3200nsec");
5342 memcpy(output_string, "3200nsec", strlen("3200nsec"));
5343 } else {
5344 len = strlen("Auto");
5345 memcpy(output_string, "Auto", strlen("Auto"));
5346 }
5347 output_string[len] = '\0';
developera3511852023-06-14 14:12:59 +08005348 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5349 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005350}
5351
5352//Set the guard interval value.
5353INT wifi_setRadioGuardInterval(INT radioIndex, CHAR *string) //Tr181
5354{
developera3511852023-06-14 14:12:59 +08005355 wifi_guard_interval_t GI;
5356 int ret = 0;
developer72fb0bb2023-01-11 09:46:29 +08005357
developera3511852023-06-14 14:12:59 +08005358 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005359
developera3511852023-06-14 14:12:59 +08005360 if (strcmp(string, "400nsec") == 0)
5361 GI = wifi_guard_interval_400;
5362 else if (strcmp(string , "800nsec") == 0)
5363 GI = wifi_guard_interval_800;
5364 else if (strcmp(string , "1600nsec") == 0)
5365 GI = wifi_guard_interval_1600;
5366 else if (strcmp(string , "3200nsec") == 0)
5367 GI = wifi_guard_interval_3200;
5368 else
5369 GI = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +08005370
developera3511852023-06-14 14:12:59 +08005371 ret = wifi_setGuardInterval(radioIndex, GI);
developer72fb0bb2023-01-11 09:46:29 +08005372
developera3511852023-06-14 14:12:59 +08005373 if (ret == RETURN_ERR) {
5374 wifi_dbg_printf("%s: wifi_setGuardInterval return error\n", __func__);
5375 return RETURN_ERR;
5376 }
developer72fb0bb2023-01-11 09:46:29 +08005377
developera3511852023-06-14 14:12:59 +08005378 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5379 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005380}
5381
5382//Get the Modulation Coding Scheme index, eg: "-1", "1", "15"
5383INT wifi_getRadioMCS(INT radioIndex, INT *output_int) //Tr181
5384{
developera3511852023-06-14 14:12:59 +08005385 char buf[32]={0};
5386 char mcs_file[64] = {0};
5387 char cmd[MAX_CMD_SIZE] = {0};
5388 UINT mode_bitmap = 0;
developere40952c2023-06-15 18:46:43 +08005389 int res;
developerc14d83a2023-06-29 20:09:42 +08005390 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08005391
developera3511852023-06-14 14:12:59 +08005392 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5393 if(output_int == NULL)
5394 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005395 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5396 if (os_snprintf_error(sizeof(mcs_file), res)) {
5397 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5398 return RETURN_ERR;
5399 }
5400
5401 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", mcs_file);
5402 if (os_snprintf_error(sizeof(cmd), res)) {
5403 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5404 return RETURN_ERR;
5405 }
developer72fb0bb2023-01-11 09:46:29 +08005406
developera3511852023-06-14 14:12:59 +08005407 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08005408 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +08005409 if (hal_strtol(buf, 10, &tmp) < 0) {
5410 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005411 }
developerc14d83a2023-06-29 20:09:42 +08005412 *output_int = tmp;
developerd14dff12023-06-28 22:47:44 +08005413 } else {
developera3511852023-06-14 14:12:59 +08005414 // output the max MCS for the current radio mode
5415 if (wifi_getRadioMode(radioIndex, buf, &mode_bitmap) == RETURN_ERR) {
5416 wifi_dbg_printf("%s: wifi_getradiomode return error.\n", __func__);
5417 return RETURN_ERR;
5418 }
5419 if (mode_bitmap & WIFI_MODE_AX) {
5420 *output_int = 11;
5421 } else if (mode_bitmap & WIFI_MODE_AC) {
5422 *output_int = 9;
5423 } else if (mode_bitmap & WIFI_MODE_N) {
5424 *output_int = 7;
5425 }
5426 }
5427 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005428
developera3511852023-06-14 14:12:59 +08005429 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005430}
5431
5432//Set the Modulation Coding Scheme index
5433INT wifi_setRadioMCS(INT radioIndex, INT MCS) //Tr181
5434{
developera3511852023-06-14 14:12:59 +08005435 /*Only HE mode can specify MCS capability. We don't support MCS in HT mode,
5436 because that would be ambiguous (MCS code 8~11 refer to 2 NSS in HT but 1 NSS in HE adn VHT).*/
5437 char config_file[64] = {0};
5438 char set_value[16] = {0};
5439 char mcs_file[32] = {0};
5440 struct params set_config = {0};
5441 FILE *f = NULL;
5442 INT nss = 0;
5443 int ant_bitmap = 0;
5444 unsigned short cal_value = 0;
5445 UCHAR tval = 0, i = 0;
developere40952c2023-06-15 18:46:43 +08005446 int res;
developer72fb0bb2023-01-11 09:46:29 +08005447
developera3511852023-06-14 14:12:59 +08005448 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005449
developere40952c2023-06-15 18:46:43 +08005450 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5451 if (os_snprintf_error(sizeof(config_file), res)) {
5452 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5453 return RETURN_ERR;
5454 }
developer72fb0bb2023-01-11 09:46:29 +08005455
developera3511852023-06-14 14:12:59 +08005456 // -1 means auto
5457 if (MCS > 15 || MCS < -1) {
developer75bd10c2023-06-27 11:34:08 +08005458 wifi_debug(DEBUG_ERROR, "invalid MCS %d\n", MCS);
developera3511852023-06-14 14:12:59 +08005459 return RETURN_ERR;
5460 }
5461 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);/*nss is a bit map value,1111*/
5462 for(; ant_bitmap > 0; ant_bitmap >>= 1)
5463 nss += 1;
5464 //printf("%s:nss = %d\n", __func__, nss);
5465 /*16-bit combination of 2-bit values of Max HE-MCS For 1..8 SS;each 2-bit value have following meaning:
5466 0 = HE-MCS 0-7, 1 = HE-MCS 0-9, 2 = HE-MCS 0-11, 3 = not supported*/
5467 if (MCS > 9 || MCS == -1)
5468 tval = 2;/*one stream value*/
5469 else if (MCS > 7)
5470 tval = 1;
5471 else
5472 tval = 0;
5473 for (i = 0; i < nss; i++)
5474 cal_value |= (tval << (2*i));
developere40952c2023-06-15 18:46:43 +08005475 res = snprintf(set_value, sizeof(set_value), "%x", cal_value);
5476 if (os_snprintf_error(sizeof(set_value), res)) {
5477 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5478 return RETURN_ERR;
5479 }
5480
developera3511852023-06-14 14:12:59 +08005481 WIFI_ENTRY_EXIT_DEBUG("%s:set=%s, cal=%x\n", __func__, set_value, cal_value);
5482 set_config.name = "he_basic_mcs_nss_set";/*He capability in beacon or response*/
5483 set_config.value = set_value;
developer72fb0bb2023-01-11 09:46:29 +08005484
developera3511852023-06-14 14:12:59 +08005485 wifi_hostapdWrite(config_file, &set_config, 1);
5486 wifi_hostapdProcessUpdate(radioIndex, &set_config, 1);
developer72fb0bb2023-01-11 09:46:29 +08005487
developera3511852023-06-14 14:12:59 +08005488 // 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 +08005489 res = snprintf(mcs_file, sizeof(mcs_file), "%s%d.txt", MCS_FILE, radioIndex);
5490 if (os_snprintf_error(sizeof(mcs_file), res)) {
5491 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5492 return RETURN_ERR;
5493 }
5494
developera3511852023-06-14 14:12:59 +08005495 f = fopen(mcs_file, "w");
5496 if (f == NULL) {
developere75ba632023-06-29 16:03:33 +08005497 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
5498 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +08005499 return RETURN_ERR;
5500 }
developere75ba632023-06-29 16:03:33 +08005501 if (fprintf(f, "%d", MCS) < 0)
5502 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
5503 if (fclose(f) == EOF) {
5504 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
5505 return RETURN_ERR;
5506 }
developer72fb0bb2023-01-11 09:46:29 +08005507
developera3511852023-06-14 14:12:59 +08005508 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5509 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005510}
5511
5512//Get supported Transmit Power list, eg : "0,25,50,75,100"
5513//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.
5514INT wifi_getRadioTransmitPowerSupported(INT radioIndex, CHAR *output_list) //Tr181
5515{
developere40952c2023-06-15 18:46:43 +08005516 int res;
5517 if (NULL == output_list)
5518 return RETURN_ERR;
5519 res = snprintf(output_list, 64,"0,25,50,75,100");
5520 if (os_snprintf_error(64, res)) {
5521 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5522 return RETURN_ERR;
5523 }
5524
5525 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005526}
5527
5528//Get current Transmit Power in dBm units.
5529//The transmite power level is in units of full power for this radio.
5530INT wifi_getRadioTransmitPower(INT radioIndex, ULONG *output_ulong) //RDKB
5531{
developera3511852023-06-14 14:12:59 +08005532 char interface_name[16] = {0};
5533 char cmd[MAX_CMD_SIZE]={0};
5534 char buf[16]={0};
developera1255e42023-05-13 17:45:02 +08005535 char pwr_file[128]={0};
developere40952c2023-06-15 18:46:43 +08005536 int res;
developera1255e42023-05-13 17:45:02 +08005537
developera3511852023-06-14 14:12:59 +08005538 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005539
developera3511852023-06-14 14:12:59 +08005540 if(output_ulong == NULL)
5541 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005542
developera3511852023-06-14 14:12:59 +08005543 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5544 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005545 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5546 if (os_snprintf_error(sizeof(pwr_file), res)) {
5547 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5548 return RETURN_ERR;
5549 }
5550
5551 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", pwr_file);
5552 if (os_snprintf_error(sizeof(cmd), res)) {
5553 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5554 return RETURN_ERR;
5555 }
5556
developera1255e42023-05-13 17:45:02 +08005557 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08005558 if (strlen(buf) > 0) {
5559 if (hal_strtoul(buf, 10, output_ulong) < 0) {
5560 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08005561 }
5562 } else
developera1255e42023-05-13 17:45:02 +08005563 *output_ulong = 100;
developera3511852023-06-14 14:12:59 +08005564 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5565 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005566}
5567
5568//Set Transmit Power
5569//The transmite power level is in units of full power for this radio.
5570INT wifi_setRadioTransmitPower(INT radioIndex, ULONG TransmitPower) //RDKB
5571{
developera3511852023-06-14 14:12:59 +08005572 char interface_name[16] = {0};
5573 char *support;
5574 char buf[128]={0};
5575 char txpower_str[64] = {0};
developera1255e42023-05-13 17:45:02 +08005576 char pwr_file[128]={0};
developera3511852023-06-14 14:12:59 +08005577 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +08005578 int if_idx, ret = 0;
5579 struct nl_msg *msg = NULL;
5580 struct nlattr * msg_data = NULL;
5581 struct mtk_nl80211_param param;
5582 struct unl unl_ins;
developere40952c2023-06-15 18:46:43 +08005583 int res;
developer72fb0bb2023-01-11 09:46:29 +08005584
developera3511852023-06-14 14:12:59 +08005585 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08005586
developera3511852023-06-14 14:12:59 +08005587 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5588 return RETURN_ERR;
5589 // Get the Tx power supported list and check that is the input in the list
developere40952c2023-06-15 18:46:43 +08005590 res = snprintf(txpower_str, sizeof(txpower_str), "%lu", TransmitPower);
5591 if (os_snprintf_error(sizeof(txpower_str), res)) {
5592 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5593 return RETURN_ERR;
5594 }
developera3511852023-06-14 14:12:59 +08005595 wifi_getRadioTransmitPowerSupported(radioIndex, buf);
5596 support = strtok(buf, ",");
5597 while(true)
5598 {
5599 if(support == NULL) { // input not in the list
5600 wifi_dbg_printf("Input value is invalid.\n");
5601 return RETURN_ERR;
5602 }
5603 if (strncmp(txpower_str, support, strlen(support)) == 0) {
5604 break;
5605 }
5606 support = strtok(NULL, ",");
5607 }
developerfead3972023-05-25 20:15:02 +08005608
5609 if_idx = if_nametoindex(interface_name);
5610 /*init mtk nl80211 vendor cmd*/
5611 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
5612 param.if_type = NL80211_ATTR_IFINDEX;
5613 param.if_idx = if_idx;
5614 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
5615 if (ret) {
5616 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
5617 return RETURN_ERR;
5618 }
5619 /*add mtk vendor cmd data*/
5620 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_PERCENTAGE_EN, 1)) {
5621 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5622 nlmsg_free(msg);
5623 goto err;
5624 }
5625
5626 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_DROP_CTRL, TransmitPower)) {
5627 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
5628 nlmsg_free(msg);
5629 goto err;
5630 }
5631
5632 /*send mtk nl80211 vendor msg*/
5633 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
5634 if (ret) {
5635 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
5636 goto err;
5637 }
5638 /*deinit mtk nl80211 vendor msg*/
5639 mtk_nl80211_deint(&unl_ins);
5640 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
5641
developere40952c2023-06-15 18:46:43 +08005642 res = snprintf(pwr_file, sizeof(pwr_file), "%s%d.txt", POWER_PERCENTAGE, radioIndex);
5643 if (os_snprintf_error(sizeof(pwr_file), res)) {
5644 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5645 return RETURN_ERR;
5646 }
5647
developera3511852023-06-14 14:12:59 +08005648 f = fopen(pwr_file, "w");
5649 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +08005650 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera3511852023-06-14 14:12:59 +08005651 return RETURN_ERR;
5652 }
5653 fprintf(f, "%lu", TransmitPower);
developerc14d83a2023-06-29 20:09:42 +08005654 if (fclose(f) == EOF)
5655 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera1255e42023-05-13 17:45:02 +08005656 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +08005657err:
5658 mtk_nl80211_deint(&unl_ins);
5659 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
5660 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005661}
5662
5663//get 80211h Supported. 80211h solves interference with satellites and radar using the same 5 GHz frequency band
5664INT wifi_getRadioIEEE80211hSupported(INT radioIndex, BOOL *Supported) //Tr181
5665{
developera3511852023-06-14 14:12:59 +08005666 if (NULL == Supported)
5667 return RETURN_ERR;
5668 *Supported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +08005669
developera3511852023-06-14 14:12:59 +08005670 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005671}
5672
5673//Get 80211h feature enable
5674INT wifi_getRadioIEEE80211hEnabled(INT radioIndex, BOOL *enable) //Tr181
5675{
developera3511852023-06-14 14:12:59 +08005676 char buf[64]={'\0'};
5677 char config_file[64] = {'\0'};
developer75bd10c2023-06-27 11:34:08 +08005678 int res;
developer72fb0bb2023-01-11 09:46:29 +08005679
developera3511852023-06-14 14:12:59 +08005680 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5681 if(enable == NULL)
5682 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005683
developer75bd10c2023-06-27 11:34:08 +08005684 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5685 if (os_snprintf_error(sizeof(config_file), res)) {
5686 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5687 return RETURN_ERR;
5688 }
developera3511852023-06-14 14:12:59 +08005689 /* wifi_hostapdRead(config_file, "ieee80211h", buf, sizeof(buf)); */
5690 wifi_datfileRead(config_file, "IEEE80211H", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08005691
developera3511852023-06-14 14:12:59 +08005692 if (strncmp(buf, "1", 1) == 0)
5693 *enable = TRUE;
5694 else
5695 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08005696
developera3511852023-06-14 14:12:59 +08005697 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5698 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005699}
5700
5701//Set 80211h feature enable
5702INT wifi_setRadioIEEE80211hEnabled(INT radioIndex, BOOL enable) //Tr181
5703{
developera3511852023-06-14 14:12:59 +08005704 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5705 struct params params={'\0'};
5706 struct params dat={0};
5707 char config_file[MAX_BUF_SIZE] = {0};
5708 char config_dat_file[MAX_BUF_SIZE] = {0};
5709 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08005710 int res;
developer72fb0bb2023-01-11 09:46:29 +08005711
developera3511852023-06-14 14:12:59 +08005712 params.name = "ieee80211h";
developer72fb0bb2023-01-11 09:46:29 +08005713
developera3511852023-06-14 14:12:59 +08005714 if (enable) {
5715 params.value = "1";
5716 } else {
5717 params.value = "0";
5718 }
developer72fb0bb2023-01-11 09:46:29 +08005719
developera3511852023-06-14 14:12:59 +08005720 dat.name = "IEEE80211H";
5721 dat.value = params.value;
developerd1824452023-05-18 12:30:04 +08005722
developera3511852023-06-14 14:12:59 +08005723 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +08005724 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5725 if (os_snprintf_error(sizeof(config_file), res)) {
5726 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5727 return RETURN_ERR;
5728 }
5729
5730 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
5731 if (os_snprintf_error(sizeof(config_dat_file), res)) {
5732 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5733 return RETURN_ERR;
5734 }
developer69b61b02023-03-07 17:17:44 +08005735
developera3511852023-06-14 14:12:59 +08005736 wifi_hostapdWrite(config_file, &params, 1);
5737 wifi_datfileWrite(config_dat_file, &dat, 1);
5738 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5739 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5740 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005741}
5742
5743//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.
5744INT wifi_getRadioCarrierSenseThresholdRange(INT radioIndex, INT *output) //P3
5745{
developera3511852023-06-14 14:12:59 +08005746 if (NULL == output)
5747 return RETURN_ERR;
5748 *output=100;
developer72fb0bb2023-01-11 09:46:29 +08005749
developera3511852023-06-14 14:12:59 +08005750 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005751}
5752
5753//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.
5754INT wifi_getRadioCarrierSenseThresholdInUse(INT radioIndex, INT *output) //P3
5755{
developera3511852023-06-14 14:12:59 +08005756 if (NULL == output)
5757 return RETURN_ERR;
5758 *output = -99;
developer72fb0bb2023-01-11 09:46:29 +08005759
developera3511852023-06-14 14:12:59 +08005760 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005761}
5762
5763INT wifi_setRadioCarrierSenseThresholdInUse(INT radioIndex, INT threshold) //P3
5764{
developera3511852023-06-14 14:12:59 +08005765 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005766}
5767
5768
5769//Time interval between transmitting beacons (expressed in milliseconds). This parameter is based ondot11BeaconPeriod from [802.11-2012].
5770INT wifi_getRadioBeaconPeriod(INT radioIndex, UINT *output)
5771{
developera3511852023-06-14 14:12:59 +08005772 char interface_name[16] = {0};
5773 char cmd[MAX_BUF_SIZE]={'\0'};
5774 char buf[MAX_CMD_SIZE]={'\0'};
developere40952c2023-06-15 18:46:43 +08005775 int res;
developer72fb0bb2023-01-11 09:46:29 +08005776
developera3511852023-06-14 14:12:59 +08005777 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5778 if(output == NULL)
5779 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005780
developera3511852023-06-14 14:12:59 +08005781 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
5782 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08005783 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s status | grep beacon_int | cut -d '=' -f2 | tr -d '\n'", interface_name);
5784 if (os_snprintf_error(sizeof(cmd), res)) {
5785 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5786 return RETURN_ERR;
5787 }
5788
developera3511852023-06-14 14:12:59 +08005789 _syscmd(cmd, buf, sizeof(buf));
5790 *output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +08005791
developera3511852023-06-14 14:12:59 +08005792 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5793 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005794}
developer69b61b02023-03-07 17:17:44 +08005795
developer72fb0bb2023-01-11 09:46:29 +08005796INT wifi_setRadioBeaconPeriod(INT radioIndex, UINT BeaconPeriod)
5797{
developera3511852023-06-14 14:12:59 +08005798 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5799 struct params params={'\0'};
5800 char buf[MAX_BUF_SIZE] = {'\0'};
5801 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08005802 int res;
developer72fb0bb2023-01-11 09:46:29 +08005803
developera3511852023-06-14 14:12:59 +08005804 if (BeaconPeriod < 15 || BeaconPeriod > 65535)
5805 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08005806
developera3511852023-06-14 14:12:59 +08005807 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08005808 res = snprintf(buf, sizeof(buf), "%u", BeaconPeriod);
5809 if (os_snprintf_error(sizeof(buf), res)) {
5810 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5811 return RETURN_ERR;
5812 }
5813
developera3511852023-06-14 14:12:59 +08005814 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08005815
developer75bd10c2023-06-27 11:34:08 +08005816 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5817 if (os_snprintf_error(sizeof(config_file), res)) {
5818 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5819 return RETURN_ERR;
5820 }
developera3511852023-06-14 14:12:59 +08005821 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08005822
developera3511852023-06-14 14:12:59 +08005823 wifi_hostapdProcessUpdate(radioIndex, &params, 1);
5824 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5825 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005826}
5827
5828//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.
5829INT wifi_getRadioBasicDataTransmitRates(INT radioIndex, CHAR *output)
5830{
developera3511852023-06-14 14:12:59 +08005831 //TODO: need to revisit below implementation
5832 char *temp;
5833 char temp_output[128] = {0};
5834 char temp_TransmitRates[64] = {0};
5835 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08005836 int res;
developer72fb0bb2023-01-11 09:46:29 +08005837
developera3511852023-06-14 14:12:59 +08005838 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5839 if (NULL == output)
5840 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08005841
5842 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
5843 if (os_snprintf_error(sizeof(config_file), res)) {
5844 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5845 return RETURN_ERR;
5846 }
developera3511852023-06-14 14:12:59 +08005847 wifi_hostapdRead(config_file,"basic_rates",temp_TransmitRates,64);
developer69b61b02023-03-07 17:17:44 +08005848
developera3511852023-06-14 14:12:59 +08005849 if (strlen(temp_TransmitRates) == 0) { // config not set, use supported rate
5850 wifi_getRadioSupportedDataTransmitRates(radioIndex, output);
5851 } else {
5852 temp = strtok(temp_TransmitRates," ");
5853 while(temp!=NULL)
5854 {
5855 // Convert 100 kbps to Mbps
5856 temp[strlen(temp)-1]=0;
5857 if((temp[0]=='5') && (temp[1]=='\0'))
5858 {
5859 temp="5.5";
5860 }
developer32f2a182023-06-27 19:50:41 +08005861 if (strlen(temp) >= sizeof(temp_output))
5862 return RETURN_ERR;
5863 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005864 temp = strtok(NULL," ");
5865 if(temp!=NULL)
5866 {
developer32f2a182023-06-27 19:50:41 +08005867 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
5868 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005869 }
5870 }
developer32f2a182023-06-27 19:50:41 +08005871 memcpy(output, temp_output, strlen(temp_output));
5872 output[strlen(temp_output)] = '\0';
developera3511852023-06-14 14:12:59 +08005873 }
5874 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
5875 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08005876}
5877
5878INT wifi_setRadioBasicDataTransmitRates(INT radioIndex, CHAR *TransmitRates)
5879{
developera3511852023-06-14 14:12:59 +08005880 char *temp;
developer32f2a182023-06-27 19:50:41 +08005881 char temp1[128] = {0};
5882 char temp_output[128] = {0};
5883 char temp_TransmitRates[128] = {0};
5884 char set[128] = {0};
5885 char sub_set[128] = {0};
developera3511852023-06-14 14:12:59 +08005886 int set_count=0,subset_count=0;
5887 int set_index=0,subset_index=0;
5888 char *token;
5889 int flag=0, i=0;
5890 struct params params={'\0'};
5891 char config_file[MAX_BUF_SIZE] = {0};
5892 wifi_band band = wifi_index_to_band(radioIndex);
developer32f2a182023-06-27 19:50:41 +08005893 int res;
developer72fb0bb2023-01-11 09:46:29 +08005894
developera3511852023-06-14 14:12:59 +08005895 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
5896 if(NULL == TransmitRates)
5897 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +08005898 if (strlen(TransmitRates) >= sizeof(sub_set))
5899 return RETURN_ERR;
5900
5901 memcpy(sub_set, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08005902
developera3511852023-06-14 14:12:59 +08005903 //Allow only supported Data transmit rate to be set
5904 wifi_getRadioSupportedDataTransmitRates(radioIndex,set);
5905 token = strtok(sub_set,",");
5906 while( token != NULL ) /* split the basic rate to be set, by comma */
5907 {
5908 sub_set[subset_count]=atoi(token);
5909 subset_count++;
5910 token=strtok(NULL,",");
5911 }
5912 token=strtok(set,",");
5913 while(token!=NULL) /* split the supported rate by comma */
5914 {
5915 set[set_count]=atoi(token);
5916 set_count++;
5917 token=strtok(NULL,",");
5918 }
5919 for(subset_index=0;subset_index < subset_count;subset_index++) /* Compare each element of subset and set */
5920 {
5921 for(set_index=0;set_index < set_count;set_index++)
5922 {
5923 flag=0;
5924 if(sub_set[subset_index]==set[set_index])
5925 break;
5926 else
5927 flag=1; /* No match found */
5928 }
5929 if(flag==1)
5930 return RETURN_ERR; //If value not found return Error
5931 }
developer32f2a182023-06-27 19:50:41 +08005932
5933 if (strlen(TransmitRates) >= sizeof(temp_TransmitRates))
5934 return RETURN_ERR;
5935
5936 memcpy(temp_TransmitRates, TransmitRates, strlen(TransmitRates));
developer72fb0bb2023-01-11 09:46:29 +08005937
developera3511852023-06-14 14:12:59 +08005938 for(i=0;i<strlen(temp_TransmitRates);i++)
5939 {
5940 //if (((temp_TransmitRates[i]>=48) && (temp_TransmitRates[i]<=57)) | (temp_TransmitRates[i]==32))
5941 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
5942 {
5943 continue;
5944 }
5945 else
5946 {
5947 return RETURN_ERR;
5948 }
5949 }
developera3511852023-06-14 14:12:59 +08005950 temp = strtok(temp_TransmitRates,",");
5951 while(temp!=NULL)
5952 {
developer32f2a182023-06-27 19:50:41 +08005953 if (strlen(temp) >= sizeof(temp1))
5954 return RETURN_ERR;
5955 strncpy(temp1, temp, strlen(temp));
developera3511852023-06-14 14:12:59 +08005956 if(band == band_5)
5957 {
5958 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
5959 {
5960 return RETURN_ERR;
5961 }
5962 }
developer72fb0bb2023-01-11 09:46:29 +08005963
developera3511852023-06-14 14:12:59 +08005964 if(strcmp(temp,"5.5")==0)
5965 {
developer32f2a182023-06-27 19:50:41 +08005966 memcpy(temp1, "55", 2);
developera3511852023-06-14 14:12:59 +08005967 }
5968 else
5969 {
developer32f2a182023-06-27 19:50:41 +08005970 if (strlen(temp1) >= (sizeof(temp1) - 1))
5971 return RETURN_ERR;
5972 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +08005973 }
developer32f2a182023-06-27 19:50:41 +08005974 if (strlen(temp1) >= (sizeof(temp_output) - strlen(temp_output)))
5975 return RETURN_ERR;
5976 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005977 temp = strtok(NULL,",");
5978 if(temp!=NULL)
5979 {
developer32f2a182023-06-27 19:50:41 +08005980 if (strlen(temp_output) >= (sizeof(temp_output) - 1))
5981 return RETURN_ERR;
5982 strncat(temp_output," ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +08005983 }
5984 }
developer32f2a182023-06-27 19:50:41 +08005985 memcpy(TransmitRates, temp_output, strlen(temp_output));
5986 TransmitRates[strlen(temp_output)] = '\0';
5987
developera3511852023-06-14 14:12:59 +08005988 params.name= "basic_rates";
5989 params.value =TransmitRates;
developer72fb0bb2023-01-11 09:46:29 +08005990
developera3511852023-06-14 14:12:59 +08005991 wifi_dbg_printf("\n%s:",__func__);
5992 wifi_dbg_printf("\nparams.value=%s\n",params.value);
5993 wifi_dbg_printf("\n******************Transmit rates=%s\n",TransmitRates);
developer32f2a182023-06-27 19:50:41 +08005994 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,radioIndex);
5995 if (os_snprintf_error(sizeof(config_file), res)) {
5996 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
5997 return RETURN_ERR;
5998 }
5999
developera3511852023-06-14 14:12:59 +08006000 wifi_hostapdWrite(config_file,&params,1);
6001 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6002 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006003}
6004
developer72fb0bb2023-01-11 09:46:29 +08006005INT wifi_halGetIfStatsNull(wifi_radioTrafficStats2_t *output_struct)
6006{
developera3511852023-06-14 14:12:59 +08006007 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6008 output_struct->radio_BytesSent = 0;
6009 output_struct->radio_BytesReceived = 0;
6010 output_struct->radio_PacketsSent = 0;
6011 output_struct->radio_PacketsReceived = 0;
6012 output_struct->radio_ErrorsSent = 0;
6013 output_struct->radio_ErrorsReceived = 0;
6014 output_struct->radio_DiscardPacketsSent = 0;
6015 output_struct->radio_DiscardPacketsReceived = 0;
6016 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6017 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006018}
6019
6020
6021INT wifi_halGetIfStats(char *ifname, wifi_radioTrafficStats2_t *pStats)
6022{
developera3511852023-06-14 14:12:59 +08006023 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6024 CHAR buf[MAX_CMD_SIZE] = {0};
6025 CHAR Value[MAX_BUF_SIZE] = {0};
6026 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08006027 int res;
developer37646972023-06-29 10:58:43 +08006028 unsigned long ret;
developer72fb0bb2023-01-11 09:46:29 +08006029
developera3511852023-06-14 14:12:59 +08006030 if (ifname == NULL || strlen(ifname) <= 1)
6031 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006032
developer33f13ba2023-07-12 16:19:06 +08006033 res = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", ifname);
6034 if (res) {
6035 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08006036 return RETURN_ERR;
6037 }
6038
developera3511852023-06-14 14:12:59 +08006039 fp = fopen("/tmp/Radio_Stats.txt", "r");
6040 if(fp == NULL)
6041 {
6042 printf("/tmp/Radio_Stats.txt not exists \n");
6043 return RETURN_ERR;
6044 }
developerd14dff12023-06-28 22:47:44 +08006045 if (fclose(fp) != 0) {
6046 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6047 return RETURN_ERR;
6048 }
developer72fb0bb2023-01-11 09:46:29 +08006049
developer75bd10c2023-06-27 11:34:08 +08006050 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6051 if (os_snprintf_error(sizeof(buf), res)) {
6052 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6053 return RETURN_ERR;
6054 }
developera3511852023-06-14 14:12:59 +08006055 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006056
6057 if (hal_strtoul(Value, 10, &ret) < 0) {
developer37646972023-06-29 10:58:43 +08006058 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +08006059 }
developerc14d83a2023-06-29 20:09:42 +08006060
developer37646972023-06-29 10:58:43 +08006061 pStats->radio_PacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006062
developer86035662023-06-28 19:21:12 +08006063 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6064 if (os_snprintf_error(sizeof(buf), res)) {
6065 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6066 return RETURN_ERR;
6067 }
developera3511852023-06-14 14:12:59 +08006068 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006069
6070 if (hal_strtoul(Value, 10, &ret) < 0) {
6071 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006072 }
developerc14d83a2023-06-29 20:09:42 +08006073
developer37646972023-06-29 10:58:43 +08006074 pStats->radio_PacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006075
developer86035662023-06-28 19:21:12 +08006076 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX bytes' | tr -s ' ' | cut -d ':' -f2 | cut -d ' ' -f1");
6077 if (os_snprintf_error(sizeof(buf), res)) {
6078 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6079 return RETURN_ERR;
6080 }
developera3511852023-06-14 14:12:59 +08006081 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006082 if (hal_strtoul(Value, 10, &ret) < 0) {
6083 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006084 }
6085 pStats->radio_BytesReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006086
developer86035662023-06-28 19:21:12 +08006087 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX bytes' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6088 if (os_snprintf_error(sizeof(buf), res)) {
6089 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6090 return RETURN_ERR;
6091 }
developera3511852023-06-14 14:12:59 +08006092 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006093 if (hal_strtoul(Value, 10, &ret) < 0) {
6094 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006095 }
6096 pStats->radio_BytesSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006097
developer86035662023-06-28 19:21:12 +08006098 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6099 if (os_snprintf_error(sizeof(buf), res)) {
6100 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6101 return RETURN_ERR;
6102 }
developera3511852023-06-14 14:12:59 +08006103 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006104 if (hal_strtoul(Value, 10, &ret) < 0) {
6105 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006106 }
6107 pStats->radio_ErrorsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006108
developer86035662023-06-28 19:21:12 +08006109 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
6110 if (os_snprintf_error(sizeof(buf), res)) {
6111 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6112 return RETURN_ERR;
6113 }
developera3511852023-06-14 14:12:59 +08006114 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006115 if (hal_strtoul(Value, 10, &ret) < 0) {
6116 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006117 }
6118 pStats->radio_ErrorsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006119
developer86035662023-06-28 19:21:12 +08006120 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
6121 if (os_snprintf_error(sizeof(buf), res)) {
6122 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6123 return RETURN_ERR;
6124 }
developera3511852023-06-14 14:12:59 +08006125 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08006126 if (hal_strtoul(Value, 10, &ret) < 0) {
6127 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006128 }
6129 pStats->radio_DiscardPacketsReceived = ret;
developer72fb0bb2023-01-11 09:46:29 +08006130
developer86035662023-06-28 19:21:12 +08006131 res = snprintf(buf, sizeof(buf), "cat /tmp/Radio_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | 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);
developerc14d83a2023-06-29 20:09:42 +08006137 if (hal_strtoul(Value, 10, &ret) < 0) {
6138 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08006139 }
6140 pStats->radio_DiscardPacketsSent = ret;
developer72fb0bb2023-01-11 09:46:29 +08006141
developera3511852023-06-14 14:12:59 +08006142 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6143 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006144}
6145
6146INT GetIfacestatus(CHAR *interface_name, CHAR *status)
6147{
developer7e4a2a62023-04-06 19:56:03 +08006148 CHAR buf[MAX_CMD_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006149 int res;
developer72fb0bb2023-01-11 09:46:29 +08006150
developer7e4a2a62023-04-06 19:56:03 +08006151 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6152
6153 if (interface_name != NULL && (strlen(interface_name) > 1) && status != NULL) {
developer75bd10c2023-06-27 11:34:08 +08006154 res = snprintf(buf, sizeof(buf), "%s%s%s%s%s", "ifconfig -a ",
6155 interface_name, " | grep ", interface_name, " | wc -l");
6156
6157 if (os_snprintf_error(sizeof(buf), res)) {
6158 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6159 return RETURN_ERR;
6160 }
developer7e4a2a62023-04-06 19:56:03 +08006161 File_Reading(buf, status);
6162 }
6163
6164 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
6165 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006166}
6167
6168//Get detail radio traffic static info
6169INT wifi_getRadioTrafficStats2(INT radioIndex, wifi_radioTrafficStats2_t *output_struct) //Tr181
6170{
developera3511852023-06-14 14:12:59 +08006171 CHAR interface_name[64] = {0};
6172 BOOL iface_status = FALSE;
6173 wifi_radioTrafficStats2_t radioTrafficStats = {0};
developer72fb0bb2023-01-11 09:46:29 +08006174
developera3511852023-06-14 14:12:59 +08006175 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
6176 if (NULL == output_struct)
6177 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006178
developera3511852023-06-14 14:12:59 +08006179 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6180 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006181
developera3511852023-06-14 14:12:59 +08006182 wifi_getApEnable(radioIndex, &iface_status);
developer72fb0bb2023-01-11 09:46:29 +08006183
developera3511852023-06-14 14:12:59 +08006184 if (iface_status == TRUE)
6185 wifi_halGetIfStats(interface_name, &radioTrafficStats);
6186 else
6187 wifi_halGetIfStatsNull(&radioTrafficStats); // just set some transmission statistic value to 0
developer72fb0bb2023-01-11 09:46:29 +08006188
developera3511852023-06-14 14:12:59 +08006189 output_struct->radio_BytesSent = radioTrafficStats.radio_BytesSent;
6190 output_struct->radio_BytesReceived = radioTrafficStats.radio_BytesReceived;
6191 output_struct->radio_PacketsSent = radioTrafficStats.radio_PacketsSent;
6192 output_struct->radio_PacketsReceived = radioTrafficStats.radio_PacketsReceived;
6193 output_struct->radio_ErrorsSent = radioTrafficStats.radio_ErrorsSent;
6194 output_struct->radio_ErrorsReceived = radioTrafficStats.radio_ErrorsReceived;
6195 output_struct->radio_DiscardPacketsSent = radioTrafficStats.radio_DiscardPacketsSent;
6196 output_struct->radio_DiscardPacketsReceived = radioTrafficStats.radio_DiscardPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +08006197
developera3511852023-06-14 14:12:59 +08006198 output_struct->radio_PLCPErrorCount = 0; //The number of packets that were received with a detected Physical Layer Convergence Protocol (PLCP) header error.
6199 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].
6200 output_struct->radio_InvalidMACCount = 0; //The number of packets that were received with a detected invalid MAC header error.
6201 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.
6202 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
6203 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
6204 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
6205 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
6206 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 +08006207
developera3511852023-06-14 14:12:59 +08006208 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
6209 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
6210 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
6211 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 +08006212
developera3511852023-06-14 14:12:59 +08006213 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006214
developera3511852023-06-14 14:12:59 +08006215 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006216}
6217
6218//Set radio traffic static Measureing rules
6219INT wifi_setRadioTrafficStatsMeasure(INT radioIndex, wifi_radioTrafficStatsMeasure_t *input_struct) //Tr181
6220{
developera39cfb22023-06-20 16:28:17 +08006221 char inf_name[IF_NAME_SIZE] = {0};
6222 unsigned int if_idx = 0;
6223 int ret = -1;
6224 struct unl unl_ins;
6225 struct nl_msg *msg = NULL;
6226 struct nlattr * msg_data = NULL;
6227 struct mtk_nl80211_param param;
6228
6229 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6230 return RETURN_ERR;
6231 if_idx = if_nametoindex(inf_name);
6232 if (!if_idx) {
6233 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6234 return RETURN_ERR;
6235 }
6236 /*init mtk nl80211 vendor cmd*/
6237 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6238 param.if_type = NL80211_ATTR_IFINDEX;
6239 param.if_idx = if_idx;
6240
6241 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6242 if (ret) {
6243 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6244 return RETURN_ERR;
6245 }
6246 /*add mtk vendor cmd data*/
6247 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD,
6248 sizeof(wifi_radioTrafficStatsMeasure_t), input_struct)) {
6249 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6250 MTK_NL80211_VENDOR_ATTR_RADIO_SET_STATS_MEASURING_METHOD);
6251 nlmsg_free(msg);
6252 goto err;
6253 }
6254
6255 /*send mtk nl80211 vendor msg*/
6256 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6257 if (ret) {
6258 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6259 goto err;
6260 }
6261 /*deinit mtk nl80211 vendor msg*/
6262 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006263 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006264err:
6265 mtk_nl80211_deint(&unl_ins);
6266 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6267 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006268}
6269
6270//To start or stop RadioTrafficStats
6271INT wifi_setRadioTrafficStatsRadioStatisticsEnable(INT radioIndex, BOOL enable)
6272{
developera39cfb22023-06-20 16:28:17 +08006273 char inf_name[IF_NAME_SIZE] = {0};
6274 unsigned int if_idx = 0;
6275 int ret = -1;
6276 struct unl unl_ins;
6277 struct nl_msg *msg = NULL;
6278 struct nlattr * msg_data = NULL;
6279 struct mtk_nl80211_param param;
6280
6281 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
6282 return RETURN_ERR;
6283 if_idx = if_nametoindex(inf_name);
6284 if (!if_idx) {
6285 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
6286 return RETURN_ERR;
6287 }
6288 /*init mtk nl80211 vendor cmd*/
6289 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_RADIO_STATS;
6290 param.if_type = NL80211_ATTR_IFINDEX;
6291 param.if_idx = if_idx;
6292
6293 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
6294 if (ret) {
6295 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
6296 return RETURN_ERR;
6297 }
6298 /*add mtk vendor cmd data*/
6299 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL, enable)) {
6300 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n",
6301 MTK_NL80211_VENDOR_ATTR_RADIO_SET_MEASURE_ENABEL);
6302 nlmsg_free(msg);
6303 goto err;
6304 }
6305
6306 /*send mtk nl80211 vendor msg*/
6307 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
6308 if (ret) {
6309 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
6310 goto err;
6311 }
6312 /*deinit mtk nl80211 vendor msg*/
6313 mtk_nl80211_deint(&unl_ins);
developera3511852023-06-14 14:12:59 +08006314 return RETURN_OK;
developera39cfb22023-06-20 16:28:17 +08006315err:
6316 mtk_nl80211_deint(&unl_ins);
6317 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
6318 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006319}
6320
6321//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
6322INT wifi_getRadioStatsReceivedSignalLevel(INT radioIndex, INT signalIndex, INT *SignalLevel) //Tr181
6323{
developera3511852023-06-14 14:12:59 +08006324 if (NULL == SignalLevel)
6325 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +08006326
developer9ce44382023-06-28 11:09:37 +08006327 *SignalLevel = -19;
developer72fb0bb2023-01-11 09:46:29 +08006328
developera3511852023-06-14 14:12:59 +08006329 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006330}
6331
6332//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6333INT wifi_applyRadioSettings(INT radioIndex)
6334{
developera3511852023-06-14 14:12:59 +08006335 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006336}
6337
6338//Get the radio index assocated with this SSID entry
6339INT wifi_getSSIDRadioIndex(INT ssidIndex, INT *radioIndex)
6340{
developera3511852023-06-14 14:12:59 +08006341 if(NULL == radioIndex)
6342 return RETURN_ERR;
6343 int max_radio_num = 0;
6344 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006345 if(max_radio_num == 0){
6346 return RETURN_ERR;
6347 }
developera3511852023-06-14 14:12:59 +08006348 *radioIndex = ssidIndex%max_radio_num;
6349 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006350}
6351
6352//Device.WiFi.SSID.{i}.Enable
6353//Get SSID enable configuration parameters (not the SSID enable status)
6354INT wifi_getSSIDEnable(INT ssidIndex, BOOL *output_bool) //Tr181
6355{
developera3511852023-06-14 14:12:59 +08006356 if (NULL == output_bool)
6357 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006358
developera3511852023-06-14 14:12:59 +08006359 return wifi_getApEnable(ssidIndex, output_bool);
developer72fb0bb2023-01-11 09:46:29 +08006360}
6361
6362//Device.WiFi.SSID.{i}.Enable
6363//Set SSID enable configuration parameters
6364INT wifi_setSSIDEnable(INT ssidIndex, BOOL enable) //Tr181
6365{
developera3511852023-06-14 14:12:59 +08006366 return wifi_setApEnable(ssidIndex, enable);
developer72fb0bb2023-01-11 09:46:29 +08006367}
6368
6369//Device.WiFi.SSID.{i}.Status
6370//Get the SSID enable status
6371INT wifi_getSSIDStatus(INT ssidIndex, CHAR *output_string) //Tr181
6372{
developer9ce44382023-06-28 11:09:37 +08006373 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +08006374 int res;
developer72fb0bb2023-01-11 09:46:29 +08006375
developera3511852023-06-14 14:12:59 +08006376 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6377 if (NULL == output_string)
6378 return RETURN_ERR;
developer69b61b02023-03-07 17:17:44 +08006379
developera3511852023-06-14 14:12:59 +08006380 wifi_getApEnable(ssidIndex,&output_bool);
developere40952c2023-06-15 18:46:43 +08006381 res = snprintf(output_string, 32, output_bool==1?"Enabled":"Disabled");
6382 if (os_snprintf_error(32, res)) {
6383 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6384 return RETURN_ERR;
6385 }
developer72fb0bb2023-01-11 09:46:29 +08006386
developera3511852023-06-14 14:12:59 +08006387 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6388 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006389}
6390
6391// Outputs a 32 byte or less string indicating the SSID name. Sring buffer must be preallocated by the caller.
6392INT wifi_getSSIDName(INT apIndex, CHAR *output)
6393{
developera3511852023-06-14 14:12:59 +08006394 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006395 int res;
developer72fb0bb2023-01-11 09:46:29 +08006396
developera3511852023-06-14 14:12:59 +08006397 if (NULL == output)
6398 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006399
developer75bd10c2023-06-27 11:34:08 +08006400 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6401 if (os_snprintf_error(sizeof(config_file), res)) {
6402 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6403 return RETURN_ERR;
6404 }
developera3511852023-06-14 14:12:59 +08006405 wifi_hostapdRead(config_file,"ssid",output,32);
developer72fb0bb2023-01-11 09:46:29 +08006406
developera3511852023-06-14 14:12:59 +08006407 wifi_dbg_printf("\n[%s]: SSID Name is : %s",__func__,output);
6408 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006409}
6410
developer69b61b02023-03-07 17:17:44 +08006411// Set a max 32 byte string and sets an internal variable to the SSID name
developer72fb0bb2023-01-11 09:46:29 +08006412INT wifi_setSSIDName(INT apIndex, CHAR *ssid_string)
6413{
developera3511852023-06-14 14:12:59 +08006414 struct params params;
6415 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08006416 int res;
developer72fb0bb2023-01-11 09:46:29 +08006417
developera3511852023-06-14 14:12:59 +08006418 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6419 if(NULL == ssid_string || strlen(ssid_string) >= 32 || strlen(ssid_string) == 0 )
6420 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006421
developera3511852023-06-14 14:12:59 +08006422 params.name = "ssid";
6423 params.value = ssid_string;
developer75bd10c2023-06-27 11:34:08 +08006424
6425 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
6426 if (os_snprintf_error(sizeof(config_file), res)) {
6427 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6428 return RETURN_ERR;
6429 }
6430
developera3511852023-06-14 14:12:59 +08006431 wifi_hostapdWrite(config_file, &params, 1);
6432 wifi_hostapdProcessUpdate(apIndex, &params, 1);
6433 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006434
developera3511852023-06-14 14:12:59 +08006435 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006436}
6437
6438//Get the BSSID
6439INT wifi_getBaseBSSID(INT ssidIndex, CHAR *output_string) //RDKB
6440{
developer7e4a2a62023-04-06 19:56:03 +08006441 char cmd[MAX_CMD_SIZE] = {0};
6442 char inf_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08006443 int res;
developer72fb0bb2023-01-11 09:46:29 +08006444
developera3511852023-06-14 14:12:59 +08006445 if (!output_string)
developerdaf24792023-06-06 11:40:04 +08006446 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006447
developer47cc27a2023-05-17 23:09:58 +08006448 if (wifi_GetInterfaceName(ssidIndex, inf_name) != RETURN_OK)
6449 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08006450
developer5b2f10c2023-05-25 17:02:21 +08006451 if (ssidIndex < 0 || ssidIndex > MAX_APS) {
6452 wifi_debug(DEBUG_ERROR, "innvalide ssidIdex(%d)\n", ssidIndex);
6453 strncpy(output_string, "\0", 1);
6454 return RETURN_ERR;
6455 }
developere40952c2023-06-15 18:46:43 +08006456 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep bssid | cut -d '=' -f2 | tr -d '\\n'", inf_name);
6457 if (os_snprintf_error(sizeof(cmd), res)) {
6458 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6459 return RETURN_ERR;
6460 }
6461
developer5b2f10c2023-05-25 17:02:21 +08006462 _syscmd(cmd, output_string, 64);
developer7e4a2a62023-04-06 19:56:03 +08006463
developer5b2f10c2023-05-25 17:02:21 +08006464 /* if hostapd does not control interface even if this interface has been brought up,
6465 * try to get its mac address by iw command.
6466 */
6467 if(strlen(output_string) == 0) {
6468 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08006469 res = snprintf(cmd, sizeof(cmd), "iw dev %s info | grep \"addr\" | awk \'{print $2}\'", inf_name);
6470 if (os_snprintf_error(sizeof(cmd), res)) {
6471 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6472 return RETURN_ERR;
6473 }
6474
developer5b2f10c2023-05-25 17:02:21 +08006475 _syscmd(cmd, output_string, 64);
6476 }
developer72fb0bb2023-01-11 09:46:29 +08006477
developer5b2f10c2023-05-25 17:02:21 +08006478 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006479}
6480
6481//Get the MAC address associated with this Wifi SSID
6482INT wifi_getSSIDMACAddress(INT ssidIndex, CHAR *output_string) //Tr181
6483{
developera3511852023-06-14 14:12:59 +08006484 wifi_getBaseBSSID(ssidIndex,output_string);
6485 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006486}
6487
6488//Get the basic SSID traffic static info
6489//Apply SSID and AP (in the case of Acess Point devices) to the hardware
6490//Not all implementations may need this function. If not needed for a particular implementation simply return no-error (0)
6491INT wifi_applySSIDSettings(INT ssidIndex)
6492{
developera3511852023-06-14 14:12:59 +08006493 char interface_name[16] = {0};
6494 BOOL status = false;
6495 char cmd[MAX_CMD_SIZE] = {0};
6496 char buf[MAX_CMD_SIZE] = {0};
6497 int apIndex, ret;
6498 int max_radio_num = 0;
6499 int radioIndex = 0;
developere40952c2023-06-15 18:46:43 +08006500 int res;
developer72fb0bb2023-01-11 09:46:29 +08006501
developera3511852023-06-14 14:12:59 +08006502 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08006503 if(max_radio_num == 0){
6504 return RETURN_ERR;
6505 }
developera3511852023-06-14 14:12:59 +08006506 radioIndex = ssidIndex % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +08006507
developera3511852023-06-14 14:12:59 +08006508 wifi_getApEnable(ssidIndex,&status);
6509 // Do not apply when ssid index is disabled
6510 if (status == false)
6511 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006512
developera3511852023-06-14 14:12:59 +08006513 /* Doing full remove and add for ssid Index
6514 * Not all hostapd options are supported with reload
6515 * for example macaddr_acl
6516 */
6517 if(wifi_setApEnable(ssidIndex,false) != RETURN_OK)
6518 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006519
developera3511852023-06-14 14:12:59 +08006520 ret = wifi_setApEnable(ssidIndex,true);
developer72fb0bb2023-01-11 09:46:29 +08006521
developera3511852023-06-14 14:12:59 +08006522 /* Workaround for hostapd issue with multiple bss definitions
6523 * when first created interface will be removed
6524 * then all vaps other vaps on same phy are removed
6525 * after calling setApEnable to false readd all enabled vaps */
6526 for(int i=0; i < MAX_APS/max_radio_num; i++) {
6527 apIndex = max_radio_num*i+radioIndex;
6528 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
6529 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006530 res = snprintf(cmd, sizeof(cmd), "cat %s | grep %s | cut -d'=' -f2", VAP_STATUS_FILE, interface_name);
6531 if (os_snprintf_error(sizeof(cmd), res)) {
6532 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6533 return RETURN_ERR;
6534 }
6535
developera3511852023-06-14 14:12:59 +08006536 _syscmd(cmd, buf, sizeof(buf));
6537 if(*buf == '1')
6538 wifi_setApEnable(apIndex, true);
6539 }
developer72fb0bb2023-01-11 09:46:29 +08006540
developera3511852023-06-14 14:12:59 +08006541 return ret;
developer72fb0bb2023-01-11 09:46:29 +08006542}
6543
6544struct channels_noise {
developera3511852023-06-14 14:12:59 +08006545 int channel;
6546 int noise;
developer72fb0bb2023-01-11 09:46:29 +08006547};
6548
6549// Return noise array for each channel
6550int get_noise(int radioIndex, struct channels_noise *channels_noise_arr, int channels_num)
6551{
developera3511852023-06-14 14:12:59 +08006552 char interface_name[16] = {0};
6553 FILE *f = NULL;
6554 char cmd[128] = {0};
6555 char line[256] = {0};
developer75bd10c2023-06-27 11:34:08 +08006556 int tmp = 0, arr_index = -1, res;
developer72fb0bb2023-01-11 09:46:29 +08006557
developera3511852023-06-14 14:12:59 +08006558 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6559 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08006560
6561 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep 'frequency\\|noise' | awk '{print $2}'", interface_name);
6562 if (os_snprintf_error(sizeof(cmd), res)) {
6563 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6564 return RETURN_ERR;
6565 }
developer72fb0bb2023-01-11 09:46:29 +08006566
developera3511852023-06-14 14:12:59 +08006567 if ((f = popen(cmd, "r")) == NULL) {
6568 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
6569 return RETURN_ERR;
6570 }
developer69b61b02023-03-07 17:17:44 +08006571
developera3511852023-06-14 14:12:59 +08006572 while(fgets(line, sizeof(line), f) != NULL) {
6573 if(arr_index < channels_num){
developer37646972023-06-29 10:58:43 +08006574 if (sscanf(line, "%d", &tmp) == EOF)
6575 continue;
developera3511852023-06-14 14:12:59 +08006576 if (tmp > 0) { // channel frequency, the first line must be frequency
6577 arr_index++;
6578 channels_noise_arr[arr_index].channel = ieee80211_frequency_to_channel(tmp);
6579 } else { // noise
6580 channels_noise_arr[arr_index].noise = tmp;
6581 }
6582 }else{
6583 break;
6584 }
6585 }
6586 pclose(f);
6587 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08006588}
6589
6590//Start the wifi scan and get the result into output buffer for RDKB to parser. The result will be used to manage endpoint list
6591//HAL funciton should allocate an data structure array, and return to caller with "neighbor_ap_array"
developer69b61b02023-03-07 17:17:44 +08006592INT wifi_getNeighboringWiFiDiagnosticResult2(INT radioIndex, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size) //Tr181
developer72fb0bb2023-01-11 09:46:29 +08006593{
developera3511852023-06-14 14:12:59 +08006594 int index = -1;
6595 wifi_neighbor_ap2_t *scan_array = NULL;
6596 char cmd[256]={0};
6597 char buf[128]={0};
6598 char file_name[32] = {0};
6599 char filter_SSID[32] = {0};
6600 char line[256] = {0};
6601 char interface_name[16] = {0};
6602 char *ret = NULL;
6603 int freq=0;
6604 FILE *f = NULL;
developerc14d83a2023-06-29 20:09:42 +08006605 unsigned long channels_num = 0;
developera3511852023-06-14 14:12:59 +08006606 int vht_channel_width = 0;
6607 int get_noise_ret = RETURN_ERR;
6608 bool filter_enable = false;
6609 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
developere40952c2023-06-15 18:46:43 +08006610 int phyId = 0, res;
developerc14d83a2023-06-29 20:09:42 +08006611 unsigned long len, tmp;
developer72fb0bb2023-01-11 09:46:29 +08006612
developera3511852023-06-14 14:12:59 +08006613 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08006614
developera3511852023-06-14 14:12:59 +08006615 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
6616 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08006617
6618 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radioIndex);
6619 if (os_snprintf_error(sizeof(file_name), res)) {
6620 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6621 return RETURN_ERR;
6622 }
developer72fb0bb2023-01-11 09:46:29 +08006623
developera3511852023-06-14 14:12:59 +08006624 f = fopen(file_name, "r");
6625 if (f != NULL) {
developerd14dff12023-06-28 22:47:44 +08006626 if (fgets(buf, sizeof(file_name), f) == NULL)
6627 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006628 if ((strncmp(buf, "0", 1)) != 0) {
developerd14dff12023-06-28 22:47:44 +08006629 if (fgets(filter_SSID, sizeof(file_name), f) == NULL)
6630 wifi_debug(DEBUG_ERROR, "fgets failed\n");
developera3511852023-06-14 14:12:59 +08006631 if (strlen(filter_SSID) != 0)
6632 filter_enable = true;
6633 }
developerd14dff12023-06-28 22:47:44 +08006634 if (fclose(f) != 0) {
6635 wifi_debug(DEBUG_ERROR, "fclose fail\n");
6636 return RETURN_ERR;
6637 }
developera3511852023-06-14 14:12:59 +08006638 }
developer72fb0bb2023-01-11 09:46:29 +08006639
developera3511852023-06-14 14:12:59 +08006640 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +08006641 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
6642 if (os_snprintf_error(sizeof(cmd), res)) {
6643 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6644 return RETURN_ERR;
6645 }
6646
developera3511852023-06-14 14:12:59 +08006647 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +08006648 if (hal_strtoul(buf, 10, &tmp) < 0) {
6649 wifi_debug(DEBUG_ERROR, "strtol fail\n");
6650 }
developer72fb0bb2023-01-11 09:46:29 +08006651
developerc14d83a2023-06-29 20:09:42 +08006652 channels_num = tmp;
developer32f2a182023-06-27 19:50:41 +08006653 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 +08006654 // 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 +08006655 if (os_snprintf_error(sizeof(cmd), res)) {
6656 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6657 return RETURN_ERR;
6658 }
developer86035662023-06-28 19:21:12 +08006659 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developera3511852023-06-14 14:12:59 +08006660 if ((f = popen(cmd, "r")) == NULL) {
developerb14b3462023-07-01 18:02:42 +08006661 wifi_debug(DEBUG_ERROR, "popen error\n");
developera3511852023-06-14 14:12:59 +08006662 return RETURN_ERR;
6663 }
developer9ce44382023-06-28 11:09:37 +08006664 struct channels_noise *channels_noise_arr = NULL;
6665 if(channels_num > 0 && channels_num <= 243){
6666 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
6667 } else{
developerb14b3462023-07-01 18:02:42 +08006668 wifi_debug(DEBUG_ERROR, "channel num = %ld!!\n", channels_num);
developer9ce44382023-06-28 11:09:37 +08006669 }
6670
6671 if(channels_noise_arr != NULL){
6672 get_noise_ret = get_noise(radioIndex, channels_noise_arr, channels_num);
6673 } else{
developerb14b3462023-07-01 18:02:42 +08006674 wifi_debug(DEBUG_ERROR, "channels_noise_arr is NULL!!\n");
developer9ce44382023-06-28 11:09:37 +08006675 }
6676
developer69b61b02023-03-07 17:17:44 +08006677
developera3511852023-06-14 14:12:59 +08006678 ret = fgets(line, sizeof(line), f);
6679 while (ret != NULL) {
6680 if(strstr(line, "BSS") != NULL) { // new neighbor info
6681 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
6682 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
6683 // 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 +08006684
developera3511852023-06-14 14:12:59 +08006685 if (!filter_BSS) {
6686 index++;
6687 wifi_neighbor_ap2_t *tmp;
6688 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
6689 if (tmp == NULL) { // no more memory to use
6690 index--;
6691 wifi_dbg_printf("%s: realloc failed\n", __func__);
6692 break;
6693 }
6694 scan_array = tmp;
6695 }
6696 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +08006697
developera3511852023-06-14 14:12:59 +08006698 filter_BSS = false;
developer86035662023-06-28 19:21:12 +08006699 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
6700 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6701 goto err;
6702 }
developerc79e9172023-06-06 19:48:03 +08006703 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +08006704 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +08006705 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +08006706 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +08006707 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +08006708 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
6709 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +08006710 if (sscanf(line," freq: %d", &freq) != 1) {
6711 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006712 }
developera3511852023-06-14 14:12:59 +08006713 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +08006714
developera3511852023-06-14 14:12:59 +08006715 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +08006716 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006717 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +08006718 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006719 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +08006720 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006721 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
6722 }
6723 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +08006724 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +08006725 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +08006726 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +08006727 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +08006728 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +08006729 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
6730 }
developer72fb0bb2023-01-11 09:46:29 +08006731
developera3511852023-06-14 14:12:59 +08006732 scan_array[index].ap_Noise = 0;
6733 if (get_noise_ret == RETURN_OK) {
6734 for (int i = 0; i < channels_num; i++) {
6735 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
6736 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
6737 break;
6738 }
6739 }
6740 }
6741 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +08006742 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
6743 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6744 goto err;
6745 }
developera3511852023-06-14 14:12:59 +08006746 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +08006747 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
6748 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6749 goto err;
6750 }
developera3511852023-06-14 14:12:59 +08006751 } else if (strstr(line,"SSID") != NULL) {
developer86035662023-06-28 19:21:12 +08006752 if (sscanf(line," SSID: %32s", scan_array[index].ap_SSID) != 1) {
6753 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +08006754 //goto err;
developer86035662023-06-28 19:21:12 +08006755 }
developera3511852023-06-14 14:12:59 +08006756 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) != 0) {
6757 filter_BSS = true;
6758 }
6759 } else if (strstr(line, "Supported rates") != NULL) {
6760 char SRate[80] = {0}, *tmp = NULL;
6761 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +08006762 if (strlen(line) >= sizeof(SRate))
developer86035662023-06-28 19:21:12 +08006763 goto err;
developer32f2a182023-06-27 19:50:41 +08006764 strncpy(SRate, line, strlen(line));
developera3511852023-06-14 14:12:59 +08006765 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +08006766 if (tmp == NULL)
6767 goto err;
developera3511852023-06-14 14:12:59 +08006768 tmp = strtok(NULL, ":");
developer86035662023-06-28 19:21:12 +08006769 if (tmp == NULL)
6770 goto err;
developer32f2a182023-06-27 19:50:41 +08006771 if (strlen(tmp) >= sizeof(buf))
developer86035662023-06-28 19:21:12 +08006772 goto err;
developer32f2a182023-06-27 19:50:41 +08006773 strncpy(buf, tmp, strlen(tmp));
developera3511852023-06-14 14:12:59 +08006774 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +08006775
developera3511852023-06-14 14:12:59 +08006776 tmp = strtok(buf, " \n");
6777 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +08006778 if (strlen(tmp) >= (sizeof(SRate) - strlen(SRate)))
developer86035662023-06-28 19:21:12 +08006779 goto err;
developer32f2a182023-06-27 19:50:41 +08006780 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +08006781 if (SRate[strlen(SRate) - 1] == '*') {
6782 SRate[strlen(SRate) - 1] = '\0';
6783 }
developer32f2a182023-06-27 19:50:41 +08006784 if (strlen(SRate) >= (sizeof(SRate) - 1))
developer86035662023-06-28 19:21:12 +08006785 goto err;
developer32f2a182023-06-27 19:50:41 +08006786 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +08006787
developera3511852023-06-14 14:12:59 +08006788 tmp = strtok(NULL, " \n");
6789 }
6790 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +08006791 if (sizeof(scan_array[index].ap_SupportedDataTransferRates) <= strlen(SRate))
developer86035662023-06-28 19:21:12 +08006792 goto err;
developer32f2a182023-06-27 19:50:41 +08006793 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, strlen(SRate));
developera3511852023-06-14 14:12:59 +08006794 } else if (strstr(line, "DTIM") != NULL) {
developer86035662023-06-28 19:21:12 +08006795 if (sscanf(line,"DTIM Period %u", &(scan_array[index].ap_DTIMPeriod)) != 1) {
6796 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer86035662023-06-28 19:21:12 +08006797 }
developera3511852023-06-14 14:12:59 +08006798 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006799 if (sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) <= 4)
developer86035662023-06-28 19:21:12 +08006800 goto err;
developer32f2a182023-06-27 19:50:41 +08006801 strncat(scan_array[index].ap_SupportedStandards, ",ac",
6802 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6803 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
6804 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +08006805 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006806 strncat(scan_array[index].ap_SupportedStandards, ",n", sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6807 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
6808 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +08006809 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006810 if (fgets(line, sizeof(line), f) == NULL) {
6811 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6812 goto err;
6813 }
6814 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
6815 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6816 goto err;
6817 }
developera3511852023-06-14 14:12:59 +08006818 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +08006819 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +08006820 } else {
developere40952c2023-06-15 18:46:43 +08006821 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +08006822 }
developere40952c2023-06-15 18:46:43 +08006823 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6824 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +08006825 goto err;
developere40952c2023-06-15 18:46:43 +08006826 }
6827
developera3511852023-06-14 14:12:59 +08006828 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6829 continue;
6830 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +08006831 if (fgets(line, sizeof(line), f) == NULL) {
6832 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6833 goto err;
6834 }
developerc14d83a2023-06-29 20:09:42 +08006835 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
developer86035662023-06-28 19:21:12 +08006836 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6837 goto err;
6838 }
developera3511852023-06-14 14:12:59 +08006839 if (!strcmp(buf, "above")) {
6840 //40Mhz +
developere40952c2023-06-15 18:46:43 +08006841 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 +08006842 }
6843 else if (!strcmp(buf, "below")) {
6844 //40Mhz -
developere40952c2023-06-15 18:46:43 +08006845 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 +08006846 } else {
6847 //20Mhz
developere40952c2023-06-15 18:46:43 +08006848 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11N%s_HT20", radioIndex%1 ? "A": "G");
6849 }
6850 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
6851 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +08006852 goto err;
developera3511852023-06-14 14:12:59 +08006853 }
developere40952c2023-06-15 18:46:43 +08006854
developera3511852023-06-14 14:12:59 +08006855 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
6856 continue;
6857 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006858 strncat(scan_array[index].ap_SupportedStandards, ",ax",
6859 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
6860 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
6861 scan_array[index].ap_OperatingStandards[2] = '\0';
developer86035662023-06-28 19:21:12 +08006862 if (fgets(line, sizeof(line), f) == NULL) {
6863 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6864 goto err;
6865 }
developera3511852023-06-14 14:12:59 +08006866 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006867 if (strstr(line, "HE40/2.4GHz") != NULL) {
6868 len = strlen("11AXHE40PLUS");
6869 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
6870 } else {
6871 len = strlen("11AXHE20");
6872 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
6873 }
6874 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +08006875 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
6876 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006877 len = strlen("11AXHE80");
6878 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
6879 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developer86035662023-06-28 19:21:12 +08006880 if (fgets(line, sizeof(line), f) == NULL) {
6881 wifi_debug(DEBUG_ERROR, "fgets fail\n");
6882 goto err;
6883 }
developera3511852023-06-14 14:12:59 +08006884 } else
6885 continue;
developer32f2a182023-06-27 19:50:41 +08006886 if (strstr(line, "HE160/5GHz") != NULL) {
6887 len = strlen("11AXHE160");
6888 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
6889 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
6890 }
developera3511852023-06-14 14:12:59 +08006891 }
6892 continue;
6893 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006894 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
6895 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006896 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +08006897 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
6898 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +08006899 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +08006900 if (sscanf(line, " * Group cipher: %64s", scan_array[index].ap_EncryptionMode) != 1) {
6901 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
6902 goto err;
6903 }
developera3511852023-06-14 14:12:59 +08006904 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +08006905 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
6906 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +08006907 }
6908 }
developerb14b3462023-07-01 18:02:42 +08006909 ret = fgets(line, sizeof(line), f);
developera3511852023-06-14 14:12:59 +08006910 }
developer72fb0bb2023-01-11 09:46:29 +08006911
developera3511852023-06-14 14:12:59 +08006912 if (!filter_BSS) {
6913 *output_array_size = index + 1;
6914 } else {
6915 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
6916 *output_array_size = index;
6917 }
6918 *neighbor_ap_array = scan_array;
6919 pclose(f);
6920 free(channels_noise_arr);
6921 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6922 return RETURN_OK;
developer86035662023-06-28 19:21:12 +08006923err:
6924 pclose(f);
6925 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +08006926 if (scan_array)
6927 free(scan_array);
developer86035662023-06-28 19:21:12 +08006928 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6929 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006930}
6931
6932//>> Deprecated: used for old RDKB code.
6933INT wifi_getRadioWifiTrafficStats(INT radioIndex, wifi_radioTrafficStats_t *output_struct)
6934{
developera3511852023-06-14 14:12:59 +08006935 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006936
developera3511852023-06-14 14:12:59 +08006937 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6938 output_struct->wifi_PLCPErrorCount = 0;
6939 output_struct->wifi_FCSErrorCount = 0;
6940 output_struct->wifi_InvalidMACCount = 0;
6941 output_struct->wifi_PacketsOtherReceived = 0;
6942 output_struct->wifi_Noise = 0;
6943 status = RETURN_OK;
6944 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
6945 return status;
developer72fb0bb2023-01-11 09:46:29 +08006946}
6947
6948INT wifi_getBasicTrafficStats(INT apIndex, wifi_basicTrafficStats_t *output_struct)
6949{
developera3511852023-06-14 14:12:59 +08006950 char interface_name[16] = {0};
6951 char cmd[128] = {0};
6952 char buf[1280] = {0};
6953 char *pos = NULL;
developere40952c2023-06-15 18:46:43 +08006954 int res;
developer72fb0bb2023-01-11 09:46:29 +08006955
developera3511852023-06-14 14:12:59 +08006956 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
6957 if (NULL == output_struct)
6958 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006959
developera3511852023-06-14 14:12:59 +08006960 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
6961 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08006962
developera3511852023-06-14 14:12:59 +08006963 memset(output_struct, 0, sizeof(wifi_basicTrafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08006964
developere40952c2023-06-15 18:46:43 +08006965 res = snprintf(cmd, sizeof(cmd), "ifconfig %s", interface_name);
6966 if (os_snprintf_error(sizeof(cmd), res)) {
6967 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6968 return RETURN_ERR;
6969 }
6970
developera3511852023-06-14 14:12:59 +08006971 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08006972
developera3511852023-06-14 14:12:59 +08006973 pos = buf;
6974 if ((pos = strstr(pos, "RX packets:")) == NULL)
6975 return RETURN_ERR;
6976 output_struct->wifi_PacketsReceived = atoi(pos+strlen("RX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08006977
developera3511852023-06-14 14:12:59 +08006978 if ((pos = strstr(pos, "TX packets:")) == NULL)
6979 return RETURN_ERR;
6980 output_struct->wifi_PacketsSent = atoi(pos+strlen("TX packets:"));
developer72fb0bb2023-01-11 09:46:29 +08006981
developera3511852023-06-14 14:12:59 +08006982 if ((pos = strstr(pos, "RX bytes:")) == NULL)
6983 return RETURN_ERR;
6984 output_struct->wifi_BytesReceived = atoi(pos+strlen("RX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08006985
developera3511852023-06-14 14:12:59 +08006986 if ((pos = strstr(pos, "TX bytes:")) == NULL)
6987 return RETURN_ERR;
6988 output_struct->wifi_BytesSent = atoi(pos+strlen("TX bytes:"));
developer72fb0bb2023-01-11 09:46:29 +08006989
developer37646972023-06-29 10:58:43 +08006990 res = snprintf(cmd, sizeof(cmd),
6991 "hostapd_cli -i %s list_sta | wc -l | tr -d '\n'", interface_name);
6992 if (os_snprintf_error(sizeof(cmd), res)) {
6993 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
6994 return RETURN_ERR;
6995 }
developera3511852023-06-14 14:12:59 +08006996 _syscmd(cmd, buf, sizeof(buf));
developer37646972023-06-29 10:58:43 +08006997 if (sscanf(buf, "%lu", &output_struct->wifi_Associations) == EOF)
6998 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developer72fb0bb2023-01-11 09:46:29 +08006999
developera3511852023-06-14 14:12:59 +08007000 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7001 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007002}
7003
7004INT wifi_getWifiTrafficStats(INT apIndex, wifi_trafficStats_t *output_struct)
7005{
developera3511852023-06-14 14:12:59 +08007006 char interface_name[IF_NAME_SIZE] = {0};
7007 char interface_status[MAX_BUF_SIZE] = {0};
7008 char Value[MAX_BUF_SIZE] = {0};
7009 char buf[MAX_CMD_SIZE] = {0};
developera3511852023-06-14 14:12:59 +08007010 FILE *fp = NULL;
developere40952c2023-06-15 18:46:43 +08007011 int res;
developerc14d83a2023-06-29 20:09:42 +08007012 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +08007013
developera3511852023-06-14 14:12:59 +08007014 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7015 if (NULL == output_struct)
7016 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007017
developera3511852023-06-14 14:12:59 +08007018 memset(output_struct, 0, sizeof(wifi_trafficStats_t));
developer72fb0bb2023-01-11 09:46:29 +08007019
developera3511852023-06-14 14:12:59 +08007020 if (wifi_GetInterfaceName(apIndex,interface_name) != RETURN_OK)
7021 return RETURN_ERR;
7022 GetIfacestatus(interface_name, interface_status);
developer72fb0bb2023-01-11 09:46:29 +08007023
developera3511852023-06-14 14:12:59 +08007024 if(0 != strcmp(interface_status, "1"))
7025 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08007026
developer33f13ba2023-07-12 16:19:06 +08007027 res = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", interface_name);
7028 if (res) {
7029 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +08007030 return RETURN_ERR;
7031 }
developer72fb0bb2023-01-11 09:46:29 +08007032
developera3511852023-06-14 14:12:59 +08007033 fp = fopen("/tmp/SSID_Stats.txt", "r");
7034 if(fp == NULL)
7035 {
7036 printf("/tmp/SSID_Stats.txt not exists \n");
7037 return RETURN_ERR;
7038 }
developer37646972023-06-29 10:58:43 +08007039 if (fclose(fp) == EOF) {
7040 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
7041 return RETURN_ERR;
7042 }
developer72fb0bb2023-01-11 09:46:29 +08007043
developer37646972023-06-29 10:58:43 +08007044 res = snprintf(buf, sizeof(buf),
7045 "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7046 if (os_snprintf_error(sizeof(buf), res)) {
7047 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7048 return RETURN_ERR;
7049 }
developera3511852023-06-14 14:12:59 +08007050 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007051
7052 if (hal_strtoul(Value, 10, &tmp) < 0) {
7053 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007054 }
developerc14d83a2023-06-29 20:09:42 +08007055 output_struct->wifi_ErrorsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007056
developer37646972023-06-29 10:58:43 +08007057 res = snprintf(buf, sizeof(buf),
7058 "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f3 | cut -d ' ' -f1");
7059 if (os_snprintf_error(sizeof(buf), res)) {
7060 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7061 return RETURN_ERR;
7062 }
developera3511852023-06-14 14:12:59 +08007063 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007064
7065 if (hal_strtoul(Value, 10, &tmp) < 0) {
7066 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007067 }
developerc14d83a2023-06-29 20:09:42 +08007068 output_struct->wifi_ErrorsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007069
developer37646972023-06-29 10:58:43 +08007070 res = snprintf(buf, sizeof(buf),
7071 "cat /tmp/SSID_Stats.txt | grep 'RX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7072 if (os_snprintf_error(sizeof(buf), res)) {
7073 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7074 return RETURN_ERR;
7075 }
developera3511852023-06-14 14:12:59 +08007076 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007077
7078 if (hal_strtoul(Value, 10, &tmp) < 0) {
7079 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007080 }
developerc14d83a2023-06-29 20:09:42 +08007081 output_struct->wifi_DiscardedPacketsReceived = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007082
developer37646972023-06-29 10:58:43 +08007083 res = snprintf(buf, sizeof(buf),
7084 "cat /tmp/SSID_Stats.txt | grep 'TX packets' | tr -s ' ' | cut -d ':' -f4 | cut -d ' ' -f1");
7085 if (os_snprintf_error(sizeof(buf), res)) {
7086 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7087 return RETURN_ERR;
7088 }
developera3511852023-06-14 14:12:59 +08007089 File_Reading(buf, Value);
developerc14d83a2023-06-29 20:09:42 +08007090
7091 if (hal_strtoul(Value, 10, &tmp) < 0) {
7092 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +08007093 }
developerc14d83a2023-06-29 20:09:42 +08007094 output_struct->wifi_DiscardedPacketsSent = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007095
developera3511852023-06-14 14:12:59 +08007096 output_struct->wifi_UnicastPacketsSent = 0;
7097 output_struct->wifi_UnicastPacketsReceived = 0;
7098 output_struct->wifi_MulticastPacketsSent = 0;
7099 output_struct->wifi_MulticastPacketsReceived = 0;
7100 output_struct->wifi_BroadcastPacketsSent = 0;
7101 output_struct->wifi_BroadcastPacketsRecevied = 0;
7102 output_struct->wifi_UnknownPacketsReceived = 0;
developer72fb0bb2023-01-11 09:46:29 +08007103
developera3511852023-06-14 14:12:59 +08007104 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7105 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007106}
7107
7108INT wifi_getSSIDTrafficStats(INT apIndex, wifi_ssidTrafficStats_t *output_struct)
7109{
developera3511852023-06-14 14:12:59 +08007110 INT status = RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007111
developera3511852023-06-14 14:12:59 +08007112 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7113 //Below values should get updated from hal
7114 output_struct->wifi_RetransCount=0;
7115 output_struct->wifi_FailedRetransCount=0;
7116 output_struct->wifi_RetryCount=0;
7117 output_struct->wifi_MultipleRetryCount=0;
7118 output_struct->wifi_ACKFailureCount=0;
7119 output_struct->wifi_AggregatedPacketCount=0;
developer72fb0bb2023-01-11 09:46:29 +08007120
developera3511852023-06-14 14:12:59 +08007121 status = RETURN_OK;
7122 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007123
developera3511852023-06-14 14:12:59 +08007124 return status;
developer72fb0bb2023-01-11 09:46:29 +08007125}
7126
7127INT wifi_getNeighboringWiFiDiagnosticResult(wifi_neighbor_ap_t **neighbor_ap_array, UINT *output_array_size)
7128{
developera3511852023-06-14 14:12:59 +08007129 INT status = RETURN_ERR;
7130 UINT index;
7131 wifi_neighbor_ap_t *pt=NULL;
developer72fb0bb2023-01-11 09:46:29 +08007132
developera3511852023-06-14 14:12:59 +08007133 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7134 *output_array_size=2;
7135 //zqiu: HAL alloc the array and return to caller. Caller response to free it.
7136 *neighbor_ap_array=(wifi_neighbor_ap_t *)calloc(sizeof(wifi_neighbor_ap_t), *output_array_size);
developer86035662023-06-28 19:21:12 +08007137 if (*neighbor_ap_array == NULL) {
7138 wifi_debug(DEBUG_ERROR, "calloc fail!\n");
7139 return RETURN_ERR;
7140 }
developera3511852023-06-14 14:12:59 +08007141 for (index = 0, pt=*neighbor_ap_array; index < *output_array_size; index++, pt++) {
developer32f2a182023-06-27 19:50:41 +08007142 pt->ap_Radio[0] = '\0';
7143 pt->ap_SSID[0] = '\0';
7144 pt->ap_BSSID[0] = '\0';
7145 pt->ap_Mode[0] = '\0';
developera3511852023-06-14 14:12:59 +08007146 pt->ap_Channel=1;
7147 pt->ap_SignalStrength=0;
developer32f2a182023-06-27 19:50:41 +08007148 pt->ap_SecurityModeEnabled[0] = '\0';
7149 pt->ap_EncryptionMode[0] = '\0';
7150 pt->ap_OperatingFrequencyBand[0] = '\0';
7151 pt->ap_SupportedStandards[0] = '\0';
7152 pt->ap_OperatingStandards[0] = '\0';
7153 pt->ap_OperatingChannelBandwidth[0] = '\0';
7154 pt->ap_BasicDataTransferRates[0] = '\0';
7155 pt->ap_SupportedDataTransferRates[0] = '\0';
developera3511852023-06-14 14:12:59 +08007156 pt->ap_BeaconPeriod=1;
7157 pt->ap_Noise=0;
developera3511852023-06-14 14:12:59 +08007158 pt->ap_DTIMPeriod=1;
7159 pt->ap_ChannelUtilization = 1;
7160 }
developer72fb0bb2023-01-11 09:46:29 +08007161
developera3511852023-06-14 14:12:59 +08007162 status = RETURN_OK;
7163 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007164
developera3511852023-06-14 14:12:59 +08007165 return status;
developer72fb0bb2023-01-11 09:46:29 +08007166}
7167
7168//----------------- AP HAL -------------------------------
7169
7170//>> Deprecated: used for old RDKB code.
7171INT wifi_getAllAssociatedDeviceDetail(INT apIndex, ULONG *output_ulong, wifi_device_t **output_struct)
7172{
developera3511852023-06-14 14:12:59 +08007173 if (NULL == output_ulong || NULL == output_struct)
7174 return RETURN_ERR;
7175 *output_ulong = 0;
7176 *output_struct = NULL;
7177 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007178}
7179
7180#ifdef HAL_NETLINK_IMPL
7181static int AssoDevInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +08007182 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7183 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +08007184 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
7185 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +08007186 char mac_addr[20];
7187 static int count=0;
7188 int rate=0;
developer72fb0bb2023-01-11 09:46:29 +08007189
developera3511852023-06-14 14:12:59 +08007190 wifi_device_info_t *out = (wifi_device_info_t*)arg;
developer72fb0bb2023-01-11 09:46:29 +08007191
developera3511852023-06-14 14:12:59 +08007192 nla_parse(tb,
7193 NL80211_ATTR_MAX,
7194 genlmsg_attrdata(gnlh, 0),
7195 genlmsg_attrlen(gnlh, 0),
7196 NULL);
developer72fb0bb2023-01-11 09:46:29 +08007197
developera3511852023-06-14 14:12:59 +08007198 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +08007199 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +08007200 return NL_SKIP;
7201 }
developer72fb0bb2023-01-11 09:46:29 +08007202
7203
developera3511852023-06-14 14:12:59 +08007204 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +08007205 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +08007206 return NL_SKIP;
7207 }
developer72fb0bb2023-01-11 09:46:29 +08007208
developera3511852023-06-14 14:12:59 +08007209 //devIndex starts from 1
7210 if( ++count == out->wifi_devIndex )
7211 {
7212 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
7213 //Getting the mac addrress
7214 mac_addr_aton(out->wifi_devMacAddress,mac_addr);
developer12fb9f62023-06-30 15:26:27 +08007215 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7216 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
7217 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7218 return NL_SKIP;
7219 }
developera3511852023-06-14 14:12:59 +08007220 }
developer72fb0bb2023-01-11 09:46:29 +08007221
developera3511852023-06-14 14:12:59 +08007222 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
7223 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7224 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7225 out->wifi_devTxRate = rate/10;
7226 }
7227 }
developer72fb0bb2023-01-11 09:46:29 +08007228
developer12fb9f62023-06-30 15:26:27 +08007229 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7230 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) {
7231 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
7232 return NL_SKIP;
7233 }
developera3511852023-06-14 14:12:59 +08007234 }
developera3511852023-06-14 14:12:59 +08007235 if(sinfo[NL80211_STA_INFO_RX_BITRATE]) {
7236 if(rinfo[NL80211_RATE_INFO_BITRATE]) {
7237 rate=nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
7238 out->wifi_devRxRate = rate/10;
7239 }
7240 }
7241 if(sinfo[NL80211_STA_INFO_SIGNAL_AVG])
7242 out->wifi_devSignalStrength = (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
developer72fb0bb2023-01-11 09:46:29 +08007243
developera3511852023-06-14 14:12:59 +08007244 out->wifi_devAssociatedDeviceAuthentiationState = 1;
7245 count = 0; //starts the count for next cycle
7246 return NL_STOP;
7247 }
developer72fb0bb2023-01-11 09:46:29 +08007248
developera3511852023-06-14 14:12:59 +08007249 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +08007250
7251}
7252#endif
7253
7254INT wifi_getAssociatedDeviceDetail(INT apIndex, INT devIndex, wifi_device_t *output_struct)
7255{
developera3511852023-06-14 14:12:59 +08007256 Netlink nl = {0};
7257 char if_name[IF_NAME_SIZE] = {0};
7258 char interface_name[16] = {0};
developere40952c2023-06-15 18:46:43 +08007259 int res;
developer72fb0bb2023-01-11 09:46:29 +08007260
developera3511852023-06-14 14:12:59 +08007261 wifi_device_info_t info = {0};
7262 info.wifi_devIndex = devIndex;
developer72fb0bb2023-01-11 09:46:29 +08007263
developera3511852023-06-14 14:12:59 +08007264 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
7265 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007266
developere40952c2023-06-15 18:46:43 +08007267 res = snprintf(if_name,sizeof(if_name),"%s", interface_name);
7268 if (os_snprintf_error(sizeof(if_name), res)) {
7269 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7270 return RETURN_ERR;
7271 }
developer72fb0bb2023-01-11 09:46:29 +08007272
developera3511852023-06-14 14:12:59 +08007273 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007274
developera3511852023-06-14 14:12:59 +08007275 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +08007276 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +08007277 return -1;
7278 }
developer72fb0bb2023-01-11 09:46:29 +08007279
developera3511852023-06-14 14:12:59 +08007280 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +08007281
developera3511852023-06-14 14:12:59 +08007282 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +08007283 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +08007284 nlfree(&nl);
7285 return -2;
7286 }
developer72fb0bb2023-01-11 09:46:29 +08007287
developera3511852023-06-14 14:12:59 +08007288 genlmsg_put(msg,
7289 NL_AUTO_PID,
7290 NL_AUTO_SEQ,
7291 nl.id,
7292 0,
7293 NLM_F_DUMP,
7294 NL80211_CMD_GET_STATION,
7295 0);
developer72fb0bb2023-01-11 09:46:29 +08007296
developera3511852023-06-14 14:12:59 +08007297 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
7298 nl_send_auto_complete(nl.socket, msg);
7299 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,AssoDevInfo_callback,&info);
7300 nl_recvmsgs(nl.socket, nl.cb);
7301 nlmsg_free(msg);
7302 nlfree(&nl);
developer72fb0bb2023-01-11 09:46:29 +08007303
developera3511852023-06-14 14:12:59 +08007304 output_struct->wifi_devAssociatedDeviceAuthentiationState = info.wifi_devAssociatedDeviceAuthentiationState;
7305 output_struct->wifi_devRxRate = info.wifi_devRxRate;
7306 output_struct->wifi_devTxRate = info.wifi_devTxRate;
7307 output_struct->wifi_devSignalStrength = info.wifi_devSignalStrength;
7308 memcpy(&output_struct->wifi_devMacAddress, &info.wifi_devMacAddress, sizeof(info.wifi_devMacAddress));
7309 return RETURN_OK;
7310}
developer72fb0bb2023-01-11 09:46:29 +08007311
developera3511852023-06-14 14:12:59 +08007312INT wifi_kickAssociatedDevice(INT apIndex, wifi_device_t *device)
7313{
7314 if (NULL == device)
7315 return RETURN_ERR;
7316 return RETURN_OK;
7317}
7318//<<
developer72fb0bb2023-01-11 09:46:29 +08007319
developer72fb0bb2023-01-11 09:46:29 +08007320
7321//--------------wifi_ap_hal-----------------------------
7322//enables CTS protection for the radio used by this AP
7323INT wifi_setRadioCtsProtectionEnable(INT apIndex, BOOL enable)
7324{
developera3511852023-06-14 14:12:59 +08007325 //save config and Apply instantly
7326 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007327}
7328
7329// enables OBSS Coexistence - fall back to 20MHz if necessary for the radio used by this ap
7330INT wifi_setRadioObssCoexistenceEnable(INT apIndex, BOOL enable)
7331{
developera3511852023-06-14 14:12:59 +08007332 char config_file[64] = {'\0'};
7333 char config_dat_file[64] = {'\0'};
7334 char buf[64] = {'\0'};
7335 struct params list = {0};
7336 struct params dat = {0};
7337 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007338 int res;
developer72fb0bb2023-01-11 09:46:29 +08007339
developera3511852023-06-14 14:12:59 +08007340 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7341 list.name = "ht_coex";
developere40952c2023-06-15 18:46:43 +08007342 res = snprintf(buf, sizeof(buf), "%d", enable);
7343 if (os_snprintf_error(sizeof(buf), res)) {
7344 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7345 return RETURN_ERR;
7346 }
7347
developera3511852023-06-14 14:12:59 +08007348 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007349
developera3511852023-06-14 14:12:59 +08007350 dat.name = "HT_BSSCoexistence";
7351 dat.value = buf;
developerd1824452023-05-18 12:30:04 +08007352
developera3511852023-06-14 14:12:59 +08007353 band = wifi_index_to_band(apIndex);
developere40952c2023-06-15 18:46:43 +08007354 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7355 if (os_snprintf_error(sizeof(config_file), res)) {
7356 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7357 return RETURN_ERR;
7358 }
7359
7360 res = snprintf(config_dat_file, sizeof(config_dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7361 if (os_snprintf_error(sizeof(config_dat_file), res)) {
7362 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7363 return RETURN_ERR;
7364 }
7365
developera3511852023-06-14 14:12:59 +08007366 wifi_hostapdWrite(config_file, &list, 1);
7367 wifi_datfileWrite(config_dat_file, &dat, 1);
7368 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007369
developera3511852023-06-14 14:12:59 +08007370 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007371
developera3511852023-06-14 14:12:59 +08007372 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007373}
7374
7375//P3 // sets the fragmentation threshold in bytes for the radio used by this ap
7376INT wifi_setRadioFragmentationThreshold(INT apIndex, UINT threshold)
7377{
developera3511852023-06-14 14:12:59 +08007378 char config_file[MAX_BUF_SIZE] = {'\0'};
7379 char buf[MAX_BUF_SIZE] = {'\0'};
7380 struct params list;
developere40952c2023-06-15 18:46:43 +08007381 int res;
developer72fb0bb2023-01-11 09:46:29 +08007382
developera3511852023-06-14 14:12:59 +08007383 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7384 if (threshold < 256 || threshold > 2346 )
7385 return RETURN_ERR;
7386 list.name = "fragm_threshold";
developere40952c2023-06-15 18:46:43 +08007387 res = snprintf(buf, sizeof(buf), "%d", threshold);
7388 if (os_snprintf_error(sizeof(buf), res)) {
7389 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7390 return RETURN_ERR;
7391 }
7392
developera3511852023-06-14 14:12:59 +08007393 list.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08007394
developere40952c2023-06-15 18:46:43 +08007395 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
7396 if (os_snprintf_error(sizeof(config_file), res)) {
7397 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7398 return RETURN_ERR;
7399 }
7400
developera3511852023-06-14 14:12:59 +08007401 wifi_hostapdWrite(config_file, &list, 1);
7402 wifi_hostapdProcessUpdate(apIndex, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +08007403
developera3511852023-06-14 14:12:59 +08007404 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007405
developera3511852023-06-14 14:12:59 +08007406 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007407}
7408
7409// enable STBC mode in the hardwarwe, 0 == not enabled, 1 == enabled
7410INT wifi_setRadioSTBCEnable(INT radioIndex, BOOL STBC_Enable)
7411{
developera3511852023-06-14 14:12:59 +08007412 char config_file[64] = {'\0'};
7413 char cmd[512] = {'\0'};
7414 char buf[512] = {'\0'};
7415 char stbc_config[16] = {'\0'};
7416 wifi_band band;
7417 int iterator = 0;
7418 BOOL current_stbc = FALSE;
7419 int ant_count = 0;
7420 int ant_bitmap = 0;
7421 struct params list;
7422 char dat_file[64] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007423 int res;
developer72fb0bb2023-01-11 09:46:29 +08007424
developera3511852023-06-14 14:12:59 +08007425 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007426
developera3511852023-06-14 14:12:59 +08007427 band = wifi_index_to_band(radioIndex);
7428 if (band == band_invalid)
7429 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007430
developera3511852023-06-14 14:12:59 +08007431 if (band == band_2_4)
7432 iterator = 1;
7433 else if ((band == band_5) || (band == band_6))
7434 iterator = 2;
7435 else
7436 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007437
developera3511852023-06-14 14:12:59 +08007438 wifi_getRadioTxChainMask(radioIndex, &ant_bitmap);
7439 for (; ant_bitmap > 0; ant_bitmap >>= 1)
7440 ant_count += ant_bitmap & 1;
developer72fb0bb2023-01-11 09:46:29 +08007441
developera3511852023-06-14 14:12:59 +08007442 if (ant_count == 1 && STBC_Enable == TRUE) {
developer75bd10c2023-06-27 11:34:08 +08007443 wifi_debug(DEBUG_ERROR, "can not enable STBC when using only one antenna\n");
developera3511852023-06-14 14:12:59 +08007444 return RETURN_OK;
7445 }
developer72fb0bb2023-01-11 09:46:29 +08007446
developere40952c2023-06-15 18:46:43 +08007447 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7448 if (os_snprintf_error(sizeof(config_file), res)) {
7449 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7450 return RETURN_ERR;
7451 }
developer72fb0bb2023-01-11 09:46:29 +08007452
developera3511852023-06-14 14:12:59 +08007453 // set ht and vht config
7454 for (int i = 0; i < iterator; i++) {
7455 memset(stbc_config, 0, sizeof(stbc_config));
7456 memset(cmd, 0, sizeof(cmd));
7457 memset(buf, 0, sizeof(buf));
7458 list.name = (i == 0)?"ht_capab":"vht_capab";
developere40952c2023-06-15 18:46:43 +08007459 res = snprintf(stbc_config, sizeof(stbc_config), "%s", list.name);
7460 if (os_snprintf_error(sizeof(stbc_config), res)) {
7461 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7462 return RETURN_ERR;
7463 }
7464
7465 res = snprintf(cmd, sizeof(cmd), "cat %s | grep -E '^%s' | grep 'STBC'", config_file, stbc_config);
7466 if (os_snprintf_error(sizeof(cmd), res)) {
7467 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7468 return RETURN_ERR;
7469 }
7470
developera3511852023-06-14 14:12:59 +08007471 _syscmd(cmd, buf, sizeof(buf));
7472 if (strlen(buf) != 0)
7473 current_stbc = TRUE;
7474 if (current_stbc == STBC_Enable)
7475 continue;
developer72fb0bb2023-01-11 09:46:29 +08007476
developera3511852023-06-14 14:12:59 +08007477 if (STBC_Enable == TRUE) {
7478 // Append the STBC flags in capab config
7479 memset(cmd, 0, sizeof(cmd));
7480 if (i == 0)
developere40952c2023-06-15 18:46:43 +08007481 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC][RX-STBC1]/' %s", config_file);
developera3511852023-06-14 14:12:59 +08007482 else
developere40952c2023-06-15 18:46:43 +08007483 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1][RX-STBC-1]/' %s", config_file);
7484 if (os_snprintf_error(sizeof(cmd), res)) {
7485 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7486 return RETURN_ERR;
7487 }
7488
developera3511852023-06-14 14:12:59 +08007489 _syscmd(cmd, buf, sizeof(buf));
7490 } else if (STBC_Enable == FALSE) {
7491 // Remove the STBC flags and remain other flags in capab
7492 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08007493 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7494 if (os_snprintf_error(sizeof(cmd), res)) {
7495 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7496 return RETURN_ERR;
7497 }
7498
developera3511852023-06-14 14:12:59 +08007499 _syscmd(cmd, buf, sizeof(buf));
7500 memset(cmd, 0, sizeof(cmd));
developere40952c2023-06-15 18:46:43 +08007501 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[RX-STBC-?[1-3]*\\]//' %s", config_file);
7502 if (os_snprintf_error(sizeof(cmd), res)) {
7503 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7504 return RETURN_ERR;
7505 }
7506
developera3511852023-06-14 14:12:59 +08007507 _syscmd(cmd, buf, sizeof(buf));
7508 }
7509 wifi_hostapdRead(config_file, list.name, buf, sizeof(buf));
7510 list.value = buf;
7511 wifi_hostapdProcessUpdate(radioIndex, &list, 1);
7512 }
developere40952c2023-06-15 18:46:43 +08007513 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7514 if (os_snprintf_error(sizeof(dat_file), res)) {
7515 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7516 return RETURN_ERR;
7517 }
7518
7519 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_STBC=.*/HT_STBC=%d/g' %s", STBC_Enable, dat_file);
7520 if (os_snprintf_error(sizeof(cmd), res)) {
7521 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7522 return RETURN_ERR;
7523 }
7524
developera1255e42023-05-13 17:45:02 +08007525 _syscmd(cmd, buf, sizeof(buf));
7526 if ((band == band_5) || (band == band_6)) {
developere40952c2023-06-15 18:46:43 +08007527 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^VHT_STBC=.*/VHT_STBC=%d/g' %s", STBC_Enable, dat_file);
7528 if (os_snprintf_error(sizeof(cmd), res)) {
7529 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7530 return RETURN_ERR;
7531 }
7532
developera1255e42023-05-13 17:45:02 +08007533 _syscmd(cmd, buf, sizeof(buf));
7534 }
developera3511852023-06-14 14:12:59 +08007535 /*wifi_reloadAp(radioIndex);
developera1255e42023-05-13 17:45:02 +08007536 the caller do this.*/
developer72fb0bb2023-01-11 09:46:29 +08007537
developera3511852023-06-14 14:12:59 +08007538 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7539 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007540}
7541
7542// outputs A-MSDU enable status, 0 == not enabled, 1 == enabled
7543INT wifi_getRadioAMSDUEnable(INT radioIndex, BOOL *output_bool)
7544{
developera3511852023-06-14 14:12:59 +08007545 char dat_file[128] = {0};
developer2c22d832023-05-18 17:46:26 +08007546 wifi_band band;
7547 char amdus_buff[8] = {'\0'};
developere40952c2023-06-15 18:46:43 +08007548 int res;
developer72fb0bb2023-01-11 09:46:29 +08007549
developer2c22d832023-05-18 17:46:26 +08007550 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007551
developer2c22d832023-05-18 17:46:26 +08007552 band = wifi_index_to_band(radioIndex);
7553 if (band == band_invalid) {
7554 printf("%s:Band Error\n", __func__);
7555 return RETURN_ERR;
7556 }
developere40952c2023-06-15 18:46:43 +08007557 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7558 if (os_snprintf_error(sizeof(dat_file), res)) {
7559 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7560 return RETURN_ERR;
7561 }
7562
developer2c22d832023-05-18 17:46:26 +08007563 wifi_datfileRead(dat_file, "HT_AMSDU", amdus_buff, sizeof(amdus_buff));
7564 if (strncmp(amdus_buff, "1", 1) == 0)
developera3511852023-06-14 14:12:59 +08007565 *output_bool = TRUE;
developer2c22d832023-05-18 17:46:26 +08007566 else
7567 *output_bool = FALSE;
developer72fb0bb2023-01-11 09:46:29 +08007568
developer2c22d832023-05-18 17:46:26 +08007569 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7570
7571 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007572}
7573
7574// enables A-MSDU in the hardware, 0 == not enabled, 1 == enabled
7575INT wifi_setRadioAMSDUEnable(INT radioIndex, BOOL amsduEnable)
7576{
developer2c22d832023-05-18 17:46:26 +08007577 char dat_file[128] = {0};
7578 BOOL enable;
7579 wifi_band band;
7580 char amdus_buff[8] = {'\0'};
7581 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08007582 int res;
developer72fb0bb2023-01-11 09:46:29 +08007583
developer2c22d832023-05-18 17:46:26 +08007584 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007585
developer2c22d832023-05-18 17:46:26 +08007586 band = wifi_index_to_band(radioIndex);
7587 if (band == band_invalid) {
7588 printf("%s:Band Error\n", __func__);
7589 return RETURN_ERR;
7590 }
developere40952c2023-06-15 18:46:43 +08007591 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7592 if (os_snprintf_error(sizeof(dat_file), res)) {
7593 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7594 return RETURN_ERR;
7595 }
7596
developer2c22d832023-05-18 17:46:26 +08007597 wifi_datfileRead(dat_file, "HT_AMSDU", amdus_buff, sizeof(amdus_buff));
7598 if (strncmp(amdus_buff, "1", 1) == 0)
developera3511852023-06-14 14:12:59 +08007599 enable = TRUE;
developer2c22d832023-05-18 17:46:26 +08007600 else
7601 enable = FALSE;
7602 if (amsduEnable == enable)
developera3511852023-06-14 14:12:59 +08007603 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007604
developer2c22d832023-05-18 17:46:26 +08007605 params.name = "HT_AMSDU";
7606 if (amsduEnable)
7607 params.value = "1";
7608 else
7609 params.value = "0";
7610 wifi_datfileWrite(dat_file, &params, 1);
7611 wifi_reloadAp(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08007612
developer2c22d832023-05-18 17:46:26 +08007613 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007614
developera3511852023-06-14 14:12:59 +08007615 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007616}
7617
7618//P2 // outputs the number of Tx streams
7619INT wifi_getRadioTxChainMask(INT radioIndex, INT *output_int)
7620{
developera3511852023-06-14 14:12:59 +08007621 char buf[8] = {0};
7622 char cmd[128] = {0};
7623 int phyId = 0;
developere40952c2023-06-15 18:46:43 +08007624 int res;
developerc14d83a2023-06-29 20:09:42 +08007625 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007626
developera3511852023-06-14 14:12:59 +08007627 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007628
developera3511852023-06-14 14:12:59 +08007629 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +08007630 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'Configured Antennas' | awk '{print $4}'", phyId);
7631 if (os_snprintf_error(sizeof(cmd), res)) {
7632 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7633 return RETURN_ERR;
7634 }
7635
developera3511852023-06-14 14:12:59 +08007636 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007637
developerc14d83a2023-06-29 20:09:42 +08007638 if (hal_strtol(buf, 16, &tmp) < 0) {
7639 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007640 }
developerc14d83a2023-06-29 20:09:42 +08007641 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007642
developera3511852023-06-14 14:12:59 +08007643 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007644
developera3511852023-06-14 14:12:59 +08007645 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007646}
7647
7648INT fitChainMask(INT radioIndex, int antcount)
7649{
developera3511852023-06-14 14:12:59 +08007650 char buf[128] = {0};
7651 char cmd[128] = {0};
7652 char config_file[64] = {0};
7653 wifi_band band;
7654 struct params list[2] = {0};
developere40952c2023-06-15 18:46:43 +08007655 int res;
developer72fb0bb2023-01-11 09:46:29 +08007656
developera3511852023-06-14 14:12:59 +08007657 band = wifi_index_to_band(radioIndex);
7658 if (band == band_invalid)
7659 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007660
developera3511852023-06-14 14:12:59 +08007661 list[0].name = "he_mu_beamformer";
7662 list[1].name = "he_su_beamformer";
developer72fb0bb2023-01-11 09:46:29 +08007663
developere40952c2023-06-15 18:46:43 +08007664 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
7665 if (os_snprintf_error(sizeof(config_file), res)) {
7666 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7667 return RETURN_ERR;
7668 }
7669
developera3511852023-06-14 14:12:59 +08007670 if (antcount == 1) {
7671 // remove config about multiple antennas
developere40952c2023-06-15 18:46:43 +08007672 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[TX-STBC(-2BY1)?*\\]//' %s", config_file);
7673 if (os_snprintf_error(sizeof(cmd), res)) {
7674 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7675 return RETURN_ERR;
7676 }
7677
developera3511852023-06-14 14:12:59 +08007678 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007679
developere40952c2023-06-15 18:46:43 +08007680 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SOUNDING-DIMENSION-.\\]//' %s", config_file);
7681 if (os_snprintf_error(sizeof(cmd), res)) {
7682 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7683 return RETURN_ERR;
7684 }
7685
developera3511852023-06-14 14:12:59 +08007686 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007687
developere40952c2023-06-15 18:46:43 +08007688 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SU-BEAMFORMER\\]//' %s", config_file);
7689 if (os_snprintf_error(sizeof(cmd), res)) {
7690 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7691 return RETURN_ERR;
7692 }
7693
developera3511852023-06-14 14:12:59 +08007694 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007695
developere40952c2023-06-15 18:46:43 +08007696 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[MU-BEAMFORMER\\]//' %s", config_file);
7697 if (os_snprintf_error(sizeof(cmd), res)) {
7698 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7699 return RETURN_ERR;
7700 }
7701
developera3511852023-06-14 14:12:59 +08007702 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007703
developera3511852023-06-14 14:12:59 +08007704 list[0].value = "0";
7705 list[1].value = "0";
7706 } else {
7707 // 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.
7708 if (band == band_2_4 || band == band_5) {
developere40952c2023-06-15 18:46:43 +08007709 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '^ht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7710 if (os_snprintf_error(sizeof(cmd), res)) {
7711 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7712 return RETURN_ERR;
7713 }
7714
developera3511852023-06-14 14:12:59 +08007715 _syscmd(cmd, buf, sizeof(buf));
7716 if (strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +08007717 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[TX-STBC]/' %s", config_file);
7718 if (os_snprintf_error(sizeof(cmd), res)) {
7719 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7720 return RETURN_ERR;
7721 }
7722
developera3511852023-06-14 14:12:59 +08007723 _syscmd(cmd, buf, sizeof(buf));
7724 }
7725 }
7726 if (band == band_5) {
developere40952c2023-06-15 18:46:43 +08007727 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '^vht_capab=.*RX-STBC' | grep -v 'TX-STBC'", config_file);
7728 if (os_snprintf_error(sizeof(cmd), res)) {
7729 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7730 return RETURN_ERR;
7731 }
7732
developera3511852023-06-14 14:12:59 +08007733 _syscmd(cmd, buf, sizeof(buf));
7734 if (strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +08007735 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[TX-STBC-2BY1]/' %s", config_file);
7736 if (os_snprintf_error(sizeof(cmd), res)) {
7737 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7738 return RETURN_ERR;
7739 }
7740
developera3511852023-06-14 14:12:59 +08007741 _syscmd(cmd, buf, sizeof(buf));
7742 }
7743 }
developer72fb0bb2023-01-11 09:46:29 +08007744
developere40952c2023-06-15 18:46:43 +08007745 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[SU-BEAMFORMER\\]'", config_file);
7746 if (os_snprintf_error(sizeof(cmd), res)) {
7747 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7748 return RETURN_ERR;
7749 }
7750
developera3511852023-06-14 14:12:59 +08007751 _syscmd(cmd, buf, sizeof(buf));
7752 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007753 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SU-BEAMFORMER]/' %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));
7760 }
developer72fb0bb2023-01-11 09:46:29 +08007761
developere40952c2023-06-15 18:46:43 +08007762 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[MU-BEAMFORMER\\]'", config_file);
developerc14d83a2023-06-29 20:09:42 +08007763 if (os_snprintf_error(sizeof(cmd), res)) {
7764 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7765 return RETURN_ERR;
7766 }
developera3511852023-06-14 14:12:59 +08007767 _syscmd(cmd, buf, sizeof(buf));
7768 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007769 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[MU-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));
7776 }
developer72fb0bb2023-01-11 09:46:29 +08007777
developere40952c2023-06-15 18:46:43 +08007778 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '\\[SOUNDING-DIMENSION-.\\]'", config_file);
7779 if (os_snprintf_error(sizeof(cmd), res)) {
7780 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7781 return RETURN_ERR;
7782 }
7783
developera3511852023-06-14 14:12:59 +08007784 _syscmd(cmd, buf, sizeof(buf));
7785 if (strlen(buf) == 0) {
developere40952c2023-06-15 18:46:43 +08007786 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SOUNDING-DIMENSION-%d]/' %s", antcount, config_file);
developera3511852023-06-14 14:12:59 +08007787 } else {
developere40952c2023-06-15 18:46:43 +08007788 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/(SOUNDING-DIMENSION-)./\\1%d/' %s", antcount, config_file);
7789 }
7790 if (os_snprintf_error(sizeof(cmd), res)) {
7791 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7792 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +08007793 }
developere40952c2023-06-15 18:46:43 +08007794
developera3511852023-06-14 14:12:59 +08007795 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007796
developera3511852023-06-14 14:12:59 +08007797 list[0].value = "1";
7798 list[1].value = "1";
7799 }
7800 wifi_hostapdWrite(config_file, list, 2);
developerdaf24792023-06-06 11:40:04 +08007801 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007802}
7803
7804//P2 // sets the number of Tx streams to an enviornment variable
7805INT wifi_setRadioTxChainMask(INT radioIndex, INT numStreams)
7806{
developera3511852023-06-14 14:12:59 +08007807 char cmd[128] = {0};
7808 char buf[128] = {0};
7809 int phyId = 0;
7810 int cur_mask = 0;
7811 int antcountmsk = 0;
developera1255e42023-05-13 17:45:02 +08007812 INT cur_nss = 0;
developer863a4a62023-06-06 16:55:59 +08007813 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +08007814 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +08007815 int res;
developer72fb0bb2023-01-11 09:46:29 +08007816
developera3511852023-06-14 14:12:59 +08007817 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007818
developera3511852023-06-14 14:12:59 +08007819 if (numStreams <= 0) {
developer75bd10c2023-06-27 11:34:08 +08007820 wifi_debug(DEBUG_ERROR, "chainmask is not supported %d.\n", numStreams);
developera3511852023-06-14 14:12:59 +08007821 return RETURN_ERR;
7822 }
developer72fb0bb2023-01-11 09:46:29 +08007823
developera3511852023-06-14 14:12:59 +08007824 wifi_getRadioTxChainMask(radioIndex, &cur_mask);//this is mask value
developera1255e42023-05-13 17:45:02 +08007825 for(; cur_mask > 0; cur_mask >>= 1)//convert to number of streams.
7826 cur_nss += 1;
7827 WIFI_ENTRY_EXIT_DEBUG("%s:cur_nss=%d, new_nss=%d\n", __func__, cur_nss, numStreams);
developera3511852023-06-14 14:12:59 +08007828 if (cur_nss == numStreams)
7829 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007830
developera3511852023-06-14 14:12:59 +08007831 wifi_setRadioEnable(radioIndex, FALSE);
developer72fb0bb2023-01-11 09:46:29 +08007832
developera3511852023-06-14 14:12:59 +08007833 phyId = radio_index_to_phy(radioIndex);
developera1255e42023-05-13 17:45:02 +08007834 //iw need mask value.
7835 for (;numStreams > 0; numStreams--)
7836 antcountmsk |= 0x1 << (numStreams - 1);
developere40952c2023-06-15 18:46:43 +08007837 res = snprintf(cmd, sizeof(cmd), "iw phy%d set antenna 0x%x 2>&1", phyId, antcountmsk);
7838 if (os_snprintf_error(sizeof(cmd), res)) {
7839 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7840 return RETURN_ERR;
7841 }
developerb758dfd2023-06-21 17:32:07 +08007842
developera3511852023-06-14 14:12:59 +08007843 _syscmd(cmd, buf, sizeof(buf));
7844 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007845 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007846 return RETURN_ERR;
7847 }
7848 band = wifi_index_to_band(radioIndex);
developera1255e42023-05-13 17:45:02 +08007849 if (band == band_invalid) {
7850 printf("%s:Band Error\n", __func__);
7851 return RETURN_ERR;
7852 }
developere40952c2023-06-15 18:46:43 +08007853 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
7854 if (os_snprintf_error(sizeof(dat_file), res)) {
7855 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7856 return RETURN_ERR;
7857 }
developerb758dfd2023-06-21 17:32:07 +08007858
developere40952c2023-06-15 18:46:43 +08007859 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_TxStream=.*/HT_TxStream=%d/g' %s", numStreams, dat_file);
7860 if (os_snprintf_error(sizeof(cmd), res)) {
7861 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7862 return RETURN_ERR;
7863 }
developerb758dfd2023-06-21 17:32:07 +08007864
developera1255e42023-05-13 17:45:02 +08007865 _syscmd(cmd, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +08007866 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007867 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007868 return RETURN_ERR;
7869 }
developere40952c2023-06-15 18:46:43 +08007870 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/^HT_RxStream=.*/HT_RxStream=%d/g' %s", numStreams, dat_file);
7871 if (os_snprintf_error(sizeof(cmd), res)) {
7872 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7873 return RETURN_ERR;
7874 }
developerb758dfd2023-06-21 17:32:07 +08007875
developera1255e42023-05-13 17:45:02 +08007876 _syscmd(cmd, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +08007877 if (strlen(buf) > 0) {
developer75bd10c2023-06-27 11:34:08 +08007878 wifi_debug(DEBUG_ERROR, "cmd %s error, output: %s\n", cmd, buf);
developera3511852023-06-14 14:12:59 +08007879 return RETURN_ERR;
7880 }
7881 fitChainMask(radioIndex, numStreams);
7882 wifi_setRadioEnable(radioIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +08007883
developera3511852023-06-14 14:12:59 +08007884 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7885 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007886}
7887
7888//P2 // outputs the number of Rx streams
7889INT wifi_getRadioRxChainMask(INT radioIndex, INT *output_int)
7890{
developera3511852023-06-14 14:12:59 +08007891 char buf[8] = {0};
7892 char cmd[128] = {0};
7893 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +08007894 int res;
developerc14d83a2023-06-29 20:09:42 +08007895 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08007896
developera3511852023-06-14 14:12:59 +08007897 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007898
developera3511852023-06-14 14:12:59 +08007899 phyId = radio_index_to_phy(radioIndex);
developer75bd10c2023-06-27 11:34:08 +08007900
7901 res = snprintf(cmd, sizeof(cmd), "iw phy%d info | grep 'Configured Antennas' | awk '{print $6}'", phyId);
7902 if (os_snprintf_error(sizeof(cmd), res)) {
7903 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7904 return RETURN_ERR;
7905 }
developera3511852023-06-14 14:12:59 +08007906 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08007907
developerc14d83a2023-06-29 20:09:42 +08007908 if (hal_strtol(buf, 16, &tmp) < 0) {
7909 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08007910 }
developerc14d83a2023-06-29 20:09:42 +08007911 *output_int = tmp;
developer72fb0bb2023-01-11 09:46:29 +08007912
developera3511852023-06-14 14:12:59 +08007913 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08007914
developera3511852023-06-14 14:12:59 +08007915 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007916}
7917
7918//P2 // sets the number of Rx streams to an enviornment variable
7919INT wifi_setRadioRxChainMask(INT radioIndex, INT numStreams)
7920{
developera3511852023-06-14 14:12:59 +08007921 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
7922 if (wifi_setRadioTxChainMask(radioIndex, numStreams) == RETURN_ERR) {
developer75bd10c2023-06-27 11:34:08 +08007923 wifi_debug(DEBUG_ERROR, "wifi_setRadioTxChainMask return error.\n");
developera3511852023-06-14 14:12:59 +08007924 return RETURN_ERR;
7925 }
7926 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
7927 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08007928}
7929
7930//Get radio RDG enable setting
7931INT wifi_getRadioReverseDirectionGrantSupported(INT radioIndex, BOOL *output_bool)
7932{
developer47cc27a2023-05-17 23:09:58 +08007933 if (NULL == output_bool)
7934 return RETURN_ERR;
7935
7936 *output_bool = TRUE;
7937 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007938}
7939
7940//Get radio RDG enable setting
7941INT wifi_getRadioReverseDirectionGrantEnable(INT radioIndex, BOOL *output_bool)
7942{
developer47cc27a2023-05-17 23:09:58 +08007943 char rdg_status[2] = {0};
7944 char dat_file[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08007945 int res;
developer47cc27a2023-05-17 23:09:58 +08007946
7947 if (NULL == output_bool)
7948 return RETURN_ERR;
7949
7950 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08007951 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
7952 if (os_snprintf_error(sizeof(dat_file), res)) {
7953 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7954 return RETURN_ERR;
7955 }
developer47cc27a2023-05-17 23:09:58 +08007956
7957 wifi_datfileRead(dat_file, "HT_RDG", rdg_status, sizeof(rdg_status));
7958 if (!strncmp(rdg_status, "1", sizeof(rdg_status)))
7959 *output_bool = TRUE;
7960 else
7961 *output_bool = FALSE;
7962
7963 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007964}
7965
7966//Set radio RDG enable setting
7967INT wifi_setRadioReverseDirectionGrantEnable(INT radioIndex, BOOL enable)
7968{
developer47cc27a2023-05-17 23:09:58 +08007969 char dat_file[MAX_CMD_SIZE] = {0};
7970 struct params params = {0};
developere40952c2023-06-15 18:46:43 +08007971 int res;
developer47cc27a2023-05-17 23:09:58 +08007972
7973 /*prepare dat file path*/
developere40952c2023-06-15 18:46:43 +08007974 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, radioIndex);
7975 if (os_snprintf_error(sizeof(dat_file), res)) {
7976 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
7977 return RETURN_ERR;
7978 }
developer47cc27a2023-05-17 23:09:58 +08007979
7980 params.name = "HT_RDG";
7981
developera3511852023-06-14 14:12:59 +08007982 if (enable) {
7983 params.value = "1";
7984 } else {
7985 params.value = "0";
7986 }
developer47cc27a2023-05-17 23:09:58 +08007987
7988 wifi_datfileWrite(dat_file, &params, 1);
7989
7990 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08007991}
7992
developer5cd4c862023-05-26 09:34:42 +08007993
7994int mtk_get_ba_auto_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08007995{
developer5cd4c862023-05-26 09:34:42 +08007996 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7997 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
7998 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7999 unsigned char status;
8000 unsigned char *out_status = data;
8001 int err = 0;
developer8e6583c2023-05-23 13:36:06 +08008002
developer5cd4c862023-05-26 09:34:42 +08008003 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8004 genlmsg_attrlen(gnlh, 0), NULL);
8005 if (err < 0){
8006 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8007 return err;
8008 }
developer8e6583c2023-05-23 13:36:06 +08008009
developer5cd4c862023-05-26 09:34:42 +08008010 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8011 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
8012 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8013 if (err < 0){
8014 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8015 return err;
8016 }
developer8e6583c2023-05-23 13:36:06 +08008017
developer5cd4c862023-05-26 09:34:42 +08008018 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]) {
8019 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO]);
8020 if (status == 0) {
8021 wifi_debug(DEBUG_NOTICE, "disabled\n");
8022 } else {
8023 wifi_debug(DEBUG_NOTICE, "enabled\n");
8024 }
8025 *out_status = status;
8026 }
8027 }
developer8e6583c2023-05-23 13:36:06 +08008028
developer5cd4c862023-05-26 09:34:42 +08008029 return 0;
8030}
developer8e6583c2023-05-23 13:36:06 +08008031
developer5cd4c862023-05-26 09:34:42 +08008032int mtk_get_ba_decline_status_callback(struct nl_msg *msg, void *data)
8033{
8034 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8035 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_BA_ATTR_MAX + 1];
8036 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8037 unsigned char status;
8038 unsigned char *out_status = data;
8039 int err = 0;
8040
8041 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8042 genlmsg_attrlen(gnlh, 0), NULL);
8043 if (err < 0) {
8044 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8045 return err;
8046 }
8047
8048 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8049 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_BA_ATTR_MAX,
8050 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8051 if (err < 0) {
8052 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_AP_BA_ATTR_MAX fails\n");
8053 return err;
8054 }
8055
8056 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]) {
8057 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO]);
8058 if (status == 0) {
8059 wifi_debug(DEBUG_NOTICE, "disabled\n");
8060 } else {
8061 wifi_debug(DEBUG_NOTICE, "enabled\n");
8062 }
8063 *out_status = status;
8064 }
8065 }
8066
8067 return NL_OK;
developer72fb0bb2023-01-11 09:46:29 +08008068}
8069
developer5cd4c862023-05-26 09:34:42 +08008070INT mtk_wifi_get_ba_decl_auto_status(
8071 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back, BOOL *output_bool)
8072{
8073 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008074 unsigned int if_idx = 0;
8075 int ret = -1;
8076 struct unl unl_ins;
8077 struct nl_msg *msg = NULL;
8078 struct nlattr * msg_data = NULL;
8079 struct mtk_nl80211_param param;
8080
8081 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8082 return RETURN_ERR;
8083 if_idx = if_nametoindex(inf_name);
8084 if (!if_idx) {
8085 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8086 return RETURN_ERR;
8087 }
8088 /*init mtk nl80211 vendor cmd*/
8089 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8090 param.if_type = NL80211_ATTR_IFINDEX;
8091 param.if_idx = if_idx;
8092
8093 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8094 if (ret) {
8095 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8096 return RETURN_ERR;
8097 }
8098 /*add mtk vendor cmd data*/
8099 if (nla_put_u8(msg, vendor_data_attr, 0xf)) {
8100 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8101 nlmsg_free(msg);
8102 goto err;
8103 }
8104
8105 /*send mtk nl80211 vendor msg*/
8106 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8107 if (ret) {
8108 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8109 goto err;
8110 }
8111 /*deinit mtk nl80211 vendor msg*/
8112 mtk_nl80211_deint(&unl_ins);
8113 wifi_debug(DEBUG_NOTICE,"send cmd success, get output_bool:%d\n", *output_bool);
8114 return RETURN_OK;
8115err:
8116 mtk_nl80211_deint(&unl_ins);
8117 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8118 return RETURN_ERR;
8119}
developere0ff7232023-06-08 16:33:14 +08008120
8121INT mtk_wifi_set_auto_ba_en(
8122 INT apIndex, INT vendor_data_attr, BOOL enable)
8123{
8124 char inf_name[IF_NAME_SIZE] = {0};
8125 unsigned int if_idx = 0;
8126 int ret = -1;
8127 struct unl unl_ins;
8128 struct nl_msg *msg = NULL;
8129 struct nlattr * msg_data = NULL;
8130 struct mtk_nl80211_param param;
8131
8132 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8133 return RETURN_ERR;
8134 if_idx = if_nametoindex(inf_name);
8135 if (!if_idx) {
8136 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8137 return RETURN_ERR;
8138 }
8139 /*init mtk nl80211 vendor cmd*/
8140 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_BA;
8141 param.if_type = NL80211_ATTR_IFINDEX;
8142 param.if_idx = if_idx;
8143
8144 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8145 if (ret) {
8146 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8147 return RETURN_ERR;
8148 }
8149 /*add mtk vendor cmd data*/
8150 if (nla_put_u8(msg, vendor_data_attr, enable)) {
8151 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8152 nlmsg_free(msg);
8153 goto err;
8154 }
8155
8156 /*send mtk nl80211 vendor msg*/
8157 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8158 if (ret) {
8159 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8160 goto err;
8161 }
8162 /*deinit mtk nl80211 vendor msg*/
8163 mtk_nl80211_deint(&unl_ins);
8164 return RETURN_OK;
8165err:
8166 mtk_nl80211_deint(&unl_ins);
8167 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8168 return RETURN_ERR;
8169}
8170
developer5cd4c862023-05-26 09:34:42 +08008171//Get radio ADDBA enable setting
8172INT wifi_getRadioDeclineBARequestEnable(INT radioIndex, BOOL *output_bool)
8173{
8174 if (output_bool == NULL) {
8175 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8176 return RETURN_ERR;
8177 }
8178 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
8179 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO, mtk_get_ba_decline_status_callback, output_bool) != RETURN_OK) {
8180 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO(0x%x) fails\n",
8181 MTK_NL80211_VENDOR_ATTR_AP_BA_DECLINE_INFO);
8182 return RETURN_ERR;
8183 }
8184 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8185 return RETURN_OK;
8186}
8187
developer72fb0bb2023-01-11 09:46:29 +08008188//Set radio ADDBA enable setting
8189INT wifi_setRadioDeclineBARequestEnable(INT radioIndex, BOOL enable)
8190{
developera3511852023-06-14 14:12:59 +08008191 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008192}
8193
8194//Get radio auto block ack enable setting
8195INT wifi_getRadioAutoBlockAckEnable(INT radioIndex, BOOL *output_bool)
8196{
developer5cd4c862023-05-26 09:34:42 +08008197 if (output_bool == NULL) {
8198 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8199 return RETURN_ERR;
8200 }
developer8e6583c2023-05-23 13:36:06 +08008201
developera3511852023-06-14 14:12:59 +08008202 if (mtk_wifi_get_ba_decl_auto_status(radioIndex,
developer5cd4c862023-05-26 09:34:42 +08008203 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO,
8204 mtk_get_ba_auto_status_callback, output_bool) != RETURN_OK) {
8205 wifi_debug(DEBUG_ERROR, "cmd MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO(0x%x) fails\n",
8206 MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO);
8207 return RETURN_ERR;
8208 }
8209 wifi_debug(DEBUG_NOTICE, "cmd success:output_bool(%d)\n", *output_bool);
8210 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008211}
8212
8213//Set radio auto block ack enable setting
8214INT wifi_setRadioAutoBlockAckEnable(INT radioIndex, BOOL enable)
8215{
developera3511852023-06-14 14:12:59 +08008216 if (mtk_wifi_set_auto_ba_en
developere0ff7232023-06-08 16:33:14 +08008217 (radioIndex, MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO, enable) != RETURN_OK) {
8218 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_AP_BA_EN_INFO cmd fails\n");
8219 return RETURN_ERR;
8220 }
8221 wifi_debug(DEBUG_ERROR, "send cmd success: set auto ba enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008222 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008223}
8224
8225//Get radio 11n pure mode enable support
8226INT wifi_getRadio11nGreenfieldSupported(INT radioIndex, BOOL *output_bool)
8227{
developera3511852023-06-14 14:12:59 +08008228 if (NULL == output_bool)
8229 return RETURN_ERR;
8230 *output_bool = TRUE;
8231 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008232}
8233
8234//Get radio 11n pure mode enable setting
8235INT wifi_getRadio11nGreenfieldEnable(INT radioIndex, BOOL *output_bool)
8236{
developera3511852023-06-14 14:12:59 +08008237 if (NULL == output_bool)
8238 return RETURN_ERR;
8239 *output_bool = TRUE;
8240 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008241}
8242
8243//Set radio 11n pure mode enable setting
developer86035662023-06-28 19:21:12 +08008244INT wifi_setRadio11nGreenfieldEnable(INT radioIndex, BOOL enable)
developer72fb0bb2023-01-11 09:46:29 +08008245{
developer82533be2023-06-28 17:21:01 +08008246 char interface_name[16] = {0};
8247 int if_idx, ret = 0;
8248 struct nl_msg *msg = NULL;
8249 struct nlattr * msg_data = NULL;
8250 struct mtk_nl80211_param param;
8251 struct unl unl_ins;
8252
8253 if (radioIndex > MAX_APS) {
8254 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", radioIndex);
8255 return RETURN_ERR;
8256 }
8257 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8258 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
8259 return RETURN_ERR;
developerd14dff12023-06-28 22:47:44 +08008260
developer82533be2023-06-28 17:21:01 +08008261 if_idx = if_nametoindex(interface_name);
8262 /*init mtk nl80211 vendor cmd*/
8263 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_AP_BSS;
8264 param.if_type = NL80211_ATTR_IFINDEX;
8265 param.if_idx = if_idx;
8266 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8267 if (ret) {
8268 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8269 return RETURN_ERR;
8270 }
8271 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_AP_HT_OP_MODE, enable)) {
8272 printf("Nla put attribute error\n");
8273 nlmsg_free(msg);
8274 goto err;
8275 }
8276 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
8277 if (ret) {
8278 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
8279 goto err;
8280 }
8281 mtk_nl80211_deint(&unl_ins);
8282 //wifi_debug(DEBUG_NOTICE, "set Gf cmd success.\n");
8283 printf("set gf=%d cmd success.\n", enable);
8284 return RETURN_OK;
8285err:
8286 mtk_nl80211_deint(&unl_ins);
8287 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08008288 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008289}
8290
developer5cd4c862023-05-26 09:34:42 +08008291int mtk_get_igmp_status_callback(struct nl_msg *msg, void *data)
developer72fb0bb2023-01-11 09:46:29 +08008292{
developer5cd4c862023-05-26 09:34:42 +08008293 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8294 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX + 1];
8295 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8296 unsigned char status = 0, *out_status = data;
8297 int err = 0;
developer72fb0bb2023-01-11 09:46:29 +08008298
developer5cd4c862023-05-26 09:34:42 +08008299 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8300 genlmsg_attrlen(gnlh, 0), NULL);
8301 if (err < 0) {
8302 wifi_debug(DEBUG_ERROR, "get NL80211_ATTR_MAX fails\n");
8303 return err;
8304 }
developer72fb0bb2023-01-11 09:46:29 +08008305
developer5cd4c862023-05-26 09:34:42 +08008306 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8307 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX,
8308 tb[NL80211_ATTR_VENDOR_DATA], NULL);
8309 if (err < 0){
8310 wifi_debug(DEBUG_ERROR, "get MTK_NL80211_VENDOR_MCAST_SNOOP_ATTR_MAX fails\n");
8311 return err;
8312 }
developer72fb0bb2023-01-11 09:46:29 +08008313
developer5cd4c862023-05-26 09:34:42 +08008314 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]) {
8315 status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE]);
8316 if (status == 0) {
8317 wifi_debug(DEBUG_NOTICE, "disabled\n");
8318 } else {
8319 wifi_debug(DEBUG_NOTICE, "enabled\n");
8320 }
8321 *out_status = status;
8322 wifi_debug(DEBUG_NOTICE, "status: %d\n", *out_status);
8323 }
8324 }
8325
8326 return 0;
8327}
8328
8329INT mtk_wifi_set_igmp_en_status(
8330 INT apIndex, INT vendor_data_attr, mtk_nl80211_cb call_back,
8331 unsigned char in_en_stat, BOOL *output_bool)
8332{
8333 char inf_name[IF_NAME_SIZE] = {0};
developer5cd4c862023-05-26 09:34:42 +08008334 unsigned int if_idx = 0;
8335 int ret = -1;
8336 struct unl unl_ins;
8337 struct nl_msg *msg = NULL;
8338 struct nlattr * msg_data = NULL;
8339 struct mtk_nl80211_param param;
8340
8341 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
8342 return RETURN_ERR;
8343 if_idx = if_nametoindex(inf_name);
8344 if (!if_idx) {
8345 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
8346 return RETURN_ERR;
8347 }
8348 /*init mtk nl80211 vendor cmd*/
8349 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_MULTICAST_SNOOPING;
8350 param.if_type = NL80211_ATTR_IFINDEX;
8351 param.if_idx = if_idx;
8352
8353 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
8354 if (ret) {
8355 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
8356 return RETURN_ERR;
8357 }
8358 /*add mtk vendor cmd data*/
8359 if (nla_put_u8(msg, vendor_data_attr, in_en_stat)) {
8360 wifi_debug(DEBUG_ERROR, "Nla put vendor_data_attr(%d) attribute error\n", vendor_data_attr);
8361 nlmsg_free(msg);
8362 goto err;
8363 }
8364
8365 /*send mtk nl80211 vendor msg*/
8366 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, call_back, output_bool);
8367 if (ret) {
8368 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
8369 goto err;
8370 }
8371 /*deinit mtk nl80211 vendor msg*/
8372 mtk_nl80211_deint(&unl_ins);
8373 if (output_bool) {
8374 wifi_debug(DEBUG_NOTICE, "send cmd success, get output_bool:%d\n", *output_bool);
8375 } else {
8376 wifi_debug(DEBUG_NOTICE, "send cmd success.\n");
8377 }
8378 return RETURN_OK;
8379err:
8380 mtk_nl80211_deint(&unl_ins);
8381 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
8382 return RETURN_ERR;
8383}
8384
8385
8386//Get radio IGMP snooping enable setting
8387INT wifi_getRadioIGMPSnoopingEnable(INT radioIndex, BOOL *output_bool)
8388{
8389 if (output_bool == NULL) {
8390 wifi_debug(DEBUG_ERROR, "invalid: output_bool is null\n");
8391 return RETURN_ERR;
8392 }
developera3511852023-06-14 14:12:59 +08008393 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008394 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8395 mtk_get_igmp_status_callback, 0xf, output_bool)!= RETURN_OK) {
8396 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8397 return RETURN_ERR;
8398 }
8399 wifi_debug(DEBUG_ERROR, "send cmd success: get igmp status:(%d)\n", *output_bool);
developera3511852023-06-14 14:12:59 +08008400 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008401}
8402
8403//Set radio IGMP snooping enable setting
8404INT wifi_setRadioIGMPSnoopingEnable(INT radioIndex, BOOL enable)
8405{
developera3511852023-06-14 14:12:59 +08008406 if (mtk_wifi_set_igmp_en_status
developer5cd4c862023-05-26 09:34:42 +08008407 (radioIndex, MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE,
8408 NULL, enable, NULL) != RETURN_OK) {
8409 wifi_debug(DEBUG_ERROR, "send MTK_NL80211_VENDOR_ATTR_MCAST_SNOOP_ENABLE cmd fails\n");
8410 return RETURN_ERR;
8411 }
8412 wifi_debug(DEBUG_ERROR, "send cmd success: set igmp enable(%d)\n", enable);
developera3511852023-06-14 14:12:59 +08008413 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008414}
8415
8416//Get the Reset count of radio
developer69b61b02023-03-07 17:17:44 +08008417INT wifi_getRadioResetCount(INT radioIndex, ULONG *output_int)
developer72fb0bb2023-01-11 09:46:29 +08008418{
developera3511852023-06-14 14:12:59 +08008419 if (NULL == output_int)
8420 return RETURN_ERR;
8421 *output_int = get_radio_reset_cnt(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +08008422
developera3511852023-06-14 14:12:59 +08008423 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008424}
8425
8426
8427//---------------------------------------------------------------------------------------------------
8428//
8429// Additional Wifi AP level APIs used for Access Point devices
8430//
8431//---------------------------------------------------------------------------------------------------
8432
8433// creates a new ap and pushes these parameters to the hardware
8434INT wifi_createAp(INT apIndex, INT radioIndex, CHAR *essid, BOOL hideSsid)
8435{
developera3511852023-06-14 14:12:59 +08008436 // Deprecated when use hal version 3, use wifi_createVap() instead.
8437 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008438}
8439
8440// deletes this ap entry on the hardware, clears all internal variables associaated with this ap
8441INT wifi_deleteAp(INT apIndex)
8442{
developer7e4a2a62023-04-06 19:56:03 +08008443 char interface_name[16] = {0};
8444 char buf[MAX_BUF_SIZE];
8445 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08008446 int res;
developer72fb0bb2023-01-11 09:46:29 +08008447
developer7e4a2a62023-04-06 19:56:03 +08008448 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
8449 return RETURN_ERR;
developer8a3bbbf2023-03-15 17:47:23 +08008450
developere40952c2023-06-15 18:46:43 +08008451 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
8452 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
8453 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8454 return RETURN_ERR;
8455 }
developer7e4a2a62023-04-06 19:56:03 +08008456 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008457
developer7e4a2a62023-04-06 19:56:03 +08008458 wifi_removeApSecVaribles(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008459
developer7e4a2a62023-04-06 19:56:03 +08008460 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008461}
8462
8463// Outputs a 16 byte or less name assocated with the AP. String buffer must be pre-allocated by the caller
8464INT wifi_getApName(INT apIndex, CHAR *output_string)
8465{
developer7e4a2a62023-04-06 19:56:03 +08008466 char interface_name[IF_NAME_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +08008467 int radio_idx = 0;
8468 int bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008469 int res;
developer72fb0bb2023-01-11 09:46:29 +08008470
developer7e4a2a62023-04-06 19:56:03 +08008471 if(!output_string)
8472 return RETURN_ERR;
8473
8474 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
8475 vap_index_to_array_index(apIndex, &radio_idx, &bss_idx);
8476
developere40952c2023-06-15 18:46:43 +08008477 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 +08008478 } else
developere40952c2023-06-15 18:46:43 +08008479 res = snprintf(output_string, IF_NAME_SIZE, "%s", interface_name);
8480
8481 if (os_snprintf_error(IF_NAME_SIZE, res)) {
8482 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8483 return RETURN_ERR;
8484 }
developer7e4a2a62023-04-06 19:56:03 +08008485
8486 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008487}
8488
8489// Outputs the index number in that corresponds to the SSID string
8490INT wifi_getIndexFromName(CHAR *inputSsidString, INT *output_int)
8491{
developer7e4a2a62023-04-06 19:56:03 +08008492 char cmd [128] = {0};
8493 char buf[32] = {0};
8494 char ap_idx = 0;
8495 char *apIndex_str = NULL;
8496 char radio_idx = 0;
8497 char bss_idx = 0;
developere40952c2023-06-15 18:46:43 +08008498 int res;
developerc14d83a2023-06-29 20:09:42 +08008499 long int tmp;
developer72fb0bb2023-01-11 09:46:29 +08008500
developere40952c2023-06-15 18:46:43 +08008501 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 +08008502 inputSsidString);
developere40952c2023-06-15 18:46:43 +08008503
8504 if (os_snprintf_error(sizeof(cmd), res)) {
8505 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8506 return RETURN_ERR;
8507 }
8508
developer7e4a2a62023-04-06 19:56:03 +08008509 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008510
developer7e4a2a62023-04-06 19:56:03 +08008511 if (strlen(buf)) {
8512 apIndex_str = strtok(buf, "\n");
developerd14dff12023-06-28 22:47:44 +08008513 if (apIndex_str == NULL) {
8514 wifi_debug(DEBUG_ERROR, "strtok fail\n");
8515 return RETURN_ERR;
8516 }
developerc14d83a2023-06-29 20:09:42 +08008517 if (hal_strtol(apIndex_str, 10, &tmp) < 0) {
8518 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +08008519 }
developerc14d83a2023-06-29 20:09:42 +08008520 *output_int = tmp;
developer7e4a2a62023-04-06 19:56:03 +08008521 return RETURN_OK;
8522 }
developer72fb0bb2023-01-11 09:46:29 +08008523
developer7e4a2a62023-04-06 19:56:03 +08008524 /* If interface name is not in hostapd config, the caller maybe wifi agent to generate data model.*/
8525 if (strstr(inputSsidString, PREFIX_WIFI6G)) {
8526 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI6G));
8527 radio_idx = 2;
8528 } else if (strstr(inputSsidString, PREFIX_WIFI5G)) {
8529 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI5G));
8530 radio_idx = 1;
8531 } else if (strstr(inputSsidString, PREFIX_WIFI2G)) {
8532 bss_idx = atoi(inputSsidString + strlen(PREFIX_WIFI2G));
8533 radio_idx = 0;
8534 } else {
8535 printf("%s: hostapd conf not find, unknow inf(%s), return ERROR!!!(%d).\n",
8536 __func__, inputSsidString, ap_idx);
developera3511852023-06-14 14:12:59 +08008537 *output_int = -1;
8538 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08008539 }
8540
8541 ap_idx = array_index_to_vap_index(radio_idx, bss_idx);
8542
8543 if (ap_idx >= 0 && ap_idx < MAX_VAP) {
8544 printf("%s: hostapd conf not find, inf(%s), use inf idx(%d).\n",
8545 __func__, inputSsidString, ap_idx);
8546 *output_int = ap_idx;
8547 return RETURN_OK;
8548 }
8549
8550 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008551}
8552
8553INT wifi_getApIndexFromName(CHAR *inputSsidString, INT *output_int)
8554{
developera3511852023-06-14 14:12:59 +08008555 return wifi_getIndexFromName(inputSsidString, output_int);
developer72fb0bb2023-01-11 09:46:29 +08008556}
8557
8558// Outputs a 32 byte or less string indicating the beacon type as "None", "Basic", "WPA", "11i", "WPAand11i"
8559INT wifi_getApBeaconType(INT apIndex, CHAR *output_string)
8560{
developera3511852023-06-14 14:12:59 +08008561 char buf[MAX_BUF_SIZE] = {0};
8562 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08008563 int res;
developer72fb0bb2023-01-11 09:46:29 +08008564
developera3511852023-06-14 14:12:59 +08008565 if(NULL == output_string)
8566 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008567
developer32f2a182023-06-27 19:50:41 +08008568 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
8569 if (os_snprintf_error(sizeof(config_file), res)) {
8570 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8571 return RETURN_ERR;
8572 }
developera3511852023-06-14 14:12:59 +08008573 wifi_hostapdRead(config_file, "wpa", buf, sizeof(buf));
8574 if((strcmp(buf,"3")==0))
developere40952c2023-06-15 18:46:43 +08008575 res = snprintf(output_string, 32, "WPAand11i");
developera3511852023-06-14 14:12:59 +08008576 else if((strcmp(buf,"2")==0))
developere40952c2023-06-15 18:46:43 +08008577 res = snprintf(output_string, 32, "11i");
developera3511852023-06-14 14:12:59 +08008578 else if((strcmp(buf,"1")==0))
developere40952c2023-06-15 18:46:43 +08008579 res = snprintf(output_string, 32, "WPA");
developera3511852023-06-14 14:12:59 +08008580 else
developere40952c2023-06-15 18:46:43 +08008581 res = snprintf(output_string, 32, "None");
8582 if (os_snprintf_error(32, res)) {
8583 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8584 return RETURN_ERR;
8585 }
developer72fb0bb2023-01-11 09:46:29 +08008586
developera3511852023-06-14 14:12:59 +08008587 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008588}
8589
8590// Sets the beacon type enviornment variable. Allowed input strings are "None", "Basic", "WPA, "11i", "WPAand11i"
8591INT wifi_setApBeaconType(INT apIndex, CHAR *beaconTypeString)
8592{
developera3511852023-06-14 14:12:59 +08008593 char config_file[MAX_BUF_SIZE] = {0};
8594 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008595 int res;
developer72fb0bb2023-01-11 09:46:29 +08008596
developera3511852023-06-14 14:12:59 +08008597 if (NULL == beaconTypeString)
8598 return RETURN_ERR;
8599 list.name = "wpa";
8600 list.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008601
developera3511852023-06-14 14:12:59 +08008602 if((strcmp(beaconTypeString,"WPAand11i")==0))
8603 list.value="3";
8604 else if((strcmp(beaconTypeString,"11i")==0))
8605 list.value="2";
8606 else if((strcmp(beaconTypeString,"WPA")==0))
8607 list.value="1";
developer72fb0bb2023-01-11 09:46:29 +08008608
developer75bd10c2023-06-27 11:34:08 +08008609 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8610 if (os_snprintf_error(sizeof(config_file), res)) {
8611 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8612 return RETURN_ERR;
8613 }
developera3511852023-06-14 14:12:59 +08008614 wifi_hostapdWrite(config_file, &list, 1);
8615 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8616 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
8617 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008618}
8619
8620// sets the beacon interval on the hardware for this AP
8621INT wifi_setApBeaconInterval(INT apIndex, INT beaconInterval)
8622{
developera3511852023-06-14 14:12:59 +08008623 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8624 struct params params={'\0'};
8625 char buf[MAX_BUF_SIZE] = {'\0'};
8626 char config_file[MAX_BUF_SIZE] = {'\0'};
developere40952c2023-06-15 18:46:43 +08008627 int res;
developer72fb0bb2023-01-11 09:46:29 +08008628
developera3511852023-06-14 14:12:59 +08008629 params.name = "beacon_int";
developere40952c2023-06-15 18:46:43 +08008630 res = snprintf(buf, sizeof(buf), "%u", beaconInterval);
8631 if (os_snprintf_error(sizeof(buf), res)) {
8632 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8633 return RETURN_ERR;
8634 }
developera3511852023-06-14 14:12:59 +08008635 params.value = buf;
developer72fb0bb2023-01-11 09:46:29 +08008636
developer75bd10c2023-06-27 11:34:08 +08008637 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8638 if (os_snprintf_error(sizeof(config_file), res)) {
8639 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8640 return RETURN_ERR;
8641 }
developera3511852023-06-14 14:12:59 +08008642 wifi_hostapdWrite(config_file, &params, 1);
developer69b61b02023-03-07 17:17:44 +08008643
developera3511852023-06-14 14:12:59 +08008644 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8645 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8646 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008647}
8648
8649INT wifi_setDTIMInterval(INT apIndex, INT dtimInterval)
8650{
developera3511852023-06-14 14:12:59 +08008651 if (wifi_setApDTIMInterval(apIndex, dtimInterval) != RETURN_OK)
8652 return RETURN_ERR;
8653 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008654}
8655
8656// Get the packet size threshold supported.
8657INT wifi_getApRtsThresholdSupported(INT apIndex, BOOL *output_bool)
8658{
developera3511852023-06-14 14:12:59 +08008659 //save config and apply instantly
8660 if (NULL == output_bool)
8661 return RETURN_ERR;
8662 *output_bool = TRUE;
8663 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008664}
8665
8666// sets the packet size threshold in bytes to apply RTS/CTS backoff rules.
8667INT wifi_setApRtsThreshold(INT apIndex, UINT threshold)
8668{
developera3511852023-06-14 14:12:59 +08008669 char buf[16] = {0};
8670 char config_file[128] = {0};
8671 struct params param = {0};
developere40952c2023-06-15 18:46:43 +08008672 int res;
developer72fb0bb2023-01-11 09:46:29 +08008673
developera3511852023-06-14 14:12:59 +08008674 if (threshold > 65535) {
developer75bd10c2023-06-27 11:34:08 +08008675 wifi_debug(DEBUG_ERROR, "rts threshold %u is too big.\n", threshold);
developera3511852023-06-14 14:12:59 +08008676 return RETURN_ERR;
8677 }
developer72fb0bb2023-01-11 09:46:29 +08008678
developere40952c2023-06-15 18:46:43 +08008679 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8680 if (os_snprintf_error(sizeof(config_file), res)) {
8681 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8682 return RETURN_ERR;
8683 }
8684
8685 res = snprintf(buf, sizeof(buf), "%u", threshold);
8686 if (os_snprintf_error(sizeof(buf), res)) {
8687 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8688 return RETURN_ERR;
8689 }
developera3511852023-06-14 14:12:59 +08008690 param.name = "rts_threshold";
8691 param.value = buf;
8692 wifi_hostapdWrite(config_file, &param, 1);
8693 wifi_hostapdProcessUpdate(apIndex, &param, 1);
8694 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +08008695
developera3511852023-06-14 14:12:59 +08008696 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008697}
8698
8699// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8700INT wifi_getApWpaEncryptoinMode(INT apIndex, CHAR *output_string)
8701{
developere40952c2023-06-15 18:46:43 +08008702 int res;
8703
developera3511852023-06-14 14:12:59 +08008704 if (NULL == output_string)
8705 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08008706 res = snprintf(output_string, 32, "TKIPandAESEncryption");
8707 if (os_snprintf_error(32, res)) {
8708 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8709 return RETURN_ERR;
8710 }
developera3511852023-06-14 14:12:59 +08008711 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008712
8713}
8714
8715// outputs up to a 32 byte string as either "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8716INT wifi_getApWpaEncryptionMode(INT apIndex, CHAR *output_string)
8717{
developera3511852023-06-14 14:12:59 +08008718 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8719 char *param_name = NULL;
8720 char buf[32] = {0}, config_file[MAX_BUF_SIZE] = {0};
developerc79e9172023-06-06 19:48:03 +08008721 unsigned int len;
developere40952c2023-06-15 18:46:43 +08008722 int res;
developer72fb0bb2023-01-11 09:46:29 +08008723
developera3511852023-06-14 14:12:59 +08008724 if(NULL == output_string)
8725 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008726
developer75bd10c2023-06-27 11:34:08 +08008727 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8728 if (os_snprintf_error(sizeof(config_file), res)) {
8729 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8730 return RETURN_ERR;
8731 }
8732
developera3511852023-06-14 14:12:59 +08008733 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08008734
developera3511852023-06-14 14:12:59 +08008735 if(strcmp(buf,"0")==0)
8736 {
8737 printf("%s: wpa_mode is %s ......... \n", __func__, buf);
developere40952c2023-06-15 18:46:43 +08008738 res = snprintf(output_string, 32, "None");
8739 if (os_snprintf_error(32, res)) {
8740 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8741 return RETURN_ERR;
8742 }
developera3511852023-06-14 14:12:59 +08008743 return RETURN_OK;
8744 }
8745 else if((strcmp(buf,"3")==0) || (strcmp(buf,"2")==0))
8746 param_name = "rsn_pairwise";
8747 else if((strcmp(buf,"1")==0))
8748 param_name = "wpa_pairwise";
8749 else
8750 return RETURN_ERR;
8751 memset(output_string,'\0',32);
8752 wifi_hostapdRead(config_file,param_name,output_string,32);
8753 if (strlen(output_string) == 0) { // rsn_pairwise is optional. When it is empty use wpa_pairwise instead.
8754 param_name = "wpa_pairwise";
8755 memset(output_string, '\0', 32);
8756 wifi_hostapdRead(config_file, param_name, output_string, 32);
8757 }
8758 wifi_dbg_printf("\n%s output_string=%s",__func__,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008759
developera3511852023-06-14 14:12:59 +08008760 if(strcmp(output_string,"TKIP CCMP") == 0) {
developerc79e9172023-06-06 19:48:03 +08008761 len = strlen("TKIPandAESEncryption");
8762 memcpy(output_string,"TKIPandAESEncryption", len);
8763 output_string[len] = '\0';
8764 } else if(strcmp(output_string,"TKIP") == 0) {
8765 len = strlen("TKIPEncryption");
8766 memcpy(output_string,"TKIPEncryption", len);
8767 output_string[len] = '\0';
8768 } else if(strcmp(output_string,"CCMP") == 0) {
8769 len = strlen("AESEncryption");
8770 memcpy(output_string,"AESEncryption", len);
8771 output_string[len] = '\0';
8772 }
developer72fb0bb2023-01-11 09:46:29 +08008773
developera3511852023-06-14 14:12:59 +08008774 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8775 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008776}
8777
8778// sets the encyption mode enviornment variable. Valid string format is "TKIPEncryption", "AESEncryption", or "TKIPandAESEncryption"
8779INT wifi_setApWpaEncryptionMode(INT apIndex, CHAR *encMode)
8780{
developera3511852023-06-14 14:12:59 +08008781 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8782 struct params params={'\0'};
8783 char output_string[32];
8784 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08008785 int res;
developer72fb0bb2023-01-11 09:46:29 +08008786
developera3511852023-06-14 14:12:59 +08008787 memset(output_string,'\0',32);
8788 wifi_getApBeaconType(apIndex,output_string);
developer72fb0bb2023-01-11 09:46:29 +08008789
developera3511852023-06-14 14:12:59 +08008790 if(strcmp(encMode, "TKIPEncryption") == 0)
8791 params.value = "TKIP";
8792 else if(strcmp(encMode,"AESEncryption") == 0)
8793 params.value = "CCMP";
8794 else if(strcmp(encMode,"TKIPandAESEncryption") == 0)
8795 params.value = "TKIP CCMP";
developer72fb0bb2023-01-11 09:46:29 +08008796
developera3511852023-06-14 14:12:59 +08008797 if((strcmp(output_string,"WPAand11i")==0))
8798 {
8799 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008800 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8801 if (os_snprintf_error(sizeof(config_file), res)) {
8802 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8803 return RETURN_ERR;
8804 }
developera3511852023-06-14 14:12:59 +08008805 wifi_hostapdWrite(config_file, &params, 1);
8806 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008807
developera3511852023-06-14 14:12:59 +08008808 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008809 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8810 if (os_snprintf_error(sizeof(config_file), res)) {
8811 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8812 return RETURN_ERR;
8813 }
developera3511852023-06-14 14:12:59 +08008814 wifi_hostapdWrite(config_file, &params, 1);
8815 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +08008816
developera3511852023-06-14 14:12:59 +08008817 return RETURN_OK;
8818 }
8819 else if((strcmp(output_string,"11i")==0))
8820 {
8821 params.name = "rsn_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008822 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8823 if (os_snprintf_error(sizeof(config_file), res)) {
8824 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8825 return RETURN_ERR;
8826 }
developera3511852023-06-14 14:12:59 +08008827 wifi_hostapdWrite(config_file, &params, 1);
8828 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8829 return RETURN_OK;
8830 }
8831 else if((strcmp(output_string,"WPA")==0))
8832 {
8833 params.name = "wpa_pairwise";
developer75bd10c2023-06-27 11:34:08 +08008834 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8835 if (os_snprintf_error(sizeof(config_file), res)) {
8836 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8837 return RETURN_ERR;
8838 }
developera3511852023-06-14 14:12:59 +08008839 wifi_hostapdWrite(config_file, &params, 1);
8840 wifi_hostapdProcessUpdate(apIndex, &params, 1);
8841 return RETURN_OK;
8842 }
developer72fb0bb2023-01-11 09:46:29 +08008843
developera3511852023-06-14 14:12:59 +08008844 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
8845 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008846}
8847
8848// deletes internal security varable settings for this ap
8849INT wifi_removeApSecVaribles(INT apIndex)
8850{
developer0155a502023-06-19 20:33:57 +08008851 char config_file[MAX_BUF_SIZE] = {0};
8852 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008853 int res;
developer0155a502023-06-19 20:33:57 +08008854
8855 list.name = "wpa";
8856 list.value = "0";
8857
developer75bd10c2023-06-27 11:34:08 +08008858 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8859 if (os_snprintf_error(sizeof(config_file), res)) {
8860 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8861 return RETURN_ERR;
8862 }
developer0155a502023-06-19 20:33:57 +08008863 wifi_hostapdWrite(config_file, &list, 1);
8864
8865 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008866}
8867
8868// changes the hardware settings to disable encryption on this ap
8869INT wifi_disableApEncryption(INT apIndex)
8870{
developer0155a502023-06-19 20:33:57 +08008871 char config_file[MAX_BUF_SIZE] = {0};
8872 struct params list;
developer75bd10c2023-06-27 11:34:08 +08008873 int res;
developer0155a502023-06-19 20:33:57 +08008874
8875 list.name = "wpa";
8876 list.value = "0";
8877
developer75bd10c2023-06-27 11:34:08 +08008878 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8879 if (os_snprintf_error(sizeof(config_file), res)) {
8880 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8881 return RETURN_ERR;
8882 }
developer0155a502023-06-19 20:33:57 +08008883 wifi_hostapdWrite(config_file, &list, 1);
8884 wifi_hostapdProcessUpdate(apIndex, &list, 1);
8885 wifi_reloadAp(apIndex);
8886
8887 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008888}
8889
8890// set the authorization mode on this ap
8891// mode mapping as: 1: open, 2: shared, 4:auto
8892INT wifi_setApAuthMode(INT apIndex, INT mode)
8893{
developera3511852023-06-14 14:12:59 +08008894 struct params params={0};
8895 char config_file[64] = {0};
developer75bd10c2023-06-27 11:34:08 +08008896 int res;
developer72fb0bb2023-01-11 09:46:29 +08008897
developera3511852023-06-14 14:12:59 +08008898 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008899
developera3511852023-06-14 14:12:59 +08008900 wifi_dbg_printf("\n%s algo_mode=%d", __func__, mode);
8901 params.name = "auth_algs";
developer72fb0bb2023-01-11 09:46:29 +08008902
developere5750452023-05-15 16:46:42 +08008903 if ((mode & 1 && mode & 2) || mode & 4)
developera3511852023-06-14 14:12:59 +08008904 params.value = "3";
8905 else if (mode & 2)
8906 params.value = "2";
8907 else if (mode & 1)
8908 params.value = "1";
8909 else
8910 params.value = "0";
developer72fb0bb2023-01-11 09:46:29 +08008911
developer75bd10c2023-06-27 11:34:08 +08008912 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
8913 if (os_snprintf_error(sizeof(config_file), res)) {
8914 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8915 return RETURN_ERR;
8916 }
developera3511852023-06-14 14:12:59 +08008917 wifi_hostapdWrite(config_file, &params, 1);
8918 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +08008919 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +08008920 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008921
developera3511852023-06-14 14:12:59 +08008922 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08008923}
8924
8925// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
8926INT wifi_setApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
8927{
developera3511852023-06-14 14:12:59 +08008928 //save to wifi config, and wait for wifi restart to apply
8929 struct params params={'\0'};
8930 char config_file[MAX_BUF_SIZE] = {0};
8931 int ret;
developer72fb0bb2023-01-11 09:46:29 +08008932
developera3511852023-06-14 14:12:59 +08008933 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
8934 if(authMode == NULL)
8935 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08008936
developera3511852023-06-14 14:12:59 +08008937 wifi_dbg_printf("\n%s AuthMode=%s",__func__,authMode);
8938 params.name = "wpa_key_mgmt";
developer72fb0bb2023-01-11 09:46:29 +08008939
developera3511852023-06-14 14:12:59 +08008940 if((strcmp(authMode,"PSKAuthentication") == 0) || (strcmp(authMode,"SharedAuthentication") == 0))
8941 params.value = "WPA-PSK";
8942 else if(strcmp(authMode,"EAPAuthentication") == 0)
8943 params.value = "WPA-EAP";
8944 else if (strcmp(authMode, "SAEAuthentication") == 0)
8945 params.value = "SAE";
8946 else if (strcmp(authMode, "EAP_192-bit_Authentication") == 0)
8947 params.value = "WPA-EAP-SUITE-B-192";
8948 else if (strcmp(authMode, "PSK-SAEAuthentication") == 0)
8949 params.value = "WPA-PSK WPA-PSK-SHA256 SAE";
8950 else if (strcmp(authMode, "Enhanced_Open") == 0)
8951 params.value = "OWE";
8952 else if(strcmp(authMode,"None") == 0) //Donot change in case the authMode is None
8953 return RETURN_OK; //This is taken careof in beaconType
developer72fb0bb2023-01-11 09:46:29 +08008954
developer32f2a182023-06-27 19:50:41 +08008955 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
8956 if (os_snprintf_error(sizeof(config_file), ret)) {
8957 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8958 return RETURN_ERR;
8959 }
developera3511852023-06-14 14:12:59 +08008960 ret=wifi_hostapdWrite(config_file,&params,1);
8961 if(!ret)
8962 ret=wifi_hostapdProcessUpdate(apIndex, &params, 1);
8963 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +08008964
developera3511852023-06-14 14:12:59 +08008965 return ret;
developer72fb0bb2023-01-11 09:46:29 +08008966}
8967
8968// sets an enviornment variable for the authMode. Valid strings are "None", "EAPAuthentication" or "SharedAuthentication"
8969INT wifi_getApBasicAuthenticationMode(INT apIndex, CHAR *authMode)
8970{
developera3511852023-06-14 14:12:59 +08008971 //save to wifi config, and wait for wifi restart to apply
8972 char BeaconType[50] = {0};
8973 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +08008974 int res;
developer32f2a182023-06-27 19:50:41 +08008975 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +08008976
developera3511852023-06-14 14:12:59 +08008977 *authMode = 0;
8978 wifi_getApBeaconType(apIndex,BeaconType);
8979 printf("%s____%s \n",__FUNCTION__,BeaconType);
developer72fb0bb2023-01-11 09:46:29 +08008980
developer32f2a182023-06-27 19:50:41 +08008981 if(strcmp(BeaconType,"None") == 0) {
8982 memcpy(authMode, "None", 4);
8983 authMode[4] = '\0';
8984 } else {
8985 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
developer75bd10c2023-06-27 11:34:08 +08008986 if (os_snprintf_error(sizeof(config_file), res)) {
8987 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
8988 return RETURN_ERR;
8989 }
developera3511852023-06-14 14:12:59 +08008990 wifi_hostapdRead(config_file, "wpa_key_mgmt", authMode, 32);
8991 wifi_dbg_printf("\n[%s]: AuthMode Name is : %s",__func__,authMode);
developer32f2a182023-06-27 19:50:41 +08008992 if(strcmp(authMode,"WPA-PSK") == 0) {
8993 len = strlen("SharedAuthentication");
8994 memcpy(authMode, "SharedAuthentication", len);
8995 authMode[len] = '\0';
8996 } else if(strcmp(authMode,"WPA-EAP") == 0) {
8997 len = strlen("EAPAuthentication");
8998 memcpy(authMode, "EAPAuthentication", len);
8999 authMode[len] = '\0';
9000 }
developera3511852023-06-14 14:12:59 +08009001 }
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// Outputs the number of stations associated per AP
9007INT wifi_getApNumDevicesAssociated(INT apIndex, ULONG *output_ulong)
9008{
developera3511852023-06-14 14:12:59 +08009009 char interface_name[16] = {0};
9010 char cmd[128]={0};
9011 char buf[128]={0};
9012 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +08009013 int res;
developer72fb0bb2023-01-11 09:46:29 +08009014
developera3511852023-06-14 14:12:59 +08009015 if(apIndex > MAX_APS)
9016 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009017
developera3511852023-06-14 14:12:59 +08009018 wifi_getApEnable(apIndex,&status);
9019 if (!status)
9020 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009021
developera3511852023-06-14 14:12:59 +08009022 //sprintf(cmd, "iw dev %s station dump | grep Station | wc -l", interface_name);//alternate method
9023 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9024 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +08009025 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta | wc -l", interface_name);
9026 if (os_snprintf_error(sizeof(cmd), res)) {
9027 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9028 return RETURN_ERR;
9029 }
developera3511852023-06-14 14:12:59 +08009030 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +08009031 if (sscanf(buf,"%lu", output_ulong) != 1) {
9032 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
9033 return RETURN_ERR;
9034 }
developer72fb0bb2023-01-11 09:46:29 +08009035
developera3511852023-06-14 14:12:59 +08009036 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009037}
9038
9039// manually removes any active wi-fi association with the device specified on this ap
9040INT wifi_kickApAssociatedDevice(INT apIndex, CHAR *client_mac)
9041{
developera3511852023-06-14 14:12:59 +08009042 char inf_name[16] = {0};
9043 char cmd[MAX_CMD_SIZE] = {0};
9044 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +08009045 int res;
developer72fb0bb2023-01-11 09:46:29 +08009046
developera3511852023-06-14 14:12:59 +08009047 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9048 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +08009049
9050 res = snprintf(cmd, sizeof(cmd),"hostapd_cli -i %s disassociate %s", inf_name, client_mac);
9051 if (os_snprintf_error(sizeof(cmd), res)) {
9052 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9053 return RETURN_ERR;
9054 }
developer7e4a2a62023-04-06 19:56:03 +08009055
developera3511852023-06-14 14:12:59 +08009056 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +08009057
developera3511852023-06-14 14:12:59 +08009058 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009059}
9060
9061// outputs the radio index for the specified ap. similar as wifi_getSsidRadioIndex
9062INT wifi_getApRadioIndex(INT apIndex, INT *output_int)
9063{
developer7e4a2a62023-04-06 19:56:03 +08009064 int max_radio_num = 0;
9065
9066 if(NULL == output_int)
9067 return RETURN_ERR;
9068
9069 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +08009070 if(max_radio_num == 0){
9071 return RETURN_ERR;
9072 }
developer7e4a2a62023-04-06 19:56:03 +08009073 *output_int = apIndex % max_radio_num;
9074
9075 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009076}
9077
9078// sets the radio index for the specific ap
9079INT wifi_setApRadioIndex(INT apIndex, INT radioIndex)
9080{
developera3511852023-06-14 14:12:59 +08009081 //set to config only and wait for wifi reset to apply settings
9082 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009083}
9084
developer0155a502023-06-19 20:33:57 +08009085int mtk_get_ap_metrics(struct nl_msg *msg, void *cb)
9086{
9087 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9088 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009089 struct genlmsghdr *gnlh;
developer0155a502023-06-19 20:33:57 +08009090 wdev_ap_metric ap_metric;
9091 wdev_ap_metric *p_ap_metric = &ap_metric;
9092 int err = 0;
9093 struct mtk_nl80211_cb_data *cb_data = cb;
9094
9095 if (!msg || !cb_data) {
developerc14d83a2023-06-29 20:09:42 +08009096 wifi_debug(DEBUG_ERROR, "msgor cb_data is null,error.\n");
developer0155a502023-06-19 20:33:57 +08009097 return NL_SKIP;
9098 }
developerc14d83a2023-06-29 20:09:42 +08009099 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer0155a502023-06-19 20:33:57 +08009100
9101 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9102 genlmsg_attrlen(gnlh, 0), NULL);
9103 if (err < 0) {
9104 wifi_debug(DEBUG_ERROR, "nla_parse ap_metrics nl80211 msg fails,error.\n");
9105 return err;
9106 }
9107
9108 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9109 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_ATTR_GET_STATISTIC_MAX,
9110 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9111 if (err < 0) {
9112 wifi_debug(DEBUG_ERROR, "GET_STATISTIC_MAX fails,error.\n");
9113 return err;
9114 }
9115
9116 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]) {
9117 p_ap_metric = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS]);
9118 if (p_ap_metric) {
9119 memcpy(cb_data->out_buf , &p_ap_metric->cu, sizeof(unsigned char));
9120 }
9121 }
9122 }
9123
9124 return NL_OK;
9125}
9126
developer121a8e72023-05-22 09:19:39 +08009127
9128#define MAX_ACL_DUMP_LEN 4096
9129int mtk_acl_list_dump_callback(struct nl_msg *msg, void *cb)
9130{
9131 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9132 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX + 1];
developerc14d83a2023-06-29 20:09:42 +08009133 struct genlmsghdr *gnlh;
developer121a8e72023-05-22 09:19:39 +08009134 char *show_str = NULL;
developer2edaf012023-05-24 14:24:53 +08009135 int err = 0;
developer121a8e72023-05-22 09:19:39 +08009136 unsigned short acl_result_len = 0;
9137 struct mtk_nl80211_cb_data *cb_data = cb;
developer121a8e72023-05-22 09:19:39 +08009138 if (!msg || !cb_data) {
developerdaf24792023-06-06 11:40:04 +08009139 wifi_debug(DEBUG_ERROR, "msg(%p) or cb_data(%p) is null,error.\n", msg, cb_data);
developer121a8e72023-05-22 09:19:39 +08009140 return NL_SKIP;
9141 }
developerc14d83a2023-06-29 20:09:42 +08009142
9143 gnlh = nlmsg_data(nlmsg_hdr(msg));
developer121a8e72023-05-22 09:19:39 +08009144 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9145 genlmsg_attrlen(gnlh, 0), NULL);
9146 if (err < 0) {
developer2edaf012023-05-24 14:24:53 +08009147 wifi_debug(DEBUG_ERROR, "nla_parse acl list nl80211 msg fails,error.\n");
developer121a8e72023-05-22 09:19:39 +08009148 return NL_SKIP;
9149 }
developer121a8e72023-05-22 09:19:39 +08009150 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9151 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_AP_ACL_ATTR_MAX,
9152 tb[NL80211_ATTR_VENDOR_DATA], NULL);
9153 if (err < 0)
9154 return NL_SKIP;
9155 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]) {
9156 acl_result_len = nla_len(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9157 show_str = nla_data(vndr_tb[MTK_NL80211_VENDOR_ATTR_ACL_LIST_INFO]);
9158 if (acl_result_len > MAX_ACL_DUMP_LEN) {
9159 wifi_debug(DEBUG_ERROR,"the scan result len is invalid !!!\n");
9160 return NL_SKIP;
9161 } else if (*(show_str + acl_result_len - 1) != '\0') {
9162 wifi_debug(DEBUG_INFO, "the result string is not ended with right terminator, handle it!!!\n");
9163 *(show_str + acl_result_len - 1) = '\0';
9164 }
9165 wifi_debug(DEBUG_INFO, "driver msg:%s\n", show_str);
developer2edaf012023-05-24 14:24:53 +08009166
9167 if (cb_data->out_len >= acl_result_len) {
9168 memset(cb_data->out_buf, 0, cb_data->out_len);
9169 /*skip the first line: 'policy=1\n' to find the acl mac addrs*/
9170 memmove(cb_data->out_buf, show_str, acl_result_len);
9171 wifi_debug(DEBUG_INFO, "out buff:%s\n", cb_data->out_buf);
9172 } else {
9173 memset(cb_data->out_buf, 0, cb_data->out_len);
developer121a8e72023-05-22 09:19:39 +08009174 }
developer121a8e72023-05-22 09:19:39 +08009175 } else
9176 wifi_debug(DEBUG_ERROR, "no acl result attr\n");
9177 } else
9178 wifi_debug(DEBUG_ERROR, "no any acl result from driver\n");
9179 return NL_OK;
9180}
developer72fb0bb2023-01-11 09:46:29 +08009181// Get the ACL MAC list per AP
developer2edaf012023-05-24 14:24:53 +08009182INT mtk_wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
developer72fb0bb2023-01-11 09:46:29 +08009183{
developer7e4a2a62023-04-06 19:56:03 +08009184 char inf_name[IF_NAME_SIZE] = {0};
developer121a8e72023-05-22 09:19:39 +08009185 unsigned int if_idx = 0;
9186 int ret = -1;
9187 struct unl unl_ins;
9188 struct nl_msg *msg = NULL;
9189 struct nlattr * msg_data = NULL;
9190 struct mtk_nl80211_param param;
9191 struct mtk_nl80211_cb_data cb_data;
developer7e4a2a62023-04-06 19:56:03 +08009192 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9193 return RETURN_ERR;
developer121a8e72023-05-22 09:19:39 +08009194 if_idx = if_nametoindex(inf_name);
9195 if (!if_idx) {
developer2edaf012023-05-24 14:24:53 +08009196 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
developer121a8e72023-05-22 09:19:39 +08009197 return RETURN_ERR;
9198 }
9199 /*init mtk nl80211 vendor cmd*/
9200 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9201 param.if_type = NL80211_ATTR_IFINDEX;
9202 param.if_idx = if_idx;
9203
9204 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9205 if (ret) {
9206 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9207 return RETURN_ERR;
9208 }
developer121a8e72023-05-22 09:19:39 +08009209 /*add mtk vendor cmd data*/
9210 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_SHOW_ALL)) {
developer2edaf012023-05-24 14:24:53 +08009211 wifi_debug(DEBUG_ERROR, "Nla put ACL_SHOW_ALL attribute error\n");
developer121a8e72023-05-22 09:19:39 +08009212 nlmsg_free(msg);
9213 goto err;
9214 }
developer72fb0bb2023-01-11 09:46:29 +08009215
developer121a8e72023-05-22 09:19:39 +08009216 /*send mtk nl80211 vendor msg*/
9217 cb_data.out_buf = macArray;
9218 cb_data.out_len = buf_size;
9219
9220 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_acl_list_dump_callback, &cb_data);
9221 if (ret) {
9222 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9223 goto err;
9224 }
9225 /*deinit mtk nl80211 vendor msg*/
9226 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009227 wifi_debug(DEBUG_NOTICE,"send cmd success, get out_buf:%s\n", macArray);
developera3511852023-06-14 14:12:59 +08009228 return RETURN_OK;
developer121a8e72023-05-22 09:19:39 +08009229err:
9230 mtk_nl80211_deint(&unl_ins);
developer2edaf012023-05-24 14:24:53 +08009231 wifi_debug(DEBUG_ERROR,"send cmd fails\n");
developer121a8e72023-05-22 09:19:39 +08009232 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009233}
9234
developer2edaf012023-05-24 14:24:53 +08009235INT wifi_getApAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9236{
9237 char *mac_arry_buf = NULL;
9238
9239 mac_arry_buf = malloc(buf_size);
9240 if (!mac_arry_buf) {
9241 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
9242 return RETURN_ERR;
9243 }
9244 memset(mac_arry_buf, 0, buf_size);
9245 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
9246 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
9247 free(mac_arry_buf);
9248 mac_arry_buf = NULL;
9249 return RETURN_ERR;
9250 }
9251 /*
9252 mtk format to wifi hal format:
9253 "policy=1
9254 00:11:22:33:44:55
9255 00:11:22:33:44:66
9256 "
9257 -->
9258 "00:11:22:33:44:55
9259 00:11:22:33:44:66
9260 "
9261 */
9262 memset(macArray, 0, buf_size);
9263 if (*mac_arry_buf != '\0' && strchr(mac_arry_buf,'\n')) {
9264 memmove(macArray, strchr(mac_arry_buf,'\n')+1, strlen(strchr(mac_arry_buf,'\n')+1)+1);
9265 wifi_debug(DEBUG_NOTICE,"macArray:\n%s\n", macArray);
9266 }
9267 free(mac_arry_buf);
9268 mac_arry_buf = NULL;
9269 return RETURN_OK;
9270}
9271
developer72fb0bb2023-01-11 09:46:29 +08009272INT wifi_getApDenyAclDevices(INT apIndex, CHAR *macArray, UINT buf_size)
9273{
developer72fb0bb2023-01-11 09:46:29 +08009274
developer7e4a2a62023-04-06 19:56:03 +08009275 wifi_getApAclDevices(apIndex, macArray, buf_size);
developer72fb0bb2023-01-11 09:46:29 +08009276
developera3511852023-06-14 14:12:59 +08009277 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009278}
9279
9280
9281// Get the list of stations associated per AP
9282INT wifi_getApDevicesAssociated(INT apIndex, CHAR *macArray, UINT buf_size)
9283{
developer7e4a2a62023-04-06 19:56:03 +08009284 char interface_name[IF_NAME_SIZE] = {0};
9285 char cmd[MAX_CMD_SIZE];
developere40952c2023-06-15 18:46:43 +08009286 int res;
developer72fb0bb2023-01-11 09:46:29 +08009287
developer7e4a2a62023-04-06 19:56:03 +08009288 if(apIndex > 3) //Currently supporting apIndex upto 3
developera3511852023-06-14 14:12:59 +08009289 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009290
developer7e4a2a62023-04-06 19:56:03 +08009291 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developera3511852023-06-14 14:12:59 +08009292 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009293
developere40952c2023-06-15 18:46:43 +08009294 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta", interface_name);
9295 if (os_snprintf_error(sizeof(cmd), res)) {
9296 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
9297 return RETURN_ERR;
9298 }
developer7e4a2a62023-04-06 19:56:03 +08009299 _syscmd(cmd, macArray, buf_size);
9300 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009301}
9302
developer8dd72532023-05-17 19:58:35 +08009303int hex2num(char c)
9304{
9305 if (c >= '0' && c <= '9')
9306 return c - '0';
9307 if (c >= 'a' && c <= 'f')
9308 return c - 'a' + 10;
9309 if (c >= 'A' && c <= 'F')
9310 return c - 'A' + 10;
9311 return -1;
9312}
9313
9314/**
9315 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
9316 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
9317 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
9318 * Returns: Characters used (> 0) on success, -1 on failure
9319 */
9320int hwaddr_aton2(const char *txt, unsigned char *addr)
9321{
9322 int i;
9323 const char *pos = txt;
9324
9325 for (i = 0; i < 6; i++) {
9326 int a, b;
9327
9328 while (*pos == ':' || *pos == '.' || *pos == '-')
9329 pos++;
9330
9331 a = hex2num(*pos++);
9332 if (a < 0)
9333 return -1;
9334 b = hex2num(*pos++);
9335 if (b < 0)
9336 return -1;
9337 *addr++ = (a << 4) | b;
9338 }
9339
9340 return pos - txt;
9341}
9342
developer72fb0bb2023-01-11 09:46:29 +08009343// adds the mac address to the filter list
9344//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9345INT wifi_addApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9346{
developer7e4a2a62023-04-06 19:56:03 +08009347 char inf_name[IF_NAME_SIZE] = {0};
developer8dd72532023-05-17 19:58:35 +08009348 int if_idx, ret = 0;
developer49b17232023-05-19 16:35:19 +08009349 struct nl_msg *msg = NULL;
9350 struct nlattr * msg_data = NULL;
9351 struct mtk_nl80211_param param;
developer8dd72532023-05-17 19:58:35 +08009352 unsigned char mac[ETH_ALEN] = {0x00, 0x0c, 0x43, 0x11, 0x22, 0x33};
9353 struct unl unl_ins;
developer7e4a2a62023-04-06 19:56:03 +08009354 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9355 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +08009356 if (!DeviceMacAddress)
9357 return RETURN_ERR;
developer8dd72532023-05-17 19:58:35 +08009358 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
developer2edaf012023-05-24 14:24:53 +08009359 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
developer8dd72532023-05-17 19:58:35 +08009360 return RETURN_ERR;
9361 }
developer8dd72532023-05-17 19:58:35 +08009362 if_idx = if_nametoindex(inf_name);
developer2edaf012023-05-24 14:24:53 +08009363 if (!if_idx) {
9364 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9365 return RETURN_ERR;
9366 }
developer49b17232023-05-19 16:35:19 +08009367 /*init mtk nl80211 vendor cmd*/
9368 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9369 param.if_type = NL80211_ATTR_IFINDEX;
9370 param.if_idx = if_idx;
9371 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9372 if (ret) {
9373 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009374 return RETURN_ERR;
9375 }
developer49b17232023-05-19 16:35:19 +08009376 /*add mtk vendor cmd data*/
9377 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_ADD_MAC, ETH_ALEN, mac)) {
developer2edaf012023-05-24 14:24:53 +08009378 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
developer8dd72532023-05-17 19:58:35 +08009379 nlmsg_free(msg);
9380 goto err;
9381 }
developer49b17232023-05-19 16:35:19 +08009382 /*send mtk nl80211 vendor msg*/
9383 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9384 if (ret) {
9385 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
developer8dd72532023-05-17 19:58:35 +08009386 goto err;
9387 }
developer49b17232023-05-19 16:35:19 +08009388 /*deinit mtk nl80211 vendor msg*/
9389 mtk_nl80211_deint(&unl_ins);
9390 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer8dd72532023-05-17 19:58:35 +08009391 return RETURN_OK;
9392err:
developer49b17232023-05-19 16:35:19 +08009393 mtk_nl80211_deint(&unl_ins);
9394 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developer8dd72532023-05-17 19:58:35 +08009395 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009396}
9397
9398// deletes the mac address from the filter list
9399//DeviceMacAddress is in XX:XX:XX:XX:XX:XX format
9400INT wifi_delApAclDevice(INT apIndex, CHAR *DeviceMacAddress)
9401{
developer2edaf012023-05-24 14:24:53 +08009402 struct unl unl_ins;
9403 int if_idx = 0, ret = 0;
9404 struct nl_msg *msg = NULL;
9405 struct nlattr * msg_data = NULL;
9406 struct mtk_nl80211_param param;
developer7e4a2a62023-04-06 19:56:03 +08009407 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +08009408 unsigned char mac[ETH_ALEN] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009409
developer7e4a2a62023-04-06 19:56:03 +08009410 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9411 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009412
developer7e4a2a62023-04-06 19:56:03 +08009413 if (!DeviceMacAddress)
9414 return RETURN_ERR;
9415
developer2edaf012023-05-24 14:24:53 +08009416 if (hwaddr_aton2(DeviceMacAddress, mac) < 0) {
9417 wifi_debug(DEBUG_ERROR, "error device mac address=%s\n", DeviceMacAddress);
9418 return RETURN_ERR;
9419 }
developer72fb0bb2023-01-11 09:46:29 +08009420
developer2edaf012023-05-24 14:24:53 +08009421 if_idx = if_nametoindex(inf_name);
9422 if (!if_idx) {
9423 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9424 return RETURN_ERR;
9425 }
9426 /*init mtk nl80211 vendor cmd*/
9427 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9428 param.if_type = NL80211_ATTR_IFINDEX;
9429 param.if_idx = if_idx;
9430 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9431 if (ret) {
9432 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9433 return RETURN_ERR;
9434 }
9435 /*add mtk vendor cmd data*/
9436 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_ACL_DEL_MAC, ETH_ALEN, mac)) {
9437 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9438 nlmsg_free(msg);
9439 goto err;
9440 }
9441 /*send mtk nl80211 vendor msg*/
9442 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9443 if (ret) {
9444 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9445 goto err;
9446 }
9447 /*deinit mtk nl80211 vendor msg*/
9448 mtk_nl80211_deint(&unl_ins);
9449 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
9450 return RETURN_OK;
9451err:
9452 mtk_nl80211_deint(&unl_ins);
9453 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9454 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009455}
9456
9457// outputs the number of devices in the filter list
9458INT wifi_getApAclDeviceNum(INT apIndex, UINT *output_uint)
9459{
developer2edaf012023-05-24 14:24:53 +08009460 char *mac_arry = NULL, *ptr = NULL, mac_str[18] = {0};
9461 UINT buf_size = 1024;
9462 UINT sta_num = 0;
9463 unsigned char mac[ETH_ALEN] = {0};
developera3511852023-06-14 14:12:59 +08009464 if(output_uint == NULL)
developerdaf24792023-06-06 11:40:04 +08009465 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009466
developer2edaf012023-05-24 14:24:53 +08009467 mac_arry = (char *)malloc(buf_size);
9468 if (mac_arry == NULL) {
9469 wifi_debug(DEBUG_ERROR, "malloc mac_arry fails\n");
developer7e4a2a62023-04-06 19:56:03 +08009470 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009471 }
developerdaf24792023-06-06 11:40:04 +08009472 memset(mac_arry, 0, buf_size);
developer2edaf012023-05-24 14:24:53 +08009473 /*mac_arry str format: 00:11:22:33:44:55\n00:11:22:33:44:66\0*/
9474 if (wifi_getApAclDevices(apIndex, mac_arry, buf_size)!= RETURN_OK) {
9475 wifi_debug(DEBUG_ERROR, "get acl list entries fails\n");
developer9ce44382023-06-28 11:09:37 +08009476 free(mac_arry);
developer2edaf012023-05-24 14:24:53 +08009477 return RETURN_ERR;
9478 }
9479 /*count the acl str nums:*/
9480 wifi_debug(DEBUG_NOTICE, "mac_arry: %s\n", mac_arry);
developer7e4a2a62023-04-06 19:56:03 +08009481
developer2edaf012023-05-24 14:24:53 +08009482 /*mac addr string format:
9483 exp1: 00:11:22:33:44:55\0
9484 exp2: 00:11:22:33:44:55\n00:11:22:33:44:66\0
9485 */
9486 ptr = mac_arry;
9487 while (sscanf(ptr, "%17s", mac_str) == 1) {
9488 if (hwaddr_aton2(mac_str, mac) >= 0)
9489 sta_num++;
9490 ptr = strstr(ptr, mac_str) + strlen(mac_str);
9491 }
9492 *output_uint = sta_num;
9493 wifi_debug(DEBUG_NOTICE, "output_uint: %d\n", *output_uint);
9494 free(mac_arry);
9495 mac_arry = NULL;
developer7e4a2a62023-04-06 19:56:03 +08009496 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009497}
9498
9499INT apply_rules(INT apIndex, CHAR *client_mac,CHAR *action,CHAR *interface)
9500{
developer75bd10c2023-06-27 11:34:08 +08009501 int res;
developer72fb0bb2023-01-11 09:46:29 +08009502
developera3511852023-06-14 14:12:59 +08009503 if(strcmp(action,"DENY")==0)
9504 {
developer33f13ba2023-07-12 16:19:06 +08009505 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 +08009506 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009507 if (res) {
9508 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009509 return RETURN_ERR;
9510 }
developera3511852023-06-14 14:12:59 +08009511 return RETURN_OK;
9512 }
developer72fb0bb2023-01-11 09:46:29 +08009513
developera3511852023-06-14 14:12:59 +08009514 if(strcmp(action,"ALLOW")==0)
9515 {
developer33f13ba2023-07-12 16:19:06 +08009516 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 +08009517 apIndex, interface, client_mac);
developer33f13ba2023-07-12 16:19:06 +08009518 if (res) {
9519 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +08009520 return RETURN_ERR;
9521 }
developera3511852023-06-14 14:12:59 +08009522 return RETURN_OK;
9523 }
developer72fb0bb2023-01-11 09:46:29 +08009524
developera3511852023-06-14 14:12:59 +08009525 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009526
9527}
9528
9529// enable kick for devices on acl black list
9530INT wifi_kickApAclAssociatedDevices(INT apIndex, BOOL enable)
9531{
developera3511852023-06-14 14:12:59 +08009532 char aclArray[MAX_BUF_SIZE] = {0}, *acl = NULL;
9533 char assocArray[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009534
developera3511852023-06-14 14:12:59 +08009535 wifi_getApDenyAclDevices(apIndex, aclArray, sizeof(aclArray));
9536 wifi_getApDevicesAssociated(apIndex, assocArray, sizeof(assocArray));
developer72fb0bb2023-01-11 09:46:29 +08009537
developera3511852023-06-14 14:12:59 +08009538 /* if there are no devices connected there is nothing to do */
9539 if (strlen(assocArray) < 17)
9540 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009541
developera3511852023-06-14 14:12:59 +08009542 if (enable == TRUE) {
9543 /* kick off the MAC which is in ACL array (deny list) */
9544 acl = strtok(aclArray, "\n");
9545 while (acl != NULL) {
9546 if (strlen(acl) >= 17 && strcasestr(assocArray, acl))
9547 wifi_kickApAssociatedDevice(apIndex, acl);
developer72fb0bb2023-01-11 09:46:29 +08009548
developera3511852023-06-14 14:12:59 +08009549 acl = strtok(NULL, "\n");
9550 }
developer72fb0bb2023-01-11 09:46:29 +08009551 wifi_setApMacAddressControlMode(apIndex, 2);
developera3511852023-06-14 14:12:59 +08009552 } else
developer72fb0bb2023-01-11 09:46:29 +08009553 wifi_setApMacAddressControlMode(apIndex, 0);
developer72fb0bb2023-01-11 09:46:29 +08009554
developera3511852023-06-14 14:12:59 +08009555 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009556}
9557
9558INT wifi_setPreferPrivateConnection(BOOL enable)
9559{
developera3511852023-06-14 14:12:59 +08009560 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009561}
9562
9563// sets the mac address filter control mode. 0 == filter disabled, 1 == filter as whitelist, 2 == filter as blacklist
9564INT wifi_setApMacAddressControlMode(INT apIndex, INT filterMode)
9565{
developer2edaf012023-05-24 14:24:53 +08009566 int if_idx = 0, ret = 0;
9567 struct unl unl_ins;
9568 struct nl_msg *msg = NULL;
9569 struct nlattr * msg_data = NULL;
9570 struct mtk_nl80211_param param;
9571 int acl_policy = -1;
developer7e4a2a62023-04-06 19:56:03 +08009572 char inf_name[IF_NAME_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +08009573
developer7e4a2a62023-04-06 19:56:03 +08009574 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
9575 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +08009576 if_idx = if_nametoindex(inf_name);
9577 if (!if_idx) {
9578 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
9579 return RETURN_ERR;
9580 }
9581 /*init mtk nl80211 vendor cmd*/
9582 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
9583 param.if_type = NL80211_ATTR_IFINDEX;
9584 param.if_idx = if_idx;
9585 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9586 if (ret) {
9587 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9588 return RETURN_ERR;
9589 }
9590 /*add mtk vendor cmd data*/
9591 if (filterMode == 0) {
9592 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_DISABLE;
9593 } else if (filterMode == 1) {
9594 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_WHITE_LIST;
9595 } else if (filterMode == 2) {
9596 acl_policy = MTK_NL80211_VENDOR_ATTR_ACL_ENABLE_BLACK_LIST;
9597 } else {
9598 wifi_debug(DEBUG_ERROR, "filtermode(%d) not support error\n", filterMode);
9599 nlmsg_free(msg);
9600 goto err;
9601 }
9602 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, acl_policy)) {
9603 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
9604 nlmsg_free(msg);
9605 goto err;
9606 }
9607 /*send mtk nl80211 vendor msg*/
9608 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9609 if (ret) {
9610 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
9611 goto err;
9612 }
9613 /*deinit mtk nl80211 vendor msg*/
9614 mtk_nl80211_deint(&unl_ins);
9615 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
developer7e4a2a62023-04-06 19:56:03 +08009616 return RETURN_OK;
developer2edaf012023-05-24 14:24:53 +08009617err:
9618 mtk_nl80211_deint(&unl_ins);
9619 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
9620 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009621}
9622
9623// 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.
9624INT wifi_setApVlanEnable(INT apIndex, BOOL VlanEnabled)
9625{
developera3511852023-06-14 14:12:59 +08009626 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009627}
9628
9629// gets the vlan ID for this ap from an internal enviornment variable
9630INT wifi_getApVlanID(INT apIndex, INT *output_int)
9631{
developera3511852023-06-14 14:12:59 +08009632 if(apIndex==0)
9633 {
9634 *output_int=100;
9635 return RETURN_OK;
9636 }
developer72fb0bb2023-01-11 09:46:29 +08009637
developera3511852023-06-14 14:12:59 +08009638 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009639}
9640
9641// sets the vlan ID for this ap to an internal enviornment variable
9642INT wifi_setApVlanID(INT apIndex, INT vlanId)
9643{
developera3511852023-06-14 14:12:59 +08009644 //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 +08009645 char interface_name[16] = {0};
9646 int if_idx, ret = 0;
9647 struct nl_msg *msg = NULL;
9648 struct nlattr * msg_data = NULL;
9649 struct mtk_nl80211_param param;
9650 struct unl unl_ins;
9651
9652 if (apIndex > MAX_APS) {
9653 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9654 return RETURN_ERR;
9655 }
9656 if (vlanId > 4095 || vlanId < 1) {
9657 wifi_debug(DEBUG_ERROR, "Invalid vlanId %d\n", vlanId);
9658 return RETURN_ERR;
9659 }
9660 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9661 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9662 return RETURN_ERR;
9663 /*step 1. mwctl dev %s set vlan_tag 0*/
9664 if_idx = if_nametoindex(interface_name);
9665 /*init mtk nl80211 vendor cmd*/
9666 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9667 param.if_type = NL80211_ATTR_IFINDEX;
9668 param.if_idx = if_idx;
9669 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9670 if (ret) {
9671 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9672 return RETURN_ERR;
9673 }
9674 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, vlanId)) {
9675 printf("Nla put attribute error\n");
9676 nlmsg_free(msg);
9677 goto err;
9678 }
9679 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9680 if (ret) {
9681 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9682 goto err;
9683 }
9684 mtk_nl80211_deint(&unl_ins);
9685 //wifi_debug(DEBUG_NOTICE, "set vlanId cmd success.\n", vlanId);
9686 printf("set vlanId=%d cmd success.\n", vlanId);
9687 return RETURN_OK;
9688err:
9689 mtk_nl80211_deint(&unl_ins);
9690 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
developera3511852023-06-14 14:12:59 +08009691 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009692}
9693
developercc5cbfb2023-06-13 18:29:52 +08009694char br_name[IFNAMSIZ] = "brlan0";
9695
developer72fb0bb2023-01-11 09:46:29 +08009696// gets bridgeName, IP address and Subnet. bridgeName is a maximum of 32 characters,
9697INT wifi_getApBridgeInfo(INT index, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9698{
developercc5cbfb2023-06-13 18:29:52 +08009699 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9700 struct ifreq ifr;
9701 struct sockaddr_in *sin;
9702
9703 memcpy(bridgeName, br_name, strlen(br_name));
9704
9705 if (sock == -1) {
9706 wifi_debug(DEBUG_ERROR, "socket failed");
9707 return RETURN_ERR;
9708 }
9709
developerd14dff12023-06-28 22:47:44 +08009710 strncpy(ifr.ifr_name, br_name, strlen(br_name));
developercc5cbfb2023-06-13 18:29:52 +08009711 ifr.ifr_addr.sa_family = AF_INET;
9712 if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
9713 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFADDR) failed, %s, bridge_name=%s\n",
9714 strerror(errno), br_name);
developer9ce44382023-06-28 11:09:37 +08009715 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009716 return RETURN_ERR;
9717 }
9718
9719 sin = (struct sockaddr_in *)&ifr.ifr_addr;
9720 wifi_debug(DEBUG_ERROR, "Bridge device %s has IP address: %s\n", br_name, inet_ntoa(sin->sin_addr));
9721 memcpy(IP, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9722
9723 if (ioctl(sock, SIOCGIFNETMASK, &ifr) < 0) {
9724 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFNETMASK) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009725 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009726 return RETURN_ERR;
9727 }
9728
9729 wifi_debug(DEBUG_ERROR, "Bridge device %s has subnet mask: %s\n", br_name, inet_ntoa(sin->sin_addr));
9730 memcpy(subnet, inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
9731 close(sock);
developer72fb0bb2023-01-11 09:46:29 +08009732
developera3511852023-06-14 14:12:59 +08009733 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +08009734}
9735
9736//sets bridgeName, IP address and Subnet to internal enviornment variables. bridgeName is a maximum of 32 characters
9737INT wifi_setApBridgeInfo(INT apIndex, CHAR *bridgeName, CHAR *IP, CHAR *subnet)
9738{
developera3511852023-06-14 14:12:59 +08009739 //save settings, wait for wifi reset or wifi_pushBridgeInfo to apply.
developercc5cbfb2023-06-13 18:29:52 +08009740 struct ifreq ifr;
9741 struct sockaddr_in sin;
9742 int sock = socket(AF_INET, SOCK_DGRAM, 0);
9743
developerc14d83a2023-06-29 20:09:42 +08009744 if(sock < 0) {
9745 wifi_debug(DEBUG_ERROR, "sock init fail\n");
9746 return RETURN_ERR;
9747 }
9748
developercc5cbfb2023-06-13 18:29:52 +08009749 if (strlen(bridgeName) >= IFNAMSIZ) {
9750 wifi_debug(DEBUG_ERROR, "invalide bridgeName length=%ld\n", strlen(bridgeName));
developer9ce44382023-06-28 11:09:37 +08009751 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009752 return RETURN_ERR;
9753 }
9754
9755 if (strlen(br_name) >= IFNAMSIZ) {
9756 wifi_debug(DEBUG_ERROR, "invalide br_name length=%ld in strorage\n", strlen(br_name));
developer9ce44382023-06-28 11:09:37 +08009757 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009758 return RETURN_ERR;
9759 }
9760
9761 if (sock == -1) {
developera3511852023-06-14 14:12:59 +08009762 wifi_debug(DEBUG_ERROR, "socket failed");
developercc5cbfb2023-06-13 18:29:52 +08009763 return RETURN_ERR;
9764 }
9765
9766 memset(&ifr, 0, sizeof(ifr));
9767 strncpy(ifr.ifr_name, br_name, strlen(br_name));
9768 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9769 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009770 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009771 return RETURN_ERR;
9772 }
9773
9774 ifr.ifr_flags = (short)(ifr.ifr_flags & ~IFF_UP);
9775 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9776 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009777 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009778 return RETURN_ERR;
9779 }
9780
9781 memset(&ifr, 0, sizeof(ifr));
9782 strncpy(ifr.ifr_name, br_name, IFNAMSIZ);
developerd14dff12023-06-28 22:47:44 +08009783 strncpy(ifr.ifr_newname, bridgeName, strlen(bridgeName));
developercc5cbfb2023-06-13 18:29:52 +08009784 if (ioctl(sock, SIOCSIFNAME, &ifr) < 0) {
9785 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNAME) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009786 close(sock);
developera3511852023-06-14 14:12:59 +08009787 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009788 }
9789
9790 memset(br_name, 0, sizeof(br_name));
9791 memcpy(br_name, bridgeName, strlen(bridgeName));
9792
9793 memset(&ifr, 0, sizeof(ifr));
9794 strncpy(ifr.ifr_name, bridgeName, IFNAMSIZ);
9795 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9796 wifi_debug(DEBUG_ERROR, "ioctl(SIOCGIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009797 close(sock);
developera3511852023-06-14 14:12:59 +08009798 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009799 }
9800 ifr.ifr_flags = (short)(ifr.ifr_flags | IFF_UP);
9801 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
9802 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFFLAGS) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009803 close(sock);
developera3511852023-06-14 14:12:59 +08009804 return RETURN_ERR;
developercc5cbfb2023-06-13 18:29:52 +08009805 }
9806
9807 memset(&ifr, 0, sizeof(ifr));
9808 memcpy(ifr.ifr_name, bridgeName, strlen(bridgeName));
9809
9810 memset(&sin, 0, sizeof(struct sockaddr_in));
9811 sin.sin_family = AF_INET;
9812 if (inet_aton(IP, &(sin.sin_addr)) == 0) {
9813 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developer9ce44382023-06-28 11:09:37 +08009814 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009815 return RETURN_ERR;
9816 }
9817 memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr_in));
9818 if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
9819 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFADDR) failed, %s", strerror(errno));
developer9ce44382023-06-28 11:09:37 +08009820 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009821 return RETURN_ERR;
9822 }
9823
9824 if (inet_aton(subnet, &((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr) == 0) {
9825 wifi_debug(DEBUG_ERROR, "inet_aton failed");
developerc14d83a2023-06-29 20:09:42 +08009826 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009827 return RETURN_ERR;
9828 }
9829 if (ioctl(sock, SIOCSIFNETMASK, &ifr) < -1) {
9830 wifi_debug(DEBUG_ERROR, "ioctl(SIOCSIFNETMASK) failed, %s", strerror(errno));
developerc14d83a2023-06-29 20:09:42 +08009831 close(sock);
developercc5cbfb2023-06-13 18:29:52 +08009832 return RETURN_ERR;
9833 }
9834
9835 close(sock);
developera3511852023-06-14 14:12:59 +08009836 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009837}
9838
9839// reset the vlan configuration for this ap
9840INT wifi_resetApVlanCfg(INT apIndex)
9841{
developera1255e42023-05-13 17:45:02 +08009842 char interface_name[16] = {0};
developer2202b332023-05-24 16:23:22 +08009843 int if_idx, ret = 0;
9844 struct nl_msg *msg = NULL;
9845 struct nlattr * msg_data = NULL;
9846 struct mtk_nl80211_param param;
9847 struct unl unl_ins;
9848 struct vlan_policy_param vlan_param;
developer72fb0bb2023-01-11 09:46:29 +08009849
developer2202b332023-05-24 16:23:22 +08009850 if (apIndex > MAX_APS) {
9851 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
9852 return RETURN_ERR;
9853 }
developer72fb0bb2023-01-11 09:46:29 +08009854
developer2202b332023-05-24 16:23:22 +08009855 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
9856 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
9857 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +08009858
developer2202b332023-05-24 16:23:22 +08009859 /*step 1. mwctl dev %s set vlan_tag 0*/
9860 if_idx = if_nametoindex(interface_name);
9861 /*init mtk nl80211 vendor cmd*/
9862 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_VLAN;
9863 param.if_type = NL80211_ATTR_IFINDEX;
9864 param.if_idx = if_idx;
9865 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
developer72fb0bb2023-01-11 09:46:29 +08009866
developer2202b332023-05-24 16:23:22 +08009867 if (ret) {
9868 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9869 return RETURN_ERR;
9870 }
developer72fb0bb2023-01-11 09:46:29 +08009871
developer2202b332023-05-24 16:23:22 +08009872 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_TAG_INFO, 0)) {
9873 printf("Nla put attribute error\n");
9874 nlmsg_free(msg);
9875 goto err;
9876 }
developer72fb0bb2023-01-11 09:46:29 +08009877
developer2202b332023-05-24 16:23:22 +08009878 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9879 if (ret) {
9880 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9881 goto err;
9882 }
9883 mtk_nl80211_deint(&unl_ins);
9884 wifi_debug(DEBUG_NOTICE, "set vlan_tag 0 cmd success.\n");
developer72fb0bb2023-01-11 09:46:29 +08009885
developer2202b332023-05-24 16:23:22 +08009886 /*step 2. mwctl dev %s set vlan_priority 0*/
9887 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9888 if (ret) {
9889 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9890 return RETURN_ERR;
9891 }
developer72fb0bb2023-01-11 09:46:29 +08009892
developer2202b332023-05-24 16:23:22 +08009893 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_PRIORITY_INFO, 0)) {
9894 printf("Nla put attribute error\n");
9895 nlmsg_free(msg);
9896 goto err;
9897 }
developer72fb0bb2023-01-11 09:46:29 +08009898
developer2202b332023-05-24 16:23:22 +08009899 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9900 if (ret) {
9901 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9902 goto err;
9903 }
9904 mtk_nl80211_deint(&unl_ins);
9905 wifi_debug(DEBUG_NOTICE, "set vlan_priority 0 cmd success.\n");
9906
9907 /*step 3. mwctl dev %s set vlan_id 0*/
9908 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9909 if (ret) {
9910 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
developera1255e42023-05-13 17:45:02 +08009911 return RETURN_ERR;
developer2202b332023-05-24 16:23:22 +08009912 }
developer72fb0bb2023-01-11 09:46:29 +08009913
developer2202b332023-05-24 16:23:22 +08009914 if (nla_put_u16(msg, MTK_NL80211_VENDOR_ATTR_VLAN_ID_INFO, 0)) {
9915 printf("Nla put attribute error\n");
9916 nlmsg_free(msg);
9917 goto err;
9918 }
9919
9920 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9921 if (ret) {
9922 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9923 goto err;
9924 }
9925 mtk_nl80211_deint(&unl_ins);
9926 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
9927
9928 /*step 4. mwctl dev %s set vlan_en 0*/
9929 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9930 if (ret) {
9931 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9932 return RETURN_ERR;
9933 }
9934
9935 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_VLAN_EN_INFO, 0)) {
9936 printf("Nla put attribute error\n");
9937 nlmsg_free(msg);
9938 goto err;
9939 }
9940
9941 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9942 if (ret) {
9943 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9944 goto err;
9945 }
9946 mtk_nl80211_deint(&unl_ins);
9947 wifi_debug(DEBUG_NOTICE, "set vlan_id cmd success.\n");
9948
9949 /*step 5. mwctl dev %s set vlan_policy 0:4*/
9950 vlan_param.direction = 0;
9951 vlan_param.policy = 4;
9952 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9953 if (ret) {
9954 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9955 return RETURN_ERR;
9956 }
9957 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
9958 printf("Nla put attribute error\n");
9959 nlmsg_free(msg);
9960 goto err;
9961 }
9962
9963 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9964 if (ret) {
9965 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9966 goto err;
9967 }
9968 mtk_nl80211_deint(&unl_ins);
9969 wifi_debug(DEBUG_NOTICE, "set vlan_policy 0:4 cmd success.\n");
9970
9971 /*step 6. mwctl dev %s set vlan_policy 1:0*/
9972 vlan_param.direction = 1;
9973 vlan_param.policy = 0;
9974 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
9975 if (ret) {
9976 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
9977 return RETURN_ERR;
9978 }
9979
9980 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_VLAN_POLICY_INFO, sizeof(vlan_param), &vlan_param)) {
9981 printf("Nla put attribute error\n");
9982 nlmsg_free(msg);
9983 goto err;
9984 }
9985
9986 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
9987 if (ret) {
9988 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
9989 goto err;
9990 }
9991 /*deinit mtk nl80211 vendor msg*/
9992 mtk_nl80211_deint(&unl_ins);
9993 wifi_debug(DEBUG_NOTICE, "set vlan_policy 1:0 cmd success.\n");
9994
9995 /*TODO need to modify VLAN config in dat file*/
9996 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
9997
9998 return RETURN_OK;
9999err:
10000 mtk_nl80211_deint(&unl_ins);
10001 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10002 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010003}
10004
10005// 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.
10006INT wifi_createHostApdConfig(INT apIndex, BOOL createWpsCfg)
10007{
developera3511852023-06-14 14:12:59 +080010008 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010009}
10010
10011// starts hostapd, uses the variables in the hostapd config with format compatible with the specific hostapd implementation
10012INT wifi_startHostApd()
10013{
developera3511852023-06-14 14:12:59 +080010014 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +080010015 v_secure_system("systemctl start hostapd.service");
developera3511852023-06-14 14:12:59 +080010016 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10017 return RETURN_OK;
10018 //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 +080010019}
10020
10021// stops hostapd
developer69b61b02023-03-07 17:17:44 +080010022INT wifi_stopHostApd()
developer72fb0bb2023-01-11 09:46:29 +080010023{
developera3511852023-06-14 14:12:59 +080010024 char cmd[128] = {0};
10025 char buf[128] = {0};
developer75bd10c2023-06-27 11:34:08 +080010026 int res;
developer72fb0bb2023-01-11 09:46:29 +080010027
developer75bd10c2023-06-27 11:34:08 +080010028 res = snprintf(cmd, sizeof(cmd), "systemctl stop hostapd");
10029 if (os_snprintf_error(sizeof(cmd), res)) {
10030 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10031 return RETURN_ERR;
10032 }
developera3511852023-06-14 14:12:59 +080010033 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010034
developera3511852023-06-14 14:12:59 +080010035 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010036}
10037
10038// restart hostapd dummy function
10039INT wifi_restartHostApd()
10040{
developera3511852023-06-14 14:12:59 +080010041 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer33f13ba2023-07-12 16:19:06 +080010042 v_secure_system("systemctl restart hostapd-global");
developera3511852023-06-14 14:12:59 +080010043 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010044
developera3511852023-06-14 14:12:59 +080010045 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010046}
10047
10048// sets the AP enable status variable for the specified ap.
10049INT wifi_setApEnable(INT apIndex, BOOL enable)
10050{
developer7e4a2a62023-04-06 19:56:03 +080010051 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080010052 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +080010053 char cmd[MAX_CMD_SIZE] = {0};
10054 char buf[MAX_BUF_SIZE] = {0};
developer47cc27a2023-05-17 23:09:58 +080010055 BOOL status = FALSE;
developer7e4a2a62023-04-06 19:56:03 +080010056 int max_radio_num = 0;
10057 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010058 int res;
developer72fb0bb2023-01-11 09:46:29 +080010059
developer7e4a2a62023-04-06 19:56:03 +080010060 wifi_getApEnable(apIndex, &status);
developer72fb0bb2023-01-11 09:46:29 +080010061
developer7e4a2a62023-04-06 19:56:03 +080010062 wifi_getMaxRadioNumber(&max_radio_num);
10063 if (enable == status)
10064 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010065
developer7e4a2a62023-04-06 19:56:03 +080010066 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10067 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010068
developer7e4a2a62023-04-06 19:56:03 +080010069 if (enable == TRUE) {
10070 int radioIndex = apIndex % max_radio_num;
10071 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080010072 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s up", interface_name);
10073 if (os_snprintf_error(sizeof(cmd), res)) {
10074 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10075 return RETURN_ERR;
10076 }
developerf3c7d292023-05-29 17:57:16 +080010077 _syscmd(cmd, buf, sizeof(buf));
developer8a3bbbf2023-03-15 17:47:23 +080010078
developere40952c2023-06-15 18:46:43 +080010079 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
10080 if (os_snprintf_error(MAX_BUF_SIZE, res)) {
10081 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10082 return RETURN_ERR;
10083 }
10084 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
10085 if (os_snprintf_error(sizeof(cmd), res)) {
10086 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10087 return RETURN_ERR;
10088 }
developer7e4a2a62023-04-06 19:56:03 +080010089 _syscmd(cmd, buf, sizeof(buf));
10090 } else {
developere40952c2023-06-15 18:46:43 +080010091 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
10092 if (os_snprintf_error(sizeof(cmd), res)) {
10093 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10094 return RETURN_ERR;
10095 }
developer7e4a2a62023-04-06 19:56:03 +080010096 _syscmd(cmd, buf, sizeof(buf));
developere40952c2023-06-15 18:46:43 +080010097 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s down", interface_name);
10098 if (os_snprintf_error(sizeof(cmd), res)) {
10099 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10100 return RETURN_ERR;
10101 }
developerf3c7d292023-05-29 17:57:16 +080010102 _syscmd(cmd, buf, sizeof(buf));
developer7e4a2a62023-04-06 19:56:03 +080010103 }
developere40952c2023-06-15 18:46:43 +080010104 res = snprintf(cmd, MAX_CMD_SIZE, "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer7e4a2a62023-04-06 19:56:03 +080010105 interface_name, interface_name, enable, VAP_STATUS_FILE);
developere40952c2023-06-15 18:46:43 +080010106 if (os_snprintf_error(sizeof(cmd), res)) {
10107 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10108 return RETURN_ERR;
10109 }
developer7e4a2a62023-04-06 19:56:03 +080010110 _syscmd(cmd, buf, sizeof(buf));
10111 //Wait for wifi up/down to apply
10112 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010113}
10114
10115// Outputs the setting of the internal variable that is set by wifi_setApEnable().
10116INT wifi_getApEnable(INT apIndex, BOOL *output_bool)
10117{
developer7e4a2a62023-04-06 19:56:03 +080010118 char interface_name[IF_NAME_SIZE] = {0};
10119 char cmd[MAX_CMD_SIZE] = {0};
developerc1aa6532023-06-09 09:37:01 +080010120 char buf[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080010121 int res;
developer72fb0bb2023-01-11 09:46:29 +080010122
developer7e4a2a62023-04-06 19:56:03 +080010123 if ((!output_bool) || (apIndex < 0) || (apIndex >= MAX_APS))
10124 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010125
developer7e4a2a62023-04-06 19:56:03 +080010126 *output_bool = 0;
developer72fb0bb2023-01-11 09:46:29 +080010127
developer7e4a2a62023-04-06 19:56:03 +080010128 if ((apIndex >= 0) && (apIndex < MAX_APS)) {
10129 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK) {
10130 *output_bool = FALSE;
10131 return RETURN_OK;
10132 }
developerc1aa6532023-06-09 09:37:01 +080010133
developer75bd10c2023-06-27 11:34:08 +080010134 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s status | grep state | cut -d '=' -f2", interface_name);
10135 if (os_snprintf_error(sizeof(cmd), res)) {
10136 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10137 return RETURN_ERR;
10138 }
developerc1aa6532023-06-09 09:37:01 +080010139 _syscmd(cmd, buf, sizeof(buf));
10140
10141 if(strncmp(buf, "ENABLED", 7) == 0 || strncmp(buf, "ACS", 3) == 0 ||
10142 strncmp(buf, "HT_SCAN", 7) == 0 || strncmp(buf, "DFS", 3) == 0) {
10143 *output_bool = TRUE;
10144 }
developer7e4a2a62023-04-06 19:56:03 +080010145 }
developer72fb0bb2023-01-11 09:46:29 +080010146
developer7e4a2a62023-04-06 19:56:03 +080010147 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010148}
10149
developer69b61b02023-03-07 17:17:44 +080010150// Outputs the AP "Enabled" "Disabled" status from driver
10151INT wifi_getApStatus(INT apIndex, CHAR *output_string)
developer72fb0bb2023-01-11 09:46:29 +080010152{
developer9ce44382023-06-28 11:09:37 +080010153 BOOL output_bool = 0;
developere40952c2023-06-15 18:46:43 +080010154 int res;
developer72fb0bb2023-01-11 09:46:29 +080010155
developer7e4a2a62023-04-06 19:56:03 +080010156 if (!output_string) {
10157 printf("%s: null pointer!", __func__);
10158 return RETURN_ERR;
10159 }
developer72fb0bb2023-01-11 09:46:29 +080010160
developer7e4a2a62023-04-06 19:56:03 +080010161 wifi_getApEnable(apIndex, &output_bool);
developer72fb0bb2023-01-11 09:46:29 +080010162
developer7e4a2a62023-04-06 19:56:03 +080010163 if(output_bool == 1)
developere40952c2023-06-15 18:46:43 +080010164 res = snprintf(output_string, 32, "Up");
developer7e4a2a62023-04-06 19:56:03 +080010165 else
developere40952c2023-06-15 18:46:43 +080010166 res = snprintf(output_string, 32, "Disable");
10167 if (os_snprintf_error(32, res)) {
10168 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10169 return RETURN_ERR;
10170 }
developer7e4a2a62023-04-06 19:56:03 +080010171
10172 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010173}
10174
10175//Indicates whether or not beacons include the SSID name.
10176// outputs a 1 if SSID on the AP is enabled, else outputs 0
10177INT wifi_getApSsidAdvertisementEnable(INT apIndex, BOOL *output)
10178{
developera3511852023-06-14 14:12:59 +080010179 //get the running status
10180 char config_file[MAX_BUF_SIZE] = {0};
10181 char buf[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080010182 int res;
developer72fb0bb2023-01-11 09:46:29 +080010183
developera3511852023-06-14 14:12:59 +080010184 if (!output)
10185 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010186
developer32f2a182023-06-27 19:50:41 +080010187 res = snprintf(config_file, sizeof(config_file),
10188 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010189 if (os_snprintf_error(sizeof(config_file), res)) {
10190 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10191 return RETURN_ERR;
10192 }
developera3511852023-06-14 14:12:59 +080010193 wifi_hostapdRead(config_file, "ignore_broadcast_ssid", buf, sizeof(buf));
10194 // default is enable
10195 if (strlen(buf) == 0 || strncmp("0", buf, 1) == 0)
10196 *output = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080010197
developera3511852023-06-14 14:12:59 +080010198 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010199}
10200
10201// sets an internal variable for ssid advertisement. Set to 1 to enable, set to 0 to disable
10202INT wifi_setApSsidAdvertisementEnable(INT apIndex, BOOL enable)
10203{
developera3511852023-06-14 14:12:59 +080010204 //store the config, apply instantly
10205 char config_file[MAX_BUF_SIZE] = {0};
10206 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010207 int res;
developer72fb0bb2023-01-11 09:46:29 +080010208
developera3511852023-06-14 14:12:59 +080010209 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10210 list.name = "ignore_broadcast_ssid";
10211 list.value = enable?"0":"1";
developer72fb0bb2023-01-11 09:46:29 +080010212
developer32f2a182023-06-27 19:50:41 +080010213 res = snprintf(config_file, sizeof(config_file),
10214 "%s%d.conf", CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010215 if (os_snprintf_error(sizeof(config_file), res)) {
10216 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10217 return RETURN_ERR;
10218 }
developera3511852023-06-14 14:12:59 +080010219 wifi_hostapdWrite(config_file, &list, 1);
10220 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10221 //TODO: call hostapd_cli for dynamic_config_control
10222 wifi_reloadAp(apIndex);
10223 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010224
developera3511852023-06-14 14:12:59 +080010225 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010226}
10227
10228//The maximum number of retransmission for a packet. This corresponds to IEEE 802.11 parameter dot11ShortRetryLimit.
10229INT wifi_getApRetryLimit(INT apIndex, UINT *output_uint)
10230{
developer47cc27a2023-05-17 23:09:58 +080010231 /* get the running status */
10232 if(!output_uint)
developera3511852023-06-14 14:12:59 +080010233 return RETURN_ERR;
developer47cc27a2023-05-17 23:09:58 +080010234
10235 *output_uint = 15;
10236 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010237}
10238
developer47cc27a2023-05-17 23:09:58 +080010239/*Do not support AP retry limit fix*/
developer72fb0bb2023-01-11 09:46:29 +080010240INT wifi_setApRetryLimit(INT apIndex, UINT number)
10241{
developer47cc27a2023-05-17 23:09:58 +080010242 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010243}
10244
developer95c045d2023-05-24 19:26:28 +080010245int get_wmm_cap_status_callback(struct nl_msg *msg, void *data)
10246{
10247 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10248 struct nlattr *vndr_tb[MTK_NL80211_VENDOR_WMM_ATTR_MAX + 1];
10249 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developer2f79c922023-06-02 17:33:42 +080010250 unsigned char *status = (unsigned char *)data;
developer95c045d2023-05-24 19:26:28 +080010251 int err = 0;
developer95c045d2023-05-24 19:26:28 +080010252
10253 err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10254 genlmsg_attrlen(gnlh, 0), NULL);
10255 if (err < 0)
10256 return err;
10257
10258 if (tb[NL80211_ATTR_VENDOR_DATA]) {
10259 err = nla_parse_nested(vndr_tb, MTK_NL80211_VENDOR_WMM_ATTR_MAX,
10260 tb[NL80211_ATTR_VENDOR_DATA], NULL);
10261 if (err < 0)
10262 return err;
10263
10264 if (vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]) {
developer95c045d2023-05-24 19:26:28 +080010265 *status = nla_get_u8(vndr_tb[MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO]);
10266 }
10267 }
10268
10269 return 0;
10270}
10271
developer72fb0bb2023-01-11 09:46:29 +080010272//Indicates whether this access point supports WiFi Multimedia (WMM) Access Categories (AC).
10273INT wifi_getApWMMCapability(INT apIndex, BOOL *output)
10274{
developer95c045d2023-05-24 19:26:28 +080010275 int if_idx, ret = 0;
developera3511852023-06-14 14:12:59 +080010276 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010277 unsigned char status = 0;
10278 struct nl_msg *msg = NULL;
10279 struct nlattr * msg_data = NULL;
10280 struct mtk_nl80211_param param;
10281 struct unl unl_ins;
developer8e6583c2023-05-23 13:36:06 +080010282
developera3511852023-06-14 14:12:59 +080010283 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10284 if(!output)
developerdaf24792023-06-06 11:40:04 +080010285 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010286
developer95c045d2023-05-24 19:26:28 +080010287 if (apIndex > MAX_APS) {
10288 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10289 return RETURN_ERR;
10290 }
10291
developera3511852023-06-14 14:12:59 +080010292 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
developerdaf24792023-06-06 11:40:04 +080010293 return RETURN_ERR;
developer95c045d2023-05-24 19:26:28 +080010294
10295 if_idx = if_nametoindex(interface_name);
10296 /*init mtk nl80211 vendor cmd*/
10297 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10298 param.if_type = NL80211_ATTR_IFINDEX;
10299 param.if_idx = if_idx;
10300 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10301
10302 if (ret) {
10303 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10304 return RETURN_ERR;
10305 }
10306
10307 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, 0xf)) {
10308 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10309 nlmsg_free(msg);
10310 goto err;
10311 }
10312
10313 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, get_wmm_cap_status_callback,
10314 (void *)&status);
10315 if (ret) {
10316 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10317 goto err;
10318 }
10319 mtk_nl80211_deint(&unl_ins);
10320
10321 *output = status == 0 ? FALSE : TRUE;
10322 wifi_debug(DEBUG_NOTICE, "wmm cap (%u).\n", (unsigned int)(*output));
developer8e6583c2023-05-23 13:36:06 +080010323
developera3511852023-06-14 14:12:59 +080010324 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10325 return RETURN_OK;
10326err:
developer95c045d2023-05-24 19:26:28 +080010327 mtk_nl80211_deint(&unl_ins);
10328 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10329 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010330}
10331
10332//Indicates whether this access point supports WMM Unscheduled Automatic Power Save Delivery (U-APSD). Note: U-APSD support implies WMM support.
10333INT wifi_getApUAPSDCapability(INT apIndex, BOOL *output)
10334{
developera3511852023-06-14 14:12:59 +080010335 //get the running status from driver
10336 char cmd[128] = {0};
10337 char buf[128] = {0};
10338 int max_radio_num = 0, radioIndex = 0;
10339 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080010340 int res;
developer72fb0bb2023-01-11 09:46:29 +080010341
developera3511852023-06-14 14:12:59 +080010342 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010343
developera3511852023-06-14 14:12:59 +080010344 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080010345 if(max_radio_num == 0){
10346 return RETURN_ERR;
10347 }
developera3511852023-06-14 14:12:59 +080010348 radioIndex = apIndex % max_radio_num;
10349 phyId = radio_index_to_phy(radioIndex);
developere40952c2023-06-15 18:46:43 +080010350 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d info | grep u-APSD", phyId);
10351 if (os_snprintf_error(sizeof(cmd), res)) {
10352 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10353 return RETURN_ERR;
10354 }
developera3511852023-06-14 14:12:59 +080010355 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010356
developera3511852023-06-14 14:12:59 +080010357 if (strlen(buf) > 0)
10358 *output = true;
developer72fb0bb2023-01-11 09:46:29 +080010359
developera3511852023-06-14 14:12:59 +080010360 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010361
developera3511852023-06-14 14:12:59 +080010362 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010363}
10364
10365//Whether WMM support is currently enabled. When enabled, this is indicated in beacon frames.
10366INT wifi_getApWmmEnable(INT apIndex, BOOL *output)
10367{
developera3511852023-06-14 14:12:59 +080010368 return wifi_getApWMMCapability(apIndex, output);
developer72fb0bb2023-01-11 09:46:29 +080010369}
10370
10371// enables/disables WMM on the hardwawre for this AP. enable==1, disable == 0
10372INT wifi_setApWmmEnable(INT apIndex, BOOL enable)
10373{
developer95c045d2023-05-24 19:26:28 +080010374 int if_idx, ret = 0;
10375 char interface_name[16] = {0};
developer95c045d2023-05-24 19:26:28 +080010376 struct nl_msg *msg = NULL;
10377 struct nlattr * msg_data = NULL;
10378 struct mtk_nl80211_param param;
10379 struct unl unl_ins;
developer72fb0bb2023-01-11 09:46:29 +080010380
developer95c045d2023-05-24 19:26:28 +080010381 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010382
developer95c045d2023-05-24 19:26:28 +080010383 if (apIndex > MAX_APS) {
10384 wifi_debug(DEBUG_ERROR, "Invalid apIndex %d\n", apIndex);
10385 return RETURN_ERR;
10386 }
developer72fb0bb2023-01-11 09:46:29 +080010387
developer95c045d2023-05-24 19:26:28 +080010388 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10389 return RETURN_ERR;
developer8e6583c2023-05-23 13:36:06 +080010390
developer95c045d2023-05-24 19:26:28 +080010391 if_idx = if_nametoindex(interface_name);
10392 /*init mtk nl80211 vendor cmd*/
10393 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_WMM;
10394 param.if_type = NL80211_ATTR_IFINDEX;
10395 param.if_idx = if_idx;
10396 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
10397
10398 if (ret) {
10399 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
10400 return RETURN_ERR;
10401 }
10402
10403 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_WMM_AP_CAP_INFO, enable ? 1 : 0)) {
10404 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
10405 nlmsg_free(msg);
10406 goto err;
10407 }
10408
10409 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
10410 if (ret) {
10411 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vendor msg fails\n");
10412 goto err;
10413 }
10414 mtk_nl80211_deint(&unl_ins);
10415
10416 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
10417 return RETURN_OK;
10418err:
10419 mtk_nl80211_deint(&unl_ins);
10420 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
10421 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010422}
10423
developer95c045d2023-05-24 19:26:28 +080010424
developer72fb0bb2023-01-11 09:46:29 +080010425//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.
10426INT wifi_getApWmmUapsdEnable(INT apIndex, BOOL *output)
10427{
developer75bd10c2023-06-27 11:34:08 +080010428 int res;
10429
developera3511852023-06-14 14:12:59 +080010430 //get the running status from driver
10431 if(!output)
10432 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010433
developera3511852023-06-14 14:12:59 +080010434 char config_file[128] = {0};
10435 char buf[16] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010436
developer75bd10c2023-06-27 11:34:08 +080010437 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10438 if (os_snprintf_error(sizeof(config_file), res)) {
10439 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10440 return RETURN_ERR;
10441 }
developera3511852023-06-14 14:12:59 +080010442 wifi_hostapdRead(config_file, "uapsd_advertisement_enabled", buf, sizeof(buf));
10443 if (strlen(buf) == 0 || strncmp("1", buf, 1) == 0)
10444 *output = TRUE;
10445 else
10446 *output = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080010447
developera3511852023-06-14 14:12:59 +080010448 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010449}
10450
10451// enables/disables Automatic Power Save Delivery on the hardwarwe for this AP
10452INT wifi_setApWmmUapsdEnable(INT apIndex, BOOL enable)
10453{
developera3511852023-06-14 14:12:59 +080010454 //save config and apply instantly.
10455 char config_file[MAX_BUF_SIZE] = {0};
10456 struct params list;
developer75bd10c2023-06-27 11:34:08 +080010457 int res;
developer72fb0bb2023-01-11 09:46:29 +080010458
developera3511852023-06-14 14:12:59 +080010459 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10460 list.name = "uapsd_advertisement_enabled";
10461 list.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080010462
developer75bd10c2023-06-27 11:34:08 +080010463 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10464 if (os_snprintf_error(sizeof(config_file), res)) {
10465 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10466 return RETURN_ERR;
10467 }
developera3511852023-06-14 14:12:59 +080010468 wifi_hostapdWrite(config_file, &list, 1);
10469 wifi_hostapdProcessUpdate(apIndex, &list, 1);
10470 wifi_quick_reload_ap(apIndex);
10471 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010472
developera3511852023-06-14 14:12:59 +080010473 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010474}
10475
10476// Sets the WMM ACK policy on the hardware. AckPolicy false means do not acknowledge, true means acknowledge
10477INT wifi_setApWmmOgAckPolicy(INT apIndex, INT class, BOOL ackPolicy) //RDKB
10478{
developera3511852023-06-14 14:12:59 +080010479 char interface_name[16] = {0};
10480 // assume class 0->BE, 1->BK, 2->VI, 3->VO
10481 char cmd[MAX_CMD_SIZE] = {0};
10482 char buf[128] = {0};
10483 char ack_filepath[128] = {0};
10484 uint16_t bitmap = 0;
10485 uint16_t class_map[4] = {0x0009, 0x0006, 0x0030, 0x00C0};
10486 FILE *f = NULL;
developere40952c2023-06-15 18:46:43 +080010487 int res;
developerc14d83a2023-06-29 20:09:42 +080010488 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080010489
developera3511852023-06-14 14:12:59 +080010490 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010491
developera3511852023-06-14 14:12:59 +080010492 // Get current setting
developere40952c2023-06-15 18:46:43 +080010493 res = snprintf(ack_filepath, sizeof(ack_filepath), "%s%d.txt", NOACK_MAP_FILE, apIndex);
10494 if (os_snprintf_error(sizeof(ack_filepath), res)) {
10495 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10496 return RETURN_ERR;
10497 }
10498 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", ack_filepath);
10499 if (os_snprintf_error(sizeof(cmd), res)) {
10500 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10501 return RETURN_ERR;
10502 }
developera3511852023-06-14 14:12:59 +080010503 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +080010504 if (strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080010505 if (hal_strtoul(buf, 10, &tmp) < 0) {
10506 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080010507 }
developerc14d83a2023-06-29 20:09:42 +080010508 bitmap = tmp;
developerd14dff12023-06-28 22:47:44 +080010509 }
developer72fb0bb2023-01-11 09:46:29 +080010510
developera3511852023-06-14 14:12:59 +080010511 if (ackPolicy == TRUE) { // True, unset this class
10512 bitmap &= ~class_map[class];
10513 } else { // False, set this class
10514 bitmap |= class_map[class];
10515 }
developer72fb0bb2023-01-11 09:46:29 +080010516
developera3511852023-06-14 14:12:59 +080010517 f = fopen(ack_filepath, "w");
10518 if (f == NULL) {
developer37646972023-06-29 10:58:43 +080010519 if (fprintf(stderr, "%s: fopen failed\n", __func__) < 0)
10520 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
developera3511852023-06-14 14:12:59 +080010521 return RETURN_ERR;
10522 }
developer37646972023-06-29 10:58:43 +080010523 if (fprintf(f, "%hu", bitmap) < 0)
10524 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
10525
10526 if (fclose(f) == EOF) {
10527 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
10528 return RETURN_ERR;
10529 }
developer72fb0bb2023-01-11 09:46:29 +080010530
developera3511852023-06-14 14:12:59 +080010531 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
10532 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010533 res = snprintf(cmd, sizeof(cmd), "iw dev %s set noack_map 0x%04x\n", interface_name, bitmap);
10534 if (os_snprintf_error(sizeof(cmd), res)) {
10535 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10536 return RETURN_ERR;
10537 }
developera3511852023-06-14 14:12:59 +080010538 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010539
developera3511852023-06-14 14:12:59 +080010540 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
10541 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010542}
10543
10544//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.
10545INT wifi_getApMaxAssociatedDevices(INT apIndex, UINT *output_uint)
10546{
developer75bd10c2023-06-27 11:34:08 +080010547 int res;
10548
developera3511852023-06-14 14:12:59 +080010549 //get the running status from driver
10550 if(!output_uint)
10551 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010552
developera3511852023-06-14 14:12:59 +080010553 char output[16]={'\0'};
10554 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010555
developer75bd10c2023-06-27 11:34:08 +080010556 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10557 if (os_snprintf_error(sizeof(config_file), res)) {
10558 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10559 return RETURN_ERR;
10560 }
developera3511852023-06-14 14:12:59 +080010561 wifi_hostapdRead(config_file, "max_num_sta", output, sizeof(output));
10562 if (strlen(output) == 0) *output_uint = MAX_ASSOCIATED_STA_NUM;
10563 else {
10564 int device_num = atoi(output);
10565 if (device_num > MAX_ASSOCIATED_STA_NUM || device_num < 0) {
10566 wifi_dbg_printf("\n[%s]: get max_num_sta error: %d", __func__, device_num);
10567 return RETURN_ERR;
10568 }
10569 else {
10570 *output_uint = device_num;
10571 }
10572 }
developer72fb0bb2023-01-11 09:46:29 +080010573
developera3511852023-06-14 14:12:59 +080010574 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010575}
10576
10577INT wifi_setApMaxAssociatedDevices(INT apIndex, UINT number)
10578{
developera3511852023-06-14 14:12:59 +080010579 //store to wifi config, apply instantly
10580 char str[MAX_BUF_SIZE]={'\0'};
10581 struct params params;
10582 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080010583 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080010584
developera3511852023-06-14 14:12:59 +080010585 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10586 if (number > MAX_ASSOCIATED_STA_NUM) {
10587 WIFI_ENTRY_EXIT_DEBUG("%s: Invalid input\n",__func__);
10588 return RETURN_ERR;
10589 }
developer75bd10c2023-06-27 11:34:08 +080010590 res = snprintf(str, sizeof(str), "%d", number);
10591 if (os_snprintf_error(sizeof(str), res)) {
10592 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10593 return RETURN_ERR;
10594 }
developera3511852023-06-14 14:12:59 +080010595 params.name = "max_num_sta";
10596 params.value = str;
developer72fb0bb2023-01-11 09:46:29 +080010597
developer32f2a182023-06-27 19:50:41 +080010598 res = snprintf(config_file,
10599 sizeof(config_file), "%s%d.conf",CONFIG_PREFIX, apIndex);
developer75bd10c2023-06-27 11:34:08 +080010600 if (os_snprintf_error(sizeof(config_file), res)) {
10601 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10602 return RETURN_ERR;
10603 }
developer32f2a182023-06-27 19:50:41 +080010604 ret = wifi_hostapdWrite(config_file, &params, 1);
developera3511852023-06-14 14:12:59 +080010605 if (ret) {
10606 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdWrite() return %d\n"
10607 ,__func__, ret);
10608 }
developer72fb0bb2023-01-11 09:46:29 +080010609
developera3511852023-06-14 14:12:59 +080010610 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10611 if (ret) {
10612 WIFI_ENTRY_EXIT_DEBUG("Inside %s: wifi_hostapdProcessUpdate() return %d\n"
10613 ,__func__, ret);
10614 }
10615 wifi_reloadAp(apIndex);
10616 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010617
developera3511852023-06-14 14:12:59 +080010618 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010619}
10620
10621//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.
10622INT wifi_getApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT *output_uint)
10623{
developera3511852023-06-14 14:12:59 +080010624 //get the current threshold
10625 if(!output_uint)
10626 return RETURN_ERR;
10627 wifi_getApMaxAssociatedDevices(apIndex, output_uint);
10628 if (*output_uint == 0)
10629 *output_uint = 50;
10630 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010631}
10632
10633INT wifi_setApAssociatedDevicesHighWatermarkThreshold(INT apIndex, UINT Threshold)
10634{
developera3511852023-06-14 14:12:59 +080010635 //store the config, reset threshold, reset AssociatedDevicesHighWatermarkThresholdReached, reset AssociatedDevicesHighWatermarkDate to current time
10636 if (!wifi_setApMaxAssociatedDevices(apIndex, Threshold))
10637 return RETURN_OK;
10638 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010639}
10640
10641//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.
10642INT wifi_getApAssociatedDevicesHighWatermarkThresholdReached(INT apIndex, UINT *output_uint)
10643{
developera3511852023-06-14 14:12:59 +080010644 if(!output_uint)
10645 return RETURN_ERR;
10646 *output_uint = 3;
10647 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010648}
10649
10650//Maximum number of associated devices that have ever associated with the access point concurrently since the last reset of the device or WiFi module.
10651INT wifi_getApAssociatedDevicesHighWatermark(INT apIndex, UINT *output_uint)
10652{
developera3511852023-06-14 14:12:59 +080010653 if(!output_uint)
10654 return RETURN_ERR;
10655 *output_uint = 3;
10656 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010657}
10658
10659//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.
10660INT wifi_getApAssociatedDevicesHighWatermarkDate(INT apIndex, ULONG *output_in_seconds)
10661{
developera3511852023-06-14 14:12:59 +080010662 if(!output_in_seconds)
10663 return RETURN_ERR;
10664 *output_in_seconds = 0;
10665 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010666}
10667
10668//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
10669INT wifi_getApSecurityModesSupported(INT apIndex, CHAR *output)
10670{
developere40952c2023-06-15 18:46:43 +080010671 int res;
10672
developera3511852023-06-14 14:12:59 +080010673 if(!output || apIndex>=MAX_APS)
10674 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080010675 //res = snprintf(output, 128, "None,WPA-Personal,WPA2-Personal,WPA-WPA2-Personal,WPA-Enterprise,WPA2-Enterprise,WPA-WPA2-Enterprise");
10676 res = snprintf(output, 128, "None,WPA2-Personal,WPA-WPA2-Personal,WPA2-Enterprise,WPA-WPA2-Enterprise,WPA3-Personal,WPA3-Enterprise");
10677 if (os_snprintf_error(128, res)) {
10678 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10679 return RETURN_ERR;
10680 }
developera3511852023-06-14 14:12:59 +080010681 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010682}
developer72fb0bb2023-01-11 09:46:29 +080010683
10684//The value MUST be a member of the list reported by the ModesSupported parameter. Indicates which security mode is enabled.
10685INT wifi_getApSecurityModeEnabled(INT apIndex, CHAR *output)
10686{
developera3511852023-06-14 14:12:59 +080010687 char config_file[128] = {0};
10688 char wpa[16] = {0};
10689 char key_mgmt[64] = {0};
developer9ce44382023-06-28 11:09:37 +080010690 int res = -1;
developere40952c2023-06-15 18:46:43 +080010691
developera3511852023-06-14 14:12:59 +080010692 if (!output)
10693 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010694
developer75bd10c2023-06-27 11:34:08 +080010695 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10696 if (os_snprintf_error(sizeof(config_file), res)) {
10697 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10698 return RETURN_ERR;
10699 }
developera3511852023-06-14 14:12:59 +080010700 wifi_hostapdRead(config_file, "wpa", wpa, sizeof(wpa));
developer72fb0bb2023-01-11 09:46:29 +080010701
developer32f2a182023-06-27 19:50:41 +080010702 memcpy(output, "None", 4);//Copying "None" to output string for default case
10703 output[4] = '\0';
developera3511852023-06-14 14:12:59 +080010704 wifi_hostapdRead(config_file, "wpa_key_mgmt", key_mgmt, sizeof(key_mgmt));
10705 if (strstr(key_mgmt, "WPA-PSK") && strstr(key_mgmt, "SAE") == NULL) {
10706 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010707 res = snprintf(output, 32, "WPA-Personal");
developera3511852023-06-14 14:12:59 +080010708 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010709 res = snprintf(output, 32, "WPA2-Personal");
developera3511852023-06-14 14:12:59 +080010710 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010711 res = snprintf(output, 32, "WPA-WPA2-Personal");
developer72fb0bb2023-01-11 09:46:29 +080010712
developera3511852023-06-14 14:12:59 +080010713 } else if (strstr(key_mgmt, "WPA-EAP-SUITE-B-192")) {
developere40952c2023-06-15 18:46:43 +080010714 res = snprintf(output, 32, "WPA3-Enterprise");
developera3511852023-06-14 14:12:59 +080010715 } else if (strstr(key_mgmt, "WPA-EAP")) {
10716 if (!strcmp(wpa, "1"))
developere40952c2023-06-15 18:46:43 +080010717 res = snprintf(output, 32, "WPA-Enterprise");
developera3511852023-06-14 14:12:59 +080010718 else if (!strcmp(wpa, "2"))
developere40952c2023-06-15 18:46:43 +080010719 res = snprintf(output, 32, "WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010720 else if (!strcmp(wpa, "3"))
developere40952c2023-06-15 18:46:43 +080010721 res = snprintf(output, 32, "WPA-WPA2-Enterprise");
developera3511852023-06-14 14:12:59 +080010722 } else if (strstr(key_mgmt, "SAE")) {
10723 if (strstr(key_mgmt, "WPA-PSK") == NULL)
developere40952c2023-06-15 18:46:43 +080010724 res = snprintf(output, 32, "WPA3-Personal");
developera3511852023-06-14 14:12:59 +080010725 else
developere40952c2023-06-15 18:46:43 +080010726 res = snprintf(output, 32, "WPA3-Personal-Transition");
10727 }
10728 if (os_snprintf_error(32, res)) {
10729 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10730 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080010731 }
developer72fb0bb2023-01-11 09:46:29 +080010732
developera3511852023-06-14 14:12:59 +080010733 //save the beaconTypeString to wifi config and hostapd config file. Wait for wifi reset or hostapd restart to apply
10734 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010735}
developer69b61b02023-03-07 17:17:44 +080010736
developer72fb0bb2023-01-11 09:46:29 +080010737INT wifi_setApSecurityModeEnabled(INT apIndex, CHAR *encMode)
10738{
developer32f2a182023-06-27 19:50:41 +080010739 char securityType[32] = {0};
10740 char authMode[32] = {0};
10741 unsigned long len_sec, len_auth;
developera3511852023-06-14 14:12:59 +080010742 //store settings and wait for wifi up to apply
10743 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
10744 if(!encMode)
10745 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010746
developera3511852023-06-14 14:12:59 +080010747 if (strcmp(encMode, "None")==0)
10748 {
developer32f2a182023-06-27 19:50:41 +080010749 len_sec = strlen("None");
10750 len_auth = strlen("None");
10751 memcpy(securityType, "None", len_sec);
10752 memcpy(authMode, "None", len_auth);
10753 } else if (strcmp(encMode, "WPA-WPA2-Personal")==0) {
10754 len_sec = strlen("WPAand11i");
10755 memcpy(securityType, "WPAand11i", len_sec);
10756 len_auth = strlen("PSKAuthentication");
10757 memcpy(authMode, "PSKAuthentication", len_auth);
10758 } else if (strcmp(encMode, "WPA-WPA2-Enterprise")==0) {
10759 len_sec = strlen("WPAand11i");
10760 memcpy(securityType, "WPAand11i", len_sec);
10761 len_auth = strlen("EAPAuthentication");
10762 memcpy(authMode, "EAPAuthentication", len_auth);
10763 } else if (strcmp(encMode, "WPA-Personal")==0) {
10764 len_sec = strlen("WPA");
10765 memcpy(securityType, "WPA", len_sec);
10766 len_auth = strlen("PSKAuthentication");
10767 memcpy(authMode, "PSKAuthentication", len_auth);
10768 } else if (strcmp(encMode, "WPA-Enterprise")==0) {
10769 len_sec = strlen("WPA");
10770 memcpy(securityType, "WPA", len_sec);
10771 len_auth = strlen("EAPAuthentication");
10772 memcpy(authMode, "EAPAuthentication", len_auth);
10773 } else if (strcmp(encMode, "WPA2-Personal")==0) {
10774 len_sec = strlen("11i");
10775 memcpy(securityType, "11i", len_sec);
10776 len_auth = strlen("PSKAuthentication");
10777 memcpy(authMode, "PSKAuthentication", len_auth);
10778 } else if (strcmp(encMode, "WPA2-Enterprise")==0) {
10779 len_sec = strlen("11i");
10780 memcpy(securityType, "11i", len_sec);
10781 len_auth = strlen("EAPAuthentication");
10782 memcpy(authMode, "EAPAuthentication", len_auth);
10783 } else if (strcmp(encMode, "WPA3-Personal") == 0) {
10784 len_sec = strlen("11i");
10785 memcpy(securityType, "11i", len_sec);
10786 len_auth = strlen("SAEAuthentication");
10787 memcpy(authMode, "SAEAuthentication", len_auth);
10788 } else if (strcmp(encMode, "WPA3-Personal-Transition") == 0) {
10789 len_sec = strlen("11i");
10790 memcpy(securityType, "11i", len_sec);
10791 len_auth = strlen("PSK-SAEAuthentication");
10792 memcpy(authMode, "PSK-SAEAuthentication", len_auth);
10793 } else if (strcmp(encMode, "WPA3-Enterprise") == 0) {
10794 len_sec = strlen("11i");
10795 memcpy(securityType, "11i", len_sec);
10796 len_auth = strlen("EAP_192-bit_Authentication");
10797 memcpy(authMode, "EAP_192-bit_Authentication", len_auth);
10798 } else if (strcmp(encMode, "OWE") == 0) {
10799 len_sec = strlen("11i");
10800 memcpy(securityType, "11i", len_sec);
10801 len_auth = strlen("Enhanced_Open");
10802 memcpy(authMode, "Enhanced_Open", len_auth);
10803 } else {
10804 len_sec = strlen("None");
10805 memcpy(securityType, "None", len_sec);
10806 len_auth = strlen("None");
10807 memcpy(authMode, "None", len_auth);
developera3511852023-06-14 14:12:59 +080010808 }
developer32f2a182023-06-27 19:50:41 +080010809 securityType[len_sec] = '\0';
10810 authMode[len_auth] = '\0';
developera3511852023-06-14 14:12:59 +080010811 wifi_setApBeaconType(apIndex, securityType);
10812 wifi_setApBasicAuthenticationMode(apIndex, authMode);
10813 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010814
developera3511852023-06-14 14:12:59 +080010815 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080010816}
developer72fb0bb2023-01-11 09:46:29 +080010817
10818
10819//A literal PreSharedKey (PSK) expressed as a hexadecimal string.
10820// output_string must be pre-allocated as 64 character string by caller
10821// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10822INT wifi_getApSecurityPreSharedKey(INT apIndex, CHAR *output_string)
10823{
developera3511852023-06-14 14:12:59 +080010824 char buf[16] = {0};
10825 char config_file[MAX_BUF_SIZE] = {0};
10826 int res;
developer72fb0bb2023-01-11 09:46:29 +080010827
developera3511852023-06-14 14:12:59 +080010828 if(output_string==NULL)
10829 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010830
developera3511852023-06-14 14:12:59 +080010831 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10832 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010833 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10834 return RETURN_ERR;
10835 }
developera3511852023-06-14 14:12:59 +080010836 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080010837
developera3511852023-06-14 14:12:59 +080010838 if(strcmp(buf,"0")==0)
10839 {
10840 printf("wpa_mode is %s ......... \n",buf);
10841 return RETURN_ERR;
10842 }
developer72fb0bb2023-01-11 09:46:29 +080010843
developera3511852023-06-14 14:12:59 +080010844 wifi_dbg_printf("\nFunc=%s\n",__func__);
10845 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
10846 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080010847 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10848 return RETURN_ERR;
10849 }
developere5750452023-05-15 16:46:42 +080010850 wifi_hostapdRead(config_file,"wpa_psk",output_string,65);
developera3511852023-06-14 14:12:59 +080010851 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010852
developera3511852023-06-14 14:12:59 +080010853 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010854}
10855
10856// sets an enviornment variable for the psk. Input string preSharedKey must be a maximum of 64 characters
10857// PSK Key of 8 to 63 characters is considered an ASCII string, and 64 characters are considered as HEX value
10858INT wifi_setApSecurityPreSharedKey(INT apIndex, CHAR *preSharedKey)
10859{
developera3511852023-06-14 14:12:59 +080010860 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
10861 struct params params={'\0'};
developer32f2a182023-06-27 19:50:41 +080010862 int ret;
developera3511852023-06-14 14:12:59 +080010863 char config_file[MAX_BUF_SIZE] = {0};
developer72fb0bb2023-01-11 09:46:29 +080010864
developera3511852023-06-14 14:12:59 +080010865 if(NULL == preSharedKey)
10866 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010867
developera3511852023-06-14 14:12:59 +080010868 params.name = "wpa_psk";
developer72fb0bb2023-01-11 09:46:29 +080010869
developera3511852023-06-14 14:12:59 +080010870 if(strlen(preSharedKey) != 64)
10871 {
10872 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 64 chars\n");
10873 return RETURN_ERR;
10874 }
10875 params.value = preSharedKey;
developer32f2a182023-06-27 19:50:41 +080010876 ret = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10877 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080010878 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10879 return RETURN_ERR;
10880 }
developera3511852023-06-14 14:12:59 +080010881 ret = wifi_hostapdWrite(config_file, &params, 1);
10882 if(!ret) {
10883 ret = wifi_hostapdProcessUpdate(apIndex, &params, 1);
10884 wifi_reloadAp(apIndex);
10885 }
10886 return ret;
10887 //TODO: call hostapd_cli for dynamic_config_control
developer72fb0bb2023-01-11 09:46:29 +080010888}
10889
10890//A passphrase from which the PreSharedKey is to be generated, for WPA-Personal or WPA2-Personal or WPA-WPA2-Personal security modes.
10891// outputs the passphrase, maximum 63 characters
10892INT wifi_getApSecurityKeyPassphrase(INT apIndex, CHAR *output_string)
10893{
developera3511852023-06-14 14:12:59 +080010894 char config_file[MAX_BUF_SIZE] = {0}, buf[32] = {0};
developer75bd10c2023-06-27 11:34:08 +080010895 int res;
developer72fb0bb2023-01-11 09:46:29 +080010896
developera3511852023-06-14 14:12:59 +080010897 wifi_dbg_printf("\nFunc=%s\n",__func__);
10898 if (NULL == output_string)
10899 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010900
developer75bd10c2023-06-27 11:34:08 +080010901 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
10902 if (os_snprintf_error(sizeof(config_file), res)) {
10903 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10904 return RETURN_ERR;
10905 }
10906
developera3511852023-06-14 14:12:59 +080010907 wifi_hostapdRead(config_file,"wpa",buf,sizeof(buf));
10908 if(strcmp(buf,"0")==0)
10909 {
10910 printf("wpa_mode is %s ......... \n",buf);
10911 return RETURN_ERR;
10912 }
developer72fb0bb2023-01-11 09:46:29 +080010913
developera3511852023-06-14 14:12:59 +080010914 wifi_hostapdRead(config_file,"wpa_passphrase",output_string,64);
10915 wifi_dbg_printf("\noutput_string=%s\n",output_string);
developer72fb0bb2023-01-11 09:46:29 +080010916
developera3511852023-06-14 14:12:59 +080010917 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080010918}
10919
10920// sets the passphrase enviornment variable, max 63 characters
10921INT wifi_setApSecurityKeyPassphrase(INT apIndex, CHAR *passPhrase)
10922{
developera3511852023-06-14 14:12:59 +080010923 //save to wifi config and hotapd config. wait for wifi reset or hostapd restet to apply
10924 struct params params={'\0'};
10925 char config_file[MAX_BUF_SIZE] = {0};
10926 int ret;
developer72fb0bb2023-01-11 09:46:29 +080010927
developera3511852023-06-14 14:12:59 +080010928 if(NULL == passPhrase)
10929 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010930
developera3511852023-06-14 14:12:59 +080010931 if(strlen(passPhrase)<8 || strlen(passPhrase)>63)
10932 {
10933 wifi_dbg_printf("\nCannot Set Preshared Key length of preshared key should be 8 to 63 chars\n");
10934 return RETURN_ERR;
10935 }
10936 params.name = "wpa_passphrase";
10937 params.value = passPhrase;
developer32f2a182023-06-27 19:50:41 +080010938 ret = snprintf(config_file, sizeof(config_file),
10939 "%s%d.conf", CONFIG_PREFIX, apIndex);
10940 if (os_snprintf_error(sizeof(config_file), ret)) {
developer75bd10c2023-06-27 11:34:08 +080010941 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10942 return RETURN_ERR;
10943 }
developera3511852023-06-14 14:12:59 +080010944 ret=wifi_hostapdWrite(config_file,&params,1);
10945 if(!ret) {
10946 wifi_hostapdProcessUpdate(apIndex, &params, 1);
developere5750452023-05-15 16:46:42 +080010947 wifi_reloadAp(apIndex);
developera3511852023-06-14 14:12:59 +080010948 }
developer72fb0bb2023-01-11 09:46:29 +080010949
developera3511852023-06-14 14:12:59 +080010950 return ret;
developer72fb0bb2023-01-11 09:46:29 +080010951}
10952
10953//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.
10954INT wifi_setApSecurityReset(INT apIndex)
10955{
developera3511852023-06-14 14:12:59 +080010956 char original_config_file[64] = {0};
10957 char current_config_file[64] = {0};
10958 char buf[64] = {0};
10959 char cmd[64] = {0};
10960 char wpa[4] = {0};
10961 char wpa_psk[64] = {0};
10962 char wpa_passphrase[64] = {0};
10963 char wpa_psk_file[128] = {0};
10964 char wpa_key_mgmt[64] = {0};
10965 char wpa_pairwise[32] = {0};
10966 wifi_band band;
10967 struct params list[6];
developere40952c2023-06-15 18:46:43 +080010968 int res;
developer72fb0bb2023-01-11 09:46:29 +080010969
developera3511852023-06-14 14:12:59 +080010970 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080010971
developera3511852023-06-14 14:12:59 +080010972 band = wifi_index_to_band(apIndex);
developer37646972023-06-29 10:58:43 +080010973 if (band == band_2_4) {
10974 res = snprintf(original_config_file, sizeof(original_config_file),
10975 "/etc/hostapd-2G.conf");
10976 if (os_snprintf_error(sizeof(original_config_file), res)) {
10977 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10978 return RETURN_ERR;
10979 }
10980 } else if (band == band_5) {
10981 res = snprintf(original_config_file, sizeof(original_config_file),
10982 "/etc/hostapd-5G.conf");
10983 if (os_snprintf_error(sizeof(original_config_file), res)) {
10984 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10985 return RETURN_ERR;
10986 }
10987 } else if (band == band_6) {
10988 res = snprintf(original_config_file, sizeof(original_config_file),
10989 "/etc/hostapd-6G.conf");
10990 if (os_snprintf_error(sizeof(original_config_file), res)) {
10991 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
10992 return RETURN_ERR;
10993 }
10994 } else
developera3511852023-06-14 14:12:59 +080010995 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080010996
developera3511852023-06-14 14:12:59 +080010997 wifi_hostapdRead(original_config_file, "wpa", wpa, sizeof(wpa));
10998 list[0].name = "wpa";
10999 list[0].value = wpa;
developer69b61b02023-03-07 17:17:44 +080011000
developera3511852023-06-14 14:12:59 +080011001 wifi_hostapdRead(original_config_file, "wpa_psk", wpa_psk, sizeof(wpa_psk));
11002 list[1].name = "wpa_psk";
11003 list[1].value = wpa_psk;
developer72fb0bb2023-01-11 09:46:29 +080011004
developera3511852023-06-14 14:12:59 +080011005 wifi_hostapdRead(original_config_file, "wpa_passphrase", wpa_passphrase, sizeof(wpa_passphrase));
11006 list[2].name = "wpa_passphrase";
11007 list[2].value = wpa_passphrase;
developer72fb0bb2023-01-11 09:46:29 +080011008
developera3511852023-06-14 14:12:59 +080011009 wifi_hostapdRead(original_config_file, "wpa_psk_file", wpa_psk_file, sizeof(wpa_psk_file));
developer72fb0bb2023-01-11 09:46:29 +080011010
developera3511852023-06-14 14:12:59 +080011011 if (strlen(wpa_psk_file) == 0)
developer32f2a182023-06-27 19:50:41 +080011012 memcpy(wpa_psk_file, PSK_FILE, strlen(PSK_FILE));
developer72fb0bb2023-01-11 09:46:29 +080011013
developera3511852023-06-14 14:12:59 +080011014 if (access(wpa_psk_file, F_OK) != 0) {
developerd14dff12023-06-28 22:47:44 +080011015 res = snprintf(cmd, sizeof(cmd), "touch %s", wpa_psk_file);
developere40952c2023-06-15 18:46:43 +080011016 if (os_snprintf_error(sizeof(cmd), res)) {
11017 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11018 return RETURN_ERR;
11019 }
developera3511852023-06-14 14:12:59 +080011020 _syscmd(cmd, buf, sizeof(buf));
11021 }
11022 list[3].name = "wpa_psk_file";
11023 list[3].value = wpa_psk_file;
developer72fb0bb2023-01-11 09:46:29 +080011024
developera3511852023-06-14 14:12:59 +080011025 wifi_hostapdRead(original_config_file, "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt));
11026 list[4].name = "wpa_key_mgmt";
11027 list[4].value = wpa_key_mgmt;
developer72fb0bb2023-01-11 09:46:29 +080011028
developera3511852023-06-14 14:12:59 +080011029 wifi_hostapdRead(original_config_file, "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise));
11030 list[5].name = "wpa_pairwise";
11031 list[5].value = wpa_pairwise;
developer72fb0bb2023-01-11 09:46:29 +080011032
developer32f2a182023-06-27 19:50:41 +080011033 res = snprintf(current_config_file, sizeof(current_config_file),
11034 "%s%d.conf", CONFIG_PREFIX, apIndex);
11035 if (os_snprintf_error(sizeof(current_config_file), res)) {
11036 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11037 return RETURN_ERR;
11038 }
developera3511852023-06-14 14:12:59 +080011039 wifi_hostapdWrite(current_config_file, list, 6);
developer72fb0bb2023-01-11 09:46:29 +080011040
developera3511852023-06-14 14:12:59 +080011041 wifi_setApEnable(apIndex, FALSE);
11042 wifi_setApEnable(apIndex, TRUE);
developer72fb0bb2023-01-11 09:46:29 +080011043
developera3511852023-06-14 14:12:59 +080011044 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11045 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011046}
11047
11048//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).
11049INT wifi_getApSecurityRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11050{
developera3511852023-06-14 14:12:59 +080011051 char config_file[64] = {0};
11052 char buf[64] = {0};
11053 char cmd[256] = {0};
developere40952c2023-06-15 18:46:43 +080011054 int res;
developer72fb0bb2023-01-11 09:46:29 +080011055
developera3511852023-06-14 14:12:59 +080011056 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011057
developera3511852023-06-14 14:12:59 +080011058 if(!IP_output || !Port_output || !RadiusSecret_output)
11059 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011060
developera3511852023-06-14 14:12:59 +080011061 // Read the first matched config
developere40952c2023-06-15 18:46:43 +080011062 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11063 if (os_snprintf_error(sizeof(config_file), res)) {
11064 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11065 return RETURN_ERR;
11066 }
developer32f2a182023-06-27 19:50:41 +080011067 res = snprintf(cmd, sizeof(cmd),
11068 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
developer75bd10c2023-06-27 11:34:08 +080011069 if (os_snprintf_error(sizeof(cmd), res)) {
11070 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11071 return RETURN_ERR;
11072 }
developera3511852023-06-14 14:12:59 +080011073 _syscmd(cmd, buf, sizeof(buf));
11074 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011075
developera3511852023-06-14 14:12:59 +080011076 memset(buf, 0, sizeof(buf));
developer75bd10c2023-06-27 11:34:08 +080011077 res = snprintf(cmd, sizeof(cmd), "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11078 if (os_snprintf_error(sizeof(cmd), res)) {
11079 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11080 return RETURN_ERR;
11081 }
developera3511852023-06-14 14:12:59 +080011082 _syscmd(cmd, buf, sizeof(buf));
11083 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011084
developera3511852023-06-14 14:12:59 +080011085 memset(buf, 0, sizeof(buf));
developer75bd10c2023-06-27 11:34:08 +080011086 res = snprintf(cmd, sizeof(cmd), "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
11087 if (os_snprintf_error(sizeof(cmd), res)) {
11088 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11089 return RETURN_ERR;
11090 }
developera3511852023-06-14 14:12:59 +080011091 _syscmd(cmd, buf, sizeof(buf));
11092 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011093
developera3511852023-06-14 14:12:59 +080011094 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11095 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011096}
11097
11098INT wifi_setApSecurityRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11099{
developera3511852023-06-14 14:12:59 +080011100 char config_file[64] = {0};
11101 char port_str[8] = {0};
11102 char cmd[256] = {0};
11103 char buf[128] = {0};
11104 int res;
developer72fb0bb2023-01-11 09:46:29 +080011105
developera3511852023-06-14 14:12:59 +080011106 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011107
developere5750452023-05-15 16:46:42 +080011108 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011109 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011110
developera3511852023-06-14 14:12:59 +080011111 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11112 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011113
developera3511852023-06-14 14:12:59 +080011114 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11115 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011116 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11117 return RETURN_ERR;
11118 }
developer72fb0bb2023-01-11 09:46:29 +080011119
developera3511852023-06-14 14:12:59 +080011120 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 1'", config_file);
11121 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011122 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11123 return RETURN_ERR;
11124 }
developera3511852023-06-14 14:12:59 +080011125 _syscmd(cmd, buf, sizeof(buf));
11126 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011127
developere40952c2023-06-15 18:46:43 +080011128 res = snprintf(port_str, sizeof(port_str), "%d", port);
developer37646972023-06-29 10:58:43 +080011129 if (os_snprintf_error(sizeof(port_str), res)) {
11130 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11131 return RETURN_ERR;
11132 }
developera3511852023-06-14 14:12:59 +080011133 if (strlen(buf) == 0) {
11134 // Append
11135 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 1\\n"
11136 "auth_server_addr=%s\\n"
11137 "auth_server_port=%s\\n"
11138 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11139 if (os_snprintf_error(sizeof(cmd), res)) {
11140 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11141 return RETURN_ERR;
11142 }
11143 } else {
11144 // Delete the three lines setting after the "# radius 1" comment
11145 res = snprintf(cmd, sizeof(cmd), "sed -i '/# radius 1/{n;N;N;d}' %s", config_file);
11146 if (os_snprintf_error(sizeof(cmd), res)) {
11147 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11148 return RETURN_ERR;
11149 }
11150 _syscmd(cmd, buf, sizeof(buf));
11151 memset(cmd, 0, sizeof(cmd));
11152 // Use "# radius 1" comment to find the location to insert the radius setting
11153 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 1/"
11154 "# radius 1\\n"
11155 "auth_server_addr=%s\\n"
11156 "auth_server_port=%s\\n"
11157 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11158 if (os_snprintf_error(sizeof(cmd), res)) {
11159 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11160 return RETURN_ERR;
11161 }
11162 }
11163 if(_syscmd(cmd, buf, sizeof(buf))) {
11164 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11165 return RETURN_ERR;
11166 }
developer72fb0bb2023-01-11 09:46:29 +080011167
developera3511852023-06-14 14:12:59 +080011168 wifi_reloadAp(apIndex);
11169 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11170 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011171}
11172
11173INT wifi_getApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
11174{
developera3511852023-06-14 14:12:59 +080011175 char config_file[64] = {0};
11176 char buf[64] = {0};
11177 char cmd[256] = {0};
developere40952c2023-06-15 18:46:43 +080011178 int res;
developer72fb0bb2023-01-11 09:46:29 +080011179
developera3511852023-06-14 14:12:59 +080011180 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011181
developera3511852023-06-14 14:12:59 +080011182 if(!IP_output || !Port_output || !RadiusSecret_output)
11183 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011184
developera3511852023-06-14 14:12:59 +080011185 // Read the second matched config
developere40952c2023-06-15 18:46:43 +080011186 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11187 if (os_snprintf_error(sizeof(config_file), res)) {
11188 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11189 return RETURN_ERR;
11190 }
11191
developer32f2a182023-06-27 19:50:41 +080011192 res = snprintf(cmd, sizeof(cmd),
11193 "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11194 config_file);
developer75bd10c2023-06-27 11:34:08 +080011195 if (os_snprintf_error(sizeof(cmd), res)) {
11196 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11197 return RETURN_ERR;
11198 }
developera3511852023-06-14 14:12:59 +080011199 _syscmd(cmd, buf, sizeof(buf));
11200 strncpy(IP_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011201
developera3511852023-06-14 14:12:59 +080011202 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080011203 res = snprintf(cmd, sizeof(cmd),
11204 "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11205 config_file);
developer75bd10c2023-06-27 11:34:08 +080011206 if (os_snprintf_error(sizeof(cmd), res)) {
11207 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11208 return RETURN_ERR;
11209 }
developera3511852023-06-14 14:12:59 +080011210 _syscmd(cmd, buf, sizeof(buf));
11211 *Port_output = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011212
developera3511852023-06-14 14:12:59 +080011213 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080011214 res = snprintf(cmd, sizeof(cmd),
11215 "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"",
11216 config_file);
developer75bd10c2023-06-27 11:34:08 +080011217 if (os_snprintf_error(sizeof(cmd), res)) {
11218 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11219 return RETURN_ERR;
11220 }
11221
developera3511852023-06-14 14:12:59 +080011222 _syscmd(cmd, buf, sizeof(buf));
11223 strncpy(RadiusSecret_output, buf, 64);
developer72fb0bb2023-01-11 09:46:29 +080011224
developera3511852023-06-14 14:12:59 +080011225 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11226 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011227}
11228
11229INT wifi_setApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
11230{
developera3511852023-06-14 14:12:59 +080011231 char config_file[64] = {0};
11232 char port_str[8] = {0};
11233 char cmd[256] = {0};
11234 char buf[128] = {0};
11235 int res;
developer72fb0bb2023-01-11 09:46:29 +080011236
developera3511852023-06-14 14:12:59 +080011237 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011238
developere5750452023-05-15 16:46:42 +080011239 if (wifi_getApSecurityModeEnabled(apIndex, buf) != RETURN_OK)
developera3511852023-06-14 14:12:59 +080011240 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011241
developera3511852023-06-14 14:12:59 +080011242 if (strstr(buf, "Enterprise") == NULL) // non Enterprise mode sould not set radius server info
11243 return RETURN_ERR;
developere5750452023-05-15 16:46:42 +080011244
developera3511852023-06-14 14:12:59 +080011245 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11246 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080011247 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11248 return RETURN_ERR;
11249 }
developer72fb0bb2023-01-11 09:46:29 +080011250
developera3511852023-06-14 14:12:59 +080011251 res = snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 2'", config_file);
11252 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011253 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11254 return RETURN_ERR;
11255 }
developera3511852023-06-14 14:12:59 +080011256 _syscmd(cmd, buf, sizeof(buf));
11257 memset(cmd, 0, sizeof(cmd));
developer72fb0bb2023-01-11 09:46:29 +080011258
developera3511852023-06-14 14:12:59 +080011259 res = snprintf(port_str, sizeof(port_str), "%d", port);
11260 if (os_snprintf_error(sizeof(port_str), res)) {
developer46506162023-06-12 10:09:39 +080011261 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11262 return RETURN_ERR;
11263 }
developera3511852023-06-14 14:12:59 +080011264 if (strlen(buf) == 0) {
11265 // Append
11266 res = snprintf(cmd, sizeof(cmd), "echo -e '# radius 2\\n"
11267 "auth_server_addr=%s\\n"
11268 "auth_server_port=%s\\n"
11269 "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
11270 if (os_snprintf_error(sizeof(cmd), res)) {
11271 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11272 return RETURN_ERR;
11273 }
11274 } else {
11275 // Delete the three lines setting after the "# radius 2" comment
11276 res = snprintf(cmd, sizeof(cmd), "sed -i '/# radius 2/{n;N;N;d}' %s", config_file);
11277 if (os_snprintf_error(sizeof(cmd), res)) {
11278 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11279 return RETURN_ERR;
11280 }
11281 _syscmd(cmd, buf, sizeof(buf));
11282 memset(cmd, 0, sizeof(cmd));
11283 // Use "# radius 2" comment to find the location to insert the radius setting
11284 res = snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 2/"
11285 "# radius 2\\n"
11286 "auth_server_addr=%s\\n"
11287 "auth_server_port=%s\\n"
11288 "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
11289 if (os_snprintf_error(sizeof(cmd), res)) {
11290 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11291 return RETURN_ERR;
11292 }
11293 }
11294 if(_syscmd(cmd, buf, sizeof(buf))) {
11295 wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
11296 return RETURN_ERR;
11297 }
developer72fb0bb2023-01-11 09:46:29 +080011298
developera3511852023-06-14 14:12:59 +080011299 wifi_reloadAp(apIndex);
11300 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11301 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011302}
11303
11304//RadiusSettings
11305INT wifi_getApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *output)
11306{
developera3511852023-06-14 14:12:59 +080011307 if(!output)
11308 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011309
developera3511852023-06-14 14:12:59 +080011310 output->RadiusServerRetries = 3; //Number of retries for Radius requests.
11311 output->RadiusServerRequestTimeout = 5; //Radius request timeout in seconds after which the request must be retransmitted for the # of retries available.
11312 output->PMKLifetime = 28800; //Default time in seconds after which a Wi-Fi client is forced to ReAuthenticate (def 8 hrs).
11313 output->PMKCaching = FALSE; //Enable or disable caching of PMK.
11314 output->PMKCacheInterval = 300; //Time interval in seconds after which the PMKSA (Pairwise Master Key Security Association) cache is purged (def 5 minutes).
11315 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.
11316 output->BlacklistTableTimeout = 600; //Time interval in seconds for which a client will continue to be blacklisted once it is marked so.
11317 output->IdentityRequestRetryInterval = 5; //Time Interval in seconds between identity requests retries. A value of 0 (zero) disables it.
11318 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 +080011319 //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 +080011320
developera3511852023-06-14 14:12:59 +080011321 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011322}
11323
11324INT wifi_setApSecurityRadiusSettings(INT apIndex, wifi_radius_setting_t *input)
11325{
developera3511852023-06-14 14:12:59 +080011326 //store the paramters, and apply instantly
11327 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011328}
11329
11330//Device.WiFi.AccessPoint.{i}.WPS.Enable
11331//Enables or disables WPS functionality for this access point.
11332// outputs the WPS enable state of this ap in output_bool
11333INT wifi_getApWpsEnable(INT apIndex, BOOL *output_bool)
11334{
developera3511852023-06-14 14:12:59 +080011335 char interface_name[16] = {0};
11336 char buf[MAX_BUF_SIZE] = {0}, cmd[MAX_CMD_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080011337 int res;
11338
developera3511852023-06-14 14:12:59 +080011339 if(!output_bool)
11340 return RETURN_ERR;
11341 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11342 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080011343 res = snprintf(cmd, sizeof(cmd),
11344 "hostapd_cli -i %s get_config | grep wps_state | cut -d '=' -f2",
11345 interface_name);
developer75bd10c2023-06-27 11:34:08 +080011346 if (os_snprintf_error(sizeof(cmd), res)) {
11347 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11348 return RETURN_ERR;
11349 }
developera3511852023-06-14 14:12:59 +080011350 _syscmd(cmd, buf, sizeof(buf));
11351 if(strstr(buf, "configured"))
11352 *output_bool=TRUE;
11353 else
11354 *output_bool=FALSE;
developer72fb0bb2023-01-11 09:46:29 +080011355
developera3511852023-06-14 14:12:59 +080011356 return RETURN_OK;
developer69b61b02023-03-07 17:17:44 +080011357}
developer72fb0bb2023-01-11 09:46:29 +080011358
11359//Device.WiFi.AccessPoint.{i}.WPS.Enable
11360// sets the WPS enable enviornment variable for this ap to the value of enableValue, 1==enabled, 0==disabled
11361INT wifi_setApWpsEnable(INT apIndex, BOOL enable)
11362{
developera3511852023-06-14 14:12:59 +080011363 char config_file[MAX_BUF_SIZE] = {0};
11364 char buf[128] = {0};
11365 struct params params;
developere40952c2023-06-15 18:46:43 +080011366 int res;
developer72fb0bb2023-01-11 09:46:29 +080011367
developera3511852023-06-14 14:12:59 +080011368 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11369 //store the paramters, and wait for wifi up to apply
11370 params.name = "wps_state";
11371 if (enable == TRUE) {
11372 wifi_getApBeaconType(apIndex, buf);
11373 if (strncmp(buf, "None", 4) == 0) // If ap didn't set encryption
11374 params.value = "1";
11375 else // If ap set encryption
11376 params.value = "2";
11377 } else {
11378 params.value = "0";
11379 }
developer72fb0bb2023-01-11 09:46:29 +080011380
developere40952c2023-06-15 18:46:43 +080011381 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11382 if (os_snprintf_error(sizeof(config_file), res)) {
11383 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11384 return RETURN_ERR;
11385 }
developera3511852023-06-14 14:12:59 +080011386 wifi_hostapdWrite(config_file, &params, 1);
11387 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11388 wifi_reloadAp(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080011389
developera3511852023-06-14 14:12:59 +080011390 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11391 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011392}
11393
11394//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
11395INT wifi_getApWpsConfigMethodsSupported(INT apIndex, CHAR *output)
11396{
developere40952c2023-06-15 18:46:43 +080011397 int res;
developera3511852023-06-14 14:12:59 +080011398 if(!output)
11399 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011400 res = snprintf(output, 128, "PushButton,PIN");
11401 if (os_snprintf_error(128, res)) {
11402 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11403 return RETURN_ERR;
11404 }
11405
developera3511852023-06-14 14:12:59 +080011406 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011407}
11408
11409//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11410//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.
11411// Outputs a common separated list of the enabled WPS config methods, 64 bytes max
11412INT wifi_getApWpsConfigMethodsEnabled(INT apIndex, CHAR *output)
11413{
developere40952c2023-06-15 18:46:43 +080011414 int res;
developera3511852023-06-14 14:12:59 +080011415 if(!output)
11416 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011417 res = snprintf(output, 64, "PushButton,PIN");//Currently, supporting these two methods
11418 if (os_snprintf_error(64, res)) {
11419 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11420 return RETURN_ERR;
11421 }
developer72fb0bb2023-01-11 09:46:29 +080011422
developera3511852023-06-14 14:12:59 +080011423 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011424}
11425
11426//Device.WiFi.AccessPoint.{i}.WPS.ConfigMethodsEnabled
11427// 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
11428INT wifi_setApWpsConfigMethodsEnabled(INT apIndex, CHAR *methodString)
11429{
developera3511852023-06-14 14:12:59 +080011430 //apply instantly. No setting need to be stored.
11431 char methods[MAX_BUF_SIZE], *token, *next_token;
11432 char config_file[MAX_BUF_SIZE], config_methods[MAX_BUF_SIZE] = {0};
11433 struct params params;
developere40952c2023-06-15 18:46:43 +080011434 int res;
developer72fb0bb2023-01-11 09:46:29 +080011435
developera3511852023-06-14 14:12:59 +080011436 if(!methodString)
11437 return RETURN_ERR;
11438 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11439 //store the paramters, and wait for wifi up to apply
developer72fb0bb2023-01-11 09:46:29 +080011440
developere40952c2023-06-15 18:46:43 +080011441 res = snprintf(methods, sizeof(methods), "%s", methodString);
11442 if (os_snprintf_error(sizeof(methods), res)) {
11443 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11444 return RETURN_ERR;
11445 }
developera3511852023-06-14 14:12:59 +080011446 for(token=methods; *token; token=next_token) {
11447 strtok_r(token, ",", &next_token);
11448 if(*token=='U' && !strcmp(methods, "USBFlashDrive"))
developere40952c2023-06-15 18:46:43 +080011449 res = snprintf(config_methods, sizeof(config_methods), "%s ", "usba");
developera3511852023-06-14 14:12:59 +080011450 else if(*token=='E')
11451 {
11452 if(!strcmp(methods, "Ethernet"))
developere40952c2023-06-15 18:46:43 +080011453 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ethernet");
developera3511852023-06-14 14:12:59 +080011454 else if(!strcmp(methods, "ExternalNFCToken"))
developere40952c2023-06-15 18:46:43 +080011455 res = snprintf(config_methods, sizeof(config_methods), "%s ", "ext_nfc_token");
developera3511852023-06-14 14:12:59 +080011456 else
11457 printf("%s: Unknown WpsConfigMethod\n", __func__);
11458 }
11459 else if(*token=='I' && !strcmp(token, "IntegratedNFCToken"))
developere40952c2023-06-15 18:46:43 +080011460 res = snprintf(config_methods, sizeof(config_methods), "%s ", "int_nfc_token");
developera3511852023-06-14 14:12:59 +080011461 else if(*token=='N' && !strcmp(token, "NFCInterface"))
developere40952c2023-06-15 18:46:43 +080011462 res = snprintf(config_methods, sizeof(config_methods), "%s ", "nfc_interface");
developera3511852023-06-14 14:12:59 +080011463 else if(*token=='P' )
11464 {
11465 if(!strcmp(token, "PushButton"))
developere40952c2023-06-15 18:46:43 +080011466 res = snprintf(config_methods, sizeof(config_methods), "%s ", "push_button");
developera3511852023-06-14 14:12:59 +080011467 else if(!strcmp(token, "PIN"))
developere40952c2023-06-15 18:46:43 +080011468 res = snprintf(config_methods, sizeof(config_methods), "%s ", "keypad");
developera3511852023-06-14 14:12:59 +080011469 else
11470 printf("%s: Unknown WpsConfigMethod\n", __func__);
11471 }
11472 else
11473 printf("%s: Unknown WpsConfigMethod\n", __func__);
developere40952c2023-06-15 18:46:43 +080011474
developer37646972023-06-29 10:58:43 +080011475 if (os_snprintf_error(sizeof(config_methods), res)) {
11476 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11477 return RETURN_ERR;
11478 }
11479 }
developera3511852023-06-14 14:12:59 +080011480 params.name = "config_methods";
11481 params.value = config_methods;
developere40952c2023-06-15 18:46:43 +080011482 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11483 if (os_snprintf_error(sizeof(config_file), res)) {
11484 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11485 return RETURN_ERR;
11486 }
developera3511852023-06-14 14:12:59 +080011487 wifi_hostapdWrite(config_file, &params, 1);
11488 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11489 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011490
developera3511852023-06-14 14:12:59 +080011491 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011492}
11493
11494// outputs the pin value, ulong_pin must be allocated by the caller
11495INT wifi_getApWpsDevicePIN(INT apIndex, ULONG *output_ulong)
11496{
developera3511852023-06-14 14:12:59 +080011497 char buf[MAX_BUF_SIZE] = {0};
11498 char cmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080011499 int res;
developer72fb0bb2023-01-11 09:46:29 +080011500
developera3511852023-06-14 14:12:59 +080011501 if(!output_ulong)
11502 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011503 res = snprintf(cmd, sizeof(cmd), "cat %s%d.conf | grep ap_pin | cut -d '=' -f2", CONFIG_PREFIX, apIndex);
11504 if (os_snprintf_error(sizeof(cmd), res)) {
11505 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11506 return RETURN_ERR;
11507 }
developera3511852023-06-14 14:12:59 +080011508 _syscmd(cmd, buf, sizeof(buf));
developerd14dff12023-06-28 22:47:44 +080011509 if(strlen(buf) > 0) {
developerc14d83a2023-06-29 20:09:42 +080011510 if (hal_strtoul(buf, 10, output_ulong) < 0) {
11511 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080011512 }
11513 }
developer72fb0bb2023-01-11 09:46:29 +080011514
developera3511852023-06-14 14:12:59 +080011515 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011516}
11517
11518// set an enviornment variable for the WPS pin for the selected AP. Normally, Device PIN should not be changed.
11519INT wifi_setApWpsDevicePIN(INT apIndex, ULONG pin)
11520{
developera3511852023-06-14 14:12:59 +080011521 //set the pin to wifi config and hostpad config. wait for wifi reset or hostapd reset to apply
11522 char ap_pin[16] = {0};
11523 char config_file[MAX_BUF_SIZE] = {0};
11524 struct params params;
developere40952c2023-06-15 18:46:43 +080011525 int res;
developer72fb0bb2023-01-11 09:46:29 +080011526
developera3511852023-06-14 14:12:59 +080011527 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011528 res = snprintf(ap_pin, sizeof(ap_pin), "%lu", pin);
11529 if (os_snprintf_error(sizeof(ap_pin), res)) {
11530 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11531 return RETURN_ERR;
11532 }
developera3511852023-06-14 14:12:59 +080011533 params.name = "ap_pin";
11534 params.value = ap_pin;
developere40952c2023-06-15 18:46:43 +080011535 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
11536 if (os_snprintf_error(sizeof(config_file), res)) {
11537 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11538 return RETURN_ERR;
11539 }
developera3511852023-06-14 14:12:59 +080011540 wifi_hostapdWrite(config_file, &params, 1);
11541 wifi_hostapdProcessUpdate(apIndex, &params, 1);
11542 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011543
developera3511852023-06-14 14:12:59 +080011544 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011545}
11546
11547// Output string is either Not configured or Configured, max 32 characters
11548INT wifi_getApWpsConfigurationState(INT apIndex, CHAR *output_string)
11549{
developera3511852023-06-14 14:12:59 +080011550 char interface_name[16] = {0};
11551 char cmd[MAX_CMD_SIZE];
11552 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011553 int res;
developer72fb0bb2023-01-11 09:46:29 +080011554
developera3511852023-06-14 14:12:59 +080011555 if(!output_string)
11556 return RETURN_ERR;
11557 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080011558 res = snprintf(output_string, 32, "Not configured");
11559 if (os_snprintf_error(32, res)) {
11560 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11561 return RETURN_ERR;
11562 }
developera3511852023-06-14 14:12:59 +080011563 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11564 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011565 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep wps_state | cut -d'=' -f2", interface_name);
11566 if (os_snprintf_error(sizeof(cmd), res)) {
11567 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11568 return RETURN_ERR;
11569 }
developera3511852023-06-14 14:12:59 +080011570 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011571
developerc14d83a2023-06-29 20:09:42 +080011572 if(!strncmp(buf, "configured", 10)) {
developere40952c2023-06-15 18:46:43 +080011573 res = snprintf(output_string, 32, "Configured");
developerc14d83a2023-06-29 20:09:42 +080011574 if (os_snprintf_error(32, res)) {
11575 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11576 return RETURN_ERR;
11577 }
11578 }
developera3511852023-06-14 14:12:59 +080011579 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080011580
developera3511852023-06-14 14:12:59 +080011581 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011582}
11583
11584// sets the WPS pin for this AP
11585INT wifi_setApWpsEnrolleePin(INT apIndex, CHAR *pin)
11586{
developera3511852023-06-14 14:12:59 +080011587 char interface_name[16] = {0};
11588 char cmd[MAX_CMD_SIZE];
11589 char buf[MAX_BUF_SIZE]={0};
developer9ce44382023-06-28 11:09:37 +080011590 BOOL enable = 0;
developere40952c2023-06-15 18:46:43 +080011591 int res;
developer72fb0bb2023-01-11 09:46:29 +080011592
developera3511852023-06-14 14:12:59 +080011593 wifi_getApEnable(apIndex, &enable);
11594 if (!enable)
11595 return RETURN_ERR;
11596 wifi_getApWpsEnable(apIndex, &enable);
11597 if (!enable)
11598 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011599
developera3511852023-06-14 14:12:59 +080011600 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11601 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011602 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_pin any %s", interface_name, pin);
11603 if (os_snprintf_error(sizeof(cmd), res)) {
11604 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11605 return RETURN_ERR;
11606 }
developera3511852023-06-14 14:12:59 +080011607 _syscmd(cmd, buf, sizeof(buf));
11608 if((strstr(buf, "OK"))!=NULL)
11609 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011610
developera3511852023-06-14 14:12:59 +080011611 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011612}
11613
11614// This function is called when the WPS push button has been pressed for this AP
11615INT wifi_setApWpsButtonPush(INT apIndex)
11616{
developera3511852023-06-14 14:12:59 +080011617 char cmd[MAX_CMD_SIZE];
11618 char buf[MAX_BUF_SIZE]={0};
11619 char interface_name[16] = {0};
11620 BOOL enable=FALSE;
developere40952c2023-06-15 18:46:43 +080011621 int res;
developer72fb0bb2023-01-11 09:46:29 +080011622
developera3511852023-06-14 14:12:59 +080011623 wifi_getApEnable(apIndex, &enable);
11624 if (!enable)
11625 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011626
developera3511852023-06-14 14:12:59 +080011627 wifi_getApWpsEnable(apIndex, &enable);
11628 if (!enable)
11629 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011630
developera3511852023-06-14 14:12:59 +080011631 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11632 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011633
developere40952c2023-06-15 18:46:43 +080011634 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_cancel; hostapd_cli -i%s wps_pbc", interface_name, interface_name);
11635 if (os_snprintf_error(sizeof(cmd), res)) {
11636 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11637 return RETURN_ERR;
11638 }
developera3511852023-06-14 14:12:59 +080011639 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011640
developera3511852023-06-14 14:12:59 +080011641 if((strstr(buf, "OK"))!=NULL)
11642 return RETURN_OK;
11643 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011644}
11645
11646// cancels WPS mode for this AP
11647INT wifi_cancelApWPS(INT apIndex)
11648{
developera3511852023-06-14 14:12:59 +080011649 char interface_name[16] = {0};
11650 char cmd[MAX_CMD_SIZE];
11651 char buf[MAX_BUF_SIZE]={0};
developere40952c2023-06-15 18:46:43 +080011652 int res;
developer72fb0bb2023-01-11 09:46:29 +080011653
developera3511852023-06-14 14:12:59 +080011654 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11655 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080011656 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s wps_cancel", interface_name);
11657 if (os_snprintf_error(sizeof(cmd), res)) {
11658 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11659 return RETURN_ERR;
11660 }
developera3511852023-06-14 14:12:59 +080011661 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080011662
developera3511852023-06-14 14:12:59 +080011663 if((strstr(buf, "OK"))!=NULL)
11664 return RETURN_OK;
11665 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080011666}
11667
11668//Device.WiFi.AccessPoint.{i}.AssociatedDevice.*
11669//HAL funciton should allocate an data structure array, and return to caller with "associated_dev_array"
11670INT wifi_getApAssociatedDeviceDiagnosticResult(INT apIndex, wifi_associated_dev_t **associated_dev_array, UINT *output_array_size)
11671{
developera3511852023-06-14 14:12:59 +080011672 char interface_name[16] = {0};
11673 FILE *f = NULL;
11674 int read_flag=0, auth_temp=0, mac_temp=0;
11675 char cmd[256] = {0}, buf[2048] = {0};
11676 char *param = NULL, *value = NULL, *line=NULL;
11677 size_t len = 0;
11678 wifi_associated_dev_t *dev=NULL;
11679 int res;
developer72fb0bb2023-01-11 09:46:29 +080011680
developera3511852023-06-14 14:12:59 +080011681 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11682 *associated_dev_array = NULL;
11683 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
11684 return RETURN_ERR;
11685 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
11686 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011687 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11688 return RETURN_ERR;
11689 }
developera3511852023-06-14 14:12:59 +080011690 _syscmd(cmd,buf,sizeof(buf));
11691 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080011692
developera3511852023-06-14 14:12:59 +080011693 if (*output_array_size <= 0)
11694 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011695
developera3511852023-06-14 14:12:59 +080011696 dev=(wifi_associated_dev_t *) calloc (*output_array_size, sizeof(wifi_associated_dev_t));
developere75ba632023-06-29 16:03:33 +080011697 if (!dev) {
11698 wifi_debug(DEBUG_ERROR, "Unexpected calloc fail\n");
11699 return RETURN_ERR;
11700 }
developera3511852023-06-14 14:12:59 +080011701 *associated_dev_array = dev;
11702 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta > /tmp/connected_devices.txt" , interface_name);
11703 if (os_snprintf_error(sizeof(cmd), res)) {
developer46506162023-06-12 10:09:39 +080011704 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
11705 return RETURN_ERR;
11706 }
developera3511852023-06-14 14:12:59 +080011707 _syscmd(cmd,buf,sizeof(buf));
11708 f = fopen("/tmp/connected_devices.txt", "r");
11709 if (f==NULL)
11710 {
11711 *output_array_size=0;
11712 return RETURN_ERR;
11713 }
11714 while ((getline(&line, &len, f)) != -1)
11715 {
11716 param = strtok(line,"=");
developere75ba632023-06-29 16:03:33 +080011717 if (!param)
11718 continue;
developera3511852023-06-14 14:12:59 +080011719 value = strtok(NULL,"=");
developere75ba632023-06-29 16:03:33 +080011720 if (!value)
11721 continue;
developer72fb0bb2023-01-11 09:46:29 +080011722
developera3511852023-06-14 14:12:59 +080011723 if( strcmp("flags",param) == 0 )
11724 {
11725 value[strlen(value)-1]='\0';
11726 if(strstr (value,"AUTHORIZED") != NULL )
11727 {
11728 dev[auth_temp].cli_AuthenticationState = 1;
11729 dev[auth_temp].cli_Active = 1;
11730 auth_temp++;
11731 read_flag=1;
11732 }
11733 }
11734 if(read_flag==1)
11735 {
11736 if( strcmp("dot11RSNAStatsSTAAddress",param) == 0 )
11737 {
11738 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080011739 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
developera3511852023-06-14 14:12:59 +080011740 (unsigned int *)&dev[mac_temp].cli_MACAddress[0],
11741 (unsigned int *)&dev[mac_temp].cli_MACAddress[1],
11742 (unsigned int *)&dev[mac_temp].cli_MACAddress[2],
11743 (unsigned int *)&dev[mac_temp].cli_MACAddress[3],
11744 (unsigned int *)&dev[mac_temp].cli_MACAddress[4],
developere75ba632023-06-29 16:03:33 +080011745 (unsigned int *)&dev[mac_temp].cli_MACAddress[5] ) == EOF)
11746 continue;
developera3511852023-06-14 14:12:59 +080011747 mac_temp++;
11748 read_flag=0;
11749 }
11750 }
11751 }
11752 *output_array_size = auth_temp;
11753 auth_temp=0;
11754 mac_temp=0;
11755 free(line);
developere75ba632023-06-29 16:03:33 +080011756 if (fclose(f) == EOF) {
11757 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
11758 return RETURN_ERR;
11759 }
developera3511852023-06-14 14:12:59 +080011760 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
11761 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080011762}
11763
11764#define MACADDRESS_SIZE 6
11765
11766INT wifihal_AssociatedDevicesstats3(INT apIndex,CHAR *interface_name,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
11767{
developera3511852023-06-14 14:12:59 +080011768 FILE *fp = NULL;
11769 char str[MAX_BUF_SIZE] = {0};
11770 int wificlientindex = 0 ;
11771 int count = 0;
11772 int signalstrength = 0;
11773 int arr[MACADDRESS_SIZE] = {0};
11774 unsigned char mac[MACADDRESS_SIZE] = {0};
11775 UINT wifi_count = 0;
developere40952c2023-06-15 18:46:43 +080011776 int res;
developerc14d83a2023-06-29 20:09:42 +080011777 wifi_associated_dev3_t* temp = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011778
developera3511852023-06-14 14:12:59 +080011779 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
11780 *output_array_size = 0;
11781 *associated_dev_array = NULL;
developer72fb0bb2023-01-11 09:46:29 +080011782
developer33f13ba2023-07-12 16:19:06 +080011783 fp = v_secure_popen("r", "iw dev %s station dump | grep %s | wc -l",
11784 interface_name, interface_name);
developera3511852023-06-14 14:12:59 +080011785 if (fp == NULL)
11786 {
11787 printf("Failed to run command inside function %s\n",__FUNCTION__ );
11788 return RETURN_ERR;
11789 }
developer72fb0bb2023-01-11 09:46:29 +080011790
developera3511852023-06-14 14:12:59 +080011791 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080011792 if (fgets(str, sizeof(str)-1, fp) == NULL) {
11793 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080011794 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011795 return RETURN_ERR;
11796 }
developera3511852023-06-14 14:12:59 +080011797 wifi_count = (unsigned int) atoi ( str );
11798 *output_array_size = wifi_count;
11799 printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
developer33f13ba2023-07-12 16:19:06 +080011800 v_secure_pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080011801
developera3511852023-06-14 14:12:59 +080011802 if(wifi_count == 0)
11803 {
11804 return RETURN_OK;
11805 }
11806 else
11807 {
developer9ce44382023-06-28 11:09:37 +080011808 if(wifi_count <= 0 || wifi_count > MAX_ASSOCIATED_STA_NUM){
11809 return RETURN_ERR;
11810 }
developera3511852023-06-14 14:12:59 +080011811 temp = (wifi_associated_dev3_t*)calloc(1, sizeof(wifi_associated_dev3_t)*wifi_count) ;
11812 if(temp == NULL)
11813 {
11814 printf("Error Statement. Insufficient memory \n");
11815 return RETURN_ERR;
11816 }
developer72fb0bb2023-01-11 09:46:29 +080011817
developer33f13ba2023-07-12 16:19:06 +080011818 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
11819 if (res) {
11820 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011821 free(temp);
developere40952c2023-06-15 18:46:43 +080011822 return RETURN_ERR;
11823 }
developer33f13ba2023-07-12 16:19:06 +080011824
developera3511852023-06-14 14:12:59 +080011825 if(apIndex == 0)
developer33f13ba2023-07-12 16:19:06 +080011826 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_2G.txt", interface_name);
developera3511852023-06-14 14:12:59 +080011827 else if(apIndex == 1)
developer33f13ba2023-07-12 16:19:06 +080011828 res = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", interface_name);
11829 if (res) {
11830 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer9ce44382023-06-28 11:09:37 +080011831 free(temp);
developere40952c2023-06-15 18:46:43 +080011832 return RETURN_ERR;
11833 }
developer72fb0bb2023-01-11 09:46:29 +080011834
developera3511852023-06-14 14:12:59 +080011835 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
11836 if(fp == NULL)
11837 {
11838 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
11839 free(temp);
11840 return RETURN_ERR;
11841 }
developere75ba632023-06-29 16:03:33 +080011842 if (fclose(fp) == EOF) {
11843 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developerc14d83a2023-06-29 20:09:42 +080011844 free(temp);
developere75ba632023-06-29 16:03:33 +080011845 return RETURN_ERR;
11846 }
developer72fb0bb2023-01-11 09:46:29 +080011847
developer33f13ba2023-07-12 16:19:06 +080011848 fp = v_secure_popen("r", "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
developera3511852023-06-14 14:12:59 +080011849 if(fp)
11850 {
11851 for(count =0 ; count < wifi_count; count++)
11852 {
developer86035662023-06-28 19:21:12 +080011853 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11854 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011855 goto err;
developer86035662023-06-28 19:21:12 +080011856 }
developera3511852023-06-14 14:12:59 +080011857 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
11858 {
11859 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
11860 {
11861 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080011862
developera3511852023-06-14 14:12:59 +080011863 }
11864 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
11865 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]);
11866 }
11867 temp[count].cli_AuthenticationState = 1; //TODO
11868 temp[count].cli_Active = 1; //TODO
11869 }
developer33f13ba2023-07-12 16:19:06 +080011870 v_secure_pclose(fp);
developer86035662023-06-28 19:21:12 +080011871 }
11872
developer33f13ba2023-07-12 16:19:06 +080011873 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 +080011874 if(fp)
11875 {
developer33f13ba2023-07-12 16:19:06 +080011876 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011877 }
developer33f13ba2023-07-12 16:19:06 +080011878 fp = v_secure_popen("r", "cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011879 if(fp)
11880 {
11881 for(count =0 ; count < wifi_count ;count++)
11882 {
developer86035662023-06-28 19:21:12 +080011883 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11884 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011885 goto err;
developer86035662023-06-28 19:21:12 +080011886 }
developera3511852023-06-14 14:12:59 +080011887 signalstrength = atoi(str);
11888 temp[count].cli_SignalStrength = signalstrength;
11889 temp[count].cli_RSSI = signalstrength;
11890 temp[count].cli_SNR = signalstrength + 95;
11891 }
developer33f13ba2023-07-12 16:19:06 +080011892 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011893 }
developer72fb0bb2023-01-11 09:46:29 +080011894
11895
developera3511852023-06-14 14:12:59 +080011896 if((apIndex == 0) || (apIndex == 4))
11897 {
11898 for(count =0 ; count < wifi_count ;count++)
11899 {
developer32f2a182023-06-27 19:50:41 +080011900 memcpy(temp[count].cli_OperatingStandard,"g", 1);
11901 temp[count].cli_OperatingStandard[1] = '\0';
11902 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
11903 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080011904 }
developer72fb0bb2023-01-11 09:46:29 +080011905
developera3511852023-06-14 14:12:59 +080011906 //BytesSent
developer33f13ba2023-07-12 16:19:06 +080011907 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 +080011908 if(fp)
11909 {
developer33f13ba2023-07-12 16:19:06 +080011910 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011911 }
developer33f13ba2023-07-12 16:19:06 +080011912 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011913 if(fp)
11914 {
11915 for (count = 0; count < wifi_count; count++)
11916 {
developer86035662023-06-28 19:21:12 +080011917 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11918 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developer33f13ba2023-07-12 16:19:06 +080011919 v_secure_pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080011920 free(temp);
developer86035662023-06-28 19:21:12 +080011921 return RETURN_ERR;
developerc14d83a2023-06-29 20:09:42 +080011922 }
11923 if (hal_strtoul(str, 10, &(temp[count].cli_BytesSent)) < 0) {
11924 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011925 }
developera3511852023-06-14 14:12:59 +080011926 }
developer33f13ba2023-07-12 16:19:06 +080011927 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011928 }
developer72fb0bb2023-01-11 09:46:29 +080011929
developera3511852023-06-14 14:12:59 +080011930 //BytesReceived
developer86035662023-06-28 19:21:12 +080011931
developer33f13ba2023-07-12 16:19:06 +080011932 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 +080011933 if (fp)
11934 {
developer33f13ba2023-07-12 16:19:06 +080011935 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011936 }
developer33f13ba2023-07-12 16:19:06 +080011937 fp = v_secure_popen("r", "cat /tmp/Ass_Bytes_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011938 if (fp)
11939 {
11940 for (count = 0; count < wifi_count; count++)
11941 {
developer86035662023-06-28 19:21:12 +080011942 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11943 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011944 goto err;
11945 }
11946 if (hal_strtoul(str, 10, &(temp[count].cli_BytesReceived)) < 0) {
11947 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011948 }
developera3511852023-06-14 14:12:59 +080011949 }
developer33f13ba2023-07-12 16:19:06 +080011950 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011951 }
developer72fb0bb2023-01-11 09:46:29 +080011952
developera3511852023-06-14 14:12:59 +080011953 //PacketsSent
developer33f13ba2023-07-12 16:19:06 +080011954 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 +080011955 if (fp)
11956 {
developer33f13ba2023-07-12 16:19:06 +080011957 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011958 }
developer72fb0bb2023-01-11 09:46:29 +080011959
developer33f13ba2023-07-12 16:19:06 +080011960 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011961 if (fp)
11962 {
11963 for (count = 0; count < wifi_count; count++)
11964 {
developer86035662023-06-28 19:21:12 +080011965 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11966 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011967 goto err;
11968 }
11969 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsSent)) < 0) {
11970 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011971 }
developera3511852023-06-14 14:12:59 +080011972 }
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
developera3511852023-06-14 14:12:59 +080011976 //PacketsReceived
developer33f13ba2023-07-12 16:19:06 +080011977 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 +080011978 if (fp)
11979 {
developer33f13ba2023-07-12 16:19:06 +080011980 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011981 }
developer33f13ba2023-07-12 16:19:06 +080011982 fp = v_secure_popen("r", "cat /tmp/Ass_Packets_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080011983 if (fp)
11984 {
11985 for (count = 0; count < wifi_count; count++)
11986 {
developer86035662023-06-28 19:21:12 +080011987 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
11988 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080011989 goto err;
11990 }
11991 if (hal_strtoul(str, 10, &(temp[count].cli_PacketsReceived)) < 0) {
11992 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080011993 }
developera3511852023-06-14 14:12:59 +080011994 }
developer33f13ba2023-07-12 16:19:06 +080011995 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080011996 }
developer72fb0bb2023-01-11 09:46:29 +080011997
developera3511852023-06-14 14:12:59 +080011998 //ErrorsSent
developer33f13ba2023-07-12 16:19:06 +080011999 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 +080012000 if (fp)
12001 {
developer33f13ba2023-07-12 16:19:06 +080012002 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012003 }
developer33f13ba2023-07-12 16:19:06 +080012004 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012005 if (fp)
12006 {
12007 for (count = 0; count < wifi_count; count++)
12008 {
developer86035662023-06-28 19:21:12 +080012009 if (fgets(str, MAX_BUF_SIZE, fp) == NULL){
12010 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012011 goto err;
12012 }
12013 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
12014 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012015 }
developera3511852023-06-14 14:12:59 +080012016 }
developer33f13ba2023-07-12 16:19:06 +080012017 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012018 }
developer72fb0bb2023-01-11 09:46:29 +080012019
developera3511852023-06-14 14:12:59 +080012020 //ErrorsSent
developer86035662023-06-28 19:21:12 +080012021
developer33f13ba2023-07-12 16:19:06 +080012022 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 +080012023 if (fp)
12024 {
developer33f13ba2023-07-12 16:19:06 +080012025 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012026 }
developer33f13ba2023-07-12 16:19:06 +080012027 fp = v_secure_popen("r", "cat /tmp/Ass_Tx_Failed.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012028 if (fp)
12029 {
12030 for (count = 0; count < wifi_count; count++)
12031 {
developer86035662023-06-28 19:21:12 +080012032 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12033 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012034 goto err;
12035 }
12036 if (hal_strtoul(str, 10, &(temp[count].cli_ErrorsSent)) < 0) {
12037 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012038 }
developera3511852023-06-14 14:12:59 +080012039 }
developer33f13ba2023-07-12 16:19:06 +080012040 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012041 }
developer72fb0bb2023-01-11 09:46:29 +080012042
developera3511852023-06-14 14:12:59 +080012043 //LastDataDownlinkRate
developer33f13ba2023-07-12 16:19:06 +080012044 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 +080012045 if (fp)
12046 {
developer33f13ba2023-07-12 16:19:06 +080012047 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012048 }
developer33f13ba2023-07-12 16:19:06 +080012049 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012050 if (fp)
developerc14d83a2023-06-29 20:09:42 +080012051 {
12052 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012053 for (count = 0; count < wifi_count; count++)
12054 {
developer86035662023-06-28 19:21:12 +080012055 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12056 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012057 goto err;
12058 }
12059
12060 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12061 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012062 }
developerc14d83a2023-06-29 20:09:42 +080012063 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080012064 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
12065 }
developer33f13ba2023-07-12 16:19:06 +080012066 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012067 }
developer72fb0bb2023-01-11 09:46:29 +080012068
developera3511852023-06-14 14:12:59 +080012069 //LastDataUplinkRate
developer33f13ba2023-07-12 16:19:06 +080012070 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 +080012071 if (fp)
12072 {
developer33f13ba2023-07-12 16:19:06 +080012073 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012074 }
developer33f13ba2023-07-12 16:19:06 +080012075 fp = v_secure_popen("r", "cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2");
developera3511852023-06-14 14:12:59 +080012076 if (fp)
12077 {
developerc14d83a2023-06-29 20:09:42 +080012078 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080012079 for (count = 0; count < wifi_count; count++)
12080 {
developer86035662023-06-28 19:21:12 +080012081 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
12082 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080012083 goto err;
12084 }
12085 if (hal_strtoul(str, 10, &tmp_u) < 0) {
12086 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer86035662023-06-28 19:21:12 +080012087 }
developerc14d83a2023-06-29 20:09:42 +080012088 temp[count].cli_LastDataUplinkRate = tmp_u;
12089
developera3511852023-06-14 14:12:59 +080012090 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
12091 }
developer33f13ba2023-07-12 16:19:06 +080012092 v_secure_pclose(fp);
developera3511852023-06-14 14:12:59 +080012093 }
developer72fb0bb2023-01-11 09:46:29 +080012094
developera3511852023-06-14 14:12:59 +080012095 }
12096 else if ((apIndex == 1) || (apIndex == 5))
12097 {
12098 for (count = 0; count < wifi_count; count++)
12099 {
developer32f2a182023-06-27 19:50:41 +080012100 memcpy(temp[count].cli_OperatingStandard, "a", 1);
12101 temp[count].cli_OperatingStandard[1] = '\0';
12102 memcpy(temp[count].cli_OperatingChannelBandwidth, "20MHz", 5);
12103 temp[count].cli_OperatingChannelBandwidth[5] = '\0';
developera3511852023-06-14 14:12:59 +080012104 temp[count].cli_BytesSent = 0;
12105 temp[count].cli_BytesReceived = 0;
12106 temp[count].cli_LastDataUplinkRate = 0;
12107 temp[count].cli_LastDataDownlinkRate = 0;
12108 temp[count].cli_PacketsSent = 0;
12109 temp[count].cli_PacketsReceived = 0;
12110 temp[count].cli_ErrorsSent = 0;
12111 }
12112 }
developer72fb0bb2023-01-11 09:46:29 +080012113
developera3511852023-06-14 14:12:59 +080012114 for (count = 0; count < wifi_count; count++)
12115 {
12116 temp[count].cli_Retransmissions = 0;
12117 temp[count].cli_DataFramesSentAck = 0;
12118 temp[count].cli_DataFramesSentNoAck = 0;
12119 temp[count].cli_MinRSSI = 0;
12120 temp[count].cli_MaxRSSI = 0;
12121 strncpy(temp[count].cli_InterferenceSources, "", 64);
12122 memset(temp[count].cli_IPAddress, 0, 64);
12123 temp[count].cli_RetransCount = 0;
12124 temp[count].cli_FailedRetransCount = 0;
12125 temp[count].cli_RetryCount = 0;
12126 temp[count].cli_MultipleRetryCount = 0;
12127 }
12128 *associated_dev_array = temp;
12129 }
12130 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12131 return RETURN_OK;
developerc14d83a2023-06-29 20:09:42 +080012132err:
12133 if (temp)
12134 free(temp);
developer12fb9f62023-06-30 15:26:27 +080012135 pclose(fp);
developerc14d83a2023-06-29 20:09:42 +080012136 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012137}
12138
developer7e4a2a62023-04-06 19:56:03 +080012139int wifihal_interfacestatus(CHAR *wifi_status, CHAR *interface_name)
developer72fb0bb2023-01-11 09:46:29 +080012140{
developera3511852023-06-14 14:12:59 +080012141 char cmd[MAX_CMD_SIZE] = {0};
developer7e4a2a62023-04-06 19:56:03 +080012142 char buf[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080012143 int res;
developer32f2a182023-06-27 19:50:41 +080012144 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012145
developera3511852023-06-14 14:12:59 +080012146 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080012147
developere40952c2023-06-15 18:46:43 +080012148 res = snprintf(cmd, MAX_CMD_SIZE, "ifconfig %s | grep RUNNING | tr -s ' ' | cut -d ' ' -f4 | tr -d '\\n'",
developer7e4a2a62023-04-06 19:56:03 +080012149 interface_name);
developere40952c2023-06-15 18:46:43 +080012150 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
12151 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12152 return RETURN_ERR;
12153 }
12154
developera3511852023-06-14 14:12:59 +080012155 _syscmd(cmd, buf, MAX_BUF_SIZE);
developer7e4a2a62023-04-06 19:56:03 +080012156
developer32f2a182023-06-27 19:50:41 +080012157 len = strlen(buf);
12158 if (len >= sizeof(buf)) {
12159 wifi_debug(DEBUG_ERROR, "Unexpected buf size\n");
12160 return RETURN_ERR;
12161 }
12162 strncpy(wifi_status, buf, len); /* TBD: check wifi_status mem lenth and replace with strcpy later */
12163 wifi_status[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012164
developera3511852023-06-14 14:12:59 +080012165 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12166 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012167}
12168
developer72fb0bb2023-01-11 09:46:29 +080012169static const char *get_line_from_str_buf(const char *buf, char *line)
12170{
developera3511852023-06-14 14:12:59 +080012171 int i;
12172 int n = strlen(buf);
developer72fb0bb2023-01-11 09:46:29 +080012173
developera3511852023-06-14 14:12:59 +080012174 for (i = 0; i < n; i++) {
12175 line[i] = buf[i];
12176 if (buf[i] == '\n') {
12177 line[i] = '\0';
12178 return &buf[i + 1];
12179 }
12180 }
developer72fb0bb2023-01-11 09:46:29 +080012181
developera3511852023-06-14 14:12:59 +080012182 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080012183}
12184
12185INT wifi_getApAssociatedDeviceDiagnosticResult3(INT apIndex, wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12186{
developera3511852023-06-14 14:12:59 +080012187 char interface_name[16] = {0};
12188 FILE *f = NULL;
12189 int auth_temp= -1;
12190 char cmd[256] = {0}, buf[2048] = {0};
12191 char *param = NULL, *value = NULL, *line=NULL;
12192 size_t len = 0;
12193 wifi_associated_dev3_t *dev=NULL;
developer75bd10c2023-06-27 11:34:08 +080012194 int res;
developer72fb0bb2023-01-11 09:46:29 +080012195
developera3511852023-06-14 14:12:59 +080012196 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12197 *associated_dev_array = NULL;
12198 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12199 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080012200
12201 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s all_sta | grep AUTHORIZED | wc -l", interface_name);
12202 if (os_snprintf_error(sizeof(cmd), res)) {
12203 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12204 return RETURN_ERR;
12205 }
12206
developera3511852023-06-14 14:12:59 +080012207 _syscmd(cmd, buf, sizeof(buf));
12208 *output_array_size = atoi(buf);
developer72fb0bb2023-01-11 09:46:29 +080012209
developera3511852023-06-14 14:12:59 +080012210 if (*output_array_size <= 0)
12211 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012212
developera3511852023-06-14 14:12:59 +080012213 dev=(wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
developer86035662023-06-28 19:21:12 +080012214
12215 if (dev == NULL) {
12216 wifi_debug(DEBUG_ERROR, "calloc fail\n");
12217 return RETURN_ERR;
12218 }
developera3511852023-06-14 14:12:59 +080012219 *associated_dev_array = dev;
developer32f2a182023-06-27 19:50:41 +080012220 res = snprintf(cmd, sizeof(cmd),
12221 "hostapd_cli -i%s all_sta > /tmp/diagnostic3_devices.txt" , interface_name);
developer75bd10c2023-06-27 11:34:08 +080012222 if (os_snprintf_error(sizeof(cmd), res)) {
12223 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12224 return RETURN_ERR;
12225 }
12226
developera3511852023-06-14 14:12:59 +080012227 _syscmd(cmd,buf,sizeof(buf));
12228 f = fopen("/tmp/diagnostic3_devices.txt", "r");
12229 if (f == NULL)
12230 {
12231 *output_array_size=0;
12232 return RETURN_ERR;
12233 }
12234 while ((getline(&line, &len, f)) != -1)
12235 {
12236 param = strtok(line, "=");
developere75ba632023-06-29 16:03:33 +080012237 if (!param)
12238 continue;
developera3511852023-06-14 14:12:59 +080012239 value = strtok(NULL, "=");
developere75ba632023-06-29 16:03:33 +080012240 if (!value)
12241 continue;
developer72fb0bb2023-01-11 09:46:29 +080012242
developera3511852023-06-14 14:12:59 +080012243 if( strcmp("flags",param) == 0 )
12244 {
12245 value[strlen(value)-1]='\0';
12246 if(strstr (value,"AUTHORIZED") != NULL )
12247 {
12248 auth_temp++;
12249 dev[auth_temp].cli_AuthenticationState = 1;
12250 dev[auth_temp].cli_Active = 1;
12251 }
12252 } else if (auth_temp < 0) {
12253 continue;
12254 } else if( strcmp("dot11RSNAStatsSTAAddress", param) == 0 )
12255 {
12256 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012257 if (sscanf(value, "%x:%x:%x:%x:%x:%x",
12258 (unsigned int *)&dev[auth_temp].cli_MACAddress[0],
12259 (unsigned int *)&dev[auth_temp].cli_MACAddress[1],
12260 (unsigned int *)&dev[auth_temp].cli_MACAddress[2],
12261 (unsigned int *)&dev[auth_temp].cli_MACAddress[3],
12262 (unsigned int *)&dev[auth_temp].cli_MACAddress[4],
12263 (unsigned int *)&dev[auth_temp].cli_MACAddress[5]) == EOF)
12264 continue;
developera3511852023-06-14 14:12:59 +080012265 } else if (strcmp("signal", param) == 0) {
12266 value[strlen(value)-1]='\0';
developere75ba632023-06-29 16:03:33 +080012267 if (sscanf(value, "%d", &dev[auth_temp].cli_RSSI) == EOF)
12268 continue;
developera3511852023-06-14 14:12:59 +080012269 dev[auth_temp].cli_SNR = 95 + dev[auth_temp].cli_RSSI;
12270 }
12271 }
developer0d26f2c2023-05-25 19:46:36 +080012272 if (line)
developera3511852023-06-14 14:12:59 +080012273 free(line);
developerc14d83a2023-06-29 20:09:42 +080012274
12275 if (fclose(f) != 0) {
12276 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12277 }
12278
developera3511852023-06-14 14:12:59 +080012279 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12280 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012281}
developer72fb0bb2023-01-11 09:46:29 +080012282
12283/* getIPAddress function */
12284/**
12285* @description Returning IpAddress of the Matched String
12286*
developer69b61b02023-03-07 17:17:44 +080012287* @param
developer72fb0bb2023-01-11 09:46:29 +080012288* @str Having MacAddress
developer69b61b02023-03-07 17:17:44 +080012289* @ipaddr Having ipaddr
developer72fb0bb2023-01-11 09:46:29 +080012290* @return The status of the operation
12291* @retval RETURN_OK if successful
12292* @retval RETURN_ERR if any error is detected
12293*
12294*/
12295
12296INT getIPAddress(char *str,char *ipaddr)
12297{
developera3511852023-06-14 14:12:59 +080012298 FILE *fp = NULL;
12299 char buf[1024] = {0},ipAddr[50] = {0},phyAddr[100] = {0},hostName[100] = {0};
12300 int LeaseTime = 0,ret = 0;
developer32f2a182023-06-27 19:50:41 +080012301 unsigned long len;
12302
developera3511852023-06-14 14:12:59 +080012303 if ( (fp=fopen("/nvram/dnsmasq.leases", "r")) == NULL )
12304 {
12305 return RETURN_ERR;
12306 }
developer72fb0bb2023-01-11 09:46:29 +080012307
developera3511852023-06-14 14:12:59 +080012308 while ( fgets(buf, sizeof(buf), fp)!= NULL )
12309 {
12310 /*
12311 Sample:sss
12312 1560336751 00:cd:fe:f3:25:e6 10.0.0.153 NallamousiPhone 01:00:cd:fe:f3:25:e6
12313 1560336751 12:34:56:78:9a:bc 10.0.0.154 NallamousiPhone 01:00:cd:fe:f3:25:e6
12314 */
12315 ret = sscanf(buf, LM_DHCP_CLIENT_FORMAT,
12316 &(LeaseTime),
12317 phyAddr,
12318 ipAddr,
12319 hostName
12320 );
12321 if(ret != 4)
12322 continue;
developer32f2a182023-06-27 19:50:41 +080012323 if (strcmp(str,phyAddr) == 0) {
12324 len = strlen(ipAddr);
12325 strncpy(ipaddr, ipAddr, len);
12326 ipaddr[len] = '\0';
12327 }
developera3511852023-06-14 14:12:59 +080012328 }
developer37646972023-06-29 10:58:43 +080012329 if (fclose(fp) == EOF) {
12330 wifi_debug(DEBUG_ERROR, "fclose fail\n");
12331 return RETURN_ERR;
12332 }
developera3511852023-06-14 14:12:59 +080012333 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012334}
12335
12336/* wifi_getApInactiveAssociatedDeviceDiagnosticResult function */
12337/**
12338* @description Returning Inactive wireless connected clients informations
12339*
developer69b61b02023-03-07 17:17:44 +080012340* @param
developer72fb0bb2023-01-11 09:46:29 +080012341* @filename Holding private_wifi 2g/5g content files
12342* @associated_dev_array Having inactiv wireless clients informations
12343* @output_array_size Returning Inactive wireless counts
12344* @return The status of the operation
12345* @retval RETURN_OK if successful
12346* @retval RETURN_ERR if any error is detected
12347*
12348*/
12349
12350INT wifi_getApInactiveAssociatedDeviceDiagnosticResult(char *filename,wifi_associated_dev3_t **associated_dev_array, UINT *output_array_size)
12351{
developera3511852023-06-14 14:12:59 +080012352 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12353 int count = 0,maccount = 0,i = 0,wificlientindex = 0;
12354 FILE *fp = NULL;
12355 int arr[MACADDRESS_SIZE] = {0};
12356 unsigned char mac[MACADDRESS_SIZE] = {0};
12357 char path[1024] = {0},str[1024] = {0},ipaddr[50] = {0},buf[512] = {0};
developer86035662023-06-28 19:21:12 +080012358 int res;
12359
12360 res = snprintf(buf, sizeof(buf), "cat %s | grep Station | sort | uniq | wc -l",filename);
12361 if (os_snprintf_error(sizeof(buf), res)) {
12362 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12363 return RETURN_ERR;
12364 }
developera3511852023-06-14 14:12:59 +080012365 fp = popen(buf,"r");
12366 if(fp == NULL)
12367 return RETURN_ERR;
12368 else
12369 {
developerd14dff12023-06-28 22:47:44 +080012370 if (fgets(path,sizeof(path),fp) == NULL) {
12371 wifi_debug(DEBUG_ERROR, "fgets fail\n");
12372 pclose(fp);
12373 return RETURN_ERR;
12374 }
developera3511852023-06-14 14:12:59 +080012375 maccount = atoi(path);
12376 }
12377 pclose(fp);
12378 *output_array_size = maccount;
12379 wifi_associated_dev3_t* temp = NULL;
developer9ce44382023-06-28 11:09:37 +080012380 if(*output_array_size > 0 && *output_array_size < MAX_ASSOCIATED_STA_NUM){
12381 temp = (wifi_associated_dev3_t *) calloc (*output_array_size, sizeof(wifi_associated_dev3_t));
12382 } else {
12383 return RETURN_ERR;
12384 }
developer37646972023-06-29 10:58:43 +080012385
developera3511852023-06-14 14:12:59 +080012386 *associated_dev_array = temp;
12387 if(temp == NULL)
12388 {
12389 printf("Error Statement. Insufficient memory \n");
12390 return RETURN_ERR;
12391 }
12392 memset(buf,0,sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080012393 res = snprintf(buf, sizeof(buf),
12394 "cat %s | grep Station | cut -d ' ' -f2 | sort | uniq",filename);
12395 if (os_snprintf_error(sizeof(buf), res)) {
12396 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12397 return RETURN_ERR;
12398 }
developera3511852023-06-14 14:12:59 +080012399 fp = popen(buf,"r");
12400 if (fp == NULL) {
developer37646972023-06-29 10:58:43 +080012401 res = fprintf(stderr, "%s: failed pipe command %s.\n", __func__, buf);
12402 if (res < 0) {
12403 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12404 }
developera3511852023-06-14 14:12:59 +080012405 return RETURN_ERR;
12406 }
12407 for(count = 0; count < maccount ; count++)
12408 {
developer37646972023-06-29 10:58:43 +080012409 if (fgets(path,sizeof(path),fp) == NULL)
12410 continue;
developera3511852023-06-14 14:12:59 +080012411 for(i = 0; path[i]!='\n';i++)
12412 str[i]=path[i];
12413 str[i]='\0';
12414 getIPAddress(str,ipaddr);
developera3511852023-06-14 14:12:59 +080012415 if(strlen(ipaddr) > 0)
12416 {
developer33f13ba2023-07-12 16:19:06 +080012417 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 +080012418 {
12419 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12420 {
12421 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12422 {
12423 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012424
developera3511852023-06-14 14:12:59 +080012425 }
12426 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer37646972023-06-29 10:58:43 +080012427 if (fprintf(stderr,"%sMAC %d = %X:%X:%X:%X:%X:%X \n", __FUNCTION__,count, temp[count].cli_MACAddress[0],
12428 temp[count].cli_MACAddress[1], temp[count].cli_MACAddress[2],
12429 temp[count].cli_MACAddress[3], temp[count].cli_MACAddress[4],
12430 temp[count].cli_MACAddress[5]) < 0) {
12431 wifi_debug(DEBUG_ERROR, "Unexpected fprintf fail\n");
12432 break;
12433 }
developera3511852023-06-14 14:12:59 +080012434 }
12435 temp[count].cli_AuthenticationState = 0; //TODO
12436 temp[count].cli_Active = 0; //TODO
12437 temp[count].cli_SignalStrength = 0;
12438 }
12439 else //Active wireless clients info
12440 {
12441 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
12442 {
12443 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
12444 {
12445 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080012446
developera3511852023-06-14 14:12:59 +080012447 }
12448 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
developer12fb9f62023-06-30 15:26:27 +080012449 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 +080012450 }
12451 temp[count].cli_Active = 1;
12452 }
12453 }
12454 memset(ipaddr,0,sizeof(ipaddr));
12455 }
12456 pclose(fp);
12457 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12458 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012459}
12460//Device.WiFi.X_RDKCENTRAL-COM_BandSteering object
12461//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Capability bool r/o
12462//To get Band Steering Capability
12463INT wifi_getBandSteeringCapability(BOOL *support)
12464{
developera3511852023-06-14 14:12:59 +080012465 *support = FALSE;
12466 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012467}
12468
12469
12470//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.Enable bool r/w
12471//To get Band Steering enable status
12472INT wifi_getBandSteeringEnable(BOOL *enable)
12473{
developera3511852023-06-14 14:12:59 +080012474 *enable = FALSE;
12475 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012476}
12477
12478//To turn on/off Band steering
12479INT wifi_setBandSteeringEnable(BOOL enable)
12480{
developera3511852023-06-14 14:12:59 +080012481 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012482}
12483
12484//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.APGroup string r/w
12485//To get Band Steering AP group
12486INT wifi_getBandSteeringApGroup(char *output_ApGroup)
12487{
developera3511852023-06-14 14:12:59 +080012488 if (NULL == output_ApGroup)
12489 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012490
developer32f2a182023-06-27 19:50:41 +080012491 memcpy(output_ApGroup, "1,2", 3);
12492 output_ApGroup[3] = '\0';
developera3511852023-06-14 14:12:59 +080012493 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012494}
12495
12496//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.UtilizationThreshold int r/w
12497//to set and read the band steering BandUtilizationThreshold parameters
12498INT wifi_getBandSteeringBandUtilizationThreshold (INT radioIndex, INT *pBuThreshold)
12499{
developera3511852023-06-14 14:12:59 +080012500 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012501}
12502
12503INT wifi_setBandSteeringBandUtilizationThreshold (INT radioIndex, INT buThreshold)
12504{
developera3511852023-06-14 14:12:59 +080012505 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012506}
12507
12508//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.RSSIThreshold int r/w
12509//to set and read the band steering RSSIThreshold parameters
12510INT wifi_getBandSteeringRSSIThreshold (INT radioIndex, INT *pRssiThreshold)
12511{
developera3511852023-06-14 14:12:59 +080012512 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012513}
12514
12515INT wifi_setBandSteeringRSSIThreshold (INT radioIndex, INT rssiThreshold)
12516{
developera3511852023-06-14 14:12:59 +080012517 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012518}
12519
12520
12521//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.PhyRateThreshold int r/w
12522//to set and read the band steering physical modulation rate threshold parameters
12523INT wifi_getBandSteeringPhyRateThreshold (INT radioIndex, INT *pPrThreshold)
12524{
developera3511852023-06-14 14:12:59 +080012525 //If chip is not support, return -1
12526 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012527}
12528
12529INT wifi_setBandSteeringPhyRateThreshold (INT radioIndex, INT prThreshold)
12530{
developera3511852023-06-14 14:12:59 +080012531 //If chip is not support, return -1
12532 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012533}
12534
12535//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.OverloadInactiveTime int r/w
12536//to set and read the inactivity time (in seconds) for steering under overload condition
12537INT wifi_getBandSteeringOverloadInactiveTime(INT radioIndex, INT *pPrThreshold)
12538{
developera3511852023-06-14 14:12:59 +080012539 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012540}
12541
12542INT wifi_setBandSteeringOverloadInactiveTime(INT radioIndex, INT prThreshold)
12543{
developera3511852023-06-14 14:12:59 +080012544 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012545}
12546
12547//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.BandSetting.{i}.IdleInactiveTime int r/w
12548//to set and read the inactivity time (in seconds) for steering under Idle condition
12549INT wifi_getBandSteeringIdleInactiveTime(INT radioIndex, INT *pPrThreshold)
12550{
developera3511852023-06-14 14:12:59 +080012551 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012552}
12553
12554INT wifi_setBandSteeringIdleInactiveTime(INT radioIndex, INT prThreshold)
12555{
developera3511852023-06-14 14:12:59 +080012556 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012557}
12558
12559//Device.WiFi.X_RDKCENTRAL-COM_BandSteering.History string r/o
12560//pClientMAC[64]
12561//pSourceSSIDIndex[64]
12562//pDestSSIDIndex[64]
12563//pSteeringReason[256]
12564INT wifi_getBandSteeringLog(INT record_index, ULONG *pSteeringTime, CHAR *pClientMAC, INT *pSourceSSIDIndex, INT *pDestSSIDIndex, INT *pSteeringReason)
12565{
developera3511852023-06-14 14:12:59 +080012566 //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 +080012567 long int tim_tmp = time(NULL);
12568 if (tim_tmp < 0)
12569 return RETURN_ERR;
12570 *pSteeringTime = tim_tmp;
developera3511852023-06-14 14:12:59 +080012571 *pSteeringReason = 0; //TODO: need to assign correct steering reason (INT numeric, i suppose)
12572 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012573}
12574
12575INT wifi_ifConfigDown(INT apIndex)
12576{
developera3511852023-06-14 14:12:59 +080012577 INT status = RETURN_OK;
12578 char cmd[64];
developere40952c2023-06-15 18:46:43 +080012579 int res;
developer72fb0bb2023-01-11 09:46:29 +080012580
developer33f13ba2023-07-12 16:19:06 +080012581 res = v_secure_system("ifconfig ath%d down", apIndex);
12582 if (res) {
12583 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080012584 return RETURN_ERR;
12585 }
developera3511852023-06-14 14:12:59 +080012586 printf("%s: %s\n", __func__, cmd);
developer72fb0bb2023-01-11 09:46:29 +080012587
developera3511852023-06-14 14:12:59 +080012588 return status;
developer72fb0bb2023-01-11 09:46:29 +080012589}
12590
12591INT wifi_ifConfigUp(INT apIndex)
12592{
developera3511852023-06-14 14:12:59 +080012593 char interface_name[16] = {0};
12594 char cmd[128];
12595 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012596 int res;
developer72fb0bb2023-01-11 09:46:29 +080012597
developera3511852023-06-14 14:12:59 +080012598 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
12599 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080012600 res = snprintf(cmd, sizeof(cmd), "ifconfig %s up 2>/dev/null", interface_name);
12601 if (os_snprintf_error(sizeof(cmd), res)) {
12602 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12603 return RETURN_ERR;
12604 }
developera3511852023-06-14 14:12:59 +080012605 _syscmd(cmd, buf, sizeof(buf));
12606 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012607}
12608
12609//>> Deprecated. Replace with wifi_applyRadioSettings
12610INT wifi_pushBridgeInfo(INT apIndex)
12611{
developerb2977562023-05-24 17:54:12 +080012612 char ip[32] = {0};
12613 char subnet[32] = {0};
12614 char bridge[32] = {0};
12615 char cmd[128] = {0};
12616 char buf[1024] = {0};
developere40952c2023-06-15 18:46:43 +080012617 int res;
developer72fb0bb2023-01-11 09:46:29 +080012618
developerb2977562023-05-24 17:54:12 +080012619 wifi_getApBridgeInfo(apIndex, bridge, ip, subnet);
developer72fb0bb2023-01-11 09:46:29 +080012620
developere40952c2023-06-15 18:46:43 +080012621 res = snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s ", bridge, ip, subnet);
12622 if (os_snprintf_error(sizeof(cmd), res)) {
12623 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12624 return RETURN_ERR;
12625 }
developerb2977562023-05-24 17:54:12 +080012626 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080012627
developerb2977562023-05-24 17:54:12 +080012628 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012629}
12630
12631INT wifi_pushChannel(INT radioIndex, UINT channel)
12632{
developera3511852023-06-14 14:12:59 +080012633 char interface_name[16] = {0};
12634 char cmd[128];
12635 char buf[1024];
developere40952c2023-06-15 18:46:43 +080012636 int res;
developer72fb0bb2023-01-11 09:46:29 +080012637
developera3511852023-06-14 14:12:59 +080012638 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
12639 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080012640 res = snprintf(cmd, sizeof(cmd), "iwconfig %s freq %d",interface_name,channel);
12641 if (os_snprintf_error(sizeof(cmd), res)) {
12642 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12643 return RETURN_ERR;
12644 }
developera3511852023-06-14 14:12:59 +080012645 _syscmd(cmd,buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080012646
developera3511852023-06-14 14:12:59 +080012647 return 0;
developer72fb0bb2023-01-11 09:46:29 +080012648}
12649
12650INT wifi_pushChannelMode(INT radioIndex)
12651{
developera3511852023-06-14 14:12:59 +080012652 //Apply Channel mode, pure mode, etc that been set by wifi_setRadioChannelMode() instantly
12653 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012654}
12655
12656INT wifi_pushDefaultValues(INT radioIndex)
12657{
developera3511852023-06-14 14:12:59 +080012658 //Apply Comcast specified default radio settings instantly
12659 //AMPDU=1
12660 //AMPDUFrames=32
12661 //AMPDULim=50000
12662 //txqueuelen=1000
developer72fb0bb2023-01-11 09:46:29 +080012663
developera3511852023-06-14 14:12:59 +080012664 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012665}
12666
12667INT wifi_pushTxChainMask(INT radioIndex)
12668{
developera3511852023-06-14 14:12:59 +080012669 //Apply default TxChainMask instantly
12670 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012671}
12672
12673INT wifi_pushRxChainMask(INT radioIndex)
12674{
developera3511852023-06-14 14:12:59 +080012675 //Apply default RxChainMask instantly
12676 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012677}
12678
12679INT wifi_pushSSID(INT apIndex, CHAR *ssid)
12680{
developer7e4a2a62023-04-06 19:56:03 +080012681 INT status;
developer72fb0bb2023-01-11 09:46:29 +080012682
developer7e4a2a62023-04-06 19:56:03 +080012683 status = wifi_setSSIDName(apIndex, ssid);
12684 wifi_quick_reload_ap(apIndex);
developer72fb0bb2023-01-11 09:46:29 +080012685
developer7e4a2a62023-04-06 19:56:03 +080012686 return status;
developer72fb0bb2023-01-11 09:46:29 +080012687}
12688
12689INT wifi_pushSsidAdvertisementEnable(INT apIndex, BOOL enable)
12690{
developera3511852023-06-14 14:12:59 +080012691 int ret;
developerc1aa6532023-06-09 09:37:01 +080012692 ret = wifi_setApSsidAdvertisementEnable(apIndex, enable);
12693
12694 return ret;
developer72fb0bb2023-01-11 09:46:29 +080012695}
12696
12697INT wifi_getRadioUpTime(INT radioIndex, ULONG *output)
12698{
developera3511852023-06-14 14:12:59 +080012699 time_t now;
developere82c0ca2023-05-10 16:25:35 +080012700
developerd14dff12023-06-28 22:47:44 +080012701 now = time(NULL);
12702 if (now < 0) {
12703 wifi_debug(DEBUG_ERROR, "GET time fail\n");
12704 return RETURN_ERR;
12705 }
developere82c0ca2023-05-10 16:25:35 +080012706 if (now > radio_up_time[radioIndex])
12707 *output = now - radio_up_time[radioIndex];
12708 else {
12709 *output = 0;
12710 return RETURN_ERR;
12711 }
12712
developera3511852023-06-14 14:12:59 +080012713 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012714}
12715
12716INT wifi_getApEnableOnLine(INT wlanIndex, BOOL *enabled)
12717{
developera3511852023-06-14 14:12:59 +080012718 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012719}
12720
12721INT wifi_getApSecurityWpaRekeyInterval(INT apIndex, INT *output_int)
12722{
developera3511852023-06-14 14:12:59 +080012723 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012724}
12725
12726//To-do
12727INT wifi_getApSecurityMFPConfig(INT apIndex, CHAR *output_string)
12728{
developera3511852023-06-14 14:12:59 +080012729 char output[16]={'\0'};
12730 char config_file[MAX_BUF_SIZE] = {0};
12731 int res;
developer72fb0bb2023-01-11 09:46:29 +080012732
developera3511852023-06-14 14:12:59 +080012733 if (!output_string)
12734 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012735
developera3511852023-06-14 14:12:59 +080012736 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12737 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012738 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12739 return RETURN_ERR;
12740 }
developera3511852023-06-14 14:12:59 +080012741 wifi_hostapdRead(config_file, "ieee80211w", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012742
developera3511852023-06-14 14:12:59 +080012743 if (strlen(output) == 0)
developere40952c2023-06-15 18:46:43 +080012744 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012745 else if (strncmp(output, "0", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012746 res = snprintf(output_string, 64, "Disabled");
developera3511852023-06-14 14:12:59 +080012747 else if (strncmp(output, "1", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012748 res = snprintf(output_string, 64, "Optional");
developera3511852023-06-14 14:12:59 +080012749 else if (strncmp(output, "2", 1) == 0)
developere40952c2023-06-15 18:46:43 +080012750 res = snprintf(output_string, 64, "Required");
developera3511852023-06-14 14:12:59 +080012751 else {
12752 wifi_dbg_printf("\n[%s]: Unexpected ieee80211w=%s", __func__, output);
12753 return RETURN_ERR;
12754 }
developere40952c2023-06-15 18:46:43 +080012755 if (os_snprintf_error(64, res)) {
12756 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12757 return RETURN_ERR;
12758 }
developer72fb0bb2023-01-11 09:46:29 +080012759
developera3511852023-06-14 14:12:59 +080012760 wifi_dbg_printf("\n[%s]: ieee80211w is : %s", __func__, output);
12761 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012762}
12763INT wifi_setApSecurityMFPConfig(INT apIndex, CHAR *MfpConfig)
12764{
developera3511852023-06-14 14:12:59 +080012765 struct params params;
12766 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080012767 int res;
developer72fb0bb2023-01-11 09:46:29 +080012768
developera3511852023-06-14 14:12:59 +080012769 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12770 if(NULL == MfpConfig || strlen(MfpConfig) >= 32 )
12771 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012772
developera3511852023-06-14 14:12:59 +080012773 params.name = "ieee80211w";
12774 if (strncmp(MfpConfig, "Disabled", strlen("Disabled")) == 0)
12775 params.value = "0";
12776 else if (strncmp(MfpConfig, "Optional", strlen("Optional")) == 0)
12777 params.value = "1";
12778 else if (strncmp(MfpConfig, "Required", strlen("Required")) == 0)
12779 params.value = "2";
12780 else{
12781 wifi_dbg_printf("%s: invalid MfpConfig. Input has to be Disabled, Optional or Required \n", __func__);
12782 return RETURN_ERR;
12783 }
developer75bd10c2023-06-27 11:34:08 +080012784
12785 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
12786 if (os_snprintf_error(sizeof(config_file), res)) {
12787 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12788 return RETURN_ERR;
12789 }
12790
developera3511852023-06-14 14:12:59 +080012791 wifi_hostapdWrite(config_file, &params, 1);
12792 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
12793 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012794}
12795INT wifi_getRadioAutoChannelEnable(INT radioIndex, BOOL *output_bool)
12796{
developera3511852023-06-14 14:12:59 +080012797 char output[16]={'\0'};
12798 char config_file[MAX_BUF_SIZE] = {0};
12799 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080012800 int res;
developer72fb0bb2023-01-11 09:46:29 +080012801
developera3511852023-06-14 14:12:59 +080012802 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12803 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080012804 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
12805 if (os_snprintf_error(sizeof(config_file), res)) {
12806 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12807 return RETURN_ERR;
12808 }
developera3511852023-06-14 14:12:59 +080012809 wifi_datfileRead(config_file, "AutoChannelSelect" , output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080012810
developera3511852023-06-14 14:12:59 +080012811 if (strncmp(output, "0", 1) == 0)
12812 *output_bool = FALSE;
12813 else if (strncmp(output, "1", 1) == 0)
12814 *output_bool = TRUE;
12815 else if (strncmp(output, "2", 1) == 0)
12816 *output_bool = TRUE;
12817 else if (strncmp(output, "3", 1) == 0)
12818 *output_bool = TRUE;
12819 else
12820 *output_bool = FALSE;
12821 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012822
developera3511852023-06-14 14:12:59 +080012823 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012824}
12825
12826INT wifi_getRouterEnable(INT wlanIndex, BOOL *enabled)
12827{
developera3511852023-06-14 14:12:59 +080012828 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012829}
12830
12831INT wifi_setApSecurityWpaRekeyInterval(INT apIndex, INT *rekeyInterval)
12832{
developera3511852023-06-14 14:12:59 +080012833 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012834}
12835
12836INT wifi_setRouterEnable(INT wlanIndex, INT *RouterEnabled)
12837{
developera3511852023-06-14 14:12:59 +080012838 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012839}
12840
12841INT wifi_getRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
12842{
developera3511852023-06-14 14:12:59 +080012843 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12844 char config_file[MAX_BUF_SIZE] = {0};
developer32f2a182023-06-27 19:50:41 +080012845 char tmp_output[MAX_BUF_SIZE] = {0};
developera3511852023-06-14 14:12:59 +080012846 int res;
developer72fb0bb2023-01-11 09:46:29 +080012847
developera3511852023-06-14 14:12:59 +080012848 if (NULL == output)
12849 return RETURN_ERR;
12850 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12851 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012852 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12853 return RETURN_ERR;
12854 }
developera3511852023-06-14 14:12:59 +080012855 wifi_hostapdRead(config_file,"hw_mode",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012856
developer32f2a182023-06-27 19:50:41 +080012857 if(strcmp(output,"b")==0) {
12858 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,11");
12859 if (os_snprintf_error(sizeof(tmp_output), res)) {
12860 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12861 return RETURN_ERR;
12862 }
12863 } else if (strcmp(output,"a")==0) {
12864 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "6,9,11,12,18,24,36,48,54");
12865 if (os_snprintf_error(sizeof(tmp_output), res)) {
12866 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12867 return RETURN_ERR;
12868 }
12869 } else if ((strcmp(output,"n")==0) | (strcmp(output,"g")==0)) {
12870 res = snprintf(tmp_output, sizeof(tmp_output), "%s", "1,2,5.5,6,9,11,12,18,24,36,48,54");
12871 if (os_snprintf_error(sizeof(tmp_output), res)) {
12872 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12873 return RETURN_ERR;
12874 }
developer75bd10c2023-06-27 11:34:08 +080012875 }
developer32f2a182023-06-27 19:50:41 +080012876 memcpy(output, tmp_output, strlen(tmp_output));
12877 output[strlen(tmp_output)] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012878
developera3511852023-06-14 14:12:59 +080012879 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
12880 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012881}
12882
12883INT wifi_getRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
12884{
developera3511852023-06-14 14:12:59 +080012885 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12886 char *temp;
developer32f2a182023-06-27 19:50:41 +080012887 char temp_output[128] = {0};
12888 char temp_TransmitRates[128] = {0};
developera3511852023-06-14 14:12:59 +080012889 char config_file[MAX_BUF_SIZE] = {0};
12890 int res;
developer32f2a182023-06-27 19:50:41 +080012891 unsigned long len;
developer72fb0bb2023-01-11 09:46:29 +080012892
developera3511852023-06-14 14:12:59 +080012893 if (NULL == output)
12894 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080012895
developera3511852023-06-14 14:12:59 +080012896 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
12897 if (os_snprintf_error(sizeof(config_file), res)) {
developer46506162023-06-12 10:09:39 +080012898 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
12899 return RETURN_ERR;
12900 }
developera3511852023-06-14 14:12:59 +080012901 wifi_hostapdRead(config_file,"supported_rates",output,64);
developer72fb0bb2023-01-11 09:46:29 +080012902
developera3511852023-06-14 14:12:59 +080012903 if (strlen(output) == 0) {
12904 wifi_getRadioSupportedDataTransmitRates(wlanIndex, output);
12905 return RETURN_OK;
12906 }
developer32f2a182023-06-27 19:50:41 +080012907 len = strlen(output);
12908 if (len >= sizeof(temp_TransmitRates)) {
12909 wifi_debug(DEBUG_ERROR, "Unexpected strlen(output)\n");
12910 return RETURN_ERR;
12911 }
12912 strncpy(temp_TransmitRates, output, len);
developera3511852023-06-14 14:12:59 +080012913 temp = strtok(temp_TransmitRates," ");
12914 while(temp!=NULL)
12915 {
12916 temp[strlen(temp)-1]=0;
12917 if((temp[0]=='5') && (temp[1]=='\0'))
12918 {
12919 temp="5.5";
12920 }
developer32f2a182023-06-27 19:50:41 +080012921 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp)) {
12922 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
12923 return RETURN_ERR;
12924 }
12925 strncat(temp_output, temp, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080012926 temp = strtok(NULL," ");
12927 if(temp!=NULL)
12928 {
developer32f2a182023-06-27 19:50:41 +080012929 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
12930 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
12931 return RETURN_ERR;
12932 }
12933 strncat(temp_output, ",", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080012934 }
12935 }
developer32f2a182023-06-27 19:50:41 +080012936 len = strlen(temp_output);
12937 strncpy(output, temp_output, len);
12938 output[len] = '\0';
developera3511852023-06-14 14:12:59 +080012939 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080012940
developera3511852023-06-14 14:12:59 +080012941 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012942}
12943
12944INT wifi_setRadioSupportedDataTransmitRates(INT wlanIndex,CHAR *output)
12945{
developera3511852023-06-14 14:12:59 +080012946 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080012947}
12948
12949
12950INT wifi_setRadioOperationalDataTransmitRates(INT wlanIndex,CHAR *output)
12951{
developera3511852023-06-14 14:12:59 +080012952 int i=0;
12953 char *temp;
12954 char temp1[128] = {0};
12955 char temp_output[128] = {0};
12956 char temp_TransmitRates[128] = {0};
12957 struct params params={'\0'};
12958 char config_file[MAX_BUF_SIZE] = {0};
12959 wifi_band band = wifi_index_to_band(wlanIndex);
developer32f2a182023-06-27 19:50:41 +080012960 unsigned long len;
12961 int res;
developer72fb0bb2023-01-11 09:46:29 +080012962
developera3511852023-06-14 14:12:59 +080012963 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
12964 if(NULL == output)
12965 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080012966
12967 len = strlen(output);
12968 if (len >= sizeof(temp_TransmitRates)) {
12969 wifi_debug(DEBUG_ERROR, "not enough room in temp_TransmitRates\n");
12970 return RETURN_ERR;
12971 }
12972 strncpy(temp_TransmitRates, output, len);
12973 temp_TransmitRates[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080012974
developera3511852023-06-14 14:12:59 +080012975 for(i=0;i<strlen(temp_TransmitRates);i++)
12976 {
12977 if (((temp_TransmitRates[i]>='0') && (temp_TransmitRates[i]<='9')) || (temp_TransmitRates[i]==' ') || (temp_TransmitRates[i]=='.') || (temp_TransmitRates[i]==','))
12978 {
12979 continue;
12980 }
12981 else
12982 {
12983 return RETURN_ERR;
12984 }
12985 }
developera3511852023-06-14 14:12:59 +080012986 temp = strtok(temp_TransmitRates,",");
12987 while(temp!=NULL)
12988 {
developer32f2a182023-06-27 19:50:41 +080012989 len = strlen(temp);
12990 if (len >= sizeof(temp1)) {
12991 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
12992 return RETURN_ERR;
12993 }
12994 strncpy(temp1, temp, len);
12995 temp1[len] = '\0';
developera3511852023-06-14 14:12:59 +080012996 if(band == band_5)
12997 {
12998 if((strcmp(temp,"1")==0) || (strcmp(temp,"2")==0) || (strcmp(temp,"5.5")==0))
12999 {
13000 return RETURN_ERR;
13001 }
13002 }
developer72fb0bb2023-01-11 09:46:29 +080013003
developer32f2a182023-06-27 19:50:41 +080013004 if(strcmp(temp,"5.5")==0) {
13005 strncpy(temp1, "55", 2);
13006 temp1[2] = '\0';
13007 } else {
13008 if ((sizeof(temp1) - strlen(temp1)) <= 1) {
13009 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13010 return RETURN_ERR;
13011 }
13012 strncat(temp1, "0", sizeof(temp1) - strlen(temp1) - 1);
developera3511852023-06-14 14:12:59 +080013013 }
developer32f2a182023-06-27 19:50:41 +080013014
13015 if ((sizeof(temp_output) - strlen(temp_output)) <= strlen(temp1)) {
13016 wifi_debug(DEBUG_ERROR, "not enough room in temp_output\n");
13017 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080013018 }
developer32f2a182023-06-27 19:50:41 +080013019 strncat(temp_output, temp1, sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013020 temp = strtok(NULL,",");
13021 if(temp!=NULL)
13022 {
developer32f2a182023-06-27 19:50:41 +080013023 if ((sizeof(temp_output) - strlen(temp_output)) <= 1) {
13024 wifi_debug(DEBUG_ERROR, "not enough room in temp1\n");
13025 return RETURN_ERR;
13026 }
13027 strncat(temp_output, " ", sizeof(temp_output) - strlen(temp_output) - 1);
developera3511852023-06-14 14:12:59 +080013028 }
13029 }
developer32f2a182023-06-27 19:50:41 +080013030 len = strlen(temp_output);
13031 strncpy(output, temp_output, len);
13032 output[len] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080013033
developera3511852023-06-14 14:12:59 +080013034 params.name = "supported_rates";
13035 params.value = output;
developer72fb0bb2023-01-11 09:46:29 +080013036
developera3511852023-06-14 14:12:59 +080013037 wifi_dbg_printf("\n%s:",__func__);
13038 wifi_dbg_printf("params.value=%s\n",params.value);
developer32f2a182023-06-27 19:50:41 +080013039 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,wlanIndex);
13040 if (os_snprintf_error(sizeof(config_file), res)) {
13041 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13042 return RETURN_ERR;
13043 }
developera3511852023-06-14 14:12:59 +080013044 wifi_hostapdWrite(config_file,&params,1);
13045 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013046
developera3511852023-06-14 14:12:59 +080013047 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013048}
13049
13050
13051static char *sncopy(char *dst, int dst_sz, const char *src)
13052{
developera3511852023-06-14 14:12:59 +080013053 if (src && dst && dst_sz > 0) {
13054 strncpy(dst, src, dst_sz);
13055 dst[dst_sz - 1] = '\0';
13056 }
13057 return dst;
developer72fb0bb2023-01-11 09:46:29 +080013058}
13059
13060static int util_get_sec_chan_offset(int channel, const char* ht_mode)
13061{
developera3511852023-06-14 14:12:59 +080013062 if (0 == strcmp(ht_mode, "HT40") ||
13063 0 == strcmp(ht_mode, "HT80") ||
13064 0 == strcmp(ht_mode, "HT160")) {
13065 switch (channel) {
13066 case 1 ... 7:
13067 case 36:
13068 case 44:
13069 case 52:
13070 case 60:
13071 case 100:
13072 case 108:
13073 case 116:
13074 case 124:
13075 case 132:
13076 case 140:
13077 case 149:
13078 case 157:
13079 return 1;
13080 case 8 ... 13:
13081 case 40:
13082 case 48:
13083 case 56:
13084 case 64:
13085 case 104:
13086 case 112:
13087 case 120:
13088 case 128:
13089 case 136:
13090 case 144:
13091 case 153:
13092 case 161:
13093 return -1;
13094 default:
13095 return -EINVAL;
13096 }
13097 }
developer72fb0bb2023-01-11 09:46:29 +080013098
developera3511852023-06-14 14:12:59 +080013099 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013100}
13101
13102static int util_get_6g_sec_chan_offset(int channel, const char* ht_mode)
13103{
developera3511852023-06-14 14:12:59 +080013104 int idx = channel%8;
13105 if (0 == strcmp(ht_mode, "HT40") ||
13106 0 == strcmp(ht_mode, "HT80") ||
13107 0 == strcmp(ht_mode, "HT160")) {
13108 switch (idx) {
13109 case 1:
13110 return 1;
13111 case 5:
13112 return -1;
13113 default:
13114 return -EINVAL;
13115 }
13116 }
developer72fb0bb2023-01-11 09:46:29 +080013117
developera3511852023-06-14 14:12:59 +080013118 return -EINVAL;
developer72fb0bb2023-01-11 09:46:29 +080013119}
13120static void util_hw_mode_to_bw_mode(const char* hw_mode, char *bw_mode, int bw_mode_len)
13121{
developera3511852023-06-14 14:12:59 +080013122 if (NULL == hw_mode) return;
developer72fb0bb2023-01-11 09:46:29 +080013123
developera3511852023-06-14 14:12:59 +080013124 if (0 == strcmp(hw_mode, "ac"))
13125 sncopy(bw_mode, bw_mode_len, "ht vht");
developer72fb0bb2023-01-11 09:46:29 +080013126
developera3511852023-06-14 14:12:59 +080013127 if (0 == strcmp(hw_mode, "n"))
13128 sncopy(bw_mode, bw_mode_len, "ht");
developer72fb0bb2023-01-11 09:46:29 +080013129
developera3511852023-06-14 14:12:59 +080013130 return;
developer72fb0bb2023-01-11 09:46:29 +080013131}
13132
13133static int util_chan_to_freq(int chan)
13134{
developera3511852023-06-14 14:12:59 +080013135 if (chan == 14)
13136 return 2484;
13137 else if (chan < 14)
13138 return 2407 + chan * 5;
13139 else if (chan >= 182 && chan <= 196)
13140 return 4000 + chan * 5;
13141 else
13142 return 5000 + chan * 5;
13143 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013144}
13145
13146static int util_6G_chan_to_freq(int chan)
13147{
developera3511852023-06-14 14:12:59 +080013148 if (chan)
13149 return 5950 + chan * 5;
13150 else
13151 return 0;
developer69b61b02023-03-07 17:17:44 +080013152
developer72fb0bb2023-01-11 09:46:29 +080013153}
13154const int *util_unii_5g_chan2list(int chan, int width)
13155{
developera3511852023-06-14 14:12:59 +080013156 static const int lists[] = {
13157 // <width>, <chan1>, <chan2>..., 0,
13158 20, 36, 0,
13159 20, 40, 0,
13160 20, 44, 0,
13161 20, 48, 0,
13162 20, 52, 0,
13163 20, 56, 0,
13164 20, 60, 0,
13165 20, 64, 0,
13166 20, 100, 0,
13167 20, 104, 0,
13168 20, 108, 0,
13169 20, 112, 0,
13170 20, 116, 0,
13171 20, 120, 0,
13172 20, 124, 0,
13173 20, 128, 0,
13174 20, 132, 0,
13175 20, 136, 0,
13176 20, 140, 0,
13177 20, 144, 0,
13178 20, 149, 0,
13179 20, 153, 0,
13180 20, 157, 0,
13181 20, 161, 0,
13182 20, 165, 0,
13183 40, 36, 40, 0,
13184 40, 44, 48, 0,
13185 40, 52, 56, 0,
13186 40, 60, 64, 0,
13187 40, 100, 104, 0,
13188 40, 108, 112, 0,
13189 40, 116, 120, 0,
13190 40, 124, 128, 0,
13191 40, 132, 136, 0,
13192 40, 140, 144, 0,
13193 40, 149, 153, 0,
13194 40, 157, 161, 0,
13195 80, 36, 40, 44, 48, 0,
13196 80, 52, 56, 60, 64, 0,
13197 80, 100, 104, 108, 112, 0,
13198 80, 116, 120, 124, 128, 0,
13199 80, 132, 136, 140, 144, 0,
13200 80, 149, 153, 157, 161, 0,
13201 160, 36, 40, 44, 48, 52, 56, 60, 64, 0,
13202 160, 100, 104, 108, 112, 116, 120, 124, 128, 0,
13203 -1 // final delimiter
13204 };
13205 const int *start;
13206 const int *p;
developer72fb0bb2023-01-11 09:46:29 +080013207
developera3511852023-06-14 14:12:59 +080013208 for (p = lists; *p != -1; p++) {
13209 if (*p == width) {
13210 for (start = ++p; *p != 0; p++) {
13211 if (*p == chan)
13212 return start;
13213 }
13214 }
13215 // move to the end of channel list of given width
13216 while (*p != 0) {
13217 p++;
13218 }
13219 }
developer72fb0bb2023-01-11 09:46:29 +080013220
developera3511852023-06-14 14:12:59 +080013221 return NULL;
developer72fb0bb2023-01-11 09:46:29 +080013222}
13223
13224static int util_unii_5g_centerfreq(const char *ht_mode, int channel)
13225{
developera3511852023-06-14 14:12:59 +080013226 if (NULL == ht_mode)
13227 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013228
developera3511852023-06-14 14:12:59 +080013229 const int width = atoi(strlen(ht_mode) > 2 ? ht_mode + 2 : "20");
13230 const int *chans = util_unii_5g_chan2list(channel, width);
13231 int sum = 0;
13232 int cnt = 0;
developer72fb0bb2023-01-11 09:46:29 +080013233
developera3511852023-06-14 14:12:59 +080013234 if (NULL == chans)
13235 return 0;
developer72fb0bb2023-01-11 09:46:29 +080013236
developera3511852023-06-14 14:12:59 +080013237 while (*chans) {
13238 sum += *chans;
13239 cnt++;
13240 chans++;
13241 }
13242 if (cnt == 0)
13243 return 0;
13244 return sum / cnt;
developer72fb0bb2023-01-11 09:46:29 +080013245}
13246
13247static int util_unii_6g_centerfreq(const char *ht_mode, int channel)
13248{
developerc14d83a2023-06-29 20:09:42 +080013249 long int width;
developera3511852023-06-14 14:12:59 +080013250 int idx = 0 ;
13251 int centerchan = 0;
13252 int chan_ofs = 1;
developer72fb0bb2023-01-11 09:46:29 +080013253
developerc14d83a2023-06-29 20:09:42 +080013254 if (NULL == ht_mode)
13255 return 0;
13256
13257 if (hal_strtol((char *)(ht_mode + 2), 10, &width) < 0) {
13258 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13259 }
13260
developera3511852023-06-14 14:12:59 +080013261 if (width == 40){
13262 idx = ((channel/4) + chan_ofs)%2;
13263 switch (idx) {
13264 case 0:
13265 centerchan = (channel - 2);
13266 break;
13267 case 1:
13268 centerchan = (channel + 2);
13269 break;
13270 default:
13271 return -EINVAL;
13272 }
13273 }else if (width == 80){
13274 idx = ((channel/4) + chan_ofs)%4;
13275 switch (idx) {
13276 case 0:
13277 centerchan = (channel - 6);
13278 break;
13279 case 1:
13280 centerchan = (channel + 6);
13281 break;
13282 case 2:
13283 centerchan = (channel + 2);
13284 break;
13285 case 3:
13286 centerchan = (channel - 2);
13287 break;
13288 default:
13289 return -EINVAL;
13290 }
13291 }else if (width == 160){
13292 switch (channel) {
13293 case 1 ... 29:
13294 centerchan = 15;
13295 break;
13296 case 33 ... 61:
13297 centerchan = 47;
13298 break;
13299 case 65 ... 93:
13300 centerchan = 79;
13301 break;
13302 case 97 ... 125:
13303 centerchan = 111;
13304 break;
13305 case 129 ... 157:
13306 centerchan = 143;
13307 break;
13308 case 161 ... 189:
13309 centerchan = 175;
13310 break;
13311 case 193 ... 221:
13312 centerchan = 207;
13313 break;
13314 default:
13315 return -EINVAL;
13316 }
13317 }
13318 return centerchan;
developer72fb0bb2023-01-11 09:46:29 +080013319}
13320static int util_radio_get_hw_mode(int radioIndex, char *hw_mode, int hw_mode_size)
13321{
developera3511852023-06-14 14:12:59 +080013322 BOOL onlyG, onlyN, onlyA;
13323 CHAR tmp[64];
13324 int ret = wifi_getRadioStandard(radioIndex, tmp, &onlyG, &onlyN, &onlyA);
13325 if (ret == RETURN_OK) {
13326 sncopy(hw_mode, hw_mode_size, tmp);
13327 }
13328 return ret;
developer72fb0bb2023-01-11 09:46:29 +080013329}
13330
13331INT wifi_pushRadioChannel2(INT radioIndex, UINT channel, UINT channel_width_MHz, UINT csa_beacon_count)
13332{
developera3511852023-06-14 14:12:59 +080013333 // Sample commands:
13334 // hostapd_cli -i wifi1 chan_switch 30 5200 sec_channel_offset=-1 center_freq1=5190 bandwidth=40 ht vht
13335 // hostapd_cli -i wifi0 chan_switch 30 2437
13336 int ret = 0;
13337 char center_freq1_str[32] = ""; // center_freq1=%d
13338 char opt_chan_info_str[32] = ""; // bandwidth=%d ht vht
13339 char sec_chan_offset_str[32] = ""; // sec_channel_offset=%d
13340 char hw_mode[16] = ""; // n|ac
13341 char bw_mode[16] = ""; // ht|ht vht
13342 char ht_mode[16] = ""; // HT20|HT40|HT80|HT160
13343 char interface_name[16] = {0};
13344 int sec_chan_offset;
13345 int width;
13346 char config_file[64] = {0};
13347 char *ext_str = "None";
13348 wifi_band band = band_invalid;
13349 int center_chan = 0;
13350 int center_freq1 = 0;
developere40952c2023-06-15 18:46:43 +080013351 int res;
developer72fb0bb2023-01-11 09:46:29 +080013352
developere40952c2023-06-15 18:46:43 +080013353 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radioIndex);
13354 if (os_snprintf_error(sizeof(config_file), res)) {
13355 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13356 return RETURN_ERR;
13357 }
developer72fb0bb2023-01-11 09:46:29 +080013358
developera3511852023-06-14 14:12:59 +080013359 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
13360 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013361
developera3511852023-06-14 14:12:59 +080013362 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013363
developera3511852023-06-14 14:12:59 +080013364 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080013365
developera3511852023-06-14 14:12:59 +080013366 width = channel_width_MHz > 20 ? channel_width_MHz : 20;
developer72fb0bb2023-01-11 09:46:29 +080013367
developera3511852023-06-14 14:12:59 +080013368 // Get radio mode HT20|HT40|HT80 etc.
13369 if (channel){
developere40952c2023-06-15 18:46:43 +080013370 res = snprintf(ht_mode, sizeof(ht_mode), "HT%d", width);
13371 if (os_snprintf_error(sizeof(ht_mode), res)) {
13372 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13373 return RETURN_ERR;
13374 }
developer72fb0bb2023-01-11 09:46:29 +080013375
developera3511852023-06-14 14:12:59 +080013376 // Provide bandwith if specified
13377 if (channel_width_MHz > 20) {
13378 // Select bandwidth mode from hardware n --> ht | ac --> ht vht
13379 util_radio_get_hw_mode(radioIndex, hw_mode, sizeof(hw_mode));
13380 util_hw_mode_to_bw_mode(hw_mode, bw_mode, sizeof(bw_mode));
developer72fb0bb2023-01-11 09:46:29 +080013381
developere40952c2023-06-15 18:46:43 +080013382 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d %s", width, bw_mode);
developera3511852023-06-14 14:12:59 +080013383 }else if (channel_width_MHz == 20){
developere40952c2023-06-15 18:46:43 +080013384 res = snprintf(opt_chan_info_str, sizeof(opt_chan_info_str), "bandwidth=%d ht", width);
developera3511852023-06-14 14:12:59 +080013385 }
developer72fb0bb2023-01-11 09:46:29 +080013386
developere40952c2023-06-15 18:46:43 +080013387 if (os_snprintf_error(sizeof(opt_chan_info_str), res)) {
13388 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13389 return RETURN_ERR;
13390 }
developer72fb0bb2023-01-11 09:46:29 +080013391
developera3511852023-06-14 14:12:59 +080013392 if (channel_width_MHz > 20) {
13393 if (band == band_6){
13394 center_chan = util_unii_6g_centerfreq(ht_mode, channel);
13395 if(center_chan){
13396 center_freq1 = util_6G_chan_to_freq(center_chan);
13397 }
13398 }else{
13399 center_chan = util_unii_5g_centerfreq(ht_mode, channel);
13400 if(center_chan){
13401 center_freq1 = util_chan_to_freq(center_chan);
13402 }
13403 }
developer69b61b02023-03-07 17:17:44 +080013404
developera3511852023-06-14 14:12:59 +080013405 if (center_freq1)
developere40952c2023-06-15 18:46:43 +080013406 res = snprintf(center_freq1_str, sizeof(center_freq1_str), "center_freq1=%d", center_freq1);
developer69b61b02023-03-07 17:17:44 +080013407
developera3511852023-06-14 14:12:59 +080013408 }
developere40952c2023-06-15 18:46:43 +080013409 if (os_snprintf_error(sizeof(center_freq1_str), res)) {
13410 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13411 return RETURN_ERR;
13412 }
developer72fb0bb2023-01-11 09:46:29 +080013413
developera3511852023-06-14 14:12:59 +080013414 // Find channel offset +1/-1 for wide modes (HT40|HT80|HT160)
13415 if (band == band_6){
13416 sec_chan_offset = util_get_6g_sec_chan_offset(channel, ht_mode);
13417 }else{
13418 sec_chan_offset = util_get_sec_chan_offset(channel, ht_mode);
13419 }
developere40952c2023-06-15 18:46:43 +080013420 if (sec_chan_offset != -EINVAL) {
13421 res = snprintf(sec_chan_offset_str, sizeof(sec_chan_offset_str), "sec_channel_offset=%d", sec_chan_offset);
13422 if (os_snprintf_error(sizeof(sec_chan_offset_str), res)) {
13423 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13424 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013425 }
developere40952c2023-06-15 18:46:43 +080013426 }
developera3511852023-06-14 14:12:59 +080013427 // Only the first AP, other are hanging on the same radio
13428 ret = wifi_setChannel_netlink(radioIndex, &channel, NULL);
13429 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013430 wifi_debug(DEBUG_ERROR,"wifi_setChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013431 return RETURN_ERR;
13432 }
13433 /* wifi_dbg_printf("execute: '%s'\n", cmd);
13434 ret = _syscmd(cmd, buf, sizeof(buf));
13435 wifi_reloadAp(radioIndex); */
developer72fb0bb2023-01-11 09:46:29 +080013436
developera3511852023-06-14 14:12:59 +080013437 ret = wifi_setRadioChannel(radioIndex, channel);
13438 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080013439 wifi_debug(DEBUG_ERROR,"wifi_setRadioChannel return error.\n");
developera3511852023-06-14 14:12:59 +080013440 return RETURN_ERR;
13441 }
developer72fb0bb2023-01-11 09:46:29 +080013442
developera3511852023-06-14 14:12:59 +080013443 if (sec_chan_offset == 1)
13444 ext_str = "Above";
13445 else if (sec_chan_offset == -1)
13446 ext_str = "Below";
developer72fb0bb2023-01-11 09:46:29 +080013447
developera3511852023-06-14 14:12:59 +080013448 /*wifi_setRadioCenterChannel(radioIndex, center_chan); */
developer72fb0bb2023-01-11 09:46:29 +080013449
developera3511852023-06-14 14:12:59 +080013450 } else {
13451 if (channel_width_MHz > 20)
13452 ext_str = "Above";
13453 }
developer72fb0bb2023-01-11 09:46:29 +080013454
developera3511852023-06-14 14:12:59 +080013455 wifi_setRadioExtChannel(radioIndex, ext_str);
developer72fb0bb2023-01-11 09:46:29 +080013456
developera3511852023-06-14 14:12:59 +080013457 char mhz_str[16];
developere40952c2023-06-15 18:46:43 +080013458 res = snprintf(mhz_str, sizeof(mhz_str), "%dMHz", width);
13459 if (os_snprintf_error(sizeof(mhz_str), res)) {
13460 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13461 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013462 }
developera3511852023-06-14 14:12:59 +080013463 wifi_setRadioOperatingChannelBandwidth(radioIndex, mhz_str);
developer72fb0bb2023-01-11 09:46:29 +080013464
developera3511852023-06-14 14:12:59 +080013465 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013466
developera3511852023-06-14 14:12:59 +080013467 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013468}
13469
13470INT wifi_getNeighboringWiFiStatus(INT radio_index, wifi_neighbor_ap2_t **neighbor_ap_array, UINT *output_array_size)
13471{
developera3511852023-06-14 14:12:59 +080013472 int index = -1;
13473 wifi_neighbor_ap2_t *scan_array = NULL;
13474 char cmd[256]={0};
13475 char buf[128]={0};
13476 char file_name[32] = {0};
13477 char filter_SSID[32] = {0};
13478 char line[256] = {0};
13479 char interface_name[16] = {0};
13480 char *ret = NULL;
13481 int freq=0;
13482 FILE *f = NULL;
developer86035662023-06-28 19:21:12 +080013483 long int channels_num = 0;
developera3511852023-06-14 14:12:59 +080013484 int vht_channel_width = 0;
13485 int get_noise_ret = RETURN_ERR;
13486 bool filter_enable = false;
13487 bool filter_BSS = false; // The flag determine whether the BSS information need to be filterd.
13488 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080013489 int res;
developer32f2a182023-06-27 19:50:41 +080013490 unsigned long len;
developerb14b3462023-07-01 18:02:42 +080013491 struct channels_noise *channels_noise_arr = NULL;
developer72fb0bb2023-01-11 09:46:29 +080013492
developera3511852023-06-14 14:12:59 +080013493 WIFI_ENTRY_EXIT_DEBUG("Inside %s: %d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013494
developere40952c2023-06-15 18:46:43 +080013495 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, radio_index);
13496 if (os_snprintf_error(sizeof(file_name), res)) {
13497 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13498 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080013499 }
developera3511852023-06-14 14:12:59 +080013500 f = fopen(file_name, "r");
13501 if (f != NULL) {
developer86035662023-06-28 19:21:12 +080013502 if (fgets(filter_SSID, sizeof(file_name), f) == NULL) {
13503 wifi_debug(DEBUG_ERROR, "fgets fail\n");
developerc14d83a2023-06-29 20:09:42 +080013504 if (fclose(f) != 0) {
13505 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13506 }
developer86035662023-06-28 19:21:12 +080013507 return RETURN_ERR;
13508 }
developera3511852023-06-14 14:12:59 +080013509 if (strlen(filter_SSID) != 0)
13510 filter_enable = true;
developerd14dff12023-06-28 22:47:44 +080013511 if (fclose(f) != 0) {
13512 wifi_debug(DEBUG_ERROR, "fclose fail\n");
13513 return RETURN_ERR;
13514 }
developera3511852023-06-14 14:12:59 +080013515 }
developer72fb0bb2023-01-11 09:46:29 +080013516
developera3511852023-06-14 14:12:59 +080013517 if (wifi_GetInterfaceName(radio_index, interface_name) != RETURN_OK)
13518 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013519
developera3511852023-06-14 14:12:59 +080013520 phyId = radio_index_to_phy(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080013521
developere40952c2023-06-15 18:46:43 +080013522 res = snprintf(cmd, sizeof(cmd), "iw phy phy%d channels | grep * | grep -v disable | wc -l", phyId);
13523 if (os_snprintf_error(sizeof(cmd), res)) {
13524 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13525 return RETURN_ERR;
13526 }
developera3511852023-06-14 14:12:59 +080013527 _syscmd(cmd, buf, sizeof(buf));
developer86035662023-06-28 19:21:12 +080013528 if (hal_strtol(buf, 10, &channels_num) < 0) {
13529 wifi_debug(DEBUG_ERROR, "strtol fail\n");
13530 return RETURN_ERR;
13531 }
developer72fb0bb2023-01-11 09:46:29 +080013532
developer32f2a182023-06-27 19:50:41 +080013533 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan dump | grep '%s\\|SSID\\|freq\\|beacon interval\\|capabilities\\|signal\\|Supported rates\\|DTIM\\| \
developera3511852023-06-14 14:12:59 +080013534 // 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 +080013535 if (os_snprintf_error(sizeof(cmd), res)) {
13536 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13537 return RETURN_ERR;
13538 }
13539
developer86035662023-06-28 19:21:12 +080013540 wifi_debug(DEBUG_ERROR, "cmd: %s\n", cmd);
developera3511852023-06-14 14:12:59 +080013541 if ((f = popen(cmd, "r")) == NULL) {
13542 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
13543 return RETURN_ERR;
13544 }
developer69b61b02023-03-07 17:17:44 +080013545
developerb14b3462023-07-01 18:02:42 +080013546 if (channels_num > 0 && channels_num <= 243) {
13547 channels_noise_arr = calloc(channels_num, sizeof(struct channels_noise));
developer86035662023-06-28 19:21:12 +080013548
developerb14b3462023-07-01 18:02:42 +080013549 if (channels_noise_arr == NULL) {
13550 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13551 goto err;
13552 }
13553 get_noise_ret = get_noise(radio_index, channels_noise_arr, channels_num);
developer9ce44382023-06-28 11:09:37 +080013554 }
developer86035662023-06-28 19:21:12 +080013555
developera3511852023-06-14 14:12:59 +080013556 ret = fgets(line, sizeof(line), f);
13557 while (ret != NULL) {
13558 if(strstr(line, "BSS") != NULL) { // new neighbor info
13559 // The SSID field is not in the first field. So, we should store whole BSS informations and the filter flag.
13560 // And we will determine whether we need the previous BSS infomation when parsing the next BSS field or end of while loop.
13561 // 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 +080013562
developera3511852023-06-14 14:12:59 +080013563 if (!filter_BSS) {
13564 index++;
13565 wifi_neighbor_ap2_t *tmp;
13566 tmp = realloc(scan_array, sizeof(wifi_neighbor_ap2_t)*(index+1));
13567 if (tmp == NULL) { // no more memory to use
13568 index--;
13569 wifi_dbg_printf("%s: realloc failed\n", __func__);
13570 break;
13571 }
13572 scan_array = tmp;
13573 }
13574 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
developer72fb0bb2023-01-11 09:46:29 +080013575
developera3511852023-06-14 14:12:59 +080013576 filter_BSS = false;
developer86035662023-06-28 19:21:12 +080013577 if (sscanf(line, "BSS %17s", scan_array[index].ap_BSSID) != 1) {
13578 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13579 goto err;
13580 }
developerc79e9172023-06-06 19:48:03 +080013581 memset(scan_array[index].ap_Mode, 0, sizeof(scan_array[index].ap_Mode));
developera3511852023-06-14 14:12:59 +080013582 memcpy(scan_array[index].ap_Mode, "Infrastructure", strlen("Infrastructure"));
developerc79e9172023-06-06 19:48:03 +080013583 memset(scan_array[index].ap_SecurityModeEnabled, 0, sizeof(scan_array[index].ap_SecurityModeEnabled));
developera3511852023-06-14 14:12:59 +080013584 memcpy(scan_array[index].ap_SecurityModeEnabled, "None", strlen("None"));
developerc79e9172023-06-06 19:48:03 +080013585 memset(scan_array[index].ap_EncryptionMode, 0, sizeof(scan_array[index].ap_EncryptionMode));
developera3511852023-06-14 14:12:59 +080013586 memcpy(scan_array[index].ap_EncryptionMode, "None", strlen("None"));
13587 } else if (strstr(line, "freq") != NULL) {
developer86035662023-06-28 19:21:12 +080013588 if (sscanf(line," freq: %d", &freq) != 1) {
13589 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +080013590 //goto err;
developer86035662023-06-28 19:21:12 +080013591 }
developera3511852023-06-14 14:12:59 +080013592 scan_array[index].ap_Channel = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080013593
developera3511852023-06-14 14:12:59 +080013594 if (freq >= 2412 && freq <= 2484) {
developerc79e9172023-06-06 19:48:03 +080013595 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013596 memcpy(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz"));
developerc79e9172023-06-06 19:48:03 +080013597 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013598 memcpy(scan_array[index].ap_SupportedStandards, "b,g", strlen("b,g"));
developerc79e9172023-06-06 19:48:03 +080013599 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013600 memcpy(scan_array[index].ap_OperatingStandards, "g", strlen("g"));
13601 }
13602 else if (freq >= 5160 && freq <= 5805) {
developerc79e9172023-06-06 19:48:03 +080013603 memset(scan_array[index].ap_OperatingFrequencyBand, 0, sizeof(scan_array[index].ap_OperatingFrequencyBand));
developera3511852023-06-14 14:12:59 +080013604 memcpy(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz"));
developerc79e9172023-06-06 19:48:03 +080013605 memset(scan_array[index].ap_SupportedStandards, 0, sizeof(scan_array[index].ap_SupportedStandards));
developera3511852023-06-14 14:12:59 +080013606 memcpy(scan_array[index].ap_SupportedStandards, "a", strlen("a"));
developerc79e9172023-06-06 19:48:03 +080013607 memset(scan_array[index].ap_OperatingStandards, 0, sizeof(scan_array[index].ap_OperatingStandards));
developera3511852023-06-14 14:12:59 +080013608 memcpy(scan_array[index].ap_OperatingStandards, "a", strlen("a"));
13609 }
developer72fb0bb2023-01-11 09:46:29 +080013610
developera3511852023-06-14 14:12:59 +080013611 scan_array[index].ap_Noise = 0;
13612 if (get_noise_ret == RETURN_OK) {
13613 for (int i = 0; i < channels_num; i++) {
13614 if (scan_array[index].ap_Channel == channels_noise_arr[i].channel) {
13615 scan_array[index].ap_Noise = channels_noise_arr[i].noise;
13616 break;
13617 }
13618 }
13619 }
13620 } else if (strstr(line, "beacon interval") != NULL) {
developer86035662023-06-28 19:21:12 +080013621 if (sscanf(line," beacon interval: %d TUs", &(scan_array[index].ap_BeaconPeriod)) != 1) {
13622 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13623 goto err;
13624 }
developera3511852023-06-14 14:12:59 +080013625 } else if (strstr(line, "signal") != NULL) {
developer86035662023-06-28 19:21:12 +080013626 if (sscanf(line," signal: %d", &(scan_array[index].ap_SignalStrength)) != 1) {
13627 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13628 goto err;
13629 }
developera3511852023-06-14 14:12:59 +080013630 } else if (strstr(line,"SSID") != NULL) {
developer86035662023-06-28 19:21:12 +080013631 if (sscanf(line," SSID: %63s", scan_array[index].ap_SSID) != 1) {
13632 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +080013633 //goto err;
developer86035662023-06-28 19:21:12 +080013634 }
developera3511852023-06-14 14:12:59 +080013635 if (filter_enable && strcmp(scan_array[index].ap_SSID, filter_SSID) != 0) {
13636 filter_BSS = true;
13637 }
13638 } else if (strstr(line, "Supported rates") != NULL) {
13639 char SRate[80] = {0}, *tmp = NULL;
13640 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080013641 len = strlen(line);
13642 if (len >= sizeof(SRate)) {
13643 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013644 goto err;
developer32f2a182023-06-27 19:50:41 +080013645 }
13646 strncpy(SRate, line, len);
developera3511852023-06-14 14:12:59 +080013647 tmp = strtok(SRate, ":");
developer86035662023-06-28 19:21:12 +080013648 if (tmp == NULL)
13649 goto err;
developera3511852023-06-14 14:12:59 +080013650 tmp = strtok(NULL, ":");
developer86035662023-06-28 19:21:12 +080013651 if (tmp == NULL)
13652 goto err;
13653
developer32f2a182023-06-27 19:50:41 +080013654 len = strlen(tmp);
13655 if (len >= sizeof(buf)) {
13656 wifi_debug(DEBUG_ERROR, "not enough room in buf\n");
developer86035662023-06-28 19:21:12 +080013657 goto err;
developer32f2a182023-06-27 19:50:41 +080013658 }
13659 strncpy(buf, tmp, len);
developera3511852023-06-14 14:12:59 +080013660 memset(SRate, 0, sizeof(SRate));
developer72fb0bb2023-01-11 09:46:29 +080013661
developera3511852023-06-14 14:12:59 +080013662 tmp = strtok(buf, " \n");
13663 while (tmp != NULL) {
developer32f2a182023-06-27 19:50:41 +080013664 if ((sizeof(SRate) - strlen(SRate)) <= strlen(tmp)) {
13665 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013666 goto err;
developer32f2a182023-06-27 19:50:41 +080013667 }
13668 strncat(SRate, tmp, sizeof(SRate) - strlen(SRate) - 1);
developera3511852023-06-14 14:12:59 +080013669 if (SRate[strlen(SRate) - 1] == '*') {
13670 SRate[strlen(SRate) - 1] = '\0';
13671 }
developer32f2a182023-06-27 19:50:41 +080013672 if ((sizeof(SRate) - strlen(SRate)) <= 1) {
13673 wifi_debug(DEBUG_ERROR, "not enough room in SRate\n");
developer86035662023-06-28 19:21:12 +080013674 goto err;
developer32f2a182023-06-27 19:50:41 +080013675 }
13676 strncat(SRate, ",", sizeof(SRate) - strlen(SRate) - 1);
developer72fb0bb2023-01-11 09:46:29 +080013677
developera3511852023-06-14 14:12:59 +080013678 tmp = strtok(NULL, " \n");
13679 }
13680 SRate[strlen(SRate) - 1] = '\0';
developer32f2a182023-06-27 19:50:41 +080013681 len = strlen(SRate);
13682 if (len >= sizeof(scan_array[index].ap_SupportedDataTransferRates)) {
13683 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedDataTransferRates\n");
developer86035662023-06-28 19:21:12 +080013684 goto err;
developer32f2a182023-06-27 19:50:41 +080013685 }
13686 strncpy(scan_array[index].ap_SupportedDataTransferRates, SRate, len);
13687 scan_array[index].ap_SupportedDataTransferRates[len] = '\0';
developera3511852023-06-14 14:12:59 +080013688 } else if (strstr(line, "DTIM") != NULL) {
developer86035662023-06-28 19:21:12 +080013689 if (sscanf(line,"DTIM Period %u", &(scan_array[index].ap_DTIMPeriod)) != 1) {
13690 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
developerb14b3462023-07-01 18:02:42 +080013691 //goto err;
developer86035662023-06-28 19:21:12 +080013692 }
developera3511852023-06-14 14:12:59 +080013693 } else if (strstr(line, "VHT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013694 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13695 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013696 goto err;
developer32f2a182023-06-27 19:50:41 +080013697 }
13698 strncat(scan_array[index].ap_SupportedStandards, ",ac",
13699 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13700 memcpy(scan_array[index].ap_OperatingStandards, "ac", 2);
13701 scan_array[index].ap_OperatingStandards[2] = '\0';
developera3511852023-06-14 14:12:59 +080013702 } else if (strstr(line, "HT capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013703 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 2) {
13704 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013705 goto err;
developer32f2a182023-06-27 19:50:41 +080013706 }
13707 strncat(scan_array[index].ap_SupportedStandards, ",n",
13708 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13709 memcpy(scan_array[index].ap_OperatingStandards, "n", 1);
13710 scan_array[index].ap_OperatingStandards[1] = '\0';
developera3511852023-06-14 14:12:59 +080013711 } else if (strstr(line, "VHT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013712 if (fgets(line, sizeof(line), f) == NULL) {
13713 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13714 goto err;
13715 }
13716 if (sscanf(line," * channel width: %d", &vht_channel_width) != 1) {
13717 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13718 goto err;
13719 }
developera3511852023-06-14 14:12:59 +080013720 if(vht_channel_width == 1) {
developere40952c2023-06-15 18:46:43 +080013721 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT80");
developera3511852023-06-14 14:12:59 +080013722 } else {
developere40952c2023-06-15 18:46:43 +080013723 res = snprintf(scan_array[index].ap_OperatingChannelBandwidth, sizeof(scan_array[index].ap_OperatingChannelBandwidth), "11AC_VHT40");
developera3511852023-06-14 14:12:59 +080013724 }
developere40952c2023-06-15 18:46:43 +080013725 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13726 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013727 goto err;
developer32f2a182023-06-27 19:50:41 +080013728 }
developere40952c2023-06-15 18:46:43 +080013729
developera3511852023-06-14 14:12:59 +080013730 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
13731 continue;
13732 } else if (strstr(line, "HT operation") != NULL) {
developer86035662023-06-28 19:21:12 +080013733 if (fgets(line, sizeof(line), f) == NULL) {
13734 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13735 goto err;
13736 }
developer86035662023-06-28 19:21:12 +080013737 if (sscanf(line," * secondary channel offset: %127s", buf) != 1) {
13738 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13739 goto err;
13740 }
developera3511852023-06-14 14:12:59 +080013741 if (!strcmp(buf, "above")) {
13742 //40Mhz +
developere40952c2023-06-15 18:46:43 +080013743 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 +080013744 }
13745 else if (!strcmp(buf, "below")) {
13746 //40Mhz -
developere40952c2023-06-15 18:46:43 +080013747 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 +080013748 } else {
13749 //20Mhz
developere40952c2023-06-15 18:46:43 +080013750 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 +080013751 }
developere40952c2023-06-15 18:46:43 +080013752 if (os_snprintf_error(sizeof(scan_array[index].ap_OperatingChannelBandwidth), res)) {
13753 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developer86035662023-06-28 19:21:12 +080013754 goto err;
developer32f2a182023-06-27 19:50:41 +080013755 }
developere40952c2023-06-15 18:46:43 +080013756
developera3511852023-06-14 14:12:59 +080013757 if (strstr(line, "BSS") != NULL) // prevent to get the next neighbor information
13758 continue;
13759 } else if (strstr(line, "HE capabilities") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013760 if ((sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards)) <= 3) {
13761 wifi_debug(DEBUG_ERROR, "not enough room in scan_array[index].ap_SupportedStandards\n");
developer86035662023-06-28 19:21:12 +080013762 goto err;
developer32f2a182023-06-27 19:50:41 +080013763 }
13764 strncat(scan_array[index].ap_SupportedStandards, ",ax",
13765 sizeof(scan_array[index].ap_SupportedStandards) - strlen(scan_array[index].ap_SupportedStandards) - 1);
13766 memcpy(scan_array[index].ap_OperatingStandards, "ax", 2);
13767 scan_array[index].ap_OperatingStandards[2] = '\0';
developerc14d83a2023-06-29 20:09:42 +080013768 if (fgets(line, sizeof(line), f) == NULL) {
13769 wifi_debug(DEBUG_ERROR, "fgets fail\n");
13770 goto err;
13771 }
developera3511852023-06-14 14:12:59 +080013772 if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "2.4GHz", strlen("2.4GHz")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013773 if (strstr(line, "HE40/2.4GHz") != NULL) {
13774 len = strlen("11AXHE40PLUS");
13775 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE40PLUS", len);
13776 } else {
13777 len = strlen("11AXHE20");
13778 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE20", len);
13779 }
13780 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013781 } else if (strncmp(scan_array[index].ap_OperatingFrequencyBand, "5GHz", strlen("5GHz")) == 0) {
13782 if (strstr(line, "HE80/5GHz") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013783 len = strlen("11AXHE80");
13784 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE80", len);
13785 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
developera3511852023-06-14 14:12:59 +080013786 ret = fgets(line, sizeof(line), f);
13787 } else
13788 continue;
developer32f2a182023-06-27 19:50:41 +080013789 if (strstr(line, "HE160/5GHz") != NULL) {
13790 len = strlen("11AXHE160");
13791 memcpy(scan_array[index].ap_OperatingChannelBandwidth, "11AXHE160", len);
13792 scan_array[index].ap_OperatingChannelBandwidth[len] = '\0';
13793 }
developera3511852023-06-14 14:12:59 +080013794 }
13795 continue;
13796 } else if (strstr(line, "WPA") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013797 memcpy(scan_array[index].ap_SecurityModeEnabled, "WPA", 3);
13798 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013799 } else if (strstr(line, "RSN") != NULL) {
developer32f2a182023-06-27 19:50:41 +080013800 memcpy(scan_array[index].ap_SecurityModeEnabled, "RSN", 3);
13801 scan_array[index].ap_SecurityModeEnabled[3] = '\0';
developera3511852023-06-14 14:12:59 +080013802 } else if (strstr(line, "Group cipher") != NULL) {
developer86035662023-06-28 19:21:12 +080013803 if (sscanf(line, " * Group cipher: %63s", scan_array[index].ap_EncryptionMode) != 1) {
13804 wifi_debug(DEBUG_ERROR, "sscanf fail\n");
13805 goto err;
13806 }
developera3511852023-06-14 14:12:59 +080013807 if (strncmp(scan_array[index].ap_EncryptionMode, "CCMP", strlen("CCMP")) == 0) {
developer32f2a182023-06-27 19:50:41 +080013808 memcpy(scan_array[index].ap_EncryptionMode, "AES", 3);
13809 scan_array[index].ap_EncryptionMode[3] = '\0';
developera3511852023-06-14 14:12:59 +080013810 }
13811 }
13812 ret = fgets(line, sizeof(line), f);
13813 }
developer72fb0bb2023-01-11 09:46:29 +080013814
developera3511852023-06-14 14:12:59 +080013815 if (!filter_BSS) {
13816 *output_array_size = index + 1;
13817 } else {
13818 memset(&(scan_array[index]), 0, sizeof(wifi_neighbor_ap2_t));
13819 *output_array_size = index;
13820 }
13821 *neighbor_ap_array = scan_array;
13822 pclose(f);
13823 free(channels_noise_arr);
13824 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
13825 return RETURN_OK;
developer86035662023-06-28 19:21:12 +080013826err:
developerc14d83a2023-06-29 20:09:42 +080013827 pclose(f);
developer86035662023-06-28 19:21:12 +080013828 free(channels_noise_arr);
developerc14d83a2023-06-29 20:09:42 +080013829 if (scan_array)
13830 free(scan_array);
developer86035662023-06-28 19:21:12 +080013831 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013832}
13833
13834INT wifi_getApAssociatedDeviceStats(
developera3511852023-06-14 14:12:59 +080013835 INT apIndex,
13836 mac_address_t *clientMacAddress,
13837 wifi_associated_dev_stats_t *associated_dev_stats,
13838 u64 *handle)
developer72fb0bb2023-01-11 09:46:29 +080013839{
developera3511852023-06-14 14:12:59 +080013840 wifi_associated_dev_stats_t *dev_stats = associated_dev_stats;
13841 char interface_name[50] = {0};
13842 char cmd[1024] = {0};
13843 char mac_str[18] = {0};
13844 char *key = NULL;
13845 char *val = NULL;
13846 FILE *f = NULL;
13847 char *line = NULL;
13848 size_t len = 0;
developer75bd10c2023-06-27 11:34:08 +080013849 int res;
developer72fb0bb2023-01-11 09:46:29 +080013850
developera3511852023-06-14 14:12:59 +080013851 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
13852 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
13853 return RETURN_ERR;
13854 }
developer72fb0bb2023-01-11 09:46:29 +080013855
developer32f2a182023-06-27 19:50:41 +080013856 res = snprintf(mac_str, sizeof(mac_str), "%x:%x:%x:%x:%x:%x",
13857 (*clientMacAddress)[0], (*clientMacAddress)[1], (*clientMacAddress)[2],
13858 (*clientMacAddress)[3], (*clientMacAddress)[4], (*clientMacAddress)[5]);
developer75bd10c2023-06-27 11:34:08 +080013859 if (os_snprintf_error(sizeof(mac_str), res)) {
13860 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13861 return RETURN_ERR;
13862 }
13863
13864 res = snprintf(cmd, sizeof(cmd), "iw dev %s station get %s | grep 'rx\\|tx' | tr -d '\t'", interface_name, mac_str);
13865 if (os_snprintf_error(sizeof(cmd), res)) {
13866 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13867 return RETURN_ERR;
13868 }
13869
developera3511852023-06-14 14:12:59 +080013870 if((f = popen(cmd, "r")) == NULL) {
13871 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
13872 return RETURN_ERR;
13873 }
developer72fb0bb2023-01-11 09:46:29 +080013874
developera3511852023-06-14 14:12:59 +080013875 while ((getline(&line, &len, f)) != -1) {
13876 key = strtok(line,":");
developer37646972023-06-29 10:58:43 +080013877 if (key == NULL)
13878 continue;
developera3511852023-06-14 14:12:59 +080013879 val = strtok(NULL,":");
developer37646972023-06-29 10:58:43 +080013880 if (val == NULL)
13881 continue;
developer72fb0bb2023-01-11 09:46:29 +080013882
developerb61d3362023-06-29 14:10:19 +080013883 if(!strncmp(key,"rx bytes",8))
13884 if (sscanf(val, "%llu", &dev_stats->cli_rx_bytes) != 1) {
13885 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080013886 continue;
developerb61d3362023-06-29 14:10:19 +080013887 }
13888 if(!strncmp(key,"tx bytes",8))
13889 if (sscanf(val, "%llu", &dev_stats->cli_tx_bytes) != 1) {
13890 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
developer37646972023-06-29 10:58:43 +080013891 continue;
developerb61d3362023-06-29 14:10:19 +080013892 }
developer37646972023-06-29 10:58:43 +080013893 if(!strncmp(key,"rx packets",10)) {
13894 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
13895 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13896 continue;
13897 }
13898 }
13899 if(!strncmp(key,"tx packets",10)) {
13900 if (sscanf(val, "%llu", &dev_stats->cli_tx_frames) == EOF) {
13901 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13902 continue;
13903 }
13904 }
13905 if(!strncmp(key,"tx retries",10)) {
13906 if (sscanf(val, "%llu", &dev_stats->cli_tx_retries) == EOF) {
13907 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13908 continue;
13909 }
13910 }
13911 if(!strncmp(key,"tx failed",9)) {
13912 if (sscanf(val, "%llu", &dev_stats->cli_tx_errors) == EOF) {
13913 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13914 continue;
13915 }
13916 }
13917 if(!strncmp(key,"rx drop misc",13)) {
13918 if (sscanf(val, "%llu", &dev_stats->cli_rx_errors) == EOF) {
13919 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13920 continue;
13921 }
developerd14dff12023-06-28 22:47:44 +080013922 }
developera3511852023-06-14 14:12:59 +080013923 if(!strncmp(key,"rx bitrate",10)) {
13924 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080013925 if (val == NULL)
13926 continue;
developer37646972023-06-29 10:58:43 +080013927 if (sscanf(val, "%lf", &dev_stats->cli_rx_rate) == EOF) {
13928 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13929 continue;
13930 }
developera3511852023-06-14 14:12:59 +080013931 }
13932 if(!strncmp(key,"tx bitrate",10)) {
13933 val = strtok(val, " ");
developerc14d83a2023-06-29 20:09:42 +080013934 if (val == NULL)
13935 continue;
developer37646972023-06-29 10:58:43 +080013936 if (sscanf(val, "%lf", &dev_stats->cli_tx_rate) == EOF) {
13937 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
13938 continue;
13939 }
developera3511852023-06-14 14:12:59 +080013940 }
13941 }
13942 free(line);
13943 pclose(f);
13944 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013945}
13946
13947INT wifi_getSSIDNameStatus(INT apIndex, CHAR *output_string)
13948{
developera3511852023-06-14 14:12:59 +080013949 char interface_name[IF_NAME_SIZE] = {0};
13950 char cmd[MAX_CMD_SIZE] = {0}, buf[32] = {0};
developere40952c2023-06-15 18:46:43 +080013951 int res;
developer72fb0bb2023-01-11 09:46:29 +080013952
developera3511852023-06-14 14:12:59 +080013953 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer7e4a2a62023-04-06 19:56:03 +080013954
developera3511852023-06-14 14:12:59 +080013955 if (NULL == output_string)
13956 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080013957
developera3511852023-06-14 14:12:59 +080013958 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
13959 return RETURN_ERR;
developer7e4a2a62023-04-06 19:56:03 +080013960
developere40952c2023-06-15 18:46:43 +080013961 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s get_config | grep ^ssid | cut -d '=' -f2 | tr -d '\\n'", interface_name);
13962 if (os_snprintf_error(sizeof(cmd), res)) {
13963 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13964 return RETURN_ERR;
13965 }
developera3511852023-06-14 14:12:59 +080013966 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080013967
developera3511852023-06-14 14:12:59 +080013968 //size of SSID name restricted to value less than 32 bytes
developere40952c2023-06-15 18:46:43 +080013969 res = snprintf(output_string, 32, "%s", buf);
13970 if (os_snprintf_error(32, res)) {
13971 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
13972 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080013973 }
developera3511852023-06-14 14:12:59 +080013974 WIFI_ENTRY_EXIT_DEBUG("Exit %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080013975
developera3511852023-06-14 14:12:59 +080013976 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080013977}
13978
13979INT wifi_getApMacAddressControlMode(INT apIndex, INT *output_filterMode)
13980{
developer2edaf012023-05-24 14:24:53 +080013981 char *mac_arry_buf = NULL;
13982 INT policy = -1;
13983 INT buf_size = 1024;
developer72fb0bb2023-01-11 09:46:29 +080013984
developer2edaf012023-05-24 14:24:53 +080013985 mac_arry_buf = malloc(buf_size);
13986 if (!mac_arry_buf) {
13987 wifi_debug(DEBUG_ERROR,"malloc mac_arry_buf fails\n");
developer7e4a2a62023-04-06 19:56:03 +080013988 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080013989 }
13990 memset(mac_arry_buf, 0, buf_size);
13991 if (mtk_wifi_getApAclDevices(apIndex, mac_arry_buf, buf_size) != RETURN_OK) {
13992 wifi_debug(DEBUG_ERROR,"mtk_wifi_getApAclDevices get fails\n");
13993 goto err;
13994 }
13995 /*
13996 mtk format to get policy:
13997 "policy=1
13998 00:11:22:33:44:55
13999 00:11:22:33:44:66
14000 "
14001 */
14002 if (strlen(mac_arry_buf) < strlen("policy=1") || sscanf(mac_arry_buf, "policy=%01d", &policy) != 1) {
14003 wifi_debug(DEBUG_ERROR,"mac_arry_buf(%s) invalid\n", mac_arry_buf);
14004 goto err;
14005 }
14006 if (!(policy >=0 && policy <= 2)){
14007 wifi_debug(DEBUG_ERROR,"policy(%d) is invalid\n", policy);
14008 goto err;
14009 }
14010 *output_filterMode = policy;
14011 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), success\n", *output_filterMode);
14012 free(mac_arry_buf);
14013 mac_arry_buf = NULL;
14014 return RETURN_OK;
14015err:
14016 free(mac_arry_buf);
14017 mac_arry_buf = NULL;
14018 wifi_debug(DEBUG_NOTICE, "output_filterMode(%d), fails\n", *output_filterMode);
14019 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014020}
14021
developer2edaf012023-05-24 14:24:53 +080014022
developer72fb0bb2023-01-11 09:46:29 +080014023INT wifi_getApAssociatedDeviceDiagnosticResult2(INT apIndex,wifi_associated_dev2_t **associated_dev_array,UINT *output_array_size)
14024{
developera3511852023-06-14 14:12:59 +080014025 FILE *fp = NULL;
14026 char str[MAX_BUF_SIZE] = {0};
14027 int wificlientindex = 0 ;
14028 int count = 0;
14029 int signalstrength = 0;
14030 int arr[MACADDRESS_SIZE] = {0};
14031 unsigned char mac[MACADDRESS_SIZE] = {0};
14032 UINT wifi_count = 0;
14033 char pipeCmd[MAX_CMD_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014034 int res;
developer72fb0bb2023-01-11 09:46:29 +080014035
developera3511852023-06-14 14:12:59 +080014036 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14037 *output_array_size = 0;
14038 *associated_dev_array = NULL;
14039 char interface_name[50] = {0};
developer72fb0bb2023-01-11 09:46:29 +080014040
developera3511852023-06-14 14:12:59 +080014041 if(wifi_getApName(apIndex, interface_name) != RETURN_OK) {
14042 wifi_dbg_printf("%s: wifi_getApName failed\n", __FUNCTION__);
14043 return RETURN_ERR;
14044 }
developer72fb0bb2023-01-11 09:46:29 +080014045
developer75bd10c2023-06-27 11:34:08 +080014046 res = snprintf(pipeCmd, sizeof(pipeCmd), "iw dev %s station dump | grep %s | wc -l", interface_name, interface_name);
14047 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14048 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14049 return RETURN_ERR;
14050 }
developera3511852023-06-14 14:12:59 +080014051 fp = popen(pipeCmd, "r");
14052 if (fp == NULL)
14053 {
14054 printf("Failed to run command inside function %s\n",__FUNCTION__ );
14055 return RETURN_ERR;
14056 }
developer72fb0bb2023-01-11 09:46:29 +080014057
developera3511852023-06-14 14:12:59 +080014058 /* Read the output a line at a time - output it. */
developer86035662023-06-28 19:21:12 +080014059 if (fgets(str, sizeof(str)-1, fp) == NULL) {
14060 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14061 pclose(fp);
14062 return RETURN_ERR;
14063 }
developera3511852023-06-14 14:12:59 +080014064 wifi_count = (unsigned int) atoi ( str );
14065 *output_array_size = wifi_count;
14066 wifi_dbg_printf(" In rdkb hal ,Wifi Client Counts and index %d and %d \n",*output_array_size,apIndex);
14067 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014068
developera3511852023-06-14 14:12:59 +080014069 if(wifi_count == 0)
14070 {
14071 return RETURN_OK;
14072 }
14073 else
14074 {
14075 wifi_associated_dev2_t* temp = NULL;
14076 temp = (wifi_associated_dev2_t*)calloc(wifi_count, sizeof(wifi_associated_dev2_t));
14077 *associated_dev_array = temp;
14078 if(temp == NULL)
14079 {
14080 printf("Error Statement. Insufficient memory \n");
14081 return RETURN_ERR;
14082 }
developer72fb0bb2023-01-11 09:46:29 +080014083
developer33f13ba2023-07-12 16:19:06 +080014084 res = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", interface_name);
14085 if (res) {
14086 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080014087 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014088 }
developere40952c2023-06-15 18:46:43 +080014089
developera3511852023-06-14 14:12:59 +080014090 fp = fopen("/tmp/AssociatedDevice_Stats.txt", "r");
14091 if(fp == NULL)
14092 {
14093 printf("/tmp/AssociatedDevice_Stats.txt not exists \n");
14094 return RETURN_ERR;
14095 }
developere75ba632023-06-29 16:03:33 +080014096 if (fclose(fp) == EOF) {
14097 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14098 return RETURN_ERR;
14099 }
developer72fb0bb2023-01-11 09:46:29 +080014100
developer86035662023-06-28 19:21:12 +080014101 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep Station | cut -d ' ' -f 2");
14102 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14103 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14104 return RETURN_ERR;
14105 }
developera3511852023-06-14 14:12:59 +080014106 fp = popen(pipeCmd, "r");
14107 if(fp)
14108 {
14109 for(count =0 ; count < wifi_count; count++)
14110 {
developer86035662023-06-28 19:21:12 +080014111 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14112 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14113 pclose(fp);
14114 return RETURN_ERR;
14115 }
developera3511852023-06-14 14:12:59 +080014116 if( MACADDRESS_SIZE == sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]) )
14117 {
14118 for( wificlientindex = 0; wificlientindex < MACADDRESS_SIZE; ++wificlientindex )
14119 {
14120 mac[wificlientindex] = (unsigned char) arr[wificlientindex];
developer72fb0bb2023-01-11 09:46:29 +080014121
developera3511852023-06-14 14:12:59 +080014122 }
14123 memcpy(temp[count].cli_MACAddress,mac,(sizeof(unsigned char))*6);
14124 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]);
14125 }
14126 temp[count].cli_AuthenticationState = 1; //TODO
14127 temp[count].cli_Active = 1; //TODO
14128 }
14129 pclose(fp);
14130 }
developer72fb0bb2023-01-11 09:46:29 +080014131
developera3511852023-06-14 14:12:59 +080014132 //Updating RSSI per client
developer86035662023-06-28 19:21:12 +080014133 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep signal | tr -s ' ' | cut -d ' ' -f 2 > /tmp/wifi_signalstrength.txt");
14134 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14135 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14136 return RETURN_ERR;
14137 }
14138
developera3511852023-06-14 14:12:59 +080014139 fp = popen(pipeCmd, "r");
14140 if(fp)
14141 {
14142 pclose(fp);
14143 }
14144 fp = popen("cat /tmp/wifi_signalstrength.txt | tr -s ' ' | cut -f 2","r");
14145 if(fp)
14146 {
14147 for(count =0 ; count < wifi_count ;count++)
14148 {
developer86035662023-06-28 19:21:12 +080014149 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14150 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14151 pclose(fp);
14152 return RETURN_ERR;
14153 }
developera3511852023-06-14 14:12:59 +080014154 signalstrength = atoi(str);
14155 temp[count].cli_RSSI = signalstrength;
14156 }
14157 pclose(fp);
14158 }
developer72fb0bb2023-01-11 09:46:29 +080014159
14160
developera3511852023-06-14 14:12:59 +080014161 //LastDataDownlinkRate
developer86035662023-06-28 19:21:12 +080014162 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep 'tx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Send.txt");
14163 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14164 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14165 return RETURN_ERR;
14166 }
developera3511852023-06-14 14:12:59 +080014167 fp = popen(pipeCmd, "r");
14168 if (fp)
14169 {
14170 pclose(fp);
14171 }
14172 fp = popen("cat /tmp/Ass_Bitrate_Send.txt | tr -s ' ' | cut -f 2", "r");
14173 if (fp)
14174 {
developerc14d83a2023-06-29 20:09:42 +080014175 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014176 for (count = 0; count < wifi_count; count++)
14177 {
developer86035662023-06-28 19:21:12 +080014178 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14179 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14180 pclose(fp);
14181 return RETURN_ERR;
14182 }
developerc14d83a2023-06-29 20:09:42 +080014183 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14184 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14185 }
14186 temp[count].cli_LastDataDownlinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014187 temp[count].cli_LastDataDownlinkRate = (temp[count].cli_LastDataDownlinkRate * 1024); //Mbps -> Kbps
14188 }
14189 pclose(fp);
14190 }
developer72fb0bb2023-01-11 09:46:29 +080014191
developera3511852023-06-14 14:12:59 +080014192 //LastDataUplinkRate
developer86035662023-06-28 19:21:12 +080014193 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /tmp/AssociatedDevice_Stats.txt | grep 'rx bitrate' | tr -s ' ' | cut -d ' ' -f 2 > /tmp/Ass_Bitrate_Received.txt");
14194 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14195 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14196 return RETURN_ERR;
14197 }
developera3511852023-06-14 14:12:59 +080014198 fp = popen(pipeCmd, "r");
14199 if (fp)
14200 {
14201 pclose(fp);
14202 }
14203 fp = popen("cat /tmp/Ass_Bitrate_Received.txt | tr -s ' ' | cut -f 2", "r");
14204 if (fp)
14205 {
developerc14d83a2023-06-29 20:09:42 +080014206 unsigned long tmp_u;
developera3511852023-06-14 14:12:59 +080014207 for (count = 0; count < wifi_count; count++)
14208 {
developer86035662023-06-28 19:21:12 +080014209 if (fgets(str, MAX_BUF_SIZE, fp) == NULL) {
14210 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14211 pclose(fp);
14212 return RETURN_ERR;
14213 }
developerc14d83a2023-06-29 20:09:42 +080014214 if (hal_strtoul(str, 10, &tmp_u) < 0) {
14215 wifi_debug(DEBUG_ERROR, "strtol fail\n");
14216 }
14217 temp[count].cli_LastDataUplinkRate = tmp_u;
developera3511852023-06-14 14:12:59 +080014218 temp[count].cli_LastDataUplinkRate = (temp[count].cli_LastDataUplinkRate * 1024); //Mbps -> Kbps
14219 }
14220 pclose(fp);
14221 }
14222 }
14223 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14224 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014225
14226}
14227
14228INT wifi_getSSIDTrafficStats2(INT ssidIndex,wifi_ssidTrafficStats2_t *output_struct)
14229{
developera3511852023-06-14 14:12:59 +080014230 FILE *fp = NULL;
14231 char interface_name[50] = {0};
14232 char pipeCmd[128] = {0};
14233 char str[256] = {0};
14234 wifi_ssidTrafficStats2_t *out = output_struct;
developer75bd10c2023-06-27 11:34:08 +080014235 int res;
developerd14dff12023-06-28 22:47:44 +080014236 unsigned int recv;
developer72fb0bb2023-01-11 09:46:29 +080014237
developera3511852023-06-14 14:12:59 +080014238 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
14239 if (!output_struct)
14240 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014241
developera3511852023-06-14 14:12:59 +080014242 memset(out, 0, sizeof(wifi_ssidTrafficStats2_t));
14243 if (wifi_GetInterfaceName(ssidIndex, interface_name) != RETURN_OK)
14244 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080014245 res = snprintf(pipeCmd, sizeof(pipeCmd), "cat /proc/net/dev | grep %s", interface_name);
14246 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14247 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14248 return RETURN_ERR;
14249 }
developer72fb0bb2023-01-11 09:46:29 +080014250
developera3511852023-06-14 14:12:59 +080014251 fp = popen(pipeCmd, "r");
14252 if (fp == NULL) {
developer86035662023-06-28 19:21:12 +080014253 wifi_debug(DEBUG_ERROR, "%s: popen failed\n", __func__);
14254 return RETURN_ERR;
14255 }
14256 if (fgets(str, sizeof(str), fp) == NULL) {
14257 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14258 pclose(fp);
developera3511852023-06-14 14:12:59 +080014259 return RETURN_ERR;
14260 }
developer86035662023-06-28 19:21:12 +080014261
developera3511852023-06-14 14:12:59 +080014262 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014263
developera3511852023-06-14 14:12:59 +080014264 if (strlen(str) == 0) // interface not exist
14265 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014266
developerd14dff12023-06-28 22:47:44 +080014267 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 +080014268 &out->ssid_DiscardedPacketsReceived, &out->ssid_BytesSent, &out->ssid_PacketsSent, &out->ssid_ErrorsSent, &out->ssid_DiscardedPacketsSent);
developerd14dff12023-06-28 22:47:44 +080014269 if (recv != 8) {
14270 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
14271 return RETURN_ERR;
14272 }
developer72fb0bb2023-01-11 09:46:29 +080014273
developera3511852023-06-14 14:12:59 +080014274 memset(str, 0, sizeof(str));
developer75bd10c2023-06-27 11:34:08 +080014275
14276 res = snprintf(pipeCmd, sizeof(pipeCmd), "tail -n1 /proc/net/netstat");
14277 if (os_snprintf_error(sizeof(pipeCmd), res)) {
14278 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14279 return RETURN_ERR;
14280 }
developera3511852023-06-14 14:12:59 +080014281 fp = popen(pipeCmd, "r");
14282 if (fp == NULL) {
developer75bd10c2023-06-27 11:34:08 +080014283 wifi_debug(DEBUG_ERROR, "popen failed\n");
developera3511852023-06-14 14:12:59 +080014284 return RETURN_ERR;
14285 }
developer86035662023-06-28 19:21:12 +080014286
14287 if (fgets(str, sizeof(str), fp) == NULL) {
14288 wifi_debug(DEBUG_ERROR, "fgets fail\n");
14289 pclose(fp);
14290 return RETURN_ERR;
14291 }
developer72fb0bb2023-01-11 09:46:29 +080014292
developer37646972023-06-29 10:58:43 +080014293 if (sscanf(str, "%*[^:]: %lu %lu %lu %lu", &out->ssid_MulticastPacketsReceived, &out->ssid_MulticastPacketsSent, &out->ssid_BroadcastPacketsRecevied, \
14294 &out->ssid_BroadcastPacketsSent) == EOF)
14295 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
developera3511852023-06-14 14:12:59 +080014296 pclose(fp);
developer72fb0bb2023-01-11 09:46:29 +080014297
developera3511852023-06-14 14:12:59 +080014298 out->ssid_UnicastPacketsSent = out->ssid_PacketsSent - out->ssid_MulticastPacketsSent - out->ssid_BroadcastPacketsSent - out->ssid_DiscardedPacketsSent;
14299 out->ssid_UnicastPacketsReceived = out->ssid_PacketsReceived - out->ssid_MulticastPacketsReceived - out->ssid_BroadcastPacketsRecevied - out->ssid_DiscardedPacketsReceived;
developer72fb0bb2023-01-11 09:46:29 +080014300
developera3511852023-06-14 14:12:59 +080014301 // Not supported
14302 output_struct->ssid_RetransCount = 0;
14303 output_struct->ssid_FailedRetransCount = 0;
14304 output_struct->ssid_RetryCount = 0;
14305 output_struct->ssid_MultipleRetryCount = 0;
14306 output_struct->ssid_ACKFailureCount = 0;
14307 output_struct->ssid_AggregatedPacketCount = 0;
developer72fb0bb2023-01-11 09:46:29 +080014308
developera3511852023-06-14 14:12:59 +080014309 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014310}
14311
14312//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).
14313INT wifi_getApIsolationEnable(INT apIndex, BOOL *output)
14314{
developera3511852023-06-14 14:12:59 +080014315 char output_val[16]={'\0'};
14316 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080014317 int res;
developer72fb0bb2023-01-11 09:46:29 +080014318
developera3511852023-06-14 14:12:59 +080014319 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14320 if (!output)
14321 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080014322
14323 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
14324 if (os_snprintf_error(sizeof(config_file), res)) {
14325 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14326 return RETURN_ERR;
14327 }
developera3511852023-06-14 14:12:59 +080014328 wifi_hostapdRead(config_file, "ap_isolate", output_val, sizeof(output_val));
developer72fb0bb2023-01-11 09:46:29 +080014329
developera3511852023-06-14 14:12:59 +080014330 if( strcmp(output_val,"1") == 0 )
14331 *output = TRUE;
14332 else
14333 *output = FALSE;
14334 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014335
developera3511852023-06-14 14:12:59 +080014336 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014337}
14338
14339INT wifi_setApIsolationEnable(INT apIndex, BOOL enable)
14340{
developera3511852023-06-14 14:12:59 +080014341 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14342 char string[MAX_BUF_SIZE]={'\0'};
14343 char config_file[MAX_BUF_SIZE] = {0};
14344 struct params params;
developer75bd10c2023-06-27 11:34:08 +080014345 int res;
developer72fb0bb2023-01-11 09:46:29 +080014346
developer32f2a182023-06-27 19:50:41 +080014347 string[0] = enable == TRUE ? '1' : '0';
developer72fb0bb2023-01-11 09:46:29 +080014348
developera3511852023-06-14 14:12:59 +080014349 params.name = "ap_isolate";
14350 params.value = string;
developer72fb0bb2023-01-11 09:46:29 +080014351
developer75bd10c2023-06-27 11:34:08 +080014352 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
14353 if (os_snprintf_error(sizeof(config_file), res)) {
14354 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14355 return RETURN_ERR;
14356 }
14357
developera3511852023-06-14 14:12:59 +080014358 wifi_hostapdWrite(config_file,&params,1);
14359 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014360
developera3511852023-06-14 14:12:59 +080014361 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014362}
14363
14364INT wifi_getApManagementFramePowerControl(INT apIndex, INT *output_dBm)
14365{
developera3511852023-06-14 14:12:59 +080014366 char mgmtpwr_file[32] = {0};
14367 char cmd[64] = {0};
14368 char buf[32]={0};
developere40952c2023-06-15 18:46:43 +080014369 int res;
developerc14d83a2023-06-29 20:09:42 +080014370 long int tmp;
developera1255e42023-05-13 17:45:02 +080014371
developera3511852023-06-14 14:12:59 +080014372 if (NULL == output_dBm)
14373 return RETURN_ERR;
developere40952c2023-06-15 18:46:43 +080014374 res = snprintf(mgmtpwr_file, sizeof(mgmtpwr_file), "%s%d.txt", MGMT_POWER_CTRL, apIndex);
14375 if (os_snprintf_error(sizeof(mgmtpwr_file), res)) {
14376 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14377 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014378 }
developere40952c2023-06-15 18:46:43 +080014379
14380 res = snprintf(cmd, sizeof(cmd), "cat %s 2> /dev/null", mgmtpwr_file);
14381 if (os_snprintf_error(sizeof(cmd), res)) {
14382 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14383 return RETURN_ERR;
14384 }
developera3511852023-06-14 14:12:59 +080014385 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080014386 if (strlen(buf) > 0) {
14387 if (hal_strtol(buf, 10, &tmp) < 0) {
14388 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080014389 }
developerc14d83a2023-06-29 20:09:42 +080014390 *output_dBm = tmp;
developerd14dff12023-06-28 22:47:44 +080014391 } else
developera1255e42023-05-13 17:45:02 +080014392 *output_dBm = 23;
developera3511852023-06-14 14:12:59 +080014393 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014394}
14395
14396INT wifi_setApManagementFramePowerControl(INT wlanIndex, INT dBm)
14397{
developera1255e42023-05-13 17:45:02 +080014398 char interface_name[16] = {0};
developera1255e42023-05-13 17:45:02 +080014399 char mgmt_pwr_file[128]={0};
14400 FILE *f = NULL;
developerfead3972023-05-25 20:15:02 +080014401 int if_idx, ret = 0;
14402 struct nl_msg *msg = NULL;
14403 struct nlattr * msg_data = NULL;
14404 struct mtk_nl80211_param param;
14405 struct unl unl_ins;
14406 char power[16] = {0};
developere40952c2023-06-15 18:46:43 +080014407 int res;
developera1255e42023-05-13 17:45:02 +080014408
14409 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
14410
14411 if (wifi_GetInterfaceName(wlanIndex, interface_name) != RETURN_OK)
14412 return RETURN_ERR;
developerfead3972023-05-25 20:15:02 +080014413
14414 if_idx = if_nametoindex(interface_name);
14415 /*init mtk nl80211 vendor cmd*/
14416 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_TXPOWER;
14417 param.if_type = NL80211_ATTR_IFINDEX;
14418 param.if_idx = if_idx;
14419
14420 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14421 if (ret) {
14422 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14423 return RETURN_ERR;
14424 }
14425
14426 /*add mtk vendor cmd data*/
developere40952c2023-06-15 18:46:43 +080014427 res = snprintf(power, sizeof(power), "%d", dBm);
14428 if (os_snprintf_error(sizeof(power), res)) {
14429 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14430 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014431 }
developere40952c2023-06-15 18:46:43 +080014432
developerfead3972023-05-25 20:15:02 +080014433 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_TXPWR_MGMT, strlen(power), power)) {
14434 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14435 nlmsg_free(msg);
14436 goto err;
14437 }
14438
14439 /*send mtk nl80211 vendor msg*/
14440 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14441 if (ret) {
14442 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14443 goto err;
14444 }
14445
14446 /*deinit mtk nl80211 vendor msg*/
14447 mtk_nl80211_deint(&unl_ins);
14448 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14449
developere40952c2023-06-15 18:46:43 +080014450 res = snprintf(mgmt_pwr_file, sizeof(mgmt_pwr_file), "%s%d.txt", MGMT_POWER_CTRL, wlanIndex);
14451 if (os_snprintf_error(sizeof(mgmt_pwr_file), res)) {
14452 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14453 return RETURN_ERR;
developerb758dfd2023-06-21 17:32:07 +080014454 }
developere40952c2023-06-15 18:46:43 +080014455
developera1255e42023-05-13 17:45:02 +080014456 f = fopen(mgmt_pwr_file, "w");
14457 if (f == NULL) {
developerc14d83a2023-06-29 20:09:42 +080014458 wifi_debug(DEBUG_ERROR, "%s: fopen failed\n", __func__);
developera1255e42023-05-13 17:45:02 +080014459 return RETURN_ERR;
14460 }
14461 fprintf(f, "%d", dBm);
developerc14d83a2023-06-29 20:09:42 +080014462 if (fclose(f) == EOF)
14463 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14464
developer72fb0bb2023-01-11 09:46:29 +080014465 return RETURN_OK;
developerfead3972023-05-25 20:15:02 +080014466err:
14467 mtk_nl80211_deint(&unl_ins);
14468 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14469 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014470}
14471INT wifi_getRadioDcsChannelMetrics(INT radioIndex,wifi_channelMetrics_t *input_output_channelMetrics_array,INT size)
14472{
developera3511852023-06-14 14:12:59 +080014473 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014474}
14475INT wifi_setRadioDcsDwelltime(INT radioIndex, INT ms)
14476{
developera3511852023-06-14 14:12:59 +080014477 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014478}
14479INT wifi_getRadioDcsDwelltime(INT radioIndex, INT *ms)
14480{
developera3511852023-06-14 14:12:59 +080014481 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014482}
14483INT wifi_setRadioDcsScanning(INT radioIndex, BOOL enable)
14484{
developera3511852023-06-14 14:12:59 +080014485 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014486}
14487INT wifi_setBSSTransitionActivation(UINT apIndex, BOOL activate)
14488{
developera3511852023-06-14 14:12:59 +080014489 char config_file[MAX_BUF_SIZE] = {0};
14490 struct params list;
developere40952c2023-06-15 18:46:43 +080014491 int res;
developer72fb0bb2023-01-11 09:46:29 +080014492
developera3511852023-06-14 14:12:59 +080014493 list.name = "bss_transition";
14494 list.value = activate?"1":"0";
developere40952c2023-06-15 18:46:43 +080014495 res = snprintf(config_file, sizeof(config_file), "%s%d.conf",CONFIG_PREFIX,apIndex);
14496 if (os_snprintf_error(sizeof(config_file), res)) {
14497 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14498 return RETURN_ERR;
14499 }
developera3511852023-06-14 14:12:59 +080014500 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080014501
developera3511852023-06-14 14:12:59 +080014502 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014503}
14504wifi_apAuthEvent_callback apAuthEvent_cb = NULL;
14505
14506void wifi_apAuthEvent_callback_register(wifi_apAuthEvent_callback callback_proc)
14507{
developera3511852023-06-14 14:12:59 +080014508 return;
developer72fb0bb2023-01-11 09:46:29 +080014509}
14510
14511INT wifi_setApCsaDeauth(INT apIndex, INT mode)
14512{
developera3511852023-06-14 14:12:59 +080014513 // TODO Implement me!
14514 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014515}
14516
14517INT wifi_setApScanFilter(INT apIndex, INT mode, CHAR *essid)
14518{
developera3511852023-06-14 14:12:59 +080014519 char file_name[128] = {0};
14520 FILE *f = NULL;
14521 int max_num_radios = 0;
developer75bd10c2023-06-27 11:34:08 +080014522 int res, ret;
developer72fb0bb2023-01-11 09:46:29 +080014523
developera3511852023-06-14 14:12:59 +080014524 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014525
developera3511852023-06-14 14:12:59 +080014526 wifi_getMaxRadioNumber(&max_num_radios);
14527 if (essid == NULL || strlen(essid) == 0 || apIndex == -1) {
14528 for (int index = 0; index < max_num_radios; index++) {
developere40952c2023-06-15 18:46:43 +080014529 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, index);
14530 if (os_snprintf_error(sizeof(file_name), res)) {
14531 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14532 return RETURN_ERR;
14533 }
14534
developera3511852023-06-14 14:12:59 +080014535 f = fopen(file_name, "w");
14536 if (f == NULL)
14537 return RETURN_ERR;
14538 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014539 if (mode) {
14540 ret = fprintf(f, "%s", essid);
14541 if (ret < 0)
14542 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14543 }
developer72fb0bb2023-01-11 09:46:29 +080014544
developerd14dff12023-06-28 22:47:44 +080014545 if (fclose(f) != 0) {
14546 wifi_debug(DEBUG_ERROR, "fclose fail\n");
14547 return RETURN_ERR;
14548 }
developera3511852023-06-14 14:12:59 +080014549 }
14550 } else { // special case, need to set AP's SSID as filter for each radio.
developere40952c2023-06-15 18:46:43 +080014551 res = snprintf(file_name, sizeof(file_name), "%s%d.txt", ESSID_FILE, apIndex);
14552 if (os_snprintf_error(sizeof(file_name), res)) {
14553 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14554 return RETURN_ERR;
14555 }
14556
developera3511852023-06-14 14:12:59 +080014557 f = fopen(file_name, "w");
14558 if (f == NULL)
14559 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014560
developera3511852023-06-14 14:12:59 +080014561 // For mode == 0 is to disable filter, just don't write to the file.
developer75bd10c2023-06-27 11:34:08 +080014562 if (mode) {
14563 ret = fprintf(f, "%s", essid);
14564 if (ret < 0)
14565 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
14566 }
developer72fb0bb2023-01-11 09:46:29 +080014567
developer37646972023-06-29 10:58:43 +080014568 if (fclose(f) == EOF) {
14569 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
14570 return RETURN_ERR;
14571 }
developera3511852023-06-14 14:12:59 +080014572 }
developer72fb0bb2023-01-11 09:46:29 +080014573
developera3511852023-06-14 14:12:59 +080014574 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
14575 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014576}
14577
14578INT wifi_pushRadioChannel(INT radioIndex, UINT channel)
14579{
developera3511852023-06-14 14:12:59 +080014580 // TODO Implement me!
14581 //Apply wifi_pushRadioChannel() instantly
14582 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014583}
14584
14585INT wifi_setRadioStatsEnable(INT radioIndex, BOOL enable)
14586{
developera3511852023-06-14 14:12:59 +080014587 // TODO Implement me!
14588 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014589}
14590
developer72fb0bb2023-01-11 09:46:29 +080014591
developera3511852023-06-14 14:12:59 +080014592static int tidStats_callback(struct nl_msg *msg, void *arg) {
14593 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14594 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
14595 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
14596 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1],*tidattr;
14597 int rem , tid_index = 0;
developer72fb0bb2023-01-11 09:46:29 +080014598
developera3511852023-06-14 14:12:59 +080014599 wifi_associated_dev_tid_stats_t *out = (wifi_associated_dev_tid_stats_t*)arg;
14600 wifi_associated_dev_tid_entry_t *stats_entry;
developer72fb0bb2023-01-11 09:46:29 +080014601
developera3511852023-06-14 14:12:59 +080014602 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
14603 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
14604 };
14605 static struct nla_policy tid_policy[NL80211_TID_STATS_MAX + 1] = {
14606 [NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
14607 };
developer72fb0bb2023-01-11 09:46:29 +080014608
developera3511852023-06-14 14:12:59 +080014609 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
14610 genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080014611
developer72fb0bb2023-01-11 09:46:29 +080014612
developera3511852023-06-14 14:12:59 +080014613 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014614 wifi_debug(DEBUG_ERROR, "station stats missing!\n");
developera3511852023-06-14 14:12:59 +080014615 return NL_SKIP;
14616 }
developer72fb0bb2023-01-11 09:46:29 +080014617
developera3511852023-06-14 14:12:59 +080014618 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
14619 tb[NL80211_ATTR_STA_INFO],
14620 stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014621 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014622 return NL_SKIP;
14623 }
developer72fb0bb2023-01-11 09:46:29 +080014624
developera3511852023-06-14 14:12:59 +080014625 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14626 nla_for_each_nested(tidattr, sinfo[NL80211_STA_INFO_TID_STATS], rem)
14627 {
14628 stats_entry = &out->tid_array[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014629
developera3511852023-06-14 14:12:59 +080014630 stats_entry->tid = tid_index;
14631 stats_entry->ac = _tid_ac_index_get[tid_index];
developer72fb0bb2023-01-11 09:46:29 +080014632
developera3511852023-06-14 14:12:59 +080014633 if(sinfo[NL80211_STA_INFO_TID_STATS])
14634 {
14635 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,tidattr, tid_policy)) {
14636 printf("failed to parse nested stats attributes!");
14637 return NL_SKIP;
14638 }
14639 }
14640 if(stats_info[NL80211_TID_STATS_TX_MSDU])
14641 stats_entry->num_msdus = (unsigned long long)nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
developer72fb0bb2023-01-11 09:46:29 +080014642
developera3511852023-06-14 14:12:59 +080014643 if(tid_index < (PS_MAX_TID - 1))
14644 tid_index++;
14645 }
14646 }
14647 //ToDo: sum_time_ms, ewma_time_ms
14648 return NL_SKIP;
14649}
developer72fb0bb2023-01-11 09:46:29 +080014650
developera3511852023-06-14 14:12:59 +080014651INT wifi_getApAssociatedDeviceTidStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_tid_stats_t *tid_stats, ULLONG *handle)
14652{
14653 Netlink nl;
14654 char if_name[IF_NAME_SIZE];
14655 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080014656 int res;
developer72fb0bb2023-01-11 09:46:29 +080014657
developera3511852023-06-14 14:12:59 +080014658 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
14659 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014660
developere40952c2023-06-15 18:46:43 +080014661 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
14662 if (os_snprintf_error(sizeof(if_name), res)) {
14663 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14664 return RETURN_ERR;
14665 }
developer72fb0bb2023-01-11 09:46:29 +080014666
developera3511852023-06-14 14:12:59 +080014667 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080014668
developera3511852023-06-14 14:12:59 +080014669 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080014670 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080014671 return -1;
14672 }
developer72fb0bb2023-01-11 09:46:29 +080014673
developera3511852023-06-14 14:12:59 +080014674 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080014675
developera3511852023-06-14 14:12:59 +080014676 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080014677 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080014678 nlfree(&nl);
14679 return -2;
14680 }
developer72fb0bb2023-01-11 09:46:29 +080014681
developera3511852023-06-14 14:12:59 +080014682 genlmsg_put(msg,
14683 NL_AUTO_PID,
14684 NL_AUTO_SEQ,
14685 nl.id,
14686 0,
14687 0,
14688 NL80211_CMD_GET_STATION,
14689 0);
developer72fb0bb2023-01-11 09:46:29 +080014690
developera3511852023-06-14 14:12:59 +080014691 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
14692 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
14693 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,tidStats_callback,tid_stats);
14694 nl_send_auto_complete(nl.socket, msg);
14695 nl_recvmsgs(nl.socket, nl.cb);
14696 nlmsg_free(msg);
14697 nlfree(&nl);
14698 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014699}
14700
14701
14702INT wifi_startNeighborScan(INT apIndex, wifi_neighborScanMode_t scan_mode, INT dwell_time, UINT chan_num, UINT *chan_list)
14703{
developera3511852023-06-14 14:12:59 +080014704 char interface_name[16] = {0};
14705 char cmd[128]={0};
14706 char buf[128]={0};
14707 int freq = 0;
developere40952c2023-06-15 18:46:43 +080014708 int res;
developer72fb0bb2023-01-11 09:46:29 +080014709
developera3511852023-06-14 14:12:59 +080014710 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014711
developera3511852023-06-14 14:12:59 +080014712 // full mode is used to scan all channels.
14713 // multiple channels is ambiguous, iw can not set multiple frequencies in one time.
14714 if (scan_mode != WIFI_RADIO_SCAN_MODE_FULL)
14715 ieee80211_channel_to_frequency(chan_list[0], &freq);
developer72fb0bb2023-01-11 09:46:29 +080014716
developera3511852023-06-14 14:12:59 +080014717 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
14718 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014719
developera3511852023-06-14 14:12:59 +080014720 if (freq)
developere40952c2023-06-15 18:46:43 +080014721 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 +080014722 else
developere40952c2023-06-15 18:46:43 +080014723 res = snprintf(cmd, sizeof(cmd), "iw dev %s scan trigger duration %d", interface_name, dwell_time);
14724 if (os_snprintf_error(sizeof(cmd), res)) {
14725 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
14726 return RETURN_ERR;
14727 }
developer72fb0bb2023-01-11 09:46:29 +080014728
developera3511852023-06-14 14:12:59 +080014729 _syscmd(cmd, buf, sizeof(buf));
14730 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080014731
developera3511852023-06-14 14:12:59 +080014732 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014733}
14734
14735
14736INT wifi_steering_setGroup(UINT steeringgroupIndex, wifi_steering_apConfig_t *cfg_2, wifi_steering_apConfig_t *cfg_5)
14737{
developera3511852023-06-14 14:12:59 +080014738 // TODO Implement me!
14739 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014740}
14741
14742INT wifi_steering_clientSet(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_steering_clientConfig_t *config)
14743{
developera3511852023-06-14 14:12:59 +080014744 // TODO Implement me!
14745 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014746}
14747
14748INT wifi_steering_clientRemove(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14749{
developera3511852023-06-14 14:12:59 +080014750 // TODO Implement me!
14751 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014752}
14753
14754INT wifi_steering_clientMeasure(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac)
14755{
developera3511852023-06-14 14:12:59 +080014756 // TODO Implement me!
14757 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014758}
14759
14760INT wifi_steering_clientDisconnect(UINT steeringgroupIndex, INT apIndex, mac_address_t client_mac, wifi_disconnectType_t type, UINT reason)
14761{
developera3511852023-06-14 14:12:59 +080014762 // TODO Implement me!
14763 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014764}
14765
14766INT wifi_steering_eventRegister(wifi_steering_eventCB_t event_cb)
14767{
developera3511852023-06-14 14:12:59 +080014768 // TODO Implement me!
14769 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014770}
14771
14772INT wifi_steering_eventUnregister(void)
14773{
developera3511852023-06-14 14:12:59 +080014774 // TODO Implement me!
14775 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014776}
14777
14778INT wifi_delApAclDevices(INT apIndex)
14779{
developer7e4a2a62023-04-06 19:56:03 +080014780 char inf_name[IF_NAME_SIZE] = {0};
developer2edaf012023-05-24 14:24:53 +080014781 struct unl unl_ins;
14782 int if_idx = 0, ret = 0;
14783 struct nl_msg *msg = NULL;
14784 struct nlattr * msg_data = NULL;
14785 struct mtk_nl80211_param param;
developer72fb0bb2023-01-11 09:46:29 +080014786
developer7e4a2a62023-04-06 19:56:03 +080014787 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
14788 return RETURN_ERR;
developer2edaf012023-05-24 14:24:53 +080014789 if_idx = if_nametoindex(inf_name);
14790 if (!if_idx) {
14791 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
14792 return RETURN_ERR;
14793 }
14794 /*init mtk nl80211 vendor cmd*/
14795 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
14796 param.if_type = NL80211_ATTR_IFINDEX;
14797 param.if_idx = if_idx;
14798 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
14799 if (ret) {
14800 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
14801 return RETURN_ERR;
14802 }
14803 /*add mtk vendor cmd data*/
14804 if (nla_put_flag(msg, MTK_NL80211_VENDOR_ATTR_ACL_CLEAR_ALL)) {
14805 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
14806 nlmsg_free(msg);
14807 goto err;
14808 }
14809 /*send mtk nl80211 vendor msg*/
14810 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
14811 if (ret) {
14812 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
14813 goto err;
14814 }
14815 /*deinit mtk nl80211 vendor msg*/
14816 mtk_nl80211_deint(&unl_ins);
14817 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
14818 return RETURN_OK;
14819err:
14820 mtk_nl80211_deint(&unl_ins);
14821 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
14822 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014823
developera3511852023-06-14 14:12:59 +080014824 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014825}
14826
developer72fb0bb2023-01-11 09:46:29 +080014827static int rxStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080014828 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14829 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080014830 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
14831 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080014832 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
14833 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080014834
developera3511852023-06-14 14:12:59 +080014835 nla_parse(tb,
14836 NL80211_ATTR_MAX,
14837 genlmsg_attrdata(gnlh, 0),
14838 genlmsg_attrlen(gnlh, 0),
14839 NULL);
developer72fb0bb2023-01-11 09:46:29 +080014840
developera3511852023-06-14 14:12:59 +080014841 if (!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014842 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080014843 return NL_SKIP;
14844 }
developer72fb0bb2023-01-11 09:46:29 +080014845
developera3511852023-06-14 14:12:59 +080014846 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014847 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014848 return NL_SKIP;
14849 }
14850 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080014851
developera3511852023-06-14 14:12:59 +080014852 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080014853
developera3511852023-06-14 14:12:59 +080014854 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
14855 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy )) {
developer75bd10c2023-06-27 11:34:08 +080014856 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080014857 return NL_SKIP;
14858 }
14859 }
developer72fb0bb2023-01-11 09:46:29 +080014860
developera3511852023-06-14 14:12:59 +080014861 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14862 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
14863 printf("failed to parse nested stats attributes!");
14864 return NL_SKIP;
14865 }
14866 }
14867 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
developer72fb0bb2023-01-11 09:46:29 +080014868
developera3511852023-06-14 14:12:59 +080014869 if( nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]) )
14870 {
14871 printf("Type is VHT\n");
14872 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
14873 ((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 +080014874
developera3511852023-06-14 14:12:59 +080014875 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
14876 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 1;
14877 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
14878 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14879 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
14880 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14881 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
14882 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 2;
14883 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]) )
14884 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
14885 } else {
14886 printf(" OFDM or CCK \n");
14887 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bw = 0;
14888 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->nss = 0;
14889 }
14890 }
developer72fb0bb2023-01-11 09:46:29 +080014891
developera3511852023-06-14 14:12:59 +080014892 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
developereff896f2023-05-29 14:52:55 +080014893 if(rinfo[NL80211_RATE_INFO_MCS])
14894 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
14895 }
developera3511852023-06-14 14:12:59 +080014896 if (sinfo[NL80211_STA_INFO_RX_BYTES64])
developereff896f2023-05-29 14:52:55 +080014897 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]);
14898 else if (sinfo[NL80211_STA_INFO_RX_BYTES])
14899 ((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 +080014900
developera3511852023-06-14 14:12:59 +080014901 if (sinfo[NL80211_STA_INFO_TID_STATS]) {
14902 if (stats_info[NL80211_TID_STATS_RX_MSDU])
developereff896f2023-05-29 14:52:55 +080014903 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_RX_MSDU]);
14904 }
developer72fb0bb2023-01-11 09:46:29 +080014905
developereff896f2023-05-29 14:52:55 +080014906 if (sinfo[NL80211_STA_INFO_SIGNAL])
14907 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->rssi_combined = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
14908 //Assigning 0 for RETRIES ,PPDUS and MPDUS as we dont have rx retries attribute in libnl_3.3.0
14909 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->retries = 0;
14910 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->ppdus = 0;
14911 ((wifi_associated_dev_rate_info_rx_stats_t*)arg)->msdus = 0;
14912 //rssi_array need to be filled
14913 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080014914}
developer72fb0bb2023-01-11 09:46:29 +080014915
14916INT wifi_getApAssociatedDeviceRxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_rx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
14917{
developera3511852023-06-14 14:12:59 +080014918 Netlink nl;
14919 char if_name[32];
14920 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
14921 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080014922
developera3511852023-06-14 14:12:59 +080014923 *output_array_size = sizeof(wifi_associated_dev_rate_info_rx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080014924
developera3511852023-06-14 14:12:59 +080014925 if (*output_array_size <= 0)
14926 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014927
developera3511852023-06-14 14:12:59 +080014928 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080014929
developera3511852023-06-14 14:12:59 +080014930 if (nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080014931 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080014932 return 0;
14933 }
developer72fb0bb2023-01-11 09:46:29 +080014934
developera3511852023-06-14 14:12:59 +080014935 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080014936
developera3511852023-06-14 14:12:59 +080014937 if (!msg) {
developer75bd10c2023-06-27 11:34:08 +080014938 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080014939 nlfree(&nl);
14940 return 0;
14941 }
developer72fb0bb2023-01-11 09:46:29 +080014942
developera3511852023-06-14 14:12:59 +080014943 genlmsg_put(msg,
14944 NL_AUTO_PID,
14945 NL_AUTO_SEQ,
14946 nl.id,
14947 0,
14948 0,
14949 NL80211_CMD_GET_STATION,
14950 0);
developer72fb0bb2023-01-11 09:46:29 +080014951
developera3511852023-06-14 14:12:59 +080014952 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, *clientMacAddress);
14953 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
14954 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, rxStatsInfo_callback, stats_array);
14955 nl_send_auto_complete(nl.socket, msg);
14956 nl_recvmsgs(nl.socket, nl.cb);
14957 nlmsg_free(msg);
14958 nlfree(&nl);
14959 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080014960}
14961
developer72fb0bb2023-01-11 09:46:29 +080014962static int txStatsInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080014963 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14964 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
developerc14d83a2023-06-29 20:09:42 +080014965 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1] = {NULL};
14966 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1] = {NULL};
developera3511852023-06-14 14:12:59 +080014967 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1];
14968 char mac_addr[20],dev[20];
developer72fb0bb2023-01-11 09:46:29 +080014969
developera3511852023-06-14 14:12:59 +080014970 nla_parse(tb,
14971 NL80211_ATTR_MAX,
14972 genlmsg_attrdata(gnlh, 0),
14973 genlmsg_attrlen(gnlh, 0),
14974 NULL);
developer72fb0bb2023-01-11 09:46:29 +080014975
developera3511852023-06-14 14:12:59 +080014976 if(!tb[NL80211_ATTR_STA_INFO]) {
developer75bd10c2023-06-27 11:34:08 +080014977 wifi_debug(DEBUG_ERROR, "sta stats missing!\n");
developera3511852023-06-14 14:12:59 +080014978 return NL_SKIP;
14979 }
developer72fb0bb2023-01-11 09:46:29 +080014980
developera3511852023-06-14 14:12:59 +080014981 if(nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,tb[NL80211_ATTR_STA_INFO], stats_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014982 wifi_debug(DEBUG_ERROR, "failed to parse nested attributes!\n");
developera3511852023-06-14 14:12:59 +080014983 return NL_SKIP;
14984 }
developer72fb0bb2023-01-11 09:46:29 +080014985
developera3511852023-06-14 14:12:59 +080014986 mac_addr_ntoa(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
developer72fb0bb2023-01-11 09:46:29 +080014987
developera3511852023-06-14 14:12:59 +080014988 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080014989
developera3511852023-06-14 14:12:59 +080014990 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
14991 if(nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
developer75bd10c2023-06-27 11:34:08 +080014992 wifi_debug(DEBUG_ERROR, "failed to parse nested rate attributes!");
developera3511852023-06-14 14:12:59 +080014993 return NL_SKIP;
14994 }
14995 }
developer72fb0bb2023-01-11 09:46:29 +080014996
developera3511852023-06-14 14:12:59 +080014997 if(sinfo[NL80211_STA_INFO_TID_STATS])
14998 {
14999 if(nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,sinfo[NL80211_STA_INFO_TID_STATS], tid_policy)) {
15000 printf("failed to parse nested stats attributes!");
15001 return NL_SKIP;
15002 }
15003 }
15004 if (tb[NL80211_ATTR_VHT_CAPABILITY]) {
15005 if(nla_data(tb[NL80211_ATTR_VHT_CAPABILITY]))
15006 {
15007 printf("Type is VHT\n");
15008 if(rinfo[NL80211_RATE_INFO_VHT_NSS])
15009 ((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 +080015010
developera3511852023-06-14 14:12:59 +080015011 if(rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
15012 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 1;
15013 if(rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
15014 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15015 if(rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
15016 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15017 if(rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
15018 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 2;
15019 if((rinfo[NL80211_RATE_INFO_10_MHZ_WIDTH]) || (rinfo[NL80211_RATE_INFO_5_MHZ_WIDTH]))
15020 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15021 }
15022 else
15023 {
15024 printf(" OFDM or CCK \n");
15025 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bw = 0;
15026 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->nss = 0;
15027 }
15028 }
developer72fb0bb2023-01-11 09:46:29 +080015029
developera3511852023-06-14 14:12:59 +080015030 if(sinfo[NL80211_STA_INFO_TX_BITRATE]) {
15031 if(rinfo[NL80211_RATE_INFO_MCS])
15032 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
15033 }
developer72fb0bb2023-01-11 09:46:29 +080015034
developera3511852023-06-14 14:12:59 +080015035 if(sinfo[NL80211_STA_INFO_TX_BYTES64])
15036 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]);
15037 else if (sinfo[NL80211_STA_INFO_TX_BYTES])
15038 ((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 +080015039
developera3511852023-06-14 14:12:59 +080015040 //Assigning 0 for mpdus and ppdus , as we do not have attributes in netlink
15041 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
15042 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->mpdus = 0;
developer72fb0bb2023-01-11 09:46:29 +080015043
developera3511852023-06-14 14:12:59 +080015044 if(sinfo[NL80211_STA_INFO_TID_STATS]) {
15045 if(stats_info[NL80211_TID_STATS_TX_MSDU])
15046 ((wifi_associated_dev_rate_info_tx_stats_t*)arg)->msdus = nla_get_u64(stats_info[NL80211_TID_STATS_TX_MSDU]);
15047 }
developer72fb0bb2023-01-11 09:46:29 +080015048
developera3511852023-06-14 14:12:59 +080015049 if(sinfo[NL80211_STA_INFO_TX_RETRIES])
15050 ((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 +080015051
developera3511852023-06-14 14:12:59 +080015052 if(sinfo[NL80211_STA_INFO_TX_FAILED] && sinfo[NL80211_STA_INFO_TX_PACKETS])
15053 ((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 +080015054
developera3511852023-06-14 14:12:59 +080015055 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015056}
developer72fb0bb2023-01-11 09:46:29 +080015057
15058INT wifi_getApAssociatedDeviceTxStatsResult(INT radioIndex, mac_address_t *clientMacAddress, wifi_associated_dev_rate_info_tx_stats_t **stats_array, UINT *output_array_size, ULLONG *handle)
15059{
developera3511852023-06-14 14:12:59 +080015060 Netlink nl;
15061 char if_name[IF_NAME_SIZE];
15062 char interface_name[IF_NAME_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015063 int res;
15064
developera3511852023-06-14 14:12:59 +080015065 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK)
15066 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015067
developera3511852023-06-14 14:12:59 +080015068 *output_array_size = sizeof(wifi_associated_dev_rate_info_tx_stats_t);
developer72fb0bb2023-01-11 09:46:29 +080015069
developera3511852023-06-14 14:12:59 +080015070 if (*output_array_size <= 0)
15071 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015072
developere40952c2023-06-15 18:46:43 +080015073 res = snprintf(if_name, sizeof(if_name), "%s", interface_name);
15074 if (os_snprintf_error(sizeof(if_name), res)) {
15075 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15076 return RETURN_ERR;
15077 }
developer72fb0bb2023-01-11 09:46:29 +080015078
developera3511852023-06-14 14:12:59 +080015079 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015080
developera3511852023-06-14 14:12:59 +080015081 if(nl.id < 0) {
developer75bd10c2023-06-27 11:34:08 +080015082 wifi_debug(DEBUG_ERROR, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015083 return 0;
15084 }
developer72fb0bb2023-01-11 09:46:29 +080015085
developera3511852023-06-14 14:12:59 +080015086 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015087
developera3511852023-06-14 14:12:59 +080015088 if(!msg) {
developer75bd10c2023-06-27 11:34:08 +080015089 wifi_debug(DEBUG_ERROR, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015090 nlfree(&nl);
15091 return 0;
15092 }
developer72fb0bb2023-01-11 09:46:29 +080015093
developera3511852023-06-14 14:12:59 +080015094 genlmsg_put(msg,
15095 NL_AUTO_PID,
15096 NL_AUTO_SEQ,
15097 nl.id,
15098 0,
15099 0,
15100 NL80211_CMD_GET_STATION,
15101 0);
developer72fb0bb2023-01-11 09:46:29 +080015102
developera3511852023-06-14 14:12:59 +080015103 nla_put(msg, NL80211_ATTR_MAC, MAC_ALEN, clientMacAddress);
15104 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15105 nl_cb_set(nl.cb, NL_CB_VALID , NL_CB_CUSTOM, txStatsInfo_callback, stats_array);
15106 nl_send_auto_complete(nl.socket, msg);
15107 nl_recvmsgs(nl.socket, nl.cb);
15108 nlmsg_free(msg);
15109 nlfree(&nl);
15110 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015111}
15112
15113INT wifi_getBSSTransitionActivation(UINT apIndex, BOOL *activate)
15114{
developera3511852023-06-14 14:12:59 +080015115 // TODO Implement me!
15116 char buf[MAX_BUF_SIZE] = {0};
15117 char config_file[MAX_BUF_SIZE] = {0};
developere40952c2023-06-15 18:46:43 +080015118 int res;
developer72fb0bb2023-01-11 09:46:29 +080015119
developere40952c2023-06-15 18:46:43 +080015120 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15121 if (os_snprintf_error(sizeof(config_file), res)) {
15122 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15123 return RETURN_ERR;
15124 }
developera3511852023-06-14 14:12:59 +080015125 wifi_hostapdRead(config_file, "bss_transition", buf, sizeof(buf));
15126 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015127
developera3511852023-06-14 14:12:59 +080015128 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015129}
15130
15131INT wifi_setNeighborReportActivation(UINT apIndex, BOOL activate)
15132{
developera3511852023-06-14 14:12:59 +080015133 char config_file[MAX_BUF_SIZE] = {0};
15134 struct params list;
developer75bd10c2023-06-27 11:34:08 +080015135 int res;
developer72fb0bb2023-01-11 09:46:29 +080015136
developera3511852023-06-14 14:12:59 +080015137 list.name = "rrm_neighbor_report";
15138 list.value = activate?"1":"0";
developer75bd10c2023-06-27 11:34:08 +080015139 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15140 if (os_snprintf_error(sizeof(config_file), res)) {
15141 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15142 return RETURN_ERR;
15143 }
developera3511852023-06-14 14:12:59 +080015144 wifi_hostapdWrite(config_file, &list, 1);
developer72fb0bb2023-01-11 09:46:29 +080015145
developera3511852023-06-14 14:12:59 +080015146 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015147}
15148
15149INT wifi_getNeighborReportActivation(UINT apIndex, BOOL *activate)
15150{
developera3511852023-06-14 14:12:59 +080015151 char buf[32] = {0};
15152 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080015153 int res;
developer72fb0bb2023-01-11 09:46:29 +080015154
developer75bd10c2023-06-27 11:34:08 +080015155 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
15156 if (os_snprintf_error(sizeof(config_file), res)) {
15157 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15158 return RETURN_ERR;
15159 }
developera3511852023-06-14 14:12:59 +080015160 wifi_hostapdRead(config_file, "rrm_neighbor_report", buf, sizeof(buf));
15161 *activate = (strncmp("1",buf,1) == 0);
developer72fb0bb2023-01-11 09:46:29 +080015162
developera3511852023-06-14 14:12:59 +080015163 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015164}
15165#undef HAL_NETLINK_IMPL
15166#ifdef HAL_NETLINK_IMPL
15167static int chanSurveyInfo_callback(struct nl_msg *msg, void *arg) {
developera3511852023-06-14 14:12:59 +080015168 struct nlattr *tb[NL80211_ATTR_MAX + 1];
15169 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
15170 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
15171 char dev[20];
15172 int freq =0 ;
15173 static int i=0;
developer72fb0bb2023-01-11 09:46:29 +080015174
developera3511852023-06-14 14:12:59 +080015175 wifi_channelStats_t_loc *out = (wifi_channelStats_t_loc*)arg;
developer72fb0bb2023-01-11 09:46:29 +080015176
developera3511852023-06-14 14:12:59 +080015177 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
15178 };
developer72fb0bb2023-01-11 09:46:29 +080015179
developera3511852023-06-14 14:12:59 +080015180 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh, 0), NULL);
developer72fb0bb2023-01-11 09:46:29 +080015181
developera3511852023-06-14 14:12:59 +080015182 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
developer72fb0bb2023-01-11 09:46:29 +080015183
developera3511852023-06-14 14:12:59 +080015184 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
15185 fprintf(stderr, "survey data missing!\n");
15186 return NL_SKIP;
15187 }
developer72fb0bb2023-01-11 09:46:29 +080015188
developera3511852023-06-14 14:12:59 +080015189 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,tb[NL80211_ATTR_SURVEY_INFO],survey_policy))
15190 {
15191 fprintf(stderr, "failed to parse nested attributes!\n");
15192 return NL_SKIP;
15193 }
developer72fb0bb2023-01-11 09:46:29 +080015194
15195
developera3511852023-06-14 14:12:59 +080015196 if(out[0].array_size == 1 )
15197 {
15198 if(sinfo[NL80211_SURVEY_INFO_IN_USE])
15199 {
15200 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15201 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15202 out[0].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015203
developera3511852023-06-14 14:12:59 +080015204 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15205 out[0].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15206 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15207 out[0].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15208 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15209 out[0].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15210 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15211 out[0].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15212 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15213 out[0].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15214 if (sinfo[NL80211_SURVEY_INFO_TIME])
15215 out[0].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15216 return NL_STOP;
15217 }
15218 } else {
15219 if ( i <= out[0].array_size ) {
15220 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
15221 freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
15222 out[i].ch_number = ieee80211_frequency_to_channel(freq);
developer72fb0bb2023-01-11 09:46:29 +080015223
developera3511852023-06-14 14:12:59 +080015224 if (sinfo[NL80211_SURVEY_INFO_NOISE])
15225 out[i].ch_noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
15226 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
15227 out[i].ch_utilization_busy_rx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
15228 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
15229 out[i].ch_utilization_busy_tx = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
15230 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
15231 out[i].ch_utilization_busy = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
15232 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
15233 out[i].ch_utilization_busy_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
15234 if (sinfo[NL80211_SURVEY_INFO_TIME])
15235 out[i].ch_utilization_total = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
15236 }
15237 }
developer72fb0bb2023-01-11 09:46:29 +080015238
developera3511852023-06-14 14:12:59 +080015239 i++;
15240 return NL_SKIP;
developer72fb0bb2023-01-11 09:46:29 +080015241}
15242#endif
15243
15244static int ieee80211_channel_to_frequency(int channel, int *freqMHz)
15245{
developera3511852023-06-14 14:12:59 +080015246 char command[MAX_CMD_SIZE], output[MAX_BUF_SIZE];
15247 FILE *fp;
developere40952c2023-06-15 18:46:43 +080015248 int res;
developer72fb0bb2023-01-11 09:46:29 +080015249
developera3511852023-06-14 14:12:59 +080015250 if(access("/tmp/freq-channel-map.txt", F_OK)==-1)
15251 {
15252 printf("Creating Frequency-Channel Map\n");
developer33f13ba2023-07-12 16:19:06 +080015253 v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
developera3511852023-06-14 14:12:59 +080015254 }
developere40952c2023-06-15 18:46:43 +080015255 res = snprintf(command, sizeof(command), "cat /tmp/freq-channel-map.txt | grep '\\[%d\\]$' | cut -d' ' -f1", channel);
15256 if (os_snprintf_error(sizeof(command), res)) {
15257 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15258 return RETURN_ERR;
15259 }
15260
developera3511852023-06-14 14:12:59 +080015261 if((fp = popen(command, "r")))
15262 {
developerc14d83a2023-06-29 20:09:42 +080015263 if (fgets(output, sizeof(output), fp) == NULL) {
15264 wifi_debug(DEBUG_ERROR, "fgets fail\n");
15265 pclose(fp);
15266 return RETURN_ERR;
15267 }
developera3511852023-06-14 14:12:59 +080015268 *freqMHz = atoi(output);
15269 pclose(fp);
15270 }
developer72fb0bb2023-01-11 09:46:29 +080015271
developera3511852023-06-14 14:12:59 +080015272 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015273}
15274
developer2f79c922023-06-02 17:33:42 +080015275static int get_survey_dump_buf(INT radioIndex, int channel, char *buf, size_t bufsz)
developer72fb0bb2023-01-11 09:46:29 +080015276{
developera3511852023-06-14 14:12:59 +080015277 int freqMHz = -1;
15278 char cmd[MAX_CMD_SIZE] = {'\0'};
15279 char interface_name[16] = {0};
developer32f2a182023-06-27 19:50:41 +080015280 int res;
developer72fb0bb2023-01-11 09:46:29 +080015281
developera3511852023-06-14 14:12:59 +080015282 ieee80211_channel_to_frequency(channel, &freqMHz);
15283 if (freqMHz == -1) {
15284 wifi_dbg_printf("%s: failed to get channel frequency for channel: %d\n", __func__, channel);
15285 return -1;
15286 }
developer72fb0bb2023-01-11 09:46:29 +080015287
developer86035662023-06-28 19:21:12 +080015288 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
15289 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
15290 }
developer32f2a182023-06-27 19:50:41 +080015291 res = snprintf(cmd, sizeof(cmd), "iw dev %s survey dump | grep -A5 %d | tr -d '\\t'", interface_name, freqMHz);
15292 if (os_snprintf_error(sizeof(cmd), res)) {
15293 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15294 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080015295 }
developer72fb0bb2023-01-11 09:46:29 +080015296
developera3511852023-06-14 14:12:59 +080015297 if (_syscmd(cmd, buf, bufsz) == RETURN_ERR) {
15298 wifi_dbg_printf("%s: failed to execute '%s' for radioIndex=%d\n", __FUNCTION__, cmd, radioIndex);
15299 return -1;
15300 }
developer72fb0bb2023-01-11 09:46:29 +080015301
developera3511852023-06-14 14:12:59 +080015302 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015303}
15304
15305static int fetch_survey_from_buf(INT radioIndex, const char *buf, wifi_channelStats_t *stats)
15306{
developera3511852023-06-14 14:12:59 +080015307 const char *ptr = buf;
15308 char *key = NULL;
15309 char *val = NULL;
15310 char line[256] = { '\0' };
developer72fb0bb2023-01-11 09:46:29 +080015311
developera3511852023-06-14 14:12:59 +080015312 while ((ptr = get_line_from_str_buf(ptr, line))) {
15313 if (strstr(line, "Frequency")) continue;
developer72fb0bb2023-01-11 09:46:29 +080015314
developera3511852023-06-14 14:12:59 +080015315 key = strtok(line, ":");
developerc14d83a2023-06-29 20:09:42 +080015316 if (key == NULL)
15317 continue;
developera3511852023-06-14 14:12:59 +080015318 val = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080015319 if (val == NULL)
15320 continue;
developera3511852023-06-14 14:12:59 +080015321 wifi_dbg_printf("%s: key='%s' val='%s'\n", __func__, key, val);
developer72fb0bb2023-01-11 09:46:29 +080015322
developera3511852023-06-14 14:12:59 +080015323 if (!strcmp(key, "noise")) {
developer37646972023-06-29 10:58:43 +080015324 if (sscanf(val, "%d", &stats->ch_noise) == EOF)
15325 continue;
developera3511852023-06-14 14:12:59 +080015326 if (stats->ch_noise == 0) {
15327 // Workaround for missing noise information.
15328 // Assume -95 for 2.4G and -103 for 5G
15329 if (radioIndex == 0) stats->ch_noise = -95;
15330 if (radioIndex == 1) stats->ch_noise = -103;
15331 }
15332 }
15333 else if (!strcmp(key, "channel active time")) {
developer37646972023-06-29 10:58:43 +080015334 if (sscanf(val, "%llu", &stats->ch_utilization_total) == EOF)
15335 continue;
developera3511852023-06-14 14:12:59 +080015336 }
15337 else if (!strcmp(key, "channel busy time")) {
developer37646972023-06-29 10:58:43 +080015338 if (sscanf(val, "%llu", &stats->ch_utilization_busy) == EOF)
15339 continue;
developera3511852023-06-14 14:12:59 +080015340 }
15341 else if (!strcmp(key, "channel receive time")) {
developer37646972023-06-29 10:58:43 +080015342 if (sscanf(val, "%llu", &stats->ch_utilization_busy_rx) == EOF)
15343 continue;
developera3511852023-06-14 14:12:59 +080015344 }
15345 else if (!strcmp(key, "channel transmit time")) {
developer37646972023-06-29 10:58:43 +080015346 if (sscanf(val, "%llu", &stats->ch_utilization_busy_tx) == EOF)
15347 continue;
developera3511852023-06-14 14:12:59 +080015348 }
15349 };
developer72fb0bb2023-01-11 09:46:29 +080015350
developera3511852023-06-14 14:12:59 +080015351 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015352}
15353
15354INT wifi_getRadioChannelStats(INT radioIndex,wifi_channelStats_t *input_output_channelStats_array,INT array_size)
15355{
developera3511852023-06-14 14:12:59 +080015356 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080015357#ifdef HAL_NETLINK_IMPL
developera3511852023-06-14 14:12:59 +080015358 Netlink nl;
15359 wifi_channelStats_t_loc local[array_size];
15360 char if_name[32];
developer72fb0bb2023-01-11 09:46:29 +080015361
developera3511852023-06-14 14:12:59 +080015362 local[0].array_size = array_size;
developer72fb0bb2023-01-11 09:46:29 +080015363
developera3511852023-06-14 14:12:59 +080015364 if (wifi_GetInterfaceName(radioIndex, if_name) != RETURN_OK)
15365 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015366
developera3511852023-06-14 14:12:59 +080015367 nl.id = initSock80211(&nl);
developer72fb0bb2023-01-11 09:46:29 +080015368
developera3511852023-06-14 14:12:59 +080015369 if (nl.id < 0) {
developerc14d83a2023-06-29 20:09:42 +080015370 wifi_debug(DEBUG_ERROR,, "Error initializing netlink \n");
developera3511852023-06-14 14:12:59 +080015371 return -1;
15372 }
developer72fb0bb2023-01-11 09:46:29 +080015373
developera3511852023-06-14 14:12:59 +080015374 struct nl_msg* msg = nlmsg_alloc();
developer72fb0bb2023-01-11 09:46:29 +080015375
developera3511852023-06-14 14:12:59 +080015376 if (!msg) {
developerc14d83a2023-06-29 20:09:42 +080015377 wifi_debug(DEBUG_ERROR,, "Failed to allocate netlink message.\n");
developera3511852023-06-14 14:12:59 +080015378 nlfree(&nl);
15379 return -2;
15380 }
developer72fb0bb2023-01-11 09:46:29 +080015381
developera3511852023-06-14 14:12:59 +080015382 genlmsg_put(msg,
15383 NL_AUTO_PID,
15384 NL_AUTO_SEQ,
15385 nl.id,
15386 0,
15387 NLM_F_DUMP,
15388 NL80211_CMD_GET_SURVEY,
15389 0);
developer72fb0bb2023-01-11 09:46:29 +080015390
developera3511852023-06-14 14:12:59 +080015391 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(if_name));
15392 nl_send_auto_complete(nl.socket, msg);
15393 nl_cb_set(nl.cb,NL_CB_VALID,NL_CB_CUSTOM,chanSurveyInfo_callback,local);
15394 nl_recvmsgs(nl.socket, nl.cb);
15395 nlmsg_free(msg);
15396 nlfree(&nl);
15397 //Copying the Values
15398 for(int i=0;i<array_size;i++)
15399 {
15400 input_output_channelStats_array[i].ch_number = local[i].ch_number;
15401 input_output_channelStats_array[i].ch_noise = local[i].ch_noise;
15402 input_output_channelStats_array[i].ch_utilization_busy_rx = local[i].ch_utilization_busy_rx;
15403 input_output_channelStats_array[i].ch_utilization_busy_tx = local[i].ch_utilization_busy_tx;
15404 input_output_channelStats_array[i].ch_utilization_busy = local[i].ch_utilization_busy;
15405 input_output_channelStats_array[i].ch_utilization_busy_ext = local[i].ch_utilization_busy_ext;
15406 input_output_channelStats_array[i].ch_utilization_total = local[i].ch_utilization_total;
15407 //TODO: ch_radar_noise, ch_max_80211_rssi, ch_non_80211_noise, ch_utilization_busy_self
15408 }
developer72fb0bb2023-01-11 09:46:29 +080015409#else
developera3511852023-06-14 14:12:59 +080015410 ULONG channel = 0;
15411 int i;
15412 int number_of_channels = array_size;
15413 char buf[512];
developer72fb0bb2023-01-11 09:46:29 +080015414
developera3511852023-06-14 14:12:59 +080015415 if (number_of_channels == 0) {
15416 if (wifi_getRadioChannel(radioIndex, &channel) != RETURN_OK) {
15417 wifi_dbg_printf("%s: cannot get current channel for radioIndex=%d\n", __func__, radioIndex);
15418 return RETURN_ERR;
15419 }
15420 number_of_channels = 1;
15421 input_output_channelStats_array[0].ch_number = channel;
15422 }
developer72fb0bb2023-01-11 09:46:29 +080015423
developera3511852023-06-14 14:12:59 +080015424 for (i = 0; i < number_of_channels; i++) {
developer72fb0bb2023-01-11 09:46:29 +080015425
developera3511852023-06-14 14:12:59 +080015426 input_output_channelStats_array[i].ch_noise = 0;
15427 input_output_channelStats_array[i].ch_utilization_busy_rx = 0;
15428 input_output_channelStats_array[i].ch_utilization_busy_tx = 0;
15429 input_output_channelStats_array[i].ch_utilization_busy = 0;
15430 input_output_channelStats_array[i].ch_utilization_busy_ext = 0; // XXX: unavailable
15431 input_output_channelStats_array[i].ch_utilization_total = 0;
developer72fb0bb2023-01-11 09:46:29 +080015432
developera3511852023-06-14 14:12:59 +080015433 memset(buf, 0, sizeof(buf));
15434 if (get_survey_dump_buf(radioIndex, input_output_channelStats_array[i].ch_number, buf, sizeof(buf))) {
15435 return RETURN_ERR;
15436 }
15437 if (fetch_survey_from_buf(radioIndex, buf, &input_output_channelStats_array[i])) {
15438 wifi_dbg_printf("%s: cannot fetch survey from buf for radioIndex=%d\n", __func__, radioIndex);
15439 return RETURN_ERR;
15440 }
developer72fb0bb2023-01-11 09:46:29 +080015441
developera3511852023-06-14 14:12:59 +080015442 // XXX: fake missing 'self' counter which is not available in iw survey output
15443 // the 'self' counter (a.k.a 'bss') requires Linux Kernel update
15444 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 +080015445
developera3511852023-06-14 14:12:59 +080015446 input_output_channelStats_array[i].ch_utilization_busy_rx *= 1000;
15447 input_output_channelStats_array[i].ch_utilization_busy_tx *= 1000;
15448 input_output_channelStats_array[i].ch_utilization_busy_self *= 1000;
15449 input_output_channelStats_array[i].ch_utilization_busy *= 1000;
15450 input_output_channelStats_array[i].ch_utilization_total *= 1000;
developer72fb0bb2023-01-11 09:46:29 +080015451
developera3511852023-06-14 14:12:59 +080015452 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",
15453 __func__,
15454 input_output_channelStats_array[i].ch_number,
15455 input_output_channelStats_array[i].ch_noise,
15456 input_output_channelStats_array[i].ch_utilization_total,
15457 input_output_channelStats_array[i].ch_utilization_busy,
15458 input_output_channelStats_array[i].ch_utilization_busy_rx,
15459 input_output_channelStats_array[i].ch_utilization_busy_tx,
15460 input_output_channelStats_array[i].ch_utilization_busy_self,
15461 input_output_channelStats_array[i].ch_utilization_busy_ext);
15462 }
developer72fb0bb2023-01-11 09:46:29 +080015463#endif
developera3511852023-06-14 14:12:59 +080015464 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
15465 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015466}
15467#define HAL_NETLINK_IMPL
15468
15469/* Hostapd events */
15470
15471#ifndef container_of
15472#define offset_of(st, m) ((size_t)&(((st *)0)->m))
15473#define container_of(ptr, type, member) \
developera3511852023-06-14 14:12:59 +080015474 ((type *)((char *)ptr - offset_of(type, member)))
developer72fb0bb2023-01-11 09:46:29 +080015475#endif /* container_of */
15476
15477struct ctrl {
developera3511852023-06-14 14:12:59 +080015478 char sockpath[128];
15479 char sockdir[128];
15480 char bss[IFNAMSIZ];
15481 char reply[4096];
15482 int ssid_index;
15483 void (*cb)(struct ctrl *ctrl, int level, const char *buf, size_t len);
15484 void (*overrun)(struct ctrl *ctrl);
15485 struct wpa_ctrl *wpa;
15486 unsigned int ovfl;
15487 size_t reply_len;
15488 int initialized;
15489 ev_timer retry;
15490 ev_timer watchdog;
15491 ev_stat stat;
15492 ev_io io;
developer72fb0bb2023-01-11 09:46:29 +080015493};
15494static wifi_newApAssociatedDevice_callback clients_connect_cb;
15495static wifi_apDisassociatedDevice_callback clients_disconnect_cb;
15496static struct ctrl wpa_ctrl[MAX_APS];
15497static int initialized;
15498
15499static unsigned int ctrl_get_drops(struct ctrl *ctrl)
15500{
developera3511852023-06-14 14:12:59 +080015501 char cbuf[256] = {};
15502 struct msghdr msg = { .msg_control = cbuf, .msg_controllen = sizeof(cbuf) };
15503 struct cmsghdr *cmsg;
15504 unsigned int ovfl = ctrl->ovfl;
developer86035662023-06-28 19:21:12 +080015505 unsigned int drop = 0;
developer72fb0bb2023-01-11 09:46:29 +080015506
developer86035662023-06-28 19:21:12 +080015507 if (recvmsg(ctrl->io.fd, &msg, MSG_DONTWAIT) < 0)
15508 return drop;
developera3511852023-06-14 14:12:59 +080015509 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
15510 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_RXQ_OVFL)
15511 ovfl = *(unsigned int *)CMSG_DATA(cmsg);
developer72fb0bb2023-01-11 09:46:29 +080015512
developera3511852023-06-14 14:12:59 +080015513 drop = ovfl - ctrl->ovfl;
15514 ctrl->ovfl = ovfl;
developer72fb0bb2023-01-11 09:46:29 +080015515
developera3511852023-06-14 14:12:59 +080015516 return drop;
developer72fb0bb2023-01-11 09:46:29 +080015517}
15518
15519static void ctrl_close(struct ctrl *ctrl)
15520{
developera3511852023-06-14 14:12:59 +080015521 if (ctrl->io.cb)
15522 ev_io_stop(EV_DEFAULT_ &ctrl->io);
15523 if (ctrl->retry.cb)
15524 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15525 if (!ctrl->wpa)
15526 return;
developer72fb0bb2023-01-11 09:46:29 +080015527
developera3511852023-06-14 14:12:59 +080015528 wpa_ctrl_detach(ctrl->wpa);
15529 wpa_ctrl_close(ctrl->wpa);
15530 ctrl->wpa = NULL;
15531 printf("WPA_CTRL: closed index=%d\n", ctrl->ssid_index);
developer72fb0bb2023-01-11 09:46:29 +080015532}
15533
15534static void ctrl_process(struct ctrl *ctrl)
15535{
developera3511852023-06-14 14:12:59 +080015536 const char *str;
15537 int drops;
15538 int level;
developer72fb0bb2023-01-11 09:46:29 +080015539
developera3511852023-06-14 14:12:59 +080015540 /* Example events:
15541 *
15542 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19
15543 * <3>AP-STA-CONNECTED 60:b4:f7:f0:0a:19 keyid=sample_keyid
15544 * <3>AP-STA-DISCONNECTED 60:b4:f7:f0:0a:19
15545 * <3>CTRL-EVENT-CONNECTED - Connection to 00:1d:73:73:88:ea completed [id=0 id_str=]
15546 * <3>CTRL-EVENT-DISCONNECTED bssid=00:1d:73:73:88:ea reason=3 locally_generated=1
15547 */
15548 if (!(str = index(ctrl->reply, '>')))
15549 return;
15550 if (sscanf(ctrl->reply, "<%d>", &level) != 1)
15551 return;
developer72fb0bb2023-01-11 09:46:29 +080015552
developera3511852023-06-14 14:12:59 +080015553 str++;
developer72fb0bb2023-01-11 09:46:29 +080015554
developera3511852023-06-14 14:12:59 +080015555 if (strncmp("AP-STA-CONNECTED ", str, 17) == 0) {
15556 if (!(str = index(ctrl->reply, ' ')))
15557 return;
15558 wifi_associated_dev_t sta;
15559 memset(&sta, 0, sizeof(sta));
developer72fb0bb2023-01-11 09:46:29 +080015560
developere75ba632023-06-29 16:03:33 +080015561 if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
15562 &sta.cli_MACAddress[0], &sta.cli_MACAddress[1], &sta.cli_MACAddress[2],
15563 &sta.cli_MACAddress[3], &sta.cli_MACAddress[4], &sta.cli_MACAddress[5]) == EOF) {
15564 wifi_debug(DEBUG_ERROR, "Unexpected sscanf fail\n");
15565 return;
15566 }
developer72fb0bb2023-01-11 09:46:29 +080015567
developera3511852023-06-14 14:12:59 +080015568 sta.cli_Active=true;
developer72fb0bb2023-01-11 09:46:29 +080015569
developera3511852023-06-14 14:12:59 +080015570 (clients_connect_cb)(ctrl->ssid_index, &sta);
15571 goto handled;
15572 }
developer72fb0bb2023-01-11 09:46:29 +080015573
developera3511852023-06-14 14:12:59 +080015574 if (strncmp("AP-STA-DISCONNECTED ", str, 20) == 0) {
15575 if (!(str = index(ctrl->reply, ' ')))
15576 return;
developer72fb0bb2023-01-11 09:46:29 +080015577
developera3511852023-06-14 14:12:59 +080015578 (clients_disconnect_cb)(ctrl->ssid_index, (char*)str, 0);
15579 goto handled;
15580 }
developer72fb0bb2023-01-11 09:46:29 +080015581
developera3511852023-06-14 14:12:59 +080015582 if (strncmp("CTRL-EVENT-TERMINATING", str, 22) == 0) {
15583 printf("CTRL_WPA: handle TERMINATING event\n");
15584 goto retry;
15585 }
developer72fb0bb2023-01-11 09:46:29 +080015586
developera3511852023-06-14 14:12:59 +080015587 if (strncmp("AP-DISABLED", str, 11) == 0) {
15588 printf("CTRL_WPA: handle AP-DISABLED\n");
15589 goto retry;
15590 }
developer72fb0bb2023-01-11 09:46:29 +080015591
developera3511852023-06-14 14:12:59 +080015592 printf("Event not supported!!\n");
developer72fb0bb2023-01-11 09:46:29 +080015593
15594handled:
15595
developera3511852023-06-14 14:12:59 +080015596 if ((drops = ctrl_get_drops(ctrl))) {
15597 printf("WPA_CTRL: dropped %d messages index=%d\n", drops, ctrl->ssid_index);
15598 if (ctrl->overrun)
15599 ctrl->overrun(ctrl);
15600 }
developer72fb0bb2023-01-11 09:46:29 +080015601
developera3511852023-06-14 14:12:59 +080015602 return;
developer72fb0bb2023-01-11 09:46:29 +080015603
15604retry:
developera3511852023-06-14 14:12:59 +080015605 printf("WPA_CTRL: closing\n");
15606 ctrl_close(ctrl);
15607 printf("WPA_CTRL: retrying from ctrl prcoess\n");
15608 ev_timer_again(EV_DEFAULT_ &ctrl->retry);
developer72fb0bb2023-01-11 09:46:29 +080015609}
15610
15611static void ctrl_ev_cb(EV_P_ struct ev_io *io, int events)
15612{
developera3511852023-06-14 14:12:59 +080015613 struct ctrl *ctrl = container_of(io, struct ctrl, io);
15614 int err;
developer72fb0bb2023-01-11 09:46:29 +080015615
developera3511852023-06-14 14:12:59 +080015616 memset(ctrl->reply, 0, sizeof(ctrl->reply));
15617 ctrl->reply_len = sizeof(ctrl->reply) - 1;
15618 err = wpa_ctrl_recv(ctrl->wpa, ctrl->reply, &ctrl->reply_len);
15619 ctrl->reply[ctrl->reply_len] = 0;
15620 if (err < 0) {
15621 if (errno == EAGAIN || errno == EWOULDBLOCK)
15622 return;
15623 ctrl_close(ctrl);
15624 ev_timer_again(EV_A_ &ctrl->retry);
15625 return;
15626 }
developer72fb0bb2023-01-11 09:46:29 +080015627
developera3511852023-06-14 14:12:59 +080015628 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015629}
15630
15631static int ctrl_open(struct ctrl *ctrl)
15632{
developera3511852023-06-14 14:12:59 +080015633 int fd;
developer72fb0bb2023-01-11 09:46:29 +080015634
developera3511852023-06-14 14:12:59 +080015635 if (ctrl->wpa)
15636 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015637
developera3511852023-06-14 14:12:59 +080015638 ctrl->wpa = wpa_ctrl_open(ctrl->sockpath);
15639 if (!ctrl->wpa)
15640 goto err;
developer72fb0bb2023-01-11 09:46:29 +080015641
developera3511852023-06-14 14:12:59 +080015642 if (wpa_ctrl_attach(ctrl->wpa) < 0)
15643 goto err_close;
developer72fb0bb2023-01-11 09:46:29 +080015644
developera3511852023-06-14 14:12:59 +080015645 fd = wpa_ctrl_get_fd(ctrl->wpa);
15646 if (fd < 0)
15647 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015648
developera3511852023-06-14 14:12:59 +080015649 if (setsockopt(fd, SOL_SOCKET, SO_RXQ_OVFL, (int[]){1}, sizeof(int)) < 0)
15650 goto err_detach;
developer72fb0bb2023-01-11 09:46:29 +080015651
developera3511852023-06-14 14:12:59 +080015652 ev_io_init(&ctrl->io, ctrl_ev_cb, fd, EV_READ);
15653 ev_io_start(EV_DEFAULT_ &ctrl->io);
developer72fb0bb2023-01-11 09:46:29 +080015654
developera3511852023-06-14 14:12:59 +080015655 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015656
15657err_detach:
developera3511852023-06-14 14:12:59 +080015658 wpa_ctrl_detach(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015659err_close:
developera3511852023-06-14 14:12:59 +080015660 wpa_ctrl_close(ctrl->wpa);
developer72fb0bb2023-01-11 09:46:29 +080015661err:
developera3511852023-06-14 14:12:59 +080015662 ctrl->wpa = NULL;
15663 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015664}
15665
15666static void ctrl_stat_cb(EV_P_ ev_stat *stat, int events)
15667{
developera3511852023-06-14 14:12:59 +080015668 struct ctrl *ctrl = container_of(stat, struct ctrl, stat);
developer72fb0bb2023-01-11 09:46:29 +080015669
developera3511852023-06-14 14:12:59 +080015670 printf("WPA_CTRL: index=%d file state changed\n", ctrl->ssid_index);
15671 ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015672}
15673
15674static void ctrl_retry_cb(EV_P_ ev_timer *timer, int events)
15675{
developera3511852023-06-14 14:12:59 +080015676 struct ctrl *ctrl = container_of(timer, struct ctrl, retry);
developer72fb0bb2023-01-11 09:46:29 +080015677
developera3511852023-06-14 14:12:59 +080015678 printf("WPA_CTRL: index=%d retrying\n", ctrl->ssid_index);
15679 if (ctrl_open(ctrl) == 0) {
15680 printf("WPA_CTRL: retry successful\n");
15681 ev_timer_stop(EV_DEFAULT_ &ctrl->retry);
15682 }
developer72fb0bb2023-01-11 09:46:29 +080015683}
15684
15685int ctrl_enable(struct ctrl *ctrl)
15686{
developera3511852023-06-14 14:12:59 +080015687 if (ctrl->wpa)
15688 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015689
developera3511852023-06-14 14:12:59 +080015690 if (!ctrl->stat.cb) {
15691 ev_stat_init(&ctrl->stat, ctrl_stat_cb, ctrl->sockpath, 0.);
15692 ev_stat_start(EV_DEFAULT_ &ctrl->stat);
15693 }
developer72fb0bb2023-01-11 09:46:29 +080015694
developera3511852023-06-14 14:12:59 +080015695 if (!ctrl->retry.cb) {
15696 ev_timer_init(&ctrl->retry, ctrl_retry_cb, 0., 5.);
15697 }
developer72fb0bb2023-01-11 09:46:29 +080015698
developera3511852023-06-14 14:12:59 +080015699 return ctrl_open(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015700}
15701
15702static void
15703ctrl_msg_cb(char *buf, size_t len)
15704{
developera3511852023-06-14 14:12:59 +080015705 struct ctrl *ctrl = container_of(buf, struct ctrl, reply);
developer72fb0bb2023-01-11 09:46:29 +080015706
developera3511852023-06-14 14:12:59 +080015707 printf("WPA_CTRL: unsolicited message: index=%d len=%zu msg=%s", ctrl->ssid_index, len, buf);
15708 ctrl_process(ctrl);
developer72fb0bb2023-01-11 09:46:29 +080015709}
15710
15711static int ctrl_request(struct ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len)
15712{
developera3511852023-06-14 14:12:59 +080015713 int err;
developer72fb0bb2023-01-11 09:46:29 +080015714
developera3511852023-06-14 14:12:59 +080015715 if (!ctrl->wpa)
15716 return -1;
15717 if (*reply_len < 2)
15718 return -1;
developer72fb0bb2023-01-11 09:46:29 +080015719
developera3511852023-06-14 14:12:59 +080015720 (*reply_len)--;
15721 ctrl->reply_len = sizeof(ctrl->reply);
15722 err = wpa_ctrl_request(ctrl->wpa, cmd, cmd_len, ctrl->reply, &ctrl->reply_len, ctrl_msg_cb);
15723 printf("WPA_CTRL: index=%d cmd='%s' err=%d\n", ctrl->ssid_index, cmd, err);
15724 if (err < 0)
15725 return err;
developer72fb0bb2023-01-11 09:46:29 +080015726
developera3511852023-06-14 14:12:59 +080015727 if (ctrl->reply_len > *reply_len)
15728 ctrl->reply_len = *reply_len;
developer72fb0bb2023-01-11 09:46:29 +080015729
developera3511852023-06-14 14:12:59 +080015730 *reply_len = ctrl->reply_len;
15731 memcpy(reply, ctrl->reply, *reply_len);
15732 reply[*reply_len - 1] = 0;
15733 printf("WPA_CTRL: index=%d reply='%s'\n", ctrl->ssid_index, reply);
15734 return 0;
developer72fb0bb2023-01-11 09:46:29 +080015735}
15736
15737static void ctrl_watchdog_cb(EV_P_ ev_timer *timer, int events)
15738{
developera3511852023-06-14 14:12:59 +080015739 const char *pong = "PONG";
15740 const char *ping = "PING";
15741 char reply[1024];
15742 size_t len = sizeof(reply);
15743 int err;
15744 ULONG s, snum;
15745 INT ret;
15746 BOOL status;
developer72fb0bb2023-01-11 09:46:29 +080015747
developera3511852023-06-14 14:12:59 +080015748 printf("WPA_CTRL: watchdog cb\n");
developer72fb0bb2023-01-11 09:46:29 +080015749
developera3511852023-06-14 14:12:59 +080015750 ret = wifi_getSSIDNumberOfEntries(&snum);
15751 if (ret != RETURN_OK) {
15752 printf("%s: failed to get SSID count", __func__);
15753 return;
15754 }
developer72fb0bb2023-01-11 09:46:29 +080015755
developera3511852023-06-14 14:12:59 +080015756 if (snum > MAX_APS) {
15757 printf("more ssid than supported! %lu\n", snum);
15758 return;
15759 }
developer72fb0bb2023-01-11 09:46:29 +080015760
developera3511852023-06-14 14:12:59 +080015761 for (s = 0; s < snum; s++) {
15762 if (wifi_getApEnable(s, &status) != RETURN_OK) {
15763 printf("%s: failed to get AP Enable for index: %lu\n", __func__, s);
15764 continue;
15765 }
15766 if (status == false) continue;
developer72fb0bb2023-01-11 09:46:29 +080015767
developera3511852023-06-14 14:12:59 +080015768 memset(reply, 0, sizeof(reply));
15769 len = sizeof(reply);
15770 printf("WPA_CTRL: pinging index=%d\n", wpa_ctrl[s].ssid_index);
15771 err = ctrl_request(&wpa_ctrl[s], ping, strlen(ping), reply, &len);
15772 if (err == 0 && len > strlen(pong) && !strncmp(reply, pong, strlen(pong)))
15773 continue;
developer72fb0bb2023-01-11 09:46:29 +080015774
developera3511852023-06-14 14:12:59 +080015775 printf("WPA_CTRL: ping timeout index=%d\n", wpa_ctrl[s].ssid_index);
15776 ctrl_close(&wpa_ctrl[s]);
15777 printf("WPA_CTRL: ev_timer_again %lu\n", s);
15778 ev_timer_again(EV_DEFAULT_ &wpa_ctrl[s].retry);
15779 }
developer72fb0bb2023-01-11 09:46:29 +080015780}
15781
15782static int init_wpa()
15783{
developer9ce44382023-06-28 11:09:37 +080015784 int ret = 0;
developera3511852023-06-14 14:12:59 +080015785 ULONG s, snum;
developer72fb0bb2023-01-11 09:46:29 +080015786
developera3511852023-06-14 14:12:59 +080015787 ret = wifi_getSSIDNumberOfEntries(&snum);
15788 if (ret != RETURN_OK) {
15789 printf("%s: failed to get SSID count", __func__);
15790 return RETURN_ERR;
15791 }
developer72fb0bb2023-01-11 09:46:29 +080015792
developera3511852023-06-14 14:12:59 +080015793 if (snum > MAX_APS) {
15794 printf("more ssid than supported! %lu\n", snum);
15795 return RETURN_ERR;
15796 }
developer72fb0bb2023-01-11 09:46:29 +080015797
developera3511852023-06-14 14:12:59 +080015798 for (s = 0; s < snum; s++) {
15799 memset(&wpa_ctrl[s], 0, sizeof(struct ctrl));
developer32f2a182023-06-27 19:50:41 +080015800 ret = snprintf(wpa_ctrl[s].sockpath, sizeof(wpa_ctrl[s].sockpath), "%s%lu", SOCK_PREFIX, s);
15801 if (os_snprintf_error(sizeof(wpa_ctrl[s].sockpath), ret)) {
15802 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15803 return RETURN_ERR;
15804 }
developera3511852023-06-14 14:12:59 +080015805 wpa_ctrl[s].ssid_index = s;
15806 ctrl_enable(&wpa_ctrl[s]);
15807 }
developer72fb0bb2023-01-11 09:46:29 +080015808
developera3511852023-06-14 14:12:59 +080015809 ev_timer_init(&wpa_ctrl->watchdog, ctrl_watchdog_cb, 0., 30.);
15810 ev_timer_again(EV_DEFAULT_ &wpa_ctrl->watchdog);
developer72fb0bb2023-01-11 09:46:29 +080015811
developera3511852023-06-14 14:12:59 +080015812 initialized = 1;
15813 printf("WPA_CTRL: initialized\n");
developer72fb0bb2023-01-11 09:46:29 +080015814
developera3511852023-06-14 14:12:59 +080015815 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080015816}
15817
15818void wifi_newApAssociatedDevice_callback_register(wifi_newApAssociatedDevice_callback callback_proc)
15819{
developera3511852023-06-14 14:12:59 +080015820 clients_connect_cb = callback_proc;
15821 if (!initialized)
15822 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015823}
15824
15825void wifi_apDisassociatedDevice_callback_register(wifi_apDisassociatedDevice_callback callback_proc)
15826{
developera3511852023-06-14 14:12:59 +080015827 clients_disconnect_cb = callback_proc;
15828 if (!initialized)
15829 init_wpa();
developer72fb0bb2023-01-11 09:46:29 +080015830}
15831
15832INT wifi_setBTMRequest(UINT apIndex, CHAR *peerMac, wifi_BTMRequest_t *request)
15833{
developera3511852023-06-14 14:12:59 +080015834 // TODO Implement me!
15835 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015836}
15837
15838INT wifi_setRMBeaconRequest(UINT apIndex, CHAR *peer, wifi_BeaconRequest_t *in_request, UCHAR *out_DialogToken)
15839{
developera3511852023-06-14 14:12:59 +080015840 // TODO Implement me!
15841 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015842}
15843
15844INT wifi_getRadioChannels(INT radioIndex, wifi_channelMap_t *outputMap, INT outputMapSize)
15845{
developera3511852023-06-14 14:12:59 +080015846 int i;
15847 int phyId = -1;
15848 char cmd[256] = {0};
15849 char channel_numbers_buf[256] = {0};
15850 char dfs_state_buf[256] = {0};
15851 char line[256] = {0};
15852 const char *ptr;
15853 BOOL dfs_enable = false;
developere40952c2023-06-15 18:46:43 +080015854 int res;
developer72fb0bb2023-01-11 09:46:29 +080015855
developera3511852023-06-14 14:12:59 +080015856 memset(outputMap, 0, outputMapSize*sizeof(wifi_channelMap_t)); // all unused entries should be zero
developer72fb0bb2023-01-11 09:46:29 +080015857
developera3511852023-06-14 14:12:59 +080015858 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
15859 phyId = radio_index_to_phy(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080015860
developere40952c2023-06-15 18:46:43 +080015861 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\\|");
15862 if (os_snprintf_error(sizeof(cmd), res)) {
15863 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
15864 return RETURN_ERR;
15865 }
developer72fb0bb2023-01-11 09:46:29 +080015866
developera3511852023-06-14 14:12:59 +080015867 if (_syscmd(cmd, channel_numbers_buf, sizeof(channel_numbers_buf)) == RETURN_ERR) {
15868 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15869 return RETURN_ERR;
15870 }
developer72fb0bb2023-01-11 09:46:29 +080015871
developera3511852023-06-14 14:12:59 +080015872 ptr = channel_numbers_buf;
15873 i = 0;
15874 while ((ptr = get_line_from_str_buf(ptr, line))) {
15875 if (i >= outputMapSize) {
15876 wifi_dbg_printf("%s: DFS map size too small\n", __FUNCTION__);
15877 return RETURN_ERR;
15878 }
developerd14dff12023-06-28 22:47:44 +080015879 if (sscanf(line, "%d", &outputMap[i].ch_number) != 1) {
15880 wifi_debug(DEBUG_ERROR, "sscanf format error.\n");
15881 return RETURN_ERR;
15882 }
developerd1824452023-05-18 12:30:04 +080015883
developera3511852023-06-14 14:12:59 +080015884 memset(cmd, 0, sizeof(cmd));
15885 // Below command should fetch string for DFS state (usable, available or unavailable)
15886 // Example line: "DFS state: usable (for 78930 sec)"
15887 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) {
15888 wifi_dbg_printf("%s: failed to build dfs state command\n", __FUNCTION__);
15889 return RETURN_ERR;
15890 }
developer72fb0bb2023-01-11 09:46:29 +080015891
developera3511852023-06-14 14:12:59 +080015892 memset(dfs_state_buf, 0, sizeof(dfs_state_buf));
15893 if (_syscmd(cmd, dfs_state_buf, sizeof(dfs_state_buf)) == RETURN_ERR) {
15894 wifi_dbg_printf("%s: failed to execute '%s'\n", __FUNCTION__, cmd);
15895 return RETURN_ERR;
15896 }
developer72fb0bb2023-01-11 09:46:29 +080015897
developera3511852023-06-14 14:12:59 +080015898 wifi_dbg_printf("DFS state = '%s'\n", dfs_state_buf);
developer59fda4f2023-05-16 15:47:38 +080015899
developera3511852023-06-14 14:12:59 +080015900 if (!strcmp(dfs_state_buf, "usable")) {
15901 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_FINISHED;
15902 } else if (!strcmp(dfs_state_buf, "available")) {
15903 outputMap[i].ch_state = CHAN_STATE_DFS_CAC_COMPLETED;
15904 } else if (!strcmp(dfs_state_buf, "unavailable")) {
15905 outputMap[i].ch_state = CHAN_STATE_DFS_NOP_START;
15906 } else {
15907 outputMap[i].ch_state = CHAN_STATE_AVAILABLE;
15908 }
15909 i++;
15910 }
developer40ba1762023-05-13 11:03:49 +080015911
developera3511852023-06-14 14:12:59 +080015912 return RETURN_OK;
developerd1824452023-05-18 12:30:04 +080015913
developera3511852023-06-14 14:12:59 +080015914 wifi_dbg_printf("%s: wrong radio index (%d)\n", __FUNCTION__, radioIndex);
15915 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015916}
15917
15918INT wifi_chan_eventRegister(wifi_chan_eventCB_t eventCb)
15919{
developera3511852023-06-14 14:12:59 +080015920 // TODO Implement me!
15921 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015922}
15923
15924INT wifi_getRadioBandUtilization (INT radioIndex, INT *output_percentage)
15925{
developer0155a502023-06-19 20:33:57 +080015926 int ret = -1;
15927 char inf_name[IF_NAME_SIZE] = {0};
15928 int if_idx = 0;
15929 struct unl unl_ins;
15930 struct nl_msg *msg = NULL;
15931 struct nlattr * msg_data = NULL;
15932 struct mtk_nl80211_param param;
15933 struct mtk_nl80211_cb_data cb_data;
15934 wdev_ap_metric ap_metric;
15935
15936 /*init mtk nl80211 vendor cmd*/
15937
15938 if (wifi_GetInterfaceName(radioIndex, inf_name) != RETURN_OK)
15939 return RETURN_ERR;
15940 if_idx = if_nametoindex(inf_name);
15941 if (!if_idx) {
15942 wifi_debug(DEBUG_ERROR,"can't finde ifname(%s) index,ERROR\n", inf_name);
15943 return RETURN_ERR;
15944 }
15945
15946 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_GET_STATISTIC;
15947 param.if_type = NL80211_ATTR_IFINDEX;
15948 param.if_idx = if_idx;
15949
15950 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
15951 if (ret) {
15952 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
15953 return RETURN_ERR;
15954 }
15955
15956 /*add mtk vendor cmd data*/
15957
15958 if (nla_put(msg, MTK_NL80211_VENDOR_ATTR_GET_AP_METRICS, sizeof(wdev_ap_metric), (char *)&ap_metric)) {
15959 wifi_debug(DEBUG_ERROR, "Nla put GET_AP_METRICS attribute error\n");
15960 nlmsg_free(msg);
15961 goto err;
15962 }
15963
15964 /*send mtk nl80211 vendor msg*/
15965 cb_data.out_buf = (char *)output_percentage;
15966 cb_data.out_len = sizeof(wdev_ap_metric);
15967 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, mtk_get_ap_metrics, &cb_data);
15968 if (ret) {
15969 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
15970 goto err;
15971 }
15972
15973 /*deinit mtk nl80211 vendor msg*/
15974 mtk_nl80211_deint(&unl_ins);
15975 wifi_debug(DEBUG_NOTICE, "set cmd success.\n");
15976 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
15977
developera3511852023-06-14 14:12:59 +080015978 return RETURN_OK;
developer0155a502023-06-19 20:33:57 +080015979err:
15980 mtk_nl80211_deint(&unl_ins);
15981 wifi_debug(DEBUG_ERROR, "set cmd fails.\n");
15982 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015983}
15984
developer0155a502023-06-19 20:33:57 +080015985
developer72fb0bb2023-01-11 09:46:29 +080015986INT wifi_getApAssociatedClientDiagnosticResult(INT apIndex, char *mac_addr, wifi_associated_dev3_t *dev_conn)
15987{
developera3511852023-06-14 14:12:59 +080015988 // TODO Implement me!
15989 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015990}
15991
15992INT wifi_switchBand(char *interface_name,INT radioIndex,char *freqBand)
15993{
developera3511852023-06-14 14:12:59 +080015994 // TODO API refrence Implementaion is present on RPI hal
15995 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080015996}
15997
15998INT wifi_getRadioPercentageTransmitPower(INT apIndex, ULONG *txpwr_pcntg)
15999{
developera3511852023-06-14 14:12:59 +080016000 ULONG pwr_percentage = 0;
developer72fb0bb2023-01-11 09:46:29 +080016001
developera3511852023-06-14 14:12:59 +080016002 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16003 if(txpwr_pcntg == NULL)
developerdaf24792023-06-06 11:40:04 +080016004 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016005
developera1255e42023-05-13 17:45:02 +080016006 wifi_getRadioTransmitPower(apIndex, &pwr_percentage);
16007 *txpwr_pcntg = pwr_percentage;
developera3511852023-06-14 14:12:59 +080016008 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16009 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016010}
16011
16012INT wifi_setZeroDFSState(UINT radioIndex, BOOL enable, BOOL precac)
16013{
developera3511852023-06-14 14:12:59 +080016014 // TODO precac feature.
16015 struct params params[2] = {0};
16016 char config_file[128] = {0};
16017 BOOL dfs_enable = false;
16018 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016019 int res;
developer72fb0bb2023-01-11 09:46:29 +080016020
developera3511852023-06-14 14:12:59 +080016021 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16022 band = wifi_index_to_band(radioIndex);
16023 wifi_getRadioDfsEnable(radioIndex, &dfs_enable);
developer72fb0bb2023-01-11 09:46:29 +080016024
developera3511852023-06-14 14:12:59 +080016025 if (dfs_enable == false) {
16026 WIFI_ENTRY_EXIT_DEBUG("Please enable DFS firstly!: %s\n", __func__);
16027 return RETURN_ERR;
16028 }
16029 params[0].name = "DfsZeroWaitDefault";
16030 params[0].value = enable?"1":"0";
16031 params[1].name = "DfsDedicatedZeroWait";
16032 params[1].value = enable?"1":"0";
developere40952c2023-06-15 18:46:43 +080016033 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16034 if (os_snprintf_error(sizeof(config_file), res)) {
16035 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16036 return RETURN_ERR;
16037 }
developera3511852023-06-14 14:12:59 +080016038 wifi_datfileWrite(config_file, params, 2);
16039 wifi_reloadAp(radioIndex);
16040 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016041
developera3511852023-06-14 14:12:59 +080016042 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16043 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016044}
16045
16046INT wifi_getZeroDFSState(UINT radioIndex, BOOL *enable, BOOL *precac)
16047{
developera3511852023-06-14 14:12:59 +080016048 char config_file[128] = {0};
16049 char buf1[32] = {0};
16050 char buf2[32] = {0};
16051 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080016052 int res;
developer72fb0bb2023-01-11 09:46:29 +080016053
developera3511852023-06-14 14:12:59 +080016054 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16055 if (NULL == enable || NULL == precac)
16056 return RETURN_ERR;
16057 band = wifi_index_to_band(radioIndex);
developere40952c2023-06-15 18:46:43 +080016058 res = snprintf(config_file, sizeof(config_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16059 if (os_snprintf_error(sizeof(config_file), res)) {
16060 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16061 return RETURN_ERR;
16062 }
developera3511852023-06-14 14:12:59 +080016063 wifi_datfileRead(config_file, "DfsZeroWaitDefault", buf1, sizeof(buf1));
16064 wifi_datfileRead(config_file, "DfsDedicatedZeroWait", buf2, sizeof(buf2));
16065 if ((strncmp(buf1, "1", 1) == 0) && (strncmp(buf2, "1", 1) == 0))
16066 *enable = true;
16067 else
16068 *enable = false;
developer72fb0bb2023-01-11 09:46:29 +080016069
developera3511852023-06-14 14:12:59 +080016070 /* TODO precac feature */
developer72fb0bb2023-01-11 09:46:29 +080016071
developera3511852023-06-14 14:12:59 +080016072 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16073 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016074}
16075
16076INT wifi_isZeroDFSSupported(UINT radioIndex, BOOL *supported)
16077{
developera3511852023-06-14 14:12:59 +080016078 *supported = TRUE;
16079 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016080}
16081
16082INT wifi_setDownlinkMuType(INT radio_index, wifi_dl_mu_type_t mu_type)
16083{
developer863a4a62023-06-06 16:55:59 +080016084 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016085 wifi_band band = band_invalid;
16086 char ofdmabuf[32] = {'\0'};
16087 char mimobuf[32] = {'\0'};
16088 char new_ofdmabuf[32] = {'\0'};
16089 char new_mimobuf[32] = {'\0'};
16090 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016091 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16092 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16093 UCHAR bss_cnt = 0;
16094 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016095 int res;
developer72fb0bb2023-01-11 09:46:29 +080016096
developera3511852023-06-14 14:12:59 +080016097 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developera1255e42023-05-13 17:45:02 +080016098 if ((mu_type < WIFI_DL_MU_TYPE_NONE)
16099 || (mu_type > WIFI_DL_MU_TYPE_OFDMA_MIMO)) {
16100 printf("%s:mu_type input Error", __func__);
16101 return RETURN_ERR;
16102 }
developera3511852023-06-14 14:12:59 +080016103 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016104 if (band == band_invalid) {
16105 printf("%s:Band Error\n", __func__);
16106 return RETURN_ERR;
16107 }
developere40952c2023-06-15 18:46:43 +080016108 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16109 if (os_snprintf_error(sizeof(dat_file), res)) {
16110 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16111 return RETURN_ERR;
16112 }
16113
developera1255e42023-05-13 17:45:02 +080016114 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016115 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16116 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016117 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16118 get_bssnum_byindex(radio_index, &bss_cnt);
16119 val_cnt = 2*bss_cnt - 1;
16120 WIFI_ENTRY_EXIT_DEBUG("bss number: %d\n", bss_cnt);
16121 if ((val_cnt >= sizeof(new_ofdmabuf))
16122 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016123 printf("%s:bss cnt Error", __func__);
developera1255e42023-05-13 17:45:02 +080016124 return RETURN_ERR;
16125 }
16126 /*translate set value*/
16127 if (mu_type == WIFI_DL_MU_TYPE_NONE) {
16128 strncpy(new_ofdmabuf, str_zero, val_cnt);
16129 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016130 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA) {
developera1255e42023-05-13 17:45:02 +080016131 strncpy(new_ofdmabuf, str_one, val_cnt);
16132 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016133 } else if (mu_type == WIFI_DL_MU_TYPE_MIMO) {
developera1255e42023-05-13 17:45:02 +080016134 strncpy(new_ofdmabuf, str_zero, val_cnt);
16135 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016136 } else if (mu_type == WIFI_DL_MU_TYPE_OFDMA_MIMO) {
developera1255e42023-05-13 17:45:02 +080016137 strncpy(new_ofdmabuf, str_one, val_cnt);
16138 strncpy(new_mimobuf, str_one, val_cnt);
developera3511852023-06-14 14:12:59 +080016139 }
developera1255e42023-05-13 17:45:02 +080016140 WIFI_ENTRY_EXIT_DEBUG("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16141 /*same value, not operation*/
16142 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16143 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16144 printf("%s:Reduntant value\n", __func__);
16145 return RETURN_OK;
16146 }
16147 /*modify dat file to new file*/
16148 params[0].name="MuOfdmaDlEnable";
16149 params[0].value=new_ofdmabuf;
16150 params[1].name="MuMimoDlEnable";
16151 params[1].value=new_mimobuf;
16152 wifi_datfileWrite(dat_file, params, 2);
16153 /*hostapd control restarp ap to take effect on these new value*/
16154 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016155 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16156 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016157}
16158
16159INT wifi_getDownlinkMuType(INT radio_index, wifi_dl_mu_type_t *mu_type)
16160{
developer5a333cf2023-06-06 18:18:50 +080016161 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016162 wifi_band band = band_invalid;
16163 char ofdmabuf[32] = {'\0'};
16164 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016165 char *token = NULL;
developerc14d83a2023-06-29 20:09:42 +080016166 long int ofdma = 0;
16167 long int mimo = 0;
developere40952c2023-06-15 18:46:43 +080016168 int res;
developer72fb0bb2023-01-11 09:46:29 +080016169
developera3511852023-06-14 14:12:59 +080016170 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016171
developera3511852023-06-14 14:12:59 +080016172 if (mu_type == NULL)
16173 return RETURN_ERR;
16174 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016175 if (band == band_invalid) {
16176 printf("%s:Band Error\n", __func__);
16177 return RETURN_ERR;
16178 }
developere40952c2023-06-15 18:46:43 +080016179 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16180 if (os_snprintf_error(sizeof(dat_file), res)) {
16181 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16182 return RETURN_ERR;
16183 }
developera1255e42023-05-13 17:45:02 +080016184 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016185 wifi_datfileRead(dat_file, "MuOfdmaDlEnable", ofdmabuf, sizeof(ofdmabuf));
16186 wifi_datfileRead(dat_file, "MuMimoDlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016187
developera1255e42023-05-13 17:45:02 +080016188 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016189 if (token == NULL) {
16190 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16191 return RETURN_ERR;
16192 }
developerc14d83a2023-06-29 20:09:42 +080016193 if (hal_strtol(token, 10, &ofdma) < 0) {
16194 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developerd14dff12023-06-28 22:47:44 +080016195 }
developerc14d83a2023-06-29 20:09:42 +080016196
developera1255e42023-05-13 17:45:02 +080016197 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016198 if (token == NULL) {
16199 wifi_debug(DEBUG_ERROR, "Unexpected strtok fail\n");
16200 return RETURN_ERR;
16201 }
developerc14d83a2023-06-29 20:09:42 +080016202
16203 if (hal_strtol(token, 10, &mimo) < 0) {
16204 wifi_debug(DEBUG_ERROR, "strtol fail\n");
developer37646972023-06-29 10:58:43 +080016205 }
developerc14d83a2023-06-29 20:09:42 +080016206
developera1255e42023-05-13 17:45:02 +080016207 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d,mimo=%d\n", __func__, ofdma, mimo);
16208 if ((ofdma == 1) && (mimo == 1))
16209 *mu_type = WIFI_DL_MU_TYPE_OFDMA_MIMO;
16210 else if ((ofdma == 0) && (mimo == 1))
16211 *mu_type = WIFI_DL_MU_TYPE_MIMO;
16212 else if ((ofdma == 1) && (mimo == 0))
16213 *mu_type = WIFI_DL_MU_TYPE_OFDMA;
16214 else
16215 *mu_type = WIFI_DL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016216 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16217 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016218}
16219
16220INT wifi_setUplinkMuType(INT radio_index, wifi_ul_mu_type_t mu_type)
16221{
developera3511852023-06-14 14:12:59 +080016222 // hemu onoff=<val> (bitmap- UL MU-MIMO(bit3), DL MU-MIMO(bit2), UL OFDMA(bit1), DL OFDMA(bit0))
developer863a4a62023-06-06 16:55:59 +080016223 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016224 wifi_band band = band_invalid;
16225 char ofdmabuf[32] = {'\0'};
16226 char mimobuf[32] = {'\0'};
16227 char new_ofdmabuf[32] = {'\0'};
16228 char new_mimobuf[32] = {'\0'};
16229 struct params params[2];
developera1255e42023-05-13 17:45:02 +080016230 char *str_zero = "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0";/*default 15bss per band.*/
16231 char *str_one = "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1";
16232 UCHAR bss_cnt = 0;
16233 UCHAR val_cnt = 0;
developere40952c2023-06-15 18:46:43 +080016234 int res;
developer72fb0bb2023-01-11 09:46:29 +080016235
developera3511852023-06-14 14:12:59 +080016236 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16237 band = wifi_index_to_band(radio_index);
developera1255e42023-05-13 17:45:02 +080016238 if (band == band_invalid) {
16239 printf("%s:Band Error\n", __func__);
16240 return RETURN_ERR;
16241 }
16242 if ((mu_type < WIFI_UL_MU_TYPE_NONE)
16243 || (mu_type > WIFI_UL_MU_TYPE_OFDMA)) {
16244 printf("%s:mu_type input Error\n", __func__);
16245 return RETURN_ERR;
16246 }
developere40952c2023-06-15 18:46:43 +080016247 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16248 if (os_snprintf_error(sizeof(dat_file), res)) {
16249 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16250 return RETURN_ERR;
16251 }
developera1255e42023-05-13 17:45:02 +080016252 /*get current value in dat file*/
developera3511852023-06-14 14:12:59 +080016253 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16254 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developera1255e42023-05-13 17:45:02 +080016255 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma-%s, mimo-%s\n", __func__, ofdmabuf, mimobuf);
16256 get_bssnum_byindex(radio_index, &bss_cnt);
16257 val_cnt = 2*bss_cnt - 1;
16258 printf("bssNumber:%d,ValCnt:%d\n", bss_cnt, val_cnt);
16259 if ((val_cnt >= sizeof(new_ofdmabuf))
16260 || (val_cnt >= sizeof(new_mimobuf))) {
developer49c83812023-06-06 14:23:53 +080016261 printf("%s:bss cnt Error\n", __func__);
developera1255e42023-05-13 17:45:02 +080016262 return RETURN_ERR;
16263 }
16264 /*translate set value*/
16265 if (mu_type == WIFI_UL_MU_TYPE_NONE) {
16266 strncpy(new_ofdmabuf, str_zero, val_cnt);
16267 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016268 }
developera1255e42023-05-13 17:45:02 +080016269 if (mu_type == WIFI_UL_MU_TYPE_OFDMA) {
16270 strncpy(new_ofdmabuf, str_one, val_cnt);
16271 strncpy(new_mimobuf, str_zero, val_cnt);
developera3511852023-06-14 14:12:59 +080016272 }
developera1255e42023-05-13 17:45:02 +080016273 printf("%s:new_ofdmabuf-%s, new_mimobuf-%s\n", __func__, new_ofdmabuf, new_mimobuf);
16274 /*same value, not operation*/
16275 if ((strncmp(new_mimobuf, mimobuf, 1) ==0)
16276 && (strncmp(new_ofdmabuf, ofdmabuf, 1) ==0)) {
16277 printf("%s:Reduntant value\n", __func__);
16278 return RETURN_OK;
16279 }
16280 /*modify dat file to new file*/
16281 params[0].name="MuOfdmaUlEnable";
16282 params[0].value=new_ofdmabuf;
16283 params[1].name="MuMimoUlEnable";
16284 params[1].value=new_mimobuf;
16285 wifi_datfileWrite(dat_file, params, 2);
16286 wifi_reloadAp(radio_index);
developera3511852023-06-14 14:12:59 +080016287 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16288 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016289}
16290
16291INT wifi_getUplinkMuType(INT radio_index, wifi_ul_mu_type_t *mu_type)
16292{
developer863a4a62023-06-06 16:55:59 +080016293 CHAR dat_file[64] = {0};
developera3511852023-06-14 14:12:59 +080016294 wifi_band band = band_invalid;
16295 char ofdmabuf[32] = {'\0'};
16296 char mimobuf[32] = {'\0'};
developera1255e42023-05-13 17:45:02 +080016297 char *token = NULL;
16298 UCHAR ofdma = 0;
16299 UCHAR mimo = 0;
developere40952c2023-06-15 18:46:43 +080016300 int res;
developerc14d83a2023-06-29 20:09:42 +080016301 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016302
developera3511852023-06-14 14:12:59 +080016303 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016304
developera3511852023-06-14 14:12:59 +080016305 if (mu_type == NULL)
16306 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016307 band = wifi_index_to_band(radio_index);
16308 if (band == band_invalid) {
16309 printf("%s:Band Error", __func__);
16310 return RETURN_ERR;
16311 }
developere40952c2023-06-15 18:46:43 +080016312 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16313 if (os_snprintf_error(sizeof(dat_file), res)) {
16314 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16315 return RETURN_ERR;
16316 }
developera1255e42023-05-13 17:45:02 +080016317 /*get current value in dat file*/
16318 wifi_datfileRead(dat_file, "MuOfdmaUlEnable", ofdmabuf, sizeof(ofdmabuf));
16319 wifi_datfileRead(dat_file, "MuMimoUlEnable", mimobuf, sizeof(mimobuf));
developer72fb0bb2023-01-11 09:46:29 +080016320
developera1255e42023-05-13 17:45:02 +080016321 token = strtok(ofdmabuf, ";");
developerd14dff12023-06-28 22:47:44 +080016322 if (token == NULL) {
16323 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16324 return RETURN_ERR;
16325 }
developerc14d83a2023-06-29 20:09:42 +080016326
16327 if (hal_strtoul(token, 10, &tmp) < 0) {
16328 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016329 }
developerc14d83a2023-06-29 20:09:42 +080016330 ofdma = tmp;
developera1255e42023-05-13 17:45:02 +080016331 token = strtok(mimobuf, ";");
developer37646972023-06-29 10:58:43 +080016332 if (token == NULL) {
16333 wifi_debug(DEBUG_ERROR, "strtok fail\n");
16334 return RETURN_ERR;
16335 }
developerc14d83a2023-06-29 20:09:42 +080016336
16337 if (hal_strtoul(token, 10, &tmp) < 0) {
16338 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developer37646972023-06-29 10:58:43 +080016339 }
developerc14d83a2023-06-29 20:09:42 +080016340 mimo = tmp;
16341
developera1255e42023-05-13 17:45:02 +080016342 WIFI_ENTRY_EXIT_DEBUG("%s:ofdma=%d, mimo=%d\n", __func__, ofdma, mimo);
16343 if ((ofdma == 1) && (mimo == 0))
16344 *mu_type = WIFI_UL_MU_TYPE_OFDMA;
16345 else
16346 *mu_type = WIFI_UL_MU_TYPE_NONE;
developera3511852023-06-14 14:12:59 +080016347 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16348 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016349}
16350
16351
16352INT wifi_setGuardInterval(INT radio_index, wifi_guard_interval_t guard_interval)
16353{
developera3511852023-06-14 14:12:59 +080016354 char cmd[128] = {0};
16355 char buf[256] = {0};
16356 char config_file[64] = {0};
16357 char GI[8] = {0};
16358 UINT mode_map = 0;
16359 FILE *f = NULL;
16360 wifi_band band = band_invalid;
16361 char dat_file[64] = {'\0'};
16362 struct params params[3];
developere40952c2023-06-15 18:46:43 +080016363 int res;
developer72fb0bb2023-01-11 09:46:29 +080016364
developera3511852023-06-14 14:12:59 +080016365 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016366
developera3511852023-06-14 14:12:59 +080016367 if (wifi_getRadioMode(radio_index, buf, &mode_map) == RETURN_ERR) {
16368 wifi_dbg_printf("%s: wifi_getRadioMode return error\n", __func__);
16369 return RETURN_ERR;
16370 }
developera1255e42023-05-13 17:45:02 +080016371 /*sanity check*/
16372 if (((guard_interval == wifi_guard_interval_1600)
16373 || (guard_interval == wifi_guard_interval_3200))
developerdaf24792023-06-06 11:40:04 +080016374 && ((mode_map & (WIFI_MODE_BE | WIFI_MODE_AX)) == 0)) {
developera3511852023-06-14 14:12:59 +080016375 wifi_dbg_printf("%s: N/AC Mode not support 1600/3200ns GI\n", __func__);
16376 return RETURN_ERR;
developera1255e42023-05-13 17:45:02 +080016377 }
developere40952c2023-06-15 18:46:43 +080016378 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16379 if (os_snprintf_error(sizeof(config_file), res)) {
16380 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16381 return RETURN_ERR;
16382 }
developera3511852023-06-14 14:12:59 +080016383 band = wifi_index_to_band(radio_index);
developer72fb0bb2023-01-11 09:46:29 +080016384
developera3511852023-06-14 14:12:59 +080016385 // Hostapd are not supported HE mode GI 1600, 3200 ns.
16386 if (guard_interval == wifi_guard_interval_800) { // remove all capab about short GI
developere40952c2023-06-15 18:46:43 +080016387 res = snprintf(cmd, sizeof(cmd), "sed -r -i 's/\\[SHORT-GI-(.){1,2}0\\]//g' %s", config_file);
16388 if (os_snprintf_error(sizeof(cmd), res)) {
16389 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16390 return RETURN_ERR;
16391 }
developera3511852023-06-14 14:12:59 +080016392 _syscmd(cmd, buf, sizeof(buf));
16393 } else if (guard_interval == wifi_guard_interval_400 || guard_interval == wifi_guard_interval_auto){
16394 wifi_hostapdRead(config_file, "ht_capab", buf, sizeof(buf));
16395 if (strstr(buf, "[SHORT-GI-") == NULL) {
developere40952c2023-06-15 18:46:43 +080016396 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^ht_capab=.*/s/$/[SHORT-GI-20][SHORT-GI-40]/' %s", config_file);
16397 if (os_snprintf_error(sizeof(cmd), res)) {
16398 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16399 return RETURN_ERR;
16400 }
developera3511852023-06-14 14:12:59 +080016401 _syscmd(cmd, buf, sizeof(buf));
16402 }
16403 if (band == band_5) {
16404 wifi_hostapdRead(config_file, "vht_capab", buf, sizeof(buf));
16405 if (strstr(buf, "[SHORT-GI-") == NULL) {
developere40952c2023-06-15 18:46:43 +080016406 res = snprintf(cmd, sizeof(cmd), "sed -r -i '/^vht_capab=.*/s/$/[SHORT-GI-80][SHORT-GI-160]/' %s", config_file);
16407 if (os_snprintf_error(sizeof(cmd), res)) {
16408 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16409 return RETURN_ERR;
16410 }
developera3511852023-06-14 14:12:59 +080016411 _syscmd(cmd, buf, sizeof(buf));
16412 }
16413 }
16414 }
16415 /*wifi_reloadAp(radio_index);
developera1255e42023-05-13 17:45:02 +080016416 caller "wifi_setRadioOperatingParameters" have done this step.
16417 */
developere40952c2023-06-15 18:46:43 +080016418 res = snprintf(dat_file, sizeof(dat_file), "%s%d.dat", LOGAN_DAT_FILE, band);
16419 if (os_snprintf_error(sizeof(dat_file), res)) {
16420 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16421 return RETURN_ERR;
16422 }
developera3511852023-06-14 14:12:59 +080016423 if (guard_interval == wifi_guard_interval_400) {
developera1255e42023-05-13 17:45:02 +080016424 params[0].name = "HT_GI";
16425 params[0].value = "1";
16426 params[1].name = "VHT_SGI";
16427 params[1].value = "1";
16428 wifi_datfileWrite(dat_file, params, 2);
developer32f2a182023-06-27 19:50:41 +080016429 memcpy(GI, "0.4", 3);
developera1255e42023-05-13 17:45:02 +080016430 } else {
16431 params[0].name = "HT_GI";
16432 params[0].value = "0";
16433 params[1].name = "VHT_SGI";
16434 params[1].value = "0";
16435 /*should enable FIXED_HE_GI_SUPPORT in driver*/
16436 params[2].name = "FgiFltf";
16437 if (guard_interval == wifi_guard_interval_800) {
16438 params[2].value = "800";
developer32f2a182023-06-27 19:50:41 +080016439 memcpy(GI, "0.8", 3);
developera1255e42023-05-13 17:45:02 +080016440 } else if (guard_interval == wifi_guard_interval_1600) {
16441 params[2].value = "1600";
developer32f2a182023-06-27 19:50:41 +080016442 memcpy(GI, "1.6", 3);
developera1255e42023-05-13 17:45:02 +080016443 } else if (guard_interval == wifi_guard_interval_3200) {
16444 params[2].value = "3200";
developer32f2a182023-06-27 19:50:41 +080016445 memcpy(GI, "3.2", 3);
developera1255e42023-05-13 17:45:02 +080016446 } else if (guard_interval == wifi_guard_interval_auto) {
16447 params[2].value = "0";
developer32f2a182023-06-27 19:50:41 +080016448 memcpy(GI, "auto", 4);
developera1255e42023-05-13 17:45:02 +080016449 }
16450 wifi_datfileWrite(dat_file, params, 3);
16451 }
developera3511852023-06-14 14:12:59 +080016452 // Record GI for get GI function
developere40952c2023-06-15 18:46:43 +080016453 res = snprintf(buf, sizeof(buf), "%s%d.txt", GUARD_INTERVAL_FILE, radio_index);
16454 if (os_snprintf_error(sizeof(buf), res)) {
16455 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16456 return RETURN_ERR;
16457 }
developera3511852023-06-14 14:12:59 +080016458 f = fopen(buf, "w");
16459 if (f == NULL)
16460 return RETURN_ERR;
16461 fprintf(f, "%s", GI);
developerc14d83a2023-06-29 20:09:42 +080016462 if (fclose(f) == EOF)
16463 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016464 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16465 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016466}
16467
16468INT wifi_getGuardInterval(INT radio_index, wifi_guard_interval_t *guard_interval)
16469{
developera3511852023-06-14 14:12:59 +080016470 char buf[32] = {0};
16471 char cmd[64] = {0};
developere40952c2023-06-15 18:46:43 +080016472 int res;
developer72fb0bb2023-01-11 09:46:29 +080016473
developera3511852023-06-14 14:12:59 +080016474 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080016475
developera3511852023-06-14 14:12:59 +080016476 if (guard_interval == NULL)
16477 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016478
developere40952c2023-06-15 18:46:43 +080016479 res = snprintf(cmd, sizeof(cmd), "cat %s%d.txt 2> /dev/null", GUARD_INTERVAL_FILE, radio_index);
16480 if (os_snprintf_error(sizeof(cmd), res)) {
16481 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16482 return RETURN_ERR;
16483 }
developera3511852023-06-14 14:12:59 +080016484 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016485
developera3511852023-06-14 14:12:59 +080016486 if (strncmp(buf, "0.4", 3) == 0)
16487 *guard_interval = wifi_guard_interval_400;
16488 else if (strncmp(buf, "0.8", 3) == 0)
16489 *guard_interval = wifi_guard_interval_800;
16490 else if (strncmp(buf, "1.6", 3) == 0)
16491 *guard_interval = wifi_guard_interval_1600;
16492 else if (strncmp(buf, "3.2", 3) == 0)
16493 *guard_interval = wifi_guard_interval_3200;
16494 else
16495 *guard_interval = wifi_guard_interval_auto;
developer72fb0bb2023-01-11 09:46:29 +080016496
developera3511852023-06-14 14:12:59 +080016497 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16498 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016499}
16500
16501INT wifi_setBSSColor(INT radio_index, UCHAR color)
16502{
developera3511852023-06-14 14:12:59 +080016503 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16504 struct params params = {0};
16505 char config_file[128] = {0};
16506 char bss_color[4] ={0};
developere40952c2023-06-15 18:46:43 +080016507 int res;
developer72fb0bb2023-01-11 09:46:29 +080016508
developera1255e42023-05-13 17:45:02 +080016509 if (color < 1 || color > 63) {
16510 wifi_dbg_printf("color value is err:%d.\n", color);
16511 return RETURN_ERR;
16512 }
developera3511852023-06-14 14:12:59 +080016513 params.name = "he_bss_color";
developere40952c2023-06-15 18:46:43 +080016514 res = snprintf(bss_color, sizeof(bss_color), "%hhu", color);
16515 if (os_snprintf_error(sizeof(bss_color), res)) {
16516 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16517 return RETURN_ERR;
16518 }
developera3511852023-06-14 14:12:59 +080016519 params.value = bss_color;
developer75bd10c2023-06-27 11:34:08 +080016520
16521 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16522 if (os_snprintf_error(sizeof(config_file), res)) {
16523 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16524 return RETURN_ERR;
16525 }
developera3511852023-06-14 14:12:59 +080016526 wifi_hostapdWrite(config_file, &params, 1);
16527 //wifi_hostapdProcessUpdate(radio_index, &params, 1);
developera1255e42023-05-13 17:45:02 +080016528 wifi_reloadAp(radio_index);
developer69b61b02023-03-07 17:17:44 +080016529
developera3511852023-06-14 14:12:59 +080016530 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
16531 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016532}
16533
16534INT wifi_getBSSColor(INT radio_index, UCHAR *color)
16535{
developera3511852023-06-14 14:12:59 +080016536 char config_file[128] = {0};
16537 char buf[64] = {0};
16538 char temp_output[128] = {'\0'};
developere40952c2023-06-15 18:46:43 +080016539 int res;
developerc14d83a2023-06-29 20:09:42 +080016540 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080016541
developera3511852023-06-14 14:12:59 +080016542 wifi_dbg_printf("\nFunc=%s\n", __func__);
16543 if (NULL == color)
16544 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016545
developer75bd10c2023-06-27 11:34:08 +080016546 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, radio_index);
16547 if (os_snprintf_error(sizeof(config_file), res)) {
16548 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16549 return RETURN_ERR;
16550 }
developera3511852023-06-14 14:12:59 +080016551 wifi_hostapdRead(config_file, "he_bss_color", buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080016552
developera3511852023-06-14 14:12:59 +080016553 if(strlen(buf) > 0) {
developere40952c2023-06-15 18:46:43 +080016554 res = snprintf(temp_output, sizeof(temp_output), "%s", buf);
developera3511852023-06-14 14:12:59 +080016555 } else {
developere40952c2023-06-15 18:46:43 +080016556 res = snprintf(temp_output, sizeof(temp_output), "1"); // default value
16557 }
16558 if (os_snprintf_error(sizeof(temp_output), res)) {
16559 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16560 return RETURN_ERR;
developera3511852023-06-14 14:12:59 +080016561 }
developer72fb0bb2023-01-11 09:46:29 +080016562
developerc14d83a2023-06-29 20:09:42 +080016563 if (hal_strtoul(temp_output, 10, &tmp) < 0) {
16564 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerd14dff12023-06-28 22:47:44 +080016565 }
developerc14d83a2023-06-29 20:09:42 +080016566 *color = tmp;
developera3511852023-06-14 14:12:59 +080016567 wifi_dbg_printf("\noutput_string=%s\n", color);
developer72fb0bb2023-01-11 09:46:29 +080016568
developera3511852023-06-14 14:12:59 +080016569 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016570}
16571
16572/* multi-psk support */
16573INT wifi_getMultiPskClientKey(INT apIndex, mac_address_t mac, wifi_key_multi_psk_t *key)
16574{
developera3511852023-06-14 14:12:59 +080016575 char cmd[256];
16576 char interface_name[16] = {0};
developer75bd10c2023-06-27 11:34:08 +080016577 int res;
developer72fb0bb2023-01-11 09:46:29 +080016578
developera3511852023-06-14 14:12:59 +080016579 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16580 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016581
developer75bd10c2023-06-27 11:34:08 +080016582 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 +080016583 interface_name,
16584 mac[0],
16585 mac[1],
16586 mac[2],
16587 mac[3],
16588 mac[4],
16589 mac[5]
16590 );
developer75bd10c2023-06-27 11:34:08 +080016591 if (os_snprintf_error(sizeof(cmd), res)) {
16592 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16593 return RETURN_ERR;
16594 }
developera3511852023-06-14 14:12:59 +080016595 printf("DEBUG LOG wifi_getMultiPskClientKey(%s)\n",cmd);
16596 _syscmd(cmd, key->wifi_keyId, 64);
developer72fb0bb2023-01-11 09:46:29 +080016597
16598
developera3511852023-06-14 14:12:59 +080016599 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016600}
16601
16602INT wifi_pushMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16603{
developera3511852023-06-14 14:12:59 +080016604 char interface_name[16] = {0};
16605 FILE *fd = NULL;
16606 char fname[100];
16607 char cmd[128] = {0};
16608 char out[64] = {0};
16609 wifi_key_multi_psk_t * key = NULL;
developer75bd10c2023-06-27 11:34:08 +080016610 int res, ret;
developere40952c2023-06-15 18:46:43 +080016611
developera3511852023-06-14 14:12:59 +080016612 if(keysNumber < 0)
16613 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016614
developere40952c2023-06-15 18:46:43 +080016615 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16616 if (os_snprintf_error(sizeof(fname), res)) {
16617 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16618 return RETURN_ERR;
16619 }
developera3511852023-06-14 14:12:59 +080016620 fd = fopen(fname, "w");
16621 if (!fd) {
16622 return RETURN_ERR;
16623 }
16624 key= (wifi_key_multi_psk_t *) keys;
16625 for(int i=0; i<keysNumber; ++i, key++) {
developer75bd10c2023-06-27 11:34:08 +080016626 ret = fprintf(fd, "keyid=%s 00:00:00:00:00:00 %s\n", key->wifi_keyId, key->wifi_psk);
16627 if (ret < 0)
16628 wifi_debug(DEBUG_ERROR, "fprintf fail\n");
developera3511852023-06-14 14:12:59 +080016629 }
developerd14dff12023-06-28 22:47:44 +080016630 if (fclose(fd) != 0) {
16631 wifi_debug(DEBUG_ERROR, "fclose fail\n");
16632 return RETURN_ERR;
16633 }
developer72fb0bb2023-01-11 09:46:29 +080016634
developera3511852023-06-14 14:12:59 +080016635 //reload file
16636 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16637 return RETURN_ERR;
developer32f2a182023-06-27 19:50:41 +080016638 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i%s raw RELOAD_WPA_PSK", interface_name);
16639
16640 if (os_snprintf_error(sizeof(cmd), res)) {
16641 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16642 return RETURN_ERR;
16643 }
developera3511852023-06-14 14:12:59 +080016644 _syscmd(cmd, out, 64);
16645 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016646}
16647
16648INT wifi_getMultiPskKeys(INT apIndex, wifi_key_multi_psk_t *keys, INT keysNumber)
16649{
developera3511852023-06-14 14:12:59 +080016650 FILE *fd = NULL;
16651 char fname[100];
16652 char * line = NULL;
16653 char * pos = NULL;
16654 size_t len = 0;
16655 ssize_t read = 0;
16656 INT ret = RETURN_OK;
16657 wifi_key_multi_psk_t *keys_it = NULL;
developere40952c2023-06-15 18:46:43 +080016658 int res;
developer72fb0bb2023-01-11 09:46:29 +080016659
developera3511852023-06-14 14:12:59 +080016660 if (keysNumber < 1) {
16661 return RETURN_ERR;
16662 }
developer72fb0bb2023-01-11 09:46:29 +080016663
developere40952c2023-06-15 18:46:43 +080016664 res = snprintf(fname, sizeof(fname), "%s%d.psk", PSK_FILE, apIndex);
16665 if (os_snprintf_error(sizeof(fname), res)) {
16666 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16667 return RETURN_ERR;
16668 }
developera3511852023-06-14 14:12:59 +080016669 fd = fopen(fname, "r");
16670 if (!fd) {
16671 return RETURN_ERR;
16672 }
developer72fb0bb2023-01-11 09:46:29 +080016673
developera3511852023-06-14 14:12:59 +080016674 if (keys == NULL) {
16675 ret = RETURN_ERR;
16676 goto close;
16677 }
developer72fb0bb2023-01-11 09:46:29 +080016678
developera3511852023-06-14 14:12:59 +080016679 keys_it = keys;
16680 while ((read = getline(&line, &len, fd)) != -1) {
16681 //Strip trailing new line if present
16682 if (read > 0 && line[read-1] == '\n') {
16683 line[read-1] = '\0';
16684 }
developer72fb0bb2023-01-11 09:46:29 +080016685
developera3511852023-06-14 14:12:59 +080016686 if(strcmp(line,"keyid=")) {
developer37646972023-06-29 10:58:43 +080016687 if (sscanf(line, "keyid=%63s", keys_it->wifi_keyId) == EOF)
16688 continue;
developera3511852023-06-14 14:12:59 +080016689 if (!(pos = index(line, ' '))) {
16690 ret = RETURN_ERR;
16691 goto close;
16692 }
16693 pos++;
16694 //Here should be 00:00:00:00:00:00
16695 if (!(strcmp(pos,"00:00:00:00:00:00"))) {
16696 printf("Not supported MAC: %s\n", pos);
16697 }
16698 if (!(pos = index(pos, ' '))) {
16699 ret = RETURN_ERR;
16700 goto close;
16701 }
16702 pos++;
developer72fb0bb2023-01-11 09:46:29 +080016703
developera3511852023-06-14 14:12:59 +080016704 //The rest is PSK
developere40952c2023-06-15 18:46:43 +080016705 res = snprintf(&keys_it->wifi_psk[0], sizeof(keys_it->wifi_psk), "%s", pos);
16706 if (os_snprintf_error(sizeof(keys_it->wifi_psk), res)) {
16707 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
developerc14d83a2023-06-29 20:09:42 +080016708 if (fclose(fd) == EOF)
16709 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
16710
developere40952c2023-06-15 18:46:43 +080016711 return RETURN_ERR;
16712 }
16713
developera3511852023-06-14 14:12:59 +080016714 keys_it++;
developer72fb0bb2023-01-11 09:46:29 +080016715
developera3511852023-06-14 14:12:59 +080016716 if(--keysNumber <= 0)
developer72fb0bb2023-01-11 09:46:29 +080016717 break;
developera3511852023-06-14 14:12:59 +080016718 }
16719 }
developer72fb0bb2023-01-11 09:46:29 +080016720
16721close:
developera3511852023-06-14 14:12:59 +080016722 free(line);
developer37646972023-06-29 10:58:43 +080016723 if (fclose(fd) == EOF)
16724 wifi_debug(DEBUG_ERROR, "Unexpected fclose fail\n");
developera3511852023-06-14 14:12:59 +080016725 return ret;
developer72fb0bb2023-01-11 09:46:29 +080016726}
16727/* end of multi-psk support */
16728
16729INT wifi_setNeighborReports(UINT apIndex,
developera3511852023-06-14 14:12:59 +080016730 UINT numNeighborReports,
16731 wifi_NeighborReport_t *neighborReports)
developer72fb0bb2023-01-11 09:46:29 +080016732{
developera3511852023-06-14 14:12:59 +080016733 char hex_bssid[13] = { 0 };
16734 char bssid[18] = { 0 };
16735 char nr[100] = { 0 };
16736 char ssid[32];
16737 char hex_ssid[32];
16738 char interface_name[16] = {0};
16739 INT ret;
developere40952c2023-06-15 18:46:43 +080016740 int res;
developerd14dff12023-06-28 22:47:44 +080016741 unsigned char hex_ssid_len;
developer72fb0bb2023-01-11 09:46:29 +080016742
developera3511852023-06-14 14:12:59 +080016743 /*rmeove all neighbors*/
16744 wifi_dbg_printf("\n[%s]: removing all neighbors from %s\n", __func__, interface_name);
16745 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
16746 return RETURN_ERR;
developer33f13ba2023-07-12 16:19:06 +080016747
16748 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 +080016749 interface_name, interface_name);
16750
developer33f13ba2023-07-12 16:19:06 +080016751 if (res) {
16752 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developer75bd10c2023-06-27 11:34:08 +080016753 return RETURN_ERR;
16754 }
developer72fb0bb2023-01-11 09:46:29 +080016755
developera3511852023-06-14 14:12:59 +080016756 for(unsigned int i = 0; i < numNeighborReports; i++)
16757 {
16758 memset(ssid, 0, sizeof(ssid));
16759 ret = wifi_getSSIDName(apIndex, ssid);
16760 if (ret != RETURN_OK)
16761 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080016762
developera3511852023-06-14 14:12:59 +080016763 memset(hex_ssid, 0, sizeof(hex_ssid));
developerd14dff12023-06-28 22:47:44 +080016764 hex_ssid_len = sizeof(hex_ssid);
16765 for(size_t j = 0,k = 0; ssid[j] != '\0' && k < sizeof(hex_ssid); j++,k+=2 ) {
16766 res = snprintf(hex_ssid + k, hex_ssid_len, "%02x", ssid[j]);
16767
16768 if (os_snprintf_error(hex_ssid_len, res)) {
16769 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16770 return RETURN_ERR;
16771 }
16772 hex_ssid_len = sizeof(hex_ssid) - strlen(hex_ssid);
16773 }
developer72fb0bb2023-01-11 09:46:29 +080016774
developere40952c2023-06-15 18:46:43 +080016775 res = snprintf(hex_bssid, sizeof(hex_bssid),
developera3511852023-06-14 14:12:59 +080016776 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
16777 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 +080016778 if (os_snprintf_error(sizeof(hex_bssid), res)) {
16779 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16780 return RETURN_ERR;
16781 }
16782 res = snprintf(bssid, sizeof(bssid),
developera3511852023-06-14 14:12:59 +080016783 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
16784 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 +080016785 if (os_snprintf_error(sizeof(bssid), res)) {
16786 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16787 return RETURN_ERR;
16788 }
developer72fb0bb2023-01-11 09:46:29 +080016789
developere40952c2023-06-15 18:46:43 +080016790 res = snprintf(nr, sizeof(nr),
developera3511852023-06-14 14:12:59 +080016791 "%s" // bssid
16792 "%02hhx%02hhx%02hhx%02hhx" // bssid_info
16793 "%02hhx" // operclass
16794 "%02hhx" // channel
16795 "%02hhx", // phy_mode
16796 hex_bssid,
16797 neighborReports[i].info & 0xff, (neighborReports[i].info >> 8) & 0xff,
16798 (neighborReports[i].info >> 16) & 0xff, (neighborReports[i].info >> 24) & 0xff,
16799 neighborReports[i].opClass,
16800 neighborReports[i].channel,
16801 neighborReports[i].phyTable);
developere40952c2023-06-15 18:46:43 +080016802 if (os_snprintf_error(sizeof(nr), res)) {
16803 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
16804 return RETURN_ERR;
16805 }
developer72fb0bb2023-01-11 09:46:29 +080016806
developer33f13ba2023-07-12 16:19:06 +080016807 res = v_secure_system("hostapd_cli set_neighbor %s ssid=%s nr=%s -i %s",
16808 bssid, hex_ssid, nr, interface_name);
16809 if (res) {
16810 wifi_debug(DEBUG_ERROR, "v_secure_system fail\n");
developere40952c2023-06-15 18:46:43 +080016811 return RETURN_ERR;
16812 }
developera3511852023-06-14 14:12:59 +080016813 }
developer72fb0bb2023-01-11 09:46:29 +080016814
developera3511852023-06-14 14:12:59 +080016815 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016816}
16817
16818INT wifi_getApInterworkingElement(INT apIndex, wifi_InterworkingElement_t *output_struct)
16819{
developera3511852023-06-14 14:12:59 +080016820 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080016821}
16822
16823#ifdef _WIFI_HAL_TEST_
16824int main(int argc,char **argv)
16825{
developera3511852023-06-14 14:12:59 +080016826 int index;
16827 INT ret=0;
16828 char buf[1024]="";
developer72fb0bb2023-01-11 09:46:29 +080016829
developera3511852023-06-14 14:12:59 +080016830 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
16831 if(argc<3)
16832 {
16833 if(argc==2)
16834 {
16835 if(!strcmp(argv[1], "init"))
16836 return wifi_init();
16837 if(!strcmp(argv[1], "reset"))
16838 return wifi_reset();
16839 if(!strcmp(argv[1], "wifi_getHalVersion"))
16840 {
16841 char buffer[64];
16842 if(wifi_getHalVersion(buffer)==RETURN_OK)
16843 printf("Version: %s\n", buffer);
16844 else
16845 printf("Error in wifi_getHalVersion\n");
16846 return RETURN_OK;
16847 }
16848 }
16849 printf("wifihal <API> <radioIndex> <arg1> <arg2> ...\n");
16850 exit(-1);
16851 }
developer72fb0bb2023-01-11 09:46:29 +080016852
developera3511852023-06-14 14:12:59 +080016853 index = atoi(argv[2]);
developer33f13ba2023-07-12 16:19:06 +080016854 if (strstr(argv[1], "test_system")!=NULL) {
16855 ret = v_secure_system("iw phy | grep 'MHz \\[' | cut -d' ' -f2,4 > /tmp/freq-channel-map.txt");
16856 printf("ret = %d\n", ret);
16857
16858 ret = v_secure_system("hostapd_cli show_neighbor -i %s | awk '{print $1 \" \" $2}' | xargs -n2 -r hostapd_cli remove_neighbor -i %s",
16859 "ra0", "ra0");
16860 printf("ret = %d\n", ret);
16861
16862 ret = v_secure_system("echo %s > /var/prevchanval2G_AutoChannelEnable", "1,2,3");
16863 printf("ret = %d\n", ret);
16864
16865 ret = v_secure_system("ifconfig -a %s > /tmp/Radio_Stats.txt", "rai0");
16866 printf("ret = %d\n", ret);
16867
16868 ret = v_secure_system("ifconfig %s > /tmp/SSID_Stats.txt", "rax0");
16869 printf("ret = %d\n", ret);
16870
16871 ret = v_secure_system("iw dev %s station dump > /tmp/AssociatedDevice_Stats.txt", "ra0");
16872 printf("ret = %d\n", ret);
16873 ret = v_secure_system("iw dev %s station dump | grep Station >> /tmp/AllAssociated_Devices_5G.txt", "ra0");
16874 printf("ret = %d\n", ret);
16875 ret = v_secure_system("ping -q -c 1 -W 1 \"%s\" > /dev/console 2>&1", "20.1.1.101");
16876 printf("ret = %d\n", ret);
16877
16878 return 0;
16879 }
16880
16881 if (strstr(argv[1], "test_popen")!=NULL) {
16882 FILE *fp = v_secure_popen("w", "cat");
16883 fprintf(fp, "popen write success\n");
16884 fclose(fp);
16885
16886 char buf[1024];
16887 memset(buf, 0, sizeof(buf));
16888 fp = v_secure_popen("r", "echo popen read success");
16889 if (fp == NULL) {
16890 printf("v_secure_popen failed\n");
16891 } else {
16892 if (fgets(buf, sizeof(buf), fp) == NULL) {
16893 printf("v_secure_popen read error\n");
16894 } else {
16895 printf("%s\n", buf);
16896 }
16897 v_secure_pclose(fp);
16898 }
16899
16900#if 0
16901 fp = v_secure_popen("r", "hostapd_cli -i %s reload", "ra0");
16902 if (fp == NULL) {
16903 printf("v_secure_popen failed\n");
16904 } else {
16905 if (fgets(buf, sizeof(buf), fp) == NULL) {
16906 printf("v_secure_popen read error\n");
16907 } else {
16908 printf("%s", buf);
16909 }
16910 v_secure_pclose(fp);
16911 }
16912#endif
16913
16914 long int band;
16915 memset(buf, 0, sizeof(buf));
16916 ret = _syscmd_secure(buf, sizeof(buf),
16917 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 0);
16918 if (hal_strtol(buf, 16, &band) < 0) {
16919 wifi_debug(DEBUG_ERROR, "strtol fail\n");
16920 }
16921 printf("ret = %d, band0=%ld\n", ret, band);
16922
16923 memset(buf, 0, sizeof(buf));
16924 ret = _syscmd_secure(buf, sizeof(buf),
16925 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 1);
16926 if (hal_strtol(buf, 16, &band) < 0) {
16927 wifi_debug(DEBUG_ERROR, "strtol fail\n");
16928 }
16929 printf("ret = %d, band1=%ld\n", ret, band);
16930
16931 memset(buf, 0, sizeof(buf));
16932 ret = _syscmd_secure(buf, sizeof(buf),
16933 "iw phy%d info | grep 'Band .:' | tail -n 1 | tr -d ':\\n' | awk '{print $2}'", 2);
16934 if (hal_strtol(buf, 16, &band) < 0) {
16935 wifi_debug(DEBUG_ERROR, "strtol fail\n");
16936 }
16937 printf("ret = %d, band2=%ld\n", ret, band);
16938 }
16939
developera3511852023-06-14 14:12:59 +080016940 if(strstr(argv[1], "wifi_getApName")!=NULL)
16941 {
16942 wifi_getApName(index,buf);
16943 printf("Ap name is %s \n",buf);
16944 return 0;
16945 }
developerb14b3462023-07-01 18:02:42 +080016946 if(strstr(argv[1], "wifi_getRadioExtChannel")!=NULL)
16947 {
16948 wifi_getRadioExtChannel(index,buf);
16949 printf("extchannel is %s \n",buf);
16950 return 0;
16951 }
developerfead3972023-05-25 20:15:02 +080016952 if(strstr(argv[1], "wifi_setRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080016953 {
developerfead3972023-05-25 20:15:02 +080016954 UINT pureMode = atoi(argv[3]);
16955
developera3511852023-06-14 14:12:59 +080016956 wifi_setRadioMode(index, NULL, pureMode);
16957 printf("Ap SET Radio mode 0x%x\n", pureMode);
16958 return 0;
16959 }
16960 if (strstr(argv[1], "wifi_setRadioAutoBlockAckEnable") != NULL) {
16961 unsigned char enable = atoi(argv[3]);
16962 if (enable)
16963 wifi_setRadioAutoBlockAckEnable(index, TRUE);
16964 else
16965 wifi_setRadioAutoBlockAckEnable(index, FALSE);
16966 printf("%s handle wifi_setRadioAutoBlockAckEnable\n", __FUNCTION__);
16967 }
developera39cfb22023-06-20 16:28:17 +080016968 if(strstr(argv[1], "wifi_setRadioTrafficStatsRadioStatisticsEnable")!=NULL)
16969 {
16970 wifi_setRadioTrafficStatsRadioStatisticsEnable(index, TRUE);
16971 printf("Ap SET wifi_setRadioTrafficStatsRadioStatisticsEnable\n");
16972 return 0;
16973 }
16974 if(strstr(argv[1], "wifi_setRadioTrafficStatsMeasure")!=NULL)
16975 {
16976 wifi_radioTrafficStatsMeasure_t input = {30, 200};
16977
16978 wifi_setRadioTrafficStatsMeasure(index, &input);
16979 printf("Ap SET wifi_setRadioTrafficStatsMeasure\n");
16980 return 0;
16981 }
developerfead3972023-05-25 20:15:02 +080016982 if(strstr(argv[1], "wifi_setRadioTransmitPower")!=NULL)
developera3511852023-06-14 14:12:59 +080016983 {
developerfead3972023-05-25 20:15:02 +080016984 ULONG TransmitPower = atoi(argv[3]);
16985
developera3511852023-06-14 14:12:59 +080016986 wifi_setRadioTransmitPower(index, TransmitPower);
16987 printf("Ap SET TransmitPower %lu\n", TransmitPower);
16988 return 0;
16989 }
developerfead3972023-05-25 20:15:02 +080016990 if(strstr(argv[1], "wifi_setApManagementFramePowerControl")!=NULL)
developera3511852023-06-14 14:12:59 +080016991 {
developerfead3972023-05-25 20:15:02 +080016992 INT TransmitPower = atoi(argv[3]);
16993
developera3511852023-06-14 14:12:59 +080016994 wifi_setApManagementFramePowerControl(index, TransmitPower);
16995 printf("Ap SET Mgnt TransmitPower %d\n", TransmitPower);
16996 return 0;
16997 }
developerfead3972023-05-25 20:15:02 +080016998 if(strstr(argv[1], "wifi_setRadioBW")!=NULL)
developera3511852023-06-14 14:12:59 +080016999 {
developerfead3972023-05-25 20:15:02 +080017000 CHAR *bandwith = argv[3];
17001
developera3511852023-06-14 14:12:59 +080017002 wifi_setRadioOperatingChannelBandwidth(index, bandwith);
17003 printf("Ap SET bw %s\n", bandwith);
17004 return 0;
17005 }
developerfead3972023-05-25 20:15:02 +080017006 if(strstr(argv[1], "wifi_factoryResetRadio")!=NULL)
developera3511852023-06-14 14:12:59 +080017007 {
17008 wifi_factoryResetRadio(index);
17009 printf("wifi_factoryResetRadio ok!\n");
17010 return 0;
17011 }
developerfead3972023-05-25 20:15:02 +080017012 if(strstr(argv[1], "wifi_getRadioResetCount")!=NULL)
developera3511852023-06-14 14:12:59 +080017013 {
17014 ULONG rst_cnt;
17015 wifi_getRadioResetCount(index, &rst_cnt);
17016 printf("wifi_factoryResetRadio rst_cnt = %lu\n", rst_cnt);
17017 return 0;
17018 }
developer2edaf012023-05-24 14:24:53 +080017019 if (strncmp(argv[1], "wifi_addApAclDevice", strlen(argv[1])) == 0) {
developer49b17232023-05-19 16:35:19 +080017020 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017021 {
17022 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17023 exit(-1);
17024 }
developer49b17232023-05-19 16:35:19 +080017025 wifi_addApAclDevice(index, argv[3]);
17026 return 0;
17027 }
developer2edaf012023-05-24 14:24:53 +080017028 if (strncmp(argv[1], "wifi_getApAclDevices", strlen(argv[1])) == 0) {
17029 wifi_getApAclDevices(index, buf, 1024);
17030 wifi_debug(DEBUG_NOTICE, "Ap acl Devices: %s\n", buf);
developer121a8e72023-05-22 09:19:39 +080017031 return 0;
17032 }
developer2edaf012023-05-24 14:24:53 +080017033 if (strncmp(argv[1], "wifi_delApAclDevice", strlen(argv[1])) == 0) {
17034 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017035 {
17036 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17037 exit(-1);
17038 }
developer2edaf012023-05-24 14:24:53 +080017039 wifi_delApAclDevice(index, argv[3]);
17040 return 0;
17041 }
17042 if (strncmp(argv[1], "wifi_delApAclDevices", strlen(argv[1])) == 0) {
17043 wifi_delApAclDevices(index);
17044 return 0;
17045 }
17046 if (strncmp(argv[1], "wifi_getApAclDeviceNum", strlen(argv[1])) == 0) {
developer863a4a62023-06-06 16:55:59 +080017047 UINT acl_num = 0;
developer2edaf012023-05-24 14:24:53 +080017048 wifi_getApAclDeviceNum(index, &acl_num);
17049 wifi_debug(DEBUG_NOTICE, "Ap acl numbers: %d\n", acl_num);
17050 return 0;
17051 }
17052 if (strncmp(argv[1], "wifi_getApDenyAclDevices", strlen(argv[1])) == 0) {
17053 wifi_getApDenyAclDevices(index, buf, 1024);
17054 wifi_debug(DEBUG_NOTICE, "Ap Deny Acl Devices: %s\n", buf);
17055 return 0;
17056 }
17057 if (strncmp(argv[1], "wifi_setApMacAddressControlMode", strlen(argv[1])) == 0) {
17058 int filter_mode = 0;
17059 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017060 {
17061 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17062 exit(-1);
17063 }
developer2edaf012023-05-24 14:24:53 +080017064 filter_mode = atoi(argv[3]);
17065 wifi_setApMacAddressControlMode(index,filter_mode);
17066 return 0;
17067 }
developer5cd4c862023-05-26 09:34:42 +080017068 if (strncmp(argv[1], "wifi_getRadioDeclineBARequestEnable", strlen(argv[1])) == 0) {
17069 BOOL output_bool = 0;
17070 wifi_getRadioDeclineBARequestEnable(index, &output_bool);
17071 wifi_debug(DEBUG_NOTICE, "Ap get radio ba decline enable: %d\n", output_bool);
17072 return 0;
17073 }
17074 if (strncmp(argv[1], "wifi_getRadioAutoBlockAckEnable", strlen(argv[1])) == 0) {
17075 BOOL output_bool = 0;
17076 wifi_getRadioAutoBlockAckEnable(index, &output_bool);
17077 wifi_debug(DEBUG_NOTICE, "Ap get radio auto_ba enable: %d\n", output_bool);
17078 return 0;
17079 }
17080
17081 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17082 int filter_mode = 0;
17083 wifi_getApMacAddressControlMode(index, &filter_mode);
17084 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17085 return 0;
17086 }
17087 if (strncmp(argv[1], "wifi_setRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17088 int enable = 0;
17089 if(argc <= 3 )
developera3511852023-06-14 14:12:59 +080017090 {
17091 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17092 exit(-1);
17093 }
developer5cd4c862023-05-26 09:34:42 +080017094 enable = (BOOL)atoi(argv[3]);
17095 wifi_setRadioIGMPSnoopingEnable(index, enable);
17096 wifi_debug(DEBUG_NOTICE, "Ap set IGMP Snooping Enable: %d\n", enable);
17097 return 0;
17098 }
developer326d4232023-06-15 16:45:30 +080017099 if (strncmp(argv[1], "wifi_setRadioDCSEnable(", strlen(argv[1])) == 0) {
17100 int enable = 0;
17101 if(argc <= 3 )
17102 {
17103 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17104 exit(-1);
17105 }
17106 enable = (BOOL)atoi(argv[3]);
17107 wifi_setRadioDCSEnable(index, enable);
17108 wifi_debug(DEBUG_NOTICE, "Ap set DCS Enable: %d\n", enable);
17109 return 0;
17110 }
17111 if (strncmp(argv[1], "wifi_getRadioAutoChannelRefreshPeriod(", strlen(argv[1])) == 0) {
17112 ULONG period = 0;
developer5cd4c862023-05-26 09:34:42 +080017113
developer326d4232023-06-15 16:45:30 +080017114 wifi_getRadioAutoChannelRefreshPeriod(index, &period);
17115 wifi_debug(DEBUG_NOTICE, "Get RefreshPeriod: %ld\n", period);
17116 return 0;
17117 }
17118 if (strncmp(argv[1], "wifi_setRadioDfsRefreshPeriod(", strlen(argv[1])) == 0) {
17119 ULONG period = 0;
17120
17121 period = (ULONG)atoi(argv[3]);
17122 wifi_setRadioDfsRefreshPeriod(index, period);
17123 wifi_debug(DEBUG_NOTICE, "Set RefreshPeriod: %ld\n", period);
17124 return 0;
17125 }
17126 if (strncmp(argv[1], "wifi_setRadioDCSChannelPool(", strlen(argv[1])) == 0) {
17127 char pool[256] = {'\0'};
17128
developerc14d83a2023-06-29 20:09:42 +080017129 strncpy(pool, argv[3], strlen(argv[3]));
developer326d4232023-06-15 16:45:30 +080017130 wifi_setRadioDCSChannelPool(index, pool);
17131 wifi_debug(DEBUG_NOTICE, "Set DCSChannelPool: %s\n", pool);
17132 return 0;
17133 }
17134 if (strncmp(argv[1], "wifi_getRadioDCSChannelPool(", strlen(argv[1])) == 0) {
17135 char pool[256] = {'\0'};
17136
17137 wifi_getRadioDCSChannelPool(index, pool);
17138 wifi_debug(DEBUG_NOTICE, "Get DCSChannelPool: %s\n", pool);
17139 return 0;
17140 }
developer5cd4c862023-05-26 09:34:42 +080017141 if (strncmp(argv[1], "wifi_getRadioIGMPSnoopingEnable", strlen(argv[1])) == 0) {
17142 BOOL out_status = 0;
17143 wifi_getRadioIGMPSnoopingEnable(index, &out_status);
17144 wifi_debug(DEBUG_NOTICE, "Ap get IGMP Snooping Enable: %d\n", out_status);
17145 return 0;
17146 }
developer121a8e72023-05-22 09:19:39 +080017147
developer95c045d2023-05-24 19:26:28 +080017148 if (strncmp(argv[1], "wifi_setApWmmEnable", strlen(argv[1])) == 0) {
17149 int enable = 0;
17150 if(argc <= 3)
17151 {
17152 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17153 exit(-1);
17154 }
17155 enable = atoi(argv[3]);
17156 wifi_setApWmmEnable(index,enable);
17157 return 0;
17158 }
developerc1aa6532023-06-09 09:37:01 +080017159 if (strncmp(argv[1], "wifi_pushSsidAdvertisementEnable", strlen(argv[1])) == 0) {
17160 int enable = 0;
17161 if(argc <= 3)
17162 {
17163 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17164 exit(-1);
17165 }
17166 enable = atoi(argv[3]);
17167 wifi_pushSsidAdvertisementEnable(index,enable);
17168 return 0;
17169 }
developer56fbedb2023-05-30 16:47:05 +080017170 if (strncmp(argv[1], "wifi_down", strlen(argv[1])) == 0) {
17171 wifi_down();
17172 return 0;
17173 }
developer95c045d2023-05-24 19:26:28 +080017174
developer56fbedb2023-05-30 16:47:05 +080017175 if (strncmp(argv[1], "wifi_getRadioStatus", strlen(argv[1])) == 0) {
17176 BOOL enable = 0;
17177
17178 wifi_getRadioStatus(index, &enable);
17179 wifi_debug(DEBUG_NOTICE, "wifi_getRadioStatus enable: %d\n", (int)enable);
17180 return 0;
17181 }
developer333c1eb2023-05-31 14:59:39 +080017182
developer95c045d2023-05-24 19:26:28 +080017183 if (strncmp(argv[1], "wifi_getApWMMCapability", strlen(argv[1])) == 0) {
17184 BOOL enable = 0;
17185
17186 wifi_getApWMMCapability(index, &enable);
17187 wifi_debug(DEBUG_NOTICE, "wifi_getApWMMCapability enable: %d\n", (int)enable);
17188 return 0;
17189 }
17190
17191 if (strncmp(argv[1], "wifi_getApWmmEnable", strlen(argv[1])) == 0) {
17192 BOOL enable = 0;
17193
17194 wifi_getApWmmEnable(index, &enable);
17195 wifi_debug(DEBUG_NOTICE, "wifi_getApWmmEnable enable: %d\n", (int)enable);
17196 return 0;
17197 }
17198
developer2edaf012023-05-24 14:24:53 +080017199 if (strncmp(argv[1], "wifi_getApMacAddressControlMode", strlen(argv[1])) == 0) {
17200 int filter_mode = 0;
17201 wifi_getApMacAddressControlMode(index, &filter_mode);
17202 wifi_debug(DEBUG_NOTICE, "Ap MacAddress Control Mode: %d\n", filter_mode);
17203 return 0;
17204 }
developer0f10c772023-05-16 21:43:39 +080017205 if(strstr(argv[1], "wifi_getRadioMode")!=NULL)
developera3511852023-06-14 14:12:59 +080017206 {
developer863a4a62023-06-06 16:55:59 +080017207 UINT mode = 0;
developer0f10c772023-05-16 21:43:39 +080017208
developera3511852023-06-14 14:12:59 +080017209 wifi_getRadioMode(index, buf, &mode);
17210 printf("Ap Radio mode is %s , mode = 0x%x\n", buf, mode);
17211 return 0;
17212 }
17213 if(strstr(argv[1], "wifi_getRadioAutoChannelEnable")!=NULL)
17214 {
17215 BOOL b = FALSE;
17216 BOOL *output_bool = &b;
17217 wifi_getRadioAutoChannelEnable(index,output_bool);
17218 printf("Channel enabled = %d \n",b);
17219 return 0;
17220 }
17221 if(strstr(argv[1], "wifi_getApWpaEncryptionMode")!=NULL)
17222 {
17223 wifi_getApWpaEncryptionMode(index,buf);
17224 printf("encryption enabled = %s\n",buf);
17225 return 0;
17226 }
17227 if(strstr(argv[1], "wifi_getApSsidAdvertisementEnable")!=NULL)
17228 {
17229 BOOL b = FALSE;
17230 BOOL *output_bool = &b;
17231 wifi_getApSsidAdvertisementEnable(index,output_bool);
17232 printf("advertisment enabled = %d\n",b);
17233 return 0;
17234 }
17235 if(strstr(argv[1],"wifi_getApAssociatedDeviceTidStatsResult")!=NULL)
17236 {
17237 if(argc <= 3 )
17238 {
17239 printf("Insufficient arguments \n");
17240 exit(-1);
17241 }
developer72fb0bb2023-01-11 09:46:29 +080017242
developera3511852023-06-14 14:12:59 +080017243 char sta[20] = {'\0'};
17244 ULLONG handle= 0;
developerc14d83a2023-06-29 20:09:42 +080017245 strncpy(sta,argv[3], strlen(argv[3]));
developera3511852023-06-14 14:12:59 +080017246 mac_address_t st;
developer72fb0bb2023-01-11 09:46:29 +080017247 mac_addr_aton(st,sta);
17248
developera3511852023-06-14 14:12:59 +080017249 wifi_associated_dev_tid_stats_t tid_stats;
17250 wifi_getApAssociatedDeviceTidStatsResult(index,&st,&tid_stats,&handle);
17251 for(int tid_index=0; tid_index<PS_MAX_TID; tid_index++) //print tid stats
17252 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);
17253 }
developer72fb0bb2023-01-11 09:46:29 +080017254
developera3511852023-06-14 14:12:59 +080017255 if(strstr(argv[1], "getApEnable")!=NULL) {
17256 BOOL enable;
17257 ret=wifi_getApEnable(index, &enable);
17258 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17259 }
17260 else if(strstr(argv[1], "setApEnable")!=NULL) {
17261 BOOL enable = atoi(argv[3]);
17262 ret=wifi_setApEnable(index, enable);
17263 printf("%s %d: %d, returns %d\n", argv[1], index, enable, ret);
17264 }
17265 else if(strstr(argv[1], "getApStatus")!=NULL) {
17266 char status[64];
17267 ret=wifi_getApStatus(index, status);
17268 printf("%s %d: %s, returns %d\n", argv[1], index, status, ret);
17269 }
17270 else if(strstr(argv[1], "wifi_getSSIDNameStatus")!=NULL)
17271 {
17272 wifi_getSSIDNameStatus(index,buf);
17273 printf("%s %d: active ssid : %s\n",argv[1], index,buf);
17274 return 0;
17275 } else if(strstr(argv[1], "wifi_resetApVlanCfg")!=NULL) {
17276 wifi_resetApVlanCfg(index);
17277 printf("%s %d: wifi_resetApVlanCfg : %s\n",argv[1], index,buf);
17278 return 0;
17279 }
17280 else if(strstr(argv[1], "getSSIDTrafficStats2")!=NULL) {
17281 wifi_ssidTrafficStats2_t stats={0};
17282 ret=wifi_getSSIDTrafficStats2(index, &stats); //Tr181
17283 printf("%s %d: returns %d\n", argv[1], index, ret);
17284 printf(" ssid_BytesSent =%lu\n", stats.ssid_BytesSent);
17285 printf(" ssid_BytesReceived =%lu\n", stats.ssid_BytesReceived);
17286 printf(" ssid_PacketsSent =%lu\n", stats.ssid_PacketsSent);
17287 printf(" ssid_PacketsReceived =%lu\n", stats.ssid_PacketsReceived);
17288 printf(" ssid_RetransCount =%lu\n", stats.ssid_RetransCount);
17289 printf(" ssid_FailedRetransCount =%lu\n", stats.ssid_FailedRetransCount);
17290 printf(" ssid_RetryCount =%lu\n", stats.ssid_RetryCount);
17291 printf(" ssid_MultipleRetryCount =%lu\n", stats.ssid_MultipleRetryCount);
17292 printf(" ssid_ACKFailureCount =%lu\n", stats.ssid_ACKFailureCount);
17293 printf(" ssid_AggregatedPacketCount =%lu\n", stats.ssid_AggregatedPacketCount);
17294 printf(" ssid_ErrorsSent =%lu\n", stats.ssid_ErrorsSent);
17295 printf(" ssid_ErrorsReceived =%lu\n", stats.ssid_ErrorsReceived);
17296 printf(" ssid_UnicastPacketsSent =%lu\n", stats.ssid_UnicastPacketsSent);
17297 printf(" ssid_UnicastPacketsReceived =%lu\n", stats.ssid_UnicastPacketsReceived);
17298 printf(" ssid_DiscardedPacketsSent =%lu\n", stats.ssid_DiscardedPacketsSent);
17299 printf(" ssid_DiscardedPacketsReceived =%lu\n", stats.ssid_DiscardedPacketsReceived);
17300 printf(" ssid_MulticastPacketsSent =%lu\n", stats.ssid_MulticastPacketsSent);
17301 printf(" ssid_MulticastPacketsReceived =%lu\n", stats.ssid_MulticastPacketsReceived);
17302 printf(" ssid_BroadcastPacketsSent =%lu\n", stats.ssid_BroadcastPacketsSent);
17303 printf(" ssid_BroadcastPacketsRecevied =%lu\n", stats.ssid_BroadcastPacketsRecevied);
17304 printf(" ssid_UnknownPacketsReceived =%lu\n", stats.ssid_UnknownPacketsReceived);
17305 }
17306 else if(strstr(argv[1], "getNeighboringWiFiDiagnosticResult2")!=NULL) {
17307 wifi_neighbor_ap2_t *neighbor_ap_array=NULL, *pt=NULL;
17308 UINT array_size=0;
17309 UINT i=0;
17310 ret=wifi_getNeighboringWiFiDiagnosticResult2(index, &neighbor_ap_array, &array_size);
17311 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17312 for(i=0, pt=neighbor_ap_array; i<array_size; i++, pt++) {
17313 printf(" neighbor %d:\n", i);
17314 printf(" ap_SSID =%s\n", pt->ap_SSID);
17315 printf(" ap_BSSID =%s\n", pt->ap_BSSID);
17316 printf(" ap_Mode =%s\n", pt->ap_Mode);
17317 printf(" ap_Channel =%d\n", pt->ap_Channel);
17318 printf(" ap_SignalStrength =%d\n", pt->ap_SignalStrength);
17319 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17320 printf(" ap_EncryptionMode =%s\n", pt->ap_EncryptionMode);
17321 printf(" ap_SupportedStandards =%s\n", pt->ap_SupportedStandards);
17322 printf(" ap_OperatingStandards =%s\n", pt->ap_OperatingStandards);
17323 printf(" ap_OperatingChannelBandwidth =%s\n", pt->ap_OperatingChannelBandwidth);
17324 printf(" ap_SecurityModeEnabled =%s\n", pt->ap_SecurityModeEnabled);
17325 printf(" ap_BeaconPeriod =%d\n", pt->ap_BeaconPeriod);
17326 printf(" ap_Noise =%d\n", pt->ap_Noise);
17327 printf(" ap_BasicDataTransferRates =%s\n", pt->ap_BasicDataTransferRates);
17328 printf(" ap_SupportedDataTransferRates =%s\n", pt->ap_SupportedDataTransferRates);
17329 printf(" ap_DTIMPeriod =%d\n", pt->ap_DTIMPeriod);
17330 printf(" ap_ChannelUtilization =%d\n", pt->ap_ChannelUtilization);
17331 }
17332 if(neighbor_ap_array)
17333 free(neighbor_ap_array); //make sure to free the list
17334 }
17335 else if(strstr(argv[1], "getApAssociatedDeviceDiagnosticResult")!=NULL) {
17336 wifi_associated_dev_t *associated_dev_array=NULL, *pt=NULL;
17337 UINT array_size=0;
17338 UINT i=0;
17339 ret=wifi_getApAssociatedDeviceDiagnosticResult(index, &associated_dev_array, &array_size);
17340 printf("%s %d: array_size=%d, returns %d\n", argv[1], index, array_size, ret);
17341 for(i=0, pt=associated_dev_array; i<array_size; i++, pt++) {
17342 printf(" associated_dev %d:\n", i);
17343 printf(" cli_OperatingStandard =%s\n", pt->cli_OperatingStandard);
17344 printf(" cli_OperatingChannelBandwidth =%s\n", pt->cli_OperatingChannelBandwidth);
17345 printf(" cli_SNR =%d\n", pt->cli_SNR);
17346 printf(" cli_InterferenceSources =%s\n", pt->cli_InterferenceSources);
17347 printf(" cli_DataFramesSentAck =%lu\n", pt->cli_DataFramesSentAck);
17348 printf(" cli_DataFramesSentNoAck =%lu\n", pt->cli_DataFramesSentNoAck);
17349 printf(" cli_BytesSent =%lu\n", pt->cli_BytesSent);
17350 printf(" cli_BytesReceived =%lu\n", pt->cli_BytesReceived);
17351 printf(" cli_RSSI =%d\n", pt->cli_RSSI);
17352 printf(" cli_MinRSSI =%d\n", pt->cli_MinRSSI);
17353 printf(" cli_MaxRSSI =%d\n", pt->cli_MaxRSSI);
17354 printf(" cli_Disassociations =%d\n", pt->cli_Disassociations);
17355 printf(" cli_AuthenticationFailures =%d\n", pt->cli_AuthenticationFailures);
17356 }
17357 if(associated_dev_array)
17358 free(associated_dev_array); //make sure to free the list
17359 }
developer72fb0bb2023-01-11 09:46:29 +080017360
developera3511852023-06-14 14:12:59 +080017361 if(strstr(argv[1],"wifi_getRadioChannelStats")!=NULL)
17362 {
developer72fb0bb2023-01-11 09:46:29 +080017363#define MAX_ARRAY_SIZE 64
developera3511852023-06-14 14:12:59 +080017364 int i, array_size;
17365 char *p, *ch_str;
17366 wifi_channelStats_t input_output_channelStats_array[MAX_ARRAY_SIZE];
developer72fb0bb2023-01-11 09:46:29 +080017367
developera3511852023-06-14 14:12:59 +080017368 if(argc != 5)
17369 {
17370 printf("Insufficient arguments, Usage: wifihal wifi_getRadioChannelStats <AP-Index> <Array-Size> <Comma-seperated-channel-numbers>\n");
17371 exit(-1);
17372 }
17373 memset(input_output_channelStats_array, 0, sizeof(input_output_channelStats_array));
developer72fb0bb2023-01-11 09:46:29 +080017374
developera3511852023-06-14 14:12:59 +080017375 for (i=0, array_size=atoi(argv[3]), ch_str=argv[4]; i<array_size; i++, ch_str=p)
17376 {
17377 strtok_r(ch_str, ",", &p);
17378 input_output_channelStats_array[i].ch_number = atoi(ch_str);
17379 }
17380 wifi_getRadioChannelStats(atoi(argv[2]), input_output_channelStats_array, array_size);
17381 if(!array_size)
17382 array_size=1;//Need to print current channel statistics
17383 for(i=0; i<array_size; i++)
17384 printf("chan num = %d \t, noise =%d\t ch_utilization_busy_rx = %lld \t,\
17385 ch_utilization_busy_tx = %lld \t,ch_utilization_busy = %lld \t,\
17386 ch_utilization_busy_ext = %lld \t, ch_utilization_total = %lld \t \n",\
17387 input_output_channelStats_array[i].ch_number,\
17388 input_output_channelStats_array[i].ch_noise,\
17389 input_output_channelStats_array[i].ch_utilization_busy_rx,\
17390 input_output_channelStats_array[i].ch_utilization_busy_tx,\
17391 input_output_channelStats_array[i].ch_utilization_busy,\
17392 input_output_channelStats_array[i].ch_utilization_busy_ext,\
17393 input_output_channelStats_array[i].ch_utilization_total);
17394 }
developer72fb0bb2023-01-11 09:46:29 +080017395
developera3511852023-06-14 14:12:59 +080017396 if(strstr(argv[1],"wifi_getAssociatedDeviceDetail")!=NULL)
17397 {
17398 if(argc <= 3 )
17399 {
17400 printf("Insufficient arguments \n");
17401 exit(-1);
17402 }
17403 char mac_addr[20] = {'\0'};
17404 wifi_device_t output_struct;
17405 int dev_index = atoi(argv[3]);
developer72fb0bb2023-01-11 09:46:29 +080017406
developera3511852023-06-14 14:12:59 +080017407 wifi_getAssociatedDeviceDetail(index,dev_index,&output_struct);
17408 mac_addr_ntoa(mac_addr,output_struct.wifi_devMacAddress);
17409 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);
17410 }
developer72fb0bb2023-01-11 09:46:29 +080017411
developera3511852023-06-14 14:12:59 +080017412 if(strstr(argv[1],"wifi_setNeighborReports")!=NULL)
17413 {
17414 if (argc <= 3)
17415 {
17416 printf("Insufficient arguments\n");
17417 exit(-1);
17418 }
17419 char args[256];
17420 wifi_NeighborReport_t *neighborReports;
developer72fb0bb2023-01-11 09:46:29 +080017421
developera3511852023-06-14 14:12:59 +080017422 neighborReports = calloc(argc - 2, sizeof(neighborReports));
17423 if (!neighborReports)
17424 {
17425 printf("Failed to allocate memory");
17426 exit(-1);
17427 }
developer72fb0bb2023-01-11 09:46:29 +080017428
developera3511852023-06-14 14:12:59 +080017429 for (int i = 3; i < argc; ++i)
17430 {
17431 char *val;
17432 int j = 0;
developerc14d83a2023-06-29 20:09:42 +080017433 unsigned long tmp;
developera3511852023-06-14 14:12:59 +080017434 memset(args, 0, sizeof(args));
17435 strncpy(args, argv[i], sizeof(args));
17436 val = strtok(args, ";");
17437 while (val != NULL)
17438 {
17439 if (j == 0)
17440 {
17441 mac_addr_aton(neighborReports[i - 3].bssid, val);
17442 } else if (j == 1)
17443 {
developerc14d83a2023-06-29 20:09:42 +080017444 if (hal_strtoul(val, 16, &tmp) < 0) {
17445 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017446 }
17447 neighborReports[i - 3].info = tmp;
developera3511852023-06-14 14:12:59 +080017448 } else if (j == 2)
17449 {
developerc14d83a2023-06-29 20:09:42 +080017450 if (hal_strtoul(val, 16, &tmp) < 0) {
17451 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017452 }
17453 neighborReports[i - 3].opClass = tmp;
17454
developera3511852023-06-14 14:12:59 +080017455 } else if (j == 3)
17456 {
developerc14d83a2023-06-29 20:09:42 +080017457 if (hal_strtoul(val, 16, &tmp) < 0) {
17458 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017459 }
17460 neighborReports[i - 3].channel = tmp;
developera3511852023-06-14 14:12:59 +080017461 } else if (j == 4)
17462 {
developerc14d83a2023-06-29 20:09:42 +080017463 if (hal_strtoul(val, 16, &tmp) < 0) {
17464 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017465 }
17466 neighborReports[i - 3].phyTable = tmp;
developera3511852023-06-14 14:12:59 +080017467 } else {
17468 printf("Insufficient arguments]n\n");
17469 exit(-1);
17470 }
17471 val = strtok(NULL, ";");
17472 j++;
17473 }
17474 }
developer72fb0bb2023-01-11 09:46:29 +080017475
developera3511852023-06-14 14:12:59 +080017476 INT ret = wifi_setNeighborReports(index, argc - 3, neighborReports);
17477 if (ret != RETURN_OK)
17478 {
17479 printf("wifi_setNeighborReports ret = %d", ret);
17480 exit(-1);
17481 }
17482 }
17483 if(strstr(argv[1],"wifi_getRadioIfName")!=NULL)
17484 {
17485 if((ret=wifi_getRadioIfName(index, buf))==RETURN_OK)
17486 printf("%s.\n", buf);
17487 else
17488 printf("Error returned\n");
17489 }
17490 if(strstr(argv[1],"wifi_getApSecurityModesSupported")!=NULL)
17491 {
17492 if((ret=wifi_getApSecurityModesSupported(index, buf))==RETURN_OK)
17493 printf("%s.\n", buf);
17494 else
17495 printf("Error returned\n");
17496 }
17497 if(strstr(argv[1],"wifi_getRadioOperatingChannelBandwidth")!=NULL)
17498 {
17499 if (argc <= 2)
17500 {
17501 printf("Insufficient arguments\n");
17502 exit(-1);
17503 }
17504 char buf[64]= {'\0'};
17505 wifi_getRadioOperatingChannelBandwidth(index,buf);
17506 printf("Current bandwidth is %s \n",buf);
17507 return 0;
17508 }
17509 if(strstr(argv[1],"pushRadioChannel2")!=NULL)
17510 {
17511 if (argc <= 5)
17512 {
17513 printf("Insufficient arguments\n");
17514 exit(-1);
17515 }
17516 UINT channel = atoi(argv[3]);
17517 UINT width = atoi(argv[4]);
17518 UINT beacon = atoi(argv[5]);
17519 INT ret = wifi_pushRadioChannel2(index,channel,width,beacon);
17520 printf("Result = %d", ret);
17521 }
developercc5cbfb2023-06-13 18:29:52 +080017522 if(strstr(argv[1],"wifi_getApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017523 {
developercc5cbfb2023-06-13 18:29:52 +080017524 char br_name[64], ip[64], subset[64] = {0};
17525 wifi_getApBridgeInfo(0, br_name, ip, subset);
17526 printf("wifi_getApBridgeInfo br_name = %s, ip = %s, subset = %s\n", br_name, ip, subset);
developera3511852023-06-14 14:12:59 +080017527 }
developercc5cbfb2023-06-13 18:29:52 +080017528 if(strstr(argv[1],"wifi_enableGreylistAccessControl")!=NULL)
developera3511852023-06-14 14:12:59 +080017529 {
developercc5cbfb2023-06-13 18:29:52 +080017530 int enable = atoi(argv[3]);
17531 wifi_enableGreylistAccessControl(enable == 0 ? FALSE : TRUE);
17532 printf("wifi_enableGreylistAccessControl enable=%d\n", enable);
developera3511852023-06-14 14:12:59 +080017533 }
developercc5cbfb2023-06-13 18:29:52 +080017534 if(strstr(argv[1],"wifi_setApBridgeInfo")!=NULL)
developera3511852023-06-14 14:12:59 +080017535 {
developercc5cbfb2023-06-13 18:29:52 +080017536 wifi_setApBridgeInfo(0, argv[3], argv[4], argv[5]);
17537 printf("wifi_setApBridgeInfo br_name = %s, ip = %s, subset = %s\n", argv[3], argv[4], argv[5]);
developera3511852023-06-14 14:12:59 +080017538 }
developer72fb0bb2023-01-11 09:46:29 +080017539
developer6e578302023-06-21 10:11:16 +080017540 if(strstr(argv[1], "wifi_getATMCapable")!=NULL)
17541 {
17542 BOOL b = FALSE;
17543 BOOL *output_bool = &b;
17544 wifi_getATMCapable(output_bool);
17545 printf("ATM capable = %d \n",b);
17546 return 0;
17547 }
17548 if (strncmp(argv[1], "wifi_setATMEnable", strlen(argv[1])) == 0) {
17549 int enable = 0;
17550 if(argc <= 3)
17551 {
17552 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17553 exit(-1);
17554 }
17555 enable = atoi(argv[3]);
17556 wifi_setATMEnable(enable);
17557 return 0;
17558 }
17559 if (strncmp(argv[1], "wifi_getATMEnable", strlen(argv[1])) == 0) {
17560 BOOL b = FALSE;
17561 BOOL *output_bool = &b;
17562 wifi_getATMEnable(output_bool);
17563 printf("ATM enable = %d \n", b);
17564 return 0;
17565 }
17566 if (strncmp(argv[1], "wifi_setApATMAirTimePercent", strlen(argv[1])) == 0) {
17567 unsigned int percent = 0;
17568 if(argc <= 3)
17569 {
17570 wifi_debug(DEBUG_ERROR, "Insufficient arguments \n");
17571 exit(-1);
17572 }
17573 percent = atoi(argv[3]);
17574 wifi_setApATMAirTimePercent(index, percent);
17575 return 0;
17576 }
17577 if (strncmp(argv[1], "wifi_getApATMAirTimePercent", strlen(argv[1])) == 0) {
17578 unsigned int percent = 0;
17579 unsigned int *output = &percent;
17580
17581 wifi_getApATMAirTimePercent(index, output);
17582 printf("ATM percent = %d \n", percent);
17583 return 0;
17584 }
developer82533be2023-06-28 17:21:01 +080017585 if (strstr(argv[1],"setGF")!=NULL)
17586 {
17587 BOOL enable = atoi(argv[3]);
17588 if((ret=wifi_setRadio11nGreenfieldEnable(index, enable))==RETURN_OK)
17589 printf("wifi_setRadio11nGreenfieldEnable success\n");
17590 else
17591 printf("wifi_setRadio11nGreenfieldEnable Error\n");
17592 }
17593 if (strstr(argv[1],"setVID")!=NULL)
17594 {
17595 INT vid = atoi(argv[3]);
17596 if((ret=wifi_setApVlanID(index, vid))==RETURN_OK)
17597 printf("wifi_setApVlanID success.\n");
17598 else
17599 printf("wifi_setApVlanID Error\n");
developerd14dff12023-06-28 22:47:44 +080017600 }
17601 if (strncmp(argv[1], "wifi_getApATMSta", strlen(argv[1])) == 0) {
17602 UCHAR outbuf[256]={0};
17603
17604 wifi_getApATMSta(index, outbuf, sizeof(outbuf));
17605 printf("sta air time percent is %s \n", outbuf);
17606 return 0;
17607 }
developera3511852023-06-14 14:12:59 +080017608 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17609 return 0;
developer72fb0bb2023-01-11 09:46:29 +080017610}
17611
17612#endif
17613
17614#ifdef WIFI_HAL_VERSION_3
17615
developer32f2a182023-06-27 19:50:41 +080017616INT BitMapToTransmitRates(UINT bitMap, char *BasicRate, unsigned long size)
developer72fb0bb2023-01-11 09:46:29 +080017617{
developera3511852023-06-14 14:12:59 +080017618 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer32f2a182023-06-27 19:50:41 +080017619 if (bitMap & WIFI_BITRATE_1MBPS) {
17620 if ((size - strlen(BasicRate)) <= 2)
17621 return RETURN_ERR;
17622 strncat(BasicRate, "1,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17623 }
17624 if (bitMap & WIFI_BITRATE_2MBPS) {
17625 if ((size - strlen(BasicRate)) <= 2)
17626 return RETURN_ERR;
17627 strncat(BasicRate, "2,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17628 }
17629 if (bitMap & WIFI_BITRATE_5_5MBPS) {
17630 if ((size - strlen(BasicRate)) <= 4)
17631 return RETURN_ERR;
17632 strncat(BasicRate, "5.5,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17633 }
17634 if (bitMap & WIFI_BITRATE_6MBPS) {
17635 if ((size - strlen(BasicRate)) <= 2)
17636 return RETURN_ERR;
17637 strncat(BasicRate, "6,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17638 }
17639 if (bitMap & WIFI_BITRATE_9MBPS) {
17640 if ((size - strlen(BasicRate)) <= 2)
17641 return RETURN_ERR;
17642 strncat(BasicRate, "9,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17643 }
17644 if (bitMap & WIFI_BITRATE_11MBPS) {
17645 if ((size - strlen(BasicRate)) <= 3)
17646 return RETURN_ERR;
17647 strncat(BasicRate, "11,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17648 }
17649 if (bitMap & WIFI_BITRATE_12MBPS) {
17650 if ((size - strlen(BasicRate)) <= 3)
17651 return RETURN_ERR;
17652 strncat(BasicRate, "12,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17653 }
17654 if (bitMap & WIFI_BITRATE_18MBPS) {
17655 if ((size - strlen(BasicRate)) <= 3)
17656 return RETURN_ERR;
17657 strncat(BasicRate, "18,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17658 }
17659 if (bitMap & WIFI_BITRATE_24MBPS) {
17660 if ((size - strlen(BasicRate)) <= 3)
17661 return RETURN_ERR;
17662 strncat(BasicRate, "24,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17663 }
17664 if (bitMap & WIFI_BITRATE_36MBPS) {
17665 if ((size - strlen(BasicRate)) <= 3)
17666 return RETURN_ERR;
17667 strncat(BasicRate, "36,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17668 }
17669 if (bitMap & WIFI_BITRATE_48MBPS) {
17670 if ((size - strlen(BasicRate)) <= 3)
17671 return RETURN_ERR;
17672 strncat(BasicRate, "48,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17673 }
17674 if (bitMap & WIFI_BITRATE_54MBPS) {
17675 if ((size - strlen(BasicRate)) <= 3)
17676 return RETURN_ERR;
17677 strncat(BasicRate, "54,", sizeof(BasicRate) - strlen(BasicRate) - 1);
17678 }
developera3511852023-06-14 14:12:59 +080017679 if (strlen(BasicRate) != 0) // remove last comma
17680 BasicRate[strlen(BasicRate) - 1] = '\0';
17681 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17682 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017683}
17684
17685INT TransmitRatesToBitMap (char *BasicRatesList, UINT *basicRateBitMap)
17686{
developera3511852023-06-14 14:12:59 +080017687 UINT BitMap = 0;
17688 char *rate;
developer72fb0bb2023-01-11 09:46:29 +080017689
developera3511852023-06-14 14:12:59 +080017690 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
17691 rate = strtok(BasicRatesList, ",");
17692 while(rate != NULL)
17693 {
17694 if (strcmp(rate, "1") == 0)
17695 BitMap |= WIFI_BITRATE_1MBPS;
17696 else if (strcmp(rate, "2") == 0)
17697 BitMap |= WIFI_BITRATE_2MBPS;
17698 else if (strcmp(rate, "5.5") == 0)
17699 BitMap |= WIFI_BITRATE_5_5MBPS;
17700 else if (strcmp(rate, "6") == 0)
17701 BitMap |= WIFI_BITRATE_6MBPS;
17702 else if (strcmp(rate, "9") == 0)
17703 BitMap |= WIFI_BITRATE_9MBPS;
17704 else if (strcmp(rate, "11") == 0)
17705 BitMap |= WIFI_BITRATE_11MBPS;
17706 else if (strcmp(rate, "12") == 0)
17707 BitMap |= WIFI_BITRATE_12MBPS;
17708 else if (strcmp(rate, "18") == 0)
17709 BitMap |= WIFI_BITRATE_18MBPS;
17710 else if (strcmp(rate, "24") == 0)
17711 BitMap |= WIFI_BITRATE_24MBPS;
17712 else if (strcmp(rate, "36") == 0)
17713 BitMap |= WIFI_BITRATE_36MBPS;
17714 else if (strcmp(rate, "48") == 0)
17715 BitMap |= WIFI_BITRATE_48MBPS;
17716 else if (strcmp(rate, "54") == 0)
17717 BitMap |= WIFI_BITRATE_54MBPS;
17718 rate = strtok(NULL, ",");
17719 }
17720 *basicRateBitMap = BitMap;
17721 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
17722 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017723}
17724
17725// This API is used to configured all radio operation parameter in a single set. it includes channel number, channelWidth, mode and auto chammel configuration.
17726INT wifi_setRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17727{
developera3511852023-06-14 14:12:59 +080017728 char buf[128] = {0};
17729 int bandwidth = 20;
17730 int set_mode = 0;
developer56fbedb2023-05-30 16:47:05 +080017731 BOOL drv_dat_change = 0, hapd_conf_change = 0;
developera3511852023-06-14 14:12:59 +080017732 wifi_radio_operationParam_t current_param;
developer72fb0bb2023-01-11 09:46:29 +080017733
developera3511852023-06-14 14:12:59 +080017734 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017735
developera3511852023-06-14 14:12:59 +080017736 multiple_set = TRUE;
17737 if (wifi_getRadioOperatingParameters(index, &current_param) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017738 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingParameters return error.\n");
developera3511852023-06-14 14:12:59 +080017739 return RETURN_ERR;
17740 }
17741 if (current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17742 if (wifi_setRadioAutoChannelEnable(index, operationParam->autoChannelEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017743 wifi_debug(DEBUG_ERROR, "wifi_setRadioAutoChannelEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017744 return RETURN_ERR;
17745 }
17746 drv_dat_change = TRUE;
17747 }
17748 if (current_param.channelWidth != operationParam->channelWidth ||
17749 current_param.channel != operationParam->channel ||
17750 current_param.autoChannelEnabled != operationParam->autoChannelEnabled) {
17751 if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_20MHZ)
17752 bandwidth = 20;
17753 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_40MHZ)
17754 bandwidth = 40;
17755 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80MHZ)
17756 bandwidth = 80;
17757 else if (operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_160MHZ || operationParam->channelWidth == WIFI_CHANNELBANDWIDTH_80_80MHZ)
17758 bandwidth = 160;
developer72fb0bb2023-01-11 09:46:29 +080017759
developera3511852023-06-14 14:12:59 +080017760 if (operationParam->autoChannelEnabled) {
17761 if (wifi_pushRadioChannel2(index, 0, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017762 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017763 return RETURN_ERR;
17764 }
17765 } else {
17766 if (wifi_pushRadioChannel2(index, operationParam->channel, bandwidth, operationParam->csa_beacon_count) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017767 wifi_debug(DEBUG_ERROR, "wifi_pushRadioChannel2 return error.\n");
developera3511852023-06-14 14:12:59 +080017768 return RETURN_ERR;
17769 }
17770 }
developer56fbedb2023-05-30 16:47:05 +080017771 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017772 }
17773 if (current_param.variant != operationParam->variant) {
17774 // Two different definition bit map, so need to check every bit.
17775 if (operationParam->variant & WIFI_80211_VARIANT_A)
17776 set_mode |= WIFI_MODE_A;
17777 if (operationParam->variant & WIFI_80211_VARIANT_B)
17778 set_mode |= WIFI_MODE_B;
17779 if (operationParam->variant & WIFI_80211_VARIANT_G)
17780 set_mode |= WIFI_MODE_G;
17781 if (operationParam->variant & WIFI_80211_VARIANT_N)
17782 set_mode |= WIFI_MODE_N;
17783 if (operationParam->variant & WIFI_80211_VARIANT_AC)
17784 set_mode |= WIFI_MODE_AC;
17785 if (operationParam->variant & WIFI_80211_VARIANT_AX)
17786 set_mode |= WIFI_MODE_AX;
17787 // Second parameter is to set channel band width, it is done by wifi_pushRadioChannel2 if changed.
17788 memset(buf, 0, sizeof(buf));
17789 drv_dat_change = TRUE;
17790 if (wifi_setRadioMode_by_dat(index, set_mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017791 wifi_debug(DEBUG_ERROR, "wifi_setRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080017792 return RETURN_ERR;
17793 }
17794 }
17795 if (current_param.dtimPeriod != operationParam->dtimPeriod) {
developer56fbedb2023-05-30 16:47:05 +080017796 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017797 if (wifi_setApDTIMInterval(index, operationParam->dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017798 wifi_debug(DEBUG_ERROR, "wifi_setApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017799 return RETURN_ERR;
17800 }
17801 }
17802 if (current_param.beaconInterval != operationParam->beaconInterval) {
developer56fbedb2023-05-30 16:47:05 +080017803 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017804 if (wifi_setRadioBeaconPeriod(index, operationParam->beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017805 wifi_debug(DEBUG_ERROR, "wifi_setRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080017806 return RETURN_ERR;
17807 }
17808 }
17809 if (current_param.operationalDataTransmitRates != operationParam->operationalDataTransmitRates) {
developer56fbedb2023-05-30 16:47:05 +080017810 hapd_conf_change = TRUE;
developer32f2a182023-06-27 19:50:41 +080017811 BitMapToTransmitRates(operationParam->operationalDataTransmitRates, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080017812 if (wifi_setRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017813 wifi_debug(DEBUG_ERROR, "wifi_setRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080017814 return RETURN_ERR;
17815 }
17816 }
17817 if (current_param.fragmentationThreshold != operationParam->fragmentationThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017818 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017819 if (wifi_setRadioFragmentationThreshold(index, operationParam->fragmentationThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017820 wifi_debug(DEBUG_ERROR, "wifi_setRadioFragmentationThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017821 return RETURN_ERR;
17822 }
17823 }
17824 if (current_param.guardInterval != operationParam->guardInterval) {
developer56fbedb2023-05-30 16:47:05 +080017825 hapd_conf_change = TRUE;
17826 drv_dat_change = TRUE;
17827 if (wifi_setGuardInterval(index, operationParam->guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017828 wifi_debug(DEBUG_ERROR, "wifi_setGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017829 return RETURN_ERR;
17830 }
17831 }
17832 if (current_param.transmitPower != operationParam->transmitPower) {
developer56fbedb2023-05-30 16:47:05 +080017833 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017834 if (wifi_setRadioTransmitPower(index, operationParam->transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017835 wifi_debug(DEBUG_ERROR, "wifi_setRadioTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080017836 return RETURN_ERR;
17837 }
17838 }
17839 if (current_param.rtsThreshold != operationParam->rtsThreshold) {
developer56fbedb2023-05-30 16:47:05 +080017840 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017841 if (wifi_setApRtsThreshold(index, operationParam->rtsThreshold) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017842 wifi_debug(DEBUG_ERROR, "wifi_setApRtsThreshold return error.\n");
developera3511852023-06-14 14:12:59 +080017843 return RETURN_ERR;
17844 }
17845 }
17846 if (current_param.obssCoex != operationParam->obssCoex) {
developer56fbedb2023-05-30 16:47:05 +080017847 hapd_conf_change = TRUE;
developera3511852023-06-14 14:12:59 +080017848 if (wifi_setRadioObssCoexistenceEnable(index, operationParam->obssCoex) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017849 wifi_debug(DEBUG_ERROR, "wifi_setRadioObssCoexistenceEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017850 return RETURN_ERR;
17851 }
17852 }
17853 if (current_param.stbcEnable != operationParam->stbcEnable) {
developer56fbedb2023-05-30 16:47:05 +080017854 hapd_conf_change = TRUE;
17855 drv_dat_change = TRUE;
developera3511852023-06-14 14:12:59 +080017856 if (wifi_setRadioSTBCEnable(index, operationParam->stbcEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017857 wifi_debug(DEBUG_ERROR, "wifi_setRadioSTBCEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017858 return RETURN_ERR;
17859 }
17860 }
17861 if (current_param.greenFieldEnable != operationParam->greenFieldEnable) {
17862 if (wifi_setRadio11nGreenfieldEnable(index, operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017863 wifi_debug(DEBUG_ERROR, "wifi_setRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017864 return RETURN_ERR;
17865 }
17866 }
developer72fb0bb2023-01-11 09:46:29 +080017867
developera3511852023-06-14 14:12:59 +080017868 /* only down/up interface when dat file has been changed,
17869 * if enable is true, then restart the radio.
17870 */
17871 if (drv_dat_change == TRUE) {
17872 wifi_setRadioEnable(index, FALSE);
developer56fbedb2023-05-30 16:47:05 +080017873 if (operationParam->enable == TRUE)
developera3511852023-06-14 14:12:59 +080017874 wifi_setRadioEnable(index, TRUE);
17875 } else if (hapd_conf_change == TRUE) {
17876 hostapd_raw_remove_bss(index);
17877 if (operationParam->enable == TRUE)
17878 hostapd_raw_add_bss(index);
17879 }
developer56fbedb2023-05-30 16:47:05 +080017880
developera3511852023-06-14 14:12:59 +080017881 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080017882
developera3511852023-06-14 14:12:59 +080017883 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080017884}
17885
17886INT wifi_getRadioOperatingParameters(wifi_radio_index_t index, wifi_radio_operationParam_t *operationParam)
17887{
developera3511852023-06-14 14:12:59 +080017888 char band[64] = {0};
17889 char buf[256] = {0};
17890 char config_file[64] = {0};
17891 char cmd[128] = {0};
17892 UINT mode = 0;
17893 BOOL enabled = FALSE;
developer863a4a62023-06-06 16:55:59 +080017894 int dtimPeriod;
developer2f79c922023-06-02 17:33:42 +080017895 UINT beaconInterval;
17896 UINT basicDataTransmitRates;
17897 UINT operationalDataTransmitRates;
17898 wifi_guard_interval_t guardInterval;
17899 UINT transmitPower;
developere40952c2023-06-15 18:46:43 +080017900 int res;
developerc14d83a2023-06-29 20:09:42 +080017901 unsigned long tmp;
developer72fb0bb2023-01-11 09:46:29 +080017902
developera3511852023-06-14 14:12:59 +080017903 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
17904 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080017905
developera3511852023-06-14 14:12:59 +080017906 memset(operationParam, 0, sizeof(wifi_radio_operationParam_t));
developere40952c2023-06-15 18:46:43 +080017907 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, index);
17908 if (os_snprintf_error(sizeof(config_file), res)) {
17909 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
17910 return RETURN_ERR;
17911 }
developera3511852023-06-14 14:12:59 +080017912 if (wifi_getRadioEnable(index, &enabled) != RETURN_OK)
17913 {
developer75bd10c2023-06-27 11:34:08 +080017914 wifi_debug(DEBUG_ERROR, "wifi_getRadioEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017915 return RETURN_ERR;
17916 }
17917 operationParam->enable = enabled;
developer72fb0bb2023-01-11 09:46:29 +080017918
developera3511852023-06-14 14:12:59 +080017919 memset(band, 0, sizeof(band));
17920 if (wifi_getRadioOperatingFrequencyBand(index, band) != RETURN_OK)
17921 {
developer75bd10c2023-06-27 11:34:08 +080017922 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingFrequencyBand return error.\n");
developera3511852023-06-14 14:12:59 +080017923 return RETURN_ERR;
17924 }
developer72fb0bb2023-01-11 09:46:29 +080017925
developera3511852023-06-14 14:12:59 +080017926 if (!strcmp(band, "2.4GHz"))
17927 operationParam->band = WIFI_FREQUENCY_2_4_BAND;
17928 else if (!strcmp(band, "5GHz"))
17929 operationParam->band = WIFI_FREQUENCY_5_BAND;
17930 else if (!strcmp(band, "6GHz"))
17931 operationParam->band = WIFI_FREQUENCY_6_BAND;
17932 else
17933 {
developer75bd10c2023-06-27 11:34:08 +080017934 wifi_debug(DEBUG_ERROR, "cannot decode band for radio index %d ('%s')\n", index, band);
developera3511852023-06-14 14:12:59 +080017935 }
developer72fb0bb2023-01-11 09:46:29 +080017936
developera3511852023-06-14 14:12:59 +080017937 wifi_hostapdRead(config_file, "channel", buf, sizeof(buf));
17938 if (strcmp(buf, "0") == 0 || strcmp(buf, "acs_survey") == 0) {
17939 operationParam->channel = 0;
17940 operationParam->autoChannelEnabled = TRUE;
17941 } else {
developerc14d83a2023-06-29 20:09:42 +080017942 if (hal_strtoul(buf, 10, &tmp) < 0) {
17943 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080017944 }
17945 operationParam->channel = tmp;
17946
developera3511852023-06-14 14:12:59 +080017947 operationParam->autoChannelEnabled = FALSE;
17948 }
developer72fb0bb2023-01-11 09:46:29 +080017949
developera3511852023-06-14 14:12:59 +080017950 memset(buf, 0, sizeof(buf));
17951 if (wifi_getRadioOperatingChannelBandwidth(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017952 wifi_debug(DEBUG_ERROR, "wifi_getRadioOperatingChannelBandwidth return error.\n");
developera3511852023-06-14 14:12:59 +080017953 return RETURN_ERR;
17954 }
17955 if (!strcmp(buf, "20MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
17956 else if (!strcmp(buf, "40MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
17957 else if (!strcmp(buf, "80MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
17958 else if (!strcmp(buf, "160MHz")) operationParam->channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
17959 else
17960 {
developer75bd10c2023-06-27 11:34:08 +080017961 wifi_debug(DEBUG_ERROR, "Unknown channel bandwidth: %s\n", buf);
developera3511852023-06-14 14:12:59 +080017962 return false;
17963 }
developer72fb0bb2023-01-11 09:46:29 +080017964
developera3511852023-06-14 14:12:59 +080017965 if (wifi_getRadioMode(index, buf, &mode) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017966 wifi_debug(DEBUG_ERROR, "wifi_getRadioMode return error.\n");
developera3511852023-06-14 14:12:59 +080017967 return RETURN_ERR;
17968 }
17969 // Two different definition bit map, so need to check every bit.
17970 if (mode & WIFI_MODE_A)
17971 operationParam->variant |= WIFI_80211_VARIANT_A;
17972 if (mode & WIFI_MODE_B)
17973 operationParam->variant |= WIFI_80211_VARIANT_B;
17974 if (mode & WIFI_MODE_G)
17975 operationParam->variant |= WIFI_80211_VARIANT_G;
17976 if (mode & WIFI_MODE_N)
17977 operationParam->variant |= WIFI_80211_VARIANT_N;
17978 if (mode & WIFI_MODE_AC)
17979 operationParam->variant |= WIFI_80211_VARIANT_AC;
17980 if (mode & WIFI_MODE_AX)
17981 operationParam->variant |= WIFI_80211_VARIANT_AX;
17982 if (wifi_getRadioDCSEnable(index, &operationParam->DCSEnabled) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017983 wifi_debug(DEBUG_ERROR, "wifi_getRadioDCSEnable return error.\n");
developera3511852023-06-14 14:12:59 +080017984 return RETURN_ERR;
17985 }
17986 if (wifi_getApDTIMInterval(index, &dtimPeriod) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017987 wifi_debug(DEBUG_ERROR, "wifi_getApDTIMInterval return error.\n");
developera3511852023-06-14 14:12:59 +080017988 return RETURN_ERR;
17989 }
developer2f79c922023-06-02 17:33:42 +080017990 operationParam->dtimPeriod = dtimPeriod;
developera3511852023-06-14 14:12:59 +080017991 if (wifi_getRadioBeaconPeriod(index, &beaconInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017992 wifi_debug(DEBUG_ERROR, "wifi_getRadioBeaconPeriod return error.\n");
developera3511852023-06-14 14:12:59 +080017993 return RETURN_ERR;
17994 }
developer2f79c922023-06-02 17:33:42 +080017995 operationParam->beaconInterval = beaconInterval;
developer72fb0bb2023-01-11 09:46:29 +080017996
developera3511852023-06-14 14:12:59 +080017997 memset(buf, 0, sizeof(buf));
17998 if (wifi_getRadioSupportedDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080017999 wifi_debug(DEBUG_ERROR, "wifi_getRadioSupportedDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018000 return RETURN_ERR;
18001 }
18002 TransmitRatesToBitMap(buf, &basicDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018003 operationParam->basicDataTransmitRates = basicDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018004
developera3511852023-06-14 14:12:59 +080018005 memset(buf, 0, sizeof(buf));
18006 if (wifi_getRadioBasicDataTransmitRates(index, buf) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018007 wifi_debug(DEBUG_ERROR, "wifi_getRadioBasicDataTransmitRates return error.\n");
developera3511852023-06-14 14:12:59 +080018008 return RETURN_ERR;
18009 }
18010 TransmitRatesToBitMap(buf, &operationalDataTransmitRates);
developer2f79c922023-06-02 17:33:42 +080018011 operationParam->operationalDataTransmitRates = operationalDataTransmitRates;
developer72fb0bb2023-01-11 09:46:29 +080018012
developera3511852023-06-14 14:12:59 +080018013 memset(buf, 0, sizeof(buf));
18014 wifi_hostapdRead(config_file, "fragm_threshold", buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080018015 if (hal_strtoul(buf, 10, &tmp) < 0) {
18016 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18017 }
18018 operationParam->fragmentationThreshold = tmp;
developer72fb0bb2023-01-11 09:46:29 +080018019
developera3511852023-06-14 14:12:59 +080018020 if (wifi_getGuardInterval(index, &guardInterval) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018021 wifi_debug(DEBUG_ERROR, "wifi_getGuardInterval return error.\n");
developera3511852023-06-14 14:12:59 +080018022 return RETURN_ERR;
18023 }
developer2f79c922023-06-02 17:33:42 +080018024 operationParam->guardInterval = guardInterval;
18025
developera3511852023-06-14 14:12:59 +080018026 if (wifi_getRadioPercentageTransmitPower(index, (ULONG *)&transmitPower) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018027 wifi_debug(DEBUG_ERROR, "wifi_getRadioPercentageTransmitPower return error.\n");
developera3511852023-06-14 14:12:59 +080018028 return RETURN_ERR;
18029 }
developer2f79c922023-06-02 17:33:42 +080018030 operationParam->transmitPower = transmitPower;
developer72fb0bb2023-01-11 09:46:29 +080018031
developera3511852023-06-14 14:12:59 +080018032 memset(buf, 0, sizeof(buf));
18033 wifi_hostapdRead(config_file, "rts_threshold", buf, sizeof(buf));
18034 if (strcmp(buf, "-1") == 0) {
18035 operationParam->rtsThreshold = (UINT)-1; // maxuimum unsigned integer value
18036 operationParam->ctsProtection = FALSE;
18037 } else {
developerc14d83a2023-06-29 20:09:42 +080018038 if (hal_strtoul(buf, 10, &tmp) < 0) {
18039 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
18040 }
18041 operationParam->rtsThreshold = tmp;
developera3511852023-06-14 14:12:59 +080018042 operationParam->ctsProtection = TRUE;
18043 }
developer72fb0bb2023-01-11 09:46:29 +080018044
developera3511852023-06-14 14:12:59 +080018045 memset(buf, 0, sizeof(buf));
18046 wifi_hostapdRead(config_file, "ht_coex", buf, sizeof(buf));
18047 if (strcmp(buf, "0") == 0)
18048 operationParam->obssCoex = FALSE;
18049 else
18050 operationParam->obssCoex = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018051
developere40952c2023-06-15 18:46:43 +080018052 res = snprintf(cmd, sizeof(cmd), "cat %s | grep STBC", config_file);
18053 if (os_snprintf_error(sizeof(cmd), res)) {
18054 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18055 return RETURN_ERR;
18056 }
developera3511852023-06-14 14:12:59 +080018057 _syscmd(cmd, buf, sizeof(buf));
18058 if (strlen(buf) != 0)
18059 operationParam->stbcEnable = TRUE;
18060 else
18061 operationParam->stbcEnable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018062
developera3511852023-06-14 14:12:59 +080018063 if (wifi_getRadio11nGreenfieldEnable(index, &operationParam->greenFieldEnable) != RETURN_OK) {
developer75bd10c2023-06-27 11:34:08 +080018064 wifi_debug(DEBUG_ERROR, "wifi_getRadio11nGreenfieldEnable return error.\n");
developera3511852023-06-14 14:12:59 +080018065 return RETURN_ERR;
18066 }
developer72fb0bb2023-01-11 09:46:29 +080018067
developera3511852023-06-14 14:12:59 +080018068 // Below value is hardcoded
developer72fb0bb2023-01-11 09:46:29 +080018069
developera3511852023-06-14 14:12:59 +080018070 operationParam->numSecondaryChannels = 0;
18071 for (int i = 0; i < MAXNUMSECONDARYCHANNELS; i++) {
18072 operationParam->channelSecondary[i] = 0;
18073 }
18074 operationParam->csa_beacon_count = 15;
18075 operationParam->countryCode = wifi_countrycode_US; // hard to convert string to corresponding enum
developer72fb0bb2023-01-11 09:46:29 +080018076
developera3511852023-06-14 14:12:59 +080018077 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18078 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018079}
18080
18081static int array_index_to_vap_index(UINT radioIndex, int arrayIndex)
18082{
developera3511852023-06-14 14:12:59 +080018083 int max_radio_num = 0;
developer72fb0bb2023-01-11 09:46:29 +080018084
developera3511852023-06-14 14:12:59 +080018085 wifi_getMaxRadioNumber(&max_radio_num);
18086 if (radioIndex >= max_radio_num) {
developer75bd10c2023-06-27 11:34:08 +080018087 wifi_debug(DEBUG_ERROR, "Wrong radio index (%d)\n", radioIndex);
developera3511852023-06-14 14:12:59 +080018088 return RETURN_ERR;
18089 }
developer72fb0bb2023-01-11 09:46:29 +080018090
developera3511852023-06-14 14:12:59 +080018091 return (arrayIndex * max_radio_num) + radioIndex;
developer72fb0bb2023-01-11 09:46:29 +080018092}
18093
developer96b38512023-02-22 11:17:45 +080018094static int vap_index_to_array_index(int vapIndex, int *radioIndex, int *arrayIndex)
18095{
developera3511852023-06-14 14:12:59 +080018096 int max_radio_num = 0;
developer96b38512023-02-22 11:17:45 +080018097
developera3511852023-06-14 14:12:59 +080018098 if ((vapIndex < 0) || (vapIndex > MAX_NUM_VAP_PER_RADIO*MAX_NUM_RADIOS))
developer96b38512023-02-22 11:17:45 +080018099 return -1;
18100
developera3511852023-06-14 14:12:59 +080018101 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080018102 if(max_radio_num == 0){
18103 return RETURN_ERR;
18104 }
developera3511852023-06-14 14:12:59 +080018105 (*radioIndex) = vapIndex % max_radio_num;
18106 (*arrayIndex) = vapIndex / max_radio_num;
developer96b38512023-02-22 11:17:45 +080018107
developera3511852023-06-14 14:12:59 +080018108 return 0;
developer96b38512023-02-22 11:17:45 +080018109}
18110
18111
developer72fb0bb2023-01-11 09:46:29 +080018112wifi_bitrate_t beaconRate_string_to_enum(char *beaconRate) {
developera3511852023-06-14 14:12:59 +080018113 if (strncmp(beaconRate, "1Mbps", 5) == 0)
18114 return WIFI_BITRATE_1MBPS;
18115 else if (strncmp(beaconRate, "2Mbps", 5) == 0)
18116 return WIFI_BITRATE_2MBPS;
18117 else if (strncmp(beaconRate, "5.5Mbps", 7) == 0)
18118 return WIFI_BITRATE_5_5MBPS;
18119 else if (strncmp(beaconRate, "6Mbps", 5) == 0)
18120 return WIFI_BITRATE_6MBPS;
18121 else if (strncmp(beaconRate, "9Mbps", 5) == 0)
18122 return WIFI_BITRATE_9MBPS;
18123 else if (strncmp(beaconRate, "11Mbps", 6) == 0)
18124 return WIFI_BITRATE_11MBPS;
18125 else if (strncmp(beaconRate, "12Mbps", 6) == 0)
18126 return WIFI_BITRATE_12MBPS;
18127 else if (strncmp(beaconRate, "18Mbps", 6) == 0)
18128 return WIFI_BITRATE_18MBPS;
18129 else if (strncmp(beaconRate, "24Mbps", 6) == 0)
18130 return WIFI_BITRATE_24MBPS;
18131 else if (strncmp(beaconRate, "36Mbps", 6) == 0)
18132 return WIFI_BITRATE_36MBPS;
18133 else if (strncmp(beaconRate, "48Mbps", 6) == 0)
18134 return WIFI_BITRATE_48MBPS;
18135 else if (strncmp(beaconRate, "54Mbps", 6) == 0)
18136 return WIFI_BITRATE_54MBPS;
18137 return WIFI_BITRATE_DEFAULT;
developer72fb0bb2023-01-11 09:46:29 +080018138}
18139
developer32f2a182023-06-27 19:50:41 +080018140struct beacon_rate_2_string {
18141 wifi_bitrate_t beacon;
18142 char beacon_str[8];
18143};
18144
18145struct beacon_rate_2_string br2str[12] = {
18146 {WIFI_BITRATE_1MBPS, "1Mbps"},
18147 {WIFI_BITRATE_2MBPS, "2Mbps"},
18148 {WIFI_BITRATE_5_5MBPS, "5.5Mbps"},
18149 {WIFI_BITRATE_6MBPS, "6Mbps"},
18150 {WIFI_BITRATE_9MBPS, "9Mbps"},
18151 {WIFI_BITRATE_11MBPS, "11Mbps"},
18152 {WIFI_BITRATE_12MBPS, "12Mbps"},
18153 {WIFI_BITRATE_18MBPS, "18Mbps"},
18154 {WIFI_BITRATE_24MBPS, "24Mbps"},
18155 {WIFI_BITRATE_36MBPS, "36Mbps"},
18156 {WIFI_BITRATE_48MBPS, "48Mbps"},
18157 {WIFI_BITRATE_54MBPS, "54Mbps"}
18158};
18159
18160INT beaconRate_enum_to_string(wifi_bitrate_t beacon, char *beacon_str, unsigned long str_size)
developer72fb0bb2023-01-11 09:46:29 +080018161{
developer32f2a182023-06-27 19:50:41 +080018162 int i;
18163 unsigned long len;
18164
18165 for (i = 0; i < (sizeof(br2str)/sizeof(br2str[0])); i++) {
18166 if (beacon == br2str[i].beacon) {
18167 len = strlen(br2str[i].beacon_str);
18168 if (len >= str_size)
18169 return RETURN_ERR;
18170 memcpy(beacon_str, br2str[i].beacon_str, len);
18171 beacon_str[len] = '\0';
18172 break;
18173 }
18174 }
developera3511852023-06-14 14:12:59 +080018175 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018176}
18177
18178INT wifi_getRadioVapInfoMap(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18179{
developera3511852023-06-14 14:12:59 +080018180 INT mode = 0;
18181 INT ret = -1;
18182 UINT output = 0;
18183 int i = 0;
18184 int vap_index = 0;
18185 BOOL enabled = FALSE;
18186 char buf[32] = {0};
18187 wifi_vap_security_t security = {0};
developere40952c2023-06-15 18:46:43 +080018188 int res;
developer72fb0bb2023-01-11 09:46:29 +080018189
developera3511852023-06-14 14:12:59 +080018190 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18191 printf("Entering %s index = %d\n", __func__, (int)index);
developer72fb0bb2023-01-11 09:46:29 +080018192
developera3511852023-06-14 14:12:59 +080018193 ret = wifi_BandProfileRead(0, index, "BssidNum", buf, sizeof(buf), "0");
18194 if (ret != 0) {
developer75bd10c2023-06-27 11:34:08 +080018195 wifi_debug(DEBUG_ERROR, "wifi_BandProfileRead BssidNum failed\n");
developera3511852023-06-14 14:12:59 +080018196 return RETURN_ERR;
18197 }
developerfde01262023-05-22 15:15:24 +080018198
developera3511852023-06-14 14:12:59 +080018199 map->num_vaps = atoi(buf);
18200 if (map->num_vaps <= 0) {
developer75bd10c2023-06-27 11:34:08 +080018201 wifi_debug(DEBUG_ERROR, "invalid BssidNum %s\n", buf);
developera3511852023-06-14 14:12:59 +080018202 return RETURN_ERR;
18203 }
developerfde01262023-05-22 15:15:24 +080018204
developera3511852023-06-14 14:12:59 +080018205 for (i = 0; i < map->num_vaps; i++)
18206 {
18207 map->vap_array[i].radio_index = index;
developer72fb0bb2023-01-11 09:46:29 +080018208
developera3511852023-06-14 14:12:59 +080018209 vap_index = array_index_to_vap_index(index, i);
18210 if (vap_index < 0)
18211 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080018212
developer9ce44382023-06-28 11:09:37 +080018213 strncpy(map->vap_array[i].bridge_name, BRIDGE_NAME,sizeof(map->vap_array[i].bridge_name) - 1);
18214 map->vap_array[i].bridge_name[sizeof(map->vap_array[i].bridge_name) - 1] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080018215
developera3511852023-06-14 14:12:59 +080018216 map->vap_array[i].vap_index = vap_index;
developer72fb0bb2023-01-11 09:46:29 +080018217
developera3511852023-06-14 14:12:59 +080018218 memset(buf, 0, sizeof(buf));
18219 ret = wifi_getApName(vap_index, buf);
18220 if (ret != RETURN_OK) {
18221 printf("%s: wifi_getApName return error\n", __func__);
18222 return RETURN_ERR;
18223 }
developere40952c2023-06-15 18:46:43 +080018224 res = snprintf(map->vap_array[i].vap_name, sizeof(map->vap_array[i].vap_name), "%s", buf);
18225 if (os_snprintf_error(sizeof(map->vap_array[i].vap_name), res)) {
18226 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18227 return RETURN_ERR;
18228 }
developer72fb0bb2023-01-11 09:46:29 +080018229
developera3511852023-06-14 14:12:59 +080018230 memset(buf, 0, sizeof(buf));
18231 ret = wifi_getSSIDName(vap_index, buf);
18232 if (ret != RETURN_OK) {
18233 printf("%s: wifi_getSSIDName return error\n", __func__);
18234 return RETURN_ERR;
18235 }
developere40952c2023-06-15 18:46:43 +080018236 res = snprintf(map->vap_array[i].u.bss_info.ssid, sizeof(map->vap_array[i].u.bss_info.ssid), "%s", buf);
18237 if (os_snprintf_error(sizeof(map->vap_array[i].u.bss_info.ssid), res)) {
18238 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18239 return RETURN_ERR;
18240 }
developer72fb0bb2023-01-11 09:46:29 +080018241
developera3511852023-06-14 14:12:59 +080018242 map->vap_array[i].u.bss_info.enabled = true;
developer72fb0bb2023-01-11 09:46:29 +080018243
developera3511852023-06-14 14:12:59 +080018244 ret = wifi_getApSsidAdvertisementEnable(vap_index, &enabled);
18245 if (ret != RETURN_OK) {
18246 printf("%s: wifi_getApSsidAdvertisementEnable return error\n", __func__);
18247 return RETURN_ERR;
18248 }
18249 map->vap_array[i].u.bss_info.showSsid = enabled;
developer69b61b02023-03-07 17:17:44 +080018250
developera3511852023-06-14 14:12:59 +080018251 ret = wifi_getApIsolationEnable(vap_index, &enabled);
18252 if (ret != RETURN_OK) {
18253 printf("%s: wifi_getApIsolationEnable return error\n", __func__);
18254 return RETURN_ERR;
18255 }
18256 map->vap_array[i].u.bss_info.isolation = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018257
developera3511852023-06-14 14:12:59 +080018258 ret = wifi_getApMaxAssociatedDevices(vap_index, &output);
18259 if (ret != RETURN_OK) {
18260 printf("%s: wifi_getApMaxAssociatedDevices return error\n", __func__);
18261 return RETURN_ERR;
18262 }
18263 map->vap_array[i].u.bss_info.bssMaxSta = output;
developer72fb0bb2023-01-11 09:46:29 +080018264
developera3511852023-06-14 14:12:59 +080018265 ret = wifi_getBSSTransitionActivation(vap_index, &enabled);
18266 if (ret != RETURN_OK) {
18267 printf("%s: wifi_getBSSTransitionActivation return error\n", __func__);
18268 return RETURN_ERR;
18269 }
18270 map->vap_array[i].u.bss_info.bssTransitionActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018271
developera3511852023-06-14 14:12:59 +080018272 ret = wifi_getNeighborReportActivation(vap_index, &enabled);
18273 if (ret != RETURN_OK) {
18274 printf("%s: wifi_getNeighborReportActivation return error\n", __func__);
18275 return RETURN_ERR;
18276 }
18277 map->vap_array[i].u.bss_info.nbrReportActivated = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018278
developera3511852023-06-14 14:12:59 +080018279 ret = wifi_getApSecurity(vap_index, &security);
18280 if (ret != RETURN_OK) {
18281 printf("%s: wifi_getApSecurity return error\n", __func__);
18282 return RETURN_ERR;
18283 }
18284 map->vap_array[i].u.bss_info.security = security;
developer72fb0bb2023-01-11 09:46:29 +080018285
developera3511852023-06-14 14:12:59 +080018286 ret = wifi_getApMacAddressControlMode(vap_index, &mode);
18287 if (ret != RETURN_OK) {
18288 printf("%s: wifi_getApMacAddressControlMode return error\n", __func__);
18289 return RETURN_ERR;
18290 }
18291 if (mode == 0)
18292 map->vap_array[i].u.bss_info.mac_filter_enable = FALSE;
18293 else
18294 map->vap_array[i].u.bss_info.mac_filter_enable = TRUE;
18295 if (mode == 1)
18296 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_white_list;
18297 else if (mode == 2)
18298 map->vap_array[i].u.bss_info.mac_filter_mode = wifi_mac_filter_mode_black_list;
developer72fb0bb2023-01-11 09:46:29 +080018299
developera3511852023-06-14 14:12:59 +080018300 ret = wifi_getApWmmEnable(vap_index, &enabled);
18301 if (ret != RETURN_OK) {
18302 printf("%s: wifi_getApWmmEnable return error\n", __func__);
18303 return RETURN_ERR;
18304 }
18305 map->vap_array[i].u.bss_info.wmm_enabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018306
developera3511852023-06-14 14:12:59 +080018307 ret = wifi_getApUAPSDCapability(vap_index, &enabled);
18308 if (ret != RETURN_OK) {
18309 printf("%s: wifi_getApUAPSDCapability return error\n", __func__);
18310 return RETURN_ERR;
18311 }
18312 map->vap_array[i].u.bss_info.UAPSDEnabled = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018313
developera3511852023-06-14 14:12:59 +080018314 memset(buf, 0, sizeof(buf));
18315 ret = wifi_getApBeaconRate(map->vap_array[i].radio_index, buf);
18316 if (ret != RETURN_OK) {
18317 printf("%s: wifi_getApBeaconRate return error\n", __func__);
18318 return RETURN_ERR;
18319 }
18320 map->vap_array[i].u.bss_info.beaconRate = beaconRate_string_to_enum(buf);
developer72fb0bb2023-01-11 09:46:29 +080018321
developera3511852023-06-14 14:12:59 +080018322 memset(buf, 0, sizeof(buf));
18323 ret = wifi_getBaseBSSID(vap_index, buf);
18324 if (ret != RETURN_OK) {
18325 printf("%s: wifi_getBaseBSSID return error\n", __func__);
18326 return RETURN_ERR;
18327 }
developer5b2f10c2023-05-25 17:02:21 +080018328 if (hwaddr_aton2(buf, map->vap_array[i].u.bss_info.bssid) < 0) {
18329 printf("%s: hwaddr_aton2 fail\n", __func__);
developera3511852023-06-14 14:12:59 +080018330 return RETURN_ERR;
developer5b2f10c2023-05-25 17:02:21 +080018331 }
developer72fb0bb2023-01-11 09:46:29 +080018332
developera3511852023-06-14 14:12:59 +080018333 ret = wifi_getRadioIGMPSnoopingEnable(map->vap_array[i].radio_index, &enabled);
18334 if (ret != RETURN_OK) {
developerc14d83a2023-06-29 20:09:42 +080018335 wifi_debug(DEBUG_ERROR, "%s: wifi_getRadioIGMPSnoopingEnable\n", __func__);
developera3511852023-06-14 14:12:59 +080018336 return RETURN_ERR;
18337 }
18338 map->vap_array[i].u.bss_info.mcast2ucast = enabled;
developer72fb0bb2023-01-11 09:46:29 +080018339
developera3511852023-06-14 14:12:59 +080018340 // TODO: wps, noack
18341 }
18342 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18343 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018344}
18345
developer47cc27a2023-05-17 23:09:58 +080018346void checkVapStatus(int apIndex, BOOL *enable)
developer72fb0bb2023-01-11 09:46:29 +080018347{
developera3511852023-06-14 14:12:59 +080018348 char if_name[16] = {0};
18349 char cmd[128] = {0};
18350 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080018351 int res;
developer72fb0bb2023-01-11 09:46:29 +080018352
developera3511852023-06-14 14:12:59 +080018353 *enable = FALSE;
18354 if (wifi_GetInterfaceName(apIndex, if_name) != RETURN_OK)
18355 return;
developer72fb0bb2023-01-11 09:46:29 +080018356
developere40952c2023-06-15 18:46:43 +080018357 res = snprintf(cmd, sizeof(cmd), "cat %s | grep ^%s=1", VAP_STATUS_FILE, if_name);
18358 if (os_snprintf_error(sizeof(cmd), res)) {
18359 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18360 return;
18361 }
developera3511852023-06-14 14:12:59 +080018362 _syscmd(cmd, buf, sizeof(buf));
18363 if (strlen(buf) > 0)
18364 *enable = TRUE;
18365 return;
developer72fb0bb2023-01-11 09:46:29 +080018366}
18367
developer56fbedb2023-05-30 16:47:05 +080018368int hostapd_manage_bss(INT apIndex, BOOL enable)
18369{
18370 char interface_name[16] = {0};
developerb149d9d2023-06-06 16:14:22 +080018371 char config_file[MAX_SUB_CMD_SIZE] = {0};
developer56fbedb2023-05-30 16:47:05 +080018372 char cmd[MAX_CMD_SIZE] = {0};
18373 char buf[MAX_BUF_SIZE] = {0};
18374 BOOL status = FALSE;
18375 int max_radio_num = 0;
18376 int phyId = 0;
developere40952c2023-06-15 18:46:43 +080018377 int res;
developer56fbedb2023-05-30 16:47:05 +080018378
18379 wifi_getApEnable(apIndex, &status);
18380
18381 wifi_getMaxRadioNumber(&max_radio_num);
18382 if (enable == status)
18383 return RETURN_OK;
18384
18385 if (wifi_GetInterfaceName(apIndex, interface_name) != RETURN_OK)
18386 return RETURN_ERR;
18387
18388 if (enable == TRUE) {
18389 int radioIndex = apIndex % max_radio_num;
18390 phyId = radio_index_to_phy(radioIndex);
developerc14d83a2023-06-29 20:09:42 +080018391 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080018392 res = snprintf(config_file, MAX_BUF_SIZE, "%s%d.conf", CONFIG_PREFIX, apIndex);
18393 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18394 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18395 return RETURN_ERR;
18396 }
18397 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw ADD bss_config=phy%d:%s", phyId, config_file);
18398 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18399 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18400 return RETURN_ERR;
18401 }
developer56fbedb2023-05-30 16:47:05 +080018402 _syscmd(cmd, buf, sizeof(buf));
18403 } else {
developerc14d83a2023-06-29 20:09:42 +080018404 wifi_debug(DEBUG_ERROR, "%s %d\n", __func__, __LINE__);
developere40952c2023-06-15 18:46:43 +080018405 res = snprintf(cmd, MAX_CMD_SIZE, "hostapd_cli -i global raw REMOVE %s", interface_name);
18406 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18407 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18408 return RETURN_ERR;
18409 }
developer56fbedb2023-05-30 16:47:05 +080018410 _syscmd(cmd, buf, sizeof(buf));
18411 }
developere40952c2023-06-15 18:46:43 +080018412 res = snprintf(cmd, MAX_CMD_SIZE, "sed -i -n -e '/^%s=/!p' -e '$a%s=%d' %s",
developer56fbedb2023-05-30 16:47:05 +080018413 interface_name, interface_name, enable, VAP_STATUS_FILE);
developere40952c2023-06-15 18:46:43 +080018414 if (os_snprintf_error(MAX_CMD_SIZE, res)) {
18415 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18416 return RETURN_ERR;
18417 }
developer56fbedb2023-05-30 16:47:05 +080018418 _syscmd(cmd, buf, sizeof(buf));
18419 //Wait for wifi up/down to apply
18420 return RETURN_OK;
18421}
18422
18423int hostapd_raw_add_bss(int apIndex)
18424{
18425 return hostapd_manage_bss(apIndex, TRUE);
18426}
18427
18428int hostapd_raw_remove_bss(int apIndex)
18429{
18430 return hostapd_manage_bss(apIndex, FALSE);
18431}
18432
18433int hostapd_raw_restart_bss(int apIndex)
developer333c1eb2023-05-31 14:59:39 +080018434{
developerdaf24792023-06-06 11:40:04 +080018435 int ret = 0;
18436
18437 ret = hostapd_raw_remove_bss(apIndex);
18438 if(ret != RETURN_OK)
18439 return RETURN_ERR;
18440
18441 ret = hostapd_raw_add_bss(apIndex);
18442 if(ret != RETURN_OK)
18443 return RETURN_ERR;
18444
18445 return RETURN_OK;
developer56fbedb2023-05-30 16:47:05 +080018446}
18447
developer72fb0bb2023-01-11 09:46:29 +080018448INT wifi_createVAP(wifi_radio_index_t index, wifi_vap_info_map_t *map)
18449{
developera3511852023-06-14 14:12:59 +080018450 unsigned int i;
18451 wifi_vap_info_t *vap_info = NULL;
18452 int acl_mode;
18453 int ret = 0;
18454 char buf[256] = {0};
18455 char cmd[128] = {0};
18456 char config_file[64] = {0};
18457 char psk_file[64] = {0};
18458 BOOL enable = FALSE;
18459 int band_idx;
developere40952c2023-06-15 18:46:43 +080018460 int res;
developere740c2a2023-05-23 18:34:32 +080018461
developer72fb0bb2023-01-11 09:46:29 +080018462
developera3511852023-06-14 14:12:59 +080018463 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
18464 printf("Entering %s index = %d\n", __func__, (int)index);
18465 for (i = 0; i < map->num_vaps; i++)
18466 {
18467 multiple_set = TRUE;
18468 vap_info = &map->vap_array[i];
developer72fb0bb2023-01-11 09:46:29 +080018469
developera3511852023-06-14 14:12:59 +080018470 // Check vap status file to enable multiple ap if the system boot.
18471 checkVapStatus(vap_info->vap_index, &enable);
18472 if (vap_info->u.bss_info.enabled == FALSE && enable == FALSE)
18473 continue;
developer72fb0bb2023-01-11 09:46:29 +080018474
developer75bd10c2023-06-27 11:34:08 +080018475 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 +080018476
developera3511852023-06-14 14:12:59 +080018477 band_idx = radio_index_to_band(index);
developere40952c2023-06-15 18:46:43 +080018478 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18479 if (os_snprintf_error(sizeof(config_file), res)) {
18480 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18481 return RETURN_ERR;
18482 }
developer9ce44382023-06-28 11:09:37 +080018483 if(band_idx >= 0 && band_idx < sizeof(wifi_band_str)/sizeof(wifi_band_str[0])){
18484 res = snprintf(cmd, sizeof(cmd), "cp /etc/hostapd-%s.conf %s", wifi_band_str[band_idx], config_file);
18485 } else{
18486 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18487 return RETURN_ERR;
18488 }
developere40952c2023-06-15 18:46:43 +080018489 if (os_snprintf_error(sizeof(cmd), res)) {
18490 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18491 return RETURN_ERR;
18492 }
developera3511852023-06-14 14:12:59 +080018493 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080018494
developera3511852023-06-14 14:12:59 +080018495 struct params params[3];
18496 params[0].name = "interface";
18497 params[0].value = vap_info->vap_name;
developere40952c2023-06-15 18:46:43 +080018498 res = snprintf(psk_file, sizeof(psk_file), "\\/nvram\\/hostapd%d.psk", vap_info->vap_index);
18499 if (os_snprintf_error(sizeof(psk_file), res)) {
18500 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18501 return RETURN_ERR;
18502 }
developera3511852023-06-14 14:12:59 +080018503 params[1].name = "wpa_psk_file";
18504 params[1].value = psk_file;
18505 params[2].name = "ssid";
18506 params[2].value = vap_info->u.bss_info.ssid;
developer72fb0bb2023-01-11 09:46:29 +080018507
developer37646972023-06-29 10:58:43 +080018508 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, vap_info->vap_index);
18509 if (os_snprintf_error(sizeof(config_file), res)) {
18510 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18511 return RETURN_ERR;
18512 }
developera3511852023-06-14 14:12:59 +080018513 wifi_hostapdWrite(config_file, params, 3);
developer72fb0bb2023-01-11 09:46:29 +080018514
developere40952c2023-06-15 18:46:43 +080018515 res = snprintf(cmd, sizeof(cmd), "touch %s", psk_file);
18516 if (os_snprintf_error(sizeof(cmd), res)) {
18517 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18518 return RETURN_ERR;
18519 }
developera3511852023-06-14 14:12:59 +080018520 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080018521
developera3511852023-06-14 14:12:59 +080018522 ret = wifi_setSSIDName(vap_info->vap_index, vap_info->u.bss_info.ssid);
18523 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018524 wifi_debug(DEBUG_ERROR,"wifi_setSSIDName return error\n");
developera3511852023-06-14 14:12:59 +080018525 return RETURN_ERR;
18526 }
developer72fb0bb2023-01-11 09:46:29 +080018527
developera3511852023-06-14 14:12:59 +080018528 ret = wifi_setApSsidAdvertisementEnable(vap_info->vap_index, vap_info->u.bss_info.showSsid);
18529 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018530 wifi_debug(DEBUG_ERROR, "wifi_setApSsidAdvertisementEnable return error\n");
developera3511852023-06-14 14:12:59 +080018531 return RETURN_ERR;
18532 }
developer72fb0bb2023-01-11 09:46:29 +080018533
developera3511852023-06-14 14:12:59 +080018534 ret = wifi_setApIsolationEnable(vap_info->vap_index, vap_info->u.bss_info.isolation);
18535 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018536 wifi_debug(DEBUG_ERROR, "wifi_setApIsolationEnable return error\n");
developera3511852023-06-14 14:12:59 +080018537 return RETURN_ERR;
18538 }
developer72fb0bb2023-01-11 09:46:29 +080018539
developera3511852023-06-14 14:12:59 +080018540 ret = wifi_setApMaxAssociatedDevices(vap_info->vap_index, vap_info->u.bss_info.bssMaxSta);
18541 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018542 wifi_debug(DEBUG_ERROR, "wifi_setApMaxAssociatedDevices return error\n");
developera3511852023-06-14 14:12:59 +080018543 return RETURN_ERR;
18544 }
developer72fb0bb2023-01-11 09:46:29 +080018545
developera3511852023-06-14 14:12:59 +080018546 ret = wifi_setBSSTransitionActivation(vap_info->vap_index, vap_info->u.bss_info.bssTransitionActivated);
18547 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018548 wifi_debug(DEBUG_ERROR, "wifi_setBSSTransitionActivation return error\n");
developera3511852023-06-14 14:12:59 +080018549 return RETURN_ERR;
18550 }
developer72fb0bb2023-01-11 09:46:29 +080018551
developera3511852023-06-14 14:12:59 +080018552 ret = wifi_setNeighborReportActivation(vap_info->vap_index, vap_info->u.bss_info.nbrReportActivated);
18553 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018554 wifi_debug(DEBUG_ERROR, "wifi_setNeighborReportActivation return error\n");
developera3511852023-06-14 14:12:59 +080018555 return RETURN_ERR;
18556 }
developer72fb0bb2023-01-11 09:46:29 +080018557
developera3511852023-06-14 14:12:59 +080018558 if (vap_info->u.bss_info.mac_filter_enable == false){
18559 acl_mode = 0;
18560 }else {
18561 if (vap_info->u.bss_info.mac_filter_mode == wifi_mac_filter_mode_black_list){
18562 acl_mode = 2;
developere40952c2023-06-15 18:46:43 +080018563 res = snprintf(cmd, sizeof(cmd), "touch %s%d", DENY_PREFIX, vap_info->vap_index);
18564 if (os_snprintf_error(sizeof(cmd), res)) {
18565 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18566 return RETURN_ERR;
18567 }
developera3511852023-06-14 14:12:59 +080018568 _syscmd(cmd, buf, sizeof(buf));
18569 }else{
18570 acl_mode = 1;
18571 }
18572 }
developer72fb0bb2023-01-11 09:46:29 +080018573
developera3511852023-06-14 14:12:59 +080018574 ret = wifi_setApWmmEnable(vap_info->vap_index, vap_info->u.bss_info.wmm_enabled);
18575 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018576 wifi_debug(DEBUG_ERROR, "wifi_setApWmmEnable return error\n");
developera3511852023-06-14 14:12:59 +080018577 return RETURN_ERR;
18578 }
developer72fb0bb2023-01-11 09:46:29 +080018579
developera3511852023-06-14 14:12:59 +080018580 ret = wifi_setApWmmUapsdEnable(vap_info->vap_index, vap_info->u.bss_info.UAPSDEnabled);
18581 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018582 wifi_debug(DEBUG_ERROR, "wifi_setApWmmUapsdEnable return error\n");
developera3511852023-06-14 14:12:59 +080018583 return RETURN_ERR;
18584 }
developer72fb0bb2023-01-11 09:46:29 +080018585
developera3511852023-06-14 14:12:59 +080018586 memset(buf, 0, sizeof(buf));
developer32f2a182023-06-27 19:50:41 +080018587 beaconRate_enum_to_string(vap_info->u.bss_info.beaconRate, buf, sizeof(buf));
developera3511852023-06-14 14:12:59 +080018588 ret = wifi_setApBeaconRate(vap_info->radio_index, buf);
18589 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018590 wifi_debug(DEBUG_ERROR, "wifi_setApBeaconRate return error\n");
developera3511852023-06-14 14:12:59 +080018591 return RETURN_ERR;
18592 }
developer72fb0bb2023-01-11 09:46:29 +080018593
developera3511852023-06-14 14:12:59 +080018594 ret = wifi_setRadioIGMPSnoopingEnable(vap_info->radio_index, vap_info->u.bss_info.mcast2ucast);
18595 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018596 wifi_debug(DEBUG_ERROR, "wifi_setRadioIGMPSnoopingEnable\n");
developera3511852023-06-14 14:12:59 +080018597 return RETURN_ERR;
18598 }
developer72fb0bb2023-01-11 09:46:29 +080018599
developera3511852023-06-14 14:12:59 +080018600 ret = wifi_setApSecurity(vap_info->vap_index, &vap_info->u.bss_info.security);
18601 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018602 wifi_debug(DEBUG_ERROR, "wifi_setApSecurity return error\n");
developera3511852023-06-14 14:12:59 +080018603 return RETURN_ERR;
18604 }
developer333c1eb2023-05-31 14:59:39 +080018605
developer56fbedb2023-05-30 16:47:05 +080018606 hostapd_raw_restart_bss(vap_info->vap_index);
developer72fb0bb2023-01-11 09:46:29 +080018607
developera3511852023-06-14 14:12:59 +080018608 multiple_set = FALSE;
developer23e71282023-01-18 10:25:19 +080018609
developera3511852023-06-14 14:12:59 +080018610 // If config use hostapd_cli to set, we calling these type of functions after enable the ap.
18611 ret = wifi_setApMacAddressControlMode(vap_info->vap_index, acl_mode);
18612 if (ret != RETURN_OK) {
developer86035662023-06-28 19:21:12 +080018613 wifi_debug(DEBUG_ERROR, "wifi_setApMacAddressControlMode return error\n");
developera3511852023-06-14 14:12:59 +080018614 return RETURN_ERR;
18615 }
developer72fb0bb2023-01-11 09:46:29 +080018616
developera3511852023-06-14 14:12:59 +080018617 // TODO mgmtPowerControl, interworking, wps
18618 }
18619 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18620 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018621}
18622
18623int parse_channel_list_int_arr(char *pchannels, wifi_channels_list_t* chlistptr)
18624{
developera3511852023-06-14 14:12:59 +080018625 char *token, *next;
18626 const char s[2] = ",";
18627 int count =0;
developer72fb0bb2023-01-11 09:46:29 +080018628
developera3511852023-06-14 14:12:59 +080018629 /* get the first token */
18630 token = strtok_r(pchannels, s, &next);
developer72fb0bb2023-01-11 09:46:29 +080018631
developera3511852023-06-14 14:12:59 +080018632 /* walk through other tokens */
18633 while( token != NULL && count < MAX_CHANNELS) {
18634 chlistptr->channels_list[count++] = atoi(token);
18635 token = strtok_r(NULL, s, &next);
18636 }
developer72fb0bb2023-01-11 09:46:29 +080018637
developera3511852023-06-14 14:12:59 +080018638 return count;
developer72fb0bb2023-01-11 09:46:29 +080018639}
18640
18641static int getRadioCapabilities(int radioIndex, wifi_radio_capabilities_t *rcap)
18642{
developera3511852023-06-14 14:12:59 +080018643 INT status;
18644 wifi_channels_list_t *chlistp;
18645 CHAR output_string[64];
18646 CHAR pchannels[128];
18647 CHAR interface_name[16] = {0};
18648 wifi_band band;
developere40952c2023-06-15 18:46:43 +080018649 int res;
developer72fb0bb2023-01-11 09:46:29 +080018650
developera3511852023-06-14 14:12:59 +080018651 if(rcap == NULL)
18652 {
18653 return RETURN_ERR;
18654 }
developer72fb0bb2023-01-11 09:46:29 +080018655
developera3511852023-06-14 14:12:59 +080018656 rcap->numSupportedFreqBand = 1;
18657 band = wifi_index_to_band(radioIndex);
developer72fb0bb2023-01-11 09:46:29 +080018658
developera3511852023-06-14 14:12:59 +080018659 if (band == band_2_4)
18660 rcap->band[0] = WIFI_FREQUENCY_2_4_BAND;
18661 else if (band == band_5)
18662 rcap->band[0] = WIFI_FREQUENCY_5_BAND;
18663 else if (band == band_6)
18664 rcap->band[0] = WIFI_FREQUENCY_6_BAND;
developer72fb0bb2023-01-11 09:46:29 +080018665
developera3511852023-06-14 14:12:59 +080018666 chlistp = &(rcap->channel_list[0]);
18667 memset(pchannels, 0, sizeof(pchannels));
developer72fb0bb2023-01-11 09:46:29 +080018668
developera3511852023-06-14 14:12:59 +080018669 /* possible number of radio channels */
18670 status = wifi_getRadioPossibleChannels(radioIndex, pchannels);
18671 {
18672 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, pchannels);
18673 }
18674 /* Number of channels and list*/
18675 chlistp->num_channels = parse_channel_list_int_arr(pchannels, chlistp);
developer72fb0bb2023-01-11 09:46:29 +080018676
developera3511852023-06-14 14:12:59 +080018677 /* autoChannelSupported */
18678 /* always ON with wifi_getRadioAutoChannelSupported */
18679 rcap->autoChannelSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018680
developera3511852023-06-14 14:12:59 +080018681 /* DCSSupported */
18682 /* always ON with wifi_getRadioDCSSupported */
18683 rcap->DCSSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018684
developera3511852023-06-14 14:12:59 +080018685 /* zeroDFSSupported - TBD */
18686 rcap->zeroDFSSupported = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080018687
developera3511852023-06-14 14:12:59 +080018688 /* Supported Country List*/
18689 memset(output_string, 0, sizeof(output_string));
18690 status = wifi_getRadioCountryCode(radioIndex, output_string);
18691 if( status != 0 ) {
18692 printf("[wifi_hal dbg] : func[%s] line[%d] error_ret[%d] radio_index[%d] output[%s]\n", __FUNCTION__, __LINE__, status, radioIndex, output_string);
18693 return RETURN_ERR;
18694 } else {
18695 printf("[wifi_hal dbg] : func[%s] line[%d], output [%s]\n", __FUNCTION__, __LINE__, output_string);
18696 }
18697 if(!strcmp(output_string,"US")){
18698 rcap->countrySupported[0] = wifi_countrycode_US;
18699 rcap->countrySupported[1] = wifi_countrycode_CA;
18700 } else if (!strcmp(output_string,"CA")) {
18701 rcap->countrySupported[0] = wifi_countrycode_CA;
18702 rcap->countrySupported[1] = wifi_countrycode_US;
18703 } else {
18704 printf("[wifi_hal dbg] : func[%s] line[%d] radio_index[%d] Invalid Country [%s]\n", __FUNCTION__, __LINE__, radioIndex, output_string);
18705 }
developer72fb0bb2023-01-11 09:46:29 +080018706
developera3511852023-06-14 14:12:59 +080018707 rcap->numcountrySupported = 2;
developer72fb0bb2023-01-11 09:46:29 +080018708
developera3511852023-06-14 14:12:59 +080018709 /* csi */
18710 rcap->csi.maxDevices = 8;
18711 rcap->csi.soudingFrameSupported = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080018712
developer86035662023-06-28 19:21:12 +080018713 if (wifi_GetInterfaceName(radioIndex, interface_name) != RETURN_OK) {
18714 wifi_debug(DEBUG_ERROR, "wifi_GetInterfaceName fail\n");
18715 }
developere40952c2023-06-15 18:46:43 +080018716 res = snprintf(rcap->ifaceName, sizeof(interface_name), "%s",interface_name);
18717 if (os_snprintf_error(sizeof(interface_name), res)) {
18718 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18719 return RETURN_ERR;
18720 }
developer72fb0bb2023-01-11 09:46:29 +080018721
developera3511852023-06-14 14:12:59 +080018722 /* channelWidth - all supported bandwidths */
18723 int i=0;
18724 rcap->channelWidth[i] = 0;
18725 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18726 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18727 WIFI_CHANNELBANDWIDTH_40MHZ);
developer72fb0bb2023-01-11 09:46:29 +080018728
developera3511852023-06-14 14:12:59 +080018729 }
18730 else if (rcap->band[i] & (WIFI_FREQUENCY_5_BAND ) || rcap->band[i] & (WIFI_FREQUENCY_6_BAND)) {
18731 rcap->channelWidth[i] |= (WIFI_CHANNELBANDWIDTH_20MHZ |
18732 WIFI_CHANNELBANDWIDTH_40MHZ |
18733 WIFI_CHANNELBANDWIDTH_80MHZ | WIFI_CHANNELBANDWIDTH_160MHZ);
18734 }
developer72fb0bb2023-01-11 09:46:29 +080018735
18736
developera3511852023-06-14 14:12:59 +080018737 /* mode - all supported variants */
18738 // rcap->mode[i] = WIFI_80211_VARIANT_H;
18739 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) {
18740 rcap->mode[i] = ( WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX );
18741 }
18742 else if (rcap->band[i] & WIFI_FREQUENCY_5_BAND ) {
18743 rcap->mode[i] = ( WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX );
18744 }
18745 else if (rcap->band[i] & WIFI_FREQUENCY_6_BAND) {
18746 rcap->mode[i] = ( WIFI_80211_VARIANT_AX );
18747 }
18748 rcap->maxBitRate[i] = ( rcap->band[i] & WIFI_FREQUENCY_2_4_BAND ) ? 300 :
18749 ((rcap->band[i] & WIFI_FREQUENCY_5_BAND) ? 1734 : 0);
developer72fb0bb2023-01-11 09:46:29 +080018750
developera3511852023-06-14 14:12:59 +080018751 /* supportedBitRate - all supported bitrates */
18752 rcap->supportedBitRate[i] = 0;
18753 if (rcap->band[i] & WIFI_FREQUENCY_2_4_BAND) {
18754 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18755 WIFI_BITRATE_11MBPS | WIFI_BITRATE_12MBPS);
18756 }
18757 else if ((rcap->band[i] & (WIFI_FREQUENCY_5_BAND )) || (rcap->band[i] & (WIFI_FREQUENCY_6_BAND))) {
18758 rcap->supportedBitRate[i] |= (WIFI_BITRATE_6MBPS | WIFI_BITRATE_9MBPS |
18759 WIFI_BITRATE_12MBPS | WIFI_BITRATE_18MBPS | WIFI_BITRATE_24MBPS |
18760 WIFI_BITRATE_36MBPS | WIFI_BITRATE_48MBPS | WIFI_BITRATE_54MBPS);
18761 }
developer72fb0bb2023-01-11 09:46:29 +080018762
18763
developera3511852023-06-14 14:12:59 +080018764 rcap->transmitPowerSupported_list[i].numberOfElements = 5;
18765 rcap->transmitPowerSupported_list[i].transmitPowerSupported[0]=12;
18766 rcap->transmitPowerSupported_list[i].transmitPowerSupported[1]=25;
18767 rcap->transmitPowerSupported_list[i].transmitPowerSupported[2]=50;
18768 rcap->transmitPowerSupported_list[i].transmitPowerSupported[3]=75;
18769 rcap->transmitPowerSupported_list[i].transmitPowerSupported[4]=100;
18770 rcap->cipherSupported = 0;
18771 rcap->cipherSupported |= WIFI_CIPHER_CAPA_ENC_TKIP | WIFI_CIPHER_CAPA_ENC_CCMP;
18772 rcap->maxNumberVAPs = MAX_NUM_VAP_PER_RADIO;
developer72fb0bb2023-01-11 09:46:29 +080018773
developera3511852023-06-14 14:12:59 +080018774 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018775}
18776
18777INT wifi_getHalCapability(wifi_hal_capability_t *cap)
18778{
developera3511852023-06-14 14:12:59 +080018779 INT status = 0, radioIndex = 0;
18780 char output[MAX_BUF_SIZE] = {0};
18781 int iter = 0;
18782 unsigned int j = 0;
developer9ce44382023-06-28 11:09:37 +080018783 int max_num_radios = 0;
developera3511852023-06-14 14:12:59 +080018784 wifi_interface_name_idex_map_t *iface_info = NULL;
developer72fb0bb2023-01-11 09:46:29 +080018785
developera3511852023-06-14 14:12:59 +080018786 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018787
developera3511852023-06-14 14:12:59 +080018788 memset(cap, 0, sizeof(wifi_hal_capability_t));
developer72fb0bb2023-01-11 09:46:29 +080018789
developera3511852023-06-14 14:12:59 +080018790 /* version */
18791 cap->version.major = WIFI_HAL_MAJOR_VERSION;
18792 cap->version.minor = WIFI_HAL_MINOR_VERSION;
developer72fb0bb2023-01-11 09:46:29 +080018793
developera3511852023-06-14 14:12:59 +080018794 /* number of radios platform property */
18795 wifi_getMaxRadioNumber(&max_num_radios);
18796 cap->wifi_prop.numRadios = max_num_radios;
developer72fb0bb2023-01-11 09:46:29 +080018797
developera3511852023-06-14 14:12:59 +080018798 for(radioIndex=0; radioIndex < cap->wifi_prop.numRadios; radioIndex++)
18799 {
18800 status = getRadioCapabilities(radioIndex, &(cap->wifi_prop.radiocap[radioIndex]));
18801 if (status != 0) {
18802 printf("%s: getRadioCapabilities idx = %d\n", __FUNCTION__, radioIndex);
18803 return RETURN_ERR;
18804 }
developer72fb0bb2023-01-11 09:46:29 +080018805
developera3511852023-06-14 14:12:59 +080018806 for (j = 0; j < cap->wifi_prop.radiocap[radioIndex].maxNumberVAPs; j++)
18807 {
18808 if (iter >= MAX_NUM_RADIOS * MAX_NUM_VAP_PER_RADIO)
18809 {
18810 printf("%s: to many vaps for index map (%d)\n", __func__, iter);
18811 return RETURN_ERR;
18812 }
18813 iface_info = &cap->wifi_prop.interface_map[iter];
18814 iface_info->phy_index = radioIndex; // XXX: parse phyX index instead
18815 iface_info->rdk_radio_index = radioIndex;
18816 memset(output, 0, sizeof(output));
18817 if (wifi_getRadioIfName(radioIndex, output) == RETURN_OK)
18818 {
18819 strncpy(iface_info->interface_name, output, sizeof(iface_info->interface_name) - 1);
18820 }
18821 // TODO: bridge name
18822 // TODO: vlan id
18823 // TODO: primary
18824 iface_info->index = array_index_to_vap_index(radioIndex, j);
18825 memset(output, 0, sizeof(output));
18826 if (wifi_getApName(iface_info->index, output) == RETURN_OK)
18827 {
18828 strncpy(iface_info->vap_name, output, sizeof(iface_info->vap_name) - 1);
18829 }
18830 iter++;
18831 }
18832 }
developer72fb0bb2023-01-11 09:46:29 +080018833
developera3511852023-06-14 14:12:59 +080018834 cap->BandSteeringSupported = FALSE;
18835 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
18836 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018837}
18838
18839INT wifi_setOpportunisticKeyCaching(int ap_index, BOOL okc_enable)
18840{
developera3511852023-06-14 14:12:59 +080018841 struct params h_config={0};
18842 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018843 int res;
developer72fb0bb2023-01-11 09:46:29 +080018844
developera3511852023-06-14 14:12:59 +080018845 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018846
developera3511852023-06-14 14:12:59 +080018847 h_config.name = "okc";
18848 h_config.value = okc_enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018849
developere40952c2023-06-15 18:46:43 +080018850 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18851 if (os_snprintf_error(sizeof(config_file), res)) {
18852 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18853 return RETURN_ERR;
18854 }
developera3511852023-06-14 14:12:59 +080018855 wifi_hostapdWrite(config_file, &h_config, 1);
18856 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018857
developera3511852023-06-14 14:12:59 +080018858 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18859 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018860}
18861
18862INT wifi_setSAEMFP(int ap_index, BOOL enable)
18863{
developera3511852023-06-14 14:12:59 +080018864 struct params h_config={0};
18865 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018866 int res;
developer72fb0bb2023-01-11 09:46:29 +080018867
developera3511852023-06-14 14:12:59 +080018868 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018869
developera3511852023-06-14 14:12:59 +080018870 h_config.name = "sae_require_mfp";
18871 h_config.value = enable?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018872
developere40952c2023-06-15 18:46:43 +080018873 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18874 if (os_snprintf_error(sizeof(config_file), res)) {
18875 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18876 return RETURN_ERR;
18877 }
developera3511852023-06-14 14:12:59 +080018878 wifi_hostapdWrite(config_file, &h_config, 1);
18879 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018880
developera3511852023-06-14 14:12:59 +080018881 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18882 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018883}
18884
18885INT wifi_setSAEpwe(int ap_index, int sae_pwe)
18886{
developera3511852023-06-14 14:12:59 +080018887 struct params h_config={0};
18888 char config_file[64] = {0};
18889 char buf[128] = {0};
developere40952c2023-06-15 18:46:43 +080018890 int res;
developer72fb0bb2023-01-11 09:46:29 +080018891
developera3511852023-06-14 14:12:59 +080018892 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018893
developera3511852023-06-14 14:12:59 +080018894 h_config.name = "sae_pwe";
developere40952c2023-06-15 18:46:43 +080018895 res = snprintf(buf, sizeof(buf), "%d", sae_pwe);
18896 if (os_snprintf_error(sizeof(buf), res)) {
18897 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18898 return RETURN_ERR;
18899 }
18900
developera3511852023-06-14 14:12:59 +080018901 h_config.value = buf;
developer72fb0bb2023-01-11 09:46:29 +080018902
developere40952c2023-06-15 18:46:43 +080018903 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18904 if (os_snprintf_error(sizeof(config_file), res)) {
18905 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18906 return RETURN_ERR;
18907 }
developera3511852023-06-14 14:12:59 +080018908 wifi_hostapdWrite(config_file, &h_config, 1);
18909 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018910
developera3511852023-06-14 14:12:59 +080018911 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18912 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018913}
18914
18915INT wifi_setDisable_EAPOL_retries(int ap_index, BOOL disable_EAPOL_retries)
18916{
developera3511852023-06-14 14:12:59 +080018917 // wpa3 use SAE instead of PSK, so we need to disable this feature when using wpa3.
18918 struct params h_config={0};
18919 char config_file[64] = {0};
developere40952c2023-06-15 18:46:43 +080018920 int res;
developer72fb0bb2023-01-11 09:46:29 +080018921
developera3511852023-06-14 14:12:59 +080018922 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n", __func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018923
developera3511852023-06-14 14:12:59 +080018924 h_config.name = "wpa_disable_eapol_key_retries";
18925 h_config.value = disable_EAPOL_retries?"1":"0";
developer72fb0bb2023-01-11 09:46:29 +080018926
developere40952c2023-06-15 18:46:43 +080018927 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18928 if (os_snprintf_error(sizeof(config_file), res)) {
18929 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18930 return RETURN_ERR;
18931 }
developera3511852023-06-14 14:12:59 +080018932 wifi_hostapdWrite(config_file, &h_config, 1);
18933 wifi_hostapdProcessUpdate(ap_index, &h_config, 1);
developer72fb0bb2023-01-11 09:46:29 +080018934
developera3511852023-06-14 14:12:59 +080018935 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n", __func__, __LINE__);
18936 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080018937}
18938
18939INT wifi_setApSecurity(INT ap_index, wifi_vap_security_t *security)
18940{
developera3511852023-06-14 14:12:59 +080018941 char buf[128] = {0};
18942 char config_file[128] = {0};
18943 char cmd[MAX_CMD_SIZE] = {0};
18944 char password[65] = {0};
18945 char mfp[32] = {0};
18946 char wpa_mode[32] = {0};
18947 BOOL okc_enable = FALSE;
18948 BOOL sae_MFP = FALSE;
18949 BOOL disable_EAPOL_retries = TRUE;
18950 int sae_pwe = 0;
18951 struct params params = {0};
18952 wifi_band band = band_invalid;
developere40952c2023-06-15 18:46:43 +080018953 int res;
developer72fb0bb2023-01-11 09:46:29 +080018954
developera3511852023-06-14 14:12:59 +080018955 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080018956
developera3511852023-06-14 14:12:59 +080018957 multiple_set = TRUE;
developer37646972023-06-29 10:58:43 +080018958 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
18959 if (os_snprintf_error(sizeof(config_file), res)) {
18960 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
18961 return RETURN_ERR;
18962 }
developera3511852023-06-14 14:12:59 +080018963 if (security->mode == wifi_security_mode_none) {
developer9ce44382023-06-28 11:09:37 +080018964 strncpy(wpa_mode, "None",sizeof(wpa_mode) - 1);
18965 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18966 } else if (security->mode == wifi_security_mode_wpa_personal) {
18967 strncpy(wpa_mode, "WPA-Personal",sizeof(wpa_mode) - 1);
18968 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18969 } else if (security->mode == wifi_security_mode_wpa2_personal){
18970 strncpy(wpa_mode, "WPA2-Personal",sizeof(wpa_mode) - 1);
18971 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18972 } else if (security->mode == wifi_security_mode_wpa_wpa2_personal){
18973 strncpy(wpa_mode, "WPA-WPA2-Personal",sizeof(wpa_mode) - 1);
18974 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18975 } else if (security->mode == wifi_security_mode_wpa_enterprise){
18976 strncpy(wpa_mode, "WPA-Enterprise",sizeof(wpa_mode) - 1);
18977 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18978 } else if (security->mode == wifi_security_mode_wpa2_enterprise){
18979 strncpy(wpa_mode, "WPA2-Enterprise",sizeof(wpa_mode) - 1);
18980 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18981 } else if (security->mode == wifi_security_mode_wpa_wpa2_enterprise){
18982 strncpy(wpa_mode, "WPA-WAP2-Enterprise",sizeof(wpa_mode) - 1);
18983 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
18984 } else if (security->mode == wifi_security_mode_wpa3_personal) {
18985 strncpy(wpa_mode, "WPA3-Personal",sizeof(wpa_mode) - 1);
18986 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080018987 okc_enable = TRUE;
18988 sae_MFP = TRUE;
18989 sae_pwe = 2;
18990 disable_EAPOL_retries = FALSE;
18991 } else if (security->mode == wifi_security_mode_wpa3_transition) {
developer9ce44382023-06-28 11:09:37 +080018992 strncpy(wpa_mode, "WPA3-Personal-Transition",sizeof(wpa_mode) - 1);
18993 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080018994 okc_enable = TRUE;
18995 sae_MFP = TRUE;
18996 sae_pwe = 2;
18997 disable_EAPOL_retries = FALSE;
18998 } else if (security->mode == wifi_security_mode_wpa3_enterprise) {
developer9ce44382023-06-28 11:09:37 +080018999 strncpy(wpa_mode, "WPA3-Enterprise",sizeof(wpa_mode) - 1);
19000 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019001 sae_MFP = TRUE;
19002 sae_pwe = 2;
19003 disable_EAPOL_retries = FALSE;
19004 } else if (security->mode == wifi_security_mode_enhanced_open) {
developer9ce44382023-06-28 11:09:37 +080019005 strncpy(wpa_mode, "OWE",sizeof(wpa_mode) - 1);
19006 wpa_mode[sizeof(wpa_mode) - 1] = '\0';
developera3511852023-06-14 14:12:59 +080019007 sae_MFP = TRUE;
19008 sae_pwe = 2;
19009 disable_EAPOL_retries = FALSE;
19010 }
developer72fb0bb2023-01-11 09:46:29 +080019011
developera3511852023-06-14 14:12:59 +080019012 band = wifi_index_to_band(ap_index);
19013 if (band == band_6 && strstr(wpa_mode, "WPA3") == NULL) {
developerc14d83a2023-06-29 20:09:42 +080019014 wifi_debug(DEBUG_ERROR, "%s: 6G band must set with wpa3.\n", __func__);
developera3511852023-06-14 14:12:59 +080019015 return RETURN_ERR;
19016 }
developer72fb0bb2023-01-11 09:46:29 +080019017
developera3511852023-06-14 14:12:59 +080019018 wifi_setApSecurityModeEnabled(ap_index, wpa_mode);
19019 wifi_setOpportunisticKeyCaching(ap_index, okc_enable);
19020 wifi_setSAEMFP(ap_index, sae_MFP);
19021 wifi_setSAEpwe(ap_index, sae_pwe);
19022 wifi_setDisable_EAPOL_retries(ap_index, disable_EAPOL_retries);
developer72fb0bb2023-01-11 09:46:29 +080019023
developera3511852023-06-14 14:12:59 +080019024 if (security->mode != wifi_security_mode_none && security->mode != wifi_security_mode_enhanced_open) {
19025 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) {
19026 int key_len = strlen(security->u.key.key);
19027 // wpa_psk and wpa_passphrase cann;t use at the same time, the command replace one with the other.
19028 if (key_len == 64) { // set wpa_psk
19029 strncpy(password, security->u.key.key, 64); // 64 characters
19030 password[64] = '\0';
19031 wifi_setApSecurityPreSharedKey(ap_index, password);
developere40952c2023-06-15 18:46:43 +080019032 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^wpa_passphrase=/!p' %s", config_file);
19033 if (os_snprintf_error(sizeof(cmd), res)) {
19034 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19035 return RETURN_ERR;
19036 }
developera3511852023-06-14 14:12:59 +080019037 } else if (key_len >= 8 && key_len < 64) { // set wpa_passphrase
19038 strncpy(password, security->u.key.key, 63);
19039 password[63] = '\0';
19040 wifi_setApSecurityKeyPassphrase(ap_index, password);
developere40952c2023-06-15 18:46:43 +080019041 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^wpa_psk=/!p' %s", config_file);
developer9ce44382023-06-28 11:09:37 +080019042 if (os_snprintf_error(sizeof(cmd), res)) {
19043 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19044 return RETURN_ERR;
19045 }
developera3511852023-06-14 14:12:59 +080019046 } else
19047 return RETURN_ERR;
19048 _syscmd(cmd, buf, sizeof(buf));
19049 }
19050 if (security->u.key.type == wifi_security_key_type_sae || security->u.key.type == wifi_security_key_type_psk_sae) {
19051 params.name = "sae_password";
19052 params.value = security->u.key.key;
19053 wifi_hostapdWrite(config_file, &params, 1);
19054 } else { // remove sae_password
developere40952c2023-06-15 18:46:43 +080019055 res = snprintf(cmd, sizeof(cmd), "sed -i -n -e '/^sae_password=/!p' %s", config_file);
19056 if (os_snprintf_error(sizeof(cmd), res)) {
19057 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19058 return RETURN_ERR;
19059 }
developera3511852023-06-14 14:12:59 +080019060 _syscmd(cmd, buf, sizeof(buf));
19061 }
19062 }
developer72fb0bb2023-01-11 09:46:29 +080019063
developera3511852023-06-14 14:12:59 +080019064 if (security->mode != wifi_security_mode_none) {
19065 memset(&params, 0, sizeof(params));
19066 params.name = "wpa_pairwise";
19067 if (security->encr == wifi_encryption_tkip)
19068 params.value = "TKIP";
19069 else if (security->encr == wifi_encryption_aes)
19070 params.value = "CCMP";
19071 else if (security->encr == wifi_encryption_aes_tkip)
19072 params.value = "TKIP CCMP";
19073 wifi_hostapdWrite(config_file, &params, 1);
19074 }
developer72fb0bb2023-01-11 09:46:29 +080019075
developer9ce44382023-06-28 11:09:37 +080019076 if (security->mfp == wifi_mfp_cfg_disabled){
19077 strncpy(mfp,"Disabled",sizeof(mfp)-1);
19078 mfp[sizeof(mfp)-1] = '\0';
19079 } else if (security->mfp == wifi_mfp_cfg_optional){
19080 strncpy(mfp,"Optional",sizeof(mfp)-1);
19081 mfp[sizeof(mfp)-1] = '\0';
19082 } else if (security->mfp == wifi_mfp_cfg_required){
19083 strncpy(mfp,"Required",sizeof(mfp)-1);
19084 mfp[sizeof(mfp)-1] = '\0';
19085 }
developera3511852023-06-14 14:12:59 +080019086 wifi_setApSecurityMFPConfig(ap_index, mfp);
developer72fb0bb2023-01-11 09:46:29 +080019087
developera3511852023-06-14 14:12:59 +080019088 memset(&params, 0, sizeof(params));
19089 params.name = "transition_disable";
19090 if (security->wpa3_transition_disable == TRUE)
19091 params.value = "0x01";
19092 else
19093 params.value = "0x00";
19094 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019095
developera3511852023-06-14 14:12:59 +080019096 memset(&params, 0, sizeof(params));
19097 params.name = "wpa_group_rekey";
developere40952c2023-06-15 18:46:43 +080019098 res = snprintf(buf, sizeof(buf), "%d", security->rekey_interval);
19099 if (os_snprintf_error(sizeof(buf), res)) {
19100 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19101 return RETURN_ERR;
19102 }
developera3511852023-06-14 14:12:59 +080019103 params.value = buf;
19104 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019105
developera3511852023-06-14 14:12:59 +080019106 memset(&params, 0, sizeof(params));
19107 params.name = "wpa_strict_rekey";
19108 params.value = security->strict_rekey?"1":"0";
19109 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019110
developera3511852023-06-14 14:12:59 +080019111 memset(&params, 0, sizeof(params));
19112 params.name = "wpa_pairwise_update_count";
19113 if (security->eapol_key_retries == 0)
19114 security->eapol_key_retries = 4; // 0 is invalid, set to default value.
developere40952c2023-06-15 18:46:43 +080019115 res = snprintf(buf, sizeof(buf), "%u", security->eapol_key_retries);
19116 if (os_snprintf_error(sizeof(buf), res)) {
19117 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19118 return RETURN_ERR;
19119 }
developera3511852023-06-14 14:12:59 +080019120 params.value = buf;
19121 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019122
developera3511852023-06-14 14:12:59 +080019123 memset(&params, 0, sizeof(params));
19124 params.name = "disable_pmksa_caching";
19125 params.value = security->disable_pmksa_caching?"1":"0";
19126 wifi_hostapdWrite(config_file, &params, 1);
developer72fb0bb2023-01-11 09:46:29 +080019127
developera3511852023-06-14 14:12:59 +080019128 if (multiple_set == FALSE) {
19129 wifi_setApEnable(ap_index, FALSE);
19130 wifi_setApEnable(ap_index, TRUE);
19131 }
developer72fb0bb2023-01-11 09:46:29 +080019132
developera3511852023-06-14 14:12:59 +080019133 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019134
developera3511852023-06-14 14:12:59 +080019135 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019136}
19137
19138INT wifi_getApSecurity(INT ap_index, wifi_vap_security_t *security)
19139{
developera3511852023-06-14 14:12:59 +080019140 char buf[256] = {0};
19141 char config_file[128] = {0};
developer86035662023-06-28 19:21:12 +080019142 long int disable = 0;
19143 long int tmp;
developera3511852023-06-14 14:12:59 +080019144 bool set_sae = FALSE;
developere75ba632023-06-29 16:03:33 +080019145 int res;
developer72fb0bb2023-01-11 09:46:29 +080019146
developera3511852023-06-14 14:12:59 +080019147 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developere75ba632023-06-29 16:03:33 +080019148 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, ap_index);
19149 if (os_snprintf_error(sizeof(config_file), res)) {
19150 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19151 return RETURN_ERR;
19152 }
developera3511852023-06-14 14:12:59 +080019153 wifi_getApSecurityModeEnabled(ap_index, buf); // Get wpa config
19154 security->mode = wifi_security_mode_none;
19155 if (strlen(buf) != 0) {
19156 if (!strcmp(buf, "WPA-Personal"))
19157 security->mode = wifi_security_mode_wpa_personal;
19158 else if (!strcmp(buf, "WPA2-Personal"))
19159 security->mode = wifi_security_mode_wpa2_personal;
19160 else if (!strcmp(buf, "WPA-WPA2-Personal"))
19161 security->mode = wifi_security_mode_wpa_wpa2_personal;
19162 else if (!strcmp(buf, "WPA-Enterprise"))
19163 security->mode = wifi_security_mode_wpa_enterprise;
19164 else if (!strcmp(buf, "WPA2-Enterprise"))
19165 security->mode = wifi_security_mode_wpa2_enterprise;
19166 else if (!strcmp(buf, "WPA-WPA2-Enterprise"))
19167 security->mode = wifi_security_mode_wpa_wpa2_enterprise;
19168 else if (!strcmp(buf, "WPA3-Personal"))
19169 security->mode = wifi_security_mode_wpa3_personal;
19170 else if (!strcmp(buf, "WPA3-Personal-Transition"))
19171 security->mode = wifi_security_mode_wpa3_transition;
19172 else if (!strcmp(buf, "WPA3-Enterprise"))
19173 security->mode = wifi_security_mode_wpa3_enterprise;
19174 else if (!strcmp(buf, "OWE"))
19175 security->mode = wifi_security_mode_enhanced_open;
19176 }
developer72fb0bb2023-01-11 09:46:29 +080019177
developera3511852023-06-14 14:12:59 +080019178 wifi_hostapdRead(config_file,"wpa_pairwise",buf,sizeof(buf));
19179 if (security->mode == wifi_security_mode_none)
19180 security->encr = wifi_encryption_none;
19181 else {
19182 if (strcmp(buf, "TKIP") == 0)
19183 security->encr = wifi_encryption_tkip;
19184 else if (strcmp(buf, "CCMP") == 0)
19185 security->encr = wifi_encryption_aes;
19186 else
19187 security->encr = wifi_encryption_aes_tkip;
19188 }
developer72fb0bb2023-01-11 09:46:29 +080019189
developera3511852023-06-14 14:12:59 +080019190 if (security->mode != wifi_security_mode_none) {
19191 memset(buf, 0, sizeof(buf));
19192 // wpa3 can use one or both configs as password, so we check sae_password first.
19193 wifi_hostapdRead(config_file, "sae_password", buf, sizeof(buf));
19194 if (strlen(buf) != 0) {
19195 if (security->mode == wifi_security_mode_wpa3_personal || security->mode == wifi_security_mode_wpa3_transition)
19196 security->u.key.type = wifi_security_key_type_sae;
19197 set_sae = TRUE;
19198 strncpy(security->u.key.key, buf, sizeof(buf));
19199 }
19200 wifi_hostapdRead(config_file, "wpa_passphrase", buf, sizeof(buf));
19201 if (strlen(buf) != 0){
19202 if (set_sae == TRUE)
19203 security->u.key.type = wifi_security_key_type_psk_sae;
19204 else if (strlen(buf) == 64)
19205 security->u.key.type = wifi_security_key_type_psk;
19206 else
19207 security->u.key.type = wifi_security_key_type_pass;
19208 strncpy(security->u.key.key, buf, sizeof(security->u.key.key));
19209 }
19210 security->u.key.key[255] = '\0';
19211 }
developer72fb0bb2023-01-11 09:46:29 +080019212
developera3511852023-06-14 14:12:59 +080019213 memset(buf, 0, sizeof(buf));
19214 wifi_getApSecurityMFPConfig(ap_index, buf);
19215 if (strcmp(buf, "Disabled") == 0)
19216 security->mfp = wifi_mfp_cfg_disabled;
19217 else if (strcmp(buf, "Optional") == 0)
19218 security->mfp = wifi_mfp_cfg_optional;
19219 else if (strcmp(buf, "Required") == 0)
19220 security->mfp = wifi_mfp_cfg_required;
developer72fb0bb2023-01-11 09:46:29 +080019221
developera3511852023-06-14 14:12:59 +080019222 memset(buf, 0, sizeof(buf));
19223 security->wpa3_transition_disable = FALSE;
19224 wifi_hostapdRead(config_file, "transition_disable", buf, sizeof(buf));
developer3de255a2023-06-29 10:35:45 +080019225 if (strlen(buf) == 0)
19226 disable = 0;
19227 else {
19228 if (hal_strtol(buf, 16, &disable) < 0) {
19229 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19230 return RETURN_ERR;
19231 }
developer86035662023-06-28 19:21:12 +080019232 }
developera3511852023-06-14 14:12:59 +080019233 if (disable != 0)
19234 security->wpa3_transition_disable = TRUE;
developer72fb0bb2023-01-11 09:46:29 +080019235
developera3511852023-06-14 14:12:59 +080019236 memset(buf, 0, sizeof(buf));
19237 wifi_hostapdRead(config_file, "wpa_group_rekey", buf, sizeof(buf));
19238 if (strlen(buf) == 0)
19239 security->rekey_interval = 86400;
developer86035662023-06-28 19:21:12 +080019240 else {
19241 if (hal_strtol(buf, 10, &tmp) < 0) {
19242 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19243 return RETURN_ERR;
19244 }
19245 security->rekey_interval = tmp;
19246 }
developer72fb0bb2023-01-11 09:46:29 +080019247
developera3511852023-06-14 14:12:59 +080019248 memset(buf, 0, sizeof(buf));
19249 wifi_hostapdRead(config_file, "wpa_strict_rekey", buf, sizeof(buf));
19250 if (strlen(buf) == 0)
19251 security->strict_rekey = 1;
developer86035662023-06-28 19:21:12 +080019252 else {
19253 if (hal_strtol(buf, 10, &tmp) < 0) {
19254 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19255 return RETURN_ERR;
19256 }
19257 security->strict_rekey = tmp;
19258 }
developer72fb0bb2023-01-11 09:46:29 +080019259
developera3511852023-06-14 14:12:59 +080019260 memset(buf, 0, sizeof(buf));
19261 wifi_hostapdRead(config_file, "wpa_pairwise_update_count", buf, sizeof(buf));
19262 if (strlen(buf) == 0)
19263 security->eapol_key_retries = 4;
developer86035662023-06-28 19:21:12 +080019264 else {
19265 if (hal_strtol(buf, 10, &tmp) < 0) {
19266 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19267 return RETURN_ERR;
19268 }
19269 security->eapol_key_retries = tmp;
19270 }
developer72fb0bb2023-01-11 09:46:29 +080019271
developera3511852023-06-14 14:12:59 +080019272 memset(buf, 0, sizeof(buf));
19273 wifi_hostapdRead(config_file, "disable_pmksa_caching", buf, sizeof(buf));
19274 if (strlen(buf) == 0)
19275 security->disable_pmksa_caching = FALSE;
developer86035662023-06-28 19:21:12 +080019276 else {
19277 if (hal_strtol(buf, 10, &(tmp)) < 0) {
19278 wifi_debug(DEBUG_ERROR, "strtol fail\n");
19279 return RETURN_ERR;
19280 }
19281 security->disable_pmksa_caching = tmp ? TRUE : FALSE;
19282 }
developera3511852023-06-14 14:12:59 +080019283 /* TODO
19284 eapol_key_timeout, eap_identity_req_timeout, eap_identity_req_retries, eap_req_timeout, eap_req_retries
19285 */
19286 security->eapol_key_timeout = 1000; // Unit is ms. The default value in protocol.
19287 security->eap_identity_req_timeout = 0;
19288 security->eap_identity_req_retries = 0;
19289 security->eap_req_timeout = 0;
19290 security->eap_req_retries = 0;
19291 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19292 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019293}
19294
19295#endif /* WIFI_HAL_VERSION_3 */
19296
19297#ifdef WIFI_HAL_VERSION_3_PHASE2
19298INT wifi_getApAssociatedDevice(INT ap_index, mac_address_t *output_deviceMacAddressArray, UINT maxNumDevices, UINT *output_numDevices)
19299{
developera3511852023-06-14 14:12:59 +080019300 char interface_name[16] = {0};
19301 char cmd[128] = {0};
19302 char buf[128] = {0};
19303 char *mac_addr = NULL;
19304 BOOL status = FALSE;
19305 size_t len = 0;
developer72fb0bb2023-01-11 09:46:29 +080019306
developera3511852023-06-14 14:12:59 +080019307 if(ap_index > MAX_APS)
19308 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019309
developera3511852023-06-14 14:12:59 +080019310 *output_numDevices = 0;
19311 wifi_getApEnable(ap_index, &status);
19312 if (status == FALSE)
19313 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019314
developera3511852023-06-14 14:12:59 +080019315 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19316 return RETURN_ERR;
19317 sprintf(cmd, "hostapd_cli -i %s list_sta", interface_name);
19318 _syscmd(cmd, buf, sizeof(buf));
developer72fb0bb2023-01-11 09:46:29 +080019319
developera3511852023-06-14 14:12:59 +080019320 mac_addr = strtok(buf, "\n");
19321 for (int i = 0; i < maxNumDevices && mac_addr != NULL; i++) {
19322 *output_numDevices = i + 1;
developerc14d83a2023-06-29 20:09:42 +080019323 wifi_debug(DEBUG_ERROR,, "mac_addr: %s\n", mac_addr);
developera3511852023-06-14 14:12:59 +080019324 addr_ptr = output_deviceMacAddressArray[i];
19325 mac_addr_aton(addr_ptr, mac_addr);
19326 mac_addr = strtok(NULL, "\n");
19327 }
developer72fb0bb2023-01-11 09:46:29 +080019328
developera3511852023-06-14 14:12:59 +080019329 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019330}
19331#else
19332INT wifi_getApAssociatedDevice(INT ap_index, CHAR *output_buf, INT output_buf_size)
19333{
developera3511852023-06-14 14:12:59 +080019334 char interface_name[16] = {0};
19335 char cmd[128];
19336 BOOL status = false;
developer75bd10c2023-06-27 11:34:08 +080019337 int res;
developer72fb0bb2023-01-11 09:46:29 +080019338
developera3511852023-06-14 14:12:59 +080019339 if(ap_index > MAX_APS || output_buf == NULL || output_buf_size <= 0)
19340 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019341
developera3511852023-06-14 14:12:59 +080019342 output_buf[0] = '\0';
developer72fb0bb2023-01-11 09:46:29 +080019343
developera3511852023-06-14 14:12:59 +080019344 wifi_getApEnable(ap_index,&status);
19345 if (!status)
19346 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019347
developera3511852023-06-14 14:12:59 +080019348 if (wifi_GetInterfaceName(ap_index, interface_name) != RETURN_OK)
19349 return RETURN_ERR;
developer75bd10c2023-06-27 11:34:08 +080019350 res = snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s list_sta | tr '\\n' ',' | sed 's/.$//'", interface_name);
19351 if (os_snprintf_error(sizeof(cmd), res)) {
19352 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19353 return RETURN_ERR;
19354 }
19355
developera3511852023-06-14 14:12:59 +080019356 _syscmd(cmd, output_buf, output_buf_size);
developer69b61b02023-03-07 17:17:44 +080019357
developera3511852023-06-14 14:12:59 +080019358 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019359}
19360#endif
19361
19362INT wifi_getProxyArp(INT apIndex, BOOL *enable)
19363{
developera3511852023-06-14 14:12:59 +080019364 char output[16]={'\0'};
19365 char config_file[MAX_BUF_SIZE] = {0};
developer75bd10c2023-06-27 11:34:08 +080019366 int res;
developer72fb0bb2023-01-11 09:46:29 +080019367
developera3511852023-06-14 14:12:59 +080019368 if (!enable)
19369 return RETURN_ERR;
developer72fb0bb2023-01-11 09:46:29 +080019370
developer75bd10c2023-06-27 11:34:08 +080019371 res = snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
19372 if (os_snprintf_error(sizeof(config_file), res)) {
19373 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19374 return RETURN_ERR;
19375 }
developera3511852023-06-14 14:12:59 +080019376 wifi_hostapdRead(config_file, "proxy_arp", output, sizeof(output));
developer72fb0bb2023-01-11 09:46:29 +080019377
developera3511852023-06-14 14:12:59 +080019378 if (strlen(output) == 0)
19379 *enable = FALSE;
19380 else if (strncmp(output, "1", 1) == 0)
19381 *enable = TRUE;
19382 else
19383 *enable = FALSE;
developer72fb0bb2023-01-11 09:46:29 +080019384
developera3511852023-06-14 14:12:59 +080019385 wifi_dbg_printf("\n[%s]: proxy_arp is : %s", __func__, output);
19386 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019387}
19388
19389INT wifi_getRadioStatsEnable(INT radioIndex, BOOL *output_enable)
19390{
developera3511852023-06-14 14:12:59 +080019391 if (NULL == output_enable || radioIndex >=MAX_NUM_RADIOS)
19392 return RETURN_ERR;
19393 *output_enable=TRUE;
19394 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019395}
19396
19397INT wifi_getTWTsessions(INT ap_index, UINT maxNumberSessions, wifi_twt_sessions_t *twtSessions, UINT *numSessionReturned)
19398{
developera3511852023-06-14 14:12:59 +080019399 char cmd[128] = {0};
19400 char buf[128] = {0};
19401 char line[128] = {0};
19402 FILE *f = NULL;
19403 int index = 0;
19404 int exp = 0;
19405 int mantissa = 0;
19406 int duration = 0;
19407 int radio_index = 0;
19408 int max_radio_num = 0;
19409 uint twt_wake_interval = 0;
19410 int phyId = 0;
developer75bd10c2023-06-27 11:34:08 +080019411 int res;
developerc14d83a2023-06-29 20:09:42 +080019412 unsigned long tmp_u, tmp_l;
developer75bd10c2023-06-27 11:34:08 +080019413
developera3511852023-06-14 14:12:59 +080019414 WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
developer72fb0bb2023-01-11 09:46:29 +080019415
developera3511852023-06-14 14:12:59 +080019416 wifi_getMaxRadioNumber(&max_radio_num);
developer9ce44382023-06-28 11:09:37 +080019417 if(max_radio_num == 0){
19418 return RETURN_ERR;
19419 }
developera3511852023-06-14 14:12:59 +080019420 radio_index = ap_index % max_radio_num;
developer72fb0bb2023-01-11 09:46:29 +080019421
developera3511852023-06-14 14:12:59 +080019422 phyId = radio_index_to_phy(radio_index);
developer75bd10c2023-06-27 11:34:08 +080019423
19424 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | wc -l", phyId);
19425 if (os_snprintf_error(sizeof(cmd), res)) {
19426 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19427 return RETURN_ERR;
19428 }
developera3511852023-06-14 14:12:59 +080019429 _syscmd(cmd, buf, sizeof(buf));
developerc14d83a2023-06-29 20:09:42 +080019430 if (hal_strtoul(buf, 10, &tmp_u) < 0) {
19431 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019432 }
19433 *numSessionReturned = tmp_u - 1;
19434
developera3511852023-06-14 14:12:59 +080019435 if (*numSessionReturned > maxNumberSessions)
19436 *numSessionReturned = maxNumberSessions;
19437 else if (*numSessionReturned < 1) {
19438 *numSessionReturned = 0;
19439 return RETURN_OK;
19440 }
developer72fb0bb2023-01-11 09:46:29 +080019441
developer75bd10c2023-06-27 11:34:08 +080019442 res = snprintf(cmd, sizeof(cmd), "cat /sys/kernel/debug/ieee80211/phy%d/mt76/twt_stats | tail -n %d | tr '|' ' ' | tr -s ' '", phyId, *numSessionReturned);
19443 if (os_snprintf_error(sizeof(cmd), res)) {
19444 wifi_debug(DEBUG_ERROR, "Unexpected snprintf fail\n");
19445 return RETURN_ERR;
19446 }
developera3511852023-06-14 14:12:59 +080019447 if ((f = popen(cmd, "r")) == NULL) {
19448 wifi_dbg_printf("%s: popen %s error\n", __func__, cmd);
19449 return RETURN_ERR;
19450 }
developer72fb0bb2023-01-11 09:46:29 +080019451
developera3511852023-06-14 14:12:59 +080019452 // the format of each line is "[wcid] [id] [flags] [exp] [mantissa] [duration] [tsf]"
19453 while((fgets(line, sizeof(line), f)) != NULL) {
19454 char *tmp = NULL;
developer9ce44382023-06-28 11:09:37 +080019455 size_t len = strlen(line);
19456 strncpy(buf, line,len);
19457 buf[len] = '\0';
developera3511852023-06-14 14:12:59 +080019458 tmp = strtok(buf, " ");
developerd14dff12023-06-28 22:47:44 +080019459 if (tmp == NULL)
19460 break;
developerc14d83a2023-06-29 20:09:42 +080019461
19462 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19463 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19464 }
19465 twtSessions[index].numDevicesInSession = tmp_u;
developera3511852023-06-14 14:12:59 +080019466 tmp = strtok(NULL, " ");
developerd14dff12023-06-28 22:47:44 +080019467 if (tmp == NULL)
19468 break;
developerc14d83a2023-06-29 20:09:42 +080019469
19470 if (hal_strtoul(tmp, 10, &tmp_u) < 0) {
19471 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19472 }
19473 twtSessions[index].twtParameters.operation.flowID = tmp_u;
developera3511852023-06-14 14:12:59 +080019474 tmp = strtok(NULL, " ");
19475 if (strstr(tmp, "t")) {
19476 twtSessions[index].twtParameters.operation.trigger_enabled = TRUE;
19477 }
19478 if (strstr(tmp, "a")) {
19479 twtSessions[index].twtParameters.operation.announced = TRUE;
19480 }
19481 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019482 if (tmp == NULL)
19483 continue;
developerc14d83a2023-06-29 20:09:42 +080019484
19485 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19486 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
19487 }
19488 exp = tmp_l;
19489
developera3511852023-06-14 14:12:59 +080019490 tmp = strtok(NULL, " ");
developer37646972023-06-29 10:58:43 +080019491 if (tmp == NULL)
19492 continue;
developerc14d83a2023-06-29 20:09:42 +080019493 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19494 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019495 }
19496 mantissa = tmp_l;
19497
developera3511852023-06-14 14:12:59 +080019498 tmp = strtok(NULL, " ");
developerc14d83a2023-06-29 20:09:42 +080019499
19500 if (hal_strtoul(tmp, 10, &tmp_l) < 0) {
19501 wifi_debug(DEBUG_ERROR, "Unexpected strtoul fail\n");
developerc14d83a2023-06-29 20:09:42 +080019502 }
19503 duration = tmp_l;
developer72fb0bb2023-01-11 09:46:29 +080019504
developera3511852023-06-14 14:12:59 +080019505 // only implicit supported
19506 twtSessions[index].twtParameters.operation.implicit = TRUE;
19507 // only individual agreement supported
19508 twtSessions[index].twtParameters.agreement = wifi_twt_agreement_type_individual;
developer72fb0bb2023-01-11 09:46:29 +080019509
developera3511852023-06-14 14:12:59 +080019510 // wakeInterval_uSec is a unsigned integer, but the maximum TWT wake interval could be 2^15 (mantissa) * 2^32 = 2^47.
19511 twt_wake_interval = mantissa * (1 << exp);
19512 if (mantissa == 0 || twt_wake_interval/mantissa != (1 << exp)) {
19513 // Overflow handling
19514 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = -1; // max unsigned int
19515 } else {
19516 twtSessions[index].twtParameters.params.individual.wakeInterval_uSec = twt_wake_interval;
19517 }
19518 twtSessions[index].twtParameters.params.individual.minWakeDuration_uSec = duration * 256;
19519 index++;
19520 }
developer72fb0bb2023-01-11 09:46:29 +080019521
developera3511852023-06-14 14:12:59 +080019522 pclose(f);
19523 WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
19524 return RETURN_OK;
developer72fb0bb2023-01-11 09:46:29 +080019525}
developercc5cbfb2023-06-13 18:29:52 +080019526
19527INT wifi_enableGreylistAccessControl(BOOL enable)
19528{
19529 char inf_name[IFNAMSIZ] = {0};
19530 int if_idx, ret = 0;
19531 struct nl_msg *msg = NULL;
19532 struct nlattr * msg_data = NULL;
19533 struct mtk_nl80211_param param;
19534 struct unl unl_ins;
19535 unsigned short apIndex = 0;
19536
19537 for (apIndex = 0; apIndex < MAX_APS; apIndex++) {
19538 if (wifi_GetInterfaceName(apIndex, inf_name) != RETURN_OK)
19539 continue;
19540
19541 if_idx = if_nametoindex(inf_name);
19542 if (!if_idx) {
19543 wifi_debug(DEBUG_ERROR, "can't finde ifname(%s) index,ERROR\n", inf_name);
19544 continue;
19545 }
19546
19547 /*init mtk nl80211 vendor cmd*/
19548 param.sub_cmd = MTK_NL80211_VENDOR_SUBCMD_SET_ACL;
19549 param.if_type = NL80211_ATTR_IFINDEX;
19550 param.if_idx = if_idx;
19551 ret = mtk_nl80211_init(&unl_ins, &msg, &msg_data, &param);
19552 if (ret) {
19553 wifi_debug(DEBUG_ERROR, "init mtk 80211 netlink and msg fails\n");
19554 return RETURN_ERR;
19555 }
19556
19557 if (nla_put_u8(msg, MTK_NL80211_VENDOR_ATTR_ACL_POLICY, enable == FALSE ? 0 : 1)) {
19558 wifi_debug(DEBUG_ERROR, "Nla put attribute error\n");
19559 nlmsg_free(msg);
19560 mtk_nl80211_deint(&unl_ins);
19561 continue;
19562 }
19563
19564 /*send mtk nl80211 vendor msg*/
19565 ret = mtk_nl80211_send(&unl_ins, msg, msg_data, NULL, NULL);
19566 if (ret) {
19567 wifi_debug(DEBUG_ERROR, "send mtk nl80211 vender msg fails\n");
19568 mtk_nl80211_deint(&unl_ins);
19569 continue;
19570 }
19571 /*deinit mtk nl80211 vendor msg*/
19572 mtk_nl80211_deint(&unl_ins);
19573 wifi_debug(DEBUG_NOTICE, " %s cmd success.\n", inf_name);
19574 }
19575
19576 return RETURN_OK;
19577}