int main( void )
{
    halInit();
    printf("\r\n****************************************************\r\n");
    printf("Simple Application Example - COORDINATOR - using AFZDO\r\n");
    HAL_ENABLE_INTERRUPTS();
    startZnp(COORDINATOR);

    printf("On Network!\r\n");
    
    /* On network, display info about this network */   
    getNetworkConfigurationParameters();                
    getDeviceInformation();
    
    /* Now the network is running - wait for any received messages from the ZNP */
#ifdef VERBOSE_MESSAGE_DISPLAY    
        printAfIncomingMsgHeaderNames();
#endif
        while (1)
    {
        while (SRDY_IS_HIGH());      //wait until SRDY goes low indicating a message has been received.   
        displayMessages();
    }
    
}
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;
        }
    } 
}