examples/Base/FeatMngrExample/src/FeatMngrExample.cpp

00001 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "FeatMngrExample.h"
00017 #include <e32base.h>
00018 #include <e32std.h>
00019 #include <e32def.h>
00020 #include <e32cmn.h>
00021 
00022 //String literals
00023 _LIT(KNewline, "\n");
00024 
00025 CFeatMngrExample* CFeatMngrExample::NewL(TInt aPriority)
00026         {
00027         CFeatMngrExample* self=new(ELeave)CFeatMngrExample(aPriority);
00028         CleanupStack::PushL(self);
00029         self->ConstructL();
00030         CleanupStack::Pop(self);
00031         return self;
00032         }
00033 
00034 void CFeatMngrExample::ConstructL()
00035         {
00036         //Create console
00037         _LIT(KTextConsoleTitle, "FeatureManager");
00038         iConsole = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen));
00039         _LIT(KWelcome, "Welcome to the Feature Management Example\n");
00040         iConsole->Printf(KWelcome);
00041         
00042         //Create another process to receive notification. 
00043         _LIT(KProcessName, "FeatureChecker.exe");
00044         User::LeaveIfError(iProcess.Create(KProcessName, KNullDesC));
00045         iProcess.Resume();
00046         
00047         User::LeaveIfError(iFeatControl.Open());
00048         }
00049 
00050 CFeatMngrExample::CFeatMngrExample(TInt aPriority):CActive( aPriority )
00051         {       
00052         CActiveScheduler::Add(this);
00053         }
00054 
00058 CFeatMngrExample::~CFeatMngrExample()
00059         {
00060         Cancel();
00061         delete iConsole;
00062         iFeatControl.Close();
00063         iProcess.Close();
00064         }
00065 
00066 void CFeatMngrExample::RunL()
00067         {
00068         TRAPD(err,ProcessKeyPressL(TChar(iConsole->KeyCode())));
00069         if(err != KErrNone)
00070                 {
00071                 iConsole->Printf(_L("Failed. Error: %d\r\n"),err);
00072                 RequestCharacter();
00073                 }       
00074         }
00075 
00080 void CFeatMngrExample::ProcessKeyPressL(TChar aChar)
00081     {
00082         switch(aChar)
00083                 {
00084                 case '1':
00085                         {
00086                         ListFeaturesL();
00087                         break;
00088                         }
00089                 case '2':
00090                         {
00091                         AddL();
00092                         break;
00093                         }
00094                 case '3':
00095                         {
00096                         UpdateL();
00097                         break;
00098                         }
00099                 case '4':
00100                         {
00101                         EnableFeatureL();
00102                         break;
00103                         }
00104                 case '5':
00105                         {
00106                         DeleteL();
00107                         break;
00108                         }
00109                 case '6':
00110                         {
00111                         CheckFeatureSupportL();
00112                         break;
00113                         }
00114                 default:
00115                         CActiveScheduler::Stop();
00116                         break;
00117                 }
00118     }
00119 
00124 void CFeatMngrExample::AddL()
00125         {
00126         _LIT(KFeat, "Enter the UID of the feature to be added.\n");
00127         _LIT(KSelectUIDL1, "Select any UID, but in order to receive notification from featurechecker.exe,\n");
00128         _LIT(KSelectUIDL2,"choose a UID from the range 12000000 to 1200001A.\n");
00129         _LIT(KSelectUIDL3, "Notification takes the form of an info message displayed at the top right\n");
00130         _LIT(KSelectUIDL4, "corner of the screen.\n");
00131         iConsole->Printf(KFeat);
00132         iConsole->Printf(KSelectUIDL1);
00133         iConsole->Printf(KSelectUIDL2);
00134         iConsole->Printf(KSelectUIDL3);
00135         iConsole->Printf(KSelectUIDL4);
00136         TUid uidFeature;
00137         uidFeature.iUid = GetUserInput(EFalse);
00138                 
00139         TBitFlags32 flags(0);
00140         //Set the feature to be upgradable and modifiable.
00141     flags.Set( EFeatureSupported );
00142     flags.Set( EFeatureModifiable );
00143         
00144         _LIT(KSetFeaturedata, "\nEnter an integer value to set the feature data\n");
00145         iConsole->Printf(KSetFeaturedata);
00146         TUint32 fdata = GetUserInput(ETrue);
00147         iConsole->Printf(KNewline);
00148         TFeatureEntry featureEntry(uidFeature ,flags, fdata); 
00149         TInt featadded = User::LeaveIfError(iFeatControl.AddFeature(featureEntry));
00150         if(featadded == KErrNone)
00151                 {
00152                 _LIT(KFeatureAdded, "A new feature is added.\n");
00153                 iConsole->Printf(KFeatureAdded);
00154                 }
00155         RequestCharacter();
00156         }
00157 
00162 void CFeatMngrExample::DeleteL()
00163         {
00164         _LIT(KUIDtoDelete, "Enter the UID of the feature to delete\n");
00165         iConsole->Printf(KUIDtoDelete);
00166         TUid uidFeature;
00167         uidFeature.iUid = GetUserInput(EFalse);
00168         iConsole->Printf(KNewline);
00169         TInt featdeleted = User::LeaveIfError(iFeatControl.DeleteFeature(uidFeature));
00170         if(featdeleted == KErrNone)
00171                 {
00172                 _LIT(KFeatDeleted, "The feature is deleted.\n");
00173                 iConsole->Printf(KFeatDeleted);
00174                 }
00175         RequestCharacter();
00176         }
00177 
00182 void CFeatMngrExample::EnableFeatureL()
00183         {
00184         _LIT(KUIDtoEnable, "Enter the UID of the feature to enable/disable\n");
00185         iConsole->Printf(KUIDtoEnable);
00186         TUid uidFeature;
00187         uidFeature.iUid = GetUserInput(EFalse);
00188         
00189         _LIT(KEnable, "\nPress e/d to enable/disable it\n");
00190         iConsole->Printf(KEnable);
00191         TChar ch = iConsole->Getch();
00192         if(ch == 'e')
00193                 {
00194                 User::LeaveIfError(iFeatControl.EnableFeature(uidFeature));
00195                 }
00196         else if(ch == 'd')
00197                 {
00198                 User::LeaveIfError(iFeatControl.DisableFeature(uidFeature));
00199                 }
00200         RequestCharacter();
00201         }
00202 
00207 void CFeatMngrExample::CheckFeatureSupportL()
00208         {
00209         _LIT(KPrintUIDtoCheck, "Enter the UID to check if it is supported\n");
00210         iConsole->Printf(KPrintUIDtoCheck);
00211         
00212         TUid uidFeature;
00213         uidFeature.iUid = GetUserInput(EFalse);
00214         iConsole->Printf(KNewline);
00215         User::LeaveIfError(iFeatControl.FeatureSupported(uidFeature));
00216         
00217         TInt err = iFeatControl.FeatureSupported(uidFeature);
00218         if(err == KFeatureSupported)
00219                 {
00220                 _LIT(KFeatSupported, "\nThis feature is supported\n");
00221                 iConsole->Printf(KFeatSupported);
00222                 }
00223         else if(err == KFeatureUnsupported)
00224                 {
00225                 _LIT(KFeatNotsupported, "\nThis feature is not supported\n");
00226                 iConsole->Printf(KFeatNotsupported);
00227                 }
00228         else if(err == KErrNotFound)
00229                 {
00230                 _LIT(KFeatNotFound, "\nThis feature is not found\n");
00231                 iConsole->Printf(KFeatNotFound);
00232                 }
00233         else
00234                 {
00235                 _LIT(KErr, "Failed : %d");
00236                 iConsole->Printf(KErr, err);
00237                 }
00238         
00239         RequestCharacter();
00240         }
00241 
00245 void CFeatMngrExample::ListFeaturesL()
00246         {
00247         RFeatureUidArray supportedFeatures;
00248         CleanupClosePushL(supportedFeatures);
00249         
00250         iFeatControl.ListSupportedFeatures(supportedFeatures);
00251         TInt featureCount = supportedFeatures.Count();
00252         _LIT(KFeatureCount, "The number of features present is : %d \n");
00253         iConsole->Printf(KFeatureCount, featureCount);
00254         for(int j=0; j< featureCount; j++)
00255                 {
00256                 _LIT(KFeatureUID, "Feature UID%d is %x \n");
00257                 iConsole->Printf(KFeatureUID, (j+1), supportedFeatures[j]);
00258                 //list 12 entries at a time, to ensure visibility in the console.
00259                 if((j+1)%12 == 0)
00260                         {
00261                         _LIT(KPress, "Press any key to continue\n");
00262                         iConsole->Printf(KPress);
00263                         iConsole->Getch();
00264                         }
00265                 }
00266         CleanupStack::PopAndDestroy(&supportedFeatures);
00267         RequestCharacter();     
00268         }
00269 
00277 TInt64 CFeatMngrExample::GetUserInput(TBool aDecimal)
00278         {
00279         TBufC<50> buf;
00280         TPtr ptr(buf.Des());
00281         TKeyCode ch = iConsole->Getch();
00282         while(ch != EKeyEnter)
00283                 {
00284                 _LIT(KChar, "%c");
00285                 iConsole->Printf(KChar,ch);
00286                 if(ch!= EKeyBackspace)
00287                         {
00288                         ptr.Append(ch);
00289                         }
00290                 ch=iConsole->Getch();                           
00291                 }
00292         TInt64 value;
00293         TInt err;
00294         if(aDecimal)
00295                 {
00296                 err = TLex(ptr).Val(value);
00297                 }
00298         else
00299                 {
00300                 err = TLex(ptr).Val(value, EHex);
00301                 }
00302         if (err == KErrNone)
00303                 {
00304                 return value;
00305                 }
00306         else 
00307                 {
00308                 return KErrArgument; // Error condition - could not parse input.
00309                 }
00310         }
00311 
00315 void CFeatMngrExample::UpdateL()
00316         {
00317         _LIT(KFeatUpdate, "Enter the UID of the feature to be updated\n");
00318         iConsole->Printf(KFeatUpdate);
00319         TUid uidFeature;
00320         uidFeature.iUid = GetUserInput(EFalse);
00321                         
00322         _LIT(KSetFeaturedata, "\nEnter a number to set as the feature data\n");
00323         iConsole->Printf(KSetFeaturedata);
00324         TUint32 fdata = GetUserInput(ETrue);
00325         iConsole->Printf(KNewline);
00326         //Updates any existing feature  
00327         User::LeaveIfError(iFeatControl.SetFeature(uidFeature , fdata));
00328         RequestCharacter();
00329         }
00330 
00331 
00332 void CFeatMngrExample::DoCancel()
00333         {
00334         iConsole->ReadCancel();
00335         }
00336 
00337 
00338 void CFeatMngrExample::RequestCharacter()
00339         {
00340         _LIT(KMenuL1,"\nSelect one option from the following:\n");
00341         _LIT(KMenuL2,"1.List the features currently present\n");
00342         _LIT(KMenuL3,"2.Add a feature\n");
00343         _LIT(KMenuL4,"3.Update a feature\n");
00344         _LIT(KMenuL5,"4.Enable/Disable a feature\n");
00345         _LIT(KMenuL6,"5.Delete a feature\n");
00346         _LIT(KMenuL7,"6.Check feature support\n");
00347         _LIT(KMenuL8,"Press any other key to exit\n");
00348         
00349         iConsole->Printf(KMenuL1);
00350         iConsole->Printf(KMenuL2);
00351         iConsole->Printf(KMenuL3);
00352         iConsole->Printf(KMenuL4);
00353         iConsole->Printf(KMenuL5);
00354         iConsole->Printf(KMenuL6);
00355         iConsole->Printf(KMenuL7);
00356         iConsole->Printf(KMenuL8);
00357         iConsole->Printf(KNewline);
00358         // Read the key press. RunL() will be called when iStatus completes.
00359     iConsole->Read(iStatus);
00360     if(!IsActive())
00361         {
00362         SetActive();
00363         }
00364         }
00365 
00366 static void MainL()
00367         {
00368         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00369         CleanupStack::PushL(scheduler);
00370         CActiveScheduler::Install(scheduler);
00371         
00372         CFeatMngrExample* featMngr = CFeatMngrExample::NewL();
00373         CleanupStack::PushL(featMngr);
00374         featMngr->RequestCharacter();
00375         CActiveScheduler::Start();
00376         
00377         CleanupStack::PopAndDestroy(featMngr);
00378         CleanupStack::PopAndDestroy(scheduler);
00379         }
00380 
00381 extern TInt E32Main()
00382         {
00383         // Create cleanup stack
00384         __UHEAP_MARK;
00385         CTrapCleanup* cleanup = CTrapCleanup::New();
00386         if(cleanup == NULL)
00387                 {
00388                 return KErrNoMemory;
00389                 }
00390         // Run application code inside a TRAP harness.
00391         TRAPD(mainError, MainL());
00392         if(mainError != KErrNone)
00393                 {
00394                 _LIT(KUserPanic,"Failed to complete");  
00395                 User::Panic(KUserPanic, mainError);
00396                 }
00397         delete cleanup;
00398         __UHEAP_MARKEND;
00399         return KErrNone;
00400         }

Generated by  doxygen 1.6.2