forked from earlephilhower/arduino-esp8266littlefs-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP8266LittleFS.java
357 lines (313 loc) · 12.8 KB
/
ESP8266LittleFS.java
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Tool to put the contents of the sketch's "data" subfolder
into a LittleFS partition image and upload it to an ESP8266 MCU
Original copyright (c) 2015 Hristo Gochkov (ficeto at ficeto dot com)
Modified from SPIFFS to LittleFS by Earle F. Philhower, III
Modified for OTA password by Ladi Kehl
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
*/
package com.esp8266.mklittlefs;
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JOptionPane;
import processing.app.PreferencesData;
import processing.app.Editor;
import processing.app.Base;
import processing.app.BaseNoGui;
import processing.app.Platform;
import processing.app.Sketch;
import processing.app.tools.Tool;
import processing.app.helpers.ProcessUtils;
import processing.app.debug.TargetPlatform;
import org.apache.commons.codec.digest.DigestUtils;
import processing.app.helpers.FileUtils;
import cc.arduino.files.DeleteFilesOnShutdown;
/**
* Example Tools menu entry.
*/
public class ESP8266LittleFS implements Tool {
Editor editor;
public void init(Editor editor) {
this.editor = editor;
}
public String getMenuTitle() {
return "ESP8266 LittleFS Data Upload";
}
private int listenOnProcess(String[] arguments){
try {
final Process p = ProcessUtils.exec(arguments);
Thread thread = new Thread() {
public void run() {
try {
InputStreamReader reader = new InputStreamReader(p.getInputStream());
int c;
while ((c = reader.read()) != -1)
System.out.print((char) c);
reader.close();
reader = new InputStreamReader(p.getErrorStream());
while ((c = reader.read()) != -1)
System.err.print((char) c);
reader.close();
} catch (Exception e){}
}
};
thread.start();
int res = p.waitFor();
thread.join();
return res;
} catch (Exception e){
return -1;
}
}
private void sysExec(final String[] arguments){
Thread thread = new Thread() {
public void run() {
try {
if(listenOnProcess(arguments) != 0){
editor.statusError("LittleFS Upload failed!");
} else {
editor.statusNotice("LittleFS Image Uploaded");
}
} catch (Exception e){
editor.statusError("LittleFS Upload failed!");
}
}
};
thread.start();
}
private String getBuildFolderPath(Sketch s) {
// first of all try the getBuildPath() function introduced with IDE 1.6.12
// see commit arduino/Arduino#fd1541eb47d589f9b9ea7e558018a8cf49bb6d03
try {
String buildpath = s.getBuildPath().getAbsolutePath();
return buildpath;
}
catch (IOException er) {
editor.statusError(er);
}
catch (Exception er) {
try {
File buildFolder = FileUtils.createTempFolder("build", DigestUtils.md5Hex(s.getMainFilePath()) + ".tmp");
return buildFolder.getAbsolutePath();
}
catch (IOException e) {
editor.statusError(e);
}
catch (Exception e) {
// Arduino 1.6.5 doesn't have FileUtils.createTempFolder
// String buildPath = BaseNoGui.getBuildFolder().getAbsolutePath();
java.lang.reflect.Method method;
try {
method = BaseNoGui.class.getMethod("getBuildFolder");
File f = (File) method.invoke(null);
return f.getAbsolutePath();
} catch (SecurityException ex) {
editor.statusError(ex);
} catch (IllegalAccessException ex) {
editor.statusError(ex);
} catch (InvocationTargetException ex) {
editor.statusError(ex);
} catch (NoSuchMethodException ex) {
editor.statusError(ex);
}
}
}
return "";
}
private long getIntPref(String name){
String data = BaseNoGui.getBoardPreferences().get(name);
if(data == null || data.contentEquals("")) return 0;
if(data.startsWith("0x")) return Long.parseLong(data.substring(2), 16);
else return Integer.parseInt(data);
}
private void createAndUpload(){
if(!PreferencesData.get("target_platform").contentEquals("esp8266")){
System.err.println();
editor.statusError("LittleFS Not Supported on "+PreferencesData.get("target_platform"));
return;
}
if(!BaseNoGui.getBoardPreferences().containsKey("build.spiffs_start") || !BaseNoGui.getBoardPreferences().containsKey("build.spiffs_end")){
System.err.println();
editor.statusError("LittleFS Not Defined for "+BaseNoGui.getBoardPreferences().get("name"));
return;
}
long spiStart, spiEnd, spiPage, spiBlock;
try {
spiStart = getIntPref("build.spiffs_start");
spiEnd = getIntPref("build.spiffs_end");
spiPage = getIntPref("build.spiffs_pagesize");
if(spiPage == 0) spiPage = 256;
spiBlock = getIntPref("build.spiffs_blocksize");
if(spiBlock == 0) spiBlock = 4096;
} catch(Exception e){
editor.statusError(e);
return;
}
TargetPlatform platform = BaseNoGui.getTargetPlatform();
//Make sure mklittlefs binary exists
String mkspiffsCmd;
if(PreferencesData.get("runtime.os").contentEquals("windows"))
mkspiffsCmd = "mklittlefs.exe";
else
mkspiffsCmd = "mklittlefs";
File tool = new File(platform.getFolder() + "/tools", mkspiffsCmd);
if (!tool.exists() || !tool.isFile()) {
tool = new File(platform.getFolder() + "/tools/mklittlefs", mkspiffsCmd);
if (!tool.exists()) {
tool = new File(PreferencesData.get("runtime.tools.mklittlefs.path"), mkspiffsCmd);
if (!tool.exists()) {
System.err.println();
editor.statusError("LittleFS Error: mklittlefs not found!");
return;
}
}
}
Boolean isNetwork = false;
File espota = new File(platform.getFolder()+"/tools");
File esptool = new File(platform.getFolder()+"/tools");
String serialPort = PreferencesData.get("serial.port");
String pythonCmd = PreferencesData.get("runtime.os").contentEquals("windows") ? "python3.exe" : "python3";
String uploadCmd = "";
//make sure the serial port or IP is defined
if (serialPort == null || serialPort.isEmpty()) {
System.err.println();
editor.statusError("LittleFS Error: serial port not defined!");
return;
}
// Find upload.py, don't fail if not present for backwards compat
File uploadPyFile = new File(platform.getFolder()+"/tools", "upload.py");
if (uploadPyFile.exists() && uploadPyFile.isFile()) {
uploadCmd = uploadPyFile.getAbsolutePath();
}
// Find python.exe if present, don't fail if not found for backwards compat
String[] paths = { platform.getFolder()+"/tools", platform.getFolder()+"/tools/python3", PreferencesData.get("runtime.tools.python3.path") };
for (String s: paths) {
File toolPyFile = new File(s, pythonCmd);
if (toolPyFile.exists() && toolPyFile.isFile() && toolPyFile.canExecute()) {
pythonCmd = toolPyFile.getAbsolutePath();
break;
}
}
// pythonCmd now points to either an installed exe with full path or just plain "python3(.exe)"
//find espota if IP else find esptool
if(serialPort.split("\\.").length == 4){
isNetwork = true;
String espotaCmd = "espota.py";
espota = new File(platform.getFolder()+"/tools", espotaCmd);
if(!espota.exists() || !espota.isFile()){
System.err.println();
editor.statusError("LittleFS Error: espota not found!");
return;
}
} else {
String esptoolCmd = platform.getTool("esptool").get("cmd");
esptool = new File(platform.getFolder()+"/tools", esptoolCmd);
if(!esptool.exists() || !esptool.isFile()){
esptool = new File(platform.getFolder()+"/tools/esptool", esptoolCmd);
if(!esptool.exists()){
esptool = new File(PreferencesData.get("runtime.tools.esptool.path"), esptoolCmd);
if (!esptool.exists() && uploadCmd.isEmpty()) {
System.err.println();
editor.statusError("LittleFS Error: esptool not found!");
return;
}
}
}
}
//load a list of all files
int fileCount = 0;
File dataFolder = new File(editor.getSketch().getFolder(), "data");
if (!dataFolder.exists()) {
dataFolder.mkdirs();
}
if(dataFolder.exists() && dataFolder.isDirectory()){
File[] files = dataFolder.listFiles();
if(files.length > 0){
for(File file : files){
if((file.isDirectory() || file.isFile()) && !file.getName().startsWith(".")) fileCount++;
}
}
}
String dataPath = dataFolder.getAbsolutePath();
String toolPath = tool.getAbsolutePath();
String sketchName = editor.getSketch().getName();
String imagePath = getBuildFolderPath(editor.getSketch()) + "/" + sketchName + ".mklittlefs.bin";
String resetMethod = BaseNoGui.getBoardPreferences().get("upload.resetmethod");
String uploadSpeed = BaseNoGui.getBoardPreferences().get("upload.speed");
String uploadAddress = BaseNoGui.getBoardPreferences().get("build.spiffs_start");
Object[] options = { "Yes", "No" };
String title = "LittleFS Create";
String message = "No files have been found in your data folder!\nAre you sure you want to create an empty LittleFS image?";
if(fileCount == 0 && JOptionPane.showOptionDialog(editor, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]) != JOptionPane.YES_OPTION){
System.err.println();
editor.statusError("LittleFS Warning: mklittlefs canceled!");
return;
}
editor.statusNotice("LittleFS Creating Image...");
System.out.println("[LittleFS] data : "+dataPath);
System.out.println("[LittleFS] size : "+((spiEnd - spiStart)/1024));
System.out.println("[LittleFS] page : "+spiPage);
System.out.println("[LittleFS] block : "+spiBlock);
try {
if(listenOnProcess(new String[]{toolPath, "-c", dataPath, "-p", spiPage+"", "-b", spiBlock+"", "-s", (spiEnd - spiStart)+"", imagePath}) != 0) {
System.err.println();
editor.statusError("LittleFS Create Failed!");
return;
}
} catch (Exception e){
editor.statusError(e);
editor.statusError("LittleFS Create Failed!");
return;
}
editor.statusNotice("LittleFS Uploading Image...");
System.out.println("[LittleFS] upload : "+imagePath);
if(isNetwork){
System.out.println("[LittleFS] IP : "+serialPort);
System.out.println();
// ask for a password
String passCode = JOptionPane.showInputDialog(editor, "Please enter password:\n(leave empty if none required!)","ESP8266LittleFS OTA Update", JOptionPane.QUESTION_MESSAGE);
if(passCode != null) {
if(passCode.isEmpty()) {
sysExec(new String[]{pythonCmd, espota.getAbsolutePath(), "-i", serialPort, "-s", "-f", imagePath});
} else {
sysExec(new String[]{pythonCmd, espota.getAbsolutePath(), "-i", serialPort, "-s", "-f", imagePath, "-a", passCode});
}
}
} else {
System.out.println("[LittleFS] address : "+uploadAddress);
System.out.println("[LittleFS] reset : "+resetMethod);
System.out.println("[LittleFS] port : "+serialPort);
System.out.println("[LittleFS] speed : "+uploadSpeed);
if (!uploadCmd.isEmpty()) {
System.out.println("[LittleFS] python : "+pythonCmd);
System.out.println("[LittleFS] uploader : "+uploadCmd);
}
System.out.println();
if (!uploadCmd.isEmpty()) {
sysExec(new String[]{pythonCmd, uploadCmd, "--chip", "esp8266", "--port", serialPort, "--baud", uploadSpeed, "write_flash", uploadAddress, imagePath});
} else {
sysExec(new String[]{esptool.getAbsolutePath(), "-cd", resetMethod, "-cb", uploadSpeed, "-cp", serialPort, "-ca", uploadAddress, "-cf", imagePath});
}
}
}
public void run() {
createAndUpload();
}
}