File: device_management.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 (280 lines) | stat: -rw-r--r-- 7,020 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
/***************************************************************************
 *                                                                         *
 *                         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 "config.h"
#include "device_management.h"
#include "pm_interface.h"
#include "globals.h"

using namespace std;

DeviceManagement::DeviceManagement()
{
}

void DeviceManagement::init()
{
	list<string> device_udis;

	/*** wlan devices ***/
	WlanDevice::getDevices(device_udis);

	for (list< string >::iterator it = device_udis.begin();
	     it != device_udis.end(); ++it) {
		addWlanDevice(*it);
	}
	device_udis.clear();

	/*** usb devices ***/
	UsbDevice::getDevices(device_udis);

	for (list< string >::iterator it = device_udis.begin();
	     it != device_udis.end(); ++it) {
		addUsbDevice(*it);
	}
	device_udis.clear();

	/*** sound devices ***/
	SoundDevice::getDevices(device_udis);
	
	for (list< string >::iterator it = device_udis.begin();
	     it != device_udis.end(); ++it) {
		addSoundDevice(*it);
	}
	device_udis.clear();

	
	/*** lan devices ***/
	LanDevice::getDevices(device_udis);
	
	for (list< string >::iterator it = device_udis.begin();
	     it != device_udis.end(); ++it) {
		addLanDevice(*it);
	}
}

DeviceManagement::~DeviceManagement()
{
	for (list< Device* >::iterator it = _devices.begin();
	     it != _devices.end(); ++it) {
		
		(*it)->resume();
		delete (*it);
	}
}

bool DeviceManagement::suspend(DPM_DEVICE_CLASS device_class, int device_num, bool force)
{
	if (!Powersave::Globals::config_obj->current_scheme->DPM_ENABLED) {
		return false;
	}

	int num = 0;

	for (list< Device* >::iterator it = _devices.begin();
	     it != _devices.end(); ++it) {

		// continue if this is the wrong device class
		if ((*it)->deviceClass() != device_class) {
			continue;
		}

		//suspend all devices off this class
		if (device_num < 0) {
			(*it)->suspend(DPM_STATE_D3, force);
		}
		// only suspend one device
		else if (device_num == num) {
			(*it)->suspend(DPM_STATE_D3, force);
			break;
		}

		num++;
	}

	if (num == 0) {
		return false;
	}

	return true;
}

bool DeviceManagement::update(std::list< DPM_DEVICE_CLASS > rpm_classes, bool force)
{
	if (!Powersave::Globals::config_obj->current_scheme->DPM_ENABLED) {
		return false;
	}

	for (list< Device* >::iterator it = _devices.begin();
	     it != _devices.end(); ++it) {
		
		list< DPM_DEVICE_CLASS >::iterator i = find(rpm_classes.begin(),
							    rpm_classes.end(),
							    (*it)->deviceClass());

		DPM_POWER_STATE state = (*it)->updateState();
		if (i != rpm_classes.end()) {
			if (state == DPM_STATE_D0) {
				(*it)->suspend();
			}
			else if ( state == DPM_STATE_UNKNOWN ) {
				_devices.erase(it);
				it = _devices.begin();
			}
		}		
		else {
			if (state > DPM_STATE_D0) {
				(*it)->resume();
			}
			else if (state == DPM_STATE_UNKNOWN) {
				_devices.erase(it);
				it = _devices.begin();
			}
		}
	}

	return true;
}
bool DeviceManagement::resume(DPM_DEVICE_CLASS device_class, int device_num)
{
	if (!Powersave::Globals::config_obj->current_scheme->DPM_ENABLED) {
		return false;
	}

	int num = 0;

	for (list< Device* >::iterator it = _devices.begin();
	     it != _devices.end(); ++it) {

		if ((*it)->deviceClass() != device_class) {
			continue;
		}

		/* resume all devices off this class */
		if (device_num < 0) {
			(*it)->resume();
		}
		/* only resume one device */
		else if (device_num == num) {
			(*it)->resume();
			break;
		}
		
		num++;
	}

	if (num == 0) {
		return false;
	}

	return true;
}

int DeviceManagement::getDevices(DPM_DEVICE_CLASS device_class,
				 list< string > &udis,
				 list< DPM_POWER_STATE > &power_states)
{
	for (list< Device* >::iterator it = _devices.begin();
	     it != _devices.end(); ++it) {

		if ((*it)->deviceClass() == device_class) {
			DPM_POWER_STATE state = (*it)->updateState();
			if (state == DPM_STATE_UNKNOWN) {
				_devices.erase(it);
				it = _devices.begin();
			}
			else {
				udis.push_back((*it)->udi());
				power_states.push_back((*it)->updateState());
			}
		}
	}

	return udis.size();
}

bool DeviceManagement::checkDeviceAdded(char *udi)
{
	if (UsbDevice::isDevice(udi)) {
		addUsbDevice(udi);
	}
	else if (WlanDevice::isDevice(udi)) {
		addWlanDevice(udi);
	}
	else if (LanDevice::isDevice(udi)) {
		addLanDevice(udi);
	}
	else if (SoundDevice::isDevice(udi)) {
		addSoundDevice(udi);
	}
	else {
		return false;
	}

	return true;
}

bool DeviceManagement::checkDeviceRemoved(char *udi)
{
	for (list< Device* >::iterator it = _devices.begin();
	     it != _devices.end(); ++it) {
		
		if ((*it)->udi() == udi) {
			_devices.erase(it);
			pDebug(DBG_INFO, "Removed device '%s'", udi);
			return true;
		}
	}

	return false;
}

void DeviceManagement::addWlanDevice(const string &udi)
{
	WlanDevice* dev = new WlanDevice(udi);
	_devices.push_back(dev);
	pDebug(DBG_INFO,"added wlan device %s\n", udi.c_str());
}

void DeviceManagement::addUsbDevice(const string &udi)
{
	UsbDevice* dev = new UsbDevice(udi);
	_devices.push_back(dev);
	pDebug(DBG_INFO,"added usb device %s\n", udi.c_str());
}

void DeviceManagement::addSoundDevice(const string &udi)
{
	SoundDevice* dev = new SoundDevice(udi);
	_devices.push_back(dev);
	pDebug(DBG_INFO,"added sound device %s\n", udi.c_str());
}

void DeviceManagement::addLanDevice(const string &udi)
{
	LanDevice* dev = new LanDevice(udi);
	_devices.push_back(dev);
	pDebug(DBG_INFO,"added aln device %s\n", udi.c_str());
}