File: device.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 (415 lines) | stat: -rw-r--r-- 9,843 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
/***************************************************************************
 *                                                                         *
 *                         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 "powersave_hal.h"
#include "device.h"
#include "pm_interface.h"

char *DPM_CLASS_NAMES[DPM_CLASS_MAX] = {
	"usb",
	"lan",
	"wlan",
	"sound"
};

using namespace std;

/*** class Device ***/

Device::Device(const string &udi)
{
	_udi = udi;

	char *device_path = ps_hal_get_property_string((char*)_udi.c_str(),
						       "linux.sysfs_path");
	if (device_path == NULL || strlen(device_path) == 0) {
		pDebug(DBG_WARN, "Could not get linux.sysfs_path of device '%s'",
		       _udi.c_str());
		return;
	}

	_device_path.append(device_path);
	libhal_free_string(device_path);
}

Device::~Device()
{
}

bool Device::suspend(DPM_POWER_STATE state, bool force)
{
	if (updateState() == state) {
		pDebug(DBG_INFO, "Power state %d already set in '%s'",
		       state, _state_file.c_str());
		return false;
	}

	if ( !force && isActive() ) {
		pDebug(DBG_INFO, "Tried to suspend device '%s', but it is in use",
		       _udi.c_str());
		return false;
	}
	
	errno = 0;
	FILE *file = fopen(_state_file.c_str(), "w");

	if (file == NULL) {
		pDebug(DBG_DIAG, "Could not open power state file '%s': %s",
		       _state_file.c_str(), strerror(errno));
		return false;
	}

	errno = 0;
	if (fprintf(file, "%d", state) < 0) {
		pDebug(DBG_ERR, "Could not write to state file '%s'",
		       _state_file.c_str());
		fclose(file);

		return false;
	}

	pDebug(DBG_INFO, "wrote %d to power state file '%s'\n",
	       state, _state_file.c_str());

	// set current state the device is in
	_current_state = state;

	errno = 0;
	if (fclose(file) != 0) {
		pDebug(DBG_WARN, "Unable to close power state file '%s': %s",
		       _state_file.c_str(), strerror(errno));
		return false;
	}
	
	return true;
}

bool Device::resume()
{
	return suspend(DPM_STATE_D0, true);
}

DPM_POWER_STATE Device::updateState()
{
	int state = -1;

	errno = 0;
	FILE *file = fopen(_state_file.c_str(), "r");

	if (file == NULL) {
		pDebug(DBG_DIAG, "Could not open power state file '%s': %s",
		       _state_file.c_str(), strerror(errno));
		return DPM_STATE_UNKNOWN;
	}

	errno = 0;
	if (fscanf(file, "%d", &state) < 0) {
		pDebug(DBG_ERR, "Could not read from state file '%s'",
		       _state_file.c_str());
		fclose(file);

		return DPM_STATE_UNKNOWN;
	}

	errno = 0;
	if (fclose(file) != 0) {
		pDebug(DBG_WARN, "Unable to close power state file '%s': %s",
		       _state_file.c_str(), strerror(errno));
		return DPM_STATE_D0;
	}

	if (state < 0) {
		pDebug(DBG_WARN, "Read value from state file '%s' is no power state",
		       _state_file.c_str());
		return DPM_STATE_UNKNOWN;
	}

	// update current state
	_current_state = (DPM_POWER_STATE)state;
	
	return _current_state;
}

DPM_DEVICE_CLASS Device::deviceClass()
{
	return _device_class;
}

string Device::udi()
{
	return _udi;
}

/*** class UsbDevice ***/

UsbDevice::UsbDevice(const string &udi) : Device(udi)
{
	_device_class = DPM_CLASS_USB;
	_state_file = _device_path + "/power/state";	
	pDebug(DBG_INFO, "Set power state file of usb devices to %s",
	       _state_file.c_str());
	updateState();
}

UsbDevice::~UsbDevice()
{
}

bool UsbDevice::isActive()
{
	return false;
}

void UsbDevice::getDevices(std::list<std::string> &device_udis)
{
	if (!ps_hal_init()) {
		return;
	}

	char **usb_devs;
	int num_devs;
	usb_devs = libhal_manager_find_device_string_match(ps_hal_context(),
							   "info.bus",
							   "usb",
							   &num_devs,
							   ps_hal_dbus_error());

	pDebug(DBG_INFO, "Found %d usb devices", num_devs);

	if (num_devs == 0) {
		libhal_free_string_array(usb_devs);
		return;
	}

	for (int i = 0; i < num_devs; i++) {
		device_udis.push_back(usb_devs[i]);
		pDebug(DBG_INFO, "Added usb device '%s'", usb_devs[i]);
	}

	libhal_free_string_array(usb_devs);
}

bool UsbDevice::isDevice(const char *udi) {
	bool ret = true;
	char *bus = ps_hal_get_property_string((char*)udi, "info.bus");

	if ( bus == NULL || strcmp(bus, DPM_CLASS_NAMES[DPM_CLASS_USB]) != 0) {
		ret = false;
	}

	libhal_free_string(bus);
	return ret;
}

