bluetoothappprofiles/avrcp/playerinformation/src/playerinformation.cpp
changeset 70 f5508c13dfe0
parent 67 16e4b9007960
child 71 083fd884d7dd
equal deleted inserted replaced
67:16e4b9007960 70:f5508c13dfe0
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @publishedAll
       
    19  @released
       
    20 */
       
    21 
       
    22 #include <bluetooth/logger.h>
       
    23 #include <playerinformationtarget.h>
       
    24 #include <e32property.h> 
       
    25 #include <hwrmpowerstatesdkpskeys.h>
       
    26 
       
    27 #include "playerapplicationsetting.h"
       
    28 #include "eventsmask.h"
       
    29 #include "playerinformation.h"
       
    30 
       
    31 #ifdef __FLOG_ACTIVE
       
    32 _LIT8(KLogComponent, LOG_COMPONENT_AVRCP_PLAYER_INFO);
       
    33 #endif
       
    34 
       
    35 EXPORT_C CPlayerInfoTarget* CPlayerInfoTarget::NewL(CRemConInterfaceSelector& aInterfaceSelector,
       
    36 										MPlayerCapabilitiesObserver*& aPlayerCapabilitiesObserver,
       
    37 										MPlayerApplicationSettingsObserver*& aPlayerApplicationSettingsObserver,
       
    38 										MPlayerEventsObserver*& aPlayerEventsObserver,
       
    39 										MPlayerApplicationSettingsNotify& aApplicationSettingNotifier )
       
    40 	{
       
    41 	LOG_STATIC_FUNC
       
    42 
       
    43 	CPlayerInfoTarget* self = new(ELeave) CPlayerInfoTarget(aInterfaceSelector, aApplicationSettingNotifier);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL( aPlayerCapabilitiesObserver, aPlayerApplicationSettingsObserver, aPlayerEventsObserver );
       
    46 	CleanupStack::Pop(self);
       
    47 	return self;
       
    48 	}
       
    49 	
       
    50 /** 
       
    51 Constructs this interface.
       
    52 @param aInterfaceSelector An interface selector for use with this interface.
       
    53 @internalComponent
       
    54 @released
       
    55 */
       
    56 CPlayerInfoTarget::CPlayerInfoTarget(CRemConInterfaceSelector& aInterfaceSelector, MPlayerApplicationSettingsNotify& aApplicationSettingNotifier)
       
    57 :	CRemConInterfaceBase(TUid::Uid(KRemConPlayerInformationUid), 
       
    58 						 KMaxLengthPlayerInformationMsg,
       
    59 						 aInterfaceSelector,
       
    60 						 ERemConClientTypeTarget),
       
    61 	iApplicationSettingNotifier(aApplicationSettingNotifier),
       
    62 	iPlaybackStatusUpdatePending(EFalse)
       
    63 	{
       
    64 	}
       
    65 	
       
    66 		
       
    67 void CPlayerInfoTarget::ConstructL(MPlayerCapabilitiesObserver*& aPlayerCapabilitiesObserver,
       
    68 										MPlayerApplicationSettingsObserver*& aPlayerApplicationSettingsObserver,
       
    69 										MPlayerEventsObserver*& aPlayerEventsObserver )
       
    70 	{
       
    71 	// reserve two AV/C frames, for this reasoning:
       
    72 	// GetCapabilities (PDU 0x10) can generate a response
       
    73 	// which is (255 * 3 bytes) + 2 bytes = 767 total and
       
    74 	// will fit into two AVC frames. Other PDUs generate
       
    75 	// less than this, except for PDUs 0x15 and 0x16 which
       
    76 	// use their own buffer pre-allocated by calling Size()
       
    77 	iOutBuf.CreateL(KAVCFrameMaxLength * 2);
       
    78 	
       
    79 	BaseConstructL();
       
    80 	
       
    81 	aPlayerCapabilitiesObserver = this;
       
    82 	aPlayerApplicationSettingsObserver = this;
       
    83 	aPlayerEventsObserver = this;
       
    84 	
       
    85 	// default values for event notifiers
       
    86 	iPlayBackStatus = EStopped;
       
    87 	
       
    88 	// If no track currently selected
       
    89 	iTrackId = KNoTrackSelected;
       
    90 	
       
    91 	iLengthInMilliseconds = 0;
       
    92 	iTrackPosition = EStart;
       
    93 	
       
    94 	// If no track currently selected, then return KPlaybackPositionUnknown
       
    95 	iPlaybackPositionInMilliseconds = KPlaybackPositionUnknown;
       
    96 	iLastNotifiedPlaybackPositionInMilliseconds	= KPlaybackPositionUnknown;
       
    97 
       
    98 	// The time interval used for playback position notification
       
    99 	iPlayBackIntervalInMilliseconds = 0;
       
   100 
       
   101 	// Try to detect battery status from hardware P&S properties	
       
   102 	iBatteryStatus = DetectBatteryStatus();
       
   103 
       
   104 	// The Bluetooth SIG vendor id must always be present
       
   105 	// See AVRCP 1.3 Specification, section 5.5.1, table 5.4
       
   106 	iCompanyIdList.AppendL(KBluetoothSIGVendorId);
       
   107  
       
   108 	// AVRCP 1.3 specification, Appendix H says that PlaybackStatusChanged
       
   109 	// and TrackChanged event notification support is mandatory, so add these
       
   110 	iSupportedNotificationEventList = new (ELeave) TEventsMask();
       
   111 	iSupportedNotificationEventList->Append(ERegisterNotificationPlaybackStatusChanged);
       
   112 	iSupportedNotificationEventList->Append(ERegisterNotificationTrackChanged);
       
   113 	}
       
   114 
       
   115 EXPORT_C CPlayerInfoTarget::~CPlayerInfoTarget()
       
   116 	{
       
   117 	iPendingNotificationEventList.Close();
       
   118 	iCompanyIdList.Close();
       
   119 	iOutBuf.Close();
       
   120 
       
   121 	// Free up all the settings memory
       
   122 	THashMapIter<TInt, CPlayerApplicationSettings*> iter(iPlayerApplicationSettings);
       
   123 	CPlayerApplicationSettings* const* setting = iter.NextValue();
       
   124 	while ( setting != NULL )
       
   125 		{
       
   126 		delete *setting;
       
   127 		setting = iter.NextValue();
       
   128 		}
       
   129 
       
   130 	// Now close the hash table itself
       
   131 	iPlayerApplicationSettings.Close();
       
   132 	delete iSupportedNotificationEventList;
       
   133 	}
       
   134 	
       
   135 		
       
   136 TAny* CPlayerInfoTarget::GetInterfaceIf(TUid aUid)
       
   137 	{
       
   138 	TAny* ret = NULL;
       
   139 	if ( aUid == TUid::Uid(KRemConInterfaceIf2) )
       
   140 		{
       
   141 		ret = reinterpret_cast<TAny*>(
       
   142 			static_cast<MRemConInterfaceIf2*>(this)
       
   143 			);
       
   144 		}
       
   145 
       
   146 	return ret;	
       
   147 	}
       
   148 
       
   149 // from MRemConInterfaceIf
       
   150 void CPlayerInfoTarget::MrcibNewMessage(TUint aOperationId, const TDesC8& aData, TRemConMessageSubType aMsgSubType)
       
   151 	{
       
   152 	LOG(_L("CPlayerInfoTarget::MrcibNewMessage"));
       
   153 	LOG1(_L("\taOperationId = 0x%02x"), aOperationId);
       
   154 	LOG1(_L("\taData.Length = %d"), aData.Length());
       
   155 	LOG1(_L("\taMsgSubType = 0x%02x"), aMsgSubType);
       
   156 
       
   157 	TMetadataTransferPDU currentOp = RAvrcpIPC::GetPDUIdFromIPCOperationId(aOperationId);
       
   158 	
       
   159 	switch (currentOp)
       
   160 		{
       
   161 		case EGetCapabilities:
       
   162 			{
       
   163 			/* process this in playercapabilities */
       
   164 			ProcessGetCapabilities( aData );
       
   165 			break;
       
   166 			}
       
   167 		case EListPlayerApplicationSettingAttributes:	
       
   168 			{
       
   169 			/* respond with Player Application Attributes*/
       
   170 			ProcessListPlayerApplicationAttributes( aOperationId );
       
   171 			break;
       
   172 			}
       
   173 		case EGetPlayerApplicationSettingAttributeText:	
       
   174 			{
       
   175 			/* respond with Player Application Attribute Text*/
       
   176 			ProcessGetPlayerApplicationAttributeText( aData, aOperationId );
       
   177 			break;
       
   178 			}
       
   179 		case EListPlayerApplicationSettingValues:
       
   180 			{
       
   181 			/* respond with Player Application Values*/
       
   182 			ProcessListPlayerApplicationValues( aData, aOperationId );
       
   183 			break;
       
   184 			}
       
   185 		case EGetPlayerApplicationSettingValueText:
       
   186 			{
       
   187 			/* respond with Application Value Text*/
       
   188 			ProcessGetPlayerApplicationValueText( aData, aOperationId );
       
   189 			break;
       
   190 			}
       
   191 		case EGetCurrentPlayerApplicationSettingValue:
       
   192 			{
       
   193 			/* respond with Current Player Application Value*/
       
   194 			ProcessGetCurrentPlayerApplicationValue( aData, aOperationId );
       
   195 			break;
       
   196 			}
       
   197 		case ESetPlayerApplicationSettingValue:
       
   198 			{
       
   199 			/* Set Player Application Value*/
       
   200 			ProcessSetPlayerApplicationValue( aData, aOperationId );
       
   201 			break;
       
   202 			}
       
   203 		case EGetPlayStatus:
       
   204 			{
       
   205 			// process in playerevents
       
   206 			ProcessGetPlayStatus();
       
   207 			break;
       
   208 			}
       
   209 		case ERegisterNotification:
       
   210 			{
       
   211 			// obtain eventId from aOperationId
       
   212 			TRegisterNotificationEvent eventId = RAvrcpIPC::GetEventIdFromIPCOperationId(aOperationId);
       
   213 			
       
   214 			// register for Notifications
       
   215 			if (aMsgSubType == ERemConNotifyCommandAwaitingInterim)
       
   216 				{
       
   217 				ProcessGetStatusAndBeginObserving(aOperationId, eventId, aData);
       
   218 				}
       
   219 			else if (aMsgSubType == ERemConNotifyCommandAwaitingChanged)
       
   220 				{
       
   221 				ProcessGetStatus(aOperationId, eventId);
       
   222 				}
       
   223 		 	break;
       
   224 		 	}
       
   225 		case EGetPlayStatusUpdate:
       
   226 			{
       
   227 			// process in playerevents
       
   228 			ProcessGetPlayStatusUpdate(aData);
       
   229 			break;
       
   230 			}
       
   231 		default:
       
   232 			break;
       
   233 		}
       
   234 		
       
   235 	}
       
   236 
       
   237 MPlayerEventsObserver::TTargetBatteryStatus CPlayerInfoTarget::DetectBatteryStatus()
       
   238 	{
       
   239 	MPlayerEventsObserver::TTargetBatteryStatus status = ECritical;
       
   240 	TInt err = KErrNone;
       
   241 	TInt batteryStatus = EBatteryStatusUnknown;
       
   242 	TInt chargingStatus = EChargingStatusNotConnected;
       
   243 	err = RProperty::Get(KPSUidHWRMPowerState,KHWRMBatteryStatus,batteryStatus);
       
   244 	if (!err)
       
   245 		{
       
   246 		err = RProperty::Get(KPSUidHWRMPowerState,KHWRMChargingStatus,chargingStatus);
       
   247 		}
       
   248 
       
   249 	switch (batteryStatus)
       
   250 		{
       
   251 		// Normal or Charging
       
   252 		case EBatteryStatusOk:
       
   253 			{
       
   254 			if (chargingStatus == EChargingStatusCharging ||
       
   255 				chargingStatus == EChargingStatusAlmostComplete ||
       
   256 				chargingStatus == EChargingStatusChargingContinued)
       
   257 				{
       
   258 				iBatteryStatus = EExternal;
       
   259 				}
       
   260 			else
       
   261 				{
       
   262 				if (chargingStatus == EChargingStatusChargingComplete)
       
   263 					{
       
   264 					iBatteryStatus = EFullCharge;
       
   265 					}
       
   266 				else
       
   267 					{
       
   268 					iBatteryStatus = ENormal;
       
   269 					}
       
   270 				}
       
   271 			}
       
   272 			break;
       
   273 		// Warning
       
   274 		case EBatteryStatusLow:
       
   275 			{
       
   276 			iBatteryStatus = EWarning;
       
   277 			}
       
   278 			break;
       
   279 		// Critical
       
   280 		case EBatteryStatusEmpty:
       
   281 			{
       
   282 			iBatteryStatus = ECritical;
       
   283 			}
       
   284 			break;
       
   285 		// Unknown state, set as ECritical
       
   286 		case EBatteryStatusUnknown:
       
   287 			{
       
   288 			iBatteryStatus = ECritical;
       
   289 			}
       
   290 			break;
       
   291 		}
       
   292 	return status;
       
   293 	}