scli.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.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 "esp_vfs_dev.h"
  13. #include "linenoise/linenoise.h"
  14. #include <freertos/FreeRTOS.h>
  15. #include <freertos/task.h>
  16. #include <freertos/queue.h>
  17. #include <driver/uart.h>
  18. #define BLE_RX_TIMEOUT (30000 / portTICK_PERIOD_MS)
  19. #define BLE_RX_PARAM (10000 / portTICK_PERIOD_MS)
  20. #define YES_NO_PARAM (5000 / portTICK_PERIOD_MS)
  21. static QueueHandle_t cli_handle;
  22. static int key[2];
  23. static int conn_param[10];
  24. static int mtu;
  25. #define CONSOLE_PROMPT_LEN_MAX (32)
  26. typedef enum {
  27. CONSOLE_REPL_STATE_DEINIT,
  28. CONSOLE_REPL_STATE_INIT,
  29. CONSOLE_REPL_STATE_START,
  30. } repl_state_t;
  31. static int conn_param_handler(int argc, char *argv[])
  32. {
  33. ESP_LOGI("Conn param Arguments entered", "%d", argc);
  34. if (argc != 7) {
  35. return -1;
  36. }
  37. sscanf(argv[1], "%d", &conn_param[0]);
  38. sscanf(argv[2], "%d", &conn_param[1]);
  39. sscanf(argv[3], "%d", &conn_param[2]);
  40. sscanf(argv[4], "%d", &conn_param[3]);
  41. sscanf(argv[5], "%d", &conn_param[4]);
  42. sscanf(argv[6], "%d", &conn_param[5]);
  43. ESP_LOGI("You entered", "%s %d %d %d %d %d %d", argv[0], conn_param[0], conn_param[1],
  44. conn_param[2], conn_param[3], conn_param[4], conn_param[5]);
  45. xQueueSend(cli_handle, &conn_param[0], 500 / portTICK_PERIOD_MS);
  46. return 0;
  47. }
  48. static int conn_mtu_handler(int argc, char *argv[])
  49. {
  50. ESP_LOGI("MTU Arguments entered", "%d", argc);
  51. if (argc != 2) {
  52. return -1;
  53. }
  54. sscanf(argv[1], "%d", &mtu);
  55. ESP_LOGI("You entered", "%s %d", argv[0], mtu);
  56. xQueueSend(cli_handle, &mtu, 500 / portTICK_PERIOD_MS);
  57. return 0;
  58. }
  59. static int throughput_demo_handler(int argc, char *argv[])
  60. {
  61. char pkey[8];
  62. if (argc != 3) {
  63. return -1;
  64. }
  65. sscanf(argv[1], "%s", pkey);
  66. if (strcmp(pkey, "read") == 0) {
  67. key[0] = 1;
  68. } else if (strcmp(pkey, "write") == 0) {
  69. key[0] = 2;
  70. } else if (strcmp(pkey, "notify") == 0) {
  71. key[0] = 3;
  72. } else {
  73. key[0] = 0;
  74. }
  75. sscanf(argv[2], "%d", &key[1]);
  76. ESP_LOGI("Throughput demo handler", "%s %s %d", argv[0], argv[1], key[1]);
  77. xQueueSend(cli_handle, &key[0], 500 / portTICK_PERIOD_MS);
  78. return 0;
  79. }
  80. static int yesno_handler(int argc, char *argv[])
  81. {
  82. char yesno[4];
  83. bool yes;
  84. if (argc != 2) {
  85. return -1;
  86. }
  87. sscanf(argv[1], "%s", yesno);
  88. if (strcmp(yesno, "Yes") || strcmp (yesno, "YES") || strcmp(yesno, "yes")) {
  89. yes = 1;
  90. } else {
  91. yes = 0;
  92. }
  93. ESP_LOGI("User entered", "%s %s", argv[0], yesno);
  94. xQueueSend(cli_handle, &yes, 500 / portTICK_PERIOD_MS);
  95. return 0;
  96. }
  97. int scli_receive_yesno(bool *console_key)
  98. {
  99. return xQueueReceive(cli_handle, console_key, YES_NO_PARAM);
  100. }
  101. int scli_receive_key(int *console_key)
  102. {
  103. return xQueueReceive(cli_handle, console_key, BLE_RX_PARAM);
  104. }
  105. int cli_receive_key(int *console_key)
  106. {
  107. return xQueueReceive(cli_handle, console_key, BLE_RX_TIMEOUT);
  108. }
  109. void scli_reset_queue(void)
  110. {
  111. xQueueReset(cli_handle);
  112. }
  113. static esp_console_cmd_t cmds[] = {
  114. {
  115. .command = "conn",
  116. .help = "Set connection parameters: min itvl, max itvl, latency,"
  117. "supervision timeout, min CE, max CE",
  118. .func = &conn_param_handler,
  119. },
  120. {
  121. .command = "MTU",
  122. .help = "Set MTU value",
  123. .func = &conn_mtu_handler,
  124. },
  125. {
  126. .command = "throughput",
  127. .help = "Enter read/write/notify and time (in seconds)",
  128. .func = &throughput_demo_handler,
  129. },
  130. {
  131. .command = "Insert",
  132. .help = "Enter Insert Yes for YES or Insert No for NO",
  133. .func = &yesno_handler,
  134. },
  135. };
  136. void ble_register_cli(void)
  137. {
  138. int cmds_num = sizeof(cmds) / sizeof(esp_console_cmd_t);
  139. int i;
  140. for (i = 0; i < cmds_num; i++) {
  141. esp_console_cmd_register(&cmds[i]);
  142. }
  143. cli_handle = xQueueCreate( 1, sizeof(int) * 6);
  144. if (cli_handle == NULL) {
  145. return;
  146. }
  147. ESP_LOGI("CLI", "BLE CLI registered ");
  148. return;
  149. }