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 */
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;
}