conn_primitive_remote.py
Go to the documentation of this file.
#!/usr/bin/env python
"""
mbed SDK
Copyright (c) 2011-2016 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.
"""
from mbed_host_tests import DEFAULT_BAUD_RATE
from mbed_host_tests.host_tests_conn_proxy.conn_primitive import ConnectorPrimitive
class RemoteConnectorPrimitive(ConnectorPrimitive):
def __init__(self, name, config, importer=__import__):
ConnectorPrimitive.__init__(self, name)
self.configconfig = config
self.target_idtarget_id = self.configconfig.get('target_id', None)
self.grm_hostgrm_host = config.get('grm_host', None)
self.grm_portgrm_port = int(config.get('grm_port', 8000))
self.grm_modulegrm_module = config.get('grm_module', 'unknown')
self.platform_nameplatform_name = config.get('platform_name', None)
self.baudratebaudrate = config.get('baudrate', DEFAULT_BAUD_RATE)
self.image_pathimage_path = config.get('image_path', None)
self.allocate_requirementsallocate_requirements = {"platform_name": self.platform_nameplatform_name}
if self.configconfig.get("tags"):
self.allocate_requirementsallocate_requirements["tags"] = {}
for tag in config["tags"].split(','):
self.allocate_requirementsallocate_requirements["tags"][tag] = True
# Global Resource Mgr tool-kit
self.remote_moduleremote_module = None
self.selected_resourceselected_resource = None
self.clientclient = None
# Initialize remote resource manager
self.__remote_init__remote_init(importer)
def __remote_init(self, importer):
"""! Initialize DUT using GRM APIs """
# We want to load global resource manager module by name from command line (switch --grm)
try:
self.remote_moduleremote_module = importer(self.grm_modulegrm_module)
except ImportError as error:
self.loggerlogger.prn_err("unable to load global resource manager '%s' module!" % self.grm_modulegrm_module)
self.loggerlogger.prn_err(str(error))
self.remote_moduleremote_module = None
return False
self.loggerlogger.prn_inf("remote resources initialization: remote(host=%s, port=%s)" %
(self.grm_hostgrm_host, self.grm_portgrm_port))
# Connect to remote global resource manager
self.clientclient = self.remote_moduleremote_module.create(host=self.grm_hostgrm_host, port=self.grm_portgrm_port)
# First get the resources
resources = self.clientclient.get_resources()
self.loggerlogger.prn_inf("remote resources count: %d" % len(resources))
# Query for available resource
# Automatic selection and allocation of a resource
try:
self.selected_resourceselected_resource = self.clientclient.allocate(self.allocate_requirementsallocate_requirements)
except Exception as error:
self.loggerlogger.prn_err("can't allocate resource: '%s', reason: %s" % (self.platform_nameplatform_name, str(error)))
return False
# Remote DUT connection, flashing and reset...
try:
self.__remote_flashing__remote_flashing(self.image_pathimage_path, forceflash=True)
self.__remote_connect__remote_connect(baudrate=self.baudratebaudrate)
self.__remote_reset__remote_reset()
except Exception as error:
self.loggerlogger.prn_err(str(error))
self.__remote_release__remote_release()
return False
return True
def __remote_connect(self, baudrate=DEFAULT_BAUD_RATE):
"""! Open remote connection to DUT """
self.loggerlogger.prn_inf("opening connection to platform at baudrate='%s'" % baudrate)
if not self.selected_resourceselected_resource:
raise Exception("remote resource not exists!")
try:
serial_parameters = self.remote_moduleremote_module.SerialParameters(baudrate=baudrate)
self.selected_resourceselected_resource.open_connection(parameters=serial_parameters)
except Exception:
self.loggerlogger.prn_inf("open_connection() failed")
raise
def __remote_disconnect(self):
if not self.selected_resourceselected_resource:
raise Exception("remote resource not exists!")
try:
if self.connected connectedconnected():
self.selected_resourceselected_resource.close_connection()
except Exception as error:
self.loggerlogger.prn_err("RemoteConnectorPrimitive.disconnect() failed, reason: " + str(error))
def __remote_reset(self):
"""! Use GRM remote API to reset DUT """
self.loggerlogger.prn_inf("remote resources reset...")
if not self.selected_resourceselected_resource:
raise Exception("remote resource not exists!")
try:
if self.selected_resourceselected_resource.reset() is False:
raise Exception("remote resources reset failed!")
except Exception:
self.loggerlogger.prn_inf("reset() failed")
raise
def __remote_flashing(self, filename, forceflash=False):
"""! Use GRM remote API to flash DUT """
self.loggerlogger.prn_inf("remote resources flashing with '%s'..." % filename)
if not self.selected_resourceselected_resource:
raise Exception("remote resource not exists!")
try:
if self.selected_resourceselected_resource.flash(filename, forceflash=forceflash) is False:
raise Exception("remote resource flashing failed!")
except Exception:
self.loggerlogger.prn_inf("flash() failed")
raise
def read(self, count):
"""! Read 'count' bytes of data from DUT """
if not self.connected connectedconnected():
raise Exception("remote resource not exists!")
data = str()
try:
data = self.selected_resourceselected_resource.read(count)
except Exception as error:
self.loggerlogger.prn_err("RemoteConnectorPrimitive.read(%d): %s" % (count, str(error)))
return data
def write(self, payload, log=False):
"""! Write 'payload' to DUT """
if self.connected connectedconnected():
try:
self.selected_resourceselected_resource.write(payload)
if log:
self.loggerlogger.prn_txd(payload)
return True
except Exception as error:
self.LAST_ERROR LAST_ERRORLAST_ERROR = "remote write error: %s" % str(error)
self.loggerlogger.prn_err(str(error))
return False
def flush(self):
pass
def allocated(self):
return self.remote_module and self.selected_resource and self.selected_resource.is_allocated
def connected(self):
return self.allocatedallocated() and self.selected_resourceselected_resource.is_connected
def __remote_release(self):
try:
if self.allocatedallocated():
self.selected_resourceselected_resource.release()
self.selected_resourceselected_resource = None
except Exception as error:
self.loggerlogger.prn_err("RemoteConnectorPrimitive.release failed, reason: " + str(error))
def finish(self):
# Finally once we're done with the resource
# we disconnect and release the allocation
if self.allocatedallocated():
self.__remote_disconnect__remote_disconnect()
self.__remote_release__remote_release()
def reset(self):
self.__remote_reset__remote_reset()
def __del__(self):
self.finish finishfinish()
Exception
mbed_host_tests.host_tests_conn_proxy.conn_primitive.ConnectorPrimitive
Definition: conn_primitive.py:29
mbed_host_tests.host_tests_conn_proxy.conn_primitive.ConnectorPrimitive.LAST_ERROR
LAST_ERROR
Definition: conn_primitive.py:32
mbed_host_tests.host_tests_conn_proxy.conn_primitive.ConnectorPrimitive.finish
def finish(self)
Handle DUT dtor like (close resource) operations here.
Definition: conn_primitive.py:87
mbed_host_tests.host_tests_conn_proxy.conn_primitive.ConnectorPrimitive.logger
logger
Definition: conn_primitive.py:33
mbed_host_tests.host_tests_conn_proxy.conn_primitive.ConnectorPrimitive.connected
def connected(self)
Check if there is a connection to DUT.
Definition: conn_primitive.py:75
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive
Definition: conn_primitive_remote.py:23
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__del__
def __del__(self)
Definition: conn_primitive_remote.py:186
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.read
def read(self, count)
Read 'count' bytes of data from DUT.
Definition: conn_primitive_remote.py:135
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.LAST_ERROR
LAST_ERROR
Definition: conn_primitive_remote.py:155
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.finish
def finish(self)
Handle DUT dtor like (close resource) operations here.
Definition: conn_primitive_remote.py:176
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.selected_resource
selected_resource
Definition: conn_primitive_remote.py:43
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.connected
def connected(self)
Check if there is a connection to DUT.
Definition: conn_primitive_remote.py:165
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.image_path
image_path
Definition: conn_primitive_remote.py:33
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__remote_reset
def __remote_reset(self)
Definition: conn_primitive_remote.py:111
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.allocate_requirements
allocate_requirements
Definition: conn_primitive_remote.py:34
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.platform_name
platform_name
Definition: conn_primitive_remote.py:31
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.grm_module
grm_module
Definition: conn_primitive_remote.py:30
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__remote_init
def __remote_init(self, importer)
Definition: conn_primitive_remote.py:49
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.reset
def reset(self)
Reset the dut.
Definition: conn_primitive_remote.py:183
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.client
client
Definition: conn_primitive_remote.py:44
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__remote_disconnect
def __remote_disconnect(self)
Definition: conn_primitive_remote.py:102
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__init__
def __init__(self, name, config, importer=__import__)
Definition: conn_primitive_remote.py:24
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.target_id
target_id
Definition: conn_primitive_remote.py:27
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.grm_host
grm_host
Definition: conn_primitive_remote.py:28
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__remote_connect
def __remote_connect(self, baudrate=DEFAULT_BAUD_RATE)
Definition: conn_primitive_remote.py:90
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.grm_port
grm_port
Definition: conn_primitive_remote.py:29
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.flush
def flush(self)
Flush read/write channels of DUT.
Definition: conn_primitive_remote.py:159
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.write
def write(self, payload, log=False)
Write 'payload' to DUT.
Definition: conn_primitive_remote.py:146
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__remote_release
def __remote_release(self)
Definition: conn_primitive_remote.py:168
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.config
config
Definition: conn_primitive_remote.py:26
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.baudrate
baudrate
Definition: conn_primitive_remote.py:32
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.__remote_flashing
def __remote_flashing(self, filename, forceflash=False)
Definition: conn_primitive_remote.py:123
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.allocated
def allocated(self)
Definition: conn_primitive_remote.py:162
mbed_host_tests.host_tests_conn_proxy.conn_primitive_remote.RemoteConnectorPrimitive.remote_module
remote_module
Definition: conn_primitive_remote.py:42
mbed_host_tests.host_tests_conn_proxy.conn_primitive
Definition: conn_primitive.py:1