1 From fbb0ec5e3cc2f5b6d9cd023fd5cf7ae38b97ad6a Mon Sep 17 00:00:00 2001
2 From: Cameron Katri <me@cameronkatri.com>
3 Date: Thu, 20 May 2021 15:03:17 -0400
4 Subject: [PATCH] acpiconf(8): Add libxo(3) support
7 usr.sbin/acpi/acpiconf/Makefile | 2 +
8 usr.sbin/acpi/acpiconf/acpiconf.8 | 12 ++-
9 usr.sbin/acpi/acpiconf/acpiconf.c | 122 ++++++++++++++++--------------
10 3 files changed, 80 insertions(+), 56 deletions(-)
12 diff --git a/usr.sbin/acpi/acpiconf/Makefile b/usr.sbin/acpi/acpiconf/Makefile
13 index 0bbadc1e8fb..479cf9563b2 100644
14 --- a/usr.sbin/acpi/acpiconf/Makefile
15 +++ b/usr.sbin/acpi/acpiconf/Makefile
16 @@ -5,4 +5,6 @@ PROG= acpiconf
22 .include <bsd.prog.mk>
23 diff --git a/usr.sbin/acpi/acpiconf/acpiconf.8 b/usr.sbin/acpi/acpiconf/acpiconf.8
24 index 53888d8489e..7e4e7c9564c 100644
25 --- a/usr.sbin/acpi/acpiconf/acpiconf.8
26 +++ b/usr.sbin/acpi/acpiconf/acpiconf.8
31 -.Dd December 27, 2018
37 .Nd control ACPI power management
44 @@ -46,6 +47,13 @@ utility allows the user control of the ACPI power management
46 The following command-line options are recognized:
47 .Bl -tag -width ".Fl s Ar type"
51 +in a selection of different human and machine readable formats.
54 +for details on command line arguments.
56 Displays a summary of available options.
58 @@ -96,6 +104,8 @@ for more information about
63 +.Xr xo_parse_args 3 ,
67 diff --git a/usr.sbin/acpi/acpiconf/acpiconf.c b/usr.sbin/acpi/acpiconf/acpiconf.c
68 index 8a31657fb98..f4d5e06b0d3 100644
69 --- a/usr.sbin/acpi/acpiconf/acpiconf.c
70 +++ b/usr.sbin/acpi/acpiconf/acpiconf.c
72 #include <sys/ioctl.h>
75 +#include <libxo/xo.h>
77 #include <dev/acpica/acpiio.h>
79 @@ -53,7 +54,7 @@ acpi_init(void)
81 acpifd = open(ACPIDEV, O_RDONLY);
83 - err(EX_OSFILE, ACPIDEV);
84 + xo_err(EX_OSFILE, ACPIDEV);
87 /* Prepare to sleep and then wait for the signal that sleeping can occur. */
88 @@ -65,7 +66,7 @@ acpi_sleep(int sleep_type)
89 /* Notify OS that we want to sleep. devd(8) gets this notify. */
90 ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
92 - err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
93 + xo_err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
96 /* Ack or abort a pending suspend request. */
97 @@ -76,7 +77,7 @@ acpi_sleep_ack(int err_val)
99 ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
101 - err(EX_IOERR, "ack sleep type failed");
102 + xo_err(EX_IOERR, "ack sleep type failed");
105 /* should be a acpi define, but doesn't appear to be */
106 @@ -92,78 +93,82 @@ acpi_battinfo(int num)
109 if (num < 0 || num > 64)
110 - errx(EX_USAGE, "invalid battery %d", num);
111 + xo_errx(EX_USAGE, "invalid battery %d", num);
113 /* Print battery design information. */
115 if (ioctl(acpifd, ACPIIO_BATT_GET_BIX, &battio) == -1)
116 - err(EX_IOERR, "get battery info (%d) failed", num);
117 + xo_err(EX_IOERR, "get battery info (%d) failed", num);
118 amp = battio.bix.units;
119 pwr_units = amp ? "mA" : "mW";
120 if (battio.bix.dcap == UNKNOWN_CAP)
121 - printf("Design capacity:\tunknown\n");
122 + xo_emit("{Lc:Design capacity}{P:\t}{:design-capacity/unknown}\n");
124 - printf("Design capacity:\t%d %sh\n", battio.bix.dcap,
126 + xo_emit("{Lc:Design capacity}{P:\t}{:design-capacity/%d}{Uw:/%sh}\n",
127 + battio.bix.dcap, pwr_units);
128 if (battio.bix.lfcap == UNKNOWN_CAP)
129 - printf("Last full capacity:\tunknown\n");
130 + xo_emit("{Lc:Last full capacity}{P:\t}{:last-full-capacity/unknown}\n");
132 - printf("Last full capacity:\t%d %sh\n", battio.bix.lfcap,
134 - printf("Technology:\t\t%s\n", battio.bix.btech == 0 ?
135 + xo_emit("{Lc:Last full capacity}{P:\t}{:last-full-capacity/%d}{Uw:/%sh}\n",
136 + battio.bix.lfcap, pwr_units);
137 + xo_emit("{Lc:Technology}{P:\t\t}{:technology}\n", battio.bix.btech == 0 ?
138 "primary (non-rechargeable)" : "secondary (rechargeable)");
139 if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_1)) {
140 - printf("Battery Swappable Capability:\t");
141 + xo_emit("{Lc:Battery Swappable Capability}{P:\t}");
142 if (battio.bix.scap == ACPI_BIX_SCAP_NO)
143 - printf("Non-swappable\n");
144 + xo_emit("{:swappable/Non-swappable/no}\n");
145 else if (battio.bix.scap == ACPI_BIX_SCAP_COLD)
146 - printf("cold swap\n");
147 + xo_emit("{:swappable/cold swap/cold}\n");
148 else if (battio.bix.scap == ACPI_BIX_SCAP_HOT)
149 - printf("hot swap\n");
150 + xo_emit("{:swappable/hot swap/hot}\n");
152 - printf("unknown\n");
153 + xo_emit("{:swappable/unknown}\n");
155 if (battio.bix.dvol == UNKNOWN_CAP)
156 - printf("Design voltage:\t\tunknown\n");
157 + xo_emit("{Lc:Design voltage}{P:\t\t}{:voltage/unknown}\n");
159 - printf("Design voltage:\t\t%d mV\n", battio.bix.dvol);
160 - printf("Capacity (warn):\t%d %sh\n", battio.bix.wcap, pwr_units);
161 - printf("Capacity (low):\t\t%d %sh\n", battio.bix.lcap, pwr_units);
162 + xo_emit("{Lc:Design voltage}{P:\t\t}{:voltage/%d}{Uw:mV}\n", battio.bix.dvol);
163 + xo_emit("{Lc:Capacity (warn)}{P:\t}{:warn-capacity/%d}{Uw:/%sh}\n",
164 + battio.bix.wcap, pwr_units);
165 + xo_emit("{Lc:Capacity (low)}{P:\t\t}{:low-capacity/%d}{Uw:/%sh}\n",
166 + battio.bix.lcap, pwr_units);
167 if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_0)) {
168 if (battio.bix.cycles != ACPI_BATT_UNKNOWN)
169 - printf("Cycle Count:\t\t%d\n", battio.bix.cycles);
170 - printf("Mesurement Accuracy:\t%d %%\n",
171 + xo_emit("{Lc:Cycle Count}{P:\t\t}{:cycles/%d}\n", battio.bix.cycles);
172 + xo_emit("{Lc:Mesurement Accuracy}{P:\t}{:accuracy/%d}{Uw:%%}\n",
173 battio.bix.accuracy / 1000);
174 if (battio.bix.stmax != ACPI_BATT_UNKNOWN)
175 - printf("Max Sampling Time:\t%d ms\n",
176 + xo_emit("{Lc:Max Sampling Time}{P:\t}{:sampling-max/%d}{Uw:ms}\n",
178 if (battio.bix.stmin != ACPI_BATT_UNKNOWN)
179 - printf("Min Sampling Time:\t%d ms\n",
180 + xo_emit("{Lc:Min Sampling Time}{P:\t}{:sampling-min/%d}{Uw:ms}\n",
182 - printf("Max Average Interval:\t%d ms\n",
183 + xo_emit("{Lc:Max Average Interval}{P:\t}{:avg-interval-max/%d}{Uw:ms}\n",
185 - printf("Min Average Interval:\t%d ms\n",
186 + xo_emit("{Lc:Min Average Interval}{P:\t}{:avg-interval-min/%d}{Uw:ms}\n",
189 - printf("Low/warn granularity:\t%d %sh\n", battio.bix.gra1, pwr_units);
190 - printf("Warn/full granularity:\t%d %sh\n", battio.bix.gra2, pwr_units);
191 - printf("Model number:\t\t%s\n", battio.bix.model);
192 - printf("Serial number:\t\t%s\n", battio.bix.serial);
193 - printf("Type:\t\t\t%s\n", battio.bix.type);
194 - printf("OEM info:\t\t%s\n", battio.bix.oeminfo);
195 + xo_emit("{Lc:Low\\/warn granularity}{P:\t}{:granularity-lw/%d}{Uw:/%sh}\n",
196 + battio.bix.gra1, pwr_units);
197 + xo_emit("{Lc:Warn\\/full granularity}{P:\t}{:granularity-wf/%d}{Uw:/%sh}\n",
198 + battio.bix.gra2, pwr_units);
199 + xo_emit("{Lc:Model number}{P:\t\t}{:model}\n", battio.bix.model);
200 + xo_emit("{Lc:Serial number}{P:\t\t}{:serial}\n", battio.bix.serial);
201 + xo_emit("{Lc:Type}{P:\t\t\t}{:type}\n", battio.bix.type);
202 + xo_emit("{Lc:OEM info}{P:\t\t}{:oeminfo}\n", battio.bix.oeminfo);
204 /* Fetch battery voltage information. */
205 volt = UNKNOWN_VOLTAGE;
207 if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
208 - err(EX_IOERR, "get battery status (%d) failed", num);
209 + xo_err(EX_IOERR, "get battery status (%d) failed", num);
210 if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
211 volt = battio.bst.volt;
213 /* Print current battery state information. */
215 if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
216 - err(EX_IOERR, "get battery user info (%d) failed", num);
217 + xo_err(EX_IOERR, "get battery user info (%d) failed", num);
218 if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
220 switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
221 @@ -188,36 +193,36 @@ acpi_battinfo(int num)
225 - printf("State:\t\t\t%s\n", state);
226 + xo_emit("{Lc:State}{P:\t\t\t}{:state}\n", state);
227 if (battio.battinfo.cap == -1)
228 - printf("Remaining capacity:\tunknown\n");
229 + xo_emit("{Lc:Remaining capacity}{P:\t}{:remaining-capacity/unknown}\n");
231 - printf("Remaining capacity:\t%d%%\n",
232 + xo_emit("{Lc:Remaining capacity}{P:\t}{:remaining-capacity/%d}{D:%%}\n",
233 battio.battinfo.cap);
234 if (battio.battinfo.min == -1)
235 - printf("Remaining time:\t\tunknown\n");
236 + xo_emit("{Lc:Remaining time}{P:\t\t}{:remaining-time/unknown}\n");
238 hours = battio.battinfo.min / 60;
239 min = battio.battinfo.min % 60;
240 - printf("Remaining time:\t\t%d:%02d\n", hours, min);
241 + xo_emit("{Lc:Remaining time}{P:\t\t}{:remaining-time/%d:%02d}\n", hours, min);
243 if (battio.battinfo.rate == -1)
244 - printf("Present rate:\t\tunknown\n");
245 + xo_emit("{Lc:Present rate}{P:\t\t}{:present-rate/unknown}\n");
246 else if (amp && volt != UNKNOWN_VOLTAGE) {
247 - printf("Present rate:\t\t%d mA (%d mW)\n",
248 + xo_emit("{Lc:Present rate}{P:\t\t}{:present-rate/%d}{Uw:mA} {D:(}{:present-rate-mw/%d}{Uw:mW}{D:)}\n",
249 battio.battinfo.rate,
250 battio.battinfo.rate * volt / 1000);
252 - printf("Present rate:\t\t%d %s\n",
253 + xo_emit("{Lc:Present rate}{P:\t\t}{:present-rate/%d}{Uw:/%s}\n",
254 battio.battinfo.rate, pwr_units);
256 - printf("State:\t\t\tnot present\n");
257 + xo_emit("{Lc:State}{P:\t\t\t}{:state/not present}\n");
259 /* Print battery voltage information. */
260 if (volt == UNKNOWN_VOLTAGE)
261 - printf("Present voltage:\tunknown\n");
262 + xo_emit("{Lc:Present voltage}{P:\t}{q:present-voltage/unknown}\n");
264 - printf("Present voltage:\t%d mV\n", volt);
265 + xo_emit("{Lc:Present voltage}{P:\t}{q:present-voltage/%d}{Uw:mV}\n", volt);
269 @@ -225,7 +230,9 @@ acpi_battinfo(int num)
271 usage(const char* prog)
273 - printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
274 + xo_error("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
275 + xo_close_container("acpiconf");
280 @@ -237,6 +244,9 @@ main(int argc, char *argv[])
281 int iflag = 0, kflag = 0, sflag = 0;
285 + argc = xo_parse_args(argc, argv);
286 + xo_open_container("acpiconf");
290 @@ -249,13 +259,13 @@ main(int argc, char *argv[])
292 battery = strtol(optarg, &end, 10);
293 if ((size_t)(end - optarg) != strlen(optarg))
294 - errx(EX_USAGE, "invalid battery");
295 + xo_errx(EX_USAGE, "invalid battery");
299 ack = strtol(optarg, &end, 10);
300 if ((size_t)(end - optarg) != strlen(optarg))
301 - errx(EX_USAGE, "invalid ack argument");
302 + xo_errx(EX_USAGE, "invalid ack argument");
306 @@ -263,9 +273,9 @@ main(int argc, char *argv[])
308 sleep_type = strtol(optarg, &end, 10);
309 if ((size_t)(end - optarg) != strlen(optarg))
310 - errx(EX_USAGE, "invalid sleep type");
311 + xo_errx(EX_USAGE, "invalid sleep type");
312 if (sleep_type < 1 || sleep_type > 4)
313 - errx(EX_USAGE, "invalid sleep type (%d)",
314 + xo_errx(EX_USAGE, "invalid sleep type (%d)",
318 @@ -278,19 +288,19 @@ main(int argc, char *argv[])
321 if (iflag != 0 && kflag != 0 && sflag != 0)
322 - errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
323 + xo_errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
327 - errx(EX_USAGE, "-i and -k are mutually exclusive");
328 + xo_errx(EX_USAGE, "-i and -k are mutually exclusive");
330 - errx(EX_USAGE, "-i and -s are mutually exclusive");
331 + xo_errx(EX_USAGE, "-i and -s are mutually exclusive");
332 acpi_battinfo(battery);
337 - errx(EX_USAGE, "-k and -s are mutually exclusive");
338 + xo_errx(EX_USAGE, "-k and -s are mutually exclusive");
342 @@ -299,5 +309,7 @@ main(int argc, char *argv[])
343 acpi_sleep(sleep_type);
346 + xo_close_container("acpiconf");