def _handshake(self): self._write_reg("main_control", "stop", expect_response=False) exp_addr = protocol.get_addr_for_reg("main_control") exp_enc_val = protocol.encode_reg_val("main_control", "stop") exp_reg_val = protocol.UnpackedRegVal(exp_addr, exp_enc_val) exp_packet = protocol.UnpackedRegWriteResponse(exp_reg_val) exp_frame = protocol.insert_packet_into_frame(exp_packet) self._link.recv_until(exp_frame) idn_reg = self._read_reg("product_id") if idn_reg != protocol.EXPECTED_ID: raise ClientError("unexpected product id")
def get_reg_vals_for_config(config): reg_vals = [] for reg in get_regs_for_mode(config.mode): config_attr = reg.config_attr if config_attr is None: continue config_val = getattr(config, config_attr, None) if config_val is not None: enc_val = protocol.encode_reg_val(reg, config_val) rv = protocol.UnpackedRegVal(reg.addr, enc_val) reg_vals.append(rv) return reg_vals
def _write_reg_raw(self, addr, enc_val, expect_response=True): addr = protocol.get_addr_for_reg(addr) rrv = protocol.UnpackedRegVal(addr, enc_val) req = protocol.UnpackedRegWriteRequest(rrv) self._send_packet(req) log.debug("sent reg w req: addr: {:3} val: {}".format(addr, self._fmt_enc_val(enc_val))) if expect_response: res = self._recv_packet() if not isinstance(res, protocol.UnpackedRegWriteResponse): raise ClientError("got unexpected packet (expected reg write response)") if res.reg_val != rrv: raise ClientError("reg write failed") log.debug("recv reg w res: ok")
def test_unpack_stream_data_segment(): rv_addr = ptcl.get_addr_for_reg(test_mode_reg) rv_enc_val = ptcl.encode_reg_val(test_mode_reg, 123) rvs = [ptcl.UnpackedRegVal(rv_addr, rv_enc_val)] buffer = bytearray(b'\x12\x34\x56') unp_stream_data = ptcl.UnpackedStreamData(rvs, buffer) pkd_stream_data_segment = bytearray() pkd_stream_data_segment.append(ptcl.STREAM_BUFFER) pkd_stream_data_segment.extend(b"\x03\x00") pkd_stream_data_segment.extend(buffer) pkd_stream_data_segment.append(ptcl.STREAM_RESULT_INFO) pkd_stream_data_segment.extend(b"\x05\x00") pkd_stream_data_segment.append(rv_addr) pkd_stream_data_segment.extend(rv_enc_val) unpacked = ptcl.unpack_stream_data_segment(pkd_stream_data_segment) assert unpacked == unp_stream_data
import pytest import acconeer_utils.clients.reg.protocol as ptcl test_mode = "envelope" test_mode_val = 2 test_reg = ptcl.REG_LOOKUP[ptcl.NO_MODE]["main_control"] test_mode_reg = ptcl.REG_LOOKUP["envelope"]["running_average_factor"] unp_reg_val = ptcl.UnpackedRegVal(2, b"\x03\x00\x00\x00") unp_reg_read_res = ptcl.UnpackedRegReadResponse(unp_reg_val) pkd_reg_read_res_segment = b"\x02\x03\x00\x00\x00" pkd_reg_read_res_packet = bytearray([ptcl.REG_READ_RESPONSE]) + pkd_reg_read_res_segment pkd_reg_read_res_frame = (bytearray([ptcl.START_MARKER]) + b"\x05\x00" + pkd_reg_read_res_packet + bytearray([ptcl.END_MARKER])) unp_reg_write_req = ptcl.UnpackedRegWriteRequest(unp_reg_val) pkd_reg_write_req_packet = bytearray() pkd_reg_write_req_packet.append(ptcl.REG_WRITE_REQUEST) pkd_reg_write_req_packet.append(unp_reg_write_req.reg_val.addr) pkd_reg_write_req_packet.extend(unp_reg_write_req.reg_val.val) pkd_reg_write_req_frame = bytearray() pkd_reg_write_req_frame.append(ptcl.START_MARKER) pkd_reg_write_req_frame.extend(b"\x05\x00") # len pkd_reg_write_req_frame.extend(pkd_reg_write_req_packet) pkd_reg_write_req_frame.append(ptcl.END_MARKER)