From ad30f6c487a931ab0c8fe10ba3c8b188ceeb669d Mon Sep 17 00:00:00 2001 From: DigitalSmile Date: Mon, 29 Apr 2024 16:46:44 +0300 Subject: [PATCH] Fix bug with index in ffm api --- .../gpio/core/poll/PollingData.java | 12 ++++----- .../gpio/i2c/structs/SMBusData.java | 8 +++--- .../gpio/i2c/structs/SMBusIoctlData.java | 16 ++++++------ .../gpio/pin/structs/ChipInfo.java | 6 ++--- .../gpio/pin/structs/LineAttribute.java | 18 ++++++------- .../gpio/pin/structs/LineConfig.java | 14 +++++----- .../gpio/pin/structs/LineConfigAttribute.java | 8 +++--- .../gpio/pin/structs/LineEvent.java | 22 ++++++++-------- .../gpio/pin/structs/LineInfo.java | 26 +++++++++---------- .../gpio/pin/structs/LineRequest.java | 26 +++++++++---------- .../gpio/pin/structs/LineValues.java | 8 +++--- 11 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/main/java/org/digitalsmile/gpio/core/poll/PollingData.java b/src/main/java/org/digitalsmile/gpio/core/poll/PollingData.java index 7cd07ea..623b27f 100644 --- a/src/main/java/org/digitalsmile/gpio/core/poll/PollingData.java +++ b/src/main/java/org/digitalsmile/gpio/core/poll/PollingData.java @@ -34,17 +34,17 @@ public MemoryLayout getMemoryLayout() { @Override public PollingData fromBytes(MemorySegment buffer) throws Throwable { return new PollingData( - (int) FD_FLAGS.get(buffer), - (short) EVENTS_FLAGS.get(buffer), - (short) REVENTS_FLAGS.get(buffer) + (int) FD_FLAGS.get(buffer, 0L), + (short) EVENTS_FLAGS.get(buffer, 0L), + (short) REVENTS_FLAGS.get(buffer, 0L) ); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - FD_FLAGS.set(buffer, fd); - EVENTS_FLAGS.set(buffer, events); - REVENTS_FLAGS.set(buffer, revents); + FD_FLAGS.set(buffer, 0L, fd); + EVENTS_FLAGS.set(buffer, 0L, events); + REVENTS_FLAGS.set(buffer, 0L, revents); } @Override diff --git a/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusData.java b/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusData.java index d19aa08..6f775e5 100644 --- a/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusData.java +++ b/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusData.java @@ -76,15 +76,15 @@ public MemoryLayout getMemoryLayout() { public SMBusData fromBytes(MemorySegment buffer) throws Throwable { var _byte = (byte) VH_BYTE.get(buffer); var word = (byte) VH_WORD.get(buffer); - var block = ((MemorySegment) MH_BLOCK.invokeExact(buffer)).toArray(ValueLayout.JAVA_BYTE); + var block = ((MemorySegment) MH_BLOCK.invokeExact(buffer, 0L)).toArray(ValueLayout.JAVA_BYTE); return new SMBusData(_byte, word, block); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - VH_BYTE.set(buffer, _byte); - VH_WORD.set(buffer, word); - var tmp = ((MemorySegment) MH_BLOCK.invokeExact(buffer)); + VH_BYTE.set(buffer, 0L, _byte); + VH_WORD.set(buffer, 0L, word); + var tmp = ((MemorySegment) MH_BLOCK.invokeExact(buffer, 0L)); for (int i = 0; i < block.length; i++) { tmp.setAtIndex(ValueLayout.JAVA_BYTE, i, block[i]); } diff --git a/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusIoctlData.java b/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusIoctlData.java index faced7a..b9fd838 100644 --- a/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusIoctlData.java +++ b/src/main/java/org/digitalsmile/gpio/i2c/structs/SMBusIoctlData.java @@ -39,22 +39,22 @@ public MemoryLayout getMemoryLayout() { @SuppressWarnings("unchecked") @Override public SMBusIoctlData fromBytes(MemorySegment buffer) throws Throwable { - var readWrite = (byte) VH_READ_WRITE.get(buffer); - var command = (byte) VH_COMMAND.get(buffer); - var size = (int) VH_SIZE.get(buffer); - var tmp = (MemorySegment) MH_DATA.get(buffer); + var readWrite = (byte) VH_READ_WRITE.get(buffer, 0L); + var command = (byte) VH_COMMAND.get(buffer, 0L); + var size = (int) VH_SIZE.get(buffer, 0L); + var tmp = (MemorySegment) MH_DATA.get(buffer, 0L); var data0 = data.fromBytes(tmp); return new SMBusIoctlData(readWrite, command, size, data0); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - VH_READ_WRITE.set(buffer, readWrite); - VH_COMMAND.set(buffer, command); - VH_SIZE.set(buffer, size); + VH_READ_WRITE.set(buffer, 0L, readWrite); + VH_COMMAND.set(buffer, 0L, command); + VH_SIZE.set(buffer, 0L, size); var smbusOffHeap = offHeap.allocate(SMBusData.LAYOUT); data.toBytes(smbusOffHeap); - MH_DATA.set(buffer, smbusOffHeap); + MH_DATA.set(buffer, 0L, smbusOffHeap); } @Override diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/ChipInfo.java b/src/main/java/org/digitalsmile/gpio/pin/structs/ChipInfo.java index 9315ceb..570304f 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/ChipInfo.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/ChipInfo.java @@ -47,7 +47,7 @@ public ChipInfo fromBytes(MemorySegment buffer) throws Throwable { return new ChipInfo( invokeExact(MH_NAME, buffer).toArray(ValueLayout.JAVA_BYTE), invokeExact(MH_LABEL, buffer).toArray(ValueLayout.JAVA_BYTE), - (int) VH_LINES.get(buffer) + (int) VH_LINES.get(buffer, 0L) ); } @@ -61,11 +61,11 @@ public void toBytes(MemorySegment buffer) throws Throwable { for (int i = 0; i < label.length; i++) { tmp.setAtIndex(ValueLayout.JAVA_INT, i, label[i]); } - VH_LINES.set(buffer, lines); + VH_LINES.set(buffer, 0L, lines); } private static MemorySegment invokeExact(MethodHandle handle, MemorySegment buffer) throws Throwable { - return ((MemorySegment) handle.invokeExact(buffer)); + return ((MemorySegment) handle.invokeExact(buffer, 0L)); } @Override diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineAttribute.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineAttribute.java index 3a0373d..5a50452 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineAttribute.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineAttribute.java @@ -68,15 +68,15 @@ public MemoryLayout getMemoryLayout() { @SuppressWarnings("unchecked") @Override public LineAttribute fromBytes(MemorySegment buffer) throws Throwable { - var id = (int) VH_ID.get(buffer); + var id = (int) VH_ID.get(buffer, 0L); long flags = 0, values = 0; int debouncePeriodUs = 0; var unionSize = LAYOUT.select(MemoryLayout.PathElement.groupElement("union")).byteSize(); var unionBuffer = buffer.asSlice(LAYOUT.byteSize() - unionSize, unionSize); switch (AttributeId.getByValue(id)) { - case FLAGS -> flags = (long) VH_FLAGS.get(unionBuffer); - case OUTPUT_VALUES -> values = (long) VH_VALUES.get(unionBuffer); - case ID_DEBOUNCE -> debouncePeriodUs = (int) VH_DEBOUNCE_PERIOD_US.get(unionBuffer); + case FLAGS -> flags = (long) VH_FLAGS.get(unionBuffer, 0L); + case OUTPUT_VALUES -> values = (long) VH_VALUES.get(unionBuffer, 0L); + case ID_DEBOUNCE -> debouncePeriodUs = (int) VH_DEBOUNCE_PERIOD_US.get(unionBuffer, 0L); case null, default -> throw new NativeMemoryException("id value is not one of " + Arrays.toString(AttributeId.values())); } @@ -91,15 +91,15 @@ public LineAttribute fromBytes(MemorySegment buffer) throws Throwable { @Override public void toBytes(MemorySegment buffer) throws Throwable { - VH_ID.set(buffer, id); - VH_PADDING.set(buffer, 0); + VH_ID.set(buffer, 0L, id); + VH_PADDING.set(buffer, 0L, 0); var unionSize = LAYOUT.select(MemoryLayout.PathElement.groupElement("union")).byteSize(); var unionBuffer = buffer.asSlice(LAYOUT.byteSize() - unionSize, unionSize); switch (AttributeId.getByValue(id)) { - case FLAGS -> VH_FLAGS.set(unionBuffer, flags); - case OUTPUT_VALUES -> VH_VALUES.set(unionBuffer, values); - case ID_DEBOUNCE -> VH_DEBOUNCE_PERIOD_US.set(unionBuffer, debouncePeriodUs); + case FLAGS -> VH_FLAGS.set(unionBuffer, 0L, flags); + case OUTPUT_VALUES -> VH_VALUES.set(unionBuffer, 0L, values); + case ID_DEBOUNCE -> VH_DEBOUNCE_PERIOD_US.set(unionBuffer, 0L, debouncePeriodUs); case null, default -> throw new NativeMemoryException("id value is not one of " + Arrays.toString(AttributeId.values())); } diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfig.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfig.java index 71b2e57..eca58da 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfig.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfig.java @@ -45,15 +45,15 @@ public MemoryLayout getMemoryLayout() { @SuppressWarnings("unchecked") @Override public LineConfig fromBytes(MemorySegment buffer) throws Throwable { - var numAttrs = (int) VH_NUM_ATTRS.get(buffer); - var attrsMemorySegment = (MemorySegment) MH_ATTRS.invokeExact(buffer); + var numAttrs = (int) VH_NUM_ATTRS.get(buffer, 0L); + var attrsMemorySegment = (MemorySegment) MH_ATTRS.invokeExact(buffer, 0L); var attrs = new LineConfigAttribute[numAttrs]; for (int i = 0; i < numAttrs; i++) { var attr = LineConfigAttribute.createEmpty(); attrs[i] = attr.fromBytes(attrsMemorySegment.asSlice(LineConfigAttribute.LAYOUT.byteSize() * i, LineConfigAttribute.LAYOUT.byteSize())); } return new LineConfig( - (long) VH_FLAGS.get(buffer), + (long) VH_FLAGS.get(buffer, 0L), numAttrs, attrs ); @@ -61,13 +61,13 @@ public LineConfig fromBytes(MemorySegment buffer) throws Throwable { @Override public void toBytes(MemorySegment buffer) throws Throwable { - VH_FLAGS.set(buffer, flags); - VH_NUM_ATTRS.set(buffer, numAttrs); - var tmp = (MemorySegment) MH_PADDING.invokeExact(buffer); + VH_FLAGS.set(buffer, 0L, flags); + VH_NUM_ATTRS.set(buffer, 0L, numAttrs); + var tmp = (MemorySegment) MH_PADDING.invokeExact(buffer, 0L); for (int i = 0; i < 5; i++) { tmp.setAtIndex(ValueLayout.JAVA_INT, i, 0); } - tmp = (MemorySegment) MH_ATTRS.invokeExact(buffer); + tmp = (MemorySegment) MH_ATTRS.invokeExact(buffer, 0L); for (int i = 0; i < numAttrs; i++) { attrs[i].toBytes(tmp.asSlice(LineConfigAttribute.LAYOUT.byteSize() * i, LineConfigAttribute.LAYOUT.byteSize())); } diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfigAttribute.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfigAttribute.java index c9b8c8b..b6318a7 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfigAttribute.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineConfigAttribute.java @@ -39,20 +39,20 @@ public MemoryLayout getMemoryLayout() { @SuppressWarnings("unchecked") @Override public LineConfigAttribute fromBytes(MemorySegment buffer) throws Throwable { - var attrMemorySegment = (MemorySegment) MH_ATTR.invokeExact(buffer); + var attrMemorySegment = (MemorySegment) MH_ATTR.invokeExact(buffer, 0L); var attr = LineAttribute.createEmpty().fromBytes(attrMemorySegment); return new LineConfigAttribute( attr, - (long) VH_MASK.get(buffer) + (long) VH_MASK.get(buffer, 0L) ); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - var attrMemorySegment = (MemorySegment) MH_ATTR.invokeExact(buffer); + var attrMemorySegment = (MemorySegment) MH_ATTR.invokeExact(buffer, 0L); attr.toBytes(attrMemorySegment); - VH_MASK.set(buffer, mask); + VH_MASK.set(buffer, 0L, mask); } @Override diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineEvent.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineEvent.java index fcb6ed3..72b0af0 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineEvent.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineEvent.java @@ -54,23 +54,23 @@ public MemoryLayout getMemoryLayout() { @Override public LineEvent fromBytes(MemorySegment buffer) throws Throwable { return new LineEvent( - (long) VH_TIMESTAMP_NS.get(buffer), - (int) VH_ID.get(buffer), - (int) VH_OFFSET.get(buffer), - (int) VH_SEQ_NO.get(buffer), - (int) VH_LINE_SEQ_NO.get(buffer) + (long) VH_TIMESTAMP_NS.get(buffer, 0L), + (int) VH_ID.get(buffer, 0L), + (int) VH_OFFSET.get(buffer, 0L), + (int) VH_SEQ_NO.get(buffer, 0L), + (int) VH_LINE_SEQ_NO.get(buffer, 0L) ); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - VH_TIMESTAMP_NS.set(buffer, timestampNs); - VH_ID.set(buffer, id); - VH_OFFSET.set(buffer, offset); - VH_SEQ_NO.set(buffer, seqNo); - VH_LINE_SEQ_NO.set(buffer, lineSeqNo); + VH_TIMESTAMP_NS.set(buffer, 0L, timestampNs); + VH_ID.set(buffer, 0L, id); + VH_OFFSET.set(buffer, 0L, offset); + VH_SEQ_NO.set(buffer, 0L, seqNo); + VH_LINE_SEQ_NO.set(buffer, 0L, lineSeqNo); - var tmp = (MemorySegment) MH_PADDING.invokeExact(buffer); + var tmp = (MemorySegment) MH_PADDING.invokeExact(buffer, 0L); for (int i = 0; i < 5; i++) { tmp.setAtIndex(ValueLayout.JAVA_INT, i, 0); } diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineInfo.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineInfo.java index 18f17e5..1b8c32f 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineInfo.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineInfo.java @@ -56,42 +56,42 @@ public MemoryLayout getMemoryLayout() { @SuppressWarnings("unchecked") @Override public LineInfo fromBytes(MemorySegment buffer) throws Throwable { - var numAttrs = (int) VH_NUM_ATTRS.get(buffer); - var attrsMemorySegment = (MemorySegment) MH_ATTRS.invokeExact(buffer); + var numAttrs = (int) VH_NUM_ATTRS.get(buffer, 0L); + var attrsMemorySegment = (MemorySegment) MH_ATTRS.invokeExact(buffer, 0L); var attrs = new LineAttribute[numAttrs]; for (int i = 0; i < numAttrs; i++) { var attr = LineAttribute.createEmpty(); attrs[i] = attr.fromBytes(attrsMemorySegment.asSlice(LineAttribute.LAYOUT.byteSize() * i, LineAttribute.LAYOUT.byteSize())); } return new LineInfo( - ((MemorySegment) MH_NAME.invokeExact(buffer)).toArray(ValueLayout.JAVA_BYTE), - ((MemorySegment) MH_CONSUMER.invokeExact(buffer)).toArray(ValueLayout.JAVA_BYTE), - (int) VH_OFFSET.get(buffer), + ((MemorySegment) MH_NAME.invokeExact(buffer, 0L)).toArray(ValueLayout.JAVA_BYTE), + ((MemorySegment) MH_CONSUMER.invokeExact(buffer, 0L)).toArray(ValueLayout.JAVA_BYTE), + (int) VH_OFFSET.get(buffer, 0L), numAttrs, - (long) VH_FLAGS.get(buffer), + (long) VH_FLAGS.get(buffer, 0L), attrs ); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - var tmp = (MemorySegment) MH_NAME.invokeExact(buffer); + var tmp = (MemorySegment) MH_NAME.invokeExact(buffer, 0L); for (int i = 0; i < name.length; i++) { tmp.setAtIndex(ValueLayout.JAVA_BYTE, i, name[i]); } - tmp = (MemorySegment) MH_CONSUMER.invokeExact(buffer); + tmp = (MemorySegment) MH_CONSUMER.invokeExact(buffer, 0L); for (int i = 0; i < consumer.length; i++) { tmp.setAtIndex(ValueLayout.JAVA_BYTE, i, consumer[i]); } - VH_OFFSET.set(buffer, offset); - VH_NUM_ATTRS.set(buffer, numAttrs); - VH_FLAGS.set(buffer, flags); + VH_OFFSET.set(buffer, 0L, offset); + VH_NUM_ATTRS.set(buffer, 0L,numAttrs); + VH_FLAGS.set(buffer, 0L, flags); - tmp = (MemorySegment) MH_ATTRS.invokeExact(buffer); + tmp = (MemorySegment) MH_ATTRS.invokeExact(buffer, 0L); for (int i = 0; i < numAttrs; i++) { attrs[i].toBytes(tmp.asSlice(LineAttribute.LAYOUT.byteSize() * i, LineAttribute.LAYOUT.byteSize())); } - tmp = (MemorySegment) MH_PADDING.invokeExact(buffer); + tmp = (MemorySegment) MH_PADDING.invokeExact(buffer, 0L); for (int i = 0; i < 4; i++) { tmp.setAtIndex(ValueLayout.JAVA_INT, i, 0); } diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineRequest.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineRequest.java index 8dfebe0..9c6fd94 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineRequest.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineRequest.java @@ -59,37 +59,37 @@ public MemoryLayout getMemoryLayout() { @SuppressWarnings("unchecked") @Override public LineRequest fromBytes(MemorySegment buffer) throws Throwable { - var lineConfigMemoryBuffer = (MemorySegment) MH_CONFIG.invokeExact(buffer); + var lineConfigMemoryBuffer = (MemorySegment) MH_CONFIG.invokeExact(buffer, 0L); var lineConfig = LineConfig.createEmpty().fromBytes(lineConfigMemoryBuffer); return new LineRequest( - ((MemorySegment) MH_OFFSETS.invokeExact(buffer)).toArray(ValueLayout.JAVA_INT), - ((MemorySegment) MH_CONSUMER.invokeExact(buffer)).toArray(ValueLayout.JAVA_BYTE), + ((MemorySegment) MH_OFFSETS.invokeExact(buffer, 0L)).toArray(ValueLayout.JAVA_INT), + ((MemorySegment) MH_CONSUMER.invokeExact(buffer, 0L)).toArray(ValueLayout.JAVA_BYTE), lineConfig, - (int) VH_NUM_LINES.get(buffer), - (int) VH_EVENT_BUFFER_SIZE.get(buffer), - (int) VH_FD.get(buffer) + (int) VH_NUM_LINES.get(buffer, 0L), + (int) VH_EVENT_BUFFER_SIZE.get(buffer, 0L), + (int) VH_FD.get(buffer, 0L) ); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - var tmp = (MemorySegment) MH_OFFSETS.invokeExact(buffer); + var tmp = (MemorySegment) MH_OFFSETS.invokeExact(buffer, 0L); for (int i = 0; i < offsets.length; i++) { tmp.setAtIndex(ValueLayout.JAVA_INT, i, offsets[i]); } - tmp = (MemorySegment) MH_CONSUMER.invokeExact(buffer); + tmp = (MemorySegment) MH_CONSUMER.invokeExact(buffer, 0L); for (int i = 0; i < consumer.length; i++) { tmp.setAtIndex(ValueLayout.JAVA_BYTE, i, consumer[i]); } - tmp = (MemorySegment) MH_CONFIG.invokeExact(buffer); + tmp = (MemorySegment) MH_CONFIG.invokeExact(buffer, 0L); config.toBytes(tmp); - VH_NUM_LINES.set(buffer, numLines); - VH_EVENT_BUFFER_SIZE.set(buffer, eventBufferSize); - tmp = (MemorySegment) MH_PADDING.invokeExact(buffer); + VH_NUM_LINES.set(buffer, 0L, numLines); + VH_EVENT_BUFFER_SIZE.set(buffer, 0L, eventBufferSize); + tmp = (MemorySegment) MH_PADDING.invokeExact(buffer, 0L); for (int i = 0; i < 5; i++) { tmp.setAtIndex(ValueLayout.JAVA_INT, i, 0); } - VH_FD.set(buffer, fd); + VH_FD.set(buffer, 0L, fd); } @Override diff --git a/src/main/java/org/digitalsmile/gpio/pin/structs/LineValues.java b/src/main/java/org/digitalsmile/gpio/pin/structs/LineValues.java index 1db0c1f..0e60b0f 100644 --- a/src/main/java/org/digitalsmile/gpio/pin/structs/LineValues.java +++ b/src/main/java/org/digitalsmile/gpio/pin/structs/LineValues.java @@ -30,14 +30,14 @@ public MemoryLayout getMemoryLayout() { @Override public LineValues fromBytes(MemorySegment buffer) throws Throwable { return new LineValues( - (long) VH_BITS.get(buffer), - (long) VH_MASK.get(buffer) + (long) VH_BITS.get(buffer, 0L), + (long) VH_MASK.get(buffer, 0L) ); } @Override public void toBytes(MemorySegment buffer) throws Throwable { - VH_BITS.set(buffer, bits); - VH_MASK.set(buffer, mask); + VH_BITS.set(buffer, 0L, bits); + VH_MASK.set(buffer, 0L, mask); } }