def decodeFromSLIP(self, timeout=None): dataBuffer = [] startOfPacket = False endOfPacket = False slipSkipped = -1 while not startOfPacket: startOfPacket = (self.getSerialByte(timeout) == SLIP_START) slipSkipped += 1 if slipSkipped > 0: raise Exceptions.UARTPacketError("SLIP skipped " + str(slipSkipped) + " bytes") while not endOfPacket: serialByte = self.getSerialByte(timeout) if serialByte == SLIP_END: endOfPacket = True elif serialByte == SLIP_ESC: serialByte = self.getSerialByte() if serialByte == SLIP_ESC_START: dataBuffer.append(SLIP_START) elif serialByte == SLIP_ESC_END: dataBuffer.append(SLIP_END) elif serialByte == SLIP_ESC_ESC: dataBuffer.append(SLIP_ESC) else: raise Exceptions.UARTPacketError( "Unexpected character after SLIP_ESC: %d." % serialByte) else: dataBuffer.append(serialByte) return dataBuffer
def sendHopSequence(self, hopSequence): for chan in hopSequence: if chan not in VALID_ADV_CHANS: raise Exceptions.InvalidAdvChannel("%s is not an adv channel" % str(chan)) payload = [len(hopSequence) ] + hopSequence + [37] * (3 - len(hopSequence)) self.sendPacket(SET_ADV_CHANNEL_HOP_SEQ, payload) self.notify("NEW_ADV_HOP_SEQ", {"hopSequence": hopSequence})
def readPayload(self, packetList): self.blePacket = None self.OK = False if not self.validatePacketList(packetList): raise Exceptions.InvalidPacketException( "packet list not valid: %s" % str(packetList)) else: self.valid = True self.payload = packetList[PAYLOAD_POS:PAYLOAD_POS + self.payloadLength] if self.id == EVENT_PACKET: try: self.bleHeaderLength = packetList[BLE_HEADER_LEN_POS] if self.bleHeaderLength == BLE_HEADER_LENGTH: self.flags = packetList[FLAGS_POS] self.readFlags() self.channel = packetList[CHANNEL_POS] self.rawRSSI = packetList[RSSI_POS] self.RSSI = -self.rawRSSI self.txAdd = packetList[TXADD_POS] & TXADD_MSK self.eventCounter = parseLittleEndian( packetList[EVENTCOUNTER_POS:EVENTCOUNTER_POS + 2]) self.timestamp = parseLittleEndian( packetList[TIMESTAMP_POS:TIMESTAMP_POS + 2]) # self.payload = packetList[13:(4+self.length)] # The hardware adds a padding byte which isn't sent on air. The following removes it. self.packetList.pop(BLEPACKET_POS + 6) self.payloadLength -= 1 if packetList[PAYLOAD_LEN_POS] > 0: packetList[PAYLOAD_LEN_POS] -= 1 if self.OK: try: self.blePacket = BlePacket(packetList[BLEPACKET_POS:]) except: logging.exception("blePacket error") except: # malformed packet logging.exception("packet error") self.OK = False elif self.id == PING_RESP: self.version = parseLittleEndian( self.packetList[PAYLOAD_POS:PAYLOAD_POS + 2]) elif self.id == SWITCH_BAUD_RATE_RESP or self.id == SWITCH_BAUD_RATE_REQ: self.baud_rate = parseLittleEndian( packetList[PAYLOAD_POS:PAYLOAD_POS + 4]) elif self.id == TEST_RESULT_ID: self.testId = packetList[PAYLOAD_POS] self.testLength = packetList[PAYLOAD_POS + 1] self.testPayload = packetList[PAYLOAD_POS + 2:]
def _continuouslyPipe(self): while not self._exit: try: packet = self._packetReader.getPacket(timeout=2) if packet == None or not packet.valid: packet.boardId = self._boardId self._appendPacket(packet) raise Exceptions.InvalidPacketException("") except Exceptions.SnifferTimeout as e: #logging.info(str(e)) packet = None except (SerialException, ValueError): logging.exception("UART read error") logging.error("Lost contact with sniffer hardware.") self._doExit() except Exceptions.InvalidPacketException: # logging.error("Continuously pipe: Invalid packet, skipping.") pass else: if packet.id == EVENT_PACKET: self._processBLEPacket(packet) elif packet.id == EVENT_FOLLOW: # This packet has no value for the user. pass elif packet.id == EVENT_CONNECT: self._connectEventPacketCounterValue = packet.packetCounter self._inConnection = True self._currentConnectRequest = copy.copy( self._findPacketByPacketCounter( self._connectEventPacketCounterValue - 1) ) # copy it because packets are eventually deleted elif packet.id == EVENT_DISCONNECT: if self._inConnection: self._packetsInLastConnection = packet.packetCounter - self._connectEventPacketCounterValue self._inConnection = False elif packet.id == SWITCH_BAUD_RATE_RESP and self._switchingBaudRate: self._switchingBaudRate = False if (packet.baudRate == self._proposedBaudRate): self._packetReader.switchBaudRate( self._proposedBaudRate) else: self._switchBaudRate(packet.baudRate)
def __init__(self, packetList): try: if packetList == []: raise Exceptions.InvalidPacketException( "packet list not valid: %s" % str(packetList)) self.packetList = packetList self.readStaticHeader(packetList) self.readDynamicHeader(packetList) self.readPayload(packetList) except Exceptions.InvalidPacketException as e: logging.error("Invalid packet: %s" % str(e)) self.OK = False self.valid = False except: logging.exception("packet creation error") logging.info("packetList: " + str(packetList)) self.OK = False self.valid = False
def read(self, length, timeout=None): if timeout != self.ser.timeout: try: self.ser.timeout = timeout except ValueError as e: logging.error("Error setting UART read timeout. Continuing.") try: value = self.ser.read(length) except Exception as e: logging.error("UART Error:" + str(e)) self.close() raise if len(value) != length: raise Exceptions.SnifferTimeout("UART read timeout (" + str(self.ser.timeout) + " seconds).") if self.useByteQueue: self.byteQueue.extend(stringToList(value)) return value
def getSerialByte(self, timeout=None): serialByte = self.uart.readByte(timeout) if len(serialByte) != 1: raise Exceptions.SnifferTimeout("Byte read timed out.") return ord(serialByte)