Exploit database

GH-14fb583d3d1bd2da10251bb1a37b93d6

gitchw/ghostlock-cve-2026-43499

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

168 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files25

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

workspace/decomp/HeyOTA/sources/com/heytap/accessory/BaseSocket.java

Java · 30,232 bytes
package com.heytap.accessory;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import com.heytap.accessory.api.IServiceChannelCallback;
import com.heytap.accessory.api.IServiceConnectionCallback;
import com.heytap.accessory.api.ManagerConfig;
import com.heytap.accessory.bean.GeneralException;
import com.heytap.accessory.bean.PeerAgent;
import com.heytap.accessory.bean.TrafficReport;
import com.heytap.accessory.bean.UnSupportException;
import com.heytap.accessory.constant.AFConstants;
import com.heytap.accessory.logging.SdkLog;
import com.heytap.accessory.utils.AFArraysUtils;
import com.heytap.accessory.utils.SdkConfig;
import com.heytap.accessory.utils.SystemUtils;
import com.heytap.accessory.utils.buffer.Buffer;
import com.heytap.accessory.utils.buffer.BufferException;
import com.heytap.accessory.utils.buffer.BufferPool;
import com.heytap.accessory.utils.buffer.BufferPoolImpl;
import java.io.IOException;
import java.lang.Thread;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* loaded from: classes.dex */
public abstract class BaseSocket {
    public static final int CONNECTION_LOST_DEVICE_DETACHED = 1;
    public static final int CONNECTION_LOST_PEER_DISCONNECTED = 0;
    public static final int CONNECTION_LOST_RETRANSMISSION_FAILED = 2;
    public static final int CONNECTION_LOST_UNKNOWN_REASON = 3;
    private static final String DATA_KEY = "_";
    public static final int ERROR_CANCELLED = 20008;
    public static final int ERROR_CONNECTION_ALREADY_CLOSED = 20005;
    public static final int ERROR_FATAL = 20001;
    public static final int ERROR_INTERNAL_ERROR = 20003;
    public static final int ERROR_INVALID_CHANNEL = 20006;
    public static final int ERROR_INVALID_PARAMS = 20002;
    public static final int ERROR_WRITE_TIMEDOUT = 20007;
    private static final int SOCKET_CONNECTED = 1;
    private static final int SOCKET_DISCONNECTED = 2;
    private static final int SOCKET_FORCE_CLOSED = 3;
    private static final String TAG = "BaseSocket";
    private BaseAdapter mAdapter;
    private List<Long> mAvailableChannelIdList;
    private PeerAgent mConnectedPeer;
    private String mConnectionId;
    private d mConnectionStatusCallback;
    private int mIsConnected = 2;
    private e mSocketHandler;
    private Map<String, TrafficReport> mTrafficReportMap;

    /* loaded from: classes.dex */
    private final class ServiceChannelCallback extends IServiceChannelCallback.Stub {
        /* synthetic */ ServiceChannelCallback(BaseSocket baseSocket, a aVar) {
            this();
        }

        @Override // com.heytap.accessory.api.IServiceChannelCallback
        public void onError(Bundle bundle) {
            if (bundle.containsKey(AFConstants.EXTRA_ERROR_CODE)) {
                Message obtainMessage = BaseSocket.this.mSocketHandler.obtainMessage(3);
                obtainMessage.arg1 = bundle.getInt(AFConstants.EXTRA_ERROR_CODE);
                BaseSocket.this.mSocketHandler.sendMessage(obtainMessage);
                return;
            }
            SdkLog.w(BaseSocket.TAG, "onChannelError with no error code!");
        }

        @Override // com.heytap.accessory.api.IServiceChannelCallback
        public void onRead(Bundle bundle) {
            long j = bundle.getLong(AFConstants.EXTRA_CHANNEL_ID);
            Message obtainMessage = BaseSocket.this.mSocketHandler.obtainMessage(2);
            obtainMessage.arg1 = (int) j;
            obtainMessage.obj = bundle;
            BaseSocket.this.mSocketHandler.sendMessage(obtainMessage);
        }

