Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is a shell archive. Save it in a file, remove anything before
- # this line, and then unpack it by entering "sh file". Note, it may
- # create directories; files and directories will be owned by you and
- # have default permissions.
- #
- # This archive contains:
- #
- # BBGPIOFactory.java
- # GPIO.java
- # GPIOFactory.java
- # I2CBus.java
- # I2CDevice.java
- # POSIX.java
- #
- echo x - BBGPIOFactory.java
- sed 's/^X//' >BBGPIOFactory.java << 'END-of-BBGPIOFactory.java'
- Ximport java.util.*;
- Ximport java.io.*;
- X
- X/**
- X * GPIOFactory for BeagleBone Black with the GPIO device-tree overlays from
- X * https://github.com/nomel/beaglebone/tree/master/gpio-header. It is assumed
- X * the overlay for the required GPIO has been loaded if required - this factory
- X * won't do that.
- X */
- Xclass BBGPIOFactory extends GPIOFactory {
- X
- X private static String CAPEMGRFILE, DTODIR;
- X
- X private static final String[] gpionames = {
- X null, // 0
- X "P8.25", // 1
- X "P9.22", // 2
- X "P9.21", // 3
- X "P9.18", // 4
- X "P9.17", // 5
- X null, // 6
- X "P9.42", // 7
- X "P8.35", // 8
- X "P8.33", // 9
- X "P8.31", // 10
- X "P8.32", // 11
- X "P9.20", // 12
- X "P9.19", // 13
- X "P9.26", // 14
- X "P9.24", // 15
- X null, // 16
- X null, // 17
- X null, // 18
- X null, // 19
- X "P9.41", // 20
- X null, // 21
- X "P8.19", // 22
- X "P8.13", // 23
- X null, // 24
- X null, // 25
- X "P8.14", // 26
- X "P8.17", // 27
- X null, // 28
- X null, // 29
- X "P9.11", // 30
- X "P9.13", // 31
- X null, // 32
- X "P8.24", // 33
- X "P8.5", // 34
- X "P8.6", // 35
- X "P8.23", // 36
- X "P8.22", // 37
- X "P8.3", // 38
- X "P8.4", // 39
- X null, // 40
- X null, // 41
- X null, // 42
- X null, // 43
- X "P8.12", // 44
- X "P8.11", // 45
- X "P8.16", // 46
- X "P8.15", // 47
- X "P9.15", // 48
- X "P9.23", // 49
- X "P9.14", // 50
- X "P9.16", // 51
- X null, // 52
- X null, // 53
- X null, // 54
- X null, // 55
- X null, // 56
- X null, // 57
- X null, // 58
- X null, // 59
- X "P9.12", // 60
- X "P8.26", // 61
- X "P8.21", // 62
- X "P8.20", // 63
- X null, // 64
- X "P8.18", // 65
- X "P8.7", // 66
- X "P8.8", // 67
- X "P8.10", // 68
- X "P8.9", // 69
- X "P8.45", // 70
- X "P8.46", // 71
- X "P8.43", // 72
- X "P8.44", // 73
- X "P8.41", // 74
- X "P8.42", // 75
- X "P8.39", // 76
- X "P8.40", // 77
- X "P8.37", // 78
- X "P8.38", // 79
- X "P8.36", // 80
- X "P8.34", // 81
- X null, // 82
- X null, // 83
- X null, // 84
- X null, // 85
- X "P8.27", // 86
- X "P8.29", // 87
- X "P8.28", // 88
- X "P8.30", // 89
- X null, // 90
- X null, // 91
- X null, // 92
- X null, // 93
- X null, // 94
- X null, // 95
- X null, // 96
- X null, // 97
- X null, // 98
- X null, // 99
- X null, // 100
- X null, // 101
- X null, // 102
- X null, // 103
- X null, // 104
- X null, // 105
- X null, // 106
- X null, // 107
- X null, // 108
- X null, // 109
- X "P9.31", // 110
- X "P9.29", // 111
- X "P9.30", // 112
- X "P9.28", // 113
- X "P9.42b", // 114
- X "P9.27", // 115
- X "P9.41b", // 116
- X "P9.25", // 117
- X };
- X
- X synchronized static boolean canCreate() {
- X // Do we need to check more?
- X if (CAPEMGRFILE == null) {
- X File f = new File("/sys/devices");
- X File[] sf = f.listFiles();
- X for (int i=0;i<sf.length;i++) {
- X if (CAPEMGRFILE == null && sf[i].getName().startsWith("bone_capemgr.") && f.isDirectory()) {
- X CAPEMGRFILE = sf[i].getAbsolutePath() + "/slots";
- X } else if (sf[i].getName().startsWith("ocp.") && f.isDirectory()) {
- X DTODIR = sf[i].getAbsolutePath();
- X }
- X }
- X if (CAPEMGRFILE == null || DTODIR == null) {
- X return false;
- X }
- X }
- X return true;
- X }
- X
- X private static void writeToFile(String file, String data) throws IOException {
- X Writer out = new OutputStreamWriter(new FileOutputStream(file), "ISO-8859-1");
- X out.write(data);
- X out.close();
- X }
- X
- X public GPIO open(int gpio, GPIO.Mode mode) throws IOException {
- X if (gpio > gpionames.length || gpio < 0 || gpionames[gpio] == null) {
- X throw new IllegalArgumentException(Integer.toString(gpio));
- X }
- X File[] f = new File(DTODIR).listFiles();
- X String prefix = "gpio-"+gpionames[gpio]+"_gpio"+gpio;
- X boolean found = false;
- X for (int i=0;!found && i<f.length;i++) {
- X found = f[i].getName().startsWith(prefix);
- X }
- X if (!found) {
- X// String s = gpionames[gpio].replaceAll("\\.0", ".");
- X// System.out.println("## Writing \"gpio-"+gpionames[gpio]+"\" to "+CAPEMGRFILE);
- X writeToFile(CAPEMGRFILE, "gpio-"+gpionames[gpio]);
- X }
- X GPIO gpioobj = new GPIO(gpio, "gpio"+gpio+"@"+gpionames[gpio], this);
- X gpioobj.setMode(mode);
- X return gpioobj;
- X }
- X
- X public int getNumGPIOs() {
- X return gpionames.length;
- X }
- X
- X public void setPull(int gpio, boolean input, int pull) throws IOException {
- X String s = pull > 0 ? "rxEnable_pullUp" : pull < 0 ? "rxEnable_pullDown" : "rxEnable_pullNone";
- X File[] f = new File(DTODIR).listFiles();
- X String prefix = "gpio-"+gpionames[gpio]+"_gpio"+gpio;
- X for (int i=0;i<f.length;i++) {
- X if (f[i].getName().startsWith(prefix)) {
- X writeToFile(DTODIR + "/" + f[i].getName() + "/state", s);
- X return;
- X }
- X }
- X throw new IOException("No file \""+DTODIR+"/"+prefix+"*\"");
- X }
- X
- X}
- END-of-BBGPIOFactory.java
- echo x - GPIO.java
- sed 's/^X//' >GPIO.java << 'END-of-GPIO.java'
- Ximport java.util.*;
- Ximport java.io.*;
- X
- X/**
- X * The GPIO class represents a single GPIO pin on the hardware, which may be
- X * output or input, possibly with pullup or pulldown resistors.
- X * <pre class="example">
- X * GPIO gpio = GPIOFactory.getInstance().open(gpionum, GPIO.Mode.INPUT_PULLUP);
- X * </pre>
- X */
- Xpublic class GPIO {
- X
- X public static enum Mode {
- X OUTPUT_LOW, OUTPUT_HIGH, INPUT_NONE, INPUT_PULLUP, INPUT_PULLDOWN
- X };
- X
- X // Everything in this class should be standard to all reasonably modern Linux
- X // distributions on any hardware. Variations in the hardware are managed by
- X // different GPIOFactory implementations
- X
- X private static final String GPIODIR = "/sys/class/gpio";
- X private static GPIO[] all;
- X private static PollThread pollthread;
- X private static final List<GPIO> pollfdlist = new ArrayList<GPIO>();
- X
- X private final int gpio, fd;
- X private String name;
- X private final GPIOFactory factory;
- X private boolean output, value, poll;
- X
- X static {
- X pollthread = new PollThread();
- X pollthread.start();
- X }
- X
- X private static void writeToFile(String file, String data) throws IOException {
- X Writer out = new OutputStreamWriter(new FileOutputStream(file), "ISO-8859-1");
- X out.write(data);
- X out.close();
- X }
- X
- X /**
- X * Open a new GPIO. The GPIO must not be opened elsewhere. Called by factory
- X * @param gpio the GPIO number
- X * @param name the user-friendly name
- X * @param factory the factory creating the GPIO
- X */
- X public GPIO(int gpio, String name, GPIOFactory factory) throws IOException {
- X synchronized(GPIO.class) {
- X if (all == null) {
- X all = new GPIO[factory.getNumGPIOs()];
- X }
- X if (all[gpio] != null) {
- X throw new IllegalArgumentException("GPIO "+gpio+" already open");
- X }
- X all[gpio] = this;
- X }
- X String valfile = GPIODIR + "/gpio" + gpio + "/value";
- X if (!new File(valfile).exists()) {
- X writeToFile(GPIODIR+"/export", Integer.toString(gpio));
- X }
- X this.gpio = gpio;
- X this.fd = POSIX.open(valfile, POSIX.O_RDWR | POSIX.O_NONBLOCK);
- X if (fd < 0) {
- X throw new IOException("Couldn't open \""+valfile+"\": "+POSIX.errmsg());
- X }
- X this.factory = factory;
- X this.name = name;
- X }
- X
- X /**
- X * Close the GPIO and release it to be opened elsewhere
- X */
- X public void close() {
- X setPolling(false);
- X POSIX.close(fd);
- X synchronized(GPIO.class) {
- X all[gpio] = null;
- X }
- X }
- X
- X public int hashCode() {
- X return gpio;
- X }
- X
- X public boolean equals(Object o) {
- X return o == this;
- X }
- X
- X void setEdge(boolean rising, boolean falling) throws IOException {
- X String s = rising ? falling ? "both" : "rising" : falling ? "falling" : "none";
- X writeToFile(GPIODIR + "/gpio" + gpio + "/edge", s);
- X }
- X
- X public boolean isOutput() {
- X return output;
- X }
- X
- X /**
- X * Set the Mode of the GPIO.
- X */
- X public void setMode(Mode mode) throws IOException {
- X this.output = mode == Mode.OUTPUT_LOW || mode == Mode.OUTPUT_HIGH;
- X writeToFile(GPIODIR + "/gpio" + gpio + "/direction", output ? "out": "in");
- X if (!output) {
- X factory.setPull(gpio, !output, mode == Mode.INPUT_PULLUP ? 1 : mode == Mode.INPUT_PULLDOWN ? -1 : 0);
- X if (POSIX.lseek(fd, 0, POSIX.SEEK_SET) < 0) {
- X throw new IOException("Can't seek: "+POSIX.errmsg());
- X }
- X } else {
- X set(mode == Mode.OUTPUT_HIGH);
- X }
- X }
- X
- X /**
- X * For output GPIOs, set the output value to on or off. Illegal for input GPIOs
- X */
- X public void set(boolean on) {
- X if (output) {
- X byte[] b = new byte[] { on ? (byte)'1' : (byte)'0' };
- X POSIX.write(fd, b, 1);
- X if (value != on) {
- X value = on;
- X // TODO Fire an event here notifiying listeners the GPIO has changed
- X// Marshall.pubGPIO(value, this);
- X }
- X } else {
- X throw new IllegalStateException("input");
- X }
- X }
- X
- X private boolean doget() throws IOException {
- X byte[] b = new byte[1];
- X if (POSIX.pread(fd, b, 1, 0) < 0) { // pread so we don't need to lseek between reads!
- X throw new IOException("Can't read: "+POSIX.errmsg());
- X }
- X return value = b[0] == '1' ? true : false;
- X }
- X
- X /**
- X * Get the value of the GPIO. For Output GPIOs, this is the last value set.
- X * For input GPIOs with no listener attached, this reads the pin immediately.
- X * For input GPIos with listners attached, the value is kept updated by the
- X * listener
- X */
- X public boolean get() {
- X if (!output && !poll) {
- X try {
- X doget();
- X } catch (IOException e) {
- X throw new RuntimeException(e);
- X }
- X }
- X return value;
- X }
- X
- X public synchronized void setPolling(boolean poll) {
- X if (poll == this.poll) {
- X return;
- X }
- X if (poll) {
- X try {
- X setEdge(true, true);
- X } catch (IOException e) {
- X throw new RuntimeException(e);
- X }
- X synchronized(pollfdlist) {
- X pollfdlist.add(this);
- X pollfdlist.notifyAll();
- X }
- X } else {
- X synchronized(pollfdlist) {
- X pollfdlist.remove(this);
- X pollfdlist.notifyAll();
- X }
- X try {
- X setEdge(false, false);
- X } catch (IOException e) {
- X throw new RuntimeException(e);
- X }
- X }
- X this.poll = poll;
- X }
- X
- X /**
- X * Return the GPIO number of this GPIO
- X */
- X public int getGPIO() {
- X return gpio;
- X }
- X
- X /**
- X * Return the user-friendly name of this GPIO
- X */
- X public String getName() {
- X return name;
- X }
- X
- X /**
- X * Set the user-friendly name of this GPIO
- X */
- X public void setName(String name) {
- X this.name = name;
- X }
- X
- X private static class PollThread extends Thread {
- X
- X public PollThread() {
- X super("GPIOPoller");
- X setDaemon(true);
- X }
- X
- X public void run() {
- X final int polltimeout = 1000;
- X POSIX.Pollfd[] pollfd = null;
- X GPIO[] gpiolist = null;
- X int num;
- X while (pollthread != null) {
- X synchronized(pollfdlist) {
- X if (pollfdlist.isEmpty()) {
- X try {
- X pollfdlist.wait();
- X } catch (InterruptedException e) {}
- X }
- X gpiolist = pollfdlist.toArray(new GPIO[pollfdlist.size()]);
- X }
- X num = gpiolist.length;
- X if (pollfd == null || pollfd.length < num) {
- X pollfd = new POSIX.Pollfd[num];
- X for (int i=0;i<num;i++) {
- X pollfd[i] = new POSIX.Pollfd();
- X }
- X }
- X for (int i=0;i<num;i++) {
- X pollfd[i].init(gpiolist[i].fd, POSIX.POLLPRI);
- X }
- X if (num > 0) {
- X int ret = POSIX.poll(pollfd, num, polltimeout);
- X// System.out.println("> Poll gave "+ret+"/"+num);
- X if (ret > 0) {
- X int minpin = -1, maxpin = 0;
- X long c1 = 0, c2 = 0;
- X for (int i=0;i < num && ret > 0;i++) {
- X if (pollfd[i].isPri()) {
- X if (minpin < 0) {
- X minpin = i;
- X }
- X maxpin = i;
- X GPIO gpio = gpiolist[i];
- X boolean old = gpio.value;
- X try {
- X gpio.doget();
- X } catch (IOException e) {
- X e.printStackTrace();
- X }
- X if (old != gpio.value) {
- X if (i < 64) {
- X c1 |= (1<<i);
- X } else {
- X c2 |= (1<<(i-64));
- X }
- X }
- X ret--;
- X }
- X }
- X for (int i=minpin;i<=maxpin;i++) {
- X if ((i < 64 && (c1&(1<<i)) != 0) || (i >= 64 && (c2&(1<<(i-64))) != 0)) {
- X GPIO gpio = gpiolist[i];
- X boolean value = gpio.value;
- X // TODO Fire an event here notifiying listeners the GPIO has changed
- X// Marshall.pubGPIO(value, gpio);
- X }
- X }
- X } else if (ret < 0) {
- X// Marshall.pubMessage(new IOException("Poll failed: "+POSIX.errmsg()), this);
- X new IOException("Poll failed: "+POSIX.errmsg()).printStackTrace();
- X }
- X }
- X }
- X }
- X }
- X
- X public String toString() {
- X return name;
- X }
- X
- X}
- END-of-GPIO.java
- echo x - GPIOFactory.java
- sed 's/^X//' >GPIOFactory.java << 'END-of-GPIOFactory.java'
- Ximport java.io.*;
- X
- X/**
- X * A GPIOFactory creates GPIO objects which are suitable for the current
- X * hardware.
- X */
- Xpublic abstract class GPIOFactory {
- X
- X private static GPIOFactory instance;
- X
- X /**
- X * Get a GPIOFactory for the current hardware (best guess)
- X */
- X public synchronized static GPIOFactory getInstance() {
- X if (instance == null) {
- X if (BBGPIOFactory.canCreate()) {
- X instance = new BBGPIOFactory();
- X }
- X if (instance == null) {
- X throw new IllegalStateException("No usable factory found");
- X }
- X }
- X return instance;
- X }
- X
- X /**
- X * Open a new GPIO
- X * @param gpio the gpio number, as found in /sys/class/gpio
- X * @param mode the {@link GPIO.Mode mode} to open the GPIO in - input or output
- X */
- X public abstract GPIO open(int gpio, GPIO.Mode mode) throws IOException;
- X
- X /**
- X * Set the pullup or pulldown resistors if available. If the required state
- X * is not available an IOException is thrown
- X * @param gpio the gpio
- X * @param input whether the pin is in input mode or not
- X * @param pull A value > 0 for pullup, < 0 for pull down or 0 for no pull
- X */
- X public abstract void setPull(int gpio, boolean input, int pull) throws IOException;
- X
- X /**
- X * Get the number of GPIOs available from this factory (0 < gpio < num);
- X */
- X public abstract int getNumGPIOs();
- X
- X}
- END-of-GPIOFactory.java
- echo x - I2CBus.java
- sed 's/^X//' >I2CBus.java << 'END-of-I2CBus.java'
- Ximport java.io.*;
- Ximport java.nio.*;
- Ximport java.util.*;
- X
- X/**
- X * An I2C bus represents the file "/dev/i2c-nnn", where nnn is the bus number.
- X * All I/O to that file goes through this class, and many {@link I2CDevice} devices
- X * may be used on the bus. All I/O methods on this class are synchronized, which means
- X * for the purposes of the API they are atomic. If a non-atomic exchange of data is required
- X * by the I2C device, it should synchronize on this class while doing so.
- X * </p><p>
- X * Operations may also be queued on the timer returned from {@link #getTime} by calling
- X * {@link #queue}.
- X * </p>
- X *
- X * <pre class="example">
- X * I2CBus bus = I2CBus.openBus(1);
- X * I2CDevice device = new I2CDevice(0x34, bus);
- X * int val = device.smReadByte(register)
- X * bus.close();
- X * </pre>
- X */
- Xpublic class I2CBus {
- X
- X private static final int NUMBUSSES = 4;
- X private static final I2CBus[] bus = new I2CBus[NUMBUSSES];
- X
- X private final String name;
- X private final int num, fd;
- X private Timer timer;
- X private int address;
- X
- X /**
- X * Open the specified I2C bus
- X * @param num the bus number
- X */
- X public static synchronized I2CBus openBus(int num) throws IOException {
- X if (num < 0 || num >= NUMBUSSES) {
- X throw new IllegalArgumentException(Integer.toString(num));
- X }
- X if (bus[num] == null) {
- X bus[num] = new I2CBus(num);
- X }
- X return bus[num];
- X }
- X
- X private I2CBus(int num) throws IOException {
- X this.num = num;
- X this.name = "/dev/i2c-"+num;
- X if ((this.fd = POSIX.open(name, POSIX.O_RDWR)) < 0) {
- X throw new IOException("Can't open "+name+": "+POSIX.errmsg());
- X }
- X }
- X
- X /**
- X * Close this I2C bus
- X */
- X public synchronized void close() {
- X POSIX.close(fd);
- X if (timer != null) {
- X timer.cancel();
- X }
- X synchronized(I2CBus.class) {
- X bus[num] = null;
- X }
- X }
- X
- X public String toString() {
- X return name;
- X }
- X
- X private void set(int address) throws IOException {
- X if (this.address != address) {
- X if (POSIX.ioctl(fd, POSIX.I2C_SLAVE, address) < 0) {
- X throw new IOException("Can't set address 0x"+Integer.toHexString(address)+" on "+this);
- X }
- X this.address = address;
- X }
- X }
- X
- X /**
- X * Read data for the specified bus
- X * @param address the device address
- X * @param buf the array to read into
- X * @param len the number of bytes to read
- X */
- X public synchronized void read(int address, byte[] buf, int off, int len) throws IOException {
- X set(address);
- X if (off == 0) {
- X if (POSIX.read(fd, buf, len) != len) {
- X throw new IOException("Read failed for "+this);
- X }
- X } else {
- X ByteBuffer b = ByteBuffer.wrap(buf);
- X b.position(off);
- X if (POSIX.read(fd, b, len) != len) {
- X throw new IOException("Read failed for "+this);
- X }
- X }
- X }
- X
- X /**
- X * Write data to the specified bus
- X * @param address the device address
- X * @param wbuf the array to write from
- X * @param wlen the number of bytes to write
- X */
- X public synchronized void write(int address, byte[] buf, int off, int len) throws IOException {
- X set(address);
- X if (off == 0) {
- X if (POSIX.write(fd, buf, len) != len) {
- X throw new IOException("Write failed for "+this);
- X }
- X } else {
- X ByteBuffer b = ByteBuffer.wrap(buf);
- X b.position(off);
- X if (POSIX.write(fd, b, len) != len) {
- X throw new IOException("Write failed for "+this);
- X }
- X }
- X }
- X
- X /**
- X * Write data to the specified bus, then read some back
- X * @param address the device address
- X * @param wbuf the array to write from
- X * @param woff the offset into the wbuf array
- X * @param wlen the number of bytes to write
- X * @param rbuf the array to read into
- X * @param roff the offset into the rbuf array
- X * @param rlen the number of bytes to read
- X */
- X public synchronized void writeread(int address, byte[] wbuf, int woff, int wlen, byte[] rbuf, int roff, int rlen) throws IOException {
- X set(address);
- X if (woff == 0) {
- X if (POSIX.write(fd, wbuf, wlen) != wlen) {
- X throw new IOException("Write failed for "+this);
- X }
- X } else {
- X ByteBuffer b = ByteBuffer.wrap(wbuf);
- X b.position(woff);
- X if (POSIX.write(fd, b, wlen) != wlen) {
- X throw new IOException("Write failed for "+this);
- X }
- X }
- X if (roff == 0) {
- X if (POSIX.read(fd, rbuf, rlen) != rlen) {
- X throw new IOException("Read failed for "+this);
- X }
- X } else {
- X ByteBuffer b = ByteBuffer.wrap(rbuf);
- X b.position(roff);
- X if (POSIX.read(fd, b, rlen) != rlen) {
- X throw new IOException("Read failed for "+this);
- X }
- X }
- X }
- X
- X public synchronized void writeread(int address, ByteBuffer wbuf, ByteBuffer rbuf) throws IOException {
- X set(address);
- X if (POSIX.write(fd, wbuf, wbuf.remaining()) < 0) {
- X throw new IOException("Write failed for "+this);
- X }
- X if (POSIX.read(fd, rbuf, rbuf.remaining()) < 0) {
- X throw new IOException("Read failed for "+this);
- X }
- X }
- X
- X /**
- X * Queue a task (a read or write, normally) in the specified number of ms
- X * @param r the task to run
- X * @param ms the number of ms to delay before running it
- X */
- X public void queue(final Runnable r, int ms) {
- X Timer timer = getTimer();
- X synchronized(timer) {
- X timer.schedule(new TimerTask() {
- X public void run() {
- X r.run();
- X }
- X }, ms);
- X }
- X }
- X
- X /**
- X * Get a timer that can be used to queue tasks on this bus
- X */
- X public synchronized Timer getTimer() {
- X if (timer == null) {
- X timer = new Timer(true);
- X }
- X return timer;
- X }
- X
- X}
- END-of-I2CBus.java
- echo x - I2CDevice.java
- sed 's/^X//' >I2CDevice.java << 'END-of-I2CDevice.java'
- Ximport java.io.*;
- Ximport java.util.*;
- X
- X/**
- X * An I2CDevice is an address on an I2CBus. This class can be used
- X * as a base for more complex devices.
- X */
- Xpublic class I2CDevice {
- X
- X private I2CBus bus;
- X private int address;
- X private static byte[] buf = new byte[4];
- X private TimerTask polltask;
- X
- X /**
- X * Create a new I2CDevice
- X */
- X public I2CDevice() {
- X }
- X
- X /**
- X * Set the I2C bus and address
- X * @param address the 7-bit device address
- X * @param bus the bus the device is on.
- X */
- X public void setAddress(int address, I2CBus bus) {
- X if (address < 0 || address > 0x7F || bus == null) {
- X throw new IllegalArgumentException("Invalid address or bus");
- X }
- X this.address = address;
- X this.bus = bus;
- X }
- X
- X /**
- X * Get the bus this device was created on
- X */
- X public I2CBus getBus() {
- X return bus;
- X }
- X
- X /**
- X * Get the bus address
- X */
- X public int getAddress() {
- X return address;
- X }
- X
- X public String toString() {
- X return bus+"@0x"+Integer.toHexString(address);
- X }
- X
- X public int read() throws IOException {
- X bus.read(address, buf, 0, 1);
- X return buf[0] & 0xFF;
- X }
- X
- X public void read(byte[] buf) throws IOException {
- X bus.read(address, buf, 0, buf.length);
- X }
- X
- X public void read(byte[] buf, int off, int len) throws IOException {
- X bus.read(address, buf, off, len);
- X }
- X
- X public void write(int b) throws IOException {
- X buf[0] = (byte)b;
- X bus.write(address, buf, 0, 1);
- X }
- X
- X public void write(byte[] b) throws IOException {
- X bus.write(address, b, 0, b.length);
- X }
- X
- X public void write(byte[] b, int off, int len) throws IOException {
- X bus.write(address, b, off, len);
- X }
- X
- X /**
- X * Read a byte from the specified register of a System Management bus device
- X */
- X public int smReadByte(int register) throws IOException {
- X buf[0] = (byte)register;
- X bus.writeread(address, buf, 0, 1, buf, 0, 1);
- X return buf[0]&0xFF;
- X }
- X
- X /**
- X * Read a 16-bit word from the specified register of a System Management bus device
- X */
- X public int smReadWord(int register) throws IOException {
- X buf[0] = (byte)register;
- X bus.writeread(address, buf, 0, 1, buf, 0, 2);
- X return ((buf[0]&0xFF)<<8) | (buf[1]&0xFF);
- X }
- X
- X /**
- X * Write a byte to the specified register of a System Management bus device
- X */
- X public void smWriteByte(int register, int data) throws IOException {
- X buf[0] = (byte)register;
- X buf[1] = (byte)data;
- X bus.write(address, buf, 0, 2);
- X }
- X
- X /**
- X * Write a 16-bit word to the specified register of a System Management bus device
- X */
- X public void smWriteWord(int register, int data) throws IOException {
- X buf[0] = (byte)register;
- X buf[1] = (byte)((data>>8)&0xFF);
- X buf[2] = (byte)(data&0xFF);
- X bus.write(address, buf, 0, 3);
- X }
- X
- X public void poll() throws IOException {
- X throw new UnsupportedOperationException();
- X }
- X
- X public void autopoll(int ms) {
- X if (ms == 0 && polltask != null) {
- X polltask.cancel();
- X polltask = null;
- X } else if (ms > 0) {
- X if (polltask == null) {
- X polltask = new TimerTask() {
- X public void run() {
- X try {
- X poll();
- X } catch (Exception e) {
- X e.printStackTrace();
- X autopoll(0);
- X }
- X }
- X };
- X }
- X getBus().getTimer().schedule(polltask, 0, ms);
- X }
- X }
- X
- X
- X}
- END-of-I2CDevice.java
- echo x - POSIX.java
- sed 's/^X//' >POSIX.java << 'END-of-POSIX.java'
- Ximport com.sun.jna.*;
- Ximport java.util.*;
- Ximport java.io.*;
- Ximport java.nio.*;
- X
- X/**
- X * A JNA-based wrapped around the various C libraries under Linux.
- X * Much of this was lifted from the "JTermios" classes of the purejavacomm
- X * API at https://github.com/nyholku/purejavacomm
- X */
- Xpublic class POSIX {
- X
- X private static final boolean IS64B = NativeLong.SIZE == 8;
- X
- X // sys/fileio.h stuff
- X public static final int FIONREAD = 0x541B;
- X // bits/fcntl-linux.h
- X public static final int O_RDONLY = 0x00000000;
- X public static final int O_WRONLY = 0x00000001;
- X public static final int O_RDWR = 0x00000002;
- X public static final int O_CREAT = 0x00000040;
- X public static final int O_EXCL = 0x00000080;
- X public static final int O_NOCTTY = 0x00000100;
- X public static final int O_TRUNC = 0x00000200;
- X public static final int O_APPEND = 0x00000400;
- X public static final int O_NONBLOCK = 0x00000800;
- X public static final int O_NDELAY = O_NONBLOCK;
- X public static final int O_SYNC = 0x00101000;
- X public static final int O_DIRECT = 0x00004000;
- X public static final int O_LARGEFILE = 0x00008000;
- X public static final int O_DIRECTORY = 0x00010000;
- X public static final int O_NOFOLLOW = 0x00020000;
- X public static final int O_NOATIME = 0x00040000;
- X public static final int O_CLOEXEC = 0x00080000;
- X public static final int F_GETFL = 0x00000003;
- X public static final int F_SETFL = 0x00000004;
- X
- X // errno.h stuff
- X public static final int EACCES = 11;
- X public static final int EAGAIN = 13;
- X public static final int EBADF = 9;
- X public static final int EBUSY = 16;
- X public static final int EEXIST = 17;
- X public static final int EINTR = 4;
- X public static final int EINVAL = 22;
- X public static final int EIO = 5;
- X public static final int EISDIR = 21;
- X public static final int ELOOP = 40;
- X public static final int EMFILE = 24;
- X public static final int ENAMETOOLONG = 36;
- X public static final int ENFILE = 23;
- X public static final int ENOENT = 2;
- X public static final int ENOSR = 63;
- X public static final int ENOSPC = 28;
- X public static final int ENOTDIR = 20;
- X public static final int ENXIO = 6;
- X public static final int EOVERFLOW = 75;
- X public static final int EROFS = 30;
- X public static final int ENOTSUP = 95;
- X // seek
- X public static final int SEEK_SET = 0;
- X public static final int SEEK_CUR = 1;
- X public static final int SEEK_END = 2;
- X // linux/i2c-dev.h
- X public static final int I2C_RETRIES = 0x0701;
- X public static final int I2C_TIMEOUT = 0x0702;
- X public static final int I2C_SLAVE = 0x0703;
- X public static final int I2C_SLAVE_FORCE = 0x0706;
- X public static final int I2C_FUNCS = 0x0705;
- X public static final int I2C_RDWR = 0x0707;
- X public static final int I2C_PEC = 0x0708;
- X public static final int I2C_SMBUS = 0x0720;
- X // termios.h stuff
- X public static final int TIOCM_RNG = 0x00000080;
- X public static final int TIOCM_CAR = 0x00000040;
- X public static final int IGNBRK = 0x00000001;
- X public static final int BRKINT = 0x00000002;
- X public static final int IGNPAR = 0x00000004;
- X public static final int PARMRK = 0x00000008;
- X public static final int INLCR = 0x00000040;
- X public static final int IGNCR = 0x00000080;
- X public static final int ICRNL = 0x00000100;
- X public static final int ECHONL = 0x00000040;
- X public static final int IEXTEN = 0x00008000;
- X public static final int CLOCAL = 0x00000800;
- X public static final int OPOST = 0x00000001;
- X public static final int VSTART = 0x00000008;
- X public static final int TCSANOW = 0x00000000;
- X public static final int VSTOP = 0x00000009;
- X public static final int VMIN = 0x00000006;
- X public static final int VTIME = 0x00000005;
- X public static final int VEOF = 0x00000004;
- X public static final int TIOCMGET = 0x00005415;
- X public static final int TIOCM_CTS = 0x00000020;
- X public static final int TIOCM_DSR = 0x00000100;
- X public static final int TIOCM_RI = 0x00000080;
- X public static final int TIOCM_CD = 0x00000040;
- X public static final int TIOCM_DTR = 0x00000002;
- X public static final int TIOCM_RTS = 0x00000004;
- X public static final int ICANON = 0x00000002;
- X public static final int ECHO = 0x00000008;
- X public static final int ECHOE = 0x00000010;
- X public static final int ISIG = 0x00000001;
- X public static final int TIOCMSET = 0x00005418;
- X public static final int IXON = 0x00000400;
- X public static final int IXOFF = 0x00001000;
- X public static final int IXANY = 0x00000800;
- X public static final int CRTSCTS = 0x80000000;
- X public static final int TCSADRAIN = 0x00000001;
- X public static final int INPCK = 0x00000010;
- X public static final int ISTRIP = 0x00000020;
- X public static final int CSIZE = 0x00000030;
- X public static final int TCIFLUSH = 0x00000000;
- X public static final int TCOFLUSH = 0x00000001;
- X public static final int TCIOFLUSH = 0x00000002;
- X public static final int CS5 = 0x00000000;
- X public static final int CS6 = 0x00000010;
- X public static final int CS7 = 0x00000020;
- X public static final int CS8 = 0x00000030;
- X public static final int CSTOPB = 0x00000040;
- X public static final int CREAD = 0x00000080;
- X public static final int PARENB = 0x00000100;
- X public static final int PARODD = 0x00000200;
- X public static final int CMSPAR = 0x40000000;
- X public static final int B0 = 0;
- X public static final int B50 = 1;
- X public static final int B75 = 2;
- X public static final int B110 = 3;
- X public static final int B134 = 4;
- X public static final int B150 = 5;
- X public static final int B200 = 6;
- X public static final int B300 = 7;
- X public static final int B600 = 8;
- X public static final int B1200 = 9;
- X public static final int B1800 = 10;
- X public static final int B2400 = 11;
- X public static final int B4800 = 12;
- X public static final int B9600 = 13;
- X public static final int B19200 = 14;
- X public static final int B38400 = 15;
- X public static final int B57600 = 4097;
- X public static final int B115200 = 4098;
- X public static final int B230400 = 4099;
- X // bits/poll.h. See also Pollfd class
- X public static final short POLLIN = 0x01;
- X public static final short POLLPRI = 0x02;
- X public static final short POLLOUT = 0x04;
- X // spi.h stuff
- X private static final int SPI_IOC_MAGIC = 'k';
- X public static final int SPI_IOC_RD_MODE = ioc(true, SPI_IOC_MAGIC, 1, 1);
- X public static final int SPI_IOC_WR_MODE = ioc(false, SPI_IOC_MAGIC, 1, 1);
- X public static final int SPI_IOC_RD_LSB_FIRST = ioc(true, SPI_IOC_MAGIC, 2, 1);
- X public static final int SPI_IOC_WR_LSB_FIRST = ioc(false, SPI_IOC_MAGIC, 2, 1);
- X public static final int SPI_IOC_RD_BITS_PER_WORD = ioc(true, SPI_IOC_MAGIC, 3, 1);
- X public static final int SPI_IOC_WR_BITS_PER_WORD = ioc(false, SPI_IOC_MAGIC, 3, 1);
- X public static final int SPI_IOC_RD_MAX_SPEED_HZ = ioc(true, SPI_IOC_MAGIC, 4, 4);
- X public static final int SPI_IOC_WR_MAX_SPEED_HZ = ioc(false, SPI_IOC_MAGIC, 4, 4);
- X // misc stuff
- X public static final int DC1 = 0x11; // Ctrl-Q;
- X public static final int DC3 = 0x13; // Ctrl-S;
- X
- X private static final FastLibrary libcfast = new FastLibrary();
- X private static final SlowLibrary libcslow = (SlowLibrary)Native.loadLibrary("c", SlowLibrary.class);
- X
- X //----------------------------------------------------------------------------
- X
- X private static int ioc(boolean read, int type, int nr, int size) {
- X final int dirbits = 2;
- X final int sizebits = 14;
- X final int nrbits = 8;
- X final int typebits = 8;
- X if (size >= 1<<sizebits) {
- X throw new IllegalArgumentException("SPI message too long");
- X }
- X return ((read ? 2 : 1)<<(sizebits + nrbits + typebits)) | (size<<(typebits + nrbits)) | (type<<nrbits) | nr;
- X }
- X
- X public static void printf(String format, Object... args) {
- X libcslow.printf(format, args);
- X }
- X
- X public static void puts(ByteBuffer buf) {
- X libcfast.puts(buf);
- X }
- X
- X public static int errno() {
- X return Native.getLastError();
- X }
- X
- X public static String errmsg() {
- X return strerror(errno());
- X }
- X
- X public static String strerror(int num) {
- X return libcfast.strerror(num);
- X }
- X
- X public static void cfmakeraw(Termios termios) {
- X termios_struct t = new termios_struct(termios);
- X libcfast.cfmakeraw(t);
- X t.update(termios);
- X }
- X
- X public static int fcntl(int fd, int cmd, int arg) {
- X return libcfast.fcntl(fd, cmd, arg);
- X }
- X
- X public static int tcdrain(int fd) {
- X return libcfast.tcdrain(fd);
- X }
- X
- X public static int speedToBaud(int speed) {
- X switch(speed) {
- X case B0: return 0;
- X case B50: return 50;
- X case B75: return 75;
- X case B110: return 110;
- X case B134: return 134;
- X case B150: return 150;
- X case B200: return 200;
- X case B300: return 300;
- X case B600: return 600;
- X case B1200: return 1200;
- X case B1800: return 1800;
- X case B2400: return 2400;
- X case B4800: return 4800;
- X case B9600: return 9600;
- X case B19200: return 19200;
- X case B38400: return 38400;
- X case B57600: return 57600;
- X case B115200: return 115200;
- X case B230400: return 230400;
- X default: return -1;
- X }
- X }
- X
- X public static int baudToSpeed(int baud) {
- X switch(baud) {
- X case 0: return B0;
- X case 50: return B50;
- X case 75: return B75;
- X case 110: return B110;
- X case 134: return B134;
- X case 150: return B150;
- X case 200: return B200;
- X case 300: return B300;
- X case 600: return B600;
- X case 1200: return B1200;
- X case 1800: return B1800;
- X case 2400: return B2400;
- X case 4800: return B4800;
- X case 9600: return B9600;
- X case 19200: return B19200;
- X case 38400: return B38400;
- X case 57600: return B57600;
- X case 115200: return B115200;
- X case 230400: return B230400;
- X default: return -1;
- X }
- X }
- X
- X
- X public static int cfgetispeed(Termios termios) {
- X return libcfast.cfgetispeed(new termios_struct(termios)).intValue();
- X }
- X
- X public static int cfgetospeed(Termios termios) {
- X return libcfast.cfgetospeed(new termios_struct(termios)).intValue();
- X }
- X
- X public static int cfsetispeed(Termios termios, int speed) {
- X termios_struct t = new termios_struct(termios);
- X int ret = libcfast.cfsetispeed(t, new NativeLong(speed));
- X t.update(termios);
- X return ret;
- X }
- X
- X public static int cfsetospeed(Termios termios, int speed) {
- X termios_struct t = new termios_struct(termios);
- X int ret = libcfast.cfsetospeed(t, new NativeLong(speed));
- X t.update(termios);
- X return ret;
- X }
- X
- X public static int open(String s, int t) {
- X return libcfast.open(s, t);
- X }
- X
- X public static int read(int fd, ByteBuffer buffer, int len) {
- X return IS64B ? (int)libcfast.read(fd, buffer, (long)len) : libcfast.read(fd, buffer, len);
- X }
- X
- X public static int read(int fd, byte[] buffer, int len) {
- X return IS64B ? (int)libcfast.read(fd, buffer, (long)len) : libcfast.read(fd, buffer, len);
- X }
- X
- X public static int pread(int fd, byte[] buffer, int len, long offset) {
- X return IS64B ? (int)libcfast.pread(fd, buffer, (long)len, new NativeLong(offset)) : libcfast.pread(fd, buffer, len, new NativeLong(offset));
- X }
- X
- X public static int write(int fd, ByteBuffer buffer, int len) {
- X return IS64B ? (int)libcfast.write(fd, buffer, (long)len) : libcfast.write(fd, buffer, len);
- X }
- X
- X public static int write(int fd, byte[] buffer, int len) {
- X return IS64B ? (int)libcfast.write(fd, buffer, (long)len) : libcfast.write(fd, buffer, len);
- X }
- X
- X public static long lseek(int fd, long off, int whence) {
- X return libcfast.lseek(new NativeLong(fd), new NativeLong(off), new NativeLong(whence)).longValue();
- X }
- X
- X public static int close(int fd) {
- X return libcfast.close(fd);
- X }
- X
- X public static int tcflush(int fd, int b) {
- X return libcfast.tcflush(fd, b);
- X }
- X
- X public static int tcgetattr(int fd, Termios termios) {
- X termios_struct t = new termios_struct();
- X int ret = libcfast.tcgetattr(fd, t);
- X t.update(termios);
- X return ret;
- X }
- X
- X public static void perror(String msg) {
- X libcfast.perror(msg);
- X }
- X
- X public static int tcsendbreak(int fd, int duration) {
- X // If duration is not zero, it sends zero-valued bits for duration*N seconds,
- X // where N is at least 0.25, and not more than 0.5.
- X return libcfast.tcsendbreak(fd, duration / 250);
- X }
- X
- X public static int tcsetattr(int fd, int cmd, Termios termios) {
- X return libcfast.tcsetattr(fd, cmd, new termios_struct(termios));
- X }
- X
- X public static void FD_CLR(int fd, FDSet set) {
- X if (set == null) {
- X return;
- X }
- X set.bits[fd / FDSet.NFBBITS] &= ~(1 << (fd % FDSet.NFBBITS));
- X }
- X
- X public static boolean FD_ISSET(int fd, FDSet set) {
- X if (set == null) {
- X return false;
- X }
- X return (set.bits[fd / FDSet.NFBBITS] & (1 << (fd % FDSet.NFBBITS))) != 0;
- X }
- X
- X public static void FD_SET(int fd, FDSet set) {
- X if (set == null) {
- X return;
- X }
- X set.bits[fd / FDSet.NFBBITS] |= 1 << (fd % FDSet.NFBBITS);
- X }
- X
- X public static void FD_ZERO(FDSet set) {
- X if (set == null) {
- X return;
- X }
- X Arrays.fill(set.bits, 0);
- X }
- X
- X public static int select(int nfds, FDSet rfds, FDSet wfds, FDSet efds, TimeVal timeout) {
- X timeval_struct tout = null;
- X if (timeout != null) {
- X tout = new timeval_struct(timeout);
- X }
- X
- X int[] r = rfds != null ? rfds.bits : null;
- X int[] w = wfds != null ? wfds.bits : null;
- X int[] e = efds != null ? efds.bits : null;
- X return libcfast.select(nfds, r, w, e, tout);
- X }
- X
- X public static int poll(Pollfd fds[], int nfds, int timeout) {
- X int[] v = new int[nfds * 2];
- X for (int i=0,j=0;i<nfds;i++) {
- X v[j++] = fds[i].fd;
- X v[j++] = fds[i].events;
- X }
- X int ret = libcfast.poll(v, nfds, timeout);
- X for (int i=0;i<nfds;i++) {
- X fds[i].events = v[i*2+1];
- X }
- X return ret;
- X }
- X
- X public static int ioctl(int fd, int cmd, int data) {
- X return libcfast.ioctl(fd, cmd, data);
- X }
- X
- X public static int ioctl(int fd, int cmd, byte[] data) {
- X return libcfast.ioctl(fd, cmd, data);
- X }
- X
- X public static int ioctl(int fd, int cmd, int[] data) {
- X return libcfast.ioctl(fd, cmd, data);
- X }
- X
- X private static final spi_ioc_transfer_struct spi_ioc_transfer_instance = new spi_ioc_transfer_struct();
- X
- X public static void ioctl_spimessage(int fd, ByteBuffer tx, ByteBuffer rx, int len) throws IOException {
- X synchronized(spi_ioc_transfer_instance) {
- X spi_ioc_transfer_instance.tx_buf = rx == null ? 0 : Pointer.nativeValue(Native.getDirectBufferPointer(tx));
- X spi_ioc_transfer_instance.rx_buf = rx == null ? 0 : Pointer.nativeValue(Native.getDirectBufferPointer(rx));
- X spi_ioc_transfer_instance.len = len;
- X int cmd = ioc(false, SPI_IOC_MAGIC, 0, spi_ioc_transfer_instance.size());
- X int v = libcfast.ioctl(fd, cmd, spi_ioc_transfer_instance);
- X if (v < 0) {
- X throw new IOException("ioctl(SPI_IOC_MESSAGE): "+POSIX.errmsg());
- X }
- X }
- X }
- X
- X //----------------------------------------------------------------------
- X
- X private static class FastLibrary {
- X native public int puts(ByteBuffer buf);
- X native public long memcpy(int[] dst, short[] src, long n);
- X native public int memcpy(int[] dst, short[] src, int n);
- X native public int pipe(int[] fds);
- X native public int tcdrain(int fd);
- X native public void cfmakeraw(termios_struct termios);
- X native public int fcntl(int fd, int cmd, int arg);
- X native public int ioctl(int fd, int cmd, int arg);
- X native public int ioctl(int fd, int cmd, int[] arg);
- X native public int ioctl(int fd, int cmd, byte[] arg);
- X native public int ioctl(int fd, int cmd, spi_ioc_transfer_struct arg);
- X native public int open(String path, int flags);
- X native public int close(int fd);
- X native public String strerror(int errno);
- X native public int tcgetattr(int fd, termios_struct termios);
- X native public int tcsetattr(int fd, int cmd, termios_struct termios);
- X native public int cfsetispeed(termios_struct termios, NativeLong i);
- X native public int cfsetospeed(termios_struct termios, NativeLong i);
- X native public NativeLong cfgetispeed(termios_struct termios);
- X native public NativeLong cfgetospeed(termios_struct termios);
- X native public int write(int fd, ByteBuffer buffer, int count);
- X native public int read(int fd, ByteBuffer buffer, int count);
- X native public int pread(int fd, byte[] buffer, int count, NativeLong offset);
- X native public long write(int fd, ByteBuffer buffer, long count);
- X native public long read(int fd, ByteBuffer buffer, long count);
- X native public long pread(int fd, byte[] buffer, long count, NativeLong offset);
- X native public int write(int fd, byte[] buffer, int count);
- X native public int read(int fd, byte[] buffer, int count);
- X native public long write(int fd, byte[] buffer, long count);
- X native public long read(int fd, byte[] buffer, long count);
- X native public NativeLong lseek(NativeLong fd, NativeLong off, NativeLong whence);
- X native public int select(int n, int[] read, int[] write, int[] error, timeval_struct timeout);
- X native public int poll(int[] fds, int nfds, int timeout);
- X native public int tcflush(int fd, int qs);
- X native public void perror(String msg);
- X native public int tcsendbreak(int fd, int duration);
- X static {
- X try {
- X Native.register("c");
- X } catch (Exception e) {
- X e.printStackTrace();
- X }
- X }
- X }
- X
- X private interface SlowLibrary extends Library {
- X void printf(String format, Object... args);
- X public long memcpy(int[] dst, short[] src, long n);
- X public int memcpy(int[] dst, short[] src, int n);
- X public int pipe(int[] fds);
- X public int tcdrain(int fd);
- X public void cfmakeraw(termios_struct termios);
- X public int fcntl(int fd, int cmd, int arg);
- X public int ioctl(int fd, int cmd, int[] arg);
- X public int ioctl(int fd, int cmd, Pointer arg);
- X// public int ioctl(int fd, int cmd, serial arg);
- X public int open(String path, int flags);
- X public int close(int fd);
- X public int tcgetattr(int fd, termios_struct termios);
- X public int tcsetattr(int fd, int cmd, termios_struct termios);
- X public int cfsetispeed(termios_struct termios, NativeLong i);
- X public int cfsetospeed(termios_struct termios, NativeLong i);
- X public NativeLong cfgetispeed(termios_struct termios);
- X public NativeLong cfgetospeed(termios_struct termios);
- X public int write(int fd, byte[] buffer, int count);
- X public int read(int fd, byte[] buffer, int count);
- X public long write(int fd, byte[] buffer, long count);
- X public long read(int fd, byte[] buffer, long count);
- X public NativeLong lseek(NativeLong fd, NativeLong off, NativeLong whence);
- X public int select(int n, int[] read, int[] write, int[] error, timeval_struct timeout);
- X public int tcflush(int fd, int qs);
- X public void perror(String msg);
- X public int tcsendbreak(int fd, int duration);
- X }
- X
- X public static class timeval_struct extends Structure {
- X public NativeLong tv_sec;
- X public NativeLong tv_usec;
- X
- X public timeval_struct(TimeVal timeout) {
- X tv_sec = new NativeLong(timeout.tv_sec);
- X tv_usec = new NativeLong(timeout.tv_usec);
- X }
- X
- X @Override protected List getFieldOrder() {
- X return Arrays.asList("tv_sec", "tv_usec");
- X }
- X }
- X
- X /*
- X public static class serial extends Structure {
- X public int type;
- X public int line;
- X public int port;
- X public int irq;
- X public int flags;
- X public int xmit_fifo_size;
- X public int custom_divisor;
- X public int baud_base;
- X public short close_delay;
- X public short io_type;
- X public int hub6;
- X public short closing_wait;
- X public short closing_wait2;
- X public Pointer iomem_base;
- X public short iomem_reg_shift;
- X public int port_high;
- X public NativeLong iomap_base;
- X
- X @Override protected List getFieldOrder() {
- X return Arrays.asList("type", "line", "port", "irq", "flags", "xmit_fifo_size", "custom_divisor", "baud_base", "close_delay", "io_type", "hub6", "closing_wait", "closing_wait2", "iomem_base", "iomem_reg_shift", "port_high", "iomap_base");
- X }
- X };
- X */
- X
- X public static class termios_struct extends Structure {
- X public int c_iflag;
- X public int c_oflag;
- X public int c_cflag;
- X public int c_lflag;
- X public byte c_line;
- X public byte[] c_cc = new byte[32];
- X public int c_ispeed;
- X public int c_ospeed;
- X
- X public termios_struct() {
- X }
- X
- X public termios_struct(Termios t) {
- X c_iflag = t.c_iflag;
- X c_oflag = t.c_oflag;
- X c_cflag = t.c_cflag;
- X c_lflag = t.c_lflag;
- X System.arraycopy(t.c_cc, 0, c_cc, 0, t.c_cc.length);
- X c_ispeed = t.c_ispeed;
- X c_ospeed = t.c_ospeed;
- X }
- X
- X public void update(Termios t) {
- X t.c_iflag = c_iflag;
- X t.c_oflag = c_oflag;
- X t.c_cflag = c_cflag;
- X t.c_lflag = c_lflag;
- X System.arraycopy(c_cc, 0, t.c_cc, 0, t.c_cc.length);
- X t.c_ispeed = c_ispeed;
- X t.c_ospeed = c_ospeed;
- X }
- X
- X @Override protected List getFieldOrder() {
- X return Arrays.asList("c_iflag", "c_oflag", "c_cflag", "c_lflag", "c_line", "c_cc", "c_ispeed", "c_ospeed");
- X }
- X }
- X
- X public static final class FDSet {
- X static final int FD_SET_SIZE = 1024;
- X static final int NFBBITS = 32;
- X int[] bits = new int[(FD_SET_SIZE + NFBBITS - 1) / NFBBITS];
- X
- X public String toString() {
- X return String.format("%08X%08X", bits[0], bits[1]);
- X }
- X }
- X
- X public static final class Pollfd {
- X int fd;
- X int events;
- X
- X public Pollfd() {
- X }
- X
- X public Pollfd(int fd, short events) {
- X this.fd = fd;
- X this.events = eventmask[events&7];
- X }
- X
- X public void init(int fd, short events) {
- X this.fd = fd;
- X this.events = eventmask[events&7];
- X }
- X
- X boolean isIn() {
- X return (events & POLLIN_OUT) != 0;
- X }
- X boolean isOut() {
- X return (events & POLLOUT_OUT) != 0;
- X }
- X boolean isPri() {
- X return (events & POLLPRI_OUT) != 0;
- X }
- X boolean isErr() {
- X return (events & POLLERR_OUT) != 0;
- X }
- X boolean isHup() {
- X return (events & POLLHUP_OUT) != 0;
- X }
- X boolean isNval() {
- X return (events & POLLNVAL_OUT) != 0;
- X }
- X
- X private static final int POLLIN_OUT = pollMask(1, POLLIN);
- X private static final int POLLPRI_OUT = pollMask(1, POLLPRI);
- X private static final int POLLOUT_OUT = pollMask(1, POLLOUT);
- X private static final int POLLERR_OUT = pollMask(1, (short)0x08);
- X private static final int POLLHUP_OUT = pollMask(1, (short)0x10);
- X private static final int POLLNVAL_OUT = pollMask(1, (short)0x20);
- X private static int pollMask(int out, short n) {
- X short[] s = new short[2];
- X int[] d = new int[1];
- X s[out] = n;
- X if (IS64B) {
- X libcfast.memcpy(d, s, (long) 4);
- X } else {
- X libcfast.memcpy(d, s, (int) 4);
- X }
- X return d[0];
- X }
- X private static final int[] eventmask = new int[8];
- X static {
- X for (short i=0;i<8;i++) {
- X eventmask[i] = pollMask(0, i);
- X }
- X }
- X }
- X
- X public static final class TimeVal {
- X public long tv_sec;
- X public long tv_usec;
- X public TimeVal(long sec, long usec) {
- X this.tv_sec = sec;
- X this.tv_usec = usec;
- X }
- X public String toString() {
- X return String.format("%d.%06d", tv_sec, tv_usec);
- X }
- X }
- X
- X public static final class Termios {
- X public int c_iflag;
- X public int c_oflag;
- X public int c_cflag;
- X public int c_lflag;
- X public byte[] c_cc = new byte[20];
- X public int c_ispeed;
- X public int c_ospeed;
- X public void set(Termios s) {
- X c_iflag = s.c_iflag;
- X c_oflag = s.c_oflag;
- X c_cflag = s.c_cflag;
- X c_lflag = s.c_lflag;
- X System.arraycopy(s.c_cc, 0, c_cc, 0, c_cc.length);
- X c_ispeed = s.c_ispeed;
- X c_ospeed = s.c_ospeed;
- X }
- X }
- X
- X public static class spi_ioc_transfer_struct extends Structure {
- X public long tx_buf;
- X public long rx_buf;
- X public int len;
- X public int speed_hz;
- X public short delay_usecs;
- X public byte bits_per_word;
- X public byte cs_change;
- X public int pad;
- X
- X @Override protected List getFieldOrder() {
- X return Arrays.asList("tx_buf", "rx_buf", "len", "speed_hz", "delay_usecs", "bits_per_word", "cs_change", "pad");
- X }
- X }
- X
- X}
- END-of-POSIX.java
- exit
Add Comment
Please, Sign In to add comment