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

Subversion Repositories openarty

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    netuart.cpp
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     
8
//
9
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
15
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
 
39 4 dgisselq
#include <stdio.h>
40
#include <stdlib.h>
41
#include <unistd.h>
42
#include <sys/types.h>
43
#include <sys/stat.h>
44
#include <fcntl.h>
45
#include <termios.h>
46
#include <sys/socket.h>
47
#include <arpa/inet.h>
48
#include <string.h>
49
#include <poll.h>
50
#include <signal.h>
51
#include <ctype.h>
52
#include <assert.h>
53
#include <errno.h>
54
 
55
#include "port.h"
56
 
57
void    sigstop(int v) {
58
        fprintf(stderr, "SIGSTOP!!\n");
59
        exit(0);
60
}
61
void    sighup(int v) {
62
        fprintf(stderr, "SIGHUP!!\n");
63
        exit(0);
64
}
65
void    sigint(int v) {
66
        fprintf(stderr, "SIGINT!!\n");
67
        exit(0);
68
}
69
void    sigsegv(int v) {
70
        fprintf(stderr, "SIGSEGV!!\n");
71
        exit(0);
72
}
73
void    sigbus(int v) {
74
        fprintf(stderr, "SIGBUS!!\n");
75
        exit(0);
76
}
77
void    sigpipe(int v) {
78
        fprintf(stderr, "SIGPIPE!!\n");
79
        exit(0);
80
}
81
 
