htiui/HtiCommPlugins/HtiBtCommPlugin/HtiBtCommServer/src/HtiBtCommServer.cpp
branchRCL_3
changeset 11 454d022d514b
parent 9 404ad6c9bc20
child 12 aefcba28a3e0
child 13 822c625f7adc
equal deleted inserted replaced
9:404ad6c9bc20 11:454d022d514b
     1 /*
       
     2 * Copyright (c) 2009 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: This file contains the implementation of the
       
    15 *              CHtiBtCommServer class.
       
    16 *              CHtiBtCommServer handles Symbian server side operations
       
    17 *              such as server starting and client session creation.
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include "HtiBtClientServerCommon.h"
       
    24 #include "HtiBtCommServer.h"
       
    25 #include "HtiBtCommServerSession.h"
       
    26 #include "Logger.h"
       
    27 
       
    28 #include <e32base.h>
       
    29 #include <e32std.h>
       
    30 #include <e32svr.h>
       
    31 #include <e32uid.h>
       
    32 
       
    33 // CONSTANTS
       
    34 // For memory allocations
       
    35 const TUint KBtCommServerHeapSizeMin       = 0x6000;
       
    36 const TUint KBtCommServerHeapSizeMax       = 0x20000;
       
    37 const TUint KBtCommServerStackSize         = 0x8000;
       
    38 
       
    39 
       
    40 //*****************************************************************************
       
    41 //
       
    42 // Class CHtiBtCommServer
       
    43 //
       
    44 //*****************************************************************************
       
    45 
       
    46 /*---------------------------------------------------------------------------*/
       
    47 CHtiBtCommServer::CHtiBtCommServer( TInt aPriority )
       
    48     : CServer2( aPriority, ESharableSessions )
       
    49     {
       
    50     LOGFW(DebugLog(_L("CHtiBtCommServer: CHtiBtCommServer()")))
       
    51     __DECLARE_NAME(_S( "CHtiBtCommServer" ));
       
    52     }
       
    53 
       
    54 /*---------------------------------------------------------------------------*/
       
    55 void CHtiBtCommServer::ConstructL()
       
    56     {
       
    57     LOGFW(DebugLog(_L("CHtiBtCommServer: ConstructL()")))
       
    58     }
       
    59 
       
    60 /*---------------------------------------------------------------------------*/
       
    61 CHtiBtCommServer* CHtiBtCommServer::NewL()
       
    62     {
       
    63     LOGFW(DebugLog(_L("CHtiBtCommServer: NewL()"));)
       
    64 
       
    65     CHtiBtCommServer *pS =
       
    66         new (ELeave) CHtiBtCommServer( EBtCommServerPriority );
       
    67     CleanupStack::PushL( pS );
       
    68     __ASSERT_ALWAYS( pS != NULL, PanicServer( ESvrCreateServer ) );
       
    69 
       
    70     pS->ConstructL();
       
    71     CleanupStack::Pop( pS );
       
    72     User::SetProcessCritical( User::ENotCritical );
       
    73     User::SetCritical( User::ENotCritical );
       
    74     LOGFW(InfoLog(_L("Server was started")))
       
    75     return pS;
       
    76     }
       
    77 
       
    78 /*---------------------------------------------------------------------------*/
       
    79 CHtiBtCommServer::~CHtiBtCommServer()
       
    80     {
       
    81     LOGFW(DebugLog(_L("CHtiBtCommServer: ~CHtiBtCommServer()")))
       
    82     }
       
    83 
       
    84 /*---------------------------------------------------------------------------*/
       
    85 void CHtiBtCommServer::SessionFreed()
       
    86     {
       
    87     LOGFW(DebugLog(_L("CHtiBtCommServer: SessionFreed(): Stopping active scheduler"));)
       
    88     iSession = NULL; // iSession is owned by server framework, not deleted here
       
    89     CActiveScheduler::Stop();
       
    90     LOGFW(DebugLog(_L("CHtiBtCommServer: SessionFreed(): Done"));)
       
    91     }
       
    92 
       
    93 /*---------------------------------------------------------------------------*/
       
    94 void CHtiBtCommServer::SessionCreated( CHtiBtCommServerSession* aSession )
       
    95     {
       
    96     LOGFW(DebugLog(_L("CHtiBtCommServer: SessionCreated()"));)
       
    97     iSession = aSession;
       
    98     }
       
    99 
       
   100 /*---------------------------------------------------------------------------*/
       
   101 CSession2* CHtiBtCommServer::NewSessionL( const TVersion &aVersion,
       
   102     const RMessage2& aMessage ) const
       
   103     {
       
   104     LOGFW(DebugLog(_L("CHtiBtCommServer: NewSessionL() - IPC V2"));)
       
   105     aMessage.IsNull();
       
   106     if ( iSession )
       
   107         User::Leave( KErrAlreadyExists ); // Allow only one session
       
   108 
       
   109     // Check that server is the right version
       
   110     TVersion ver( KBtCommServerMajorVersionNumber,
       
   111                   KBtCommServerMinorVersionNumber,
       
   112                   KBtCommServerBuildVersionNumber );
       
   113     if ( !User::QueryVersionSupported( ver, aVersion ) )
       
   114         {
       
   115         User::Leave( KErrNotSupported );
       
   116         }
       
   117     return CHtiBtCommServerSession::NewL( (CHtiBtCommServer*)this );
       
   118     }
       
   119 
       
   120 /*---------------------------------------------------------------------------*/
       
   121 GLDEF_C TInt CHtiBtCommServer::ThreadFunction( TAny* anArg )
       
   122     {
       
   123     LOGFW(_L("CHtiBtCommServer: ThreadFunction(): Starting"));
       
   124 
       
   125     __UHEAP_MARK;
       
   126 
       
   127     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   128 
       
   129     // Convert argument into semaphore reference
       
   130     RSemaphore& semaphore = *(RSemaphore*)anArg;
       
   131 
       
   132     // Start scheduler...
       
   133     LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Installing active scheduler"));)
       
   134     CActiveScheduler *pA = new CActiveScheduler;
       
   135     __ASSERT_ALWAYS( pA != NULL, PanicServer( EMainSchedulerError ) );
       
   136     CActiveScheduler::Install( pA );
       
   137 
       
   138     CHtiBtCommServer* pS = NULL;
       
   139     TRAPD(err,
       
   140         // ...and server
       
   141         LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Creating server instance"));)
       
   142         pS = CHtiBtCommServer::NewL();
       
   143         )
       
   144 
       
   145     if ( err != KErrNone )
       
   146         {
       
   147         LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Failed creating server instance"));)
       
   148         }
       
   149 
       
   150     LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Starting server"));)
       
   151     __ASSERT_ALWAYS( pS->Start(KBtCommServerName) == KErrNone,
       
   152         PanicServer( ESvrStartServer ) ); // Make first request pending,
       
   153 
       
   154     // Signal that server has started
       
   155     LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Signalling client: server is up and running"));)
       
   156     semaphore.Signal();
       
   157 
       
   158     LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Waiting for server's death"));)
       
   159     // Start receiving requests from clients
       
   160     CActiveScheduler::Start();
       
   161     LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Server was stopped"));)
       
   162     LOGFW(InfoLog(_L("Server was stopped")))
       
   163     delete pS;
       
   164     LOGFW(DebugLog(_L("CHtiBtCommServer: ThreadFunction(): Server was deleted"));)
       
   165 
       
   166     // Finished
       
   167     delete pA;
       
   168     pA = NULL;
       
   169 
       
   170      // Destroy clean-up stack
       
   171     delete cleanup;
       
   172     cleanup = NULL;
       
   173 
       
   174     __UHEAP_MARKEND;
       
   175     return KErrNone;
       
   176     }
       
   177 
       
   178 /*---------------------------------------------------------------------------*/
       
   179 GLDEF_C void PanicServer( TBtCommServerPanic aPanic )
       
   180     {
       
   181     LOGFW(DebugLog(_L("CHtiBtCommServer: PanicServer()"));)
       
   182     _LIT( KTxtServerPanic, "BtCommServer panic" );
       
   183     User::Panic( KTxtServerPanic, aPanic );
       
   184     }
       
   185 
       
   186 /*---------------------------------------------------------------------------*/
       
   187 EXPORT_C TInt StartThread()
       
   188     {
       
   189 //    LOGFW(_L("CHtiBtCommServer: StartThread()"));
       
   190     TInt res = KErrNone;
       
   191 
       
   192     // Create server - if one of this name does not already exist
       
   193     TFindServer findBtCommServer( KBtCommServerName );
       
   194     TFullName name;
       
   195     if ( findBtCommServer.Next( name ) != KErrNone ) // Server doesn't exist
       
   196         {
       
   197          // Create a semaphore to know when thread initialization has finished
       
   198         RSemaphore semaphore;
       
   199         semaphore.CreateLocal(0);
       
   200 
       
   201 //        LOGFW(_L("CHtiBtCommServer: Created Semaphore...\n"));
       
   202 
       
   203         // Create new server thread and thread's main function
       
   204         RThread thread;
       
   205         res = thread.Create( KBtCommServerName,
       
   206                              CHtiBtCommServer::ThreadFunction,
       
   207                              KBtCommServerStackSize,
       
   208                              KBtCommServerHeapSizeMin,
       
   209                              KBtCommServerHeapSizeMax,
       
   210                              &semaphore );
       
   211 
       
   212 
       
   213         if ( res == KErrNone ) // Thread created ok - now start it going
       
   214             {
       
   215 //            LOGFW(_L("CHtiBtCommServer: StartThread() - Create OK"));
       
   216 
       
   217             thread.SetPriority( EPriorityNormal );
       
   218 //            TRequestStatus stat1;
       
   219 //            thread.Logon(stat1);
       
   220 
       
   221             thread.Resume();  // Start it going
       
   222 
       
   223             semaphore.Wait(); // Wait until it's initialized
       
   224 
       
   225             thread.Close();   // No longer interest in the other thread
       
   226             }
       
   227         else // Thread not created ok
       
   228             {
       
   229             // No further interest in it
       
   230 //            LOGFW(_L("CHtiBtCommServer: StartThread() - Create FAIL"));
       
   231             thread.Close();
       
   232             }
       
   233         semaphore.Close();
       
   234         }
       
   235     return res;
       
   236     }
       
   237 
       
   238 /*---------------------------------------------------------------------------*/
       
   239 GLDEF_C TInt E32Main()
       
   240     {
       
   241     return KErrNone;
       
   242     }
       
   243 
       
   244 // End of the file