File: acpi.cpp

package info (click to toggle)
powersave 0.14.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,764 kB
  • ctags: 999
  • sloc: sh: 11,357; cpp: 8,103; ansic: 2,631; makefile: 388
file content (406 lines) | stat: -rw-r--r-- 12,200 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/***************************************************************************
 *                                                                         *
 *                         Powersave Daemon                                *
 *                                                                         *
 *          Copyright (C) 2004,2005 SUSE Linux Products GmbH               *
 *                                                                         *
 *               Author(s): Holger Macht <hmacht@suse.de>                  *
 *                                                                         *
 * 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 you   *
 * 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., *
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  *
 *                                                                         *
 ***************************************************************************/

#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>

#include <sstream>

#include "acpi.h"
#include "stringutil.h"
#include "dbus_server.h"
#include "event_management.h"
#include "globals.h"

using namespace Powersave::Globals;

string ACPI_Interface::ACPI_EVENTFILE = "/proc/acpi/event";

ACPI_Interface::ACPI_Interface():PM_Interface()
{
	pDebug(DBG_DEBUG, "Constructor ACPI_Interface");

	_last_button_occurence = getTimestamp();

	if (setBatteryAlarm_() < 0) {
		pDebug(DBG_DIAG, "could not set battery alarm");
	}

}

ACPI_Interface::~ACPI_Interface()
{
	/* reset thermal trip points to BIOS settings (general_conf object
	 * stored BIOS settings) */
	if (_thermal_trip_points_supported 
	    && (config_obj->ENABLE_THERMAL_MANAGEMENT == KERNEL
		|| config_obj->ENABLE_THERMAL_MANAGEMENT == KERNEL_PASSIVE)) {
		for (int x = 0;
		     x < MAX_THERMAL_ZONES && config_obj->thermal_zones[x].present;
		     x++) {
			// ignore thermal zones without a passive trip point in
			// KERNEL_PASSIVE mode
			if (config_obj->ENABLE_THERMAL_MANAGEMENT == KERNEL_PASSIVE 
			    && config_obj->thermal_zones[x].passive < 1)
				continue;
			setThermalTrippoints(x, config_obj->thermal_zones[x]);
		}
	}
}

int ACPI_Interface::openHWEventFD()
{
	int fd;

	char discard[MAX_LINE_SIZE];
	struct stat file_stats;

	if (stat(ACPI_EVENTFILE.c_str(), &file_stats)) {
		pDebug(DBG_DIAG, "Cannot stat %s", ACPI_EVENTFILE.c_str());
		return -1;
	}
	if (S_ISSOCK(file_stats.st_mode)) {
		if ((fd = openAcpidSocket()) < 0) {
			pDebug(DBG_DIAG, "Cannot connect to acpid socket %s",
			       ACPI_EVENTFILE.c_str());
			return -1;
		}
	} else if (S_ISREG(file_stats.st_mode)) {
		fd = open(ACPI_EVENTFILE.c_str(), O_RDONLY | O_NONBLOCK);
		if (fd < 0) {
			pDebug(DBG_WARN, "Cannot open %s: %s",
			       ACPI_EVENTFILE.c_str(), strerror(errno));
			return -1;
		}
	} else {
		pDebug(DBG_DIAG, "No valid acpi event file: %s", ACPI_EVENTFILE.c_str());
		return -1;
	}
	// discard any events happened before
	while (read(fd, discard, sizeof(discard)) > 0) {
		pDebug(DBG_INFO, "Event '%s' discarded", discard);
	}
	// set errno back: could be EAGAIN if no data is available
	errno = 0;
	return fd;
}

