|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <stddef.h> |
| 10 | +#include <string.h> |
| 11 | +#include "esp_system.h" |
| 12 | +#include "nvs_flash.h" |
| 13 | +#include "esp_event.h" |
| 14 | +#include "esp_netif.h" |
| 15 | +#include "eppp_link.h" |
| 16 | +#include "lwip/sockets.h" |
| 17 | +#include "esp_log.h" |
| 18 | +#include "mqtt_client.h" |
| 19 | +#include "ping/ping_sock.h" |
| 20 | +#include "esp_console.h" |
| 21 | +#include "esp_wifi_remote.h" |
| 22 | + |
| 23 | +void register_iperf(void); |
| 24 | +esp_err_t client_init(void); |
| 25 | + |
| 26 | +static const char *TAG = "eppp_host_example"; |
| 27 | + |
| 28 | +#if CONFIG_EXAMPLE_MQTT |
| 29 | +static void mqtt_event_handler(void *args, esp_event_base_t base, int32_t event_id, void *event_data) |
| 30 | +{ |
| 31 | + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); |
| 32 | + esp_mqtt_event_handle_t event = event_data; |
| 33 | + esp_mqtt_client_handle_t client = event->client; |
| 34 | + int msg_id; |
| 35 | + switch ((esp_mqtt_event_id_t)event_id) { |
| 36 | + case MQTT_EVENT_CONNECTED: |
| 37 | + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
| 38 | + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); |
| 39 | + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
| 40 | + |
| 41 | + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); |
| 42 | + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); |
| 43 | + |
| 44 | + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); |
| 45 | + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); |
| 46 | + |
| 47 | + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); |
| 48 | + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); |
| 49 | + break; |
| 50 | + case MQTT_EVENT_DISCONNECTED: |
| 51 | + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); |
| 52 | + break; |
| 53 | + |
| 54 | + case MQTT_EVENT_SUBSCRIBED: |
| 55 | + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
| 56 | + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); |
| 57 | + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
| 58 | + break; |
| 59 | + case MQTT_EVENT_UNSUBSCRIBED: |
| 60 | + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
| 61 | + break; |
| 62 | + case MQTT_EVENT_PUBLISHED: |
| 63 | + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
| 64 | + break; |
| 65 | + case MQTT_EVENT_DATA: |
| 66 | + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); |
| 67 | + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); |
| 68 | + printf("DATA=%.*s\r\n", event->data_len, event->data); |
| 69 | + break; |
| 70 | + case MQTT_EVENT_ERROR: |
| 71 | + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
| 72 | + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { |
| 73 | + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); |
| 74 | + } |
| 75 | + break; |
| 76 | + default: |
| 77 | + ESP_LOGI(TAG, "Other event id:%d", event->event_id); |
| 78 | + break; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +static void mqtt_app_start(void) |
| 83 | +{ |
| 84 | + esp_mqtt_client_config_t mqtt_cfg = { |
| 85 | + .broker.address.uri = CONFIG_EXAMPLE_BROKER_URL, |
| 86 | + }; |
| 87 | + |
| 88 | + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); |
| 89 | + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ |
| 90 | + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); |
| 91 | + esp_mqtt_client_start(client); |
| 92 | +} |
| 93 | +#endif // MQTT |
| 94 | + |
| 95 | +#if CONFIG_EXAMPLE_ICMP_PING |
| 96 | +static void test_on_ping_success(esp_ping_handle_t hdl, void *args) |
| 97 | +{ |
| 98 | + uint8_t ttl; |
| 99 | + uint16_t seqno; |
| 100 | + uint32_t elapsed_time, recv_len; |
| 101 | + ip_addr_t target_addr; |
| 102 | + esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); |
| 103 | + esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl)); |
| 104 | + esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); |
| 105 | + esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len)); |
| 106 | + esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time)); |
| 107 | + printf("%" PRId32 "bytes from %s icmp_seq=%d ttl=%d time=%" PRId32 " ms\n", |
| 108 | + recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time); |
| 109 | +} |
| 110 | + |
| 111 | +static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args) |
| 112 | +{ |
| 113 | + uint16_t seqno; |
| 114 | + ip_addr_t target_addr; |
| 115 | + esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); |
| 116 | + esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); |
| 117 | + printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno); |
| 118 | +} |
| 119 | + |
| 120 | +static void test_on_ping_end(esp_ping_handle_t hdl, void *args) |
| 121 | +{ |
| 122 | + uint32_t transmitted; |
| 123 | + uint32_t received; |
| 124 | + uint32_t total_time_ms; |
| 125 | + esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted)); |
| 126 | + esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received)); |
| 127 | + esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); |
| 128 | + printf("%" PRId32 " packets transmitted, %" PRId32 " received, time %" PRId32 "ms\n", transmitted, received, total_time_ms); |
| 129 | + |
| 130 | +} |
| 131 | +#endif // PING |
| 132 | + |
| 133 | +void app_main(void) |
| 134 | +{ |
| 135 | + ESP_LOGI(TAG, "[APP] Startup.."); |
| 136 | + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); |
| 137 | + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); |
| 138 | + |
| 139 | + ESP_ERROR_CHECK(nvs_flash_init()); |
| 140 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 141 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 142 | + |
| 143 | + /* Sets up the default EPPP-connection |
| 144 | + */ |
| 145 | + eppp_config_t config = EPPP_DEFAULT_CLIENT_CONFIG(); |
| 146 | +#if CONFIG_EPPP_LINK_DEVICE_SPI |
| 147 | + config.transport = EPPP_TRANSPORT_SPI; |
| 148 | + config.task.priority = 5; |
| 149 | +#else |
| 150 | + config.transport = EPPP_TRANSPORT_UART; |
| 151 | + config.uart.tx_io = 10; |
| 152 | + config.uart.rx_io = 11; |
| 153 | + config.uart.baud = 2000000; |
| 154 | +#endif |
| 155 | + esp_netif_t *eppp_netif = eppp_connect(&config); |
| 156 | + if (eppp_netif == NULL) { |
| 157 | + ESP_LOGE(TAG, "Failed to connect"); |
| 158 | + return ; |
| 159 | + } |
| 160 | + |
| 161 | + client_init(); |
| 162 | + |
| 163 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 164 | + ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN); |
| 165 | + ESP_ERROR_CHECK(esp_wifi_remote_init(&cfg)); |
| 166 | + |
| 167 | + wifi_config_t wifi_config = { |
| 168 | + .sta = { |
| 169 | + .ssid = CONFIG_ESP_WIFI_SSID, |
| 170 | + .password = CONFIG_ESP_WIFI_PASSWORD, |
| 171 | + }, |
| 172 | + }; |
| 173 | + |
| 174 | + esp_err_t err = esp_wifi_remote_set_mode(WIFI_MODE_STA); |
| 175 | + ESP_LOGI(TAG, "esp_wifi_remote_set_mode() returned = %x", err); |
| 176 | + ESP_ERROR_CHECK(esp_wifi_remote_set_config(WIFI_IF_STA, &wifi_config) ); |
| 177 | + ESP_ERROR_CHECK(esp_wifi_remote_start() ); |
| 178 | + vTaskDelay(pdMS_TO_TICKS(1000)); |
| 179 | + ESP_ERROR_CHECK(esp_wifi_remote_connect() ); |
| 180 | + |
| 181 | +// // Setup global DNS |
| 182 | +// esp_netif_dns_info_t dns; |
| 183 | +// dns.ip.u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_GLOBAL_DNS); |
| 184 | +// dns.ip.type = ESP_IPADDR_TYPE_V4; |
| 185 | +// ESP_ERROR_CHECK(esp_netif_set_dns_info(eppp_netif, ESP_NETIF_DNS_MAIN, &dns)); |
| 186 | + |
| 187 | +#if CONFIG_EXAMPLE_IPERF |
| 188 | + esp_console_repl_t *repl = NULL; |
| 189 | + esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); |
| 190 | + esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); |
| 191 | + repl_config.prompt = "iperf>"; |
| 192 | + // init console REPL environment |
| 193 | + ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl)); |
| 194 | + |
| 195 | + register_iperf(); |
| 196 | + |
| 197 | + printf("\n =======================================================\n"); |
| 198 | + printf(" | Steps to Test PPP Client Bandwidth |\n"); |
| 199 | + printf(" | |\n"); |
| 200 | + printf(" | 1. Enter 'help', check all supported commands |\n"); |
| 201 | + printf(" | 2. Start PPP server on host system |\n"); |
| 202 | + printf(" | - pppd /dev/ttyUSB1 115200 192.168.11.1:192.168.11.2 modem local noauth debug nocrtscts nodetach +ipv6\n"); |
| 203 | + printf(" | 3. Wait ESP32 to get IP from PPP server |\n"); |
| 204 | + printf(" | 4. Enter 'pppd info' (optional) |\n"); |
| 205 | + printf(" | 5. Server: 'iperf -u -s -i 3' |\n"); |
| 206 | + printf(" | 6. Client: 'iperf -u -c SERVER_IP -t 60 -i 3' |\n"); |
| 207 | + printf(" | |\n"); |
| 208 | + printf(" =======================================================\n\n"); |
| 209 | + |
| 210 | + // start console REPL |
| 211 | + ESP_ERROR_CHECK(esp_console_start_repl(repl)); |
| 212 | +#endif |
| 213 | + |
| 214 | +#if CONFIG_EXAMPLE_ICMP_PING |
| 215 | + ip_addr_t target_addr = { .type = IPADDR_TYPE_V4, .u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_PING_ADDR) }; |
| 216 | + |
| 217 | + esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG(); |
| 218 | + ping_config.timeout_ms = 2000; |
| 219 | + ping_config.interval_ms = 20, |
| 220 | + ping_config.target_addr = target_addr; |
| 221 | + ping_config.count = 100; // ping in infinite mode |
| 222 | + /* set callback functions */ |
| 223 | + esp_ping_callbacks_t cbs; |
| 224 | + cbs.on_ping_success = test_on_ping_success; |
| 225 | + cbs.on_ping_timeout = test_on_ping_timeout; |
| 226 | + cbs.on_ping_end = test_on_ping_end; |
| 227 | + esp_ping_handle_t ping; |
| 228 | + esp_ping_new_session(&ping_config, &cbs, &ping); |
| 229 | + /* start ping */ |
| 230 | + esp_ping_start(ping); |
| 231 | +#endif // PING |
| 232 | + |
| 233 | +#if CONFIG_EXAMPLE_MQTT |
| 234 | + mqtt_app_start(); |
| 235 | +#endif |
| 236 | +} |
0 commit comments