예제 #1
0
void
main_thread_entry(cyg_addrword_t p)
{
    CYG_TEST_INFO("Initializing lwIP");
    cyg_lwip_simple_init();

    test_state = TEST_CHAT_INIT;
    req_state = REQ_DNS_INIT;

    while (1) {
        cyg_lwip_simple_poll();
        switch (test_state) {
        case TEST_INITIAL:
            diag_printf("INFO:<Starting test run (%d/%d)>\n", run, NUM_RUNS);
            test_state = TEST_CHAT_INIT;
            break;
        case TEST_CHAT_INIT:
            CYG_TEST_INFO("Initializing chat");
            sd = sio_open(SIO_DEV_PPPOS);
            if (!sd)
                CYG_TEST_FAIL_FINISH("Cannot open serial");
            chat = chat_new(sd, chat_items, CHAT_TIMEOUT, chat_cb, chat_send_cb, NULL);
            if (!chat)
                CYG_TEST_FAIL_FINISH("Cannot allocate chat");
            test_state = TEST_CHAT_RUN;
            break;
        case TEST_CHAT_RUN:
            chat_poll(chat);
            break;
        case TEST_CHAT_FINISH:
            chat_free(chat);
            if (chat_ok)
                test_state = TEST_PPP_INIT;
            else
                test_state = TEST_NEXT_RUN;
            break;
        case TEST_PPP_INIT:
            CYG_TEST_INFO("Initializing PPP");
            if (ppp_init() != ERR_OK)
                CYG_TEST_FAIL_FINISH("Cannot initialize PPP");
            pd = ppp_open_serial(sd, ppp_status_cb, (void *) pd);
            if (pd < 0)
                CYG_TEST_FAIL_FINISH("Cannot open PPP over serial");
            ppp_set_auth(PPPAUTHTYPE_ANY, CYGDAT_NET_LWIP_PPP_TEST_USERNAME,
                                          CYGDAT_NET_LWIP_PPP_TEST_PASSWORD);
            test_state = TEST_PPP_INIT_WAIT;
            break;
        case TEST_PPP_INIT_WAIT:
            ppp_poll(pd);
            break;
        case TEST_PPP_UP:
            ppp_poll(pd);
            handle_req_state();
            break;
        case TEST_PPP_CLOSE:
            ppp_close(pd);
            test_state = TEST_PPP_CLOSE_WAIT;
            break;
        case TEST_PPP_CLOSE_WAIT:
            ppp_poll(pd);
            if (!ppp_is_open(pd))
                // Escape from data mode
                sio_write(sd, (u8_t *) "+++", 3);
                cyg_thread_delay(200);
                test_state = TEST_NEXT_RUN;
            break;
        case TEST_NEXT_RUN:
            if (run < NUM_RUNS) {
                run++;
                test_state = TEST_INITIAL;
            } else {
                test_state = TEST_FINISH;
            }
            break;
        case TEST_FINISH:
            test_state = TEST_CHAT_INIT;
            diag_printf("INFO:<Test done (%d/%d) successful runs\n",
                        success, NUM_RUNS);
            break;
        }
        cyg_thread_yield();
    }
}
예제 #2
0
파일: pppd.c 프로젝트: acassis/ros2_nuttx
int pppd(struct pppd_settings_s *pppd_settings)
{
  struct pollfd fds[2];
  int ret;
  struct ppp_context_s *ctx;

  ctx = (struct ppp_context_s*)malloc(sizeof(struct ppp_context_s));
  memset(ctx, 0, sizeof(struct ppp_context_s));
  strcpy((char*)ctx->ifname, "ppp%d");

  ctx->settings = pppd_settings;
  ctx->if_fd = tun_alloc((char*)ctx->ifname);
  if (ctx->if_fd < 0)
    {
      free(ctx);
      return 2;
    }

  ctx->ctl.fd = open_tty(pppd_settings->ttyname);
  if (ctx->ctl.fd < 0)
    {
      close(ctx->ctl.fd);
      free(ctx);
      return 2;
    }

  ctx->ctl.echo    = true;
  ctx->ctl.verbose = true;
  ctx->ctl.timeout = 30;

  fds[0].fd = ctx->if_fd;
  fds[0].events = POLLIN;

  fds[1].fd = ctx->ctl.fd;
  fds[1].events = POLLIN;

  ppp_init(ctx);
  ppp_reconnect(ctx);

  while (1)
    {
      fds[0].revents = fds[1].revents = 0;

      ret = poll(fds, 2, 1000);

      if (ret > 0 && fds[0].revents & POLLIN)
        {
          ret = read(ctx->if_fd, ctx->ip_buf, PPP_RX_BUFFER_SIZE);
          printf("read from tun :%i\n", ret);
          if (ret > 0)
            {
              ctx->ip_len = ret;
              ppp_send(ctx);
              ctx->ip_len = 0;
            }
        }

      ppp_poll(ctx);

      if (ppp_check_errors(ctx))
        {
          ppp_reconnect(ctx);
        }
      else
        {
          if (ctx->ip_len > 0)
            {
              ret = write(ctx->if_fd, ctx->ip_buf, ctx->ip_len);
              ctx->ip_len = 0;

              ret = read(ctx->if_fd, ctx->ip_buf, PPP_RX_BUFFER_SIZE);
              if (ret > 0)
                {
                  ctx->ip_len = ret;
                  ppp_send(ctx);
                  ctx->ip_len = 0;
                }
            }
        }
    }

  return 1;
}