Skip to content

Commit 1244082

Browse files
committed
feat(eppp): Use wifi-netif over PPP channels
1 parent 125492e commit 1244082

File tree

2 files changed

+208
-34
lines changed

2 files changed

+208
-34
lines changed

components/eppp_link/examples/host/main/app_main.c

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,48 @@ static void mqtt_app_start(void)
9797
#endif // MQTT
9898

9999
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
100+
#define HELLO_DONE BIT0
101+
#define WIFI_MAC BIT1
102+
#define CONNECT BIT2
103+
100104
typedef enum {
101105
UNKNOWN,
102-
INIT,
103-
CONNECTED,
104-
DISCONNECTED,
106+
HELLO,
107+
START,
105108
ERROR,
106109
} state_t;
107110
static state_t s_state = UNKNOWN;
108-
static eppp_channel_fn_t s_transmit[CHANNEL_ID_MAX] = {NULL};
111+
static esp_netif_t *s_wifi_netif = NULL;
112+
static EventGroupHandle_t s_events;
113+
static eppp_channel_fn_t s_transmit = {NULL};
109114
static esp_err_t recv(esp_netif_t *netif, int nr, void *buffer, size_t len)
110115
{
111-
char data[32];
112-
if (len < sizeof(data)) {
113-
memcpy(data, buffer, len);
114-
printf("Received [%d] %*s\n", (int)len, (int)len, data);
115-
return ESP_OK;
116+
if (nr == 1) {
117+
printf("Received channel=%d len=%d %.*s\n", nr, (int)len, (int)len, (char *)buffer);
118+
if (s_state == HELLO) {
119+
const char hello[] = "Hello client";
120+
const char mac[] = "MAC: ";
121+
size_t mac_len = 5 /* MAC: */ + 6 * 2 /* 6 bytes per char */ + 5 /* : */ + 1 /* \0 */;
122+
if (len == sizeof(hello) && memcmp(buffer, hello, len) == 0) {
123+
xEventGroupSetBits(s_events, HELLO_DONE);
124+
} else if (len == mac_len && memcmp(buffer, mac, 5) == 0) {
125+
uint8_t mac_addr[6];
126+
sscanf((char *)buffer + 5, "%2" PRIx8 ":%2" PRIx8 ":%2" PRIx8 ":%2" PRIx8 ":%2" PRIx8 ":%2" PRIx8,
127+
&mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
128+
printf("Parsed MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
129+
esp_netif_set_mac(s_wifi_netif, mac_addr);
130+
xEventGroupSetBits(s_events, WIFI_MAC);
131+
}
132+
} else if (s_state == START) {
133+
const char connected[] = "Connected";
134+
if (len == sizeof(connected) && memcmp(buffer, connected, len) == 0) {
135+
xEventGroupSetBits(s_events, CONNECT);
136+
}
137+
}
138+
} else if (nr == 2) {
139+
printf("Received WIFI channel=%d len=%d\n", nr, (int)len);
140+
// ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_INFO);
141+
return esp_wifi_remote_channel_rx(NULL, buffer, NULL, len);
116142
}
117143
return ESP_OK;
118144
}
@@ -121,14 +147,67 @@ static void task_transmit(void *arg)
121147
{
122148
int counter = 0;
123149
esp_netif_t *eppp_netif = (esp_netif_t *)arg;
150+
while (1) {
151+
EventBits_t bits = xEventGroupWaitBits(s_events,
152+
HELLO_DONE | WIFI_MAC | CONNECT,
153+
pdTRUE,
154+
pdFALSE,
155+
pdMS_TO_TICKS(1000));
156+
// if (bits & HELLO_SERVER) {
157+
// const char hello[] = "Hello server";
158+
// s_transmit(eppp_netif, 1, hello, sizeof(hello));
159+
// } else
160+
if (bits & HELLO_DONE) {
161+
ESP_LOGI(TAG, "Hello done");
162+
const char command[] = "Get MAC";
163+
s_transmit(eppp_netif, 1, (void*)command, sizeof(command));
164+
// s_state = CONNECTED;
165+
} else if (bits & WIFI_MAC) {
166+
s_state = START;
167+
ESP_LOGI(TAG, "Starting WIFI");
168+
esp_netif_action_start(s_wifi_netif, 0, 0, 0);
169+
} else if (bits & CONNECT) {
170+
ESP_LOGI(TAG, "WIFI connected");
171+
esp_netif_action_connected(s_wifi_netif, 0, 0, 0);
172+
} else if (s_state != START) {
173+
const char hello[] = "Hello server";
174+
s_transmit(eppp_netif, 1, (void*)hello, sizeof(hello));
175+
s_state = HELLO;
176+
}
177+
}
178+
124179
while (1) {
180+
125181
char data[32];
126182
sprintf(data, "Message from host #%d", counter++);
127183
s_transmit(eppp_netif, 1, data, strlen(data));
128184
vTaskDelay(1000 / portTICK_PERIOD_MS);
129185
}
130186
}
131-
#endif
187+
188+
esp_netif_t* esp_wifi_remote_create_default_sta(void);
189+
190+
// move to netif2
191+
ESP_EVENT_DEFINE_BASE(WIFI_REMOTE_EVENT);
192+
193+
static esp_err_t wifi_remote_tx(void *h, void *buffer, size_t len)
194+
{
195+
esp_netif_t *eppp_netif = (esp_netif_t *)h;
196+
// ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_INFO);
197+
return s_transmit(eppp_netif, 2, buffer, len);
198+
}
199+
200+
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
201+
{
202+
ESP_LOGI(TAG, "IP event_handler: event_base=%s event_id=%d", event_base, event_id);
203+
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
204+
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
205+
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
206+
xEventGroupSetBits(s_events, CONNECT);
207+
}
208+
}
209+
210+
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
132211

133212
void app_main(void)
134213
{
@@ -198,11 +277,15 @@ void app_main(void)
198277
ESP_ERROR_CHECK(console_cmd_start());
199278

200279
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
280+
ESP_RETURN_ON_FALSE(s_events = xEventGroupCreate(),, TAG, "Failed to create event group");
201281
ESP_RETURN_ON_FALSE(eppp_add_channels(eppp_netif, 1, &s_transmit, recv) == ESP_OK,, TAG, "Failed to add channels");
202282
ESP_RETURN_ON_FALSE(s_transmit,, TAG, "Channel tx function is not set");
203283
xTaskCreate(task_transmit, "task_transmit", 2048, eppp_netif, 5, NULL);
284+
s_wifi_netif = esp_wifi_remote_create_default_sta();
285+
esp_wifi_remote_channel_set(WIFI_IF_STA, eppp_netif, wifi_remote_tx);
286+
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL);
204287
#endif
205288
#if CONFIG_EXAMPLE_MQTT
206-
mqtt_app_start();
289+
// mqtt_app_start();
207290
#endif
208291
}

components/eppp_link/examples/slave/main/eppp_slave.c

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "esp_check.h"
1515
#include "nvs_flash.h"
1616
#include "eppp_link.h"
17+
#include "esp_private/wifi.h"
1718

1819
static const char *TAG = "eppp_slave";
1920

@@ -27,14 +28,20 @@ static EventGroupHandle_t s_wifi_event_group;
2728
* - we failed to connect after the maximum amount of retries */
2829
#define WIFI_CONNECTED_BIT BIT0
2930
#define WIFI_FAIL_BIT BIT1
31+
//---------------------------------------
32+
#define WIFI_CONNECTED BIT2
3033

3134

3235
static int s_retry_num = 0;
36+
static EventGroupHandle_t s_events;
3337

3438
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
3539
{
40+
ESP_LOGI(TAG, "event_handler: event_base=%s event_id=%d", event_base, event_id);
3641
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
42+
ESP_LOGI(TAG, "WIFI start event");
3743
esp_wifi_connect();
44+
xEventGroupSetBits(s_events, WIFI_CONNECTED);
3845
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
3946
if (s_retry_num < CONFIG_ESP_MAXIMUM_RETRY) {
4047
esp_wifi_connect();
@@ -56,7 +63,7 @@ void init_network_interface(void)
5663
{
5764
s_wifi_event_group = xEventGroupCreate();
5865

59-
esp_netif_create_default_wifi_sta();
66+
// esp_netif_create_default_wifi_sta();
6067

6168
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
6269
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
@@ -82,9 +89,10 @@ void init_network_interface(void)
8289
};
8390
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
8491
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
85-
ESP_ERROR_CHECK(esp_wifi_start());
92+
// ESP_ERROR_CHECK(esp_wifi_start());
8693

8794
ESP_LOGI(TAG, "wifi_init_sta finished.");
95+
return;
8896

8997
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
9098
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
@@ -116,23 +124,111 @@ void init_network_interface(void)
116124
#endif // SoC WiFi capable chip
117125

118126
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
127+
#define HELLO_RECEIVED BIT0
128+
#define MAC_REQUEST BIT1
129+
130+
typedef enum {
131+
UNKNOWN,
132+
HELLO,
133+
START,
134+
CONNECTED,
135+
DISCONNECTED,
136+
ERROR,
137+
} state_t;
138+
static state_t s_state = UNKNOWN;
139+
static eppp_channel_fn_t s_transmit = {NULL};
140+
static esp_netif_t *s_eppp_netif = NULL;
141+
119142
static esp_err_t recv(esp_netif_t *netif, int nr, void *buffer, size_t len)
120143
{
121-
char data[32];
122-
if (len < sizeof(data)) {
123-
memcpy(data, buffer, len);
124-
printf("Received [%d] %*s\n", (int)len, (int)len, data);
125-
return ESP_OK;
144+
if (nr == 1) {
145+
printf("Received channel=%d len=%d %.*s\n", nr, (int)len, (int)len, (char *)buffer);
146+
if (s_state == HELLO) {
147+
const char hello[] = "Hello server";
148+
const char mac[] = "Get MAC";
149+
if (len == sizeof(hello) && memcmp(buffer, hello, len) == 0) {
150+
xEventGroupSetBits(s_events, HELLO_RECEIVED);
151+
} else if (len == sizeof(mac) && memcmp(buffer, mac, sizeof(mac)) == 0) {
152+
xEventGroupSetBits(s_events, MAC_REQUEST);
153+
}
154+
}
155+
} else if (nr == 2) {
156+
printf("Received WIFI channel=%d len=%d\n", nr, (int)len);
157+
return esp_wifi_internal_tx(WIFI_IF_STA, buffer, len);
158+
}
159+
return ESP_OK;
160+
}
161+
162+
163+
static esp_err_t wifi_receive(void *buffer, uint16_t len, void *eb)
164+
{
165+
s_transmit(s_eppp_netif, 2, buffer, len);
166+
esp_wifi_internal_free_rx_buffer(eb);
167+
return ESP_OK;
168+
}
169+
170+
171+
static void task_transmit(void *arg)
172+
{
173+
int counter = 0;
174+
esp_netif_t *eppp_netif = (esp_netif_t *)arg;
175+
while (1) {
176+
EventBits_t bits = xEventGroupWaitBits(s_events,
177+
HELLO_RECEIVED | MAC_REQUEST | WIFI_CONNECTED,
178+
pdTRUE,
179+
pdFALSE,
180+
pdMS_TO_TICKS(1000));
181+
// if (bits & HELLO_SERVER) {
182+
// const char hello[] = "Hello server";
183+
// s_transmit(eppp_netif, 1, hello, sizeof(hello));
184+
// } else
185+
if (bits & HELLO_RECEIVED) {
186+
const char hello[] = "Hello client";
187+
s_transmit(eppp_netif, 1, (void*)hello, sizeof(hello));
188+
// s_state = CONNECTED;
189+
} else if (bits & MAC_REQUEST) {
190+
uint8_t mac[6];
191+
esp_err_t ret;
192+
193+
if ((ret = esp_wifi_get_mac(WIFI_IF_STA, mac)) != ESP_OK) {
194+
ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret);
195+
s_state = ERROR;
196+
continue;
197+
}
198+
char mac_data[5 /* MAC: */ + 6 * 2 /* 6 bytes per char */ + 5 /* : */ + 1 /* \0 */];
199+
sprintf(mac_data, "MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
200+
ESP_LOGI(TAG, "Sending MAC: %.*s", (int)sizeof(mac_data), mac_data);
201+
s_transmit(eppp_netif, 1, (void*)mac_data, sizeof(mac_data));
202+
s_state = START;
203+
ret = esp_wifi_start();
204+
ESP_LOGI(TAG, "WIFI start result: %d", ret);
205+
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_receive);
206+
} else if (bits & WIFI_CONNECTED) {
207+
ESP_LOGI(TAG, "WIFI connected");
208+
const char connected[] = "Connected";
209+
s_transmit(eppp_netif, 1, (void*)connected, sizeof(connected));
210+
} else {
211+
s_state = HELLO;
212+
printf("Do nothing\n");
213+
// const char hello[] = "Hello server";
214+
// s_transmit(eppp_netif, 1, (void*)hello, sizeof(hello));
215+
// s_state = HELLO;
216+
}
217+
}
218+
219+
while (1) {
220+
221+
char data[32];
222+
sprintf(data, "Message from host #%d", counter++);
223+
s_transmit(eppp_netif, 1, data, strlen(data));
224+
vTaskDelay(1000 / portTICK_PERIOD_MS);
126225
}
127-
return ESP_ERR_INVALID_SIZE;
128226
}
227+
129228
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
130229

131230
void app_main(void)
132231
{
133-
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
134-
static eppp_channel_fn_t transmit = NULL;
135-
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
136232
//Initialize NVS
137233
esp_err_t ret = nvs_flash_init();
138234
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@@ -165,21 +261,16 @@ void app_main(void)
165261
config.transport = EPPP_TRANSPORT_SDIO;
166262
#endif // transport device
167263

168-
esp_netif_t *eppp_netif = eppp_listen(&config);
169-
if (eppp_netif == NULL) {
264+
s_eppp_netif = eppp_listen(&config);
265+
if (s_eppp_netif == NULL) {
170266
ESP_LOGE(TAG, "Failed to setup connection");
171267
return ;
172268
}
173-
ESP_ERROR_CHECK(esp_netif_napt_enable(eppp_netif));
269+
// ESP_ERROR_CHECK(esp_netif_napt_enable(eppp_netif));
174270
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
175-
ESP_RETURN_ON_FALSE(eppp_add_channels(eppp_netif, 1, &transmit, recv) == ESP_OK,, TAG, "Failed to add channels");
176-
ESP_RETURN_ON_FALSE(transmit,, TAG, "Channel tx function is not set");
177-
int counter = 0;
178-
while (1) {
179-
char data[32];
180-
sprintf(data, "Message from slave #%d", counter++);
181-
transmit(eppp_netif, 1, data, strlen(data));
182-
vTaskDelay(1000 / portTICK_PERIOD_MS);
183-
}
271+
ESP_RETURN_ON_FALSE(s_events = xEventGroupCreate(),, TAG, "Failed to create event group");
272+
ESP_RETURN_ON_FALSE(eppp_add_channels(s_eppp_netif, 1, &s_transmit, recv) == ESP_OK,, TAG, "Failed to add channels");
273+
ESP_RETURN_ON_FALSE(s_transmit,, TAG, "Channel tx function is not set");
274+
xTaskCreate(task_transmit, "task_transmit", 2048, s_eppp_netif, 5, NULL);
184275
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
185276
}

0 commit comments

Comments
 (0)