satengine/SatServer/Engine/src/CSatBIPUtils.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2002-2008 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:  Utility class for BIP
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <es_sock.h>
       
    20 #include <etelqos.h>
       
    21 #include "SatLog.h"
       
    22 #include "CSatBIPUtils.h"
       
    23 #include "CSatBIPGPRSDataChannel.h"
       
    24 #include "csatbipconnectionobserver.h"
       
    25 #include "MSatBIPDataChannel.h"
       
    26 #include "MSatBIPChannelStatusObserver.h"
       
    27 #include "MSatBIPDataAvailableObserver.h"
       
    28 //lint -e766 Used inside TRAP macro, lint misfunction.
       
    29 #include "EnginePanic.h"
       
    30 #include "TSatChannelIDInfo.h"
       
    31 #include "MSatUtils.h"
       
    32 #include "CSatApnHandler.h"
       
    33 #include "csatsactivewrapper.h"
       
    34 #include "msatmultimodeapi.h"
       
    35 
       
    36 const TInt KPDPContextPos( 7 );
       
    37 const TInt KMaxAvailChannels( 7 );
       
    38 const TInt8 KProposeQoSParametersPop( 2 );
       
    39 
       
    40 // ======== MEMBER FUNCTIONS ========
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Two-phased constructor.
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CSatBIPUtils* CSatBIPUtils::NewL( MSatUtils& aUtils )
       
    47     {
       
    48     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::NewL calling" )
       
    49 
       
    50     CSatBIPUtils* self = new ( ELeave ) CSatBIPUtils( aUtils );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop( self );
       
    54 
       
    55     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::NewL exiting" )
       
    56     return self;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Destructor
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CSatBIPUtils::~CSatBIPUtils()
       
    64     {
       
    65     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::~CSatBIPUtils calling" )
       
    66 
       
    67     // Close all data channels
       
    68     if ( iDataChannels )
       
    69         {
       
    70         iDataChannels->ResetAndDestroy();
       
    71         }
       
    72     delete iDataChannels;
       
    73 
       
    74     if ( iChannelIds )
       
    75         {
       
    76         iChannelIds->ResetAndDestroy();
       
    77         }
       
    78     delete iChannelIds;
       
    79     
       
    80     delete iChannelId;
       
    81     
       
    82     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::~CSatBIPUtils\
       
    83             Close connection and subconnection" )
       
    84     iSubConnection.Close();
       
    85     iConnection.Close();
       
    86     iSocketServer.Close();
       
    87     iPacketService.Close();
       
    88     
       
    89     delete iApnHandler;
       
    90 
       
    91     if ( iWrapper )
       
    92         {
       
    93         iWrapper->CancelWrapper();
       
    94         delete iWrapper;
       
    95         iWrapper = NULL;
       
    96         }
       
    97 
       
    98     if ( iConnObserver )
       
    99         {
       
   100         iConnObserver->Cancel();
       
   101         delete iConnObserver;
       
   102         iConnObserver = NULL;
       
   103         }
       
   104 
       
   105     iDataAvailableObserver = NULL;
       
   106     iChannelStatusObserver = NULL;
       
   107 
       
   108     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::~CSatBIPUtils exiting" )
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Handles event
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CSatBIPUtils::Event( TInt aEvent )
       
   116     {
       
   117     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::Event calling" )
       
   118 
       
   119     // Check event
       
   120     if ( MSatUtils::ECommandCancelled == aEvent ||
       
   121          MSatUtils::ECancelledUsingEndKey == aEvent )
       
   122         {
       
   123         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::Event check event" )
       
   124         if ( iDataChannels )
       
   125             {
       
   126             const TInt channelCount( iDataChannels->Count() );
       
   127             LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::Event channelCount: %d",
       
   128                   channelCount )
       
   129             // Cancel all channels
       
   130             for ( TInt index = 0; index < channelCount; index++ )
       
   131                 {
       
   132                 MSatBIPDataChannel* channel = iDataChannels->At( index );
       
   133                 __ASSERT_ALWAYS( channel,
       
   134                     PanicSatEngine( ESatEngineNullPointer ) );
       
   135                 channel->CancelAll();
       
   136                 }
       
   137             }
       
   138         }
       
   139 
       
   140     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::Event exiting" )
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // Creates new data channel
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 MSatBIPDataChannel* CSatBIPUtils::CreateChannelL(
       
   148     const TSatBearerType aBearerType, TInt& aErrCode )
       
   149     {
       
   150     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CreateChannelL calling" )
       
   151 
       
   152     TInt errCode( MSatBIPUtils::ESatBIPNoChannelAvail );
       
   153     MSatBIPDataChannel* channel = NULL;
       
   154 
       
   155     // Get available channel ID
       
   156     TSatChannelIdInfo* channelId( AvailableChannelID() );
       
   157 
       
   158     // Check is the channel available
       
   159     if ( channelId )
       
   160         {
       
   161         // Create the right type of data channel
       
   162         if ( MSatBIPUtils::ESatGPRS == aBearerType )
       
   163             {
       
   164             LOG( NORMAL, 
       
   165             "SATENGINE: CSatBIPUtils::CreateChannelL ESatGPRS" )
       
   166             // New GPRS data channel
       
   167             channel = CSatBIPGPRSDataChannel::NewLC( *this, iSocketServer,
       
   168                 *channelId, iConnection, iSubConnection );
       
   169             // Reserve channel ID
       
   170             channelId->ReserveChannel();
       
   171             // Add data channel to list
       
   172             iDataChannels->AppendL( channel );
       
   173             // Remove from CleanupStack
       
   174             CleanupStack::Pop( /* channel */ );
       
   175             // Return code
       
   176             errCode = MSatBIPUtils::ESatBIPSuccess;
       
   177 
       
   178 #ifndef SAT_USE_DUMMY_TSY
       
   179             // Start connection observer
       
   180             if ( !iConnObserver )
       
   181                 {
       
   182                 LOG( NORMAL, " Starting connection observer" )
       
   183                 iConnObserver = 
       
   184                     new ( ELeave ) CSatBIPConnectionObserver( *this );
       
   185                 iConnObserver->StartObserver();
       
   186                 }
       
   187 #endif
       
   188             }
       
   189         else
       
   190             {
       
   191             // Others that GPRS are not yet supported
       
   192             LOG( NORMAL, 
       
   193                 "SATENGINE: CSatBIPUtils::CreateChannelL Not supported" )
       
   194             errCode = MSatBIPUtils::ESatBIPNoChannelAvail;
       
   195             }
       
   196         }
       
   197     else
       
   198         {
       
   199         // Channel IDs not available
       
   200         LOG( NORMAL, 
       
   201             "SATENGINE: CSatBIPUtils::CreateChannelL Not available" )
       
   202         errCode = MSatBIPUtils::ESatBIPNoChannelAvail;
       
   203         }
       
   204 
       
   205     // Assign completion code
       
   206     aErrCode = errCode;
       
   207 
       
   208     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CreateChannelL exiting" )
       
   209     return channel;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // Closes the data channel
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 TInt CSatBIPUtils::CloseChannel( const TInt aChannelId )
       
   217     {
       
   218     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseChannel calling" )
       
   219 
       
   220     TInt errCode( MSatBIPUtils::ESatBIPChannelIdNotValid );
       
   221 
       
   222     TInt index( 0 );
       
   223     TBool channelClosed( EFalse );
       
   224 
       
   225     // Loop the list of data channels and close the correct one.
       
   226     while ( index < iDataChannels->Count() && !channelClosed )
       
   227         {
       
   228         MSatBIPDataChannel* channel = iDataChannels->At( index );
       
   229         __ASSERT_ALWAYS( channel, PanicSatEngine( ESatEngineNullPointer ) );
       
   230 
       
   231         if ( channel->ChannelId() == aChannelId )
       
   232             {
       
   233             LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseChannel \
       
   234             channel->ChannelId() == aChannelId" )
       
   235              // Makes next data channel creation possible
       
   236             iDataChannelActivated = EFalse;
       
   237             // Channel found, close it and return Success.
       
   238             channel->CloseChannel();
       
   239             // Delete data channel
       
   240             iDataChannels->Delete( index );
       
   241             delete channel;
       
   242             errCode = MSatBIPUtils::ESatBIPSuccess;
       
   243             channelClosed = ETrue;
       
   244             }
       
   245         // Goto next channel
       
   246         index++;
       
   247         }
       
   248     LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::CloseChannel index: %d", index )
       
   249 
       
   250     // Free the space left in possible Delete call.
       
   251     iDataChannels->Compress();
       
   252     
       
   253     // Close connections if there're no active channels
       
   254     if ( !IsContextActive() )
       
   255         {
       
   256         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseChannel\
       
   257             Close connection and subconnection" )
       
   258         iSubConnection.Close();
       
   259         SetSubConnectionActivated( EFalse );
       
   260         iConnection.Close();
       
   261         SetConnectionActivated( EFalse );
       
   262         
       
   263         if ( iConnObserver )
       
   264             {
       
   265             iConnObserver->Cancel();
       
   266             delete iConnObserver;
       
   267             iConnObserver = NULL;
       
   268             }
       
   269         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseChannel\
       
   270             Close iPacketService" )
       
   271         iPacketService.Close();
       
   272         iPacketServiceActive = EFalse;
       
   273         }
       
   274     
       
   275     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseChannel exiting" )
       
   276     return errCode;
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // Closes all data channels
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 void CSatBIPUtils::CloseAllChannels()
       
   284     {
       
   285     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseAllChannels calling" )
       
   286     LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::CloseAllChannels \
       
   287           channelcount: %d", iChannelIds->Count() )
       
   288     // Loop the list of data channels and close the correct one.
       
   289     for ( TInt i = 0; i < iChannelIds->Count(); i++ )
       
   290         {
       
   291         TSatChannelIdInfo* id = iChannelIds->At( i );
       
   292         // If channel is reserved -> Close it
       
   293         if ( id->IsReserved() )
       
   294             {
       
   295             LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseAllChannels \
       
   296             id->IsReserved()" )
       
   297             CloseChannel( id->ChannelId() );
       
   298             }
       
   299         }
       
   300     
       
   301     // Close subsession to socket server
       
   302     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseAllChannels\
       
   303             Close connection and subconnection" )
       
   304     iSubConnection.Close();
       
   305     SetSubConnectionActivated( EFalse );
       
   306     iConnection.Close();
       
   307     SetConnectionActivated( EFalse );
       
   308     
       
   309     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::CloseAllChannels exiting" )
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // Returns the correct data channel
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 MSatBIPDataChannel* CSatBIPUtils::DataChannel( const TInt aChannelId,
       
   317             TInt& aErrCode )
       
   318     {
       
   319     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataChannel calling" )
       
   320 
       
   321     // If the channel cannot be found, returns channel not valid error code
       
   322     TInt errCode( MSatBIPUtils::ESatBIPChannelIdNotValid );
       
   323     MSatBIPDataChannel* channel = NULL;
       
   324     TBool found( EFalse );
       
   325     TInt index( 0 );
       
   326 
       
   327     // Loop the list of data channels and return the correct one.
       
   328     while ( index < iDataChannels->Count() && !found )
       
   329         {
       
   330         channel = iDataChannels->At( index );
       
   331         __ASSERT_ALWAYS( channel, PanicSatEngine( ESatEngineNullPointer ) );
       
   332         // Compare channel ID's
       
   333         if ( channel->ChannelId() == aChannelId )
       
   334             {
       
   335             LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataChannel \
       
   336             channel->ChannelId() == aChannelId" )
       
   337             // Channel found
       
   338             found = ETrue;
       
   339             if ( channel->IsContextActive() )
       
   340                 {
       
   341                 LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataChannel \
       
   342                 IsContextActive" )
       
   343                 // Channel found and it is active
       
   344                 errCode = MSatBIPUtils::ESatBIPSuccess;
       
   345                 }
       
   346             else
       
   347                 {
       
   348                 // Channel found but it is closed
       
   349                 errCode = MSatBIPUtils::ESatBIPChannelClosed;
       
   350                 }
       
   351             }
       
   352         index++;
       
   353         }
       
   354     LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::DataChannel \
       
   355           index: %d", index )
       
   356     // Assign completion code to param
       
   357     aErrCode = errCode;
       
   358 
       
   359     LOG2( SIMPLE,
       
   360         "SATENGINE: CSatBIPUtils::DataChannel exiting with return code %d",
       
   361         errCode )
       
   362     return channel;
       
   363     }
       
   364 
       
   365 // ---------------------------------------------------------------------------
       
   366 // Generates TChannelStatus object
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 RSat::TChannelStatus CSatBIPUtils::GenerateChannelStatusL(
       
   370     const TInt aChannelId, TInt aStatus )
       
   371     {
       
   372     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::GenerateChannelStatusL calling" )
       
   373 
       
   374     // ChannelStatus's first byte contains channel ID and pdp context status,
       
   375     // second byte contains information. values can be '00' or '05'
       
   376     RSat::TChannelStatus channelStatus;
       
   377     TInt err( MSatBIPUtils::ESatBIPNoChannelAvail );
       
   378 
       
   379     // Used to check the PDP context state
       
   380     MSatBIPDataChannel* dataChannel = DataChannel( aChannelId, err );
       
   381 
       
   382     // If channel is not active, status is still needed
       
   383     if ( ESatBIPSuccess == err || ESatBIPChannelClosed == err )
       
   384         {
       
   385         // Generate the first byte
       
   386         TUint8 channelId( 0 );   // last 3 bits.
       
   387         TUint8 pdpContext( 0 );  // first bit
       
   388         TUint8 firstByte( 0 );    // First byte
       
   389 
       
   390         // Channel ID. Bits 6 to 8.
       
   391         channelId = dataChannel->ChannelStatusChannelId();
       
   392 
       
   393         // If PDP Context is active, first bit is 1
       
   394         if ( dataChannel->IsContextActive() )
       
   395             {
       
   396             LOG( SIMPLE, "SATENGINE: CSatBIPUtils::GenerateChannelStatusL \
       
   397             dataChannel IsContextActive" )
       
   398             pdpContext = 1 << KPDPContextPos;
       
   399             }
       
   400 
       
   401         // We don't need data channel anymore
       
   402         dataChannel = NULL;
       
   403 
       
   404         // All the information for the first byte is gathered
       
   405         firstByte = static_cast<TUint8>( pdpContext ^ channelId );
       
   406         channelStatus.Append( firstByte );
       
   407         // Second byte should go straigth
       
   408         channelStatus.Append( aStatus );
       
   409         // LOG values
       
   410         LOG2( NORMAL,
       
   411             "SATENGINE: CSatBIPUtils::GenerateChannelStatusL First byte: %b",
       
   412             firstByte )
       
   413         LOG2( NORMAL,
       
   414             "SATENGINE: CSatBIPUtils::GenerateChannelStatusL Second byte: %b",
       
   415             aStatus )
       
   416         }
       
   417     else
       
   418         {
       
   419         LOG2( NORMAL, "SATENGINE: CSatBIPUtils::GenerateChannelStatusL \
       
   420             Data channel not found: %i", err )
       
   421         User::LeaveIfError( KErrNotFound );
       
   422         }
       
   423 
       
   424     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::GenerateChannelStatusL exiting" )
       
   425     return channelStatus;
       
   426     }
       
   427 
       
   428 // ---------------------------------------------------------------------------
       
   429 // Returns the interface reference to APN handler
       
   430 // ---------------------------------------------------------------------------
       
   431 //
       
   432 MSatApnHandler& CSatBIPUtils::ApnHandler()
       
   433     {
       
   434     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ApnHandler calling" )
       
   435 
       
   436     // Assert, Apn handler cannot be NULL
       
   437     __ASSERT_ALWAYS( iApnHandler, PanicSatEngine( ESatEngineNullPointer ) );
       
   438 
       
   439     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ApnHandler exiting" )
       
   440     return *iApnHandler;
       
   441     }
       
   442 
       
   443 // ---------------------------------------------------------------------------
       
   444 // Returns the state of the PDP Context
       
   445 // ---------------------------------------------------------------------------
       
   446 //
       
   447 TBool CSatBIPUtils::IsContextActive()
       
   448     {
       
   449     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::IsContextActive calling" )
       
   450 
       
   451     TBool retVal( EFalse );
       
   452     TInt index( 0 );
       
   453     
       
   454     // Loop the list of data channels and end if active channel is found
       
   455     while ( index < iDataChannels->Count() && !retVal )
       
   456         {
       
   457         MSatBIPDataChannel* channel = iDataChannels->At( index );
       
   458         __ASSERT_ALWAYS( channel, PanicSatEngine( ESatEngineNullPointer ) );
       
   459         retVal = channel->IsContextActive();
       
   460         // Goto next channel
       
   461         index++;
       
   462         }
       
   463     LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::IsContextActive \
       
   464           index: %d", index )
       
   465     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::IsContextActive exiting" )
       
   466     return retVal;
       
   467     }
       
   468 
       
   469 // ---------------------------------------------------------------------------
       
   470 // Returns the array of data channels
       
   471 // ---------------------------------------------------------------------------
       
   472 //
       
   473 CArrayPtrFlat<MSatBIPDataChannel>* CSatBIPUtils::DataChannels()
       
   474     {
       
   475     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataChannels calling - exiting" )
       
   476     return iDataChannels;
       
   477     }
       
   478 
       
   479 // ---------------------------------------------------------------------------
       
   480 // Request to notify when data is available
       
   481 // ---------------------------------------------------------------------------
       
   482 //
       
   483 void CSatBIPUtils::NotifyDataAvailable(
       
   484     MSatBIPDataAvailableObserver* aObserver )
       
   485     {
       
   486     LOG( SIMPLE,
       
   487         "SATENGINE: CSatBIPUtils::NotifyDataAvailable calling - exiting" )
       
   488     iDataAvailableObserver = aObserver;
       
   489     }
       
   490 
       
   491 // ---------------------------------------------------------------------------
       
   492 // Request to notify when an error occurs in data channel
       
   493 // ---------------------------------------------------------------------------
       
   494 //
       
   495 void CSatBIPUtils::NotifyChannelStatus(
       
   496     MSatBIPChannelStatusObserver* aObserver )
       
   497     {
       
   498     LOG( SIMPLE,
       
   499         "SATENGINE: CSatBIPUtils::NotifyChannelStatus calling - exiting" )
       
   500     iChannelStatusObserver = aObserver;
       
   501     }
       
   502 
       
   503 // ---------------------------------------------------------------------------
       
   504 // Cancels the DataAvailable request
       
   505 // ---------------------------------------------------------------------------
       
   506 //
       
   507 void CSatBIPUtils::CancelDataAvailable()
       
   508     {
       
   509     LOG( SIMPLE,
       
   510         "SATENGINE: CSatBIPUtils::CancelDataAvailable calling - exiting" )
       
   511     iDataAvailableObserver = NULL;
       
   512     }
       
   513 
       
   514 // ---------------------------------------------------------------------------
       
   515 // Cancels the ChannelStatus request
       
   516 // ---------------------------------------------------------------------------
       
   517 //
       
   518 void CSatBIPUtils::CancelChannelStatus()
       
   519     {
       
   520     LOG( SIMPLE,
       
   521         "SATENGINE: CSatBIPUtils::CancelChannelStatus calling - exiting" )
       
   522     iChannelStatusObserver = NULL;
       
   523     }
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // DataAvailable event. Notifies observer.
       
   527 // ---------------------------------------------------------------------------
       
   528 //
       
   529 void CSatBIPUtils::DataAvailable( const TInt aChannelId, const TInt aLength )
       
   530     {
       
   531     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataAvailable calling" )
       
   532 
       
   533     // If there is an active request for this event, notify observer.
       
   534     if ( iDataAvailableObserver )
       
   535         {
       
   536         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataAvailable \
       
   537         iDataAvailableObserver true" )
       
   538         iDataAvailableObserver->DataAvailable( aChannelId, aLength );
       
   539         }
       
   540 
       
   541     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::DataAvailable exiting" )
       
   542     }
       
   543 
       
   544 // ---------------------------------------------------------------------------
       
   545 // ChannelStatus event. Notifies observer.
       
   546 // ---------------------------------------------------------------------------
       
   547 //
       
   548 void CSatBIPUtils::ChannelStatus( const TInt aChannelId, const TInt aStatus )
       
   549     {
       
   550     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ChannelStatus calling" )
       
   551 
       
   552     // If there is an active request for this event, notify observer.
       
   553     if ( iChannelStatusObserver )
       
   554         {
       
   555         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ChannelStatus \
       
   556         iChannelStatusObserver true" )
       
   557         iChannelStatusObserver->ChannelStatus( aChannelId, aStatus );
       
   558         }
       
   559 
       
   560     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ChannelStatus exiting" )
       
   561     }
       
   562 
       
   563 // ---------------------------------------------------------------------------
       
   564 // C++ default constructor can NOT contain any code, that
       
   565 // might leave.
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 CSatBIPUtils::CSatBIPUtils( MSatUtils& aUtils ) :
       
   569     iChannelId( NULL ),
       
   570     iUtils( aUtils )
       
   571     {
       
   572     LOG( SIMPLE,
       
   573         "SATENGINE: CSatBIPUtils::CSatBIPUtils calling-exiting" )
       
   574     }
       
   575 
       
   576 // ---------------------------------------------------------------------------
       
   577 // Two-phased constructor.
       
   578 // ---------------------------------------------------------------------------
       
   579 //
       
   580 void CSatBIPUtils::ConstructL()
       
   581     {
       
   582     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ConstructL calling" )
       
   583 
       
   584     // Create array for the data channels.
       
   585     iDataChannels =
       
   586         new ( ELeave ) CArrayPtrFlat<MSatBIPDataChannel>( KMaxAvailChannels );
       
   587 
       
   588     // Create array for Channel ID infos
       
   589     iChannelIds =
       
   590         new ( ELeave ) CArrayPtrFlat<TSatChannelIdInfo>( KMaxAvailChannels );
       
   591 
       
   592     TUint8 index = 1;
       
   593     
       
   594     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel1, index++ );
       
   595     iChannelIds->AppendL( iChannelId );
       
   596     iChannelId = NULL;
       
   597     
       
   598     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel2, index++ );
       
   599     iChannelIds->AppendL( iChannelId );
       
   600     iChannelId = NULL;
       
   601     
       
   602     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel3, index++ );
       
   603     iChannelIds->AppendL( iChannelId );
       
   604     iChannelId = NULL;
       
   605     
       
   606     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel4, index++ );
       
   607     iChannelIds->AppendL( iChannelId );
       
   608     iChannelId = NULL;
       
   609     
       
   610     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel5, index++ );
       
   611     iChannelIds->AppendL( iChannelId );
       
   612     iChannelId = NULL;
       
   613     
       
   614     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel6, index++ );
       
   615     iChannelIds->AppendL( iChannelId );
       
   616     iChannelId = NULL;
       
   617     
       
   618     iChannelId = new ( ELeave ) TSatChannelIdInfo( RSat::KChannel7, index++ );
       
   619     iChannelIds->AppendL( iChannelId );
       
   620     iChannelId = NULL;
       
   621     
       
   622     // Regiseter to listen events
       
   623     iUtils.RegisterL( this, MSatUtils::ECommandCancelled );
       
   624     iUtils.RegisterL( this, MSatUtils::ECancelledUsingEndKey );
       
   625 
       
   626     // Connect to Socket Server
       
   627     User::LeaveIfError( iSocketServer.Connect() );
       
   628     iPacketServiceActive = EFalse;
       
   629     iApnHandler = CSatApnHandler::NewL( iUtils );
       
   630 
       
   631     iWrapper = new ( ELeave ) CSatSActiveWrapper;
       
   632     
       
   633     SetConnectionActivated( EFalse );
       
   634     SetSubConnectionActivated( EFalse );
       
   635 
       
   636     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ConstructL exiting" )
       
   637     }
       
   638 
       
   639 // ---------------------------------------------------------------------------
       
   640 // Returns next available channel ID
       
   641 // ---------------------------------------------------------------------------
       
   642 //
       
   643 TSatChannelIdInfo* CSatBIPUtils::AvailableChannelID()
       
   644     {
       
   645     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::AvailableChannelID calling" )
       
   646 
       
   647     TSatChannelIdInfo* channelId = NULL;
       
   648 
       
   649     TInt index( 0 );
       
   650     TBool idFound( EFalse );
       
   651 
       
   652     // Loop channel IDs
       
   653     while ( index < iChannelIds->Count() && !idFound )
       
   654         {
       
   655         channelId = iChannelIds->At( index );
       
   656         // If channel is free, take it
       
   657         if ( !channelId->IsReserved() )
       
   658             {
       
   659             LOG( SIMPLE, "SATENGINE: CSatBIPUtils::AvailableChannelID \
       
   660             channel free" )
       
   661             idFound = ETrue;
       
   662             }
       
   663          // goto next channel
       
   664         index++;
       
   665         }
       
   666     LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::AvailableChannelID \
       
   667           index: %d", index )
       
   668     if ( !idFound )
       
   669         {
       
   670         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::AvailableChannelID \
       
   671         idFound false" )
       
   672         // If not found, return NULL
       
   673         channelId = NULL;
       
   674         }
       
   675 
       
   676     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::AvailableChannelID exiting" )
       
   677     return channelId;
       
   678     }
       
   679 
       
   680 // ---------------------------------------------------------------------------
       
   681 // Blocks synchronous requests
       
   682 // ---------------------------------------------------------------------------
       
   683 //
       
   684 RPacketQoS::TQoSGPRSNegotiated CSatBIPUtils::ProposeQoSParametersL(
       
   685     const RPacketQoS::TQoSGPRSRequested& aQoSParams )
       
   686     {
       
   687     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ProposeQoSParametersL calling" )
       
   688 
       
   689     // Number of contexts
       
   690     TInt count( ContextCount() ); // Get the number of contexts
       
   691     // QoS profile name
       
   692     TName profName;
       
   693     // Handle to the actual context
       
   694     RPacketContext packetContext;
       
   695     // QoS session
       
   696     RPacketQoS qos;
       
   697     // Context info
       
   698     RPacketService::TContextInfo info;
       
   699     // Error value for asynchronous calls
       
   700     TInt statusError( KErrNone );
       
   701 
       
   702     if ( count > 0 ) // At least one context found
       
   703         {
       
   704         // Get the last contexts info
       
   705         iPacketService.GetContextInfo( 
       
   706             iWrapper->RequestStatus(), count - 1, info );
       
   707 
       
   708         statusError = iWrapper->SetActiveAndWait(); // Wait for completion
       
   709         LOG2( SIMPLE, "     ContextInfo iActiveStatus: %i", statusError )
       
   710         LOG2( SIMPLE, "     ContextInfo CntxStatus: %i", info.iStatus )
       
   711         // Leave point
       
   712         User::LeaveIfError( statusError );
       
   713         }
       
   714     else
       
   715         {
       
   716         // Didn't find any contexts
       
   717         User::Leave( KErrNotFound );
       
   718         }
       
   719 
       
   720     // Get the context by name
       
   721     LOG( SIMPLE, "     Open existing context" )
       
   722     User::LeaveIfError(
       
   723         packetContext.OpenExistingContext( iPacketService, info.iName ) );
       
   724 
       
   725     CleanupClosePushL( packetContext );
       
   726 
       
   727     // Get the QoS Profile
       
   728     LOG( SIMPLE, "     Get QoS Profile name" )
       
   729     User::LeaveIfError( packetContext.GetProfileName( profName ) );
       
   730 
       
   731     // Get the instance of the QoS profile
       
   732     if ( profName.Length() > 0 )
       
   733         {
       
   734         LOG( SIMPLE, "     Open existing QoS profile" )
       
   735         User::LeaveIfError( qos.OpenExistingQoS( packetContext, profName ) );
       
   736         }
       
   737 
       
   738     CleanupClosePushL( qos );
       
   739 
       
   740     TPckg<RPacketQoS::TQoSGPRSRequested> qosPckg( aQoSParams );
       
   741 
       
   742     qos.SetProfileParameters( iWrapper->RequestStatus(), qosPckg );
       
   743 
       
   744     User::LeaveIfError( iWrapper->SetActiveAndWait() ); // Wait for completion
       
   745 
       
   746     // Get negotioted parameters
       
   747     RPacketQoS::TQoSGPRSNegotiated negParams;
       
   748     TPckg<RPacketQoS::TQoSGPRSNegotiated> negPckg( negParams );
       
   749     qos.GetProfileParameters( iWrapper->RequestStatus(), negPckg );
       
   750 
       
   751     User::LeaveIfError( iWrapper->SetActiveAndWait() ); // Wait for completion
       
   752 
       
   753     LOG2( SIMPLE, "     GetNegQoSParams Status: %i", statusError )
       
   754     LOG2( SIMPLE, "     GetNegQoSParams Reliability: %i", 
       
   755           negParams.iReliability )
       
   756     LOG2( SIMPLE, "     GetNegQoSParams Precedence: %i", 
       
   757           negParams.iPrecedence )
       
   758     LOG2( SIMPLE, "     GetNegQoSParams Delay: %i", negParams.iDelay )
       
   759     LOG2( SIMPLE, "     GetNegQoSParams Peak: %i", negParams.iPeakThroughput )
       
   760     LOG2( SIMPLE, "     GetNegQoSParams Mean: %i", negParams.iMeanThroughput )
       
   761 
       
   762     // Cleaning and closing: packetContext, qos
       
   763     CleanupStack::PopAndDestroy( KProposeQoSParametersPop );
       
   764 
       
   765     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ProposeQoSParametersL exiting" )
       
   766     return negParams;
       
   767     }
       
   768 
       
   769 // ---------------------------------------------------------------------------
       
   770 // Opens session to GPRS Service
       
   771 // ---------------------------------------------------------------------------
       
   772 //
       
   773 TInt CSatBIPUtils::GprsService()
       
   774     {
       
   775     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::GprsService calling" )
       
   776 
       
   777     TInt ret( KErrNone );
       
   778     if ( !iPacketServiceActive )
       
   779         {
       
   780         LOG( SIMPLE, "SATENGINE: CSatBIPUtils::GprsService \
       
   781         iPacketServiceActive false" )
       
   782         ret = iPacketService.Open( *( iUtils.MultiModeApi().Phone() ) );
       
   783         iPacketServiceActive = ETrue;
       
   784         }
       
   785 
       
   786     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::GprsService exiting" )
       
   787     return ret;
       
   788     }
       
   789 
       
   790 // ---------------------------------------------------------------------------
       
   791 // Gives reference to RPacketService
       
   792 // ---------------------------------------------------------------------------
       
   793 //
       
   794 RPacketService& CSatBIPUtils::PacketService()
       
   795     {
       
   796     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::PacketService calling" )
       
   797 
       
   798     GprsService();
       
   799 
       
   800     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::PacketService exiting" )
       
   801     return iPacketService;
       
   802     }
       
   803 
       
   804 // ---------------------------------------------------------------------------
       
   805 // CSatBIPUtils::ConnectionStatus
       
   806 // ---------------------------------------------------------------------------
       
   807 //
       
   808 RPacketService::TStatus CSatBIPUtils::ConnectionStatus() const
       
   809     {
       
   810     LOG( NORMAL,
       
   811         "SATENGINE: CSatBIPUtils::ConnectionStatus calling" )
       
   812 
       
   813 #ifndef SAT_USE_DUMMY_TSY
       
   814     RPacketService::TStatus status( RPacketService::EStatusSuspended );
       
   815 #else
       
   816     RPacketService::TStatus status( RPacketService::EStatusActive );
       
   817 #endif
       
   818 
       
   819     if ( iConnObserver )
       
   820         {
       
   821         LOG( NORMAL,
       
   822         "SATENGINE: CSatBIPUtils::ConnectionStatus iConnObserver true" )
       
   823         status = iConnObserver->Status();
       
   824         }
       
   825 
       
   826     LOG( NORMAL,
       
   827         "SATENGINE: CSatBIPUtils::ConnectionStatus exiting" )
       
   828     return status;
       
   829     }
       
   830 
       
   831 // ---------------------------------------------------------------------------
       
   832 // CSatBIPUtils::StopUdpLink
       
   833 // ---------------------------------------------------------------------------
       
   834 //
       
   835 void CSatBIPUtils::StopUdpLink()
       
   836     {
       
   837     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::StopUdpLink  calling" )
       
   838     if ( iDataChannels )
       
   839         {
       
   840         const TInt channelCount( iDataChannels->Count() );
       
   841         LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::StopUdpLink \
       
   842           channelCount: %d", channelCount )
       
   843         // Cancel all channels
       
   844         for ( TInt index = 0; index < channelCount; index++ )
       
   845             {
       
   846             MSatBIPDataChannel* channel = iDataChannels->At( index );
       
   847             __ASSERT_ALWAYS( channel,
       
   848                 PanicSatEngine( ESatEngineNullPointer ) );
       
   849             channel->StopUdpSocket();
       
   850             }
       
   851         }
       
   852     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::StopUdpLink exiting" )
       
   853     }
       
   854 
       
   855 // ---------------------------------------------------------------------------
       
   856 // Synchronous request complete
       
   857 // ---------------------------------------------------------------------------
       
   858 //
       
   859 TInt CSatBIPUtils::ContextCount()
       
   860     {
       
   861     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ContextCount calling" )
       
   862 
       
   863     // Number of contexts
       
   864     TInt count( 0 );
       
   865     TInt maxAllowed( 0 );
       
   866 
       
   867     // Init PacketService
       
   868     TInt error( GprsService() );
       
   869 
       
   870     if ( KErrNone == error )
       
   871         {
       
   872         // Enumerate contexts
       
   873         iPacketService.EnumerateContexts( 
       
   874             iWrapper->RequestStatus(), count, maxAllowed );
       
   875         error = iWrapper->SetActiveAndWait(); // Wait for completion
       
   876         LOG2( SIMPLE, "     EnumContexts Status: %i", error )
       
   877         LOG2( SIMPLE, "     EnumContexts Count: %i", count )
       
   878         LOG2( SIMPLE, "     EnumContexts MaxAllowed: %i", maxAllowed )
       
   879         // Check status
       
   880         if ( KErrNone != error )
       
   881             {
       
   882             LOG( SIMPLE, "SATENGINE: CSatBIPUtils::ContextCount \
       
   883             KErrNone != error" )
       
   884             count = 0;
       
   885             }
       
   886         }
       
   887 
       
   888     LOG2( SIMPLE, "SATENGINE: CSatBIPUtils::ContextCount exiting: %i", count )
       
   889     return count;
       
   890     }
       
   891 
       
   892 // ---------------------------------------------------------------------------
       
   893 // CSatBIPUtils::SetConnectionActivated()
       
   894 // ---------------------------------------------------------------------------
       
   895 //
       
   896 void CSatBIPUtils::SetConnectionActivated( TBool aSet )
       
   897     {
       
   898     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::SetConnectionActivated calling" )
       
   899     iConnectionActivated = aSet;
       
   900     LOG( SIMPLE, "SATENGINE: CSatBIPUtils::SetConnectionActivated exiting" )
       
   901     }
       
   902     
       
   903 // ---------------------------------------------------------------------------
       
   904 // CSatBIPUtils::IsConnectionActivated()
       
   905 // ---------------------------------------------------------------------------
       
   906 //
       
   907 TBool CSatBIPUtils::IsConnectionActivated()
       
   908     {
       
   909     LOG( SIMPLE, 
       
   910         "SATENGINE: CSatBIPUtils::IsConnectionActivated calling-exiting" )
       
   911     return iConnectionActivated;
       
   912     }
       
   913 
       
   914 // ---------------------------------------------------------------------------
       
   915 // CSatBIPUtils::SetSubConnectionActivated()
       
   916 // ---------------------------------------------------------------------------
       
   917 //
       
   918 void CSatBIPUtils::SetSubConnectionActivated( TBool aSet )
       
   919     {
       
   920     LOG( SIMPLE, 
       
   921         "SATENGINE: CSatBIPUtils::SetSubConnectionActivated calling" )
       
   922     iSubConnectionActivated = aSet;
       
   923     LOG( SIMPLE, 
       
   924         "SATENGINE: CSatBIPUtils::SetSubConnectionActivated exiting" )
       
   925     }
       
   926     
       
   927 // ---------------------------------------------------------------------------
       
   928 // CSatBIPUtils::IsSubConnectionActivated()
       
   929 // ---------------------------------------------------------------------------
       
   930 //
       
   931 TBool CSatBIPUtils::IsSubConnectionActivated()
       
   932     {
       
   933     LOG( SIMPLE, 
       
   934         "SATENGINE: CSatBIPUtils::IsSubConnectionActivated calling-exiting" )
       
   935     return iSubConnectionActivated;
       
   936     }
       
   937