[PATCH] src/serial.c: Add serial helper read and write function

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/OpenBSC@lists.osmocom.org/.

Alvaro Neira alvaroneay at gmail.com
Tue Feb 11 14:44:21 UTC 2014


From: Álvaro Neira Ayuso <anayuso at 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 at 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;
+}
+
 /*! @} */





More information about the OpenBSC mailing list