Exemple #1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t *addr;

  PROCESS_BEGIN();

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
	PRINTF("SIMPLE_UNICAST_SENDER: Unicast attempt!\n");
    addr = servreg_hack_lookup(SERVICE_ID);
	
    if(addr != NULL) {
      static unsigned int message_number;
      char buf[20];

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf(" \n");
      sprintf(buf, "Message %d", message_number);
      message_number++;
      simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}
Exemple #2
0
PROCESS_THREAD(udp_server_process, ev, data)
{

  PROCESS_BEGIN();
  static uip_ipaddr_t ipaddr;
  rpl_dag_t *dag;
  int i;
  uint8_t state;
  static struct etimer timer;
  static struct simple_udp_connection connection;
  static int packet = 0;

  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); 

  //uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
  //dag = rpl_set_root(RPL_DEFAULT_INSTANCE, (uip_ip6addr_t*) &ipaddr);
  //rpl_set_prefix(dag, &ipaddr, 64);

  simple_udp_register(&connection, UDP_PORT-1, NULL, UDP_PORT, receiver);

  printf("IPv6 addresses: ");
  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
    state = uip_ds6_if.addr_list[i].state;
    if(uip_ds6_if.addr_list[i].isused &&
       (state == ADDR_TENTATIVE 
       || state == ADDR_PREFERRED)) {
            uip_debug_ipaddr_print(
               &uip_ds6_if.addr_list[i].ipaddr);
            printf("\n");
    }
  }
  printf("UDP server started\n");
  etimer_set(&timer, CLOCK_SECOND >> 1);
  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
    etimer_restart(&timer);
    leds_toggle(LEDS_GREEN);
  }
  PROCESS_END();
}
Exemple #3
0
/*---------------------------------------------------------------------------*/
static void
set_global_address(void)
{
  uip_ipaddr_t ipaddr;
  int i;
  uint8_t state;

  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);

  printf("IPv6 addresses: ");
  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
    state = uip_ds6_if.addr_list[i].state;
    if(uip_ds6_if.addr_list[i].isused &&
       (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
      uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
      printf("\n");
    }
  }
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(border_router_process, ev, data)
{
  static struct etimer et;

  PROCESS_BEGIN();

/* While waiting for the prefix to be sent through the SLIP connection, the future
 * border router can join an existing DAG as a parent or child, or acquire a default
 * router that will later take precedence over the SLIP fallback interface.
 * Prevent that by turning the radio off until we are initialized as a DAG root.
 */
  prefix_set = 0;

  PROCESS_PAUSE();

  PRINTF("RPL-Border router started\n");

  
  /* Request prefix until it has been received */
  while(!prefix_set) {
    etimer_set(&et, CLOCK_SECOND);
    request_prefix();
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
    PRINTF("Waiting for prefix\n");
  }

  PRINTF("Obtained prefix: ");
  uip_debug_ipaddr_print(&prefix);
  PRINTF("\n");

  rpl_tools_init(&prefix);

  etimer_set(&et, CLOCK_SECOND * 60);
  while(1) {
    print_network_status();
    PROCESS_YIELD_UNTIL(etimer_expired(&et));
    etimer_reset(&et);
  }
  PROCESS_END();
}
Exemple #5
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(sender_node_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t addr;

  PROCESS_BEGIN();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));

    uip_ip6addr(&addr, 0xaaaa, 0, 0, 0, 0x0201, 0x001, 0x001, 0x001);

    {
      static unsigned int message_number;
      char buf[20];

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(&addr);
      printf("\n");
      sprintf(buf, "Message %d", message_number);
      message_number++;
      simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, &addr);
    }
  }

  PROCESS_END();
}
Exemple #6
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(sender_process, ev, data)
{

  PROCESS_BEGIN();
  static uip_ipaddr_t ipaddr, unicastaddr;
  int i;
  uint8_t state;
  static struct etimer timer;
  static struct simple_udp_connection connection;
  static char *packet = "Supertest";

  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); 

  uip_ip6addr(&unicastaddr, 0xfe80, 0, 0, 0, 0xc30c, 0, 0, 2);

  simple_udp_register(&connection, UDP_PORT, NULL, UDP_PORT, NULL);

  printf("IPv6 addresses: ");
  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
    state = uip_ds6_if.addr_list[i].state;
    if(uip_ds6_if.addr_list[i].isused &&
       (state == ADDR_TENTATIVE 
       || state == ADDR_PREFERRED)) {
            uip_debug_ipaddr_print(
               &uip_ds6_if.addr_list[i].ipaddr);
            printf("\n");
    }
  }
  etimer_set(&timer, CLOCK_SECOND);
  while(1) {
    printf("STO MANDANDO UNICAST\n");
    simple_udp_sendto(&connection, packet, strlen(packet)+1, &unicastaddr);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
    etimer_restart(&timer);
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
static uip_ipaddr_t *
set_global_address(void)
{
  static uip_ipaddr_t ipaddr;
  int i;
  uint8_t state;

  /* Assign a unique local address (RFC4193,
     http://tools.ietf.org/html/rfc4193). */
  uip_ip6addr(&ipaddr, 0xfc00, 0, 0, 0, 0, 0, 0, 0);
  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);

  printf("IPv6 addresses: ");
  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
    state = uip_ds6_if.addr_list[i].state;
    if(uip_ds6_if.addr_list[i].isused &&
       (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
      uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
    }
  }
  printf("\r\n");
  return &ipaddr;
}
Exemple #8
0
void lowlevel_init()
{
  rimeaddr_t rimeaddr;
  uint16_t *fsize = (uint16_t *)0x1FF8004C;
  uint16_t *uid96 = (uint16_t *)0x1FF80050;
  uint32_t *dbgmcu_id = (uint32_t *)0xE0042000;
  uint16_t uid16;

  dbg_setup_uart();

  printf("\nInitialising\n");

  printf("Device ID: 0x%03x, silicon rev: 0x%04x\n", 
         (unsigned int)*dbgmcu_id & 0x0fff, 
         (unsigned int)(*dbgmcu_id >> 16) & 0xffff);
  printf("Flash size is %d kB\n", *fsize);
  printf("UID96 is %04x %04x %04x %04x %04x %04x\n", 
         uid96[0], uid96[1], uid96[2], uid96[3], uid96[4], uid96[5]);
  uid16 = u101_chksum(0, (uint8_t *)uid96, 12);
  printf("Pseudo-UID16 is %02x\n", uid16);

  clock_init();
  rtimer_init();
  process_init();
  process_start(&etimer_process, NULL);
  ctimer_init();
  serial_line_init();
  leds_init();

#ifdef U101_RF231
  printf("Low-level networking init\n");
  queuebuf_init();
  NETSTACK_RADIO.init();
  NETSTACK_RADIO.on();
  NETSTACK_MAC.init();
  NETSTACK_RDC.on();
#endif
#if 0
  printf("%s %s, channel check rate %u Hz, radio channel %u\n",
         NETSTACK_MAC.name, 
         NETSTACK_RDC.name,
         CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
                         NETSTACK_RDC.channel_check_interval()),
         RF_CHANNEL);
#endif
  //memcpy(&uip_lladdr.addr, ds2411_id, sizeof(uip_lladdr.addr));

