Exemple #1
0
/** Attempts to start the ZNP.
@param the type of device we're starting, e.g. ROUTER
@post znpResult contains the ZNP library error code, if any.
@return ZNP_SUCCESS if successful, else an error code indicating where it failed.
@see Communications Examples for more information about each of these steps.
*/
signed int startZnp(unsigned char deviceType)
{
    printf("ZNP Startup ");
    
    znpInit(); 
    if (znpResult != ZNP_SUCCESS) 
        return -1; 
    
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    if (znpResult != ZNP_SUCCESS) 
        return -2; 
    
    znpReset();
    if (znpResult != ZNP_SUCCESS) 
        return -3; 
    
    setZigbeeDeviceType(deviceType);
    if (znpResult != ZNP_SUCCESS) 
        return -4; 
    
    setChannel(DEFAULT_CHANNEL);
    if (znpResult != ZNP_SUCCESS) 
        return -5; 
    
    setCallbacks(CALLBACKS_ENABLED);
    if (znpResult != ZNP_SUCCESS) 
        return -6; 
    
    afRegisterGenericApplication();
    if (znpResult != ZNP_SUCCESS) 
        return -8; 
    
    zdoStartApplication();
    if (znpResult != ZNP_SUCCESS) 
        return -9; 
    
    /* Wait until this device has joined a network.
    Device State will change to DEV_ROUTER or DEV_COORD to indicate that the device has correctly joined a network. */
    unsigned char deviceState = 0;
    switch (deviceType)
    {
    case ROUTER: deviceState = DEV_ROUTER; break;
    case END_DEVICE: deviceState = DEV_END_DEVICE; break;
    case COORDINATOR: deviceState = DEV_ZB_COORD; break;
    default: printf("ERROR - UNKNOWN DEVICE TYPE\r\n"); return -10;
    }
    
    znpResult = waitForDeviceState(deviceState);
    if (znpResult != ZNP_SUCCESS) 
        return -10; 
    
    return ZNP_SUCCESS;
}
int main( void )
{
    halInit();
    printf("\r\n****************************************************\r\n");
    printf("Packet Error Rate Tester - COORDINATOR - using AFZDO\r\n");
    buttonIsr = &handleButtonPress;       
    HAL_ENABLE_INTERRUPTS();
    setLed(0);
    /* Initialize the ZNP */
    printf("Initializing the ZNP\r\n");    
    znpInit(); 
    handleReturnValue();
    
    /* Set Startup Options (will restore the ZNP to default values on reset) */
    printf("Setting StartupOptions\r\n");
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    handleReturnValue();
    
    /* Reset the ZNP */
    printf("Reset the ZNP\r\n");    
    znpReset();
    handleReturnValue();
    
    /* Set Zigbee Device Type to be COORDINATOR */
    printf("Setting Zigbee Device Type\r\n"); 
    setZigbeeDeviceType(COORDINATOR);
    handleReturnValue();
    
    /* Reset the ZNP */
    printf("Reset the ZNP\r\n");    
    znpReset();
    handleReturnValue();
    
    /* Configure the ZNP for our application */
    printf("Registering Application\r\n");   
    afRegisterGenericApplication();
    handleReturnValue();
    
    /* Now, start the application. We will receive a START_REQUEST_SRSP, and then if it is successful, a START_CONFIRM. */
    printf("Starting the Application\r\n"); 
    zdoStartApplication();
    handleReturnValue();
    
    /** Wait until we get on the network. 
    We will receive a ZDO_STATE_CHANGE_IND message whenever the state changes. */
    waitForDeviceState(DEV_ZB_COORD);

    printf("On Network!\r\n");
    setLed(1);
    
    /* On network, display info about this network */
#ifdef DISPLAY_NETWORK_INFORMATION     
    getNetworkConfigurationParameters();                
    getDeviceInformation();
#endif  
    
    printf("Silently listening for packets; press button to display count\r\n");
    printf("Count = %u; Start router sending packets\r\n", packetCounter);
    
    /* Now the network is running - continually poll for any received messages from the ZNP */
    waitForPackets();
}
/**
Start the Module and join a network, using the AF/ZDO interface. Reads RF Operating Region (US/EU) from GPIO.
@param moduleConfiguration the settings to use to start the module
@param applicationConfiguration the settings to use to start the Zigbee Application
@see struct moduleConfiguration in module_utilities.h for information about each field of the moduleConfiguration
@see struct applicationConfiguration in application_configuration.h for information about each field of the applicationConfiguration
@see module.c for more information about each of these steps.
*/
moduleResult_t startModule(const struct moduleConfiguration* mc, const struct applicationConfiguration* ac)
{
	printf("Module Startup\r\n");
    /* Initialize the Module */
    RETURN_RESULT_IF_FAIL(moduleReset(), METHOD_START_MODULE);
    /* Clear out any old network or state information (if requested) */
    RETURN_RESULT_IF_FAIL(setStartupOptions(mc->startupOptions), METHOD_START_MODULE);   

/* This section is for reading Operating region from GPIO pins - omit if using different method */
    /* First, configure GPIOs as inputs: */
    RETURN_RESULT_IF_FAIL(sysGpio(GPIO_SET_DIRECTION , GPIO_DIRECTION_ALL_INPUTS), METHOD_START_MODULE);    
    RETURN_RESULT_IF_FAIL(sysGpio(GPIO_SET_INPUT_MODE , GPIO_INPUT_MODE_ALL_PULL_UPS), METHOD_START_MODULE); 
    /* Now, read GPIO inputs 0 & 1: */
    RETURN_RESULT_IF_FAIL(sysGpio(GPIO_READ, (GPIO_0 | GPIO_1)), METHOD_START_MODULE);
    /* The operating region (US vs. EU etc.) is based on DIP Switch settings, read from GPIO 0 & 1 */
    uint16_t moduleRegion = zmBuf[SYS_GPIO_READ_RESULT_FIELD];
    
/*** Done reading GPIO inputs. If application needs them as outputs then configure accordingly ***/
    
    /* Reset the Module to apply the changes we just set */
    RETURN_RESULT_IF_FAIL(moduleReset(), METHOD_START_MODULE);
    /* Read the productId - this indicates the model of module used. */
    uint8_t productId = zmBuf[SYS_RESET_IND_PRODUCTID_FIELD]; 
    /* If this is not valid (bad firmware) then stop */
    RETURN_RESULT_IF_EXPRESSION_TRUE((productId < MINIMUM_BUILD_ID), METHOD_START_MODULE,
                                     ZM_INVALID_MODULE_CONFIGURATION);       
    /* Configure the module's RF output */
    setModuleRfPower(productId, moduleRegion);
    /* Set any end device options */
    if (mc->deviceType == END_DEVICE)
	    RETURN_RESULT_IF_FAIL(setPollRate(mc->endDevicePollRate), METHOD_START_MODULE);
    /* Configure which RF Channels to use. If none set then this will default to 11. */
    RETURN_RESULT_IF_FAIL(setZigbeeDeviceType(mc->deviceType), METHOD_START_MODULE);     // Set Zigbee Device Type
    RETURN_RESULT_IF_FAIL(setChannelMask(mc->channelMask), METHOD_START_MODULE);
    RETURN_RESULT_IF_FAIL(setPanId(mc->panId), METHOD_START_MODULE);
    RETURN_RESULT_IF_FAIL(setCallbacks(CALLBACKS_ENABLED), METHOD_START_MODULE);
    
	                                                    // Note: ZCD_NV_SECURITY_MODE defaults to 01
	  if (mc->securityMode != SECURITY_MODE_OFF)        // Note: If a coordinator has ZCD_NV_SECURITY_MODE = 00, router must have ZCD_NV_SECURITY_MODE = 01 or else they won't communicate
	  {
	      RETURN_RESULT_IF_FAIL(setSecurityMode(mc->securityMode), METHOD_START_MODULE);
	      RETURN_RESULT_IF_FAIL(setSecurityKey(mc->securityKey), METHOD_START_MODULE);
	  }

	  if (ac == GENERIC_APPLICATION_CONFIGURATION)	  //TODO: use custom applicationFramework if this isn't null:
	  {
	    RETURN_RESULT_IF_FAIL(afRegisterGenericApplication(), METHOD_START_MODULE);    // Configure the Module for our application
	  } else {
	    printf("Custom Application Frameworks not supported\r\n");
	    return INVALID_PARAMETER;
	  }
	  RETURN_RESULT_IF_FAIL(zdoStartApplication(), METHOD_START_MODULE);		// Start your engines

	  /* Wait until this device has joined a network. Device State will change to DEV_ROUTER,
      DEV_END_DEVICE, or DEV_COORD to indicate that the device has correctly joined a network. */

	#ifdef ZDO_STATE_CHANGE_IND_HANDLED_BY_APPLICATION  //if you're handling this in your application instead...
	  return MODULE_SUCCESS;
	#else
	#define TEN_SECONDS 10000
	#define FIFTEEN_SECONDS 15000
	#define START_TIMEOUT FIFTEEN_SECONDS
	  RETURN_RESULT(waitForDeviceState(getDeviceStateForDeviceType(mc->deviceType), START_TIMEOUT), METHOD_START_MODULE);
	#endif

}
/**
Starts module using an operating region as a parameter. This does NOT read the GPIO pin to set the region.
@param mc the module configuration - what RF channel, which PAN ID, etc. These options are used in
this expressStartModule function as arguments to the various functions.
@param ac the Zigbee application configuration - which endpoint to use and other global settings. 
If not using GENERIC_APPLICATION_CONFIGURATION then you must #define the compilation option 
SUPPORT_CUSTOM_APPLICATION_CONFIGURATION. Normally this option is NOT defined to reduce code size.
@param moduleRegion - which region of the world to use to ensure FCC/ETSI compliance.
*/
moduleResult_t expressStartModule(const struct moduleConfiguration* mc, const struct applicationConfiguration* ac, const uint8_t moduleRegion)
{
    printf("Express Startup ");
    
    /* Initialize the Module */
    RETURN_RESULT_IF_FAIL(moduleReset(), METHOD_EXPRESS_START_MODULE);
    
    /* Clear out any old network or state information (if requested) */
    printf("Startup Options 0x%02X\r\n", mc->startupOptions);
    RETURN_RESULT_IF_FAIL(setStartupOptions(mc->startupOptions), METHOD_EXPRESS_START_MODULE);

    /* Reset the Module to apply the changes we just set */
    RETURN_RESULT_IF_FAIL(moduleReset(), METHOD_EXPRESS_START_MODULE);
    
    /* Read the productId - this indicates the model of module used. */
    uint8_t productId = zmBuf[SYS_RESET_IND_PRODUCTID_FIELD]; 
    
    /* If this is not valid (bad firmware) then stop */
    RETURN_RESULT_IF_EXPRESSION_TRUE((productId < MINIMUM_BUILD_ID), METHOD_EXPRESS_START_MODULE, ZM_INVALID_MODULE_CONFIGURATION); 
    
    /* Configure the module's RF output */ 
    setModuleRfPower(productId, moduleRegion);
    
    /* Set any end device options */
    if (mc->deviceType == END_DEVICE)
    {
	    RETURN_RESULT_IF_FAIL(setPollRate(mc->endDevicePollRate), METHOD_EXPRESS_START_MODULE);
    }
    /* Override for testing low power operation - referenced in Basic Comms ED example*/
#ifdef DISABLE_END_DEVICE_POLLING
    RETURN_RESULT_IF_FAIL(setPollRate(0), METHOD_EXPRESS_START_MODULE);
#endif
    
    /* Configure the Zigbee Device Type (Coordinator, Router, End Device */
    RETURN_RESULT_IF_FAIL(setZigbeeDeviceType(mc->deviceType), METHOD_EXPRESS_START_MODULE);

    /* Configure which RF Channels to use. If none set then this will default to a default set. */
    RETURN_RESULT_IF_FAIL(setChannelMask(mc->channelMask), METHOD_EXPRESS_START_MODULE);
    
    /* Set the PAN ID, if you want to restrict the module to only a particular PAN ID. */
    RETURN_RESULT_IF_FAIL(setPanId(mc->panId), METHOD_EXPRESS_START_MODULE);
    RETURN_RESULT_IF_FAIL(setCallbacks(CALLBACKS_ENABLED), METHOD_EXPRESS_START_MODULE);
    
    /* Set security mode and security key if required. Note: If a coordinator has 
    ZCD_NV_SECURITY_MODE = 00 then router must have ZCD_NV_SECURITY_MODE = 01 or else they won't communicate */
    if (mc->securityMode != SECURITY_MODE_OFF)        
    {
    	RETURN_RESULT_IF_FAIL(setSecurityMode(mc->securityMode), METHOD_EXPRESS_START_MODULE);
    	RETURN_RESULT_IF_FAIL(setSecurityKey(mc->securityKey), METHOD_EXPRESS_START_MODULE);
    }

#ifdef SUPPORT_CUSTOM_APPLICATION_CONFIGURATION    
    if (ac == GENERIC_APPLICATION_CONFIGURATION)
    {
    RETURN_RESULT_IF_FAIL(afRegisterGenericApplication(), METHOD_EXPRESS_START_MODULE);    // Configure the Module for our application
    } else {
    RETURN_RESULT_IF_FAIL(afRegisterApplication(ac), METHOD_EXPRESS_START_MODULE);
    }
#else    
    if (ac == GENERIC_APPLICATION_CONFIGURATION)
    {
    RETURN_RESULT_IF_FAIL(afRegisterGenericApplication(), METHOD_EXPRESS_START_MODULE);    // Configure the Module for our application
    } else {
        /* Note: to use a custom application configuration, you must #define SUPPORT_CUSTOM_APPLICATION_CONFIGURATION. */
    return INVALID_PARAMETER;
    }    
#endif
    
    /* Note: you can register more than one Zigbee endpoint; just call afRegisterApplication() here
    for the next endpoint */
    
    /* Start the module with the registered application configuration */
    RETURN_RESULT_IF_FAIL(zdoStartApplication(), METHOD_EXPRESS_START_MODULE);

	  /* Wait until this device has joined a network. Device State will change to DEV_ROUTER,
      DEV_END_DEVICE, or DEV_COORD to indicate that the device has correctly joined a network. */

	#ifdef ZDO_STATE_CHANGE_IND_HANDLED_BY_APPLICATION  //if you're handling this in your application instead...
	  return MODULE_SUCCESS;
	#else
	#define TEN_SECONDS 10000
	#define FIFTEEN_SECONDS 15000
	  RETURN_RESULT(waitForDeviceState(getDeviceStateForDeviceType(mc->deviceType), FIFTEEN_SECONDS), METHOD_EXPRESS_START_MODULE);
	#endif
}
int main( void )
{
    halInit();
    printf("\r\n****************************************************\r\n");
    printf("Basic Communications Example - COORDINATOR - using AFZDO\r\n");
    HAL_ENABLE_INTERRUPTS();
    setLed(0);
    /* Initialize the ZNP */
    printf("Initializing the ZNP  ");    
    znpInit(); 
    handleReturnValue();
    
    /* Set Startup Options (will restore the ZNP to default values on reset) */
    printf("Setting StartupOptions  ");
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    handleReturnValue();
    
    /* Reset the ZNP */
    printf("Reset the ZNP  ");    
    znpReset();
    handleReturnValue();
    
    /* Set Zigbee Device Type to be COORDINATOR */
    printf("Setting Zigbee Device Type  "); 
    setZigbeeDeviceType(COORDINATOR);
    handleReturnValue();
    
    /* Enabling Callbacks (required to receive ZDO_IEEE_ADDR_RSP)  */
//#ifdef FIND_MAC_ADDRESS_OF_SENDER  //define this to print out sender's MAC address
    printf("Enabling Callbacks  "); 
    setCallbacks(CALLBACKS_ENABLED);
    handleReturnValue();    
//#endif
    
    /* Configure the ZNP for our application */
    printf("Registering Application  ");   
    afRegisterGenericApplication();
    handleReturnValue();
    
    /* Now, start the application. We will receive a START_REQUEST_SRSP, and then if it is successful, a START_CONFIRM. */
    printf("Starting the Application  "); 
    zdoStartApplication();
    handleReturnValue();
    
    /** Wait until we get on the network. 
    We will receive a ZDO_STATE_CHANGE_IND message whenever the state changes. */
    waitForDeviceState(DEV_ZB_COORD);

    printf("On Network!\r\n");
    setLed(1);
            
    /* On network, display info about this network */
#ifdef DISPLAY_NETWORK_INFORMATION     
    getNetworkConfigurationParameters();                
    getDeviceInformation();
#endif  

    /* Now the network is running - continually poll for any received messages from the ZNP */
#ifdef FIND_MAC_ADDRESS_OF_SENDER  //define this to print out sender's MAC address
    displayReceivedMessagesAndFindDevice();
#else
    displayReceivedMessages();
#endif    
    
}
int main( void )
{
    halInit(); 
    printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n");          
    printf("\r\nBasic Communications Example - ROUTER - using Simple API\r\n");
    HAL_ENABLE_INTERRUPTS();
    
    //Simple check to ensure that both security options weren't #defined.
#if defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("ERROR - only select one security option!\r\n");
    while (1);
#endif 
    
    /* Initialize the ZNP */
    printf("Initializing the ZNP\r\n");
    znpInit(); 
    handleReturnValue();
    
    /* Set Startup Options (will restore the ZNP to default values on reset) */
    printf("Setting StartupOptions\r\n");    
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    handleReturnValue();
    
    /* Reset the ZNP */
    printf("Reset the ZNP\r\n");    
    znpReset();
    handleReturnValue();
    
    /* Set Zigbee Device Type to be ROUTER */
    printf("Setting Zigbee Device Type\r\n"); 
    setZigbeeDeviceType(ROUTER);
    handleReturnValue();

    /* Configure security mode, if it is being used */
#ifdef USE_SECURITY_MODE_PRECONFIGURED_KEYS
    printf("SECURITY ON WITH PRECONFIGURED KEYS\r\n");
    
    /* Turn security ON with pre-configured keys */
    setSecurityMode(SECURITY_MODE_PRECONFIGURED_KEYS);
    handleReturnValue();
    
    /* All devices on the network must be loaded with the same key */    
    setSecurityKey(key);
    handleReturnValue();    
#endif
    
#ifdef USE_SECURITY_MODE_COORD_DIST_KEYS
    printf("SECURITY ON WITH COORDINATOR DISTRIBUTING KEYS\r\n");
    
    /* Turn security ON with the coordinator distributing keys. */
    setSecurityMode(SECURITY_MODE_COORD_DIST_KEYS);
    handleReturnValue();
    
    /* This is the key that will be distributed to other devices when they attempt to join */
    setSecurityKey(key);
    handleReturnValue();
#endif   
    
#if !defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && !defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("SECURITY OFF\r\n");
#endif 
    
    /* Configure the ZNP for our application */
    printf("Registering Application\r\n");  
    sapiRegisterGenericApplication();
    handleReturnValue();
    
    /* Now, start the application. We will receive a START_REQUEST_SRSP, and then if it is successful, a START_CONFIRM. */
    printf("Starting the Application\r\n");      
    sapiStartApplication();
    handleReturnValue();
    
    printf("On Network!\r\n");
    setLed(0);
    /* On network, display info about this network */
#ifdef DISPLAY_NETWORK_INFORMATION     
    getNetworkConfigurationParameters();                
    getDeviceInformation();
#endif    
    
    /* Now the network is running - send a message to the coordinator every few seconds.*/
    unsigned char counter = 0;
    unsigned char testMessage[] = {0xF0,0xF1,0xF2,0xF3,0xF4};
#define TEST_CLUSTER 0x77    

        pollAndDisplay();
    
    while (1)
    {
        printf("Sending Message %u\r\n", counter++);
        sendData(0, TEST_CLUSTER, testMessage, 5);        
        handleReturnValue();
        toggleLed(1);         
        delayMs(5000);          
    }   
}
int main( void )
{
    halInit(); //Sets up all hardware inc debug Tx/Rx (Used for WiFi <--> ZNP)
    printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n");        
    printf("\r\nBasic Communications Example - COORDINATOR - using Simple API\r\n");
    HAL_ENABLE_INTERRUPTS();
    setLed(0);
    //Simple check to ensure that both security options weren't #defined.
#if defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("ERROR\r\n");
    while (1);
#endif 
    
    /* Initialize the ZNP */
    printf("Initializing the ZNP\r\n");    
    znpInit(); 
    handleReturnValue();
    
    /* Set Startup Options (will restore the ZNP to default values on reset) */
    printf("Setting StartupOptions\r\n");
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    handleReturnValue();
    
    /* Reset the ZNP */
    printf("Reset the ZNP\r\n");    
    znpReset();
    handleReturnValue();
    
    /* Set Zigbee Device Type to be COORDINATOR */
    printf("Setting Zigbee Device Type\r\n"); 
    setZigbeeDeviceType(COORDINATOR);
    handleReturnValue();
    
    /* Enabling Callbacks (required to receive ZDO_IEEE_ADDR_RSP)  */
    printf("Enabling Callbacks\r\n"); 
    setCallbacks(CALLBACKS_ENABLED);
    handleReturnValue();    

    /* Configure security mode, if it is being used */
#ifdef USE_SECURITY_MODE_PRECONFIGURED_KEYS
    printf("SECURITY ON WITH PRECONFIGURED KEYS\r\n");
    
    /* Turn security ON with pre-configured keys */
    setSecurityMode(SECURITY_MODE_PRECONFIGURED_KEYS);
    handleReturnValue();
    
    /* All devices on the network must be loaded with the same key */    
    setSecurityKey(key);
    handleReturnValue();    
#endif
    
#ifdef USE_SECURITY_MODE_COORD_DIST_KEYS
    printf("SECURITY ON WITH COORDINATOR DISTRIBUTING KEYS\r\n");
    
    /* Turn security ON with the coordinator distributing keys. */
    setSecurityMode(SECURITY_MODE_COORD_DIST_KEYS);
    handleReturnValue();
    
    /* This is the key that will be distributed to other devices when they attempt to join */
    setSecurityKey(key);
    handleReturnValue();
#endif   
    
#if !defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && !defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("SECURITY OFF\r\n");
#endif 
    
    /* Configure the ZNP for our application: */
    printf("Registering Application\r\n");
    sapiRegisterGenericApplication();    
    handleReturnValue();
    
    /* Now, start the application. We will receive a START_REQUEST_SRSP, and then if it is successful, a START_CONFIRM. */
    printf("Starting the Application\r\n");      
    sapiStartApplication();
    handleReturnValue();
    
    printf("On Network!\r\n");
    setLed(1);
    
    /* On network, display info about this network */
#ifdef DISPLAY_NETWORK_INFORMATION      
    getNetworkConfigurationParameters();                
    getDeviceInformation();
#endif    
    
    /* Now the network is running - continually poll for any received messages from the ZNP */
    //displayReceivedMessages();
    
    while(1)
    {
		/*
      int light = getLightSense();
      if(light > 100)
      {
        sendData(0xC72, 0xF, 0x1, 1);
      }
      else
      {
        sendData(0xC72, 0xF, 0x2, 1);
      }
		 */
		int cmd = readUART(); //Read UART
		//Depending on command, send appropriate message to relevant end point
		switch (cmd) {
			case CLOSE1:
				//sendData(dest, cluster, data, dataLength);
				sendData(0xC72, 0xF, 0x1, 1);
				break;
			case OPEN1:
				sendData(0xC72, 0xF, 0x2, 1);
				break;
			case FILM1:
				sendData(0xC72, 0xF, 0x3, 1);
				break;
			case DAY1:
				sendData(0xC72, 0xF, 0x4, 1);
				break;
			case NIGHT1:
				sendData(0xC72, 0xF, 0x5, 1);
				break;
			case CLOSE2:
				sendData(0xC72, 0xF, 0x6, 1);
				break;
			case OPEN2:
				sendData(0xC72, 0xF, 0x7, 1);
				break;
			case FILM2:
				sendData(0xC72, 0xF, 0x8, 1);
				break;
			case DAY2:
				sendData(0xC72, 0xF, 0x9, 1);
				break;
			case NIGHT2:
				sendData(0xC72, 0xF, 0xA, 1);
				break;
			default:
				break;
		}
    }
}
int main( void )
{
    halInit();
    printf("\r\n****************************************************\r\n");    
    printf("Secure Communications Example - ROUTER - using AFZDO\r\n");
    HAL_ENABLE_INTERRUPTS();
    
    //Simple idiot check to ensure that we didn't accidentally define both security options.
#if defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("ERROR - only select one security option!\r\n");
    while (1);
#endif 
    
    /* Initialize the ZNP */
    printf("Initializing the ZNP\r\n");
    znpInit(); 
    handleReturnValue();
    
    /* Set Startup Options (will restore the ZNP to default values on reset) */
    printf("Setting StartupOptions\r\n");    
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    handleReturnValue();
    
    /* Reset the ZNP */
    printf("Reset the ZNP\r\n");    
    znpReset();
    handleReturnValue();
    
    /* Set Zigbee Device Type to be ROUTER */
    printf("Setting Zigbee Device Type\r\n"); 
    setZigbeeDeviceType(ROUTER);
    handleReturnValue();

    /* Configure security mode, if it is being used */
#ifdef USE_SECURITY_MODE_PRECONFIGURED_KEYS
    printf("PRECONFIGURED KEYS\r\n");
    /* Turn security ON with pre-configured keys */
    setSecurityMode(SECURITY_MODE_PRECONFIGURED_KEYS);
    handleReturnValue();    
    
    /* All devices on the network must be loaded with the same key */ 
    setSecurityKey(key);
    handleReturnValue();    
#endif
    
#ifdef USE_SECURITY_MODE_COORD_DIST_KEYS
    printf("COORDINATOR DISTRIBUTING KEYS\r\n");
    
    /* Turn security ON with the coordinator distributing keys. 
    Since this is the router we don't need to load a key; it will be sent to us by the coordinator! */
    setSecurityMode(SECURITY_MODE_COORD_DIST_KEYS);
    handleReturnValue();
#endif
    
#if !defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && !defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("WARNING - NO SECURITY OPTION SELECTED; SECURITY OFF\r\n");
#endif 
    
    /* Configure the ZNP for our application */
    printf("Registering Application\r\n");   
    afRegisterGenericApplication();
    handleReturnValue();
    
    /* Now, start the application. We will receive a START_REQUEST_SRSP, and then if it is successful, a START_CONFIRM. */
    printf("Starting the Application\r\n");     
    zdoStartApplication();
    handleReturnValue();
    
    /** Wait until we get on the network. 
    We will receive a ZDO_STATE_CHANGE_IND message whenever the state changes. */
    waitForDeviceState(DEV_ROUTER);

    printf("On Network!\r\n");
    setLed(0);
    
    /* On network, display info about this network */
#ifdef DISPLAY_NETWORK_INFORMATION     
    getNetworkConfigurationParameters();                
    getDeviceInformation();
#endif  
    
    /* Now the network is running - send a message to the coordinator every few seconds.*/
    unsigned char counter = 0;
    unsigned char testMessage[] = {0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9};
#define TEST_CLUSTER 0x77    
    
    while (1)
    {
        printf("Sending Message %u\r\n", counter++);
        afSendData(DEFAULT_ENDPOINT,DEFAULT_ENDPOINT,0, TEST_CLUSTER, testMessage, 10);
        handleReturnValue();
        toggleLed(1);         
        delayMs(5000);          
    }   
}
int main( void )
{
    halInit();
    printf("\r\n****************************************************\r\n");
    printf("Secure Communications Example - COORDINATOR - using AFZDO\r\n");
    HAL_ENABLE_INTERRUPTS();
    setLed(0);
    
    //Simple idiot check to ensure that we didn't accidentally define both security options.
#if defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("ERROR - only select one security option!\r\n");
    while (1);
#endif     
    
    /* Initialize the ZNP */
    printf("Initializing the ZNP\r\n");    
    znpInit(); 
    handleReturnValue();
    
    /* Set Startup Options (will restore the ZNP to default values on reset) */
    printf("Setting StartupOptions\r\n");
    setStartupOptions(STARTOPT_CLEAR_CONFIG + STARTOPT_CLEAR_STATE);
    handleReturnValue();
    
    /* Set Zigbee Device Type to be COORDINATOR */
    printf("Setting Zigbee Device Type\r\n"); 
    setZigbeeDeviceType(COORDINATOR);
    handleReturnValue();
    
    /* Enabling Callbacks (required to receive ZDO_IEEE_ADDR_RSP)  */
    printf("Enabling Callbacks\r\n"); 
    setCallbacks(CALLBACKS_ENABLED);
    handleReturnValue();    
    
    /* Reset the ZNP */
    printf("Reset the ZNP\r\n");    
    znpReset();
    handleReturnValue();
    
    /* Configure security mode, if it is being used */
#ifdef USE_SECURITY_MODE_PRECONFIGURED_KEYS
    printf("SECURITY ON WITH PRECONFIGURED KEYS\r\n");
    
    /* Turn security ON with pre-configured keys */
    setSecurityMode(SECURITY_MODE_PRECONFIGURED_KEYS);
    handleReturnValue();
    
    /* All devices on the network must be loaded with the same key */    
    setSecurityKey(key);
    handleReturnValue();    
#endif
    
#ifdef USE_SECURITY_MODE_COORD_DIST_KEYS
    printf("SECURITY ON WITH COORDINATOR DISTRIBUTING KEYS\r\n");
    
    /* Turn security ON with the coordinator distributing keys. */
    setSecurityMode(SECURITY_MODE_COORD_DIST_KEYS);
    handleReturnValue();
    
    /* This is the key that will be distributed to other devices when they attempt to join */
    setSecurityKey(key);
    handleReturnValue();
#endif   
    
    /** Note: if no security option is selected then this will behave like a normal coordinator
    and will accept join requests from any device. */
#if !defined(USE_SECURITY_MODE_PRECONFIGURED_KEYS) && !defined(USE_SECURITY_MODE_COORD_DIST_KEYS)
    printf("WARNING - NO SECURITY OPTION SELECTED; SECURITY OFF\r\n");
#endif 
    
    /* Configure the ZNP for our application */
    printf("Registering Application\r\n");     
    afRegisterGenericApplication();    
    handleReturnValue();
    
    /* Now, start the application. We will receive a START_REQUEST_SRSP, and then if it is successful, a START_CONFIRM. */
    printf("Starting the Application\r\n"); 
    zdoStartApplication();
    handleReturnValue();
    
    /** Wait until we get on the network. 
    We will receive a ZDO_STATE_CHANGE_IND message whenever the state changes. */
    waitForDeviceState(DEV_ZB_COORD);

    printf("On Network!\r\n");
    setLed(1);
    
    /* On network, display info about this network */
#ifdef DISPLAY_NETWORK_INFORMATION     
    getNetworkConfigurationParameters();                
    getDeviceInformation();
#endif  

    /* Now the network is running - continually poll for any received messages from the ZNP */
    if (SRDY_IS_HIGH())
        pollAndDisplay();
    
    displayReceivedMessages();
    
}