int ACPI_Interface::handleHWEventRequest(int fd)
{
	char buf[2] = "";
	int x = 0, y;
	std::string line = "";
	std::string type;
	std::string dev_name;
	std::string port;
	std::string count;
	long time_in_millisecs = 0;

	/* this may come from acpid socket or from /proc/acpi/event */
	while ((y = read(fd, buf, 1)) > 0) {
		line += buf;
		x++;
		if (*buf == '\n')
			break;
	}
	// end of file ? - maybe acpid and/or socket missing ?
	if (x == 0) {
		pDebug(DBG_DIAG, "hwEvent is empty or not present, fd: %d", fd);
		return -2;
	}
	// error 
	else if (y < 0) {
		int tmp = DBG_WARN;
		if (errno == EAGAIN)
			tmp = DBG_DIAG;
		pDebug(tmp, "Could not read from hwEvent fd %d: %s",
		       fd, strerror(errno));
		// this seems to be a real error; we can also get EAGAIN which
		// does not have to be an error (but should be handled, TODO!)
		if (line.empty())
			return -2;
	}
	// just to make sure
	// this also removes the trailing '\n'
	line = stripLeadingWS(line);
	line = stripTrailingWS(line);

	// empty event?
	if (line.empty())
		return 0;

	pDebug(DBG_DIAG, "ACPI Event: '%s' ignore buttons: %s",
	       line.c_str(), (_sleep_triggered ? "yes" : "no"));

	std::istringstream eStream(line.c_str());

	eStream >> type >> dev_name >> port >> count;
	// check if any of obove sections are empty
	if (!type.length() || !dev_name.length() || !port.length() || !count.length()) {
		pDebug(DBG_WARN, "Empty or incomplete event. Ignoring...");
		// we should not get here since we already tested for empty events above.
		return 1;
	}
	pDebug(DBG_DEBUG, "type: %s, dev_name: %s, port: %s, count: %s",
	       type.c_str(), dev_name.c_str(), port.c_str(), count.c_str());

	// notify the clients about an acpi event
	DBus_Server::emitSignal("AcpiEvent", line.c_str());

	if (type == "button/sleep") {
		if (_sleep_triggered) {
			pDebug(DBG_DIAG, "Sleep button ignored while resuming.");
			return 0;
		}

		// workaround for double button occurencies
		time_in_millisecs = getTimestamp();
		if ((time_in_millisecs - _last_button_occurence) 
		    < _button_time_window) {
			pDebug(DBG_DIAG, 
			       "button.sleep event occured twice in %dms, ignored", _button_time_window);
			_last_button_occurence = time_in_millisecs;
			return 0;
		} else {
			_last_button_occurence = time_in_millisecs;
			pDebug(DBG_INFO, "button.sleep event occured");
			_eM->executeEvent("button.sleep", line.c_str());
			return 1;
		}
	} else if (type == "button/power") {
		if (_sleep_triggered) {
			pDebug(DBG_DIAG, "Power button ignored while resuming.");
			return 0;
		}
		// workaround for double button occurencies
		time_in_millisecs = getTimestamp();
		if ((time_in_millisecs - _last_button_occurence)
		    < _button_time_window) {
			pDebug(DBG_DIAG, 
			       "button.power event occured twice in %dms, ignored", _button_time_window);
			_last_button_occurence = time_in_millisecs;
			return 0;
		} else {
			// wait a bit, maybe the machine should be cut off power
			if (config_obj->current_scheme->POWER_BUTTON_DELAY > 0)
				sleep(config_obj->current_scheme->POWER_BUTTON_DELAY);
			// get new timestamp because of sleep.
			_last_button_occurence = getTimestamp();
			pDebug(DBG_INFO, "button.power event occured");
			_eM->executeEvent("button.power", line.c_str());
			return 1;
		}
	} else if (type == "button/lid") {
		if (_sleep_triggered) {
			pDebug(DBG_DIAG, "Lid button ignored while resuming.");
			return 0;
		}
		switch (getLidState()) {
		case LID_OPEN:
			pDebug(DBG_INFO, "lid open event occured");
			_eM->executeEvent("button.lid.open", line.c_str());
			return 1;
		case LID_CLOSED:
			pDebug(DBG_INFO, "lid closed event occured");
			_eM->executeEvent("button.lid.closed", line.c_str());
			return 1;
		default:
			pDebug(DBG_ERR, "Could not determine lid state");
			return -1;
		}
	} else if (type == "battery") {
		// This is to avoid polling of battery.
		// set /proc/acpi/battery/*/alarm and wait for events in /proc/acpi/event

		// adjust battery alarm here, maybe we have a false alarm!
		if (setBatteryAlarm_() < 0) {
			pDebug(DBG_DIAG, "could not set battery alarm");
		}
		return 1;
	}
	// break if else to execute a battery event if ac_offline 
	// and battery state is not normal
	else if (type == "ac_adapter") {
		if (checkACStateChanges() < 0) {
			pDebug(DBG_WARN, "Could not read out AC_Adapter state.");
			return -1;
		}
		return 1;
	} else if (type == "thermal_zone") {
		pDebug(DBG_INFO, "Thermal event happend.");
		checkTemperatureStateChanges();
		return 1;
	} else if (type == "processor") {
		pDebug(DBG_INFO, "Processor event happend -> ignoring.");
		return 1;
	} else {
		pDebug(DBG_DIAG, "unknown HW event, using [other]."
		       " type '%s' dev_name '%s' port '%s' count '%s'",
		       type.c_str(), dev_name.c_str(), port.c_str(), count.c_str());
		_eM->executeEvent("other", line.c_str());
		return 1;
	}
}

