Java_se_sics_cooja_corecomm_[CLASS_NAME]_tick(JNIEnv *env, jobject obj)
{
  /* Let all simulation interfaces act first */
  simNoYield = 1;
  doActionsBeforeTick();
  simNoYield = 0;

  /* Check if any e-timers are pending (save result for state decisions) */
  if (etimer_pending()) {
    /* Poll etimers */
    etimer_request_poll();
    simEtimerPending = 1;
  } else {
    simEtimerPending = 0;
  }

  /* Let Contiki execute one or a part of the process_run()-function via the thread.
  	This call stores the process_run() return value */
  cooja_mt_exec(&process_run_thread);

  /* Let all simulation interfaces act before returning to java */
  simNoYield = 1;
  doActionsAfterTick();
  simNoYield = 0;

  /* Look for new e-timers */
  if (!simEtimerPending && etimer_pending()) {
    /* Poll etimers */
    etimer_request_poll();
    simEtimerPending = 1;
  }

  /* Save nearest event timer expiration time (0 if no timers) */
  simNextExpirationTime = etimer_next_expiration_time();
}
Beispiel #2
0
Java_se_sics_cooja_corecomm_[CLASS_NAME]_tick(JNIEnv *env, jobject obj)
{
  /* Let all simulation interfaces act first */
  simNoYield = 1;
  doActionsBeforeTick();
  simNoYield = 0;

  /* Poll etimer process */
  if (etimer_pending()) {	
    etimer_request_poll();
  }

  /* Let Contiki handle a few events.
  	This call stores the process_run() return value */
  cooja_mt_exec(&process_run_thread);

  /* Let all simulation interfaces act before returning to java */
  simNoYield = 1;
  doActionsAfterTick();
  simNoYield = 0;

  /* Look for new e-timers */
  simEtimerPending = etimer_pending();

  /* Save nearest event timer expiration time */
  if (simEtimerPending) {
    simNextExpirationTime = etimer_next_expiration_time() - simCurrentTime;
  }
}
Beispiel #3
0
/**
 * \brief      Let mote execute one "block" of code (tick mote).
 *
 *             Let mote defined by the active contiki processes and current
 *             process memory execute some program code. This code must not block
 *             or else this function will never return. A typical contiki
 *             process will return when it executes PROCESS_WAIT..() statements.
 *
 *             Before the control is left to contiki processes, any messages
 *             from the Java part are handled. These may for example be
 *             incoming network data. After the contiki processes return control,
 *             messages to the Java part are also handled (those which may need
 *             special attention).
 *
 *             This is a JNI function and should only be called via the
 *             responsible Java part (MoteType.java).
 */
JNIEXPORT void JNICALL
Java_org_contikios_cooja_corecomm_CLASSNAME_tick(JNIEnv *env, jobject obj)
{
  clock_time_t nextEtimer;
  rtimer_clock_t nextRtimer;

  simProcessRunValue = 0;

  /* Let all simulation interfaces act first */
  doActionsBeforeTick();

  /* Poll etimer process */
  if (etimer_pending()) {
    etimer_request_poll();
  }

  /* Let rtimers run.
   * Sets simProcessRunValue */
  cooja_mt_exec(&rtimer_thread);

  if(simProcessRunValue == 0) {
    /* Rtimers done: Let Contiki handle a few events.
     * Sets simProcessRunValue */
    cooja_mt_exec(&process_run_thread);
  }

  /* Let all simulation interfaces act before returning to java */
  doActionsAfterTick();

  /* Do we have any pending timers */
  simEtimerPending = etimer_pending() || rtimer_arch_pending();
  if(!simEtimerPending) {
    return;
  }

  /* Save nearest expiration time */
  nextEtimer = etimer_next_expiration_time() - (clock_time_t) simCurrentTime;
  nextRtimer = rtimer_arch_next() - (rtimer_clock_t) simCurrentTime;
  if(etimer_pending() && rtimer_arch_pending()) {
    simNextExpirationTime = MIN(nextEtimer, nextRtimer);
  } else if (etimer_pending()) {
    simNextExpirationTime = nextEtimer;
  } else if (rtimer_arch_pending()) {
    simNextExpirationTime = nextRtimer;
  }
}
Beispiel #4
0
/*---------------------------------------------------------------------------*/
int
main(int argc, char **argv)
{
    PRINTF(">> %s:main()\n", __FILE__);

start:
    /* Yield once during bootup */
    kleenet_schedule_state(MILLISECOND);
    kleenet_yield_state();
    simCurrentTime = NODE_TIME();

    PRINTF("> Executing Contiki at simulation time %lu\n", clock_time());

    /* Code from contiki-cooja-main.c:process_run_thread_loop (before loop) */
    doActionsBeforeTick();
    contiki_init();
    doActionsAfterTick();

    while (1) {

        /* reboot */
        if ((clock_seconds() >= failure_delay()) && reboot_once) {
            /* symbolic node reboot every 10 seconds */
            if (clock_seconds() % 10 == 0) {
                if (failure_reboot_node()) {
                    PRINTF("main(): rebooting node once @ %lu, %lu seconds\n",
                           clock_time(), clock_seconds());
                    reboot_once = 0;
                    exit_all_processes();
                    goto start;
                }
            }
        }
        /* halt */
        if ((clock_seconds() >= failure_delay()) && halt_once) {
            /* symbolic node outage every 10 seconds */
            if (clock_seconds() % 10 == 0) {
                if (failure_halt_node()) {
                    PRINTF("main(): halting node once @ %lu, %lu seconds\n",
                           clock_time(), clock_seconds());
                    halt_once = 0;
                    exit_all_processes();
                    /* loop forerver */
                    while(1) {
                        kleenet_schedule_state(MILLISECOND);
                        kleenet_yield_state();
                    }
                }
            }
        }

        /* Update time */
        simCurrentTime = NODE_TIME();
        simProcessRunValue = 0;

        /* Do actions before tick */
        doActionsBeforeTick();

        /* Poll etimer process */
        if (etimer_pending()) {
            etimer_request_poll();
        }

        /* Code from contiki-cooja-main.c:process_run_thread_loop */
        simProcessRunValue = process_run();
        while(simProcessRunValue-- > 0) {
            process_run();
        }
        simProcessRunValue = process_nevents();

        /* Do actions after tick */
        doActionsAfterTick();

        /* Request next tick for remaining events / timers */
        if (simProcessRunValue != 0) {
            kleenet_schedule_state(MILLISECOND);
            kleenet_yield_state();
            continue;
        }

        /* Request tick next wakeup time */
        if (etimer_pending()) {
            simNextExpirationTime = etimer_next_expiration_time() - simCurrentTime;
        } else {
            simNextExpirationTime = 0;
            PRINTF("WARNING: No more timers pending\n");
            kleenet_schedule_state(MILLISECOND);
            kleenet_yield_state();
            continue;
        }

        /* check next expiration time */
        if (simNextExpirationTime <= 0) {
            PRINTF("WARNING: Event timer already expired, but has been delayed: %lu\n",
                   simNextExpirationTime);
            kleenet_schedule_state(MILLISECOND);
        } else {
            kleenet_schedule_state(simNextExpirationTime*MILLISECOND);
        }

        /* yield active state */
        kleenet_yield_state();

    }
    return 0;
}