cli_commands.c 8.0 KB

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