-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoor-monitor.c
240 lines (203 loc) · 5.99 KB
/
door-monitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* +-----------------------------------------------------------------------+
* | Copyright (C) 2017-2018 George Z. Zachos |
* +-----------------------------------------------------------------------+
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* Name: George Z. Zachos
* Email: gzzachos <at> gmail.com
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <wiringPi.h>
/* Global definitions */
#define __STR(val) #val
#define STR(val) __STR(val)
#define REED_SENSOR_PIN 0
#define BUZZER_PIN 13
#define DOOR_IS_CLOSED(state) (state == LOW)
#define DOOR_WAS_OPEN(state) (state == HIGH)
#define DOOR_WAS_CLOSED(state) (state == LOW)
#define DOOR_CLOSED(state,prev_state) \
(DOOR_WAS_OPEN(prev_state) && DOOR_IS_CLOSED(state))
#define DOOR_OPENED(state,prev_state) \
(DOOR_WAS_CLOSED(prev_state) && !DOOR_IS_CLOSED(state))
typedef struct timeval timeval_t;
typedef struct thrarg_s {
timeval_t req_timeval;
char *state;
} thrarg_t;
/* Function prototypes */
long get_timestamp(timeval_t tv);
thrarg_t *alloc_arg(timeval_t req_timeval, char *state);
void *notify_by_mail(void *arg);
void terminate(int signo);
void *hit_buzzer(void *arg);
int timevalcmp(timeval_t tv1, timeval_t tv2);
/* Global declarations */
volatile sig_atomic_t exitflag = 0;
volatile timeval_t latest_closed_door;
int main(void)
{
volatile int curr_state,
prev_state = -1;
pthread_attr_t attr;
pthread_t tid;
thrarg_t *arg, *buzzer_arg;
timeval_t req_timeval;
long req_timestamp;
openlog("door-monitor", LOG_PID, LOG_USER);
signal(SIGTERM, &terminate);
signal(SIGINT, &terminate);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
wiringPiSetup();
if (piHiPri(99))
syslog(LOG_INFO, "piHiPri: %s", strerror(errno));
pinMode(REED_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
for (; exitflag == 0;)
{
curr_state = digitalRead(REED_SENSOR_PIN);
gettimeofday(&req_timeval, NULL);
req_timestamp = get_timestamp(req_timeval);
if (DOOR_CLOSED(curr_state, prev_state))
{
latest_closed_door = req_timeval;
syslog(LOG_INFO, "%ld: Door closed\n", req_timestamp);
arg = alloc_arg(req_timeval, "closed");
pthread_create(&tid, &attr, ¬ify_by_mail, (void *)arg);
}
if (DOOR_OPENED(curr_state, prev_state))
{
syslog(LOG_INFO, "%ld: Door opened\n", req_timestamp);
buzzer_arg = alloc_arg(req_timeval, "opened");
pthread_create(&tid, &attr, &hit_buzzer, (void *)buzzer_arg);
arg = alloc_arg(req_timeval, "opened");
pthread_create(&tid, &attr, ¬ify_by_mail, (void *)arg);
}
prev_state = curr_state;
delay((unsigned) 100);
}
syslog(LOG_INFO, "Termination requested (%s)", strsignal(exitflag));
pthread_attr_destroy(&attr);
closelog();
return (EXIT_SUCCESS);
}
long get_timestamp(timeval_t tv)
{
return ((long) tv.tv_sec + (tv.tv_usec)*1.0E-06);
}
thrarg_t *alloc_arg(timeval_t req_timeval, char *state)
{
thrarg_t *arg;
arg = (thrarg_t *) malloc(sizeof(thrarg_t));
if (!arg)
{
syslog(LOG_INFO, "malloc: %s", strerror(errno));
return NULL;
}
arg->req_timeval = req_timeval;
arg->state = state;
return arg;
}
void *notify_by_mail(void *arg)
{
char cmd[256], *state;
int ret, attempts;
long req_timestamp;
if (!arg)
return ((void *) EXIT_FAILURE);
req_timestamp = get_timestamp(((thrarg_t *) arg)->req_timeval);
state = ((thrarg_t *) arg)->state;
snprintf(cmd, 256, "%s/door-sendmail.sh %s %ld", STR(TARGET_DIR),
state, req_timestamp);
for (attempts = 10; attempts > 0; attempts--)
{
ret = system(cmd);
if (ret != -1 && WIFEXITED(ret))
{
ret = WEXITSTATUS(ret);
if (ret == 0)
{
syslog(LOG_INFO, "%ld: Mail sent (%s)\n",
req_timestamp, state);
free(arg);
return ((void *) EXIT_SUCCESS);
}
}
syslog(LOG_INFO, "%ld: Attempt to send mail #%d/9\n",
req_timestamp, 10-attempts);
pthread_yield();
delay((unsigned) 500);
}
free(arg);
return ((void *) EXIT_FAILURE);
}
void terminate(int signo)
{
exitflag = signo;
}
void *hit_buzzer(void *arg)
{
int i;
long req_timestamp;
timeval_t opendoor_time;
if (!arg)
return ((void *) EXIT_FAILURE);
opendoor_time = ((thrarg_t *) arg)->req_timeval;
req_timestamp = get_timestamp(opendoor_time);
for (i = 0; i < 3; i++)
{
digitalWrite(BUZZER_PIN, HIGH);
delay((unsigned) 1000);
digitalWrite(BUZZER_PIN, LOW);
delay((unsigned) 500);
}
for (i = 0; timevalcmp(latest_closed_door, opendoor_time) == -1; i++)
{
if (i == 60*5)
{
syslog(LOG_INFO, "%ld: Door is still open\n", req_timestamp);
digitalWrite(BUZZER_PIN, HIGH);
delay((unsigned) 1000*5);
digitalWrite(BUZZER_PIN, LOW);
i = 0;
}
delay((unsigned) 1000);
}
free(arg);
return ((void *) EXIT_SUCCESS);
}
int timevalcmp(timeval_t tv1, timeval_t tv2)
{
if (tv1.tv_sec < tv2.tv_sec)
return (-1);
else if (tv1.tv_sec > tv2.tv_sec)
return (1);
else if (tv1.tv_usec < tv2.tv_usec)
return (-1);
else if (tv1.tv_usec > tv2.tv_usec)
return (1);
return (0);
}