static gboolean set_profile(PyraEventhandler *eventhandler, guint profile_number) { PyraEventhandlerPrivate *priv = eventhandler->priv; guint profile_index; GError *local_error = NULL; if (profile_number == 0) return TRUE; profile_index = profile_number - 1; if (priv->actual_profile_index != profile_index) { if (!pyra_actual_profile_write(priv->device, profile_index, &local_error)) { g_warning(_("Could not activate profile %i: %s"), profile_number, local_error->message); g_clear_error(&local_error); return FALSE; } profile_changed(eventhandler, profile_number); } return TRUE; }
void parse_args( int argc, char *argv[]) { int i; for (i = 1; i < argc; ++i) { if (strncmp( argv[i], "--profile=", 10) == 0) { dynamic_profile.profile_id = (uint16_t) strtoul( &argv[i][10], NULL, 16); } else if (strcmp( argv[i], "--aps") == 0) { dynamic_profile.flags = WPAN_CLUST_FLAG_ENCRYPT; } else if (strncmp( argv[i], "--password=", 11) == 0) { set_password( &argv[i][11]); } } profile_changed(); }
static void profile_changed_cb(gpointer source, guchar profile_number, gpointer user_data) { PyraEventhandler *eventhandler = PYRA_EVENTHANDLER(user_data); profile_changed(eventhandler, profile_number); }
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); } } }