4 Copyright (c) 2011-2016 ARM Limited
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
19 from past.builtins
import basestring
26 from .conn_primitive_serial
import SerialConnectorPrimitive
27 from .conn_primitive_remote
import RemoteConnectorPrimitive
28 from .conn_primitive_fastmodel
import FastmodelConnectorPrimitive
30 if (sys.version_info > (3, 0)):
31 from queue
import Empty
as QueueEmpty
33 from Queue
import Empty
as QueueEmpty
36 """! Simple auxiliary class used to walk through a buffer and search for KV tokens """
38 self.
KIVI_REGEXKIVI_REGEX =
r"\{\{([\w\d_-]+);([^\}]+)\}\}"
44 """! Append stream buffer with payload and process. Returns non-KV strings"""
45 logger = HtrunLogger(
'CONN')
47 self.
buffbuff += payload.decode(
'utf-8')
48 except UnicodeDecodeError:
49 logger.prn_wrn(
"UnicodeDecodeError encountered!")
50 self.
buffbuff += payload.decode(
'utf-8',
'ignore')
51 lines = self.
buffbuff.split(
'\n')
52 self.
buffbuff = lines[-1]
60 (key, value) = m.groups()
64 pos = line.find(match)
66 after = line[pos + len(match):]
68 discarded.append(before)
71 discarded.append(after)
74 discarded.append(line)
78 """! Check if there is a KV value in buffer """
79 return len(self.
kvlkvl) > 0
83 return self.
kvlkvl.pop(0)
84 return None,
None, time()
88 """! Factory producing connectors based on type and config
89 @param conn_resource Name of connection primitive (e.g. 'serial' for
90 local serial port connection or 'grm' for global resource manager)
91 @param event_queue Even queue of Key-Value protocol
92 @param config Global configuration for connection process
93 @param logger Host Test logger instance
94 @return Object of type <ConnectorPrimitive> or None if type of connection primitive unknown (conn_resource)
96 polling_timeout = int(config.get(
'polling_timeout', 60))
97 logger.prn_inf(
"notify event queue about extra %d sec timeout for serial port pooling"%polling_timeout)
98 event_queue.put((
'__timeout', polling_timeout, time()))
100 if conn_resource ==
'serial':
105 port = config.get(
'port')
106 baudrate = config.get(
'baudrate')
108 logger.prn_inf(
"initializing serial port listener... ")
114 elif conn_resource ==
'grm':
116 logger.prn_inf(
"initializing global resource mgr listener... ")
118 elif conn_resource ==
'fmc':
120 logger.prn_inf(
"initializing fast model connection")
123 logger.pn_err(
"unknown connection resource!")
124 raise NotImplementedError(
"ConnectorPrimitive factory: unknown connection resource '%s'!"% conn_resource)
131 def __notify_conn_lost():
132 error_msg = connector.error()
134 event_queue.put((
'__notify_conn_lost', error_msg, time()))
136 def __notify_sync_failed():
137 error_msg = connector.error()
139 event_queue.put((
'__notify_sync_failed', error_msg, time()))
141 logger = HtrunLogger(
'CONN')
142 logger.prn_inf(
"starting connection process...")
146 event_queue.put((
'__conn_process_start', 1, time()))
149 sync_behavior = int(config.get(
'sync_behavior', 1))
150 sync_timeout = config.get(
'sync_timeout', 1.0)
151 conn_resource = config.get(
'conn_resource',
'serial')
158 if not connector.connected():
159 logger.prn_err(
"Failed to connect to resource")
170 sync_uuid_discovered =
False
172 def __send_sync(timeout=None):
173 sync_uuid = str(uuid.uuid4())
176 logger.prn_inf(
"Reset the part and send in new preamble...")
178 logger.prn_inf(
"resending new preamble '%s' after %0.2f sec"% (sync_uuid, timeout))
180 logger.prn_inf(
"sending preamble '%s'"% sync_uuid)
182 if connector.write_kv(
'__sync', sync_uuid):
188 if not connector.write(
"mbed" * 10, log=
True):
204 if sync_behavior > 0:
206 logger.prn_inf(
"sending up to %s __sync packets (specified with --sync=%s)"% (sync_behavior, sync_behavior))
207 sync_uuid = __send_sync()
210 sync_uuid_list.append(sync_uuid)
215 elif sync_behavior == 0:
217 logger.prn_wrn(
"skipping __sync packet (specified with --sync=%s)"% sync_behavior)
220 logger.prn_inf(
"sending multiple __sync packets (specified with --sync=%s)"% sync_behavior)
222 sync_uuid = __send_sync()
224 sync_uuid_list.append(sync_uuid)
234 if not connector.connected():
240 (key, value, _) = dut_event_queue.get(block=
False)
245 if key ==
'__host_test_finished' and value ==
True:
246 logger.prn_inf(
"received special event '%s' value='%s', finishing"% (key, value))
249 elif key ==
'__reset':
250 logger.prn_inf(
"received special event '%s', resetting dut" % (key))
252 event_queue.put((
"reset_complete", 0, time()))
253 elif not connector.write_kv(key, value):
254 connector.write_kv(key, value)
259 data = connector.read(2304)
262 print_lines = kv_buffer.append(data)
263 for line
in print_lines:
265 event_queue.put((
'__rxd_line', line, time()))
266 while kv_buffer.search():
267 key, value, timestamp = kv_buffer.pop_kv()
269 if sync_uuid_discovered:
270 event_queue.put((key, value, timestamp))
271 logger.prn_inf(
"found KV pair in stream: {{%s;%s}}, queued..."% (key, value))
274 if value
in sync_uuid_list:
275 sync_uuid_discovered =
True
276 event_queue.put((key, value, time()))
277 idx = sync_uuid_list.index(value)
278 logger.prn_inf(
"found SYNC in stream: {{%s;%s}} it is #%d sent, queued..."% (key, value, idx))
280 logger.prn_err(
"found faulty SYNC in stream: {{%s;%s}}, ignored..."% (key, value))
281 logger.prn_inf(
"Resetting the part and sync timeout to clear out the buffer...")
285 logger.prn_wrn(
"found KV pair in stream: {{%s;%s}}, ignoring..."% (key, value))
287 if not sync_uuid_discovered:
294 if sync_behavior != 0:
295 time_to_sync_again = time() - loop_timer
296 if time_to_sync_again > sync_timeout:
297 sync_uuid = __send_sync(timeout=time_to_sync_again)
300 sync_uuid_list.append(sync_uuid)
305 if sync_behavior == 0:
310 elif last_sync ==
True:
312 __notify_sync_failed()
Simple auxiliary class used to walk through a buffer and search for KV tokens.
def search(self)
Check if there is a KV value in buffer.
def append(self, payload)
Append stream buffer with payload and process.
def conn_primitive_factory(conn_resource, config, event_queue, logger)
Factory producing connectors based on type and config.
def conn_process(event_queue, dut_event_queue, config)