cli_commands.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. void cli_command_help(string_t args, void* context) {
  7. (void)args;
  8. Cli* cli = context;
  9. printf("Commands we have:");
  10. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  11. CliCommandDict_it_t it;
  12. for(CliCommandDict_it(it, cli->commands); !CliCommandDict_end_p(it); CliCommandDict_next(it)) {
  13. CliCommandDict_itref_t* ref = CliCommandDict_ref(it);
  14. printf(" ");
  15. printf(string_get_cstr(ref->key));
  16. };
  17. furi_check(osMutexRelease(cli->mutex) == osOK);
  18. if(string_size(args) > 0) {
  19. cli_nl();
  20. printf("Also I have no clue what '");
  21. printf(string_get_cstr(args));
  22. printf("' is.");
  23. }
  24. }
  25. void cli_command_version(string_t args, void* context) {
  26. (void)args;
  27. (void)context;
  28. printf("Bootloader\r\n");
  29. cli_print_version(api_hal_version_get_boot_version());
  30. printf("Firmware\r\n");
  31. cli_print_version(api_hal_version_get_fw_version());
  32. }
  33. void cli_command_uuid(string_t args, void* context) {
  34. (void)args;
  35. (void)context;
  36. size_t uid_size = api_hal_uid_size();
  37. const uint8_t* uid = api_hal_uid();
  38. string_t byte_str;
  39. string_init(byte_str);
  40. string_cat_printf(byte_str, "UID:");
  41. for(size_t i = 0; i < uid_size; i++) {
  42. uint8_t uid_byte = uid[i];
  43. string_cat_printf(byte_str, "%02X", uid_byte);
  44. }
  45. printf(string_get_cstr(byte_str));
  46. }
  47. void cli_command_date(string_t args, void* context) {
  48. RTC_DateTypeDef date;
  49. RTC_TimeTypeDef time;
  50. // TODO add get_datetime to core, not use HAL here
  51. // READ ORDER MATTERS! Time then date.
  52. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  53. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  54. string_t datetime_str;
  55. string_init(datetime_str);
  56. string_cat_printf(datetime_str, "%.2d:%.2d:%.2d ", time.Hours, time.Minutes, time.Seconds);
  57. string_cat_printf(datetime_str, "%.2d-%.2d-%.2d", date.Month, date.Date, 2000 + date.Year);
  58. printf(string_get_cstr(datetime_str));
  59. string_clear(datetime_str);
  60. }
  61. void cli_command_log(string_t args, void* context) {
  62. Cli* cli = context;
  63. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  64. printf("Press any key to stop...\r\n");
  65. cli_getc(cli);
  66. furi_stdglue_set_global_stdout_callback(NULL);
  67. }
  68. void cli_command_vibro(string_t args, void* context) {
  69. if(!string_cmp(args, "0")) {
  70. api_hal_vibro_on(false);
  71. } else if(!string_cmp(args, "1")) {
  72. api_hal_vibro_on(true);
  73. } else {
  74. printf("Wrong input");
  75. }
  76. }
  77. void cli_command_led(string_t args, void* context) {
  78. // Get first word as light name
  79. Light light;
  80. string_t light_name;
  81. string_init(light_name);
  82. size_t ws = string_search_char(args, ' ');
  83. if(ws == STRING_FAILURE) {
  84. printf("Wrong input");
  85. string_clear(light_name);
  86. return;
  87. } else {
  88. string_set_n(light_name, args, 0, ws);
  89. string_right(args, ws);
  90. string_strim(args);
  91. }
  92. // Check light name
  93. if(!string_cmp(light_name, "r")) {
  94. light = LightRed;
  95. } else if(!string_cmp(light_name, "g")) {
  96. light = LightGreen;
  97. } else if(!string_cmp(light_name, "b")) {
  98. light = LightBlue;
  99. } else if(!string_cmp(light_name, "bl")) {
  100. light = LightBacklight;
  101. } else {
  102. printf("Wrong argument");
  103. string_clear(light_name);
  104. return;
  105. }
  106. string_clear(light_name);
  107. // Read light value from the rest of the string
  108. char* end_ptr;
  109. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  110. if(!(value < 256 && *end_ptr == '\0')) {
  111. printf("Wrong argument");
  112. return;
  113. }
  114. api_hal_light_set(light, value);
  115. }
  116. void cli_command_gpio_set(string_t args, void* context) {
  117. char pin_names[][4] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  118. GpioPin gpio[] = {
  119. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  120. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  121. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  122. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  123. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  124. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  125. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  126. {.port = GPIOA, .pin = LL_GPIO_PIN_7}};
  127. uint8_t num = 0;
  128. bool pin_found = false;
  129. // Get first word as pin name
  130. string_t pin_name;
  131. string_init(pin_name);
  132. size_t ws = string_search_char(args, ' ');
  133. if(ws == STRING_FAILURE) {
  134. printf("Wrong input. Correct usage: gpio_set <pin_name> <0|1>");
  135. string_clear(pin_name);
  136. return;
  137. } else {
  138. string_set_n(pin_name, args, 0, ws);
  139. string_right(args, ws);
  140. string_strim(args);
  141. }
  142. // Search correct pin name
  143. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  144. if(!string_cmp(pin_name, pin_names[num])) {
  145. pin_found = true;
  146. break;
  147. }
  148. }
  149. if(!pin_found) {
  150. printf("Wrong pin name. Available pins: ");
  151. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  152. printf("%s ", pin_names[i]);
  153. }
  154. string_clear(pin_name);
  155. return;
  156. }
  157. string_clear(pin_name);
  158. // Read "0" or "1" as second argument to set or reset pin
  159. if(!string_cmp(args, "0")) {
  160. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  161. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  162. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  163. } else if(!string_cmp(args, "1")) {
  164. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  165. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  166. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  167. } else {
  168. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  169. }
  170. return;
  171. }
  172. void cli_command_os_info(string_t args, void* context) {
  173. const uint8_t threads_num_max = 32;
  174. osThreadId_t threads_id[threads_num_max];
  175. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  176. printf("Free HEAP size: %d\r\n", xPortGetFreeHeapSize());
  177. printf("Minimum heap size: %d\r\n", xPortGetMinimumEverFreeHeapSize());
  178. printf("%d threads in total:\r\n", thread_num);
  179. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack free");
  180. for(uint8_t i = 0; i < thread_num; i++) {
  181. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  182. printf(
  183. "%-20s 0x%-12lx %-14ld %ld\r\n",
  184. osThreadGetName(threads_id[i]),
  185. (uint32_t)tcb->pxStack,
  186. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  187. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  188. }
  189. return;
  190. }
  191. void cli_commands_init(Cli* cli) {
  192. cli_add_command(cli, "help", cli_command_help, cli);
  193. cli_add_command(cli, "?", cli_command_help, cli);
  194. cli_add_command(cli, "version", cli_command_version, cli);
  195. cli_add_command(cli, "!", cli_command_version, cli);
  196. cli_add_command(cli, "uid", cli_command_uuid, cli);
  197. cli_add_command(cli, "date", cli_command_date, cli);
  198. cli_add_command(cli, "log", cli_command_log, cli);
  199. cli_add_command(cli, "vibro", cli_command_vibro, cli);
  200. cli_add_command(cli, "led", cli_command_led, cli);
  201. cli_add_command(cli, "gpio_set", cli_command_gpio_set, cli);
  202. cli_add_command(cli, "os_info", cli_command_os_info, cli);
  203. }