27
27
public class HexUtils {
28
28
private final static String TAG = HexUtils .class .getSimpleName ();
29
29
30
- private final static String PXT_MAGIC = "708E3B92C615A841C49866C975EE5197" ;
31
-
32
30
private final static int INIT = 0 ;
33
31
private final static int INVALID_FILE = 1 ;
34
32
private final static int NO_PARTIAL_FLASH = 2 ;
@@ -38,18 +36,6 @@ public class HexUtils {
38
36
BufferedReader reader = null ;
39
37
List <String > hexLines = new ArrayList <String >();
40
38
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
-
53
39
public HexUtils (String filePath ){
54
40
// Hex Utils initialization
55
41
// Open File
@@ -87,10 +73,20 @@ public Boolean openHexFile(String filePath) throws IOException {
87
73
return true ;
88
74
}
89
75
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
+ */
90
81
public int numOfLines () {
91
82
return hexLines .size ();
92
83
}
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
+ */
94
90
public int searchForData (String search ) throws IOException {
95
91
// Iterate through
96
92
ListIterator i = hexLines .listIterator ();
@@ -105,19 +101,40 @@ public int searchForData(String search) throws IOException {
105
101
// Return -1 if no match
106
102
return -1 ;
107
103
}
108
-
104
+
105
+ /*
106
+ * Returns data from an index
107
+ * @param index
108
+ * @return data as string
109
+ */
109
110
public String getDataFromIndex (int index ) throws IOException {
110
111
return getRecordData (hexLines .get (index ));
111
112
}
112
113
114
+ /*
115
+ * Returns record type from an index
116
+ * @param index
117
+ * @return type as int
118
+ */
113
119
public int getRecordTypeFromIndex (int index ) throws IOException {
114
120
return getRecordType (hexLines .get (index ));
115
121
}
116
122
123
+ /*
124
+ * Returns record address from an index
125
+ * Note: does not include segment address
126
+ * @param index
127
+ * @return address as int
128
+ */
117
129
public int getRecordAddressFromIndex (int index ) throws IOException {
118
130
return getRecordAddress (hexLines .get (index ));
119
131
}
120
132
133
+ /*
134
+ * Returns segment address from an index
135
+ * @param index
136
+ * @return address as int
137
+ */
121
138
public int getSegmentAddress (int index ) throws IOException {
122
139
// Look backwards to find current segment address
123
140
int segmentAddress = -1 ;
@@ -132,46 +149,6 @@ public int getSegmentAddress(int index) throws IOException {
132
149
return Integer .parseInt (getRecordData (hexLines .get (cur )), 16 );
133
150
}
134
151
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
-
175
152
/*
176
153
Used to get the data address from a record
177
154
@param Record as a String
@@ -208,14 +185,6 @@ private int getRecordType(String record){
208
185
}
209
186
}
210
187
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
-
219
188
/*
220
189
Used to get the data from a record
221
190
@param Record as a String
@@ -231,68 +200,6 @@ private String getRecordData(String record){
231
200
}
232
201
}
233
202
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
-
296
203
/*
297
204
Number of lines / packets in file
298
205
*/
@@ -304,12 +211,5 @@ public int numOfLines(String filePath) throws IOException {
304
211
return lines ;
305
212
}
306
213
307
- /*
308
- Lines / packets before magic
309
- */
310
- public int getMagicLines ()
311
- {
312
- return magicLines ;
313
- }
314
214
}
315
215
0 commit comments