Subversion Repositories HomeAutomation

Rev

Rev 770 | Rev 877 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
453 arune 1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Threading;
5
 
6
//this class should probably be integrated into canNMT instead
7
 
8
public class Downloader {
9
    private HexFile hf;
10
    private DaemonConnection dc;
11
    private bool isBiosUpdate;
12
    private ulong dlOffset = 0;
13
    //private Thread down;
14
    private byte receiveID;
770 arune 15
    private uint hwid;
16
    private bool HWID_INUSE;
453 arune 17
 
586 arune 18
    private enum dState { SEND_START, WAIT_ACK_PRG, SEND_PGM_DATA, WAIT_ACK_DATA, RESEND_ADDR, WAIT_DONE, SEND_DONE, SEND_BIOS_UPDATE, SEND_RESET, DONE, DEBUG_STATE2, WAIT_BOOT, SEND_EMPTY_START, WAIT_ACK_PRG_EMPTY, SEND_EMPTY_DATA, WAIT_ACK_EMPTY_DATA, SEND_DONE_EMPTY, WAIT_DONE_EMPTY};
453 arune 19
 
20
    private const byte CAN_NMT              = 0x00;
21
 
22
    private const byte CAN_NMT_RESET        = 0x04;
23
    private const byte CAN_NMT_BIOS_START   = 0x08;
24
    private const byte CAN_NMT_PGM_START    = 0x0C;
25
    private const byte CAN_NMT_PGM_DATA     = 0x10;
26
    private const byte CAN_NMT_PGM_END      = 0x14;
27
    private const byte CAN_NMT_PGM_COPY     = 0x18;
28
    private const byte CAN_NMT_PGM_ACK      = 0x1C;
29
    private const byte CAN_NMT_PGM_NACK     = 0x20;
30
    private const byte CAN_NMT_START_APP    = 0x24;
31
    private const byte CAN_NMT_APP_START    = 0x28;
32
    private const byte CAN_NMT_HEARTBEAT    = 0x2C;
33
 
34
    private const byte MY_ID = 0x00;
35
    //private const byte TARGET_ID = 0x78;
36
    private const byte MY_NID = 0x0;
37
    private const byte ADDR0_INDEX = 0;
38
    private const byte ADDR1_INDEX = 1;
39
    private const byte ADDR2_INDEX = 2;
40
    private const byte ADDR3_INDEX = 3;
770 arune 41
 
42
    private const byte HWID0_INDEX = 4;
43
    private const byte HWID1_INDEX = 5;
44
    private const byte HWID2_INDEX = 6;
45
    private const byte HWID3_INDEX = 7;
453 arune 46
 
632 arune 47
    private const int TIMEOUT_MS = 1000;
48
    private const int TIMEOUT_SHORT_MS = 200;
453 arune 49
 
50
    private long timeStart = 0;
51
 
770 arune 52
    public Downloader(HexFile hf, DaemonConnection dc, byte receiveID, bool isBiosUpdate, bool HWID_INUSE, uint hwid) {
453 arune 53
        this.hf = hf;
54
        this.dc = dc;
55
        this.receiveID = receiveID;
56
        this.isBiosUpdate = isBiosUpdate;
770 arune 57
        this.hwid = hwid;
58
        this.HWID_INUSE = HWID_INUSE;
453 arune 59
    }
60
 
61
    public int calcCRC(HexFile hf) {
62
        int crc=0;
63
        for (ulong i=hf.getAddrLower(); i <= hf.getAddrUpper(); i++) {
64
            crc = crc16_update(crc, hf.getByte(i));
65
        }
66
        return crc;
67
    }
68
 
69
    public int crc16_update(int crc, byte a) {
70
        int i;
71
        crc ^= a;
72
        for (i = 0; i < 8; ++i) {
73
            if ((crc & 1) != 0)
74
                crc = (crc >> 1) ^ 0xA001;
75
            else
76
                crc = (crc >> 1);
77
        }
78
        return crc;
79
    }
80
 
81
    public bool go() {
82
        if (dc==null || hf==null || hf.getLength()==0) return false;
83
        return downloader();
84
    }
85
 
86
    public void abort() {
87
        //if (down != null && down.IsAlive) down.Abort();
88
    }
89
 
90
    public bool downloader() {
91
        dState pgs = dState.SEND_START;
92
        int t = 0;
632 arune 93
        int t2 = 0;
453 arune 94
        CanPacket outCm = null;
95
        CanPacket cm = null;
96
        byte[] data = new byte[8];
97
        uint byteSent = 0;
98
        ulong currentAddress = 0;
99
        bool done = false;
100
        bool errorOccured = false;
101
 
876 arune 102
        //int rotstate=0;
103
        int dldisplaystate=0;
104
 
453 arune 105
        try {
106
            if (isBiosUpdate) {
107
                dlOffset = hf.getAddrLower();
108
                //Console.WriteLine(dlOffset);
109
                //dlOffset = 200;
110
            }
111
 
770 arune 112
            CanNMT cpn = new CanNMT(HWID_INUSE);
453 arune 113
            cpn.setReceiver(receiveID);
114
            bool hasMessage = false;
115
 
116
            while (!done) {
117
                Thread.Sleep(1);
118
 
119
                switch (pgs) {
120
                    case dState.SEND_START:
121
                        // Send boot start packet to target.
122
                        // and wait for ack.
123
                        currentAddress = hf.getAddrLower();
124
                        //data[RID_INDEX] = TARGET_ID;
125
                        data[ADDR0_INDEX] = (byte)((currentAddress-dlOffset) & 0x0000FF);
126
                        data[ADDR1_INDEX] = (byte)(((currentAddress-dlOffset) & 0x00FF00) >> 8);
127
                        data[ADDR2_INDEX] = (byte)(((currentAddress-dlOffset) & 0xFF0000) >> 16);
128
                        data[ADDR3_INDEX] = 0;
770 arune 129
                        if (HWID_INUSE) {
130
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
131
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
132
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
133
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
134
                        }
453 arune 135
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
136
                        outCm = cpn.getPgmStartPacket(data);
137
                        dc.sendCanPacket(outCm);
138
                        t = Environment.TickCount;
632 arune 139
                        t2 = Environment.TickCount;
453 arune 140
                        pgs = dState.WAIT_ACK_PRG;
141
                        timeStart = Environment.TickCount;
876 arune 142
                        Console.WriteLine("Downloading...");
453 arune 143
                        break;
144
 
145
 
146
                    case dState.WAIT_ACK_PRG:
147
                        // Check for timeout, resend start packet in that case..
148
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
149
                            Console.WriteLine("Timeout while waiting for start prg ack.");
150
                            errorOccured = true;
151
                            pgs = dState.SEND_RESET;
152
                        }
467 arune 153
                        //Thread.Sleep(1);
453 arune 154
                        // If received ack.
155
                        hasMessage = dc.getData(out cm);
156
                        if (hasMessage) {
770 arune 157
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
158
                            if (cpnin.isNMTPacket()) {
159
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
453 arune 160
                                // If no error
161
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
162
                                    // Start sending program data..
163
                                    byteSent = 0;
164
                                    pgs = dState.SEND_PGM_DATA;
165
                                }
166
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
167
                                    // else ..
168
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
169
                                    errorOccured = true;
170
                                    pgs = dState.SEND_RESET;
171
                                }
172
                            }
173
                        }
174
                        break;
175
 
176
                    case dState.SEND_PGM_DATA:
177
                        //Console.WriteLine("byteSent = "+byteSent);
876 arune 178
//disable                       Console.Write(".");
179
//Console.Write("\b");
180
//int curleft = Console.CursorLeft;
181
//int curtop = Console.CursorTop;
182
//Console.SetCursorPosition(curleft, curtop-1);
183
 
184
//if (rotstate==0) {
185
//   Console.Write("-");
186
//   rotstate=1;
187
//} else if (rotstate==1) {
188
//   Console.Write("\\");
189
//   rotstate=2;
190
//} else if (rotstate==2) {
191
//   Console.Write("|");
192
//   rotstate=3;
193
//} else if (rotstate==3) {
194
//   Console.Write("/");
195
//   rotstate=0;
196
//}
197
//Console.Write("\r");
198
//dldisplaystate
199
 
200
//FULHACK deulx
201
                        ulong percent = byteSent*100/(hf.getAddrUpper()-currentAddress);
202
 
203
                        if (percent>9 && dldisplaystate==0) {
204
                           Console.Write("\r");
205
                           Console.Write(percent+"%");
206
                           dldisplaystate=1;
207
                        } else if (percent>19 && dldisplaystate==1) {
208
                           Console.Write("\r");
209
                           Console.Write(percent+"%");
210
                           dldisplaystate=2;
211
                        } else if (percent>29 && dldisplaystate==2) {
212
                           Console.Write("\r");
213
                           Console.Write(percent+"%");
214
                           dldisplaystate=3;
215
                        } else if (percent>39 && dldisplaystate==3) {
216
                           Console.Write("\r");
217
                           Console.Write(percent+"%");
218
                           dldisplaystate=4;
219
                        } else if (percent>49 && dldisplaystate==4) {
220
                           Console.Write("\r");
221
                           Console.Write(percent+"%");
222
                           dldisplaystate=5;
223
                        } else if (percent>59 && dldisplaystate==5) {
224
                           Console.Write("\r");
225
                           Console.Write(percent+"%");
226
                           dldisplaystate=6;
227
                        } else if (percent>69 && dldisplaystate==6) {
228
                           Console.Write("\r");
229
                           Console.Write(percent+"%");
230
                           dldisplaystate=7;
231
                        } else if (percent>79 && dldisplaystate==7) {
232
                           Console.Write("\r");
233
                           Console.Write(percent+"%");
234
                           dldisplaystate=8;
235
                        } else if (percent>89 && dldisplaystate==8) {
236
                           Console.Write("\r");
237
                           Console.Write(percent+"%");
238
                           dldisplaystate=9;
239
                        } else if (percent>99 && dldisplaystate==9) {
240
                           Console.Write("\r");
241
                           Console.Write(percent+"%");
242
                           dldisplaystate=10;
243
                        }
244
 
453 arune 245
                        // Send program data.
246
                        byte datalength = 2;
247
                        for (ulong i = 0; i < 6; i++) {
248
                            // Populate data.
249
                            data[i+2] = hf.getByte(currentAddress + i + byteSent);
250
                            if (currentAddress + i + byteSent > hf.getAddrUpper()) {
251
                                break;  //then done
252
                            }
253
                            datalength++;
254
                        }
255
                        data[0] = (byte)(byteSent & 0x00FF);
256
                        data[1] = (byte)((byteSent & 0xFF00) >> 8);
257
 
258
                        outCm = cpn.getPgmDataPacket(data, datalength);
259
                        dc.sendCanPacket(outCm);
260
 
261
                        t = Environment.TickCount;
632 arune 262
                        t2 = Environment.TickCount;
453 arune 263
                        byteSent += 6;
264
                        pgs = dState.WAIT_ACK_DATA;
265
                        break;
266
 
267
 
268
                    case dState.WAIT_ACK_DATA:
269
                        // Wait for pgm ack.
632 arune 270
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
453 arune 271
                            // Woops, error. 
272
                            Console.WriteLine("Timeout while waiting for data ack.");
273
                            errorOccured = true;
274
                            pgs = dState.SEND_RESET;
275
                        }
634 arune 276
                        else if ((Environment.TickCount - t2) > TIMEOUT_SHORT_MS) {
632 arune 277
                            // Woops, error. 
876 arune 278
//disable                           Console.Write(":");
632 arune 279
                            byteSent -= 6;
280
                            pgs = dState.SEND_PGM_DATA;
281
                            break;
282
                        }
467 arune 283
                        //Thread.Sleep(1);
453 arune 284
                        // If received ack.
285
                        hasMessage = dc.getData(out cm);
286
                        if (hasMessage) {
770 arune 287
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
288
                            if (cpnin.isNMTPacket()) {
289
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
453 arune 290
                                // If no error
291
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
292
                                    //currentAddress += 2;
293
                                    // Check if end.
294
                                    if (currentAddress + byteSent > hf.getAddrUpper()) {
295
                                        // Yes, we are done, send done.
296
                                        pgs = dState.SEND_DONE;
297
                                    }
298
                                    else {
299
                                        // More to write.
300
                                        //byteSent = 0;
301
                                        pgs = dState.SEND_PGM_DATA;
302
                                    }
303
                                }
304
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
305
                                    // else ..
306
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
307
                                    errorOccured = true;
308
                                    pgs = dState.SEND_RESET;
309
                                }
310
                            }
