messagingappbase/ncnlist/src/CNcnMobileSignalStrengthHandler.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002 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:   Mobile signal strength handler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <mmtsy_names.h>
       
    22 #include "CNcnMobileSignalStrengthHandler.h"
       
    23 #include "MNcnSignalStrengthObserver.h"
       
    24 #include "NcnDebug.h"
       
    25 
       
    26 // -----------------------------------------------------------------
       
    27 // CNcnMobileSignalStrengthHandler::NewL
       
    28 // -----------------------------------------------------------------
       
    29 CNcnMobileSignalStrengthHandler* CNcnMobileSignalStrengthHandler::NewL( )
       
    30     {
       
    31     CNcnMobileSignalStrengthHandler* self = new (ELeave) CNcnMobileSignalStrengthHandler();
       
    32     
       
    33     CleanupStack::PushL( self );
       
    34     self->ConstructL();
       
    35     CleanupStack::Pop( self );
       
    36     
       
    37     return self;
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------
       
    41 // CNcnMobileSignalStrengthHandler::~CNcnMobileSignalStrengthHandler
       
    42 // -----------------------------------------------------------------
       
    43 CNcnMobileSignalStrengthHandler::~CNcnMobileSignalStrengthHandler()
       
    44     {
       
    45     Cancel();
       
    46     
       
    47     // close session handles
       
    48     iMobilePhone.Close();
       
    49     iTelServer.Close();    
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------
       
    53 // CNcnMobileSignalStrengthHandler::CNcnMobileSignalStrengthHandler
       
    54 // -----------------------------------------------------------------
       
    55 CNcnMobileSignalStrengthHandler::CNcnMobileSignalStrengthHandler() :
       
    56     CActive( EPriorityNormal )
       
    57     {
       
    58     // empty
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------
       
    62 // CNcnMobileSignalStrengthHandler::ConstructL
       
    63 // -----------------------------------------------------------------
       
    64 void CNcnMobileSignalStrengthHandler::ConstructL()
       
    65     {
       
    66     // add AO to scheduler
       
    67     CActiveScheduler::Add( this );
       
    68     
       
    69     // initialize sessions
       
    70     InitializeSessionsL();
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------
       
    74 // CNcnMobileSignalStrengthHandler::RunL
       
    75 // -----------------------------------------------------------------
       
    76 void CNcnMobileSignalStrengthHandler::RunL()
       
    77     {
       
    78     NCN_RDEBUG( _L("CNcnMobileSignalStrengthHandler::RunL") );
       
    79 
       
    80     // if operation completed successfully
       
    81     if( iStatus.Int() == KErrNone )
       
    82         {
       
    83         NCN_RDEBUG_INT( _L("CNcnMobileSignalStrengthHandler::RunL - Signal strength updated to %d"),
       
    84             iSignalStrength );
       
    85         
       
    86         NotifyObserver();
       
    87         }
       
    88         
       
    89     // if observing is still active reissue the notification request    
       
    90     if( iObserving )
       
    91         {
       
    92         NCN_RDEBUG( _L("CNcnMobileSignalStrengthHandler::RunL - NotifySignalStrengthChange") );
       
    93        // request
       
    94         iMobilePhone.NotifySignalStrengthChange( iStatus, iSignalStrength, iBars );
       
    95     
       
    96         // activate
       
    97         SetActive();
       
    98         }
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------
       
   102 // CNcnMobileSignalStrengthHandler::RunError
       
   103 // -----------------------------------------------------------------
       
   104 TInt CNcnMobileSignalStrengthHandler::RunError( TInt aError )
       
   105     {
       
   106     return aError;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------
       
   110 // CNcnMobileSignalStrengthHandler::DoCancel
       
   111 // -----------------------------------------------------------------
       
   112 void CNcnMobileSignalStrengthHandler::DoCancel()
       
   113     {
       
   114     // cancel notification request
       
   115     // if we're observing
       
   116     if( iObserving )
       
   117         {
       
   118         iMobilePhone.CancelAsyncRequest( EMobilePhoneNotifySignalStrengthChange );
       
   119         }    
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------
       
   123 // CNcnMobileSignalStrengthHandler::ObservingSupported
       
   124 // -----------------------------------------------------------------
       
   125 TBool CNcnMobileSignalStrengthHandler::ObservingSupported()
       
   126     {
       
   127     TUint32 caps;
       
   128     iMobilePhone.GetSignalCaps( caps );
       
   129     
       
   130     return ( caps & RMobilePhone::KCapsNotifySignalStrengthChange );
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------
       
   134 // CNcnMobileSignalStrengthHandler::RequestsSupported
       
   135 // -----------------------------------------------------------------    
       
   136 TBool CNcnMobileSignalStrengthHandler::RequestsSupported()
       
   137     {
       
   138     TUint32 caps;
       
   139     iMobilePhone.GetSignalCaps( caps );
       
   140     
       
   141     return ( caps & RMobilePhone::KCapsGetSignalStrength );
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------
       
   145 // CNcnMobileSignalStrengthHandler::SetSignalStrengthObserverL
       
   146 // -----------------------------------------------------------------
       
   147 void CNcnMobileSignalStrengthHandler::SetSignalStrengthObserverL(
       
   148     MNcnSignalStrengthObserver* aObserver )
       
   149     {
       
   150     // leave if observing is not supported
       
   151     if( !ObservingSupported() )
       
   152         {
       
   153         User::Leave( KErrNotSupported );
       
   154         }
       
   155         
       
   156     iObserver = aObserver;
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------
       
   160 // CNcnMobileSignalStrengthHandler::StartObservingL
       
   161 // -----------------------------------------------------------------
       
   162 void CNcnMobileSignalStrengthHandler::StartObservingL()
       
   163     {
       
   164     // leave if observing is not supported
       
   165     if( !ObservingSupported() )
       
   166         {
       
   167         User::Leave( KErrNotSupported );
       
   168         }        
       
   169     
       
   170     // update signal str
       
   171     TRAPD( err, UpdateSignalStrengthL() );
       
   172     
       
   173     // check operation
       
   174     if( err != KErrNone )
       
   175         {        
       
   176         NCN_RDEBUG_INT( _L("CNcnMobileSignalStrengthHandler::StartObservingL - Failed to initialize signal strength (err = %d)"),
       
   177             err );
       
   178         iSignalStrength = 0;
       
   179         }
       
   180     else
       
   181         {
       
   182         NCN_RDEBUG_INT( _L("CNcnMobileSignalStrengthHandler::StartObservingL - Signal strength initialized to %d"),
       
   183             iSignalStrength );
       
   184         }
       
   185     
       
   186     // update state
       
   187     iObserving = ETrue;
       
   188     
       
   189     // Notify current observer about signal strength
       
   190     NotifyObserver();
       
   191     
       
   192     // request for notification    
       
   193     iMobilePhone.NotifySignalStrengthChange( iStatus, iSignalStrength, iBars );
       
   194     
       
   195     // activate
       
   196     SetActive();                            
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------
       
   200 // CNcnMobileSignalStrengthHandler::StopObservingL
       
   201 // -----------------------------------------------------------------
       
   202 void CNcnMobileSignalStrengthHandler::StopObservingL()
       
   203     {
       
   204     if( !iObserving )
       
   205         {
       
   206         User::Leave( KErrGeneral );
       
   207         }
       
   208         
       
   209     // cancel the request    
       
   210     Cancel();
       
   211         
       
   212     // update state
       
   213     iObserving = EFalse;
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------
       
   217 // CNcnMobileSignalStrengthHandler::SignalStrengthL
       
   218 // -----------------------------------------------------------------
       
   219 TInt CNcnMobileSignalStrengthHandler::SignalStrengthL()
       
   220     {
       
   221     // leave if requests are not supported
       
   222     if( !RequestsSupported() )
       
   223         {
       
   224         User::Leave( KErrNotSupported );
       
   225         }
       
   226         
       
   227     // if we're not observing we have to update the value manually,
       
   228     // otherwise value is up-to-date    
       
   229     if( !iObserving )
       
   230         {
       
   231         UpdateSignalStrengthL();
       
   232         }
       
   233     
       
   234     // return the current value
       
   235     return iSignalStrength;        
       
   236     }
       
   237     
       
   238 // -----------------------------------------------------------------
       
   239 // CNcnMobileSignalStrengthHandler::BarValueL
       
   240 // -----------------------------------------------------------------
       
   241 TInt CNcnMobileSignalStrengthHandler::BarValueL()
       
   242     {
       
   243     // leave if requests are not supported
       
   244     if( !RequestsSupported() )
       
   245         {
       
   246         User::Leave( KErrNotSupported );
       
   247         }
       
   248         
       
   249     // if we're not observing we have to update the value manually,
       
   250     // otherwise value is up-to-date    
       
   251     if( !iObserving )
       
   252         {
       
   253         UpdateSignalStrengthL();
       
   254         }
       
   255     
       
   256     // return the current value
       
   257     return iBars;      
       
   258     }
       
   259     
       
   260 // -----------------------------------------------------------------
       
   261 // CNcnMobileSignalStrengthHandler::InitializeSessionsL
       
   262 // -----------------------------------------------------------------    
       
   263 void CNcnMobileSignalStrengthHandler::InitializeSessionsL()
       
   264     {
       
   265     // connect to tel server
       
   266     User::LeaveIfError( iTelServer.Connect() );
       
   267     
       
   268     RTelServer::TPhoneInfo phoneInfo;
       
   269     TInt numPhone;
       
   270     
       
   271     // load tsy module and get number of phones
       
   272     User::LeaveIfError( iTelServer.LoadPhoneModule( KMmTsyModuleName ) );
       
   273     User::LeaveIfError( iTelServer.EnumeratePhones( numPhone) );
       
   274 
       
   275     // find the correct phone
       
   276     for (TInt i(0); i < numPhone; i++)
       
   277         {
       
   278         TName tsyName;
       
   279         User::LeaveIfError( iTelServer.GetPhoneInfo( i, phoneInfo ) );
       
   280         User::LeaveIfError( iTelServer.GetTsyName( i,tsyName ) );
       
   281 
       
   282         if ( tsyName.CompareF( KMmTsyModuleName ) == 0)
       
   283             {
       
   284             break;
       
   285             }
       
   286         }
       
   287     
       
   288     // open mobile phone session    
       
   289     User::LeaveIfError( iMobilePhone.Open( iTelServer, phoneInfo.iName ) );        
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------
       
   293 // CNcnMobileSignalStrengthHandler::UpdateSignalStrengthL
       
   294 // -----------------------------------------------------------------
       
   295 void CNcnMobileSignalStrengthHandler::UpdateSignalStrengthL()
       
   296     {
       
   297     // only update the value if requests are supported
       
   298     if( RequestsSupported() )
       
   299         {
       
   300         TRequestStatus status;                
       
   301         
       
   302         // get signal strength
       
   303         iMobilePhone.GetSignalStrength( status, iSignalStrength, iBars );
       
   304         
       
   305         // Wait for request since we need to be able to supply
       
   306         // the signal strength from syncrhonous access method (we
       
   307         // have to wait somewhere anyway)
       
   308         User::WaitForRequest( status );
       
   309         
       
   310         NCN_RDEBUG_INT( _L("CNcnMobileSignalStrengthHandler::UpdateSignalStrengthL - GetSignalStrength returned with %d"),
       
   311             status.Int() );
       
   312         
       
   313         // leave if error was encountered
       
   314         User::LeaveIfError( status.Int() );
       
   315         }
       
   316     }
       
   317 
       
   318 // -----------------------------------------------------------------
       
   319 // CNcnMobileSignalStrengthHandler::NotifyObserver
       
   320 // -----------------------------------------------------------------    
       
   321 void CNcnMobileSignalStrengthHandler::NotifyObserver()
       
   322     {
       
   323     // notify observer, if set
       
   324     if( iObserver )
       
   325         {
       
   326         TRAP_IGNORE( iObserver->SignalStrengthAndBarUpdatedL( iSignalStrength, iBars ) );
       
   327         }
       
   328     }
       
   329 
       
   330 //  End of File