main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "led_ctl.h"
  6. void usage(const char *prog)
  7. {
  8. printf("Usage: %s <command> [args]\n", prog);
  9. printf("Commands:\n");
  10. printf(" color <r|g> Set LED color\n");
  11. printf(" heartoff turn off heart led\n");
  12. printf(" blink <on|off> [time] Set blink (time unit: 100ms, def: 5)\n");
  13. printf(" status Show current heart_led status\n");
  14. printf(" ac Show AC power status\n");
  15. printf(" btn Show rst button status \n");
  16. printf(" bat Show bat info \n");
  17. printf(" batledsys [state] Control Bat led with state by system\n");
  18. printf(" batledec Control Bat led by EC\n");
  19. printf("\nExample:\n");
  20. printf(" sudo %s color r (Set color to RED)\n", prog);
  21. printf(" sudo %s blink on 5 (Blink every 500ms)\n", prog);
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. if (geteuid() != 0)
  26. {
  27. fprintf(stderr, "Error: Must run as root (sudo).\n");
  28. return 1;
  29. }
  30. if (led_driver_init() < 0)
  31. {
  32. return 1;
  33. }
  34. if (argc < 2)
  35. {
  36. usage(argv[0]);
  37. return 1;
  38. }
  39. get_ec_version();
  40. const char *cmd = argv[1];
  41. if (strcmp(cmd, "color") == 0)
  42. {
  43. if (argc < 3)
  44. {
  45. fprintf(stderr, "Error: Missing color argument.\n");
  46. return 1;
  47. }
  48. if (strcmp(argv[2], "r") == 0)
  49. {
  50. if (led_set_color(LED_RED) == 0)
  51. printf("OK: Color set to RED\n");
  52. }
  53. else if (strcmp(argv[2], "g") == 0)
  54. {
  55. if (led_set_color(LED_GREEN) == 0)
  56. printf("OK: Color set to GREEN\n");
  57. }
  58. else
  59. {
  60. fprintf(stderr, "Error: Unknown color '%s'\n", argv[2]);
  61. return 1;
  62. }
  63. }
  64. else if (strcmp(cmd, "blink") == 0)
  65. {
  66. if (argc < 3)
  67. {
  68. fprintf(stderr, "Error: Missing state argument.\n");
  69. return 1;
  70. }
  71. if (strcmp(argv[2], "on") == 0)
  72. {
  73. int t = 5; // 500ms
  74. if (argc >= 4)
  75. t = atoi(argv[3]);
  76. if (t < 0 || t > 255)
  77. t = 5;
  78. if (led_set_blink(1, (uint8_t)t) == 0)
  79. printf("OK: Blink ON (Interval: %d00ms)\n", t);
  80. }
  81. else if (strcmp(argv[2], "off") == 0)
  82. {
  83. if (led_set_blink(0, 0) == 0)
  84. printf("OK: Blink OFF\n");
  85. }
  86. else
  87. {
  88. fprintf(stderr, "Error: Unknown state '%s'\n", argv[2]);
  89. return 1;
  90. }
  91. }
  92. else if (strcmp(cmd, "status") == 0)
  93. {
  94. led_print_status();
  95. }
  96. else if (strcmp(cmd, "heartoff") == 0)
  97. {
  98. heart_led_turn_off();
  99. }
  100. else if (strcmp(cmd, "batledsys") == 0)
  101. {
  102. if (argc < 3)
  103. {
  104. fprintf(stderr, "Error: Missing color argument.\n");
  105. return 1;
  106. }
  107. if (strcmp(argv[2], "0") == 0)
  108. {
  109. if (bat_led_set_color(0) == 1)
  110. printf("OK: Color set to green\n");
  111. }
  112. else if (strcmp(argv[2], "1") == 0)
  113. {
  114. if (bat_led_set_color(1) == 1)
  115. printf("OK: Color set to red\n");
  116. }
  117. else if (strcmp(argv[2], "2") == 0)
  118. {
  119. if (bat_led_set_color(2) == 1)
  120. printf("OK: Color set to green\n");
  121. }
  122. else if (strcmp(argv[2], "3") == 0)
  123. {
  124. if (bat_led_set_color(3) == 1)
  125. printf("OK: Color set to red\n");
  126. }
  127. else if (strcmp(argv[2], "4") == 0)
  128. {
  129. if (bat_led_set_color(4) == 1)
  130. printf("OK: turn off all \n");
  131. }
  132. else
  133. {
  134. fprintf(stderr, "Error: Unknown color '%s'\n", argv[2]);
  135. return 1;
  136. }
  137. }
  138. else if (strcmp(cmd, "batledec") == 0)
  139. {
  140. bat_led_ctrl_by_ec();
  141. }
  142. else if (strcmp(cmd, "bat") == 0)
  143. {
  144. get_bat_info();
  145. ac_bat_state();
  146. }
  147. else if (strcmp(cmd, "ac") == 0)
  148. {
  149. ac_present();
  150. }
  151. else if (strcmp(cmd, "btn") == 0)
  152. {
  153. soft_rest_btn();
  154. }
  155. else
  156. {
  157. usage(argv[0]);
  158. return 1;
  159. }
  160. return 0;
  161. }