Mbed Host Tests
conn_primitive_fastmodel.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""
3mbed SDK
4Copyright (c) 2011-2018 ARM Limited
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17"""
18import telnetlib
19import socket
20from .conn_primitive import ConnectorPrimitive, ConnectorPrimitiveException
21
22
23class FastmodelConnectorPrimitive(ConnectorPrimitive):
24 def __init__(self, name, config):
25 ConnectorPrimitive.__init__(self, name)
26 self.config = config
27 self.fm_config = config.get('fm_config', None)
28 self.platform_name = config.get('platform_name', None)
29 self.image_path = config.get('image_path', None)
30 self.polling_timeoutpolling_timeout = int(config.get('polling_timeout', 60))
31
32 # FastModel Agent tool-kit
33 self.fm_agent_module = None
34 self.resource = None
35
36 # Initialize FastModel
37 if self.__fastmodel_init():
38
39 # FastModel Launch load and run, equivalent to DUT connection, flashing and reset...
42 self.__fastmodel_run()
43
44
45 def __fastmodel_init(self):
46 """! Initialize models using fm_agent APIs """
47 self.logger.prn_inf("Initializing FastModel...")
48
49 try:
50 self.fm_agent_module = __import__("fm_agent")
51 except ImportError as e:
52 self.logger.prn_err("unable to load mbed-fastmodel-agent module. Check if the module install correctly.")
53 self.fm_agent_module = None
54 self.logger.prn_err("Importing failed : %s" % str(e))
55 raise ConnectorPrimitiveException("Importing failed : %s" % str(e))
56 try:
57 self.resource = self.fm_agent_module.FastmodelAgent(logger=self.logger)
58 self.resource.setup_simulator(self.platform_name,self.fm_config)
59 if self.__resource_allocated():
60 pass
61 except self.fm_agent_module.SimulatorError as e:
62 self.logger.prn_err("module fm_agent, create() failed: %s"% str(e))
63 raise ConnectorPrimitiveException("FastModel Initializing failed as throw SimulatorError!")
64
65 return True
66
67 def __fastmodel_launch(self):
68 """! launch the FastModel"""
69 self.logger.prn_inf("Launching FastModel...")
70 try:
71 if not self.resource.start_simulator():
72 raise ConnectorPrimitiveException("FastModel running failed, run_simulator() return False!")
73 except self.fm_agent_module.SimulatorError as e:
74 self.logger.prn_err("start_simulator() failed: %s"% str(e))
75 raise ConnectorPrimitiveException("FastModel launching failed as throw FastModelError!")
76
77 def __fastmodel_run(self):
78 """! Use fm_agent API to run the FastModel """
79 self.logger.prn_inf("Running FastModel...")
80 try:
81 if not self.resource.run_simulator():
82 raise ConnectorPrimitiveException("FastModel running failed, run_simulator() return False!")
83 except self.fm_agent_module.SimulatorError as e:
84 self.logger.prn_err("run_simulator() failed: %s"% str(e))
85 raise ConnectorPrimitiveException("FastModel running failed as throw SimulatorError!")
86
87 def __fastmodel_load(self, filename):
88 """! Use fm_agent API to load image to FastModel, this is functional equivalent to flashing DUT"""
89 self.logger.prn_inf("loading FastModel with image '%s'..."% filename)
90 try:
91 if not self.resource.load_simulator(filename):
92 raise ConnectorPrimitiveException("FastModel loading failed, load_simulator() return False!")
93 except self.fm_agent_module.SimulatorError as e:
94 self.logger.prn_err("run_simulator() failed: %s"% str(e))
95 raise ConnectorPrimitiveException("FastModel loading failed as throw SimulatorError!")
96
97 def __resource_allocated(self):
98 """! Check whether FastModel resource been allocated
99 @return True or throw an exception
100 """
101 if self.resource:
102 return True
103 else:
104 self.logger.prn_err("FastModel resource not available!")
105 return False
106
107 def read(self, count):
108 """! Read data from DUT, count is not used for FastModel"""
109 date = str()
110 if self.__resource_allocated():
111 try:
112 data = self.resource.read()
113 except self.fm_agent_module.SimulatorError as e:
114 self.logger.prn_err("FastmodelConnectorPrimitive.read() failed: %s"% str(e))
115 else:
116 return data
117 else:
118 return False
119 def write(self, payload, log=False):
120 """! Write 'payload' to DUT"""
121 if self.__resource_allocated():
122 if log:
123 self.logger.prn_txd(payload)
124 try:
125 self.resource.write(payload)
126 except self.fm_agent_module.SimulatorError as e:
127 self.logger.prn_err("FastmodelConnectorPrimitive.write() failed: %s"% str(e))
128 else:
129 return True
130 else:
131 return False
132
133 def flush(self):
134 """! flush not supported in FastModel_module"""
135 pass
136
137 def connected(self):
138 """! return whether FastModel is connected """
139 if self.__resource_allocated():
140 return self.resource.is_simulator_alive
141 else:
142 return False
143
144 def finish(self):
145 """! shutdown the FastModel and release the allocation """
146 if self.__resource_allocated():
147 try:
148 self.resource.shutdown_simulator()
149 self.resource = None
150 except self.fm_agent_module.SimulatorError as e:
151 self.logger.prn_err("FastmodelConnectorPrimitive.finish() failed: %s"% str(e))
152
153 def reset(self):
154 if self.__resource_allocated():
155 try:
156 if not self.resource.reset_simulator():
157 self.logger.prn_err("FastModel reset failed, reset_simulator() return False!")
158 except self.fm_agent_module.SimulatorError as e:
159 self.logger.prn_err("FastmodelConnectorPrimitive.reset() failed: %s"% str(e))
160
161 def __del__(self):
162 self.finishfinish()
def finish(self)
Handle DUT dtor like (close resource) operations here.
def read(self, count)
Read data from DUT, count is not used for FastModel.