cli_commands.c 12 KB

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