rssileds.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * configurable RSSI LED control daemon for OpenWrt
  3. * (c) 2012 Allnet GmbH, Daniel Golle <[email protected]>
  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 [email protected], or
  20. * ALLNET GmbH
  21. * Maistr. 2
  22. * D-82110 Germering
  23. * Germany
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #include <unistd.h>
  32. #include <syslog.h>
  33. #include "iwinfo.h"
  34. #define RUN_DIR "/var/run"
  35. #define LEDS_BASEPATH "/sys/class/leds/"
  36. #define BACKEND_RETRY_DELAY 500000
  37. char *ifname;
  38. int qual_max;
  39. struct led {
  40. char *sysfspath;
  41. FILE *controlfd;
  42. unsigned char state;
  43. };
  44. typedef struct rule rule_t;
  45. struct rule {
  46. struct led *led;
  47. int minq;
  48. int maxq;
  49. int boffset;
  50. int bfactor;
  51. rule_t *next;
  52. };
  53. void log_rules(rule_t *rules)
  54. {
  55. rule_t *rule = rules;
  56. while (rule)
  57. {
  58. syslog(LOG_INFO, " %s r: %d..%d, o: %d, f: %d\n",
  59. rule->led->sysfspath,
  60. rule->minq, rule->maxq,
  61. rule->boffset, rule->bfactor);
  62. rule = rule->next;
  63. }
  64. }
  65. int set_led(struct led *led, unsigned char value)
  66. {
  67. char buf[8];
  68. if ( ! led )
  69. return -1;
  70. if ( ! led->controlfd )
  71. return -1;
  72. if ( led->state == value )
  73. return 0;
  74. snprintf(buf, 8, "%d", value);
  75. rewind(led->controlfd);
  76. if ( ! fwrite(buf, sizeof(char), strlen(buf), led->controlfd) )
  77. return -2;
  78. fflush(led->controlfd);
  79. led->state=value;
  80. return 0;
  81. }
  82. int init_led(struct led **led, char *ledname)
  83. {
  84. struct led *newled;
  85. struct stat statbuffer;
  86. int status;
  87. char *bp;
  88. FILE *bfp;
  89. bp = calloc(sizeof(char), strlen(ledname) + strlen(LEDS_BASEPATH) + 12);
  90. if ( ! bp )
  91. goto return_error;
  92. sprintf(bp, "%s%s/brightness", LEDS_BASEPATH, ledname);
  93. status = stat(bp, &statbuffer);
  94. if ( status )
  95. goto cleanup_fname;
  96. bfp = fopen( bp, "w" );
  97. if ( !bfp )
  98. goto cleanup_fname;
  99. if ( ferror(bfp) )
  100. goto cleanup_fp;
  101. /* sysfs path exists and, allocate LED struct */
  102. newled = calloc(sizeof(struct led),1);
  103. if ( !newled )
  104. goto cleanup_fp;
  105. newled->sysfspath = bp;
  106. newled->controlfd = bfp;
  107. *led = newled;
  108. if ( set_led(newled, 255) )
  109. goto cleanup_fp;
  110. if ( set_led(newled, 0) )
  111. goto cleanup_fp;
  112. return 0;
  113. cleanup_fp:
  114. fclose(bfp);
  115. cleanup_fname:
  116. free(bp);
  117. return_error:
  118. syslog(LOG_CRIT, "can't open LED %s\n", ledname);
  119. *led = NULL;
  120. return -1;
  121. }
  122. void close_led(struct led **led)
  123. {
  124. fclose((*led)->controlfd);
  125. free((*led)->sysfspath);
  126. free((*led));
  127. (*led)=NULL;
  128. }
  129. int quality(const struct iwinfo_ops *iw, const char *ifname)
  130. {
  131. int qual;
  132. if ( ! iw ) return -1;
  133. if (qual_max < 1)
  134. if (iw->quality_max(ifname, &qual_max))
  135. return -1;
  136. if (iw->quality(ifname, &qual))
  137. return -1;
  138. return ( qual * 100 ) / qual_max ;
  139. }
  140. int open_backend(const struct iwinfo_ops **iw, const char *ifname)
  141. {
  142. *iw = iwinfo_backend(ifname);
  143. if (!(*iw))
  144. return 1;
  145. return 0;
  146. }
  147. void update_leds(rule_t *rules, int q)
  148. {
  149. rule_t *rule = rules;
  150. while (rule)
  151. {
  152. int b;
  153. /* offset and factore correction according to rule */
  154. b = ( q + rule->boffset ) * rule->bfactor;
  155. if ( b < 0 )
  156. b=0;
  157. if ( b > 255 )
  158. b=255;
  159. if ( q >= rule->minq && q <= rule->maxq )
  160. set_led(rule->led, (unsigned char)b);
  161. else
  162. set_led(rule->led, 0);
  163. rule = rule->next;
  164. }
  165. }
  166. int main(int argc, char **argv)
  167. {
  168. int i,q,q0,r,s;
  169. const struct iwinfo_ops *iw = NULL;
  170. rule_t *headrule = NULL, *currentrule = NULL;
  171. if (argc < 9 || ( (argc-4) % 5 != 0 ) )
  172. {
  173. printf("syntax: %s (ifname) (refresh) (threshold) (rule) [rule] ...\n", argv[0]);
  174. printf(" rule: (sysfs-name) (minq) (maxq) (offset) (factore)\n");
  175. return 1;
  176. }
  177. ifname = argv[1];
  178. /* refresh interval */
  179. if ( sscanf(argv[2], "%d", &r) != 1 )
  180. return 1;
  181. /* sustain threshold */
  182. if ( sscanf(argv[3], "%d", &s) != 1 )
  183. return 1;
  184. openlog("rssileds", LOG_PID, LOG_DAEMON);
  185. syslog(LOG_INFO, "monitoring %s, refresh rate %d, threshold %d\n", ifname, r, s);
  186. currentrule = headrule;
  187. for (i=4; i<argc; i=i+5) {
  188. if (! currentrule)
  189. {
  190. /* first element in the list */
  191. currentrule = calloc(sizeof(rule_t),1);
  192. headrule = currentrule;
  193. }
  194. else
  195. {
  196. /* follow-up element */
  197. currentrule->next = calloc(sizeof(rule_t),1);
  198. currentrule = currentrule->next;
  199. }
  200. if ( init_led(&(currentrule->led), argv[i]) )
  201. return 1;
  202. if ( sscanf(argv[i+1], "%d", &(currentrule->minq)) != 1 )
  203. return 1;
  204. if ( sscanf(argv[i+2], "%d", &(currentrule->maxq)) != 1 )
  205. return 1;
  206. if ( sscanf(argv[i+3], "%d", &(currentrule->boffset)) != 1 )
  207. return 1;
  208. if ( sscanf(argv[i+4], "%d", &(currentrule->bfactor)) != 1 )
  209. return 1;
  210. }
  211. log_rules(headrule);
  212. q0 = -1;
  213. do {
  214. q = quality(iw, ifname);
  215. if ( q < q0 - s || q > q0 + s ) {
  216. update_leds(headrule, q);
  217. q0=q;
  218. };
  219. // re-open backend...
  220. if ( q == -1 && q0 == -1 ) {
  221. if (iw) {
  222. iwinfo_finish();
  223. iw=NULL;
  224. usleep(BACKEND_RETRY_DELAY);
  225. }
  226. while (open_backend(&iw, ifname))
  227. usleep(BACKEND_RETRY_DELAY);
  228. }
  229. usleep(r);
  230. } while(1);
  231. iwinfo_finish();
  232. return 0;
  233. }