package com.bonait.bnframework.common.helper;

import android.os.Build;
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.nio.channels.FileLock;

public class BPASerialPort {
    private static final String TAG = "BPASerialPort";
    private FileDescriptor mFd = null;
    private FileInputStream mFileInputStream = null;
    private FileOutputStream mFileOutputStream = null;
    private FileLock lock = null;
    public boolean isOpen;

    public BPASerialPort(String path, int baudrate, int nBits, char nEvent, int nStop) {
        File device = new File(path);
        if (!device.canRead() || !device.canWrite()) {
            try {
                Process su = Runtime.getRuntime().exec("/system/bin/su");
                String cmd = "chmod 666 " + device.getAbsolutePath() + "\nexit\n";
                su.getOutputStream().write(cmd.getBytes());
                if (su.waitFor() != 0 || !device.canRead() || !device.canWrite()) {
                    throw new SecurityException();
                }
            } catch (Exception var11) {
                var11.printStackTrace();
                throw new SecurityException();
            }
        }

        this.mFd = open(device.getAbsolutePath(), baudrate, nBits, nEvent, nStop);
        if (this.mFd == null) {
            Log.e("BPASerialPort", "打开串口异常");

            try {
                throw new IOException();
            } catch (IOException var10) {
                var10.printStackTrace();
            }
        } else {
            this.mFileInputStream = new FileInputStream(this.mFd);
            this.mFileOutputStream = new FileOutputStream(this.mFd);

            try {
                this.lock = this.mFileOutputStream.getChannel().tryLock();
                if (this.lock == null) {
                    Log.e("BPASerialPort", "BPASerialPort: 串口被占用");
                } else {
                    this.isOpen = true;
                }
            } catch (IOException var9) {
                var9.printStackTrace();
            }
        }

    }

    public int write(byte[] data, int len) {
        if (this.mFileOutputStream != null && data != null && len > 0) {
            try {
                this.mFileOutputStream.write(data, 0, len);
                return 0;
            } catch (IOException var4) {
                Log.e("BPASerialPort", "write: 写入数据异常");
                var4.printStackTrace();
                return -1;
            }
        } else {
            return -1;
        }
    }

    public int read(byte[] data) {
        if (this.mFileInputStream != null && data != null) {
            try {
                int len = this.mFileInputStream.read(data);
                return len;
            } catch (IOException var4) {
                Log.e("SerialPort", "read: 读取数据异常");
                var4.printStackTrace();
                return -2;
            }
        } else {
            return -2;
        }
    }

    public void clearBuffer() {
        try {
            this.mFileInputStream.skip((long)this.mFileInputStream.available());
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    public void Close() {
        this.isOpen = false;

        try {
            if (Build.VERSION.SDK_INT >= 19 && this.lock != null) {
                this.lock.close();
            }
        } catch (IOException var2) {
            var2.printStackTrace();
        }

        this.close();
    }

    private static native FileDescriptor open(String var0, int var1, int var2, char var3, int var4);

    private native void close();

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