cli_commands.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "cli_commands.h"
  2. #include <furi_hal.h>
  3. #include <furi_hal_gpio.h>
  4. #include <furi_hal_info.h>
  5. #include <task_control_block.h>
  6. #include <time.h>
  7. #include <notification/notification_messages.h>
  8. #include <loader/loader.h>
  9. #include <stream_buffer.h>
  10. // Close to ISO, `date +'%Y-%m-%d %H:%M:%S %u'`
  11. #define CLI_DATE_FORMAT "%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d"
  12. void cli_command_device_info_callback(const char* key, const char* value, bool last, void* context) {
  13. printf("%-24s: %s\r\n", key, value);
  14. }
  15. /*
  16. * Device Info Command
  17. * This command is intended to be used by humans
  18. */
  19. void cli_command_device_info(Cli* cli, string_t args, void* context) {
  20. furi_hal_info_get(cli_command_device_info_callback, context);
  21. }
  22. void cli_command_help(Cli* cli, string_t args, void* context) {
  23. (void)args;
  24. printf("Commands we have:");
  25. // Command count
  26. const size_t commands_count = CliCommandTree_size(cli->commands);
  27. const size_t commands_count_mid = commands_count / 2 + commands_count % 2;
  28. // Use 2 iterators from start and middle to show 2 columns
  29. CliCommandTree_it_t it_left;
  30. CliCommandTree_it(it_left, cli->commands);
  31. CliCommandTree_it_t it_right;
  32. CliCommandTree_it(it_right, cli->commands);
  33. for(size_t i = 0; i < commands_count_mid; i++) CliCommandTree_next(it_right);
  34. // Iterate throw tree
  35. for(size_t i = 0; i < commands_count_mid; i++) {
  36. printf("\r\n");
  37. // Left Column
  38. if(!CliCommandTree_end_p(it_left)) {
  39. printf("%-30s", string_get_cstr(*CliCommandTree_ref(it_left)->key_ptr));
  40. CliCommandTree_next(it_left);
  41. }
  42. // Right Column
  43. if(!CliCommandTree_end_p(it_right)) {
  44. printf("%s", string_get_cstr(*CliCommandTree_ref(it_right)->key_ptr));
  45. CliCommandTree_next(it_right);
  46. }
  47. };
  48. if(string_size(args) > 0) {
  49. cli_nl();
  50. printf("Also I have no clue what '");
  51. printf("%s", string_get_cstr(args));
  52. printf("' is.");
  53. }
  54. }
  55. void cli_command_date(Cli* cli, string_t args, void* context) {
  56. FuriHalRtcDateTime datetime = {0};
  57. if(string_size(args) > 0) {
  58. uint16_t hours, minutes, seconds, month, day, year, weekday;
  59. int ret = sscanf(
  60. string_get_cstr(args),
  61. "%hu-%hu-%hu %hu:%hu:%hu %hu",
  62. &year,
  63. &month,
  64. &day,
  65. &hours,
  66. &minutes,
  67. &seconds,
  68. &weekday);
  69. // Some variables are going to discard upper byte
  70. // There will be some funky behaviour which is not breaking anything
  71. datetime.hour = hours;
  72. datetime.minute = minutes;
  73. datetime.second = seconds;
  74. datetime.weekday = weekday;
  75. datetime.month = month;
  76. datetime.day = day;
  77. datetime.year = year;
  78. if(ret != 7) {
  79. printf(
  80. "Invalid datetime format, use `%s`. sscanf %d %s",
  81. "%Y-%m-%d %H:%M:%S %u",
  82. ret,
  83. string_get_cstr(args));
  84. return;
  85. }
  86. if(!furi_hal_rtc_validate_datetime(&datetime)) {
  87. printf("Invalid datetime data");
  88. return;
  89. }
  90. furi_hal_rtc_set_datetime(&datetime);
  91. // Verification
  92. furi_hal_rtc_get_datetime(&datetime);
  93. printf(
  94. "New datetime is: " CLI_DATE_FORMAT,
  95. datetime.year,
  96. datetime.month,
  97. datetime.day,
  98. datetime.hour,
  99. datetime.minute,
  100. datetime.second,
  101. datetime.weekday);
  102. } else {
  103. furi_hal_rtc_get_datetime(&datetime);
  104. printf(
  105. CLI_DATE_FORMAT,
  106. datetime.year,
  107. datetime.month,
  108. datetime.day,
  109. datetime.hour,
  110. datetime.minute,
  111. datetime.second,
  112. datetime.weekday);
  113. }
  114. }
  115. #define CLI_COMMAND_LOG_RING_SIZE 2048
  116. #define CLI_COMMAND_LOG_BUFFER_SIZE 64
  117. void cli_command_log_tx_callback(const uint8_t* buffer, size_t size, void* context) {
  118. xStreamBufferSend(context, buffer, size, 0);
  119. }
  120. void cli_command_log(Cli* cli, string_t args, void* context) {
  121. StreamBufferHandle_t ring = xStreamBufferCreate(CLI_COMMAND_LOG_RING_SIZE, 1);
  122. uint8_t buffer[CLI_COMMAND_LOG_BUFFER_SIZE];
  123. furi_hal_console_set_tx_callback(cli_command_log_tx_callback, ring);
  124. printf("Press CTRL+C to stop...\r\n");
  125. while(!cli_cmd_interrupt_received(cli)) {
  126. size_t ret = xStreamBufferReceive(ring, buffer, CLI_COMMAND_LOG_BUFFER_SIZE, 50);
  127. cli_write(cli, buffer, ret);
  128. }
  129. furi_hal_console_set_tx_callback(NULL, NULL);
  130. vStreamBufferDelete(ring);
  131. }
  132. void cli_command_vibro(Cli* cli, string_t args, void* context) {
  133. if(!string_cmp(args, "0")) {
  134. NotificationApp* notification = furi_record_open("notification");
  135. notification_message_block(notification, &sequence_reset_vibro);
  136. furi_record_close("notification");
  137. } else if(!string_cmp(args, "1")) {
  138. NotificationApp* notification = furi_record_open("notification");
  139. notification_message_block(notification, &sequence_set_vibro_on);
  140. furi_record_close("notification");
  141. } else {
  142. cli_print_usage("vibro", "<1|0>", string_get_cstr(args));
  143. }
  144. }
  145. void cli_command_debug(Cli* cli, string_t args, void* context) {
  146. if(!string_cmp(args, "0")) {
  147. furi_hal_rtc_reset_flag(FuriHalRtcFlagDebug);
  148. loader_update_menu();
  149. printf("Debug disabled.");
  150. } else if(!string_cmp(args, "1")) {
  151. furi_hal_rtc_set_flag(FuriHalRtcFlagDebug);
  152. loader_update_menu();
  153. printf("Debug enabled.");
  154. } else {
  155. cli_print_usage("debug", "<1|0>", string_get_cstr(args));
  156. }
  157. }
  158. void cli_command_led(Cli* cli, string_t args, void* context) {
  159. // Get first word as light name
  160. NotificationMessage notification_led_message;
  161. string_t light_name;
  162. string_init(light_name);
  163. size_t ws = string_search_char(args, ' ');
  164. if(ws == STRING_FAILURE) {
  165. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  166. string_clear(light_name);
  167. return;
  168. } else {
  169. string_set_n(light_name, args, 0, ws);
  170. string_right(args, ws);
  171. string_strim(args);
  172. }
  173. // Check light name
  174. if(!string_cmp(light_name, "r")) {
  175. notification_led_message.type = NotificationMessageTypeLedRed;
  176. } else if(!string_cmp(light_name, "g")) {
  177. notification_led_message.type = NotificationMessageTypeLedGreen;
  178. } else if(!string_cmp(light_name, "b")) {
  179. notification_led_message.type = NotificationMessageTypeLedBlue;
  180. } else if(!string_cmp(light_name, "bl")) {
  181. notification_led_message.type = NotificationMessageTypeLedDisplay;
  182. } else {
  183. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  184. string_clear(light_name);
  185. return;
  186. }
  187. string_clear(light_name);
  188. // Read light value from the rest of the string
  189. char* end_ptr;
  190. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  191. if(!(value < 256 && *end_ptr == '\0')) {
  192. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  193. return;
  194. }
  195. // Set led value
  196. notification_led_message.data.led.value = value;
  197. // Form notification sequence
  198. const NotificationSequence notification_sequence = {
  199. &notification_led_message,
  200. NULL,
  201. };
  202. // Send notification
  203. NotificationApp* notification = furi_record_open("notification");
  204. notification_internal_message_block(notification, &notification_sequence);
  205. furi_record_close("notification");
  206. }
  207. void cli_command_gpio_set(Cli* cli, string_t args, void* context) {
  208. char pin_names[][4] = {
  209. "PC0",
  210. "PC1",
  211. "PC3",
  212. "PB2",
  213. "PB3",
  214. "PA4",
  215. "PA6",
  216. "PA7",
  217. #ifdef FURI_DEBUG
  218. "PA0",
  219. "PB7",
  220. "PB8",
  221. "PB9"
  222. #endif
  223. };
  224. GpioPin gpio[] = {
  225. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  226. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  227. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  228. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  229. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  230. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  231. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  232. {.port = GPIOA, .pin = LL_GPIO_PIN_7},
  233. #ifdef FURI_DEBUG
  234. {.port = GPIOA, .pin = LL_GPIO_PIN_0}, // IR_RX (PA0)
  235. {.port = GPIOB, .pin = LL_GPIO_PIN_7}, // UART RX (PB7)
  236. {.port = GPIOB, .pin = LL_GPIO_PIN_8}, // SPEAKER (PB8)
  237. {.port = GPIOB, .pin = LL_GPIO_PIN_9}, // IR_TX (PB9)
  238. #endif
  239. };
  240. uint8_t num = 0;
  241. bool pin_found = false;
  242. // Get first word as pin name
  243. string_t pin_name;
  244. string_init(pin_name);
  245. size_t ws = string_search_char(args, ' ');
  246. if(ws == STRING_FAILURE) {
  247. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  248. string_clear(pin_name);
  249. return;
  250. } else {
  251. string_set_n(pin_name, args, 0, ws);
  252. string_right(args, ws);
  253. string_strim(args);
  254. }
  255. // Search correct pin name
  256. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  257. if(!string_cmp(pin_name, pin_names[num])) {
  258. pin_found = true;
  259. break;
  260. }
  261. }
  262. if(!pin_found) {
  263. printf("Wrong pin name. Available pins: ");
  264. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  265. printf("%s ", pin_names[i]);
  266. }
  267. string_clear(pin_name);
  268. return;
  269. }
  270. string_clear(pin_name);
  271. // Read "0" or "1" as second argument to set or reset pin
  272. if(!string_cmp(args, "0")) {
  273. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  274. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  275. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  276. } else if(!string_cmp(args, "1")) {
  277. #ifdef FURI_DEBUG
  278. if(num == 8) { // PA0
  279. printf(
  280. "Setting PA0 pin HIGH with TSOP connected can damage IR receiver. Are you sure you want to continue? (y/n)?\r\n");
  281. char c = cli_getc(cli);
  282. if(c != 'y' && c != 'Y') {
  283. printf("Cancelled.\r\n");
  284. return;
  285. }
  286. }
  287. #endif
  288. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  289. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  290. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  291. } else {
  292. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  293. }
  294. return;
  295. }
  296. void cli_command_ps(Cli* cli, string_t args, void* context) {
  297. const uint8_t threads_num_max = 32;
  298. osThreadId_t threads_id[threads_num_max];
  299. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  300. printf(
  301. "%-20s %-14s %-8s %-8s %s\r\n", "Name", "Stack start", "Heap", "Stack", "Stack min free");
  302. for(uint8_t i = 0; i < thread_num; i++) {
  303. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  304. printf(
  305. "%-20s 0x%-12lx %-8d %-8ld %-8ld\r\n",
  306. osThreadGetName(threads_id[i]),
  307. (uint32_t)tcb->pxStack,
  308. memmgr_heap_get_thread_memory(threads_id[i]),
  309. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(StackType_t),
  310. osThreadGetStackSpace(threads_id[i]));
  311. }
  312. printf("\r\nTotal: %d", thread_num);
  313. }
  314. void cli_command_free(Cli* cli, string_t args, void* context) {
  315. printf("Free heap size: %d\r\n", memmgr_get_free_heap());
  316. printf("Total heap size: %d\r\n", memmgr_get_total_heap());
  317. printf("Minimum heap size: %d\r\n", memmgr_get_minimum_free_heap());
  318. printf("Maximum heap block: %d\r\n", memmgr_heap_get_max_free_block());
  319. }
  320. void cli_command_free_blocks(Cli* cli, string_t args, void* context) {
  321. memmgr_heap_printf_free_blocks();
  322. }
  323. void cli_command_i2c(Cli* cli, string_t args, void* context) {
  324. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  325. printf("Scanning external i2c on PC0(SCL)/PC1(SDA)\r\n"
  326. "Clock: 100khz, 7bit address\r\n"
  327. "\r\n");
  328. printf(" | 0 1 2 3 4 5 6 7 8 9 A B C D E F\r\n");
  329. printf("--+--------------------------------\r\n");
  330. for(uint8_t row = 0; row < 0x8; row++) {
  331. printf("%x | ", row);
  332. for(uint8_t column = 0; column <= 0xF; column++) {
  333. bool ret = furi_hal_i2c_is_device_ready(
  334. &furi_hal_i2c_handle_external, ((row << 4) + column) << 1, 2);
  335. printf("%c ", ret ? '#' : '-');
  336. }
  337. printf("\r\n");
  338. }
  339. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  340. }
  341. void cli_commands_init(Cli* cli) {
  342. cli_add_command(cli, "!", CliCommandFlagParallelSafe, cli_command_device_info, NULL);
  343. cli_add_command(cli, "device_info", CliCommandFlagParallelSafe, cli_command_device_info, NULL);
  344. cli_add_command(cli, "?", CliCommandFlagParallelSafe, cli_command_help, NULL);
  345. cli_add_command(cli, "help", CliCommandFlagParallelSafe, cli_command_help, NULL);
  346. cli_add_command(cli, "date", CliCommandFlagParallelSafe, cli_command_date, NULL);
  347. cli_add_command(cli, "log", CliCommandFlagParallelSafe, cli_command_log, NULL);
  348. cli_add_command(cli, "debug", CliCommandFlagDefault, cli_command_debug, NULL);
  349. cli_add_command(cli, "ps", CliCommandFlagParallelSafe, cli_command_ps, NULL);
  350. cli_add_command(cli, "free", CliCommandFlagParallelSafe, cli_command_free, NULL);
  351. cli_add_command(cli, "free_blocks", CliCommandFlagParallelSafe, cli_command_free_blocks, NULL);
  352. cli_add_command(cli, "vibro", CliCommandFlagDefault, cli_command_vibro, NULL);
  353. cli_add_command(cli, "led", CliCommandFlagDefault, cli_command_led, NULL);
  354. cli_add_command(cli, "gpio_set", CliCommandFlagDefault, cli_command_gpio_set, NULL);
  355. cli_add_command(cli, "i2c", CliCommandFlagDefault, cli_command_i2c, NULL);
  356. }