whyclj
2019-06-24 66db8d445a53a8ed8410f7196f5c65de7a29bce7
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
package com.dev.btse.data;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
 
import com.battmonitor.base.Crc16;
 
public class TmpSensor_ComBuf {
        TmpSensor_Cmd m_CMD = new TmpSensor_Cmd();
        
        public TmpSensor_Cmd getTmpSensorCmd()
        {
            return this.m_CMD;
        }
        
        public boolean putByteBuffer(final ByteBuffer bf)
        {
            if(false == m_CMD.putByteBuffer(bf))
                return false;
            
            return true;
        }
        
        public static ByteBuffer makeTmpSensorCommBuf(int addr, int cmd,int reg_index, ByteBuffer bf, boolean aes_en)
        {
            TmpSensor_Cmd m_CMD = new TmpSensor_Cmd();
            ByteBuffer buffer = ByteBuffer.allocate(m_CMD.BYTE_LEN + bf.limit() + 32 );
            buffer.order(ByteOrder.BIG_ENDIAN);            //ÉèÖÃByteBufferµÄ×Ö½ÚÐò
            
            m_CMD.makeCmd(addr, cmd,reg_index,getByteCountByReg(reg_index));
            buffer.put(m_CMD.getByteBuffer());
            buffer.put(bf);
            
            if(true == aes_en) {
                Random rd = new Random();
                if(0 == (buffer.position()%Ecb_Aes.AES_BLOCK_SIZE)) {
                    for(int n=0; n<(Ecb_Aes.AES_BLOCK_SIZE/4); n++) {
                        buffer.putInt(rd.nextInt());
                    }
                } else {
                    int cnt = Ecb_Aes.AES_BLOCK_SIZE - (buffer.position()%Ecb_Aes.AES_BLOCK_SIZE);
                    for(int n=0; n<cnt; n++) {
                        buffer.put((byte) rd.nextInt());
                    }
                }
            }
            int CRC = Crc16.CalCRC16(buffer, buffer.position());
            buffer.putShort(FBS9100_ComBase.changeIntToShort(CRC));
            buffer.flip();
            
            return buffer;
        }
        
        
        /**
         * ¸ù¾Ý¼Ä´æÆ÷Æ«ÒÆµØÖ·»ñÈ¡¼Ä´æÆ÷¸öÊý
         * @return
         */
        public static int getByteCountByReg(int reg_index) {
            int bytecount = 0;
            switch(reg_index) {
                case TmpSensor_ComBase.CMD_GETTMPSENSORSTATE: 
                    return TmpSensorState.REG_BYTECOUNT;
            }
            return bytecount;
        }
    /***************************************************************************************
    ****************************** end of file (FBS_ComBuf) ********************************
    ***************************************************************************************/
}