        private ServiceChannelCallback() {
        }
    }

    /* loaded from: classes.dex */
    private final class ServiceConnectionCallback extends IServiceConnectionCallback.Stub {
        /* synthetic */ ServiceConnectionCallback(BaseSocket baseSocket, a aVar) {
            this();
        }

        @Override // com.heytap.accessory.api.IServiceConnectionCallback
        public void onConnectionLost(Bundle bundle) {
            if (!bundle.containsKey(AFConstants.EXTRA_ERROR_CODE)) {
                SdkLog.e(BaseSocket.TAG, "onConnectionLost with no error code!");
                return;
            }
            Bundle bundle2 = new Bundle();
            bundle2.putLong(AFConstants.EXTRA_CONNNECTION_ID, bundle.getLong(AFConstants.EXTRA_CONNNECTION_ID));
            Message obtainMessage = BaseSocket.this.mSocketHandler.obtainMessage(1);
            obtainMessage.arg1 = bundle.getInt(AFConstants.EXTRA_ERROR_CODE);
            obtainMessage.obj = bundle2;
            BaseSocket.this.mSocketHandler.sendMessage(obtainMessage);
        }

        @Override // com.heytap.accessory.api.IServiceConnectionCallback
        public void onConnectionResponse(Bundle bundle) {
            Message obtainMessage = BaseSocket.this.mSocketHandler.obtainMessage(4);
            obtainMessage.arg1 = bundle.getInt(AFConstants.EXTRA_ERROR_CODE, 10012);
            obtainMessage.obj = bundle.getString(AFConstants.EXTRA_CONNNECTION_ID, null);
            obtainMessage.setData(bundle);
            BaseSocket.this.mSocketHandler.sendMessage(obtainMessage);
        }

