File: libsys_acpi.c

package info (click to toggle)
cpufreqd 1.2.3-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,648 kB
  • ctags: 210
  • sloc: sh: 8,665; ansic: 1,648; makefile: 120
file content (237 lines) | stat: -rw-r--r-- 6,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 *  Copyright (C) 2002,2003,2004  Mattia Dongili<dongili@supereva.it>
 *                                George Staikos <staikos@0wned.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "libsys.h"

int no_dots(const struct dirent *d);

char (*bat_path)[255] = 0L;
char ac_file[255];
char version[101];
int bat_num = 0;
int read_batteries = 0;
float bat_full_capacity = 0;
int acpi_info_workaround = 0;
static int err = 0;

/* int no_dots(const struct dirent *d)
 * 
 * Filter function for scandir, returns
 * 0 if the first char of d_name is not
 * a dot.
 */
int no_dots(const struct dirent *d) {
  return d->d_name[0]!= '.';
}

/* void libsys_init(void)
 *
 * Initialize local variables.
 */
int libsys_init(void) {
  
  FILE *fp;
  struct dirent **namelist;
  int n = 0;
  
  fp = fopen(ACPI_PROC_DIR"info", "r");
  if (!fp) {
    cp_log(LOG_ERR, "libsys_init(): %s: %s\n", ACPI_PROC_DIR, strerror(errno));
    err++;
    return -1;
  }
  fscanf(fp, "version: %s", version);
  fclose(fp);

  cp_log(LOG_NOTICE, "libsys_init(): ACPICA version %s\n", version);
  
  /* get batteries paths */
  bat_num = n = scandir(ACPI_BATTERY_DIR, &namelist, no_dots, alphasort);
  if (n > 0) {
    bat_path = (char (*)[])malloc(bat_num*255*sizeof(char));
    
    while (n--) {
      /* put the path into the array */
      snprintf(bat_path[n], 255, "%s%s", ACPI_BATTERY_DIR, namelist[n]->d_name);
      cp_log(LOG_DEBUG, "libsys_init() - battery path: %s\n", bat_path[n]);
      free(namelist[n]);
    }
    free(namelist);
  } else {
    cp_log(LOG_ERR, "libsys_init() - no batteries found, not a laptop?\n");
    /*cp_log(LOG_ERR, "libsys_init() - scandir: %s\n", strerror(errno));
    err++;*/
  }
  cp_log(LOG_INFO, "libsys_init() - found %d battery slots\n", bat_num);
        
  /* get AC path */
  n = scandir(ACPI_AC_DIR, &namelist, no_dots, alphasort);
  if (n > 0) {
    while (n--) {
      snprintf(ac_file, 255, "%s%s/state", ACPI_AC_DIR, namelist[n]->d_name);
      cp_log(LOG_DEBUG, "libsys_init() - AC path: %s\n", ac_file);
      free(namelist[n]);
    } 
    free(namelist);
  } else {
    cp_log(LOG_DEBUG, "libsys_init(): no ac adapters found, not a laptop?\n");
  }
    
  return 0;
}

/* void libsys_fini (void)
 *
 * Free allocated structures.
 */
void libsys_fini (void) {
  if (bat_path)
    free(bat_path);
}

/* int scan_system_info(sys_info *s) 
 *
 * Reads ACPI info and fills the input sys_info struct.
 * Implements the prototype needed by cpufreqd to get
 * info from the PM layer.
 * 
 * Returns 0 on success, -1 otherwise
 *
 */
int scan_system_info(sys_info *s) {

  FILE *fp;
  char buf[101];

  int i = 0;
  float bat_total_capacity = 0;
  char line[5][101];
  char file_name[256];
  
  /* check if _init was successful */
  if (err>0)
    return -1;
  
  if (!(configuration->acpi_workaround && acpi_info_workaround == bat_num)) {
    bat_full_capacity = 0;
    read_batteries = 0;
  }
  
  /* Read battery informations */
  for (i=0; i<bat_num; i++) {
    /** /proc/acpi/battery/.../info **/
    snprintf(file_name, 255, "%s/info", bat_path[i]);

    /* avoid reading the info file if configured */
    if (configuration->acpi_workaround && acpi_info_workaround == bat_num) {
      cp_log(LOG_INFO, "scan_system_info(): not reading %s, ACPI workaround enabled.\n", file_name);
    
    } else {
      
      acpi_info_workaround++;
      fp = fopen(file_name, "r");
      if (!fp) {
        cp_log(LOG_ERR , "scan_system_info(): %s: %s\n", file_name, strerror(errno));
        return -1;
      }

      while (fgets(buf, 100, fp)) {
        memset(line, 0, 5*101);
        sscanf(buf, "%s %s %s %s %s\n", line[0], line[1], line[2], line[3], line[4]);
        /* last full capacity: 37740 mWh */
        if (strcmp(line[0], "last")==0 &&
            strcmp(line[1], "full")==0 &&
            strcmp(line[2], "capacity:")==0) {
          bat_full_capacity += atof(line[3]);
          read_batteries++;
        }

      }
      fclose(fp);
    }
      
    /* check battery state if there is at least 1 battery */
    if (read_batteries > 0) {
      /** /proc/acpi/battery/.../state **/
      snprintf(file_name, 255, "%s/state", bat_path[i]);
      fp = fopen(file_name, "r");
      if (!fp) {
        cp_log(LOG_ERR, "scan_system_info(): %s: %s\n", file_name, strerror(errno));
        return -1;
      }

      /* calculates medium battery life between all batteries */
      while (fgets(buf, 100, fp)) {
        memset(line, 0, 5*100);
        sscanf(buf, "%s %s %s %s %s\n", line[0], line[1], line[2], line[3], line[4]);
        /* present: yes */
        if (strcmp(line[0], "present:")==0) {
          if (strcmp(line[1], "yes")==0) {
            s->has_battery |= 1;
          } else {
            s->has_battery |= 0;
            break;
          }
        }
        /* remaining capacity: 34010 mWh */
        if (strcmp(line[0], "remaining")==0) {
          bat_total_capacity += atof(line[2]);
          break;
        }
      }
      fclose(fp);
    } /* end if has_battery*/
      
  } /* end bat_path loop */

  if (read_batteries > 0)
    s->battery_percent = 100 * (bat_total_capacity / (double)bat_full_capacity) / read_batteries;

  /* Read AC adapter state if it exists */
  if (ac_file[0] != 0) {
    fp = fopen(ac_file, "r");
    if (!fp) {
      cp_log(LOG_ERR, "scan_system_info(): %s: %s\n", ac_file, strerror(errno));
      return -1;
    }

    while (fgets(buf, 100, fp)) {
      memset(line, 0, 2*101);
      sscanf(buf, "%s %s\n", line[0], line[1]);
      /* state: on-line */
      if (strcmp(line[0], "state:")==0) {
        s->ac = strcmp(line[1], "on-line")==0 ? 1 : 0;
        break;
      }
    }
    fclose(fp);
  } else {
    /* uh! no AC dir? 
     * consider it online, maybe it's a desktop
     */
    s->ac = 1;
  }

  cp_log(LOG_INFO, "scan_system_info(): battery %s - %d - %s\n",
                    s->has_battery?"present":"absent", 
                    s->battery_percent, 
                    s->ac?"on-line":"off-line");
  
  return 0;
}