cli_commands.c 13 KB

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