/**
	@brief
	Toggles the reset line of the XBee device.

	@param[in]	xbee	XBee device to reset.

	@retval	0			Successfully toggled reset.
	@retval	-EINVAL	Invalid xbee_dev_t passed to function.
	@retval	-EIO		This XBee device doesn't have an interface to the
							module's reset pin.
*/
_xbee_device_debug
int xbee_dev_reset( xbee_dev_t *xbee)
{
	uint16_t t;

	if (! xbee)
	{
		return -EINVAL;
	}
	if (! xbee->reset)
	{
		return -EIO;
	}

	xbee->reset( xbee, 1);
	// need to hold reset for 250ns
	// Wait for the millisecond timer to roll over twice to ensure
	// 250ns elapsed.  Consider adding high-resolution sleep function
	// to the platform files (usleep?).
	t = XBEE_SET_TIMEOUT_MS(XBEE_MS_TIMER_RESOLUTION + 1);
	while (! XBEE_CHECK_TIMEOUT_MS(t));
	xbee->reset( xbee, 0);
	#ifdef XBEE_DEVICE_ENABLE_ATMODE
		xbee->mode = XBEE_MODE_UNKNOWN;
	#endif

	return 0;
}
Example #2
0
void wait_startup( void)
{
    uint16_t t;
    
    printf( "waiting for XBee to start up...\n");
    t = XBEE_SET_TIMEOUT_MS( 3000);     // 3 seconds
    while (! XBEE_CHECK_TIMEOUT_MS( t));
}
/*
	main

	Initiate communication with the XBee module, then accept AT commands from
	STDIO, pass them to the XBee module and print the result.
*/
int main( void)
{
    char cmdstr[80];
    int status;
    uint16_t t;


    if (xbee_dev_init( &my_xbee, &XBEE_SERPORT, xbee_awake_pin, xbee_reset_pin))
    {
        printf( "Failed to initialize device.\n");
        return 0;
    }

    //xbee_dev_reset( &my_xbee);
    // give the XBee 500ms to wake up after resetting it (or exit if it
    // receives a packet)
    t = XBEE_SET_TIMEOUT_MS(500);
    while (! XBEE_CHECK_TIMEOUT_MS(t) && xbee_dev_tick( &my_xbee) <= 0);

    // Initialize the AT Command layer for this XBee device and have the
    // driver query it for basic information (hardware version, firmware version,
    // serial number, IEEE address, etc.)
    xbee_cmd_init_device( &my_xbee);
    printf( "Waiting for driver to query the XBee device...\n");
    do {
        xbee_dev_tick( &my_xbee);
        status = xbee_cmd_query_status( &my_xbee);
    } while (status == -EBUSY);
    if (status)
    {
        printf( "Error %d waiting for query to complete.\n", status);
    }

    // report on the settings
    xbee_dev_dump_settings( &my_xbee, XBEE_DEV_DUMP_FLAG_DEFAULT);

    printATCmds( &my_xbee);
    printf("Target setting for remote commands:\n");
    printf(" > addr   (addr is hex ieee addr, high bytes assumed 0013a200)\n");
    printf(" >        (reset to local device)\n");
    printf(" <        (reinstate previous remote target)\n");

    while (1)
    {
        while (xbee_readline( cmdstr, sizeof cmdstr) == -EAGAIN)
        {
            xbee_dev_tick( &my_xbee);
        }

        if (! strncmpi( cmdstr, "menu", 4))
        {
            printATCmds( &my_xbee);
        }
        else if (! strcmpi( cmdstr, "quit"))
        {
            return 0;
        }
        else if ( cmdstr[0] == '>')
        {
            have_target = set_target( cmdstr+1, &target_ieee);
        }
        else if ( cmdstr[0] == '<')
        {
            if (ever_had_target)
            {
                have_target = 1;
                printf("Reinstating %" PRIsFAR "\n",
                       addr64_format(cmdstr, &target_ieee));
            }
            else
            {
                printf("Nothing to reinstate\n");
            }
        }
        else
        {
            have_target ?
            process_command_remote( &my_xbee, cmdstr, &target_ieee) :
            process_command( &my_xbee, cmdstr);
        }
    }
}
int main( int argc, char *argv[])
{
    const char *firmware = NULL;
    char xmodem_buffer[69];
    char cmdstr[80];
    int status, i;
    xbee_serial_t XBEE_SERPORT;
    FILE *fw_file = NULL;
#ifdef VERBOSE
    uint16_t last_state;
#endif
    uint16_t last_packet;
    target_t *target = NULL;

    // turn off buffering so status changes (lines ending in \r) display
    setvbuf( stdout, NULL, _IONBF, 0);

    memset( target_list, 0, sizeof target_list);

    // set serial port
    parse_serial_arguments( argc, argv, &XBEE_SERPORT);

    // parse args for this program
    parse_args( argc, argv);

    // initialize the serial and device layer for this XBee device
    if (xbee_dev_init( &my_xbee, &XBEE_SERPORT, NULL, NULL))
    {
        printf( "Failed to initialize device.\n");
        return 0;
    }

    // Initialize the WPAN layer of the XBee device driver.  This layer enables
    // endpoints and clusters, and is required for all ZigBee layers.
    xbee_wpan_init( &my_xbee, &sample_endpoints.zdo);

    // Initialize the AT Command layer for this XBee device and have the
    // driver query it for basic information (hardware version, firmware version,
    // serial number, IEEE address, etc.)
    xbee_cmd_init_device( &my_xbee);
    printf( "Waiting for driver to query the XBee device...\n");
    do {
        xbee_dev_tick( &my_xbee);
        status = xbee_cmd_query_status( &my_xbee);
    } while (status == -EBUSY);
    if (status)
    {
        printf( "Error %d waiting for query to complete.\n", status);
    }

    // report on the settings
    xbee_dev_dump_settings( &my_xbee, XBEE_DEV_DUMP_FLAG_DEFAULT);

    print_help();

    if (dynamic_profile.profile_id != 0)
    {
        printf( "Using profile ID 0x%04x with%s APS encryption.\n",
                dynamic_profile.profile_id,
                (dynamic_profile.flags & WPAN_CLUST_FLAG_ENCRYPT) ? "" : "out");
        xbee_ota_find_devices( &my_xbee.wpan_dev, xbee_found, NULL);
    }

    while (1)
    {
        while (xbee_readline( cmdstr, sizeof cmdstr) == -EAGAIN)
        {
            wpan_tick( &my_xbee.wpan_dev);

            if (fw_file != NULL)
            {
                if (xbee_xmodem_tx_tick( &xbee_ota.xbxm) != 0)
                {
                    uint16_t timer;

                    printf( "upload complete     \n");
                    fclose( fw_file);
                    fw_file = NULL;

                    // wait one second for device to reboot then rediscover it
                    timer = XBEE_SET_TIMEOUT_MS(1000);
                    while (! XBEE_CHECK_TIMEOUT_MS( timer));

                    xbee_ota_find_devices( &my_xbee.wpan_dev, xbee_found, NULL);
                }

#ifdef VERBOSE
                if (last_state != xbee_ota.xbxm.state)
                {
                    printf( "state change from %u to %u\n", last_state,
                            xbee_ota.xbxm.state);
                    last_state = xbee_ota.xbxm.state;
                }
#endif
                if (last_packet != xbee_ota.xbxm.packet_num)
                {
#ifdef VERBOSE
                    printf( "packet #%u\n", xbee_ota.xbxm.packet_num);
#else
                    printf( " %" PRIu32 " bytes\r",
                            UINT32_C(64) * xbee_ota.xbxm.packet_num);
#endif
                    last_packet = xbee_ota.xbxm.packet_num;
                }
            }
        }

        if (! strcmpi( cmdstr, "quit"))
        {
            return 0;
        }
        else if (! strcmpi( cmdstr, "help") || ! strcmp( cmdstr, "?"))
        {
            print_help();
        }
        else if (! strcmpi( cmdstr, "find"))
        {
            if (dynamic_profile.profile_id == 0)
            {
                puts( "Error: specify a profile via cmd line or 'profile' cmd");
            }
            else
            {
                xbee_ota_find_devices( &my_xbee.wpan_dev, xbee_found, NULL);
            }
        }
        else if (! strncmpi( cmdstr, "profile ", 8))
        {
            dynamic_profile.profile_id = strtoul( &cmdstr[8], NULL, 16);
            printf( "Profile ID set to 0x%04x\n", dynamic_profile.profile_id);
            profile_changed();
        }
        else if (! strcmpi( cmdstr, "target"))
        {
            puts( " #: --IEEE Address--- Ver. --------Application Name--------"
                  " ---Date Code----");
            for (i = 0; i < target_index; ++i)
            {
                print_target( i);
            }
            puts( "End of List");
        }
        else if (! strncmpi( cmdstr, "target ", 7))
        {
            i = (int) strtoul( &cmdstr[7], NULL, 10);
            if (target_index == 0)
            {
                printf( "error, no targets in list, starting search now...\n");
                xbee_ota_find_devices( &my_xbee.wpan_dev, xbee_found, NULL);
            }
            else if (i < 0 || i >= target_index)
            {
                printf( "error, index %d is invalid (must be 0 to %u)\n", i,
                        target_index - 1);
            }
            else
            {
                target = &target_list[i];
                puts( "set target to:");
                print_target( i);
            }
        }
        else if (! strcmpi( cmdstr, "F") && target != NULL)
        {
            // If the target is stuck in the bootloader, send an 'F' to start
            // a firmware update.
            wpan_envelope_t envelope;

            wpan_envelope_create( &envelope, &my_xbee.wpan_dev, &target->ieee,
                                  WPAN_NET_ADDR_UNDEFINED);
            envelope.options = current_profile->flags;
            xbee_transparent_serial_str( &envelope, "F");
        }
        else if (! strcmpi( cmdstr, "firmware"))
        {
            firmware = get_file();
        }
        else if (! strcmpi( cmdstr, "password"))
        {
            set_password( "");
            puts( "cleared password (will use default of a single null byte)");
        }
        else if (! strncmpi( cmdstr, "password ", 9))
        {
            set_password( &cmdstr[9]);
            printf( "set password to [%.*s]\n", xbee_ota.auth_length,
                    xbee_ota.auth_data);
        }
        else if (! strcmpi( cmdstr, "aps"))
        {
            xbee_ota.flags ^= XBEE_OTA_FLAG_APS_ENCRYPT;
            printf( "APS encryption %sabled\n",
                    (xbee_ota.flags & XBEE_OTA_FLAG_APS_ENCRYPT) ? "en" : "dis");
        }
        else if (! strcmpi( cmdstr, "go"))
        {
            if (target == NULL)
            {
                if (target_index > 0)
                {
                    target = &target_list[0];
                }
                else
                {
                    puts( "no targets available to send to");
                    continue;
                }
            }

            if (firmware == NULL)
            {
                firmware = get_file();
                if (firmware == NULL)
                {
                    printf( "Canceled.\n");
                    continue;
                }
            }

            printf( "Starting xmodem upload of\n  %s\n", firmware);

            fw_file = fopen( firmware, "rb");
            if (! fw_file)
            {
                printf( "Failed to open '%s'\n", firmware);
                exit( -1);
            }

            status = xbee_ota_init( &xbee_ota, &my_xbee.wpan_dev, &target->ieee);

            if (status)
            {
                printf( "%s returned %d\n", "xbee_ota_init", status);
                continue;
            }

            status = xbee_xmodem_set_source( &xbee_ota.xbxm, xmodem_buffer,
                                             fw_read, fw_file);
            if (status)
            {
                printf( "%s returned %d\n", "xbee_xmodem_set_source", status);
                continue;
            }

            // reset the xbee_xmodem_state_t state machine, keeping existing flags
            status = xbee_xmodem_tx_init( &xbee_ota.xbxm, xbee_ota.xbxm.flags);
            if (status)
            {
                printf( "%s returned %d\n", "xbee_xmodem_tx_init", status);
                continue;
            }

            // reset copies of basic cluster -- need to refresh after update
            memset( &target->basic, 0, sizeof(target->basic));

#ifdef VERBOSE
            last_state = last_packet = 0;
#endif

            // main loop will tick the xmodem transfre until fw_file == NULL
        }
#ifdef XBEE_XMODEM_TESTING
        else if (! strcmpi( cmdstr, "ACK"))
        {
            xbee_ota.xbxm.flags |= XBEE_XMODEM_FLAG_DROP_ACK;
        }
        else if (! strcmpi( cmdstr, "FRAME"))
        {
            xbee_ota.xbxm.flags |= XBEE_XMODEM_FLAG_DROP_FRAME;
        }
        else if (! strcmpi( cmdstr, "CRC"))
        {
            xbee_ota.xbxm.flags |= XBEE_XMODEM_FLAG_BAD_CRC;
        }
#endif
        else if (! strncmpi( cmdstr, "AT", 2))
        {
            process_command( &my_xbee, &cmdstr[2]);
        }
        else
        {
            printf( "unknown command: '%s'\n", cmdstr);
        }
    }
}