Esempio n. 1
0
uint8_t
rpl_is_addr_opp(uip_ipaddr_t *destipaddr)
{
  /* TODO: assumes one dag for now */
  rpl_dag_t *dag;
  dag = rpl_get_any_dag();
  return uip_ipaddr_cmp(destipaddr, &dag->dag_id);
}
Esempio n. 2
0
/*---------------------------------------------------------------------------*/
static void
timeout_handler(void)
{
  rpl_dag_t *dag = rpl_get_any_dag();
  if (dag != NULL) {
    PRINTF("DAG Found\n");
  }
}
Esempio n. 3
0
/* Upon leaving a TSCH network, perform a local repair
 * (cleanup neighbor state, reset Trickle timer etc)
 * To use, set #define TSCH_CALLBACK_LEAVING_NETWORK tsch_rpl_callback_leaving_network */
void
tsch_rpl_callback_leaving_network(void)
{
  rpl_dag_t *dag = rpl_get_any_dag();
  if(dag != NULL) {
    rpl_local_repair(dag->instance);
  }
}
Esempio n. 4
0
/*---------------------------------------------------------------------------*/
static int
is_done(void)
{
#if UIP_CONF_IPV6_RPL
  return rpl_get_any_dag() != NULL;
#else /* UIP_CONF_IPV6_RPL */
  return 1;
#endif /* UIP_CONF_IPV6_RPL */
}
Esempio n. 5
0
/*---------------------------------------------------------------------------*/
const uip_ipaddr_t *
simple_rpl_parent(void)
{
    rpl_dag_t *dag;

    dag = rpl_get_any_dag();
    if(dag != NULL && dag->preferred_parent != NULL) {
        return rpl_get_parent_ipaddr(dag->preferred_parent);
    }
    return NULL;
}
Esempio n. 6
0
void
rpl_opp_routing_reset(void)
{
  rpl_dag_t *dag;
  dag = rpl_get_any_dag();
  if(uip_ds6_is_my_addr(&dag->dag_id)) {
    we_are_root = 1;
    current_rank = ROOT_RANK(dag->instance);
  } else {
    current_rank = INFINITE_RANK;
  }
}
Esempio n. 7
0
/*---------------------------------------------------------------------------*/
const uip_ipaddr_t *
simple_rpl_root(void)
{
    rpl_dag_t *dag;

    dag = rpl_get_any_dag();
    if(dag != NULL) {
        return &dag->dag_id;
    }

    return NULL;
}
Esempio n. 8
0
PROCESS_THREAD(rplstats, ev, data)
{
  uip_ipaddr_t *addr;
  PROCESS_BEGIN();

  etimer_set(&et_ping, 5 * CLOCK_SECOND);

  while(1) {
	  uint16_t content_len;
	  PROCESS_WAIT_EVENT();

	  if(etimer_expired(&et_ping)) 
	  {
		  dag = rpl_get_any_dag();
		  if(dag != NULL) {		  
			  PRINTF("post!\n\r");
			  PRINTF("prefix info, len %d\n\r", dag->prefix_info.length);
			  PRINT6ADDR(&(dag->prefix_info.prefix));
			  PRINTF("\n\r");
			  addr = &(dag->prefix_info.prefix);
			  /* assume 64 bit prefix for now */
			  sprintf(host, "[%02x%02x:%02x%02x:%02x%02x:%02x%02x::1]", 
				  ((u8_t *)addr)[0], ((u8_t *)addr)[1], 
				  ((u8_t *)addr)[2], ((u8_t *)addr)[3], 
				  ((u8_t *)addr)[4], ((u8_t *)addr)[5], 
				  ((u8_t *)addr)[6], ((u8_t *)addr)[7]);
			  PRINTF("host: %s\n\r", host);

			  content_len = create_rank_msg(buf);
			  s = httpd_ws_request(HTTPD_WS_POST, host, NULL, port,
					       path, ct_json,
					       content_len, send_buf);
			  while (s->state != 0) { PROCESS_PAUSE(); }
			  
			  first = 1;
		  	  for (parent = dag->preferred_parent; parent != NULL; parent = parent->next) {
		  	  	  content_len = create_parent_msg(buf, parent, first);
		  	  	  s = httpd_ws_request(HTTPD_WS_POST, host, NULL, port,
		  	  			   path, ct_json,
		  	  			   content_len, send_buf);
				  while (s->state != 0) { PROCESS_PAUSE(); }
		  	  	  first = 0;
		  	  }

		  }
		  
		  etimer_set(&et_ping, 5 * CLOCK_SECOND);

	  }
  }
  
  PROCESS_END();
}
Esempio n. 9
0
/*---------------------------------------------------------------------------*/
int
simple_rpl_init_dag_immediately(void)
{
    struct uip_ds6_addr *root_if;
    int i;
    uint8_t state;
    uip_ipaddr_t *ipaddr = NULL;

    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_PREFERRED &&
                !uip_is_addr_link_local(&uip_ds6_if.addr_list[i].ipaddr)) {
            ipaddr = &uip_ds6_if.addr_list[i].ipaddr;
        }
    }

    if(ipaddr != NULL) {
        root_if = uip_ds6_addr_lookup(ipaddr);
        if(root_if != NULL) {
            rpl_dag_t *dag;
            uip_ipaddr_t prefix;

            rpl_set_root(RPL_DEFAULT_INSTANCE, ipaddr);
            dag = rpl_get_any_dag();

            /* If there are routes in this dag, we remove them all as we are
               from now on the new dag root and the old routes are wrong */
            rpl_remove_routes(dag);
            if(dag->instance != NULL &&
                    dag->instance->def_route != NULL) {
                uip_ds6_defrt_rm(dag->instance->def_route);
                dag->instance->def_route = NULL;
            }

#if CONTIKI_TARGET_TRXEB1120 || CONTIKI_TARGET_ETH1120 || CONTIKI_TARGET_TRXEB2520 || CONTIKI_TARGET_TRXEB1101 || CONTIKI_TARGET_ETH1101 || CONTIKI_TARGET_ETH2520
            uip_ip6addr(&prefix, 0xfc00, 0, 0xabba, 0xabba, 0, 0, 0, 0);
#else /* CONTIKI_TARGET_TRXEB1120 */
            uip_ip6addr(&prefix, 0xfc00, 0, 0, 0, 0, 0, 0, 0);
#endif /* CONTIKI_TARGET_TRXEB1120 */
            rpl_set_prefix(dag, &prefix, 64);
            printf("\r\nsimple_rpl_init_dag: created a new RPL dag\n");
            return 0;
        } else {
            printf("\r\nsimple_rpl_init_dag: failed to create a new RPL DAG\n");
            return -1;
        }
    } else {
        printf("\r\nsimple_rpl_init_dag: failed to create a new RPL DAG, no preferred IP address found\n");
        return -2;
    }
}
Esempio n. 10
0
/*---------------------------------------------------------------------------*/
int
rpl_dag_root_init_dag_immediately(void)
{
  struct uip_ds6_addr *root_if;
  int i;
  uint8_t state;
  uip_ipaddr_t *ipaddr = NULL;

  rpl_dag_root_init();

  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_PREFERRED &&
       !uip_is_addr_linklocal(&uip_ds6_if.addr_list[i].ipaddr)) {
      ipaddr = &uip_ds6_if.addr_list[i].ipaddr;
    }
  }

  if(ipaddr != NULL) {
    root_if = uip_ds6_addr_lookup(ipaddr);
    if(root_if != NULL) {
      rpl_dag_t *dag;
      uip_ipaddr_t prefix;

      rpl_set_root(RPL_DEFAULT_INSTANCE, ipaddr);
      dag = rpl_get_any_dag();

      /* If there are routes in this dag, we remove them all as we are
         from now on the new dag root and the old routes are wrong */
      if(RPL_IS_STORING(dag->instance)) {
        rpl_remove_routes(dag);
      }
      if(dag->instance != NULL &&
         dag->instance->def_route != NULL) {
	uip_ds6_defrt_rm(dag->instance->def_route);
        dag->instance->def_route = NULL;
      }

      uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
      rpl_set_prefix(dag, &prefix, 64);
      PRINTF("RPL: rpl_dag_root_init_dag: created a new RPL dag\n");
      return 0;
    } else {
      PRINTF("RPL: rpl_dag_root_init_dag: failed to create a new RPL DAG\n");
      return -1;
    }
  } else {
    PRINTF("RPL: rpl_dag_root_init_dag: failed to create a new RPL DAG, no preferred IP address found\n");
    return -2;
  }
}
Esempio n. 11
0
/*---------------------------------------------------------------------------*/
static void
send_packet(void *ptr)
{
	struct data_msg msg;
	static uint8_t seqno;
	uint16_t rank;
	uint16_t num_neighbors;

	rpl_dag_t *dag;

	memset(&msg,0,sizeof(msg));
	seqno++;
	msg.seqno = seqno;
	//linkaddr_copy(&parent, &linkaddr_null);

	/* Let's suppose we have only one instance */
	dag = rpl_get_any_dag();
	/*
	if(dag !=NULL) {
		preferred_parent = dag->preferred_parent;
		if(preferred_parent != NULL) {
			uip_ds6_nbr_t *nbr;
			nbr = uip_ds6_nbr_lookup(rpl_get_parent_ipaddr(preferred_parent));
			if(nbr !=NULL){
				/* USE parts of the IPV6 address as the parent address, in reversed byte order. */
/*		        parent.u8[LINKADDR_SIZE - 1] = nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 2];
		        parent.u8[LINKADDR_SIZE - 2] = nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 1];
		        parent_etx = rpl_get_parent_rank((uip_lladdr_t *) uip_ds6_nbr_get_ll(nbr)) / 2;
			}
		}
		*/
		rank = dag->rank;
		num_neighbors = uip_ds6_nbr_num();
	/*} else {
		rank = 0;
		num_neighbors = 0;
	}*/
	msg.len = sizeof(struct data_msg) / sizeof(uint16_t);
	msg.clock = clock_time();

	msg.rank = rank;
	msg.num_neighbors = num_neighbors;
	PRINTF("DATA send to %d: ",server_ipaddr.u8[sizeof(server_ipaddr.u8) - 1]);
	PRINTF("seqno: %d, len: %d, clock: %u, rank: %d, num_neighbors: %d \n",
			msg.seqno,msg.len,msg.clock,msg.rank,msg.num_neighbors);

	//common_net_print();

	uip_udp_packet_sendto(client_conn, &msg, sizeof(msg),
	                        &server_ipaddr, UIP_HTONS(UDP_SERVER_PORT));
}
Esempio n. 12
0
void
rpl_opp_routing_init(void)
{
  nbr_table_register(forwarder_set, NULL);
  memb_init(&forwarder_set_memb);
  rpl_dag_t *dag;
  dag = rpl_get_any_dag();
  if(uip_ds6_is_my_addr(&dag->dag_id)) {
    we_are_root = 1;
    current_rank = ROOT_RANK(dag->instance);
  } else {
    process_start(&rpl_forwarder_set_update_process, NULL);
  }
}
Esempio n. 13
0
/*---------------------------------------------------------------------------*/
static void
handle_periodic_timer(void *ptr)
{
  rpl_purge_routes();
  rpl_recalculate_ranks();

  /* handle DIS */
#if UIP_ND6_ENGINE != UIP_ND6_ENGINE_RPL
#if RPL_DIS_SEND
  next_dis++;
  if(rpl_get_any_dag() == NULL && next_dis >= RPL_DIS_INTERVAL) {
    next_dis = 0;
    dis_output(NULL);
  }
#endif
#else
 if(rpl_get_any_dag() == NULL && stimer_expired(&uip_ds6_timer_dis)) {
	discount++;
	dis_output(NULL); 
	stimer_set(&uip_ds6_timer_dis, rs_rtx_time(discount));
 }
#endif
  ctimer_reset(&periodic_timer);
}
Esempio n. 14
0
/*---------------------------------------------------------------------------*/
static void
handle_periodic_timer(void *ptr)
{
  rpl_purge_routes();
  rpl_recalculate_ranks();

  /* handle DIS */
#if RPL_DIS_SEND
  next_dis++;
  if(rpl_get_any_dag() == NULL && next_dis >= RPL_DIS_INTERVAL) {
    next_dis = 0;
    dis_output(NULL);
  }
#endif
  ctimer_reset(&periodic_timer);
}
/*---------------------------------------------------------------------------*/
static void
net_init(uip_ipaddr_t *br_prefix)
{
  uip_ipaddr_t global_ipaddr;

  if(br_prefix) { /* We are RPL root. Will be set automatically
                     as TSCH pan coordinator via the tsch-rpl module */
    memcpy(&global_ipaddr, br_prefix, 16);
    uip_ds6_set_addr_iid(&global_ipaddr, &uip_lladdr);
    uip_ds6_addr_add(&global_ipaddr, 0, ADDR_AUTOCONF);
    rpl_set_root(RPL_DEFAULT_INSTANCE, &global_ipaddr);
    rpl_set_prefix(rpl_get_any_dag(), br_prefix, 64);
    rpl_repair_root(RPL_DEFAULT_INSTANCE);
  }

  NETSTACK_MAC.on();
}
Esempio n. 16
0
/*----------------------------------------------------------------------------*/
static void _demo_udp_sendMsg(uint32_t seqID)
{
	char 		pc_buf[MAX_S_PAYLOAD_LEN];
	int			i;
	i = sprintf(pc_buf, "%lu | ", seqID);
	if (seqID != 0)
		l_expSeqID++;
	LOG_INFO("Lost packets (%lu)\n\r", l_expSeqID - seqID);
    rpl_dag_t *dag = rpl_get_any_dag();
    if(dag && dag->instance->def_route) {
    	_demo_add_ipaddr(pc_buf + i, &dag->instance->def_route->ipaddr);
    } else {
      sprintf(pc_buf + i, "(null)");
    }
    LOG_INFO("Send message: %s\n\r",pc_buf);
	uip_udp_packet_sendto(pst_conn, pc_buf, strlen(pc_buf),&un_server_ipaddr,UIP_HTONS(__SERVER_PORT));
} /* _demo_udp_sendMsg */
Esempio n. 17
0
/*---------------------------------------------------------------------------*/
static void
create_dag_callback(void *ptr)
{
    const uip_ipaddr_t *root, *ipaddr;
    printf("\r\nCtimer timer callback for dag root");
    root = simple_rpl_root();
    ipaddr = simple_rpl_global_address();

    if(root == NULL || uip_ipaddr_cmp(root, ipaddr)) {
        /* The RPL network we are joining is one that we created, so we
           become root. */
        printf("\r\n No root available, we'll make ourself as root ");
        if(to_become_root) {
            simple_rpl_init_dag_immediately();
            to_become_root = 0;
        }
    } else {
        rpl_dag_t *dag;

        dag = rpl_get_any_dag();
#if 1
        printf("\r\nFound a network we did not create, ");
        printf("version %d grounded %d preference %d used %d joined %d rank %d\n",
               dag->version, dag->grounded,
               dag->preference, dag->used,
               dag->joined, dag->rank);
        printf("\r\n Root's nodeid = 0x%2X%2X. ", root->u8[14], root->u8[15]);
#endif /* _DEBUG_ */

        /* We found a RPL network that we did not create so we just join
           it without becoming root. But if the network has an infinite
           rank, we assume the network has broken, and we become the new
           root of the network. */

        if(dag->rank == INFINITE_RANK) {
            if(to_become_root) {
                printf("\r\n The dag rank is infinite, so we'll become the root");
                simple_rpl_init_dag_immediately();
                to_become_root = 0;
            }
        }

        /* Try again after the grace period */
        ctimer_set(&c, RPL_DAG_GRACE_PERIOD, create_dag_callback, NULL);
    }
}
Esempio n. 18
0
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
	rpl_dag_t *dag;
	rpl_parent_t *parent;
	int32_t strpos = 0;
	const uip_ipaddr_t *addr;
	addr = &uip_ds6_if.addr_list[1].ipaddr;
	dag = rpl_get_any_dag();

	parent_index = 0;

	if (dag != NULL)
	{
		/* seek to the parents entry and return it */
		strpos += sprintf(&(buffer[strpos]),"{\"node\":\"n%x\"",addr->u8[15]); // last addr byte of mote
		strpos += sprintf(&(buffer[strpos]),",\"nbr\":{");
		parent = nbr_table_head(rpl_parents);  // addr of first neighbor
		while (parent != NULL)
		{
			etx_table[parent_index].nbr_addr = rpl_get_parent_ipaddr(parent)->u8[15];
			etx_table[parent_index].nbr_etx = rpl_get_parent_link_metric(parent);
			etx_table[parent_index].p = parent;
			strpos += sprintf(&(buffer[strpos]),"\"n%x\":%u,",etx_table[parent_index].nbr_addr, etx_table[parent_index].nbr_etx);
			parent = nbr_table_next(rpl_parents, parent);
			parent_index++;
		}
		PRINTF("parent_index:%d\n",parent_index);
	}
	else
	{ /* no DAG */
		strpos += sprintf(&(buffer[strpos]),"{}\n");
	}
	//PRINTF("strpos: %ld\n", strpos);
	//rpl_print_neighbor_list(); // get parents for debug purposes
	//PRINTF("buf_parents: %s\n", buffer);
	strpos += sprintf(&(buffer[strpos-1]),"}}\n"); //replace the last comma
	//PRINTF("strpos-after: %ld\n", strpos);
	REST.set_header_content_type(response, APPLICATION_JSON);
	REST.set_header_max_age(response, res_etx.periodic->period / CLOCK_SECOND);
	//*offset = -1;  // try to fix Copper response
	REST.set_response_payload(response, buffer, snprintf((char *)buffer, preferred_size, "%s", buffer));

	/* The REST.subscription_handler() will be called for observable resources by the REST framework. */
}
Esempio n. 19
0
/*---------------------------------------------------------------------------*/
static void
create_rpl_dag(uip_ipaddr_t *ipaddr)
{
  struct uip_ds6_addr *root_if;
  root_if = uip_ds6_addr_lookup(ipaddr);
  if(root_if != NULL) {
    rpl_dag_t *dag;
    uip_ipaddr_t prefix;

    rpl_set_root(RPL_DEFAULT_INSTANCE, ipaddr);
    dag = rpl_get_any_dag();
    uip_ip6addr(&prefix, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
    rpl_set_prefix(dag, &prefix, 64);
    printf("created a new RPL dag\n");
  } else {
    printf("failed to create a new RPL DAG\n");
  }
}
Esempio n. 20
0
/* Set TSCH EB period based on current RPL DIO period.
 * To use, set #define RPL_CALLBACK_PARENT_SWITCH tsch_rpl_callback_new_dio_interval */
void
tsch_rpl_callback_new_dio_interval(uint8_t dio_interval)
{
  /* Transmit EBs only if we have a valid rank as per 6TiSCH minimal */
  rpl_dag_t *dag = rpl_get_any_dag();
  if(dag != NULL && dag->rank != INFINITE_RANK) {
    /* If we are root set TSCH as coordinator */
    if(dag->rank == ROOT_RANK(dag->instance)) {
      tsch_set_coordinator(1);
    }
    /* Set EB period */
    tsch_set_eb_period(TSCH_EB_PERIOD);
    /* Set join priority based on RPL rank */
    tsch_set_join_priority(DAG_RANK(dag->rank, dag->instance) - 1);
  } else {
    tsch_set_eb_period(0);
  }
}
Esempio n. 21
0
/*---------------------------------------------------------------------------*/
static void
create_dag_callback(void *ptr)
{
  const uip_ipaddr_t *root, *ipaddr;

  root = simple_rpl_root();
  ipaddr = simple_rpl_global_address();

  if(root == NULL || uip_ipaddr_cmp(root, ipaddr)) {
    /* The RPL network we are joining is one that we created, so we
       become root. */
    if(to_become_root) {
      simple_rpl_init_dag_immediately();
      to_become_root = 0;
    }
  } else {
    rpl_dag_t *dag;

    dag = rpl_get_any_dag();
#if DEBUG
    printf("Found a network we did not create\n");
    printf("version %d grounded %d preference %d used %d joined %d rank %d\n",
           dag->version, dag->grounded,
           dag->preference, dag->used,
           dag->joined, dag->rank);
#endif /* DEBUG */

    /* We found a RPL network that we did not create so we just join
       it without becoming root. But if the network has an infinite
       rank, we assume the network has broken, and we become the new
       root of the network. */

    if(dag->rank == INFINITE_RANK) {
      if(to_become_root) {
        simple_rpl_init_dag_immediately();
        to_become_root = 0;
      }
    }

    /* Try again after the grace period */
    ctimer_set(&c, RPL_DAG_GRACE_PERIOD, create_dag_callback, NULL);
  }
}
void
collect_common_net_print(void)
{
  rpl_dag_t *dag;
  uip_ds6_route_t *r;

  /* Let's suppose we have only one instance */
  dag = rpl_get_any_dag();
  if(dag->preferred_parent != NULL) {
    PRINTF("Preferred parent: ");
    PRINT6ADDR(rpl_get_parent_ipaddr(dag->preferred_parent));
    PRINTF("\n");
  }
  for(r = uip_ds6_route_head();
      r != NULL;
      r = uip_ds6_route_next(r)) {
    PRINT6ADDR(&r->ipaddr);
  }
  PRINTF("---\n");
}
Esempio n. 23
0
void collect_common_net_print(void)
{
	rpl_dag_t *dag;
	int i;
	/* Let's suppose we have only one instance. */
	dag = rpl_get_any_dag();
	if(dag->preferred_parent != NULL) {
		PRINTF("Preferred parent: ");
		PRINT6ADDR(&uip_ds6_routing_table[i].ipaddr);
		PRINTF("\n");
	}
	PRINTF("Route entries:\n");
	for(i = 0; i < UIP_DS6_ROUTE_NB; i++) {
		if(uip_ds6_routing_table[i].isused) {
			PRINT6ADDR(&uip_ds6_routing_table[i].ipaddr);
			PRINTF("\n");
		}
	}
	PRINTF("---\n");
}
Esempio n. 24
0
void
collect_common_send()
{
  /* Send the query reply back to the sink */
  rpl_parent_t *preferred_parent;
  rpl_dag_t *dag;

  query_reply_t qrpkt;
  dag = rpl_get_any_dag();
  if(dag != NULL) {
    preferred_parent = dag->preferred_parent;
  if(preferred_parent != NULL) {
    uip_ds6_nbr_t *nbr;
  nbr = uip_ds6_nbr_lookup(rpl_get_parent_ipaddr(preferred_parent));
  if(nbr != NULL) {
    // Use parts of the IPv6 address as the parent address, in reversed byte order. 
        qrpkt.parent_etx = (uint16_t)preferred_parent->link_metric + preferred_parent->rank;
      }
    }
  }
  uip_udp_packet_sendto(client_conn, &qrpkt, sizeof(qrpkt),
      &server_ipaddr, UIP_HTONS(UDP_SERVER_PORT));
}
Esempio n. 25
0
/*---------------------------------------------------------------------------*/
static void
timeout_handler(void)
{
  static int seq_id;
  char buf[MAX_PAYLOAD_LEN];
  int i;
  uip_ip6addr_t *globaladdr = NULL;
  uint16_t dest_port = CETIC_6LBR_NODE_INFO_PORT;
  int has_dest = 0;
  rpl_dag_t *dag;

  uip_ds6_addr_t *addr_desc = uip_ds6_get_global(ADDR_PREFERRED);
  if(addr_desc != NULL) {
    globaladdr = &addr_desc->ipaddr;
    dag = rpl_get_any_dag();
    if(dag) {
      uip_ipaddr_copy(&dest_addr, globaladdr);
      memcpy(&dest_addr.u8[8], &dag->dag_id.u8[8], sizeof(uip_ipaddr_t) / 2);
      has_dest = 1;
    }
  }

  if(has_dest) {
    if(client_conn == NULL) {
      PRINTF("UDP-CLIENT: address destination: ");
      PRINT6ADDR(&dest_addr);
      PRINTF("\n");
      client_conn = udp_new(&dest_addr, UIP_HTONS(dest_port), NULL);

      if(client_conn != NULL) {
        PRINTF("Created a connection with the server ");
        PRINT6ADDR(&client_conn->ripaddr);
        PRINTF(" local/remote port %u/%u\n",
               UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
      } else {
        PRINTF("Could not open connection\n");
      }
    } else {
      if(memcmp(&client_conn->ripaddr, &dest_addr, sizeof(uip_ipaddr_t)) != 0) {
        PRINTF("UDP-CLIENT: new address destination: ");
        PRINT6ADDR(&dest_addr);
        PRINTF("\n");
        uip_udp_remove(client_conn);
        client_conn = udp_new(&dest_addr, UIP_HTONS(dest_port), NULL);
        if(client_conn != NULL) {
          PRINTF("Created a connection with the server ");
          PRINT6ADDR(&client_conn->ripaddr);
          PRINTF(" local/remote port %u/%u\n",
                 UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
        } else {
          PRINTF("Could not open connection\n");
        }
      }
    }
    if(client_conn != NULL) {
      PRINTF("Client sending to: ");
      PRINT6ADDR(&client_conn->ripaddr);
      i = sprintf(buf, "%d | ", ++seq_id);
      dag = rpl_get_any_dag();
      if(dag && dag->instance->def_route) {
        add_ipaddr(buf + i, &dag->instance->def_route->ipaddr);
      } else {
        sprintf(buf + i, "(null)");
      }
      PRINTF(" (msg: %s)\n", buf);
      uip_udp_packet_send(client_conn, buf, strlen(buf));
    } else {
      PRINTF("No connection created\n");
    }
  } else {
    PRINTF("No address configured\n");
  }
}
Esempio n. 26
0
/*
 * this function produce a string @buf in form of json data
 * example of buf:
 *
 * {'clk':%d,'syn':%d,'cpu':%d,'lpm':%d,'tras':%d,'lst':%d,
 * 'parent':%s,'etx':%d,'rt':%d,\'nbr':%d,'bea_itv':%d,'sen':%d}
 *
 */
void collectd_prepare_data()
{
	uint16_t parent_etx;
	uint16_t rtmetric;
	uint16_t num_neighbors;
	uint16_t beacon_interval;
	rpl_parent_t *preferred_parent;
	uip_lladdr_t lladdr_parent;
	rpl_dag_t *dag;

//copied from collect-view.c
	static unsigned long last_cpu, last_lpm, last_transmit, last_listen;
	unsigned long cpu, lpm, transmit, listen;
	u16_t clock, timesynch_time;

	clock = clock_time();
#if TIMESYNCH_CONF_ENABLED
	timesynch_time = timesynch_time();
#else /* TIMESYNCH_CONF_ENABLED */
	timesynch_time = 0;
#endif /* TIMESYNCH_CONF_ENABLED */

	/*save to buf */
	blen = 0;
	ADD("{'clk':%u,'syn':%u,", clock, timesynch_time);

	energest_flush();
	cpu = energest_type_time(ENERGEST_TYPE_CPU) - last_cpu;
	lpm = energest_type_time(ENERGEST_TYPE_LPM) - last_lpm;
	transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT) - last_transmit;
	listen = energest_type_time(ENERGEST_TYPE_LISTEN) - last_listen;

	/* Make sure that the values are within 16 bits. If they are larger,
	 we scale them down to fit into 16 bits. */
	//TODO: why do i need to scale down to 16 bit?
	while(cpu >= 65536ul || lpm >= 65536ul ||
			transmit >= 65536ul || listen >= 65536ul) {
		cpu /= 2;
		lpm /= 2;
		transmit /= 2;
		listen /= 2;
	}
	/* prepare for next calling */
	last_cpu = energest_type_time(ENERGEST_TYPE_CPU);
	last_lpm = energest_type_time(ENERGEST_TYPE_LPM);
	last_transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT);
	last_listen = energest_type_time(ENERGEST_TYPE_LISTEN);

	/* save to buf */
	ADD("'cpu':%u,'lpm':%u,'tras':%u,'lst':%u,",
			(u16_t)cpu, (u16_t)lpm,
			(u16_t)transmit, (u16_t)listen);

	/* initial value, if there's not any dag */
	parent_etx = 0;
	rtmetric = 0;
	beacon_interval = 0;
	num_neighbors = 0;
	/* Let's suppose we have only one instance */
	dag = rpl_get_any_dag();
	if(dag != NULL) {
		preferred_parent = dag->preferred_parent;
		if(preferred_parent != NULL) {
			uip_ds6_nbr_t *nbr;
			nbr = uip_ds6_nbr_lookup(&preferred_parent->addr);
			if(nbr != NULL) {
				//PRINT6ADDR(&nbr->lladdr);
				memcpy(&lladdr_parent, &nbr->lladdr, sizeof(uip_lladdr_t));
				parent_etx = neighbor_info_get_metric((rimeaddr_t *) &nbr->lladdr) / 2;
			}
		}
		rtmetric = dag->rank;
		beacon_interval = (uint16_t) ((2L << dag->instance->dio_intcurrent) / 1000);
		num_neighbors = RPL_PARENT_COUNT(dag);
	}

	char lladdr_parent_str[30];
	u8_t lladdr_str_len;
	lladdr_str_len = lladdr_print(&lladdr_parent, lladdr_parent_str, 30);

	ADD("'parent':'%s',", lladdr_parent_str);
	ADD("'etx':%u,'rt':%u,'nbr':%u,'bea_itv':%u,",
			parent_etx, rtmetric, num_neighbors,
			beacon_interval);

	//collectd_arch_read_sensors();
#if CONTIKI_TARGET_SKY
	u8_t sensors[MAX_SENSORS_BUF_SIZE];
	//PRINTF("oh,sky\n");
	if (collect_view_arch_read_sensors(sensors, MAX_SENSORS_BUF_SIZE) >= 0) {
		ADD("'sen':'%s',", sensors);
	}
#endif
	ADD("}");
}
Esempio n. 27
0
void
parents_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
  int32_t strpos = 0;
  volatile uint8_t i;
  rpl_dag_t *dag;
  rpl_parent_t *parent;

  size_t len = 0;
  uint8_t index;
  const char *pstr;
  uint8_t count;

  dag = rpl_get_any_dag();

  if(dag != NULL) {

		/* count the number of routes and return the total */
		count = 0;
		parent = nbr_table_head(rpl_parents);
		while(parent != NULL) {
			parent = nbr_table_next(rpl_parents, parent);
			count++;
		}

		if ((len = REST.get_query_variable(request, "index", &pstr))) {

			index = (uint8_t)atoi(pstr);

			if (index >= count) {
				strpos = snprintf((char *)buffer, preferred_size, "{}");
			} else {
				/* seek to the route entry and return it */
				i = 0;
				parent = nbr_table_head(rpl_parents);
				while(parent != NULL) {
					if (i == index) {
						break;
					}
					parent = nbr_table_next(rpl_parents, parent);
					i++;
				}

				if (parent == dag->preferred_parent) {
					strpos = create_parent_msg((char *)buffer, parent, 1);
				} else {
					strpos = create_parent_msg((char *)buffer, parent, 0);
				}
			}

			REST.set_header_content_type(response, APPLICATION_JSON);

		} else { /* index not provided */
			strpos += snprintf((char *)buffer, preferred_size, "%d", count);
		}

	} else { /* no DAG */
		strpos += snprintf((char *)buffer, preferred_size, "{\"err\": \"no DAG\"}");
		REST.set_header_content_type(response, APPLICATION_JSON);
	}

	*offset = -1;
	REST.set_response_payload(response, buffer, strpos);

}
Esempio n. 28
0
/*-----------------------------------------------------------------------*/
void collect_common_send(void)
{
	static uint8_t seqno;
	struct {
		uint8_t seqno;
		uint8_t for_alignment;
		struct collect_view_data_msg msg;
		uint8_t hop_count;
		uint8_t lifetime;
		uint16_t data[72];
	} msg;
	
	uint16_t parent_etx;
	uint16_t rtmetric;
	uint16_t num_neighbors;
	uint16_t beacon_interval;
	rpl_parent_t *preferred_parent;
	rimeaddr_t parent;
	rpl_dag_t *dag;

	if(sender_conn == NULL) {
		return;
	}
	memset(&msg, 0, sizeof(msg));
	seqno++;
	if(seqno == 0) {
		seqno = 128; /* Wrap to 128 to identify restarts */
	}
	msg.seqno = seqno;

	rimeaddr_copy(&parent, &rimeaddr_null);
	parent_etx = 0;

	/* Let's suppose we have only one instance. */
	dag = rpl_get_any_dag();
	if(dag != NULL) {
		preferred_parent = dag->preferred_parent;
		if(preferred_parent != NULL) {
			uip_ds6_nbr_t *nbr;
			nbr = uip_ds6_nbr_lookup(&preferred_parent->addr);
			if(nbr != NULL) {
				/* Use parts of the IPv6 address as the parent address,
				 * in reversed byte order. */
				parent.u8[RIMEADDR_SIZE - 1] = 
					nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 2];
				parent.u8[RIMEADDR_SIZE - 2] =
					nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 1];
				parent_etx = 
				    neighbor_info_get_metric((rimeaddr_t *)&nbr->lladdr)/2;
			}
		}

		rtmetric = dag->rank;
		beacon_interval = 
			(uint16_t) ((2L << dag->instance->dio_intcurrent) / 1000);
		num_neighbors = RPL_PARENT_COUNT(dag);
	} else {
		rtmetric = 0;
		beacon_interval = 0;
		num_neighbors = 0;
	}
	
	collect_view_construct_message(&msg.msg, &parent, parent_etx, rtmetric,
									num_neighbors, beacon_interval);
	memset(msg.data, 12, sizeof(msg.data));
