File: pm_interface.cpp

package info (click to toggle)
powersave 0.15.20-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,360 kB
  • ctags: 610
  • sloc: sh: 10,767; cpp: 5,264; ansic: 1,495; makefile: 342
file content (759 lines) | stat: -rw-r--r-- 22,323 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/***************************************************************************
 *                                                                         *
 *                         Powersave Daemon                                *
 *                                                                         *
 *          Copyright (C) 2004,2005,2006 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 "config.h"
#include <sys/wait.h>
#include <sys/time.h>
#include <liblazy.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#include "event_management.h"
#include "main_loop.h"
#include "brightness.h"
#include "cpufreq_management.h"
#include "globals.h"
#include "pm_interface.h"
#include "cpu.h"

using namespace Powersave::Globals;

int PM_Interface::rereadConfig()
{
	int ret = config_obj->readConfFiles();

	/* reset/reread everything that could have been changed in
	   configs ... */
	if (cpufreq->isSupported()) {
		cpufreq->setModes(config_obj->current_scheme->CPUFREQUENCY, _eM);
	}
	activateSettings();
	return ret;
}


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

	// initialise everything we have in the constructor !
	// acpi/apm specifc things are initialised in the
	// appropriate ACPI/APM_Interface constructor

	// **** I N I T   S E C T I O N *************************/

	// this should never happen, hopefully the term signal cleans up memory and stops app */
	if (!config_obj || !config_obj->current_scheme || !config_obj->AC_scheme || !config_obj->Battery_scheme) {
		pDebug(DBG_ERR, "Current, AC or Battery scheme is not set. Exit.");
		kill(getpid(), SIGTERM);
	}

	_cooling_mode_supported = true;
	_thermal_trip_points_supported = true;
	_can_suspend_to_disk = true;
	_can_suspend_to_ram = true;

	// allocate memory for event management object
	_eM = new EventManagement();

	_brightness = new Brightness();

	int can;
	liblazy_hal_get_property_bool(DBUS_HAL_UDI_COMPUTER,
				      "power_management.can_suspend_to_disk", &can);
	if (can != 1)
		_can_suspend_to_disk = false;
	liblazy_hal_get_property_bool(DBUS_HAL_UDI_COMPUTER,
				      "power_management.can_suspend_to_ram", &can);
	if (can != 1)
		_can_suspend_to_ram = false;
}

PM_Interface::~PM_Interface()
{

	pDebug(DBG_DEBUG, "PM Destructor: delete allocated Objects");

	_eM->executeEvent("daemon.terminate");
	delete(_eM);
	delete(_brightness);

	// enable all CPUs
	CPU::enableAll();
}

int PM_Interface::start()
{
	// ********  trigger start event ****************/
	_eM->executeEvent("daemon.start");

	if (_battery.updateBatteryState(_cur_state.AC_STATE, _eM) < 0) {
		pDebug(DBG_INFO, "No battery available.");
	} else {
		pDebug(DBG_INFO, "There is at least one battery available.");
		// reset battery state, so the first checkBatteryChanges throws a battery
		// event if necessary
		_battery.resetState();
	}

	// Warning: when we start, the scheme should change from "nothing" to "active
	// scheme", a daemon.scheme.change event should be thrown and activateSettings
	// should be called. In reality, it changes from "Performance" to "active scheme",
	// which on AC is often "Performance" => no change => no event => no activate.
	// Not fatal, but we should fix it. In the meantime, we work around it here:
	activateSettings();
	_eM->executeEvent("daemon.scheme.change");
	
	/* this also sets current scheme, do not change order */
	updateACState();
	activateScheme(_cur_state.AC_STATE);
	
	// if the battery is already low, throw an appropriate event.
	checkBatteryStateChanges();

	return 1;
}

