2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2017 Christoph Rackwitz <christoph.rackwitz@rwth-aachen.de>
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.
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.
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/>.
20 # http://www.gorferay.com/type-a-communications-interface/
21 # https://resources.infosecinstitute.com/introduction-rfid-security/
22 # https://www.radio-electronics.com/info/wireless/nfc/near-field-communications-modulation-rf-signal-interface.php
23 # https://www.researchgate.net/figure/Modified-Miller-Code_fig16_283498836
26 # modified Miller: falling edge
28 import sigrokdecode as srd
30 def roundto(x, k=1.0):
31 return round(x / k) * k
33 class Decoder(srd.Decoder):
37 longname = 'Miller decoder for NFC'
38 desc = 'Decodes (modified) Miller encoding as used in NFC communication.'
43 {'id': 'data', 'name': 'Data', 'desc': 'Data signal'},
46 {'id': 'baudrate', 'desc': 'Baud rate', 'default': 106000},
47 {'id': 'edge', 'desc': 'Edge', 'default': 'falling', 'values': ('rising', 'falling', 'either')},
51 ('bitstring', 'Bitstring'),
53 annotation_rows = tuple((u, v, (i,)) for i, (u, v) in enumerate(annotations))
55 ('raw', 'Raw binary'),
59 self.samplerate = None
61 def metadata(self, key, value):
62 if key == srd.SRD_CONF_SAMPLERATE:
63 self.samplerate = value
66 self.out_ann = self.register(srd.OUTPUT_ANN)
67 self.out_binary = self.register(srd.OUTPUT_BINARY)
69 def decode_bits(self):
70 timeunit = self.samplerate / self.options['baudrate']
71 edgetype = self.options['edge'][0]
73 self.wait({0: edgetype}) # first symbol, beginning of unit
74 prevedge = self.samplenum
76 # start of message: '0'
78 yield (0, prevedge, prevedge + timeunit)
79 expectedstart = self.samplenum + timeunit
81 # end of message: '0' followed by one idle symbol
84 self.wait([{0: edgetype}, {'skip': int(3 * timeunit)}])
85 got_timeout = self.matched[1]
86 sampledelta = (self.samplenum - prevedge)
87 prevedge = self.samplenum
88 timedelta = roundto(sampledelta / timeunit, 0.5)
90 # a mark stands for a 1 bit
91 # a mark has an edge in the middle
93 # a space stands for a 0 bit
94 # a space either has an edge at the beginning or no edge at all
95 # after a mark, a space is edge-less
96 # after a space, a space has an edge
98 # we get 1.0, 1.5, 2.0 times between edges
100 # end of transmission is always a space, either edged or edge-less
102 if prevbit == 0: # space -> ???
103 if timedelta == 1.0: # 1.0 units -> space
104 yield (0, self.samplenum, self.samplenum + timeunit)
106 expectedstart = self.samplenum + timeunit
107 elif timedelta == 1.5: # 1.5 units -> mark
108 yield (1, expectedstart, self.samplenum + 0.5*timeunit)
110 expectedstart = self.samplenum + timeunit*0.5
111 elif timedelta >= 2.0:
112 # idle symbol (end of message)
115 # assert timedelta >= 2.0
116 yield (False, self.samplenum - sampledelta, self.samplenum)
120 yield (False, self.samplenum - sampledelta, self.samplenum)
122 if timedelta == 1.0: # 1.0 units -> mark again (1.5 from start)
123 yield (1, expectedstart, self.samplenum + 0.5*timeunit)
125 expectedstart = self.samplenum + 0.5*timeunit
126 elif timedelta == 1.5: # 1.5 units -> space (no pulse) and space (pulse)
127 yield (0, expectedstart, self.samplenum)
128 yield (0, self.samplenum, self.samplenum + timeunit)
130 expectedstart = self.samplenum + timeunit
131 elif timedelta == 2.0: # 2.0 units -> space (no pulse) and mark (pulse)
132 yield (0, expectedstart, expectedstart + timeunit)
133 yield (1, self.samplenum - 0.5*timeunit, self.samplenum + 0.5*timeunit)
135 expectedstart = self.samplenum + timeunit*0.5
136 else: # longer -> space and end of message
137 yield (0, expectedstart, expectedstart + timeunit)
141 def decode_run(self):
148 for bit in self.decode_bits():
152 (value, ss, es) = bit
155 self.put(int(ss), int(es), self.out_ann, [1, ['ERROR']])
157 self.put(int(ss), int(es), self.out_ann, [0, ['{}'.format(value)]])
163 if stringstart is None:
168 bitvalue |= value << numbits
171 bitstring += '{}'.format(value)
178 self.put(int(stringstart), int(stringend), self.out_ann, [1, ['{}'.format(bitstring)]])
180 numbytes = numbits // 8 + (numbits % 8 > 0)
181 bytestring = bitvalue.to_bytes(numbytes, 'little')
182 self.put(int(stringstart), int(stringend), self.out_binary, [0, bytestring])