os/kernelhwsrv/kerneltest/f32test/smassstorage/src/t_gml_tur.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 //
    15 
    16 #include <e32std.h>
    17 #include <e32std_private.h>
    18 #include <e32test.h>
    19 #include <testusbc.h>
    20 #include "t_gml_tur_protocol.h"
    21 #include "t_gml_tur_controller.h"
    22 #include "cbulkonlytransport.h"
    23 
    24 LOCAL_D RTest test(_L("t_gml_tur"));
    25 
    26 _LIT(KDriverFileName,"TESTUSBC.LDD");
    27 _LIT(KLddName,"usbc");
    28 _LIT(KTransportThreadName,"TransportThread");
    29 _LIT(KTransportSemName,"TransportThreadSem");
    30 
    31 LOCAL_D const TInt KFiveDrives = 5;
    32 LOCAL_D const TEndpointNumber KOutEp = EEndpoint1;
    33 
    34 GLDEF_D CUsbMassStorageController* gController = NULL;
    35 
    36 LOCAL_C void SetCBWHeader(TDes8& cbwData, TInt32 aDataTransferLength, TBool aDataIn, TUint8 aLun, TUint8 aCBLength)
    37 	{
    38 	//dCBWSignature
    39 	cbwData[0] = 0x55;
    40 	cbwData[1] = 0x53;
    41 	cbwData[2] = 0x42;
    42 	cbwData[3] = 0x43;
    43 	//dCBWTag
    44 	cbwData[4] = 0x01;
    45 	cbwData[5] = 0x00;
    46 	cbwData[6] = 0x00;
    47 	cbwData[7] = 0x00;
    48 	//dCBWDataTransferLength
    49 	cbwData[8] = TUint8((aDataTransferLength & 0x000000FF));
    50 	cbwData[9] = TUint8((aDataTransferLength & 0x0000FF00) >> 8);
    51 	cbwData[10] = TUint8((aDataTransferLength & 0x00FF0000) >> 16);
    52 	cbwData[11] = TUint8((aDataTransferLength & 0xFF000000) >> 24);
    53 	//bmCBWFlags
    54 	if (aDataIn)
    55 		{
    56 		cbwData[12] = 0x80;
    57 		}
    58 	else
    59 		{
    60 		cbwData[12] = 0x00;
    61 		}
    62 	//bCBWLUN
    63 	cbwData[13] = aLun;
    64 	//bCBWCBLength
    65 	cbwData[14] = aCBLength;
    66 	}
    67 	
    68 LOCAL_C TInt TransportThreadEntry(TAny* aPtr)
    69 	{
    70 	TInt err = KErrNone;
    71 	TInt numDrives = KFiveDrives;
    72 	
    73 	//Create and install cleanup trap and active scheduler
    74 	CTrapCleanup* cleanup = CTrapCleanup::New();
    75 	if (cleanup == NULL)
    76 		{
    77 		return KErrNoMemory;
    78 		}
    79 
    80 	CActiveScheduler* sched = new CActiveScheduler;
    81 	if (sched == NULL)
    82 		{
    83 		return KErrNoMemory;
    84 		}
    85 	CActiveScheduler::Install(sched);
    86 	
    87 	//Start transport
    88 	CScsiProtocol* protocol = NULL;
    89 	TRAP(err, protocol = CScsiProtocol::NewL());
    90 	if (err != KErrNone)
    91 		{
    92 		return err;
    93 		}
    94 		
    95 	CUsbMassStorageController* controller = (CUsbMassStorageController*)aPtr;
    96 	gController = controller;
    97 	controller->CreateL(0);
    98 	
    99 	CBulkOnlyTransport* transport = NULL;
   100 	TRAP(err, transport = CBulkOnlyTransport::NewL(numDrives, *controller));
   101 	if (err != KErrNone)
   102 		{
   103 		return err;
   104 		}
   105 
   106 	controller->SetTransport(transport);
   107 	TRAP(err, transport->InitialiseTransportL(1));
   108 
   109 	transport->RegisterProtocol(*protocol);
   110 	transport->Start();
   111 	
   112 	//Synchronize with test thread
   113 	RSemaphore gSemThreadReady;
   114 	gSemThreadReady.OpenGlobal(KTransportSemName);
   115 	gSemThreadReady.Signal();
   116 	gSemThreadReady.Close();
   117 	
   118 	CActiveScheduler::Start();
   119 	
   120 	delete transport;
   121 	delete controller;
   122 	delete protocol;
   123 	delete sched;
   124 	delete cleanup;
   125 	
   126 	return KErrNone;
   127 	}
   128 	
   129 GLDEF_C TInt E32Main()
   130 	{
   131 	test.Title();
   132 	
   133 	TInt err;
   134 	
   135 	test.Start(_L("Loading ldd"));
   136 	err = User::LoadLogicalDevice(KDriverFileName);
   137 	test(err == KErrNone || err == KErrAlreadyExists);
   138 	
   139 	RDevTestUsbcClient ldd;
   140 	err = ldd.Open(0);
   141 	test(err == KErrNone);
   142 	
   143 	//ldd.ResetEndpoints();
   144 	
   145 	RSemaphore gSemThreadReady;
   146 	err = gSemThreadReady.CreateGlobal(KTransportSemName, 0);
   147 	
   148 	CUsbMassStorageController* controller = new CUsbMassStorageController();
   149 	test(controller != NULL);
   150 		
   151 	//Start transport thread.
   152 	RThread transportThread;
   153 	test.Next(_L("Creating transport thread, Max Lun = 4"));
   154 	err = transportThread.Create(KTransportThreadName, TransportThreadEntry, KDefaultStackSize, NULL, (void*)controller);
   155 	test(err == KErrNone);
   156 	transportThread.Resume();
   157 	
   158 	//Synchronize with transport thread.
   159 	gSemThreadReady.Wait();
   160 	gSemThreadReady.Close();
   161 	test(gController != NULL);
   162 		
   163 	TRequestStatus status;
   164 	
   165 	test.Next(_L("Writing GetMaxLun request to endpoint 0"));
   166 	//Write GetMaxLun request to endpoint 0
   167 	TBuf8<KRequestHdrSize> controlData;
   168 	controlData.SetLength(KRequestHdrSize);
   169 	controlData.FillZ();
   170 	controlData[0] = 0xA1;
   171 	controlData[1] = 0xFE;
   172 	controlData[6] = 0x01;
   173 	ldd.HostWrite(status, EEndpoint0, controlData, KRequestHdrSize);
   174 	
   175 	User::WaitForRequest(status);
   176 	test.Printf(_L("status = %d"), status.Int());
   177 	test(status.Int() == KErrNone);
   178 	
   179 	//Read request response
   180 	controlData.SetLength(1);
   181 	ldd.HostRead(status, EEndpoint0, controlData, 1);
   182 	
   183 	User::WaitForRequest(status);
   184 	test(status.Int() == KErrNone);
   185 	
   186 	test.Printf(_L("Max LUN: %d\n"), controlData[0]);
   187 	test(controlData[0] == KFiveDrives - 1);
   188 	
   189 	//Send TEST UNIT READY command
   190 	TBuf8<KCbwLength> cbwData;
   191 	cbwData.SetLength(KCbwLength);
   192 	cbwData.FillZ();
   193 	SetCBWHeader(cbwData, 6, ETrue, 0, 6);
   194 	ldd.HostWrite(status, KOutEp, cbwData, KCbwLength);
   195 	User::WaitForRequest(status);
   196 	test(status.Int() == KErrNone);
   197 	
   198 	test.Next(_L("Writing Reset request to endpoint 0"));
   199 	//Write Reset request to endpoint 0
   200 	controlData.SetLength(KRequestHdrSize);
   201 	controlData.FillZ();
   202 	controlData[0] = 0x21;
   203 	controlData[1] = 0xFF;
   204 	ldd.HostWrite(status, EEndpoint0, controlData, KRequestHdrSize);
   205 	
   206 	User::WaitForRequest(status);
   207 	test(status.Int() == KErrNone);
   208 	
   209 	User::After(3000000); //3 seconds
   210 	test(gController->IsReset());
   211 	test.Printf(_L("Controller got reset request\n"));
   212 	
   213 	TRequestStatus logonStatus;
   214 	transportThread.Logon(logonStatus);
   215 	TRequestStatus* statusPtr = &(controller->iStatus);
   216 	transportThread.RequestComplete(statusPtr, KErrNone);
   217 	//Wait for thread to die
   218 	test.Printf(_L("Waiting for controller thread to die\n"));
   219 	User::WaitForRequest(logonStatus);
   220 	transportThread.Close();
   221 	test.Printf(_L("The thread is dead, long live the thread\n"));
   222 	
   223 	ldd.Close();
   224 	
   225 	test.Printf(_L("Unloading ldd"));
   226 	err = User::FreeLogicalDevice(KLddName);
   227 	test(err == KErrNone);
   228 	
   229     test.End();
   230     
   231 	return 0;
   232 	}