scli.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include "esp_log.h"
  22. #include <string.h>
  23. #include <esp_log.h>
  24. #include <esp_console.h>
  25. #include <freertos/FreeRTOS.h>
  26. #include <freertos/task.h>
  27. #include <freertos/queue.h>
  28. #include <driver/uart.h>
  29. #include "bleprph.h"
  30. #define BLE_RX_TIMEOUT (30000 / portTICK_PERIOD_MS)
  31. static TaskHandle_t cli_task;
  32. static QueueHandle_t cli_handle;
  33. static int stop;
  34. static int enter_passkey_handler(int argc, char *argv[])
  35. {
  36. int key;
  37. char pkey[8];
  38. int num;
  39. if (argc != 2) {
  40. return -1;
  41. }
  42. sscanf(argv[1], "%s", pkey);
  43. ESP_LOGI("You entered", "%s %s", argv[0], argv[1]);
  44. num = pkey[0];
  45. if (isalpha(num)) {
  46. if ((strcasecmp(pkey, "Y") == 0) || (strcasecmp(pkey, "Yes") == 0)) {
  47. key = 1;
  48. xQueueSend(cli_handle, &key, 0);
  49. } else {
  50. key = 0;
  51. xQueueSend(cli_handle, &key, 0);
  52. }
  53. } else {
  54. sscanf(pkey, "%d", &key);
  55. xQueueSend(cli_handle, &key, 0);
  56. }
  57. return 0;
  58. }
  59. int scli_receive_key(int *console_key)
  60. {
  61. return xQueueReceive(cli_handle, console_key, BLE_RX_TIMEOUT);
  62. }
  63. static esp_console_cmd_t cmds[] = {
  64. {
  65. .command = "key",
  66. .help = "",
  67. .func = enter_passkey_handler,
  68. },
  69. };
  70. static int ble_register_cli(void)
  71. {
  72. int cmds_num = sizeof(cmds) / sizeof(esp_console_cmd_t);
  73. int i;
  74. for (i = 0; i < cmds_num; i++) {
  75. esp_console_cmd_register(&cmds[i]);
  76. }
  77. return 0;
  78. }
  79. static void scli_task(void *arg)
  80. {
  81. int uart_num = (int) arg;
  82. uint8_t linebuf[256];
  83. int i, cmd_ret;
  84. esp_err_t ret;
  85. QueueHandle_t uart_queue;
  86. uart_event_t event;
  87. uart_driver_install(uart_num, 256, 0, 8, &uart_queue, 0);
  88. /* Initialize the console */
  89. esp_console_config_t console_config = {
  90. .max_cmdline_args = 8,
  91. .max_cmdline_length = 256,
  92. };
  93. esp_console_init(&console_config);
  94. while (!stop) {
  95. i = 0;
  96. memset(linebuf, 0, sizeof(linebuf));
  97. do {
  98. ret = xQueueReceive(uart_queue, (void * )&event, (TickType_t)portMAX_DELAY);
  99. if (ret != pdPASS) {
  100. if (stop == 1) {
  101. break;
  102. } else {
  103. continue;
  104. }
  105. }
  106. if (event.type == UART_DATA) {
  107. while (uart_read_bytes(uart_num, (uint8_t *) &linebuf[i], 1, 0)) {
  108. if (linebuf[i] == '\r') {
  109. uart_write_bytes(uart_num, "\r\n", 2);
  110. } else {
  111. uart_write_bytes(uart_num, (char *) &linebuf[i], 1);
  112. }
  113. i++;
  114. }
  115. }
  116. } while ((i < 255) && linebuf[i - 1] != '\r');
  117. if (stop) {
  118. break;
  119. }
  120. /* Remove the truncating \r\n */
  121. linebuf[strlen((char *)linebuf) - 1] = '\0';
  122. ret = esp_console_run((char *) linebuf, &cmd_ret);
  123. if (ret < 0) {
  124. break;
  125. }
  126. }
  127. vTaskDelete(NULL);
  128. }
  129. int scli_init(void)
  130. {
  131. /* Register CLI "key <value>" to accept input from user during pairing */
  132. ble_register_cli();
  133. xTaskCreate(scli_task, "scli_cli", 4096, (void *) 0, 3, &cli_task);
  134. if (cli_task == NULL) {
  135. return ESP_FAIL;
  136. }
  137. cli_handle = xQueueCreate( 1, sizeof(int) );
  138. if (cli_handle == NULL) {
  139. return ESP_FAIL;
  140. }
  141. return ESP_OK;
  142. }