accessoryservices/autoaudioasy/src/cautoaudioasyservice.cpp
branchRCL_3
changeset 21 ccb4f6b3db21
equal deleted inserted replaced
20:1ddbe54d0645 21:ccb4f6b3db21
       
     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:  This class handles connect and disconnect to AccFw.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <acccongenericid.h>
       
    19 #include <accpolobjectcon.h>
       
    20 #include <accpolnamevaluerecord.h>
       
    21 #include <accconfigfileparser.h>
       
    22 #include <accpolcommonnamevaluepairs.h>
       
    23 #include <accessoryservicesinternalpskeys.h>
       
    24 #include <autoaudiopskeys.h>
       
    25 
       
    26 #include "cautoaudioasyservice.h"
       
    27 #include "cautoaudioasypropertyobserver.h"
       
    28 #include "acc_debug.h"
       
    29 
       
    30 #define SET_STATE(state) (iState=state)
       
    31  
       
    32 // ----------------------------------------------------------------------------------
       
    33 // CAutoAudioAsyService::CAutoAudioAsyService() 
       
    34 // ----------------------------------------------------------------------------------
       
    35 CAutoAudioAsyService::CAutoAudioAsyService ()
       
    36 	: CActive ( EPriorityStandard )
       
    37     {
       
    38     CActiveScheduler::Add ( this );
       
    39     }
       
    40 
       
    41 // ----------------------------------------------------------------------------------
       
    42 // CAutoAudioAsyService::NewL ()
       
    43 // ----------------------------------------------------------------------------------
       
    44 CAutoAudioAsyService* CAutoAudioAsyService::NewL ()
       
    45     {
       
    46     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::NewL" );
       
    47     CAutoAudioAsyService* self = new ( ELeave ) CAutoAudioAsyService ();
       
    48     CleanupStack::PushL ( self );
       
    49     self->ConstructL ();
       
    50     CleanupStack::Pop ( self );
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ----------------------------------------------------------------------------------
       
    55 // CAutoAudioAsyService::ConstructL ()
       
    56 // ----------------------------------------------------------------------------------
       
    57 void CAutoAudioAsyService::ConstructL ()
       
    58     {
       
    59     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::ConstructL" );
       
    60     User::LeaveIfError ( iAccessoryServer.Connect () );
       
    61     User::LeaveIfError ( iAccessoryControl.CreateSubSession ( iAccessoryServer ) );
       
    62     SET_STATE ( EServiceStateIdle );	
       
    63     }
       
    64 
       
    65 // ----------------------------------------------------------------------------------
       
    66 // CAutoAudioAsyService::~CAutoAudioAsyService()
       
    67 // ----------------------------------------------------------------------------------
       
    68 CAutoAudioAsyService::~CAutoAudioAsyService ()
       
    69     {
       
    70     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::~CAutoAudioAsyService" );
       
    71     Cancel ();
       
    72 	
       
    73 	delete iPropertyObserver;
       
    74     iAccessoryControl.CloseSubSession ();
       
    75     iAccessoryServer.Close ();
       
    76     
       
    77 	FreeResources ();
       
    78     }
       
    79 	
       
    80 void CAutoAudioAsyService::StartL ()
       
    81 	{
       
    82     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::StartL" );
       
    83 	iPropertyObserver = CAutoAudioAsyPropertyObserver::NewL ( KPSUidAccessoryServices, KPSAutoKeyRTPStreamingConnectionStatus, *this );
       
    84 	}
       
    85 
       
    86 // ----------------------------------------------------------------------------------
       
    87 // CAutoAudioAsyService::PropertyChange ()
       
    88 // Defined in MAutoAudioAsyPropertyListener interface.
       
    89 // Receives notifications about RTP Streaming channel property changes. Property changes occur
       
    90 // when accessory is connected or disconnected or headunit attached or detached
       
    91 // ----------------------------------------------------------------------------------
       
    92 void CAutoAudioAsyService::PropertyChange ( RProperty& aProperty, TUid aUid, TUint aKey, TInt aStatus )
       
    93     {
       
    94     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::PropertyChange" );
       
    95 
       
    96     TInt propValue;
       
    97     TInt status = aProperty.Get ( propValue );
       
    98     COM_TRACE_1 ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::PropertyChange -- request property value %d", propValue );
       
    99     
       
   100     if ( aUid == KPSUidAccessoryServices && aKey == KPSAutoKeyRTPStreamingConnectionStatus )
       
   101         {
       
   102         // Check if audio accessory has been connected
       
   103         if ( ( status == KErrNone ) && ( aStatus == KErrNone ) )
       
   104             {
       
   105 			if ( propValue == EAudioConnectionStatusUnidirectional ) // Current release supports only UniDirectional, so other key values ignored.
       
   106 				{
       
   107 				TInt err;
       
   108 				switch ( iState )
       
   109 					{
       
   110 					case EServiceStateIdle:
       
   111 						TRAP ( err, ConnectAccessoryL () );
       
   112 						if ( err != KErrNone )
       
   113 							{
       
   114 							COM_TRACE_1 ("[AccFW:AutoAudioAsy] ConnectAccessoryL error %d", err );
       
   115 							FreeResources (); // switch to EServiceStateIdle							
       
   116 							}
       
   117 						break;
       
   118 						
       
   119 					case EServiceStateConnecting:
       
   120 					case EServiceStateConnected:
       
   121 						// Nothing to do
       
   122 						break;
       
   123 					}
       
   124 				return;
       
   125 				}
       
   126             }
       
   127 			
       
   128         // Otherwise assume that it's disconnected
       
   129         AccessoryDisconnected ();
       
   130         }
       
   131     }
       
   132 
       
   133 // ----------------------------------------------------------------------------------
       
   134 // CAutoAudioAsyService::AccessoryDisconnected ()
       
   135 // Called when a disconnect notification is received
       
   136 // ----------------------------------------------------------------------------------
       
   137 void CAutoAudioAsyService::AccessoryDisconnected ()
       
   138     {
       
   139     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::AccessoryDisconnected" );
       
   140     TRequestStatus status;
       
   141     switch ( iState )
       
   142         {
       
   143         case EServiceStateConnected:
       
   144             iAccessoryControl.DisconnectAccessory ( status, iGenericId->GenericID () );
       
   145             User::WaitForRequest ( status );
       
   146             FreeResources (); // sets state to EServiceStateIdle
       
   147             break;
       
   148 
       
   149         case EServiceStateConnecting:
       
   150             Cancel ();
       
   151             break;
       
   152 
       
   153         default:
       
   154             // nothing to do in this case
       
   155             break;
       
   156         }
       
   157     }
       
   158 
       
   159 // ----------------------------------------------------------------------------------
       
   160 // CAutoAudioAsyService::ConnectAccessoryL ()
       
   161 // Notifies the accessory server and switches to EServiceStateConnecting state.
       
   162 // ----------------------------------------------------------------------------------
       
   163 void CAutoAudioAsyService::ConnectAccessoryL ()
       
   164     {
       
   165     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::ConnectAccessoryL" );
       
   166 
       
   167     RArray<TAccPolNameValueRecord> nameValueArray;
       
   168     CleanupClosePushL ( nameValueArray );
       
   169 
       
   170     // Set GenericID header
       
   171     COM_TRACE_ ( "[AccFW:AutoAudioAsy] Set generic header" );
       
   172     iGenericIDHeader.iAccessoryDeviceType   = KDTCarKit;
       
   173     iGenericIDHeader.iPhysicalConnection    = KPCUSB;
       
   174     iGenericIDHeader.iApplicationProtocol   = 0x0;
       
   175     iGenericIDHeader.iCapabilitiesSubblocks = KSBAudioSubblock;
       
   176     iGenericIDHeader.iHWDeviceID            = 0x0;
       
   177     iGenericIDHeader.iHWModelID				= KNullDesC;
       
   178 
       
   179     iGenericId = CAccConGenericID::NewL ();
       
   180 
       
   181     COM_TRACE_ ( "[AccFW:AutoAudioAsy] Set capabilities" );
       
   182     
       
   183     // Set "Audio Output Type" capability
       
   184 	COM_TRACE_ ( "[AccFW:AutoAudioAsy] KAccAudioOutputType " );
       
   185     nameValueArray.AppendL ( TAccPolNameValueRecord ( KAccAudioOutputType,
       
   186         EAccAudioOutPutTypePublic, EAPVInt, EAPVPolicy ) );
       
   187 
       
   188     // Set "Audio stereo" capability
       
   189 	COM_TRACE_ ( "[AccFW:AutoAudioAsy] KAccStereoAudio " );
       
   190 	nameValueArray.AppendL ( TAccPolNameValueRecord ( KAccStereoAudio ) );
       
   191 
       
   192     // Set "Integrated audio output"
       
   193 	COM_TRACE_ ( "[AccFW:AutoAudioAsy] KAccIntegratedAudioOutput " );
       
   194 	nameValueArray.AppendL ( TAccPolNameValueRecord ( KAccIntegratedAudioOutput ) );
       
   195 
       
   196 	// Set "RTP streaming"
       
   197 	COM_TRACE_ ( "[AccFW:AutoAudioAsy] KAccRTPStreaming " );
       
   198 	nameValueArray.AppendL ( TAccPolNameValueRecord ( KAccRTPStreaming ) );
       
   199 
       
   200     // Make generic id
       
   201     CAccConfigFileParser* accConfigFileParser = CAccConfigFileParser::NewL ( KNullDesC );
       
   202     CleanupStack::PushL ( accConfigFileParser );
       
   203     accConfigFileParser->ParseGenericIDL ( iGenericId, iGenericIDHeader, nameValueArray );
       
   204 
       
   205     // Connect accessory
       
   206     COM_TRACE_ ( "[AccFW:AutoAudioAsy] Connecting accessory..." );
       
   207     iAccessoryControl.ConnectAccessory ( iStatus, iGenericId, EFalse );
       
   208     SET_STATE ( EServiceStateConnecting );
       
   209     SetActive ();
       
   210 
       
   211     CleanupStack::PopAndDestroy ( accConfigFileParser );
       
   212     CleanupStack::PopAndDestroy ( &nameValueArray );
       
   213     }    
       
   214 
       
   215 // ----------------------------------------------------------------------------------
       
   216 // CActive methods
       
   217 // ----------------------------------------------------------------------------------
       
   218 void CAutoAudioAsyService::DoCancel ()
       
   219     {
       
   220     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::DoCancel" );
       
   221     iAccessoryControl.CancelConnectAccessory ();
       
   222     FreeResources ();
       
   223     }
       
   224 
       
   225 TInt CAutoAudioAsyService::RunError ( TInt /*aError*/ )
       
   226     {
       
   227     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::RunError" );
       
   228     return KErrNone;
       
   229     }
       
   230 
       
   231 void CAutoAudioAsyService::RunL ()
       
   232     {
       
   233     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::RunL" );
       
   234 	TInt retVal ( iStatus.Int () );
       
   235 	COM_TRACE_1 ( "[AccFW:AutoAudioAsy] Connected with return value %d", retVal );
       
   236 	
       
   237     if ( KErrNone == retVal )
       
   238 		{
       
   239 			switch ( iState )
       
   240 			{
       
   241 			case EServiceStateConnecting:				
       
   242 				SET_STATE ( EServiceStateConnected );
       
   243 				break;
       
   244 			
       
   245 			case EServiceStateIdle:
       
   246 			case EServiceStateConnected:
       
   247 			default:
       
   248 				COM_TRACE_ ( "State machine broken" );
       
   249 				break;
       
   250 			}
       
   251 		}
       
   252 	else
       
   253 		{
       
   254 		COM_TRACE_ ( "[AccFW:AutoAudioAsy] Accessory Server failed to accept connection, freeing up resources" );		
       
   255 		FreeResources ();
       
   256 		}
       
   257 	}	
       
   258 	
       
   259 // ----------------------------------------------------------------------------------
       
   260 // Frees resources and set state to EServiceStateIdle
       
   261 // ----------------------------------------------------------------------------------
       
   262 void CAutoAudioAsyService::FreeResources ()
       
   263     {
       
   264     COM_TRACE_ ( "[AccFW:AutoAudioAsy] CAutoAudioAsyService::FreeResources" );
       
   265 
       
   266     if ( iState != EServiceStateIdle )
       
   267         {
       
   268         SET_STATE ( EServiceStateIdle );
       
   269         }
       
   270 
       
   271     delete iGenericId;
       
   272     iGenericId = NULL;
       
   273     }