utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2009-2011 Wander Lairson Costa
  2. #
  3. # The following terms apply to all files associated
  4. # with the software unless explicitly disclaimed in individual files.
  5. #
  6. # The authors hereby grant permission to use, copy, modify, distribute,
  7. # and license this software and its documentation for any purpose, provided
  8. # that existing copyright notices are retained in all copies and that this
  9. # notice is included verbatim in any distributions. No written agreement,
  10. # license, or royalty fee is required for any of the authorized uses.
  11. # Modifications to this software may be copyrighted by their authors
  12. # and need not follow the licensing terms described here, provided that
  13. # the new terms are clearly indicated on the first page of each file where
  14. # they apply.
  15. #
  16. # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  17. # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  18. # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  19. # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  20. # POSSIBILITY OF SUCH DAMAGE.
  21. #
  22. # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  23. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
  25. # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  26. # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  27. # MODIFICATIONS.
  28. import sys
  29. import os.path
  30. import operator
  31. from ctypes import c_ubyte, POINTER, cast
  32. parent_dir = os.path.split(os.getcwd())[0]
  33. # if we are at PyUSB source tree, add usb package to python path
  34. if os.path.exists(os.path.join(parent_dir, 'usb')):
  35. sys.path.insert(0, parent_dir)
  36. import usb.core
  37. import logging
  38. import devinfo
  39. import time
  40. import unittest
  41. import usb._interop as _interop
  42. logger = logging.getLogger('usb.test')
  43. # data generation functions
  44. def get_array_data1(length = 10):
  45. return _interop.as_array(range(length))
  46. def get_array_data2(length = 10):
  47. data = list(range(length))
  48. data.reverse()
  49. return _interop.as_array(data)
  50. def get_list_data1(length = 10):
  51. return list(range(length))
  52. def get_list_data2(length = 10):
  53. data = list(range(length))
  54. data.reverse()
  55. return data
  56. def get_str_data1(length = 10):
  57. return ''.join([chr(x) for x in range(length)])
  58. def get_str_data2(length = 10):
  59. data = list(range(length))
  60. data.reverse()
  61. return ''.join([chr(x) for x in data])
  62. def to_array(data):
  63. return _interop.as_array(data)
  64. def delay_after_reset():
  65. time.sleep(3) # necessary to wait device reenumeration
  66. # check if our test hardware is present
  67. def find_my_device(backend = None):
  68. try:
  69. return usb.core.find(backend=backend,
  70. idVendor=devinfo.ID_VENDOR,
  71. idProduct=devinfo.ID_PRODUCT)
  72. except Exception:
  73. return None
  74. def run_tests(suite):
  75. runner = unittest.TextTestRunner()
  76. runner.run(suite)
  77. def data_len(data):
  78. a = _interop.as_array(data)
  79. return len(data) * a.itemsize
  80. def array_equals(a1, a2):
  81. if a1.typecode != 'u' and a2.typecode != 'u':
  82. return a1 == a2
  83. else:
  84. # as python3 strings are unicode, loads of trouble,
  85. # because we read data from USB devices are byte arrays
  86. l1 = len(a1) * a1.itemsize
  87. l2 = len(a2) * a2.itemsize
  88. if l1 != l2:
  89. return False
  90. c_ubyte_p = POINTER(c_ubyte)
  91. p1 = cast(a1.buffer_info()[0], c_ubyte_p)
  92. p2 = cast(a2.buffer_info()[0], c_ubyte_p)
  93. # we do a item by item compare we unicode is involved
  94. return all(map(operator.eq, p1[:l1], p2[:l2]))