#if defined WITH_UIP6

  printf("\nAddresses [%u max]\n", UIP_DS6_ADDR_NB);
  
  for (i=0; i<UIP_DS6_ADDR_NB; i++) {
    if (uip_ds6_if.addr_list[i].isused) {
      uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
      printf("\n");
    }
  }
#endif

  /* Temporarily, we use a part of the STM32's UID as address. 
     It seems like uid_0[1] is usable in our batch. Note that
     this does not guarrantee unique addresses.
   */
  rimeaddr.u8[0] = (uint8_t)(uid16 >> 8) & 0xff;
  rimeaddr.u8[1] = (uint8_t)(uid16 & 0xff);
  printf("Rime address is: %02x.%02x\n", 
         rimeaddr.u8[0], rimeaddr.u8[1]);

#if NETSTACK_CONF_RADIO == rf230_driver
  rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&rimeaddr.u8);
  rf230_set_channel(CHANNEL_802_15_4);
  rimeaddr_set_node_addr(&rimeaddr);
#endif
  process_start(&tcpip_process, NULL);

#if defined WITH_UIP6

  printf("Tentative link-local IPv6 address ");
  {
    uip_ds6_addr_t *lladdr;
    int i;
    lladdr = uip_ds6_get_link_local(-1);
    for(i = 0; i < 7; ++i) {
      printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
             lladdr->ipaddr.u8[i * 2 + 1]);
    }
    printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
  }
  printf("\n");
#endif

  //em_init();

  leds_on(LEDS_ALL);

  print_local_addresses();

#ifdef WITH_UIP
  printf("Starting tcpip and fw\n");
  process_start(&tcpip_process, NULL);
  process_start(&uip_fw_process, NULL);
#endif
#ifdef WITH_USB
  //(void)setup_usb();
  process_start(&usbeth_process, NULL);
