cli_commands.c 7.3 KB

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