cli_commands.c 14 KB

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