[PATCH] src/serial.c: Add serial helper read and write function
From: Álvaro Neira Ayuso <anayuso@sysmocom.de> This patch provide two new functions for reading and writing from the serial interface. I think this two functions makes it easy reading and writing from the serial interface. Signed-off-by: Alvaro Neira Ayuso <anayuso@sysmocom.de> --- include/osmocom/core/serial.h | 4 ++++ src/serial.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/osmocom/core/serial.h b/include/osmocom/core/serial.h index 1640a6d..056010b 100644 --- a/include/osmocom/core/serial.h +++ b/include/osmocom/core/serial.h @@ -33,10 +33,14 @@ #include <termios.h> +struct msgb; + int osmo_serial_init(const char *dev, speed_t baudrate); int osmo_serial_set_baudrate(int fd, speed_t baudrate); int osmo_serial_set_custom_baudrate(int fd, int baudrate); int osmo_serial_clear_custom_baudrate(int fd); +int osmo_serial_write(int fd, struct msgb *msg); +int osmo_serial_read(int fd, struct msgb *msg, int numbytes); /*! @} */ diff --git a/src/serial.c b/src/serial.c index 66ee756..818426a 100644 --- a/src/serial.c +++ b/src/serial.c @@ -43,6 +43,7 @@ #endif #include <osmocom/core/serial.h> +#include <osmocom/core/msgb.h> #if 0 @@ -226,4 +227,36 @@ osmo_serial_clear_custom_baudrate(int fd) return 0; } +int +osmo_serial_read(int fd, struct msgb *msg, int numbytes) +{ + int rc, bread = 0; + + while (bread < numbytes) { + rc = read(fd, msg->tail, numbytes - bread); + if (rc < 0) { + msgb_free(msg); + return -1; + } + bread += rc; + msgb_put(msg, rc); + } + + return bread; +} + +int +osmo_serial_write(int fd, struct msgb *msg) +{ + int bwritten = 0, ret = 0; + + bwritten = write(fd, msg->data, msg->len); + if (bwritten < 0 || bwritten < msg->len) { + msgb_free(msg); + ret = -1; + } + + return ret; +} + /*! @} */
Shouldn't this be in libosmocore? 11.02.2014 15:44, Alvaro Neira пишет:
From: Álvaro Neira Ayuso <anayuso@sysmocom.de>
This patch provide two new functions for reading and writing from the serial interface. I think this two functions makes it easy reading and writing from the serial interface.
Signed-off-by: Alvaro Neira Ayuso <anayuso@sysmocom.de> --- include/osmocom/core/serial.h | 4 ++++ src/serial.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
diff --git a/include/osmocom/core/serial.h b/include/osmocom/core/serial.h index 1640a6d..056010b 100644 --- a/include/osmocom/core/serial.h +++ b/include/osmocom/core/serial.h @@ -33,10 +33,14 @@
#include <termios.h>
+struct msgb; + int osmo_serial_init(const char *dev, speed_t baudrate); int osmo_serial_set_baudrate(int fd, speed_t baudrate); int osmo_serial_set_custom_baudrate(int fd, int baudrate); int osmo_serial_clear_custom_baudrate(int fd); +int osmo_serial_write(int fd, struct msgb *msg); +int osmo_serial_read(int fd, struct msgb *msg, int numbytes);
/*! @} */
diff --git a/src/serial.c b/src/serial.c index 66ee756..818426a 100644 --- a/src/serial.c +++ b/src/serial.c @@ -43,6 +43,7 @@ #endif
#include <osmocom/core/serial.h> +#include <osmocom/core/msgb.h>
#if 0 @@ -226,4 +227,36 @@ osmo_serial_clear_custom_baudrate(int fd) return 0; }
+int +osmo_serial_read(int fd, struct msgb *msg, int numbytes) +{ + int rc, bread = 0; + + while (bread < numbytes) { + rc = read(fd, msg->tail, numbytes - bread); + if (rc < 0) { + msgb_free(msg); + return -1; + } + bread += rc; + msgb_put(msg, rc); + } + + return bread; +} + +int +osmo_serial_write(int fd, struct msgb *msg) +{ + int bwritten = 0, ret = 0; + + bwritten = write(fd, msg->data, msg->len); + if (bwritten < 0 || bwritten < msg->len) { + msgb_free(msg); + ret = -1; + } + + return ret; +} + /*! @} */
-- best regards, Max, http://fairwaves.ru
El 11/02/14 16:06, ☎ escribió:
Shouldn't this be in libosmocore?
Yes sorry, i have forgot to put it. Thanks Max
11.02.2014 15:44, Alvaro Neira пишет:
From: Álvaro Neira Ayuso <anayuso@sysmocom.de>
This patch provide two new functions for reading and writing from the serial interface. I think this two functions makes it easy reading and writing from the serial interface.
Signed-off-by: Alvaro Neira Ayuso <anayuso@sysmocom.de> --- include/osmocom/core/serial.h | 4 ++++ src/serial.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
diff --git a/include/osmocom/core/serial.h b/include/osmocom/core/serial.h index 1640a6d..056010b 100644 --- a/include/osmocom/core/serial.h +++ b/include/osmocom/core/serial.h @@ -33,10 +33,14 @@
#include <termios.h>
+struct msgb; + int osmo_serial_init(const char *dev, speed_t baudrate); int osmo_serial_set_baudrate(int fd, speed_t baudrate); int osmo_serial_set_custom_baudrate(int fd, int baudrate); int osmo_serial_clear_custom_baudrate(int fd); +int osmo_serial_write(int fd, struct msgb *msg); +int osmo_serial_read(int fd, struct msgb *msg, int numbytes);
/*! @} */
diff --git a/src/serial.c b/src/serial.c index 66ee756..818426a 100644 --- a/src/serial.c +++ b/src/serial.c @@ -43,6 +43,7 @@ #endif
#include <osmocom/core/serial.h> +#include <osmocom/core/msgb.h>
#if 0 @@ -226,4 +227,36 @@ osmo_serial_clear_custom_baudrate(int fd) return 0; }
+int +osmo_serial_read(int fd, struct msgb *msg, int numbytes) +{ + int rc, bread = 0; + + while (bread < numbytes) { + rc = read(fd, msg->tail, numbytes - bread); + if (rc < 0) { + msgb_free(msg); + return -1; + } + bread += rc; + msgb_put(msg, rc); + } + + return bread; +} + +int +osmo_serial_write(int fd, struct msgb *msg) +{ + int bwritten = 0, ret = 0; + + bwritten = write(fd, msg->data, msg->len); + if (bwritten < 0 || bwritten < msg->len) { + msgb_free(msg); + ret = -1; + } + + return ret; +} + /*! @} */
Alvaro Neira wrote:
+++ b/src/serial.c . +int +osmo_serial_read(int fd, struct msgb *msg, int numbytes) +{ + int rc, bread = 0;
This is a nice place to add a bounds check..
+ while (bread < numbytes) { + rc = read(fd, msg->tail, numbytes - bread);
..so that there is guaranteed room in the msgb for the requested number of bytes.
+ if (rc < 0) { + msgb_free(msg); + return -1; + }
Are you sure that these functions should destroy the msgb on errors? Also: What about the bytes which have already been read, in case of error? The proposed code discards those bytes. Is that OK? (I don't know the answer to this; that's why I'm asking.)
+int +osmo_serial_write(int fd, struct msgb *msg) +{ + int bwritten = 0, ret = 0; + + bwritten = write(fd, msg->data, msg->len); + if (bwritten < 0 || bwritten < msg->len) {
This is incorrect. Both read() and write() may return less than 'count' bytes. You'll need to loop here too to ensure that all data has been written to the port.
+ msgb_free(msg);
Same question as with the _read function - should this function destroy the msgb on errors?
+ ret = -1;
I would like the functions to use similar flow. My personal preference for how to handle the return value is the way you use return in the _read function, I find that much nicer than the ret variable in the _write function. But more importantly I think the two should be consistent where possible. //Peter
participants (4)
-
Alvaro Neira -
Peter Stuge -
Álvaro Neira Ayuso -
☎