Skip to content
Open
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
10 changes: 6 additions & 4 deletions pyocd/probe/pydapaccess/cmsis_dap_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) 2021 Chris Reed
# Copyright (c) 2022 Toshiba Electronic Devices & Storage Corporation
# Copyright (c) 2025 Norbert Hipfl
# Copyright (c) 2026 Solarboaters
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -171,9 +172,8 @@ def dap_info(self, id_):
in the DAP_Info command documentation, an int is returned. Otherwise either a string is returned, or
None if the returned info value length is 0.
@exception DAPAccessIntf.DeviceError Raised under these conditions:
- Response packet is for a different command.
- Response packet is truncated or is for a different command.
- An int-type info was requested, but the returned value length is not 1, 2, or 4.
- A string-type info was requested, but the returned value length is greater than the response packet size minus response header and terminating null byte on the string.
"""
assert type(id_) is DAPAccessIntf.ID

Expand All @@ -183,11 +183,15 @@ def dap_info(self, id_):
self.interface.write(cmd)

resp = self.interface.read()
if len(resp) < 2:
raise DAPAccessIntf.DeviceError("truncated DAP_INFO response header")
if resp[0] != Command.DAP_INFO:
# Response is to a different command
raise DAPAccessIntf.DeviceError("expected DAP_INFO")

resp_len = resp[1]
if resp_len > (len(resp) - 2):
raise DAPAccessIntf.DeviceError("invalid DAP_INFO response length for %s" % id_.name)

# Integer values
if id_ in INTEGER_INFOS:
Expand All @@ -203,8 +207,6 @@ def dap_info(self, id_):
# String values. They are sent as C strings with a terminating null char, so we strip it out.
if resp_len == 0:
return None
if resp_len > (len(resp) - 2):
raise DAPAccessIntf.DeviceError("invalid DAP_INFO response length for %s" % id_.name)
return bytearray(resp[2:2 + resp_len - 1]).decode('utf-8', 'replace')

def set_led(self, type, enabled):
Expand Down
84 changes: 84 additions & 0 deletions test/unit/test_dap_info_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# pyOCD debugger
# Copyright (c) 2026 Solarboaters
# SPDX-License-Identifier: Apache-2.0
#
# 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.

from unittest.mock import Mock

import pytest

from pyocd.core.helpers import ConnectHelper
from pyocd.core.session import Session
from pyocd.probe.cmsis_dap_probe import CMSISDAPProbe
from pyocd.probe.pydapaccess.cmsis_dap_core import CMSISDAPProtocol
from pyocd.probe.pydapaccess.dap_access_api import DAPAccessIntf
from pyocd.probe.pydapaccess.dap_access_cmsis_dap import DAPAccessCMSISDAP


ID = DAPAccessIntf.ID


@pytest.mark.parametrize('response', [b'', b'\x00', b'\x00\x01', b'\x00\x02\x01', b'\x00\x04\x01\x02\x03'])
def test_truncated_info_is_a_device_error(response):
interface = Mock()
interface.read.return_value = response
with pytest.raises(DAPAccessIntf.DeviceError):
CMSISDAPProtocol(interface).dap_info(ID.MAX_PACKET_COUNT)
interface.write.assert_called_once_with([0, ID.MAX_PACKET_COUNT.value])


@pytest.mark.parametrize('info,response,expected', [
(ID.MAX_PACKET_COUNT, b'\x00\x01\x01', 1),
(ID.MAX_PACKET_SIZE, b'\x00\x02\x00\x08', 2048),
(ID.SWO_BUFFER_SIZE, b'\x00\x04\x78\x56\x34\x12', 0x12345678),
(ID.MAX_PACKET_COUNT, b'\x00\x01\x01.0' + bytes(59), 1),
(ID.VENDOR, b'\x00\x04ARM\x00', 'ARM'),
(ID.VENDOR, b'\x00\x00', None),
])
def test_valid_info(info, response, expected):
interface = Mock()
interface.read.return_value = response
assert CMSISDAPProtocol(interface).dap_info(info) == expected


@pytest.mark.parametrize('info,response', [
(ID.VENDOR, b'\x00\x04AR'),
(ID.MAX_PACKET_COUNT, b'\x01\x01\x01'),
(ID.MAX_PACKET_COUNT, b'\x00\x03\x01\x02\x03'),
])
def test_invalid_info_is_still_rejected(info, response):
interface = Mock()
interface.read.return_value = response
with pytest.raises(DAPAccessIntf.DeviceError):
CMSISDAPProtocol(interface).dap_info(info)


def test_empty_usb_response_does_not_abort_probe_listing(capsys, monkeypatch):
# Replace only the USB transport; exercise the reported open/identify/list stack.
interface = Mock()
interface.get_serial_number.return_value = 'empty-reply-probe'
interface.vendor_name = 'Example'
interface.product_name = 'CMSIS-DAP'
interface.vid = 0x1234
interface.pid = 0x5678
interface.read.return_value = b''
probe = CMSISDAPProbe(DAPAccessCMSISDAP(None, interface=interface))
other = Mock(description='Working probe', unique_id='working-probe', associated_board_info=None)
session = Session(None, options={'cmsis_dap.limit_packets': False}, no_config=True)
monkeypatch.setattr(Session, 'get_current', staticmethod(lambda: session))
ConnectHelper._print_probe_list([probe, other])
output = capsys.readouterr().out
assert 'empty-reply-probe' in output
assert 'working-probe' in output
interface.write.assert_called_once_with([0, ID.MAX_PACKET_COUNT.value])