Skip to content

Commit d5b4888

Browse files
committed
Merge branch 'feature/tx_power' into 'master'
esp_hosted: Add support for set/get tx power See merge request app-frameworks/esp_hosted!290
2 parents 2755b0d + 9726a3d commit d5b4888

File tree

9 files changed

+282
-24
lines changed

9 files changed

+282
-24
lines changed

esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c

+6
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,12 @@ void process_priv_commamd(uint8_t if_type, uint8_t *payload, uint16_t payload_le
439439
process_set_mcast_mac_list(if_type, payload, payload_len);
440440
break;
441441

442+
case CMD_GET_TXPOWER:
443+
case CMD_SET_TXPOWER:
444+
ESP_LOGI(TAG, "Tx power command\n");
445+
process_tx_power(if_type, payload, payload_len, header->cmd_code);
446+
break;
447+
442448
default:
443449
ESP_LOGI(TAG, "Unsupported cmd[0x%x] received\n", header->cmd_code);
444450
break;

esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c

+50
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,56 @@ int process_set_ip(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
875875
return ret;
876876
}
877877

878+
int process_tx_power(uint8_t if_type, uint8_t *payload, uint16_t payload_len, uint8_t cmd)
879+
{
880+
interface_buffer_handle_t buf_handle = {0};
881+
esp_err_t ret = ESP_OK;
882+
struct cmd_set_get_val *val;
883+
int8_t max_tx_power;
884+
885+
if (cmd == CMD_SET_TXPOWER) {
886+
val = (struct cmd_set_get_val *)payload;
887+
max_tx_power = val->value;
888+
esp_wifi_set_max_tx_power(max_tx_power);
889+
}
890+
891+
/*ESP_LOG_BUFFER_HEXDUMP("MAC Filter", (uint8_t *) &mac_list, sizeof(mac_list), ESP_LOG_INFO);*/
892+
893+
buf_handle.if_type = if_type;
894+
buf_handle.if_num = 0;
895+
buf_handle.payload_len = sizeof(struct cmd_set_get_val);
896+
buf_handle.pkt_type = PACKET_TYPE_COMMAND_RESPONSE;
897+
898+
buf_handle.payload = heap_caps_malloc(buf_handle.payload_len, MALLOC_CAP_DMA);
899+
esp_wifi_get_max_tx_power(&max_tx_power);
900+
assert(buf_handle.payload);
901+
memset(buf_handle.payload, 0, buf_handle.payload_len);
902+
903+
val = (struct cmd_set_get_val *)(buf_handle.payload);
904+
val->value = max_tx_power;
905+
val->header.cmd_code = cmd;
906+
val->header.len = 0;
907+
val->header.cmd_status = CMD_RESPONSE_SUCCESS;
908+
909+
buf_handle.priv_buffer_handle = buf_handle.payload;
910+
buf_handle.free_buf_handle = free;
911+
912+
ret = send_command_response(&buf_handle);
913+
if (ret != pdTRUE) {
914+
ESP_LOGE(TAG, "Slave -> Host: Failed to send command response\n");
915+
goto DONE;
916+
}
917+
918+
return ESP_OK;
919+
920+
DONE:
921+
if (buf_handle.payload)
922+
free(buf_handle.payload);
923+
924+
return ret;
925+
}
926+
927+
878928
int process_sta_disconnect(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
879929
{
880930
struct command_header *header;

esp_hosted_ng/esp/esp_driver/network_adapter/main/include/adapter.h

+31-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#ifndef __ESP_NETWORK_ADAPTER__H
55
#define __ESP_NETWORK_ADAPTER__H
66

7+
#include <sys/cdefs.h>
8+
79
#define PRIO_Q_HIGH 0
810
#define PRIO_Q_MID 1
911
#define PRIO_Q_LOW 2
@@ -37,7 +39,7 @@ struct esp_payload_header {
3739
uint8_t priv_pkt_type; /* Packet type for priv interface */
3840
};
3941
/* Do no add anything here */
40-
} __attribute__((packed));
42+
} __packed;
4143

4244
struct ieee_mgmt_header {
4345
uint16_t frame_control;
@@ -46,9 +48,9 @@ struct ieee_mgmt_header {
4648
uint8_t sa[MAC_ADDR_LEN];
4749
uint8_t bssid[MAC_ADDR_LEN];
4850
uint16_t seq_ctrl;
49-
}__attribute__((packed));
51+
} __packed;
5052

51-
enum ESP_INTERFACE_TYPE{
53+
enum ESP_INTERFACE_TYPE {
5254
ESP_STA_IF,
5355
ESP_AP_IF,
5456
ESP_HCI_IF,
@@ -116,6 +118,8 @@ enum COMMAND_CODE {
116118
CMD_STA_ASSOC,
117119
CMD_SET_IP_ADDR,
118120
CMD_SET_MCAST_MAC_ADDR,
121+
CMD_GET_TXPOWER,
122+
CMD_SET_TXPOWER,
119123
CMD_MAX,
120124
};
121125

@@ -152,13 +156,13 @@ struct scan_request {
152156
char ssid[MAX_SSID_LEN+1];
153157
uint8_t channel;
154158
uint8_t pad[2];
155-
}__attribute__((packed));
159+
} __packed;
156160

157161
struct cmd_config_mac_address {
158162
struct command_header header;
159163
uint8_t mac_addr[MAC_ADDR_LEN];
160164
uint8_t pad[2];
161-
}__attribute__((packed));
165+
} __packed;
162166

163167
struct cmd_sta_auth {
164168
struct command_header header;
@@ -169,14 +173,14 @@ struct cmd_sta_auth {
169173
uint8_t auth_data_len;
170174
uint8_t pad[2];
171175
uint8_t auth_data[];
172-
}__attribute__((packed));
176+
} __packed;
173177

174178
struct cmd_sta_assoc {
175179
struct command_header header;
176180
uint8_t assoc_ie_len;
177181
uint8_t pad[3];
178182
uint8_t assoc_ie[];
179-
}__attribute__((packed));
183+
} __packed;
180184

