package com.ifpdos.factorytest.mic.hid;

import android.util.Log;
import com.ifpdos.factorytest.mic.hid.jni.HidApi;

/* JADX INFO: loaded from: classes.dex */
public class HidHwApi {
    private static final String TAG = "HidApi";
    int mWriteFd = -1;
    int mReadFd = -1;

    public void close() {
        try {
            HidApi.close(this.mReadFd);
            HidApi.close(this.mWriteFd);
        } catch (Throwable th) {
            Log.e(TAG, "HidApi.open error", th);
        }
    }

    public boolean open(String str) {
        int iOpen;
        close();
        if (str == null) {
            return false;
        }
        try {
            this.mWriteFd = HidApi.open(str, 0);
            iOpen = HidApi.open(str, 1);
            this.mReadFd = iOpen;
        } catch (Throwable th) {
            Log.e(TAG, "HidApi.open error", th);
        }
        return this.mWriteFd >= 0 && iOpen >= 0;
    }

    public int write(byte[] bArr, int i) {
        try {
            return HidApi.write(this.mWriteFd, bArr, i);
        } catch (Throwable th) {
            Log.e(TAG, "HidApi.write error", th);
            return -1;
        }
    }

    public int read(byte[] bArr, int i, int i2) {
        long jCurrentTimeMillis = System.currentTimeMillis();
        if (this.mReadFd < 0) {
            return -1;
        }
        while (true) {
            int i3 = HidApi.read(this.mReadFd, bArr, i);
            if (i3 == i) {
                return i3;
            }
            if (System.currentTimeMillis() - jCurrentTimeMillis > i2) {
                Log.e(TAG, "Read timeout.");
                return i3;
            }
            try {
                Thread.sleep(1L);
            } catch (Exception unused) {
            }
        }
    }
}
