Skip to content

Commit d0ace0d

Browse files
committed
feat(wifi_remote): Initial stubs for wifi-remote
1 parent a363bee commit d0ace0d

File tree

7 files changed

+280
-0
lines changed

7 files changed

+280
-0
lines changed

esp_wifi_remote/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if(NOT CONFIG_ESP_WIFI_ENABLED)
2+
set(src_wifi_is_remote esp_wifi_remote.c)
3+
endif()
4+
5+
idf_component_register(INCLUDE_DIRS include
6+
SRCS wifi_remote_rpc.c wifi_remote_net.c wifi_remote_init.c ${src_wifi_is_remote}
7+
REQUIRES esp_event esp_netif
8+
PRIV_REQUIRES esp_wifi esp_hosted)

esp_wifi_remote/esp_wifi_remote.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "esp_wifi.h"
8+
#include "esp_log.h"
9+
#include "esp_wifi_remote.h"
10+
11+
#define WEAK __attribute__((weak))
12+
13+
WEAK ESP_EVENT_DEFINE_BASE(WIFI_EVENT);
14+
15+
WEAK wifi_osi_funcs_t g_wifi_osi_funcs;
16+
WEAK const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs;
17+
WEAK uint64_t g_wifi_feature_caps;
18+
19+
WEAK esp_err_t esp_wifi_connect(void)
20+
{
21+
return remote_esp_wifi_connect();
22+
}
23+
24+
WEAK esp_err_t esp_wifi_init(const wifi_init_config_t *config)
25+
{
26+
return remote_esp_wifi_init(config);
27+
}
28+
29+
WEAK esp_err_t esp_wifi_set_mode(wifi_mode_t mode)
30+
{
31+
return remote_esp_wifi_set_mode(mode);
32+
}
33+
34+
WEAK esp_err_t esp_wifi_set_config(wifi_interface_t interface, wifi_config_t *conf)
35+
{
36+
return remote_esp_wifi_set_config(interface, conf);
37+
}
38+
39+
WEAK esp_err_t esp_wifi_start(void)
40+
{
41+
return remote_esp_wifi_start();
42+
}
43+
44+
WEAK esp_err_t esp_wifi_stop(void)
45+
{
46+
return remote_esp_wifi_stop();
47+
}
48+
49+
WEAK esp_err_t esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6])
50+
{
51+
return remote_esp_wifi_get_mac(ifx, mac);
52+
}

esp_wifi_remote/idf_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version: "1.0.0"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
#include "esp_wifi.h"
8+
9+
// Public API
10+
esp_err_t remote_esp_wifi_connect(void);
11+
esp_err_t remote_esp_wifi_init(const wifi_init_config_t *config);
12+
esp_err_t remote_esp_wifi_set_mode(wifi_mode_t mode);
13+
esp_err_t remote_esp_wifi_set_config(wifi_interface_t ifx, wifi_config_t *conf);
14+
esp_err_t remote_esp_wifi_start(void);
15+
esp_err_t remote_esp_wifi_stop(void);
16+
esp_err_t remote_esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6]);
17+
18+
19+
// TODO: Move this to private include
20+
// Private API
21+
esp_err_t remote_esp_wifi_init_slave(void);
22+
23+
// handling channels
24+
esp_err_t esp_wifi_remote_channel_rx(void *h, void *buffer, size_t len);
25+
esp_err_t esp_wifi_remote_channel_set(wifi_interface_t ifx, void *h, esp_err_t (*tx_cb)(void *, void *, size_t));
26+
esp_err_t esp_wifi_remote_rpc_channel_rx(void *h, void *buffer, size_t len);
27+
esp_err_t esp_wifi_remote_rpc_channel_set(void *h, esp_err_t (*tx_cb)(void *, void *, size_t));

esp_wifi_remote/wifi_remote_init.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "esp_err.h"
8+
#include <rpc_wrapper.h>
9+
#include "esp_wifi_remote.h"
10+
11+
esp_err_t remote_esp_wifi_init_slave(void)
12+
{
13+
if (esp_hosted_setup() != ESP_OK) {
14+
return ESP_FAIL;
15+
}
16+
esp_hosted_channel_fn_t tx_cb;
17+
esp_hosted_channel_t * ch;
18+
19+
// Add an RPC channel with default config (i.e. secure=true)
20+
esp_hosted_channel_config_t config = ESP_HOSTED_CHANNEL_CONFIG_DEFAULT();
21+
ch = esp_hosted_add_channel(&config, &tx_cb, esp_wifi_remote_rpc_channel_rx);
22+
esp_wifi_remote_rpc_channel_set(ch, tx_cb);
23+
24+
// Add two other channels for the two WiFi interfaces (STA, softAP) in plain text
25+
config.secure = false;
26+
ch = esp_hosted_add_channel(&config, &tx_cb, esp_wifi_remote_channel_rx); // TODO: add checks for `ch` and `tx_cb`
27+
esp_wifi_remote_channel_set(WIFI_IF_STA, ch, tx_cb);
28+
ch = esp_hosted_add_channel(&config, &tx_cb, esp_wifi_remote_channel_rx);
29+
esp_wifi_remote_channel_set(WIFI_IF_AP, ch, tx_cb);
30+
31+
return ESP_OK;
32+
}

