blob: 8f6d034370740ac6348ed79937d744edb3624c78 [file] [log] [blame]
developer71050452022-08-26 14:46:27 +08001From 976808df0118205583d30079ba87b7e25aeeaa6f Mon Sep 17 00:00:00 2001
2From: "Allen.Ye" <allen.ye@mediatek.com>
3Date: Thu, 25 Aug 2022 20:03:10 +0800
4Subject: [PATCH] HAL: Refactor get and set power functions
5
6---
7 source/wifi/wifi_hal.c | 92 +++++++++++++++++++++++++++++++++++-------
8 1 file changed, 77 insertions(+), 15 deletions(-)
9
10diff --git a/source/wifi/wifi_hal.c b/source/wifi/wifi_hal.c
11index 9be6a11..3aa1938 100644
12--- a/source/wifi/wifi_hal.c
13+++ b/source/wifi/wifi_hal.c
14@@ -2486,25 +2486,23 @@ INT wifi_getRadioTransmitPowerSupported(INT radioIndex, CHAR *output_list) //Tr1
15 return RETURN_OK;
16 }
17
18-//Get current Transmit Power, eg "75", "100"
19+//Get current Transmit Power in dBm units.
20 //The transmite power level is in units of full power for this radio.
21 INT wifi_getRadioTransmitPower(INT radioIndex, ULONG *output_ulong) //RDKB
22 {
23 char cmd[128]={0};
24- char buf[256]={0};
25- INT apIndex;
26- //save config and apply instantly
27+ char buf[16]={0};
28+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
29
30- if (NULL == output_ulong)
31+ if(output_ulong == NULL)
32 return RETURN_ERR;
33
34- //zqiu:TODO:save config
35- apIndex = (radioIndex==0) ?0 :1;
36-
37- snprintf(cmd, sizeof(cmd), "iwlist %s%d txpower | grep Tx-Power | cut -d'=' -f2", AP_PREFIX, apIndex);
38+ snprintf(cmd, sizeof(cmd), "iw %s%d info | grep txpower | awk '{print $2}' | cut -d '.' -f1 | tr -d '\\n'", AP_PREFIX, radioIndex);
39 _syscmd(cmd, buf, sizeof(buf));
40- *output_ulong = atol(buf);
41
42+ *output_ulong = strtol(buf, NULL, 10);
43+
44+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
45 return RETURN_OK;
46 }
47
48@@ -2512,12 +2510,38 @@ INT wifi_getRadioTransmitPower(INT radioIndex, ULONG *output_ulong) //RDKB
49 //The transmite power level is in units of full power for this radio.
50 INT wifi_setRadioTransmitPower(INT radioIndex, ULONG TransmitPower) //RDKB
51 {
52+ char *support;
53 char cmd[128]={0};
54- char buf[256]={0};
55- INT apIndex;
56+ char buf[128]={0};
57+ char txpower_str[64] = {0};
58+ int txpower = 0;
59+ int maximum_tx = 0;
60+
61+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
62
63- snprintf(cmd, sizeof(cmd), "iwconfig %s%d txpower %lu", AP_PREFIX, radioIndex, TransmitPower);
64+ snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s%d status | grep max_txpower | cut -d '=' -f2 | tr -d '\n'", AP_PREFIX, radioIndex);
65 _syscmd(cmd, buf, sizeof(buf));
66+ maximum_tx = strtol(buf, NULL, 10);
67+
68+ // Get the Tx power supported list and check that is the input in the list
69+ snprintf(txpower_str, sizeof(txpower_str), "%lu", TransmitPower);
70+ wifi_getRadioTransmitPowerSupported(radioIndex, buf);
71+ support = strtok(buf, ",");
72+ while(true)
73+ {
74+ if(support == NULL) { // input not in the list
75+ wifi_dbg_printf("Input value is invalid.\n");
76+ return RETURN_ERR;
77+ }
78+ if (strncmp(txpower_str, support, strlen(support)) == 0) {
79+ break;
80+ }
81+ support = strtok(NULL, ",");
82+ }
83+ txpower = TransmitPower*maximum_tx/100;
84+ snprintf(cmd, sizeof(cmd), "iw phy phy%d set txpower fixed %d00", radioIndex, txpower);
85+ _syscmd(cmd, buf, sizeof(buf));
86+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
87
88 return RETURN_OK;
89 }
90@@ -9586,8 +9610,46 @@ INT wifi_switchBand(char *interface_name,INT radioIndex,char *freqBand)
91
92 INT wifi_getRadioPercentageTransmitPower(INT apIndex, ULONG *txpwr_pcntg)
93 {
94- //TO-Do Implement this
95- txpwr_pcntg = 0;
96+ char cmd[128]={'\0'};
97+ char buf[128]={'\0'};
98+ char *support;
99+ int maximum_tx = 0, current_tx = 0;
100+
101+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
102+ if(txpwr_pcntg == NULL)
103+ return RETURN_ERR;
104+
105+ // Get the maximum tx power of the device
106+ snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s%d status | grep max_txpower | cut -d '=' -f2 | tr -d '\n'", AP_PREFIX, apIndex);
107+ _syscmd(cmd, buf, sizeof(buf));
108+ maximum_tx = strtol(buf, NULL, 10);
109+
110+ // Get the current tx power
111+ memset(cmd, 0, sizeof(cmd));
112+ memset(buf, 0, sizeof(buf));
113+ snprintf(cmd, sizeof(cmd), "iw %s%d info | grep txpower | awk '{print $2}' | cut -d '.' -f1 | tr -d '\\n'", AP_PREFIX, apIndex);
114+ _syscmd(cmd, buf, sizeof(buf));
115+ current_tx = strtol(buf, NULL, 10);
116+
117+ // Get the power supported list and find the current power percentage in supported list
118+ memset(buf, 0, sizeof(buf));
119+ wifi_getRadioTransmitPowerSupported(apIndex, buf);
120+ support = strtok(buf, ",");
121+ while(true)
122+ {
123+ if(support == NULL) { // current power is not in supported list, this should not happen if the power is set by hal.
124+ *txpwr_pcntg = 0;
125+ wifi_dbg_printf("current power is not in supported list\n");
126+ return RETURN_ERR;
127+ }
128+ int tmp = maximum_tx*strtol(support, NULL, 10)/100;
129+ if (tmp == current_tx) {
130+ *txpwr_pcntg = strtol(support, NULL, 10);
131+ break;
132+ }
133+ support = strtok(NULL, ",");
134+ }
135+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
136 return RETURN_OK;
137 }
138
139--
1402.18.0
141