os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/drivepublisher.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) 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
// Class implementation of the drive publishing classes -
sl@0
    15
// RDriveMediaErrorPublisher,
sl@0
    16
// RDriveStateChangedPublisher,
sl@0
    17
// CDriveTransferPublisher,
sl@0
    18
// CDriveWriteTransferPublisher,
sl@0
    19
// CDriveReadTransferPublisher,
sl@0
    20
// CUsbTransferPublisher,
sl@0
    21
// CUsbReadTransferPublisher,
sl@0
    22
// CUsbReadTransferPublisher.
sl@0
    23
// 
sl@0
    24
//
sl@0
    25
sl@0
    26
sl@0
    27
sl@0
    28
/**
sl@0
    29
 @file
sl@0
    30
 @internalTechnology
sl@0
    31
*/
sl@0
    32
sl@0
    33
#include <e32std.h>
sl@0
    34
#include <e32base.h>
sl@0
    35
#include <e32property.h>
sl@0
    36
#include <f32file.h>
sl@0
    37
sl@0
    38
#include "mstypes.h"
sl@0
    39
#include "msctypes.h"
sl@0
    40
#include "usbmsshared.h"
sl@0
    41
sl@0
    42
#include "drivepublisher.h"
sl@0
    43
#include "drivemanager.h"
sl@0
    44
#include "debug.h"
sl@0
    45
#include "msdebug.h"
sl@0
    46
sl@0
    47
//
sl@0
    48
// Use Lookup table to translate from the internal pair of state variables
sl@0
    49
// to the externally published drive state code.
sl@0
    50
//
sl@0
    51
LOCAL_D	const TUint8 table[][5] =
sl@0
    52
{
sl@0
    53
//TMountState=EDisconnected
sl@0
    54
	{EUsbMsDriveState_Disconnected,
sl@0
    55
	 EUsbMsDriveState_Disconnected,
sl@0
    56
	 EUsbMsDriveState_Disconnected,
sl@0
    57
	 EUsbMsDriveState_Disconnected,
sl@0
    58
	 EUsbMsDriveState_Disconnected},
sl@0
    59
//TMountState=EConnecting
sl@0
    60
	{EUsbMsDriveState_Connecting,
sl@0
    61
	 EUsbMsDriveState_Connecting,
sl@0
    62
	 EUsbMsDriveState_Connecting,
sl@0
    63
	 EUsbMsDriveState_Connecting,
sl@0
    64
	 EUsbMsDriveState_Connecting},
sl@0
    65
//TMountState=EConnected
sl@0
    66
	//EIdle,EActive,ELocked,EMediaNotPresent,EErrDisMounted
sl@0
    67
	{EUsbMsDriveState_Connected,
sl@0
    68
	 EUsbMsDriveState_Active,
sl@0
    69
	 EUsbMsDriveState_Locked,
sl@0
    70
	 EUsbMsDriveState_MediaNotPresent,
sl@0
    71
	 EUsbMsDriveState_Removed},
sl@0
    72
//TMountState=EDisconnecting
sl@0
    73
	{EUsbMsDriveState_Disconnecting,
sl@0
    74
	 EUsbMsDriveState_Disconnecting,
sl@0
    75
	 EUsbMsDriveState_Disconnecting,
sl@0
    76
	 EUsbMsDriveState_Disconnecting,
sl@0
    77
	 EUsbMsDriveState_Disconnecting}
sl@0
    78
};
sl@0
    79
sl@0
    80
sl@0
    81
//----------------------------------------------------------------------------
sl@0
    82
/**
sl@0
    83
Constructor
sl@0
    84
*/
sl@0
    85
RDriveMediaErrorPublisher::RDriveMediaErrorPublisher()
sl@0
    86
	{
sl@0
    87
	__MSFNLOG
sl@0
    88
	_LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
sl@0
    89
	_LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
sl@0
    90
sl@0
    91
	TInt result = RProperty::Define(EUsbMsDriveState_MediaError, RProperty::EInt,
sl@0
    92
									KMassStorageReadPolicy, KMassStorageWritePolicy);
sl@0
    93
sl@0
    94
	__ASSERT_DEBUG(result == KErrAlreadyExists || result == KErrNone, User::Invariant());
sl@0
    95
sl@0
    96
	result = iMediaErrorProperty.Attach(KUsbMsDriveState_Category, EUsbMsDriveState_MediaError);
sl@0
    97
	__ASSERT_DEBUG(result == KErrNone, User::Invariant());
sl@0
    98
	}
