Created
August 26, 2021 17:29
-
-
Save david-cermak/f710199aebea66c6388142f185069dc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* PPPoS Client Example | |
This example code is in the Public Domain (or CC0 licensed, at your option.) | |
Unless required by applicable law or agreed to in writing, this | |
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
CONDITIONS OF ANY KIND, either express or implied. | |
*/ | |
#include <string.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/event_groups.h" | |
#include "esp_netif.h" | |
#include "esp_netif_ppp.h" | |
#include "esp_modem.h" | |
#include "esp_modem_netif.h" | |
#include "esp_log.h" | |
#include "lwip/err.h" | |
#include "lwip/sockets.h" | |
#include "null_dce.h" | |
static const char *TAG = "pppos_test"; | |
static EventGroupHandle_t event_group = NULL; | |
static const int CONNECT_BIT = BIT0; | |
static const int STOP_BIT = BIT1; | |
static const int DISCONNECT_BIT = BIT2; | |
static void modem_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) | |
{ | |
switch (event_id) { | |
case ESP_MODEM_EVENT_PPP_START: | |
ESP_LOGI(TAG, "Modem PPP Started"); | |
break; | |
case ESP_MODEM_EVENT_PPP_STOP: | |
ESP_LOGI(TAG, "Modem PPP Stopped"); | |
xEventGroupSetBits(event_group, STOP_BIT); | |
break; | |
case ESP_MODEM_EVENT_UNKNOWN: | |
ESP_LOGW(TAG, "Unknow line received: %s", (char *)event_data); | |
break; | |
default: | |
break; | |
} | |
} | |
static void on_ppp_changed(void *arg, esp_event_base_t event_base, | |
int32_t event_id, void *event_data) | |
{ | |
ESP_LOGI(TAG, "PPP state changed event %d", event_id); | |
if (event_id == NETIF_PPP_PHASE_DISCONNECT) { | |
xEventGroupSetBits(event_group, DISCONNECT_BIT); | |
} | |
} | |
static void on_ip_event(void *arg, esp_event_base_t event_base, | |
int32_t event_id, void *event_data) | |
{ | |
if (event_id == IP_EVENT_PPP_GOT_IP) { | |
ESP_LOGI(TAG, "GOT ip event!!!"); | |
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; | |
ESP_LOGI(TAG, "PPP client connected to PPP Server"); | |
ESP_LOGI(TAG, "IP : " IPSTR, IP2STR(&event->ip_info.ip)); | |
ESP_LOGI(TAG, "Netmask : " IPSTR, IP2STR(&event->ip_info.netmask)); | |
ESP_LOGI(TAG, "Gateway : " IPSTR, IP2STR(&event->ip_info.gw)); | |
xEventGroupSetBits(event_group, CONNECT_BIT); | |
} else if (event_id == IP_EVENT_PPP_LOST_IP) { | |
ESP_LOGI(TAG, "Modem Disconnect from PPP Server"); | |
} else if (event_id == IP_EVENT_GOT_IP6) { | |
ESP_LOGI(TAG, "GOT IPv6 event!"); | |
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; | |
ESP_LOGI(TAG, "Got IPv6 address " IPV6STR, IPV62STR(event->ip6_info.ip)); | |
} | |
} | |
void app_main(void) { | |
ESP_ERROR_CHECK(esp_netif_init()); | |
ESP_ERROR_CHECK(esp_event_loop_create_default()); | |
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL)); | |
ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL)); | |
while (1) { | |
event_group = xEventGroupCreate(); | |
/* create dte object */ | |
esp_modem_dte_config_t config = ESP_MODEM_DTE_DEFAULT_CONFIG(); | |
/* setup UART specific configuration based on kconfig options */ | |
config.rx_io_num = 26; | |
config.tx_io_num = 25; | |
modem_dte_t *dte = esp_modem_dte_init(&config); | |
/* Register event handler */ | |
ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL)); | |
// Init netif object | |
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP(); | |
esp_netif_t *esp_netif = esp_netif_new(&cfg); | |
assert(esp_netif); | |
void *modem_netif_adapter = esp_modem_netif_setup(dte); | |
esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif); | |
modem_dce_t *dce = null_dce_init(dte); | |
/* attach the modem to the network interface */ | |
esp_netif_attach(esp_netif, modem_netif_adapter); | |
/* Wait for IP address */ | |
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY); | |
ESP_LOGI(TAG, "CONNECTED!"); | |
ESP_LOGI(TAG, "STOP"); | |
ESP_ERROR_CHECK(esp_modem_stop_ppp(dte)); | |
esp_modem_netif_clear_default_handlers(modem_netif_adapter); | |
esp_modem_netif_teardown(modem_netif_adapter); | |
esp_netif_destroy(esp_netif); | |
ESP_LOGD(TAG, "netif destroyed"); | |
xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY); | |
ESP_ERROR_CHECK(dce->deinit(dce)); | |
ESP_ERROR_CHECK(dte->deinit(dte)); | |
vEventGroupDelete(event_group); | |
event_group = NULL; | |
ESP_LOGI(TAG, "All deleted"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For me, got crash with xQueueGenericSend when I add some TCP between connected and stop as well.