Subversion Repositories HomeAutomation

Rev

Rev 882 | 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;
1458 cougar 48
    private const int TIMEOUT_SHORT_MS = 500;
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;
878 arune 103
        //int dldisplaystate=0;
104
        double lastpercent=0;
105
        double lasttenpercent=0;
106
        int nrOfTicks=0;
876 arune 107
 
453 arune 108
        try {
109
            if (isBiosUpdate) {
110
                dlOffset = hf.getAddrLower();
111
                //Console.WriteLine(dlOffset);
112
                //dlOffset = 200;
113
            }
114
 
770 arune 115
            CanNMT cpn = new CanNMT(HWID_INUSE);
453 arune 116
            cpn.setReceiver(receiveID);
117
            bool hasMessage = false;
118
 
119
            while (!done) {
120
                Thread.Sleep(1);
121
 
122
                switch (pgs) {
123
                    case dState.SEND_START:
124
                        // Send boot start packet to target.
125
                        // and wait for ack.
126
                        currentAddress = hf.getAddrLower();
127
                        //data[RID_INDEX] = TARGET_ID;
128
                        data[ADDR0_INDEX] = (byte)((currentAddress-dlOffset) & 0x0000FF);
129
                        data[ADDR1_INDEX] = (byte)(((currentAddress-dlOffset) & 0x00FF00) >> 8);
130
                        data[ADDR2_INDEX] = (byte)(((currentAddress-dlOffset) & 0xFF0000) >> 16);
131
                        data[ADDR3_INDEX] = 0;
770 arune 132
                        if (HWID_INUSE) {
133
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
134
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
135
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
136
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
137
                        }
453 arune 138
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
139
                        outCm = cpn.getPgmStartPacket(data);
140
                        dc.sendCanPacket(outCm);
141
                        t = Environment.TickCount;
632 arune 142
                        t2 = Environment.TickCount;
453 arune 143
                        pgs = dState.WAIT_ACK_PRG;
144
                        timeStart = Environment.TickCount;
876 arune 145
                        Console.WriteLine("Downloading...");
882 arune 146
                        //Console.Write("  0% [                                        ]\r");
878 arune 147
                        Console.Write("  0% [");
453 arune 148
                        break;
149
 
150
 
151
                    case dState.WAIT_ACK_PRG:
152
                        // Check for timeout, resend start packet in that case..
153
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
154
                            Console.WriteLine("Timeout while waiting for start prg ack.");
155
                            errorOccured = true;
156
                            pgs = dState.SEND_RESET;
157
                        }
467 arune 158
                        //Thread.Sleep(1);
453 arune 159
                        // If received ack.
160
                        hasMessage = dc.getData(out cm);
161
                        if (hasMessage) {
770 arune 162
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
163
                            if (cpnin.isNMTPacket()) {
164
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
453 arune 165
                                // If no error
166
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
167
                                    // Start sending program data..
168
                                    byteSent = 0;
169
                                    pgs = dState.SEND_PGM_DATA;
170
                                }
171
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
172
                                    // else ..
173
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
174
                                    errorOccured = true;
175
                                    pgs = dState.SEND_RESET;
176
                                }
177
                            }
178
                        }
179
                        break;
180
 
181
                    case dState.SEND_PGM_DATA:
876 arune 182
 
878 arune 183
                        double percent = (double)byteSent*100/((double)hf.getAddrUpper()-(double)currentAddress);
184
                        //Console.Write("  0% [                                        ]");
876 arune 185
 
878 arune 186
                        if (percent >= lastpercent+2.5) {
187
                        //Console.WriteLine(percent);
188
                           Console.Write("=");
189
                           nrOfTicks++;
190
                           lastpercent = lastpercent+2.5;
876 arune 191
                        }
878 arune 192
                        if (percent > lasttenpercent+10) {
879 arune 193
                           for (int i=nrOfTicks; i<40; i++) {
194
                              Console.Write(" ");
195
                           }
196
                           Console.Write("]");
878 arune 197
                           Console.Write("\r");
198
                           Console.Write(" "+Math.Round(percent,0)+"% [");
199
 
882 arune 200
                           lasttenpercent = lasttenpercent+10;
878 arune 201
                           for (int i=0; i<nrOfTicks; i++) {
202
                              Console.Write("=");
203
                           }
204
                        }
876 arune 205
 
878 arune 206
 
453 arune 207
                        // Send program data.
208
                        byte datalength = 2;
209
                        for (ulong i = 0; i < 6; i++) {
210
                            // Populate data.
211
                            data[i+2] = hf.getByte(currentAddress + i + byteSent);
212
                            if (currentAddress + i + byteSent > hf.getAddrUpper()) {
213
                                break;  //then done
214
                            }
215
                            datalength++;
216
                        }
