+ System.out.printf("Waiting for device to become ready\n");
+
+ Thread.sleep(1000);
+ }
+ if (link == null)
+ throw new IOException(String.format("%s: open failed",
+ device.toShortString()));
+
+ System.out.printf("Checking device ready\n");
+
+ /* See if the link is usable already */
+ if (is_pair_programmed() || link.is_loader()) {
+ System.out.printf("Device ready for use\n");
+ do_success(link);
+ return;
+ }
+
+ System.out.printf("Checking log erased\n");
+
+ if (is_log_erased()) {
+ System.out.printf("Fetching config data\n");
+ AltosConfigData config_data = link.config_data();
+ System.out.printf("version %s\n", config_data.version);
+ /* Completely erase TeleGPS flash when firmware is old */
+ if (config_data.compare_version("1.9.7") < 0)
+ {
+ if (!do_notify_erase(config_data))
+ throw new IOException(String.format("%s: not erasing log",
+ device.toShortString()));
+ System.out.printf("Erasing log\n");
+ link.printf("Z DoIt\n");
+ link.synchronize(120 * 1000);
+ }
+ }
+
+ java.util.List<AltosDevice> prev_devices =
+ AltosUSBDevice.list(AltosLib.product_altusmetrum);
+
+ /* Nope, switch to loader and
+ * wait for it to re-appear
+ */
+
+ System.out.printf("Switch to loader\n");
+
+ link.to_loader();
+
+ /* This is a bit fragile, but
+ * I'm not sure what else to
+ * do other than ask the user.
+ *
+ * Search for a device which
+ * wasn't there before we
+ * asked the target to switch
+ * to loader mode
+ */
+
+ device = null;
+ for (;;) {
+ Thread.sleep(100);
+ java.util.List<AltosDevice> devices =
+ AltosUSBDevice.list(AltosLib.product_altusmetrum);
+
+ for (AltosDevice d : devices) {
+ boolean matched = false;
+ System.out.printf("\tfound device %s\n", d.toShortString());
+ for (AltosDevice p : prev_devices)
+ if (d.equals(p)) {
+ matched = true;
+ break;
+ }
+ if (!matched) {
+ System.out.printf("Identified new device %s\n", d.toShortString());
+ device = (AltosUSBDevice) d;
+ new_device = true;
+ break;
+ }
+ }
+ if (device != null)
+ break;
+ }