feat(examples): Support simple PPP connect in multinet example

This commit is contained in:
David Cermak
2023-06-22 17:27:16 +02:00
parent 00d7c40848
commit 1c20328dcf
10 changed files with 302 additions and 114 deletions

View File

@ -1,2 +1,13 @@
idf_component_register(SRCS ethernet_netif.c multi_netif_main.c wifi_connect.c check_connection.c ppp_connect.c
set(ppp_connect_srcs ppp_connect.c)
if(CONFIG_EXAMPLE_PPP_CONNECT_ESP_MODEM)
list(APPEND ppp_connect_srcs ppp_connect_esp_modem.c)
else()
list(APPEND ppp_connect_srcs ppp_connect_simple.c)
endif()
idf_component_register(SRCS multi_netif_main.c
check_connection.c
wifi_connect.c
ethernet_connect.c
${ppp_connect_srcs}
INCLUDE_DIRS ".")

View File

@ -1,4 +1,4 @@
menu "Connection Configuration"
menu "Example Configuration"
config ESP_WIFI_SSID
string "WiFi SSID"
@ -24,19 +24,34 @@ menu "Connection Configuration"
help
Set APN (Access Point Name), a logical name to choose data network
config EXAMPLE_MODEM_UART_TX_PIN
config EXAMPLE_PPP_UART_TX_PIN
int "TXD Pin Number"
default 15
range 0 31
help
Pin number of UART TX.
config EXAMPLE_MODEM_UART_RX_PIN
config EXAMPLE_PPP_UART_RX_PIN
int "RXD Pin Number"
default 14
range 0 31
help
Pin number of UART RX.
choice EXAMPLE_PPP_CONNECT
prompt "Connect to PPP server"
default EXAMPLE_PPP_CONNECT_ESP_MODEM
help
Choose modem interface library.
We use esp_modem by default, but in some
simple cases (and a very constrained environment)
we could simply connect UART directly to lwIP.
To experiment with this option, choose EXAMPLE_PPP_CONNECT_SIMPLE
config EXAMPLE_PPP_CONNECT_ESP_MODEM
bool "Using esp_modem library"
config EXAMPLE_PPP_CONNECT_SIMPLE
bool "Using simple UART-PPP driver"
endchoice
endmenu

View File

@ -82,7 +82,7 @@ static void got_ip_event_handler(void *args, esp_event_base_t event_base,
}
static void teardown_eth(iface_info_t *info)
static void eth_destroy(iface_info_t *info)
{
struct eth_info_t *eth_info = __containerof(info, struct eth_info_t, parent);
@ -95,11 +95,11 @@ static void teardown_eth(iface_info_t *info)
free(eth_info);
}
iface_info_t *setup_eth(int prio)
iface_info_t *eth_init(int prio)
{
struct eth_info_t *eth_info = malloc(sizeof(struct eth_info_t));
assert(eth_info);
eth_info->parent.teardown = teardown_eth;
eth_info->parent.destroy = eth_destroy;
eth_info->parent.name = "Ethernet";
// Init common MAC and PHY configs to default

View File

@ -18,7 +18,7 @@
struct iface_info_t {
esp_netif_t *netif;
esp_netif_dns_info_t dns[2];
void (*teardown)(struct iface_info_t *);
void (*destroy)(struct iface_info_t *);
const char *name;
bool connected;
};

View File

@ -23,9 +23,9 @@
#include "nvs_flash.h"
#include "iface_info.h"
iface_info_t *setup_eth(int prio);
iface_info_t *setup_wifi(int prio);
iface_info_t *setup_ppp(int prio);
iface_info_t *eth_init(int prio);
iface_info_t *wifi_init(int prio);
iface_info_t *ppp_init(int prio);
esp_err_t check_connectivity(const char *host);
#define HOST "www.espressif.com"
@ -69,9 +69,9 @@ void app_main(void)
// all interfaces
iface_info_t *ifaces[] = {
setup_eth(ETH_PRIO),
setup_wifi(WIFI_PRIO),
setup_ppp(PPP_PRIO),
eth_init(ETH_PRIO),
wifi_init(WIFI_PRIO),
ppp_init(PPP_PRIO),
};
size_t num_of_ifaces = sizeof(ifaces) / sizeof(ifaces[0]);
@ -123,7 +123,7 @@ void app_main(void)
ESP_LOGI(TAG, "Stop and cleanup all interfaces");
for (int i = 0; i < num_of_ifaces; ++i) {
if (ifaces[i]) {
ifaces[i]->teardown(ifaces[i]);
ifaces[i]->destroy(ifaces[i]);
}
}
}

View File

@ -17,16 +17,10 @@
#include "esp_netif.h"
#include "esp_netif_ppp.h"
#include "mqtt_client.h"
#include "esp_modem_api.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "iface_info.h"
struct ppp_info_t {
iface_info_t parent;
esp_modem_dce_t *dce;
bool stop_task;
};
#include "ppp_connect.h"
static const int CONNECT_BIT = BIT0;
static const char *TAG = "pppos_connect";
@ -79,105 +73,23 @@ static void on_ip_event(void *arg, esp_event_base_t event_base,
}
static void teardown_ppp(iface_info_t *info)
static void ppp_destroy(iface_info_t *info)
{
struct ppp_info_t *ppp_info = __containerof(info, struct ppp_info_t, parent);
esp_netif_action_disconnected(ppp_info->parent.netif, 0, 0, 0);
esp_netif_action_stop(ppp_info->parent.netif, 0, 0, 0);
esp_err_t err = esp_modem_set_mode(ppp_info->dce, ESP_MODEM_MODE_COMMAND);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
return;
}
esp_modem_destroy(ppp_info->dce);
ppp_destroy_context(ppp_info);
vEventGroupDelete(event_group);
ppp_info->stop_task = true;
free(info);
}
static void ppp_task(void *args)
{
struct ppp_info_t *ppp_info = args;
int backoff_time = 15000;
const int max_backoff = 60000;
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
dte_config.uart_config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN;
dte_config.uart_config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN;
ppp_info->dce = esp_modem_new(&dte_config, &dce_config, ppp_info->parent.netif);
int rssi, ber;
esp_err_t ret = esp_modem_get_signal_quality(ppp_info->dce, &rssi, &ber);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with %d %s", ret, esp_err_to_name(ret));
goto failed;
}
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
ret = esp_modem_set_mode(ppp_info->dce, ESP_MODEM_MODE_DATA);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_DATA) failed with %d", ret);
goto failed;
}
failed:
#define CONTINUE_LATER() backoff_time *= 2; \
if (backoff_time > max_backoff) { backoff_time = max_backoff; } \
continue;
// now let's keep retrying
while (!ppp_info->stop_task) {
vTaskDelay(pdMS_TO_TICKS(backoff_time));
if (ppp_info->parent.connected) {
backoff_time = 5000;
continue;
}
// try if the modem got stuck in data mode
ESP_LOGI(TAG, "Trying to Sync with modem");
ret = esp_modem_sync(ppp_info->dce);
if (ret != ESP_OK) {
ESP_LOGI(TAG, "Switching to command mode");
esp_modem_set_mode(ppp_info->dce, ESP_MODEM_MODE_COMMAND);
ESP_LOGI(TAG, "Retry sync 3 times");
for (int i = 0; i < 3; ++i) {
ret = esp_modem_sync(ppp_info->dce);
if (ret == ESP_OK) {
break;
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
if (ret != ESP_OK) {
CONTINUE_LATER();
}
}
ESP_LOGI(TAG, "Manual hang-up before reconnecting");
ret = esp_modem_at(ppp_info->dce, "ATH", NULL, 2000);
if (ret != ESP_OK) {
CONTINUE_LATER();
}
ret = esp_modem_get_signal_quality(ppp_info->dce, &rssi, &ber);
if (ret != ESP_OK) {
CONTINUE_LATER();
}
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
ret = esp_modem_set_mode(ppp_info->dce, ESP_MODEM_MODE_DATA);
if (ret != ESP_OK) {
CONTINUE_LATER();
}
}
#undef CONTINUE_LATER
vTaskDelete(NULL);
}
iface_info_t *setup_ppp(int prio)
iface_info_t *init_ppp(int prio)
{
struct ppp_info_t *ppp_info = calloc(1, sizeof(struct ppp_info_t));
assert(ppp_info);
ppp_info->parent.teardown = teardown_ppp;
ppp_info->parent.destroy = ppp_destroy;
ppp_info->parent.name = "Modem";
event_group = xEventGroupCreate();
@ -187,6 +99,7 @@ iface_info_t *setup_ppp(int prio)
esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP();
base_netif_cfg.route_prio = prio;
esp_netif_config_t netif_ppp_config = { .base = &base_netif_cfg,
.driver = ppp_driver_cfg,
.stack = ESP_NETIF_NETSTACK_DEFAULT_PPP
};
@ -205,6 +118,6 @@ iface_info_t *setup_ppp(int prio)
err:
teardown_ppp(&ppp_info->parent);
ppp_destroy(&ppp_info->parent);
return NULL;
}

View File

@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
struct ppp_info_t {
iface_info_t parent;
void *context;
bool stop_task;
};
extern const esp_netif_driver_ifconfig_t *ppp_driver_cfg;
void ppp_task(void *args);
void ppp_destroy_context(struct ppp_info_t *ppp_info);

View File

@ -0,0 +1,109 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_netif.h"
#include "esp_netif_ppp.h"
#include "mqtt_client.h"
#include "esp_modem_api.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "iface_info.h"
#include "ppp_connect.h"
static const char *TAG = "ppp_esp_modem";
const esp_netif_driver_ifconfig_t *ppp_driver_cfg = NULL;
void ppp_task(void *args)
{
struct ppp_info_t *ppp_info = args;
int backoff_time = 15000;
const int max_backoff = 60000;
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
dte_config.uart_config.tx_io_num = CONFIG_EXAMPLE_PPP_UART_TX_PIN;
dte_config.uart_config.rx_io_num = CONFIG_EXAMPLE_PPP_UART_RX_PIN;
esp_modem_dce_t *dce = esp_modem_new(&dte_config, &dce_config, ppp_info->parent.netif);
ppp_info->context = dce;
int rssi, ber;
esp_err_t ret = esp_modem_get_signal_quality(dce, &rssi, &ber);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with %d %s", ret, esp_err_to_name(ret));
goto failed;
}
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
ret = esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_DATA) failed with %d", ret);
goto failed;
}
failed:
#define CONTINUE_LATER() backoff_time *= 2; \
if (backoff_time > max_backoff) { backoff_time = max_backoff; } \
continue;
// now let's keep retrying
while (!ppp_info->stop_task) {
vTaskDelay(pdMS_TO_TICKS(backoff_time));
if (ppp_info->parent.connected) {
backoff_time = 5000;
continue;
}
// try if the modem got stuck in data mode
ESP_LOGI(TAG, "Trying to Sync with modem");
ret = esp_modem_sync(dce);
if (ret != ESP_OK) {
ESP_LOGI(TAG, "Switching to command mode");
esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
ESP_LOGI(TAG, "Retry sync 3 times");
for (int i = 0; i < 3; ++i) {
ret = esp_modem_sync(dce);
if (ret == ESP_OK) {
break;
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
if (ret != ESP_OK) {
CONTINUE_LATER();
}
}
ESP_LOGI(TAG, "Manual hang-up before reconnecting");
ret = esp_modem_at(dce, "ATH", NULL, 2000);
if (ret != ESP_OK) {
CONTINUE_LATER();
}
ret = esp_modem_get_signal_quality(dce, &rssi, &ber);
if (ret != ESP_OK) {
CONTINUE_LATER();
}
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
ret = esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA);
if (ret != ESP_OK) {
CONTINUE_LATER();
}
}
#undef CONTINUE_LATER
vTaskDelete(NULL);
}
void ppp_destroy_context(struct ppp_info_t *ppp_info)
{
esp_modem_dce_t *dce = ppp_info->context;
esp_err_t err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
return;
}
esp_modem_destroy(dce);
}

