]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2cfilter/i2cfilter.py
srd: List 'report()' in all PDs for consistency.
[libsigrokdecode.git] / decoders / i2cfilter / i2cfilter.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 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 # Generic I2C filtering protocol decoder
21
22 import sigrokdecode as srd
23
24 class Decoder(srd.Decoder):
25     api_version = 1
26     id = 'i2cfilter'
27     name = 'I2C filter'
28     longname = 'I2C filter'
29     desc = 'Filter out specific addresses/directions in an I2C stream.'
30     license = 'gplv3+'
31     inputs = ['i2c']
32     outputs = []
33     options = {
34         'address': ['Address to filter out of the I2C stream', 0],
35         'direction': ['Direction to filter (read/write)', '']
36     }
37
38     def __init__(self, **kwargs):
39         self.state = None
40
41     def start(self, metadata):
42         self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2cdata')
43         if self.options['direction'] not in ('', 'read', 'write'):
44             raise Exception('Invalid direction: expected "read" or "write"')
45
46     def report(self):
47         pass
48
49     def decode(self, ss, es, data):
50         try:
51             cmd, data = data
52         except Exception as e:
53             raise Exception('Malformed I2C input: %s' % str(e)) from e
54
55         # Whichever state we're in, these always reset the state machine.
56         # This should make it easier to deal with corrupt data etc.
57         if cmd in ('START', 'START REPEAT'):
58             self.state = 'start'
59             return
60         if cmd == 'STOP':
61             self.state = None
62             return
63         if cmd in ('ACK', 'NACK'):
64             # Don't care, we just want data.
65             return
66
67         if self.state == 'start':
68             # Start of a transfer, see if we want this one.
69             if cmd == 'ADDRESS READ' and self.options['direction'] == 'write':
70                 return
71             elif cmd == 'ADDRESS WRITE' and self.options['direction'] == 'read':
72                 return
73             elif cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
74                 if self.options['address'] in (0, data):
75                     # We want this tranfer.
76                     self.state = 'transfer'
77         elif self.state == 'transfer':
78             if cmd in ('DATA READ', 'DATA WRITE'):
79                 self.put(ss, es, self.out_proto, data)
80         else:
81             raise Exception('Invalid state: %s' % self.state)
82