/* SPDX-License-Identifier: GPL-2.0 */ #define _GNU_SOURCE 1 #include #include #include #include #include #include #include #include #include #include #define PPP_DEV "/dev/ppp" int main(int argc, char *argv[]) { int tty_fd, ppp_ch_fd, ppp_unit_fd; int ppp_disc = N_PPP; int ch_idx; int unit = -1; int flags; struct termios termios; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } /* 1. tty */ tty_fd = open(argv[1], O_RDWR); if (tty_fd < 0) { perror("open(tty_fd)"); return 1; } /* set exclusive mode */ if (ioctl(tty_fd, TIOCEXCL, 0) < 0 && errno != EIO) { perror("ioctl(tty_fd, TIOCEXCL)"); return 1; } /* tty setting */ if (tcgetattr(tty_fd, &termios) < 0) { perror("tcgetattr(tty_fd)"); return 1; } /* now hardware flow control */ termios.c_cflag &= ~CRTSCTS; /* increase speed */ cfsetspeed(&termios, B460800); /* applay changed values */ if (tcsetattr(tty_fd, TCSANOW, &termios) < 0) { perror("tcsetattr(tty_fd)"); return 1; } /* set tty to ppp line discipline */ if (ioctl(tty_fd, TIOCSETD, &ppp_disc)) { perror("ioctl(tty_fd, TIOCSETD)"); return 1; } /* get channel index */ if (ioctl(tty_fd, PPPIOCGCHAN, &ch_idx) < 0) { perror("ioctl(tty_fd, PPPIOCGCHAN)"); return 1; } /* 2. ppp channel */ ppp_ch_fd = open(PPP_DEV, O_RDWR); if (ppp_ch_fd < 0) { perror("open(ppp_ch_fd)"); return 1; } /* connect attach tty to channel */ if (ioctl(ppp_ch_fd, PPPIOCATTCHAN, &ch_idx) < 0) { perror("ioctl(ppp_ch_fd, PPPIOCATTCHAN)"); return 1; } /* 3. ppp unit */ ppp_unit_fd = open(PPP_DEV, O_RDWR); if (ppp_unit_fd < 0) { perror("open(ppp_unit_fd)"); return 1; } if (ioctl(ppp_unit_fd, PPPIOCNEWUNIT, &unit) < 0) { perror("ioctl(ppp_unit_fd, PPPIOCNEWUNIT)"); return 1; } /* connect ppp channel to ppp unit */ if (ioctl(ppp_ch_fd, PPPIOCCONNECT, &unit) < 0) { perror("ioctl(ppp_ch_fd, PPPIOCATTCHAN)"); return 1; } for (;;) { uint8_t buf[2048]; long i; long len = read(ppp_unit_fd, buf, sizeof(buf)); if (len < 0) break; printf("rx: len %ld", len); for (i = 0; i < len; i++) { switch (i % 16) { case 0: printf("\n"); break; case 4: case 8: case 12: printf(" "); break; } printf("%02x", buf[i]); } printf("\n"); } return 0; }