-
Notifications
You must be signed in to change notification settings - Fork 52
/
list_opcodes.py
399 lines (336 loc) · 10.1 KB
/
list_opcodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import sys
import argparse
import textwrap
from pprint import pprint, pformat
from binascii import hexlify
# from datetime import datetime
# from scapy.all import *
from decocare import lib, history
from decocare.history import NotADate
def get_opt_parser( ):
parser = argparse.ArgumentParser( )
parser.add_argument('infile', nargs="+",
default=sys.stdin,
type=argparse.FileType('r'),
help="Find dates in this file.")
parser.add_argument('--out',
default=sys.stdout,
type=argparse.FileType('w'),
help="Write records here.")
return parser
def parse_date(date):
try:
return history.parse_date(date)
except NotADate, e: pass
return None
class Record(object):
_names = {
0x01: 'Bolus',
0x03: 'Prime',
0x06: 'NoDelivery',
0x07: 'ResultTotals',
0x08: 'ChangeBasalProfile',
0x0C: 'ClearAlarm',
0x14: 'SelectBasalProfile',
0x16: 'TempBasal[eof]',
0x17: 'ChangeTime',
0x18: 'NewTimeSet',
0x19: 'LowBattery',
0x1a: 'Battery',
0x1e: 'PumpSuspend',
0x1f: 'PumpResume',
0x0a: 'CalForBG',
0x21: 'Rewind',
0x26: 'EnableDisableRemote',
0x27: 'ChangeRemoteID',
0x33: 'TempBasal',
0x34: 'LowReservoir',
0x5b: 'BolusWizard',
0x5c: 'BolusGiven?',
0x63: 'ChangeUtility?',
0x64: 'ChangeTimeDisplay',
}
_head = {
0x01: 4,
0x03: 5,
0x06: 4,
0x07: 5,
0x28: 7,
0x45: 7,
# observed on bewest-pump
# 0x2e: 24,
# 0x5c: 12,
0x5b: 2,
0x5c: 2,
# 0x0c: 22,
0x6d: 46,
# 0x6d: 46 - 5,
# hacks
}
_date = 5
_body = {
#0x5b: 15,
# 0x5b: 22,
0x5b: 13,
0x45: 3,
#0x06: 7,
0x07: 38 + 3,
0x08: 42,
0x26: 14,
0x33: 1,
0x34: 0,
# 0x6b: 15,
# 0x18: 6,
# 0x21: 23,
#0x6c: 32,
# observed on bewest-pump
# hacks
#0x0a: 0,
#0x6c: 3,
}
def __init__(self, head=bytearray( ), date=bytearray( ), body=bytearray( ) ):
self.head = head
self.date = date
self.opcode = None
if head[:1]:
self.opcode = head[0]
self.datetime = parse_date(date)
self.body = body
@classmethod
def lookup_head(cls, opcode):
return cls._head.get(opcode, 2)
@classmethod
def lookup_date(cls, opcode):
return cls._date
@classmethod
def has_variable_head(cls, opcode):
if opcode == 0x5c:
return True
return False
@classmethod
def variable_read(cls, opcode, body):
if opcode == 0x5c and body[1:]:
#print "XXX: VARIABLE READ: %#04x" % body[1]
return body[1]
return 0
@classmethod
def seeks_null(cls, opcode, body):
return False
if opcode == 0x5c:
return True
if False and opcode == 0x5b:
# print 'XXX: %#04x' % body[13]
if body[13:] and body[13] == 0x5c:
return True
return False
@classmethod
def is_midnight(cls, head):
if head[1:] and head[0] == 0x07:
if head[1:] and head[1] == 0x00:
return True
return False
@classmethod
def lookup_body(cls, opcode):
# print "lookup body for opcode %#04x" % (opcode)
return cls._body.get(opcode, 0)
def __str__(self):
name = self._names.get(self.opcode, self.__class__.__name__)
lengths = 'head[{}], body[{}]'.format(len(self.head), len(self.body))
# opcodes = ' '.join(['%#04x' % x for x in self.head[:1]])
opcodes = '%#04x' % self.opcode
return ' '.join([name, self.date_str( ), lengths, opcodes ])
def date_str(self):
result = 'unknown'
if self.datetime is not None:
result = self.datetime.isoformat( )
else:
if self.is_midnight(self.head):
result = "MIDNIGHT!?: {}".format(history.unmask_date(self.date))
return result
def pformat(self, prefix=''):
head = '\n'.join([ " op hex (%s)" % len(self.head), lib.hexdump(self.head, indent=4),
" decimal", int_dump(self.head, indent=11) ])
date = '\n'.join([ " datetime (%s)" % self.date_str( ),
lib.hexdump(self.date, indent=4) ])
body = " body (%s)" % len(self.body)
if len(self.body) > 0:
body = '\n'.join([ body,
" hex", lib.hexdump(self.body, indent=4),
" decimal", int_dump(self.body, indent=11) ])
extra = [ ]
hour_bits = history.extra_hour_bits(self.date[1])
year_bits = history.extra_year_bits(self.date[4])
day_bits = history.extra_hour_bits(self.date[3])
if 1 in hour_bits:
extra.append("HOUR BITS: {}".format(str(hour_bits)))
if 1 in day_bits:
extra.append("DAY BITS: {}".format(str(day_bits)))
if 1 in year_bits:
extra.append("YEAR BITS: {}".format(str(year_bits)))
extra = ' ' + ' '.join(extra)
return '\n'.join([ prefix, head, date, body, extra ])
def eat_nulls(fd):
nulls = bytearray( )
for B in iter(lambda: bytearray(fd.read(1)), bytearray("")):
if B[0] == 0x00:
nulls.extend(B)
else:
fd.seek(fd.tell( ) - 1)
break
print "found %s nulls" % len(nulls)
return nulls
def seek_null(fd):
bolus = bytearray( )
for B in iter(lambda: fd.read(1), ""):
bolus.append(B)
# print lib.hexdump(bolus)
if B == bytearray([ 0x00 ]):
return bolus
return bolus
def find_dates(stream):
records = [ ]
errors = [ ]
bolus = bytearray( )
extra = bytearray( )
opcode = ''
for B in iter(lambda: bytearray(stream.read(2)), bytearray("")):
opcode = B[0]
#bolus.append(B)
bolus.extend(B)
head_length = Record.lookup_head(opcode)
body_length = Record.lookup_body(opcode)
date_length = Record.lookup_date(opcode)
total = len(bolus)
if total < head_length:
bolus.extend(bytearray(stream.read(head_length-total)))
variable_read = Record.variable_read(opcode, bolus)
if variable_read > 2:
#print "super special"
bolus.extend(bytearray(stream.read(variable_read)))
#print lib.hexdump( bolus )
opcode = bolus[variable_read]
#print "NEW OPCODE %#04x" % opcode
head_length = Record.lookup_head(opcode)
body_length = Record.lookup_body(opcode)
total = len(bolus) - variable_read
if total < head_length:
bolus.extend( stream.read(head_length-total) )
total = len(bolus)
#body_length = Record.lookup_body(opcode)
#date_length = Record.lookup_date(opcode)
#total = len(bolus)
total = len(bolus)
head_length = total
head = bolus[:max(total, 1)]
bolus.extend(bytearray(stream.read(date_length)))
date = bolus[head_length:head_length+date_length]
total = len(bolus)
# print repr(bolus), date_length, repr(date)
if len(date) < 5:
print "DATE LESS THAN 5!", stream.tell( )
print lib.hexdump(bolus)
break
records[-1].body.extend(bolus)
datetime = parse_date(date)
if bytearray( [0x00] * max(total-2, 5) ) in bolus:
nulls = bytearray(eat_nulls(stream))
pos = stream.tell( )
if pos > 1021:
bolus = bolus + nulls + bytearray(stream.read(-1))
crc = bolus[-2:]
nulls = bolus[:-2]
print "EOF {} nulls, CRC:".format(len(nulls))
print lib.hexdump(crc)
else:
print total, ' ', max(total-2, 5)
print lib.hexdump( [0x00] * min(total, 5) )
print
print "TOO MANY NULLS, BAILING ON STREAM at %s " % stream.tell( )
print "bolus"
print lib.hexdump(bolus)
print "nulls"
print lib.hexdump(nulls)
print "MISSING: ARE THERE 32 more bytes?"
print lib.hexdump(bytearray(stream.read(32)))
# records[-1].body.extend(nulls)
break
if not Record.is_midnight(head):
if datetime is None:
print ("#### MISSING DATETIME @ %s," % stream.tell( )),
print "reading more to debug %#04x" % opcode
print lib.hexdump(bolus, indent=4)
print int_dump(bolus, indent=11)
extra = bytearray(stream.read(32))
print "##### DEBUG HEX"
print lib.hexdump(extra, indent=4)
print "##### DEBUG DECIMAL"
print int_dump(extra, indent=11)
if history.parse_date(bolus):
print "XXX:???:XXX", history.parse_date(bolus).isoformat( )
break
if datetime is not None or Record.is_midnight(head):
body = bytearray(stream.read(body_length))
bolus.extend(body)
if False or Record.seeks_null(opcode, body):
print "should eat up to null, second %s" % repr(body[1:])
if body[1:]:
if body[-1] != 0x00:
extra = seek_null(stream)
print "found %s extra" % len(extra)
body.extend(extra)
bolus.extend(extra)
epi = bytearray(stream.read(date_length))
finished = parse_date(epi)
record = Record(head, date, body)
prefix = "#### RECORD %s %s" % (len(records), str(record) )
print record.pformat(prefix)
print ""
records.append(record)
bolus = bytearray( )
opcode = ''
return records
def int_dump(stream, indent=0):
"""
>>> int_dump(bytearray([0x01, 0x02]))
' 1 2'
"""
cells = [ '%#04s' % (x) for x in stream ]
lines = [ ]
indent = ''.join( [ ' ' ] * indent )
while cells:
octet = cells[:8]
line = ' '.join(octet)
lines.append(indent + line)
cells = cells[8:]
out = ('\n').join([ line for line in lines ])
return out
def main( ):
parser = get_opt_parser( )
opts = parser.parse_args( )
tw_opts = {
'width': 50,
'subsequent_indent': ' ',
'initial_indent': ' ',
}
wrapper = textwrap.TextWrapper(**tw_opts)
for stream in opts.infile:
print "## START %s" % (stream.name)
records = find_dates(stream)
i = 0
for record in records:
prefix = '#### RECORD {} {}'.format(i, str(record))
# record.pformat(prefix)
i += 1
print "`end %s: %s records`" % (stream.name, len(records))
stream.close( )
if __name__ == '__main__':
import doctest
failures, tests = doctest.testmod( )
if failures > 0:
print "REFUSING TO RUN DUE TO FAILED TESTS"
sys.exit(1)
main( )
#####
# EOF