24
24
import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
25
25
26
26
import java .io .IOException ;
27
- import java .lang .reflect .Method ;
28
27
import java .util .Arrays ;
29
28
import java .util .UUID ;
30
29
@@ -41,6 +40,7 @@ public abstract class PartialFlashingBaseService extends IntentService {
41
40
public static final UUID PARTIAL_FLASH_CHARACTERISTIC = UUID .fromString ("e97d3b10-251d-470a-a062-fa1922dfa9a8" );
42
41
public static final UUID PARTIAL_FLASHING_SERVICE = UUID .fromString ("e97dd91d-251d-470a-a062-fa1922dfa9a8" );
43
42
public static final String PXT_MAGIC = "708E3B92C615A841C49866C975EE5197" ;
43
+ public static final String UPY_MAGIC = ".*FE307F59.{16}9DD7B1C1.*" ;
44
44
45
45
private static final UUID MICROBIT_DFU_SERVICE = UUID .fromString ("e95d93b0-251d-470a-a062-fa1922dfa9a8" );
46
46
private static final UUID MICROBIT_SECURE_DFU_SERVICE = UUID .fromString ("0000fe59-0000-1000-8000-00805f9b34fb" );
@@ -89,6 +89,7 @@ public abstract class PartialFlashingBaseService extends IntentService {
89
89
private static final int REGION_MAKECODE = 2 ;
90
90
91
91
// DAL Hash
92
+ boolean python = false ;
92
93
String dalHash ;
93
94
94
95
// Partial Flashing Commands
@@ -193,17 +194,17 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
193
194
Log .w (TAG , "onServicesDiscovered SUCCESS" );
194
195
195
196
Log .v (TAG , String .valueOf (gatt .getServices ()));
196
- mConnectionState = STATE_CONNECTED_AND_READY ;
197
197
} else {
198
198
Log .w (TAG , "onServicesDiscovered received: " + status );
199
199
mConnectionState = STATE_ERROR ;
200
200
}
201
201
202
202
// Clear locks
203
+ mConnectionState = STATE_CONNECTED_AND_READY ;
203
204
synchronized (lock ) {
204
205
lock .notifyAll ();
205
206
}
206
- Log .v (TAG , "Cleared locks" );
207
+ Log .v (TAG , "onServicesDiscovered :: Cleared locks" );
207
208
}
208
209
209
210
@ Override
@@ -254,7 +255,10 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
254
255
Log .v (TAG , "Hash: " + bytesToHex (hash ));
255
256
256
257
// If Region is DAL get HASH
257
- if (notificationValue [1 ] == REGION_DAL )
258
+ if (notificationValue [1 ] == REGION_DAL && python == false )
259
+ dalHash = bytesToHex (hash );
260
+
261
+ if (notificationValue [1 ] == REGION_DAL && python == true )
258
262
dalHash = bytesToHex (hash );
259
263
260
264
synchronized (region_lock ) {
@@ -276,12 +280,14 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
276
280
public void onDescriptorWrite (BluetoothGatt gatt ,
277
281
BluetoothGattDescriptor descriptor ,
278
282
int status ){
283
+ Log .v (TAG , "onDescriptorWrite :: " + status );
284
+
279
285
if (status == BluetoothGatt .GATT_SUCCESS ) {
280
286
Log .v (TAG , "Descriptor success" );
281
287
Log .v (TAG , "GATT: " + gatt .toString () + ", Desc: " + descriptor .toString () + ", Status: " + status );
282
288
descriptorWriteSuccess = true ;
283
289
} else {
284
- Log .v (TAG , "Descriptor write failed" );
290
+ Log .v (TAG , "onDescriptorWrite: " + status );
285
291
}
286
292
287
293
synchronized (lock ) {
@@ -356,6 +362,12 @@ public int attemptPartialFlash(String filePath) {
356
362
HexUtils hex = new HexUtils (filePath );
357
363
Log .v (TAG , "searchForData()" );
358
364
int magicIndex = hex .searchForData (PXT_MAGIC );
365
+
366
+ if (magicIndex == -1 ) {
367
+ magicIndex = hex .searchForDataRegEx (UPY_MAGIC );
368
+ python = true ;
369
+ }
370
+
359
371
Log .v (TAG , "/searchForData() = " + magicIndex );
360
372
if (magicIndex > -1 ) {
361
373
@@ -370,14 +382,31 @@ public int attemptPartialFlash(String filePath) {
370
382
}
371
383
372
384
// Find DAL hash
385
+ if (python ) magicIndex = magicIndex - 3 ;
386
+
373
387
int record_length = hex .getRecordDataLengthFromIndex (magicIndex );
374
388
Log .v (TAG , "Length of record: " + record_length );
375
389
376
390
int magic_offset = (record_length == 64 ) ? 32 : 0 ;
377
391
String hashes = hex .getDataFromIndex (magicIndex + ((record_length == 64 ) ? 0 : 1 )); // Size of rows
378
392
int chunks_per_line = magic_offset / 16 ;
379
393
380
- if (!hashes .substring (magic_offset , magic_offset + 16 ).equals (dalHash )) {
394
+ Log .v (TAG , hashes );
395
+
396
+ if (hashes .charAt (3 ) == '2' ) {
397
+ // Uses a hash pointer. Create regex and extract from hex
398
+ String regEx = ".*" +
399
+ dalHash +
400
+ ".*" ;
401
+ int hashIndex = hex .searchForDataRegEx (regEx );
402
+
403
+ // TODO Uses CRC of hash
404
+ if (hashIndex == -1 ) {
405
+ // return PF_ATTEMPT_DFU;
406
+ }
407
+ // hashes = hex.getDataFromIndex(hashIndex);
408
+ // Log.v(TAG, "hash: " + hashes);
409
+ } else if (!hashes .substring (magic_offset , magic_offset + 16 ).equals (dalHash )) {
381
410
Log .v (TAG , hashes .substring (magic_offset , magic_offset + 16 ) + " " + (dalHash ));
382
411
return PF_ATTEMPT_DFU ;
383
412
}
@@ -582,7 +611,7 @@ protected void onHandleIntent(@Nullable Intent intent) {
582
611
// Check partial flashing service exists
583
612
if (pfService == null ) {
584
613
Log .v (TAG , "Partial Flashing Service == null" );
585
- final Intent broadcast = new Intent (BROADCAST_PF_FAILED );
614
+ final Intent broadcast = new Intent (BROADCAST_PF_ATTEMPT_DFU );
586
615
LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
587
616
return ;
588
617
}
@@ -591,8 +620,9 @@ protected void onHandleIntent(@Nullable Intent intent) {
591
620
partialFlashCharacteristic = pfService .getCharacteristic (PARTIAL_FLASH_CHARACTERISTIC );
592
621
593
622
if (partialFlashCharacteristic == null ) {
594
- final Intent broadcast = new Intent (BROADCAST_PF_FAILED );
623
+ final Intent broadcast = new Intent (BROADCAST_PF_ATTEMPT_DFU );
595
624
LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
625
+ return ;
596
626
}
597
627
598
628
// Set up notifications
@@ -603,21 +633,17 @@ protected void onHandleIntent(@Nullable Intent intent) {
603
633
BluetoothGattDescriptor descriptor = partialFlashCharacteristic .getDescriptor (CLIENT_CHARACTERISTIC_CONFIG );
604
634
descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
605
635
res = mBluetoothGatt .writeDescriptor (descriptor );
636
+ Log .v (TAG ,"writeDescriptor: " + res );
606
637
607
638
// We have to wait until device receives a response or an error occur
608
639
try {
609
640
synchronized (lock ) {
610
- lock .wait (2000 );
611
- Log .v (TAG , String . valueOf ( descriptor .getValue () ));
641
+ lock .wait ();
642
+ Log .v (TAG , "Descriptor value: " + descriptor .getValue ());
612
643
}
613
644
} catch (final InterruptedException e ) {
614
645
}
615
646
616
- if (!descriptorWriteSuccess ) {
617
- final Intent broadcast = new Intent (BROADCAST_PF_FAILED );
618
- LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
619
- }
620
-
621
647
/*
622
648
if(false) {
623
649
// Partial flashing started but failed. Need to PF or USB flash to fix
@@ -653,7 +679,7 @@ protected void onHandleIntent(@Nullable Intent intent) {
653
679
LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
654
680
return ;
655
681
}
656
- ;
682
+
657
683
658
684
microbitDFUCharacteristic .setValue (payload );
659
685
boolean status = mBluetoothGatt .writeCharacteristic (microbitDFUCharacteristic );
@@ -668,17 +694,6 @@ protected void onHandleIntent(@Nullable Intent intent) {
668
694
}
669
695
}
670
696
671
- // Refresh services
672
- try {
673
- // BluetoothGatt gatt
674
- final Method refresh = mBluetoothGatt .getClass ().getMethod ("refresh" );
675
- if (refresh != null ) {
676
- refresh .invoke (mBluetoothGatt );
677
- }
678
- } catch (Exception e ) {
679
- // Log it
680
- }
681
-
682
697
mBluetoothGatt .disconnect ();
683
698
684
699
Log .v (TAG , "Send Intent: BROADCAST_PF_ATTEMPT_DFU" );
0 commit comments