commonappservices/alarmserver/Server/Source/ASSrvSession.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "ASSrvSession.h"
       
    17 
       
    18 // System includes
       
    19 #include <s32mem.h>
       
    20 
       
    21 // User includes
       
    22 #include "ASShdPragma.h"
       
    23 #include "ASShdOpCodes.h"
       
    24 //
       
    25 #include "ASSrvTimer.h"
       
    26 #include "ASSrvAlarmQueue.h"
       
    27 #include "ASSrvStaticUtils.h"
       
    28 #include "ASSrvSessionEngine.h"
       
    29 #include "ASSrvSoundSettings.h"
       
    30 #include "ASSrvServerWideData.h"
       
    31 #include "ASSrvSoundController.h"
       
    32 #include "ASSrvAlarmSoundDetails.h"
       
    33 #include "ASSrvNotificationCoordinator.h"
       
    34 
       
    35 #ifdef _DEBUG
       
    36 #include "ASSrvEnvironmentChangeManager.h"
       
    37 #endif 
       
    38 
       
    39 // Type definitions
       
    40 
       
    41 // Constants
       
    42 const TInt KAlarmServerTransferBufferExpandSize = 100;
       
    43 const TInt KSlot0 = 0;
       
    44 const TInt KSlot1 = 1;
       
    45 const TInt KSlot2 = 2;
       
    46 
       
    47 // Enumerations
       
    48 
       
    49 // Classes referenced
       
    50 
       
    51 
       
    52 //
       
    53 // ----> CASSrvSession (source)
       
    54 //
       
    55 
       
    56 //*************************************************************************************
       
    57 CASSrvSession::CASSrvSession(CASSrvServerWideData& aServerWideData)
       
    58 :	iServerWideData(aServerWideData), iAlarmSink(aServerWideData)
       
    59 	{
       
    60 	}
       
    61 
       
    62 
       
    63 //*************************************************************************************
       
    64 CASSrvSession::~CASSrvSession()
       
    65 	{
       
    66 	HandleServerDestruction();
       
    67 	//
       
    68 	delete iTransferBuffer;
       
    69 	//
       
    70 	iChangeEventBuffer.Close();
       
    71 	}
       
    72 
       
    73 
       
    74 //*************************************************************************************
       
    75 void CASSrvSession::ConstructL()
       
    76 	{
       
    77 	iTransferBuffer = CBufFlat::NewL(KAlarmServerTransferBufferExpandSize);
       
    78 	iSessionEngine = CASSrvSessionEngine::NewL(ServerData(), *this, *this);
       
    79 	}
       
    80 
       
    81 
       
    82 //*************************************************************************************
       
    83 CASSrvSession* CASSrvSession::NewL(CASSrvServerWideData& aServerWideData)
       
    84 	{
       
    85 	CASSrvSession* self = new(ELeave) CASSrvSession(aServerWideData);
       
    86 	CleanupStack::PushL(self);
       
    87 	self->ConstructL();
       
    88 	CleanupStack::Pop(self);
       
    89 	return self;
       
    90 	}
       
    91 
       
    92 
       
    93 //
       
    94 //
       
    95 //
       
    96 
       
    97 
       
    98 //*************************************************************************************
       
    99 /**
       
   100  * Called by the server's destructor. We need to be told that the server is
       
   101  * being destroyed, since the server owns the server-wide data, and this class
       
   102  * must destroy the session engine (which will unregister for various notifications)
       
   103  * using the server wide data instance.
       
   104  */
       
   105 void CASSrvSession::HandleServerDestruction()
       
   106 	{
       
   107 	delete iSessionEngine;
       
   108 	iSessionEngine = NULL;
       
   109 	}
       
   110 
       
   111 
       
   112 //
       
   113 //
       
   114 //
       
   115 
       
   116 
       
   117 //*************************************************************************************
       
   118 /**
       
   119  * @see MASSrvAnyEventObserver
       
   120  */
       
   121 void CASSrvSession::MASSrvAnyEventHandleChange(TAlarmChangeEvent aEvent, TAlarmId aAlarmId)
       
   122 	{
       
   123 	CompleteChangeNotificationMessage(aEvent, aAlarmId);
       
   124 	}
       
   125 
       
   126 
       
   127 //
       
   128 //
       
   129 //
       
   130 
       
   131 
       
   132 //*************************************************************************************
       
   133 /**
       
   134  * @see MASSrvSession
       
   135  *
       
   136  * Facade to the real session
       
   137  */
       
   138 TASSrvSessionId CASSrvSession::MASSrvSessionId() const
       
   139 	{
       
   140 	// This shouldn't ever be called
       
   141 	__ASSERT_DEBUG(EFalse, ASSrvStaticUtils::Fault(ASSrvStaticUtils::EASSrvFaultFacadeInterfaceError));
       
   142 	return KNullSessionId;
       
   143 	}
       
   144 
       
   145 
       
   146 //*************************************************************************************
       
   147 /**
       
   148  * @see MASSrvSession
       
   149  *
       
   150  * Facade to the real session
       
   151  */
       
   152 void CASSrvSession::MASSrvSessionFullName(TDes& aDes) const
       
   153 	{
       
   154 	aDes.Zero();
       
   155 	}
       
   156 
       
   157 
       
   158 //
       
   159 //
       
   160 //
       
   161 
       
   162 
       
   163 //*************************************************************************************
       
   164 void CASSrvSession::ServiceL(const RMessage2& aMessage)
       
   165 	{
       
   166 	// Leave's are caught by CASSrvServer::RunError
       
   167 	iMessage=&aMessage;
       
   168 	const TBool completeMessage = DoServiceL();
       
   169 	if	(completeMessage)
       
   170 		aMessage.Complete(KErrNone);
       
   171 	iMessage=NULL;
       
   172 	}
       
   173 
       
   174 
       
   175 //
       
   176 //
       
   177 //
       
   178 
       
   179 
       
   180 //*************************************************************************************
       
   181 TBool CASSrvSession::DoServiceL()
       
   182 	{
       
   183 	// Reset alarm sink to remove old data
       
   184 	iAlarmSink.Reset();
       
   185 
       
   186 	//
       
   187 	// POSSIBLY OPTIMISE THIS LOT INTO A LOOKUP TABLE?
       
   188 	//
       
   189 	TBool completeMessage = EFalse;
       
   190 	const TInt opCode = Message().Function();
       
   191 	//
       
   192 	switch(opCode)
       
   193 		{
       
   194 	//
       
   195 	// CMD - ALARM SPECIFIC FUNCTIONALITY
       
   196 	case EASShdOpCodeAlarmAdd:
       
   197 		completeMessage = CmdAlarmAddL();
       
   198 		break;
       
   199 	case EASShdOpCodeAlarmAddWithNotification:
       
   200 		completeMessage = CmdAlarmAddWithNotificationL();
       
   201 		break;
       
   202 	case EASShdOpCodeAlarmNotificationCancelAndDeQueue:
       
   203 		completeMessage = CmdAlarmNotificationCancelAndDeQueueL();
       
   204 		break;
       
   205 	case EASShdOpCodeGetAlarmDetails:
       
   206 		completeMessage = CmdGetAlarmDetailsL();
       
   207 		break;
       
   208 	case EASShdOpCodeAlarmDelete:
       
   209 		completeMessage = CmdAlarmDeleteL();
       
   210 		break;
       
   211 	case EASShdOpCodeGetAlarmCategory:
       
   212 		completeMessage = CmdGetAlarmCategoryL();
       
   213 		break;
       
   214 	case EASShdOpCodeGetAlarmOwner:
       
   215 		completeMessage = CmdGetAlarmOwnerL();
       
   216 		break;
       
   217 	case EASShdOpCodeSetAlarmStatus:
       
   218 		completeMessage = CmdSetAlarmStatusL();
       
   219 		break;
       
   220 	case EASShdOpCodeSetAlarmStatusForCalendarFile:
       
   221         completeMessage = CmdSetAlarmStatusForCalendarFileL();
       
   222         break;		
       
   223 	case EASShdOpCodeGetAlarmStatus:
       
   224 		completeMessage = CmdGetAlarmStatusL();
       
   225 		break;
       
   226 	case EASShdOpCodeSetAlarmDayOrTimed:
       
   227 		completeMessage = CmdSetAlarmDayOrTimedL();
       
   228 		break;
       
   229 	case EASShdOpCodeGetAlarmDayOrTimed:
       
   230 		completeMessage = CmdGetAlarmDayOrTimedL();
       
   231 		break;
       
   232 	case EASShdOpCodeGetAlarmCharacteristics:
       
   233 		completeMessage = CmdGetAlarmCharacteristicsL();
       
   234 		break;
       
   235 	case EASShdOpCodeSetAlarmCharacteristics:
       
   236 		completeMessage = CmdSetAlarmCharacteristicsL();
       
   237 		break;
       
   238 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   239 	case EASShdOpCodeSetWakeup:
       
   240 		completeMessage = CmdSetWakeupL();
       
   241 		break;
       
   242 #endif
       
   243 #ifdef SYMBIAN_ALARM_REPEAT_EXTENSIONS
       
   244 	case EASShdOpCodeGetAlarmDays:
       
   245 		completeMessage = CmdGetAlarmDaysL();
       
   246 		break;
       
   247 	case EASShdOpCodeSetAlarmDays:
       
   248 		completeMessage = CmdSetAlarmDaysL();
       
   249 		break;
       
   250 	case EASShdOpCodeSetContinuous:
       
   251 		completeMessage = CmdSetContinuousL();
       
   252 		break;
       
   253 	case EASShdOpCodeGetContinuous:
       
   254 		completeMessage = CmdContinuousL();
       
   255 		break;
       
   256 #endif
       
   257 	case EASShdOpCodeOrphanAlarm:
       
   258 		completeMessage = CmdOrphanAlarmL();
       
   259 		break;
       
   260 	case EASShdOpCodeSetClientData:
       
   261 		completeMessage = CmdSetClientDataL();
       
   262 		break;
       
   263 	//
       
   264 	// CMD - ALARM DATA FUNCTIONALITY
       
   265 	case EASShdOpCodeAlarmDataAttach:
       
   266 		completeMessage = CmdAlarmDataAttachL();
       
   267 		break;
       
   268 	case EASShdOpCodeAlarmDataDetach:
       
   269 		completeMessage = CmdAlarmDataDetachL();
       
   270 		break;
       
   271 	case EASShdOpCodeAlarmDataSize:
       
   272 		completeMessage = CmdAlarmDataSizeL();
       
   273 		break;
       
   274 	case EASShdOpCodeGetAlarmData:
       
   275 		completeMessage = CmdGetAlarmDataL();
       
   276 		break;
       
   277 	//
       
   278 	// CMD - CATEGORY-SPECIFIC FUNCTIONALITY
       
   279 	case EASShdOpCodeSetAlarmStatusByCategory:
       
   280 		completeMessage = CmdSetAlarmStatusByCategoryL();
       
   281 		break;
       
   282 	case EASShdOpCodeGetAlarmCountForCategory:
       
   283 		completeMessage = CmdGetAlarmCountForCategoryL();
       
   284 		break;
       
   285 	case EASShdOpCodeAlarmDeleteAllByCategory:
       
   286 		completeMessage = CmdAlarmDeleteAllByCategoryL();
       
   287 		break;
       
   288 	case EASShdOpCodeAlarmDeleteByCategory:
       
   289 		completeMessage = CmdAlarmDeleteByCategoryL();
       
   290 		break;
       
   291 	case EASShdOpCodeAlarmDeleteByCalendarFile:
       
   292         completeMessage = CmdAlarmDeleteByCalendarFileL();
       
   293         break;
       
   294 	case EASShdOpCodeGetAvailableCategoryList:
       
   295 		completeMessage = CmdGetAvailableCategoryListL();
       
   296 		break;
       
   297 	case EASShdOpCodeGetAlarmIdListForCategory:
       
   298 		completeMessage = CmdGetAlarmIdListForCategoryL();
       
   299 		break;
       
   300 	//
       
   301 	// CMD - MISC FUNCTIONALITY
       
   302 	case EASShdOpCodeAlarmCountByState:
       
   303 		completeMessage = CmdAlarmCountByStateL();
       
   304 		break;
       
   305 	case EASShdOpCodeGetAlarmIdListByState:
       
   306 		completeMessage = CmdGetAlarmIdListByStateL();
       
   307 		break;
       
   308 	case EASShdOpCodeGetAlarmIdList:
       
   309 		completeMessage = CmdGetAlarmIdListL();
       
   310 		break;
       
   311 	case EASShdOpCodeGetNextDueAlarmId:
       
   312 		completeMessage = CmdGetNextDueAlarmIdL();
       
   313 		break;
       
   314 	case EASShdOpCodeNumberOfAlarmsActiveInQueue:
       
   315 		completeMessage = CmdNumberOfActiveAlarmsInQueueL();
       
   316 		break;
       
   317 	//
       
   318 	// CMD - SOUND CONTROL
       
   319 	case EASShdOpCodeSetAlarmSoundState:
       
   320 		completeMessage = CmdSetAlarmSoundStateL();
       
   321 		break;
       
   322 	case EASShdOpCodeGetAlarmSoundState:
       
   323 		completeMessage = CmdGetAlarmSoundStateL();
       
   324 		break;
       
   325 	case EASShdOpCodeSetAlarmSoundsSilentUntil:
       
   326 		completeMessage = CmdSetAlarmSoundsSilentUntilL();
       
   327 		break;
       
   328 	case EASShdOpCodeSetAlarmSoundsSilentFor:
       
   329 		completeMessage = CmdSetAlarmSoundsSilentForL();
       
   330 		break;
       
   331 	case EASShdOpCodeGetAlarmSoundsSilentUntil:
       
   332 		completeMessage = CmdGetAlarmSoundsSilentUntilL();
       
   333 		break;
       
   334 	case EASShdOpCodeCancelAlarmSilence:
       
   335 		completeMessage = CmdCancelAlarmSilenceL();
       
   336 		break;
       
   337 	case EASShdOpCodeAlarmSoundsTemporarilySilenced:
       
   338 		completeMessage = CmdGetAlarmSoundsTemporarilySilencedL();
       
   339 		break;
       
   340 	case EASShdOpCodeSetAlarmPlayIntervals:
       
   341 		completeMessage = CmdSetAlarmPlayIntervalsL();
       
   342 		break;
       
   343 	case EASShdOpCodeGetAlarmPlayIntervals:
       
   344 		completeMessage = CmdGetAlarmPlayIntervalsL();
       
   345 		break;
       
   346 	//
       
   347 	// CMD - CHANGE NOTIFICATION
       
   348 	case EASShdOpCodeNotifyChange:
       
   349 		completeMessage = CmdNotifyChangeL();
       
   350 		break;
       
   351 	case EASShdOpCodeNotifyChangeCancel:
       
   352 		completeMessage = CmdNotifyChangeCancelL();
       
   353 		break;
       
   354 	//
       
   355 	// CMD - DEBUG ONLY
       
   356 	case EASShdOpCodeDbgShutDownServer:
       
   357 		completeMessage = CmdDbgShutDownServerL();
       
   358 		break;
       
   359 	case EASShdOpCodeDbgFailAlloc:
       
   360 		completeMessage = CmdDbgFailAllocL();
       
   361 		break;
       
   362 	case EASShdOpCodeDbgPreventUserNotify:
       
   363 		completeMessage = CmdDbgPreventUserNotifyL();
       
   364 		break;
       
   365 	case EASShdOpCodeDbgSnoozeAlarm:
       
   366 		completeMessage = CmdDbgSnoozeAlarmL();
       
   367 		break;
       
   368 	case EASShdOpCodeDbgSetEnvironmentChangesHandling:
       
   369 		completeMessage = CmdDbgSetEnvChgHandling();
       
   370 		break;	
       
   371 	//
       
   372 	// CMD - MISC
       
   373 	case EASShdOpCodeFetchTransferBuffer:
       
   374 		completeMessage = CmdFetchTransferBufferL();
       
   375 		break;
       
   376 	//
       
   377 	// CMD - FROM MASShdAlarmInfoProvider
       
   378 	case EASShdOpCodeAlarmCount:
       
   379 		completeMessage = CmdInfoAlarmCountL();
       
   380 		break;
       
   381 	case EASShdOpCodeAlarmAtIndex:
       
   382 		completeMessage = CmdInfoAlarmByIndexL();
       
   383 		break;
       
   384 	case EASShdOpCodeFlushServer:
       
   385 		completeMessage = ETrue;
       
   386 		break;
       
   387  
       
   388 
       
   389 	default:
       
   390 		ASSrvStaticUtils::PanicClient(Message(), EAlarmServerInitiatedClientPanicInvalidOperation);
       
   391 		break;
       
   392 		}
       
   393 		
       
   394 	return completeMessage;
       
   395 	}
       
   396 
       
   397 
       
   398 //
       
   399 //
       
   400 //
       
   401 
       
   402 
       
   403 //*************************************************************************************
       
   404 TBool CASSrvSession::CmdAlarmAddL()
       
   405 	{
       
   406 	// want write access to alarm data
       
   407 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   408 
       
   409 	// Read the client specified alarm object
       
   410 	TPckg<TASShdAlarm> package(iAlarmSink);
       
   411 	Message().ReadL(KSlot0, package);
       
   412 
       
   413 	if (iAlarmSink.IsFloating())
       
   414 		{
       
   415 		ConvertFromLocalToUtc(iAlarmSink);			
       
   416 		}
       
   417 	
       
   418 	//Add the Client SID to the AlarmData, only in the EKA2 platform
       
   419 	iAlarmSink.SetSid(Message().SecureId());
       
   420 	// Add the alarm
       
   421 	iSessionEngine->AlarmAddL(iAlarmSink);
       
   422 
       
   423 	// Get an up to date representation of the alarm (which 
       
   424 	// we will write back to the client's address space).
       
   425 	TASSrvAlarm* newAlarm = ServerData().Queue().QueueAlarmById(iAlarmSink.Id());
       
   426 	AlarmDataAttachL(*newAlarm);
       
   427 	// Write back to the client's address space 
       
   428 	TASSrvAlarm copyAlarm(*newAlarm);
       
   429 	
       
   430 	if (copyAlarm.IsFloating())
       
   431 		{
       
   432 		ConvertFromUtcToLocal(copyAlarm);
       
   433 		}
       
   434 		
       
   435 	TPckgC<TASShdAlarm> pNewAlarm(copyAlarm);
       
   436 	
       
   437 	Message().WriteL(KSlot0, pNewAlarm);
       
   438 	//
       
   439 	return ETrue;
       
   440 	}
       
   441 
       
   442 
       
   443 //*************************************************************************************
       
   444 void CASSrvSession::RequestExpiryNotificationL(TASSrvAlarm& aAlarm)
       
   445 	{
       
   446 	TRAPD(err, aAlarm.RequestExpiryNotificationL(Message()));
       
   447 	if	(err != KErrNone)
       
   448 		{
       
   449 		// Couldn't setup the notification, so cancel the alarm
       
   450 		ServerData().Queue().DeQueueAlarm(aAlarm);
       
   451 
       
   452 		// This will complete the request
       
   453 		User::Leave(err);
       
   454 		}
       
   455 	else	
       
   456 		{
       
   457 		//Clear sound pause flag if it is set in the case that there is a snoozed alarm is waiting. 
       
   458 		//This is to fix defect WAG-5B8H7J "Wrong behaviour if create an alarm before the snoozed alarm goes off."
       
   459 		ServerData().SoundSettings().ClearSoundPauseFlag();
       
   460 
       
   461 		TASSrvAlarm copyAlarm(aAlarm);
       
   462 		
       
   463 		if(copyAlarm.IsFloating())
       
   464 			{
       
   465 			ConvertFromUtcToLocal(copyAlarm);
       
   466 			}
       
   467 		
       
   468 		TPckgC<TASShdAlarm> pNewAlarm(copyAlarm);		
       
   469 		// Write back to the client's address space
       
   470 		Message().WriteL(KSlot0, pNewAlarm);
       
   471 
       
   472 		// Set the session id
       
   473 		aAlarm.SetOriginatingSessionId(iSessionEngine->MASSrvSessionId());
       
   474 		}
       
   475 	}
       
   476 
       
   477 
       
   478 TBool CASSrvSession::CmdAlarmAddWithNotificationL()
       
   479 	{
       
   480 	// want write access to alarm data
       
   481 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   482 
       
   483 	// Read and add the alarm, and updates iAlarmSink with the newly-allocated
       
   484 	// Alarm Id.
       
   485 	TAlarmId allocatedId = ServerData().Queue().NextFreeAlarmId();
       
   486 	TPckg<TASShdAlarm> package(iAlarmSink);
       
   487 	Message().ReadL(KSlot0, package);
       
   488 	
       
   489 	if (iAlarmSink.IsFloating())
       
   490 		{
       
   491 		ConvertFromLocalToUtc(iAlarmSink);			
       
   492 		}
       
   493 	
       
   494 	//Add the Client SID to the AlarmData, only in the EKA2 platform
       
   495 	iAlarmSink.SetSid(Message().SecureId());
       
   496 	//Add the Client SID to the AlarmData
       
   497 	iSessionEngine->AlarmAddL(iAlarmSink, allocatedId);
       
   498 	// Get a handle on the alarm
       
   499 	TASSrvAlarm* newAlarm = ServerData().Queue().QueueAlarmById(iAlarmSink.Id());
       
   500 	AlarmDataAttachL(*newAlarm);	
       
   501 	RequestExpiryNotificationL(*newAlarm);
       
   502 	// Don't complete the message - it's asynchronously completed when the alarm
       
   503 	// expires, or is cancelled.
       
   504 	return EFalse;
       
   505 	}
       
   506 
       
   507 void CASSrvSession::AlarmDataAttachL(TASSrvAlarm& aAlarm)
       
   508 	{
       
   509 	const TInt dataSizeInBytes = static_cast<TInt>(Message().Int1());
       
   510 	if(dataSizeInBytes>0)
       
   511 		{
       
   512 		HBufC8* data = HBufC8::NewLC(dataSizeInBytes);
       
   513 		TPtr8 pData(data->Des());
       
   514 		Message().ReadL(KSlot2, pData);
       
   515 		// Give ownership of the data to the alarm. This will leave with 
       
   516 		// KErrInUse should the specified alarm already have attached data.
       
   517 		CleanupStack::Pop(data);
       
   518 		aAlarm.DataAttachL(data);
       
   519 		}
       
   520 	}
       
   521 
       
   522 //*************************************************************************************
       
   523 TBool CASSrvSession::CmdAlarmNotificationCancelAndDeQueueL()
       
   524 	{
       
   525 	// want write access to alarm data
       
   526 	TRAPD(err, ServerData().Queue().CheckAlarmQueueWritableL());
       
   527 	if (err != KErrNone && err != KErrLocked)
       
   528 		{
       
   529 		User::Leave(err);	
       
   530 		}
       
   531  
       
   532 
       
   533 	// Get the alarm id from the client
       
   534 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   535 	// Get the alarm from the queue
       
   536 	TASSrvAlarm* alarm = ServerData().Queue().QueueAlarmById(alarmId);
       
   537 	if (alarm)
       
   538 		{
       
   539 		if(err == KErrNone)
       
   540 			{
       
   541 			alarm->RequestExpiryNotificationComplete(KErrCancel);
       
   542 			// Dequeue the alarm
       
   543 			alarm->DeQueue();	
       
   544 			}
       
   545 		else
       
   546 			{
       
   547 			alarm->RequestExpiryNotificationComplete(KErrLocked);	
       
   548 			}
       
   549 		}
       
   550 	//
       
   551 	return ETrue;
       
   552 	}
       
   553 
       
   554 
       
   555 //*************************************************************************************
       
   556 TBool CASSrvSession::CmdGetAlarmDetailsL()
       
   557 	{
       
   558 	// Get the alarm id from the client
       
   559 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   560 
       
   561 	// Populate the alarm sink with the details
       
   562 	iSessionEngine->AlarmDetailsL(alarmId, iAlarmSink);
       
   563 
       
   564 	if (iAlarmSink.IsFloating())
       
   565 		{
       
   566 		ConvertFromUtcToLocal(iAlarmSink);			
       
   567 		}
       
   568 
       
   569 	// Write back the alarm to the client's address space
       
   570 	
       
   571 	TPckgC<TASShdAlarm> package(iAlarmSink);	
       
   572 	
       
   573 	Message().WriteL(KSlot1, package);
       
   574 	//
       
   575 	return ETrue;
       
   576 	}
       
   577 
       
   578 
       
   579 //*************************************************************************************
       
   580 TBool CASSrvSession::CmdAlarmDeleteL()
       
   581 	{
       
   582 	// want write access to alarm data
       
   583 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   584 
       
   585 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   586 	iSessionEngine->AlarmDeleteL(alarmId);
       
   587 	//
       
   588 	return ETrue;
       
   589 	}
       
   590 
       
   591 
       
   592 //*************************************************************************************
       
   593 TBool CASSrvSession::CmdGetAlarmCategoryL()
       
   594 	{
       
   595 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   596 	TPckgBuf<TAlarmCategory> category(iSessionEngine->AlarmCategoryL(alarmId));
       
   597 	Message().WriteL(KSlot1, category);
       
   598 	//
       
   599 	return ETrue;
       
   600 	}
       
   601 
       
   602 
       
   603 //*************************************************************************************
       
   604 TBool CASSrvSession::CmdGetAlarmOwnerL()
       
   605 	{
       
   606 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   607 	//
       
   608 	TFullName owner;
       
   609 	const TInt error = ServerData().SessionCollection().MASSessionCollectionAlarmOwner(alarmId, owner);
       
   610 	User::LeaveIfError(error);
       
   611 	//
       
   612 	Message().WriteL(KSlot1, owner);
       
   613 	//
       
   614 	return ETrue;
       
   615 	}
       
   616 
       
   617 
       
   618 //*************************************************************************************
       
   619 TBool CASSrvSession::CmdSetAlarmStatusL()
       
   620 	{
       
   621 	// want write access to alarm data
       
   622 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   623 
       
   624 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   625 	const TAlarmStatus status = static_cast<TAlarmStatus>(Message().Int1());
       
   626 	iSessionEngine->SetAlarmStatusL(alarmId, status);
       
   627 	//
       
   628 	return ETrue;
       
   629 	}
       
   630 
       
   631 //*************************************************************************************
       
   632 TBool CASSrvSession::CmdSetAlarmStatusForCalendarFileL()
       
   633     {
       
   634     // want write access to alarm data
       
   635     ServerData().Queue().CheckAlarmQueueWritableL();
       
   636     
       
   637     HBufC* filename(HBufC::NewLC(Message().GetDesLengthL(KSlot0)));
       
   638     TPtr fileNamePtr(filename->Des());
       
   639     Message().ReadL(KSlot0, fileNamePtr);
       
   640     
       
   641     const TAlarmStatus status = static_cast<TAlarmStatus>(Message().Int1());
       
   642     iSessionEngine->SetAlarmStatusForCalendarFileL(*filename, status);
       
   643         
       
   644     CleanupStack::PopAndDestroy(filename);
       
   645     
       
   646     return ETrue;
       
   647     }
       
   648 
       
   649 //*************************************************************************************
       
   650 TBool CASSrvSession::CmdGetAlarmStatusL()
       
   651 	{
       
   652 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   653 	TPckgBuf<TAlarmStatus> status(iSessionEngine->AlarmStatusL(alarmId));
       
   654 	Message().WriteL(KSlot1, status);
       
   655 	//
       
   656 	return ETrue;
       
   657 	}
       
   658 
       
   659 
       
   660 //*************************************************************************************
       
   661 TBool CASSrvSession::CmdSetAlarmDayOrTimedL()
       
   662 	{
       
   663 	// want write access to alarm data
       
   664 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   665 
       
   666 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   667 	const TAlarmDayOrTimed dayOrTimed = static_cast<TAlarmDayOrTimed>(Message().Int1());
       
   668 	iSessionEngine->SetAlarmDayOrTimedL(alarmId, dayOrTimed);
       
   669 	//
       
   670 	return ETrue;
       
   671 	}
       
   672 
       
   673 
       
   674 //*************************************************************************************
       
   675 TBool CASSrvSession::CmdGetAlarmDayOrTimedL()
       
   676 	{
       
   677 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   678 	TPckgBuf<TAlarmDayOrTimed> dayOrTimed(iSessionEngine->AlarmDayOrTimedL(alarmId));
       
   679 	Message().WriteL(KSlot1, dayOrTimed);
       
   680 	//
       
   681 	return ETrue;
       
   682 	}
       
   683 
       
   684 //*************************************************************************************
       
   685 TBool CASSrvSession::CmdGetAlarmCharacteristicsL()
       
   686 	{
       
   687 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   688 	TPckgBuf<TAlarmCharacteristicsFlags> flags(iSessionEngine->AlarmCharacteristicsL(alarmId));
       
   689 	Message().WriteL(KSlot1, flags);
       
   690 	//
       
   691 	return ETrue;
       
   692 	}
       
   693 
       
   694 
       
   695 //*************************************************************************************
       
   696 TBool CASSrvSession::CmdSetAlarmCharacteristicsL()
       
   697 	{
       
   698 	// want write access to alarm data
       
   699 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   700 
       
   701 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   702 	//
       
   703 	TAlarmCharacteristicsFlags flags;
       
   704 	TPckg<TAlarmCharacteristicsFlags> pFlags(flags);
       
   705 	Message().ReadL(KSlot1, pFlags);
       
   706 	//
       
   707 	iSessionEngine->SetAlarmCharacteristicsL(alarmId, flags);
       
   708 	//
       
   709 	return ETrue;
       
   710 	}
       
   711 
       
   712 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   713 TBool CASSrvSession::CmdSetWakeupL()
       
   714 	{
       
   715 	// want write access to alarm data
       
   716 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   717 
       
   718 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   719 	const TBool enable = static_cast<TBool>(Message().Int1());
       
   720 	iSessionEngine->SetWakeupL(alarmId, enable);
       
   721 	//
       
   722 	return ETrue;
       
   723 	}
       
   724 #endif
       
   725 
       
   726 #ifdef SYMBIAN_ALARM_REPEAT_EXTENSIONS
       
   727 TBool CASSrvSession::CmdGetAlarmDaysL()
       
   728 	{
       
   729 	const TAlarmId KAlarmId = static_cast<TAlarmId>(Message().Int0());
       
   730 	TPckgBuf<TUint8> alarmDays = iSessionEngine->AlarmDaysL(KAlarmId);
       
   731 	Message().WriteL(KSlot1, alarmDays);
       
   732 
       
   733 	return ETrue;
       
   734 	}
       
   735 
       
   736 TBool CASSrvSession::CmdSetAlarmDaysL()
       
   737 	{
       
   738 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   739 
       
   740 	const TAlarmId KAlarmId = static_cast<TAlarmId>(Message().Int0());
       
   741 	const TUint8 KAlarmDays = static_cast<TUint8>(Message().Int1());
       
   742 	iSessionEngine->SetAlarmDaysL(KAlarmId, KAlarmDays);
       
   743 
       
   744 	return ETrue;
       
   745 	}
       
   746 
       
   747 TBool CASSrvSession::CmdSetContinuousL()
       
   748 	{
       
   749 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   750 	
       
   751 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   752 	const TBool enable = static_cast<TBool>(Message().Int1());
       
   753 	iSessionEngine->SetContinuousL(alarmId, enable);
       
   754 	
       
   755 	return ETrue;
       
   756 	}
       
   757 
       
   758 TBool CASSrvSession::CmdContinuousL()
       
   759 	{
       
   760 	const TAlarmId KAlarmId = static_cast<TAlarmId>(Message().Int0());
       
   761 	TPckgBuf<TBool> continuous = iSessionEngine->ContinuousL(KAlarmId);
       
   762 	Message().WriteL(KSlot1, continuous);
       
   763 	
       
   764 	return ETrue;
       
   765 	}
       
   766 #endif
       
   767 
       
   768 TBool CASSrvSession::CmdOrphanAlarmL()
       
   769 	{
       
   770 	// want write access to alarm data
       
   771 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   772 
       
   773 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   774 	iSessionEngine->SetAlarmOrphanedL(alarmId);
       
   775 	return ETrue;
       
   776 	}
       
   777 
       
   778 
       
   779 //*************************************************************************************
       
   780 TBool CASSrvSession::CmdSetClientDataL()
       
   781 	{
       
   782 	TPckg<TASShdAlarm> package(iAlarmSink);
       
   783 	Message().ReadL(KSlot1, package);
       
   784 
       
   785   	TASSrvAlarm& alarm = ServerData().Queue().QueueAlarmByIdL(iAlarmSink.Id());
       
   786   	alarm.ClientData1() = iAlarmSink.ClientData1();
       
   787   	alarm.ClientData2() = iAlarmSink.ClientData2();
       
   788   	TBitFlags16 tempFlags = alarm.ClientFlags();
       
   789   	tempFlags.SetValue(iAlarmSink.ClientFlags().Value());
       
   790   	alarm.ClientFlags() = tempFlags;
       
   791 	
       
   792 	return ETrue;
       
   793 	}
       
   794 
       
   795 
       
   796 //
       
   797 //
       
   798 //
       
   799 
       
   800 
       
   801 //*************************************************************************************
       
   802 TBool CASSrvSession::CmdAlarmDataAttachL()
       
   803 	{
       
   804 	// want write access to alarm data
       
   805 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   806 
       
   807 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   808 	const TInt dataSizeInBytes = static_cast<TInt>(Message().Int1());
       
   809 	//
       
   810 	HBufC8* data = HBufC8::NewLC(dataSizeInBytes);
       
   811 	TPtr8 pData(data->Des());
       
   812 	Message().ReadL(KSlot2, pData);
       
   813 	
       
   814 	// Give ownership of the data to the alarm. This will leave with 
       
   815 	// KErrInUse should the specified alarm already have attached data.
       
   816 	// Or KErrLocked if Alarm Server Backup or Restore are in progress.
       
   817 	CleanupStack::Pop(data);
       
   818 	iSessionEngine->AlarmDataAttachL(alarmId, data);
       
   819 	//
       
   820 	return ETrue;
       
   821 	}
       
   822 
       
   823 
       
   824 //*************************************************************************************
       
   825 TBool CASSrvSession::CmdAlarmDataDetachL()
       
   826 	{
       
   827 	// want write access to alarm data
       
   828 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   829 
       
   830 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   831 	//
       
   832 	// Remove the data from the pool - if the specified alarm doesn't exist
       
   833 	// within the data pool, then this will leave with KErrNotFound.
       
   834 	// Or KErrLocked if Alarm Server Backup or Restore are in progress.
       
   835 	iSessionEngine->AlarmDataDetachL(alarmId);	
       
   836 	//
       
   837 	return ETrue;
       
   838 	}
       
   839 
       
   840 
       
   841 //*************************************************************************************
       
   842 TBool CASSrvSession::CmdAlarmDataSizeL()
       
   843 	{
       
   844 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   845 	//
       
   846 	TASSrvAlarm& alarm = ServerData().Queue().QueueAlarmByIdL(alarmId);
       
   847 	Message().Complete(alarm.DataSizeL());
       
   848 	
       
   849 	// Already completed above
       
   850 	return EFalse;
       
   851 	}
       
   852 
       
   853 
       
   854 //*************************************************************************************
       
   855 TBool CASSrvSession::CmdGetAlarmDataL()
       
   856 	{
       
   857 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
   858 	//
       
   859 	TASSrvAlarm& alarm = ServerData().Queue().QueueAlarmByIdL(alarmId);
       
   860 	const TPtrC8 pData(alarm.DataL());
       
   861 	//
       
   862 	const TInt maxLength = static_cast<TInt>(Message().Int1());
       
   863 	if	(pData.Size() > maxLength)
       
   864 		{
       
   865 		ASSrvStaticUtils::PanicClient(Message(), EAlarmServerInitiatedClientPanicInsufficientRoomForAlarmData);
       
   866 		return EFalse;
       
   867 		}
       
   868 	
       
   869 	Message().WriteL(KSlot2, pData);
       
   870 	//
       
   871 	return ETrue;
       
   872 	}
       
   873 
       
   874 //
       
   875 //
       
   876 //
       
   877 
       
   878 
       
   879 //*************************************************************************************
       
   880 TBool CASSrvSession::CmdSetAlarmStatusByCategoryL()
       
   881 	{
       
   882 	// want write access to alarm data
       
   883 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   884 
       
   885 	const TAlarmCategory category = { static_cast<TInt>(Message().Int0()) };
       
   886 	const TAlarmStatus status = static_cast<TAlarmStatus>(Message().Int1());
       
   887 	//
       
   888 	iSessionEngine->SetAlarmStatusByCategoryL(category, status);
       
   889 	//
       
   890 	return ETrue;
       
   891 	}
       
   892 
       
   893 
       
   894 //*************************************************************************************
       
   895 TBool CASSrvSession::CmdGetAlarmCountForCategoryL()
       
   896 	{
       
   897 	const TAlarmCategory category = { static_cast<TInt>(Message().Int0()) };
       
   898 	const TInt countForCategory = iSessionEngine->AlarmCountByCategory(category);
       
   899 	//
       
   900 	TPckg<TInt> pCount(countForCategory);
       
   901 	Message().WriteL(KSlot1, pCount);
       
   902 	//
       
   903 	return ETrue;
       
   904 	}
       
   905 
       
   906 
       
   907 //*************************************************************************************
       
   908 TBool CASSrvSession::CmdAlarmDeleteAllByCategoryL()
       
   909 	{
       
   910 	// want write access to alarm data
       
   911 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   912 
       
   913 	const TAlarmCategory category = { static_cast<TInt>(Message().Int0()) };
       
   914 	const TBool restrictToOrphanedAlarms = static_cast<TBool>(Message().Int1());
       
   915 	iSessionEngine->DeleteAllAlarmsByCategoryL(category, restrictToOrphanedAlarms);
       
   916 	//
       
   917 	return ETrue;
       
   918 	}
       
   919 
       
   920 //*************************************************************************************
       
   921 TBool CASSrvSession::CmdAlarmDeleteByCalendarFileL()
       
   922     {
       
   923     // want write access to alarm data
       
   924     ServerData().Queue().CheckAlarmQueueWritableL();
       
   925     
       
   926     HBufC* filename(HBufC::NewLC(Message().GetDesLengthL(KSlot0)));
       
   927     TPtr fileNamePtr(filename->Des());
       
   928     Message().ReadL(KSlot0, fileNamePtr);
       
   929 
       
   930     const TDeleteType whatToDelete = static_cast<TInt>(Message().Int1());
       
   931     iSessionEngine->DeleteAllAlarmsByCalendarFileL(*filename, whatToDelete);
       
   932     
       
   933     CleanupStack::PopAndDestroy(filename);
       
   934 
       
   935     return ETrue;
       
   936     }
       
   937 
       
   938 //*************************************************************************************
       
   939 TBool CASSrvSession::CmdAlarmDeleteByCategoryL()
       
   940 	{
       
   941 	// want write access to alarm data
       
   942 	ServerData().Queue().CheckAlarmQueueWritableL();
       
   943 
       
   944 	const TAlarmCategory category = { static_cast<TInt>(Message().Int0()) };
       
   945 	const TDeleteType whatToDelete = static_cast<TInt>(Message().Int1());
       
   946 	iSessionEngine->DeleteAllAlarmsByCategoryL(category, EFalse, whatToDelete);
       
   947 	//
       
   948 	return ETrue;
       
   949 	}
       
   950 
       
   951 //*************************************************************************************
       
   952 TBool CASSrvSession::CmdGetAvailableCategoryListL()
       
   953 	{
       
   954 	// Get a list of available categories
       
   955 	RArray<TAlarmCategory>* list = iSessionEngine->AlarmCategoryListLC();
       
   956 
       
   957 	// Stream them out to a buffer
       
   958 	const TInt count = list->Count();
       
   959 	TransferBuffer().Reset();
       
   960 	RBufWriteStream stream(TransferBuffer());
       
   961 	CleanupClosePushL(stream); // don't think this is needed but adding it just to be sure
       
   962 	
       
   963 	// Externalize the count, since this is needed client-side
       
   964 	stream.WriteInt32L(count);
       
   965 	
       
   966 	// Now externalize the alarm categories, one by one.
       
   967 	for(TInt i=0; i<count; i++)
       
   968 		{
       
   969 		stream << (*list)[i];
       
   970 		}
       
   971 	stream.CommitL();
       
   972 	CleanupStack::Pop(&stream);
       
   973 
       
   974 	// Finished with the list now
       
   975 	CleanupStack::PopAndDestroy(list);
       
   976 
       
   977 	// Write the size of the list back to the client
       
   978 	TPckgBuf<TInt> size(TransferBuffer().Size());
       
   979 	Message().WriteL(KSlot0, size);
       
   980 
       
   981 	// Client will now fetch the contents of the transfer buffer in
       
   982 	// another request.
       
   983 	return ETrue;
       
   984 	}
       
   985 
       
   986 
       
   987 //*************************************************************************************
       
   988 TBool CASSrvSession::CmdGetAlarmIdListForCategoryL()
       
   989 	{
       
   990 	// Get a list of available categories
       
   991 	const TAlarmCategory category = { static_cast<TInt>(Message().Int0()) };
       
   992 	RArray<TAlarmId>* list = iSessionEngine->AlarmIdListByCategoryLC(category);
       
   993 
       
   994 	// Stream to transfer buffer
       
   995 	StreamAlarmIdsToTransferBufferL(*list);
       
   996 	CleanupStack::PopAndDestroy(list);
       
   997 
       
   998 	// Client will now fetch the contents of the transfer buffer in
       
   999 	// another request.
       
  1000 	// coverity [leaked_storage]
       
  1001 	return ETrue;
       
  1002 	}
       
  1003 
       
  1004 
       
  1005 //
       
  1006 //
       
  1007 //
       
  1008 
       
  1009 
       
  1010 //*************************************************************************************
       
  1011 TBool CASSrvSession::CmdAlarmCountByStateL()
       
  1012 	{
       
  1013 	const TAlarmState state = static_cast<TAlarmState>(Message().Int0());
       
  1014 	const TInt count = iSessionEngine->AlarmCountByState(state);
       
  1015 	//
       
  1016 	TPckg<TInt> pCount(count);
       
  1017 	Message().WriteL(KSlot1, pCount);
       
  1018 	//
       
  1019 	return ETrue;
       
  1020 	}
       
  1021 
       
  1022 
       
  1023 //*************************************************************************************
       
  1024 TBool CASSrvSession::CmdGetAlarmIdListByStateL()
       
  1025 	{
       
  1026 	// Get a list of available categories
       
  1027 	const TAlarmState state = static_cast<TAlarmState>(Message().Int0());
       
  1028 	RArray<TAlarmId>* list = iSessionEngine->AlarmIdListByStateLC(state);
       
  1029 
       
  1030 	// Stream to transfer buffer
       
  1031 	StreamAlarmIdsToTransferBufferL(*list);
       
  1032 	CleanupStack::PopAndDestroy(list);
       
  1033 
       
  1034 	// Client will now fetch the contents of the transfer buffer in
       
  1035 	// another request.
       
  1036 	// coverity [leaked_storage]
       
  1037 	return ETrue;
       
  1038 	}
       
  1039 
       
  1040 
       
  1041 //*************************************************************************************
       
  1042 TBool CASSrvSession::CmdGetAlarmIdListL()
       
  1043 	{
       
  1044 	// Get a list of available categories
       
  1045 	RArray<TAlarmId>* list = iSessionEngine->AlarmIdListLC();
       
  1046 
       
  1047 	// Stream to transfer buffer
       
  1048 	StreamAlarmIdsToTransferBufferL(*list);
       
  1049 	CleanupStack::PopAndDestroy(list);
       
  1050 
       
  1051 	// Client will now fetch the contents of the transfer buffer in
       
  1052 	// another request.
       
  1053 	// coverity [leaked_storage]
       
  1054 	return ETrue;
       
  1055 	}
       
  1056 
       
  1057 
       
  1058 //*************************************************************************************
       
  1059 TBool CASSrvSession::CmdGetNextDueAlarmIdL()
       
  1060 	{
       
  1061 	// Fetch the head item from the queue. If there isn't one, then we return KNullAlarmID
       
  1062 	const TAlarmId id = ServerData().Timer().NextDueAlarmId();
       
  1063 	TPckgC<TAlarmId> nextDueAlarm(id);
       
  1064 	Message().WriteL(KSlot0, nextDueAlarm);
       
  1065 	//
       
  1066 	return ETrue;
       
  1067 	}
       
  1068 
       
  1069 
       
  1070 //*************************************************************************************
       
  1071 TBool CASSrvSession::CmdNumberOfActiveAlarmsInQueueL()
       
  1072 	{
       
  1073 	const TInt count = iSessionEngine->NumberOfActiveAlarmsInQueue();
       
  1074 	TPckgC<TInt> pCount(count);
       
  1075 	Message().WriteL(KSlot0, pCount);
       
  1076 	//
       
  1077 	return ETrue;
       
  1078 	}
       
  1079 
       
  1080 
       
  1081 //
       
  1082 //
       
  1083 //
       
  1084 
       
  1085 
       
  1086 //*************************************************************************************
       
  1087 TBool CASSrvSession::CmdSetAlarmSoundStateL()
       
  1088 	{
       
  1089 	const TAlarmGlobalSoundState state = static_cast<TAlarmGlobalSoundState>(Message().Int0());
       
  1090 	ServerData().SoundSettings().SetGlobalSoundState(state);
       
  1091 	//
       
  1092 	return ETrue;
       
  1093 	}
       
  1094 
       
  1095 
       
  1096 //*************************************************************************************
       
  1097 TBool CASSrvSession::CmdGetAlarmSoundStateL()
       
  1098 	{
       
  1099 	TPckgBuf<TAlarmGlobalSoundState> soundState(ServerData().SoundSettings().GlobalSoundState());
       
  1100 	Message().WriteL(KSlot0, soundState);
       
  1101 	//
       
  1102 	return ETrue;
       
  1103 	}
       
  1104 
       
  1105 
       
  1106 //*************************************************************************************
       
  1107 TBool CASSrvSession::CmdSetAlarmSoundsSilentUntilL()
       
  1108 	{
       
  1109 	TTime time;
       
  1110 	TPckg<TTime> pTime(time);
       
  1111 	Message().ReadL(KSlot0, pTime);
       
  1112 	//
       
  1113 	
       
  1114 	time -= User::UTCOffset(); // Value is in local time format
       
  1115 	TDateTime t = time.DateTime();
       
  1116 	ASSrvStaticUtils::RoundTimeDownToTheMinute(time);
       
  1117 	t = time.DateTime();
       
  1118 	
       
  1119 	// Can't defer time until a point in the past
       
  1120 	TTime now(ASSrvStaticUtils::UtcTimeNow());
       
  1121 	if	(time < now)
       
  1122 		{
       
  1123 		User::Leave(KErrArgument);
       
  1124 		}
       
  1125 	else
       
  1126 		{
       
  1127 		ServerData().SoundController().SetFloating();
       
  1128 		ServerData().SoundController().MakeAllSoundsQuietUntil(time);
       
  1129 	//
       
  1130 		}
       
  1131 	return ETrue;
       
  1132 	}
       
  1133 
       
  1134 
       
  1135 //*************************************************************************************
       
  1136 TBool CASSrvSession::CmdSetAlarmSoundsSilentForL()
       
  1137 	{
       
  1138 	TTimeIntervalMinutes timeIntervalMinutes;
       
  1139 	TPckg<TTimeIntervalMinutes> pTimeIntervalMinutes(timeIntervalMinutes);
       
  1140 	Message().ReadL(KSlot0, pTimeIntervalMinutes);
       
  1141 
       
  1142 	// Can't defer time until a point in the past
       
  1143 	TTime now(ASSrvStaticUtils::UtcTimeNow());	
       
  1144 	TTime time(now);
       
  1145 		
       
  1146 	time += timeIntervalMinutes;
       
  1147 	
       
  1148 	if	(time < now)
       
  1149 		{
       
  1150 		User::Leave(KErrArgument);
       
  1151 		}
       
  1152 	else
       
  1153 		{
       
  1154 		ServerData().SoundController().SetFixed();
       
  1155 		ServerData().SoundController().MakeAllSoundsQuietUntil(time);
       
  1156 		}
       
  1157 	//
       
  1158 	return ETrue;
       
  1159 	}
       
  1160 
       
  1161 
       
  1162 //*************************************************************************************
       
  1163 TBool CASSrvSession::CmdGetAlarmSoundsSilentUntilL()
       
  1164 	{
       
  1165 	if	(ServerData().SoundController().InQuietPeriod())
       
  1166 		{
       
  1167 		// Return as local time
       
  1168 		TPckgBuf<TTime> time(ServerData().SoundController().SoundsQuietUntil() + User::UTCOffset());
       
  1169 		Message().WriteL(KSlot0, time);
       
  1170 		}
       
  1171 	else
       
  1172 		{
       
  1173 		// Not in a silent period
       
  1174 		User::Leave(KErrGeneral);
       
  1175 		}
       
  1176 	//
       
  1177 	return ETrue;
       
  1178 	}
       
  1179 
       
  1180 
       
  1181 //*************************************************************************************
       
  1182 TBool CASSrvSession::CmdCancelAlarmSilenceL()
       
  1183 	{
       
  1184 	CASSrvSoundController& soundController = ServerData().SoundController();
       
  1185 	soundController.CancelSilence();
       
  1186 	//
       
  1187 	return ETrue;
       
  1188 	}
       
  1189 
       
  1190 
       
  1191 //*************************************************************************************
       
  1192 TBool CASSrvSession::CmdGetAlarmSoundsTemporarilySilencedL()
       
  1193 	{
       
  1194 	CASSrvSoundController& soundController = ServerData().SoundController();
       
  1195 	const TBool inQuietPeriod = soundController.InQuietPeriod();
       
  1196 	TPckgC<TBool> package(inQuietPeriod);
       
  1197 	Message().WriteL(KSlot0, package);
       
  1198 	//
       
  1199 	return ETrue;
       
  1200 	}
       
  1201 
       
  1202 
       
  1203 //*************************************************************************************
       
  1204 TBool CASSrvSession::CmdSetAlarmPlayIntervalsL()
       
  1205 	{
       
  1206 	// want write access to alarm data
       
  1207 	ServerData().Queue().CheckAlarmQueueWritableL();
       
  1208 
       
  1209 	// Make the transfer buffer big enough to hold the serialized 
       
  1210 	// play intervals. Int1 holds the size in bytes of the buffer required.
       
  1211 	TransferBuffer().Reset();
       
  1212 	//
       
  1213 	const TInt transferBufferSizeRequired = Message().Int1();
       
  1214 	TransferBuffer().ResizeL(transferBufferSizeRequired);
       
  1215 
       
  1216 	// Now read the buffer
       
  1217 	TPtr8 pBuffer(TransferBuffer().Ptr(0));
       
  1218 	Message().ReadL(KSlot0, pBuffer);
       
  1219 
       
  1220 	// Create stream view on buffer
       
  1221 	RBufReadStream stream(TransferBuffer());
       
  1222 	CleanupClosePushL(stream);
       
  1223 
       
  1224 	// Now internalize the play interval
       
  1225 	RArray<TASSrvAlarmSoundDetails>* newIntervals = ASSrvStaticUtils::InternalizeSoundPlayIntervalsLC(stream);
       
  1226 	
       
  1227 	// Check the sound play intervals are valid
       
  1228 	const TInt error = ASSrvStaticUtils::ValidateSoundPlayIntervals(*newIntervals);
       
  1229 	User::LeaveIfError(error);
       
  1230 
       
  1231 	// They're okay, so update the sound settings
       
  1232 	const TInt setError = ServerData().SoundSettings().SetSoundIntervals(newIntervals);
       
  1233 	User::LeaveIfError(setError);
       
  1234 
       
  1235 	CleanupStack::Pop(newIntervals);
       
  1236 	CleanupStack::PopAndDestroy(&stream);
       
  1237 	//
       
  1238 	return ETrue;
       
  1239 	}
       
  1240 
       
  1241 
       
  1242 //*************************************************************************************
       
  1243 TBool CASSrvSession::CmdGetAlarmPlayIntervalsL()
       
  1244 	{
       
  1245 	// Externalize all the sound play intervals to the transfer buffer.
       
  1246 	TransferBuffer().Reset();
       
  1247 	RBufWriteStream stream(TransferBuffer());
       
  1248 	CleanupClosePushL(stream);
       
  1249 	ServerData().SoundSettings().ExternalizeSoundIntervalsL(stream);
       
  1250 	stream.CommitL();
       
  1251 	CleanupStack::PopAndDestroy(&stream);
       
  1252 
       
  1253 	// Write the size of the list back to the client
       
  1254 	TPckgBuf<TInt> size(TransferBuffer().Size());
       
  1255 	Message().WriteL(KSlot0, size);
       
  1256 	//
       
  1257 	return ETrue;
       
  1258 	}
       
  1259 
       
  1260 //
       
  1261 //
       
  1262 //
       
  1263 
       
  1264 
       
  1265 //*************************************************************************************
       
  1266 TBool CASSrvSession::CmdNotifyChangeL()
       
  1267 	{
       
  1268 	if	(iFlags.IsSet(EAlarmSessionFlagsNotifyPending))
       
  1269 		{
       
  1270 		ASSrvStaticUtils::PanicClient(Message(), EAlarmServerInitiatedClientPanicChangeNotificationAlreadyOutstanding);
       
  1271 		return EFalse;
       
  1272 		}
       
  1273 
       
  1274 	// If we have something in the buffer, notify the client straight away
       
  1275 	if	(iChangeEventBuffer.Count())
       
  1276 		{
       
  1277 		// Oldest event is at the head of the queue
       
  1278 		const TASSrvBufferredEvent& event = iChangeEventBuffer[0];
       
  1279 
       
  1280 		// BC: Modification to maintain BC with older version of alarm server
       
  1281 		// the new alarm server must not write to the client because there is nothing in the 
       
  1282 		// client address space to write to
       
  1283 		if(Message().Int1() == EFalse)
       
  1284 			{
       
  1285 			TPckgC<TAlarmId> package(event.AlarmId());
       
  1286 			Message().WriteL(KSlot0, package);
       
  1287 			}
       
  1288 		Message().Complete(event.Code());
       
  1289 		iChangeEventBuffer.Remove(0);
       
  1290 		}
       
  1291 	else
       
  1292 		{
       
  1293 		// Wait for something
       
  1294 		iFlags.Set(EAlarmSessionFlagsNotifyPending);
       
  1295 		iAlarmQueueChangeNotificationMessage = Message();
       
  1296 		}
       
  1297 	//
       
  1298 	return EFalse;
       
  1299 	}
       
  1300 
       
  1301 
       
  1302 //*************************************************************************************
       
  1303 TBool CASSrvSession::CmdNotifyChangeCancelL()
       
  1304 	{
       
  1305 	// Can't panic the client if there isn't a pending request,
       
  1306 	// because its possible for the client thread to request
       
  1307 	// cancellation (when it still has an outstanding request)
       
  1308 	// but the alarm server thread runs because an event occurs,
       
  1309 	// hence the scheduler doesn't return to idle (to service
       
  1310 	// the cancellation request) until after an event has just
       
  1311 	// been completed. The TT_AMOD test code from TimeW exhibits
       
  1312 	// this problem.
       
  1313 	if	(iFlags.IsSet(EAlarmSessionFlagsNotifyPending))
       
  1314 		CompleteChangeNotificationMessage(KErrCancel, KNullAlarmId);
       
  1315 	return ETrue;
       
  1316 	}
       
  1317 
       
  1318 //
       
  1319 //
       
  1320 //
       
  1321 
       
  1322 
       
  1323 //*************************************************************************************
       
  1324 TBool CASSrvSession::CmdDbgShutDownServerL()
       
  1325 	{
       
  1326 #ifdef _DEBUG
       
  1327 	CActiveScheduler::Stop();
       
  1328 #endif
       
  1329 	return ETrue;
       
  1330 	}
       
  1331 
       
  1332 
       
  1333 //*************************************************************************************
       
  1334 TBool CASSrvSession::CmdDbgFailAllocL()
       
  1335 	{
       
  1336 #ifdef _DEBUG
       
  1337 	const TInt count = Message().Int0();
       
  1338 	if	(count)
       
  1339 		{
       
  1340 		__UHEAP_FAILNEXT(count);
       
  1341 		}
       
  1342 	else
       
  1343 		__UHEAP_RESET;
       
  1344 #endif
       
  1345 	return ETrue;
       
  1346 	}
       
  1347 
       
  1348 
       
  1349 //*************************************************************************************
       
  1350 TBool CASSrvSession::CmdDbgPreventUserNotifyL()
       
  1351 	{
       
  1352 #ifdef _DEBUG
       
  1353 	const TBool preventUserNotifications = static_cast<TBool>(Message().Int0());
       
  1354 	ServerData().NotificationCoordinator().__DbgPreventUserNotify(preventUserNotifications);
       
  1355 #endif
       
  1356 	return ETrue;
       
  1357 	}
       
  1358 
       
  1359 
       
  1360 //*************************************************************************************
       
  1361 TBool CASSrvSession::CmdDbgSnoozeAlarmL()
       
  1362 	{
       
  1363 #ifdef _DEBUG
       
  1364 	const TAlarmId alarmId = static_cast<TAlarmId>(Message().Int0());
       
  1365 	//
       
  1366 	TTime timeWhenAlarmShouldAwaken;
       
  1367 	TPckg<TTime> package(timeWhenAlarmShouldAwaken);
       
  1368 	Message().ReadL(KSlot1, package);
       
  1369 	//
       
  1370 
       
  1371 	TASSrvAlarm& alarm = ServerData().Queue().QueueAlarmByIdL(alarmId);
       
  1372 	alarm.Snooze(timeWhenAlarmShouldAwaken);
       
  1373 #endif
       
  1374 	return ETrue;
       
  1375 	}
       
  1376 //*************************************************************************************
       
  1377 
       
  1378 TBool CASSrvSession::CmdDbgSetEnvChgHandling()
       
  1379 	{
       
  1380 #ifdef _DEBUG
       
  1381 	TInt val = Message().Int0();
       
  1382 	ServerData().EnvironmentChangeManager().SetEnvironmentChangesHandling(val);
       
  1383 #endif
       
  1384 	return ETrue;
       
  1385 	}
       
  1386 //
       
  1387 //
       
  1388 //
       
  1389 
       
  1390 
       
  1391 //*************************************************************************************
       
  1392 TBool CASSrvSession::CmdInfoAlarmCountL()
       
  1393 	{
       
  1394 	TPckg<TInt> pCount(ServerData().Queue().QueueAlarmCount());
       
  1395 	Message().WriteL(KSlot0, pCount);
       
  1396 	//
       
  1397 	return ETrue;
       
  1398 	}
       
  1399 
       
  1400 
       
  1401 //*************************************************************************************
       
  1402 TBool CASSrvSession::CmdInfoAlarmByIndexL()
       
  1403 	{
       
  1404 	CASSrvAlarmQueue& queue = ServerData().Queue();
       
  1405 
       
  1406 	// Get the alarm index from the client
       
  1407 	const TInt count = queue.QueueAlarmCount();
       
  1408 	const TInt index = Message().Int0();
       
  1409 	if	(index < count)
       
  1410 		{
       
  1411 		// Populate the alarm sink with the details
       
  1412 		const TASShdAlarm& alarm = queue.QueueAlarmAt(index);
       
  1413 
       
  1414 		// Write back the alarm to the client's address space
       
  1415 		TPckgC<TASShdAlarm> package(alarm);
       
  1416 		Message().WriteL(KSlot1, package);
       
  1417 		}
       
  1418 	else
       
  1419 		User::Leave(KErrNotFound);
       
  1420 	//
       
  1421 	return ETrue;
       
  1422 	}
       
  1423 
       
  1424 
       
  1425 //
       
  1426 //
       
  1427 //
       
  1428 
       
  1429 
       
  1430 //*************************************************************************************
       
  1431 TBool CASSrvSession::CmdFetchTransferBufferL()
       
  1432 	{
       
  1433 	TPtr8 pBuffer(iTransferBuffer->Ptr(0));
       
  1434 	Message().WriteL(KSlot0, pBuffer);
       
  1435 	//
       
  1436 	return ETrue;
       
  1437 	}
       
  1438 
       
  1439 
       
  1440 //*************************************************************************************
       
  1441 /**
       
  1442  * Externalize the specified array to the transfer buffer
       
  1443  */
       
  1444 void CASSrvSession::StreamAlarmIdsToTransferBufferL(RArray<TAlarmId>& aArray)
       
  1445 	{
       
  1446 	// Stream them out to a buffer
       
  1447 	const TInt count = aArray.Count();
       
  1448 	TransferBuffer().Reset();
       
  1449 	RBufWriteStream stream(TransferBuffer());
       
  1450 	CleanupClosePushL(stream); // don't think this is needed but adding it just to be sure
       
  1451 	
       
  1452 	// Externalize the count, since this is needed client-side
       
  1453 	stream.WriteInt32L(count);
       
  1454 	
       
  1455 	// Now externalize the alarm categories, one by one.
       
  1456 	for(TInt i=0; i<count; i++)
       
  1457 		{
       
  1458 		stream.WriteInt32L(aArray[i]);
       
  1459 		}
       
  1460 	stream.CommitL();
       
  1461 	CleanupStack::Pop(&stream);
       
  1462 
       
  1463 	// Write the size of the list back to the client
       
  1464 	TPckgBuf<TInt> size(TransferBuffer().Size());
       
  1465 	Message().WriteL(KSlot1, size);
       
  1466 	}
       
  1467 
       
  1468 
       
  1469 //*************************************************************************************
       
  1470 /**
       
  1471  * If there isn't memory to buffer an event then we just overwrite the 
       
  1472  * head event.
       
  1473  */
       
  1474 void CASSrvSession::CompleteChangeNotificationMessage(TInt aCompletionCode, TAlarmId aAlarmId)
       
  1475 	{
       
  1476 	// If the client hasn't set up a pending request then we buffer the event.
       
  1477 	// Events are always added to the end of the buffer. The oldest event is
       
  1478 	// at the head.
       
  1479 	if	(!iFlags.IsSet(EAlarmSessionFlagsNotifyPending))
       
  1480 		{
       
  1481 		// Add event to buffer, replacing the oldest if the buffer is full.
       
  1482 		TInt count = iChangeEventBuffer.Count();
       
  1483 		if	(count == KAlarmServerMaxNumberOfChangeEventsToBuffer)
       
  1484 			{
       
  1485 			// Delete the oldest event
       
  1486 			--count;
       
  1487 			iChangeEventBuffer.Remove(0);
       
  1488 			}
       
  1489 
       
  1490 		// Try and insert. If there isn't memory to buffer the event then
       
  1491 		// we replace the most recent event with this new one.
       
  1492 		const TInt error = iChangeEventBuffer.Append(TASSrvBufferredEvent(aCompletionCode, aAlarmId));
       
  1493 		if	(error != KErrNone && count)
       
  1494 			{
       
  1495 			TASSrvBufferredEvent& event = iChangeEventBuffer[count - 1];
       
  1496 			event = TASSrvBufferredEvent(aCompletionCode, aAlarmId);
       
  1497 			}
       
  1498 		}
       
  1499 	else
       
  1500 		{
       
  1501 		// Report event straight away
       
  1502 		iFlags.Clear(EAlarmSessionFlagsNotifyPending);
       
  1503 
       
  1504 		// BC: Modification to enable BC with old alarm server; 
       
  1505 		// this is so that the server does not write to the client address space;
       
  1506 		// the old API does not include a member variable that the new alarm server
       
  1507 		// assumes in the client and so it is important that the server does not write
       
  1508 		// to this address in the old client.
       
  1509 		if(iAlarmQueueChangeNotificationMessage.Int1() != EFalse)
       
  1510 			{
       
  1511 			iAlarmQueueChangeNotificationMessage.Complete(aCompletionCode);
       
  1512 			}
       
  1513 		else
       
  1514 			{
       
  1515 			// Write to client's address space
       
  1516 			TPckgC<TAlarmId> package(aAlarmId);
       
  1517 			TRAPD(error, iAlarmQueueChangeNotificationMessage.WriteL(0, package));
       
  1518 			if	(error != KErrNone && error == KErrBadDescriptor)
       
  1519 				ASSrvStaticUtils::PanicClient(iAlarmQueueChangeNotificationMessage, EAlarmServerInitiatedClientPanicBadDescriptor);
       
  1520 			else
       
  1521 				iAlarmQueueChangeNotificationMessage.Complete(aCompletionCode);
       
  1522 			}
       
  1523 		}
       
  1524 	}
       
  1525 
       
  1526 /**
       
  1527 Converts a UTC alarm to local alarm
       
  1528 */
       
  1529 void CASSrvSession::ConvertFromUtcToLocal(TASShdAlarm& aAlarm) const
       
  1530     {
       
  1531 	// converting from UTC to local.
       
  1532     TTimeIntervalSeconds cachedoffset = ServerData().CachedUtcOffset();
       
  1533 
       
  1534 	if (aAlarm.NextDueTime() != Time::NullTTime())
       
  1535 	    {
       
  1536 	    aAlarm.NextDueTime() += cachedoffset;
       
  1537 	    }
       
  1538 	if (aAlarm.OriginalExpiryTime() != Time::NullTTime())
       
  1539 	    {
       
  1540 	    aAlarm.OriginalExpiryTime() += cachedoffset;
       
  1541 	    } 
       
  1542 	}
       
  1543 
       
  1544 /**
       
  1545 Converts a local alarm to UTC alarm
       
  1546 */
       
  1547 void CASSrvSession::ConvertFromLocalToUtc(TASShdAlarm& aAlarm) const
       
  1548     {
       
  1549 	// converting times from local to UTC.
       
  1550     TTimeIntervalSeconds cachedoffset = ServerData().CachedUtcOffset();
       
  1551 
       
  1552 	if (aAlarm.NextDueTime() != Time::NullTTime())
       
  1553 	    {
       
  1554 	    aAlarm.NextDueTime() -= cachedoffset;
       
  1555 	    }	
       
  1556 	if (aAlarm.OriginalExpiryTime() != Time::NullTTime())
       
  1557 	    {
       
  1558 	    aAlarm.OriginalExpiryTime() -= cachedoffset;
       
  1559 	    }
       
  1560 	}
       
  1561