void PM_Interface::checkBatteryStateChanges()
{

	pDebug(DBG_DEBUG, "Check battery state");
	// only switch if a lower battery level is reached
	if (_battery.updateBatteryState(_cur_state.AC_STATE, _eM) == 1) {
		switch (_battery.batteryState()) {
		case BAT_NORM_STATE:
			_eM->executeEvent("battery.normal");
			pDebug(DBG_INFO, "Battery state changed to normal");
			break;
		case BAT_WARN_STATE:
			_eM->executeEvent("battery.warning");
			pDebug(DBG_INFO, "Battery state changed to warning");
			break;
		case BAT_LOW_STATE:
			_eM->executeEvent("battery.low");
			pDebug(DBG_INFO, "Battery state changed to low");
			break;
		case BAT_CRIT_STATE:
			_eM->executeEvent("battery.critical");
			pDebug(DBG_INFO, "Battery state changed to critical");
			break;
		case BAT_NONE_STATE:
			pDebug(DBG_INFO, "Battery disappeared.");
			break;
		default:
			pDebug(DBG_ERR, "Undefined battery state ..., that's bad");
		}
	} else {
		pDebug(DBG_INFO, "Battery state unchanged");
	}
}

int PM_Interface::checkACStateChanges()
{
	AC_ADAPTER_STATE ac_state;
	if (updateACState()) {
		pDebug(DBG_DIAG, "AC state changed");
		ac_state = _cur_state.AC_STATE;

		if (ac_state == AC_OFFLINE) {
			// set battery state
			_eM->executeEvent("acadapter.offline");
			pDebug(DBG_INFO, "ac adapter offline event occured");
			return 1;
		} else if (ac_state == AC_ONLINE) {
			_eM->executeEvent("acadapter.online");
			pDebug(DBG_INFO, "ac adapter online event occured");
			return 1;
		} else if (ac_state == AC_UNKNOWN) {
			// assume online for now...
			pDebug(DBG_DIAG, "AC state changed to unknown, assume online");
			ac_state = AC_ONLINE;
			return 1;
		} else {
			pDebug(DBG_ERR, "Could not read out AC adapter state, FATAL ERROR");
			return -1;
		}
	} else
		return 0;
}

void PM_Interface::checkTemperatureStateChanges()
{
	int ret = 0;
	pDebug(DBG_DEBUG, "Check temperature State");
	ret = updateTemperatureState();
	/* it is getting hotter */
	if (ret == 1) {
		switch (_cur_state.TEMP_STATE) {
		case CRITICAL:
			_eM->executeEvent("temperature.critical");
			pDebug(DBG_WARN, "Temperature state changed to critical.");
			break;
		case HOT:
			_eM->executeEvent("temperature.hot");
			pDebug(DBG_WARN, "Temperature state changed to hot.");
			break;
		case PASSIVE:
			_eM->executeEvent("temperature.passive");
			pDebug(DBG_INFO, "Temperature state changed to passive.");
			break;
			/* I do not differ between ACTIVE (fans on) and
			   OK mode (no cooling) at the moment */
		case ACTIVE:
			_eM->executeEvent("temperature.active");
			pDebug(DBG_INFO, "Temperature state changed to active.");
			break;
		case OK:
			pDebug(DBG_DIAG, "Temperature state changed to ok - this should"
			       "not happen at this point ->bug ->mail@renninger.de.");
			break;
		default:
			pDebug(DBG_DIAG, "Undefined temperature state ->bug ->mail@renninger.de.");
			break;
		}
	}
	/* it is getting cooler */
	else if (ret == 2) {
		switch (_cur_state.TEMP_STATE) {
		case CRITICAL:
			_eM->executeEvent("temperature.critical");
			pDebug(DBG_INFO, "Temperature state changed to critical. This should"
			       "not happen at this point ->bug ->mail@renninger.de.");
			break;
		case HOT:
			_eM->executeEvent("temperature.hot");
			pDebug(DBG_INFO, "Temperature state changed to hot.");
			break;
		case PASSIVE:
			_eM->executeEvent("temperature.passive");
			pDebug(DBG_INFO, "Temperature state changed to passive.");
			break;
			/* I do not differ between ACTIVE (fans on) and
			   OK mode (no cooling) at the moment */
		case ACTIVE:
			_eM->executeEvent("temperature.active");
			pDebug(DBG_INFO, "Temperature state changed to active.");
			break;
		case OK:
			_eM->executeEvent("temperature.ok");
			pDebug(DBG_INFO, "Temperature state changed to ok.");
			break;
		default:
			pDebug(DBG_INFO, "Undefined temperature state ->bug ->mail@renninger.de.");
			break;
		}
	}
}


