def get_hdlc_rpc_client(device: str, baudrate: int, output: Any, **kwargs): """Get the HdlcRpcClient based on arguments.""" serial_device = serial.Serial(device, baudrate, timeout=1) def read(): return serial_device.read(8192) write = serial_device.write return HdlcRpcClient(read, PROTOS, default_channels(write), lambda data: write_to_file(data, output))
def console(device: str, baudrate: int, socket_addr: str, output: Any) -> int: """Starts an interactive RPC console for HDLC.""" # argparse.FileType doesn't correctly handle '-' for binary files. if output is sys.stdout: output = sys.stdout.buffer logfile = create_temp_log_file() pw_cli.log.install(logging.INFO, True, False, logfile) if socket_addr is None: serial_device = serial.Serial(device, baudrate, timeout=1) def read(): return serial_device.read(8192) write = serial_device.write else: try: socket_device = SocketClientImpl(socket_addr) read = socket_device.read write = socket_device.write except ValueError: _LOG.exception('Failed to initialize socket at %s', socket_addr) return 1 _start_ipython_terminal( HdlcRpcClient(read, PROTOS, default_channels(write), lambda data: write_to_output(data, output))) return 0
def __init__( self, read: Callable[[], bytes], write: Callable[[bytes], int], paths_or_modules: Union[Iterable["python_protos.PathOrModule"], "python_protos.Library"]): """Creates an RPC client configured to communicate using HDLC. Args: read: Function that reads bytes; e.g serial_device.read. write: Function that writes bytes; e.g serial_device.write. paths_or_modules: Paths to .proto files or proto modules. """ if not PIGWEED_IMPORT: raise errors.DependencyUnavailableError( "Pigweed python packages are not available in this environment." ) if isinstance(paths_or_modules, python_protos.Library): self.protos = paths_or_modules else: self.protos = python_protos.Library.from_paths(paths_or_modules) client_impl = callback_client.Impl() channels = rpc.default_channels(write) self.client = pw_rpc.Client.from_modules(client_impl, channels, self.protos.modules()) self.frame_handlers = { _DEFAULT_ADDRESS: self._handle_rpc_packet, _STDOUT_ADDRESS: lambda frame: rpc.write_to_file(frame.data) } self.read = read self._stop_event = threading.Event() self._worker = None
def get_hdlc_rpc_client(device: str, baudrate: int, proto_globs: Collection[str], socket_addr: str, **kwargs): """Get the HdlcRpcClient based on arguments.""" del kwargs # ignore if not proto_globs: proto_globs = ['**/*.proto'] protos = list(_expand_globs(proto_globs)) if not protos: _LOG.critical('No .proto files were found with %s', ', '.join(proto_globs)) _LOG.critical('At least one .proto file is required') return 1 _LOG.debug('Found %d .proto files found with %s', len(protos), ', '.join(proto_globs)) # TODO(rgoliver): When pw has a generalized transport for RPC this should # use it so it isn't specific to HDLC if socket_addr is None: serial_device = serial.Serial(device, baudrate, timeout=1) read = lambda: serial_device.read(8192) write = serial_device.write else: try: socket_device = SocketClientImpl(socket_addr) read = socket_device.read write = socket_device.write except ValueError: _LOG.exception('Failed to initialize socket at %s', socket_addr) return 1 return HdlcRpcClient(read, protos, default_channels(write))
def console(device: str, baudrate: int, token_databases: Collection[tokens.Database], socket_addr: str, output: Any, raw_serial: bool) -> int: """Starts an interactive RPC console for HDLC.""" # argparse.FileType doesn't correctly handle '-' for binary files. if output is sys.stdout: output = sys.stdout.buffer logfile = create_temp_log_file() pw_cli.log.install(logging.INFO, True, False, logfile) serial_impl = SerialWithLogging if socket_addr is None: serial_device = serial_impl(device, baudrate, timeout=0) def read(): return serial_device.read(8192) write = serial_device.write else: try: socket_device = SocketClientImpl(socket_addr) read = socket_device.read write = socket_device.write except ValueError: _LOG.exception('Failed to initialize socket at %s', socket_addr) return 1 callback_client_impl = callback_client.Impl( default_unary_timeout_s=5.0, default_stream_timeout_s=None, ) detokenizer = Detokenizer(tokens.Database.merged(*token_databases), show_errors=False) if token_databases else None if raw_serial: threading.Thread( target=_read_raw_serial, daemon=True, args=( read, lambda data: write_to_output(data, output, detokenizer), )).start() _start_ipython_raw_terminal() else: _start_ipython_hdlc_terminal( HdlcRpcClient( read, PROTOS, default_channels(write), lambda data: write_to_output(data, output, detokenizer), client_impl=callback_client_impl)) return 0
def __init__(self, device_tty, baud, platform_module=None, platform_args=None): ser = serial.Serial(device_tty, baud, timeout=0.01) self.pw_rpc_client = HdlcRpcClient(lambda: ser.read(4096), [PROTO], default_channels(ser.write)) self._platform = None print("Platform args: %s" % platform_args) print("Platform module: %s" % platform_module) if platform_module: m = importlib.import_module(platform_module) create_platform_method = getattr(m, "create_platform") self._platform = create_platform_method(platform_args)
def __init__(self, device, protos): """ Pigweed Client class containing RPC client initialization and service functions Create HdlcRpcCLient object and redirect serial communication to it :param device: test device instance :param protos: array of RPC protocols """ self.device = device self.device.stop() self.last_timeout = self.device.serial.get_timeout() self.device.serial.set_timeout(0.01) self._pw_rpc_client = HdlcRpcClient( lambda: self.device.serial.read(4096), protos, default_channels(self.device.serial.write)) self._rpcs = self._pw_rpc_client.rpcs()
def script(device: str, baud: int) -> None: # Set up a pw_rpc client that uses HDLC. ser = serial.Serial(device, baud, timeout=0.01) client = HdlcRpcClient(lambda: ser.read(4096), [PROTO], default_channels(ser.write)) # Make a shortcut to the EchoService. echo_service = client.rpcs().pw.rpc.EchoService # Call some RPCs and check the results. status, payload = echo_service.Echo(msg='Hello') if status.ok(): print('The status was', status) print('The payload was', payload) else: print('Uh oh, this RPC returned', status) status, payload = echo_service.Echo(msg='Goodbye!') print('The device says:', payload.msg)
def console(device: str, baudrate: int, proto_globs: Collection[str], socket_addr: str, output: Any) -> int: """Starts an interactive RPC console for HDLC.""" # argparse.FileType doesn't correctly handle '-' for binary files. if output is sys.stdout: output = sys.stdout.buffer if not proto_globs: proto_globs = ['**/*.proto'] protos = list(_expand_globs(proto_globs)) if not protos: _LOG.critical('No .proto files were found with %s', ', '.join(proto_globs)) _LOG.critical('At least one .proto file is required') return 1 _LOG.debug('Found %d .proto files found with %s', len(protos), ', '.join(proto_globs)) if socket_addr is None: serial_device = serial.Serial(device, baudrate, timeout=1) read = lambda: serial_device.read(8192) write = serial_device.write else: try: socket_device = SocketClientImpl(socket_addr) read = socket_device.read write = socket_device.write except ValueError: _LOG.exception('Failed to initialize socket at %s', socket_addr) return 1 _start_ipython_terminal( HdlcRpcClient(read, protos, default_channels(write), lambda data: write_to_file(data, output))) return 0