]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/pan1321.py
srd: pan1321: Support replies from device.
[libsigrokdecode.git] / decoders / pan1321.py
index f79d820ca9bfeade22ba3d2346b3602dbef69daf..031e03dbfca831544b994ee68c36b6d80929ebbb 100644 (file)
@@ -18,6 +18,8 @@
 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 ##
 
+#
+# Panasonic PAN1321 Bluetooth module protocol decoder
 #
 # TODO
 #
@@ -30,10 +32,14 @@ ANN_ASCII = 0
 # UART 'data' packet type.
 T_DATA = 1
 
+# ...
+RX = 0
+TX = 1
+
 class Decoder(srd.Decoder):
     id = 'pan1321'
-    name = 'Panasonic PAN1321'
-    longname = 'TODO.'
+    name = 'PAN1321'
+    longname = 'Panasonic PAN1321'
     desc = 'TODO.'
     longdesc = 'TODO.'
     author = 'Uwe Hermann'
@@ -41,17 +47,14 @@ class Decoder(srd.Decoder):
     license = 'gplv2+'
     inputs = ['uart']
     outputs = ['pan1321']
-    # probes = [
-    # ]
-    options = {
-    }
+    probes = []
+    options = {}
     annotations = [
-        # ANN_ASCII
         ['ASCII', 'TODO: description'],
     ]
 
     def __init__(self, **kwargs):
-        self.cmd = ''
+        self.cmd = ['', '']
 
     def start(self, metadata):
         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'pan1321')
@@ -60,33 +63,55 @@ class Decoder(srd.Decoder):
     def report(self):
         pass
 
+    def handle_host_command(self, ss, es, rxtx, s):
+        if s.startswith('AT+JSEC'):
+            pin = s[s.find('\r\n') - 4:len(s) - 2]
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Host set the Bluetooth PIN to ' + pin]])
+        elif s.startswith('AT+JSLN'):
+            name = s[s.find(',') + 1:-2]
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Host set the Bluetooth name to ' + name]])
+        else:
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Host sent unsupported command']])
+        self.cmd[rxtx] = ''
+
+    def handle_device_reply(self, ss, es, rxtx, s):
+        if s == 'ROK\r\n':
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Device initialized correctly']])
+        elif s == 'OK\r\n':
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Device acknowledged last command']])
+        elif s.startswith('ERR'):
+            error = s[s.find('=') + 1:]
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Device sent error code ' + error]])
+        else:
+            self.put(ss, es, self.out_ann,
+                     [ANN_ASCII, ['Device sent an unknown reply']])
+        self.cmd[rxtx] = ''
+
     def decode(self, ss, es, data):
-        ptype, pdata = data
+        ptype, rxtx, pdata = data
 
         # For now, ignore all UART packets except the actual data packets.
         if ptype != T_DATA:
             return
 
-        # Append new (ASCII) byte to the current command.
-        self.cmd += chr(pdata)
+        # Append a new (ASCII) byte to the currently built/parsed command.
+        self.cmd[rxtx] += chr(pdata)
 
         # Get packets/bytes until an \r\n sequence is found (end of command).
-        if chr(pdata) != '\n':
+        if self.cmd[rxtx][-1:] != '\n':
             return
 
-        s = self.cmd
-
-        # FIXME: This is just a quick hack.
-        if s.startswith('AT+JSEC'):
-            pin = s[s.find('\r\n') - 4:len(s) - 2]
-            self.put(ss, es, self.out_ann,
-                     [ANN_ASCII, ['Setting Bluetooth PIN to ' + pin]])
-        elif s.startswith('AT+JSLN'):
-            name = s[s.find(',') + 1:-2]
-            self.put(ss, es, self.out_ann,
-                     [ANN_ASCII, ['Setting Bluetooth name to ' + name]])
+        # Handle host commands and device replies.
+        if rxtx == RX:
+            self.handle_device_reply(ss, es, rxtx, self.cmd[rxtx])
+        elif rxtx == TX:
+            self.handle_host_command(ss, es, rxtx, self.cmd[rxtx])
         else:
-            self.put(ss, es, self.out_ann, [ANN_ASCII, ['Unsupported command']])
-
-        self.cmd = ''
+            pass # TODO: Error.