sensorservices/sensorserver/src/server/sensrvchannel.cpp
changeset 0 4e1aa6a622a0
child 21 ccb4f6b3db21
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2006-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Sensor server channel implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <centralrepository.h>
       
    21 #include "sensrvdefines.h"
       
    22 #include "senserverchannel.h"
       
    23 #include "sensrvchannelbuffer.h"
       
    24 #include "sensrvtransaction.h"
       
    25 #include "sensrvclientserver.h"
       
    26 #include "sensrvpluginproxy.h"
       
    27 #include "sensrvtransactionqueue.h"
       
    28 #include "sensrvssymediator.h"
       
    29 #include "sensrvsession.h"
       
    30 #include "sensrvchannellistener.h"
       
    31 #include "sensrvservermediator.h"
       
    32 #include "sensrvtransactionmonitor.h"
       
    33 #include "sensrvproxymanager.h"
       
    34 #include "sensrvpropertyqueue.h"
       
    35 #include "sensrvconditionevaluator.h"
       
    36 #include "sensrvprivatecrkeys.h"
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // 2-phase constructor
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CSensrvChannel* CSensrvChannel::NewL( const TSensrvResourceChannelInfo& aInfo,
       
    43 									  CSensrvPluginProxy& aProxy )
       
    44     {
       
    45     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::NewL(aInfo.iChannelId: %d, aProxy: 0x%x)" ), aInfo.iChannelId, aProxy.ImplementationUid().iUid ) );
       
    46 
       
    47     CSensrvChannel* self = new( ELeave ) CSensrvChannel( aInfo, aProxy );
       
    48 
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop( self );
       
    52 
       
    53     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::NewL - return 0x%x" ), self ) );
       
    54 
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // C++ constructor
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CSensrvChannel::CSensrvChannel( const TSensrvChannelInfo& aInfo,
       
    63                                 CSensrvPluginProxy& aProxy )
       
    64     : iId( aInfo.iChannelId ),
       
    65       iState( EChannelStateClosed ),
       
    66       iProxy( aProxy ),
       
    67       iChannelInfo( aInfo ),
       
    68       iHighestListenerPriority( KMinTInt )
       
    69     {
       
    70     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CSensrvChannel()" ) ) );
       
    71 
       
    72     // Nothing to do
       
    73 
       
    74     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CSensrvChannel - return" ) ) );
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // 2nd phase of construction
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 void CSensrvChannel::ConstructL()
       
    82     {
       
    83     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ConstructL()" ) ) );
       
    84 
       
    85     iChannelQueue = CSensrvTransactionQueue::NewL( ETrue );
       
    86 
       
    87     iChannelInfo.iChannelGroup = TSensrvResourceChannelInfo::ESensrvChannelGroupNotSolved;
       
    88 
       
    89     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ConstructL - return" ) ) );
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // Destructor
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 CSensrvChannel::~CSensrvChannel()
       
    97     {
       
    98     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::~CSensrvChannel()" ) ) );
       
    99 
       
   100     TInt count( iListenerList.Count() );
       
   101     for ( TInt i=0; i < count; i++ )
       
   102         {
       
   103         delete iListenerList[ i ];
       
   104         }
       
   105 
       
   106     iListenerList.Reset();
       
   107 
       
   108     // Remove the first transactions in channel queue from mediator queues and
       
   109     // transaction monitor, too. Rest of the queued transactions cannot be
       
   110     // queued anywhere else, so no need to check them.
       
   111     CSensrvTransaction* firstTransaction = NULL;
       
   112     if ( iChannelQueue )
       
   113         {
       
   114         firstTransaction = iChannelQueue->First();
       
   115         }
       
   116 
       
   117     if ( iProxy.ServerMediator() )
       
   118         {
       
   119         iProxy.ServerMediator()->RemoveSingleTransaction( firstTransaction );
       
   120         }
       
   121     if ( iProxy.SsyMediator() )
       
   122         {
       
   123         iProxy.SsyMediator()->RemoveSingleTransaction( firstTransaction );
       
   124         }
       
   125     if ( iProxy.TransactionMonitor() )
       
   126         {
       
   127         iProxy.TransactionMonitor()->RemoveTransaction( firstTransaction );
       
   128         }
       
   129 
       
   130     delete iChannelQueue;
       
   131     delete iChannelBuffer;
       
   132     delete iDataAvailableTransaction;
       
   133     delete iForceBufferFilledTransaction;
       
   134     delete iForceChannelCloseTransaction;
       
   135     delete iPropertyChangedTransaction;
       
   136     delete iConditionEvaluator;
       
   137 
       
   138     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::~CSensrvChannel - return" ) ) );
       
   139     }
       
   140 
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // Handles message according to message type
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 void CSensrvChannel::DispatchMessage( CSensrvMessage& aMessage )
       
   147     {
       
   148     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::DispatchMessage(aMessage.Function(): %d)" ), aMessage.Function() ) );
       
   149 
       
   150     TInt err( KErrNone );
       
   151     CSensrvTransaction* transaction = NULL;
       
   152     CSensrvTransaction::TSensrvTransactionType transType( CSensrvTransaction::ETransTypeNone );
       
   153     
       
   154     // Check command code and call appropriate function
       
   155     switch ( aMessage.Function() )
       
   156         {
       
   157         case ESensrvSrvReqOpenChannel:
       
   158             {
       
   159             transType = CSensrvTransaction::ETransTypeOpenChannel ;
       
   160             break;
       
   161             }
       
   162 
       
   163         case ESensrvSrvReqCloseChannel:
       
   164             {
       
   165             transType = CSensrvTransaction::ETransTypeCloseChannel ;
       
   166             break;
       
   167             }
       
   168 
       
   169         case ESensrvSrvReqStartListening:
       
   170             {
       
   171             transType = CSensrvTransaction::ETransTypeStartListening ;
       
   172             break;
       
   173             }
       
   174 
       
   175         case ESensrvSrvReqStopListening:
       
   176             {
       
   177             transType = CSensrvTransaction::ETransTypeStopListening ;
       
   178             break;
       
   179             }
       
   180 
       
   181         case ESensrvSrvReqAsyncChannelData:
       
   182         case ESensrvSrvReqAsyncPropertyData:
       
   183         case ESensrvSrvReqStopPropertyListening:
       
   184         case ESensrvSrvReqAddConditionSet:
       
   185         case ESensrvSrvReqRemoveConditionSet:
       
   186         case ESensrvSrvReqAsyncConditionMet:
       
   187             {
       
   188             // Get correct listener and pass message there
       
   189             CSensrvChannelListener* listener = GetListener( aMessage.Session() );
       
   190 
       
   191             if ( listener )
       
   192                 {
       
   193                 listener->DispatchMessage( aMessage );
       
   194                 }
       
   195             else
       
   196                 {
       
   197                 err = KErrNotFound;
       
   198                 aMessage.Complete( err );
       
   199                 }
       
   200 
       
   201             break;
       
   202             }
       
   203 
       
   204         case ESensrvSrvReqGetProperty:
       
   205             {
       
   206             transType = CSensrvTransaction::ETransTypeGetProperty ;
       
   207             break;
       
   208             }
       
   209 
       
   210         case ESensrvSrvReqSetProperty:
       
   211             {
       
   212             transType = CSensrvTransaction::ETransTypeSetProperty ;
       
   213             break;
       
   214             }
       
   215 
       
   216         case ESensrvSrvReqGetAllProperties:
       
   217             {
       
   218             transType = CSensrvTransaction::ETransTypeGetAllProperties ;
       
   219             break;
       
   220             }
       
   221             
       
   222         case ESensrvSrvReqStartConditionListening:
       
   223             {
       
   224             transType = CSensrvTransaction::ETransTypeStartConditionListening ;
       
   225             break;
       
   226             }
       
   227             
       
   228         case ESensrvSrvReqStopConditionListening:
       
   229             {
       
   230             transType = CSensrvTransaction::ETransTypeStopConditionListening ;
       
   231             break;
       
   232             }
       
   233 
       
   234         // Cannot identify the message
       
   235         default:
       
   236             {
       
   237             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::DispatchMessage - ERROR: unknown command" )) );
       
   238             aMessage.Complete( KErrArgument );
       
   239             break;
       
   240             }
       
   241         }
       
   242     //Construct Transaction
       
   243     if( transType != CSensrvTransaction::ETransTypeNone )
       
   244         {
       
   245         TRAP( err, transaction = CSensrvTransaction::NewL(
       
   246                     &aMessage,
       
   247                     &iProxy,
       
   248                     this,
       
   249                     transType) );
       
   250         
       
   251         // Queue transaction
       
   252         if ( transaction && err == KErrNone )
       
   253             {
       
   254             transaction->SetState( CSensrvTransaction::ETransStateQueued );
       
   255             err = iChannelQueue->Add( transaction );
       
   256             }
       
   257         }
       
   258     // Handle error
       
   259     if ( err != KErrNone )
       
   260         {
       
   261         if ( transaction )
       
   262             {
       
   263             transaction->SetErrorCode( err );
       
   264             transaction->Complete();
       
   265             delete transaction;
       
   266             transaction = NULL;
       
   267             }
       
   268         else
       
   269             {
       
   270             // Do nothing, message gets deleted and completed with error
       
   271             // on transaction cleanup in cases where transaction creation failed.
       
   272             }
       
   273         }
       
   274     else
       
   275         {
       
   276         HandleNextTransaction();
       
   277         }
       
   278 
       
   279     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::DispatchMessage - return" ) ) );
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // Channel opened by SSY, check that providers are correctly set and
       
   284 // complete the transaction.
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CSensrvChannel::ChannelOpened(TInt aErrorCode,
       
   288                                    MSsyChannelDataProvider* aDataProvider,
       
   289                                    MSsyPropertyProvider* aSensorPropertyProvider)
       
   290     {
       
   291     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelOpened(aErrorCode: %d)" ), aErrorCode ) );
       
   292 
       
   293     TInt err( aErrorCode );
       
   294     TInt propErr( KErrNone );
       
   295 
       
   296     // If no error, set state to open, if not supported, need to solve dependent channels later
       
   297     if ( err == KErrNone || err == KErrNotSupported )
       
   298         {
       
   299         __ASSERT_ALWAYS( aDataProvider && aSensorPropertyProvider,
       
   300                          User::Panic( KSensrvPanicCategory, ESensrvPanicNullProvider ) );
       
   301         iDataProvider = aDataProvider;
       
   302         iPropertyProvider = aSensorPropertyProvider;
       
   303 
       
   304         // Get data rate for determining maximum buffering count
       
   305         TSensrvProperty property( KSensrvPropIdDataRate, KSensrvItemIndexNone, 0 );
       
   306         TRAP(propErr, iPropertyProvider->GetPropertyL( iId, property ) );
       
   307 
       
   308         if ( propErr == KErrNone )
       
   309             {
       
   310             TInt maxRate( 0 );
       
   311 
       
   312             if ( ESensrvArrayPropertyInfo == property.GetArrayIndex() )
       
   313                 {
       
   314                 //Array property
       
   315                 TInt maxValueIndex( 0 );
       
   316                 property.GetMaxValue( maxValueIndex );
       
   317                 property.SetItemIndex( KSensrvItemIndexNone ); //Initialize
       
   318                 property.SetArrayIndex( maxValueIndex ); //Set array index for filter criteria
       
   319 
       
   320                 TRAP( propErr, iPropertyProvider->GetPropertyL( iId, property ) );
       
   321                 property.GetValue( maxRate );
       
   322                 }
       
   323             else
       
   324                 {
       
   325                 property.GetMaxValue( maxRate );
       
   326                 }
       
   327 
       
   328             if ( propErr == KErrNone )
       
   329                 {
       
   330                 iMaxBufferingCount = iProxy.ProxyManager().BufferSizeMultiplier()
       
   331                                      * maxRate
       
   332                                      / KSensrvDataBufferLengthMultiplier;
       
   333 
       
   334                 // Very small buffers still need buffering count of at least one
       
   335                 if ( iMaxBufferingCount < KSensrvMinimumBufferingCount )
       
   336                     {
       
   337                     iMaxBufferingCount = KSensrvMinimumBufferingCount;
       
   338                     }
       
   339                 }
       
   340             else
       
   341                 {
       
   342                 ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelOpened - ERROR: Unable to get maximum data rate from array, using default buffer max size. Error: %d" ), propErr ) );
       
   343                 iMaxBufferingCount = KSensrvDefaultMaxBufferingCount;
       
   344                 }
       
   345             }
       
   346         else
       
   347             {
       
   348             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelOpened - ERROR: Unable to get maximum data rate, using default buffer max size. Error: %d" ), propErr ) );
       
   349             iMaxBufferingCount = KSensrvDefaultMaxBufferingCount;
       
   350             }
       
   351 
       
   352         iState = EChannelStateOpen;
       
   353         }
       
   354 
       
   355     CSensrvTransaction* transaction = iChannelQueue->First();
       
   356 
       
   357     // Unexpected callbacks from SSY mean that executing transaction has been cleaned,
       
   358     // so we should not set the error code or notify server mediator
       
   359     if (   transaction
       
   360        && transaction->Type() == CSensrvTransaction::ETransTypeOpenChannel
       
   361        && transaction->State() == CSensrvTransaction::ETransStateHandledAtSsy )
       
   362         {
       
   363         if ( err != KErrNone && err != KErrNotSupported )
       
   364             {
       
   365             transaction->SetErrorCode( err );
       
   366             }
       
   367 
       
   368         // Notify server thread that transaction is complete
       
   369         // Error can be ignored, since we are at SSY thread
       
   370         iProxy.ServerMediator()->Notify( transaction );
       
   371         }
       
   372     else
       
   373         {
       
   374         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelOpened - Transaction cleaned already" ) ) );
       
   375         }
       
   376 
       
   377 
       
   378     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelOpened - return" ) ) );
       
   379     }
       
   380 
       
   381 // ---------------------------------------------------------------------------
       
   382 // Channel closed by SSY. Cleanup channel and complete the transaction.
       
   383 // ---------------------------------------------------------------------------
       
   384 //
       
   385 void CSensrvChannel::ChannelClosed()
       
   386     {
       
   387     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelClosed()" ) ) );
       
   388 
       
   389     iState = EChannelStateClosed;
       
   390 
       
   391     CSensrvTransaction* transaction = iChannelQueue->First();
       
   392 
       
   393     // Unexpected callbacks from SSY mean transaction has been cleaned, so we should not
       
   394     // notify server mediator
       
   395     if (  transaction
       
   396        && transaction->Type()  == CSensrvTransaction::ETransTypeCloseChannel
       
   397        && transaction->State() == CSensrvTransaction::ETransStateHandledAtSsy )
       
   398         {
       
   399         // Notify server thread that transaction is complete
       
   400         // Error can be ignored, since we are at SSY thread
       
   401         iProxy.ServerMediator()->Notify( transaction );
       
   402         }
       
   403 
       
   404     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelClosed - return" ) ) );
       
   405     }
       
   406 
       
   407 // ---------------------------------------------------------------------------
       
   408 // Availability has changed. Handle it.
       
   409 // ---------------------------------------------------------------------------
       
   410 //
       
   411 void CSensrvChannel::HandleAvailabilityChange( const TSensrvProperty& aProperty )
       
   412 	{
       
   413 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange()" ) ) );
       
   414 
       
   415 	TInt propValue;
       
   416 
       
   417 	aProperty.GetValue( propValue );
       
   418 
       
   419 	HandleAvailabilityChange( static_cast<TSensrvAvailability>( propValue ) );
       
   420 
       
   421 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange - return" ) ) );
       
   422 	}
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // Availability has changed. Handle it.
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 void CSensrvChannel::HandleAvailabilityChange( const TSensrvAvailability& value )
       
   429 	{
       
   430 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange()" ) ) );
       
   431 
       
   432 	switch( value )
       
   433 		{
       
   434 		// Channel level transactions handled here
       
   435 		case ESensrvAvailabilityFalse:
       
   436 			COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange - Availability changed to ESensrvAvailabilityFalse" ) ) );
       
   437 			SetChannelUnavailable();
       
   438 			break;
       
   439 
       
   440 		case ESensrvAvailabilityTrue:
       
   441 			COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange - Availability changed to ESensrvAvailabilityTrue" ) ) );
       
   442 			SetChannelAvailable();
       
   443 			break;
       
   444 		
       
   445 		default:
       
   446 		    COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange - Incorrect value received for Availability, Value ignored" ) ) );
       
   447 		    break;
       
   448 		}
       
   449 
       
   450 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleAvailabilityChange - return" ) ) );
       
   451 	}
       
   452 
       
   453 // ---------------------------------------------------------------------------
       
   454 // Availability has changed. Handle it.
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 void CSensrvChannel::SetChannelAvailable( )
       
   458 	{
       
   459 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailable()" ) ) );
       
   460 	//start channel data
       
   461 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailable - return" ) ) );
       
   462 	}
       
   463 
       
   464 
       
   465 // ---------------------------------------------------------------------------
       
   466 // Availability has changed. Handle it.
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 void CSensrvChannel::SetChannelUnavailable( )
       
   470 	{
       
   471 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelUnavailable()" ) ) );
       
   472 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelUnavailable - return" ) ) );
       
   473 	}
       
   474 
       
   475 // ---------------------------------------------------------------------------
       
   476 // Property has changed. Store changed property to property queue.
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 TBool CSensrvChannel::StoreChangedProperty( const TSensrvProperty& aProperty,
       
   480                                             const CSensrvSession* aIgnoreSession )
       
   481     {
       
   482     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::StoreChangedProperty(aProperty.GetPropertyId() 0x%x, aIgnoreSession: 0x%x)" ), aProperty.GetPropertyId(), aIgnoreSession ) );
       
   483 
       
   484 	TBool retval( EFalse );
       
   485 	TInt propertyId = aProperty.GetPropertyId();
       
   486 
       
   487 	if ( ( propertyId == KSensrvPropIdAvailability ) )
       
   488 		{
       
   489 		COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::StoreChangedProperty - server property changed: %d" ), propertyId ) );
       
   490 
       
   491 		HandleAvailabilityChange( aProperty );
       
   492 		}
       
   493 
       
   494 	TInt i( iListenerList.Count() );
       
   495 	TInt err( KErrNone );
       
   496 
       
   497 	for (i--; i >= 0; i--)
       
   498 		{
       
   499 		if ( &( iListenerList[ i ]->Session()) != aIgnoreSession )
       
   500 			{
       
   501 			err = iListenerList[ i ]->StoreChangedProperty( aProperty );
       
   502 
       
   503 			if ( err != KErrNone )
       
   504 				{
       
   505 				// Probably OOM. Not critical to lose property change notification.
       
   506 				ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::StoreChangedProperty - ERROR: Error while adding property change to listener queue: %d" ), err ) );
       
   507 				}
       
   508 			else
       
   509 				{
       
   510 				retval = ETrue;
       
   511 				}
       
   512 			}
       
   513 		}
       
   514 
       
   515     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::StoreChangedProperty - return: %d" ), retval ) );
       
   516 
       
   517     return retval;
       
   518     }
       
   519 
       
   520 // ---------------------------------------------------------------------------
       
   521 // Handles transaction finalization and completes transaction on channels's queue.
       
   522 // ---------------------------------------------------------------------------
       
   523 //
       
   524 void CSensrvChannel::CompleteTransaction( CSensrvTransaction* aTransaction )
       
   525     {
       
   526     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction(aTransaction: 0x%x)" ), aTransaction ) );
       
   527 
       
   528     __ASSERT_ALWAYS( aTransaction, User::Panic( KSensrvPanicCategory, ESensrvPanicNullTransaction ) );
       
   529 
       
   530     TInt err = aTransaction->ErrorCode();
       
   531 
       
   532     switch( aTransaction->Type() )
       
   533         {
       
   534         // Channel level transactions handled here
       
   535         case CSensrvTransaction::ETransTypeOpenChannel:
       
   536             {
       
   537             // If opening failed at SSY, no need to create buffer
       
   538             if ( iState != EChannelStateClosed )
       
   539                 {
       
   540                 if ( !iChannelBuffer )
       
   541                     {
       
   542                     TRAP( err, iChannelBuffer =
       
   543                         CSensrvChannelBuffer::NewL( iChannelInfo.iDataItemSize,
       
   544                                                    iMaxBufferingCount * KSensrvDataBufferLengthMultiplier ) )
       
   545 
       
   546                     if ( err == KErrNone)
       
   547                         {
       
   548                         // Write maximum buffering count and data item size to message
       
   549                         CSensrvTransaction::TChannelOpenMessageData data( iMaxBufferingCount, iChannelInfo.iDataItemSize );
       
   550                         err = aTransaction->SetMessageData( &data );
       
   551                         }
       
   552                     else
       
   553                         {
       
   554                         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - ERROR: Buffer creation failed: %d" ), err ) );
       
   555                         }
       
   556                     }
       
   557                 }
       
   558 
       
   559             if ( err == KErrNone )
       
   560                 {
       
   561                 // Update indicator values on open and notify changes to clients
       
   562 
       
   563                 HandleDependencyOnOpen();
       
   564 
       
   565                 HandleSetPropertySuccessIndOnCreateOrDelete();
       
   566                 }
       
   567             else
       
   568                 {
       
   569                 // If there was error, delete the listener
       
   570                 aTransaction->SetErrorCode(err);
       
   571                 DeleteListener( aTransaction->Session() );
       
   572                 CloseIfNeeded();
       
   573                 }
       
   574             }
       
   575             break;
       
   576 
       
   577         case CSensrvTransaction::ETransTypeCloseChannel:
       
   578             {
       
   579             // If an error occurred, channel was left open, so only delete buffer
       
   580             // and condition evaluator if channel state is closed.
       
   581             if ( iState == EChannelStateClosed )
       
   582                 {
       
   583                 delete iChannelBuffer;
       
   584                 iChannelBuffer = NULL;
       
   585 
       
   586                 delete iConditionEvaluator;
       
   587                 iConditionEvaluator = NULL;
       
   588                 }
       
   589 
       
   590             // Client listener is already deleted successfully, so report ok to client
       
   591             // even if there was an error.
       
   592             aTransaction->SetErrorCode( KErrNone );
       
   593             }
       
   594             break;
       
   595 
       
   596         case CSensrvTransaction::ETransTypeStartListening:
       
   597         case CSensrvTransaction::ETransTypeStartConditionListening:
       
   598             {
       
   599             if ( err != KErrNone )
       
   600                 {
       
   601                 CSensrvChannelListener* listener = GetListener(aTransaction->Session());
       
   602 
       
   603                 // If error at SSY, stop listening
       
   604                 if ( listener )
       
   605                     {
       
   606                     CSensrvChannelListener::TSensrvChannelListenerState stopType =
       
   607                         aTransaction->Type() == CSensrvTransaction::ETransTypeStartListening ?
       
   608                              CSensrvChannelListener::EChannelListenerStateDataListening :
       
   609                              CSensrvChannelListener::EChannelListenerStateConditionListening;
       
   610 
       
   611                     // Stop listening that was started, but do not delete any
       
   612                     // added conditions, as client will expect them to be added if he retries.
       
   613                     listener->StopListening(stopType, EFalse);
       
   614                     }
       
   615                 else
       
   616                     {
       
   617                     // No listener found, we may have to close
       
   618                     CloseIfNeeded();
       
   619                     }
       
   620                 }
       
   621             else
       
   622                 {
       
   623                 iState = EChannelStateListening;
       
   624                 }
       
   625             }
       
   626             break;
       
   627 
       
   628         case CSensrvTransaction::ETransTypeStopListening:
       
   629         case CSensrvTransaction::ETransTypeStopConditionListening:
       
   630             {
       
   631             if ( err != KErrNone )
       
   632                 {
       
   633                 // If an error occurred, assume channel was left active still.
       
   634                 // However, client listener state has already been set to not-active,
       
   635                 // so report ok to client.
       
   636                 aTransaction->SetErrorCode( KErrNone );
       
   637                 }
       
   638             else
       
   639                 {
       
   640                 iState = EChannelStateOpen;
       
   641                 }
       
   642             }
       
   643             break;
       
   644 
       
   645         case CSensrvTransaction::ETransTypeMediatorNewDataAvailable:
       
   646             {
       
   647             // Remove force transaction from monitor
       
   648             if ( iForcingBufferFilled )
       
   649                 {
       
   650                 iForcingBufferFilled = EFalse;
       
   651                 iProxy.TransactionMonitor()->RemoveTransaction( iForceBufferFilledTransaction );
       
   652                 }
       
   653 
       
   654             // Notify each listener that new data is available
       
   655             TInt i( iListenerList.Count() );
       
   656             for ( i--; i >= 0; i-- )
       
   657                 {
       
   658                 err = iListenerList[ i ]->NewDataAvailable();
       
   659 
       
   660                 // Delete any listeners who cannot handle new data, as most likely the message
       
   661                 // descriptors for writing are invalid.
       
   662                 if (err != KErrNone)
       
   663                     {
       
   664                     ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - ERROR: Error while handling new data in listener, deleting... (%d)" ), err ) );
       
   665                     delete iListenerList[ i ];
       
   666                     iListenerList.Remove( i );
       
   667                     CloseIfNeeded();
       
   668                     }
       
   669                 }
       
   670             }
       
   671             break;
       
   672 
       
   673         case CSensrvTransaction::ETransTypeMediatorForceChannelClose:
       
   674             {
       
   675             // If channel is already closed, no need for this
       
   676             if ( iState != EChannelStateClosed )
       
   677                 {
       
   678                 // First transaction requires special handling, if it is already executing.
       
   679                 CSensrvTransaction* first = iChannelQueue->First();
       
   680                 TBool closeNeeded( ETrue );
       
   681 
       
   682                 if (   first
       
   683                     && first->State() != CSensrvTransaction::ETransStateQueued
       
   684                     && first->Type() != CSensrvTransaction::ETransTypeCloseChannel )
       
   685                     {
       
   686                     // There is a non-close transaction executing, so
       
   687                     // remove the transaction from mediators
       
   688                     if ( iProxy.ServerMediator() )
       
   689                         {
       
   690                         iProxy.ServerMediator()->RemoveSingleTransaction( first );
       
   691                         }
       
   692 
       
   693                     if ( iProxy.SsyMediator() )
       
   694                         {
       
   695                         iProxy.SsyMediator()->RemoveSingleTransaction( first );
       
   696                         }
       
   697                     }
       
   698                 else if (   first
       
   699                          && first->State() != CSensrvTransaction::ETransStateQueued
       
   700                          && first->Type() == CSensrvTransaction::ETransTypeCloseChannel )
       
   701                     {
       
   702                     // The first executing transaction is already a close transaction,
       
   703                     // so it is not necessary to force close anymore.
       
   704                     closeNeeded = EFalse;
       
   705                     }
       
   706                 else
       
   707                     {
       
   708                     // No executing transaction, proceed with close
       
   709                     }
       
   710 
       
   711                 // Delete listeners and their transactions if close needed
       
   712                 if (closeNeeded)
       
   713                     {
       
   714                     TInt i(iListenerList.Count());
       
   715                     for ( i--; i >= 0; i-- )
       
   716                         {
       
   717                         iChannelQueue->Remove( &( iListenerList[ i ]->Session() ) );
       
   718 
       
   719                         delete iListenerList[ i ];
       
   720                         iListenerList.Remove( i );
       
   721                         }
       
   722 
       
   723                     // Insert new channel close to beginning of the queue
       
   724                     // (Queue might still have open channel transactions in it)
       
   725                     CSensrvTransaction* closeTransaction = NULL;
       
   726                     TRAP( err, closeTransaction = CSensrvTransaction::NewL(
       
   727                           NULL,
       
   728                           &iProxy,
       
   729                           this,
       
   730                           CSensrvTransaction::ETransTypeCloseChannel ) );
       
   731 
       
   732                     if ( (err==KErrNone) && closeTransaction )
       
   733                         {
       
   734                         closeTransaction->SetState( CSensrvTransaction::ETransStateQueued );
       
   735                         err = iChannelQueue->Add( closeTransaction, EFalse );
       
   736 
       
   737                         if ( err != KErrNone )
       
   738                             {
       
   739                             delete closeTransaction;
       
   740                             closeTransaction = NULL;
       
   741                             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - ERROR: Unable to queue internal close transaction" ) ) );
       
   742                             }
       
   743                         else
       
   744                             {
       
   745                             HandleNextTransaction();
       
   746                             }
       
   747                         }
       
   748                     else
       
   749                         {
       
   750                         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - ERROR: Unable to create internal close transaction" ) ) );
       
   751                         }
       
   752                     }
       
   753                 }
       
   754             else
       
   755                 {
       
   756                 COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - Channel already closed when handling ETransTypeMediatorForceChannelClose." ) ) );
       
   757                 }
       
   758             }
       
   759             break;
       
   760 
       
   761         case CSensrvTransaction::ETransTypeGetProperty:
       
   762             {
       
   763             //Data is already written in SSY mediator
       
   764             }
       
   765             break;
       
   766 
       
   767         case CSensrvTransaction::ETransTypeGetAllProperties:
       
   768             {
       
   769             //Data is already written in SSY mediator
       
   770             }
       
   771             break;
       
   772 
       
   773         case CSensrvTransaction::ETransTypeSetProperty:
       
   774             {
       
   775             // Since SSY won't be sending the property change notification to this channel,
       
   776             // send notification to other clients than setter.
       
   777             }
       
   778             break;
       
   779 
       
   780         case CSensrvTransaction::ETransTypeMediatorPropertyChanged:
       
   781             {
       
   782             // Update indicator values on property change and notify changes to clients
       
   783             HandleSetPropertySuccessIndOnPropertyChange();
       
   784 
       
   785             // Notify each listener that new data is available
       
   786             TInt i( iListenerList.Count() );
       
   787             for ( i--; i >= 0; i-- )
       
   788                 {
       
   789                 iListenerList[i]->HandleNextProperty();
       
   790                 }
       
   791             }
       
   792             break;
       
   793 
       
   794         default:
       
   795             // Unexpected transaction type, so we do not know what to do with it
       
   796             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - ERROR: Invalid transaction type" ) ) );
       
   797             User::Panic(KSensrvPanicCategory, ESensrvPanicUnknownTransactionType);
       
   798             break;
       
   799         }
       
   800 
       
   801     // Mediator transactions do not cause further handling
       
   802     if ( !aTransaction->IsMediatorType() )
       
   803         {
       
   804         // Complete the transaction
       
   805         iProxy.TransactionMonitor()->RemoveTransaction(aTransaction);
       
   806         iChannelQueue->Remove(aTransaction, CSensrvTransactionQueue::ERemovalTypeComplete);
       
   807 
       
   808         // Execute next transaction in queue, if any
       
   809         HandleNextTransaction();
       
   810         }
       
   811 
       
   812     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CompleteTransaction - return" ) ) );
       
   813     }
       
   814 
       
   815 // ---------------------------------------------------------------------------
       
   816 // Cleans up everything related to terminated session.
       
   817 // ---------------------------------------------------------------------------
       
   818 //
       
   819 void CSensrvChannel::SessionTerminated( CSensrvSession* aSession )
       
   820     {
       
   821     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SessionTerminated(aSession: 0x%x)"),aSession ) );
       
   822 
       
   823     // Cleanup any waiting transactions for this session
       
   824     iChannelQueue->Remove(aSession);
       
   825 
       
   826     // Delete listener for session if it exists
       
   827     DeleteListener(aSession);
       
   828 
       
   829     // Close channel if channel state, transactions and listeners are not correctly
       
   830     // matched.
       
   831     CloseIfNeeded();
       
   832 
       
   833     // NOTE: Session termination for a session that has currently executing synchronous
       
   834     // client/server message is _extremely_ unlikely and will probably never happen in real devices,
       
   835     // Only plausable case that comes to mind is that client thread is paniced by some other server
       
   836     // handling some asynchronous message for that client, while the client is waiting on
       
   837     // synchronous SendReceive call to sensor server.
       
   838 
       
   839     // However, just in case, handle the next transaction. If next transaction and removed transaction
       
   840     // were both open channel or close channel transactions, the handling of next transaction might
       
   841     // cause error at SSY, depending on timing and how SSY handles simultaneous open/close to the same
       
   842     // channel. Preventing this from occuring in server code seems to be quite complex,
       
   843     // so for a case this rare it is not cost effective.
       
   844     HandleNextTransaction();
       
   845 
       
   846     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SessionTerminated - return" )) );
       
   847     }
       
   848 
       
   849 // ---------------------------------------------------------------------------
       
   850 // Handles SSY notification failure.
       
   851 // ---------------------------------------------------------------------------
       
   852 //
       
   853 void CSensrvChannel::HandleSsyNotifyFailure()
       
   854     {
       
   855     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSsyNotifyFailure()" ) ) );
       
   856 
       
   857     // Check queued transactions to determine if they are handled at SSY
       
   858     CSensrvTransaction* transaction = iChannelQueue->First();
       
   859 
       
   860     if ( transaction && transaction->State() == CSensrvTransaction::ETransStateNotifyFailed )
       
   861         {
       
   862         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSsyNotifyFailure - Notify failed transaction found" ) ) );
       
   863         transaction->SetState( CSensrvTransaction::ETransStateHandledAtSsy );
       
   864         CompleteTransaction( transaction );
       
   865         }
       
   866 
       
   867     // Also check new data available transaction
       
   868     if ( iDataAvailableTransaction && iDataAvailableTransaction->State() == CSensrvTransaction::ETransStateNotifyFailed )
       
   869         {
       
   870         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSsyNotifyFailure - Notify failed for iDataAvailableTransaction" ) ) );
       
   871         iDataAvailableTransaction->SetState( CSensrvTransaction::ETransStateHandledAtSsy );
       
   872         CompleteTransaction( iDataAvailableTransaction );
       
   873         }
       
   874 
       
   875     // Also check force channel close transaction
       
   876     if ( iForceChannelCloseTransaction && iForceChannelCloseTransaction->State() == CSensrvTransaction::ETransStateNotifyFailed )
       
   877         {
       
   878         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSsyNotifyFailure - Notify failed for iForceChannelCloseTransaction" ) ) );
       
   879         iForceChannelCloseTransaction->SetState( CSensrvTransaction::ETransStateHandledAtSsy );
       
   880         CompleteTransaction( iForceChannelCloseTransaction );
       
   881         }
       
   882 
       
   883     // Also check property change transaction
       
   884     if ( iPropertyChangedTransaction && iPropertyChangedTransaction->State() == CSensrvTransaction::ETransStateNotifyFailed )
       
   885         {
       
   886         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSsyNotifyFailure - Notify failed for iPropertyChangedTransaction" ) ) );
       
   887         iPropertyChangedTransaction->SetState( CSensrvTransaction::ETransStateHandledAtSsy );
       
   888         CompleteTransaction( iPropertyChangedTransaction );
       
   889         }
       
   890 
       
   891     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSsyNotifyFailure - return" ) ) );
       
   892     }
       
   893 
       
   894 // ---------------------------------------------------------------------------
       
   895 // Forces buffer filled at SSY.
       
   896 // ---------------------------------------------------------------------------
       
   897 //
       
   898 void CSensrvChannel::ForceBufferFilled()
       
   899     {
       
   900     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ForceBufferFilled()" ) ) );
       
   901 
       
   902     // Notify SSY thread that buffer fill must be forced.
       
   903     if (iForceBufferFilledTransaction)
       
   904         {
       
   905         if (iForcingBufferFilled)
       
   906             {
       
   907             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ForceBufferFilled - Already forcing bufferfilled" ) ) );
       
   908             }
       
   909         else
       
   910             {
       
   911             TInt err = iProxy.SsyMediator()->Notify(iForceBufferFilledTransaction);
       
   912 
       
   913             if (err == KErrNone)
       
   914                 {
       
   915                 iForcingBufferFilled = ETrue;
       
   916                 iProxy.TransactionMonitor()->AddTransaction(iForceBufferFilledTransaction);
       
   917                 }
       
   918             else
       
   919                 {
       
   920                 // Just trace, it is not critical if notifying SSY mediator fails in this case.
       
   921                 ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::ForceBufferFilled - ERROR: SSY mediator notify failed: %d" ), err ) );
       
   922                 }
       
   923             }
       
   924         }
       
   925     else
       
   926         {
       
   927         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::ForceBufferFilled - ERROR: Buffer filled transaction NULL" ) ) );
       
   928         }
       
   929 
       
   930     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ForceBufferFilled - return. iForcingBufferFilled: %d" ), iForcingBufferFilled ) );
       
   931     }
       
   932 
       
   933 // ---------------------------------------------------------------------------
       
   934 // Recalculates buffering count to match lowest need of all buffers.
       
   935 //
       
   936 // Note: Even for one client it is necessary to recalculate buffering count,
       
   937 // as buffering period caused buffer fills combined with a client that
       
   938 // occasionally doesn't get data in timely manner will need recalculation
       
   939 // after such events.
       
   940 // Trying to optimize this to skip unnecessary recalculation is quite complex,
       
   941 // and probably not that much faster than just recalculating always
       
   942 // without extra checks.
       
   943 // ---------------------------------------------------------------------------
       
   944 //
       
   945 TInt CSensrvChannel::RecalculateBufferingCount()
       
   946     {
       
   947     TInt count(iListenerList.Count());
       
   948 
       
   949     TInt previousLow(iMaxBufferingCount);
       
   950 
       
   951     // Determine lowest remaining need of each listener.
       
   952     TInt remainingCount(0);
       
   953 
       
   954     if (count)
       
   955         {
       
   956         for (TInt i=0; previousLow > KSensrvMinimumBufferingCount && i < count; i++)
       
   957             {
       
   958             if (   iListenerList[i]->State() & CSensrvChannelListener::EChannelListenerStateConditionListening
       
   959                 || iListenerList[i]->State() & CSensrvChannelListener::EChannelListenerStateDataListening )
       
   960                 {
       
   961                 remainingCount = iListenerList[i]->RemainingBufferingCount();
       
   962 
       
   963                 if (remainingCount < previousLow)
       
   964                     {
       
   965                     previousLow = remainingCount;
       
   966                     }
       
   967                 }
       
   968             }
       
   969         }
       
   970     else
       
   971         {
       
   972         // No listeners, so return zero count.
       
   973         previousLow = 0;
       
   974         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::RecalculateBufferingCount - No listeners" ) ) );
       
   975         }
       
   976 
       
   977     BUFFER_TRACE( (_L( "#### Recalculated buffering count: %d" ), previousLow) );
       
   978 
       
   979     return previousLow;
       
   980     }
       
   981 
       
   982 // ---------------------------------------------------------------------------
       
   983 // CSensrvChannel::GetAffectedClients
       
   984 //
       
   985 // Client is considered as "affected" if the channel is opened by the client.
       
   986 // When the channel is open, channel listener also exists.
       
   987 // Priorities are queried from ChannelListener.
       
   988 // ---------------------------------------------------------------------------
       
   989 //
       
   990 void CSensrvChannel::GetAffectedClients( RArray<TInt>& aAffectedClients,
       
   991                                          TSecureId aCallerSecureId  )
       
   992     {
       
   993     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetAffectedClients()" ) ) );
       
   994 
       
   995     TInt count(iListenerList.Count());
       
   996     for (TInt i=0; i < count; i++)
       
   997         {
       
   998         if (aCallerSecureId == iListenerList[i]->Session().SecureId() )
       
   999             {
       
  1000             //Filter away
       
  1001             }
       
  1002         else
       
  1003             {
       
  1004             aAffectedClients.Append( iListenerList[i]->Priority() );
       
  1005             }
       
  1006         }
       
  1007 
       
  1008     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetAffectedClients - %d client(s) found - return" ), count ) );
       
  1009     }
       
  1010 
       
  1011 // ---------------------------------------------------------------------------
       
  1012 // Creates a new condition evaluator or returns an existing one.
       
  1013 // Once the evaluator is created, it will not be destroyed until channel
       
  1014 // is closed.
       
  1015 // ---------------------------------------------------------------------------
       
  1016 //
       
  1017 CSensrvConditionEvaluator* CSensrvChannel::ConditionEvaluatorL()
       
  1018     {
       
  1019     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ConditionEvaluatorL()" ) ) );
       
  1020 
       
  1021     if (!iConditionEvaluator)
       
  1022         {
       
  1023         iConditionEvaluator = CSensrvConditionEvaluator::NewL( iChannelInfo );
       
  1024         }
       
  1025 
       
  1026     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ConditionEvaluatorL - return 0x%x" ), iConditionEvaluator ) );
       
  1027 
       
  1028     return iConditionEvaluator;
       
  1029     }
       
  1030 
       
  1031 // ---------------------------------------------------------------------------
       
  1032 // Handles next transaction
       
  1033 // ---------------------------------------------------------------------------
       
  1034 //
       
  1035 void CSensrvChannel::HandleNextTransaction()
       
  1036     {
       
  1037     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction()" ) ) );
       
  1038 
       
  1039     CSensrvTransaction* transaction = iChannelQueue->First();
       
  1040 
       
  1041     // Handle transactions until a transaction is left in executing state
       
  1042     while (transaction)
       
  1043         {
       
  1044         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - Handling transaction of type: %d" ), transaction->Type() ) );
       
  1045 
       
  1046         if (transaction->State() != CSensrvTransaction::ETransStateQueued)
       
  1047             {
       
  1048             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - Previous transaction still executing" ) ) );
       
  1049             transaction = NULL;
       
  1050             break;
       
  1051             }
       
  1052         
       
  1053         TInt err(KErrNone);
       
  1054 
       
  1055         transaction->SetState(CSensrvTransaction::ETransStateExecuting);
       
  1056 
       
  1057         TBool ssyTransaction(EFalse);
       
  1058 
       
  1059         // Check channel closed state, only open and close have special operations
       
  1060         if ( iState == EChannelStateClosed
       
  1061             && ( transaction->Type() != CSensrvTransaction::ETransTypeOpenChannel
       
  1062                  && transaction->Type() != CSensrvTransaction::ETransTypeCloseChannel ) )
       
  1063             {
       
  1064             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: Channel closed." ) ) );
       
  1065             err = KErrNotFound;
       
  1066 
       
  1067             // Since channel is closed, also delete listener, if it exists
       
  1068             DeleteListener(transaction->Session());
       
  1069             }
       
  1070         else
       
  1071             {
       
  1072             switch(transaction->Type())
       
  1073                 {
       
  1074                 ///////////////////////////////////////////////
       
  1075                 case CSensrvTransaction::ETransTypeOpenChannel:
       
  1076                     {
       
  1077                     // If session already has a listener, channel is already open
       
  1078                     // for this client, which is an error
       
  1079                     CSensrvChannelListener* listener = GetListener(transaction->Session());
       
  1080 
       
  1081                     if (listener)
       
  1082                         {
       
  1083                         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: ETransTypeOpenChannel, listener already exists." ) ) );
       
  1084                         err = KErrAlreadyExists;
       
  1085                         }
       
  1086                     else
       
  1087                         {
       
  1088                         // Add new listener for the session
       
  1089                         TRAP(err, listener = CSensrvChannelListener::NewL(*transaction->Session(), *this, iProxy));
       
  1090 
       
  1091                         if (err == KErrNone)
       
  1092                             {
       
  1093                             err = iListenerList.Append(listener);
       
  1094                             if (err == KErrNone )
       
  1095                                 {
       
  1096                                 // Quick update is allowed only when the highest has been searched earlier.
       
  1097                                 if ( iHighestListenerPriorityMultiples > 0 )
       
  1098                                     {
       
  1099                                     // Update cached highest listener priority tracking
       
  1100                                     TInt priority( listener->Priority() );
       
  1101                                     if ( priority > iHighestListenerPriority )
       
  1102                                         {
       
  1103                                         iHighestListenerPriority = priority;
       
  1104                                         iHighestListenerPriorityMultiples = 1;
       
  1105                                         }
       
  1106                                     else if ( priority == iHighestListenerPriority )
       
  1107                                         {
       
  1108                                         iHighestListenerPriorityMultiples++;
       
  1109                                         }
       
  1110                                     }
       
  1111                                 }
       
  1112                             else
       
  1113                                 {
       
  1114                                 delete listener;
       
  1115                                 listener = NULL;
       
  1116                                 ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: ETransTypeOpenChannel, listener append failed: %d." ), err ) );
       
  1117                                 }
       
  1118                             }
       
  1119                         else
       
  1120                             {
       
  1121                             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: ETransTypeOpenChannel, new listener creation failed: %d" ), err ) );
       
  1122                             }
       
  1123                         }
       
  1124 
       
  1125                     // If channel state is closed, SSY needs to handle transaction to open channel.
       
  1126                     // Also stop unloading timer if it is active.
       
  1127                     if ( err == KErrNone && iState == EChannelStateClosed )
       
  1128                         {
       
  1129                         // Stop unload timer if it happens to be active
       
  1130                         iProxy.StopUnloadTimer();
       
  1131                        
       
  1132                         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - Opening channel to SSY" ) ) );
       
  1133                         ssyTransaction = ETrue;
       
  1134                         }
       
  1135 
       
  1136                     // Write maximum buffering count and data item size to message, if buffer exists.
       
  1137                     if ( err == KErrNone && iChannelBuffer )
       
  1138                         {
       
  1139                         CSensrvTransaction::TChannelOpenMessageData data( iMaxBufferingCount, iChannelInfo.iDataItemSize );
       
  1140                         err = transaction->SetMessageData( &data );
       
  1141                         }
       
  1142 
       
  1143                     // Create mediator transactions to be used in notifying property changes.
       
  1144                     // These transactions are never deleted and are reused.
       
  1145                     if ( err == KErrNone && !iPropertyChangedTransaction )
       
  1146                         {
       
  1147                         TRAP( err, iPropertyChangedTransaction = CSensrvTransaction::NewL(
       
  1148                                    NULL,
       
  1149                                    &iProxy,
       
  1150                                    this,
       
  1151                                    CSensrvTransaction::ETransTypeMediatorPropertyChanged ) );
       
  1152 
       
  1153                         if ( err != KErrNone )
       
  1154                             {
       
  1155                             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: ETransTypeMediatorPropertyChanged transaction creation failed: %d" ), err ) );
       
  1156                             }
       
  1157                         }
       
  1158 
       
  1159                     if ( err != KErrNone)
       
  1160                         {
       
  1161                         DeleteListener(transaction->Session());
       
  1162                         }
       
  1163                     // If SSY transaction required, handle the indication when the SSY transaction completes
       
  1164                     if ( err == KErrNone && !ssyTransaction )
       
  1165                         {
       
  1166                         HandleDependencyOnOpen();
       
  1167 
       
  1168                         // Update indicator values on open and notify changes to clients
       
  1169                         HandleSetPropertySuccessIndOnCreateOrDelete();
       
  1170                         }
       
  1171                     }
       
  1172                     break;
       
  1173 
       
  1174                 ///////////////////////////////////////////////
       
  1175                 case CSensrvTransaction::ETransTypeCloseChannel:
       
  1176                     {
       
  1177                     //get the listener
       
  1178                     CSensrvChannelListener* listener = FindListener( transaction->Session() );
       
  1179 
       
  1180                     TInt priority( KMinTInt );
       
  1181 
       
  1182                     if ( listener )
       
  1183                         {
       
  1184                         priority = listener->Priority();
       
  1185                         }
       
  1186 
       
  1187                     DeleteListener( transaction->Session() );
       
  1188 
       
  1189                     HandleDependencyOnClose( priority ); //listener is gone
       
  1190 
       
  1191                     // If there are more listeners remaining, or channel already closed, do not close channel
       
  1192                     if ( iState == EChannelStateClosed )
       
  1193                         {
       
  1194                         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - Channel already closed, not closing channel to SSY" ) ) );
       
  1195                         }
       
  1196                     else if ( iListenerList.Count() > 0 )
       
  1197                         {
       
  1198                         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - More clients remaining, not closing channel to SSY" ) ) );
       
  1199                         }
       
  1200                     else
       
  1201                         {
       
  1202                         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - Closing channel to SSY" ) ) );
       
  1203                         ssyTransaction = ETrue;
       
  1204                         }
       
  1205                     // Update indicator values on close and notify changes to clients
       
  1206 
       
  1207                     HandleSetPropertySuccessIndOnCreateOrDelete();
       
  1208                     }
       
  1209                     break;
       
  1210 
       
  1211                 ///////////////////////////////////////////////
       
  1212                 case CSensrvTransaction::ETransTypeStartListening:
       
  1213                 case CSensrvTransaction::ETransTypeStartConditionListening:
       
  1214                     {
       
  1215                     err = HandleStartListeningTransaction(*transaction);
       
  1216                     ssyTransaction = ETrue;
       
  1217                     }
       
  1218                     break;
       
  1219 
       
  1220                 ///////////////////////////////////////////////
       
  1221                 case CSensrvTransaction::ETransTypeStopListening:
       
  1222                 case CSensrvTransaction::ETransTypeStopConditionListening:
       
  1223                     {
       
  1224                     HandleStopListeningTransaction(*transaction, ssyTransaction);
       
  1225                     }
       
  1226                     break;
       
  1227 
       
  1228                 ///////////////////////////////////////////////
       
  1229                 case CSensrvTransaction::ETransTypeGetProperty:
       
  1230                 case CSensrvTransaction::ETransTypeGetAllProperties:
       
  1231                 case CSensrvTransaction::ETransTypeSetProperty:
       
  1232                     {
       
  1233                     ssyTransaction = ETrue;
       
  1234                     CSensrvChannelListener* listener = GetListener(transaction->Session());
       
  1235                     if ( !listener )
       
  1236                         {
       
  1237                         //Listener doesn't exist anymore (for some reason).
       
  1238                         err = KErrNotFound;
       
  1239                         }
       
  1240                     }
       
  1241                     break;
       
  1242 
       
  1243                 ///////////////////////////////////////////////
       
  1244                 default:
       
  1245                     // Unexpected transaction type, so we do not know what to do with it
       
  1246                     ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: Invalid transaction type" ) ) );
       
  1247                     User::Panic(KSensrvPanicCategory, ESensrvPanicUnknownTransactionType);
       
  1248                     break;
       
  1249                 }
       
  1250             }
       
  1251 
       
  1252         if (err == KErrNone && ssyTransaction)
       
  1253             {
       
  1254             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - Notifying SSY..." ) ) );
       
  1255             // If SSY mediator is not yet initialized, it is an error
       
  1256             __ASSERT_ALWAYS(iProxy.SsyMediator(), User::Panic(KSensrvPanicCategory, ESensrvPanicNullSsyMediator));
       
  1257 
       
  1258             // Notify SSY mediator that transaction is ready to be handled.
       
  1259             err = iProxy.SsyMediator()->Notify(transaction);
       
  1260             }
       
  1261 
       
  1262         // If there was error, transaction needs to be removed from queue (which also completes it)
       
  1263         // Transaction also needs to be completed if SSY transaction is not needed.
       
  1264         // If transaction is completed, next one in queue is handled.
       
  1265         if (err != KErrNone)
       
  1266             {
       
  1267             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: Failed to handle next transaction: %d, type: %d" ), err, transaction->Type() ) );
       
  1268             transaction->SetErrorCode(err);
       
  1269             iChannelQueue->Remove(transaction, CSensrvTransactionQueue::ERemovalTypeComplete);
       
  1270 
       
  1271             // Prepare to handle next transaction
       
  1272             transaction = iChannelQueue->First();
       
  1273             }
       
  1274         else if (!ssyTransaction)
       
  1275             {
       
  1276             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - SSY transaction not needed, completing transaction" ) ) );
       
  1277             iChannelQueue->Remove(transaction, CSensrvTransactionQueue::ERemovalTypeComplete);
       
  1278 
       
  1279             // Prepare to handle next transaction
       
  1280             transaction = iChannelQueue->First();
       
  1281             }
       
  1282         else
       
  1283             {
       
  1284             // SSY transaction executing, no need for further transaction handling now
       
  1285             iProxy.TransactionMonitor()->AddTransaction(transaction);
       
  1286             transaction = NULL;
       
  1287             }
       
  1288         } // End while(transaction)
       
  1289 
       
  1290     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - return" ) ) );
       
  1291     }
       
  1292 
       
  1293 // ---------------------------------------------------------------------------
       
  1294 // Gets listener pointer from list or NULL if no listener found.
       
  1295 // ---------------------------------------------------------------------------
       
  1296 //
       
  1297 CSensrvChannelListener* CSensrvChannel::GetListener(CSensrvSession* aSession)
       
  1298     {
       
  1299     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetListener(aSession: 0x%x)" ), aSession ) );
       
  1300 
       
  1301     CSensrvChannelListener* listener = NULL;
       
  1302     TInt count(iListenerList.Count());
       
  1303 
       
  1304     for (TInt i=0; i < count; i++)
       
  1305         {
       
  1306         if (&iListenerList[i]->Session() == aSession)
       
  1307             {
       
  1308             listener = iListenerList[i];
       
  1309             break;
       
  1310             }
       
  1311         }
       
  1312 
       
  1313     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetListener - return 0x%x" ), listener ) );
       
  1314 
       
  1315     return listener;
       
  1316     }
       
  1317 
       
  1318 // ---------------------------------------------------------------------------
       
  1319 // Deletes listener if it exists
       
  1320 // ---------------------------------------------------------------------------
       
  1321 //
       
  1322 void CSensrvChannel::DeleteListener(CSensrvSession* aSession)
       
  1323     {
       
  1324     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::DeleteListener(aSession: 0x%x)" ), aSession ) );
       
  1325 
       
  1326     TInt count(iListenerList.Count());
       
  1327     TBool deleted( EFalse );
       
  1328 
       
  1329     for ( TInt i=0; !deleted && i < count; i++ )
       
  1330         {
       
  1331         if ( &iListenerList[i]->Session() == aSession )
       
  1332             {
       
  1333             deleted = ETrue;
       
  1334             CSensrvChannelListener* ptr = iListenerList[i];
       
  1335             if ( ptr == iCurrentSetPropertyControl )
       
  1336                 {
       
  1337                 // Reset the property control
       
  1338                 iCurrentSetPropertyControl = NULL;
       
  1339                 }
       
  1340             if ( iHighestListenerPriorityMultiples > 0 &&
       
  1341                  ptr->Priority() == iHighestListenerPriority )
       
  1342                 {
       
  1343                 // Update cached highest listener priority tracking
       
  1344                 if ( !( --iHighestListenerPriorityMultiples ) )
       
  1345                     {
       
  1346                     // Reset the highest priority when there are no multiples.
       
  1347                     // Next GetHighestListenerPriority() call triggers complete priority check.
       
  1348                     iHighestListenerPriority = KMinTInt;
       
  1349                     }
       
  1350                 }
       
  1351             delete ptr;
       
  1352             iListenerList.Remove(i);
       
  1353             }
       
  1354         }
       
  1355 
       
  1356     // If listener was not found, it is normally not an error, but in some cases it is interesting.
       
  1357     // -> Trace only regular COMPONENT_TRACE
       
  1358     if ( !deleted )
       
  1359         {
       
  1360         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::DeleteListener - Listener not found" ) ) );
       
  1361         }
       
  1362 
       
  1363     // Check if unload timer needs to be started, since there are no more clients
       
  1364     if (!iListenerList.Count())
       
  1365         {
       
  1366         iProxy.StartUnloadTimerIfNeeded();
       
  1367         }
       
  1368 
       
  1369     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::DeleteListener - return" ) ) );
       
  1370     }
       
  1371 
       
  1372 // ---------------------------------------------------------------------------
       
  1373 // Finds a listener based on session
       
  1374 // ---------------------------------------------------------------------------
       
  1375 //
       
  1376 CSensrvChannelListener* CSensrvChannel::FindListener( CSensrvSession* aSession )
       
  1377     {
       
  1378     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::FindListener(aSession: 0x%x)" ), aSession ) );
       
  1379 
       
  1380     TInt count( iListenerList.Count() );
       
  1381     CSensrvChannelListener* ptr( NULL );
       
  1382 
       
  1383     if ( aSession )
       
  1384 		{
       
  1385 		for ( TInt i=0; i < count; i++ )
       
  1386 			{
       
  1387 			if ( &iListenerList[i]->Session() == aSession )
       
  1388 				{
       
  1389 				ptr = iListenerList[ i ];
       
  1390 				break;
       
  1391 				}
       
  1392 			}
       
  1393 		}
       
  1394 
       
  1395     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::FindListener - return" ) ) );
       
  1396 
       
  1397 	return ptr;
       
  1398     }
       
  1399 
       
  1400 // ---------------------------------------------------------------------------
       
  1401 // Closes the channel to SSY if there are no more listeners.
       
  1402 // ---------------------------------------------------------------------------
       
  1403 //
       
  1404 void CSensrvChannel::CloseIfNeeded()
       
  1405     {
       
  1406     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded()" ) ) );
       
  1407 
       
  1408     // Check listener count and state
       
  1409     if (iState == EChannelStateClosed)
       
  1410         {
       
  1411         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - Close NOT needed, channel already closed" ) ) );
       
  1412         }
       
  1413     else
       
  1414         {
       
  1415         TInt err(KErrNone);
       
  1416         TInt count(iListenerList.Count());
       
  1417         CSensrvTransaction* transaction = NULL;
       
  1418 
       
  1419         if (!count)
       
  1420             {
       
  1421             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - Close needed, no clients" ) ) );
       
  1422             // Create internal close transaction and queue it
       
  1423             TRAP(err, transaction = CSensrvTransaction::NewL(
       
  1424                 NULL,
       
  1425                 &iProxy,
       
  1426                 this,
       
  1427                 CSensrvTransaction::ETransTypeCloseChannel));
       
  1428             }
       
  1429         else if (iState == EChannelStateListening)
       
  1430             {
       
  1431             // Check if any of other clients are actually listening. If not,
       
  1432             // queue stop listening transaction
       
  1433             TBool activeFound(EFalse);
       
  1434 
       
  1435             for (TInt i=0; !activeFound && i < count; i++)
       
  1436                 {
       
  1437                 if (   iListenerList[i]->State() & CSensrvChannelListener::EChannelListenerStateConditionListening
       
  1438                     || iListenerList[i]->State() & CSensrvChannelListener::EChannelListenerStateDataListening)
       
  1439                     {
       
  1440                     activeFound = ETrue;
       
  1441                     }
       
  1442                 }
       
  1443 
       
  1444             if (!activeFound)
       
  1445                 {
       
  1446                 COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - Stop needed, no active clients" ) ) );
       
  1447                 TRAP(err, transaction = CSensrvTransaction::NewL(
       
  1448                     NULL,
       
  1449                     &iProxy,
       
  1450                     this,
       
  1451                     CSensrvTransaction::ETransTypeStopListening));
       
  1452                 }
       
  1453             else
       
  1454                 {
       
  1455                 COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - Stop NOT needed, active client found" ) ) );
       
  1456                 }
       
  1457             }
       
  1458         else
       
  1459             {
       
  1460             // State open and some other clients, no need to do anything
       
  1461             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - Close NOT needed, channel open and other clients found " ) ) );
       
  1462             }
       
  1463 
       
  1464         if ( (err == KErrNone) && (transaction) )
       
  1465             {
       
  1466             // Queue transaction. If there is some open transaction queued before close,
       
  1467             // that is alright, close/stop will be ignored in transaction handling if state at
       
  1468             // that point is not suitable.
       
  1469             transaction->SetState(CSensrvTransaction::ETransStateQueued);
       
  1470             err = iChannelQueue->Add(transaction);
       
  1471 
       
  1472             if (err != KErrNone)
       
  1473                 {
       
  1474                 delete transaction;
       
  1475                 transaction = NULL;
       
  1476                 ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - ERROR: Unable to queue close/stop transaction" ) ) );
       
  1477                 }
       
  1478             }
       
  1479         else
       
  1480             {
       
  1481             transaction = NULL;
       
  1482             }
       
  1483         }
       
  1484 
       
  1485     // Note that if there is an error creating/queueing transaction, channel will stay in incorrect state,
       
  1486     // but can't do anything about it.
       
  1487 
       
  1488     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::CloseIfNeeded - return" ) ) );
       
  1489     }
       
  1490 
       
  1491 
       
  1492 // ---------------------------------------------------------------------------
       
  1493 // Handles start data and condition listening transactions
       
  1494 // ---------------------------------------------------------------------------
       
  1495 //
       
  1496 TInt CSensrvChannel::HandleStartListeningTransaction(CSensrvTransaction& aTransaction)
       
  1497     {
       
  1498     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction()" ) ) );
       
  1499 
       
  1500     TInt err(KErrNone);
       
  1501 
       
  1502     CSensrvChannelListener::TSensrvChannelListenerState targetState
       
  1503         (CSensrvChannelListener::EChannelListenerStateDataListening);
       
  1504 
       
  1505     if (aTransaction.Type() == CSensrvTransaction::ETransTypeStartConditionListening)
       
  1506         {
       
  1507         // Condition evaluator must exist
       
  1508         TRAP( err, ConditionEvaluatorL() );
       
  1509 
       
  1510         targetState = CSensrvChannelListener::EChannelListenerStateConditionListening;
       
  1511         }
       
  1512     else // ETransTypeStartListening
       
  1513         {
       
  1514         // Target state already data listening
       
  1515         }
       
  1516 
       
  1517     if (err == KErrNone)
       
  1518         {
       
  1519         // Check that there is listener for this client session
       
  1520         CSensrvChannelListener* listener = GetListener(aTransaction.Session());
       
  1521 
       
  1522         if (!listener)
       
  1523             {
       
  1524             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: Listener doesn't exist." ) ) );
       
  1525             err = KErrNotFound;
       
  1526             }
       
  1527         else if (listener->State() & targetState)
       
  1528             {
       
  1529             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: Listener already listening." ) ) );
       
  1530             err = KErrAlreadyExists;
       
  1531             }
       
  1532         else
       
  1533             {
       
  1534             // Create mediator transactions to be used in notifying data writes to buffer.
       
  1535             // These transactions are never deleted and are reused.
       
  1536             if (!iDataAvailableTransaction)
       
  1537                 {
       
  1538                 TRAP(err, iDataAvailableTransaction = CSensrvTransaction::NewL(
       
  1539                     NULL,
       
  1540                     &iProxy,
       
  1541                     this,
       
  1542                     CSensrvTransaction::ETransTypeMediatorNewDataAvailable));
       
  1543 
       
  1544                 if (err != KErrNone)
       
  1545                     {
       
  1546                     ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: ETransTypeMediatorNewDataAvailable transaction creation failed: %d" ), err ) );
       
  1547                     }
       
  1548                 }
       
  1549 
       
  1550             if (err == KErrNone && !iForceBufferFilledTransaction)
       
  1551                 {
       
  1552                 TRAP(err, iForceBufferFilledTransaction = CSensrvTransaction::NewL(
       
  1553                     NULL,
       
  1554                     &iProxy,
       
  1555                     this,
       
  1556                     CSensrvTransaction::ETransTypeMediatorForceBufferFilled));
       
  1557 
       
  1558                 if (err != KErrNone)
       
  1559                     {
       
  1560                     ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: ETransTypeMediatorForceBufferFilled transaction creation failed: %d" ), err ) );
       
  1561                     }
       
  1562                 }
       
  1563 
       
  1564             if (err == KErrNone && !iForceChannelCloseTransaction)
       
  1565                 {
       
  1566                 TRAP(err, iForceChannelCloseTransaction = CSensrvTransaction::NewL(
       
  1567                     NULL,
       
  1568                     &iProxy,
       
  1569                     this,
       
  1570                     CSensrvTransaction::ETransTypeMediatorForceChannelClose));
       
  1571 
       
  1572                 if (err != KErrNone)
       
  1573                     {
       
  1574                     ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: ETransTypeMediatorForceChannelClose transaction creation failed: %d" ), err ) );
       
  1575                     }
       
  1576                 }
       
  1577 
       
  1578             if ( err == KErrNone )
       
  1579                 {
       
  1580                 // Get buffering count and period from message
       
  1581                 TSensrvStartListeningParametersPckgBuf pckg;
       
  1582                 err = aTransaction.Message()->Read( KSensrvStartListeningParametersSlot, pckg );
       
  1583 
       
  1584                 if ( err == KErrNone )
       
  1585                     {
       
  1586                     TSensrvStartListeningParameters startParams = pckg();
       
  1587 
       
  1588 					TSensrvResourceChannelInfo::TSensrvChannelGroup group = ChannelGroup();
       
  1589 
       
  1590 					if (   group == TSensrvResourceChannelInfo::ESensrvChannelGroupEvent
       
  1591 						|| group == TSensrvResourceChannelInfo::ESensrvChannelGroupState )
       
  1592 						{
       
  1593 						startParams.iBufferingPeriod = 0;
       
  1594 						}
       
  1595 
       
  1596                     err = listener->StartListening( startParams.iDesiredBufferingCount,
       
  1597                                                     startParams.iMaximumBufferingCount,
       
  1598                                                     startParams.iBufferingPeriod,
       
  1599                                                     targetState );
       
  1600                     if (err != KErrNone)
       
  1601                         {
       
  1602                         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: Listener adding failed: %d" ), err ) );
       
  1603                         }
       
  1604                     }
       
  1605                 else
       
  1606                     {
       
  1607                     ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: Start parameters reading failed: %d" ), err ) );
       
  1608                     }
       
  1609                 }
       
  1610             }
       
  1611         }
       
  1612     else
       
  1613         {
       
  1614         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - ERROR: Condition evaluator plugin loading failed: %d" ), err ) );
       
  1615         }
       
  1616 
       
  1617 
       
  1618     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - return %d" ), err ) );
       
  1619 
       
  1620     return err;
       
  1621     }
       
  1622 
       
  1623 // ---------------------------------------------------------------------------
       
  1624 // Handles stop data and condition listening transactions
       
  1625 // ---------------------------------------------------------------------------
       
  1626 //
       
  1627 TInt CSensrvChannel::HandleStopListeningTransaction(CSensrvTransaction& aTransaction,
       
  1628                                                     TBool& aSsyTransactionNeeded)
       
  1629     {
       
  1630     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStopListeningTransaction()" ) ) );
       
  1631 
       
  1632     TInt err(KErrNone);
       
  1633 
       
  1634     CSensrvChannelListener::TSensrvChannelListenerState currentState
       
  1635         (CSensrvChannelListener::EChannelListenerStateDataListening);
       
  1636 
       
  1637     if (aTransaction.Type() == CSensrvTransaction::ETransTypeStopConditionListening)
       
  1638         {
       
  1639         currentState = CSensrvChannelListener::EChannelListenerStateConditionListening;
       
  1640         }
       
  1641     else // ETransTypeStartListening
       
  1642         {
       
  1643         // Target state already data listening
       
  1644         }
       
  1645 
       
  1646     // Check that there is listener for this client session
       
  1647     CSensrvChannelListener* listener = GetListener(aTransaction.Session());
       
  1648 
       
  1649     if (listener && !(listener->State() & currentState))
       
  1650         {
       
  1651         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: Listener not listening." ) ) );
       
  1652         err = KErrNotFound;
       
  1653         }
       
  1654     else
       
  1655         {
       
  1656         if (listener)
       
  1657             {
       
  1658             listener->StopListening(currentState, ETrue);
       
  1659             }
       
  1660 
       
  1661         if (iState == EChannelStateOpen)
       
  1662             {
       
  1663             ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleNextTransaction - ERROR: Channel not listening. Listener: 0x%x" ), listener ) );
       
  1664             err = KErrNotFound;
       
  1665             }
       
  1666         else // iState == EChannelStateListening
       
  1667             {
       
  1668             // Check if there are any more active listeners
       
  1669             TInt count(iListenerList.Count());
       
  1670             TBool activeFound(EFalse);
       
  1671 
       
  1672             for (TInt i=0; !activeFound && i < count; i++)
       
  1673                 {
       
  1674                 if (   iListenerList[i]->State() & CSensrvChannelListener::EChannelListenerStateConditionListening
       
  1675                     || iListenerList[i]->State() & CSensrvChannelListener::EChannelListenerStateDataListening)
       
  1676                     {
       
  1677                     activeFound = ETrue;
       
  1678                     }
       
  1679                 }
       
  1680 
       
  1681             if (!activeFound)
       
  1682                 {
       
  1683                 // No active listeners remain, stop listening at SSY
       
  1684                 aSsyTransactionNeeded = ETrue;
       
  1685                 }
       
  1686             else
       
  1687                 {
       
  1688                 // At least one active listener remaining, do not stop listening at SSY
       
  1689                 aSsyTransactionNeeded = EFalse;
       
  1690                 }
       
  1691             }
       
  1692         }
       
  1693 
       
  1694     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleStartListeningTransaction - return %d" ), err ) );
       
  1695 
       
  1696     return err;
       
  1697     }
       
  1698 
       
  1699 // ---------------------------------------------------------------------------
       
  1700 // CSensrvChannel::GetHighestListenerPriority()
       
  1701 // ---------------------------------------------------------------------------
       
  1702 //
       
  1703 TInt CSensrvChannel::GetHighestListenerPriority( TInt& aHighestPriority )
       
  1704     {
       
  1705     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetHighestListenerPriority()" ) ) );
       
  1706 
       
  1707     // Prefer the cached results if possible
       
  1708     if ( iHighestListenerPriorityMultiples <= 0 )
       
  1709         {
       
  1710         // Search the highest priority and its multiples from this channel and cache the results
       
  1711         iHighestListenerPriorityMultiples = 0;
       
  1712         iHighestListenerPriority = KMinTInt;
       
  1713         TInt i( iListenerList.Count() );
       
  1714         for( i--; i >= 0; i-- )
       
  1715             {
       
  1716             CSensrvChannelListener* ptr = iListenerList[ i ];
       
  1717             TInt priority( ptr->Priority() );
       
  1718             if ( priority > iHighestListenerPriority )
       
  1719                 {
       
  1720                 iHighestListenerPriority = priority;
       
  1721                 iHighestListenerPriorityMultiples = 1;
       
  1722                 }
       
  1723             else if ( priority == iHighestListenerPriority )
       
  1724                 {
       
  1725                 iHighestListenerPriorityMultiples++;
       
  1726                 }
       
  1727             }
       
  1728         }
       
  1729 
       
  1730     aHighestPriority = iHighestListenerPriority;
       
  1731 
       
  1732     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetHighestListenerPriority() - channel: %d priority: %d multiples: %d return" ),
       
  1733         iId, iHighestListenerPriority, iHighestListenerPriorityMultiples ) );
       
  1734 
       
  1735     return iHighestListenerPriorityMultiples;
       
  1736     }
       
  1737 
       
  1738 // ---------------------------------------------------------------------------
       
  1739 // CSensrvChannel::GetLowestChannelPriority()
       
  1740 // ---------------------------------------------------------------------------
       
  1741 //
       
  1742 TInt CSensrvChannel::GetLowestChannelPriority( const RSensrvChannelList* aAffectedChannelsList, TSensrvChannelId& aChannelId )
       
  1743     {
       
  1744 
       
  1745     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetLowestChannelPriority()" ) ) );
       
  1746 
       
  1747     TInt lowestPriority( KMaxTInt );
       
  1748     TInt newPriority( 0 );
       
  1749     TInt multiplesOnChannel( 0 );
       
  1750 
       
  1751     if ( aAffectedChannelsList )
       
  1752         {
       
  1753         TInt i( aAffectedChannelsList->Count() );
       
  1754         CSensrvChannel* channel = NULL;
       
  1755         for ( i--; i >= 0; i-- )
       
  1756             {
       
  1757 			if ( ( *aAffectedChannelsList )[ i ] != iId )
       
  1758             	{
       
  1759 				channel = iProxy.GetChannelForId( ( *aAffectedChannelsList )[ i ] );
       
  1760 				if ( channel )
       
  1761 					{
       
  1762 					multiplesOnChannel = channel->GetHighestListenerPriority( newPriority );
       
  1763 					if ( multiplesOnChannel > 0 )
       
  1764 						{
       
  1765 						if ( newPriority < lowestPriority )
       
  1766 							{
       
  1767 							lowestPriority 	= newPriority;
       
  1768 							aChannelId 		= ( *aAffectedChannelsList )[ i ];
       
  1769 							}
       
  1770 						}
       
  1771 					}
       
  1772 				}
       
  1773             }
       
  1774         }
       
  1775 
       
  1776     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetLowestChannelPriority() - channel %d priority: %d" ), aChannelId, lowestPriority ) );
       
  1777 
       
  1778 	return lowestPriority;
       
  1779     }
       
  1780 
       
  1781 // ---------------------------------------------------------------------------
       
  1782 // CSensrvChannel::GetHighestChannelPriority()
       
  1783 // ---------------------------------------------------------------------------
       
  1784 //
       
  1785 TInt CSensrvChannel::GetHighestChannelPriority( const RSensrvChannelList* aAffectedChannelsList, TSensrvChannelId& aChannelId )
       
  1786     {
       
  1787 
       
  1788     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetHighestChannelPriority()" ) ) );
       
  1789 
       
  1790     TInt highestPriority( KMinTInt );
       
  1791     TInt newPriority( 0 );
       
  1792     TInt multiplesOnChannel( 0 );
       
  1793 
       
  1794     if ( aAffectedChannelsList )
       
  1795         {
       
  1796         TInt i( aAffectedChannelsList->Count() );
       
  1797         CSensrvChannel* channel = NULL;
       
  1798         for ( i--; i >= 0; i-- )
       
  1799             {
       
  1800 			if ( ( *aAffectedChannelsList )[ i ] != iId )
       
  1801             	{
       
  1802 				channel = iProxy.GetChannelForId( ( *aAffectedChannelsList )[ i ] );
       
  1803 				if ( channel )
       
  1804 					{
       
  1805 					multiplesOnChannel = channel->GetHighestListenerPriority( newPriority );
       
  1806 					if ( multiplesOnChannel > 0 )
       
  1807 						{
       
  1808 						if ( newPriority > highestPriority )
       
  1809 							{
       
  1810 							highestPriority = newPriority;
       
  1811 							aChannelId 		= ( *aAffectedChannelsList )[ i ];
       
  1812 							}
       
  1813 						}
       
  1814 					}
       
  1815 				}
       
  1816             }
       
  1817         }
       
  1818 
       
  1819     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetHighestChannelPriority() - channel %d priority: %d" ), aChannelId, highestPriority ) );
       
  1820 
       
  1821 	return highestPriority;
       
  1822     }
       
  1823 
       
  1824 // ---------------------------------------------------------------------------
       
  1825 // CSensrvChannel::GetHighestListenerPriorityOfAffectedChannels()
       
  1826 // ---------------------------------------------------------------------------
       
  1827 //
       
  1828 TInt CSensrvChannel::GetHighestListenerPriorityOfAffectedChannels( TInt& aHighestPriority )
       
  1829     {
       
  1830     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetHighestListenerPriorityOfAffectedChannels()" ) ) );
       
  1831 
       
  1832     // Search the highest priority and its multiples from all affected channels
       
  1833     RSensrvChannelList* affectedChannels = GetAffectedChannels();
       
  1834     aHighestPriority = KMinTInt;
       
  1835     TInt multiples( 0 );
       
  1836     if ( affectedChannels )
       
  1837         {
       
  1838         TInt i( affectedChannels->Count() );
       
  1839         TInt highestOfChannel( 0 );
       
  1840         TInt multiplesOnChannel( 0 );
       
  1841         CSensrvChannel* channel = NULL;
       
  1842         for ( i--; i >= 0; i-- )
       
  1843             {
       
  1844             channel = iProxy.GetChannelForId( ( *affectedChannels )[ i ] );
       
  1845             if ( channel )
       
  1846                 {
       
  1847                 multiplesOnChannel = channel->GetHighestListenerPriority( highestOfChannel );
       
  1848                 if ( multiplesOnChannel > 0 )
       
  1849                     {
       
  1850                     if ( highestOfChannel > aHighestPriority )
       
  1851                         {
       
  1852                         aHighestPriority = highestOfChannel;
       
  1853                         multiples = multiplesOnChannel;
       
  1854                         }
       
  1855                     else if ( highestOfChannel == aHighestPriority )
       
  1856                         {
       
  1857                         multiples += multiplesOnChannel;
       
  1858                         }
       
  1859                     }
       
  1860                 }
       
  1861             }
       
  1862 
       
  1863         affectedChannels->Reset();
       
  1864         delete affectedChannels;
       
  1865 		affectedChannels = NULL;
       
  1866 
       
  1867         }
       
  1868 
       
  1869     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetHighestListenerPriorityOfAffectedChannels() - priority: %d multiples: %d return" ),
       
  1870          aHighestPriority, multiples ) );
       
  1871 
       
  1872     return multiples;
       
  1873     }
       
  1874 
       
  1875 // ---------------------------------------------------------------------------
       
  1876 // CSensrvChannel::UpdateSetPropertySuccessIndToChannelListeners()
       
  1877 // ---------------------------------------------------------------------------
       
  1878 //
       
  1879 void CSensrvChannel::UpdateSetPropertySuccessIndToChannelListeners(
       
  1880         CSensrvChannel& aChannel, TInt aHighestOfAffectedChannels, TInt aMultiplesOnAffectedChannels )
       
  1881     {
       
  1882     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::UpdateSetPropertySuccessIndToChannelListeners()" ) ) );
       
  1883 
       
  1884     TInt highestOfChannel( 0 );
       
  1885     TInt multiplesOnChannel( aChannel.GetHighestListenerPriority( highestOfChannel ) );
       
  1886     if ( multiplesOnChannel > 0 )
       
  1887         {
       
  1888         if ( aChannel.iCurrentSetPropertyControl &&
       
  1889              aChannel.iCurrentSetPropertyControl->Priority() < highestOfChannel )
       
  1890             {
       
  1891             // Reset the property control due to higher priority listener
       
  1892             aChannel.iCurrentSetPropertyControl = NULL;
       
  1893             }
       
  1894         CSensrvChannelListener::TPropertySetSuccessEvaluationData evaluationData(
       
  1895             aHighestOfAffectedChannels, aMultiplesOnAffectedChannels,
       
  1896             highestOfChannel, multiplesOnChannel );
       
  1897         TInt i( aChannel.iListenerList.Count() );
       
  1898         for( i--; i >= 0; i-- )
       
  1899             {
       
  1900             aChannel.iListenerList[ i ]->HandleSetPropertySuccessInd( evaluationData );
       
  1901             }
       
  1902         }
       
  1903 
       
  1904     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::UpdateSetPropertySuccessIndToChannelListeners() - return" ) ) );
       
  1905     }
       
  1906 
       
  1907 // ---------------------------------------------------------------------------
       
  1908 // CSensrvChannel::HandleSetPropertySuccessIndOnPropertyChange()
       
  1909 // ---------------------------------------------------------------------------
       
  1910 //
       
  1911 void CSensrvChannel::HandleSetPropertySuccessIndOnPropertyChange()
       
  1912     {
       
  1913     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSetPropertySuccessIndOnPropertyChange()" ) ) );
       
  1914 
       
  1915     TInt highestOfAffectedChannels( 0 );
       
  1916     TInt multiplesOnAffectedChannels( GetHighestListenerPriorityOfAffectedChannels( highestOfAffectedChannels ) );
       
  1917 
       
  1918     // Send the priority information for listener of this channel to be evaluated
       
  1919     UpdateSetPropertySuccessIndToChannelListeners(
       
  1920         *this, highestOfAffectedChannels, multiplesOnAffectedChannels );
       
  1921 
       
  1922     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSetPropertySuccessIndOnPropertyChange() - return" ) ) );
       
  1923     }
       
  1924 
       
  1925 // ---------------------------------------------------------------------------
       
  1926 // CSensrvChannel::HandleSetPropertySuccessIndOnCreateOrDelete()
       
  1927 // ---------------------------------------------------------------------------
       
  1928 //
       
  1929 void CSensrvChannel::HandleSetPropertySuccessIndOnCreateOrDelete()
       
  1930     {
       
  1931     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSetPropertySuccessIndOnCreateOrDelete()" ) ) );
       
  1932 
       
  1933     // Get the priority information
       
  1934     RSensrvChannelList* affectedChannels = GetAffectedChannels();
       
  1935     if ( affectedChannels )
       
  1936         {
       
  1937         // Get the priority information
       
  1938         TInt highestOfAffectedChannels( 0 );
       
  1939         TInt multiplesOnAffectedChannels( GetHighestListenerPriorityOfAffectedChannels(
       
  1940             highestOfAffectedChannels ) );
       
  1941 
       
  1942         // Send the priority information for listeners of affected channels to be evaluated
       
  1943         TInt i( affectedChannels->Count() );
       
  1944         for ( i--; i >= 0; i-- )
       
  1945             {
       
  1946             CSensrvChannel* channel = iProxy.GetChannelForId( ( *affectedChannels )[ i ] );
       
  1947             if ( channel )
       
  1948                 {
       
  1949                 UpdateSetPropertySuccessIndToChannelListeners(
       
  1950                     *channel, highestOfAffectedChannels, multiplesOnAffectedChannels );
       
  1951                 }
       
  1952             }
       
  1953 
       
  1954         affectedChannels->Reset();
       
  1955         delete affectedChannels;
       
  1956 		affectedChannels = NULL;
       
  1957 
       
  1958         }
       
  1959 
       
  1960     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleSetPropertySuccessIndOnCreateOrDelete() - return" ) ) );
       
  1961     }
       
  1962 
       
  1963 // ---------------------------------------------------------------------------
       
  1964 // CSensrvChannel::TakePropertySetControl()
       
  1965 // ---------------------------------------------------------------------------
       
  1966 //
       
  1967 void CSensrvChannel::TakePropertySetControl( CSensrvChannelListener* aListener )
       
  1968     {
       
  1969     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::TakePropertySetControl()" ) ) );
       
  1970 
       
  1971     if ( iCurrentSetPropertyControl != aListener )
       
  1972         {
       
  1973         if ( aListener )
       
  1974             {
       
  1975             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::TakePropertySetControl() channel: %d listener: 0x%x priority: %d" ),
       
  1976                 iId, aListener, aListener->Priority() ) );
       
  1977             }
       
  1978         else
       
  1979             {
       
  1980             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::TakePropertySetControl() channel: %d listener: 0x%x" ), iId, aListener ) );
       
  1981             }
       
  1982         iCurrentSetPropertyControl = aListener;
       
  1983         }
       
  1984 
       
  1985     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::TakePropertySetControl() - return" ) ) );
       
  1986     }
       
  1987 
       
  1988 // ---------------------------------------------------------------------------
       
  1989 // CSensrvChannel::GetAffectedChannels()
       
  1990 // ---------------------------------------------------------------------------
       
  1991 //
       
  1992 RSensrvChannelList* CSensrvChannel::GetAffectedChannels() const
       
  1993     {
       
  1994     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetAffectedChannels()" ) ) );
       
  1995 
       
  1996     RSensrvChannelList* ret = new RSensrvChannelList();
       
  1997 
       
  1998     if ( iProxy.SsyMediator() && ret )
       
  1999         {
       
  2000         iProxy.SsyMediator()->AffectedChannels( iId, iPropertyProvider, ret );
       
  2001         }
       
  2002 
       
  2003     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetAffectedChannels() - return 0x%x" ), ret ) );
       
  2004     return ret;
       
  2005     }
       
  2006 
       
  2007 // ---------------------------------------------------------------------------
       
  2008 // CSensrvChannel::GetAvailabilityAffectedChannels()
       
  2009 // ---------------------------------------------------------------------------
       
  2010 //
       
  2011 RSensrvChannelList* CSensrvChannel::GetAvailabilityAffectedChannels( TSensrvAvailability aValue ) const
       
  2012     {
       
  2013     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetAvailabilityAffectedChannels()" ) ) );
       
  2014 
       
  2015     RSensrvChannelList* ret = new RSensrvChannelList();
       
  2016 
       
  2017     if ( iProxy.SsyMediator() && ret )
       
  2018         {
       
  2019         iProxy.SsyMediator()->AvailabilityAffectedChannels( iId, iPropertyProvider, ret, aValue );
       
  2020         }
       
  2021 
       
  2022     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::GetAvailabilityAffectedChannels() - return 0x%x" ), ret ) );
       
  2023     return ret;
       
  2024     }
       
  2025 
       
  2026 // ---------------------------------------------------------------------------
       
  2027 // CSensrvChannel::HandleDependencyOnOpen()
       
  2028 // ---------------------------------------------------------------------------
       
  2029 //
       
  2030 void CSensrvChannel::HandleDependencyOnOpen()
       
  2031 	{
       
  2032 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen() channel %d" ), iId ) );
       
  2033 
       
  2034 	TInt propErr( KErrNone );														//for property reading status
       
  2035 	TSensrvProperty property( KSensrvPropIdAvailability, KSensrvItemIndexNone, 0 );	//for property
       
  2036 
       
  2037 	TRAP( propErr, iPropertyProvider->GetPropertyL( iId, property ) ); //handle property reading error below
       
  2038 
       
  2039 	if ( propErr == KErrNone )
       
  2040 		{//availability property read OK ->
       
  2041 		TInt isAvailable( static_cast<TInt>( ESensrvAvailabilityFalse ) );	//for availability property value
       
  2042 
       
  2043 		property.GetValue( isAvailable ); //get iId channel availability property
       
  2044 
       
  2045 		if ( ESensrvAvailabilityFalse == ( static_cast<TSensrvAvailability>( isAvailable ) ) )
       
  2046 			{//current channel is not available, check if it needs to be set available ->
       
  2047 			COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen - channel %d unavailable (%d)" ),iId, isAvailable ) );
       
  2048 
       
  2049 			RSensrvChannelList* affectedChannels = GetAvailabilityAffectedChannels( ESensrvAvailabilityTrue ); //get the affected channels
       
  2050 
       
  2051 			if ( affectedChannels )
       
  2052 				{//affected channels returned a valid pointer ->
       
  2053 				if ( affectedChannels->Count() )
       
  2054 					{//there are affected channels ->
       
  2055 					TInt 				channelPriority( KMinTInt ); //for current channel priority
       
  2056 					TInt 				othersPriority( KMinTInt );	 //for other channel priority
       
  2057 					TSensrvChannelId 	otherChannelId( 0 );		 //for other channel channel id
       
  2058 
       
  2059 					(void)GetHighestListenerPriority( channelPriority ); //get current channel highest priority, multiplicity value is not needed, thus cast'd to void
       
  2060 
       
  2061 					othersPriority = GetLowestChannelPriority( affectedChannels, otherChannelId );
       
  2062 
       
  2063 					if ( channelPriority <= othersPriority )
       
  2064 						{//a higher priority client is blocking this channel ->
       
  2065 						//current channel highest is not high enough!
       
  2066 						COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen - channel %d priority (%d) is not high enough (vs. %d) to be enabled" ),iId, channelPriority, othersPriority ) );
       
  2067 						}//<- a higher priority client is blocking this channel
       
  2068 
       
  2069 					else
       
  2070 						{//this channel now has high enough priority user to enable and override another channel ->
       
  2071 						//other has lower highest priority, disable it and enable this
       
  2072 						COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen - channel %d priority (%d) is high enough (vs. %d)" ),iId, channelPriority, othersPriority ) );
       
  2073 
       
  2074 						(void)SetChannelAvailability( iId /*<- to enable*/, otherChannelId /* <- to disable */, property );
       
  2075 						}//<- this channel has now high enough priority user to enable and override another channel
       
  2076 					}//<-there are affected channels
       
  2077 				else
       
  2078 					{// no affected channels in list ->
       
  2079 					(void)SetChannelAvailability( iId /*<- to enable*/, property, ESensrvAvailabilityTrue );
       
  2080 					} // <- no affected channels
       
  2081 
       
  2082 		        affectedChannels->Reset();
       
  2083 		        delete affectedChannels;
       
  2084 				affectedChannels = NULL;
       
  2085 
       
  2086 				}//<- affected channels returned a valid pointer
       
  2087 			}//<- current channel is not available, check if it needs to be set available
       
  2088 		else
       
  2089 			{//Current channel is available ->
       
  2090 			COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen - channel %d already available (%d) or no affected channels" ),iId, isAvailable ) );
       
  2091 			}//<- Current channel is available
       
  2092 		}//<- availability property read OK
       
  2093 	else
       
  2094 		{
       
  2095 		//Error reading property
       
  2096 		ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen - ERROR: Unable to get property  Error: %d" ), propErr ) );
       
  2097 		}
       
  2098 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnOpen - return" ) ) );
       
  2099 	}
       
  2100 
       
  2101 // ---------------------------------------------------------------------------
       
  2102 // CSensrvChannel::HandleDependencyOnClose()
       
  2103 // ---------------------------------------------------------------------------
       
  2104 //
       
  2105 void CSensrvChannel::HandleDependencyOnClose( const TInt &aPriority )
       
  2106 	{
       
  2107 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose() channel %d" ), iId ) );
       
  2108 
       
  2109 	TInt propErr( KErrNone );
       
  2110 
       
  2111 	TSensrvProperty property( KSensrvPropIdAvailability, KSensrvItemIndexNone, 0 );
       
  2112 
       
  2113 	TRAP( propErr, iPropertyProvider->GetPropertyL( iId, property ) ); //get availability property for current channel
       
  2114 
       
  2115 	if ( propErr == KErrNone )
       
  2116 		{//availability property read OK ->
       
  2117 
       
  2118 		TInt isAvailable( static_cast<TInt>( ESensrvAvailabilityFalse ) );	//for availability property value
       
  2119 
       
  2120 		property.GetValue( isAvailable ); 	//get iId channel availability property
       
  2121 
       
  2122 		if ( ESensrvAvailabilityTrue == ( static_cast<TSensrvAvailability>( isAvailable ) ) )
       
  2123 			{//Current channel is available, check if it needs to be set unavailable ->
       
  2124 
       
  2125 			RSensrvChannelList* affectedChannels = GetAvailabilityAffectedChannels( ESensrvAvailabilityFalse );
       
  2126 
       
  2127 			if ( affectedChannels )
       
  2128 				{//affected channels returned a valid pointer ->
       
  2129 				if ( affectedChannels->Count() )
       
  2130 					{//there are affected channels ->
       
  2131 					TInt 				channelPriority( KMinTInt ); //for current channel priority
       
  2132 					TInt 				othersPriority( KMinTInt );	 //for other channel priority
       
  2133 					TSensrvChannelId 	otherChannelId( 0 );		 //for other channel channel id
       
  2134 
       
  2135 					(void)GetHighestListenerPriority( channelPriority ); //get current channel highest priority, multiplicity value is not needed, thus cast'd to void
       
  2136 
       
  2137 					if ( channelPriority >= aPriority )
       
  2138 						{//current channel has remaining listener(s) with a higher priority than listener that just left ->
       
  2139 						//do nuthin', only highest priority / channel matters
       
  2140 						COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - current channel has highest or equal priority" ) ) );
       
  2141 						}//<- current channel has remaining listener(s) with a higher priority than listener that just left
       
  2142 					else if ( 0 < ListenerCount() )
       
  2143 						{//highest priority listener of the channel left, other listeners exist ->
       
  2144 
       
  2145 						othersPriority = GetHighestChannelPriority( affectedChannels, otherChannelId );
       
  2146 
       
  2147 						if ( channelPriority < othersPriority )
       
  2148 							{//an unavailable channel has a higher priority listener than current channel ->
       
  2149 							COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - channel %d priority is high enough (%d) vs. current channel %d (%d)" ),
       
  2150 												otherChannelId, othersPriority, iId, channelPriority ) );
       
  2151 
       
  2152 							(void)SetChannelAvailability( otherChannelId /* <- to enable */, iId /* <- to disable */, property );
       
  2153 							}//<- an unavailable channel has a higher priority listener than current channel
       
  2154 							else
       
  2155 							{//an unavailable channel does not have a higher priority listener than current channel ->
       
  2156 							COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - other channel %d priority is not high enough (%d) vs. current channel %d (%d)" ),
       
  2157 												otherChannelId, othersPriority, iId, channelPriority ) );
       
  2158 							}//<- an unavailable channel does not have a higher priority listener than current channel
       
  2159 						}//<- highest priority listener of the channel left, other listeners exist
       
  2160 					else
       
  2161 						{//highest priority of the channel closed, no other listeners exist ->
       
  2162 						othersPriority = GetHighestChannelPriority( affectedChannels, otherChannelId );
       
  2163 
       
  2164 						COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - channel %d has no more listeners, enabling channel %d" ),iId, otherChannelId ) );
       
  2165 
       
  2166 						(void)SetChannelAvailability( otherChannelId /* <- to enable */, iId /* <- to disable */, property );
       
  2167 						} //<- highest priority of the channel closed, no other listeners exist
       
  2168 					}//<- there are affected channels
       
  2169 
       
  2170 				affectedChannels->Reset();
       
  2171 				delete affectedChannels;
       
  2172 				affectedChannels = NULL;
       
  2173 
       
  2174 				}//<- affected channels returned a valid pointer
       
  2175 			}//<- Current channel is available, check if it needs to be set unavailable
       
  2176 		else
       
  2177 			{//not available ->
       
  2178 			COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - channel not available (%d) or no affected channels" ), isAvailable ) );
       
  2179 			}//<- not available
       
  2180 		}//<- availability property read OK
       
  2181 	else
       
  2182 		{
       
  2183 		//Error reading property
       
  2184 		ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - ERROR: Unable to get property  Error: %d" ), propErr ) );
       
  2185 		}
       
  2186 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::HandleDependencyOnClose - return" ) ) );
       
  2187 	}
       
  2188 
       
  2189 // ---------------------------------------------------------------------------
       
  2190 // CSensrvChannel::SetAvailability()
       
  2191 // ---------------------------------------------------------------------------
       
  2192 //
       
  2193 TInt CSensrvChannel::SetAvailability( TSensrvChannelId aChannelId, TSensrvProperty& aProperty, TSensrvAvailability aValue, const TSecurityInfo& aSecurityInfo )
       
  2194 	{
       
  2195 	TInt propErr( KErrNone );
       
  2196 
       
  2197 	aProperty.SetValue( static_cast<TInt>( aValue ) );
       
  2198 	aProperty.SetSecurityInfo( aSecurityInfo );
       
  2199 
       
  2200 	TRAP( propErr, iPropertyProvider->SetPropertyL( aChannelId, aProperty ) );
       
  2201 
       
  2202 	if ( propErr == KErrNone )
       
  2203 		{
       
  2204 		//property set ok
       
  2205 		COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetAvailability - Channel %d property set to %d OK" ), aChannelId, aValue ) );
       
  2206 		}
       
  2207 	else
       
  2208 		{
       
  2209 		//handle property error
       
  2210 		ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetAvailability - ERROR: Unable to set property for dependent channel - Error: %d" ), propErr ) );
       
  2211 		}
       
  2212 
       
  2213 	return propErr;
       
  2214 	}
       
  2215 
       
  2216 // ---------------------------------------------------------------------------
       
  2217 // CSensrvChannel::SetChannelAvailability()
       
  2218 // ---------------------------------------------------------------------------
       
  2219 //
       
  2220 TInt CSensrvChannel::SetChannelAvailability( TSensrvChannelId aChannelIdToEnable, TSensrvChannelId aChannelIdToDisable, TSensrvProperty& aProperty )
       
  2221 	{
       
  2222 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability" ) ) );
       
  2223 
       
  2224 	TInt status ( KErrNone );
       
  2225 	TSecurityInfo securityInfo;
       
  2226 
       
  2227 	securityInfo.SetToCurrentInfo();
       
  2228 
       
  2229 	status = SetAvailability( aChannelIdToDisable, aProperty, ESensrvAvailabilityFalse, securityInfo );
       
  2230 
       
  2231 	if ( KErrNone == status )
       
  2232 		{
       
  2233 		status = SetAvailability( aChannelIdToEnable, aProperty, ESensrvAvailabilityTrue, securityInfo );
       
  2234 
       
  2235 		if ( KErrNone != status )
       
  2236 			{
       
  2237 			//Handle error
       
  2238 			ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability - ERROR: Unable to set channel %d to available - Error: %d" ), aChannelIdToEnable, status ) );
       
  2239 
       
  2240 			//rewind
       
  2241 			(void)SetAvailability( aChannelIdToDisable, aProperty, ESensrvAvailabilityTrue, securityInfo );
       
  2242 			}
       
  2243 		}
       
  2244 	else
       
  2245 		{
       
  2246 		//Handle error
       
  2247 		ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability - ERROR: Unable to set channel %d to unavailable - Error: %d" ), aChannelIdToDisable, status ) );
       
  2248 		//nothing to rewind
       
  2249 		}
       
  2250 
       
  2251 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability() - return %d" ), status ) );
       
  2252 
       
  2253 	return status;
       
  2254 	}
       
  2255 
       
  2256 // ---------------------------------------------------------------------------
       
  2257 // CSensrvChannel::SetChannelAvailable()
       
  2258 // ---------------------------------------------------------------------------
       
  2259 //
       
  2260 TInt CSensrvChannel::SetChannelAvailability( TSensrvChannelId aChannelId, TSensrvProperty& aProperty, TSensrvAvailability aValue )
       
  2261 	{
       
  2262 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability" ) ) );
       
  2263 
       
  2264 	TInt status ( KErrNone );
       
  2265 	TSecurityInfo securityInfo;
       
  2266 
       
  2267 	securityInfo.SetToCurrentInfo();
       
  2268 
       
  2269 	status = SetAvailability( aChannelId, aProperty, aValue, securityInfo );
       
  2270 	if ( KErrNone != status )
       
  2271 		{
       
  2272 		//Handle error
       
  2273 		ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability - ERROR: Unable to set channel %d to %d - Error: %d" ), aChannelId, aValue, status ) );
       
  2274 		}
       
  2275 
       
  2276 	COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::SetChannelAvailability() - return %d" ), status ) );
       
  2277 
       
  2278 	return status;
       
  2279 	}
       
  2280 
       
  2281 // ---------------------------------------------------------------------------
       
  2282 // Reads channel group
       
  2283 // ---------------------------------------------------------------------------
       
  2284 //
       
  2285 TSensrvResourceChannelInfo::TSensrvChannelGroup CSensrvChannel::ReadChannelGroup()
       
  2286     {
       
  2287     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ReadChannelGroup()" ) ) );
       
  2288 
       
  2289     // Use not defined as default if not defined or read fails
       
  2290     TInt channelGroup( TSensrvResourceChannelInfo::ESensrvChannelGroupNotDefined );
       
  2291     TInt err( KErrNone );
       
  2292 
       
  2293     // Open channel group repository and get necessary values
       
  2294     CRepository* repository = NULL;
       
  2295     TRAP( err, repository = CRepository::NewL( KCRUidSensorServerChannelGroups ) );
       
  2296     if ( err == KErrNone )
       
  2297         {
       
  2298         err = repository->Get( iChannelInfo.iChannelType, channelGroup );
       
  2299         if ( err != KErrNone || channelGroup < TSensrvResourceChannelInfo::ESensrvChannelGroupNotDefined )
       
  2300             {
       
  2301             channelGroup = TSensrvResourceChannelInfo::ESensrvChannelGroupNotDefined;
       
  2302             }
       
  2303         }
       
  2304     else
       
  2305         {
       
  2306         ERROR_TRACE( ( _L( "Sensor Server - CSensrvChannel::ReadChannelGroup - Channel groups repository opening failed: %d" ), err ) );
       
  2307         }
       
  2308 
       
  2309     // Cleanup repository
       
  2310     delete repository;
       
  2311     repository = NULL;
       
  2312 
       
  2313     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ReadChannelGroup() - return %d" ), channelGroup ) );
       
  2314 
       
  2315     return static_cast< TSensrvResourceChannelInfo::TSensrvChannelGroup >( channelGroup );
       
  2316     }
       
  2317 
       
  2318 // ---------------------------------------------------------------------------
       
  2319 // Gets channel group
       
  2320 // ---------------------------------------------------------------------------
       
  2321 //
       
  2322 TSensrvResourceChannelInfo::TSensrvChannelGroup CSensrvChannel::ChannelGroup()
       
  2323     {
       
  2324     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelGroup()" ) ) );
       
  2325 
       
  2326     if ( iChannelInfo.iChannelGroup == TSensrvResourceChannelInfo::ESensrvChannelGroupNotSolved )
       
  2327         {
       
  2328         iChannelInfo.iChannelGroup = ReadChannelGroup();
       
  2329         }
       
  2330 
       
  2331     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvChannel::ChannelGroup() - return %d" ), iChannelInfo.iChannelGroup ) );
       
  2332 
       
  2333     return iChannelInfo.iChannelGroup;
       
  2334     }