os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/drivepublisher.cpp
Update contrib.
1 // Copyright (c) 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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Class implementation of the drive publishing classes -
15 // RDriveMediaErrorPublisher,
16 // RDriveStateChangedPublisher,
17 // CDriveTransferPublisher,
18 // CDriveWriteTransferPublisher,
19 // CDriveReadTransferPublisher,
20 // CUsbTransferPublisher,
21 // CUsbReadTransferPublisher,
22 // CUsbReadTransferPublisher.
35 #include <e32property.h>
40 #include "usbmsshared.h"
42 #include "drivepublisher.h"
43 #include "drivemanager.h"
48 // Use Lookup table to translate from the internal pair of state variables
49 // to the externally published drive state code.
51 LOCAL_D const TUint8 table[][5] =
53 //TMountState=EDisconnected
54 {EUsbMsDriveState_Disconnected,
55 EUsbMsDriveState_Disconnected,
56 EUsbMsDriveState_Disconnected,
57 EUsbMsDriveState_Disconnected,
58 EUsbMsDriveState_Disconnected},
59 //TMountState=EConnecting
60 {EUsbMsDriveState_Connecting,
61 EUsbMsDriveState_Connecting,
62 EUsbMsDriveState_Connecting,
63 EUsbMsDriveState_Connecting,
64 EUsbMsDriveState_Connecting},
65 //TMountState=EConnected
66 //EIdle,EActive,ELocked,EMediaNotPresent,EErrDisMounted
67 {EUsbMsDriveState_Connected,
68 EUsbMsDriveState_Active,
69 EUsbMsDriveState_Locked,
70 EUsbMsDriveState_MediaNotPresent,
71 EUsbMsDriveState_Removed},
72 //TMountState=EDisconnecting
73 {EUsbMsDriveState_Disconnecting,
74 EUsbMsDriveState_Disconnecting,
75 EUsbMsDriveState_Disconnecting,
76 EUsbMsDriveState_Disconnecting,
77 EUsbMsDriveState_Disconnecting}
81 //----------------------------------------------------------------------------
85 RDriveMediaErrorPublisher::RDriveMediaErrorPublisher()
88 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
89 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
91 TInt result = RProperty::Define(EUsbMsDriveState_MediaError, RProperty::EInt,
92 KMassStorageReadPolicy, KMassStorageWritePolicy);
94 __ASSERT_DEBUG(result == KErrAlreadyExists || result == KErrNone, User::Invariant());
96 result = iMediaErrorProperty.Attach(KUsbMsDriveState_Category, EUsbMsDriveState_MediaError);
97 __ASSERT_DEBUG(result == KErrNone, User::Invariant());
101 RDriveMediaErrorPublisher::~RDriveMediaErrorPublisher()
104 iMediaErrorProperty.Close();
105 RProperty::Delete(KUsbMsDriveState_Category, EUsbMsDriveState_MediaError);
111 Publishes the Media Error property event
113 @param aError ETrue if drive media has an error else EFalse for no error
115 void RDriveMediaErrorPublisher::PublishErrorL(TBool aError)
118 __PRINT1(_L("<< RDriveMediaErrorPublisher::PublishError %x"), aError);
121 iMediaErrorProperty.Get(oldValue);
123 if (oldValue != aError)
125 User::LeaveIfError(iMediaErrorProperty.Set(aError));
129 //----------------------------------------------------------------------------
136 RDriveStateChangedPublisher::RDriveStateChangedPublisher(const TMsDriveList& aDrives,
137 const TLunToDriveMap& aDriveMap)
143 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
144 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
146 TInt result = RProperty::Define(KUsbMsDriveState_Category,
147 EUsbMsDriveState_DriveStatus, RProperty::EByteArray,
148 KMassStorageReadPolicy, KMassStorageWritePolicy,
150 __ASSERT_DEBUG(result == KErrAlreadyExists || result == KErrNone, User::Invariant());
151 result = result; // remove urel warning
155 RDriveStateChangedPublisher::~RDriveStateChangedPublisher()
158 RProperty::Delete(KUsbMsDriveState_Category, EUsbMsDriveState_DriveStatus);
165 Sends a property event on behalf of CMassStorageDrive, with the mountstate and drivestate
166 values encoded into one 32-bit word.
168 void RDriveStateChangedPublisher::DriveStateChanged()
171 TUsbMsDrivesStatus allDrivesStatus;
173 for(TUint8 i = 0; i < iDrives.Count(); i++)
175 allDrivesStatus.Append(iDriveMap[i]);
177 CMassStorageDrive::TMountState ms = iDrives[i]->MountState();
178 TLocalDriveRef::TDriveState ds = iDrives[i]->DriveState();
179 TInt driveStatus = EUsbMsDriveState_Error;
180 if((TUint8)ds < sizeof(table[0]) && (TUint8)ms < sizeof(table)/sizeof(table[0]))
182 driveStatus = table[ms][ds];
183 __PRINT3(_L("ms=%d ds=%d %d"), ms, ds, driveStatus);
185 allDrivesStatus.Append(driveStatus);
189 __PRINT1(_L("Publishing EUsbMsDriveState_DriveStatus for %d drives\n"),
190 allDrivesStatus.Length()/2);
192 if(KErrNone != RProperty::Set(KUsbMsDriveState_Category,
193 EUsbMsDriveState_DriveStatus,
196 __ASSERT_DEBUG(EFalse,User::Invariant());
201 //----------------------------------------------------------------------------
204 Private default constructor to ensure that NewL is used
209 CUsbTransferPublisher::CUsbTransferPublisher(TUsbMsDriveState_Subkey aSubKey,
210 const TBytesTransferedList& aArray)
218 void CUsbTransferPublisher::ConstructL()
222 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
223 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
225 TInt r = RProperty::Define(iSubKey, RProperty::EByteArray,
226 KMassStorageReadPolicy, KMassStorageWritePolicy,
227 KUsbMsMaxDrives*sizeof(TInt));
229 if (r != KErrAlreadyExists)
231 User::LeaveIfError(r);
234 // Attach to the properties here. Only do this once, continuously attaching
235 // will currently cause a memory leak
236 User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, iSubKey));
238 // Create the EDataTransferred timer
239 iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
240 iTimerRunning = EFalse;
247 CUsbTransferPublisher::~CUsbTransferPublisher()
257 RProperty::Delete(KUsbMsDriveState_Category, iSubKey);
262 A static wrapper for the DoPublishDataTransferredEvent member function
263 for use as a timer callback function.
265 @param obj 'this' pointer
266 @return not used in CPeriodic callback (see TCallback)
268 TInt CUsbTransferPublisher::PublishDataTransferredEvent(TAny* obj)
271 static_cast<CUsbTransferPublisher*>(obj)->DoPublishDataTransferredEvent();
277 Update the data transferred properties if the counts have changed since
280 void CUsbTransferPublisher::DoPublishDataTransferredEvent()
283 if (PublishDataTransferred())
285 // some data has been transfered so reset the counter
286 iTimerCancelCnt = ETimerCancelDelay;
289 // Update the cancel count if no data was transferred the last
290 // (few) times this has been called
291 if (--iTimerCancelCnt == 0)
294 iTimerCancelCnt = ETimerCancelDelay;
300 Update the data transferred properties if the counts have changed since
303 TBool CUsbTransferPublisher::PublishDataTransferred()
306 TUsbMsBytesTransferred bytesTransferred;
307 TBool dataTransferred = EFalse;
309 for (TInt i = 0; i < iArray.Count(); i++)
311 bytesTransferred[i] = GetBytesTransferred(i);
314 // Update the properties only if they have changed
315 // (or if there's an error reading the old value.)
316 // Possible optimisation: keep a copy of the value
317 // as a member variable so we don't need the Get.
318 TUsbMsBytesTransferred oldValue;
320 if ((iProperty.Get(oldValue) != KErrNone) || (oldValue != bytesTransferred))
323 // trace of the bytes transferred
324 for (TInt j=0; j < iArray.Count(); j++)
326 if(oldValue[j] != bytesTransferred[j])
328 __PRINT3(_L("CDrivePublisher: KBytes[%d] %d->%d\n"), j, oldValue[j], bytesTransferred[j]);
332 if (KErrNone != iProperty.Set(bytesTransferred))
334 __ASSERT_DEBUG(EFalse, User::Invariant());
336 dataTransferred = ETrue;
339 return dataTransferred;
344 Starts timer to periodically publish results.
345 If the timer is not yet running then start it.
347 void CUsbTransferPublisher::StartTimer()
352 // EDataTransferred event every second
353 const TTimeIntervalMicroSeconds32 interval = 1 * 1000 * 1000;
354 TCallBack callback(PublishDataTransferredEvent, this);
355 __PRINT(_L("Starting timer"));
356 iTimer->Start(interval, interval, callback);
357 iTimerRunning = ETrue;
363 Ensure that the Timer is stopped
365 void CUsbTransferPublisher::StopTimer()
370 __PRINT(_L("Stopping timer"));
372 iTimerRunning = EFalse;
377 //----------------------------------------------------------------------------
379 Constructor for Write property
383 CUsbWriteTransferPublisher* CUsbWriteTransferPublisher::NewL(const TBytesTransferedList& aArray)
386 CUsbWriteTransferPublisher* self = new (ELeave) CUsbWriteTransferPublisher(aArray);
387 CleanupStack::PushL(self);
394 CUsbWriteTransferPublisher::CUsbWriteTransferPublisher(const TBytesTransferedList& aArray)
395 : CUsbTransferPublisher(EUsbMsDriveState_KBytesWritten, aArray)
401 //----------------------------------------------------------------------------
403 Constructor for Read property
407 CUsbReadTransferPublisher* CUsbReadTransferPublisher::NewL(const TBytesTransferedList& aArray)
410 CUsbReadTransferPublisher* self = new (ELeave) CUsbReadTransferPublisher(aArray);
411 CleanupStack::PushL(self);
418 CUsbReadTransferPublisher::CUsbReadTransferPublisher(const TBytesTransferedList& aArray)
419 : CUsbTransferPublisher(EUsbMsDriveState_KBytesRead, aArray)
425 Transfer function for the property
427 @return Cumulative bytes read since the host connected to the drive,
428 in multiples of KBytesPerKilobyte rounded to nearest integer value.
429 The KBytes refer to multiples of 1000, not 1024.
431 TUint CUsbTransferPublisher::GetBytesTransferred(TLun aLun) const
433 return I64LOW(iArray[aLun] / (TUint64)1000);