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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [netusb.cpp] - Blame information for rev 25

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    netusb.cpp
4
//
5
// Project:     XuLA2 board
6
//
7
// Purpose:     Forwards a XuLA2 board USB connection over a TCP socket, so
8
//              a non-local computer can control the board.
9
//
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// License:     GPL, v3, as defined and found on www.gnu.org,
29
//              http://www.gnu.org/licenses/gpl.html
30
//
31
//
32
////////////////////////////////////////////////////////////////////////////////
33
//
34
//
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <sys/types.h>
39
#include <sys/stat.h>
40
#include <fcntl.h>
41
#include <termios.h>
42
#include <sys/socket.h>
43
#include <arpa/inet.h>
44
#include <string.h>
45
#include <poll.h>
46
#include <signal.h>
47
#include <ctype.h>
48
#include <assert.h>
49
#include <errno.h>
50
 
51
#include "port.h"
52
#include "usbi.h"
53
 
54
void    sigstop(int v) {
55
        fprintf(stderr, "SIGSTOP!!\n");
56
        exit(0);
57
}
58
void    sighup(int v) {
59
        fprintf(stderr, "SIGHUP!!\n");
60
        exit(0);
61
}
62
void    sigint(int v) {
63
        fprintf(stderr, "SIGINT!!\n");
64
        exit(0);
65
}
66
void    sigsegv(int v) {
67
        fprintf(stderr, "SIGSEGV!!\n");
68
        exit(0);
69
}
70
void    sigbus(int v) {
71
        fprintf(stderr, "SIGBUS!!\n");
72
        exit(0);
73
}
74
void    sigpipe(int v) {
75
        fprintf(stderr, "SIGPIPE!!\n");
76
        exit(0);
77
}
78
 
79
int     setup_listener(const int port) {
80
        int     skt;
81
        struct  sockaddr_in     my_addr;
82
 
83
        printf("Listening on port %d\n", port);
84
 
85
        skt = socket(AF_INET, SOCK_STREAM, 0);
86
        if (skt < 0) {
87
                perror("Could not allocate socket: ");
88
                exit(-1);
89
        }
90
 
91
        // Set the reuse address option
92
        {
93
                int optv = 1, er;
94
                er = setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
95
                if (er != 0) {
96
                        perror("SockOpt Err:");
97
                        exit(-1);
98
                }
99
        }
100
 
101
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
102
        my_addr.sin_family = AF_INET;
103
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
104
        my_addr.sin_port = htons(port);
105
 
106
        if (bind(skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
107
                perror("BIND FAILED:");
108
                exit(-1);
109
        }
110
 
111
        if (listen(skt, 1) != 0) {
112
                perror("Listen failed:");
113
                exit(-1);
114
        }
115
 
116
        return skt;
117
}
118
 
119
class   LINBUFS {
120
public:
121
        char    m_iline[512], m_oline[512];
122
        char    m_buf[256];
123
        int     m_ilen, m_olen;
124
        bool    m_connected;
125
 
126
        LINBUFS(void) {
127
                m_ilen = 0; m_olen = 0; m_connected = false;
128
        }
129
};
130
 
131
bool    check_incoming(LINBUFS &lb, USBI *usbp, int confd, int timeout) {
132
        struct  pollfd  p[2];
133
        int     pv, nfds;
134
 
135
        if (confd >= 0) {
136
                // Only do this if we currently have a network connection
137
                p[0].fd = confd;
138
                p[0].events = POLLIN | POLLRDHUP | POLLERR;
139
                nfds = 1;
140
 
141
                if ((pv=poll(p, nfds, timeout)) < 0) {
142
                        perror("Poll Failed!  O/S Err:");
143
                        exit(-1);
144
                }
145
                if (p[0].revents & POLLIN) {
146
                        /*
147
                         else if (p[0].revents)
148
                        printf("UNKNOWN TTY EVENT: %d\n", p[0].revents);
149
                */
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
                                close(confd);
158
                        } else if (nr > 0) {
159
                                usbp->write(lb.m_buf, nr);
160
                        } for(int i=0; i<nr; i++) {
161
                                lb.m_oline[lb.m_olen++] = lb.m_buf[i];
162
                                assert(lb.m_buf[i] != '\0');
163 25 dgisselq
                                if ((lb.m_oline[lb.m_olen-1]=='\n')
164
                                        ||(lb.m_oline[lb.m_olen-1]=='\r')
165
                                        ||(lb.m_olen >= (int)sizeof(lb.m_oline)-1)) {
166 5 dgisselq
                                        if (lb.m_olen >= (int)sizeof(lb.m_oline)-1)
167
                                                lb.m_oline[lb.m_olen] = '\0';
168
                                        else
169
                                                lb.m_oline[lb.m_olen-1] = '\0';
170
                                        if (lb.m_olen > 1)
171
                                                printf("< %s\n", lb.m_oline);
172
                                        lb.m_olen = 0;
173
                                }
174
                        }
175
                } else if ((nfds>1)&&(p[1].revents)) {
176
                        printf("UNKNOWN SKT EVENT: %d\n", p[1].revents);
177
                }
178
        }
179
 
180
        if (usbp->poll(2)) {
181
                int     nr, nrn;
182
 
183
                nr = nrn = usbp->read(lb.m_buf,1,2);
184
                while((nrn>0)&&(nr<255)&&((lb.m_buf[0]&0x80)==0)&&(lb.m_buf[0]>0x10)) {
185
                        nrn = usbp->read(&lb.m_buf[nr],1,2);
186
                        nr += nrn;
187
                } if ((nr > 0)&&(confd >= 0)) {
188
                        // Return our result if we have a network
189
                        // connection
190
                        write(confd, lb.m_buf, nr);
191
                } for(int i=0; i<nr; i++) {
192
                        lb.m_iline[lb.m_ilen++] = lb.m_buf[i];
193 25 dgisselq
                        if ((lb.m_iline[lb.m_ilen-1]=='\n')
194
                                ||(lb.m_iline[lb.m_ilen-1]=='\r')
195
                                ||(lb.m_ilen >= (int)sizeof(lb.m_iline)-1)) {
196
                                if (lb.m_ilen >= (int)sizeof(lb.m_iline)-1)
197 5 dgisselq
                                        lb.m_iline[lb.m_ilen] = '\0';
198
                                else
199
                                        lb.m_iline[lb.m_ilen-1] = '\0';
200
                                if (lb.m_ilen > 1)
201
                                        printf("%c %s\n",
202
                                                (confd>=0)?'>':'#', lb.m_iline);
203
                                lb.m_ilen = 0;
204
                        }
205
                }
206
        }
207
 
208
        return (pv > 0);
209
}
210
 
