cli_commands.c 15 KB

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