conn_primitive_fastmodel.py
Go to the documentation of this file.
#!/usr/bin/env python
"""
mbed SDK
Copyright (c) 2011-2018 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 telnetlib
import socket
from .conn_primitive import ConnectorPrimitive, ConnectorPrimitiveException
class FastmodelConnectorPrimitive(ConnectorPrimitive):
def __init__(self, name, config):
ConnectorPrimitive.__init__(self, name)
self.configconfig = config
self.fm_configfm_config = config.get('fm_config', None)
self.platform_nameplatform_name = config.get('platform_name', None)
self.image_pathimage_path = config.get('image_path', None)
self.polling_timeout polling_timeoutpolling_timeout = int(config.get('polling_timeout', 60))
# FastModel Agent tool-kit
self.fm_agent_modulefm_agent_module = None
self.resourceresource = None
# Initialize FastModel
if self.__fastmodel_init__fastmodel_init():
# FastModel Launch load and run, equivalent to DUT connection, flashing and reset...
self.__fastmodel_launch__fastmodel_launch()
self.__fastmodel_load__fastmodel_load(self.image_pathimage_path)
self.__fastmodel_run__fastmodel_run()
def __fastmodel_init(self):
"""! Initialize models using fm_agent APIs """
self.loggerlogger.prn_inf("Initializing FastModel...")
try:
self.fm_agent_modulefm_agent_module = __import__("fm_agent")
except ImportError as e:
self.loggerlogger.prn_err("unable to load mbed-fastmodel-agent module. Check if the module install correctly.")
self.fm_agent_modulefm_agent_module = None
self.loggerlogger.prn_err("Importing failed : %s" % str(e))
raise ConnectorPrimitiveException("Importing failed : %s" % str(e))
try:
self.resourceresource = self.fm_agent_modulefm_agent_module.FastmodelAgent(logger=self.loggerlogger)
self.resourceresource.setup_simulator(self.platform_nameplatform_name,self.fm_configfm_config)
if self.__resource_allocated__resource_allocated():
pass
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("module fm_agent, create() failed: %s"% str(e))
raise ConnectorPrimitiveException("FastModel Initializing failed as throw SimulatorError!")
return True
def __fastmodel_launch(self):
"""! launch the FastModel"""
self.loggerlogger.prn_inf("Launching FastModel...")
try:
if not self.resourceresource.start_simulator():
raise ConnectorPrimitiveException("FastModel running failed, run_simulator() return False!")
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("start_simulator() failed: %s"% str(e))
raise ConnectorPrimitiveException("FastModel launching failed as throw FastModelError!")
def __fastmodel_run(self):
"""! Use fm_agent API to run the FastModel """
self.loggerlogger.prn_inf("Running FastModel...")
try:
if not self.resourceresource.run_simulator():
raise ConnectorPrimitiveException("FastModel running failed, run_simulator() return False!")
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("run_simulator() failed: %s"% str(e))
raise ConnectorPrimitiveException("FastModel running failed as throw SimulatorError!")
def __fastmodel_load(self, filename):
"""! Use fm_agent API to load image to FastModel, this is functional equivalent to flashing DUT"""
self.loggerlogger.prn_inf("loading FastModel with image '%s'..."% filename)
try:
if not self.resourceresource.load_simulator(filename):
raise ConnectorPrimitiveException("FastModel loading failed, load_simulator() return False!")
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("run_simulator() failed: %s"% str(e))
raise ConnectorPrimitiveException("FastModel loading failed as throw SimulatorError!")
def __resource_allocated(self):
"""! Check whether FastModel resource been allocated
@return True or throw an exception
"""
if self.resourceresource:
return True
else:
self.loggerlogger.prn_err("FastModel resource not available!")
return False
def read(self, count):
"""! Read data from DUT, count is not used for FastModel"""
date = str()
if self.__resource_allocated__resource_allocated():
try:
data = self.resourceresource.read()
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("FastmodelConnectorPrimitive.read() failed: %s"% str(e))
else:
return data
else:
return False
def write(self, payload, log=False):
"""! Write 'payload' to DUT"""
if self.__resource_allocated__resource_allocated():
if log:
self.loggerlogger.prn_txd(payload)
try:
self.resourceresource.write(payload)
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("FastmodelConnectorPrimitive.write() failed: %s"% str(e))
else:
return True
else:
return False
def flush(self):
"""! flush not supported in FastModel_module"""
pass
def connected(self):
"""! return whether FastModel is connected """
if self.__resource_allocated():
return self.resource.is_simulator_alive
else:
return False
def finish(self):
"""! shutdown the FastModel and release the allocation """
if self.__resource_allocated__resource_allocated():
try:
self.resourceresource.shutdown_simulator()
self.resourceresource = None
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("FastmodelConnectorPrimitive.finish() failed: %s"% str(e))
def reset(self):
if self.__resource_allocated__resource_allocated():
try:
if not self.resourceresource.reset_simulator():
self.loggerlogger.prn_err("FastModel reset failed, reset_simulator() return False!")
except self.fm_agent_modulefm_agent_module.SimulatorError as e:
self.loggerlogger.prn_err("FastmodelConnectorPrimitive.reset() failed: %s"% str(e))
def __del__(self):
self.finish finishfinish()
mbed_host_tests.host_tests_conn_proxy.conn_primitive.ConnectorPrimitiveException
Definition: conn_primitive.py:22
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.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.polling_timeout
polling_timeout
Definition: conn_primitive.py:34
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_fastmodel.FastmodelConnectorPrimitive
Definition: conn_primitive_fastmodel.py:23
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.image_path
image_path
Definition: conn_primitive_fastmodel.py:29
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.read
def read(self, count)
Read data from DUT, count is not used for FastModel.
Definition: conn_primitive_fastmodel.py:107
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.flush
def flush(self)
flush not supported in FastModel_module
Definition: conn_primitive_fastmodel.py:133
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.connected
def connected(self)
return whether FastModel is connected
Definition: conn_primitive_fastmodel.py:137
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__fastmodel_run
def __fastmodel_run(self)
Definition: conn_primitive_fastmodel.py:77
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__fastmodel_load
def __fastmodel_load(self, filename)
Definition: conn_primitive_fastmodel.py:87
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.fm_agent_module
fm_agent_module
Definition: conn_primitive_fastmodel.py:33
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__fastmodel_init
def __fastmodel_init(self)
Definition: conn_primitive_fastmodel.py:45
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.resource
resource
Definition: conn_primitive_fastmodel.py:34
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.fm_config
fm_config
Definition: conn_primitive_fastmodel.py:27
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.config
config
Definition: conn_primitive_fastmodel.py:26
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__init__
def __init__(self, name, config)
Definition: conn_primitive_fastmodel.py:24
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__fastmodel_launch
def __fastmodel_launch(self)
Definition: conn_primitive_fastmodel.py:67
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__resource_allocated
def __resource_allocated(self)
Definition: conn_primitive_fastmodel.py:97
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.platform_name
platform_name
Definition: conn_primitive_fastmodel.py:28
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.write
def write(self, payload, log=False)
Write 'payload' to DUT.
Definition: conn_primitive_fastmodel.py:119
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.polling_timeout
polling_timeout
Definition: conn_primitive_fastmodel.py:30
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.finish
def finish(self)
shutdown the FastModel and release the allocation
Definition: conn_primitive_fastmodel.py:144
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.__del__
def __del__(self)
Definition: conn_primitive_fastmodel.py:161
mbed_host_tests.host_tests_conn_proxy.conn_primitive_fastmodel.FastmodelConnectorPrimitive.reset
def reset(self)
Reset the dut.
Definition: conn_primitive_fastmodel.py:153