root/trunk/freewrt/package/broadcom-watchbutton/watchbutton.c

Revision 658, 3.0 kB (checked in by markus, 2 years ago)

added small watchdog for WRT54G3G button. This is intended to be replaces as soon as gpio is handled on interupts.

Line 
1 /*
2  * watch-button.c - watchdog for WRT54G3G UMTS button
3  *
4  * Copyright (C) 2004-2006 Markus Wigge
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <sys/time.h>
33
34 #define PROC_FILE "/proc/sys/button"
35
36 int proc_fd;
37 int pipe_fds[2];
38
39 void alrm_handler(int signo)
40 {
41     static char last;
42     char c;
43     lseek(proc_fd, 0, SEEK_SET);
44     if (read(proc_fd, &c, 1) == 1) {
45         if ((last == '1') && (c == '0')) {
46             write(pipe_fds[1], "1", 1);
47         }
48         last = c;
49     }
50     else {
51         write(pipe_fds[1], "q", 1);
52     }
53 }
54
55 int main(void)
56 {
57     struct sigaction sa;
58     struct itimerval itv, itv0;
59
60     /*
61        open pipes to communicate with signal handler
62      */
63     if (pipe(pipe_fds) < 0) {
64         perror("pipe");
65         return 1;
66     }
67
68     /*
69        open status file for reading
70      */
71     proc_fd = open(PROC_FILE, O_RDONLY);
72     if (proc_fd < 0) {
73         perror(PROC_FILE);
74         return 1;
75     }
76
77     /*
78        Initialize signal hander
79      */
80     memset(&sa, 0, sizeof(sa));
81     sigemptyset(&sa.sa_mask);
82     sa.sa_handler = alrm_handler;
83     sa.sa_flags = SA_RESTART;
84     if (sigaction(SIGALRM, &sa, NULL) < 0) {
85         perror("sigaction");
86         return 1;
87     }
88
89     /*
90        initialize/start SIGALRM timer
91      */
92     itv.it_interval.tv_sec   = itv.it_value.tv_sec   = 0;
93     itv.it_interval.tv_usec  = itv.it_value.tv_usec  = 100000;
94     itv0.it_interval.tv_sec  = itv0.it_value.tv_sec  = 0;
95     itv0.it_interval.tv_usec = itv0.it_value.tv_usec = 0;
96     if (setitimer(ITIMER_REAL, &itv, NULL) < 0) {
97         perror("setitimer");
98         return 1;
99     }
100
101     while (1) {
102         char c = 0;
103         if (read(pipe_fds[0], &c, 1) == 1) {
104             if (c == '1') {
105                 char *cmd = "/sbin/hotplug button";
106                 // disable timer before executing
107                 setitimer(ITIMER_REAL, &itv0, NULL);
108                 system(cmd);
109                 // enable timer again
110                 setitimer(ITIMER_REAL, &itv, NULL);
111             }
112             else if (c == 'q') {
113                 return 0;
114             }
115         }
116     }
117 }
Note: See TracBrowser for help on using the browser.