cli_commands.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #include <shci.h>
  9. /*
  10. * Device Info Command
  11. * This command is intended to be used by humans and machines
  12. * Keys and values format MUST NOT BE changed
  13. */
  14. void cli_command_device_info(Cli* cli, string_t args, void* context) {
  15. // Model name
  16. printf("hardware_model : %s\r\n", api_hal_version_get_model_name());
  17. const char* name = api_hal_version_get_name_ptr();
  18. if(name) {
  19. printf("hardware_name : %s\r\n", name);
  20. }
  21. // Unique ID
  22. printf("hardware_uid : ");
  23. const uint8_t* uid = api_hal_version_uid();
  24. for(size_t i = 0; i < api_hal_version_uid_size(); i++) {
  25. printf("%02X", uid[i]);
  26. }
  27. printf("\r\n");
  28. // Board Revision
  29. printf("hardware_ver : %d\r\n", api_hal_version_get_hw_version());
  30. printf("hardware_target : %d\r\n", api_hal_version_get_hw_target());
  31. printf("hardware_body : %d\r\n", api_hal_version_get_hw_body());
  32. printf("hardware_connect : %d\r\n", api_hal_version_get_hw_connect());
  33. printf("hardware_timestamp : %lu\r\n", api_hal_version_get_hw_timestamp());
  34. // Color and Region
  35. printf("hardware_color : %d\r\n", api_hal_version_get_hw_color());
  36. printf("hardware_region : %d\r\n", api_hal_version_get_hw_region());
  37. // Bootloader Version
  38. const Version* boot_version = api_hal_version_get_boot_version();
  39. if(boot_version) {
  40. printf("boot_version : %s\r\n", version_get_version(boot_version));
  41. printf("boot_commit : %s\r\n", version_get_githash(boot_version));
  42. printf("boot_branch : %s\r\n", version_get_gitbranch(boot_version));
  43. printf("boot_build_date : %s\r\n", version_get_builddate(boot_version));
  44. }
  45. // Firmware version
  46. const Version* firmware_version = api_hal_version_get_firmware_version();
  47. if(firmware_version) {
  48. printf("firmware_version : %s\r\n", version_get_version(firmware_version));
  49. printf("firmware_commit : %s\r\n", version_get_githash(firmware_version));
  50. printf("firmware_branch : %s\r\n", version_get_gitbranch(firmware_version));
  51. printf("firmware_build_date : %s\r\n", version_get_builddate(firmware_version));
  52. }
  53. WirelessFwInfo_t pWirelessInfo;
  54. if(api_hal_bt_is_alive() && SHCI_GetWirelessFwInfo(&pWirelessInfo) == SHCI_Success) {
  55. printf("radio_alive : true\r\n");
  56. // FUS Info
  57. printf("radio_fus_major : %d\r\n", pWirelessInfo.FusVersionMajor);
  58. printf("radio_fus_minor : %d\r\n", pWirelessInfo.FusVersionMinor);
  59. printf("radio_fus_sub : %d\r\n", pWirelessInfo.FusVersionSub);
  60. printf("radio_fus_sram2b : %dK\r\n", pWirelessInfo.FusMemorySizeSram2B);
  61. printf("radio_fus_sram2a : %dK\r\n", pWirelessInfo.FusMemorySizeSram2A);
  62. printf("radio_fus_flash : %dK\r\n", pWirelessInfo.FusMemorySizeFlash * 4);
  63. // Stack Info
  64. printf("radio_stack_type : %d\r\n", pWirelessInfo.StackType);
  65. printf("radio_stack_major : %d\r\n", pWirelessInfo.VersionMajor);
  66. printf("radio_stack_minor : %d\r\n", pWirelessInfo.VersionMinor);
  67. printf("radio_stack_sub : %d\r\n", pWirelessInfo.VersionSub);
  68. printf("radio_stack_branch : %d\r\n", pWirelessInfo.VersionBranch);
  69. printf("radio_stack_release : %d\r\n", pWirelessInfo.VersionReleaseType);
  70. printf("radio_stack_sram2b : %dK\r\n", pWirelessInfo.MemorySizeSram2B);
  71. printf("radio_stack_sram2a : %dK\r\n", pWirelessInfo.MemorySizeSram2A);
  72. printf("radio_stack_sram1 : %dK\r\n", pWirelessInfo.MemorySizeSram1);
  73. printf("radio_stack_flash : %dK\r\n", pWirelessInfo.MemorySizeFlash * 4);
  74. // Mac address
  75. printf("radio_ble_mac : ");
  76. const uint8_t* ble_mac = api_hal_version_get_ble_mac();
  77. for(size_t i = 0; i < 6; i++) {
  78. printf("%02X", ble_mac[i]);
  79. }
  80. printf("\r\n");
  81. } else {
  82. printf("radio_alive : false\r\n");
  83. }
  84. }
  85. void cli_command_help(Cli* cli, string_t args, void* context) {
  86. (void)args;
  87. printf("Commands we have:");
  88. // Command count
  89. const size_t commands_count = CliCommandTree_size(cli->commands);
  90. const size_t commands_count_mid = commands_count / 2 + commands_count % 2;
  91. // Use 2 iterators from start and middle to show 2 columns
  92. CliCommandTree_it_t it_left;
  93. CliCommandTree_it(it_left, cli->commands);
  94. CliCommandTree_it_t it_right;
  95. CliCommandTree_it(it_right, cli->commands);
  96. for(size_t i = 0; i < commands_count_mid; i++) CliCommandTree_next(it_right);
  97. // Iterate throw tree
  98. for(size_t i = 0; i < commands_count_mid; i++) {
  99. printf("\r\n");
  100. // Left Column
  101. if(!CliCommandTree_end_p(it_left)) {
  102. printf("%-30s", string_get_cstr(*CliCommandTree_ref(it_left)->key_ptr));
  103. CliCommandTree_next(it_left);
  104. }
  105. // Right Column
  106. if(!CliCommandTree_end_p(it_right)) {
  107. printf(string_get_cstr(*CliCommandTree_ref(it_right)->key_ptr));
  108. CliCommandTree_next(it_right);
  109. }
  110. };
  111. if(string_size(args) > 0) {
  112. cli_nl();
  113. printf("Also I have no clue what '");
  114. printf(string_get_cstr(args));
  115. printf("' is.");
  116. }
  117. }
  118. void cli_command_date(Cli* cli, string_t args, void* context) {
  119. RTC_TimeTypeDef time;
  120. RTC_DateTypeDef date;
  121. if(string_size(args) > 0) {
  122. uint16_t Hours, Minutes, Seconds, Month, Date, Year, WeekDay;
  123. int ret = sscanf(
  124. string_get_cstr(args),
  125. "%hu:%hu:%hu %hu-%hu-%hu %hu",
  126. &Hours,
  127. &Minutes,
  128. &Seconds,
  129. &Month,
  130. &Date,
  131. &Year,
  132. &WeekDay);
  133. if(ret == 7) {
  134. time.Hours = Hours;
  135. time.Minutes = Minutes;
  136. time.Seconds = Seconds;
  137. time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  138. time.StoreOperation = RTC_STOREOPERATION_RESET;
  139. date.WeekDay = WeekDay;
  140. date.Month = Month;
  141. date.Date = Date;
  142. date.Year = Year - 2000;
  143. HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN);
  144. HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN);
  145. // Verification
  146. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  147. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  148. printf(
  149. "New time is: %.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
  150. time.Hours,
  151. time.Minutes,
  152. time.Seconds,
  153. date.Month,
  154. date.Date,
  155. 2000 + date.Year,
  156. date.WeekDay);
  157. } else {
  158. printf(
  159. "Invalid time format, use `hh:mm:ss MM-DD-YYYY WD`. sscanf %d %s",
  160. ret,
  161. string_get_cstr(args));
  162. return;
  163. }
  164. } else {
  165. // TODO add get_datetime to core, not use HAL here
  166. // READ ORDER MATTERS! Time then date.
  167. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  168. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  169. printf(
  170. "%.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
  171. time.Hours,
  172. time.Minutes,
  173. time.Seconds,
  174. date.Month,
  175. date.Date,
  176. 2000 + date.Year,
  177. date.WeekDay);
  178. }
  179. }
  180. void cli_command_log(Cli* cli, string_t args, void* context) {
  181. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  182. printf("Press any key to stop...\r\n");
  183. cli_getc(cli);
  184. furi_stdglue_set_global_stdout_callback(NULL);
  185. }
  186. void cli_command_vibro(Cli* cli, string_t args, void* context) {
  187. if(!string_cmp(args, "0")) {
  188. NotificationApp* notification = furi_record_open("notification");
  189. notification_message_block(notification, &sequence_reset_vibro);
  190. furi_record_close("notification");
  191. } else if(!string_cmp(args, "1")) {
  192. NotificationApp* notification = furi_record_open("notification");
  193. notification_message_block(notification, &sequence_set_vibro_on);
  194. furi_record_close("notification");
  195. } else {
  196. cli_print_usage("vibro", "<1|0>", string_get_cstr(args));
  197. }
  198. }
  199. void cli_command_led(Cli* cli, string_t args, void* context) {
  200. // Get first word as light name
  201. NotificationMessage notification_led_message;
  202. string_t light_name;
  203. string_init(light_name);
  204. size_t ws = string_search_char(args, ' ');
  205. if(ws == STRING_FAILURE) {
  206. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  207. string_clear(light_name);
  208. return;
  209. } else {
  210. string_set_n(light_name, args, 0, ws);
  211. string_right(args, ws);
  212. string_strim(args);
  213. }
  214. // Check light name
  215. if(!string_cmp(light_name, "r")) {
  216. notification_led_message.type = NotificationMessageTypeLedRed;
  217. } else if(!string_cmp(light_name, "g")) {
  218. notification_led_message.type = NotificationMessageTypeLedGreen;
  219. } else if(!string_cmp(light_name, "b")) {
  220. notification_led_message.type = NotificationMessageTypeLedBlue;
  221. } else if(!string_cmp(light_name, "bl")) {
  222. notification_led_message.type = NotificationMessageTypeLedDisplay;
  223. } else {
  224. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  225. string_clear(light_name);
  226. return;
  227. }
  228. string_clear(light_name);
  229. // Read light value from the rest of the string
  230. char* end_ptr;
  231. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  232. if(!(value < 256 && *end_ptr == '\0')) {
  233. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  234. return;
  235. }
  236. // Set led value
  237. notification_led_message.data.led.value = value;
  238. // Form notification sequence
  239. const NotificationSequence notification_sequence = {
  240. &notification_led_message,
  241. NULL,
  242. };
  243. // Send notification
  244. NotificationApp* notification = furi_record_open("notification");
  245. notification_internal_message_block(notification, &notification_sequence);
  246. furi_record_close("notification");
  247. }
  248. void cli_command_gpio_set(Cli* cli, string_t args, void* context) {
  249. char pin_names[][4] = {
  250. "PC0",
  251. "PC1",
  252. "PC3",
  253. "PB2",
  254. "PB3",
  255. "PA4",
  256. "PA6",
  257. "PA7",
  258. #ifdef DEBUG
  259. "PA0",
  260. "PB7",
  261. "PB8",
  262. "PB9"
  263. #endif
  264. };
  265. GpioPin gpio[] = {
  266. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  267. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  268. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  269. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  270. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  271. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  272. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  273. {.port = GPIOA, .pin = LL_GPIO_PIN_7},
  274. #ifdef DEBUG
  275. {.port = GPIOA, .pin = LL_GPIO_PIN_0}, // IR_RX (PA0)
  276. {.port = GPIOB, .pin = LL_GPIO_PIN_7}, // UART RX (PB7)
  277. {.port = GPIOB, .pin = LL_GPIO_PIN_8}, // SPEAKER (PB8)
  278. {.port = GPIOB, .pin = LL_GPIO_PIN_9}, // IR_TX (PB9)
  279. #endif
  280. };
  281. uint8_t num = 0;
  282. bool pin_found = false;
  283. // Get first word as pin name
  284. string_t pin_name;
  285. string_init(pin_name);
  286. size_t ws = string_search_char(args, ' ');
  287. if(ws == STRING_FAILURE) {
  288. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  289. string_clear(pin_name);
  290. return;
  291. } else {
  292. string_set_n(pin_name, args, 0, ws);
  293. string_right(args, ws);
  294. string_strim(args);
  295. }
  296. // Search correct pin name
  297. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  298. if(!string_cmp(pin_name, pin_names[num])) {
  299. pin_found = true;
  300. break;
  301. }
  302. }
  303. if(!pin_found) {
  304. printf("Wrong pin name. Available pins: ");
  305. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  306. printf("%s ", pin_names[i]);
  307. }
  308. string_clear(pin_name);
  309. return;
  310. }
  311. string_clear(pin_name);
  312. // Read "0" or "1" as second argument to set or reset pin
  313. if(!string_cmp(args, "0")) {
  314. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  315. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  316. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  317. } else if(!string_cmp(args, "1")) {
  318. #ifdef DEBUG
  319. if(num == 8) { // PA0
  320. printf(
  321. "Setting PA0 pin HIGH with TSOP connected can damage IR receiver. Are you sure you want to continue? (y/n)?\r\n");
  322. char c = cli_getc(cli);
  323. if(c != 'y' && c != 'Y') {
  324. printf("Cancelled.\r\n");
  325. return;
  326. }
  327. }
  328. #endif
  329. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  330. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  331. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  332. } else {
  333. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  334. }
  335. return;
  336. }
  337. void cli_command_ps(Cli* cli, string_t args, void* context) {
  338. const uint8_t threads_num_max = 32;
  339. osThreadId_t threads_id[threads_num_max];
  340. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  341. printf("%d threads in total:\r\n", thread_num);
  342. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack watermark");
  343. for(uint8_t i = 0; i < thread_num; i++) {
  344. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  345. printf(
  346. "%-20s 0x%-12lx %-14ld %ld\r\n",
  347. osThreadGetName(threads_id[i]),
  348. (uint32_t)tcb->pxStack,
  349. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  350. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  351. }
  352. }
  353. void cli_command_free(Cli* cli, string_t args, void* context) {
  354. printf("Free heap size: %d\r\n", memmgr_get_free_heap());
  355. printf("Minimum heap size: %d\r\n", memmgr_get_minimum_free_heap());
  356. printf("Maximum heap block: %d\r\n", memmgr_heap_get_max_free_block());
  357. }
  358. void cli_command_free_blocks(Cli* cli, string_t args, void* context) {
  359. memmgr_heap_printf_free_blocks();
  360. }
  361. void cli_commands_init(Cli* cli) {
  362. cli_add_command(cli, "!", CliCommandFlagParallelSafe, cli_command_device_info, NULL);
  363. cli_add_command(cli, "device_info", CliCommandFlagParallelSafe, cli_command_device_info, NULL);
  364. cli_add_command(cli, "?", CliCommandFlagParallelSafe, cli_command_help, NULL);
  365. cli_add_command(cli, "help", CliCommandFlagParallelSafe, cli_command_help, NULL);
  366. cli_add_command(cli, "date", CliCommandFlagParallelSafe, cli_command_date, NULL);
  367. cli_add_command(cli, "log", CliCommandFlagParallelSafe, cli_command_log, NULL);
  368. cli_add_command(cli, "ps", CliCommandFlagParallelSafe, cli_command_ps, NULL);
  369. cli_add_command(cli, "free", CliCommandFlagParallelSafe, cli_command_free, NULL);
  370. cli_add_command(cli, "free_blocks", CliCommandFlagParallelSafe, cli_command_free_blocks, NULL);
  371. cli_add_command(cli, "vibro", CliCommandFlagDefault, cli_command_vibro, NULL);
  372. cli_add_command(cli, "led", CliCommandFlagDefault, cli_command_led, NULL);
  373. cli_add_command(cli, "gpio_set", CliCommandFlagDefault, cli_command_gpio_set, NULL);
  374. }