Skip to content

Commit d535b56

Browse files
committed
irmHexUtils - add conversion to binary
1 parent 4716964 commit d535b56

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class irmHexUtils {
4848
public long resultAddrNext;
4949
public int resultDataSize;
5050
public byte [] resultHex;
51+
public byte [] resultData;
5152

5253
public void scanInit() {
5354
scanHexSize = 0;
@@ -494,4 +495,95 @@ public boolean universalHexToApplicationHex( final byte [] universalHex, final i
494495
scanAddrNext = mnp[1];
495496
return scanForDataHex( universalHex, hexBlock);
496497
}
498+
499+
// Extract data from records (0x00, 0x0D)
500+
public int scanForData( byte [] data, final byte [] hex, final int hexSize) {
501+
scanHexSize = hexSize;
502+
503+
for ( lineNext = 0; lineNext < scanHexSize; /*empty*/) {
504+
if (!parseLine( hex))
505+
return 0;
506+
507+
int rlen = lineNext - lineHidx;
508+
if ( rlen == 0)
509+
continue;
510+
511+
switch ( lineType) {
512+
case 0: // Data
513+
case 0x0D:
514+
long fullAddr = lastBaseAddr + lineAddr;
515+
if ( fullAddr + lineCount > scanAddrMin && fullAddr < scanAddrNext) {
516+
int first = (int) ( scanAddrMin > fullAddr ? scanAddrMin - fullAddr : 0);
517+
int next = (int) ( scanAddrNext < fullAddr + lineCount ? scanAddrNext - fullAddr : lineCount);
518+
int length = next - first;
519+
int dataOffset = (int) ( fullAddr + first - scanAddrMin);
520+
if ( data != null) {
521+
byte [] lineBytes = new byte[ lineCount];
522+
if ( !lineData( hex, lineHidx, lineBytes, 0)) {
523+
return 0;
524+
}
525+
System.arraycopy( lineBytes, first, data, dataOffset, length);
526+
}
527+
if ( resultAddrMin > fullAddr + first) {
528+
resultAddrMin = fullAddr + first;
529+
}
530+
if ( resultAddrNext < fullAddr + next) {
531+
resultAddrNext = fullAddr + next;
532+
}
533+
}
534+
break;
535+
default:
536+
break;
537+
}
538+
}
539+
540+
// calculate size of data from scanMin
541+
if ( resultAddrNext > resultAddrMin) {
542+
resultDataSize = ( int) ( resultAddrNext - scanAddrMin);
543+
} else {
544+
resultDataSize = 0; // no data between
545+
}
546+
547+
return resultDataSize;
548+
}
549+
550+
// Scan for single target data hex from universal hex
551+
//
552+
// return false on failure
553+
public boolean scanForData( final byte [] hex) {
554+
resultData = null;
555+
556+
try {
557+
long dataSize = scanForData( null, hex, hex.length);
558+
if ( dataSize == 0)
559+
return false;
560+
561+
if ( dataSize % 4 != 0) {
562+
// Android DFU library will not proceed if data is not word aligned
563+
// iOS library doesn't seem to mind
564+
dataSize += 4 - dataSize % 4;
565+
}
566+
resultData = new byte[ (int) dataSize];
567+
Arrays.fill( resultData, (byte) 0xFF);
568+
dataSize = scanForData( resultData, hex, hex.length);
569+
if ( dataSize == 0) {
570+
return false;
571+
}
572+
} catch (Exception e) {
573+
return false;
574+
}
575+
return true;
576+
}
577+
578+
// Extract data from application hex
579+
//
580+
// return false on failure
581+
// generated data is in resultData
582+
public boolean applicationHexToData( final byte [] hex, final int hexBlock) {
583+
scanInit();
584+
long [] mnp = hexBlockToAppRegion( hexBlock);
585+
scanAddrMin = mnp[0];
586+
scanAddrNext = mnp[1];
587+
return scanForData( hex);
588+
}
497589
};

0 commit comments

Comments
 (0)