DELL
2025-04-28 e6eb7fb0af366e370f125668d62e89eb0004f517
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
package com.dev.fbs9600_mon;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.zip.CRC32;
 
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
 
import main.page_debug_inf;
import sp_comm.CommSerialPort;
import sp_comm.SP_COMM;
 
import com.ComFn;
import com.Crc16;
import com.Ecb_Aes;
import com.PlaySound;
 
public class SPCommMon_DFU {
    public static final short CMD_TYPE_READ = 0x03;
    public static final short CMD_TYPE_WRITE = 0x05;
    
    public static final byte BOOTLOADER_CMD_QUIT = 0;
    public static final byte BOOTLOADER_CMD_GETINF = 1;
    public static final byte BOOTLOADER_CMD_WRITE = 2;
    public static final byte BOOTLOADER_CMD_READ = 3;
    
    public static final int BOOTLOADER_BUF_LEN_128 = 128;
    public static final int BOOTLOADER_BUF_LEN_256 = 256;
    public static final int BOOTLOADER_BUF_LEN_1024 = 1024;
    private int BOOTLOADER_BUF_LEN = SPCommMon_DFU.BOOTLOADER_BUF_LEN_256;
 
    private ByteBuffer DFUCommRxBuffer = ByteBuffer.allocate(1248); 
    private ByteBuffer DFUCommTxBuffer = ByteBuffer.allocate(1248);
    
    public short comm_tx_cnt = 0;
    public short comm_rx_cnt = 0;
    
    public int dt_target_addr = 0;
    public short dt_dfu_addr_start = 0;
    public short dt_dfu_addr_end = 0;
    
    private boolean dt_dfu_read_check_en = false;
    private int dt_dfu_FirmVersion = 0;
    private int dt_multy_comm = CommSerialPort.mutycomm_Type_Samd09;
    private int m_DEV_TYPE = SPCommMon.DEV_TYPE_MON_SAMD09;
    private JTextArea dfu_inf = null;
    private page_debug_inf dt_debug_inf = null;
    
    //private SPCommMon m_SPComm = null;
    private SP_COMM m_COMM = null;
    private boolean dt_update_en = true;
    private boolean CommThread_EN = true;
    /*
    public SPCommMon_DFU(SPCommMon sp_comm) {
        m_SPComm = sp_comm;
        m_DEV_TYPE = m_SPComm.m_DEV_TYPE;
        dt_target_addr = m_SPComm.dt_target_addr;
        dt_multy_comm = m_SPComm.dt_multy_comm;
        dt_dfu_read_check_en = m_SPComm.dt_dfu_read_check_en;
        dfu_inf = m_SPComm.dt_show_dfu_inf;
        
        switch(m_DEV_TYPE) {
            case SPCommMon.DEV_TYPE_MON_SAMD09: BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256; break;
            case SPCommMon.DEV_TYPE_MON_MSP430: BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256; break;
            case SPCommMon.DEV_TYPE_MON_MSP430_RF: BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_128; break;
            case SPCommMon.DEV_TYPE_MON_SWM: BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256; break;
            case SPCommMon.DEV_TYPE_NTM: BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_1024; break;
        }
        DFUCommTxBuffer.order(ByteOrder.LITTLE_ENDIAN);
        DFUCommRxBuffer.order(ByteOrder.LITTLE_ENDIAN);
    }
    */
    public SPCommMon_DFU(SP_COMM sp_comm, int dev_type, int tar_addr, 
                            boolean dfu_ck_en, JTextArea dfu_inf_t, page_debug_inf debug_inf) {
        m_COMM = sp_comm;
        m_DEV_TYPE = dev_type;
        dt_target_addr = tar_addr;
        dt_multy_comm = CommSerialPort.mutycomm_Type_Samd09;
        dt_dfu_read_check_en = dfu_ck_en;
        dfu_inf = dfu_inf_t;
        dt_debug_inf = debug_inf;
        
        switch(m_DEV_TYPE) {
            case SPCommMon.DEV_TYPE_MON_SAMD09: 
                BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256;
                dt_multy_comm = CommSerialPort.mutycomm_Type_Samd09;
            break;
            
            case SPCommMon.DEV_TYPE_MON_MSP430: {
                BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256; 
                dt_multy_comm = CommSerialPort.mutycomm_Type_Msp430;
            }
            break;
            
            case SPCommMon.DEV_TYPE_MON_MSP430_RF:  {
                BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_128; 
                dt_multy_comm = CommSerialPort.mutycomm_Type_Msp430;
            }
            break;
            
            case SPCommMon.DEV_TYPE_MON_SWM: {
                BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256; 
                dt_multy_comm = CommSerialPort.mutycomm_Type_SWM;
            }
            break;
            
            case SPCommMon.DEV_TYPE_NTM: 
                BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_1024; 
                dt_multy_comm = CommSerialPort.mutycomm_Type_Samd09;
            break;
        }
        DFUCommTxBuffer.order(ByteOrder.LITTLE_ENDIAN);
        DFUCommRxBuffer.order(ByteOrder.LITTLE_ENDIAN);
    }
    
