Пример #1
0
/*---------------------------------------------------------------------------*/
struct ip64_addrmap_entry *
ip64_addrmap_create(const uip_ip6addr_t *ip6addr,
                    uint16_t ip6port,
                    const uip_ip4addr_t *ip4addr,
                    uint16_t ip4port,
                    uint8_t protocol)
{
    struct ip64_addrmap_entry *m;

    check_age();
    m = memb_alloc(&entrymemb);
    if(m == NULL) {
        /* We could not allocate an entry, try to recycle one and try to
           allocate again. */
        if(recycle()) {
            m = memb_alloc(&entrymemb);
        }
    }
    if(m != NULL) {
        uip_ip4addr_copy(&m->ip4addr, ip4addr);
        m->ip4port = ip4port;
        uip_ip6addr_copy(&m->ip6addr, ip6addr);
        m->ip6port = ip6port;
        m->protocol = protocol;
        m->flags = FLAGS_NONE;
        m->ip6to4 = 1;
        m->ip4to6 = 0;
        timer_set(&m->timer, 0);

        /* Pick a new, unused local port. First make sure that the
           mapped_port number does not belong to any active connection. If
           so, we keep increasing the mapped_port until we're free. */
        {
            struct ip64_addrmap_entry *n;
            n = list_head(entrylist);
            while(n != NULL) {
                if(n->mapped_port == mapped_port) {
                    increase_mapped_port();
                    n = list_head(entrylist);
                } else {
                    n = list_item_next(m);
                }
            }
        }
        m->mapped_port = mapped_port;
        increase_mapped_port();

        list_add(entrylist, m);
        return m;
    }
    return NULL;
}
Пример #2
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
#if DEBUGLOGIC
  struct httpd_state *s;   //Enter here for debugging with output directed to TCPBUF
  s = sg = (struct httpd_state *)memb_alloc(&conns);
  if (1) {
#else
  struct httpd_state *s = (struct httpd_state *)state;
  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      return;
    }
