mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-21 14:32:21 +02:00
204 lines
7.8 KiB
C
204 lines
7.8 KiB
C
![]() |
/*
|
||
|
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||
|
*
|
||
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdint.h>
|
||
|
#include <stddef.h>
|
||
|
#include <string.h>
|
||
|
#include "esp_system.h"
|
||
|
#include "nvs_flash.h"
|
||
|
#include "esp_event.h"
|
||
|
#include "esp_netif.h"
|
||
|
#include "eppp_link.h"
|
||
|
#include "lwip/sockets.h"
|
||
|
#include "esp_log.h"
|
||
|
#include "mqtt_client.h"
|
||
|
#include "ping/ping_sock.h"
|
||
|
#include "esp_console.h"
|
||
|
|
||
|
void register_iperf(void);
|
||
|
|
||
|
static const char *TAG = "eppp_host_example";
|
||
|
|
||
|
#if CONFIG_EXAMPLE_MQTT
|
||
|
static void mqtt_event_handler(void *args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||
|
{
|
||
|
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
|
||
|
esp_mqtt_event_handle_t event = event_data;
|
||
|
esp_mqtt_client_handle_t client = event->client;
|
||
|
int msg_id;
|
||
|
switch ((esp_mqtt_event_id_t)event_id) {
|
||
|
case MQTT_EVENT_CONNECTED:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||
|
msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
|
||
|
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
||
|
|
||
|
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
|
||
|
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||
|
|
||
|
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
|
||
|
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||
|
|
||
|
msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
|
||
|
ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
|
||
|
break;
|
||
|
case MQTT_EVENT_DISCONNECTED:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
|
||
|
break;
|
||
|
|
||
|
case MQTT_EVENT_SUBSCRIBED:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
||
|
msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
|
||
|
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
||
|
break;
|
||
|
case MQTT_EVENT_UNSUBSCRIBED:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
||
|
break;
|
||
|
case MQTT_EVENT_PUBLISHED:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
||
|
break;
|
||
|
case MQTT_EVENT_DATA:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
|
||
|
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
|
||
|
printf("DATA=%.*s\r\n", event->data_len, event->data);
|
||
|
break;
|
||
|
case MQTT_EVENT_ERROR:
|
||
|
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
||
|
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
|
||
|
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void mqtt_app_start(void)
|
||
|
{
|
||
|
esp_mqtt_client_config_t mqtt_cfg = {
|
||
|
.broker.address.uri = CONFIG_EXAMPLE_BROKER_URL,
|
||
|
};
|
||
|
|
||
|
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||
|
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
|
||
|
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
||
|
esp_mqtt_client_start(client);
|
||
|
}
|
||
|
#endif // MQTT
|
||
|
|
||
|
#if CONFIG_EXAMPLE_ICMP_PING
|
||
|
static void test_on_ping_success(esp_ping_handle_t hdl, void *args)
|
||
|
{
|
||
|
uint8_t ttl;
|
||
|
uint16_t seqno;
|
||
|
uint32_t elapsed_time, recv_len;
|
||
|
ip_addr_t target_addr;
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
|
||
|
printf("%" PRId32 "bytes from %s icmp_seq=%d ttl=%d time=%" PRId32 " ms\n",
|
||
|
recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
|
||
|
}
|
||
|
|
||
|
static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args)
|
||
|
{
|
||
|
uint16_t seqno;
|
||
|
ip_addr_t target_addr;
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||
|
printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
|
||
|
}
|
||
|
|
||
|
static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
|
||
|
{
|
||
|
uint32_t transmitted;
|
||
|
uint32_t received;
|
||
|
uint32_t total_time_ms;
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
|
||
|
esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
|
||
|
printf("%" PRId32 " packets transmitted, %" PRId32 " received, time %" PRId32 "ms\n", transmitted, received, total_time_ms);
|
||
|
|
||
|
}
|
||
|
#endif // PING
|
||
|
|
||
|
void app_main(void)
|
||
|
{
|
||
|
ESP_LOGI(TAG, "[APP] Startup..");
|
||
|
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
|
||
|
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
||
|
|
||
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||
|
ESP_ERROR_CHECK(esp_netif_init());
|
||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||
|
|
||
|
/* Sets up the default EPPP-connection
|
||
|
*/
|
||
|
esp_netif_t *eppp_netif = eppp_connect();
|
||
|
if (eppp_netif == NULL) {
|
||
|
ESP_LOGE(TAG, "Failed to connect");
|
||
|
return ;
|
||
|
}
|
||
|
// Setup global DNS
|
||
|
esp_netif_dns_info_t dns;
|
||
|
dns.ip.u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_GLOBAL_DNS);
|
||
|
dns.ip.type = ESP_IPADDR_TYPE_V4;
|
||
|
ESP_ERROR_CHECK(esp_netif_set_dns_info(eppp_netif, ESP_NETIF_DNS_MAIN, &dns));
|
||
|
|
||
|
#if CONFIG_EXAMPLE_IPERF
|
||
|
esp_console_repl_t *repl = NULL;
|
||
|
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
|
||
|
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
|
||
|
repl_config.prompt = "iperf>";
|
||
|
// init console REPL environment
|
||
|
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
|
||
|
|
||
|
register_iperf();
|
||
|
|
||
|
printf("\n =======================================================\n");
|
||
|
printf(" | Steps to Test PPP Client Bandwidth |\n");
|
||
|
printf(" | |\n");
|
||
|
printf(" | 1. Enter 'help', check all supported commands |\n");
|
||
|
printf(" | 2. Start PPP server on host system |\n");
|
||
|
printf(" | - pppd /dev/ttyUSB1 115200 192.168.11.1:192.168.11.2 modem local noauth debug nocrtscts nodetach +ipv6\n");
|
||
|
printf(" | 3. Wait ESP32 to get IP from PPP server |\n");
|
||
|
printf(" | 4. Enter 'pppd info' (optional) |\n");
|
||
|
printf(" | 5. Server: 'iperf -u -s -i 3' |\n");
|
||
|
printf(" | 6. Client: 'iperf -u -c SERVER_IP -t 60 -i 3' |\n");
|
||
|
printf(" | |\n");
|
||
|
printf(" =======================================================\n\n");
|
||
|
|
||
|
// start console REPL
|
||
|
ESP_ERROR_CHECK(esp_console_start_repl(repl));
|
||
|
#endif
|
||
|
|
||
|
#if CONFIG_EXAMPLE_ICMP_PING
|
||
|
ip_addr_t target_addr = { .type = IPADDR_TYPE_V4, .u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_PING_ADDR) };
|
||
|
|
||
|
esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
|
||
|
ping_config.timeout_ms = 2000;
|
||
|
ping_config.interval_ms = 20,
|
||
|
ping_config.target_addr = target_addr;
|
||
|
ping_config.count = 100; // ping in infinite mode
|
||
|
/* set callback functions */
|
||
|
esp_ping_callbacks_t cbs;
|
||
|
cbs.on_ping_success = test_on_ping_success;
|
||
|
cbs.on_ping_timeout = test_on_ping_timeout;
|
||
|
cbs.on_ping_end = test_on_ping_end;
|
||
|
esp_ping_handle_t ping;
|
||
|
esp_ping_new_session(&ping_config, &cbs, &ping);
|
||
|
/* start ping */
|
||
|
esp_ping_start(ping);
|
||
|
#endif // PING
|
||
|
|
||
|
#if CONFIG_EXAMPLE_MQTT
|
||
|
mqtt_app_start();
|
||
|
#endif
|
||
|
}
|