1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Added source folder to Uploader.uploadUsingPreferences

Cleaned up Uploader.message
Better SSH output streams consuming
Introduced generic SCP.scpFile method and refactored SCP.scpHexToBoard
This commit is contained in:
Federico Fissore 2013-06-27 11:08:06 +02:00
parent 9f84ae1d64
commit 67cb6047de
4 changed files with 93 additions and 69 deletions

View File

@ -26,24 +26,60 @@
package cc.arduino.packages;
import static processing.app.I18n._;
import java.util.Collection;
import processing.app.I18n;
import processing.app.Preferences;
import processing.app.debug.MessageConsumer;
import processing.app.debug.MessageSiphon;
import processing.app.debug.RunnerException;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static processing.app.I18n._;
public abstract class Uploader implements MessageConsumer {
private String error = null;
private static final List<String> STRINGS_TO_SUPPRESS;
private static final List<String> AVRDUDE_PROBLEMS;
protected boolean verbose = Preferences.getBoolean("upload.verbose");
static {
STRINGS_TO_SUPPRESS = Arrays.asList("Connecting to programmer:",
"Found programmer: Id = \"CATERIN\"; type = S",
"Software Version = 1.0; No Hardware Version given.",
"Programmer supports auto addr increment.",
"Programmer supports buffered memory access with buffersize=128 bytes.",
"Programmer supports the following devices:", "Device code: 0x44");
public abstract boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
throws RunnerException;
AVRDUDE_PROBLEMS = Arrays.asList("Programmer is not responding",
"programmer is not responding",
"protocol error", "avrdude: ser_open(): can't open device",
"avrdude: ser_drain(): read error",
"avrdude: ser_send(): write error",
"avrdude: error: buffered memory access not supported.");
}
private static boolean stringContainsOneOf(String input, List<String> listOfStrings) {
for (String string : listOfStrings) {
if (input.contains(string)) {
return true;
}
}
return false;
}
private String error;
protected boolean verbose;
protected boolean notFoundError;
protected Uploader() {
this.error = null;
this.verbose = Preferences.getBoolean("upload.verbose");
this.notFoundError = false;
}
public abstract boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer) throws RunnerException;
public abstract boolean burnBootloader() throws RunnerException;
@ -55,13 +91,11 @@ public abstract class Uploader implements MessageConsumer {
return null;
}
protected boolean executeUploadCommand(Collection<String> command)
throws RunnerException {
protected boolean executeUploadCommand(Collection<String> command) throws RunnerException {
return executeUploadCommand(command.toArray(new String[0]));
}
protected boolean executeUploadCommand(String command[])
throws RunnerException {
protected boolean executeUploadCommand(String command[]) throws RunnerException {
notFoundError = false;
int result = -1;
@ -90,19 +124,11 @@ public abstract class Uploader implements MessageConsumer {
return result == 0;
}
boolean notFoundError;
public void message(String s) {
// selectively suppress a bunch of avrdude output for AVR109/Caterina that should already be quelled but isn't
if (!verbose && (
s.indexOf("Connecting to programmer:") != -1 ||
s.indexOf("Found programmer: Id = \"CATERIN\"; type = S") != -1 ||
s.indexOf("Software Version = 1.0; No Hardware Version given.") != -1 ||
s.indexOf("Programmer supports auto addr increment.") != -1 ||
s.indexOf("Programmer supports buffered memory access with buffersize=128 bytes.") != -1 ||
s.indexOf("Programmer supports the following devices:") != -1 ||
s.indexOf("Device code: 0x44") != -1))
if (!verbose && stringContainsOneOf(s, STRINGS_TO_SUPPRESS)) {
s = "";
}
System.err.print(s);
@ -119,13 +145,7 @@ public abstract class Uploader implements MessageConsumer {
error = _("Device is not responding, check the right serial port is selected or RESET the board right before exporting");
return;
}
if (s.contains("Programmer is not responding") ||
s.contains("programmer is not responding") ||
s.contains("protocol error") ||
s.contains("avrdude: ser_open(): can't open device") ||
s.contains("avrdude: ser_drain(): read error") ||
s.contains("avrdude: ser_send(): write error") ||
s.contains("avrdude: error: buffered memory access not supported.")) {
if (stringContainsOneOf(s, AVRDUDE_PROBLEMS)) {
error = _("Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.");
return;
}

View File

@ -36,7 +36,7 @@ public class SSHUploader extends Uploader {
}
@Override
public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer) throws RunnerException {
public boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer) throws RunnerException {
if (usingProgrammer) {
System.err.println(_("Http upload using programmer not supported"));
return false;
@ -51,7 +51,8 @@ public class SSHUploader extends Uploader {
session.setUserInfo(new NetworkMonitor.NoInteractionUserInfo());
session.connect(30000);
String uploadedSketchFile = new SCP(session).scpHexToBoard(buildPath, className);
SCP scp = new SCP(session);
String uploadedSketchFile = scp.scpHexToBoard(buildPath, className);
TargetPlatform targetPlatform = Base.getTargetPlatform();
PreferencesMap prefs = Preferences.getMap();
@ -127,16 +128,9 @@ public class SSHUploader extends Uploader {
protected int consumeOutputSyncAndReturnExitCode(Channel channel, InputStream stdout, InputStream stderr) throws IOException {
byte[] tmp = new byte[102400];
while (true) {
while (stdout.available() > 0) {
int i = stdout.read(tmp, 0, tmp.length);
if (i < 0) break;
System.out.print(new String(tmp, 0, i));
}
while (stderr != null && stderr.available() > 0) {
int i = stderr.read(tmp, 0, tmp.length);
if (i < 0) break;
System.err.print(new String(tmp, 0, i));
}
consumeStream(tmp, stdout, System.out);
consumeStream(tmp, stderr, System.err);
if (channel.isClosed()) {
return channel.getExitStatus();
}
@ -148,6 +142,16 @@ public class SSHUploader extends Uploader {
}
}
private void consumeStream(byte[] buffer, InputStream in, PrintStream out) throws IOException {
while (in != null && in.available() > 0) {
int length = in.read(buffer, 0, buffer.length);
if (length < 0) {
break;
}
out.print(new String(buffer, 0, length));
}
}
}
private static class SSHAVRDude extends SSH {
@ -173,13 +177,13 @@ public class SSHUploader extends Uploader {
super(session);
}
public String scpHexToBoard(String buildPath, String className) throws JSchException, IOException {
public void scpFile(File from, String absolutePathToDestination) throws JSchException, IOException {
Channel channel = null;
OutputStream out = null;
InputStream in = null;
try {
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("scp -t " + SKETCH_FILE);
((ChannelExec) channel).setCommand("scp -t " + absolutePathToDestination);
out = channel.getOutputStream();
in = channel.getInputStream();
@ -188,15 +192,11 @@ public class SSHUploader extends Uploader {
ensureAcknowledged(out, in);
File hex = new File(buildPath, className + ".hex");
sendFileSizeAndName(out, in, hex);
sendFileSizeAndName(out, in, from);
ensureAcknowledged(out, in);
sendFileContents(out, hex);
sendFileContents(out, from);
ensureAcknowledged(out, in);
return SKETCH_FILE;
} finally {
if (out != null) {
out.close();
@ -210,6 +210,11 @@ public class SSHUploader extends Uploader {
}
}
public String scpHexToBoard(String buildPath, String className) throws JSchException, IOException {
scpFile(new File(buildPath, className + ".hex"), SKETCH_FILE);
return SKETCH_FILE;
}
private void ensureAcknowledged(OutputStream out, InputStream in) throws IOException {
out.flush();

View File

@ -30,6 +30,7 @@ package cc.arduino.packages.uploaders;
import static processing.app.I18n._;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@ -47,9 +48,7 @@ import processing.app.helpers.StringReplacer;
public class SerialUploader extends Uploader {
public boolean uploadUsingPreferences(String buildPath, String className,
boolean usingProgrammer)
throws RunnerException {
public boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer) throws RunnerException {
// FIXME: Preferences should be reorganized
TargetPlatform targetPlatform = Base.getTargetPlatform();
PreferencesMap prefs = Preferences.getMap();

View File

@ -1684,7 +1684,7 @@ public class Sketch {
}
try {
success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer);
success = uploader.uploadUsingPreferences(getFolder(), buildPath, suggestedClassName, usingProgrammer);
} finally {
if (uploader.requiresAuthorization() && !success) {
Preferences.remove(uploader.getAuthorizationKey());