#endif
  //process_start(&lsm303_process, NULL);
  //process_start(&eriks_process, NULL);
  printf("Processes running\n");
}
Exemple #9
0
static void
receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
  int i,num_neigh;
  uint8_t *ptr;
  struct ip_list_struct *s;
  uip_ipaddr_t *addr;
  uip_ds6_addr_t * nh;

  // Check if this is the first time I get a message from this node
  if( !list_exist(sender_addr) ){
     // First message from this neighbour
     s = memb_alloc(&ip_mem);
     uip_ipaddr_copy(&s->ip, sender_addr);
     list_add(ip_list, s);
     printf("Neighbour added \n"); 
  }

  printf("Neighbour information received from ");
  uip_debug_ipaddr_print(sender_addr);
  printf("\n");
 
  num_neigh = datalen/sizeof(uip_ipaddr_t);
  ptr=data;
  printf("2nd hop neighbour list is: \n");
  for(i=0; i < num_neigh; i++){
     ptr += i*sizeof(uip_ipaddr_t);
     addr = ptr;
     uip_debug_ipaddr_print(addr);

     // Add the 2nd neighbors in the routing table

     if(uip_ds6_is_my_addr(addr))
        continue; // Hey this is myself

     if( list_exist(addr) )
	continue; // Hey you're a 1st hop neighbour

     // I can add the entry in the routing table
     uip_ds6_route_add(addr, 128, sender_addr);
     printf("\nAdded route\n");

     // Verify!
     nh = uip_ds6_route_lookup(addr);
     if( nh != NULL ){
        printf("TO=");
        uip_debug_ipaddr_print(addr);
        printf(" NEXTHOP=");
        uip_debug_ipaddr_print(nh); 	
     }
  }
  printf("\n");



}
/*---------------------------------------------------------------------------*/
void
uip_ds6_route_rm(uip_ds6_route_t *route)
{
  struct uip_ds6_route_neighbor_route *neighbor_route;
#if DEBUG != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* DEBUG != DEBUG_NONE */
  if(route != NULL && route->neighbor_routes != NULL) {

    PRINTF("uip_ds6_route_rm: removing route: ");
    PRINT6ADDR(&route->ipaddr);
    PRINTF("\n");

    /* Remove the route from the route list */
    list_remove(routelist, route);

    /* Find the corresponding neighbor_route and remove it. */
    for(neighbor_route = list_head(route->neighbor_routes->route_list);
        neighbor_route != NULL && neighbor_route->route != route;
        neighbor_route = list_item_next(neighbor_route));

    if(neighbor_route == NULL) {
      PRINTF("uip_ds6_route_rm: neighbor_route was NULL for ");
      uip_debug_ipaddr_print(&route->ipaddr);
      PRINTF("\n");
    }
    list_remove(route->neighbor_routes->route_list, neighbor_route);
    if(list_head(route->neighbor_routes->route_list) == NULL) {
      /* If this was the only route using this neighbor, remove the
         neighbor from the table - this implicitly unlocks nexthop */
#if (DEBUG) & DEBUG_ANNOTATE
      uip_ipaddr_t *nexthop = uip_ds6_route_nexthop(route);
      if(nexthop != NULL) {
        ANNOTATE("#L %u 0\n", nexthop->u8[sizeof(uip_ipaddr_t) - 1]);
      }
#endif /* (DEBUG) & DEBUG_ANNOTATE */
      PRINTF("uip_ds6_route_rm: removing neighbor too\n");
      nbr_table_remove(nbr_routes, route->neighbor_routes->route_list);
#ifdef NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK
      NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK(
          (const linkaddr_t *)nbr_table_get_lladdr(nbr_routes, route->neighbor_routes->route_list));
#endif
    }
    memb_free(&routememb, route);
    memb_free(&neighborroutememb, neighbor_route);

    num_routes--;

    PRINTF("uip_ds6_route_rm num %d\n", num_routes);

#if UIP_DS6_NOTIFICATIONS
    call_route_callback(UIP_DS6_NOTIFICATION_ROUTE_RM,
        &route->ipaddr, uip_ds6_route_nexthop(route));
#endif

#ifdef NETSTACK_CONF_ROUTE_REMOVED_CALLBACK
NETSTACK_CONF_ROUTE_REMOVED_CALLBACK(&route->ipaddr);
#endif /* NETSTACK_CONF_ROUTE_REMOVED_CALLBACK*/
  }

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

  return;
}
/*---------------------------------------------------------------------------*/
int
ip64_6to4(const uint8_t *ipv6packet, const uint16_t ipv6packet_len,
	  uint8_t *resultpacket)
{
  struct ipv4_hdr *v4hdr;
  struct ipv6_hdr *v6hdr;
  struct udp_hdr *udphdr;
  struct tcp_hdr *tcphdr;
  struct icmpv4_hdr *icmpv4hdr;
  struct icmpv6_hdr *icmpv6hdr;
  uint16_t ipv6len, ipv4len;
  struct ip64_addrmap_entry *m;
  
  v6hdr = (struct ipv6_hdr *)ipv6packet;
  v4hdr = (struct ipv4_hdr *)resultpacket;

  if((v6hdr->len[0] << 8) + v6hdr->len[1] <= ipv6packet_len) {
    ipv6len = (v6hdr->len[0] << 8) + v6hdr->len[1] + IPV6_HDRLEN;
  } else {
    PRINTF("ip64_6to4: packet smaller than reported in IPv6 header, dropping\n");
    return 0;
  }

  /* We copy the data from the IPv6 packet into the IPv4 packet. We do
     not modify the data in any way. */
  memcpy(&resultpacket[IPV4_HDRLEN],
	 &ipv6packet[IPV6_HDRLEN],
	 ipv6len - IPV6_HDRLEN);

  udphdr = (struct udp_hdr *)&resultpacket[IPV4_HDRLEN];
  tcphdr = (struct tcp_hdr *)&resultpacket[IPV4_HDRLEN];
  icmpv4hdr = (struct icmpv4_hdr *)&resultpacket[IPV4_HDRLEN];
  icmpv6hdr = (struct icmpv6_hdr *)&ipv6packet[IPV6_HDRLEN];

  /* Translate the IPv6 header into an IPv4 header. */

  /* First the basics: the IPv4 version, header length, type of
     service, and offset fields. Those are the same for all IPv4
     packets we send, regardless of the values found in the IPv6
     packet. */
  v4hdr->vhl = 0x45;
  v4hdr->tos = 0;
  v4hdr->ipoffset[0] = v4hdr->ipoffset[1] = 0;

  /* We assume that the IPv6 packet has a fixed size header with no
     extension headers, and compute the length of the IPv4 packet and
     place the resulting value in the IPv4 packet header. */
  ipv4len = ipv6len - IPV6_HDRLEN + IPV4_HDRLEN;
  v4hdr->len[0] = ipv4len >> 8;
  v4hdr->len[1] = ipv4len & 0xff;

  /* For simplicity, we set a unique IP id for each outgoing IPv4
     packet. */
  ipid++;
  v4hdr->ipid[0] = ipid >> 8;
  v4hdr->ipid[1] = ipid & 0xff;

  /* Set the IPv4 protocol. We only support TCP, UDP, and ICMP at this
     point. While the IPv4 header protocol numbers are the same as the
     IPv6 next header numbers, the ICMPv4 and ICMPv6 numbers are
     different so we cannot simply copy the contents of the IPv6 next
     header field. */
  switch(v6hdr->nxthdr) {
  case IP_PROTO_TCP:
    PRINTF("ip64_6to4: TCP header\n");
    v4hdr->proto = IP_PROTO_TCP;
    break;

  case IP_PROTO_UDP:
    PRINTF("ip64_6to4: UDP header\n");
    v4hdr->proto = IP_PROTO_UDP;
    break;

  case IP_PROTO_ICMPV6:
    PRINTF("ip64_6to4: ICMPv6 header\n");
    v4hdr->proto = IP_PROTO_ICMPV4;
    /* Translate only ECHO_REPLY messages. */
    if(icmpv6hdr->type == ICMP6_ECHO_REPLY) {
      icmpv4hdr->type = ICMP_ECHO_REPLY;
    } else {
      PRINTF("ip64_6to4: ICMPv6 mapping for type %d not implemented.\n",
	     icmpv6hdr->type);
      return 0;
    }
    break;

  default:
    /* We did not recognize the next header, and we do not attempt to
       translate something we do not understand, so we return 0 to
       indicate that no successful translation could be made. */
    PRINTF("ip64_6to4: Could not convert IPv6 next hop %d to an IPv4 protocol number.\n",
	   v6hdr->nxthdr);
    return 0;
  }

  /* We set the IPv4 ttl value to the hoplim number from the IPv6
     header. This means that information about the IPv6 topology is
     transported into to the IPv4 network. */
  v4hdr->ttl = v6hdr->hoplim;

  /* We next convert the destination address. We make this conversion
     with the ip64_addr_6to4() function. If the conversion
     fails, ip64_addr_6to4() returns 0. If so, we also return 0 to
     indicate failure. */
  if(ip64_addr_6to4(&v6hdr->destipaddr,
		    &v4hdr->destipaddr) == 0) {
    PRINTF("ip64_6to4: Could not convert IPv6 destination address.\n");
    uip_debug_ipaddr_print(&v6hdr->destipaddr);
    PRINTF("\n");
    return 0;
  }

  /* We set the source address in the IPv4 packet to be the IPv4
     address that we have been configured with through the
     ip64_set_ipv4_address() function. Only let broadcasts through. */
  if(!ip64_hostaddr_configured &&
     !uip_ip4addr_cmp(&v4hdr->destipaddr, &ipv4_broadcast_addr)) {
    PRINTF("ip64_6to4: no IPv4 address configured.\n");
    return 0;
  }
  ip64_addr_copy4(&v4hdr->srcipaddr, &ip64_hostaddr);


  /* Next we update the transport layer header. This must be updated
     in two ways: the source port number is changed and the transport
     layer checksum must be recomputed. The reason why we change the
     source port number is so that we can remember what IPv6 address
     this packet came from, in case the packet will result in a reply
     from the host on the IPv4 network. If a reply would be sent, it
     would be sent to the port number that we chose, and we will be
     able to map this back to the IPv6 address of the original sender
     of the packet.
  */

  /* We check to see if we already have an existing IP address mapping
     for this connection. If not, we create a new one. */
  if((v4hdr->proto == IP_PROTO_UDP || v4hdr->proto == IP_PROTO_TCP)) {

    if(ip64_special_ports_outgoing_is_special(uip_ntohs(udphdr->srcport))) {
      uint16_t newport;
      if(ip64_special_ports_translate_outgoing(uip_ntohs(udphdr->srcport),
					       &v6hdr->srcipaddr,
					       &newport)) {
	udphdr->srcport = uip_htons(newport);
      }
    } else if(uip_ntohs(udphdr->srcport) >= EPHEMERAL_PORTRANGE) {
      m = ip64_addrmap_lookup(&v6hdr->srcipaddr,
                              uip_ntohs(udphdr->srcport),
			      &v4hdr->destipaddr,
			      uip_ntohs(udphdr->destport),
			      v4hdr->proto);
      if(m == NULL) {
	PRINTF("Lookup failed\n");
	m = ip64_addrmap_create(&v6hdr->srcipaddr,
				uip_ntohs(udphdr->srcport),
				&v4hdr->destipaddr,
				uip_ntohs(udphdr->destport),
				v4hdr->proto);
	if(m == NULL) {
	  PRINTF("Could not create new map\n");
	  return 0;
	} else {
	  PRINTF("Could create new local port %d\n", m->mapped_port);
	}
      } else {
	PRINTF("Lookup: found local port %d (%d)\n", m->mapped_port,
	       uip_htons(m->mapped_port));
      }
      udphdr->srcport = uip_htons(m->mapped_port);
    }
  }

  /* The IPv4 header is now complete, so we can compute the IPv4
     header checksum. */
  v4hdr->ipchksum = 0;
  v4hdr->ipchksum = ~(ipv4_checksum(v4hdr));



  /* The checksum is in different places in the different protocol
     headers, so we need to be sure that we update the correct
     field. */
  switch(v4hdr->proto) {
  case IP_PROTO_TCP:
    tcphdr->tcpchksum = 0;
    tcphdr->tcpchksum = ~(ipv4_transport_checksum(resultpacket, ipv4len,
						  IP_PROTO_TCP));
    break;
  case IP_PROTO_UDP:
    udphdr->udpchksum = 0;
    udphdr->udpchksum = ~(ipv4_transport_checksum(resultpacket, ipv4len,
						  IP_PROTO_UDP));
    if(udphdr->udpchksum == 0) {
      udphdr->udpchksum = 0xffff;
    }
    break;
  case IP_PROTO_ICMPV4:
    icmpv4hdr->icmpchksum = 0;
    icmpv4hdr->icmpchksum = ~(ipv4_transport_checksum(resultpacket, ipv4len,
						      IP_PROTO_ICMPV4));
    break;

  default:
    PRINTF("ip64_6to4: transport protocol %d not implemented\n", v4hdr->proto);
    return 0;
  }

  /* Finally, we return the length of the resulting IPv4 packet. */
  PRINTF("ip64_6to4: ipv4len %d\n", ipv4len);
  return ipv4len;
}
Exemple #12
0
void readCommandLine() {
	// Check the stdin for data
	app_res = read(0, app_buffer + app_bytes_recv, BUFLEN);
	if (app_res > 0) {
		// Data received from stdin, add to buffer
		app_bytes_recv += app_res;
		if (app_buffer[app_bytes_recv - 1] == '\n') {
			// Command ended with linefeed, terminate string and inform user
		  if(memcmp(app_buffer,"reset",5)==0) {
		    ule6loTestIn_reset();
		  }
		  if(memcmp(app_buffer,"rm",2)==0) {
		      ule6lo_ip6addr_t loc_fipaddr2;
		      memset(&loc_fipaddr2, 0x00, sizeof(loc_fipaddr2));
		      loc_fipaddr2.u8[0] = 0xff;
		      loc_fipaddr2.u8[1] = 0x02;
		      loc_fipaddr2.u8[10] = 0x00;
		      loc_fipaddr2.u8[11] = 0x00;
		      loc_fipaddr2.u8[12] = 0x00;
		      loc_fipaddr2.u8[13] = 0x01;
		      loc_fipaddr2.u8[15] = 0x05;
		      
		    ule6loGI_removeMulticastAddr(&loc_fipaddr2);
		  }
		  else if(memcmp(app_buffer,"query",5)==0)
		  {
		    uip_ip6addr_t loc_fipaddr2;
        memset(&loc_fipaddr2, 0x00, sizeof(loc_fipaddr2));
       /* loc_fipaddr2.u8[0] = 0xff;
        loc_fipaddr2.u8[1] = 0x02;
        loc_fipaddr2.u8[10] = 0x00;
        loc_fipaddr2.u8[11] = 0x01;
        loc_fipaddr2.u8[12] = 0xFF;
        loc_fipaddr2.u8[13] = 0x00;
        loc_fipaddr2.u8[15] = 0x02;*/
        //unspecified..
#ifdef UIP_CONF_MLD
		    uip_icmp6_mldv1_query(&loc_fipaddr2);
#endif
		  }
		  else if(memcmp(app_buffer,"resolv",6)==0) {
		    uip_ipaddr_t* resolvedto;
		    resolv_status_t res;

		    res = resolv_lookup("test.jjo",&resolvedto);
		    if(res == RESOLV_STATUS_CACHED) {
		      printf("Resolved to ");
		      uip_debug_ipaddr_print(resolvedto);
		      printf("\n");
		    }
		    else if ( res != RESOLV_STATUS_RESOLVING ){
		      printf("Unable to resolve, result = %i. Executing query for it\n",res);
		      resolv_query("test.jjo");
		    }
		    else{
		      printf("Unable to resolve, query in progress\n");
		    }
		  }
		  else {
		    app_buffer[app_bytes_recv] = 0;
		    printf("You typed: %s\n", app_buffer);
		    //Send message to server and reset message length
		    send_udp(app_buffer);
		    //ule6loLLI_send(app_buffer, app_bytes_recv);
		  }
		  app_bytes_recv = 0;
		}
	}
}
/*simple-udp-rpl---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t *addr;


  PROCESS_BEGIN();

  //servreg_hack_init();

  // set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
      etimer_reset(&periodic_timer);
      etimer_set(&send_timer, SEND_TIME);


      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));

#ifdef WITH_STABLETIMER    
    if(stable_flag){
#else //WITH_STABLETIMER
    if(tsch_is_associated){
#endif //WITH_STABLETIMER
      /*--- target address decision ---*/
      /*-- to registered target with servreg_hack --*/
      //addr = servreg_hack_lookup(SERVICE_ID);
      /*-- to default route --*/
      //uip_ds6_defrt_t *default_route;
      //default_route = uip_ds6_defrt_lookup(uip_ds6_defrt_choose());
      //if(default_route != NULL) addr = &default_route->ipaddr;
      //else addr = NULL;
      /*-- decide by address directory--*/
      uip_ipaddr_t temp_ipaddr;
      uip_ip6addr(&temp_ipaddr,0xfd00,0,0,0,0xc30c,0,0,1);
      addr = &temp_ipaddr;
      /*-- linklocal rplnodes mcast --*/
      //uip_ipaddr_t temp_ipaddr;
      //uip_ip6addr(&temp_ipaddr, 0xff02,0,0,0,0,0,0,0x001a);
      //addr = &temp_ipaddr;
      /*-- to default parent --*/
      //addr = rpl_get_parent_ipaddr(default_instance->current_dag->preferred_parent); 

      /*--- sending ---*/ 
      if(addr != NULL) {
        static unsigned int message_number;
        char buf[20];

#ifdef WITH_LEAPFROG
        sprintf(buf, "%c%cHello Tada %04d", LEAPFROG_DATA_HEADER, leapfrog_data_counter + LEAPFROG_BEACON_OFFSET, message_number);
        leapfrog_data_counter++;
        if(leapfrog_data_counter > LEAPFROG_DATA_COUNTER_MAX) leapfrog_data_counter = 0;
#else
        sprintf(buf, "Hello TadaMatz %d", message_number);
#endif
        printf("DATA: Sending unicast to ");
        uip_debug_ipaddr_print(addr);
        printf(" '");
        printf(buf);
        printf("'\n");
        message_number++;
        simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
      } else {
        printf("DATA: addr is NULL!!");
      }
    } //if(tsch_is_associated)
  }

  PROCESS_END();
}
/* ----------------- simple-udp-rpl process end ----------------- */
/* ----------------- simple-udp-rpl process end ----------------- */

