package org.eclipse.paho.mqttv5.common.packet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;

/* JADX INFO: loaded from: classes2.dex */
public class MqttConnect extends MqttWireMessage {
    public static final String KEY = "Con";
    private static final Byte[] validProperties = {(byte) 17, (byte) 24, (byte) 33, (byte) 39, (byte) 34, (byte) 25, (byte) 23, (byte) 38, (byte) 21, (byte) 22};
    private static final Byte[] validWillProperties = {(byte) 24, (byte) 1, (byte) 2, (byte) 8, (byte) 9, (byte) 38, (byte) 3};
    private boolean cleanStart;
    private String clientId;
    private byte info;
    private int keepAliveInterval;
    private int mqttVersion;
    private byte[] password;
    private MqttProperties properties;
    private boolean reservedByte;
    private String userName;
    private String willDestination;
    private MqttMessage willMessage;
    private MqttProperties willProperties;

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte getMessageInfo() {
        return (byte) 0;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public boolean isMessageIdRequired() {
        return false;
    }

    public MqttConnect(byte b, byte[] bArr) throws MqttException, IOException {
        super((byte) 1);
        this.mqttVersion = 5;
        this.info = b;
        this.properties = new MqttProperties(validProperties);
        this.willProperties = new MqttProperties(validWillProperties);
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bArr));
        if (!MqttDataTypes.decodeUTF8(dataInputStream).equalsIgnoreCase("MQTT")) {
            throw new MqttPacketException(MqttPacketException.PACKET_CONNECT_ERROR_UNSUPPORTED_PROTOCOL_NAME);
        }
        byte b2 = dataInputStream.readByte();
        this.mqttVersion = b2;
        if (b2 != 5) {
            throw new MqttPacketException(51001);
        }
        byte b3 = dataInputStream.readByte();
        boolean z = (b3 & 1) != 0;
        this.reservedByte = z;
        this.cleanStart = (b3 & 2) != 0;
        boolean z2 = (b3 & 4) != 0;
        int i = (b3 >> 3) & 3;
        boolean z3 = (b3 & 32) != 0;
        boolean z4 = (b3 & 64) != 0;
        boolean z5 = (b3 & 128) != 0;
        if (z) {
            throw new MqttPacketException(51002);
        }
        this.keepAliveInterval = dataInputStream.readUnsignedShort();
        this.properties.decodeProperties(dataInputStream);
        this.clientId = MqttDataTypes.decodeUTF8(dataInputStream);
        if (z2) {
            this.willProperties.decodeProperties(dataInputStream);
            if (i == 3) {
                throw new MqttPacketException(MqttPacketException.PACKET_CONNECT_ERROR_INVALID_WILL_QOS);
            }
            this.willDestination = MqttDataTypes.decodeUTF8(dataInputStream);
            int i2 = dataInputStream.readShort();
            byte[] bArr2 = new byte[i2];
            dataInputStream.read(bArr2, 0, i2);
            MqttMessage mqttMessage = new MqttMessage(bArr2);
            this.willMessage = mqttMessage;
            mqttMessage.setQos(i);
            this.willMessage.setRetained(z3);
        }
        if (z5) {
            this.userName = MqttDataTypes.decodeUTF8(dataInputStream);
        }
        if (z4) {
            int i3 = dataInputStream.readShort();
            byte[] bArr3 = new byte[i3];
            this.password = bArr3;
            dataInputStream.read(bArr3, 0, i3);
        }
        dataInputStream.close();
    }

    public MqttConnect(String str, int i, boolean z, int i2, MqttProperties mqttProperties, MqttProperties mqttProperties2) {
        super((byte) 1);
        this.clientId = str;
        this.mqttVersion = i;
        this.cleanStart = z;
        this.keepAliveInterval = i2;
        if (mqttProperties != null) {
            this.properties = mqttProperties;
        } else {
            this.properties = new MqttProperties();
        }
        this.properties.setValidProperties(validProperties);
        this.willProperties = mqttProperties2;
        mqttProperties2.setValidProperties(validWillProperties);
    }

    public boolean isCleanStart() {
        return this.cleanStart;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte[] getVariableHeader() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            MqttDataTypes.encodeUTF8(dataOutputStream, "MQTT");
            dataOutputStream.write(this.mqttVersion);
            byte qos = this.cleanStart ? (byte) 2 : (byte) 0;
            MqttMessage mqttMessage = this.willMessage;
            if (mqttMessage != null) {
                qos = (byte) (((byte) (qos | 4)) | (mqttMessage.getQos() << 3));
                if (this.willMessage.isRetained()) {
                    qos = (byte) (qos | 32);
                }
            }
            if (this.userName != null) {
                qos = (byte) (qos | 128);
            }
            if (this.password != null) {
                qos = (byte) (qos | 64);
            }
            dataOutputStream.write(qos);
            dataOutputStream.writeShort(this.keepAliveInterval);
            dataOutputStream.write(this.properties.encodeProperties());
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public byte[] getPayload() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            MqttDataTypes.encodeUTF8(dataOutputStream, this.clientId);
            if (this.willMessage != null) {
                dataOutputStream.write(this.willProperties.encodeProperties());
                MqttDataTypes.encodeUTF8(dataOutputStream, this.willDestination);
                dataOutputStream.writeShort(this.willMessage.getPayload().length);
                dataOutputStream.write(this.willMessage.getPayload());
            }
            String str = this.userName;
            if (str != null) {
                MqttDataTypes.encodeUTF8(dataOutputStream, str);
            }
            byte[] bArr = this.password;
            if (bArr != null) {
                dataOutputStream.writeShort(bArr.length);
                dataOutputStream.write(this.password);
            }
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public String getKey() {
        return "Con";
    }

    public void setWillMessage(MqttMessage mqttMessage) {
        this.willMessage = mqttMessage;
    }

    public void setUserName(String str) {
        this.userName = str;
    }

    public void setPassword(byte[] bArr) {
        this.password = bArr;
    }

    public void setWillDestination(String str) {
        this.willDestination = str;
    }

    public byte getInfo() {
        return this.info;
    }

    public String getClientId() {
        return this.clientId;
    }

    public MqttMessage getWillMessage() {
        return this.willMessage;
    }

    public String getUserName() {
        return this.userName;
    }

    public byte[] getPassword() {
        return this.password;
    }

    public int getKeepAliveInterval() {
        return this.keepAliveInterval;
    }

    public String getWillDestination() {
        return this.willDestination;
    }

    public int getMqttVersion() {
        return this.mqttVersion;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public MqttProperties getProperties() {
        return this.properties;
    }

    public MqttProperties getWillProperties() {
        return this.willProperties;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public String toString() {
        return "MqttConnect [properties=" + this.properties + ", willProperties=" + this.willProperties + ", info=" + ((int) this.info) + ", clientId=" + this.clientId + ", reservedByte=" + this.reservedByte + ", cleanStart=" + this.cleanStart + ", willMessage=" + this.willMessage + ", userName=" + this.userName + ", password=" + Arrays.toString(this.password) + ", keepAliveInterval=" + this.keepAliveInterval + ", willDestination=" + this.willDestination + ", mqttVersion=" + this.mqttVersion + "]";
    }
}