View File

@ -0,0 +1,122 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "esp_netif.h"
#include "esp_log.h"
#include "esp_event.h"
#include "sdkconfig.h"
#include "iface_info.h"
#include "ppp_connect.h"
#include "driver/uart.h"
static const char *TAG = "ppp_connect_simple";
static esp_err_t transmit(void *h, void *buffer, size_t len)
{
ESP_LOG_BUFFER_HEXDUMP("ppp_connect_tx", buffer, len, ESP_LOG_VERBOSE);
uart_write_bytes(UART_NUM_1, buffer, len);
return ESP_OK;
}
static esp_netif_driver_ifconfig_t driver_cfg = {
.handle = (void *)1, // singleton driver, just to != NULL
.transmit = transmit,
};
const esp_netif_driver_ifconfig_t *ppp_driver_cfg = &driver_cfg;
#define BUF_SIZE (1024)
#define CONNECTED "CONNECT 115200"
void ppp_task(void *args)
{
struct ppp_info_t *ppp_info = args;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};
QueueHandle_t event_queue;
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, BUF_SIZE, 0, 16, &event_queue, 0));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, CONFIG_EXAMPLE_PPP_UART_TX_PIN, CONFIG_EXAMPLE_PPP_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_set_rx_timeout(UART_NUM_1, 1));
char *buffer = malloc(BUF_SIZE);
ppp_info->context = buffer;
const struct seq_t {
const char *cmd;
const char *expect;
bool allow_fail;
} init_sequence[] = {
{ .cmd = "AT\r\n", .expect = "OK" },
{ .cmd = "AT+CGDCONT=1,\"IP\",\"" CONFIG_EXAMPLE_MODEM_PPP_APN "\"\r\n", .expect = "OK" },
{ .cmd = "ATD*99##\r\n", .expect = "CONNECT", .allow_fail = true },
{ .cmd = "ATO\r\n", .expect = "CONNECT" },
};
int cmd_i = 0;
int retry = 0;
char *reply = buffer;
const int max_retries = 3;
uart_event_t event;
uart_write_bytes(UART_NUM_1, "+++", 3);
vTaskDelay(pdMS_TO_TICKS(1000));
while (retry < max_retries) {
ESP_LOGD(TAG, "Sending command: %s", init_sequence[cmd_i].cmd);
uart_write_bytes(UART_NUM_1, init_sequence[cmd_i].cmd, strlen(init_sequence[cmd_i].cmd));
xQueueReceive(event_queue, &event, pdMS_TO_TICKS(pdMS_TO_TICKS(1000)));
size_t len;
uart_get_buffered_data_len(UART_NUM_1, &len);
if (!len) {
continue;
}
len = uart_read_bytes(UART_NUM_1, reply, BUF_SIZE, 0);
ESP_LOGD(TAG, "Received: %.*s", len, reply);
if (strstr(reply, init_sequence[cmd_i].expect) || init_sequence[cmd_i].allow_fail) {
if (strstr(reply, CONNECTED)) { // are we connected already?
break;
}
cmd_i++;
continue;
}
++retry;
vTaskDelay(pdMS_TO_TICKS(retry * 1000));
}
if (retry >= max_retries) {
ESP_LOGE(TAG, "Failed to perform initial modem connection");
vTaskDelete(NULL);
}
ESP_LOGI(TAG, "Modem configured correctly, switching to PPP protocol");
esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, ppp_info->parent.netif);
esp_netif_action_start(ppp_info->parent.netif, 0, 0, 0);
while (!ppp_info->stop_task) {
xQueueReceive(event_queue, &event, pdMS_TO_TICKS(pdMS_TO_TICKS(1000)));
if (event.type == UART_DATA) {
size_t len;
uart_get_buffered_data_len(UART_NUM_1, &len);
if (len) {
len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0);
ESP_LOG_BUFFER_HEXDUMP("ppp_uart_recv", buffer, len, ESP_LOG_VERBOSE);
esp_netif_receive(ppp_info->parent.netif, buffer, len, NULL);
}
} else {
ESP_LOGW(TAG, "Received UART event: %d", event.type);
}
}
}
void ppp_destroy_context(struct ppp_info_t *ppp_info)
{
char *buffer = ppp_info->context;
ppp_info->stop_task = true;
vTaskDelay(pdMS_TO_TICKS(1000));
free(buffer);
uart_driver_delete(UART_NUM_1);
}

