bluetooth/btstack/sdp/sdpnetdb.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2000-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 #include <bluetooth/logger.h>
       
    17 #include <bt_sock.h>
       
    18 #include "debug.h"
       
    19 #include <btsdp.h>
       
    20 #include "sdpnetdb.h"
       
    21 #include "sdp.h"
       
    22 #include "sdppdu.h"
       
    23 #include "sdpkey.h"
       
    24 #include "sdpstackutil.h"
       
    25 #include "sdpclient.h"
       
    26 
       
    27 #include "BTSec.h"
       
    28 
       
    29 #ifdef __FLOG_ACTIVE
       
    30 _LIT8(KLogComponent, LOG_COMPONENT_SDP);
       
    31 #endif
       
    32 
       
    33 //Diagnostic string for security check failures, in builds without platsec
       
    34 //diagnostics this will be NULL.
       
    35 const char* const KBT_SDPNETDB_NAME_DIAG = __PLATSEC_DIAGNOSTIC_STRING("Bluetooth SDP Net Database");
       
    36 
       
    37 
       
    38 CSdpNetDbProvider::CSdpNetDbProvider(CSdpProtocol& aProtocol)
       
    39 : iProtocol(aProtocol)
       
    40 	{
       
    41 	LOG_FUNC
       
    42 	}
       
    43 
       
    44 void CSdpNetDbProvider::ConstructL()
       
    45 	{
       
    46 	LOG_FUNC
       
    47 	}
       
    48 
       
    49 CSdpNetDbProvider* CSdpNetDbProvider::NewL(CSdpProtocol& aProtocol)
       
    50 	{
       
    51 	LOG_STATIC_FUNC
       
    52 	CSdpNetDbProvider* self = new(ELeave) CSdpNetDbProvider(aProtocol);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 	CleanupStack::Pop();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 CSdpNetDbProvider::~CSdpNetDbProvider()
       
    60 	{
       
    61 	LOG_FUNC
       
    62 	if (iClient)
       
    63 		iClient->RemoveNetDbProvider(*this);
       
    64 	delete iResultBuf;
       
    65 	}
       
    66 
       
    67 void CSdpNetDbProvider::Query(TDes8& aBuffer)
       
    68 	{
       
    69 	LOG_FUNC
       
    70 	__ASSERT_DEBUG(!iQueryBuffer, Panic(ESdpTwoQuerys));
       
    71 	iQueryBuffer = &aBuffer;
       
    72 	if (iQueryBuffer->Length() < TInt(sizeof(TUint)))
       
    73 		{// Not big enough to hold the query type!!
       
    74 		Error(KErrSdpBadRequestBufferLength);
       
    75 		return;
       
    76 		}
       
    77 	TUint type = *reinterpret_cast<const TUint*>(aBuffer.Ptr());
       
    78 
       
    79 	switch(type & 0x7f)
       
    80 		{
       
    81 	case KSDPConnectQuery:
       
    82 		ConnectQuery();
       
    83 		break;
       
    84 	case KSDPServiceQuery:
       
    85 		ServiceQuery();
       
    86 		break;
       
    87 	case KSDPAttributeQuery:
       
    88 		AttributeQuery();
       
    89 		break;
       
    90 	case KSDPCancelQuery:
       
    91 		CancelCurrentOperation();
       
    92 		break;
       
    93 	case KSDPEncodedQuery:
       
    94 		EncodedQuery();
       
    95 		break;
       
    96 	case KSDPRetrieveResultQuery:
       
    97 		RetrieveResult();
       
    98 		break;
       
    99 	default:
       
   100 		Error(KErrNotSupported);
       
   101 		return;
       
   102 		};
       
   103 	}
       
   104 
       
   105 void CSdpNetDbProvider::Add(TDes8& /*aBuffer*/)
       
   106 	{
       
   107 	LOG_FUNC
       
   108 	__ASSERT_DEBUG(!iQueryBuffer, Panic(ESdpTwoQuerys));
       
   109 	iNotify->QueryComplete(KErrNotSupported);
       
   110 	}
       
   111 
       
   112 void CSdpNetDbProvider::Remove(TDes8& /*aBuffer*/)
       
   113 	{
       
   114 	LOG_FUNC
       
   115 	__ASSERT_DEBUG(!iQueryBuffer, Panic(ESdpTwoQuerys));
       
   116 	iNotify->QueryComplete(KErrNotSupported);
       
   117 	}
       
   118 
       
   119 void CSdpNetDbProvider::CancelCurrentOperation()
       
   120 	{
       
   121 	LOG_FUNC
       
   122 	iQueryBuffer = 0;
       
   123 	}
       
   124 
       
   125 void CSdpNetDbProvider::QueryComplete(const TDesC8& aData)
       
   126 	{
       
   127 	LOG_FUNC
       
   128 	if (!iQueryBuffer)
       
   129 		return;
       
   130 
       
   131 	TInt ret = KErrNone;
       
   132 	TUint type = *reinterpret_cast<const TUint*>(iQueryBuffer->Ptr());
       
   133 	if (type & 0x80)
       
   134 		{// Buffer result and Just send size back
       
   135 		delete iResultBuf;
       
   136 		iResultBuf = 0;
       
   137 		TRAP(ret, iResultBuf = aData.AllocL());
       
   138 		if (ret == KErrNone)
       
   139 			{
       
   140 			iQueryBuffer->Copy(TPckgC<TInt>(aData.Length()));
       
   141 			}
       
   142 		}
       
   143 	else
       
   144 		{// Copy the whole result back
       
   145 		ret = ReturnResult(aData);
       
   146 		}
       
   147 	iNotify->QueryComplete(ret);
       
   148 	iQueryBuffer = 0;
       
   149 	}
       
   150 
       
   151 TInt CSdpNetDbProvider::ReturnResult(const TDesC8& aData)
       
   152 	{
       
   153 	LOG_FUNC
       
   154 	if(!iQueryBuffer)
       
   155 		{
       
   156 		return KErrGeneral;
       
   157 		}
       
   158 
       
   159 	if (iQueryBuffer->MaxLength() < aData.Length())
       
   160 		{
       
   161 		return KErrArgument;
       
   162 		}
       
   163 	iQueryBuffer->Copy(aData);
       
   164 	return KErrNone;
       
   165 	}
       
   166 
       
   167 void CSdpNetDbProvider::ClientUp()
       
   168 	{
       
   169 	LOG_FUNC
       
   170 	if(!iQueryBuffer)
       
   171 		return;
       
   172 
       
   173 	iQueryBuffer->Zero();
       
   174 	QueryComplete(KNullDesC8);
       
   175 	}
       
   176 
       
   177 void CSdpNetDbProvider::ClientDown()
       
   178 	{
       
   179 	LOG_FUNC
       
   180 	if (iClient) 
       
   181 		iClient->RemoveNetDbProvider(*this);
       
   182 	iClient = 0;
       
   183 	Error(KErrDisconnected);
       
   184 	}
       
   185 
       
   186 void CSdpNetDbProvider::Error(TInt aErrorCode)
       
   187 	{
       
   188 	LOG_FUNC
       
   189 	LOG1(_L("SDP: Error encountered in SDP NetDb: %d\n"), aErrorCode);
       
   190 	if(!iQueryBuffer)
       
   191 		return;
       
   192 
       
   193 	iNotify->QueryComplete(aErrorCode);
       
   194 	iQueryBuffer = 0;
       
   195 	}
       
   196 	
       
   197 TInt CSdpNetDbProvider::SecurityCheck(MProvdSecurityChecker *aSecurityChecker)
       
   198 	{
       
   199 	LOG_FUNC	
       
   200 	__ASSERT_ALWAYS(aSecurityChecker, User::Panic(KSECURITY_PANIC, EBTPanicNullSecurityChecker));
       
   201 	
       
   202 	iSecurityChecker = aSecurityChecker;
       
   203 	return iSecurityChecker->CheckPolicy(KLOCAL_SERVICES, KBT_SDPNETDB_NAME_DIAG);			
       
   204 	}
       
   205 
       
   206 void CSdpNetDbProvider::ConnectQuery()
       
   207 	{
       
   208 	LOG_FUNC
       
   209 	if(iClient)
       
   210 		{
       
   211 		LOG(_L("SDP: ConnectQuery request from net db whilst already connected: REJECTED!\n"));
       
   212 		Error(KErrInUse);
       
   213 		return;
       
   214 		}
       
   215 	if (iQueryBuffer->Length() != sizeof(TSDPConnectQuery))
       
   216 		{
       
   217 		LOG(_L("SDP: ConnectQuery request from net db with bad query length: REJECTED!\n"));
       
   218 		Error(KErrSdpBadRequestBufferLength);
       
   219 		return;
       
   220 		}
       
   221 	LOG(_L("SDP: ConnectQuery request from net db\n"));
       
   222 	TSDPConnectQuery& queryKey = *(TSDPConnectQuery*)iQueryBuffer->Ptr();
       
   223 	iRemoteDev = queryKey.iAddr;
       
   224 	iProtocol.GetClient(*this);
       
   225 	}
       
   226 
       
   227 void CSdpNetDbProvider::ServiceQuery()
       
   228 	{
       
   229 	LOG_FUNC
       
   230 	if (!iClient)
       
   231 		{
       
   232 		Error(KErrSdpClientNotConnected);
       
   233 		return;
       
   234 		}
       
   235 	if (iQueryBuffer->Length() != sizeof(TSDPServiceSearchKey))
       
   236 		{
       
   237 		Error(KErrSdpBadRequestBufferLength);
       
   238 		return;
       
   239 		}
       
   240 	TSDPServiceSearchKey& queryKey = *(TSDPServiceSearchKey*)iQueryBuffer->Ptr();
       
   241 	// Ought to do this check, but can't because of esock defect EDNJDIN-4HPKJZ.
       
   242 //	if (iQueryBuffer->MaxLength() < (TInt)(queryKey.iMaxCount*sizeof(TUint)) + 21)
       
   243 //		{
       
   244 //		Error(KErrSdpBadResultBufferLength);
       
   245 //		return;
       
   246 //		}
       
   247 
       
   248 	if (queryKey.iStateLength > KSdpContinuationStateMaxLength)
       
   249 		{
       
   250 		Error(KErrSdpBadContinuationState);
       
   251 		return;
       
   252 		}
       
   253 
       
   254 	TPtrC8 contState(queryKey.iContinuationState, queryKey.iStateLength);
       
   255 
       
   256 	iClient->ServiceSearchRequest(*this, 
       
   257 		                          queryKey.iMaxCount, 
       
   258 								  queryKey.iUUID, 
       
   259 								  contState);
       
   260 	}
       
   261 
       
   262 void CSdpNetDbProvider::AttributeQuery()
       
   263 	{
       
   264 	LOG_FUNC
       
   265 	if (!iClient)
       
   266 		{
       
   267 		Error(KErrSdpClientNotConnected);
       
   268 		return;
       
   269 		}
       
   270 	if (iQueryBuffer->Length() != sizeof(TSDPAttributeKey))
       
   271 		{
       
   272 		Error(KErrSdpBadRequestBufferLength);
       
   273 		return;
       
   274 		}
       
   275 	TSDPAttributeKey& queryKey = *(TSDPAttributeKey*)iQueryBuffer->Ptr();
       
   276 
       
   277 	// Ought to do this check, but can't because of esock defect EDNJDIN-4HPKJZ.
       
   278 //	if (iQueryBuffer->MaxLength() < queryKey.iMaxLength + 19)
       
   279 //		{
       
   280 //		Error(KErrSdpBadResultBufferLength);
       
   281 //		return;
       
   282 //		}
       
   283 
       
   284 	if (queryKey.iStateLength > KSdpContinuationStateMaxLength)
       
   285 		{
       
   286 		Error(KErrSdpBadContinuationState);
       
   287 		return;
       
   288 		}
       
   289 
       
   290 	TPtrC8 contState(queryKey.iContinuationState, queryKey.iStateLength);
       
   291 	iClient->ServiceAttributeRequest(*this, 
       
   292 		                             queryKey.iServiceRecordHandle,
       
   293 		                             queryKey.iMaxLength,
       
   294 									 queryKey.iRange,
       
   295 									 queryKey.iAttribute,
       
   296 									 contState);
       
   297 	}
       
   298 
       
   299 void CSdpNetDbProvider::EncodedQuery()
       
   300 	{
       
   301 	LOG_FUNC
       
   302 	if (!iClient)
       
   303 		{
       
   304 		Error(KErrSdpClientNotConnected);
       
   305 		return;
       
   306 		}
       
   307 	if (iQueryBuffer->Length() <= STATIC_CAST(TInt,sizeof(TSDPEncodedKey/*Buf??*/)))
       
   308 		{
       
   309 		Error(KErrSdpBadRequestBufferLength);
       
   310 		return;
       
   311 		}
       
   312 	TSDPEncodedKey& queryKey = *(TSDPEncodedKey*)iQueryBuffer->Ptr();
       
   313 	const TUint8* pData = iQueryBuffer->Ptr();
       
   314 	pData += sizeof(TSDPEncodedKey/*Buf??*/);
       
   315 	TPtrC8 dataDesc(pData, iQueryBuffer->Length()-sizeof(TSDPEncodedKey));
       
   316 	//TPtrC8 dataDesc(iQueryBuffer->Mid(sizeof(TSDPEncodedKey)));
       
   317 
       
   318 	iClient->EncodedRequest(*this, 
       
   319 		                    queryKey.iPduId, 
       
   320 							dataDesc);
       
   321 	}
       
   322 
       
   323 
       
   324 #if 0
       
   325 void CSdpNetDbProvider::EncodedQuery()
       
   326 	{
       
   327 	LOG_FUNC
       
   328 	if (!iClient)
       
   329 		{
       
   330 		Error(KErrSdpClientNotConnected);
       
   331 		return;
       
   332 		}
       
   333 	if (iQueryBuffer->Length() < sizeof(TSDPEncodedKey/*Buf??*/))
       
   334 		{
       
   335 		Error(KErrSdpBadRequestBufferLength);
       
   336 		return;
       
   337 		}
       
   338 
       
   339 
       
   340 	TSDPEncodedKey& queryKey = *(TSDPEncodedKey*)iQueryBuffer->Ptr();
       
   341 	TPtrC8 dataDesc(iQueryBuffer->Mid(sizeof(TSDPEncodedKey)));
       
   342 
       
   343 	iClient->EncodedRequest(*this, 
       
   344 							queryKey.iPduId, 
       
   345 							dataDesc);
       
   346 	}
       
   347 #endif
       
   348 
       
   349 
       
   350 
       
   351 void CSdpNetDbProvider::RetrieveResult()
       
   352 	{
       
   353 	LOG_FUNC
       
   354 	TInt ret;
       
   355 	if (!iResultBuf)
       
   356 		{// No result to send
       
   357 		ret = KErrArgument;
       
   358 		}
       
   359 	else
       
   360 		{// Copy over the result
       
   361 		ret = ReturnResult(*iResultBuf);
       
   362 		delete iResultBuf;
       
   363 		iResultBuf = 0;
       
   364 		}
       
   365 	iNotify->QueryComplete(ret);
       
   366 	iQueryBuffer = 0;
       
   367 	}
       
   368