examples/Base/FileServer/DriveInfo/DriveInfo.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� 
00028 */
00029 
00030 
00031 
00032 #include <f32file.h>
00033 #include "CommonFramework.h"
00034         
00035 
00036 LOCAL_D RFs fsSession;
00037 
00038 // example functions
00039 void PrintDriveVolInfoL();
00040 
00041 // utility functions
00042 void FormatDriveInfo(TDes& aBuffer, const TDriveInfo aDriveInfo);
00043 void FormatVolumeInfo(TDes& aBuffer, const TVolumeInfo aVolumeInfo);
00044 
00045 void WaitForKey()
00046         {
00047         _LIT(KMessage,"Press any key to continue\n\n");
00048         console->Printf(KMessage);
00049         console->Getch();
00050         }
00051 
00052 // do the example
00053 LOCAL_C void doExampleL()
00054     {
00055         // Connect to file server
00056         User::LeaveIfError(fsSession.Connect()); // Start session
00057         PrintDriveVolInfoL();
00058         fsSession.Close(); // close file server session
00059         }
00060 
00061 void PrintDriveVolInfoL()
00062         {
00063         _LIT(KMessage,"PrintDriveVolInfoL()\n");
00064         _LIT(KValidDriveMsg,"\nValid drives as characters (and as numbers) are:\n");
00065         _LIT(KDriveChar,"%c");
00066         _LIT(KDriveNum,"(%d) ");
00067         _LIT(KNewLine,"\n");
00068         _LIT(KAvailDriveMsg,"\nUsing DriveList(), available drives are: ");
00069         _LIT(KDriveAtts,"%c: %02x ");
00070         _LIT(KDriveInfo,"\nDrive information for %c: drive is:\n%S");
00071         _LIT(KVolInfo,"\nVolume information for %c: is:\n%S");
00072 
00073         console->Printf(KMessage);
00074 
00075         // Print the valid drives as characters and as numbers. 
00076         // Then print the drive list (list of available drives), and
00077         // information about each drive in the list using Drive(). 
00078         // Finally, print volume information.
00079 
00080         console->Printf(KValidDriveMsg);
00081 
00082         TInt driveNumber=EDriveA; 
00083         TChar driveLetter; 
00084         for (; driveNumber<=EDriveZ; driveNumber++)
00085                 {
00086                 if (fsSession.IsValidDrive(driveNumber))
00087                         {
00088                         fsSession.DriveToChar(driveNumber,driveLetter);
00089                         console->Printf(KDriveChar,TUint(driveLetter));
00090                         fsSession.CharToDrive(driveLetter, driveNumber);
00091                         console->Printf(KDriveNum,driveNumber);
00092                         }
00093                 }
00094         console->Printf(KNewLine);
00095 
00096         TDriveList drivelist; 
00097         User::LeaveIfError(fsSession.DriveList(drivelist));
00098         // A TDriveList (the list of available drives), is an array of 
00099         // 26 bytes. Each byte with a non zero value signifies that the 
00100         // corresponding drive is available.
00101 
00102         console->Printf(KAvailDriveMsg);
00103         for (driveNumber=EDriveA; driveNumber<=EDriveZ;driveNumber++)
00104                 {
00105                 if (drivelist[driveNumber]) // if drive-list entry non-zero, drive is available
00106                         {
00107                         TInt err = fsSession.DriveToChar(driveNumber,driveLetter);
00108                         if(err == KErrNone)
00109                                 {
00110                                 // The following line prints the drive letter followed by the hex value 
00111                                 // of the integer indicating that drive's attributes 
00112                                 console->Printf(KDriveAtts,TUint(driveLetter), drivelist[driveNumber]);
00113                                 }
00114                         else if(err == KErrArgument)
00115                                 {
00116                                 _LIT(KTextInvalidDriveNumber, "Drive Number %d is invalid\n");
00117                                 console->Printf(KTextInvalidDriveNumber, driveNumber);
00118                                 }
00119                         else
00120                                 {
00121                                 _LIT(KTextErrCode, "DriveToChar() returned %d error code");
00122                                 console->Printf(KTextErrCode, err);
00123                                 }
00124                         }
00125                 }
00126         console->Printf(KNewLine);
00127 
00128         // Print information about available drives 
00129 
00130         TBuf<200> buffer;
00131         TDriveInfo driveInfo; 
00132         for (driveNumber=EDriveA; driveNumber<=EDriveZ; driveNumber++)
00133                 {
00134                 fsSession.Drive(driveInfo,driveNumber);
00135                 if (TInt(driveInfo.iDriveAtt)==KDriveAbsent)
00136                         // test whether drive is available. If not, skip to next drive
00137                         continue;
00138                 FormatDriveInfo(buffer,driveInfo);
00139                 TInt errNum = fsSession.DriveToChar(driveNumber,driveLetter);
00140                 if(errNum == KErrNone)
00141                         {
00142                         console->Printf(KDriveInfo,TUint(driveLetter),&buffer);
00143                         buffer.Zero();
00144                         WaitForKey();
00145                         }
00146                 }
00147 
00148         // Print volume information for all available drives. TVolumeInfo
00149         // provides drive information, and additional information about
00150         // the volume. Just print out the volume information.
00151  
00152         TVolumeInfo volumeInfo;
00153         for (driveNumber=EDriveA; driveNumber<=EDriveZ; driveNumber++)
00154                 {
00155                 TInt err=fsSession.Volume(volumeInfo,driveNumber);
00156                 if (err!=KErrNotReady) 
00157                         // Volume() returns KErrNotReady if no volume present.
00158                         // In this case, check next drive number
00159                         {
00160                         buffer.Zero();
00161                         FormatVolumeInfo(buffer,volumeInfo);
00162                         TInt errNum = fsSession.DriveToChar(driveNumber,driveLetter);
00163                         if(errNum == KErrNone)
00164                                 {
00165                                 console->Printf(KVolInfo,
00166                                 (TUint)driveLetter,&buffer);
00167                                 WaitForKey();
00168                                 }                       
00169                         }
00170                 }
00171         }
00172 
00173 void FormatDriveInfo(TDes& aBuffer, const TDriveInfo aDriveInfo)
00174         {
00175         // Append battery, media and drive information to aBuffer
00176         // Define descriptor constants using the _LIT macro 
00177         _LIT(KFormatString,"iType=%02x,iBattery=%02x,iDriveAtt=%02x,iMediaAtt=%02x\n");
00178         _LIT(KBatLow,"Battery low\n");
00179         _LIT(KBatGood,"Battery good\n");
00180         _LIT(KBatNotSupported,"Battery not supported\n");
00181         _LIT(KNotPresent,"No media present\n");
00182         _LIT(KFloppy,"Media is floppy disk\n");
00183         _LIT(KHard,"Media is hard disk\n");
00184         _LIT(KCDROM,"Media is CD-ROM\n");
00185         _LIT(KRam,"Media is RAM\n");
00186         _LIT(KFlash,"Media is flash\n");
00187         _LIT(KRom,"Media is ROM\n");
00188         _LIT(KRemote,"Media is remote\n");
00189         _LIT(KNANDFlash,"Media is NAND flash\n");
00190         _LIT(KUnknown,"Media unknown\n");
00191         _LIT(KDriveAtts,"Drive attributes:");
00192         _LIT(KLocal," local");
00193         _LIT(KROMDrive," ROM");
00194         _LIT(KRedirected," redirected");
00195         _LIT(KSubstituted," substituted");
00196         _LIT(KInternal," internal");
00197         _LIT(KRemovable," removable");
00198         _LIT(KMediaAtts,"\nMedia attributes:");
00199         _LIT(KDynamic," dynamic");
00200         _LIT(KDual," dual-density");
00201         _LIT(KFormattable," formattable");
00202         _LIT(KLockable," lockable");
00203         _LIT(KLocked," locked");
00204         _LIT(KHasPassword," has password");
00205         _LIT(KWriteProtected," write-protected");
00206         _LIT(KNewLine,"\n");
00207 
00208         aBuffer.AppendFormat(KFormatString, TInt(aDriveInfo.iType), 
00209                 TInt(aDriveInfo.iBattery), TInt(aDriveInfo.iDriveAtt), TInt(aDriveInfo.iMediaAtt));
00210 
00211         switch (aDriveInfo.iBattery)
00212                 {
00213                 case EBatLow:
00214                         aBuffer.Append(KBatLow);
00215                         break;
00216                 case EBatGood:
00217                         aBuffer.Append(KBatGood);
00218                         break;
00219                 default:
00220                         aBuffer.Append(KBatNotSupported);
00221                 }
00222 
00223         switch (aDriveInfo.iType)
00224                         {
00225                 case EMediaNotPresent:
00226                         aBuffer.Append(KNotPresent);                    
00227                         break;
00228                 case EMediaFloppy:
00229                         aBuffer.Append(KFloppy);
00230                         break;
00231                 case EMediaHardDisk:    
00232                         aBuffer.Append(KHard);
00233                         break;
00234                 case EMediaCdRom:
00235                         aBuffer.Append(KCDROM);
00236                         break;
00237                 case EMediaRam:
00238                         aBuffer.Append(KRam);
00239                         break;
00240                 case EMediaFlash:
00241                         aBuffer.Append(KFlash);
00242                         break;
00243                 case EMediaRom:
00244                         aBuffer.Append(KRom);
00245                         break;
00246                 case EMediaRemote:
00247                         aBuffer.Append(KRemote);
00248                         break;
00249                 case EMediaNANDFlash:
00250             aBuffer.Append(KNANDFlash);
00251                         break;
00252                 default: 
00253                         aBuffer.Append(KUnknown);
00254 
00255                 }
00256                 aBuffer.Append(KDriveAtts);
00257                 if (aDriveInfo.iDriveAtt & KDriveAttLocal)
00258                         aBuffer.Append(KLocal);
00259                 if (aDriveInfo.iDriveAtt & KDriveAttRom)
00260                         aBuffer.Append(KROMDrive);
00261                 if (aDriveInfo.iDriveAtt & KDriveAttRedirected)
00262                         aBuffer.Append(KRedirected);
00263                 if (aDriveInfo.iDriveAtt & KDriveAttSubsted)
00264                         aBuffer.Append(KSubstituted);
00265                 if (aDriveInfo.iDriveAtt & KDriveAttInternal)
00266                         aBuffer.Append(KInternal);
00267                 if (aDriveInfo.iDriveAtt & KDriveAttRemovable)
00268                         aBuffer.Append(KRemovable);
00269                 aBuffer.Append(KMediaAtts);
00270                 if (aDriveInfo.iMediaAtt & KMediaAttVariableSize)
00271                         aBuffer.Append(KDynamic);
00272                 if (aDriveInfo.iMediaAtt & KMediaAttDualDensity)
00273                         aBuffer.Append(KDual);
00274                 if (aDriveInfo.iMediaAtt & KMediaAttFormattable)
00275                         aBuffer.Append(KFormattable);
00276                 if (aDriveInfo.iMediaAtt & KMediaAttWriteProtected)
00277                         aBuffer.Append(KWriteProtected);
00278                 if (aDriveInfo.iMediaAtt & KMediaAttLockable)
00279                         aBuffer.Append(KLockable);
00280                 if (aDriveInfo.iMediaAtt & KMediaAttLocked)
00281                         aBuffer.Append(KLocked);
00282                 if (aDriveInfo.iMediaAtt & KMediaAttHasPassword)
00283                         aBuffer.Append(KHasPassword);
00284                 aBuffer.Append(KNewLine);
00285         }
00286 
00287 void FormatVolumeInfo(TDes& aBuffer, const TVolumeInfo aVolumeInfo)
00288         {
00289         // Append volume information to aBuffer
00290         _LIT(KUID,"Unique ID: %08x\n");
00291         _LIT(KSize,"Size: %Ld bytes\n");
00292         _LIT(KFree,"Free space: %Ld bytes\n");
00293         _LIT(KVolName,"Volume name: %S\n");
00294         aBuffer.AppendFormat(KUID,aVolumeInfo.iUniqueID);
00295         aBuffer.AppendFormat(KSize,aVolumeInfo.iSize);
00296         aBuffer.AppendFormat(KFree,aVolumeInfo.iFree);
00297         aBuffer.AppendFormat(KVolName,&aVolumeInfo.iName);
00298         }

Generated by  doxygen 1.6.2