void ota_mgr_data_req(module_code_t msg_code, addr_mode_t addr_mode, uint8_t *addr, void *payload, uint8_t len) { uip_ipaddr_t srv_addr; uint64_t server_global_prefix = 0x000000000000aaaa; if (EXTENDED_ADDR_MODE != addr_mode) { if(addr != NULL) { memcpy((uint8_t *)&(srv_addr.u16[4]),addr,PREFIX_ADDR_SIZE); memcpy((uint8_t *)&(srv_addr.u16[0]),&server_global_prefix,NATIVE_ADDR_SIZE); } else { if(server_configured) { memcpy((uint8_t *)&(srv_addr.u16[4]),(uint8_t *)&(global_server_addr.u16[4]),PREFIX_ADDR_SIZE); memcpy((uint8_t *)&(srv_addr.u16[0]),&server_global_prefix,NATIVE_ADDR_SIZE); } else { //uip_create_linklocal_allnodes_mcast(&srv_addr); ota_data_conf(msg_code, (uint8_t *) &srv_addr,SUCCESS); } } #if (OTA_COMMON_SUPPORT == 1) if(COMMON == msg_code) { memcpy(ota_common_data, &msg_code, sizeof(msg_code)); memcpy(ota_common_data+1, payload, len); simple_udp_sendto(&unicast_connection, ota_common_data, len + sizeof(msg_code), &srv_addr); ota_data_conf(msg_code, (uint8_t *) &srv_addr,SUCCESS); } #endif #if (OTA_UPGRADE_SUPPORT == 1) else if(UPGRADE == msg_code) { memcpy(ota_upgrade_data, &msg_code, sizeof(msg_code)); memcpy(ota_upgrade_data+1, payload, len); simple_udp_sendto(&unicast_connection, ota_upgrade_data, len + sizeof(msg_code), &srv_addr); ota_data_conf(msg_code, (uint8_t *) &srv_addr,SUCCESS); } #endif } }
/*---------------------------------------------------------------------------*/ void app_send_to(uint16_t id) { static unsigned int cpt; struct app_data data; uip_ipaddr_t dest_ipaddr; data.magic = ORPL_LOG_MAGIC; data.seqno = ((uint32_t)node_id << 16) + cpt; data.src = node_id; data.dest = id; data.hop = 0; data.fpcount = 0; #if WITH_LB && WITH_DIO_TARGET extern uint16_t last_periodic_value; data.dc_metric=last_periodic_value+1;//+1 to account with the packet we send #endif //node_ip6addr(&dest_ipaddr, id); set_ipaddr_from_id(&dest_ipaddr, id); LOG_FROM_APPDATAPTR(&data,"App: sending");//ORPL_LOG(&data); simple_udp_sendto(&unicast_connection, &data, sizeof(data), &dest_ipaddr); // printf("to "); // LOG_IPADDR(&dest_ipaddr); // printf("\n"); cpt++; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(broadcast_example_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; PROCESS_BEGIN(); simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); // etimer_set(&send_timer, SEND_TIME); // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); // printf("Jamming\n"); uip_create_linklocal_allnodes_mcast(&addr); simple_udp_sendto(&broadcast_connection, "jamming", 7, &addr); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(unicast_sender_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t *addr; PROCESS_BEGIN(); SENSORS_ACTIVATE(button_sensor); // SENSORS_ACTIVATE(light_sensor); servreg_hack_init(); set_global_address(); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); //etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor); // etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); addr = servreg_hack_lookup(SERVICE_ID); if(addr != NULL) { static unsigned int button_number; char buf[50]; if(button_number == 0) { button_number = 1; leds_toggle(LEDS_ALL); } else { button_number = 0; leds_toggle(LEDS_ALL); } printf("Sending unicast to "); uip_debug_ipaddr_print(addr); printf("\n"); //sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, LUMINAIRE_ILLUMINANCE); sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, (int)(410 )); simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr); } else { printf("Service %d not found\n", SERVICE_ID); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void send_msg(struct simple_udp_connection *conn, int seconds) { uip_ipaddr_t addr; struct network_reboot_msg msg; memcpy(msg.magic_bytes, magic_bytes, sizeof(msg.magic_bytes)); msg.seconds = uip_htons(seconds_until_reboot); uip_create_linklocal_allnodes_mcast(&addr); simple_udp_sendto(conn, &msg, sizeof(msg), &addr); }
static void receiver(struct simple_udp_connection *c, const uip_ipaddr_t *sender_addr, uint16_t sender_port, const uip_ipaddr_t *receiver_addr, uint16_t receiver_port, const uint8_t *data, uint16_t datalen) { printf("HO RICEVUTO QUALCOSA!\n"); leds_toggle(LEDS_RED); simple_udp_sendto(c, "CIAO", 5, sender_addr); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_broadcast_process, ev, data) { static struct etimer periodic_timer; uip_ipaddr_t addr; PROCESS_BEGIN(); simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); #if (MIST_CONF_NETSTACK & MIST_CONF_MULTICHAN) if(HAS_MULTICHANNEL_AUTH()) { multichan_force_auth(1); } #endif /* (MIST_CONF_NETSTACK & MIST_CONF_MULTICHAN) */ #if (MIST_CONF_NETSTACK & MIST_CONF_DROWSIE_MULTICHANNEL) if(HAS_MULTICHANNEL_AUTH()) { drowsie_multichannel_force_auth(1); } #endif /* (MIST_CONF_NETSTACK & MIST_CONF_DROWSIE_MULTICHANNEL) */ while(1) { static char buf[MAX_SIZE + 1]; static int current_size = MIN_SIZE; PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); if(IS_SENDER()) { int i; current_size++; if(current_size > MAX_SIZE) { current_size = MIN_SIZE; } for(i = 0; i < current_size; i++) { buf[i] = 'a'; } buf[current_size] = '\0'; printf("TX[%02d]: '%s'\n", current_size, buf); leds_toggle(LEDS_GREEN); uip_create_linklocal_allnodes_mcast(&addr); simple_udp_sendto(&broadcast_connection, buf, current_size, &addr); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ void app_send_to(uint16_t id) { static unsigned int cnt; uint32_t seqno = ((uint32_t)node_id << 16) + cnt; printf("App: sending 0x%lx\n", seqno); orpl_set_curr_seqno(seqno); simple_udp_sendto(&unicast_connection, &seqno, sizeof(seqno), &root_ipaddr); cnt++; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(broadcast_example_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; char buf[sizeof(uip_ipaddr_t) * MAX_IP]; char* ptr; int len; struct ip_list_struct *s; PROCESS_BEGIN(); simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); list_init(ip_list); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); printf("Sending broadcast adv message\n"); // Create the message with the list of mi neighbors ptr = &buf; len = 0; for(s = list_head(ip_list); s != NULL; s = list_item_next(s)) { uip_ipaddr_copy((uip_ipaddr_t*)ptr, &s->ip); ptr += sizeof(uip_ipaddr_t); len += sizeof(uip_ipaddr_t); } // An empty message is not delivered, make some padding and sent it! if(len == 0){ buf[0] = 0; len++; } // Showtime, deliver the message uip_create_linklocal_allnodes_mcast(&addr); simple_udp_sendto(&broadcast_connection, &buf, len, &addr); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void send_stream(uip_ipaddr_t *addr, uint16_t streamno) { int i; int seqno = 0; struct msg msg; memset(&msg, 0xaa, sizeof(msg)); msg.streamno = streamno; for(i=0; i<STREAMLEN; i++) { msg.seqno = ++seqno; simple_udp_sendto(&unicast_connection, (char*)&msg, sizeof(msg), addr); } }
/*---------------------------------------------------------------------------*/ static void receiver(struct simple_udp_connection *c, const uip_ipaddr_t *sender_addr, uint16_t sender_port, const uip_ipaddr_t *receiver_addr, uint16_t receiver_port, const uint8_t *data, uint16_t datalen) { #if DEBUG printf("simple-udp-ping: receiver: len %d\n", datalen); #endif /* DEBUG */ if(datalen == DATALEN && memcmp(data, "ping", 4) == 0) { /* Send back echo */ #if DEBUG printf("Sending echo to "); uip_debug_ipaddr_print(sender_addr); printf("\n"); #endif leds_toggle(LEDS_ALL); simple_udp_sendto(&ping_connection, "echo", DATALEN, sender_addr); } else if(datalen == DATALEN && memcmp(data, "echo", 4) == 0) { struct pingconn_t* pingconn = get_pingconn(sender_addr); if(pingconn != NULL) { pingconn->replied = 1; pingconn->sent = 0; if (clock_time() - pingconn->echo_time2 > CLOCK_SECOND) { pingconn->delay = clock_time() - pingconn->echo_time2; pingconn->delay *= RTIMER_SECOND; pingconn->delay /= CLOCK_SECOND; } else { pingconn->delay = RTIMER_NOW() - pingconn->echo_time; } #if DEBUG printf("Received echo from "); uip_debug_ipaddr_print(sender_addr); printf(", delay ticks %lu\n", pingconn->delay); #endif } else { printf("warning: received echo from unknown host\n"); } } else { printf( "Error, unknown data received on port %d from port %d with length %d\n", receiver_port, sender_port, datalen); } }
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 BROADCAST----------------------------------*/ PROCESS_THREAD(broadcast_example_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; static uint8_t initialize = 1; //this is used for mote-1 only for intialization #if RANDOM_TOKEN_ERROR random_init(clock_time() % 100); #endif PROCESS_BEGIN(); simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); //printf("Sending broadcast\n"); uip_create_linklocal_allnodes_mcast(&addr); // initialize transmission by mote-1 if (1 == node_id && 1 == initialize) { token = 1; initialize = 0; // this statement will never execute again } if (token) { printf("I got the token, it's my turn\n"); simple_udp_sendto(&broadcast_connection, (uint8_t *) &node_id, 4, &addr); token = 0; //trigger an event to all service subscribers printf("RES-Event Process\n"); res_event.trigger(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(unicast_sender_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t *addr; PROCESS_BEGIN(); servreg_hack_init(); set_global_address(); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); PRINTF("SIMPLE_UNICAST_SENDER: Unicast attempt!\n"); addr = servreg_hack_lookup(SERVICE_ID); if(addr != NULL) { static unsigned int message_number; char buf[20]; printf("Sending unicast to "); uip_debug_ipaddr_print(addr); printf(" \n"); sprintf(buf, "Message %d", message_number); message_number++; simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr); } else { printf("Service %d not found\n", SERVICE_ID); } } PROCESS_END(); }
PROCESS_THREAD(snd_process, ev, data){ static struct etimer et; static struct simple_udp_connection broadcast_connection; static uip_ipaddr_t mcast_allnodes_address; PROCESS_BEGIN(); etimer_set(&et, CLOCK_SECOND*HOW_MUCH_TIME); set_address(); simple_udp_register(&broadcast_connection, UDP_PORT, NULL,UDP_PORT, callback_function); mcast_allnodes_address=get_multicast_all_nodes_addr(); while(1){//A broadcast message is sent periodically PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); simple_udp_sendto(&broadcast_connection, "Hello", 5, (const uip_ipaddr_t*)&mcast_allnodes_address); etimer_reset(&et); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(simple_udp_ping_process, ev, data) { static struct etimer et; int i; PROCESS_BEGIN(); for(i = 0; i < MAX_DESTINATIONS; i++) { pingconns[i].in_use = 0; } simple_udp_register(&ping_connection, UDP_PORT, NULL, UDP_PORT, receiver); while(1) { #define PERIOD (3*CLOCK_SECOND) etimer_set(&et, PERIOD); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); for(i = 0; i < MAX_DESTINATIONS; i++) { if (pingconns[i].in_use && pingconns[i].waiting) { struct pingconn_t* pingconn = &pingconns[i]; pingconn->waiting = 0; /* Send ping */ #if DEBUG printf("Sending ping to "); uip_debug_ipaddr_print(&pingconn->host); printf("\n"); #endif simple_udp_sendto(&ping_connection, "ping", DATALEN, &pingconn->host); pingconn->echo_time = RTIMER_NOW(); pingconn->echo_time2 = clock_time(); pingconn->sent = 1; pingconn->replied = 0; break; } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void receiver(struct simple_udp_connection *c, const uip_ipaddr_t *sender_addr, uint16_t sender_port, const uip_ipaddr_t *receiver_addr, uint16_t receiver_port, const uint8_t *data, uint16_t datalen) { C(data)[datalen] = '\0'; int id; char cmd = jsonData(C(data), "cmd")[0]; uip_ipaddr_t addr; if(cmd == 'g'){ /* GET SENSOR STATUS */ id = atoi(jsonData(C(data),"id")); sensorsList[id].status = atoi(jsonData(C(data),"st")); if(sensorsList[id].type == SENSOR_NONE) sensorsList[id].type = atoi(jsonData(C(data),"ty")); sensorsList[id].lastTime = 0; sensorsList[id].failedTimeouts = 0; updateDataToServer(C(data)); /* SEND DATA TO RASPBERRY PI */ }else if(cmd == 'c'){ id = atoi(jsonData(C(data),"id")); sensorsList[id].status = atoi(jsonData(C(data),"st")); sensorsList[id].lastTime = 0; sensorsList[id].failedTimeouts = 0; if(isAllowed(jsonData(C(data),"key")) == 0) return; uip_create_linklocal_allnodes_mcast(&addr); if(isAlarmEnabled == 0){ /* ALARM IS DISABLED */ isAlarmEnabled = 1; sprintf(string6LOWPAN,"{\"cmd\":\"l\"}"); }else{ /*ALARM IS ENABLED*/ isAlarmEnabled = 0; sprintf(string6LOWPAN,"{\"cmd\":\"u\"}"); } simple_udp_sendto(&broadcast_connection, string6LOWPAN, strlen(string6LOWPAN), &addr); updateDataToServer(C(data)); updateDataToServer(C(string6LOWPAN)); } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(broadcast_example_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; //uip_ip6addr(&addr, 0xfe80, 0, 0, 0, 0, 0, 0, 1); //uip_ip6addr(&addr, 0xfe01, 0, 0, 0, 0, 0, 0, 1); PROCESS_BEGIN(); #if PLATFORM_HAS_LEDS leds_off(LEDS_ALL); #endif simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); uip_create_linklocal_allnodes_mcast(&addr); #if PLATFORM_HAS_LEDS leds_on(LEDS_ALL); #endif /*printf("Sending broadcast from: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x on port %d\n", addr.u16[0],addr.u16[1],addr.u16[2],addr.u16[3],addr.u16[4],addr.u16[5],addr.u16[6],addr.u16[7], UDP_PORT);*/ printf("Sending broadcast from: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x on port %d\n", addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7], addr.u8[8],addr.u8[9],addr.u8[10],addr.u8[11],addr.u8[12],addr.u8[13],addr.u8[14],addr.u8[15], UDP_PORT); simple_udp_sendto(&broadcast_connection, "Test", 4, &addr); #if PLATFORM_HAS_LEDS leds_off(LEDS_ALL); #endif } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ void app_send_to(uint16_t id) { static unsigned int cpt; struct app_data data; uip_ipaddr_t dest_ipaddr; data.seqno = ((uint32_t)node_id << 16) + cpt; data.src = node_id; data.dest = id; data.hop = 0; data.fpcount = 0; node_ip6addr(&dest_ipaddr, id); printf("App: sending"); rpl_trace(&data); *((struct app_data*)buf) = data; simple_udp_sendto(&unicast_connection, buf, sizeof(buf) + 1, &dest_ipaddr); cpt++; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(sender_process, ev, data) { PROCESS_BEGIN(); static uip_ipaddr_t ipaddr, unicastaddr; int i; uint8_t state; static struct etimer timer; static struct simple_udp_connection connection; static char *packet = "Supertest"; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); uip_ip6addr(&unicastaddr, 0xfe80, 0, 0, 0, 0xc30c, 0, 0, 2); simple_udp_register(&connection, UDP_PORT, NULL, UDP_PORT, NULL); printf("IPv6 addresses: "); for(i = 0; i < UIP_DS6_ADDR_NB; i++) { state = uip_ds6_if.addr_list[i].state; if(uip_ds6_if.addr_list[i].isused && (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) { uip_debug_ipaddr_print( &uip_ds6_if.addr_list[i].ipaddr); printf("\n"); } } etimer_set(&timer, CLOCK_SECOND); while(1) { printf("STO MANDANDO UNICAST\n"); simple_udp_sendto(&connection, packet, strlen(packet)+1, &unicastaddr); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer)); etimer_restart(&timer); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(sender_node_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; PROCESS_BEGIN(); set_global_address(); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); uip_ip6addr(&addr, 0xaaaa, 0, 0, 0, 0x0201, 0x001, 0x001, 0x001); { static unsigned int message_number; char buf[20]; printf("Sending unicast to "); uip_debug_ipaddr_print(&addr); printf("\n"); sprintf(buf, "Message %d", message_number); message_number++; simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, &addr); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void receiver(struct simple_udp_connection *c, const uip_ipaddr_t *sender_addr, uint16_t sender_port, const uip_ipaddr_t *receiver_addr, uint16_t receiver_port, const uint8_t *data, uint16_t datalen) { clock_time_t recv_time = clock_time(); struct broadcast_message *msg; msg = (struct broadcast_message *)data; printf("Anchor: Data received with length %d : st %lu\n", datalen, msg->sent_time ); unsigned long ll = (unsigned long)recv_time; msg->recv_time = ll; msg->anchorid = node_id; simple_udp_sendto(&unicast_connection, msg, sizeof(struct broadcast_message), receiver_addr); printf("Anchor: Msg Back to %d: %lu\n", msg->nodeid, msg->recv_time); }
/*---------------------------------------------------------------------------*/ void app_send_to(uint16_t id) { static unsigned int cnt; struct app_data data; uip_ipaddr_t dest_ipaddr; data.magic = ORPL_LOG_MAGIC; data.seqno = ((uint32_t)node_id << 16) + cnt; data.src = node_id; data.dest = id; data.hop = 0; data.fpcount = 0; ORPL_LOG_FROM_APPDATAPTR(&data, "App: sending"); #if WITH_ORPL orpl_set_curr_seqno(data.seqno); #endif /* WITH_ORPL */ set_ipaddr_from_id(&dest_ipaddr, id); simple_udp_sendto(&unicast_connection, &data, sizeof(data), &dest_ipaddr); cnt++; }
/*simple-udp-rpl---------------------------------------------------------------------------*/ PROCESS_THREAD(unicast_sender_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t *addr; PROCESS_BEGIN(); //servreg_hack_init(); // set_global_address(); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); #ifdef WITH_STABLETIMER if(stable_flag){ #else //WITH_STABLETIMER if(tsch_is_associated){ #endif //WITH_STABLETIMER /*--- target address decision ---*/ /*-- to registered target with servreg_hack --*/ //addr = servreg_hack_lookup(SERVICE_ID); /*-- to default route --*/ //uip_ds6_defrt_t *default_route; //default_route = uip_ds6_defrt_lookup(uip_ds6_defrt_choose()); //if(default_route != NULL) addr = &default_route->ipaddr; //else addr = NULL; /*-- decide by address directory--*/ uip_ipaddr_t temp_ipaddr; uip_ip6addr(&temp_ipaddr,0xfd00,0,0,0,0xc30c,0,0,1); addr = &temp_ipaddr; /*-- linklocal rplnodes mcast --*/ //uip_ipaddr_t temp_ipaddr; //uip_ip6addr(&temp_ipaddr, 0xff02,0,0,0,0,0,0,0x001a); //addr = &temp_ipaddr; /*-- to default parent --*/ //addr = rpl_get_parent_ipaddr(default_instance->current_dag->preferred_parent); /*--- sending ---*/ if(addr != NULL) { static unsigned int message_number; char buf[20]; #ifdef WITH_LEAPFROG sprintf(buf, "%c%cHello Tada %04d", LEAPFROG_DATA_HEADER, leapfrog_data_counter + LEAPFROG_BEACON_OFFSET, message_number); leapfrog_data_counter++; if(leapfrog_data_counter > LEAPFROG_DATA_COUNTER_MAX) leapfrog_data_counter = 0; #else sprintf(buf, "Hello TadaMatz %d", message_number); #endif printf("DATA: Sending unicast to "); uip_debug_ipaddr_print(addr); printf(" '"); printf(buf); printf("'\n"); message_number++; simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr); } else { printf("DATA: addr is NULL!!"); } } //if(tsch_is_associated) } PROCESS_END(); } /* ----------------- simple-udp-rpl process end ----------------- */ /* ----------------- simple-udp-rpl process end ----------------- */ #ifdef WITH_LEAPFROG /* ----------------- leapfrog process start----------------- */ PROCESS_THREAD(leapfrog_beaconing_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t *addr; PROCESS_BEGIN(); simple_udp_register(&leapfrog_unicast_connection, LEAPFROG_UDP_PORT, NULL, LEAPFROG_UDP_PORT, receiver); etimer_set(&periodic_timer, LEAPFROG_SEND_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); etimer_set(&send_timer, LEAPFROG_SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); if(tsch_is_associated){ /*--- target address decision ---*/ /*-- linklocal rplnodes mcast --*/ uip_ipaddr_t temp_ipaddr; uip_ip6addr(&temp_ipaddr, 0xff02,0,0,0,0,0,0,0x001a); addr = &temp_ipaddr; /*--- sending ---*/ if(addr != NULL) { static unsigned int message_number; char buf[20]; char possible_parent_str[1 + LEAPFROG_NUM_NEIGHBOR_NODE]; possible_parent_str[0] = leapfrog_possible_parent_num + LEAPFROG_BEACON_OFFSET; int i; for(i = 0; i < leapfrog_possible_parent_num; i++){ possible_parent_str[1 + i] = leapfrog_possible_parent_id_array[i] + LEAPFROG_BEACON_OFFSET; } sprintf(buf, "%cP%cG%cA%cC%sN%d", LEAPFROG_BEACON_HEADER, leapfrog_parent_id + LEAPFROG_BEACON_OFFSET, leapfrog_grand_parent_id + LEAPFROG_BEACON_OFFSET, leapfrog_alt_parent_id + LEAPFROG_BEACON_OFFSET, possible_parent_str, //C for candidate message_number); printf("LEAPFROG: Sending beacon to "); uip_debug_ipaddr_print(addr); printf(" '"); printf(buf); printf("'\n"); message_number++; simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr); //simple_udp_sendto(&unicast_connection, buf, cnt, addr); } else { printf("LEAPFROG: addr is null!!"); } } //if(tsch_is_associated) } PROCESS_END(); } /* ----------------- leapfrog process end ----------------- */ #endif /*WITH_LEAPFROG*/ #ifdef WITH_STABLETIMER /* ----------------- stable_timer process start ----------------- */ PROCESS_THREAD(stable_timer_process, ev, data) { static struct etimer stable_timer; PROCESS_BEGIN(); etimer_set(&stable_timer, CLOCK_SECOND * 60 * 15); //15min printf("Set Stable timer\n"); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&stable_timer)); etimer_stop(&stable_timer); stable_flag = 1; printf("Stable timer expired!! Start to send application traffic\n"); PROCESS_EXIT(); PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(check_sensor_status, ev, data) { uip_ipaddr_t addr; static struct etimer periodic_timer; int i=0; char globalStatus=0; PROCESS_BEGIN(); for(i=0;i<20;i++){ sensorsList[i].type = SENSOR_NONE; sensorsList[i].status = STATUS_OK; sensorsList[i].lastTime = 0; sensorsList[i].failedTimeouts = 0; } sensorsList[0].type = SENSOR_PIR; sensorsList[1].type = SENSOR_CHOCK; sensorsList[2].type = SENSOR_PIR; sensorsList[3].type = SENSOR_RFID; DDRB = 0x00; PORTB = 0x00; //sensorsList[0].type = SENSOR_PIR; //sensorsList[1].type = SENSOR_PIR; //sensorsList[2].type = SENSOR_CHOCK; //rs232_init(RS232_PORT_0, USART_BAUD_19200, USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); //rs232_redirect_stdout(RS232_PORT_0); //rs232_send(RS232_PORT_n, char c); // OR FOR STRINGS rs232_print(char *); rs232_set_input(RS232_PORT_0, uart0_callback); uart0_setLineCallback(baseReadyUART); rs232_init(RS232_PORT_1, USART_BAUD_9600, USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); rs232_set_input(RS232_PORT_1, uart1_callback); uart1_setLineCallback(lineReadyUART1); simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&periodic_timer, CHECK_INTERVAL); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); for(i=0;i<20;i++){ if(sensorsList[i].type != 0){ sensorsList[i].lastTime++; if(sensorsList[i].lastTime == 11){ /* SECONDS / 2 => *0->11* 6 seconds without infos */ sensorsList[i].failedTimeouts++; sensorsList[i].lastTime = 0; uip_create_linklocal_allnodes_mcast(&addr); sprintf(string6LOWPAN,"{\"cmd\":\"s\",\"id\":\"%d\"}",i); simple_udp_sendto(&broadcast_connection, string6LOWPAN, strlen(string6LOWPAN), &addr); if(sensorsList[i].failedTimeouts == 3){ sensorsList[i].failedTimeouts = 0; sensorsList[i].status = STATUS_TIMEOUT; printf("{\"cmd\":\"g\",\"id\":\"%d\",\"st\":\"%d\",\"ty\":\"%d\"}\n", i, sensorsList[i].status, sensorsList[i].type); } } } } } PROCESS_END(); }
static void send_data(const uip_ipaddr_t *addr){ simple_udp_sendto(&udp_connection, SECRET , MAX_PAYLOAD_LEN,addr); }
/* this process handle the reception of messages */ PROCESS_THREAD(delugeGroupP, ev, data) { int fd; char* buf; uint8_t nr_pages_local; static struct etimer et; static struct jsonparse_state jsonState; static ContainerRoot * newModel; static uip_ipaddr_t addr; PROCESS_BEGIN(); /* keep track of the singleton instance */ instance = (DelugeGroup*)data; /* register new event types */ NEW_AVAILABLE_OA_MODEL = process_alloc_event(); NEW_OA_MODEL_DOWNLOADED = process_alloc_event(); /* initialize model announcement's system */ simple_udp_register(&deluge_group_broadcast, 34555, NULL, 34555, model_version_recv); /* set announcement's initial value*/ instance->info.version = 0; instance->info.nr_pages = 0; /* set timer for announcements */ etimer_set(&et, CLOCK_SECOND * instance->interval); while (1) { /* Listen for announcements every interval seconds. */ PROCESS_WAIT_EVENT(); if (ev == PROCESS_EVENT_TIMER) { /* announce my model */ uip_create_linklocal_allnodes_mcast(&addr); simple_udp_sendto(&deluge_group_broadcast, &instance->info, sizeof(struct ModelInfo), &addr); etimer_restart(&et); } else if (ev == NEW_AVAILABLE_OA_MODEL){ /* receive the new over the air model */ /* contains the number of pages */ nr_pages_local = instance->info.nr_pages; /* create the file with the required number of pages */ cfs_remove(instance->fileNameWithModel); fd = cfs_open(instance->fileNameWithModel, CFS_WRITE); buf = (char*) malloc(S_PAGE); memset(buf, '0' , S_PAGE); PRINTF("Number of pages is %d\n", nr_pages_local); while(nr_pages_local) { cfs_seek(fd, 0, CFS_SEEK_END); cfs_write(fd, buf, S_PAGE); nr_pages_local--; } free(buf); cfs_close(fd); /* Deluge-based dissemination */ if (deluge_disseminate(instance->fileNameWithModel, 0, modelDownloaded)) { PRINTF("ERROR: some problem waiting for new version of the file\n"); } else { PRINTF("INFO: Waiting for new version of the file \n"); } } else if (ev == NEW_OA_MODEL_DOWNLOADED) { /* deserialize the model received over the air */ PRINTF("New model %s received in group with instance %p\n", instance->fileNameWithModel, instance); newModel = NULL; /* TODO: check if the file exists */ /* parse model from json file */ jsonparse_setup(&jsonState, instance->fileNameWithModel); newModel = JSONKevDeserializer(&jsonState, jsonparse_next(&jsonState)); cfs_close(jsonState.fd); PRINTF("INFO: Deserialization finished in Deluge Group %p\n", newModel); /* save a reference to the new model */ instance->lastReceivedModel = newModel; /* Afterwards, just call notifyNewModel */ if (newModel != NULL && notifyNewModel(newModel) == PROCESS_ERR_OK) { PRINTF("INFO: Model was successfully sent\n"); } else { PRINTF("ERROR: The model cannot be loaded!\n"); } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(broadcast_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; static unsigned int entry_number; /*----profiles defined-----*/ const char *profile1 = "{\"profile\": {\"id\": \"432\", \"T\": \"19\", \"C\": \"4500\", \"I\": \"400\", \"L\": \"0\", \"weight\": [ {\"AT\": \"0.30\"}, {\"AC\": \"0.25\"}, {\"AI\": \"0.10\"}, {\"AL\": \"0.35\"} ] } }"; const char *profile2 = "{\"profile\": {\"id\": \"235\", \"T\": \"22\", \"C\": \"4300\", \"I\": \"460\", \"L\": \"1\", \"weight\": [ {\"AT\": \"0.10\"}, {\"AC\": \"0.15\"}, {\"AI\": \"0.15\"}, {\"AL\": \"0.60\"} ] } }"; const char *profile3 = "{\"profile\": {\"id\": \"679\", \"T\": \"20\", \"C\": \"4460\", \"I\": \"340\", \"L\": \"1\", \"weight\": [ {\"AT\": \"0.30\"}, {\"AC\": \"0.30\"}, {\"AI\": \"0.20\"}, {\"AL\": \"0.20\"} ] } }"; const char *profile4 = "{\"profile\": {\"id\": \"614\", \"T\": \"19\", \"C\": \"4800\", \"I\": \"370\", \"L\": \"1\", \"weight\": [ {\"AT\": \"0.25\"}, {\"AC\": \"0.25\"}, {\"AI\": \"0.25\"}, {\"AL\": \"0.25\"} ] } }"; const char *profile5 = "{\"profile\": {\"id\": \"918\", \"T\": \"25\", \"C\": \"5500\", \"I\": \"480\", \"L\": \"0\", \"weight\": [ {\"AT\": \"0.20\"}, {\"AC\": \"0.30\"}, {\"AI\": \"0.15\"}, {\"AL\": \"0.35\"} ] } }"; const char *profile6 = "{\"profile\": {\"id\": \"019\", \"T\": \"27\", \"C\": \"5800\", \"I\": \"420\", \"L\": \"1\", \"weight\": [ {\"AT\": \"0.30\"}, {\"AC\": \"0.20\"}, {\"AI\": \"0.40\"}, {\"AL\": \"0.10\"} ] } }"; const char *profile7 = "{\"profile\": {\"id\": \"203\", \"T\": \"26\", \"C\": \"3500\", \"I\": \"470\", \"L\": \"0\", \"weight\": [ {\"AT\": \"0.60\"}, {\"AC\": \"0.20\"}, {\"AI\": \"0.15\"}, {\"AL\": \"0.05\"} ] } }"; const char *profile8 = "{\"profile\": {\"id\": \"485\", \"T\": \"25\", \"C\": \"6000\", \"I\": \"430\", \"L\": \"1\", \"weight\": [ {\"AT\": \"0.30\"}, {\"AC\": \"0.25\"}, {\"AI\": \"0.15\"}, {\"AL\": \"0.30\"} ] } }"; const char *profile9 = "{\"profile\": {\"id\": \"459\", \"T\": \"23\", \"C\": \"5600\", \"I\": \"330\", \"L\": \"0\", \"weight\": [ {\"AT\": \"0.35\"}, {\"AC\": \"0.35\"}, {\"AI\": \"0.10\"}, {\"AL\": \"0.20\"} ] } }"; const char *profile10 = "{\"profile\": {\"id\": \"570\", \"T\": \"22\", \"C\": \"3900\", \"I\": \"370\", \"L\": \"1\", \"weight\": [ {\"AT\": \"0.45\"}, {\"AC\": \"0.45\"}, {\"AI\": \"0.05\"}, {\"AL\": \"0.05\"} ] } }"; const char *profile_default = "{ profile empty }"; uip_ipaddr_t addr; PROCESS_BEGIN(); SENSORS_ACTIVATE(button_sensor); simple_udp_register(&broadcast_connection, UDP_PORT, NULL, UDP_PORT, receiver); //etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { /*-----button pressed: generate a new profile-----*/ // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor); // etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); char buf[152]; int i; entry_number++; switch(entry_number){ case 1: for(i = 0; i < 152; i++){ buf[i] = *(profile1++); printf("%c", *profile1); } break; case 2: for(i = 0; i < 152; i++){ buf[i] = *(profile2++); printf("%c", *profile2); } break; case 3: for(i = 0; i < 152; i++){ buf[i] = *(profile3++); printf("%c", *profile3); } break; case 4: for(i = 0; i < 152; i++){ buf[i] = *(profile4++); printf("%c", *profile4); } break; case 5: for(i = 0; i < 152; i++){ buf[i] = *(profile5++); printf("%c", *profile5); } break; case 6: for(i = 0; i < 152; i++){ buf[i] = *(profile6++); printf("%c", *profile6); } break; case 7: for(i = 0; i < 152; i++){ buf[i] = *(profile7++); printf("%c", *profile7); } break; case 8: for(i = 0; i < 152; i++){ buf[i] = *(profile8++); printf("%c", *profile8); } break; case 9: for(i = 0; i < 152; i++){ buf[i] = *(profile9++); printf("%c", *profile9); } break; case 10: for(i = 0; i < 152; i++){ buf[i] = *(profile10++); printf("%c", *profile10); } break; default: printf("No more profile.\n"); for(i = 0; i < 152; i++){ buf[i] = *(profile_default++); printf("%c", *profile_default); } break; } printf("Sending broadcast\n"); uip_create_linklocal_allnodes_mcast(&addr); simple_udp_sendto(&broadcast_connection, buf, strlen(buf) + 1, &addr); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(unicast_sender_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t *addr; PROCESS_BEGIN(); SENSORS_ACTIVATE(button_sensor); // SENSORS_ACTIVATE(light_sensor); servreg_hack_init(); set_global_address(); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); rx_start_duration = energest_type_time(ENERGEST_TYPE_LISTEN); //etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor); // etimer_reset(&periodic_timer); etimer_set(&send_timer, SEND_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); addr = servreg_hack_lookup(SERVICE_ID); if(addr != NULL) { static unsigned int button_number; char buf[50]; if(button_number == 0) { button_number = 1; leds_toggle(LEDS_ALL); } else { button_number = 0; leds_toggle(LEDS_ALL); } printf("Sending unicast to "); uip_debug_ipaddr_print(addr); printf("\n"); //sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, LUMINAIRE_ILLUMINANCE); sprintf(buf, "Occupancy Detector: %d, Luminous Intensity: %d", button_number, /*(int)(random() % 180 + 320 )*/ 1500 ); simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr); // printf("energy rx: %lu\n", energest_type_time(ENERGEST_TYPE_LISTEN) - rx_start_duration); //printf("energy tx: %lu\n", energest_type_time(ENERGEST_TYPE_TRANSMIT) - rx_start_duration); //printf("cpu: %lu\n", energest_type_time(ENERGEST_TYPE_CPU) - rx_start_duration); //printf("lpm: %lu\n", energest_type_time(ENERGEST_TYPE_LPM) - rx_start_duration); } else { printf("Service %d not found\n", SERVICE_ID); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void receiver(struct simple_udp_connection *c, const uip_ipaddr_t *sender_addr, uint16_t sender_port, const uip_ipaddr_t *receiver_addr, uint16_t receiver_port, const uint8_t *data, uint16_t datalen) { static int cl_temp; static int cl_sec; unsigned int clock; unsigned int second; double temp; //printf("recieved msg at clock time: %d \n",clock_time()- offset - prop_delay); //printf( "%d \n",clock_time()- offset - prop_delay); TWAMPtimestamp ts_rcv; clock = clock_time(); second = clock/CLOCK_SECOND; printf("RECEIVE AT: clock: %d || second: %d \n",clock,second); //temp = (double) (clock - start_time)/CLOCK_SECOND - second; temp = (double) (clock)/CLOCK_SECOND - second; ts_rcv.Second = second; ts_rcv.Fraction = temp * 1000; printf("Data received from "); uip_debug_ipaddr_print(sender_addr); printf(" on port %d from port %d \n", receiver_port, sender_port); printf("Datalen = %d\n",datalen); SenderUAuthPacket sender_pkt; memset(&sender_pkt, 0, sizeof(sender_pkt)); memcpy(&sender_pkt, data, datalen); ReflectorUAuthPacket reflect_pkt; //printf("Paket size = %d\n", sizeof(reflect_pkt)); reflect_pkt.SeqNo = seqno; reflect_pkt.ErrorEstimate = 999; reflect_pkt.ReceiverTimestamp = ts_rcv; reflect_pkt.SenderSeqNo = sender_pkt.SeqNo; reflect_pkt.SenderTimestamp = sender_pkt.Timestamp; reflect_pkt.SenderErrorEstimate = sender_pkt.ErrorEstimate; reflect_pkt.SenderTTL = 255; clock = clock_time(); second = clock/CLOCK_SECOND; temp = (double) (clock)/CLOCK_SECOND - second; reflect_pkt.Timestamp.Second = second; reflect_pkt.Timestamp.Fraction = temp * 1000; simple_udp_sendto(&unicast_connection, &reflect_pkt, sizeof(reflect_pkt), sender_addr); printf("Packet reflected to:\n"); uip_debug_ipaddr_print(sender_addr); printf("\n#################\n"); //Prints for debugging printf("SeqNo: %"PRIu32"\n", sender_pkt.SeqNo); printf("Seconds: %"PRIu32"\n", sender_pkt.Timestamp.Second); printf("Micro: %"PRIu32"\n", sender_pkt.Timestamp.Fraction); printf("Error: %"PRIu16"\n", sender_pkt.ErrorEstimate); current++; }