bool PM_Interface::checkThrottling()
{
	return _throttleInterface.updateCPUState(_eM);
}

int PM_Interface::updateACState()
{
	int changed;
	int old_ac_state;

	old_ac_state = _cur_state.AC_STATE;
	_cur_state.AC_STATE = (AC_ADAPTER_STATE) getACAdapterStatus();

	/* there are new error codes returned 
	   (e.g. NO_DEVICE_ERROR and NO_MODULE_ERROR) */
	if (_cur_state.AC_STATE != AC_ONLINE && _cur_state.AC_STATE != AC_OFFLINE) {
		pDebug(DBG_INFO, "Could not detect AC status, assume AC_ONLINE");
		_cur_state.AC_STATE = AC_ONLINE;
	}
	if (old_ac_state != _cur_state.AC_STATE) {
		changed = 1;
		if (activateScheme(_cur_state.AC_STATE) > 0) {
			_eM->executeEvent("daemon.scheme.change");
		}
	} else
		changed = 0;
	return changed;
}

int PM_Interface::updateTemperatureState()
{

	int x;
	int state;
	/* is also THERM_MODE but C (library) forbids the declaration */
	int temp_state = OK;

	for (x = 0; x < MAX_THERMAL_ZONES &&
		     (config_obj->current_scheme->thermal_zones[x].present) &&
		     ((state = getThermalZoneState(x)) >= OK); x++) {
		config_obj->current_scheme->thermal_zones[x].state = state;
		temp_state = max(state, temp_state);
	}
	/* here we need more proper error handling -> destroy allocated objects
	   probably the thermal module has been unloaded ... */
	if (config_obj->current_scheme->thermal_zones[x].present) {
		pDebug(DBG_DIAG, "Error during temperature evaluation");
		return -1;
	}
	if (temp_state > _cur_state.TEMP_STATE) {
		_cur_state.TEMP_STATE = (THERM_MODE) temp_state;
		return 1;
	} else if (temp_state < _cur_state.TEMP_STATE) {
		_cur_state.TEMP_STATE = (THERM_MODE) temp_state;
		return 2;
	} else
		return 0;
}

int PM_Interface::activateScheme(const string &scheme_name)
{

	int ret;
	if (!config_obj->current_scheme->SCHEME_NAME.compare(scheme_name)) {
		pDebug(DBG_DIAG, "Scheme already set");
		return 0;
	}
	ret = config_obj->setActiveScheme(scheme_name);
	if (ret <= 0)
		return ret;

	pDebug(DBG_DIAG, "switched scheme to: %s", scheme_name.c_str());

	if (scheme_name == "AdvancedPowersave") {
		pDebug(DBG_WARN, "AdvancedPowersave scheme activated. This scheme "
		       "is highly experimental. Please be careful!");
	}

	activateSettings();
	return 1;
}

int PM_Interface::activateScheme(AC_ADAPTER_STATE ac_state)
{

	int ret = 0;
	switch (ac_state) {
	case AC_ONLINE:
		ret = activateScheme(config_obj->getACScheme());
		break;
	case AC_OFFLINE:
		ret = activateScheme(config_obj->getBatteryScheme());
		break;
	case AC_UNKNOWN:
		// which one should we use -> I'd say better use AC
		// we could even test for battery level, or is this
		// too overdosed?
		ret = activateScheme(config_obj->getACScheme());
		break;
	}
	return ret;
}

void PM_Interface::activateSettings()
{
	// Switch kernel/userspace control during runtime
	if (config_obj->current_scheme->CPUFREQ_CONTROL != cpufreq->controlMode()) {

		pDebug(DBG_DIAG, "Change cpufreq control to: %s",
		       ((config_obj->current_scheme->CPUFREQ_CONTROL == CPUFREQ_USERSPACE)
			? "userspace" : "kernel"));

		delete cpufreq;
		cpufreq = NULL;
		cpufreq = new CpufreqManagement();
	}

	/* set new CPU freq mode */
	if (!cpufreq->setModes(config_obj->current_scheme->CPUFREQUENCY, _eM) < 0) {
		pDebug(DBG_WARN, "Could not set CPUFreq mode");
	}

	// disable brightness in powersaved until we have a interface clients can grab
	if (!DBus_Server::haveClient()) {
		switch (config_obj->current_scheme->DISPLAY_BRIGHTNESS) {
		case -1:		// ignore
			break;
		case -2:		// low
			_brightness->Min();
			break;
		case -3:		// medium
			_brightness->Med();
			break;
		case -4:		// high
			_brightness->Max();
		break;
		default:		// level
			_brightness->Set(config_obj->current_scheme->DISPLAY_BRIGHTNESS);
		break;
		}
	}
	main_loop->updateCallbacks();
}

