From 4869509c7bb8b70117d6c59bccd40ec41956a1ef Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Fri, 13 Sep 2019 15:29:49 +0200 Subject: [PATCH] add write function to netsource this is useful for listening sockets to also send responses, i.e. implement bi-directional communication --- lib/include/srslte/phy/io/netsource.h | 2 ++ lib/src/phy/io/netsource.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/include/srslte/phy/io/netsource.h b/lib/include/srslte/phy/io/netsource.h index 24eeef85e..5ce3d29e5 100644 --- a/lib/include/srslte/phy/io/netsource.h +++ b/lib/include/srslte/phy/io/netsource.h @@ -67,6 +67,8 @@ SRSLTE_API int srslte_netsource_read(srslte_netsource_t *q, void *buffer, int nof_bytes); +SRSLTE_API int srslte_netsource_write(srslte_netsource_t* q, void* buffer, int nbytes); + SRSLTE_API int srslte_netsource_set_timeout(srslte_netsource_t *q, uint32_t microseconds); diff --git a/lib/src/phy/io/netsource.c b/lib/src/phy/io/netsource.c index 1cb779eed..139860129 100644 --- a/lib/src/phy/io/netsource.c +++ b/lib/src/phy/io/netsource.c @@ -111,6 +111,22 @@ int srslte_netsource_read(srslte_netsource_t *q, void *buffer, int nbytes) { } } +int srslte_netsource_write(srslte_netsource_t* q, void* buffer, int nbytes) +{ + // Loop until all bytes are sent + char* ptr = (char*)buffer; + while (nbytes > 0) { + ssize_t i = send(q->connfd, ptr, nbytes, 0); + if (i < 1) { + perror("Error calling send()\n"); + return SRSLTE_ERROR; + } + ptr += i; + nbytes -= i; + } + return SRSLTE_SUCCESS; +} + int srslte_netsource_set_nonblocking(srslte_netsource_t *q) { if (fcntl(q->sockfd, F_SETFL, O_NONBLOCK)) { perror("fcntl");