#ifdef WITH_LEAPFROG
/* ----------------- leapfrog process start----------------- */
PROCESS_THREAD(leapfrog_beaconing_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t *addr;

  PROCESS_BEGIN();

  simple_udp_register(&leapfrog_unicast_connection, LEAPFROG_UDP_PORT,
                      NULL, LEAPFROG_UDP_PORT, receiver);

  etimer_set(&periodic_timer, LEAPFROG_SEND_INTERVAL);
  while(1) {
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
      etimer_reset(&periodic_timer);
      etimer_set(&send_timer, LEAPFROG_SEND_TIME);

      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
     
    if(tsch_is_associated){
      /*--- target address decision ---*/
      /*-- linklocal rplnodes mcast --*/
      uip_ipaddr_t temp_ipaddr;
      uip_ip6addr(&temp_ipaddr, 0xff02,0,0,0,0,0,0,0x001a);
      addr = &temp_ipaddr;

      /*--- sending ---*/ 
      if(addr != NULL) {
        static unsigned int message_number;
        char buf[20];
        char possible_parent_str[1 + LEAPFROG_NUM_NEIGHBOR_NODE];

        possible_parent_str[0] = leapfrog_possible_parent_num + LEAPFROG_BEACON_OFFSET;
        int i;
        for(i = 0; i < leapfrog_possible_parent_num; i++){
          possible_parent_str[1 + i] = leapfrog_possible_parent_id_array[i] + LEAPFROG_BEACON_OFFSET;
        }

        sprintf(buf, "%cP%cG%cA%cC%sN%d", 
 	  LEAPFROG_BEACON_HEADER, 
	  leapfrog_parent_id + LEAPFROG_BEACON_OFFSET, 
	  leapfrog_grand_parent_id + LEAPFROG_BEACON_OFFSET,
          leapfrog_alt_parent_id + LEAPFROG_BEACON_OFFSET,
          possible_parent_str, //C for candidate
	  message_number);
        printf("LEAPFROG: Sending beacon to ");
        uip_debug_ipaddr_print(addr);
        printf(" '");
        printf(buf);
        printf("'\n");
        message_number++;
        simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
        //simple_udp_sendto(&unicast_connection, buf, cnt, addr);
      } else {
        printf("LEAPFROG: addr is null!!");
      }
    } //if(tsch_is_associated)
  }

  PROCESS_END();
}
/* ----------------- leapfrog process end ----------------- */
#endif /*WITH_LEAPFROG*/

