kernel: fq_codel: dont reinit flow state
[openwrt.git] / package / rssileds / src / rssileds.c
1 /*
2  * configurable RSSI LED control daemon for OpenWrt
3  *  (c) 2012 Allnet GmbH, Daniel Golle <dgolle@allnet.de>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * The author may be reached as dgolle@allnet.de, or
20  * ALLNET GmbH
21  * Maistr. 2
22  * D-82110 Germering
23  * Germany
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <unistd.h>
33 #include <syslog.h>
34
35 #include "iwinfo.h"
36
37 #define RUN_DIR                 "/var/run"
38 #define LEDS_BASEPATH           "/sys/class/leds/"
39 #define BACKEND_RETRY_DELAY     500000
40
41 char *ifname;
42 int qual_max;
43
44 struct led {
45         char *sysfspath;
46         FILE *controlfd;
47         unsigned char state;
48 };
49
50 typedef struct rule rule_t;
51 struct rule {
52         struct led *led;
53         int minq;
54         int maxq;
55         int boffset;
56         int bfactor;
57         rule_t *next;
58 };
59
60 void log_rules(rule_t *rules)
61 {
62         rule_t *rule = rules;
63         while (rule)
64         {
65                 syslog(LOG_INFO, " %s r: %d..%d, o: %d, f: %d\n",
66                         rule->led->sysfspath,
67                         rule->minq, rule->maxq,
68                         rule->boffset, rule->bfactor);
69                 rule = rule->next;
70         }
71 }
72
73 int init_led(struct led **led, char *ledname)
74 {
75         struct led *newled;
76         struct stat statbuffer;
77         int status;
78         char *bp;
79         FILE *bfp;
80
81         bp = calloc(sizeof(char), strlen(ledname) + strlen(LEDS_BASEPATH) + 12);
82         if ( ! bp )
83                 goto return_error;
84
85         sprintf(bp, "%s%s/brightness", LEDS_BASEPATH, ledname);
86
87         status = stat(bp, &statbuffer);
88         if ( status )
89                 goto cleanup_fname;
90
91         bfp = fopen( bp, "w" );
92         if ( !bfp )
93                 goto cleanup_fname;
94
95         if ( ferror(bfp) )
96                 goto cleanup_fp;
97
98         /* sysfs path exists and, allocate LED struct */
99         newled = calloc(sizeof(struct led),1);
100         if ( !newled )
101                 goto cleanup_fp;
102
103         newled->sysfspath = bp;
104         newled->controlfd = bfp;
105         
106         *led = newled;
107         return 0;
108
109 cleanup_fp:
110         fclose(bfp);
111 cleanup_fname:
112         free(bp);
113 return_error:
114         syslog(LOG_CRIT, "can't open LED %s\n", ledname);
115         *led = NULL;
116         return -1;
117 }
118
119 void close_led(struct led **led)
120 {
121         fclose((*led)->controlfd);
122         free((*led)->sysfspath);
123         free((*led));
124         (*led)=NULL;
125 }
126
127 int set_led(struct led *led, unsigned char value)
128 {
129         char buf[8];
130
131         if ( ! led )
132                 return -1;
133
134         if ( ! led->controlfd )
135                 return -1;
136
137         snprintf(buf, 8, "%d", value);
138
139         rewind(led->controlfd);
140
141         if ( ! fwrite(buf, sizeof(char), strlen(buf), led->controlfd) )
142                 return -2;
143
144         fflush(led->controlfd);
145         led->state=value;
146
147         return 0;
148 }
149
150
151 int quality(const struct iwinfo_ops *iw, const char *ifname)
152 {
153         int qual;
154
155         if ( ! iw ) return -1;
156
157         if (qual_max < 1)
158                 if (iw->quality_max(ifname, &qual_max))
159                         return -1;
160
161         if (iw->quality(ifname, &qual))
162                 return -1;
163
164         return ( qual * 100 ) / qual_max ;
165 }
166
167 int open_backend(const struct iwinfo_ops **iw, const char *ifname)
168 {
169         *iw = iwinfo_backend(ifname);
170
171         if (!(*iw))
172                 return 1;
173
174         return 0;
175 }
176
177 void update_leds(rule_t *rules, int q)
178 {
179         rule_t *rule = rules;
180         while (rule)
181         {
182                 int b;
183                 /* offset and factore correction according to rule */
184                 b = ( q + rule->boffset ) * rule->bfactor;
185                 if ( b < 0 )
186                         b=0;
187                 if ( b > 255 )
188                         b=255;
189
190                 if ( q >= rule->minq && q <= rule->maxq )
191                         set_led(rule->led, (unsigned char)b);
192                 else
193                         set_led(rule->led, 0);
194
195                 rule = rule->next;
196         }
197 }
198
199 int main(int argc, char **argv)
200 {
201         int i,q,q0,r,s;
202         const struct iwinfo_ops *iw = NULL;
203         rule_t *headrule = NULL, *currentrule = NULL;
204
205         if (argc < 9 || ( (argc-4) % 5 != 0 ) )
206         {
207                 printf("syntax: %s (ifname) (refresh) (threshold) (rule) [rule] ...\n", argv[0]);
208                 printf("  rule: (sysfs-name) (minq) (maxq) (offset) (factore)\n");
209                 return 1;
210         }
211
212         ifname = argv[1];
213
214         /* refresh interval */
215         if ( sscanf(argv[2], "%d", &r) != 1 )
216                 return 1;
217
218         /* sustain threshold */
219         if ( sscanf(argv[3], "%d", &s) != 1 )
220                 return 1;
221
222         openlog("rssileds", LOG_PID, LOG_DAEMON);
223         syslog(LOG_INFO, "monitoring %s, refresh rate %d, threshold %d\n", ifname, r, s);
224
225         currentrule = headrule;
226         for (i=4; i<argc; i=i+5) {
227                 if (! currentrule)
228                 {
229                         /* first element in the list */
230                         currentrule = calloc(sizeof(rule_t),1);
231                         headrule = currentrule;
232                 }
233                 else
234                 {
235                         /* follow-up element */
236                         currentrule->next = calloc(sizeof(rule_t),1);
237                         currentrule = currentrule->next;
238                 }
239
240                 if ( init_led(&(currentrule->led), argv[i]) )
241                         return 1;
242                 
243                 if ( sscanf(argv[i+1], "%d", &(currentrule->minq)) != 1 )
244                         return 1;
245
246                 if ( sscanf(argv[i+2], "%d", &(currentrule->maxq)) != 1 )
247                         return 1;
248                 
249                 if ( sscanf(argv[i+3], "%d", &(currentrule->boffset)) != 1 )
250                         return 1;
251                 
252                 if ( sscanf(argv[i+4], "%d", &(currentrule->bfactor)) != 1 )
253                         return 1;
254         }
255         log_rules(headrule);
256
257         q0 = -1;
258         do {
259                 q = quality(iw, ifname);
260                 if ( q < q0 - s || q > q0 + s ) {
261                         update_leds(headrule, q);
262                         q0=q;
263                 };
264                 // re-open backend...
265                 if ( q == -1 && q0 == -1 ) {
266                         if (iw) {
267                                 iwinfo_finish();
268                                 iw=NULL;
269                                 usleep(BACKEND_RETRY_DELAY);
270                         }
271                         while (open_backend(&iw, ifname))
272                                 usleep(BACKEND_RETRY_DELAY);
273                 }
274                 usleep(r);
275         } while(1);
276
277         iwinfo_finish();
278
279         return 0;
280 }