Subversion Repositories HomeAutomation

Rev

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