/** Executes Alarm Server Test
** Set Alarm to notify, Set alarm sounds silent for in minutes. Change System before or after alarm expiry time.
** Sound start and stop should be as expected
@return True or False indicating the result of the alarm server test
*/
TBool CTestSystemChangeFor::ExecuteTestL()
	{
	SetSystemTimeL();

	SetInitalUtcOffsetL();

	SetAlarmL();

	// Set Global silent peroid duration in minutes (SetAlarmSoundsSilentFor)
	SetSilentForL();

	// Determine wether to change system before or after alarm expires
	GetWaitToNotify();

	iExpiryGreaterUntil = CheckExpGreaterUntilL(iInitialUtc);
	// wait for alarm to reach expiry time, then change system time, and then wait for sound start and stop
	if (iWaitToNotify)
		{
		if (!WaitToNotifyBeforeChangeL())
			{
			return EFalse;
			}
		}

	// Get new system time and set
	ChangeSystemTimeL();

	// Let the Alarm Server react to the System Time Change change
	User::After(KTimeToWait);

	iNow.UniversalTime();

	VerifySilentUntil();

	if (iWaitToNotify)  // If alarm notified then alarm state should be Notifying
		{
		INFO_PRINTF1(_L("After System Time Change, checking state....."));
		if(!CheckAlarmStatesL(EAlarmStateNotifying))
			{
			return EFalse;
			}
		}

	TTimeIntervalMinutes minutesDifference;
	User::LeaveIfError(iSystemTime.MinutesFrom(iExpiryTime, minutesDifference));

	TInt difference = minutesDifference.Int();

	// wait for alarm to expire after system time change then wait for sound start and stop
	// new system time less than expiry time, so we wait for alarm to expire
	if (!iWaitToNotify && difference <= -1 && iMoveSystemTime < iExpiryTime)
		{
		if(!WaitToNotifyAfterChangeL())
			{
			return EFalse;
			}
		}

	// new system time is equal or greater than expiry time
	// state should be notifying, so we don't wait for alarm to expire
	// Just need to check if alarm is in Notifying state as well as start.stop sounds timings
	if (!iWaitToNotify && difference >= -1 && iMoveSystemTime >= iExpiryTime)
		{
		INFO_PRINTF1(_L(" New System Time equal to Expiry Time or Greater, checking state.... "));

		if(!CheckAlarmNotifyingL())
			{
			return EFalse;
			}
		}

	// checks if sound start and stop timings are as expected
	if(!CheckStartStopSoundsL())
		{
		return EFalse;
		}

	return ETrue;
	}
//*************************************************************************************
void CConsoleAlarmAlertSession::ServiceL(const RMessage2& aMessage)
	{
	iMsgComplete = ETrue;
	const TInt function = aMessage.Function();

	//see if the server holds a set of instructions to be followed,
	//rather than waiting for user input.
	if(!iInstructionSetFound) //go look for it now.
		{
		//we will enter into this code only once
		const CServer2* tmpServ = Server();
		const CConsoleAlarmAlertServer* specific = reinterpret_cast<const CConsoleAlarmAlertServer*>(tmpServ);
		iInstructionSet = const_cast<CConsoleAlarmAlertServer*>(specific)->ResponseArray();
		iInstructionSetFound = ETrue;
		}

	//
	switch(function)
		{
    case EASAltOpCodeNotify:
    	{
		__ASSERT_ALWAYS(!iHasMessage, User::Invariant());
		iMsgComplete = EFalse;
		iMsgPtr = aMessage;
		iHasMessage = ETrue;
		CConsoleAlarmAlertSession* session = iServer->WaitingSession();
		session = this;
		break;
		}

    case EASAltOpCodeNotifyCancel:
		if	(iHasMessage)
			{
			iHasMessage=EFalse;
			iMsgPtr.Complete(KErrCancel);
			}
		break;

	case EASAltOpCodeVisible:
		{
		TBool isVisible = aMessage.Int0();
		iConsole->SetVisibility(isVisible);
		iServer->SetNotifying(isVisible);
		break;
		}

    case EASAltOpCodeSetState:
		iConsole->SetAlertState(aMessage.Int0());
		break;

    case EASAltOpCodeSetAlarm:
		SetAlarmL(aMessage);
		iConsole->iTimeInterval=0;
		iConsole->UpdateDisplay();
		break;

	case EASAltOpCodeGetUserTime:
		GetUserTimeL(aMessage);
		break;

	case EASAltOpCodeSetDeferTime:
		SetDeferTimeL(aMessage);
		iConsole->UpdateDisplay();
		break;

    case EASAltOpCodeLogon:
		__ASSERT_ALWAYS(!iLoggedOn, User::Invariant());
		iMsgComplete=EFalse;
		iLoggedOn=ETrue;
		break;

	case EASAltOpCodeStartPlayingSound:
		{
		iSoundLEDFlasher->Start();
		TCallBack callback(SoundPlaybackCallbackL, this);
		iSoundCallbackTimer->Start(TTimeIntervalMicroSeconds32(0), TTimeIntervalMicroSeconds32(3000000), callback);
		break;
		}

	case EASAltOpCodeStopPlayingSound:
		iSoundLEDFlasher->Stop();
		iSoundCallbackTimer->Cancel();
		break;

#if 0
//Test code
	case EASAltTestOpCodeAcknowledgeAlarm:
		if (iServer->WaitingSession())
			iServer->WaitingSession()->Notify(EASAltAlertServerResponseClear);
		break;

	case EASAltTestOpCodeGetAttachment:
		{
		if (iServer->Attachment() != NULL)
			{
			const TDes8& data = iServer->Attachment()->Des();
			const TInt maxLength = static_cast<TInt>(aMessage.Int0());
			if	(data.Size() > maxLength)
				User::Panic(_L("Client's buffer is too short"), KErrOverflow);
			else
				{
				if (data.Length() == 0)
					{
					_LIT8(KEmpty,"Empty");
					aMessage.WriteL(KSlot1, KEmpty);
					}
				else
					{
					aMessage.WriteL(KSlot1, data);
					}
				iServer->SetAttachment(NULL);
				}
			}
		}
		break;

	case EASAltTestOpCodeGetIsNotifying:
		{
		TPckgC<TBool> ret = iServer->IsNotifying();
		aMessage.WriteL(KSlot0, ret);
		break;
		}
#endif
	default:
		User::Invariant();
		break;
		}

	if	(iMsgComplete)
		aMessage.Complete(KErrNone);
	}