211
int     myaccept(int skt, int timeout) {
212
        int     con = -1;
213
        struct  pollfd  p[1];
214
        int     pv;
215
 
216
        p[0].fd = skt;
217
        p[0].events = POLLIN | POLLERR;
218
        if ((pv=poll(p, 1, timeout)) < 0) {
219
                perror("Poll Failed!  O/S Err:");
220
                exit(-1);
221
        } if (p[0].revents & POLLIN) {
222
                con = accept(skt, 0, 0);
223
                if (con < 0) {
224
                        perror("Accept failed!  O/S Err:");
225
                        exit(-1);
226
                }
227
        } return con;
228
}
229
 
230
int     main(int argc, char **argv) {
231
        // First, accept a network connection
232
        int     skt = setup_listener(FPGAPORT);
233
        USBI    *usbp;
234
        bool    done = false;
235
 
236
        signal(SIGSTOP, sigstop);
237
        signal(SIGBUS, sigbus);
238
        signal(SIGSEGV, sigsegv);
239
        signal(SIGPIPE, SIG_IGN);
240
        signal(SIGINT, sigint);
241
        signal(SIGHUP, sighup);
242
 
243
        usbp = new USBI();
244
 
245
        LINBUFS lb;
246
        while(!done) {
247
                int     con;
248
 
249
                // Accept a connection before going on
250
                // Let's call poll(), so we can still read any
251
                // tty messages even when not accepted
252
                con = myaccept(skt, 50);
253
                if (con >= 0) {
254
                        lb.m_connected = true;
255
 
256
                        /*
257
                        // Set our new socket as non-blocking
258
                        int flags = fcntl(fd, F_GETFL, 0);
259
                        flags |= O_NONBLOCK;
260
                        fcntl(fd, F_SETFL, flags);
261
                        */
262
 
263
                        // printf("Received a new connection\n");
264
                }
265
 
266
                // Flush any buffer within the TTY
267
                while(check_incoming(lb, usbp, -1, 0))
268
                        ;
269
 
270
                // Now, process that connection until it's gone
271
                while(lb.m_connected) {
272
                        check_incoming(lb, usbp, con, -1);
273
                }
274
        }
275
 
276
        printf("Closing our socket\n");
277
        close(skt);
278
}
279
 

powered by: WebSVN 2.1.0

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