def write(self, out_binary): """ Write to the MockStream, triggering a lookup for mock response. """ if CONFIG.DEBUG_STREAM_TX: logging.debug("TX Raw: (%d) %s", len(out_binary), util.hexify_bytes(out_binary)) out_hex = binascii.hexlify(out_binary) in_hex = self.vector[out_hex] self.rx_queue.put_nowait(binascii.unhexlify(in_hex))
def transact(self, command_id, payload="", tid=SPINEL.HEADER_DEFAULT): pkt = self.encode_packet(command_id, payload, tid) if CONFIG.DEBUG_LOG_SERIAL: msg = "TX Pay: (%i) %s " % (len(pkt), util.hexify_bytes(pkt)) logging.debug(msg) if self.use_hdlc: pkt = self.hdlc.encode(pkt) self.stream_tx(pkt)
def read(self, size=None): """ Blocking read from the MockStream. """ if not self.response or len(self.response) == 0: self.response = self.rx_queue.get(True) if size: in_binary = self.response[:size] self.response = self.response[size:] else: in_binary = self.response self.response = None if CONFIG.DEBUG_STREAM_RX: logging.debug("RX Raw: " + util.hexify_bytes(in_binary)) return in_binary
def encode(self, payload=""): """ Return the HDLC encoding of the given packet. """ fcs = HDLC_FCS_INIT packet = [] packet.append(HDLC_FLAG) for byte in payload: byte = ord(byte) fcs = self.fcs16(byte, fcs) packet = self.encode_byte(byte, packet) fcs ^= 0xffff byte = fcs & 0xFF packet = self.encode_byte(byte, packet) byte = fcs >> 8 packet = self.encode_byte(byte, packet) packet.append(HDLC_FLAG) packet = pack("%dB" % len(packet), *packet) if CONFIG.DEBUG_HDLC: logging.debug("TX Hdlc: " + hexify_bytes(packet)) return packet