Skip to content

Commit 67cb604

Browse files
author
Federico Fissore
committed
Added source folder to Uploader.uploadUsingPreferences
Cleaned up Uploader.message Better SSH output streams consuming Introduced generic SCP.scpFile method and refactored SCP.scpHexToBoard
1 parent 9f84ae1 commit 67cb604

File tree

4 files changed

+93
-69
lines changed

4 files changed

+93
-69
lines changed

app/src/cc/arduino/packages/Uploader.java

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,60 @@
2626

2727
package cc.arduino.packages;
2828

29-
import static processing.app.I18n._;
30-
31-
import java.util.Collection;
32-
3329
import processing.app.I18n;
3430
import processing.app.Preferences;
3531
import processing.app.debug.MessageConsumer;
3632
import processing.app.debug.MessageSiphon;
3733
import processing.app.debug.RunnerException;
3834

39-
public abstract class Uploader implements MessageConsumer {
35+
import java.io.File;
36+
import java.util.Arrays;
37+
import java.util.Collection;
38+
import java.util.List;
39+
40+
import static processing.app.I18n._;
41+
42+
public abstract class Uploader implements MessageConsumer {
43+
44+
private static final List<String> STRINGS_TO_SUPPRESS;
45+
private static final List<String> AVRDUDE_PROBLEMS;
46+
47+
static {
48+
STRINGS_TO_SUPPRESS = Arrays.asList("Connecting to programmer:",
49+
"Found programmer: Id = \"CATERIN\"; type = S",
50+
"Software Version = 1.0; No Hardware Version given.",
51+
"Programmer supports auto addr increment.",
52+
"Programmer supports buffered memory access with buffersize=128 bytes.",
53+
"Programmer supports the following devices:", "Device code: 0x44");
54+
55+
AVRDUDE_PROBLEMS = Arrays.asList("Programmer is not responding",
56+
"programmer is not responding",
57+
"protocol error", "avrdude: ser_open(): can't open device",
58+
"avrdude: ser_drain(): read error",
59+
"avrdude: ser_send(): write error",
60+
"avrdude: error: buffered memory access not supported.");
61+
}
62+
63+
private static boolean stringContainsOneOf(String input, List<String> listOfStrings) {
64+
for (String string : listOfStrings) {
65+
if (input.contains(string)) {
66+
return true;
67+
}
68+
}
69+
return false;
70+
}
4071

41-
private String error = null;
72+
private String error;
73+
protected boolean verbose;
74+
protected boolean notFoundError;
4275

43-
protected boolean verbose = Preferences.getBoolean("upload.verbose");
76+
protected Uploader() {
77+
this.error = null;
78+
this.verbose = Preferences.getBoolean("upload.verbose");
79+
this.notFoundError = false;
80+
}
4481

45-
public abstract boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
46-
throws RunnerException;
82+
public abstract boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer) throws RunnerException;
4783

4884
public abstract boolean burnBootloader() throws RunnerException;
4985

@@ -55,13 +91,11 @@ public String getAuthorizationKey() {
5591
return null;
5692
}
5793

58-
protected boolean executeUploadCommand(Collection<String> command)
59-
throws RunnerException {
94+
protected boolean executeUploadCommand(Collection<String> command) throws RunnerException {
6095
return executeUploadCommand(command.toArray(new String[0]));
6196
}
6297

63-
protected boolean executeUploadCommand(String command[])
64-
throws RunnerException {
98+
protected boolean executeUploadCommand(String command[]) throws RunnerException {
6599
notFoundError = false;
66100
int result = -1;
67101

@@ -80,29 +114,21 @@ protected boolean executeUploadCommand(String command[])
80114
} catch (Exception e) {
81115
e.printStackTrace();
82116
}
83-
117+
84118
if (error != null) {
85119
RunnerException exception = new RunnerException(error);
86120
exception.hideStackTrace();
87121
throw exception;
88122
}
89123

90-
return result == 0;
124+
return result == 0;
91125
}
92126

93-
boolean notFoundError;
94-
95127
public void message(String s) {
96128
// selectively suppress a bunch of avrdude output for AVR109/Caterina that should already be quelled but isn't
97-
if (!verbose && (
98-
s.indexOf("Connecting to programmer:") != -1 ||
99-
s.indexOf("Found programmer: Id = \"CATERIN\"; type = S") != -1 ||
100-
s.indexOf("Software Version = 1.0; No Hardware Version given.") != -1 ||
101-
s.indexOf("Programmer supports auto addr increment.") != -1 ||
102-
s.indexOf("Programmer supports buffered memory access with buffersize=128 bytes.") != -1 ||
103-
s.indexOf("Programmer supports the following devices:") != -1 ||
104-
s.indexOf("Device code: 0x44") != -1))
129+
if (!verbose && stringContainsOneOf(s, STRINGS_TO_SUPPRESS)) {
105130
s = "";
131+
}
106132

107133
System.err.print(s);
108134

@@ -119,13 +145,7 @@ public void message(String s) {
119145
error = _("Device is not responding, check the right serial port is selected or RESET the board right before exporting");
120146
return;
121147
}
122-
if (s.contains("Programmer is not responding") ||
123-
s.contains("programmer is not responding") ||
124-
s.contains("protocol error") ||
125-
s.contains("avrdude: ser_open(): can't open device") ||
126-
s.contains("avrdude: ser_drain(): read error") ||
127-
s.contains("avrdude: ser_send(): write error") ||
128-
s.contains("avrdude: error: buffered memory access not supported.")) {
148+
if (stringContainsOneOf(s, AVRDUDE_PROBLEMS)) {
129149
error = _("Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.");
130150
return;
131151
}

app/src/cc/arduino/packages/uploaders/SSHUploader.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String getAuthorizationKey() {
3636
}
3737

3838
@Override
39-
public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer) throws RunnerException {
39+
public boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer) throws RunnerException {
4040
if (usingProgrammer) {
4141
System.err.println(_("Http upload using programmer not supported"));
4242
return false;
@@ -51,7 +51,8 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
5151
session.setUserInfo(new NetworkMonitor.NoInteractionUserInfo());
5252
session.connect(30000);
5353

54-
String uploadedSketchFile = new SCP(session).scpHexToBoard(buildPath, className);
54+
SCP scp = new SCP(session);
55+
String uploadedSketchFile = scp.scpHexToBoard(buildPath, className);
5556

5657
TargetPlatform targetPlatform = Base.getTargetPlatform();
5758
PreferencesMap prefs = Preferences.getMap();
@@ -127,16 +128,9 @@ protected boolean execSyncCommand(String command, boolean ignoreError) throws JS
127128
protected int consumeOutputSyncAndReturnExitCode(Channel channel, InputStream stdout, InputStream stderr) throws IOException {
128129
byte[] tmp = new byte[102400];
129130
while (true) {
130-
while (stdout.available() > 0) {
131-
int i = stdout.read(tmp, 0, tmp.length);
132-
if (i < 0) break;
133-
System.out.print(new String(tmp, 0, i));
134-
}
135-
while (stderr != null && stderr.available() > 0) {
136-
int i = stderr.read(tmp, 0, tmp.length);
137-
if (i < 0) break;
138-
System.err.print(new String(tmp, 0, i));
139-
}
131+
consumeStream(tmp, stdout, System.out);
132+
consumeStream(tmp, stderr, System.err);
133+
140134
if (channel.isClosed()) {
141135
return channel.getExitStatus();
142136
}
@@ -148,6 +142,16 @@ protected int consumeOutputSyncAndReturnExitCode(Channel channel, InputStream st
148142
}
149143
}
150144

145+
private void consumeStream(byte[] buffer, InputStream in, PrintStream out) throws IOException {
146+
while (in != null && in.available() > 0) {
147+
int length = in.read(buffer, 0, buffer.length);
148+
if (length < 0) {
149+
break;
150+
}
151+
out.print(new String(buffer, 0, length));
152+
}
153+
}
154+
151155
}
152156

153157
private static class SSHAVRDude extends SSH {
@@ -173,13 +177,13 @@ public SCP(Session session) {
173177
super(session);
174178
}
175179

176-
public String scpHexToBoard(String buildPath, String className) throws JSchException, IOException {
180+
public void scpFile(File from, String absolutePathToDestination) throws JSchException, IOException {
177181
Channel channel = null;
178182
OutputStream out = null;
179183
InputStream in = null;
180184
try {
181185
channel = session.openChannel("exec");
182-
((ChannelExec) channel).setCommand("scp -t " + SKETCH_FILE);
186+
((ChannelExec) channel).setCommand("scp -t " + absolutePathToDestination);
183187

184188
out = channel.getOutputStream();
185189
in = channel.getInputStream();
@@ -188,15 +192,11 @@ public String scpHexToBoard(String buildPath, String className) throws JSchExcep
188192

189193
ensureAcknowledged(out, in);
190194

191-
File hex = new File(buildPath, className + ".hex");
192-
193-
sendFileSizeAndName(out, in, hex);
195+
sendFileSizeAndName(out, in, from);
194196
ensureAcknowledged(out, in);
195197

196-
sendFileContents(out, hex);
198+
sendFileContents(out, from);
197199
ensureAcknowledged(out, in);
198-
199-
return SKETCH_FILE;
200200
} finally {
201201
if (out != null) {
202202
out.close();
@@ -210,6 +210,11 @@ public String scpHexToBoard(String buildPath, String className) throws JSchExcep
210210
}
211211
}
212212

213+
public String scpHexToBoard(String buildPath, String className) throws JSchException, IOException {
214+
scpFile(new File(buildPath, className + ".hex"), SKETCH_FILE);
215+
return SKETCH_FILE;
216+
}
217+
213218
private void ensureAcknowledged(OutputStream out, InputStream in) throws IOException {
214219
out.flush();
215220

app/src/cc/arduino/packages/uploaders/SerialUploader.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import static processing.app.I18n._;
3232

33+
import java.io.File;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536

@@ -47,9 +48,7 @@
4748

4849
public class SerialUploader extends Uploader {
4950

50-
public boolean uploadUsingPreferences(String buildPath, String className,
51-
boolean usingProgrammer)
52-
throws RunnerException {
51+
public boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer) throws RunnerException {
5352
// FIXME: Preferences should be reorganized
5453
TargetPlatform targetPlatform = Base.getTargetPlatform();
5554
PreferencesMap prefs = Preferences.getMap();
@@ -61,7 +60,7 @@ public boolean uploadUsingPreferences(String buildPath, String className,
6160
if (usingProgrammer || prefs.get("upload.protocol") == null) {
6261
return uploadUsingProgrammer(buildPath, className);
6362
}
64-
63+
6564
// need to do a little dance for Leonardo and derivatives:
6665
// open then close the port at the magic baudrate (usually 1200 bps) first
6766
// to signal to the sketch that it should reset into bootloader. after doing
@@ -70,10 +69,10 @@ public boolean uploadUsingPreferences(String buildPath, String className,
7069
// sketch.
7170
String t = prefs.get("upload.use_1200bps_touch");
7271
boolean doTouch = t != null && t.equals("true");
73-
72+
7473
t = prefs.get("upload.wait_for_upload_port");
7574
boolean waitForUploadPort = (t != null) && t.equals("true");
76-
75+
7776
if (doTouch) {
7877
String uploadPort = prefs.get("serial.port");
7978
try {
@@ -93,7 +92,7 @@ public boolean uploadUsingPreferences(String buildPath, String className,
9392
// have already occured before we start scanning.
9493
if (!Base.isMacOS())
9594
Thread.sleep(300);
96-
95+
9796
uploadPort = waitForUploadPort(uploadPort, before);
9897
} else {
9998
Thread.sleep(400);
@@ -109,7 +108,7 @@ public boolean uploadUsingPreferences(String buildPath, String className,
109108
else
110109
prefs.put("serial.port.file", uploadPort);
111110
}
112-
111+
113112
prefs.put("build.path", buildPath);
114113
prefs.put("build.project_name", className);
115114
if (verbose)
@@ -130,7 +129,7 @@ public boolean uploadUsingPreferences(String buildPath, String className,
130129
} catch (Exception e) {
131130
throw new RunnerException(e);
132131
}
133-
132+
134133
// Remove the magic baud rate (1200bps) to avoid
135134
// future unwanted board resets
136135
try {
@@ -212,7 +211,7 @@ private String waitForUploadPort(String uploadPort, List<String> before)
212211
return uploadPort;
213212
}
214213
}
215-
214+
216215
// Something happened while detecting port
217216
throw new RunnerException(
218217
_("Couldn't find a Board on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload."));
@@ -256,10 +255,10 @@ public boolean uploadUsingProgrammer(String buildPath, String className)
256255
throw new RunnerException(e);
257256
}
258257
}
259-
258+
260259
public boolean burnBootloader() throws RunnerException {
261260
TargetPlatform targetPlatform = Base.getTargetPlatform();
262-
261+
263262
// Find preferences for the selected programmer
264263
PreferencesMap programmerPrefs;
265264
String programmer = Preferences.get("programmer");
@@ -272,12 +271,12 @@ public boolean burnBootloader() throws RunnerException {
272271
} else {
273272
programmerPrefs = targetPlatform.getProgrammer(programmer);
274273
}
275-
274+
276275
// Build configuration for the current programmer
277276
PreferencesMap prefs = Preferences.getMap();
278277
prefs.putAll(Base.getBoardPreferences());
279278
prefs.putAll(programmerPrefs);
280-
279+
281280
// Create configuration for bootloader tool
282281
PreferencesMap toolPrefs = new PreferencesMap();
283282
String tool = prefs.get("bootloader.tool");
@@ -295,7 +294,7 @@ public boolean burnBootloader() throws RunnerException {
295294
if (toolPrefs.size() == 0)
296295
throw new RunnerException(I18n.format(_("Could not find tool {0}"),
297296
tool));
298-
297+
299298
// Merge tool with global configuration
300299
prefs.putAll(toolPrefs);
301300
if (verbose) {
@@ -305,7 +304,7 @@ public boolean burnBootloader() throws RunnerException {
305304
prefs.put("erase.verbose", prefs.get("erase.params.quiet"));
306305
prefs.put("bootloader.verbose", prefs.get("bootloader.params.quiet"));
307306
}
308-
307+
309308
try {
310309
String pattern = prefs.get("erase.pattern");
311310
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);

app/src/processing/app/Sketch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ protected boolean upload(String buildPath, String suggestedClassName, boolean us
16841684
}
16851685

16861686
try {
1687-
success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer);
1687+
success = uploader.uploadUsingPreferences(getFolder(), buildPath, suggestedClassName, usingProgrammer);
16881688
} finally {
16891689
if (uploader.requiresAuthorization() && !success) {
16901690
Preferences.remove(uploader.getAuthorizationKey());

0 commit comments

Comments
 (0)