DUT.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. import time
  35. import re
  36. import threading
  37. import copy
  38. import sys
  39. import functools
  40. import serial
  41. from serial.tools import list_ports
  42. if sys.version_info[0] == 2:
  43. import Queue as _queue
  44. else:
  45. import queue as _queue
  46. class ExpectTimeout(ValueError):
  47. """ timeout for expect method """
  48. pass
  49. class UnsupportedExpectItem(ValueError):
  50. """ expect item not supported by the expect method """
  51. pass
  52. def _expect_lock(func):
  53. @functools.wraps(func)
  54. def handler(self, *args, **kwargs):
  55. with self.expect_lock:
  56. ret = func(self, *args, **kwargs)
  57. return ret
  58. return handler
  59. class _DataCache(_queue.Queue):
  60. """
  61. Data cache based on Queue. Allow users to process data cache based on bytes instead of Queue."
  62. """
  63. def __init__(self, maxsize=0):
  64. _queue.Queue.__init__(self, maxsize=maxsize)
  65. self.data_cache = str()
  66. def get_data(self, timeout=0):
  67. """
  68. get a copy of data from cache.
  69. :param timeout: timeout for waiting new queue item
  70. :return: copy of data cache
  71. """
  72. # make sure timeout is non-negative
  73. if timeout < 0:
  74. timeout = 0
  75. try:
  76. data = self.get(timeout=timeout)
  77. if isinstance(data, bytes):
  78. # convert bytes to string
  79. try:
  80. data = data.decode("utf-8", "ignore")
  81. except UnicodeDecodeError:
  82. data = data.decode("iso8859-1",)
  83. self.data_cache += data
  84. except _queue.Empty:
  85. # don't do anything when on update for cache
  86. pass
  87. return copy.deepcopy(self.data_cache)
  88. def flush(self, index=0xFFFFFFFF):
  89. """
  90. flush data from cache.
  91. :param index: if < 0 then don't do flush, otherwise flush data before index
  92. :return: None
  93. """
  94. # first add data in queue to cache
  95. self.get_data()
  96. if index > 0:
  97. self.data_cache = self.data_cache[index:]
  98. class _RecvThread(threading.Thread):
  99. def __init__(self, read, data_cache):
  100. super(_RecvThread, self).__init__()
  101. self.exit_event = threading.Event()
  102. self.setDaemon(True)
  103. self.read = read
  104. self.data_cache = data_cache
  105. def run(self):
  106. while not self.exit_event.isSet():
  107. data = self.read(1000)
  108. if data:
  109. self.data_cache.put(data)
  110. def exit(self):
  111. self.exit_event.set()
  112. self.join()
  113. class BaseDUT(object):
  114. """
  115. :param name: application defined name for port
  116. :param port: comport name, used to create DUT port
  117. :param log_file: log file name
  118. :param app: test app instance
  119. :param kwargs: extra args for DUT to create ports
  120. """
  121. DEFAULT_EXPECT_TIMEOUT = 5
  122. def __init__(self, name, port, log_file, app, **kwargs):
  123. self.expect_lock = threading.Lock()
  124. self.name = name
  125. self.port = port
  126. self.log_file = log_file
  127. self.app = app
  128. self.data_cache = _DataCache()
  129. self.receive_thread = None
  130. # open and start during init
  131. self.open()
  132. def __str__(self):
  133. return "DUT({}: {})".format(self.name, str(self.port))
  134. # define for methods need to be overwritten by Port
  135. @classmethod
  136. def list_available_ports(cls):
  137. """
  138. list all available ports.
  139. subclass (port) must overwrite this method.
  140. :return: list of available comports
  141. """
  142. pass
  143. def _port_open(self):
  144. """
  145. open the port.
  146. subclass (port) must overwrite this method.
  147. :return: None
  148. """
  149. pass
  150. def _port_read(self, size=1):
  151. """
  152. read form port. This method should not blocking for long time, otherwise receive thread can not exit.
  153. subclass (port) must overwrite this method.
  154. :param size: max size to read.
  155. :return: read data.
  156. """
  157. pass
  158. def _port_write(self, data):
  159. """
  160. write to port.
  161. subclass (port) must overwrite this method.
  162. :param data: data to write
  163. :return: None
  164. """
  165. pass
  166. def _port_close(self):
  167. """
  168. close port.
  169. subclass (port) must overwrite this method.
  170. :return: None
  171. """
  172. pass
  173. # methods that need to be overwritten by Tool
  174. @classmethod
  175. def confirm_dut(cls, port, app, **kwargs):
  176. """
  177. confirm if it's a DUT, usually used by auto detecting DUT in by Env config.
  178. subclass (tool) must overwrite this method.
  179. :param port: comport
  180. :param app: app instance
  181. :return: True or False
  182. """
  183. pass
  184. def start_app(self):
  185. """
  186. usually after we got DUT, we need to do some extra works to let App start.
  187. For example, we need to reset->download->reset to let IDF application start on DUT.
  188. subclass (tool) must overwrite this method.
  189. :return: None
  190. """
  191. pass
  192. # methods that features raw port methods
  193. def open(self):
  194. """
  195. open port and create thread to receive data.
  196. :return: None
  197. """
  198. self._port_open()
  199. self.receive_thread = _RecvThread(self._port_read, self.data_cache)
  200. self.receive_thread.start()
  201. def close(self):
  202. """
  203. close receive thread and then close port.
  204. :return: None
  205. """
  206. if self.receive_thread:
  207. self.receive_thread.exit()
  208. self._port_close()
  209. def write(self, data, eol="\r\n", flush=True):
  210. """
  211. :param data: data
  212. :param eol: end of line pattern.
  213. :param flush: if need to flush received data cache before write data.
  214. usually we need to flush data before write,
  215. make sure processing outputs generated by wrote.
  216. :return: None
  217. """
  218. # do flush before write
  219. if flush:
  220. self.data_cache.flush()
  221. # do write if cache
  222. if data:
  223. self._port_write(data + eol if eol else data)
  224. @_expect_lock
  225. def read(self, size=0xFFFFFFFF):
  226. """
  227. read(size=0xFFFFFFFF)
  228. read raw data. NOT suggested to use this method.
  229. Only use it if expect method doesn't meet your requirement.
  230. :param size: read size. default read all data
  231. :return: read data
  232. """
  233. data = self.data_cache.get_data(0)[:size]
  234. self.data_cache.flush(size)
  235. return data
  236. # expect related methods
  237. @staticmethod
  238. def _expect_str(data, pattern):
  239. """
  240. protected method. check if string is matched in data cache.
  241. :param data: data to process
  242. :param pattern: string
  243. :return: pattern if match succeed otherwise None
  244. """
  245. index = data.find(pattern)
  246. if index != -1:
  247. ret = pattern
  248. index += len(pattern)
  249. else:
  250. ret = None
  251. return ret, index
  252. @staticmethod
  253. def _expect_re(data, pattern):
  254. """
  255. protected method. check if re pattern is matched in data cache
  256. :param data: data to process
  257. :param pattern: compiled RegEx pattern
  258. :return: match groups if match succeed otherwise None
  259. """
  260. ret = None
  261. match = pattern.search(data)
  262. if match:
  263. ret = match.groups()
  264. index = match.end()
  265. else:
  266. index = -1
  267. return ret, index
  268. EXPECT_METHOD = [
  269. [type(re.compile("")), "_expect_re"],
  270. [str, "_expect_str"],
  271. ]
  272. def _get_expect_method(self, pattern):
  273. """
  274. protected method. get expect method according to pattern type.
  275. :param pattern: expect pattern, string or compiled RegEx
  276. :return: ``_expect_str`` or ``_expect_re``
  277. """
  278. for expect_method in self.EXPECT_METHOD:
  279. if isinstance(pattern, expect_method[0]):
  280. method = expect_method[1]
  281. break
  282. else:
  283. raise UnsupportedExpectItem()
  284. return self.__getattribute__(method)
  285. @_expect_lock
  286. def expect(self, pattern, timeout=DEFAULT_EXPECT_TIMEOUT):
  287. """
  288. expect(pattern, timeout=DEFAULT_EXPECT_TIMEOUT)
  289. expect received data on DUT match the pattern. will raise exception when expect timeout.
  290. :raise ExpectTimeout: failed to find the pattern before timeout
  291. :raise UnsupportedExpectItem: pattern is not string or compiled RegEx
  292. :param pattern: string or compiled RegEx(string pattern)
  293. :param timeout: timeout for expect
  294. :return: string if pattern is string; matched groups if pattern is RegEx
  295. """
  296. method = self._get_expect_method(pattern)
  297. # non-blocking get data for first time
  298. data = self.data_cache.get_data(0)
  299. start_time = time.time()
  300. while True:
  301. ret, index = method(data, pattern)
  302. if ret is not None or time.time() - start_time > timeout:
  303. self.data_cache.flush(index)
  304. break
  305. # wait for new data from cache
  306. data = self.data_cache.get_data(time.time() + timeout - start_time)
  307. if ret is None:
  308. raise ExpectTimeout(self.name + ": " + str(pattern))
  309. return ret
  310. def _expect_multi(self, expect_all, expect_item_list, timeout):
  311. """
  312. protected method. internal logical for expect multi.
  313. :param expect_all: True or False, expect all items in the list or any in the list
  314. :param expect_item_list: expect item list
  315. :param timeout: timeout
  316. :return: None
  317. """
  318. def process_expected_item(item_raw):
  319. # convert item raw data to standard dict
  320. item = {
  321. "pattern": item_raw[0] if isinstance(item_raw, tuple) else item_raw,
  322. "method": self._get_expect_method(item_raw[0] if isinstance(item_raw, tuple)
  323. else item_raw),
  324. "callback": item_raw[1] if isinstance(item_raw, tuple) else None,
  325. "index": -1,
  326. "ret": None,
  327. }
  328. return item
  329. expect_items = [process_expected_item(x) for x in expect_item_list]
  330. # non-blocking get data for first time
  331. data = self.data_cache.get_data(0)
  332. start_time = time.time()
  333. matched_expect_items = list()
  334. while True:
  335. for expect_item in expect_items:
  336. if expect_item not in matched_expect_items:
  337. # exclude those already matched
  338. expect_item["ret"], expect_item["index"] = \
  339. expect_item["method"](data, expect_item["pattern"])
  340. if expect_item["ret"] is not None:
  341. # match succeed for one item
  342. matched_expect_items.append(expect_item)
  343. break
  344. # if expect all, then all items need to be matched,
  345. # else only one item need to matched
  346. if expect_all:
  347. match_succeed = (matched_expect_items == expect_items)
  348. else:
  349. match_succeed = True if matched_expect_items else False
  350. if time.time() - start_time > timeout or match_succeed:
  351. break
  352. else:
  353. data = self.data_cache.get_data(time.time() + timeout - start_time)
  354. if match_succeed:
  355. # do callback and flush matched data cache
  356. slice_index = -1
  357. for expect_item in matched_expect_items:
  358. # trigger callback
  359. if expect_item["callback"]:
  360. expect_item["callback"](expect_item["ret"])
  361. slice_index = max(slice_index, expect_item["index"])
  362. # flush already matched data
  363. self.data_cache.flush(slice_index)
  364. else:
  365. raise ExpectTimeout(self.name + ": " + str(expect_items))
  366. @_expect_lock
  367. def expect_any(self, *expect_items, **timeout):
  368. """
  369. expect_any(*expect_items, timeout=DEFAULT_TIMEOUT)
  370. expect any of the patterns.
  371. will call callback (if provided) if pattern match succeed and then return.
  372. will pass match result to the callback.
  373. :raise ExpectTimeout: failed to match any one of the expect items before timeout
  374. :raise UnsupportedExpectItem: pattern in expect_item is not string or compiled RegEx
  375. :arg expect_items: one or more expect items.
  376. string, compiled RegEx pattern or (string or RegEx(string pattern), callback)
  377. :keyword timeout: timeout for expect
  378. :return: None
  379. """
  380. # to be compatible with python2
  381. # in python3 we can write f(self, *expect_items, timeout=DEFAULT_TIMEOUT)
  382. if "timeout" not in timeout:
  383. timeout["timeout"] = self.DEFAULT_EXPECT_TIMEOUT
  384. return self._expect_multi(False, expect_items, **timeout)
  385. @_expect_lock
  386. def expect_all(self, *expect_items, **timeout):
  387. """
  388. expect_all(*expect_items, timeout=DEFAULT_TIMEOUT)
  389. expect all of the patterns.
  390. will call callback (if provided) if all pattern match succeed and then return.
  391. will pass match result to the callback.
  392. :raise ExpectTimeout: failed to match all of the expect items before timeout
  393. :raise UnsupportedExpectItem: pattern in expect_item is not string or compiled RegEx
  394. :arg expect_items: one or more expect items.
  395. string, compiled RegEx pattern or (string or RegEx(string pattern), callback)
  396. :keyword timeout: timeout for expect
  397. :return: None
  398. """
  399. # to be compatible with python2
  400. # in python3 we can write f(self, *expect_items, timeout=DEFAULT_TIMEOUT)
  401. if "timeout" not in timeout:
  402. timeout["timeout"] = self.DEFAULT_EXPECT_TIMEOUT
  403. return self._expect_multi(True, expect_items, **timeout)
  404. class SerialDUT(BaseDUT):
  405. """ serial with logging received data feature """
  406. DEFAULT_UART_CONFIG = {
  407. "baudrate": 115200,
  408. "bytesize": serial.EIGHTBITS,
  409. "parity": serial.PARITY_NONE,
  410. "stopbits": serial.STOPBITS_ONE,
  411. "timeout": 0.05,
  412. "xonxoff": False,
  413. "rtscts": False,
  414. }
  415. def __init__(self, name, port, log_file, app, **kwargs):
  416. self.port_inst = None
  417. self.serial_configs = self.DEFAULT_UART_CONFIG.copy()
  418. self.serial_configs.update(kwargs)
  419. super(SerialDUT, self).__init__(name, port, log_file, app, **kwargs)
  420. @staticmethod
  421. def _format_data(data):
  422. """
  423. format data for logging. do decode and add timestamp.
  424. :param data: raw data from read
  425. :return: formatted data (str)
  426. """
  427. timestamp = time.time()
  428. timestamp = "{}:{}".format(time.strftime("%m-%d %H:%M:%S", time.localtime(timestamp)),
  429. str(timestamp % 1)[2:5])
  430. try:
  431. formatted_data = "[{}]:\r\n{}\r\n".format(timestamp, data.decode("utf-8", "ignore"))
  432. except UnicodeDecodeError:
  433. # if utf-8 fail, use iso-8859-1 (single char codec with range 0-255)
  434. formatted_data = "[{}]:\r\n{}\r\n".format(timestamp, data.decode("iso8859-1",))
  435. return formatted_data
  436. def _port_open(self):
  437. self.port_inst = serial.Serial(self.port, **self.serial_configs)
  438. def _port_close(self):
  439. self.port_inst.close()
  440. def _port_read(self, size=1):
  441. data = self.port_inst.read(size)
  442. if data:
  443. with open(self.log_file, "a+") as _log_file:
  444. _log_file.write(self._format_data(data))
  445. return data
  446. def _port_write(self, data):
  447. self.port_inst.write(data)
  448. @classmethod
  449. def list_available_ports(cls):
  450. return [x.device for x in list_ports.comports()]