示例#1
0
def device_monitor(ctx, **kwargs):
    project_options = {}
    try:
        with fs.cd(kwargs["project_dir"]):
            project_options = device_helpers.get_project_options(kwargs["environment"])
        kwargs = device_helpers.apply_project_monitor_options(kwargs, project_options)
    except NotPlatformIOProjectError:
        pass

    kwargs["baud"] = kwargs["baud"] or 9600

    def _tx_target(sock_dir):
        pioplus_argv = ["remote", "device", "monitor"]
        pioplus_argv.extend(device_helpers.options_to_argv(kwargs, project_options))
        pioplus_argv.extend(["--sock", sock_dir])
        try:
            pioplus_call(pioplus_argv)
        except exception.ReturnErrorCode:
            pass

    sock_dir = mkdtemp(suffix="pioplus")
    sock_file = os.path.join(sock_dir, "sock")
    try:
        t = threading.Thread(target=_tx_target, args=(sock_dir,))
        t.start()
        while t.is_alive() and not os.path.isfile(sock_file):
            sleep(0.1)
        if not t.is_alive():
            return
        with open(sock_file) as fp:
            kwargs["port"] = fp.read()
        ctx.invoke(cmd_device_monitor, **kwargs)
        t.join(2)
    finally:
        fs.rmtree(sock_dir)
示例#2
0
def device_monitor(ctx, agents, **kwargs):
    from platformio.commands.remote.client.device_monitor import DeviceMonitorClient

    if kwargs["sock"]:
        return DeviceMonitorClient(agents, **kwargs).connect()

    project_options = {}
    try:
        with fs.cd(kwargs["project_dir"]):
            project_options = device_helpers.get_project_options(
                kwargs["environment"])
        kwargs = device_helpers.apply_project_monitor_options(
            kwargs, project_options)
    except NotPlatformIOProjectError:
        pass

    kwargs["baud"] = kwargs["baud"] or 9600

    def _tx_target(sock_dir):
        subcmd_argv = ["remote", "device", "monitor"]
        subcmd_argv.extend(
            device_helpers.options_to_argv(kwargs, project_options))
        subcmd_argv.extend(["--sock", sock_dir])
        subprocess.call([proc.where_is_program("platformio")] + subcmd_argv)

    sock_dir = mkdtemp(suffix="pio")
    sock_file = os.path.join(sock_dir, "sock")
    try:
        t = threading.Thread(target=_tx_target, args=(sock_dir, ))
        t.start()
        while t.is_alive() and not os.path.isfile(sock_file):
            sleep(0.1)
        if not t.is_alive():
            return
        with open(sock_file) as fp:
            kwargs["port"] = fp.read()
        ctx.invoke(cmd_device_monitor, **kwargs)
        t.join(2)
    finally:
        fs.rmtree(sock_dir)

    return True
示例#3
0
def device_monitor(**kwargs):  # pylint: disable=too-many-branches
    # load default monitor filters
    filters_dir = os.path.join(fs.get_source_dir(), "commands", "device", "filters")
    for name in os.listdir(filters_dir):
        if not name.endswith(".py"):
            continue
        device_helpers.load_monitor_filter(os.path.join(filters_dir, name))

    project_options = {}
    try:
        with fs.cd(kwargs["project_dir"]):
            project_options = device_helpers.get_project_options(kwargs["environment"])
        kwargs = device_helpers.apply_project_monitor_options(kwargs, project_options)
    except NotPlatformIOProjectError:
        pass

    platform = None
    if "platform" in project_options:
        with fs.cd(kwargs["project_dir"]):
            platform = PlatformFactory.newPlatform(project_options["platform"])
            device_helpers.register_platform_filters(
                platform, kwargs["project_dir"], kwargs["environment"]
            )

    if not kwargs["port"]:
        ports = util.get_serial_ports(filter_hwid=True)
        if len(ports) == 1:
            kwargs["port"] = ports[0]["port"]
        elif "platform" in project_options and "board" in project_options:
            board_hwids = device_helpers.get_board_hwids(
                kwargs["project_dir"], platform, project_options["board"],
            )
            for item in ports:
                for hwid in board_hwids:
                    hwid_str = ("%s:%s" % (hwid[0], hwid[1])).replace("0x", "")
                    if hwid_str in item["hwid"]:
                        kwargs["port"] = item["port"]
                        break
                if kwargs["port"]:
                    break
    elif kwargs["port"] and (set(["*", "?", "[", "]"]) & set(kwargs["port"])):
        for item in util.get_serial_ports():
            if fnmatch(item["port"], kwargs["port"]):
                kwargs["port"] = item["port"]
                break

    # override system argv with patched options
    sys.argv = ["monitor"] + device_helpers.options_to_argv(
        kwargs,
        project_options,
        ignore=("port", "baud", "rts", "dtr", "environment", "project_dir"),
    )

    if not kwargs["quiet"]:
        click.echo(
            "--- Available filters and text transformations: %s"
            % ", ".join(sorted(miniterm.TRANSFORMATIONS.keys()))
        )
        click.echo("--- More details at http://bit.ly/pio-monitor-filters")
    try:
        miniterm.main(
            default_port=kwargs["port"],
            default_baudrate=kwargs["baud"] or 9600,
            default_rts=kwargs["rts"],
            default_dtr=kwargs["dtr"],
        )
    except Exception as e:
        raise exception.MinitermException(e)