OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [netuart.cpp] - Blame information for rev 30

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 dgisselq
#include <stdio.h>
2
#include <stdlib.h>
3
#include <unistd.h>
4
#include <sys/types.h>
5
#include <sys/stat.h>
6
#include <fcntl.h>
7
#include <termios.h>
8
#include <sys/socket.h>
9
#include <arpa/inet.h>
10
#include <string.h>
11
#include <poll.h>
12
#include <signal.h>
13
#include <ctype.h>
14
#include <assert.h>
15
#include <errno.h>
16
 
17
#include "port.h"
18
 
19
void    sigstop(int v) {
20
        fprintf(stderr, "SIGSTOP!!\n");
21
        exit(0);
22
}
23
void    sighup(int v) {
24
        fprintf(stderr, "SIGHUP!!\n");
25
        exit(0);
26
}
27
void    sigint(int v) {
28
        fprintf(stderr, "SIGINT!!\n");
29
        exit(0);
30
}
31
void    sigsegv(int v) {
32
        fprintf(stderr, "SIGSEGV!!\n");
33
        exit(0);
34
}
35
void    sigbus(int v) {
36
        fprintf(stderr, "SIGBUS!!\n");
37
        exit(0);
38
}
39
void    sigpipe(int v) {
40
        fprintf(stderr, "SIGPIPE!!\n");
41
        exit(0);
42
}
43
 