217
                        data[0] = (byte)(byteSent & 0x00FF);
218
                        data[1] = (byte)((byteSent & 0xFF00) >> 8);
219
 
220
                        outCm = cpn.getPgmDataPacket(data, datalength);
221
                        dc.sendCanPacket(outCm);
222
 
223
                        t = Environment.TickCount;
632 arune 224
                        t2 = Environment.TickCount;
453 arune 225
                        byteSent += 6;
226
                        pgs = dState.WAIT_ACK_DATA;
227
                        break;
228
 
229
 
230
                    case dState.WAIT_ACK_DATA:
231
                        // Wait for pgm ack.
632 arune 232
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
453 arune 233
                            // Woops, error. 
234
                            Console.WriteLine("Timeout while waiting for data ack.");
235
                            errorOccured = true;
236
                            pgs = dState.SEND_RESET;
237
                        }
634 arune 238
                        else if ((Environment.TickCount - t2) > TIMEOUT_SHORT_MS) {
632 arune 239
                            // Woops, error. 
876 arune 240
//disable                           Console.Write(":");
632 arune 241
                            byteSent -= 6;
242
                            pgs = dState.SEND_PGM_DATA;
243
                            break;
244
                        }
467 arune 245
                        //Thread.Sleep(1);
453 arune 246
                        // If received ack.
247
                        hasMessage = dc.getData(out cm);
248
                        if (hasMessage) {
770 arune 249
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
250
                            if (cpnin.isNMTPacket()) {
251
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
453 arune 252
                                // If no error
253
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
254
                                    //currentAddress += 2;
255
                                    // Check if end.
256
                                    if (currentAddress + byteSent > hf.getAddrUpper()) {
257
                                        // Yes, we are done, send done.
258
                                        pgs = dState.SEND_DONE;
259
                                    }
260
                                    else {
261
                                        // More to write.
262
                                        //byteSent = 0;
263
                                        pgs = dState.SEND_PGM_DATA;
264
                                    }
265
                                }
266
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
267
                                    // else ..
268
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
269
                                    errorOccured = true;
270
                                    pgs = dState.SEND_RESET;
271
                                }
272
                            }
273
                        }
274
                        break;
275
 
276
                    case dState.SEND_DONE:
879 arune 277
                       for (int i=nrOfTicks; i<40; i++) {
278
                          Console.Write(" ");
279
                       }
280
                       Console.Write("]");
876 arune 281
                       Console.Write("\r");
879 arune 282
                       Console.Write("100% [========================================]");
453 arune 283
                        // Send done
284
                        outCm = cpn.getPgmEndPacket();
285
                        dc.sendCanPacket(outCm);
286
 
287
                        t = Environment.TickCount;
632 arune 288
                        t2 = Environment.TickCount;
453 arune 289
                        pgs = dState.WAIT_DONE;
290
                        break;
291
 
292
 
293
                    case dState.WAIT_DONE:
294
                        // Check for timeout, resend done packet in that case..
295
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
296
                            Console.WriteLine("Timeout while waiting for end prg ack.");
297
                            errorOccured = true;
298
                            pgs = dState.SEND_RESET;
299
                        }
467 arune 300
                        //Thread.Sleep(1);
453 arune 301
 
302
                        hasMessage = dc.getData(out cm);
303
                        if (hasMessage) {
770 arune 304
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
305
                            if (cpnin.isNMTPacket()) {
306
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
453 arune 307
                                // If no error
308
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
309
                                    Console.WriteLine("");
310
                                    //
311
                                    int crc = calcCRC(hf);
312
 
313
                                    if (cm.getDataLength() == 2) {
314
                                        byte[] cmdata = cm.getData();
315
                                        if ((cmdata[0] == (crc & 0xFF)) && (cmdata[1] == (crc>>8))) {
316
                                            if (!isBiosUpdate) {
317
                                                Console.WriteLine("Done, successfully programmed application.");
318
                                                pgs = dState.SEND_RESET;
319
                                            } else {
320
                                                Console.WriteLine("Done, successfully programmed bios to application space.");
321
                                                pgs = dState.SEND_BIOS_UPDATE;
322
                                            }
323
                                        } else {
324
                                            string nodecrc = String.Format("{0:x4}", (int)cmdata[0] + (int)(cmdata[1]<<8));
325
                                            nodecrc = "0x"+nodecrc;
326
                                            string realcrc = String.Format("{0:x4}", (int)(crc & 0xFF) + (int)(crc & 0xFF00));
327
                                            realcrc = "0x"+realcrc;
328
                                            Console.WriteLine("CRC failed. Node sent CRC "+nodecrc+" but should be "+realcrc+".");
329
                                            errorOccured = true;
330
                                            pgs = dState.SEND_RESET;
331
                                        }
332
 
333
                                        byteSent = 0;
334
                                    }
335
                                    //Console.WriteLine("crc: " + crc + " " + (crc>>8) + " " + (crc & 0xFF));
336
                                    //Console.WriteLine("ACKpkg with CRC: " + cm.ToString());
337
                                    // 
338
                                }
339
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
340
                                    // else resend addr..
341
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
342
                                    errorOccured = true;
343
                                    pgs = dState.SEND_RESET;
344
                                }
