static char
rtimer_task(struct rtimer *t, void *ptr)
{
    PT_BEGIN(&pt);

    while (1) {
        leds_on(LEDS_ALL);
        rtimer_set(t, RTIMER_NOW() + LEDS_ON_PERIOD, 0, (rtimer_callback_t)rtimer_task, NULL);
        PT_YIELD(&pt);

        leds_off(LEDS_ALL);
        rtimer_set(t, timesynch_time_to_rtimer(0), 0, (rtimer_callback_t)rtimer_task, NULL);
        PT_YIELD(&pt);
    }

    PT_END(&pt);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(timesync_master_process, ev, data)
{
    static struct etimer et;

    PROCESS_BEGIN();

    timesynch_set_authority_level(0);

    PT_INIT(&pt);

    // wait until network is synchronized
    etimer_set(&et, SYNCH_PERIOD);
    PROCESS_WAIT_UNTIL(etimer_expired(&et));

    rtimer_set(&rt, timesynch_time_to_rtimer(0), 0, (rtimer_callback_t)rtimer_task, NULL);

    PROCESS_END();
}
Beispiel #3
0
/*---------------------------------------------------------------------------*/
static char 
powercycle(struct rtimer *t, void *ptr)
{
  int r;
#if WITH_TIMESYNCH
  rtimer_clock_t should_be, adjust;
#endif /* WITH_TIMESYNCH */

  CPRINTF("*");
  
  PT_BEGIN(&pt);

  while(1) {
    /* Only wait for some cycles to pass for someone to start sending */
    if(someone_is_sending > 0) {
      someone_is_sending--;
    }

    if(xmac_config.off_time > 0) {
      if(waiting_for_packet == 0) {
	if(we_are_sending == 0) {
	  off();
	}
      } else {
	waiting_for_packet++;
	if(waiting_for_packet > 2) {
	  /* We should not be awake for more than two consecutive
	     power cycles without having heard a packet, so we turn off
	     the radio. */
	  waiting_for_packet = 0;
#if WITH_TIMETABLE
	  TIMETABLE_TIMESTAMP(xmac_timetable, "off waiting");
#endif
	  off();
	}
      }

#if WITH_TIMESYNCH
#define NUM_SLOTS 8
      should_be = ((timesynch_rtimer_to_time(RTIMER_TIME(t)) +
		    xmac_config.off_time) &
		   ~(xmac_config.off_time + xmac_config.on_time - 1)) +
	(rimeaddr_node_addr.u8[0] % NUM_SLOTS *
	 ((xmac_config.off_time + xmac_config.on_time) / NUM_SLOTS));

      should_be = timesynch_time_to_rtimer(should_be);

      if(should_be - RTIMER_TIME(t) > xmac_config.off_time) {
	adjust = xmac_config.off_time / 2;
      } else {
	adjust = should_be - RTIMER_TIME(t);
      }
      r = rtimer_set(t, RTIMER_TIME(t) + adjust, 1,
		     (void (*)(struct rtimer *, void *))powercycle, ptr);
#else /* WITH_TIMESYNCH */
      r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.off_time, 1,
		     TC(powercycle), ptr);
#endif /* WITH_TIMESYNCH */
      if(r) {
	PRINTF("xmac: 1 could not set rtimer %d\n", r);
      }
      PT_YIELD(&pt);
    }

    if(we_are_sending == 0 &&
       waiting_for_packet == 0) {
      on();
    }
    r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.on_time, 1,
		   TC(powercycle), ptr);
    if(r) {
      PRINTF("xmac: 3 could not set rtimer %d\n", r);
    }

    PT_YIELD(&pt);
  }

  PT_END(&pt);
}