sl@0
    99
sl@0
   100
sl@0
   101
RDriveMediaErrorPublisher::~RDriveMediaErrorPublisher()
sl@0
   102
	{
sl@0
   103
	__MSFNLOG
sl@0
   104
	iMediaErrorProperty.Close();
sl@0
   105
	RProperty::Delete(KUsbMsDriveState_Category, EUsbMsDriveState_MediaError);
sl@0
   106
	}
sl@0
   107
sl@0
   108
/**
sl@0
   109
Publishing method
sl@0
   110
sl@0
   111
Publishes the Media Error property event
sl@0
   112
sl@0
   113
@param aError ETrue if drive media has an error else EFalse for no error
sl@0
   114
*/
sl@0
   115
void RDriveMediaErrorPublisher::PublishErrorL(TBool aError)
sl@0
   116
	{
sl@0
   117
    __MSFNLOG
sl@0
   118
	__PRINT1(_L("<< RDriveMediaErrorPublisher::PublishError %x"), aError);
sl@0
   119
sl@0
   120
	TInt oldValue;
sl@0
   121
	iMediaErrorProperty.Get(oldValue);
sl@0
   122
sl@0
   123
	if (oldValue != aError)
sl@0
   124
		{
sl@0
   125
		User::LeaveIfError(iMediaErrorProperty.Set(aError));
sl@0
   126
		}
sl@0
   127
	}
sl@0
   128
sl@0
   129
//----------------------------------------------------------------------------
sl@0
   130
/**
sl@0
   131
Constructor
sl@0
   132
sl@0
   133
@param aDrives
sl@0
   134
@param aDriveMap
sl@0
   135
*/
sl@0
   136
RDriveStateChangedPublisher::RDriveStateChangedPublisher(const TMsDriveList& aDrives,
sl@0
   137
														 const TLunToDriveMap& aDriveMap)
sl@0
   138
	:
sl@0
   139
	iDrives(aDrives),
sl@0
   140
	iDriveMap(aDriveMap)
sl@0
   141
	{
sl@0
   142
	__MSFNLOG
sl@0
   143
	_LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
sl@0
   144
	_LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
sl@0
   145
sl@0
   146
	TInt result = RProperty::Define(KUsbMsDriveState_Category,
sl@0
   147
									EUsbMsDriveState_DriveStatus, RProperty::EByteArray,
sl@0
   148
									KMassStorageReadPolicy, KMassStorageWritePolicy,
sl@0
   149
									KUsbMsMaxDrives*2);
sl@0
   150
	__ASSERT_DEBUG(result == KErrAlreadyExists || result == KErrNone, User::Invariant());
sl@0
   151
	result = result;	// remove urel warning
sl@0
   152
	}
sl@0
   153
sl@0
   154
sl@0
   155
RDriveStateChangedPublisher::~RDriveStateChangedPublisher()
sl@0
   156
	{
sl@0
   157
	__MSFNLOG
sl@0
   158
	RProperty::Delete(KUsbMsDriveState_Category, EUsbMsDriveState_DriveStatus);
sl@0
   159
	}
sl@0
   160
sl@0
   161
sl@0
   162
/**
sl@0
   163
Publishing method
sl@0
   164
sl@0
   165
Sends a property event on behalf of CMassStorageDrive, with the mountstate and drivestate
sl@0
   166
values encoded into one 32-bit word.
sl@0
   167
*/
sl@0
   168
