Skip to content

Commit 0d24468

Browse files
authored
Merge pull request #153 from tensoid/master
Fix apdu construction and chunking in MP::writeMemory (#152)
2 parents 1ebd6f4 + dfa3d2d commit 0d24468

File tree

1 file changed

+4
-31
lines changed

1 file changed

+4
-31
lines changed

src/io/calimero/mgmt/ManagementProceduresImpl.java

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -667,32 +667,11 @@ public void writeMemory(final IndividualAddress device, final long startAddress,
667667
1, 1, ctrl);
668668
}
669669

670-
// create structure to write
671-
/*
672-
Byte structure of data transmitted in ASDUs:
673-
| 0 | 1 | 2 3 4 5 | 6 7 8 9 |10 11 12 13 |14..31|
674-
| code | flags | dataBlockStartAddr | startAddress | endAddress | data |
675-
| 0x20 | ... | ... | ... | ... | ... |
676-
*/
677-
final int dataBlockStartAddress = 0;
678-
// int flags = dataBlockStartAddress == 0 ? 0x01 : 0;
679-
int flags = 0x01;
680-
flags |= verifyWrite ? 0x02 : 0;
681-
final long endAddress = startAddress + data.length;
682-
683-
final int offset = 14;
684-
final byte[] write = new byte[offset + data.length];
685-
write[0] = 0x20;
686-
write[1] = (byte) flags;
687-
setAddress(write, 2, dataBlockStartAddress);
688-
setAddress(write, 6, startAddress);
689-
setAddress(write, 10, endAddress);
690-
System.arraycopy(data, 0, write, 14, data.length);
691-
692-
// write memory in chunks matching the maximum asdu length of the device
670+
// write memory in chunks with a maximum length of asduLength
693671
final int asduLength = readMaxAsduLength(d);
694-
for (int i = 0; i < write.length; i += asduLength) {
695-
final byte[] range = Arrays.copyOfRange(write, i, i + asduLength);
672+
for (int i = 0; i < data.length; i += asduLength) {
673+
int remainingBytes = data.length - i;
674+
final byte[] range = Arrays.copyOfRange(data, i, i + Math.min(asduLength, remainingBytes));
696675

697676
// on server verification, our mgmt client will already compare the response value
698677
mc.writeMemory(d, (int) startAddress + i, range);
@@ -965,10 +944,4 @@ private static boolean isOddParity(final int bite)
965944
parity ^= parity >> 1;
966945
return (parity & 0x1) != 0;
967946
}
968-
969-
private static void setAddress(final byte[] data, final int from, final long address)
970-
{
971-
for (int i = 0; i < 4; ++i)
972-
data[from + i] = (byte) (address >> (3 - i) & 0xff);
973-
}
974947
}

0 commit comments

Comments
 (0)