cli_commands.c 13 KB

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