void PM_Interface::throttle(int percent)
{
	if (_cur_state.TEMP_STATE < PASSIVE) {
		_throttleInterface.throttle(percent);
	}
}

void PM_Interface::dethrottle()
{
	if (_cur_state.TEMP_STATE < PASSIVE) {
		_throttleInterface.dethrottle();
	}
}

/* mainly copied from liblazy_dbus_send_method_call implementation. Just
 * added var_args list and INT_MAX as timeout */
int PM_Interface::sendSleepRequest(const char *method, int first_arg_type, ...)
{
	DBusError	dbus_error;
	DBusConnection	*dbus_connection	= NULL;
	DBusMessage	*message		= NULL;
	DBusMessage	*reply			= NULL;
	int		ret			= 0;
	va_list		var_args;

	dbus_error_init(&dbus_error);

	dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
	if (dbus_connection == NULL || dbus_error_is_set(&dbus_error)) {
		fprintf(stderr, "Connection to dbus not ready, skipping method call %s: %s\n",
			method, dbus_error.message);
		ret = -1;
		goto Free_Error;
	}

	message = dbus_message_new_method_call(DBUS_HAL_INTERFACE, DBUS_HAL_UDI_COMPUTER,
					       DBUS_HAL_SYSTEM_POWER_INTERFACE, method);
	va_start(var_args, first_arg_type);
	dbus_message_append_args_valist(message, first_arg_type, var_args);
	va_end(var_args);

	reply = dbus_connection_send_with_reply_and_block(dbus_connection,
							  message, 1,
							  &dbus_error);
	if (dbus_error_is_set(&dbus_error)) {
		/* ignore timeout errors because it's possible that we are suspending > 6 hours */
		if (!dbus_error_has_name(&dbus_error, "org.freedesktop.DBus.Error.NoReply")) {
			fprintf(stderr, "Received error reply: %s\n", dbus_error.message);
			ret = -1;
		}
	}
	
	dbus_message_unref(message);
	dbus_connection_unref(dbus_connection);
Free_Error:
	dbus_error_free(&dbus_error);
	return ret;
}

bool PM_Interface::suspendToDisk()
{
	if (!_can_suspend_to_disk) {
		pDebug(DBG_WARN, "ACPI S4 state not available");
		return false;
	}

	if (liblazy_hal_is_caller_privileged("org.freedesktop.hal.power-management.hibernate") != 1)
		return false;
	else if (sendSleepRequest("Hibernate", DBUS_TYPE_INVALID)) {
				 pDebug(DBG_ERR, "Could not send suspend to disk request to HAL\n");
				 return false;
	} else
		pDebug(DBG_DEBUG, "Suspend to disk request sent.\n");
	
	pDebug(DBG_INFO, "Set machine into suspend to disk (S4) mode");

	return true;
}

bool PM_Interface::suspendToRam()
{
	if (!_can_suspend_to_ram) {
		pDebug(DBG_WARN, "ACPI S3 state not available");
		return false;
	}

	int wake_up = 0;

	if (liblazy_hal_is_caller_privileged("org.freedesktop.hal.power-management.suspend") != 1)
		return false;
	else if (sendSleepRequest("Suspend",
				    DBUS_TYPE_INT32,
				    &wake_up,
				    DBUS_TYPE_INVALID)) {
		pDebug(DBG_ERR, "Could not send suspend to ram request to HAL\n");
		return false;
	} else
		pDebug(DBG_DEBUG, "Suspend to ram request sent.\n");

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

	return true;
}

void PM_Interface::resume()
{
	pDebug(DBG_DIAG, "Daemon reinitialises system states, resume finished");

	checkACStateChanges();
	checkBatteryStateChanges();
}

