forked from openlcb/OpenLCB_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoaderClient.java
More file actions
executable file
·374 lines (340 loc) · 12.9 KB
/
LoaderClient.java
File metadata and controls
executable file
·374 lines (340 loc) · 12.9 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package org.openlcb;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
import org.openlcb.implementations.DatagramService;
import org.openlcb.implementations.MemoryConfigurationService;
import org.openlcb.implementations.MemoryConfigurationService.McsWriteHandler;
import org.openlcb.implementations.MemoryConfigurationService.McsWriteStreamMemo;
import static org.openlcb.ProtocolIdentification.Protocol;
//
// LoaderClient.java
//
//
// Created by David on 2015-12-29.
//
//
public class LoaderClient extends MessageDecoder {
private static final Logger logger = Logger.getLogger(LoaderClient.class.getName());
private static final byte SRC_STREAM_ID = 4;
private static final int PIP_TIMEOUT_MSEC = 3000;
private static final int FREEZE_REBOOT_TIMEOUT_MSEC = 60000;
private static final int STREAM_INIT_TIMEOUT_MSEC = 10000;
private static final int STREAM_DATA_PROCEED_TIMEOUT_MSEC = 120000;
enum State { IDLE, ABORT, FREEZE, INITCOMPL, PIP, PIPREPLY, SETUPSTREAM, STREAM, STREAMDATA, DG, UNFREEEZE, SUCCESS, FAIL };
// These objects are injected in the constructor.
Connection connection;
MemoryConfigurationService mcs;
DatagramService dcs;
State state;
NodeID src;
NodeID dest;
int space;
long address;
byte[] content;
LoaderStatusReporter feedback;
private static final int ERR_CHECKSUM_FAILED = 0x2088;
private static final int ERR_FILE_CORRUPTED = 0x1089;
private static final int ERR_FILE_INAPPROPRIATE = 0x1088;
public static abstract class LoaderStatusReporter {
public abstract void onProgress(float percent);
public abstract void onDone(int errorCode, String errorString);
}
public LoaderClient( Connection _connection, MemoryConfigurationService _mcs, DatagramService _dcs ) {
connection = _connection;
dcs = _dcs;
mcs = _mcs;
if(timer == null) {
timer = new Timer("OpenLCB LoaderClient Timeout Timer");
}
}
/* Protocol:
---> memconfig Freeze (DG)
(<--- DG ok) -- node may reboot, and not be able to garantee this
<--- InitComplete
---> PIPRequest
<--- PIPReply
IF streams implemented then use one:
---> memconfig write stream request (DG)
<--- DG ok
<--- memconfig write stream reply (DG)
---> DG ok
---> StreamInitRequest
<--- StreamInitReply
---> StreamDataSend
...
---> StreamDataComplete
ELSE use datagrams:
---> DatagramMessage
<--- DatagramAcknowledged
...
---> [stop sending data when run out of buffer]
<--- stream data proceed
...
<--- DatagramAcknowledged
THEN:
---> UnFreeze
*/
public void doLoad(NodeID _src, NodeID _dest, int _space, long _address, byte[] _content, LoaderStatusReporter _feedback) {
src = _src;
dest = _dest;
space = _space;
address = _address;
content = _content;
state = State.IDLE;
feedback = _feedback;
sendFreeze(); // allow restarts
}
private void sendFreeze() {
state = State.FREEZE;
dcs.sendData(
new DatagramService.DatagramServiceTransmitMemo(dest, new int[]{0x20, 0xA1, space}) {
// Ignores both success and failure callback, because the state machine will
// proceed on the Node Init Complete message below.
@Override
public void handleSuccess(int flags) {
}
@Override
public void handleFailure(int errorCode) {
}
});
state = State.INITCOMPL;
startTimeout(FREEZE_REBOOT_TIMEOUT_MSEC);
}
private static Timer timer;
private TimerTask task = null;
private void startTimeout(int period_msec) {
task = new TimerTask(){
public void run(){
timerExpired();
}
};
timer.schedule(task, period_msec);
}
private void endTimeout() {
if (task != null) task.cancel();
else {
state = State.FAIL;
}
task = null;
}
private void timerExpired() {
failWith(1, "Timed out in state " + state.name());
}
/**
* Checks if a message is coming back from the target node to us.
* @param msg an addressed message
* @return true iff the addressed message is coming from the loader target and addressed to
* the loader client node.
*/
private boolean isReply(Message msg) {
if (!msg.getSourceNodeID().equals(dest)) {
return false;
}
if (msg instanceof AddressedMessage) {
if (!((AddressedMessage) msg).getDestNodeID().equals(src)) {
return false;
}
}
return true;
}
@Override
public void handleInitializationComplete(InitializationCompleteMessage msg, Connection sender){
//System.out.println("lhandleInitializationComplete state: "+state);
if (state == State.INITCOMPL && isReply(msg)) {
endTimeout();
state = State.PIP;
sendPipRequest();
}
if (state == State.SUCCESS && isReply(msg)) {
state = State.IDLE;
}
}
private void sendPipRequest() {
state = State.PIPREPLY;
Message msg = new ProtocolIdentificationRequestMessage(src, dest);
connection.put(msg, this);
startTimeout(PIP_TIMEOUT_MSEC);
}
@Override
public void handleProtocolIdentificationReply(ProtocolIdentificationReplyMessage msg, Connection sender){
if (state == State.PIPREPLY && isReply(msg)) {
endTimeout();
ProtocolIdentification pi = new ProtocolIdentification(msg.getSourceNodeID(), msg);
if(!pi.hasProtocol(Protocol.FirmwareUpgradeActive)) {
failWith(1, "Target not in Upgrade state.");
} else if(pi.hasProtocol(Protocol.Stream)) {
state = State.SETUPSTREAM;
setupStream();
} else if(pi.hasProtocol(Protocol.Datagram)) {
state = State.DG;
sendDGs();
} else {
failWith(1, "Target has no Streams nor Datagrams!");
}
}
}
private int bufferSize; // chunk size
private int nextIndex;
private int errorCounter;
private byte destStreamID;
// ============================= STREAMS ==============================================
private void setupStream() {
bufferSize = 16384;
state = State.STREAM;
mcs.request(new McsWriteStreamMemo(dest, space, address, SRC_STREAM_ID) {
@Override
public void handleSuccess() {
sendStream();
}
@Override
public void handleFailure(String where, int errorCode) {
String f = "Failed to setup stream at " + where + ": error 0x" + Integer
.toHexString(errorCode);
logger.warning(f);
failWith(errorCode, f);
}
});
}
private void sendStream() {
// System.out.println("lSend Stream ");
// @todo the destStreamID is probably bogus at this point. Check why it is needed here.
// flags are 0 here by default
StreamInitiateRequestMessage m = new StreamInitiateRequestMessage(src, dest, 0, bufferSize, SRC_STREAM_ID, destStreamID);
connection.put(m, this);
startTimeout(STREAM_INIT_TIMEOUT_MSEC);
}
@Override
public void handleStreamInitiateReply(StreamInitiateReplyMessage msg, Connection sender){
// System.out.println("handleStreamInitiateReply ");
// pick up buffer size to use
if(state==State.STREAM && isReply(msg) && SRC_STREAM_ID == msg.getSourceStreamID()) {
endTimeout();
this.bufferSize = msg.getBufferSize();
this.destStreamID = msg.getDestinationStreamID();
// init transfer
nextIndex = 0;
// send data
state=State.STREAMDATA;
sendStreamNext();
}
}
private void sendStreamNext() {
int size = Math.min(bufferSize, content.length-nextIndex);
int[] data = new int[size];
// copy the needed data
for (int i=0; i<size; i++) data[i] = content[nextIndex+i];
// System.out.println("\nsendStreamNext: "+data);
Message m = new StreamDataSendMessage(src, dest, destStreamID, data);
connection.put(m, this);
// are we done?
nextIndex = nextIndex+size;
feedback.onProgress(100.0F * (float)nextIndex / (float)content.length);
if (nextIndex < content.length) {
startTimeout(STREAM_DATA_PROCEED_TIMEOUT_MSEC);
return; // wait for Data Proceed message
}
// yes, say we're done
m = new StreamDataCompleteMessage(src, dest, SRC_STREAM_ID, destStreamID);
connection.put(m, this);
sendUnfreeze();
state = State.SUCCESS;
}
@Override
public void handleStreamDataProceed(StreamDataProceedMessage msg, Connection sender){
// System.out.println("handleStreamDataProceed");
if (state == State.STREAMDATA && isReply(msg)) {
endTimeout();
sendStreamNext();
}
}
// ============================= DATAGRAMS ==============================================
private void sendDGs() {
//System.out.println("\nlsendDGs: ");
nextIndex = 0;
bufferSize = 64;
errorCounter = 0;
sendDGNext();
}
private void sendDGNext() {
final int size = Math.min(bufferSize, content.length-nextIndex);
//System.out.println("lsendDGNext Enter: "+state);
//System.out.println("content.length: "+content.length);
//System.out.println("nextIndex: "+nextIndex);
//System.out.println("lbufferSize: "+bufferSize);
//System.out.println("lsize: "+size);
byte[] data = new byte[size];
// copy the needed data
System.arraycopy(content, nextIndex, data, 0, size);
mcs.requestWrite(dest, space, nextIndex, data, new McsWriteHandler() {
@Override
public void handleFailure(int errorCode) {
if (++errorCounter > 3) {
failWith(errorCode, "Repeated errors writing to firmware space.");
} else {
sendDGNext();
}
}
@Override
public void handleSuccess() {
nextIndex += size;
errorCounter = 0;
float p = 100.0F * nextIndex / content.length;
feedback.onProgress(p);
if(nextIndex<content.length) sendDGNext();
else {
state = State.SUCCESS;
sendUnfreeze();
}
}
});
}
private void sendUnfreeze() {
dcs.sendData(new DatagramService.DatagramServiceTransmitMemo(dest, new int[]{0x20, 0xA0, space}) {
@Override
public void handleSuccess(int flags) {
if (state == State.SUCCESS) {
feedback.onProgress((float) 100.0);
feedback.onDone(0, "");
}
}
@Override
public void handleFailure(int errorCode) {
if (errorCode == DatagramRejectedMessage.DATAGRAM_REJECTED_DST_REBOOT) {
// that's ok
handleSuccess(0);
} else if (state == State.SUCCESS){
failWith(errorCode, "Download Failed in UnFreeze");
} // else we already reported a failure
}
});
}
private void failWith(int errorCode, String errorString) {
boolean b = (state != State.FAIL && state != State.UNFREEEZE);
state = State.FAIL;
String tmpString = null;
if (errorCode == ERR_CHECKSUM_FAILED) {
tmpString = "Failed download checksum; try again";
} else if (errorCode == ERR_FILE_CORRUPTED) {
tmpString = "File corrupted";
} else if (errorCode == ERR_FILE_INAPPROPRIATE) {
tmpString = "The firmware data is incompatible with this hardware node";
}
if (tmpString != null) {
errorString = tmpString + " - " + errorString;
}
feedback.onDone(errorCode, errorString);
if (b) {
sendUnfreeze();
}
}
/*
* clean up local storage
*/
public void dispose(){
if(timer!=null) {
timer.cancel();
timer = null;
}
}
}