void RDriveStateChangedPublisher::DriveStateChanged()
sl@0
   169
	{
sl@0
   170
	__MSFNLOG
sl@0
   171
	TUsbMsDrivesStatus allDrivesStatus;
sl@0
   172
sl@0
   173
	for(TUint8 i = 0; i < iDrives.Count(); i++)
sl@0
   174
		{
sl@0
   175
		allDrivesStatus.Append(iDriveMap[i]);
sl@0
   176
sl@0
   177
		CMassStorageDrive::TMountState ms = iDrives[i]->MountState();
sl@0
   178
		TLocalDriveRef::TDriveState ds = iDrives[i]->DriveState();
sl@0
   179
		TInt driveStatus = EUsbMsDriveState_Error;
sl@0
   180
		if((TUint8)ds < sizeof(table[0]) && (TUint8)ms < sizeof(table)/sizeof(table[0]))
sl@0
   181
			{
sl@0
   182
			driveStatus = table[ms][ds];
sl@0
   183
			__PRINT3(_L("ms=%d ds=%d %d"), ms, ds, driveStatus);
sl@0
   184
			}
sl@0
   185
		allDrivesStatus.Append(driveStatus);
sl@0
   186
		}
sl@0
   187
sl@0
   188
sl@0
   189
	__PRINT1(_L("Publishing EUsbMsDriveState_DriveStatus for %d drives\n"),
sl@0
   190
				allDrivesStatus.Length()/2);
sl@0
   191
sl@0
   192
	if(KErrNone != RProperty::Set(KUsbMsDriveState_Category,
sl@0
   193
								  EUsbMsDriveState_DriveStatus,
sl@0
   194
								  allDrivesStatus))
sl@0
   195
		{
sl@0
   196
		__ASSERT_DEBUG(EFalse,User::Invariant());
sl@0
   197
		}
sl@0
   198
	}
sl@0
   199
sl@0
   200
sl@0
   201
//----------------------------------------------------------------------------
sl@0
   202
sl@0
   203
/**
sl@0
   204
Private default constructor to ensure that NewL is used
sl@0
   205
sl@0
   206
@param aSubKey
sl@0
   207
@param aArray
sl@0
   208
*/
sl@0
   209
CUsbTransferPublisher::CUsbTransferPublisher(TUsbMsDriveState_Subkey aSubKey,
sl@0
   210
                                             const TBytesTransferedList& aArray)
sl@0
   211
:   iSubKey(aSubKey),
sl@0
   212
	iArray(aArray)
sl@0
   213
	{
sl@0
   214
    __MSFNLOG
sl@0
   215
	}
sl@0
   216
sl@0
   217
sl@0
   218
void CUsbTransferPublisher::ConstructL()
sl@0
   219
	{
sl@0
   220
	__MSFNLOG
sl@0
   221
sl@0
   222
	_LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
sl@0
   223
	_LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
sl@0
   224
sl@0
   225
	TInt r = RProperty::Define(iSubKey, RProperty::EByteArray,
sl@0
   226
							   KMassStorageReadPolicy, KMassStorageWritePolicy,
sl@0
   227
							   KUsbMsMaxDrives*sizeof(TInt));
sl@0
   228
sl@0
   229
	if (r != KErrAlreadyExists)
sl@0
   230
		{
sl@0
   231
		User::LeaveIfError(r);
sl@0
   232
		}
sl@0
   233
sl@0
   234
	// Attach to the properties here. Only do this once, continuously attaching
sl@0
   235
	// will currently cause a memory leak
sl@0
   236
	User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, iSubKey));
sl@0
   237
sl@0
   238
	// Create the EDataTransferred timer
sl@0
   239
	iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
sl@0
   240
	iTimerRunning = EFalse;
sl@0
   241
	}
sl@0
   242
sl@0
   243
sl@0
   244
/**
sl@0
   245
Destructor
sl@0
   246
*/
sl@0
   247
CUsbTransferPublisher::~CUsbTransferPublisher()
sl@0
   248
	{
sl@0
   249
	__MSFNLOG
sl@0
   250
	if(iTimer)
sl@0
   251
		{
sl@0
   252
		iTimer->Cancel();
sl@0
   253
		}
sl@0
   254
	delete iTimer;
sl@0
   255
	iProperty.Close();
sl@0
   256
sl@0
   257
	RProperty::Delete(KUsbMsDriveState_Category, iSubKey);
sl@0
   258
	}
sl@0
   259
sl@0
   260
sl@0
   261