//	PRINTF("The size of the packet is: %d\n", sizeof(msg));
	PRINTF("DATA send NO %d to %d\n", seqno,
				sink_ipaddr.u8[sizeof(sink_ipaddr.u8) - 1]);
//	printf("Send a message: %u \n", seqno);
	uip_udp_packet_sendto(sender_conn, &msg, sizeof(msg),
						  &sink_ipaddr, UIP_HTONS(UDP_SINK_PORT));
//	PRINTF("The size of the packet is: %u\n", sizeof(msg));
}
Esempio n. 29
0
static void send_packet(void *ptr)
{
    static uint16_t seqno = 0;
    struct app_msg msg;

    uint16_t parent_etx;
    uint16_t rtmetric;
    uint16_t num_neighbors;
    uint16_t beacon_interval;
    rpl_parent_t *preferred_parent;
    rimeaddr_t parent;
    rpl_dag_t *dag;

    if(sender_conn == NULL) return;

    memset(&msg, 0, sizeof(msg));

    seqno++;
    msg.seqno = seqno;

    rimeaddr_copy(&parent, &rimeaddr_null);
    parent_etx = 0;
    // Let's suppose we have only one instance. 
    dag = rpl_get_any_dag();
    if( dag != NULL)
    {
        preferred_parent = dag->preferred_parent;
        if(preferred_parent != NULL)
        {
            uip_ds6_nbr_t *nbr;
            nbr = uip_ds6_nbr_lookup(&preferred_parent->next);
            if(nbr != NULL)
            {
                // Use parts of the ipv6 address as the parent address.
                  // In reversed byte order. 
                parent.u8[RIMEADDR_SIZE - 1] =
                    nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 2];
                parent.u8[RIMEADDR_SIZE - 2] = 
                    nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 1];
                parent_etx =
                    rpl_get_parent_rank((rimeaddr_t *)uip_ds6_nbr_get_ll(nbr)) / 2;
            }
        }
        rtmetric = dag->rank;
        beacon_interval =
            (uint16_t)((2L<<dag->instance->dio_intcurrent) / 1000);
        num_neighbors = RPL_PARENT_COUNT(dag);

    } else {
        rtmetric = 0;
        beacon_interval = 0;
        num_neighbors = 0;
    }
  
    msg.parent_etx = parent_etx;
    msg.rtmetric = rtmetric;
    msg.num_neighbors = num_neighbors;
    msg.beacon_interval = beacon_interval;
    memset(msg.data, 11, sizeof(msg.data) - 2);
    msg.data[10] = seqno;
    uip_udp_packet_sendto(sender_conn, &msg, sizeof(msg),
                           &sink_ipaddr, UIP_HTONS(UDP_SINK_PORT));
    PRINTF("DATA send NO %d to %d * msg size %u\n", seqno,
                sink_ipaddr.u8[sizeof(sink_ipaddr.u8) - 1], sizeof(msg));
    PRINTF("Sink addr : ");
    PRINT6ADDR(&sink_ipaddr);
    PRINTF("\n");
                
}
Esempio n. 30
0
static void
timeout_handler(void)
{
  static int seq_id;
  char buf[MAX_PAYLOAD_LEN];
  int i;
  uip_ipaddr_t *globaladdr = NULL;
  uip_ipaddr_t newdest_addr;
  uint16_t dest_port = use_user_dest_addr ? user_dest_port : 3000;
  int has_dest=0;

  if ( use_user_dest_addr ) {
	uip_ipaddr_copy(&newdest_addr, &user_dest_addr);
	has_dest=1;
  } else if((globaladdr = &uip_ds6_get_global(-1)->ipaddr) != NULL) {
#if UIP_CONF_IPV6_RPL
    rpl_dag_t *dag = rpl_get_any_dag();
    uip_ipaddr_copy(&newdest_addr, globaladdr);
    memcpy(&newdest_addr.u8[8], &dag->dag_id.u8[8], sizeof(uip_ipaddr_t) / 2);
    has_dest = dag == NULL ? 0 : 1;
#else
    uip_ipaddr_t * defrt = uip_ds6_defrt_choose();
    if ( defrt != NULL ) {
      uip_ipaddr_copy(&newdest_addr, defrt);
      has_dest=1;
    }
#endif
  }

  if (client_conn == NULL) {
    if (has_dest) {
        /* At least one prefix announced : building of the global address to reach using prefixes */
        memcpy(dest_addr, &newdest_addr, sizeof(uip_ipaddr_t));
        PRINTF("UDP-CLIENT: address destination: ");
        PRINT6ADDR(dest_addr);
        PRINTF("\n");
        client_conn = udp_new(dest_addr, UIP_HTONS(dest_port), NULL);

        udp_bind(client_conn, UIP_HTONS(3001));
		PRINTF("Created a connection with the server ");
		PRINT6ADDR(&client_conn->ripaddr);
		PRINTF(" local/remote port %u/%u\n",
		UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
		print_local_addresses();
    } else {
      PRINTF("No address configured yet\n");
    }
  }
  if (client_conn != NULL) {
    if(memcmp(&client_conn->ripaddr, &newdest_addr, sizeof(uip_ipaddr_t)) != 0) {
      PRINTF("UPD-CLIENT : new address, connection changed\n");
      memcpy(dest_addr, &newdest_addr, sizeof(uip_ipaddr_t));
      client_conn = udp_new(dest_addr, UIP_HTONS(3000), NULL);
      udp_bind(client_conn, UIP_HTONS(3001));
      PRINTF("Created a connection with the server ");
      PRINT6ADDR(&client_conn->ripaddr);
      PRINTF(" local/remote port %u/%u\n",
      UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
      print_local_addresses();
    }
    if ( udp_client_run ) {
      PRINTF("Client sending to: ");
      PRINT6ADDR(&client_conn->ripaddr);
      i = sprintf(buf, "%d | ", ++seq_id);
#if UIP_CONF_IPV6_RPL
      rpl_dag_t *dag = rpl_get_any_dag();
      add_ipaddr(buf + i, &dag->instance->def_route->ipaddr);
#endif
      PRINTF(" (msg: %s)\n", buf);
      #if SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION
      uip_udp_packet_send(client_conn, buf, UIP_APPDATA_SIZE);
      #else /* SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION */
      uip_udp_packet_send(client_conn, buf, strlen(buf));
      #endif /* SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION */
    }
  }
}