bluetoothengine/btnotif/btnotifsrv/src/btnotifserver.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     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: Server class for handling commands from clients, and the 
       
    15 *                central class in btnotif thread.
       
    16 *
       
    17 */
       
    18 
       
    19 #include "btnotifserver.h"
       
    20 #include <btservices/btdevrepository.h>
       
    21 #include "btnotifsession.h"
       
    22 #include "btnotifconnectiontracker.h"
       
    23 #include "btnotifsettingstracker.h"
       
    24 #include "btnotificationmanager.h"
       
    25 #include "btnotifdeviceselector.h"
       
    26 #include "btnotifserversecpolicy.h"
       
    27 #include "btnotifclientserver.h"
       
    28 
       
    29 /**  Panic category */
       
    30 _LIT( KBTNotifPanic, "BTNotif panic" );
       
    31 
       
    32 /**  Timeout (10 sec) for shutting down the server 
       
    33  * (when BT power is off and no clients connected). */
       
    34 const TInt KBTNtoifShutdownTimeout = 10 * 1000 * 1000;
       
    35 
       
    36 // ======== LOCAL FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Start the server.
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 static void RunServerL()
       
    43     {
       
    44     BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
    45     
       
    46     (void) User::RenameThread( KBTNotifServerName );
       
    47     // Create and install the active scheduler for this thread.
       
    48     CActiveScheduler* scheduler = new( ELeave ) CActiveScheduler();
       
    49     CleanupStack::PushL( scheduler );
       
    50     CActiveScheduler::Install( scheduler );
       
    51     // create the server (and leave it on the cleanup stack)
       
    52     CBTNotifServer* notifServer = CBTNotifServer::NewLC();
       
    53     // Initialisation complete, now signal the client
       
    54     RProcess::Rendezvous( KErrNone );
       
    55         // The server is now up and running.
       
    56     BOstrace0( TRACE_NORMAL, DUMMY_DEVLIST, "[BTNOTIF]\t BTNotif server now up and running" );
       
    57     // The active scheduler runs during the lifetime of this thread.
       
    58     CActiveScheduler::Start();
       
    59     // Stopping the active scheduler means terminating the thread.
       
    60     // Cleanup the server and scheduler.
       
    61     CleanupStack::PopAndDestroy( notifServer );
       
    62     CleanupStack::PopAndDestroy( scheduler );
       
    63     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Panic the server.
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 void PanicServer( TInt aReason )
       
    71     {
       
    72     User::Panic( KBTNotifPanic, aReason );
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Panic the client through the client-side message.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void PanicClient( const RMessage2& aMessage, TInt aReason )
       
    80     {
       
    81     aMessage.Panic( KBTNotifPanic, aReason );
       
    82     }
       
    83 
       
    84 // ======== MEMBER FUNCTIONS ========
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // C++ default constructor
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CBTNotifServer::CBTNotifServer()
       
    91 :   CPolicyServer( EPriorityUserInput, KBTNotifServerPolicy )
       
    92     {
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // Symbian 2nd-phase constructor
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void CBTNotifServer::ConstructL()
       
   100     {
       
   101     // Add the server to the active scheduler (from CServer2):
       
   102     StartL( KBTNotifServerName );
       
   103     iAsyncCb = new( ELeave ) CAsyncCallBack( EPriorityHigh );
       
   104     TCallBack cb( AsyncConstructCb, this );
       
   105     iAsyncCb->Set( cb );
       
   106     iAsyncCb->CallBack();
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // Asynchronous 3rd-phase constructor
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CBTNotifServer::AsyncConstructL()
       
   114     {
       
   115     // The server class owns this registry object and provides it
       
   116     // as a singleton in the whole server process.
       
   117     // Server itself does not handle any registry events.
       
   118     // Classes that want to receive these events must register
       
   119     // observers via CBtDevRepository interface.
       
   120     iDevRep = CBtDevRepository::NewL();
       
   121     iNotificationMgr = CBTNotificationManager::NewL( this );
       
   122     iSettingsTracker = CBTNotifSettingsTracker::NewL( this );
       
   123     iConnectionTracker = CBTNotifConnectionTracker::NewL( this );
       
   124     iTimer = CDeltaTimer::NewL(CActive::EPriorityLow);
       
   125     TCallBack shutdownCb( ShutdownTimeout, this );
       
   126     iShutdownTimerEntry.Set( shutdownCb );
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // Callback for asynchronous construction.
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 TInt CBTNotifServer::AsyncConstructCb( TAny* aPtr )
       
   134     {
       
   135     TRAPD( err, ( (CBTNotifServer*) aPtr )->AsyncConstructL() );
       
   136     return err;
       
   137     }
       
   138 
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // NewLC.
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 CBTNotifServer* CBTNotifServer::NewLC()
       
   145     {
       
   146     CBTNotifServer* self = new( ELeave ) CBTNotifServer();
       
   147     CleanupStack::PushL( self );
       
   148     self->ConstructL();
       
   149     return self;
       
   150     }
       
   151 
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // Destructor
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 CBTNotifServer::~CBTNotifServer()
       
   158     {
       
   159     delete iDevSelector;
       
   160     delete iSettingsTracker;
       
   161     delete iConnectionTracker;
       
   162     delete iNotificationMgr;
       
   163     delete iAsyncCb;
       
   164     delete iTimer;
       
   165     delete iDevRep;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // Handle a change in BT power state.
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 void CBTNotifServer::HandlePowerStateChangeL( TBTPowerStateValue aState )
       
   173     {
       
   174     CheckIdle( aState );
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // Increase the session count.
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CBTNotifServer::AddSession()
       
   182     {
       
   183     ++iSessionCount;
       
   184     iTimer->Remove( iShutdownTimerEntry );
       
   185     }
       
   186 
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // Decrease the session count.
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void CBTNotifServer::RemoveSession()
       
   193     {
       
   194     if ( iSessionCount > 0 )
       
   195         {
       
   196         // session counter can't be less than 0
       
   197         --iSessionCount;
       
   198         }
       
   199     CheckIdle( iSettingsTracker->GetPowerState() );
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // get the singleton instance of device repository
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 CBtDevRepository& CBTNotifServer::DevRepository()
       
   207     {
       
   208     return *iDevRep;
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // get the singleton instance of device search notifier
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 CBTNotifDeviceSelector& CBTNotifServer::DeviceSelectorL()
       
   216     {
       
   217     if ( ! iDevSelector )
       
   218         {
       
   219         iDevSelector = CBTNotifDeviceSelector::NewL( *this );
       
   220         }
       
   221     return *iDevSelector;
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // From class CPolicyServer.
       
   226 // Create a new session object.
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 CSession2* CBTNotifServer::NewSessionL( const TVersion& aVersion, 
       
   230     const RMessage2& aMessage ) const
       
   231     {
       
   232     (void) aMessage;
       
   233     // Compare our version with client-side version, CServer2 requires that 
       
   234     // we leave if they are not compatible. 
       
   235     TVersion srvVersion( KBTNotifServerVersionMajor, KBTNotifServerVersionMinor, 
       
   236                           KBTNotifServerVersionBuild );
       
   237 
       
   238     if( !User::QueryVersionSupported( aVersion, srvVersion ) )
       
   239         {
       
   240         // EFalse is returned if our version is not less than or 
       
   241         // equal to the client version.
       
   242         User::Leave( KErrNotSupported );
       
   243         }
       
   244     return CBTNotifSession::NewL();
       
   245     }
       
   246 
       
   247 void CBTNotifServer::CheckIdle( TBTPowerStateValue aState )
       
   248     {
       
   249     // In special scenarios, we do not have to remove the timer and queue it 
       
   250     // again, but these scenarios rarely happen in end-user use cases. 
       
   251     iTimer->Remove( iShutdownTimerEntry );
       
   252     if ( iSessionCount == 0 && aState == EBTPowerOff )
       
   253         {
       
   254         // BT power is off, start the shutdown timer.
       
   255         TTimeIntervalMicroSeconds32 interval = KBTNtoifShutdownTimeout;
       
   256         iTimer->Queue( interval, iShutdownTimerEntry );
       
   257         }
       
   258     }
       
   259 
       
   260 TInt CBTNotifServer::ShutdownTimeout( TAny* aPtr )
       
   261     {
       
   262     (void) aPtr;
       
   263     CActiveScheduler::Stop();
       
   264     return KErrNone;    
       
   265     }
       
   266 
       
   267 // ======== GLOBAL FUNCTIONS ========
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // Main function of the executable.
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 GLDEF_C TInt E32Main()
       
   274     {
       
   275     // Disabled until memory leak in QT/Open C are fixed
       
   276     // __UHEAP_MARK;
       
   277     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   278     TInt err = KErrNoMemory;
       
   279     if ( cleanup )
       
   280         {
       
   281         TRAP( err, RunServerL() );
       
   282         delete cleanup;
       
   283         }
       
   284     // Disabled until memory leak in QT/Open C are fixed
       
   285     // __UHEAP_MARKEND;
       
   286     return err;
       
   287     }
       
   288 
       
   289