package ZtlApi;

import android.support.v4.os.EnvironmentCompat;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class Gpio {
    private static final String TAG = "GPIO";
    private String gpio_export = "/sys/class/gpio/export";
    String gpio_name;
    private String gpio_port = "/sys/class/gpio/gpio";
    private String gpio_unexport = "/sys/class/gpio/unexport";
    private boolean isGpioPortPrepared = false;
    String lastError = "";
    private File mGpioExport = null;
    private File mGpioPort = null;
    private File mGpioPortDirection = null;
    private File mGpioPortValue = null;
    private File mGpioUnExport = null;
    private int mPort;

    public boolean open(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        this.gpio_name = str;
        int gpioStringToInt = ZtlManager.GetInstance().gpioStringToInt(str);
        Log.e(TAG, "value" + gpioStringToInt);
        this.mPort = gpioStringToInt;
        this.mGpioExport = new File(this.gpio_export);
        this.mGpioUnExport = new File(this.gpio_unexport);
        boolean prepare_gpio_port = prepare_gpio_port(this.mPort);
        this.isGpioPortPrepared = prepare_gpio_port;
        return prepare_gpio_port;
    }

    public String getDirection() {
        File file;
        if (this.isGpioPortPrepared && (file = this.mGpioPortDirection) != null && file.exists()) {
            return readGpioNode(this.mGpioPortDirection);
        }
        return EnvironmentCompat.MEDIA_UNKNOWN;
    }

    public void setDirection(String str) {
        if (this.isGpioPortPrepared && this.mGpioPortDirection != null && !getDirection().equals(str)) {
            writeGpioNode(this.mGpioPortDirection, str);
        }
    }

    public int getValue() {
        String readGpioNode;
        if (!this.isGpioPortPrepared || this.mGpioPortDirection == null || (readGpioNode = readGpioNode(this.mGpioPortValue)) == null) {
            return -1;
        }
        if (readGpioNode.equals("0")) {
            return 0;
        }
        if (readGpioNode.equals("1")) {
            return 1;
        }
        try {
            return Integer.valueOf(readGpioNode).intValue();
        } catch (NumberFormatException unused) {
            return -1;
        }
    }

    public int getValue(String str) {
        if (this.isGpioPortPrepared && this.mGpioPort.exists() && this.mGpioPortDirection.exists() && readGpioNode(this.mGpioPortDirection).equals(str)) {
            return getValue();
        }
        return -1;
    }

    public void setValue(int i) {
        if (this.isGpioPortPrepared && getValue() != i) {
            writeGpioNode(this.mGpioPortValue, Integer.toString(i));
        }
    }

    public void setValue(String str, int i) {
        if (this.isGpioPortPrepared) {
            if (!getDirection().equals(str)) {
                writeGpioNode(this.mGpioPortDirection, str);
            }
            writeGpioNode(this.mGpioPortValue, Integer.toString(i));
        }
    }

    private void writeGpioNode(File file, String str) {
        if (file.exists() && file.exists()) {
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(str.getBytes(), 0, str.getBytes().length);
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                if (e.toString().contains("Permission denied")) {
                    ZtlManager GetInstance = ZtlManager.GetInstance();
                    GetInstance.execRootCmdSilent("chmod 777 " + file.getAbsolutePath());
                    Log.e(TAG, "正在申请权限");
                    writeGpioNode(file, str);
                    return;
                }
                Log.e(TAG, "writeGpioNode " + this.gpio_name + " 错误");
                e.printStackTrace();
            }
        }
    }

    private boolean prepare_gpio_port(int i) {
        if (this.mGpioExport.exists()) {
            String str = this.gpio_port + i;
            if (!ZtlManager.GetInstance().isExist(str)) {
                writeGpioNode(this.mGpioExport, Integer.toString(i));
            }
            String str2 = str + "/direction";
            String str3 = str + "/value";
            File file = new File(str);
            this.mGpioPort = file;
            if (!file.exists()) {
                if (this.gpio_name != null) {
                    Log.e(TAG, "系统没有导出" + this.gpio_name + " 请看文档或查询定昌技术支持");
                }
                this.lastError = "系统没有导出这个io口。请看文档或查询定昌技术支持" + this.mPort;
                return false;
            }
            this.mGpioPortDirection = new File(str2);
            this.mGpioPortValue = new File(str3);
        }
        if (!this.mGpioPort.exists() || !this.mGpioPortDirection.exists() || !this.mGpioPortValue.exists()) {
            return false;
        }
        return true;
    }

    public String getLastError() {
        String str = this.lastError;
        this.lastError = "";
        return str;
    }

    private boolean gpio_request() {
        return this.isGpioPortPrepared;
    }

    /* access modifiers changed from: package-private */
    public void gpio_free() {
        if (this.isGpioPortPrepared) {
            writeGpioNode(this.mGpioUnExport, Integer.toString(this.mPort));
        }
    }

    private String readGpioNode(File file) {
        String str = null;
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            str = bufferedReader.readLine();
            bufferedReader.close();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return str;
        }
    }
}
