examples/buttons: fix strange code in buttons_main

Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2022-02-15 13:20:02 +02:00 committed by Xiang Xiao
parent 020503576c
commit 9a85c405f5

View File

@ -143,7 +143,7 @@ static bool g_button_daemon_started;
static int button_daemon(int argc, char *argv[]) static int button_daemon(int argc, char *argv[])
{ {
#ifdef CONFIG_EXAMPLES_BUTTONS_POLL #ifdef CONFIG_EXAMPLES_BUTTONS_POLL
struct pollfd fds[CONFIG_INPUT_BUTTONS_NPOLLWAITERS]; struct pollfd fds[1];
#endif #endif
#ifdef CONFIG_EXAMPLES_BUTTONS_SIGNAL #ifdef CONFIG_EXAMPLES_BUTTONS_SIGNAL
@ -232,7 +232,6 @@ static int button_daemon(int argc, char *argv[])
#ifdef CONFIG_EXAMPLES_BUTTONS_POLL #ifdef CONFIG_EXAMPLES_BUTTONS_POLL
bool timeout; bool timeout;
bool pollin;
int nbytes; int nbytes;
#endif #endif
@ -256,17 +255,14 @@ static int button_daemon(int argc, char *argv[])
#ifdef CONFIG_EXAMPLES_BUTTONS_POLL #ifdef CONFIG_EXAMPLES_BUTTONS_POLL
/* Prepare the File Descriptor for poll */ /* Prepare the File Descriptor for poll */
memset(fds, 0, memset(fds, 0, sizeof(fds));
sizeof(struct pollfd)*CONFIG_INPUT_BUTTONS_NPOLLWAITERS);
fds[0].fd = fd; fds[0].fd = fd;
fds[0].events = POLLIN; fds[0].events = POLLIN;
timeout = false; timeout = false;
pollin = false;
ret = poll(fds, CONFIG_INPUT_BUTTONS_NPOLLWAITERS, ret = poll(fds, 1, CONFIG_INPUT_BUTTONS_POLL_DELAY);
CONFIG_INPUT_BUTTONS_POLL_DELAY);
printf("\nbutton_daemon: poll returned: %d\n", ret); printf("\nbutton_daemon: poll returned: %d\n", ret);
if (ret < 0) if (ret < 0)
@ -283,34 +279,25 @@ static int button_daemon(int argc, char *argv[])
{ {
printf("button_daemon: ERROR poll reported: %d\n", errno); printf("button_daemon: ERROR poll reported: %d\n", errno);
} }
else
{
pollin = true;
}
/* In any event, read until the pipe is empty */ /* In any event, read until the pipe is empty */
for (i = 0; i < CONFIG_INPUT_BUTTONS_NPOLLWAITERS; i++)
{
do do
{ {
nbytes = read(fds[i].fd, (void *)&sample, nbytes = read(fds[0].fd, (void *)&sample, sizeof(btn_buttonset_t));
sizeof(btn_buttonset_t));
if (nbytes <= 0) if (nbytes <= 0)
{ {
if (nbytes == 0 || errno == EAGAIN) if (nbytes == 0 || errno == EAGAIN)
{ {
if ((fds[i].revents & POLLIN) != 0) if ((fds[0].revents & POLLIN) != 0)
{ {
printf("button_daemon: ERROR no read data[%d]\n", printf("button_daemon: ERROR no read data\n");
i);
} }
} }
else if (errno != EINTR) else if (errno != EINTR)
{ {
printf("button_daemon: read[%d] failed: %d\n", i, printf("button_daemon: read failed: %d\n", errno);
errno);
} }
nbytes = 0; nbytes = 0;
@ -320,7 +307,7 @@ static int button_daemon(int argc, char *argv[])
if (timeout) if (timeout)
{ {
printf("button_daemon: ERROR? Poll timeout, " printf("button_daemon: ERROR? Poll timeout, "
"but data read[%d]\n", i); "but data read\n");
printf(" (might just be a race " printf(" (might just be a race "
"condition)\n"); "condition)\n");
} }
@ -330,10 +317,9 @@ static int button_daemon(int argc, char *argv[])
* through * through
*/ */
fds[i].revents = 0; fds[0].revents = 0;
} }
while (nbytes > 0); while (nbytes > 0);
}
#endif #endif
#ifdef CONFIG_EXAMPLES_BUTTONS_NAMES #ifdef CONFIG_EXAMPLES_BUTTONS_NAMES