upnp/upnpstack/messagehandler/src/upnpmessagehandlerengine.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2  * Copyright (c) 2005-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:  Message handler engine
       
    15  *
       
    16  */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <commdb.h>
       
    20 #include <commdbconnpref.h>
       
    21 #include "upnpcons.h"
       
    22 #include "upnpmessagehandlerengine.h"
       
    23 #include "upnpssdpserver.h"
       
    24 
       
    25 #ifdef RD_UPNP_REMOTE_ACCESS
       
    26 #include "upnpipfiltering.h"
       
    27 #include "upnpipfilteringdnsquery.h"
       
    28 #endif
       
    29 
       
    30 #include "upnpssdphandlerup.h"
       
    31 #include "upnpssdphandlerbase.h"
       
    32 #include "upnpssdphandlerdown.h"
       
    33 #include "upnpconnectionmanagerproxy.h"
       
    34 
       
    35 #define KLogFile _L("UpnpMessageHandler.txt")
       
    36 #include "upnpcustomlog.h"
       
    37 
       
    38 
       
    39 // ================= MEMBER FUNCTIONS =======================
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CUpnpMessageHandlerEngine::NewLC
       
    43 // Two-phased constructor.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CUpnpMessageHandlerEngine* CUpnpMessageHandlerEngine::NewL(
       
    47     MMessageHandlerEngineObserver& aObserver )
       
    48     {
       
    49     // call the default constructor
       
    50     CUpnpMessageHandlerEngine* self = new (ELeave) CUpnpMessageHandlerEngine( aObserver );
       
    51 
       
    52     // call the default L constructor when in cleanup stack
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop( self );
       
    56 
       
    57     // returns new instance
       
    58     return self;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CUpnpMessageHandlerEngine::CUpnpMessageHandlerEngine
       
    63 // C++ default constructor can NOT contain any code, that
       
    64 // might leave.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CUpnpMessageHandlerEngine::CUpnpMessageHandlerEngine(
       
    68     MMessageHandlerEngineObserver& aObserver ) :
       
    69     iObserver(aObserver)
       
    70     {
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CUpnpMessageHandlerEngine::ConstructL
       
    75 // Symbian 2nd phase constructor can leave.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CUpnpMessageHandlerEngine::ConstructL()
       
    79     {
       
    80     LOG_FUNC_NAME;
       
    81 
       
    82     // create a socket server connection
       
    83     User::LeaveIfError( iSocketServ.Connect() );
       
    84 
       
    85     CreateSessionIdPropertyL();
       
    86 
       
    87     iSsdpHandlerUp = CUpnpSsdpHandlerUp::NewL( *this );
       
    88     iSsdpHandlerDown = CUpnpSsdpHandlerDown::NewL( *this );
       
    89     iCurrentSsdpHandler = iSsdpHandlerUp;
       
    90 
       
    91     // device library stores information about remote and local devices
       
    92     iDeviceLibrary = CUpnpDeviceLibrary::NewL( *this, 0 );
       
    93 
       
    94     iConnectionManagerProxy = CUpnpConnectionManagerProxy::NewL( iSocketServ );
       
    95     iConnectionManagerProxy->SubscribeForNetworkEventsL( this );
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CUpnpMessageHandlerEngine::CreateSessionIdPropertyL
       
   100 // Create session id property and signal id semaphore
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CUpnpMessageHandlerEngine::CreateSessionIdPropertyL()
       
   104     {
       
   105     // create RProperty used as session id counter
       
   106     TInt err = RProperty::Define( KUPnPUtilsCat, EUPnPUtilsCounter,
       
   107         RProperty::EInt );
       
   108     if ( err != KErrAlreadyExists )
       
   109         {
       
   110         User::LeaveIfError( err );
       
   111         }
       
   112 
       
   113     // there are four global widely-used semaphores in the system
       
   114     // first two semaphores are for getting a running session id
       
   115     // first semaphore is an access semaphore
       
   116     // if it's signaled, we can have access to id semaphore
       
   117     // id semaphore holds the current session id
       
   118     //
       
   119     // the second place where these semaphores are used is CUpnpHttpMessage::NewSessionIdL()
       
   120     // 
       
   121     // basically, we want to tell the CUpnpHttpMessage class that the
       
   122     // EUPnPUtilsCounter RProperty is defined and ready to use
       
   123 
       
   124     TInt result =
       
   125             iAccessSemaphore.OpenGlobal( KSessionIdAccessSemaphoreName() );
       
   126     if ( result == KErrNotFound )
       
   127         {
       
   128         result = iAccessSemaphore.CreateGlobal(
       
   129             KSessionIdAccessSemaphoreName(), 0 );
       
   130             
       
   131         if ( result != KErrNone )
       
   132             {
       
   133             LOGS( "MESSAGEHANDLER *** Session id access semaphore creation failed" );
       
   134             }
       
   135         }
       
   136     else if ( result != KErrNone )
       
   137         {
       
   138         LOGS( "MESSAGEHANDLER *** Session id access semaphore creation failed" );
       
   139         }
       
   140     User::LeaveIfError( result );
       
   141 
       
   142     iAccessSemaphore.Signal();
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CUpnpMessageHandlerEngine::~CUpnpMessageHandlerEngine
       
   147 // Destructor.
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 CUpnpMessageHandlerEngine::~CUpnpMessageHandlerEngine()
       
   151     {
       
   152     __ASSERT_DEBUG( iCurrentSsdpHandler == iSsdpHandlerUp
       
   153             || iCurrentSsdpHandler == iSsdpHandlerDown,
       
   154             User::Panic( KMessageHandler, EMessageHandlerBadState ) );
       
   155     delete iDeviceLibrary;
       
   156 
       
   157     delete iSsdpHandlerUp;
       
   158     delete iSsdpHandlerDown;
       
   159 
       
   160     delete iConnectionManagerProxy;
       
   161 
       
   162 #ifdef RD_UPNP_REMOTE_ACCESS    
       
   163     /*************   IPFiltering    *****************/
       
   164     delete iIPFilterRepository;
       
   165     /************************************************/
       
   166 #endif
       
   167 
       
   168     // close socket server connection
       
   169     iSocketServ.Close();
       
   170     iAccessSemaphore.Close();
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CUpnpMessageHandlerEngine::AdvertiseDeviceL
       
   175 // Advertise device
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 void CUpnpMessageHandlerEngine::AdvertiseDeviceL( TInt aLive,
       
   179     CUpnpDeviceLibraryElement& aElement )
       
   180     {
       
   181     iCurrentSsdpHandler->AdvertiseDeviceL( aLive, aElement );
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CUpnpMessageHandlerEngine::DeviceListChangedL
       
   186 // Inform observer that device list changed.
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void CUpnpMessageHandlerEngine::DeviceListChangedL()
       
   190     {
       
   191     LOG_FUNC_NAME;
       
   192 
       
   193     //Check waiting
       
   194     if ( iCurrentSsdpHandler == iSsdpHandlerUp )
       
   195         {        
       
   196         iObserver.DeviceListUpdateL();
       
   197         }
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CUpnpMessageHandlerEngine::DeviceList
       
   202 // Get device list.
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 RPointerArray<CUpnpDeviceLibraryElement>& CUpnpMessageHandlerEngine::DeviceList()
       
   206     {
       
   207     LOG_FUNC_NAME;
       
   208     // return device list
       
   209     return iDeviceLibrary->DeviceList();
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CUpnpMessageHandlerEngine::AddLocalDeviceL
       
   214 // Add local device to device library.
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 TInt CUpnpMessageHandlerEngine::AddLocalDeviceL(
       
   218     TUpnpAddLocalDevice &aDevice, TDesC8& aBuffer )
       
   219     {
       
   220     LOG_FUNC_NAME;
       
   221     
       
   222     iDeviceLibrary->AddInfoL( &aDevice, aBuffer, iConnectionManagerProxy->LocalAddress() );
       
   223 
       
   224     return KErrNone;
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CUpnpMessageHandlerEngine::AddLocalControlPointL
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 TInt CUpnpMessageHandlerEngine::AddLocalControlPoint()
       
   232     {
       
   233     return iSsdpHandlerUp->AddLocalControlPoint();
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CUpnpMessageHandlerEngine::RemoveLocalControlPointL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 TInt CUpnpMessageHandlerEngine::RemoveLocalControlPoint()
       
   241     {
       
   242     return iSsdpHandlerUp->RemoveLocalControlPoint();
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CUpnpMessageHandlerEngine::StopFilteringDeviceL
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 void CUpnpMessageHandlerEngine::StopFilteringDeviceL( const TDesC8& aUuid )
       
   250     {
       
   251     iDeviceLibrary->StopFilteringDeviceL( aUuid );
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CUpnpMessageHandlerEngine::DeviceLibrary
       
   256 // Get device library.
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 CUpnpDeviceLibrary* CUpnpMessageHandlerEngine::DeviceLibrary()
       
   260     {
       
   261     LOG_FUNC_NAME;
       
   262     return iDeviceLibrary;
       
   263     }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CUpnpMessageHandlerEngine::SsdpEventL
       
   267 // Callback function for received ssdp messages.
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 void CUpnpMessageHandlerEngine::SsdpEventL( CUpnpSsdpMessage *aMessage )
       
   271     {
       
   272     LOG_FUNC_NAME;
       
   273 
       
   274     // ignore messages with M-SEARCH there are serverd by SSDP Server internally
       
   275     if ( aMessage->IsSsdpMSearch() )
       
   276         {
       
   277         return;
       
   278         }
       
   279     // forward message to device library
       
   280     iDeviceLibrary->AddInfoL( aMessage );
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CUpnpMessageHandlerEngine::SsdpSearchL
       
   285 // Do ssdp searches.
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 void CUpnpMessageHandlerEngine::SsdpSearchL( TDesC8& aSearchString,
       
   289     TDesC8& aMX )
       
   290     {
       
   291     LOG_FUNC_NAME;
       
   292     iCurrentSsdpHandler->SsdpSearchL( aSearchString, aMX );
       
   293     }
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CUpnpMessageHandlerEngine::ActiveIap
       
   297 // Returns the iap id (which is got in function above).
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 TInt CUpnpMessageHandlerEngine::ActiveIap() const
       
   301     {
       
   302     LOG_FUNC_NAME;
       
   303     return iConnectionManagerProxy->ActiveIap();
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CUpnpMessageHandlerEngine::RemoveLocalDevice
       
   308 // Removes local device from device library.
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CUpnpMessageHandlerEngine::RemoveLocalDeviceL( const TDesC8 &aUuid,
       
   312     TBool aSilent )
       
   313     {
       
   314     LOG_FUNC_NAME;
       
   315 
       
   316     // forward the request to device library
       
   317     if ( iCurrentSsdpHandler == iSsdpHandlerUp)
       
   318         {
       
   319         if ( !aSilent )
       
   320             {
       
   321             iDeviceLibrary->RemoveL( aUuid );
       
   322             }
       
   323         else
       
   324             {
       
   325             iDeviceLibrary->RemoveSilentL( aUuid );
       
   326             }
       
   327         }
       
   328 
       
   329     LOGS( "CUpnpMessageHandlerEngine::RemoveLocalDevice passed");
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CUpnpMessageHandlerEngine::StartSsdp
       
   334 // Start SSPD.
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 TInt CUpnpMessageHandlerEngine::StartSsdpL()
       
   338     {
       
   339     LOG_FUNC_NAME;
       
   340     if ( !iCurrentSsdpHandler->IsStarted() )
       
   341         {
       
   342         LOGS( "MESSAGEHANDLER *** Connecting to same access point...");        
       
   343 
       
   344         iCurrentSsdpHandler->StartL( iSocketServ );
       
   345 
       
   346 #ifdef RD_UPNP_REMOTE_ACCESS    
       
   347         /**********IPFiltering*******************/
       
   348         iIPFilterRepository = CUpnpIPFiltering::NewL( iSocketServ,
       
   349                 ActiveIap(),
       
   350                 iObserver );
       
   351         /****************************************/
       
   352 #endif
       
   353 
       
   354         return KErrNone;
       
   355         }
       
   356     else
       
   357         {
       
   358         // Ensure all two servers are started
       
   359         iCurrentSsdpHandler->StartL( iSocketServ );
       
   360         return KErrNone;
       
   361         }
       
   362     }
       
   363 
       
   364 
       
   365 // -----------------------------------------------------------------------------
       
   366 // CUpnpMessageHandlerEngine::InterfaceDown
       
   367 // -----------------------------------------------------------------------------
       
   368 //
       
   369 void CUpnpMessageHandlerEngine::InterfaceDown()
       
   370     {
       
   371     LOG_FUNC_NAME;
       
   372        
       
   373     iSsdpHandlerUp->Stop();
       
   374     iCurrentSsdpHandler = iSsdpHandlerDown;
       
   375     }
       
   376 
       
   377 #ifdef RD_UPNP_REMOTE_ACCESS
       
   378 // -----------------------------------------------------------------------------
       
   379 // CUpnpServiceInfo::AddAddressL
       
   380 // -----------------------------------------------------------------------------
       
   381 //
       
   382 void CUpnpMessageHandlerEngine::AddAddressL( const TInetAddr& aAddress )
       
   383     {
       
   384     User::LeaveIfError( iIPFilterRepository->AddAddressL( aAddress ) );
       
   385     iObserver.IPListChange();
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CUpnpServiceInfo::AddAddressL
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 void CUpnpMessageHandlerEngine::AddAddressL( const RMessage2& aMessage,
       
   393         const TDesC8& aRemoteName )
       
   394     {
       
   395     CUpnpIPFilteringDNSQuery* query = CUpnpIPFilteringDNSQuery::NewLC( aMessage,
       
   396             aRemoteName );
       
   397     iIPFilterRepository->AddAddressL( query );
       
   398     CleanupStack::Pop( query );
       
   399     }
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CUpnpServiceInfo::RemoveAddressL
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 void CUpnpMessageHandlerEngine::RemoveAddressL( const TInetAddr& aAddress )
       
   406     {
       
   407     if ( iIPFilterRepository->RemoveAddressL( aAddress ) >=0 )
       
   408         {
       
   409         iObserver.IPListChange();
       
   410         }
       
   411     }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CUpnpServiceInfo::RemoveAddressL
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 void CUpnpMessageHandlerEngine::RemoveAddressL( const RMessage2& aMessage,
       
   418         const TDesC8& aRemoteName )
       
   419     {
       
   420     CUpnpIPFilteringDNSQuery* query = CUpnpIPFilteringDNSQuery::NewLC( aMessage,
       
   421             aRemoteName );
       
   422     iIPFilterRepository->RemoveAddressL( query );
       
   423     CleanupStack::Pop( query );
       
   424     }
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CUpnpServiceInfo::RemoveAll
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 void CUpnpMessageHandlerEngine::RemoveAll()
       
   431     {
       
   432     if ( iIPFilterRepository->RemoveAll() >=0 )
       
   433         {
       
   434         iObserver.IPListChange();
       
   435         }
       
   436     }
       
   437 
       
   438 // -----------------------------------------------------------------------------
       
   439 // CUpnpServiceInfo::IsIPAllowed
       
   440 // -----------------------------------------------------------------------------
       
   441 //
       
   442 TBool CUpnpMessageHandlerEngine::IsIPAllowed( const TInetAddr& aAddress ) const
       
   443     {
       
   444     return iIPFilterRepository->IsAllowed( aAddress );
       
   445     }
       
   446 
       
   447 // -----------------------------------------------------------------------------
       
   448 // CUpnpServiceInfo::IPFilteringStatus
       
   449 // -----------------------------------------------------------------------------
       
   450 //
       
   451 void CUpnpMessageHandlerEngine::IPFilteringStatus( TInt& aListSize ) const
       
   452     {
       
   453     aListSize = iIPFilterRepository->GetIPFilterList().Size();
       
   454     }
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // CUpnpServiceInfo::GetIPFilterList
       
   458 // -----------------------------------------------------------------------------
       
   459 //
       
   460 const TDesC8& CUpnpMessageHandlerEngine::GetIPFilterList()
       
   461     {
       
   462     return iIPFilterRepository->GetIPFilterList();
       
   463     }
       
   464 #endif
       
   465 
       
   466 // -----------------------------------------------------------------------------
       
   467 // CUpnpMessageHandlerEngine::NetworkEventWlanLost
       
   468 // -----------------------------------------------------------------------------
       
   469 //
       
   470 void CUpnpMessageHandlerEngine::NetworkEvent( CUpnpNetworkEventBase* aEvent )
       
   471     {
       
   472     LOG_FUNC_NAME;
       
   473 
       
   474     if ( aEvent->SubscriberError() < KErrNone && aEvent->Type() == EUnknownEvent )
       
   475         {
       
   476         LOGS( "MESSAGEHANDLER *** NetworkEvent error" );
       
   477         return;
       
   478         }
       
   479 
       
   480     switch ( aEvent->Type() )
       
   481         {
       
   482         case EWlanLostEvent:
       
   483             {
       
   484             InterfaceDown();
       
   485   
       
   486             TDblQueIter<CSession2>& sessionIter = iObserver.Sessions();
       
   487         
       
   488             sessionIter.SetToFirst();
       
   489             CUpnpMessageHandlerSession *session = 
       
   490                     reinterpret_cast<CUpnpMessageHandlerSession*>( sessionIter++ );
       
   491             while ( session )
       
   492                 {
       
   493                 session->CancelPendingRequests();
       
   494                 session = reinterpret_cast<CUpnpMessageHandlerSession*>( sessionIter++ );
       
   495                 }
       
   496             }
       
   497             break;
       
   498 
       
   499         case EAddressChangeEvent:
       
   500             {
       
   501             CUpnpNetworkEventAddressChange* networkEvent = 
       
   502                                 static_cast< CUpnpNetworkEventAddressChange* >( aEvent );
       
   503   
       
   504             TInetAddr addr = networkEvent->Address();
       
   505 
       
   506             TRAPD( error, iCurrentSsdpHandler->AddressChangeL( addr ) );
       
   507             TRAP( error, iDeviceLibrary->RemoveAllDevicesL() );
       
   508 
       
   509             if ( error )
       
   510                 {
       
   511                 // only case this should happen
       
   512                 ASSERT( error == KErrNoMemory );
       
   513                 }
       
   514             }
       
   515             break;
       
   516         }
       
   517     }
       
   518 
       
   519 // End of File