示例#1
0
static void console_handle_clubby_ready(struct clubby_event *evt,
                                        void *user_data) {
  (void) evt;
  if (!s_waiting_for_resp) {
    console_process_data((struct v7 *) user_data);
  }
}
static void* console_reader_thread(void *arg)
{
  vantage_console_context_t *vantage_console_priv = (vantage_console_context_t *)arg;
 
  unsigned char in_buf[TTY_RECV_BUFFER_SIZE];
  int n;
  uint64_t exp;
  int timeout_sec = (VANTAGE_PERIODIC_WEATHER_DATA_QUERY_IN_S + 1);

  struct pollfd fds[2];
  int     console_tty_fd = vantage_console_priv->console_tty_fd;
  int     timer_fd = vantage_console_priv->timer_fd;
  int     ret, console_need_reopen;

  if (loglevel > 0)
  {
    LOG_printf(LOG_LVL_INFO, "TTY Reader thread started\n");
  }

  /* Prepare the poll fd set */
  fds[0].fd = console_tty_fd;
  fds[0].events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
  fds[1].fd = timer_fd;
  fds[1].events = POLLIN;

  while(vantage_console_priv->tty_thread_stop_req == 0)
  {
    console_need_reopen = 0;

    if (loglevel > 2)
    {
      LOG_printf(LOG_LVL_DEBUG, "TTY thread -> select\n");
    }

    ret = poll(fds, 2, timeout_sec * 1000);
    if (ret == -1) 
    {
      LOG_printf(LOG_LVL_ERROR, "Poll error. Errno %d\n", errno);
      return NULL;
    }

    if (loglevel > 2)
    {
      LOG_printf(LOG_LVL_DEBUG, "TTY thread poll returns %d\n", ret);
    }

    if (ret == 0)
    {
      LOG_printf(LOG_LVL_ERROR, "Poll timeout. Try to re-open the tty. Errno %d\n", errno);

      console_need_reopen = 1;
      goto console_reopen_check;
    }
    else
    {
      if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
      {
        LOG_printf(LOG_LVL_ERROR, "Error detected on the TTY device. Try to re-open the tty. revents %x\n", fds[0].revents);

        console_need_reopen = 1;
        goto console_reopen_check;
      }
      else if (fds[0].revents & POLLIN)
      {
        if (loglevel > 2)
        {
          LOG_printf(LOG_LVL_DEBUG, "Console FD set\n");
        }
        n = read(console_tty_fd, in_buf, TTY_RECV_BUFFER_SIZE);
        if (n > 0) {
          if (loglevel > 2)
          {
            LOG_printf(LOG_LVL_DEBUG, "Readed %d bytes from TTY\n", n);
          }
          console_process_data(vantage_console_priv, in_buf, n);
        }
        else
        {
          LOG_printf(LOG_LVL_ERROR, "0 bytes read from the TTY device. Try to re-open the tty. revents %x\n");

          console_need_reopen = 1;
          goto console_reopen_check;
        }

        timeout_sec = (VANTAGE_PERIODIC_WEATHER_DATA_QUERY_IN_S + 1);
      }

      if (fds[1].revents & POLLIN)
      {
        if (loglevel > 2)
        {
          LOG_printf(LOG_LVL_DEBUG, "Timer FD set\n");
        }
        read(timer_fd, &exp, sizeof(uint64_t));

        if (loglevel > 0)
        {
          LOG_printf(LOG_LVL_INFO, "Send LOOP%s packet request\n", use_loop2 ? "2" : "");
        }

        /* Wake-up console before LOOP packet request */
        console_wakeup(console_tty_fd);

        if (use_loop2)
        {
          write(console_tty_fd, LOOP2_PACKET_REQ, sizeof(LOOP2_PACKET_REQ));  /* Request LOOP2 packet */
        }
        else
        {
          write(console_tty_fd, LOOP_PACKET_REQ, sizeof(LOOP_PACKET_REQ));  /* Request LOOP packet */
        }

        timeout_sec = 2;
      }
    }

console_reopen_check:
    if (console_need_reopen)
    {
      do
      {
        close(console_tty_fd);
        vantage_console_priv->console_tty_fd = -1;

        console_tty_fd = console_tty_probe(vantage_console_priv->console_dev_path, vantage_console_priv->use_usb_serial);
        if (console_tty_fd < 0)
        {
          LOG_printf(LOG_LVL_ERROR, "Could not find console. Try again in 1 second\n");
          sleep(1);
        }
        else
        {
          vantage_console_priv->console_tty_fd = console_tty_fd; 

          timeout_sec = (VANTAGE_PERIODIC_WEATHER_DATA_QUERY_IN_S + 1);

          console_need_reopen = 0;
        }
      } while(console_need_reopen);
    }
  }

  if (loglevel > 0)
  {
    LOG_printf(LOG_LVL_INFO, "TTY Reader thread stopped\n");
  }

  return NULL;
}
示例#3
0
static void clubby_cb(struct clubby_event *evt, void *user_data) {
  (void) evt;
  s_waiting_for_resp = 0;
  console_process_data((struct v7 *) user_data);
}