cli_commands.c 15 KB

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