| 
									
										
										
										
											2013-05-06 20:25:38 +07:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |     @file     test_msc_host.c | 
					
						
							|  |  |  |     @author   hathach (tinyusb.org) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @section LICENSE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Software License Agreement (BSD License) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Copyright (c) 2013, hathach (tinyusb.org) | 
					
						
							|  |  |  |     All rights reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |     modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |     1. Redistributions of source code must retain the above copyright | 
					
						
							|  |  |  |     notice, this list of conditions and the following disclaimer. | 
					
						
							|  |  |  |     2. Redistributions in binary form must reproduce the above copyright | 
					
						
							|  |  |  |     notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  |     documentation and/or other materials provided with the distribution. | 
					
						
							|  |  |  |     3. Neither the name of the copyright holders nor the | 
					
						
							|  |  |  |     names of its contributors may be used to endorse or promote products | 
					
						
							|  |  |  |     derived from this software without specific prior written permission. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | 
					
						
							|  |  |  |     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
					
						
							|  |  |  |     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY | 
					
						
							|  |  |  |     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
					
						
							|  |  |  |     INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
					
						
							|  |  |  |     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND | 
					
						
							|  |  |  |     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
					
						
							|  |  |  |     INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS | 
					
						
							|  |  |  |     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This file is part of the tinyusb stack. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, pipe_null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 13:51:54 +07:00
										 |  |  |   TEST_ASSERT(TUSB_ERROR_NONE != msch_open_subtask(dev_addr, p_msc_interface_desc, &length)); | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test_open_pipe_out_failed(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, (pipe_handle_t) {1} ); | 
					
						
							|  |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_out, TUSB_CLASS_MSC, pipe_null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 13:51:54 +07:00
										 |  |  |   TEST_ASSERT(TUSB_ERROR_NONE != msch_open_subtask(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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-24 15:05:11 +07:00
										 |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, pipe_in); | 
					
						
							|  |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_out, TUSB_CLASS_MSC, pipe_out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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 -------------//
 | 
					
						
							|  |  |  |   TEST_ASSERT_STATUS( msch_open_subtask(dev_addr, p_msc_interface_desc, &length) ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_in, TUSB_CLASS_MSC, pipe_in); | 
					
						
							|  |  |  |   hcd_pipe_open_ExpectAndReturn(dev_addr, p_edp_out, TUSB_CLASS_MSC, pipe_out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- get max lun -------------//
 | 
					
						
							|  |  |  |   usbh_control_xfer_subtask_StubWithCallback(stub_control_xfer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- Code Under Test -------------//
 | 
					
						
							|  |  |  |   TEST_ASSERT_STATUS( msch_open_subtask(dev_addr, p_msc_interface_desc, &length) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   TEST_ASSERT_EQUAL(p_msc_interface_desc->bInterfaceNumber, p_msc->interface_number); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-04-18 13:51:54 +07:00
										 |  |  | #endif
 |