/** Displays the radio's device Information Properties. Device Information Properties include: - Device State indicates whether the ZNP is on a network or not, and what type of device it is. This is a handy thing to check if things aren't operating correctly. If the device is starting as a coordinator, you'll see states of 01, 08, 08, then 09 once it has fully started. - MAC Address (aka IEEE Address) is a globally unique serial number for this IC. - Device Short Address is a network address assigned by the coordinator, similar to an IP Address in DHCP. The Coordinator always has a Short Address of 0. - Parent MAC Address is the IEEE Address of this device's "parent", i.e. which device was used to join the network. For a router, once joined this parent MAC address is irrelevant. This DIP will NOT be updated if the network reforms. For an end-device then this parent MAC address will always specify which router the end-device is joined to. - Channel is which frequency channel the device is operating on. - PAN ID (Personal Area Network Identifier) of the network is a unique number shared for all devices on the same network. - Extended PAN ID of the network is the coordinator's MAC Address. If device is not connected to a network then the Short Address fields will be 0xFEFF, the Parent MAC Address and channel will be 0, and the Extended PAN ID will be this device's MAC Address. @post znpResult contains the error code, or ZNP_SUCCESS if success. */ void getDeviceInformation() { printf("Device Information Properties (MSB first)\r\n"); getDeviceInformationProperty(DIP_STATE); if (znpResult != 0) return; printf(" Device State: %s (%u)\r\n", getDeviceStateName(znpBuf[SRSP_DIP_VALUE_FIELD]), (znpBuf[SRSP_DIP_VALUE_FIELD])); getDeviceInformationProperty(DIP_MAC_ADDRESS); printf(" MAC Address: "); if (znpResult != 0) return; for (int i = SRSP_DIP_VALUE_FIELD+7; i>=SRSP_DIP_VALUE_FIELD; i--) printf("%02X ", znpBuf[i]); printf("\r\n"); getDeviceInformationProperty(DIP_SHORT_ADDRESS); if (znpResult != 0) return; printf(" Short Address: %04X\r\n", CONVERT_TO_INT(znpBuf[SRSP_DIP_VALUE_FIELD] , znpBuf[SRSP_DIP_VALUE_FIELD+1])); getDeviceInformationProperty(DIP_PARENT_SHORT_ADDRESS); if (znpResult != 0) return; printf(" Parent Short Address: %04X\r\n", CONVERT_TO_INT(znpBuf[SRSP_DIP_VALUE_FIELD] , znpBuf[SRSP_DIP_VALUE_FIELD+1])); getDeviceInformationProperty(DIP_PARENT_MAC_ADDRESS); if (znpResult != 0) return; printf(" Parent MAC Address: "); for (int i = SRSP_DIP_VALUE_FIELD+7; i>=SRSP_DIP_VALUE_FIELD; i--) printf("%02X ", znpBuf[i]); printf("\r\n"); getDeviceInformationProperty(DIP_CHANNEL); if (znpResult != 0) return; printf(" Device Channel: %u\r\n", znpBuf[SRSP_DIP_VALUE_FIELD]); getDeviceInformationProperty(DIP_PANID); if (znpResult != 0) return; printf(" PAN ID: %04X\r\n", CONVERT_TO_INT(znpBuf[SRSP_DIP_VALUE_FIELD], znpBuf[SRSP_DIP_VALUE_FIELD+1])); getDeviceInformationProperty(DIP_EXTENDED_PANID); if (znpResult != 0) return; printf(" Extended PAN ID: "); for (int i = SRSP_DIP_VALUE_FIELD+7; i>=SRSP_DIP_VALUE_FIELD; i--) printf("%02X ", znpBuf[i]); printf("\r\n"); }
/** Retrieves the Media Access Controller (MAC) Address (aka Long Address, aka IEEE Address) The MAC Address is an eight byte globally unique serial number for this IC. @post znpResult contains the error code, or ZNP_SUCCESS if success. @return a pointer to the beginning of the MAC address (LSB first), or a pointer to indeterminate data if error. */ unsigned char* getMacAddress() { return (getDeviceInformationProperty(DIP_MAC_ADDRESS)); }
/** Retrieves the device state - indicates whether it is on the network or not. @post znpResult contains the error code, or ZNP_SUCCESS if success. @return the device state: DEV_HOLD, DEV_NWK_DISC, DEV_ROUTER etc. or an indeterminate value if error. */ unsigned char getDeviceState() { getDeviceInformationProperty(DIP_STATE); return znpBuf[SRSP_HEADER_SIZE+1]; }
void stateMachine() { while (1) { 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; } //note: other flags (for different messages or events) can be added here break; } case STATE_ZNP_STARTUP: { #define ZNP_START_DELAY_IF_FAIL_MS 5000 /* Start the network; if fails then wait a second and try again. */ signed int startResult = startZnp(END_DEVICE); while (startResult != ZNP_SUCCESS) { printf("FAILED. Error Code %i, ZNP Result %i. Retrying...\r\n", startResult, znpResult); delayMs(ZNP_START_DELAY_IF_FAIL_MS); startResult = startZnp(END_DEVICE); } printf("Success\r\n"); //ZNP Initialized so store MAC Address memcpy(hdr.mac, getMacAddress(), 8); #ifdef SEND_MESSAGE_ON_TIMER signed int timerResult = initTimer(4, WAKEUP_AFTER_TIMER); if (timerResult != 0) { printf("timerResult Error %i, STOPPING\r\n", timerResult); while (1); } #endif state = STATE_DISPLAY_NETWORK_INFORMATION; break; } case STATE_DISPLAY_NETWORK_INFORMATION: { printf("~ni~"); /* On network, display info about this network */ getNetworkConfigurationParameters(); getDeviceInformation(); state = STATE_SEND_INFO_MESSAGE; break; } case STATE_SEND_INFO_MESSAGE: { printf("~im~"); setLed(1); im.header->sequence = sequenceNumber++; im.cause = infoMessageCause; unsigned char* panid = getDeviceInformationProperty(DIP_PANID); im.parameters[0] = CONVERT_TO_INT(*panid, *(panid+1)); //PAN ID im.parameters[1] = getVcc3(); //VCC im.parameters[2] = getLightSensor(); //Light Sensor printInfoMessage(&im); #define ZNP_RESTART_DELAY_IF_MESSAGE_FAIL_MS 5000 unsigned char msgBuf[100]; serializeInfoMessage(&im, msgBuf); afSendData(DEFAULT_ENDPOINT,DEFAULT_ENDPOINT,0, INFO_MESSAGE_CLUSTER, msgBuf, getSizeOfInfoMessage(&im)); if (znpResult != ZNP_SUCCESS) { printf("afSendData error %i; restarting...\r\n", znpResult); delayMs(ZNP_RESTART_DELAY_IF_MESSAGE_FAIL_MS); //allow enough time for coordinator to fully restart, if that caused our problem state = STATE_ZNP_STARTUP; } else { state = STATE_IDLE; clearLeds(); HAL_SLEEP(); } break; } default: //should never happen { printf("UNKNOWN STATE\r\n"); state = STATE_ZNP_STARTUP; } break; } } }