Skip to content

Commit

Permalink
fix #7: fix a bug when working in API escaped mode
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Moral <ruben.moral@digi.com>
  • Loading branch information
rubenmoral committed Dec 1, 2017
1 parent f420d87 commit 840c024
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 3 additions & 3 deletions digi/xbee/packets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class XBeePacket:
__metaclass__ = ABCMeta
__ESCAPE_BYTES = [i.value for i in SpecialByte]
__ESCAPE_FACTOR = 0x20
__ESCAPE_BYTE = SpecialByte.ESCAPE_BYTE.code
ESCAPE_BYTE = SpecialByte.ESCAPE_BYTE.code

def __init__(self):
"""
Expand Down Expand Up @@ -242,10 +242,10 @@ def _unescape_data(data):
new_data = bytearray(0)
des_escape = False
for byte in data:
if byte == XBeePacket.__ESCAPE_BYTE:
if byte == XBeePacket.ESCAPE_BYTE:
des_escape = True
else:
new_data += chr(byte ^ 0x20 if des_escape else byte)
new_data.append(byte ^ 0x20 if des_escape else byte)
des_escape = False
return new_data

Expand Down
17 changes: 14 additions & 3 deletions digi/xbee/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
from digi.xbee.models.address import XBee64BitAddress, XBee16BitAddress
from digi.xbee.models.message import XBeeMessage, ExplicitXBeeMessage, IPMessage, \
SMSMessage
from digi.xbee.models.mode import OperatingMode
from digi.xbee.models.options import ReceiveOptions
from digi.xbee.models.protocol import XBeeProtocol
from digi.xbee.packets import factory
from digi.xbee.packets.aft import ApiFrameType
from digi.xbee.packets.base import XBeePacket
from digi.xbee.packets.common import ReceivePacket
from digi.xbee.packets.raw import RX64Packet, RX16Packet
from digi.xbee.util import utils
Expand Down Expand Up @@ -294,7 +296,7 @@ def run(self):
self.__stop = False
while not self.__stop:
# Try to read a packet.
raw_packet = self.__try_read_packet()
raw_packet = self.__try_read_packet(self.__xbee_device.operating_mode)

if raw_packet is not None:
# If the current protocol is 802.15.4, the packet may have to be discarded.
Expand Down Expand Up @@ -623,7 +625,7 @@ def __execute_user_callbacks(self, xbee_packet, remote=None):
sender=str(xbee_packet.phone_number),
more_data=xbee_packet.data))

def __try_read_packet(self):
def __try_read_packet(self, operating_mode=OperatingMode.API_MODE):
"""
Reads the next packet. Starts to read when finds the start delimiter.
The last byte read is the checksum.
Expand All @@ -639,13 +641,22 @@ def __try_read_packet(self):
"""
try:
xbee_packet = bytearray(1)
# Add packet delimiter.
xbee_packet[0] = self.__serial_port.read_byte()
while xbee_packet[0] != SpecialByte.HEADER_BYTE.value:
xbee_packet[0] = self.__serial_port.read_byte()
packet_length = self.__serial_port.read_bytes(2)
# Add packet length.
xbee_packet += packet_length
length = utils.length_to_int(packet_length)
xbee_packet += self.__serial_port.read_bytes(length)
# Add packet payload.
for _ in range(0, length):
read_byte = self.__serial_port.read_byte()
xbee_packet.append(read_byte)
# Read escaped bytes in API escaped mode.
if operating_mode == OperatingMode.ESCAPED_API_MODE and read_byte == XBeePacket.ESCAPE_BYTE:
xbee_packet.append(self.__serial_port.read_byte())
# Add packet checksum.
xbee_packet.append(self.__serial_port.read_byte())
return xbee_packet
except TimeoutException:
Expand Down

0 comments on commit 840c024

Please sign in to comment.