cli_commands.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "cli_commands.h"
  2. #include <api-hal.h>
  3. #include <api-hal-gpio.h>
  4. #include <rtc.h>
  5. #include <task-control-block.h>
  6. #include <time.h>
  7. #include <notification/notification-messages.h>
  8. void cli_command_help(Cli* cli, string_t args, void* context) {
  9. (void)args;
  10. printf("Commands we have:");
  11. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  12. // Get the middle element
  13. CliCommandTree_it_t it_mid;
  14. uint8_t cmd_num = CliCommandTree_size(cli->commands);
  15. uint8_t i = cmd_num / 2 + cmd_num % 2;
  16. for(CliCommandTree_it(it_mid, cli->commands); i; --i, CliCommandTree_next(it_mid))
  17. ;
  18. // Use 2 iterators from start and middle to show 2 columns
  19. CliCommandTree_it_t it_i;
  20. CliCommandTree_it_t it_j;
  21. for(CliCommandTree_it(it_i, cli->commands), CliCommandTree_it_set(it_j, it_mid);
  22. !CliCommandTree_it_equal_p(it_i, it_mid);
  23. CliCommandTree_next(it_i), CliCommandTree_next(it_j)) {
  24. CliCommandTree_itref_t* ref = CliCommandTree_ref(it_i);
  25. printf("\r\n");
  26. printf("%-30s", string_get_cstr(ref->key_ptr[0]));
  27. ref = CliCommandTree_ref(it_j);
  28. printf(string_get_cstr(ref->key_ptr[0]));
  29. };
  30. furi_check(osMutexRelease(cli->mutex) == osOK);
  31. if(string_size(args) > 0) {
  32. cli_nl();
  33. printf("Also I have no clue what '");
  34. printf(string_get_cstr(args));
  35. printf("' is.");
  36. }
  37. }
  38. void cli_command_version(Cli* cli, string_t args, void* context) {
  39. (void)args;
  40. (void)context;
  41. printf("Bootloader\r\n");
  42. cli_print_version(api_hal_version_get_boot_version());
  43. printf("Firmware\r\n");
  44. cli_print_version(api_hal_version_get_fw_version());
  45. }
  46. void cli_command_uuid(Cli* cli, string_t args, void* context) {
  47. (void)args;
  48. (void)context;
  49. size_t uid_size = api_hal_uid_size();
  50. const uint8_t* uid = api_hal_uid();
  51. string_t byte_str;
  52. string_init(byte_str);
  53. string_cat_printf(byte_str, "UID:");
  54. for(size_t i = 0; i < uid_size; i++) {
  55. uint8_t uid_byte = uid[i];
  56. string_cat_printf(byte_str, "%02X", uid_byte);
  57. }
  58. printf(string_get_cstr(byte_str));
  59. }
  60. void cli_command_date(Cli* cli, string_t args, void* context) {
  61. RTC_DateTypeDef date;
  62. RTC_TimeTypeDef time;
  63. // TODO add get_datetime to core, not use HAL here
  64. // READ ORDER MATTERS! Time then date.
  65. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  66. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  67. string_t datetime_str;
  68. string_init(datetime_str);
  69. string_cat_printf(datetime_str, "%.2d:%.2d:%.2d ", time.Hours, time.Minutes, time.Seconds);
  70. string_cat_printf(datetime_str, "%.2d-%.2d-%.2d", date.Month, date.Date, 2000 + date.Year);
  71. printf(string_get_cstr(datetime_str));
  72. string_clear(datetime_str);
  73. }
  74. void cli_command_log(Cli* cli, string_t args, void* context) {
  75. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  76. printf("Press any key to stop...\r\n");
  77. cli_getc(cli);
  78. furi_stdglue_set_global_stdout_callback(NULL);
  79. }
  80. void cli_command_hw_info(Cli* cli, string_t args, void* context) {
  81. printf(
  82. "%-20s %d.F%dB%dC%d\r\n",
  83. "HW version:",
  84. api_hal_version_get_hw_version(),
  85. api_hal_version_get_hw_target(),
  86. api_hal_version_get_hw_body(),
  87. api_hal_version_get_hw_connect());
  88. time_t time = api_hal_version_get_hw_timestamp();
  89. char time_string[26] = "";
  90. ctime_r(&time, time_string);
  91. if(time_string[strlen(time_string) - 1] == '\n') {
  92. time_string[strlen(time_string) - 1] = '\0';
  93. }
  94. printf("%-20s %s\r\n", "Production date:", time_string);
  95. const char* name = api_hal_version_get_name_ptr();
  96. if(name) {
  97. printf("%-20s %s", "Name:", name);
  98. }
  99. }
  100. void cli_command_vibro(Cli* cli, string_t args, void* context) {
  101. if(!string_cmp(args, "0")) {
  102. NotificationApp* notification = furi_record_open("notification");
  103. notification_message_block(notification, &sequence_reset_vibro);
  104. furi_record_close("notification");
  105. } else if(!string_cmp(args, "1")) {
  106. NotificationApp* notification = furi_record_open("notification");
  107. notification_message_block(notification, &sequence_set_vibro_on);
  108. furi_record_close("notification");
  109. } else {
  110. cli_print_usage("vibro", "<1|0>", string_get_cstr(args));
  111. }
  112. }
  113. void cli_command_led(Cli* cli, string_t args, void* context) {
  114. // Get first word as light name
  115. NotificationMessage notification_led_message;
  116. string_t light_name;
  117. string_init(light_name);
  118. size_t ws = string_search_char(args, ' ');
  119. if(ws == STRING_FAILURE) {
  120. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  121. string_clear(light_name);
  122. return;
  123. } else {
  124. string_set_n(light_name, args, 0, ws);
  125. string_right(args, ws);
  126. string_strim(args);
  127. }
  128. // Check light name
  129. if(!string_cmp(light_name, "r")) {
  130. notification_led_message.type = NotificationMessageTypeLedRed;
  131. } else if(!string_cmp(light_name, "g")) {
  132. notification_led_message.type = NotificationMessageTypeLedGreen;
  133. } else if(!string_cmp(light_name, "b")) {
  134. notification_led_message.type = NotificationMessageTypeLedBlue;
  135. } else if(!string_cmp(light_name, "bl")) {
  136. notification_led_message.type = NotificationMessageTypeLedDisplay;
  137. } else {
  138. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  139. string_clear(light_name);
  140. return;
  141. }
  142. string_clear(light_name);
  143. // Read light value from the rest of the string
  144. char* end_ptr;
  145. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  146. if(!(value < 256 && *end_ptr == '\0')) {
  147. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  148. return;
  149. }
  150. // Set led value
  151. notification_led_message.data.led.value = value;
  152. // Form notification sequence
  153. const NotificationSequence notification_sequence = {
  154. &notification_led_message,
  155. NULL,
  156. };
  157. // Send notification
  158. NotificationApp* notification = furi_record_open("notification");
  159. notification_internal_message_block(notification, &notification_sequence);
  160. furi_record_close("notification");
  161. }
  162. void cli_command_gpio_set(Cli* cli, string_t args, void* context) {
  163. char pin_names[][4] = {
  164. "PC0",
  165. "PC1",
  166. "PC3",
  167. "PB2",
  168. "PB3",
  169. "PA4",
  170. "PA6",
  171. "PA7",
  172. #ifdef DEBUG
  173. "PA0",
  174. "PB7",
  175. "PB8",
  176. "PB9"
  177. #endif
  178. };
  179. GpioPin gpio[] = {
  180. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  181. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  182. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  183. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  184. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  185. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  186. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  187. {.port = GPIOA, .pin = LL_GPIO_PIN_7},
  188. #ifdef DEBUG
  189. {.port = GPIOA, .pin = LL_GPIO_PIN_0}, // IR_RX (PA0)
  190. {.port = GPIOB, .pin = LL_GPIO_PIN_7}, // UART RX (PB7)
  191. {.port = GPIOB, .pin = LL_GPIO_PIN_8}, // SPEAKER (PB8)
  192. {.port = GPIOB, .pin = LL_GPIO_PIN_9}, // IR_TX (PB9)
  193. #endif
  194. };
  195. uint8_t num = 0;
  196. bool pin_found = false;
  197. // Get first word as pin name
  198. string_t pin_name;
  199. string_init(pin_name);
  200. size_t ws = string_search_char(args, ' ');
  201. if(ws == STRING_FAILURE) {
  202. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  203. string_clear(pin_name);
  204. return;
  205. } else {
  206. string_set_n(pin_name, args, 0, ws);
  207. string_right(args, ws);
  208. string_strim(args);
  209. }
  210. // Search correct pin name
  211. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  212. if(!string_cmp(pin_name, pin_names[num])) {
  213. pin_found = true;
  214. break;
  215. }
  216. }
  217. if(!pin_found) {
  218. printf("Wrong pin name. Available pins: ");
  219. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  220. printf("%s ", pin_names[i]);
  221. }
  222. string_clear(pin_name);
  223. return;
  224. }
  225. string_clear(pin_name);
  226. // Read "0" or "1" as second argument to set or reset pin
  227. if(!string_cmp(args, "0")) {
  228. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  229. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  230. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  231. } else if(!string_cmp(args, "1")) {
  232. #ifdef DEBUG
  233. if(num == 8) { // PA0
  234. printf(
  235. "Setting PA0 pin HIGH with TSOP connected can damage IR receiver. Are you sure you want to continue? (y/n)?\r\n");
  236. char c = cli_getc(cli);
  237. if(c != 'y' || c != 'Y') {
  238. printf("Cancelled.\r\n");
  239. return;
  240. }
  241. }
  242. #endif
  243. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  244. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  245. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  246. } else {
  247. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  248. }
  249. return;
  250. }
  251. void cli_command_os_info(Cli* cli, string_t args, void* context) {
  252. const uint8_t threads_num_max = 32;
  253. osThreadId_t threads_id[threads_num_max];
  254. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  255. printf("Free HEAP size: %d\r\n", xPortGetFreeHeapSize());
  256. printf("Minimum heap size: %d\r\n", xPortGetMinimumEverFreeHeapSize());
  257. printf("%d threads in total:\r\n", thread_num);
  258. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack free");
  259. for(uint8_t i = 0; i < thread_num; i++) {
  260. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  261. printf(
  262. "%-20s 0x%-12lx %-14ld %ld\r\n",
  263. osThreadGetName(threads_id[i]),
  264. (uint32_t)tcb->pxStack,
  265. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  266. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  267. }
  268. return;
  269. }
  270. void cli_commands_init(Cli* cli) {
  271. cli_add_command(cli, "help", cli_command_help, NULL);
  272. cli_add_command(cli, "?", cli_command_help, NULL);
  273. cli_add_command(cli, "version", cli_command_version, NULL);
  274. cli_add_command(cli, "!", cli_command_version, NULL);
  275. cli_add_command(cli, "uid", cli_command_uuid, NULL);
  276. cli_add_command(cli, "date", cli_command_date, NULL);
  277. cli_add_command(cli, "log", cli_command_log, NULL);
  278. cli_add_command(cli, "hw_info", cli_command_hw_info, NULL);
  279. cli_add_command(cli, "vibro", cli_command_vibro, NULL);
  280. cli_add_command(cli, "led", cli_command_led, NULL);
  281. cli_add_command(cli, "gpio_set", cli_command_gpio_set, NULL);
  282. cli_add_command(cli, "os_info", cli_command_os_info, NULL);
  283. }