summaryrefslogtreecommitdiff
path: root/wifi-new.c
blob: cf311b9ec536bda7ecd4f1eb98f6832638f60ced (plain)
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
/*
 * Copyright 2007, Intel Corporation
 * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
 *
 * This file is part of PowerTOP
 *
 * This program file 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; version 2 of the License.
 *
 * 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 in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 *	Johannes Berg <johannes@sipsolutions.net>
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <dirent.h>
#include <linux/types.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
/* satisfy weird wireless.h include dependencies */
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/wireless.h>

#include "powertop.h"

static int wext_sock = -1;
static char wireless_nic[IFNAMSIZ + 1] = {0};
static bool ps_not_working = false;

static bool check_wireless_unused(void)
{
	struct iwreq wrq;
	char ssid[34] = {0}; /* wext sometimes NUL-terminates */

	memset(&wrq, 0, sizeof(wrq));
	strncpy(wrq.ifr_name, wireless_nic, sizeof(wrq.ifr_name));

	/* get mode -- PS only available in STA mode */
	if (ioctl(wext_sock, SIOCGIWMODE, &wrq) < 0)
		return false;
	if (wrq.u.mode != IW_MODE_INFRA)
		return false;

	wrq.u.essid.pointer = ssid;
	wrq.u.essid.length = sizeof(ssid);
	if (ioctl(wext_sock, SIOCGIWESSID, &wrq) < 0)
		return false;

	/* no SSID, so can't be connected */
	if (!wrq.u.essid.flags)
		return true;

	if (ioctl(wext_sock, SIOCGIWAP, &wrq) < 0)
		return false;

	/* no AP -- not connected */
	if (memcmp(wrq.u.ap_addr.sa_data, "\0\0\0\0\0\0", 6) == 0)
		return true;

	return false;
}

static void activate_down_suggestion(void)
{
	struct ifreq ifr;
	int ret;

	strncpy(ifr.ifr_name, wireless_nic, sizeof(ifr.ifr_name));

	/* get flags */
	ret = ioctl(wext_sock, SIOCGIFFLAGS, &ifr);
	if (ret < 0)
		return;
	ifr.ifr_flags &= ~IFF_UP;

	ioctl(wext_sock, SIOCSIFFLAGS, &ifr);
}

static int check_wireless_powersave(void)
{
	struct iwreq wrq;

	if (ps_not_working)
		return false;

	memset(&wrq, 0, sizeof(wrq));
	strncpy(wrq.ifr_name, wireless_nic, sizeof(wrq.ifr_name));

	/* get powersave -- if supported */
	if (ioctl(wext_sock, SIOCGIWPOWER, &wrq) < 0) {
		ps_not_working = 1;
		return false;
	}

	return wrq.u.param.disabled;
}

static void activate_wireless_suggestion(void)
{
	struct iwreq wrq;

	memset(&wrq, 0, sizeof(wrq));
	strncpy(wrq.ifr_name, wireless_nic, sizeof(wrq.ifr_name));

	wrq.u.param.disabled = false;
	wrq.u.param.flags = IW_POWER_ON | IW_POWER_TIMEOUT;
	wrq.u.param.value = 500*1000; /* 500 ms */
 again:
	/* set powersave -- if supported */
	if (ioctl(wext_sock, SIOCSIWPOWER, &wrq) == 0)
		return;

	/* maybe timeout is not supported? */
	if (wrq.u.param.flags & IW_POWER_TIMEOUT) {
		wrq.u.param.flags = IW_POWER_ON;
		goto again;
	}

	ps_not_working = true;
}

static void find_wireless_nic(void)
{
	DIR *net;
	struct dirent *ent;
	struct ifreq ifr;
	struct iwreq wrq;
	int ret;

	memset(&ifr, 0, sizeof(struct ifreq));
	memset(&wrq, 0, sizeof(wrq));

	net = opendir("/sys/class/net/");
	if (!net)
		return;

	while ((ent = readdir(net))) {
		strncpy(ifr.ifr_name, ent->d_name, sizeof(ifr.ifr_name));
		strncpy(wrq.ifr_name, ent->d_name, sizeof(wrq.ifr_name));

		/* Check if the interface is up */
		ret = ioctl(wext_sock, SIOCGIFFLAGS, &ifr);
		if (ret < 0)
			continue;
		if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)))
			continue;

		/* and a wireless interface (that we handle) */
		ret = ioctl(wext_sock, SIOCGIWNAME, &wrq);
		if (ret < 0)
			continue;

		strcpy(wireless_nic, "wlan0");
		break;
	}

	closedir(net);
}

void suggest_wifi_new_powersave(void)
{
	char sug[1024];

	if (wext_sock == -1) {
		wext_sock = socket(AF_INET, SOCK_DGRAM, 0);
		if (wext_sock < 0)
			return;
	}

	if (strlen(wireless_nic) == 0)
		find_wireless_nic();

	if (strlen(wireless_nic) == 0)
		return;

	if (check_wireless_unused()) {
		sprintf(sug, _("Suggestion: Disable the unused WIFI radio by setting the interface down:\n "
			       "ifconfig %s down\n"), wireless_nic);
		add_suggestion(sug, 60, 'D', _(" D - disable wireless "), activate_down_suggestion);
	} else if (check_wireless_powersave()) {
		sprintf(sug, _("Suggestion: Enable wireless power saving mode by executing the following command:\n "
			       " iwconfig %s power timeout 500ms\n"
			       "This will sacrifice network performance slightly to save power."), wireless_nic);
		add_suggestion(sug, 20, 'W', _(" W - Enable Wireless power saving "), activate_wireless_suggestion);
	}
}