Beispiel #1
0
def get_pdm_address():
    r = RileyLink()
    try:
        verify_auth(request)
        while True:
            timeout = 30000
            if request.args.get('timeout') is not None:
                timeout = int(request.args.get('timeout')) * 1000
                if timeout > 30000:
                    raise RestApiException("Timeout cannot be more than 30 seconds")

            data = r.get_packet(timeout)
            if data is None:
                p = None
                break

            if data is not None and len(data) > 2:
                calc = crc8(data[2:-1])
                if data[-1] == calc:
                    p = Packet.from_data(data[2:-1])
                    break
        if p is None:
            raise RestApiException("No pdm packet detected")

        return {"radio_address": p.address, "radio_address_hex": "%8X" % p.address}
    finally:
        r.disconnect(ignore_errors=True)
Beispiel #2
0
def get_pdm_address():
    r = RileyLink()
    try:
        verify_auth(request)
        while True:
            timeout = 30000
            if request.args.get('timeout') is not None:
                timeout = int(request.args.get('timeout')) * 1000
                if timeout > 30000:
                    raise RestApiException(
                        "Timeout cannot be more than 30 seconds")

            data = r.get_packet(timeout)
            if data is None:
                p = None
                break

            if data is not None and len(data) > 2:
                calc = crc8(data[2:-1])
                if data[-1] == calc:
                    p = Packet.from_data(data[2:-1])
                    break
        if p is None:
            respond_error("No pdm packet detected")

        return respond_ok({"address": p.address})
    except RestApiException as rae:
        return respond_error(str(rae))
    except Exception:
        logger.exception("Error while trying to read address")
        return respond_error("Other error. Please check log files.")
    finally:
        r.disconnect(ignore_errors=True)
Beispiel #3
0
    def __sendPacketUntilQuiet(self, packetToSend, trackSequencing=True):
        if trackSequencing:
            packetToSend.setSequence(self.packetSequence)
        logging.debug("SENDING PACKET expecting quiet: %s" % packetToSend)
        data = packetToSend.data
        data += bytes([crc.crc8(data)])

        while True:
            self.rileyLink.send_final_packet(data, 10, 25, 42)
            timed_out = False
            try:
                received = self.rileyLink.get_packet(300)
            except RileyLinkError as rle:
                if rle.response_code != Response.RX_TIMEOUT:
                    raise rle
                else:
                    timed_out = True

            if not timed_out:
                p = self.__getPacket(received)
                if p is not None:
                    continue

            if trackSequencing:
                self.packetSequence = (self.packetSequence + 1) % 32
            return
Beispiel #4
0
    def _send_packet(self, packetToSend):
        packetToSend.setSequence(self.packetSequence)
        try:
            data = packetToSend.data
            data += bytes([crc.crc8(data)])
            while True:
                self.logger.debug("SENDING FINAL PACKET: %s" % packetToSend)
                received = self.rileyLink.send_and_receive_packet(data, 0, 20, 1000, 2, 40)
                if received is None:
                    received = self.rileyLink.get_packet(2.5)
                    if received is None:
                        self.logger.debug("Silence has fallen")
                        break
                p = self._get_packet(received)
                if p is None:
                    self.logger.debug("Received illegal packet")
                    continue
                if p.address != packetToSend.address:
                    self.logger.debug("Received packet for a different address")
                    continue
                if self.last_packet_received is not None:
                    if p.type == self.last_packet_received.type and \
                            p.sequence == self.last_packet_received.sequence:
                        self.logger.debug("Received previous response")
                        continue
                self.logger.warning("Resynchronization requested")
                self.packetSequence = (self.packetSequence + 1) % 32
                self.messageSequence = 0
                raise TransmissionOutOfSyncError()

            self.packetSequence = (self.packetSequence + 1) % 32
            self.logger.debug("SEND FINAL complete")
        except RileyLinkError as rle:
            raise ProtocolError("Radio error during sending") from rle
Beispiel #5
0
 def _get_packet(data):
     p = None
     if data is not None and len(data) > 2:
         calc = crc.crc8(data[2:-1])
         if data[-1] == calc:
             try:
                 p = Packet.from_data(data[2:-1])
                 getLogger().debug("RECEIVED PACKET: %s" % p)
             except ProtocolError as pe:
                 getLogger().warning("Crc match on an invalid packet, error: %s" % pe)
     return p
