All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmuc.josistack.internal.presentation.asn1.CPAPPDU Maven / Gradle / Ivy

Go to download

OpenIEC61850 is a library implementing the IEC 61850 MMS communication standard (client and server).

The newest version!
/**
 * This class file was automatically generated by jASN1 v1.9.0 (http://www.openmuc.org)
 */

package org.openmuc.josistack.internal.presentation.asn1;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import org.openmuc.jasn1.ber.BerLength;
import org.openmuc.jasn1.ber.BerTag;
import org.openmuc.jasn1.ber.ReverseByteArrayOutputStream;

public class CPAPPDU implements Serializable {

    private static final long serialVersionUID = 1L;

    public static class NormalModeParameters implements Serializable {

        private static final long serialVersionUID = 1L;

        public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 16);

        public byte[] code = null;
        private ProtocolVersion protocolVersion = null;
        private RespondingPresentationSelector respondingPresentationSelector = null;
        private PresentationContextDefinitionResultList presentationContextDefinitionResultList = null;
        private PresentationRequirements presentationRequirements = null;
        private UserSessionRequirements userSessionRequirements = null;
        private UserData userData = null;

        public NormalModeParameters() {
        }

        public NormalModeParameters(byte[] code) {
            this.code = code;
        }

        public void setProtocolVersion(ProtocolVersion protocolVersion) {
            this.protocolVersion = protocolVersion;
        }

        public ProtocolVersion getProtocolVersion() {
            return protocolVersion;
        }

        public void setRespondingPresentationSelector(RespondingPresentationSelector respondingPresentationSelector) {
            this.respondingPresentationSelector = respondingPresentationSelector;
        }

        public RespondingPresentationSelector getRespondingPresentationSelector() {
            return respondingPresentationSelector;
        }

        public void setPresentationContextDefinitionResultList(
                PresentationContextDefinitionResultList presentationContextDefinitionResultList) {
            this.presentationContextDefinitionResultList = presentationContextDefinitionResultList;
        }

        public PresentationContextDefinitionResultList getPresentationContextDefinitionResultList() {
            return presentationContextDefinitionResultList;
        }

        public void setPresentationRequirements(PresentationRequirements presentationRequirements) {
            this.presentationRequirements = presentationRequirements;
        }

        public PresentationRequirements getPresentationRequirements() {
            return presentationRequirements;
        }

        public void setUserSessionRequirements(UserSessionRequirements userSessionRequirements) {
            this.userSessionRequirements = userSessionRequirements;
        }

        public UserSessionRequirements getUserSessionRequirements() {
            return userSessionRequirements;
        }

        public void setUserData(UserData userData) {
            this.userData = userData;
        }

        public UserData getUserData() {
            return userData;
        }

        public int encode(OutputStream os) throws IOException {
            return encode(os, true);
        }

        public int encode(OutputStream os, boolean withTag) throws IOException {

            if (code != null) {
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
                if (withTag) {
                    return tag.encode(os) + code.length;
                }
                return code.length;
            }

            int codeLength = 0;
            if (userData != null) {
                codeLength += userData.encode(os);
            }

            if (userSessionRequirements != null) {
                codeLength += userSessionRequirements.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 9
                os.write(0x89);
                codeLength += 1;
            }

            if (presentationRequirements != null) {
                codeLength += presentationRequirements.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 8
                os.write(0x88);
                codeLength += 1;
            }

            if (presentationContextDefinitionResultList != null) {
                codeLength += presentationContextDefinitionResultList.encode(os, false);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 5
                os.write(0xA5);
                codeLength += 1;
            }

            if (respondingPresentationSelector != null) {
                codeLength += respondingPresentationSelector.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 3
                os.write(0x83);
                codeLength += 1;
            }

            if (protocolVersion != null) {
                codeLength += protocolVersion.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 0
                os.write(0x80);
                codeLength += 1;
            }

            codeLength += BerLength.encodeLength(os, codeLength);

            if (withTag) {
                codeLength += tag.encode(os);
            }

            return codeLength;

        }

        public int decode(InputStream is) throws IOException {
            return decode(is, true);
        }

        public int decode(InputStream is, boolean withTag) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerTag berTag = new BerTag();

            if (withTag) {
                codeLength += tag.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);

            int totalLength = length.val;
            codeLength += totalLength;

