power_cli.c 2.8 KB

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