Ejemplo n.º 1
0
Archivo: dhcpc.c Proyecto: lanwan/elua
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_dhcp(void))
{
  PT_BEGIN(&s.pt);
  
  /* try_again:*/
  s.state = STATE_SENDING;
  s.ticks = CLOCK_SECOND;

  do {
    send_discover();
    s.timer_init = platform_timer_start( ELUA_DHCP_TIMER_ID );
    PT_WAIT_UNTIL(&s.pt, uip_newdata() || platform_timer_get_diff_crt( ELUA_DHCP_TIMER_ID, s.timer_init ) >= s.ticks );
    if(uip_newdata() && parse_msg() == DHCPOFFER) {
      uip_flags &= ~UIP_NEWDATA;
      s.state = STATE_OFFER_RECEIVED;
      break;
    }
    uip_flags &= ~UIP_NEWDATA;
    if(s.ticks < CLOCK_SECOND * 60) {
      s.ticks *= 2;
    } else {
      s.ipaddr[0] = 0;
      goto dhcp_failed;
    }
  } while(s.state != STATE_OFFER_RECEIVED);
  
  s.ticks = CLOCK_SECOND;

  do {
    send_request();
    s.timer_init = platform_timer_start( ELUA_DHCP_TIMER_ID );
    PT_WAIT_UNTIL(&s.pt, uip_newdata() || platform_timer_get_diff_crt( ELUA_DHCP_TIMER_ID, s.timer_init ) >= s.ticks );
    
    if(uip_newdata() && parse_msg() == DHCPACK) {
      uip_flags &= ~UIP_NEWDATA;
      s.state = STATE_CONFIG_RECEIVED;
      break;
    }
    uip_flags &= ~UIP_NEWDATA;
    if(s.ticks <= CLOCK_SECOND * 10) {
      s.ticks += CLOCK_SECOND;
    } else {
      PT_RESTART(&s.pt);
    }
  } while(s.state != STATE_CONFIG_RECEIVED);
  
dhcp_failed:
  dhcpc_configured(&s);
  
  /*
   * PT_END restarts the thread so we do this instead. Eventually we
   * should reacquire expired leases here.
   */
  while(1) {
    PT_YIELD(&s.pt);
  }

  PT_END(&s.pt);
}
Ejemplo n.º 2
0
// Lua: time_us = getdiffnow( id, start )
static int tmr_getdiffnow( lua_State *L )
{
  timer_data_type start, res;
  unsigned id;

  id = ( unsigned )luaL_optinteger( L, 1, PLATFORM_TIMER_SYS_ID );
  MOD_CHECK_TIMER( id );
  start = ( timer_data_type )luaL_checknumber( L, 2 );
  res = platform_timer_get_diff_crt( id, start );
  lua_pushnumber( L, ( lua_Number )res );
  return 1;
}
Ejemplo n.º 3
0
// (tmr-getdiffnow 'num 'num) -> num
any tmr_getdiffnow(any ex) {
  timer_data_type start, res;
  unsigned id;
  any x, y;

  x = cdr(ex), y = EVAL(car(x));
  NeedNum(ex, y);
  id = unBox(y); // get id.
  MOD_CHECK_TIMER(ex, id);

  x = cdr(x), y = EVAL(car(x));
  NeedNum(ex, y);
  start = unBox(y); // get start.
  res = platform_timer_get_diff_crt(id, start);
  return box(res);
}
Ejemplo n.º 4
0
int platform_uart_recv( unsigned id, unsigned timer_id, timer_data_type timeout )
{
  timer_data_type tmr_start;
  int res;
  
  if( timeout == 0 )
    return cmn_recv_helper( id, timeout );
  else if( timeout ==  PLATFORM_TIMER_INF_TIMEOUT )
    return cmn_recv_helper( id, timeout );
  else
  {
    // Receive char with the specified timeout
    tmr_start = platform_timer_start( timer_id );
    while( 1 )
    {
      if( ( res = cmn_recv_helper( id, 0 ) ) >= 0 )
        break;
      if( platform_timer_get_diff_crt( timer_id, tmr_start ) >= timeout )
        break;
    }
    return res;
  }
}