        private ServiceConnectionCallback() {
            SdkLog.d(BaseSocket.TAG, "ServiceConnectionCallback new");
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class a implements Runnable {

        /* renamed from: a, reason: collision with root package name */
        final /* synthetic */ String f31a;

        /* renamed from: b, reason: collision with root package name */
        final /* synthetic */ PeerAgent f32b;

        a(String str, PeerAgent peerAgent) {
            this.f31a = str;
            this.f32b = peerAgent;
        }

        @Override // java.lang.Runnable
        public void run() {
            int errorCode;
            try {
                a aVar = null;
                errorCode = BaseSocket.this.mAdapter.requestServiceConnection(this.f31a, this.f32b, new ServiceConnectionCallback(BaseSocket.this, aVar), new ServiceChannelCallback(BaseSocket.this, aVar));
            } catch (GeneralException e) {
                SdkLog.e(BaseSocket.TAG, "Failed to initiate connection!", e);
                errorCode = e.getErrorCode();
            }
            if (errorCode == 0) {
                SdkLog.i(BaseSocket.TAG, "Connection request enqued successfully for peer:" + this.f32b.getAgentId());
                return;
            }
            SdkLog.i(BaseSocket.TAG, "Connection request failed for peer:" + this.f32b.getAgentId() + " Reason:" + errorCode + " Cleaning up now");
            if (BaseSocket.this.mConnectionStatusCallback != null) {
                BaseSocket.this.mConnectionStatusCallback.a(this.f32b, errorCode);
            }
            BaseSocket.this.cleanupSocket();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements Runnable {

        /* renamed from: a, reason: collision with root package name */
        final /* synthetic */ BaseAdapter f33a;

        /* renamed from: b, reason: collision with root package name */
        final /* synthetic */ String f34b;
        final /* synthetic */ PeerAgent c;

        b(BaseAdapter baseAdapter, String str, PeerAgent peerAgent) {
            this.f33a = baseAdapter;
            this.f34b = str;
            this.c = peerAgent;
        }

        @Override // java.lang.Runnable
        public void run() {
            a aVar = null;
            try {
                BaseAdapter baseAdapter = this.f33a;
                String str = this.f34b;
                PeerAgent peerAgent = this.c;
                Bundle acceptServiceConnection = baseAdapter.acceptServiceConnection(str, peerAgent, peerAgent.getTransactionId(), new ServiceConnectionCallback(BaseSocket.this, aVar), new ServiceChannelCallback(BaseSocket.this, aVar));
                String string = acceptServiceConnection.getString(AFConstants.EXTRA_CONNNECTION_ID);
                long[] longArray = acceptServiceConnection.getLongArray(AFConstants.EXTRA_CHANNEL_ID);
                int length = longArray == null ? 0 : longArray.length;
                SdkLog.d(BaseSocket.TAG, "Connection accepted successfully. connection Id:" + string + " channel Id count:" + length);
                BaseSocket.this.handleConnectionResponse(string, longArray, 0);
            } catch (GeneralException e) {
                SdkLog.e(BaseSocket.TAG, "Failed to accept service connection: " + e.getMessage());
                BaseSocket.this.handleConnectionResponse(null, null, e.getErrorCode());
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class c implements Thread.UncaughtExceptionHandler {

        /* loaded from: classes.dex */
        class a implements Runnable {

            /* renamed from: a, reason: collision with root package name */
            final /* synthetic */ Throwable f35a;

            a(c cVar, Throwable th) {
                this.f35a = th;
            }

            @Override // java.lang.Runnable
            public void run() {
                throw new RuntimeException(this.f35a);
            }
        }

        c(BaseSocket baseSocket) {
        }

        @Override // java.lang.Thread.UncaughtExceptionHandler
        public void uncaughtException(Thread thread, Throwable th) {
            SdkLog.e(BaseSocket.TAG, "Exception in Socket background thread:" + thread.getName() + "exception: " + th.getMessage());
            thread.interrupt();
            new Handler(Looper.getMainLooper()).post(new a(this, th));
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public interface d {
        void a(PeerAgent peerAgent, int i);

        void a(PeerAgent peerAgent, BaseSocket baseSocket);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static final class e extends Handler {

        /* renamed from: a, reason: collision with root package name */
        BaseSocket f36a;

        public e(BaseSocket baseSocket, Looper looper) {
            super(looper);
            this.f36a = baseSocket;
        }

        public synchronized void a() {
            super.getLooper().quit();
            this.f36a = null;
        }

        @Override // android.os.Handler
        public synchronized void handleMessage(Message message) {
            int i = message.what;
            if (i == 1) {
                Bundle bundle = (Bundle) message.obj;
                if (bundle != null) {
                    this.f36a.handleConnectionLoss(bundle.getLong(AFConstants.EXTRA_CONNNECTION_ID), message.arg1);
                } else {
                    SdkLog.e(BaseSocket.TAG, "MESSAGE_SERVICE_CONNECTION_LOSS: (bundle==null)");
                    this.f36a.handleConnectionLoss(0L, message.arg1);
                }
            } else if (i == 2) {
                this.f36a.handleIncomingData(message.arg1, (Bundle) message.obj);
            } else if (i != 4) {
                SdkLog.e(BaseSocket.TAG, "Invalid message: " + message.what);
            } else {
                Bundle data = message.getData();
                this.f36a.handleConnectionResponse((String) message.obj, data != null ? data.getLongArray(AFConstants.EXTRA_CHANNEL_ID) : null, message.arg1);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public BaseSocket(String str) {
    }

    private void cacheTrafficReport(String str, int i, TrafficReport trafficReport) {
        if (this.mTrafficReportMap == null) {
            this.mTrafficReportMap = new HashMap();
        }
        this.mTrafficReportMap.put(getDataKey(str, i), trafficReport);
    }

    private boolean checkCompressedUnSupport(PeerAgent peerAgent) {
        return peerAgent == null || peerAgent.getAccessory() == null || !peerAgent.getAccessory().supportCompression();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void cleanupSocket() {
        this.mSocketHandler.removeCallbacksAndMessages(null);
        this.mSocketHandler.a();
        SdkLog.d(TAG, "SocketHandler quit");
        unbindTransferServiceWhenDisconnect();
    }

    private static String getDataKey(String str, int i) {
        return str + DATA_KEY + i;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleConnectionLoss(long j, int i) {
        if (i == 20001) {
            this.mIsConnected = 3;
        } else {
            this.mIsConnected = 2;
        }
        onServiceConnectionLost(j, i);
        handleServiceConnectionLostErrorCode(i);
        cleanupSocket();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleConnectionResponse(String str, long[] jArr, int i) {
        if (this.mConnectionStatusCallback == null) {
            SdkLog.w(TAG, "Connection status callback not found! Ignoring response");
            return;
        }
        if (str == null) {
            SdkLog.w(TAG, "connectionId is null so cleaning up");
            this.mConnectionStatusCallback.a(this.mConnectedPeer, i);
            cleanupSocket();
            return;
        }
        this.mConnectionId = str;
        this.mIsConnected = 1;
        SdkLog.i(TAG, "onServiceConnectionResponse:" + this.mConnectedPeer);
        this.mAvailableChannelIdList = AFArraysUtils.toList(jArr);
        this.mConnectionStatusCallback.a(this.mConnectedPeer, this);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleIncomingData(int i, Bundle bundle) {
        if (this.mIsConnected != 1) {
            SdkLog.w(TAG, "Ignoring data, socket is not yet established");
            return;
        }
        byte[] byteArray = bundle.getByteArray(AFConstants.EXTRA_READ_BYTES);
        if (byteArray == null) {
            SdkLog.e(TAG, "Failed to reassemble! - null data received!");
            return;
        }
        int i2 = bundle.getInt(AFConstants.EXTRA_READ_LENGHT);
        int i3 = bundle.getInt(AFConstants.EXTRA_READ_OFFSET);
        cacheTrafficReport(this.mConnectionId, i, TrafficReport.createFromBundle(bundle));
        if (!ManagerConfig.ACCESSORY_FRAMEWORK_PACKAGE.equals(this.mAdapter.getPackageName())) {
            SdkLog.d(TAG, "handleIncomingData: " + byteArray.length + " [" + i3 + ", " + i2 + "]");
        }
        try {
            byte[] buffer = BufferPoolImpl.obtainExact(i2).getBuffer();
            SystemUtils.arraycopy(byteArray, i3, buffer, 0, i2);
            onReceive(Long.parseLong(this.mConnectionId), i, buffer);
        } catch (Exception e2) {
            e2.printStackTrace();
        } finally {
            this.mAdapter.recycle(byteArray);
        }
    }

    private void handleServiceConnectionLostErrorCode(int i) {
        if (i == 20001) {
            SdkLog.i(TAG, "onServiceConnectionLost() -> ERROR_FATAL");
            return;
        }
        if (i == 0) {
            SdkLog.i(TAG, "onServiceConnectionLost() -> CONNECTION_LOST_PEER_DISCONNECTED");
            return;
        }
        if (i == 1) {
            SdkLog.i(TAG, "onServiceConnectionLost() -> CONNECTION_LOST_DEVICE_DETACHED");
            return;
        }
        if (i == 2) {
            SdkLog.i(TAG, "onServiceConnectionLost() -> CONNECTION_LOST_RETRANSMISSION_FAILED");
            return;
        }
        if (i != 3) {
            SdkLog.w(TAG, "onServiceConnectionLost() error_code: " + i);
            return;
        }
        SdkLog.i(TAG, "onServiceConnectionLost() -> CONNECTION_LOST_UNKNOWN_REASON");
    }

    private void requestClose() {
        try {
            int closeServiceConnection = this.mAdapter.closeServiceConnection(this.mConnectionId);
            if (closeServiceConnection == 20005) {
                SdkLog.i(TAG, "Connection is already closed");
            } else if (closeServiceConnection == 0) {
                SdkLog.i(TAG, "Connection " + this.mConnectionId + " close requested successfully");
            }
        } catch (GeneralException e2) {
            SdkLog.e(TAG, "Failed to close connection!", e2);
        }
    }

    private synchronized void sendData(int i, byte[] bArr, int i2, boolean z) throws IOException {
        if (i >= 0) {
            if (this.mIsConnected != 1) {
                SdkLog.e(TAG, "Send failed. Socket already closed");
                throw new IOException("Send failed. Socket already closed");
            }
            if (bArr != null) {
                if (bArr.length != 0) {
                    if (bArr.length <= this.mConnectedPeer.getMaxAllowedDataSize()) {
                        sendDataNonFragment(i, bArr, 0, false, i2, z);
                    } else {
                        SdkLog.e(TAG, "Data too long:" + bArr.length + " , " + this.mConnectedPeer.getMaxAllowedDataSize());
                        throw new IllegalArgumentException("Data Too long! size:" + bArr.length + " Max allowed Size:" + this.mConnectedPeer.getMaxAllowedDataSize() + ". check PeerAgent.getMaxAllowedDataSize()");
                    }
                } else {
                    SdkLog.e(TAG, "sendData: data length is 0");
                    throw new IllegalArgumentException("Invalaid data length 0");
                }
            } else {
                SdkLog.e(TAG, "sendData: data is null");
                throw new IllegalArgumentException("Invalid data to send:NULL");
            }
        } else {
            SdkLog.e(TAG, "Send Failed : there is no service channel at the index");
            throw new IOException("end Failed : there is no service channel at the index");
        }
    }

    private void sendDataNonFragment(int i, byte[] bArr, int i2, boolean z, int i3, boolean z2) throws IOException {
        try {
            if (this.mIsConnected == 1) {
                Buffer obtain = BufferPool.obtain(SdkConfig.getFrameworkMaxHeaderLength() + bArr.length + i2 + SdkConfig.getFrameworkMaxFooterLength());
                obtain.setOffset(SdkConfig.getFrameworkMaxHeaderLength());
                obtain.extractFrom(bArr, 0, bArr.length);
                int send = this.mAdapter.send(new com.heytap.accessory.b(this.mConnectedPeer, this.mConnectionId, i, obtain.getBuffer(), z, obtain.getPayloadLength(), obtain.getOffset(), i3, z2));
                if (send != 0) {
                    if (send == 20005) {
                        this.mIsConnected = 2;
                        SdkLog.e(TAG, "Write failed: Connection closed");
                        throw new IOException("Write failed:Connection already closed");
                    }
                    if (send == 20008) {
                        SdkLog.e(TAG, "write failed: user cancelled");
                        return;
                    }
                    if (send != 20006) {
                        if (send != 20007) {
                            return;
                        }
                        SdkLog.e(TAG, "Write failed: Timed out!");
                        close();
                        throw new IOException("Write failed: Timed out!");
                    }
                    SdkLog.e(TAG, "Write failed. Attempt to write on invalid channel:" + i);
                    throw new IllegalArgumentException("Write failed. Attempt to write on invalid channel:" + i);
                }
                return;
            }
            SdkLog.w(TAG, "Data send failed, connection closed!");
            throw new IOException("Failed to send, connection closed!");
        } catch (GeneralException e2) {
            SdkLog.e(TAG, "Send failed!", e2);
            throw new IOException("Send Failed", e2);
        } catch (BufferException e3) {
            SdkLog.e(TAG, "BufferException: " + e3.getLocalizedMessage());
        }
    }

    private boolean startSocketHandler(String str, String str2) {
        HandlerThread handlerThread = new HandlerThread("Socket:" + str + DATA_KEY + str2);
        handlerThread.setUncaughtExceptionHandler(new c(this));
        String str3 = TAG;
        SdkLog.d(str3, "socketHandlerThread start");
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        if (looper == null) {
            SdkLog.e(str3, "Failed get Looper for Socket: initiator:" + str + " Peer Id:" + str2);
            return false;
        }
        this.mSocketHandler = new e(this, looper);
        return true;
    }

    private void unbindTransferServiceWhenDisconnect() {
        String canonicalName = getClass().getCanonicalName();
        if (this.mAdapter != null) {
            if (BaseAdapter.INTERNAL_FILE_CONSUMER.equals(canonicalName) || BaseAdapter.INTERNAL_STREAM_CONSUMER.equals(canonicalName)) {
                this.mAdapter.unbindTransferService();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void acceptServiceConnection(String str, PeerAgent peerAgent, BaseAdapter baseAdapter, d dVar) {
        this.mConnectedPeer = peerAgent;
        this.mAdapter = baseAdapter;
        this.mConnectionStatusCallback = dVar;
        startSocketHandler(str, peerAgent.getAgentId());
        this.mSocketHandler.post(new b(baseAdapter, str, peerAgent));
    }

    public void cleanupChannel(String str, int i) {
        try {
            this.mAdapter.cleanupChannel(str, i);
        } catch (GeneralException e2) {
            SdkLog.e(TAG, "cleanupChannel failed." + i, e2);
        }
    }

    public void close() {
        if (this.mIsConnected == 1) {
            this.mIsConnected = 2;
            SdkLog.i(TAG, this.mAdapter.getPackageName() + " requested to close socket for Peer:" + this.mConnectedPeer.getAgentId());
            requestClose();
            return;
        }
        SdkLog.i(TAG, "Connection is already closed");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void forceClose() {
        if (this.mIsConnected == 1) {
            this.mIsConnected = 3;
            Message obtainMessage = this.mSocketHandler.obtainMessage(1);
            obtainMessage.arg1 = 20001;
            Bundle bundle = new Bundle();
            try {
                bundle.putLong(AFConstants.EXTRA_CONNNECTION_ID, Long.parseLong(this.mConnectionId));
            } catch (NumberFormatException e2) {
                SdkLog.w(TAG, e2);
            }
            obtainMessage.obj = bundle;
            this.mSocketHandler.sendMessage(obtainMessage);
            SdkLog.i(TAG, "Socket:" + this.mConnectionId + " has been force closed!");
        }
    }

    public PeerAgent getConnectedPeerAgent() {
        return this.mConnectedPeer;
    }

    public String getConnectionId() {
        return this.mConnectionId;
    }

    public int getServiceChannelId(int i) {
        if (this.mAvailableChannelIdList == null) {
            SdkLog.e(TAG, "Failed because Service Profile is null");
            return -1;
        }
        if (i >= 0 && i < getServiceChannelSize()) {
            return this.mAvailableChannelIdList.get(i).intValue();
        }
        SdkLog.e(TAG, "Failed because of wrong index");
        return -1;
    }

    public int getServiceChannelSize() {
        List<Long> list = this.mAvailableChannelIdList;
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public TrafficReport getTrafficReport(String str, int i) {
        if (this.mTrafficReportMap == null) {
            return null;
        }
        return this.mTrafficReportMap.get(getDataKey(str, i));
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void initiateServiceconnection(String str, PeerAgent peerAgent, BaseAdapter baseAdapter, d dVar) {
        this.mConnectedPeer = peerAgent;
        this.mConnectionStatusCallback = dVar;
        this.mAdapter = baseAdapter;
        startSocketHandler(str, peerAgent.getAgentId());
        this.mSocketHandler.post(new a(str, peerAgent));
    }

    public boolean isConnected() {
        return this.mIsConnected == 1;
    }

    public abstract void onError(int i, String str, int i2);

    public abstract void onReceive(long j, int i, byte[] bArr);

    protected abstract void onServiceConnectionLost(long j, int i);

    public synchronized void secureSend(int i, byte[] bArr) throws IOException {
        if (i < 0) {
            SdkLog.e(TAG, "Send Failed : there is no service channel at the index");
        } else {
            if (this.mIsConnected != 1) {
                throw new IOException("Secure Send failed. Socket already closed");
            }
            if (bArr != null) {
                if (bArr.length != 0) {
                    if (bArr.length <= this.mConnectedPeer.getMaxAllowedDataSize()) {
                        SdkLog.d(TAG, "Sending data:" + bArr.length + " bytes");
                        sendDataNonFragment(i, bArr, getConnectedPeerAgent().getAccessory().getEncryptionPaddingLength(), true, 3, false);
                    } else {
                        SdkLog.e(TAG, "SecureSend:Data too long:" + bArr.length);
                        throw new IllegalArgumentException("Secure send:Data Too long! size:" + bArr.length + " Max allowed Size:" + this.mConnectedPeer.getMaxAllowedDataSize() + ". check PeerAgent.getMaxAllowedDataSize()");
                    }
                } else {
                    SdkLog.e(TAG, "SecureSend: data length is 0");
                    throw new IllegalArgumentException("Invalaid data length 0");
                }
            } else {
                SdkLog.e(TAG, "secureSend: data is null");
                throw new IllegalArgumentException("Invalid data to send:NULL");
            }
        }
    }

    public void send(int i, byte[] bArr) throws IOException {
        sendData(i, bArr, 3, false);
    }

    public void sendAlign(int i, byte[] bArr) throws IOException {
        sendData(i, bArr, 1, true);
    }

    public void sendCompressed(int i, byte[] bArr) throws IOException, UnSupportException {
        if (!checkCompressedUnSupport(getConnectedPeerAgent())) {
            sendData(i, bArr, 1, false);
        } else {
            SdkLog.i(TAG, "current peer is note supported compression");
            throw new UnSupportException("the peer agent doesn't support the compression feature, please check");
        }
    }

    public void sendUncompressed(int i, byte[] bArr) throws IOException {
        sendData(i, bArr, 2, false);
    }

    public void send(int i, Buffer buffer) throws IOException {
        sendData(i, buffer, 3, false);
    }

    public void sendAlign(int i, Buffer buffer) throws IOException {
        sendData(i, buffer, 3, true);
    }

    public void sendUncompressed(int i, Buffer buffer) throws IOException {
        sendData(i, buffer, 2, false);
    }

    public void sendCompressed(int i, Buffer buffer) throws IOException {
        sendData(i, buffer, 1, false);
    }

    private void sendData(int i, Buffer buffer, int i2, boolean z) throws IOException {
        if (i < 0) {
            SdkLog.e(TAG, "Send Failed : there is no service channel at the index");
            return;
        }
        if (this.mIsConnected != 1) {
            throw new IOException("Send failed. Socket already closed");
        }
        if (buffer != null) {
            if (buffer.getBuffer().length != 0) {
                if (buffer.getBuffer().length <= this.mConnectedPeer.getMaxAllowedDataSize()) {
                    try {
                        if (this.mIsConnected == 1) {
                            int send = this.mAdapter.send(new com.heytap.accessory.b(this.mConnectedPeer, this.mConnectionId, i, buffer.getBuffer(), false, buffer.getPayloadLength(), buffer.getOffset(), i2, z));
                            if (send != 0) {
                                if (send == 20005) {
                                    this.mIsConnected = 2;
                                    SdkLog.e(TAG, "Write failed: Connection closed");
                                    throw new IOException("Write failed:Connection already closed");
                                }
                                if (send == 20006) {
                                    SdkLog.e(TAG, "Write failed. Attempt to write on invalid channel:" + i);
                                    throw new IllegalArgumentException("Write failed. Attempt to write on invalid channel:" + i);
                                }
                                if (send != 20007) {
                                    SdkLog.e(TAG, "Write failed. status:" + send);
                                    return;
                                }
                                SdkLog.e(TAG, "Write failed: Timed out!");
                                close();
                                throw new IOException("Write failed: Timed out!");
                            }
                            return;
                        }
                        SdkLog.w(TAG, "Data send failed, connection closed!");
                        throw new IOException("Failed to send, connection closed!");
                    } catch (GeneralException e2) {
                        SdkLog.e(TAG, "Send failed!", e2);
                        throw new IOException("Send Failed", e2);
                    }
                }
                SdkLog.e(TAG, "Data too long:" + buffer.getBuffer().length);
                throw new IllegalArgumentException("Data Too long! size:" + buffer.getBuffer().length + " Max allowed Size:" + this.mConnectedPeer.getMaxAllowedDataSize() + ". check PeerAgent.getMaxAllowedDataSize()");
            }
            SdkLog.e(TAG, "sendData: data length is 0");
            throw new IllegalArgumentException("Invalaid data length 0");
        }
        SdkLog.e(TAG, "sendData: data is null");
        throw new IllegalArgumentException("Invalid data to send:NULL");
    }
}