Skip to content

Commit fcc36de

Browse files
committed
Backported parsing of IS AT command response as I/O data from ZigBee devices
1 parent 60a97a6 commit fcc36de

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

xbee/ieee.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ class XBee(XBeeBase):
131131
[{'name':'frame_id', 'len':1},
132132
{'name':'command', 'len':2},
133133
{'name':'status', 'len':1},
134-
{'name':'parameter', 'len':None}]},
134+
{'name':'parameter', 'len':None}],
135+
'parsing': [('parameter',
136+
lambda xbee,original: xbee._parse_IS_at_response(original))]
137+
},
135138
b"\x97":
136139
{'name':'remote_at_response',
137140
'structure':
@@ -140,8 +143,21 @@ class XBee(XBeeBase):
140143
{'name':'source_addr', 'len':2},
141144
{'name':'command', 'len':2},
142145
{'name':'status', 'len':1},
143-
{'name':'parameter', 'len':None}]},
146+
{'name':'parameter', 'len':None}],
147+
'parsing': [('parameter',
148+
lambda xbee,original: xbee._parse_IS_at_response(original))]
149+
},
144150
}
151+
152+
def _parse_IS_at_response(self, packet_info):
153+
"""
154+
If the given packet is a successful remote AT response for an IS
155+
command, parse the parameter field as IO data.
156+
"""
157+
if packet_info['id'] in ('at_response','remote_at_response') and packet_info['command'].lower() == b'is' and packet_info['status'] == b'\x00':
158+
return self._parse_samples(packet_info['parameter'])
159+
else:
160+
return packet_info['parameter']
145161

146162
def __init__(self, *args, **kwargs):
147163
# Call the super class constructor to save the serial port

xbee/tests/test_ieee.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import unittest, sys, traceback
1111
from xbee.tests.Fake import FakeDevice, FakeReadDevice
1212
from xbee.ieee import XBee
13+
from xbee.frame import APIFrame
1314
from xbee.python2to3 import byteToInt, intToByte, stringToBytes
1415

1516
class InitXBee(unittest.TestCase):
@@ -561,6 +562,72 @@ def test_read_at_params(self):
561562
'parameter':b'\x00\x00\x00'}
562563
self.assertEqual(info, expected_info)
563564

565+
def test_is_response_parsed_as_io(self):
566+
"""
567+
I/O data in a AT response for an IS command is parsed.
568+
"""
569+
## Build IO data
570+
# One sample, ADC 0 enabled
571+
# DIO 1,3,5,7 enabled
572+
header = b'\x01\x02\xAA'
573+
574+
# First 7 bits ignored, DIO8 low, DIO 0-7 alternating
575+
# ADC0 value of 255
576+
sample = b'\x00\xAA\x00\xFF'
577+
data = header + sample
578+
579+
device = FakeReadDevice(
580+
APIFrame(data = b'\x88DIS\x00' + data).output()
581+
)
582+
583+
xbee = XBee(device)
584+
585+
info = xbee.wait_read_frame()
586+
expected_info = {'id':'at_response',
587+
'frame_id':b'D',
588+
'command':b'IS',
589+
'status':b'\x00',
590+
'parameter':[{'dio-1':True,
591+
'dio-3':True,
592+
'dio-5':True,
593+
'dio-7':True,
594+
'adc-0':255}]}
595+
self.assertEqual(info, expected_info)
596+
597+
def test_is_remote_response_parsed_as_io(self):
598+
"""
599+
I/O data in a Remote AT response for an IS command is parsed.
600+
"""
601+
## Build IO data
602+
# One sample, ADC 0 enabled
603+
# DIO 1,3,5,7 enabled
604+
header = b'\x01\x02\xAA'
605+
606+
# First 7 bits ignored, DIO8 low, DIO 0-7 alternating
607+
# ADC0 value of 255
608+
sample = b'\x00\xAA\x00\xFF'
609+
data = header + sample
610+
611+
device = FakeReadDevice(
612+
APIFrame(data = b'\x97D\x00\x13\xa2\x00@oG\xe4v\x1aIS\x00' + data).output()
613+
)
614+
615+
xbee = XBee(device)
616+
617+
info = xbee.wait_read_frame()
618+
expected_info = {'id':'remote_at_response',
619+
'frame_id':b'D',
620+
'source_addr_long': b'\x00\x13\xa2\x00@oG\xe4',
621+
'source_addr': b'v\x1a',
622+
'command':b'IS',
623+
'status':b'\x00',
624+
'parameter':[{'dio-1':True,
625+
'dio-3':True,
626+
'dio-5':True,
627+
'dio-7':True,
628+
'adc-0':255}]}
629+
self.assertEqual(info, expected_info)
630+
564631
def test_read_io_data(self):
565632
"""
566633
XBee class should properly read and parse incoming IO data

0 commit comments

Comments
 (0)