bluetoothengine/bteng/src/btengsrvstate.cpp
changeset 0 f63038272f30
child 8 0707dd69d236
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     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:  Bluetooth Engine server state machine class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <ecom/ecomresolverparams.h>
       
    21 
       
    22 #include "btengsrvstate.h"
       
    23 #include "btengserver.h"
       
    24 #include "btengsrvpluginmgr.h"
       
    25 #include "debug.h"
       
    26 
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // C++ default constructor
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CBTEngSrvState::CBTEngSrvState( CBTEngServer* aServer )
       
    35 :   iState( EInit ),
       
    36     iOperation( ESrvOpIdle ),
       
    37     iServer( aServer )
       
    38     {
       
    39     }
       
    40 
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Symbian 2nd-phase constructor
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 void CBTEngSrvState::ConstructL()
       
    47     {
       
    48     TRACE_FUNC_ENTRY
       
    49     // Create the active object handling asynchronous state transitions.
       
    50     // The state transitions need to be done fast, as it can be initiated 
       
    51     // through user interaction (i.e. the user is waiting).
       
    52     const TCallBack cb(&ChangeStateCb, this);
       
    53     iAsyncCallback = new(ELeave) CAsyncCallBack(cb, CActive::EPriorityHigh);
       
    54     TRACE_FUNC_EXIT
       
    55     }
       
    56 
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // NewL
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CBTEngSrvState* CBTEngSrvState::NewL( CBTEngServer* aServer )
       
    63     {
       
    64     CBTEngSrvState* self = new( ELeave ) CBTEngSrvState( aServer );
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL();
       
    67     CleanupStack::Pop( self );
       
    68     return self;
       
    69     }
       
    70 
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Destructor
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CBTEngSrvState::~CBTEngSrvState()
       
    77     {
       
    78     delete iAsyncCallback;
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // Returns the current state machine operation.
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 CBTEngSrvState::TBTEngSrvOperation CBTEngSrvState::CurrentOperation()
       
    87     {
       
    88     return iOperation;
       
    89     }
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // ?implementation_description
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CBTEngSrvState::StartStateMachineL( TBool aState )
       
    97     {
       
    98     TRACE_FUNC_ENTRY
       
    99     if( aState )
       
   100         {
       
   101         // Power on; starting state is initializing the stack
       
   102         iState = EInitBTStack;
       
   103         iOperation = EPowerOn;
       
   104         }
       
   105     else
       
   106         {
       
   107         // Power off, starting state is to disconnect the plug-ins
       
   108         iState = EDisconnectPlugins;
       
   109         iOperation = EPowerOff;
       
   110         iServer->UpdateCenRepPowerKeyL( EBTPowerOff );
       
   111         iServer->SetUiIndicatorsL();
       
   112         }
       
   113     ChangeState();
       
   114     }
       
   115 
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // ?implementation_description
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CBTEngSrvState::ChangeState()
       
   122     {
       
   123     TRACE_FUNC_ENTRY
       
   124     if(!iAsyncCallback->IsActive())
       
   125         {
       
   126         iAsyncCallback->CallBack();
       
   127         }
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // ?implementation_description
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 TInt CBTEngSrvState::ChangeStateCb(TAny* aThis)
       
   135     {
       
   136     TRAPD(err, static_cast<CBTEngSrvState*>(aThis)->RequestCompletedL());
       
   137     if(err != KErrNone)
       
   138         {
       
   139         static_cast<CBTEngSrvState*>(aThis)->HandleError(err);
       
   140         }
       
   141     return KErrNone;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // From class MBTEngActiveObserver.
       
   146 // ?implementation_description
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CBTEngSrvState::RequestCompletedL()
       
   150     {
       
   151     switch( iState )
       
   152         {
       
   153         case EInitBTStack:
       
   154             {
       
   155             iState = ELoadDIService;
       
   156             iServer->InitBTStackL();
       
   157             }
       
   158             break;
       
   159         case ELoadDIService:
       
   160             {
       
   161             iState = ELoadPlugins;
       
   162             iServer->ManageDIServiceL( ETrue );
       
   163             }
       
   164             break;
       
   165         case ELoadPlugins:
       
   166             {
       
   167             iState = ELoadNextPlugin;
       
   168             TEComResolverParams params;
       
   169             iServer->iPluginMgr->LoadProfilePluginsL( params );
       
   170             }
       
   171             break;
       
   172         case ELoadNextPlugin:
       
   173             {
       
   174                 // Only change the state when all plug-ins are loaded
       
   175             if( iServer->iPluginMgr->LoadPluginL() <= 0 )
       
   176                 {
       
   177                 iState = EIdle;
       
   178                 }
       
   179             }
       
   180             break;
       
   181         case EStopBTStack:
       
   182             {
       
   183             iState = EWaitingForPowerOff;
       
   184             iServer->StopBTStackL();
       
   185             }
       
   186             break;
       
   187         case EUnloadDIService:
       
   188             {
       
   189             iState = EStopBTStack;
       
   190             iServer->ManageDIServiceL( EFalse );
       
   191             }
       
   192             break;
       
   193         case EUnloadPlugins:
       
   194             {
       
   195             iState = EUnloadDIService;
       
   196             iServer->iPluginMgr->UnloadProfilePlugins();
       
   197             }
       
   198             break;
       
   199         case EDisconnectPlugins:
       
   200             {
       
   201             iState = EUnloadPlugins;
       
   202             // This is the starting state; the disconnect command has 
       
   203             // already been sent. Just one more loop to give more time
       
   204             // for disconnecting.
       
   205             }
       
   206             break;
       
   207         case EWaitingForPowerOff:
       
   208             {
       
   209             // Disconnecting all connections completed and 
       
   210             // called us back; we are done.
       
   211             iState = EIdle;
       
   212             }   // Fall through
       
   213         case EIdle:
       
   214             {
       
   215             if( iOperation == EPowerOn )
       
   216                 {
       
   217                 iServer->UpdateCenRepPowerKeyL( EBTPowerOn );
       
   218                 iServer->SetUiIndicatorsL();
       
   219                 }
       
   220             iOperation = ESrvOpIdle;
       
   221             iServer->CheckIdle();
       
   222             }
       
   223         case EInit:
       
   224         default:
       
   225             break;
       
   226         }
       
   227     // In state EWaitingForPowerOff, we change the state 
       
   228     // through a callback in the server.
       
   229     if( iOperation != ESrvOpIdle && iState != EWaitingForPowerOff )
       
   230         {
       
   231         ChangeState();
       
   232         }
       
   233     TRACE_FUNC_EXIT
       
   234     }
       
   235 
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // From class MBTEngActiveObserver.
       
   239 // Handles a leave in RunL/RequestCompletedL.
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CBTEngSrvState::HandleError(TInt aError)
       
   243     {
       
   244     TRACE_FUNC_ARG( ( _L( "error: %d" ), aError ) )
       
   245     
       
   246     (void) aError;
       
   247     // All critical operations are done in turning power on; all other
       
   248     // operations are recoverable i.e. we can do without loading/unloading
       
   249     // of services. So simply change the state to handle next state change.
       
   250     ChangeState();
       
   251     }