You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by er...@apache.org on 2020/04/06 12:34:10 UTC

[plc4x] 01/02: Updating driver to correctly parse MultipleServiceResponse -manually use the parser after calcultating service length with offsets

This is an automated email from the ASF dual-hosted git repository.

erobinet pushed a commit to branch eip
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit a2a99f848e112b38acaaae4f36b76e9258dd76f9
Author: Etienne Robinet <et...@gmail.com>
AuthorDate: Mon Apr 6 14:32:29 2020 +0200

    Updating driver to correctly parse MultipleServiceResponse
    -manually use the parser after calcultating service length with offsets
---
 .../eip/readwrite/protocol/EipProtocolLogic.java   | 293 ++++++++++++---------
 .../plc4x/java/spi/generation/ReadBuffer.java      |   6 +-
 .../eip/src/main/resources/protocols/eip/eip.mspec |   8 +-
 3 files changed, 177 insertions(+), 130 deletions(-)

diff --git a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/readwrite/protocol/EipProtocolLogic.java b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/readwrite/protocol/EipProtocolLogic.java
index bfba54f..e776e5c 100644
--- a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/readwrite/protocol/EipProtocolLogic.java
+++ b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/readwrite/protocol/EipProtocolLogic.java
@@ -20,6 +20,7 @@ package org.apache.plc4x.java.eip.readwrite.protocol;
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
+import io.netty.buffer.UnpooledDirectByteBuf;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.plc4x.java.api.messages.*;
@@ -29,10 +30,13 @@ import org.apache.plc4x.java.api.value.*;
 import org.apache.plc4x.java.eip.readwrite.*;
 import org.apache.plc4x.java.eip.readwrite.configuration.EIPConfiguration;
 import org.apache.plc4x.java.eip.readwrite.field.EipField;
+import org.apache.plc4x.java.eip.readwrite.io.CipServiceIO;
 import org.apache.plc4x.java.eip.readwrite.types.CIPDataTypeCode;
 import org.apache.plc4x.java.spi.ConversationContext;
 import org.apache.plc4x.java.spi.Plc4xProtocolBase;
 import org.apache.plc4x.java.spi.configuration.HasConfiguration;
+import org.apache.plc4x.java.spi.generation.ParseException;
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
 import org.apache.plc4x.java.spi.messages.*;
 import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
 import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
@@ -47,13 +51,13 @@ import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.atomic.AtomicInteger;
 
