systemswstubs/asyreference/src/ASYReferenceMainServiceBase.cpp
changeset 46 e1758cbb96ac
parent 0 0ce1b5ce9557
equal deleted inserted replaced
43:e71858845f73 46:e1758cbb96ac
       
     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:  Used as an reference implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <AccessoryControl.h>
       
    22 #include <AccessoryServer.h>
       
    23 #include <AccConGenericID.h>
       
    24 #include <AccConfigFileParser.h>
       
    25 #include <AccPolProprietaryNameValuePairs.h>
       
    26 #include "ASYReferenceMainServiceBase.h"
       
    27 #include "acc_debug.h"
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // MACROS
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 // MODULE DATA STRUCTURES
       
    40 
       
    41 // LOCAL FUNCTION PROTOTYPES
       
    42 
       
    43 // FORWARD DECLARATIONS
       
    44 
       
    45 // ============================= LOCAL FUNCTIONS ===============================
       
    46 
       
    47 // ============================ MEMBER FUNCTIONS ===============================
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // 
       
    51 // Two-phased constructor.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CASYReferenceMainServiceBase::CASYReferenceMainServiceBase()    
       
    55     : iAccessoryControl()
       
    56     , iAccConfigFileParser( NULL )
       
    57     , iState( EStateUnknown )
       
    58     , iConnectedAccessory( NULL )
       
    59     {
       
    60 
       
    61     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::CASYReferenceMainServiceBase() - Enter" );
       
    62     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::CASYReferenceMainServiceBase() - Return" );
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // 
       
    67 // Two-phased constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 void CASYReferenceMainServiceBase::ConstructL()
       
    71     {    
       
    72     
       
    73     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::ConstructL() - Enter" );
       
    74     
       
    75     // ConfigFileParser instance is created for GID parsing.
       
    76     iAccConfigFileParser = CAccConfigFileParser::NewL( KNullDesC );    
       
    77     
       
    78     // AccessoryControl instance is created for Accessory Connection / Disconnection handling.
       
    79     iAccessoryControl = new ( ELeave ) RAccessoryControl();    
       
    80 
       
    81     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::ConstructL() - Return" );
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // 
       
    86 // Two-phased constructor.
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 CASYReferenceMainServiceBase* CASYReferenceMainServiceBase::NewL()
       
    90     {
       
    91 
       
    92     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::NewL() - Enter" );
       
    93 
       
    94     CASYReferenceMainServiceBase * self = new ( ELeave ) CASYReferenceMainServiceBase();
       
    95     CleanupStack::PushL( self );
       
    96     self->ConstructL();
       
    97     CleanupStack::Pop( self );
       
    98 
       
    99     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::NewL() - Return" );
       
   100 
       
   101     return self;
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // 
       
   106 // destructor.
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 CASYReferenceMainServiceBase::~CASYReferenceMainServiceBase()
       
   110     {
       
   111 
       
   112     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::~CASYReferenceMainServiceBase() - Enter" );
       
   113 
       
   114     if( iAccessoryControl )
       
   115         {
       
   116         iAccessoryControl->CloseSubSession();
       
   117         }
       
   118     
       
   119     delete iConnectedAccessory;
       
   120     iConnectedAccessory = NULL;
       
   121 
       
   122     delete iAccessoryControl;
       
   123     iAccessoryControl = NULL;
       
   124 
       
   125     delete iAccConfigFileParser;
       
   126     iAccConfigFileParser = NULL;
       
   127     
       
   128     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::~CASYReferenceMainServiceBase() - Return" );
       
   129     
       
   130     }
       
   131 
       
   132 // ----------------------------------------------------------------------------------
       
   133 // CASYReferenceMainServiceBase::StartL
       
   134 // ----------------------------------------------------------------------------------
       
   135 //
       
   136 TInt CASYReferenceMainServiceBase::StartL()
       
   137     {
       
   138     
       
   139     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::StartL() - Enter" );
       
   140 
       
   141     //
       
   142     //CASYReferenceMainServiceBase internal state is change to InitWait
       
   143     //
       
   144   StateChangeL( EStateInitWait );
       
   145 
       
   146     TInt err ( KErrNone );
       
   147    
       
   148    
       
   149    	//
       
   150    	// Following syntax is used from now on to illustrate ASY sequences.
       
   151    	//
       
   152    	   	
       
   153     //
       
   154     // Comments of initialization sequence during terminal boot are marked as [A.n].
       
   155     //
       
   156 
       
   157     //
       
   158     // Comments of accessory connection sequence are marked as [B.n]
       
   159     //
       
   160     
       
   161     //
       
   162     // Comments of accessory disconnection sequence are marked as [C.n]
       
   163     //    
       
   164 
       
   165     //
       
   166     // Comments of internal state change of accessory feature are marked as [D.n]
       
   167     // Example external headphones are connected to headset control unit.
       
   168 
       
   169     //
       
   170     // Comments of getting/setting accessory internal state are marked as [E.n]
       
   171     //
       
   172 
       
   173     // [A.1] Signals Accessory Server that ASY implementation is ready to receive commands.     
       
   174     //       Process command by init parameter is received after signal method call 
       
   175     //       (Receiver in this example is CASYReferenceCmdHandlerBase class).
       
   176     Signal();
       
   177     
       
   178     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::StartL() - Return" );
       
   179 
       
   180     return err;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CASYReferenceMainServiceBase::ProcessResponseL
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CASYReferenceMainServiceBase::ProcessResponseL( TASYMessage& /*aMessage*/ )
       
   188     {    
       
   189     
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CASYReferenceMainServiceBase::ProcessErrorResponseL
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 void CASYReferenceMainServiceBase::ProcessErrorResponseL(
       
   197     TInt aErrorCode,
       
   198     TASYMessage& aMessage )
       
   199     {
       
   200     
       
   201     (void) aMessage;
       
   202     (void) aErrorCode;
       
   203     
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CASYReferenceMainServiceBase::RaiseEventL
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CASYReferenceMainServiceBase::RaiseEventL( TASYMessage& aMessage )
       
   211     {
       
   212     
       
   213     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::RaiseEventL() - Enter" );
       
   214     
       
   215     //
       
   216     // [B.1], [C.1], [D.1] Accessory events are received from Accessory Driver/Domestic OS.
       
   217     //
       
   218     if( aMessage.GroupId() == KASYMessageGroupAccessoryEvent )
       
   219         {
       
   220         // Event processing according the accessory event.
       
   221         switch( aMessage.MessageId() )
       
   222             {
       
   223             case KASYMsgAccessoryConnectionStateChangedEvent:
       
   224                 {
       
   225     
       
   226                 // Accessory connection state is changed.
       
   227                 TASYMsgAccessoryConnectionStateChangedEventBuf package;
       
   228                 package.Copy( aMessage.Data() );
       
   229                          
       
   230                 ConnectionStateChangedL( package() );
       
   231                 }
       
   232                 break;
       
   233             case KASYMsgAccessoryFeatureStateChangedEvent:
       
   234                 {
       
   235                 
       
   236                 // State of accessory feature is changed.
       
   237                 TASYMsgAccessoryFeatureStateChangedEventBuf package;
       
   238                 package.Copy( aMessage.Data() );
       
   239 
       
   240                 AccessoryFeatureStateBooleanChangedL( package() );
       
   241                 }
       
   242                 break;
       
   243             default:                
       
   244                 User::Leave( KErrGeneral );
       
   245                 break;
       
   246             };
       
   247         }
       
   248     else
       
   249         {        
       
   250         User::Leave( KErrGeneral );
       
   251         }
       
   252     
       
   253     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::RaiseEventL() - Return" );
       
   254     }
       
   255 
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CASYReferenceMainServiceBase::StateChangeL
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 void CASYReferenceMainServiceBase::StateChangeL( TState aState )
       
   262     {
       
   263     
       
   264     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::StateChangeL() - Enter" );
       
   265 
       
   266     TInt err ( KErrGeneral );
       
   267 
       
   268     switch ( aState )
       
   269         {
       
   270         case EStateInitWait:
       
   271             {
       
   272    
       
   273             if ( iState != EStateUnknown )
       
   274                 {
       
   275                 User::Leave( KErrGeneral );
       
   276                 }
       
   277             else
       
   278                 {                
       
   279                 //
       
   280                 // initialization of event subscribe(s) can be done here.
       
   281                 //
       
   282                 }
       
   283             }
       
   284             break;
       
   285         case EStateInitOngoing:
       
   286             {
       
   287             
       
   288             if ( iState != EStateInitWait )
       
   289                 {            
       
   290                 User::Leave( KErrGeneral );
       
   291                 }
       
   292                 //
       
   293                 // Current state extra handling can be added here, if needed.
       
   294                 //
       
   295             }
       
   296             break;
       
   297         case EStateInitOk:
       
   298             {            
       
   299             if ( iState != EStateInitOngoing )
       
   300                 {             
       
   301                 User::Leave( KErrGeneral );
       
   302                 }
       
   303             else
       
   304                 { 
       
   305                 //
       
   306                 // Initialized sequence is ready, and session can be created to accessory control.
       
   307                 //                               
       
   308                 err = iAccessoryControl->CreateSubSession( CASYMainServiceBase::Session() );                
       
   309 
       
   310                 if ( err != KErrNone )
       
   311                     {                    
       
   312                     User::Leave( err );
       
   313                     }
       
   314                 }
       
   315             }
       
   316             break;
       
   317         
       
   318         default:
       
   319             {            
       
   320             User::Leave( KErrGeneral );
       
   321             }
       
   322             break;
       
   323         }
       
   324 
       
   325     //
       
   326     // Example class state change.
       
   327     //
       
   328     iState = aState;
       
   329 
       
   330     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::StateChangeL() - Return" );
       
   331 
       
   332     }
       
   333     
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // CASYReferenceMainServiceBase::ConnectionStateChangedL
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 void CASYReferenceMainServiceBase::ConnectionStateChangedL( AccessoryConnection::TConnectionStateInfoStruct& aState )
       
   340     {
       
   341 
       
   342     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::ConnectionStateChangedL() - Enter" );
       
   343     //
       
   344     // Accessory Connection handling example.
       
   345     //    
       
   346     TRequestStatus status;
       
   347 
       
   348     switch( aState.iState )
       
   349         {
       
   350         case AccessoryConnection::EStateConnected:
       
   351             {
       
   352 
       
   353             //
       
   354             // [B.2] Event of an accessory connection is raised to ASY from a Accessory Driver/Domestic OS.
       
   355             //
       
   356 
       
   357             // Delete old Generic ID
       
   358             if( iConnectedAccessory )
       
   359                 {
       
   360                
       
   361                 delete iConnectedAccessory;
       
   362                 iConnectedAccessory = NULL;
       
   363                 }
       
   364                 
       
   365             TUint64 accessoryId( aState.iAccessoryId );
       
   366             
       
   367 
       
   368             iConnectedAccessory = CAccConGenericID::NewL();
       
   369 
       
   370             // [B.3] Parse a new Generic ID according to the accessory spesific ID.
       
   371             iAccConfigFileParser->FindL( iConnectedAccessory, accessoryId, KNullDesC );
       
   372    
       
   373             // Check if accessory is not supported
       
   374             if( ( iConnectedAccessory->GenericID().UniqueID() ) < 0 )
       
   375                 {
       
   376                 // Accessory is not supported. Debug purpose.
       
   377                 }
       
   378             else
       
   379                 {
       
   380                 // Accessory is supported. Debug purpose.
       
   381                 }
       
   382             
       
   383             // [B.4] Send connect accessory request to Accessory Server
       
   384             iAccessoryControl->ConnectAccessory( status, iConnectedAccessory, EFalse );
       
   385             
       
   386             // the WaitForRequest method can be used because ConnectAccessory is immediately complete by
       
   387             // Accessory Server.
       
   388             User::WaitForRequest( status );
       
   389             
       
   390             }
       
   391             break;
       
   392         case AccessoryConnection::EStateNotConnected:
       
   393             {
       
   394 
       
   395             //
       
   396             // [C.2] Event of an accessory disconnection is raised to ASY from a Accessory Driver/Domestic OS.
       
   397             //
       
   398 
       
   399             // [C.3] Get current Generic ID.
       
   400             TAccPolGenericID genericId = iConnectedAccessory->GenericID();
       
   401 
       
   402             // [C.4] Send disconnect request to Accessory Server.
       
   403             iAccessoryControl->DisconnectAccessory( status, genericId );
       
   404 
       
   405             // the WaitForRequest can be used because DisconnectAccessory is immediately complete by
       
   406             // Accessory Server.
       
   407             User::WaitForRequest( status );
       
   408            
       
   409             delete iConnectedAccessory;
       
   410             iConnectedAccessory = NULL;
       
   411             }
       
   412             break;
       
   413         default:
       
   414             {
       
   415             User::Leave( KErrGeneral );
       
   416             }
       
   417             break;
       
   418         }
       
   419 
       
   420     if ( status.Int() != KErrNone )
       
   421         {
       
   422 
       
   423         }
       
   424 
       
   425     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::ConnectionStateChangedL() - Return" );
       
   426     }
       
   427 
       
   428 
       
   429 // -----------------------------------------------------------------------------
       
   430 // CASYReferenceMainServiceBase::AccessoryFeatureStateBooleanChangedL
       
   431 // -----------------------------------------------------------------------------
       
   432 void CASYReferenceMainServiceBase::AccessoryFeatureStateBooleanChangedL( AccessoryFeature::TFeatureStateBooleanStruct& aState )
       
   433     {
       
   434 
       
   435     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::AccessoryFeatureStateBooleanChangedL() - Enter" );
       
   436 
       
   437     // [D.2] the accessory internal state is change.
       
   438     
       
   439     TASYCommandParamRecord record;
       
   440 
       
   441     // See accessory feature example --> MapAccessoryFeatureL method. 
       
   442     MapAccessoryFeatureL( aState.iFeature, record );
       
   443     
       
   444     TAccValueTypeTBool state;
       
   445     state.iValue = aState.iState;
       
   446 
       
   447     //Current Generic ID
       
   448     TAccPolGenericID genericId = iConnectedAccessory->GenericID();    
       
   449 
       
   450     // [D.4] Accessory internal state change is indicated to Accessory Server.  
       
   451     iAccessoryControl->AccessoryValueChangedNotifyL( genericId, record.iNameRecord, state );
       
   452 
       
   453     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::AccessoryFeatureStateBooleanChangedL() - Return" );
       
   454     
       
   455     }
       
   456     
       
   457 // -----------------------------------------------------------------------------
       
   458 // CASYReferenceMainServiceBase::MapAccessoryFeatureL
       
   459 // -----------------------------------------------------------------------------
       
   460 void CASYReferenceMainServiceBase::MapAccessoryFeatureL( AccessoryFeature::TAccessoryFeature& aAccessoryFeature, 
       
   461                                                          TASYCommandParamRecord& aCommandParamRecord )
       
   462     {
       
   463         
       
   464     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::AccessoryFeatureStateBooleanChangedL() - Enter" );
       
   465 
       
   466     // Couple feature examples
       
   467     switch( aAccessoryFeature )
       
   468         {
       
   469         
       
   470         // [D.3]
       
   471         //A headphone is not connected to the sound adapter which means that the accessory is not detected as a control connection (the headset icon is not shown on the phone display).
       
   472         //Audio Out Connector capability value is updated by ASY when the headphone is connected to the sound adapter, which means that the accessory connection is updated as a control connection.
       
   473          
       
   474         case AccessoryFeature::EHeadphonesConnected:
       
   475             {            
       
   476             aCommandParamRecord.iNameRecord.SetNameL( KAccAudioOutConnector );
       
   477             }
       
   478             break;
       
   479         case AccessoryFeature::EExternalPowerSourceConnected:
       
   480             {            
       
   481             aCommandParamRecord.iNameRecord.SetNameL( KAccExtPowerSupply );
       
   482             }
       
   483             break;
       
   484         
       
   485         default:
       
   486             {            
       
   487             User::Leave( KErrGeneral );
       
   488             }
       
   489             break;
       
   490         }    
       
   491 
       
   492     COM_TRACE_( "[AccFW:AsyReference] CASYReferenceMainServiceBase::AccessoryFeatureStateBooleanChangedL() - Return" );
       
   493 
       
   494     }
       
   495 
       
   496 // End of file
       
   497