311
                        }
312
                        break;
313
 
314
                    case dState.SEND_DONE:
876 arune 315
                       Console.Write("\r");
316
                       Console.Write("100%");
453 arune 317
                        // Send done
318
                        outCm = cpn.getPgmEndPacket();
319
                        dc.sendCanPacket(outCm);
320
 
321
                        t = Environment.TickCount;
632 arune 322
                        t2 = Environment.TickCount;
453 arune 323
                        pgs = dState.WAIT_DONE;
324
                        break;
325
 
326
 
327
                    case dState.WAIT_DONE:
328
                        // Check for timeout, resend done packet in that case..
329
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
330
                            Console.WriteLine("Timeout while waiting for end prg ack.");
331
                            errorOccured = true;
332
                            pgs = dState.SEND_RESET;
333
                        }
467 arune 334
                        //Thread.Sleep(1);
453 arune 335
 
336
                        hasMessage = dc.getData(out cm);
337
                        if (hasMessage) {
770 arune 338
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
339
                            if (cpnin.isNMTPacket()) {
340
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
453 arune 341
                                // If no error
342
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
343
                                    Console.WriteLine("");
344
                                    //
345
                                    int crc = calcCRC(hf);
346
 
347
                                    if (cm.getDataLength() == 2) {
348
                                        byte[] cmdata = cm.getData();
349
                                        if ((cmdata[0] == (crc & 0xFF)) && (cmdata[1] == (crc>>8))) {
350
                                            if (!isBiosUpdate) {
351
                                                Console.WriteLine("Done, successfully programmed application.");
352
                                                pgs = dState.SEND_RESET;
353
                                            } else {
354
                                                Console.WriteLine("Done, successfully programmed bios to application space.");
355
                                                pgs = dState.SEND_BIOS_UPDATE;
356
                                            }
357
                                        } else {
358
                                            string nodecrc = String.Format("{0:x4}", (int)cmdata[0] + (int)(cmdata[1]<<8));
359
                                            nodecrc = "0x"+nodecrc;
360
                                            string realcrc = String.Format("{0:x4}", (int)(crc & 0xFF) + (int)(crc & 0xFF00));
361
                                            realcrc = "0x"+realcrc;
362
                                            Console.WriteLine("CRC failed. Node sent CRC "+nodecrc+" but should be "+realcrc+".");
363
                                            errorOccured = true;
364
                                            pgs = dState.SEND_RESET;
365
                                        }
366
 
367
                                        byteSent = 0;
368
                                    }
369
                                    //Console.WriteLine("crc: " + crc + " " + (crc>>8) + " " + (crc & 0xFF));
370
                                    //Console.WriteLine("ACKpkg with CRC: " + cm.ToString());
371
                                    // 
372
                                }
373
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
374
                                    // else resend addr..
375
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
376
                                    errorOccured = true;
377
                                    pgs = dState.SEND_RESET;
378
                                }
