| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | /* ICMP echo 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 <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include "sdkconfig.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | #include "lwip/inet.h"
 | 
					
						
							|  |  |  | #include "lwip/netdb.h"
 | 
					
						
							|  |  |  | #include "lwip/sockets.h"
 | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | #include "esp_console.h"
 | 
					
						
							|  |  |  | #include "esp_event.h"
 | 
					
						
							|  |  |  | #include "nvs_flash.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | #include "argtable3/argtable3.h"
 | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | #include "protocol_examples_common.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | #include "ping/ping_sock.h"
 | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | static void cmd_ping_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)); | 
					
						
							| 
									
										
										
										
											2022-12-15 16:15:39 +01:00
										 |  |  |     printf("%" PRIu32 " bytes from %s icmp_seq=%" PRIu16 " ttl=%" PRIu16 " time=%" PRIu32 " ms\n", | 
					
						
							| 
									
										
										
										
											2020-07-06 19:20:48 +08:00
										 |  |  |            recv_len, ipaddr_ntoa((ip_addr_t*)&target_addr), seqno, ttl, elapsed_time); | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args) | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     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)); | 
					
						
							| 
									
										
										
										
											2020-07-06 19:20:48 +08:00
										 |  |  |     printf("From %s icmp_seq=%d timeout\n",ipaddr_ntoa((ip_addr_t*)&target_addr), seqno); | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ip_addr_t target_addr; | 
					
						
							|  |  |  |     uint32_t transmitted; | 
					
						
							|  |  |  |     uint32_t received; | 
					
						
							|  |  |  |     uint32_t total_time_ms; | 
					
						
							| 
									
										
										
										
											2023-04-22 21:56:33 +08:00
										 |  |  |     uint32_t loss; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     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_IPADDR, &target_addr, sizeof(target_addr)); | 
					
						
							|  |  |  |     esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); | 
					
						
							| 
									
										
										
										
											2023-04-22 21:56:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (transmitted > 0) { | 
					
						
							|  |  |  |         loss = (uint32_t)((1 - ((float)received) / transmitted) * 100); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         loss = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     if (IP_IS_V4(&target_addr)) { | 
					
						
							|  |  |  |         printf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr))); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr))); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-15 16:15:39 +01:00
										 |  |  |     printf("%" PRIu32 " packets transmitted, %" PRIu32 " received, %" PRIu32 "%% packet loss, time %" PRIu32 "ms\n", | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |            transmitted, received, loss, total_time_ms); | 
					
						
							|  |  |  |     // delete the ping sessions, so that we clean up all resources and can create a new ping session
 | 
					
						
							|  |  |  |     // we don't have to call delete function in the callback, instead we can call delete function from other tasks
 | 
					
						
							|  |  |  |     esp_ping_delete_session(hdl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct { | 
					
						
							|  |  |  |     struct arg_dbl *timeout; | 
					
						
							|  |  |  |     struct arg_dbl *interval; | 
					
						
							|  |  |  |     struct arg_int *data_size; | 
					
						
							|  |  |  |     struct arg_int *count; | 
					
						
							|  |  |  |     struct arg_int *tos; | 
					
						
							| 
									
										
										
										
											2022-06-06 14:11:19 +08:00
										 |  |  |     struct arg_int *ttl; | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     struct arg_str *host; | 
					
						
							|  |  |  |     struct arg_end *end; | 
					
						
							| 
									
										
										
										
											2020-05-28 11:00:56 +08:00
										 |  |  | } ping_args; | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int do_ping_cmd(int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int nerrors = arg_parse(argc, argv, (void **)&ping_args); | 
					
						
							|  |  |  |     if (nerrors != 0) { | 
					
						
							|  |  |  |         arg_print_errors(stderr, ping_args.end, argv[0]); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ping_args.timeout->count > 0) { | 
					
						
							|  |  |  |         config.timeout_ms = (uint32_t)(ping_args.timeout->dval[0] * 1000); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ping_args.interval->count > 0) { | 
					
						
							|  |  |  |         config.interval_ms = (uint32_t)(ping_args.interval->dval[0] * 1000); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ping_args.data_size->count > 0) { | 
					
						
							|  |  |  |         config.data_size = (uint32_t)(ping_args.data_size->ival[0]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ping_args.count->count > 0) { | 
					
						
							|  |  |  |         config.count = (uint32_t)(ping_args.count->ival[0]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     if (ping_args.tos->count > 0) { | 
					
						
							|  |  |  |         config.tos = (uint32_t)(ping_args.tos->ival[0]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-06 14:11:19 +08:00
										 |  |  |     if (ping_args.ttl->count > 0) { | 
					
						
							|  |  |  |         config.ttl = (uint32_t)(ping_args.ttl->ival[0]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     // parse IP address
 | 
					
						
							| 
									
										
										
										
											2020-07-06 19:20:48 +08:00
										 |  |  |     struct sockaddr_in6 sock_addr6; | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     ip_addr_t target_addr; | 
					
						
							|  |  |  |     memset(&target_addr, 0, sizeof(target_addr)); | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 19:20:48 +08:00
										 |  |  |     if (inet_pton(AF_INET6, ping_args.host->sval[0], &sock_addr6.sin6_addr) == 1) { | 
					
						
							|  |  |  |         /* convert ip6 string to ip6 address */ | 
					
						
							|  |  |  |         ipaddr_aton(ping_args.host->sval[0], &target_addr); | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-07-06 19:20:48 +08:00
										 |  |  |         struct addrinfo hint; | 
					
						
							|  |  |  |         struct addrinfo *res = NULL; | 
					
						
							|  |  |  |         memset(&hint, 0, sizeof(hint)); | 
					
						
							|  |  |  |         /* convert ip4 string or hostname to ip4 or ip6 address */ | 
					
						
							|  |  |  |         if (getaddrinfo(ping_args.host->sval[0], NULL, &hint, &res) != 0) { | 
					
						
							|  |  |  |             printf("ping: unknown host %s\n", ping_args.host->sval[0]); | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (res->ai_family == AF_INET) { | 
					
						
							|  |  |  |             struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; | 
					
						
							|  |  |  |             inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; | 
					
						
							|  |  |  |             inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         freeaddrinfo(res); | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     config.target_addr = target_addr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* set callback functions */ | 
					
						
							|  |  |  |     esp_ping_callbacks_t cbs = { | 
					
						
							| 
									
										
										
										
											2022-05-16 11:30:55 -05:00
										 |  |  |         .cb_args = NULL, | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |         .on_ping_success = cmd_ping_on_ping_success, | 
					
						
							|  |  |  |         .on_ping_timeout = cmd_ping_on_ping_timeout, | 
					
						
							| 
									
										
										
										
											2022-05-16 11:30:55 -05:00
										 |  |  |         .on_ping_end = cmd_ping_on_ping_end | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  |     esp_ping_handle_t ping; | 
					
						
							|  |  |  |     esp_ping_new_session(&config, &cbs, &ping); | 
					
						
							|  |  |  |     esp_ping_start(ping); | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  | static void register_ping(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ping_args.timeout = arg_dbl0("W", "timeout", "<t>", "Time to wait for a response, in seconds"); | 
					
						
							|  |  |  |     ping_args.interval = arg_dbl0("i", "interval", "<t>", "Wait interval seconds between sending each packet"); | 
					
						
							|  |  |  |     ping_args.data_size = arg_int0("s", "size", "<n>", "Specify the number of data bytes to be sent"); | 
					
						
							|  |  |  |     ping_args.count = arg_int0("c", "count", "<n>", "Stop after sending count packets"); | 
					
						
							|  |  |  |     ping_args.tos = arg_int0("Q", "tos", "<n>", "Set Type of Service related bits in IP datagrams"); | 
					
						
							| 
									
										
										
										
											2022-06-06 14:11:19 +08:00
										 |  |  |     ping_args.ttl = arg_int0("T", "ttl", "<n>", "Set Time to Live related bits in IP datagrams"); | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     ping_args.host = arg_str1(NULL, NULL, "<host>", "Host address"); | 
					
						
							|  |  |  |     ping_args.end = arg_end(1); | 
					
						
							|  |  |  |     const esp_console_cmd_t ping_cmd = { | 
					
						
							|  |  |  |         .command = "ping", | 
					
						
							|  |  |  |         .help = "send ICMP ECHO_REQUEST to network hosts", | 
					
						
							|  |  |  |         .hint = NULL, | 
					
						
							|  |  |  |         .func = &do_ping_cmd, | 
					
						
							|  |  |  |         .argtable = &ping_args | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_console_cmd_register(&ping_cmd)); | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 11:00:56 +08:00
										 |  |  | static esp_console_repl_t *s_repl = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* handle 'quit' command */ | 
					
						
							|  |  |  | static int do_cmd_quit(int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     printf("ByeBye\r\n"); | 
					
						
							|  |  |  |     s_repl->del(s_repl); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static esp_err_t register_quit(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_console_cmd_t command = { | 
					
						
							|  |  |  |         .command = "quit", | 
					
						
							|  |  |  |         .help = "Quit REPL environment", | 
					
						
							|  |  |  |         .func = &do_cmd_quit | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return esp_console_cmd_register(&command); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | void app_main(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(nvs_flash_init()); | 
					
						
							| 
									
										
										
										
											2019-11-29 10:54:02 +01:00
										 |  |  |     ESP_ERROR_CHECK(esp_netif_init()); | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_loop_create_default()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); | 
					
						
							| 
									
										
										
										
											2020-10-14 18:10:11 +08:00
										 |  |  |     // install console REPL environment
 | 
					
						
							|  |  |  | #if CONFIG_ESP_CONSOLE_UART
 | 
					
						
							| 
									
										
										
										
											2020-05-28 11:00:56 +08:00
										 |  |  |     esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl)); | 
					
						
							| 
									
										
										
										
											2020-10-14 18:10:11 +08:00
										 |  |  | #elif CONFIG_ESP_CONSOLE_USB_CDC
 | 
					
						
							|  |  |  |     esp_console_dev_usb_cdc_config_t cdc_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT(); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&cdc_config, &repl_config, &s_repl)); | 
					
						
							| 
									
										
										
										
											2021-04-23 15:46:02 +08:00
										 |  |  | #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
 | 
					
						
							|  |  |  |     esp_console_dev_usb_serial_jtag_config_t usbjtag_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT(); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbjtag_config, &repl_config, &repl)); | 
					
						
							| 
									
										
										
										
											2020-10-14 18:10:11 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 16:22:20 +08:00
										 |  |  |     /* register wifi connect commands */ | 
					
						
							| 
									
										
										
										
											2022-07-07 00:34:06 +08:00
										 |  |  |     example_register_wifi_connect_commands(); | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     /* register command `ping` */ | 
					
						
							|  |  |  |     register_ping(); | 
					
						
							| 
									
										
										
										
											2020-05-28 11:00:56 +08:00
										 |  |  |     /* register command `quit` */ | 
					
						
							|  |  |  |     register_quit(); | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 18:01:04 +08:00
										 |  |  |     // start console REPL
 | 
					
						
							| 
									
										
										
										
											2020-05-28 11:00:56 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_console_start_repl(s_repl)); | 
					
						
							| 
									
										
										
										
											2019-10-13 19:01:33 +08:00
										 |  |  | } |