accessoryservices/accessoryserver/src/Server/AccSrvConnectionController.cpp
changeset 0 4e1aa6a622a0
child 3 a811597961f0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  Connection Controller
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "AccSrvConnectionController.h"
       
    22 #include "AccPolAccessoryPolicy.h"
       
    23 #include "AccSrvServerModel.h"
       
    24 #include "AccSrvNotificationQueue.h"
       
    25 #include "AccSrvConnectionStatusHandler.h"
       
    26 #include "AccSrvModeHandler.h"
       
    27 #include "AccPolGenericIDArrayAccessor.h"
       
    28 #include "acc_debug.h"
       
    29 #include "AccSrvASYProxyHandler.h"
       
    30 #include "AccPolGenericIDAccessor.h"
       
    31 #include "AccSrvSettingsHandler.h"
       
    32 #include "AccPolSubblockNameArrayAccessor.h"
       
    33 #include "AccSrvChargingContextController.h"
       
    34 #include <AccConfigFileParser.h>
       
    35 #include <s32mem.h>
       
    36 #include "AccSrvWiredConnectionPublisher.h"
       
    37 
       
    38 // EXTERNAL DATA STRUCTURES
       
    39 
       
    40 // EXTERNAL FUNCTION PROTOTYPES
       
    41 
       
    42 // CONSTANTS
       
    43 
       
    44 const TUid KAccFwUiNoteNotifierUid = { 0x10205061 };
       
    45 
       
    46 // MACROS
       
    47 
       
    48 // LOCAL CONSTANTS AND MACROS
       
    49 
       
    50 // MODULE DATA STRUCTURES
       
    51 
       
    52 // LOCAL FUNCTION PROTOTYPES
       
    53 
       
    54 // FORWARD DECLARATIONS
       
    55 
       
    56 // ============================= LOCAL FUNCTIONS ===============================
       
    57 
       
    58 // ============================ MEMBER FUNCTIONS ===============================
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CAccSrvConnectionController::CAccSrvConnectionController
       
    62 // C++ default constructor can NOT contain any code, that
       
    63 // might leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CAccSrvConnectionController::CAccSrvConnectionController()
       
    67     : CActive( EPriorityStandard ),
       
    68       iPolicy( NULL ),
       
    69       iServerModel( NULL ),
       
    70       iNotificationQueue( NULL ),
       
    71       iConnectionStatusHandler( NULL ),
       
    72       iModeHandler( NULL ),
       
    73       iASYProxyHandler( NULL ),
       
    74       iShowNotes( EFalse ),
       
    75       iWiredConnPublisher( NULL ),
       
    76       iReplyPck( iReplyValue)
       
    77     {
       
    78     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::CAccSrvConnectionController()" );
       
    79 
       
    80     CActiveScheduler::Add( this );//Add the active object to the active scheduler
       
    81 
       
    82     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::CAccSrvConnectionController - return" );
       
    83     }
       
    84 
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CAccSrvConnectionController::ConstructL
       
    88 // Symbian 2nd phase constructor can leave.
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 
       
    92 void CAccSrvConnectionController::ConstructL( CAccSrvServerModel* aServerModel )
       
    93     {
       
    94     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::ConstructL()" );
       
    95 
       
    96     iServerModel             = aServerModel;
       
    97     iPolicy                  = CAccPolAccessoryPolicy::NewL( &iServerModel->CapabilityStorage());
       
    98     iNotificationQueue       = CAccSrvNotificationQueue::NewL();
       
    99     iConnectionStatusHandler = CAccSrvConnectionStatusHandler::NewL( this );
       
   100     iModeHandler             = CAccSrvModeHandler::NewL( this );
       
   101     iASYProxyHandler         = CAccSrvASYProxyHandler::NewL( this );
       
   102     iSettingsHandler         = CAccSrvSettingsHandler::NewL( this, *aServerModel );
       
   103     iWiredConnPublisher      = CAccSrvWiredConnectionPublisher::NewL();
       
   104     TRAPD( err, iChargingContextController = CAccSrvChargingContextController::NewL() );
       
   105     if ( err != KErrNone ) // Server does not need to die on charging context fail.
       
   106         {
       
   107         COM_TRACE_1( "[AccFW:AccServer] CAccSrvConnectionController::ConstructL - CAccSrvChargingContextController err %d", err );
       
   108         iChargingContextController = NULL;
       
   109         }
       
   110 
       
   111     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::ConstructL - return void" );
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CAccSrvConnectionController::NewL
       
   116 // Two-phased constructor.
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 CAccSrvConnectionController* CAccSrvConnectionController::NewL(
       
   120     CAccSrvServerModel* aServerModel )
       
   121     {
       
   122     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::NewL()" );
       
   123 
       
   124     CAccSrvConnectionController* self = new( ELeave ) CAccSrvConnectionController;
       
   125 
       
   126     CleanupStack::PushL( self );
       
   127     self->ConstructL( aServerModel );
       
   128     CleanupStack::Pop( self );
       
   129 
       
   130     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::NewL - return self" );
       
   131 
       
   132     return self;
       
   133     }
       
   134 
       
   135 // Destructor
       
   136 CAccSrvConnectionController::~CAccSrvConnectionController()
       
   137     {
       
   138     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::~CAccSrvConnectionController()" );
       
   139 
       
   140     if( NULL != iPolicy )
       
   141         {
       
   142         delete iPolicy;
       
   143         }
       
   144 
       
   145     if( NULL != iNotificationQueue )
       
   146         {
       
   147         delete iNotificationQueue;
       
   148         }
       
   149 
       
   150     if( NULL != iConnectionStatusHandler )
       
   151         {
       
   152         delete iConnectionStatusHandler;
       
   153         }
       
   154 
       
   155     if( NULL != iModeHandler )
       
   156         {
       
   157         delete iModeHandler;
       
   158         }
       
   159 
       
   160     if( NULL != iASYProxyHandler )
       
   161         {
       
   162         delete iASYProxyHandler;
       
   163         }
       
   164 
       
   165     if( NULL != iSettingsHandler )
       
   166         {
       
   167         delete iSettingsHandler;
       
   168         }
       
   169     
       
   170     if( NULL != iChargingContextController )
       
   171         {
       
   172         delete iChargingContextController;
       
   173         }
       
   174 
       
   175     delete iWiredConnPublisher;
       
   176 
       
   177     // Handlers must be deleted after iPolicy and iASYProxyHandler
       
   178     // (used from handlers).
       
   179     iConnectionHandler.ResetAndDestroy();
       
   180     
       
   181     if ( iNotifier.Handle() )
       
   182         {
       
   183         iNotifier.Close();
       
   184         }
       
   185     Cancel();
       
   186 
       
   187     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::~CAccSrvConnectionController - return" );
       
   188     }
       
   189 
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CAccSrvConnectionController::ServerModel
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 CAccSrvServerModel& CAccSrvConnectionController::ServerModel() const
       
   196     {
       
   197     return *iServerModel;
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CAccSrvConnectionController::NotificationQueue
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 CAccSrvNotificationQueue& CAccSrvConnectionController::NotificationQueue() const
       
   205     {
       
   206     return *iNotificationQueue;
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CAccSrvConnectionController::ASYProxyHandler
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 CAccSrvASYProxyHandler& CAccSrvConnectionController::ASYProxyHandler() const
       
   214     {
       
   215     return *iASYProxyHandler;
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CAccSrvConnectionController::SettingsHandler
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 CAccSrvSettingsHandler& CAccSrvConnectionController::SettingsHandler() const
       
   223     {
       
   224     return *iSettingsHandler;
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CAccSrvConnectionController::Policy
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 CAccPolAccessoryPolicy& CAccSrvConnectionController::Policy() const
       
   232     {
       
   233     return *iPolicy;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CAccSrvConnectionController::HandleConnectL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CAccSrvConnectionController::HandleConnectL( TAccPolGenericID& aGenericID,
       
   241                                                   TUint aCallerThreadID,
       
   242                                                   TBool aEvaluateConnectionRules,
       
   243                                                   TBool aUpdateConnection )
       
   244     {
       
   245     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL()" );
       
   246 
       
   247 	if ( aUpdateConnection )
       
   248 	    {
       
   249 	    COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL() Update connection" );
       
   250 		
       
   251 		//check is there connection handling ongoing for unique id
       
   252 		TInt index( KErrNotFound );
       
   253 		for ( TInt i(0); i < iConnectionHandler.Count(); i++ )
       
   254 	    	{
       
   255 			if ( aGenericID.UniqueID() == 
       
   256                  iConnectionHandler.operator[]( i )->UniqueIDForConnectionHandler() )
       
   257 			    {
       
   258 				index = i;
       
   259 				break;
       
   260 			    }
       
   261 	    	}
       
   262 		
       
   263 		//cancel current connection handling and reset connection handler
       
   264 		if ( KErrNotFound == index)
       
   265 		    {
       
   266             //Create a new connection handler for this connection and start the handling
       
   267             CAccSrvConnectionHandler* connectionHandler = CAccSrvConnectionHandler::NewL( this, iServerModel, iPolicy );
       
   268             CleanupStack::PushL( connectionHandler );
       
   269             iConnectionHandler.InsertL( connectionHandler, 0 );
       
   270             CleanupStack::Pop( connectionHandler );
       
   271             index = 0;
       
   272             }
       
   273         else
       
   274             {
       
   275             COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL() Connection handling ongoing - delete current connection handler" );
       
   276 			iConnectionHandler.operator[]( index )->CancelConnectionHandling();
       
   277             iConnectionHandler.operator[]( index )->ResetHandler();
       
   278    		    }
       
   279    		
       
   280 	    // Restart connection handling
       
   281     	iConnectionHandler.operator[]( index )->StartConnectionHandlingL( 
       
   282                                                     aGenericID,
       
   283                                                     aEvaluateConnectionRules, 
       
   284                                                     iServerModel->IsConnectedOrDetected( 
       
   285                                                         aGenericID ) );
       
   286     	}
       
   287 	else
       
   288     	{
       
   289 	    COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL() New connection" );
       
   290 
       
   291 	    // Store ThreadId for ASY handling
       
   292 	    iServerModel->StoreASYThreadID( aCallerThreadID, aGenericID.UniqueID() );
       
   293 
       
   294 	    iServerModel->AddPhysicalConnectionL( aGenericID );		
       
   295 
       
   296 	    iWiredConnPublisher->HandleConnectL( aGenericID );
       
   297 
       
   298         // Search for free connection handlers
       
   299         TInt index( KErrNotFound);
       
   300 		for ( TInt i(0); i < iConnectionHandler.Count(); i++ )
       
   301 		    {
       
   302 			if ( iConnectionHandler.operator[]( i )->IsFree() )
       
   303 			    {
       
   304 				index = i;
       
   305 				break;
       
   306 			    }
       
   307 		    }
       
   308 
       
   309         // Create new connection handler if available connection handler is not found
       
   310         if ( KErrNotFound == index )
       
   311             {
       
   312             COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL - Available handler not found, creating new" );
       
   313             CAccSrvConnectionHandler* handler = CAccSrvConnectionHandler::NewL( 
       
   314                 this, iServerModel, iPolicy );
       
   315             CleanupStack::PushL( handler );
       
   316             handler->StartConnectionHandlingL( aGenericID, aEvaluateConnectionRules, EFalse );
       
   317             iConnectionHandler.AppendL( handler );
       
   318             CleanupStack::Pop( handler );
       
   319             }
       
   320         else
       
   321             {
       
   322             // Available hander found, using that
       
   323             COM_TRACE_1( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL - Available handler found at %i, using that", index );
       
   324             iConnectionHandler.operator[]( index )->StartConnectionHandlingL( 
       
   325                 aGenericID, aEvaluateConnectionRules, EFalse );
       
   326             }	
       
   327     	}
       
   328 
       
   329     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectL - return void" );
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CAccSrvConnectionController::HandleConnectionUpdateValidationL
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CAccSrvConnectionController::HandleConnectionUpdateValidationL( 
       
   337     const TAccPolGenericID& aGenericID, 
       
   338 	TAccPolGenericID& aOldGenericId,
       
   339 	const CAccSrvConnectionHandler* aCaller,	
       
   340 	TInt aError )
       
   341     {
       
   342     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectionUpdateValidationL()" );
       
   343     
       
   344     TInt err ( KErrNone );
       
   345     TAccSrvConnection isValidConnection(EAccSrvConnect);
       
   346     TBool isConnected( iServerModel->IsConnected( aGenericID ) );
       
   347 
       
   348     if( KErrNone == aError )
       
   349         {   
       
   350         isValidConnection = iPolicy->IsValidConnectionL( aGenericID, err );
       
   351         }
       
   352     else
       
   353         {
       
   354         err = aError;
       
   355         isValidConnection = EAccSrvDeny;
       
   356         }
       
   357             
       
   358     if( iServerModel->FindAndRemoveCancelledConnectAccessory( aGenericID.UniqueID() ) )
       
   359         {
       
   360         //Connection of this accessory is cancelled.
       
   361         err = KErrCancel;
       
   362         isValidConnection = EAccSrvDeny;
       
   363         }
       
   364 
       
   365     switch( isValidConnection )
       
   366         {
       
   367         case EAccSrvDetect:
       
   368             {
       
   369             if( isConnected )
       
   370                 {
       
   371                 //accessory was connected, but now it is detected
       
   372  			    iServerModel->AddPhysicalConnectionL( aGenericID ); //Move GID from physical connection array
       
   373 			    iServerModel->RemoveConnectionL( aGenericID, EFalse );            //to connection array
       
   374  		        
       
   375   		        // Complete all possible connection status related requests
       
   376         		iNotificationQueue->CompleteControlMessageL( EAccessoryConnectionStatusChanged,
       
   377                                                 		     KErrNone,
       
   378 	                                                    	 aGenericID.UniqueID() );
       
   379         		iNotificationQueue->CompleteControlMessageL( EAccessoryDisconnected,
       
   380                 		                                     KErrNone,
       
   381                         		                             aOldGenericId.UniqueID() );
       
   382                 }
       
   383             else
       
   384                 {
       
   385                 //Accessory remains in detected state.
       
   386                 }
       
   387             }
       
   388             break;
       
   389 
       
   390         case EAccSrvConnect:
       
   391             {
       
   392             if( isConnected )
       
   393                 {
       
   394                 //update connection generic id array
       
   395                 iServerModel->UpdateConnectionL( aGenericID);
       
   396                 
       
   397  		        // Complete all possible connection status related requests
       
   398         		iNotificationQueue->CompleteControlMessageL( EAccessoryConnectionStatusChanged,
       
   399                                                 		     KErrNone,
       
   400 	                                                    	 aGenericID.UniqueID() );
       
   401         		iNotificationQueue->CompleteControlMessageL( EAccessoryDisconnected,
       
   402                 		                                     KErrNone,
       
   403                         		                             aOldGenericId.UniqueID() );
       
   404     		    iNotificationQueue->CompleteControlMessageL( ENewAccessoryConnected,
       
   405                                         		             KErrNone,
       
   406                                 		                     aGenericID.UniqueID() );
       
   407                 }
       
   408             else
       
   409                 {
       
   410                 //accessory was detected, but now it is connected
       
   411 			    iServerModel->RemovePhysicalConnection( aGenericID ); //Move GID from physical connection array
       
   412 			    iServerModel->AddConnectionL( aGenericID );            //to connection array
       
   413   
       
   414  		        // Complete all possible connection status related requests
       
   415         		iNotificationQueue->CompleteControlMessageL( EAccessoryConnectionStatusChanged,
       
   416                                                 		     KErrNone,
       
   417 	                                                    	 aGenericID.UniqueID() );
       
   418     		    iNotificationQueue->CompleteControlMessageL( ENewAccessoryConnected,
       
   419                                         		             KErrNone,
       
   420                                 		                     aGenericID.UniqueID() );
       
   421                 }
       
   422             }
       
   423             break;
       
   424 
       
   425         case EAccSrvDeny:
       
   426             {
       
   427             iServerModel->RemovePhysicalConnection( aGenericID );
       
   428             iServerModel->RemoveASYThreadID( aGenericID.UniqueID() );//Remove ThreadID GID mapping
       
   429             iServerModel->CapabilityStorage().RemoveCapabilityList( aGenericID);
       
   430             
       
   431          
       
   432             }
       
   433             break;
       
   434 
       
   435         default:
       
   436             {
       
   437             TRACE_ASSERT_ALWAYS;//unhandled enum
       
   438             }
       
   439             break;
       
   440         }
       
   441 
       
   442     iNotificationQueue->CompleteControlMessageL( EConnectAccessory,
       
   443                                                  err,
       
   444                                                  aGenericID.UniqueID() );
       
   445 
       
   446     //"This" connection handler has done its job
       
   447     TInt index( iConnectionHandler.Find( aCaller ) );
       
   448     if( KErrNotFound != index )
       
   449         {
       
   450         delete iConnectionHandler.operator[]( index );
       
   451         iConnectionHandler.Remove( index );
       
   452         }
       
   453     else
       
   454         {
       
   455         TRACE_ASSERT_ALWAYS; //iConnectionHandler array is not in sync
       
   456         }
       
   457 
       
   458     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectionUpdateValidationL - return void" );
       
   459     }
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CAccSrvConnectionController::ConnectionHandlingCancel
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 void CAccSrvConnectionController::ConnectionHandlingCancel(
       
   466     const CAccSrvConnectionHandler* aCaller )
       
   467     {
       
   468     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::ConnectionHandlingCancel - start" );
       
   469     //"This" connection handler has done its job
       
   470     TInt index( iConnectionHandler.Find( aCaller ) );
       
   471     if( KErrNotFound != index )
       
   472         {
       
   473         COM_TRACE_1( "[AccFW:AccServer] CAccSrvConnectionController::ConnectionHandlingCancel - Handler found at %i", index );
       
   474         iConnectionHandler.operator[]( index )->CancelConnectionHandling();
       
   475         iConnectionHandler.operator[]( index )->ResetHandler();
       
   476         }
       
   477     else
       
   478         {
       
   479         TRACE_ASSERT_ALWAYS; //iConnectionHandler array is not in sync
       
   480         }
       
   481 	COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::ConnectionHandlingCancel - end" );
       
   482     }
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // CAccSrvConnectionController::HandleConnectValidationL.
       
   486 //
       
   487 // During AccessoryConnection this is the phase where all critical capabilities
       
   488 // are initialized and decision for connection acceptance can be done.
       
   489 // -----------------------------------------------------------------------------
       
   490 //
       
   491 void CAccSrvConnectionController::HandleConnectValidationL(
       
   492     const TAccPolGenericID& aGenericID,
       
   493     const CAccSrvConnectionHandler* aCaller,
       
   494     TInt aError )
       
   495     {
       
   496     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectValidationL()" );
       
   497     
       
   498     TInt err ( KErrNone );
       
   499     TAccSrvConnection isValidConnection( EAccSrvConnect );
       
   500 
       
   501     if( KErrNone == aError )
       
   502         {   
       
   503         isValidConnection = iPolicy->IsValidConnectionL( aGenericID, err );
       
   504         }
       
   505     else
       
   506         {
       
   507         err = aError;
       
   508         isValidConnection = EAccSrvDeny;
       
   509         }
       
   510             
       
   511     if( iServerModel->FindAndRemoveCancelledConnectAccessory( aGenericID.UniqueID() ) )
       
   512         {
       
   513         //Connection of this accessory is cancelled.
       
   514         err = KErrCancel;
       
   515         isValidConnection = EAccSrvDeny;
       
   516         }
       
   517 
       
   518     iNotificationQueue->CompleteControlMessageL( EConnectAccessory,
       
   519                                                  err,
       
   520                                                  aGenericID.UniqueID() );
       
   521     switch( isValidConnection )
       
   522         {
       
   523         case EAccSrvDetect:
       
   524             {
       
   525             // Accessory is detected but not connected.
       
   526             // Further changes to capabilities may change this
       
   527             // i.e. connection status changes from detected to connected.
       
   528             // Detected accessory is allready in physical connection table
       
   529             // (AddPhysicalConnectionL), so there is nothing to do here.
       
   530             }
       
   531             break;
       
   532 
       
   533         case EAccSrvConnect:
       
   534             {
       
   535             PromoteToConnectedL( aGenericID );
       
   536             }
       
   537             break;
       
   538 
       
   539         case EAccSrvDeny:
       
   540             {
       
   541             iServerModel->RemovePhysicalConnection( aGenericID );
       
   542             iServerModel->RemoveASYThreadID( aGenericID.UniqueID() );//Remove ThreadID GID mapping
       
   543             iServerModel->CapabilityStorage().RemoveCapabilityList( aGenericID );
       
   544             
       
   545          
       
   546             }
       
   547             break;
       
   548 
       
   549         default:
       
   550             {
       
   551             TRACE_ASSERT_ALWAYS;//unhandled enum
       
   552             }
       
   553             break;
       
   554         }
       
   555 
       
   556     //"This" connection handler has done its job
       
   557     TInt index( iConnectionHandler.Find( aCaller ) );
       
   558     if( KErrNotFound != index )
       
   559         {
       
   560         delete iConnectionHandler.operator[]( index );
       
   561         iConnectionHandler.Remove( index );
       
   562         }
       
   563     else
       
   564         {
       
   565         TRACE_ASSERT_ALWAYS; //iConnectionHandler array is not in sync
       
   566         }
       
   567 
       
   568     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectValidationL - return void" );
       
   569     }
       
   570 
       
   571 // -----------------------------------------------------------------------------
       
   572 // CAccSrvConnectionController::HandleConnectionChangeValidationL
       
   573 //
       
   574 // Accessory is allready connected and a capability is changed.
       
   575 // This may change a single accessory connection
       
   576 // - from detected to connected
       
   577 // - from connected to detected
       
   578 // -----------------------------------------------------------------------------
       
   579 //
       
   580 void CAccSrvConnectionController::HandleConnectionChangeValidationL(
       
   581     const TAccPolGenericID& aGenericID )
       
   582     {
       
   583     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectionChangeValidationL()" );
       
   584 
       
   585     TBool isConnected( iServerModel->IsConnected( aGenericID ) );
       
   586     TInt err( KErrNone );
       
   587 
       
   588     switch( iPolicy->IsValidConnectionL( aGenericID, err ) )
       
   589         {
       
   590         case EAccSrvConnect:
       
   591             {
       
   592             if( isConnected )
       
   593                 {
       
   594                 //accessory is allready connected -> no need to do anything
       
   595                 }
       
   596             else
       
   597                 {
       
   598                 //accessory was detected, but now it is connected
       
   599                 PromoteToConnectedL( aGenericID );
       
   600                 }
       
   601             }
       
   602             break;
       
   603 
       
   604         case EAccSrvDetect:
       
   605             {
       
   606             if( isConnected )
       
   607                 {
       
   608                 //accessory was connected, but now it is detected
       
   609                 LowerToDetectedL( aGenericID );
       
   610                 }
       
   611             else
       
   612                 {
       
   613                 //Accessory remains in detected state.
       
   614                 }
       
   615             }
       
   616             break;
       
   617 
       
   618         default:
       
   619             {
       
   620             TRACE_ASSERT_ALWAYS;//Logigal error in connection/detection handling
       
   621             }
       
   622             break;
       
   623         }
       
   624 
       
   625     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectionChangeValidationL - return void" );
       
   626     }
       
   627 
       
   628 // -----------------------------------------------------------------------------
       
   629 // CAccSrvConnectionController::PromoteToConnectedL
       
   630 // -----------------------------------------------------------------------------
       
   631 //
       
   632 void CAccSrvConnectionController::PromoteToConnectedL(
       
   633     const TAccPolGenericID& aGenericID )
       
   634     {
       
   635     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::PromoteToConnectedL()" );
       
   636 
       
   637     iServerModel->RemovePhysicalConnection( aGenericID ); //Move GID from physical connection array
       
   638     iServerModel->AddConnectionL( aGenericID );            //to connection array
       
   639 
       
   640     iNotificationQueue->CompleteControlMessageL( ENewAccessoryConnected,
       
   641                                                  KErrNone,
       
   642                                                  aGenericID.UniqueID() );
       
   643 
       
   644     iConnectionStatusHandler->IssueRequest();
       
   645 
       
   646     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::PromoteToConnectedL - return void" );
       
   647     }
       
   648 
       
   649 // -----------------------------------------------------------------------------
       
   650 // CAccSrvConnectionController::LowerToDetectedL
       
   651 // -----------------------------------------------------------------------------
       
   652 //
       
   653 void CAccSrvConnectionController::LowerToDetectedL(
       
   654     const TAccPolGenericID& aGenericID )
       
   655     {
       
   656     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::LowerToDetectedL()" );
       
   657 
       
   658     iServerModel->RemoveConnectionL( aGenericID, EFalse );//Move GID from connection array to physical connection array
       
   659     iServerModel->AddPhysicalConnectionL( aGenericID );
       
   660 
       
   661     iNotificationQueue->CompleteControlMessageL( EAccessoryDisconnected,
       
   662                                                  KErrNone,
       
   663                                                  aGenericID.UniqueID() );
       
   664                                                  
       
   665     iConnectionStatusHandler->IssueRequest();
       
   666 
       
   667     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::LowerToDetectedL - return void" );
       
   668     }
       
   669 
       
   670 // -----------------------------------------------------------------------------
       
   671 // CAccSrvConnectionController::HandleDisconnectL
       
   672 // -----------------------------------------------------------------------------
       
   673 //
       
   674 void CAccSrvConnectionController::HandleDisconnectL(
       
   675     const TAccPolGenericID& aGenericID )
       
   676     {
       
   677     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleDisconnectL()" );
       
   678 
       
   679 	//check is there connection handling ongoing for unique id
       
   680 	TInt index( KErrNotFound);
       
   681 	for ( TInt i(0); i < iConnectionHandler.Count(); i++ )
       
   682 	    {
       
   683 		if ( aGenericID.UniqueID() == 
       
   684              iConnectionHandler.operator[]( i )->UniqueIDForConnectionHandler() )
       
   685 		    {
       
   686 			index = i;
       
   687 			break;
       
   688 	    	}
       
   689 	    }
       
   690 
       
   691 	//cancel current connection handling and remove connection handler
       
   692 	if ( index >= 0 )
       
   693 	    {
       
   694 		iConnectionHandler.operator[]( index )->CancelConnectionHandling();
       
   695         iConnectionHandler.operator[]( index )->ResetHandler();
       
   696         }
       
   697 
       
   698     iServerModel->RemoveASYThreadID( aGenericID.UniqueID() );//Remove ThreadID GID mapping
       
   699 
       
   700     iServerModel->RemoveConnectionL( aGenericID, ETrue/*store last disconnect accessory*/ );
       
   701     iServerModel->RemovePhysicalConnection( aGenericID );
       
   702 
       
   703     iWiredConnPublisher->HandleDisconnectL( aGenericID );
       
   704 
       
   705     iNotificationQueue->CompleteControlMessageL( EAccessoryDisconnected,
       
   706                                                  KErrNone,
       
   707                                                  aGenericID.UniqueID() );
       
   708 
       
   709     //Remove names/values from capability storage
       
   710 	iServerModel->CapabilityStorage().RemoveCapabilityList( 
       
   711         iServerModel->LastDisconnectedAccessory() );
       
   712 
       
   713     iServerModel->ClearLastDisconnectedAccessory();
       
   714 
       
   715     iConnectionStatusHandler->IssueRequest();
       
   716 
       
   717 #ifdef _DEBUG
       
   718     //Check how many accessories are allready connected
       
   719     TAccPolGenericIDArray genericIDArray;
       
   720     iServerModel->CurrentConnectionStatusL( genericIDArray );
       
   721     COM_TRACE_1( "[AccFW:AccServer] CAccSrvConnectionController::HandleDisconnectL - Still connected accesories:%d", genericIDArray.Count() );
       
   722 #endif // _DEBUG
       
   723 
       
   724     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleDisconnectL - return void" );
       
   725     }
       
   726 
       
   727 // -----------------------------------------------------------------------------
       
   728 // CAccSrvConnectionController::HandleConnectionStatusChangedL
       
   729 // -----------------------------------------------------------------------------
       
   730 //
       
   731 void CAccSrvConnectionController::HandleConnectionStatusChangedL()
       
   732     {
       
   733     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectionStatusChangedL()" );
       
   734 
       
   735     TAccPolGenericIDArray genericIDArray;
       
   736 
       
   737     iServerModel->CurrentConnectionStatusL( genericIDArray );
       
   738     iNotificationQueue->CompleteControlMessageL( EAccessoryConnectionStatusChanged,
       
   739                                                  KErrNone,
       
   740                                                  KErrNotFound );
       
   741     iSettingsHandler->ConnectionStatusChangedL( genericIDArray );
       
   742 
       
   743     // Able to show notes
       
   744     iShowNotes = ETrue;
       
   745     iModeHandler->IssueRequest();
       
   746 
       
   747     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleConnectionStatusChangedL - return void" );
       
   748     }
       
   749     
       
   750 // -----------------------------------------------------------------------------
       
   751 // CAccSrvConnectionController::HandleAccessoryModeChangedL
       
   752 // -----------------------------------------------------------------------------
       
   753 //
       
   754 void CAccSrvConnectionController::HandleAccessoryModeChangedL(
       
   755     TInt aDbId,
       
   756     TBool aAudioOutputStatus )
       
   757     {
       
   758     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL()" );
       
   759 
       
   760     TAccPolGenericIDArray genericIDArray;
       
   761     TAccPolAccessoryMode accMode;
       
   762 
       
   763     iServerModel->CurrentConnectionStatusL( genericIDArray );
       
   764 
       
   765     accMode = iPolicy->ResolveAccessoryModeL( genericIDArray, 
       
   766                                               aDbId, 
       
   767                                               aAudioOutputStatus, 
       
   768                                               iServerModel->AccessoryMode(), 
       
   769                                               iServerModel->UniqueID() );
       
   770 
       
   771     if( iServerModel->SetAccessoryMode( accMode, aDbId ) ) //Store accessory mode
       
   772         {
       
   773         if( iModeHandler->IsActive() || iConnectionStatusHandler->IsActive() )
       
   774             {
       
   775             //Model will be updated after pending ModeHandler or ConnectionStatusHandler is executed.
       
   776             //This Mode update can de filtered out for that reason.
       
   777             COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL - Mode update filtered" );
       
   778             }
       
   779         else
       
   780             {
       
   781             iNotificationQueue->CompleteControlMessageL( EAccessoryModeChanged,
       
   782                                                          KErrNone,
       
   783                                                          KErrNotFound );
       
   784             }
       
   785 	
       
   786 		    COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL - Show information note!" );			
       
   787 
       
   788 		    TBool showNote( EFalse );
       
   789 		    TInt noteValue( 0 );
       
   790 			
       
   791 		    switch ( accMode.iAccessoryMode )
       
   792 		      {
       
   793 			    case EAccModeWiredHeadset:
       
   794 			    case EAccModeWiredCarKit:
       
   795 			    case EAccModeTextDevice:
       
   796 			    case EAccModeLoopset:
       
   797 			    case EAccModeMusicStand:
       
   798 			    case EAccModeTVOut:
       
   799 			    case EAccModeHDMI:
       
   800 			    case EAccModeHeadphones:
       
   801 			    {
       
   802 				  TAccPolGenericID genericID;
       
   803 				  iServerModel->GetLastConnectedAccessoryL( genericID );
       
   804 				  TUint32 num = genericID.SubblockCaps();
       
   805 				  if( num & KSBAudioSubblock )
       
   806 				      {
       
   807 					    CCapValue* capValue = iServerModel->CapabilityStorage().GetCapability( genericID, KAccIntegratedAudioInput );
       
   808 					    if( iInformationNoteDefault )
       
   809 						      {
       
   810 						      noteValue = iInformationNoteDefault;
       
   811 						      showNote = ETrue;
       
   812 						      }
       
   813 					    if( !capValue && !iInformationNoteDefault )
       
   814 						      {
       
   815 						      showNote = ETrue;
       
   816 						      }
       
   817 					    }
       
   818 				    else
       
   819 					    {
       
   820 					    if( iInformationNoteDefault )
       
   821 						      {
       
   822 						      noteValue = iInformationNoteDefault;
       
   823 						      showNote = ETrue;
       
   824 						      }
       
   825 					    }
       
   826 				  }
       
   827 			    break;			  
       
   828 			    default:
       
   829 			    {
       
   830 				  showNote = EFalse;				
       
   831 			    }
       
   832 			    break;
       
   833 		    }
       
   834 
       
   835 		    if ( showNote && iShowNotes )
       
   836 		        {		    	
       
   837 		        if( iServerModel->IdleDetected() )
       
   838 		            {
       
   839                     if ( iNotifier.Handle() )
       
   840                         {
       
   841                         iNotifier.Close();
       
   842                         }
       
   843 			        if( IsActive() )
       
   844 			            {
       
   845     		            Cancel();
       
   846     		            }
       
   847 
       
   848 			        //connect to notifier
       
   849 		            COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL - notifier.Connect" );
       
   850 		            TInt err = iNotifier.Connect();
       
   851 		            if(err != KErrNone)
       
   852 		                {
       
   853 		                //ok if cannot show note, this is possible during boot
       
   854 		                COM_TRACE_1( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL - Couldn't connect to notifier: err = %d", err);	
       
   855 		                }
       
   856 		            else
       
   857 		                {
       
   858 		                TPckg<TInt> valuePckg( noteValue );
       
   859 		                iStatus = KRequestPending;
       
   860                         iNotifier.StartNotifierAndGetResponse( 
       
   861                                         iStatus, 
       
   862                                         KAccFwUiNoteNotifierUid, 
       
   863                                         valuePckg, 
       
   864                                         iReplyPck);
       
   865                         SetActive();
       
   866     		            }
       
   867     		        }
       
   868 		        //reset values
       
   869 	            iShowNotes = EFalse;
       
   870 			    iInformationNoteUID = 0;
       
   871 			    iInformationNoteDefault = 0;			      
       
   872     		    }
       
   873         }        
       
   874     else
       
   875         {
       
   876         // Accessory Mode was allready up-to-date,
       
   877         // no need to complete Accessory Mode changed notification.
       
   878         COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL - Mode allready up-to-date" );
       
   879         }
       
   880 
       
   881     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::HandleAccessoryModeChangedL - return void" );
       
   882     }
       
   883 
       
   884 // -----------------------------------------------------------------------------
       
   885 // CAccSrvConnectionController::SetDefaultAccessoryInformation
       
   886 // -----------------------------------------------------------------------------
       
   887 //
       
   888 void CAccSrvConnectionController::SetDefaultAccessoryInformation(
       
   889     const TAccPolGenericID& aGenericID, 
       
   890 	const TUint32 aDefaultValue )
       
   891     {
       
   892 	COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::SetDefaultAccessoryInformation()" );
       
   893 	iInformationNoteUID = aGenericID.UniqueID();
       
   894 	iInformationNoteDefault = aDefaultValue;
       
   895     }
       
   896 
       
   897 // -----------------------------------------------------------------------------
       
   898 // CAccSrvConnectionController::RunL
       
   899 // -----------------------------------------------------------------------------
       
   900 //
       
   901 void CAccSrvConnectionController::RunL()
       
   902     {
       
   903     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::RunL()" );
       
   904     
       
   905     iNotifier.Close();
       
   906     }
       
   907 
       
   908 // -----------------------------------------------------------------------------
       
   909 // CAccSrvConnectionController::DoCancel
       
   910 // -----------------------------------------------------------------------------
       
   911 //
       
   912 void CAccSrvConnectionController::DoCancel()
       
   913     {
       
   914     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::DoCancel()" );
       
   915 
       
   916     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::DoCancel - return void" );
       
   917     }
       
   918 
       
   919 // -----------------------------------------------------------------------------
       
   920 // CAccSrvConnectionController::RegisterControlMessageL
       
   921 // -----------------------------------------------------------------------------
       
   922 //
       
   923 void CAccSrvConnectionController::RegisterControlMessageL( TMsgID aMsgID,
       
   924                                                            MQueueObserver* aCallback )
       
   925     {
       
   926     iNotificationQueue->RegisterControlMessage( aMsgID, aCallback );
       
   927     }
       
   928 
       
   929 // -----------------------------------------------------------------------------
       
   930 // CAccSrvConnectionController::CancelControlMessageL
       
   931 // -----------------------------------------------------------------------------
       
   932 //
       
   933 void CAccSrvConnectionController::CancelControlMessage( TMsgID aMsgID,
       
   934                                                         MQueueObserver* aCallback )
       
   935     {
       
   936     iNotificationQueue->CancelControlMessage( aMsgID, aCallback );
       
   937     }
       
   938 
       
   939 // -----------------------------------------------------------------------------
       
   940 // CAccSrvConnectionController::HandleASYCommsL
       
   941 // -----------------------------------------------------------------------------
       
   942 //
       
   943 TInt CAccSrvConnectionController::HandleASYCommsL( 
       
   944     TProcessCmdId aCmdId,
       
   945     TASYCommandParamRecord& aASYCommandParamRecord,
       
   946     TPtr8* aPtrBuf )
       
   947     {
       
   948     return iASYProxyHandler->HandleASYCommsL( aCmdId,
       
   949                                               aASYCommandParamRecord,
       
   950                                               aPtrBuf );
       
   951     }
       
   952 
       
   953 // -----------------------------------------------------------------------------
       
   954 // CAccSrvConnectionController::ServerModelHandle
       
   955 // -----------------------------------------------------------------------------
       
   956 //
       
   957 CAccSrvServerModel& CAccSrvConnectionController::ServerModelHandle()
       
   958     {
       
   959     return CAccSrvConnectionController::ServerModel();
       
   960     }
       
   961 
       
   962 // -----------------------------------------------------------------------------
       
   963 // CAccSrvConnectionController::InitializeConnectionStatusL
       
   964 // -----------------------------------------------------------------------------
       
   965 //
       
   966 void CAccSrvConnectionController::InitializeConnectionStatusL()
       
   967     {
       
   968     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::InitializeConnectionStatusL()" );
       
   969     iNotificationQueue->CompleteControlMessageL( 
       
   970         EAccessoryModeAsyncChanged, KErrNone, KErrNotFound );
       
   971     iNotificationQueue->CompleteControlMessageL( 
       
   972         EAccessoryConnectionStatusAsyncChanged, KErrNone, KErrNotFound );
       
   973     COM_TRACE_( "[AccFW:AccServer] CAccSrvConnectionController::InitializeConnectionStatusL - return void" );
       
   974     }
       
   975 
       
   976 //  End of File