forked from gm-stack/overlook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdm880.py
161 lines (141 loc) · 4.83 KB
/
rdm880.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
class Packet(object):
def __init__(self, cmd=None, data=[], stationid=0x00):
self.stationid = stationid
self.cmd = cmd
self.data = data
self.length = None
self.status = None
self.bcc = None
def build(self, debug=False):
self.msgtype = 'command'
pack = {
'stationid': chr(self.stationid),
'cmd': chr(self.cmd),
'data': ''.join([chr(x) for x in self.data]),
}
pack['length'] = chr(len(pack['data']) + 1)
pack['bcc'] = self.stationid ^ ord(pack['length']) ^ self.cmd
for c in self.data:
pack['bcc'] ^= c
pack['bcc'] = chr(pack['bcc'])
raw = '\xaa%(stationid)s%(length)s%(cmd)s%(data)s%(bcc)s\xbb' % pack
if debug:
for k, v in pack.items():
print k.ljust(20, ' '), ' '.join(['%02x' % ord(x) for x in v])
print 'raw'.ljust(20, ' '), ' '.join(['%02X' % ord(x) for x in raw])
return raw
def parse(self, raw):
self.msgtype = 'reply'
raw = [ord(x) for x in raw]
if raw[0] != 0xAA or raw[-1] != 0xBB:
print 'Missing STX or ETX'
return None
raw = raw[1:-1]
self.stationid, self.length, self.status = raw[:3]
raw = raw[4:]
self.data = raw[:self.length - 2]
raw = raw[self.length - 2:]
self.bcc = raw[0]
if len(raw) > 1:
print 'Length did not match packet size!'
return None
def __str__(self):
ret = 'msgtype %s\n' % self.msgtype
if self.msgtype == 'command':
ret += 'stationid %02X\n' % self.stationid
ret += 'cmd %02X\n' % self.cmd
ret += 'data %s\n' % ' '.join(['%02X' % x for x in self.data])
else:
ret += 'stationid %02X\n' % self.stationid
ret += 'length %02X\n' % self.length
ret += 'status %02X (%s)\n' % (self.status, Status.get(self.status, 'Unknown status code'))
ret += 'data %s\n' % ' '.join(['%02X' % x for x in self.data])
ret += 'bcc %02X\n' % self.bcc
return ret.rstrip('\n')
def execute(self, io):
raw = self.build()
io.write(raw)
raw = io.read(3)
length = ord(raw[2])
raw += io.read(length + 2)
p = Packet()
p.parse(raw)
return p
class CommandSet(object):
def __init__(self, name, data):
self.name = name
self.data = data
def __getattr__(self, key):
return self.data[key]
ISO14443A = CommandSet('ISO14443A', {
'Request': 0x03,
'Anticollision': 0x04,
'Select': 0x05,
'Halt': 0x06,
'Transfer': 0x28,
})
ISO14443B = CommandSet('ISO14443B', {
'Request': 0x09,
'Anticollision': 0x0A,
'Attrib': 0x0B,
'Rst': 0x0C,
'Transfer': 0x0D,
})
Mifare = CommandSet('Mifare', {
'Read': 0x20,
'Write': 0x21,
'InitVal': 0x22,
'Decrement': 0x23,
'Increment': 0x24,
'GetSNR': 0x25,
})
System = CommandSet('System', {
'SetAddress': 0x80,
'SetBaudrate': 0x81,
'SetSerialNumber': 0x82,
'GetSerialNumber': 0x83,
'Write_UserInfo': 0x84,
'Read_UserInfo': 0x85,
'Get_VersionNum': 0x86,
'Control_Led1': 0x87,
'Control_Led2': 0x88,
'Control_Buzzer': 0x89,
})
ISO15693 = CommandSet('ISO15693', {
'Inventory': 0x10,
'Read': 0x11,
'Write': 0x12,
'Lockblock': 0x13,
'StayQuiet': 0x14,
'Select': 0x15,
'Resetready': 0x16,
'Write_AFI': 0x17,
'Lock_AFI': 0x18,
'Write_DSFID': 0x19,
'Lock_DSFID': 0x1A,
'Get_Information': 0x1B,
'Get_Multiple_Block_Security': 0x1C,
'Transfer': 0x1D,
})
Status = {
0x00: 'Command OK',
0x01: 'Command failed',
0x80: 'Set OK',
0x81: 'Set failed',
0x82: 'Reader reply timeout',
0x83: 'Card does not exist',
0x84: 'The data response from the card is error',
0x85: 'Invalid command parameter',
0x87: 'Unknown internal error',
0x8f: 'Reader received unknown command',
0x8a: 'ISO14443: Error in InitVal process',
0x8b: 'ISO14443: Wrong SNR during anticollision loop',
0x8c: 'ISO14443: Authentication failure',
0x90: 'ISO15693: The card does not support this command',
0x91: 'ISO15693: Invalid command format',
0x92: 'ISO15693: Do not support option mode',
0x93: 'ISO15693: Block does not exist',
0x94: 'ISO15693: The object has been locked',
0x95: 'ISO15693: Lock operation failed',
0x96: 'ISO15693: Operation failed',
}