forked from mysensors/Raspberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PiGatewaySerial.cpp
186 lines (159 loc) · 4.01 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
/*
* 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 <RF24.h>
#include <RF24ComVUsb.h>
#include <MyGateway.h>
#include <Version.h>
/* variable indicating if the server is still running */
volatile static int running = 1;
/* PTY file descriptors */
int pty_master = -1;
int pty_slave = -1;
RF24ComVUsb com_dev;
RF24Frontend rf24_device(com_dev);
/*
* handler for SIGINT signal
*/
void handle_sigint(int sig)
{
printf("Received SIGINT\n");
running = 0;
}
/*
* callback function writting data from RF24 module to the PTY
*/
void write_msg_to_pty(char *msg)
{
size_t len = 0;
if (msg == NULL)
{
printf("[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);
}
/*
* Main gateway logic
*/
int main(int argc, char **argv)
{
static const char *serial_tty = "/dev/ttyMySensorsGateway";
struct pollfd fds;
MyGateway *gw = NULL;
int status = EXIT_SUCCESS;
int ret;
printf("Starting PiGatewaySerial...\n");
printf("Protocol version - %s\n", LIBRARY_VERSION);
/* register the signal handler */
signal(SIGINT, handle_sigint);
/* create MySensors Gateway object */
gw = new MyGateway(rf24_device, 60);
if (gw == NULL)
{
printf("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)
{
printf("Could not create a PTY! (%d) %s\n", errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
printf("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)
{
printf("Could not create a symlink '%s' to PTY! (%d) %s\n", serial_tty, errno, strerror(errno));
status = EXIT_FAILURE;
goto cleanup;
}
printf("Gateway tty: %s\n", serial_tty);
close(pty_slave);
configure_master_fd(pty_master);
fds.events = POLLRDNORM;
fds.fd = pty_master;
/* 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)
{
printf("poll() error (%d) %s\n", errno, strerror(errno));
}
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)
{
printf("read error (%d) %s\n", errno, strerror(errno));
continue;
}
buff[size] = '\0';
gw->parseAndSend(buff);
}
}
}
cleanup:
printf("Exiting...\n");
//if (gw)
// delete(gw);
(void) unlink(serial_tty);
return status;
}