From: rarseverino Date: Fri, 18 Sep 2009 15:58:21 +0000 (+0000) Subject: *** empty log message *** X-Git-Tag: rc_6_tinyos_2_1_1~240 X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=commitdiff_plain;h=46fb6669bc2ad9723739e93a7d76dcef283a97a5;p=tinyos-2.x.git *** empty log message *** --- diff --git a/tos/lib/net/zigbee/apps/Test_APL/Makefile b/tos/lib/net/zigbee/apps/Test_APL/Makefile index 086b607e..31172b4b 100644 --- a/tos/lib/net/zigbee/apps/Test_APL/Makefile +++ b/tos/lib/net/zigbee/apps/Test_APL/Makefile @@ -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 index 0e2f0931..00000000 --- a/tos/lib/net/zigbee/apps/Test_APL/Test_APL.nc +++ /dev/null @@ -1,74 +0,0 @@ -/* - * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise - * @author Andre Cunha - * - */ -#include - -#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 index 00000000..cf230138 --- /dev/null +++ b/tos/lib/net/zigbee/apps/Test_APL/Test_APLC.nc @@ -0,0 +1,74 @@ +/* + * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise + * @author Andre Cunha + * + */ +#include + +#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 index c01bcaa6..00000000 --- a/tos/lib/net/zigbee/apps/Test_APL/Test_APLM.nc +++ /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 -#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 as T_init; - - uses interface Timer as T_test; - - uses interface Timer as T_schedule; - -//user button - uses interface Get; - uses interface Notify; - -} -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 index 00000000..801f7a35 --- /dev/null +++ b/tos/lib/net/zigbee/apps/Test_APL/Test_APLP.nc @@ -0,0 +1,885 @@ +//#define OPEN_ZB_MAC + +/* + * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise + * @author Andre Cunha + * + */ +#include +#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 as T_init; + + uses interface Timer as T_test; + + uses interface Timer as T_schedule; + +//user button + uses interface Get; + uses interface Notify; + +} +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(); + + } + +} + + + +} + diff --git a/tos/lib/net/zigbee/ieee802154/nwk/Makefile b/tos/lib/net/zigbee/ieee802154/nwk/Makefile index 55edac20..d06d4ae4 100644 --- a/tos/lib/net/zigbee/ieee802154/nwk/Makefile +++ b/tos/lib/net/zigbee/ieee802154/nwk/Makefile @@ -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 index a2353257..00000000 --- a/tos/lib/net/zigbee/ieee802154/nwk/NWK.nc +++ /dev/null @@ -1,141 +0,0 @@ -/* - * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise - * @author Andre Cunha - * - */ -#include - -#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 index 00000000..a3b313c3 --- /dev/null +++ b/tos/lib/net/zigbee/ieee802154/nwk/NWKC.nc @@ -0,0 +1,141 @@ +/* + * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise + * @author Andre Cunha + * + */ +#include + +#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 index 2a9c49b0..00000000 --- a/tos/lib/net/zigbee/ieee802154/nwk/NWKM.nc +++ /dev/null @@ -1,1539 +0,0 @@ -/* - * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise - * @author Andre Cunha - * - */ -#include -#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 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 +#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 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