# Path: src/crazyflieROS/cflib/crtp/crtpstack.py
# class CRTPPacket(object):
#     """
#     A packet that can be sent via the CRTP.
#     """
# 
#     def __init__(self, header=0, data=None):
#         """
#         Create an empty packet with default values.
#         """
#         self.size = 0
#         self._data = ""
#         # The two bits in position 3 and 4 needs to be set for legacy
#         # support of the bootloader
#         self.header = header | 0x3 << 2
#         self._port = (header & 0xF0) >> 4
#         self._channel = header & 0x03
#         if data:
#             self._set_data(data)
# 
#     def _get_channel(self):
#         """Get the packet channel"""
#         return self._channel
# 
#     def _set_channel(self, channel):
#         """Set the packet channel"""
#         self._channel = channel
#         self._update_header()
# 
#     def _get_port(self):
#         """Get the packet port"""
#         return self._port
# 
#     def _set_port(self, port):
#         """Set the packet port"""
#         self._port = port
#         self._update_header()
# 
#     def get_header(self):
#         """Get the header"""
#         self._update_header()
#         return self.header
# 
#     def set_header(self, port, channel):
#         """
#         Set the port and channel for this packet.
#         """
#         self._port = port
#         self.channel = channel
#         self._update_header()
# 
#     def _update_header(self):
#         """Update the header with the port/channel values"""
#         # The two bits in position 3 and 4 needs to be set for legacy
#         # support of the bootloader
#         self.header = ((self._port & 0x0f) << 4 | 3 << 2 |
#                        (self.channel & 0x03))
# 
#     #Some python madness to access different format of the data
#     def _get_data(self):
#         """Get the packet data"""
#         return self._data
# 
#     def _set_data(self, data):
#         """Set the packet data"""
#         if type(data) == str:
#             self._data = data
#         elif type(data) == list or type(data) == tuple:
#             if len(data) == 1:
#                 self._data = struct.pack("B", data[0])
#             elif len(data) > 1:
#                 self._data = struct.pack("B" * len(data), *data)
#             else:
#                 self._data = ""
#         else:
#             raise Exception("Data shall be of str, tupple or list type")
# 
#     def _get_data_l(self):
#         """Get the data in the packet as a list"""
#         return list(self._get_data_t())
# 
#     def _get_data_t(self):
#         """Get the data in the packet as a tuple"""
#         return struct.unpack("B" * len(self._data), self._data)
# 
#     def __str__(self):
#         """Get a string representation of the packet"""
#         return "{}:{} {}".format(self._port, self.channel, self.datat)
# 
#     data = property(_get_data, _set_data)
#     datal = property(_get_data_l, _set_data)
#     datat = property(_get_data_t, _set_data)
#     datas = property(_get_data, _set_data)
#     port = property(_get_port, _set_port)
#     channel = property(_get_channel, _set_channel)
# 
# Path: src/crazyflieROS/cflib/crtp/exceptions.py
# class WrongUriType (Exception):
#     """ Wrong type of URI for this interface """
#     pass


import logging
import threading
import Queue
import re
import array
                import traceback
from cflib.crtp.crtpdriver import CRTPDriver
from .crtpstack import CRTPPacket
from .exceptions import WrongUriType
from cflib.drivers.crazyradio import Crazyradio
from usb import USBError
            except Exception as e:
                self.link_error_callback("Error communicating with crazy radio"
                                         " ,it has probably been unplugged!\n"
                                         "Exception:%s\n\n%s" % (e,
                                         traceback.format_exc()))

            # Analise the in data packet ...
            if ackStatus is None:
                if (self.link_error_callback is not None):
                    self.link_error_callback("Dongle communication error"
                                             " (ackStatus==None)")
                continue

            if (self.link_quality_callback is not None):
                self.link_quality_callback((10 - ackStatus.retry) * 10)

            # If no copter, retry
            if ackStatus.ack is False:
                self.retryBeforeDisconnect = self.retryBeforeDisconnect - 1
                if (self.retryBeforeDisconnect == 0 and
                        self.link_error_callback is not None):
                    self.link_error_callback("Too many packets lost")
                continue
            self.retryBeforeDisconnect = self.RETRYCOUNT_BEFORE_DISCONNECT

            data = ackStatus.data

            # If there is a copter in range, the packet is analysed and the
            # next packet to send is prepared
            if (len(data) > 0):

