]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
*** empty log message ***
authorrarseverino <rarseverino>
Fri, 18 Sep 2009 15:58:21 +0000 (15:58 +0000)
committerrarseverino <rarseverino>
Fri, 18 Sep 2009 15:58:21 +0000 (15:58 +0000)
tos/lib/net/zigbee/apps/Test_APL/Makefile
tos/lib/net/zigbee/apps/Test_APL/Test_APL.nc [deleted file]
tos/lib/net/zigbee/apps/Test_APL/Test_APLC.nc [new file with mode: 0644]
tos/lib/net/zigbee/apps/Test_APL/Test_APLM.nc [deleted file]
tos/lib/net/zigbee/apps/Test_APL/Test_APLP.nc [new file with mode: 0644]
tos/lib/net/zigbee/ieee802154/nwk/Makefile
tos/lib/net/zigbee/ieee802154/nwk/NWK.nc [deleted file]
tos/lib/net/zigbee/ieee802154/nwk/NWKC.nc [new file with mode: 0644]
tos/lib/net/zigbee/ieee802154/nwk/NWKM.nc [deleted file]
tos/lib/net/zigbee/ieee802154/nwk/NWKP.nc [new file with mode: 0644]
tos/lib/net/zigbee/ieee802154/nwk/nwk_const.h

index 086b607e490591e6486f53365df58ee64e0e9219..31172b4b62039fa02612ca8a38ba463f4694d7d4 100644 (file)
@@ -1,4 +1,4 @@
-COMPONENT=Test_APL
+COMPONENT=Test_APLC
 
 TKN154_PLATFORM_INCLUDE=$(TOSROOT)/tos/platforms/telosb/mac/tkn154/Makefile.include
 
@@ -33,6 +33,8 @@ PFLAGS += -I$(TOSROOT)/tos/lib/mac/tkn154 \
 
   include $(TKN154_PLATFORM_INCLUDE)   
 
+
+
 endif
 
 include $(MAKERULES)           
diff --git a/tos/lib/net/zigbee/apps/Test_APL/Test_APL.nc b/tos/lib/net/zigbee/apps/Test_APL/Test_APL.nc
deleted file mode 100644 (file)
index 0e2f093..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
- * @author Andre Cunha
- *
- */
-#include <Timer.h>
-
-#ifndef TKN154_MAC
-
-#include "phy_const.h"
-#include "phy_enumerations.h"
-#include "mac_const.h"
-#include "mac_enumerations.h"
-#include "mac_func.h"
-
-#endif
-
-#include "nwk_const.h"
-#include "nwk_enumerations.h"
-#include "nwk_func.h"
-#include "UserButton.h"
-
-
-
-configuration Test_APL {
-}
-implementation {
-
-  components MainC;
-  components LedsC;
-  components Test_APLM;
-    
-  Test_APLM.Boot -> MainC;
-    
-  components NWK;
-  
-  Test_APLM.Leds -> LedsC;
-  
-  
-       components new TimerMilliC() as T_init;
-       Test_APLM.T_init -> T_init;
-               
-       components new TimerMilliC() as T_test;
-       Test_APLM.T_test -> T_test;
-               
-       components new TimerMilliC() as T_schedule;
-       Test_APLM.T_schedule -> T_schedule;
-  
-  
-  //User Button
-  components UserButtonC;
-
-  Test_APLM.Get -> UserButtonC;
-  Test_APLM.Notify -> UserButtonC;
-  
-  
-  Test_APLM.NLDE_DATA ->NWK.NLDE_DATA;
-  
-  Test_APLM.NLME_NETWORK_DISCOVERY -> NWK.NLME_NETWORK_DISCOVERY;
-  Test_APLM.NLME_NETWORK_FORMATION -> NWK.NLME_NETWORK_FORMATION;
-  /*Test_APLM.NLME_PERMIT_JOINING-> NWK_control.NLME_PERMIT_JOINING;
-  */
-  Test_APLM.NLME_START_ROUTER -> NWK.NLME_START_ROUTER;
-  Test_APLM.NLME_JOIN -> NWK.NLME_JOIN;
-  /*Test_APLM.NLME_DIRECT_JOIN -> NWK_control.NLME_DIRECT_JOIN;
-  */
-  Test_APLM.NLME_LEAVE -> NWK.NLME_LEAVE;
-  /*Test_APLM.NLME_RESET -> NWK_control.NLME_RESET;
-  */
-  Test_APLM.NLME_SYNC -> NWK.NLME_SYNC;
-  Test_APLM.NLME_GET -> NWK.NLME_GET;
-  Test_APLM.NLME_SET -> NWK.NLME_SET;
-}
-
diff --git a/tos/lib/net/zigbee/apps/Test_APL/Test_APLC.nc b/tos/lib/net/zigbee/apps/Test_APL/Test_APLC.nc
new file mode 100644 (file)
index 0000000..cf23013
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author Andre Cunha
+ *
+ */
+#include <Timer.h>
+
+#ifndef TKN154_MAC
+
+#include "phy_const.h"
+#include "phy_enumerations.h"
+#include "mac_const.h"
+#include "mac_enumerations.h"
+#include "mac_func.h"
+
+#endif
+
+#include "nwk_const.h"
+#include "nwk_enumerations.h"
+#include "nwk_func.h"
+#include "UserButton.h"
+
+
+
+configuration Test_APLC {
+}
+implementation {
+
+  components MainC;
+  components LedsC;
+  components Test_APLP;
+    
+  Test_APLP.Boot -> MainC;
+    
+  components NWKC;
+  
+  Test_APLP.Leds -> LedsC;
+  
+  
+       components new TimerMilliC() as T_init;
+       Test_APLP.T_init -> T_init;
+               
+       components new TimerMilliC() as T_test;
+       Test_APLP.T_test -> T_test;
+               
+       components new TimerMilliC() as T_schedule;
+       Test_APLP.T_schedule -> T_schedule;
+  
+  
+  //User Button
+  components UserButtonC;
+
+  Test_APLP.Get -> UserButtonC;
+  Test_APLP.Notify -> UserButtonC;
+  
+  
+  Test_APLP.NLDE_DATA ->NWKC.NLDE_DATA;
+  
+  Test_APLP.NLME_NETWORK_DISCOVERY -> NWKC.NLME_NETWORK_DISCOVERY;
+  Test_APLP.NLME_NETWORK_FORMATION -> NWKC.NLME_NETWORK_FORMATION;
+  /*Test_APLP.NLME_PERMIT_JOINING-> NWKC.NLME_PERMIT_JOINING;
+  */
+  Test_APLP.NLME_START_ROUTER -> NWKC.NLME_START_ROUTER;
+  Test_APLP.NLME_JOIN -> NWKC.NLME_JOIN;
+  /*Test_APLP.NLME_DIRECT_JOIN -> NWKC.NLME_DIRECT_JOIN;
+  */
+  Test_APLP.NLME_LEAVE -> NWKC.NLME_LEAVE;
+  Test_APLP.NLME_RESET -> NWKC.NLME_RESET;
+  
+  Test_APLP.NLME_SYNC -> NWKC.NLME_SYNC;
+  Test_APLP.NLME_GET -> NWKC.NLME_GET;
+  Test_APLP.NLME_SET -> NWKC.NLME_SET;
+}
+
diff --git a/tos/lib/net/zigbee/apps/Test_APL/Test_APLM.nc b/tos/lib/net/zigbee/apps/Test_APL/Test_APLM.nc
deleted file mode 100644 (file)
index c01bcaa..0000000
+++ /dev/null
@@ -1,884 +0,0 @@
-//#define OPEN_ZB_MAC
-
-/*
- * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
- * @author Andre Cunha
- *
- */
-#include <Timer.h>
-#include "printfUART.h"
-
-module Test_APLM {
-
-       uses interface Boot;
-       uses interface Leds;
-       
-       uses interface NLDE_DATA;
-//NLME NWK Management services
-       uses interface NLME_NETWORK_DISCOVERY;
-       uses interface NLME_NETWORK_FORMATION;  
-       uses interface NLME_START_ROUTER;
-       uses interface NLME_JOIN;
-       uses interface NLME_LEAVE;
-       uses interface NLME_SYNC;
-       
-       /*
-       uses interface NLME_PERMIT_JOINING;
-       uses interface NLME_DIRECT_JOIN;*/
-       uses interface NLME_RESET;
-       
-       
-       uses interface NLME_GET;
-       uses interface NLME_SET;
-                       
-       uses interface Timer<TMilli> as T_init;
-       
-       uses interface Timer<TMilli> as T_test;
-       
-       uses interface Timer<TMilli> as T_schedule;             
-
-//user button
-  uses interface Get<button_state_t>;
-  uses interface Notify<button_state_t>;       
-  
-}
-implementation {
-       
-       //descriptor of the Parent device
-       networkdescriptor PAN_network;
-       //boolean variable definig if the device has joined to the PAN
-       uint8_t joined =0x00;
-       //boolean variable defining if the device is waiting for the beacon request response
-       uint8_t requested_scheduling = 0x00;
-       //function used to start the beacon broadcast (router devices)
-       task void start_sending_beacons_request();
-       //function used to schedule the beacon requests (PAN coordinator)
-       void process_beacon_scheduling(uint16_t source_address,uint8_t beacon_order, uint8_t superframe_order);
-       
-       void process_beacon_scheduling(uint16_t source_address,uint8_t beacon_order, uint8_t superframe_order)
-       {
-       
-               uint8_t nsdu_pay[6];
-               
-               beacon_scheduling *beacon_scheduling_ptr;
-               
-               beacon_scheduling_ptr = (beacon_scheduling *)&nsdu_pay[0];
-               
-               switch(source_address)
-               {
-                       
-                       /*****************************************************************************/
-                       /*DEPTH 
-                       mwkMaxChildren (Cm)     6
-                       nwkMaxDepth (Lm)        4
-                       mwkMaxRouters (Rm)      4
-                       */
-                       /*
-                       case 0x0001: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0002: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0021: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x2C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0003: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0022: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0004: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0005: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       
-                       case 0x0023: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       
-                       case 0x0024: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                                               */                      
-                       /*****************************************************************************/
-                       /*NORMAL TEST
-                       mwkMaxChildren (Cm)     6
-                       nwkMaxDepth (Lm)        3
-                       mwkMaxRouters (Rm)      4
-*/
-                       case 0x0001: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0020: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xE0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0002: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0009: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0021: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0028: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0003: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0004: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x000a: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x000b: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0022: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0023: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0029: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x002a: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       
-                       /*******************************************************************/
-                       /*
-                       case 0x0001: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0002: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0009: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                                       
-                                       
-                       case 0x0020: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0021: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0028: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;          
-                       
-                       case 0x003F: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xE0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0040: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0047: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                                       
-                       case 0x005e: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x02;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xDC;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x005f: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0066: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;  
-                       */
-                       /*******************************************************************/
-                       /*
-                       case 0x0001: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0002: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0009: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x00010: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0017: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;          
-                       
-                       
-                       case 0x0020: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x68;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0021: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0028: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0002F: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0036: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;          
-                       
-                       
-                       case 0x003F: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x02;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xD0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0040: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0047: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0004E: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0055: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;  
-                                       
-                                       
-                                       
-                       case 0x005e: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x04;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x38;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x005F: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0066: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0006D: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-                       case 0x0074: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;  
-                       */
-                       default: 
-                                       beacon_scheduling_ptr->request_type = SCHEDULING_DENY;
-                                       beacon_scheduling_ptr->beacon_order = beacon_order;
-                                       beacon_scheduling_ptr->superframe_order = superframe_order;
-                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[1] = 0x00;
-                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-                                       break;
-               
-               }
-
-               
-               call NLDE_DATA.request(source_address,0x06, nsdu_pay, 1, 1, 0x00, 0);
-       return;
-       }
-
-       task void start_sending_beacons_request()
-       {
-               uint8_t nsdu_pay[6];
-               
-               beacon_scheduling *beacon_scheduling_ptr;
-               
-               beacon_scheduling_ptr = (beacon_scheduling *)&nsdu_pay[0];
-               
-               beacon_scheduling_ptr->request_type = SCHEDULING_REQUEST;
-               beacon_scheduling_ptr->beacon_order = BEACON_ORDER;
-               beacon_scheduling_ptr->superframe_order = SUPERFRAME_ORDER;
-               beacon_scheduling_ptr->transmission_offset[0] = 0x00;
-               beacon_scheduling_ptr->transmission_offset[1] = 0x00;
-               beacon_scheduling_ptr->transmission_offset[2] = 0x00;
-       
-               requested_scheduling = 0x01;
-
-               //command result_t NLDE_DATA.request(uint16_t DstAddr, uint8_t NsduLength, uint8_t Nsdu[], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, bool SecurityEnable)
-               call NLDE_DATA.request(0x0000,0x06, nsdu_pay, 0x01, 0x01, 0x00, 0x00);
-       
-               call T_schedule.startOneShot(20000);
-               //call Schedule_timer.start(TIMER_ONE_SHOT,20000);
-       return;
-       }
-       
-       
-       
-  event void Boot.booted() {
-       
-       call Notify.enable();   
-       
-       //using the telosb motes the used button enables the association to the network (routers and end devices)
-       //or the beacon broadcasting (PAN coordinator)
-       //in the MICAz motes a timer is needed to start all the above operations
-               
-       //if (TYPE_DEVICE == COORDINATOR)
-       //      call T_init.startOneShot(12000);
-               
-  }
-
-
-/*****************************************************
-****************TIMER EVENTS***************************
-******************************************************/ 
-
-
-/*******************T_init**************************/
-  event void T_init.fired() {
-    //printfUART("Timer fired\n", "");
-       if (TYPE_DEVICE == COORDINATOR)
-       {       
-               
-
-
-//printfUART("coordinator procedure\n", "");
-               //start forming a PAN
-               //command result_t request(uint32_t ScanChannels, uint8_t ScanDuration, uint8_t BeaconOrder, uint8_t SuperframeOrder, uint16_t PANId, bool BatteryLifeExtension);
-               call NLME_NETWORK_FORMATION.request(0x000000ff, 8, BEACON_ORDER, SUPERFRAME_ORDER, MAC_PANID,0);
-               
-               //call Leds.redOff();
-               //call Leds.led0Off();
-               //call Test_timer.start(TIMER_REPEAT, 8000); 
-       }
-       else
-       {       
-
-//printfUART("child procedure\n", "");
-       
-       
-               call NLME_NETWORK_DISCOVERY.request(0x000000ff, 8);
-       
-                       
-       }
-       return;
-  }
-
-/*******************T_test**************************/
-  event void T_test.fired() {
-    
-       uint8_t nsdu_pay[20];
-       //printfUART("Test_timer.fired\n", "");
-       nsdu_pay[0]=0x05;
-       nsdu_pay[1]=0x05;
-       nsdu_pay[2]=0x05;
-       nsdu_pay[3]=0x05;
-       nsdu_pay[4]=0x05;
-       nsdu_pay[5]=0x05;
-       nsdu_pay[6]=0x05;
-       
-       //call Leds.redToggle();
-       
-       //command result_t NLDE_DATA.request(uint16_t DstAddr, uint8_t NsduLength, uint8_t Nsdu[], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, bool SecurityEnable)
-       call NLDE_DATA.request(0x0000,0x07, nsdu_pay, 1, 1, 0x00, 0);
-  }
-  
-  /*******************T_schedule**************************/
-  event void T_schedule.fired() {
-       //event that fires if the negotiation for beacon transmission is unsuccessful 
-       //(the device does not receive any negotiation reply)
-       if(requested_scheduling == 0x01)
-       {
-               post start_sending_beacons_request();
-       }
-    
-  }
-
-
-/*****************************************************
-****************NLDE EVENTS***************************
-******************************************************/ 
-
-/*************************NLDE_DATA*****************************/
-
-event error_t NLDE_DATA.confirm(uint8_t NsduHandle, uint8_t Status)
-{
-       //printfUART("NLME_DATA.confirm\n", "");
-       
-       return SUCCESS;
-}
-
-event error_t NLDE_DATA.indication(uint16_t SrcAddress, uint16_t NsduLength,uint8_t Nsdu[100], uint16_t LinkQuality)
-{
-
-       uint32_t start_time=0x00000000;
-       uint16_t start_time1=0x0000;
-       uint16_t start_time2=0x0000;
-               
-       if (TYPE_DEVICE == COORDINATOR)
-       {       
-               if(Nsdu[0] == SCHEDULING_REQUEST)
-               {
-                       //the PAN coordinator receives a negotiation request
-                       process_beacon_scheduling(SrcAddress,Nsdu[1],Nsdu[2]);
-               }
-       }
-       
-       if(TYPE_DEVICE==ROUTER && requested_scheduling ==0x01)
-       {
-               //the routes receives a negotiation reply
-               atomic requested_scheduling =0x00;
-
-               if(Nsdu[0] == SCHEDULING_ACCEPT)
-               {
-                       start_time1 =( (Nsdu[3] << 0) ) ;
-                       start_time2 =( (Nsdu[4] << 8 ) | (Nsdu[5] << 0 ) );
-                       
-                       start_time = ( ((uint32_t)start_time1 << 16) | (start_time2 << 0));
-                       
-                       call NLME_START_ROUTER.request(Nsdu[1],Nsdu[2],0,start_time);
-               }
-
-       }
-       
-       return SUCCESS;
-}
-
-/*****************************************************
-****************NLME EVENTS***************************
-******************************************************/ 
-
-/*****************NLME_NETWORK_DISCOVERY**************************/
-event error_t NLME_NETWORK_DISCOVERY.confirm(uint8_t NetworkCount,networkdescriptor networkdescriptorlist[], uint8_t Status)
-{
-       //printfUART("NLME_NETWORK_DISCOVERY.confirm\n", ""); 
-
-       PAN_network = networkdescriptorlist[0];
-       
-       if (TYPE_DEVICE == ROUTER)
-       {
-               //printfUART("go join router\n", ""); 
-               call NLME_JOIN.request(networkdescriptorlist[0].PANId, 0x01, 0, 0x000000ff, 8, 0, 0, 0);
-       }
-       else
-       {
-               //printfUART("go join non router\n", ""); 
-               call NLME_JOIN.request(networkdescriptorlist[0].PANId, 0x00, 0, 0x000000ff, 8, 0, 0, 0);
-       }       
-       return SUCCESS;
-}
-
-/*****************NLME_NETWORK_FORMATION**********************/
-
-event error_t NLME_NETWORK_FORMATION.confirm(uint8_t Status)
-{      
-       
-
-//printfUART("NLME_NETWORK_FORMATION.confirm\n", ""); 
-
-       return SUCCESS;
-}
-/*****************NLME_START_ROUTER*****************************/
-event error_t NLME_START_ROUTER.confirm(uint8_t Status)
-{ 
-       //printfUART("NLME_START_ROUTER.confirm\n", ""); 
-               
-       return SUCCESS;
-}
-
-/*************************NLME_JOIN*****************************/
-
-event error_t NLME_JOIN.indication(uint16_t ShortAddress, uint32_t ExtendedAddress[], uint8_t CapabilityInformation, bool SecureJoin)
-{
-       //printfUART("NLME_JOIN.indication\n", "");
-       
-       return SUCCESS;
-}
-
-event error_t NLME_JOIN.confirm(uint16_t PANId, uint8_t Status)
-{      
-       //printfUART("NLME_JOIN.confirm\n", "");
-       
-       switch(Status)
-       {
-       
-               case NWK_SUCCESS:                       joined =0x01;
-                                                                       if (TYPE_DEVICE == ROUTER)
-                                                                       {
-                                                                               //join procedure successful
-                                                                               //call Leds.redOff();
-                                                                               call Leds.led0Off();
-                                                                               
-                                                                               requested_scheduling = 0x01;
-                                                                               
-                                                                               call T_schedule.startOneShot(9000);
-                                                                               
-
-                                                                               //call Test_timer.start(TIMER_REPEAT, 8000); 
-                                                                       }
-                                                                       else
-                                                                       {
-                                                                               //the device is an end device and starts transmitting data periodically
-                                                                               call T_test.startPeriodic(10000);
-                                                                               //call Test_timer.start(TIMER_REPEAT, 10000); 
-                                                                       }
-                                                                       break;
-                                                                       
-               case NWK_NOT_PERMITTED:         joined =0x00;
-                                                                       //join failed
-                                                                       break;
-                                                                       
-               default:                                        //default procedure - join failed
-                                                                       joined =0x00;
-                                                                       break;
-       }
-       return SUCCESS;
-}
-
-
-/*************************NLME_LEAVE****************************/
-
-event error_t NLME_LEAVE.indication(uint32_t DeviceAddress[])
-{
-       ////printfUART("NLME_LEAVE.indication\n", "");
-       return SUCCESS;
-}
-
-event error_t NLME_LEAVE.confirm(uint32_t DeviceAddress[], uint8_t Status)
-{
-       //printfUART("NLME_LEAVE.confirm\n", "");
-       return SUCCESS;
-}
-
-
-
-/*************************NLME_SYNC*****************************/
-
-event error_t NLME_SYNC.indication()
-{
-       //printfUART("NLME_SYNC.indication\n", "");
-       return SUCCESS;
-}
-
-event error_t NLME_SYNC.confirm(uint8_t Status)
-{
-       
-       return SUCCESS;
-}
-
-
-/*************************************************************/
-/*****************        NLME-SET     ********************/
-/*************************************************************/
-
-event error_t NLME_SET.confirm(uint8_t Status, uint8_t NIBAttribute)
-{
-
-return SUCCESS;
-}
-
-/*************************************************************/
-/*****************        NLME-GET     ********************/
-/*************************************************************/
-
-event error_t NLME_GET.confirm(uint8_t Status, uint8_t NIBAttribute, uint16_t NIBAttributeLength, uint16_t NIBAttributeValue)
-{
-
-
-return SUCCESS;
-}
-
-event error_t NLME_RESET.confirm(uint8_t status){
-
-call T_init.startOneShot(5000);
-return SUCCESS;
-}
-
-event void Notify.notify( button_state_t state)
-{
-       if (state == BUTTON_PRESSED) {
-               //call Leds.led0On();
-               
-               call NLME_RESET.request();
-
-       }
-    
-}
-
-
-  
-}
-
diff --git a/tos/lib/net/zigbee/apps/Test_APL/Test_APLP.nc b/tos/lib/net/zigbee/apps/Test_APL/Test_APLP.nc
new file mode 100644 (file)
index 0000000..801f7a3
--- /dev/null
@@ -0,0 +1,885 @@
+//#define OPEN_ZB_MAC
+
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author Andre Cunha
+ *
+ */
+#include <Timer.h>
+#include "printfUART.h"
+
+module Test_APLP {
+
+       uses interface Boot;
+       uses interface Leds;
+       
+       uses interface NLDE_DATA;
+//NLME NWK Management services
+       uses interface NLME_NETWORK_DISCOVERY;
+       uses interface NLME_NETWORK_FORMATION;  
+       uses interface NLME_START_ROUTER;
+       uses interface NLME_JOIN;
+       uses interface NLME_LEAVE;
+       uses interface NLME_SYNC;
+       
+       /*
+       uses interface NLME_PERMIT_JOINING;
+       uses interface NLME_DIRECT_JOIN;*/
+       uses interface NLME_RESET;
+       
+       
+       uses interface NLME_GET;
+       uses interface NLME_SET;
+                       
+       uses interface Timer<TMilli> as T_init;
+       
+       uses interface Timer<TMilli> as T_test;
+       
+       uses interface Timer<TMilli> as T_schedule;             
+
+//user button
+  uses interface Get<button_state_t>;
+  uses interface Notify<button_state_t>;       
+  
+}
+implementation {
+       
+       //descriptor of the Parent device
+       networkdescriptor PAN_network;
+       //boolean variable definig if the device has joined to the PAN
+       uint8_t joined =0x00;
+       //boolean variable defining if the device is waiting for the beacon request response
+       uint8_t requested_scheduling = 0x00;
+       //function used to start the beacon broadcast (router devices)
+       task void start_sending_beacons_request();
+       //function used to schedule the beacon requests (PAN coordinator)
+       void process_beacon_scheduling(uint16_t source_address,uint8_t beacon_order, uint8_t superframe_order);
+       
+       void process_beacon_scheduling(uint16_t source_address,uint8_t beacon_order, uint8_t superframe_order)
+       {
+       
+               uint8_t nsdu_pay[6];
+               
+               beacon_scheduling *beacon_scheduling_ptr;
+               
+               beacon_scheduling_ptr = (beacon_scheduling *)&nsdu_pay[0];
+               
+               switch(source_address)
+               {
+                       
+                       /*****************************************************************************/
+                       /*DEPTH 
+                       mwkMaxChildren (Cm)     6
+                       nwkMaxDepth (Lm)        4
+                       mwkMaxRouters (Rm)      4
+                       */
+                       /*
+                       case 0x0001: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0002: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0021: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x2C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0003: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0022: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0004: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0005: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       
+                       case 0x0023: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       
+                       case 0x0024: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                                               */                      
+                       /*****************************************************************************/
+                       /*NORMAL TEST
+                       mwkMaxChildren (Cm)     6
+                       nwkMaxDepth (Lm)        3
+                       mwkMaxRouters (Rm)      4
+*/
+                       case 0x0001: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0020: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xE0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0002: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0009: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0021: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0028: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0003: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0004: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x000a: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x000b: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0022: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0023: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0029: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x002a: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       
+                       /*******************************************************************/
+                       /*
+                       case 0x0001: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0002: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0009: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                                       
+                                       
+                       case 0x0020: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0021: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0028: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;          
+                       
+                       case 0x003F: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xE0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0040: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0047: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                                       
+                       case 0x005e: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x02;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xDC;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x005f: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0066: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;  
+                       */
+                       /*******************************************************************/
+                       /*
+                       case 0x0001: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0002: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0009: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x00010: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0017: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;          
+                       
+                       
+                       case 0x0020: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x01;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x68;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0021: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0028: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0002F: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0036: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;          
+                       
+                       
+                       case 0x003F: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x02;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xD0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0040: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0047: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0004E: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0055: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;  
+                                       
+                                       
+                                       
+                       case 0x005e: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x04;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x38;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x005F: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x3C;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0066: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x78;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0006D: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xB4;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+                       case 0x0074: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_ACCEPT;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0xF0;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;  
+                       */
+                       default: 
+                                       beacon_scheduling_ptr->request_type = SCHEDULING_DENY;
+                                       beacon_scheduling_ptr->beacon_order = beacon_order;
+                                       beacon_scheduling_ptr->superframe_order = superframe_order;
+                                       beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[1] = 0x00;
+                                       beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+                                       break;
+               
+               }
+
+               
+               call NLDE_DATA.request(source_address,0x06, nsdu_pay, 1, 1, 0x00, 0);
+       return;
+       }
+
+       task void start_sending_beacons_request()
+       {
+               uint8_t nsdu_pay[6];
+               
+               beacon_scheduling *beacon_scheduling_ptr;
+               
+               beacon_scheduling_ptr = (beacon_scheduling *)&nsdu_pay[0];
+               
+               beacon_scheduling_ptr->request_type = SCHEDULING_REQUEST;
+               beacon_scheduling_ptr->beacon_order = BEACON_ORDER;
+               beacon_scheduling_ptr->superframe_order = SUPERFRAME_ORDER;
+               beacon_scheduling_ptr->transmission_offset[0] = 0x00;
+               beacon_scheduling_ptr->transmission_offset[1] = 0x00;
+               beacon_scheduling_ptr->transmission_offset[2] = 0x00;
+       
+               requested_scheduling = 0x01;
+
+               //command result_t NLDE_DATA.request(uint16_t DstAddr, uint8_t NsduLength, uint8_t Nsdu[], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, bool SecurityEnable)
+               call NLDE_DATA.request(0x0000,0x06, nsdu_pay, 0x01, 0x01, 0x00, 0x00);
+       
+               call T_schedule.startOneShot(20000);
+               //call Schedule_timer.start(TIMER_ONE_SHOT,20000);
+       return;
+       }
+       
+       
+       
+  event void Boot.booted() {
+       
+       call Notify.enable();   
+       
+       //using the telosb motes the used button enables the association to the network (routers and end devices)
+       //or the beacon broadcasting (PAN coordinator)
+       //in the MICAz motes a timer is needed to start all the above operations
+               
+       //if (TYPE_DEVICE == COORDINATOR)
+       //      call T_init.startOneShot(12000);
+               
+  }
+
+
+/*****************************************************
+****************TIMER EVENTS***************************
+******************************************************/ 
+
+
+/*******************T_init**************************/
+  event void T_init.fired() {
+    //printfUART("Timer fired\n", "");
+       if (TYPE_DEVICE == COORDINATOR)
+       {       
+               
+
+
+//printfUART("coordinator procedure\n", "");
+               //start forming a PAN
+               //command result_t request(uint32_t ScanChannels, uint8_t ScanDuration, uint8_t BeaconOrder, uint8_t SuperframeOrder, uint16_t PANId, bool BatteryLifeExtension);
+               call NLME_NETWORK_FORMATION.request(0x000000ff, 8, BEACON_ORDER, SUPERFRAME_ORDER, MAC_PANID,0);
+               
+               //call Leds.redOff();
+               //call Leds.led0Off();
+               //call Test_timer.start(TIMER_REPEAT, 8000); 
+       }
+       else
+       {       
+
+//printfUART("child procedure\n", "");
+       
+       
+               call NLME_NETWORK_DISCOVERY.request(0x000000ff, 8);
+       
+                       
+       }
+       return;
+  }
+
+/*******************T_test**************************/
+  event void T_test.fired() {
+    
+       uint8_t nsdu_pay[20];
+       //printfUART("Test_timer.fired\n", "");
+       nsdu_pay[0]=0x05;
+       nsdu_pay[1]=0x05;
+       nsdu_pay[2]=0x05;
+       nsdu_pay[3]=0x05;
+       nsdu_pay[4]=0x05;
+       nsdu_pay[5]=0x05;
+       nsdu_pay[6]=0x05;
+       
+       //call Leds.redToggle();
+       
+       //command result_t NLDE_DATA.request(uint16_t DstAddr, uint8_t NsduLength, uint8_t Nsdu[], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, bool SecurityEnable)
+       call NLDE_DATA.request(0x0000,0x07, nsdu_pay, 1, 1, 0x00, 0);
+  }
+  
+  /*******************T_schedule**************************/
+  event void T_schedule.fired() {
+       //event that fires if the negotiation for beacon transmission is unsuccessful 
+       //(the device does not receive any negotiation reply)
+       if(requested_scheduling == 0x01)
+       {
+               post start_sending_beacons_request();
+       }
+    
+  }
+
+
+/*****************************************************
+****************NLDE EVENTS***************************
+******************************************************/ 
+
+/*************************NLDE_DATA*****************************/
+
+event error_t NLDE_DATA.confirm(uint8_t NsduHandle, uint8_t Status)
+{
+       //printfUART("NLME_DATA.confirm\n", "");
+       
+       return SUCCESS;
+}
+
+event error_t NLDE_DATA.indication(uint16_t SrcAddress, uint16_t NsduLength,uint8_t Nsdu[100], uint16_t LinkQuality)
+{
+
+       uint32_t start_time=0x00000000;
+       uint16_t start_time1=0x0000;
+       uint16_t start_time2=0x0000;
+               
+       if (TYPE_DEVICE == COORDINATOR)
+       {       
+               if(Nsdu[0] == SCHEDULING_REQUEST)
+               {
+                       //the PAN coordinator receives a negotiation request
+                       process_beacon_scheduling(SrcAddress,Nsdu[1],Nsdu[2]);
+               }
+       }
+       
+       if(TYPE_DEVICE==ROUTER && requested_scheduling ==0x01)
+       {
+               //the routes receives a negotiation reply
+               atomic requested_scheduling =0x00;
+
+               if(Nsdu[0] == SCHEDULING_ACCEPT)
+               {
+                       start_time1 =( (Nsdu[3] << 0) ) ;
+                       start_time2 =( (Nsdu[4] << 8 ) | (Nsdu[5] << 0 ) );
+                       
+                       start_time = ( ((uint32_t)start_time1 << 16) | (start_time2 << 0));
+                       
+                       call NLME_START_ROUTER.request(Nsdu[1],Nsdu[2],0,start_time);
+               }
+
+       }
+       
+       return SUCCESS;
+}
+
+/*****************************************************
+****************NLME EVENTS***************************
+******************************************************/ 
+
+/*****************NLME_NETWORK_DISCOVERY**************************/
+event error_t NLME_NETWORK_DISCOVERY.confirm(uint8_t NetworkCount,networkdescriptor networkdescriptorlist[], uint8_t Status)
+{
+       //printfUART("NLME_NETWORK_DISCOVERY.confirm\n", ""); 
+
+       PAN_network = networkdescriptorlist[0];
+       
+       if (TYPE_DEVICE == ROUTER)
+       {
+               //printfUART("go join router\n", ""); 
+               call NLME_JOIN.request(networkdescriptorlist[0].PANId, 0x01, 0, 0x000000ff, 8, 0, 0, 0);
+       }
+       else
+       {
+               //printfUART("go join non router\n", ""); 
+               call NLME_SYNC.request(1);
+               call NLME_JOIN.request(networkdescriptorlist[0].PANId, 0x00, 0, 0x000000ff, 8, 0, 0, 0);
+       }       
+       return SUCCESS;
+}
+
+/*****************NLME_NETWORK_FORMATION**********************/
+
+event error_t NLME_NETWORK_FORMATION.confirm(uint8_t Status)
+{      
+       
+
+//printfUART("NLME_NETWORK_FORMATION.confirm\n", ""); 
+
+       return SUCCESS;
+}
+/*****************NLME_START_ROUTER*****************************/
+event error_t NLME_START_ROUTER.confirm(uint8_t Status)
+{ 
+       //printfUART("NLME_START_ROUTER.confirm\n", ""); 
+               
+       return SUCCESS;
+}
+
+/*************************NLME_JOIN*****************************/
+
+event error_t NLME_JOIN.indication(uint16_t ShortAddress, uint32_t ExtendedAddress[], uint8_t CapabilityInformation, bool SecureJoin)
+{
+       //printfUART("NLME_JOIN.indication\n", "");
+       
+       return SUCCESS;
+}
+
+event error_t NLME_JOIN.confirm(uint16_t PANId, uint8_t Status)
+{      
+       //printfUART("NLME_JOIN.confirm\n", "");
+       
+       switch(Status)
+       {
+       
+               case NWK_SUCCESS:                       joined =0x01;
+                                                                       if (TYPE_DEVICE == ROUTER)
+                                                                       {
+                                                                               //join procedure successful
+                                                                               //call Leds.redOff();
+                                                                               call Leds.led0Off();
+                                                                               
+                                                                               requested_scheduling = 0x01;
+                                                                               
+                                                                               call T_schedule.startOneShot(9000);
+                                                                               
+
+                                                                               //call Test_timer.start(TIMER_REPEAT, 8000); 
+                                                                       }
+                                                                       else
+                                                                       {
+                                                                               //the device is an end device and starts transmitting data periodically
+                                                                               call T_test.startPeriodic(10000);
+                                                                               //call Test_timer.start(TIMER_REPEAT, 10000); 
+                                                                       }
+                                                                       break;
+                                                                       
+               case NWK_NOT_PERMITTED:         joined =0x00;
+                                                                       //join failed
+                                                                       break;
+                                                                       
+               default:                                        //default procedure - join failed
+                                                                       joined =0x00;
+                                                                       break;
+       }
+       return SUCCESS;
+}
+
+
+/*************************NLME_LEAVE****************************/
+
+event error_t NLME_LEAVE.indication(uint32_t DeviceAddress[])
+{
+       ////printfUART("NLME_LEAVE.indication\n", "");
+       return SUCCESS;
+}
+
+event error_t NLME_LEAVE.confirm(uint32_t DeviceAddress[], uint8_t Status)
+{
+       //printfUART("NLME_LEAVE.confirm\n", "");
+       return SUCCESS;
+}
+
+
+
+/*************************NLME_SYNC*****************************/
+
+event error_t NLME_SYNC.indication()
+{
+       //printfUART("NLME_SYNC.indication\n", "");
+       return SUCCESS;
+}
+
+event error_t NLME_SYNC.confirm(uint8_t Status)
+{
+       
+       return SUCCESS;
+}
+
+
+/*************************************************************/
+/*****************        NLME-SET     ********************/
+/*************************************************************/
+
+event error_t NLME_SET.confirm(uint8_t Status, uint8_t NIBAttribute)
+{
+
+return SUCCESS;
+}
+
+/*************************************************************/
+/*****************        NLME-GET     ********************/
+/*************************************************************/
+
+event error_t NLME_GET.confirm(uint8_t Status, uint8_t NIBAttribute, uint16_t NIBAttributeLength, uint16_t NIBAttributeValue)
+{
+
+
+return SUCCESS;
+}
+
+event error_t NLME_RESET.confirm(uint8_t status){
+
+call T_init.startOneShot(5000);
+return SUCCESS;
+}
+
+event void Notify.notify( button_state_t state)
+{
+       if (state == BUTTON_PRESSED) {
+               //call Leds.led0On();
+               
+               call NLME_RESET.request();
+
+       }
+    
+}
+
+
+  
+}
+
index 55edac20ee0ed5c45973a6071c642cec86f0aa1b..d06d4ae4d73d99d3334bf1e53c74cf39014b224a 100644 (file)
@@ -1,4 +1,4 @@
-COMPONENT=NWK
+COMPONENT=NWKC
 
 PFLAGS +=      -I$(TOSROOT)/tos/ieee802154/includes \
                        -I$(TOSROOT)/tos/ieee802154/mac \
