cli_commands.c 8.1 KB

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