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) *****************************
|
***************************************************************************************/
|