2005-09-25 16:11:32 +02:00
|
|
|
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
/*
|
2007-06-22 00:24:57 +02:00
|
|
|
Uploader - abstract uploading baseclass (common to both uisp and avrdude)
|
2006-08-27 11:45:28 +02:00
|
|
|
Part of the Arduino project - http://www.arduino.cc/
|
2005-09-25 16:11:32 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2006-01-13 00:24:12 +01:00
|
|
|
$Id$
|
2005-09-25 16:11:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package processing.app;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2007-06-22 00:24:57 +02:00
|
|
|
public abstract class Uploader implements MessageConsumer {
|
2005-09-25 16:11:32 +02:00
|
|
|
static final String BUGS_URL =
|
|
|
|
"https://developer.berlios.de/bugs/?group_id=3590";
|
|
|
|
static final String SUPER_BADNESS =
|
|
|
|
"Compiler error, please submit this code to " + BUGS_URL;
|
|
|
|
|
|
|
|
RunnerException exception;
|
|
|
|
//PdePreferences preferences;
|
|
|
|
|
|
|
|
//Serial serialPort;
|
|
|
|
static InputStream serialInput;
|
|
|
|
static OutputStream serialOutput;
|
|
|
|
//int serial; // last byte of data received
|
|
|
|
|
2006-03-21 20:17:31 +01:00
|
|
|
public Uploader() {
|
2005-09-25 16:11:32 +02:00
|
|
|
}
|
|
|
|
|
2007-06-22 00:24:57 +02:00
|
|
|
public abstract boolean uploadUsingPreferences(String buildPath, String className)
|
|
|
|
throws RunnerException;
|
2006-03-21 20:46:13 +01:00
|
|
|
|
2007-10-07 20:39:14 +02:00
|
|
|
public abstract boolean burnBootloader(String programmer) throws RunnerException;
|
2006-03-21 20:17:31 +01:00
|
|
|
|
2007-10-06 22:26:45 +02:00
|
|
|
protected void flushSerialBuffer() throws RunnerException {
|
2007-06-22 00:24:57 +02:00
|
|
|
// Cleanup the serial buffer
|
2007-10-06 22:26:45 +02:00
|
|
|
try {
|
|
|
|
Serial serialPort = new Serial();
|
|
|
|
byte[] readBuffer;
|
|
|
|
while(serialPort.available() > 0) {
|
|
|
|
readBuffer = serialPort.readBytes();
|
|
|
|
try {
|
|
|
|
Thread.sleep(100);
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
serialPort.setDTR(false);
|
|
|
|
|
2007-06-22 00:24:57 +02:00
|
|
|
try {
|
|
|
|
Thread.sleep(100);
|
|
|
|
} catch (InterruptedException e) {}
|
2007-07-19 02:26:08 +02:00
|
|
|
|
2007-10-06 22:26:45 +02:00
|
|
|
serialPort.setDTR(true);
|
|
|
|
|
|
|
|
serialPort.dispose();
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new RunnerException(e.getMessage());
|
|
|
|
}
|
2006-03-21 20:17:31 +01:00
|
|
|
}
|
2005-09-25 16:11:32 +02:00
|
|
|
|
2007-06-22 00:24:57 +02:00
|
|
|
protected boolean executeUploadCommand(Collection commandDownloader)
|
|
|
|
throws RunnerException
|
|
|
|
{
|
2005-09-25 16:11:32 +02:00
|
|
|
firstErrorFound = false; // haven't found any errors yet
|
|
|
|
secondErrorFound = false;
|
|
|
|
notFoundError = false;
|
|
|
|
int result=0; // pre-initialized to quiet a bogus warning from jikes
|
2007-06-22 00:24:57 +02:00
|
|
|
|
|
|
|
String userdir = System.getProperty("user.dir") + File.separator;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
try {
|
2007-06-22 00:24:57 +02:00
|
|
|
String[] commandArray = new String[commandDownloader.size()];
|
|
|
|
commandDownloader.toArray(commandArray);
|
|
|
|
|
2006-08-27 11:45:28 +02:00
|
|
|
String avrBasePath;
|
|
|
|
if(Base.isMacOS()) {
|
2007-10-06 16:27:42 +02:00
|
|
|
avrBasePath = new String("hardware/tools/avr/bin/");
|
2006-08-27 11:45:28 +02:00
|
|
|
}
|
|
|
|
else if(Base.isLinux()) {
|
2007-10-22 19:47:05 +02:00
|
|
|
avrBasePath = new String("hardware/tools/");
|
2006-08-27 11:45:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2007-10-06 16:27:42 +02:00
|
|
|
avrBasePath = new String(userdir + "hardware/tools/avr/bin/");
|
2006-08-27 11:45:28 +02:00
|
|
|
}
|
2007-06-22 00:24:57 +02:00
|
|
|
|
|
|
|
commandArray[0] = avrBasePath + commandArray[0];
|
|
|
|
|
2006-08-27 11:45:28 +02:00
|
|
|
if (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);
|
2005-09-25 16:11:32 +02:00
|
|
|
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 = true;
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
if(result!=0)
|
|
|
|
return false;
|
|
|
|
} catch (Exception e) {
|
|
|
|
String msg = e.getMessage();
|
2007-10-07 00:12:43 +02:00
|
|
|
if ((msg != null) && (msg.indexOf("uisp: not found") != -1) && (msg.indexOf("avrdude: not found") != -1)) {
|
2005-09-25 16:11:32 +02:00
|
|
|
//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) {
|
|
|
|
//System.err.println("MSG: " + 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("the selected serial port "+s+" does not exist or your board is not connected");
|
|
|
|
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;
|
|
|
|
}
|
2007-10-07 00:12:43 +02:00
|
|
|
if (s.indexOf("Programmer is not responding") != -1 ||
|
|
|
|
s.indexOf("programmer is not responding") != -1 ||
|
|
|
|
s.indexOf("protocol error") != -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?");
|
2005-09-25 16:11:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|