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

Subversion Repositories xulalx25soc

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

Go to most recent revision | 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
                                if ((lb.m_oline[lb.m_olen-1]=='\n')||(lb.m_oline[lb.m_olen-1]=='\r')||(lb.m_olen >= (int)sizeof(lb.m_oline)-1)) {
164
                                        if (lb.m_olen >= (int)sizeof(lb.m_oline)-1)
165
                                                lb.m_oline[lb.m_olen] = '\0';
166
                                        else
167
                                                lb.m_oline[lb.m_olen-1] = '\0';
168
                                        if (lb.m_olen > 1)
169
                                                printf("< %s\n", lb.m_oline);
170
                                        lb.m_olen = 0;
171
                                }
172
                        }
173
                } else if ((nfds>1)&&(p[1].revents)) {
174
                        printf("UNKNOWN SKT EVENT: %d\n", p[1].revents);
175
                }
176
        }
177
 
178
        if (usbp->poll(2)) {
179
                int     nr, nrn;
180
 
181
                nr = nrn = usbp->read(lb.m_buf,1,2);
182
                while((nrn>0)&&(nr<255)&&((lb.m_buf[0]&0x80)==0)&&(lb.m_buf[0]>0x10)) {
183
                        nrn = usbp->read(&lb.m_buf[nr],1,2);
184
                        nr += nrn;
185
                } if ((nr > 0)&&(confd >= 0)) {
186
                        // Return our result if we have a network
187
                        // connection
188
                        write(confd, lb.m_buf, nr);
189
                } for(int i=0; i<nr; i++) {
190
                        lb.m_iline[lb.m_ilen++] = lb.m_buf[i];
191
                        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)) {
192
                                if (lb.m_ilen >= sizeof(lb.m_iline)-1)
193
                                        lb.m_iline[lb.m_ilen] = '\0';
194
                                else
195
                                        lb.m_iline[lb.m_ilen-1] = '\0';
196
                                if (lb.m_ilen > 1)
197
                                        printf("%c %s\n",
198
                                                (confd>=0)?'>':'#', lb.m_iline);
199
                                lb.m_ilen = 0;
200
                        }
201
                }
202
        }
203
 
204
        return (pv > 0);
205
}
206
 
207
int     myaccept(int skt, int timeout) {
208
        int     con = -1;
209
        struct  pollfd  p[1];
210
        int     pv;
211
 
212
        p[0].fd = skt;
213
        p[0].events = POLLIN | POLLERR;
214
        if ((pv=poll(p, 1, timeout)) < 0) {
215
                perror("Poll Failed!  O/S Err:");
216
                exit(-1);
217
        } if (p[0].revents & POLLIN) {
218
                con = accept(skt, 0, 0);
219
                if (con < 0) {
220
                        perror("Accept failed!  O/S Err:");
221
                        exit(-1);
222
                }
223
        } return con;
224
}
225
 
226
int     main(int argc, char **argv) {
227
        // First, accept a network connection
228
        int     skt = setup_listener(FPGAPORT);
229
        USBI    *usbp;
230
        bool    done = false;
231
 
232
        signal(SIGSTOP, sigstop);
233
        signal(SIGBUS, sigbus);
234
        signal(SIGSEGV, sigsegv);
235
        signal(SIGPIPE, SIG_IGN);
236
        signal(SIGINT, sigint);
237
        signal(SIGHUP, sighup);
238
 
239
        usbp = new USBI();
240
 
241
        LINBUFS lb;
242
        while(!done) {
243
                int     con;
244
 
245
                // Accept a connection before going on
246
                // Let's call poll(), so we can still read any
247
                // tty messages even when not accepted
248
                con = myaccept(skt, 50);
249
                if (con >= 0) {
250
                        lb.m_connected = true;
251
 
252
                        /*
253
                        // Set our new socket as non-blocking
254
                        int flags = fcntl(fd, F_GETFL, 0);
255
                        flags |= O_NONBLOCK;
256
                        fcntl(fd, F_SETFL, flags);
257
                        */
258
 
259
                        // printf("Received a new connection\n");
260
                }
261
 
262
                // Flush any buffer within the TTY
263
                while(check_incoming(lb, usbp, -1, 0))
264
                        ;
265
 
266
                // Now, process that connection until it's gone
267
                while(lb.m_connected) {
268
                        check_incoming(lb, usbp, con, -1);
269
                }
270
        }
271
 
272
        printf("Closing our socket\n");
273
        close(skt);
274
}
275
 

powered by: WebSVN 2.1.0

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