bluetooth/btexample/example/afhsetter/AFHSetter.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 //
       
    15 
       
    16 #include <e32base.h>
       
    17 #include <e32std.h>
       
    18 #include <e32test.h>
       
    19 #include <c32comm.h>
       
    20 #include <f32file.h>
       
    21 #include <bt_subscribe.h>
       
    22 
       
    23 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    24 #include <bt_subscribe_partner.h>
       
    25 #endif
       
    26 
       
    27 #include <bt_sock.h>
       
    28 #include "AFHSetter.h"
       
    29 #if defined (__WINS__)
       
    30 #define PDD_NAME _L("ECDRV")
       
    31 #define LDD_NAME _L("ECOMM")
       
    32 #else  // __GCC32__
       
    33 #define PDD_NAME _L("EUART1")
       
    34 #define LDD_NAME _L("ECOMM")
       
    35 // #define ETNA_PDD_NAME _L("EUART2") // for debugging over com2
       
    36 #endif
       
    37 
       
    38 _LIT(KInstructions, "ffffffffffffffffffff allows all frequencies.\nYou will either be instructed to type in a new number\nor to enter individual bad channels by choosing a sequence\nof numbers in [0, 78]");
       
    39 
       
    40 inline void __BTDEBUGGER()
       
    41 // needed to call __DEBUGGER inside an __ASSERT
       
    42 	{
       
    43 	__DEBUGGER()
       
    44 	}
       
    45 
       
    46 GLDEF_D RTest test(_L("AFH Setter App"));
       
    47 
       
    48 void LoadLDD_PDD()
       
    49 	{
       
    50 	TInt r;
       
    51 #ifdef __EPOC32__
       
    52 	r=StartC32();
       
    53 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
    54 		{
       
    55 		test.Printf(_L("Failed %d!\n\r"),r);
       
    56 		test(r==KErrNone);
       
    57 		}
       
    58 	else
       
    59 		test.Printf(_L("Started C32\n"));
       
    60 #endif
       
    61 	test.Printf(_L("Loading PDD\n"));
       
    62 	r=User::LoadPhysicalDevice(PDD_NAME);
       
    63 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
    64 		{
       
    65 		test.Printf(_L("Failed %d!\n\r"),r);
       
    66 		test(r==KErrNone);
       
    67 		}
       
    68 	else 
       
    69 		test.Printf(_L("Loaded LDD\n"));
       
    70 	test.Printf(_L("Loading LDD\n"));
       
    71 	r=User::LoadLogicalDevice(LDD_NAME);
       
    72 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
    73 		{
       
    74 		test.Printf(_L("Failed %d!\n\r"),r);
       
    75 		test(r==KErrNone);
       
    76 		}
       
    77 	else
       
    78 		test.Printf(_L("Loaded PDD\n"));
       
    79 	}
       
    80 
       
    81 
       
    82 void TestL()
       
    83 	{
       
    84 	test.Printf(_L("%S\n"), &KInstructions);
       
    85 
       
    86 	// This function is essential!
       
    87 	LoadLDD_PDD();
       
    88 	// For some reason, you have to do the following to
       
    89 	// ensure that the file server behaves properly.
       
    90 
       
    91 
       
    92 //	File session
       
    93 	RFs fs;
       
    94 	fs.Connect();
       
    95 	fs.Close();
       
    96 	
       
    97 	CAFHSetter* AFHsetter = CAFHSetter::NewL(test);
       
    98 	//Subscriber* AFHSubscriber = CAFHSubscriber::NewL(test);
       
    99 
       
   100 	CActiveScheduler::Start();
       
   101 	
       
   102 	//delete AFHSubscriber;
       
   103 	delete AFHsetter;
       
   104 	}
       
   105 
       
   106 
       
   107 CAFHSetter* CAFHSetter::NewL(RTest& aTest)
       
   108 	{
       
   109 	CAFHSetter* s = new(ELeave) CAFHSetter(aTest);
       
   110 	CleanupStack::PushL(s);
       
   111 	s->ConstructL();
       
   112 	CleanupStack::Pop(s);
       
   113 	return s;
       
   114 	}
       
   115 
       
   116 CAFHSetter::CAFHSetter(RTest& aTest)
       
   117 : CActive(EPriorityStandard), iTest(aTest)
       
   118 	{
       
   119 	CActiveScheduler::Add(this);
       
   120 	}
       
   121 
       
   122 CAFHSetter::~CAFHSetter()
       
   123 	{
       
   124 	Cancel();
       
   125 	iTimer.Close();
       
   126 	iSS.Close();
       
   127 	}
       
   128 
       
   129 void CAFHSetter::ConstructL()
       
   130 	{
       
   131 	//start timer
       
   132 	iTimer.CreateLocal();
       
   133 	//start bluetooth
       
   134 	iSS.Connect();
       
   135 	TRequestStatus status;
       
   136     //iSS.StartProtocol(KBTAddrFamily,KSockSeqPacket,KBTLinkManager,status);
       
   137     iSS.StartProtocol(KBTAddrFamily,KSockSeqPacket,KL2CAP,status);
       
   138     User::WaitForRequest(status);
       
   139 	
       
   140 	//start publish and subscribe
       
   141 	_LIT_SECURITY_POLICY_PASS(KPassPolicy);
       
   142 	TInt err;
       
   143 	err=iProperty.Define(KPropertyUidBluetoothCategory,
       
   144 						 KPropertyKeyBluetoothSetAFHHostChannelClassification,
       
   145 						 RProperty::EByteArray,
       
   146 						 KPassPolicy,
       
   147 						 KPassPolicy);
       
   148 	err=iProperty.Define(KPropertyUidBluetoothCategory,
       
   149 						 KPropertyKeyBluetoothSetAFHChannelAssessmentMode,
       
   150 						 RProperty::EInt,
       
   151 						 KPassPolicy,
       
   152 						 KPassPolicy);
       
   153 	__ASSERT_DEBUG(err==0, __BTDEBUGGER());
       
   154 	
       
   155 	if (err) test.Printf(_L("Error %d defining property, continuing anyway\n"),err);
       
   156 
       
   157 
       
   158 	Start();
       
   159 	}
       
   160 
       
   161 void CAFHSetter::Start()
       
   162 	{
       
   163 	iTest.Console()->ClearScreen();
       
   164 	SendBluetoothAFHHostChannelClassification(); //send classification as kick off
       
   165 	GetClassificationBySettingBadChannels();
       
   166 	iTimer.After(iStatus,1);
       
   167 	SetActive();
       
   168 	}
       
   169 
       
   170 TInt CAFHSetter::SendBluetoothAFHHostChannelClassification()
       
   171 	{
       
   172 	//Set channels 3,4,5,6,7,8,9,10,12 and 15 as busy - not to be used by 
       
   173 	//Bluetooth Adaptive Frequency Hopping.
       
   174 	RProperty property;
       
   175 	TBTAFHHostChannelClassification channelClassification;
       
   176 	_LIT_SECURITY_POLICY_PASS(KPassPolicy);
       
   177 	TInt ret = KErrNone;
       
   178 	ret=property.Define(KPropertyUidBluetoothCategory,
       
   179 				        KPropertyKeyBluetoothSetAFHHostChannelClassification,
       
   180 				        RProperty::EByteArray,
       
   181 						KPassPolicy,
       
   182 						KPassPolicy);
       
   183 	if(ret!=KErrNone)
       
   184 			{
       
   185 			return ret;
       
   186 			}
       
   187 	ret = channelClassification.SetChannelRangeBad(3, 10);
       
   188 	if(ret!=KErrNone)
       
   189 		{
       
   190 		return ret;
       
   191 		}
       
   192 	ret = channelClassification.SetChannelBad(12);
       
   193 	if(ret!=KErrNone)
       
   194 		{
       
   195 		return ret;
       
   196 		}
       
   197 	ret = channelClassification.SetChannelBad(15);
       
   198 	if(ret!=KErrNone)
       
   199 		{
       
   200 		return ret;
       
   201 		}
       
   202 	ret = property.Set(KPropertyUidBluetoothCategory,  
       
   203 					   KPropertyKeyBluetoothSetAFHHostChannelClassification, 
       
   204 					   channelClassification);
       
   205 	property.Delete(KPropertyUidBluetoothCategory,
       
   206 			 KPropertyKeyBluetoothSetAFHHostChannelClassification);
       
   207 	return ret;
       
   208 	}
       
   209 
       
   210 TInt CAFHSetter::GetNextChannelNum()
       
   211     {
       
   212 	TKeyCode code;
       
   213 	TBuf8<2> character;
       
   214 	TUint8 byte;
       
   215 	character.SetLength(0);
       
   216 
       
   217 	FOREVER
       
   218 		{
       
   219 		code = iTest.Console()->Getch();
       
   220 		iState &= ~KSetChannelsBad;
       
   221 	
       
   222 		// If <CR> finish editing string
       
   223 		if (code == 0x0d)
       
   224 			{
       
   225 			return KSendHCC;
       
   226 			}
       
   227 
       
   228 		else if (code == 'r' || code == 'R')
       
   229 			{
       
   230 			iTest.Console()->Printf(_L("Taking ranges...\n"));
       
   231 			iState |= KPairs;
       
   232 			iState &= ~KLast;
       
   233 			character.SetLength(0);
       
   234 			}
       
   235 		
       
   236 		else if (code == 's' || code == 'S')
       
   237 			{
       
   238 			iTest.Console()->Printf(_L("Taking singles...\n"));
       
   239 			iState &= ~KPairs;
       
   240 			iState |= KLast;
       
   241 			character.SetLength(0);
       
   242 			}
       
   243 		
       
   244 		// if <BS> remove last character
       
   245 		else if (code == 0x08)
       
   246 			{
       
   247 			character.SetLength(0);
       
   248 			}
       
   249 
       
   250 		else
       
   251 			{
       
   252 			if (character.Length() == 0)
       
   253 				{
       
   254 				character.Append(code);
       
   255 				}
       
   256 			else
       
   257 				{
       
   258 				character.Append(code);
       
   259 				TLex8 lex(character);
       
   260 				character.SetLength(0);
       
   261 				TInt err = lex.Val(byte, EDecimal);
       
   262 				if(!err)
       
   263 					{
       
   264 					iTest.Console()->Printf(_L("%02d"),byte);
       
   265 					if(iState & KLast)
       
   266 						{
       
   267 						iTest.Console()->Printf(_L("\n"));
       
   268 						iChannel2 = byte;
       
   269 						if(iState & KPairs)
       
   270 							{
       
   271 							iState &= ~KLast;
       
   272 							}
       
   273 						iState |= KSetChannelsBad;
       
   274 						}
       
   275 					else
       
   276 						{
       
   277 						iTest.Console()->Printf(_L(", "));
       
   278 						iChannel1 = byte;
       
   279 						iState |= KLast;
       
   280 						}
       
   281 					return KErrNone;
       
   282 					}
       
   283 				else
       
   284 					{
       
   285 					__BTDEBUGGER();
       
   286 					return err;
       
   287 					}
       
   288 				}
       
   289 			}
       
   290 		}
       
   291     }
       
   292 
       
   293 void CAFHSetter::GetClassificationBySettingBadChannels()
       
   294     {
       
   295 	test.Printf(_L("Please enter a sequence of bad channels [0, 78]\n"));
       
   296 	test.Printf(_L("Please type 'r' to CHANGE to enter bad channels as RANGES...\n"));
       
   297 	test.Printf(_L(".... type 's' to CHANGE to enter bad channels SINGLY (default)...\n"));
       
   298 	test.Printf(_L("Press return to finish\n\n"));
       
   299 	
       
   300 	iHCC.Reset();
       
   301 
       
   302 	for(TUint8 i=0; i<KHCIAFHHostChannelClassificationSize; ++i)
       
   303 		{
       
   304 		__ASSERT_DEBUG(iHCC[i] == 0xff, __BTDEBUGGER());
       
   305 		}
       
   306 	
       
   307 	iState = 0;
       
   308 	iState |= KLast;
       
   309 
       
   310 	FOREVER
       
   311 		{
       
   312 		TInt ret = GetNextChannelNum();
       
   313 		if(ret==KSendHCC)
       
   314 			{
       
   315 			return;
       
   316 			}
       
   317 		
       
   318 		else if(ret!=KErrNone)
       
   319 			{
       
   320 			__BTDEBUGGER();
       
   321 			}
       
   322 			
       
   323 		else if(ret==KErrNone && iState&KSetChannelsBad)
       
   324 			{
       
   325 			if(iState & KPairs)
       
   326 				{
       
   327 				ret = iHCC.SetChannelRangeBad(iChannel1, iChannel2);
       
   328 				}
       
   329 			else
       
   330 				{
       
   331 				ret = iHCC.SetChannelBad(iChannel2);
       
   332 				}
       
   333 				
       
   334 			if(ret!=KErrNone)
       
   335 				{
       
   336 				__BTDEBUGGER();
       
   337 				}
       
   338 			}
       
   339 		else
       
   340 			{
       
   341 			__ASSERT_DEBUG(!(iState&KSetChannelsBad), __BTDEBUGGER());
       
   342 			}
       
   343 		}
       
   344     }
       
   345 
       
   346 void CAFHSetter::DoCancel()
       
   347 	{
       
   348 	iTest.Console()->ReadCancel();
       
   349 	}
       
   350 
       
   351 void CAFHSetter::RunL()
       
   352 	{
       
   353 	if(TInt err = iStatus.Int()!=KErrNone)
       
   354 		{
       
   355 		__BTDEBUGGER();
       
   356 		}
       
   357 	TInt ret = iProperty.Set(KPropertyUidBluetoothCategory, KPropertyKeyBluetoothSetAFHHostChannelClassification, iHCC);
       
   358 	__ASSERT_DEBUG(ret==0, __BTDEBUGGER());
       
   359 	//second request
       
   360 	/**/
       
   361 	ret = iProperty.Set(KPropertyUidBluetoothCategory, KPropertyKeyBluetoothSetAFHHostChannelClassification, iHCC);
       
   362 	__ASSERT_DEBUG(ret==0, __BTDEBUGGER());
       
   363 	/**/
       
   364 	test.Printf(_L("\n\nContinue?\n"));
       
   365 	TKeyCode code;
       
   366 	code = iTest.Console()->Getch();
       
   367 	switch(code)
       
   368 		{
       
   369 		case 'n':
       
   370 		case 'N':
       
   371 		case 27:
       
   372 			CActiveScheduler::Stop();
       
   373 			return;
       
   374 		default:
       
   375 			;//continue
       
   376 		}
       
   377 
       
   378 	Start();
       
   379 	}
       
   380 
       
   381 
       
   382 
       
   383 //....
       
   384 TInt E32Main()
       
   385 	{
       
   386 	CTrapCleanup* cleanupStack=CTrapCleanup::New();
       
   387 	CActiveScheduler::Install(new CActiveScheduler);
       
   388 
       
   389 	TRAPD(err,TestL());	//	Ignore err
       
   390 
       
   391 	if (err != KErrNone)
       
   392 		{
       
   393 		test.Printf(_L("Error %d"), err);
       
   394 		test.Getch();
       
   395 		}
       
   396 
       
   397 	delete cleanupStack;
       
   398 	return err;
       
   399 	}