bluetoothcommsprofiles/btpan/panagt/panlocalsdphelper.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-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 // panhelperlocalsdp.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "panlocalsdphelper.h"
       
    19 #include "panagtutils.h"
       
    20 #include "pancommon.h"			// For KBnepPsm
       
    21 #include "panprog.h"			// For EOther
       
    22 
       
    23 using namespace PanAgent;
       
    24 
       
    25 const TSdpServRecordHandle KIllegalSdpRecordHandle = 0;
       
    26 
       
    27 CPanHelperLocalSdpRegistrar* CPanHelperLocalSdpRegistrar::NewL()
       
    28 /**
       
    29 Return a newly constructed local sdp registrar
       
    30 */
       
    31 	{
       
    32 	CPanHelperLocalSdpRegistrar* self = new(ELeave) CPanHelperLocalSdpRegistrar();
       
    33 	CleanupStack::PushL(self);
       
    34 	self->ConstructL();
       
    35 	CleanupStack::Pop(self);
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 CPanHelperLocalSdpRegistrar::CPanHelperLocalSdpRegistrar()
       
    40 /**
       
    41 do nothing
       
    42 */
       
    43 	{	}
       
    44 	
       
    45 void CPanHelperLocalSdpRegistrar::ConstructL()
       
    46 /**
       
    47 Initialisation
       
    48 */
       
    49 	{
       
    50 	User::LeaveIfError(iSdpServer.Connect());
       
    51 	User::LeaveIfError(iSdpDatabase.Open(iSdpServer));
       
    52 	for(TUint i=0;i<KMaxPanRoles;i++)
       
    53 		{
       
    54 		iLocalSdpRecordHandles[i] = KIllegalSdpRecordHandle;
       
    55 		}
       
    56 	}
       
    57 
       
    58 
       
    59 	
       
    60 CPanHelperLocalSdpRegistrar::~CPanHelperLocalSdpRegistrar()
       
    61 /**
       
    62 Clean up
       
    63 */
       
    64 	{
       
    65 	// Remove any SDP records created by this instance.
       
    66 	for(TUint i=0;i<KMaxPanRoles;i++)
       
    67 		{
       
    68 		if(iLocalSdpRecordHandles[i]!=KIllegalSdpRecordHandle)
       
    69 			{
       
    70 			iSdpDatabase.DeleteRecord(iLocalSdpRecordHandles[i]);
       
    71 			iLocalSdpRecordHandles[i] = KIllegalSdpRecordHandle;
       
    72 			}
       
    73 		}
       
    74 
       
    75 	iSdpDatabase.Close();
       
    76 	iSdpServer.Close();
       
    77 	}
       
    78 	
       
    79 
       
    80 
       
    81 void CPanHelperLocalSdpRegistrar::RegisterLocalSdpRecordL(TBluetoothPanRole aRole, TInt /*aIapId*/)
       
    82 /**
       
    83 Register the specified role in the local SDP database
       
    84 @param aRole The role to register
       
    85 @param aIapId The IAP that we're using - for finding out which network layer protocols 
       
    86 we're supporting (not implemented at present), and filling in NAP-related stuff when 
       
    87 we support NAP (also not supported at present!)
       
    88 */
       
    89 	{
       
    90 	_LIT8(KProviderName, "Symbian OS");
       
    91 	
       
    92 	// Make sure any array access is going to fall within the array bounds
       
    93 	__ASSERT_DEBUG(static_cast<TUint>(aRole) >= static_cast<TUint>(EPanRoleU), PanAgentPanic(EPanRoleOutOfBounds));
       
    94 	__ASSERT_DEBUG((static_cast<TUint>(aRole) - static_cast<TUint>(EPanRoleU)) <= KMaxPanRoles, PanAgentPanic(EPanRoleOutOfBounds));
       
    95 	__ASSERT_DEBUG(((aRole==EPanRoleU)||(aRole==EPanRoleGn)||(aRole==EPanRoleNap)), PanAgentPanic(EAttemptToRegisterIllegalPanRoleInLocalSdpDatabase)); //<@note embeds knowledge of valid roles
       
    96 
       
    97 	TUUID uuid(static_cast<TUint32>(aRole));
       
    98 	
       
    99 	// Rebase the role to use role as index into array
       
   100 	TUint panRoleRecordHandleIndex = static_cast<TUint>(aRole) - static_cast<TUint>(EPanRoleU);
       
   101 
       
   102 	// Check if the record is already registered.  If so, no action is required.
       
   103 	if(iLocalSdpRecordHandles[panRoleRecordHandleIndex] == KIllegalSdpRecordHandle)
       
   104 		{	
       
   105 		// Create the SDP record
       
   106 		iSdpDatabase.CreateServiceRecordL(uuid, iLocalSdpRecordHandles[panRoleRecordHandleIndex]);
       
   107 	
       
   108 		
       
   109 		TSdpServRecordHandle recordHandle = iLocalSdpRecordHandles[panRoleRecordHandleIndex];
       
   110 		iSdpDatabase.UpdateAttributeL(recordHandle,
       
   111 			KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetProviderName,
       
   112 			KProviderName
       
   113 			);
       
   114 		//
       
   115 		// Add attributes
       
   116 		//
       
   117 		CSdpAttrValue* attrVal = NULL;
       
   118 		CSdpAttrValueDES* attrValDES = NULL;	
       
   119 		
       
   120 		// Service Class ID list is handled above when creating the record
       
   121 		// protocol descriptor list
       
   122 		attrValDES = CSdpAttrValueDES::NewDESL(0);
       
   123 		CleanupStack::PushL(attrValDES);
       
   124 		attrValDES->StartListL()
       
   125 			->BuildDESL()->StartListL()
       
   126 				->BuildUUIDL(TUUID(TUint16(KL2CAPUUID)))					// UUID for protocol: L2CAP
       
   127 				->BuildUintL(TSdpIntBuf<TUint16>(KBnepPsm))					// PSM: BNEP
       
   128 			->EndListL()
       
   129 			->BuildDESL()->StartListL()
       
   130 				->BuildUUIDL(TUUID(TUint16(KBnepUUID)))						// UUID for protocol: BNEP
       
   131 				->BuildUintL(TSdpIntBuf<TUint16>(KBnepVersion))				// version: 0x0100
       
   132 				->BuildDESL()->StartListL()
       
   133 					->BuildUintL(TSdpIntBuf<TUint16>(0x0800))				// ethernet protocol: IPv4
       
   134 					->BuildUintL(TSdpIntBuf<TUint16>(0x0806))				// ethernet protocol: ARP
       
   135 					->BuildUintL(TSdpIntBuf<TUint16>(0x86dd))				// ethernet protocol: IPv6
       
   136 				->EndListL()
       
   137 			->EndListL()
       
   138 		->EndListL();
       
   139 		iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdProtocolDescriptorList, *attrValDES);
       
   140 		CleanupStack::PopAndDestroy(attrValDES);
       
   141 
       
   142 		// language base attribute ID list
       
   143 		attrValDES = CSdpAttrValueDES::NewDESL(0);
       
   144 		CleanupStack::PushL(attrValDES);
       
   145 		attrValDES->StartListL()
       
   146 			->BuildUintL(TSdpIntBuf<TUint16>(KLanguageEnglish))
       
   147 			->BuildUintL(TSdpIntBuf<TUint16>(KSdpAttrIdCharacterEncodingUTF8))
       
   148 			->BuildUintL(TSdpIntBuf<TUint16>(KSdpAttrIdBasePrimaryLanguage))
       
   149 		->EndListL();
       
   150 		iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdLanguageBaseAttributeIDList, *attrValDES);
       
   151 		CleanupStack::PopAndDestroy(attrValDES);
       
   152 
       
   153 		// bluetooth profile descriptor list
       
   154 		attrValDES = CSdpAttrValueDES::NewDESL(0);
       
   155 		CleanupStack::PushL(attrValDES);
       
   156 		attrValDES->StartListL()
       
   157 			->BuildDESL()->StartListL()
       
   158 				->BuildUUIDL(uuid)											// role: uuid of PAN role
       
   159 				->BuildUintL(TSdpIntBuf<TUint16>(KPanRoleVersion))			// version: 0x0100
       
   160 			->EndListL()
       
   161 		->EndListL();
       
   162 		iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBluetoothProfileDescriptorList, *attrValDES);
       
   163 		CleanupStack::PopAndDestroy(attrValDES);
       
   164 		
       
   165 		// bluetooth browse list
       
   166 		attrValDES = CSdpAttrValueDES::NewDESL(0);
       
   167 		CleanupStack::PushL(attrValDES);
       
   168 		attrValDES->StartListL()
       
   169 					->BuildUUIDL(TUUID(TUint32(KPublicBrowseGroupUUID)))	 // Public browse group										
       
   170 				  ->EndListL();
       
   171 		iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBrowseGroupList, *attrValDES);
       
   172 		CleanupStack::PopAndDestroy(attrValDES);
       
   173 	
       
   174 		// service name
       
   175 		switch(aRole)
       
   176 			{
       
   177 			case EPanRoleU:
       
   178 				{
       
   179 				attrVal = CSdpAttrValueString::NewStringL(_L8("Personal Ad-hoc User Service"));
       
   180 				CleanupStack::PushL(attrVal);
       
   181 				iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetServiceName, *attrVal);
       
   182 				CleanupStack::PopAndDestroy(attrVal);
       
   183 				break;
       
   184 				}
       
   185 			case EPanRoleGn:
       
   186 				{
       
   187 				attrVal = CSdpAttrValueString::NewStringL(_L8("Group Ad-hoc Network Service"));
       
   188 				CleanupStack::PushL(attrVal);
       
   189 				iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetServiceName, *attrVal);
       
   190 				CleanupStack::PopAndDestroy(attrVal);
       
   191 				break;
       
   192 				}
       
   193 			case EPanRoleNap:
       
   194 				{
       
   195 				attrVal = CSdpAttrValueString::NewStringL(_L8("Network Access Point Service"));
       
   196 				CleanupStack::PushL(attrVal);
       
   197 				iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetServiceName, *attrVal);
       
   198 				CleanupStack::PopAndDestroy(attrVal);
       
   199 				break;			
       
   200 				}
       
   201 			default:
       
   202 				{
       
   203 				PanAgentPanic(EAttemptToRegisterIllegalPanRoleInLocalSdpDatabase);
       
   204 				}
       
   205 			}
       
   206 			
       
   207 		// service description
       
   208 		switch(aRole)
       
   209 			{
       
   210 			case EPanRoleU:
       
   211 				{
       
   212 				attrVal = CSdpAttrValueString::NewStringL(_L8("Personal Ad-hoc User Service"));
       
   213 				CleanupStack::PushL(attrVal);
       
   214 				iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetServiceDescription, *attrVal);
       
   215 				CleanupStack::PopAndDestroy(attrVal);
       
   216 				break;			
       
   217 				}
       
   218 			case EPanRoleGn:
       
   219 				{
       
   220 				attrVal = CSdpAttrValueString::NewStringL(_L8("Personal Group Ad-hoc Network Service"));
       
   221 				CleanupStack::PushL(attrVal);
       
   222 				iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetServiceDescription, *attrVal);
       
   223 				CleanupStack::PopAndDestroy(attrVal);
       
   224 				break;
       
   225 				}
       
   226 			case EPanRoleNap:
       
   227 				{
       
   228 				attrVal = CSdpAttrValueString::NewStringL(_L8("Personal Ad-hoc Network Service which provides access to a network"));
       
   229 				CleanupStack::PushL(attrVal);
       
   230 				iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdBasePrimaryLanguage + KSdpAttrIdOffsetServiceDescription, *attrVal);
       
   231 				CleanupStack::PopAndDestroy(attrVal);
       
   232 				break;			
       
   233 				}
       
   234 			default:
       
   235 				{
       
   236 				PanAgentPanic(EAttemptToRegisterIllegalPanRoleInLocalSdpDatabase);
       
   237 				}
       
   238 			}
       
   239 
       
   240 		// Add NAP specific fields.
       
   241 		if(aRole == EPanRoleNap)
       
   242 			{
       
   243 			// Set the uplink type
       
   244 			attrVal = CSdpAttrValueInt::NewIntL(TSdpIntBuf<TUint16>(EOther));	// Access type set to 'other'
       
   245 			CleanupStack::PushL(attrVal);
       
   246 			iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdNetAccessType, *attrVal);
       
   247 			CleanupStack::PopAndDestroy(attrVal);
       
   248 			
       
   249 			// Set the uplink maximum throughput
       
   250 			attrVal = CSdpAttrValueInt::NewIntL(TSdpIntBuf<TUint32>(KPanNapDefaultUplinkThroughput));
       
   251 			CleanupStack::PushL(attrVal);
       
   252 			iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdMaxNetAccessRate, *attrVal);
       
   253 			CleanupStack::PopAndDestroy(attrVal);
       
   254 			}
       
   255 			
       
   256 		// security description
       
   257 		attrVal = CSdpAttrValueUint::NewUintL(TSdpIntBuf<TUint16>(EServiceLevelSecurity)); 		// security type: service level
       
   258 		CleanupStack::PushL(attrVal);
       
   259 		iSdpDatabase.UpdateAttributeL(recordHandle, KSdpAttrIdSecurityDescription, *attrVal);
       
   260 		CleanupStack::PopAndDestroy(attrVal);
       
   261 		}
       
   262 	}
       
   263 	
       
   264 void CPanHelperLocalSdpRegistrar::UnregisterLocalSdpRecord(TBluetoothPanRole aRole)
       
   265 /**
       
   266 Can be called to unregister an SDP record even if it is not registered.
       
   267 Simplifies logic in PAN agent, so it doesn't need to keep track of which roles it has unregistered
       
   268 */
       
   269 	{
       
   270 	// Make sure any array access is going to fall within the array bounds
       
   271 	__ASSERT_DEBUG(static_cast<TUint>(aRole) >= static_cast<TUint>(EPanRoleU), PanAgentPanic(EPanRoleOutOfBounds));
       
   272 	__ASSERT_DEBUG((static_cast<TUint>(aRole) - static_cast<TUint>(EPanRoleU)) <= KMaxPanRoles, PanAgentPanic(EPanRoleOutOfBounds));
       
   273 	__ASSERT_ALWAYS(((aRole==EPanRoleU)||(aRole==EPanRoleGn)||(aRole==EPanRoleNap)), PanAgentPanic(EAttemptToRegisterIllegalPanRoleInLocalSdpDatabase)); //<@note embeds knowledge of valid roles
       
   274 
       
   275 	TInt arrayIndex = (static_cast<TUint>(aRole) - static_cast<TUint>(EPanRoleU));
       
   276 	if(iLocalSdpRecordHandles[arrayIndex]!=KIllegalSdpRecordHandle)
       
   277 		{
       
   278 		iSdpDatabase.DeleteRecord(iLocalSdpRecordHandles[arrayIndex]);
       
   279 		iLocalSdpRecordHandles[arrayIndex] = KIllegalSdpRecordHandle;
       
   280 		}
       
   281 	}