int ACPI_Interface::setBatteryAlarm_()
{
	int ret = 1;
	static bool supported = true;

	if (!supported)
		return 1;


	switch (_battery.batteryState()) {
	case BAT_NORM_STATE:
		if (setBatteryAlarm(config_obj->current_scheme->BAT_WARN_LIMIT) < 0)
			ret = -1;
		pDebug(DBG_INFO, "Alarm set to %d%%",
		       config_obj->current_scheme->BAT_WARN_LIMIT);
		break;
	case BAT_WARN_STATE:
		if (setBatteryAlarm(config_obj->current_scheme->BAT_LOW_LIMIT) < 0)
			ret = -1;
		pDebug(DBG_INFO, "Alarm set to %d%%",
		       config_obj->current_scheme->BAT_LOW_LIMIT);
		break;
	case BAT_LOW_STATE:
		if (setBatteryAlarm(config_obj->current_scheme->BAT_CRIT_LIMIT) < 0)
			ret = -1;
		pDebug(DBG_INFO, "Alarm set to %d%%",
		       config_obj->current_scheme->BAT_CRIT_LIMIT);
		break;
	case BAT_CRIT_STATE:
		pDebug(DBG_INFO, "State is already critical, alarm not set");
		// normally not needed but checked anyway ...
		break;
	case BAT_NONE_STATE:
		// should never happen ...
		ret = -1;
		break;
	}

	if (ret < 0)
		supported = false;

	return ret;
}

void ACPI_Interface::activateSettings()
{
	PM_Interface::activateSettings();
	int x;
	/* override BIOS trip points in /proc/acpi/thermal_zone/../trip_points */
	if (_thermal_trip_points_supported && config_obj->ENABLE_THERMAL_MANAGEMENT == KERNEL) {
		for (x = 0;
		     x < MAX_THERMAL_ZONES && config_obj->current_scheme->thermal_zones[x].present;
		     x++) {

			setThermalTrippoints(x, config_obj->current_scheme->thermal_zones[x]);
		}
		/* trip points could not be set -> deactivating */
		if (x == 0) {
			_thermal_trip_points_supported = false;
			pDebug(DBG_DIAG, "No trip point support");
		}
	}
	if (_cooling_mode_supported) {
		for (x = 0; x < MAX_THERMAL_ZONES
			     && setCoolingMode(x, config_obj->current_scheme->COOLING_POLICY) > 0;
		     x++) ;
		if (x == 0) {
			_cooling_mode_supported = false;
			pDebug(DBG_DIAG, "BIOS has no cooling mode support.");
		}
	}
	/* throttle CPU if configured  */
	if (config_obj->current_scheme->ALWAYS_THROTTLE) {
		_throttleInterface.throttle(config_obj->current_scheme->MAX_CPU_THROTTLING);
	} else
		_throttleInterface.dethrottle();
}

int ACPI_Interface::suspend_to_ram()
{
	if (!((_supported_sleeping_states & ACPI_S3)
	      || (_supported_sleeping_states & ACPI_S3_BIOS))) {
		pDebug(DBG_WARN, "ACPI S3 state not available");
		return -1;
	}

	pDebug(DBG_INFO, "Set machine into suspend2ram (S3) mode");

	executeScript(ACPI_SLEEP_SCRIPT, "suspend2ram");

	return 1;
}

int ACPI_Interface::standby()
{
	if (!(_supported_sleeping_states & ACPI_S1)) {
		pDebug(DBG_WARN, "ACPI S1 state not available");
		return -1;
	}

	pDebug(DBG_INFO, "Set machine into standby (S1) mode");

	executeScript(ACPI_SLEEP_SCRIPT, "standby");

	return 1;
}

unsigned long ACPI_Interface::getTimestamp()
{
	timeval t;
	gettimeofday(&t, NULL);
	return (t.tv_sec * 1000 + t.tv_usec / 1000);
}

int ACPI_Interface::openAcpidSocket()
{
	int fd;
	int r;
	struct sockaddr_un addr;
	int fl;

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd < 0) {
		return fd;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	sprintf(addr.sun_path, "%s", ACPI_EVENTFILE.c_str());

	r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
	if (r < 0) {
		close(fd);
		return r;
	}
	fl = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, fl | O_NONBLOCK);
	return fd;
}