-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
405 lines (333 loc) · 10.3 KB
/
main.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*****************************************************************************
* udp_server: Control EIWOMISA over udp
*****************************************************************************
* Copyright (C) 2009-2011 Kai Hermann
*
* Authors: Kai Hermann <kai.uwe.hermann at gmail dot com>
*
* 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.
*****************************************************************************/
#include "git_rev.h"
#define VERSION "0.4"
#define PROGNAME "eiwomisarc_server"
#define COPYRIGHT "2009-2011, Kai Hermann"
#define BUFFSIZE 6
/* UDP & other includes */
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
/* RS-232 */
#include <fcntl.h> /* file control definitions */
#include <errno.h> /* error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/* message functions */
#include "messages.h"
/* argtable */
#include "argtable2/argtable2.h"
/* signal handling */
#include <signal.h>
/* global serial port */
int global_serialport = -1;
/* signal handler */
void sigfunc(int sig) {
if(global_serialport != -1) {
close(global_serialport);
}
printf("\n");
exit (0);
}
/* check for valid baudrate
* throw _warning_ if baudrate is exotic */
int check_baudrate(int pBaud)
{
switch (pBaud) {
case B0: break;
case B50: break;
case B75: break;
case B110: break;
case B134: break;
case B150: break;
case B200: break;
case B300: break;
case B600: break;
case B1200: break;
case B1800: break;
case B2400: break;
case B4800: break;
case B9600: break;
case B19200: break;
case B38400: break;
default:
msg_Dbg("BAUDRATE seems to be a little bit exotic\nbut hey, you'll know what you are doing ;)");
break;
}
return 0;
}
/* open serial port
* returns the file descriptor on success or -1 on error */
int open_port(const char *pPort, int pBaud)
{
/* serial port file descriptor */
int fd = open(pPort, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) {
msg_Err("Unable to open serial-port");
msg_Info("Retrying in 5 seconds...");
sleep(5);
fd = open(pPort, O_RDWR | O_NOCTTY | O_NDELAY);
}
if (fd == -1) {
die("Unable to open serial-port");
} else {
fcntl(fd, F_SETFL, 0);
struct termios options;
/* get the current options for the port */
tcgetattr(fd, &options);
check_baudrate(pBaud);
/* set baudrate */
cfsetispeed(&options, pBaud);
cfsetospeed(&options, pBaud);
/* enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
/* set the new options for the port */
tcsetattr(fd, TCSANOW, &options);
msg_Dbg("BAUDRATE SET TO %i",pBaud);
}
return (fd);
}
/* check if buffer is valid */
int checkbuffer (unsigned char *buffer) {
int error = 0;
/* byte0: startbyte = 255 */
if(buffer[0] == 255) {
msg_Dbg("buffer[0] == 255");
} else {
error = 1;
msg_Dbg("buffer[0] != 255");
}
/* byte1: value part 1/2*/
if(buffer[1] < 255) {
msg_Dbg("buffer[1] <255");
} else {
error = 1;
msg_Dbg("buffer[1] >= 255");
}
/* byte2: value part 2/2 */
if(buffer[2] < 2) {
msg_Dbg("buffer[2] <2");
} else {
error = 1;
msg_Dbg("buffer[2] >= 2");
}
/* byte3: channel part 1/3 */
if(buffer[3] < 255) {
msg_Dbg("buffer[3] <255");
} else {
error = 1;
msg_Dbg("buffer[3] >= 255");
}
/* byte4: channel part 2/3 */
if(buffer[4] < 255) {
msg_Dbg("buffer[4] <255");
} else {
error = 1;
msg_Dbg("buffer[4] >= 255");
}
/* byte5: channel part 3/3 */
if(buffer[5] < 5) {
msg_Dbg("buffer[5] <5");
} else {
error = 1;
msg_Dbg("buffer[5] >= 255");
}
return error;
}
/* check if IP address is provided in a valid format,
if ip is invalid, use localhost. */
in_addr_t check_ip(char *pIp)
{
in_addr_t ip;
if(inet_pton(AF_INET, pIp, &ip) < 1) {
msg_Err("Incorrect client IP provided, using 127.0.0.1");
inet_pton(AF_INET, "127.0.0.1", &ip);
}
return ip;
}
/* mainloop */
int mymain(int port, char *serialport, int baud, char *validip)
{
/* check if port, serialport and baudrate are set, otherwise use defaults */
if (port == -1) {
msg_Info("No Port set - using 1337");
port = 1337;
}
if (serialport == NULL) {
msg_Info("No Serialport set - using /dev/ttyS0");
serialport = "/dev/ttyS0";
}
if (baud == -1) {
msg_Info("No Baudrate set - using 9600");
baud = 9600;
}
int sock;
struct sockaddr_in server;
struct sockaddr_in client;
unsigned char buffer[BUFFSIZE];
unsigned int clientlen, serverlen;
int received = 0;
/* create the UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
die("Failed to create socket\n");
}
/* construct the server sockaddr_in structure */
memset(&server, 0, sizeof(server)); /* Clear struct */
server.sin_family = AF_INET; /* Internet/IP */
server.sin_addr.s_addr = htonl(INADDR_ANY); /* Any IP address */
server.sin_port = htons(port); /* server port */
/* bind the socket */
serverlen = sizeof(server);
if (bind(sock, (struct sockaddr *) &server, serverlen) < 0) {
die("Failed to bind server socket\n");
}
/* signal handler */
signal(SIGTERM,sigfunc);
signal(SIGINT,sigfunc);
/* open serial port */
global_serialport = open_port(serialport, baud);
/* wait for UDP-packets */
while (42) {
/* Receive a message from the client */
clientlen = sizeof(client);
if ((received = recvfrom(sock, buffer, BUFFSIZE, 0,
(struct sockaddr *) &client,
&clientlen)) < 0) {
die("Failed to receive message\n");
}
if(validip != NULL && client.sin_addr.s_addr != check_ip(validip)) {
msg_Info("Wrong client tried to connect to server: %s", inet_ntoa(client.sin_addr));
} else {
msg_Info("Client connected: %s", inet_ntoa(client.sin_addr));
if (checkbuffer(buffer) == 0) {
msg_Dbg("buffer0-5: '%s'", buffer);
/* RS-232 Code start */
int n = write(global_serialport, buffer, BUFFSIZE);
if (n < 0) {
msg_Err("write() failed!");
} else {
msg_Dbg("Value(s) written to serial port");
}
/* RS-232 Code end */
}
}
}
/* close serial port */
close(global_serialport);
return 0;
}
int main(int argc, char **argv)
{
struct arg_int *serverport = arg_int0("pP","port","","serverport, default: 1337");
struct arg_str *serialport = arg_str0("sS", "serial", "", "serial port, default /dev/ttyS0");
struct arg_int *baud = arg_int0("bB", "baud","","baudrate, default: 9600");
struct arg_str *client = arg_str0("cC","client","","only accept messages from this client");
struct arg_lit *help = arg_lit0("hH","help","print this help and exit");
struct arg_lit *version = arg_lit0(NULL,"version","print version information and exit");
struct arg_lit *debug = arg_lit0(NULL,"debug","print debug messages");
struct arg_lit *silent = arg_lit0(NULL,"silent","print no messages");
struct arg_end *end = arg_end(20);
void* argtable[] = {serverport,serialport,baud,client,help,version,debug,silent,end};
int nerrors;
int exitcode=0;
/* verify the argtable[] entries were allocated sucessfully */
if (arg_nullcheck(argtable) != 0) {
/* NULL entries were detected, some allocations must have failed */
printf("%s: insufficient memory\n",PROGNAME);
exitcode=1;
goto exit;
}
/* set any command line default values prior to parsing */
/* nothing */
/* Parse the command line as defined by argtable[] */
nerrors = arg_parse(argc,argv,argtable);
/* special case: '--help' takes precedence over error reporting */
if (help->count > 0) {
printf("usage: %s", PROGNAME);
arg_print_syntax(stdout,argtable,"\n");
arg_print_glossary(stdout,argtable," %-25s %s\n");
exitcode=0;
goto exit;
}
/* special case: '--version' takes precedence error reporting */
if (version->count > 0) {
printf("'%s' version ",PROGNAME);
printf("%s",VERSION);
printf("\nGIT-REVISION: ");
printf("%s",GITREV);
printf("\n%s receives udp-packets and controls\n",PROGNAME);
printf("the EIWOMISA controller over RS-232\n");
printf("%s",COPYRIGHT);
printf("\n");
exitcode=0;
goto exit;
}
/* If the parser returned any errors then display them and exit */
if (nerrors > 0) {
/* Display the error details contained in the arg_end struct.*/
arg_print_errors(stdout,end,PROGNAME);
printf("Try '%s --help' for more information.\n",PROGNAME);
exitcode=1;
goto exit;
}
/* special case: with no command line options induce brief help and use defaults */
if (argc==1) {
printf("No command-line options present, using defaults.\n",PROGNAME);
printf("Try '%s --help' for more information.\n",PROGNAME);
}
/* normal case: take the command line options at face value */
/* check if server port is set */
int i_serverport = -1;
if(serverport->count>0)
i_serverport = (int)serverport->ival[0];
/* check if serial port is set */
char* i_serialport = NULL;
if(serialport->count>0)
i_serialport = (char*)serialport->sval[0];
/* check if baudrate is set */
int i_baudrate = -1;
if(baud->count>0)
i_baudrate = (int)baud->ival[0];
/* check if client ip is set */
char* i_client = NULL;
if(client->count>0) {
i_client = (char *)client->sval[0];
}
/* --debug enables debug messages */
if (debug->count > 0) {
printf("debug messages enabled\n");
msglevel = 3;
}
/* --silent disables all (!) messages */
if (silent->count > 0) {
msglevel = 0;
}
exitcode = mymain(i_serverport, i_serialport, i_baudrate, i_client);
exit:
/* deallocate each non-null entry in argtable[] */
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return exitcode;
}