test_usb_helpers.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include "unity.h"
  8. #include "test_utils.h"
  9. #include "usb/usb_host.h"
  10. /*
  11. Tests that check the configuration descriptor parsing functions provided in usb_host.h work by parsing a fixed
  12. configuration descriptor. The fixed configuration descriptor used in this test is provided below (both in textual and
  13. byte format), and is of a UVC device. Thus the configuration descriptor has a good set of scenarios that can be tested
  14. (such as multiple interfaces, alternate settings, class specific descriptors, default endpoint only interfaces etc).
  15. */
  16. /*
  17. Configuration Descriptor:
  18. bLength 9
  19. bDescriptorType 2
  20. wTotalLength 0x0185
  21. bNumInterfaces 2
  22. bConfigurationValue 1
  23. iConfiguration 0
  24. bmAttributes 0x80
  25. (Bus Powered)
  26. MaxPower 500mA
  27. Interface Association:
  28. bLength 8
  29. bDescriptorType 11
  30. bFirstInterface 0
  31. bInterfaceCount 2
  32. bFunctionClass 14 Video
  33. bFunctionSubClass 3 Video Interface Collection
  34. bFunctionProtocol 0
  35. iFunction 5
  36. Interface Descriptor:
  37. bLength 9
  38. bDescriptorType 4
  39. bInterfaceNumber 0
  40. bAlternateSetting 0
  41. bNumEndpoints 1
  42. bInterfaceClass 14 Video
  43. bInterfaceSubClass 1 Video Control
  44. bInterfaceProtocol 0
  45. iInterface 5
  46. VideoControl Interface Descriptor:
  47. bLength 13
  48. bDescriptorType 36
  49. bDescriptorSubtype 1 (HEADER)
  50. bcdUVC 1.00
  51. wTotalLength 0x004f
  52. dwClockFrequency 15.000000MHz
  53. bInCollection 1
  54. baInterfaceNr( 0) 1
  55. VideoControl Interface Descriptor:
  56. bLength 9
  57. bDescriptorType 36
  58. bDescriptorSubtype 3 (OUTPUT_TERMINAL)
  59. bTerminalID 4
  60. wTerminalType 0x0101 USB Streaming
  61. bAssocTerminal 0
  62. bSourceID 3
  63. iTerminal 0
  64. VideoControl Interface Descriptor:
  65. bLength 28
  66. bDescriptorType 36
  67. bDescriptorSubtype 6 (EXTENSION_UNIT)
  68. bUnitID 3
  69. guidExtensionCode {4cf18db6-abd0-495c-9876-1fa3942ff9fa}
  70. bNumControl 24
  71. bNrPins 1
  72. baSourceID( 0) 2
  73. bControlSize 3
  74. bmControls( 0) 0xff
  75. bmControls( 1) 0xff
  76. bmControls( 2) 0xff
  77. iExtension 0
  78. VideoControl Interface Descriptor:
  79. bLength 18
  80. bDescriptorType 36
  81. bDescriptorSubtype 2 (INPUT_TERMINAL)
  82. bTerminalID 1
  83. wTerminalType 0x0201 Camera Sensor
  84. bAssocTerminal 0
  85. iTerminal 0
  86. wObjectiveFocalLengthMin 0
  87. wObjectiveFocalLengthMax 0
  88. wOcularFocalLength 0
  89. bControlSize 3
  90. bmControls 0x0000000e
  91. Auto-Exposure Mode
  92. Auto-Exposure Priority
  93. Exposure Time (Absolute)
  94. VideoControl Interface Descriptor:
  95. bLength 11
  96. bDescriptorType 36
  97. bDescriptorSubtype 5 (PROCESSING_UNIT)
  98. Warning: Descriptor too short
  99. bUnitID 2
  100. bSourceID 1
  101. wMaxMultiplier 0
  102. bControlSize 2
  103. bmControls 0x0000177f
  104. Brightness
  105. Contrast
  106. Hue
  107. Saturation
  108. Sharpness
  109. Gamma
  110. White Balance Temperature
  111. Backlight Compensation
  112. Gain
  113. Power Line Frequency
  114. White Balance Temperature, Auto
  115. iProcessing 0
  116. bmVideoStandards 0x62
  117. NTSC - 525/60
  118. PAL - 525/60
  119. Endpoint Descriptor:
  120. bLength 7
  121. bDescriptorType 5
  122. bEndpointAddress 0x83 EP 3 IN
  123. bmAttributes 3
  124. Transfer Type Interrupt
  125. Synch Type None
  126. Usage Type Data
  127. wMaxPacketSize 0x0010 1x 16 bytes
  128. bInterval 6
  129. Interface Descriptor:
  130. bLength 9
  131. bDescriptorType 4
  132. bInterfaceNumber 1
  133. bAlternateSetting 0
  134. bNumEndpoints 0
  135. bInterfaceClass 14 Video
  136. bInterfaceSubClass 2 Video Streaming
  137. bInterfaceProtocol 0
  138. iInterface 0
  139. VideoStreaming Interface Descriptor:
  140. bLength 15
  141. bDescriptorType 36
  142. bDescriptorSubtype 1 (INPUT_HEADER)
  143. bNumFormats 2
  144. wTotalLength 0x00f7
  145. bEndPointAddress 129
  146. bmInfo 0
  147. bTerminalLink 4
  148. bStillCaptureMethod 0
  149. bTriggerSupport 0
  150. bTriggerUsage 0
  151. bControlSize 1
  152. bmaControls( 0) 0
  153. bmaControls( 1) 0
  154. VideoStreaming Interface Descriptor:
  155. bLength 27
  156. bDescriptorType 36
  157. bDescriptorSubtype 4 (FORMAT_UNCOMPRESSED)
  158. bFormatIndex 1
  159. bNumFrameDescriptors 2
  160. guidFormat {85f6cc1d-0c9f-44f5-9ce0-97c7dd8c98ab}
  161. bBitsPerPixel 16
  162. bDefaultFrameIndex 1
  163. bAspectRatioX 0
  164. bAspectRatioY 0
  165. bmInterlaceFlags 0x00
  166. Interlaced stream or variable: No
  167. Fields per frame: 2 fields
  168. Field 1 first: No
  169. Field pattern: Field 1 only
  170. bCopyProtect 0
  171. VideoStreaming Interface Descriptor:
  172. bLength 30
  173. bDescriptorType 36
  174. bDescriptorSubtype 5 (FRAME_UNCOMPRESSED)
  175. bFrameIndex 1
  176. bmCapabilities 0x00
  177. Still image unsupported
  178. wWidth 480
  179. wHeight 320
  180. dwMinBitRate 12288000
  181. dwMaxBitRate 12288000
  182. dwMaxVideoFrameBufferSize 307200
  183. dwDefaultFrameInterval 2000000
  184. bFrameIntervalType 1
  185. dwFrameInterval( 0) 2000000
  186. VideoStreaming Interface Descriptor:
  187. bLength 30
  188. bDescriptorType 36
  189. bDescriptorSubtype 5 (FRAME_UNCOMPRESSED)
  190. bFrameIndex 2
  191. bmCapabilities 0x00
  192. Still image unsupported
  193. wWidth 640
  194. wHeight 480
  195. dwMinBitRate 73728000
  196. dwMaxBitRate 73728000
  197. dwMaxVideoFrameBufferSize 614400
  198. dwDefaultFrameInterval 666666
  199. bFrameIntervalType 1
  200. dwFrameInterval( 0) 666666
  201. VideoStreaming Interface Descriptor:
  202. bLength 11
  203. bDescriptorType 36
  204. bDescriptorSubtype 6 (FORMAT_MJPEG)
  205. bFormatIndex 2
  206. bNumFrameDescriptors 4
  207. bFlags 0
  208. Fixed-size samples: No
  209. bDefaultFrameIndex 1
  210. bAspectRatioX 0
  211. bAspectRatioY 0
  212. bmInterlaceFlags 0x00
  213. Interlaced stream or variable: No
  214. Fields per frame: 1 fields
  215. Field 1 first: No
  216. Field pattern: Field 1 only
  217. bCopyProtect 0
  218. VideoStreaming Interface Descriptor:
  219. bLength 30
  220. bDescriptorType 36
  221. bDescriptorSubtype 7 (FRAME_MJPEG)
  222. bFrameIndex 1
  223. bmCapabilities 0x00
  224. Still image unsupported
  225. wWidth 640
  226. wHeight 480
  227. dwMinBitRate 36864000
  228. dwMaxBitRate 36864000
  229. dwMaxVideoFrameBufferSize 307789
  230. dwDefaultFrameInterval 666666
  231. bFrameIntervalType 1
  232. dwFrameInterval( 0) 666666
  233. VideoStreaming Interface Descriptor:
  234. bLength 38
  235. bDescriptorType 36
  236. bDescriptorSubtype 7 (FRAME_MJPEG)
  237. bFrameIndex 2
  238. bmCapabilities 0x00
  239. Still image unsupported
  240. wWidth 480
  241. wHeight 320
  242. dwMinBitRate 6144000
  243. dwMaxBitRate 18432000
  244. dwMaxVideoFrameBufferSize 154189
  245. dwDefaultFrameInterval 666666
  246. bFrameIntervalType 3
  247. dwFrameInterval( 0) 666666
  248. dwFrameInterval( 1) 1000000
  249. dwFrameInterval( 2) 2000000
  250. VideoStreaming Interface Descriptor:
  251. bLength 30
  252. bDescriptorType 36
  253. bDescriptorSubtype 7 (FRAME_MJPEG)
  254. bFrameIndex 3
  255. bmCapabilities 0x00
  256. Still image unsupported
  257. wWidth 352
  258. wHeight 288
  259. dwMinBitRate 12165120
  260. dwMaxBitRate 12165120
  261. dwMaxVideoFrameBufferSize 101965
  262. dwDefaultFrameInterval 666666
  263. bFrameIntervalType 1
  264. dwFrameInterval( 0) 666666
  265. VideoStreaming Interface Descriptor:
  266. bLength 30
  267. bDescriptorType 36
  268. bDescriptorSubtype 7 (FRAME_MJPEG)
  269. bFrameIndex 4
  270. bmCapabilities 0x00
  271. Still image unsupported
  272. wWidth 320
  273. wHeight 240
  274. dwMinBitRate 9216000
  275. dwMaxBitRate 9216000
  276. dwMaxVideoFrameBufferSize 77389
  277. dwDefaultFrameInterval 666666
  278. bFrameIntervalType 1
  279. dwFrameInterval( 0) 666666
  280. VideoStreaming Interface Descriptor:
  281. bLength 6
  282. bDescriptorType 36
  283. bDescriptorSubtype 13 (COLORFORMAT)
  284. bColorPrimaries 1 (BT.709,sRGB)
  285. bTransferCharacteristics 1 (BT.709)
  286. bMatrixCoefficients 4 (SMPTE 170M (BT.601))
  287. Interface Descriptor:
  288. bLength 9
  289. bDescriptorType 4
  290. bInterfaceNumber 1
  291. bAlternateSetting 1
  292. bNumEndpoints 1
  293. bInterfaceClass 14 Video
  294. bInterfaceSubClass 2 Video Streaming
  295. bInterfaceProtocol 0
  296. iInterface 0
  297. Endpoint Descriptor:
  298. bLength 7
  299. bDescriptorType 5
  300. bEndpointAddress 0x81 EP 1 IN
  301. bmAttributes 5
  302. Transfer Type Isochronous
  303. Synch Type Asynchronous
  304. Usage Type Data
  305. wMaxPacketSize 0x03bc 1x 956 bytes
  306. bInterval 1
  307. */
  308. static uint8_t config_desc_bytes [] = {
  309. 0x09, 0x02, 0x85, 0x01, 0x02, 0x01, 0x00, 0x80, 0xFA, 0x08, 0x0B, 0x00, 0x02, 0x0E, 0x03, 0x00, 0x05, 0x09, 0x04,
  310. 0x00, 0x00, 0x01, 0x0E, 0x01, 0x00, 0x05, 0x0D, 0x24, 0x01, 0x00, 0x01, 0x4F, 0x00, 0xC0, 0xE1, 0xE4, 0x00, 0x01,
  311. 0x01, 0x09, 0x24, 0x03, 0x04, 0x01, 0x01, 0x00, 0x03, 0x00, 0x1C, 0x24, 0x06, 0x03, 0xB6, 0x8D, 0xF1, 0x4C, 0xD0,
  312. 0xAB, 0x5C, 0x49, 0x76, 0x98, 0xFA, 0xF9, 0x2F, 0x94, 0xA3, 0x1F, 0x18, 0x01, 0x02, 0x03, 0xFF, 0xFF, 0xFF, 0x00,
  313. 0x12, 0x24, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0B,
  314. 0x24, 0x05, 0x02, 0x01, 0x00, 0x00, 0x02, 0x7F, 0x17, 0x00, 0x07, 0x05, 0x83, 0x03, 0x10, 0x00, 0x06, 0x05, 0x25,
  315. 0x03, 0x80, 0x00, 0x09, 0x04, 0x01, 0x00, 0x00, 0x0E, 0x02, 0x00, 0x00, 0x0F, 0x24, 0x01, 0x02, 0xF7, 0x00, 0x81,
  316. 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x1B, 0x24, 0x04, 0x01, 0x02, 0x1D, 0xCC, 0xF6, 0x85, 0x9F, 0x0C,
  317. 0xF5, 0x44, 0xE0, 0x9C, 0xAB, 0x98, 0x8C, 0xDD, 0xC7, 0x97, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x24, 0x05,
  318. 0x01, 0x00, 0xE0, 0x01, 0x40, 0x01, 0x00, 0x80, 0xBB, 0x00, 0x00, 0x80, 0xBB, 0x00, 0x00, 0xB0, 0x04, 0x00, 0x80,
  319. 0x84, 0x1E, 0x00, 0x01, 0x80, 0x84, 0x1E, 0x00, 0x1E, 0x24, 0x05, 0x02, 0x00, 0x80, 0x02, 0xE0, 0x01, 0x00, 0x00,
  320. 0x65, 0x04, 0x00, 0x00, 0x65, 0x04, 0x00, 0x60, 0x09, 0x00, 0x2A, 0x2C, 0x0A, 0x00, 0x01, 0x2A, 0x2C, 0x0A, 0x00,
  321. 0x0B, 0x24, 0x06, 0x02, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x24, 0x07, 0x01, 0x00, 0x80, 0x02, 0xE0,
  322. 0x01, 0x00, 0x80, 0x32, 0x02, 0x00, 0x80, 0x32, 0x02, 0x4D, 0xB2, 0x04, 0x00, 0x2A, 0x2C, 0x0A, 0x00, 0x01, 0x2A,
  323. 0x2C, 0x0A, 0x00, 0x26, 0x24, 0x07, 0x02, 0x00, 0xE0, 0x01, 0x40, 0x01, 0x00, 0xC0, 0x5D, 0x00, 0x00, 0x40, 0x19,
  324. 0x01, 0x4D, 0x5A, 0x02, 0x00, 0x2A, 0x2C, 0x0A, 0x00, 0x03, 0x2A, 0x2C, 0x0A, 0x00, 0x40, 0x42, 0x0F, 0x00, 0x80,
  325. 0x84, 0x1E, 0x00, 0x1E, 0x24, 0x07, 0x03, 0x00, 0x60, 0x01, 0x20, 0x01, 0x00, 0xA0, 0xB9, 0x00, 0x00, 0xA0, 0xB9,
  326. 0x00, 0x4D, 0x8E, 0x01, 0x00, 0x2A, 0x2C, 0x0A, 0x00, 0x01, 0x2A, 0x2C, 0x0A, 0x00, 0x1E, 0x24, 0x07, 0x04, 0x00,
  327. 0x40, 0x01, 0xF0, 0x00, 0x00, 0xA0, 0x8C, 0x00, 0x00, 0xA0, 0x8C, 0x00, 0x4D, 0x2E, 0x01, 0x00, 0x2A, 0x2C, 0x0A,
  328. 0x00, 0x01, 0x2A, 0x2C, 0x0A, 0x00, 0x06, 0x24, 0x0D, 0x01, 0x01, 0x04, 0x09, 0x04, 0x01, 0x01, 0x01, 0x0E, 0x02,
  329. 0x00, 0x00, 0x07, 0x05, 0x81, 0x05, 0xBC, 0x03, 0x01,
  330. };
  331. _Static_assert(sizeof(config_desc_bytes) == 0x0185, "Configuration Descriptor size does not match");
  332. #define TEST_NUM_INTF_DESC 3 //Total number of interface descriptors (including alternate)
  333. // --------------------- Sub-Test 1 ------------------------
  334. /*
  335. Test if we can walk the configuration descriptor to find each interface descriptor
  336. */
  337. static void test_walk_desc(const usb_config_desc_t *config_desc)
  338. {
  339. int offset = 0;
  340. const usb_standard_desc_t *cur_desc = (usb_standard_desc_t *)config_desc;
  341. for (int i = 0; i < TEST_NUM_INTF_DESC; i++) {
  342. cur_desc = usb_parse_next_descriptor_of_type(cur_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset);
  343. TEST_ASSERT_NOT_EQUAL(NULL, cur_desc);
  344. }
  345. //Attempting to look for another interface descriptor should result in NULL
  346. cur_desc = usb_parse_next_descriptor_of_type(cur_desc, config_desc->wTotalLength, USB_B_DESCRIPTOR_TYPE_INTERFACE, &offset);
  347. TEST_ASSERT_EQUAL(NULL, cur_desc);
  348. }
  349. /*
  350. Test if the count of number of alternate descriptors is correct
  351. */
  352. static void test_alt_intf_desc_count(const usb_config_desc_t *config_desc)
  353. {
  354. //bInterface 0 has no alternate interfaces
  355. TEST_ASSERT_EQUAL(0, usb_parse_interface_number_of_alternate(config_desc, 0));
  356. //bInterface 1 has 1 alternate interface
  357. TEST_ASSERT_EQUAL(1, usb_parse_interface_number_of_alternate(config_desc, 1));
  358. //Non existent bInterface 2 should return -1
  359. TEST_ASSERT_EQUAL(-1, usb_parse_interface_number_of_alternate(config_desc, 2));
  360. }
  361. static void test_parse_intf_and_ep(const usb_config_desc_t *config_desc)
  362. {
  363. int offset_intf = 0;
  364. //Get bInterfaceNumber 0 (index 0)
  365. const usb_intf_desc_t *intf_desc = usb_parse_interface_descriptor(config_desc, 0, 0, &offset_intf);
  366. TEST_ASSERT_NOT_EQUAL(NULL, intf_desc);
  367. //Should only have one endpoint
  368. int offset_ep = offset_intf;
  369. const usb_ep_desc_t *ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, 0, config_desc->wTotalLength, &offset_ep);
  370. TEST_ASSERT_NOT_EQUAL(NULL, ep_desc);
  371. TEST_ASSERT_EQUAL(0x83, ep_desc->bEndpointAddress);
  372. offset_ep = offset_intf;
  373. ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, 1, config_desc->wTotalLength, &offset_ep);
  374. TEST_ASSERT_EQUAL(NULL, ep_desc);
  375. //Get bInterfaceNumber 1 alternate setting 0
  376. offset_intf = 0;
  377. intf_desc = usb_parse_interface_descriptor(config_desc, 1, 0, &offset_intf);
  378. TEST_ASSERT_NOT_EQUAL(NULL, intf_desc);
  379. //Should have no endpoints
  380. offset_ep = offset_intf;
  381. ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, 0, config_desc->wTotalLength, &offset_ep);
  382. TEST_ASSERT_EQUAL(NULL, ep_desc);
  383. //Get bInterfaceNumber 1 alternate setting 1
  384. offset_intf = 0;
  385. intf_desc = usb_parse_interface_descriptor(config_desc, 1, 1, &offset_intf);
  386. TEST_ASSERT_NOT_EQUAL(NULL, intf_desc);
  387. //Should only have one endpoint
  388. offset_ep = offset_intf;
  389. ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, 0, config_desc->wTotalLength, &offset_ep);
  390. TEST_ASSERT_NOT_EQUAL(NULL, ep_desc);
  391. TEST_ASSERT_EQUAL(0x81, ep_desc->bEndpointAddress);
  392. offset_ep = offset_intf;
  393. ep_desc = usb_parse_endpoint_descriptor_by_index(intf_desc, 1, config_desc->wTotalLength, &offset_ep);
  394. TEST_ASSERT_EQUAL(NULL, ep_desc);
  395. }
  396. static void test_parse_ep_by_address(const usb_config_desc_t *config_desc)
  397. {
  398. int offset_ep = 0;
  399. //Get bInterface 0 bAlternateSetting 0 EP 0x83
  400. const usb_ep_desc_t *ep_desc = usb_parse_endpoint_descriptor_by_address(config_desc, 0, 0, 0x83, &offset_ep);
  401. TEST_ASSERT_NOT_EQUAL(NULL, ep_desc);
  402. TEST_ASSERT_EQUAL(0x83, ep_desc->bEndpointAddress);
  403. //Getting same EP address under different interface should return NULL
  404. offset_ep = 0;
  405. ep_desc = usb_parse_endpoint_descriptor_by_address(config_desc, 1, 0, 0x83, &offset_ep);
  406. TEST_ASSERT_EQUAL(NULL, ep_desc);
  407. //Get bInterface 1 bAlternateSetting 1 EP 0x81
  408. offset_ep = 0;
  409. ep_desc = usb_parse_endpoint_descriptor_by_address(config_desc, 1, 1, 0x81, &offset_ep);
  410. TEST_ASSERT_NOT_EQUAL(NULL, ep_desc);
  411. TEST_ASSERT_EQUAL(0x81, ep_desc->bEndpointAddress);
  412. //Getting same EP address under different interface should return NULL
  413. offset_ep = 0;
  414. ep_desc = usb_parse_endpoint_descriptor_by_address(config_desc, 1, 0, 0x81, &offset_ep);
  415. TEST_ASSERT_EQUAL(NULL, ep_desc);
  416. }
  417. TEST_CASE("Test USB Helpers descriptor parsing", "[usb_host][ignore]")
  418. {
  419. const usb_config_desc_t *config_desc = (const usb_config_desc_t *)config_desc_bytes;
  420. test_walk_desc(config_desc);
  421. test_alt_intf_desc_count(config_desc);
  422. test_parse_intf_and_ep(config_desc);
  423. test_parse_ep_by_address(config_desc);
  424. }