Mercurial > hg > toybox
annotate toys/other/acpi.c @ 1064:0d7d6fed8141 draft
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
I could probably add -c fairly easily (print type, cur_state, and max_state)
-t is more difficult, since temperatures are reported in at least
1000 * degrees F and 10 * degrees C.
author | Isaac Dunham <ibid.ag@gmail.com> |
---|---|
date | Mon, 09 Sep 2013 11:19:59 -0500 |
parents | |
children | 9211bc984285 |
rev | line source |
---|---|
1064
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
1 /* acpi.c - show power state |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
2 * Written by Isaac Dunham, 2013 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
3 * No standard. |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
4 USE_ACPI(NEWTOY(acpi, "ab", TOYFLAG_USR|TOYFLAG_BIN)) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
5 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
6 config ACPI |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
7 bool "acpi" |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
8 default n |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
9 help |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
10 usage: acpi [-ab] |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
11 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
12 Show status of power sources. |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
13 -a show power adapters |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
14 -b show batteries |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
15 */ |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
16 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
17 #define FOR_acpi |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
18 #include "toys.h" |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
19 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
20 GLOBALS( |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
21 int ac; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
22 int bat; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
23 ) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
24 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
25 int read_int_at(int dirfd, char *name) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
26 int fd, ret=0; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
27 if ((fd=openat(dirfd, name, O_RDONLY)) < 0) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
28 return -1; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
29 FILE * fil = xfdopen(fd, "r"); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
30 fscanf(fil, "%d", &ret); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
31 fclose(fil); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
32 return ret; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
33 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
34 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
35 int acpi_callback(struct dirtree *tree) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
36 { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
37 errno = 0; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
38 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
39 if (tree->name[0]=='.') |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
40 return 0; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
41 if (strlen(dirtree_path(tree, NULL)) < 26) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
42 return (DIRTREE_RECURSE | DIRTREE_SYMFOLLOW); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
43 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
44 int dfd=open(dirtree_path(tree, NULL), O_RDONLY); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
45 if (dfd > 0) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
46 int fd; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
47 if ((fd = openat(dfd, "type", O_RDONLY)) < 0) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
48 close(dfd); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
49 return 0; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
50 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
51 read(fd, toybuf, 4096); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
52 close(fd); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
53 if (0 == strncmp(toybuf, "Battery", 7)) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
54 if (toys.optflags & FLAG_b || (!toys.optflags)) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
55 int cap = 0, curr = 0, max = 0; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
56 if ((cap = read_int_at(dfd, "capacity")) < 0) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
57 if ((max = read_int_at(dfd, "charge_full")) > 0) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
58 curr = read_int_at(dfd, "charge_now"); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
59 } else if ((max = read_int_at(dfd, "energy_full")) > 0) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
60 curr = read_int_at(dfd, "energy_now"); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
61 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
62 if (max > 0 && (curr >= 0)) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
63 cap = 100 * curr / max; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
64 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
65 if (cap >= 0) printf("Battery %d: %d%%\n", TT.bat++, cap); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
66 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
67 } else { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
68 //ac |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
69 if (toys.optflags & FLAG_a) { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
70 int on; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
71 if ((on = read_int_at(dfd, "online")) >= 0) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
72 printf("Adapter %d: %s-line\n", TT.ac++, (on ? "on" : "off")); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
73 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
74 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
75 close(dfd); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
76 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
77 return 0; |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
78 } |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
79 |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
80 void acpi_main(void) |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
81 { |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
82 dirtree_read("/sys/class/power_supply", acpi_callback); |
0d7d6fed8141
I got tired of trying to guess how much terminal time I could get in on my phone, so here's a basic acpi.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
83 } |