            if (totalLength == 0) {
                return codeLength;
            }
            subCodeLength += berTag.decode(is);
            if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 0)) {
                protocolVersion = new ProtocolVersion();
                subCodeLength += protocolVersion.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berTag.decode(is);
            }

            if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 3)) {
                respondingPresentationSelector = new RespondingPresentationSelector();
                subCodeLength += respondingPresentationSelector.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berTag.decode(is);
            }

            if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 5)) {
                presentationContextDefinitionResultList = new PresentationContextDefinitionResultList();
                subCodeLength += presentationContextDefinitionResultList.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berTag.decode(is);
            }

            if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 8)) {
                presentationRequirements = new PresentationRequirements();
                subCodeLength += presentationRequirements.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berTag.decode(is);
            }

            if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 9)) {
                userSessionRequirements = new UserSessionRequirements();
                subCodeLength += userSessionRequirements.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berTag.decode(is);
            }

            userData = new UserData();
            int choiceDecodeLength = userData.decode(is, berTag);
            subCodeLength += choiceDecodeLength;
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            throw new IOException("Unexpected end of sequence, length tag: " + totalLength
                    + ", actual sequence length: " + subCodeLength);

        }

        public void encodeAndSave(int encodingSizeGuess) throws IOException {
            ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(encodingSizeGuess);
            encode(os, false);
            code = os.getArray();
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            appendAsString(sb, 0);
            return sb.toString();
        }

        public void appendAsString(StringBuilder sb, int indentLevel) {

            sb.append("{");
            boolean firstSelectedElement = true;
            if (protocolVersion != null) {
                sb.append("\n");
                for (int i = 0; i < indentLevel + 1; i++) {
                    sb.append("\t");
                }
                sb.append("protocolVersion: ").append(protocolVersion);
                firstSelectedElement = false;
            }

            if (respondingPresentationSelector != null) {
                if (!firstSelectedElement) {
                    sb.append(",\n");
                }
                for (int i = 0; i < indentLevel + 1; i++) {
                    sb.append("\t");
                }
                sb.append("respondingPresentationSelector: ").append(respondingPresentationSelector);
                firstSelectedElement = false;
            }

            if (presentationContextDefinitionResultList != null) {
                if (!firstSelectedElement) {
                    sb.append(",\n");
                }
                for (int i = 0; i < indentLevel + 1; i++) {
                    sb.append("\t");
                }
                sb.append("presentationContextDefinitionResultList: ");
                presentationContextDefinitionResultList.appendAsString(sb, indentLevel + 1);
                firstSelectedElement = false;
            }

            if (presentationRequirements != null) {
                if (!firstSelectedElement) {
                    sb.append(",\n");
                }
                for (int i = 0; i < indentLevel + 1; i++) {
                    sb.append("\t");
                }
                sb.append("presentationRequirements: ").append(presentationRequirements);
                firstSelectedElement = false;
            }

            if (userSessionRequirements != null) {
                if (!firstSelectedElement) {
                    sb.append(",\n");
                }
                for (int i = 0; i < indentLevel + 1; i++) {
                    sb.append("\t");
                }
                sb.append("userSessionRequirements: ").append(userSessionRequirements);
                firstSelectedElement = false;
            }

            if (userData != null) {
                if (!firstSelectedElement) {
                    sb.append(",\n");
                }
                for (int i = 0; i < indentLevel + 1; i++) {
                    sb.append("\t");
                }
                sb.append("userData: ");
                userData.appendAsString(sb, indentLevel + 1);
                firstSelectedElement = false;
            }

            sb.append("\n");
            for (int i = 0; i < indentLevel; i++) {
                sb.append("\t");
            }
            sb.append("}");
        }

    }

    public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 17);

    public byte[] code = null;
    private ModeSelector modeSelector = null;
    private NormalModeParameters normalModeParameters = null;

    public CPAPPDU() {
    }

    public CPAPPDU(byte[] code) {
        this.code = code;
    }

    public void setModeSelector(ModeSelector modeSelector) {
        this.modeSelector = modeSelector;
    }

    public ModeSelector getModeSelector() {
        return modeSelector;
    }

    public void setNormalModeParameters(NormalModeParameters normalModeParameters) {
        this.normalModeParameters = normalModeParameters;
    }

    public NormalModeParameters getNormalModeParameters() {
        return normalModeParameters;
    }

    public int encode(OutputStream os) throws IOException {
        return encode(os, true);
    }

    public int encode(OutputStream os, boolean withTag) throws IOException {

        if (code != null) {
            for (int i = code.length - 1; i >= 0; i--) {
                os.write(code[i]);
            }
            if (withTag) {
                return tag.encode(os) + code.length;
            }
            return code.length;
        }

        int codeLength = 0;
        if (normalModeParameters != null) {
            codeLength += normalModeParameters.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
            os.write(0xA2);
            codeLength += 1;
        }

        codeLength += modeSelector.encode(os, false);
        // write tag: CONTEXT_CLASS, CONSTRUCTED, 0
        os.write(0xA0);
        codeLength += 1;

        codeLength += BerLength.encodeLength(os, codeLength);

        if (withTag) {
            codeLength += tag.encode(os);
        }

        return codeLength;

    }

    public int decode(InputStream is) throws IOException {
        return decode(is, true);
    }

    public int decode(InputStream is, boolean withTag) throws IOException {
        int codeLength = 0;
        int subCodeLength = 0;
        BerTag berTag = new BerTag();

        if (withTag) {
            codeLength += tag.decodeAndCheck(is);
        }

        BerLength length = new BerLength();
        codeLength += length.decode(is);

        int totalLength = length.val;
        while (subCodeLength < totalLength) {
            subCodeLength += berTag.decode(is);
            if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
                modeSelector = new ModeSelector();
                subCodeLength += modeSelector.decode(is, false);
            }
            else if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 2)) {
                normalModeParameters = new NormalModeParameters();
                subCodeLength += normalModeParameters.decode(is, false);
            }
        }
        if (subCodeLength != totalLength) {
            throw new IOException("Length of set does not match length tag, length tag: " + totalLength
                    + ", actual set length: " + subCodeLength);

        }
        codeLength += subCodeLength;

        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws IOException {
        ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(encodingSizeGuess);
        encode(os, false);
        code = os.getArray();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        appendAsString(sb, 0);
        return sb.toString();
    }

    public void appendAsString(StringBuilder sb, int indentLevel) {

        sb.append("{");
        sb.append("\n");
        for (int i = 0; i < indentLevel + 1; i++) {
            sb.append("\t");
        }
        if (modeSelector != null) {
            sb.append("modeSelector: ");
            modeSelector.appendAsString(sb, indentLevel + 1);
        }
        else {
            sb.append("modeSelector: ");
        }

        if (normalModeParameters != null) {
            sb.append(",\n");
            for (int i = 0; i < indentLevel + 1; i++) {
                sb.append("\t");
            }
            sb.append("normalModeParameters: ");
            normalModeParameters.appendAsString(sb, indentLevel + 1);
        }

        sb.append("\n");
        for (int i = 0; i < indentLevel; i++) {
            sb.append("\t");
        }
        sb.append("}");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy