Skip to content

Commit

Permalink
iiod-responder: Handle BINARY command in binary mode
Browse files Browse the repository at this point in the history
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 <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Dec 1, 2023
1 parent 4701693 commit 498f4f1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion iiod-responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit 498f4f1

Please sign in to comment.