wmbiff: Bump to version 0.4.33
[dockapps.git] / wmwifi / src / wireless.c
blobbc9753b1bf1bdfa9aa0a263079351e26a872d974
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <X11/X.h>
7 #include <X11/xpm.h>
8 #include "wmwifi.h"
9 #include <net/ethernet.h>
10 #include <linux/if.h>
11 #include <linux/socket.h>
12 #include <linux/wireless.h>
15 * This is my quick and dirty way of accessing the Linux Wireless Extensions
16 * from the /proc/net/wireless interface.
17 * Open the file, and get the Interface name, link, level, and noise.
18 * - Jess
21 int get_wifi_info(struct wifi *wfi)
23 struct iwreq wrq;
24 struct iw_range range;
25 int skfd;
26 float ret;
27 int i;
28 FILE *fp;
29 char buff[1024] = "";
31 if (fp = fopen("/proc/net/wireless", "r")) {
32 //if (fp = fopen("./demo.txt", "r")) {
33 for (i = 0; i < (wfi->ifnum + 3); i++) {
34 fgets(buff, sizeof(buff), fp);
35 buff[strlen(buff) + 1] = '\0';
37 fclose(fp);
38 } else {
39 wfi->max_qual = -1;
40 fprintf(stdout,
41 "ERROR: Cannot Open Linux Wireless Extensions!\n\b");
42 // exit(1);
45 sscanf(buff, "%s %*s %f %d", &wfi->ifname, &wfi->link, &wfi->level);
46 /* Don't know why I have to make a second call to sscanf
47 * to pull wfi->noise, but for some wierd reason I cannot
48 * pull it above along with the other stats.
50 sscanf(buff, "%*s %*s %*s %*s %d", &wfi->noise);
52 /* Thanks to Jeroen Nijhof <jnijhof@nijhofnet.nl>
53 * for catching this, I guess I overlooked it.
54 * Strip out the trailing ':' if exists.
56 if (wfi->ifname[strlen(wfi->ifname) - 1] == ':') {
57 wfi->ifname[strlen(wfi->ifname) - 1] = '\0';
60 memset(&wfi->essid, 0, IW_ESSID_MAX_SIZE + 1);
62 skfd = socket(AF_INET, SOCK_DGRAM, 0);
64 memset(&range, 0, sizeof(struct iw_range));
66 wrq.u.data.pointer = (caddr_t) & range;
67 wrq.u.data.length = sizeof(struct iw_range);
68 wrq.u.data.flags = 0;
69 strncpy(wrq.ifr_name, wfi->ifname, IFNAMSIZ);
70 ret = ioctl(skfd, SIOCGIWRANGE, &wrq);
71 if (ret > -1) {
72 ret = range.max_qual.qual;
73 } else {
74 fprintf(stdout, "ERROR\n");
75 /* Maybe put auto-learning HERE? */
78 wfi->max_qual = ret;
80 wrq.u.essid.pointer = (caddr_t) wfi->essid;
81 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
82 wrq.u.essid.flags = 0;
83 ret = ioctl(skfd, SIOCGIWESSID, &wrq);
85 ret = ioctl(skfd, SIOCGIWRATE, &wrq);
86 memcpy(&(wfi->bitrate), &(wrq.u.bitrate), sizeof(struct iw_param));
88 if (strlen(wfi->essid) < 1) {
89 strcpy(wfi->essid, "not_set");
92 if (wfi->link > wfi->max_link) {
93 wfi->max_link = wfi->link;
95 if (wfi->max_link > wfi->max_qual) {
96 wfi->max_qual = wfi->max_link;
99 * For debugging purposes.
100 * Check these values against /proc/net/wireless and
101 * iwconfig
103 fprintf(stdout, "wfi->ifname -> %s\n", wfi->ifname);
104 fprintf(stdout, "wfi->max_qual -> %f\n", wfi->max_qual);
105 fprintf(stdout, "wfi->essid -> %s\n", wfi->essid);
106 fprintf(stdout, "wfi->link -> %f\n", wfi->link);
107 fprintf(stdout, "wfi->level -> %d\n", wfi->level);
108 fprintf(stdout, "wfi->noise -> %d\n", wfi->noise - 0x100);
109 fprintf(stdout, "wfi->bitrate -> %d\n", wfi->bitrate.value);
110 fprintf(stdout, "wfi->max_link -> %.f\n", wfi->max_link);
114 close(skfd);
116 return ret;
119 void next_if(struct wifi *wfi)
121 int max;
123 max = get_max_ifs();
124 wfi->max_link = 0;
126 if (wfi->ifnum < max) {
127 wfi->ifnum++;
128 } else {
129 wfi->ifnum = 0;
132 /* Not used anymore, how many mouse button's do
133 * you have?
135 void last_if(struct wifi *wfi)
137 int max;
139 max = get_max_ifs();
140 wfi->max_link = 0;
142 if (wfi->ifnum > 0) {
143 wfi->ifnum--;
144 } else {
145 wfi->ifnum = max;
148 int get_max_ifs(void)
150 FILE *fp;
151 char buff[255];
152 int i = 0;
154 if (fp = fopen("/proc/net/wireless", "r")) {
155 //if (fp = fopen("./demo.txt", "r")) {
156 for (i = -3; fgets(buff, sizeof(buff), fp); i++) {
158 fclose(fp);
160 return i;