forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploader.java
More file actions
executable file
·233 lines (196 loc) · 7.81 KB
/
Uploader.java
File metadata and controls
executable file
·233 lines (196 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Uploader - abstract uploading baseclass (common to both uisp and avrdude)
Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2004-05
Hernando Barragan
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$
*/
package processing.app.debug;
import processing.app.Base;
import processing.app.Preferences;
import processing.app.Serial;
import processing.app.SerialException;
import processing.app.SerialNotFoundException;
import processing.app.I18n;
import static processing.app.I18n._;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
//#ifndef RXTX
//import javax.comm.*;
//#else
// rxtx uses package gnu.io, but all the class names
// are the same as those used by javax.comm
import gnu.io.*;
//#endif
public abstract class Uploader implements MessageConsumer {
static final String BUGS_URL =
_("https://developer.berlios.de/bugs/?group_id=3590");
static final String SUPER_BADNESS =
I18n.format(_("Compiler error, please submit this code to {0}"), BUGS_URL);
RunnerException exception;
//PdePreferences preferences;
//Serial serialPort;
static InputStream serialInput;
static OutputStream serialOutput;
//int serial; // last byte of data received
boolean verbose;
public Uploader() {
}
public abstract boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
throws RunnerException, SerialException;
public abstract boolean burnBootloader() throws RunnerException;
protected void flushSerialBuffer() throws RunnerException, SerialException {
// Cleanup the serial buffer
try {
Serial serialPort = new Serial();
byte[] readBuffer;
while(serialPort.available() > 0) {
readBuffer = serialPort.readBytes();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
serialPort.setDTR(false);
serialPort.setRTS(false);
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
serialPort.setDTR(true);
serialPort.setRTS(true);
serialPort.dispose();
} catch (SerialNotFoundException e) {
throw e;
} catch(Exception e) {
e.printStackTrace();
throw new RunnerException(e.getMessage());
}
}
protected boolean executeUploadCommand(Collection commandDownloader)
throws RunnerException
{
firstErrorFound = false; // haven't found any errors yet
secondErrorFound = false;
notFoundError = false;
int result=0; // pre-initialized to quiet a bogus warning from jikes
String userdir = System.getProperty("user.dir") + File.separator;
try {
String[] commandArray = new String[commandDownloader.size()];
commandDownloader.toArray(commandArray);
if (verbose || Preferences.getBoolean("upload.verbose")) {
for(int i = 0; i < commandArray.length; i++) {
System.out.print(commandArray[i] + " ");
}
System.out.println();
}
Process process = Runtime.getRuntime().exec(commandArray);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
boolean compiling = true;
while (compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) {
}
}
if(exception!=null) {
exception.hideStackTrace();
throw exception;
}
if(result!=0)
return false;
} catch (Exception e) {
String msg = e.getMessage();
if ((msg != null) && (msg.indexOf("uisp: not found") != -1) && (msg.indexOf("avrdude: not found") != -1)) {
//System.err.println("uisp is missing");
//JOptionPane.showMessageDialog(editor.base,
// "Could not find the compiler.\n" +
// "uisp is missing from your PATH,\n" +
// "see readme.txt for help.",
// "Compiler error",
// JOptionPane.ERROR_MESSAGE);
return false;
} else {
e.printStackTrace();
result = -1;
}
}
//System.out.println("result2 is "+result);
// if the result isn't a known, expected value it means that something
// is fairly wrong, one possibility is that jikes has crashed.
//
if (exception != null) throw exception;
if ((result != 0) && (result != 1 )) {
exception = new RunnerException(SUPER_BADNESS);
//editor.error(exception);
//PdeBase.openURL(BUGS_URL);
//throw new PdeException(SUPER_BADNESS);
}
return (result == 0); // ? true : false;
}
boolean firstErrorFound;
boolean secondErrorFound;
// part of the PdeMessageConsumer interface
//
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 (!Preferences.getBoolean("upload.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))
s = "";
System.err.print(s);
// ignore cautions
if (s.indexOf("Error") != -1) {
//exception = new RunnerException(s+" Check the serial port selected or your Board is connected");
//System.out.println(s);
notFoundError = true;
return;
}
if(notFoundError) {
//System.out.println("throwing something");
exception = new RunnerException(I18n.format(_("the selected serial port {0} does not exist or your board is not connected"), s));
return;
}
if (s.indexOf("Device is not responding") != -1 ) {
exception = new RunnerException(_("Device is not responding, check the right serial port is selected or RESET the board right before exporting"));
return;
}
if (s.indexOf("Programmer is not responding") != -1 ||
s.indexOf("programmer is not responding") != -1 ||
s.indexOf("protocol error") != -1 ||
s.indexOf("avrdude: ser_open(): can't open device") != -1 ||
s.indexOf("avrdude: ser_drain(): read error") != -1 ||
s.indexOf("avrdude: ser_send(): write error") != -1 ||
s.indexOf("avrdude: error: buffered memory access not supported.") != -1) {
exception = new RunnerException(_("Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions."));
return;
}
if (s.indexOf("Expected signature") != -1) {
exception = new RunnerException(_("Wrong microcontroller found. Did you select the right board from the Tools > Board menu?"));
return;
}
}
}