Beispiel #6
0
    def _exchange_packets(self, packet_to_send, expected_type):
        send_retries = 30
        while send_retries > 0:
            try:
                packet_to_send.setSequence(self.packetSequence)
                expected_sequence = (self.packetSequence + 1) % 32
                expected_address = packet_to_send.address
                self.logger.debug("SENDING PACKET EXP RESPONSE: %s" % packet_to_send)
                data = packet_to_send.data
                data += bytes([crc.crc8(data)])

                if packet_to_send.type == "PDM":
                    send_retries -= 1
                    received = self.packetRadio.send_and_receive_packet(data, 0, 0, 100, 1, 130)
                else:
                    received = self.packetRadio.send_and_receive_packet(data, 0, 0, 100, 10, 20)

                if received is None:
                    self.logger.debug("Received nothing")
                    self.packetRadio.tx_up()
                    continue
                p, rssi = self._get_packet(received)
                if p is None:
                    self.logger.debug("Received bad packet")
                    self.packetRadio.tx_down()
                    continue
                if p.address != expected_address and p.address2 != packet_to_send.address2:
                    self.logger.debug("Received packet for a different radio_address")
                    self.packetRadio.tx_down()
                    continue

                if p.type != expected_type or p.sequence != expected_sequence:
                    if self.last_packet_received is not None:
                        if p.type == self.last_packet_received.type and \
                                p.sequence == self.last_packet_received.sequence:
                            self.logger.debug("Received previous response")
                            self.packetRadio.tx_up()
                            continue

                    self.logger.debug("Received unexpected packet")
                    self.packetSequence = (p.sequence + 1) % 32
                    continue

                self.packetSequence = (self.packetSequence + 2) % 32
                self.last_packet_received = p
                self.logger.debug("SEND AND RECEIVE complete")
                return p
            except PacketRadioError:
                self.logger.exception("Radio error during send and receive")
                self.packetRadio.disconnect()
        else:
            raise ProtocolError("Exceeded retry count while send and receive")
Beispiel #7
0
 def _get_packet(data):
     p = None
     rssi = None
     if data is not None and len(data) > 2:
         rssi = data[0]
         calc = crc.crc8(data[2:-1])
         if data[-1] == calc:
             try:
                 p = Packet.from_data(data[2:-1])
                 getLogger().debug("RECEIVED PACKET: %s RSSI: %d" % (p, rssi))
             except ProtocolError as pe:
                 getLogger().warning("Crc match on an invalid packet, error: %s" % pe)
     return p, rssi
Beispiel #8
0
    def _exchange_packets(self, packet_to_send, expected_type):
        packet_to_send.setSequence(self.packetSequence)
        expected_sequence = (self.packetSequence + 1) % 32
        expected_address = packet_to_send.address
        send_retries = 3
        while send_retries > 0:
            try:
                self.logger.debug("SENDING PACKET EXP RESPONSE: %s" % packet_to_send)
                data = packet_to_send.data
                data += bytes([crc.crc8(data)])

                if packet_to_send.type == "PDM":
                    send_retries -= 1
                    received = self.rileyLink.send_and_receive_packet(data, 0, 300, 300, 10, 80)
                else:
                    received = self.rileyLink.send_and_receive_packet(data, 0, 20, 300, 10, 20)

                if received is None:
                    self.logger.debug("Received nothing")
                    continue
                p = self._get_packet(received)
                if p is None:
                    self.logger.debug("Received illegal packet")
                    continue
                if p.address != expected_address:
                    self.logger.debug("Received packet for a different address")
                    continue

                if p.type != expected_type or p.sequence != expected_sequence:
                    if self.last_packet_received is not None:
                        if p.type == self.last_packet_received.type and \
                                p.sequence == self.last_packet_received.sequence:
                            self.logger.debug("Received previous response")
                            continue

                    self.logger.debug("Resynchronization requested")
                    self.packetSequence = (p.sequence + 1) % 32
                    self.messageSequence = 0
                    raise TransmissionOutOfSyncError()

                self.packetSequence = (self.packetSequence + 2) % 32
                self.last_packet_received = p
                self.logger.debug("SEND AND RECEIVE complete")
                return p
            except RileyLinkError as rle:
                raise ProtocolError("Radio error during send and receive") from rle
        else:
            raise ProtocolError("Exceeded retry count while send and receive")
Beispiel #9
0
    def parse(data):
        if len(data) < 5:
            #raise ProtocolError("Packet length too small")
            return None

        crc = data[-1]
        crc_computed = crc8(data[:-1])
        if crc != crc_computed:
            #raise ProtocolError("Packet crc error")
            return None

        address = struct.unpack(">I", data[0:4])[0]

        type = RadioPacketType(data[4] & 0b11100000)
        sequence = data[4] & 0b00011111

        body = data[5:-1]
        return RadioPacket(address, type, sequence, body)
