2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
39 * $Id: apm.c,v 1.18 2007/04/25 22:08:17 bernd67 Exp $
43 * Much of the ACPI code is taken from Florian Schaefers
44 * Acpi-Power Enlightenment epplet
53 /* APM related stuff */
55 #define APM_PROC "/proc/apm"
59 char driver_version[10];
60 int apm_version_major;
61 int apm_version_minor;
66 int battery_percentage;
72 /* ACPI related stuff */
74 #define ACPI_PROC "/proc/acpi/info"
76 static const char * acpi_info[] =
78 "/proc/acpi/battery/0/info",
79 "/proc/acpi/battery/1/info",
80 "/proc/acpi/battery/BATA/info",
81 "/proc/acpi/battery/BAT0/info",
82 "/proc/acpi/battery/BAT1/info"
85 static const char * acpi_state[] =
87 "/proc/acpi/battery/0/status",
88 "/proc/acpi/battery/1/status",
89 "/proc/acpi/battery/BATA/state",
90 "/proc/acpi/battery/BAT0/state",
91 "/proc/acpi/battery/BAT1/state"
95 #define ACPI_BT_CNT (sizeof(acpi_state) / sizeof(char *))
98 static const char * acpi_ac[] =
100 "/proc/acpi/ac_adapter/0/status",
101 "/proc/acpi/ac_adapter/AC/state",
102 "/proc/acpi/ac_adapter/ACAD/state"
105 #define ACPI_AC_CNT (sizeof(acpi_ac) / sizeof(char *))
111 static int method, fd_index;
116 apm_read_apm(struct olsr_apm_info *);
119 apm_read_acpi(struct olsr_apm_info *);
128 struct olsr_apm_info ainfo;
131 OLSR_PRINTF(3, "Initializing APM\n");
133 if(((fd_index = acpi_probe()) >= 0) && apm_read_acpi(&ainfo))
135 else if(apm_read_apm(&ainfo))
139 apm_printinfo(&ainfo);
146 apm_printinfo(struct olsr_apm_info *ainfo)
148 OLSR_PRINTF(5, "APM info:\n\tAC status %d\n\tBattery percentage %d%%\n\tBattery time left %d mins\n\n",
149 ainfo->ac_line_status,
150 ainfo->battery_percentage,
151 ainfo->battery_time_left);
156 apm_read(struct olsr_apm_info *ainfo)
161 return apm_read_apm(ainfo);
163 return apm_read_acpi(ainfo);
172 apm_read_apm(struct olsr_apm_info *ainfo)
177 struct linux_apm_info lainfo;
180 if((apm_procfile = fopen(APM_PROC, "r")) == NULL)
183 if(fgets(buffer, sizeof(buffer), apm_procfile) == NULL)
185 fclose(apm_procfile);
186 /* Try re-opening the file */
187 if((apm_procfile = fopen(APM_PROC, "r")) != NULL)
190 if(fgets(buffer, sizeof(buffer), apm_procfile) == NULL)
193 fprintf(stderr, "OLSRD: Could not read APM info - setting willingness to default");
194 fclose(apm_procfile);
198 fclose(apm_procfile);
201 //printf("READ: %s\n", buffer);
204 sscanf(buffer, "%s %d.%d %x %x %x %x %d%% %d %s\n",
205 lainfo.driver_version,
206 &lainfo.apm_version_major,
207 &lainfo.apm_version_minor,
209 &lainfo.ac_line_status,
210 &lainfo.battery_status,
211 &lainfo.battery_flags,
212 &lainfo.battery_percentage,
213 &lainfo.battery_time,
216 lainfo.using_minutes = !strncmp(units, "min", 3) ? 1 : 0;
219 * Should take care of old APM type info here
223 * Fix possible percentage error
225 if(lainfo.battery_percentage > 100)
226 lainfo.battery_percentage = -1;
228 /* Fill the provided struct */
230 if(lainfo.ac_line_status)
231 ainfo->ac_line_status = OLSR_AC_POWERED;
233 ainfo->ac_line_status = OLSR_BATTERY_POWERED;
235 ainfo->battery_percentage = lainfo.battery_percentage;
236 ainfo->battery_time_left = lainfo.battery_time;
243 apm_read_acpi(struct olsr_apm_info *ainfo)
246 int bat_max = 5000; /* Find some sane value */
249 printf("READING ACPI\n");
254 /* No battery was found or AC power was detected */
255 ainfo->ac_line_status = OLSR_AC_POWERED;
256 ainfo->battery_percentage = 100;
261 if((fd = fopen(acpi_info[fd_index], "r")) == NULL)
266 char s1[32], s2[32], s3[32], s4[32], inbuff[127];
267 if (fgets(inbuff, sizeof(inbuff), fd) == NULL)
270 sscanf(inbuff, "%s %s %s %s", s1, s2, s3, s4);
271 if (!strcasecmp(s2, "full"))
277 if((fd = fopen(acpi_state[fd_index], "r")) == NULL)
280 /* Extract battery status */
283 char s1[32], s2[32], s3[32], s4[32], inbuff[127];
284 if (fgets(inbuff, sizeof(inbuff), fd) == NULL)
286 sscanf(inbuff, "%s %s %s %s", s1, s2, s3, s4);
288 /* find remaining juice */
289 if(!strcasecmp(s1, "Remaining"))
294 ainfo->ac_line_status = OLSR_BATTERY_POWERED;
295 ainfo->battery_percentage = bat_val * 100 / bat_max;
306 /* First check for AC power */
308 for(i = 0; i < ACPI_AC_CNT; i++)
312 FILE *fd = fopen(acpi_ac[i], "r");
314 /* Try opening the info file */
319 rc = fscanf(fd, "%s %s", s1, s2);
321 /* Close info entry */
327 /* Running on AC power */
328 if(!strcasecmp(s2, "on-line"))
332 /* Only checking the first found battery entry... */
333 for(i = 0; i < ACPI_BT_CNT; i ++)
337 FILE *fd = fopen(acpi_info[i], "r");
339 /* Try opening the info file */
344 rc = fscanf(fd, "%s %s", s1, s2);
345 /* Close info entry */
351 /* Check if battery is present */
352 if((!strcasecmp(s1, "present:")) && (!strcasecmp(s2, "no")))
355 /* Open the corresponding state file */
356 if((fd = fopen(acpi_state[i], "r")) == NULL)
363 /* No battery found */