1
1
package org .deepjava .openOCDInterface ;
2
2
3
+ import java .net .InetSocketAddress ;
3
4
import java .net .Socket ;
5
+ import java .net .SocketAddress ;
4
6
import java .util .ArrayList ;
5
7
import java .util .Map ;
6
8
@@ -52,28 +54,45 @@ public void openConnection() throws TargetConnectionException {
52
54
if (dbg ) StdStreams .vrb .println ("[TARGET] no socket connection possible, start OpenOCD" );
53
55
String cmd = DeepPlugin .getDefault ().getPreferenceStore ().getString (PreferenceConstants .DEFAULT_OPENOCD_CMD );
54
56
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 );
57
59
try {
58
60
if (System .getProperty ("os.name" ).toLowerCase ().indexOf ("windows" ) >= 0 ) { // is windows system
59
61
String path = cmd .substring (0 , cmd .lastIndexOf ("bin-x64" ));
60
62
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 );
62
64
Runtime .getRuntime ().exec ("cmd /c start \" \" \" " + cmd + "\" " + opt , null , dir );
63
65
} else if (System .getProperty ("os.name" ).toLowerCase ().indexOf ("linux" ) >= 0 ) { // is linux system
64
66
String path = cmd .substring (0 , cmd .lastIndexOf ("openocd" ));
65
67
File dir = new File (path );
66
- if (dbg ) StdStreams .vrb .println (cmd + opt );
68
+ if (dbg ) StdStreams .vrb .println ("[TARGET] " + cmd + opt );
67
69
Runtime .getRuntime ().exec (cmd + opt , null , dir );
68
70
}
69
- socket = new Socket (hostname , port );
71
+ socket = new Socket ();
72
+ SocketAddress addr = new InetSocketAddress (hostname , port );
73
+ socket .connect (addr , 10000 );
70
74
socket .setSoTimeout (1000 );
71
75
out = socket .getOutputStream ();
72
76
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
+ }
74
93
} 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 );
77
96
}
78
97
} catch (Exception e ) {
79
98
if (dbg ) StdStreams .vrb .println ("[TARGET] Connection failed on " + hostname );
@@ -97,7 +116,10 @@ public void setOptions(HString opts) {
97
116
@ Override
98
117
public void closeConnection () {
99
118
try {
100
- if (socket != null ) socket .close ();
119
+ if (socket != null ) {
120
+ out .write (("shutdown\r \n " ).getBytes ());
121
+ socket .close ();
122
+ }
101
123
} catch (IOException e ) {
102
124
// do nothing
103
125
}
@@ -110,9 +132,17 @@ public boolean isConnected() {
110
132
try {
111
133
if (socket == null || socket .getInputStream () == null ) return false ;
112
134
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 ) {
116
146
return false ;
117
147
}
118
148
} catch (IOException e ) {
@@ -126,7 +156,7 @@ public int getTargetState() throws TargetConnectionException {
126
156
int timeout = 5 ; // in sleep cycles of 100ms
127
157
try {
128
158
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
130
160
int count = 0 ;
131
161
while (true ) {
132
162
int n = in .available ();
0 commit comments