/** * add the runicast message to the runicast buffer */ void add_to_RU_list (Runicast_msg runicast_msg){ if(list_length(runicast_list)>MAX_MSG_ENTRY-1){ memb_free(&runicast_list_memb, list_chop(runicast_list)); } Ru_msg_list *runicast_entry; runicast_entry = memb_alloc(&runicast_list_memb); printf("<alloc mem> runicast_entry alloc mem\n"); if(runicast_entry == NULL){ printf("runicast can alloc mem\n"); list_chop(runicast_list); } //memset(runicast_entry,0, sizeof(Ru_msg_list)); rimeaddr_copy(&runicast_entry->runicast_msg.last_hop,&rimeaddr_node_addr); runicast_entry->runicast_msg.native_sen_type = runicast_msg.native_sen_type; runicast_entry->runicast_msg.seqno = runicast_msg.seqno; rimeaddr_copy(&runicast_entry->runicast_msg.source,&runicast_msg.source); // runicast_entry->runicast_msg.timestamp = runicast_msg.timestamp; runicast_entry->runicast_msg.value = runicast_msg.value; list_add(runicast_list,runicast_entry); }
static void recv_runicast(struct runicast_conn *c, const rimeaddr_t *from, uint8_t seqno) { struct history_entry *e = NULL; for (e = list_head(history_table); e != NULL; e = e->next) { if (rimeaddr_cmp(&e->addr, from)) break; } if (e == NULL) { e = memb_alloc(&history_mem); if (e == NULL) e = list_chop(history_table); rimeaddr_copy(&e->addr, from); e->seq = seqno; list_push(history_table, e); } else { if (e->seq == seqno) { /* printf("Runicast message received from %d.%d, seqno %d (DUPLICATE)\n", from->u8[0], from->u8[1], seqno); */ return; } e->seq = seqno; } uint16_t cur_time = clock_time()/CLOCK_SECOND; // printf("%d\n%d\n%s\n", from->u8[0], cur_time, (char *)packetbuf_dataptr()); /* Receiving a message triggers the next process in the sequence to begin */ if (rimeaddr_node_addr.u8[0] != SINK_NODE) { sleep_time = atoi(packetbuf_dataptr()); process_exit(&node_timeout_process); process_start(&node_read_process, NULL); } else { char received_string[10]; strcpy(received_string, packetbuf_dataptr()); uint8_t counter = 0; for (counter = 0; counter < 10; counter++) if (received_string[counter] == '!') break; for (counter; counter < 10; counter++) received_string[counter] = '\0'; printf("DATA %d %d %s\n", from->u8[0], cur_time, received_string); } }
static void recv_runicast(struct runicast_conn *c, const linkaddr_t *from, uint8_t seqno) { struct history_entry *e = NULL; int16_t data, id; for(e = list_head(history_table); e != NULL; e = e->next) { if(linkaddr_cmp(&e->addr, from)) { break; } } if(e == NULL) { /* Create new history entry */ e = memb_alloc(&history_mem); if(e == NULL) { e = list_chop(history_table); /* Remove oldest at full history */ } linkaddr_copy(&e->addr, from); e->seq = seqno; list_push(history_table, e); } else { /* Detect duplicate callback */ if(e->seq == seqno) { printf("runicast message received from %d.%d, seqno %d (DUPLICATE)\n", from->u8[0], from->u8[1], seqno); } /* Update existing history entry */ e->seq = seqno; } printf("Basestation: runicast message received from %d.%d, seqno %d\n", from->u8[0], from->u8[1], seqno); struct runicast_message *received_msg = packetbuf_dataptr(); if (received_msg->type == RUNICAST_TYPE_TEMP) { time_delay = received_msg->data; printf("Temperature from actuator %d has value %d",id,data) }
/*---------------------------- hop count ----------------------------------*/ int c_route_hc_add(const rimeaddr_t *dest, const rimeaddr_t *nexthop, uint8_t cost, uint8_t seqno, uint8_t lqi, struct pipe* p) { struct c_route_entry *e; /* Avoid inserting duplicate entries. */ e = c_route_lookup(dest); if(e != NULL && rimeaddr_cmp(&e->nexthop, nexthop)) { list_remove(p->route_table, e); printf("route_add: removing DUPLICATE entry to %d.%d with nexthop %d.%d and cost %d\n", e->dest.u8[0], e->dest.u8[1], e->nexthop.u8[0], e->nexthop.u8[1], e->cost); } else { /* Allocate a new entry or reuse the oldest entry with highest cost. */ e = memb_alloc(&p->route_mem); if(e == NULL) { /* Remove oldest entry. XXX */ e = list_chop(p->route_table); printf("route_add: removing entry to %d.%d with nexthop %d.%d and cost %d\n", e->dest.u8[0], e->dest.u8[1], e->nexthop.u8[0], e->nexthop.u8[1], e->cost); } } rimeaddr_copy(&e->dest, dest); rimeaddr_copy(&e->nexthop, nexthop); e->cost = cost; e->seqno = seqno; e->time = 0; e->decay = 0; /* New entry goes first. */ list_push(p->route_table, e); printf("route_add: new entry to %d.%d with nexthop %d.%d and cost %d\n", e->dest.u8[0], e->dest.u8[1], e->nexthop.u8[0], e->nexthop.u8[1], e->cost); c_route_output(p); return 0; }
void conditionalFQDiscard() { pmesg(200, "%s :: %s :: Line #%d\n", __FILE__, __func__, __LINE__); static fe_queue_entry_t* discardQe;// = memb_alloc(&send_stack_mem); if(list_length(send_stack) >= SEND_STACK_SIZE) { discardQe = list_chop(send_stack); list_remove(message_pool, discardQe -> msg); memb_free(&message_pool_mem, discardQe -> msg); list_remove(q_entry_pool, discardQe); memb_free(&q_entry_pool_mem, discardQe); #ifdef VIRTQ // Dropped a data packet, increase virtual queue size virtualQueueSize++; #endif } }
/*---------------------------------------------------------------------------*/ int route_add(const linkaddr_t *dest, const linkaddr_t *nexthop, uint8_t cost, uint8_t seqno) { struct route_entry *e; /* Avoid inserting duplicate entries. */ e = route_lookup(dest); if(e != NULL && linkaddr_cmp(&e->nexthop, nexthop)) { list_remove(route_table, e); } else { /* Allocate a new entry or reuse the oldest entry with highest cost. */ e = memb_alloc(&route_mem); if(e == NULL) { /* Remove oldest entry. XXX */ e = list_chop(route_table); PRINTF("route_add: removing entry to %d.%d with nexthop %d.%d and cost %d\n", e->dest.u8[0], e->dest.u8[1], e->nexthop.u8[0], e->nexthop.u8[1], e->cost); } } linkaddr_copy(&e->dest, dest); linkaddr_copy(&e->nexthop, nexthop); e->cost = cost; e->seqno = seqno; e->time = 0; e->decay = 0; /* New entry goes first. */ list_push(route_table, e); PRINTF("route_add: new entry to %d.%d with nexthop %d.%d and cost %d\n", e->dest.u8[0], e->dest.u8[1], e->nexthop.u8[0], e->nexthop.u8[1], e->cost); return 0; }
/** * add broadcast entry in the broadcast buffer, if the message type exits , the message will be updated, if * not exist, a new entry to the broadcast buffer will be created */ void add_broadcast_entry(uint8_t native_sen_type, uint8_t msg_type, uint8_t frequency, uint8_t sub_type ,rimeaddr_t last_hop,rimeaddr_t source,rimeaddr_t base_station,uint8_t hops,uint8_t condition,uint8_t assemble_count, uint32_t timestamp) { //printf("in add broadcast entry -> msg_type ->%u\n",msg_type); bool find = false; struct Br_msg_list *br_msg_list; if (list_length(broadcast_list) > 0) { for (br_msg_list = list_head(broadcast_list); br_msg_list != NULL; br_msg_list = br_msg_list->next) { if (br_msg_list->broadcast_msg.subscribe_type == sub_type&&br_msg_list->broadcast_msg.create_timestamp<timestamp) { update_br_entry(br_msg_list, local_node.sen_type, msg_type, frequency, sub_type, last_hop, source, base_station, hops, condition, assemble_count,timestamp); printf("[UPDATE_BR_ENTRY]: sub_type->%u native_type->%u\n", br_msg_list->broadcast_msg.subscribe_type, br_msg_list->broadcast_msg.native_sen_type); find = true; break; } } } if (!find) { if (list_length(broadcast_list) > MAX_MSG_ENTRY - 1) { memb_free(&broadcast_list_memb, list_chop(broadcast_list)); printf("[BUF FULL]chop msg\n"); } Br_msg_list *temp; temp = create_broadcast_msg(local_node.sen_type, msg_type, frequency, sub_type, last_hop, source, base_station, hops, condition, assemble_count,clock_seconds()); if (temp != NULL) { list_add(broadcast_list, temp); printf("[ADD-TO-BRLIST] sub type %u list length %u\n", temp->broadcast_msg.subscribe_type, list_length(broadcast_list)); print_br_list(); } } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(rpl_forwarder_set_update_process, ev, instance) { static rpl_rank_t best_rank; static rpl_rank_t best_forwarder_rank; static rpl_rank_t curr_rank; static rpl_forwarder_set_member_t *curr_forwarder; static rpl_forwarder_set_member_t *next_forwarder; static rpl_forwarder_set_member_t *best_forwarder; static rpl_forwarder_set_member_t *last_forwarder; static nbr_table_item_t *nbr_table_parent; static uint8_t *membership_status; PROCESS_BEGIN(); while(1) { PROCESS_WAIT_EVENT(); if(ev == PROCESS_EVENT_CONTINUE) { nbr_table_parent = nbr_table_head(rpl_parents); best_rank = 0xFFFF; if(nbr_table_parent != NULL) { list_init(forwarder_set_list); list_init(current_set_list); /* Build a list of potential forwarders */ int is_potential_forwarder; PRINTF("RPL: Potential forwarders: "); do { is_potential_forwarder = 0; curr_forwarder = memb_alloc(&forwarder_set_memb); if(curr_forwarder == NULL) { PRINTF("RPL: unable to allocate forwarder set member\n"); return 0; } curr_forwarder->forwarder = (rpl_parent_t *)nbr_table_parent; if(((rpl_instance_t *)instance)->of->parent_path_cost(curr_forwarder->forwarder) < 0xFFFF) { list_add(forwarder_set_list, curr_forwarder); PRINTF("%u ", rpl_get_parent_lladdr(curr_forwarder->forwarder)->u8[7]); is_potential_forwarder = 1; } membership_status = (uint8_t *)nbr_table_add_lladdr(forwarder_set, rpl_get_parent_lladdr(curr_forwarder->forwarder), NBR_TABLE_REASON_UNDEFINED, NULL); if(membership_status == NULL) { PRINTF("RPL: error setting membership status in forwarder set\n"); return 0; } else { *membership_status = 0; } if(!is_potential_forwarder) { memb_free(&forwarder_set_memb, curr_forwarder); } nbr_table_parent = nbr_table_next(rpl_parents, nbr_table_parent); } while(nbr_table_parent != NULL); PRINTF("\n"); last_forwarder = NULL; /* Try all incremental combinations in ascending order according to rank */ while(list_head(forwarder_set_list) != NULL) { next_forwarder = list_head(forwarder_set_list); best_forwarder = next_forwarder; best_forwarder_rank = ((rpl_instance_t *)instance)->of->parent_path_cost(best_forwarder->forwarder); /* Instead of sorting, we'll go through and find the best forwarder each time */ do { curr_forwarder = next_forwarder; curr_rank = ((rpl_instance_t *)instance)->of->parent_path_cost(curr_forwarder->forwarder); if(curr_rank < best_forwarder_rank) { best_forwarder = curr_forwarder; best_forwarder_rank = curr_rank; } next_forwarder = list_item_next(curr_forwarder); } while(next_forwarder != NULL); /* Add the best forwarder to the current list and remove it from the candidate list */ //PRINTF("RPL: adding %u with rank %u\n", rpl_get_parent_lladdr(best_forwarder->forwarder)->u8[7], best_forwarder_rank); list_remove(forwarder_set_list, best_forwarder); list_add(current_set_list, best_forwarder); /* Find the routing metric with the current list and compare to the best so far */ curr_rank = ((rpl_instance_t *)instance)->of->rank_via_set(list_head(current_set_list)); //PRINTF("RPL: resulting rank %u, current best rank %u\n", curr_rank, best_rank); if(curr_rank <= best_rank) { best_rank = curr_rank; last_forwarder = list_tail(current_set_list); } } /* Trim the list back to the best combination. */ if(last_forwarder != NULL) { while(list_head(current_set_list) != NULL) { curr_forwarder = list_chop(current_set_list); if(curr_forwarder == last_forwarder) { list_add(current_set_list, last_forwarder); break; } memb_free(&forwarder_set_memb, curr_forwarder); } if(list_head(current_set_list) == NULL) { PRINTF("RPL: error trimming forwarder set list\n"); } PRINTF("RPL: best rank %u\n", best_rank); #if DEBUG PRINTF("RPL: best set "); next_forwarder = list_head(current_set_list); do { curr_forwarder = next_forwarder; PRINTF("%u ", rpl_get_parent_lladdr(curr_forwarder->forwarder)->u8[7]); next_forwarder = list_item_next(curr_forwarder); } while(next_forwarder != NULL); PRINTF("\n"); #endif /* Now we have the best combination - store it in a neighbor table */ while(list_head(current_set_list) != NULL) { curr_forwarder = list_pop(current_set_list); membership_status = nbr_table_get_from_lladdr(forwarder_set, rpl_get_parent_lladdr(curr_forwarder->forwarder)); *membership_status = 1; memb_free(&forwarder_set_memb, curr_forwarder); } } else { PRINTF("RPL: empty forwarder set\n"); } } PRINTF("RPL: updating opp routing rank from %d to %d\n", current_rank, best_rank); if(best_rank != 0xFFFF) { current_rank = best_rank; } else { current_rank = INFINITE_RANK; } } } PROCESS_END(); }
/** * remove all of the runicast message buffered */ void remove_all_runicast_entry(){ while(list_length(runicast_list)>0){ list_chop(runicast_list); } };
void remove_all_br_entry(){ while(list_length(broadcast_list)>0){ memb_free(&broadcast_list_memb,list_chop(broadcast_list)); } }