|
| 1 | +/* |
| 2 | + GenericNetworkUploader - generic network uploader implementation |
| 3 | + makes possible to implement firmware updates over the air on any device |
| 4 | + Part of the Arduino project - http://www.arduino.cc/ |
| 5 | +
|
| 6 | + Copyright (c) 2004-05 |
| 7 | + Hernando Barragan |
| 8 | + Copyright (c) 2012 |
| 9 | + Cristian Maglie <[email protected]> |
| 10 | + Copyright (c) 2015 |
| 11 | + Hristo Gochkov <[email protected]> |
| 12 | +
|
| 13 | + This program is free software; you can redistribute it and/or modify |
| 14 | + it under the terms of the GNU General Public License as published by |
| 15 | + the Free Software Foundation; either version 2 of the License, or |
| 16 | + (at your option) any later version. |
| 17 | +
|
| 18 | + This program is distributed in the hope that it will be useful, |
| 19 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + GNU General Public License for more details. |
| 22 | +
|
| 23 | + You should have received a copy of the GNU General Public License |
| 24 | + along with this program; if not, write to the Free Software Foundation, |
| 25 | + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | +*/ |
| 27 | + |
| 28 | +package cc.arduino.packages.uploaders; |
| 29 | + |
| 30 | +import cc.arduino.packages.BoardPort; |
| 31 | +import cc.arduino.packages.Uploader; |
| 32 | +import processing.app.*; |
| 33 | +import processing.app.debug.RunnerException; |
| 34 | +import processing.app.debug.TargetPlatform; |
| 35 | +import processing.app.helpers.PreferencesMap; |
| 36 | +import processing.app.helpers.StringReplacer; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +import static processing.app.I18n.tr; |
| 42 | + |
| 43 | +public class GenericNetworkUploader extends Uploader { |
| 44 | + |
| 45 | + private final BoardPort port; |
| 46 | + |
| 47 | + public GenericNetworkUploader(BoardPort port) { |
| 48 | + this.port = port; |
| 49 | + } |
| 50 | + |
| 51 | + public boolean requiresAuthorization() { |
| 52 | + return this.port.getPrefs().get("auth_upload").contentEquals("yes"); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getAuthorizationKey() { |
| 57 | + return "runtime.pwd." + this.port.getAddress(); |
| 58 | + } |
| 59 | + |
| 60 | + public boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer, List<String> warningsAccumulator) throws Exception { |
| 61 | + TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform(); |
| 62 | + PreferencesMap prefs = PreferencesData.getMap(); |
| 63 | + PreferencesMap boardPreferences = BaseNoGui.getBoardPreferences(); |
| 64 | + if (boardPreferences != null) { |
| 65 | + prefs.putAll(boardPreferences); |
| 66 | + } |
| 67 | + String tool = prefs.getOrExcept("upload.tool"); |
| 68 | + if (tool.contains(":")) { |
| 69 | + String[] split = tool.split(":", 2); |
| 70 | + targetPlatform = BaseNoGui.getCurrentTargetPlatformFromPackage(split[0]); |
| 71 | + tool = split[1]; |
| 72 | + } |
| 73 | + prefs.putAll(targetPlatform.getTool(tool)); |
| 74 | + |
| 75 | + String password = ""; |
| 76 | + if(requiresAuthorization()){ |
| 77 | + password = prefs.getOrExcept(getAuthorizationKey()); |
| 78 | + } |
| 79 | + prefs.put("network.password", password); |
| 80 | + |
| 81 | + prefs.put("network.port", this.port.getPrefs().get("port")); |
| 82 | + |
| 83 | + prefs.put("build.path", buildPath); |
| 84 | + prefs.put("build.project_name", className); |
| 85 | + if (verbose) { |
| 86 | + prefs.put("upload.verbose", prefs.getOrExcept("upload.params.verbose")); |
| 87 | + } else { |
| 88 | + prefs.put("upload.verbose", prefs.getOrExcept("upload.params.quiet")); |
| 89 | + } |
| 90 | + |
| 91 | + boolean uploadResult; |
| 92 | + try { |
| 93 | + String pattern; |
| 94 | + //check if there is a separate pattern for network uploads |
| 95 | + pattern = prefs.get("upload.network_pattern"); |
| 96 | + if(pattern == null) |
| 97 | + pattern = prefs.getOrExcept("upload.pattern"); |
| 98 | + String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true); |
| 99 | + uploadResult = executeUploadCommand(cmd); |
| 100 | + } catch (RunnerException e) { |
| 101 | + throw e; |
| 102 | + } catch (Exception e) { |
| 103 | + throw new RunnerException(e); |
| 104 | + } |
| 105 | + return uploadResult; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean burnBootloader() throws RunnerException { |
| 110 | + throw new RunnerException("Burning bootloader is not supported via network!"); |
| 111 | + } |
| 112 | +} |
0 commit comments