Digi International Inc. / Mbed 2 deprecated XBeeDM_node_discovery

Dependencies:   XBeeLib mbed

Fork of XBeeZB_node_discovery by Digi International Inc.

Revision:
3:ee7b06215329
Parent:
2:5e41a845f211
--- a/main.cpp	Fri May 08 11:53:15 2015 +0200
+++ b/main.cpp	Mon May 11 18:00:30 2015 +0200
@@ -17,27 +17,21 @@
 using namespace DigiLog;
 #endif
 
-#define REMOTE_NODE_ID      "MyNodeID" /* TODO: Write a node's NI here*/
-
 using namespace XBeeLib;
 
 Serial *log_serial;
 
-XBeeZB * xbee = NULL;
+static RemoteXBeeZB send_to = RemoteXBeeZB();
 
 void discovery_function(const RemoteXBeeZB& remote, char const * const node_id)
 {
-    MBED_ASSERT(xbee != NULL);
-
     const uint64_t remote_addr64 = remote.get_addr64();
 
-    log_serial->printf("Found device '%s' [%08x:%08x][%04X] sending a message to it\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
-
-    const char message[] = "Hello neighbor!";
-    log_serial->printf("Sending data to remote device\r\n");
-    const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1);
-    if (txStatus != TxStatusSuccess) {
-        log_serial->printf("Found an error while sending data %d\r\n", txStatus);
+    log_serial->printf("Found device '%s' [%08x:%08x][%04X]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
+    if (send_to.is_valid()) {
+        log_serial->printf( "Skipping salutation to '%s' [%08x:%08x][%04X] because previous one has not been sent\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
+    } else {
+        send_to = remote;
     }
 }
 
@@ -52,46 +46,36 @@
     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
 #endif
 
-    xbee = new XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+    XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
 
     /* Register callbacks */
-    xbee->register_node_discovery_cb(&discovery_function);
+    xbee.register_node_discovery_cb(&discovery_function);
 
-    RadioStatus const radioStatus = xbee->init();
+    RadioStatus const radioStatus = xbee.init();
     MBED_ASSERT(radioStatus == Success);
 
     /* Wait until the device has joined the network */
     log_serial->printf("Waiting for device to join the network: ");
-    while (!xbee->is_joined()) {
+    while (!xbee.is_joined()) {
         wait_ms(1000);
         log_serial->printf(".");
     }
     log_serial->printf("OK\r\n");
 
     log_serial->printf("Starting Node Discovery\r\n");
-
-    log_serial->printf("Trying to discover '%s' by its NI\r\n", REMOTE_NODE_ID);
-    RemoteXBeeZB remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID);
-
-    if (remote_node.is_valid()) {
-        const uint64_t addr64 = remote_node.get_addr64();
-        log_serial->printf("Found '%s'! [%08x:%08x|%04x]\r\n", REMOTE_NODE_ID, UINT64_HI32(addr64), UINT64_LO32(addr64), remote_node.get_addr16());
-
-        const char message[] = "Hello " REMOTE_NODE_ID "!";
-        log_serial->printf("Sending data to remote device\r\n");
-        const TxStatus txStatus = xbee->send_data(remote_node, (const uint8_t *)message, sizeof message - 1);
-        if (txStatus != TxStatusSuccess) {
-            log_serial->printf("Found an error while sending data %d\r\n", txStatus);
-        }
-    } else {
-        log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID);
-    }
-
-    log_serial->printf("Starting Node Discovery\r\n");
-    xbee->start_node_discovery();
+    xbee.start_node_discovery();
 
     while (true) {
-        xbee->process_rx_frames();
+        xbee.process_rx_frames();
+        if (send_to.is_valid()) {
+            const char message[] = "Hello neighbor!";
+            log_serial->printf("Sending data to remote device\r\n");
+            const TxStatus txStatus = xbee.send_data(send_to, (const uint8_t *)message, sizeof message - 1);
+            if (txStatus != TxStatusSuccess) {
+                log_serial->printf("Found an error while sending data %d\r\n", txStatus);
+            }
+            send_to = RemoteXBeeZB(); /* Set it to an uninitialized object again */
+        }
         wait_ms(10);
     }