    public void setDFUCommState(boolean comm_en, boolean dfu_en) {
        CommThread_EN = comm_en;
        dt_update_en = dfu_en;
    }
    
    private ByteBuffer makeDfuBuf(ByteBuffer buf_t, byte cmd, int prog_addr, byte[] bt_data) {
        buf_t.order(ByteOrder.LITTLE_ENDIAN);
        buf_t.clear();
        //---------------------------------------------------//
        //-------------- BOOTLOADER_CMD_GETINF --------------//
        if((CommSerialPort.mutycomm_Type_Msp430 == dt_multy_comm)
            || (CommSerialPort.mutycomm_Type_SWM == dt_multy_comm)) {
            buf_t.put((byte) dt_target_addr);
            for(int n=0; n<3; n++) {
                buf_t.put((byte) 0xAA);
            }
        } else {
            for(int n=0; n<3; n++) {
                buf_t.put((byte) 0xAA);
            }
            buf_t.put((byte) dt_target_addr);
        }
        
        buf_t.put(cmd);
        buf_t.put((byte) 0x00);
        if((SPCommMon.DEV_TYPE_MON_MSP430==m_DEV_TYPE) 
            || (SPCommMon.DEV_TYPE_MON_MSP430_RF==m_DEV_TYPE)) {
            buf_t.putShort((short) prog_addr);
        } else {
            buf_t.putInt(prog_addr);
        }
        buf_t.putShort((short) 0);
        
        if(BOOTLOADER_CMD_WRITE == cmd) {
            //for(int n=0; n<BOOTLOADER_BUF_LEN; n++) {
            buf_t.put(bt_data);
            //}
        }
        
        int pos = buf_t.position();
        int crc = Crc16.CalCRC16(buf_t, pos) & 0xFFFF;
        if((SPCommMon.DEV_TYPE_MON_MSP430==m_DEV_TYPE) 
            || (SPCommMon.DEV_TYPE_MON_MSP430_RF==m_DEV_TYPE)) {
            buf_t.putShort(8, (short) (crc));
        } else {
            buf_t.putShort(10, (short) (crc));
        }
        
        buf_t.flip();
        return buf_t;
    }
    
