Skip to content

Commit

Permalink
Changed to bytearray for CRTP data (bitcraze#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
evoggy committed Sep 7, 2015
1 parent 17c6262 commit 8590367
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 74 deletions.
4 changes: 2 additions & 2 deletions lib/cfclient/ui/toolboxes/CrtpSharkToolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def _packet(self, dir, pk):
line.setData(0, Qt.DisplayRole, "%d" % ms_diff)
line.setData(1, Qt.DisplayRole, "%s" % dir)
line.setData(2, Qt.DisplayRole, "%d/%d" % (pk.port, pk.channel))
line.setData(3, Qt.DisplayRole, pk.datal.__str__())
line.setData(3, Qt.DisplayRole, pk.data.decode("UTF-8"))

s = "%d, %s, %d/%d, %s" % (ms_diff, dir, pk.port, pk.channel,
pk.datal.__str__())
pk.data.decode("UTF-8"))
self._data.append(s)

self.logTree.addTopLevelItem(line)
Expand Down
4 changes: 2 additions & 2 deletions lib/cflib/crazyflie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def remove_port_callback(self, port, cb):

def _no_answer_do_retry(self, pk, pattern):
"""Resend packets that we have not gotten answers to"""
logger.debug("Resending for pattern %s", pattern)
logger.info("Resending for pattern %s", pattern)
# Set the timer to None before trying to send again
self.send_packet(pk, expected_reply=pattern, resend=True)