379
                            }
380
                        }
381
                        break;
382
 
383
                    case dState.SEND_BIOS_UPDATE:
384
                        data[0] = (byte)(0 & 0x00FF);
385
                        data[1] = (byte)((0 & 0xFF00) >> 8);
386
                        data[2] = (byte)(dlOffset & 0x00FF);
387
                        data[3] = (byte)((dlOffset & 0xFF00) >> 8);
388
                        data[4] = (byte)((hf.getAddrUpper()-hf.getAddrLower()+1) & 0x00FF);
389
                        data[5] = (byte)(((hf.getAddrUpper()-hf.getAddrLower()+1) & 0xFF00) >> 8);
390
 
391
                        outCm = cpn.getPgmCopyPacket(data);
392
                        Console.WriteLine("Now say a prayer, moving bios");
393
                        dc.sendCanPacket(outCm);
394
                        t = Environment.TickCount;
632 arune 395
                        t2 = Environment.TickCount;
586 arune 396
                        pgs = dState.WAIT_BOOT;
397
                        //pgs = dState.DONE;
453 arune 398
                        break;
586 arune 399
 
400
                    case dState.WAIT_BOOT:
401
                        // Wait for node reset answer (boot msg).
637 arune 402
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
586 arune 403
                            // Woops, error. 
