Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESP crash trace decoding to monitor #3383

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions platformio/commands/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
from fnmatch import fnmatch
from os import getcwd

import click
from serial.tools import miniterm

from platformio import exception, fs, util
from platformio.compat import dump_json_to_unicode
from platformio.compat import dump_json_to_unicode, load_python_module
from platformio.managers.platform import PlatformFactory
from platformio.project.config import ProjectConfig
from platformio.project.exception import NotPlatformIOProjectError
Expand Down Expand Up @@ -165,7 +165,7 @@ def device_list( # pylint: disable=too-many-branches
@click.option(
"-d",
"--project-dir",
default=getcwd,
default=os.getcwd,
type=click.Path(exists=True, file_okay=False, dir_okay=True, resolve_path=True),
)
@click.option(
Expand All @@ -186,14 +186,20 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches
except NotPlatformIOProjectError:
pass

platform = None
if "platform" in project_options:
with fs.cd(kwargs["project_dir"]):
platform = PlatformFactory.newPlatform(project_options["platform"])
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 = get_board_hwids(
kwargs["project_dir"],
project_options["platform"],
platform,
project_options["board"],
)
for item in ports:
Expand Down Expand Up @@ -227,7 +233,6 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches
except Exception as e:
raise exception.MinitermException(e)


def apply_project_monitor_options(cli_options, project_options):
for k in ("port", "speed", "rts", "dtr"):
k2 = "monitor_%s" % k
Expand Down Expand Up @@ -274,7 +279,51 @@ def get_project_options(environment=None):
def get_board_hwids(project_dir, platform, board):
with fs.cd(project_dir):
return (
PlatformFactory.newPlatform(platform)
platform
.board_config(board)
.get("build.hwids", [])
)

class DeviceMonitorFilter(miniterm.Transform):
# NAME = "esp_exception_decoder" - all filters must have one

# Called by PlatformIO to pass context
def __init__(self, project_dir, environment):
super(DeviceMonitorFilter, self).__init__()

self.project_dir = project_dir
self.environment = environment

self.config = ProjectConfig.get_instance()
if not self.environment:
default_envs = self.config.default_envs()
if default_envs:
self.environment = default_envs[0]
else:
self.environment = self.config.envs()[0]

# Called by the miniterm library when the filter is acutally used
def __call__(self):
return self

def register_platform_filters(platform, project_dir, environment):
monitor_dir = os.path.join(platform.get_dir(), "monitor")
if not os.path.isdir(monitor_dir):
return

for fn in os.listdir(monitor_dir):
if not fn.startswith("filter_") or not fn.endswith(".py"):
continue
path = os.path.join(monitor_dir, fn)
if not os.path.isfile(path):
continue

dot = fn.find(".")
module = load_python_module("platformio.commands.device.%s" % fn[:dot], path)
for key in dir(module):
member = getattr(module, key)
try:
if issubclass(member, DeviceMonitorFilter) and hasattr(member, "NAME"):
miniterm.TRANSFORMATIONS[member.NAME] = member(project_dir, environment)
except TypeError:
pass
2 changes: 1 addition & 1 deletion platformio/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def is_bytes(x):
def path_to_unicode(path):
Copy link
Contributor Author

@Tasssadar Tasssadar Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns unicode if it already is unicode, but string if it isn't? Seemed like a bug to me, and it doesn't seem to be used anywhere yet, so I changed it to always return unicode (I needed it in the filter in plaftorm-espressif32).

if isinstance(path, unicode):
return path
return path.decode(get_filesystem_encoding()).encode("utf-8")
return path.decode(get_filesystem_encoding())

def hashlib_encode_data(data):
if is_bytes(data):
Expand Down