181185
struct cmd_sta_connect {
182186
struct command_header header;
@@ -187,24 +191,24 @@ struct cmd_sta_connect {
187191
uint8_t is_auth_open;
188192
uint8_t assoc_ie_len;
189193
uint8_t assoc_ie[];
190-
}__attribute__((packed));
194+
} __packed;
191195

192196
struct cmd_sta_disconnect {
193197
struct command_header header;
194198
uint16_t reason_code;
195199
uint8_t pad[2];
196-
}__attribute__((packed));
200+
} __packed;
197201

198202
struct cmd_set_ip_addr {
199203
struct command_header header;
200204
uint32_t ip;
201-
}__attribute__((packed));
205+
} __packed;
202206

203207
struct cmd_set_mcast_mac_addr {
204208
struct command_header header;
205209
uint8_t count;
206210
uint8_t mcast_addr[MAX_MULTICAST_ADDR_COUNT][MAC_ADDR_LEN];
207-
}__attribute__((packed));
211+
} __packed;
208212

209213
struct wifi_sec_key {
210214
uint32_t algo;
@@ -217,18 +221,23 @@ struct wifi_sec_key {
217221
uint8_t del;
218222
uint8_t set_cur;
219223
uint8_t pad[2];
220-
}__attribute__((packed));
224+
} __packed;
225+
226+
struct cmd_set_get_val {
227+
struct command_header header;
228+
uint32_t value;
229+
} __packed;
221230

222231
struct cmd_key_operation {
223232
struct command_header header;
224233
struct wifi_sec_key key;
225-
}__attribute__((packed));
234+
} __packed;
226235

227236
struct event_header {
228237
uint8_t event_code;
229238
uint8_t status;
230239
uint16_t len;
231-
}__attribute__((packed));
240+
} __packed;
232241

233242
struct scan_event {
234243
struct event_header header;
@@ -240,7 +249,7 @@ struct scan_event {
240249
uint16_t frame_len;
241250
uint8_t pad[2];
242251
uint8_t frame[0];
243-
}__attribute__((packed));
252+
} __packed;
244253

245254
struct auth_event {
246255
struct event_header header;
@@ -252,7 +261,7 @@ struct auth_event {
252261
uint16_t frame_len;
253262
uint8_t pad[2];
254263
uint8_t frame[0];
255-
}__attribute__((packed));
264+
} __packed;
256265

257266
struct assoc_event {
258267
struct event_header header;
@@ -265,32 +274,32 @@ struct assoc_event {
265274
uint32_t rssi;
266275
uint64_t tsf;
267276
uint8_t frame[0];
268-
}__attribute__((packed));
277+
} __packed;
269278

270279
struct disconnect_event {
271280
struct event_header header;
272281
uint8_t bssid[MAC_ADDR_LEN];
273282
char ssid[MAX_SSID_LEN+1];
274283
uint8_t reason;
275-
}__attribute__((packed));
284+
} __packed;
276285

277286
struct esp_internal_bootup_event {
278287
struct event_header header;
279288
uint8_t len;
280289
uint8_t pad[3];
281290
uint8_t data[0];
282-
}__attribute__((packed));
291+
} __packed;
283292

284293
struct fw_version {
285294
uint8_t major1;
286295
uint8_t major2;
287296
uint8_t minor;
288-
}__attribute__((packed));
297+
} __packed;
289298

290299
struct fw_data {
291300
struct fw_version version;
292301
uint32_t last_reset_reason;
293-
}__attribute__((packed));
302+
} __packed;
294303

295304

296305

@@ -299,7 +308,7 @@ static inline uint16_t compute_checksum(uint8_t *buf, uint16_t len)
299308
uint16_t checksum = 0;
300309
uint16_t i = 0;
301310