404
                            Console.WriteLine("Timeout while waiting for node to boot.");
405
                            errorOccured = true;
406
                            pgs = dState.SEND_DONE;
407
                        }
408
                        //Thread.Sleep(1);
409
                        // If received boot msg.
410
                        hasMessage = dc.getData(out cm);
411
                        if (hasMessage) {
770 arune 412
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
413
                            if (cpnin.isNMTPacket()) {
414
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 415
                                // If no error
416
                                if (cpnin.getType() == CAN_NMT_BIOS_START) {
417
                                    pgs = dState.SEND_EMPTY_START;
418
                                }
419
                                else {
420
                                    // else ..
421
                                }
422
                            }
423
                        }
424
                        break;
453 arune 425
 
586 arune 426
                    case dState.SEND_EMPTY_START:
427
                        // Send boot start packet to target.
428
                        // and wait for ack.
429
 
430
                        //data[RID_INDEX] = TARGET_ID;
431
                        data[ADDR0_INDEX] = (byte)((0) & 0x0000FF);
432
                        data[ADDR1_INDEX] = (byte)(((0) & 0x00FF00) >> 8);
433
                        data[ADDR2_INDEX] = (byte)(((0) & 0xFF0000) >> 16);
434
                        data[ADDR3_INDEX] = 0;
