{GPL, other}: Relicense to MIT license.
[collectd.git] / src / wireless.c
1 /**
2  * collectd - src/wireless.c
3  * Copyright (C) 2006,2007  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30
31 #if !KERNEL_LINUX
32 # error "No applicable input method."
33 #endif
34
35 #define WIRELESS_PROC_FILE "/proc/net/wireless"
36
37 #if 0
38 static double wireless_dbm_to_watt (double dbm)
39 {
40         double watt;
41
42         /*
43          * dbm = 10 * log_{10} (1000 * power / W)
44          * power = 10^(dbm/10) * W/1000 
45          */
46
47         watt = pow (10.0, (dbm / 10.0)) / 1000.0;
48
49         return (watt);
50 }
51 #endif
52
53 static void wireless_submit (const char *plugin_instance, const char *type,
54                 double value)
55 {
56         value_t values[1];
57         value_list_t vl = VALUE_LIST_INIT;
58
59         values[0].gauge = value;
60
61         vl.values = values;
62         vl.values_len = 1;
63         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
64         sstrncpy (vl.plugin, "wireless", sizeof (vl.plugin));
65         sstrncpy (vl.plugin_instance, plugin_instance,
66                         sizeof (vl.plugin_instance));
67         sstrncpy (vl.type, type, sizeof (vl.type));
68
69         plugin_dispatch_values (&vl);
70 } /* void wireless_submit */
71
72 #define POWER_MIN -90.0
73 #define POWER_MAX -50.0
74 static double wireless_percent_to_power (double quality)
75 {
76         assert ((quality >= 0.0) && (quality <= 100.0));
77
78         return ((quality * (POWER_MAX - POWER_MIN)) + POWER_MIN);
79 } /* double wireless_percent_to_power */
80
81 static int wireless_read (void)
82 {
83 #ifdef KERNEL_LINUX
84         FILE *fh;
85         char buffer[1024];
86
87         char   *device;
88         double  quality;
89         double  power;
90         double  noise;
91         
92         char *fields[8];
93         int   numfields;
94
95         int devices_found;
96         int len;
97
98         /* there are a variety of names for the wireless device */
99         if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
100         {
101                 char errbuf[1024];
102                 WARNING ("wireless: fopen: %s",
103                                 sstrerror (errno, errbuf, sizeof (errbuf)));
104                 return (-1);
105         }
106
107         devices_found = 0;
108         while (fgets (buffer, sizeof (buffer), fh) != NULL)
109         {
110                 char *endptr;
111
112                 numfields = strsplit (buffer, fields, 8);
113
114                 if (numfields < 5)
115                         continue;
116
117                 len = strlen (fields[0]) - 1;
118                 if (len < 1)
119                         continue;
120                 if (fields[0][len] != ':')
121                         continue;
122                 fields[0][len] = '\0';
123
124                 device  = fields[0];
125
126                 quality = strtod (fields[2], &endptr);
127                 if (fields[2] == endptr)
128                         quality = -1.0; /* invalid */
129
130                 /* power [dBm] < 0.0 */
131                 power = strtod (fields[3], &endptr);
132                 if (fields[3] == endptr)
133                         power = 1.0; /* invalid */
134                 else if ((power >= 0.0) && (power <= 100.0))
135                         power = wireless_percent_to_power (power);
136                 else if ((power > 100.0) && (power <= 256.0))
137                         power = power - 256.0;
138                 else if (power > 0.0)
139                         power = 1.0; /* invalid */
140
141                 /* noise [dBm] < 0.0 */
142                 noise = strtod (fields[4], &endptr);
143                 if (fields[4] == endptr)
144                         noise = 1.0; /* invalid */
145                 else if ((noise >= 0.0) && (noise <= 100.0))
146                         noise = wireless_percent_to_power (noise);
147                 else if ((noise > 100.0) && (noise <= 256.0))
148                         noise = noise - 256.0;
149                 else if (noise > 0.0)
150                         noise = 1.0; /* invalid */
151
152                 wireless_submit (device, "signal_quality", quality);
153                 wireless_submit (device, "signal_power", power);
154                 wireless_submit (device, "signal_noise", noise);
155
156                 devices_found++;
157         }
158
159         fclose (fh);
160
161         /* If no wireless devices are present return an error, so the plugin
162          * code delays our read function. */
163         if (devices_found == 0)
164                 return (-1);
165 #endif /* KERNEL_LINUX */
166
167         return (0);
168 } /* int wireless_read */
169
170 void module_register (void)
171 {
172         plugin_register_read ("wireless", wireless_read);
173 } /* void module_register */