-
Notifications
You must be signed in to change notification settings - Fork 12
/
pseudocode.py
382 lines (321 loc) · 8.33 KB
/
pseudocode.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
#!/usr/bin/python
import user
import struct
import sys
import serial
import time
import logging
from pprint import pprint, pformat
from insulaudit.core import Command
from insulaudit.clmm.usbstick import *
from insulaudit import lib
logging.basicConfig( stream=sys.stdout )
log = logging.getLogger( 'auditor' )
log.setLevel( logging.FATAL )
log.info( 'hello world' )
io = logging.getLogger( 'auditor.io' )
io.setLevel( logging.DEBUG )
"""
#####################
#
# Command Stuff
# (pseudocode analysis of MM512.java)
The Pump Packet looks like this:
7 bytes with parameters on the end
00 167
01 serial[ 0 ]
02 serial[ 1 ]
03 serial[ 2 ]
04 commandCode
05 sequenceNumber or paramCount
06 [ parameters ]
06/07 CRC8(packet)
or:
167, serial, code, seq/param, params, CRC8(packet)
NB the whole thing is wrapped by encodeDC()
get ACK packet: is:
packet = [ 167 ] + serial + [ 6, 0 ]
packet.append(CRC8(packet))
buildPacket:
# 7 bytes + params
packet = [ ]
head = [ 167, ] + serial
body = [ self.code ]
tail = [ 0 ]
if paramCount > 0:
if sequenceNumber:
tail = [ sequenceNumber ]
else:
tail = [ paramCount ]
tail.append( commandParams )
packet = head + body + tail
packet.append(CRC8(packet))
return encodeDC(packet)
makeCommandPacket:
# returns a new "Command"
# which special cases 93
command = Command(self.code, 0, 0, 0)
if code == 93 and commandParams[0] == 1:
#
command.setUseMultiXmitMode(true)
return command
makeDataPacket(packetNumber, sequenceNumber, paramCount):
command = Command(self.code, 0, 0, self.commandType)
command.paramCount = paramCount
command.sequenceNumber = sequenceNumber
command.params = self.params[packetOffset:packetOffset+packetSize]
return command
"""
"""
#####################
#
# Pump Stuff
# (pseudocode analysis of MM512.java)
sendAck:
packet = [ 167 ] + serial + [ 6, 0 ]
packet.append(CRC8(packet))
packet = encodeDC(packet)
com_command = [ 5, packet.length ]
serial.write(com_command + packet)
usb.readAckByte()
# turn off RF
usb.readReadyByte(false)
checkAck:
bytesAvail = usb.readStatus( )
usb.sendTransferDataCommand( ) # exec read data flow on usb
response = serial.read( )
ack = decodeDC(response)
# can check header and CRC of response
# should match the previously sent command
ackBytes[4] == 6 # command ACK ok
error = lookup_error(ackBytes[5])
initDevice:
cmdPowerControl.execute()
readPumpModelNumber.execute()
cmdReadError.execute()
cmdReadState.execute()
cmdReadTmpBasal.execute()
initDeviceAfterModelNumberKnown()
bolus = detectActiveBolus()
if !bolus:
cmdSetSuspend.execute()
detectActiveBolus:
cmdDetectBolus.execute()
# make sure it worked
shutdownPump
cmdCancelSuspend.execute()
execute:
# retry 3 times
# if not ready, do usb.initCommunications
executeIO()
executeIO:
if paramCount > 0:
packet = makeCommandPacket()
packet.executeIO()
if paramCount > 128:
data = makeDataPacket(1, 1, 64)
data.sendAndRead()
data = makeDataPacket(2, 2, 64)
data.sendAndRead()
data = makeDataPacket(3, 131, 16)
data.sendAndRead()
elsif paramCount > 64:
data = makeDataPacket(1, 1, 64)
data.sendAndRead()
data = makeDataPacket(2, 130, 32)
data.sendAndRead()
else:
# special case commandCode == 64
sendAndRead()
sendAndRead:
sendCommand()
if expectedLength > 0 and !isHaltRequested():
if pages
data = readDeviceDataPage(length)
else:
data = readDeviceData()
bytesRead += data.length
else
if commandParams.length > 0:
# setState(7)
bytesRead += commandParams.length
checkAck()
sendCommand
packet = buildPacket()
usbCmd = 0
# only when cmd ==93 (powerCTRL)
if isUseMultiXmitMode():
usbCmd = 10
elsif paramCount == 0:
usbCmd = 5
else:
# turn on RFMode, new session
usbCmd = 4
usbCmd = [ usbCmd, packet.length ]
command = usbCmd + packet
serial.write(command)
usb.readAckByte()
# special case command 93 and paramCount == 0
# needs to be sensitive to timeout
usb.readReadyByte(usbCmd[0] == 4)
checkHeaderAndCRC(deviceData):
# check CRC8
# check serial number
readDeviceDataPage(expectedBytes):
# collect multiple pages of data for commands with longer reads.
done = False
pages = [ ]
while not done:
# get a page
data = readDeviceData()
# if no more data we're done
if data.length == 0
done = true
else
# add data to pages
pages.append(data)
done = pages.length >= expectedBytes || isHaltRequested()
# sendAck to acknowledge receipt of this page
if not done and isHaltRequested():
sendAck()
return pages
readDeviceData:
bytesAvail = usb.readStatus( )
usb.sendTransferDataCommand( ) # exec read data flow on usb
response = decodeDC(serial.read( ))
ack = usb.readAckByte()
if (!ack) throw IOException
checkHeaderCRC(response)
response[5] is NAK (21) # look up NAK
if response[4] != commandCode # throw Error
dataLen = response[5] # length
cpyLen = len(response) - 6 - 1
return response[6:-1]
packSerial
return makePackedBCD(serial)
encodeDC(msg):
# realign bytes
nibbles = [ ]
encoded = [ ]
# collect nibbles
for (b in msg):
highNibble = b >> 4 & 0xF
lowNibble = b & 0xF
dcValue1 = ENCODE_TABLE[highNibble]
dcValue2 = ENCODE_TABLE[lowNibble]
nibbles.append(dcValue1 >> 2)
high2Bits = dcValue1 & 0x3
low2Bits = dcValue2 >> 4 & 0x3
nibbles.append( high2Bits << 2 | low2Bits )
nibbles.append( dcValue2 & 0xF )
for i in nibbles.iter:
# last item gets a padding terminator
v = nibbles[i]
lb = (v, 5)
# most elide the next item
if i < nibbles.length - 1:
lb = (v, nibbles[i+1])
encoded.append(Util.makeByte(lb[0], lb[2]))
return encoded
decodeDC(msg):
decoded = [ ]
nibbleCount = 0
bitCount = 0
sixBitValue = 0
highValue = 0
highNibble = 0
#
for B in msg:
bP = 7
while bP >= 0:
bitValue = B >> bP & 0x1
sixBitValue = sixBitValue << 1 | bitValue
bitCount++
if (bitCount !=6)
continue; # next
nibbleCount++
if nibbleCount == 1:
highNibble = decodeDCByte(sixBitValue)
else
lowNibble = decodeDCByte(sixBitValue)
byteValue = makeByte(highNibble, lowNibble)
# append to result
decoded.append(byteValue)
nibbleCount = 0
sixBitValue = 0
bitCount = 0
bp--
return decoded
decodeDCByte(B):
# B should be 0 < B && B < 63
# look up in decode table
for k, v in ENCODE_TABLE:
if V == B
return k
set/isUseMultiXmitMode: # simple getter/setter for m_useMultiXmitMode
Command(code, bytesPerRecord, maxRecords, address, addressLength, commandType)
XXX: acquireDataFromDevice, acquireDataFromDeviceConclusion only have disassembly, making it a bit harder to understand.
"""
"""
##########
#
# USB Device
#
initUSBComms
# clear the buffer
serial.readUntilEmpty( )
# set RS232 MODE On
# check success, first byte == 51 READY
sendCommandCheckReply(6, 51)
numOldBytes = readStatus( )
if numOldBytes > 0:
sendTransferDataCommand( )
message = serial.read( )
usb.readAckByte()
readAckByte:
# retries twice
serial.read(1) == 'U' # 85
# else NAK == 'f' # 102
readStatus:
# sets m_status, used to decode receivedByteCount, hasData, RS232Mode,
# FilterRepeat, AutoSleep, Status Error, SelfTestError
self.status = sendCommandGetReply(2)
bytesAvailable = serial.read(1)
readAckByte() # serial.read(1) == ACK
sendCommandGetReply:
sendCommand(command)
return serial.read(1)
sendCommandCheckReply(command, expect):
# retries twice
reply = sendCommandGetReply(command)
return reply == expect
sendCommand(command):
serial.write(command)
setRfMode:
sendCommand(7)
# sleep
readAckByte()
readReadyByte(setRFMode):
#retries twice
readReadyByteIO(setRFMode)
# adjust sleep timing
readReadyByteIO(setRFMode)
# for some reason this is tightly coupled
if setRfMode: setRfMode
# retries twice
serial.read(1) == 51
sendDataTransferCommand:
# data transfer command
sendCommand(8)
"""
from insulaudit import core, lib
class Link( core.CommBuffer ):
class ID:
VENDOR = 0x0a21
PRODUCT = 0x8001
timeout = .150
def
if __name__ == '__main__':
io.info("hello world")
#####
# EOF