diff --git a/examples/rpl-border-router/Makefile b/examples/rpl-border-router/Makefile index aaa8ab6cf..76c07cb8b 100644 --- a/examples/rpl-border-router/Makefile +++ b/examples/rpl-border-router/Makefile @@ -15,6 +15,7 @@ endif include $(SOURCES_DIR)/Makefile PROJECTDIRS += $(SOURCES_DIR) +PROJECT_SOURCEFILES += httpd-simple.c webserver.c border-router-common.c CFLAGS += -DPROJECT_CONF_PATH=\"$(SOURCES_DIR)/project-conf.h\" include $(CONTIKI)/Makefile.include diff --git a/examples/rpl-border-router/common/border-router-common.c b/examples/rpl-border-router/common/border-router-common.c new file mode 100644 index 000000000..ba3288777 --- /dev/null +++ b/examples/rpl-border-router/common/border-router-common.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2017, RISE SICS + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#include "contiki.h" +#include "border-router-common.h" +#include "rpl-dag-root.h" + +/* Log configuration */ +#include "sys/log.h" +#define LOG_MODULE "BR" +#define LOG_LEVEL LOG_LEVEL_INFO + +uint8_t prefix_set; + +/*---------------------------------------------------------------------------*/ +void +print_local_addresses(void) +{ + int i; + uint8_t state; + + LOG_INFO("Server IPv6 addresses:\n"); + 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)) { + LOG_INFO(" "); + LOG_INFO_6ADDR(&uip_ds6_if.addr_list[i].ipaddr); + LOG_INFO_("\n"); + } + } +} +/*---------------------------------------------------------------------------*/ +void +set_prefix_64(uip_ipaddr_t *prefix_64) +{ + prefix_set = 1; + rpl_dag_root_init(prefix_64, NULL); + rpl_dag_root_init_dag_immediately(); +} diff --git a/examples/rpl-border-router/common/border-router-common.h b/examples/rpl-border-router/common/border-router-common.h new file mode 100644 index 000000000..25d58aab0 --- /dev/null +++ b/examples/rpl-border-router/common/border-router-common.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017, RISE SICS + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#include "contiki.h" +#include "net/ipv6/uip.h" +#include "net/ipv6/uip-ds6.h" + +extern uint8_t prefix_set; + +void print_local_addresses(void); +void set_prefix_64(uip_ipaddr_t *prefix_64); diff --git a/examples/rpl-border-router/common/httpd-simple.c b/examples/rpl-border-router/common/httpd-simple.c index 5c6a545a3..dbe1ac80f 100644 --- a/examples/rpl-border-router/common/httpd-simple.c +++ b/examples/rpl-border-router/common/httpd-simple.c @@ -42,8 +42,6 @@ #include "contiki-net.h" -//#include "urlconv.h" - #if UIP_CONF_TCP == 0 #error HTTP server needs TCP enabled #endif @@ -153,7 +151,7 @@ PT_THREAD(handle_output(struct httpd_state *s)) /*---------------------------------------------------------------------------*/ const char http_get[] = "GET "; const char http_index_html[] = "/index.html"; -//const char http_referer[] = "Referer:" + static PT_THREAD(handle_input(struct httpd_state *s)) { @@ -207,7 +205,6 @@ handle_connection(struct httpd_state *s) handle_output(s); } } - /*---------------------------------------------------------------------------*/ void httpd_appcall(void *state) @@ -250,7 +247,6 @@ httpd_appcall(void *state) uip_abort(); } } - /*---------------------------------------------------------------------------*/ void httpd_init(void) diff --git a/examples/rpl-border-router/common/httpd-simple.h b/examples/rpl-border-router/common/httpd-simple.h index a16dbd99e..7dbe46bfd 100644 --- a/examples/rpl-border-router/common/httpd-simple.h +++ b/examples/rpl-border-router/common/httpd-simple.h @@ -51,7 +51,7 @@ #endif /* WEBSERVER_CONF_CFS_CONNS */ struct httpd_state; -typedef char (* httpd_simple_script_t)(struct httpd_state *s); +typedef char (*httpd_simple_script_t)(struct httpd_state *s); struct httpd_state { struct timer timer; diff --git a/examples/rpl-border-router/common/webserver.c b/examples/rpl-border-router/common/webserver.c new file mode 100644 index 000000000..00adddb7c --- /dev/null +++ b/examples/rpl-border-router/common/webserver.c @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2017, RISE SICS + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#include "contiki.h" +#include "rpl.h" +#if UIP_CONF_IPV6_RPL_LITE == 0 +#include "rpl-private.h" +#endif /* UIP_CONF_IPV6_RPL_LITE == 0 */ + +#include +#include + +/*---------------------------------------------------------------------------*/ +static const char *TOP = "Contiki-NG\n"; +static const char *BOTTOM = "\n"; +#if BUF_USES_STACK +static char *bufptr, *bufend; +#define ADD(...) do { \ + bufptr += snprintf(bufptr, bufend - bufptr, __VA_ARGS__); \ + } while(0) +#else +static char buf[256]; +static int blen; +#define ADD(...) do { \ + blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__); \ + } while(0) +#endif + +/* Use simple webserver with only one page for minimum footprint. + * Multiple connections can result in interleaved tcp segments since + * a single static buffer is used for all segments. + */ +#include "httpd-simple.h" +/* The internal webserver can provide additional information if + * enough program flash is available. + */ +#define WEBSERVER_CONF_LOADTIME 0 +#define WEBSERVER_CONF_FILESTATS 0 +#define WEBSERVER_CONF_NEIGHBOR_STATUS 0 +/* Adding links requires a larger RAM buffer. To avoid static allocation + * the stack can be used for formatting; however tcp retransmissions + * and multiple connections can result in garbled segments. + * TODO:use PSOCk_GENERATOR_SEND and tcp state storage to fix this. + */ +#define WEBSERVER_CONF_ROUTE_LINKS 0 +#if WEBSERVER_CONF_ROUTE_LINKS +#define BUF_USES_STACK 1 +#endif + +/*---------------------------------------------------------------------------*/ +static void +ipaddr_add(const uip_ipaddr_t *addr) +{ + uint16_t a; + int i, f; + for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) { + a = (addr->u8[i] << 8) + addr->u8[i + 1]; + if(a == 0 && f >= 0) { + if(f++ == 0) { + ADD("::"); + } + } else { + if(f > 0) { + f = -1; + } else if(i > 0) { + ADD(":"); + } + ADD("%x", a); + } + } +} +/*---------------------------------------------------------------------------*/ +static +PT_THREAD(generate_routes(struct httpd_state *s)) +{ + static uip_ds6_route_t *r; +#if RPL_WITH_NON_STORING + static rpl_ns_node_t *link; +#endif /* RPL_WITH_NON_STORING */ + static uip_ds6_nbr_t *nbr; +#if BUF_USES_STACK + char buf[256]; +#endif +#if WEBSERVER_CONF_LOADTIME + static clock_time_t numticks; + numticks = clock_time(); +#endif + + PSOCK_BEGIN(&s->sout); + + SEND_STRING(&s->sout, TOP); +#if BUF_USES_STACK + bufptr = buf; + bufend = bufptr + sizeof(buf); +#else + blen = 0; +#endif + ADD("Neighbors
");
+
+  for(nbr = nbr_table_head(ds6_neighbors);
+      nbr != NULL;
+      nbr = nbr_table_next(ds6_neighbors, nbr)) {
+
+#if WEBSERVER_CONF_NEIGHBOR_STATUS
+#if BUF_USES_STACK
+    { char *j = bufptr + 25;
+      ipaddr_add(&nbr->ipaddr);
+      while(bufptr < j) ADD(" ");
+      switch(nbr->state) {
+      case NBR_INCOMPLETE: ADD(" INCOMPLETE");
+        break;
+      case NBR_REACHABLE: ADD(" REACHABLE");
+        break;
+      case NBR_STALE: ADD(" STALE");
+        break;
+      case NBR_DELAY: ADD(" DELAY");
+        break;
+      case NBR_PROBE: ADD(" NBR_PROBE");
+        break;
+      }
+    }
+#else
+    { uint8_t j = blen + 25;
+      ipaddr_add(&nbr->ipaddr);
+      while(blen < j) ADD(" ");
+      switch(nbr->state) {
+      case NBR_INCOMPLETE: ADD(" INCOMPLETE");
+        break;
+      case NBR_REACHABLE: ADD(" REACHABLE");
+        break;
+      case NBR_STALE: ADD(" STALE");
+        break;
+      case NBR_DELAY: ADD(" DELAY");
+        break;
+      case NBR_PROBE: ADD(" NBR_PROBE");
+        break;
+      }
+    }
+#endif
+#else
+    ipaddr_add(&nbr->ipaddr);
+#endif
+
+    ADD("\n");
+#if BUF_USES_STACK
+    if(bufptr > bufend - 45) {
+      SEND_STRING(&s->sout, buf);
+      bufptr = buf;
+      bufend = bufptr + sizeof(buf);
+    }
+#else
+    if(blen > sizeof(buf) - 45) {
+      SEND_STRING(&s->sout, buf);
+      blen = 0;
+    }
+#endif
+  }
+  ADD("
Routes
\n");
+  SEND_STRING(&s->sout, buf);
+#if BUF_USES_STACK
+  bufptr = buf;
+  bufend = bufptr + sizeof(buf);
+#else
+  blen = 0;
+#endif
+
+  for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r)) {
+
+#if BUF_USES_STACK
+#if WEBSERVER_CONF_ROUTE_LINKS
+    ADD("ipaddr);
+    ADD("]/status.shtml>");
+    ipaddr_add(&r->ipaddr);
+    ADD("");
+#else
+    ipaddr_add(&r->ipaddr);
+#endif
+#else
+#if WEBSERVER_CONF_ROUTE_LINKS
+    ADD("ipaddr);
+    ADD("]/status.shtml>");
+    SEND_STRING(&s->sout, buf);
+    blen = 0;
+    ipaddr_add(&r->ipaddr);
+    ADD("");
+#else
+    ipaddr_add(&r->ipaddr);
+#endif
+#endif
+    ADD("/%u (via ", r->length);
+    ipaddr_add(uip_ds6_route_nexthop(r));
+    if(1 || (r->state.lifetime < 600)) {
+      ADD(") %lus\n", (unsigned long)r->state.lifetime);
+    } else {
+      ADD(")\n");
+    }
+    SEND_STRING(&s->sout, buf);
+#if BUF_USES_STACK
+    bufptr = buf;
+    bufend = bufptr + sizeof(buf);
+#else
+    blen = 0;
+#endif
+  }
+  ADD("
"); + +#if RPL_WITH_NON_STORING + ADD("Links
\n");
+  SEND_STRING(&s->sout, buf);
+#if BUF_USES_STACK
+  bufptr = buf;
+  bufend = bufptr + sizeof(buf);
+#else
+  blen = 0;
+#endif
+  for(link = rpl_ns_node_head(); link != NULL; link = rpl_ns_node_next(link)) {
+    if(link->parent != NULL) {
+      uip_ipaddr_t child_ipaddr;
+      uip_ipaddr_t parent_ipaddr;
+
+      rpl_ns_get_node_global_addr(&child_ipaddr, link);
+      rpl_ns_get_node_global_addr(&parent_ipaddr, link->parent);
+
+#if BUF_USES_STACK
+#if WEBSERVER_CONF_ROUTE_LINKS
+      ADD("");
+      ipaddr_add(&child_ipaddr);
+      ADD("");
+#else
+      ipaddr_add(&child_ipaddr);
+#endif
+#else
+#if WEBSERVER_CONF_ROUTE_LINKS
+      ADD("");
+      SEND_STRING(&s->sout, buf);
+      blen = 0;
+      ipaddr_add(&child_ipaddr);
+      ADD("");
+#else
+      ipaddr_add(&child_ipaddr);
+#endif
+#endif
+
+      ADD(" (parent: ");
+      ipaddr_add(&parent_ipaddr);
+      if(1 || (link->lifetime < 600)) {
+        ADD(") %us\n", (unsigned int)link->lifetime);
+      } else {
+        ADD(")\n");
+      }
+      SEND_STRING(&s->sout, buf);
+#if BUF_USES_STACK
+      bufptr = buf;
+      bufend = bufptr + sizeof(buf);
+#else
+      blen = 0;
+#endif
+    }
+  }
+  ADD("
"); +#endif /* RPL_WITH_NON_STORING */ + +#if WEBSERVER_CONF_FILESTATS + static uint16_t numtimes; + ADD("
This page sent %u times", ++numtimes); +#endif + +#if WEBSERVER_CONF_LOADTIME + numticks = clock_time() - numticks + 1; + ADD(" (%u.%02u sec)", numticks / CLOCK_SECOND, (100 * (numticks % CLOCK_SECOND)) / CLOCK_SECOND)); +#endif + + SEND_STRING(&s->sout, buf); + SEND_STRING(&s->sout, BOTTOM); + + PSOCK_END(&s->sout); +} +#if BORDER_ROUTER_CONF_WEBSERVER +/*---------------------------------------------------------------------------*/ +PROCESS(webserver_nogui_process, "Web server"); +PROCESS_THREAD(webserver_nogui_process, ev, data) +{ + PROCESS_BEGIN(); + + httpd_init(); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); + httpd_appcall(data); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +httpd_simple_script_t +httpd_simple_get_script(const char *name) +{ + return generate_routes; +} +#endif /* BORDER_ROUTER_CONF_WEBSERVER */ diff --git a/examples/rpl-border-router/embedded/Makefile b/examples/rpl-border-router/embedded/Makefile index cd806474f..5faba70c8 100644 --- a/examples/rpl-border-router/embedded/Makefile +++ b/examples/rpl-border-router/embedded/Makefile @@ -4,7 +4,7 @@ PROJECTDIRS += $(SOURCES_DIR)/$(TARGET) -PROJECT_SOURCEFILES += slip-bridge.c httpd-simple.c border-router-embedded.c +PROJECT_SOURCEFILES += slip-bridge.c border-router-embedded.c $(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c (cd $(CONTIKI)/tools && $(MAKE) tunslip6) diff --git a/examples/rpl-border-router/embedded/border-router-embedded.c b/examples/rpl-border-router/embedded/border-router-embedded.c index cc2f78b5c..d1d8cdafc 100644 --- a/examples/rpl-border-router/embedded/border-router-embedded.c +++ b/examples/rpl-border-router/embedded/border-router-embedded.c @@ -36,353 +36,22 @@ */ #include "contiki.h" -#include "contiki-lib.h" -#include "contiki-net.h" -#include "net/ipv6/uip.h" -#include "net/ipv6/uip-ds6.h" #include "rpl.h" -#if UIP_CONF_IPV6_RPL_LITE == 0 -#include "rpl-private.h" -#endif /* UIP_CONF_IPV6_RPL_LITE == 0 */ -#include "rpl-dag-root.h" -#if RPL_WITH_NON_STORING -#include "rpl-ns.h" -#endif /* RPL_WITH_NON_STORING */ -#include "net/netstack.h" #include "dev/button-sensor.h" #include "dev/slip.h" -#include "net/ipv6/uip-debug.h" -/*---------------------------------------------------------------------------*/ -#include -#include -#include -#include +#include "border-router-common.h" + /*---------------------------------------------------------------------------*/ /* Log configuration */ #include "sys/log.h" #define LOG_MODULE "BR" #define LOG_LEVEL LOG_LEVEL_INFO -/*---------------------------------------------------------------------------*/ -static uip_ipaddr_t prefix; -static uint8_t prefix_set; + +void request_prefix(void); + /*---------------------------------------------------------------------------*/ PROCESS(border_router_process, "Border router process"); /*---------------------------------------------------------------------------*/ -#if BORDER_ROUTER_CONF_WEBSERVER -/* Use simple webserver with only one page for minimum footprint. - * Multiple connections can result in interleaved tcp segments since - * a single static buffer is used for all segments. - */ -#include "httpd-simple.h" -/* The internal webserver can provide additional information if - * enough program flash is available. - */ -#define WEBSERVER_CONF_LOADTIME 0 -#define WEBSERVER_CONF_FILESTATS 0 -#define WEBSERVER_CONF_NEIGHBOR_STATUS 0 -/* Adding links requires a larger RAM buffer. To avoid static allocation - * the stack can be used for formatting; however tcp retransmissions - * and multiple connections can result in garbled segments. - * TODO:use PSOCk_GENERATOR_SEND and tcp state storage to fix this. - */ -#define WEBSERVER_CONF_ROUTE_LINKS 0 -#if WEBSERVER_CONF_ROUTE_LINKS -#define BUF_USES_STACK 1 -#endif - -PROCESS(webserver_nogui_process, "Web server"); -PROCESS_THREAD(webserver_nogui_process, ev, data) -{ - PROCESS_BEGIN(); - - httpd_init(); - - while(1) { - PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); - httpd_appcall(data); - } - - PROCESS_END(); -} - -static const char *TOP = "ContikiRPL\n"; -static const char *BOTTOM = "\n"; -#if BUF_USES_STACK -static char *bufptr, *bufend; -#define ADD(...) do { \ - bufptr += snprintf(bufptr, bufend - bufptr, __VA_ARGS__); \ - } while(0) -#else -static char buf[256]; -static int blen; -#define ADD(...) do { \ - blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__); \ - } while(0) -#endif - -/*---------------------------------------------------------------------------*/ -static void -ipaddr_add(const uip_ipaddr_t *addr) -{ - uint16_t a; - int i, f; - for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) { - a = (addr->u8[i] << 8) + addr->u8[i + 1]; - if(a == 0 && f >= 0) { - if(f++ == 0) ADD("::"); - } else { - if(f > 0) { - f = -1; - } else if(i > 0) { - ADD(":"); - } - ADD("%x", a); - } - } -} -/*---------------------------------------------------------------------------*/ -static -PT_THREAD(generate_routes(struct httpd_state *s)) -{ - static uip_ds6_route_t *r; -#if RPL_WITH_NON_STORING - static rpl_ns_node_t *link; -#endif /* RPL_WITH_NON_STORING */ - static uip_ds6_nbr_t *nbr; -#if BUF_USES_STACK - char buf[256]; -#endif -#if WEBSERVER_CONF_LOADTIME - static clock_time_t numticks; - numticks = clock_time(); -#endif - - PSOCK_BEGIN(&s->sout); - - SEND_STRING(&s->sout, TOP); -#if BUF_USES_STACK - bufptr = buf;bufend=bufptr+sizeof(buf); -#else - blen = 0; -#endif - ADD("Neighbors
");
-
-  for(nbr = nbr_table_head(ds6_neighbors);
-      nbr != NULL;
-      nbr = nbr_table_next(ds6_neighbors, nbr)) {
-
-#if WEBSERVER_CONF_NEIGHBOR_STATUS
-#if BUF_USES_STACK
-{char* j=bufptr+25;
-      ipaddr_add(&nbr->ipaddr);
-      while (bufptr < j) ADD(" ");
-      switch (nbr->state) {
-      case NBR_INCOMPLETE: ADD(" INCOMPLETE");break;
-      case NBR_REACHABLE: ADD(" REACHABLE");break;
-      case NBR_STALE: ADD(" STALE");break;
-      case NBR_DELAY: ADD(" DELAY");break;
-      case NBR_PROBE: ADD(" NBR_PROBE");break;
-      }
-}
-#else
-{uint8_t j=blen+25;
-      ipaddr_add(&nbr->ipaddr);
-      while (blen < j) ADD(" ");
-      switch (nbr->state) {
-      case NBR_INCOMPLETE: ADD(" INCOMPLETE");break;
-      case NBR_REACHABLE: ADD(" REACHABLE");break;
-      case NBR_STALE: ADD(" STALE");break;
-      case NBR_DELAY: ADD(" DELAY");break;
-      case NBR_PROBE: ADD(" NBR_PROBE");break;
-      }
-}
-#endif
-#else
-      ipaddr_add(&nbr->ipaddr);
-#endif
-
-      ADD("\n");
-#if BUF_USES_STACK
-      if(bufptr > bufend - 45) {
-        SEND_STRING(&s->sout, buf);
-        bufptr = buf; bufend = bufptr + sizeof(buf);
-      }
-#else
-      if(blen > sizeof(buf) - 45) {
-        SEND_STRING(&s->sout, buf);
-        blen = 0;
-      }
-#endif
-  }
-  ADD("
Routes
\n");
-  SEND_STRING(&s->sout, buf);
-#if BUF_USES_STACK
-  bufptr = buf; bufend = bufptr + sizeof(buf);
-#else
-  blen = 0;
-#endif
-
-  for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r)) {
-
-#if BUF_USES_STACK
-#if WEBSERVER_CONF_ROUTE_LINKS
-    ADD("ipaddr);
-    ADD("]/status.shtml>");
-    ipaddr_add(&r->ipaddr);
-    ADD("");
-#else
-    ipaddr_add(&r->ipaddr);
-#endif
-#else
-#if WEBSERVER_CONF_ROUTE_LINKS
-    ADD("ipaddr);
-    ADD("]/status.shtml>");
-    SEND_STRING(&s->sout, buf); //TODO: why tunslip6 needs an output here, wpcapslip does not
-    blen = 0;
-    ipaddr_add(&r->ipaddr);
-    ADD("");
-#else
-    ipaddr_add(&r->ipaddr);
-#endif
-#endif
-    ADD("/%u (via ", r->length);
-    ipaddr_add(uip_ds6_route_nexthop(r));
-    if(1 || (r->state.lifetime < 600)) {
-      ADD(") %lus\n", (unsigned long)r->state.lifetime);
-    } else {
-      ADD(")\n");
-    }
-    SEND_STRING(&s->sout, buf);
-#if BUF_USES_STACK
-    bufptr = buf; bufend = bufptr + sizeof(buf);
-#else
-    blen = 0;
-#endif
-  }
-  ADD("
"); - -#if RPL_WITH_NON_STORING - ADD("Links
\n");
-  SEND_STRING(&s->sout, buf);
-#if BUF_USES_STACK
-  bufptr = buf; bufend = bufptr + sizeof(buf);
-#else
-  blen = 0;
-#endif
-  for(link = rpl_ns_node_head(); link != NULL; link = rpl_ns_node_next(link)) {
-    if(link->parent != NULL) {
-      uip_ipaddr_t child_ipaddr;
-      uip_ipaddr_t parent_ipaddr;
-
-      rpl_ns_get_node_global_addr(&child_ipaddr, link);
-      rpl_ns_get_node_global_addr(&parent_ipaddr, link->parent);
-
-#if BUF_USES_STACK
-#if WEBSERVER_CONF_ROUTE_LINKS
-      ADD("");
-      ipaddr_add(&child_ipaddr);
-      ADD("");
-#else
-      ipaddr_add(&child_ipaddr);
-#endif
-#else
-#if WEBSERVER_CONF_ROUTE_LINKS
-      ADD("");
-      SEND_STRING(&s->sout, buf); //TODO: why tunslip6 needs an output here, wpcapslip does not
-      blen = 0;
-      ipaddr_add(&child_ipaddr);
-      ADD("");
-#else
-      ipaddr_add(&child_ipaddr);
-#endif
-#endif
-
-      ADD(" (parent: ");
-      ipaddr_add(&parent_ipaddr);
-      if(1 || (link->lifetime < 600)) {
-        ADD(") %us\n", (unsigned int)link->lifetime); // iotlab printf does not have %lu
-        //ADD(") %lus\n", (unsigned long)r->state.lifetime);
-      } else {
-        ADD(")\n");
-      }
-      SEND_STRING(&s->sout, buf);
-#if BUF_USES_STACK
-      bufptr = buf; bufend = bufptr + sizeof(buf);
-#else
-      blen = 0;
-#endif
-    }
-  }
-  ADD("
"); -#endif /* RPL_WITH_NON_STORING */ - -#if WEBSERVER_CONF_FILESTATS - static uint16_t numtimes; - ADD("
This page sent %u times",++numtimes); -#endif - -#if WEBSERVER_CONF_LOADTIME - numticks = clock_time() - numticks + 1; - ADD(" (%u.%02u sec)",numticks/CLOCK_SECOND,(100*(numticks%CLOCK_SECOND))/CLOCK_SECOND)); -#endif - - SEND_STRING(&s->sout, buf); - SEND_STRING(&s->sout, BOTTOM); - - PSOCK_END(&s->sout); -} -/*---------------------------------------------------------------------------*/ -httpd_simple_script_t -httpd_simple_get_script(const char *name) -{ - return generate_routes; -} -#endif /* BORDER_ROUTER_CONF_WEBSERVER */ -/*---------------------------------------------------------------------------*/ -static void -print_local_addresses(void) -{ - int i; - uint8_t state; - - LOG_INFO("Server IPv6 addresses:\n"); - 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)) { - LOG_INFO(" "); - LOG_INFO_6ADDR(&uip_ds6_if.addr_list[i].ipaddr); - LOG_INFO_("\n"); - } - } -} -/*---------------------------------------------------------------------------*/ -void -request_prefix(void) -{ - /* mess up uip_buf with a dirty request... */ - uip_buf[0] = '?'; - uip_buf[1] = 'P'; - uip_len = 2; - slip_send(); - uip_clear_buf(); -} -/*---------------------------------------------------------------------------*/ -void -set_prefix_64(uip_ipaddr_t *prefix_64) -{ - memcpy(&prefix, prefix_64, 16); - prefix_set = 1; - rpl_dag_root_init(prefix_64, NULL); - rpl_dag_root_init_dag_immediately(); -} -/*---------------------------------------------------------------------------*/ PROCESS_THREAD(border_router_process, ev, data) { static struct etimer et; @@ -413,13 +82,11 @@ PROCESS_THREAD(border_router_process, ev, data) NETSTACK_MAC.on(); -#if DEBUG || 1 print_local_addresses(); -#endif while(1) { PROCESS_YIELD(); - if (ev == sensors_event && data == &button_sensor) { + if(ev == sensors_event && data == &button_sensor) { LOG_INFO("Initiating global repair\n"); #if UIP_CONF_IPV6_RPL_LITE rpl_global_repair(); diff --git a/examples/rpl-border-router/embedded/slip-bridge.c b/examples/rpl-border-router/embedded/slip-bridge.c index abf95dfa8..cb85793f0 100644 --- a/examples/rpl-border-router/embedded/slip-bridge.c +++ b/examples/rpl-border-router/embedded/slip-bridge.c @@ -53,6 +53,18 @@ void set_prefix_64(uip_ipaddr_t *); static uip_ipaddr_t last_sender; + +/*---------------------------------------------------------------------------*/ +void +request_prefix(void) +{ + /* mess up uip_buf with a dirty request... */ + uip_buf[0] = '?'; + uip_buf[1] = 'P'; + uip_len = 2; + slip_send(); + uip_clear_buf(); +} /*---------------------------------------------------------------------------*/ static void slip_input_callback(void) diff --git a/examples/rpl-border-router/native/Makefile b/examples/rpl-border-router/native/Makefile index f178da2a8..30690df0f 100644 --- a/examples/rpl-border-router/native/Makefile +++ b/examples/rpl-border-router/native/Makefile @@ -1,7 +1,7 @@ MODULES += os/services/slip-cmd PROJECT_SOURCEFILES += border-router-native.c -PROJECT_SOURCEFILES += border-router-cmds.c tun-bridge.c httpd-simple.c +PROJECT_SOURCEFILES += border-router-cmds.c tun-bridge.c PROJECT_SOURCEFILES += slip-config.c slip-dev.c border-router-mac.c MAKE_MAC = MAKE_MAC_OTHER diff --git a/examples/rpl-border-router/native/border-router-cmds.c b/examples/rpl-border-router/native/border-router-cmds.c index 70f1739d7..75788a920 100644 --- a/examples/rpl-border-router/native/border-router-cmds.c +++ b/examples/rpl-border-router/native/border-router-cmds.c @@ -47,7 +47,6 @@ #define DEBUG DEBUG_NONE #include "net/ipv6/uip-debug.h" - uint8_t command_context; void packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx); @@ -86,7 +85,7 @@ border_router_cmd_handler(const uint8_t *data, int len) } else if(data[1] == 'R' && command_context == CMD_CONTEXT_RADIO) { /* We need to know that this is from the slip-radio here. */ PRINTF("Packet data report for sid:%d st:%d tx:%d\n", - data[2], data[3], data[4]); + data[2], data[3], data[4]); packet_sent(data[2], data[3], data[4]); return 1; } else if(data[1] == 'D' && command_context == CMD_CONTEXT_RADIO) { @@ -99,7 +98,7 @@ border_router_cmd_handler(const uint8_t *data, int len) PRINTF("Got request message of type %c\n", data[1]); if(data[1] == 'M' && command_context == CMD_CONTEXT_STDIO) { uint8_t buf[20]; - char* hexchar = "0123456789abcdef"; + char *hexchar = "0123456789abcdef"; int j; /* this is just a test so far... just to see if it works */ buf[0] = '!'; @@ -141,7 +140,7 @@ PROCESS_THREAD(border_router_cmd_process, ev, data) PROCESS_YIELD(); if(ev == serial_line_event_message && data != NULL) { PRINTF("Got serial data!!! %s of len: %d\n", - (char *)data, strlen((char *)data)); + (char *)data, strlen((char *)data)); command_context = CMD_CONTEXT_STDIO; cmd_input(data, strlen((char *)data)); } diff --git a/examples/rpl-border-router/native/border-router-mac.c b/examples/rpl-border-router/native/border-router-mac.c index bfb20ce58..7b002183e 100644 --- a/examples/rpl-border-router/native/border-router-mac.c +++ b/examples/rpl-border-router/native/border-router-mac.c @@ -66,7 +66,8 @@ struct tx_callback { static struct tx_callback callbacks[MAX_CALLBACKS]; /*---------------------------------------------------------------------------*/ -void packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx) +void +packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx) { if(sessionid < MAX_CALLBACKS) { struct tx_callback *callback; @@ -114,7 +115,6 @@ send_packet(mac_callback_t sent, void *ptr) /* Failed to allocate space for headers */ PRINTF("br-rdc: send failed, too large header\n"); mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1); - } else { /* here we send the data over SLIP to the radio-chip */ size = 0; diff --git a/examples/rpl-border-router/native/border-router-native.c b/examples/rpl-border-router/native/border-router-native.c index 8b62fb0f0..fd5e3cde7 100644 --- a/examples/rpl-border-router/native/border-router-native.c +++ b/examples/rpl-border-router/native/border-router-native.c @@ -39,34 +39,23 @@ */ #include "contiki.h" -#include "contiki-lib.h" #include "contiki-net.h" -#include "net/ipv6/uip.h" -#include "net/ipv6/uip-ds6.h" #include "rpl.h" -#include "rpl-dag-root.h" - -#include "net/netstack.h" -#include "dev/slip.h" +#include "border-router-common.h" #include "cmd.h" #include "border-router.h" #include "border-router-cmds.h" -#include -#include -#include -#include - #define DEBUG DEBUG_FULL #include "net/ipv6/uip-debug.h" +#include + #define MAX_SENSORS 4 extern long slip_sent; extern long slip_received; -static uip_ipaddr_t prefix; -static uint8_t prefix_set; static uint8_t mac_set; static uint8_t sensor_count = 0; @@ -82,145 +71,6 @@ CMD_HANDLERS(border_router_cmd_handler); PROCESS(border_router_process, "Border router process"); -#if BORDER_ROUTER_CONF_WEBSERVER -/* Use simple webserver with only one page */ -#include "httpd-simple.h" -PROCESS(webserver_nogui_process, "Web server"); -PROCESS_THREAD(webserver_nogui_process, ev, data) -{ - PROCESS_BEGIN(); - - httpd_init(); - - while(1) { - PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); - httpd_appcall(data); - } - - PROCESS_END(); -} - -static const char *TOP = "ContikiRPL\n"; -static const char *BOTTOM = "\n"; -static char buf[128]; -static int blen; -#define ADD(...) do { \ - blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__); \ - } while(0) -/*---------------------------------------------------------------------------*/ -static void -ipaddr_add(const uip_ipaddr_t *addr) -{ - uint16_t a; - int i, f; - for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) { - a = (addr->u8[i] << 8) + addr->u8[i + 1]; - if(a == 0 && f >= 0) { - if(f++ == 0 && sizeof(buf) - blen >= 2) { - buf[blen++] = ':'; - buf[blen++] = ':'; - } - } else { - if(f > 0) { - f = -1; - } else if(i > 0 && blen < sizeof(buf)) { - buf[blen++] = ':'; - } - ADD("%x", a); - } - } -} -/*---------------------------------------------------------------------------*/ -static -PT_THREAD(generate_routes(struct httpd_state *s)) -{ - static int i; - static uip_ds6_route_t *r; - static uip_ds6_nbr_t *nbr; - - PSOCK_BEGIN(&s->sout); - - SEND_STRING(&s->sout, TOP); - - blen = 0; - ADD("Neighbors
");
-  for(nbr = nbr_table_head(ds6_neighbors);
-      nbr != NULL;
-      nbr = nbr_table_next(ds6_neighbors, nbr)) {
-    ipaddr_add(&nbr->ipaddr);
-    ADD("\n");
-    if(blen > sizeof(buf) - 45) {
-      SEND_STRING(&s->sout, buf);
-      blen = 0;
-    }
-  }
-
-  ADD("
Routes
");
-  SEND_STRING(&s->sout, buf);
-  blen = 0;
-  for(r = uip_ds6_route_head();
-      r != NULL;
-      r = uip_ds6_route_next(r)) {
-    ipaddr_add(&r->ipaddr);
-    ADD("/%u (via ", r->length);
-    ipaddr_add(uip_ds6_route_nexthop(r));
-    if(r->state.lifetime < 600) {
-      ADD(") %lus\n", (unsigned long)r->state.lifetime);
-    } else {
-      ADD(")\n");
-    }
-    SEND_STRING(&s->sout, buf);
-    blen = 0;
-  }
-  ADD("
"); -//if(blen > 0) { - SEND_STRING(&s->sout, buf); -// blen = 0; -//} - - if(sensor_count > 0) { - ADD("Sensors
");
-    SEND_STRING(&s->sout, buf);
-    blen = 0;
-    for(i = 0; i < sensor_count; i++) {
-      ADD("%s\n", sensors[i]);
-      SEND_STRING(&s->sout, buf);
-      blen = 0;
-    }
-    ADD("
"); - SEND_STRING(&s->sout, buf); - } - - - SEND_STRING(&s->sout, BOTTOM); - - PSOCK_END(&s->sout); -} -/*---------------------------------------------------------------------------*/ -httpd_simple_script_t -httpd_simple_get_script(const char *name) -{ - return generate_routes; -} -#endif /* BORDER_ROUTER_CONF_WEBSERVER */ -/*---------------------------------------------------------------------------*/ -static void -print_local_addresses(void) -{ - int i; - uint8_t state; - - PRINTA("Server IPv6 addresses:\n"); - 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)) { - PRINTA(" %p: =>", &uip_ds6_if.addr_list[i]); - uip_debug_ipaddr_print(&(uip_ds6_if.addr_list[i]).ipaddr); - PRINTA("\n"); - } - } -} /*---------------------------------------------------------------------------*/ static void request_mac(void) @@ -250,7 +100,6 @@ border_router_print_stat() printf("bytes received over SLIP: %ld\n", slip_received); printf("bytes sent over SLIP: %ld\n", slip_sent); } - /*---------------------------------------------------------------------------*/ /* Format: ;;...;*/ /* this function just cut at ; and store in the sensor array */ @@ -260,7 +109,7 @@ border_router_set_sensors(const char *data, int len) int i; int last_pos = 0; int sc = 0; - for(i = 0;i < len; i++) { + for(i = 0; i < len; i++) { if(data[i] == ';') { sensors[sc][i - last_pos] = 0; memcpy(sensors[sc++], &data[last_pos], i - last_pos); @@ -276,15 +125,6 @@ border_router_set_sensors(const char *data, int len) sensor_count = sc; } /*---------------------------------------------------------------------------*/ -static void -set_prefix_64(uip_ipaddr_t *prefix_64) -{ - memcpy(&prefix, prefix_64, 16); - prefix_set = 1; - rpl_dag_root_init(prefix_64, NULL); - rpl_dag_root_init_dag_immediately(); -} -/*---------------------------------------------------------------------------*/ PROCESS_THREAD(border_router_process, ev, data) { static struct etimer et; @@ -323,9 +163,7 @@ PROCESS_THREAD(border_router_process, ev, data) } } -#if DEBUG print_local_addresses(); -#endif while(1) { etimer_set(&et, CLOCK_SECOND * 2); diff --git a/examples/rpl-border-router/native/project-conf.h b/examples/rpl-border-router/native/project-conf.h index 5a0c524eb..841a31f67 100644 --- a/examples/rpl-border-router/native/project-conf.h +++ b/examples/rpl-border-router/native/project-conf.h @@ -40,7 +40,6 @@ /* use a non-default MAC driver */ #define NETSTACK_CONF_MAC border_router_mac_driver - #define SLIP_DEV_CONF_SEND_DELAY (CLOCK_SECOND / 32) #define WEBSERVER_CONF_CFS_CONNS 2 @@ -50,7 +49,6 @@ #define CMD_CONF_OUTPUT border_router_cmd_output - /* used by wpcap (see /cpu/native/net/wpcap-drv.c) */ #define SELECT_CALLBACK 1 diff --git a/examples/rpl-border-router/native/slip-config.c b/examples/rpl-border-router/native/slip-config.c index d6b6e0660..a8e17f22c 100644 --- a/examples/rpl-border-router/native/slip-config.c +++ b/examples/rpl-border-router/native/slip-config.c @@ -89,17 +89,17 @@ slip_config_handle_arguments(int argc, char **argv) case 's': if(strncmp("/dev/", optarg, 5) == 0) { - slip_config_siodev = optarg + 5; + slip_config_siodev = optarg + 5; } else { - slip_config_siodev = optarg; + slip_config_siodev = optarg; } break; case 't': if(strncmp("/dev/", optarg, 5) == 0) { - strncpy(slip_config_tundev, optarg + 5, sizeof(slip_config_tundev)); + strncpy(slip_config_tundev, optarg + 5, sizeof(slip_config_tundev)); } else { - strncpy(slip_config_tundev, optarg, sizeof(slip_config_tundev)); + strncpy(slip_config_tundev, optarg, sizeof(slip_config_tundev)); } break; @@ -113,43 +113,47 @@ slip_config_handle_arguments(int argc, char **argv) case 'd': slip_config_basedelay = 10; - if(optarg) slip_config_basedelay = atoi(optarg); + if(optarg) { + slip_config_basedelay = atoi(optarg); + } break; case 'v': slip_config_verbose = 2; - if(optarg) slip_config_verbose = atoi(optarg); + if(optarg) { + slip_config_verbose = atoi(optarg); + } break; case '?': case 'h': default: -fprintf(stderr,"usage: %s [options] ipaddress\n", prog); -fprintf(stderr,"example: border-router.native -L -v2 -s ttyUSB1 fd00::1/64\n"); -fprintf(stderr,"Options are:\n"); + fprintf(stderr, "usage: %s [options] ipaddress\n", prog); + fprintf(stderr, "example: border-router.native -L -v2 -s ttyUSB1 fd00::1/64\n"); + fprintf(stderr, "Options are:\n"); #ifdef linux -fprintf(stderr," -B baudrate 9600,19200,38400,57600,115200,921600 (default 115200)\n"); + fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200,921600 (default 115200)\n"); #else -fprintf(stderr," -B baudrate 9600,19200,38400,57600,115200 (default 115200)\n"); + fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200 (default 115200)\n"); #endif -fprintf(stderr," -H Hardware CTS/RTS flow control (default disabled)\n"); -fprintf(stderr," -L Log output format (adds time stamps)\n"); -fprintf(stderr," -s siodev Serial device (default /dev/ttyUSB0)\n"); -fprintf(stderr," -a host Connect via TCP to server at \n"); -fprintf(stderr," -p port Connect via TCP to server at :\n"); -fprintf(stderr," -t tundev Name of interface (default tun0)\n"); -fprintf(stderr," -v[level] Verbosity level\n"); -fprintf(stderr," -v0 No messages\n"); -fprintf(stderr," -v1 Encapsulated SLIP debug messages (default)\n"); -fprintf(stderr," -v2 Printable strings after they are received\n"); -fprintf(stderr," -v3 Printable strings and SLIP packet notifications\n"); -fprintf(stderr," -v4 All printable characters as they are received\n"); -fprintf(stderr," -v5 All SLIP packets in hex\n"); -fprintf(stderr," -v Equivalent to -v3\n"); -fprintf(stderr," -d[basedelay] Minimum delay between outgoing SLIP packets.\n"); -fprintf(stderr," Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n"); -fprintf(stderr," -d is equivalent to -d10.\n"); -exit(1); + fprintf(stderr, " -H Hardware CTS/RTS flow control (default disabled)\n"); + fprintf(stderr, " -L Log output format (adds time stamps)\n"); + fprintf(stderr, " -s siodev Serial device (default /dev/ttyUSB0)\n"); + fprintf(stderr, " -a host Connect via TCP to server at \n"); + fprintf(stderr, " -p port Connect via TCP to server at :\n"); + fprintf(stderr, " -t tundev Name of interface (default tun0)\n"); + fprintf(stderr, " -v[level] Verbosity level\n"); + fprintf(stderr, " -v0 No messages\n"); + fprintf(stderr, " -v1 Encapsulated SLIP debug messages (default)\n"); + fprintf(stderr, " -v2 Printable strings after they are received\n"); + fprintf(stderr, " -v3 Printable strings and SLIP packet notifications\n"); + fprintf(stderr, " -v4 All printable characters as they are received\n"); + fprintf(stderr, " -v5 All SLIP packets in hex\n"); + fprintf(stderr, " -v Equivalent to -v3\n"); + fprintf(stderr, " -d[basedelay] Minimum delay between outgoing SLIP packets.\n"); + fprintf(stderr, " Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n"); + fprintf(stderr, " -d is equivalent to -d10.\n"); + exit(1); break; } } diff --git a/examples/rpl-border-router/native/slip-dev.c b/examples/rpl-border-router/native/slip-dev.c index b5d3b171b..63b6f1638 100644 --- a/examples/rpl-border-router/native/slip-dev.c +++ b/examples/rpl-border-router/native/slip-dev.c @@ -80,7 +80,6 @@ long slip_received = 0; int slipfd = 0; -//#define PROGRESS(s) fprintf(stderr, s) #define PROGRESS(s) do { } while(0) #define SLIP_END 0300 @@ -93,9 +92,9 @@ static void * get_in_addr(struct sockaddr *sa) { if(sa->sa_family == AF_INET) { - return &(((struct sockaddr_in*)sa)->sin_addr); + return &(((struct sockaddr_in *)sa)->sin_addr); } - return &(((struct sockaddr_in6*)sa)->sin6_addr); + return &(((struct sockaddr_in6 *)sa)->sin6_addr); } /*---------------------------------------------------------------------------*/ static int @@ -138,7 +137,7 @@ connect_to_server(const char *host, const char *port) fcntl(fd, F_SETFL, O_NONBLOCK); inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), - s, sizeof(s)); + s, sizeof(s)); /* all done with this structure */ freeaddrinfo(servinfo); @@ -178,23 +177,25 @@ serial_input(FILE *inslip) { static unsigned char inbuf[2048]; static int inbufptr = 0; - int ret,i; + int ret, i; unsigned char c; #ifdef linux ret = fread(&c, 1, 1, inslip); - if(ret == -1 || ret == 0) err(1, "serial_input: read"); + if(ret == -1 || ret == 0) { + err(1, "serial_input: read"); + } goto after_fread; #endif - read_more: +read_more: if(inbufptr >= sizeof(inbuf)) { - fprintf(stderr, "*** dropping large %d byte packet\n", inbufptr); - inbufptr = 0; + fprintf(stderr, "*** dropping large %d byte packet\n", inbufptr); + inbufptr = 0; } ret = fread(&c, 1, 1, inslip); #ifdef linux - after_fread: +after_fread: #endif if(ret == -1) { err(1, "serial_input: read"); @@ -208,12 +209,12 @@ serial_input(FILE *inslip) case SLIP_END: if(inbufptr > 0) { if(inbuf[0] == '!') { - command_context = CMD_CONTEXT_RADIO; - cmd_input(inbuf, inbufptr); + command_context = CMD_CONTEXT_RADIO; + cmd_input(inbuf, inbufptr); } else if(inbuf[0] == '?') { #define DEBUG_LINE_MARKER '\r' } else if(inbuf[0] == DEBUG_LINE_MARKER) { - fwrite(inbuf + 1, inbufptr - 1, 1, stdout); + fwrite(inbuf + 1, inbufptr - 1, 1, stdout); } else if(is_sensible_string(inbuf, inbufptr)) { if(slip_config_verbose == 1) { /* strings already echoed below for verbose>1 */ fwrite(inbuf, inbufptr, 1, stdout); @@ -224,19 +225,25 @@ serial_input(FILE *inslip) if(slip_config_verbose > 4) { #if WIRESHARK_IMPORT_FORMAT printf("0000"); - for(i = 0; i < inbufptr; i++) printf(" %02x", inbuf[i]); + for(i = 0; i < inbufptr; i++) { + printf(" %02x", inbuf[i]); + } #else printf(" "); for(i = 0; i < inbufptr; i++) { printf("%02x", inbuf[i]); - if((i & 3) == 3) printf(" "); - if((i & 15) == 15) printf("\n "); + if((i & 3) == 3) { + printf(" "); + } + if((i & 15) == 15) { + printf("\n "); + } } #endif printf("\n"); } } - slip_packet_input(inbuf, inbufptr); + slip_packet_input(inbuf, inbufptr); } inbufptr = 0; } @@ -266,7 +273,7 @@ serial_input(FILE *inslip) /* Echo all printable characters for verbose==4 */ if(slip_config_verbose == 4) { if(c == 0 || c == '\r' || c == '\n' || c == '\t' || (c >= ' ' && c <= '~')) { - fwrite(&c, 1, 1, stdout); + fwrite(&c, 1, 1, stdout); } } else if(slip_config_verbose >= 2) { if(c == '\n' && is_sensible_string(inbuf, inbufptr)) { @@ -279,7 +286,6 @@ serial_input(FILE *inslip) goto read_more; } - unsigned char slip_buf[2048]; int slip_end, slip_begin, slip_packet_end, slip_packet_count; static struct timer send_delay_timer; @@ -367,13 +373,19 @@ write_to_serial(int outfd, const uint8_t *inbuf, int len) if(slip_config_verbose > 4) { #if WIRESHARK_IMPORT_FORMAT printf("0000"); - for(i = 0; i < len; i++) printf(" %02x", p[i]); + for(i = 0; i < len; i++) { + printf(" %02x", p[i]); + } #else printf(" "); for(i = 0; i < len; i++) { printf("%02x", p[i]); - if((i & 3) == 3) printf(" "); - if((i & 15) == 15) printf("\n "); + if((i & 3) == 3) { + printf(" "); + } + if((i & 15) == 15) { + printf("\n "); + } } #endif printf("\n"); @@ -420,9 +432,13 @@ stty_telos(int fd) speed_t speed = slip_config_b_rate; int i; - if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush"); + if(tcflush(fd, TCIOFLUSH) == -1) { + err(1, "tcflush"); + } - if(tcgetattr(fd, &tty) == -1) err(1, "tcgetattr"); + if(tcgetattr(fd, &tty) == -1) { + err(1, "tcgetattr"); + } cfmakeraw(&tty); @@ -440,23 +456,31 @@ stty_telos(int fd) cfsetispeed(&tty, speed); cfsetospeed(&tty, speed); - if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr"); + if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) { + err(1, "tcsetattr"); + } #if 1 /* Nonblocking read and write. */ /* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(1, "fcntl"); */ tty.c_cflag |= CLOCAL; - if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr"); + if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) { + err(1, "tcsetattr"); + } i = TIOCM_DTR; - if(ioctl(fd, TIOCMBIS, &i) == -1) err(1, "ioctl"); + if(ioctl(fd, TIOCMBIS, &i) == -1) { + err(1, "ioctl"); + } #endif - usleep(10*1000); /* Wait for hardware 10ms. */ + usleep(10 * 1000); /* Wait for hardware 10ms. */ /* Flush input and output buffers. */ - if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush"); + if(tcflush(fd, TCIOFLUSH) == -1) { + err(1, "tcflush"); + } } /*---------------------------------------------------------------------------*/ static int @@ -498,7 +522,6 @@ slip_init(void) if(slipfd == -1) { err(1, "can't connect to ``%s:%s''", slip_config_host, slip_config_port); } - } else if(slip_config_siodev != NULL) { if(strcmp(slip_config_siodev, "null") == 0) { /* Disable slip */ @@ -508,7 +531,6 @@ slip_init(void) if(slipfd == -1) { err(1, "can't open siodev ``/dev/%s''", slip_config_siodev); } - } else { static const char *siodevs[] = { "ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */ @@ -518,7 +540,7 @@ slip_init(void) slip_config_siodev = siodevs[i]; slipfd = devopen(slip_config_siodev, O_RDWR | O_NONBLOCK); if(slipfd != -1) { - break; + break; } } if(slipfd == -1) { @@ -530,7 +552,7 @@ slip_init(void) if(slip_config_host != NULL) { fprintf(stderr, "********SLIP opened to ``%s:%s''\n", slip_config_host, - slip_config_port); + slip_config_port); } else { fprintf(stderr, "********SLIP started on ``/dev/%s''\n", slip_config_siodev); stty_telos(slipfd); diff --git a/examples/rpl-border-router/native/tun-bridge.c b/examples/rpl-border-router/native/tun-bridge.c index d7096d5e2..a60819b26 100644 --- a/examples/rpl-border-router/native/tun-bridge.c +++ b/examples/rpl-border-router/native/tun-bridge.c @@ -96,7 +96,6 @@ ssystem(const char *fmt, ...) fflush(stdout); return system(cmd); } - /*---------------------------------------------------------------------------*/ void cleanup(void) @@ -110,7 +109,6 @@ cleanup(void) " | sh", slip_config_tundev); } - /*---------------------------------------------------------------------------*/ void sigcleanup(int signo) @@ -118,7 +116,6 @@ sigcleanup(int signo) fprintf(stderr, "signal %d\n", signo); exit(0); /* exit(0) will call cleanup() */ } - /*---------------------------------------------------------------------------*/ void ifconf(const char *tundev, const char *ipaddr) @@ -154,7 +151,7 @@ tun_alloc(char *dev) struct ifreq ifr; int fd, err; - if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { + if((fd = open("/dev/net/tun", O_RDWR)) < 0) { return -1; } @@ -164,10 +161,11 @@ tun_alloc(char *dev) * IFF_NO_PI - Do not provide packet information */ ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - if(*dev != 0) + if(*dev != 0) { strncpy(ifr.ifr_name, dev, IFNAMSIZ); + } - if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) { + if((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) { close(fd); return err; } @@ -191,11 +189,10 @@ tun_init() slip_init(); } - #else -static uint16_t delaymsec=0; -static uint32_t delaystartsec,delaystartmsec; +static uint16_t delaymsec = 0; +static uint32_t delaystartsec, delaystartmsec; /*---------------------------------------------------------------------------*/ void @@ -209,7 +206,9 @@ tun_init() tunfd = tun_alloc(slip_config_tundev); - if(tunfd == -1) err(1, "main: open"); + if(tunfd == -1) { + err(1, "main: open"); + } select_set_callback(tunfd, &tun_select_callback); @@ -222,7 +221,6 @@ tun_init() signal(SIGINT, sigcleanup); ifconf(slip_config_tundev, slip_config_ipaddr); } - /*---------------------------------------------------------------------------*/ static int tun_output(uint8_t *data, int len) @@ -239,11 +237,11 @@ int tun_input(unsigned char *data, int maxlen) { int size; - if((size = read(tunfd, data, maxlen)) == -1) + if((size = read(tunfd, data, maxlen)) == -1) { err(1, "tun_input: read"); + } return size; } - /*---------------------------------------------------------------------------*/ static void init(void) @@ -259,8 +257,6 @@ output(void) } return 0; } - - const struct uip_fallback_interface rpl_interface = { init, output }; @@ -274,7 +270,6 @@ set_fd(fd_set *rset, fd_set *wset) FD_SET(tunfd, rset); return 1; } - /*---------------------------------------------------------------------------*/ static void @@ -287,12 +282,16 @@ handle_fd(fd_set *rset, fd_set *wset) struct timeval tv; int dmsec; gettimeofday(&tv, NULL); - dmsec=(tv.tv_sec-delaystartsec)*1000+tv.tv_usec/1000-delaystartmsec; - if(dmsec<0) delaymsec=0; - if(dmsec>delaymsec) delaymsec=0; + dmsec = (tv.tv_sec - delaystartsec) * 1000 + tv.tv_usec / 1000 - delaystartmsec; + if(dmsec < 0) { + delaymsec = 0; + } + if(dmsec > delaymsec) { + delaymsec = 0; + } } - if(delaymsec==0) { + if(delaymsec == 0) { int size; if(FD_ISSET(tunfd, rset)) { @@ -303,10 +302,10 @@ handle_fd(fd_set *rset, fd_set *wset) if(slip_config_basedelay) { struct timeval tv; - gettimeofday(&tv, NULL) ; - delaymsec=slip_config_basedelay; - delaystartsec =tv.tv_sec; - delaystartmsec=tv.tv_usec/1000; + gettimeofday(&tv, NULL); + delaymsec = slip_config_basedelay; + delaystartsec = tv.tv_sec; + delaystartmsec = tv.tv_usec / 1000; } } }