770 arune 435
                        if (HWID_INUSE) {
436
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
437
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
438
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
439
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
440
                        }
586 arune 441
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
442
                        outCm = cpn.getPgmStartPacket(data);
443
                        dc.sendCanPacket(outCm);
444
                        t = Environment.TickCount;
632 arune 445
                        t2 = Environment.TickCount;
586 arune 446
                        pgs = dState.WAIT_ACK_PRG_EMPTY;
447
                        break;
448
 
449
 
450
                    case dState.WAIT_ACK_PRG_EMPTY:
451
                        // Check for timeout, resend start packet in that case..
637 arune 452
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
770 arune 453
                            Console.WriteLine("Timeout while waiting for start prg ack during empty-first-block-cycle.");
586 arune 454
                            errorOccured = true;
455
                            pgs = dState.SEND_RESET;
456
                        }
457
                        //Thread.Sleep(1);
458
                        // If received ack.
459
                        hasMessage = dc.getData(out cm);
460
                        if (hasMessage) {
770 arune 461
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
462
                            if (cpnin.isNMTPacket()) {
463
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 464
                                // If no error
465
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
466
                                    // Start sending program data..
467
                                    byteSent = 0;
468
                                    pgs = dState.SEND_EMPTY_DATA;
469
                                }
470
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
471
                                    // else ..
472
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
473
                                    errorOccured = true;
474
                                    pgs = dState.SEND_RESET;
475
                                }
476
                            }
477
                        }
478
                        break;
479
 
480
                    case dState.SEND_EMPTY_DATA:
481
                        // Send program data.
482
                        data[0] = 0;
483
                        data[1] = 0;
484
                        data[2] = 0xff;
485
                        data[3] = 0xff;
486
 
487
                        outCm = cpn.getPgmDataPacket(data, 4);
488
                        dc.sendCanPacket(outCm);
489
 
490
                        t = Environment.TickCount;
632 arune 491
                        t2 = Environment.TickCount;
