power_cli.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. void power_cli_debug(Cli* cli, string_t args) {
  19. furi_hal_power_dump_state();
  20. }
  21. void power_cli_5v(Cli* cli, string_t args) {
  22. if(!string_cmp(args, "0")) {
  23. furi_hal_power_disable_otg();
  24. } else if(!string_cmp(args, "1")) {
  25. furi_hal_power_enable_otg();
  26. } else {
  27. cli_print_usage("power_otg", "<1|0>", string_get_cstr(args));
  28. }
  29. }
  30. void power_cli_3v3(Cli* cli, string_t args) {
  31. if(!string_cmp(args, "0")) {
  32. furi_hal_power_disable_external_3_3v();
  33. } else if(!string_cmp(args, "1")) {
  34. furi_hal_power_enable_external_3_3v();
  35. } else {
  36. cli_print_usage("power_ext", "<1|0>", string_get_cstr(args));
  37. }
  38. }
  39. static void power_cli_command_print_usage() {
  40. printf("Usage:\r\n");
  41. printf("power <cmd> <args>\r\n");
  42. printf("Cmd list:\r\n");
  43. printf("\toff\t - shutdown power\r\n");
  44. printf("\treboot\t - reboot\r\n");
  45. printf("\treboot2dfu\t - reboot to dfu bootloader\r\n");
  46. printf("\tdebug\t - show debug information\r\n");
  47. printf("\t5v <0 or 1>\t - enable or disable 5v ext\r\n");
  48. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  49. printf("\t3v3 <0 or 1>\t - enable or disable 3v3 ext\r\n");
  50. }
  51. }
  52. void power_cli(Cli* cli, string_t args, void* context) {
  53. string_t cmd;
  54. string_init(cmd);
  55. do {
  56. if(!args_read_string_and_trim(args, cmd)) {
  57. power_cli_command_print_usage();
  58. break;
  59. }
  60. if(string_cmp_str(cmd, "off") == 0) {
  61. power_cli_off(cli, args);
  62. break;
  63. }
  64. if(string_cmp_str(cmd, "reboot") == 0) {
  65. power_cli_reboot(cli, args);
  66. break;
  67. }
  68. if(string_cmp_str(cmd, "reboot2dfu") == 0) {
  69. power_cli_reboot2dfu(cli, args);
  70. break;
  71. }
  72. if(string_cmp_str(cmd, "debug") == 0) {
  73. power_cli_debug(cli, args);
  74. break;
  75. }
  76. if(string_cmp_str(cmd, "5v") == 0) {
  77. power_cli_5v(cli, args);
  78. break;
  79. }
  80. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  81. if(string_cmp_str(cmd, "3v3") == 0) {
  82. power_cli_3v3(cli, args);
  83. break;
  84. }
  85. }
  86. power_cli_command_print_usage();
  87. } while(false);
  88. string_clear(cmd);
  89. }
  90. void power_on_system_start() {
  91. #ifdef SRV_CLI
  92. Cli* cli = furi_record_open("cli");
  93. cli_add_command(cli, "power", CliCommandFlagParallelSafe, power_cli, NULL);
  94. furi_record_close("cli");
  95. #endif
  96. }