cli_commands.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_TimeTypeDef time;
  62. RTC_DateTypeDef date;
  63. if(string_size(args) > 0) {
  64. uint16_t Hours, Minutes, Seconds, Month, Date, Year, WeekDay;
  65. int ret = sscanf(
  66. string_get_cstr(args),
  67. "%hu:%hu:%hu %hu-%hu-%hu %hu",
  68. &Hours,
  69. &Minutes,
  70. &Seconds,
  71. &Month,
  72. &Date,
  73. &Year,
  74. &WeekDay);
  75. if(ret == 7) {
  76. time.Hours = Hours;
  77. time.Minutes = Minutes;
  78. time.Seconds = Seconds;
  79. time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  80. time.StoreOperation = RTC_STOREOPERATION_RESET;
  81. date.WeekDay = WeekDay;
  82. date.Month = Month;
  83. date.Date = Date;
  84. date.Year = Year - 2000;
  85. HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN);
  86. HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN);
  87. // Verification
  88. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  89. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  90. printf(
  91. "New time is: %.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
  92. time.Hours,
  93. time.Minutes,
  94. time.Seconds,
  95. date.Month,
  96. date.Date,
  97. 2000 + date.Year,
  98. date.WeekDay);
  99. } else {
  100. printf(
  101. "Invalid time format, use `hh:mm:ss MM-DD-YYYY WD`. sscanf %d %s",
  102. ret,
  103. string_get_cstr(args));
  104. return;
  105. }
  106. } else {
  107. // TODO add get_datetime to core, not use HAL here
  108. // READ ORDER MATTERS! Time then date.
  109. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  110. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  111. printf(
  112. "%.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
  113. time.Hours,
  114. time.Minutes,
  115. time.Seconds,
  116. date.Month,
  117. date.Date,
  118. 2000 + date.Year,
  119. date.WeekDay);
  120. }
  121. }
  122. void cli_command_log(Cli* cli, string_t args, void* context) {
  123. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  124. printf("Press any key to stop...\r\n");
  125. cli_getc(cli);
  126. furi_stdglue_set_global_stdout_callback(NULL);
  127. }
  128. void cli_command_hw_info(Cli* cli, string_t args, void* context) {
  129. printf(
  130. "%-20s %d.F%dB%dC%d\r\n",
  131. "HW version:",
  132. api_hal_version_get_hw_version(),
  133. api_hal_version_get_hw_target(),
  134. api_hal_version_get_hw_body(),
  135. api_hal_version_get_hw_connect());
  136. time_t time = api_hal_version_get_hw_timestamp();
  137. char time_string[26] = "";
  138. ctime_r(&time, time_string);
  139. if(time_string[strlen(time_string) - 1] == '\n') {
  140. time_string[strlen(time_string) - 1] = '\0';
  141. }
  142. printf("%-20s %s\r\n", "Production date:", time_string);
  143. const char* name = api_hal_version_get_name_ptr();
  144. if(name) {
  145. printf("%-20s %s", "Name:", name);
  146. }
  147. }
  148. void cli_command_vibro(Cli* cli, string_t args, void* context) {
  149. if(!string_cmp(args, "0")) {
  150. NotificationApp* notification = furi_record_open("notification");
  151. notification_message_block(notification, &sequence_reset_vibro);
  152. furi_record_close("notification");
  153. } else if(!string_cmp(args, "1")) {
  154. NotificationApp* notification = furi_record_open("notification");
  155. notification_message_block(notification, &sequence_set_vibro_on);
  156. furi_record_close("notification");
  157. } else {
  158. cli_print_usage("vibro", "<1|0>", string_get_cstr(args));
  159. }
  160. }
  161. void cli_command_led(Cli* cli, string_t args, void* context) {
  162. // Get first word as light name
  163. NotificationMessage notification_led_message;
  164. string_t light_name;
  165. string_init(light_name);
  166. size_t ws = string_search_char(args, ' ');
  167. if(ws == STRING_FAILURE) {
  168. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  169. string_clear(light_name);
  170. return;
  171. } else {
  172. string_set_n(light_name, args, 0, ws);
  173. string_right(args, ws);
  174. string_strim(args);
  175. }
  176. // Check light name
  177. if(!string_cmp(light_name, "r")) {
  178. notification_led_message.type = NotificationMessageTypeLedRed;
  179. } else if(!string_cmp(light_name, "g")) {
  180. notification_led_message.type = NotificationMessageTypeLedGreen;
  181. } else if(!string_cmp(light_name, "b")) {
  182. notification_led_message.type = NotificationMessageTypeLedBlue;
  183. } else if(!string_cmp(light_name, "bl")) {
  184. notification_led_message.type = NotificationMessageTypeLedDisplay;
  185. } else {
  186. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  187. string_clear(light_name);
  188. return;
  189. }
  190. string_clear(light_name);
  191. // Read light value from the rest of the string
  192. char* end_ptr;
  193. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  194. if(!(value < 256 && *end_ptr == '\0')) {
  195. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  196. return;
  197. }
  198. // Set led value
  199. notification_led_message.data.led.value = value;
  200. // Form notification sequence
  201. const NotificationSequence notification_sequence = {
  202. &notification_led_message,
  203. NULL,
  204. };
  205. // Send notification
  206. NotificationApp* notification = furi_record_open("notification");
  207. notification_internal_message_block(notification, &notification_sequence);
  208. furi_record_close("notification");
  209. }
  210. void cli_command_gpio_set(Cli* cli, string_t args, void* context) {
  211. char pin_names[][4] = {
  212. "PC0",
  213. "PC1",
  214. "PC3",
  215. "PB2",
  216. "PB3",
  217. "PA4",
  218. "PA6",
  219. "PA7",
  220. #ifdef DEBUG
  221. "PA0",
  222. "PB7",
  223. "PB8",
  224. "PB9"
  225. #endif
  226. };
  227. GpioPin gpio[] = {
  228. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  229. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  230. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  231. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  232. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  233. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  234. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  235. {.port = GPIOA, .pin = LL_GPIO_PIN_7},
  236. #ifdef DEBUG
  237. {.port = GPIOA, .pin = LL_GPIO_PIN_0}, // IR_RX (PA0)
  238. {.port = GPIOB, .pin = LL_GPIO_PIN_7}, // UART RX (PB7)
  239. {.port = GPIOB, .pin = LL_GPIO_PIN_8}, // SPEAKER (PB8)
  240. {.port = GPIOB, .pin = LL_GPIO_PIN_9}, // IR_TX (PB9)
  241. #endif
  242. };
  243. uint8_t num = 0;
  244. bool pin_found = false;
  245. // Get first word as pin name
  246. string_t pin_name;
  247. string_init(pin_name);
  248. size_t ws = string_search_char(args, ' ');
  249. if(ws == STRING_FAILURE) {
  250. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  251. string_clear(pin_name);
  252. return;
  253. } else {
  254. string_set_n(pin_name, args, 0, ws);
  255. string_right(args, ws);
  256. string_strim(args);
  257. }
  258. // Search correct pin name
  259. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  260. if(!string_cmp(pin_name, pin_names[num])) {
  261. pin_found = true;
  262. break;
  263. }
  264. }
  265. if(!pin_found) {
  266. printf("Wrong pin name. Available pins: ");
  267. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  268. printf("%s ", pin_names[i]);
  269. }
  270. string_clear(pin_name);
  271. return;
  272. }
  273. string_clear(pin_name);
  274. // Read "0" or "1" as second argument to set or reset pin
  275. if(!string_cmp(args, "0")) {
  276. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  277. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  278. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  279. } else if(!string_cmp(args, "1")) {
  280. #ifdef DEBUG
  281. if(num == 8) { // PA0
  282. printf(
  283. "Setting PA0 pin HIGH with TSOP connected can damage IR receiver. Are you sure you want to continue? (y/n)?\r\n");
  284. char c = cli_getc(cli);
  285. if(c != 'y' && c != 'Y') {
  286. printf("Cancelled.\r\n");
  287. return;
  288. }
  289. }
  290. #endif
  291. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  292. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  293. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  294. } else {
  295. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  296. }
  297. return;
  298. }
  299. void cli_command_os_info(Cli* cli, string_t args, void* context) {
  300. const uint8_t threads_num_max = 32;
  301. osThreadId_t threads_id[threads_num_max];
  302. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  303. printf("Free HEAP size: %d\r\n", xPortGetFreeHeapSize());
  304. printf("Minimum heap size: %d\r\n", xPortGetMinimumEverFreeHeapSize());
  305. printf("%d threads in total:\r\n", thread_num);
  306. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack free");
  307. for(uint8_t i = 0; i < thread_num; i++) {
  308. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  309. printf(
  310. "%-20s 0x%-12lx %-14ld %ld\r\n",
  311. osThreadGetName(threads_id[i]),
  312. (uint32_t)tcb->pxStack,
  313. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  314. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  315. }
  316. return;
  317. }
  318. void cli_commands_init(Cli* cli) {
  319. cli_add_command(cli, "help", cli_command_help, NULL);
  320. cli_add_command(cli, "?", cli_command_help, NULL);
  321. cli_add_command(cli, "version", cli_command_version, NULL);
  322. cli_add_command(cli, "!", cli_command_version, NULL);
  323. cli_add_command(cli, "uid", cli_command_uuid, NULL);
  324. cli_add_command(cli, "date", cli_command_date, NULL);
  325. cli_add_command(cli, "log", cli_command_log, NULL);
  326. cli_add_command(cli, "hw_info", cli_command_hw_info, NULL);
  327. cli_add_command(cli, "vibro", cli_command_vibro, NULL);
  328. cli_add_command(cli, "led", cli_command_led, NULL);
  329. cli_add_command(cli, "gpio_set", cli_command_gpio_set, NULL);
  330. cli_add_command(cli, "os_info", cli_command_os_info, NULL);
  331. }