baseintegtests/baseintegrationtest/testsuites/sd/src/sdusb.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Enables USB mass storage mode. Ends when disconnected by user.
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <massstorage.h>
       
    19 #include <d32usbc.h>
       
    20 #include "sdusb.h"
       
    21 
       
    22 /*
       
    23 Class constructor
       
    24 
       
    25 @param None
       
    26 @return None
       
    27 */
       
    28 CBaseTestSDUsb::CBaseTestSDUsb()
       
    29 	{
       
    30 	SetTestStepName(KTestStepUsb);
       
    31 	}
       
    32 
       
    33 /*
       
    34 Test Step Preamble
       
    35  - Initialise attribute iDrive
       
    36  - Connect to the File Server
       
    37 
       
    38 @param None
       
    39 @return EPass if successful or EFail if not
       
    40 @see TVerdict
       
    41 */
       
    42 TVerdict CBaseTestSDUsb::doTestStepPreambleL()
       
    43 	{
       
    44 	SetTestStepResult(EFail);
       
    45 	
       
    46 	if (!InitDriveLetter())
       
    47 		return TestStepResult();
       
    48 	if (!InitFileServer())
       
    49 		return TestStepResult();
       
    50 
       
    51 	SetTestStepResult(EPass);
       
    52 	return TestStepResult();
       
    53 	}
       
    54 
       
    55 /*
       
    56 Test step
       
    57 
       
    58 @param None
       
    59 @return EPass if successful or EFail if not
       
    60 @see TVerdict
       
    61 */
       
    62 TVerdict CBaseTestSDUsb::doTestStepL()
       
    63 	{
       
    64 	TInt r;
       
    65 	_LIT(KMsFsy, "MSFS.FSY");
       
    66 	_LIT(KMsFs, "MassStorageFileSystem");
       
    67 	
       
    68 	// Add MS file system	
       
    69 	r = iFs.AddFileSystem(KMsFsy);
       
    70 	if (r != KErrNone && r != KErrAlreadyExists)
       
    71 		{
       
    72 		ERR_PRINTF2(_L("AddFileSystem failed: %d"), r);
       
    73 		SetTestStepResult(EFail);
       
    74 		return TestStepResult();
       
    75 		}
       
    76 	
       
    77 	RDevUsbcClient usb;
       
    78 
       
    79 	// Load the logical device
       
    80 	_LIT(KDriverFileName,"EUSBC.LDD");
       
    81 	r = User::LoadLogicalDevice(KDriverFileName);
       
    82 	if (r != KErrNone && r != KErrAlreadyExists)
       
    83 		{
       
    84 		ERR_PRINTF2(_L("LoadLogicalDevice failed: %d"), r);
       
    85 		SetTestStepResult(EFail);
       
    86 		return TestStepResult();
       
    87 		}
       
    88 
       
    89 	r = usb.Open(0);
       
    90 	if (r != KErrNone)
       
    91 		{
       
    92 		ERR_PRINTF2(_L("RDevUsbcClient::Open failed: %d"), r);
       
    93 		SetTestStepResult(EFail);
       
    94 		return TestStepResult();
       
    95 		}
       
    96 
       
    97 	RUsbMassStorage UsbMs;
       
    98 	TBuf<8>  t_vendorId(_L("vendor"));
       
    99 	TBuf<16> t_productId(_L("product"));
       
   100 	TBuf<4>  t_productRev(_L("1.00"));
       
   101 
       
   102 	TMassStorageConfig msConfig;
       
   103 	msConfig.iVendorId.Copy(t_vendorId);
       
   104 	msConfig.iProductId.Copy(t_productId);
       
   105 	msConfig.iProductRev.Copy(t_productRev);
       
   106 
       
   107 	// Connect to Mass Storage
       
   108 	r = UsbMs.Connect();
       
   109 	if (r != KErrNone)
       
   110 		{
       
   111 		ERR_PRINTF2(_L("RUsbMassStorage::Connect failed: %d"), r);
       
   112 		SetTestStepResult(EFail);
       
   113 		return TestStepResult();
       
   114 		}
       
   115 
       
   116 	// Start Mass Storage
       
   117 	r = UsbMs.Start(msConfig);
       
   118 	if (r != KErrNone)
       
   119 		{
       
   120 		ERR_PRINTF2(_L("RUsbMassStorage::Start failed: %d"), r);
       
   121 		SetTestStepResult(EFail);
       
   122 		return TestStepResult();
       
   123 		}
       
   124 
       
   125 	TBuf8<KUsbDescSize_Device> deviceDescriptor;
       
   126 	r = usb.GetDeviceDescriptor(deviceDescriptor);
       
   127 	if (r != KErrNone)
       
   128 		{
       
   129 		ERR_PRINTF2(_L("RDevUsbcClient::GetDeviceDescriptor failed: %d"), r);
       
   130 		SetTestStepResult(EFail);
       
   131 		return TestStepResult();
       
   132 		}
       
   133 
       
   134 	const TInt KUsbSpecOffset = 2;
       
   135 	const TInt KUsbDeviceClassOffset = 4;
       
   136 	const TInt KUsbVendorIdOffset = 8;
       
   137 	const TInt KUsbProductIdOffset = 10;
       
   138 	const TInt KUsbDevReleaseOffset = 12;
       
   139 	//Change the USB spec number to 2.00
       
   140 	deviceDescriptor[KUsbSpecOffset]   = 0x00;
       
   141 	deviceDescriptor[KUsbSpecOffset+1] = 0x02;
       
   142 	//Change the Device Class, Device SubClass and Device Protocol 
       
   143 	deviceDescriptor[KUsbDeviceClassOffset] = 0x00;
       
   144 	deviceDescriptor[KUsbDeviceClassOffset+1] = 0x00;
       
   145 	deviceDescriptor[KUsbDeviceClassOffset+2] = 0x00;
       
   146 	//Change the device vendor ID (VID) to 0x0E22 (Symbian)
       
   147 	deviceDescriptor[KUsbVendorIdOffset]   = 0x22;   // little endian
       
   148 	deviceDescriptor[KUsbVendorIdOffset+1] = 0x0E;
       
   149 	//Change the device product ID (PID) to 0x1111
       
   150 	deviceDescriptor[KUsbProductIdOffset]   = 0x12;
       
   151 	deviceDescriptor[KUsbProductIdOffset+1] = 0x11;
       
   152 	//Change the device release number to 3.05
       
   153 	deviceDescriptor[KUsbDevReleaseOffset]   = 0x05;
       
   154 	deviceDescriptor[KUsbDevReleaseOffset+1] = 0x03;
       
   155 	r = usb.SetDeviceDescriptor(deviceDescriptor);
       
   156 	if (r != KErrNone)
       
   157 		{
       
   158 		ERR_PRINTF2(_L("RDevUsbcClient::SetDeviceDescriptor failed: %d"), r);
       
   159 		SetTestStepResult(EFail);
       
   160 		return TestStepResult();
       
   161 		}
       
   162 
       
   163 
       
   164 	_LIT16(productID_L, "Symbian USB Mass Storage Device");
       
   165 	TBuf16<KUsbStringDescStringMaxSize / 2> productID(productID_L);
       
   166 	// Set product string descriptor
       
   167 	r = usb.SetProductStringDescriptor(productID);
       
   168 	if (r != KErrNone)
       
   169 		{
       
   170 		ERR_PRINTF2(_L("RDevUsbcClient::SetProductStringDescriptor failed: %d"), r);
       
   171 		SetTestStepResult(EFail);
       
   172 		return TestStepResult();
       
   173 		}
       
   174 	
       
   175 	TRequestStatus enum_status;
       
   176 	INFO_PRINTF1(_L("Re-enumerating..."));
       
   177 	usb.ReEnumerate(enum_status);
       
   178 	User::WaitForRequest(enum_status);
       
   179    	INFO_PRINTF1(_L("Re-enumerating done"));
       
   180    	
       
   181    	// Mount Mass Storage FS
       
   182     r = iFs.DismountFileSystem(_L("fat"), iDrive);
       
   183     if (r != KErrNone)
       
   184    		{
       
   185 		ERR_PRINTF2(_L("RFs::DismountFileSystem (FAT) failed: %d"), r);
       
   186 		SetTestStepResult(EFail);
       
   187 		return TestStepResult();
       
   188 		}
       
   189 	
       
   190 	r = iFs.MountFileSystem(KMsFs, iDrive);
       
   191 	if (r != KErrNone)
       
   192 		{
       
   193 		ERR_PRINTF2(_L("RFs::MountFileSystem (MSFS) failed: %d"), r);
       
   194 		SetTestStepResult(EFail);
       
   195 		return TestStepResult();
       
   196 		}
       
   197 	
       
   198 	// Wait until the USB cable is removed or device is suspended
       
   199 	TUsbcDeviceState initialStatus;
       
   200 	r = usb.DeviceStatus(initialStatus);
       
   201 	if (r != KErrNone)
       
   202 		{
       
   203 		ERR_PRINTF2(_L("RDevUsbcClient::DeviceStatus failed: %d"), r);
       
   204 		SetTestStepResult(EFail);
       
   205 		return TestStepResult();
       
   206 		}
       
   207 	if (initialStatus == EUsbcDeviceStateUndefined)
       
   208 		{
       
   209 		ERR_PRINTF1(_L("USB device status is undefined"));
       
   210 		SetTestStepResult(EFail);
       
   211 		return TestStepResult();
       
   212 		}
       
   213 	TUint deviceState = initialStatus;
       
   214 	do {
       
   215 		TRequestStatus rs;
       
   216 		usb.AlternateDeviceStatusNotify(rs, deviceState);
       
   217 		User::WaitForRequest(rs);
       
   218 		} while (deviceState != EUsbcDeviceStateUndefined && deviceState != EUsbcDeviceStateSuspended);
       
   219 
       
   220 	// Dismount Mass Storage FS
       
   221     r = iFs.DismountFileSystem(KMsFs, iDrive);
       
   222     if (r != KErrNone)
       
   223    		{
       
   224 		ERR_PRINTF2(_L("RFs::DismountFileSystem (MSFS) failed: %d"), r);
       
   225 		SetTestStepResult(EFail);
       
   226 		return TestStepResult();
       
   227 		}
       
   228 	
       
   229 	r = iFs.MountFileSystem(_L("fat"), iDrive);
       
   230 	if (r != KErrNone)
       
   231 		{
       
   232 		ERR_PRINTF2(_L("RFs::MountFileSystem (FAT) failed: %d"), r);
       
   233 		SetTestStepResult(EFail);
       
   234 		return TestStepResult();
       
   235 		}
       
   236 	
       
   237 	// Stop USB Mass Storage
       
   238 	r = UsbMs.Stop();
       
   239 	if (r != KErrNone)
       
   240 		{
       
   241 		ERR_PRINTF2(_L("RUsbMassStorage::Stop failed: %d"), r);
       
   242 		SetTestStepResult(EFail);
       
   243 		return TestStepResult();
       
   244 		}
       
   245 
       
   246 	UsbMs.Close();
       
   247 	usb.Close();
       
   248 	r = iFs.RemoveFileSystem(KMsFs);
       
   249 	if (r != KErrNone)
       
   250 		{
       
   251 		ERR_PRINTF2(_L("RFs::RemoveFileSystem failed: %d"), r);
       
   252 		SetTestStepResult(EFail);
       
   253 		return TestStepResult();
       
   254 		}
       
   255 
       
   256 	r = User::FreeLogicalDevice(_L("USBC"));
       
   257 	if (r != KErrNone)
       
   258 		{
       
   259 		ERR_PRINTF2(_L("FreeLogicalDevice failed: %d"), r);
       
   260 		SetTestStepResult(EFail);
       
   261 		return TestStepResult();
       
   262 		}
       
   263 
       
   264 	SetTestStepResult(EPass);
       
   265 	return TestStepResult();
       
   266 	}