Skip to content

Commit 4716964

Browse files
committed
irmHexUtils - fix V2 checksums
1 parent 0daa359 commit 4716964

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

app/src/main/java/com/samsung/microbit/utils/irmHexUtils.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public void scanInit() {
6464
resultAddrNext = 0;
6565
}
6666

67+
public static byte digittohex( int d) {
68+
if ( d >= 0 && d <= 9) {
69+
return (byte) ( '0' + d);
70+
}
71+
if ( d >= 10 && d <= 16) {
72+
return (byte) ( 'A' + ( d - 10));
73+
}
74+
return -1;
75+
}
76+
6777
public static int hextodigit( final byte c) {
6878
if ( c >= '0' && c <= '9') {
6979
return c - '0';
@@ -199,6 +209,29 @@ public static int lineCheck( final byte [] hex, final int hexIdx) {
199209
return hextobyte( hex, hexIdx + 9 + count * 2);
200210
}
201211

212+
public static boolean setCheck( byte [] hex, final int hexIdx) {
213+
int sum = calcSum(hex, hexIdx);
214+
if (sum < 0) {
215+
return false;
216+
}
217+
int check = (sum == 0 ? 0 : (256 - sum));
218+
int count = hextobyte( hex, hexIdx + 1);
219+
int checkIdx = hexIdx + 9 + count * 2;
220+
hex[ checkIdx] = digittohex(check / 16);
221+
hex[ checkIdx + 1] = digittohex(check % 16);
222+
223+
int chk = irmHexUtils.lineCheck( hex, hexIdx);
224+
if ( chk < 0) {
225+
return false;
226+
}
227+
sum = irmHexUtils.calcSum( hex, hexIdx);
228+
sum = ( chk + sum) % 256;
229+
if ( sum != 0 ) {
230+
return false;
231+
}
232+
return true;
233+
}
234+
202235
public static boolean lineData( final byte [] hex, final int hexIdx, byte [] data, final int idx) {
203236
int count = hextobyte( hex, hexIdx + 1);
204237
if ( count < 0)
@@ -339,7 +372,6 @@ public int scanForDataHex( byte [] datahex, final int hexBlock, final byte [] un
339372
if ( !isUniversal || dataWanted) {
340373
long fullAddr = lastBaseAddr + lineAddr;
341374
if ( fullAddr + lineCount > scanAddrMin && fullAddr < scanAddrNext) {
342-
// TODO support part lines?
343375
if ( resultAddrMin > fullAddr) {
344376
resultAddrMin = fullAddr;
345377
}
@@ -350,6 +382,9 @@ public int scanForDataHex( byte [] datahex, final int hexBlock, final byte [] un
350382
System.arraycopy(universalhex, lineHidx, datahex, hexSize, rlen);
351383
datahex[hexSize + 7] = '0';
352384
datahex[hexSize + 8] = '0';
385+
if ( !setCheck( datahex, hexSize)) {
386+
return 0;
387+
}
353388
}
354389
lastSize = hexSize;
355390
lastType = lineType;
@@ -459,4 +494,4 @@ public boolean universalHexToApplicationHex( final byte [] universalHex, final i
459494
scanAddrNext = mnp[1];
460495
return scanForDataHex( universalHex, hexBlock);
461496
}
462-
};
497+
};

0 commit comments

Comments
 (0)