/** Executes Alarm Server Test
** Set Alarm to notify, Set alarm sounds silent until. Change System before or after alarm expiry time.
** Sound start and stop should be as expected
@return True or False indicating the result of the alarm server test
*/
TBool CTestSystemChangeUntil::ExecuteTestL()
	{
	SetSystemTimeL();

	SetInitalUtcOffsetL();

	SetAlarmL();

	// Set Global silent peroid duration in minutes (SetAlarmSoundsSilentFor)
	SetSilentUntilL();

	iUTCUntil = CalculateUTCUntilL(iInitialUtc);

	// Determine wether to change utc before or after alarm expires
	GetWaitToNotify();

	//Get New Offset and set
	iNewSysGreaterExp = EFalse;
	iUntilExpired = EFalse;
	iExpiryGreaterUntil = CheckExpGreaterUntilL(iInitialUtc);
	// wait for alarm to reach expiry time, then change system time, and then wait for sound start and stop
	if (iWaitToNotify)
		{
		if (!WaitToNotifyBeforeChangeL())
			{
			return EFalse;
			}
		}

	// Get new system time and set
	ChangeSystemTimeL();

	// Let the Alarm Server react to the System Time Change change
	User::After(KTimeToWait);

	iNow.UniversalTime();

	// silent time should have not changed,after system time change
	if (iNow <= iUTCUntil) // If present utc time is greater then until time, then we dont check
		{                // silent period as it will crash (getuntil crashes, as its time has passed
		if (!iWaitToNotify && !iExpiryGreaterUntil) //if silent period expired this will crash
			{
			if (!CheckSilentUntilL())
				{
				return EFalse;
				}
			}
		}

	if (iWaitToNotify) // If alarm has expired, alarm should be in Notifying state, then validate for sound start and stop
		{
		INFO_PRINTF1(_L("After System Time Change, checking state....."));
		if(!CheckAlarmStatesL(EAlarmStateNotifying))
			{
			return EFalse;
			}
		}

	TTimeIntervalMinutes minutesDifference;
	User::LeaveIfError(iSystemTime.MinutesFrom(iExpiryTime, minutesDifference));

	TInt difference = minutesDifference.Int();

	// wait for alarm to expire after utc change then wait for sound start and stop
	// new system time less than expiry time, so we wait for alarm to expire
	if (!iWaitToNotify && difference <= -1 && iMoveSystemTime < iExpiryTime)
		{
		if (!WaitToNotifyAfterChangeL())
			{
			return EFalse;
			}
		}

	// new system time is equal or greater than expiry time
	// state should be notifying,as alarm should expire immediately, so we dont wait for alarm to expire
	if (!iWaitToNotify && difference >= -1 && iMoveSystemTime >= iExpiryTime)
		{
		INFO_PRINTF1(_L(" New System Time equal to Expiry Time or Greater, checking state.... "));
		if(!CheckAlarmStatesL(EAlarmStateNotifying))
			{
			return EFalse;
			}

		WaitTimerExpiry();
		INFO_PRINTF1(_L("This expiry notification is due to the timer invalidating due to System Time Change "));
		iNewSysGreaterExp = ETrue;
		}

	if (iInitialUtc < 0)
		{
		INFO_PRINTF1(_L("More than an hour to go to play sounds "));
		}
	else
		{
		if (!CheckStartStopTimingsL()) // checks if start and stop timings are as expected
			{
			return EFalse;
			}
		}

	return ETrue;
	}