| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  | /* 
 | 
					
						
							|  |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2018, hathach (tinyusb.org) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of the TinyUSB stack. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "stdlib.h"
 | 
					
						
							|  |  |  | #include "unity.h"
 | 
					
						
							| 
									
										
										
										
											2013-05-06 20:09:06 +07:00
										 |  |  | #include "tusb_option.h"
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:40:34 +07:00
										 |  |  | #include "tusb_errors.h"
 | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | #include "binary.h"
 | 
					
						
							|  |  |  | #include "type_helper.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | #include "descriptor_test.h"
 | 
					
						
							|  |  |  | #include "mock_osal.h"
 | 
					
						
							|  |  |  | #include "mock_hcd.h"
 | 
					
						
							|  |  |  | #include "mock_usbh.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mock_msch_callback.h"
 | 
					
						
							|  |  |  | #include "msc_host.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 14:31:11 +07:00
										 |  |  | extern msch_interface_t msch_data[CFG_TUSB_HOST_DEVICE_MAX]; | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 12:32:40 +07:00
										 |  |  | static tusb_desc_interface_t const *p_msc_interface_desc = &desc_configuration.msc_interface; | 
					
						
							|  |  |  | static tusb_desc_endpoint_t const *p_edp_in  = &desc_configuration.msc_endpoint_in; | 
					
						
							|  |  |  | static tusb_desc_endpoint_t const *p_edp_out = &desc_configuration.msc_endpoint_out; | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | static msch_interface_t* p_msc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static uint8_t dev_addr; | 
					
						
							|  |  |  | static pipe_handle_t pipe_in, pipe_out; | 
					
						
							|  |  |  | static pipe_handle_t const pipe_null = { 0 }; | 
					
						
							|  |  |  | static uint16_t length; | 
					
						
							| 
									
										
										
										
											2013-05-09 12:49:52 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | void setUp(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-10 14:31:11 +07:00
										 |  |  |   dev_addr = RANDOM(CFG_TUSB_HOST_DEVICE_MAX)+1; | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 13:51:54 +07:00
										 |  |  |   osal_semaphore_create_IgnoreAndReturn( (osal_semaphore_handle_t) 0x1234); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  |   msch_init(); | 
					
						
							| 
									
										
										
										
											2018-04-10 14:31:11 +07:00
										 |  |  |   TEST_ASSERT_MEM_ZERO(msch_data, sizeof(msch_interface_t)*CFG_TUSB_HOST_DEVICE_MAX); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   p_msc = &msch_data[dev_addr-1]; | 
					
						
							|  |  |  |   pipe_in  = (pipe_handle_t) {.dev_addr = dev_addr, .xfer_type = TUSB_XFER_BULK, .index = 1}; | 
					
						
							|  |  |  |   pipe_out = (pipe_handle_t) {.dev_addr = dev_addr, .xfer_type = TUSB_XFER_BULK, .index = 2}; | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void tearDown(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  |   length = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test_open_pipe_in_failed(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-11 13:12:21 +07:00
										 |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, pipe_null); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-10 22:06:48 +07:00
										 |  |  |   TEST_ASSERT(TUSB_ERROR_NONE != msch_open(dev_addr, p_msc_interface_desc, &length)); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test_open_pipe_out_failed(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-11 13:12:21 +07:00
										 |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, (pipe_handle_t) {1} ); | 
					
						
							|  |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_out, TUSB_CLASS_MSC, pipe_null); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-10 22:06:48 +07:00
										 |  |  |   TEST_ASSERT(TUSB_ERROR_NONE != msch_open(dev_addr, p_msc_interface_desc, &length)); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tusb_error_t stub_control_xfer(uint8_t dev_addr, uint8_t bmRequestType, uint8_t bRequest, | 
					
						
							|  |  |  |                                uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t* data, int num_call) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch ( num_call ) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     case 0: // get max lun
 | 
					
						
							| 
									
										
										
										
											2018-03-21 15:31:58 +07:00
										 |  |  |       TEST_ASSERT_EQUAL(bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RECIPIENT_INTERFACE), | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  |                         bmRequestType); | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  |       TEST_ASSERT_EQUAL(MSC_REQ_GET_MAX_LUN, bRequest); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  |       TEST_ASSERT_EQUAL(p_msc_interface_desc->bInterfaceNumber, wIndex); | 
					
						
							|  |  |  |       TEST_ASSERT_EQUAL(1, wLength); | 
					
						
							|  |  |  |       *data = 1; // TODO multiple LUN support
 | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       TEST_FAIL(); | 
					
						
							|  |  |  |       return TUSB_ERROR_FAILED; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-04-18 13:51:54 +07:00
										 |  |  | #if 0 // TODO TEST enable this
 | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | void test_open_desc_length(void) | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-11 13:12:21 +07:00
										 |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, pipe_in); | 
					
						
							|  |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_out, TUSB_CLASS_MSC, pipe_out); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   usbh_control_xfer_subtask_IgnoreAndReturn(TUSB_ERROR_NONE); | 
					
						
							|  |  |  |   hcd_pipe_xfer_IgnoreAndReturn(TUSB_ERROR_NONE); | 
					
						
							|  |  |  |   hcd_pipe_queue_xfer_IgnoreAndReturn(TUSB_ERROR_NONE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- Code Under Test -------------//
 | 
					
						
							| 
									
										
										
										
											2018-12-10 22:06:48 +07:00
										 |  |  |   TEST_ASSERT_STATUS( msch_open(dev_addr, p_msc_interface_desc, &length) ); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 12:32:40 +07:00
										 |  |  |   TEST_ASSERT_EQUAL(sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t), | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  |                     length); | 
					
						
							| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | void test_open_ok(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-11 13:12:21 +07:00
										 |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, pipe_in); | 
					
						
							|  |  |  |   hcd_edpt_open_ExpectAndReturn(dev_addr, p_edp_out, TUSB_CLASS_MSC, pipe_out); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   //------------- get max lun -------------//
 | 
					
						
							|  |  |  |   usbh_control_xfer_subtask_StubWithCallback(stub_control_xfer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- Code Under Test -------------//
 | 
					
						
							| 
									
										
										
										
											2018-12-10 22:06:48 +07:00
										 |  |  |   TEST_ASSERT_STATUS( msch_open(dev_addr, p_msc_interface_desc, &length) ); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   TEST_ASSERT_EQUAL(p_msc_interface_desc->bInterfaceNumber, p_msc->interface_number); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-04-18 13:51:54 +07:00
										 |  |  | #endif
 |