bluetoothmgmt/bluetoothclientlib/avctpservices/avctpbody.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2005-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  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <bluetooth/logger.h>
       
    22 #include <es_sock.h>
       
    23 #include <bluetoothav.h>
       
    24 
       
    25 #include "avctpbody.h"
       
    26 #include "avctpreceiver.h"
       
    27 #include "avctpsender.h"
       
    28 #include "avctpremotedevices.h"
       
    29 #include "avctpserviceutils.h"
       
    30 #include "avctpcommon.h"
       
    31 
       
    32 #ifdef __FLOG_ACTIVE
       
    33 _LIT8(KLogComponent, LOG_COMPONENT_AVCTP_SERVICES);
       
    34 #endif
       
    35 
       
    36 using namespace SymbianAvctp;
       
    37 
       
    38 /***********************************************************************************/
       
    39 // Construction / Destruction Methods
       
    40 
       
    41 /**
       
    42 Two phase constructor of CAvctpBody. 
       
    43 @param aNotify the client to notify of AVCTP events
       
    44 @param aPid the profile id that the client wishes to use
       
    45 @internalComponent
       
    46 */
       
    47 CAvctpBody* CAvctpBody::NewL(MAvctpEventNotify& aNotify, TPid aPid, MAvctpChannel*& aPrimaryChannel)
       
    48 	{
       
    49 	LOG_STATIC_FUNC
       
    50 	CAvctpBody* self = CAvctpBody::NewLC(aNotify, aPid, aPrimaryChannel);
       
    51 	CleanupStack::Pop(self);
       
    52 	return self;	
       
    53 	}
       
    54 
       
    55 /**
       
    56 Two phase constructor of CAvctpBody which leaves newly created object on cleanupstack
       
    57 @param aNotify the client to notify of AVCTP events
       
    58 @param aPid the profile id that the client wishes to use
       
    59 @internalComponent
       
    60 */
       
    61 CAvctpBody* CAvctpBody::NewLC(MAvctpEventNotify& aNotify, TPid aPid, MAvctpChannel*& aPrimaryChannel)
       
    62 	{
       
    63 	LOG_STATIC_FUNC
       
    64 	CAvctpBody* self = new(ELeave) CAvctpBody(aNotify, aPid);
       
    65 	CleanupStack::PushL(self);
       
    66 	self->ConstructL(aPrimaryChannel);
       
    67 	return self;
       
    68 	}
       
    69 
       
    70 
       
    71 void CAvctpBody::ConstructL(MAvctpChannel*& aPrimaryChannel)
       
    72 	{
       
    73 	CONNECT_LOGGER
       
    74 	LOG_FUNC // must be after tls is created
       
    75 	
       
    76 	aPrimaryChannel = NULL;
       
    77 	
       
    78 	// Create proxy objects
       
    79 	iPrimaryChannelProxy = new(ELeave) TPrimaryChannelProxy(*this);
       
    80 	iSecondaryChannelProxy = new(ELeave) TSecondaryChannelProxy(*this);
       
    81 
       
    82 	// Start establishing AVCTP services
       
    83 	User::LeaveIfError(iSocketServ.Connect());
       
    84 	User::LeaveIfError(iSocketServ.ShareAuto());
       
    85 
       
    86 	User::LeaveIfError(iAvctpPrimaryDataSocket.Open(iSocketServ, KBTAddrFamily, KSockDatagram, KAVCTP));
       
    87 
       
    88 	// bind that socket so that the protocol knows what channel it represents
       
    89 	TAvctpSockAddr addr;
       
    90 	addr.SetPid(iPid);
       
    91 	addr.SetChannel(KAvctpPrimaryChannel);
       
    92 	User::LeaveIfError(iAvctpPrimaryDataSocket.Bind(addr));
       
    93 
       
    94 	iPrimaryChannelSender = CAvctpSender::NewL(iNotify, iAvctpPrimaryDataSocket, iPid);
       
    95 	iPrimaryChannelReceiver = CAvctpReceiver::NewL(iNotify, iAvctpPrimaryDataSocket, iPid);
       
    96 
       
    97 	// Needs to be called after the iAvctpControlSocket.Open call
       
    98 	iRemoteDevices = CAvctpRemoteDevices::NewL(iNotify, iSocketServ, iPid);
       
    99 	
       
   100 	// Finally we are done so assign channel interface
       
   101 	aPrimaryChannel = static_cast<MAvctpChannel*>(iPrimaryChannelProxy);
       
   102 	}
       
   103 
       
   104 /**
       
   105 c'tor
       
   106 @internalComponent
       
   107 */
       
   108 CAvctpBody::CAvctpBody(MAvctpEventNotify& aNotify, TPid aPid):
       
   109  	iNotify(aNotify), 
       
   110 	iPid(aPid)
       
   111 	{
       
   112 	LOG_FUNC // okay to be before tls created cause it's not created by the time this function returns
       
   113 	LOG2(_L("aNotify: 0x%08x, aPid: %d"), &aNotify, aPid)
       
   114 	}
       
   115 
       
   116 /**
       
   117 d'tor
       
   118 @internalComponent
       
   119 */
       
   120 CAvctpBody::~CAvctpBody()
       
   121 	{
       
   122 	LOG_FUNC
       
   123 
       
   124 	delete iPrimaryChannelSender;
       
   125 	delete iPrimaryChannelReceiver;
       
   126 	delete iSecondaryChannelSender;
       
   127 	delete iSecondaryChannelReceiver;
       
   128 	delete iRemoteDevices;
       
   129 
       
   130 	iAvctpSecondaryDataSocket.Close();
       
   131 	iAvctpPrimaryDataSocket.Close();
       
   132 	iSocketServ.Close();
       
   133 	
       
   134 	delete iSecondaryChannelProxy;
       
   135 	delete iPrimaryChannelProxy;
       
   136 
       
   137 	CLOSE_LOGGER
       
   138 	}
       
   139 
       
   140 /***********************************************************************************/
       
   141 // Methods to support RAvctp
       
   142 	
       
   143 void CAvctpBody::Close(RAvctp::TCloseType aImmediacy)
       
   144 	{
       
   145 	LOG_FUNC
       
   146 	
       
   147 	TRequestStatus status;
       
   148 	
       
   149 	if(iAvctpSecondaryDataSocket.SubSessionHandle())
       
   150 		{
       
   151 		iAvctpSecondaryDataSocket.Shutdown(aImmediacy == RAvctp::ENormal ? RSocket::ENormal : RSocket::EImmediate, status);
       
   152 		User::WaitForRequest(status);
       
   153 		}
       
   154 
       
   155 	iAvctpPrimaryDataSocket.Shutdown(aImmediacy == RAvctp::ENormal ? RSocket::ENormal : RSocket::EImmediate, status);
       
   156 	User::WaitForRequest(status);
       
   157 	
       
   158 	delete this;
       
   159 	}
       
   160 		
       
   161 void CAvctpBody::CloseGracefully()
       
   162 	{
       
   163 	LOG_FUNC
       
   164 	
       
   165 	iNotify.MaenErrorNotify(TBTDevAddr(0), KErrNotSupported);
       
   166 	}
       
   167 	
       
   168 /**
       
   169 Function from the AVCTP spec to request a channel connection to a remote
       
   170 entity.
       
   171 @return KErrArgument if a NULL TBTDevAddr is provided as an argument or
       
   172 		 another system wide error code  
       
   173 @internalComponent
       
   174 */	
       
   175 TInt CAvctpBody::PrimaryChannelAttachRequest(const TBTDevAddr& aBTDevice)
       
   176  	{
       
   177 	LOG_FUNC
       
   178 
       
   179 #ifdef _DEBUG
       
   180 	TBuf<12> addr;
       
   181 	aBTDevice.GetReadable(addr);
       
   182 	LOG1(_L("to BT Device %S"), &addr);
       
   183 #endif
       
   184 	
       
   185 	return !IsNullAddress(aBTDevice) ? iRemoteDevices->PrimaryChannelAttachRequest(aBTDevice) : KErrArgument;
       
   186  	}
       
   187 
       
   188 TBool CAvctpBody::IsAttached(const TBTDevAddr& aBTDevice)
       
   189 	{
       
   190 	LOG_FUNC
       
   191 	return iRemoteDevices->IsAttached(aBTDevice);
       
   192 	}
       
   193 
       
   194 /**
       
   195 Function from the AVCTP spec to explicitly request disconnection of an existing
       
   196 channel.
       
   197 @param aBTDevice The remote device to disconnect
       
   198 @return KErrArgument if a NULL TBTDevAddr is provided as an argument or
       
   199 		 another system wide error code
       
   200 @internalComponent
       
   201 */
       
   202 TInt CAvctpBody::PrimaryChannelDetachRequest(const TBTDevAddr& aBTDevice)
       
   203 	{
       
   204 	LOG_FUNC
       
   205 #ifdef _DEBUG
       
   206 	TBuf<12> addr;
       
   207 	aBTDevice.GetReadable(addr);
       
   208 	LOG1(_L("to BT Device %S"), &addr);
       
   209 #endif
       
   210 
       
   211 	return !IsNullAddress(aBTDevice) ? iRemoteDevices->PrimaryChannelDetachRequest(aBTDevice) : KErrArgument;
       
   212 	}
       
   213 
       
   214 /**
       
   215 Sends AVCTP message
       
   216 The Pid to send the message to was given when the client opened the RAvctp object
       
   217 used to send this message.
       
   218 
       
   219 @pre must be connected to remote
       
   220 @param aBTDevice device address to which to send message
       
   221 @param aTransactionLabel transaction label
       
   222 @param aType the message type, i.e. Command or Response
       
   223 @param aMessageInformation the contents of the message to send
       
   224 @return KErrInUse if the sender AO is active or
       
   225 		 KErrInvalidTransactionLabel if the transaction label was invalid or
       
   226 		 KErrArgument if a NULL TBTDevAddr is provided as an argument or
       
   227 		 another system wide error code
       
   228 @internalComponent
       
   229 */
       
   230 
       
   231 TInt CAvctpBody::SendMessage(const TBTDevAddr& aBTDevice, 
       
   232 							 TTransactionLabel aTransactionLabel,
       
   233 							 TMessageType aType,
       
   234 							 const TDesC8& aMessageInformation,
       
   235 							 TBool aIsSecondChannel)
       
   236 	{
       
   237 	LOG_FUNC
       
   238 
       
   239 #ifdef _DEBUG
       
   240 	TBuf<12> addr;
       
   241 	aBTDevice.GetReadable(addr);
       
   242 	LOG1(_L("to BT Device %S"), &addr);
       
   243 #endif
       
   244 
       
   245 	LOG2(_L("aTransactionLabel: %d, aType: %d"), aTransactionLabel, aType)
       
   246 
       
   247 	TInt ret = KErrNone;
       
   248 	if (IsNullAddress(aBTDevice))
       
   249 		{
       
   250 		ret = KErrArgument;
       
   251 		}
       
   252 	else if(IsValidTransactionLabel(aTransactionLabel))
       
   253 		{
       
   254 		__ASSERT_ALWAYS(aType == ECommand || aType == EResponse, Panic(EAvctpInvalidMessageType));
       
   255 		
       
   256 		CAvctpSender& sender(aIsSecondChannel ? *iSecondaryChannelSender : *iPrimaryChannelSender);
       
   257 		ret = sender.SendMessage(aBTDevice, aTransactionLabel, aType, aMessageInformation);
       
   258 		}
       
   259 	else
       
   260 		{
       
   261 		ret = KErrInvalidTransactionLabel;
       
   262 		}
       
   263 	return ret;
       
   264 	}
       
   265 
       
   266 
       
   267 void CAvctpBody::PrimaryChannelCancelAttach(const TBTDevAddr& aBTDevice)
       
   268 	{
       
   269 	LOG_FUNC
       
   270 
       
   271 #ifdef _DEBUG
       
   272 	TBuf<12> addr;
       
   273 	aBTDevice.GetReadable(addr);
       
   274 	LOG1(_L("to BT Device %S"), &addr);
       
   275 #endif
       
   276 
       
   277 	__ASSERT_DEBUG(!IsNullAddress(aBTDevice), Panic(EAvctpNullBTDevAddr));
       
   278 	iRemoteDevices->PrimaryChannelCancelAttach(aBTDevice);
       
   279 	}
       
   280 
       
   281 void CAvctpBody::PrimaryChannelCancelSend()
       
   282 	{
       
   283 	LOG_FUNC
       
   284 
       
   285 	iPrimaryChannelSender->Cancel();
       
   286 	}
       
   287 
       
   288 void CAvctpBody::SecondaryChannelCancelSend()
       
   289 	{
       
   290 	LOG_FUNC
       
   291 
       
   292 	iSecondaryChannelSender->Cancel();
       
   293 	}
       
   294 
       
   295 TInt CAvctpBody::InstallSecondaryChannel(MAvctpEventNotify& aSecondChannelObserver, MAvctpChannel*& aSecondaryChannel)
       
   296 	{
       
   297 	LOG_FUNC
       
   298 	TRAPD(err, DoInstallSecondaryChannelL(aSecondChannelObserver, aSecondaryChannel));
       
   299 	return err;
       
   300 	}
       
   301 
       
   302 void CAvctpBody::DoInstallSecondaryChannelL(MAvctpEventNotify& aSecondChannelObserver, MAvctpChannel*& aSecondaryChannel)
       
   303 	{
       
   304 	LOG_FUNC
       
   305 	LEAVEIFERRORL(iAvctpSecondaryDataSocket.Open(iSocketServ, KBTAddrFamily, KSockDatagram, KAVCTP));
       
   306 
       
   307 	// bind that socket so that the protocol knows what channel it represents
       
   308 	TAvctpSockAddr addr;
       
   309 	addr.SetChannel(KAvctpSecondaryChannel);
       
   310 	addr.SetPid(iPid);
       
   311 	User::LeaveIfError(iAvctpSecondaryDataSocket.Bind(addr));
       
   312 
       
   313 	iSecondaryChannelNotify = &aSecondChannelObserver;
       
   314 
       
   315 	__ASSERT_DEBUG(!iSecondaryChannelSender, Panic(EAvctpSenderAlreadyAllocated));
       
   316 	iSecondaryChannelSender = CAvctpSender::NewL(*iSecondaryChannelNotify, iAvctpSecondaryDataSocket, iPid);
       
   317 	__ASSERT_DEBUG(!iSecondaryChannelReceiver, Panic(EAvctpReceiverAlreadyAllocated));
       
   318 	iSecondaryChannelReceiver = CAvctpReceiver::NewL(*iSecondaryChannelNotify, iAvctpSecondaryDataSocket, iPid);
       
   319 	iRemoteDevices->SetSecondaryChannelNotifyL(&aSecondChannelObserver);
       
   320 	
       
   321 	// finally set the interface
       
   322 	aSecondaryChannel = static_cast<MAvctpChannel*>(iSecondaryChannelProxy);
       
   323 	}
       
   324 
       
   325 void CAvctpBody::UninstallSecondaryChannel(RAvctp::TCloseType aImmediacy)
       
   326 	{
       
   327 	LOG_FUNC
       
   328 	
       
   329 	TRequestStatus status;
       
   330 	iAvctpSecondaryDataSocket.Shutdown(aImmediacy == RAvctp::ENormal ? RSocket::ENormal : RSocket::EImmediate, status);
       
   331 	User::WaitForRequest(status);
       
   332 	
       
   333 	delete iSecondaryChannelSender;
       
   334 	iSecondaryChannelSender = NULL;
       
   335 	delete iSecondaryChannelReceiver;
       
   336 	iSecondaryChannelReceiver = NULL;
       
   337 	iSecondaryChannelNotify = NULL;
       
   338 	iAvctpSecondaryDataSocket.Close();
       
   339 	TRAPD(err, iRemoteDevices->SetSecondaryChannelNotifyL(NULL));
       
   340 	__ASSERT_DEBUG(err == KErrNone, Panic(EAvctpUnexpectedLeave));
       
   341 	(void)(err == KErrNone);	// to avoid "variable not used" warning in release mode
       
   342 	}
       
   343 
       
   344 TInt CAvctpBody::SecondaryChannelAttachRequest(const TBTDevAddr& aBTDevice)
       
   345 	{
       
   346 	LOG_FUNC
       
   347 
       
   348 #ifdef _DEBUG
       
   349 	TBuf<12> addr;
       
   350 	aBTDevice.GetReadable(addr);
       
   351 	LOG1(_L("to BT Device %S"), &addr);
       
   352 #endif
       
   353 
       
   354 	return !IsNullAddress(aBTDevice) ? iRemoteDevices->SecondaryChannelAttachRequest(aBTDevice) : KErrArgument;
       
   355 	}
       
   356 
       
   357 TInt CAvctpBody::SecondaryChannelDetachRequest(const TBTDevAddr& aBTDevice)
       
   358 	{
       
   359 	LOG_FUNC
       
   360 
       
   361 #ifdef _DEBUG
       
   362 	TBuf<12> addr;
       
   363 	aBTDevice.GetReadable(addr);
       
   364 	LOG1(_L("to BT Device %S"), &addr);
       
   365 #endif
       
   366 
       
   367 	return !IsNullAddress(aBTDevice) ? iRemoteDevices->SecondaryChannelDetachRequest(aBTDevice) : KErrArgument;
       
   368 	}
       
   369 
       
   370 void CAvctpBody::SecondaryChannelCancelAttach(const TBTDevAddr& aBTDevice)
       
   371 	{
       
   372 	LOG_FUNC
       
   373 	
       
   374 #ifdef _DEBUG
       
   375 	TBuf<12> addr;
       
   376 	aBTDevice.GetReadable(addr);
       
   377 	LOG1(_L("to BT Device %S"), &addr);
       
   378 #endif
       
   379 
       
   380 	__ASSERT_DEBUG(!IsNullAddress(aBTDevice), Panic(EAvctpNullBTDevAddr));
       
   381 	iRemoteDevices->SecondaryChannelCancelAttach(aBTDevice);
       
   382 	}
       
   383 
       
   384 //
       
   385 // Channel Proxies
       
   386 //
       
   387 
       
   388 TPrimaryChannelProxy::TPrimaryChannelProxy(CAvctpBody& aBody)
       
   389 	: iBody(aBody)
       
   390 	{
       
   391 	}
       
   392 	
       
   393 TInt TPrimaryChannelProxy::MacAttachRequest(const TBTDevAddr& aBTDevice)
       
   394 	{
       
   395 	return iBody.PrimaryChannelAttachRequest(aBTDevice);
       
   396 	}
       
   397 
       
   398 TBool TPrimaryChannelProxy::MacIsAttached(const TBTDevAddr& aBTDevice)
       
   399 	{
       
   400 	return iBody.IsAttached(aBTDevice);
       
   401 	}
       
   402 
       
   403 void TPrimaryChannelProxy::MacCancelAttach(const TBTDevAddr& aBTDevice)
       
   404 	{
       
   405 	iBody.PrimaryChannelCancelAttach(aBTDevice);
       
   406 	}
       
   407 
       
   408 TInt TPrimaryChannelProxy::MacDetachRequest(const TBTDevAddr& aBTDevice)
       
   409 	{
       
   410 	return iBody.PrimaryChannelDetachRequest(aBTDevice);
       
   411 	}
       
   412 
       
   413 TInt TPrimaryChannelProxy::MacSendMessage(const TBTDevAddr& aBTDevice, 
       
   414 					SymbianAvctp::TTransactionLabel aTransactionLabel,
       
   415 					SymbianAvctp::TMessageType aType,
       
   416 					const TDesC8& aMessageInformation)
       
   417 	{
       
   418 	return iBody.SendMessage(aBTDevice, aTransactionLabel, aType, aMessageInformation, EFalse);
       
   419 	}
       
   420 
       
   421 void TPrimaryChannelProxy::MacCancelSend()
       
   422 	{
       
   423 	iBody.PrimaryChannelCancelSend();
       
   424 	}
       
   425 
       
   426 
       
   427 TSecondaryChannelProxy::TSecondaryChannelProxy(CAvctpBody& aBody)
       
   428 	: iBody(aBody)
       
   429 	{
       
   430 	}
       
   431 	
       
   432 TInt TSecondaryChannelProxy::MacAttachRequest(const TBTDevAddr& aBTDevice)
       
   433 	{
       
   434 	return iBody.SecondaryChannelAttachRequest(aBTDevice);
       
   435 	}
       
   436 
       
   437 TBool TSecondaryChannelProxy::MacIsAttached(const TBTDevAddr& aBTDevice)
       
   438 	{
       
   439 	return iBody.IsAttached(aBTDevice);
       
   440 	}
       
   441 
       
   442 void TSecondaryChannelProxy::MacCancelAttach(const TBTDevAddr& aBTDevice)
       
   443 	{
       
   444 	iBody.SecondaryChannelCancelAttach(aBTDevice);
       
   445 	}
       
   446 
       
   447 TInt TSecondaryChannelProxy::MacDetachRequest(const TBTDevAddr& aBTDevice)
       
   448 	{
       
   449 	return iBody.SecondaryChannelDetachRequest(aBTDevice);
       
   450 	}
       
   451 
       
   452 TInt TSecondaryChannelProxy::MacSendMessage(const TBTDevAddr& aBTDevice, 
       
   453 					SymbianAvctp::TTransactionLabel aTransactionLabel,
       
   454 					SymbianAvctp::TMessageType aType,
       
   455 					const TDesC8& aMessageInformation)
       
   456 	{
       
   457 	return iBody.SendMessage(aBTDevice, aTransactionLabel, aType, aMessageInformation, ETrue);
       
   458 	}
       
   459 
       
   460 void TSecondaryChannelProxy::MacCancelSend()
       
   461 	{
       
   462 	iBody.SecondaryChannelCancelSend();
       
   463 	}
       
   464 
       
   465