package com.tencent.usb;

import android.util.Log;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/* JADX INFO: loaded from: classes.dex */
public final class UsbSerialPortDevice {
    public static final String DEFAULT_SU_PATH = "/system/xbin/su";
    private static final String TAG = "SerialPortDevice";
    private static String sSuPath = "/system/xbin/su";
    private int baudrate;
    private int dataBits;
    private File device;
    private int flags;
    private FileDescriptor mFd;
    private FileInputStream mFileInputStream;
    private FileOutputStream mFileOutputStream;
    private int parity;
    private int stopBits;

    static {
        System.loadLibrary("SerialPort");
    }

    public UsbSerialPortDevice(File file, int i2) {
        this(file, i2, 8, 0, 1, 0);
    }

    public UsbSerialPortDevice(File file, int i2, int i3, int i4, int i5) {
        this(file, i2, i3, i4, i5, 0);
    }

    public UsbSerialPortDevice(File file, int i2, int i3, int i4, int i5, int i6) throws IOException {
        this.device = file;
        this.baudrate = i2;
        this.dataBits = i3;
        this.parity = i4;
        this.stopBits = i5;
        this.flags = i6;
        if (!file.canRead() || !file.canWrite()) {
            try {
                Process processExec = Runtime.getRuntime().exec(sSuPath);
                processExec.getOutputStream().write(("chmod 666 " + file.getAbsolutePath() + "\nexit\n").getBytes());
                if (processExec.waitFor() != 0 || !file.canRead() || !file.canWrite()) {
                    throw new SecurityException();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                throw new SecurityException();
            }
        }
        FileDescriptor fileDescriptorOpenDevNative = openDevNative(file.getAbsolutePath(), i2, i3, i4, i5, i6);
        this.mFd = fileDescriptorOpenDevNative;
        if (fileDescriptorOpenDevNative == null) {
            Log.e(TAG, "native open returns null");
            throw new IOException();
        }
        this.mFileInputStream = new FileInputStream(this.mFd);
        this.mFileOutputStream = new FileOutputStream(this.mFd);
    }

    private native int closeDevNative();

    public static String getSuPath() {
        return sSuPath;
    }

    private native FileDescriptor openDevNative(String str, int i2, int i3, int i4, int i5, int i6);

    public static void setSuPath(String str) {
        if (str == null) {
            return;
        }
        sSuPath = str;
    }

    public void close() {
        closeDevNative();
    }

    public int getBaudrate() {
        return this.baudrate;
    }

    public int getDataBits() {
        return this.dataBits;
    }

    public File getDevice() {
        return this.device;
    }

    public int getFlags() {
        return this.flags;
    }

    public InputStream getInputStream() {
        return this.mFileInputStream;
    }

    public OutputStream getOutputStream() {
        return this.mFileOutputStream;
    }

    public int getParity() {
        return this.parity;
    }

    public int getStopBits() {
        return this.stopBits;
    }
}