/**
sl@0
   262
A static wrapper for the DoPublishDataTransferredEvent member function
sl@0
   263
for use as a timer callback function.
sl@0
   264
sl@0
   265
@param obj 'this' pointer
sl@0
   266
@return not used in CPeriodic callback (see TCallback)
sl@0
   267
*/
sl@0
   268
TInt CUsbTransferPublisher::PublishDataTransferredEvent(TAny* obj)
sl@0
   269
	{
sl@0
   270
	__MSFNSLOG
sl@0
   271
	static_cast<CUsbTransferPublisher*>(obj)->DoPublishDataTransferredEvent();
sl@0
   272
	return 1;
sl@0
   273
	}
sl@0
   274
sl@0
   275
sl@0
   276
/**
sl@0
   277
Update the data transferred properties if the counts have changed since
sl@0
   278
the last update.
sl@0
   279
*/
sl@0
   280
void CUsbTransferPublisher::DoPublishDataTransferredEvent()
sl@0
   281
	{
sl@0
   282
    __MSFNLOG
sl@0
   283
	if (PublishDataTransferred())
sl@0
   284
		{
sl@0
   285
		// some data has been transfered so reset the counter
sl@0
   286
		iTimerCancelCnt = ETimerCancelDelay;
sl@0
   287
		}
sl@0
   288
sl@0
   289
	// Update the cancel count if no data was transferred the last
sl@0
   290
	// (few) times this has been called
sl@0
   291
	if (--iTimerCancelCnt == 0)
sl@0
   292
		{
sl@0
   293
		StopTimer();
sl@0
   294
		iTimerCancelCnt = ETimerCancelDelay;
sl@0
   295
		}
sl@0
   296
	}
sl@0
   297
sl@0
   298
sl@0
   299
/**
sl@0
   300
Update the data transferred properties if the counts have changed since
sl@0
   301
the last update.
sl@0
   302
*/
sl@0
   303
TBool CUsbTransferPublisher::PublishDataTransferred()
sl@0
   304
	{
sl@0
   305
	__MSFNLOG
sl@0
   306
	TUsbMsBytesTransferred bytesTransferred;
sl@0
   307
	TBool dataTransferred = EFalse;
sl@0
   308
sl@0
   309
	for (TInt i = 0; i < iArray.Count(); i++)
sl@0
   310
		{
sl@0
   311
		bytesTransferred[i] = GetBytesTransferred(i);
sl@0
   312
		}
sl@0
   313
sl@0
   314
	// Update the properties only if they have changed
sl@0
   315
	// (or if there's an error reading the old value.)
sl@0
   316
	// Possible optimisation: keep a copy of the value
sl@0
   317
	// as a member variable so we don't need the Get.
sl@0
   318
	TUsbMsBytesTransferred oldValue;
sl@0
   319
sl@0
   320
	if ((iProperty.Get(oldValue) != KErrNone) || (oldValue != bytesTransferred))
sl@0
   321
		{
sl@0
   322
#ifdef __PRINT3
sl@0
   323
		// trace of the bytes transferred
sl@0
   324
		for (TInt j=0; j < iArray.Count(); j++)
sl@0
   325
			{
sl@0
   326
			if(oldValue[j] != bytesTransferred[j])
sl@0
   327
				{
sl@0
   328
				__PRINT3(_L("CDrivePublisher: KBytes[%d] %d->%d\n"), j, oldValue[j], bytesTransferred[j]);
sl@0
   329
				}
sl@0
   330
			}
sl@0
   331
#endif
sl@0
   332
		if (KErrNone != iProperty.Set(bytesTransferred))
sl@0
   333
			{
sl@0
   334
			__ASSERT_DEBUG(EFalse, User::Invariant());
sl@0
   335
			}
sl@0
   336
		dataTransferred = ETrue;
sl@0
   337
		}
sl@0
   338
sl@0
   339
	return dataTransferred;
sl@0
   340
	}
sl@0
   341
sl@0
   342
sl@0
   343
/**
sl@0
   344
Starts timer to periodically publish results.
sl@0
   345
If the timer is not yet running then start it.
sl@0
   346
*/
sl@0
   347
