/* ************************************************** */
int set_header(call_t *c, packet_t *packet, destination_t *dst) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct neighbor *n_hop = get_nexthop(c, &(dst->position));
    destination_t destination;
    struct routing_header *header = (struct routing_header *) (packet->data + nodedata->overhead);
    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};

    /* if no route, return -1 */
    if (dst->id != BROADCAST_ADDR && n_hop == NULL) {
        nodedata->data_noroute++;
        return -1;
    }
    else if (dst->id == BROADCAST_ADDR) {
      n_hop->id = BROADCAST_ADDR;
    }

    /* set routing header */
    header->dst = dst->id;
    header->dst_pos.x = dst->position.x;
    header->dst_pos.y = dst->position.y;
    header->dst_pos.z = dst->position.z;
    header->src = c->node;
    header->src_pos.x = get_node_position(c->node)->x;
    header->src_pos.y = get_node_position(c->node)->y;
    header->src_pos.z = get_node_position(c->node)->z;
    header->type = DATA_PACKET;
    header->hop = nodedata->hop;

    /* Set mac header */
    destination.id = n_hop->id;
    destination.position.x = -1;
    destination.position.y = -1;
    destination.position.z = -1;
    return SET_HEADER(&c0, packet, &destination);
}
void add_neighbor(call_t *c, struct routing_header *header) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct neighbor *neighbor = NULL;

    /* check wether neighbor already exists */
    das_init_traverse(nodedata->neighbors);      
    while ((neighbor = (struct neighbor *) das_traverse(nodedata->neighbors)) != NULL) {      
        if (neighbor->id == header->src) {
            neighbor->position.x = header->src_pos.x;
            neighbor->position.y = header->src_pos.y;
            neighbor->position.z = header->src_pos.z;
            neighbor->time = get_time();
            return;
        }
    }  

    neighbor = (struct neighbor *) malloc(sizeof(struct neighbor));
    neighbor->id = header->src;
    neighbor->position.x = header->src_pos.x;
    neighbor->position.y = header->src_pos.y;
    neighbor->position.z = header->src_pos.z;
    neighbor->time = get_time();
    das_insert(nodedata->neighbors, (void *) neighbor);
    return;
}
/* ************************************************** */
int set_header(call_t *c, packet_t *packet, destination_t *dst) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct neighbor *n_hop = get_nexthop(c, dst->id);
    destination_t destination;
    struct routing_header *header = (struct routing_header *) 
                                            (packet->data + nodedata->overhead);
    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};

    /* No route available */
    if (dst->id != BROADCAST_ADDR && n_hop == NULL) {
        return -1;
    } 

    /* Set routing header */
    header->dst = dst->id;
    header->src = c->node;
    header->hop = nodedata->hop;

    /* Set MAC header */
    if (dst->id == BROADCAST_ADDR) {
        destination.id = BROADCAST_ADDR;
    } else {
        destination.id = n_hop->id;        
    }
    destination.position.x = -1;
    destination.position.y = -1;
    destination.position.z = -1;

    return SET_HEADER(&c0, packet, &destination);
}
Beispiel #4
0
/* ************************************************** */
void rx_xy_hello(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct xy_hello_p *hello = (struct xy_hello_p *) (packet->data + nodedata->overhead);
    struct xy_neighbor *neighbor;

   /* check for existing neighbors */
    das_init_traverse(nodedata->neighbors);      
    while ((neighbor = (struct xy_neighbor *) das_traverse(nodedata->neighbors)) != NULL) {
        if (neighbor->id == hello->src) {
            neighbor->position.x = hello->position.x;
            neighbor->position.y = hello->position.y;
            neighbor->position.z = hello->position.z;
            neighbor->time       = get_time();
            packet_dealloc(packet);
            return;
        }
    }  

    /* new neighbor */
    neighbor = (struct xy_neighbor *) malloc(sizeof(struct xy_neighbor));
    neighbor->id         = hello->src;
    neighbor->position.x = hello->position.x;
    neighbor->position.y = hello->position.y;
    neighbor->position.z = hello->position.z;
    neighbor->time       = get_time();
    das_insert(nodedata->neighbors, (void *) neighbor);
    packet_dealloc(packet);
    return;
}
/* ************************************************** */
void tx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};
    
    nodedata->data_tx++;
    TX(&c0, packet);
}
/* Find all the neighbors (i.e. nodes in range) of the current node */
int find_neighbors(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct neighbor *neighbor = NULL;    
    nodeid_t i;
    double dist = 0;
    int nb_neigh = 0;

    /* Parse all the nodes in the simulation: and find the ones 
     * that are in range of that node
     */
    for(i = 0; i < get_node_count(); i++) {
        /* Do not include myself */
        if (i == c->node) {
            continue;
        }

        dist = distance(get_node_position(c->node), get_node_position(i));
        if (dist <= nodedata->range) {
            /* Add the node in the list of neighbors */
            neighbor = (struct neighbor *) malloc(sizeof(struct neighbor));
            neighbor->id = i;
            neighbor->position.x = get_node_position(i)->x;
            neighbor->position.y = get_node_position(i)->y;
            neighbor->position.z = get_node_position(i)->z;
            neighbor->time = get_time();
            //PRINT_ROUTING("Node %d is added to neighbor list of node %d\n",
            //              neighbor->id, c->node);    
            das_insert(nodedata->neighbors, (void *) neighbor);
            nb_neigh++;
        }
    }

    return nb_neigh;
}
Beispiel #7
0
// ENVOI DE PACKET HELLO
int init_one_hop(call_t *c, void *args) {
    struct nodedata *nodedata = get_node_private_data(c);

    //recuperer le support de communication DOWN
    entityid_t *down = get_entity_links_down(c);
    call_t c0 = {down[0], c->node};

    //destination de paquet
    destination_t destination = {BROADCAST_ADDR, {-1, -1, -1}};

    //creation de paquet et initialisation de son data
    packet_t *packet = packet_alloc(c, nodedata->overhead[0] + sizeof(struct packet_hello));
    struct packet_hello *hello = (struct packet_hello *) (packet->data + nodedata->overhead[0]);

    //initilailser les données
    hello->type=HELLO;
    hello->source=c->node;

    if (SET_HEADER(&c0, packet, &destination) == -1) {
            packet_dealloc(packet);
            return -1;
    }

    DEBUG;
    /*printf("Node %d (%lf %lf %lf) broadcast a packet hello, at %lf\n", c->node,                                 //id de Noeud
           get_node_position(c->node)->x,get_node_position(c->node)->y,get_node_position(c->node)->z,           //la postion x, y,z de Noeud
           get_time_now_second());//*/                                                                              //l'instant d'envoi.

    //L'envoi
    TX(&c0,packet);
    //tous c'est bien passé
    return 1;
}
double get_range_Tr(call_t *c)
{
    array_t *mac=get_mac_entities(c);
    call_t c0 = {mac->elts[0], c->node, c->entity};
    struct macnodedata* macdata = get_node_private_data(&c0);
    return macdata->range;
}
Beispiel #9
0
void rx_source_adv(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct source_adv_p *source = (struct source_adv_p *) (packet->data + nodedata->overhead);
    struct sensor_adv_p *sensor;
    packet_t *packet0;
    destination_t dst = {BROADCAST_ADDR, {-1, -1, -1}};
    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};

    /* check adv sequence */
    if (source->s_seq <= nodedata->s_seq[source->source]) {
        /* old request */
        packet_dealloc(packet);
        return;
    }
    nodedata->s_seq[source->source] = source->s_seq;
    
    /* reply */
    packet0 = packet_create(c, nodedata->overhead + sizeof(struct sensor_adv_p), -1);
    sensor = (struct sensor_adv_p *) (packet0->data + nodedata->overhead);
    if (SET_HEADER(&c0, packet0, &dst) == -1) {
        packet_dealloc(packet);
        packet_dealloc(packet0);
        return;
    }
    sensor->type = SENSOR_ADV_TYPE;
    sensor->sensor = c->node;
    sensor->source = source->source;
    sensor->s_seq = source->s_seq;
    TX(&c0, packet0);
    packet_dealloc(packet);

    return;
}
/* ************************************************** */
int bootstrap(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);
    nodedata->tx_busy = -1;
    nodedata->rx_busy = -1;
    nodedata->rxdBm = MIN_DBM;
    return 0;
}
/* ************************************************** */
double get_noise(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);
    entityid_t *down = get_entity_links_down(c);

    c->from = down[0];
    return MEDIA_GET_NOISE(c, nodedata->channel);
}
/* ************************************************** */
void tx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    array_t *down = get_entity_bindings_down(c);
    int i = down->size;
    
    /* radio sleep */
    if (nodedata->sleep) {
        packet_dealloc(packet);
        return;
    }

    /* radio activity */
    cs_init(c);
    nodedata->tx_busy = packet->id;

    /* log tx */
    PRINT_REPLAY("radio-tx0 %"PRId64" %d 50\n", get_time(), c->node);

    /* transmit to antenna */
    while (i--) {
        packet_t *packet_down;
        
        if (i > 0) {
            packet_down = packet_clone(packet);         
        } else {
            packet_down = packet;
        }
        c->from = down->elts[i];
        MEDIA_TX(c, packet_down);
    }

    return;
}
/* ************************************************** */
void rx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct _802_15_4_header *header = (struct _802_15_4_header *) packet->data;
    array_t *up = get_entity_bindings_up(c);
    int i = up->size;

    /* cf p171 ref 802.15.4-2006: discard packet while in backoff */
    if (nodedata->state != STATE_IDLE) {
        packet_dealloc(packet);
        return;
        
    }

    if ((header->dst != c->node) && (header->dst != BROADCAST_ADDR)) {
        packet_dealloc(packet);
        return;
    }
    
    while (i--) {
        call_t c_up = {up->elts[i], c->node, c->entity};
        packet_t *packet_up;
        
        if (i > 0) {
            packet_up = packet_clone(packet);         
        } else {
            packet_up = packet;
        }
        RX(&c_up, packet_up);
    }

    return;
}
/* ************************************************** */
void rx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);

    packet_PROTOCOLE *data = (packet_PROTOCOLE *) (packet->data + nodedata->overhead);


    /*******************************HELLO 1 voisinage***************************/
    switch(data->type)
    {
    case HELLO:
    {
        rx_hello(c,packet);
        break;
    }
    case RBOP:
    {
        if(list_recherche(data->destinations,c->node))
        {
            SHOW_GRAPH("G: %d %d\n",data->redirected_by,c->node);
            if(list_PACKET_recherche_tout(nodedata->paquets,data->src,data->seq)==0)
                PROTOCOLE_reception(c,packet);
        }

        break;
    }
    default:
        printf("J'ai recu un packet de type %d NON reconnu\n", data->type);
        break;
    }
}
Beispiel #15
0
/* Greedy geographic routing => computing the nexthop */ 
struct xy_neighbor *xy_next_hop(call_t *c, position_t *position) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct xy_neighbor *neighbor = NULL, *n_hop = NULL;
    double dist = distance(get_node_position(c->node), position);
    double d = dist;
    uint64_t clock = get_time();

    /* parse neighbors */
    das_init_traverse(nodedata->neighbors);    

    while ((neighbor = (struct xy_neighbor *) das_traverse(nodedata->neighbors)) != NULL) {

#ifdef CHECK_ACTIVE_NODE
        if ( !is_node_alive(neighbor->id) || ((nodedata->h_timeout > 0) && (clock - neighbor->time) >= nodedata->h_timeout) ) {
            continue;
        }
#else
        if ((nodedata->h_timeout > 0) && (clock - neighbor->time) >= nodedata->h_timeout ) {
            continue;
        }
#endif
        
        /* choose next hop */
        if ((d = distance(&(neighbor->position), position)) < dist) {
            dist = d;
            n_hop = neighbor;
        }
    }    
    
    return n_hop;
}
double setRangeToFarestNeighbour(call_t *c, graphe* g, arbre* bipTree)
{
	struct nodedata *nodedata = get_node_private_data(c);
	struct protocoleData *entitydata = get_entity_private_data(c);
	
	// calcul de la distance entre ce noeud et son voisin 1-hop (dans l'arbre de BIP) le plus eloigne
	list *fils = 0;
	double distMax = 0, dist;
	position_t pos;
	arbre_get_fils(&fils, bipTree, c->node);
	while(fils != 0)
	{
		pos = listeNodes_getPos(nodedata->oneHopNeighbourhood, fils->val);
		dist = distance(get_node_position(c->node), &pos);
		if(dist > distMax)
		{
			distMax = dist;
		}
		fils = fils->suiv;
	}
	
	
	// set le range du module propagation a la valeur desiree
	set_range_Tr(c, distMax);
	//printf("rayon d'emission de %d fixe a %lf\n", c->node, macdata->range);
	
	return distMax;
}
/* ************************************************** */
void rx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    array_t *up = get_entity_bindings_up(c);
    int i = up->size;
    struct routing_header *header = (struct routing_header *) 
        (packet->data + nodedata->overhead);

    /* Forward packet if node is not the recipient */
	if ((header->dst != BROADCAST_ADDR) && (header->dst != c->node) ) {
        forward(c, packet);
        return;
    }
        
    while (i--) {
        call_t c_up = {up->elts[i], c->node, c->entity};
        packet_t *packet_up;	     
        if (i > 0) {
            packet_up = packet_clone(packet);         
        } else {
            packet_up = packet;
        }

        RX(&c_up, packet_up);
    }

    return;
}
void set_range_Tr(call_t *c, double range)
{
	range = (floor(range*10)+1)/10.0;
    array_t *mac=get_mac_entities(c);
    call_t c0 = {mac->elts[0], c->node, c->entity};
    struct macnodedata* macdata = get_node_private_data(&c0);
    macdata->range = range;
}
/* ************************************************** */
void rx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    array_t *up = get_entity_bindings_up(c);
    int i = up->size;

    /* radio sleep */
    if (nodedata->sleep) {
        packet_dealloc(packet);
        return;
    }

    /* half-duplex */
    if (nodedata->tx_busy != -1) {
        packet_dealloc(packet);
        return;
    }
    
    /* handle carrier sense */
    if (nodedata->rx_busy == packet->id) {
        nodedata->rx_busy = -1;
        nodedata->rxdBm   = MIN_DBM;
        /* log rx */
        PRINT_REPLAY("radio-rx1 %"PRId64" %d\n", get_time(), c->node);
        /* consume energy */
        battery_consume_rx(c, packet->duration);
    } else {
        packet_dealloc(packet);
        return;
    }

    /* check wether the reception has killed us */
    if (!is_node_alive(c->node)) {
        packet_dealloc(packet);
        return;
    }

    /* drop packet depending on the FER */
    if (get_random_double() < packet->PER) {
        packet_dealloc(packet);
        return;
    }    

    /* forward to upper layers */
    while (i--) {
        call_t c_up = {up->elts[i], c->node, c->entity};
        packet_t *packet_up;

        if (i > 0) {
            packet_up = packet_clone(packet);         
        } else {
            packet_up = packet;
        }
        RX(&c_up, packet_up);
    }

    return;
}
/* ************************************************** */
int neighbor_timeout(void *data, void *arg) {
    struct neighbor *neighbor = (struct neighbor *) data;
    call_t *c = (call_t *) arg;
    struct nodedata *nodedata = get_node_private_data(c);
    if ((get_time() - neighbor->time) >= nodedata->timeout) {
        return 1;
    }
    return 0;
}
/* *********************************************** */
int set_header( call_t *c , packet_t * packet , destination_t * dst )
{
    struct nodedata *nodedata = get_node_private_data(c);
    struct protocoleData *entitydata =get_entity_private_data(c);

    packet_PROTOCOLE *data = (packet_PROTOCOLE *) (packet->data + nodedata->overhead);
    //augmenter le nbr d'evenement
    nodedata->nbr_evenement++;

    if(entitydata->debug)
        DBG("RBOP - %d SET HEADER  \n",c->node);

    //Fixé le rayon
    if(nodedata->range<0)
    {

        listeNodes *tmp=nodedata->oneHopNeighbourhood;
        position_t pos1 = *get_node_position(c->node);
        double distMax = 0;
        while(tmp)
        {
            if(list_recherche(nodedata->RNG,tmp->values.node))
            {
                position_t pos2= {tmp->values.x,tmp->values.y,tmp->values.z};
                double dist=distance(&pos1,&pos2);
                if(distMax<dist)    distMax=dist;
            }
            tmp=tmp->suiv;
        }
        set_range_Tr(c,distMax);
        nodedata->range=get_range_Tr(c);
        if(entitydata->debug)
            DBG("RBOP  - %d FIXE RANGE TO %.2lf  \n",c->node,get_range_Tr(c));
    }



    //remplissage de data
    data->type=RBOP;
    data->src=c->node;
    data->src_pos=*get_node_position(c->node);
    data->seq=nodedata->nbr_evenement;
    data->redirected_by=c->node;

    data->destinations=Nullptr(list);
    list_copy(&data->destinations,nodedata->RNG);

    list_PACKET_insert_tout(&nodedata->paquets,data->src,data->seq,data->redirected_by);



    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};

    destination_t destination = {BROADCAST_ADDR, {-1, -1, -1}};

    return SET_HEADER(&c0, packet, &destination);
}
Beispiel #22
0
int bootstrap(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);
    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};
  
    /* get overhead */
    nodedata->overhead = GET_HEADER_SIZE(&c0);

    return 0;
}
Beispiel #23
0
//RECEPTION et REPONDRE AU PACKET HELLO
int rx_one_hop(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);

    //RECEPTION  DE PACKET HELLO
    struct packet_hello *hello = (struct packet_hello *) (packet->data + nodedata->overhead[0]);

    DEBUG;
    /*printf("NOde %d a recu un HELLO de %d (%lf %lf %lf) at %lf\n",c->node,
           hello->source,
           get_node_position(hello->source)->x,get_node_position(hello->source)->y,get_node_position(hello->source)->z,
           get_time_now_second());//*/


    //l'ajoute de voisin
    if(!list_recherche(nodedata->N1,hello->source))
        list_insert(&nodedata->N1,hello->source);


    //REPONSE DE PAKET HELLO

    //recuperer le support de communication MAC
     entityid_t *down = get_entity_links_down(c);
     call_t c0 = {down[0], c->node};

     //destination de paquet
     destination_t destination = {hello->source, {get_node_position(hello->source)->x,get_node_position(hello->source)->y,get_node_position(hello->source)->z}};

     //creation de paquet et initialisation de son data
     packet_t *rpacket = packet_alloc(c, nodedata->overhead[0] + sizeof(struct packet_hello));
     struct packet_hello *rhello = (struct packet_hello *) (rpacket->data + nodedata->overhead[0]);

     //initilailser les données
     rhello->type     =   REP_HELLO;
     rhello->source   =   c->node;

     if (SET_HEADER(&c0, rpacket, &destination) == -1) {
         packet_dealloc(rpacket);
         return -1;
     }

     DEBUG;
     /*printf("Node %d (%lf %lf %lf) repond a %d  packet hello, at %lf\n", c->node,                               //id de Noeud de noeud encours
                get_node_position(c->node)->x,get_node_position(c->node)->y,get_node_position(c->node)->z,           //la postion x, y,z de Noeud
                hello->source,                                                                                       //id de noued de destination
                get_time_now_second()); //*/                                                                             //l'instant d'envoi.

     //L'envoi
     TX(&c0,rpacket);

    //liberer le packet
    packet_dealloc(packet);

    //tous c'est bien passé
    return 1;

}
/* ************************************************** */
void tx(call_t *c, packet_t *packet) {
    struct nodedata *nodedata = get_node_private_data(c);
    
    das_insert(nodedata->packets, (void *) packet);

    if (nodedata->state == STATE_IDLE) {
        nodedata->clock = get_time();  
        state_machine(c,NULL);
    } 
}
int unsetnode(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);
    struct neighbor *neighbor;
    while ((neighbor = (struct neighbor *) das_pop(nodedata->neighbors)) != NULL) {
        free(neighbor);
    }
    das_destroy(nodedata->neighbors);    
    free(nodedata);
    return 0;
}
int get_header_real_size(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);

    if (nodedata->overhead == -1) {
        call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};        
        nodedata->overhead = GET_HEADER_REAL_SIZE(&c0);
    }
    
    return nodedata->overhead + sizeof(struct routing_header);
}
Beispiel #27
0
/* ************************************************** */
int bootstrap(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);

    PRINT_REPLAY("mobility %"PRId64" %d %lf %lf %lf\n", get_time(), c->node, 
                 get_node_position(c->node)->x, get_node_position(c->node)->y, 
                 get_node_position(c->node)->z);
    
    nodedata->lupdate = get_time();
    nodedata->nupdate = get_time() + nodedata->pausetime;
    return 0;
}
Beispiel #28
0
/* ************************************************** */
void xy_stats(call_t *c) {
   struct nodedata *nodedata = get_node_private_data(c);
   position_t *position = get_node_position(c->node);

   if (nodedata->type == INLINE_NODE) {
     printf("INLINE node %d (%lf,%lf,%lf)  Group_id=%d   Neighbors=%d\n", c->node, position->x, position->y, position->z, nodedata->group_id, das_getsize(nodedata->neighbors));
   } else {
     printf("SENSOR node %d (%lf,%lf,%lf)  Neighbors=%d\n", c->node, position->x, position->y, position->z, das_getsize(nodedata->neighbors));

   }
}
/* ************************************************** */
int check_channel_busy(call_t *c) {
    struct nodedata *nodedata = get_node_private_data(c);
    call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};

    if (nodedata->cs && (radio_get_cs(&c0) >= nodedata->EDThreshold)) {
        return 1;
    }
    if (nodedata->cca && (radio_get_noise(&c0) >= nodedata->EDThreshold)) {
        return 1;
    }
    return 0;
}
int get_header_size( call_t * c )
{
    struct nodedata *nodedata = get_node_private_data(c);

    if (nodedata->overhead == -1)
    {
        call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity};
        nodedata->overhead = GET_HEADER_SIZE(&c0);
    }

    return nodedata->overhead + sizeof(packet_PROTOCOLE);
}