bluetoothmgmt/bluetoothclientlib/avctpservices/channelcontrollers.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 // basechcontroller.cpp
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalTechnology
       
    22 */
       
    23 #include <bluetooth/logger.h>
       
    24 #include "avctpPriorities.h"
       
    25 #include "avctpcommon.h"
       
    26 #include "channelcontrollers.h"
       
    27 
       
    28 #ifdef __FLOG_ACTIVE
       
    29 _LIT8(KLogComponent, LOG_COMPONENT_AVCTP_SERVICES);
       
    30 #endif
       
    31 
       
    32 using namespace SymbianAvctp;
       
    33 
       
    34 CBaseChController::CBaseChController(MControlChannelNotify& aNotify, RSocketServ& aSockServer, TControlIoctls aIoctl, SymbianAvctp::TPid aPid, TInt aChannel) :
       
    35 	CActive(KDefaultPriority),
       
    36 	iNotify(aNotify),
       
    37 	iSockServer(aSockServer),
       
    38 	iIoctl(aIoctl),
       
    39 	iPid(aPid),
       
    40 	iChannel(aChannel)
       
    41 	{
       
    42 	LOG_FUNC
       
    43 	}
       
    44 
       
    45 CBaseChController::~CBaseChController()
       
    46 	{
       
    47 	LOG_FUNC
       
    48 	Cancel();
       
    49 	iTransport.Close();
       
    50 	}
       
    51 
       
    52 void CBaseChController::BaseConstructL()
       
    53 	{
       
    54 	LOG_FUNC
       
    55 
       
    56 	CActiveScheduler::Add(this);
       
    57 	TInt err = iTransport.Open(iSockServer, KBTAddrFamily, KSockRaw, KAVCTP);
       
    58 	User::LeaveIfError(err);
       
    59 	}
       
    60 	
       
    61 void CBaseChController::DoCancel()
       
    62 	{
       
    63 	LOG_FUNC
       
    64 	iTransport.CancelAll();
       
    65 	}
       
    66 
       
    67 void CBaseChController::RunL()
       
    68 	{
       
    69 	LOG_FUNC
       
    70 	TInt err = iStatus.Int();
       
    71 	if (err == KErrNone && iIoctlBuf().iError != KErrNone)
       
    72 		{
       
    73 		err = iIoctlBuf().iError;
       
    74 		}
       
    75 	
       
    76 	NotifyIoctlCompleted(err);
       
    77 	}
       
    78 
       
    79 TInt CBaseChController::RunError(TInt aError)
       
    80 	{
       
    81 	LOG_FUNC
       
    82 	NotifyIoctlError(aError);
       
    83 	return KErrNone;	
       
    84 	}
       
    85 
       
    86 void CBaseChController::Ioctl()
       
    87 	{
       
    88 	LOG_FUNC
       
    89 	iTransport.CancelIoctl();
       
    90 	iIoctlBuf().iIoctl = iIoctl;
       
    91 	iIoctlBuf().iAddr.SetChannel(iChannel);
       
    92 	iTransport.Ioctl(iIoctl, iStatus, &iIoctlBuf, KSolBtAVCTP);
       
    93 	SetActive();
       
    94 	}
       
    95 
       
    96 void CBaseChController::AttachRequest(const TBTDevAddr& aBTDevice)
       
    97 	{
       
    98 	LOG_FUNC
       
    99 	
       
   100 	Cancel();	// cancel the outstanding active object
       
   101 
       
   102 	TAvctpSockAddr addr;
       
   103 	addr.SetBTAddr(aBTDevice);
       
   104 	addr.SetPid(iPid);
       
   105 	addr.SetChannel(iIoctl ==  EPrimaryChannelAttachToTransport || iIoctl == EAwaitProvidedTransport ? 0 : 1);
       
   106 	
       
   107 	iIoctlBuf().iAddr = addr;
       
   108 	Ioctl();
       
   109 	}
       
   110 
       
   111 void CBaseChController::Listen()
       
   112 	{
       
   113 	LOG_FUNC
       
   114 	Listen(TBTDevAddr(0));
       
   115 	}
       
   116 
       
   117 void CBaseChController::Listen(const TBTDevAddr& aBTDevice)
       
   118 	{
       
   119 	LOG_FUNC
       
   120 	Cancel();
       
   121 	
       
   122 	TAvctpSockAddr addr;
       
   123 	addr.SetBTAddr(aBTDevice);
       
   124 	addr.SetPid(iPid);
       
   125 	
       
   126 	iIoctlBuf().iAddr = addr;
       
   127 	Ioctl();
       
   128 	}
       
   129 
       
   130 void CBaseChController::DetachRequest(const TBTDevAddr& aBTDevice)
       
   131 	{
       
   132 	LOG_FUNC
       
   133 	Cancel();	// cancel all outstanding ioctl
       
   134 	
       
   135 	TAvctpSockAddr addr;
       
   136 	addr.SetBTAddr(aBTDevice);
       
   137 	addr.SetPid(iPid);
       
   138 	
       
   139 	iIoctlBuf().iAddr = addr;
       
   140 	Ioctl();
       
   141 	}
       
   142 
       
   143 void CBaseChController::RefuseAttach(const TBTDevAddr& aBTDevice)
       
   144 	{
       
   145 	LOG_FUNC
       
   146 	Cancel();	// cancel all outstanding ioctl
       
   147 	
       
   148 	TAvctpSockAddr addr;
       
   149 	addr.SetBTAddr(aBTDevice);
       
   150 	addr.SetPid(iPid);
       
   151 	
       
   152 	iIoctlBuf().iAddr = addr;
       
   153 	Ioctl();
       
   154 	}
       
   155 
       
   156 void CBaseChController::AgreeAttachment(const TBTDevAddr& aBTDevice)
       
   157 	{
       
   158 	LOG_FUNC
       
   159 	Cancel();	// cancel all outstanding ioctl
       
   160 	
       
   161 	TAvctpSockAddr addr;
       
   162 	addr.SetBTAddr(aBTDevice);
       
   163 	addr.SetPid(iPid);
       
   164 	
       
   165 	iIoctlBuf().iAddr = addr;
       
   166 	Ioctl();
       
   167 	}
       
   168 
       
   169 
       
   170 // CPrimaryChannelController
       
   171 CPrimaryChannelController* CPrimaryChannelController::NewL(MControlChannelNotify& aNotify, RSocketServ& aSockServer, SymbianAvctp::TPid aPid)
       
   172 	{
       
   173 	LOG_STATIC_FUNC
       
   174 
       
   175 	CPrimaryChannelController* self = CPrimaryChannelController::NewLC(aNotify, aSockServer, aPid);
       
   176 	CleanupStack::Pop(self);
       
   177 	return self;		
       
   178 	}
       
   179 
       
   180 CPrimaryChannelController* CPrimaryChannelController::NewLC(MControlChannelNotify& aNotify, RSocketServ& aSockServer, SymbianAvctp::TPid aPid)
       
   181 	{
       
   182 	LOG_STATIC_FUNC
       
   183 
       
   184 	CPrimaryChannelController* self = new(ELeave) CPrimaryChannelController(aNotify, aSockServer, aPid);
       
   185 	CleanupStack::PushL(self);
       
   186 	self->BaseConstructL();
       
   187 	return self;	
       
   188 	}
       
   189 
       
   190 CPrimaryChannelController::CPrimaryChannelController(MControlChannelNotify& aNotify, RSocketServ& aSockServer, SymbianAvctp::TPid aPid) :
       
   191 	CBaseChController(aNotify, aSockServer, EAwaitProvidedTransport, aPid, KAvctpPrimaryChannel)
       
   192 	{
       
   193 	LOG_FUNC
       
   194 	}
       
   195 
       
   196 void CPrimaryChannelController::AttachRequest(const TBTDevAddr& aBTDevice)
       
   197 	{
       
   198 	LOG_FUNC
       
   199 	iIoctl = EPrimaryChannelAttachToTransport;
       
   200 	CBaseChController::AttachRequest(aBTDevice);
       
   201 	}
       
   202 
       
   203 void CPrimaryChannelController::DetachRequest(const TBTDevAddr& aBTDevice)
       
   204 	{
       
   205 	LOG_FUNC
       
   206 	iIoctl = EPrimaryChannelDetachFromTransport;
       
   207 	CBaseChController::DetachRequest(aBTDevice);
       
   208 	}
       
   209 
       
   210 void CPrimaryChannelController::RefuseAttach(const TBTDevAddr& aBTDevice)
       
   211 	{
       
   212 	LOG_FUNC
       
   213 	iIoctl = EPrimaryChannelRefuseAttach;
       
   214 	CBaseChController::RefuseAttach(aBTDevice);
       
   215 	}
       
   216 
       
   217 CPrimaryChannelController::~CPrimaryChannelController()
       
   218 	{
       
   219 	LOG_FUNC
       
   220 	}
       
   221 
       
   222 void CPrimaryChannelController::NotifyIoctlCompleted(TInt aError)
       
   223 	{
       
   224 	LOG_FUNC
       
   225 	
       
   226 	// get some parameters
       
   227 	TBTDevAddr remdev = iIoctlBuf().iAddr.BTAddr();
       
   228 	
       
   229 	switch(iIoctlBuf().iIoctl)
       
   230 		{
       
   231 		case EAttachConfirm:
       
   232 			{
       
   233 			TInt mtu = KDefaultMtu;
       
   234 			if (aError == KErrNone)
       
   235 				{
       
   236 				TPckgBuf<TOptionMessage> optBuf;
       
   237 				optBuf().iAddr = remdev;
       
   238 				TInt mtuErr = iTransport.GetOpt(KAvctpBaseOutboundMTU, KSolBtAVCTP, optBuf);
       
   239 				mtu = mtuErr ? KDefaultMtu : optBuf().iMtu;
       
   240 				}
       
   241 			iNotify.PrimaryChannelAttachConfirm(remdev, mtu, aError);
       
   242 			break;
       
   243 			}
       
   244 		case EAttachIndicate:
       
   245 			{
       
   246 			__ASSERT_DEBUG(aError == KErrNone, Panic(EAvctpUnexpectedErrorCode));
       
   247 			TInt mtu = KDefaultMtu;
       
   248 			TPckgBuf<TOptionMessage> optBuf;
       
   249 			optBuf().iAddr = remdev;
       
   250 			TInt mtuErr = iTransport.GetOpt(KAvctpBaseOutboundMTU, KSolBtAVCTP, optBuf);
       
   251 			mtu = mtuErr ? KDefaultMtu : optBuf().iMtu;
       
   252 			iNotify.PrimaryChannelAttachIndicate(remdev, mtu);
       
   253 			break;
       
   254 			}
       
   255 		case EDetachConfirm:
       
   256 			{
       
   257 			iNotify.PrimaryChannelDetachConfirm(remdev, aError);
       
   258 			break;
       
   259 			}
       
   260 		case EDetachIndicate:
       
   261 			{
       
   262 			iNotify.PrimaryChannelDetachIndicate(remdev);
       
   263 			break;
       
   264 			}
       
   265 		case EError:
       
   266 			iNotify.PrimaryChannelIoctlError(remdev, aError);
       
   267 			break;
       
   268 		case EPrimaryChannelAgreeAttachment:
       
   269 			iNotify.PrimaryChannelAgreementError(remdev, aError);
       
   270 			break;
       
   271 		}
       
   272 	}
       
   273 
       
   274 void CPrimaryChannelController::NotifyIoctlError(TInt aError)
       
   275 	{
       
   276 	LOG_FUNC
       
   277 	iNotify.PrimaryChannelIoctlError(iIoctlBuf().iAddr, aError);
       
   278 	}
       
   279 
       
   280 void CPrimaryChannelController::Listen()
       
   281 	{
       
   282 	LOG_FUNC
       
   283 	iIoctl =EAwaitProvidedTransport;	// so when the ioctl is submitted again it is the it is the right one
       
   284 	CBaseChController::Listen();
       
   285 	}
       
   286 
       
   287 void CPrimaryChannelController::AgreeAttachment(const TBTDevAddr& aBTDevice)
       
   288 	{
       
   289 	LOG_FUNC
       
   290 	iIoctl = EPrimaryChannelAgreeAttachment;
       
   291 	CBaseChController::AgreeAttachment(aBTDevice);
       
   292 	}
       
   293 
       
   294 // CSecondaryChannelController
       
   295 CSecondaryChannelController* CSecondaryChannelController::NewL(MControlChannelNotify& aNotify, RSocketServ& aSockServer, SymbianAvctp::TPid aPid)
       
   296 	{
       
   297 	LOG_STATIC_FUNC
       
   298 
       
   299 	CSecondaryChannelController* self = CSecondaryChannelController::NewLC(aNotify, aSockServer, aPid);
       
   300 	CleanupStack::Pop(self);
       
   301 	return self;		
       
   302 	}
       
   303 
       
   304 CSecondaryChannelController* CSecondaryChannelController::NewLC(MControlChannelNotify& aNotify, RSocketServ& aSockServer, SymbianAvctp::TPid aPid)
       
   305 	{
       
   306 	LOG_STATIC_FUNC
       
   307 
       
   308 	CSecondaryChannelController* self = new(ELeave) CSecondaryChannelController(aNotify, aSockServer, aPid);
       
   309 	CleanupStack::PushL(self);
       
   310 	self->BaseConstructL();
       
   311 	return self;	
       
   312 	}
       
   313 
       
   314 CSecondaryChannelController::CSecondaryChannelController(MControlChannelNotify& aNotify, RSocketServ& aSockServer, SymbianAvctp::TPid aPid) :
       
   315 	CBaseChController(aNotify, aSockServer, EAwaitExtendedTransport, aPid, KAvctpSecondaryChannel)
       
   316 	{
       
   317 	LOG_FUNC
       
   318 	}
       
   319 
       
   320 void CSecondaryChannelController::AttachRequest(const TBTDevAddr& aBTDevice)
       
   321 	{
       
   322 	LOG_FUNC
       
   323 	iIoctl = ESecondaryChannelAttachToTransport;
       
   324 	CBaseChController::AttachRequest(aBTDevice);
       
   325 	}
       
   326 
       
   327 void CSecondaryChannelController::AttachPassively(const TBTDevAddr& aBTDevice)
       
   328 	{
       
   329 	LOG_FUNC
       
   330 	iIoctl = EAwaitExtendedTransport;
       
   331 	CBaseChController::Listen(aBTDevice);
       
   332 	}
       
   333 
       
   334 void CSecondaryChannelController::DetachRequest(const TBTDevAddr& aBTDevice)
       
   335 	{
       
   336 	LOG_FUNC
       
   337 	iIoctl = ESecondaryChannelDetachFromTransport;
       
   338 	CBaseChController::DetachRequest(aBTDevice);
       
   339 	}
       
   340 
       
   341 void CSecondaryChannelController::RefuseAttach(const TBTDevAddr& aBTDevice)
       
   342 	{
       
   343 	LOG_FUNC
       
   344 	iIoctl = ESecondaryChannelRefuseAttach;
       
   345 	CBaseChController::RefuseAttach(aBTDevice);
       
   346 	}
       
   347 
       
   348 CSecondaryChannelController::~CSecondaryChannelController()
       
   349 	{
       
   350 	LOG_FUNC
       
   351 	}
       
   352 
       
   353 void CSecondaryChannelController::NotifyIoctlCompleted(TInt aError)
       
   354 	{
       
   355 	LOG_FUNC
       
   356 	// get some parameters
       
   357 	
       
   358 	TBTDevAddr remdev = iIoctlBuf().iAddr.BTAddr();
       
   359 	
       
   360 	switch(iIoctlBuf().iIoctl)
       
   361 		{
       
   362 		case EAttachConfirm:
       
   363 			{
       
   364 			TInt mtu = KDefaultMtu;
       
   365 			if (aError == KErrNone)
       
   366 				{
       
   367 				TPckgBuf<TOptionMessage> optBuf;
       
   368 				optBuf().iAddr = remdev;
       
   369 				TInt mtuErr = iTransport.GetOpt(KAvctpExtendOutboundMTU, KSolBtAVCTP, optBuf);
       
   370 				mtu = mtuErr ? KDefaultMtu : optBuf().iMtu;
       
   371 				}
       
   372 			iNotify.SecondaryChannelAttachConfirm(remdev, mtu, aError);
       
   373 			break;
       
   374 			}
       
   375 		case EAttachIndicate:
       
   376 			{
       
   377 			__ASSERT_DEBUG(aError == KErrNone, Panic(EAvctpUnexpectedErrorCode));
       
   378 			TInt mtu = KDefaultMtu;
       
   379 			TPckgBuf<TOptionMessage> optBuf;
       
   380 			optBuf().iAddr = remdev;
       
   381 			TInt mtuErr = iTransport.GetOpt(KAvctpExtendOutboundMTU, KSolBtAVCTP, optBuf);
       
   382 			mtu = mtuErr ? KDefaultMtu : optBuf().iMtu;
       
   383 			iNotify.SecondaryChannelAttachIndicate(remdev, mtu);
       
   384 			break;
       
   385 			}
       
   386 		case EDetachConfirm:
       
   387 			{
       
   388 			iNotify.SecondaryChannelDetachConfirm(remdev, aError);
       
   389 			break;
       
   390 			}
       
   391 		case EDetachIndicate:
       
   392 			{
       
   393 			iNotify.SecondaryChannelDetachIndicate(remdev);
       
   394 			break;
       
   395 			}
       
   396 		case EError:
       
   397 			iNotify.SecondaryChannelIoctlError(remdev, aError);
       
   398 			break;
       
   399 		case ESecondaryChannelAgreeAttachment:
       
   400 			iNotify.SecondaryChannelAgreementError(remdev, aError);
       
   401 			break;
       
   402 		default:
       
   403 			break;
       
   404 		}
       
   405 	}
       
   406 
       
   407 void CSecondaryChannelController::NotifyIoctlError(TInt aError)
       
   408 	{
       
   409 	LOG_FUNC
       
   410 	iNotify.SecondaryChannelIoctlError(iIoctlBuf().iAddr, aError);
       
   411 	}
       
   412 
       
   413 void CSecondaryChannelController::Listen()
       
   414 	{
       
   415 	LOG_FUNC
       
   416 	iIoctl =EAwaitExtendedTransport;	// so when the ioctl is submitted again it is the it is the right one
       
   417 	CBaseChController::Listen();
       
   418 	}
       
   419 
       
   420 void CSecondaryChannelController::AgreeAttachment(const TBTDevAddr& aBTDevice)
       
   421 	{
       
   422 	LOG_FUNC
       
   423 	iIoctl = ESecondaryChannelAgreeAttachment;
       
   424 	CBaseChController::AgreeAttachment(aBTDevice);
       
   425 	}