#endif
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    /*    timer_set(&s->timer, CLOCK_SECOND * 100);*/
    s->timer = 0;
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      ++s->timer;
      if(s->timer >= 20) {
        uip_abort();
        memb_free(&conns, s);
      }
    } else {
      s->timer = 0;
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
/*---------------------------------------------------------------------------*/
void
httpd_init(void)
{
  tcp_listen(UIP_HTONS(80));
  memb_init(&conns);
  httpd_cgi_init();
}
void _lmst_nodelist_reconstruct() {
	// clean list
	while(list_length(list_nodelist) > 0) {
		node_t *item = list_pop(list_nodelist);
		networkaddr_reference_free(item->address);
		memb_free(&memb_nodelist, item);
	}

	// add all nodes to list
	neighbor_t *item_neighbor;
	for(item_neighbor = list_head(component_neighbordiscovery_neighbors()); item_neighbor != NULL; item_neighbor = list_item_next(item_neighbor)) {
		node_t *item_node;
		bool found;

		// check for node1
		found = false;
		for(item_node = list_head(list_nodelist); item_node != NULL; item_node = list_item_next(item_node)) {
			if(networkaddr_equal(item_neighbor->node1, item_node->address)) {
				found = true;
				break;
			}
		}
		if(!found) {
			if((item_node = memb_alloc(&memb_nodelist)) == NULL) {
				printf("ERROR[topologycontrol-lmst]: nodelist is full\n");
			} else {
				item_node->address = networkaddr_reference_alloc(item_neighbor->node1);
				item_node->edge = NULL;
				list_add(list_nodelist, item_node);
			}
		}

		// check for node2
		found = false;
		for(item_node = list_head(list_nodelist); item_node != NULL; item_node = list_item_next(item_node)) {
			if(networkaddr_equal(item_neighbor->node2, item_node->address)) {
				found = true;
				break;
			}
		}
		if(!found) {
			if((item_node = memb_alloc(&memb_nodelist)) == NULL) {
				printf("ERROR[topologycontrol-lmst]: nodelist is full\n");
			} else {
				item_node->address = networkaddr_reference_alloc(item_neighbor->node2);
				item_node->edge = NULL;
				list_add(list_nodelist, item_node);
			}
		}
	}
}
Пример #4
0
/*---------------------------------------------------------------------------*/
struct queuebuf *
queuebuf_new_from_packetbuf(void)
{
  struct queuebuf *buf;
  struct queuebuf_ref *rbuf;

  if(packetbuf_is_reference()) {
    rbuf = memb_alloc(&refbufmem);
    if(rbuf != NULL) {
#if QUEUEBUF_STATS
      ++queuebuf_ref_len;
#if CONTIKI_TARGET_NETSIM
      /*      node_log("%d %d\n",
	       queuebuf_len,
	       queuebuf_ref_len);*/
#endif /* CONTIKI_TARGET_NETSIM */
#endif /* QUEUEBUF_STATS */
      rbuf->len = packetbuf_datalen();
      rbuf->ref = packetbuf_reference_ptr();
      rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr);
    } else {
      PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n");
    }
    return (struct queuebuf *)rbuf;
  } else {
    buf = memb_alloc(&bufmem);
    if(buf != NULL) {
#if QUEUEBUF_STATS
      ++queuebuf_len;
      if(queuebuf_len == queuebuf_max_len + 1) {
	memb_free(&bufmem, buf);
	queuebuf_len--;
	return NULL;
      }
#if CONTIKI_TARGET_NETSIM
      /*      node_log("%d %d\n",
	       queuebuf_len,
	       queuebuf_ref_len);*/
#endif /* CONTIKI_TARGET_NETSIM */
#endif /* QUEUEBUF_STATS */
      buf->len = packetbuf_copyto(buf->data);
      packetbuf_attr_copyto(buf->attrs, buf->addrs);
    } else {
      PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n");
    }
    return buf;
  }
}
Пример #5
0
/*-----------------------------------------------------------------------------------*/
coap_observer_t *
coap_add_observer(uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len, const char *url)
{
  /* Remove existing observe relationship, if any. */
  coap_remove_observer_by_url(addr, port, url);

  coap_observer_t *o = memb_alloc(&observers_memb);

  if (o)
  {
    o->url = url;
    uip_ipaddr_copy(&o->addr, addr);
    o->port = port;
    o->token_len = token_len;
    memcpy(o->token, token, token_len);
    o->last_mid = 0;

    stimer_set(&o->refresh_timer, COAP_OBSERVING_REFRESH_INTERVAL);

    PRINTF("Adding observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
    list_add(observers_list, o);
  }

  return o;
}
Пример #6
0
/*---------------------------------------------------------------------------*/
coap_observer_t *
coap_add_observer(uip_ipaddr_t *addr, uint16_t port, const uint8_t *token,
                  size_t token_len, const char *uri)
{
  /* Remove existing observe relationship, if any. */
  coap_remove_observer_by_uri(addr, port, uri);

  coap_observer_t *o = memb_alloc(&observers_memb);

  if(o) {
    o->url = uri;
    uip_ipaddr_copy(&o->addr, addr);
    o->port = port;
    o->token_len = token_len;
    memcpy(o->token, token, token_len);
    o->last_mid = 0;

    PRINTF("Adding observer (%u/%u) for /%s [0x%02X%02X]\n",
           list_length(observers_list) + 1, COAP_MAX_OBSERVERS,
           o->url, o->token[0], o->token[1]);
    list_add(observers_list, o);
  }

  return o;
}
Пример #7
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
  struct httpd_state *s = (struct httpd_state *)state;

  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      return;
    }
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    timer_set(&s->timer, CLOCK_SECOND * 10);
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      if(timer_expired(&s->timer)) {
	uip_abort();
      }
    } else {
      timer_reset(&s->timer);
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
/**
 * create a broadcast msg from the argument given
 */
Br_msg_list* create_broadcast_msg(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){
    struct Br_msg_list *broadcast_entry;
    broadcast_entry = memb_alloc(&broadcast_list_memb);
    if(broadcast_entry == NULL){
        printf("@broacast create can not alloc memoery\n");
        list_chop(broadcast_list);
    }
    //memset(broadcast_entry,0, sizeof(struct Br_msg_list));
    broadcast_entry->broadcast_msg.seqno = br_seqno;
    broadcast_entry->broadcast_msg.native_sen_type = native_sen_type;
    rimeaddr_copy(&broadcast_entry->broadcast_msg.last_hop, &last_hop);
    rimeaddr_copy(&broadcast_entry->broadcast_msg.source, &source);
    rimeaddr_copy(&broadcast_entry->broadcast_msg.base_station,&base_station);
    broadcast_entry->broadcast_msg.hops = hops;
    broadcast_entry->broadcast_msg.msg_type = msg_type;
    //  broadcast_entry->broadcast_msg.timestamp = CLOCK_SECOND;
    broadcast_entry->broadcast_msg.frequency = frequency;
    broadcast_entry->broadcast_msg.subscribe_type = sub_type;
    broadcast_entry->broadcast_msg.condition = condition;
    broadcast_entry->broadcast_msg.assemble_count = assemble_count;
    broadcast_entry->broadcast_msg.create_timestamp = timestamp;
    if(br_seqno>254){
        br_seqno = 0;
    }else{
        br_seqno++;
    }

    return broadcast_entry;
}
Пример #9
0
/*---------------------------------------------------------------------------*/
static void
received_announcement(struct announcement *a,
                      const rimeaddr_t *from,
		      uint16_t id, uint16_t value)
{
  struct example_neighbor *e;

     /* printf("Got announcement from %d.%d, id %d, value %d\n",
      from->u8[0], from->u8[1], id, value);*/

  for(e = list_head(neighbor_table); e != NULL; e = e->next) {
    if(rimeaddr_cmp(from, &e->addr)) {
      /* Our neighbor was found, so we update the timeout. */
      ctimer_set(&e->ctimer, NEIGHBOR_TIMEOUT, remove_neighbor, e);
      return;
    }
  }

  e = memb_alloc(&neighbor_mem);
  if(e != NULL) {
    rimeaddr_copy(&e->addr, from);
    e->rank=value;
    list_add(neighbor_table, e);
    ctimer_set(&e->ctimer, NEIGHBOR_TIMEOUT, remove_neighbor, e);
  }
}
Пример #10
0
/*---------------------------------------------------------------------------*/
static void
add_packet_stats(int input_or_output)
{
  struct powertrace_sniff_stats *s;

  /* Go through the list of stats to find one that matches the channel
     of the packet. If we don't find one, we allocate a new one and
     put it on the list. */
  for(s = list_head(stats_list); s != NULL; s = list_item_next(s)) {
    if(s->channel == packetbuf_attr(PACKETBUF_ATTR_CHANNEL)
#if UIP_CONF_IPV6
       && s->proto == packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID)
#endif
       ) {
      add_stats(s, input_or_output);
      break;
    }
  }
  if(s == NULL) {
    s = memb_alloc(&stats_memb);
    if(s != NULL) {
      memset(s, 0, sizeof(struct powertrace_sniff_stats));
      s->channel = packetbuf_attr(PACKETBUF_ATTR_CHANNEL);
#if UIP_CONF_IPV6
      s->proto = packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID);
#endif
      list_add(stats_list, s);
      add_stats(s, input_or_output);
    }
  }
}
Пример #11
0
device *
create_device(uint8_t devId,
		char *name,
		char *icon,
		type_t type,
		unit_t unit,
		char *min,
		char *max,
		interface_t interf,
		float logTime,
		sensor_t sensor){
	device *newDev;
	newDev = memb_alloc(&device_mem);

	if(newDev!=NULL){
		newDev->deviceId = devId;
		strncpy(newDev->deviceName, name, strlen(name));
		strncpy(newDev->icon, icon, strlen(icon));
		newDev->type = type;
		newDev->unit = unit;
		strncpy(newDev->min, min, strlen(min));
		strncpy(newDev->max, max, strlen(max));
		newDev->interfaceDisplay = interf;
		newDev->logTime = logTime;
		newDev->sensor = sensor;
		newDev->initialized = FALSE;
		printf("create_device(): created new dev\n");
	}else{
		printf("create_device(): failed to allocate memory\n");
		return NULL;
	}

	return newDev;
}
Пример #12
0
/*---------------------------------------------------------------------------*/
static void
recv_runicast(struct runicast_conn *c, const rimeaddr_t *from, uint8_t seqno)
{
  /* OPTIONAL: Sender history */
  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) {
    /* Create new history entry */
    e = memb_alloc(&history_mem);
    if(e == NULL) {
      e = list_chop(history_table); /* Remove oldest at full history */
    }
    rimeaddr_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);
      return;
    }
    /* Update existing history entry */
    e->seq = seqno;
  }

  printf("runicast message received from %d.%d, seqno %d\n",
	 from->u8[0], from->u8[1], seqno);
}
Пример #13
0
/*---------------------------------------------------------------------------*/
rpl_parent_t *
rpl_add_parent(rpl_dag_t *dag, rpl_dio_t *dio, uip_ipaddr_t *addr)
{
  rpl_parent_t *p;

  if(RPL_PARENT_COUNT(dag) == RPL_MAX_PARENTS_PER_DAG) {
    return NULL;
  }

  p = memb_alloc(&parent_memb);
  if(p == NULL) {
    RPL_STAT(rpl_stats.mem_overflows++);
    return NULL;
  }
  memcpy(&p->addr, addr, sizeof(p->addr));
  p->dag = dag;
  p->rank = dio->rank;
  p->dtsn = dio->dtsn;
  p->link_metric = RPL_INIT_LINK_METRIC;
#if RPL_DAG_MC != RPL_DAG_MC_NONE
  memcpy(&p->mc, &dio->mc, sizeof(p->mc));
#endif /* RPL_DAG_MC != RPL_DAG_MC_NONE */
  list_add(dag->parents, p);
  return p;
}
Пример #14
0
/*
 * This function is called when an incoming announcement arrives. The
 * function checks the neighbor table to see if the neighbor is
 * already present in the list. If the neighbor is not present in the
 * list, a new neighbor table entry is allocated and is added to the
 * neighbor table.
 */
