package safeluck.drive.evaluation.platformMessage;
|
|
import android.util.Log;
|
|
import com.anyun.exam.lib.MyLog;
|
import com.anyun.im_lib.util.ByteUtil;
|
|
import safeluck.drive.evaluation.util.Utils;
|
|
/**
|
* MyApplication2
|
* Created by lzw on 2019/12/17. 15:39:23
|
* 邮箱:632393724@qq.com
|
* All Rights Saved! Chongqing AnYun Tech co. LTD
|
*/
|
public abstract class DriveExamProtocol {
|
|
private static final String TAG = "DriveExamProtocol";
|
/***========================
|
消息结构表
|
标识位 消息头 消息体 校验码 标识位
|
0x7E 0x7E
|
======================**/
|
// 标识位(字节流开始)
|
private byte MESSAGE_HEAD = 0x7e;
|
//标识位(字节流结束)
|
private byte MESSAGE_TAIL = 0x7e;
|
|
//校验码指从消息头开始,同后一字节异或,直到校验码前一个字节,占用一个字节
|
//校验码 先暂时写死 todo
|
private byte checkCode = 0x78;
|
|
|
/***===========以下是消息头=============***/
|
//协议版本号 BYTE 235,固定
|
private short protocol_version = 235;
|
//消息ID
|
private short msg_id;
|
|
|
/** 消息体属性格式结构表
|
15 14 13 | 12 11 10 | 9 8 7 6 5 4 3 2 1 0
|
保留 数据加密方式 消息体长度
|
**/
|
//消息体属性
|
private short msg_property =2;
|
|
/**
|
* 终端手机号 字符串长度必须为16
|
**/
|
private String phoneOnTerminal = "0008618513021245";
|
|
/**
|
*
|
* 13 消息流水号 WORD 按发送顺序从0开始循环累加
|
*
|
*/
|
public static short msg_serial_num=0;
|
|
//15 预留 BYTE 预留
|
private byte reserve = 0x00;
|
/***===========消息头结束=============***/
|
|
/**
|
* 消息体 需要子类实现
|
*/
|
protected abstract byte[] createMessageBody();
|
|
/**
|
* 构造函数
|
* @param msg_id 消息ID
|
*/
|
public DriveExamProtocol(short msg_id) {
|
this.msg_id = msg_id;
|
}
|
|
/**
|
* 消息转为byte数组 7E......7E
|
* @return
|
*/
|
public byte[] toBytes(){
|
byte[] desBytes = new byte[1+16+msgBodyLength()+1+1];
|
int pos = 0;
|
|
|
//标识位
|
desBytes[pos] = MESSAGE_HEAD;
|
pos++;
|
|
|
//协议版本号
|
byte[] protoVersion = ByteUtil.shortGetByte(protocol_version);
|
System.arraycopy(protoVersion,0,desBytes,pos,protoVersion.length);
|
pos +=protoVersion.length;
|
|
//消息ID
|
byte[] msgIdBytes = ByteUtil.shortGetBytes(msg_id);
|
System.arraycopy(msgIdBytes,0,desBytes,pos,msgIdBytes.length);
|
pos+=msgIdBytes.length;
|
|
//消息体属性 默认为消息体长度
|
msg_property = msgBodyLength();
|
byte[] msg_pro_bytes = ByteUtil.shortGetBytes(msg_property);
|
System.arraycopy(msg_pro_bytes,0,desBytes,pos,msg_pro_bytes.length);
|
pos+=msg_pro_bytes.length;
|
//终端手机号
|
byte[] phoneBytes = ByteUtil.str2Bcd(phoneOnTerminal);
|
System.arraycopy(phoneBytes,0,desBytes,pos,phoneBytes.length);
|
pos+=phoneBytes.length;
|
//消息流水号
|
byte[] msg_serialNum = ByteUtil.shortGetBytes(msg_serial_num++);
|
System.arraycopy(msg_serialNum,0,desBytes,pos,msg_serialNum.length);
|
pos+=msg_serialNum.length;
|
//保留byte
|
desBytes[pos] = reserve;
|
pos++;
|
|
//消息体
|
byte[] messageBodyBytes = createMessageBody();
|
System.arraycopy(messageBodyBytes,0,desBytes,pos,messageBodyBytes.length);
|
pos+=messageBodyBytes.length;
|
|
//校验码
|
checkCode = Utils.calCheckCode(ByteUtil.subArray(desBytes,1,pos-1));
|
desBytes[pos] = checkCode;
|
pos++;
|
//末尾结束标识位
|
desBytes[pos] = MESSAGE_TAIL;
|
|
MyLog.i(TAG, "原始包长度="+(pos+1));
|
MyLog.i(TAG, "原始包内容: "+ByteUtil.byte2HexStr(desBytes));
|
byte[] tranferbytes = Utils.transferMeaning(desBytes);
|
MyLog.i(TAG,"转义后的包内容:"+ByteUtil.byte2HexStr(tranferbytes));
|
return tranferbytes;
|
}
|
|
protected abstract short msgBodyLength();
|
|
}
|