302-
while(i < len) {
311+
while (i < len) {
303312
checksum += buf[i];
304313
i++;
305314
}

esp_hosted_ng/esp/esp_driver/network_adapter/main/include/cmd.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int process_auth_request(uint8_t if_type, uint8_t *payload, uint16_t payload_len
4646
int process_assoc_request(uint8_t if_type, uint8_t *payload, uint16_t payload_len);
4747
int process_set_ip(uint8_t if_type, uint8_t *payload, uint16_t payload_len);
4848
int process_set_mcast_mac_list(uint8_t if_type, uint8_t *payload, uint16_t payload_len);
49+
int process_tx_power(uint8_t if_type, uint8_t *payload, uint16_t payload_len, uint8_t cmd_code);
4950
esp_err_t initialise_wifi(void);
5051

5152
inline esp_err_t send_command_response(interface_buffer_handle_t *buf_handle)

esp_hosted_ng/host/esp_cfg80211.c

+89
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,93 @@ static void esp_cfg80211_set_wakeup(struct wiphy *wiphy,
491491
/*esp_dbg("\n");*/
492492
}
493493

494+
//TODO get MAX_TAX_POWER from Firmware for future chips
495+
#define MAX_TAX_POWER (20 * 100)
496+
static bool is_txpwr_valid(int mbm)
497+
{
498+
if (mbm > MAX_TAX_POWER)
499+
return false;
500+
501+
return true;
502+
}
503+
504+
static int mbm_to_esp_pwr(int mbm)
505+
{
506+
return ((mbm * 4) / 100);
507+
}
508+
509+
static int esp_pwr_to_dbm(int power)
510+
{
511+
return ((power / 4));
512+
}
513+
514+
static int esp_cfg80211_set_tx_power(struct wiphy *wiphy,
515+
struct wireless_dev *wdev,
516+
enum nl80211_tx_power_setting type, int mbm)
517+
{
518+
struct esp_adapter *adapter = esp_get_adapter();
519+
struct esp_wifi_device *priv = NULL;
520+
521+
if (!wiphy || !adapter) {
522+
esp_info("%u invalid input %p %p \n", __LINE__, wiphy, wdev);
523+
return -EINVAL;
524+
}
525+
526+
esp_dbg("\n");
527+
528+
priv = adapter->priv[0];
529+
if (!priv) {
530+
esp_err("Empty priv\n");
531+
return 0;
532+
}
533+
534+
switch (type) {
535+
case NL80211_TX_POWER_AUTOMATIC:
536+
priv->tx_pwr_type = NL80211_TX_POWER_AUTOMATIC;
537+
priv->tx_pwr = mbm_to_esp_pwr(MAX_TAX_POWER);
538+
break;
539+
case NL80211_TX_POWER_LIMITED:
540+
if (!is_txpwr_valid(mbm)) {
541+
esp_warn("mbm:%d not support\n", mbm);
542+
return -EOPNOTSUPP;
543+
}
544+
priv->tx_pwr_type = NL80211_TX_POWER_LIMITED;
545+
priv->tx_pwr = mbm_to_esp_pwr(mbm);
546+
break;
547+
case NL80211_TX_POWER_FIXED:
548+
return -EOPNOTSUPP;
549+
break;
550+
default:
551+
esp_warn("unknown type:%d\n", type);
552+
}
553+
554+
return cmd_set_tx_power(priv, priv->tx_pwr);
555+
}
556+
557+
static int esp_cfg80211_get_tx_power(struct wiphy *wiphy,
558+
struct wireless_dev *wdev,
559+
int *dbm)
560+
{
561+
struct esp_wifi_device *priv = NULL;
562+
563+
if (!wiphy || !wdev || !dbm || !wdev->netdev) {
564+
esp_info("%u invalid input\n", __LINE__);
565+
return -EINVAL;
566+
}
567+
568+
esp_dbg("\n");
569+
priv = netdev_priv(wdev->netdev);
570+
571+
if (!priv) {
572+
esp_err("Empty priv\n");
573+
return -EINVAL;
574+
}
575+
576+
*dbm = esp_pwr_to_dbm(priv->tx_pwr);
577+
578+
return 0;
579+
}
580+
494581
static struct cfg80211_ops esp_cfg80211_ops = {
495582
#if 0
496583
.add_virtual_intf = esp_cfg80211_add_iface,
@@ -511,6 +598,8 @@ static struct cfg80211_ops esp_cfg80211_ops = {
511598
.suspend = esp_cfg80211_suspend,
512599
.resume = esp_cfg80211_resume,
513600
.set_wakeup = esp_cfg80211_set_wakeup,
601+
.set_tx_power = esp_cfg80211_set_tx_power,
602+
.get_tx_power = esp_cfg80211_get_tx_power,
514603
};
515604

516605
int esp_cfg80211_register(struct esp_adapter *adapter)

0 commit comments

Comments
 (0)