/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udpstream_process, ev, data) { static uint16_t streamno; static struct etimer et; static uip_ipaddr_t *ipaddr; PROCESS_BEGIN(); PROCESS_PAUSE(); printf("Formatting Coffee FS...\n"); cfs_coffee_format(); printf("done.\n"); /* We need re-initialize queuebuf after formatting */ queuebuf_init(); /* Start service registration */ servreg_hack_init(); ipaddr = set_global_address(); if(node_id == SINK_ID) { /* The sink creates a dag and waits for UDP datagrams */ create_rpl_dag(ipaddr); servreg_hack_register(SERVICE_ID, ipaddr); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); while(1) { PROCESS_WAIT_EVENT(); } } else if(node_id == SENDER_ID) { /* The sender looks for the sink and sends UDP streams */ ipaddr = NULL; simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&et, 10*CLOCK_SECOND); etimer_restart(&et); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); if(ipaddr != NULL) { streamno++; send_stream(ipaddr, streamno); } else { ipaddr = servreg_hack_lookup(SERVICE_ID); if(ipaddr != NULL) { etimer_set(&et, 2*CLOCK_SECOND); printf("Streaming to "); uip_debug_ipaddr_print(ipaddr); printf("\n"); } else { printf("Service %d not found\n", SERVICE_ID); } } etimer_restart(&et); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et, wake_timer, periodic_timer; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("UDP client process started\n"); #if UIP_CONF_ROUTER set_global_address(); #endif print_local_addresses(); set_connection_address(&ipaddr); /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); udp_bind(client_conn, UIP_HTONS(3001)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, CLOCK_SECOND*10); PROCESS_WAIT_UNTIL(etimer_expired(&et)); // Wait for DAD and Router Discovery procedure to end. etimer_set(&et, SEND_INTERVAL); etimer_set(&wake_timer, AWAKE_INTERVAL); etimer_set(&periodic_timer, 1); while(1) { PROCESS_YIELD(); if(etimer_expired(&wake_timer)){ // if timer hasn't expired do not go in deep sleep, in order to receive a response. printf("Sleeping...\r\n"); sensorsPowerDown(); sleep_seconds(SLEEP_INTERVAL_SECONDS); // Put system in deep sleep mode for a while. sensorsPowerUp(); printf("Awake\r\n"); } if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); etimer_restart(&wake_timer); } else if(ev == tcpip_event) { tcpip_handler(); } /* Make the process be called almost immediately, * so that it can force the system to go into deep sleep. */ etimer_restart(&periodic_timer); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(ipv6_process, ev, data) { PROCESS_BEGIN(); printf("started!\n"); etimer_set(&periodic_timer, 1*CLOCK_SECOND); while(1) { PROCESS_YIELD(); if (etimer_expired(&periodic_timer)) { if (!(spibyte & 0x01)) { SPI_READ(spibuf[0]); } else { SPI_WRITE(spibuf[1]); } spibyte++; etimer_restart(&periodic_timer); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(hello_world_process, ev, data) { PROCESS_BEGIN(); static struct etimer myTimer; printf("Hello, world\n"); leds_init(); //PORTR_OUT = 0x3; etimer_set(&myTimer, CLOCK_SECOND); while(1) { PROCESS_WAIT_EVENT(); // PORTR.OUTTGL = 0x3; leds_toggle(0x3); printf("Toggle LED0 from process: %10d: %X\n", i++, leds_get()); etimer_restart(&myTimer); } PROCESS_END(); }
PROCESS_THREAD(rtc_test, ev, data) { PROCESS_BEGIN(); leds_off(LEDS_ALL); etimer_set(&periodic_timer_rtc, CLOCK_SECOND*5); while(1) { PROCESS_YIELD(); if (etimer_expired(&periodic_timer_rtc)) { rv3049_read_time(&rtctime); if (rtctime.seconds - 3 > last_seconds) { leds_toggle(LEDS_BLUE); leds_off(LEDS_RED); } else { leds_toggle(LEDS_RED); leds_off(LEDS_BLUE); } last_seconds = rtctime.seconds; etimer_restart(&periodic_timer_rtc); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("UDP client process started\n"); print_local_addresses(); set_connection_address(&ipaddr); /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); udp_bind(client_conn, UIP_HTONS(3001)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); /* NOTE: Use IPv6 address of server here. */ uip_ip6addr(&ipaddr, 0xfe80, 0x0000, 0x0000, 0x0000, 0x48ad, 0x5dff, 0xfe71, 0x5d9d); /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); udp_bind(client_conn, UIP_HTONS(3034)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, SEND_INTERVAL); while (1) { PROCESS_YIELD(); if (etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } else if (ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void start_periodic_tcp_timer(void) { if(etimer_expired(&periodic)) { etimer_restart(&periodic); } }
PROCESS_THREAD(udp_process, ev, data) { static struct uip_udp_conn *s; static struct etimer et; PROCESS_BEGIN(); uart0_set_br(1000000); /* setup udp connection */ s = udp_broadcast_new(UIP_HTONS(2020),NULL); // incoming udp_bind(s,UIP_HTONS(2020)); // outgoing /* start estimator process and init psock connection handler */ process_start(&ahrs_process, NULL); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER); uip_udp_packet_send(s,msg,strlen(msg)); etimer_restart(&et); } PROCESS_END(); }
PROCESS_THREAD(udp_plug_process, ev, data) { static struct etimer et; PROCESS_BEGIN(); PRINTF("UDP server started\r\n"); #if DEV_BOARD leds_on(LEDS_RED | LEDS_GREEN); SENSORS_ACTIVATE(button_sensor); #else leds_on(LEDS_GREEN); #ifdef HAS_PIR_SENSOR pir_state = PIR_DISPATCH; etimer_set(&pir_timer, PIR_INIT_TIME); #endif #endif #if HAS_TEMP_SENSOR start_temp_conv(); #endif #if HAS_LIGHT_SENSOR light_sensor_init(); #endif udp_conn = udp_new(NULL, UIP_HTONS(0), NULL); udp_bind(udp_conn, UIP_HTONS(PLUG_PORT)); PRINTF("listening on udp port %u\r\n",UIP_HTONS(udp_conn->lport)); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } #ifdef HAS_PIR_SENSOR if((pir_state == PIR_DISPATCH)&&(etimer_expired(&pir_timer))) { SENSORS_ACTIVATE(button_sensor); pir_state = PIR_READY; } #endif if(ev == tcpip_event) { PRINTF("Calling tcpip_Handler\r\n"); tcpip_handler(); } if (ev == sensors_event && data == &button_sensor) { #ifndef DEV_BOARD handle_pir_event(); #endif PRINTF("Button Pressed\r\n"); } } PROCESS_END(); }
void gp_int() { etimer_set(&periodic_timer_gpio, CLOCK_SECOND/10); unsigned int i; for(i = 0;i<100000000;i++) leds_toggle(LEDS_BLUE); printf("@@@@@@@@@@@@@@\n"); etimer_restart(&periodic_timer_gpio); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(node_process, ev, data) { static struct etimer et; PROCESS_BEGIN(); /* 3 possible roles: * - role_6ln: simple node, will join any network, secured or not * - role_6dg: DAG root, will advertise (unsecured) beacons * */ static int is_coordinator = 0; static enum { role_6ln, role_6dr } node_role; node_role = role_6ln; #if CONFIG_VIA_BUTTON { #define CONFIG_WAIT_TIME 10 SENSORS_ACTIVATE(button_sensor); etimer_set(&et, CLOCK_SECOND * CONFIG_WAIT_TIME); while(!etimer_expired(&et)) { printf("Init: current role: %s. Will start in %u seconds.\n", node_role == role_6ln ? "6ln" : "6dr", CONFIG_WAIT_TIME); PROCESS_WAIT_EVENT_UNTIL(((ev == sensors_event) && (data == &button_sensor) && button_sensor.value(0) > 0) || etimer_expired(&et)); if(ev == sensors_event && data == &button_sensor && button_sensor.value(0) > 0) { node_role = (node_role + 1) % 2; etimer_restart(&et); } } } #endif /* CONFIG_VIA_BUTTON */ printf("Init: node starting with role %s\n", node_role == role_6ln ? "6ln" : "6dr"); is_coordinator = node_role > role_6ln; if(is_coordinator) { uip_ipaddr_t prefix; uip_ip6addr(&prefix, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); rpl_tools_init(&prefix); } else { rpl_tools_init(NULL); } /* Print out routing tables every minute */ etimer_set(&et, CLOCK_SECOND * 60); while(1) { print_network_status(); PROCESS_YIELD_UNTIL(etimer_expired(&et)); etimer_reset(&et); } PROCESS_END(); }
/*============================================================================*/ void ctimer_restart(struct ctimer *c) { if(gc_init) { etimer_restart(&c->etimer); } list_remove(gp_ctimList, c); list_add(gp_ctimList, c); }
/** * calculates the new sampling rate (the "magic" part of SDF is done here) */ static void update_sampling_rate(struct etimer* sample_timer, struct etimer* transmit_timer, int real_calculation) { // samplingrate energy sample if(last_samplingrate_energysample == 0 || (time() - last_samplingrate_energysample) / SDF_SAMPLINGRATE_UPDATEINTERVAL > 0) { // take sample // first call with no information on last samplingrate and childs will // not do anything: reference energy sample is accquired samplingrate_sample_energy_drain(samplingrate, last_samplingrate_childs); // set infos for next sample last_samplingrate_childs = udphelper_childs_all_count(); last_samplingrate_energysample = time(); } // calculate samplingrate if(real_calculation) { // calculate samplingrate if((time() - time_init) / SDF_INITIALIZATIONPHASE == 0) { fpint fp_samplingrate = fpint_div(fpint_to(SDF_SAMPLINGRATE_MINIMAL), fpint_to(SPEEDMULTIPLIER)); samplingrate = fpint_from(fpint_round(fp_samplingrate)); } else { int max_samples = (udphelper_address_equals(udphelper_address_parent(&ip_parent), &ip_sink)) ? -1 : last_parent_samlingrate; samplingrate = samplingrate_calculate(max_samples); // send sampling rate to all childs if(udphelper_childs_direct_count() > 0) { samplingrate_children_transmitted = 0; samplingrate_children_count = udphelper_childs_direct_count(); etimer_restart(transmit_timer); } } // debug printf("[%lds] ", time()); printf("%d samples (battery=%ldmAh)\n", samplingrate, fpint_from(battery_capacity())); } // set samplingrate timer (evenly distributed samples with gaps at start and end) int sampling_delay = (SDF_SAMPLINGRATE_UPDATEINTERVAL - 60) / samplingrate; etimer_set(sample_timer, CLOCK_SECOND * sampling_delay / SPEEDMULTIPLIER); etimer_restart(sample_timer); // reset old samples counter (new interval begins) sampled = 0; }
/*---------------------------------------------------------------------------*/ static void _fradio_handler(c_event_t ev, p_data_t data) { uint8_t len = 0; if (etimer_expired(&tmr)) { if ((len = _fradio_read(packetbuf_dataptr(), PACKETBUF_SIZE)) > 0) { packetbuf_set_datalen(len); //p_ns->lmac->input(); } etimer_restart(&tmr); } }
/*---------------------------------------------------------------------------*/ void ctimer_restart(struct ctimer *c) { if(initialized) { PROCESS_CONTEXT_BEGIN(&ctimer_process); etimer_restart(&c->etimer); PROCESS_CONTEXT_END(&ctimer_process); } list_remove(ctimer_list, c); list_add(ctimer_list, c); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; int port = 3000; /* Default to 3000 if not using service discovery. */ PROCESS_BEGIN(); PRINTF("UDP client process started\n"); #if UIP_CONF_ROUTER set_global_address(); #endif print_local_addresses(); static resolv_status_t status = RESOLV_STATUS_UNCACHED; while(status != RESOLV_STATUS_CACHED) { #if RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD status = set_connection_address(&ipaddr, &port); #else status = set_connection_address(&ipaddr, NULL); #endif if(status == RESOLV_STATUS_RESOLVING) { PROCESS_WAIT_EVENT_UNTIL(ev == resolv_event_found); } else if(status != RESOLV_STATUS_CACHED) { PRINTF("Can't get connection address.\n"); PROCESS_YIELD(); } } /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(port), NULL); udp_bind(client_conn, UIP_HTONS(port + 1)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
/*-----------------------------------------------------------------------------------*/ void coap_notify_observers(resource_t *resource, int32_t obs_counter, void *notification) { coap_packet_t *const coap_res = (coap_packet_t *) notification; coap_observer_t* obs = NULL; uint8_t preferred_type = coap_res->type; PRINTF("Observing: Notification from %s\n", resource->url); /* Iterate over observers. */ for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next) { if (obs->url==resource->url) /* using RESOURCE url pointer as handle */ { coap_transaction_t *transaction = NULL; /*TODO implement special transaction for CON, sharing the same buffer to allow for more observers. */ if ( (transaction = coap_new_transaction(coap_get_mid(), &obs->addr, obs->port)) ) { PRINTF(" Observer "); PRINT6ADDR(&obs->addr); PRINTF(":%u\n", obs->port); /* Update last MID for RST matching. */ obs->last_mid = transaction->mid; /* Prepare response */ coap_res->mid = transaction->mid; if (obs_counter>=0) coap_set_header_observe(coap_res, obs_counter); coap_set_header_token(coap_res, obs->token, obs->token_len); /* Use CON to check whether client is still there/interested after COAP_OBSERVING_REFRESH_INTERVAL. */ if (etimer_expired(&obs->refresh_timer)) { PRINTF(" Refreshing with CON\n"); coap_res->type = COAP_TYPE_CON; etimer_restart(&obs->refresh_timer); } else { coap_res->type = preferred_type; } transaction->packet_len = coap_serialize_message(coap_res, transaction->packet); coap_send_transaction(transaction); } } } }
/*----------------------------------------------------------------------------*/ static void _demo_udp_callback(c_event_t c_event, p_data_t p_data) { char *pc_str; if (etimer_expired(&st_et)) { _demo_udp_sendMsg(l_lastSeqId); etimer_restart(&st_et); } else if (c_event == EVENT_TYPE_TCPIP) { if (uip_newdata()) { pc_str = uip_appdata; pc_str[uip_datalen()] = '\0'; LOG_INFO("Packet from a server: '%s'\n\r", pc_str); l_lastSeqId = _demo_extractSeq(pc_str); } } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(delete_process, ev, data) { static struct etimer update_check_timer; static struct etimer update_send_timer; static struct akes_nbr_entry *next; PROCESS_BEGIN(); PRINTF("akes-delete: Started update_process\n"); etimer_set(&update_check_timer, UPDATE_CHECK_INTERVAL * CLOCK_SECOND); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&update_check_timer)); PRINTF("akes-delete: #permanent = %d\n", akes_nbr_count(AKES_NBR_PERMANENT)); next = akes_nbr_head(); while(next) { if(!next->permanent || !akes_nbr_is_expired(next, AKES_NBR_PERMANENT)) { next = akes_nbr_next(next); continue; } /* wait for a random period of time to avoid collisions */ etimer_set(&update_send_timer, akes_get_random_waiting_period()); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&update_send_timer)); /* check if something happened in the meantime */ if(!akes_nbr_is_expired(next, AKES_NBR_PERMANENT)) { next = akes_nbr_next(next); continue; } /* send UPDATE */ akes_send_update(next); PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); PRINTF("akes-delete: Sent UPDATE\n"); etimer_set(&update_send_timer, UPDATEACK_WAITING_PERIOD * CLOCK_SECOND); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&update_send_timer)); if(akes_nbr_is_expired(next, AKES_NBR_PERMANENT)) { akes_nbr_delete(next, AKES_NBR_PERMANENT); next = akes_nbr_head(); } else { next = akes_nbr_next(next); } } etimer_restart(&update_check_timer); } PROCESS_END(); }
PROCESS_THREAD(udp_component_kev, ev, data) { static uip_ipaddr_t addr; static struct simple_udp_connection unicast_connection; static struct etimer timer; static UDPClientComponent* inst; static uint16_t message_number; PROCESS_BEGIN(); PRINTF("UDP server started\n"); /* confire server address */ uip_ip6addr(&addr, 0xaaaa, 0, 0, 0, 0, 0, 0, 1); inst = (UDPClientComponent*) data; /* print local address */ print_local_addresses(); etimer_set(&timer, CLOCK_SECOND * (inst->interval/1000)); /* initialize the UDP stuff */ simple_udp_register(&unicast_connection, inst->remotePort, NULL, inst->remotePort, receiver); /* TODO: print the RPL tree */ while(1) { PROCESS_WAIT_EVENT(); if (ev == PROCESS_EVENT_TIMER) { char buf[20]; sprintf(buf, "Message %d", message_number++); printf("Sending %s unicast to ", buf); uip_debug_ipaddr_print(&addr); printf("\n"); /* send message to the server */ simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, &addr); generate_routes(); etimer_restart(&timer); } else { /* process network messages */ } } PROCESS_END(); }
PROCESS_THREAD(mac_eth_process, ev, data) { PROCESS_BEGIN(); etimer_set(&mac_eth_periodic, CLOCK_SECOND * 10); while(1) { PROCESS_WAIT_EVENT(); if(ev == PROCESS_EVENT_TIMER) { uipv4_arp_timer(); etimer_restart(&mac_eth_periodic); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(wait_for_dag, ev, data) { static struct etimer et; PROCESS_BEGIN(); PRINTF("Wait for DAG process started\n"); etimer_set(&et, INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("UDP client process started\n"); uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0x0215, 0x2000, 0x0002, 0x2145); /* new connection with remote host */ l_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); if(!l_conn) { PRINTF("udp_new l_conn error.\n"); } udp_bind(l_conn, UIP_HTONS(LOCAL_CONN_PORT)); PRINTF("Link-Local connection with "); PRINT6ADDR(&l_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(l_conn->lport), UIP_HTONS(l_conn->rport)); uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0x0215, 0x2000, 0x0002, 0x2145); g_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); if(!g_conn) { PRINTF("udp_new g_conn error.\n"); } udp_bind(g_conn, UIP_HTONS(GLOBAL_CONN_PORT)); PRINTF("Global connection with "); PRINT6ADDR(&g_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(g_conn->lport), UIP_HTONS(g_conn->rport)); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ void tcpip_uipcall(void) { register uip_udp_appstate_t *ts; #if UIP_UDP if(uip_conn != NULL) { ts = &uip_conn->appstate; } else { ts = &uip_udp_conn->appstate; } #else /* UIP_UDP */ ts = &uip_conn->appstate; #endif /* UIP_UDP */ #if UIP_TCP { static unsigned char i; register struct listenport *l; /* If this is a connection request for a listening port, we must mark the connection with the right process ID. */ if(uip_connected()) { l = &s.listenports[0]; for(i = 0; i < UIP_LISTENPORTS; ++i) { if(l->port == uip_conn->lport && l->p != PROCESS_NONE) { ts->p = l->p; ts->state = NULL; break; } ++l; } /* Start the periodic polling, if it isn't already active. */ if(etimer_expired(&periodic)) { etimer_restart(&periodic); } } } #endif if(ts->p != NULL) { process_post_synch(ts->p, tcpip_event, ts->state); } }
PROCESS_THREAD(ntpdemo_process, ev, data) { static struct etimer et; PROCESS_BEGIN(); etimer_set(&et, CLOCK_SECOND); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { printf("%lu\n", getCurrTime()); etimer_restart(&et); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(key_sampling, ev, data) { PROCESS_BEGIN(); static struct etimer et; static int previous_key_value = 0; static char debounce_check = 0; int current_key_value; etimer_set(&et, CLOCK_SECOND / 50); while(1) { PROCESS_WAIT_EVENT_UNTIL((ev == PROCESS_EVENT_TIMER) || (ev == PROCESS_EVENT_MSG)); if(ev == PROCESS_EVENT_TIMER) { /* Handle sensor reading. */ PRINTF("Key sample\n"); current_key_value = get_key_value(); if(debounce_check != 0) { /* Check if key remained constant */ if(previous_key_value == current_key_value) { sensors_changed(&button_sensor); key_value = current_key_value; debounce_check = 0; } else { /* Bouncing */ previous_key_value = current_key_value; } } else /* Check for new key change */ if(current_key_value != previous_key_value) { previous_key_value = current_key_value; debounce_check = 1; } etimer_reset(&et); } else { /* ev == PROCESS_EVENT_MSG */ if(*(buttons_status_t *)data == BUTTONS_STATUS_NOT_ACTIVE) { /* Stop sampling */ etimer_stop(&et); } else if((*(buttons_status_t *)data == BUTTONS_STATUS_ACTIVE)) { /* restart sampling */ etimer_restart(&et); } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(config_thread(struct pt *pt, process_event_t ev, process_data_t data)) { static struct etimer pushtimer; static int counter; PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, ev == sensors_event && data == &button_sensor); beep(); leds_on(LEDS_YELLOW); etimer_set(&pushtimer, CLOCK_SECOND); for(counter = 0; !etimer_expired(&pushtimer); ++counter) { etimer_restart(&pushtimer); PT_YIELD_UNTIL(pt, (ev == sensors_event && data == &button_sensor) || etimer_expired(&pushtimer)); } place_id = counter; beep_quick(place_id); pingeron = 1; packet_count = 20; etimer_set(&etimer, CLOCK_SECOND / 2); leds_off(LEDS_YELLOW); leds_on(LEDS_RED); PT_WAIT_UNTIL(pt, packet_count == 0); pingeron = 0; leds_off(LEDS_RED); } PT_END(pt); }
PROCESS_THREAD(prng_test_process, ev, data) { static unsigned char seed[SEEDLEN]; PROCESS_BEGIN(); lpm_set_max_pm(LPM_PM0); etimer_set(&et, WAIT_TIME); for (;;) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); etimer_restart(&et); leds_on(LEDS_ALL); RfRnd(seed, sizeof(seed)); leds_off(LEDS_ALL); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(ipv6_process, ev, data) { PROCESS_BEGIN(); // Set the local address uip_ip6addr(&my_addr, 0, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&my_addr, &uip_lladdr); uip_ds6_addr_add(&my_addr, 0, ADDR_MANUAL); // Setup the destination address uiplib_ipaddrconv(RECEIVER_ADDR, &dest_addr); // Add a "neighbor" for our custom route // Setup the default broadcast route uiplib_ipaddrconv(ADDR_ALL_ROUTERS, &bcast_ipaddr); uip_ds6_nbr_add(&bcast_ipaddr, &bcast_lladdr, 0, NBR_REACHABLE); uip_ds6_route_add(&dest_addr, 128, &bcast_ipaddr); // Setup a udp "connection" client_conn = udp_new(&dest_addr, UIP_HTONS(RECEIVER_PORT), NULL); if (client_conn == NULL) { // Too many udp connections // not sure how to exit...stupid contiki } udp_bind(client_conn, UIP_HTONS(3001)); etimer_set(&periodic_timer, 10*CLOCK_SECOND); while(1) { PROCESS_YIELD(); if (etimer_expired(&periodic_timer)) { send_handler(ev, data); etimer_restart(&periodic_timer); } else if (ev == tcpip_event) { recv_handler(); } } PROCESS_END(); }