int PM_Interface::executeScript(const string &script, const string &param)
{
	int ret = -1;
	int pid;
	char *argv[3];
	argv[0] = new char[script.size() + 1];
	argv[1] = new char[param.size() + 1];
	strcpy(argv[0], script.c_str());
	strcpy(argv[1], param.c_str());
	argv[2] = NULL;

	string full_path_to_script(SCRIPT_DIR);
	full_path_to_script.append("/" + script);

	pid = fork();
	if (!pid) {
		signal(SIGHUP, SIG_DFL);
		signal(SIGTERM, SIG_DFL);
		signal(SIGINT, SIG_DFL);
		signal(SIGQUIT, SIG_DFL);
		signal(SIGPIPE, SIG_DFL);
		signal(SIGCHLD, SIG_DFL);
		// close all fds
		int fdlimit = sysconf(_SC_OPEN_MAX);

		for (int fd = 0; fd < fdlimit; fd++)
			close(fd);

		// set environment variables for scripts
		setenv("KDE_BINDIR", KDE_BINDIR, 1);
		setenv("GNOME_BINDIR", GNOME_BINDIR, 1);
		setenv("SCRIPT_DIR", config_obj->script_dir.c_str(), 1);
		setenv("PUB_SCRIPT_DIR", config_obj->pub_script_dir.c_str(), 1);
		setenv("SYSCONF_DIR", config_obj->config_dir.c_str(), 1);
		setenv("BIN_DIR", BIN_DIR, 1);

		execv(full_path_to_script.c_str(), argv);

		// we should never get here.
		pDebug(DBG_ERR, "could not execv the sleep script '%s'. Argument: '%s', error: '%s'",
		       full_path_to_script.c_str(), argv[1], strerror(errno));
		_exit(127);
	} else if (pid == -1) {
		pDebug(DBG_WARN, "Could not fork for sleep script");
	} else {
		// no need to wait for script to return
	}
	return ret;
}

int PM_Interface::screenlock(const string &param)
{

	DBus_Server::emitSignal("Screenlock", param.c_str());

	if (!DBus_Server::haveClient()) {
		executeScript("do_screen_saver", param);
	}
	return 1;
}

int PM_Interface::x_notification(const string &param)
{
	DBus_Server::emitSignal("Notification", param.c_str());

	if (!DBus_Server::haveClient()) {
		executeScript("do_x_notification", param);
	}
	return 1;
}

DBUS_ERROR PM_Interface::handleScriptsMessage(DBusMessage *msg, const string &msg_member)
{
 	char dummy[] = "";

	pDebug (DBG_DIAG, "Handling Scripts request");
	if (msg_member == "ScriptReturn") {
		int event_id, request;

		// get the event id
		if (liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_INT32, &event_id, 0) < 0) {
			pDebug(DBG_WARN, "Script returned but we could not get the event id");
			return REPLY_INVALID_PARAM;
		}
		// get the so called mode ( notify, progress, true, false )
		if (!liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_INT32, &request, 1) < 0) {
			pDebug(DBG_WARN, "Script returned but we could not to get the request");
			return REPLY_INVALID_PARAM;
		}
		// get the message
		if (!liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_STRING, &dummy, 0) < 0) {
			pDebug(DBG_INFO, "Script returned but we could not to get the message."
			       " Continuing anyway...");
			strcpy(dummy, "");
		}

		pDebug(DBG_DEBUG, "Script returned: event id: %d, request: %d, message: '%s'",
		       event_id, request, dummy);

		string str_dummy = dummy;
		_eM->checkScriptReturn(event_id, request, str_dummy);

		return REPLY_SUCCESS;
	} else
		return REPLY_INVALID_METHOD;

	return REPLY_GENERAL_ERROR;
}

