From 73ffa7d6d27073c979adfd099cdf3c0ced59efc8 Mon Sep 17 00:00:00 2001 From: Jian Jun Chen Date: Wed, 31 Aug 2022 03:18:03 -0700 Subject: [PATCH] misc: life_mngr: revise try_receive_message_by_uart Revise try_receive_message_by_uart to read one char from uart one time. With this implementation each char can be checked. This can be used to address the following 2 problems: 1) nosie data: it is found that there is noise data in the uart from guest VM when guest startup. 2) split multiple commands Tracked-On: #8111 Signed-off-by: Jian Jun Chen --- misc/services/life_mngr/uart.c | 35 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/misc/services/life_mngr/uart.c b/misc/services/life_mngr/uart.c index 935b0bf55..188704e8b 100644 --- a/misc/services/life_mngr/uart.c +++ b/misc/services/life_mngr/uart.c @@ -22,31 +22,32 @@ /* it read from uart, and if end is '\0' or '\n' or len = buff-len it will return */ static ssize_t try_receive_message_by_uart(int fd, void *buffer, size_t buf_len) { - ssize_t rc = 0U, count = 0U; - char *tmp; + ssize_t rc = 0, count = 0; + char *p = (char *)buffer; + char ch; unsigned int retry_times = RETRY_RECV_TIMES; - do { - /* NOTE: Now we can't handle multi command message at one time. */ - rc = read(fd, buffer + count, buf_len - count); - if (rc > 0) { - count += rc; - tmp = (char *)buffer; - if ((tmp[count - 1] == '\0') || (tmp[count - 1] == '\n') - || (count == buf_len)) { - if (tmp[count - 1] == '\n') - tmp[count - 1] = '\0'; + while (count < buf_len) { + rc = read(fd, &ch, 1); + if (rc == 1) { + if (ch == (char)(-1)) /* ignore noise data */ + continue; + if (ch == '\n') /* end of command */ + ch = '\0'; + p[count++] = ch; + if (ch == '\0') break; - } - } else { - if (errno == EAGAIN) { - usleep(WAIT_RECV); + } else if ((rc == -1) && (errno == EAGAIN)) { + if (retry_times > 0) { retry_times--; + usleep(WAIT_RECV); } else { break; } + } else { + break; } - } while (retry_times != 0U); + } return count; }