-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArmUsbDevice.java
267 lines (239 loc) · 9.94 KB
/
ArmUsbDevice.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
// ----------------------------------------------------------------------------
// The confidential and proprietary information contained in this file may
// only be used by a person authorized under and to the extent permitted
// by a subsisting licensing agreement from ARM Limited or its affiliates.
//
// (C)COPYRIGHT 2018 ARM Limited or its affiliates.
// ALL RIGHTS RESERVED
//
// This entire notice must be reproduced on all copies of this file
// and copies of this file may only be made by a person if such person is
// permitted to do so under the terms of a subsisting license agreement
// from ARM Limited or its affiliates.
// ----------------------------------------------------------------------------
package com.arm.armsda.serial;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.util.Log;
import com.arm.mbed.sda.proxysdk.ProxyException;
import com.arm.mbed.sda.proxysdk.devices.AbstractDevice;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class ArmUsbDevice extends AbstractDevice implements ISerialDataSink {
private static final int SHA256_LENGTH = 32;
private SerialHandler serialHandler;
private File logFile;
private FileOutputStream outputStream;
private Context ctx;
private StringBuffer logStringBuffer = new StringBuffer("");
private final static String LINE_SEPARATOR = System.getProperty("line.separator");
private byte[] workBuffer = new byte[SerialHandler.INPUT_BUFFER_LENGTH * 2];
private State state;
private byte[] msgBuffer;
private int mark;
private int expectedMsgLength;
private byte[] response;
private enum State {
BomSearch,
MsgLenSearch,
InMessage,
DigestSearch;
};
public ArmUsbDevice(UsbSerialPort serialPort, Context ctx) {
this.serialHandler = new SerialHandler(serialPort);
logStringBuffer.setLength(0);
this.ctx = ctx;
logFile = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"Mbed_log.txt");
logFile.delete();
}
@Override // from AbstractDevice
public byte[] sendMessage(byte[] operationMessage) {
// Write message to usb
byte[] serialProtocolMessage = SerialMessage.formatSerialProtocolMessage(operationMessage);
serialHandler.write(serialProtocolMessage);
// Read response (Read message from usb)
state = State.BomSearch;
msgBuffer = null;
workBuffer = new byte[SerialHandler.INPUT_BUFFER_LENGTH * 2];
mark = 0;
expectedMsgLength = 0;
try {
Log.d("M_FILE", "Opening log file");
//Opening the log file (append mode), in case that the open file location/path is
//read-only FileNotFoundException will be thrown
outputStream = new FileOutputStream(logFile, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
serialHandler.startRead(this);
response = null;
//for (int i = 0; i < 300; i ++) { // 300 * 100 is 30 secs
while (true) { // 300 * 100 is 30 secs
try {
Thread.sleep(200);
if (response != null) {
break;
}
} catch (InterruptedException ex) {
throw new ProxyException("Failed to get response from device");
}
}
// initiate media scan and put the new things into the path array to
// make the scanner aware of the location and the files you want to see
MediaScannerConnection.scanFile(
ctx,
new String[] {logFile.toString()},
null,
null);
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override // from ISerialDataSink
public void onNewData(byte[] data) {
byte[] tmpBuffer = new byte[mark + data.length];
System.arraycopy(workBuffer, 0, tmpBuffer, 0, mark);
System.arraycopy(data, 0, tmpBuffer, mark, data.length);
if (state == State.BomSearch) {
if (!findBom(tmpBuffer)) {
return;
} else {
tmpBuffer = new byte[mark];
System.arraycopy(workBuffer, 0, tmpBuffer, 0, mark);
state = State.MsgLenSearch;
}
}
if (state == State.MsgLenSearch) {
System.out.println("ArmUsbDevice: MsgLenSearch");
if (tmpBuffer.length < Integer.BYTES) {
System.arraycopy(tmpBuffer, 0, workBuffer, 0, tmpBuffer.length);
mark = tmpBuffer.length;
sleeping();
return;
}
ByteBuffer bb = ByteBuffer.wrap(tmpBuffer).order(ByteOrder.BIG_ENDIAN);
expectedMsgLength = bb.getInt();
tmpBuffer = Arrays.copyOfRange(tmpBuffer, Integer.BYTES, tmpBuffer.length);
state = State.InMessage;
}
if (state == State.InMessage) {
System.out.println("ArmUsbDevice: InMessage");
if (tmpBuffer.length < expectedMsgLength) {
System.arraycopy(tmpBuffer, 0, workBuffer, 0, tmpBuffer.length);
mark = tmpBuffer.length;
sleeping();
return;
}
msgBuffer = Arrays.copyOfRange(tmpBuffer, 0, expectedMsgLength);
tmpBuffer = Arrays.copyOfRange(tmpBuffer, expectedMsgLength, tmpBuffer.length);
state = State.DigestSearch;
}
if (state == State.DigestSearch) {
System.out.println("ArmUsbDevice: DigestSearch");
if (tmpBuffer.length < SHA256_LENGTH) {
System.arraycopy(tmpBuffer, 0, workBuffer, 0, tmpBuffer.length);
mark = tmpBuffer.length;
sleeping();
return;
}
byte[] msgDigest = Arrays.copyOfRange(tmpBuffer, 0, SHA256_LENGTH);
if (null == msgBuffer) {
System.out.println("ArmUsbDevice: msgBuffer is null?");
}
byte[] expectedDigest = SerialMessage.getDigest(msgBuffer);
if (!Arrays.equals(msgDigest, expectedDigest)) {
System.out.println("msgDigest:" + Arrays.toString(msgDigest) + " Size: " + msgDigest.length);
System.out.println("expectedDigest:" + Arrays.toString(expectedDigest) + " Size: " + expectedDigest.length);
System.out.println("tmpBuffer:" + Arrays.toString(tmpBuffer) + " Size: " + tmpBuffer.length);
throw new ProxyException("Device message has invalid digest");
}
printToLogAndStdout(tmpBuffer, true);
serialHandler.stopRead();
state = State.BomSearch;
System.out.println("ArmUsbDevice: Returning Message!");
Log.i("", "ArmUsbDevice: Returning Message!");
response = msgBuffer;
}
}
private boolean findBom(byte[] tmpBuffer) {
boolean found = false;
int i = 0;
for (; i <= tmpBuffer.length - SerialMessage.BOM.length; i ++) {
found = true;
for (int j = 0; j < SerialMessage.BOM.length; j ++) {
if (tmpBuffer[i + j] != SerialMessage.BOM[j]) {
found = false;
break;
}
}
if (found) {
System.out.println("### Bom has been found!!");
break;
}
}
byte[] logBuffer = new byte[i];
System.arraycopy(tmpBuffer, 0, logBuffer, 0, i);
printToLogAndStdout(logBuffer, false);
if (!found) {
mark = tmpBuffer.length - i;
System.arraycopy(tmpBuffer, i, workBuffer, 0, mark);
} else {
mark = tmpBuffer.length - (i + SerialMessage.BOM.length);
System.arraycopy(tmpBuffer, i + SerialMessage.BOM.length, workBuffer, 0, mark);
}
return found;
}
private void sleeping() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void printToLogAndStdout(byte[] logBuffer, boolean lastCall) {
if (lastCall) {
Log.i("MBED_DEBUG", logStringBuffer.toString());
try {
outputStream.write(logStringBuffer.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return;
}
String logStr = null;
try {
logStr = new String(logBuffer, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
logStringBuffer.append(logStr);
String[] lines = logStringBuffer.toString().split(LINE_SEPARATOR);
if (lines.length > 1) {
for (int index = 0; index < lines.length -1; index++) {
Log.i("MBED_DEBUG", lines[index]);
try {
outputStream.write(lines[index].getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
logStringBuffer.setLength(0);
logStringBuffer.append(lines[lines.length-1]);
}
}
}