Skip to content

Commit da78647

Browse files
Merge pull request #5077 from dhwalters423/dhwalters423-add-detect-info
Added interface version information to mbed detect command.
2 parents a0b624b + 61f46b8 commit da78647

File tree

3 files changed

+197
-5
lines changed

3 files changed

+197
-5
lines changed

tools/detect_targets.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
# Imports related to mbed build api
3232
from tools.build_api import mcu_toolchain_matrix
3333
from tools.test_api import get_autodetected_MUTS_list
34+
from tools.test_api import get_module_avail
3435
from argparse import ArgumentParser
3536

37+
try:
38+
import mbed_lstools
39+
except:
40+
pass
3641

3742
def main():
3843
"""Entry Point"""
@@ -75,15 +80,17 @@ def main():
7580
count = 0
7681
for mut in muts.values():
7782
if re.match(mcu_filter, mut['mcu']):
83+
interface_version = get_interface_version(mut['disk'])
7884
print ""
79-
print "[mbed] Detected %s, port %s, mounted %s" % \
80-
(mut['mcu'], mut['port'], mut['disk'])
85+
print "[mbed] Detected %s, port %s, mounted %s, interface version %s:" % \
86+
(mut['mcu'], mut['port'], mut['disk'], interface_version)
87+
8188
print "[mbed] Supported toolchains for %s" % mut['mcu']
8289
print mcu_toolchain_matrix(platform_filter=mut['mcu'])
8390
count += 1
8491

8592
if count == 0:
86-
print "[mbed] No mbed targets where detected on your system."
93+
print "[mbed] No mbed targets were detected on your system."
8794

8895
except KeyboardInterrupt:
8996
print "\n[CTRL+c] exit"
@@ -92,6 +99,32 @@ def main():
9299
traceback.print_exc(file=sys.stdout)
93100
print "[ERROR] %s" % str(exc)
94101
sys.exit(1)
102+
103+
def get_interface_version(mount_point):
104+
""" Function returns interface version from the target mounted on the specified mount point
105+
106+
mount_point can be acquired via the following:
107+
muts = get_autodetected_MUTS_list()
108+
for mut in muts.values():
109+
mount_point = mut['disk']
110+
111+
@param mount_point Name of disk where platform is connected to host machine.
112+
"""
113+
if get_module_avail('mbed_lstools'):
114+
try :
115+
mbeds = mbed_lstools.create()
116+
details_txt = mbeds.get_details_txt(mount_point)
117+
118+
if 'Interface Version' in details_txt:
119+
return details_txt['Interface Version']
120+
121+
elif 'Version' in details_txt:
122+
return details_txt['Version']
123+
124+
except :
125+
return 'unknown'
126+
127+
return 'unknown'
95128

96129
if __name__ == '__main__':
97130
main()

