コード例 #1
0
        private byte[] unpackFrame(int cmdNoCipher, int cmdGlobalCipher, byte[] data)
        {
            if ((data[0] & 0xFF) == Constants.xDlmsApdu.Exception.ExceptionResponse)
            {
                DlmsException.DlmsExceptionReason[] reasons = new DlmsException.DlmsExceptionReason[2];
                reasons[0] = DlmsException.DlmsExceptionReason.STATE_ERROR_UNKNOWN;
                reasons[1] = DlmsException.DlmsExceptionReason.SERVICE_ERROR_UNKNOWN;

                if (data[1] == 1)
                {
                    reasons[0] = DlmsException.DlmsExceptionReason.STATE_ERROR_SERVICE_NOT_ALLOWED;
                }
                else if (data[1] == 2)
                {
                    reasons[0] = DlmsException.DlmsExceptionReason.STATE_ERROR_SERVICE_UNKNOWN;
                }

                if (data[2] == 1)
                {
                    reasons[1] = DlmsException.DlmsExceptionReason.SERVICE_ERROR_OPERATION_NOT_POSSIBLE;
                }
                else if (data[2] == 2)
                {
                    reasons[1] = DlmsException.DlmsExceptionReason.SERVICE_ERROR_NOT_SUPPORTED;
                }
                else if (data[2] == 3)
                {
                    reasons[1] = DlmsException.DlmsExceptionReason.SERVICE_ERROR_OTHER_REASON;
                }

                throw new DlmsException(reasons);
            }
            if (parameters.securityType != SecurityType.NONE)
            {
                if ((data[0] & 0xff) != cmdGlobalCipher)
                {
                    throw new DlmsException(DlmsException.DlmsExceptionReason.RECEIVED_INVALID_COMMAND_ID);
                }
                data = getPayload(data, 1);
                data = Security.reverseAuthenticatedEncryption(parameters, connection, data);
            }
            if ((data[0] & 0xFF) != cmdNoCipher)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.RECEIVED_INVALID_COMMAND_ID);
            }
            return(helper.extensions.copyOfRange(data, 1, data.Length));
        }
コード例 #2
0
        private static void parseUserInfo(byte[] data, CosemParameters parameters, CosemConnection connection)
        {
            if (data == null || data.Length < 16)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (data[0] != Constants.Ber.OCTET_STRING)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (data[1] < 14 || data[1] != data.Length - 2)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (parameters.securityType != SecurityType.NONE)
            {
                if (data[2] != Constants.xDlmsApdu.GlobalCiphering.INITIATE_RESPONSE)
                {
                    throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
                }
                if (data[3] != data.Length - 4)
                {
                    throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
                }
                byte[] encrypted = new byte[data[3] & 0xFF];
                Array.Copy(data, 4, encrypted, 0, encrypted.Length);
                data = Security.reverseAuthenticatedEncryption(parameters, connection, encrypted);
            }
            else
            {
                data = helper.extensions.copyOfRange(data, 2, data.Length);
            }
            if (data[0] != Constants.xDlmsApdu.NoCiphering.INITIATE_RESPONSE)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (data[1] != 0 || data[2] != Constants.DLMS_VERSION)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (data[3] != Constants.ConformanceBlock.TAG)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (data[4] != 0x1F || data[5] != 0x04 || data[6] != 0x00)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            connection.conformanceBlock = helper.extensions.copyOfRange(data, 7, 10);
            //int vaa = ByteBuffer.allocate(2).put(Arrays.copyOfRange(data, 12, data.Length)).getShort(0);
            byte[] aux = helper.extensions.copyOfRange(data, 12, data.Length);
            int    vaa = BitConverter.ToInt16(new byte[] { (aux[1]), (aux[0]) }, 0);

            if (parameters.referenceType == ReferenceType.LOGICAL_NAME && vaa != 0x0007)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            if (parameters.referenceType == ReferenceType.SHORT_NAME && vaa != 0xFA00)
            {
                throw new DlmsException(DlmsException.DlmsExceptionReason.MALFORMED_AARE_FRAME);
            }
            connection.maxPduSize = ((data[10] & 0xFF) << 8) | (data[11] & 0xFF);
        }