4 #include "usbdev_utils.h"
16 #include "stream_test.h"
20 static void report_error(
const char *rsn) {
21 std::cerr <<
"ERROR: " << rsn <<
": " << strerror(errno) <<
" (error "
22 << errno <<
")" << std::endl;
26 int port_open(
const char *dev_name,
bool write) {
27 const char *port_type = write ?
"output" :
"input";
28 int fd = open(dev_name, write ? O_WRONLY : O_RDONLY);
30 std::cerr <<
"ERROR: Could not open " << port_type <<
" port '" << dev_name
39 if (tcgetattr(fd, &tty) != 0) {
40 report_error(
"Failed getting terminal attributes");
46 tty.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
47 tty.c_cflag |= CS8 | CREAD | CLOCAL;
50 tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG);
53 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
54 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
57 tty.c_oflag &= ~(OPOST | ONLCR);
65 cfsetispeed(&tty, B4000000);
66 cfsetospeed(&tty, B4000000);
69 if (tcsetattr(fd, TCSANOW, &tty) != 0) {
70 report_error(
"Failed setting terminal attributes");
79 ssize_t recv_bytes(
int in, uint8_t *buf,
size_t len) {
83 ssize_t n = read(in, buf, len);
85 printf(
"Received %zd byte(s)\n", n);
86 for (
int idx = 0; idx < n; idx++) {
87 printf(
"0x%02x\n", buf[idx]);
92 report_error(
"Failed to read from input port");
104 ssize_t send_bytes(
int out,
const uint8_t *data,
size_t len) {
105 ssize_t nwritten = 0;
108 ssize_t n = write(out, data, len);
110 report_error(
"Failed to write to output port");
123 uint64_t time_us(
void) {
125 int ret = gettimeofday(&ts, NULL);
128 return ((uint64_t)ts.tv_sec * 1000000u) + ts.tv_usec;
132 void buffer_dump(FILE *out,
const uint8_t *data,
size_t n) {
133 static const char hex_digits[] =
"0123456789abcdef";
134 const unsigned ncols = 0x20u;
135 char buf[ncols * 4u + 2u];
138 const unsigned chunk = (n > ncols) ? ncols : (
unsigned)n;
139 const uint8_t *row = data;
144 while (idx < chunk) {
145 dp[0] = hex_digits[row[idx] >> 4];
146 dp[1] = hex_digits[row[idx++] & 0xfu];
150 while (idx++ < ncols) {
151 dp[2] = dp[1] = dp[0] =
' ';
156 for (idx = 0u; idx < chunk; idx++) {
157 uint8_t ch = row[idx];
158 *dp++ = (ch < ' ' || ch >= 0x80u) ?
'.' : ch;
161 fprintf(out,
"%s\n", buf);
170 extern uint64_t elapsed_time(uint64_t start);
171 extern uint16_t get_le16(
const uint8_t *p);