File: libsys_apm.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 (92 lines) | stat: -rw-r--r-- 2,532 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
/*
 *  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 libsys_init(void) {
  struct stat sb;
  int rc;
  
  rc = stat(APM_PROC_FILE, &sb);
  if (rc < 0) {
    cp_log(LOG_ERR, "libsys_init(): %s: %s\n", APM_PROC_FILE, strerror(errno));
    return -1;
  }
  return 0;
}

void libsys_fini (void) {}

/* int scan_system_info(sys_info *s) 
 *
 * Reads APM 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];
  
  /***** APM SCAN *****/
  char ignore3[101];
  int ignore;
  unsigned int ignore2;
  unsigned int batt_flag;
    
  fp = fopen(APM_PROC_FILE , "r");
  if (!fp) {
    cp_log(LOG_ERR, "scan_system_info(): %s: %s\n", APM_PROC_FILE, strerror(errno));
    return -1;
  }
    
  if (!fgets(buf, 100, fp)) {
    fclose(fp);
    cp_log(LOG_ERR, "scan_system_info(): %s: %s\n", APM_PROC_FILE, strerror(errno));
    return -1;
  }
    
  sscanf(buf, "%s %d.%d %x %x %x %x %d%% %d %s\n",
                    s->version, &ignore, &ignore, &s->flags, &s->ac, &ignore2,
                    &batt_flag, &s->battery_percent, &s->battery_time, ignore3);

  if (!strncmp(ignore3, "sec", 3)) {
    s->battery_time /= 60;
  }
    
  if (s->battery_percent > 100) {
    s->battery_percent = -1;
  }

  s->has_battery = batt_flag < 128;
    
  fclose(fp);
  
  cp_log(LOG_INFO, "scan_system_info(): battery %s - %d - %s cpu %d\n",
                    s->has_battery?"present":"absent", 
                    s->battery_percent, 
                    s->ac?"on-line":"off-line",
		    s->cpu_percent );
  return 0;
}