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