connectionmonitoring/connmon/connectionmonitor/src/ConnMonIAP.cpp
changeset 71 9f263f780e41
parent 52 bbe4544dfd31
child 72 0c32cf868819
equal deleted inserted replaced
70:ac5daea24fb0 71:9f263f780e41
    32 #include "connmondtmnoti.h"
    32 #include "connmondtmnoti.h"
    33 #include "connmontelnoti.h"
    33 #include "connmontelnoti.h"
    34 #include "ConnMonBearerNotifier.h"
    34 #include "ConnMonBearerNotifier.h"
    35 #include "log.h"
    35 #include "log.h"
    36 #include "cellulardatausagekeyupdater.h"
    36 #include "cellulardatausagekeyupdater.h"
       
    37 #include "connmonasyncstopdaemon.h"
    37 
    38 
    38 // ============================ MEMBER FUNCTIONS ===============================
    39 // ============================ MEMBER FUNCTIONS ===============================
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // TAsyncStopQueueElement::TAsyncStopQueueElement
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 TAsyncStopQueueElement::TAsyncStopQueueElement( const RMessage2& aMessage )
       
    46         :
       
    47         iMessage( aMessage ),
       
    48         iStatus( 0 ),
       
    49         iIdCount( 0 ),
       
    50         iStopCount( 0 )
       
    51     {
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // TAsyncStopQueueElement::Add
       
    56 // Add a connection ID to the ID array. Does nothing if the array is already full. 
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 void TAsyncStopQueueElement::Add( TUint aConnectionId )
       
    60     {
       
    61     if ( iIdCount < KMaxConnectionCount )
       
    62         {
       
    63         iId[iIdCount] = aConnectionId;
       
    64         iIdCount++;
       
    65         }
       
    66     }
    39 
    67 
    40 // -----------------------------------------------------------------------------
    68 // -----------------------------------------------------------------------------
    41 // TConnInfo::TConnInfo
    69 // TConnInfo::TConnInfo
    42 // -----------------------------------------------------------------------------
    70 // -----------------------------------------------------------------------------
    43 //
    71 //
    59         iDataVolumeAO( 0 ),
    87         iDataVolumeAO( 0 ),
    60         iConnDownNotifier( 0 ),
    88         iConnDownNotifier( 0 ),
    61         iActivityNotifier( 0 ),
    89         iActivityNotifier( 0 ),
    62         iActivity( 0 ),
    90         iActivity( 0 ),
    63         iBearerInfo( aBearerInfo ),
    91         iBearerInfo( aBearerInfo ),
    64         iBearerNotifier( 0 )
    92         iBearerNotifier( 0 ),
       
    93         iAsyncStopStatus( EConnMonAsyncStopNotStarted )
    65     {
    94     {
    66     }
    95     }
    67 
    96 
    68 // -----------------------------------------------------------------------------
    97 // -----------------------------------------------------------------------------
    69 // TConnInfo::Reset
    98 // TConnInfo::Reset
    86     iConnAttach       = 0;
   115     iConnAttach       = 0;
    87     iConnDownNotifier = 0;
   116     iConnDownNotifier = 0;
    88     iActivityNotifier = 0;
   117     iActivityNotifier = 0;
    89     iActivity         = 0;
   118     iActivity         = 0;
    90     iBearerNotifier   = 0;
   119     iBearerNotifier   = 0;
       
   120     iAsyncStopStatus  = EConnMonAsyncStopNotStarted;
    91     }
   121     }
    92 
   122 
    93 // -----------------------------------------------------------------------------
   123 // -----------------------------------------------------------------------------
    94 // TConnInfo::Compare
   124 // TConnInfo::Compare
    95 // Compares two TConnInfo items (by connection id).
   125 // Compares two TConnInfo items (by connection id).
   339 //
   369 //
   340 // Destructor
   370 // Destructor
   341 CConnMonIAP::~CConnMonIAP()
   371 CConnMonIAP::~CConnMonIAP()
   342     {
   372     {
   343     LOGENTRFN("CConnMonIAP::~CConnMonIAP()")
   373     LOGENTRFN("CConnMonIAP::~CConnMonIAP()")
       
   374             
       
   375     iAsyncStopQueue.Close();
       
   376     
   344     // Delele dial up modules.
   377     // Delele dial up modules.
   345     delete iCsdFax;
   378     delete iCsdFax;
   346     iCsdFax = NULL;
   379     iCsdFax = NULL;
   347 
   380 
   348     delete iPsdFax;
   381     delete iPsdFax;
  1867 TInt CConnMonIAP::SetUintAttribute(
  1900 TInt CConnMonIAP::SetUintAttribute(
  1868         const RMessage2& /*aMessage*/,
  1901         const RMessage2& /*aMessage*/,
  1869         const TUint /*aValue*/ )
  1902         const TUint /*aValue*/ )
  1870     {
  1903     {
  1871     return KErrNotSupported;
  1904     return KErrNotSupported;
       
  1905     }
       
  1906 
       
  1907 // -----------------------------------------------------------------------------
       
  1908 // CConnMonIAP::AsyncConnectionStopL
       
  1909 // Checks parameter validity first. If all ok creates an active object that
       
  1910 // completes the connection stop in a separate thread. 
       
  1911 // -----------------------------------------------------------------------------
       
  1912 //
       
  1913 TInt CConnMonIAP::AsyncConnectionStopL( const RMessage2& aMessage )
       
  1914     {
       
  1915     LOGENTRFN("CConnMonIAP::AsyncConnectionStopL()")
       
  1916     TInt result( KErrGeneral );
       
  1917     TUint connectionId( aMessage.Int0() );
       
  1918     
       
  1919     TInt index = Index( connectionId );
       
  1920     if ( index < 0 )
       
  1921         {
       
  1922         LOGIT1("AsyncConnectionStopL: unknown connection index %d", connectionId)
       
  1923         result = KErrNotFound;
       
  1924         }
       
  1925     else
       
  1926         {
       
  1927         // Check if value is true or false
       
  1928         if ( !( aMessage.Int3() ) )
       
  1929             {
       
  1930             LOGIT("AsyncConnectionStopL: boolean value was false, quitting")
       
  1931             result = KErrNone;
       
  1932             }
       
  1933         else
       
  1934             {
       
  1935             if ( iConnInfos[index].iAsyncStopStatus != EConnMonStopCompleted )
       
  1936                 {
       
  1937                 TInt threadStartError( KErrNone );
       
  1938                 if ( iConnInfos[index].iAsyncStopStatus == EConnMonAsyncStopNotStarted )
       
  1939                     {
       
  1940                     threadStartError = StartAsyncStopThreadL( index );
       
  1941                     }
       
  1942                 if ( threadStartError == KErrNone )
       
  1943                     {
       
  1944                     // Associate this connection into this stop request
       
  1945                     TAsyncStopQueueElement request( aMessage );
       
  1946                     request.Add( connectionId );
       
  1947                     iAsyncStopQueue.Append( request );
       
  1948                     result = KRequestPending;
       
  1949                     }
       
  1950                 else
       
  1951                     {
       
  1952                     result = threadStartError;
       
  1953                     }
       
  1954                 }
       
  1955             else
       
  1956                 {
       
  1957                 // Connection has already been closed, but not yet removed from connection table
       
  1958                 result = KErrNone;
       
  1959                 }
       
  1960             }
       
  1961         }
       
  1962     LOGEXITFN1("CConnMonIAP::AsyncConnectionStopL()", result)
       
  1963     return result;
       
  1964     }
       
  1965 
       
  1966 // -----------------------------------------------------------------------------
       
  1967 // CConnMonIAP::AsyncConnectionStopAllL
       
  1968 // Checks parameter validity first. If all ok creates an active object for each
       
  1969 // connection that completes the connection stop in a separate thread. If any
       
  1970 // errors occur, returns the first encountered error code.
       
  1971 // -----------------------------------------------------------------------------
       
  1972 //
       
  1973 TInt CConnMonIAP::AsyncConnectionStopAllL( const RMessage2& aMessage )
       
  1974     {
       
  1975     LOGENTRFN("CConnMonIAP::AsyncConnectionStopAllL()")
       
  1976 
       
  1977     TInt result( KErrNone );
       
  1978     // Check if value is true or false
       
  1979     if ( !( aMessage.Int3() ) )
       
  1980         {
       
  1981         LOGIT("AsyncConnectionStopAllL: boolean value was false, quitting")
       
  1982         result = KErrNone;
       
  1983         }
       
  1984     else
       
  1985         {
       
  1986         TAsyncStopQueueElement request( aMessage );
       
  1987         TInt count = iConnInfos.Count();
       
  1988         result = KErrNone;
       
  1989         
       
  1990         for ( TInt i = 0; i < count; i++ )
       
  1991             {
       
  1992             if ( iConnInfos[i].iAsyncStopStatus != EConnMonStopCompleted )
       
  1993                 {
       
  1994                 TInt threadStartError( KErrNone );
       
  1995                 if ( iConnInfos[i].iAsyncStopStatus == EConnMonAsyncStopNotStarted )
       
  1996                     {
       
  1997                     threadStartError = StartAsyncStopThreadL( i );
       
  1998                     }
       
  1999                 if ( threadStartError == KErrNone )
       
  2000                     {
       
  2001                     // Associate this connection into this stop request
       
  2002                     request.Add( iConnInfos[i].iConnectionId );
       
  2003                     }
       
  2004                 else
       
  2005                     {
       
  2006                     // Thread start failed
       
  2007                     if ( result == KErrNone )
       
  2008                         {
       
  2009                         // Keep only the first error that occurs
       
  2010                         result = threadStartError;
       
  2011                         }
       
  2012                     LOGIT2("AsyncConnectionStopAllL: Error starting stop thread for conn. id %d <%d>",
       
  2013                             iConnInfos[i].iConnectionId, threadStartError)
       
  2014                     }
       
  2015                 }
       
  2016             }
       
  2017         // If one or more async connection stop threads have been started, and
       
  2018         // no errors have been encountered, set request to pending and add to
       
  2019         // queue to wait for thread(s) to complete.
       
  2020         if ( request.iIdCount > 0 )
       
  2021             {
       
  2022             if ( result == KErrNone )
       
  2023                 {
       
  2024                 iAsyncStopQueue.Append( request );
       
  2025                 result = KRequestPending;
       
  2026                 }
       
  2027             }
       
  2028         }
       
  2029     LOGEXITFN1("CConnMonIAP::AsyncConnectionStopAllL()", result)
       
  2030     return result;
       
  2031     }
       
  2032 
       
  2033 // -----------------------------------------------------------------------------
       
  2034 // CConnMonIAP::StartAsyncStopThread
       
  2035 // Starts a new thread to asynchronously stop a connection. Returns KErrNone if
       
  2036 // thread started successfully.
       
  2037 // -----------------------------------------------------------------------------
       
  2038 //
       
  2039 TInt CConnMonIAP::StartAsyncStopThreadL( TInt aIndex )
       
  2040     {
       
  2041     LOGENTRFN("CConnMonIAP::StartAsyncStopThreadL()")
       
  2042     TInt err( KErrNone );
       
  2043     
       
  2044     if ( aIndex < 0 || aIndex >= iConnInfos.Count() )
       
  2045         {
       
  2046         return KErrArgument;
       
  2047         }
       
  2048 
       
  2049     iConnInfos[aIndex].iAsyncStopStatus = EConnMonAsyncStopStarted;
       
  2050     if ( iConnInfos[aIndex].iBearer < EBearerExternalCSD )
       
  2051         {
       
  2052         // Stop an internal connection
       
  2053         LOGIT("StartAsyncStopThreadL: connection type internal")
       
  2054 
       
  2055         // Cancel data volume and activity notifiers before stopping
       
  2056         if ( iConnInfos[aIndex].iDLDataNotifier != 0 )
       
  2057             {
       
  2058             iConnInfos[aIndex].iDLDataNotifier->Cancel();
       
  2059             }
       
  2060         if ( iConnInfos[aIndex].iULDataNotifier != 0 )
       
  2061             {
       
  2062             iConnInfos[aIndex].iULDataNotifier->Cancel();
       
  2063             }
       
  2064         if ( iConnInfos[aIndex].iDataVolumeAO != 0 )
       
  2065             {
       
  2066             iConnInfos[aIndex].iDataVolumeAO->Cancel();
       
  2067             }
       
  2068         if ( iConnInfos[aIndex].iActivityNotifier != 0 )
       
  2069             {
       
  2070             iConnInfos[aIndex].iActivityNotifier->Cancel();
       
  2071             }
       
  2072 
       
  2073         // Create and start a oneshot active object that starts and waits for
       
  2074         // the async stop thread to complete
       
  2075         CConnMonAsyncStopDaemon* asyncStopDaemon = CConnMonAsyncStopDaemon::NewL( this );
       
  2076         err = asyncStopDaemon->Start(
       
  2077                 iConnInfos[aIndex].iConnectionId,
       
  2078                 iConnInfos[aIndex].iIapId,
       
  2079                 iConnInfos[aIndex].iNetId );
       
  2080         if ( err )
       
  2081             {
       
  2082             // Error, cleanup the active object since it did not start
       
  2083             delete asyncStopDaemon;
       
  2084             }
       
  2085         }
       
  2086     else if ( iConnInfos[aIndex].iBearer == EBearerExternalGPRS     ||
       
  2087               iConnInfos[aIndex].iBearer == EBearerExternalEdgeGPRS ||
       
  2088               iConnInfos[aIndex].iBearer == EBearerExternalWCDMA    ||
       
  2089               iConnInfos[aIndex].iBearer == EBearerExternalCDMA2000 ||
       
  2090               ( !iConnInfos[aIndex].iBearerInfo.iInternal &&
       
  2091                 ( iConnInfos[aIndex].iBearerInfo.iBearer == EBearerInfoHSDPA ||
       
  2092                   iConnInfos[aIndex].iBearerInfo.iBearer == EBearerInfoHSUPA ||
       
  2093                   iConnInfos[aIndex].iBearerInfo.iBearer == EBearerInfoHSxPA ) ) )
       
  2094         {
       
  2095         // Stop an external PSD connection
       
  2096         LOGIT("StartAsyncStopThreadL: connection type external PSD")
       
  2097         if ( iPsdFax )
       
  2098             {
       
  2099             CConnMonAsyncStopDaemon* asyncStopDaemon = CConnMonAsyncStopDaemon::NewL( this );
       
  2100             err = asyncStopDaemon->Start(
       
  2101                     iConnInfos[aIndex].iConnectionId,
       
  2102                     iPsdFax );
       
  2103             if ( err )
       
  2104                 {
       
  2105                 // Error, cleanup the active object since it did not start
       
  2106                 delete asyncStopDaemon;
       
  2107                 }
       
  2108             }
       
  2109         else
       
  2110             {
       
  2111             err = KErrNotFound;
       
  2112             }
       
  2113         }
       
  2114     else if ( iConnInfos[aIndex].iBearer == EBearerExternalCSD   ||
       
  2115               iConnInfos[aIndex].iBearer == EBearerExternalHSCSD ||
       
  2116               iConnInfos[aIndex].iBearer == EBearerExternalWcdmaCSD )
       
  2117         {
       
  2118         LOGIT("AsyncConnectionStopL: Error, connection type external CSD not supported")
       
  2119         err = KErrNotSupported;
       
  2120         }
       
  2121     else
       
  2122         {
       
  2123         LOGIT("AsyncConnectionStopL: Error, unknown bearer")
       
  2124         err = KErrNotSupported;
       
  2125         }
       
  2126 
       
  2127     LOGEXITFN1("CConnMonIAP::StartAsyncStopThreadL()", err)
       
  2128     return err;
       
  2129     }
       
  2130 
       
  2131 // -----------------------------------------------------------------------------
       
  2132 // CConnMonIAP::CompleteAsyncStopReqs
       
  2133 // Called when an asynchronous connection stop has completed.
       
  2134 // This method will go through all pending async connection stop requests and
       
  2135 // mark this connections status to those requests that are waiting for it.
       
  2136 // Those requests that were only waiting for this connection anymore, are
       
  2137 // completed.
       
  2138 // -----------------------------------------------------------------------------
       
  2139 //
       
  2140 void CConnMonIAP::CompleteAsyncStopReqs( const TUint aConnectionId, const TInt aError )
       
  2141     {
       
  2142     LOGENTRFN("CConnMonIAP::CompleteAsyncStopReqs()")
       
  2143 
       
  2144     // Mark the internal status of the connection to stopped
       
  2145     TInt index = Index( aConnectionId );
       
  2146     LOGIT3("Connection %d (index %d) marked as stopped <%d>", aConnectionId, index, aError)
       
  2147     if ( index >= 0 )
       
  2148         {
       
  2149         iConnInfos[index].iAsyncStopStatus = EConnMonStopCompleted;
       
  2150         }
       
  2151 
       
  2152 #ifdef _DEBUG    
       
  2153     LOGIT1("iAsyncStopQueue contents (%d:)", iAsyncStopQueue.Count() )
       
  2154     for ( TInt i = 0; i < iAsyncStopQueue.Count(); i++ )
       
  2155         {
       
  2156         LOGIT1("iIdCount   = %d", iAsyncStopQueue[i].iIdCount)
       
  2157         LOGIT1("iStopCount = %d", iAsyncStopQueue[i].iStopCount)
       
  2158         LOGIT1("iStatus    = %d", iAsyncStopQueue[i].iStatus)
       
  2159         for ( TInt j = 0; j < iAsyncStopQueue[i].iIdCount; j++ )
       
  2160             {
       
  2161             LOGIT1(".       id = %02d", iAsyncStopQueue[i].iId[j])
       
  2162             }
       
  2163         }
       
  2164 #endif // _DEBUG
       
  2165 
       
  2166     // Go through all pending async stop requests. If a request has been
       
  2167     // cancelled, it has been removed from this array already.
       
  2168     for ( TInt i = 0; i < iAsyncStopQueue.Count(); i++ )
       
  2169         {
       
  2170         // Go through all connections related to this request
       
  2171         for ( TInt j = 0; j < iAsyncStopQueue[i].iIdCount; j++ )
       
  2172             {
       
  2173             // Was this request waiting for the connection that was just stopped
       
  2174             if ( iAsyncStopQueue[i].iId[j] == aConnectionId )
       
  2175                 {
       
  2176                 iAsyncStopQueue[i].iStopCount++;
       
  2177                 // Set iAsyncStopQueue[i].iId[j] to 0 if tracking is needed
       
  2178 
       
  2179                 // Store the first encountered error
       
  2180                 if ( aError && iAsyncStopQueue[i].iStatus == KErrNone )
       
  2181                     {
       
  2182                     iAsyncStopQueue[i].iStatus = aError;
       
  2183                     }
       
  2184                 }
       
  2185             }
       
  2186         // Have all connections stopped that this request is waiting for
       
  2187         if ( iAsyncStopQueue[i].iStopCount >= iAsyncStopQueue[i].iIdCount )
       
  2188             {
       
  2189             LOGIT3("Completing request at index %d, id count %d <%d>",
       
  2190                     i, iAsyncStopQueue[i].iIdCount, iAsyncStopQueue[i].iStatus)
       
  2191             iAsyncStopQueue[i].iMessage.Complete( iAsyncStopQueue[i].iStatus );
       
  2192             iAsyncStopQueue.Remove( i );
       
  2193             i--;
       
  2194             }
       
  2195         }
       
  2196     LOGEXITFN("CConnMonIAP::CompleteAsyncStopReqs()")
       
  2197     }
       
  2198 
       
  2199 // -----------------------------------------------------------------------------
       
  2200 // CConnMonIAP::CleanupConnectionInfo
       
  2201 // If the stopped connection is still in the connection info array and
       
  2202 // connection up/down notifier isn't running, removes the obsolete information
       
  2203 // and notifiers from the connection info array.
       
  2204 // -----------------------------------------------------------------------------
       
  2205 //
       
  2206 TInt CConnMonIAP::CleanupConnectionInfo( const TUint& aConnectionId )
       
  2207     {
       
  2208     LOGENTRFN("CConnMonIAP::CleanupConnectionInfo()")
       
  2209     TInt err( KErrNone );
       
  2210     
       
  2211     TInt index = Index( aConnectionId );
       
  2212     if ( index < 0 )
       
  2213         {
       
  2214         LOGIT("CleanupConnectionInfo: ID no longer in connection table")
       
  2215         err = KErrNotFound;
       
  2216         }
       
  2217     else
       
  2218         {
       
  2219         // If conn up/down notifier is active, let the event remove connection later.
       
  2220         if ( !iConnUpDownNotifier || !iConnUpDownNotifier->IsActive() )
       
  2221             {
       
  2222             err = RemoveConnection( iConnInfos[index] );
       
  2223             }
       
  2224         else
       
  2225             {
       
  2226             LOGIT("CleanupConnectionInfo: iConnUpDownNotifier active, letting event do cleanup")
       
  2227             err = KErrNone;
       
  2228             }
       
  2229         }
       
  2230     LOGEXITFN1("CConnMonIAP::CleanupConnectionInfo()", err)
       
  2231     return err;
       
  2232     }
       
  2233 
       
  2234 // -----------------------------------------------------------------------------
       
  2235 // CConnMonIAP::CancelAsyncStopReqs
       
  2236 // Cancels all asynchronous connection stop request for a specific client.
       
  2237 // -----------------------------------------------------------------------------
       
  2238 //
       
  2239 void CConnMonIAP::CancelAsyncStopReqs( CSession2* aSession )
       
  2240     {
       
  2241     LOGENTRFN("CConnMonIAP::CancelAsyncStopReqs()")
       
  2242 
       
  2243     LOGIT1("AsyncStopQueue count = %d", iAsyncStopQueue.Count())
       
  2244     for ( TInt i = 0; i < iAsyncStopQueue.Count(); i++ )
       
  2245         {
       
  2246         if ( iAsyncStopQueue[i].iMessage.Session() == aSession )
       
  2247             {
       
  2248             iAsyncStopQueue[i].iMessage.Complete( KErrCancel );
       
  2249             iAsyncStopQueue.Remove( i );
       
  2250             i--;
       
  2251             LOGIT1("Cancelled a request for session %d", aSession)
       
  2252             }
       
  2253         }
       
  2254 
       
  2255     LOGEXITFN("CConnMonIAP::CancelAsyncStopReqs()")
  1872     }
  2256     }
  1873 
  2257 
  1874 // -----------------------------------------------------------------------------
  2258 // -----------------------------------------------------------------------------
  1875 // CConnMonIAP::SetBoolAttributeL
  2259 // CConnMonIAP::SetBoolAttributeL
  1876 // Used for stopping the connections.
  2260 // Used for stopping the connections.