test_console.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "sdkconfig.h"
  9. #include "unity.h"
  10. #include "test_utils.h"
  11. #include "esp_console.h"
  12. #include "argtable3/argtable3.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. static int do_hello_cmd(int argc, char **argv)
  16. {
  17. printf("Hello World\n");
  18. return 0;
  19. }
  20. TEST_CASE("esp console init/deinit test", "[console]")
  21. {
  22. esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
  23. TEST_ESP_OK(esp_console_init(&console_config));
  24. const esp_console_cmd_t cmd = {
  25. .command = "hello",
  26. .help = "Print Hello World",
  27. .hint = NULL,
  28. .func = do_hello_cmd,
  29. };
  30. TEST_ESP_OK(esp_console_cmd_register(&cmd));
  31. // re-register the same command, just for test
  32. TEST_ESP_OK(esp_console_cmd_register(&cmd));
  33. TEST_ESP_OK(esp_console_deinit());
  34. }
  35. static esp_console_repl_t *s_repl = NULL;
  36. /* handle 'quit' command */
  37. static int do_cmd_quit(int argc, char **argv)
  38. {
  39. printf("ByeBye\r\n");
  40. s_repl->del(s_repl);
  41. return 0;
  42. }
  43. // Enter "quit" to exit REPL environment
  44. TEST_CASE("esp console repl test", "[console][ignore]")
  45. {
  46. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  47. esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  48. TEST_ESP_OK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl));
  49. esp_console_cmd_t cmd = {
  50. .command = "quit",
  51. .help = "Quit REPL environment",
  52. .func = &do_cmd_quit
  53. };
  54. TEST_ESP_OK(esp_console_cmd_register(&cmd));
  55. TEST_ESP_OK(esp_console_start_repl(s_repl));
  56. vTaskDelay(pdMS_TO_TICKS(2000));
  57. }