Commit a9f13c7c by tiger

忽略crc校验

1 parent 823bbc50
...@@ -11,8 +11,6 @@ import com.google.common.collect.Maps; ...@@ -11,8 +11,6 @@ import com.google.common.collect.Maps;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpBase; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpBase;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
import cn.com.glsx.charging.util.ByteUtils; import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.ClassUtil; import cn.com.glsx.charging.util.ClassUtil;
import cn.com.glsx.charging.util.ClassUtils; import cn.com.glsx.charging.util.ClassUtils;
...@@ -79,19 +77,10 @@ public class Ocpp16Helper { ...@@ -79,19 +77,10 @@ public class Ocpp16Helper {
/** /**
* TcpBase在build时自动填充 * TcpBase在build时自动填充
*/ */
@Deprecated
private static <D extends TcpData, P extends TcpBase<D>> void fillLen(P b) {
byte[] bs = build(b);
b.setLen((byte) (bs.length - 4));
}
/**
* TcpBase在build时自动填充
*/
private static <D extends TcpData, P extends TcpBase<D>> short calculateCrc(P b) { private static <D extends TcpData, P extends TcpBase<D>> short calculateCrc(P b) {
byte[] bs = build(b); byte[] bs = build(b);
byte[] data = ByteUtils.getBytes(bs, 2, bs.length - 4); byte[] data = ByteUtils.getBytes(bs, 2, bs.length - 4);
short crc = (short) CrcUtils.calculateCRC(data); short crc = CrcUtils.calculateCRC(data);
return crc; return crc;
} }
...@@ -99,7 +88,7 @@ public class Ocpp16Helper { ...@@ -99,7 +88,7 @@ public class Ocpp16Helper {
short crc = calculateCrc(b); short crc = calculateCrc(b);
boolean rs = crc == b.getCrc(); boolean rs = crc == b.getCrc();
if (!rs) { if (!rs) {
log.warn("【crc校验】expect:{} actual:{}", crc, b.getCrc()); log.warn("【crc校验】{} expect:{} actual:{}", b.getClass().getSimpleName(), crc, b.getCrc());
} }
return rs; return rs;
} }
...@@ -237,9 +226,10 @@ public class Ocpp16Helper { ...@@ -237,9 +226,10 @@ public class Ocpp16Helper {
// 补充crc // 补充crc
byte[] data = byteBuf.slice(2, byteBuf.readableBytes() - 4).array(); byte[] data = byteBuf.slice(2, byteBuf.readableBytes() - 4).array();
short crc = (short) CrcUtils.calculateCRC(data); short crc = CrcUtils.calculateCRC(data);
byteBuf.setShort(byteBuf.readableBytes() - 2, crc); byteBuf.setShort(byteBuf.readableBytes() - 2, crc);
b.setCrc(crc); b.setCrc(crc);
// log.info("【{}】build crc:{}", d.getClass().getSimpleName(), crc);
} }
// 读取数据 // 读取数据
...@@ -325,21 +315,4 @@ public class Ocpp16Helper { ...@@ -325,21 +315,4 @@ public class Ocpp16Helper {
return p; return p;
} }
public static void main(String[] args) {
LoginReq req = new LoginReq();
req.setData(new LoginReqData());
fillLen(req);
req.setCrc(calculateCrc(req));
verifyCrc(req);
System.out.println(req);
System.out.println(build(req).length);
scan();
byte[] bs = build(req);
TcpBase<TcpData> base = parse(bs);
System.out.println(base);
}
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base; package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.req.StartMachineReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.resp.StartMachineRespHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.ModelChargingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.ModelValReqHandler;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.req.StartMachineReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.resp.StartMachineRespHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.notify.TimeNotifyHandler; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.notify.TimeNotifyHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.HeartbeatReqHandler; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.HeartbeatReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.LoginReqHandler; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.LoginReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.ModelChargingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.ModelValReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.rsp.LoginRspHandler; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.rsp.LoginRspHandler;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
...@@ -53,13 +53,19 @@ public final class TcpMapper { ...@@ -53,13 +53,19 @@ public final class TcpMapper {
public static TcpMapper CMD_NOTIFY_TIME = regist(0x56, "对时设置", TimeNotifyHandler.class); public static TcpMapper CMD_NOTIFY_TIME = regist(0x56, "对时设置", TimeNotifyHandler.class);
//运营交互帧类型码数据定义 0x31~0x47 // 运营交互帧类型码数据定义 0x31~0x47
public static TcpMapper FTCD_RSP_REMOTE_START_CHARGING = regist(0x33, "远程启动充电命令回复", StartMachineRespHandler.class); public static TcpMapper FTCD_RSP_REMOTE_START_CHARGING = regist(0x33, "远程启动充电命令回复", StartMachineRespHandler.class);
public static TcpMapper FTCD_REQ_REMOTE_CONTROL_STARTUP = regist(0x34, "运营平台远程控制启机", StartMachineReqHandler.class); public static TcpMapper FTCD_REQ_REMOTE_CONTROL_STARTUP = regist(0x34, "运营平台远程控制启机", StartMachineReqHandler.class);
public static TcpMapper FTCD_RSP_REMOTE_SHUTDOWN = regist(0x35, "远程停机命令回复", null); public static TcpMapper FTCD_RSP_REMOTE_SHUTDOWN = regist(0x35, "远程停机命令回复", null);
public static TcpMapper FTCD_REQ_REMOTE_SHUTDOWN = regist(0x36, "运营平台远程停机", null); public static TcpMapper FTCD_REQ_REMOTE_SHUTDOWN = regist(0x36, "运营平台远程停机", null);
public static TcpMapper FTCD_REQ_TRANSACTION_RECORDS = regist(0x3B, "交易记录", null); public static TcpMapper FTCD_REQ_TRANSACTION_RECORDS = regist(0x3B, "交易记录", null);
public static TcpMapper FTCD_RSP_TRANSACTION_CONFIRMATION = regist(0x40, "交易记录确认", null); public static TcpMapper FTCD_RSP_TRANSACTION_CONFIRMATION = regist(0x40, "交易记录确认", null);
public static TcpMapper fromType(byte type) { public static TcpMapper fromType(byte type) {
TcpMapper c = mappers.get(type); TcpMapper c = mappers.get(type);
if (c == null) { if (c == null) {
......
...@@ -54,9 +54,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -54,9 +54,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes)); log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes));
log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), up); log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), up);
if (!Ocpp16Helper.verifyCrc(up)) { // if (!Ocpp16Helper.verifyCrc(up)) {
log.warn("【警告】crc校验不通过:{}", up); // log.warn("【警告】crc校验不通过:{}", up);
} // }
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass(); Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null && up instanceof TcpRsp) { if (handlerClass == null && up instanceof TcpRsp) {
......
...@@ -2,16 +2,19 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive; ...@@ -2,16 +2,19 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.StartMachineReq;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.CrcUtils;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
/** /**
* @Description 当用户通过远程启动充电时,发送本命令
* 报文示例: * 报文示例:
* 68 (起始标志) 30 (数据长度) 007C (序列号域) 00 (加密标志) 34 (类型) * 68 (起始标志) 30 (数据长度) 007C (序列号域) 00 (加密标志) 34(类型)
* 55031412782305012018061914444680(交易流水号)55031412782305(桩编码)01(枪号:1 * 55031412782305012018061914444680(交易流水号)55031412782305(桩编码)01(枪号:1枪)
* 枪)0000001000000573(逻辑卡号:1000000573)00000000D14B0A54(物理卡号:D14B0A54) * 0000001000000573(逻辑卡号:1000000573)00000000D14B0A54(物理卡号:D14B0A54)A0860100(账户余额:1000.00)4622
* A0860100(账户余额:1000.00)4622 *
* @Description 当用户通过远程启动充电时,发送本命令
* @Author yelq * @Author yelq
* @Date 2023/11/6-19:16 * @Date 2023/11/6-19:16
* @Version 1.0 * @Version 1.0
...@@ -67,38 +70,23 @@ public class StartMachineReqData extends TcpData { ...@@ -67,38 +70,23 @@ public class StartMachineReqData extends TcpData {
*/ */
private byte[] accountBalance = new byte[4]; private byte[] accountBalance = new byte[4];
/** public static void main(String[] args) {
* 将字节数组转换为二进制字符串 Ocpp16Helper.scan();
* @param bytes 字节数组
* @return 二进制字符串
*/
public static String bytesToBinaryString(byte[] bytes) {
StringBuilder binaryString = new StringBuilder();
for (byte b : bytes) {
for (int i = 7; i >= 0; i--) {
binaryString.append((b >> i) & 1);
}
}
return binaryString.toString();
}
/** // 68 (起始标志) 30 (数据长度) 007C (序列号域) 00 (加密标志) 34
* 将字节数组转换为十进制数 // (类型)55031412782305012018061914444680(交易流水号)55031412782305(桩编码)01(枪号:1枪)0000001000000573(逻辑卡号:1000000573)00000000D14B0A54(物理卡号:D14B0A54)A0860100(账户余额:1000.00)4622
* @param bytes 字节数组 String hex = "6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622";
* @return 十进制数 System.out.println(hex);
*/ byte[] bytes = ByteUtils.fromHex(hex);
public static long bytesToDecimal(byte[] bytes) { StartMachineReq protocol = Ocpp16Helper.parse(bytes);
long decimal = 0; System.out.println("expect: " + protocol.getCrc());
for (int i = 0; i < bytes.length; i++) {
decimal = (decimal << 8) | (bytes[i] & 0xFF); System.out.println(protocol);
} byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
return decimal; System.out.println("actual: " + CrcUtils.calculateCRC(data));
}
//0000100011001011 bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
public static void main(String[] args) {
byte[] bytes1 = Ocpp16Helper.binToBytes("0000100011001011");
System.out.println(bytesToDecimal(bytes1));
System.out.println(bytesToBinaryString(bytes1));
} }
} }
...@@ -2,16 +2,15 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login; ...@@ -2,16 +2,15 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.CrcUtils;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
/** /**
* 68(起始标志)22(数据长度)0000(序列号域)00(加密标志)01(类型)55031412782305
* (桩编码)00(桩类型)02(充电枪数量)0F(通信协议版本:V1.5)56342E312E353000(程
* 序版本:v4.1.50)01(网络链接类型)01010101010101010101(sim 卡)04(运营商)675A(帧
* 校验域
*
* @author kome * @author kome
* @date 2023年11月1日 下午6:00:39 * @date 2023年11月1日 下午6:00:39
*/ */
...@@ -68,4 +67,21 @@ public class LoginReqData extends TcpData { ...@@ -68,4 +67,21 @@ public class LoginReqData extends TcpData {
*/ */
private byte operator; private byte operator;
public static void main(String[] args) {
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));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
}
} }
package cn.com.glsx.charging.gbt.ocpp16; package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.StartMachineReq;
import org.junit.Assert; import org.junit.Assert;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -9,6 +8,7 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; ...@@ -9,6 +8,7 @@ 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.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.HeartbeatReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginReqData; 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.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq; 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.LoginRsp;
...@@ -26,7 +26,7 @@ public class ClientSimulateTest { ...@@ -26,7 +26,7 @@ public class ClientSimulateTest {
@Test @Test
public void Test() throws Exception { public void Test() throws Exception {
//6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622 // 6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622
Ocpp16Helper.scan(); Ocpp16Helper.scan();
...@@ -45,6 +45,28 @@ public class ClientSimulateTest { ...@@ -45,6 +45,28 @@ public class ClientSimulateTest {
} }
@Test @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() { public void testCrc() {
Ocpp16Helper.scan(); Ocpp16Helper.scan();
...@@ -61,6 +83,23 @@ public class ClientSimulateTest { ...@@ -61,6 +83,23 @@ public class ClientSimulateTest {
Assert.assertEquals(rsp.getCrc(), 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 { public static void main(String[] args) throws Exception {
Ocpp16Helper.scan(); Ocpp16Helper.scan();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!