voipplugins/dhcppositionprovider/src/dhcpwlanqueryhandler.cpp
changeset 0 a4daefaec16c
equal deleted inserted replaced
-1:000000000000 0:a4daefaec16c
       
     1 /*
       
     2 * Copyright (c) 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sip.h>
       
    19 #include <sipprofileregistry.h>
       
    20 #include <sipprofile.h>
       
    21 #include <comms-infras/es_config.h>
       
    22 #include <in_sock.h>
       
    23 
       
    24 #include "dhcppsylogging.h"
       
    25 #include "dhcppsyrequesterprogressobserver.h"
       
    26 #include "dhcppsypanic.h"
       
    27 #include "dhcpsocketlistener.h"
       
    28 #include "dhcpwlanqueryhandler.h"
       
    29 #include "dhcplocationinformationparser.h"
       
    30 #include "dhcpdatasender.h"
       
    31 #include "dhcpdatalocationrequest.h"
       
    32 #include "dhcpconstants.h"
       
    33 #include "dhcpconnectionengine.h"
       
    34 #include "dhcpserveripaddressresolver.h"
       
    35 
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // NewL
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CDhcpWlanQueryHandler* CDhcpWlanQueryHandler::NewL(MDhcpPsyRequesterProgressObserver *aObserver)
       
    44     {
       
    45     TRACESTRING( "CDhcpWlanQueryHandler::NewL" );
       
    46     CDhcpWlanQueryHandler* self = new(ELeave) CDhcpWlanQueryHandler (aObserver);
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self );
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // Destructor
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CDhcpWlanQueryHandler::~CDhcpWlanQueryHandler()
       
    58     {
       
    59     TRACESTRING( "CDhcpWlanQueryHandler::~CDhcpWlanQueryHandler" );
       
    60 
       
    61     // Close opened resources and delete reserved memory
       
    62     Cancel();
       
    63 
       
    64     Flush ();
       
    65 
       
    66     delete iLctnDataRequestor;
       
    67     delete iLctnDataListener;
       
    68 
       
    69     iSocket.Close ();
       
    70 
       
    71     iConnection.Close();
       
    72     iSocketServer.Close();
       
    73 
       
    74     TRACESTRING( "CDhcpWlanQueryHandler::~CDhcpWlanQueryHandler -- end" );
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // Constructor
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CDhcpWlanQueryHandler::CDhcpWlanQueryHandler (MDhcpPsyRequesterProgressObserver *aObserver)
       
    82     : CActive (CActive::EPriorityStandard), iPsyProgressObserver (aObserver)
       
    83     {
       
    84     TRACESTRING( "CDhcpWlanQueryHandler::CDhcpWlanQueryHandler" );
       
    85     CActiveScheduler::Add(this);
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // LocationData
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 HBufC8*  CDhcpWlanQueryHandler::LocationData ()
       
    93     {
       
    94     return iLocationBufferData;
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // MakeLocationQueryL
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CDhcpWlanQueryHandler::MakeLocationQueryL (TUint32 aIapProfileId)
       
   102     {
       
   103     TRACESTRING( "CDhcpWlanQueryHandler::MakeLocationQueryL" )
       
   104     iIapProfileId = aIapProfileId;
       
   105 
       
   106     // Check is the device already connected to the wlan accessoint.
       
   107     TBool connectionAttached = iConnectionEngine->IsConnectedL(iIapProfileId);
       
   108     if (connectionAttached)
       
   109         {
       
   110         TRACESTRING("CDhcpWlanQueryHandler::MakeLocationQueryL, Attached" )
       
   111         iDhcpResolver = CDhcpPsyDhcpServerIPResolver::NewL(iConnection, iStatus);
       
   112         iState = EDhcpIpAddressQuery;
       
   113         }
       
   114     else
       
   115         {
       
   116         TRACESTRING("CDhcpWlanQueryHandler::MakeLocationQueryL, Not attached" )
       
   117         iConnectionEngine->ConnectL (aIapProfileId, iStatus);
       
   118         iState = EAttachToConnection;
       
   119         }
       
   120 
       
   121     iPsyProgressObserver->ProgressPsyRequestNotificationL (MDhcpPsyRequesterProgressObserver::ELocationQueryStarted,
       
   122          KErrNone);
       
   123 
       
   124     SetActive();
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // ConstructL
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 void CDhcpWlanQueryHandler::ConstructL()
       
   132     {
       
   133     TRACESTRING( "CDhcpWlanQueryHandler::ConstructL" );
       
   134     User::LeaveIfError( iSocketServer.Connect() );
       
   135     User::LeaveIfError( iConnection.Open( iSocketServer ) );
       
   136 
       
   137     iConnectionEngine = CDhcpConnectionEngine::NewL(iConnection);
       
   138     // Add socket listener and requestor classes up. Rest parameters will be
       
   139     // provided later on.
       
   140     iLctnDataRequestor = CDhcpDataRequestor::NewL(iSocket);
       
   141     iLctnDataListener = CDhcpPsySocketListener::NewL(&iSocket);
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CDhcpWlanQueryHandler::RunL
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 void CDhcpWlanQueryHandler::RunL()
       
   149     {
       
   150     TRACESTRING2( "CDhcpWlanQueryHandler::RunL, %d ", iStatus.Int());
       
   151     TRACESTRING2( "CDhcpWlanQueryHandler::RunL, state %d ", iState);
       
   152     TInt result = iStatus.Int();
       
   153     if (result != KErrNone && iState!=EDhcpIpAddressQuery)
       
   154         {
       
   155         iState=EDhpWlanIdle;
       
   156         iPsyProgressObserver->ProgressPsyRequestNotificationL (MDhcpPsyRequesterProgressObserver::ELocationQueryFinished,
       
   157              iStatus.Int());
       
   158         return;
       
   159         }
       
   160     switch (iState)
       
   161         {
       
   162         case EAttachToConnection:
       
   163             {
       
   164             TRACESTRING( "CDhcpWlanQueryHandler::RunL, AttachToConnection, start");
       
   165 
       
   166             iState=EDhcpIpAddressQuery;
       
   167 
       
   168             // Get ip address of the dhcp server.
       
   169             iDhcpResolver = CDhcpPsyDhcpServerIPResolver::NewL(iConnection, iStatus);
       
   170             SetActive ();
       
   171             }
       
   172             break;
       
   173         case EDhcpIpAddressQuery:
       
   174             {
       
   175             TRACESTRING( "CDhcpWlanQueryHandler::RunL, create and send the dhcp inform message to the wlan net, start");
       
   176 
       
   177             User::LeaveIfError( iSocket.Open( iSocketServer,
       
   178                     KAfInet,
       
   179                     KSockDatagram,
       
   180                     KProtocolInetUdp,
       
   181                     iConnection ) );
       
   182 
       
   183             TInetAddr addr( KInetAddrAny, KDhcpDefaultCliPort );
       
   184             iSocket.Bind( addr );
       
   185 
       
   186             // Create a dhcp inform message and send it.
       
   187             iLctnDataRequestor->SendDHCPMessageL(iDhcpResolver->DhcpServerInetAddress(),
       
   188                 iIapProfileId, iStatus);
       
   189 
       
   190             iState = ESendDhcpLocatinQuery;
       
   191             SetActive ();
       
   192             }
       
   193             break;
       
   194 
       
   195         case ESendDhcpLocatinQuery:
       
   196             {
       
   197             TRACESTRING( "CDhcpWlanQueryHandler::RunL, send complete" );
       
   198             // Store transaction id to the listener object
       
   199             iLctnDataListener->SetTransActionIdToFollow (iLctnDataRequestor->TransActionIdToFollow());
       
   200             // And start to listen the dhcp ack message from the subnet.
       
   201             iLctnDataListener->RcvDHCPMessage (iStatus);
       
   202             iState = EReceiveDhcpLocationAck;
       
   203             SetActive ();
       
   204             }
       
   205             break;
       
   206         case EReceiveDhcpLocationAck:
       
   207             {
       
   208             TRACESTRING( "CDhcpWlanQueryHandler::RunL, response received." );
       
   209             TInt statusCode = iStatus.Int();
       
   210 
       
   211             // Make final XML buffer if possible.
       
   212             TPtrC8 geoConfigInformation (iLctnDataListener->GeoConfigDhcpInformation());
       
   213             TPtrC8 civicInformation (iLctnDataListener->LocationCivicInformation ());
       
   214 
       
   215             TRACESTRING2( "CDhcpWlanQueryHandler::RunL, Response, geoConfig length %d",
       
   216                     geoConfigInformation.Length()
       
   217                     );
       
   218 
       
   219             TRACESTRING2 ( "CDhcpWlanQueryHandler::RunL, Response, civicInformation length %d",
       
   220                     civicInformation.Length()
       
   221                     );
       
   222 
       
   223             if(iLocationBufferData)
       
   224                 {
       
   225                 delete iLocationBufferData;
       
   226                 iLocationBufferData = NULL;
       
   227                 }
       
   228 
       
   229             // We should able to calculate maximun length of target buffer
       
   230             iLocationBufferData = HBufC8::NewL(KDhcpMaxLocInfoLength);
       
   231             TDhcpLocationInformationParser informationParser;
       
   232             TPtr8 ptr (iLocationBufferData->Des());
       
   233 
       
   234             // Parse at next preferred order option 123 and if doesn't found then option 99 if
       
   235             // possible ...
       
   236             if (geoConfigInformation.Length())          // option 123, GeoSpatial location information
       
   237                 {
       
   238                 informationParser.ParseLocationInformation(
       
   239                     geoConfigInformation,
       
   240                     TDhcpLocationInformationParser::EDHCPCoordinates, ptr);
       
   241                 }
       
   242             else if (civicInformation.Length())        // option 99, civic address location information
       
   243                 {
       
   244                 informationParser.ParseLocationInformation(
       
   245                     civicInformation,
       
   246                     TDhcpLocationInformationParser::EDHCPCivicAddress, ptr);
       
   247                 }
       
   248             else
       
   249                 {
       
   250                 // No valid information from the dhcp server,  notify caller for that.
       
   251                 statusCode = KErrNotFound;
       
   252                 }
       
   253 
       
   254             iState=EDhpWlanIdle;
       
   255             iPsyProgressObserver->ProgressPsyRequestNotificationL (MDhcpPsyRequesterProgressObserver::ELocationQueryFinished,
       
   256                  statusCode );
       
   257             }
       
   258             break;
       
   259         default:
       
   260             {
       
   261             TRACESTRING( "CDhcpWlanQueryHandler::RunL, invalid switch-case" );
       
   262             User::Leave (KErrGeneral);
       
   263             }
       
   264             break;
       
   265         }
       
   266     }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CDhcpWlanQueryHandler::DoCancel
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CDhcpWlanQueryHandler::DoCancel()
       
   273     {
       
   274     TRACESTRING( "CDhcpWlanQueryHandler::DoCancel" )
       
   275     if (iConnectionEngine)
       
   276         {
       
   277         iConnectionEngine->Cancel();
       
   278         }
       
   279     // Cancel onqoing request if needed.
       
   280     if (iDhcpResolver)
       
   281         {
       
   282         iDhcpResolver->Cancel();
       
   283         }
       
   284 
       
   285     if (iLctnDataRequestor)
       
   286         {
       
   287         iLctnDataRequestor->Cancel();
       
   288         }
       
   289 
       
   290     if (iLctnDataListener)
       
   291         {
       
   292         iLctnDataListener->Cancel();
       
   293         }
       
   294 
       
   295     TRAPD (err,
       
   296         iPsyProgressObserver->ProgressPsyRequestNotificationL (MDhcpPsyRequesterProgressObserver::ECancelled,
       
   297              KErrNone ) ) ;
       
   298 
       
   299     TRequestStatus *status = &iStatus;
       
   300     User::RequestComplete ( status, err );
       
   301 
       
   302     TRACESTRING( "CDhcpWlanQueryHandler::DoCancel finished" )
       
   303     }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CDhcpWlanQueryHandler::RunError
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 TInt CDhcpWlanQueryHandler::RunError (TInt aErrorCode)
       
   310     {
       
   311     TRACESTRING2( "CDhcpWlanQueryHandler::RunError %d", aErrorCode )
       
   312     TRACESTRING2( "CDhcpWlanQueryHandler::RunError, state, %d", iState )
       
   313     iState=EDhpWlanIdle;
       
   314     TRAPD (err,
       
   315         iPsyProgressObserver->ProgressPsyRequestNotificationL (MDhcpPsyRequesterProgressObserver::ELocationQueryFinished,
       
   316             aErrorCode ) );
       
   317     return err;
       
   318     }
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CDhcpWlanQueryHandler::Flush
       
   322 // -----------------------------------------------------------------------------
       
   323 //
       
   324 void CDhcpWlanQueryHandler::Flush ()
       
   325     {
       
   326     TRACESTRING( "CDhcpWlanQueryHandler::Flush" )
       
   327     delete iConnectionEngine;
       
   328     iConnectionEngine = NULL;
       
   329     delete iDhcpResolver;
       
   330     iDhcpResolver = NULL;
       
   331     delete iLocationBufferData;
       
   332     iLocationBufferData=NULL;
       
   333     }
       
   334