Skip to content

Commit cf101dc

Browse files
authored
Merge pull request #640 from Sloeber/fix_upload_problems
Fix upload problems for AUTH OAT
2 parents 9645d8b + 190f0df commit cf101dc

File tree

4 files changed

+178
-174
lines changed

4 files changed

+178
-174
lines changed

io.sloeber.core/config/pre_processing_platform_default.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ A.PACKAGES={eclipse_home}/arduinoPlugin/packages
1919

2020

2121
#for esp8266 network upload
22-
A.tools.esp8266OTA={A.tools.esptool.network_cmd}
23-
A.tools.esp8266OTA.upload.pattern={A.tools.esptool.upload.network_pattern}
22+
A.tools.esp8266OTA={A.TOOLS.ESPTOOL.NETWORK_CMD}
23+
A.tools.esp8266OTA.upload.pattern={A.TOOLS.ESPTOOL.UPLOAD.NETWORK_PATTERN}
2424
A.esp8266.network.upload.tool=esp8266OTA
2525
A.TOOLS.ESPTOOL.NETWORK.PASSWORD={A.NETWORK.AUTH}
2626

io.sloeber.core/src/io/sloeber/core/common/IndexHelper.java

Lines changed: 86 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -17,90 +17,103 @@
1717
import org.eclipse.core.runtime.NullProgressMonitor;
1818

