use of org.apache.plc4x.java.spi.generation.ReadBufferByteBased 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.ReadBufferByteBased 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.ReadBufferByteBased in project plc4x by apache.
the class ManualParserTest method parse.
public static final SocketCanFrameStub parse(String hex) throws Exception {
byte[] input = Hex.decodeHex(hex.toCharArray());
ReadBufferByteBased readBuffer = new ReadBufferByteBased(input, ByteOrder.LITTLE_ENDIAN);
int rawId = readBuffer.readInt(32);
boolean extended = (rawId & EXTENDED_FRAME_FORMAT_FLAG) != 0;
boolean remote = (rawId & REMOTE_TRANSMISSION_FLAG) != 0;
boolean error = (rawId & ERROR_FRAME_FLAG) != 0;
int id = extended ? (rawId & EXTENDED_FORMAT_IDENTIFIER_MASK) : (rawId & STANDARD_FORMAT_IDENTIFIER_MASK);
int length = readBuffer.readByte();
byte[] data = readBuffer.getBytes(8, 8 + length);
return new SocketCanFrameStub(id, extended, remote, error, data);
}
use of org.apache.plc4x.java.spi.generation.ReadBufferByteBased 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.ReadBufferByteBased 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);
}
Aggregations