upnpavcontroller/upnpavcontrollerserver/src/upnpavcontrollerserver.cpp
changeset 0 7f85d04be362
child 34 eab116a48b80
child 38 5360b7ddc251
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2006 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:      AVController server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDE FILES
       
    25 // System
       
    26 #include <e32svr.h>
       
    27 #include <f32file.h> 
       
    28 
       
    29 // upnp stack
       
    30 #include <upnpdevice.h>
       
    31 
       
    32 // upnpframework / avcontroller api
       
    33 #include "upnpavcontrollerglobals.h"
       
    34 
       
    35 // upnpframework / internal api's
       
    36 #include "upnpconnectionmonitor.h"
       
    37 
       
    38 #include "upnpsecaccesscontroller.h"
       
    39 
       
    40 
       
    41 // avcontroller server internal
       
    42 #include "upnpavcontrollerserver.h"
       
    43 #include "upnpavcontrollersession.h"
       
    44 
       
    45 #include <upnpsettings.h>
       
    46 
       
    47 #include "upnpavcontrolpoint.h"
       
    48 
       
    49 
       
    50 #include "upnpavdispatcher.h"
       
    51 #include "upnpdevicerepository.h"
       
    52 #include "upnpavdeviceextended.h"
       
    53 #include "upnpaverrorhandler.h"
       
    54 
       
    55 _LIT( KComponentLogfile, "upnpavcontrollerserver.txt" );
       
    56 #include "upnplog.h"
       
    57 
       
    58 // CONSTANTS
       
    59 _LIT8( KMediaServer,                    "MediaServer" );
       
    60 _LIT8( KUPnPRootDevice,                 "upnp:rootdevice" );
       
    61 
       
    62 const TInt KMaxDepth = 4;
       
    63 const TInt KMaxDeviceCount = 6;
       
    64 
       
    65 const TUint myRangeCount = 3;
       
    66 
       
    67 const TInt myRanges[ myRangeCount ] = 
       
    68     {
       
    69     0, // numbers 0-18
       
    70     18, // numbers 18-81 
       
    71     81 // numbers 81-KMaxInt
       
    72     };
       
    73 
       
    74 const TUint8 myElementsIndex[ myRangeCount ] = 
       
    75     {
       
    76     0, 
       
    77     1, 
       
    78     CPolicyServer::ENotSupported
       
    79     };
       
    80     
       
    81 const CPolicyServer::TPolicyElement myElements[] = 
       
    82     {
       
    83     {_INIT_SECURITY_POLICY_C3(ECapabilityNetworkServices,
       
    84         ECapabilityReadUserData, ECapabilityWriteUserData ),
       
    85         CPolicyServer::EFailClient },
       
    86     {_INIT_SECURITY_POLICY_C1(ECapabilityNetworkServices),
       
    87         CPolicyServer::EFailClient}
       
    88     };
       
    89     
       
    90 const CPolicyServer::TPolicy myPolicy =
       
    91     {
       
    92     CPolicyServer::EAlwaysPass, //specifies all connect attempts should pass
       
    93     myRangeCount,                   
       
    94     myRanges,
       
    95     myElementsIndex,
       
    96     myElements,
       
    97     };
       
    98 
       
    99 // ============================ MEMBER FUNCTIONS ============================
       
   100 
       
   101 // --------------------------------------------------------------------------
       
   102 // CUpnpAVControllerServer::CUpnpAVControllerServer
       
   103 // See upnpavcontrollerserver.h
       
   104 // --------------------------------------------------------------------------
       
   105 CUpnpAVControllerServer::CUpnpAVControllerServer( TInt aPriority ):
       
   106     CPolicyServer( aPriority, myPolicy ),
       
   107     iShutdownTimeoutValue( KTimerCycle10 ),
       
   108     iServerState( EStateUndefined )
       
   109     {    
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------
       
   113 // CUpnpAVControllerServer::ConstructL
       
   114 // See upnpavcontrollerserver.h
       
   115 // --------------------------------------------------------------------------
       
   116 void CUpnpAVControllerServer::ConstructL()
       
   117     {
       
   118     __LOG( "CUpnpAVControllerServer::ConstructL" );
       
   119                                
       
   120     iDispatcher = CUPnPAVDispatcher::NewL( *this );
       
   121     
       
   122     iServerState = EStateStartingUp;
       
   123 
       
   124     CUpnpSettings* settings = CUpnpSettings::NewL( KCRUidUPnPStack );
       
   125     settings->Get( CUpnpSettings::KUPnPStackIapId, iIAP );
       
   126     delete settings;    
       
   127 
       
   128     User::LeaveIfError( iMediaServer.Connect() );
       
   129     iMonitor = CUPnPConnectionMonitor::NewL( *this, iIAP );
       
   130     
       
   131     StartL( KAVControllerName );   
       
   132     
       
   133     iServerTimer = CUPnPAVTimer::NewL( *this,
       
   134         CUPnPAVTimer::ETimerServerShutdown );
       
   135     
       
   136     iServerTimer->Start( iShutdownTimeoutValue );
       
   137     
       
   138     iMSTimer = CUPnPAVTimer::NewL( *this, CUPnPAVTimer::ETimerMediaServer );
       
   139     __LOG( "CUpnpAVControllerServer::ConstructL - Finished" );
       
   140     }
       
   141 
       
   142 // --------------------------------------------------------------------------
       
   143 // CUpnpAVControllerServer::StartUpL
       
   144 // See upnpavcontrollerserver.h
       
   145 // --------------------------------------------------------------------------
       
   146 void CUpnpAVControllerServer::StartUpL()
       
   147     {
       
   148     __LOG( "CUpnpAVControllerServer::StartUpL" );
       
   149 
       
   150     TInt error = KErrNone;
       
   151     if( iServerState == EStateStartingUp )
       
   152         {
       
   153         __LOG( "StartUpL - Starting up" );
       
   154               
       
   155         
       
   156         if( !iAVControlPoint )
       
   157             {
       
   158             __LOG( "CUpnpAVControllerServer::StartUpL - CP" );
       
   159 
       
   160             TRAP( error, iAVControlPoint = CUpnpAVControlPoint::NewL( 
       
   161                                                                *iDispatcher ));
       
   162             // If operation fails for some reason , the 10 second timeout 
       
   163             // is completely useless and wrong in this case. 
       
   164             // The server should be shut down immediately   
       
   165             if( error != KErrNone )
       
   166                 {
       
   167                 iShutdownTimeoutValue = 0;   
       
   168                 User::Leave( error );
       
   169                 }
       
   170             }
       
   171         if( !iDeviceRepository )
       
   172             {
       
   173             iDeviceRepository = CUPnPDeviceRepository::NewL( *iAVControlPoint );
       
   174             }
       
   175         iServerState = EStateRunning;  
       
   176         }
       
   177     else if( iServerState == EStateShuttingDown )
       
   178         {
       
   179         __LOG( "StartUpL - Wlan disconnected or shutting down, leave" );
       
   180         User::Leave( KErrDisconnected );
       
   181         }
       
   182     else
       
   183         {
       
   184         __LOG( "StartUpL - Server running" );
       
   185         }        
       
   186 
       
   187     __LOG( "StartUpL - Completed" );    
       
   188     }
       
   189 
       
   190 void CUpnpAVControllerServer::CancelStartUp()
       
   191     {
       
   192     __LOG( "CUpnpAVControllerServer::CancelStartUp" );
       
   193     
       
   194     // Cancel can occur only when the av controller instance is deleted
       
   195     // right after the asyncronous part of the construction has been
       
   196     // started. There is no proper way to cancel, but we can set the
       
   197     // shutdown timer to 0, so the server will shutdown immidiately.
       
   198     
       
   199     if( iSessionCount <= 1 )
       
   200         {
       
   201         iShutdownTimeoutValue = 0;    
       
   202         }  
       
   203     }
       
   204 
       
   205 // --------------------------------------------------------------------------
       
   206 // CUpnpAVControllerServer::NewLC
       
   207 // See upnpavcontrollerserver.h
       
   208 // --------------------------------------------------------------------------
       
   209 CUpnpAVControllerServer* CUpnpAVControllerServer::NewLC()
       
   210     {
       
   211     CUpnpAVControllerServer* self = new( ELeave )
       
   212         CUpnpAVControllerServer( EPriorityNormal );   
       
   213     CleanupStack::PushL( self );
       
   214     self->ConstructL();
       
   215     return self;
       
   216     }
       
   217     
       
   218 // --------------------------------------------------------------------------
       
   219 // CUpnpAVControllerServer::~CUpnpAVControllerServer
       
   220 // See upnpavcontrollerserver.h
       
   221 // --------------------------------------------------------------------------
       
   222 CUpnpAVControllerServer::~CUpnpAVControllerServer()
       
   223     {
       
   224     __LOG( "CUpnpAVControllerServer::~CUpnpAVControllerServer" );
       
   225     
       
   226     delete iAVControlPoint;
       
   227     delete iDispatcher;
       
   228     delete iDeviceRepository;
       
   229    
       
   230     delete iMonitor;
       
   231     delete iServerTimer;
       
   232     delete iMSTimer;
       
   233 
       
   234     iMediaServer.Close();
       
   235 
       
   236     for( TInt i = 0; i < iStartMessages.Count(); i++ )
       
   237         {
       
   238         iStartMessages[ i ]->Complete( KErrCancel );
       
   239         }
       
   240     iStartMessages.ResetAndDestroy();  
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CUpnpAVControllerServer::NewSessionL
       
   245 // See upnpavcontrollerserver.h
       
   246 // --------------------------------------------------------------------------
       
   247 CSession2* CUpnpAVControllerServer::NewSessionL( const TVersion& aVersion,
       
   248     const RMessage2& aMessage ) const
       
   249     {
       
   250     __LOG( "CUpnpAVControllerServer::NewSessionL" );
       
   251     
       
   252     if( iServerState == EStateShuttingDown )
       
   253         {
       
   254         __LOG( "NewSessionL - server shutting down, no new sessions \
       
   255 are allowed at this point" );
       
   256         User::Leave( KErrDisconnected );
       
   257         }
       
   258     else if( iServerState == EStateStartingUp && iSessionCount > 0 )
       
   259         {
       
   260         __LOG( "NewSessionL - server starting up, no new sessions \
       
   261 are allowed at this point" );
       
   262         User::Leave( KErrServerBusy );
       
   263         }
       
   264         
       
   265     // Check we're the right version
       
   266     if ( !User::QueryVersionSupported( TVersion( 
       
   267             KAVControllerMajorVersionNumber,
       
   268             KAVControllerMinorVersionNumber,
       
   269             KAVControllerBuildVersionNumber ),
       
   270             aVersion ) )
       
   271         {
       
   272         User::Leave( KErrNotSupported );
       
   273         }
       
   274 
       
   275     // Make new session
       
   276     RThread client;
       
   277     aMessage.Client(client);  
       
   278     return CUpnpAVControllerSession::NewL(
       
   279         *(CUpnpAVControllerServer*)this );
       
   280     }
       
   281     
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CUpnpAVControllerServer::UPnPAVTimerCallback
       
   285 // See upnpavcontrollerserver.h
       
   286 // --------------------------------------------------------------------------
       
   287 void CUpnpAVControllerServer::UPnPAVTimerCallback(
       
   288     CUPnPAVTimer::TAVTimerType aType ) 
       
   289     {
       
   290     __LOG( "CUpnpAVControllerServer::UPnPAVTimerCallback" );
       
   291 
       
   292 
       
   293     if( aType == CUPnPAVTimer::ETimerServerShutdown )
       
   294         {
       
   295         iServerState = EStateShuttingDown;
       
   296            
       
   297         if( iMSTimer->IsActive() )
       
   298             {
       
   299             // if the Media Server timer is still running for some reason
       
   300             iMSTimer->Cancel();
       
   301             StopMediaServer();
       
   302             }
       
   303         CActiveScheduler::Stop();
       
   304         }
       
   305     else if( aType == CUPnPAVTimer::ETimerMediaServer )
       
   306         {
       
   307         if( iStartingMS )
       
   308             {
       
   309             StopMediaServer();
       
   310             TInt count = iStartMessages.Count();
       
   311             for( TInt i = 0; i < count; i++ )
       
   312                 {
       
   313                 iStartMessages[ i ]->Complete( KErrTimedOut );
       
   314                 }
       
   315             iStartMessages.ResetAndDestroy();
       
   316             
       
   317             iStartingMS = EFalse;
       
   318             }
       
   319         else // Shutting down
       
   320             {
       
   321             StopMediaServer();
       
   322             }            
       
   323         }
       
   324     else
       
   325         {
       
   326         
       
   327         }        
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // CUpnpAVControllerServer::ConnectionLost
       
   332 // See upnpavcontrollerserver.h
       
   333 // --------------------------------------------------------------------------
       
   334 void CUpnpAVControllerServer::ConnectionLost()
       
   335     {
       
   336     __LOG( "CUpnpAVControllerServer::ConnectionLost" );
       
   337     
       
   338     iShutdownTimeoutValue = 0; // Set shutdown timer value to 0, we want to
       
   339     // shut down the server immidiately after the last session has been
       
   340     // closed
       
   341     
       
   342     if( iServerState == EStateRunning && iDeviceRepository )
       
   343         {
       
   344         __LOG( "ConnectionLost - Server running" );
       
   345         iDeviceRepository->ConnectionLost();    
       
   346 
       
   347         CSession2* s;
       
   348         iSessionIter.SetToFirst(); 
       
   349         while ( ( s = iSessionIter++ ) != NULL )
       
   350             {
       
   351             CUpnpAVControllerSession* sess =
       
   352                 static_cast<CUpnpAVControllerSession*>(s);
       
   353             if( sess )
       
   354                 {
       
   355                 sess->ConnectionLost();    
       
   356                 }
       
   357             };  
       
   358         iServerState = EStateShuttingDown;
       
   359         }
       
   360     else if (iServerState == EStateStartingUp )
       
   361         {
       
   362         __LOG( "ConnectionLost - Server starting up" );
       
   363         iServerState = EStateShuttingDown;
       
   364         }    
       
   365 
       
   366     // If don't have any clients connect to server and current WLAN connection
       
   367     // is lost, we want to shut down the server immidiately.
       
   368     if ( iSessionCount <= 0 )
       
   369         {
       
   370         if ( iServerTimer->IsActive() )
       
   371             {
       
   372             iServerTimer->Cancel();
       
   373             }
       
   374         iServerTimer->Start( iShutdownTimeoutValue );
       
   375         }
       
   376     }
       
   377 
       
   378 // --------------------------------------------------------------------------
       
   379 // CUpnpAVControllerServer::RunError
       
   380 // See upnpavcontrollerserver.h
       
   381 // --------------------------------------------------------------------------
       
   382 TInt CUpnpAVControllerServer::RunError( TInt aError )
       
   383     {
       
   384     __LOG( "CUpnpAVControllerServer::RunError" );
       
   385        
       
   386     if ( aError == KErrBadDescriptor )
       
   387         {
       
   388         PanicClient( Message(), EAVControllerServerBadDescriptor );
       
   389         }
       
   390     else
       
   391         {
       
   392         Message().Complete( aError );
       
   393         }
       
   394         
       
   395     // The leave will result in an early return from CServer::RunL(),
       
   396     // skipping the call to request another message. So do that now in order
       
   397     // to keep the server running.
       
   398     ReStart();
       
   399     // Handled the error fully
       
   400     return KErrNone;
       
   401     }
       
   402 
       
   403 // --------------------------------------------------------------------------
       
   404 // CUpnpAVControllerServer::PanicClient
       
   405 // See upnpavcontrollerserver.h
       
   406 // --------------------------------------------------------------------------
       
   407 void CUpnpAVControllerServer::PanicClient(const RMessage2& aMessage,
       
   408     TAVControllerServerPanic aPanic)
       
   409     {
       
   410     __LOG( "CUpnpAVControllerServer::PanicClient" );
       
   411        
       
   412     aMessage.Panic( KAVControllerName, aPanic );
       
   413     }
       
   414 
       
   415 // --------------------------------------------------------------------------
       
   416 // CUpnpAVControllerServer::PanicServer
       
   417 // See upnpavcontrollerserver.h
       
   418 // --------------------------------------------------------------------------
       
   419 void CUpnpAVControllerServer::PanicServer(TAVControllerServerPanic aPanic)
       
   420     {
       
   421     __LOG( "CUpnpAVControllerServer::PanicServer" );
       
   422     
       
   423     User::Panic( KAVControllerName, aPanic );
       
   424     }
       
   425 
       
   426 
       
   427 // --------------------------------------------------------------------------
       
   428 // CUpnpAVControllerServer::ThreadFunctionL
       
   429 // See upnpavcontrollerserver.h
       
   430 // --------------------------------------------------------------------------
       
   431 void CUpnpAVControllerServer::ThreadFunctionL()
       
   432     {
       
   433     __LOG( "CUpnpAVControllerServer::ThreadFunctionL" );
       
   434     
       
   435     // Construct active scheduler
       
   436     CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
       
   437     CleanupStack::PushL( activeScheduler );
       
   438     // Install active scheduler
       
   439     // We don't need to check whether an active scheduler is already
       
   440     // installed
       
   441     // as this is a new thread, so there won't be one
       
   442     CActiveScheduler::Install( activeScheduler );    
       
   443     // Construct our server        
       
   444     CUpnpAVControllerServer* server = CUpnpAVControllerServer::NewLC();
       
   445     
       
   446     RProcess::Rendezvous( KErrNone );                
       
   447     // Start handling requests
       
   448     CActiveScheduler::Start();      
       
   449              
       
   450     CleanupStack::PopAndDestroy( server );  
       
   451     CleanupStack::PopAndDestroy( activeScheduler );
       
   452     
       
   453     }
       
   454 
       
   455 // --------------------------------------------------------------------------
       
   456 // CUpnpAVControllerServer::HandleEmbeddedDiscoveredDevicesL
       
   457 // See upnpavcontrollerserver.h
       
   458 // --------------------------------------------------------------------------
       
   459 void CUpnpAVControllerServer::HandleEmbeddedDiscoveredDevicesL(
       
   460     CUpnpDevice& aDevice, TInt aDepth )
       
   461     {
       
   462     __LOG( "CUpnpAVControllerServer::HandleEmbeddedDiscoveredDevicesL" );
       
   463     
       
   464     if( aDepth <= KMaxDepth && iDiscoveredDeviceCount <= KMaxDeviceCount )
       
   465         {
       
   466         RPointerArray<CUpnpDevice>& devList = aDevice.DeviceList();
       
   467         TInt count = devList.Count();
       
   468         for( TInt i = 0; i < count; i++ )
       
   469             {
       
   470             iDeviceRepository->AddDeviceL( *devList[ i ] );
       
   471             TInt sessionId = iAVControlPoint->CmProtocolInfoActionL(
       
   472                 devList[ i ]->Uuid() );
       
   473             iDiscoveredDeviceCount++;    
       
   474             
       
   475             HandleEmbeddedDiscoveredDevicesL( *devList[ i ], ++aDepth );
       
   476             }        
       
   477         }
       
   478     else
       
   479         {
       
   480         __LOG( "HandleEmbeddedDiscoveredDevicesL - max depth \
       
   481 or count reached" );
       
   482         }    
       
   483     }
       
   484 
       
   485 // --------------------------------------------------------------------------
       
   486 // CUpnpAVControllerServer::HandleEmbeddedDisappearedDevicesL
       
   487 // See upnpavcontrollerserver.h
       
   488 // --------------------------------------------------------------------------
       
   489 void CUpnpAVControllerServer::HandleEmbeddedDisappearedDevicesL(
       
   490     CUpnpDevice& aDevice, TInt aDepth )
       
   491     {
       
   492     __LOG( "CUpnpAVControllerServer::HandleEmbeddedDisappearedDevicesL" );
       
   493     
       
   494     if( aDepth <= KMaxDepth && iDisappearedDeviceCount <= KMaxDeviceCount )
       
   495         {
       
   496         RPointerArray<CUpnpDevice>& devList = aDevice.DeviceList();
       
   497         TInt count = devList.Count();
       
   498         for( TInt i = 0; i < count; i++ )
       
   499             {
       
   500             CUpnpAVDeviceExtended& ext = iDeviceRepository->FindDeviceL(
       
   501                 devList[ i ]->Uuid() );
       
   502             
       
   503             CSession2* s;
       
   504             iSessionIter.SetToFirst(); 
       
   505             while ( ( s = iSessionIter++ ) != NULL )
       
   506                 {
       
   507                 CUpnpAVControllerSession* sess =
       
   508                     static_cast<CUpnpAVControllerSession*>(s);
       
   509                 if( sess )
       
   510                     {
       
   511                     sess->DeviceDisappearedL( ext );    
       
   512                     }
       
   513                 };        
       
   514 
       
   515             iDeviceRepository->Remove( ext.Uuid() );
       
   516             
       
   517             iDisappearedDeviceCount++;    
       
   518             
       
   519             HandleEmbeddedDisappearedDevicesL( *devList[ i ], ++aDepth );
       
   520             }        
       
   521         }
       
   522     else
       
   523         {
       
   524         __LOG( "HandleEmbeddedDisappearedDevicesL - max depth \
       
   525 or count reached" );
       
   526         }    
       
   527     }
       
   528 
       
   529 // --------------------------------------------------------------------------
       
   530 // CUpnpAVControllerServer::StopMediaServer
       
   531 // See upnpavcontrollerserver.h
       
   532 // --------------------------------------------------------------------------
       
   533 void CUpnpAVControllerServer::StopMediaServer()
       
   534     {
       
   535     __LOG( "CUpnpAVControllerServer::StopMediaServer" );
       
   536     
       
   537     if( iShutdownTimeoutValue )
       
   538         {
       
   539         __LOG( "StopMediaServer - normal shutdown" );
       
   540         iMediaServer.Stop( RUpnpMediaServerClient::EStopNormal );
       
   541         }
       
   542     else
       
   543         {
       
   544         __LOG( "StopMediaServer - silent shutdown" );
       
   545         iMediaServer.Stop( RUpnpMediaServerClient::EStopSilent );
       
   546         }
       
   547     }
       
   548 
       
   549 // --------------------------------------------------------------------------
       
   550 // CUpnpAVControllerServer::ThreadFunction
       
   551 // See upnpavcontrollerserver.h
       
   552 // --------------------------------------------------------------------------
       
   553 TInt CUpnpAVControllerServer::ThreadFunction()
       
   554     {
       
   555     __LOG( "CUpnpAVControllerServer::ThreadFunction" );
       
   556     
       
   557     __UHEAP_MARK;
       
   558     
       
   559     User::RenameThread(KAVControllerThreadName);
       
   560     
       
   561     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   562     if ( !(cleanupStack) )
       
   563         {
       
   564         PanicServer( EAVControllerServerCreateTrapCleanup );
       
   565         }
       
   566 
       
   567     TRAPD( err, ThreadFunctionL() );
       
   568     if ( err != KErrNone )
       
   569         {
       
   570         __LOG1( "ThreadFunction, creation failed: %d", err );
       
   571         //PanicServer( EAVControllerServerSrvCreateServer );
       
   572         }
       
   573 
       
   574     delete cleanupStack;
       
   575     cleanupStack = NULL;
       
   576        
       
   577     __UHEAP_MARKEND;
       
   578 
       
   579     return err;
       
   580     }
       
   581 
       
   582 // --------------------------------------------------------------------------
       
   583 // CUpnpAVControllerServer::IncrementSessions
       
   584 // See upnpavcontrollerserver.h
       
   585 // --------------------------------------------------------------------------
       
   586 void CUpnpAVControllerServer::IncrementSessions() 
       
   587     {
       
   588     __LOG( "CUpnpAVControllerServer::IncrementSessions" );
       
   589     
       
   590     iSessionCount++;
       
   591     if( iServerTimer->IsActive() )
       
   592         {
       
   593         iServerTimer->Cancel();
       
   594         __LOG( "IncrementSessions - make a search" );
       
   595         if( iAVControlPoint )
       
   596             {
       
   597             TRAP_IGNORE( iAVControlPoint->SearchL( KUPnPRootDevice ) );
       
   598             }
       
   599         }   
       
   600     }
       
   601 
       
   602 // --------------------------------------------------------------------------
       
   603 // CUpnpAVControllerServer::DecrementSessions
       
   604 // See upnpavcontrollerserver.h
       
   605 // --------------------------------------------------------------------------
       
   606 void CUpnpAVControllerServer::DecrementSessions()
       
   607     {
       
   608     __LOG( "CUpnpAVControllerServer::DecrementSessions" );
       
   609     
       
   610     iSessionCount--;
       
   611     if ( iSessionCount <= 0 )
       
   612         {
       
   613         if( iServerTimer->IsActive() )
       
   614             {
       
   615             iServerTimer->Cancel();
       
   616             }
       
   617         iServerTimer->Start( iShutdownTimeoutValue );
       
   618         }
       
   619     }
       
   620 
       
   621 // --------------------------------------------------------------------------
       
   622 // CUpnpAVControllerServer::DeviceDiscoveredL
       
   623 // See upnpavcontrollerserver.h
       
   624 // --------------------------------------------------------------------------
       
   625 void CUpnpAVControllerServer::DeviceDiscoveredL( CUpnpDevice& aDevice )
       
   626     {
       
   627     __LOG( "CUpnpAVControllerServer::DeviceDiscoveredL" );
       
   628 
       
   629     if( aDevice.Local() && aDevice.DeviceType().Find( KMediaServer )
       
   630         != KErrNotFound )
       
   631         {
       
   632         // It's the local S60 MS
       
   633         
       
   634         if( iStartingMS )
       
   635             {
       
   636             iMSTimer->Cancel();
       
   637             }   
       
   638         CUpnpSecAccessController* accessController = 
       
   639             CUpnpSecAccessController::NewL();
       
   640             __LOG( "CUpnpAVControllerServer::DeviceDiscoveredL \
       
   641 adding the local media server IP to the list of authorized addresses." );
       
   642 
       
   643         accessController->AddAllowedAddress( aDevice.Address() );
       
   644         delete accessController; 
       
   645         accessController = NULL;
       
   646         }
       
   647         
       
   648     iDeviceRepository->AddDeviceL( aDevice );
       
   649     TInt sessionId = iAVControlPoint->CmProtocolInfoActionL(
       
   650         aDevice.Uuid() );
       
   651     iDiscoveredDeviceCount = 1; // First (root) device
       
   652 
       
   653     }
       
   654 
       
   655 // --------------------------------------------------------------------------
       
   656 // CUpnpAVControllerServer::DeviceDisappearedL
       
   657 // See upnpavcontrollerserver.h
       
   658 // --------------------------------------------------------------------------
       
   659 void CUpnpAVControllerServer::DeviceDisappearedL( CUpnpDevice& aDevice )
       
   660     {
       
   661     __LOG( "CUpnpAVControllerServer::DeviceDisappearedL" );
       
   662     
       
   663     if( aDevice.Local() && aDevice.DeviceType().Find( KMediaServer )
       
   664         != KErrNotFound )
       
   665         {  
       
   666         // It's the local S60 MS
       
   667         iMediaServerOnline = EFalse;
       
   668         }
       
   669     
       
   670     // Get a corresponding device from the device repository
       
   671     CUpnpAVDeviceExtended& tmp = iDeviceRepository->FindDeviceL(
       
   672         aDevice.Uuid() ); 
       
   673     
       
   674     // Let the clients know about the disappeared device
       
   675     CSession2* s;
       
   676     iSessionIter.SetToFirst(); 
       
   677     while ( ( s = iSessionIter++ ) != NULL )
       
   678         {
       
   679         CUpnpAVControllerSession* sess =
       
   680             static_cast<CUpnpAVControllerSession*>(s);
       
   681         if( sess )
       
   682             {
       
   683             sess->DeviceDisappearedL( tmp );    
       
   684             }
       
   685         };        
       
   686             // Remove from the device repository
       
   687     iDeviceRepository->Remove( aDevice.Uuid() );
       
   688     iDisappearedDeviceCount = 1;
       
   689     
       
   690     }
       
   691 
       
   692 // --------------------------------------------------------------------------
       
   693 // CUpnpAVControllerServer::DeviceDisappearedL
       
   694 // See upnpavcontrollerserver.h
       
   695 // --------------------------------------------------------------------------
       
   696 void CUpnpAVControllerServer::DeviceDisappearedL( const TDesC8& aUuid )
       
   697     {
       
   698     __LOG( "CUpnpAVControllerServer::DeviceDisappearedL uid" );
       
   699     // Get a corresponding device from the device repository
       
   700     CUpnpAVDeviceExtended& tmp = iDeviceRepository->FindDeviceL(
       
   701         aUuid ); 
       
   702     
       
   703     // Let the clients know about the disappeared device
       
   704     CSession2* s;
       
   705     iSessionIter.SetToFirst(); 
       
   706     while ( ( s = iSessionIter++ ) != NULL )
       
   707         {
       
   708         CUpnpAVControllerSession* sess =
       
   709             static_cast<CUpnpAVControllerSession*>( s );
       
   710         if ( sess )
       
   711             {
       
   712             sess->DeviceDisappearedL( tmp );    
       
   713             }
       
   714         }       
       
   715     // Remove from the device repository
       
   716     iDeviceRepository->Remove( aUuid );
       
   717     iDisappearedDeviceCount = 1;
       
   718     __LOG( "CUpnpAVControllerServer::DeviceDisappearedL uid End" );
       
   719     }
       
   720 
       
   721 // --------------------------------------------------------------------------
       
   722 // CUpnpAVControllerServer::StartMediaServerL
       
   723 // See upnpavcontrollerserver.h
       
   724 // --------------------------------------------------------------------------
       
   725 void CUpnpAVControllerServer::StartMediaServerL( const RMessage2& aMessage )
       
   726     {
       
   727     __LOG( "CUpnpAVControllerServer::StartMediaServerL" );
       
   728 
       
   729     if( iMediaServerOnline )
       
   730         {
       
   731         // Started already, complete the msg
       
   732         iMSTimer->Cancel();
       
   733         aMessage.Complete( EAVControllerStartMediaServerCompleted );
       
   734         iServerUserCount++;
       
   735         }
       
   736     else
       
   737         {
       
   738         // Start the media server and timer
       
   739         if( iStartMessages.Count() > 0 )
       
   740             {
       
   741             RMessage2* message = new (ELeave) RMessage2( aMessage );
       
   742             iStartMessages.AppendL( message );
       
   743             }
       
   744         else
       
   745             {
       
   746             // Check if the stack's security is enabled
       
   747             TBool upnpSecurityEnabled = EFalse;
       
   748             TRAPD( secCheckError, upnpSecurityEnabled = 
       
   749                 CUpnpSecAccessController::IsMediaServerSecurityEnabledL() );
       
   750 
       
   751             // If the security is not enabled, enable it now
       
   752             if( secCheckError == KErrNone &&
       
   753                 !upnpSecurityEnabled )
       
   754                 {
       
   755                 TRAP_IGNORE( 
       
   756                     CUpnpSecAccessController::EnableMediaServerSecurityL() );
       
   757                 }
       
   758 
       
   759             RMessage2* message = new(ELeave) RMessage2( aMessage );
       
   760             iStartMessages.AppendL( message );
       
   761             User::LeaveIfError( iMediaServer.Start() );
       
   762             iMSTimer->Start( iShutdownTimeoutValue );
       
   763             iStartingMS = ETrue;
       
   764             }    
       
   765         }            
       
   766     }
       
   767     
       
   768 // --------------------------------------------------------------------------
       
   769 // CUpnpAVControllerServer::CancelStartMediaServerL
       
   770 // See upnpavcontrollerserver.h
       
   771 // --------------------------------------------------------------------------
       
   772 void CUpnpAVControllerServer::CancelStartMediaServerL(
       
   773     const RMessage2& aMessage )
       
   774     {
       
   775     __LOG( "CUpnpAVControllerServer::CancelStartMediaServerL" );
       
   776 
       
   777     if( !iMSActivatedBeforeStart )
       
   778         {
       
   779         StopMediaServer();
       
   780         }
       
   781 
       
   782     TInt count = iStartMessages.Count();
       
   783     for( TInt i = 0; i < count; i++ )
       
   784         {
       
   785         iStartMessages[ i ]->Complete( KErrCancel );
       
   786         }
       
   787     iStartMessages.ResetAndDestroy();
       
   788     iMSTimer->Cancel();
       
   789     
       
   790     aMessage.Complete( KErrNone );
       
   791         
       
   792     }
       
   793 
       
   794 // --------------------------------------------------------------------------
       
   795 // CUpnpAVControllerServer::StopMediaServerL
       
   796 // See upnpavcontrollerserver.h
       
   797 // --------------------------------------------------------------------------
       
   798 void CUpnpAVControllerServer::StopMediaServerL( const RMessage2& aMessage )
       
   799     {
       
   800     __LOG( "CUpnpAVControllerServer::StopMediaServerL" );
       
   801     
       
   802     if( iMediaServerOnline )
       
   803         {
       
   804         iServerUserCount--;
       
   805         if( iServerUserCount <= 0 )
       
   806             {
       
   807             if( !iMSActivatedBeforeStart )
       
   808                 {
       
   809                 iMSTimer->Start( iShutdownTimeoutValue );
       
   810                 }
       
   811             iServerUserCount = 0;
       
   812             }
       
   813         }
       
   814     aMessage.Complete( KErrNone );
       
   815     }
       
   816 
       
   817 // --------------------------------------------------------------------------
       
   818 // CUpnpAVControllerServer::MSServicesInUse
       
   819 // See upnpavcontrollerserver.h
       
   820 // --------------------------------------------------------------------------
       
   821 void CUpnpAVControllerServer::MSServicesInUse( const RMessage2& aMessage )
       
   822     {
       
   823     if( iServerUserCount > 0 || iStartingMS
       
   824          || iMSTimer->IsActive()
       
   825         )        
       
   826         {
       
   827         TPckg<TBool> resp0( ETrue );
       
   828         aMessage.Write( 0, resp0 );        
       
   829         }
       
   830     else
       
   831         {
       
   832         TPckg<TBool> resp0( EFalse );
       
   833         aMessage.Write( 0, resp0 );                
       
   834         }    
       
   835     aMessage.Complete( KErrNone );    
       
   836     }
       
   837     
       
   838 // --------------------------------------------------------------------------
       
   839 // CUpnpAVControllerServer::CmProtocolInfoResponse
       
   840 // See upnpavcontrollerserver.h
       
   841 // --------------------------------------------------------------------------
       
   842 void CUpnpAVControllerServer::CmProtocolInfoResponse( const TDesC8& aUuid,
       
   843     TInt /*aSessionId*/, TInt aErr, const TDesC8& aSource,
       
   844     const TDesC8& aSink )
       
   845     {
       
   846     __LOG1( "CUpnpAVControllerServer::CmProtocolInfoResponse, \
       
   847 aErr = %d", aErr );
       
   848 
       
   849     aErr = UPnPAVErrorHandler::ConvertToSymbianErrorCode( aErr,
       
   850         EUPnPConnectionManagerError );    
       
   851 
       
   852     if( aErr == KErrNone )
       
   853         {
       
   854         CUpnpAVDeviceExtended* dev = NULL;
       
   855         TRAPD( err, dev = &iDeviceRepository->AddProtocolInfoL(
       
   856             aUuid, aSource, aSink ) );
       
   857         
       
   858         if( err == KErrNone )    
       
   859             {
       
   860             // Device discovered and protocolinfo was retrieved successfully
       
   861             CSession2* s;
       
   862             iSessionIter.SetToFirst(); 
       
   863             while ( ( s = iSessionIter++ ) != NULL )
       
   864                 {
       
   865                 CUpnpAVControllerSession* sess =
       
   866                     static_cast<CUpnpAVControllerSession*>(s);
       
   867                 if( sess )
       
   868                     {
       
   869                     TRAP_IGNORE( sess->DeviceDiscoveredL( *dev ) );    
       
   870                     }
       
   871                 };        
       
   872             
       
   873             if( dev->Local() )
       
   874                 {
       
   875                 iMediaServerOnline = ETrue;
       
   876                 
       
   877                 if( iStartingMS )
       
   878                     {
       
   879                     TInt count = iStartMessages.Count();
       
   880                     for( TInt i = 0; i < count; i++ )
       
   881                         {
       
   882                         iStartMessages[ i ]->Complete(
       
   883                             EAVControllerStartMediaServerCompleted );
       
   884                         iServerUserCount++;    
       
   885                         }
       
   886                     iStartMessages.ResetAndDestroy();        
       
   887                     
       
   888                     iStartingMS = EFalse;                
       
   889                     }
       
   890                 else
       
   891                     {
       
   892                     __LOG( "Sharing was enabled before AVC server start" );
       
   893                     iMSActivatedBeforeStart = ETrue;
       
   894                     }    
       
   895                 
       
   896                 }    
       
   897             }
       
   898         else
       
   899             {
       
   900             // Could not add protocolinfo, it's invalid or corrupted
       
   901             // Device cannot be used
       
   902             HandleFailedProtocolInfoResponse( aUuid );          
       
   903             }
       
   904         }
       
   905     else
       
   906         {
       
   907         // A problem occured fetching protocolinfo
       
   908         // Device cannot be used
       
   909         HandleFailedProtocolInfoResponse( aUuid );
       
   910         }        
       
   911     }
       
   912 
       
   913 void CUpnpAVControllerServer::HandleFailedProtocolInfoResponse(
       
   914     const TDesC8& aUuid )
       
   915     {
       
   916     __LOG( "CUpnpAVControllerServer::HandleFailedProtocolInfoResponse" );
       
   917     
       
   918     CUpnpAVDeviceExtended* dev = NULL;
       
   919     TRAPD( err, dev = &iDeviceRepository->FindDeviceL( aUuid ) )
       
   920     if( err == KErrNone )
       
   921         {
       
   922         if( iStartingMS && dev->Local() )
       
   923             {
       
   924             __LOG( "HandleFailedProtocolInfoResponse - local, stop and \
       
   925 complete messages" );
       
   926             
       
   927             StopMediaServer();
       
   928             
       
   929             TInt count = iStartMessages.Count();
       
   930             for( TInt i = 0; i < count; i++ )
       
   931                 {
       
   932                 iStartMessages[ i ]->Complete( err );
       
   933                 iServerUserCount++;    
       
   934                 }
       
   935             iStartMessages.ResetAndDestroy();        
       
   936             
       
   937             iStartingMS = EFalse;
       
   938             }                           
       
   939         }
       
   940     else
       
   941         {
       
   942         // Not found, no can do
       
   943         __LOG( "HandleFailedProtocolInfoResponse - not found" );
       
   944         }    
       
   945     iDeviceRepository->Remove( aUuid );
       
   946     }
       
   947 
       
   948 // --------------------------------------------------------------------------
       
   949 // CUpnpAVControllerServer::ControlPoint
       
   950 // See upnpavcontrollerserver.h
       
   951 // --------------------------------------------------------------------------
       
   952 CUpnpAVControlPoint& CUpnpAVControllerServer::ControlPoint()
       
   953     {
       
   954     return *iAVControlPoint;
       
   955     }
       
   956 
       
   957 // --------------------------------------------------------------------------
       
   958 // CUpnpAVControllerServer::MediaServer
       
   959 // See upnpavcontrollerserver.h
       
   960 // --------------------------------------------------------------------------
       
   961 RUpnpMediaServerClient& CUpnpAVControllerServer::MediaServer()
       
   962     {
       
   963     return iMediaServer;
       
   964     }
       
   965     
       
   966 // --------------------------------------------------------------------------
       
   967 // CUpnpAVControllerServer::Dispatcher
       
   968 // See upnpavcontrollerserver.h
       
   969 // --------------------------------------------------------------------------
       
   970 CUPnPAVDispatcher& CUpnpAVControllerServer::Dispatcher()
       
   971     {
       
   972     return *iDispatcher;
       
   973     }
       
   974 
       
   975 // --------------------------------------------------------------------------
       
   976 // CUpnpAVControllerServer::DeviceRepository
       
   977 // See upnpavcontrollerserver.h
       
   978 // --------------------------------------------------------------------------
       
   979 CUPnPDeviceRepository& CUpnpAVControllerServer::DeviceRepository()
       
   980     {
       
   981     return *iDeviceRepository;
       
   982     }
       
   983 
       
   984 TInt CUpnpAVControllerServer::IAP()
       
   985     {
       
   986     return iIAP;
       
   987     }
       
   988     
       
   989 // ============================= LOCAL FUNCTIONS ============================
       
   990 
       
   991 // --------------------------------------------------------------------------
       
   992 // E32Main entry point.
       
   993 // Returns: KErrNone
       
   994 // --------------------------------------------------------------------------
       
   995 TInt E32Main()
       
   996     {
       
   997     return CUpnpAVControllerServer::ThreadFunction();
       
   998     }
       
   999 
       
  1000 // End of File
       
  1001