void CUsbTransferPublisher::StartTimer()
sl@0
   348
	{
sl@0
   349
	__MSFNLOG
sl@0
   350
	if (!iTimerRunning)
sl@0
   351
		{
sl@0
   352
		// EDataTransferred event every second
sl@0
   353
		const TTimeIntervalMicroSeconds32 interval = 1 * 1000 * 1000;
sl@0
   354
		TCallBack callback(PublishDataTransferredEvent, this);
sl@0
   355
		__PRINT(_L("Starting timer"));
sl@0
   356
		iTimer->Start(interval, interval, callback);
sl@0
   357
		iTimerRunning = ETrue;
sl@0
   358
		}
sl@0
   359
	}
sl@0
   360
sl@0
   361
sl@0
   362
/**
sl@0
   363
Ensure that the Timer is stopped
sl@0
   364
*/
sl@0
   365
void CUsbTransferPublisher::StopTimer()
sl@0
   366
	{
sl@0
   367
	__MSFNLOG
sl@0
   368
	if (iTimerRunning)
sl@0
   369
		{
sl@0
   370
		__PRINT(_L("Stopping timer"));
sl@0
   371
		iTimer->Cancel();
sl@0
   372
		iTimerRunning = EFalse;
sl@0
   373
		}
sl@0
   374
	}
sl@0
   375
sl@0
   376
sl@0
   377
//----------------------------------------------------------------------------
sl@0
   378
/**
sl@0
   379
Constructor for Write property
sl@0
   380
sl@0
   381
@param aArray
sl@0
   382
*/
sl@0
   383
CUsbWriteTransferPublisher* CUsbWriteTransferPublisher::NewL(const TBytesTransferedList& aArray)
sl@0
   384
	{
sl@0
   385
	__MSFNSLOG
sl@0
   386
	CUsbWriteTransferPublisher* self = new (ELeave) CUsbWriteTransferPublisher(aArray);
sl@0
   387
	CleanupStack::PushL(self);
sl@0
   388
	self->ConstructL();
sl@0
   389
	CleanupStack::Pop();
sl@0
   390
	return self;
sl@0
   391
	}
sl@0
   392
sl@0
   393
sl@0
   394
CUsbWriteTransferPublisher::CUsbWriteTransferPublisher(const TBytesTransferedList& aArray)
sl@0
   395
:   CUsbTransferPublisher(EUsbMsDriveState_KBytesWritten, aArray)
sl@0
   396
	{
sl@0
   397
    __MSFNLOG
sl@0
   398
	}
sl@0
   399
sl@0
   400
sl@0
   401
//----------------------------------------------------------------------------
sl@0
   402
/**
sl@0
   403
Constructor for Read property
sl@0
   404
sl@0
   405
@param aArray
sl@0
   406
*/
sl@0
   407
CUsbReadTransferPublisher* CUsbReadTransferPublisher::NewL(const TBytesTransferedList& aArray)
sl@0
   408
	{
sl@0
   409
	__MSFNSLOG
sl@0
   410
	CUsbReadTransferPublisher* self = new (ELeave) CUsbReadTransferPublisher(aArray);
sl@0
   411
	CleanupStack::PushL(self);
sl@0
   412
	self->ConstructL();
sl@0
   413
	CleanupStack::Pop();
sl@0
   414
	return self;
sl@0
   415
	}
sl@0
   416
sl@0
   417
sl@0
   418
CUsbReadTransferPublisher::CUsbReadTransferPublisher(const TBytesTransferedList& aArray)
sl@0
   419
:   CUsbTransferPublisher(EUsbMsDriveState_KBytesRead, aArray)
sl@0
   420
	{
sl@0
   421
    __MSFNLOG
sl@0
   422
	}
sl@0
   423
sl@0
   424
/**
sl@0
   425
Transfer function for the property
sl@0
   426
sl@0
   427
@return Cumulative bytes read since the host connected to the drive,
sl@0
   428
        in multiples of KBytesPerKilobyte rounded to nearest integer value.
sl@0
   429
        The KBytes refer to multiples of 1000, not 1024.
sl@0
   430
*/
sl@0
   431
TUint CUsbTransferPublisher::GetBytesTransferred(TLun aLun) const
sl@0
   432
{
sl@0
   433
	return I64LOW(iArray[aLun] / (TUint64)1000);
sl@0
   434
}