@@ -40,8 +40,12 @@ public abstract class PartialFlashingBaseService extends IntentService {
40
40
41
41
public static final UUID PARTIAL_FLASH_CHARACTERISTIC = UUID .fromString ("e97d3b10-251d-470a-a062-fa1922dfa9a8" );
42
42
public static final UUID PARTIAL_FLASHING_SERVICE = UUID .fromString ("e97dd91d-251d-470a-a062-fa1922dfa9a8" );
43
+
44
+ public static final String PXT_MAGIC = "708E3B92C615A841C49866C975EE5197" ;
43
45
44
46
private final static String TAG = PartialFlashingBaseService .class .getSimpleName ();
47
+
48
+ public static final String BROADCAST_ACTION = "org.microbit.android.partialflashing.broadcast.BROADCAST_ACTION" ;
45
49
46
50
private BluetoothManager mBluetoothManager ;
47
51
private BluetoothAdapter mBluetoothAdapter ;
@@ -94,7 +98,13 @@ public abstract class PartialFlashingBaseService extends IntentService {
94
98
"com.example.bluetooth.le.ACTION_DATA_AVAILABLE" ;
95
99
public final static String EXTRA_DATA =
96
100
"com.example.bluetooth.le.EXTRA_DATA" ;
97
- LocalBroadcastManager localBroadcast ;
101
+
102
+ @ Override
103
+ public void onCreate () {
104
+ super .onCreate ();
105
+
106
+ final LocalBroadcastManager manager = LocalBroadcastManager .getInstance (this );
107
+ }
98
108
99
109
// Various callback methods defined by the BLE API.
100
110
private final BluetoothGattCallback mGattCallback =
@@ -228,32 +238,33 @@ public Boolean attemptPartialFlash(String filePath) {
228
238
int count = 0 ;
229
239
int progressBar = 0 ;
230
240
int numOfLines = 0 ;
231
- HexUtils hex ;
232
241
try {
233
- hex = new HexUtils ();
234
- if (hex .findHexMetaData (filePath )) {
235
242
236
- if (!hex .getTemplateHash ().equals (dalHash )){
237
- Log .v (TAG , "Received DAL hash:" + dalHash + " Template: " + hex .getTemplateHash ());
238
- }
243
+ Log .v (TAG , "starting pf" );
239
244
240
- numOfLines = hex .numOfLines (filePath );
241
- numOfLines = numOfLines - hex .getMagicLines ();
245
+ HexUtils hex = new HexUtils (filePath );
246
+ int magicIndex = hex .searchForData (PXT_MAGIC );
247
+ if (magicIndex > -1 ) {
248
+
249
+ Log .v (TAG , "found meta" );
250
+
251
+ numOfLines = hex .numOfLines () - magicIndex ;
242
252
Log .v (TAG , "Total lines: " + numOfLines );
243
253
244
254
// Ready to flash!
245
255
// Loop through data
246
256
String hexData ;
247
257
int packetNum = 0 ;
258
+ int lineCount = 0 ;
248
259
while (true ){
249
260
// Get next data to write
250
- hexData = hex .getNextData ( );
261
+ hexData = hex .getDataFromIndex ( magicIndex + lineCount );
251
262
// Check if EOF
252
- if (hex .getRecordType ( ) != 0 ) break ;
263
+ if (hex .getRecordTypeFromIndex ( magicIndex + lineCount ) != 0 ) break ;
253
264
254
265
// Log record being written
255
266
Log .v (TAG , "Hex Data : " + hexData );
256
- Log .v (TAG , "Hex Offset: " + Integer .toHexString (hex .getRecordOffset ( )));
267
+ Log .v (TAG , "Hex Offset: " + Integer .toHexString (hex .getRecordAddressFromIndex ( magicIndex + lineCount )));
257
268
258
269
// If Hex Data is Embedded Source Magic
259
270
if (hexData .length () == 32 ) {
@@ -273,11 +284,11 @@ public Boolean attemptPartialFlash(String filePath) {
273
284
// Split into bytes
274
285
int offsetToSend = 0 ;
275
286
if (count == 0 ) {
276
- offsetToSend = hex .getRecordOffset ( );
287
+ offsetToSend = hex .getRecordAddressFromIndex ( magicIndex + lineCount );
277
288
}
278
289
279
290
if (count == 1 ) {
280
- offsetToSend = hex .getSectionAddress ( );
291
+ offsetToSend = hex .getSegmentAddress ( magicIndex + lineCount );
281
292
}
282
293
283
294
Log .v (TAG , "OFFSET_TO_SEND: " + offsetToSend );
@@ -306,14 +317,14 @@ public Boolean attemptPartialFlash(String filePath) {
306
317
// If notification is retransmit -> retransmit last block.
307
318
// Else set start of new block
308
319
if (packetState == PACKET_STATE_RETRANSMIT ) {
309
- hex . rewind () ;
320
+ lineCount = lineCount - 4 ;
310
321
} else {
311
- hex .setMark ();
312
322
// TODO update UI
313
323
}
314
324
315
325
// Increment packet #
316
326
packetNum = packetNum + 1 ;
327
+ lineCount = lineCount + 1 ;
317
328
318
329
}
319
330
@@ -415,10 +426,8 @@ private boolean initialize(String deviceId) {
415
426
BluetoothGattDescriptor descriptor = partialFlashCharacteristic .getDescriptor (CLIENT_CHARACTERISTIC_CONFIG );
416
427
descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
417
428
418
- if (bluetoothStatus ) {
419
- bluetoothStatus = BLE_WAITING ;
420
- mBluetoothGatt .writeDescriptor (descriptor );
421
- }
429
+ bluetoothStatus = BLE_WAITING ;
430
+ mBluetoothGatt .writeDescriptor (descriptor );
422
431
while (!bluetoothStatus );
423
432
424
433
return true ;
@@ -431,7 +440,9 @@ protected void onHandleIntent(@Nullable Intent intent) {
431
440
final String filePath = intent .getStringExtra ("filepath" );
432
441
final String deviceAddress = intent .getStringExtra ("deviceAddress" );
433
442
443
+ Log .v (TAG , "Initialise" );
434
444
initialize (deviceAddress );
445
+ Log .v (TAG , "/Initialise" );
435
446
readMemoryMap ();
436
447
// If fails attempt full flash
437
448
if (!attemptPartialFlash (filePath ))
0 commit comments