iperf_example_main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Console example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "esp_system.h"
  10. #include "esp_log.h"
  11. #include "esp_console.h"
  12. #include "esp_vfs_dev.h"
  13. #include "driver/uart.h"
  14. #include "linenoise/linenoise.h"
  15. #include "argtable3/argtable3.h"
  16. #include "cmd_decl.h"
  17. #include "esp_vfs_fat.h"
  18. #include "nvs.h"
  19. #include "nvs_flash.h"
  20. #include "sdkconfig.h"
  21. static const char *TAG = "ethernet-console";
  22. #if CONFIG_STORE_HISTORY
  23. #define MOUNT_PATH "/data"
  24. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  25. static void initialize_filesystem()
  26. {
  27. static wl_handle_t wl_handle;
  28. const esp_vfs_fat_mount_config_t mount_config = {
  29. .max_files = 4,
  30. .format_if_mount_failed = true
  31. };
  32. esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
  33. if (err != ESP_OK) {
  34. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  35. return;
  36. }
  37. }
  38. #endif // CONFIG_STORE_HISTORY
  39. static void initialize_nvs()
  40. {
  41. esp_err_t err = nvs_flash_init();
  42. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  43. ESP_ERROR_CHECK(nvs_flash_erase());
  44. err = nvs_flash_init();
  45. }
  46. ESP_ERROR_CHECK(err);
  47. }
  48. static void initialize_console()
  49. {
  50. /* Disable buffering on stdin */
  51. setvbuf(stdin, NULL, _IONBF, 0);
  52. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  53. esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
  54. /* Move the caret to the beginning of the next line on '\n' */
  55. esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
  56. /* Configure UART. Note that REF_TICK is used so that the baud rate remains
  57. * correct while APB frequency is changing in light sleep mode.
  58. */
  59. const uart_config_t uart_config = {
  60. .baud_rate = CONFIG_CONSOLE_UART_BAUDRATE,
  61. .data_bits = UART_DATA_8_BITS,
  62. .parity = UART_PARITY_DISABLE,
  63. .stop_bits = UART_STOP_BITS_1,
  64. .use_ref_tick = true
  65. };
  66. ESP_ERROR_CHECK(uart_param_config(CONFIG_CONSOLE_UART_NUM, &uart_config));
  67. /* Install UART driver for interrupt-driven reads and writes */
  68. ESP_ERROR_CHECK(uart_driver_install(CONFIG_CONSOLE_UART_NUM,
  69. 256, 0, 0, NULL, 0));
  70. /* Tell VFS to use UART driver */
  71. esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
  72. /* Initialize the console */
  73. esp_console_config_t console_config = {
  74. .max_cmdline_args = 8,
  75. .max_cmdline_length = 256,
  76. #if CONFIG_LOG_COLORS
  77. .hint_color = atoi(LOG_COLOR_CYAN)
  78. #endif
  79. };
  80. ESP_ERROR_CHECK(esp_console_init(&console_config));
  81. /* Configure linenoise line completion library */
  82. linenoiseSetMultiLine(1);
  83. /* Tell linenoise where to get command completions and hints */
  84. linenoiseSetCompletionCallback(&esp_console_get_completion);
  85. linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
  86. /* Set command history size */
  87. linenoiseHistorySetMaxLen(100);
  88. #if CONFIG_STORE_HISTORY
  89. /* Load command history from filesystem */
  90. linenoiseHistoryLoad(HISTORY_PATH);
  91. #endif
  92. }
  93. void app_main()
  94. {
  95. initialize_nvs();
  96. #if CONFIG_STORE_HISTORY
  97. initialize_filesystem();
  98. #endif
  99. initialize_console();
  100. /* Register commands */
  101. esp_console_register_help_command();
  102. register_system();
  103. register_ethernet();
  104. /* Prompt to be printed before each line.
  105. * This can be customized, made dynamic, etc.
  106. */
  107. const char *prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
  108. printf("\n =======================================================\n");
  109. printf(" | Steps to Test Ethernet Bandwidth |\n");
  110. printf(" | |\n");
  111. printf(" | 1. Enter 'help', check all supported commands |\n");
  112. printf(" | 2. Enter 'ethernet start' |\n");
  113. printf(" | 3. Wait ESP32 to get IP from DHCP |\n");
  114. printf(" | 4. Enter 'ethernet info', optional |\n");
  115. printf(" | 4. Server: 'iperf -u -s -i 3' |\n");
  116. printf(" | 4. Client: 'iperf -u -c SERVER_IP -t 60 -i 3' |\n");
  117. printf(" | |\n");
  118. printf(" =======================================================\n\n");
  119. /* Figure out if the terminal supports escape sequences */
  120. int probe_status = linenoiseProbe();
  121. if (probe_status) {
  122. /* zero indicates success */
  123. printf("\n"
  124. "Your terminal application does not support escape sequences.\n"
  125. "Line editing and history features are disabled.\n"
  126. "On Windows, try using Putty instead.\n");
  127. linenoiseSetDumbMode(1);
  128. #if CONFIG_LOG_COLORS
  129. /* Since the terminal doesn't support escape sequences,
  130. * don't use color codes in the prompt.
  131. */
  132. prompt = "esp32> ";
  133. #endif //CONFIG_LOG_COLORS
  134. }
  135. /* Main loop */
  136. while (true) {
  137. /* Get a line using linenoise.
  138. * The line is returned when ENTER is pressed.
  139. */
  140. char *line = linenoise(prompt);
  141. if (line == NULL) {
  142. /* Ignore empty lines */
  143. continue;
  144. }
  145. /* Add the command to the history */
  146. linenoiseHistoryAdd(line);
  147. #if CONFIG_STORE_HISTORY
  148. /* Save command history to filesystem */
  149. linenoiseHistorySave(HISTORY_PATH);
  150. #endif
  151. /* Try to run the command */
  152. int ret;
  153. esp_err_t err = esp_console_run(line, &ret);
  154. if (err == ESP_ERR_NOT_FOUND) {
  155. printf("Unrecognized command\n");
  156. } else if (err == ESP_ERR_INVALID_ARG) {
  157. // command was empty
  158. } else if (err == ESP_OK && ret != ESP_OK) {
  159. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err));
  160. } else if (err != ESP_OK) {
  161. printf("Internal error: %s\n", esp_err_to_name(err));
  162. }
  163. /* linenoise allocates line buffer on the heap, so need to free it */
  164. linenoiseFree(line);
  165. }
  166. }