#ifdef WITH_STABLETIMER
/* ----------------- stable_timer process start ----------------- */
PROCESS_THREAD(stable_timer_process, ev, data)
{
  static struct etimer stable_timer;

  PROCESS_BEGIN();
  etimer_set(&stable_timer, CLOCK_SECOND * 60 * 15); //15min
  printf("Set Stable timer\n");
  
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&stable_timer));
  etimer_stop(&stable_timer);
   
  stable_flag = 1;
  printf("Stable timer expired!! Start to send application traffic\n");
  PROCESS_EXIT();

  PROCESS_END();
}
/*simple-udp-rpl---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
// #ifdef WITH_LEAPFROG //for packet elimination
//   int leapfrog_elimination_flag = 0;
    
//   if(data[0] == LEAPFROG_DATA_HEADER){
//     char tmp_lf_pc = data[1] - LEAPFROG_BEACON_OFFSET;
//     int tmp_sid = sender_addr->u8[15];
//     char tmp_lf_an = leapfrog_elimination_id_array[tmp_sid];

//     if(tmp_lf_an <= LEAPFROG_DATA_COUNTER_WIDTH){
//       if(tmp_lf_pc <= tmp_lf_an || LEAPFROG_DATA_COUNTER_MAX - (LEAPFROG_DATA_COUNTER_WIDTH - tmp_lf_an ) <= tmp_lf_pc) leapfrog_elimination_flag = 1; 
//     }else{
//       if(tmp_lf_an - LEAPFROG_DATA_COUNTER_WIDTH <= tmp_lf_pc && tmp_lf_pc <= tmp_lf_an) leapfrog_elimination_flag = 1;
//     }

//     if(leapfrog_elimination_flag == 1){
//       PRINTF("LEAPFROG: Elimination discard data\n");
//     }else{
//       PRINTF("LEAPFROG: ");
//       leapfrog_elimination_id_array[tmp_sid] = tmp_lf_pc;
//     }
//   }

//   if(leapfrog_elimination_flag != 1){
// #endif /*WITH_LEAPFROG*/

  printf("DATA: received from ");
  uip_debug_ipaddr_print(sender_addr);
  printf(" on port %d from port %d with length %d: '%s'\n",
         receiver_port, sender_port, datalen, data);

#ifdef WITH_LEAPFROG //for beaconing
  if(data[0] == LEAPFROG_BEACON_HEADER){
    char temp_sid = 0; //sender id of packet
    char temp_pid = 0; //sender's parent id
    char temp_gid = 0; //sender's grand parent id
    char temp_aid = 0; //sender's alt parent id
    temp_sid = sender_addr->u8[15]; //get most least byte. must be modified to store whole address
    temp_pid = data[2] - LEAPFROG_BEACON_OFFSET;
    temp_gid = data[4] - LEAPFROG_BEACON_OFFSET;
    temp_aid = data[6] - LEAPFROG_BEACON_OFFSET;
    char temp_pps_num;
    char temp_pps_str[LEAPFROG_NUM_NEIGHBOR_NODE];
    int temp_pps_itr;
    temp_pps_num = data[8] - LEAPFROG_BEACON_OFFSET;
    for(temp_pps_itr = 0; temp_pps_itr < (int)temp_pps_num; temp_pps_itr++){ //do nothing if temp_pps_num = 0
      temp_pps_str[temp_pps_itr] = data[8 + 1 + temp_pps_itr];
    }
    temp_pps_str[temp_pps_itr] = '\0';

    printf("LEAPFROG: receive beacon S %d P %d GP %d AP %d PPs #%d %s\n", temp_sid, temp_pid, temp_gid, temp_aid, temp_pps_num, temp_pps_str);
    
    //judge and registor parent, grandparent, alt parent 
    uip_ipaddr_t * addr;
    #ifdef WITH_LEAPFROG_TSCH
    char my_id = 0;
    addr = &uip_ds6_if.addr_list[2].ipaddr; //get own ID. [2] seems to be default
    if(addr != NULL){
      my_id = addr->u8[15];
    }
    #endif //WITH_LEAPFROG_TSCH
    addr = rpl_get_parent_ipaddr(default_instance->current_dag->preferred_parent);
    if(addr != NULL){
      char my_pid = addr->u8[15];
//      if(leapfrog_parent_id == 0){ //registor parent
//        leapfrog_parent_id = my_pid;
//      }else 
      //new parent and reset P, GP, AP
      if(leapfrog_parent_id != my_pid){ //new parent and reset P, GP, AP
        leapfrog_parent_id = my_pid;
        leapfrog_grand_parent_id = 0;
        leapfrog_alt_parent_id = 0;
        printf("LEAPFROG: reset P GP AP\n");
      }
      //judge Grand Parent
      if(leapfrog_parent_id > 0 && leapfrog_parent_id == my_pid){ //judge Grand Parent
        if(temp_sid == my_pid){
          if(temp_pid > 0 && temp_pid != my_pid){
            leapfrog_grand_parent_id = temp_pid; //get grand parent
          }
        }
      }
      //judge Alternate Parent
      if(leapfrog_grand_parent_id > 0 && temp_pid > 0 && leapfrog_grand_parent_id == temp_pid && leapfrog_parent_id != temp_sid){ //judge Alt Parent
        if(leapfrog_alt_parent_id != temp_sid){
          leapfrog_alt_parent_id = temp_sid; //get alt parent
#ifdef WITH_LEAPFROG_TSCH //add unicast tx link to AP based on own(child) ID
          linkaddr_copy(&alt_parent_linkaddr, packetbuf_addr(PACKETBUF_ADDR_SENDER));
          //alt_parent_linkaddr.u8[7] = leapfrog_alt_parent_id; //for tsch
          //printf("LEAPFROG-TSCH: update AP %d %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 
          //  leapfrog_alt_parent_id,
          //  alt_parent_linkaddr.u8[0],
          //  alt_parent_linkaddr.u8[1],
          //  alt_parent_linkaddr.u8[2],
          //  alt_parent_linkaddr.u8[3],
          //  alt_parent_linkaddr.u8[4],
          //  alt_parent_linkaddr.u8[5],
          //  alt_parent_linkaddr.u8[6],
          //  alt_parent_linkaddr.u8[7]);

          printf("LEAPFROG-TSCH: update alt tx normally -> AP %d\n", leapfrog_alt_parent_id);
          
          orchestra_leapfrog_add_uc_tx_link(leapfrog_alt_parent_id);
#endif /*WITH_LEAPFROG_TSCH*/
        }
      }else{ //judge Alternate Parent by Possible Parent
        if(my_pid != temp_sid){
          for(temp_pps_itr = 0; temp_pps_itr < (int)temp_pps_num; temp_pps_itr++){ //do nothing if temp_pps_num = 0
            if(leapfrog_grand_parent_id == data[8 + 1 + temp_pps_itr] - LEAPFROG_BEACON_OFFSET){
              leapfrog_alt_parent_id = temp_sid;
#ifdef WITH_LEAPFROG_TSCH
              linkaddr_copy(&alt_parent_linkaddr, packetbuf_addr(PACKETBUF_ADDR_SENDER));
              printf("LEAPFROG-TSCH: update alt tx by PP -> AP %d\n", leapfrog_alt_parent_id);
          
              orchestra_leapfrog_add_uc_tx_link(leapfrog_alt_parent_id);
#endif //WITH_LEAPFROG_TSCH
              break;
            }
          }
        }
      }

      for(temp_pps_itr = 0; temp_pps_itr < leapfrog_possible_parent_num; temp_pps_itr++){
        temp_pps_str[temp_pps_itr] = leapfrog_possible_parent_id_array[temp_pps_itr] + LEAPFROG_BEACON_OFFSET;
      }
      temp_pps_str[temp_pps_itr] = '\0';
      printf("LEAPFROG: own P %d GP %d AP %d PPs #%d %s\n", leapfrog_parent_id, leapfrog_grand_parent_id, leapfrog_alt_parent_id, leapfrog_possible_parent_num, temp_pps_str);

      //judge I am sender's Alt Parent and prepare Rx link for alt child
#ifdef WITH_LEAPFROG_TSCH //judge I am sender's Alt Parent
      if(temp_aid != 0 && my_id == temp_aid){
        printf("LEAPFROG-TSCH: update rx s <- (alt)C %d\n", temp_sid);

        orchestra_leapfrog_add_uc_rx_link(temp_sid);
      }
#endif /*WITH_LEAPFROG_TSCH*/      
    }
  }

  // }else{
  //   //receiving processes is skipped because of elimination
  // }
