From 498f4f19be9c0300fe6ee4820a8aae5a09168c53 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Fri, 1 Dec 2023 11:27:11 +0100 Subject: [PATCH] iiod-responder: Handle BINARY command in binary mode Unlike with the network and USB backends, IIOD is unable to detect when a client disconnected. A new client would then send the BINARY command, which would not be understood by IIOD if it already was in binary mode. Use the convenient fact that the BINARY string is exactly the same size as our command opcodes, and compare each opcode read with the "BINARY\r\n" string. In case of a match, simply send a code 0 to tell the client that we are in binary mode, and continue as usual. Signed-off-by: Paul Cercueil --- iiod-responder.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/iiod-responder.c b/iiod-responder.c index 1bd6e0c63..e571ae51b 100644 --- a/iiod-responder.c +++ b/iiod-responder.c @@ -240,12 +240,14 @@ static int iiod_responder_reader_thrd(void *d) { struct iiod_responder *priv = d; struct iiod_command cmd; - struct iiod_buf cmd_buf; + struct iiod_buf cmd_buf, ok_buf; struct iiod_io *io; ssize_t ret = 0; cmd_buf.ptr = &cmd; cmd_buf.size = sizeof(cmd); + ok_buf.ptr = "0\r\n"; + ok_buf.size = 3; iio_mutex_lock(priv->lock); @@ -254,6 +256,18 @@ static int iiod_responder_reader_thrd(void *d) ret = iiod_rw_all(priv, NULL, &cmd_buf, 1, sizeof(cmd), true); + if (!strncmp((char *)&cmd, "BINARY\r\n", 8)) { + /* If we receive again the "BINARY\r\n" string, send a + * return code of zero and continue as usual. + * This can happen with the serial backend when the + * client disconnects and a new client appears. + * Conveniently, the string is exactly 8 bytes, which is + * the size of a iio_command. */ + + iiod_rw_all(priv, NULL, &ok_buf, 1, ok_buf.size, false); + continue; + } + iio_mutex_lock(priv->lock); if (ret <= 0) break;