bluetooth/btstack/eirman/eirmanserver.cpp
changeset 0 29b1cd4cb562
child 23 32ba20339036
child 32 f72906e669b4
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2007-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  @internalComponent
       
    19 */
       
    20 
       
    21 #include "eirmanserver.h"
       
    22 #include <e32base.h>
       
    23 #include <bluetooth/logger.h>
       
    24 #include <bluetooth/logger/components.h>
       
    25 #include <bluetooth/eirmanshared.h>
       
    26 #include "eirmansession.h"
       
    27 #include "eirmanager.h"
       
    28 #include "eirmanserversecuritypolicy.h"
       
    29 #include "linkmgr.h"
       
    30 #include "eirmanpanics.h"
       
    31 
       
    32 
       
    33 #ifdef __FLOG_ACTIVE
       
    34 _LIT8(KLogComponent, LOG_COMPONENT_EIRMANAGER);
       
    35 #endif
       
    36 
       
    37 CEirManServer* CEirManServer::NewL(MHCICommandQueue& aCommandQueue, CLinkMgrProtocol& aLinkMgrProtocol)
       
    38 	{
       
    39 	LOG_STATIC_FUNC
       
    40 	
       
    41 	CEirManServer* self = new(ELeave) CEirManServer(aCommandQueue, aLinkMgrProtocol);
       
    42 	CleanupStack::PushL(self);
       
    43 	// StartL is where the kernel checks that there isn't already an instance 
       
    44 	// of the same server running, so do it before ConstructL.
       
    45 	self->StartL(KEirManServerName);
       
    46 	self->ConstructL();
       
    47 	CleanupStack::Pop(self);
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 CEirManServer::~CEirManServer()
       
    52 	{
       
    53 	LOG_FUNC
       
    54 	delete iEirManager;
       
    55 	}
       
    56 
       
    57 CEirManServer::CEirManServer(MHCICommandQueue& aCommandQueue, CLinkMgrProtocol& aLinkMgrProtocol)
       
    58 	: CPolicyServer(CActive::EPriorityStandard, KEirManServerPolicy)
       
    59 	, iCommandQueue(aCommandQueue)
       
    60 	, iLinkMgrProtocol(aLinkMgrProtocol)
       
    61 	, iSessionCount(0)
       
    62 	, iIsFeaturesReady(EFalse)
       
    63 	, iIsEirSupported(EFalse)
       
    64 	{
       
    65 	LOG_FUNC
       
    66 	}
       
    67 
       
    68 void CEirManServer::ConstructL()
       
    69 	{
       
    70 	LOG_FUNC
       
    71 	}
       
    72 
       
    73 CSession2* CEirManServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
       
    74 	{
       
    75 	LOG_FUNC
       
    76 	// Server will refuse to create any more session if we have found out eir isn't supported.
       
    77 	if(iIsFeaturesReady && !iIsEirSupported)
       
    78 		{
       
    79 		User::Leave(KErrNotSupported);
       
    80 		}
       
    81 	
       
    82 	LOG3(_L("CEirManServer::NewSessionL aVersion = (%d,%d,%d)"), aVersion.iMajor, aVersion.iMinor, aVersion.iBuild);
       
    83 	
       
    84 	// Version number check...
       
    85 	TVersion v(KEirManSrvMajorVersionNumber,
       
    86 			   KEirManSrvMinorVersionNumber,
       
    87 			   KEirManSrvBuildVersionNumber);
       
    88 	
       
    89 	if ( !User::QueryVersionSupported(v, aVersion) )
       
    90 		{
       
    91 		LEAVEIFERRORL(KErrNotSupported);
       
    92 		}
       
    93 	
       
    94 	CEirManServer* ncThis = const_cast<CEirManServer*>(this);
       
    95 	
       
    96 	CEirManSession* sess = CEirManSession::NewL(*ncThis);
       
    97 	LOG1(_L("\tsess = 0x%08x"), sess);
       
    98 	return sess;
       
    99 	}
       
   100 
       
   101 void CEirManServer::AddSession()
       
   102 	{
       
   103 	LOG_FUNC
       
   104 	if(iSessionCount++ == 0)
       
   105 		{
       
   106 		// While we have clients we need to make sure that the protocol remains alive.
       
   107 		iLinkMgrProtocol.LocalOpen();
       
   108 		}
       
   109 	}
       
   110 
       
   111 void CEirManServer::DropSession()
       
   112 	{
       
   113 	LOG_FUNC
       
   114 	if(--iSessionCount == 0)
       
   115 		{
       
   116 		// There are no long 
       
   117 		iLinkMgrProtocol.LocalClose();
       
   118 		}
       
   119 	}
       
   120 
       
   121 void CEirManServer::NotifyFeaturesReady()
       
   122 	{
       
   123 	if(iIsFeaturesReady == EFalse)
       
   124 		{
       
   125 		__ASSERT_DEBUG(!iEirManager, EIR_SERVER_PANIC(EEirServerEirMangerAlreadyExists));
       
   126 		TRAPD(err, iEirManager = CEirManager::NewL(iCommandQueue, iLinkMgrProtocol));
       
   127 		iIsFeaturesReady = ETrue;
       
   128 		
       
   129 		iSessionIter.SetToFirst();
       
   130 		CSession2* sessionPtr;
       
   131 		if(iLinkMgrProtocol.IsExtendedInquiryResponseSupportedLocally() && err == KErrNone)
       
   132 			{
       
   133 			iIsEirSupported = ETrue;
       
   134 			}
       
   135 		else
       
   136 			{
       
   137 			err = ((err != KErrNone) ? KErrNoMemory : KErrNotSupported);
       
   138 			}
       
   139 		
       
   140 		while((sessionPtr = iSessionIter++) != NULL)
       
   141 			{
       
   142 			CEirManSession* eirSession = static_cast<CEirManSession*>(sessionPtr);
       
   143 			eirSession->NotifyEirFeatureState(err);
       
   144 			}
       
   145 		}
       
   146 	}
       
   147 
       
   148 TEirFeatureState CEirManServer::EirFeatureState()
       
   149 	{
       
   150 	TEirFeatureState ret = EEirFeaturePending;
       
   151 	if(iIsFeaturesReady && iIsEirSupported)
       
   152 		{
       
   153 		ret = EEirFeatureReady;
       
   154 		}
       
   155 	else if(iIsFeaturesReady && !iIsEirSupported)
       
   156 		{
       
   157 		ret = EEirFeatureNotSupported;
       
   158 		}
       
   159 	else
       
   160 		{
       
   161 		// ret will still be EEirFeaturePending
       
   162 		}
       
   163 	return ret;
       
   164 	}
       
   165 
       
   166 CPolicyServer::TCustomResult CEirManServer::CustomSecurityCheckL(const RMessage2& aMsg, TInt& /*aAction*/, TSecurityInfo& /*aMissing*/)
       
   167 	{
       
   168 	TEirManMessages function = static_cast<TEirManMessages>(aMsg.Function());
       
   169 	TEirTag tag;
       
   170 	TCustomResult result = EFail; //Fail everything by default
       
   171 	_LIT_SECURITY_POLICY_S0(KSDPSecurityPolicy, KSDPServerID);
       
   172 	_LIT_SECURITY_POLICY_S0(KStackSecurityPolicy, KStackID);
       
   173 	_LIT_SECURITY_POLICY_C1(KVendorSpecificDataSecurityPolicy, ECapabilityWriteDeviceData);
       
   174 	if(function == EEirManRegisterTag)
       
   175 		{
       
   176 		tag = static_cast<TEirTag>(aMsg.Int0());
       
   177 		switch(tag)
       
   178 			{
       
   179 			case EEirTagName:
       
   180 			case EEirTagTxPowerLevel:
       
   181 				/** These must have come from the stack **/
       
   182 				if(KStackSecurityPolicy.CheckPolicy(aMsg))
       
   183 					{
       
   184 					result = EPass;
       
   185 					}
       
   186 				break;
       
   187 			case EEirTagSdpUuid16:
       
   188 			case EEirTagSdpUuid32:
       
   189 			case EEirTagSdpUuid128:
       
   190 				/** These must have come from SDP server **/
       
   191 				if(KSDPSecurityPolicy.CheckPolicy(aMsg))
       
   192 					{
       
   193 					result = EPass;
       
   194 					}
       
   195 				break;
       
   196 			case EEirTagManufacturerSpecific:
       
   197 				/** To do this you must have write device data **/
       
   198 				if(KVendorSpecificDataSecurityPolicy.CheckPolicy(aMsg))
       
   199 					{
       
   200 					result = EPass;
       
   201 					}
       
   202 				break;
       
   203 			
       
   204 			case EEirTagFlags:
       
   205 				/** At present no implementation of Flags is supported. 
       
   206 				    So we are rejecting this until an implementation is provided. **/
       
   207 			default: //unknown or reserved tag, reject
       
   208 				//no need to do anything 
       
   209 				break;
       
   210 			}
       
   211 		}
       
   212 	//Anything not covered by the above is invalid so do nothing and let it fail
       
   213 	if(result == EFail)
       
   214 		{
       
   215 		LOG1(_L("CEirManServer::CustomSecurityCheckL failed. Function: %d"), function);
       
   216 		}
       
   217 	return result;
       
   218 	}