static void
received_announcement(struct announcement *a,
                      const rimeaddr_t *from,
		      uint16_t id, uint16_t value)
{
  struct example_neighbor *e;

  /* We received an announcement from a neighbor so we need to update
     the neighbor list, or add a new entry to the table. */
  for(e = list_head(neighbor_table); e != NULL; e = e->next) {
    if(rimeaddr_cmp(from, &e->addr)) {
      /* Our neighbor was found, so we update the timeout. */
      ctimer_set(&e->ctimer, NEIGHBOR_TIMEOUT, remove_neighbor, e);
      return;
    }
  }

  /* The neighbor was not found in the list, so we add a new entry by
     allocating memory from the neighbor_mem pool, fill in the
     necessary fields, and add it to the list. */
  e = memb_alloc(&neighbor_mem);
  if(e != NULL) {
    rimeaddr_copy(&e->addr, from);
    e->rank=value;        
    list_add(neighbor_table, e);
    ctimer_set(&e->ctimer, NEIGHBOR_TIMEOUT, remove_neighbor, e);
  }
}
Пример #15
0
/*---------------------------------------------------------------------------*/
static void
received_announcement(struct announcement *a, const rimeaddr_t *from,
		      uint16_t id, uint16_t value)
{
  struct neighbor_entry *e;

  printf("%d.%d: announcement from neighbor %d.%d, id %d, value %d\n",
	 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	 from->u8[0], from->u8[1],
	 id, value);
  
  
  for(e = list_head(neighbor_table); e != NULL; e = e->next) {
    if(rimeaddr_cmp(from, &e->addr)) {
      ctimer_set(&e->ctimer, NEIGHBOR_TIMEOUT, remove_neighbor, e);
      show_leds();
      return;
    }
  }

  
  e = memb_alloc(&neighbor_mem);
  if(e != NULL) {
    rimeaddr_copy(&e->addr, from);
    list_add(neighbor_table, e);
    ctimer_set(&e->ctimer, NEIGHBOR_TIMEOUT, remove_neighbor, e);
  } else {
  }
  show_leds();
}
Пример #16
0
/* static function definitions */
static struct scope *
allocate_scope(data_len_t spec_len) {
	/* try to get memory for the scope */
	struct scope *scope = (struct scope *) memb_alloc(&scopes_mem);

	/* check if memory could be obtained */
	if (scope == NULL) {
		return NULL;
	}

	/* try to get memory for the specification */
	void *specs_ptr = malloc(spec_len);

	/* check if memory could be obtained */
	if (specs_ptr == NULL) {
		/* free memory for scope structure */
		memb_free(&scopes_mem, scope);
		return NULL;
	}

	/* set memory address of specification */
	scope->specs = specs_ptr;

	/* return new scope */
	return scope;
}
Пример #17
0
/* Add a TSCH neighbor */
struct tsch_neighbor *
tsch_queue_add_nbr(const linkaddr_t *addr)
{
  struct tsch_neighbor *n = NULL;
  /* If we have an entry for this neighbor already, we simply update it */
  n = tsch_queue_get_nbr(addr);
  if(n == NULL) {
    if(tsch_get_lock()) {
      /* Allocate a neighbor */
      n = memb_alloc(&neighbor_memb);
      if(n != NULL) {
        /* Initialize neighbor entry */
        memset(n, 0, sizeof(struct tsch_neighbor));
        ringbufindex_init(&n->tx_ringbuf, TSCH_QUEUE_NUM_PER_NEIGHBOR);
        linkaddr_copy(&n->addr, addr);
        n->is_broadcast = linkaddr_cmp(addr, &tsch_eb_address)
          || linkaddr_cmp(addr, &tsch_broadcast_address);
        tsch_queue_backoff_reset(n);
        /* Add neighbor to the list */
        list_add(neighbor_list, n);
      }
      tsch_release_lock();
    }
  }
  return n;
}
Пример #18
0
/* Adds and returns a slotframe (NULL if failure) */
struct tsch_slotframe *
tsch_schedule_add_slotframe(uint16_t handle, uint16_t size)
{
  if(size == 0) {
    return NULL;
  }

  if(tsch_schedule_get_slotframe_by_handle(handle)) {
    /* A slotframe with this handle already exists */
    return NULL;
  }

  if(tsch_get_lock()) {
    struct tsch_slotframe *sf = memb_alloc(&slotframe_memb);
    if(sf != NULL) {
      /* Initialize the slotframe */
      sf->handle = handle;
      ASN_DIVISOR_INIT(sf->size, size);
      LIST_STRUCT_INIT(sf, links_list);
      /* Add the slotframe to the global list */
      list_add(slotframe_list, sf);
    }
    PRINTF("TSCH-schedule: add_slotframe %u %u\n",
           handle, size);
    tsch_release_lock();
    return sf;
  }
  return NULL;
}
Пример #19
0
/*---------------------------------------------------------------------------*/
static void
register_encounter(rimeaddr_t *neighbor, clock_time_t time)
{
  struct encounter *e;

  /* If we have an entry for this neighbor already, we renew it. */
  for(e = list_head(encounter_list); e != NULL; e = list_item_next(e)) {
    if(rimeaddr_cmp(neighbor, &e->neighbor)) {
      e->time = time;
      ctimer_set(&e->remove_timer, ENCOUNTER_LIFETIME, remove_encounter, e);
      break;
    }
  }
  /* No matchin encounter was found, so we allocate a new one. */
  if(e == NULL) {
    e = memb_alloc(&encounter_memb);
    if(e == NULL) {
      /* We could not allocate memory for this encounter, so we just drop it. */
      return;
    }
    rimeaddr_copy(&e->neighbor, neighbor);
    e->time = time;
    ctimer_set(&e->remove_timer, ENCOUNTER_LIFETIME, remove_encounter, e);
    list_add(encounter_list, e);
  }
}
int routing_table_update_queuelog(struct routingtable *t,
                               const rimeaddr_t * addr,
                               uint16_t queuelog){
    struct routingtable_item *i;
   
    i = routing_table_find(t, addr);
    
    //No record for this neighbor address
    if(i == NULL) {
        // Allocate memory for the new record
        i = memb_alloc(t->memb);

        //Failed to allocate memory
        if(i == NULL) {
          return -1;
        }

        // Set default attributes
        i->next = NULL;
        rimeaddr_copy(&(i->neighbor), addr);
        i->backpressure = queuelog;
        
        //Ask weight estimator to initialize its fields 
        weight_estimator_record_init(i);
        
        //Insert the new record
        list_add(*t->list, i);
    }else{
        i->backpressure = queuelog;
    }
    //dbg_print_rtable(t);
    return 1;
}
Пример #21
0
static void
periodic(struct pipe* p)
{
	uint16_t crtseqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
	uint16_t l = 0;

	struct c_neighbor *n;
	struct c_neighbor *new_n = memb_alloc(&p->neighbor_mem);;
	double alpha = p->lqe_ewma_param.alpha;

  for(n = list_head(p->neighbor_list); n != NULL; n = list_item_next(n)) {
	  if(rimeaddr_cmp(&new_n->addr, &n->addr)) {
		  memcpy(new_n, n, sizeof (struct c_neighbor));
	 	  if (n->rate > 0.9) {
	 		 l = (uint16_t)((clock_time() - n->last_time_stamp) / (clock_time()));
	 	  } else {
	 		  l = (uint16_t)((clock_time() - n->last_time_stamp) * n->rate);
	 	  }
	 	  uint8_t lost_p = 0;
	 	  for(lost_p = 0; lost_p < l; lost_p++) { n->cost *= alpha; }
	 	  new_n->cost = n->cost;
	 	  new_n->k = l;

	      list_remove(p->neighbor_list, n);
	      memb_free(&p->neighbor_mem, n);
	      list_push(p->neighbor_list, new_n);
	      print_list(p->neighbor_list);
	  }
  }
  ctimer_set(&t, CLOCK_SECOND * NEIGHBOR_LIFETIME, periodic, p);
}
Пример #22
0
/*---------------------------------------------------------------------------*/
struct uaodv_rt_entry *
uaodv_rt_add(uip_ipaddr_t *dest, uip_ipaddr_t *nexthop,
	     unsigned hop_count, const uint32_t *seqno)
{
  struct uaodv_rt_entry *e;

  /* Avoid inserting duplicate entries. */
  e = uaodv_rt_lookup_any(dest);
  if(e != NULL) {
    list_remove(route_table, e);    
  } else {
    /* Allocate a new entry or reuse the oldest. */
    e = memb_alloc(&route_mem);
    if(e == NULL) {
      e = list_chop(route_table); /* Remove oldest entry. */
    }
  }

  uip_ipaddr_copy(&e->dest, dest);
  uip_ipaddr_copy(&e->nexthop, nexthop);
  e->hop_count = hop_count;
  e->hseqno = uip_ntohl(*seqno);
  e->is_bad = 0;

  /* New entry goes first. */
  list_push(route_table, e);

  return e;
}
Пример #23
0
/*---------------------------------------------------------------------------*/
#if QUEUEBUF_DEBUG
struct queuebuf *
queuebuf_new_from_packetbuf_debug(const char *file, int line)
#else /* QUEUEBUF_DEBUG */
struct queuebuf *
queuebuf_new_from_packetbuf(void)
#endif /* QUEUEBUF_DEBUG */
{
  struct queuebuf *buf;
  struct queuebuf_ref *rbuf;

  if(packetbuf_is_reference()) {
    rbuf = memb_alloc(&refbufmem);
    if(rbuf != NULL) {
#if QUEUEBUF_STATS
      ++queuebuf_ref_len;
#endif /* QUEUEBUF_STATS */
      rbuf->len = packetbuf_datalen();
      rbuf->ref = packetbuf_reference_ptr();
      rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr);
    } else {
      PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n");
    }
    return (struct queuebuf *)rbuf;
  } else {
    buf = memb_alloc(&bufmem);
    if(buf != NULL) {
#if QUEUEBUF_DEBUG
      list_add(queuebuf_list, buf);
      buf->file = file;
      buf->line = line;
      buf->time = clock_time();
#endif /* QUEUEBUF_DEBUG */
#if QUEUEBUF_STATS
      ++queuebuf_len;
      PRINTF("queuebuf len %d\n", queuebuf_len);
      printf("#A q=%d\n", queuebuf_len);
      if(queuebuf_len == queuebuf_max_len + 1) {
	memb_free(&bufmem, buf);
	queuebuf_len--;
	return NULL;
      }
#endif /* QUEUEBUF_STATS */
      buf->len = packetbuf_copyto(buf->data);
      packetbuf_attr_copyto(buf->attrs, buf->addrs);
    } else {
      PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n");
    }
    return buf;
  }
}
Пример #24
0
/*---------------------------------------------------------------------------*/
int
neighbor_attr_add_neighbor(const rimeaddr_t *addr)
{
  struct neighbor_attr *def;
  struct neighbor_addr *item;
  struct neighbor_addr *ptr;
  struct neighbor_addr *oldest = NULL;
  clock_time_t oldest_time;
  uint16_t i;

  if(neighbor_attr_has_neighbor(addr)) {
    return 0;
  }

  if(node_id_from_rimeaddr(addr) == 0) {
    return 0;
  }

  item = memb_alloc(&neighbor_addr_mem);
  oldest_time = clock_time();
  /* no space available for new entry, look for oldest entry and take its place */
  if(item == NULL) {
    item = list_head(neighbor_addrs);
    while(item != NULL) {
      if(item->last_lookup < oldest_time) {
        oldest = item;
        oldest_time = item->last_lookup;
      }
      item = item->next;
    }
    if(oldest != NULL) {
      item = oldest;
    } else {
      return -1;
    }
  } else {
    list_push(neighbor_addrs, item);
  }

  item->time = 0;
  item->last_lookup = clock_time();
  rimeaddr_copy(&item->addr, addr);

  /* look up index and set default values */
  ptr = neighbor_addr_mem.mem;
  for(i = 0; i < neighbor_addr_mem.num; ++i) {
    if(&ptr[i] == item) {
      break;
    }
  }

  item->index = i;

  for(def = list_head(neighbor_attrs); def != NULL; def = def->next) {
    set_attr(def, i);
  }

  return 1;
}
Пример #25
0
/*---------------------------------------------------------------------------*/
uip_ds6_defrt_t *
uip_ds6_defrt_add(uip_ipaddr_t *ipaddr, unsigned long interval)
{
  uip_ds6_defrt_t *d;

#if _DEBUG_ != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* _DEBUG_ != DEBUG_NONE */

  PRINTF("uip_ds6_defrt_add\n");
  d = uip_ds6_defrt_lookup(ipaddr);
  if(d == NULL) {
    d = memb_alloc(&defaultroutermemb);
    if(d == NULL) {
      PRINTF("uip_ds6_defrt_add: could not add default route to ");
      PRINT6ADDR(ipaddr);
      PRINTF(", out of memory\n");
      return NULL;
    } else {
      PRINTF("uip_ds6_defrt_add: adding default route to ");
      PRINT6ADDR(ipaddr);
      PRINTF("\n");
    }

    list_push(defaultrouterlist, d);
  }

  if(ipaddr == NULL) {
    /* A special case: if uip_ds6_defrt_add() is called with a NULL
       route, this is an indication that we want to force packets to
       go out to the fallback interface. If so, we add an unspecified
       route to the list of default routes. uip_ds6_defrt_choose()
       will trap this and ensure that packets go to the fallback
       interface. */
    uip_create_unspecified(&d->ipaddr);
    ipaddr = &d->ipaddr;
  } else {
    uip_ipaddr_copy(&d->ipaddr, ipaddr);
  }

  if(interval != 0) {
    stimer_set(&d->lifetime, interval);
    d->isinfinite = 0;
  } else {
    d->isinfinite = 1;
  }

  ANNOTATE("#L %u 1\n", ipaddr->u8[sizeof(uip_ipaddr_t) - 1]);

#if UIP_DS6_NOTIFICATIONS
  call_route_callback(UIP_DS6_NOTIFICATION_DEFRT_ADD, ipaddr, ipaddr);
#endif

#if _DEBUG_ != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* _DEBUG_ != DEBUG_NONE */

  return d;
}
Пример #26
0
/*----------------------------------------------------------------------------*/
  static entry_t *
  entry_allocate(void)
  {
    entry_t *e;

    e = memb_alloc(&entries_memb);
    if(e == NULL) {
      purge_flowtable();
      e = memb_alloc(&entries_memb);
      if(e == NULL) {
        PRINTF("[FLT]: Failed to allocate an entry\n");
        return NULL;
      }
    }

    entry_init(e);
    return e;
  }
