Skip to content

Commit

Permalink
iiod: Update read_line() to work with UART
Browse files Browse the repository at this point in the history
Previously, in the non-socket case, read_line() would read as much data
as possible in one read() call. As it was used almost exclusively for
USB, the amount of data returned always corresponded to the size of the bulk
data sent by the client, aka. a full line and nothing more.

With UART though, if we try reading as much data as possible, we might
get just that, and end up reading much more data than we need,
disrupting the protocol.

Fix this by reading character by character until the \n is found in the
non-socket, non-USB case.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Oct 1, 2021
1 parent 5595f1b commit aad53e3
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions iiod/ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -1397,12 +1397,12 @@ int set_buffers_count(struct parser_pdata *pdata,

ssize_t read_line(struct parser_pdata *pdata, char *buf, size_t len)
{
size_t bytes_read = 0;
ssize_t ret;
bool found;

if (pdata->fd_in_is_socket) {
struct pollfd pfd[2];
bool found;
size_t bytes_read = 0;

pfd[0].fd = pdata->fd_in;
pfd[0].events = POLLIN | POLLRDHUP;
Expand Down Expand Up @@ -1445,16 +1445,34 @@ ssize_t read_line(struct parser_pdata *pdata, char *buf, size_t len)

bytes_read += to_trunc;
} while (!found && len);
} else if (pdata->is_usb) {
ret = pdata->readfd(pdata, buf, len);

/* No \n found? Just garbage data */
if (!found)
ret = -EIO;
else
ret = bytes_read;
found = buf[ret - 1] == '\n';
} else {
ret = pdata->readfd(pdata, buf, len);
while (len) {
ret = pdata->readfd(pdata, buf, 1);
if (ret < 0)
return ret;

bytes_read++;

if (*buf == '\n')
break;

len--;
buf++;
}

found = !!len;
}

/* No \n found? Just garbage data */
if (!found)
ret = -EIO;
else
ret = bytes_read;

return ret;
}

Expand Down

0 comments on commit aad53e3

Please sign in to comment.