cli_commands.c 17 KB

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