    private int checkDfuBuf(ByteBuffer buf_t, byte cmd, int prog_addr) {
        int res = 0;
        if((SPCommMon.DEV_TYPE_MON_MSP430==m_DEV_TYPE) 
            || (SPCommMon.DEV_TYPE_MON_MSP430_RF==m_DEV_TYPE)) {
            if(buf_t.limit() < 10) {
                return -1;
            }
        } else {
            if(buf_t.limit() < 12) {
                return -2;
            }
        }
        
        int dev_addr = 0;
        if((SPCommMon.DEV_TYPE_MON_MSP430==m_DEV_TYPE) 
            || (SPCommMon.DEV_TYPE_MON_SWM==m_DEV_TYPE)) {
            buf_t.position(0);
            dev_addr = buf_t.get() & 0xFF;
        } else {
            buf_t.position(3);
            dev_addr = buf_t.get() & 0xFF;
        }
        buf_t.position(4);
        byte dfu_cmd = buf_t.get();
        byte dfu_res = buf_t.get();
        int p_addr = 0;
        int crc = 0;
        if((SPCommMon.DEV_TYPE_MON_MSP430==m_DEV_TYPE) 
            || (SPCommMon.DEV_TYPE_MON_MSP430_RF==m_DEV_TYPE)) {
            p_addr = buf_t.getShort()& 0xFFFF;
            crc = buf_t.getShort() & 0xFFFF;
            buf_t.position(8);
        } else {
            p_addr = buf_t.getInt();
            crc = buf_t.getShort() & 0xFFFF;
            buf_t.position(10);
        }
        buf_t.putShort((short) 0);
        int crc_source = Crc16.CalCRC16(buf_t, buf_t.limit()) & 0xFFFF;
        /*************************************************/
        if(crc_source != crc) {
            res = -3;
        }
        if(dev_addr != dt_target_addr) {
            res = -4;
        }
        if(dfu_cmd != cmd) {
            res = -5;
        }
        if(0 != dfu_res ) {
            res = -6;
        }
        if((BOOTLOADER_CMD_GETINF!=cmd) && (BOOTLOADER_CMD_QUIT!=cmd) && (p_addr!=prog_addr)) {
            res = -7;
        }
        if(0 == res) {
            if(BOOTLOADER_CMD_GETINF == cmd) {
                dt_dfu_FirmVersion = p_addr;
            }
        }
        
        return res;
    }
    
    private ByteBuffer check_DFU_File(String dfufile, String dfu_text_inf_t) {
        String dfu_text_inf = dfu_text_inf_t;
        Ecb_Aes my_aes = new Ecb_Aes();
        File f = new File(dfufile);
        long file_len_hd = f.length();
        ByteBuffer bbf_data = ByteBuffer.allocate((int) file_len_hd);
        bbf_data.order(ByteOrder.BIG_ENDIAN);
        try {
            FileInputStream fis_hd = new FileInputStream(f);
            byte[] buf_hd_cipher = new byte[(int) (file_len_hd)];
            fis_hd.read(buf_hd_cipher);
            fis_hd.close();
            
            byte[] buf_hd = new byte[(int) (file_len_hd)];
            my_aes.ecb_decrypt(buf_hd_cipher, buf_hd, buf_hd_cipher.length);
            bbf_data.put(buf_hd);
            bbf_data.flip();
            int crc32_source = bbf_data.getInt();
            
            CRC32 crc32 = new CRC32();
            crc32.update(bbf_data);
            
            if(crc32_source != (int)crc32.getValue()) {
                dfu_text_inf += "File CRC Check Error!";
                dfu_inf.setText(dfu_text_inf);
                JOptionPane.showMessageDialog(null, "ÊäÈëÎļþ´íÎó»ò¸ñʽÒì³£!");
                bbf_data.clear();
                return null;
            }
            
            bbf_data.position(Integer.BYTES);
            byte[] buf_hd_inf = new byte[64-4];
            bbf_data.get(buf_hd_inf);
            int len = 0;
            for(int n=0; n<buf_hd_inf.length; n++) {
                if(buf_hd_inf[n] > 0) {
                    len += 1;
                }
            }
            dfu_text_inf += "DFU File CRC Check OK, File Inf: " + new String(buf_hd_inf, 0, len, "UTF-8") + "\n";
            dfu_inf.setText(dfu_text_inf);
        } catch (IOException e1) {
            bbf_data.clear();
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return null;
        }
        
        return bbf_data;
    }
    
