power_cli.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "power_cli.h"
  2. #include <furi_hal.h>
  3. #include <cli/cli.h>
  4. #include <lib/toolbox/args.h>
  5. #include <power/power_service/power.h>
  6. void power_cli_off(Cli* cli, string_t args) {
  7. Power* power = furi_record_open("power");
  8. printf("It's now safe to disconnect USB from your flipper\r\n");
  9. osDelay(666);
  10. power_off(power);
  11. }
  12. void power_cli_reboot(Cli* cli, string_t args) {
  13. power_reboot(PowerBootModeNormal);
  14. }
  15. void power_cli_reboot2dfu(Cli* cli, string_t args) {
  16. power_reboot(PowerBootModeDfu);
  17. }
  18. static void power_cli_info_callback(const char* key, const char* value, bool last, void* context) {
  19. printf("%-24s: %s\r\n", key, value);
  20. }
  21. void power_cli_info(Cli* cli, string_t args) {
  22. furi_hal_power_info_get(power_cli_info_callback, NULL);
  23. }
  24. void power_cli_debug(Cli* cli, string_t args) {
  25. furi_hal_power_dump_state();
  26. }
  27. void power_cli_5v(Cli* cli, string_t args) {
  28. if(!string_cmp(args, "0")) {
  29. furi_hal_power_disable_otg();
  30. } else if(!string_cmp(args, "1")) {
  31. furi_hal_power_enable_otg();
  32. } else {
  33. cli_print_usage("power_otg", "<1|0>", string_get_cstr(args));
  34. }
  35. }
  36. void power_cli_3v3(Cli* cli, string_t args) {
  37. if(!string_cmp(args, "0")) {
  38. furi_hal_power_disable_external_3_3v();
  39. } else if(!string_cmp(args, "1")) {
  40. furi_hal_power_enable_external_3_3v();
  41. } else {
  42. cli_print_usage("power_ext", "<1|0>", string_get_cstr(args));
  43. }
  44. }
  45. static void power_cli_command_print_usage() {
  46. printf("Usage:\r\n");
  47. printf("power <cmd> <args>\r\n");
  48. printf("Cmd list:\r\n");
  49. printf("\toff\t - shutdown power\r\n");
  50. printf("\treboot\t - reboot\r\n");
  51. printf("\treboot2dfu\t - reboot to dfu bootloader\r\n");
  52. printf("\tinfo\t - show power info\r\n");
  53. printf("\tdebug\t - show debug information\r\n");
  54. printf("\t5v <0 or 1>\t - enable or disable 5v ext\r\n");
  55. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  56. printf("\t3v3 <0 or 1>\t - enable or disable 3v3 ext\r\n");
  57. }
  58. }
  59. void power_cli(Cli* cli, string_t args, void* context) {
  60. string_t cmd;
  61. string_init(cmd);
  62. do {
  63. if(!args_read_string_and_trim(args, cmd)) {
  64. power_cli_command_print_usage();
  65. break;
  66. }
  67. if(string_cmp_str(cmd, "off") == 0) {
  68. power_cli_off(cli, args);
  69. break;
  70. }
  71. if(string_cmp_str(cmd, "reboot") == 0) {
  72. power_cli_reboot(cli, args);
  73. break;
  74. }
  75. if(string_cmp_str(cmd, "reboot2dfu") == 0) {
  76. power_cli_reboot2dfu(cli, args);
  77. break;
  78. }
  79. if(string_cmp_str(cmd, "info") == 0) {
  80. power_cli_info(cli, args);
  81. break;
  82. }
  83. if(string_cmp_str(cmd, "debug") == 0) {
  84. power_cli_debug(cli, args);
  85. break;
  86. }
  87. if(string_cmp_str(cmd, "5v") == 0) {
  88. power_cli_5v(cli, args);
  89. break;
  90. }
  91. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  92. if(string_cmp_str(cmd, "3v3") == 0) {
  93. power_cli_3v3(cli, args);
  94. break;
  95. }
  96. }
  97. power_cli_command_print_usage();
  98. } while(false);
  99. string_clear(cmd);
  100. }
  101. void power_on_system_start() {
  102. #ifdef SRV_CLI
  103. Cli* cli = furi_record_open("cli");
  104. cli_add_command(cli, "power", CliCommandFlagParallelSafe, power_cli, NULL);
  105. furi_record_close("cli");
  106. #else
  107. UNUSED(power_cli);
  108. #endif
  109. }