From ae02a50dbafe9901717acf3061c1859815c5466a Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Sun, 6 Dec 2015 20:23:26 -0500 Subject: [PATCH] Bug 483729 - Make Arduino upload a proper launch. Creates and ArduinoLaunch object to manage the pause and resume of the serial port on launch and terminate. Registers the uploader process with the debug framework so that it's connected properly to the launch and the console. Change-Id: If14cf8ddf2c1b6ceda19bce8d37e07ebce9700f2 --- .../core/internal/launch/ArduinoLaunch.java | 47 ++++++++++ .../ArduinoLaunchConfigurationDelegate.java | 93 +++++++------------ 2 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunch.java diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunch.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunch.java new file mode 100644 index 00000000000..6554df39f7d --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunch.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2015 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.cdt.arduino.core.internal.launch; + +import org.eclipse.cdt.arduino.core.internal.remote.ArduinoRemoteConnection; +import org.eclipse.debug.core.DebugEvent; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.Launch; +import org.eclipse.debug.core.model.ISourceLocator; +import org.eclipse.remote.core.IRemoteConnection; + +public class ArduinoLaunch extends Launch { + + private final ArduinoRemoteConnection target; + private boolean wasOpen; + + public ArduinoLaunch(ILaunchConfiguration launchConfiguration, String mode, ISourceLocator locator, + IRemoteConnection target) { + super(launchConfiguration, mode, locator); + this.target = target.getService(ArduinoRemoteConnection.class); + + DebugPlugin.getDefault().addDebugEventListener(this); + } + + public void start() { + this.wasOpen = target.getRemoteConnection().isOpen(); + if (wasOpen) { + target.pause(); + } + } + + @Override + public void handleDebugEvents(DebugEvent[] events) { + super.handleDebugEvents(events); + if (isTerminated() && wasOpen) { + target.resume(); + wasOpen = false; + } + } + +} diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunchConfigurationDelegate.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunchConfigurationDelegate.java index 1c05e8b24b9..1121ff0c00b 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunchConfigurationDelegate.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/launch/ArduinoLaunchConfigurationDelegate.java @@ -15,14 +15,12 @@ import java.io.IOException; import org.eclipse.cdt.arduino.core.internal.Activator; import org.eclipse.cdt.arduino.core.internal.Messages; import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuildConfiguration; -import org.eclipse.cdt.arduino.core.internal.console.ArduinoConsoleService; import org.eclipse.cdt.arduino.core.internal.remote.ArduinoRemoteConnection; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.jobs.Job; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; @@ -43,6 +41,11 @@ public class ArduinoLaunchConfigurationDelegate extends LaunchConfigurationDeleg return connectionType.getConnection(connectionName); } + @Override + public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException { + return new ArduinoLaunch(configuration, mode, null, getTarget(configuration)); + } + @Override public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { @@ -71,69 +74,39 @@ public class ArduinoLaunchConfigurationDelegate extends LaunchConfigurationDeleg @Override public void launch(final ILaunchConfiguration configuration, String mode, final ILaunch launch, IProgressMonitor monitor) throws CoreException { - new Job(Messages.ArduinoLaunchConfigurationDelegate_0) { - protected IStatus run(IProgressMonitor monitor) { - try { - ArduinoConsoleService consoleService = Activator.getConsoleService(); - IRemoteConnection target = getTarget(configuration); - if (target == null) { - return new Status(IStatus.ERROR, Activator.getId(), - Messages.ArduinoLaunchConfigurationDelegate_2); - } - ArduinoRemoteConnection arduinoTarget = target.getService(ArduinoRemoteConnection.class); + try { + IRemoteConnection target = getTarget(configuration); + if (target == null) { + throw new CoreException( + new Status(IStatus.ERROR, Activator.getId(), Messages.ArduinoLaunchConfigurationDelegate_2)); + } + ArduinoRemoteConnection arduinoTarget = target.getService(ArduinoRemoteConnection.class); - // The project - IProject project = (IProject) configuration.getMappedResources()[0]; + // The project + IProject project = (IProject) configuration.getMappedResources()[0]; - // The build config - ArduinoBuildConfiguration arduinoConfig = ArduinoBuildConfiguration.getConfig(project, - arduinoTarget, monitor); - String[] uploadCmd = arduinoConfig.getUploadCommand(arduinoTarget.getPortName()); + // The build config + ArduinoBuildConfiguration arduinoConfig = ArduinoBuildConfiguration.getConfig(project, arduinoTarget, + monitor); + String[] uploadCmd = arduinoConfig.getUploadCommand(arduinoTarget.getPortName()); - // If opened, temporarily close the connection so we can use - // it to download the firmware. - boolean wasOpened = target.isOpen(); - if (wasOpened) { - arduinoTarget.pause(); - } + StringBuffer cmdStr = new StringBuffer(uploadCmd[0]); + for (int i = 1; i < uploadCmd.length; ++i) { + cmdStr.append(' '); + cmdStr.append(uploadCmd[i]); + } + // Start the launch + ((ArduinoLaunch) launch).start(); - StringBuffer cmdStr = new StringBuffer(uploadCmd[0]); - for (int i = 1; i < uploadCmd.length; ++i) { - cmdStr.append(' '); - cmdStr.append(uploadCmd[i]); - } - cmdStr.append('\n'); - consoleService.writeOutput(cmdStr.toString()); + // Run the process and capture the results in the console + ProcessBuilder processBuilder = new ProcessBuilder(uploadCmd).directory(arduinoConfig.getBuildDirectory()); + arduinoConfig.setEnvironment(processBuilder.environment()); + Process process = processBuilder.start(); + DebugPlugin.newProcess(launch, process, cmdStr.toString()); + } catch (IOException e) { + throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e)); + } - // Run the process and capture the results in the console - ProcessBuilder processBuilder = new ProcessBuilder(uploadCmd) - .directory(arduinoConfig.getBuildDirectory()); - arduinoConfig.setEnvironment(processBuilder.environment()); - Process process = processBuilder.start(); - - consoleService.monitor(process, null, null); - try { - process.waitFor(); - } catch (InterruptedException e) { - } - - consoleService.writeOutput("Upload complete\n"); - - // Reopen the connection - if (wasOpened) { - arduinoTarget.resume(); - } - } catch (CoreException e) { - return e.getStatus(); - } catch (IOException e) { - return new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e); - } finally { - DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch); - } - - return Status.OK_STATUS; - }; - }.schedule(); } }