]> sigrok.org Git - libsigrokdecode.git/blob - decoders/em4100/pd.py
instance.c: Fix a memory leak reported by scan-build.
[libsigrokdecode.git] / decoders / em4100 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Benjamin Larsson <benjamin@southpole.se>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 class SamplerateError(Exception):
23     pass
24
25 class Decoder(srd.Decoder):
26     api_version = 3
27     id = 'em4100'
28     name = 'EM4100'
29     longname = 'RFID EM4100'
30     desc = 'EM4100 100-150kHz RFID protocol.'
31     license = 'gplv2+'
32     inputs = ['logic']
33     outputs = ['em4100']
34     channels = (
35         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
36     )
37     options = (
38         {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
39             'values': ('active-low', 'active-high')},
40         {'id': 'datarate' , 'desc': 'Data rate', 'default': 64,
41             'values': (64, 32, 16)},
42 #        {'id': 'coding', 'desc': 'Bit coding', 'default': 'biphase',
43 #            'values': ('biphase', 'manchester', 'psk')},
44         {'id': 'coilfreq', 'desc': 'Coil frequency', 'default': 125000},
45     )
46     annotations = (
47         ('bit', 'Bit'),
48         ('header', 'Header'),
49         ('version-customer', 'Version/customer'),
50         ('data', 'Data'),
51         ('rowparity-ok', 'Row parity OK'),
52         ('rowparity-err', 'Row parity error'),
53         ('colparity-ok', 'Column parity OK'),
54         ('colparity-err', 'Column parity error'),
55         ('stopbit', 'Stop bit'),
56         ('tag', 'Tag'),
57     )
58     annotation_rows = (
59         ('bits', 'Bits', (0,)),
60         ('fields', 'Fields', (1, 2, 3, 4, 5, 6, 7, 8)),
61         ('tags', 'Tags', (9,)),
62     )
63
64     def __init__(self):
65         self.reset()
66
67     def reset(self):
68         self.samplerate = None
69         self.oldpin = None
70         self.last_samplenum = None
71         self.lastlast_samplenum = None
72         self.last_edge = 0
73         self.bit_width = 0
74         self.halfbit_limit = 0
75         self.oldpp = 0
76         self.oldpl = 0
77         self.oldsamplenum = 0
78         self.last_bit_pos = 0
79         self.ss_first = 0
80         self.first_one = 0
81         self.state = 'HEADER'
82         self.data = 0
83         self.data_bits = 0
84         self.ss_data = 0
85         self.data_parity = 0
86         self.payload_cnt = 0
87         self.data_col_parity = [0, 0, 0, 0, 0, 0]
88         self.col_parity = [0, 0, 0, 0, 0, 0]
89         self.tag = 0
90         self.all_row_parity_ok = True
91         self.col_parity_pos = []
92
93     def metadata(self, key, value):
94         if key == srd.SRD_CONF_SAMPLERATE:
95             self.samplerate = value
96         self.bit_width = (self.samplerate / self.options['coilfreq']) * self.options['datarate']
97         self.halfbit_limit = self.bit_width/2 + self.bit_width/4
98         self.polarity = 0 if self.options['polarity'] == 'active-low' else 1
99
100     def start(self):
101         self.out_ann = self.register(srd.OUTPUT_ANN)
102
103     def putbit(self, bit, ss, es):
104         self.put(ss, es, self.out_ann, [0, [str(bit)]])
105         if self.state == 'HEADER':
106             if bit == 1:
107                 if self.first_one > 0:
108                     self.first_one += 1
109                 if self.first_one == 9:
110                     self.put(self.ss_first, es, self.out_ann,
111                              [1, ['Header', 'Head', 'He', 'H']])
112                     self.first_one = 0
113                     self.state = 'PAYLOAD'
114                     return
115                 if self.first_one == 0:
116                     self.first_one = 1
117                     self.ss_first = ss
118
119             if bit == 0:
120                 self.first_one = 0
121             return
122
123         if self.state == 'PAYLOAD':
124             self.payload_cnt += 1
125             if self.data_bits == 0:
126                 self.ss_data = ss
127                 self.data = 0
128                 self.data_parity = 0
129             self.data_bits += 1
130             if self.data_bits == 5:
131                 s = 'Version/customer' if self.payload_cnt <= 10 else 'Data'
132                 c = 2 if self.payload_cnt <= 10 else 3
133                 self.put(self.ss_data, ss, self.out_ann,
134                          [c, [s + ': %X' % self.data, '%X' % self.data]])
135                 s = 'OK' if self.data_parity == bit else 'ERROR'
136                 c = 4 if s == 'OK' else 5
137                 if s == 'ERROR':
138                     self.all_row_parity_ok = False
139                 self.put(ss, es, self.out_ann,
140                          [c, ['Row parity: ' + s, 'RP: ' + s, 'RP', 'R']])
141                 self.tag = (self.tag << 4) | self.data
142                 self.data_bits = 0
143                 if self.payload_cnt == 50:
144                     self.state = 'TRAILER'
145                     self.payload_cnt = 0
146
147             self.data_parity ^= bit
148             self.data_col_parity[self.data_bits] ^= bit
149             self.data = (self.data << 1) | bit
150             return
151
152         if self.state == 'TRAILER':
153             self.payload_cnt += 1
154             if self.data_bits == 0:
155                 self.ss_data = ss
156                 self.data = 0
157                 self.data_parity = 0
158             self.data_bits += 1
159             self.col_parity[self.data_bits] = bit
160             self.col_parity_pos.append([ss, es])
161
162             if self.data_bits == 5:
163                 self.put(ss, es, self.out_ann, [8, ['Stop bit', 'SB', 'S']])
164
165                 for i in range(1, 5):
166                     s = 'OK' if self.data_col_parity[i] == \
167                                 self.col_parity[i] else 'ERROR'
168                     c = 6 if s == 'OK' else 7
169                     self.put(self.col_parity_pos[i - 1][0],
170                              self.col_parity_pos[i - 1][1], self.out_ann,
171                              [c, ['Column parity %d: %s' % (i, s),
172                                   'CP%d: %s' % (i, s), 'CP%d' % i, 'C']])
173
174                 # Emit an annotation for valid-looking tags.
175                 all_col_parity_ok = (self.data_col_parity[1:5] == self.col_parity[1:5])
176                 if all_col_parity_ok and self.all_row_parity_ok:
177                     self.put(self.ss_first, es, self.out_ann,
178                              [9, ['Tag: %010X' % self.tag, 'Tag', 'T']])
179
180                 self.tag = 0
181                 self.data_bits = 0
182
183                 if self.payload_cnt == 5:
184                     self.state = 'HEADER'
185                     self.payload_cnt = 0
186                     self.data_col_parity = [0, 0, 0, 0, 0, 0]
187                     self.col_parity = [0, 0, 0, 0, 0, 0]
188                     self.col_parity_pos = []
189                     self.all_row_parity_ok = True
190
191     def manchester_decode(self, pl, pp, pin):
192         bit = self.oldpin ^ self.polarity
193         if pl > self.halfbit_limit:
194             es = int(self.samplenum - pl/2)
195             if self.oldpl > self.halfbit_limit:
196                 ss = int(self.oldsamplenum - self.oldpl/2)
197             else:
198                 ss = int(self.oldsamplenum - self.oldpl)
199             self.putbit(bit, ss, es)
200             self.last_bit_pos = int(self.samplenum - pl/2)
201         else:
202             es = int(self.samplenum)
203             if self.oldpl > self.halfbit_limit:
204                 ss = int(self.oldsamplenum - self.oldpl/2)
205                 self.putbit(bit, ss, es)
206                 self.last_bit_pos = int(self.samplenum)
207             else:
208                 if self.last_bit_pos <= self.oldsamplenum - self.oldpl:
209                     ss = int(self.oldsamplenum - self.oldpl)
210                     self.putbit(bit, ss, es)
211                     self.last_bit_pos = int(self.samplenum)
212
213     def decode(self):
214         if not self.samplerate:
215             raise SamplerateError('Cannot decode without samplerate.')
216
217         # Initialize internal state from the very first sample.
218         (pin,) = self.wait()
219         self.oldpin = pin
220         self.last_samplenum = self.samplenum
221         self.lastlast_samplenum = self.samplenum
222         self.last_edge = self.samplenum
223         self.oldpl = 0
224         self.oldpp = 0
225         self.oldsamplenum = 0
226         self.last_bit_pos = 0
227
228         while True:
229             # Ignore identical samples, only process edges.
230             (pin,) = self.wait({0: 'e'})
231             pl = self.samplenum - self.oldsamplenum
232             pp = pin
233             self.manchester_decode(pl, pp, pin)
234             self.oldpl = pl
235             self.oldpp = pp
236             self.oldsamplenum = self.samplenum
237             self.oldpin = pin