No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

console.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * console.c
  3. *
  4. * Copyright (c) 2022 - 2024 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <inttypes.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include "config.h"
  23. #include "log.h"
  24. #include "util.h"
  25. #include "usb_cdc.h"
  26. #include "hw_id.h"
  27. #include "console.h"
  28. #define CNSL_BUFF_SIZE 64
  29. #define CNSL_REPEAT_MS 500
  30. #define DEV_AUTO_CONNECT(s) { \
  31. ble_scan(BLE_SCAN_OFF); \
  32. bd_addr_t addr; \
  33. bd_addr_type_t type; \
  34. const char *foo = s; \
  35. sscanf(foo, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu", \
  36. &addr[0], &addr[1], &addr[2], &addr[3], \
  37. &addr[4], &addr[5], &type); \
  38. ble_connect(addr, type); \
  39. while (!ble_is_connected()) { \
  40. sleep_ms(1); \
  41. } \
  42. }
  43. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  44. static uint32_t cnsl_buff_pos = 0;
  45. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  46. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  47. static bool repeat_command = false;
  48. static uint32_t last_repeat_time = 0;
  49. static void cnsl_interpret(const char *line) {
  50. if (strlen(line) == 0) {
  51. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  52. // repeat last command once
  53. println("repeating command \"%s\"", cnsl_last_command);
  54. cnsl_interpret(cnsl_last_command);
  55. println();
  56. }
  57. return;
  58. } else if (strcmp(line, "repeat") == 0) {
  59. if (!repeat_command) {
  60. // mark last command to be repeated multiple times
  61. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  62. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  63. repeat_command = true;
  64. } else {
  65. // stop repeating
  66. repeat_command = false;
  67. }
  68. } else if ((strcmp(line, "help") == 0)
  69. || (strcmp(line, "h") == 0)
  70. || (strcmp(line, "?") == 0)) {
  71. println("Dispensy Firmware Usage:");
  72. println("");
  73. println(" reset - reset back into this firmware");
  74. println(" \\x18 - reset to bootloader");
  75. println(" type - print hardware type");
  76. println(" id - print hardware ID");
  77. println("");
  78. println("Press Enter with no input to repeat last command.");
  79. println("Use repeat to continuously execute last command.");
  80. println("Stop this by calling repeat again.");
  81. } else if (strcmp(line, "reset") == 0) {
  82. reset_to_main();
  83. } else if (strcmp(line, "type") == 0) {
  84. hw_id_read();
  85. debug("HW Type: %d", hw_type());
  86. } else if (strcmp(line, "id") == 0) {
  87. hw_id_read();
  88. debug("HW ID: %d", hw_id());
  89. } else {
  90. println("unknown command \"%s\"", line);
  91. }
  92. println();
  93. }
  94. void cnsl_init(void) {
  95. cnsl_buff_pos = 0;
  96. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  97. cnsl_line_buff[i] = '\0';
  98. cnsl_last_command[i] = '\0';
  99. cnsl_repeated_command[i] = '\0';
  100. }
  101. }
  102. static int32_t cnsl_find_line_end(void) {
  103. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  104. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  105. return i;
  106. }
  107. }
  108. return -1;
  109. }
  110. void cnsl_run(void) {
  111. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  112. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  113. uint32_t now = to_ms_since_boot(get_absolute_time());
  114. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  115. println("repeating command \"%s\"", cnsl_repeated_command);
  116. cnsl_interpret(cnsl_repeated_command);
  117. println();
  118. last_repeat_time = now;
  119. }
  120. } else {
  121. if (repeat_command) {
  122. println("nothing to repeat");
  123. }
  124. repeat_command = false;
  125. }
  126. }
  127. void cnsl_handle_input(const void *buf, size_t len) {
  128. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  129. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  130. cnsl_init();
  131. }
  132. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  133. cnsl_buff_pos += len;
  134. // handle backspace and local echo
  135. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  136. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  137. if (i > 0) {
  138. // overwrite previous character and backspace
  139. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  140. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  141. }
  142. cnsl_buff_pos -= 2;
  143. } else {
  144. // just remove the backspace
  145. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  146. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  147. }
  148. cnsl_buff_pos -= 1;
  149. }
  150. usb_cdc_write((const uint8_t *)"\b \b", 3);
  151. #ifndef NDEBUG
  152. serial_write((const uint8_t *)"\b \b", 3);
  153. #endif
  154. // check for another backspace in this space
  155. i--;
  156. } else {
  157. usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
  158. #ifndef NDEBUG
  159. serial_write((const uint8_t *)(cnsl_line_buff + i), 1);
  160. #endif
  161. }
  162. }
  163. int32_t line_len = cnsl_find_line_end();
  164. if (line_len < 0) {
  165. // user has not pressed enter yet
  166. return;
  167. }
  168. // convert line to C-style string
  169. cnsl_line_buff[line_len] = '\0';
  170. cnsl_interpret(cnsl_line_buff);
  171. // store command for eventual repeats
  172. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  173. // clear string and move following data over
  174. uint32_t cnt = line_len + 1;
  175. if (cnsl_line_buff[line_len + 1] == '\n') {
  176. cnt++;
  177. }
  178. memset(cnsl_line_buff, '\0', cnt);
  179. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  180. cnsl_buff_pos -= cnt;
  181. }