whyclj
2020-08-29 166f2f34ffed7ee9562eff02482ac6a3f7168bcd
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
package com.dev.btse.comm;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
import com.battmonitor.base.Com;
import com.battmonitor.sql.MysqlConnPool;
import com.dev.btse.data.Ecb_Aes;
import com.dev.btse.data.FBS9100_Cmd;
import com.dev.btse.data.FBS9100_ComBase;
import com.dev.btse.data.FBS9100_ComBuf;
import com.dev.btse.data.FBS9100_Crc16;
 
public class FBS9100S_DFU implements Cloneable
{
    private final int BYTE_LEN = 518;
    private final int BYTE_LEN_WriteAck = 6;
    private int DFU_BUF_LEN = 256;
    
    private MysqlConnPool S_con_pool = null;
    private int S_dev_id = 0;
    
    public int op_cmd = 0;
    public int test_cmd = 0;
    
    public int DFU_DataBlockNum_Start = 0;
    public boolean DFU_EN = false;
    public int DFU_CommBuf_LEN = 256;
    public int DFU_WR_State = FBS9100S_DFU_SQL.DFU_STATE_CHECK_OK;
    public String DFU_FileName = "";
    public int DFU_FileByteLen = 0;
    
    public int DataBlockNum = 0;
    public int DataLen = 0;
    public byte[] DataDfu = new byte[DFU_BUF_LEN];
    public int CRC = 0;
    private Ecb_Aes my_aes = new Ecb_Aes();
    //private ByteBuffer FBS9100TxBuffer = ByteBuffer.allocate(1048);
    private ByteBuffer FBS9100RxBuffer = ByteBuffer.allocate(1500);
    
    public FBS9100S_DFU(MysqlConnPool con_pool, int dev_id) {
        S_con_pool = con_pool;
        S_dev_id = dev_id;
    }
    
    public FBS9100S_DFU clone()
    {
        FBS9100S_DFU obj = null;  
        try
        {
            obj = (FBS9100S_DFU)super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            e.printStackTrace();
        }
        return obj;
    }
    
    public void clear()
    {
        CRC = 0;
    }
    
    public boolean putWriteAckByteBuffer(final ByteBuffer bf)
    {
        if(bf.limit() < BYTE_LEN_WriteAck)
            return false;
        
        ByteBuffer tmpbuf = bf;
        int crc0 = tmpbuf.getShort(BYTE_LEN_WriteAck-2) & 0xFFFF;
        int crc1 = FBS9100_Crc16.CalCRC16(tmpbuf, BYTE_LEN_WriteAck-2);
        if(crc0 != crc1) {
            return false;
        }
        
        tmpbuf.position(0);
        DataBlockNum = (tmpbuf.getShort()&0xFFFF);
        DataLen = (tmpbuf.getShort()&0xFFFF);
        
        tmpbuf.compact();
        tmpbuf.flip();
        
        return true;
    }
    
    public boolean putReadAckByteBuffer(final ByteBuffer bf)
    {
        int crc_pos = this.DFU_CommBuf_LEN+BYTE_LEN_WriteAck;
        if(bf.limit() < crc_pos) {
            return false;
        }
        
        ByteBuffer tmpbuf = bf;
        int crc0 = tmpbuf.getShort(crc_pos-2) & 0xFFFF;
        int crc1 = FBS9100_Crc16.CalCRC16(tmpbuf, crc_pos-2);
        if(crc0 != crc1) {
            return false;
        }
        
        tmpbuf.position(0);
        DataBlockNum = (tmpbuf.getShort()&0xFFFF);
        DataLen = (tmpbuf.getShort()&0xFFFF);
        
        tmpbuf.compact();
        tmpbuf.flip();
        
        return true;
    }
    
    public boolean checkDfuWriteAckBuf(ByteBuffer ack_bf) {
        boolean res = false;
        
        FBS9100_Cmd m_cmd = new FBS9100_Cmd();
        if(true == m_cmd.putByteBuffer(ack_bf)) {
            if(true == putWriteAckByteBuffer(ack_bf)) {
                res = true;
            }
        }
        
        return res;
    }
    
    public boolean checkDfuReadAckBuf(ByteBuffer ack_bf) {
        boolean res = false;
        
        FBS9100_Cmd m_cmd = new FBS9100_Cmd();
        if(true == m_cmd.putByteBuffer(ack_bf)) {
            if(true == putReadAckByteBuffer(ack_bf)) {
                res = true;
            }
        }
        
        return res;
    }
    
    public ByteBuffer getWriteByteBuffer(int num, byte[] dat, int len)
    {
        ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN);
        bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
        
        bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(num));
        bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(len));
        bytebuffer.put(dat);
        CRC = FBS9100_Crc16.CalCRC16(bytebuffer, bytebuffer.position());
        bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(CRC));
        
        bytebuffer.flip();
        
        return bytebuffer;
    }
    
    public ByteBuffer getReadByteBuffer(int num, int data_len)
    {
        ByteBuffer bytebuffer = ByteBuffer.allocate(128);
        bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
        
        //DataBlockIndex = num;
        //DataLen = data_len;
        bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(num));
        bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(data_len));
        CRC = FBS9100_Crc16.CalCRC16(bytebuffer, bytebuffer.position());
        bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(CRC));
        
        bytebuffer.flip();
        
        return bytebuffer;
    }
    
    public void sendFBS9100Data(Socket socket_t, int cmd, ByteBuffer bbf_tx) throws IOException {
        ByteBuffer bf_t = FBS9100_ComBuf.makeFbs9100CommBuf(255, cmd, bbf_tx, true);
        byte[] plain_tx_t = new byte[bf_t.limit()];
        byte[] cipher_tx_t = new byte[bf_t.limit()];
        bf_t.get(plain_tx_t);
        my_aes.ecb_encrypt(plain_tx_t, cipher_tx_t, plain_tx_t.length);
        //System.out.println(ComFn.bytesToHexString(cipher_tx_t, cipher_tx_t.length));
        socket_t.setSoTimeout(3000);
        try {
            InputStream in = socket_t.getInputStream();
            OutputStream out = socket_t.getOutputStream();
            out.write(cipher_tx_t);
            out.flush();
            //****************************************************//
            FBS9100RxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            FBS9100RxBuffer.clear();
            byte[] rx_buf_t = new byte[1024];
            while(true) {
                int rx_len_t = in.read(rx_buf_t);
                if((FBS9100RxBuffer.position()+rx_len_t) 
                        < (FBS9100RxBuffer.capacity()-1)) {
                    FBS9100RxBuffer.put(rx_buf_t, 0, rx_len_t);
                }
                if(FBS9100RxBuffer.position() > 8) {
                    break;
                }
            }
            FBS9100RxBuffer.flip();
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }
    
    public boolean check_If_DFU_Is_Eanbled() {
        boolean res_t = false;
        
        FBS9100S_DFU_SQL.queryFbs9100S_DFU_StateBydev_id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
        if(false == this.DFU_EN) {
            res_t = false;
        } else {
            res_t = true;
            
            if((FBS9100S_DFU_SQL.DFU_STATE_READ != this.DFU_WR_State) 
                    && (FBS9100S_DFU_SQL.DFU_STATE_WRITE != this.DFU_WR_State)
                    && (FBS9100S_DFU_SQL.DFU_STATE_CHECK_OK != this.DFU_WR_State)) {
                System.out.println("DFU_WR_State must is DFU_STATE_READ or DFU_STATE_WRITE " + Com.getNowTimeWithAt());
                res_t = false;
            }
            if(0 == this.DFU_DataBlockNum_Start) {
                System.out.println("DFU_DataBlockNum_Start must bigger then Zero " + Com.getNowTimeWithAt());
                res_t = false;
            }
            
            File f = new File(this.DFU_FileName);
            if(false == f.exists()) {
                res_t = false;
                System.out.println("DFU_File_Not Exist " + Com.getNowTimeWithAt());
            }
        }
        
        return res_t;
    }
    
    public boolean runDFU(Socket socket_t) {
        //--------------------------- BOOTLOADER_CMD_WRITE-----------------------------//
        String dfu_text_inf = String.format("DevId:%d DFU Start " + Com.getNowTimeWithAt(), this.S_dev_id);
        System.out.println(dfu_text_inf);
        //dt_show_msg.setText(dfu_text_inf);
        boolean prog_ok = false;
        FileInputStream fis = null;
        
        try {
            File f = new File(this.DFU_FileName);
            this.DFU_FileByteLen = (int) f.length();
            
            this.DFU_BUF_LEN = this.DFU_CommBuf_LEN;
            if(this.DFU_BUF_LEN > 512) {
                this.DFU_BUF_LEN = 512;
            }
            if(this.DFU_BUF_LEN < 0) {
                this.DFU_BUF_LEN = 256;
            }
            
            byte[] buf_to_flash = new byte[this.DFU_BUF_LEN];
            Thread.sleep(100);
            fis = new FileInputStream(f);
            int dfu_datablock_num = 0;
            //int percent = 0;
            while(true)
            {
                FBS9100S_DFU_SQL.queryFbs9100S_DFU_StateBydev_id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
                //====================================//
                if(false == this.DFU_EN) {
                    dfu_text_inf = String.format("DevId:%d DFU Manual Stop " + Com.getNowTimeWithAt(), this.S_dev_id);
                    System.out.println(dfu_text_inf);
                    //dt_show_msg.setText(dfu_text_inf);
                    break;
                }
                //====================================//
                if(FBS9100S_DFU_SQL.DFU_STATE_WRITE != this.DFU_WR_State) {
                    prog_ok = true;
                    dfu_text_inf = String.format("DevId:%d DFU Write State Is Already Done " + Com.getNowTimeWithAt(), this.S_dev_id);
                    System.out.println(dfu_text_inf);
                    //dt_show_msg.setText(dfu_text_inf);
                    break;
                }
                //====================================//
                int data_len_towrite = 0;
                while(dfu_datablock_num < this.DFU_DataBlockNum_Start)
                {
                    for(int n=0; n<buf_to_flash.length; n++) {
                        buf_to_flash[n] = (byte) 0xFF;
                    }
                    data_len_towrite = fis.read(buf_to_flash);
                    if(data_len_towrite <= 0) {
                        break;
                    } else {
                        dfu_datablock_num += 1;
                    }
                } 
                //====================================//
                if(data_len_towrite > 0) {
                    Thread.sleep(1);
                    for(int cnt_t=0; cnt_t<3; cnt_t++) {
                        sendFBS9100Data(socket_t, FBS9100_ComBase.CMD_FBS9100_WriteDFU, 
                                        this.getWriteByteBuffer(dfu_datablock_num, buf_to_flash, data_len_towrite));
                        /**/
                        //readMsg(FBS9100RxBuffer);
                        byte[] cipher_buf = new byte[FBS9100RxBuffer.limit()];
                        byte[] plain_buf = new byte[FBS9100RxBuffer.limit()];
                        FBS9100RxBuffer.get(cipher_buf);
                        my_aes.ecb_decrypt(cipher_buf, plain_buf, cipher_buf.length);
                        //System.out.println(ComFn.bytesToHexString(plain_buf, plain_buf.length));
                        FBS9100RxBuffer.position(0);
                        FBS9100RxBuffer.put(plain_buf);
                        FBS9100RxBuffer.flip();
                        if(true == this.checkDfuWriteAckBuf(FBS9100RxBuffer)) {
                            if(this.DataBlockNum == dfu_datablock_num) {
                                prog_ok = true;
                                break;
                            }
                        } else {
                            prog_ok = false;
                            Thread.sleep(500);
                        }
                    }
                    if(false == prog_ok) {
                        dfu_text_inf = String.format("DevId:%d DFU Write Error! " + Com.getNowTimeWithAt(), this.S_dev_id);
                        System.out.println(dfu_text_inf);
                        //dt_show_msg.setText(dfu_text_inf);
                        break;
                    } else {
                        this.DFU_DataBlockNum_Start += 1;
                        FBS9100S_DFU_SQL.updateFbs9100S_DFU_StatByDev_Id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
                    }
                    /*
                    int tran_len_t = dfu_datablock_num*this.DFU_BUF_LEN;
                    if(percent < (tran_len_t*100)/file_len) {
                        percent = (int) ((tran_len_t*100)/file_len);
                        if(percent > 100) {
                            percent = 100;
                        }
                        //System.out.println(String.format("DevId:%d DFU Write: %d%% Done", this.S_dev_id, percent));
                        //dt_show_msg.setText(String.format("DFU Write: %d%% Done.\n", percent));
                    }
                    */
                } else {
                    this.DFU_DataBlockNum_Start = 1;
                    this.DFU_WR_State = FBS9100S_DFU_SQL.DFU_STATE_READ;
                    FBS9100S_DFU_SQL.updateFbs9100S_DFU_StatByDev_Id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
                    
                    dfu_text_inf = String.format("DevId:%d DFU Write: %d%% Done", this.S_dev_id, 100);
                    //dt_show_msg.setText(dfu_text_inf);
                    System.out.println(dfu_text_inf);
                    break;
                }
            }
        } catch (IOException | InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        //*********************************************************************************//
        //*********************************************************************************//
        //*********************************************************************************//
        //*********************************************************************************//
        boolean check_ok = false;
        if((true == prog_ok) && (true == this.DFU_EN)) {
            FileInputStream fis_ck = null;
            try {
                File f_ck = new File(this.DFU_FileName);
                long file_len_ck = f_ck.length();
                byte[] buf_check = new byte[this.DFU_BUF_LEN];
                
                Thread.sleep(100);
                fis_ck = new FileInputStream(f_ck);
                int check_datablock_num = 0;
                int percent = 0;
                while(true)
                {
                    FBS9100S_DFU_SQL.queryFbs9100S_DFU_StateBydev_id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
                    //====================================//
                    if(false == this.DFU_EN) {
                        dfu_text_inf = String.format("DevId:%d DFU Check Manual Stop. " + Com.getNowTimeWithAt(), this.S_dev_id);
                        System.out.println(dfu_text_inf);
                        //dt_show_msg.setText(dfu_text_inf);
                        break;
                    }
                    if(FBS9100S_DFU_SQL.DFU_STATE_READ != this.DFU_WR_State) {
                        check_ok = true;
                        dfu_text_inf = String.format("DevId:%d DFU Read And Check State Is Already Done. " + Com.getNowTimeWithAt(), this.S_dev_id);
                        System.out.println(dfu_text_inf);
                        //dt_show_msg.setText(dfu_text_inf);
                        break;
                    }
                    //====================================//
                    int len_buf_check = 0;
                    while(check_datablock_num < this.DFU_DataBlockNum_Start)
                    {
                        for(int n=0; n<buf_check.length; n++) {
                            buf_check[n] = (byte) 0xFF;
                        }
                        len_buf_check = fis_ck.read(buf_check);
                        if(len_buf_check <= 0) {
                            break;
                        } else {
                            check_datablock_num += 1;
                        }
                    }
                    //====================================//
                    if(len_buf_check > 0) {
                        Thread.sleep(1);
                        for(int cnt_t=0; cnt_t<3; cnt_t++) {
                            sendFBS9100Data(socket_t, FBS9100_ComBase.CMD_FBS9100_ReadDFU, 
                                            this.getReadByteBuffer(check_datablock_num, len_buf_check));
                            //readMsg(FBS9100RxBuffer);
                            byte[] cipher_buf = new byte[FBS9100RxBuffer.limit()];
                            byte[] plain_buf = new byte[FBS9100RxBuffer.limit()];
                            FBS9100RxBuffer.get(cipher_buf);
                            my_aes.ecb_decrypt(cipher_buf, plain_buf, cipher_buf.length);
                            //System.out.println(ComFn.bytesToHexString(plain_buf, plain_buf.length));
                            FBS9100RxBuffer.position(0);
                            FBS9100RxBuffer.put(plain_buf);
                            FBS9100RxBuffer.flip();
                            if(true == this.checkDfuReadAckBuf(FBS9100RxBuffer)) {
                                if(this.DataBlockNum == check_datablock_num) {
                                    byte[] s_buf = new byte[len_buf_check];
                                    FBS9100RxBuffer.get(s_buf);
                                    check_ok = true;
                                    for(int cn=0; cn<len_buf_check; cn++) {
                                        if(buf_check[cn] != s_buf[cn]) {
                                            check_ok = false;
                                            break;
                                        }
                                    }
                                }
                            } else {
                                check_ok = false;
                                Thread.sleep(500);
                            }
                            if(true == check_ok) {
                                break;
                            }
                        }
                        if(false == check_ok) {
                            dfu_text_inf = String.format("DevId:%d DFU File Check Error! " + Com.getNowTimeWithAt(), this.S_dev_id);
                            System.out.println(dfu_text_inf);
                            //dt_show_msg.setText(dfu_text_inf);
                            break;
                        } else {
                            this.DFU_DataBlockNum_Start += 1;
                            FBS9100S_DFU_SQL.updateFbs9100S_DFU_StatByDev_Id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
                        }
                        
                        int tran_len_t = check_datablock_num*this.DFU_BUF_LEN;
                        if(percent < (tran_len_t*100)/file_len_ck) {
                            percent = (int) ((tran_len_t*100)/file_len_ck);
                            if(percent > 100) {
                                percent = 100;
                            }
                            //System.out.println(String.format("DevId:%d DFU File Check: %d%% Done", this.S_dev_id, percent));
                            //dt_show_msg.setText(String.format("DFU File Check: %d%% Done.\n", percent));
                        }
                    } else {
                        this.DFU_WR_State = FBS9100S_DFU_SQL.DFU_STATE_CHECK_OK;
                        FBS9100S_DFU_SQL.updateFbs9100S_DFU_StatByDev_Id(S_con_pool, S_dev_id, FBS9100S_DFU.this);
                        dfu_text_inf = String.format("DevId:%d DFU File Check: %d%% Done", this.S_dev_id, 100);
                        System.out.println(dfu_text_inf);
                        //dt_show_msg.setText(dfu_text_inf);
                        break;
                    }
                }
            } catch (IOException | InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                try {
                    fis_ck.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            //*****************************************************************************//
        }
        //*********************************************************************************//
        return check_ok;
        //*********************************************************************************//
    }
}
/***************************************************************************************
****************************** end of file (FBS_TestParam) *****************************
***************************************************************************************/