Beispiel #10
0
    def _send_packet(self, packetToSend):
        self.send_final_complete.clear()
        while True:
            try:
                packetToSend.setSequence(self.packetSequence)
                data = packetToSend.data
                data += bytes([crc.crc8(data)])

                self.logger.debug("SENDING FINAL PACKET: %s" % packetToSend)
                received = self.packetRadio.send_and_receive_packet(data, 3, 100, 100, 3, 20)
                if self.request_arrived.wait(timeout=0):
                    self.logger.debug("Prematurely exiting final phase to process next request")
                    self.packetSequence = (self.packetSequence + 2) % 32
                    return
                if received is None:
                    received = self.packetRadio.get_packet(1.0)
                    if received is None:
                        self.logger.debug("Silence has fallen")
                        break
                p, rssi = self._get_packet(received)
                if p is None:
                    self.logger.debug("Received bad packet")
                    self.packetRadio.tx_down()
                    continue
                if p.address != packetToSend.address and p.address2 != packetToSend.address2:
                    self.logger.debug("Received packet for a different radio_address")
                    self.packetRadio.tx_down()
                    continue
                if self.last_packet_received is not None:
                    if p.type == self.last_packet_received.type and \
                            p.sequence == self.last_packet_received.sequence:
                        self.logger.debug("Received previous response")
                        self.packetRadio.tx_up()
                        continue
                self.logger.debug("Received unexpected packet")
                self.packetSequence = (p.sequence + 1) % 32
                continue

            except PacketRadioError:
                self.logger.exception("Radio error during sending")
                self.packetRadio.disconnect()
        self.packetSequence = (self.packetSequence + 1) % 32
        self.logger.debug("SEND FINAL complete")
        self.send_final_complete.set()
Beispiel #11
0
    def __sendPacketAndGetPacketResponse(self,
                                         packetToSend,
                                         expectedType,
                                         trackSequencing=True):
        expectedAddress = packetToSend.address
        while True:
            if trackSequencing:
                packetToSend.setSequence(self.packetSequence)
            logging.debug("SENDING PACKET expecting response: %s" %
                          packetToSend)
            data = packetToSend.data
            data += bytes([crc.crc8(data)])
            received = self.rileyLink.send_and_receive_packet(
                data, 0, 0, 300, 30, 127)
            p = self.__getPacket(received)
            if p is not None and p.address == expectedAddress:
                logging.debug("RECEIVED PACKET: %s" % p)
                packet_accepted = False
                if expectedType is None:
                    if self.lastPacketReceived is None:
                        packet_accepted = True
                    else:
                        if self.lastPacketReceived.data != p.data:
                            packet_accepted = True
                else:
                    if p.type == expectedType:
                        packet_accepted = True

                if packet_accepted:
                    logging.debug("received packet accepted. %s" % p)
                    if trackSequencing:
                        self.packetSequence = (p.sequence + 1) % 32
                    self.lastPacketReceived = p
                    return p
                else:
                    logging.debug(
                        "received packet does not match expected criteria. %s"
                        % p)
                    if trackSequencing:
                        self.packetSequence = (p.sequence + 1) % 32
Beispiel #12
0
pod.lot = int(sys.argv[1])
pod.tid = int(sys.argv[2])
pod.lastNonce = None

r = RileyLink("88:6b:0f:44:fc:1b")
print("connecting to RL")
r.connect()
print("initializing")
r.init_radio()
print("ready to listen for pdm")

p = None
while True:
    data = r.get_packet(30000)
    if data is not None and len(data) > 2:
        calc = crc8(data[2:-1])
        if data[-1] == calc:
            p = Packet(0, data[2:-1])
            break

r.disconnect()
print("disconnected")

print("Setting address as 0x%08x" % p.address)
pod.address = p.address
pod.Save(sys.argv[3])

print(
    "Now put the pdm away, wait until it's not communicating with the pod or shut it off"
)
input("press enter to continue")
Beispiel #13
0
 def __getPacket(self, data):
     if data is not None and len(data) > 2:
         calc = crc.crc8(data[2:-1])
         if data[-1] == calc:
             return Packet(0, data[2:-1])
     return None
Beispiel #14
0
 def get_data(self):
     data = struct.pack(">I", self.address)
     data += bytes([self.type | self.sequence ])
     data += self.body
     data +=  bytes([crc8(data)])
     return data