tools/test/detect_targets_test.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2017 ARM Limited
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
"""
17+
18+
import unittest
19+
from mock import patch
20+
from tools.detect_targets import get_interface_version
21+
22+
23+
class MbedLsToolsMock():
24+
"""
25+
Mock of mbedls tools
26+
"""
27+
28+
def __init__(self, test_type):
29+
self.interface_test_type = test_type
30+
31+
def get_details_txt(self, mount_point):
32+
return self.details_txt_types[self.interface_test_type];
33+
34+
# Static details.txt types.
35+
details_txt_types = {
36+
'details_valid_interface_version' : {
37+
'Unique ID': '0226000029164e45002f0012706e0006f301000097969900',
38+
'HIF ID': '97969900',
39+
'Auto Reset': '0',
40+
'Automation allowed': '0',
41+
'Daplink Mode': 'Interface',
42+
'Interface Version': '0240',
43+
'Git SHA': 'c765cbb590f57598756683254ca38b211693ae5e',
44+
'Local Mods': '0',
45+
'USB Interfaces': 'MSD, CDC, HID',
46+
'Interface CRC': '0x26764ebf'
47+
},
48+
'details_valid_version' : {
49+
'Version': '0226',
50+
'Build': 'Aug 24 2015 17:06:30',
51+
'Git Commit SHA': '27a236b9fe39c674a703c5c89655fbd26b8e27e1',
52+
'Git Local mods': 'Yes'
53+
},
54+
'details_missing_interface_version' : {
55+
'Unique ID': '0226000033514e450044500585d4001de981000097969900',
56+
'HIC ID': '97969900',
57+
'Auto Reset': '0',
58+
'Automation allowed': '0',
59+
'Overflow detection': '0',
60+
'Daplink Mode': 'Interface',
61+
'Git SHA': 'b403a07e3696cee1e116d44cbdd64446e056ce38',
62+
'Local Mods': '0',
63+
'USB Interfaces': 'MSD, CDC, HID',
64+
'Interface CRC': '0x4d98bf7e',
65+
'Remount count': '0'
66+
},
67+
'details_invalid_none' : None
68+
}
69+
70+
"""
71+
Tests for detect_targets.py
72+
"""
73+
74+
class DetectTargetsTest(unittest.TestCase):
75+
"""
76+
Test cases for Detect Target functionality
77+
"""
78+
79+
def setUp(self):
80+
"""
81+
Called before each test case
82+
83+
:return:
84+
"""
85+
self.missing_mount_point = None
86+
self.mount_point = "D:"
87+
88+
def tearDown(self):
89+
"""
90+
Nothing to tear down.
91+
Called after each test case
92+
93+
:return:
94+
"""
95+
pass
96+
97+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version'))
98+
def test_interface_version_valid(self, mbed_lstools_mock):
99+
"""
100+
Test that checks function returns correctly when given a valid Interface Version
101+
102+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
103+
:return
104+
"""
105+
106+
interface_version = get_interface_version(self.mount_point)
107+
assert interface_version == '0240'
108+
109+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_version'))
110+
def test_version_valid(self, mbed_lstools_mock):
111+
"""
112+
Test that checks function returns correctly when given a valid Version
113+
114+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
115+
:return
116+
"""
117+
118+
interface_version = get_interface_version(self.mount_point)
119+
assert interface_version == '0226'
120+
121+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_missing_interface_version'))
122+
def test_interface_version_missing_interface_version(self, mbed_lstools_mock):
123+
"""
124+
Test that checks function returns correctly when DETAILS.txt is present
125+
but an interface version is not listed.
126+
127+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
128+
:return
129+
"""
130+
131+
interface_version = get_interface_version(self.mount_point)
132+
assert interface_version == 'unknown'
133+
134+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
135+
def test_version_none(self, mbed_lstools_mock):
136+
"""
137+
Test that checks function returns correctly when a valid mount point is supplied
138+
but DETAILS.txt is not present.
139+
140+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
141+
:return
142+
"""
143+
144+
interface_version = get_interface_version(self.mount_point)
145+
assert interface_version == 'unknown'
146+
147+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
148+
def test_interface_version_missing_mount_point(self, mbed_lstools_mock):
149+
"""
150+
Test that checks function returns correctly when no mount point is supplied.
151+
152+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
153+
:return
154+
"""
155+
156+
interface_version = get_interface_version(self.missing_mount_point)
157+
assert interface_version == 'unknown'
158+
159+
if __name__ == '__main__':
160+
unittest.main()

tools/test_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,11 +1644,10 @@ def detect_database_verbose(db_url):
16441644

16451645

16461646
def get_module_avail(module_name):
1647-
""" This function returns True if module_name is already impored module
1647+
""" This function returns True if module_name is already imported module
16481648
"""
16491649
return module_name in sys.modules.keys()
16501650

1651-
16521651
def get_autodetected_MUTS_list(platform_name_filter=None):
16531652
oldError = None
16541653
if os.name == 'nt':

0 commit comments

Comments
 (0)