Line 1... |
Line 1... |
|
////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Filename: netuart.cpp
|
|
//
|
|
// Project: OpenArty, an entirely open SoC based upon the Arty platform
|
|
//
|
|
// Purpose:
|
|
//
|
|
// Creator: Dan Gisselquist, Ph.D.
|
|
// Gisselquist Technology, LLC
|
|
//
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
|
//
|
|
// 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
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
|
// your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
// for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along
|
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
|
// target there if the PDF file isn't present.) If not, see
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
|
//
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
|
// 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>
|
Line 128... |
Line 166... |
lb.m_connected = false;
|
lb.m_connected = false;
|
nfds = 1;
|
nfds = 1;
|
// assert(nw == nr);
|
// assert(nw == nr);
|
}
|
}
|
}
|
}
|
|
} else if (nr < 0) {
|
|
fprintf(stderr, "ERR: Could not read from TTY\n");
|
|
perror("O/S Err:");
|
|
exit(EXIT_FAILURE);
|
|
} else if (nr == 0) {
|
|
fprintf(stderr, "TTY device has closed\n");
|
|
exit(EXIT_SUCCESS);
|
} 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')
|
|
||((unsigned)lb.m_ilen
|
|
>= sizeof(lb.m_iline)-1)) {
|
|
if ((unsigned)lb.m_ilen >= 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;
|
}
|
}
|
}
|
}
|
} else if (p[0].revents)
|
} else if (p[0].revents) {
|
printf("UNKNOWN TTY EVENT: %d\n", p[0].revents);
|
fprintf(stderr, "ERR: UNKNOWN TTY EVENT: %d\n", p[0].revents);
|
|
perror("O/S Err?");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
|
if((nfds>1)&&(p[1].revents & POLLIN)) {
|
if((nfds>1)&&(p[1].revents & POLLIN)) {
|
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;
|
Line 170... |
Line 222... |
} else if (nw < 0) {
|
} else if (nw < 0) {
|
fprintf(stderr, "ERR: %4d\n", errno);
|
fprintf(stderr, "ERR: %4d\n", errno);
|
perror("O/S Err: ");
|
perror("O/S Err: ");
|
assert(nw > 0);
|
assert(nw > 0);
|
break;
|
break;
|
|
} else if (nw == 0) {
|
|
// TTY device has closed our connection
|
|
fprintf(stderr, "TTY device has closed\n");
|
|
exit(EXIT_SUCCESS);
|
|
break;
|
}
|
}
|
// if (nw != nr-ttlw)
|
// if (nw != nr-ttlw)
|
// printf("Only wrote %d\n", nw);
|
// printf("Only wrote %d\n", nw);
|
ttlw += nw;
|
ttlw += nw;
|
} while(ttlw < nr);
|
} while(ttlw < 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 >= sizeof(lb.m_oline)-1)) {
|
if ((lb.m_oline[lb.m_olen-1]=='\n')
|
if (lb.m_olen >= sizeof(lb.m_oline)-1)
|
||(lb.m_oline[lb.m_olen-1]=='\r')
|
|
||((unsigned)lb.m_olen
|
|
>= sizeof(lb.m_oline)-1)) {
|
|
if ((unsigned)lb.m_olen >= 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);
|
fprintf(stderr, "UNKNOWN SKT EVENT: %d\n", p[1].revents);
|
|
perror("O/S Err?");
|
|
exit(EXIT_FAILURE);
|
}
|
}
|
|
|
return (pv > 0);
|
return (pv > 0);
|
}
|
}
|
|
|
Line 326... |
Line 388... |
// Flush any buffer within the TTY
|
// Flush any buffer within the TTY
|
while(check_incoming(lb, tty, -1, 0))
|
while(check_incoming(lb, tty, -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, tty, con, -1);
|
check_incoming(lb, tty, con, -1);
|
}
|
}
|
}
|
|
|
|
printf("Closing our socket\n");
|
printf("Closing our socket\n");
|
close(skt);
|
close(skt);
|
}
|
}
|
|
|