author | sl |
Tue, 10 Jun 2014 14:32:02 +0200 | |
changeset 1 | 260cb5ec6c19 |
permissions | -rw-r--r-- |
sl@0 | 1 |
/* |
sl@0 | 2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
sl@0 | 3 |
* All rights reserved. |
sl@0 | 4 |
* This component and the accompanying materials are made available |
sl@0 | 5 |
* under the terms of the License "Eclipse Public License v1.0" |
sl@0 | 6 |
* which accompanies this distribution, and is available |
sl@0 | 7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
sl@0 | 8 |
* |
sl@0 | 9 |
* Initial Contributors: |
sl@0 | 10 |
* Nokia Corporation - initial contribution. |
sl@0 | 11 |
* |
sl@0 | 12 |
* Contributors: |
sl@0 | 13 |
* |
sl@0 | 14 |
* Description: |
sl@0 | 15 |
* |
sl@0 | 16 |
*/ |
sl@0 | 17 |
|
sl@0 | 18 |
|
sl@0 | 19 |
#include <e32base.h> |
sl@0 | 20 |
#include <bacline.h> |
sl@0 | 21 |
|
sl@0 | 22 |
#include "commandlineargs.h" |
sl@0 | 23 |
#include "sntpclientengine.h" |
sl@0 | 24 |
#include "util.h" |
sl@0 | 25 |
|
sl@0 | 26 |
LOCAL_C void RunSNTPClientL(); |
sl@0 | 27 |
LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs); |
sl@0 | 28 |
|
sl@0 | 29 |
|
sl@0 | 30 |
GLDEF_C TInt E32Main() // main function called by E32 |
sl@0 | 31 |
{ |
sl@0 | 32 |
__UHEAP_MARK; |
sl@0 | 33 |
CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack |
sl@0 | 34 |
TRAPD(err, RunSNTPClientL()); |
sl@0 | 35 |
delete cleanup; // destroy clean-up stack |
sl@0 | 36 |
__UHEAP_MARKEND; |
sl@0 | 37 |
return err; // and return |
sl@0 | 38 |
} |
sl@0 | 39 |
|
sl@0 | 40 |
|
sl@0 | 41 |
|
sl@0 | 42 |
LOCAL_C void RunSNTPClientL() |
sl@0 | 43 |
{ |
sl@0 | 44 |
TRAP_IGNORE(Util::SetAppropriateTimezoneL()); |
sl@0 | 45 |
|
sl@0 | 46 |
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; |
sl@0 | 47 |
CleanupStack::PushL(scheduler); |
sl@0 | 48 |
CActiveScheduler::Install(scheduler); |
sl@0 | 49 |
|
sl@0 | 50 |
// Parse the command line |
sl@0 | 51 |
|
sl@0 | 52 |
TCommandLineArgs args; |
sl@0 | 53 |
ParseCommandLineL(args); |
sl@0 | 54 |
|
sl@0 | 55 |
// Create the client object, and allow it to run to completion |
sl@0 | 56 |
|
sl@0 | 57 |
CSNTPClient* engine = CSNTPClient::NewLC(args); |
sl@0 | 58 |
engine->Start(); |
sl@0 | 59 |
CActiveScheduler::Start(); |
sl@0 | 60 |
|
sl@0 | 61 |
args.iServers.ResetAndDestroy(); |
sl@0 | 62 |
|
sl@0 | 63 |
CActiveScheduler::Install(NULL); |
sl@0 | 64 |
|
sl@0 | 65 |
if (engine->State() == EStateFailed) |
sl@0 | 66 |
{ |
sl@0 | 67 |
User::Leave(KErrGeneral); |
sl@0 | 68 |
} |
sl@0 | 69 |
else if (engine->State() == EStateAborted) |
sl@0 | 70 |
{ |
sl@0 | 71 |
User::Leave(KErrTimedOut); |
sl@0 | 72 |
} |
sl@0 | 73 |
|
sl@0 | 74 |
CleanupStack::PopAndDestroy(2, scheduler); |
sl@0 | 75 |
|
sl@0 | 76 |
} |
sl@0 | 77 |
|
sl@0 | 78 |
LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs) |
sl@0 | 79 |
{ |
sl@0 | 80 |
|
sl@0 | 81 |
/* Get and parse the command line arguments */ |
sl@0 | 82 |
|
sl@0 | 83 |
TBool parsingOffset(EFalse); |
sl@0 | 84 |
TBool enoughArguments(EFalse); |
sl@0 | 85 |
|
sl@0 | 86 |
aArgs.iOffset = 0; |
sl@0 | 87 |
aArgs.iApplyDaylightSavings = EFalse; |
sl@0 | 88 |
|
sl@0 | 89 |
CCommandLineArguments* args = CCommandLineArguments::NewLC(); |
sl@0 | 90 |
TInt argCount = args->Count(); |
sl@0 | 91 |
|
sl@0 | 92 |
/* Ignore the first argument, this is the executable name */ |
sl@0 | 93 |
|
sl@0 | 94 |
for (TInt i = 1; i < argCount; ++i) |
sl@0 | 95 |
{ |
sl@0 | 96 |
|
sl@0 | 97 |
TPtrC arg = args->Arg(i); |
sl@0 | 98 |
|
sl@0 | 99 |
|
sl@0 | 100 |
if (parsingOffset) |
sl@0 | 101 |
{ |
sl@0 | 102 |
|
sl@0 | 103 |
if (arg.Length() != 3) |
sl@0 | 104 |
{ |
sl@0 | 105 |
User::Leave(KErrArgument); |
sl@0 | 106 |
} |
sl@0 | 107 |
|
sl@0 | 108 |
|
sl@0 | 109 |
TInt temp(((arg[1] - '0') * 10) + (arg[2] - '0')); |
sl@0 | 110 |
|
sl@0 | 111 |
if (arg[0] == '-') |
sl@0 | 112 |
{ |
sl@0 | 113 |
aArgs.iOffset = -temp; |
sl@0 | 114 |
} |
sl@0 | 115 |
else |
sl@0 | 116 |
{ |
sl@0 | 117 |
aArgs.iOffset = temp; |
sl@0 | 118 |
} |
sl@0 | 119 |
|
sl@0 | 120 |
parsingOffset = EFalse; |
sl@0 | 121 |
|
sl@0 | 122 |
} |
sl@0 | 123 |
else if (arg.CompareF(KCommandLineDaylightSavings) == 0) |
sl@0 | 124 |
{ |
sl@0 | 125 |
aArgs.iApplyDaylightSavings = ETrue; |
sl@0 | 126 |
} |
sl@0 | 127 |
else if (arg.CompareF(KCommandLineOffset) == 0) |
sl@0 | 128 |
{ |
sl@0 | 129 |
parsingOffset = ETrue; |
sl@0 | 130 |
} |
sl@0 | 131 |
else |
sl@0 | 132 |
{ |
sl@0 | 133 |
// This argument must be the IP address/hostname |
sl@0 | 134 |
// sanity check the name |
sl@0 | 135 |
|
sl@0 | 136 |
if (arg.Length() > 256) |
sl@0 | 137 |
{ |
sl@0 | 138 |
User::Leave(KErrArgument); |
sl@0 | 139 |
} |
sl@0 | 140 |
|
sl@0 | 141 |
HBufC* server = arg.AllocLC(); |
sl@0 | 142 |
User::LeaveIfError(aArgs.iServers.Append(server)); |
sl@0 | 143 |
CleanupStack::Pop(server); |
sl@0 | 144 |
|
sl@0 | 145 |
enoughArguments = ETrue; |
sl@0 | 146 |
} |
sl@0 | 147 |
|
sl@0 | 148 |
} |
sl@0 | 149 |
|
sl@0 | 150 |
// Sanity check the arguments |
sl@0 | 151 |
if ((!enoughArguments) || aArgs.iOffset < -12 || aArgs.iOffset > 13) |
sl@0 | 152 |
{ |
sl@0 | 153 |
User::Leave(KErrArgument); |
sl@0 | 154 |
} |
sl@0 | 155 |
|
sl@0 | 156 |
CleanupStack::PopAndDestroy(args); |
sl@0 | 157 |
|
sl@0 | 158 |
} |