Пример #27
0
static relation_t *
relation_allocate(void)
{
  relation_t *rel;

  rel = memb_alloc(&relations_memb);
  if(rel == NULL) {
    purge_relations();
    rel = memb_alloc(&relations_memb);
    if(rel == NULL) {
      PRINTF("DB: Failed to allocate a relation\n");
      return NULL;
    }
  }

  relation_clear(rel);
  return rel;
}
Пример #28
0
/*---------------------------------------------------------------------------*/
void
phase_update(const struct phase_list *list,
             const rimeaddr_t *neighbor, rtimer_clock_t time,
             int mac_status)
{
  struct phase *e;
  uint8_t tmp;

  /* If we have an entry for this neighbor already, we renew it. */
  e = find_neighbor(list, neighbor);
  if(e != NULL) {
    if(mac_status == MAC_TX_OK) {
#if PHASE_DRIFT_CORRECT
      e->drift = time-e->time;
#endif
      e->time = time;
    }
    /* If the neighbor didn't reply to us, it may have switched
       phase (rebooted). We try a number of transmissions to it
       before we drop it from the phase list. */
    if(mac_status == MAC_TX_NOACK) {
      PRINTF("phase noacks %d to %d.%d\n", e->noacks, neighbor->u8[0], neighbor->u8[1]);
      tmp=      e->noacks;
      e->noacks=tmp++;
      if(e->noacks == 1) {
        timer_set(&e->noacks_timer, MAX_NOACKS_TIME);
      }
      if(e->noacks >= MAX_NOACKS || timer_expired(&e->noacks_timer)) {
        PRINTF("drop %d\n", neighbor->u8[0]);
        list_remove(*list->list, e);
        memb_free(list->memb, e);
        return;
      }
    } else if(mac_status == MAC_TX_OK) {
      e->noacks = 0;
    }
  } else {
    /* No matching phase was found, so we allocate a new one. */
    if(mac_status == MAC_TX_OK && e == NULL) {
      e = memb_alloc(list->memb);
      if(e == NULL) {
        PRINTF("phase alloc NULL\n");
        /* We could not allocate memory for this phase, so we drop
           the last item on the list and reuse it for our phase. */
        e = list_chop(*list->list);
      }
      rimeaddr_copy(&e->neighbor, neighbor);
      e->time = time;
#if PHASE_DRIFT_CORRECT
      e->drift = 0;
#endif
      e->noacks = 0;
      list_push(*list->list, e);
    }
  }
}
Пример #29
0
/* Adds a link to a slotframe, return a pointer to it (NULL if failure) */
struct tsch_link *
tsch_schedule_add_link(struct tsch_slotframe *slotframe,
                       uint8_t link_options, enum link_type link_type, const linkaddr_t *address,
                       uint16_t timeslot, uint16_t channel_offset)
{
  struct tsch_link *l = NULL;
  if(slotframe != NULL) {
    /* We currently support only one link per timeslot in a given slotframe. */
    /* Start with removing the link currently installed at this timeslot (needed
     * to keep neighbor state in sync with link options etc.) */
    tsch_schedule_remove_link_by_timeslot(slotframe, timeslot);
    if(!tsch_get_lock()) {
      PRINTF("TSCH-schedule:! add_link memb_alloc couldn't take lock\n");
    } else {
      l = memb_alloc(&link_memb);
      if(l == NULL) {
        PRINTF("TSCH-schedule:! add_link memb_alloc failed\n");
      } else {
        static int current_link_handle = 0;
        struct tsch_neighbor *n;
        /* Add the link to the slotframe */
        list_add(slotframe->links_list, l);
        /* Initialize link */
        l->handle = current_link_handle++;
        l->link_options = link_options;
        l->link_type = link_type;
        l->slotframe_handle = slotframe->handle;
        l->timeslot = timeslot;
        l->channel_offset = channel_offset;
        l->data = NULL;
        if(address == NULL) {
          address = &linkaddr_null;
        }
        linkaddr_copy(&l->addr, address);

        PRINTF("TSCH-schedule: add_link %u %u %u %u %u %u\n",
               slotframe->handle, link_options, link_type, timeslot, channel_offset, TSCH_LOG_ID_FROM_LINKADDR(address));

        /* Release the lock before we update the neighbor (will take the lock) */
        tsch_release_lock();

        if(l->link_options & LINK_OPTION_TX) {
          n = tsch_queue_add_nbr(&l->addr);
          /* We have a tx link to this neighbor, update counters */
          if(n != NULL) {
            n->tx_links_count++;
            if(!(l->link_options & LINK_OPTION_SHARED)) {
              n->dedicated_tx_links_count++;
            }
          }
        }
      }
    }
  }
  return l;
}
Пример #30
0
/*---------------------------------------------------------------------------*/
uip_ds6_defrt_t *
uip_ds6_defrt_add(uip_ipaddr_t *ipaddr, unsigned long interval)
{
  uip_ds6_defrt_t *d;

#if DEBUG != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* DEBUG != DEBUG_NONE */

  PRINTF("uip_ds6_defrt_add\n");
  d = uip_ds6_defrt_lookup(ipaddr);
  if(d == NULL) {
    d = memb_alloc(&defaultroutermemb);
    if(d == NULL) {
      PRINTF("uip_ds6_defrt_add: could not add default route to ");
      PRINT6ADDR(ipaddr);
      PRINTF(", out of memory\n");
      return NULL;
    } else {
      PRINTF("uip_ds6_defrt_add: adding default route to ");
      PRINT6ADDR(ipaddr);
      PRINTF("\n");
    }

    list_push(defaultrouterlist, d);
  }

  uip_ipaddr_copy(&d->ipaddr, ipaddr);

//ADILA EDIT 03/11/14
//d->parentCh = cc2420_get_channel();
/*if(d->parentCh == 0) {
d->parentCh = 26;
}*/
//printf("\n\nINITIALISE D->PARENTCH %d\n\n", d->parentCh);
//-------------------

  if(interval != 0) {
    stimer_set(&d->lifetime, interval);
    d->isinfinite = 0;
  } else {
    d->isinfinite = 1;
  }

  ANNOTATE("#L %u 1\n", ipaddr->u8[sizeof(uip_ipaddr_t) - 1]);

#if UIP_DS6_NOTIFICATIONS
  call_route_callback(UIP_DS6_NOTIFICATION_DEFRT_ADD, ipaddr, ipaddr);
#endif

#if DEBUG != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* DEBUG != DEBUG_NONE */

  return d;
}