1919
public class IndexHelper {
20-
/**
21-
* given a list of names from the indexer. Find a function call and return
22-
* the parameter I assume there is only one parameter and it is a string so
23-
* there are quotes
24-
*
25-
* @param names
26-
* the names provided by the indexer
27-
* @param function
28-
* the name of the function for which we are looking for a
29-
* parameter
30-
* @return the string or defaultValue if no string is found
31-
*/
32-
private static String findParameterInFunction(IIndexName[] names, String function, String defaultValue) {
33-
for (IIndexName name : names) {
34-
String SetupFileName = name.getFileLocation().getFileName();
35-
String SetupFileContent;
36-
try {
37-
SetupFileContent = FileUtils.readFileToString(new File(SetupFileName));
38-
} catch (IOException e) {
39-
return defaultValue;
40-
}
41-
SetupFileContent = SetupFileContent.replaceAll("//.*|/\\*((.|\\n)(?!=*/))+\\*/", ""); //$NON-NLS-1$ //$NON-NLS-2$
42-
int serialBeginStart = SetupFileContent.indexOf(function);
43-
if (serialBeginStart != -1) {
44-
int serialBeginStartbraket = SetupFileContent.indexOf("(", serialBeginStart); //$NON-NLS-1$
45-
if (serialBeginStartbraket != -1) {
46-
int serialBeginCloseBraket = SetupFileContent.indexOf(")", serialBeginStartbraket); //$NON-NLS-1$
47-
if (serialBeginCloseBraket != -1) {
48-
return SetupFileContent.substring(serialBeginStartbraket + 1, serialBeginCloseBraket).trim();
20+
/**
21+
* given a list of names from the indexer. Find a function call and return
22+
* the parameter I assume there is only one parameter The parameter is
23+
* considered everything between the ()
24+
*
25+
* @param names
26+
* the names provided by the indexer
27+
* @param function
28+
* the name of the function for which we are looking for a
29+
* parameter
30+
* @return the string or defaultValue if no string is found
31+
*/
32+
private static String findParameterInFunction(IIndexName[] names, String function, String defaultValue) {
33+
for (IIndexName name : names) {
34+
String codeFileName = name.getFileLocation().getFileName();
35+
String rawCodeFileContent;
36+
try {
37+
rawCodeFileContent = FileUtils.readFileToString(new File(codeFileName));
38+
} catch (IOException e) {
39+
return defaultValue;
40+
}
41+
String codeFileContent = rawCodeFileContent.replaceAll("//.*|/\\*((.|\\n)(?!=*/))+\\*/", ""); //$NON-NLS-1$ //$NON-NLS-2$
42+
int functionStart = codeFileContent.indexOf(function);
43+
if (functionStart != -1) {
44+
int parameterStartQuote = codeFileContent.indexOf("(", functionStart); //$NON-NLS-1$
45+
if (parameterStartQuote != -1) {
46+
char[] functionParams = codeFileContent.substring(parameterStartQuote, parameterStartQuote + 30)
47+
.toCharArray();
48+
int curbrackets = 1;
49+
int curchar = 1;
50+
while ((curbrackets != 0) && (curchar < functionParams.length)) {
51+
if (functionParams[curchar] == ')') {
52+
curbrackets--;
53+
} else {
54+
if (functionParams[curchar] == '(') {
55+
curbrackets++;
56+
}
57+
}
58+
curchar++;
59+
}
60+
if (curbrackets == 0) {
61+
return codeFileContent.substring(parameterStartQuote + 1, parameterStartQuote + curchar - 1);
62+
}
4963

50-
}
64+
}
65+
}
5166
}
52-
}
67+
return defaultValue;
68+
5369
}
54-
return defaultValue;
5570

56-
}
71+
/**
72+
* given a project look in the source code for the line of code that sets
73+
* the password;
74+
*
75+
*
76+
*
77+
* return the password string of no_pwd_found_in_code
78+
*
79+
* @param iProject
80+
* @return
81+
*/
82+
public static String findParameterInFunction(IProject project, String parentFunctionName, String childFunctionName,
83+
String defaultValue) {
5784

58-
/**
59-
* given a project look in the source code for the line of code that sets
60-
* the password;
61-
*
62-
*
63-
*
64-
* return the password string of no_pwd_found_in_code
65-
*
66-
* @param iProject
67-
* @return
68-
*/
69-
public static String findParameterInFunction(IProject project, String parentFunctionName, String childFunctionName,
70-
String defaultValue) {
85+
ICProject curProject = CoreModel.getDefault().getCModel().getCProject(project.getName());
7186

72-
ICProject curProject = CoreModel.getDefault().getCModel().getCProject(project.getName());
87+
IIndex index = null;
88+
try {
89+
index = CCorePlugin.getIndexManager().getIndex(curProject);
90+
index.acquireReadLock();
91+
IIndexBinding[] bindings = index.findBindings(parentFunctionName.toCharArray(), IndexFilter.ALL_DECLARED,
92+
new NullProgressMonitor());
93+
ICPPFunction parentFunction = null;
94+
for (IIndexBinding curbinding : bindings) {
95+
if (curbinding instanceof ICPPFunction) {
96+
parentFunction = (ICPPFunction) curbinding;
97+
}
98+
}
7399

74-
IIndex index = null;
75-
try {
76-
index = CCorePlugin.getIndexManager().getIndex(curProject);
77-
index.acquireReadLock();
78-
IIndexBinding[] bindings = index.findBindings(parentFunctionName.toCharArray(), IndexFilter.ALL_DECLARED,
79-
new NullProgressMonitor());
80-
ICPPFunction parentFunction = null;
81-
for (IIndexBinding curbinding : bindings) {
82-
if (curbinding instanceof ICPPFunction) {
83-
parentFunction = (ICPPFunction) curbinding;
84-
}
85-
}
100+
if (parentFunction == null) {
101+
return defaultValue;// that on found binding must be a function
102+
}
86103

87-
if (parentFunction == null) {
88-
return defaultValue;// that on found binding must be a function
89-
}
104+
IIndexName[] names = index.findNames(parentFunction, org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS);
90105

91-
IIndexName[] names = index.findNames(parentFunction, org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS);
106+
return findParameterInFunction(names, childFunctionName, defaultValue);
92107

93-
return findParameterInFunction(names, childFunctionName, defaultValue);
108+
} catch (CoreException | InterruptedException e) {
109+
e.printStackTrace();
110+
} finally {
111+
if (index != null) {
112+
index.releaseReadLock();
113+
}
114+
}
94115

95-
} catch (CoreException | InterruptedException e) {
96-
e.printStackTrace();
97-
} finally {
98-
if (index != null) {
99-
index.releaseReadLock();
100-
}
116+
return defaultValue;
101117
}
102118

103-
return defaultValue;
104-
}
105-
106119
}

io.sloeber.core/src/io/sloeber/core/tools/uploaders/arduinoUploader.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import org.eclipse.core.resources.IFile;
1414
import org.eclipse.core.resources.IProject;
1515
import org.eclipse.core.runtime.IProgressMonitor;
16+
import org.eclipse.core.runtime.IStatus;
17+
import org.eclipse.core.runtime.Status;
1618
import org.eclipse.ui.console.MessageConsole;
1719

18-
import io.sloeber.core.api.PasswordManager;
1920
import io.sloeber.core.common.Common;
2021
import io.sloeber.core.common.Const;
2122
import io.sloeber.core.common.IndexHelper;
2223
import io.sloeber.core.communication.ArduinoSerial;
23-
import io.sloeber.core.tools.Helpers;
2424

2525
public class arduinoUploader implements IRealUpload {
2626

@@ -77,24 +77,20 @@ public boolean uploadUsingPreferences(IFile hexFile, boolean usingProgrammer, IP
7777
command = envManager
7878
.getVariable(Common.get_Jantje_KEY_RECIPE(Const.ACTION_UPLOAD), configurationDescription, true)
7979
.getValue();
80-
} catch (Exception e) {// ignore all errors
80+
} catch (Exception e) {
81+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "Failed to get the Upload recipe ", e)); //$NON-NLS-1$
82+
return false;
8183
}
8284

8385
try {
8486
GenericLocalUploader.RunConsoledCommand(this.myConsole, command, monitor);
8587
} catch (IOException e1) {
86-
e1.printStackTrace();
87-
88+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "Failed to run the Upload recipe ", e1)); //$NON-NLS-1$
8889
return false;
8990
}
9091
if (boardName.startsWith("Arduino Due ")) { //$NON-NLS-1$
9192
ArduinoSerial.reset_Arduino_by_baud_rate(MComPort, 115200, 100);
9293
}
93-
// for web authorized upload
94-
if (needsPassword) {
95-
String passWord = getPasswordFromCode();
96-
PasswordManager.setPwd(MComPort, myLogin, passWord);
97-
}
9894

