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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [netusb.cpp] - Diff between revs 5 and 25

Only display areas with differences | Details | Blame | View Log

Rev 5 Rev 25
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Filename:    netusb.cpp
// Filename:    netusb.cpp
//
//
// Project:     XuLA2 board
// Project:     XuLA2 board
//
//
// Purpose:     Forwards a XuLA2 board USB connection over a TCP socket, so
// Purpose:     Forwards a XuLA2 board USB connection over a TCP socket, so
//              a non-local computer can control the board.
//              a non-local computer can control the board.
//
//
//
//
// Creator:     Dan Gisselquist, Ph.D.
// Creator:     Dan Gisselquist, Ph.D.
//              Gisselquist Technology, LLC
//              Gisselquist Technology, LLC
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Copyright (C) 2015, Gisselquist Technology, LLC
// Copyright (C) 2015, Gisselquist Technology, LLC
//
//
// This program is free software (firmware): you can redistribute it and/or
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of  the GNU General Public License as published
// modify it under the terms of  the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// your option) any later version.
//
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// for more details.
//
//
// License:     GPL, v3, as defined and found on www.gnu.org,
// License:     GPL, v3, as defined and found on www.gnu.org,
//              http://www.gnu.org/licenses/gpl.html
//              http://www.gnu.org/licenses/gpl.html
//
//
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
//
//
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fcntl.h>
#include <termios.h>
#include <termios.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <string.h>
#include <string.h>
#include <poll.h>
#include <poll.h>
#include <signal.h>
#include <signal.h>
#include <ctype.h>
#include <ctype.h>
#include <assert.h>
#include <assert.h>
#include <errno.h>
#include <errno.h>
 
 
#include "port.h"
#include "port.h"
#include "usbi.h"
#include "usbi.h"
 
 
void    sigstop(int v) {
void    sigstop(int v) {
        fprintf(stderr, "SIGSTOP!!\n");
        fprintf(stderr, "SIGSTOP!!\n");
        exit(0);
        exit(0);
}
}
void    sighup(int v) {
void    sighup(int v) {
        fprintf(stderr, "SIGHUP!!\n");
        fprintf(stderr, "SIGHUP!!\n");
        exit(0);
        exit(0);
}
}
void    sigint(int v) {
void    sigint(int v) {
        fprintf(stderr, "SIGINT!!\n");
        fprintf(stderr, "SIGINT!!\n");
        exit(0);
        exit(0);
}
}
void    sigsegv(int v) {
void    sigsegv(int v) {
        fprintf(stderr, "SIGSEGV!!\n");
        fprintf(stderr, "SIGSEGV!!\n");
        exit(0);
        exit(0);
}
}
void    sigbus(int v) {
void    sigbus(int v) {
        fprintf(stderr, "SIGBUS!!\n");
        fprintf(stderr, "SIGBUS!!\n");
        exit(0);
        exit(0);
}
}
void    sigpipe(int v) {
void    sigpipe(int v) {
        fprintf(stderr, "SIGPIPE!!\n");
        fprintf(stderr, "SIGPIPE!!\n");
        exit(0);
        exit(0);
}
}
 
 
int     setup_listener(const int port) {
int     setup_listener(const int port) {
        int     skt;
        int     skt;
        struct  sockaddr_in     my_addr;
        struct  sockaddr_in     my_addr;
 
 
        printf("Listening on port %d\n", port);
        printf("Listening on port %d\n", port);
 
 
        skt = socket(AF_INET, SOCK_STREAM, 0);
        skt = socket(AF_INET, SOCK_STREAM, 0);
        if (skt < 0) {
        if (skt < 0) {
                perror("Could not allocate socket: ");
                perror("Could not allocate socket: ");
                exit(-1);
                exit(-1);
        }
        }
 
 
        // Set the reuse address option
        // Set the reuse address option
        {
        {
                int optv = 1, er;
                int optv = 1, er;
                er = setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
                er = setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
                if (er != 0) {
                if (er != 0) {
                        perror("SockOpt Err:");
                        perror("SockOpt Err:");
                        exit(-1);
                        exit(-1);
                }
                }
        }
        }
 
 
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
        my_addr.sin_family = AF_INET;
        my_addr.sin_family = AF_INET;
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        my_addr.sin_port = htons(port);
        my_addr.sin_port = htons(port);
 
 
        if (bind(skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
        if (bind(skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
                perror("BIND FAILED:");
                perror("BIND FAILED:");
                exit(-1);
                exit(-1);
        }
        }
 
 
        if (listen(skt, 1) != 0) {
        if (listen(skt, 1) != 0) {
                perror("Listen failed:");
                perror("Listen failed:");
                exit(-1);
                exit(-1);
        }
        }
 
 
        return skt;
        return skt;
}
}
 
 
class   LINBUFS {
class   LINBUFS {
public:
public:
        char    m_iline[512], m_oline[512];
        char    m_iline[512], m_oline[512];
        char    m_buf[256];
        char    m_buf[256];
        int     m_ilen, m_olen;
        int     m_ilen, m_olen;
        bool    m_connected;
        bool    m_connected;
 
 
        LINBUFS(void) {
        LINBUFS(void) {
                m_ilen = 0; m_olen = 0; m_connected = false;
                m_ilen = 0; m_olen = 0; m_connected = false;
        }
        }
};
};
 
 
bool    check_incoming(LINBUFS &lb, USBI *usbp, int confd, int timeout) {
bool    check_incoming(LINBUFS &lb, USBI *usbp, int confd, int timeout) {
        struct  pollfd  p[2];
        struct  pollfd  p[2];
        int     pv, nfds;
        int     pv, nfds;
 
 
        if (confd >= 0) {
        if (confd >= 0) {
                // Only do this if we currently have a network connection
                // Only do this if we currently have a network connection
                p[0].fd = confd;
                p[0].fd = confd;
                p[0].events = POLLIN | POLLRDHUP | POLLERR;
                p[0].events = POLLIN | POLLRDHUP | POLLERR;
                nfds = 1;
                nfds = 1;
 
 
                if ((pv=poll(p, nfds, timeout)) < 0) {
                if ((pv=poll(p, nfds, timeout)) < 0) {
                        perror("Poll Failed!  O/S Err:");
                        perror("Poll Failed!  O/S Err:");
                        exit(-1);
                        exit(-1);
                }
                }
                if (p[0].revents & POLLIN) {
                if (p[0].revents & POLLIN) {
                        /*
                        /*
                         else if (p[0].revents)
                         else if (p[0].revents)
                        printf("UNKNOWN TTY EVENT: %d\n", p[0].revents);
                        printf("UNKNOWN TTY EVENT: %d\n", p[0].revents);
                */
                */
                        int nr = read(confd, lb.m_buf, 256);
                        int nr = read(confd, lb.m_buf, 256);
                        if (nr == 0) {
                        if (nr == 0) {
                                lb.m_connected = false;
                                lb.m_connected = false;
                                if (lb.m_olen > 0) {
                                if (lb.m_olen > 0) {
                                        lb.m_oline[lb.m_olen] = '\0';
                                        lb.m_oline[lb.m_olen] = '\0';
                                        printf("< %s\n", lb.m_oline);
                                        printf("< %s\n", lb.m_oline);
                                } lb.m_olen = 0;
                                } lb.m_olen = 0;
                                close(confd);
                                close(confd);
                        } else if (nr > 0) {
                        } else if (nr > 0) {
                                usbp->write(lb.m_buf, nr);
                                usbp->write(lb.m_buf, nr);
                        } for(int i=0; i<nr; i++) {
                        } for(int i=0; i<nr; i++) {
                                lb.m_oline[lb.m_olen++] = lb.m_buf[i];
                                lb.m_oline[lb.m_olen++] = lb.m_buf[i];
                                assert(lb.m_buf[i] != '\0');
                                assert(lb.m_buf[i] != '\0');
                                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)) {
                                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)) {
                                        if (lb.m_olen >= (int)sizeof(lb.m_oline)-1)
                                        if (lb.m_olen >= (int)sizeof(lb.m_oline)-1)
                                                lb.m_oline[lb.m_olen] = '\0';
                                                lb.m_oline[lb.m_olen] = '\0';
                                        else
                                        else
                                                lb.m_oline[lb.m_olen-1] = '\0';
                                                lb.m_oline[lb.m_olen-1] = '\0';
                                        if (lb.m_olen > 1)
                                        if (lb.m_olen > 1)
                                                printf("< %s\n", lb.m_oline);
                                                printf("< %s\n", lb.m_oline);
                                        lb.m_olen = 0;
                                        lb.m_olen = 0;
                                }
                                }
                        }
                        }
                } else if ((nfds>1)&&(p[1].revents)) {
                } else if ((nfds>1)&&(p[1].revents)) {
                        printf("UNKNOWN SKT EVENT: %d\n", p[1].revents);
                        printf("UNKNOWN SKT EVENT: %d\n", p[1].revents);
                }
                }
        }
        }
 
 
        if (usbp->poll(2)) {
        if (usbp->poll(2)) {
                int     nr, nrn;
                int     nr, nrn;
 
 
                nr = nrn = usbp->read(lb.m_buf,1,2);
                nr = nrn = usbp->read(lb.m_buf,1,2);
                while((nrn>0)&&(nr<255)&&((lb.m_buf[0]&0x80)==0)&&(lb.m_buf[0]>0x10)) {
                while((nrn>0)&&(nr<255)&&((lb.m_buf[0]&0x80)==0)&&(lb.m_buf[0]>0x10)) {
                        nrn = usbp->read(&lb.m_buf[nr],1,2);
                        nrn = usbp->read(&lb.m_buf[nr],1,2);
                        nr += nrn;
                        nr += nrn;
                } if ((nr > 0)&&(confd >= 0)) {
                } if ((nr > 0)&&(confd >= 0)) {
                        // Return our result if we have a network
                        // Return our result if we have a network
                        // connection
                        // connection
                        write(confd, lb.m_buf, nr);
                        write(confd, lb.m_buf, nr);
                } for(int i=0; i<nr; i++) {
                } for(int i=0; i<nr; i++) {
                        lb.m_iline[lb.m_ilen++] = lb.m_buf[i];
                        lb.m_iline[lb.m_ilen++] = lb.m_buf[i];
                        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)) {
                        if ((lb.m_iline[lb.m_ilen-1]=='\n')
                                if (lb.m_ilen >= sizeof(lb.m_iline)-1)
                                ||(lb.m_iline[lb.m_ilen-1]=='\r')
 
                                ||(lb.m_ilen >= (int)sizeof(lb.m_iline)-1)) {
 
                                if (lb.m_ilen >= (int)sizeof(lb.m_iline)-1)
                                        lb.m_iline[lb.m_ilen] = '\0';
                                        lb.m_iline[lb.m_ilen] = '\0';
                                else
                                else
                                        lb.m_iline[lb.m_ilen-1] = '\0';
                                        lb.m_iline[lb.m_ilen-1] = '\0';
                                if (lb.m_ilen > 1)
                                if (lb.m_ilen > 1)
                                        printf("%c %s\n",
                                        printf("%c %s\n",
                                                (confd>=0)?'>':'#', lb.m_iline);
                                                (confd>=0)?'>':'#', lb.m_iline);
                                lb.m_ilen = 0;
                                lb.m_ilen = 0;
                        }
                        }
                }
                }
        }
        }
 
 
        return (pv > 0);
        return (pv > 0);
}
}
 
 
int     myaccept(int skt, int timeout) {
int     myaccept(int skt, int timeout) {
        int     con = -1;
        int     con = -1;
        struct  pollfd  p[1];
        struct  pollfd  p[1];
        int     pv;
        int     pv;
 
 
        p[0].fd = skt;
        p[0].fd = skt;
        p[0].events = POLLIN | POLLERR;
        p[0].events = POLLIN | POLLERR;
        if ((pv=poll(p, 1, timeout)) < 0) {
        if ((pv=poll(p, 1, timeout)) < 0) {
                perror("Poll Failed!  O/S Err:");
                perror("Poll Failed!  O/S Err:");
                exit(-1);
                exit(-1);
        } if (p[0].revents & POLLIN) {
        } if (p[0].revents & POLLIN) {
                con = accept(skt, 0, 0);
                con = accept(skt, 0, 0);
                if (con < 0) {
                if (con < 0) {
                        perror("Accept failed!  O/S Err:");
                        perror("Accept failed!  O/S Err:");
                        exit(-1);
                        exit(-1);
                }
                }
        } return con;
        } return con;
}
}
 
 
int     main(int argc, char **argv) {
int     main(int argc, char **argv) {
        // First, accept a network connection
        // First, accept a network connection
        int     skt = setup_listener(FPGAPORT);
        int     skt = setup_listener(FPGAPORT);
        USBI    *usbp;
        USBI    *usbp;
        bool    done = false;
        bool    done = false;
 
 
        signal(SIGSTOP, sigstop);
        signal(SIGSTOP, sigstop);
        signal(SIGBUS, sigbus);
        signal(SIGBUS, sigbus);
        signal(SIGSEGV, sigsegv);
        signal(SIGSEGV, sigsegv);
        signal(SIGPIPE, SIG_IGN);
        signal(SIGPIPE, SIG_IGN);
        signal(SIGINT, sigint);
        signal(SIGINT, sigint);
        signal(SIGHUP, sighup);
        signal(SIGHUP, sighup);
 
 
        usbp = new USBI();
        usbp = new USBI();
 
 
        LINBUFS lb;
        LINBUFS lb;
        while(!done) {
        while(!done) {
                int     con;
                int     con;
 
 
                // Accept a connection before going on
                // Accept a connection before going on
                // Let's call poll(), so we can still read any
                // Let's call poll(), so we can still read any
                // tty messages even when not accepted
                // tty messages even when not accepted
                con = myaccept(skt, 50);
                con = myaccept(skt, 50);
                if (con >= 0) {
                if (con >= 0) {
                        lb.m_connected = true;
                        lb.m_connected = true;
 
 
                        /*
                        /*
                        // Set our new socket as non-blocking
                        // Set our new socket as non-blocking
                        int flags = fcntl(fd, F_GETFL, 0);
                        int flags = fcntl(fd, F_GETFL, 0);
                        flags |= O_NONBLOCK;
                        flags |= O_NONBLOCK;
                        fcntl(fd, F_SETFL, flags);
                        fcntl(fd, F_SETFL, flags);
                        */
                        */
 
 
                        // printf("Received a new connection\n");
                        // printf("Received a new connection\n");
                }
                }
 
 
                // Flush any buffer within the TTY
                // Flush any buffer within the TTY
                while(check_incoming(lb, usbp, -1, 0))
                while(check_incoming(lb, usbp, -1, 0))
                        ;
                        ;
 
 
                // Now, process that connection until it's gone
                // Now, process that connection until it's gone
                while(lb.m_connected) {
                while(lb.m_connected) {
                        check_incoming(lb, usbp, con, -1);
                        check_incoming(lb, usbp, con, -1);
                }
                }
        }
        }
 
 
        printf("Closing our socket\n");
        printf("Closing our socket\n");
        close(skt);
        close(skt);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

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