cli_commands.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. printf("%-20s %s\r", "Production date:", ctime(&time));
  90. const char* name = api_hal_version_get_name_ptr();
  91. if(name) {
  92. printf("%-20s %s", "Name:", name);
  93. }
  94. }
  95. void cli_command_vibro(Cli* cli, string_t args, void* context) {
  96. if(!string_cmp(args, "0")) {
  97. NotificationApp* notification = furi_record_open("notification");
  98. notification_message_block(notification, &sequence_reset_vibro);
  99. furi_record_close("notification");
  100. } else if(!string_cmp(args, "1")) {
  101. NotificationApp* notification = furi_record_open("notification");
  102. notification_message_block(notification, &sequence_set_vibro_on);
  103. furi_record_close("notification");
  104. } else {
  105. cli_print_usage("vibro", "<1|0>", string_get_cstr(args));
  106. }
  107. }
  108. void cli_command_led(Cli* cli, string_t args, void* context) {
  109. // Get first word as light name
  110. NotificationMessage notification_led_message;
  111. string_t light_name;
  112. string_init(light_name);
  113. size_t ws = string_search_char(args, ' ');
  114. if(ws == STRING_FAILURE) {
  115. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  116. string_clear(light_name);
  117. return;
  118. } else {
  119. string_set_n(light_name, args, 0, ws);
  120. string_right(args, ws);
  121. string_strim(args);
  122. }
  123. // Check light name
  124. if(!string_cmp(light_name, "r")) {
  125. notification_led_message.type = NotificationMessageTypeLedRed;
  126. } else if(!string_cmp(light_name, "g")) {
  127. notification_led_message.type = NotificationMessageTypeLedGreen;
  128. } else if(!string_cmp(light_name, "b")) {
  129. notification_led_message.type = NotificationMessageTypeLedBlue;
  130. } else if(!string_cmp(light_name, "bl")) {
  131. notification_led_message.type = NotificationMessageTypeLedDisplay;
  132. } else {
  133. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  134. string_clear(light_name);
  135. return;
  136. }
  137. string_clear(light_name);
  138. // Read light value from the rest of the string
  139. char* end_ptr;
  140. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  141. if(!(value < 256 && *end_ptr == '\0')) {
  142. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  143. return;
  144. }
  145. // Set led value
  146. notification_led_message.data.led.value = value;
  147. // Form notification sequence
  148. const NotificationSequence notification_sequence = {
  149. &notification_led_message,
  150. NULL,
  151. };
  152. // Send notification
  153. NotificationApp* notification = furi_record_open("notification");
  154. notification_internal_message_block(notification, &notification_sequence);
  155. furi_record_close("notification");
  156. }
  157. void cli_command_gpio_set(Cli* cli, string_t args, void* context) {
  158. char pin_names[][4] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  159. GpioPin gpio[] = {
  160. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  161. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  162. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  163. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  164. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  165. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  166. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  167. {.port = GPIOA, .pin = LL_GPIO_PIN_7}};
  168. uint8_t num = 0;
  169. bool pin_found = false;
  170. // Get first word as pin name
  171. string_t pin_name;
  172. string_init(pin_name);
  173. size_t ws = string_search_char(args, ' ');
  174. if(ws == STRING_FAILURE) {
  175. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  176. string_clear(pin_name);
  177. return;
  178. } else {
  179. string_set_n(pin_name, args, 0, ws);
  180. string_right(args, ws);
  181. string_strim(args);
  182. }
  183. // Search correct pin name
  184. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  185. if(!string_cmp(pin_name, pin_names[num])) {
  186. pin_found = true;
  187. break;
  188. }
  189. }
  190. if(!pin_found) {
  191. printf("Wrong pin name. Available pins: ");
  192. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  193. printf("%s ", pin_names[i]);
  194. }
  195. string_clear(pin_name);
  196. return;
  197. }
  198. string_clear(pin_name);
  199. // Read "0" or "1" as second argument to set or reset pin
  200. if(!string_cmp(args, "0")) {
  201. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  202. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  203. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  204. } else if(!string_cmp(args, "1")) {
  205. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  206. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  207. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  208. } else {
  209. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  210. }
  211. return;
  212. }
  213. void cli_command_os_info(Cli* cli, string_t args, void* context) {
  214. const uint8_t threads_num_max = 32;
  215. osThreadId_t threads_id[threads_num_max];
  216. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  217. printf("Free HEAP size: %d\r\n", xPortGetFreeHeapSize());
  218. printf("Minimum heap size: %d\r\n", xPortGetMinimumEverFreeHeapSize());
  219. printf("%d threads in total:\r\n", thread_num);
  220. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack free");
  221. for(uint8_t i = 0; i < thread_num; i++) {
  222. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  223. printf(
  224. "%-20s 0x%-12lx %-14ld %ld\r\n",
  225. osThreadGetName(threads_id[i]),
  226. (uint32_t)tcb->pxStack,
  227. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  228. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  229. }
  230. return;
  231. }
  232. void cli_commands_init(Cli* cli) {
  233. cli_add_command(cli, "help", cli_command_help, NULL);
  234. cli_add_command(cli, "?", cli_command_help, NULL);
  235. cli_add_command(cli, "version", cli_command_version, NULL);
  236. cli_add_command(cli, "!", cli_command_version, NULL);
  237. cli_add_command(cli, "uid", cli_command_uuid, NULL);
  238. cli_add_command(cli, "date", cli_command_date, NULL);
  239. cli_add_command(cli, "log", cli_command_log, NULL);
  240. cli_add_command(cli, "hw_info", cli_command_hw_info, NULL);
  241. cli_add_command(cli, "vibro", cli_command_vibro, NULL);
  242. cli_add_command(cli, "led", cli_command_led, NULL);
  243. cli_add_command(cli, "gpio_set", cli_command_gpio_set, NULL);
  244. cli_add_command(cli, "os_info", cli_command_os_info, NULL);
  245. }