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

Added interface version information to mbed detect command. #5077

Merged
merged 11 commits into from Oct 5, 2017
39 changes: 36 additions & 3 deletions tools/detect_targets.py
Expand Up @@ -31,8 +31,13 @@
# Imports related to mbed build api
from tools.build_api import mcu_toolchain_matrix
from tools.test_api import get_autodetected_MUTS_list
from tools.test_api import get_module_avail
from argparse import ArgumentParser

try:
import mbed_lstools
except:
pass

def main():
"""Entry Point"""
Expand Down Expand Up @@ -75,15 +80,17 @@ def main():
count = 0
for mut in muts.values():
if re.match(mcu_filter, mut['mcu']):
interface_version = get_interface_version(mut['disk'])
print ""
print "[mbed] Detected %s, port %s, mounted %s" % \
(mut['mcu'], mut['port'], mut['disk'])
print "[mbed] Detected %s, port %s, mounted %s, interface version %s:" % \
(mut['mcu'], mut['port'], mut['disk'], interface_version)

print "[mbed] Supported toolchains for %s" % mut['mcu']
print mcu_toolchain_matrix(platform_filter=mut['mcu'])
count += 1

if count == 0:
print "[mbed] No mbed targets where detected on your system."
print "[mbed] No mbed targets were detected on your system."

except KeyboardInterrupt:
print "\n[CTRL+c] exit"
Expand All @@ -92,6 +99,32 @@ def main():
traceback.print_exc(file=sys.stdout)
print "[ERROR] %s" % str(exc)
sys.exit(1)

def get_interface_version(mount_point):
""" Function returns interface version from the target mounted on the specified mount point

mount_point can be acquired via the following:
muts = get_autodetected_MUTS_list()
for mut in muts.values():
mount_point = mut['disk']

@param mount_point Name of disk where platform is connected to host machine.
"""
if get_module_avail('mbed_lstools'):
try :
mbeds = mbed_lstools.create()
details_txt = mbeds.get_details_txt(mount_point)

if 'Interface Version' in details_txt:
return details_txt['Interface Version']

elif 'Version' in details_txt:
return details_txt['Version']

except :
return 'unknown'

return 'unknown'

if __name__ == '__main__':
main()
160 changes: 160 additions & 0 deletions tools/test/detect_targets_test.py
@@ -0,0 +1,160 @@
"""
mbed SDK
Copyright (c) 2017 ARM Limited

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.
"""

import unittest
from mock import patch
from tools.detect_targets import get_interface_version


class MbedLsToolsMock():
"""
Mock of mbedls tools
"""

def __init__(self, test_type):
self.interface_test_type = test_type

def get_details_txt(self, mount_point):
return self.details_txt_types[self.interface_test_type];

# Static details.txt types.
details_txt_types = {
'details_valid_interface_version' : {
'Unique ID': '0226000029164e45002f0012706e0006f301000097969900',
'HIF ID': '97969900',
'Auto Reset': '0',
'Automation allowed': '0',
'Daplink Mode': 'Interface',
'Interface Version': '0240',
'Git SHA': 'c765cbb590f57598756683254ca38b211693ae5e',
'Local Mods': '0',
'USB Interfaces': 'MSD, CDC, HID',
'Interface CRC': '0x26764ebf'
},
'details_valid_version' : {
'Version': '0226',
'Build': 'Aug 24 2015 17:06:30',
'Git Commit SHA': '27a236b9fe39c674a703c5c89655fbd26b8e27e1',
'Git Local mods': 'Yes'
},
'details_missing_interface_version' : {
'Unique ID': '0226000033514e450044500585d4001de981000097969900',
'HIC ID': '97969900',
'Auto Reset': '0',
'Automation allowed': '0',
'Overflow detection': '0',
'Daplink Mode': 'Interface',
'Git SHA': 'b403a07e3696cee1e116d44cbdd64446e056ce38',
'Local Mods': '0',
'USB Interfaces': 'MSD, CDC, HID',
'Interface CRC': '0x4d98bf7e',
'Remount count': '0'
},
'details_invalid_none' : None
}

"""
Tests for detect_targets.py
"""

class DetectTargetsTest(unittest.TestCase):
"""
Test cases for Detect Target functionality
"""

def setUp(self):
"""
Called before each test case

:return:
"""
self.missing_mount_point = None
self.mount_point = "D:"

def tearDown(self):
"""
Nothing to tear down.
Called after each test case

:return:
"""
pass

@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version'))
def test_interface_version_valid(self, mbed_lstools_mock):
"""
Test that checks function returns correctly when given a valid Interface Version

:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
:return
"""

interface_version = get_interface_version(self.mount_point)
assert interface_version == '0240'

@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_version'))
def test_version_valid(self, mbed_lstools_mock):
"""
Test that checks function returns correctly when given a valid Version

:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
:return
"""

interface_version = get_interface_version(self.mount_point)
assert interface_version == '0226'

@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_missing_interface_version'))
def test_interface_version_missing_interface_version(self, mbed_lstools_mock):
"""
Test that checks function returns correctly when DETAILS.txt is present
but an interface version is not listed.

:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
:return
"""

interface_version = get_interface_version(self.mount_point)
assert interface_version == 'unknown'

@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
def test_version_none(self, mbed_lstools_mock):
"""
Test that checks function returns correctly when a valid mount point is supplied
but DETAILS.txt is not present.

:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
:return
"""

interface_version = get_interface_version(self.mount_point)
assert interface_version == 'unknown'

@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
def test_interface_version_missing_mount_point(self, mbed_lstools_mock):
"""
Test that checks function returns correctly when no mount point is supplied.

:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
:return
"""

interface_version = get_interface_version(self.missing_mount_point)
assert interface_version == 'unknown'

if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions tools/test_api.py
Expand Up @@ -1643,11 +1643,10 @@ def detect_database_verbose(db_url):


def get_module_avail(module_name):
""" This function returns True if module_name is already impored module
""" This function returns True if module_name is already imported module
"""
return module_name in sys.modules.keys()


def get_autodetected_MUTS_list(platform_name_filter=None):
oldError = None
if os.name == 'nt':
Expand Down