use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class EipProtocolLogic method decodeWriteResponse.
private PlcResponse decodeWriteResponse(CipService p, PlcWriteRequest writeRequest) {
Map<String, PlcResponseCode> responses = new HashMap<>();
if (p instanceof CipWriteResponse) {
CipWriteResponse resp = (CipWriteResponse) p;
String fieldName = writeRequest.getFieldNames().iterator().next();
EipField field = (EipField) writeRequest.getField(fieldName);
responses.put(fieldName, decodeResponseCode(resp.getStatus()));
return new DefaultPlcWriteResponse(writeRequest, responses);
} else if (p instanceof MultipleServiceResponse) {
MultipleServiceResponse resp = (MultipleServiceResponse) p;
int nb = resp.getServiceNb();
List<CipService> arr = new ArrayList<>(nb);
ReadBufferByteBased read = new ReadBufferByteBased(resp.getServicesData());
int total = (int) read.getTotalBytes();
for (int i = 0; i < nb; i++) {
int length = 0;
int offset = resp.getOffsets().get(i);
if (offset == nb - 1) {
// Get the rest if last
length = total - offset;
} else {
// Calculate length with offsets
length = resp.getOffsets().get(i + 1) - offset;
}
ReadBuffer serviceBuf = new ReadBufferByteBased(read.getBytes(offset, length), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
CipService service = null;
try {
service = CipService.staticParse(read, length);
arr.add(service);
} catch (ParseException e) {
throw new PlcRuntimeException(e);
}
}
Services services = new Services(nb, resp.getOffsets(), arr, -1);
Iterator<String> it = writeRequest.getFieldNames().iterator();
for (int i = 0; i < nb && it.hasNext(); i++) {
String fieldName = it.next();
EipField field = (EipField) writeRequest.getField(fieldName);
PlcValue plcValue = null;
if (services.getServices().get(i) instanceof CipWriteResponse) {
CipWriteResponse writeResponse = (CipWriteResponse) services.getServices().get(i);
PlcResponseCode code = decodeResponseCode(writeResponse.getStatus());
responses.put(fieldName, code);
}
}
return new DefaultPlcWriteResponse(writeRequest, responses);
}
return null;
}
use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class ManualParserTest method readBufferTest.
@Test
public void readBufferTest() throws Exception {
ReadBuffer buffer = new ReadBufferByteBased(new byte[] { (byte) 0xA1, 0x05, 0x00, 0x00 }, ByteOrder.LITTLE_ENDIAN);
int value = buffer.readInt(32);
assertEquals(value, 0x5A1);
}
use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class BenchmarkGeneratedS7 method main.
public static void main(String[] args) throws Exception {
byte[] rData = Hex.decodeHex("0300001611e00000000f00c2020100c1020311c0010a");
long start = System.currentTimeMillis();
int numRunsParse = 2000000;
// Benchmark the parsing code
TPKTPacket packet = null;
for (int i = 0; i < numRunsParse; i++) {
ReadBuffer rBuf = new ReadBufferByteBased(rData);
packet = TPKTPacket.staticParse(rBuf);
}
long endParsing = System.currentTimeMillis();
System.out.println("Parsed " + numRunsParse + " packets in " + (endParsing - start) + "ms");
System.out.println("That's " + ((float) (endParsing - start) / numRunsParse) + "ms per packet");
// Benchmark the serializing code
int numRunsSerialize = 2000000;
byte[] oData = null;
for (int i = 0; i < numRunsSerialize; i++) {
WriteBufferByteBased wBuf = new WriteBufferByteBased(packet.getLengthInBytes());
packet.serialize(wBuf);
oData = wBuf.getData();
}
long endSerializing = System.currentTimeMillis();
System.out.println("Serialized " + numRunsSerialize + " packets in " + (endSerializing - endParsing) + "ms");
System.out.println("That's " + ((float) (endSerializing - endParsing) / numRunsSerialize) + "ms per packet");
if (!Arrays.equals(rData, oData)) {
for (int i = 0; i < rData.length; i++) {
if (rData[i] != oData[i]) {
System.out.println("Difference in byte " + i);
}
}
System.out.println("Not equals");
} else {
System.out.println("Bytes equal");
}
}
use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class Df1Protocol method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
DF1Symbol resp;
// do {
in.markReaderIndex();
short size = 0x00;
// Yes, it's a little complicated, but we need to find out if we've got enough data.
if (in.readableBytes() > 1) {
if (in.getUnsignedByte(0) != (short) 0x10) {
logger.warn("Expecting DF1 magic number: {}", 0x10);
if (logger.isDebugEnabled()) {
logger.debug("Got Data: {}", ByteBufUtil.hexDump(in));
}
exceptionCaught(ctx, new PlcProtocolException(String.format("Expecting DF1 magic number: %02X", 0x10)));
return;
}
short symbolType = in.getUnsignedByte(1);
switch(symbolType) {
case (short) 0x02:
{
if (in.readableBytes() < 5) {
return;
}
short commandType = in.getUnsignedByte(4);
switch(commandType) {
case (short) 0x01:
{
if (in.readableBytes() < 11) {
return;
}
break;
}
case (short) 0x41:
{
// TODO: Let's just assume all is good for now ...
break;
}
}
break;
}
case (short) 0x03:
{
if (in.readableBytes() < 4) {
return;
}
break;
}
}
}
// Parse the message received from the DF1 device
byte[] data = new byte[in.readableBytes()];
in.readBytes(data);
ReadBuffer readBuffer = new ReadBufferByteBased(data);
resp = DF1Symbol.staticParse(readBuffer);
// } while (readWasSucessfull);
// // TODO if unableto read
// in.resetReaderIndex();
// Add the received message to the output
out.add(resp);
}
use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class ManualParserSerializerTest method main.
public static void main(String[] args) throws Exception {
byte[] data = Hex.decodeHex("0000f1000000c0a817c80101feffc0a817140101530309000500d1000000000000002f00000000000000c90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a00a07d0f7e1c8e31489f8cf1006b3604b888defc88f64eb6fbb217d11fc5cf5f148b0abf05407c941a93b8263301db0f4940d66875727a0000000000d2040000ab6459032bbf7e03b888defcb8a5b249044de82ee03a2ab8a577006f006c0066000000488bf0488bfa33c9");
// byte[] data = Hex.decodeHex("0000f1000000c0a817c80101feffc0a817140101530309000500d1000000000000002f00000000000000c90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a00a07d0f7e1c8e31489f8cf1006b3604b888defc");
ReadBuffer readBuffer = new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN);
final AmsTCPPacket amsTCPPacket = AmsTCPPacket.staticParse(readBuffer);
System.out.println(amsTCPPacket);
}
Aggregations