diff options
author | Uwe Kleine-König <u.kleine-koenig@pengutronix.de> | 2019-06-14 17:22:55 +0200 |
---|---|---|
committer | Uwe Kleine-König <u.kleine-koenig@pengutronix.de> | 2019-06-14 17:22:55 +0200 |
commit | 5d0b8b4ac73d434160c1b71a7922ad7309082a46 (patch) | |
tree | 90c54aaef5f9726a9e52978af493135cb419f9f5 | |
parent | 141c575ca2cf7e1931807b6601d078e1dc2e8862 (diff) | |
download | microcom-5d0b8b4ac73d434160c1b71a7922ad7309082a46.tar.gz microcom-5d0b8b4ac73d434160c1b71a7922ad7309082a46.tar.xz |
Replace reading and writing to a device by a ios specific callback
This is a restructuring without any intended side effect. The motivation
is that this way telnet handling can go to telnet.c and so only affect
telnet connections. Similar for can this should allow to get rid of the
pipe and pthread stuff.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
-rw-r--r-- | can.c | 12 | ||||
-rw-r--r-- | microcom.h | 2 | ||||
-rw-r--r-- | mux.c | 42 | ||||
-rw-r--r-- | serial.c | 12 | ||||
-rw-r--r-- | telnet.c | 12 |
5 files changed, 75 insertions, 5 deletions
@@ -49,6 +49,16 @@ struct can_data { static struct can_data data; static pthread_t can_thread; +static ssize_t can_write(struct ios_ops *ios, const void *buf, size_t count) +{ + return write(ios->fd, buf, count); +} + +static ssize_t can_read(struct ios_ops *ios, void *buf, size_t count) +{ + return read(ios->fd, buf, count); +} + static int can_set_speed(struct ios_ops *ios, unsigned long speed) { return 0; @@ -154,6 +164,8 @@ struct ios_ops *can_init(char *interface_id) if (!ios) return NULL; + ios->write = can_write; + ios->read = can_read; ios->set_speed = can_set_speed; ios->set_flow = can_set_flow; ios->send_break = can_send_break; @@ -38,6 +38,8 @@ #define DEFAULT_CAN_ID (0x200) struct ios_ops { + ssize_t (*write)(struct ios_ops *, const void *buf, size_t count); + ssize_t (*read)(struct ios_ops *, void *buf, size_t count); int (*set_speed)(struct ios_ops *, unsigned long speed); #define FLOW_NONE 0 #define FLOW_SOFT 1 @@ -20,6 +20,7 @@ #include "microcom.h" #include <arpa/telnet.h> #include <arpa/inet.h> +#include <stdarg.h> #include <stdbool.h> #define BUFSIZE 1024 @@ -219,6 +220,37 @@ static void write_receive_buf(const unsigned char *buf, int len) write(logfd, buf, len); } +static int ios_printf(struct ios_ops *ios, const char *format, ...) +{ + char buf[20]; + int size, written = 0; + ssize_t ret; + va_list args; + + va_start(args, format); + + size = vsnprintf(buf, sizeof(buf), format, args); + + va_end(args); + + if (size >= sizeof(buf)) { + /* truncated output */ + errno = EIO; + return -1; + } + + while (written < size) { + ret = ios->write(ios, buf + written, size - written); + if (ret < 0) + return ret; + + written += ret; + assert(written <= size); + } + + return written; +} + /* This function is called with buf[0] being IAC. */ static int handle_command(struct ios_ops *ios, unsigned char *buf, int len) { @@ -255,7 +287,7 @@ static int handle_command(struct ios_ops *ios, unsigned char *buf, int len) } else { /* unknown/unimplemented option -> DONT */ dbg_printf(" -> DONT\n"); - dprintf(ios->fd, "%c%c%c", IAC, DONT, buf[2]); + ios_printf(ios, "%c%c%c", IAC, DONT, buf[2]); } return 3; @@ -283,7 +315,7 @@ static int handle_command(struct ios_ops *ios, unsigned char *buf, int len) } else { /* Oh, cannot handle that one, so send a WONT */ dbg_printf(" -> WONT\n"); - dprintf(ios->fd, "%c%c%c", IAC, WONT, buf[2]); + ios_printf(ios, "%c%c%c", IAC, WONT, buf[2]); } return 3; @@ -322,7 +354,7 @@ static int handle_receive_buf(struct ios_ops *ios, unsigned char *buf, int len) case 5: write_receive_buf(sendbuf, buf - sendbuf); if (answerback) - write(ios->fd, answerback, strlen(answerback)); + ios->write(ios, answerback, strlen(answerback)); else write_receive_buf(buf, 1); @@ -353,7 +385,7 @@ static void cook_buf(struct ios_ops *ios, unsigned char *buf, int num) current++; /* and write the sequence before esc char to the comm port */ if (current) - write(ios->fd, buf, current); + ios->write(ios, buf, current); if (current < num) { /* process an escape sequence */ /* found an escape character */ @@ -411,7 +443,7 @@ int mux_loop(struct ios_ops *ios) if (FD_ISSET(ios->fd, &ready)) { /* pf has characters for us */ - len = read(ios->fd, buf, BUFSIZE); + len = ios->read(ios, buf, BUFSIZE); if (len < 0) { ret = -errno; fprintf(stderr, "%s\n", strerror(-ret)); @@ -48,6 +48,16 @@ static void init_comm(struct termios *pts) pts->c_iflag &= ~ICRNL; } +static ssize_t serial_write(struct ios_ops *ios, const void *buf, size_t count) +{ + return write(ios->fd, buf, count); +} + +static ssize_t serial_read(struct ios_ops *ios, void *buf, size_t count) +{ + return read(ios->fd, buf, count); +} + static int serial_set_handshake_line(struct ios_ops *ios, int pin, int enable) { int flag; @@ -158,6 +168,8 @@ struct ios_ops * serial_init(char *device) if (!lockfile) return NULL; + ops->write = serial_write; + ops->read = serial_read; ops->set_speed = serial_set_speed; ops->set_flow = serial_set_flow; ops->set_handshake_line = serial_set_handshake_line; @@ -26,6 +26,16 @@ #include "microcom.h" +static ssize_t telnet_write(struct ios_ops *ios, const void *buf, size_t count) +{ + return write(ios->fd, buf, count); +} + +static ssize_t telnet_read(struct ios_ops *ios, void *buf, size_t count) +{ + return read(ios->fd, buf, count); +} + static int telnet_set_speed(struct ios_ops *ios, unsigned long speed) { @@ -92,6 +102,8 @@ struct ios_ops *telnet_init(char *hostport) if (!ios) return NULL; + ios->write = telnet_write; + ios->read = telnet_read; ios->set_speed = telnet_set_speed; ios->set_flow = telnet_set_flow; ios->send_break = telnet_send_break; |