hwrmhaptics/hapticsserver/src/hwrmhapticssession.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 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:  Haptics server session implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "hwrmhapticssession.h"
       
    20 #include "hwrmhapticsserver.h"
       
    21 #include "hwrmhapticsservice.h"
       
    22 #include "hwrmhapticstrace.h"
       
    23 
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // C++ constructor
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CHWRMHapticsSession::CHWRMHapticsSession()    
       
    30     {
       
    31     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::CHWRMHapticsSession()" ) ) );
       
    32     }    
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // Two-phased constructor.
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CHWRMHapticsSession* CHWRMHapticsSession::NewL()
       
    39     {
       
    40     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::NewL()" ) ) );
       
    41 
       
    42     CHWRMHapticsSession* self = new ( ELeave ) CHWRMHapticsSession();
       
    43 
       
    44     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::NewL - return 0x%x" ), self ) );
       
    45 
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // CreateL calls server's AddSession to increment session count.
       
    51 // This method is called by CServer2 when creating new session.
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 void CHWRMHapticsSession::CreateL() 
       
    55     {
       
    56     MyServer().AddSession();
       
    57     }
       
    58 
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // This method is called by CServer2 when disconnecting the session.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CHWRMHapticsSession::Disconnect(const RMessage2& aMessage)
       
    65     {
       
    66     // close device as client may fail to call CloseActuator()
       
    67     if ( iService )
       
    68         {
       
    69         TRAP_IGNORE( iService->ExecuteMessageL( aMessage ) );
       
    70         }
       
    71     CSession2::Disconnect( aMessage );
       
    72     }
       
    73 
       
    74    
       
    75 // ---------------------------------------------------------------------------
       
    76 // Destructor
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CHWRMHapticsSession::~CHWRMHapticsSession()
       
    80     {
       
    81     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::~CHWRMHapticsSession()" ) ) );
       
    82 
       
    83     delete iService;
       
    84     iService = NULL;
       
    85 
       
    86     // reduce the amount of session the server is handling
       
    87     MyServer().DropSession();
       
    88 
       
    89 
       
    90     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::~CHWRMHapticsSession - return" ) ) );
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // Calls request handling functions. Also traps any leaves and signals 
       
    95 // client if error occurs.
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CHWRMHapticsSession::ServiceL( const RMessage2& aMessage )
       
    99     {
       
   100     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::ServiceL(0x%x)" ), aMessage.Handle() ) );
       
   101 
       
   102     // Reset completion need flag
       
   103     iCompletionNeeded = ETrue;
       
   104     
       
   105     TRAPD( error, DispatchMessageL( aMessage ) );
       
   106 
       
   107     if ( error != KErrNone )
       
   108         {
       
   109         COMPONENT_TRACE( ( _L("CHWRMHapticsSession::ServiceL(): Error in DispatchMessageL: %d"), error ) );
       
   110         iCompletionNeeded = ETrue;
       
   111         }
       
   112 
       
   113     if ( iCompletionNeeded )
       
   114         {
       
   115         aMessage.Complete( error );
       
   116         }
       
   117 
       
   118     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::ServiceL - return" )) );
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // Handles commands received in RMessage2.
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 void CHWRMHapticsSession::DispatchMessageL( const RMessage2& aMessage )
       
   126     {
       
   127     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::DispatchMessageL(0x%x), client SID: 0x%x" ), aMessage.Function(), aMessage.SecureId().iId ) );
       
   128 
       
   129     // Check command code and call appropriate function
       
   130     if ( EHWRMHapticsService == aMessage.Function() )
       
   131         {
       
   132         if ( iService )
       
   133             {
       
   134             User::Leave( KErrAlreadyExists );
       
   135             }
       
   136 
       
   137         // Initialize haptics. This will initialize plugin manager, 
       
   138         // recervation handler and common data
       
   139         MyServer().InitHaptics();
       
   140 
       
   141         // get a pointer to the plugin manager
       
   142         CHWRMHapticsPluginManager* pluginHandler =
       
   143             MyServer().PluginManager();
       
   144 
       
   145         // get a pointer to the reservation handler
       
   146         CHWRMHapticsReservationHandler* reservationHandler = 
       
   147             MyServer().ReservationHandler();
       
   148 
       
   149         // get a pointer to the common data
       
   150         CHWRMHapticsCommonData* hapticsCommonData = 
       
   151             MyServer().HapticsCommonData();
       
   152         
       
   153         // if handlers or common data is NULL, this resource is not supported 
       
   154         // by device
       
   155         if ( !pluginHandler || !reservationHandler || !hapticsCommonData )
       
   156             {
       
   157             User::Leave( KErrNotSupported );
       
   158             }
       
   159         else
       
   160             {
       
   161             COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::DispatchMessageL - creating iService" )) );
       
   162 
       
   163             // create haptics service
       
   164             iService = CHWRMHapticsService::NewL( pluginHandler, 
       
   165                                                   reservationHandler, 
       
   166                                                   *hapticsCommonData,
       
   167                                                   aMessage );
       
   168             }
       
   169         }
       
   170     else // Cannot identify the message, it must be for service.
       
   171         {
       
   172         if ( iService )
       
   173             {
       
   174             iCompletionNeeded = iService->ExecuteMessageL( aMessage );
       
   175             }
       
   176         else
       
   177             {
       
   178             // iService not yet created
       
   179             User::Leave( KErrBadHandle );
       
   180             }
       
   181         }
       
   182 
       
   183     COMPONENT_TRACE( ( _L( "CHWRMHapticsSession::DispatchMessageL - return" ) ) );
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // Convenience method for returning a reference to the Server object that 
       
   188 // created this session
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 CHWRMHapticsServer& CHWRMHapticsSession::MyServer()
       
   192     {
       
   193     return *static_cast<CHWRMHapticsServer*>( 
       
   194         const_cast<CServer2*>( CSession2::Server() ) );
       
   195     }
       
   196 
       
   197 //  End of File