#endif /*WITH_LEAPFROG*/
}
Exemple #15
0
/*------Done in a subroutine to keep main routine stack usage small--------*/
void initialize(void)
{
#if WITH_SLIP
  //Slip border router on uart0
  rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
#else
  /* First rs232 port for debugging */
  rs232_init(RS232_PORT_0, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);

  /* Redirect stdout to first port */
  rs232_redirect_stdout(RS232_PORT_0);

  /* Get input from first port */
  rs232_set_input(RS232_PORT_0, serial_line_input_byte);
#endif

  clock_init();

  if(MCUSR & (1<<PORF )) PRINTA("Power-on reset.\n");
  if(MCUSR & (1<<EXTRF)) PRINTA("External reset!\n");
  if(MCUSR & (1<<BORF )) PRINTA("Brownout reset!\n");
  if(MCUSR & (1<<WDRF )) PRINTA("Watchdog reset!\n");
  if(MCUSR & (1<<JTRF )) PRINTA("JTAG reset!\n");

#if ANNOUNCE_BOOT
  PRINTA("\n*******Booting %s*******\n",CONTIKI_VERSION_STRING);
#endif

  /* Initialize process subsystem */
  process_init();

  /* etimers must be started before ctimer_init */
  process_start(&etimer_process, NULL);

  /* Initilaize serial line for input */
  serial_line_init();

  /* Initialize board LEDs */
  leds_init();

  /* When used for testing, we should start with them off */
  leds_off(LEDS_GREEN|LEDS_YELLOW);

  ctimer_init();
  /* Start radio and radio receive process */
  NETSTACK_RADIO.init();

  /* Set addresses BEFORE starting tcpip process */

  rimeaddr_t addr;
  memset(&addr, 0, sizeof(rimeaddr_t));
  get_mac_from_eeprom(addr.u8);

  memcpy(&uip_lladdr.addr, &addr.u8, 8);

// Is this required with IPv6, I wonder?
#if 1
  rf230_set_pan_addr(
    IEEE802154_PANID,
    0,
    (uint8_t *)&addr.u8
  );
  rf230_set_channel(26);

  rimeaddr_set_node_addr(&addr);
#endif

  PRINTF("MAC address %x:%x:%x:%x:%x:%x:%x:%x\n",addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7]);

  /* Initialize stack protocols */
  queuebuf_init();
  NETSTACK_RDC.init();
  NETSTACK_MAC.init();
  NETSTACK_NETWORK.init();

#if ANNOUNCE_BOOT
  PRINTA("%s %s, channel %u",NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel());
  if (NETSTACK_RDC.channel_check_interval)  //function pointer is zero for sicslowmac
  {
    unsigned short tmp;
    tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\
                        NETSTACK_RDC.channel_check_interval());
    if (tmp<65535) PRINTA(", check rate %u Hz",tmp);
  }
  PRINTA("\n");
#endif

#if UIP_CONF_ROUTER
//#warning Zigduino has not been tested with UIP_CONF_ROUTER
#if ANNOUNCE_BOOT
  PRINTA("Routing Enabled\n");
#endif
#endif

  /* Sensors process means all processes will get an event posted whenever sensors change.
     Not always desired, so may want to put this on a compile switch. */
  process_start(&sensors_process, NULL);
  SENSORS_ACTIVATE(button_sensor);

  process_start(&tcpip_process, NULL);

  //Give ourselves a prefix
  // init_net();

  /* Add easy addresses for testing */
  uip_ip6addr_t ipaddr;
  uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, addr.u8[7]);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
#if UIP_CONF_ROUTER
  uip_ds6_prefix_add(&ipaddr,64,0,0,0,0);
#else
  uip_ds6_prefix_add(&ipaddr,64,0);
#endif

  /* Create a route through the border router for site-local addresses, fec0::/64. */
  uip_ds6_route_t *rep;
  uip_ip6addr_t next_hop;
  uip_ip6addr(&ipaddr, 0xfec0, 0, 0, 0, 0, 0, 0, 0);
  uip_ip6addr(&next_hop, 0xaaaa, 0, 0, 0, 0, 0, 0, 1);
  if((rep = uip_ds6_route_add(&ipaddr, 64, &next_hop, 0)) == NULL)
  {
    printf("*** Failed to add a route to fec0::/64\n");
  }
 
  printf("Autostart other processes\n");
  /* Autostart other processes */
  autostart_start(autostart_processes);

  /*--------------------------Announce the configuration---------------------*/
