You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.0 KiB
97 lines
3.0 KiB
package com.iflytop.gd.system.models;
|
|
|
|
import com.iflytop.gd.system.utils.ByteArray;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
|
|
public class DataPacket {
|
|
public static final int PACKET_TYPE_OFFSET = 0;
|
|
public static final int CMDID_OFFSET = 1;
|
|
public static final int MODULE_ID_OFFSET = 3;
|
|
public static final int INDEX_OFFSET = 4;
|
|
public static final int DATA_LEN_OFFSET = 6;
|
|
public static final int DATA_OFFSET = 7;
|
|
|
|
public static final int PACKET_MIN_LEN = 8;//
|
|
|
|
public static final int CMD_OVERTIME = 1500;
|
|
|
|
|
|
public static final int PACKET_TYPE_CMD = 0xA0;
|
|
public static final int PACKET_TYPE_ACK = 0xA1;
|
|
public static final int PACKET_TYPE_ERROR_ACK = 0xA2;
|
|
public static final int PACKET_TYPE_EVENT = 0xA3;
|
|
|
|
byte[] raw;
|
|
|
|
public DataPacket(byte[] cmd) {
|
|
raw = new byte[cmd.length];
|
|
System.arraycopy(cmd, 0, raw, 0, cmd.length);
|
|
}
|
|
|
|
public String toByteString() {
|
|
return ByteArray.toByteString(raw);
|
|
}
|
|
|
|
static public DataPacket createPacket(Integer moduleId, int packetType, Integer cmdId, Integer[] params) {
|
|
int bufferSize = DataPacket.PACKET_MIN_LEN + 4 * params.length;
|
|
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
|
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
|
buffer.put((byte) (packetType & 0xff)); // packetType
|
|
buffer.putShort((short) (cmdId & 0xffff)); // cmdid
|
|
buffer.put((byte) (moduleId & 0xFF)); // moduleId
|
|
buffer.put((byte) 0x4C); // index
|
|
buffer.put((byte) 0x00); // index
|
|
buffer.put((byte) (params.length * 4)); // datalen
|
|
for (int value : params) {
|
|
buffer.putInt(value);
|
|
}
|
|
// int8_t checksum;
|
|
int checksum = 0;
|
|
for (int i = 0; i < bufferSize - 1; i++) {
|
|
checksum += buffer.get(i);
|
|
}
|
|
buffer.put((byte) checksum);
|
|
return new DataPacket(buffer.array());
|
|
}
|
|
|
|
|
|
public static DataPacket createCommandDataPacket(Integer moduleId, Integer cmdId, Integer... params) {
|
|
return createPacket(moduleId, PACKET_TYPE_CMD, cmdId, params);
|
|
}
|
|
|
|
public int getPacketIndex() {
|
|
return ByteArray.readU16bit(raw, INDEX_OFFSET);
|
|
}
|
|
|
|
public void setPacketIndex(int packetIndex) {
|
|
ByteArray.setU16bit(raw, INDEX_OFFSET, packetIndex);
|
|
int checkSum = computeCheckSum();
|
|
ByteArray.setU8(raw, raw.length - 1, checkSum);
|
|
}
|
|
|
|
public int computeCheckSum() {
|
|
int checkcode = 0;
|
|
for (int i = 0; i < raw.length - 1; i++) {
|
|
checkcode += raw[i];
|
|
}
|
|
return checkcode & 0xFF;
|
|
}
|
|
|
|
public int getCmdId() {
|
|
return ByteArray.readU16bit(raw, CMDID_OFFSET);
|
|
}
|
|
|
|
public int getPacketType() {
|
|
return ByteArray.readU8bit(raw, PACKET_TYPE_OFFSET);
|
|
}
|
|
|
|
public int getModuleId() {
|
|
return ByteArray.readU8bit(raw, MODULE_ID_OFFSET);
|
|
}
|
|
|
|
public int getDataLen() {
|
|
return ByteArray.readU8bit(raw, DATA_LEN_OFFSET);
|
|
}
|
|
}
|