-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal_handler.c
executable file
·280 lines (255 loc) · 9.18 KB
/
signal_handler.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "mhc.h"
/* Handle termination of the program
*/
void signalHandler( int signum ) {
// Return codes from function calls
int rc;
// Pointer to log file
FILE *fPtr;
// Exit message
char * exitMsg = "Unknown termination signal";
// Try to catch non system signals
if (signum > 32) {
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"signalHandler called: %u\n",signum);
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
return;
}
// Final exit signal
if (signum == 0) {
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"signalHandler called: %u\n",signum);
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
exit(signum);
}
switch (signum) {
case SIGINT:
exitMsg = "\nSIGINT - Interactive attention signal received";
getSysTime(1);
fPtr = fopen("/home/pi/mhcv.txt", "a"); // /home/pi on Raspian
dbgLen = sprintf(debugMsg,"%s: Received SIGINT:\nExitMsg: %s\nSignum: %d\n", sysTime, exitMsg, signum);
fwrite(debugMsg,dbgLen,1,fPtr);
fclose(fPtr);
break;
case SIGUSR1: // called when UDP data has been received and is ready to be published on MQTT
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"signalHandler called: SIGUSR1\n");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
checkMQTT();
if (mqttUp) { // only try to publish if we are connected to the broker
// if (hasWEIJSON) {
// //{"de":"wei","te":29,"hu":46,"he":29.2}
// char inWeather[50];
// sprintf(inWeather,"{\"de\":\"wei\",\"te\":%.1f,\"hu\":%.1f,\"he\":%.1f}"
// , tempInside, humidInside, heatInside);
// udpSend(inWeather, strlen(inWeather));
// }
// // Publish device status to MQTT broker
// mqttPublisher();
}
return;
break;
case SIGUSR2: // called if command arrived over MQTT or TCP server
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"signalHandler called: SIGUSR2\n");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
// Check if we received a command over MQTT
if (hasMqttCmd) {
if (mqttDebugOn) {
dbgLen = sprintf(debugMsg,"SIGUSR2: received a command over MQTT\n");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
mqttProcess();
}
// Check if there is an incoming TCP request
if (hasTCPdata) {
hasTCPdata = false;
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"SIGUSR2: received TCP packet: >%s<\n", tcpBufTemp);
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
if (strncmp(tcpBufTemp,"r",1) == 0) {
dbgLen = sprintf(debugMsg,"SIGUSR2: TCP packet: Reboot!");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
// Reset the machine!
system ("sudo reboot now");
} else if (strncmp(tcpBufTemp,"dg",2) == 0) {
// Toggle Debug!
genDebugOn = !genDebugOn;
dbgLen = sprintf(debugMsg,"SIGUSR2: TCP packet: Switch General Debug to %s\n",
genDebugOn ? "true" : "false");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
} else if (strncmp(tcpBufTemp,"dc",2) == 0) {
// Toggle Debug!
camDebugOn = !camDebugOn;
dbgLen = sprintf(debugMsg,"SIGUSR2: TCP packet: Switch Camera Debug to %s\n",
camDebugOn ? "true" : "false");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
} else if (strncmp(tcpBufTemp,"dd",2) == 0) {
// Toggle Debug!
dht11DebugOn = !dht11DebugOn;
dbgLen = sprintf(debugMsg,"SIGUSR2: TCP packet: Switch DHT11 Debug to %s\n",
dht11DebugOn ? "true" : "false");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
} else if (strncmp(tcpBufTemp,"dm",2) == 0) {
// Toggle Debug!
mqttDebugOn = !mqttDebugOn;
dbgLen = sprintf(debugMsg,"SIGUSR2: TCP packet: Switch MQTT Debug to %s\n",
mqttDebugOn ? "true" : "false");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
} else if (strncmp(tcpBufTemp,"du",2) == 0) {
// Toggle Debug!
udpDebugOn = !udpDebugOn;
dbgLen = sprintf(debugMsg,"SIGUSR2: TCP packet: Switch UDP Debug to %s\n",
udpDebugOn ? "true" : "false");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
}
// Check if there is an incoming TCP debug request
if (hasDebugTCPdata) {
hasDebugTCPdata = false;
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"SIGUSR2: received TCP debug packet: %s\n", tcpBufTemp);
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
// Write the received data into a file
getSysTime(1);
fPtr = fopen("/home/pi/log.txt", "a"); // /home/pi on Raspian
fwrite(sysTime,20,1,fPtr);
fwrite(debugTcpBufTemp,debugTcpRecvBytesTemp,1,fPtr);
fwrite("\n",1,1,fPtr);
fclose(fPtr);
}
return;
break;
case SIGALRM: // called every 10 seconds
// Count up events for device's clocks update time
ntpCnt++;
// Count up events for camera status update time
camStatusCnt++;
// Check if we are still connected to the MQTT server
checkMQTT();
// Check if the UDP listener is still active
checkUDP();
// Check if TCP listener is still active
checkTCP();
// Check if we are still connected to DiskStation
checkSamba();
// Get temperature from DHT11 sensor
// if (genDebugOn) {
// dbgLen = sprintf(debugMsg,"SIGALRM triggered -> get temperature\n");
// tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
// }
// dht11Read();
// Check if it is time to reboot
lunchReboot();
// Check if it is time to reboot the WiFi router
// rebootRouter();
if (ntpCnt >= 1080) { // 10s * 1080 = 10800s = 180min = 3 hours
// Update device's clocks every 3 hours
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"SIGALRM 1080 times triggered -> update device's clocks\n");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
if (tcpUp) { // only try to update if TCP socket is open
updateDeviceTime();
}
ntpCnt = 0;
// Check if storage is getting full and clean it up if necessary
cleanDir();
}
if (camStatusCnt >= 5) { // 10s * 6 = 1min
camStatusCnt = 0;
// Send camera status every 1 minute
if (camDebugOn) {
dbgLen = sprintf(debugMsg,"SIGALRM 60 times triggered -> send camera status\n");
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
// Init JSON object
json_value_free(mhcJSON); // Free the allocated memory (if any)
mhcJSON = json_value_init_object(); // Init JSON object
JSON_Object *root_object = json_value_get_object(mhcJSON);
json_object_set_string(root_object,"de","mhc");
hasMHCJSON = true;
// Broadcast status
char *statusMsg = "{\"de\":\"mhc\"}\00";
int statMsgLen = 0;
statMsgLen = mbstowcs(NULL,statusMsg,0);
udpSend(statusMsg, statMsgLen);
// // String for status output
// char *serialized_string = json_serialize_to_string(mhcJSON);
// int jsonLenRoot = 0;
// jsonLenRoot = mbstowcs(NULL,serialized_string,0);
// if (jsonLenRoot != 0) {
// udpSend(serialized_string, jsonLenRoot);
// }
// json_free_serialized_string(serialized_string);
// Publish device status to MQTT broker
mqttPublisher();
}
// Check if we have a day change and move videos into subfolder
moveVideos();
if (!udpUp) { // UDP receiver stopped
int parameter;
pthread_create(&threadUDPserv, NULL, udpServer, ¶meter);
}
return;
break;
default:
if (genDebugOn) {
dbgLen = sprintf(debugMsg,"Received unknown signal:\nExitMsg: %s\nSignum: %d\n", exitMsg, signum);
tcpSendToPort(debugMsg, ipDBG, dbgLen, DEBUG_TCP_PORT_ANDROID);
}
getSysTime(1);
fPtr = fopen("/home/pi/mhcv.txt", "a"); // /home/pi on Raspian
dbgLen = sprintf(debugMsg,"%s: Received signal:\nExitMsg: %s\nSignum: %d\n", sysTime, exitMsg, signum);
fwrite(debugMsg,dbgLen,1,fPtr);
fclose(fPtr);
exit(signum);
break;
}
printf("%s - %d\n", exitMsg, signum);
fPtr = fopen("/home/pi/mhcv.txt", "a"); // /home/pi on Raspian
dbgLen = sprintf(debugMsg,"%s: Received SIGINT:\nExitMsg: %s\nSignum: %d\n", sysTime, exitMsg, signum);
fwrite(debugMsg,dbgLen,1,fPtr);
fclose(fPtr);
// Stop the CAM streaming thread
#ifdef CAM_ACTIV
pthread_cancel(threadCamStream);
#endif
// Stop the TCP server thread
pthread_cancel(threadTCPserv);
// Close TCP socket
if (tcpUp) {
tcpClose();
}
// Stop the TCP debug server thread
pthread_cancel(threadDebugserv);
// Close TCP socket
if (tcpDebugUp) {
debugTcpClose();
}
// Stop the interval timer
myIntTimer.it_value.tv_sec = 0;
myIntTimer.it_value.tv_usec = 0;
myIntTimer.it_interval.tv_sec = 0;
myIntTimer.it_interval.tv_usec = 0;
setitimer (ITIMER_REAL, &myIntTimer, NULL);
// Unsubscribe from MQTT topic(s)
unsubscribeTopic("/CMD");
// Close MQTT connection
if (mqttUp) {
mqttClose();
}
// Close UDP socket
if (udpUp) {
udpClose();
}
system("sudo /usr/local/bin/mhc &");
// terminate program
exit(signum);
}