diff --git a/tos/lib/net/zigbee/ieee802154/nwk/NWK.nc b/tos/lib/net/zigbee/ieee802154/nwk/NWK.nc
deleted file mode 100644 (file)
index a235325..0000000
+++ /dev/null
@@ -1,141 +0,0 @@
-/*
- * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
- * @author Andre Cunha
- *
- */
-#include <Timer.h>
-
-#ifndef TKN154_MAC
-#endif
-#include "phy_const.h"
-#include "phy_enumerations.h"
-#include "mac_const.h"
-
-
-
-#include "mac_enumerations.h"
-#include "mac_func.h"
-#include "nwk_func.h"
-#include "nwk_enumerations.h"
-#include "nwk_const.h"
-
-
-configuration NWK {
-
-       //provides
-       
-       //NLDE NWK data service  
-       
-       provides interface NLDE_DATA;
-       
-       
-       //NLME NWK Management service
-       
-       provides interface NLME_NETWORK_FORMATION;
-       provides interface NLME_NETWORK_DISCOVERY;
-       provides interface NLME_START_ROUTER;
-       provides interface NLME_JOIN;
-       provides interface NLME_LEAVE;
-       
-       /*     
-       provides interface NLME_PERMIT_JOINING;
-       provides interface NLME_DIRECT_JOIN;            
-       provides interface NLME_RESET;
-       */
-       provides interface NLME_SYNC;
-       
-       provides interface NLME_GET;
-       provides interface NLME_SET;
-
-}
-implementation {
-
-  components MainC;
-  MainC.SoftwareInit -> NWKM;
-  
-  components LedsC;
-  components NWKM;
-       
-
-
-
-  NWKM.Leds -> LedsC;
-   
-   
-       components RandomC;
-       NWKM.Random -> RandomC;
-
-
-  //MAC interfaces
-#ifndef TKN154_MAC
-
-  components Mac;
-
-  NWKM.MLME_START -> Mac.MLME_START;
-  
-  NWKM.MLME_GET ->Mac.MLME_GET;
-  NWKM.MLME_SET ->Mac.MLME_SET;
-  
-  NWKM.MLME_BEACON_NOTIFY ->Mac.MLME_BEACON_NOTIFY;
-  NWKM.MLME_GTS -> Mac.MLME_GTS;
-  
-  NWKM.MLME_ASSOCIATE->Mac.MLME_ASSOCIATE;
-  NWKM.MLME_DISASSOCIATE->Mac.MLME_DISASSOCIATE;
-  
-  NWKM.MLME_ORPHAN->Mac.MLME_ORPHAN;
-  NWKM.MLME_SYNC->Mac.MLME_SYNC;
-  NWKM.MLME_SYNC_LOSS->Mac.MLME_SYNC_LOSS;
-  NWKM.MLME_RESET->Mac.MLME_RESET;
-  NWKM.MLME_SCAN->Mac.MLME_SCAN;
-  
-  NWKM.MCPS_DATA->Mac.MCPS_DATA;
-#else
-
-
-  components WrapperC;
-  NWKM.MLME_RESET->WrapperC.OPENZB_MLME_RESET;
-  NWKM.MLME_START -> WrapperC.OPENZB_MLME_START;
-  
-  NWKM.MLME_GET ->WrapperC.OPENZB_MLME_GET;
-  NWKM.MLME_SET ->WrapperC.OPENZB_MLME_SET;
-  
-  NWKM.MLME_BEACON_NOTIFY ->WrapperC.OPENZB_MLME_BEACON_NOTIFY;
-  NWKM.MLME_GTS -> WrapperC.OPENZB_MLME_GTS;
-  
-  NWKM.MLME_ASSOCIATE->WrapperC.OPENZB_MLME_ASSOCIATE;
-  NWKM.MLME_DISASSOCIATE->WrapperC.OPENZB_MLME_DISASSOCIATE;
-  
-  NWKM.MLME_ORPHAN->WrapperC.OPENZB_MLME_ORPHAN;
-  NWKM.MLME_SYNC->WrapperC.OPENZB_MLME_SYNC;
-  NWKM.MLME_SYNC_LOSS->WrapperC.OPENZB_MLME_SYNC_LOSS;
-  NWKM.MLME_SCAN->WrapperC.OPENZB_MLME_SCAN;
-  
-  NWKM.MCPS_DATA->WrapperC.OPENZB_MCPS_DATA;
-#endif
-
-///////////////
-       
-       //NLDE NWK data service  
-       NLDE_DATA=NWKM;
-       
-       //NLME NWK Management service
-       NLME_NETWORK_FORMATION=NWKM;
-       NLME_NETWORK_DISCOVERY=NWKM;
-       
-       NLME_START_ROUTER=NWKM;
-       
-       NLME_JOIN=NWKM;
-       NLME_LEAVE=NWKM;
-       
-       /*
-       NLME_PERMIT_JOINING=NWKM;
-       NLME_DIRECT_JOIN=NWKM;
-       NLME_RESET=NWKM;
-       */
-       NLME_SYNC=NWKM;
-       NLME_GET=NWKM;
-       NLME_SET=NWKM;
-         
-         
-}
diff --git a/tos/lib/net/zigbee/ieee802154/nwk/NWKC.nc b/tos/lib/net/zigbee/ieee802154/nwk/NWKC.nc
new file mode 100644 (file)
index 0000000..a3b313c
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author Andre Cunha
+ *
+ */
+#include <Timer.h>
+
+#ifndef TKN154_MAC
+#endif
+#include "phy_const.h"
+#include "phy_enumerations.h"
+#include "mac_const.h"
+
+
+
+#include "mac_enumerations.h"
+#include "mac_func.h"
+#include "nwk_func.h"
+#include "nwk_enumerations.h"
+#include "nwk_const.h"
+
+
+configuration NWKC {
+
+       //provides
+       
+       //NLDE NWK data service  
+       
+       provides interface NLDE_DATA;
+       
+       
+       //NLME NWK Management service
+       
+       provides interface NLME_NETWORK_FORMATION;
+       provides interface NLME_NETWORK_DISCOVERY;
+       provides interface NLME_START_ROUTER;
+       provides interface NLME_JOIN;
+       provides interface NLME_LEAVE;
+       
+       /*     
+       provides interface NLME_PERMIT_JOINING;
+       provides interface NLME_DIRECT_JOIN;    */
+       provides interface NLME_RESET;
+       
+       provides interface NLME_SYNC;
+       
+       provides interface NLME_GET;
+       provides interface NLME_SET;
+
+}
+implementation {
+
+  components MainC;
+  MainC.SoftwareInit -> NWKP;
+  
+  components LedsC;
+  components NWKP;
+       
+
+
+
+  NWKP.Leds -> LedsC;
+   
+   
+       components RandomC;
+       NWKP.Random -> RandomC;
+
+
+  //MAC interfaces
+#ifndef TKN154_MAC
+
+  components Mac;
+
+  NWKP.MLME_START -> Mac.MLME_START;
+  
+  NWKP.MLME_GET ->Mac.MLME_GET;
+  NWKP.MLME_SET ->Mac.MLME_SET;
+  
+  NWKP.MLME_BEACON_NOTIFY ->Mac.MLME_BEACON_NOTIFY;
+  NWKP.MLME_GTS -> Mac.MLME_GTS;
+  
+  NWKP.MLME_ASSOCIATE->Mac.MLME_ASSOCIATE;
+  NWKP.MLME_DISASSOCIATE->Mac.MLME_DISASSOCIATE;
+  
+  NWKP.MLME_ORPHAN->Mac.MLME_ORPHAN;
+  NWKP.MLME_SYNC->Mac.MLME_SYNC;
+  NWKP.MLME_SYNC_LOSS->Mac.MLME_SYNC_LOSS;
+  NWKP.MLME_RESET->Mac.MLME_RESET;
+  NWKP.MLME_SCAN->Mac.MLME_SCAN;
+  
+  NWKP.MCPS_DATA->Mac.MCPS_DATA;
+#else
+
+
+  components WrapperC;
+  NWKP.MLME_RESET->WrapperC.OPENZB_MLME_RESET;
+  NWKP.MLME_START -> WrapperC.OPENZB_MLME_START;
+  
+  NWKP.MLME_GET ->WrapperC.OPENZB_MLME_GET;
+  NWKP.MLME_SET ->WrapperC.OPENZB_MLME_SET;
+  
+  NWKP.MLME_BEACON_NOTIFY ->WrapperC.OPENZB_MLME_BEACON_NOTIFY;
+  NWKP.MLME_GTS -> WrapperC.OPENZB_MLME_GTS;
+  
+  NWKP.MLME_ASSOCIATE->WrapperC.OPENZB_MLME_ASSOCIATE;
+  NWKP.MLME_DISASSOCIATE->WrapperC.OPENZB_MLME_DISASSOCIATE;
+  
+  NWKP.MLME_ORPHAN->WrapperC.OPENZB_MLME_ORPHAN;
+  NWKP.MLME_SYNC->WrapperC.OPENZB_MLME_SYNC;
+  NWKP.MLME_SYNC_LOSS->WrapperC.OPENZB_MLME_SYNC_LOSS;
+  NWKP.MLME_SCAN->WrapperC.OPENZB_MLME_SCAN;
+  
+  NWKP.MCPS_DATA->WrapperC.OPENZB_MCPS_DATA;
+#endif
+
+///////////////
+       
+       //NLDE NWK data service  
+       NLDE_DATA=NWKP;
+       
+       //NLME NWK Management service
+       NLME_NETWORK_FORMATION=NWKP;
+       NLME_NETWORK_DISCOVERY=NWKP;
+       
+       NLME_START_ROUTER=NWKP;
+       
+       NLME_JOIN=NWKP;
+       NLME_LEAVE=NWKP;
+       
+       /*
+       NLME_PERMIT_JOINING=NWKP;
+       NLME_DIRECT_JOIN=NWKP;*/
+       NLME_RESET=NWKP;
+       
+       NLME_SYNC=NWKP;
+       NLME_GET=NWKP;
+       NLME_SET=NWKP;
+         
+         
+}
diff --git a/tos/lib/net/zigbee/ieee802154/nwk/NWKM.nc b/tos/lib/net/zigbee/ieee802154/nwk/NWKM.nc
deleted file mode 100644 (file)
index 2a9c49b..0000000
+++ /dev/null
@@ -1,1539 +0,0 @@
-/*
- * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
- * @author Andre Cunha
- *
- */
-#include <Timer.h>
-#include "printfUART.h"
-
-
-
-
-module NWKM {
-       
-
-       uses interface Leds;
-       
-       //MAC interfaces
-#ifndef TKN154_MAC
-       uses interface MLME_START;
-       
-       uses interface MLME_GET;
-       uses interface MLME_SET;
-       
-       uses interface MLME_BEACON_NOTIFY;
-       uses interface MLME_GTS;
-       
-       uses interface MLME_ASSOCIATE;
-       uses interface MLME_DISASSOCIATE;
-       
-       uses interface MLME_ORPHAN;
-       
-       uses interface MLME_SYNC;
-       uses interface MLME_SYNC_LOSS;
-       
-       uses interface MLME_RESET;
-       
-       uses interface MLME_SCAN;
-       
-       
-       uses interface MCPS_DATA;
-
-#else
-       uses interface OPENZB_MLME_START as MLME_START;
-       
-       uses interface OPENZB_MLME_GET as MLME_GET;
-       uses interface OPENZB_MLME_SET as MLME_SET;
-       
-       uses interface OPENZB_MLME_BEACON_NOTIFY as MLME_BEACON_NOTIFY;
-       uses interface OPENZB_MLME_GTS as MLME_GTS;
-       
-       uses interface OPENZB_MLME_ASSOCIATE as MLME_ASSOCIATE;
-       uses interface OPENZB_MLME_DISASSOCIATE as MLME_DISASSOCIATE;
-       
-       uses interface OPENZB_MLME_ORPHAN as MLME_ORPHAN;
-       
-       uses interface OPENZB_MLME_SYNC as MLME_SYNC;
-       uses interface OPENZB_MLME_SYNC_LOSS as MLME_SYNC_LOSS;
-       
-       uses interface OPENZB_MLME_RESET as MLME_RESET;
-       
-       uses interface OPENZB_MLME_SCAN as MLME_SCAN;
-       
-       
-       uses interface OPENZB_MCPS_DATA as MCPS_DATA;
-
-
-#endif
-
-
-       uses interface Random;
-
-//provides
-
-       provides interface Init;
-       provides interface NLDE_DATA;
-
-       //NLME NWK Management services
-    provides interface NLME_NETWORK_FORMATION;
-       provides interface NLME_NETWORK_DISCOVERY;
-       provides interface NLME_START_ROUTER;
-       provides interface NLME_JOIN;
-       provides interface NLME_LEAVE;
-       provides interface NLME_SYNC;
-       /*  
-       provides interface NLME_PERMIT_JOINING;
-       provides interface NLME_DIRECT_JOIN;
-       provides interface NLME_RESET;
-*/     
-       provides interface NLME_GET;
-       provides interface NLME_SET;
-
-  
-}
-implementation {
-
-
-       nwkIB nwk_IB;
-       
-       uint8_t device_type = END_DEVICE;
-       
-/*****************************************************/
-/*************Neighbourtable Variables****************/
-/*****************************************************/ 
-
-       //neighbour table array:
-       neighbortableentry neighbortable[NEIGHBOUR_TABLE_SIZE];
-       //number of neigbourtable entries:
-       uint8_t neighbour_count;
-       //the index of the parents neighbortable entry
-       uint8_t parent;
-       
-
-/*****************************************************/
-/****************ASSOCIATION Variables********************/
-/*****************************************************/ 
-
-       //CURRENT NETWORK ADDRESS
-       uint16_t networkaddress=0x0000;
-
-       //COORDINATOR
-       //address assignement variables
-       uint8_t depth=0;
-       uint8_t cskip=0;
-       
-       uint8_t cskip_routing=0;
-
-       //neighbour table parent index
-       uint8_t parent_index;
-
-       //NON COORDINATOR
-
-       //current pan characteristics
-       uint16_t panid;
-       uint8_t beaconorder;
-       uint8_t superframeorder;
-       
-       //next child router address
-       uint16_t next_child_router_address;
-       uint8_t number_child_router=0x01;
-       uint8_t number_child_end_devices=0x01;
-/*****************************************************/
-/****************Integer Variables********************/
-/*****************************************************/ 
-
-       uint8_t joined=0;
-       uint8_t sync_loss=0;
-       //uint8_t synchronizing=0;
-       uint8_t syncwait;
-       
-       
-       //USED AFTER DE SCAN //GIVE SOME TIME TO THE DEVICE TO SYNC WITH THE PARENT
-       uint8_t received_beacon_count=0;
-       
-       uint8_t go_associate =0;
-       
-       PANDescriptor pan_des;
-       uint32_t coordinator_addr[2];
-/******************************************************/
-/*********NEIGHBOuRTABLE  MANAGEMENT FUNCTIONS*********/
-/******************************************************/ 
-
-       void init_nwkIB();
-       
-       uint8_t check_neighbortableentry(uint8_t addrmode,uint32_t Extended_Address0,uint32_t Extended_Address1);
-       
-       void add_neighbortableentry     (uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship);
-       void update_neighbortableentry (uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship);       
-       
-       uint8_t find_suitable_parent();
-       
-       uint16_t Cskip(uint8_t d);
-       
-       uint16_t nexthopaddress(uint16_t destinationaddress,uint8_t d);
-       
-       void list_neighbourtable();
-
-
-
- command error_t Init.init() {
-       
-       printfUART_init();//make the possibility to print
-               
-       init_nwkIB();
-       
-       nwk_IB.nwkSequenceNumber=call Random.rand16();
-  
-       return SUCCESS;
- }
-
-
-/*****************************************************************************************************/  
-/**************************************MLME-SCAN*******************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_SCAN.confirm(uint8_t status,uint8_t ScanType, uint32_t UnscannedChannels, uint8_t ResultListSize, uint8_t EnergyDetectList[], SCAN_PANDescriptor PANDescriptorList[])
-{
-//FAULT-TOLERANCE
-
-//the device channel scan ends with a scan confirm containing a list of the PANs (beacons received during the scan) 
-
-       int i;
-       uint8_t max_lqi=0;
-       uint8_t best_pan_index=0;
-       
-       
-       networkdescriptor networkdescriptorlist[1];
-       
-       
-       //call Leds.redOff();
-       
-       printfUART("4 rec scan\n", "");
-       
-       //printfUART("MLME_SCAN.confirm %i\n", ScanType);
-       
-       if (ScanType == ORPHAN_SCAN)
-       {
-               printfUART("new scan \n", "");
-       
-               call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
-               return SUCCESS;
-       }
-       
-       
-       
-       if(ScanType == ED_SCAN)
-       {
-               for(i=0;i<ResultListSize;i++)
-               {
-                       printfUART("ED SCAN %i %i\n", (0x0A + i),EnergyDetectList[i]);
-               }
-               return SUCCESS;
-       }
-       
-       for (i=0; i<ResultListSize;i++)
-       {               /*
-                       printfUART("cord id %i", PANDescriptorList[i].CoordPANId);
-                       printfUART("CoordAddress %i", PANDescriptorList[i].CoordAddress);
-                       printfUART("LogicalChannel %i", PANDescriptorList[i].LogicalChannel);
-                       printfUART("SuperframeSpec %i", PANDescriptorList[i].SuperframeSpec);
-                       printfUART("lqi %i\n", PANDescriptorList[i].lqi);
-                       */
-               if(max_lqi < PANDescriptorList[i].lqi)
-               {
-                       max_lqi =PANDescriptorList[i].lqi;
-                       best_pan_index = i;
-               }
-       }
-       
-       printfUART("SELECTED cord id %i", PANDescriptorList[best_pan_index].CoordPANId);
-       printfUART("CoordAddress %i", PANDescriptorList[best_pan_index].CoordAddress);
-       printfUART("LogicalChannel %i", PANDescriptorList[best_pan_index].LogicalChannel);
-       printfUART("SuperframeSpec %i", PANDescriptorList[best_pan_index].SuperframeSpec);
-       printfUART("lqi %i\n", PANDescriptorList[best_pan_index].lqi);
-       
-       
-       
-       /*
-       
-       coordinator_addr[0] = 0x00000001;
-       
-       coordinator_addr[1] = (uint32_t)PANDescriptorList[best_pan_index].CoordAddress;
-       
-       //pan_des = PANDescriptorList[best_pan_index];
-
-       
-       //BUILD the PAN descriptor of the COORDINATOR
-       //assuming that the adress is short
-       pan_des.CoordAddrMode = SHORT_ADDRESS;
-       pan_des.CoordPANId = PANDescriptorList[best_pan_index].CoordAddress;
-       pan_des.CoordAddress0=0x00000000;
-       pan_des.CoordAddress1=0x00000000;
-       pan_des.LogicalChannel=PANDescriptorList[best_pan_index].LogicalChannel;
-       //superframe specification field
-       pan_des.SuperframeSpec = PANDescriptorList[best_pan_index].SuperframeSpec;
-       
-       pan_des.GTSPermit=0x01;
-       pan_des.LinkQuality=0x00;
-       pan_des.TimeStamp=0x000000;
-       pan_des.SecurityUse=0;
-       pan_des.ACLEntry=0x00;
-       pan_des.SecurityFailure=0x00;
-       
-       */
-
-       
-       
-       printfUART("5 en sync %x \n", PANDescriptorList[best_pan_index].LogicalChannel);
-       // the sync enables the TimerAsync events, in order to enable the synchronization with the PAN coordinator
-       call MLME_SYNC.request(PANDescriptorList[best_pan_index].LogicalChannel,0);
-       
-       
-       
-       //The networkdescriptorlist must contain information about every network that was heard
-       
-       //make NetworkDescriptorList out of the PanDescriptorList
-
-
-    printfUART("6 add neigh\n", "");
-
-       networkdescriptorlist[0].PANId=PANDescriptorList[best_pan_index].CoordPANId;
-       networkdescriptorlist[0].LogicalChannel=LOGICAL_CHANNEL;
-       networkdescriptorlist[0].StackProfile=0x00;
-       networkdescriptorlist[0].ZigBeeVersion=0x01;
-       networkdescriptorlist[0].BeaconOrder=7;
-       networkdescriptorlist[0].SuperframeOrder=6;
-       networkdescriptorlist[0].PermitJoining=1;
-
-       add_neighbortableentry(networkdescriptorlist[0].PANId,0x00000000,0x00000000,PANDescriptorList[best_pan_index].CoordAddress,COORDINATOR,NEIGHBOR_IS_PARENT);
-       
-       signal NLME_NETWORK_DISCOVERY.confirm(1,networkdescriptorlist, NWK_SUCCESS);
-       
-       
-       
-       return SUCCESS;
-}
-
-/*****************************************************************************************************/  
-/**************************************MLME-ORPHAN****************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_ORPHAN.indication(uint32_t OrphanAddress[1], uint8_t SecurityUse, uint8_t ACLEntry)
-{
-
-       
-               
-               return 0x0000;
-       }
-
-/*****************************************************************************************************/  
-/**************************************MLME-RESET*****************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_RESET.confirm(uint8_t status)
-{
-
-
-
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/**************************************MLME-SYNC-LOSS*************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_SYNC_LOSS.indication(uint8_t LossReason)
-{
-       ////printfUART("MLME_SYNC_LOSS.indication\n", ""); 
-       sync_loss = 1;
-       syncwait=1;
-       //signal NLME_SYNC.indication();
-/*
-
-printfUART("SL\n","");
-               
-               call MLME_SCAN.request(ORPHAN_SCAN,0xFFFFFFFF,7);
-*/
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/**************************************MLME-GTS*******************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_GTS.confirm(uint8_t GTSCharacteristics, uint8_t status)
-{
-
-
-
-       return SUCCESS;
-}
-
-event error_t MLME_GTS.indication(uint16_t DevAddress, uint8_t GTSCharacteristics, bool SecurityUse, uint8_t ACLEntry)
-{
-
-
-
-       return SUCCESS;
-}
-
-/*****************************************************************************************************/  
-/**************************************MLME-BEACON NOTIFY*********************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_BEACON_NOTIFY.indication(uint8_t BSN,PANDescriptor pan_descriptor, uint8_t PenAddrSpec, uint8_t AddrList, uint8_t sduLength, uint8_t sdu[])
-{
-       
-       uint32_t destinaddress[2];
-       printfUART("BN\n", "");
-
-       
-       if (go_associate==1)
-       {
-               received_beacon_count++;
-               
-               printfUART("bn %i\n", received_beacon_count);
-               
-               if (received_beacon_count==5)
-               {
-                               printfUART("sa \n", "");
-                               
-                               go_associate=0;
-                               //call MLME_ASSOCIATE.request(pan_des.LogicalChannel,SHORT_ADDRESS,pan_des.CoordPANId,coordinator_addr,0x00,0x00);
-               
-               destinaddress[0]=0x00000000;
-               destinaddress[1]=0x00000000;
-               
-               
-               call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,0x1234,destinaddress, set_capability_information(0x00,0x00,0x00,0x00,0x00,0x01),0);
-               
-               }
-               
-       }
-
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/**************************************MLME-START*****************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_START.confirm(uint8_t status)
-{
-               ////printfUART("MLME_START.confirm\n", "");
-       if (device_type==COORDINATOR)
-       {
-               signal NLME_NETWORK_FORMATION.confirm(status);
-               joined=1;
-       }
-       else
-       {
-               signal NLME_START_ROUTER.confirm(status);
-       }
-
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/**********************                                  MLME-SET                          ******************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_SET.confirm(uint8_t status,uint8_t PIBAttribute)
-{
-       
-       
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/*************************                     MLME-GET                            ******************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue[])
-{
-       
-       
-       return SUCCESS;
-}
-       
-/*****************************************************************************************************/  
-/**************************************MLME-ASSOCIATE*************************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_ASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t CapabilityInformation, bool SecurityUse, uint8_t ACLEntry)
-{
-       
-       //notification that an other device wants to associate
-       //THIS DEVICE IS EITHER A COORDINATOR OR A ROUTER
-               
-       uint8_t cindex=0;
-
-atomic{
-
-       //check the neighbour table
-       cindex = check_neighbortableentry(LONG_ADDRESS,DeviceAddress[0] , DeviceAddress[1]);
-       
-
-       if( cindex > 0 )
-       {
-               call MLME_ASSOCIATE.response(DeviceAddress,neighbortable[cindex - 1].Network_Address,MAC_SUCCESS,0);
-       }
-       else
-       {       
-                       if(nwk_IB.nwkAvailableAddresses > 0)
-                       {
-                       
-                               //verify if the device is associating as a router or an end device
-                               if ( get_alternate_PAN_coordinator(CapabilityInformation) == 1)
-                               {
-                                       //add device to the neighbour table
-                                       add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],next_child_router_address,ROUTER,NEIGHBOR_IS_CHILD);
-                                       
-                                       printfUART("An_cr %x\n",next_child_router_address);
-
-                                       //send response, this shall lead to confirm in child device
-                                       call MLME_ASSOCIATE.response(DeviceAddress,next_child_router_address,MAC_SUCCESS,0);
-                                       //calculate the next address
-                                       next_child_router_address = networkaddress + ((number_child_router-1) * cskip) +1 ;
-                                       //increment the number of associated routers
-                                       number_child_router++;
-                                       //decrese the number of available addresses //the available addresses are influenced by the network configurations
-                                       nwk_IB.nwkAvailableAddresses--;
-                                       
-                                       printfUART("Dn_cr %x\n",next_child_router_address);
-                               }
-                               else
-                               {
-                                       //verify if its possible to associate children in the address space
-                                       //the number of end devices must be greater than 1 and lesser or iqual to maximum children minus maximum routers
-                                       if (number_child_end_devices > 1 && number_child_end_devices > (MAXCHILDREN - MAXROUTERS))
-                                       {
-                                               call MLME_ASSOCIATE.response(DeviceAddress,0xffff,CMD_RESP_PAN_CAPACITY,0);
-                                               //return SUCCESS;
-                                       }
-                                       else
-                                       {
-                                               //CHECK COMMENTED ON SHORT VERSION
-                                               add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],nwk_IB.nwkNextAddress,END_DEVICE,NEIGHBOR_IS_CHILD);
-                                               //send response, this shall lead to confirm in child device
-                                               call MLME_ASSOCIATE.response(DeviceAddress,nwk_IB.nwkNextAddress,MAC_SUCCESS,0);
-                                               
-                                               nwk_IB.nwkNextAddress=nwk_IB.nwkNextAddress + nwk_IB.nwkAddressIncrement;
-                                               
-                                               number_child_end_devices++;
-                                               
-                                               nwk_IB.nwkAvailableAddresses--;
-                                               
-                                       }
-                               }
-                               
-                               if (nwk_IB.nwkAvailableAddresses == 0 )
-                               {
-                                       call MLME_SET.request(MACASSOCIATIONPERMIT,(uint8_t *)0x00000000);
-                               }
-                       }
-                       else
-                       {
-                               //if there are no available addresses the coordinator/router shall not respond to the association request
-                               call MLME_ASSOCIATE.response(DeviceAddress,0xffff,CMD_RESP_PAN_CAPACITY,0);
-                       }
-       }
-}
-       return SUCCESS;
-}
-
-event error_t MLME_ASSOCIATE.confirm(uint16_t AssocShortAddress, uint8_t status)
-{
-
-       uint8_t v_temp[2];
-       ////printfUART("MLME_ASSOCIATE.confirm\n","");
-
-       if (AssocShortAddress == 0xffff)
-       {
-               //association failed
-               //printfUART("nwkass fail\n","");
-               signal NLME_JOIN.confirm(panid,NWK_NOT_PERMITTED);
-       
-       }
-       else
-       {
-       
-               networkaddress = AssocShortAddress;
-               //add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],nwk_IB.nwkNextAddress,END_DEVICE,NEIGHBOR_IS_CHILD);
-               //set the short address
-               
-               if (status == MAC_SUCCESS)
-               {
-                       joined = 0x01;
-                       v_temp[0] = (uint8_t)(networkaddress >> 8);
-                       v_temp[1] = (uint8_t)(networkaddress );
-
-                       //call MLME_SET.request(MACSHORTADDRESS,(uint32_t*)(uint32_t)&networkaddress);
-                       call MLME_SET.request(MACSHORTADDRESS,v_temp);
-                       
-                       signal NLME_JOIN.confirm(panid, NWK_SUCCESS);
-               }
-               else
-               {
-                       signal NLME_JOIN.confirm(panid,NWK_NOT_PERMITTED);
-               }
-       }
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/**************************************MLME-DISASSOCIATE**********************************************/
-/*****************************************************************************************************/ 
-event error_t MLME_DISASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t DisassociateReason, bool SecurityUse, uint8_t ACLEntry)
-{
-       ////printfUART("MLME_DISASSOCIATE.indication:SUCCESS\n", "");
-       signal NLME_LEAVE.confirm(DeviceAddress, NWK_SUCCESS);
-
-       return SUCCESS;
-}
-  
-event error_t MLME_DISASSOCIATE.confirm(uint8_t status)
-{
-       if (status == MAC_SUCCESS)
-       {
-               signal NLME_LEAVE.confirm(0, status);
-               ////printfUART("MLME_DISASSOCIATE.confirm:SUCCESS\n", "");
-       }
-       else
-       {
-               signal NLME_LEAVE.confirm(0, NWK_LEAVE_UNCONFIRMED);
-               ////printfUART("MLME_DISASSOCIATE.confirm:leave unconfirmed\n", "");
-       }
-
-       return SUCCESS;
-}
-/*****************************************************************************************************/  
-/*****************************************************************************************************/  
-/****************                                      MCPS EVENTS                              *************************************/
-/*****************************************************************************************************/ 
-/*****************************************************************************************************/  
-
-
-/*****************************************************************************************************/  
-/*********************                                 MCPS-DATA                          ***************************************/
-/*****************************************************************************************************/ 
-event error_t MCPS_DATA.confirm(uint8_t msduHandle, uint8_t status)
-{
-       ////printfUART("MCPS_DATA.confirm\n", "");
-       signal NLDE_DATA.confirm(1,status);
-       
-       return SUCCESS;
-}  
-event error_t MCPS_DATA.indication(uint16_t SrcAddrMode, uint16_t SrcPANId, uint32_t SrcAddr[2], uint16_t DstAddrMode, uint16_t DestPANId, uint32_t DstAddr[2], uint16_t msduLength,uint8_t msdu[100],uint16_t mpduLinkQuality, uint16_t SecurityUse, uint16_t ACLEntry)
-{
-       uint8_t payload[100];
-       uint32_t route_destination_address[2];
-       uint32_t net_addr[2];
-       uint8_t next_hop_index;
-       
-       routing_fields *routing_fields_ptr;
-       routing_fields_ptr = (routing_fields *)&msdu[0];
-       
-       net_addr[1] =(uint32_t) networkaddress;
-       net_addr[0]=0x00000000;
-
-       if ( routing_fields_ptr->destination_address == networkaddress)
-       {
-               //I am the destination
-               memcpy(&payload,&msdu[8],(msduLength-8)*sizeof(uint8_t));
-               
-               //pass data on to APL layer
-               signal NLDE_DATA.indication(routing_fields_ptr->source_address,(uint16_t)(msduLength-8),payload, mpduLinkQuality);
-               
-               return SUCCESS; 
-       }
-       else
-       {
-               //I am not the destination
-               if(device_type != COORDINATOR)
-               {
-                       if( (networkaddress < routing_fields_ptr->destination_address) && (routing_fields_ptr->destination_address < (networkaddress + cskip_routing ) ) )
-                       {
-                               printfUART("route down to appropriate child\n", "");
-                               
-                               //check if destination is one of my children
-                               next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
-
-                               if (next_hop_index == 0)
-                               {
-                                       //destination is not my child
-                                       route_destination_address[0]=0x00000000;
-                                       route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
-                               }
-                               else
-                               {
-                                       //Im routing to my child
-                                       route_destination_address[0]=0x00000000;
-                                       route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
-                               }
-                               //send message
-                               call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
-                       }
-                       else
-                       {
-                               
-                               //changes to meet with the BEACON SYNCHRONIZATION requirements
-                               //route up to the parent
-                               atomic{
-                                       route_destination_address[0]=0x00000000;
-                                       route_destination_address[1]=neighbortable[parent].Network_Address;
-
-                                       call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions_upstream(1,0,0,0,1));
-                               }
-                       }
-               }
-               else
-               {
-                       //I AM THE PAN COORDINATOR
-                       //THE COORDINATOR ALWAYS ROUTES DOWN
-                       
-                       //route down to appropriate child
-                       //check if destination is one of my children
-                       next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
-                       
-                       if (next_hop_index == 0 )
-                       {
-                               //no entry in neigbortable
-                               //calculate route address
-                               route_destination_address[0]=0x00000000;
-                               route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
-                                       
-                               call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
-                       }
-                       else
-                       {
-                               //is my child
-                               route_destination_address[0]=0x00000000;
-                               route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
-
-                               call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
-                       }
-                       
-               }
-               
-               
-       }
-       
-return SUCCESS;
-}
-
-
-/*************************************************************/
-/*******************NLDE IMPLEMENTATION***********************/
-/*************************************************************/
-       
-/*************************************************************/
-/*************************NLDE - DATA*************************/
-/*************************************************************/
-
-//This primitive requests the transfer of a data PDU
-//page 159-161
-
-command error_t NLDE_DATA.request(uint16_t DstAddr, uint16_t NsduLength, uint8_t Nsdu[100], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, uint8_t SecurityEnable)
-{      
-
-       uint32_t srcadd[2];
-       //prefixed size because the devices reset itself when there is an error in the length
-       uint8_t MSDU[100];
-       uint32_t route_destination_address[2];
-       routing_fields *routing_fields_ptr;
-       uint8_t next_hop_index;
-       
-       routing_fields_ptr = (routing_fields *)&MSDU[0];
-       
-       if(joined==0)
-       {       
-               signal NLDE_DATA.confirm(NsduHandle, NWK_INVALID_REQUEST);
-       }
-       else
-       {
-               routing_fields_ptr->frame_control= set_route_frame_control(0x00,0x01,DiscoverRoute,SecurityEnable);
-               routing_fields_ptr->destination_address=DstAddr;
-               routing_fields_ptr->source_address=networkaddress;
-               routing_fields_ptr->radius=Radius;
-               routing_fields_ptr->sequence_number=nwk_IB.nwkSequenceNumber;
-               nwk_IB.nwkSequenceNumber++;
-               
-               memcpy(&MSDU[8],&Nsdu[0],NsduLength*sizeof(uint8_t));
-
-               srcadd[0] = 0x00000000;
-               srcadd[1] = (uint32_t)networkaddress;
-               
-               if (device_type == END_DEVICE)
-               {
-                       //if the device is an end device always sends the message to the parent
-
-                       route_destination_address[0]=0x00000000;
-                       route_destination_address[1]=neighbortable[parent].Network_Address;
-                                                                                                                                                                                                                                                                                                               //ack
-                       call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS,panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions(1,0,0,0));
-                       return SUCCESS;
-               }
-               
-               //send message if the device is the COORDINATOR or a ROUTER
-               if( (networkaddress < routing_fields_ptr->destination_address) && (routing_fields_ptr->destination_address < (networkaddress + cskip_routing ) ) )
-               {
-                       //route down to appropriate child
-                       //check if destination is one of my children
-                       next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
-
-                       if (next_hop_index == 0)
-                       {
-                               //destination is not my child
-                               route_destination_address[0]=0x00000000;
-                               route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
-                       }
-                       else
-                       {
-                               //Im routing to my child
-                               route_destination_address[0]=0x00000000;
-                               route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
-                       }
-                       //send the data                                                                                                                                                                                                                                                                 //ack
-                       call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS, panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions(1,0,0,0));
-               }
-               else
-               {
-                       //route up to parent
-                       atomic{
-                               route_destination_address[0]=0x00000000;
-                               route_destination_address[1]=neighbortable[parent].Network_Address;
-                                                                                                                                                                                                                                                                                                       //ack
-                               call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS, panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions_upstream(1,0,0,0,1));
-                       }
-               }
-               
-       }
-       
-       return SUCCESS;
-}
-
-/*************************************************************/
-/*******************NLME IMPLEMENTATION***********************/
-/*************************************************************/
-
-/*************************************************************/
-/*******************NLME - START - ROUTER*********************/
-/*************************************************************/
-
-//This primitive allows the NHL of a ZigBee Router to initialize or change its superframe configuration.
-//p171 and 210
-command error_t NLME_START_ROUTER.request(uint8_t BeaconOrder, uint8_t SuperframeOrder, bool BatteryLifeExtension,uint32_t StartTime)
-{      
-       //printfUART("NLME_START_ROUTER.request\n", "");
-       
-       device_type = ROUTER;
-       
-       if(TYPE_DEVICE == ROUTER)
-       {
-       
-               //assign current BO and SO
-               beaconorder = BeaconOrder;
-               superframeorder = SuperframeOrder;
-       
-               call MLME_SET.request(MACBEACONORDER, (uint8_t *)&BeaconOrder);
-               
-               call MLME_SET.request(MACSUPERFRAMEORDER, (uint8_t *)&SuperframeOrder);
-               
-               //*******************************************************
-               //***********SET PAN VARIABLES***************************
-               depth=DEVICE_DEPTH;
-               
-               nwk_IB.nwkAvailableAddresses=AVAILABLEADDRESSES;
-               nwk_IB.nwkAddressIncrement= ADDRESSINCREMENT;
-               
-               nwk_IB.nwkMaxChildren=MAXCHILDREN;      //number of children a device is allowed to have on its current network
-               nwk_IB.nwkMaxDepth=MAXDEPTH;    //the depth a device can have
-               nwk_IB.nwkMaxRouters=MAXROUTERS;
-               
-               cskip = Cskip(depth);
-               
-               cskip_routing = Cskip(depth -1);
-               
-               nwk_IB.nwkNextAddress = networkaddress + (cskip * nwk_IB.nwkMaxRouters) + nwk_IB.nwkAddressIncrement;
-       
-               next_child_router_address = networkaddress +((number_child_router-1) * cskip) +1;
-       
-               
-               number_child_router++;
-               
-               
-               printfUART("cskip  %d\n", cskip);
-               printfUART("C %d D %d r %d\n", MAXCHILDREN,MAXDEPTH,MAXROUTERS);
-       
-               //  command error_t request(uint32_t PANId, uint8_t LogicalChannel, uint8_t BeaconOrder, uint8_t SuperframeOrder,bool PANCoordinator,bool BatteryLifeExtension,bool CoordRealignment,bool SecurityEnable);
-               call MLME_START.request(panid,LOGICAL_CHANNEL,BeaconOrder, SuperframeOrder, 0, 0,0,0,StartTime);
-               
-       }
-       else
-       {
-       
-               signal NLME_START_ROUTER.confirm(NWK_INVALID_REQUEST);
-               
-       }
-       return SUCCESS;
-}
-
-
-/*************************************************************/
-/******************NLME - NETWORK - FORMATION*****************/
-/*************************************************************/
-
-//This primitive allows the NHL to request to start a ZigBee network with itself as the coordinator
-//Page 167-169
-command error_t NLME_NETWORK_FORMATION.request(uint32_t ScanChannels, uint8_t ScanDuration, uint8_t BeaconOrder, uint8_t SuperframeOrder, uint16_t PANId, bool BatteryLifeExtension)
-{
-
-       uint8_t v_temp[6];
-
-       v_temp[0] = 0x06;
-       
-       device_type = COORDINATOR;
-       //device_type = ROUTER;
-       
-       call MLME_SET.request(MACMAXBEACONPAYLOADLENGTH,v_temp);
-       
-       //protocol ID
-       v_temp[0] = 0x00;
-       //uint8_t nwk_payload_profile_protocolversion(uint8_t stackprofile,uint8_t nwkcprotocolversion)
-       v_temp[1] = nwk_payload_profile_protocolversion(0x00,0x00);
-       //uint8_t nwk_payload_capacity(uint8_t routercapacity,uint8_t devicedepth,uint8_t enddevicecapacity)
-       v_temp[2] = nwk_payload_capacity(0x01,0x00,0x01);
-       
-       //TX OFFSET (3 bytes)
-       v_temp[3] = 0x56;
-       v_temp[4] = 0x34;
-       v_temp[5] = 0x12;
-       
-       
-       call MLME_SET.request(MACBEACONPAYLOAD,v_temp);
-       
-
-       printfUART("NLME_NETWORK_FORMATION.request\n", "");
-       //perform an energydetection scan
-       //perform an active scan
-       //and select a suitable channel
-       //panid must be less than or equal to 0x3fff
-       
-       //assign current panid
-       panid=PANId;
-       
-       //assign current BO and SO
-       beaconorder = BeaconOrder;
-       superframeorder = SuperframeOrder;
-       
-       call MLME_SET.request(MACBEACONORDER, (uint8_t *)&BeaconOrder);
-
-       call MLME_SET.request(MACSUPERFRAMEORDER, (uint8_t *)&SuperframeOrder);
-       
-       v_temp[0] = (uint8_t)(PANId >> 8);
-       v_temp[1] = (uint8_t)PANId;
-       
-       call MLME_SET.request(MACPANID, v_temp);
-
-       //static assignement of the coordinator address
-       networkaddress=0x0000;//Network address of the ZC of a network always 0x0000;
-       
-       //////printfUART("setting short addr: %i\n", networkaddress);
-       
-       v_temp[0] = (uint8_t)(networkaddress >> 8);
-       v_temp[1] = (uint8_t)(networkaddress);
-
-       
-       call MLME_SET.request(MACSHORTADDRESS,v_temp);
-       
-       
-       //*******************************************************
-       //***********SET PAN VARIABLES***************************
-       //nwk_IB.nwkNextAddress=networkaddress+0x0001;
-       depth=DEVICE_DEPTH;
-       nwk_IB.nwkAvailableAddresses=AVAILABLEADDRESSES;
-       nwk_IB.nwkAddressIncrement= ADDRESSINCREMENT;
-       
-       nwk_IB.nwkMaxChildren=MAXCHILDREN;      //number of children a device is allowed to have on its current network
-       nwk_IB.nwkMaxDepth=MAXDEPTH;    //the depth a device can have
-       nwk_IB.nwkMaxRouters=MAXROUTERS;
-       
-       cskip = Cskip(depth);
-       
-       cskip_routing = Cskip(depth -1 );
-       
-       nwk_IB.nwkNextAddress = networkaddress + (cskip * nwk_IB.nwkMaxRouters) + nwk_IB.nwkAddressIncrement;
-       
-       next_child_router_address = networkaddress +((number_child_router-1) * cskip) +1;
-       
-       number_child_router++;
-       
-       printfUART("cskip  %d\n", cskip);
-       printfUART("C %d D %d r %d\n", MAXCHILDREN,MAXDEPTH,MAXROUTERS);
-       
-       
-       call MLME_START.request(PANId, LOGICAL_CHANNEL,BeaconOrder ,SuperframeOrder,1,0,0,0,0);
-       
-
-
-
-    
-
-
-
-
-       return SUCCESS;
-}
-       
-/*************************************************************/
-/***************NLME - NETWORK - DISCOVERY *******************/
-/*************************************************************/
-
-//This primitive allows the next higher layer to request that the NWK layer discover networks currently operating within the POS.
-//p164 and 210
-command error_t NLME_NETWORK_DISCOVERY.request(uint32_t ScanChannels, uint8_t Scanduration)
-{
-       
-       //ISSUE an MLME_SCAN.request to find the available networks
-       //Temporary descover of the network
-       //Channel Scan is not working properly
-       //manually assign the network descriptor
-       
-       /*
-       networkdescriptor networkdescriptorlist[1];
-*/
-       printfUART("2 lauch passive scan\n", "");
-       //The networkdescriptorlist must contain information about every network that was heard
-       
-       //make NetworkDescriptorList out of the PanDescriptorList
-
-    call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
-
-/*
-
-       networkdescriptorlist[0].PANId=0x1234;
-       networkdescriptorlist[0].LogicalChannel=LOGICAL_CHANNEL;
-       networkdescriptorlist[0].StackProfile=0x00;
-       networkdescriptorlist[0].ZigBeeVersion=0x01;
-       networkdescriptorlist[0].BeaconOrder=7;
-       networkdescriptorlist[0].SuperframeOrder=6;
-       networkdescriptorlist[0].PermitJoining=1;
-       
-       //temporary assignement on the neighbout table of the suitable PAN coordinator
-       if (DEVICE_DEPTH == 0x01)
-               add_neighbortableentry(networkdescriptorlist[0].PANId,D1_PAN_EXT0,D1_PAN_EXT1,D1_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
-       if (DEVICE_DEPTH == 0x02)
-               add_neighbortableentry(networkdescriptorlist[0].PANId,D2_PAN_EXT0,D2_PAN_EXT1,D2_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
-       if (DEVICE_DEPTH == 0x03)
-               add_neighbortableentry(networkdescriptorlist[0].PANId,D3_PAN_EXT0,D3_PAN_EXT1,D3_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
-       if (DEVICE_DEPTH == 0x04)
-               add_neighbortableentry(networkdescriptorlist[0].PANId,D4_PAN_EXT0,D4_PAN_EXT1,D4_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
-       
-       
-       
-       signal NLME_NETWORK_DISCOVERY.confirm(1,networkdescriptorlist, NWK_SUCCESS);
-*/
-       return SUCCESS;
-}
-
-/*************************************************************/
-/************************NLME - JOIN**************************/
-/*************************************************************/
-//This primitive allows the NHL to request to join a network either through association.
-//p173 and 210
-command error_t NLME_JOIN.request(uint16_t PANId, bool JoinAsRouter, bool RejoinNetwork, uint32_t ScanChannels, uint8_t ScanDuration, uint8_t PowerSource, uint8_t RxOnWhenIdle, uint8_t MACSecurity)
-{      
-
-       //Assume we have selected a suitable parent and all previous conditions were true
-       uint32_t destinaddress[2];
-       
-       printfUART("9 find parent\n", "");
-       
-       //list_neighbourtable();
-       
-       parent_index = find_suitable_parent();
-       
-       panid = PANId;
-       
-       //printfUART("NLME_JOIN %i %i\n", parent_index,panid); 
-       
-       if(parent_index == 0)
-       {
-               signal NLME_JOIN.confirm(PANId,NWK_NOT_PERMITTED);
-       }
-       else
-       {
-               //assign the true value to parent index
-               parent_index = parent_index - 1;
-                               
-               //destinaddress[0]=neighbortable[parent_index].Extended_Address0;
-               //destinaddress[1]=neighbortable[parent_index].Extended_Address1;
-               //verificar o endere�o do pan coordinator
-               destinaddress[0]=0x00000000;
-               
-               
-               destinaddress[1] = neighbortable[parent_index].Network_Address;
-               
-               /*
-               if (DEVICE_DEPTH == 0x01)
-                       destinaddress[1]=D1_PAN_SHORT;
-               if (DEVICE_DEPTH == 0x02)
-                       destinaddress[1]=D2_PAN_SHORT;
-               if (DEVICE_DEPTH == 0x03)
-                       destinaddress[1]=D3_PAN_SHORT;
-               if (DEVICE_DEPTH == 0x04)
-                       destinaddress[1]=D4_PAN_SHORT;
-               */      
-                       
-               printfUART("10 associate to %i\n", destinaddress[1]);   
-               //set_capability_information(uint8_t alternate_PAN_coordinator, uint8_t device_type, uint8_t power_source, uint8_t receiver_on_when_idle, uint8_t security, uint8_t allocate_address)
-               
-               if (JoinAsRouter == 0x01)
-                       call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,PANId,destinaddress, set_capability_information(JoinAsRouter,0x01,PowerSource,RxOnWhenIdle,MACSecurity,0x01),0);
-               else
-               {
-               
-                       printfUART("11 go ass\n", "");  
-               
-                       coordinator_addr[0]=0x00000000;
-                       coordinator_addr[1] = neighbortable[parent_index].Network_Address;
-                       //BUILD the PAN descriptor of the COORDINATOR
-                       //assuming that the adress is short
-                       pan_des.CoordAddrMode = SHORT_ADDRESS;
-                       pan_des.CoordPANId = panid;
-                       pan_des.CoordAddress0=0x00000000;
-                       pan_des.CoordAddress1=(uint32_t)neighbortable[parent_index].Network_Address;
-                       pan_des.LogicalChannel=neighbortable[parent_index].Logical_Channel;
-                       //superframe specification field
-                       //pan_des.SuperframeSpec = neighbortable[parent_index].SuperframeSpec;
-
-                       pan_des.GTSPermit=0x01;
-                       pan_des.LinkQuality=0x00;
-                       pan_des.TimeStamp=0x000000;
-                       pan_des.SecurityUse=0;
-                       pan_des.ACLEntry=0x00;
-                       pan_des.SecurityFailure=0x00;
-       
-                       received_beacon_count=0;
-                       go_associate=1;
-                       
-                       //call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,PANId,destinaddress, set_capability_information(JoinAsRouter,0x00,PowerSource,RxOnWhenIdle,MACSecurity,0x01),0);
-               }
-       }
-       
-       return SUCCESS;
-}
-
-
-/*************************************************************/
-/************************NLME - LEAVE*************************/
-/*************************************************************/
-
-//This primitive allows the NHL to request that it or another device leaves the network
-//page 181-183
-command error_t NLME_LEAVE.request(uint32_t DeviceAddress[],bool RemoveChildren, bool MACSecurityEnable)
-{      
-       uint32_t devaddr[2];
-       ////printfUART("NLME_LEAVE.request\n", ""); 
-       if (DeviceAddress == 0)//child asked to leave
-       {       
-               if(RemoveChildren == 0)//implemented like it is always 0
-               {       
-                       //send leave request command frame: RemoveChildren subfield=0 of the command option field of the command frame payload
-                       //call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
-                       devaddr[0]=neighbortable[parent].Extended_Address0;
-                       devaddr[1]=neighbortable[parent].Extended_Address1;
-                       call MLME_DISASSOCIATE.request(devaddr,0x02,0);
-               }
-               else
-               {
-                       //send leave request command frame: RemoveChildren subfield=1
-                       //try to remove the children, call NLME_LEAVE.request(uint32_t DeviceAddress[]=address of child,bool RemoveChildren, bool MACSecurityEnable)
-                       //call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
-               }
-       }
-       else//parent forced a child to leave
-       {
-               //if(check_neighbortableentry(DeviceAddress[0], DeviceAddress[1]) == 0)
-               //{
-               //      signal NLME_LEAVE.confirm(DeviceAddress,NWK_UNKNOWN_DEVICE);
-               //      //call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
-               //}
-               
-       }
-       
-       
-       return SUCCESS;
-}
-
-/*************************************************************/
-/************************NLME - SYNC**************************/
-/*************************************************************/
-
-//This primitive allows the NHL to synchronize or extract data from its ZigBee coordinator or router
-//page 186-187
-command error_t NLME_SYNC.request(bool Track)
-{
-
-       return SUCCESS;
-}
-  
-/*************************************************************/
-/*****************        NLME-SET     ********************/
-/*************************************************************/
-       
-command error_t NLME_SET.request(uint8_t NIBAttribute, uint16_t NIBAttributeLength, uint16_t NIBAttributeValue)
-{
-
-       atomic{
-       
-               switch(NIBAttribute)
-               {
-                       case NWKSEQUENCENUMBER :                nwk_IB.nwkSequenceNumber = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkSequenceNumber: %x\n",nwk_IB.nwkSequenceNumber);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKPASSIVEACKTIMEOUT :             nwk_IB.nwkPassiveAckTimeout = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkPassiveAckTimeout: %x\n",nwk_IB.nwkPassiveAckTimeout);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                       
-                       
-                       case NWKMAXBROADCASTRETRIES :   nwk_IB.nwkMaxBroadcastRetries = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkMaxBroadcastRetries: %x\n",nwk_IB.nwkMaxBroadcastRetries);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKMAXCHILDREN :                   nwk_IB.nwkMaxChildren = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkMaxChildren: %x\n",nwk_IB.nwkMaxChildren);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKMAXDEPTH :                              nwk_IB.nwkMaxDepth = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkMaxDepth: %x\n",nwk_IB.nwkMaxDepth);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-       
-                       case NWKMAXROUTERS :                    nwk_IB.nwkMaxRouters = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkMaxRouters: %x\n",nwk_IB.nwkMaxRouters);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-       
-                       case NWKMETWORKBROADCASTDELIVERYTIME :          nwk_IB.nwkNetworkBroadcastDeliveryTime = (uint8_t) NIBAttributeValue;
-                                                                                                               //////printfUART("nwk_IB.nwkNetworkBroadcastDeliveryTime: %x\n",nwk_IB.nwkNetworkBroadcastDeliveryTime);
-                                                                                                               signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                                               break;
-                       case NWKREPORTCONSTANTCOST :    nwk_IB.nwkReportConstantCost = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkReportConstantCost: %x\n",nwk_IB.nwkReportConstantCost);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                       case NWKROUTEDISCOVERYRETRIESPERMITED :         nwk_IB.nwkRouteDiscoveryRetriesPermitted = (uint8_t) NIBAttributeValue;
-                                                                                                               //////printfUART("nwk_IB.nwkRouteDiscoveryRetriesPermitted: %x\n",nwk_IB.nwkRouteDiscoveryRetriesPermitted);
-                                                                                                               signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                                               break;
-       
-                       case NWKSYMLINK :                               nwk_IB.nwkSymLink = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkSymLink: %x\n",nwk_IB.nwkSymLink);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                       
-                       case NWKCAPABILITYINFORMATION : nwk_IB.nwkCapabilityInformation = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkCapabilityInformation: %x\n",nwk_IB.nwkCapabilityInformation);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKUSETREEADDRALLOC :              nwk_IB.nwkUseTreeAddrAlloc = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkUseTreeAddrAlloc: %x\n",nwk_IB.nwkUseTreeAddrAlloc);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKUSETREEROUTING :                nwk_IB.nwkUseTreeRouting = (uint8_t) NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkUseTreeRouting: %x\n",nwk_IB.nwkUseTreeRouting);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKNEXTADDRESS :                   nwk_IB.nwkNextAddress = NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkNextAddress: %x\n",nwk_IB.nwkNextAddress);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKAVAILABLEADDRESSES :    nwk_IB.nwkAvailableAddresses = NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkAvailableAddresses: %x\n",nwk_IB.nwkAvailableAddresses);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKADDRESSINCREMENT :              nwk_IB.nwkAddressIncrement =NIBAttributeValue;
-                                                                                       //////printfUART("nwk_IB.nwkAddressIncrement: %x\n",nwk_IB.nwkAddressIncrement);
-                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                       break;
-                                                                                       
-                       case NWKTRANSACTIONPERSISTENCETIME :    nwk_IB.nwkTransactionPersistenceTime = (uint8_t) NIBAttributeValue;
-                                                                                                       //////printfUART("nwk_IB.nwkTransactionPersistenceTime: %x\n",nwk_IB.nwkTransactionPersistenceTime);
-                                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
-                                                                                                       break;
-                                                                                       
-                       default:                                                signal NLME_SET.confirm(NWK_UNSUPPORTED_ATTRIBUTE,NIBAttribute);
-                                                                                       break;
-                       
-               }
-               
-       }
-
-
-       return SUCCESS;
-}
-
-/*************************************************************/
-/*****************        NLME-GET     ********************/
-/*************************************************************/
-       
-command error_t NLME_GET.request(uint8_t NIBAttribute)
-{
-       switch(NIBAttribute)
-       {
-               case NWKSEQUENCENUMBER :                signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkSequenceNumber);
-                                                                               break;
-                                                                       
-               case NWKPASSIVEACKTIMEOUT :             signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkPassiveAckTimeout);
-                                                                               break;
-               
-               case NWKMAXBROADCASTRETRIES :   signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxBroadcastRetries);
-                                                                               break;
-
-               case NWKMAXCHILDREN :                   signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxChildren);
-                                                                               break;
-
-               case NWKMAXDEPTH :                              signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxDepth);
-                                                                               break;
-
-               case NWKMAXROUTERS :                    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxRouters);
-                                                                               break;
-
-               case NWKMETWORKBROADCASTDELIVERYTIME :          signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkNetworkBroadcastDeliveryTime);
-                                                                                                       break;
-
-               case NWKREPORTCONSTANTCOST :    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkReportConstantCost);
-                                                                               break;
-
-               case NWKROUTEDISCOVERYRETRIESPERMITED :         signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkRouteDiscoveryRetriesPermitted);
-                                                                                                       break;
-
-               case NWKSYMLINK :                               signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkSymLink);
-                                                                               break;
-               
-               case NWKCAPABILITYINFORMATION : signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkCapabilityInformation);
-                                                                               break;
-                                                                               
-               case NWKUSETREEADDRALLOC :              signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkUseTreeAddrAlloc);
-                                                                               break;
-                                                                               
-               case NWKUSETREEROUTING :                signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkUseTreeRouting);
-                                                                               break;
-                                                                               
-               case NWKNEXTADDRESS :                   signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkNextAddress);
-                                                                               break;
-
-               case NWKAVAILABLEADDRESSES :    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkAvailableAddresses);
-                                                                               break;
-                                                                               
-               case NWKADDRESSINCREMENT :              signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkAddressIncrement);
-                                                                               break;
-                                                                               
-               case NWKTRANSACTIONPERSISTENCETIME :    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkTransactionPersistenceTime);
-                                                                                               break;
-
-               default:                                                signal NLME_GET.confirm(NWK_UNSUPPORTED_ATTRIBUTE,NIBAttribute,0x0000,0x00);
-                                                                               break;
-       }
-       
-       return SUCCESS;
-}
-
-/*************************************************************/
-/**************neighbor table management functions************/
-/*************************************************************/
-
-//check if a specific neighbourtable Entry is present
-//Return 0:Entry is not present
-//Return i:Entry is present and return the index of the entry + 1
-       uint8_t check_neighbortableentry(uint8_t addrmode,uint32_t Address0,uint32_t Address1)
-       {       
-               
-               int i=0;
-               
-               
-               //printfUART("neighbourtable check c %i\n", neighbour_count);
-       
-               if (neighbour_count == 0)
-               {
-                       //printfUART("no neib\n", "");
-                       return 0;
-               }
-               
-               if(addrmode == SHORT_ADDRESS)
-               {       
-                       for(i=0; i < neighbour_count; i++)
-                       {       
-                               ///printfUART("compare %i %i\n", neighbortable[i].Network_Address, test);
-                               
-                               if(neighbortable[i].Network_Address == (uint16_t) Address0)
-                               {
-                                       //printfUART("already present \n", "" );
-                                       return i+1;
-                               }
-                       }
-                       return 0;
-               }
-               else
-               {
-                       for(i=0; i<neighbour_count; i++)
-                       {       
-                               ////printfUART("compare %x %x\n", neighbortable[i].Extended_Address0, Address0);
-                               if( (neighbortable[i].Extended_Address0 == Address0) && (neighbortable[i].Extended_Address1 == Address1))
-                               {
-                                       //printfUART("already present \n", "" );
-                                       return i+1;
-                               }
-                       }
-                       return 0;
-               }
-               
-               return 0;
-       }
-       
-       //function user to find a parent in the neighbour table
-       //Return 0:no parent is present
-       //Return i:parent is present and return the index of the entry + 1
-       uint8_t find_suitable_parent()
-       {
-               int i =0;
-               for (i=0; i<neighbour_count; i++)
-               {
-                       if(neighbortable[i].Relationship == NEIGHBOR_IS_PARENT)
-                       {
-                               return i+1;
-                       }
-               }
-               return 0;
-       }
-
-
-       //add a neighbortable entry     
-       void add_neighbortableentry     (uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship)
-       {
-       
-       atomic{
-               neighbortable[neighbour_count].PAN_Id=PAN_Id;
-               neighbortable[neighbour_count].Extended_Address0=Extended_Address0;
-               neighbortable[neighbour_count].Extended_Address1=Extended_Address1;
-               neighbortable[neighbour_count].Network_Address=Network_Address;
-               neighbortable[neighbour_count].Device_Type=Device_Type;
-               neighbortable[neighbour_count].Relationship=Relationship;
-               
-               neighbour_count++;
-               }
-               
-       }
-
-
-       
-       //update a neighbourtable entry
-       void update_neighbortableentry(uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship)
-       {
-               //search entry with correct extended address
-               uint8_t nrofEntry=0;
-               nrofEntry = check_neighbortableentry(0x01,Extended_Address0,Extended_Address1) - 1;
-               
-               //update every attribute
-               neighbortable[nrofEntry].PAN_Id=PAN_Id;
-               neighbortable[nrofEntry].Extended_Address0=Extended_Address0;
-               neighbortable[nrofEntry].Extended_Address1=Extended_Address1;
-               neighbortable[nrofEntry].Network_Address=Network_Address;
-               neighbortable[nrofEntry].Device_Type=Device_Type;
-               neighbortable[nrofEntry].Relationship=Relationship;
-               
-               ////printfUART("updated neighbourtable entry\n", "");
-       }
-
-
-       void list_neighbourtable()
-       {
-               int i;
-               ////printfUART("N List Count: %u\n", neighbour_count);
-       
-               for(i=0;i<neighbour_count;i++)
-               {
-                       printfUART("Panid %x", neighbortable[i].PAN_Id);
-                       printfUART("Extaddr0 %x", neighbortable[i].Extended_Address0);
-                       printfUART("Extaddr1 %x", neighbortable[i].Extended_Address1);
-                       printfUART("nwkaddr %u", neighbortable[i].Network_Address);
-                       printfUART("devtype %u", neighbortable[i].Device_Type);
-                       printfUART("relation %u", neighbortable[i].Relationship);
-                       printfUART("depth %u\n", neighbortable[i].Depth);               
-               }
-       }
-/*************************************************************/
-/*****************Address Assignment functions****************/
-/*************************************************************/
-
-//calculate the size of the address sub-block for a router at depth d
-uint16_t Cskip(uint8_t d)
-{      
-       uint8_t skip;
-       uint8_t power;
-       uint8_t x=1;
-       uint8_t Cm = nwk_IB.nwkMaxChildren;
-       uint8_t Rm = nwk_IB.nwkMaxRouters;
-       uint8_t Lm = nwk_IB.nwkMaxDepth;
-       /*////printfUART("nwk_IB.nwkMaxChildren: %i\n",  nwk_IB.nwkMaxChildren);
-       ////printfUART("nwk_IB.nwkMaxRouters: %i\n", nwk_IB.nwkMaxRouters);
-       ////printfUART("nwk_IB.nwkMaxDepth: %i\n", nwk_IB.nwkMaxDepth);*/
-       if (Rm == 1)
-       {
-               skip = 1 + Cm * (Lm - d - 1);
-       }
-       else
-       {       
-               int i;
-               
-               power=(Lm - d - 1);
-               for(i=0;i<power;i++)
-               {
-                       x=x*Rm;
-               }
-               skip = (1 + Cm - Rm - (Cm * x))/(1-Rm);
-       }
-       ////printfUART("Cksip function calculated: %i\n", skip);
-       return skip;
-}
-
-//Calculate the nexthopaddress of the appropriate child if route down is required
-uint16_t nexthopaddress(uint16_t destinationaddress,uint8_t d)
-{
-       uint16_t next_hop;
-
-       next_hop=(networkaddress + 1 + ((destinationaddress-(networkaddress+1))/cskip)* cskip);
-       
-       ////printfUART("Nexthop address calculated: %i\n", next_hop);
-       return next_hop;
-}
-
-/*************************************************************/
-/*****************Initialization functions********************/
-/*************************************************************/
-       //initialization of the nwk IB
-       void init_nwkIB()
-       {       
-               //nwk IB default values p 204-206
-               nwk_IB.nwkPassiveAckTimeout=0x03;
-               nwk_IB.nwkMaxBroadcastRetries=0x03;
-               nwk_IB.nwkMaxChildren=0x07;     //number of children a device is allowed to have on its current network
-               nwk_IB.nwkMaxDepth=0x05;        //the depth a device can have
-               nwk_IB.nwkMaxRouters=0x02;      //number of routers anyone device is allowed to have on its current network
-               //neighbortableentry nwkNeighborTable[];//null set
-               nwk_IB.nwkNetworkBroadcastDeliveryTime=( nwk_IB.nwkPassiveAckTimeout * nwk_IB.nwkMaxBroadcastRetries );
-               nwk_IB.nwkReportConstantCost=0x00;
-               nwk_IB.nwkRouteDiscoveryRetriesPermitted=nwkcDiscoveryRetryLimit;
-       //set? nwkRouteTable;//Null set
-               nwk_IB.nwkSymLink=0;
-               nwk_IB.nwkCapabilityInformation=0x00;
-               nwk_IB.nwkUseTreeAddrAlloc=1;
-               nwk_IB.nwkUseTreeRouting=1;
-               nwk_IB.nwkNextAddress=0x0000;
-               nwk_IB.nwkAvailableAddresses=0x0000;
-               nwk_IB.nwkAddressIncrement=0x0001;
-               nwk_IB.nwkTransactionPersistenceTime=0x01f4;
-       }
-
-
-  
-}
-
diff --git a/tos/lib/net/zigbee/ieee802154/nwk/NWKP.nc b/tos/lib/net/zigbee/ieee802154/nwk/NWKP.nc
new file mode 100644 (file)
index 0000000..e1032cc
--- /dev/null
@@ -0,0 +1,1559 @@
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author Andre Cunha
+ *
+ */
+#include <Timer.h>
+#include "printfUART.h"
+
+
+
+
+module NWKP {
+       
+
+       uses interface Leds;
+       
+       //MAC interfaces
+#ifndef TKN154_MAC
+       uses interface MLME_START;
+       
+       uses interface MLME_GET;
+       uses interface MLME_SET;
+       
+       uses interface MLME_BEACON_NOTIFY;
+       uses interface MLME_GTS;
+       
+       uses interface MLME_ASSOCIATE;
+       uses interface MLME_DISASSOCIATE;
+       
+       uses interface MLME_ORPHAN;
+       
+       uses interface MLME_SYNC;
+       uses interface MLME_SYNC_LOSS;
+       
+       uses interface MLME_RESET;
+       
+       uses interface MLME_SCAN;
+       
+       
+       uses interface MCPS_DATA;
+
+#else
+       uses interface OPENZB_MLME_START as MLME_START;
+       
+       uses interface OPENZB_MLME_GET as MLME_GET;
+       uses interface OPENZB_MLME_SET as MLME_SET;
+       
+       uses interface OPENZB_MLME_BEACON_NOTIFY as MLME_BEACON_NOTIFY;
+       uses interface OPENZB_MLME_GTS as MLME_GTS;
+       
+       uses interface OPENZB_MLME_ASSOCIATE as MLME_ASSOCIATE;
+       uses interface OPENZB_MLME_DISASSOCIATE as MLME_DISASSOCIATE;
+       
+       uses interface OPENZB_MLME_ORPHAN as MLME_ORPHAN;
+       
+       uses interface OPENZB_MLME_SYNC as MLME_SYNC;
+       uses interface OPENZB_MLME_SYNC_LOSS as MLME_SYNC_LOSS;
+       
+       uses interface OPENZB_MLME_RESET as MLME_RESET;
+       
+       uses interface OPENZB_MLME_SCAN as MLME_SCAN;
+       
+       
+       uses interface OPENZB_MCPS_DATA as MCPS_DATA;
+
+
+#endif
+
+
+       uses interface Random;
+
+//provides
+
+       provides interface Init;
+       provides interface NLDE_DATA;
+
+       //NLME NWK Management services
+    provides interface NLME_NETWORK_FORMATION;
+       provides interface NLME_NETWORK_DISCOVERY;
+       provides interface NLME_START_ROUTER;
+       provides interface NLME_JOIN;
+       provides interface NLME_LEAVE;
+       provides interface NLME_SYNC;
+       /*  
+       provides interface NLME_PERMIT_JOINING;
+       provides interface NLME_DIRECT_JOIN;*/
+       provides interface NLME_RESET;
+       
+       provides interface NLME_GET;
+       provides interface NLME_SET;
+
+  
+}
+implementation {
+
+
+       nwkIB nwk_IB;
+       
+       uint8_t device_type = END_DEVICE;
+       
+/*****************************************************/
+/*************Neighbourtable Variables****************/
+/*****************************************************/ 
+
+       //neighbour table array:
+       neighbortableentry neighbortable[NEIGHBOUR_TABLE_SIZE];
+       //number of neigbourtable entries:
+       uint8_t neighbour_count;
+       //the index of the parents neighbortable entry
+       uint8_t parent;
+       
+
+/*****************************************************/
+/****************ASSOCIATION Variables********************/
+/*****************************************************/ 
+
+       //CURRENT NETWORK ADDRESS
+       uint16_t networkaddress=0x0000;
+
+       //COORDINATOR
+       //address assignement variables
+       uint8_t depth=0;
+       uint8_t cskip=0;
+       
+       uint8_t cskip_routing=0;
+
+       //neighbour table parent index
+       uint8_t parent_index;
+
+       //NON COORDINATOR
+
+       //current pan characteristics
+       uint16_t panid;
+       uint8_t beaconorder;
+       uint8_t superframeorder;
+       
+       //next child router address
+       uint16_t next_child_router_address;
+       uint8_t number_child_router=0x01;
+       uint8_t number_child_end_devices=0x01;
+/*****************************************************/
+/****************Integer Variables********************/
+/*****************************************************/ 
+
+       uint8_t joined=0;
+       uint8_t sync_loss=0;
+       //uint8_t synchronizing=0;
+       uint8_t syncwait;
+       
+       
+       //USED AFTER DE SCAN //GIVE SOME TIME TO THE DEVICE TO SYNC WITH THE PARENT
+       uint8_t received_beacon_count=0;
+       
+       uint8_t go_associate =0;
+       
+       PANDescriptor pan_des;
+       uint32_t coordinator_addr[2];
+/******************************************************/
+/*********NEIGHBOuRTABLE  MANAGEMENT FUNCTIONS*********/
+/******************************************************/ 
+
+       void init_nwkIB();
+       
+       uint8_t check_neighbortableentry(uint8_t addrmode,uint32_t Extended_Address0,uint32_t Extended_Address1);
+       
+       void add_neighbortableentry     (uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship);
+       void update_neighbortableentry (uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship);       
+       
+       uint8_t find_suitable_parent();
+       
+       uint16_t Cskip(uint8_t d);
+       
+       uint16_t nexthopaddress(uint16_t destinationaddress,uint8_t d);
+       
+       void list_neighbourtable();
+
+
+
+ command error_t Init.init() {
+       
+       printfUART_init();//make the possibility to print
+               
+       init_nwkIB();
+       
+       nwk_IB.nwkSequenceNumber=call Random.rand16();
+  
+       return SUCCESS;
+ }
+
+
+/*****************************************************************************************************/  
+/**************************************MLME-SCAN*******************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_SCAN.confirm(uint8_t status,uint8_t ScanType, uint32_t UnscannedChannels, uint8_t ResultListSize, uint8_t EnergyDetectList[], SCAN_PANDescriptor PANDescriptorList[])
+{
+//FAULT-TOLERANCE
+
+//the device channel scan ends with a scan confirm containing a list of the PANs (beacons received during the scan) 
+
+       int i;
+       uint8_t max_lqi=0;
+       uint8_t best_pan_index=0;
+       
+       
+       networkdescriptor networkdescriptorlist[1];
+       
+       
+       //call Leds.redOff();
+       
+       printfUART("4 rec scan\n", "");
+       
+       //printfUART("MLME_SCAN.confirm %i\n", ScanType);
+       
+       if (ScanType == ORPHAN_SCAN)
+       {
+               printfUART("new scan \n", "");
+       
+               call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
+               return SUCCESS;
+       }
+       
+       
+       
+       if(ScanType == ED_SCAN)
+       {
+               for(i=0;i<ResultListSize;i++)
+               {
+                       printfUART("ED SCAN %i %i\n", (0x0A + i),EnergyDetectList[i]);
+               }
+               return SUCCESS;
+       }
+       
+       for (i=0; i<ResultListSize;i++)
+       {               /*
+                       printfUART("cord id %i", PANDescriptorList[i].CoordPANId);
+                       printfUART("CoordAddress %i", PANDescriptorList[i].CoordAddress);
+                       printfUART("LogicalChannel %i", PANDescriptorList[i].LogicalChannel);
+                       printfUART("SuperframeSpec %i", PANDescriptorList[i].SuperframeSpec);
+                       printfUART("lqi %i\n", PANDescriptorList[i].lqi);
+                       */
+               if(max_lqi < PANDescriptorList[i].lqi)
+               {
+                       max_lqi =PANDescriptorList[i].lqi;
+                       best_pan_index = i;
+               }
+       }
+       
+       printfUART("SELECTED cord id %i", PANDescriptorList[best_pan_index].CoordPANId);
+       printfUART("CoordAddress %i", PANDescriptorList[best_pan_index].CoordAddress);
+       printfUART("LogicalChannel %i", PANDescriptorList[best_pan_index].LogicalChannel);
+       printfUART("SuperframeSpec %i", PANDescriptorList[best_pan_index].SuperframeSpec);
+       printfUART("lqi %i\n", PANDescriptorList[best_pan_index].lqi);
+       
+       
+       
+       /*
+       
+       coordinator_addr[0] = 0x00000001;
+       
+       coordinator_addr[1] = (uint32_t)PANDescriptorList[best_pan_index].CoordAddress;
+       
+       //pan_des = PANDescriptorList[best_pan_index];
+
+       
+       //BUILD the PAN descriptor of the COORDINATOR
+       //assuming that the adress is short
+       pan_des.CoordAddrMode = SHORT_ADDRESS;
+       pan_des.CoordPANId = PANDescriptorList[best_pan_index].CoordAddress;
+       pan_des.CoordAddress0=0x00000000;
+       pan_des.CoordAddress1=0x00000000;
+       pan_des.LogicalChannel=PANDescriptorList[best_pan_index].LogicalChannel;
+       //superframe specification field
+       pan_des.SuperframeSpec = PANDescriptorList[best_pan_index].SuperframeSpec;
+       
+       pan_des.GTSPermit=0x01;
+       pan_des.LinkQuality=0x00;
+       pan_des.TimeStamp=0x000000;
+       pan_des.SecurityUse=0;
+       pan_des.ACLEntry=0x00;
+       pan_des.SecurityFailure=0x00;
+       
+       */
+
+       
+       
+       printfUART("5 en sync %x \n", PANDescriptorList[best_pan_index].LogicalChannel);
+       // the sync enables the TimerAsync events, in order to enable the synchronization with the PAN coordinator
+       call MLME_SYNC.request(PANDescriptorList[best_pan_index].LogicalChannel,0);
+       
+       
+       
+       //The networkdescriptorlist must contain information about every network that was heard
+       
+       //make NetworkDescriptorList out of the PanDescriptorList
+
+
+    printfUART("6 add neigh\n", "");
+
+       networkdescriptorlist[0].PANId=PANDescriptorList[best_pan_index].CoordPANId;
+       networkdescriptorlist[0].LogicalChannel=LOGICAL_CHANNEL;
+       networkdescriptorlist[0].StackProfile=0x00;
+       networkdescriptorlist[0].ZigBeeVersion=0x01;
+       networkdescriptorlist[0].BeaconOrder=7;
+       networkdescriptorlist[0].SuperframeOrder=6;
+       networkdescriptorlist[0].PermitJoining=1;
+
+       add_neighbortableentry(networkdescriptorlist[0].PANId,0x00000000,0x00000000,PANDescriptorList[best_pan_index].CoordAddress,COORDINATOR,NEIGHBOR_IS_PARENT);
+       
+       signal NLME_NETWORK_DISCOVERY.confirm(1,networkdescriptorlist, NWK_SUCCESS);
+       
+       
+       
+       return SUCCESS;
+}
+
+/*****************************************************************************************************/  
+/**************************************MLME-ORPHAN****************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_ORPHAN.indication(uint32_t OrphanAddress[1], uint8_t SecurityUse, uint8_t ACLEntry)
+{
+
+       
+               
+               return 0x0000;
+       }
+
+/*****************************************************************************************************/  
+/**************************************MLME-RESET*****************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_RESET.confirm(uint8_t status)
+{
+
+  signal NLME_RESET.confirm(status);
+
+  return SUCCESS;
+}
+/*****************************************************************************************************/  
+/**************************************MLME-SYNC-LOSS*************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_SYNC_LOSS.indication(uint8_t LossReason)
+{
+       ////printfUART("MLME_SYNC_LOSS.indication\n", ""); 
+       sync_loss = 1;
+       syncwait=1;
+       //signal NLME_SYNC.indication();
+/*
+
+printfUART("SL\n","");
+               
+               call MLME_SCAN.request(ORPHAN_SCAN,0xFFFFFFFF,7);
+*/
+       return SUCCESS;
+}
+/*****************************************************************************************************/  
+/**************************************MLME-GTS*******************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_GTS.confirm(uint8_t GTSCharacteristics, uint8_t status)
+{
+
+
+
+       return SUCCESS;
+}
+
+event error_t MLME_GTS.indication(uint16_t DevAddress, uint8_t GTSCharacteristics, bool SecurityUse, uint8_t ACLEntry)
+{
+
+
+
+       return SUCCESS;
+}
+
+/*****************************************************************************************************/  
+/**************************************MLME-BEACON NOTIFY*********************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_BEACON_NOTIFY.indication(uint8_t BSN,PANDescriptor pan_descriptor, uint8_t PenAddrSpec, uint8_t AddrList, uint8_t sduLength, uint8_t sdu[])
+{
+       
+       uint32_t destinaddress[2];
+       printfUART("BN\n", "");
+
+       
+       if (go_associate==1)
+       {
+               received_beacon_count++;
+               
+               printfUART("bn %i\n", received_beacon_count);
+               
+               if (received_beacon_count==5)
+               {
+                               printfUART("sa \n", "");
+                               
+                               go_associate=0;
+                               //call MLME_ASSOCIATE.request(pan_des.LogicalChannel,SHORT_ADDRESS,pan_des.CoordPANId,coordinator_addr,0x00,0x00);
+               
+               destinaddress[0]=0x00000000;
+               destinaddress[1]=0x00000000;
+               
+               
+               call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,0x1234,destinaddress, set_capability_information(0x00,0x00,0x00,0x00,0x00,0x01),0);
+               
+               }
+               
+       }
+
+       return SUCCESS;
+}
+/*****************************************************************************************************/  
+/**************************************MLME-START*****************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_START.confirm(uint8_t status)
+{
+               ////printfUART("MLME_START.confirm\n", "");
+       if (device_type==COORDINATOR)
+       {
+               signal NLME_NETWORK_FORMATION.confirm(status);
+               joined=1;
+       }
+       else
+       {
+               signal NLME_START_ROUTER.confirm(status);
+       }
+
+       return SUCCESS;
+}
+/*****************************************************************************************************/  
+/**********************                                  MLME-SET                          ******************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_SET.confirm(uint8_t status,uint8_t PIBAttribute)
+{
+       
+       
+       return SUCCESS;
+}
+/*****************************************************************************************************/  
+/*************************                     MLME-GET                            ******************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue[])
+{
+       
+       
+       return SUCCESS;
+}
+       
+/*****************************************************************************************************/  
+/**************************************MLME-ASSOCIATE*************************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_ASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t CapabilityInformation, bool SecurityUse, uint8_t ACLEntry)
+{
+       
+       //notification that an other device wants to associate
+       //THIS DEVICE IS EITHER A COORDINATOR OR A ROUTER
+               
+       uint8_t cindex=0;
+
+atomic{
+
+       //check the neighbour table
+       cindex = check_neighbortableentry(LONG_ADDRESS,DeviceAddress[0] , DeviceAddress[1]);
+       
+
+       if( cindex > 0 )
+       {
+               call MLME_ASSOCIATE.response(DeviceAddress,neighbortable[cindex - 1].Network_Address,MAC_SUCCESS,0);
+       }
+       else
+       {       
+                       if(nwk_IB.nwkAvailableAddresses > 0)
+                       {
+                       
+                               //verify if the device is associating as a router or an end device
+                               if ( get_alternate_PAN_coordinator(CapabilityInformation) == 1)
+                               {
+                                       //add device to the neighbour table
+                                       add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],next_child_router_address,ROUTER,NEIGHBOR_IS_CHILD);
+                                       
+                                       printfUART("An_cr %x\n",next_child_router_address);
+
+                                       //send response, this shall lead to confirm in child device
+                                       call MLME_ASSOCIATE.response(DeviceAddress,next_child_router_address,MAC_SUCCESS,0);
+                                       //calculate the next address
+                                       next_child_router_address = networkaddress + ((number_child_router-1) * cskip) +1 ;
+                                       //increment the number of associated routers
+                                       number_child_router++;
+                                       //decrese the number of available addresses //the available addresses are influenced by the network configurations
+                                       nwk_IB.nwkAvailableAddresses--;
+                                       
+                                       printfUART("Dn_cr %x\n",next_child_router_address);
+                               }
+                               else
+                               {
+                                       //verify if its possible to associate children in the address space
+                                       //the number of end devices must be greater than 1 and lesser or iqual to maximum children minus maximum routers
+                                       if (number_child_end_devices > 1 && number_child_end_devices > (MAXCHILDREN - MAXROUTERS))
+                                       {
+                                               call MLME_ASSOCIATE.response(DeviceAddress,0xffff,CMD_RESP_PAN_CAPACITY,0);
+                                               //return SUCCESS;
+                                       }
+                                       else
+                                       {
+                                               //CHECK COMMENTED ON SHORT VERSION
+                                               add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],nwk_IB.nwkNextAddress,END_DEVICE,NEIGHBOR_IS_CHILD);
+                                               //send response, this shall lead to confirm in child device
+                                               call MLME_ASSOCIATE.response(DeviceAddress,nwk_IB.nwkNextAddress,MAC_SUCCESS,0);
+                                               
+                                               nwk_IB.nwkNextAddress=nwk_IB.nwkNextAddress + nwk_IB.nwkAddressIncrement;
+                                               
+                                               number_child_end_devices++;
+                                               
+                                               nwk_IB.nwkAvailableAddresses--;
+                                               
+                                       }
+                               }
+                               
+                               if (nwk_IB.nwkAvailableAddresses == 0 )
+                               {
+                                       call MLME_SET.request(MACASSOCIATIONPERMIT,(uint8_t *)0x00000000);
+                               }
+                       }
+                       else
+                       {
+                               //if there are no available addresses the coordinator/router shall not respond to the association request
+                               call MLME_ASSOCIATE.response(DeviceAddress,0xffff,CMD_RESP_PAN_CAPACITY,0);
+                       }
+       }
+}
+       return SUCCESS;
+}
+
+event error_t MLME_ASSOCIATE.confirm(uint16_t AssocShortAddress, uint8_t status)
+{
+
+       uint8_t v_temp[2];
+       ////printfUART("MLME_ASSOCIATE.confirm\n","");
+
+       if (AssocShortAddress == 0xffff)
+       {
+               //association failed
+               //printfUART("nwkass fail\n","");
+               signal NLME_JOIN.confirm(panid,NWK_NOT_PERMITTED);
+       
+       }
+       else
+       {
+       
+               networkaddress = AssocShortAddress;
+               //add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],nwk_IB.nwkNextAddress,END_DEVICE,NEIGHBOR_IS_CHILD);
+               //set the short address
+               
+               if (status == MAC_SUCCESS)
+               {
+                       joined = 0x01;
+                       v_temp[0] = (uint8_t)(networkaddress >> 8);
+                       v_temp[1] = (uint8_t)(networkaddress );
+
+                       //call MLME_SET.request(MACSHORTADDRESS,(uint32_t*)(uint32_t)&networkaddress);
+                       call MLME_SET.request(MACSHORTADDRESS,v_temp);
+                       
+                       signal NLME_JOIN.confirm(panid, NWK_SUCCESS);
+               }
+               else
+               {
+                       signal NLME_JOIN.confirm(panid,NWK_NOT_PERMITTED);
+               }
+       }
+       return SUCCESS;
+}
+/*****************************************************************************************************/  
+/**************************************MLME-DISASSOCIATE**********************************************/
+/*****************************************************************************************************/ 
+event error_t MLME_DISASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t DisassociateReason, bool SecurityUse, uint8_t ACLEntry)
+{
+       ////printfUART("MLME_DISASSOCIATE.indication:SUCCESS\n", "");
+       signal NLME_LEAVE.confirm(DeviceAddress, NWK_SUCCESS);
+
+       return SUCCESS;
+}
+  
+event error_t MLME_DISASSOCIATE.confirm(uint8_t status)
+{
+       if (status == MAC_SUCCESS)
+       {
+               signal NLME_LEAVE.confirm(0, status);
+               ////printfUART("MLME_DISASSOCIATE.confirm:SUCCESS\n", "");
+       }
+       else
+       {
+               signal NLME_LEAVE.confirm(0, NWK_LEAVE_UNCONFIRMED);
+               ////printfUART("MLME_DISASSOCIATE.confirm:leave unconfirmed\n", "");
+       }
+
+       return SUCCESS;
+}
+/*****************************************************************************************************/  
+/*****************************************************************************************************/  
+/****************                                      MCPS EVENTS                              *************************************/
+/*****************************************************************************************************/ 
+/*****************************************************************************************************/  
+
+
+/*****************************************************************************************************/  
+/*********************                                 MCPS-DATA                          ***************************************/
+/*****************************************************************************************************/ 
+event error_t MCPS_DATA.confirm(uint8_t msduHandle, uint8_t status)
+{
+       ////printfUART("MCPS_DATA.confirm\n", "");
+       signal NLDE_DATA.confirm(1,status);
+       
+       return SUCCESS;
+}  
+event error_t MCPS_DATA.indication(uint16_t SrcAddrMode, uint16_t SrcPANId, uint32_t SrcAddr[2], uint16_t DstAddrMode, uint16_t DestPANId, uint32_t DstAddr[2], uint16_t msduLength,uint8_t msdu[100],uint16_t mpduLinkQuality, uint16_t SecurityUse, uint16_t ACLEntry)
+{
+       uint8_t payload[100];
+       uint32_t route_destination_address[2];
+       uint32_t net_addr[2];
+       uint8_t next_hop_index;
+       
+       routing_fields *routing_fields_ptr;
+       routing_fields_ptr = (routing_fields *)&msdu[0];
+       
+       net_addr[1] =(uint32_t) networkaddress;
+       net_addr[0]=0x00000000;
+
+       if ( routing_fields_ptr->destination_address == networkaddress)
+       {
+               //I am the destination
+               memcpy(&payload,&msdu[8],(msduLength-8)*sizeof(uint8_t));
+               
+               //pass data on to APL layer
+               signal NLDE_DATA.indication(routing_fields_ptr->source_address,(uint16_t)(msduLength-8),payload, mpduLinkQuality);
+               
+               return SUCCESS; 
+       }
+       else
+       {
+               //I am not the destination
+               if(device_type != COORDINATOR)
+               {
+                       if( (networkaddress < routing_fields_ptr->destination_address) && (routing_fields_ptr->destination_address < (networkaddress + cskip_routing ) ) )
+                       {
+                               printfUART("route down to appropriate child\n", "");
+                               
+                               //check if destination is one of my children
+                               next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
+
+                               if (next_hop_index == 0)
+                               {
+                                       //destination is not my child
+                                       route_destination_address[0]=0x00000000;
+                                       route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
+                               }
+                               else
+                               {
+                                       //Im routing to my child
+                                       route_destination_address[0]=0x00000000;
+                                       route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
+                               }
+                               //send message
+                               call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
+                       }
+                       else
+                       {
+                               
+                               //changes to meet with the BEACON SYNCHRONIZATION requirements
+                               //route up to the parent
+                               atomic{
+                                       route_destination_address[0]=0x00000000;
+                                       route_destination_address[1]=neighbortable[parent].Network_Address;
+
+                                       call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions_upstream(1,0,0,0,1));
+                               }
+                       }
+               }
+               else
+               {
+                       //I AM THE PAN COORDINATOR
+                       //THE COORDINATOR ALWAYS ROUTES DOWN
+                       
+                       //route down to appropriate child
+                       //check if destination is one of my children
+                       next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
+                       
+                       if (next_hop_index == 0 )
+                       {
+                               //no entry in neigbortable
+                               //calculate route address
+                               route_destination_address[0]=0x00000000;
+                               route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
+                                       
+                               call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
+                       }
+                       else
+                       {
+                               //is my child
+                               route_destination_address[0]=0x00000000;
+                               route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
+
+                               call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
+                       }
+                       
+               }
+               
+               
+       }
+       
+return SUCCESS;
+}
+
+
+/*************************************************************/
+/*******************NLDE IMPLEMENTATION***********************/
+/*************************************************************/
+       
+/*************************************************************/
+/*************************NLDE - DATA*************************/
+/*************************************************************/
+
+//This primitive requests the transfer of a data PDU
+//page 159-161
+
+command error_t NLDE_DATA.request(uint16_t DstAddr, uint16_t NsduLength, uint8_t Nsdu[100], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, uint8_t SecurityEnable)
+{      
+
+       uint32_t srcadd[2];
+       //prefixed size because the devices reset itself when there is an error in the length
+       uint8_t MSDU[100];
+       uint32_t route_destination_address[2];
+       routing_fields *routing_fields_ptr;
+       uint8_t next_hop_index;
+       
+       routing_fields_ptr = (routing_fields *)&MSDU[0];
+       
+       if(joined==0)
+       {       
+               signal NLDE_DATA.confirm(NsduHandle, NWK_INVALID_REQUEST);
+       }
+       else
+       {
+               routing_fields_ptr->frame_control= set_route_frame_control(0x00,0x01,DiscoverRoute,SecurityEnable);
+               routing_fields_ptr->destination_address=DstAddr;
+               routing_fields_ptr->source_address=networkaddress;
+               routing_fields_ptr->radius=Radius;
+               routing_fields_ptr->sequence_number=nwk_IB.nwkSequenceNumber;
+               nwk_IB.nwkSequenceNumber++;
+               
+               memcpy(&MSDU[8],&Nsdu[0],NsduLength*sizeof(uint8_t));
+
+               srcadd[0] = 0x00000000;
+               srcadd[1] = (uint32_t)networkaddress;
+               
+               if (device_type == END_DEVICE)
+               {
+                       //if the device is an end device always sends the message to the parent
+
+                       route_destination_address[0]=0x00000000;
+                       route_destination_address[1]=neighbortable[parent].Network_Address;
+                                                                                                                                                                                                                                                                                                               //ack
+                       call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS,panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions(1,0,0,0));
+                       return SUCCESS;
+               }
+               
+               //send message if the device is the COORDINATOR or a ROUTER
+               if( (networkaddress < routing_fields_ptr->destination_address) && (routing_fields_ptr->destination_address < (networkaddress + cskip_routing ) ) )
+               {
+                       //route down to appropriate child
+                       //check if destination is one of my children
+                       next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
+
+                       if (next_hop_index == 0)
+                       {
+                               //destination is not my child
+                               route_destination_address[0]=0x00000000;
+                               route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
+                       }
+                       else
+                       {
+                               //Im routing to my child
+                               route_destination_address[0]=0x00000000;
+                               route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
+                       }
+                       //send the data                                                                                                                                                                                                                                                                 //ack
+                       call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS, panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions(1,0,0,0));
+               }
+               else
+               {
+                       //route up to parent
+                       atomic{
+                               route_destination_address[0]=0x00000000;
+                               route_destination_address[1]=neighbortable[parent].Network_Address;
+                                                                                                                                                                                                                                                                                                       //ack
+                               call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS, panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions_upstream(1,0,0,0,1));
+                       }
+               }
+               
+       }
+       
+       return SUCCESS;
+}
+
+/*************************************************************/
+/*******************NLME IMPLEMENTATION***********************/
+/*************************************************************/
+
+
+/*************************************************************
+******************* NLME-RESET********************************
+**************************************************************/
+ command error_t NLME_RESET.request(){
+
+  call MLME_RESET.request(TRUE);
+return SUCCESS;
+}
+/*************************************************************/
+/*******************NLME - START - ROUTER*********************/
+/*************************************************************/
+
+//This primitive allows the NHL of a ZigBee Router to initialize or change its superframe configuration.
+//p171 and 210
+command error_t NLME_START_ROUTER.request(uint8_t BeaconOrder, uint8_t SuperframeOrder, bool BatteryLifeExtension,uint32_t StartTime)
+{      
+       //printfUART("NLME_START_ROUTER.request\n", "");
+       
+       device_type = ROUTER;
+       
+       if(TYPE_DEVICE == ROUTER)
+       {
+       
+               //assign current BO and SO
+               beaconorder = BeaconOrder;
+               superframeorder = SuperframeOrder;
+       
+               call MLME_SET.request(MACBEACONORDER, (uint8_t *)&BeaconOrder);
+               
+               call MLME_SET.request(MACSUPERFRAMEORDER, (uint8_t *)&SuperframeOrder);
+               
+               //*******************************************************
+               //***********SET PAN VARIABLES***************************
+               depth=DEVICE_DEPTH;
+               
+               nwk_IB.nwkAvailableAddresses=AVAILABLEADDRESSES;
+               nwk_IB.nwkAddressIncrement= ADDRESSINCREMENT;
+               
+               nwk_IB.nwkMaxChildren=MAXCHILDREN;      //number of children a device is allowed to have on its current network
+               nwk_IB.nwkMaxDepth=MAXDEPTH;    //the depth a device can have
+               nwk_IB.nwkMaxRouters=MAXROUTERS;
+               
+               cskip = Cskip(depth);
+               
+               cskip_routing = Cskip(depth -1);
+               
+               nwk_IB.nwkNextAddress = networkaddress + (cskip * nwk_IB.nwkMaxRouters) + nwk_IB.nwkAddressIncrement;
+       
+               next_child_router_address = networkaddress +((number_child_router-1) * cskip) +1;
+       
+               
+               number_child_router++;
+               
+               
+               printfUART("cskip  %d\n", cskip);
+               printfUART("C %d D %d r %d\n", MAXCHILDREN,MAXDEPTH,MAXROUTERS);
+       
+               //  command error_t request(uint32_t PANId, uint8_t LogicalChannel, uint8_t BeaconOrder, uint8_t SuperframeOrder,bool PANCoordinator,bool BatteryLifeExtension,bool CoordRealignment,bool SecurityEnable);
+               call MLME_START.request(panid,LOGICAL_CHANNEL,BeaconOrder, SuperframeOrder, 0, 0,0,0,StartTime);
+               
+       }
+       else
+       {
+       
+               signal NLME_START_ROUTER.confirm(NWK_INVALID_REQUEST);
+               
+       }
+       return SUCCESS;
+}
+
+
+/*************************************************************/
+/******************NLME - NETWORK - FORMATION*****************/
+/*************************************************************/
+
+//This primitive allows the NHL to request to start a ZigBee network with itself as the coordinator
+//Page 167-169
+command error_t NLME_NETWORK_FORMATION.request(uint32_t ScanChannels, uint8_t ScanDuration, uint8_t BeaconOrder, uint8_t SuperframeOrder, uint16_t PANId, bool BatteryLifeExtension)
+{
+
+       uint8_t v_temp[6];
+
+call Leds.led0On();
+call Leds.led1On();
+call Leds.led2On();
+   
+       v_temp[0] = 0x06;
+       
+       device_type = COORDINATOR;
+       //device_type = ROUTER;
+       
+       call MLME_SET.request(MACMAXBEACONPAYLOADLENGTH,v_temp);
+       
+       //protocol ID
+       v_temp[0] = 0x00;
+       //uint8_t nwk_payload_profile_protocolversion(uint8_t stackprofile,uint8_t nwkcprotocolversion)
+       v_temp[1] = nwk_payload_profile_protocolversion(0x00,0x00);
+       //uint8_t nwk_payload_capacity(uint8_t routercapacity,uint8_t devicedepth,uint8_t enddevicecapacity)
+       v_temp[2] = nwk_payload_capacity(0x01,0x00,0x01);
+       
+       //TX OFFSET (3 bytes)
+       v_temp[3] = 0x56;
+       v_temp[4] = 0x34;
+       v_temp[5] = 0x12;
+       
+       
+       call MLME_SET.request(MACBEACONPAYLOAD,v_temp);
+       
+
+       printfUART("NLME_NETWORK_FORMATION.request\n", "");
+       //perform an energydetection scan
+       //perform an active scan
+       //and select a suitable channel
+       //panid must be less than or equal to 0x3fff
+       
+       //assign current panid
+       panid=PANId;
+       
+       //assign current BO and SO
+       beaconorder = BeaconOrder;
+       superframeorder = SuperframeOrder;
+       
+       call MLME_SET.request(MACBEACONORDER, (uint8_t *)&BeaconOrder);
+
+       call MLME_SET.request(MACSUPERFRAMEORDER, (uint8_t *)&SuperframeOrder);
+       
+       v_temp[0] = (uint8_t)(PANId >> 8);
+       v_temp[1] = (uint8_t)PANId;
+       
+       call MLME_SET.request(MACPANID, v_temp);
+
+       //static assignement of the coordinator address
+       networkaddress=0x0000;//Network address of the ZC of a network always 0x0000;
+       
+       //////printfUART("setting short addr: %i\n", networkaddress);
+       
+       v_temp[0] = (uint8_t)(networkaddress >> 8);
+       v_temp[1] = (uint8_t)(networkaddress);
+
+       
+       call MLME_SET.request(MACSHORTADDRESS,v_temp);
+       
+       
+       //*******************************************************
+       //***********SET PAN VARIABLES***************************
+       //nwk_IB.nwkNextAddress=networkaddress+0x0001;
+       depth=DEVICE_DEPTH;
+       nwk_IB.nwkAvailableAddresses=AVAILABLEADDRESSES;
+       nwk_IB.nwkAddressIncrement= ADDRESSINCREMENT;
+       
+       nwk_IB.nwkMaxChildren=MAXCHILDREN;      //number of children a device is allowed to have on its current network
+       nwk_IB.nwkMaxDepth=MAXDEPTH;    //the depth a device can have
+       nwk_IB.nwkMaxRouters=MAXROUTERS;
+       
+       cskip = Cskip(depth);
+       
+       cskip_routing = Cskip(depth -1 );
+       
+       nwk_IB.nwkNextAddress = networkaddress + (cskip * nwk_IB.nwkMaxRouters) + nwk_IB.nwkAddressIncrement;
+       
+       next_child_router_address = networkaddress +((number_child_router-1) * cskip) +1;
+       
+       number_child_router++;
+       
+       printfUART("cskip  %d\n", cskip);
+       printfUART("C %d D %d r %d\n", MAXCHILDREN,MAXDEPTH,MAXROUTERS);
+       
+       
+       call MLME_START.request(PANId, LOGICAL_CHANNEL,BeaconOrder ,SuperframeOrder,1,0,0,0,0);
+       
+
+
+
+    
+
+
+
+
+       return SUCCESS;
+}
+       
+/*************************************************************/
+/***************NLME - NETWORK - DISCOVERY *******************/
+/*************************************************************/
+
+//This primitive allows the next higher layer to request that the NWK layer discover networks currently operating within the POS.
+//p164 and 210
+command error_t NLME_NETWORK_DISCOVERY.request(uint32_t ScanChannels, uint8_t Scanduration)
+{
+       
+       //ISSUE an MLME_SCAN.request to find the available networks
+       //Temporary descover of the network
+       //Channel Scan is not working properly
+       //manually assign the network descriptor
+       
+       
+       networkdescriptor networkdescriptorlist[1];
+
+       printfUART("2 lauch passive scan\n", "");
+       //The networkdescriptorlist must contain information about every network that was heard
+       
+       //make NetworkDescriptorList out of the PanDescriptorList
+#ifndef TKN154_MAC
+    call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
+#else
+
+
+       networkdescriptorlist[0].PANId=0x1234;
+       networkdescriptorlist[0].LogicalChannel=LOGICAL_CHANNEL;
+       networkdescriptorlist[0].StackProfile=0x00;
+       networkdescriptorlist[0].ZigBeeVersion=0x01;
+       networkdescriptorlist[0].BeaconOrder=7;
+       networkdescriptorlist[0].SuperframeOrder=6;
+       networkdescriptorlist[0].PermitJoining=1;
+       
+       //temporary assignement on the neighbout table of the suitable PAN coordinator
+       if (DEVICE_DEPTH == 0x01)
+               add_neighbortableentry(networkdescriptorlist[0].PANId,D1_PAN_EXT0,D1_PAN_EXT1,D1_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
+       if (DEVICE_DEPTH == 0x02)
+               add_neighbortableentry(networkdescriptorlist[0].PANId,D2_PAN_EXT0,D2_PAN_EXT1,D2_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
+       if (DEVICE_DEPTH == 0x03)
+               add_neighbortableentry(networkdescriptorlist[0].PANId,D3_PAN_EXT0,D3_PAN_EXT1,D3_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
+       if (DEVICE_DEPTH == 0x04)
+               add_neighbortableentry(networkdescriptorlist[0].PANId,D4_PAN_EXT0,D4_PAN_EXT1,D4_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
+#endif 
+               
+       signal NLME_NETWORK_DISCOVERY.confirm(1,networkdescriptorlist, NWK_SUCCESS);
+
+       return SUCCESS;
+}
+
+/*************************************************************/
+/************************NLME - JOIN**************************/
+/*************************************************************/
+//This primitive allows the NHL to request to join a network either through association.
+//p173 and 210
+command error_t NLME_JOIN.request(uint16_t PANId, bool JoinAsRouter, bool RejoinNetwork, uint32_t ScanChannels, uint8_t ScanDuration, uint8_t PowerSource, uint8_t RxOnWhenIdle, uint8_t MACSecurity)
+{      
+
+       //Assume we have selected a suitable parent and all previous conditions were true
+       uint32_t destinaddress[2];
+       
+       printfUART("9 find parent\n", "");
+       
+       //list_neighbourtable();
+       
+       parent_index = find_suitable_parent();
+       
+       panid = PANId;
+       
+       //printfUART("NLME_JOIN %i %i\n", parent_index,panid); 
+       
+       if(parent_index == 0)
+       {
+               signal NLME_JOIN.confirm(PANId,NWK_NOT_PERMITTED);
+       }
+       else
+       {
+               //assign the true value to parent index
+               parent_index = parent_index - 1;
+                               
+               //destinaddress[0]=neighbortable[parent_index].Extended_Address0;
+               //destinaddress[1]=neighbortable[parent_index].Extended_Address1;
+               //verificar o endere�o do pan coordinator
+               destinaddress[0]=0x00000000;
+               
+               
+               destinaddress[1] = neighbortable[parent_index].Network_Address;
+#ifdef TKN154_MAC              
+               
+               if (DEVICE_DEPTH == 0x01)
+                       destinaddress[1]=D1_PAN_SHORT;
+               if (DEVICE_DEPTH == 0x02)
+                       destinaddress[1]=D2_PAN_SHORT;
+               if (DEVICE_DEPTH == 0x03)
+                       destinaddress[1]=D3_PAN_SHORT;
+               if (DEVICE_DEPTH == 0x04)
+                       destinaddress[1]=D4_PAN_SHORT;
+                       
+#endif                 
+               printfUART("10 associate to %i\n", destinaddress[1]);   
+               //set_capability_information(uint8_t alternate_PAN_coordinator, uint8_t device_type, uint8_t power_source, uint8_t receiver_on_when_idle, uint8_t security, uint8_t allocate_address)
+               
+               if (JoinAsRouter == 0x01)
+                       call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,PANId,destinaddress, set_capability_information(JoinAsRouter,0x01,PowerSource,RxOnWhenIdle,MACSecurity,0x01),0);
+               else
+               {
+               
+                       printfUART("11 go ass\n", "");  
+               
+                       coordinator_addr[0]=0x00000000;
+                       coordinator_addr[1] = neighbortable[parent_index].Network_Address;
+                       //BUILD the PAN descriptor of the COORDINATOR
+                       //assuming that the adress is short
+                       pan_des.CoordAddrMode = SHORT_ADDRESS;
+                       pan_des.CoordPANId = panid;
+                       pan_des.CoordAddress0=0x00000000;
+                       pan_des.CoordAddress1=(uint32_t)neighbortable[parent_index].Network_Address;
+                       pan_des.LogicalChannel=neighbortable[parent_index].Logical_Channel;
+                       //superframe specification field
+                       //pan_des.SuperframeSpec = neighbortable[parent_index].SuperframeSpec;
+
+                       pan_des.GTSPermit=0x01;
+                       pan_des.LinkQuality=0x00;
+                       pan_des.TimeStamp=0x000000;
+                       pan_des.SecurityUse=0;
+                       pan_des.ACLEntry=0x00;
+                       pan_des.SecurityFailure=0x00;
+       
+                       received_beacon_count=0;
+                       go_associate=1;
+#ifdef TKN154_MAC
+
+                       
+                       //call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,PANId,destinaddress, set_capability_information(JoinAsRouter,0x00,PowerSource,RxOnWhenIdle,MACSecurity,0x01),0);
+#endif
+               }
+       }
+       
+       return SUCCESS;
+}
+
+
+/*************************************************************/
+/************************NLME - LEAVE*************************/
+/*************************************************************/
+
+//This primitive allows the NHL to request that it or another device leaves the network
+//page 181-183
+command error_t NLME_LEAVE.request(uint32_t DeviceAddress[],bool RemoveChildren, bool MACSecurityEnable)
+{      
+       uint32_t devaddr[2];
+       ////printfUART("NLME_LEAVE.request\n", ""); 
+       if (DeviceAddress == 0)//child asked to leave
+       {       
+               if(RemoveChildren == 0)//implemented like it is always 0
+               {       
+                       //send leave request command frame: RemoveChildren subfield=0 of the command option field of the command frame payload
+                       //call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
+                       devaddr[0]=neighbortable[parent].Extended_Address0;
+                       devaddr[1]=neighbortable[parent].Extended_Address1;
+                       call MLME_DISASSOCIATE.request(devaddr,0x02,0);
+               }
+               else
+               {
+                       //send leave request command frame: RemoveChildren subfield=1
+                       //try to remove the children, call NLME_LEAVE.request(uint32_t DeviceAddress[]=address of child,bool RemoveChildren, bool MACSecurityEnable)
+                       //call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
+               }
+       }
+       else//parent forced a child to leave
+       {
+               //if(check_neighbortableentry(DeviceAddress[0], DeviceAddress[1]) == 0)
+               //{
+               //      signal NLME_LEAVE.confirm(DeviceAddress,NWK_UNKNOWN_DEVICE);
+               //      //call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
+               //}
+               
+       }
+       
+       
+       return SUCCESS;
+}
+
+/*************************************************************/
+/************************NLME - SYNC**************************/
+/*************************************************************/
+
+//This primitive allows the NHL to synchronize or extract data from its ZigBee coordinator or router
+//page 186-187
+command error_t NLME_SYNC.request(bool Track)
+{
+//call MLME_SET.request(0x00,0x15);
+ // call MLME_SET.request(MACCOORDSHORTADDRESS, 0x0000);
+  //   call MLME_SET.request(0x50,0x1234);
+
+  call MLME_SYNC.request(LOGICAL_CHANNEL,1);
+       return SUCCESS;
+}
+  
+/*************************************************************/
+/*****************        NLME-SET     ********************/
+/*************************************************************/
+       
+command error_t NLME_SET.request(uint8_t NIBAttribute, uint16_t NIBAttributeLength, uint16_t NIBAttributeValue)
+{
+
+       atomic{
+       
+               switch(NIBAttribute)
+               {
+                       case NWKSEQUENCENUMBER :                nwk_IB.nwkSequenceNumber = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkSequenceNumber: %x\n",nwk_IB.nwkSequenceNumber);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKPASSIVEACKTIMEOUT :             nwk_IB.nwkPassiveAckTimeout = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkPassiveAckTimeout: %x\n",nwk_IB.nwkPassiveAckTimeout);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                       
+                       
+                       case NWKMAXBROADCASTRETRIES :   nwk_IB.nwkMaxBroadcastRetries = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkMaxBroadcastRetries: %x\n",nwk_IB.nwkMaxBroadcastRetries);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKMAXCHILDREN :                   nwk_IB.nwkMaxChildren = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkMaxChildren: %x\n",nwk_IB.nwkMaxChildren);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKMAXDEPTH :                              nwk_IB.nwkMaxDepth = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkMaxDepth: %x\n",nwk_IB.nwkMaxDepth);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+       
+                       case NWKMAXROUTERS :                    nwk_IB.nwkMaxRouters = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkMaxRouters: %x\n",nwk_IB.nwkMaxRouters);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+       
+                       case NWKMETWORKBROADCASTDELIVERYTIME :          nwk_IB.nwkNetworkBroadcastDeliveryTime = (uint8_t) NIBAttributeValue;
+                                                                                                               //////printfUART("nwk_IB.nwkNetworkBroadcastDeliveryTime: %x\n",nwk_IB.nwkNetworkBroadcastDeliveryTime);
+                                                                                                               signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                                               break;
+                       case NWKREPORTCONSTANTCOST :    nwk_IB.nwkReportConstantCost = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkReportConstantCost: %x\n",nwk_IB.nwkReportConstantCost);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                       case NWKROUTEDISCOVERYRETRIESPERMITED :         nwk_IB.nwkRouteDiscoveryRetriesPermitted = (uint8_t) NIBAttributeValue;
+                                                                                                               //////printfUART("nwk_IB.nwkRouteDiscoveryRetriesPermitted: %x\n",nwk_IB.nwkRouteDiscoveryRetriesPermitted);
+                                                                                                               signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                                               break;
+       
+                       case NWKSYMLINK :                               nwk_IB.nwkSymLink = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkSymLink: %x\n",nwk_IB.nwkSymLink);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                       
+                       case NWKCAPABILITYINFORMATION : nwk_IB.nwkCapabilityInformation = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkCapabilityInformation: %x\n",nwk_IB.nwkCapabilityInformation);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKUSETREEADDRALLOC :              nwk_IB.nwkUseTreeAddrAlloc = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkUseTreeAddrAlloc: %x\n",nwk_IB.nwkUseTreeAddrAlloc);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKUSETREEROUTING :                nwk_IB.nwkUseTreeRouting = (uint8_t) NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkUseTreeRouting: %x\n",nwk_IB.nwkUseTreeRouting);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKNEXTADDRESS :                   nwk_IB.nwkNextAddress = NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkNextAddress: %x\n",nwk_IB.nwkNextAddress);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKAVAILABLEADDRESSES :    nwk_IB.nwkAvailableAddresses = NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkAvailableAddresses: %x\n",nwk_IB.nwkAvailableAddresses);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKADDRESSINCREMENT :              nwk_IB.nwkAddressIncrement =NIBAttributeValue;
+                                                                                       //////printfUART("nwk_IB.nwkAddressIncrement: %x\n",nwk_IB.nwkAddressIncrement);
+                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                       break;
+                                                                                       
+                       case NWKTRANSACTIONPERSISTENCETIME :    nwk_IB.nwkTransactionPersistenceTime = (uint8_t) NIBAttributeValue;
+                                                                                                       //////printfUART("nwk_IB.nwkTransactionPersistenceTime: %x\n",nwk_IB.nwkTransactionPersistenceTime);
+                                                                                                       signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
+                                                                                                       break;
+                                                                                       
+                       default:                                                signal NLME_SET.confirm(NWK_UNSUPPORTED_ATTRIBUTE,NIBAttribute);
+                                                                                       break;
+                       
+               }
+               
+       }
+
+
+       return SUCCESS;
+}
+
+/*************************************************************/
+/*****************        NLME-GET     ********************/
+/*************************************************************/
+       
+command error_t NLME_GET.request(uint8_t NIBAttribute)
+{
+       switch(NIBAttribute)
+       {
+               case NWKSEQUENCENUMBER :                signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkSequenceNumber);
+                                                                               break;
+                                                                       
+               case NWKPASSIVEACKTIMEOUT :             signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkPassiveAckTimeout);
+                                                                               break;
+               
+               case NWKMAXBROADCASTRETRIES :   signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxBroadcastRetries);
+                                                                               break;
+
+               case NWKMAXCHILDREN :                   signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxChildren);
+                                                                               break;
+
+               case NWKMAXDEPTH :                              signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxDepth);
+                                                                               break;
+
+               case NWKMAXROUTERS :                    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxRouters);
+                                                                               break;
+
+               case NWKMETWORKBROADCASTDELIVERYTIME :          signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkNetworkBroadcastDeliveryTime);
+                                                                                                       break;
+
+               case NWKREPORTCONSTANTCOST :    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkReportConstantCost);
+                                                                               break;
+
+               case NWKROUTEDISCOVERYRETRIESPERMITED :         signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkRouteDiscoveryRetriesPermitted);
+                                                                                                       break;
+
+               case NWKSYMLINK :                               signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkSymLink);
+                                                                               break;
+               
+               case NWKCAPABILITYINFORMATION : signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkCapabilityInformation);
+                                                                               break;
+                                                                               
+               case NWKUSETREEADDRALLOC :              signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkUseTreeAddrAlloc);
+                                                                               break;
+                                                                               
+               case NWKUSETREEROUTING :                signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkUseTreeRouting);
+                                                                               break;
+                                                                               
+               case NWKNEXTADDRESS :                   signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkNextAddress);
+                                                                               break;
+
+               case NWKAVAILABLEADDRESSES :    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkAvailableAddresses);
+                                                                               break;
+                                                                               
+               case NWKADDRESSINCREMENT :              signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkAddressIncrement);
+                                                                               break;
+                                                                               
+               case NWKTRANSACTIONPERSISTENCETIME :    signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkTransactionPersistenceTime);
+                                                                                               break;
+
+               default:                                                signal NLME_GET.confirm(NWK_UNSUPPORTED_ATTRIBUTE,NIBAttribute,0x0000,0x00);
+                                                                               break;
+       }
+       
+       return SUCCESS;
+}
+
+
+/*************************************************************/
+/**************neighbor table management functions************/
+/*************************************************************/
+
+//check if a specific neighbourtable Entry is present
+//Return 0:Entry is not present
+//Return i:Entry is present and return the index of the entry + 1
+       uint8_t check_neighbortableentry(uint8_t addrmode,uint32_t Address0,uint32_t Address1)
+       {       
+               
+               int i=0;
+               
+               
+               //printfUART("neighbourtable check c %i\n", neighbour_count);
+       
+               if (neighbour_count == 0)
+               {
+                       //printfUART("no neib\n", "");
+                       return 0;
+               }
+               
+               if(addrmode == SHORT_ADDRESS)
+               {       
+                       for(i=0; i < neighbour_count; i++)
+                       {       
+                               ///printfUART("compare %i %i\n", neighbortable[i].Network_Address, test);
+                               
+                               if(neighbortable[i].Network_Address == (uint16_t) Address0)
+                               {
+                                       //printfUART("already present \n", "" );
+                                       return i+1;
+                               }
+                       }
+                       return 0;
+               }
+               else
+               {
+                       for(i=0; i<neighbour_count; i++)
+                       {       
+                               ////printfUART("compare %x %x\n", neighbortable[i].Extended_Address0, Address0);
+                               if( (neighbortable[i].Extended_Address0 == Address0) && (neighbortable[i].Extended_Address1 == Address1))
+                               {
+                                       //printfUART("already present \n", "" );
+                                       return i+1;
+                               }
+                       }
+                       return 0;
+               }
+               
+               return 0;
+       }
+       
+       //function user to find a parent in the neighbour table
+       //Return 0:no parent is present
+       //Return i:parent is present and return the index of the entry + 1
+       uint8_t find_suitable_parent()
+       {
+               int i =0;
+               for (i=0; i<neighbour_count; i++)
+               {
+                       if(neighbortable[i].Relationship == NEIGHBOR_IS_PARENT)
+                       {
+                               return i+1;
+                       }
+               }
+               return 0;
+       }
+
+
+       //add a neighbortable entry     
+       void add_neighbortableentry     (uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship)
+       {
+       
+       atomic{
+               neighbortable[neighbour_count].PAN_Id=PAN_Id;
+               neighbortable[neighbour_count].Extended_Address0=Extended_Address0;
+               neighbortable[neighbour_count].Extended_Address1=Extended_Address1;
+               neighbortable[neighbour_count].Network_Address=Network_Address;
+               neighbortable[neighbour_count].Device_Type=Device_Type;
+               neighbortable[neighbour_count].Relationship=Relationship;
+               
+               neighbour_count++;
+               }
+               
+       }
+
+
+       
+       //update a neighbourtable entry
+       void update_neighbortableentry(uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship)
+       {
+               //search entry with correct extended address
+               uint8_t nrofEntry=0;
+               nrofEntry = check_neighbortableentry(0x01,Extended_Address0,Extended_Address1) - 1;
+               
+               //update every attribute
+               neighbortable[nrofEntry].PAN_Id=PAN_Id;
+               neighbortable[nrofEntry].Extended_Address0=Extended_Address0;
+               neighbortable[nrofEntry].Extended_Address1=Extended_Address1;
+               neighbortable[nrofEntry].Network_Address=Network_Address;
+               neighbortable[nrofEntry].Device_Type=Device_Type;
+               neighbortable[nrofEntry].Relationship=Relationship;
+               
+               ////printfUART("updated neighbourtable entry\n", "");
+       }
+
+
+       void list_neighbourtable()
+       {
+               int i;
+               ////printfUART("N List Count: %u\n", neighbour_count);
+       
+               for(i=0;i<neighbour_count;i++)
+               {
+                       printfUART("Panid %x", neighbortable[i].PAN_Id);
+                       printfUART("Extaddr0 %x", neighbortable[i].Extended_Address0);
+                       printfUART("Extaddr1 %x", neighbortable[i].Extended_Address1);
+                       printfUART("nwkaddr %u", neighbortable[i].Network_Address);
+                       printfUART("devtype %u", neighbortable[i].Device_Type);
+                       printfUART("relation %u", neighbortable[i].Relationship);
+                       printfUART("depth %u\n", neighbortable[i].Depth);               
+               }
+       }
+/*************************************************************/
+/*****************Address Assignment functions****************/
+/*************************************************************/
+
+//calculate the size of the address sub-block for a router at depth d
+uint16_t Cskip(uint8_t d)
+{      
+       uint8_t skip;
+       uint8_t power;
+       uint8_t x=1;
+       uint8_t Cm = nwk_IB.nwkMaxChildren;
+       uint8_t Rm = nwk_IB.nwkMaxRouters;
+       uint8_t Lm = nwk_IB.nwkMaxDepth;
+       /*////printfUART("nwk_IB.nwkMaxChildren: %i\n",  nwk_IB.nwkMaxChildren);
+       ////printfUART("nwk_IB.nwkMaxRouters: %i\n", nwk_IB.nwkMaxRouters);
+       ////printfUART("nwk_IB.nwkMaxDepth: %i\n", nwk_IB.nwkMaxDepth);*/
+       if (Rm == 1)
+       {
+               skip = 1 + Cm * (Lm - d - 1);
+       }
+       else
+       {       
+               int i;
+               
+               power=(Lm - d - 1);
+               for(i=0;i<power;i++)
+               {
+                       x=x*Rm;
+               }
+               skip = (1 + Cm - Rm - (Cm * x))/(1-Rm);
+       }
+       ////printfUART("Cksip function calculated: %i\n", skip);
+       return skip;
+}
+
+//Calculate the nexthopaddress of the appropriate child if route down is required
+uint16_t nexthopaddress(uint16_t destinationaddress,uint8_t d)
+{
+       uint16_t next_hop;
+
+       next_hop=(networkaddress + 1 + ((destinationaddress-(networkaddress+1))/cskip)* cskip);
+       
+       ////printfUART("Nexthop address calculated: %i\n", next_hop);
+       return next_hop;
+}
+
+/*************************************************************/
+/*****************Initialization functions********************/
+/*************************************************************/
+       //initialization of the nwk IB
+       void init_nwkIB()
+       {       
+               //nwk IB default values p 204-206
+               nwk_IB.nwkPassiveAckTimeout=0x03;
+               nwk_IB.nwkMaxBroadcastRetries=0x03;
+               nwk_IB.nwkMaxChildren=0x07;     //number of children a device is allowed to have on its current network
+               nwk_IB.nwkMaxDepth=0x05;        //the depth a device can have
+               nwk_IB.nwkMaxRouters=0x02;      //number of routers anyone device is allowed to have on its current network
+               //neighbortableentry nwkNeighborTable[];//null set
+               nwk_IB.nwkNetworkBroadcastDeliveryTime=( nwk_IB.nwkPassiveAckTimeout * nwk_IB.nwkMaxBroadcastRetries );
+               nwk_IB.nwkReportConstantCost=0x00;
+               nwk_IB.nwkRouteDiscoveryRetriesPermitted=nwkcDiscoveryRetryLimit;
+       //set? nwkRouteTable;//Null set
+               nwk_IB.nwkSymLink=0;
+               nwk_IB.nwkCapabilityInformation=0x00;
+               nwk_IB.nwkUseTreeAddrAlloc=1;
+               nwk_IB.nwkUseTreeRouting=1;
+               nwk_IB.nwkNextAddress=0x0000;
+               nwk_IB.nwkAvailableAddresses=0x0000;
+               nwk_IB.nwkAddressIncrement=0x0001;
+               nwk_IB.nwkTransactionPersistenceTime=0x01f4;
+       }
+
+
+  
+}
+
index 76b9a6a8c8760ce08d3dfa2ce25ec407aa6b235e..b5df87282fa5fa44fc9b2fa82117204771b694d7 100644 (file)
 
 //SELECTED DEVICE TYPE
 
-//#define TYPE_DEVICE END_DEVICE
+#define TYPE_DEVICE END_DEVICE
 //#define TYPE_DEVICE ROUTER
-#define TYPE_DEVICE COORDINATOR
+//#define TYPE_DEVICE COORDINATOR
 
 //test definitions
-#define DEVICE_DEPTH 0x00
+#define DEVICE_DEPTH 0x01
 
 //used to operate in a fixed channel
-#define LOGICAL_CHANNEL 0x15
+#define LOGICAL_CHANNEL 0x1A
 
 //PAN VARIABLES
 //conflict error