/** Initializes the hardware interface to the ZNP (SPI). This method must be called before any other method in the library. @post znpResult contains the error code, or ZNP_SUCCESS if success. @return a pointer to the beginning of the version structure, or a pointer to indeterminate data if error. @see Interface Specification for order of fields @todo modify this if using UART */ unsigned char* znpInit() { #ifdef ZNP_INTERFACE_VERBOSE printf("Initializing ZNP SPI\r\n"); #endif halSpiInitZnp(); return (znpReset()); }
/** 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(); }
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(); }