esp_wifi_remote/wifi_remote_net.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <esp_private/wifi.h>
7+
#include "esp_err.h"
8+
#include <rpc_wrapper.h>
9+
10+
#define CHANNELS 2
11+
12+
static esp_hosted_channel_fn_t s_tx_cb[CHANNELS];
13+
static esp_hosted_channel_t *s_channel[CHANNELS];
14+
static wifi_rxcb_t s_rx_fn[CHANNELS];
15+
16+
esp_err_t esp_wifi_remote_channel_rx(void *h, void *buffer, size_t len)
17+
{
18+
if (h == s_channel[0]) {
19+
return s_rx_fn[0](buffer, len, buffer);
20+
}
21+
if (h == s_channel[1]) {
22+
return s_rx_fn[1](buffer, len, buffer);
23+
}
24+
return ESP_FAIL;
25+
}
26+
27+
esp_err_t esp_wifi_remote_channel_set(wifi_interface_t ifx, void *h, esp_err_t (*tx_cb)(void *, void *, size_t))
28+
{
29+
if (ifx == WIFI_IF_STA) {
30+
s_channel[0] = h;
31+
s_tx_cb[0] = tx_cb;
32+
return ESP_OK;
33+
}
34+
if (ifx == WIFI_IF_AP) {
35+
s_channel[1] = h;
36+
s_tx_cb[1] = tx_cb;
37+
return ESP_OK;
38+
}
39+
return ESP_FAIL;
40+
}
41+
42+
43+
esp_err_t esp_wifi_internal_set_sta_ip(void)
44+
{
45+
return ESP_OK;
46+
}
47+
48+
esp_err_t esp_wifi_internal_reg_netstack_buf_cb(wifi_netstack_buf_ref_cb_t ref, wifi_netstack_buf_free_cb_t free)
49+
{
50+
return ESP_OK;
51+
}
52+
53+
void esp_wifi_internal_free_rx_buffer(void* buffer)
54+
{
55+
// free(buffer);
56+
}
57+
58+
int esp_wifi_internal_tx(wifi_interface_t ifx, void *buffer, uint16_t len)
59+
{
60+
if (ifx == WIFI_IF_STA) {
61+
return s_tx_cb[0](s_channel[0], buffer, len);
62+
}
63+
if (ifx == WIFI_IF_AP) {
64+
return s_tx_cb[1](s_channel[1], buffer, len);
65+
}
66+
67+
return -1;
68+
}
69+
70+
esp_err_t esp_wifi_internal_reg_rxcb(wifi_interface_t ifx, wifi_rxcb_t fn)
71+
{
72+
if (ifx == WIFI_IF_STA) {
73+
s_rx_fn[0] = fn;
74+
return ESP_OK;
75+
}
76+
if (ifx == WIFI_IF_AP) {
77+
s_rx_fn[1] = fn;
78+
return ESP_OK;
79+
}
80+
81+
return ESP_FAIL;
82+
}

esp_wifi_remote/wifi_remote_rpc.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <string.h>
7+
#include "esp_log.h"
8+
#include "esp_err.h"
9+
#include "esp_wifi.h"
10+
#include "esp_wifi_remote.h"
11+
#include "esp_hosted_api.h"
12+
13+
static esp_hosted_channel_t *s_params_channel;
14+
static esp_hosted_channel_fn_t s_params_tx;
15+
static wifi_config_t s_last_wifi_conf;
16+
17+
esp_err_t esp_wifi_remote_rpc_channel_rx(void *h, void *buffer, size_t len)
18+
{
19+
if (h == s_params_channel && len == sizeof(s_last_wifi_conf)) {
20+
memcpy(&s_last_wifi_conf, buffer, len); // TODO: use queue
21+
return ESP_OK;
22+
}
23+
return ESP_FAIL;
24+
}
25+
26+
esp_err_t esp_wifi_remote_rpc_channel_set(void *h, esp_err_t (*tx_cb)(void *, void *, size_t))
27+
{
28+
s_params_channel =h;
29+
s_params_tx = tx_cb;
30+
return ESP_OK;
31+
}
32+
33+
esp_err_t remote_esp_wifi_connect(void)
34+
{
35+
return test_wifi_connect();
36+
}
37+
38+
esp_err_t remote_esp_wifi_init(const wifi_init_config_t *config)
39+
{
40+
if (remote_esp_wifi_init_slave() != ESP_OK) {
41+
return ESP_FAIL;
42+
}
43+
return test_wifi_init(config);
44+
}
45+
46+
esp_err_t remote_esp_wifi_set_mode(wifi_mode_t mode)
47+
{
48+
return test_wifi_set_mode(mode);
49+
}
50+
51+
esp_err_t remote_esp_wifi_set_config(wifi_interface_t interface, wifi_config_t *conf)
52+
{
53+
uint8_t *param = (uint8_t*)conf;
54+
uint32_t checksum = 0; // TODO: generate a random number and add it to both
55+
for (int i=0; i<sizeof(wifi_config_t); ++i)
56+
checksum += param[i];
57+
58+
// transmit the sensitive parameters over a secure channel
59+
// s_params_tx(s_params_channel, param, sizeof(wifi_config_t));
60+
61+
// add only a checksum to the RPC
62+
return test_wifi_set_config(interface, checksum);
63+
}
64+
65+
esp_err_t remote_esp_wifi_start(void)
66+
{
67+
return test_wifi_start();
68+
}
69+
70+
esp_err_t remote_esp_wifi_stop(void)
71+
{
72+
return test_wifi_stop();
73+
}
74+
75+
esp_err_t remote_esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6])
76+
{
77+
return test_wifi_get_mac_addr(ifx, mac);
78+
}

0 commit comments

Comments
 (0)