#if ANNOUNCE_BOOT

  extern uip_ds6_netif_t uip_ds6_if;

  uint8_t i;
  PRINTA("\nIP addresses [%u max]\n",UIP_DS6_ADDR_NB);
  for (i=0; i<UIP_DS6_ADDR_NB; i++)
  {
    if (uip_ds6_if.addr_list[i].isused)
    {
      uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
      PRINTA("\n");
    }
  }
#endif /* ANNOUNCE_BOOT */
}
Exemple #16
0
/*---------------------------------Main Routine----------------------------*/
int
main(void)
{
  /* GCC depends on register r1 set to 0 (?) */
  asm volatile ("clr r1");
  
  /* Initialize in a subroutine to maximize stack space */
  initialize();

#if DEBUG
{struct process *p;
 for(p = PROCESS_LIST();p != NULL; p = ((struct process *)p->next)) {
  PRINTA("Process=%p Thread=%p  Name=\"%s\" \n",p,p->thread,PROCESS_NAME_STRING(p));
 }
}
#endif

  while(1) {
    process_run();

    watchdog_periodic();

/* Print rssi of all received packets, useful for range testing */
#ifdef RF230_MIN_RX_POWER
    uint8_t lastprint;
    if (rf230_last_rssi != lastprint) {        //can be set in halbb.c interrupt routine
        PRINTA("%u ",rf230_last_rssi);
        lastprint=rf230_last_rssi;
    }
#endif

#if 0
/* Clock.c can trigger a periodic PLL calibration in the RF230BB driver.
 * This can show when that happens.
 */
    extern uint8_t rf230_calibrated;
    if (rf230_calibrated) {
      PRINTA("\nRF230 calibrated!\n");
      rf230_calibrated=0;
    }
#endif

#if TESTRTIMER
/* Timeout can be increased up to 8 seconds maximum.
 * A one second cycle is convenient for triggering the various debug printouts.
 * The triggers are staggered to avoid printing everything at once.
 * My Jackdaw is 4% slow.
 */
    if (rtimerflag) {
      rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);
      rtimerflag=0;

#if STAMPS
if ((rtime%STAMPS)==0) {
  PRINTA("%us ",rtime);
  if (rtime%STAMPS*10) PRINTA("\n");
}
#endif
      rtime+=1;

#if PINGS && UIP_CONF_IPV6_RPL
extern void raven_ping6(void);
if ((rtime%PINGS)==1) {
  PRINTA("**Ping\n");
  raven_ping6();
}
#endif

#if ROUTES && UIP_CONF_IPV6_RPL
if ((rtime%ROUTES)==2) {

extern uip_ds6_netif_t uip_ds6_if;

  uint8_t i,j;
  uip_ds6_nbr_t *nbr;

  PRINTA("\nAddresses [%u max]\n",UIP_DS6_ADDR_NB);
  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
    if (uip_ds6_if.addr_list[i].isused) {
      uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
      PRINTA("\n");
    }
  }
  PRINTA("\nNeighbors [%u max]\n",NBR_TABLE_MAX_NEIGHBORS);

  for(nbr = nbr_table_head(ds6_neighbors);
      nbr != NULL;
      nbr = nbr_table_next(ds6_neighbors, nbr)) {
    uip_debug_ipaddr_print(&nbr->ipaddr);
    PRINTA("\n");
    j=0;
  }
  if (j) PRINTA("  <none>");
  PRINTA("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB);
  uip_ds6_route_t *r;
  for(r = uip_ds6_route_head();
      r != NULL;
      r = uip_ds6_route_next(r)) {
    if(r->isused) {
      uip_debug_ipaddr_print(&r->ipaddr);
      PRINTA("/%u (via ", r->length);
      uip_debug_ipaddr_print(uip_ds6_route_nexthop(r));
 //     if(r->state.lifetime < 600) {
        PRINTA(") %lus\n", r->state.lifetime);
 //     } else {
 //       PRINTA(")\n");
 //     }
      j=0;
    }
  }
  if (j) PRINTA("  <none>");
  PRINTA("\n---------\n");
}
#endif

#if STACKMONITOR && CONFIG_STACK_MONITOR
if ((rtime%STACKMONITOR)==3) {
  extern uint16_t __bss_end;
  uint16_t p=(uint16_t)&__bss_end;
  do {
    if (*(uint16_t *)p != 0x4242) {
      PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
      break;
    }
    p+=100;
  } while (p<RAMEND-10);
}
#endif

    }
#endif /* TESTRTIMER */

//Use with RF230BB DEBUGFLOW to show path through driver
#if RF230BB&&0
extern uint8_t debugflowsize,debugflow[];  //in rf230bb.c
  if (debugflowsize) {
    debugflow[debugflowsize]=0;
    PRINTA("%s",debugflow);
    debugflowsize=0;
   }
#endif

  }
  return 0;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;

  uip_ipaddr_t *addr;

  PROCESS_BEGIN();

  SENSORS_ACTIVATE(button_sensor);
 // SENSORS_ACTIVATE(light_sensor);

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);
  rx_start_duration = energest_type_time(ENERGEST_TYPE_LISTEN);
  //etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {

   // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
   // etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
    addr = servreg_hack_lookup(SERVICE_ID);

    if(addr != NULL) {
      static unsigned int button_number;
      char buf[50];

      if(button_number == 0) {
	button_number = 1;
	leds_toggle(LEDS_ALL);
      } else {
	button_number = 0;
	leds_toggle(LEDS_ALL);
      }

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf("\n");

      //sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, LUMINAIRE_ILLUMINANCE);
      sprintf(buf, "Occupancy Detector: %d, Luminous Intensity: %d", button_number, /*(int)(random() % 180 + 320 )*/ 1500 );      

      simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
	    //  printf("energy rx: %lu\n", energest_type_time(ENERGEST_TYPE_LISTEN) - rx_start_duration);
		//printf("energy tx: %lu\n", energest_type_time(ENERGEST_TYPE_TRANSMIT) - rx_start_duration);
		//printf("cpu: %lu\n", energest_type_time(ENERGEST_TYPE_CPU) - rx_start_duration);
		//printf("lpm: %lu\n", energest_type_time(ENERGEST_TYPE_LPM) - rx_start_duration);
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
	 const uint8_t *data,
         uint16_t datalen)
{
  static int cl_temp;
  static int cl_sec;
  unsigned int clock;
  unsigned int second;
  double temp;

  //printf("recieved msg at clock time: %d \n",clock_time()-  offset - prop_delay);
  //printf( "%d \n",clock_time()-  offset - prop_delay);
    

  TWAMPtimestamp ts_rcv;

  clock = clock_time();
  second = clock/CLOCK_SECOND;
  printf("RECEIVE AT: clock: %d || second: %d \n",clock,second);
  //temp = (double) (clock - start_time)/CLOCK_SECOND - second;
  temp = (double) (clock)/CLOCK_SECOND - second;
  ts_rcv.Second = second;
  ts_rcv.Fraction = temp * 1000;

  printf("Data received from ");  
  uip_debug_ipaddr_print(sender_addr);
  printf(" on port %d from port %d \n", receiver_port, sender_port);
  
  printf("Datalen = %d\n",datalen);
  SenderUAuthPacket sender_pkt;
  memset(&sender_pkt, 0, sizeof(sender_pkt));
  memcpy(&sender_pkt, data, datalen);
  
  ReflectorUAuthPacket reflect_pkt;
  //printf("Paket size = %d\n", sizeof(reflect_pkt));
  reflect_pkt.SeqNo = seqno;
  reflect_pkt.ErrorEstimate = 999;
  reflect_pkt.ReceiverTimestamp = ts_rcv;
  reflect_pkt.SenderSeqNo = sender_pkt.SeqNo;
  reflect_pkt.SenderTimestamp = sender_pkt.Timestamp;
  reflect_pkt.SenderErrorEstimate = sender_pkt.ErrorEstimate;
  reflect_pkt.SenderTTL = 255;


  clock = clock_time();
  second = clock/CLOCK_SECOND;
  temp = (double) (clock)/CLOCK_SECOND - second;
  reflect_pkt.Timestamp.Second = second;
  reflect_pkt.Timestamp.Fraction = temp * 1000;

  simple_udp_sendto(&unicast_connection, &reflect_pkt,
                    sizeof(reflect_pkt), sender_addr);

  printf("Packet reflected to:\n");
  uip_debug_ipaddr_print(sender_addr);
  printf("\n#################\n");

  //Prints for debugging
  printf("SeqNo: %"PRIu32"\n", sender_pkt.SeqNo);
  printf("Seconds: %"PRIu32"\n", sender_pkt.Timestamp.Second);
  printf("Micro: %"PRIu32"\n", sender_pkt.Timestamp.Fraction);
  printf("Error: %"PRIu16"\n", sender_pkt.ErrorEstimate);

  current++;
}
/*---------------------------------------------------------------------------*/
void
uip_ds6_route_rm(uip_ds6_route_t *route)
{
  struct uip_ds6_route_neighbor_route *neighbor_route;
#if DEBUG != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* DEBUG != DEBUG_NONE */
  if(route != NULL && route->neighbor_routes != NULL) {

    PRINTF("uip_ds6_route_rm: removing route: ");
    PRINT6ADDR(&route->ipaddr);
    PRINTF("\n");

    /* Remove the route from the route list */
    list_remove(routelist, route);

    /* Find the corresponding neighbor_route and remove it. */
    for(neighbor_route = list_head(route->neighbor_routes->route_list);
        neighbor_route != NULL && neighbor_route->route != route;
        neighbor_route = list_item_next(neighbor_route));

    if(neighbor_route == NULL) {
      PRINTF("uip_ds6_route_rm: neighbor_route was NULL for ");
      uip_debug_ipaddr_print(&route->ipaddr);
      PRINTF("\n");
    }
    list_remove(route->neighbor_routes->route_list, neighbor_route);
    if(list_head(route->neighbor_routes->route_list) == NULL) {
      /* If this was the only route using this neighbor, remove the
         neibhor from the table */
      PRINTF("uip_ds6_route_rm: removing neighbor too\n");
      nbr_table_remove(nbr_routes, route->neighbor_routes->route_list);
    }
    memb_free(&routememb, route);
    memb_free(&neighborroutememb, neighbor_route);

    num_routes--;

    PRINTF("uip_ds6_route_rm num %d\n", num_routes);

#if UIP_DS6_NOTIFICATIONS
    call_route_callback(UIP_DS6_NOTIFICATION_ROUTE_RM,
        &route->ipaddr, uip_ds6_route_nexthop(route));
#endif
#if 0 //(DEBUG & DEBUG_ANNOTATE) == DEBUG_ANNOTATE
    /* we need to check if this was the last route towards "nexthop" */
    /* if so - remove that link (annotation) */
    uip_ds6_route_t *r;
    for(r = uip_ds6_route_head();
        r != NULL;
        r = uip_ds6_route_next(r)) {
      uip_ipaddr_t *nextr, *nextroute;
      nextr = uip_ds6_route_nexthop(r);
      nextroute = uip_ds6_route_nexthop(route);
      if(nextr != NULL &&
         nextroute != NULL &&
         uip_ipaddr_cmp(nextr, nextroute)) {
        /* we found another link using the specific nexthop, so keep the #L */
        return;
      }
    }
    ANNOTATE("#L %u 0\n", uip_ds6_route_nexthop(route)->u8[sizeof(uip_ipaddr_t) - 1]);
#endif
  }

#if DEBUG != DEBUG_NONE
  assert_nbr_routes_list_sane();
#endif /* DEBUG != DEBUG_NONE */
  return;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t *addr;
  static int  sensor_value = 0;

  PROCESS_BEGIN();

  SENSORS_ACTIVATE(button_sensor);
  SENSORS_ACTIVATE(radio_sensor);
  
#ifdef X_NUCLEO_IKS01A1
  SENSORS_ACTIVATE(temperature_sensor);
  SENSORS_ACTIVATE(humidity_sensor);
  SENSORS_ACTIVATE(pressure_sensor);
  SENSORS_ACTIVATE(magneto_sensor);
  SENSORS_ACTIVATE(acceleration_sensor);
  SENSORS_ACTIVATE(gyroscope_sensor);
#endif /*X_NUCLEO_IKS01A1*/

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) 
  {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
    addr = servreg_hack_lookup(SERVICE_ID);
    if(addr != NULL) {
      static unsigned int message_number = 0;
      char buf[40];

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf("\n");
	  
#ifdef X_NUCLEO_IKS01A1
    switch(message_number) {
	case 0:
          sensor_value = temperature_sensor.value(0);
	  sprintf(buf, "Temperature:\t%d.%d C", sensor_value/10, ABS_VALUE(sensor_value)%10);
	  break;
	case 1:
	  sprintf(buf, "Humidity:\t%d.%d rH", humidity_sensor.value(0)/10, humidity_sensor.value(0)%10);
	  break;
	case 2:
	  sprintf(buf, "Pressure:\t%d.%d mbar", pressure_sensor.value(0)/10, pressure_sensor.value(0)%10);
	  break;
	case 3:
	  sprintf(buf, "Magneto:\t\t%d/%d/%d (X/Y/Z) mgauss", magneto_sensor.value(X_AXIS),
                                                              magneto_sensor.value(Y_AXIS),
                                                              magneto_sensor.value(Z_AXIS));
	  break;
	case 4:
	  sprintf(buf, "Acceleration:\t%d/%d/%d (X/Y/Z) mg", acceleration_sensor.value(X_AXIS),
                                                               acceleration_sensor.value(Y_AXIS),
                                                               acceleration_sensor.value(Z_AXIS));
	  break;
	case 5:
	  sprintf(buf, "Gyroscope:\t%d/%d/%d (X/Y/Z) mdps", gyroscope_sensor.value(X_AXIS),
                                                              gyroscope_sensor.value(Y_AXIS),
                                                              gyroscope_sensor.value(Z_AXIS));
	  break;
	case 6:
          sensor_value = radio_sensor.value(RADIO_SENSOR_LAST_PACKET);
          sprintf(buf, "Radio (RSSI):\t%d.%d dBm", sensor_value/10, ABS_VALUE(sensor_value)%10);
	  break;
	case 7:
          sprintf(buf, "Radio (LQI):\t%d", radio_sensor.value(RADIO_SENSOR_LAST_VALUE));
	  break;
	default:
	  break;
     }
     simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);

     message_number++;
     if(message_number > 7) {
       message_number = 0;
     }
#endif /*X_NUCLEO_IKS01A1*/
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}