/*** class Wlan Device ***/

WlanDevice::WlanDevice(const string &udi) : Device(udi)
{
	_device_class = DPM_CLASS_WLAN;
	_state_file = _device_path + "/device/power/state";
	pDebug(DBG_INFO, "Set power state file of wlan devices to %s",
	       _state_file.c_str());
	updateState();
}

WlanDevice::~WlanDevice()
{
}

bool WlanDevice::isActive()
{
	// this does not work yet, because hal does not update the
	// interface status. We will ask the Networkmanager for that in
	// the future
	int active = ps_hal_get_property_bool((char*)_udi.c_str(), "net.interface_up");
	pDebug(DBG_DIAG, "Check if wlan device '%s' is active: %s",
	       _udi.c_str(), active ? "yes" : "no");

	return false;
}

void WlanDevice::getDevices(std::list<std::string> &device_udis)
{
	if (!ps_hal_init()) {
		return;
	}

	char **wlan_devs;
	int num_devs;
	wlan_devs = libhal_find_device_by_capability(ps_hal_context(),
						     "net.80211",
						     &num_devs,
						     ps_hal_dbus_error());

	pDebug(DBG_INFO, "Found %d wlan devices", num_devs);

	if (num_devs == 0) {
		libhal_free_string_array(wlan_devs);
		return;
	}

	for (int i = 0; i < num_devs; i++) {
		device_udis.push_back(wlan_devs[i]);
		pDebug(DBG_INFO, "Added wlan device '%s'", wlan_devs[i]);
	}

	libhal_free_string_array(wlan_devs);
}

bool WlanDevice::isDevice(char *udi) {
	if (!ps_hal_query_capability(udi, "net.80211")) {
		return false;
	}
	else {
		return true;
	}
}

/*** class SoundDevice ***/

SoundDevice::SoundDevice(const string &udi) : Device(udi)
{
	_device_class = DPM_CLASS_SOUND;
	_state_file = _device_path + "/power/state";
	pDebug(DBG_INFO, "Set power state file of wlan devices to %s",
	       _state_file.c_str());
	updateState();
}

SoundDevice::~SoundDevice()
{
}

bool SoundDevice::isActive()
{
	return false;
}

void SoundDevice::getDevices(std::list<std::string> &device_udis)
{
	if (!ps_hal_init()) {
		return;
	}

	char **sound_devs;
	int num_devs;
	sound_devs = libhal_manager_find_device_string_match(ps_hal_context(),
							     "info.category",
							     "alsa",
							     &num_devs,
							     ps_hal_dbus_error());

	pDebug(DBG_INFO, "Found %d sound devices", num_devs);

	if (num_devs == 0) {
		libhal_free_string_array(sound_devs);
		return;
	}

	/* there are multiple sound devices, but we only want the parent */
	for (int i = 0; i < num_devs; i++) {
		char *physical_device = ps_hal_get_property_string(sound_devs[i],
								   "alsa.physical_device");

		if (physical_device != NULL && strlen(physical_device) > 0) {
			device_udis.push_back(physical_device);
			pDebug(DBG_INFO, "Added sound device '%s'", sound_devs[i]);
			libhal_free_string(physical_device);
			libhal_free_string_array(sound_devs);
			break;
		}
	}
}

bool SoundDevice::isDevice(const char *udi) {
	bool ret = true;
	char *category = ps_hal_get_property_string((char*)udi, "info.category");

	if (category != "alsa") {
		ret = false;
	}

	libhal_free_string(category);
	return ret;
}

/*** class LanDevice ***/

LanDevice::LanDevice(const string &udi) : Device(udi)
{
	_device_class = DPM_CLASS_LAN;
	_state_file = _device_path + "/device/power/state";
	pDebug(DBG_INFO, "Set power state file of wlan devices to %s",
	       _state_file.c_str());
	updateState();
}

LanDevice::~LanDevice()
{
}

bool LanDevice::isActive()
{
	// we will ask the Networkmanager for that in the future
	return false;
}

void LanDevice::getDevices(std::list<std::string> &device_udis)
{
	if (!ps_hal_init()) {
		return;
	}

	char **lan_devs;
	int num_devs;

	lan_devs = libhal_find_device_by_capability(ps_hal_context(),
						    "net.80203",
						    &num_devs,
						    ps_hal_dbus_error());

	pDebug(DBG_INFO, "Found %d sound devices", num_devs);

	if (num_devs == 0) {
		libhal_free_string_array(lan_devs);
		return;
	}

	for (int i = 0; i < num_devs; i++) {
		device_udis.push_back(lan_devs[i]);
		pDebug(DBG_INFO, "Added lan device '%s'", lan_devs[i]);
	}

	libhal_free_string_array(lan_devs);
}

bool LanDevice::isDevice(char *udi)
{
	if (!ps_hal_query_capability(udi, "net.80203")) {
		return false;
	}
	else {
		return true;
	}
}