345
                            }
346
                        }
347
                        break;
348
 
349
                    case dState.SEND_BIOS_UPDATE:
350
                        data[0] = (byte)(0 & 0x00FF);
351
                        data[1] = (byte)((0 & 0xFF00) >> 8);
352
                        data[2] = (byte)(dlOffset & 0x00FF);
353
                        data[3] = (byte)((dlOffset & 0xFF00) >> 8);
354
                        data[4] = (byte)((hf.getAddrUpper()-hf.getAddrLower()+1) & 0x00FF);
355
                        data[5] = (byte)(((hf.getAddrUpper()-hf.getAddrLower()+1) & 0xFF00) >> 8);
356
 
357
                        outCm = cpn.getPgmCopyPacket(data);
358
                        Console.WriteLine("Now say a prayer, moving bios");
359
                        dc.sendCanPacket(outCm);
360
                        t = Environment.TickCount;
632 arune 361
                        t2 = Environment.TickCount;
586 arune 362
                        pgs = dState.WAIT_BOOT;
363
                        //pgs = dState.DONE;
453 arune 364
                        break;
586 arune 365
 
366
                    case dState.WAIT_BOOT:
367
                        // Wait for node reset answer (boot msg).
637 arune 368
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
586 arune 369
                            // Woops, error. 
370
                            Console.WriteLine("Timeout while waiting for node to boot.");
371
                            errorOccured = true;
372
                            pgs = dState.SEND_DONE;
373
                        }
374
                        //Thread.Sleep(1);
375
                        // If received boot msg.
376
                        hasMessage = dc.getData(out cm);
377
                        if (hasMessage) {
770 arune 378
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
379
                            if (cpnin.isNMTPacket()) {
380
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 381
                                // If no error
382
                                if (cpnin.getType() == CAN_NMT_BIOS_START) {
383
                                    pgs = dState.SEND_EMPTY_START;
384
                                }
385
                                else {
386
                                    // else ..
387
                                }
388
                            }
389
                        }
390
                        break;
453 arune 391
 
586 arune 392
                    case dState.SEND_EMPTY_START:
393
                        // Send boot start packet to target.
394
                        // and wait for ack.
395
 
396
                        //data[RID_INDEX] = TARGET_ID;
397
                        data[ADDR0_INDEX] = (byte)((0) & 0x0000FF);
398
                        data[ADDR1_INDEX] = (byte)(((0) & 0x00FF00) >> 8);
399
                        data[ADDR2_INDEX] = (byte)(((0) & 0xFF0000) >> 16);
400
                        data[ADDR3_INDEX] = 0;
770 arune 401
                        if (HWID_INUSE) {
402
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
403
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
404
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
405
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
406
                        }
586 arune 407
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
408
                        outCm = cpn.getPgmStartPacket(data);
409
                        dc.sendCanPacket(outCm);
410
                        t = Environment.TickCount;
632 arune 411
                        t2 = Environment.TickCount;
586 arune 412
                        pgs = dState.WAIT_ACK_PRG_EMPTY;
413
                        break;
414
 
415
 
416
                    case dState.WAIT_ACK_PRG_EMPTY:
417
                        // Check for timeout, resend start packet in that case..
637 arune 418
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
770 arune 419
                            Console.WriteLine("Timeout while waiting for start prg ack during empty-first-block-cycle.");
586 arune 420
                            errorOccured = true;
421
                            pgs = dState.SEND_RESET;
422
                        }
423
                        //Thread.Sleep(1);
424
                        // If received ack.
425
                        hasMessage = dc.getData(out cm);
426
                        if (hasMessage) {
770 arune 427
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
428
                            if (cpnin.isNMTPacket()) {
429
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 430
                                // If no error
431
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
432
                                    // Start sending program data..
433
                                    byteSent = 0;
434
                                    pgs = dState.SEND_EMPTY_DATA;
435
                                }
436
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
437
                                    // else ..
438
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
439
                                    errorOccured = true;
440
                                    pgs = dState.SEND_RESET;
441
                                }
