Skip to content

Commit 329b3b6

Browse files
committed
Improve error reporting for OpenOCD
1 parent c2eb858 commit 329b3b6

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

src/org/deepjava/openOCDInterface/OpenOCD.java

+43-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.deepjava.openOCDInterface;
22

3+
import java.net.InetSocketAddress;
34
import java.net.Socket;
5+
import java.net.SocketAddress;
46
import java.util.ArrayList;
57
import java.util.Map;
68

@@ -52,28 +54,45 @@ public void openConnection() throws TargetConnectionException {
5254
if (dbg) StdStreams.vrb.println("[TARGET] no socket connection possible, start OpenOCD");
5355
String cmd = DeepPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.DEFAULT_OPENOCD_CMD);
5456
String opt = DeepPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.DEFAULT_OPENOCD_OPTIONS);
55-
if (dbg) StdStreams.vrb.println("cmd: " + cmd);
56-
if (dbg) StdStreams.vrb.println("options: " + opt);
57+
if (dbg) StdStreams.vrb.println("[TARGET] cmd: " + cmd);
58+
if (dbg) StdStreams.vrb.println("[TARGET] options: " + opt);
5759
try {
5860
if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) { // is windows system
5961
String path = cmd.substring(0, cmd.lastIndexOf("bin-x64"));
6062
File dir = new File(path);
61-
if (dbg) StdStreams.vrb.println("cmd /c start \"\" \"" + cmd + "\" " + opt);
63+
if (dbg) StdStreams.vrb.println("[TARGET] cmd /c start \"\" \"" + cmd + "\" " + opt);
6264
Runtime.getRuntime().exec("cmd /c start \"\" \"" + cmd + "\" " + opt, null, dir);
6365
} else if (System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0) { // is linux system
6466
String path = cmd.substring(0, cmd.lastIndexOf("openocd"));
6567
File dir = new File(path);
66-
if (dbg) StdStreams.vrb.println(cmd + opt);
68+
if (dbg) StdStreams.vrb.println("[TARGET] " + cmd + opt);
6769
Runtime.getRuntime().exec(cmd + opt, null, dir);
6870
}
69-
socket = new Socket(hostname, port);
71+
socket = new Socket();
72+
SocketAddress addr = new InetSocketAddress(hostname, port);
73+
socket.connect(addr, 10000);
7074
socket.setSoTimeout(1000);
7175
out = socket.getOutputStream();
7276
in = socket.getInputStream();
73-
if (dbg) StdStreams.vrb.println("[TARGET] started");
77+
if (dbg) StdStreams.vrb.println("[TARGET] connected");
78+
try {
79+
StringBuffer sb = new StringBuffer();
80+
out.write(("reset halt\r\n").getBytes()); // check if target present
81+
while (true) {
82+
int c = in.read();
83+
sb.append((char)c);
84+
if (sb.indexOf("not examined yet") >= 0) {
85+
if (dbg) StdStreams.vrb.println("[TARGET] Target not answering");
86+
throw new TargetConnectionException("Target not answering");
87+
}
88+
if (sb.indexOf("disabled") >= 0) return;
89+
}
90+
} catch (Exception e1) {
91+
throw new TargetConnectionException(e1.getMessage(), e1);
92+
}
7493
} catch (IOException e1) {
75-
if (dbg) StdStreams.vrb.println("[TARGET] Cannot start OpenOCD server");
76-
throw new TargetConnectionException(e1.getMessage(), e1);
94+
if (dbg) StdStreams.vrb.println("[TARGET] Cannot connect to OpenOCD server");
95+
throw new TargetConnectionException("Cannot connect to OpenOCD server", e1);
7796
}
7897
} catch (Exception e) {
7998
if (dbg) StdStreams.vrb.println("[TARGET] Connection failed on " + hostname);
@@ -97,7 +116,10 @@ public void setOptions(HString opts) {
97116
@Override
98117
public void closeConnection() {
99118
try {
100-
if (socket != null) socket.close();
119+
if (socket != null) {
120+
out.write(("shutdown\r\n").getBytes());
121+
socket.close();
122+
}
101123
} catch (IOException e) {
102124
// do nothing
103125
}
@@ -110,9 +132,17 @@ public boolean isConnected() {
110132
try {
111133
if (socket == null || socket.getInputStream() == null) return false;
112134
if (dbg) StdStreams.vrb.println("[TARGET] check returns " + (socket == null) + " " + socket.isConnected() + " " + socket.isClosed() + " " + socket.isOutputShutdown());
113-
int nof = socket.getInputStream().read();
114-
if (dbg) StdStreams.vrb.println("[TARGET] read returns " + nof);
115-
if ( nof == -1) {
135+
int ch = socket.getInputStream().read();
136+
if (dbg) StdStreams.vrb.println("[TARGET] read returns " + ch);
137+
if (dbg) StdStreams.vrb.println("[TARGET] available " + socket.getInputStream().available());
138+
if (socket.getInputStream().available() >= 100) {
139+
out.write(("shutdown\r\n").getBytes());
140+
if (dbg) StdStreams.vrb.println("[TARGET] shutdown server");
141+
long time = System.currentTimeMillis();
142+
while (System.currentTimeMillis() - time < 1000); // wait for shutdown
143+
return false;
144+
}
145+
if (ch == -1) {
116146
return false;
117147
}
118148
} catch (IOException e) {
@@ -126,7 +156,7 @@ public int getTargetState() throws TargetConnectionException {
126156
int timeout = 5; // in sleep cycles of 100ms
127157
try {
128158
in.skip(in.available());
129-
out.write(("wait_halt 10 \r\n").getBytes()); // try to read register to check if system is halted
159+
out.write(("wait_halt 10 \r\n").getBytes()); // check if system is halted
130160
int count = 0;
131161
while (true) {
132162
int n = in.available();

0 commit comments

Comments
 (0)