sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "commandlineargs.h" sl@0: #include "sntpclientengine.h" sl@0: #include "util.h" sl@0: sl@0: LOCAL_C void RunSNTPClientL(); sl@0: LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs); sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() // main function called by E32 sl@0: { sl@0: __UHEAP_MARK; sl@0: CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack sl@0: TRAPD(err, RunSNTPClientL()); sl@0: delete cleanup; // destroy clean-up stack sl@0: __UHEAP_MARKEND; sl@0: return err; // and return sl@0: } sl@0: sl@0: sl@0: sl@0: LOCAL_C void RunSNTPClientL() sl@0: { sl@0: TRAP_IGNORE(Util::SetAppropriateTimezoneL()); sl@0: sl@0: CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; sl@0: CleanupStack::PushL(scheduler); sl@0: CActiveScheduler::Install(scheduler); sl@0: sl@0: // Parse the command line sl@0: sl@0: TCommandLineArgs args; sl@0: ParseCommandLineL(args); sl@0: sl@0: // Create the client object, and allow it to run to completion sl@0: sl@0: CSNTPClient* engine = CSNTPClient::NewLC(args); sl@0: engine->Start(); sl@0: CActiveScheduler::Start(); sl@0: sl@0: args.iServers.ResetAndDestroy(); sl@0: sl@0: CActiveScheduler::Install(NULL); sl@0: sl@0: if (engine->State() == EStateFailed) sl@0: { sl@0: User::Leave(KErrGeneral); sl@0: } sl@0: else if (engine->State() == EStateAborted) sl@0: { sl@0: User::Leave(KErrTimedOut); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(2, scheduler); sl@0: sl@0: } sl@0: sl@0: LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs) sl@0: { sl@0: sl@0: /* Get and parse the command line arguments */ sl@0: sl@0: TBool parsingOffset(EFalse); sl@0: TBool enoughArguments(EFalse); sl@0: sl@0: aArgs.iOffset = 0; sl@0: aArgs.iApplyDaylightSavings = EFalse; sl@0: sl@0: CCommandLineArguments* args = CCommandLineArguments::NewLC(); sl@0: TInt argCount = args->Count(); sl@0: sl@0: /* Ignore the first argument, this is the executable name */ sl@0: sl@0: for (TInt i = 1; i < argCount; ++i) sl@0: { sl@0: sl@0: TPtrC arg = args->Arg(i); sl@0: sl@0: sl@0: if (parsingOffset) sl@0: { sl@0: sl@0: if (arg.Length() != 3) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: sl@0: TInt temp(((arg[1] - '0') * 10) + (arg[2] - '0')); sl@0: sl@0: if (arg[0] == '-') sl@0: { sl@0: aArgs.iOffset = -temp; sl@0: } sl@0: else sl@0: { sl@0: aArgs.iOffset = temp; sl@0: } sl@0: sl@0: parsingOffset = EFalse; sl@0: sl@0: } sl@0: else if (arg.CompareF(KCommandLineDaylightSavings) == 0) sl@0: { sl@0: aArgs.iApplyDaylightSavings = ETrue; sl@0: } sl@0: else if (arg.CompareF(KCommandLineOffset) == 0) sl@0: { sl@0: parsingOffset = ETrue; sl@0: } sl@0: else sl@0: { sl@0: // This argument must be the IP address/hostname sl@0: // sanity check the name sl@0: sl@0: if (arg.Length() > 256) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: HBufC* server = arg.AllocLC(); sl@0: User::LeaveIfError(aArgs.iServers.Append(server)); sl@0: CleanupStack::Pop(server); sl@0: sl@0: enoughArguments = ETrue; sl@0: } sl@0: sl@0: } sl@0: sl@0: // Sanity check the arguments sl@0: if ((!enoughArguments) || aArgs.iOffset < -12 || aArgs.iOffset > 13) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(args); sl@0: sl@0: }