9995
return true;
10096
}
@@ -108,20 +104,16 @@ public boolean uploadUsingPreferences(IFile hexFile, boolean usingProgrammer, IP
108104
*/
109105
@SuppressWarnings("nls")
110106
private String getPasswordFromCode() {
111-
return IndexHelper.findParameterInFunction(this.myProject, "setup", "ArduinoOTA.setPassword",
107+
String parameter = IndexHelper.findParameterInFunction(this.myProject, "setup", "ArduinoOTA.setPassword",
112108
"no_pwd_found_in_code");
109+
return parameter.replaceAll("\\(.*\\)", "").trim();
113110

114111
}
115112

116113
private void setEnvironmentvarsForAutorizedUpload(IContributedEnvironment contribEnv,
117114
ICConfigurationDescription configurationDescription, String host) {
118-
String passWord;
119-
PasswordManager pwdManager = new PasswordManager();
120-
if (!pwdManager.setHost(Helpers.getHostFromComPort(host))) {
121-
passWord = getPasswordFromCode();
122-
} else {
123-
passWord = pwdManager.getPassword();
124-
}
115+
String passWord = null;
116+
passWord = getPasswordFromCode();
125117
IEnvironmentVariable var = new EnvironmentVariable(Const.ENV_KEY_NETWORK_AUTH, passWord);
126118
contribEnv.addVariable(var, configurationDescription);
127119

0 commit comments

Comments
 (0)