From ec9e9d01a2c15d287c608d36d3dbfceb052568b9 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Thu, 24 Mar 2016 17:05:56 +0000 Subject: [PATCH 01/13] Adding hardware reset feature --- mbed_host_tests/host_tests/base_host_test.py | 5 +- .../host_tests_conn_proxy/conn_proxy.py | 15 +-- .../host_tests_runner/host_test_default.py | 20 +++- .../host_tests_runner/mbed_base.py | 108 +++++++++++++++--- 4 files changed, 122 insertions(+), 26 deletions(-) diff --git a/mbed_host_tests/host_tests/base_host_test.py b/mbed_host_tests/host_tests/base_host_test.py index ee849fb..a5d54c0 100644 --- a/mbed_host_tests/host_tests/base_host_test.py +++ b/mbed_host_tests/host_tests/base_host_test.py @@ -40,6 +40,7 @@ def __notify_conn_lost(self, text): def __notify_dut(self, key, value): """! Send data over serial to DUT """ if self.__event_queue: + print "adding to dut event queue %s %s" % (key, value) self.__dut_event_queue.put((key, value, time())) def notify_complete(self, result=None): @@ -49,13 +50,13 @@ def notify_complete(self, result=None): if self.__event_queue: self.__event_queue.put(('__notify_complete', result, time())) - def reset_dut(self): + def reset_dut(self, value): """ Reset device under test :return: """ if self.__event_queue: - self.__event_queue.put(('__reset_dut', True, time())) + self.__event_queue.put(('__reset_dut', value, time())) def notify_conn_lost(self, text): """! Notify main even loop that there was a DUT-host test connection error diff --git a/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py b/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py index 4c309e2..fca8cc1 100644 --- a/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py +++ b/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py @@ -35,7 +35,7 @@ def __init__(self, port, baudrate, prn_lock, config): self.logger = HtrunLogger(prn_lock, 'SERI') self.LAST_ERROR = None - self.logger.prn_inf("serial(port=%s, baudrate=%d)"% (self.port, self.baudrate)) + self.logger.prn_inf("serial(port=%s, baudrate=%d, timeout=%s)"% (self.port, self.baudrate, self.timeout)) try: self.serial = Serial(port, baudrate=baudrate, timeout=self.timeout) except SerialException as e: @@ -93,7 +93,8 @@ def write(self, payload, log=False): def write_kv(self, key, value): kv_buff = "{{%s;%s}}\n"% (key, value) self.write(kv_buff) - self.logger.prn_txd(kv_buff) + print "Written: %s" % kv_buff + #self.logger.prn_txd(kv_buff) return kv_buff def flush(self): @@ -174,11 +175,11 @@ def conn_process(event_queue, dut_event_queue, prn_lock, config): break # Send data to DUT - if not dut_event_queue.empty(): - try: - (key, value, _) = dut_event_queue.get(timeout=1) - except QueueEmpty: - continue + try: + (key, value, _) = dut_event_queue.get(timeout=0) + except QueueEmpty: + pass # Check if target sent something + else: # Return if state machine in host_test_default has finished to end process if key == '__host_test_finished' and value == True: connector.finish() diff --git a/mbed_host_tests/host_tests_runner/host_test_default.py b/mbed_host_tests/host_tests_runner/host_test_default.py index 13ec6df..86f6468 100644 --- a/mbed_host_tests/host_tests_runner/host_test_default.py +++ b/mbed_host_tests/host_tests_runner/host_test_default.py @@ -36,6 +36,8 @@ class DefaultTestSelector(DefaultTestSelectorBase): """! Select default host_test supervision (replaced after auto detection) """ + RESET_TYPE_SW_RST = "software_reset" + RESET_TYPE_HW_RST = "hardware_reset" def __init__(self, options): """! ctor @@ -190,11 +192,22 @@ def start_conn_process(): result = value break elif key == '__reset_dut': - # Disconnecting and re-connecting comm process will reset DUT + # Disconnect to avoid connection lost event dut_event_queue.put(('__host_test_finished', True, time())) p.join() - # self.mbed.update_device_info() - This call is commented but left as it would be required in hard reset. - p = start_conn_process() + + assert value in \ + [DefaultTestSelector.RESET_TYPE_SW_RST, DefaultTestSelector.RESET_TYPE_HW_RST], \ + "Unknown reset type (%s). Supported types 'software_reset' and 'hardware_reset'" % value + + if value == DefaultTestSelector.RESET_TYPE_SW_RST: + # Disconnecting and re-connecting comm process will reset DUT + p = start_conn_process() + elif value == DefaultTestSelector.RESET_TYPE_HW_RST: + # request hardware reset + self.mbed.hw_reset() + # connect to the device + p = start_conn_process() elif key == '__notify_conn_lost': # This event is sent by conn_process, DUT connection was lost self.logger.prn_err(value) @@ -208,6 +221,7 @@ def start_conn_process(): callbacks__exit = True break elif key in callbacks: + print 'received key value = %s %s ' % (key, value) # Handle callback callbacks[key](key, value, timestamp) else: diff --git a/mbed_host_tests/host_tests_runner/mbed_base.py b/mbed_host_tests/host_tests_runner/mbed_base.py index f1d10a5..03a92b9 100644 --- a/mbed_host_tests/host_tests_runner/mbed_base.py +++ b/mbed_host_tests/host_tests_runner/mbed_base.py @@ -17,11 +17,14 @@ Author: Przemyslaw Wirkus """ +import os import json import time +import requests import mbed_lstools from time import sleep - +#from mbed_tas_rm.requests import SwitchRequest +#from mbed_tas_rm.client.client import MbedTasRmClient import mbed_host_tests.host_tests_plugins as ht_plugins @@ -116,20 +119,97 @@ def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None destination_disk=disk) return result - def update_device_info(self): + def hw_reset(self): + """ + Performs hardware reset of target ned device. + + :return: + """ + try: + ip = os.environ['MBED_TAS_RM_IP'] + port = os.environ['MBED_TAS_RM_PORT'] + except KeyError, e: + print "HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset." + else: + self.reset_target(ip, port) + + def reset_target(self, ip, port): """ - Updates device's port and disk using mbedls. Typically used after reset. + Reset target device using TAS RM API + :param client: + :param target_info: :return: """ - for i in range(3): - mbed_list = mbed_lstools.create().list_mbeds_ext() - if mbed_list: - for mut in mbed_list: - if mut['target_id'] == self.target_id: - self.port = mut['serial_port'] - self.disk = mut['mount_point'] - return True - print "HOST: Failed to find target after reset. Retrying (%d)" % i - time.sleep(1) - return False + + switch_off_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": self.target_id, + "switch_command": "OFF" + } + ] + } + + switch_state_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": self.target_id, + "switch_command": "STATE" + } + ] + } + + switch_on_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": self.target_id, + "switch_command": "ON" + } + ] + } + + # reset target + switch_off_req = self.run_request(ip, port, switch_off_req) + if "error" in switch_off_req['sub_requests'][0]: + print "HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error'] + return + switch_state_req = self.run_request(ip, port, switch_state_req) + + start = time.time() + while switch_state_req['sub_requests'][0]['state'] != 'OFF' and (time.time() - start) < 300: + time.sleep(2) + switch_state_req = self.run_request(ip, port, switch_state_req) + + start = time.time() + switch_on_req = self.run_request(ip, port, switch_on_req) + while (switch_on_req['sub_requests'][0]['state'] != 'ON' or + switch_on_req['sub_requests'][0]["mount_point"] == "Not Connected") and \ + (time.time() - start) < 300: + time.sleep(2) + switch_on_req = self.run_request(ip, port, switch_on_req) + + self.port = switch_on_req['sub_requests'][0]['serial_port'] + self.disk = switch_on_req['sub_requests'][0]['mount_point'] + + def run_request(self, ip, port, request): + """ + + :param request: + :return: + """ + headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} + get_resp = requests.get("http://%s:%s/" % (ip, port), + data=json.dumps(request), + headers=headers) + resp = get_resp.json() + if get_resp.status_code == 200: + return resp + else: + return None \ No newline at end of file From 7b84af9bc85ffc79553661f09eff2d8053755b55 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Tue, 29 Mar 2016 10:01:41 +0100 Subject: [PATCH 02/13] Removed debug prints --- mbed_host_tests/host_tests/base_host_test.py | 1 - mbed_host_tests/host_tests_runner/host_test_default.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/mbed_host_tests/host_tests/base_host_test.py b/mbed_host_tests/host_tests/base_host_test.py index a5d54c0..88f9462 100644 --- a/mbed_host_tests/host_tests/base_host_test.py +++ b/mbed_host_tests/host_tests/base_host_test.py @@ -40,7 +40,6 @@ def __notify_conn_lost(self, text): def __notify_dut(self, key, value): """! Send data over serial to DUT """ if self.__event_queue: - print "adding to dut event queue %s %s" % (key, value) self.__dut_event_queue.put((key, value, time())) def notify_complete(self, result=None): diff --git a/mbed_host_tests/host_tests_runner/host_test_default.py b/mbed_host_tests/host_tests_runner/host_test_default.py index 86f6468..8d36b00 100644 --- a/mbed_host_tests/host_tests_runner/host_test_default.py +++ b/mbed_host_tests/host_tests_runner/host_test_default.py @@ -221,7 +221,6 @@ def start_conn_process(): callbacks__exit = True break elif key in callbacks: - print 'received key value = %s %s ' % (key, value) # Handle callback callbacks[key](key, value, timestamp) else: @@ -336,4 +335,4 @@ def execute(self): return self.get_test_result_int(result) except KeyboardInterrupt: - return(-3) # Keyboard interrupt \ No newline at end of file + return(-3) # Keyboard interrupt From dd67816aba9f91b259b48c4806f58fc72ad035b3 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Tue, 29 Mar 2016 10:54:04 +0100 Subject: [PATCH 03/13] Updated reset device method --- .../host_tests_runner/mbed_base.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mbed_host_tests/host_tests_runner/mbed_base.py b/mbed_host_tests/host_tests_runner/mbed_base.py index 03a92b9..1a3a4b3 100644 --- a/mbed_host_tests/host_tests_runner/mbed_base.py +++ b/mbed_host_tests/host_tests_runner/mbed_base.py @@ -188,15 +188,20 @@ def reset_target(self, ip, port): switch_state_req = self.run_request(ip, port, switch_state_req) start = time.time() - switch_on_req = self.run_request(ip, port, switch_on_req) - while (switch_on_req['sub_requests'][0]['state'] != 'ON' or - switch_on_req['sub_requests'][0]["mount_point"] == "Not Connected") and \ + self.run_request(ip, port, switch_on_req) + switch_state_req = self.run_request(ip, port, switch_state_req) + while (switch_state_req['sub_requests'][0]['state'] != 'ON' or + switch_state_req['sub_requests'][0]["mount_point"] == "Not Connected") and \ (time.time() - start) < 300: time.sleep(2) - switch_on_req = self.run_request(ip, port, switch_on_req) + switch_state_req = self.run_request(ip, port, switch_state_req) - self.port = switch_on_req['sub_requests'][0]['serial_port'] - self.disk = switch_on_req['sub_requests'][0]['mount_point'] + if (switch_state_req['sub_requests'][0]['state'] == 'ON' and + switch_state_req['sub_requests'][0]["mount_point"] != "Not Connected"): + self.port = switch_state_req['sub_requests'][0]['serial_port'] + self.disk = switch_state_req['sub_requests'][0]['mount_point'] + else: + print "HOST: Failed to reset device %s" % self.target_id def run_request(self, ip, port, request): """ @@ -212,4 +217,4 @@ def run_request(self, ip, port, request): if get_resp.status_code == 200: return resp else: - return None \ No newline at end of file + return None From 73af07217ef65d7fb1ffa26c5ad929eb2837bbf9 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Tue, 29 Mar 2016 11:14:45 +0100 Subject: [PATCH 04/13] Removed assert on reset type to add stability against test developer's mistake. --- .../host_tests_runner/host_test_default.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/mbed_host_tests/host_tests_runner/host_test_default.py b/mbed_host_tests/host_tests_runner/host_test_default.py index 8d36b00..8dcdbd5 100644 --- a/mbed_host_tests/host_tests_runner/host_test_default.py +++ b/mbed_host_tests/host_tests_runner/host_test_default.py @@ -196,18 +196,21 @@ def start_conn_process(): dut_event_queue.put(('__host_test_finished', True, time())) p.join() - assert value in \ - [DefaultTestSelector.RESET_TYPE_SW_RST, DefaultTestSelector.RESET_TYPE_HW_RST], \ - "Unknown reset type (%s). Supported types 'software_reset' and 'hardware_reset'" % value - if value == DefaultTestSelector.RESET_TYPE_SW_RST: - # Disconnecting and re-connecting comm process will reset DUT - p = start_conn_process() + self.logger.prn_inf("Performing software reset.") + # Just disconnecting and re-connecting comm process will soft reset DUT elif value == DefaultTestSelector.RESET_TYPE_HW_RST: + self.logger.prn_inf("Performing hard reset.") # request hardware reset self.mbed.hw_reset() - # connect to the device - p = start_conn_process() + else: + self.logger.prn_err("Invalid reset type (%s). Supported types [%s]." % + (value, ", ".join([DefaultTestSelector.RESET_TYPE_HW_RST, + DefaultTestSelector.RESET_TYPE_SW_RST]))) + self.logger.prn_inf("Software reset will be performed.") + + # connect to the device + p = start_conn_process() elif key == '__notify_conn_lost': # This event is sent by conn_process, DUT connection was lost self.logger.prn_err(value) From 71c63c20da90ba4f0fabc18564a436a91d2db302 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Tue, 29 Mar 2016 12:06:45 +0100 Subject: [PATCH 05/13] restructuring reset_target() --- .../host_tests_runner/mbed_base.py | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/mbed_host_tests/host_tests_runner/mbed_base.py b/mbed_host_tests/host_tests_runner/mbed_base.py index 1a3a4b3..3318605 100644 --- a/mbed_host_tests/host_tests_runner/mbed_base.py +++ b/mbed_host_tests/host_tests_runner/mbed_base.py @@ -153,16 +153,6 @@ def reset_target(self, ip, port): ] } - switch_state_req = { - "name": "switchResource", - "sub_requests": [ - { - "resource_type": "mbed_platform", - "resource_id": self.target_id, - "switch_command": "STATE" - } - ] - } switch_on_req = { "name": "switchResource", @@ -177,31 +167,44 @@ def reset_target(self, ip, port): # reset target switch_off_req = self.run_request(ip, port, switch_off_req) + if switch_off_req is None: + print "HOST: Failed to communicate with TAS RM!" + return + if "error" in switch_off_req['sub_requests'][0]: print "HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error'] return - switch_state_req = self.run_request(ip, port, switch_state_req) - start = time.time() - while switch_state_req['sub_requests'][0]['state'] != 'OFF' and (time.time() - start) < 300: - time.sleep(2) - switch_state_req = self.run_request(ip, port, switch_state_req) + def poll_state(required_state): + switch_state_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": self.target_id, + "switch_command": "STATE" + } + ] + } + resp = self.run_request(ip, port, switch_state_req) + start = time.time() + while resp and resp['sub_requests'][0]['state'] != required_state and \ + (required_state == 'OFF' or (required_state == 'ON' and + resp['sub_requests'][0]["mount_point"] != "Not Connected")) and \ + (time.time() - start) < 300: + time.sleep(2) + resp = self.run_request(ip, port, resp) + return resp + + poll_state("OFF") - start = time.time() self.run_request(ip, port, switch_on_req) - switch_state_req = self.run_request(ip, port, switch_state_req) - while (switch_state_req['sub_requests'][0]['state'] != 'ON' or - switch_state_req['sub_requests'][0]["mount_point"] == "Not Connected") and \ - (time.time() - start) < 300: - time.sleep(2) - switch_state_req = self.run_request(ip, port, switch_state_req) - - if (switch_state_req['sub_requests'][0]['state'] == 'ON' and - switch_state_req['sub_requests'][0]["mount_point"] != "Not Connected"): - self.port = switch_state_req['sub_requests'][0]['serial_port'] - self.disk = switch_state_req['sub_requests'][0]['mount_point'] - else: - print "HOST: Failed to reset device %s" % self.target_id + resp = poll_state("ON") + if resp and resp['sub_requests'][0]['state'] == 'ON' and resp['sub_requests'][0]["mount_point"] != "Not Connected": + self.port = resp['sub_requests'][0]['serial_port'] + self.disk = resp['sub_requests'][0]['mount_point'] + else: + print "HOST: Failed to reset device %s" % self.target_id def run_request(self, ip, port, request): """ From cf4b695a5a17f0577c9f9f09e0229f89f28076d1 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Tue, 29 Mar 2016 15:32:35 +0100 Subject: [PATCH 06/13] Fixed polling loop for mbed device power state --- mbed_host_tests/host_tests_runner/mbed_base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mbed_host_tests/host_tests_runner/mbed_base.py b/mbed_host_tests/host_tests_runner/mbed_base.py index 3318605..f60e269 100644 --- a/mbed_host_tests/host_tests_runner/mbed_base.py +++ b/mbed_host_tests/host_tests_runner/mbed_base.py @@ -188,10 +188,8 @@ def poll_state(required_state): } resp = self.run_request(ip, port, switch_state_req) start = time.time() - while resp and resp['sub_requests'][0]['state'] != required_state and \ - (required_state == 'OFF' or (required_state == 'ON' and - resp['sub_requests'][0]["mount_point"] != "Not Connected")) and \ - (time.time() - start) < 300: + while resp and (resp['sub_requests'][0]['state'] != required_state or (required_state == 'ON' and + resp['sub_requests'][0]["mount_point"] == "Not Connected")) and (time.time() - start) < 300: time.sleep(2) resp = self.run_request(ip, port, resp) return resp @@ -221,3 +219,4 @@ def run_request(self, ip, port, request): return resp else: return None + From 406be74cbaf0402274fe6573cadb6b2f6b85137b Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 11:59:42 +0100 Subject: [PATCH 07/13] Moving power cycle code to a new plugin --- .../host_tests_plugins/__init__.py | 2 + .../module_power_cycle_mbed.py | 172 ++++++++++++++++++ .../host_tests_runner/mbed_base.py | 106 +---------- setup.py | 3 +- 4 files changed, 185 insertions(+), 98 deletions(-) create mode 100644 mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py diff --git a/mbed_host_tests/host_tests_plugins/__init__.py b/mbed_host_tests/host_tests_plugins/__init__.py index 2fc6207..c527104 100644 --- a/mbed_host_tests/host_tests_plugins/__init__.py +++ b/mbed_host_tests/host_tests_plugins/__init__.py @@ -30,6 +30,7 @@ import module_copy_shell import module_copy_mbed import module_reset_mbed +import module_power_cycle_mbed # Additional, non standard platforms import module_copy_silabs @@ -56,6 +57,7 @@ HOST_TEST_PLUGIN_REGISTRY.register_plugin(module_reset_silabs.load_plugin()) HOST_TEST_PLUGIN_REGISTRY.register_plugin(module_copy_stlink.load_plugin()) HOST_TEST_PLUGIN_REGISTRY.register_plugin(module_reset_stlink.load_plugin()) +HOST_TEST_PLUGIN_REGISTRY.register_plugin(module_power_cycle_mbed.load_plugin()) #HOST_TEST_PLUGIN_REGISTRY.register_plugin(module_copy_jn51xx.load_plugin()) #HOST_TEST_PLUGIN_REGISTRY.register_plugin(module_reset_jn51xx.load_plugin()) diff --git a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py new file mode 100644 index 0000000..2470059 --- /dev/null +++ b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py @@ -0,0 +1,172 @@ +""" +mbed SDK +Copyright (c) 2011-2015 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. + +Author: Przemyslaw Wirkus +""" + +import os +import json +import time +import requests +from host_test_plugins import HostTestPluginBase + + +class HostTestPluginPowerCycleResetMethod(HostTestPluginBase): + + # Plugin interface + name = 'HostTestPluginHwResetMethod_Mbed' + type = 'ResetMethod' + stable = True + capabilities = ['power_cycle'] + required_parameters = ['target_id', 'device_info'] + + def setup(self, *args, **kwargs): + """! Configure plugin, this function should be called before plugin execute() method is used. + """ + return True + + def execute(self, capability, *args, **kwargs): + """! Executes capability by name + + @param capability Capability name + @param args Additional arguments + @param kwargs Additional arguments + @details Each capability e.g. may directly just call some command line program or execute building pythonic function + @return Capability call return value + """ + if 'target_id' not in kwargs or not kwargs['target_id']: + self.print_plugin_error("Error: This plugin requires mbed target_id") + return False + + if 'device_info' not in kwargs or type(kwargs['device_info']) is not dict: + self.print_plugin_error("Error: This plugin requires dict parameter 'device_info' passed by the caller.") + return False + + result = False + if self.check_parameters(capability, *args, **kwargs) is True: + if capability in HostTestPluginPowerCycleResetMethod.capabilities: + target_id = kwargs['target_id'] + device_info = kwargs['device_info'] + self.__hw_reset(target_id, device_info) + return result + + def __hw_reset(self, target_id, device_info): + """ + Performs hardware reset of target ned device. + + :return: + """ + try: + ip = os.environ['MBED_TAS_RM_IP'] + port = os.environ['MBED_TAS_RM_PORT'] + except KeyError, e: + print "HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset." + else: + self.__reset_target(ip, port, target_id, device_info) + + def __reset_target(self, ip, port, target_id, device_info): + """ + Reset target device using TAS RM API + + :param ip: + :param port: + :param target_id: + :param device_info: + :return: + """ + + switch_off_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": target_id, + "switch_command": "OFF" + } + ] + } + + + switch_on_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": target_id, + "switch_command": "ON" + } + ] + } + + # reset target + switch_off_req = self.__run_request(ip, port, switch_off_req) + if switch_off_req is None: + print "HOST: Failed to communicate with TAS RM!" + return + + if "error" in switch_off_req['sub_requests'][0]: + print "HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error'] + return + + def poll_state(required_state): + switch_state_req = { + "name": "switchResource", + "sub_requests": [ + { + "resource_type": "mbed_platform", + "resource_id": target_id, + "switch_command": "STATE" + } + ] + } + resp = self.__run_request(ip, port, switch_state_req) + start = time.time() + while resp and (resp['sub_requests'][0]['state'] != required_state or (required_state == 'ON' and + resp['sub_requests'][0]["mount_point"] == "Not Connected")) and (time.time() - start) < 300: + time.sleep(2) + resp = self.__run_request(ip, port, resp) + return resp + + poll_state("OFF") + + self.__run_request(ip, port, switch_on_req) + resp = poll_state("ON") + if resp and resp['sub_requests'][0]['state'] == 'ON' and resp['sub_requests'][0]["mount_point"] != "Not Connected": + for k, v in resp['sub_requests'][0].iteritems(): + device_info[k] = v + else: + print "HOST: Failed to reset device %s" % target_id + + @staticmethod + def __run_request(ip, port, request): + """ + + :param request: + :return: + """ + headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} + get_resp = requests.get("http://%s:%s/" % (ip, port), data=json.dumps(request), headers=headers) + resp = get_resp.json() + if get_resp.status_code == 200: + return resp + else: + return None + + +def load_plugin(): + """! Returns plugin available in this module + """ + return HostTestPluginPowerCycleResetMethod() diff --git a/mbed_host_tests/host_tests_runner/mbed_base.py b/mbed_host_tests/host_tests_runner/mbed_base.py index f60e269..f5ee602 100644 --- a/mbed_host_tests/host_tests_runner/mbed_base.py +++ b/mbed_host_tests/host_tests_runner/mbed_base.py @@ -21,10 +21,7 @@ import json import time import requests -import mbed_lstools from time import sleep -#from mbed_tas_rm.requests import SwitchRequest -#from mbed_tas_rm.client.client import MbedTasRmClient import mbed_host_tests.host_tests_plugins as ht_plugins @@ -125,98 +122,13 @@ def hw_reset(self): :return: """ - try: - ip = os.environ['MBED_TAS_RM_IP'] - port = os.environ['MBED_TAS_RM_PORT'] - except KeyError, e: - print "HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset." - else: - self.reset_target(ip, port) - - def reset_target(self, ip, port): - """ - Reset target device using TAS RM API - - :param client: - :param target_info: - :return: - """ - - switch_off_req = { - "name": "switchResource", - "sub_requests": [ - { - "resource_type": "mbed_platform", - "resource_id": self.target_id, - "switch_command": "OFF" - } - ] - } - - - switch_on_req = { - "name": "switchResource", - "sub_requests": [ - { - "resource_type": "mbed_platform", - "resource_id": self.target_id, - "switch_command": "ON" - } - ] - } - - # reset target - switch_off_req = self.run_request(ip, port, switch_off_req) - if switch_off_req is None: - print "HOST: Failed to communicate with TAS RM!" - return - - if "error" in switch_off_req['sub_requests'][0]: - print "HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error'] - return - - def poll_state(required_state): - switch_state_req = { - "name": "switchResource", - "sub_requests": [ - { - "resource_type": "mbed_platform", - "resource_id": self.target_id, - "switch_command": "STATE" - } - ] - } - resp = self.run_request(ip, port, switch_state_req) - start = time.time() - while resp and (resp['sub_requests'][0]['state'] != required_state or (required_state == 'ON' and - resp['sub_requests'][0]["mount_point"] == "Not Connected")) and (time.time() - start) < 300: - time.sleep(2) - resp = self.run_request(ip, port, resp) - return resp - - poll_state("OFF") - - self.run_request(ip, port, switch_on_req) - resp = poll_state("ON") - if resp and resp['sub_requests'][0]['state'] == 'ON' and resp['sub_requests'][0]["mount_point"] != "Not Connected": - self.port = resp['sub_requests'][0]['serial_port'] - self.disk = resp['sub_requests'][0]['mount_point'] - else: - print "HOST: Failed to reset device %s" % self.target_id - - def run_request(self, ip, port, request): - """ - - :param request: - :return: - """ - headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} - get_resp = requests.get("http://%s:%s/" % (ip, port), - data=json.dumps(request), - headers=headers) - resp = get_resp.json() - if get_resp.status_code == 200: - return resp - else: - return None + device_info = {} + result = ht_plugins.call_plugin('ResetMethod', + 'power_cycle', + target_id=self.target_id, + device_info=device_info) + if result: + self.port = device_info['serial_port'] + self.disk = device_info['mount_point'] + return result diff --git a/setup.py b/setup.py index f3c396d..e0e8f8d 100644 --- a/setup.py +++ b/setup.py @@ -53,4 +53,5 @@ def read(fname): "mbedflsh=mbed_host_tests.mbedflsh:main"], }, install_requires=["PySerial>=3.0", - "PrettyTable>=0.7.2"]) + "PrettyTable>=0.7.2", + "requests"]) From 4ca503a468b69f6e6e773e05bfafe62d5b50b88b Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 12:50:22 +0100 Subject: [PATCH 08/13] minor changes --- .../module_power_cycle_mbed.py | 17 ++++++++++++----- mbed_host_tests/host_tests_runner/mbed_base.py | 3 --- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py index 2470059..bc332b1 100644 --- a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py +++ b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py @@ -27,7 +27,7 @@ class HostTestPluginPowerCycleResetMethod(HostTestPluginBase): # Plugin interface - name = 'HostTestPluginHwResetMethod_Mbed' + name = 'HostTestPluginPowerCycleResetMethod' type = 'ResetMethod' stable = True capabilities = ['power_cycle'] @@ -60,7 +60,7 @@ def execute(self, capability, *args, **kwargs): if capability in HostTestPluginPowerCycleResetMethod.capabilities: target_id = kwargs['target_id'] device_info = kwargs['device_info'] - self.__hw_reset(target_id, device_info) + result = self.__hw_reset(target_id, device_info) return result def __hw_reset(self, target_id, device_info): @@ -69,13 +69,15 @@ def __hw_reset(self, target_id, device_info): :return: """ + result = False try: ip = os.environ['MBED_TAS_RM_IP'] port = os.environ['MBED_TAS_RM_PORT'] except KeyError, e: print "HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset." else: - self.__reset_target(ip, port, target_id, device_info) + result = self.__reset_target(ip, port, target_id, device_info) + return result def __reset_target(self, ip, port, target_id, device_info): """ @@ -111,15 +113,17 @@ def __reset_target(self, ip, port, target_id, device_info): ] } + result = False + # reset target switch_off_req = self.__run_request(ip, port, switch_off_req) if switch_off_req is None: print "HOST: Failed to communicate with TAS RM!" - return + return result if "error" in switch_off_req['sub_requests'][0]: print "HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error'] - return + return result def poll_state(required_state): switch_state_req = { @@ -147,9 +151,12 @@ def poll_state(required_state): if resp and resp['sub_requests'][0]['state'] == 'ON' and resp['sub_requests'][0]["mount_point"] != "Not Connected": for k, v in resp['sub_requests'][0].iteritems(): device_info[k] = v + result = True else: print "HOST: Failed to reset device %s" % target_id + return result + @staticmethod def __run_request(ip, port, request): """ diff --git a/mbed_host_tests/host_tests_runner/mbed_base.py b/mbed_host_tests/host_tests_runner/mbed_base.py index f5ee602..ffd77f4 100644 --- a/mbed_host_tests/host_tests_runner/mbed_base.py +++ b/mbed_host_tests/host_tests_runner/mbed_base.py @@ -17,10 +17,7 @@ Author: Przemyslaw Wirkus """ -import os import json -import time -import requests from time import sleep import mbed_host_tests.host_tests_plugins as ht_plugins From c2ae7faf60d70dba0ceb6ac60269fbc4926e81bb Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 12:51:40 +0100 Subject: [PATCH 09/13] removed debug --- mbed_host_tests/host_tests_conn_proxy/conn_proxy.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py b/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py index fca8cc1..3780662 100644 --- a/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py +++ b/mbed_host_tests/host_tests_conn_proxy/conn_proxy.py @@ -93,8 +93,7 @@ def write(self, payload, log=False): def write_kv(self, key, value): kv_buff = "{{%s;%s}}\n"% (key, value) self.write(kv_buff) - print "Written: %s" % kv_buff - #self.logger.prn_txd(kv_buff) + self.logger.prn_txd(kv_buff) return kv_buff def flush(self): From c8178bb7c1418cb312f2d9bfe38c57426776600f Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 14:39:31 +0100 Subject: [PATCH 10/13] Using self.print_plugin_error() instead of print statement --- .../host_tests_plugins/module_power_cycle_mbed.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py index bc332b1..5c0f590 100644 --- a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py +++ b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py @@ -74,7 +74,7 @@ def __hw_reset(self, target_id, device_info): ip = os.environ['MBED_TAS_RM_IP'] port = os.environ['MBED_TAS_RM_PORT'] except KeyError, e: - print "HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset." + self.print_plugin_error("HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset.") else: result = self.__reset_target(ip, port, target_id, device_info) return result @@ -118,11 +118,11 @@ def __reset_target(self, ip, port, target_id, device_info): # reset target switch_off_req = self.__run_request(ip, port, switch_off_req) if switch_off_req is None: - print "HOST: Failed to communicate with TAS RM!" + self.print_plugin_error("HOST: Failed to communicate with TAS RM!") return result if "error" in switch_off_req['sub_requests'][0]: - print "HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error'] + self.print_plugin_error("HOST: Failed to reset target. error = %s" % switch_off_req['sub_requests'][0]['error']) return result def poll_state(required_state): @@ -153,7 +153,7 @@ def poll_state(required_state): device_info[k] = v result = True else: - print "HOST: Failed to reset device %s" % target_id + self.print_plugin_error("HOST: Failed to reset device %s" % target_id) return result From 4010934ef915bd1cbe911fb3aa1668a0445e21e0 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 14:40:51 +0100 Subject: [PATCH 11/13] Fixed typo --- mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py index 5c0f590..e6dc553 100644 --- a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py +++ b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py @@ -65,7 +65,7 @@ def execute(self, capability, *args, **kwargs): def __hw_reset(self, target_id, device_info): """ - Performs hardware reset of target ned device. + Performs hardware reset of target mbed device. :return: """ From 00dc4a187e4b7a364ee79adda45d10927ea89b35 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 14:53:43 +0100 Subject: [PATCH 12/13] Moving out reading on MBED TAS RM IP and PORT env into separate method --- .../module_power_cycle_mbed.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py index e6dc553..ddff3ab 100644 --- a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py +++ b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py @@ -60,26 +60,28 @@ def execute(self, capability, *args, **kwargs): if capability in HostTestPluginPowerCycleResetMethod.capabilities: target_id = kwargs['target_id'] device_info = kwargs['device_info'] - result = self.__hw_reset(target_id, device_info) + ret = self.__get_mbed_tas_rm_addr() + if ret: + ip = ret[0] + port = ret[1] + result = self.__hw_reset(ip, port, target_id, device_info) return result - def __hw_reset(self, target_id, device_info): + def __get_mbed_tas_rm_addr(self): """ - Performs hardware reset of target mbed device. - + Get IP and Port of mbed tas rm service. :return: """ - result = False try: ip = os.environ['MBED_TAS_RM_IP'] port = os.environ['MBED_TAS_RM_PORT'] + return ip, port except KeyError, e: - self.print_plugin_error("HOST: Failed read environment variable (" + str(e) + "). Can't perform hardware reset.") - else: - result = self.__reset_target(ip, port, target_id, device_info) - return result + self.print_plugin_error("HOST: Failed to read environment variable (" + str(e) + "). Can't perform hardware reset.") + + return None - def __reset_target(self, ip, port, target_id, device_info): + def __hw_reset(self, ip, port, target_id, device_info): """ Reset target device using TAS RM API From 37ce6d6acb90c148a2c6bb85cb7f89e36649d3b1 Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Wed, 30 Mar 2016 15:50:46 +0100 Subject: [PATCH 13/13] Minor refactor --- mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py index ddff3ab..4c4a1df 100644 --- a/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py +++ b/mbed_host_tests/host_tests_plugins/module_power_cycle_mbed.py @@ -62,8 +62,7 @@ def execute(self, capability, *args, **kwargs): device_info = kwargs['device_info'] ret = self.__get_mbed_tas_rm_addr() if ret: - ip = ret[0] - port = ret[1] + ip, port = ret result = self.__hw_reset(ip, port, target_id, device_info) return result