upnp/upnpstack/controlpointbase/src/upnpcpbembeddeddevicedescription.cpp
changeset 0 f5a58ecadc66
child 9 5c72fd91570d
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2007-2007 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:  Declares simple device discovery process.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <e32base.h>
       
    22 
       
    23 #include <upnpdevice.h>
       
    24 #include "upnpcontenthandlerscontroller.h"
       
    25 
       
    26 #include "upnpcpbembeddeddevicedescription.h"
       
    27 #include "upnpcpbdevicerepository.h"
       
    28 #include "upnpcpbdevicelistutils.h"
       
    29 #include "upnpcpbhttpmessagecontroller.h"
       
    30 
       
    31 // LOGGER SETTINGS
       
    32 #define KLogFile _L("UPnPControlPoint.txt")
       
    33 #include <upnpcustomlog.h>
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CUpnpCpbEmbeddedDeviceDescription::NewL
       
    39 // Two-phased constructor
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CUpnpCpbEmbeddedDeviceDescription* CUpnpCpbEmbeddedDeviceDescription::NewL(
       
    43             MUpnpCpbHttpMessageController& aMessanger,
       
    44             CUpnpCpbDeviceRepository& aRepository )
       
    45     {
       
    46     LOG_FUNC_NAME;
       
    47     CUpnpCpbEmbeddedDeviceDescription* self =
       
    48         new (ELeave) CUpnpCpbEmbeddedDeviceDescription(aMessanger, aRepository);
       
    49 
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop(self);
       
    53 
       
    54     return self;
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CUpnpCpbDeviceDescription::ConstructL
       
    59 // Second phase constructor
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void CUpnpCpbEmbeddedDeviceDescription::ConstructL()
       
    63     {
       
    64     LOG_FUNC_NAME;
       
    65     CUpnpCpbDeviceDescription::ConstructL();
       
    66 
       
    67     iDiscoveredDevices.Reset();
       
    68     iDiscoveredDevices.Close();
       
    69 
       
    70     iAllDevicesInRoot.Reset();
       
    71     iAllDevicesInRoot.Close();
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CUpnpCpbEmbeddedDeviceDescription::~CUpnpCpbEmbeddedDeviceDescription
       
    76 // Destructor
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CUpnpCpbEmbeddedDeviceDescription::~CUpnpCpbEmbeddedDeviceDescription()
       
    80     {
       
    81     LOG_FUNC_NAME;
       
    82     iNotNeededDevices.Reset();
       
    83     iNotNeededDevices.Close();
       
    84     iAllDevicesInRoot.Reset();
       
    85     iAllDevicesInRoot.Close();
       
    86     iDiscoveredDevices.Reset();
       
    87     iDiscoveredDevices.Close();
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CUpnpCpbEmbeddedDeviceDescription::CUpnpCpbEmbeddedDeviceDescription
       
    92 // Constructor
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CUpnpCpbEmbeddedDeviceDescription::CUpnpCpbEmbeddedDeviceDescription(
       
    96     MUpnpCpbHttpMessageController& aMessanger,
       
    97     CUpnpCpbDeviceRepository& aRepository)
       
    98     : CUpnpCpbDeviceDescription(aMessanger, aRepository)
       
    99     {
       
   100     LOG_FUNC_NAME;
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CUpnpCpbEmbeddedDeviceDescription::DiscoverDeviceL
       
   105 // Start processing device.
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 TInt CUpnpCpbEmbeddedDeviceDescription::DiscoverDeviceL(CUpnpDevice* aDevice)
       
   109     {
       
   110     LOG_FUNC_NAME;
       
   111     if(!aDevice)
       
   112         {
       
   113         return KErrNotFound;
       
   114         }
       
   115 
       
   116     iAllDevicesInRoot.Reset();
       
   117     iDiscoveredDevices.Reset();
       
   118     iNotNeededDevices.Reset();
       
   119 
       
   120     iRootDevice = aDevice;
       
   121     iResult = KDisscoveryFinished;
       
   122     iUuids.Zero();
       
   123     BuildDevicesListL(iRootDevice);
       
   124 
       
   125     // Start discovery if some device has services start to download it
       
   126     // And change state to inprogress
       
   127     for(TInt i = iAllDevicesInRoot.Count() - 1;  i > -1; i--)
       
   128         {
       
   129         if(iAllDevicesInRoot.Find(iAllDevicesInRoot[i]) != KErrNotFound)
       
   130             {
       
   131             GetServiceDescriptionL(iAllDevicesInRoot[i]);
       
   132             }
       
   133         }
       
   134 
       
   135     return iResult;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CUpnpCpbEmbeddedDeviceDescription::GetIncompliteRootDevice
       
   140 //  Append discovered devices to array
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 CUpnpDevice* CUpnpCpbEmbeddedDeviceDescription::GetIncompliteRootDevice()
       
   144     {
       
   145     return iRootDevice;
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CUpnpCpbEmbeddedDeviceDescription::BuildDevicesListL
       
   150 // Build list with devices that should be processed
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CUpnpCpbEmbeddedDeviceDescription::BuildDevicesListL(CUpnpDevice* aDevice)
       
   154     {
       
   155     if (iRepository.MatchTargetDevice(aDevice->DeviceType()))
       
   156         {
       
   157         // If device is already known (partial discovery of embedded)
       
   158         // add it to temporary discovered list
       
   159         if (iRepository.IsDiscoveredDevice(aDevice->Uuid()))
       
   160             {
       
   161             iDiscoveredDevices.AppendL(aDevice);
       
   162             }
       
   163         else
       
   164             {
       
   165             iAllDevicesInRoot.AppendL( aDevice );
       
   166             }
       
   167 
       
   168         // processing embedded devices
       
   169         // GetAllDevices method returns list of all devices of embedded devices tree
       
   170         RPointerArray<CUpnpDevice> devices;
       
   171         aDevice->GetAllDevices(devices);
       
   172         CleanupClosePushL(devices);
       
   173         for (TInt i(0); i < devices.Count(); i++)
       
   174             {
       
   175             if (!iRepository.FindDevice(devices[i]->Uuid()))
       
   176                 {
       
   177                 iAllDevicesInRoot.AppendL( devices[i] );
       
   178                 }
       
   179             else
       
   180                 {
       
   181                 // device is known
       
   182                 iDiscoveredDevices.AppendL(devices[i]);
       
   183                 // if was not reported as discovered
       
   184                 if (iRepository.IsUninterestingDevice(devices[i]->Uuid())
       
   185                         && iRepository.MatchTargetDevice(devices[i]->DeviceType()))
       
   186                     {
       
   187                     iRepository.RemoveDevice(devices[i]);
       
   188                     }
       
   189                 }
       
   190             }
       
   191         CleanupStack::PopAndDestroy(&devices);
       
   192         }
       
   193     else
       
   194         {
       
   195         iNotNeededDevices.AppendL(aDevice);
       
   196         // Device is not interesting, process subdevices
       
   197         RPointerArray<CUpnpDevice> deviceList  = aDevice->DeviceList();
       
   198         for (TInt i(0); i < deviceList.Count(); i++)
       
   199             {
       
   200             BuildDevicesListL( deviceList[i] );
       
   201             }
       
   202         }
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CUpnpCpbEmbeddedDeviceDescription::GetServiceDescriptionL
       
   207 // Start getting services desriptions
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CUpnpCpbEmbeddedDeviceDescription::GetServiceDescriptionL(
       
   211     CUpnpDevice* aDevice )
       
   212     {
       
   213 
       
   214     // Because all service descriptions are not retrieved, retrieve next one.
       
   215     TPtrC8 buffer(GetNextServiceType(aDevice));
       
   216 
       
   217     if (buffer.Length())
       
   218         {
       
   219         //ask for 1st service description
       
   220         iResult = KDisscoveryInProgress;
       
   221         TInt sessionId = iMessanger.GetServiceDescriptionL(aDevice, buffer);
       
   222         // put SessionID to memory per pending Service Description request
       
   223         aDevice->WaitServiceDescriptionL( sessionId );
       
   224         }
       
   225         //no service, device is discovered
       
   226     else
       
   227         {
       
   228         iDiscoveredDevices.AppendL( aDevice );
       
   229         iAllDevicesInRoot.Remove(iAllDevicesInRoot.Find(aDevice));
       
   230         }
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CUpnpCpbEmbeddedDeviceDescription::ServiceDescription
       
   235 // Function processing service description
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 TInt CUpnpCpbEmbeddedDeviceDescription::ServiceDescriptionL(
       
   239     CUpnpHttpMessage* aMsg )
       
   240     {
       
   241     LOG_FUNC_NAME;
       
   242 
       
   243     // Check if device is waiting for this service
       
   244     TBool waiting(EFalse);
       
   245     if(!iRootDevice)
       
   246         {
       
   247         iResult = KErrNotFound;
       
   248         }
       
   249     else
       
   250         {
       
   251         TInt servCount(0);
       
   252         CUpnpDevice* device = NULL;
       
   253         for (TInt i(0); i<iAllDevicesInRoot.Count(); i++)
       
   254             {
       
   255             for (servCount = 0;
       
   256                  servCount < iAllDevicesInRoot[i]->WaitServiceDescriptionCount();
       
   257                  servCount++ )
       
   258                 {
       
   259                 if ( iAllDevicesInRoot[i]->WaitServiceDescriptionSessionId( servCount )
       
   260                          == aMsg->SessionId() )
       
   261                     {
       
   262                     device = iAllDevicesInRoot[i];
       
   263                     waiting = ETrue;
       
   264                     break;
       
   265                     }
       
   266                 }
       
   267                 if (device)
       
   268                     {
       
   269                     break;
       
   270                     }
       
   271             }
       
   272         // Waiting for service description
       
   273         if (waiting)
       
   274             {
       
   275             // Processing message
       
   276             if ( !aMsg->Is2xx())
       
   277                 {
       
   278                 LOGS("CUpnpCpbEmbeddedDeviceDescription::ServiceDescriptionL "
       
   279                      "- Wrong response recived.");
       
   280                 iAllDevicesInRoot.Remove(iAllDevicesInRoot.Find(device));
       
   281                 device->WaitServiceDescriptionRemoveSessionId(servCount);
       
   282                 if (!iAllDevicesInRoot.Count())
       
   283                     {
       
   284                     // Function will set iResult
       
   285                     BuildResultListsL(iRootDevice);
       
   286                     CreateUuidsL();
       
   287                     }
       
   288                 else
       
   289                     {
       
   290                     iResult = KDisscoveryInProgress;
       
   291                     }
       
   292                 }
       
   293             else
       
   294                 {
       
   295                 if(device != NULL)
       
   296                     {
       
   297 					iResult = ProcessServiceDescriptionL(device, aMsg);
       
   298                     }
       
   299 				else
       
   300 				    {
       
   301 					iResult = KErrNotFound;
       
   302 				    }
       
   303 
       
   304                 }
       
   305             }
       
   306         else
       
   307             {
       
   308             iResult = KErrNotFound;
       
   309             }
       
   310         }
       
   311     return iResult;
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CUpnpCpbEmbeddedDeviceDescription::ServiceDescription
       
   316 // Parse service description
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 TInt CUpnpCpbEmbeddedDeviceDescription::ProcessServiceDescriptionL(
       
   320     CUpnpDevice* aDevice, CUpnpHttpMessage* aMsg)
       
   321     {
       
   322     LOG_FUNC_NAME;
       
   323     CUpnpDevice::TServiceAdd result;
       
   324 
       
   325     // Parsing service desription
       
   326     CUpnpService* service = NULL;
       
   327 
       
   328     TRAPD( error, service = iSaxController->ParseServiceL( aMsg->Body(), aDevice ) );
       
   329     if( !service || error )
       
   330         {
       
   331         // error handling section
       
   332         iAllDevicesInRoot.Remove(iAllDevicesInRoot.Find(aDevice));
       
   333         if(!iAllDevicesInRoot.Count())
       
   334             {
       
   335             // Function will set iResult
       
   336             BuildResultListsL(iRootDevice);
       
   337             CreateUuidsL();
       
   338             }
       
   339         else
       
   340             {
       
   341             iResult = KDisscoveryInProgress;
       
   342             }
       
   343 
       
   344             return iResult;
       
   345         }
       
   346 
       
   347     CleanupStack::PushL(service);
       
   348     result = aDevice->AddServiceL(aMsg->SessionId(), service);
       
   349     CleanupStack::Pop(service);
       
   350 
       
   351     if ( result == CUpnpDevice::EAllServicesAdded )
       
   352         { // device has received all service descriptions
       
   353         LOGS("CUpnpCpbEmbeddedDeviceDescription::ProcessServiceDescriptionL "
       
   354              "- All service info added to the parent device");
       
   355         // Process devicess
       
   356         iDiscoveredDevices.AppendL( aDevice );
       
   357         iAllDevicesInRoot.Remove(iAllDevicesInRoot.Find(aDevice));
       
   358 
       
   359         if(!iAllDevicesInRoot.Count())
       
   360             {
       
   361             // Function will set iResult
       
   362             BuildResultListsL(iRootDevice);
       
   363             CreateUuidsL();
       
   364             }
       
   365         else
       
   366             {
       
   367             iResult = KDisscoveryInProgress;
       
   368             }
       
   369         }
       
   370     else if ( result == CUpnpDevice::EServiceAdded )
       
   371         {
       
   372         LOGS("CUpnpCpbEmbeddedDeviceDescription::ProcessServiceDescriptionL "
       
   373              "- Service added");
       
   374         // Waiting for next desciptions.
       
   375         iResult = KDisscoveryInProgress;
       
   376         TPtrC8 buffer(GetNextServiceType(aDevice));
       
   377         TInt sessionId = iMessanger.GetServiceDescriptionL(aDevice, buffer);
       
   378          // put SessionID to memory per pending Service Description request
       
   379         aDevice->WaitServiceDescriptionL( sessionId );
       
   380         }
       
   381     else
       
   382         {
       
   383         delete service;
       
   384         iResult = KDisscoveryInProgress;
       
   385         }
       
   386 
       
   387     LOGS1("CUpnpCpbEmbeddedDeviceDescription::ProcessServiceDescriptionL -res=%d",
       
   388         iResult);
       
   389     return iResult;
       
   390     }
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CUpnpCpbEmbeddedDeviceDescription::BuildResultListL
       
   394 //  Create result list
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 TBool CUpnpCpbEmbeddedDeviceDescription::BuildResultListsL(CUpnpDevice* aDevice)
       
   398     {
       
   399     //Devices discovered correctly will be moved to list iAllDevicesInRoot
       
   400     TBool result = ETrue;
       
   401     // Process all subdevices
       
   402     RPointerArray<CUpnpDevice> deviceList  = aDevice->DeviceList();
       
   403     for (TInt i(0); i < deviceList.Count(); i++)
       
   404         {
       
   405         result =  BuildResultListsL(deviceList[i]) && result;
       
   406         }
       
   407     // Process current device
       
   408     if (result)
       
   409         {
       
   410         if (UpnpCpbDeviceListUtils::ExistOnList(aDevice->Uuid(), iDiscoveredDevices))
       
   411             {
       
   412             if (iRepository.MatchTargetDevice(aDevice->DeviceType()))
       
   413                 {
       
   414                 // correctly discovered devices are now on iAllDevicesInRoot list
       
   415                 iAllDevicesInRoot.AppendL(aDevice);
       
   416                 iDiscoveredDevices.Remove(iDiscoveredDevices.Find(aDevice));
       
   417                 }
       
   418             }
       
   419         else
       
   420             {
       
   421             result = EFalse;
       
   422             }
       
   423         }
       
   424     return result;
       
   425     }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 // CUpnpCpbEmbeddedDeviceDescription::CreateUuidsL
       
   429 // Count result and create Uuids
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 void CUpnpCpbEmbeddedDeviceDescription::CreateUuidsL()
       
   433     {
       
   434     if(!iRootDevice)
       
   435         {
       
   436         return;
       
   437         }
       
   438 
       
   439     RPointerArray<CUpnpDevice> devices;
       
   440     iRootDevice->GetAllDevices(devices);
       
   441 
       
   442     // All devices
       
   443     TInt allDevicesNumber = devices.Count() + 1;
       
   444     // All devices with processing finished correctly
       
   445     TInt correctlyProcessedDevices = iNotNeededDevices.Count() +
       
   446                 iDiscoveredDevices.Count() + iAllDevicesInRoot.Count();
       
   447     CleanupClosePushL(devices);
       
   448     // all devices found
       
   449     if(allDevicesNumber == correctlyProcessedDevices)
       
   450         {
       
   451         iResult = KDisscoveryFinished;
       
   452         }
       
   453     else // Some devices are not finished correctly
       
   454         {
       
   455         iResult = KDisscoveryIncorrect;
       
   456 
       
   457         for (TInt i(0); i < devices.Count(); i++)
       
   458             {
       
   459             if ( !UpnpCpbDeviceListUtils::ExistOnList(devices[i]->Uuid(),
       
   460                     iNotNeededDevices )
       
   461                     && !UpnpCpbDeviceListUtils::ExistOnList(devices[i]->Uuid(),
       
   462                         iAllDevicesInRoot )
       
   463                     && !UpnpCpbDeviceListUtils::ExistOnList(devices[i]->Uuid(),
       
   464                         iDiscoveredDevices ) )
       
   465                 {
       
   466                 AddToUuidsL(devices[i]->Uuid());
       
   467                 }
       
   468             }
       
   469         AddToUuidsL(iRootDevice->Uuid());
       
   470         // root might be only unneded, or not discovered correctly in case of wrong process
       
   471         TInt idx = iNotNeededDevices.Find(iRootDevice);
       
   472         if(idx != KErrNotFound)
       
   473             {
       
   474             iNotNeededDevices.Remove(idx);
       
   475             }
       
   476         else
       
   477             {
       
   478             idx = iDiscoveredDevices.Find(iRootDevice);
       
   479             if(idx != KErrNotFound)
       
   480                 {
       
   481                 iDiscoveredDevices.Remove(idx);
       
   482                 }
       
   483             }
       
   484         }
       
   485     CleanupStack::PopAndDestroy(&devices);
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CUpnpCpbEmbeddedDeviceDescription::GetDiscoveredDeviceL
       
   490 //  Append discovered devices to array
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 void CUpnpCpbEmbeddedDeviceDescription::GetDiscoveredDeviceL(
       
   494     RPointerArray<CUpnpDevice>& aArray, TBool aRemove )
       
   495     {
       
   496     if(iResult != KDisscoveryInProgress)
       
   497         {
       
   498         for(TInt i(0); i < iAllDevicesInRoot.Count(); i++)
       
   499             {
       
   500             if( iRepository.FindDevice( iAllDevicesInRoot[i]->Uuid() )
       
   501                 && aRemove )
       
   502                 {
       
   503                 iAllDevicesInRoot.Remove( i-- );
       
   504                 }
       
   505             else
       
   506                 {
       
   507                 aArray.AppendL(iAllDevicesInRoot[i]);
       
   508                 }
       
   509             }
       
   510         }
       
   511     }
       
   512 
       
   513 // -----------------------------------------------------------------------------
       
   514 // CUpnpCpbEmbeddedDeviceDescription::GetUninterestingDeviceL
       
   515 //  Append not discovered (does not match the type) devices to array
       
   516 // -----------------------------------------------------------------------------
       
   517 //
       
   518 void CUpnpCpbEmbeddedDeviceDescription::GetUninterestingDeviceL(
       
   519     RPointerArray<CUpnpDevice>& aArray , TBool aRemove )
       
   520     {
       
   521     if(iResult != KDisscoveryInProgress)
       
   522         {
       
   523         for(TInt i(0); i < iDiscoveredDevices.Count(); i++)
       
   524             {
       
   525             if( iRepository.FindDevice( iDiscoveredDevices[i]->Uuid() )
       
   526                 && aRemove )
       
   527                 {
       
   528                 iDiscoveredDevices.Remove( i-- );
       
   529                 }
       
   530             else
       
   531                 {
       
   532                 aArray.AppendL(iDiscoveredDevices[i]);
       
   533                 }
       
   534             }
       
   535         }
       
   536     }
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CUpnpCpbEmbeddedDeviceDescription::GetUnnededDeviceL
       
   540 //  Append discovered but not metching target types devices to array
       
   541 // -----------------------------------------------------------------------------
       
   542 //
       
   543 void CUpnpCpbEmbeddedDeviceDescription::GetUnnededDeviceL(
       
   544     RPointerArray<CUpnpDevice>& aArray , TBool aRemove )
       
   545     {
       
   546     if(iResult != KDisscoveryInProgress)
       
   547         {
       
   548         for(TInt i(0); i < iNotNeededDevices.Count(); i++)
       
   549             if( iRepository.FindDevice( iNotNeededDevices[i]->Uuid() )
       
   550                 && aRemove )
       
   551                 {
       
   552                 iNotNeededDevices.Remove( i-- );
       
   553                 }
       
   554             else
       
   555                 {
       
   556                 aArray.AppendL(iNotNeededDevices[i]);
       
   557                 }
       
   558         }
       
   559     }
       
   560 
       
   561 //  End of File