| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /* powerinfo: AC adapter and battery state from the board EC.
- *
- * The EC access lives in the kernel (SYS_POWER_INFO); this command only
- * formats the snapshot. The same syscall feeds the WM's top-bar AC and
- * battery indicators. Both the ThinkPad H8 EC and the Chrome OS EC fill
- * the same snapshot.
- */
- #include "ucmd_lib.h"
- static void put_milli(unsigned int v, const char *unit)
- {
- u_putdec(v / 1000u);
- u_putc('.');
- u_putc((char)('0' + (v % 1000u) / 100u));
- u_puts(unit);
- }
- static const char *cap_unit(const struct sys_battery *b)
- {
- if (!b->unit_known)
- return "EC units";
- return b->unit_mwh ? "mWh" : "mAh";
- }
- static void show_battery(unsigned int index, const struct sys_battery *b)
- {
- unsigned int health;
- u_puts("battery ");
- u_putdec(index);
- u_puts(": ");
- if (!b->present) {
- if (b->error)
- u_puts("read error\n");
- else
- u_puts("not present\n");
- return;
- }
- u_puts("present, ");
- if (b->charging)
- u_puts("charging");
- else if (b->discharging)
- u_puts("discharging");
- else if (b->percent_valid && b->percent >= 99)
- u_puts("full");
- else
- u_puts("idle");
- u_puts(" (status 0x");
- u_put_hex2(b->status);
- u_putc(')');
- if (b->error)
- u_puts(", read error");
- u_putc('\n');
- u_puts(" charge: ");
- if (b->percent_valid) {
- u_putdec(b->percent);
- u_putc('%');
- } else {
- u_puts("unknown");
- }
- u_putc('\n');
- if (b->full && b->percent_valid) {
- u_puts(" capacity: ");
- u_putdec(b->remaining);
- u_puts(" / ");
- u_putdec(b->full);
- u_putc(' ');
- u_puts(cap_unit(b));
- u_putc('\n');
- if (b->design) {
- u_puts(" design: ");
- u_putdec(b->design);
- u_putc(' ');
- u_puts(cap_unit(b));
- health = (b->full * 100u + b->design / 2u) / b->design;
- u_puts(", health ");
- u_putdec(health);
- u_putc('%');
- u_putc('\n');
- }
- }
- if (b->voltage_mv) {
- u_puts(" voltage: ");
- put_milli(b->voltage_mv, " V");
- if (b->rate) {
- unsigned int r = b->rate < 0 ?
- (unsigned int)(-b->rate) : (unsigned int)b->rate;
- u_puts(", rate: ");
- if (b->unit_known)
- put_milli(r, b->unit_mwh ? " W" : " A");
- else {
- u_putdec(r);
- u_puts(" EC units");
- }
- }
- u_putc('\n');
- }
- if (b->manufacturer[0] || b->model[0]) {
- u_puts(" ");
- if (b->model[0]) {
- u_puts("model ");
- u_puts(b->model);
- }
- if (b->manufacturer[0]) {
- if (b->model[0])
- u_puts(", ");
- u_puts("manufacturer ");
- u_puts(b->manufacturer);
- }
- u_putc('\n');
- }
- if (b->serial[0]) {
- u_puts(" serial ");
- u_puts(b->serial);
- u_putc('\n');
- }
- if (b->cycle_count) {
- u_puts(" cycles ");
- u_putdec(b->cycle_count);
- u_putc('\n');
- }
- }
- void _start(void)
- {
- struct sys_powerinfo p;
- unsigned int i;
- if (u_power_info(&p, SYS_POWER_F_DETAIL) != 0) {
- u_puts("powerinfo: unavailable\n");
- u_exit(1);
- }
- if (!p.present) {
- u_puts("no supported embedded controller detected\n");
- u_puts("battery/AC status unavailable on this platform\n");
- u_exit(0);
- }
- if (p.source == SYS_POWER_SRC_CROS)
- u_puts("chrome ec");
- else if (p.source == SYS_POWER_SRC_TPEC)
- u_puts("thinkpad");
- else
- u_puts("embedded controller");
- if (p.vendor[0]) {
- u_putc(' ');
- u_puts(p.vendor);
- }
- if (p.product[0]) {
- u_putc(' ');
- u_puts(p.product);
- }
- if (p.version[0]) {
- u_putc(' ');
- u_puts(p.version);
- }
- u_puts(", EC rev ");
- u_putdec(p.ec_rev);
- u_putc('\n');
- u_puts("ac adapter: ");
- if (p.ac_online < 0)
- u_puts("unknown\n");
- else if (p.ac_online)
- u_puts("online\n");
- else
- u_puts("offline\n");
- for (i = 0; i < SYS_POWER_BATT_MAX; i++)
- show_battery(i, &p.battery[i]);
- u_exit(0);
- }
|