-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsynccat.c
185 lines (165 loc) · 4.15 KB
/
synccat.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
/*
* $Id: synccat.c,v 1.3 2006/06/18 00:44:51 sampo Exp $
*
* Simple main program to call dialout.c
*
* 28/4/2006 -- [email protected]
* 17.6.2006, Sampo Kellomaki ([email protected])
*/
/*#include "config.h"*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <syslog.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#ifdef HAVE_NET_SNMP
# include "snmpInterface.h"
#endif
#include "serial_sync.h"
#include "myconfig.h"
#include "errmac.h"
#define SUBAGENTNAME "open5066"
#define SNMPLOGFILE "/var/tmp/snmpOpen5066.log"
#define MYDEVICE "/dev/se_hdlc1"
#define MYFRAMESIZE 1000
#define MYBAUDRATE 11520
char* instance = "s5066d/serial_sync";
int debug = 1;
int continueRunning = 1;
void stop_server(int a);
int main(int argc, char **argv) {
int fd, n, bytes_in, bytes_out, return_value = 0,
result, snmpfd, maxFD, block = 0;
fd_set input;
struct timeval timeout;
char buffer[MAXBUFFER+1];
signal(SIGTERM, stop_server);
signal(SIGINT, stop_server);
printf("opening device %s\n", MYDEVICE);
fd = open_port(MYDEVICE); /* should check for -1 in a lot of places*/
maxFD = fd + 1;
/* write_port(fd, MYDEVICE,"Hello World!\n", strlen("Hello World!\n"));*/
FD_ZERO(&input);
#if HAVE_NET_SNMP
initializeSNMPSubagent(SUBAGENTNAME, SNMPLOGFILE);
#endif
log_port_info(fd, MYDEVICE, "before");
set_baud_rate(fd, MYDEVICE, MYBAUDRATE);
set_frame_size(fd, MYDEVICE, MYFRAMESIZE);
set_serial_opts(fd, MYDEVICE);
log_port_info(fd, MYDEVICE, "after");
while (continueRunning) {
FD_SET(fd, &input);
FD_SET(STDIN_FILENO, &input);
timeout.tv_sec = 20;
timeout.tv_usec = 0;
#if HAVE_NET_SNMP
/* NetSNMP is a bit fascist in this sense ... */
n = selectAndUpdateSNMPFDSET(&maxFD, &input, &timeout);
timeout.tv_sec = 20;
timeout.tv_usec = 0;
#endif
n = select(maxFD, &input, NULL, NULL, &timeout);
if (n < 0) {
perror("select failed");
return_value = 1;
break;
}
if (n == 0) {
puts("TIMEOUT");
return_value = 2;
break;
}
if (FD_ISSET(fd, &input)) {
if (!processSerial(fd, MYDEVICE)) {
return_value = 3;
break;
}
} else if (FD_ISSET(STDIN_FILENO, &input)) {
if (!processStdin(fd, MYDEVICE)) {
return_value = 4;
break;
}
} else {
#if HAVE_NET_SNMP
/* Not sure how to check that there were no errors... */
processSNMP(&input);
#else
puts("We should never get here");
#endif
}
#if HAVE_NET_SNMP
/* Send snmp alarms */
otherSNMPTasks();
#endif
}
log_port_info(fd, MYDEVICE, "closing");
#if HAVE_NET_SNMP
snmp_shutdown("snmpOpen5066");
#endif
close_port(fd, MYDEVICE);
return return_value;
}
/*
* processStdin
*
* Input:
* - filedescriptor of the serial port
* - String with the name of the serial device
* Output:
* - 1 if no errors
* - 0 if errors exist
*/
int processStdin(int fd, const char *device) {
char buffer[MAXBUFFER+1];
int bytes_read, bytes_written;
bytes_read = read(STDIN_FILENO, buffer, MAXBUFFER);
if (bytes_read == -1) {
perror("Error reading STDIN...");
return 0;
}
bytes_written = 0;
while (bytes_written < bytes_read) {
bytes_written += write_port(fd, MYDEVICE, buffer + bytes_written, bytes_read - bytes_written);
if (bytes_written == -1) {
perror("Error writing to serial port...");
return 0;
}
buffer[bytes_written] = '\0';
D("Read from stdin and written to fd %d port %s: <%s>",
fd, device, buffer);
}
return 1;
}
/*
* processSerial
*
* Input:
* - filedescriptor of the serial port
* - String with the name of the serial device
* Output:
* - 1 if no errors
* - 0 if errors exist
*/
int processSerial(int fd, const char *device) {
char buffer[MAXBUFFER+1];
int bytes_read, bytes_written;
bytes_read = read_port(fd, device, buffer, MAXBUFFER);
if (bytes_read == -1) {
perror("Error reading serial port...");
return 0;
}
buffer[bytes_read] = '\0';
D("Read from fd %d port %s: <%s>",
fd, device, buffer);
printf("%s", buffer);
return 1;
}
void stop_server(int a) {
continueRunning = 0;
INFO("Server gracefully stopped", "");
}