From cbe6db5570cb9e95c15d215f0ec48794de92c2e7 Mon Sep 17 00:00:00 2001
From: BytesGalore <Martin.Landsmann@HAW-Hamburg.de>
Date: Mon, 19 Jan 2015 12:07:06 +0100
Subject: [PATCH] examples/rpl_udp: Fix unconditional setting of a radio
 channel on initialization

---
 examples/rpl_udp/rpl.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/examples/rpl_udp/rpl.c b/examples/rpl_udp/rpl.c
index 3d4cad5dc3..4be9c802e3 100644
--- a/examples/rpl_udp/rpl.c
+++ b/examples/rpl_udp/rpl.c
@@ -44,7 +44,7 @@ void rpl_udp_init(int argc, char **argv)
 {
     transceiver_command_t tcmd;
     msg_t m;
-    uint32_t chan = RADIO_CHANNEL;
+    int32_t chan = 0;
 
     if (argc != 2) {
         printf("Usage: %s (r|n|h)\n", argv[0]);
@@ -70,6 +70,28 @@ void rpl_udp_init(int argc, char **argv)
         DEBUGF("Setting HW address to %u\n", id);
         net_if_set_hardware_address(0, id);
 
+        tcmd.transceivers = TRANSCEIVER;
+        tcmd.data = &chan;
+        m.type = GET_CHANNEL;
+        m.content.ptr = (void *) &tcmd;
+
+        msg_send_receive(&m, &m, transceiver_pid);
+        if( chan == 0 ) {
+            DEBUGF("The channel has not been set yet.");
+
+            /* try to set the channel to 10 (RADIO_CHANNEL) */
+            chan = RADIO_CHANNEL;
+        }
+
+        m.type = SET_CHANNEL;
+        msg_send_receive(&m, &m, transceiver_pid);
+        if( chan == 0 ) {
+            puts("ERROR: channel NOT set! Aborting initialization.");
+            return;
+        }
+
+        printf("Channel set to %d\n", chan);
+
         if (command != 'h') {
             DEBUGF("Initializing RPL for interface 0\n");
             uint8_t state = rpl_init(0);
@@ -124,15 +146,6 @@ void rpl_udp_init(int argc, char **argv)
         ipv6_init_as_router();
     }
 
-    /* set channel to 10 */
-    tcmd.transceivers = TRANSCEIVER;
-    tcmd.data = &chan;
-    m.type = SET_CHANNEL;
-    m.content.ptr = (void *) &tcmd;
-
-    msg_send_receive(&m, &m, transceiver_pid);
-    printf("Channel set to %u\n", RADIO_CHANNEL);
-
     puts("Transport layer initialized");
     /* start transceiver watchdog */
 }
-- 
GitLab