cli_commands.c 13 KB

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