ucmd_powerinfo.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* powerinfo: AC adapter and battery state from the board EC.
  2. *
  3. * The EC access lives in the kernel (SYS_POWER_INFO); this command only
  4. * formats the snapshot. The same syscall feeds the WM's top-bar AC and
  5. * battery indicators. Both the ThinkPad H8 EC and the Chrome OS EC fill
  6. * the same snapshot.
  7. */
  8. #include "ucmd_lib.h"
  9. static void put_milli(unsigned int v, const char *unit)
  10. {
  11. u_putdec(v / 1000u);
  12. u_putc('.');
  13. u_putc((char)('0' + (v % 1000u) / 100u));
  14. u_puts(unit);
  15. }
  16. static const char *cap_unit(const struct sys_battery *b)
  17. {
  18. if (!b->unit_known)
  19. return "EC units";
  20. return b->unit_mwh ? "mWh" : "mAh";
  21. }
  22. static void show_battery(unsigned int index, const struct sys_battery *b)
  23. {
  24. unsigned int health;
  25. u_puts("battery ");
  26. u_putdec(index);
  27. u_puts(": ");
  28. if (!b->present) {
  29. if (b->error)
  30. u_puts("read error\n");
  31. else
  32. u_puts("not present\n");
  33. return;
  34. }
  35. u_puts("present, ");
  36. if (b->charging)
  37. u_puts("charging");
  38. else if (b->discharging)
  39. u_puts("discharging");
  40. else if (b->percent_valid && b->percent >= 99)
  41. u_puts("full");
  42. else
  43. u_puts("idle");
  44. u_puts(" (status 0x");
  45. u_put_hex2(b->status);
  46. u_putc(')');
  47. if (b->error)
  48. u_puts(", read error");
  49. u_putc('\n');
  50. u_puts(" charge: ");
  51. if (b->percent_valid) {
  52. u_putdec(b->percent);
  53. u_putc('%');
  54. } else {
  55. u_puts("unknown");
  56. }
  57. u_putc('\n');
  58. if (b->full && b->percent_valid) {
  59. u_puts(" capacity: ");
  60. u_putdec(b->remaining);
  61. u_puts(" / ");
  62. u_putdec(b->full);
  63. u_putc(' ');
  64. u_puts(cap_unit(b));
  65. u_putc('\n');
  66. if (b->design) {
  67. u_puts(" design: ");
  68. u_putdec(b->design);
  69. u_putc(' ');
  70. u_puts(cap_unit(b));
  71. health = (b->full * 100u + b->design / 2u) / b->design;
  72. u_puts(", health ");
  73. u_putdec(health);
  74. u_putc('%');
  75. u_putc('\n');
  76. }
  77. }
  78. if (b->voltage_mv) {
  79. u_puts(" voltage: ");
  80. put_milli(b->voltage_mv, " V");
  81. if (b->rate) {
  82. unsigned int r = b->rate < 0 ?
  83. (unsigned int)(-b->rate) : (unsigned int)b->rate;
  84. u_puts(", rate: ");
  85. if (b->unit_known)
  86. put_milli(r, b->unit_mwh ? " W" : " A");
  87. else {
  88. u_putdec(r);
  89. u_puts(" EC units");
  90. }
  91. }
  92. u_putc('\n');
  93. }
  94. if (b->manufacturer[0] || b->model[0]) {
  95. u_puts(" ");
  96. if (b->model[0]) {
  97. u_puts("model ");
  98. u_puts(b->model);
  99. }
  100. if (b->manufacturer[0]) {
  101. if (b->model[0])
  102. u_puts(", ");
  103. u_puts("manufacturer ");
  104. u_puts(b->manufacturer);
  105. }
  106. u_putc('\n');
  107. }
  108. if (b->serial[0]) {
  109. u_puts(" serial ");
  110. u_puts(b->serial);
  111. u_putc('\n');
  112. }
  113. if (b->cycle_count) {
  114. u_puts(" cycles ");
  115. u_putdec(b->cycle_count);
  116. u_putc('\n');
  117. }
  118. }
  119. void _start(void)
  120. {
  121. struct sys_powerinfo p;
  122. unsigned int i;
  123. if (u_power_info(&p, SYS_POWER_F_DETAIL) != 0) {
  124. u_puts("powerinfo: unavailable\n");
  125. u_exit(1);
  126. }
  127. if (!p.present) {
  128. u_puts("no supported embedded controller detected\n");
  129. u_puts("battery/AC status unavailable on this platform\n");
  130. u_exit(0);
  131. }
  132. if (p.source == SYS_POWER_SRC_CROS)
  133. u_puts("chrome ec");
  134. else if (p.source == SYS_POWER_SRC_TPEC)
  135. u_puts("thinkpad");
  136. else
  137. u_puts("embedded controller");
  138. if (p.vendor[0]) {
  139. u_putc(' ');
  140. u_puts(p.vendor);
  141. }
  142. if (p.product[0]) {
  143. u_putc(' ');
  144. u_puts(p.product);
  145. }
  146. if (p.version[0]) {
  147. u_putc(' ');
  148. u_puts(p.version);
  149. }
  150. u_puts(", EC rev ");
  151. u_putdec(p.ec_rev);
  152. u_putc('\n');
  153. u_puts("ac adapter: ");
  154. if (p.ac_online < 0)
  155. u_puts("unknown\n");
  156. else if (p.ac_online)
  157. u_puts("online\n");
  158. else
  159. u_puts("offline\n");
  160. for (i = 0; i < SYS_POWER_BATT_MAX; i++)
  161. show_battery(i, &p.battery[i]);
  162. u_exit(0);
  163. }