-
Notifications
You must be signed in to change notification settings - Fork 68
/
PiGatewaySerial.cpp
312 lines (269 loc) · 6.96 KB
/
PiGatewaySerial.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* PiGatewaySerial.cpp - MySensors Gateway for wireless node providing a serial interface
*
* Copyright 2014 Tomas Hozza <[email protected]>
*
* 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.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <pty.h>
#include <termios.h>
#include <poll.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <syslog.h>
#include <RF24.h>
#include <MyGateway.h>
#include <Version.h>
#ifndef _TTY_NAME
#define _TTY_NAME "/dev/ttyMySensorsGateway"
#endif
#ifndef _TTY_GROUPNAME
#define _TTY_GROUPNAME "tty"
#endif
/* variable indicating if the server is still running */
volatile static int running = 1;
/* PTY file descriptors */
int pty_master = -1;
int pty_slave = -1;
static const mode_t ttyPermissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
static const char *serial_tty = _TTY_NAME;
static const char *devGroupName = _TTY_GROUPNAME;
int daemonizeFlag = 0;
void openSyslog()
{
setlogmask(LOG_UPTO (LOG_INFO));
openlog(NULL, 0, LOG_USER);
}
void closeSyslog()
{
closelog();
}
void log(int priority, const char *format, ...)
{
va_list argptr;
va_start(argptr, format);
if (daemonizeFlag == 1) {
vsyslog(priority, format, argptr);
} else {
vprintf(format, argptr);
}
va_end(argptr);
}
/*
* handler for SIGINT signal
*/
void handle_sigint(int sig)
{
log(LOG_INFO,"Received SIGINT\n");
running = 0;
}
void handle_sigusr1(int sig)
{
log(LOG_INFO,"Received SIGUSR1\n");
int curLogLevel = setlogmask(0);
if (curLogLevel != LOG_UPTO(LOG_DEBUG)) setlogmask(LOG_UPTO (LOG_DEBUG));
else setlogmask(LOG_UPTO (LOG_INFO));
}
/*
* callback function writting data from RF24 module to the PTY
*/
void write_msg_to_pty(char *msg)
{
size_t len = 0;
if (msg == NULL)
{
log(LOG_WARNING,"[callback] NULL msg received!\n");
return;
}
len = strlen(msg);
write(pty_master, msg, len);
}
/*
* configure PTY master FD
*/
void configure_master_fd(int fd)
{
struct termios settings;
tcgetattr(fd, &settings);
/* turn off ECHO of written characters */
settings.c_lflag &= ~ECHO;
tcsetattr(fd, 0, &settings);
}
static void daemonize(void)
{
pid_t pid, sid;
int fd;
// already a daemon
if ( getppid() == 1 ) return;
// Fork off the parent process
pid = fork();
if (pid < 0) exit(EXIT_FAILURE); // fork() failed
if (pid > 0) exit(EXIT_SUCCESS); // fork() successful, this is the parent process, kill it
// From here on it is child only
// Create a new SID for the child process
sid = setsid();
if (sid < 0) exit(EXIT_FAILURE); // Not logging as nobody can see it.
// Change the current working directory.
if ((chdir("/")) < 0) exit(EXIT_FAILURE);
// Divert the standard file desciptors to /dev/null
fd = open("/dev/null",O_RDWR, 0);
if (fd != -1)
{
dup2 (fd, STDIN_FILENO);
dup2 (fd, STDOUT_FILENO);
dup2 (fd, STDERR_FILENO);
if (fd > 2) close (fd);
}
// reset File Creation Mask
umask(027);
}
/*
* Main gateway logic
*/
int main(int argc, char **argv)
{
struct pollfd fds;
struct group* devGrp;
MyGateway *gw = NULL;
int status = EXIT_SUCCESS;
int ret, c;
while ((c = getopt (argc, argv, "d")) != -1)
{
switch (c)
{
case 'd':
daemonizeFlag = 1;
break;
}
}
openSyslog();
log(LOG_INFO,"Starting PiGatewaySerial...\n");
log(LOG_INFO,"Protocol version - %s\n", LIBRARY_VERSION);
/* register the signal handler */
signal(SIGINT, handle_sigint);
signal(SIGTERM, handle_sigint);
signal(SIGUSR1, handle_sigusr1);
/* create MySensors Gateway object */
#ifdef __PI_BPLUS
gw = new MyGateway(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ, 1);
#else
gw = new MyGateway(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ, 1);
#endif
if (gw == NULL)
{
log(LOG_ERR,"Could not create MyGateway! (%d) %s\n", errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
/* create PTY - Pseudo TTY device */
ret = openpty(&pty_master, &pty_slave, NULL, NULL, NULL);
if (ret != 0)
{
log(LOG_ERR,"Could not create a PTY! (%d) %s\n", errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
errno = 0;
devGrp = getgrnam(devGroupName);
if(devGrp == NULL)
{
log(LOG_ERR,"getgrnam: %s failed. (%d) %s\n", devGroupName, errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
ret = chown(ttyname(pty_slave),-1,devGrp->gr_gid);
if (ret == -1)
{
log(LOG_ERR,"chown failed. (%d) %s\n", errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
ret = chmod(ttyname(pty_slave),ttyPermissions);
if (ret != 0)
{
log(LOG_ERR,"Could not change PTY permissions! (%d) %s\n", errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
log(LOG_INFO,"Created PTY '%s'\n", ttyname(pty_slave));
/* create a symlink with predictable name to the PTY device */
unlink(serial_tty); // remove the symlink if it already exists
ret = symlink(ttyname(pty_slave), serial_tty);
if (ret != 0)
{
log(LOG_ERR,"Could not create a symlink '%s' to PTY! (%d) %s\n", serial_tty, errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
log(LOG_INFO,"Gateway tty: %s\n", serial_tty);
close(pty_slave);
configure_master_fd(pty_master);
fds.events = POLLRDNORM;
fds.fd = pty_master;
if (daemonizeFlag) daemonize();
/* we are ready, initialize the Gateway */
gw->begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, &write_msg_to_pty);
/* Do the work until interrupted */
while(running)
{
/* process radio msgs */
gw->processRadioMessage();
/* process serial port msgs */
ret = poll(&fds, 1, 500);
if (ret == -1)
{
log(LOG_ERR,"poll() error (%d) %s\n", errno, strerror(errno));
sleep(10);
}
else if (ret == 0)
{
/* timeout */
continue;
}
else
{
if (fds.revents & POLLRDNORM)
{
char buff[256];
ssize_t size;
fds.revents = 0;
size = read(pty_master, buff, sizeof(buff));
if (size < 0)
{
log(LOG_ERR,"read error (%d) %s\n", errno, strerror(errno));
continue;
}
buff[size] = '\0';
gw->parseAndSend(buff);
} else {
sleep(60);
}
}
}
cleanup:
log(LOG_INFO,"Exiting...\n");
if (gw)
delete(gw);
(void) unlink(serial_tty);
closeSyslog();
return status;
}