-
Notifications
You must be signed in to change notification settings - Fork 12
/
a.py
executable file
·338 lines (281 loc) · 9.66 KB
/
a.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
#!/usr/bin/python
import user
import struct
import sys
import serial
import time
import logging
import binascii
import itertools
from binascii import b2a_hex as dehex
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( 'carelink' )
log.setLevel( logging.FATAL )
log.info( 'hello world' )
io = logging.getLogger( 'carelink.io' )
io.setLevel( logging.DEBUG )
def EnablePumpRadioCmd():
"""
Enables some kind of special multi transmit mode
"""
cmd = FormatCommand(command=93, params=[ 0x01, 0x0A ], retries=0, expectedPages=0)
return cmd
# RF SN
# spare serial(512): 206525
# 522: 665455
def FormatCommand( serial='665455', command=141, params=[ ], retries=2, expectedPages=1 ):
""""
Write Radio Buffer Commands look like this.
While the formatting of this command seems correct, reads of usb status
afterwards should be setting tx indicator and length fields. Instead we're
getting error fields.
00 [ 0x01
01 , 0x00
02 , 0xA7 # 167, tx.packet
03 , 0x01 # ?? potential sequence count?
04 , serial[ 0 ]
05 , serial[ 1 ]
06 , serial[ 3 ]
07 , 0x80 | HighByte( paramCount )
08 , LowByte( paramCount )
09 , code == 93 ? 85 : 0 # initialize pump rf!!!?
10 , maxRetries # sequence count?
11 , pagesSent > 1 ? 2 : pagesSent # sequence count?
12 , 0
13 , code
14 , CRC8( code[ :15 ] )
15 , command parameters....
?? , CRC8( command parameters )
]
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()
ACK: is:
>>> FormatCommand( serial='665455', command=141 )
bytearray( [ 0x01, 0x00, 0xA7, 0x01, 0x66, 0x54, 0x55, 0x80,
0x00, 0x00, 0x02, 0x01, 0x00, 0x8D, 0x5B, 0x00 ] )
"""
readable = 0
code = [ 1 , 0 , 167 , 1, ]
code.extend( list( bytearray( serial.decode('hex') ) ) )
code.extend( [ 0x80 | lib.HighByte( len( params ) )
, lib.LowByte( len( params ) )
, command == 93 and 85 or 0
, retries
, expectedPages
, 0
, command
] )
io.info( 'crc stuff' )
io.info( code )
code.append( lib.CRC8.compute( code ) )
code.extend( params )
code.append( lib.CRC8.compute( params ) )
io.info( '\n' + lib.hexdump( bytearray( code ) ) )
return bytearray( code )
class USBRadioCommand( Command ):
# XXX: !!!!
code = [ 113 ]
__reply_header__ = [ 12, 0 ]
label = 'usb.readdata'
def __init__( self, length ):
super( type( self ), self ).__init__( )
code = [ 12, 0 ]
self.length = length
self.code.extend( [ lib.HighByte( length )
, lib.LowByte( length ) ] )
self.code.append( lib.CRC8.compute( self.code ) )
def readBytes( carelink, length ):
remaining = length
io.info( 'readBytes requested: %s' % length )
result = [ ]
pages = int( remaining/64 ) + 1
#while remaining > 0:
for page in xrange( pages ):
io.info( 'reading page:%s/%s' % ( page, pages ) )
response = carelink.radio( 64 )
remaining = remaining - len( response )
result.append( response )
io.info( 'page:%s:len:%s' % ( page, len( response ) ) )
result = bytearray( ).join( result )
io.info( 'readBytes read: %s' % len( result ) )
return result
def getBytesAvailable( carelink ):
info = carelink( USBStatus( ) ).info
length = info[ 'rfBytesAvailable' ]
io.info( 'initial bytes available: %s' % length )
for x in itertools.takewhile( lambda x: length==0
, itertools.count( ) ):
status = carelink( USBStatus( ) )
length = status.info[ 'rfBytesAvailable' ]
io.info( 'x:%s len: %s' % ( x, length ) )
if length == 0 and x > 10:
io.warning( 'page boundary offset problem? offset by:%s' % '?' )
return length
# utils
def initRadio( carelink ):
print "READ AND EMPTY RADIO"
length = getBytesAvailable( carelink )
print 'found length %s' % length
response = readBytes( carelink, length )
print 'contents of radio:'
debug_response( response )
pprint( carelink( USBSignalStrength( ) ).info )
pprint( carelink( USBProductInfo( ) ).info )
def waitForSignal(stick):
strength = 0
while strength < 1:
strength = stick( USBSignalStrength( ) ).info
io.info( "signal strength: %sdBm" % strength )
return strength
def getNumBytes(stick):
io.info( '##get numbytes')
rfBytes = 0
usbstatus = stick( USBStatus( ) )
start = time.time()
while (rfBytes == 0 and time.time() - start < 3) \
or usbstatus.info[ 'status' ].flags[ 'receiving.complete' ]:
try:
usbstatus = stick( USBStatus( ) )
time.sleep(.1)
rfBytes = usbstatus.info[ 'rfBytesAvailable' ]
except CommErrorException, e:
io.error(e)
print "RFBYTES: %s" % rfBytes
return rfBytes
def sendOneCommand( carelink, command=141 ):
print '######### Send one Command ###########'
time.sleep(.1)
print '###### Write Command to Port #####'
#command = FormatCommand( )
command = FormatCommand( command=command )
#print lib.hexdump( bytearray( command ) )
carelink.write( str( bytearray( command ) ) )
time.sleep(1.7)
response = carelink.read( 64 )
time.sleep(1.7)
rfBytes = getNumBytes(carelink)
print "RFBYTES: %s" % rfBytes
if rfBytes > 0:
print "read data"
print carelink.radio( rfBytes )
else:
print "FAILED TO FIND BYTES"
return
#debug_response( response )
def debug_response( response ):
header, body = response[ :14 ], response[ 14 : 14+response[13] ]
print "HEADER"
print "readable 1 == %s" % header[ 0 ]
print "success (U) fail (f) == %s (%s)" % ( chr( header[ 1 ] ),
header[ 1 ] )
print "error code 0 == %s" % header[ 2 ]
print "message length: %s" % header[ 13 ]
print lib.hexdump( header )
print "msg"
print lib.hexdump( body )
print "msg: %s" % ( str( body ) )
def decodeSerial( serial ):
return serial.encode( 'hex' )
def loopSendComand( carelink ):
for x in itertools.count( ):
print '######### BEGIN LOOP ###########'
print 'loop:%s' % x
sendOneCommand( carelink )
def loopingRead( carelink ):
for x in itertools.count( ):
print '######### BEGIN LOOP ###########'
print 'loop:%s' % x
length = getBytesAvailable( carelink )
print 'found length %s' % length
response = readBytes( carelink, length )
print lib.hexdump( response )
print "Read a total of %s bytes / %s requested" % ( len( response), length )
if len( response ) < length:
remaining = length - len( response )
print "Response was less than requested... "
print "trying the remainder: %s" % remaining
response = readBytes( carelink, remaining + ( remaining % 64 ) )
pprint( carelink( USBStatus( ) ).info )
print 'finishing loop:%s' % x
print '######### STATS ###########'
print 'signal strength: %sdBm' % \
carelink( USBSignalStrength( ) ).info
pprint( carelink( RadioInterfaceStats( ) ).info )
pprint( carelink( USBInterfaceStats( ) ).info )
pprint( carelink( USBProductInfo( ) ).info )
pprint( carelink( USBStatus( ) ).info )
print
if __name__ == '__main__':
print 'hello world'
port = sys.argv[ 1 ]
#port = '/dev/ttyUSB1'
carelink = CarelinkUsb( port )
try:
try:
pprint( carelink( USBProductInfo( ) ).info )
waitForSignal(carelink)
pprint( carelink( RadioInterfaceStats( ) ).info )
pprint( carelink( USBInterfaceStats( ) ).info )
pprint( carelink( USBProductInfo( ) ).info )
waitForSignal(carelink)
carelink.write( str( EnablePumpRadioCmd( ) ) )
reply = carelink.read( 64 )
sendOneCommand( carelink, command=0x75 )
#sendOneCommand( carelink, command=141 )
pprint( carelink( USBProductInfo( ) ).info )
pprint( carelink( RadioInterfaceStats( ) ).info )
pprint( carelink( USBInterfaceStats( ) ).info )
#sendOneCommand( command=0x75, carelink )
######
# pprint( carelink( USBStatus( ) ).info )
# pprint( carelink( USBStatus( ) ).info )
# pprint( carelink( USBStatus( ) ).info )
# pprint( carelink( USBStatus( ) ).info )
#sendOneCommand( carelink )
#initRadio( carelink )
#loopSendComand( carelink )
#loopingRead( carelink )
except KeyboardInterrupt:
print "closing"
except Exception:
carelink.close( )
raise
print "closing for real now"
carelink.close( )
sys.exit( 0 )
pprint( carelink( USBStatus( ) ).info )
info = carelink( USBStatus( ) ).info
pprint( info )
length = info[ 'rfBytesAvailable' ]
print carelink.radio( 64 )
pprint( carelink( USBStatus( ) ).info )
pprint( carelink( USBInterfaceStats( ) ).info )
pprint( carelink( RadioInterfaceStats( ) ).info )
pprint( carelink( USBInterfaceStats( ) ).info )
pprint( carelink( USBProductInfo( ) ).info )
pprint( carelink( USBStatus( ) ).info )
pprint( carelink( USBProductInfo( ) ).info )
pprint( carelink( USBInterfaceStats( ) ).info )
pprint( carelink( RadioInterfaceStats( ) ).info )
#print carelink( USBSignalStrength( ) )
#print carelink( USBInterfaceStats( ) )
#print carelink( RadioInterfaceStats( ) )
#print carelink( USBInterfaceStats( ) )
#print carelink( USBReadData( timeout = 2 ) )
#####
# EOF