scli.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include "esp_log.h"
  9. #include <string.h>
  10. #include <esp_log.h>
  11. #include <esp_console.h>
  12. #include <freertos/FreeRTOS.h>
  13. #include <freertos/task.h>
  14. #include <freertos/queue.h>
  15. #include <driver/uart.h>
  16. #include "esp_peripheral.h"
  17. #define BLE_RX_TIMEOUT (30000 / portTICK_PERIOD_MS)
  18. static TaskHandle_t cli_task;
  19. static QueueHandle_t cli_handle;
  20. static int stop;
  21. static int enter_passkey_handler(int argc, char *argv[])
  22. {
  23. int key;
  24. char pkey[8];
  25. int num;
  26. if (argc != 2) {
  27. return -1;
  28. }
  29. sscanf(argv[1], "%s", pkey);
  30. ESP_LOGI("You entered", "%s %s", argv[0], argv[1]);
  31. num = pkey[0];
  32. if (isalpha(num)) {
  33. if ((strcasecmp(pkey, "Y") == 0) || (strcasecmp(pkey, "Yes") == 0)) {
  34. key = 1;
  35. xQueueSend(cli_handle, &key, 0);
  36. } else {
  37. key = 0;
  38. xQueueSend(cli_handle, &key, 0);
  39. }
  40. } else {
  41. sscanf(pkey, "%d", &key);
  42. xQueueSend(cli_handle, &key, 0);
  43. }
  44. return 0;
  45. }
  46. int scli_receive_key(int *console_key)
  47. {
  48. return xQueueReceive(cli_handle, console_key, BLE_RX_TIMEOUT);
  49. }
  50. static esp_console_cmd_t cmds[] = {
  51. {
  52. .command = "key",
  53. .help = "",
  54. .func = enter_passkey_handler,
  55. },
  56. };
  57. static int ble_register_cli(void)
  58. {
  59. int cmds_num = sizeof(cmds) / sizeof(esp_console_cmd_t);
  60. int i;
  61. for (i = 0; i < cmds_num; i++) {
  62. esp_console_cmd_register(&cmds[i]);
  63. }
  64. return 0;
  65. }
  66. static void scli_task(void *arg)
  67. {
  68. int uart_num = (int) arg;
  69. uint8_t linebuf[256];
  70. int i, cmd_ret;
  71. esp_err_t ret;
  72. QueueHandle_t uart_queue;
  73. uart_event_t event;
  74. uart_driver_install(uart_num, 256, 0, 8, &uart_queue, 0);
  75. /* Initialize the console */
  76. esp_console_config_t console_config = {
  77. .max_cmdline_args = 8,
  78. .max_cmdline_length = 256,
  79. };
  80. esp_console_init(&console_config);
  81. while (!stop) {
  82. i = 0;
  83. memset(linebuf, 0, sizeof(linebuf));
  84. do {
  85. ret = xQueueReceive(uart_queue, (void * )&event, (TickType_t)portMAX_DELAY);
  86. if (ret != pdPASS) {
  87. if (stop == 1) {
  88. break;
  89. } else {
  90. continue;
  91. }
  92. }
  93. if (event.type == UART_DATA) {
  94. while (uart_read_bytes(uart_num, (uint8_t *) &linebuf[i], 1, 0)) {
  95. if (linebuf[i] == '\r') {
  96. uart_write_bytes(uart_num, "\r\n", 2);
  97. } else {
  98. uart_write_bytes(uart_num, (char *) &linebuf[i], 1);
  99. }
  100. i++;
  101. }
  102. }
  103. } while ((i < 255) && linebuf[i - 1] != '\r');
  104. if (stop) {
  105. break;
  106. }
  107. /* Remove the truncating \r\n */
  108. linebuf[strlen((char *)linebuf) - 1] = '\0';
  109. ret = esp_console_run((char *) linebuf, &cmd_ret);
  110. if (ret < 0) {
  111. break;
  112. }
  113. }
  114. vTaskDelete(NULL);
  115. }
  116. int scli_init(void)
  117. {
  118. /* Register CLI "key <value>" to accept input from user during pairing */
  119. ble_register_cli();
  120. xTaskCreate(scli_task, "scli_cli", 4096, (void *) 0, 3, &cli_task);
  121. if (cli_task == NULL) {
  122. return ESP_FAIL;
  123. }
  124. cli_handle = xQueueCreate( 1, sizeof(int) );
  125. if (cli_handle == NULL) {
  126. return ESP_FAIL;
  127. }
  128. return ESP_OK;
  129. }