poll机制监听多个event

/**
 * @brief powermanagement process with file descriptor
 *
 */
static int powermanagement_process(void)
{
    const struct bagad_cli_cbs cbs = { powermanagement_notify_conn_status, powermanagement_handle_msg };
    struct bagad_cli *bagad_cli;
    //TODO Unix SOCKET
/*    struct sockaddr sa_addr = {0};
    struct sockaddr_in *addr_in;*/
    struct user_data userdata;
    struct pollfd fds[3] = {0};
    struct itimerspec nval;
    int fd_timer = -1;
    int ret;

    powermanagement_log_time("process");
    ULOGW("powermanagement process");

    signal(SIGINT, &sig_handler);
    signal(SIGTERM, &sig_handler);

    sys_prop_set(PROP_PWCONTROL_BOOTCOMPLETED, "0");
    sys_prop_set(PROP_BACKLIGHT_READY, "0");
    sys_prop_set(PROP_GMSL2_SERIALIZER_READY, "0");
    sys_prop_set(PROP_BYPARTLABEL_LINK_READY, "0");
    sys_prop_set(PROP_TOUCH_INPUT_READY, "0");

    //TODO Unix SOCKET
/*    addr_in = (struct sockaddr_in *)&sa_addr;
    addr_in->sin_family = AF_INET;
    addr_in->sin_addr.s_addr = inet_addr(LOCAL_IP);
    addr_in->sin_port = htons(LOCAL_PORT);
*/
    //TODO Unix SOCKET
/*    bagad_cli = bagad_cli_new(NULL, &cbs, &sa_addr, sizeof(sa_addr), &userdata);
*/
    bagad_cli = bagad_cli_new(NULL, &cbs, NULL, 0, &userdata);
    userdata.bagad_cli = bagad_cli;
    ULOGD("bagad_cli_new: %p", bagad_cli);

    ret = pipe(fd_pipes);
    fds[0].fd = fd_pipes[0];
    fds[0].events = POLLIN;
    ULOGD("pipe fd: %d", fds[0].fd);

    fds[1].fd = bagad_cli_get_fd(bagad_cli);
    fds[1].events = POLLIN;
    ULOGD("bagad_cli_get_fd: %d", fds[0].fd);

    fd_timer = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
    nval.it_value.tv_sec  = 0;
    nval.it_value.tv_nsec = 50000000; //50ms
    nval.it_interval.tv_sec  = nval.it_value.tv_sec;
    nval.it_interval.tv_nsec = nval.it_value.tv_nsec;
    ret = timerfd_settime(fd_timer, 0, &nval, NULL);
    fds[2].fd = fd_timer;
    fds[2].events = POLLIN;
    ULOGD("timer fd: %d", fds[2].fd);

    bypartlabel_link_load_config();

    for (;;)
    {
        do
        {
            ret = poll(fds, sizeof(fds) / sizeof(fds[0]), -1);
        }
        while (ret < 0 && errno == EINTR);

        if (ret < 0)
        {
            ULOGE("poll error=%d(%s)", errno, strerror(errno));
            for (uint16_t i=0; i<(sizeof(fds) / sizeof(fds[0])); i++)
            {
                ULOGD("poll fds %d, revents %i", i, fds[i].revents);
            }
        }

        // signal fd
        if (fds[0].revents != 0)
        {
            ULOGD("signal poll revents %d, exit", fds[0].revents);
            break;
        }

        // bagad fd
        if (fds[1].revents != 0)
        {
            ULOGD("bagad poll revents %d", fds[1].revents);
            bagad_cli_process_fd(bagad_cli);
        }

        // timer fd
        if (fds[2].revents != 0)
        {
            //TODO debug: ULOGD("timer poll revents %d", fds[2].revents);

            ret = timer_process_fd(fds[2].fd);
            if (ret == 0)
            {
                ULOGD("timer all ready, stop timer");
                close(fd_timer);
                fds[2].fd = -1;
                fd_timer = -1;
            }
        }
        process_all();
    }

    close(fd_pipes[0]);
    close(fd_pipes[1]);
    bagad_cli_destroy(bagad_cli);
    if (fd_timer != -1) close(fd_timer);
    ULOGW("powermanagement exiting");
    return 0;
}

 

你可能感兴趣的:(poll机制监听多个event)