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