@Test public void dataMessageBOOLEAN() throws SecsException { byte[] data = new byte[] { 0x00, 0x00, 0x00, 0x0d, 0x00, 0x01, (byte) 0x82, 0x19, 0x00, 0x00, 0x11, 0x12, 0x13, 0x14, 0x11, 0x01, 0x01}; SecsMessage dataMessage = (SecsMessage) MessageParser.parseMessage(data, data.length, messageTypes); Data<?> text = dataMessage.getData(); Assert.assertTrue(text instanceof BOOLEAN); BOOLEAN b = (BOOLEAN) text; Assert.assertEquals(1, b.length()); Assert.assertTrue(b.getValue()); Assert.assertEquals("<BOOLEAN True>", text.toSml()); }
public static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes) throws SecsException { if (length < SecsConstants.HEADER_LENGTH) { throw new SecsParseException(String.format("Incomplete message (message length: %d)", length)); } byte[] lengthField = new byte[LENGTH_LENGTH]; System.arraycopy(data, 0, lengthField, 0, LENGTH_LENGTH); long messageLength = new U4(lengthField).getValue(0); if (length < (messageLength + LENGTH_LENGTH)) { throw new SecsParseException(String.format("Incomplete message (declared length: %d; actual length: %d)", messageLength + LENGTH_LENGTH, length)); } if (messageLength > MAX_LENGTH) { throw new SecsParseException(String.format("Message too large (%d bytes)", messageLength)); } byte[] sessionIdBuf = new byte[SESSION_ID_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SESSIONID, sessionIdBuf, 0, SESSION_ID_LENGTH); int sessionId = (int) ConversionUtils.bytesToUnsignedInteger(sessionIdBuf); byte headerByte2 = data[LENGTH_LENGTH + POS_HEADERBYTE2]; byte headerByte3 = data[LENGTH_LENGTH + POS_HEADERBYTE3]; byte pTypeByte = data[LENGTH_LENGTH + POS_PTYPE]; PType pType = PType.parse(pTypeByte); if (pType != PType.SECS_II) { throw new SecsParseException(String.format("Unsupported protocol; not SECS-II (PType: %d)", pTypeByte)); } byte sTypeByte = data[LENGTH_LENGTH + POS_STYPE]; SType sType = SType.parse(sTypeByte); if (sType == SType.UNKNOWN) { throw new SecsParseException(String.format("Unsupported message type (SType: %02x)", sTypeByte)); } byte[] transactionIdBuf = new byte[SYSTEM_BYTES_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SYSTEMBYTES, transactionIdBuf, 0, SYSTEM_BYTES_LENGTH); long transactionId = ConversionUtils.bytesToUnsignedInteger(transactionIdBuf); if (sType == SType.DATA) { int dataLength = (int) (messageLength - SecsConstants.HEADER_LENGTH); Data<?> text = null; if (dataLength > 0) { byte[] textBytes = new byte[dataLength]; System.arraycopy(data, MIN_LENGTH, textBytes, 0, dataLength); text = parseData(textBytes, 0); } int stream = headerByte2 & STREAM_MASK; int function = headerByte3; Class<? extends SecsMessage> messageType = messageTypes.get(stream * 256 + function); if (messageType != null) { try { SecsMessage dataMessage = messageType.newInstance(); dataMessage.setSessionId(sessionId); dataMessage.setTransactionId(transactionId); dataMessage.parseData(text); return dataMessage; } catch (SecsParseException e) { throw e; } catch (Exception e) { throw new SecsParseException("Could not instantiate message type: " + messageType, e); } } else { throw new UnsupportedMessageException(stream, function, transactionId); } } else { return new ControlMessage(sessionId, headerByte2, headerByte3, sType, transactionId); } }
MessageParser { public static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes) throws SecsException { if (length < SecsConstants.HEADER_LENGTH) { throw new SecsParseException(String.format("Incomplete message (message length: %d)", length)); } byte[] lengthField = new byte[LENGTH_LENGTH]; System.arraycopy(data, 0, lengthField, 0, LENGTH_LENGTH); long messageLength = new U4(lengthField).getValue(0); if (length < (messageLength + LENGTH_LENGTH)) { throw new SecsParseException(String.format("Incomplete message (declared length: %d; actual length: %d)", messageLength + LENGTH_LENGTH, length)); } if (messageLength > MAX_LENGTH) { throw new SecsParseException(String.format("Message too large (%d bytes)", messageLength)); } byte[] sessionIdBuf = new byte[SESSION_ID_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SESSIONID, sessionIdBuf, 0, SESSION_ID_LENGTH); int sessionId = (int) ConversionUtils.bytesToUnsignedInteger(sessionIdBuf); byte headerByte2 = data[LENGTH_LENGTH + POS_HEADERBYTE2]; byte headerByte3 = data[LENGTH_LENGTH + POS_HEADERBYTE3]; byte pTypeByte = data[LENGTH_LENGTH + POS_PTYPE]; PType pType = PType.parse(pTypeByte); if (pType != PType.SECS_II) { throw new SecsParseException(String.format("Unsupported protocol; not SECS-II (PType: %d)", pTypeByte)); } byte sTypeByte = data[LENGTH_LENGTH + POS_STYPE]; SType sType = SType.parse(sTypeByte); if (sType == SType.UNKNOWN) { throw new SecsParseException(String.format("Unsupported message type (SType: %02x)", sTypeByte)); } byte[] transactionIdBuf = new byte[SYSTEM_BYTES_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SYSTEMBYTES, transactionIdBuf, 0, SYSTEM_BYTES_LENGTH); long transactionId = ConversionUtils.bytesToUnsignedInteger(transactionIdBuf); if (sType == SType.DATA) { int dataLength = (int) (messageLength - SecsConstants.HEADER_LENGTH); Data<?> text = null; if (dataLength > 0) { byte[] textBytes = new byte[dataLength]; System.arraycopy(data, MIN_LENGTH, textBytes, 0, dataLength); text = parseData(textBytes, 0); } int stream = headerByte2 & STREAM_MASK; int function = headerByte3; Class<? extends SecsMessage> messageType = messageTypes.get(stream * 256 + function); if (messageType != null) { try { SecsMessage dataMessage = messageType.newInstance(); dataMessage.setSessionId(sessionId); dataMessage.setTransactionId(transactionId); dataMessage.parseData(text); return dataMessage; } catch (SecsParseException e) { throw e; } catch (Exception e) { throw new SecsParseException("Could not instantiate message type: " + messageType, e); } } else { throw new UnsupportedMessageException(stream, function, transactionId); } } else { return new ControlMessage(sessionId, headerByte2, headerByte3, sType, transactionId); } } }
MessageParser { public static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes) throws SecsException { if (length < SecsConstants.HEADER_LENGTH) { throw new SecsParseException(String.format("Incomplete message (message length: %d)", length)); } byte[] lengthField = new byte[LENGTH_LENGTH]; System.arraycopy(data, 0, lengthField, 0, LENGTH_LENGTH); long messageLength = new U4(lengthField).getValue(0); if (length < (messageLength + LENGTH_LENGTH)) { throw new SecsParseException(String.format("Incomplete message (declared length: %d; actual length: %d)", messageLength + LENGTH_LENGTH, length)); } if (messageLength > MAX_LENGTH) { throw new SecsParseException(String.format("Message too large (%d bytes)", messageLength)); } byte[] sessionIdBuf = new byte[SESSION_ID_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SESSIONID, sessionIdBuf, 0, SESSION_ID_LENGTH); int sessionId = (int) ConversionUtils.bytesToUnsignedInteger(sessionIdBuf); byte headerByte2 = data[LENGTH_LENGTH + POS_HEADERBYTE2]; byte headerByte3 = data[LENGTH_LENGTH + POS_HEADERBYTE3]; byte pTypeByte = data[LENGTH_LENGTH + POS_PTYPE]; PType pType = PType.parse(pTypeByte); if (pType != PType.SECS_II) { throw new SecsParseException(String.format("Unsupported protocol; not SECS-II (PType: %d)", pTypeByte)); } byte sTypeByte = data[LENGTH_LENGTH + POS_STYPE]; SType sType = SType.parse(sTypeByte); if (sType == SType.UNKNOWN) { throw new SecsParseException(String.format("Unsupported message type (SType: %02x)", sTypeByte)); } byte[] transactionIdBuf = new byte[SYSTEM_BYTES_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SYSTEMBYTES, transactionIdBuf, 0, SYSTEM_BYTES_LENGTH); long transactionId = ConversionUtils.bytesToUnsignedInteger(transactionIdBuf); if (sType == SType.DATA) { int dataLength = (int) (messageLength - SecsConstants.HEADER_LENGTH); Data<?> text = null; if (dataLength > 0) { byte[] textBytes = new byte[dataLength]; System.arraycopy(data, MIN_LENGTH, textBytes, 0, dataLength); text = parseData(textBytes, 0); } int stream = headerByte2 & STREAM_MASK; int function = headerByte3; Class<? extends SecsMessage> messageType = messageTypes.get(stream * 256 + function); if (messageType != null) { try { SecsMessage dataMessage = messageType.newInstance(); dataMessage.setSessionId(sessionId); dataMessage.setTransactionId(transactionId); dataMessage.parseData(text); return dataMessage; } catch (SecsParseException e) { throw e; } catch (Exception e) { throw new SecsParseException("Could not instantiate message type: " + messageType, e); } } else { throw new UnsupportedMessageException(stream, function, transactionId); } } else { return new ControlMessage(sessionId, headerByte2, headerByte3, sType, transactionId); } }  }
MessageParser { public static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes) throws SecsException { if (length < SecsConstants.HEADER_LENGTH) { throw new SecsParseException(String.format("Incomplete message (message length: %d)", length)); } byte[] lengthField = new byte[LENGTH_LENGTH]; System.arraycopy(data, 0, lengthField, 0, LENGTH_LENGTH); long messageLength = new U4(lengthField).getValue(0); if (length < (messageLength + LENGTH_LENGTH)) { throw new SecsParseException(String.format("Incomplete message (declared length: %d; actual length: %d)", messageLength + LENGTH_LENGTH, length)); } if (messageLength > MAX_LENGTH) { throw new SecsParseException(String.format("Message too large (%d bytes)", messageLength)); } byte[] sessionIdBuf = new byte[SESSION_ID_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SESSIONID, sessionIdBuf, 0, SESSION_ID_LENGTH); int sessionId = (int) ConversionUtils.bytesToUnsignedInteger(sessionIdBuf); byte headerByte2 = data[LENGTH_LENGTH + POS_HEADERBYTE2]; byte headerByte3 = data[LENGTH_LENGTH + POS_HEADERBYTE3]; byte pTypeByte = data[LENGTH_LENGTH + POS_PTYPE]; PType pType = PType.parse(pTypeByte); if (pType != PType.SECS_II) { throw new SecsParseException(String.format("Unsupported protocol; not SECS-II (PType: %d)", pTypeByte)); } byte sTypeByte = data[LENGTH_LENGTH + POS_STYPE]; SType sType = SType.parse(sTypeByte); if (sType == SType.UNKNOWN) { throw new SecsParseException(String.format("Unsupported message type (SType: %02x)", sTypeByte)); } byte[] transactionIdBuf = new byte[SYSTEM_BYTES_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SYSTEMBYTES, transactionIdBuf, 0, SYSTEM_BYTES_LENGTH); long transactionId = ConversionUtils.bytesToUnsignedInteger(transactionIdBuf); if (sType == SType.DATA) { int dataLength = (int) (messageLength - SecsConstants.HEADER_LENGTH); Data<?> text = null; if (dataLength > 0) { byte[] textBytes = new byte[dataLength]; System.arraycopy(data, MIN_LENGTH, textBytes, 0, dataLength); text = parseData(textBytes, 0); } int stream = headerByte2 & STREAM_MASK; int function = headerByte3; Class<? extends SecsMessage> messageType = messageTypes.get(stream * 256 + function); if (messageType != null) { try { SecsMessage dataMessage = messageType.newInstance(); dataMessage.setSessionId(sessionId); dataMessage.setTransactionId(transactionId); dataMessage.parseData(text); return dataMessage; } catch (SecsParseException e) { throw e; } catch (Exception e) { throw new SecsParseException("Could not instantiate message type: " + messageType, e); } } else { throw new UnsupportedMessageException(stream, function, transactionId); } } else { return new ControlMessage(sessionId, headerByte2, headerByte3, sType, transactionId); } }  static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes); static Data<?> parseData(String text); }
MessageParser { public static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes) throws SecsException { if (length < SecsConstants.HEADER_LENGTH) { throw new SecsParseException(String.format("Incomplete message (message length: %d)", length)); } byte[] lengthField = new byte[LENGTH_LENGTH]; System.arraycopy(data, 0, lengthField, 0, LENGTH_LENGTH); long messageLength = new U4(lengthField).getValue(0); if (length < (messageLength + LENGTH_LENGTH)) { throw new SecsParseException(String.format("Incomplete message (declared length: %d; actual length: %d)", messageLength + LENGTH_LENGTH, length)); } if (messageLength > MAX_LENGTH) { throw new SecsParseException(String.format("Message too large (%d bytes)", messageLength)); } byte[] sessionIdBuf = new byte[SESSION_ID_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SESSIONID, sessionIdBuf, 0, SESSION_ID_LENGTH); int sessionId = (int) ConversionUtils.bytesToUnsignedInteger(sessionIdBuf); byte headerByte2 = data[LENGTH_LENGTH + POS_HEADERBYTE2]; byte headerByte3 = data[LENGTH_LENGTH + POS_HEADERBYTE3]; byte pTypeByte = data[LENGTH_LENGTH + POS_PTYPE]; PType pType = PType.parse(pTypeByte); if (pType != PType.SECS_II) { throw new SecsParseException(String.format("Unsupported protocol; not SECS-II (PType: %d)", pTypeByte)); } byte sTypeByte = data[LENGTH_LENGTH + POS_STYPE]; SType sType = SType.parse(sTypeByte); if (sType == SType.UNKNOWN) { throw new SecsParseException(String.format("Unsupported message type (SType: %02x)", sTypeByte)); } byte[] transactionIdBuf = new byte[SYSTEM_BYTES_LENGTH]; System.arraycopy(data, LENGTH_LENGTH + POS_SYSTEMBYTES, transactionIdBuf, 0, SYSTEM_BYTES_LENGTH); long transactionId = ConversionUtils.bytesToUnsignedInteger(transactionIdBuf); if (sType == SType.DATA) { int dataLength = (int) (messageLength - SecsConstants.HEADER_LENGTH); Data<?> text = null; if (dataLength > 0) { byte[] textBytes = new byte[dataLength]; System.arraycopy(data, MIN_LENGTH, textBytes, 0, dataLength); text = parseData(textBytes, 0); } int stream = headerByte2 & STREAM_MASK; int function = headerByte3; Class<? extends SecsMessage> messageType = messageTypes.get(stream * 256 + function); if (messageType != null) { try { SecsMessage dataMessage = messageType.newInstance(); dataMessage.setSessionId(sessionId); dataMessage.setTransactionId(transactionId); dataMessage.parseData(text); return dataMessage; } catch (SecsParseException e) { throw e; } catch (Exception e) { throw new SecsParseException("Could not instantiate message type: " + messageType, e); } } else { throw new UnsupportedMessageException(stream, function, transactionId); } } else { return new ControlMessage(sessionId, headerByte2, headerByte3, sType, transactionId); } }  static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes); static Data<?> parseData(String text);  }