# Path: src/crazyflieROS/cflib/crtp/crtpstack.py
# class CRTPPacket(object):
#     """
#     A packet that can be sent via the CRTP.
#     """
# 
#     def __init__(self, header=0, data=None):
#         """
#         Create an empty packet with default values.
#         """
#         self.size = 0
#         self._data = ""
#         # The two bits in position 3 and 4 needs to be set for legacy
#         # support of the bootloader
#         self.header = header | 0x3 << 2
#         self._port = (header & 0xF0) >> 4
#         self._channel = header & 0x03
#         if data:
#             self._set_data(data)
# 
#     def _get_channel(self):
#         """Get the packet channel"""
#         return self._channel
# 
#     def _set_channel(self, channel):
#         """Set the packet channel"""
#         self._channel = channel
#         self._update_header()
# 
#     def _get_port(self):
#         """Get the packet port"""
#         return self._port
# 
#     def _set_port(self, port):
#         """Set the packet port"""
#         self._port = port
#         self._update_header()
# 
#     def get_header(self):
#         """Get the header"""
#         self._update_header()
#         return self.header
# 
#     def set_header(self, port, channel):
#         """
#         Set the port and channel for this packet.
#         """
#         self._port = port
#         self.channel = channel
#         self._update_header()
# 
#     def _update_header(self):
#         """Update the header with the port/channel values"""
#         # The two bits in position 3 and 4 needs to be set for legacy
#         # support of the bootloader
#         self.header = ((self._port & 0x0f) << 4 | 3 << 2 |
#                        (self.channel & 0x03))
# 
#     #Some python madness to access different format of the data
#     def _get_data(self):
#         """Get the packet data"""
#         return self._data
# 
#     def _set_data(self, data):
#         """Set the packet data"""
#         if type(data) == str:
#             self._data = data
#         elif type(data) == list or type(data) == tuple:
#             if len(data) == 1:
#                 self._data = struct.pack("B", data[0])
#             elif len(data) > 1:
#                 self._data = struct.pack("B" * len(data), *data)
#             else:
#                 self._data = ""
#         else:
#             raise Exception("Data shall be of str, tupple or list type")
# 
#     def _get_data_l(self):
#         """Get the data in the packet as a list"""
#         return list(self._get_data_t())
# 
#     def _get_data_t(self):
#         """Get the data in the packet as a tuple"""
#         return struct.unpack("B" * len(self._data), self._data)
# 
#     def __str__(self):
#         """Get a string representation of the packet"""
#         return "{}:{} {}".format(self._port, self.channel, self.datat)
# 
#     data = property(_get_data, _set_data)
#     datal = property(_get_data_l, _set_data)
#     datat = property(_get_data_t, _set_data)
#     datas = property(_get_data, _set_data)
#     port = property(_get_port, _set_port)
#     channel = property(_get_channel, _set_channel)
# 
# Path: src/crazyflieROS/cflib/crtp/exceptions.py
# class WrongUriType (Exception):
#     """ Wrong type of URI for this interface """
#     pass


# Path: src/crazyflieROS/cflib/crtp/radiodriver.py
import logging
import threading
import Queue
import re
import array
                import traceback
from cflib.crtp.crtpdriver import CRTPDriver
from .crtpstack import CRTPPacket
from .exceptions import WrongUriType
from cflib.drivers.crazyradio import Crazyradio
from usb import USBError

            except Exception as e:
                self.link_error_callback("Error communicating with crazy radio"
                                         " ,it has probably been unplugged!\n"
                                         "Exception:%s\n\n%s" % (e,
                                         traceback.format_exc()))

            # Analise the in data packet ...
            if ackStatus is None:
                if (self.link_error_callback is not None):
                    self.link_error_callback("Dongle communication error"
                                             " (ackStatus==None)")
                continue

            if (self.link_quality_callback is not None):
                self.link_quality_callback((10 - ackStatus.retry) * 10)

            # If no copter, retry
            if ackStatus.ack is False:
                self.retryBeforeDisconnect = self.retryBeforeDisconnect - 1
                if (self.retryBeforeDisconnect == 0 and
                        self.link_error_callback is not None):
                    self.link_error_callback("Too many packets lost")
                continue
            self.retryBeforeDisconnect = self.RETRYCOUNT_BEFORE_DISCONNECT

            data = ackStatus.data

            # If there is a copter in range, the packet is analysed and the
            # next packet to send is prepared
            if (len(data) > 0):

                inPacket = CRTPPacket(data[0], list(data[1:]))