82
int     setup_listener(const int port) {
83
        int     skt;
84
        struct  sockaddr_in     my_addr;
85
 
86
        printf("Listening on port %d\n", port);
87
 
88
        skt = socket(AF_INET, SOCK_STREAM, 0);
89
        if (skt < 0) {
90
                perror("Could not allocate socket: ");
91
                exit(-1);
92
        }
93
 
94
        // Set the reuse address option
95
        {
96
                int optv = 1, er;
97
                er = setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
98
                if (er != 0) {
99
                        perror("SockOpt Err:");
100
                        exit(-1);
101
                }
102
        }
103
 
104
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
105
        my_addr.sin_family = AF_INET;
106
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
107
        my_addr.sin_port = htons(port);
108
 
109
        if (bind(skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
110
                perror("BIND FAILED:");
111
                exit(-1);
112
        }
113
 
114
        if (listen(skt, 1) != 0) {
115
                perror("Listen failed:");
116
                exit(-1);
117
        }
118
 
119
        return skt;
120
}
121
 
122
class   LINBUFS {
123
public:
124
        char    m_iline[512], m_oline[512];
125
        char    m_buf[256];
126
        int     m_ilen, m_olen;
127
        bool    m_connected;
128
 
129
        LINBUFS(void) {
130
                m_ilen = 0; m_olen = 0; m_connected = false;
131
        }
132
};
133
 
134
bool    check_incoming(LINBUFS &lb, int ttyfd, int confd, int timeout) {
135
        struct  pollfd  p[2];
136
        int     pv, nfds;
137
 
138
        p[0].fd = ttyfd;
139
        p[0].events = POLLIN | POLLERR;
140
        if (confd >= 0) {
141
                p[1].fd = confd;
142
                p[1].events = POLLIN | POLLRDHUP | POLLERR;
143
                nfds = 2;
144
        } else nfds = 1;
145
 
146
        if ((pv=poll(p, nfds, timeout)) < 0) {
147
                perror("Poll Failed!  O/S Err:");
148
                exit(-1);
149
        }
150
        if (p[0].revents & POLLIN) {
151
                int nr = read(ttyfd, lb.m_buf, 256);
152
                if (nr > 0) {
153
                        // printf("%d read from TTY\n", nr);
154
                        if (confd >= 0) {
155
                                int     nw;
156
                                nw = write(confd, lb.m_buf, nr);
157
                                if(nw != nr) {
158
                                        // This fails when the other end resets
159
                                        // the connection.  Thus, we'll just
160
                                        // kindly close the connection and skip
161
                                        // the assert that once was at the end.
162
                                        fprintf(stderr, "ERR: Could not write return string to buffer\n");
163
                                        perror("O/S Err:");
164
                                        close(confd);
165
                                        confd = -1;
166
                                        lb.m_connected = false;
167
                                        nfds = 1;
168
                                        // assert(nw == nr);
169
                                }
170
                        }
171 51 dgisselq
                } else if (nr < 0) {
172
                        fprintf(stderr, "ERR: Could not read from TTY\n");
173
                        perror("O/S Err:");
174
                        exit(EXIT_FAILURE);
175
                } else if (nr == 0) {
176
                        fprintf(stderr, "TTY device has closed\n");
177
                        exit(EXIT_SUCCESS);
178 4 dgisselq
                } for(int i=0; i<nr; i++) {
179
                        lb.m_iline[lb.m_ilen++] = lb.m_buf[i];
180 51 dgisselq
                        if ((lb.m_iline[lb.m_ilen-1]=='\n')
181
                                        ||(lb.m_iline[lb.m_ilen-1]=='\r')
182
                                        ||((unsigned)lb.m_ilen
183
                                                >= sizeof(lb.m_iline)-1)) {
184
                                if ((unsigned)lb.m_ilen >= sizeof(lb.m_iline)-1)
185 4 dgisselq
                                        lb.m_iline[lb.m_ilen] = '\0';
186
                                else
187
                                        lb.m_iline[lb.m_ilen-1] = '\0';
188
                                if (lb.m_ilen > 1)
189
                                        printf("%c %s\n",
190
                                                (confd>=0)?'>':'#', lb.m_iline);
191
                                lb.m_ilen = 0;
192
                        }
193
                }
194 51 dgisselq
        } else if (p[0].revents) {
195
                fprintf(stderr, "ERR: UNKNOWN TTY EVENT: %d\n", p[0].revents);
196
                perror("O/S Err?");
197
                exit(EXIT_FAILURE);
198
        }
199
 
200 4 dgisselq
 
201
        if((nfds>1)&&(p[1].revents & POLLIN)) {
202
                int nr = read(confd, lb.m_buf, 256);
203
                if (nr == 0) {
204
                        lb.m_connected = false;
205
                        if (lb.m_olen > 0) {
206
                                lb.m_oline[lb.m_olen] = '\0';
207
                                printf("< %s\n", lb.m_oline);
208
                        } lb.m_olen = 0;
209
                        // printf("Disconnect\n");
210
                        close(confd);
211
                } else if (nr > 0) {
212
                        // printf("%d read from SKT\n", nr);
213
                        int nw = 0, ttlw=0;
214
 
215
                        errno = 0;
216
                        do {
217
                                nw = write(ttyfd, &lb.m_buf[ttlw], nr-ttlw);
218
 
219
                                if ((nw < 0)&&(errno == EAGAIN)) {
220
                                        nw = 0;
221
                                        usleep(10);
222
                                } else if (nw < 0) {
223
                                        fprintf(stderr, "ERR: %4d\n", errno);
224
                                        perror("O/S Err: ");
225
                                        assert(nw > 0);
226
                                        break;
227 51 dgisselq
                                } else if (nw == 0) {
228
                                        // TTY device has closed our connection
229
                                        fprintf(stderr, "TTY device has closed\n");
230
                                        exit(EXIT_SUCCESS);
231
                                        break;
232 4 dgisselq
                                }
233
                                // if (nw != nr-ttlw)
234
                                        // printf("Only wrote %d\n", nw);
235
                                ttlw += nw;
236
                        } while(ttlw < nr);
237
                } for(int i=0; i<nr; i++) {
238
                        lb.m_oline[lb.m_olen++] = lb.m_buf[i];
239
                        assert(lb.m_buf[i] != '\0');
240 51 dgisselq
                        if ((lb.m_oline[lb.m_olen-1]=='\n')
241
                                        ||(lb.m_oline[lb.m_olen-1]=='\r')
242
                                        ||((unsigned)lb.m_olen
243
                                                >= sizeof(lb.m_oline)-1)) {
244
                                if ((unsigned)lb.m_olen >= sizeof(lb.m_oline)-1)
245 4 dgisselq
                                        lb.m_oline[lb.m_olen] = '\0';
246
                                else
247
                                        lb.m_oline[lb.m_olen-1] = '\0';
248
                                if (lb.m_olen > 1)
249
                                        printf("< %s\n", lb.m_oline);
250
                                lb.m_olen = 0;
251
                        }
252
                }
253
        } else if ((nfds>1)&&(p[1].revents)) {
254 51 dgisselq
                fprintf(stderr, "UNKNOWN SKT EVENT: %d\n", p[1].revents);
255
                perror("O/S Err?");
256
                exit(EXIT_FAILURE);
257 4 dgisselq
        }
258
 
259
        return (pv > 0);
260
}
261
 
262
int     myaccept(int skt, int timeout) {
263
        int     con = -1;
264
        struct  pollfd  p[1];
265
        int     pv;
266
 
267
        p[0].fd = skt;
268
        p[0].events = POLLIN | POLLERR;
269
        if ((pv=poll(p, 1, timeout)) < 0) {
270
                perror("Poll Failed!  O/S Err:");
271
                exit(-1);
272
        } if (p[0].revents & POLLIN) {
273
                con = accept(skt, 0, 0);
274
                if (con < 0) {
275
                        perror("Accept failed!  O/S Err:");
276
                        exit(-1);
277
                }
278
        } return con;
279
}
280
 
281
int     main(int argc, char **argv) {
282
        // First, accept a network connection
283
#ifndef LOW_SPEED
284
        int     skt = setup_listener(FPGAPORT);
285
#else
286
        int     skt = setup_listener(FPGAPORT+1);
287
#endif
288
        int     tty;
289
        bool    done = false;
290
 
291
        signal(SIGSTOP, sigstop);
292
        signal(SIGBUS, sigbus);
293
        signal(SIGSEGV, sigsegv);
294
        signal(SIGPIPE, SIG_IGN);
295
        signal(SIGINT, sigint);
296
        signal(SIGHUP, sighup);
297
 
298
        if ((argc > 1)&&(NULL != strstr(argv[1], "/ttyUSB"))) {
299
                // printf("Opening %s\n", argv[1]);
300
                tty = open(argv[1], O_RDWR | O_NONBLOCK);
301
        } else if (argc == 1) {
302
                const   char *deftty = "/dev/ttyUSB2";
303
                // printf("Opening %s\n", deftty);
304
                tty = open(deftty, O_RDWR | O_NONBLOCK);
305
        } else {
306
                printf("Unknown argument: %s\n", argv[1]);
307
                exit(-2);
308
        }
309
 
310
        if (tty < 0) {
311
                printf("Could not open tty\n");
312
                perror("O/S Err:");
313
                exit(-1);
314
        } else if (isatty(tty)) {
315
                struct  termios tb;
316
 
317
                printf("Setting up TTY\n");
318
                if (tcgetattr(tty, &tb) < 0) {
319
                        printf("Could not get TTY attributes\n");
320
                        perror("O/S Err:");
321
                        exit(-2);
322
                }
323
#ifndef LOW_SPEED
324
                // Set 8 bits, 4MBaud, no parity, 1 stop bit
325
                // const char   set_highspeed[] = "00000600000PG00006";
326
                // Set 7 bits, 4MBaud, no parity, 1 stop bit
327
                if (false) {
328
                const char      set_highspeed[] = "0000060G000P";
329
                const char      read_qspic[] = "G0000D";
330
                const char      newline[] = "\n";
331
                ::write(tty, newline, sizeof(newline));
332
                ::write(tty, read_qspic, sizeof(read_qspic));
333
                ::write(tty, set_highspeed, sizeof(set_highspeed));
334
                ::write(tty, newline, sizeof(newline));
335
                printf("< "); fflush(stdout);
336
                ::write(STDOUT_FILENO, read_qspic, sizeof(read_qspic));
337
                ::write(STDOUT_FILENO, set_highspeed, sizeof(set_highspeed));
338
                ::write(STDOUT_FILENO, newline, sizeof(newline));
339
                printf("\n"); usleep(400);
340
                tcdrain(tty);
341
                }
342
#endif
343
 
344
                cfmakeraw(&tb); // Sets no parity, 8 bits, one stop bit
345
                tb.c_cflag &= (~(CRTSCTS)); // Sets no parity, 8 bit
346
                tb.c_cflag &= (~(CSTOPB)); // One stop bit
347 30 dgisselq
// #define      LOW_SPEED
348 4 dgisselq
#ifndef LOW_SPEED
349
                // Switch to 7 bit
350
                tb.c_cflag &= ~(CSIZE);
351
                tb.c_cflag |= CS7;
352
                // And 4 MBaud
353 30 dgisselq
                cfsetispeed(&tb, B1000000);
354
                cfsetospeed(&tb, B1000000);
355 4 dgisselq
#else
356
                // Set the speed to 115200 baud
357
                cfsetispeed(&tb, B115200);
358
                cfsetospeed(&tb, B115200);
359
#endif
360
                if (tcsetattr(tty, TCSANOW, &tb) < 0) {
361
                        printf("Could not set any TTY attributes\n");
362
                        perror("O/S Err:");
363
                }
364
                tcflow(tty, TCOON);
365
        }
366
 
367
        LINBUFS lb;
368
        while(!done) {
369
                int     con;
370
 
371
                // Accept a connection before going on
372
                // Let's call poll(), so we can still read any
373
                // tty messages even when not accepted
374
                con = myaccept(skt, 50);
375
                if (con >= 0) {
376
                        lb.m_connected = true;
377
 
378
                        /*
379
                        // Set our new socket as non-blocking
380
                        int flags = fcntl(fd, F_GETFL, 0);
381
                        flags |= O_NONBLOCK;
382
                        fcntl(fd, F_SETFL, flags);
383
                        */
384
 
385
                        // printf("Received a new connection\n");
386
                }
387
 
388
                // Flush any buffer within the TTY
389
                while(check_incoming(lb, tty, -1, 0))
390
                        ;
391
 
392
                // Now, process that connection until it's gone
393 51 dgisselq
                while(lb.m_connected)
394 4 dgisselq
                        check_incoming(lb, tty, con, -1);
395
        }
396
 
397
        printf("Closing our socket\n");
398
        close(skt);
399
}
400
 

powered by: WebSVN 2.1.0

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