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 <jian.jun.chen@intel.com>
This commit is contained in:
Jian Jun Chen 2022-08-31 03:18:03 -07:00 committed by acrnsi-robot
parent d459990443
commit 73ffa7d6d2
1 changed files with 18 additions and 17 deletions

View File

@ -22,31 +22,32 @@
/* it read from uart, and if end is '\0' or '\n' or len = buff-len it will return */ /* 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) static ssize_t try_receive_message_by_uart(int fd, void *buffer, size_t buf_len)
{ {
ssize_t rc = 0U, count = 0U; ssize_t rc = 0, count = 0;
char *tmp; char *p = (char *)buffer;
char ch;
unsigned int retry_times = RETRY_RECV_TIMES; unsigned int retry_times = RETRY_RECV_TIMES;
do { while (count < buf_len) {
/* NOTE: Now we can't handle multi command message at one time. */ rc = read(fd, &ch, 1);
rc = read(fd, buffer + count, buf_len - count); if (rc == 1) {
if (rc > 0) { if (ch == (char)(-1)) /* ignore noise data */
count += rc; continue;
tmp = (char *)buffer; if (ch == '\n') /* end of command */
if ((tmp[count - 1] == '\0') || (tmp[count - 1] == '\n') ch = '\0';
|| (count == buf_len)) { p[count++] = ch;
if (tmp[count - 1] == '\n') if (ch == '\0')
tmp[count - 1] = '\0';
break; break;
} } else if ((rc == -1) && (errno == EAGAIN)) {
} else { if (retry_times > 0) {
if (errno == EAGAIN) {
usleep(WAIT_RECV);
retry_times--; retry_times--;
usleep(WAIT_RECV);
} else { } else {
break; break;
} }
} else {
break;
} }
} while (retry_times != 0U); }
return count; return count;
} }