DUT.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. DUT provides 3 major groups of features:
  16. * DUT port feature, provide basic open/close/read/write features
  17. * DUT tools, provide extra methods to control the device, like download and start app
  18. * DUT expect method, provide features for users to check DUT outputs
  19. The current design of DUT have 3 classes for one DUT: BaseDUT, DUTPort, DUTTool.
  20. * BaseDUT class:
  21. * defines methods DUT port and DUT tool need to overwrite
  22. * provide the expect methods and some other methods based on DUTPort
  23. * DUTPort class:
  24. * inherent from BaseDUT class
  25. * implements the port features by overwriting port methods defined in BaseDUT
  26. * DUTTool class:
  27. * inherent from one of the DUTPort class
  28. * implements the tools features by overwriting tool methods defined in BaseDUT
  29. * could add some new methods provided by the tool
  30. This module implements the BaseDUT class and one of the port class SerialDUT.
  31. User should implement their DUTTool classes.
  32. If they using different port then need to implement their DUTPort class as well.
  33. """
  34. from __future__ import print_function
  35. import time
  36. import re
  37. import threading
  38. import copy
  39. import sys
  40. import functools
  41. import serial
  42. from serial.tools import list_ports
  43. import Utility
  44. if sys.version_info[0] == 2:
  45. import Queue as _queue
  46. else:
  47. import queue as _queue
  48. class ExpectTimeout(ValueError):
  49. """ timeout for expect method """
  50. pass
  51. class UnsupportedExpectItem(ValueError):
  52. """ expect item not supported by the expect method """
  53. pass
  54. def _expect_lock(func):
  55. @functools.wraps(func)
  56. def handler(self, *args, **kwargs):
  57. with self.expect_lock:
  58. ret = func(self, *args, **kwargs)
  59. return ret
  60. return handler
  61. def _decode_data(data):
  62. """ for python3, if the data is bytes, then decode it to string """
  63. if isinstance(data, bytes):
  64. # convert bytes to string
  65. try:
  66. data = data.decode("utf-8", "ignore")
  67. except UnicodeDecodeError:
  68. data = data.decode("iso8859-1", )
  69. return data
  70. def _pattern_to_string(pattern):
  71. try:
  72. ret = "RegEx: " + pattern.pattern
  73. except AttributeError:
  74. ret = pattern
  75. return ret
  76. class _DataCache(_queue.Queue):
  77. """
  78. Data cache based on Queue. Allow users to process data cache based on bytes instead of Queue."
  79. """
  80. def __init__(self, maxsize=0):
  81. _queue.Queue.__init__(self, maxsize=maxsize)
  82. self.data_cache = str()
  83. def _move_from_queue_to_cache(self):
  84. """
  85. move all of the available data in the queue to cache
  86. :return: True if moved any item from queue to data cache, else False
  87. """
  88. ret = False
  89. while True:
  90. try:
  91. self.data_cache += _decode_data(self.get(0))
  92. ret = True
  93. except _queue.Empty:
  94. break
  95. return ret
  96. def get_data(self, timeout=0.0):
  97. """
  98. get a copy of data from cache.
  99. :param timeout: timeout for waiting new queue item
  100. :return: copy of data cache
  101. """
  102. # make sure timeout is non-negative
  103. if timeout < 0:
  104. timeout = 0
  105. ret = self._move_from_queue_to_cache()
  106. if not ret:
  107. # we only wait for new data if we can't provide a new data_cache
  108. try:
  109. data = self.get(timeout=timeout)
  110. self.data_cache += _decode_data(data)
  111. except _queue.Empty:
  112. # don't do anything when on update for cache
  113. pass
  114. return copy.deepcopy(self.data_cache)
  115. def flush(self, index=0xFFFFFFFF):
  116. """
  117. flush data from cache.
  118. :param index: if < 0 then don't do flush, otherwise flush data before index
  119. :return: None
  120. """
  121. # first add data in queue to cache
  122. self.get_data()
  123. if index > 0:
  124. self.data_cache = self.data_cache[index:]
  125. class _LogThread(threading.Thread, _queue.Queue):
  126. """
  127. We found some SD card on Raspberry Pi could have very bad performance.
  128. It could take seconds to save small amount of data.
  129. If the DUT receives data and save it as log, then it stops receiving data until log is saved.
  130. This could lead to expect timeout.
  131. As an workaround to this issue, ``BaseDUT`` class will create a thread to save logs.
  132. Then data will be passed to ``expect`` as soon as received.
  133. """
  134. def __init__(self):
  135. threading.Thread.__init__(self, name="LogThread")
  136. _queue.Queue.__init__(self, maxsize=0)
  137. self.setDaemon(True)
  138. self.flush_lock = threading.Lock()
  139. def save_log(self, filename, data):
  140. """
  141. :param filename: log file name
  142. :param data: log data. Must be ``bytes``.
  143. """
  144. self.put({"filename": filename, "data": data})
  145. def flush_data(self):
  146. with self.flush_lock:
  147. data_cache = dict()
  148. while True:
  149. # move all data from queue to data cache
  150. try:
  151. log = self.get_nowait()
  152. try:
  153. data_cache[log["filename"]] += log["data"]
  154. except KeyError:
  155. data_cache[log["filename"]] = log["data"]
  156. except _queue.Empty:
  157. break
  158. # flush data
  159. for filename in data_cache:
  160. with open(filename, "ab+") as f:
  161. f.write(data_cache[filename])
  162. def run(self):
  163. while True:
  164. time.sleep(1)
  165. self.flush_data()
  166. class _RecvThread(threading.Thread):
  167. PERFORMANCE_PATTERN = re.compile(r"\[Performance]\[(\w+)]: ([^\r\n]+)\r?\n")
  168. def __init__(self, read, data_cache, recorded_data, record_data_lock):
  169. super(_RecvThread, self).__init__()
  170. self.exit_event = threading.Event()
  171. self.setDaemon(True)
  172. self.read = read
  173. self.data_cache = data_cache
  174. self.recorded_data = recorded_data
  175. self.record_data_lock = record_data_lock
  176. # cache the last line of recv data for collecting performance
  177. self._line_cache = str()
  178. def collect_performance(self, data):
  179. """ collect performance """
  180. if data:
  181. decoded_data = _decode_data(data)
  182. matches = self.PERFORMANCE_PATTERN.findall(self._line_cache + decoded_data)
  183. for match in matches:
  184. Utility.console_log("[Performance][{}]: {}".format(match[0], match[1]),
  185. color="orange")
  186. # cache incomplete line to later process
  187. lines = decoded_data.splitlines(True)
  188. last_line = lines[-1]
  189. if last_line[-1] != "\n":
  190. if len(lines) == 1:
  191. # only one line and the line is not finished, then append this to cache
  192. self._line_cache += lines[-1]
  193. else:
  194. # more than one line and not finished, replace line cache
  195. self._line_cache = lines[-1]
  196. else:
  197. # line finishes, flush cache
  198. self._line_cache = str()
  199. def run(self):
  200. while not self.exit_event.isSet():
  201. data = self.read(1000)
  202. if data:
  203. with self.record_data_lock:
  204. self.data_cache.put(data)
  205. for capture_id in self.recorded_data:
  206. self.recorded_data[capture_id].put(data)
  207. self.collect_performance(data)
  208. def exit(self):
  209. self.exit_event.set()
  210. self.join()
  211. class BaseDUT(object):
  212. """
  213. :param name: application defined name for port
  214. :param port: comport name, used to create DUT port
  215. :param log_file: log file name
  216. :param app: test app instance
  217. :param kwargs: extra args for DUT to create ports
  218. """
  219. DEFAULT_EXPECT_TIMEOUT = 10
  220. MAX_EXPECT_FAILURES_TO_SAVED = 10
  221. LOG_THREAD = _LogThread()
  222. LOG_THREAD.start()
  223. def __init__(self, name, port, log_file, app, **kwargs):
  224. self.expect_lock = threading.Lock()
  225. self.name = name
  226. self.port = port
  227. self.log_file = log_file
  228. self.app = app
  229. self.data_cache = _DataCache()
  230. # the main process of recorded data are done in receive thread
  231. # but receive thread could be closed in DUT lifetime (tool methods)
  232. # so we keep it in BaseDUT, as their life cycle are same
  233. self.recorded_data = dict()
  234. self.record_data_lock = threading.RLock()
  235. self.receive_thread = None
  236. self.expect_failures = []
  237. self._port_open()
  238. self.start_receive()
  239. def __str__(self):
  240. return "DUT({}: {})".format(self.name, str(self.port))
  241. def _save_expect_failure(self, pattern, data, start_time):
  242. """
  243. Save expect failure. If the test fails, then it will print the expect failures.
  244. In some cases, user will handle expect exceptions.
  245. The expect failures could be false alarm, and test case might generate a lot of such failures.
  246. Therefore, we don't print the failure immediately and limit the max size of failure list.
  247. """
  248. self.expect_failures.insert(0, {"pattern": pattern, "data": data,
  249. "start": start_time, "end": time.time()})
  250. self.expect_failures = self.expect_failures[:self.MAX_EXPECT_FAILURES_TO_SAVED]
  251. def _save_dut_log(self, data):
  252. """
  253. Save DUT log into file using another thread.
  254. This is a workaround for some devices takes long time for file system operations.
  255. See descriptions in ``_LogThread`` for details.
  256. """
  257. self.LOG_THREAD.save_log(self.log_file, data)
  258. # define for methods need to be overwritten by Port
  259. @classmethod
  260. def list_available_ports(cls):
  261. """
  262. list all available ports.
  263. subclass (port) must overwrite this method.
  264. :return: list of available comports
  265. """
  266. pass
  267. def _port_open(self):
  268. """
  269. open the port.
  270. subclass (port) must overwrite this method.
  271. :return: None
  272. """
  273. pass
  274. def _port_read(self, size=1):
  275. """
  276. read form port. This method should not blocking for long time, otherwise receive thread can not exit.
  277. subclass (port) must overwrite this method.
  278. :param size: max size to read.
  279. :return: read data.
  280. """
  281. pass
  282. def _port_write(self, data):
  283. """
  284. write to port.
  285. subclass (port) must overwrite this method.
  286. :param data: data to write
  287. :return: None
  288. """
  289. pass
  290. def _port_close(self):
  291. """
  292. close port.
  293. subclass (port) must overwrite this method.
  294. :return: None
  295. """
  296. pass
  297. # methods that need to be overwritten by Tool
  298. @classmethod
  299. def confirm_dut(cls, port, app, **kwargs):
  300. """
  301. confirm if it's a DUT, usually used by auto detecting DUT in by Env config.
  302. subclass (tool) must overwrite this method.
  303. :param port: comport
  304. :param app: app instance
  305. :return: True or False
  306. """
  307. pass
  308. def start_app(self):
  309. """
  310. usually after we got DUT, we need to do some extra works to let App start.
  311. For example, we need to reset->download->reset to let IDF application start on DUT.
  312. subclass (tool) must overwrite this method.
  313. :return: None
  314. """
  315. pass
  316. # methods that features raw port methods
  317. def start_receive(self):
  318. """
  319. Start thread to receive data.
  320. :return: None
  321. """
  322. self.receive_thread = _RecvThread(self._port_read, self.data_cache,
  323. self.recorded_data, self.record_data_lock)
  324. self.receive_thread.start()
  325. def stop_receive(self):
  326. """
  327. stop the receiving thread for the port
  328. :return: None
  329. """
  330. if self.receive_thread:
  331. self.receive_thread.exit()
  332. self.LOG_THREAD.flush_data()
  333. self.receive_thread = None
  334. def close(self):
  335. """
  336. permanently close the port
  337. """
  338. self.stop_receive()
  339. self._port_close()
  340. @staticmethod
  341. def u_to_bytearray(data):
  342. """
  343. if data is not bytearray then it tries to convert it
  344. :param data: data which needs to be checked and maybe transformed
  345. """
  346. if isinstance(data, type(u'')):
  347. try:
  348. data = data.encode('utf-8')
  349. except Exception:
  350. print(u'Cannot encode {} of type {}'.format(data, type(data)))
  351. raise
  352. return data
  353. def write(self, data, eol="\r\n", flush=True):
  354. """
  355. :param data: data
  356. :param eol: end of line pattern.
  357. :param flush: if need to flush received data cache before write data.
  358. usually we need to flush data before write,
  359. make sure processing outputs generated by wrote.
  360. :return: None
  361. """
  362. # do flush before write
  363. if flush:
  364. self.data_cache.flush()
  365. # do write if cache
  366. if data is not None:
  367. self._port_write(self.u_to_bytearray(data) + self.u_to_bytearray(eol) if eol else self.u_to_bytearray(data))
  368. @_expect_lock
  369. def read(self, size=0xFFFFFFFF):
  370. """
  371. read(size=0xFFFFFFFF)
  372. read raw data. NOT suggested to use this method.
  373. Only use it if expect method doesn't meet your requirement.
  374. :param size: read size. default read all data
  375. :return: read data
  376. """
  377. data = self.data_cache.get_data(0)[:size]
  378. self.data_cache.flush(size)
  379. return data
  380. def start_capture_raw_data(self, capture_id="default"):
  381. """
  382. Sometime application want to get DUT raw data and use ``expect`` method at the same time.
  383. Capture methods provides a way to get raw data without affecting ``expect`` or ``read`` method.
  384. If you call ``start_capture_raw_data`` with same capture id again, it will restart capture on this ID.
  385. :param capture_id: ID of capture. You can use different IDs to do different captures at the same time.
  386. """
  387. with self.record_data_lock:
  388. try:
  389. # if start capture on existed ID, we do flush data and restart capture
  390. self.recorded_data[capture_id].flush()
  391. except KeyError:
  392. # otherwise, create new data cache
  393. self.recorded_data[capture_id] = _DataCache()
  394. def stop_capture_raw_data(self, capture_id="default"):
  395. """
  396. Stop capture and get raw data.
  397. This method should be used after ``start_capture_raw_data`` on the same capture ID.
  398. :param capture_id: ID of capture.
  399. :return: captured raw data between start capture and stop capture.
  400. """
  401. with self.record_data_lock:
  402. try:
  403. ret = self.recorded_data[capture_id].get_data()
  404. self.recorded_data.pop(capture_id)
  405. except KeyError as e:
  406. e.message = "capture_id does not exist. " \
  407. "You should call start_capture_raw_data with same ID " \
  408. "before calling stop_capture_raw_data"
  409. raise e
  410. return ret
  411. # expect related methods
  412. @staticmethod
  413. def _expect_str(data, pattern):
  414. """
  415. protected method. check if string is matched in data cache.
  416. :param data: data to process
  417. :param pattern: string
  418. :return: pattern if match succeed otherwise None
  419. """
  420. index = data.find(pattern)
  421. if index != -1:
  422. ret = pattern
  423. index += len(pattern)
  424. else:
  425. ret = None
  426. return ret, index
  427. @staticmethod
  428. def _expect_re(data, pattern):
  429. """
  430. protected method. check if re pattern is matched in data cache
  431. :param data: data to process
  432. :param pattern: compiled RegEx pattern
  433. :return: match groups if match succeed otherwise None
  434. """
  435. ret = None
  436. if isinstance(pattern.pattern, type(u'')):
  437. pattern = re.compile(BaseDUT.u_to_bytearray(pattern.pattern))
  438. if isinstance(data, type(u'')):
  439. data = BaseDUT.u_to_bytearray(data)
  440. match = pattern.search(data)
  441. if match:
  442. ret = tuple(None if x is None else x.decode() for x in match.groups())
  443. index = match.end()
  444. else:
  445. index = -1
  446. return ret, index
  447. EXPECT_METHOD = [
  448. [type(re.compile("")), "_expect_re"],
  449. [type(b''), "_expect_str"], # Python 2 & 3 hook to work without 'from builtins import str' from future
  450. [type(u''), "_expect_str"],
  451. ]
  452. def _get_expect_method(self, pattern):
  453. """
  454. protected method. get expect method according to pattern type.
  455. :param pattern: expect pattern, string or compiled RegEx
  456. :return: ``_expect_str`` or ``_expect_re``
  457. """
  458. for expect_method in self.EXPECT_METHOD:
  459. if isinstance(pattern, expect_method[0]):
  460. method = expect_method[1]
  461. break
  462. else:
  463. raise UnsupportedExpectItem()
  464. return self.__getattribute__(method)
  465. @_expect_lock
  466. def expect(self, pattern, timeout=DEFAULT_EXPECT_TIMEOUT):
  467. """
  468. expect(pattern, timeout=DEFAULT_EXPECT_TIMEOUT)
  469. expect received data on DUT match the pattern. will raise exception when expect timeout.
  470. :raise ExpectTimeout: failed to find the pattern before timeout
  471. :raise UnsupportedExpectItem: pattern is not string or compiled RegEx
  472. :param pattern: string or compiled RegEx(string pattern)
  473. :param timeout: timeout for expect
  474. :return: string if pattern is string; matched groups if pattern is RegEx
  475. """
  476. method = self._get_expect_method(pattern)
  477. # non-blocking get data for first time
  478. data = self.data_cache.get_data(0)
  479. start_time = time.time()
  480. while True:
  481. ret, index = method(data, pattern)
  482. if ret is not None:
  483. self.data_cache.flush(index)
  484. break
  485. time_remaining = start_time + timeout - time.time()
  486. if time_remaining < 0:
  487. break
  488. # wait for new data from cache
  489. data = self.data_cache.get_data(time_remaining)
  490. if ret is None:
  491. pattern = _pattern_to_string(pattern)
  492. self._save_expect_failure(pattern, data, start_time)
  493. raise ExpectTimeout(self.name + ": " + pattern)
  494. return ret
  495. def _expect_multi(self, expect_all, expect_item_list, timeout):
  496. """
  497. protected method. internal logical for expect multi.
  498. :param expect_all: True or False, expect all items in the list or any in the list
  499. :param expect_item_list: expect item list
  500. :param timeout: timeout
  501. :return: None
  502. """
  503. def process_expected_item(item_raw):
  504. # convert item raw data to standard dict
  505. item = {
  506. "pattern": item_raw[0] if isinstance(item_raw, tuple) else item_raw,
  507. "method": self._get_expect_method(item_raw[0] if isinstance(item_raw, tuple)
  508. else item_raw),
  509. "callback": item_raw[1] if isinstance(item_raw, tuple) else None,
  510. "index": -1,
  511. "ret": None,
  512. }
  513. return item
  514. expect_items = [process_expected_item(x) for x in expect_item_list]
  515. # non-blocking get data for first time
  516. data = self.data_cache.get_data(0)
  517. start_time = time.time()
  518. matched_expect_items = list()
  519. while True:
  520. for expect_item in expect_items:
  521. if expect_item not in matched_expect_items:
  522. # exclude those already matched
  523. expect_item["ret"], expect_item["index"] = \
  524. expect_item["method"](data, expect_item["pattern"])
  525. if expect_item["ret"] is not None:
  526. # match succeed for one item
  527. matched_expect_items.append(expect_item)
  528. # if expect all, then all items need to be matched,
  529. # else only one item need to matched
  530. if expect_all:
  531. match_succeed = len(matched_expect_items) == len(expect_items)
  532. else:
  533. match_succeed = True if matched_expect_items else False
  534. time_remaining = start_time + timeout - time.time()
  535. if time_remaining < 0 or match_succeed:
  536. break
  537. else:
  538. data = self.data_cache.get_data(time_remaining)
  539. if match_succeed:
  540. # sort matched items according to order of appearance in the input data,
  541. # so that the callbacks are invoked in correct order
  542. matched_expect_items = sorted(matched_expect_items, key=lambda it: it["index"])
  543. # invoke callbacks and flush matched data cache
  544. slice_index = -1
  545. for expect_item in matched_expect_items:
  546. # trigger callback
  547. if expect_item["callback"]:
  548. expect_item["callback"](expect_item["ret"])
  549. slice_index = max(slice_index, expect_item["index"])
  550. # flush already matched data
  551. self.data_cache.flush(slice_index)
  552. else:
  553. pattern = str([_pattern_to_string(x["pattern"]) for x in expect_items])
  554. self._save_expect_failure(pattern, data, start_time)
  555. raise ExpectTimeout(self.name + ": " + pattern)
  556. @_expect_lock
  557. def expect_any(self, *expect_items, **timeout):
  558. """
  559. expect_any(*expect_items, timeout=DEFAULT_TIMEOUT)
  560. expect any of the patterns.
  561. will call callback (if provided) if pattern match succeed and then return.
  562. will pass match result to the callback.
  563. :raise ExpectTimeout: failed to match any one of the expect items before timeout
  564. :raise UnsupportedExpectItem: pattern in expect_item is not string or compiled RegEx
  565. :arg expect_items: one or more expect items.
  566. string, compiled RegEx pattern or (string or RegEx(string pattern), callback)
  567. :keyword timeout: timeout for expect
  568. :return: None
  569. """
  570. # to be compatible with python2
  571. # in python3 we can write f(self, *expect_items, timeout=DEFAULT_TIMEOUT)
  572. if "timeout" not in timeout:
  573. timeout["timeout"] = self.DEFAULT_EXPECT_TIMEOUT
  574. return self._expect_multi(False, expect_items, **timeout)
  575. @_expect_lock
  576. def expect_all(self, *expect_items, **timeout):
  577. """
  578. expect_all(*expect_items, timeout=DEFAULT_TIMEOUT)
  579. expect all of the patterns.
  580. will call callback (if provided) if all pattern match succeed and then return.
  581. will pass match result to the callback.
  582. :raise ExpectTimeout: failed to match all of the expect items before timeout
  583. :raise UnsupportedExpectItem: pattern in expect_item is not string or compiled RegEx
  584. :arg expect_items: one or more expect items.
  585. string, compiled RegEx pattern or (string or RegEx(string pattern), callback)
  586. :keyword timeout: timeout for expect
  587. :return: None
  588. """
  589. # to be compatible with python2
  590. # in python3 we can write f(self, *expect_items, timeout=DEFAULT_TIMEOUT)
  591. if "timeout" not in timeout:
  592. timeout["timeout"] = self.DEFAULT_EXPECT_TIMEOUT
  593. return self._expect_multi(True, expect_items, **timeout)
  594. @staticmethod
  595. def _format_ts(ts):
  596. return "{}:{}".format(time.strftime("%m-%d %H:%M:%S", time.localtime(ts)), str(ts % 1)[2:5])
  597. def print_debug_info(self):
  598. """
  599. Print debug info of current DUT. Currently we will print debug info for expect failures.
  600. """
  601. Utility.console_log("DUT debug info for DUT: {}:".format(self.name), color="orange")
  602. for failure in self.expect_failures:
  603. Utility.console_log(u"\t[pattern]: {}\r\n\t[data]: {}\r\n\t[time]: {} - {}\r\n"
  604. .format(failure["pattern"], failure["data"],
  605. self._format_ts(failure["start"]), self._format_ts(failure["end"])),
  606. color="orange")
  607. class SerialDUT(BaseDUT):
  608. """ serial with logging received data feature """
  609. DEFAULT_UART_CONFIG = {
  610. "baudrate": 115200,
  611. "bytesize": serial.EIGHTBITS,
  612. "parity": serial.PARITY_NONE,
  613. "stopbits": serial.STOPBITS_ONE,
  614. "timeout": 0.05,
  615. "xonxoff": False,
  616. "rtscts": False,
  617. }
  618. def __init__(self, name, port, log_file, app, **kwargs):
  619. self.port_inst = None
  620. self.serial_configs = self.DEFAULT_UART_CONFIG.copy()
  621. self.serial_configs.update(kwargs)
  622. super(SerialDUT, self).__init__(name, port, log_file, app, **kwargs)
  623. def _format_data(self, data):
  624. """
  625. format data for logging. do decode and add timestamp.
  626. :param data: raw data from read
  627. :return: formatted data (str)
  628. """
  629. timestamp = "[{}]".format(self._format_ts(time.time()))
  630. formatted_data = timestamp.encode() + b"\r\n" + data + b"\r\n"
  631. return formatted_data
  632. def _port_open(self):
  633. self.port_inst = serial.Serial(self.port, **self.serial_configs)
  634. def _port_close(self):
  635. self.port_inst.close()
  636. def _port_read(self, size=1):
  637. data = self.port_inst.read(size)
  638. if data:
  639. self._save_dut_log(self._format_data(data))
  640. return data
  641. def _port_write(self, data):
  642. if isinstance(data, str):
  643. data = data.encode()
  644. self.port_inst.write(data)
  645. @classmethod
  646. def list_available_ports(cls):
  647. return [x.device for x in list_ports.comports()]