cli_commands.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. // 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. if(string_size(args) > 0) {
  107. cli_nl();
  108. printf("Also I have no clue what '");
  109. printf(string_get_cstr(args));
  110. printf("' is.");
  111. }
  112. }
  113. void cli_command_date(Cli* cli, string_t args, void* context) {
  114. RTC_TimeTypeDef time;
  115. RTC_DateTypeDef date;
  116. if(string_size(args) > 0) {
  117. uint16_t Hours, Minutes, Seconds, Month, Date, Year, WeekDay;
  118. int ret = sscanf(
  119. string_get_cstr(args),
  120. "%hu:%hu:%hu %hu-%hu-%hu %hu",
  121. &Hours,
  122. &Minutes,
  123. &Seconds,
  124. &Month,
  125. &Date,
  126. &Year,
  127. &WeekDay);
  128. if(ret == 7) {
  129. time.Hours = Hours;
  130. time.Minutes = Minutes;
  131. time.Seconds = Seconds;
  132. time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  133. time.StoreOperation = RTC_STOREOPERATION_RESET;
  134. date.WeekDay = WeekDay;
  135. date.Month = Month;
  136. date.Date = Date;
  137. date.Year = Year - 2000;
  138. HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN);
  139. HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN);
  140. // Verification
  141. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  142. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  143. printf(
  144. "New time is: %.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
  145. time.Hours,
  146. time.Minutes,
  147. time.Seconds,
  148. date.Month,
  149. date.Date,
  150. 2000 + date.Year,
  151. date.WeekDay);
  152. } else {
  153. printf(
  154. "Invalid time format, use `hh:mm:ss MM-DD-YYYY WD`. sscanf %d %s",
  155. ret,
  156. string_get_cstr(args));
  157. return;
  158. }
  159. } else {
  160. // TODO add get_datetime to core, not use HAL here
  161. // READ ORDER MATTERS! Time then date.
  162. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  163. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  164. printf(
  165. "%.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
  166. time.Hours,
  167. time.Minutes,
  168. time.Seconds,
  169. date.Month,
  170. date.Date,
  171. 2000 + date.Year,
  172. date.WeekDay);
  173. }
  174. }
  175. void cli_command_log(Cli* cli, string_t args, void* context) {
  176. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  177. printf("Press any key to stop...\r\n");
  178. cli_getc(cli);
  179. furi_stdglue_set_global_stdout_callback(NULL);
  180. }
  181. void cli_command_vibro(Cli* cli, string_t args, void* context) {
  182. if(!string_cmp(args, "0")) {
  183. NotificationApp* notification = furi_record_open("notification");
  184. notification_message_block(notification, &sequence_reset_vibro);
  185. furi_record_close("notification");
  186. } else if(!string_cmp(args, "1")) {
  187. NotificationApp* notification = furi_record_open("notification");
  188. notification_message_block(notification, &sequence_set_vibro_on);
  189. furi_record_close("notification");
  190. } else {
  191. cli_print_usage("vibro", "<1|0>", string_get_cstr(args));
  192. }
  193. }
  194. void cli_command_led(Cli* cli, string_t args, void* context) {
  195. // Get first word as light name
  196. NotificationMessage notification_led_message;
  197. string_t light_name;
  198. string_init(light_name);
  199. size_t ws = string_search_char(args, ' ');
  200. if(ws == STRING_FAILURE) {
  201. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  202. string_clear(light_name);
  203. return;
  204. } else {
  205. string_set_n(light_name, args, 0, ws);
  206. string_right(args, ws);
  207. string_strim(args);
  208. }
  209. // Check light name
  210. if(!string_cmp(light_name, "r")) {
  211. notification_led_message.type = NotificationMessageTypeLedRed;
  212. } else if(!string_cmp(light_name, "g")) {
  213. notification_led_message.type = NotificationMessageTypeLedGreen;
  214. } else if(!string_cmp(light_name, "b")) {
  215. notification_led_message.type = NotificationMessageTypeLedBlue;
  216. } else if(!string_cmp(light_name, "bl")) {
  217. notification_led_message.type = NotificationMessageTypeLedDisplay;
  218. } else {
  219. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  220. string_clear(light_name);
  221. return;
  222. }
  223. string_clear(light_name);
  224. // Read light value from the rest of the string
  225. char* end_ptr;
  226. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  227. if(!(value < 256 && *end_ptr == '\0')) {
  228. cli_print_usage("led", "<r|g|b|bl> <0-255>", string_get_cstr(args));
  229. return;
  230. }
  231. // Set led value
  232. notification_led_message.data.led.value = value;
  233. // Form notification sequence
  234. const NotificationSequence notification_sequence = {
  235. &notification_led_message,
  236. NULL,
  237. };
  238. // Send notification
  239. NotificationApp* notification = furi_record_open("notification");
  240. notification_internal_message_block(notification, &notification_sequence);
  241. furi_record_close("notification");
  242. }
  243. void cli_command_gpio_set(Cli* cli, string_t args, void* context) {
  244. char pin_names[][4] = {
  245. "PC0",
  246. "PC1",
  247. "PC3",
  248. "PB2",
  249. "PB3",
  250. "PA4",
  251. "PA6",
  252. "PA7",
  253. #ifdef DEBUG
  254. "PA0",
  255. "PB7",
  256. "PB8",
  257. "PB9"
  258. #endif
  259. };
  260. GpioPin gpio[] = {
  261. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  262. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  263. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  264. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  265. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  266. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  267. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  268. {.port = GPIOA, .pin = LL_GPIO_PIN_7},
  269. #ifdef DEBUG
  270. {.port = GPIOA, .pin = LL_GPIO_PIN_0}, // IR_RX (PA0)
  271. {.port = GPIOB, .pin = LL_GPIO_PIN_7}, // UART RX (PB7)
  272. {.port = GPIOB, .pin = LL_GPIO_PIN_8}, // SPEAKER (PB8)
  273. {.port = GPIOB, .pin = LL_GPIO_PIN_9}, // IR_TX (PB9)
  274. #endif
  275. };
  276. uint8_t num = 0;
  277. bool pin_found = false;
  278. // Get first word as pin name
  279. string_t pin_name;
  280. string_init(pin_name);
  281. size_t ws = string_search_char(args, ' ');
  282. if(ws == STRING_FAILURE) {
  283. cli_print_usage("gpio_set", "<pin_name> <0|1>", string_get_cstr(args));
  284. string_clear(pin_name);
  285. return;
  286. } else {
  287. string_set_n(pin_name, args, 0, ws);
  288. string_right(args, ws);
  289. string_strim(args);
  290. }
  291. // Search correct pin name
  292. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  293. if(!string_cmp(pin_name, pin_names[num])) {
  294. pin_found = true;
  295. break;
  296. }
  297. }
  298. if(!pin_found) {
  299. printf("Wrong pin name. Available pins: ");
  300. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  301. printf("%s ", pin_names[i]);
  302. }
  303. string_clear(pin_name);
  304. return;
  305. }
  306. string_clear(pin_name);
  307. // Read "0" or "1" as second argument to set or reset pin
  308. if(!string_cmp(args, "0")) {
  309. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  310. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  311. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  312. } else if(!string_cmp(args, "1")) {
  313. #ifdef DEBUG
  314. if(num == 8) { // PA0
  315. printf(
  316. "Setting PA0 pin HIGH with TSOP connected can damage IR receiver. Are you sure you want to continue? (y/n)?\r\n");
  317. char c = cli_getc(cli);
  318. if(c != 'y' && c != 'Y') {
  319. printf("Cancelled.\r\n");
  320. return;
  321. }
  322. }
  323. #endif
  324. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  325. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  326. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  327. } else {
  328. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  329. }
  330. return;
  331. }
  332. void cli_command_ps(Cli* cli, string_t args, void* context) {
  333. const uint8_t threads_num_max = 32;
  334. osThreadId_t threads_id[threads_num_max];
  335. uint8_t thread_num = osThreadEnumerate(threads_id, threads_num_max);
  336. printf("%d threads in total:\r\n", thread_num);
  337. printf("%-20s %-14s %-14s %s\r\n", "Name", "Stack start", "Stack alloc", "Stack free");
  338. for(uint8_t i = 0; i < thread_num; i++) {
  339. TaskControlBlock* tcb = (TaskControlBlock*)threads_id[i];
  340. printf(
  341. "%-20s 0x%-12lx %-14ld %ld\r\n",
  342. osThreadGetName(threads_id[i]),
  343. (uint32_t)tcb->pxStack,
  344. (uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(uint32_t),
  345. osThreadGetStackSpace(threads_id[i]) * sizeof(uint32_t));
  346. }
  347. }
  348. void cli_command_free(Cli* cli, string_t args, void* context) {
  349. printf("Free heap size: %d\r\n", memmgr_get_free_heap());
  350. printf("Minimum heap size: %d\r\n", memmgr_get_minimum_free_heap());
  351. printf("Maximum heap block: %d\r\n", memmgr_heap_get_max_free_block());
  352. }
  353. void cli_commands_init(Cli* cli) {
  354. cli_add_command(cli, "!", CliCommandFlagParallelSafe, cli_command_device_info, NULL);
  355. cli_add_command(cli, "device_info", CliCommandFlagParallelSafe, cli_command_device_info, NULL);
  356. cli_add_command(cli, "?", CliCommandFlagParallelSafe, cli_command_help, NULL);
  357. cli_add_command(cli, "help", CliCommandFlagParallelSafe, cli_command_help, NULL);
  358. cli_add_command(cli, "date", CliCommandFlagParallelSafe, cli_command_date, NULL);
  359. cli_add_command(cli, "log", CliCommandFlagParallelSafe, cli_command_log, NULL);
  360. cli_add_command(cli, "ps", CliCommandFlagParallelSafe, cli_command_ps, NULL);
  361. cli_add_command(cli, "free", CliCommandFlagParallelSafe, cli_command_free, NULL);
  362. cli_add_command(cli, "vibro", CliCommandFlagDefault, cli_command_vibro, NULL);
  363. cli_add_command(cli, "led", CliCommandFlagDefault, cli_command_led, NULL);
  364. cli_add_command(cli, "gpio_set", CliCommandFlagDefault, cli_command_gpio_set, NULL);
  365. }