Commit 09a1f7d6 by tiger

增加crc校验

1 parent cc8203c8
......@@ -209,36 +209,30 @@ public class Ocpp16Helper {
}
}
// 读取数据
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
// 释放 ByteBuf
byteBuf.release();
// 自动补充len字段和crc字段
if (d instanceof TcpBase) {
TcpBase b = ((TcpBase) d);
// TODO seq字段处理
if (b.getSeq() == 0) {
short seq = generateSeq("Test");
b.setSeq(seq);
byteBuf.setShort(2, seq);
}
// 补充长度字段
byte len = (byte) (byteBuf.readableBytes() - 4);
byteBuf.setByte(1, len);
byte len = (byte) (data.length - 4);
b.setLen(len);
ByteUtils.putByte(data, 1, len);
// 补充crc
byte[] data = byteBuf.slice(2, byteBuf.readableBytes() - 4).array();
short crc = CrcUtils.calculateCRC(data);
byteBuf.setShort(byteBuf.readableBytes() - 2, crc);
byte[] data1 = ByteUtils.getBytes(data, 2, data.length - 4);
short crc = CrcUtils.calculateCRC(data1);
b.setCrc(crc);
ByteUtils.putShort(data, data.length - 2, crc);
// log.info("【{}】build crc:{}", d.getClass().getSimpleName(), crc);
}
// 读取数据
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
// 释放 ByteBuf
byteBuf.release();
return data;
}
......
......@@ -98,11 +98,14 @@ public class TcpClient {
@SuppressWarnings({ "rawtypes" })
public <T extends TcpBase> void sendSync(T data) {
String ip = getIp(future.channel());
// TODO seq字段处理
data.setSeq(Ocpp16Helper.generateSeq(ip));
byte[] bs = Ocpp16Helper.build(data);
sendSync(bs);
String ip = getIp(future.channel());
log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs));
log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
}
......
......@@ -54,9 +54,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes));
log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), up);
// if (!Ocpp16Helper.verifyCrc(up)) {
// log.warn("【警告】crc校验不通过:{}", up);
// }
if (!Ocpp16Helper.verifyCrc(up)) {
log.warn("【警告】crc校验不通过:{}", up);
}
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null && up instanceof TcpRsp) {
......
......@@ -90,6 +90,9 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
TcpBase rsp = handler.handle(ctx.channel(), up);
if (rsp != null) {
// seq字段处理
rsp.setSeq(up.getSeq());
byte[] rspBytes = Ocpp16Helper.build(rsp);
ctx.channel().writeAndFlush(rspBytes);
......
package cn.com.glsx.charging.util;
import java.util.zip.CRC32;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
......@@ -37,48 +35,64 @@ public class CrcUtils {
return (short) hashCode.asInt();
}
public static short calculateCRC2(byte[] data) {
CRC32 crc32 = new CRC32();
crc32.update(data);
return (short) crc32.getValue();
}
// public static short calculateCRC2(byte[] data) {
// CRC32 crc32 = new CRC32();
// crc32.update(data);
// return (short) crc32.getValue();
// }
public static short calculateCRC3(byte[] data) {
int polynomial = 0x180D; // CRC 多项式
public static short calculateCRC(byte[] data) {
int crc = 0xFFFF; // 初始值为 0xFFFF
for (byte b : data) {
crc ^= b & 0xFF;
for (int i = 0; i < 8; i++) {
if ((crc & 0x0001) != 0) {
crc = (crc >> 1) ^ polynomial;
} else {
crc >>= 1;
}
}
int index = ((crc >> 8) ^ (b & 0xFF)) & 0xFF;
crc = ((crc << 8) ^ (gabyCRCHi[index] << 8) ^ (gabyCRCLo[index] & 0xFF)) & 0xFFFF;
}
return (short) crc;
}
public static short calculateCRC(byte[] data) {
int crc = 0xFFFF; // 初始值为 0xFFFF
public static short calculateCRC2(byte[] data) {
int crc = 0xFFFF;
for (byte b : data) {
int index = ((crc >> 8) ^ (b & 0xFF)) & 0xFF;
crc = ((crc << 8) ^ (gabyCRCHi[index] << 8) ^ (gabyCRCLo[index] & 0xFF)) & 0xFFFF;
crc = ((crc >> 8) ^ gabyCRCHi[(crc ^ b) & 0xFF]) & 0xFFFF;
crc = ((crc >> 8) ^ gabyCRCLo[(crc & 0xFF)]) & 0xFFFF;
}
return (short) crc;
}
public static void main(String[] args) {
byte[] data = { 0x01, 0x02, 0x03 }; // Example data
int crc = calculateCRC(data);
System.out.printf("CRC: 0x%04X\n", crc);
private static final int POLYNOMIAL = 0x180D;
short c = (short) 0xDA4C;
System.out.println(c);
public static int calculateCRC3(byte[] data) {
int crc = 0xFFFF;
for (byte b : data) {
crc ^= b & 0xFF;
for (int i = 0; i < 8; i++) {
if ((crc & 0x0001) == 0x0001) {
crc = (crc >> 1) ^ POLYNOMIAL;
} else {
crc >>= 1;
}
}
}
return crc & 0xFFFF;
}
public static void main(String[] args) {
String hex = "68120003005655031412782305C832290D070B174270";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
System.out.println("expect:" + ByteUtils.getShort(bytes, bytes.length - 2));
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual:" + CrcUtils.calculateCRC(data));
System.out.println("actual1:" + CrcUtils.calculateCRC1(data));
System.out.println("actual2:" + CrcUtils.calculateCRC2(data));
System.out.println("actual3:" + CrcUtils.calculateCRC3(data));
}
......
package cn.com.glsx.charging.gbt.ocpp16;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.HeartbeatReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.StartMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginRsp;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.CrcUtils;
/**
* 模拟充电桩端,连接云快充平台的测试服务器
......@@ -23,83 +16,6 @@ import cn.com.glsx.charging.util.CrcUtils;
* @date 2023年11月6日 上午9:49:10
*/
public class ClientSimulateTest {
@Test
public void Test() throws Exception {
// 6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622
Ocpp16Helper.scan();
// 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
String hex = "6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
StartMachineReq rsp = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + rsp.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
System.out.println("actual1: " + CrcUtils.calculateCRC1(data));
System.out.println("actual2: " + CrcUtils.calculateCRC2(data));
System.out.println("actual3: " + CrcUtils.calculateCRC3(data));
}
@Test
public void test_LoginReq_crc() throws Exception {
Ocpp16Helper.scan();
String hex = "6822000000015503141278230500020F56342E312E353000010101010101010101010104675A";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginReq protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
System.out.println(protocol);
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
System.out.println("actual1: " + CrcUtils.calculateCRC1(data));
System.out.println("actual2: " + CrcUtils.calculateCRC2(data));
System.out.println("actual3: " + CrcUtils.calculateCRC3(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
}
@Test
public void testCrc() {
Ocpp16Helper.scan();
// 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
String hex = "680C000000025503141278230500DA4C";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp rsp = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + rsp.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
Assert.assertEquals(rsp.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_LoginRsp_Crc() {
Ocpp16Helper.scan();
// 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
String hex = "680C000100025503141278230500DEB0";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp rsp = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + rsp.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
Assert.assertEquals(rsp.getCrc(), CrcUtils.calculateCRC(data));
}
public static void main(String[] args) throws Exception {
Ocpp16Helper.scan();
......
package cn.com.glsx.charging.gbt.ocpp16;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.TimeNotify;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.CrcUtils;
public class CrcTest {
@BeforeAll
public static void test_scan() {
Ocpp16Helper.scan();
}
@Test
public void test_LoginReq_crc() throws Exception {
String hex = "6822000100015503141278230500020AFFFFFF5000000000000000000000000000000000B9BA";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginReq protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_LoginRsp_crc() {
String hex = "680C000100025503141278230500DEB0";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_HeartbeatReq_crc() {
String hex = "680D000200035503141278230500006599";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
HeartbeatReq protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_HeartbeatRsp_crc() {
String hex = "680D000300045503141278230500002980";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
HeartbeatRsp protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_TimeNotify_crc() {
String hex = "68120002005655031412782305F055020E070B178676";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
TimeNotify protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!