~ubuntu-branches/ubuntu/dapper/cpufreqd/dapper

« back to all changes in this revision

Viewing changes to src/cpufreqd_acpi_ac.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattia Dongili
  • Date: 2005-11-27 18:47:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127184742-9h26euwetr6kh1e6
Tags: 2.0.0-1

* New upstream release.
* cpufreqd.init: exit succesfully in case a stop is issued and
  cpufreqd is found running as requested by LSB thus making it
  possible to remove cpufreqd when cpufreqd itsef is stopped
  (closes: #340133)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2002-2005  Mattia Dongili <malattia@linux.it>
 
3
 *                           George Staikos <staikos@0wned.org>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
#include <dirent.h>
 
21
#include <errno.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include "cpufreqd_plugin.h"
 
26
 
 
27
#define ACPI_AC_DIR "/proc/acpi/ac_adapter/"
 
28
#define ACPI_AC_FILE "/state"
 
29
#define ACPI_AC_FORMAT "state:                   %s\n"
 
30
 
 
31
#define PLUGGED   1
 
32
#define UNPLUGGED 0
 
33
 
 
34
static char *ac_filelist[64];
 
35
static unsigned short ac_state;
 
36
static int ac_dir_num;
 
37
 
 
38
static int no_dots(const struct dirent *d) {
 
39
        return d->d_name[0]!= '.';
 
40
}
 
41
 
 
42
/*  static int acpi_ac_init(void)
 
43
 *
 
44
 *  test if AC dirs are present
 
45
 */
 
46
static int acpi_ac_init(void) {
 
47
        struct dirent **namelist = NULL;
 
48
        int n = 0;
 
49
 
 
50
        /* get AC path */
 
51
        n = scandir(ACPI_AC_DIR, &namelist, no_dots, NULL);
 
52
        if (n > 0) {
 
53
                ac_dir_num = n;
 
54
                *ac_filelist = malloc(n * 64 * sizeof(char));
 
55
                while (n--) {
 
56
                        snprintf(ac_filelist[n], 64, "%s%s%s", ACPI_AC_DIR, namelist[n]->d_name, ACPI_AC_FILE);
 
57
                        clog(LOG_INFO, "AC path %s\n", ac_filelist[n]);
 
58
                        free(namelist[n]);
 
59
                } 
 
60
                free(namelist);
 
61
 
 
62
        } else if (n < 0) {
 
63
                clog(LOG_DEBUG, "no acpi_ac module compiled or inserted? (%s: %s)\n",
 
64
                                ACPI_AC_DIR, strerror(errno));
 
65
                return -1;
 
66
 
 
67
        } else {
 
68
                clog(LOG_NOTICE, "no ac adapters found, not a laptop?\n");
 
69
                return -1;
 
70
        }
 
71
        return 0;
 
72
}
 
73
 
 
74
static int acpi_ac_exit(void) {
 
75
        if (ac_filelist != NULL)
 
76
                free(*ac_filelist);
 
77
        clog(LOG_INFO, "exited.\n");
 
78
        return 0;
 
79
}
 
80
 
 
81
/*  static int acpi_ac_update(void)
 
82
 *  
 
83
 *  reads temperature valuse ant compute a medium value
 
84
 */
 
85
static int acpi_ac_update(void) {
 
86
        char temp[50];
 
87
        int i=0;
 
88
        FILE *fp = NULL;
 
89
 
 
90
        ac_state = UNPLUGGED;
 
91
        clog(LOG_DEBUG, "called\n");
 
92
        for (i=0; i<ac_dir_num; i++) {
 
93
                fp = fopen(ac_filelist[i], "r");
 
94
                if (!fp) {
 
95
                        clog(LOG_ERR, "%s: %s\n", ac_filelist[i], strerror(errno));
 
96
                        return -1;
 
97
                }
 
98
                fscanf(fp, ACPI_AC_FORMAT, temp);
 
99
                fclose(fp);
 
100
 
 
101
                clog(LOG_DEBUG, "read %s\n", temp);
 
102
                ac_state |= (strncmp(temp, "on-line", 7)==0 ? PLUGGED : UNPLUGGED);
 
103
        }
 
104
 
 
105
        clog(LOG_INFO, "ac_adapter is %s\n",
 
106
                        ac_state==PLUGGED ? "on-line" : "off-line");
 
107
        return 0;
 
108
}
 
109
 
 
110
/*
 
111
 *  parse the 'ac' keywork
 
112
 */
 
113
static int acpi_ac_parse(const char *ev, void **obj) {
 
114
        int *ret = malloc(sizeof(int));
 
115
        if (ret == NULL) {
 
116
                clog(LOG_ERR, "couldn't make enough room for ac_status (%s)\n",
 
117
                                strerror(errno));
 
118
                return -1;
 
119
        }
 
120
 
 
121
        *ret = 0;
 
122
 
 
123
        clog(LOG_DEBUG, "called with: %s\n", ev);
 
124
 
 
125
        if (strncmp(ev, "on", 2) == 0) {
 
126
                *ret = PLUGGED;
 
127
        } else if (strncmp(ev, "off", 3) == 0) {
 
128
                *ret = UNPLUGGED;
 
129
        } else {
 
130
                clog(LOG_ERR, "couldn't parse %s\n", ev);
 
131
                free(ret);
 
132
                return -1;
 
133
        }
 
134
 
 
135
        clog(LOG_INFO, "parsed: %s\n", *ret==PLUGGED ? "on" : "off");
 
136
 
 
137
        *obj = ret;
 
138
        return 0;
 
139
}
 
140
 
 
141
/*
 
142
 *  evaluate the 'ac' keywork
 
143
 */
 
144
static int acpi_ac_evaluate(const void *s) {
 
145
        const int *ac = (const int *)s;
 
146
 
 
147
        clog(LOG_DEBUG, "called: %s [%s]\n",
 
148
                        *ac==PLUGGED ? "on" : "off", ac_state==PLUGGED ? "on" : "off");
 
149
 
 
150
        return (*ac == ac_state) ? MATCH : DONT_MATCH;
 
151
}
 
152
 
 
153
static struct cpufreqd_keyword kw[] = {
 
154
        { .word = "ac", .parse = &acpi_ac_parse, .evaluate = &acpi_ac_evaluate },
 
155
        { .word = NULL, .parse = NULL, .evaluate = NULL, .free = NULL }
 
156
};
 
157
 
 
158
static struct cpufreqd_plugin acpi_ac = {
 
159
        .plugin_name    = "acpi_ac_plugin",     /* plugin_name */
 
160
        .keywords       = kw,                   /* config_keywords */
 
161
        .plugin_init    = &acpi_ac_init,        /* plugin_init */
 
162
        .plugin_exit    = &acpi_ac_exit,        /* plugin_exit */
 
163
        .plugin_update  = &acpi_ac_update       /* plugin_update */
 
164
};
 
165
 
 
166
struct cpufreqd_plugin *create_plugin (void) {
 
167
        return &acpi_ac;
 
168
}