    private String getAndCheckDFUInf(String dfu_text_inf_t, int dfu_file_len) {
        String dfu_text_inf = dfu_text_inf_t;
        //-------------------------- BOOTLOADER_CMD_GETINF ---------------------------//
        try {
            //System.out.println("dt_dfu_bitrate:" + dt_dfu_bitrate);
            int dot_count = 0;
            while(true) {
                //====================================//
                if((false == dt_update_en) || (false == CommThread_EN)) {
                    dfu_text_inf += "DFU Manual Stop . . .\n";
                    break;
                }
                //====================================//
                Thread.sleep(250);
                m_COMM.tr_Msg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_GETINF, dfu_file_len, null), 
                                    DFUCommRxBuffer, dt_multy_comm, dt_debug_inf, true);
                int ch_res = checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_GETINF, 0);
                if(0 == ch_res) {
                    break;
                }
                String text_t = " " + String.valueOf(ch_res);
                for(int n=0; n<dot_count; n++) {
                    text_t += " .";
                }
                if(++dot_count >= 6) {
                    dot_count = 0;
                }
                
                dfu_inf.setText(dfu_text_inf + "Wait For Target DFU Ready" + text_t + "\n");
                //System.out.println("DFURX:" + ComFn.bytesToHexString(DFUCommRxBuffer.array(), DFUCommRxBuffer.limit()) + "\n");
            }
            if(true == dt_update_en) {
                dfu_text_inf += "DFU_Version: " + Long.toHexString(dt_dfu_FirmVersion).toUpperCase() + "\n";
                if(0x10010001<= dt_dfu_FirmVersion) {
                    if(0x10010005 <= dt_dfu_FirmVersion) {
                        if(false == dfu_text_inf.contains("FBS9600-DTS-March-DFU-0x10010005")) {
                            dfu_text_inf += "Error!!!!! Target DFU Is March *.cim AppFile To Write!\n";
                            dt_update_en = false;
                        }
                    } else {
                        if(true == dfu_text_inf.contains("FBS9600-DTS-March-DFU-0x10010005")) {
                            dfu_text_inf += "Error!!!!! Target DFU Is March *.sam AppFile To Write!\n";
                            dt_update_en = false;
                        }
                    }
                    if(0x1001000A == dt_dfu_FirmVersion) {
                        BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_128;
                    } else {
                    //    BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256;
                    }
                }
            }
            if(true == dt_update_en) {
                dfu_text_inf += "Target DFU Ready OK!\n";
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //-----------------------------------------------------------------------------//
        
        return dfu_text_inf;
    }
    
    private boolean DFU_Write(ArrayList<byte[]> al_dfu_data, ArrayList<Integer> al_dfu_addr, String dfu_text_inf_t) {
        String dfu_text_inf = dfu_text_inf_t;
        //--------------------------- BOOTLOADER_CMD_WRITE-----------------------------//
        boolean prog_ok = false;
        try {
            Thread.sleep(100);
            int prog_addr = 0;
            int percent = 0;
            int dfu_dat_index = 0;
            while(dfu_dat_index < al_dfu_data.size()) {
                //====================================//
                if((false == dt_update_en) || (false == CommThread_EN)) {
                    dfu_text_inf += "DFU Manual Stop . . .\n";
                    break;
                }
                //====================================//
                byte[] buf_to_flash = al_dfu_data.get(dfu_dat_index);
                prog_addr = al_dfu_addr.get(dfu_dat_index);
                dfu_dat_index += 1;
                //====================================//
                //System.out.println(ComFn.bytesToHexString(buf_to_flash, BOOTLOADER_BUF_LEN) + "\n");
                for(int cnt_t=0; cnt_t<5; cnt_t++) {
                    m_COMM.tr_Msg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_WRITE, prog_addr, buf_to_flash), 
                                                DFUCommRxBuffer, dt_multy_comm, dt_debug_inf, false);
                    if(0 == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_WRITE, prog_addr)) {
                        prog_ok = true;
                        break;
                    } else {
                        prog_ok = false;
                        Thread.sleep(100);
                    }
                }
                if(false == prog_ok) {
                    dfu_text_inf += "DFU Write Error!\n";
                    break;
                }
                
                if(percent < (dfu_dat_index*100)/al_dfu_data.size()) {
                    percent = (int) ((dfu_dat_index*100)/al_dfu_data.size());
                    if(percent > 100) {
                        percent = 100;
                    }
                    dfu_inf.setText(dfu_text_inf + String.format("DFU Write: %d%% Done.\n", percent));
                }
            }
            
            if((true == dt_update_en) || (true == CommThread_EN)) {
                dfu_text_inf += "DFU Write: 100% Done!\n";
            }
            
            dfu_inf.setText(dfu_text_inf);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //---------------------------------------------------------------------------------//
        return prog_ok;
        //---------------------------------------------------------------------------------//
    }
    
    private boolean DFU_Read(ArrayList<byte[]> al_dfu_data, ArrayList<Integer> al_dfu_addr, String dfu_text_inf_t) {
        String dfu_text_inf = dfu_text_inf_t;
        boolean check_res = false;
        int prog_addr = 0;
        int percent = 0;
        int dfu_dat_index = 0;
        try {
            while(dfu_dat_index < al_dfu_data.size()) {
                //====================================//
                if((false == dt_update_en) || (false == CommThread_EN)) {
                    dfu_text_inf += "DFU Manual Stop . . .\n";
                    break;
                }
                //====================================//
                byte[] buf_to_flash = al_dfu_data.get(dfu_dat_index);
                prog_addr = al_dfu_addr.get(dfu_dat_index);
                dfu_dat_index += 1;
                //====================================//
                for(int cnt_t=0; cnt_t<3; cnt_t++) {
                    m_COMM.tr_Msg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_READ, prog_addr, null), 
                                                DFUCommRxBuffer, dt_multy_comm, dt_debug_inf, false);
                    if(0 == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_READ, prog_addr)) {
                        check_res = true;
                        break;
                    } else {
                        check_res = false;
                        Thread.sleep(100);
                    }
                }
                if(false == check_res) {
                    break;
                }
                
                byte[] rx_data_check = new byte[BOOTLOADER_BUF_LEN];
                if((SPCommMon.DEV_TYPE_MON_MSP430==m_DEV_TYPE) 
                        || (SPCommMon.DEV_TYPE_MON_MSP430_RF==m_DEV_TYPE)) {
                    DFUCommRxBuffer.position(10);
                } else {
                    DFUCommRxBuffer.position(12);
                }
                DFUCommRxBuffer.get(rx_data_check);
                for(int n=0; n<BOOTLOADER_BUF_LEN; n++) {
                    if(buf_to_flash[n] != rx_data_check[n]) {
                        check_res = false;
                        break;
                    }
                }
                
                if(percent < (dfu_dat_index*100)/al_dfu_data.size()) {
                    percent = (int) ((dfu_dat_index*100)/al_dfu_data.size());
                    if(percent > 100) {
                        percent = 100;
                    }
                    dfu_inf.setText(dfu_text_inf + String.format("DFU Verify: %d%% Done.\n", percent));
                }
                
                if(false == check_res) {
                    break;
                }
            }
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        
        return check_res;
    }
    
    public boolean runDFU(String dt_update_file, String dfu_text_inf_t) {
        String dfu_text_inf = dfu_text_inf_t + "BOOTLOADER_BUF_LEN:" + BOOTLOADER_BUF_LEN + "\n";
        //-------------------------- FILE CRC CHECK ---------------------------//
        ByteBuffer bbf_data = check_DFU_File(dt_update_file, dfu_text_inf);
        if(null == bbf_data) {
            return false;
        }
        dfu_text_inf = dfu_inf.getText();
        //-------------------------- BOOTLOADER_CMD_GETINF ---------------------------//
        int f_len = 0;
        if(SPCommMon.DEV_TYPE_NTM == m_DEV_TYPE) {
            f_len = bbf_data.limit()-64;
        }
        dfu_text_inf = getAndCheckDFUInf(dfu_text_inf, f_len);
        //-----------------------------------------------------------------------------//
        ArrayList<byte[]> al_dfu_data = new ArrayList<byte[]>();
        ArrayList<Integer> al_dfu_addr = new ArrayList<Integer>();
        if((true == dt_update_en) && (true == CommThread_EN)) {
            if(dt_dfu_FirmVersion < 21410) {
                bbf_data.position(64);
                byte[] txt_bytes = new byte[bbf_data.limit()-64];
                bbf_data.get(txt_bytes);
                long file_len = txt_bytes.length/3;
                dfu_text_inf = getMSP430BinCode(dt_dfu_FirmVersion, txt_bytes, file_len, dfu_text_inf, 
                                                BOOTLOADER_BUF_LEN, al_dfu_data, al_dfu_addr);
            } else {
                dfu_text_inf = getBinCode(dt_dfu_FirmVersion, bbf_data, bbf_data.capacity(), dfu_text_inf, 
                                                BOOTLOADER_BUF_LEN, al_dfu_data, al_dfu_addr);
            }
        }
        //-----------------------------------------------------------------------------//
        //--------------------------- BOOTLOADER_CMD_WRITE-----------------------------//
        boolean prog_ok = DFU_Write(al_dfu_data, al_dfu_addr, dfu_text_inf);
        dfu_text_inf = dfu_inf.getText();
        //------------------------------ BOOTLOADER_CMD_READ --------------------------//
        boolean check_res = prog_ok;
        if(true == check_res) {
            try {
                Thread.sleep(100);
                if(true == dt_dfu_read_check_en) {
                    check_res = DFU_Read(al_dfu_data, al_dfu_addr, dfu_text_inf);
                }
                if((true == dt_update_en) && (true == CommThread_EN)) {
                    if(true == check_res) {
                        dfu_text_inf += "DFU Verify 100% Done.\n";
                        dfu_text_inf += "DFU Completed ~_~\n";
                        boolean restart_ck = false;
                        for(int cnt_t=0; cnt_t<3; cnt_t++) {
                            m_COMM.tr_Msg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_QUIT, 0, null), 
                                                        DFUCommRxBuffer, dt_multy_comm, dt_debug_inf, false);
                            if(0 == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_QUIT, 0)) {
                                restart_ck = true;
                                break;
                            } else {
                                Thread.sleep(100);
                            }
                        }
                        if(true == restart_ck) {
                            dfu_text_inf += "Restart Target Done.\n";
                            new PlaySound("6133.wav");
                        } else {
                            dfu_text_inf += "Restart Target Faild...\n";
                        }
                    } else {
                        dfu_text_inf += "DFU Verify Failed !!!!!!!!!!!\n";
                    }
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            
            al_dfu_data.clear();
            al_dfu_addr.clear();
        }
        
        return check_res;
    }
    /*********************************************************************************************/
    String getBinCode(int dfu_ver, ByteBuffer bbf_data, long file_len, String dfu_text_inf_t,
                    int dfu_buf_len, ArrayList<byte[]> al_dfu_data, ArrayList<Integer> al_dfu_addr) {
        String dfu_text_inf = dfu_text_inf_t;
        
        int prog_addr = 0;
        int percent = 0;
        bbf_data.position(64);
        boolean bbf_is_end = false;
        
        while(!bbf_is_end) {
            byte[] buf_to_flash = new byte[dfu_buf_len];
            for(int n=0; n<buf_to_flash.length; n++) {
                buf_to_flash[n] = (byte) 0xFF;
                if(bbf_data.hasRemaining()) {
                    buf_to_flash[n] = bbf_data.get();
                } else {
                    bbf_is_end = true;
                }
            }
            
            al_dfu_data.add(buf_to_flash);
            al_dfu_addr.add(prog_addr);
            prog_addr += dfu_buf_len;
            
            if(percent < (prog_addr*100)/file_len) {
                percent = (int) ((prog_addr*100)/file_len);
                if(percent > 100) {
                    percent = 100;
                }
                dfu_inf.setText(dfu_text_inf + String.format("DFU data release: %d%% Done.\n", percent));
            }
        }
        dfu_text_inf += "DFU data release: 100% Done!\n";
        
        return dfu_text_inf;
    }
    /*********************************************************************************************/
    String getMSP430BinCode(int dfu_ver, byte[] txt_bytes, long file_len, String dfu_text_inf_t,
                            int dfu_buf_len, ArrayList<byte[]> al_dfu_data, ArrayList<Integer> al_dfu_addr) {
        String dfu_text_inf = dfu_text_inf_t;
        int BOOT_APP_ADDR = 0x9000;//0xD000;
        int BOOT_APPVECTOR_ADDR = 0xFBDC;//0xFDE0;
        String BOOT_APPVECTOR_tag = "@fb";//0xFDE0;
        String BOOT_APPVECTOR_tagfull = "@fbe0";//0xFDE0;
        
        try {
            int prog_addr = 0;
            int percent = 0;
            BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(txt_bytes)));
            boolean bbf_is_end = false;
            int data_addr = 0;
            
            while(!bbf_is_end) {
                int data_type = 0;
                int data_cnt = 0;
                
                byte[] buf_to_flash = new byte[dfu_buf_len];
                for(int n=0; n<buf_to_flash.length; n++) {
                    buf_to_flash[n] = (byte) 0xFF;
                }
                //-----------------------------------------------------------------------//
                while (true) {
                    String line = "";
                    if((line = br.readLine()) == null) {
                        bbf_is_end = true;
                        break;
                    }
                    //System.out.println("line:" + line);
                    if((line.contains("q")) || (line.length() < 3)) {
                        bbf_is_end = true;
                        break;
                    }
                    if(line.contains("@d000") || line.contains("@9000")) {
                        BOOT_APP_ADDR = Integer.parseInt(line.substring(1, line.length()), 16);
                        if(((dfu_ver/10) >= 2040) && ((dfu_ver/10) < 2141)) {
                            BOOT_APPVECTOR_ADDR = 0xFBDC;
                            BOOT_APPVECTOR_tag = "@fb";
                            BOOT_APPVECTOR_tagfull = "@fbdc";
                        } else {
                            if(0xD000 == BOOT_APP_ADDR) {
                                BOOT_APPVECTOR_ADDR = 0xFDE0;
                                BOOT_APPVECTOR_tag = "@fd";
                                BOOT_APPVECTOR_tagfull = "@fde2";
                            } else {
                                BOOT_APPVECTOR_ADDR = 0xFBDC;
                                BOOT_APPVECTOR_tag = "@fb";
                                BOOT_APPVECTOR_tagfull = "@fbdc";
                            }
                        }
                        data_type = 0;
                        data_addr = 0;
                        continue;
                    }
                    
                    if(line.contains(BOOT_APPVECTOR_tag)) {
                        data_type = 1;
                        data_cnt = 0;
                        data_addr = Integer.parseInt(line.substring(1, line.length()), 16) - BOOT_APPVECTOR_ADDR;
                        if(line.contains(BOOT_APPVECTOR_tagfull)) {
                            break;
                        } else {
                            continue;
                        }
                    }
                    
                    byte[] dat = ComFn.hexStr2Byte(line);
                    for(int n=0; n<dat.length; n++) {
                        int index = data_cnt+n+data_addr;
                        if(index < buf_to_flash.length) {
                            buf_to_flash[index] = dat[n];
                        }
                    }
                    if(1 == data_type) {
                        data_addr += dat.length;
                    }
                    //System.out.println(ComFn.bytesToHexString(dat, dat.length));
                    if(0 == data_type) {
                        data_cnt += dat.length;
                        if(data_cnt >= dfu_buf_len) {
                            break;
                        }
                    } else {
                        prog_addr = BOOT_APPVECTOR_ADDR-BOOT_APP_ADDR;
                    }
                }
                //-----------------------------------------------------------------------//
                al_dfu_data.add(buf_to_flash);
                al_dfu_addr.add(prog_addr);
                prog_addr += dfu_buf_len;
                
                if(percent < (prog_addr*100)/file_len) {
                    percent = (int) ((prog_addr*100)/file_len);
                    if(percent > 100) {
                        percent = 100;
                    }
                    dfu_inf.setText(dfu_text_inf + String.format("DFU data release: %d%% Done.\n", percent));
                }
                //-----------------------------------------------------------------------//
            }
            dfu_text_inf += "DFU data release: 100% Done!\n";
            dfu_inf.setText(dfu_text_inf);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            dt_update_en = false;
        }
        //---------------------------------------------------------------------------------//
        
        return dfu_text_inf;
    }
    /*********************************************************************************************/
}