cli_commands.c 16 KB

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