2 * OLSR ad-hoc routing table management protocol
3 * Copyright (C) 2003 Andreas Tønnesen (andreto@ifi.uio.no)
5 * This file is part of uolsrd.
7 * uolsrd is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * uolsrd is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with uolsrd; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define APM_PROC "/proc/apm"
31 char driver_version[10];
32 int apm_version_major;
33 int apm_version_minor;
38 int battery_percentage;
45 olsr_printf(int, char *, ...);
51 struct olsr_apm_info ainfo;
53 olsr_printf(3, "Initializing APM\n");
56 apm_printinfo(&ainfo);
62 apm_printinfo(struct olsr_apm_info *ainfo)
64 olsr_printf(5, "APM info:\n\tAC status %d\n\tBattery percentage %d%%\n\n",
65 ainfo->ac_line_status,
66 ainfo->battery_percentage);
73 apm_read(struct olsr_apm_info *ainfo)
78 struct linux_apm_info lainfo;
81 if((apm_procfile = fopen(APM_PROC, "r")) == NULL)
85 fgets(buffer, sizeof(buffer) - 1, apm_procfile);
88 /* Try re-opening the file */
89 if((apm_procfile = fopen(APM_PROC, "r")) < 0)
91 fgets(buffer, sizeof(buffer) - 1, apm_procfile);
95 fprintf(stderr, "OLSRD: Could not read APM info - setting willingness to default");
101 buffer[sizeof(buffer) - 1] = '\0';
103 //printf("READ: %s\n", buffer);
106 sscanf(buffer, "%s %d.%d %x %x %x %x %d%% %d %s\n",
107 lainfo.driver_version,
108 &lainfo.apm_version_major,
109 &lainfo.apm_version_minor,
111 &lainfo.ac_line_status,
112 &lainfo.battery_status,
113 &lainfo.battery_flags,
114 &lainfo.battery_percentage,
115 &lainfo.battery_time,
118 lainfo.using_minutes = !strncmp(units, "min", 3) ? 1 : 0;
121 * Should take care of old APM type info here
125 * Fix possible percentage error
127 if(lainfo.battery_percentage > 100)
128 lainfo.battery_percentage = -1;
130 /* Fill the provided struct */
132 if(lainfo.ac_line_status)
133 ainfo->ac_line_status = OLSR_AC_POWERED;
135 ainfo->ac_line_status = OLSR_BATTERY_POWERED;
137 ainfo->battery_percentage = lainfo.battery_percentage;
139 fclose(apm_procfile);