442
                            }
443
                        }
444
                        break;
445
 
446
                    case dState.SEND_EMPTY_DATA:
447
                        // Send program data.
448
                        data[0] = 0;
449
                        data[1] = 0;
450
                        data[2] = 0xff;
451
                        data[3] = 0xff;
452
 
453
                        outCm = cpn.getPgmDataPacket(data, 4);
454
                        dc.sendCanPacket(outCm);
455
 
456
                        t = Environment.TickCount;
632 arune 457
                        t2 = Environment.TickCount;
586 arune 458
                        byteSent += 6;
459
                        pgs = dState.WAIT_ACK_EMPTY_DATA;
460
                        break;
461
 
462
 
463
                    case dState.WAIT_ACK_EMPTY_DATA:
464
                        // Wait for pgm ack.
465
                        if ((Environment.TickCount - t) > 2*TIMEOUT_MS) {
466
                            // Woops, error. 
467
                            Console.WriteLine("Timeout while waiting for data ack.");
468
                            errorOccured = true;
469
                            pgs = dState.SEND_RESET;
470
                        }
471
                        //Thread.Sleep(1);
472
                        // If received ack.
473
                        hasMessage = dc.getData(out cm);
474
                        if (hasMessage) {
770 arune 475
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
476
                            if (cpnin.isNMTPacket()) {
477
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 478
                                // If no error
479
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
480
                                    pgs = dState.SEND_DONE_EMPTY;
481
                                }
482
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
483
                                    // else ..
484
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
485
                                    errorOccured = true;
486
                                    pgs = dState.SEND_RESET;
487
                                }
488
                            }
489
                        }
490
                        break;
491
 
492
                    case dState.SEND_DONE_EMPTY:
493
                        // Send done
494
                        outCm = cpn.getPgmEndPacket();
495
                        dc.sendCanPacket(outCm);
496
 
497
                        t = Environment.TickCount;
632 arune 498
                        t2 = Environment.TickCount;
586 arune 499
                        pgs = dState.WAIT_DONE_EMPTY;
500
                        break;
501
 
502
 
503
                    case dState.WAIT_DONE_EMPTY:
504
                        // Check for timeout, resend done packet in that case..
505
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
506
                            Console.WriteLine("Timeout while waiting for end prg ack.");
507
                            errorOccured = true;
508
                            pgs = dState.SEND_RESET;
509
                        }
510
                        //Thread.Sleep(1);
511
 
512
                        hasMessage = dc.getData(out cm);
513
                        if (hasMessage) {
770 arune 514
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
515
                            if (cpnin.isNMTPacket()) {
516
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
586 arune 517
                                // If no error
518
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
519
                                    Console.WriteLine("");
520
                                    //
521
                                    Console.WriteLine("Done, successfully cleaned application flash.");
522
                                    pgs = dState.SEND_RESET;
523
                                }
524
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
525
                                    // else resend addr..
526
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
527
                                    errorOccured = true;
528
                                    pgs = dState.SEND_RESET;
529
                                }
530
                            }
531
                        }
532
                        break;                     
453 arune 533
                    case dState.SEND_RESET:
534
                        // Send reset
770 arune 535
                        cpn.doReset(dc, receiveID, hwid);
453 arune 536
                        t = Environment.TickCount;
632 arune 537
                        t2 = Environment.TickCount;
453 arune 538
                        pgs = dState.DONE;
539
                        break;
540
 
541
 
542
                    case dState.DONE:
543
                        done = true;
544
                        //down.Abort();
545
                        if (!errorOccured) {
546
                            long tsecs = ((Environment.TickCount - timeStart) / 1000);
547
                            double bitRate = hf.getLength() / ((double)(Environment.TickCount - timeStart) / 1000.0);
548
 
549
                            if (tsecs <= 0) tsecs = 1;
550
                            long mins = tsecs / 60;
551
                            long secs = tsecs % 60;
552
                            string pad = "";
553
                            if (secs < 10) {
554
                                pad = "0";
555
                            }
556
                            Console.WriteLine("Time spent: " + mins + ":" + pad + secs + ", " + Math.Round(bitRate, 2) + " bytes/s");
557
                        }
558
                        //Console.Write("> ");
559
                        break;
560
                }
561
            }
562
 
563
        } catch(Exception) {
564
            Console.WriteLine("Exception occured");
565
            //Console.WriteLine(e);
566
            //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
567
 
568
        }
569
 
570
        return !errorOccured;
571
    }
572
}