DBUS_ERROR PM_Interface::handleDBusMessage(DBusMessage *msg, DBusMessage *reply,
					   const string &msg_member)
{
	char *dummy;

	/** @todo Also reply REPLY_HW_NOT_SUPPORTED for suspend*/
	if (msg_member == "SchemesGet") {
		int active = -1;
		int battery = -1;
		int ac_power = -1;
		SchemeConfig *scheme;
		const char *scheme_name;

		for (int x = 0; x < MAX_SCHEMES; x++) {
			scheme = config_obj->getSchemeByNumber(x);
			if (scheme == NULL)
				break;
			/* only set \n if there will be another string to append */
			scheme_name = scheme->SCHEME_NAME.c_str();
			dbus_message_append_args(reply, DBUS_TYPE_STRING, &scheme_name, DBUS_TYPE_INVALID);
			if (scheme == config_obj->current_scheme)
				active = x;
			if (scheme == config_obj->Battery_scheme)
				battery = x;
			if (scheme == config_obj->AC_scheme)
				ac_power = x;
		}
		/*
		   pDebug (DBG_DIAG, "Active: %d, battery: %d, ac_power: %d",
		   battery, active, ac_power);
		 */

		/* order of appended INT32 for request "schemes":
		 * 0: no. of currently active   scheme
		 * 1: no. of           battery  scheme
		 * 2: no. of           ac_power scheme
		 * Use the values of these appended INT32 to determine which
		 * appended strings are active/battery/ac_power scheme
		 */
		if (active == -1 || battery == -1 || ac_power == -1) {
			pDebug(DBG_ERR, "Could not find active/battery/ac_power scheme %d/%d/%d",
			       active, battery, ac_power);
			return REPLY_GENERAL_ERROR;
		} else {
			dbus_message_append_args(reply,
						 DBUS_TYPE_INT32, &active,
						 DBUS_TYPE_INT32, &battery,
						 DBUS_TYPE_INT32, &ac_power, DBUS_TYPE_INVALID);
		}

		return REPLY_SUCCESS;
	} else if (msg_member == "SchemesGetDescription") {
		SchemeConfig *scheme;
		const char *description;
		if (!liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_STRING, &dummy, 0)) {
			scheme = config_obj->getScheme(dummy);

			if (scheme == NULL) {
				return REPLY_INVALID_PARAM;
			}

			description = scheme->SCHEME_DESCRIPTION.c_str();

			dbus_message_append_args(reply, DBUS_TYPE_STRING, &description, DBUS_TYPE_INVALID);

			return REPLY_SUCCESS;
		}
		else {
			return REPLY_INVALID_PARAM;
		}
	} else if (msg_member == "CpufreqCPUEnable") {
		int value;
		if (!liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_INT32, &value, 0)) {
			int ret = cpufreq->enableCPU(value); 
			if (ret < 0)
				return REPLY_HW_NOT_SUPPORTED;
			else if (ret == 0)
				return REPLY_ALREADY_SET;
			else if (ret > 0)
				return REPLY_SUCCESS;
		} else {
			return REPLY_GENERAL_ERROR;
		}
	} else if (msg_member == "CpufreqCPUDisable") {
		int value;
		if (!liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_INT32, &value, 0)) {
			int ret = cpufreq->disableCPU(value); 
			if (ret < 0)
				return REPLY_HW_NOT_SUPPORTED;
			else if (ret == 0)
				return REPLY_ALREADY_SET;
			else if (ret > 0)
				return REPLY_SUCCESS;
		} else {
			return REPLY_GENERAL_ERROR;
		}
	} else if (msg_member == "Ping") {
		return REPLY_SUCCESS;
	} else if (msg_member == "SchemesSet") {
		if (!liblazy_dbus_message_get_basic_arg(msg, DBUS_TYPE_STRING, &dummy, 0)) {
			int r = activateScheme(dummy);
			if (r == 0) {
				return REPLY_ALREADY_SET;
			} else if (r < 0) {
				pDebug(DBG_DIAG, "Scheme '%s' does not exist", dummy);
				return REPLY_INVALID_PARAM;
			} else {
				pDebug(DBG_DIAG, "Scheme '%s' received and activated", dummy);

				_eM->executeEvent("daemon.scheme.change");
				return REPLY_SUCCESS;
			}
		} else
			return REPLY_INVALID_PARAM;
	} else {
		return REPLY_INVALID_METHOD;
	}

	return REPLY_GENERAL_ERROR;
}

BATTERY_STATES PM_Interface::batteryState() {
	return _battery.batteryState();
}