586 arune 492
                        byteSent += 6;
493
                        pgs = dState.WAIT_ACK_EMPTY_DATA;
494
                        break;
495
 
496
 
497
                    case dState.WAIT_ACK_EMPTY_DATA:
498
                        // Wait for pgm ack.
499
                        if ((Environment.TickCount - t) > 2*TIMEOUT_MS) {
500
                            // Woops, error. 
501
                            Console.WriteLine("Timeout while waiting for data ack.");
502
                            errorOccured = true;
503
                            pgs = dState.SEND_RESET;
504
                        }
505
                        //Thread.Sleep(1);
506
                        // If received ack.
507
                        hasMessage = dc.getData(out cm);
508
                        if (hasMessage) {
770 arune 509
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
510
                            if (cpnin.isNMTPacket()) {
511
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 512
                                // If no error
513
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
514
                                    pgs = dState.SEND_DONE_EMPTY;
515
                                }
516
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
517
                                    // else ..
518
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
519
                                    errorOccured = true;
520
                                    pgs = dState.SEND_RESET;
521
                                }
522
                            }
523
                        }
524
                        break;
525
 
526
                    case dState.SEND_DONE_EMPTY:
527
                        // Send done
528
                        outCm = cpn.getPgmEndPacket();
529
                        dc.sendCanPacket(outCm);
530
 
531
                        t = Environment.TickCount;
632 arune 532
                        t2 = Environment.TickCount;
586 arune 533
                        pgs = dState.WAIT_DONE_EMPTY;
534
                        break;
535
 
536
 
537
                    case dState.WAIT_DONE_EMPTY:
538
                        // Check for timeout, resend done packet in that case..
539
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
540
                            Console.WriteLine("Timeout while waiting for end prg ack.");
541
                            errorOccured = true;
542
                            pgs = dState.SEND_RESET;
543
                        }
544
                        //Thread.Sleep(1);
545
 
546
                        hasMessage = dc.getData(out cm);
547
                        if (hasMessage) {
770 arune 548
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
549
                            if (cpnin.isNMTPacket()) {
550
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 551
                                // If no error
552
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
553
                                    Console.WriteLine("");
554
                                    //
555
                                    Console.WriteLine("Done, successfully cleaned application flash.");
556
                                    pgs = dState.SEND_RESET;
557
                                }
558
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
559
                                    // else resend addr..
560
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
561
                                    errorOccured = true;
562
                                    pgs = dState.SEND_RESET;
563
                                }
564
                            }
565
                        }
566
                        break;                     
453 arune 567
                    case dState.SEND_RESET:
568
                        // Send reset
770 arune 569
                        cpn.doReset(dc, receiveID, hwid);
453 arune 570
                        t = Environment.TickCount;
632 arune 571
                        t2 = Environment.TickCount;
453 arune 572
                        pgs = dState.DONE;
573
                        break;
574
 
575
 
576
                    case dState.DONE:
577
                        done = true;
578
                        //down.Abort();
579
                        if (!errorOccured) {
580
                            long tsecs = ((Environment.TickCount - timeStart) / 1000);
581
                            double bitRate = hf.getLength() / ((double)(Environment.TickCount - timeStart) / 1000.0);
582
 
583
                            if (tsecs <= 0) tsecs = 1;
584
                            long mins = tsecs / 60;
585
                            long secs = tsecs % 60;
586
                            string pad = "";
587
                            if (secs < 10) {
588
                                pad = "0";
589
                            }
590
                            Console.WriteLine("Time spent: " + mins + ":" + pad + secs + ", " + Math.Round(bitRate, 2) + " bytes/s");
591
                        }
592
                        //Console.Write("> ");
593
                        break;
594
                }
595
            }
596
 
597
        } catch(Exception) {
598
            Console.WriteLine("Exception occured");
599
            //Console.WriteLine(e);
600
            //skriva ut antal bytes skrivna vore bra här, kanske nån annan form av hjälp för debug, något gick snett liksom
601
 
602
        }
603
 
604
        return !errorOccured;
605
    }
606
}