Skip to content

VID/PID based device type detection #370

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

Merged
merged 4 commits into from
Jul 19, 2018
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
31 changes: 18 additions & 13 deletions mbed_lstools/lstools_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class MbedLsToolsBase(object):
DETAILS_TXT_NAME = 'DETAILS.TXT'
MBED_HTM_NAME = 'mbed.htm'

VENDOR_ID_DEVICE_TYPE_MAP = {
'0483': 'stlink',
Copy link

Choose a reason for hiding this comment

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

Can we rely on to these thtee VID in every boards now? what happens if VID does not match to some?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I believe we can, especially on anything released within the past few years. If the VID does not match it will default to the 'daplink' style detection (just like it used to).

'0d28': 'daplink',
'1366': 'jlink'
}

def __init__(self, list_unmounted=False, **kwargs):
""" ctor
"""
Expand Down Expand Up @@ -211,13 +217,14 @@ def _update_device_from_fs(self, device, read_details_txt):
try:
directory_entries = os.listdir(device['mount_point'])
device['directory_entries'] = directory_entries
device['device_type'] = self._detect_device_type(directory_entries)
device['device_type'] = self._detect_device_type(device)
device['target_id'] = device['target_id_usb_id']

{
'daplink': self._update_device_details_daplink,
'daplink': self._update_device_details_daplink_compatible,
'stlink': self._update_device_details_daplink_compatible,
'jlink': self._update_device_details_jlink
}[device['device_type']](device, read_details_txt, directory_entries)
}[device['device_type'] or 'daplink'](device, read_details_txt)
except (OSError, IOError) as e:
logger.warning(
'Marking device with mount point "%s" as unmounted due to the '
Expand All @@ -226,23 +233,22 @@ def _update_device_from_fs(self, device, read_details_txt):
device['device_type'] = 'unknown'


def _detect_device_type(self, directory_entries):
def _detect_device_type(self, device):
""" Returns a string of the device type
@param directory_entries List of directories and files on the device
@return 'daplink' or 'jlink'
@param device Dictionary containing device information
@return Device type located in VENDOR_ID_DEVICE_TYPE_MAP or None if unknown
"""

return 'jlink' if 'segger.html' in [e.lower() for e in directory_entries] else 'daplink'
return self.VENDOR_ID_DEVICE_TYPE_MAP.get(device.get('vendor_id'))


def _update_device_details_daplink(self, device, read_details_txt, directory_entries):
def _update_device_details_daplink_compatible(self, device, read_details_txt):
""" Updates the daplink-specific device information based on files from its 'mount_point'
@param device Dictionary containing device information
@param read_details_txt A boolean controlling the presense of the
output dict attributes read from other files present on the 'mount_point'
@param directory_entries List of directories and files on the device
"""
lowercase_directory_entries = [e.lower() for e in directory_entries]
lowercase_directory_entries = [e.lower() for e in device['directory_entries']]
if self.MBED_HTM_NAME.lower() in lowercase_directory_entries:
self._update_device_from_htm(device)
elif not read_details_txt:
Expand Down Expand Up @@ -271,12 +277,11 @@ def _update_device_details_daplink(self, device, read_details_txt, directory_ent
else:
device['platform_name'] = None

def _update_device_details_jlink(self, device, _, directory_entries):
def _update_device_details_jlink(self, device, _):
""" Updates the jlink-specific device information based on files from its 'mount_point'
@param device Dictionary containing device information
@param directory_entries List of directories and files on the device
"""
lower_case_map = {e.lower(): e for e in directory_entries}
lower_case_map = {e.lower(): e for e in device['directory_entries']}

if 'board.html' in lower_case_map:
board_file_key = 'board.html'
Expand Down
27 changes: 19 additions & 8 deletions test/mbedls_toolsbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,21 @@ def test_update_device_from_fs_unknown(self):
self.assertEqual(device['device_type'], 'unknown')

def test_detect_device_test(self):
device_type = self.base._detect_device_type(['Segger.html'])
self.assertEqual(device_type, 'jlink')

device_type = self.base._detect_device_type(['MBED.HTM', 'DETAILS.TXT'])
device_type = self.base._detect_device_type({
'vendor_id': '0483'
})
self.assertEqual(device_type, 'stlink')

device_type = self.base._detect_device_type({
'vendor_id': '0d28'
})
self.assertEqual(device_type, 'daplink')

device_type = self.base._detect_device_type({
'vendor_id': '1366'
})
self.assertEqual(device_type, 'jlink')

def test_update_device_details_jlink(self):
jlink_html_contents = ('<html><head><meta http-equiv="refresh" '
'content="0; url=http://www.nxp.com/FRDM-KL27Z"/>'
Expand All @@ -307,24 +316,26 @@ def test_update_device_details_jlink(self):

with patch('mbed_lstools.lstools_base.open', _open, create=True):
device = deepcopy(base_device)
self.base._update_device_details_jlink(device, False, ['Board.html', 'User Guide.html'])
device['directory_entries'] = ['Board.html', 'User Guide.html']
self.base._update_device_details_jlink(device, False)
self.assertEqual(device['url'], 'http://www.nxp.com/FRDM-KL27Z')
self.assertEqual(device['platform_name'], 'KL27Z')
_open.assert_called_once_with(os.path.join(dummy_mount_point, 'Board.html'), 'r')

_open.reset_mock()

device = deepcopy(base_device)
self.base._update_device_details_jlink(device, False, ['User Guide.html'])
device['directory_entries'] = ['User Guide.html']
self.base._update_device_details_jlink(device, False)
self.assertEqual(device['url'], 'http://www.nxp.com/FRDM-KL27Z')
self.assertEqual(device['platform_name'], 'KL27Z')
_open.assert_called_once_with(os.path.join(dummy_mount_point, 'User Guide.html'), 'r')

_open.reset_mock()

device = deepcopy(base_device)
self.base._update_device_details_jlink(device, False, ['unhelpful_file.html'])
self.assertEqual(device, base_device)
device['directory_entries'] = ['unhelpful_file.html']
self.base._update_device_details_jlink(device, False)
_open.assert_not_called()

def test_fs_never(self):
Expand Down