| 
									
										
										
										
											2017-06-05 16:02:39 +03:00
										 |  |  | # Copyright (c) 2014-present PlatformIO <contact@platformio.org> | 
					
						
							| 
									
										
										
										
											2015-11-18 17:16:17 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #    http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							| 
									
										
										
										
											2014-07-27 22:29:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-09 23:13:29 +03:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2019-05-25 22:14:38 +03:00
										 |  |  | from fnmatch import fnmatch | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  | from os import getcwd | 
					
						
							| 
									
										
										
										
											2014-08-09 23:13:29 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 20:16:50 +02:00
										 |  |  | import click | 
					
						
							| 
									
										
										
										
											2014-08-09 23:13:29 +03:00
										 |  |  | from serial.tools import miniterm | 
					
						
							| 
									
										
										
										
											2014-07-27 22:29:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  | from platformio import exception, fs, util | 
					
						
							| 
									
										
										
										
											2019-06-05 17:57:22 +03:00
										 |  |  | from platformio.compat import dump_json_to_unicode | 
					
						
							| 
									
										
										
										
											2019-05-07 17:51:50 +03:00
										 |  |  | from platformio.project.config import ProjectConfig | 
					
						
							| 
									
										
										
										
											2014-07-27 22:29:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 14:25:50 +03:00
										 |  |  | @click.group(short_help="Monitor device or list existing") | 
					
						
							| 
									
										
										
										
											2014-07-27 22:29:32 +03:00
										 |  |  | def cli(): | 
					
						
							| 
									
										
										
										
											2014-08-09 23:13:29 +03:00
										 |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 14:25:50 +03:00
										 |  |  | @cli.command("list", short_help="List devices") | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  | @click.option("--serial", is_flag=True, help="List serial ports, default") | 
					
						
							|  |  |  | @click.option("--logical", is_flag=True, help="List logical devices") | 
					
						
							|  |  |  | @click.option("--mdns", is_flag=True, help="List multicast DNS services") | 
					
						
							| 
									
										
										
										
											2015-01-02 21:03:14 +02:00
										 |  |  | @click.option("--json-output", is_flag=True) | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  | def device_list(  # pylint: disable=too-many-branches | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     serial, logical, mdns, json_output | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |     if not logical and not mdns: | 
					
						
							|  |  |  |         serial = True | 
					
						
							|  |  |  |     data = {} | 
					
						
							|  |  |  |     if serial: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         data["serial"] = util.get_serial_ports() | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |     if logical: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         data["logical"] = util.get_logical_devices() | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |     if mdns: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         data["mdns"] = util.get_mdns_services() | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-26 20:54:29 +02:00
										 |  |  |     single_key = list(data)[0] if len(list(data)) == 1 else None | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-02 21:03:14 +02:00
										 |  |  |     if json_output: | 
					
						
							| 
									
										
										
										
											2019-06-05 17:57:22 +03:00
										 |  |  |         return click.echo( | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             dump_json_to_unicode(data[single_key] if single_key else data) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     titles = { | 
					
						
							|  |  |  |         "serial": "Serial Ports", | 
					
						
							|  |  |  |         "logical": "Logical Devices", | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         "mdns": "Multicast DNS Services", | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-27 20:51:55 +03:00
										 |  |  |     for key, value in data.items(): | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |         if not single_key: | 
					
						
							|  |  |  |             click.secho(titles[key], bold=True) | 
					
						
							|  |  |  |             click.echo("=" * len(titles[key])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if key == "serial": | 
					
						
							|  |  |  |             for item in value: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |                 click.secho(item["port"], fg="cyan") | 
					
						
							|  |  |  |                 click.echo("-" * len(item["port"])) | 
					
						
							|  |  |  |                 click.echo("Hardware ID: %s" % item["hwid"]) | 
					
						
							|  |  |  |                 click.echo("Description: %s" % item["description"]) | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |                 click.echo("") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if key == "logical": | 
					
						
							|  |  |  |             for item in value: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |                 click.secho(item["path"], fg="cyan") | 
					
						
							|  |  |  |                 click.echo("-" * len(item["path"])) | 
					
						
							|  |  |  |                 click.echo("Name: %s" % item["name"]) | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |                 click.echo("") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if key == "mdns": | 
					
						
							|  |  |  |             for item in value: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |                 click.secho(item["name"], fg="cyan") | 
					
						
							|  |  |  |                 click.echo("-" * len(item["name"])) | 
					
						
							|  |  |  |                 click.echo("Type: %s" % item["type"]) | 
					
						
							|  |  |  |                 click.echo("IP: %s" % item["ip"]) | 
					
						
							|  |  |  |                 click.echo("Port: %s" % item["port"]) | 
					
						
							|  |  |  |                 if item["properties"]: | 
					
						
							|  |  |  |                     click.echo( | 
					
						
							|  |  |  |                         "Properties: %s" | 
					
						
							|  |  |  |                         % ( | 
					
						
							|  |  |  |                             "; ".join( | 
					
						
							|  |  |  |                                 [ | 
					
						
							|  |  |  |                                     "%s=%s" % (k, v) | 
					
						
							|  |  |  |                                     for k, v in item["properties"].items() | 
					
						
							|  |  |  |                                 ] | 
					
						
							|  |  |  |                             ) | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     ) | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |                 click.echo("") | 
					
						
							| 
									
										
										
										
											2014-07-27 22:29:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |         if single_key: | 
					
						
							|  |  |  |             click.echo("") | 
					
						
							| 
									
										
										
										
											2014-08-09 23:13:29 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 22:16:37 +02:00
										 |  |  |     return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-09 23:13:29 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-31 18:13:41 +02:00
										 |  |  | @cli.command("monitor", short_help="Monitor device (Serial)") | 
					
						
							|  |  |  | @click.option("--port", "-p", help="Port, a number or a device name") | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  | @click.option("--baud", "-b", type=int, help="Set baud rate, default=9600") | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | @click.option( | 
					
						
							|  |  |  |     "--parity", | 
					
						
							|  |  |  |     default="N", | 
					
						
							|  |  |  |     type=click.Choice(["N", "E", "O", "S", "M"]), | 
					
						
							|  |  |  |     help="Set parity, default=N", | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option("--rtscts", is_flag=True, help="Enable RTS/CTS flow control, default=Off") | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--xonxoff", is_flag=True, help="Enable software flow control, default=Off" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--rts", default=None, type=click.IntRange(0, 1), help="Set initial RTS line state" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--dtr", default=None, type=click.IntRange(0, 1), help="Set initial DTR line state" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-10-31 18:13:41 +02:00
										 |  |  | @click.option("--echo", is_flag=True, help="Enable local echo, default=Off") | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | @click.option( | 
					
						
							|  |  |  |     "--encoding", | 
					
						
							|  |  |  |     default="UTF-8", | 
					
						
							|  |  |  |     help="Set the encoding for the serial port (e.g. hexlify, " | 
					
						
							|  |  |  |     "Latin1, UTF-8), default: UTF-8", | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-10-31 18:13:41 +02:00
										 |  |  | @click.option("--filter", "-f", multiple=True, help="Add text transformation") | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | @click.option( | 
					
						
							|  |  |  |     "--eol", | 
					
						
							|  |  |  |     default="CRLF", | 
					
						
							|  |  |  |     type=click.Choice(["CR", "LF", "CRLF"]), | 
					
						
							|  |  |  |     help="End of line mode, default=CRLF", | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option("--raw", is_flag=True, help="Do not apply any encodings/transformations") | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--exit-char", | 
					
						
							|  |  |  |     type=int, | 
					
						
							|  |  |  |     default=3, | 
					
						
							|  |  |  |     help="ASCII code of special character that is used to exit " | 
					
						
							|  |  |  |     "the application, default=3 (Ctrl+C)", | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--menu-char", | 
					
						
							|  |  |  |     type=int, | 
					
						
							|  |  |  |     default=20, | 
					
						
							|  |  |  |     help="ASCII code of special character that is used to " | 
					
						
							|  |  |  |     "control miniterm (menu), default=20 (DEC)", | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "--quiet", | 
					
						
							|  |  |  |     is_flag=True, | 
					
						
							|  |  |  |     help="Diagnostics: suppress non-error messages, default=Off", | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | @click.option( | 
					
						
							|  |  |  |     "-d", | 
					
						
							|  |  |  |     "--project-dir", | 
					
						
							|  |  |  |     default=getcwd, | 
					
						
							|  |  |  |     type=click.Path(exists=True, file_okay=False, dir_okay=True, resolve_path=True), | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  | @click.option( | 
					
						
							|  |  |  |     "-e", | 
					
						
							|  |  |  |     "--environment", | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     help="Load configuration from `platformio.ini` and specified environment", | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  | def device_monitor(**kwargs):  # pylint: disable=too-many-branches | 
					
						
							| 
									
										
										
										
											2019-11-16 17:45:57 +02:00
										 |  |  |     click.echo( | 
					
						
							|  |  |  |         "Looking for advanced Serial Monitor with UI? " | 
					
						
							|  |  |  |         "Check http://bit.ly/pio-advanced-monitor" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-11-11 22:38:16 +02:00
										 |  |  |     project_options = {} | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |         with fs.cd(kwargs["project_dir"]): | 
					
						
							| 
									
										
										
										
											2019-11-11 22:38:16 +02:00
										 |  |  |             project_options = get_project_options(kwargs["environment"]) | 
					
						
							|  |  |  |         kwargs = apply_project_monitor_options(kwargs, project_options) | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  |     except exception.NotPlatformIOProject: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     if not kwargs["port"]: | 
					
						
							| 
									
										
										
										
											2017-12-18 21:31:49 +02:00
										 |  |  |         ports = util.get_serial_ports(filter_hwid=True) | 
					
						
							| 
									
										
										
										
											2016-10-31 20:06:48 +02:00
										 |  |  |         if len(ports) == 1: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             kwargs["port"] = ports[0]["port"] | 
					
						
							| 
									
										
										
										
											2019-11-11 22:38:16 +02:00
										 |  |  |     elif kwargs["port"] and (set(["*", "?", "[", "]"]) & set(kwargs["port"])): | 
					
						
							| 
									
										
										
										
											2019-05-25 22:14:38 +03:00
										 |  |  |         for item in util.get_serial_ports(): | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             if fnmatch(item["port"], kwargs["port"]): | 
					
						
							|  |  |  |                 kwargs["port"] = item["port"] | 
					
						
							| 
									
										
										
										
											2019-05-25 22:14:38 +03:00
										 |  |  |                 break | 
					
						
							| 
									
										
										
										
											2019-05-25 21:49:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 22:38:16 +02:00
										 |  |  |     # override system argv with patched options | 
					
						
							|  |  |  |     sys.argv = ["monitor"] + options_to_argv( | 
					
						
							|  |  |  |         kwargs, | 
					
						
							|  |  |  |         project_options, | 
					
						
							|  |  |  |         ignore=("port", "baud", "rts", "dtr", "environment", "project_dir"), | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-31 18:13:41 +02:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         miniterm.main( | 
					
						
							|  |  |  |             default_port=kwargs["port"], | 
					
						
							|  |  |  |             default_baudrate=kwargs["baud"] or 9600, | 
					
						
							|  |  |  |             default_rts=kwargs["rts"], | 
					
						
							|  |  |  |             default_dtr=kwargs["dtr"], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-10-31 20:06:48 +02:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2017-06-07 02:32:25 +03:00
										 |  |  |         raise exception.MinitermException(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 22:38:16 +02:00
										 |  |  | def apply_project_monitor_options(cli_options, project_options): | 
					
						
							|  |  |  |     for k in ("port", "speed", "rts", "dtr"): | 
					
						
							|  |  |  |         k2 = "monitor_%s" % k | 
					
						
							|  |  |  |         if k == "speed": | 
					
						
							|  |  |  |             k = "baud" | 
					
						
							|  |  |  |         if cli_options[k] is None and k2 in project_options: | 
					
						
							|  |  |  |             cli_options[k] = project_options[k2] | 
					
						
							|  |  |  |             if k != "port": | 
					
						
							|  |  |  |                 cli_options[k] = int(cli_options[k]) | 
					
						
							|  |  |  |     return cli_options | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def options_to_argv(cli_options, project_options, ignore=None): | 
					
						
							|  |  |  |     result = project_options.get("monitor_flags", []) | 
					
						
							|  |  |  |     for k, v in cli_options.items(): | 
					
						
							|  |  |  |         if v is None or (ignore and k in ignore): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         k = "--" + k.replace("_", "-") | 
					
						
							|  |  |  |         if k in project_options.get("monitor_flags", []): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if isinstance(v, bool): | 
					
						
							|  |  |  |             if v: | 
					
						
							|  |  |  |                 result.append(k) | 
					
						
							|  |  |  |         elif isinstance(v, tuple): | 
					
						
							|  |  |  |             for i in v: | 
					
						
							|  |  |  |                 result.extend([k, i]) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             result.extend([k, str(v)]) | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  | def get_project_options(environment=None): | 
					
						
							|  |  |  |     config = ProjectConfig.get_instance() | 
					
						
							| 
									
										
										
										
											2019-05-07 17:51:50 +03:00
										 |  |  |     config.validate(envs=[environment] if environment else None) | 
					
						
							|  |  |  |     if not environment: | 
					
						
							|  |  |  |         default_envs = config.default_envs() | 
					
						
							|  |  |  |         if default_envs: | 
					
						
							|  |  |  |             environment = default_envs[0] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             environment = config.envs()[0] | 
					
						
							| 
									
										
										
										
											2019-05-07 22:13:21 +03:00
										 |  |  |     return config.items(env=environment, as_dict=True) |