bluetooth/btstack/linkmgr/bredrcontrollerconfiguration.cpp
branchRCL_3
changeset 11 20fda83a6398
equal deleted inserted replaced
10:8a27654f7b62 11:20fda83a6398
       
     1 // Copyright (c) 2010 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 #include <bluetooth/logger.h>
       
    17 #include "debug.h"
       
    18 #include "linkutil.h"
       
    19 #include "bredrcontrollerconfiguration.h"
       
    20 #include <bt_subscribe.h>
       
    21 #include <bt_subscribe_partner.h>
       
    22 #include <bluetooth/hcicommandqueue.h>
       
    23 #include <bluetooth/hci/readpagescanactivitycommand.h>
       
    24 #include <bluetooth/hci/readpagescanactivitycompleteevent.h>
       
    25 #include <bluetooth/hci/readpagescantypecommand.h>
       
    26 #include <bluetooth/hci/readpagescantypecompleteevent.h>
       
    27 #include <bluetooth/hci/writepagescanactivitycommand.h>
       
    28 #include <bluetooth/hci/writepagescantypecommand.h>
       
    29 
       
    30 #ifdef __FLOG_ACTIVE
       
    31 _LIT8(KLogComponent, LOG_COMPONENT_LINKMGR);
       
    32 #endif
       
    33 
       
    34 const TInt KUnitializedParameters	= -1;
       
    35 
       
    36 const TUint16 KDefaultPageScanInterval	= 0x0800;
       
    37 const TUint16 KDefaultPageScanWindow	= 0x0012;
       
    38 const TUint8 KDefaultPageScanType		= 0x00;
       
    39 
       
    40 const TUint16 KFastConnectionPageScanInterval	= 0x0024;
       
    41 const TUint16 KFastConnectionPageScanWindow		= 0x0012;
       
    42 const TUint8 KFastConnectionPageScanType		= 0x01;
       
    43 
       
    44 
       
    45 CBrEdrControllerConfiguration::CBrEdrControllerConfiguration(MHCICommandQueue& aCommandQueue,
       
    46 															 CLinkMgrProtocol& aLinkMgrProtocol)
       
    47 	:	iOriginalPageScanInterval(KDefaultPageScanInterval),
       
    48 		iOriginalPageScanWindow(KDefaultPageScanWindow),
       
    49 		iOriginalPageScanType(KDefaultPageScanType),
       
    50 		iPageScanParameters(KUnitializedParameters),
       
    51 		iCommandQueue(aCommandQueue),
       
    52 		iLinkMgrProtocol(aLinkMgrProtocol)
       
    53 	{
       
    54 	LOG_FUNC
       
    55 	}
       
    56 
       
    57 CBrEdrControllerConfiguration::~CBrEdrControllerConfiguration()
       
    58 	{
       
    59 	LOG_FUNC
       
    60 	}
       
    61 
       
    62 CBrEdrControllerConfiguration* CBrEdrControllerConfiguration::NewL(MHCICommandQueue& aCommandQueue,
       
    63 																   CLinkMgrProtocol& aLinkMgrProtocol)
       
    64 	{
       
    65 	LOG_STATIC_FUNC
       
    66 	CBrEdrControllerConfiguration* self
       
    67 		= new (ELeave) CBrEdrControllerConfiguration(aCommandQueue, aLinkMgrProtocol);
       
    68 	return self;
       
    69 	}
       
    70 
       
    71 void CBrEdrControllerConfiguration::SetPageScanParametersL(TPageScanParameterSettings aPageScanParameters)
       
    72 	{
       
    73 	LOG_FUNC
       
    74 	if (iPageScanParameters == KUnitializedParameters)
       
    75 		{
       
    76 		// obtain the original Controller parameters
       
    77 		LOG(_L("obtaining the original Controller parameters"));
       
    78 		
       
    79 		CReadPageScanActivityCommand* readPageScanActCmd = CReadPageScanActivityCommand::NewL();
       
    80 		// ownership of cmd transfered even if MhcqAddCommandL leaves
       
    81 		iCommandQueue.MhcqAddCommandL(readPageScanActCmd, *this);
       
    82 
       
    83 		CReadPageScanTypeCommand* readPageScanTypeCmd = CReadPageScanTypeCommand::NewL();
       
    84 		// ownership of cmd transfered even if MhcqAddCommandL leaves
       
    85 		iCommandQueue.MhcqAddCommandL(readPageScanTypeCmd, *this);
       
    86 
       
    87 		iPageScanParameters = EStandardPageScanParameters;
       
    88 
       
    89 		// if the original parameters are requested we don't do anything
       
    90 		if (aPageScanParameters == EStandardPageScanParameters)
       
    91 			return;
       
    92 		}
       
    93 
       
    94 	// store the settings to publish them later
       
    95 	iPageScanParameters			= EStandardPageScanParameters;
       
    96 
       
    97 	TUint16 pageScanInterval	= iOriginalPageScanInterval;
       
    98 	TUint16 pageScanWindow		= iOriginalPageScanWindow;
       
    99 	TUint8 pageScanType			= iOriginalPageScanType;
       
   100 
       
   101 	if (aPageScanParameters == EFastConnectionPageScanParameters)
       
   102 		{
       
   103 		// store the settings to publish them later
       
   104 		iPageScanParameters	= EFastConnectionPageScanParameters;
       
   105 		pageScanInterval	= KFastConnectionPageScanInterval;
       
   106 		pageScanWindow		= KFastConnectionPageScanWindow;
       
   107 		pageScanType		= KFastConnectionPageScanType;
       
   108 		}
       
   109 
       
   110 	LOG1(_L("Applying page scan parameter settings %d"), iPageScanParameters);
       
   111 
       
   112 	// apply the requested page scan parameters
       
   113 	CWritePageScanActivityCommand* writePageScanActCmd
       
   114 		= CWritePageScanActivityCommand::NewL(pageScanInterval, pageScanWindow);
       
   115 	// ownership of cmd transfered even if MhcqAddCommandL leaves
       
   116 	iCommandQueue.MhcqAddCommandL(writePageScanActCmd, *this);
       
   117 
       
   118 	CWritePageScanTypeCommand* writePageScanTypeCmd = CWritePageScanTypeCommand::NewL(pageScanType);
       
   119 	// ownership of cmd transfered even if MhcqAddCommandL leaves
       
   120 	iCommandQueue.MhcqAddCommandL(writePageScanTypeCmd, *this);
       
   121 	}
       
   122 
       
   123 void CBrEdrControllerConfiguration::MhcqcCommandEventReceived(const THCIEventBase& aEvent,
       
   124 															  const CHCICommandBase* aRelatedCommand)
       
   125 	{
       
   126 	LOG_FUNC
       
   127 	LOG3(_L("Event 0x%02x (errorcode=%d) received for Command 0x%04x"), aEvent.EventCode(),
       
   128 			aEvent.ErrorCode(), aRelatedCommand->Opcode());
       
   129 	if (aEvent.EventCode() == ECommandCompleteEvent && aEvent.ErrorCode() == KErrNone)
       
   130 		{
       
   131 		switch (aRelatedCommand->Opcode())
       
   132 			{
       
   133 		case KReadPageScanActivityOpcode:
       
   134 			{
       
   135 			const TReadPageScanActivityCompleteEvent& readPageScanActivityCompleteEvent
       
   136 				= TReadPageScanActivityCompleteEvent::Cast(aEvent);
       
   137 			iOriginalPageScanInterval	= readPageScanActivityCompleteEvent.PageScanInterval();
       
   138 			iOriginalPageScanWindow		= readPageScanActivityCompleteEvent.PageScanwindow();
       
   139 			LOG2(_L("page scan activity parameters received (interval=0x%04x, window=0x%04x)"),
       
   140 					iOriginalPageScanInterval, iOriginalPageScanWindow);
       
   141 			break;
       
   142 			}
       
   143 		case KReadPageScanTypeOpcode:
       
   144 			{
       
   145 			const TReadPageScanTypeCompleteEvent& readPageScanTypeCompleteEvent
       
   146 				= TReadPageScanTypeCompleteEvent::Cast(aEvent);
       
   147 			iOriginalPageScanType = readPageScanTypeCompleteEvent.PageScanType();
       
   148 			LOG1(_L("page scan type received (=0x%02)"), iOriginalPageScanType);
       
   149 			break;
       
   150 			}
       
   151 		case KWritePageScanActivityOpcode:
       
   152 			// provide an indication that the requested parameters have been enabled
       
   153 			// as there is no way to indicate halfway success (one command sent succefully, another failed)
       
   154 			// page scan activity command status is the criteria for the whole operation
       
   155 			(void) RProperty::Set(KPropertyUidBluetoothCategory,
       
   156 								  KPropertyKeyBluetoothGetPageScanParameters, iPageScanParameters);
       
   157 			LOG1(_L("Write Page Scan Activity command confirmed, page scan parameter settings %d applied"),
       
   158 					iPageScanParameters);
       
   159 			break;
       
   160 		case KWritePageScanTypeOpcode:
       
   161 			LOG(_L("Write Page Scan Type command confirmed"));
       
   162 			break;
       
   163 		default:
       
   164 			LOG1(_L("Warning!! Upexpected Command Complete Event Received (command opcode:0x%04x)"),
       
   165 					aRelatedCommand->Opcode());
       
   166 			__ASSERT_DEBUG(EFalse, Panic(EHCIUnexpectedEvent));
       
   167 			break;
       
   168 			}
       
   169 		}
       
   170 	}
       
   171 
       
   172 void CBrEdrControllerConfiguration::SetPageScanParameters(TPageScanParameterSettings aPageScanParameters)
       
   173 	{
       
   174 	LOG_FUNC
       
   175 	// errors handled by upper level component
       
   176 	// not updated in time the KPropertyKeyBluetoothGetPageScanParameters P&K key indicates an error situation
       
   177 	TRAPD(err, SetPageScanParametersL(aPageScanParameters));
       
   178 	if (err)
       
   179 		{
       
   180 		LOG1(_L("Warning!! SetPageScanParameters left with error %d"), err);
       
   181 		}
       
   182 	}
       
   183 
       
   184 void CBrEdrControllerConfiguration::MhcqcCommandErrored(TInt IF_FLOGGING(aErrorCode),
       
   185 														const CHCICommandBase* IF_FLOGGING(aCommand))
       
   186 	{
       
   187 	LOG_FUNC
       
   188 	// errors handled by upper level component
       
   189 	// not updated in time the KPropertyKeyBluetoothGetPageScanParameters P&K key indicates an error situation
       
   190 	LOG2(_L("Warning!! Command 0x%04x failed with error %d"), aCommand->Opcode(), aErrorCode);
       
   191 	}
       
   192