Expand All @@ -276,7 +276,7 @@ def _check_for_answers(self, pk):
"""
longest_match = ()
if len(self._answer_patterns) > 0:
data = (pk.header,) + pk.datat
data = (pk.header,) + tuple(pk.data)
for p in list(self._answer_patterns.keys()):
logger.debug("Looking for pattern match on %s vs %s", p, data)
if len(p) <= len(data):
Expand Down
42 changes: 12 additions & 30 deletions lib/cflib/crazyflie/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,7 @@ def get_id_from_cstring(name):
def get_cstring_from_id(ident):
"""Return the C-storage name given the variable type id"""
try:
if type(ident) == str:
return LogTocElement.types[ord(ident)][0]
else:
return LogTocElement.types[ident][0]
return LogTocElement.types[ident][0]
except KeyError:
raise KeyError("Type [%d] not found in LogTocElement.types"
"!" % ident)
Expand All @@ -329,10 +326,7 @@ def get_cstring_from_id(ident):
def get_size_from_id(ident):
"""Return the size in bytes given the variable type id"""
try:
if type(ident) == str:
return LogTocElement.types[ord(ident)][2]
else:
return LogTocElement.types[ident][2]
return LogTocElement.types[ident][2]
except KeyError:
raise KeyError("Type [%d] not found in LogTocElement.types"
"!" % ident)
Expand All @@ -341,10 +335,7 @@ def get_size_from_id(ident):
def get_unpack_string_from_id(ident):
"""Return the Python unpack string given the variable type id"""
try:
if type(ident) == str:
return LogTocElement.types[ord(ident)][1]
else:
return LogTocElement.types[ident][1]
return LogTocElement.types[ident][1]
except KeyError:
raise KeyError(
"Type [%d] not found in LogTocElement.types!" % ident)
Expand All @@ -353,26 +344,17 @@ def __init__(self, data=None):
"""TocElement creator. Data is the binary payload of the element."""

if (data):
strs = struct.unpack("s" * len(data[2:]), data[2:])
if sys.version_info < (3,):
strs = ("{}" * len(strs)).format(*strs).split("\0")
else:
s = ""
for ch in strs:
s += ch.decode('ISO-8859-1')
strs = s.split("\x00")
self.group = strs[0]
self.name = strs[1]
naming = data[2:]
zt = bytearray((0, ))
self.group = naming[:naming.find(zt)].decode("ISO-8859-1")
self.name = naming[naming.find(zt)+1:-1].decode("ISO-8859-1")

self.ident = data[0]

self.ctype = LogTocElement.get_cstring_from_id(data[1])
self.pytype = LogTocElement.get_unpack_string_from_id(data[1])

if type(data[1]) == str:
self.access = ord(data[1]) & 0x10
else:
self.access = data[1] & 0x10
self.access = data[1] & 0x10


class Log():
Expand Down Expand Up @@ -483,8 +465,8 @@ def _find_block(self, id):
def _new_packet_cb(self, packet):
"""Callback for newly arrived packets with TOC information"""
chan = packet.channel
cmd = packet.datal[0]
payload = packet.datal[1:]
cmd = packet.data[0]
payload = packet.data[1:]

if (chan == CHAN_SETTINGS):
id = payload[0]
Expand Down Expand Up @@ -525,7 +507,7 @@ def _new_packet_cb(self, packet):
error_status, id, msg)
if block:
block.err_no = error_status
block.started_cb.call(False)
block.started_cb.call(self, False)
# This is a temporary fix, we are adding a new issue
# for this. For some reason we get an error back after
# the block has been started and added. This will show
Expand Down Expand Up @@ -563,7 +545,7 @@ def _new_packet_cb(self, packet):

if (chan == CHAN_LOGDATA):
chan = packet.channel
id = packet.datal[0]
id = packet.data[0]
block = self._find_block(id)
timestamps = struct.unpack("<BBB", packet.data[1:4])
timestamp = (
Expand Down
55 changes: 23 additions & 32 deletions lib/cflib/crazyflie/mem.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def new_data(self, mem, addr, data):
elif self.elements["version"] == 1:
self.datav0 = data
self.mem_handler.read(self, 16, 5)
else:
valid = False
if self._update_finished_cb:
self._update_finished_cb(self)
self._update_finished_cb = None

if addr == 16:
[radio_address_upper, radio_address_lower] = struct.unpack(
Expand All @@ -227,27 +232,24 @@ def new_data(self, mem, addr, data):
self._update_finished_cb = None

def _checksum256(self, st):
if sys.version_info < (3,):
return struct.pack("<B", reduce(lambda x, y: x + y,
map(ord, st)) % 256)
else:
return reduce(lambda x, y: x + y, list(st)) % 256
return reduce(lambda x, y: x + y, list(st)) % 256

def write_data(self, write_finished_cb):
image = bytearray()
if self.elements["version"] == 0:
data = (
0x00, self.elements["radio_channel"],
self.elements["radio_speed"],
self.elements["pitch_trim"], self.elements["roll_trim"])
image = struct.pack("<BBBff", *data)
image += struct.pack("<BBBff", *data)
elif self.elements["version"] == 1:
data = (
0x01, self.elements["radio_channel"],
self.elements["radio_speed"],
self.elements["pitch_trim"], self.elements["roll_trim"],
self.elements["radio_address"] >> 32,
self.elements["radio_address"] & 0xFFFFFFFF)
image = struct.pack("<BBBffBI", *data)
image += struct.pack("<BBBffBI", *data)
# Adding some magic:
image = EEPROM_TOKEN + image
image += struct.pack("B", self._checksum256(image))
Expand Down Expand Up @@ -342,8 +344,8 @@ def _parse_and_check_elements(self, data):
if test_crc == crc:
while len(elem_data) > 0:
(eid, elen) = struct.unpack("BB", elem_data[:2])
self.elements[self.element_mapping[eid]] = elem_data[
2:2 + elen]
self.elements[self.element_mapping[eid]] = \
elem_data[2:2 + elen].decode("ISO-8859-1")
elem_data = elem_data[2 + elen:]
return True
return False
Expand All @@ -360,14 +362,14 @@ def write_data(self, write_finished_cb):
header_data += struct.pack("B", header_crc)

# Now generate the elements part
elem = ""
elem = bytearray()
logger.info(list(self.elements.keys()))
for element in reversed(list(self.elements.keys())):
elem_string = self.elements[element]
# logger.info(">>>> {}".format(elem_string))
key_encoding = self._rev_element_mapping[element]
elem += struct.pack("BB", key_encoding, len(elem_string))
elem += elem_string
elem += bytearray(elem_string.encode("ISO-8859-1"))

elem_data = struct.pack("BB", 0x00, len(elem))
elem_data += elem
Expand All @@ -376,12 +378,6 @@ def write_data(self, write_finished_cb):

data = header_data + elem_data

# Write data
p = ""
for s in data:
p += "0x{:02X} ".format(s)
logger.info(p)

self.mem_handler.write(self, 0x00,
struct.unpack("B" * len(data), data))

Expand Down Expand Up @@ -428,7 +424,7 @@ def __init__(self, mem, addr, length, cf):
self.mem = mem
self.addr = addr
self._bytes_left = length
self.data = b""
self.data = bytearray()
self.cf = cf

self._current_addr = addr
Expand Down Expand Up @@ -493,7 +489,7 @@ def __init__(self, mem, addr, data, cf):
self.addr = addr
self._bytes_left = len(data)
self._data = data
self.data = ""
self.data = bytearray()
self.cf = cf

self._current_addr = addr
Expand Down Expand Up @@ -697,13 +693,8 @@ def refresh(self, refresh_done_callback):
def _new_packet_cb(self, packet):
"""Callback for newly arrived packets for the memory port"""
chan = packet.channel
cmd = packet.datal[0]
raw_payload = struct.pack("B" * (len(packet.datal) - 1),
*packet.datal[1:])
payload = packet.datal[1:]

# logger.info("--------------->CHAN:{}=>{}".format(chan,
# struct.unpack("B"*len(payload), payload)))
cmd = packet.data[0]
payload = packet.data[1:]

if chan == CHAN_INFO:
if cmd == CMD_INFO_NBR:
Expand Down Expand Up @@ -746,9 +737,9 @@ def _new_packet_cb(self, packet):
# Type - 1 byte
mem_type = payload[1]
# Size 4 bytes (as addr)
mem_size = struct.unpack("I", raw_payload[2:6])[0]
mem_size = struct.unpack("I", payload[2:6])[0]
# Addr (only valid for 1-wire?)
mem_addr_raw = struct.unpack("B" * 8, raw_payload[6:14])
mem_addr_raw = struct.unpack("B" * 8, payload[6:14])
mem_addr = ""
for m in mem_addr_raw:
mem_addr += "{:02X}".format(m)
Expand Down Expand Up @@ -807,7 +798,7 @@ def _new_packet_cb(self, packet):

if chan == CHAN_WRITE:
id = cmd
(addr, status) = struct.unpack("<IB", raw_payload[0:5])
(addr, status) = struct.unpack("<IB", payload[0:5])
logger.info(
"WRITE: Mem={}, addr=0x{:X}, status=0x{}".format(
id, addr, status))
Expand All @@ -833,8 +824,8 @@ def _new_packet_cb(self, packet):

if chan == CHAN_READ:
id = cmd
(addr, status) = struct.unpack("<IB", raw_payload[0:5])
data = struct.unpack("B" * len(raw_payload[5:]), raw_payload[5:])
(addr, status) = struct.unpack("<IB", payload[0:5])
data = struct.unpack("B" * len(payload[5:]), payload[5:])
logger.info("READ: Mem={}, addr=0x{:X}, status=0x{}, "
"data={}".format(id, addr, status, data))
# Find the read request
Expand All @@ -844,7 +835,7 @@ def _new_packet_cb(self, packet):
"mem {}".format(id))
rreq = self._read_requests[id]
if status == 0:
if rreq.add_data(addr, raw_payload[5:]):
if rreq.add_data(addr, payload[5:]):
self._read_requests.pop(id, None)
self.mem_read_cb.call(rreq.mem, rreq.addr, rreq.data)
else:
Expand Down
8 changes: 4 additions & 4 deletions lib/cflib/crazyflie/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _check_if_all_updated(self):

def _param_updated(self, pk):
"""Callback with data for an updated parameter"""
var_id = pk.datal[0]
var_id = pk.data[0]
element = self.toc.get_element_by_id(var_id)
if element:
s = struct.unpack(element.pytype, pk.data[1:])[0]
Expand Down Expand Up @@ -310,7 +310,7 @@ def request_param_setvalue(self, pk):
def _new_packet_cb(self, pk):
"""Callback for newly arrived packets"""
if pk.channel == READ_CHANNEL or pk.channel == WRITE_CHANNEL:
var_id = pk.datal[0]
var_id = pk.data[0]
if (pk.channel != TOC_CHANNEL and self._req_param == var_id and
pk is not None):
self.updated_callback(pk)
Expand All @@ -333,7 +333,7 @@ def run(self):
pk = self.request_queue.get() # Wait for request update
self.wait_lock.acquire()
if self.cf.link:
self._req_param = pk.datal[0]
self.cf.send_packet(pk, expected_reply=(pk.datat[0:2]))
self._req_param = pk.data[0]
self.cf.send_packet(pk, expected_reply=(tuple(pk.data[0:2])))
else:
self.wait_lock.release()
4 changes: 2 additions & 2 deletions lib/cflib/crazyflie/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _new_packet_cb(self, packet):
chan = packet.channel
if (chan != 0):
return
payload = struct.pack("B" * (len(packet.datal) - 1), *packet.datal[1:])
payload = packet.data[1:]

if (self.state == GET_TOC_INFO):
[self.nbr_of_items, self._crc] = struct.unpack("<BI", payload[:5])
Expand All @@ -182,7 +182,7 @@ def _new_packet_cb(self, packet):
elif (self.state == GET_TOC_ELEMENT):
# Always add new element, but only request new if it's not the
# last one.
if self.requested_index != struct.unpack("<B", payload[0:1])[0]:
if self.requested_index != payload[0]:
return
self.toc.add_element(self.element_class(payload))
logger.debug("Added element [%s]",
Expand Down
5 changes: 3 additions & 2 deletions lib/cflib/crtp/crtpstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, header=0, data=None):
Create an empty packet with default values.
"""
self.size = 0
self._data = ""
self._data = bytearray()
# The two bits in position 3 and 4 needs to be set for legacy
# support of the bootloader
self.header = header | 0x3 << 2
Expand Down Expand Up @@ -129,7 +129,8 @@ def _set_data(self, data):
elif sys.version_info >= (3,) and type(data) == bytes:
self._data = bytearray(data)
else:
raise Exception("Data shall be of str, tupple or list type")
raise Exception("Data must be bytearray, string, list or tuple,"
" not {}".format(type(data)))

def _get_data_l(self):
"""Get the data in the packet as a list"""
Expand Down

0 comments on commit 8590367

Please sign in to comment.