44
int     setup_listener(const int port) {
45
        int     skt;
46
        struct  sockaddr_in     my_addr;
47
 
48
        printf("Listening on port %d\n", port);
49
 
50
        skt = socket(AF_INET, SOCK_STREAM, 0);
51
        if (skt < 0) {
52
                perror("Could not allocate socket: ");
53
                exit(-1);
54
        }
55
 
56
        // Set the reuse address option
57
        {
58
                int optv = 1, er;
59
                er = setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
60
                if (er != 0) {
61
                        perror("SockOpt Err:");
62
                        exit(-1);
63
                }
64
        }
65
 
66
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
67
        my_addr.sin_family = AF_INET;
68
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
69
        my_addr.sin_port = htons(port);
70
 
71
        if (bind(skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
72
                perror("BIND FAILED:");
73
                exit(-1);
74
        }
75
 
76
        if (listen(skt, 1) != 0) {
77
                perror("Listen failed:");
78
                exit(-1);
79
        }
80
 
81
        return skt;
82
}
83
 
84
class   LINBUFS {
85
public:
86
        char    m_iline[512], m_oline[512];
87
        char    m_buf[256];
88
        int     m_ilen, m_olen;
89
        bool    m_connected;
90
 
91
        LINBUFS(void) {
92
                m_ilen = 0; m_olen = 0; m_connected = false;
93
        }
94
};
95
 
96
bool    check_incoming(LINBUFS &lb, int ttyfd, int confd, int timeout) {
97
        struct  pollfd  p[2];
98
        int     pv, nfds;
99
 
100
        p[0].fd = ttyfd;
101
        p[0].events = POLLIN | POLLERR;
102
        if (confd >= 0) {
103
                p[1].fd = confd;
104
                p[1].events = POLLIN | POLLRDHUP | POLLERR;
105
                nfds = 2;
106
        } else nfds = 1;
107
 
108
        if ((pv=poll(p, nfds, timeout)) < 0) {
109
                perror("Poll Failed!  O/S Err:");
110
                exit(-1);
111
        }
112
        if (p[0].revents & POLLIN) {
113
                int nr = read(ttyfd, lb.m_buf, 256);
114
                if (nr > 0) {
115
                        // printf("%d read from TTY\n", nr);
116
                        if (confd >= 0) {
117
                                int     nw;
118
                                nw = write(confd, lb.m_buf, nr);
119
                                if(nw != nr) {
120
                                        // This fails when the other end resets
121
                                        // the connection.  Thus, we'll just
122
                                        // kindly close the connection and skip
123
                                        // the assert that once was at the end.
124
                                        fprintf(stderr, "ERR: Could not write return string to buffer\n");
125
                                        perror("O/S Err:");
126
                                        close(confd);
127
                                        confd = -1;
128
                                        lb.m_connected = false;
129
                                        nfds = 1;
130
                                        // assert(nw == nr);
131
                                }
132
                        }
133
                } for(int i=0; i<nr; i++) {
134
                        lb.m_iline[lb.m_ilen++] = lb.m_buf[i];
135
                        if ((lb.m_iline[lb.m_ilen-1]=='\n')||(lb.m_iline[lb.m_ilen-1]=='\r')||(lb.m_ilen>=sizeof(lb.m_iline)-1)) {
136
                                if (lb.m_ilen >= sizeof(lb.m_iline)-1)
137
                                        lb.m_iline[lb.m_ilen] = '\0';
138
                                else
139
                                        lb.m_iline[lb.m_ilen-1] = '\0';
140
                                if (lb.m_ilen > 1)
141
                                        printf("%c %s\n",
142
                                                (confd>=0)?'>':'#', lb.m_iline);
143
                                lb.m_ilen = 0;
144
                        }
145
                }
146
        } else if (p[0].revents)
147
                printf("UNKNOWN TTY EVENT: %d\n", p[0].revents);
148
 
149
        if((nfds>1)&&(p[1].revents & POLLIN)) {
150
                int nr = read(confd, lb.m_buf, 256);
151
                if (nr == 0) {
152
                        lb.m_connected = false;
153
                        if (lb.m_olen > 0) {
154
                                lb.m_oline[lb.m_olen] = '\0';
155
                                printf("< %s\n", lb.m_oline);
156
                        } lb.m_olen = 0;
157
                        // printf("Disconnect\n");
158
                        close(confd);
159
                } else if (nr > 0) {
160
                        // printf("%d read from SKT\n", nr);
161
                        int nw = 0, ttlw=0;
162
 
163
                        errno = 0;
164
                        do {
165
                                nw = write(ttyfd, &lb.m_buf[ttlw], nr-ttlw);
166
 
167
                                if ((nw < 0)&&(errno == EAGAIN)) {
168
                                        nw = 0;
169
                                        usleep(10);
170
                                } else if (nw < 0) {
171
                                        fprintf(stderr, "ERR: %4d\n", errno);
172
                                        perror("O/S Err: ");
173
                                        assert(nw > 0);
174
                                        break;
175
                                }
176
                                // if (nw != nr-ttlw)
177
                                        // printf("Only wrote %d\n", nw);
178
                                ttlw += nw;
179
                        } while(ttlw < nr);
180
                } for(int i=0; i<nr; i++) {
181
                        lb.m_oline[lb.m_olen++] = lb.m_buf[i];
182
                        assert(lb.m_buf[i] != '\0');
183
                        if ((lb.m_oline[lb.m_olen-1]=='\n')||(lb.m_oline[lb.m_olen-1]=='\r')||(lb.m_olen >= sizeof(lb.m_oline)-1)) {
184
                                if (lb.m_olen >= sizeof(lb.m_oline)-1)
185
                                        lb.m_oline[lb.m_olen] = '\0';
186
                                else
187
                                        lb.m_oline[lb.m_olen-1] = '\0';
188
                                if (lb.m_olen > 1)
189
                                        printf("< %s\n", lb.m_oline);
190
                                lb.m_olen = 0;
191
                        }
192
                }
193
        } else if ((nfds>1)&&(p[1].revents)) {
194
                printf("UNKNOWN SKT EVENT: %d\n", p[1].revents);
195
        }
196
 
197
        return (pv > 0);
198
}
199
 
200
int     myaccept(int skt, int timeout) {
201
        int     con = -1;
202
        struct  pollfd  p[1];
203
        int     pv;
204
 
205
        p[0].fd = skt;
206
        p[0].events = POLLIN | POLLERR;
207
        if ((pv=poll(p, 1, timeout)) < 0) {
208
                perror("Poll Failed!  O/S Err:");
209
                exit(-1);
210
        } if (p[0].revents & POLLIN) {
211
                con = accept(skt, 0, 0);
212
                if (con < 0) {
213
                        perror("Accept failed!  O/S Err:");
214
                        exit(-1);
215
                }
216
        } return con;
217
}
218
 
219
int     main(int argc, char **argv) {
220
        // First, accept a network connection
221
#ifndef LOW_SPEED
222
        int     skt = setup_listener(FPGAPORT);
223
#else
224
        int     skt = setup_listener(FPGAPORT+1);
225
#endif
226
        int     tty;
227
        bool    done = false;
228
 
229
        signal(SIGSTOP, sigstop);
230
        signal(SIGBUS, sigbus);
231
        signal(SIGSEGV, sigsegv);
232
        signal(SIGPIPE, SIG_IGN);
233
        signal(SIGINT, sigint);
234
        signal(SIGHUP, sighup);
235
 
236
        if ((argc > 1)&&(NULL != strstr(argv[1], "/ttyUSB"))) {
237
                // printf("Opening %s\n", argv[1]);
238
                tty = open(argv[1], O_RDWR | O_NONBLOCK);
239
        } else if (argc == 1) {
240
                const   char *deftty = "/dev/ttyUSB2";
241
                // printf("Opening %s\n", deftty);
242
                tty = open(deftty, O_RDWR | O_NONBLOCK);
243
        } else {
244
                printf("Unknown argument: %s\n", argv[1]);
245
                exit(-2);
246
        }
247
 
248
        if (tty < 0) {
249
                printf("Could not open tty\n");
250
                perror("O/S Err:");
251
                exit(-1);
252
        } else if (isatty(tty)) {
253
                struct  termios tb;
254
 
255
                printf("Setting up TTY\n");
256
                if (tcgetattr(tty, &tb) < 0) {
257
                        printf("Could not get TTY attributes\n");
258
                        perror("O/S Err:");
259
                        exit(-2);
260
                }
261
#ifndef LOW_SPEED
262
                // Set 8 bits, 4MBaud, no parity, 1 stop bit
263
                // const char   set_highspeed[] = "00000600000PG00006";
264
                // Set 7 bits, 4MBaud, no parity, 1 stop bit
265
                if (false) {
266
                const char      set_highspeed[] = "0000060G000P";
267
                const char      read_qspic[] = "G0000D";
268
                const char      newline[] = "\n";
269
                ::write(tty, newline, sizeof(newline));
270
                ::write(tty, read_qspic, sizeof(read_qspic));
271
                ::write(tty, set_highspeed, sizeof(set_highspeed));
272
                ::write(tty, newline, sizeof(newline));
273
                printf("< "); fflush(stdout);
274
                ::write(STDOUT_FILENO, read_qspic, sizeof(read_qspic));
275
                ::write(STDOUT_FILENO, set_highspeed, sizeof(set_highspeed));
276
                ::write(STDOUT_FILENO, newline, sizeof(newline));
277
                printf("\n"); usleep(400);
278
                tcdrain(tty);
279
                }
280
#endif
281
 
282
                cfmakeraw(&tb); // Sets no parity, 8 bits, one stop bit
283
                tb.c_cflag &= (~(CRTSCTS)); // Sets no parity, 8 bit
284
                tb.c_cflag &= (~(CSTOPB)); // One stop bit
285 30 dgisselq
// #define      LOW_SPEED
286 4 dgisselq
#ifndef LOW_SPEED
287
                // Switch to 7 bit
288
                tb.c_cflag &= ~(CSIZE);
289
                tb.c_cflag |= CS7;
290
                // And 4 MBaud
291 30 dgisselq
                cfsetispeed(&tb, B1000000);
292
                cfsetospeed(&tb, B1000000);
293 4 dgisselq
#else
294
                // Set the speed to 115200 baud
295
                cfsetispeed(&tb, B115200);
296
                cfsetospeed(&tb, B115200);
297
#endif
298
                if (tcsetattr(tty, TCSANOW, &tb) < 0) {
299
                        printf("Could not set any TTY attributes\n");
300
                        perror("O/S Err:");
301
                }
302
                tcflow(tty, TCOON);
303
        }
304
 
305
        LINBUFS lb;
306
        while(!done) {
307
                int     con;
308
 
309
                // Accept a connection before going on
310
                // Let's call poll(), so we can still read any
311
                // tty messages even when not accepted
312
                con = myaccept(skt, 50);
313
                if (con >= 0) {
314
                        lb.m_connected = true;
315
 
316
                        /*
317
                        // Set our new socket as non-blocking
318
                        int flags = fcntl(fd, F_GETFL, 0);
319
                        flags |= O_NONBLOCK;
320
                        fcntl(fd, F_SETFL, flags);
321
                        */
322
 
323
                        // printf("Received a new connection\n");
324
                }
325
 
326
                // Flush any buffer within the TTY
327
                while(check_incoming(lb, tty, -1, 0))
328
                        ;
329
 
330
                // Now, process that connection until it's gone
331
                while(lb.m_connected) {
332
                        check_incoming(lb, tty, con, -1);
333
                }
334
        }
335
 
336
        printf("Closing our socket\n");
337
        close(skt);
338
}
339
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.