Skip to content

Commit 5a213f2

Browse files
committed
refactor + notifications
1 parent 58524cd commit 5a213f2

File tree

2 files changed

+44
-147
lines changed

2 files changed

+44
-147
lines changed

src/main/java/org/microbit/android/partialflashing/HexUtils.java

Lines changed: 33 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
public class HexUtils {
2828
private final static String TAG = HexUtils.class.getSimpleName();
2929

30-
private final static String PXT_MAGIC = "708E3B92C615A841C49866C975EE5197";
31-
3230
private final static int INIT = 0;
3331
private final static int INVALID_FILE = 1;
3432
private final static int NO_PARTIAL_FLASH = 2;
@@ -38,18 +36,6 @@ public class HexUtils {
3836
BufferedReader reader = null;
3937
List<String> hexLines = new ArrayList<String>();
4038

41-
int BUFFER_LIMIT = 10000;
42-
43-
String templateHash;
44-
String programHash;
45-
int sectionAddress;
46-
int magicAddress;
47-
48-
int currentRecordType = 0;
49-
int currentRecordOffset = 0;
50-
51-
int magicLines = 0;
52-
5339
public HexUtils(String filePath){
5440
// Hex Utils initialization
5541
// Open File
@@ -87,10 +73,20 @@ public Boolean openHexFile(String filePath) throws IOException {
8773
return true;
8874
}
8975

76+
/*
77+
* A function to find the length of the hex file
78+
* @param none
79+
* @ return the size (# of lines) in the hex file
80+
*/
9081
public int numOfLines() {
9182
return hexLines.size();
9283
}
93-
84+
85+
/*
86+
* A function to search for data in a hex file
87+
* @param the _string_ of data to search for
88+
* @return the index of the data. -1 if not found.
89+
*/
9490
public int searchForData(String search) throws IOException {
9591
// Iterate through
9692
ListIterator i = hexLines.listIterator();
@@ -105,19 +101,40 @@ public int searchForData(String search) throws IOException {
105101
// Return -1 if no match
106102
return -1;
107103
}
108-
104+
105+
/*
106+
* Returns data from an index
107+
* @param index
108+
* @return data as string
109+
*/
109110
public String getDataFromIndex(int index) throws IOException {
110111
return getRecordData(hexLines.get(index));
111112
}
112113

114+
/*
115+
* Returns record type from an index
116+
* @param index
117+
* @return type as int
118+
*/
113119
public int getRecordTypeFromIndex(int index) throws IOException {
114120
return getRecordType(hexLines.get(index));
115121
}
116122

123+
/*
124+
* Returns record address from an index
125+
* Note: does not include segment address
126+
* @param index
127+
* @return address as int
128+
*/
117129
public int getRecordAddressFromIndex(int index) throws IOException {
118130
return getRecordAddress(hexLines.get(index));
119131
}
120132

133+
/*
134+
* Returns segment address from an index
135+
* @param index
136+
* @return address as int
137+
*/
121138
public int getSegmentAddress(int index) throws IOException {
122139
// Look backwards to find current segment address
123140
int segmentAddress = -1;
@@ -132,46 +149,6 @@ public int getSegmentAddress(int index) throws IOException {
132149
return Integer.parseInt(getRecordData(hexLines.get(cur)), 16);
133150
}
134151

135-
public Boolean findMagic(String magic) throws IOException {
136-
String record;
137-
138-
try {
139-
while ((record = reader.readLine()) != null) {
140-
// Inc magic lines
141-
magicLines++;
142-
143-
// Record Type
144-
switch(getRecordType(record)) {
145-
case 0: // Data
146-
// Once the magic happens..
147-
if(getRecordData(record).equals(magic)){
148-
// Store Magic Address and Break
149-
magicAddress = sectionAddress + getRecordAddress(record);
150-
Log.v(TAG, "Magic Found!");
151-
return true;
152-
}
153-
break;
154-
case 1: // If record type is EOF break the loop
155-
return false;
156-
case 4:
157-
// Recent Section
158-
sectionAddress = Integer.parseInt(getRecordData(record), 16);
159-
break;
160-
}
161-
162-
// Set mark to Magic record -1
163-
reader.mark(BUFFER_LIMIT);
164-
165-
}
166-
} catch (Exception e){
167-
Log.e(TAG, "Find Magic " + e.toString());
168-
}
169-
170-
// If magic is never found and there is no EOF file marker
171-
// Should never return here
172-
return false;
173-
}
174-
175152
/*
176153
Used to get the data address from a record
177154
@param Record as a String
@@ -208,14 +185,6 @@ private int getRecordType(String record){
208185
}
209186
}
210187

211-
/*
212-
Used to get the record type from the current record if it exists
213-
@return Record type as a decimal
214-
*/
215-
public int getRecordType(){
216-
return currentRecordType;
217-
}
218-
219188
/*
220189
Used to get the data from a record
221190
@param Record as a String
@@ -231,68 +200,6 @@ private String getRecordData(String record){
231200
}
232201
}
233202

234-
/*
235-
Used to return the data from the next record
236-
*/
237-
public String getNextData() throws IOException {
238-
String data = reader.readLine();
239-
currentRecordType = getRecordType(data);
240-
currentRecordOffset = getRecordAddress(data);
241-
return getRecordData(data);
242-
}
243-
244-
// Specific Functions Used For Partial Flashing Below
245-
/*
246-
Returns the template hash
247-
@return templateHash
248-
*/
249-
public String getTemplateHash(){
250-
return templateHash;
251-
}
252-
253-
/*
254-
Returns the program hash
255-
@return programHash
256-
*/
257-
public String getProgramHash(){
258-
return programHash;
259-
}
260-
261-
/*
262-
Find HEX Meta Data
263-
*/
264-
public Boolean findHexMetaData(String filePath) throws IOException {
265-
return false;
266-
}
267-
268-
/*
269-
Find start address & start of PXT data
270-
*/
271-
public Integer getSectionAddress(){
272-
return sectionAddress;
273-
}
274-
275-
/*
276-
Get offset of current record
277-
*/
278-
public int getRecordOffset(){
279-
return currentRecordOffset;
280-
}
281-
282-
/*
283-
Set mark to beginning of page
284-
*/
285-
public void setMark() throws IOException {
286-
reader.mark(BUFFER_LIMIT);
287-
}
288-
289-
/*
290-
Rewind to start of page
291-
*/
292-
public void rewind() throws IOException {
293-
reader.reset();
294-
}
295-
296203
/*
297204
Number of lines / packets in file
298205
*/
@@ -304,12 +211,5 @@ public int numOfLines(String filePath) throws IOException {
304211
return lines;
305212
}
306213

307-
/*
308-
Lines / packets before magic
309-
*/
310-
public int getMagicLines()
311-
{
312-
return magicLines;
313-
}
314214
}
315215

src/main/java/org/microbit/android/partialflashing/PartialFlashingBaseService.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public abstract class PartialFlashingBaseService extends IntentService {
6262
private static final byte PACKET_STATE_RETRANSMIT = (byte)0xAA;
6363
private byte packetState = PACKET_STATE_WAITING;
6464

65+
// Used to lock the program state while we wait for a Bluetooth operation to complete
6566
private static final boolean BLE_WAITING = false;
6667
private static final boolean BLE_READY = true;
6768
private boolean bluetoothStatus = BLE_WAITING;
@@ -102,10 +103,13 @@ public abstract class PartialFlashingBaseService extends IntentService {
102103
@Override
103104
public void onCreate() {
104105
super.onCreate();
106+
107+
// Create intent filter and add to Local Broadcast Manager so that we can use an Intent to
108+
// start the Partial Flashing Service
105109

106110
final IntentFilter intentFilter = new IntentFilter();
107111
intentFilter.addAction(PartialFlashingBaseService.BROADCAST_ACTION);
108-
112+
109113
final LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
110114
manager.registerReceiver(broadcastReceiver, intentFilter);
111115
}
@@ -237,14 +241,6 @@ private void sendProgressBroadcast(final int progress) {
237241
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
238242
}
239243

240-
// private void sendErrorBroadcast(final int error) {
241-
//
242-
// final Intent broadcast = new Intent(BROADCAST_ERROR);
243-
//
244-
// broadcast.putExtra(EXTRA_DEVICE_ADDRESS, mDeviceAddress);
245-
// LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
246-
// }
247-
248244
// Write to BLE Flash Characteristic
249245
public Boolean writePartialFlash(byte data[]){
250246

@@ -265,13 +261,13 @@ public Boolean attemptPartialFlash(String filePath) {
265261
int numOfLines = 0;
266262
try {
267263

268-
Log.v(TAG, "starting pf");
264+
Log.v(TAG, "attemptPartialFlash()");
269265

270266
HexUtils hex = new HexUtils(filePath);
271267
int magicIndex = hex.searchForData(PXT_MAGIC);
272268
if (magicIndex > -1) {
273269

274-
Log.v(TAG, "found meta");
270+
Log.v(TAG, "Found PXT_MAGIC");
275271

276272
numOfLines = hex.numOfLines() - magicIndex;
277273
Log.v(TAG, "Total lines: " + numOfLines);
@@ -329,7 +325,6 @@ public Boolean attemptPartialFlash(String filePath) {
329325
if(count == 4){
330326
count = 0;
331327
// Wait for notification
332-
int waitTime = 0;
333328
while(packetState == PACKET_STATE_WAITING);
334329

335330
// Reset to waiting state
@@ -344,15 +339,17 @@ public Boolean attemptPartialFlash(String filePath) {
344339
if(packetState == PACKET_STATE_RETRANSMIT) {
345340
lineCount = lineCount - 4;
346341
} else {
347-
// send progress update
342+
// Send progress update
348343
Log.v(TAG, "LC: " + lineCount + ", NL: " + numOfLines);
349344
int percent = Math.round((float)100 * ((float)(lineCount) / (float)(numOfLines)));
350345
sendProgressBroadcast(percent);
346+
347+
// Next line
348+
lineCount = lineCount + 1;
351349
}
352350

353351
// Increment packet #
354352
packetNum = packetNum + 1;
355-
lineCount = lineCount + 1;
356353

357354
}
358355

0 commit comments

Comments
 (0)