Update contrib.
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".
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.
31 #include "massstoragedebug.h"
32 #include "drivepublisher.h"
33 #include "drivemanager.h"
37 // Use Lookup table to translate from the internal pair of state variables
38 // to the externally published drive state code.
40 LOCAL_D const TUint8 table[][5] =
42 //TMountState=EDisconnected
43 {EUsbMsDriveState_Disconnected,
44 EUsbMsDriveState_Disconnected,
45 EUsbMsDriveState_Disconnected,
46 EUsbMsDriveState_Disconnected,
47 EUsbMsDriveState_Disconnected},
48 //TMountState=EConnecting
49 {EUsbMsDriveState_Connecting,
50 EUsbMsDriveState_Connecting,
51 EUsbMsDriveState_Connecting,
52 EUsbMsDriveState_Connecting,
53 EUsbMsDriveState_Connecting},
54 //TMountState=EConnected
55 //EIdle,EActive,ELocked,EMediaNotPresent,EErrDisMounted
56 {EUsbMsDriveState_Connected,
57 EUsbMsDriveState_Active,
58 EUsbMsDriveState_Locked,
59 EUsbMsDriveState_MediaNotPresent,
60 EUsbMsDriveState_Removed},
61 //TMountState=EDisconnecting
62 {EUsbMsDriveState_Disconnecting,
63 EUsbMsDriveState_Disconnecting,
64 EUsbMsDriveState_Disconnecting,
65 EUsbMsDriveState_Disconnecting,
66 EUsbMsDriveState_Disconnecting}
70 //----------------------------------------------------------------------------
74 RDriveMediaErrorPublisher::RDriveMediaErrorPublisher()
76 __FNLOG("RDriveMediaErrorPublisher::RDriveMediaErrorPublisher()");
78 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
79 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
81 TInt result = RProperty::Define(EUsbMsDriveState_MediaError, RProperty::EInt,
82 KMassStorageReadPolicy, KMassStorageWritePolicy);
84 __ASSERT_DEBUG(result == KErrAlreadyExists || result == KErrNone, User::Invariant());
86 result = iMediaErrorProperty.Attach(KUsbMsDriveState_Category, EUsbMsDriveState_MediaError);
87 __ASSERT_DEBUG(result == KErrNone, User::Invariant());
91 RDriveMediaErrorPublisher::~RDriveMediaErrorPublisher()
93 __FNLOG("RDriveStatePublisher::~RDriveStatePublisher()");
95 iMediaErrorProperty.Close();
96 RProperty::Delete(KUsbMsDriveState_Category, EUsbMsDriveState_MediaError);
102 Publishes the Media Error property event
104 @param aError ETrue if drive media has an error else EFalse for no error
106 void RDriveMediaErrorPublisher::PublishError(TBool aError)
108 __PRINT1(_L("<< RDriveMediaErrorPublisher::PublishError %x"), aError);
111 iMediaErrorProperty.Get(oldValue);
113 if (oldValue != aError)
115 User::LeaveIfError(iMediaErrorProperty.Set(aError));
119 //----------------------------------------------------------------------------
126 RDriveStateChangedPublisher::RDriveStateChangedPublisher(TRefMsDriveList aDrives,
127 TRefDriveMap aDriveMap)
132 __FNLOG("RDriveStateChangedPublisher::RDriveStateChangedPublisher()");
134 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
135 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
137 TInt result = RProperty::Define(KUsbMsDriveState_Category,
138 EUsbMsDriveState_DriveStatus, RProperty::EByteArray,
139 KMassStorageReadPolicy, KMassStorageWritePolicy,
141 __ASSERT_DEBUG(result == KErrAlreadyExists || result == KErrNone, User::Invariant());
142 result = result; // remove urel warning
146 RDriveStateChangedPublisher::~RDriveStateChangedPublisher()
148 __FNLOG("RDriveStateChangedPublisher::~RDriveStateChangedPublisher()");
150 RProperty::Delete(KUsbMsDriveState_Category, EUsbMsDriveState_DriveStatus);
157 Sends a property event on behalf of CMassStorageDrive, with the mountstate and drivestate
158 values encoded into one 32-bit word.
160 void RDriveStateChangedPublisher::DriveStateChanged()
162 __FNLOG("RDriveStateChangedPublisher::DriveStateChanged");
165 TUsbMsDrivesStatus allDrivesStatus;
166 for(TUint8 i=0; i<KUsbMsMaxDrives && iDrives[i]; i++)
168 allDrivesStatus.Append(iDriveMap[i]);
170 CMassStorageDrive::TMountState ms = iDrives[i]->MountState();
171 CMassStorageDrive::TDriveState ds = iDrives[i]->DriveState();
172 TInt driveStatus = EUsbMsDriveState_Error;
173 if((TUint8)ds < sizeof(table[0]) && (TUint8)ms < sizeof(table)/sizeof(table[0]))
175 driveStatus = table[ms][ds];
176 __PRINT3(_L("ms=%d ds=%d %d"), ms, ds, driveStatus);
178 allDrivesStatus.Append(driveStatus);
181 __PRINT1(_L("Publishing EUsbMsDriveState_DriveStatus for %d drives\n"),
182 allDrivesStatus.Length()/2);
184 if(KErrNone != RProperty::Set(KUsbMsDriveState_Category,
185 EUsbMsDriveState_DriveStatus,
188 __ASSERT_DEBUG(EFalse,User::Invariant());
193 //----------------------------------------------------------------------------
194 #ifndef USB_TRANSFER_PUBLISHER
196 Private default constructor to ensure that NewL is used
201 CDriveTransferPublisher::CDriveTransferPublisher(
202 TUsbMsDriveState_Subkey aSubKey,
203 TRefMsDriveList aDrives)
211 void CDriveTransferPublisher::ConstructL()
213 __FNLOG("CDriveTransferPublisher::ConstructL");
215 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
216 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
218 TInt r = RProperty::Define(iSubKey, RProperty::EByteArray,
219 KMassStorageReadPolicy, KMassStorageWritePolicy,
220 KUsbMsMaxDrives*sizeof(TInt));
222 if (r != KErrAlreadyExists)
224 User::LeaveIfError(r);
227 User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, iSubKey));
229 // Create the EDataTransferred timer
230 iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
231 iTimerRunning = EFalse;
238 CDriveTransferPublisher::~CDriveTransferPublisher()
240 __FNLOG("CDriveTransferPublisher::~CDriveTransferPublisher");
250 RProperty::Delete(KUsbMsDriveState_Category, iSubKey);
255 A static wrapper for the DoPublishDataTransferredEvent member function
256 for use as a timer callback function.
258 @param obj 'this' pointer
259 @return not used in CPeriodic callback (see TCallback)
261 TInt CDriveTransferPublisher::PublishDataTransferredEvent(TAny* obj)
263 __FNLOG("CDrivePublisher::PublishDataTransferredEvent");
264 static_cast<CDriveTransferPublisher*>(obj)->DoPublishDataTransferredEvent();
270 Update the data transferred properties if the counts have changed since
273 void CDriveTransferPublisher::DoPublishDataTransferredEvent()
275 if (PublishDataTransferred())
277 // some data has been transfered so reset the counter
278 iTimerCancelCnt = ETimerCancelDelay;
281 // Update the cancel count if no data was transferred the last
282 // (few) times this has been called
283 if (--iTimerCancelCnt == 0)
286 iTimerCancelCnt = ETimerCancelDelay;
292 Update the data transferred properties if the counts have changed since
295 TBool CDriveTransferPublisher::PublishDataTransferred()
297 __FNLOG("CDriveWriteTransferPublisher::PublishDataTransferred");
299 TUsbMsBytesTransferred bytesTransferred;
300 TBool dataTransferred = EFalse;
302 for (TInt i=0; i < iDrives.Count() && iDrives[i]; i++)
304 bytesTransferred[i] = GetBytesTransferred(i);
307 // Update the properties only if they have changed
308 // (or if there's an error reading the old value.)
309 // Possible optimisation: keep a copy of the value
310 // as a member variable so we don't need the Get.
311 TUsbMsBytesTransferred oldValue;
313 if ((iProperty.Get(oldValue) != KErrNone) || (oldValue != bytesTransferred))
316 for (TInt j=0; j < iDrives.Count() && iDrives[j]; j++)
318 if(oldValue[j] != bytesTransferred[j])
320 __PRINT3(_L("CDrivePublisher: KBytes[%d] %d->%d\n"), j, oldValue[j], bytesTransferred[j]);
324 if (KErrNone != iProperty.Set(bytesTransferred))
326 __ASSERT_DEBUG(EFalse, User::Invariant());
328 dataTransferred = ETrue;
331 return dataTransferred;
336 Starts timer to periodically publish results.
337 If the timer is not yet running then start it.
339 void CDriveTransferPublisher::StartTimer()
341 __FNLOG("CDrivePublisher::StartTimer");
345 // EDataTransferred event every second
346 const TTimeIntervalMicroSeconds32 interval = 1 * 1000 * 1000;
347 TCallBack callback(PublishDataTransferredEvent, this);
348 __PRINT(_L("Starting timer"));
349 iTimer->Start(interval, interval, callback);
350 iTimerRunning = ETrue;
356 Ensure that the Timer is stopped
358 void CDriveTransferPublisher::StopTimer()
360 __FNLOG("CDrivePublisher::StopTimer");
364 __PRINT(_L("Stopping timer"));
366 iTimerRunning = EFalse;
371 //----------------------------------------------------------------------------
373 Constructor for Write property
377 CDriveWriteTransferPublisher* CDriveWriteTransferPublisher::NewL(TRefMsDriveList aDrives)
379 __FNLOG("CDriveWriteTransferPublisher::NewL");
381 CDriveWriteTransferPublisher* self = new (ELeave) CDriveWriteTransferPublisher(aDrives);
382 CleanupStack::PushL(self);
394 CDriveWriteTransferPublisher::CDriveWriteTransferPublisher(TRefMsDriveList aDrives)
396 CDriveTransferPublisher(EUsbMsDriveState_KBytesWritten, aDrives)
402 Transfer function for Write property
406 TUint CDriveWriteTransferPublisher::GetBytesTransferred(TUint aLun) const
408 return iDrives[aLun]->KBytesWritten();
412 //----------------------------------------------------------------------------
414 Constructor for Read property
418 CDriveReadTransferPublisher* CDriveReadTransferPublisher::NewL(TRefMsDriveList aDrives)
420 __FNLOG("CDriveWriteTransferPublisher::NewL");
422 CDriveReadTransferPublisher* self = new (ELeave) CDriveReadTransferPublisher(aDrives);
423 CleanupStack::PushL(self);
435 CDriveReadTransferPublisher::CDriveReadTransferPublisher(TRefMsDriveList aDrives)
437 CDriveTransferPublisher(EUsbMsDriveState_KBytesRead, aDrives)
443 Transfer function for Read property
447 TUint CDriveReadTransferPublisher::GetBytesTransferred(TUint aLun) const
449 return iDrives[aLun]->KBytesRead();
453 //----------------------------------------------------------------------------
456 Private default constructor to ensure that NewL is used
461 CUsbTransferPublisher::CUsbTransferPublisher(
462 TUsbMsDriveState_Subkey aSubKey,
463 TRefBytesTransferedList aArray)
471 void CUsbTransferPublisher::ConstructL()
473 __FNLOG("CUsbTransferPublisher::ConstructL");
475 _LIT_SECURITY_POLICY_PASS(KMassStorageReadPolicy);
476 _LIT_SECURITY_POLICY_S0(KMassStorageWritePolicy, KUsbMsDriveState_Category.iUid);
478 TInt r = RProperty::Define(iSubKey, RProperty::EByteArray,
479 KMassStorageReadPolicy, KMassStorageWritePolicy,
480 KUsbMsMaxDrives*sizeof(TInt));
482 if (r != KErrAlreadyExists)
484 User::LeaveIfError(r);
487 // Attach to the properties here. Only do this once, continuously attaching
488 // will currently cause a memory leak
489 User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, iSubKey));
491 // Create the EDataTransferred timer
492 iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
493 iTimerRunning = EFalse;
500 CUsbTransferPublisher::~CUsbTransferPublisher()
502 __FNLOG("CUsbTransferPublisher::~CDriveTransferPublisher");
512 RProperty::Delete(KUsbMsDriveState_Category, iSubKey);
517 A static wrapper for the DoPublishDataTransferredEvent member function
518 for use as a timer callback function.
520 @param obj 'this' pointer
521 @return not used in CPeriodic callback (see TCallback)
523 TInt CUsbTransferPublisher::PublishDataTransferredEvent(TAny* obj)
525 __FNLOG("CUsbTransferPublisher::PublishDataTransferredEvent");
526 static_cast<CUsbTransferPublisher*>(obj)->DoPublishDataTransferredEvent();
532 Update the data transferred properties if the counts have changed since
535 void CUsbTransferPublisher::DoPublishDataTransferredEvent()
537 if (PublishDataTransferred())
539 // some data has been transfered so reset the counter
540 iTimerCancelCnt = ETimerCancelDelay;
543 // Update the cancel count if no data was transferred the last
544 // (few) times this has been called
545 if (--iTimerCancelCnt == 0)
548 iTimerCancelCnt = ETimerCancelDelay;
554 Update the data transferred properties if the counts have changed since
557 TBool CUsbTransferPublisher::PublishDataTransferred()
559 __FNLOG("CUsbWriteTransferPublisher::PublishDataTransferred");
561 TUsbMsBytesTransferred bytesTransferred;
562 TBool dataTransferred = EFalse;
564 for (TInt i = 0; i < iArray.Count(); i++)
566 bytesTransferred[i] = GetBytesTransferred(i);
569 // Update the properties only if they have changed
570 // (or if there's an error reading the old value.)
571 // Possible optimisation: keep a copy of the value
572 // as a member variable so we don't need the Get.
573 TUsbMsBytesTransferred oldValue;
575 if ((iProperty.Get(oldValue) != KErrNone) || (oldValue != bytesTransferred))
578 // trace of the bytes transferred
579 for (TInt j=0; j < iArray.Count(); j++)
581 if(oldValue[j] != bytesTransferred[j])
583 __PRINT3(_L("CDrivePublisher: KBytes[%d] %d->%d\n"), j, oldValue[j], bytesTransferred[j]);
587 if (KErrNone != iProperty.Set(bytesTransferred))
589 __ASSERT_DEBUG(EFalse, User::Invariant());
591 dataTransferred = ETrue;
594 return dataTransferred;
599 Starts timer to periodically publish results.
600 If the timer is not yet running then start it.
602 void CUsbTransferPublisher::StartTimer()
604 __FNLOG("CUsbTransferPublisher::StartTimer");
608 // EDataTransferred event every second
609 const TTimeIntervalMicroSeconds32 interval = 1 * 1000 * 1000;
610 TCallBack callback(PublishDataTransferredEvent, this);
611 __PRINT(_L("Starting timer"));
612 iTimer->Start(interval, interval, callback);
613 iTimerRunning = ETrue;
619 Ensure that the Timer is stopped
621 void CUsbTransferPublisher::StopTimer()
623 __FNLOG("CUsbTransferPublisher::StopTimer");
627 __PRINT(_L("Stopping timer"));
629 iTimerRunning = EFalse;
634 //----------------------------------------------------------------------------
636 Constructor for Write property
640 CUsbWriteTransferPublisher* CUsbWriteTransferPublisher::NewL(TRefBytesTransferedList aArray)
642 __FNLOG("CUsbWriteTransferPublisher::NewL");
644 CUsbWriteTransferPublisher* self = new (ELeave) CUsbWriteTransferPublisher(aArray);
645 CleanupStack::PushL(self);
652 CUsbWriteTransferPublisher::CUsbWriteTransferPublisher(
653 TRefBytesTransferedList aArray)
655 CUsbTransferPublisher(EUsbMsDriveState_KBytesWritten, aArray)
660 //----------------------------------------------------------------------------
662 Constructor for Read property
666 CUsbReadTransferPublisher* CUsbReadTransferPublisher::NewL(TRefBytesTransferedList aArray)
668 __FNLOG("CUsbWriteTransferPublisher::NewL");
670 CUsbReadTransferPublisher* self = new (ELeave) CUsbReadTransferPublisher(aArray);
671 CleanupStack::PushL(self);
678 CUsbReadTransferPublisher::CUsbReadTransferPublisher(
679 TRefBytesTransferedList aArray)
681 CUsbTransferPublisher(EUsbMsDriveState_KBytesRead, aArray)
684 #endif // USB_TRANSFER_PUBLISHER