First public contribution.
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 // USB Mass Storage Application - also used as an improvised boot loader mechanism
27 #include "rusbotgsession.h"
30 #include "mdrivedisplay.h"
33 // Display positions and test constants
34 // Number of attached devices
35 static const TInt KRow_DevicesNumber = 13;
36 _LIT(KMsg_DevicesAttached, "USB Devices Attached = %d");
39 static const TInt KStartRow_DeviceMap = KRow_DevicesNumber + 2;
40 static const TInt KMaxRows_DeviceMap = 4;
41 _LIT(KMsg_DeviceMap_DriveList, "%d: "); // [drive index]
42 _LIT(KMsg_DeviceMap_DriveLunEntry, "%c "); // [drive letter]
46 static const TInt KStartRow_DriveMap = KStartRow_DeviceMap + KMaxRows_DeviceMap;
47 static const TInt KMaxRows_DriveMap = 4;
48 _LIT(KMsg_DriveMap_EntryLetter, "%c token = %d"); // [drive letter] [token]
49 _LIT(KDbgMsg_DriveMap_EntryLetter, "*** %c token = %d"); // [drive letter] [token]
52 static const TInt KStartRow_UpTime = 28;
53 _LIT(KMsg_UpTime, "up time : %dh:%dm:%ds "); // use trailing space to overwrite any leftover chars in line
55 static const TInt KStartRow_MemoryFree = 29;
56 _LIT(KMsg_MemoryFree, "mem (bytes) : 0x%X");
59 static const TInt KStartRow_UserKeys = 25;
60 _LIT(KMsgUser1Keys, "[Esc]=Quit [A-Z]=DriveInfo");
61 _LIT(KMsgUser2Keys, "[F5]=Hub update");
64 // Scroll Window status
65 _LIT(KScrollWindowStatus, "Page %d of %d");
68 static const TInt KStartRow_AvailableDrives = 1;
69 _LIT(KAvailDriveMsg, "Drives: ");
71 _LIT(KDriveAtts,"DriveList %c: %02x ");
74 static const TInt KStartRow_MsgWindow = 3;
75 static const TInt KStartRow_DriveInfo = KStartRow_MsgWindow;
77 // ****************************************************************************
80 CScrollWindow* CScrollWindow::NewL(CConsoleBase& aConsole)
82 CScrollWindow* r = new (ELeave) CScrollWindow(aConsole);
83 CleanupStack::PushL(r);
90 void CScrollWindow::ConstructL()
96 CScrollWindow::CScrollWindow(CConsoleBase& aConsole)
101 CScrollWindow::~CScrollWindow()
106 void CScrollWindow::Reset()
113 void CScrollWindow::AppendL(const TDesC& aLine)
116 iLineArray.AppendL(iTmpLine);
117 TInt last = iLineArray.Count() - 1;
118 iLineArray[last].Copy(aLine);
122 TLine* CScrollWindow::NewLineL()
125 iLineArray.AppendL(iTmpLine);
126 TInt last = iLineArray.Count() - 1;
127 return &iLineArray[last];
131 void CScrollWindow::Update()
133 TInt line = iPage * KPageLength;
135 TInt row = KStartRow_DriveInfo;
138 iConsole.SetPos(0, row + line%KPageLength);
139 if (line < iLineArray.Count())
141 iConsole.Printf(iLineArray[line]);
143 iConsole.ClearToEndOfLine();
146 while (((line-1)%KPageLength) != (KPageLength - 1));
147 iConsole.SetPos(0, KStartRow_DriveInfo + KPageLength);
148 iConsole.Printf(KScrollWindowStatus, iPage + 1, iLineArray.Count()/KPageLength + 1);
151 void CScrollWindow::PageInc()
153 TInt lastPage = iLineArray.Count()/KPageLength;
154 if (iPage == lastPage)
165 void CScrollWindow::PageDec()
169 TInt lastPage = iLineArray.Count()/KPageLength;
180 CDisplay* CDisplay::NewLC(RFs& aFs, CConsoleBase& aConsole)
182 CDisplay* r = new (ELeave) CDisplay(aFs, aConsole);
183 CleanupStack::PushL(r);
189 void CDisplay::ConstructL()
191 iScrollWindow = CScrollWindow::NewL(iConsole);
195 CDisplay::CDisplay(RFs& aFs, CConsoleBase& aConsole)
199 iConsole.ClearScreen();
203 CDisplay::~CDisplay()
205 delete iScrollWindow;
209 void CDisplay::Menu()
211 iConsole.SetPos(0, KStartRow_UserKeys);
212 iConsole.Printf(KMsgUser1Keys);
213 iConsole.SetPos(0, KStartRow_UserKeys + 1);
214 iConsole.Printf(KMsgUser2Keys);
215 iCursorPos = iConsole.CursorPos();
219 void CDisplay::DriveListL() const
221 TDriveList drivelist;
222 TRAPD(err, iFs.DriveList(drivelist));
227 // A TDriveList (the list of available drives), is an array of
228 // 26 bytes. Each byte with a non zero value signifies that the
229 // corresponding drive is available.
230 TBuf<KDisplayWidth> iLineBuffer;
231 iLineBuffer = KAvailDriveMsg;
234 for (TInt driveNumber = EDriveA; driveNumber <= EDriveZ;driveNumber++)
236 if (drivelist[driveNumber]) // if drive-list entry non-zero, drive is available
239 if (iLineBuffer.Length() == iLineBuffer.MaxLength())
241 iLineBuffer[iLineBuffer.MaxLength() - 1] = '>';
245 User::LeaveIfError(iFs.DriveToChar(driveNumber,driveLetter));
247 // The following line prints the drive letter followed by the hex value
248 // of the integer indicating that drive's attributes
249 RDebug::Print(KDriveAtts,TUint(driveLetter), drivelist[driveNumber]);
250 iLineBuffer.Append(driveLetter);
255 iConsole.SetPos(0, KStartRow_AvailableDrives);
256 iConsole.Printf(iLineBuffer);
257 iConsole.ClearToEndOfLine();
262 void CDisplay::DevicesNumber(TInt aDevicesNumber) const
264 iConsole.SetPos(0, KRow_DevicesNumber);
265 iConsole.Printf(KMsg_DevicesAttached, aDevicesNumber);
266 iConsole.ClearToEndOfLine();
270 void CDisplay::DriveMapL(const TDriveMap& aDriveMap) const
276 // Output to debug port
277 for (; i < aDriveMap.Count(); i++)
279 TToken token = aDriveMap[i];
282 User::LeaveIfError(iFs.DriveToChar(i, letter));
283 RDebug::Print(KDbgMsg_DriveMap_EntryLetter, TUint(letter), token);
288 TInt row = KStartRow_DriveMap;
289 for (i = (aDriveMap.Count() -1); i >= 0 && row < (KStartRow_DriveMap + KMaxRows_DriveMap); i--)
291 TToken token = aDriveMap[i];
294 User::LeaveIfError(iFs.DriveToChar(i, letter));
295 iConsole.SetPos(0, row);
296 iConsole.Printf(KMsg_DriveMap_EntryLetter, TUint(letter), token);
297 iConsole.ClearToEndOfLine();
302 for (; row < KStartRow_DriveMap + KMaxRows_DriveMap; row++)
304 iConsole.SetPos(0, row);
305 iConsole.ClearToEndOfLine();
310 void CDisplay::DeviceMapL(TInt aRow, TInt deviceIndex, const TDeviceMap& aDeviceMap) const
315 // Output to debug port
316 RDebug::Printf("*** deviceIndex = %d", deviceIndex);
317 for (TInt lunIndex = 0; lunIndex < 16; lunIndex++)
319 drive = aDeviceMap[lunIndex];
324 User::LeaveIfError(iFs.DriveToChar(drive, letter));
325 RDebug::Printf("*** drive=%d %c", drive, TUint(letter));
329 if (aRow >= KMaxRows_DeviceMap)
333 RDebug::Printf("-----> Device MAP %x", deviceIndex);
334 TInt row = KStartRow_DeviceMap + aRow;
335 iConsole.SetPos(0, row);
336 iConsole.Printf(KMsg_DeviceMap_DriveList, deviceIndex);
338 for (TInt lunIndex = 0; lunIndex < 16; lunIndex++)
340 drive = aDeviceMap[lunIndex];
345 User::LeaveIfError(iFs.DriveToChar(drive, letter));
346 iConsole.Printf(KMsg_DeviceMap_DriveLunEntry, TUint(letter));
347 iConsole.ClearToEndOfLine();
352 void CDisplay::DeviceMapClear(TInt aRow) const
354 TInt row = KStartRow_DeviceMap;
356 if (aRow > KMaxRows_DeviceMap)
359 for (row = KStartRow_DeviceMap + aRow; row < KStartRow_DeviceMap + KMaxRows_DeviceMap; row++)
361 iConsole.SetPos(0, row);
362 iConsole.ClearToEndOfLine();
367 void CDisplay::GetDriveInfoL(TChar aChar)
369 iScrollWindow->Reset();
371 TDriveInfo driveInfo;
374 User::LeaveIfError(iFs.CharToDrive(aChar, driveNumber));
377 line = iScrollWindow->NewLineL();
378 _LIT(KDrive,"Drive=%d %C");
379 line->Format(KDrive, driveNumber, TInt(aChar.GetUpperCase()));
381 iFs.Drive(driveInfo, driveNumber);
382 if (driveInfo.iDriveAtt == KDriveAbsent)
384 _LIT(KTxt_MappingDriveError, "Drive absent !");
385 iScrollWindow->AppendL(KTxt_MappingDriveError);
389 FormatDriveInfoL(driveInfo);
391 TVolumeInfo volumeInfo;
393 TInt err = iFs.Volume(volumeInfo, driveNumber);
394 if (err != KErrNotReady)
395 // Volume() returns KErrNotReady if no volume present.
396 // In this case, check next drive number
398 FormatVolumeInfoL(volumeInfo);
403 void CDisplay::DriveInfo()
405 iScrollWindow->Update();
409 void CDisplay::FormatDriveInfoL(const TDriveInfo& aDriveInfo)
411 // Append battery, media and drive information to aBuffer
412 // Define descriptor constants using the _LIT macro
413 _LIT(KDriveInfo1, "iType=%02x %02x iDriveAtt=%04x");
414 _LIT(KDriveInfo2, "iMediaAtt=%02x");
415 _LIT(KConnectionBusInternal,"Connection Bus Internal");
416 _LIT(KConnectionBusUsb,"Connection Bus USB");
417 _LIT(KConnectionBusUnknown,"Connection Bus Unknown");
418 _LIT(KNotPresent,"No media present");
419 _LIT(KFloppy,"Media is floppy disk");
420 _LIT(KHard,"Media is hard disk");
421 _LIT(KCDROM,"Media is CD-ROM");
422 _LIT(KRam,"Media is RAM");
423 _LIT(KFlash,"Media is flash");
424 _LIT(KRom,"Media is ROM");
425 _LIT(KRemote,"Media is remote");
426 _LIT(KExternal,"Media is external");
427 _LIT(KNANDFlash,"Media is NAND flash");
428 _LIT(KUnknown,"Media unknown");
429 _LIT(KDriveAtts,"Drive attributes:");
430 _LIT(KLocal," local");
431 _LIT(KROMDrive," ROM");
432 _LIT(KRedirected," redirected");
433 _LIT(KSubstituted," substituted");
434 _LIT(KInternal," internal");
435 _LIT(KRemovable," removable");
436 _LIT(KMediaAtts,"Media attributes:");
437 _LIT(KDynamic," dynamic");
438 _LIT(KDual," dual-density");
439 _LIT(KFormattable," formattable");
440 _LIT(KLockable," lockable");
441 _LIT(KLocked," locked");
442 _LIT(KHasPassword," has password");
443 _LIT(KWriteProtected," write-protected");
446 line = iScrollWindow->NewLineL();
447 line->Format(KDriveInfo1, TInt(aDriveInfo.iType), TInt(aDriveInfo.iConnectionBusType), TInt(aDriveInfo.iDriveAtt));
449 line = iScrollWindow->NewLineL();
450 line->Format(KDriveInfo2, TInt(aDriveInfo.iMediaAtt));
452 line = iScrollWindow->NewLineL();
453 switch (aDriveInfo.iConnectionBusType)
455 case EConnectionBusInternal:
456 line->Append(KConnectionBusInternal);
458 case EConnectionBusUsb:
459 line->Append(KConnectionBusUsb);
462 line->Append(KConnectionBusUnknown);
465 line = iScrollWindow->NewLineL();
466 switch (aDriveInfo.iType)
468 case EMediaNotPresent:
469 line->Append(KNotPresent);
472 line->Append(KFloppy);
478 line->Append(KCDROM);
484 line->Append(KFlash);
490 line->Append(KRemote);
492 case EMediaNANDFlash:
493 line->Append(KNANDFlash);
496 line->Append(KUnknown);
500 line = iScrollWindow->NewLineL();
501 line->Append(KDriveAtts);
502 if (aDriveInfo.iDriveAtt & KDriveAttLocal)
504 line = iScrollWindow->NewLineL();
505 line->Append(KLocal);
507 if (aDriveInfo.iDriveAtt & KDriveAttRom)
509 line = iScrollWindow->NewLineL();
510 line->Append(KROMDrive);
512 if (aDriveInfo.iDriveAtt & KDriveAttRedirected)
514 line = iScrollWindow->NewLineL();
515 line->Append(KRedirected);
517 if (aDriveInfo.iDriveAtt & KDriveAttSubsted)
519 line = iScrollWindow->NewLineL();
520 line->Append(KSubstituted);
522 if (aDriveInfo.iDriveAtt & KDriveAttInternal)
524 line = iScrollWindow->NewLineL();
525 line->Append(KInternal);
527 if (aDriveInfo.iDriveAtt & KDriveAttRemovable)
529 line = iScrollWindow->NewLineL();
530 line->Append(KRemovable);
532 if (aDriveInfo.iDriveAtt & KDriveAttExternal)
534 line = iScrollWindow->NewLineL();
535 line->Append(KExternal);
539 line = iScrollWindow->NewLineL();
540 line->Append(KMediaAtts);
541 if (aDriveInfo.iMediaAtt & KMediaAttVariableSize)
543 line = iScrollWindow->NewLineL();
544 line->Append(KDynamic);
546 if (aDriveInfo.iMediaAtt & KMediaAttDualDensity)
548 line = iScrollWindow->NewLineL();
551 if (aDriveInfo.iMediaAtt & KMediaAttFormattable)
553 line = iScrollWindow->NewLineL();
554 line->Append(KFormattable);
556 if (aDriveInfo.iMediaAtt & KMediaAttWriteProtected)
558 line = iScrollWindow->NewLineL();
559 line->Append(KWriteProtected);
561 if (aDriveInfo.iMediaAtt & KMediaAttLockable)
563 line = iScrollWindow->NewLineL();
564 line->Append(KLockable);
567 if (aDriveInfo.iMediaAtt & KMediaAttLocked)
569 line = iScrollWindow->NewLineL();
570 line->Append(KLocked);
572 if (aDriveInfo.iMediaAtt & KMediaAttHasPassword)
574 line = iScrollWindow->NewLineL();
575 line->Append(KHasPassword);
579 void CDisplay::FormatVolumeInfoL(const TVolumeInfo& aVolumeInfo)
581 // Append volume information to line
582 _LIT(KUID, "Unique ID: 0x%08X");
583 _LIT(KSize, "Size: 0x%LX bytes");
584 _LIT(KFree, "Free space: 0x%LX bytes");
585 _LIT(KVolName, "Volume name: %S");
587 line = iScrollWindow->NewLineL();
588 line->Format(KUID, aVolumeInfo.iUniqueID);
589 line = iScrollWindow->NewLineL();
590 line->Format(KSize, aVolumeInfo.iSize);
591 line = iScrollWindow->NewLineL();
592 line->Format(KFree, aVolumeInfo.iFree);
593 line = iScrollWindow->NewLineL();
594 line->Format(KVolName, &aVolumeInfo.iName);
599 void CDisplay::UpTime(TUint aUpTime) const
601 TUint totalMins = aUpTime/60;
602 TUint totalHrs = totalMins/60;
603 iConsole.SetPos(0, KStartRow_UpTime);
604 iConsole.Printf(KMsg_UpTime, totalHrs, totalMins%60, aUpTime%60);
608 void CDisplay::MemoryFree(TInt aBytes) const
610 iConsole.SetPos(0, KStartRow_MemoryFree);
611 iConsole.Printf(KMsg_MemoryFree, aBytes);
616 //////////////////////////////////////////////////////////////////////////////
618 // CMessageKeyProcessor
620 //////////////////////////////////////////////////////////////////////////////
621 CMessageKeyProcessor::CMessageKeyProcessor(CDisplay& aDisplay, RUsbOtgSession& aUsbOtgSession)
622 : CActive(CActive::EPriorityUserInput),
624 iUsbOtgSession(aUsbOtgSession)
628 CMessageKeyProcessor* CMessageKeyProcessor::NewLC(CDisplay& aDisplay, RUsbOtgSession& aUsbOtgSession)
630 CMessageKeyProcessor* self=new (ELeave) CMessageKeyProcessor(aDisplay, aUsbOtgSession);
631 CleanupStack::PushL(self);
637 void CMessageKeyProcessor::ConstructL()
639 // Add to active scheduler
640 CActiveScheduler::Add(this);
645 CMessageKeyProcessor::~CMessageKeyProcessor()
647 // Make sure we're cancelled
651 void CMessageKeyProcessor::DoCancel()
653 iDisplay.ReadCancel();
656 void CMessageKeyProcessor::RunL()
658 // Handle completed request
659 ProcessKeyPressL(iDisplay.KeyCode());
662 void CMessageKeyProcessor::RequestCharacter()
664 // A request is issued to the CConsoleBase to accept a
665 // character from the keyboard.
666 iDisplay.Read(iStatus);
670 void CMessageKeyProcessor::ProcessKeyPressL(TKeyCode aKeyCode)
672 TBool done = HandleKeyL(aKeyCode);
676 CActiveScheduler::Stop();
684 TBool CMessageKeyProcessor::HandleKeyL(TKeyCode aKeyCode)
687 if (TChar(aKeyCode).IsAlpha())
689 iDisplay.GetDriveInfoL(aKeyCode);
690 iDisplay.DriveInfo();
699 iUsbOtgSession.DeviceInserted();
700 iDisplay.DriveListL();
707 iDisplay.DriveInfo();
712 iDisplay.DriveInfo();