-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbutility.py
52 lines (43 loc) · 1.3 KB
/
usbutility.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
def hexdump( packet ):
value=""
for byte in packet[:-1]:
value=value+("0x%2.2x, " % byte)
if len(packet)>=1:
value=value+("0x%2.2x" % packet[-1])
return value
#This is for usb commands, which are all mostly little endian
def debyteifyLittleEndian( data ):
value=0
for i in range(len(data)-1, -1, -1):
value <<= 8
value |= data[i]
return value
#This is big endian version, which is for most of the buffers
#for the remote device
def debyteify( data ):
value =0
for i in range(len(data)):
value <<= 8
value |= data[i]
return value
#Big endian turning value into bytes
def byteify( value, length ):
bytes=[]
for i in range(length-1, -1, -1):
bytes.append( (value >> (i*8)) & 0xff)
return bytes
#The mask sets what bits need to match,
#All 3 fields must be the same length.
def buffer_match( dataA, dataB, mask ):
if len(dataA) != len(dataB):
return False
if len(mask) != len(dataA):
raise ValueError("Mask must be the same length as first buffer")
for i in range(len(dataA)):
if (dataA[i] & mask[i]) != (dataB[i] & mask[i]):
return False
return True
def valueOrDefault( map, key, default ):
if map.has_key(key):
return map[key]
return default