power_cli.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, FuriString* args) {
  7. UNUSED(cli);
  8. UNUSED(args);
  9. Power* power = furi_record_open(RECORD_POWER);
  10. printf("It's now safe to disconnect USB from your flipper\r\n");
  11. furi_delay_ms(666);
  12. power_off(power);
  13. }
  14. void power_cli_reboot(Cli* cli, FuriString* args) {
  15. UNUSED(cli);
  16. UNUSED(args);
  17. power_reboot(PowerBootModeNormal);
  18. }
  19. void power_cli_reboot2dfu(Cli* cli, FuriString* args) {
  20. UNUSED(cli);
  21. UNUSED(args);
  22. power_reboot(PowerBootModeDfu);
  23. }
  24. void power_cli_5v(Cli* cli, FuriString* args) {
  25. UNUSED(cli);
  26. if(!furi_string_cmp(args, "0")) {
  27. furi_hal_power_disable_otg();
  28. } else if(!furi_string_cmp(args, "1")) {
  29. furi_hal_power_enable_otg();
  30. } else {
  31. cli_print_usage("power_otg", "<1|0>", furi_string_get_cstr(args));
  32. }
  33. }
  34. void power_cli_3v3(Cli* cli, FuriString* args) {
  35. UNUSED(cli);
  36. if(!furi_string_cmp(args, "0")) {
  37. furi_hal_power_disable_external_3_3v();
  38. } else if(!furi_string_cmp(args, "1")) {
  39. furi_hal_power_enable_external_3_3v();
  40. } else {
  41. cli_print_usage("power_ext", "<1|0>", furi_string_get_cstr(args));
  42. }
  43. }
  44. static void power_cli_command_print_usage() {
  45. printf("Usage:\r\n");
  46. printf("power <cmd> <args>\r\n");
  47. printf("Cmd list:\r\n");
  48. printf("\toff\t - shutdown power\r\n");
  49. printf("\treboot\t - reboot\r\n");
  50. printf("\treboot2dfu\t - reboot to dfu bootloader\r\n");
  51. printf("\t5v <0 or 1>\t - enable or disable 5v ext\r\n");
  52. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  53. printf("\t3v3 <0 or 1>\t - enable or disable 3v3 ext\r\n");
  54. }
  55. }
  56. void power_cli(Cli* cli, FuriString* args, void* context) {
  57. UNUSED(context);
  58. FuriString* cmd;
  59. cmd = furi_string_alloc();
  60. do {
  61. if(!args_read_string_and_trim(args, cmd)) {
  62. power_cli_command_print_usage();
  63. break;
  64. }
  65. if(furi_string_cmp_str(cmd, "off") == 0) {
  66. power_cli_off(cli, args);
  67. break;
  68. }
  69. if(furi_string_cmp_str(cmd, "reboot") == 0) {
  70. power_cli_reboot(cli, args);
  71. break;
  72. }
  73. if(furi_string_cmp_str(cmd, "reboot2dfu") == 0) {
  74. power_cli_reboot2dfu(cli, args);
  75. break;
  76. }
  77. if(furi_string_cmp_str(cmd, "5v") == 0) {
  78. power_cli_5v(cli, args);
  79. break;
  80. }
  81. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  82. if(furi_string_cmp_str(cmd, "3v3") == 0) {
  83. power_cli_3v3(cli, args);
  84. break;
  85. }
  86. }
  87. power_cli_command_print_usage();
  88. } while(false);
  89. furi_string_free(cmd);
  90. }
  91. void power_on_system_start() {
  92. #ifdef SRV_CLI
  93. Cli* cli = furi_record_open(RECORD_CLI);
  94. cli_add_command(cli, "power", CliCommandFlagParallelSafe, power_cli, NULL);
  95. furi_record_close(RECORD_CLI);
  96. #else
  97. UNUSED(power_cli);
  98. #endif
  99. }