Rizin
unix-like reverse engineering framework and cli tools
socket_serial.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_socket.h>
5 #if __UNIX__
6 
7 #include <errno.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <termios.h>
11 
12 static int set_interface_attribs(int fd, int speed, int parity) {
13  struct termios tty;
14  memset(&tty, 0, sizeof tty);
15  if (tcgetattr(fd, &tty) != 0) {
16  return -1;
17  }
18  cfsetospeed(&tty, speed);
19  cfsetispeed(&tty, speed);
20 
21  tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
22  // disable IGNBRK for mismatched speed tests; otherwise receive break
23  // as \000 chars
24  tty.c_iflag &= ~IGNBRK; // disable break processing
25  tty.c_lflag = 0; // no signaling chars, no echo,
26  // no canonical processing
27  tty.c_oflag = 0; // no remapping, no delays
28  tty.c_cc[VMIN] = 0; // read doesn't block
29  tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
30 
31  tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
32 
33  tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
34  // enable reading
35  tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
36  tty.c_cflag |= parity;
37  tty.c_cflag &= ~CSTOPB;
38 #ifdef CRTSCTS
39  tty.c_cflag &= ~CRTSCTS;
40 #endif
41 
42  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
43  return -1;
44  }
45  return 0;
46 }
47 
48 RZ_API int rz_socket_connect_serial(RzSocket *sock, const char *path, int speed, int parity) {
49  int fd = open(path, O_RDWR | O_SYNC | O_BINARY, 0); // O_NOCTY
50  if (fd == -1) {
51  return -1;
52  }
53  if (speed < 1) {
54  speed = 9600; // 19200
55  }
56  (void)set_interface_attribs(fd, speed, parity);
57  sock->fd = fd;
58  return fd;
59 }
60 
61 #else // __UNIX__
62 
63 RZ_API int rz_socket_connect_serial(RzSocket *sock, const char *path, int speed, int parity) {
64  return -1;
65 }
66 #endif
#define O_BINARY
Definition: cpipe.c:13
#define RZ_API
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
return memset(p, 0, total)
#define CS8
Definition: sftypes.h:942
#define CLOCAL
Definition: sftypes.h:948
#define IGNBRK
Definition: sftypes.h:870
#define PARODD
Definition: sftypes.h:946
#define VMIN
Definition: sftypes.h:857
#define CSTOPB
Definition: sftypes.h:943
#define CRTSCTS
Definition: sftypes.h:967
#define VTIME
Definition: sftypes.h:856
#define IXON
Definition: sftypes.h:880
#define TCSANOW
Definition: sftypes.h:998
#define CREAD
Definition: sftypes.h:944
#define O_SYNC
Definition: sftypes.h:496
#define O_RDWR
Definition: sftypes.h:488
#define PARENB
Definition: sftypes.h:945
#define IXANY
Definition: sftypes.h:881
#define CSIZE
Definition: sftypes.h:938
#define IXOFF
Definition: sftypes.h:882
RZ_API int rz_socket_connect_serial(RzSocket *sock, const char *path, int speed, int parity)
Definition: socket_serial.c:63
uv_tty_t tty
Definition: main.c:7
static const z80_opcode fd[]
Definition: z80_tab.h:997