examples/Base/IPC/secureserver/secureclient.cpp

Go to the documentation of this file.
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 // This client executable has all required capabilities to modify a file.
00015 // Demonstrates all functionalities provided by the client side interface of the server.
00016 //
00017 
00018 
00019 
00023 #include "secureclientandserver.h"
00024 #include "secureclient.h"
00025 
00026 LOCAL_D CConsoleBase* console;
00027 
00028 void DoExampleL()
00029         {
00030         console->Printf(KTxtTestingCountServer);
00031         
00032         // Create a session.
00033         RSecureSession countserv;
00034         User::LeaveIfError(countserv.Connect());
00035         
00036         // Create a sub session.
00037         RSecureSubSession counterA;
00038         counterA.Open(countserv);
00039 
00040         // The user can choose to initialise the counter from the counter.dat file.
00041         console->Printf(KTxtInitCounterAWith);
00042         console->Printf(KTxtOption);
00043         TUint8 inpOption = console->Getch();
00044         if(inpOption == '1')
00045                 {
00046                 // This call will fail if the client executable does not have required file read capability.
00047                 counterA.SetCounterFromFileL();
00048                 console->Printf(KTxtInitFromFile,counterA.CounterValueL());
00049                 }
00050         else
00051                 {
00052                 // This is an invalid initialisation string. Hence, the counter will be initialised to zero.
00053                 _LIT(KTxtValueString,"a2");
00054                 console->Printf(KTxtValueString);
00055                 // Set counter from the KTxtValueString string
00056                 TInt err = counterA.SetFromString(KTxtValueString);
00057                 if (err == ENonNumericString)
00058                         {
00059                         // Print an error message as the initialisation string, KTxtValueString is invalid (non numeric format).
00060                         console->Printf(KTxtInitCounterFailed);
00061                         }
00062                 else
00063                         {
00064                         // Initialisation from string succeeded.
00065                         console->Printf(KTxtInitCounterSucceeded);
00066                         }
00067                 }
00068         RSecureSubSession counterB;
00069 
00070         // Create a sub session.
00071         counterB.Open(countserv);
00072 
00073         // The user can choose to initialise the counter from the counter.dat file.
00074         console->Printf(KTxtInitCounterBWith);
00075         console->Printf(KTxtOption);
00076         inpOption = console->Getch();
00077         if(inpOption == '1')
00078                 {
00079                 // This call will fail if the client executable does not have required file read capability.
00080                 counterB.SetCounterFromFileL();
00081                 console->Printf(KTxtInitFromFile,counterB.CounterValueL());
00082                 }
00083         else
00084                 {
00085                 // A valid numeric string.
00086                 _LIT(KTxtLegalString,"100");
00087                 console->Printf(KTxtLegalString);
00088                 // Set counter from the KTxtLegalString string.
00089                 TInt err = counterB.SetFromString(KTxtLegalString);
00090                 if (err == ENonNumericString)
00091                         {
00092                         // Print an error message as the initialisation string, KTxtValueString is invalid (non numeric format).
00093                         console->Printf(KTxtInitCounterFailed);
00094                         }
00095                 else
00096                         {
00097                         // Initialisation from string succeeded.
00098                         // Counter is initialised to 100.
00099                         console->Printf(KTxtInitCounterSucceeded);
00100                         }
00101                 }
00102         
00103         // Wait for a key press.
00104         console->Printf(KMsgPressAnyKey);
00105         console->Getch();
00106         console->ClearScreen();
00107 
00108         // Print values of both counters.
00109         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00110         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00111 
00112         // Wait for a key press.
00113         console->Printf(KMsgPressAnyKey);
00114         console->Getch();
00115         console->ClearScreen();
00116 
00117         // Increase the counter value by 1 and print it.
00118         _LIT(KTxt1,"\nIncrease counterA by default value (i.e. 1)..\n");
00119         console->Printf(KTxt1);
00120         counterA.Increase();
00121         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00122 
00123         // Increase the counter by a value greater than 1 and print it.
00124         _LIT(KTxt2,"\nIncrease counterA by 2..\n");
00125         console->Printf(KTxt2);
00126         // IncreaseByL operation undergoes a custom security check.
00127         // If the value is greater than 10 and the SID is greater than 0x70fffff5, it fails.
00128         counterA.IncreaseByL(2);
00129         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00130 
00131         // Increase the counter value by 1 and print it.
00132         _LIT(KTxt3,"\nIncrease counterB by default value (i.e. 1)..\n");
00133         console->Printf(KTxt3);
00134         counterB.Increase();
00135         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00136 
00137         // Increase the counter by a value greater than 1 and print it.
00138         _LIT(KTxt4,"\nIncrease counterA by 17..\n");
00139         console->Printf(KTxt4);
00140         // Fails if the SID is greater than 0x70fffff5
00141         counterA.IncreaseByL(17);
00142         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00143 
00144         // Wait for a key press.
00145         console->Printf(KMsgPressAnyKey);
00146         console->Getch();
00147         console->ClearScreen();
00148 
00149         // Increase the counter by a value greater than 1 and print it.
00150         _LIT(KTxt5,"\nIncrease counterB by 5..\n");
00151         console->Printf(KTxt5);
00152         counterB.IncreaseByL(5);
00153         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00154 
00155         // Decrease the counter value by 1 and print it.
00156         _LIT(KTxt6,"\nDecrease counterA..\n");
00157         console->Printf(KTxt6);
00158         counterA.Decrease();
00159         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00160 
00161         // Decrease the counter by a value greater than 1 and print it.
00162         _LIT(KTxt7,"\nDecrease counterB by 3..\n");
00163         console->Printf(KTxt7);
00164         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00165         // DecreaseByL operation undergoes a custom security check.
00166         // If the value is greater than 10 and the SID is greater than 0x70fffff5, it fails.
00167         counterB.DecreaseByL(3);
00168 
00169         // Print the resource count of the server.
00170         _LIT(KTxt8,"\nResource count is.. %d \n");
00171         console->Printf(KTxt8,countserv.ResourceCountL());
00172 
00173         // The user can save the counter value to a file.
00174         console->Printf(KTxtSaveToFile);
00175         TUint8 sel = console->Getch();
00176         if(sel == 'a')
00177                 {
00178                 // Save the value of the counter A.
00179                 console->Printf(KTxtSelectCounter,sel);
00180                 console->Printf(KTxtSaveCounter);
00181                 // This call will fail if the client executable does not have required file write capability.
00182                 counterA.SaveCounterL();
00183                 }
00184         else if(sel == 'b')
00185                 {
00186                 // Save the value of the counter B.
00187                 console->Printf(KTxtSelectCounter,sel);
00188                 console->Printf(KTxtSaveCounter);
00189                 // This call will fail if the client executable does not have required file write capability.
00190                 counterB.SaveCounterL();
00191                 }
00192 
00193         // Close both sub sessions and the main session.
00194         _LIT(KTxt9,"\nClosing counterA and then CounterB..\n");
00195         console->Printf(KTxt9);
00196         counterA.Close();
00197         counterB.Close();
00198         countserv.Close();
00199         }
00200 
00201 GLDEF_C TInt E32Main()
00202         {
00203         __UHEAP_MARK;
00204         CTrapCleanup* cleanup = CTrapCleanup::New();
00205 
00206         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00207         if (createError)
00208                 return createError;
00209 
00210         TRAPD(mainError, DoExampleL());
00211         if (mainError)
00212                 console->Printf(KTextFailed, mainError);
00213         console->Printf(KTextPressAnyKey);
00214         console->Getch();
00215 
00216         delete console;
00217         delete cleanup;
00218         __UHEAP_MARKEND;
00219         return KErrNone;
00220         }

Generated by  doxygen 1.6.2