-public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements HasConfiguration<EIPConfiguration> {
+public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket> implements HasConfiguration<EIPConfiguration> {
 
     private static final Logger logger = LoggerFactory.getLogger(EipProtocolLogic.class);
     public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(10000);
 
-    private static final short[] emptySenderContext = new short[] {(short) 0x00 ,(short) 0x00 ,(short) 0x00,
-        (short) 0x00,(short) 0x00,(short) 0x00, (short) 0x00,(short) 0x00};
+    private static final short[] emptySenderContext = new short[]{(short) 0x00, (short) 0x00, (short) 0x00,
+        (short) 0x00, (short) 0x00, (short) 0x00, (short) 0x00, (short) 0x00};
     private short[] senderContext;
     private EIPConfiguration configuration;
 
@@ -62,7 +66,7 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     private long sessionHandle;
 
     @Override
-    public void setConfiguration(EIPConfiguration configuration){
+    public void setConfiguration(EIPConfiguration configuration) {
         this.configuration = configuration;
         // Set the transaction manager to allow only one message at a time.
         this.tm = new RequestTransactionManager(1);
@@ -72,19 +76,18 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     public void onConnect(ConversationContext<EipPacket> context) {
         logger.info("Sending RegisterSession EIP Package");
         EipConnectionRequest connectionRequest =
-          new EipConnectionRequest(0L,0L,emptySenderContext,0L);
+            new EipConnectionRequest(0L, 0L, emptySenderContext, 0L);
         context.sendRequest(connectionRequest)
-            .expectResponse(EipPacket.class, REQUEST_TIMEOUT).unwrap( p -> p)
+            .expectResponse(EipPacket.class, REQUEST_TIMEOUT).unwrap(p -> p)
             .check(p -> p instanceof EipConnectionRequest)
             .handle(p -> {
-                if(p.getStatus()==0L){
+                if (p.getStatus() == 0L) {
                     sessionHandle = p.getSessionHandle();
-                    senderContext= p.getSenderContext();
+                    senderContext = p.getSenderContext();
                     logger.trace("Got assigned with Session {}", sessionHandle);
                     // Send an event that connection setup is complete.
                     context.fireConnected();
-                }
-                else{
+                } else {
                     logger.warn("Got status code [{}]", p.getStatus());
                 }
 
@@ -95,36 +98,36 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
         DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
         List<CipReadRequest> requests = new ArrayList<>(request.getNumberOfFields());
-        for(PlcField field : request.getFields()) {
+        for (PlcField field : request.getFields()) {
             EipField plcField = (EipField) field;
             String tag = plcField.getTag();
-            int elements = 1 ;
-            if(plcField.getElementNb()>1){
+            int elements = 1;
+            if (plcField.getElementNb() > 1) {
                 elements = plcField.getElementNb();
             }
 
             //We need the size of the request in words (0x91, tagLength, ... tag + possible pad)
             // Taking half to get word size
             boolean isArray = false;
-            String tagIsolated=tag;
-            if(tag.contains("[")){
-                 isArray = true;
-                 tagIsolated = tag.substring(0, tag.indexOf("["));
+            String tagIsolated = tag;
+            if (tag.contains("[")) {
+                isArray = true;
+                tagIsolated = tag.substring(0, tag.indexOf("["));
             }
-            int dataLength = (tagIsolated.length() + 2 + (tagIsolated.length() % 2)+(isArray? 2:0));
-            byte requestPathSize = (byte) (dataLength/ 2);
+            int dataLength = (tagIsolated.length() + 2 + (tagIsolated.length() % 2) + (isArray ? 2 : 0));
+            byte requestPathSize = (byte) (dataLength / 2);
             CipReadRequest req = new CipReadRequest(requestPathSize, toAnsi(tag), elements);
             requests.add(req);
         }
-        return toPlcReadResponse((InternalPlcReadRequest)readRequest, readInternal(requests));
+        return toPlcReadResponse((InternalPlcReadRequest) readRequest, readInternal(requests));
     }
 
-    private byte[] toAnsi(String tag){
+    private byte[] toAnsi(String tag) {
         int arrayIndex = 0;
         boolean isArray = false;
-        if(tag.contains("[")){
+        if (tag.contains("[")) {
             isArray = true;
-            String index = tag.substring(tag.indexOf("[")+1, tag.indexOf("]"));
+            String index = tag.substring(tag.indexOf("[") + 1, tag.indexOf("]"));
             arrayIndex = Integer.parseInt(index);
             tag = tag.substring(0, tag.indexOf("["));
         }
@@ -134,23 +137,22 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
 
         ByteBuffer buffer = ByteBuffer.allocate(dataSegLength).order(ByteOrder.LITTLE_ENDIAN);
 
-        buffer.put((byte)0x91);
-        buffer.put((byte)tag.length());
+        buffer.put((byte) 0x91);
+        buffer.put((byte) tag.length());
         byte[] tagBytes = null;
         tagBytes = tag.getBytes(StandardCharsets.US_ASCII);
 
         buffer.put(tagBytes);
-        buffer.position(2 +tagBytes.length);
+        buffer.position(2 + tagBytes.length);
 
 
-        if(isPadded)
-        {
-            buffer.put((byte)0x00);
+        if (isPadded) {
+            buffer.put((byte) 0x00);
         }
 
-        if(isArray){
-            buffer.put((byte)0x28);
-            buffer.put((byte)arrayIndex);
+        if (isArray) {
+            buffer.put((byte) 0x28);
+            buffer.put((byte) arrayIndex);
         }
         return buffer.array();
     }
@@ -158,36 +160,36 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     private CompletableFuture<PlcReadResponse> toPlcReadResponse(InternalPlcReadRequest readRequest, CompletableFuture<CipService> response) {
         return response
             .thenApply(p -> {
-                        return ((PlcReadResponse) decodeReadResponse(p, readRequest));
+                return ((PlcReadResponse) decodeReadResponse(p, readRequest));
             });
     }
 
     private CompletableFuture<CipService> readInternal(List<CipReadRequest> request) {
         CompletableFuture<CipService> future = new CompletableFuture<>();
         RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
-        if(request.size()>1){
+        if (request.size() > 1) {
 
-            short nb =(short) request.size();
+            short nb = (short) request.size();
             int[] offsets = new int[nb];
-            int offset = 2 + nb*2;
-            for(int i = 0; i < nb ; i++){
-                offsets[i]=offset;
-                offset+=request.get(i).getLengthInBytes();
+            int offset = 2 + nb * 2;
+            for (int i = 0; i < nb; i++) {
+                offsets[i] = offset;
+                offset += request.get(i).getLengthInBytes();
             }
 
             CipService[] serviceArr = new CipService[nb];
-            for(int i = 0; i < nb ; i++){
-                serviceArr[i]=request.get(i);
+            for (int i = 0; i < nb; i++) {
+                serviceArr[i] = request.get(i);
             }
-            Services data = new Services(nb,offsets,serviceArr);
+            Services data = new Services(nb, offsets, serviceArr);
             //Encapsulate the data
 
-            CipRRData pkt = new CipRRData(sessionHandle,0L, emptySenderContext, 0L,
-                    new CipExchange(
-                        new CipUnconnectedRequest(
-                            new MultipleServiceRequest(data),
-                            (byte) configuration.getBackplane(),
-                            (byte)configuration.getSlot())));
+            CipRRData pkt = new CipRRData(sessionHandle, 0L, emptySenderContext, 0L,
+                new CipExchange(
+                    new CipUnconnectedRequest(
+                        new MultipleServiceRequest(data),
+                        (byte) configuration.getBackplane(),
+                        (byte) configuration.getSlot())));
 
 
             transaction.submit(() -> context.sendRequest(pkt)
@@ -200,16 +202,15 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
                 .unwrap(p -> (CipRRData) p)
                 .unwrap(p -> p.getExchange().getService()).check(p -> p instanceof MultipleServiceResponse)
                 .unwrap(p -> (MultipleServiceResponse) p)
-                .check(p -> p.getData().getServiceNb() == nb)
+                .check(p -> p.getServiceNb() == nb)
                 .handle(p -> {
                     future.complete(p);
                     // Finish the request-transaction.
                     transaction.endRequest();
                 }));
-        }
-        else if(request.size()==1) {
-            CipExchange exchange = new CipExchange(new CipUnconnectedRequest(request.get(0),(byte)configuration.getBackplane(),(byte)configuration.getSlot()));
-            CipRRData pkt = new CipRRData(sessionHandle,0L, emptySenderContext, 0L, exchange);
+        } else if (request.size() == 1) {
+            CipExchange exchange = new CipExchange(new CipUnconnectedRequest(request.get(0), (byte) configuration.getBackplane(), (byte) configuration.getSlot()));
+            CipRRData pkt = new CipRRData(sessionHandle, 0L, emptySenderContext, 0L, exchange);
             transaction.submit(() -> context.sendRequest(pkt)
                 .expectResponse(EipPacket.class, REQUEST_TIMEOUT)
                 .onTimeout(future::completeExceptionally)
@@ -232,8 +233,8 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     private PlcResponse decodeReadResponse(CipService p, InternalPlcReadRequest readRequest) {
         Map<String, ResponseItem<PlcValue>> values = new HashMap<>();
         // only 1 field
-        if(p instanceof CipReadResponse) {
-            CipReadResponse resp = (CipReadResponse)p;
+        if (p instanceof CipReadResponse) {
+            CipReadResponse resp = (CipReadResponse) p;
             String fieldName = readRequest.getFieldNames().iterator().next();
             EipField field = (EipField) readRequest.getField(fieldName);
             PlcResponseCode code = decodeResponseCode(resp.getStatus());
@@ -247,17 +248,37 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
             values.put(fieldName, result);
         }
         //Multiple response
-        else if(p instanceof MultipleServiceResponse && ((MultipleServiceResponse) p).getStatus()==0){
-            MultipleServiceResponse responses = (MultipleServiceResponse)p;
-            Services services = responses.getData();
-            int nb = services.getServiceNb();
+        else if (p instanceof MultipleServiceResponse) {
+            MultipleServiceResponse responses = (MultipleServiceResponse) p;
+            int nb = responses.getServiceNb();
+            CipService[] arr = new CipService[nb];
+            ReadBuffer read = new ReadBuffer(responses.getServicesData(),true);
+            int total = (int) read.getTotalBytes();
+            for (int i = 0; i < nb; i++) {
+                int length = 0;
+                int offset = responses.getOffsets()[i]-responses.getOffsets()[0]; //Substract first offset as we only have the service in the buffer (not servicesNb and offsets)
+                if (i == nb - 1) {
+                    length = total - offset; //Get the rest if last
+                } else {
+                    length = responses.getOffsets()[i + 1] - offset - responses.getOffsets()[0]; //Calculate length with offsets (substracting first offset)
+                }
+                ReadBuffer serviceBuf = new ReadBuffer(read.getBytes(offset, offset+length),true);
+                CipService service = null;
+                try {
+                    service = CipServiceIO.staticParse(read, length);
+                    arr[i] = service;
+                } catch (ParseException e) {
+                    e.printStackTrace();
+                }
+            }
+            Services services = new Services(nb, responses.getOffsets(), arr);
             Iterator<String> it = readRequest.getFieldNames().iterator();
-            for(int i = 0; i<nb && it.hasNext() ; i++){
+            for (int i = 0; i < nb && it.hasNext(); i++) {
                 String fieldName = it.next();
                 EipField field = (EipField) readRequest.getField(fieldName);
                 PlcValue plcValue = null;
-                if(services.getServices()[i] instanceof CipReadResponse){
-                    CipReadResponse readResponse = (CipReadResponse)services.getServices()[i];
+                if (services.getServices()[i] instanceof CipReadResponse) {
+                    CipReadResponse readResponse = (CipReadResponse) services.getServices()[i];
                     PlcResponseCode code;
                     if (readResponse.getStatus() == 0) {
                         code = PlcResponseCode.OK;
@@ -274,33 +295,32 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
                 }
             }
         }
-        return new DefaultPlcReadResponse(readRequest,values);
+        return new DefaultPlcReadResponse(readRequest, values);
     }
 
     private PlcValue parsePlcValue(EipField field, ByteBuf data, CIPDataTypeCode type) {
         int nb = field.getElementNb();
-        if(nb>1){
-            int index =0;
+        if (nb > 1) {
+            int index = 0;
             List<PlcValue> list = new ArrayList<>();
-            for(int i=0 ; i < nb ; i++){
-                switch(type){
+            for (int i = 0; i < nb; i++) {
+                switch (type) {
                     case DINT:
                     case INT:
                     case SINT:
                         list.add(new PlcInteger(Integer.reverseBytes(data.getInt(index))));
-                        index+= type.getSize();
+                        index += type.getSize();
                         break;
                     case REAL:
                         list.add(new PlcDouble(swap(data.getFloat(index))));
-                        index+= type.getSize();
+                        index += type.getSize();
                         break;
                     default:
                         return null;
                 }
             }
             return new PlcList(list);
-        }
-        else {
+        } else {
             switch (type) {
                 case SINT:
                     return new PlcByte(data.getByte(0));
@@ -316,10 +336,10 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
         }
     }
 
-    public float swap(float value){
+    public float swap(float value) {
         int bytes = Float.floatToIntBits(value);
-        int b1 = (bytes >>  0) & 0xff;
-        int b2 = (bytes >>  8) & 0xff;
+        int b1 = (bytes >> 0) & 0xff;
+        int b2 = (bytes >> 8) & 0xff;
         int b3 = (bytes >> 16) & 0xff;
         int b4 = (bytes >> 24) & 0xff;
         return Float.intBitsToFloat(b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0);
@@ -330,73 +350,72 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
         CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
         DefaultPlcWriteRequest request = (DefaultPlcWriteRequest) writeRequest;
         List<CipWriteRequest> items = new ArrayList<>(writeRequest.getNumberOfFields());
-        for(String fieldName : request.getFieldNames()){
+        for (String fieldName : request.getFieldNames()) {
             final EipField field = (EipField) request.getField(fieldName);
             final PlcValue value = request.getPlcValue(fieldName);
             String tag = field.getTag();
-            int elements = 1 ;
-            if(field.getElementNb()>1){
+            int elements = 1;
+            if (field.getElementNb() > 1) {
                 elements = field.getElementNb();
             }
 
             //We need the size of the request in words (0x91, tagLength, ... tag + possible pad)
             // Taking half to get word size
             boolean isArray = false;
-            String tagIsolated=tag;
-            if(tag.contains("[")){
+            String tagIsolated = tag;
+            if (tag.contains("[")) {
                 isArray = true;
                 tagIsolated = tag.substring(0, tag.indexOf("["));
             }
-            int dataLength = (tagIsolated.length() + 2 + (tagIsolated.length() % 2)+(isArray? 2:0));
-            byte requestPathSize = (byte) (dataLength/ 2);
-            byte[]data = encodeValue(value,field.getType(),(short)elements);
-            CipWriteRequest writeReq = new CipWriteRequest(requestPathSize,toAnsi(tag),field.getType(),elements,data);
+            int dataLength = (tagIsolated.length() + 2 + (tagIsolated.length() % 2) + (isArray ? 2 : 0));
+            byte requestPathSize = (byte) (dataLength / 2);
+            byte[] data = encodeValue(value, field.getType(), (short) elements);
+            CipWriteRequest writeReq = new CipWriteRequest(requestPathSize, toAnsi(tag), field.getType(), elements, data);
             items.add(writeReq);
         }
 
         RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
-        if(items.size()==1){
+        if (items.size() == 1) {
             tm.startRequest();
-            CipRRData rrdata = new CipRRData(sessionHandle,0L,senderContext,0L,
+            CipRRData rrdata = new CipRRData(sessionHandle, 0L, senderContext, 0L,
                 new CipExchange(items.get(0)));
-            transaction.submit(()-> context.sendRequest(rrdata)
-            .expectResponse(EipPacket.class, REQUEST_TIMEOUT)
-            .onTimeout(future::completeExceptionally)
-            .onError((p,e) -> future.completeExceptionally(e))
-                .check( p -> p instanceof CipRRData).unwrap(p -> (CipRRData)p)
+            transaction.submit(() -> context.sendRequest(rrdata)
+                .expectResponse(EipPacket.class, REQUEST_TIMEOUT)
+                .onTimeout(future::completeExceptionally)
+                .onError((p, e) -> future.completeExceptionally(e))
+                .check(p -> p instanceof CipRRData).unwrap(p -> (CipRRData) p)
                 .check(p -> p.getSessionHandle() == sessionHandle)
-              //.check(p -> p.getSenderContext() == senderContext)
+                //.check(p -> p.getSenderContext() == senderContext)
                 .check(p -> p.getExchange().getService() instanceof CipWriteResponse)
-                .unwrap(p -> (CipWriteResponse)p.getExchange().getService())
-                .handle(p ->{
-                    future.complete((PlcWriteResponse)decodeWriteResponse(p,((InternalPlcWriteRequest)writeRequest)));
+                .unwrap(p -> (CipWriteResponse) p.getExchange().getService())
+                .handle(p -> {
+                    future.complete((PlcWriteResponse) decodeWriteResponse(p, ((InternalPlcWriteRequest) writeRequest)));
                     transaction.endRequest();
                 })
             );
-        }
-        else {
+        } else {
             tm.startRequest();
-            short nb =(short) items.size();
+            short nb = (short) items.size();
             int[] offsets = new int[nb];
-            int offset = 2 + nb*2;
-            for(int i = 0; i < nb ; i++){
-                offsets[i]=offset;
-                offset+=items.get(i).getLengthInBytes();
+            int offset = 2 + nb * 2;
+            for (int i = 0; i < nb; i++) {
+                offsets[i] = offset;
+                offset += items.get(i).getLengthInBytes();
             }
 
             CipService[] serviceArr = new CipService[nb];
-            for(int i = 0; i < nb ; i++){
-                serviceArr[i]=items.get(i);
+            for (int i = 0; i < nb; i++) {
+                serviceArr[i] = items.get(i);
             }
-            Services data = new Services(nb,offsets,serviceArr);
+            Services data = new Services(nb, offsets, serviceArr);
             //Encapsulate the data
 
-            CipRRData pkt = new CipRRData(sessionHandle,0L, emptySenderContext, 0L,
+            CipRRData pkt = new CipRRData(sessionHandle, 0L, emptySenderContext, 0L,
                 new CipExchange(
                     new CipUnconnectedRequest(
                         new MultipleServiceRequest(data),
                         (byte) configuration.getBackplane(),
-                        (byte)configuration.getSlot())));
+                        (byte) configuration.getSlot())));
 
 
             transaction.submit(() -> context.sendRequest(pkt)
@@ -409,9 +428,9 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
                 .unwrap(p -> (CipRRData) p)
                 .unwrap(p -> p.getExchange().getService()).check(p -> p instanceof MultipleServiceResponse)
                 .unwrap(p -> (MultipleServiceResponse) p)
-                .check(p -> p.getData().getServiceNb() == nb)
+                .check(p -> p.getServiceNb() == nb)
                 .handle(p -> {
-                    future.complete((PlcWriteResponse)decodeWriteResponse(p,((InternalPlcWriteRequest)writeRequest)));
+                    future.complete((PlcWriteResponse) decodeWriteResponse(p, ((InternalPlcWriteRequest) writeRequest)));
                     // Finish the request-transaction.
                     transaction.endRequest();
                 }));
@@ -422,24 +441,43 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     private PlcResponse decodeWriteResponse(CipService p, InternalPlcWriteRequest writeRequest) {
         Map<String, PlcResponseCode> responses = new HashMap<>();
 
-        if(p instanceof CipWriteResponse){
-            CipWriteResponse resp = (CipWriteResponse)p;
+        if (p instanceof CipWriteResponse) {
+            CipWriteResponse resp = (CipWriteResponse) p;
             String fieldName = writeRequest.getFieldNames().iterator().next();
-            EipField field =(EipField) writeRequest.getField(fieldName);
+            EipField field = (EipField) writeRequest.getField(fieldName);
             responses.put(fieldName, decodeResponseCode(resp.getStatus()));
-            return new DefaultPlcWriteResponse(writeRequest,responses);
-        }
-        else if(p instanceof MultipleServiceResponse){
-            MultipleServiceResponse multResponses = (MultipleServiceResponse)p;
-            Services services = multResponses.getData();
-            int nb = services.getServiceNb();
+            return new DefaultPlcWriteResponse(writeRequest, responses);
+        } else if (p instanceof MultipleServiceResponse) {
+            MultipleServiceResponse resp = (MultipleServiceResponse) p;
+            int nb = resp.getServiceNb();
+            CipService[] arr = new CipService[nb];
+            ReadBuffer read = new ReadBuffer(resp.getServicesData());
+            int total = (int) read.getTotalBytes();
+            for (int i = 0; i < nb; i++) {
+                int length = 0;
+                int offset = resp.getOffsets()[i];
+                if (offset == nb - 1) {
+                    length = total - offset; //Get the rest if last
+                } else {
+                    length = resp.getOffsets()[i + 1] - offset; //Calculate length with offsets
+                }
+                ReadBuffer serviceBuf = new ReadBuffer(read.getBytes(offset, length),true);
+                CipService service = null;
+                try {
+                    service = CipServiceIO.staticParse(read, length);
+                    arr[i] = service;
+                } catch (ParseException e) {
+                    e.printStackTrace();
+                }
+            }
+            Services services = new Services(nb, resp.getOffsets(), arr);
             Iterator<String> it = writeRequest.getFieldNames().iterator();
-            for(int i = 0; i<nb && it.hasNext() ; i++){
+            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()[i] instanceof CipWriteResponse){
-                    CipWriteResponse writeResponse = (CipWriteResponse)services.getServices()[i];
+                if (services.getServices()[i] instanceof CipWriteResponse) {
+                    CipWriteResponse writeResponse = (CipWriteResponse) services.getServices()[i];
                     PlcResponseCode code = decodeResponseCode(writeResponse.getStatus());
                     responses.put(fieldName, code);
                 }
@@ -452,7 +490,7 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
     private byte[] encodeValue(PlcValue value, CIPDataTypeCode type, short elements) {
         //ByteBuffer buffer = ByteBuffer.allocate(4+type.getSize()).order(ByteOrder.LITTLE_ENDIAN);
         ByteBuffer buffer = ByteBuffer.allocate(type.getSize()).order(ByteOrder.LITTLE_ENDIAN);
-        switch(type){
+        switch (type) {
             case SINT:
                 buffer.put(value.getByte());
                 break;
@@ -465,17 +503,20 @@ public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket>implements Has
             case REAL:
                 buffer.putDouble(value.getDouble());
                 break;
-            default:break;
+            default:
+                break;
         }
         return buffer.array();
 
     }
 
-    private PlcResponseCode decodeResponseCode(int status){
+    private PlcResponseCode decodeResponseCode(int status) {
         //TODO other status
-        switch (status){
-            case 0 : return PlcResponseCode.OK;
-            default: return PlcResponseCode.INTERNAL_ERROR;
+        switch (status) {
+            case 0:
+                return PlcResponseCode.OK;
+            default:
+                return PlcResponseCode.INTERNAL_ERROR;
         }
     }
 
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
index ed5a766..7047c4f 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
@@ -42,7 +42,7 @@ public class ReadBuffer {
         ArrayByteInput abi = new ArrayByteInput(input);
         this.bi = new MyDefaultBitInput(abi);
         this.littleEndian = littleEndian;
-        this.totalBytes = (long) input.length * 8;
+        this.totalBytes = (long) input.length;
     }
 
     public int getPos() {
@@ -56,6 +56,10 @@ public class ReadBuffer {
         return data;
     }
 
+    public long getTotalBytes() {
+        return totalBytes;
+    }
+
     public boolean hasMore(int numBits) {
         return (numBits / 8) < (totalBytes - getPos());
     }
diff --git a/protocols/eip/src/main/resources/protocols/eip/eip.mspec b/protocols/eip/src/main/resources/protocols/eip/eip.mspec
index 9cfccd9..3b03462 100644
--- a/protocols/eip/src/main/resources/protocols/eip/eip.mspec
+++ b/protocols/eip/src/main/resources/protocols/eip/eip.mspec
@@ -63,7 +63,7 @@
               [simple     uint            8   'status']
               [simple     uint            8   'extStatus']
               [enum       CIPDataTypeCode     'dataType']
-              [array      int             8   'data'  count  'dataType.size']
+              [array      int             8   'data'  count  'serviceLen-6']
         ]
         ['0x4D' CipWriteRequest
             [simple     int     8           'RequestPathSize']
@@ -86,7 +86,9 @@
                [reserved   uint    8   '0x0']
                [simple     uint    8   'status']
                [simple     uint    8   'extStatus']
-               [simple Services  'data'['serviceLen - 4']]
+               [simple     uint    16  'serviceNb']
+               [array      uint    16  'offsets'       count  'serviceNb']
+               [array      int     8   'servicesData' count 'serviceLen-6-(2*serviceNb)']
         ]
         ['0x52'   CipUnconnectedRequest
                [reserved   uint    8   '0x02']
@@ -105,7 +107,7 @@
 ]
 
 [type   'Services'  [uint   16   'servicesLen']
-    [simple uint    16  'serviceNb']
+    [simple uint        16  'serviceNb']
     [array  uint        16  'offsets'       count  'serviceNb']
     [array  CipService   'services'      count  'serviceNb' ['servicesLen/serviceNb'] ]
 ]