]> sigrok.org Git - libsigrokdecode.git/blob - decoders/pan1321/pd.py
e7ccd60378070035e57d76337034a8cd1814ddb8
[libsigrokdecode.git] / decoders / pan1321 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import sigrokdecode as srd
22
23 # ...
24 RX = 0
25 TX = 1
26
27 class Decoder(srd.Decoder):
28     api_version = 1
29     id = 'pan1321'
30     name = 'PAN1321'
31     longname = 'Panasonic PAN1321'
32     desc = 'Bluetooth RF module with Serial Port Profile (SPP).'
33     license = 'gplv2+'
34     inputs = ['uart']
35     outputs = ['pan1321']
36     annotations = [
37         ['text-verbose', 'Human-readable text (verbose)'],
38         ['text', 'Human-readable text'],
39         ['warnings', 'Human-readable warnings'],
40     ]
41
42     def __init__(self, **kwargs):
43         self.cmd = ['', '']
44         self.ss_block = None
45
46     def start(self):
47         # self.out_python = self.register(srd.OUTPUT_PYTHON)
48         self.out_ann = self.register(srd.OUTPUT_ANN)
49
50     def putx(self, data):
51         self.put(self.ss_block, self.es_block, self.out_ann, data)
52
53     def handle_host_command(self, rxtx, s):
54         if s.startswith('AT+JAAC'):
55             # AT+JAAC=<auto_accept> (0 or 1)
56             p = s[s.find('=') + 1:]
57             if p not in ('0', '1'):
58                 self.putx([2, ['Warning: Invalid JAAC parameter "%s"' % p]])
59                 return
60             x = 'Auto' if (p == '1') else 'Don\'t auto'
61             self.putx([0, ['%s-accept new connections' % x]])
62             self.putx([1, ['%s-accept connections' % x]])
63         elif s.startswith('AT+JPRO'):
64             # AT+JPRO=<mode> (0 or 1)
65             p = s[s.find('=') + 1:]
66             if p not in ('0', '1'):
67                 self.putx([2, ['Warning: Invalid JPRO parameter "%s"' % p]])
68                 return
69             onoff = 'off' if (p == '0') else 'on'
70             x = 'Leaving' if (p == '0') else 'Entering'
71             self.putx([0, ['%s production mode' % x]])
72             self.putx([1, ['Production mode = %s' % onoff]])
73         elif s.startswith('AT+JRES'):
74             # AT+JRES
75             if s != 'AT+JRES': # JRES has no params.
76                 self.putx([2, ['Warning: Invalid JRES usage.']])
77                 return
78             self.putx([0, ['Triggering a software reset']])
79             self.putx([1, ['Reset']])
80         elif s.startswith('AT+JSDA'):
81             # AT+JSDA=<l>,<d> (l: length in bytes, d: data)
82             # l is (max?) 3 decimal digits and ranges from 1 to MTU size.
83             # Data can be ASCII or binary values (l bytes total).
84             l, d = s[s.find('=') + 1:].split(',')
85             if not l.isnumeric():
86                 self.putx([2, ['Warning: Invalid data length "%s".' % l]])
87             if int(l) != len(d):
88                 self.putx([2, ['Warning: Data length mismatch (%d != %d).' % \
89                           (int(l), len(d))]])
90             # TODO: Warn if length > MTU size (which is firmware-dependent
91             # and is negotiated by both Bluetooth devices upon connection).
92             b = ''.join(['%02x ' % ord(c) for c in d])[:-1]
93             self.putx([0, ['Sending %d data bytes: %s' % (int(l), b)]])
94             self.putx([1, ['Send %d = %s' % (int(l), b)]])
95         elif s.startswith('AT+JSEC'):
96             # AT+JSEC=<secmode>,<linkkey_info>,<pintype>,<pinlen>,<pin>
97             # secmode: Security mode 1 or 3 (default).
98             # linkkey_info: Must be 1 or 2. Has no function according to docs.
99             # pintype: 1: variable pin (default), 2: fixed pin.
100             # pinlen: PIN length (2 decimal digits). Max. PIN length is 16.
101             # pin: The Bluetooth PIN ('pinlen' chars). Used if pintype=2.
102             # Note: AT+JSEC (if used) must be the first command after reset.
103             # TODO: Parse all the other parameters.
104             pin = s[-4:]
105             self.putx([0, ['Host set the Bluetooth PIN to "' + pin + '"']])
106             self.putx([1, ['PIN = ' + pin]])
107         elif s.startswith('AT+JSLN'):
108             # AT+JSLN=<namelen>,<name>
109             # namelen: Friendly name length (2 decimal digits). Max. len is 18.
110             # name: The Bluetooth "friendly name" ('namelen' ASCII characters).
111             name = s[s.find(',') + 1:]
112             self.putx([0, ['Host set the Bluetooth name to "' + name + '"']])
113             self.putx([1, ['BT name = ' + name]])
114         else:
115             self.putx([0, ['Host sent unsupported command: %s' % s]])
116             self.putx([1, ['Unsupported command: %s' % s]])
117
118     def handle_device_reply(self, rxtx, s):
119         if s == 'ROK':
120             self.putx([0, ['Device initialized correctly']])
121             self.putx([1, ['Init']])
122         elif s == 'OK':
123             self.putx([0, ['Device acknowledged last command']])
124             self.putx([1, ['ACK']])
125         elif s.startswith('ERR'):
126             error = s[s.find('=') + 1:]
127             self.putx([0, ['Device sent error code ' + error]])
128             self.putx([1, ['ERR = ' + error]])
129         else:
130             self.putx([0, ['Device sent an unknown reply: %s' % s]])
131             self.putx([1, ['Unknown reply: %s' % s]])
132
133     def decode(self, ss, es, data):
134         ptype, rxtx, pdata = data
135
136         # For now, ignore all UART packets except the actual data packets.
137         if ptype != 'DATA':
138             return
139
140         # If this is the start of a command/reply, remember the start sample.
141         if self.cmd[rxtx] == '':
142             self.ss_block = ss
143
144         # Append a new (ASCII) byte to the currently built/parsed command.
145         self.cmd[rxtx] += chr(pdata)
146
147         # Get packets/bytes until an \r\n sequence is found (end of command).
148         if self.cmd[rxtx][-2:] != '\r\n':
149             return
150
151         # Handle host commands and device replies.
152         # We remove trailing \r\n from the strings before handling them.
153         self.es_block = es
154         if rxtx == RX:
155             self.handle_device_reply(rxtx, self.cmd[rxtx][:-2])
156         elif rxtx == TX:
157             self.handle_host_command(rxtx, self.cmd[rxtx][:-2])
158         else:
159             raise Exception('Invalid rxtx value: %d' % rxtx)
160
161         self.cmd[rxtx] = ''
162