cli_commands.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  164. GpioPin gpio[] = {
  165. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  166. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  167. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  168. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  169. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  170. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  171. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  172. {.port = GPIOA, .pin = LL_GPIO_PIN_7}};
  173. uint8_t num = 0;
  174. bool pin_found = false;
  175. // Get first word as pin name
  176. string_t pin_name;
  177. string_init(pin_name);
  178. size_t ws = string_search_char(args, ' ');
  179. if(ws == STRING_FAILURE) {
  180. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  181. string_clear(pin_name);
  182. return;
  183. } else {
  184. string_set_n(pin_name, args, 0, ws);
  185. string_right(args, ws);
  186. string_strim(args);
  187. }
  188. // Search correct pin name
  189. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  190. if(!string_cmp(pin_name, pin_names[num])) {
  191. pin_found = true;
  192. break;
  193. }
  194. }
  195. if(!pin_found) {
  196. printf("Wrong pin name. Available pins: ");
  197. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  198. printf("%s ", pin_names[i]);
  199. }
  200. string_clear(pin_name);
  201. return;
  202. }
  203. string_clear(pin_name);
  204. // Read "0" or "1" as second argument to set or reset pin
  205. if(!string_cmp(args, "0")) {
  206. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  207. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  208. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  209. } else if(!string_cmp(args, "1")) {
  210. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  211. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  212. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  213. } else {
  214. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  215. }
  216. return;
  217. }
  218. void cli_command_os_info(Cli* cli, string_t args, void* context) {
  219. const uint8_t threads_num_max = 32;
  220. osThreadId_t threads_id[threads_num_max];
  221. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  222. printf("Free HEAP size: %d\r\n", xPortGetFreeHeapSize());
  223. printf("Minimum heap size: %d\r\n", xPortGetMinimumEverFreeHeapSize());
  224. printf("%d threads in total:\r\n", thread_num);
  225. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack free");
  226. for(uint8_t i = 0; i < thread_num; i++) {
  227. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  228. printf(
  229. "%-20s 0x%-12lx %-14ld %ld\r\n",
  230. osThreadGetName(threads_id[i]),
  231. (uint32_t)tcb->pxStack,
  232. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  233. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  234. }
  235. return;
  236. }
  237. void cli_commands_init(Cli* cli) {
  238. cli_add_command(cli, "help", cli_command_help, NULL);
  239. cli_add_command(cli, "?", cli_command_help, NULL);
  240. cli_add_command(cli, "version", cli_command_version, NULL);
  241. cli_add_command(cli, "!", cli_command_version, NULL);
  242. cli_add_command(cli, "uid", cli_command_uuid, NULL);
  243. cli_add_command(cli, "date", cli_command_date, NULL);
  244. cli_add_command(cli, "log", cli_command_log, NULL);
  245. cli_add_command(cli, "hw_info", cli_command_hw_info, NULL);
  246. cli_add_command(cli, "vibro", cli_command_vibro, NULL);
  247. cli_add_command(cli, "led", cli_command_led, NULL);
  248. cli_add_command(cli, "gpio_set", cli_command_gpio_set, NULL);
  249. cli_add_command(cli, "os_info", cli_command_os_info, NULL);
  250. }