scli.c 4.2 KB

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