View File

@ -66,7 +66,7 @@ static void event_handler(void *args, esp_event_base_t event_base,
}
}
static void teardown_wifi(iface_info_t *info)
static void wifi_destroy(iface_info_t *info)
{
esp_netif_action_disconnected(info->netif, 0, 0, 0);
esp_netif_action_stop(info->netif, 0, 0, 0);
@ -75,11 +75,11 @@ static void teardown_wifi(iface_info_t *info)
free(info);
}
iface_info_t *setup_wifi(int prio)
iface_info_t *wifi_init(int prio)
{
struct iface_info_t *wifi_info = malloc(sizeof(iface_info_t));
assert(wifi_info);
wifi_info->teardown = teardown_wifi;
wifi_info->destroy = wifi_destroy;
wifi_info->name = "WiFi station";
s_wifi_event_group = xEventGroupCreate();
@ -115,13 +115,13 @@ iface_info_t *setup_wifi(int prio)
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD);
teardown_wifi(wifi_info);
wifi_destroy(wifi_info);
wifi_info = NULL;
} else if (CONFIG_ESP_MAXIMUM_RETRY == 0) {
ESP_LOGI(TAG, "No connection at the moment, will keep retrying...");
} else {
ESP_LOGE(TAG, "Failed to connect withing specified timeout");
teardown_wifi(wifi_info);
wifi_destroy(wifi_info);
wifi_info = NULL;
}
return wifi_info;