|
| 1 | +package com.pi4j.plugin.ffm.common.spi; |
| 2 | + |
| 3 | +import com.pi4j.plugin.ffm.common.Pi4JLayout; |
| 4 | + |
| 5 | +import java.lang.foreign.Arena; |
| 6 | +import java.lang.foreign.MemoryLayout; |
| 7 | +import java.lang.foreign.MemorySegment; |
| 8 | +import java.lang.foreign.ValueLayout; |
| 9 | +import java.lang.invoke.VarHandle; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +import static java.lang.foreign.MemoryLayout.PathElement.groupElement; |
| 13 | + |
| 14 | +/** |
| 15 | + * Source: /usr/src/linux-headers-6.8.0-52-generic/include/linux/spi/spidev.h:70:0 |
| 16 | + */ |
| 17 | +public record SpiIocTransfer(byte[] txBuf, byte[] rxBuf, int length, int speedHz, short delayUsecs, byte bitsPerWord, |
| 18 | + byte csChange, byte txNbits, byte rxNbits, byte wordDelayUsecs, |
| 19 | + byte pad) implements Pi4JLayout { |
| 20 | + |
| 21 | + public SpiIocTransfer(byte[] txBuf, byte[] rxBuf, int length) { |
| 22 | + this(txBuf, rxBuf, length, 0, (short) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0); |
| 23 | + } |
| 24 | + |
| 25 | + public static final MemoryLayout LAYOUT = MemoryLayout.structLayout( |
| 26 | + ValueLayout.JAVA_LONG.withName("tx_buf"), |
| 27 | + ValueLayout.JAVA_LONG.withName("rx_buf"), |
| 28 | + ValueLayout.JAVA_INT.withName("len"), |
| 29 | + ValueLayout.JAVA_INT.withName("speed_hz"), |
| 30 | + ValueLayout.JAVA_SHORT.withName("delay_usecs"), |
| 31 | + ValueLayout.JAVA_BYTE.withName("bits_per_word"), |
| 32 | + ValueLayout.JAVA_BYTE.withName("cs_change"), |
| 33 | + ValueLayout.JAVA_BYTE.withName("tx_nbits"), |
| 34 | + ValueLayout.JAVA_BYTE.withName("rx_nbits"), |
| 35 | + ValueLayout.JAVA_BYTE.withName("word_delay_usecs"), |
| 36 | + ValueLayout.JAVA_BYTE.withName("pad") |
| 37 | + ); |
| 38 | + |
| 39 | + private static final VarHandle VH_TX_BUF = LAYOUT.varHandle(groupElement("tx_buf")); |
| 40 | + private static final VarHandle VH_RX_BUF = LAYOUT.varHandle(groupElement("rx_buf")); |
| 41 | + |
| 42 | + private static final VarHandle VH_LEN = LAYOUT.varHandle(groupElement("len")); |
| 43 | + private static final VarHandle VH_SPEED_HZ = LAYOUT.varHandle(groupElement("speed_hz")); |
| 44 | + private static final VarHandle VH_DELAY_USECS = LAYOUT.varHandle(groupElement("delay_usecs")); |
| 45 | + private static final VarHandle VH_BITS_PER_WORD = LAYOUT.varHandle(groupElement("bits_per_word")); |
| 46 | + private static final VarHandle VH_CS_CHANGE = LAYOUT.varHandle(groupElement("cs_change")); |
| 47 | + private static final VarHandle VH_TX_NBITS = LAYOUT.varHandle(groupElement("tx_nbits")); |
| 48 | + private static final VarHandle VH_RX_NBITS = LAYOUT.varHandle(groupElement("rx_nbits")); |
| 49 | + private static final VarHandle VH_WORD_DELAY_USECS = LAYOUT.varHandle(groupElement("word_delay_usecs")); |
| 50 | + private static final VarHandle VH_PAD = LAYOUT.varHandle(groupElement("pad")); |
| 51 | + |
| 52 | + |
| 53 | + public static SpiIocTransfer create(MemorySegment memorySegment) throws Throwable { |
| 54 | + var spiIocTransferInstance = SpiIocTransfer.createEmpty(); |
| 55 | + if (!memorySegment.equals(MemorySegment.NULL)) { |
| 56 | + spiIocTransferInstance = spiIocTransferInstance.from(memorySegment); |
| 57 | + } |
| 58 | + return spiIocTransferInstance; |
| 59 | + } |
| 60 | + |
| 61 | + public static SpiIocTransfer createEmpty() { |
| 62 | + return new SpiIocTransfer(new byte[]{}, new byte[]{}, 0, 0, (short) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public MemoryLayout getMemoryLayout() { |
| 67 | + return LAYOUT; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + @SuppressWarnings("unchecked") |
| 72 | + public SpiIocTransfer from(MemorySegment buffer) throws Throwable { |
| 73 | + return new SpiIocTransfer( |
| 74 | + txBuf1 != null ? txBuf1.toArray(ValueLayout.JAVA_BYTE) : new byte[]{}, |
| 75 | + rxBuf1 != null ? rxBuf1.toArray(ValueLayout.JAVA_BYTE) : new byte[]{}, |
| 76 | + (int) VH_LEN.get(buffer, 0L), |
| 77 | + (int) VH_SPEED_HZ.get(buffer, 0L), |
| 78 | + (short) VH_DELAY_USECS.get(buffer, 0L), |
| 79 | + (byte) VH_BITS_PER_WORD.get(buffer, 0L), |
| 80 | + (byte) VH_CS_CHANGE.get(buffer, 0L), |
| 81 | + (byte) VH_TX_NBITS.get(buffer, 0L), |
| 82 | + (byte) VH_RX_NBITS.get(buffer, 0L), |
| 83 | + (byte) VH_WORD_DELAY_USECS.get(buffer, 0L), |
| 84 | + (byte) VH_PAD.get(buffer, 0L)); |
| 85 | + } |
| 86 | + |
| 87 | + public static MemorySegment txBuf1; |
| 88 | + public static MemorySegment rxBuf1; |
| 89 | + |
| 90 | + @Override |
| 91 | + public void to(MemorySegment buffer) throws Throwable { |
| 92 | + if (txBuf != null) { |
| 93 | + txBuf1 = Arena.global().allocateFrom(ValueLayout.JAVA_BYTE, txBuf); |
| 94 | + VH_TX_BUF.set(buffer, 0L, txBuf1.address()); |
| 95 | + } |
| 96 | + if (rxBuf != null) { |
| 97 | + rxBuf1 = Arena.global().allocateFrom(ValueLayout.JAVA_BYTE, rxBuf); |
| 98 | + VH_RX_BUF.set(buffer, 0L, rxBuf1.address()); |
| 99 | + } |
| 100 | + VH_LEN.set(buffer, 0L, length); |
| 101 | + VH_SPEED_HZ.set(buffer, 0L, speedHz); |
| 102 | + VH_DELAY_USECS.set(buffer, 0L, delayUsecs); |
| 103 | + VH_BITS_PER_WORD.set(buffer, 0L, bitsPerWord); |
| 104 | + VH_CS_CHANGE.set(buffer, 0L, csChange); |
| 105 | + VH_TX_NBITS.set(buffer, 0L, txNbits); |
| 106 | + VH_RX_NBITS.set(buffer, 0L, rxNbits); |
| 107 | + VH_WORD_DELAY_USECS.set(buffer, 0L, wordDelayUsecs); |
| 108 | + VH_PAD.set(buffer, 0L, pad); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return "SpiIocTransfer{" + |
| 115 | + "txBuf=" + Arrays.toString(txBuf) + |
| 116 | + ", rxBuf=" + Arrays.toString(rxBuf) + |
| 117 | + ", length=" + length + |
| 118 | + ", speedHz=" + speedHz + |
| 119 | + ", delayUsecs=" + delayUsecs + |
| 120 | + ", bitsPerWord=" + bitsPerWord + |
| 121 | + ", csChange=" + csChange + |
| 122 | + ", txNbits=" + txNbits + |
| 123 | + ", rxNbits=" + rxNbits + |
| 124 | + ", wordDelayUsecs=" + wordDelayUsecs + |
| 125 | + ", pad=" + pad + |
| 126 | + '}'; |
| 127 | + } |
| 128 | +} |
0 commit comments