/** Parse any received messages. If it's one of our OIDs then display it. */ void parseMessages() { getMessage(); if ((zmBuf[SRSP_LENGTH_FIELD] > 0) && (IS_AF_INCOMING_MESSAGE())) { setLed(0); //LED will blink to indicate a message was received #ifdef VERBOSE_MESSAGE_DISPLAY printAfIncomingMsgHeader(zmBuf); printf("\r\n"); #endif if ((AF_INCOMING_MESSAGE_CLUSTER()) == INFO_MESSAGE_CLUSTER) { processInfoMessage(); } else if ((AF_INCOMING_MESSAGE_CLUSTER()) == CONFIG_REQUEST_MESSAGE_CLUSTER) { processConfigRequestMessage(); } else { //unknown cluster printf("Rx: "); printHexBytes(zmBuf+SRSP_HEADER_SIZE+17, zmBuf[SRSP_HEADER_SIZE+16]); //print out message payload } clearLed(0); } else if (IS_ZDO_END_DEVICE_ANNCE_IND()) { displayZdoEndDeviceAnnounce(zmBuf); } else { //unknown message, just print out the whole thing printf("MSG: "); printHexBytes(zmBuf, (zmBuf[SRSP_LENGTH_FIELD] + SRSP_HEADER_SIZE)); } zmBuf[SRSP_LENGTH_FIELD] = 0; }
void blinkTask(void *param) { //Endless loop while(1) { setLed(AT91B_LED1); osDelayTask(100); clearLed(AT91B_LED1); osDelayTask(900); } }
int main( void ) { halInit(); moduleInit(); printf("\r\n****************************************************\r\n"); printf("Packet Error Rate Tester - ROUTER\r\n"); buttonIsr = &handleButtonPress; #define MODULE_START_DELAY_IF_FAIL_MS 5000 /* Use the default module configuration */ struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_ROUTER; /* Turn Off nwk status LED if on */ clearLed(ON_NETWORK_LED); /* Loop until module starts */ while ((result = expressStartModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION, MODULE_REGION_NORTH_AMERICA)) != MODULE_SUCCESS) { /* Module startup failed; display error and blink LED */ setLed(NETWORK_FAILURE_LED); printf("Module start unsuccessful. Error Code 0x%02X. Retrying...\r\n", result); delayMs(MODULE_START_DELAY_IF_FAIL_MS/2); clearLed(NETWORK_FAILURE_LED); delayMs(MODULE_START_DELAY_IF_FAIL_MS/2); } printf("On Network!\r\n"); /* Indicate we got on the network */ setLed(ON_NETWORK_LED); /* On network, display info about this network */ #ifdef DISPLAY_NETWORK_INFORMATION displayNetworkConfigurationParameters(); displayDeviceInformation(); #else displayBasicDeviceInformation(); #endif HAL_ENABLE_INTERRUPTS(); /* Now the network is running - send messages to the Coordinator.*/ #define TEST_CLUSTER 0x77 #define MESSAGE_HEADER_LENGTH 13 #define MESSAGE_LENGTH (TEST_MESSAGE_PAYLOAD_LENGTH + MESSAGE_HEADER_LENGTH) /* Here we precompute zmBuf contents so that we don't have to do it in the loop. This is faster. This is the equivalent of afSendData(DEFAULT_ENDPOINT,DEFAULT_ENDPOINT,0, TEST_CLUSTER, testMessage, 10); */ uint8_t testBuf[MESSAGE_LENGTH]; testBuf[0] = MESSAGE_LENGTH; testBuf[1] = MSB(AF_DATA_REQUEST); testBuf[2] = LSB(AF_DATA_REQUEST); testBuf[3] = 0; testBuf[4] = 0; testBuf[5] = DEFAULT_ENDPOINT; testBuf[6] = DEFAULT_ENDPOINT; testBuf[7] = LSB(TEST_CLUSTER); testBuf[8] = MSB(TEST_CLUSTER); testBuf[9] = 0xFF; // Sequence: we don't care testBuf[10] = AF_MAC_ACK; //Could also use AF_APS_ACK; testBuf[11] = DEFAULT_RADIUS; testBuf[12] = TEST_MESSAGE_PAYLOAD_LENGTH; // Datalength //memcpy(testBuf+MESSAGE_HEADER_LENGTH, testMessage, TEST_MESSAGE_PAYLOAD_LENGTH); //testBuf is now loaded with our test message. printf("!! Sending %u messages !!\r\n", NUMBER_OF_PACKETS_TO_SEND); while (1) { uint16_t packetCounter; for (packetCounter = 0; packetCounter<NUMBER_OF_PACKETS_TO_SEND; packetCounter++) { /* Copy our message over to zmBuf because zmBuf gets overwritten when the AF_DATA_CONFIRM is received */ memcpy(zmBuf, testBuf, MESSAGE_LENGTH); /* Now initialize the payload */ int index; for (index = MESSAGE_HEADER_LENGTH; index < (TEST_MESSAGE_PAYLOAD_LENGTH + MESSAGE_HEADER_LENGTH); index++) { testBuf[index] = index; } /* Send the message to the Coordinator */ result = sendMessage(); if (result != MODULE_SUCCESS) { printf("afSendData Error %02X; stopping\r\n", result); while (1); } /* Now, wait for the AF_DATA_CONFIRM to verify that the message was successfully sent*/ while (!(MODULE_HAS_MESSAGE_WAITING())); /* Retrieve the AF_DATA_CONFIRM message */ getMessage(); if (!(IS_AF_DATA_CONFIRM())) { /* Stop if we receive a different message */ printf("Error; stopped after packet %u", packetCounter); } toggleLed(1); /* If you want to slow down the rate of sending packets then add: delayMs(1); which will add a one mSec delay after each packet is sent. */ if (((packetCounter % 100) == 0) && (packetCounter != 0)) { printf("%u\r\n", packetCounter); } } printf("Done! Sent %u packets!\r\nPress button to start again\r\n", NUMBER_OF_PACKETS_TO_SEND); /* Wait until a button is pressed, then send another 1000 */ while (!(buttonIsPressed(ANY_BUTTON))); } }
void stateMachine() { while (1) { if (zigbeeNetworkStatus == NWK_ONLINE) { if(moduleHasMessageWaiting()) //wait until SRDY goes low indicating a message has been received. { getMessage(); displayMessage(); } } switch (state) { case STATE_IDLE: { if (stateFlags & STATE_FLAG_SEND_INFO_MESSAGE) //if there is a pending info message to be sent { state = STATE_SEND_INFO_MESSAGE; //then send the message and clear the flag stateFlags &= ~STATE_FLAG_SEND_INFO_MESSAGE; } /* Other flags (for different messages or events) can be added here */ break; } case STATE_MODULE_STARTUP: { #define MODULE_START_DELAY_IF_FAIL_MS 5000 // Must be greater than MODULE_START_FAIL_LED_ONTIME moduleResult_t result; struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_END_DEVICE; /* Uncomment below to restrict the device to a specific PANID defaultConfiguration.panId = 0x1234; */ struct applicationConfiguration endDeviceConf; endDeviceConf.endPoint = 1; endDeviceConf.latencyRequested = LATENCY_NORMAL; endDeviceConf.profileId = 0xcc00; // the clock profile is 0xCC00 endDeviceConf.deviceId = 0x8866; endDeviceConf.deviceVersion = 0x01; endDeviceConf.numberOfBindingInputClusters = 4; // number of binding input cluster endDeviceConf.bindingInputClusters[0] = 0x0000; // basic cluster endDeviceConf.bindingInputClusters[1] = 0x0003; // identify cluster endDeviceConf.bindingInputClusters[2] = 0xfc01; // synchronise clock cluster endDeviceConf.bindingInputClusters[3] = 0xfc02; // send string message cluster endDeviceConf.numberOfBindingOutputClusters = 4; // number of binding output cluster endDeviceConf.bindingOutputClusters[0] = 0x0000; endDeviceConf.bindingOutputClusters[1] = 0x0003; endDeviceConf.bindingOutputClusters[2] = 0xfc01; endDeviceConf.bindingOutputClusters[3] = 0xfc02; struct applicationConfiguration ac; ac.endPoint = 1; ac.deviceVersion = 0x10; ac.profileId = 0xfafa; ac.latencyRequested = LATENCY_NORMAL; ac.numberOfBindingInputClusters = 2; ac.bindingInputClusters[0] = 0x0000; ac.bindingInputClusters[1] = 0x0900; ac.numberOfBindingOutputClusters = 2; ac.bindingOutputClusters[0] = 0x0000; ac.bindingOutputClusters[1] = 0x0900; /* Below is an example of how to restrict the device to only one channel: defaultConfiguration.channelMask = CHANNEL_MASK_17; printf("DEMO - USING CUSTOM CHANNEL 17\r\n"); */ //while ((result = startModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION)) != MODULE_SUCCESS) while ((result = startModule(&defaultConfiguration, &ac)) != MODULE_SUCCESS) { SET_NETWORK_FAILURE_LED_ON(); // Turn on the LED to show failure printf("FAILED. Error Code 0x%02X. Retrying...\r\n", result); delayMs(MODULE_START_DELAY_IF_FAIL_MS/2); SET_NETWORK_FAILURE_LED_OFF(); delayMs(MODULE_START_DELAY_IF_FAIL_MS/2); } INIT_BOOSTER_PACK_LEDS(); SET_NETWORK_LED_ON(); SET_NETWORK_FAILURE_LED_OFF(); printf("Success\r\n"); /* Module is now initialized so store MAC Address */ zbGetDeviceInfo(DIP_MAC_ADDRESS); memcpy(hdr.mac, zmBuf+SRSP_DIP_VALUE_FIELD, 8); #define MESSAGE_PERIOD_SECONDS 4 int16_t timerResult = initTimer(MESSAGE_PERIOD_SECONDS); if (timerResult != 0) { printf("timerResult Error %d, STOPPING\r\n", timerResult); while (1); } state = STATE_DISPLAY_NETWORK_INFORMATION; break; } case STATE_DISPLAY_NETWORK_INFORMATION: { printf("~ni~"); /* On network, display info about this network */ displayNetworkConfigurationParameters(); displayDeviceInformation(); /* Set module GPIOs as output and turn them off */ if ((sysGpio(GPIO_SET_DIRECTION, ALL_GPIO_PINS) != MODULE_SUCCESS) || (sysGpio(GPIO_CLEAR, ALL_GPIO_PINS) != MODULE_SUCCESS)) { printf("ERROR\r\n"); } state = STATE_SEND_INFO_MESSAGE; break; } case STATE_SEND_INFO_MESSAGE: { printf("~im~"); struct infoMessage im; /* See infoMessage.h for description of these info message fields.*/ im.header = hdr; im.deviceType = DEVICETYPE_TESLA_CONTROLS_END_DEVICE_DEMO; im.header.sequence = sequenceNumber++; im.numParameters = getSensorValues(im.kvps); // Does two things: Loads infoMessage with sensor value KVPs and gets the number of them // now, add status message interval im.kvps[im.numParameters].oid = OID_STATUS_MESSAGE_INTERVAL; im.kvps[im.numParameters].value = MESSAGE_PERIOD_SECONDS; im.numParameters++; // add zigbee module information: if (sysVersion() != MODULE_SUCCESS) { printf("ERROR retriving module information\r\n"); } else { displaySysVersion(); // Product ID im.kvps[im.numParameters].oid = OID_MODULE_PRODUCT_ID; im.kvps[im.numParameters].value = zmBuf[SYS_VERSION_RESULT_PRODUCTID_FIELD]; im.numParameters++; // FW - Major im.kvps[im.numParameters].oid = OID_MODULE_FIRMWARE_MAJOR; im.kvps[im.numParameters].value = zmBuf[SYS_VERSION_RESULT_FW_MAJOR_FIELD]; im.numParameters++; // FW - Minor im.kvps[im.numParameters].oid = OID_MODULE_FIRMWARE_MINOR; im.kvps[im.numParameters].value = zmBuf[SYS_VERSION_RESULT_FW_MINOR_FIELD]; im.numParameters++; // FW - Build im.kvps[im.numParameters].oid = OID_MODULE_FIRMWARE_BUILD; im.kvps[im.numParameters].value = zmBuf[SYS_VERSION_RESULT_FW_BUILD_FIELD]; im.numParameters++; } printInfoMessage(&im); #define RESTART_DELAY_IF_MESSAGE_FAIL_MS 5000 uint8_t messageBuffer[MAX_INFO_MESSAGE_SIZE]; serializeInfoMessage(&im, messageBuffer); setLed(SEND_MESSAGE_LED); //indicate that we are sending a message moduleResult_t result = afSendData(DEFAULT_ENDPOINT, DEFAULT_ENDPOINT, 0, INFO_MESSAGE_CLUSTER, messageBuffer, getSizeOfInfoMessage(&im)); // and send it clearLed(SEND_MESSAGE_LED); if (result != MODULE_SUCCESS) { zigbeeNetworkStatus = NWK_OFFLINE; printf("afSendData error %02X; restarting...\r\n", result); delayMs(RESTART_DELAY_IF_MESSAGE_FAIL_MS); //allow enough time for coordinator to fully restart, if that caused our problem state = STATE_MODULE_STARTUP; } else { printf("Success\r\n"); state = STATE_IDLE; } break; } default: //should never happen { printf("UNKNOWN STATE\r\n"); state = STATE_MODULE_STARTUP; } break; } } }
void toggleLed (uint8_t led) { if(LED_GPIO->DATA & SET_BIT(led)) { clearLed(led); } else setLed(led); }