ucmd_powerinfo.c 3.1 KB

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