bluetooth/btexample/example/regui/regui.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 // Example UI for Registry
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "regui.h"
       
    19 
       
    20 
       
    21 GLDEF_D TSize gSmallConsole(75,15);
       
    22 
       
    23 
       
    24 // ----------------------------------------------------------------------------
       
    25 //							Registry UI App
       
    26 // ----------------------------------------------------------------------------
       
    27 
       
    28 CRegUIApp::CRegUIApp()
       
    29 	{
       
    30 	}
       
    31 
       
    32 CRegUIApp::~CRegUIApp()
       
    33 	{
       
    34 	delete iLocalDeviceUser;
       
    35 	iUsers.ResetAndDestroy();
       
    36 	iUsers.Close();
       
    37 	iRegServ.Close();
       
    38 	delete iActiveConsole;
       
    39 	}
       
    40 
       
    41 CRegUIApp* CRegUIApp::NewL()
       
    42 	{
       
    43 	CRegUIApp* thisapp = new (ELeave) CRegUIApp();
       
    44 	CleanupStack::PushL(thisapp);
       
    45 	thisapp->ConstructL();
       
    46 	CleanupStack::Pop();
       
    47 	return thisapp;
       
    48 	}
       
    49 
       
    50 void CRegUIApp::ConstructL()
       
    51 	{
       
    52 	iActiveConsole = CActiveConsole::NewL(*this);
       
    53 	StartRegistryServerL();
       
    54 	}
       
    55 
       
    56 void CRegUIApp::StartRegistryServerL()
       
    57 	{
       
    58 	User::LeaveIfError(iRegServ.Connect());
       
    59 	}
       
    60 
       
    61 void CRegUIApp::StartL()
       
    62 	{
       
    63 	// go ....
       
    64 	TestMenu();
       
    65 	CActiveScheduler::Start();
       
    66 	}
       
    67 
       
    68 void CRegUIApp::Stop()
       
    69 	{
       
    70 	CActiveScheduler::Stop();
       
    71 	}
       
    72 
       
    73 void CRegUIApp::Error(TInt aError)
       
    74 	{
       
    75 	if (aError==KErrServerTerminated)
       
    76 		{
       
    77 		// try to restart server
       
    78 		TRAP(aError, StartRegistryServerL());
       
    79 		if (aError)
       
    80 			{
       
    81 			iActiveConsole->Console().Printf(_L("Failed to restart server. Error: %d\n"), aError);
       
    82 			iActiveConsole->Console().Printf(_L("Stopping App\n"));			
       
    83 			Stop();
       
    84 			}
       
    85 		}
       
    86 	}
       
    87 
       
    88 void CRegUIApp::CreateLocalDeviceL()
       
    89 	{
       
    90 	iLocalDeviceUser = CLocalDeviceUser::NewL(*this, iRegServ);
       
    91 	}
       
    92 
       
    93 void CRegUIApp::CreateViewL(const TBTRegistrySearch& aSearch)
       
    94 	{
       
    95 	CActiveRegistryUser* u = NewRegUserPleaseL();
       
    96 	u->Search(aSearch);
       
    97 	}
       
    98 	
       
    99 CActiveRegistryUser* CRegUIApp::NewRegUserPleaseL()
       
   100 	{
       
   101 	CActiveRegistryUser* u = CActiveRegistryUser::NewL(*this, iRegServ);
       
   102 	CleanupStack::PushL(u);
       
   103 	User::LeaveIfError(iUsers.Append(u));
       
   104 	CleanupStack::Pop(u);
       
   105 	return u;	
       
   106 	}
       
   107 	
       
   108 void CRegUIApp::KindlyRemoveRegUserPleaseL(CActiveRegistryUser* aUser)
       
   109 	{
       
   110 	for (TInt i=0; i<iUsers.Count(); i++)
       
   111 		{
       
   112 		if (iUsers[i]==aUser)
       
   113 			{
       
   114 			delete aUser;
       
   115 			iUsers.Remove(i);
       
   116 			}
       
   117 		}
       
   118 	}
       
   119 	
       
   120 void CRegUIApp::KindlyRemoveLocalDeviceUserPlease()
       
   121 	{
       
   122 	delete iLocalDeviceUser;
       
   123 	iLocalDeviceUser = NULL;
       
   124 	}
       
   125 	
       
   126 void CRegUIApp::TestMenu()
       
   127 	{
       
   128 	CConsoleBase& con = iActiveConsole->Console();
       
   129 	con.Printf(_L("Choose action...\n\n"));
       
   130 
       
   131 	con.Printf(_L("a.\t\tList all devices\n"));
       
   132 	con.Printf(_L("b.\t\tList all bonded devices\n"));
       
   133 	con.Printf(_L("t.\t\tList all trusted devices\n"));	
       
   134 	con.Printf(_L("p.\t\tList all Phones\n"));
       
   135 	con.Printf(_L("c.\t\tList all Computers\n"));
       
   136 	con.Printf(_L("l.\t\tList all Laptops\n"));
       
   137 	con.Printf(_L("v.\t\tList all least sig. cookied devices\n"));
       
   138 	con.Printf(_L("o.\t\tList all added devices\n"));
       
   139 	con.Printf(_L("s.\t\tFind specific device\n"));
       
   140 	con.Printf(_L("0.\t\tClear up completed tasks\n"));
       
   141 	con.Printf(_L("L.\t\tExamine local device info\n"));
       
   142 	con.Printf(_L("\n"));
       
   143 	con.Printf(_L("A.\t\tAdd test device\n"));
       
   144 	con.Printf(_L("Esc.\tStop\n"));
       
   145 	iActiveConsole->RequestKey();
       
   146 	}
       
   147 	
       
   148 
       
   149 void CRegUIApp::KeyPressedL(TChar aKey)	
       
   150 	{
       
   151 	TBTRegistrySearch searchCriteria;
       
   152 	
       
   153 	switch (aKey)
       
   154 		{
       
   155 	case 'a':
       
   156 		{	
       
   157 		iActiveConsole->Console().Printf(_L("Listing all devices...\n"));
       
   158 		searchCriteria.FindAll();
       
   159 		CreateViewL(searchCriteria); 
       
   160 		break;
       
   161 		}
       
   162 
       
   163 	case 'b':
       
   164 		{	
       
   165 		iActiveConsole->Console().Printf(_L("Listing bonded devices...\n"));
       
   166 		searchCriteria.FindBonded();
       
   167 		CreateViewL(searchCriteria); 
       
   168 		break;
       
   169 		}
       
   170 		
       
   171 	case 't':
       
   172 		{	
       
   173 		iActiveConsole->Console().Printf(_L("Listing trusted devices...\n"));
       
   174 		searchCriteria.FindTrusted();
       
   175 		CreateViewL(searchCriteria); 
       
   176 		break;
       
   177 		}
       
   178 		
       
   179 	case 'p':
       
   180 		{
       
   181 		iActiveConsole->Console().Printf(_L("Listing phones...\n"));
       
   182 		TBTDeviceClass phone(0, EMajorDevicePhone, 0);
       
   183 		searchCriteria.FindCoD(phone.DeviceClass(), EMajorDevice);
       
   184 		CreateViewL(searchCriteria);
       
   185 		break;
       
   186 		}
       
   187 
       
   188 	case 'c':
       
   189 		{
       
   190 		iActiveConsole->Console().Printf(_L("Listing computers...\n"));
       
   191 		TBTDeviceClass computer(0, EMajorDeviceComputer, 0);
       
   192 		searchCriteria.FindCoD(computer.DeviceClass(), EMajorDevice);
       
   193 		CreateViewL(searchCriteria);
       
   194 		break;
       
   195 		}
       
   196 	case 'l':
       
   197 		{
       
   198 		iActiveConsole->Console().Printf(_L("Listing laptops...\n"));
       
   199 		TBTDeviceClass laptop(0, EMajorDeviceComputer, EMinorDeviceComputerLaptop);
       
   200 		searchCriteria.FindCoD(laptop.DeviceClass(), static_cast<TBTDeviceClassSearch>(EMajorDevice | EMinorDevice));
       
   201 		CreateViewL(searchCriteria);
       
   202 		break;
       
   203 		}
       
   204 	case 'v':
       
   205 		{
       
   206 		iActiveConsole->Console().Printf(_L("Listing least sig. cookied devices...\n"));
       
   207 		searchCriteria.FindUiCookie(0x00000001, 0x000000001);
       
   208 		CreateViewL(searchCriteria);
       
   209 		break;
       
   210 		}
       
   211 	case 'o':
       
   212 		{
       
   213 		iActiveConsole->Console().Printf(_L("Listing owned devices...\n"));
       
   214 		searchCriteria.FindBonded();
       
   215 		searchCriteria.FindCurrentProcessOwned();
       
   216 		CreateViewL(searchCriteria);
       
   217 		break;
       
   218 		}
       
   219 	case '0':
       
   220 		{
       
   221 		for (TInt i = 0; i<iUsers.Count(); i++)
       
   222 			{
       
   223 			iUsers[i]->DestroyIfDone();
       
   224 			}
       
   225 		break;
       
   226 		}
       
   227 		
       
   228 	case 'L':
       
   229 		{
       
   230 		if(!iLocalDeviceUser)
       
   231 			{
       
   232 			iActiveConsole->Console().Printf(_L("Creating local device window...\n"));
       
   233 			CreateLocalDeviceL();
       
   234 			}
       
   235 		else
       
   236 			{
       
   237 			iActiveConsole->Console().Printf(_L("Already have local device window...\n"));
       
   238 			}
       
   239 		break;
       
   240 		}
       
   241 		
       
   242 	case 'A':
       
   243 		{
       
   244 		iActiveConsole->Console().Printf(_L("Adding test device...\n"));
       
   245 		_LIT8(KTestDeviceAddr, "\x00\x01\x02\x03\x04\x05");
       
   246 		TBTDevAddr addr(KTestDeviceAddr);
       
   247 		CBTDevice* device = CBTDevice::NewLC(addr);
       
   248 		device->SetFriendlyNameL(_L("RegUI Test Device"));
       
   249 		RBTRegistry reg;
       
   250 		User::LeaveIfError(reg.Open(iRegServ));
       
   251 		CleanupClosePushL(reg);
       
   252 		TRequestStatus status;
       
   253 		reg.AddDeviceL(*device, status);
       
   254 		User::WaitForRequest(status);
       
   255 		iActiveConsole->Console().Printf(_L("Result = %d\n"), status.Int());
       
   256 		User::LeaveIfError(status.Int());
       
   257 		CleanupStack::PopAndDestroy(2); // reg, device
       
   258 		break;
       
   259 		}
       
   260 
       
   261 	case 's':
       
   262 		break;
       
   263 
       
   264 	case EKeyEscape:
       
   265 		{
       
   266 		Stop();
       
   267 		return;
       
   268 		}
       
   269 
       
   270 
       
   271 	default:
       
   272 		iActiveConsole->Console().Printf(_L("Unknown command\r\n"));
       
   273 		}
       
   274 		
       
   275 	TestMenu();
       
   276 	}
       
   277 
       
   278 TBTDeviceName CRegUIApp::GetName()
       
   279 	{
       
   280 	// do it sync for now
       
   281 	RTest nameConsole(_L("Get name"));
       
   282 	TBTDeviceName name;
       
   283 	TChar ch;
       
   284 	do
       
   285 		{
       
   286 		ch = nameConsole.Getch();
       
   287 		if (ch==0x8)
       
   288 			{
       
   289 			name.SetLength(name.Length()-1);
       
   290 			}
       
   291 		else if (ch!=0xd) 
       
   292 			{
       
   293 			name.Append(ch);
       
   294 			}
       
   295 		nameConsole.Console()->SetPos(0);
       
   296 		nameConsole.Console()->Printf(name);
       
   297 		} while (ch!=0xd);
       
   298 	nameConsole.Close();
       
   299 	return name;
       
   300 	}
       
   301 
       
   302 // ----------------------------------------------------------------------------
       
   303 //							Active RegUser
       
   304 // ----------------------------------------------------------------------------
       
   305 
       
   306 CActiveRegistryUser::CActiveRegistryUser(CRegUIApp& aApp, RBTRegServ& aRegServ)
       
   307 : CActive(EPriorityStandard), iRegistryServer(aRegServ), iApp(aApp)
       
   308 	{
       
   309 	CActiveScheduler::Add(this);
       
   310 	}
       
   311 
       
   312 CActiveRegistryUser::~CActiveRegistryUser()
       
   313 	{
       
   314 	Cancel();
       
   315 	delete iActiveConsole;
       
   316 	delete iResponseHandler;
       
   317 	delete iViewObserver;
       
   318 	//iChildUser is NOT owned by us
       
   319 	iView.Close();
       
   320 	}
       
   321 	
       
   322 CActiveRegistryUser* CActiveRegistryUser::NewL(CRegUIApp& aApp, RBTRegServ& aRegServ)
       
   323 	{
       
   324 	CActiveRegistryUser* u = new (ELeave) CActiveRegistryUser(aApp, aRegServ);
       
   325 	CleanupStack::PushL(u);
       
   326 	u->ConstructL();
       
   327 	CleanupStack::Pop(u);
       
   328 	return u;
       
   329 	}
       
   330 	
       
   331 void CActiveRegistryUser::ConstructL()
       
   332 	{
       
   333 	iActiveConsole = CActiveConsole::NewL(*this);
       
   334 	User::LeaveIfError(iView.Open(iRegistryServer));
       
   335 	}
       
   336 	
       
   337 void CActiveRegistryUser::DestroyIfDone()
       
   338 	{
       
   339 	if (iState == EDone)
       
   340 		{
       
   341 		iApp.KindlyRemoveRegUserPleaseL(this); //Qualified
       
   342 		}
       
   343 	}
       
   344 
       
   345 void CActiveRegistryUser::KeyPressedL(TChar aKey)
       
   346 	{
       
   347 	// non supported at moment
       
   348 	CConsoleBase& con = iActiveConsole->Console();
       
   349 
       
   350 	// these valid in any state	
       
   351 	if (PreProcessKey(aKey))
       
   352 		return;
       
   353 	
       
   354 	if (iState == ESelecting)
       
   355 		{
       
   356 		if ( (aKey == 'u') || ( aKey == 'U') )
       
   357 			{
       
   358 			// the user typed u or U in order to unpair all devices displayed
       
   359 			UnpairAllDevicesInView();
       
   360 			}
       
   361 		else if ( (aKey == 'd') || ( aKey == 'D') )
       
   362 			{
       
   363 			// the user typed d or D in order to delete all devices displayed
       
   364 			Delete();
       
   365 			}
       
   366 		else
       
   367 			{
       
   368 			// the input was numerical, this is to select one device only from the list	
       
   369 			// just to make this simple, and to show that synchronous can be BAD (can't cancel)
       
   370 			TInt n = static_cast<TInt>(aKey)-49;
       
   371 			
       
   372 			if (n>=0 && n<=iResponseHandler->Results().Count())
       
   373 				{
       
   374 				// create that device as the child
       
   375 				iChildUser = iApp.NewRegUserPleaseL(); //Qualified
       
   376 			
       
   377 				TBTRegistrySearch s;
       
   378 				s.FindAddress(iResponseHandler->Results()[n]->BDAddr());
       
   379 				iChildUser->Search(s);
       
   380 				}
       
   381 			else
       
   382 				{
       
   383 				con.Printf(_L("Invalid device!\n"));
       
   384 				}
       
   385 			}
       
   386 		iActiveConsole->RequestKey();
       
   387 		}
       
   388 	else if (iState == EReady)
       
   389 		{
       
   390 		switch (aKey)
       
   391 			{
       
   392 			case 'a':
       
   393 				RequireAuthorisation(ETrue);
       
   394 				break;
       
   395 			case 'b':
       
   396 				RequireAuthorisation(EFalse);
       
   397 				break;			
       
   398 			case 'u':
       
   399 				Unpair();
       
   400 				break;
       
   401 			case 'd':
       
   402 				Delete();
       
   403 				break;
       
   404 			case 'c':
       
   405 					{
       
   406 					TRAPD(err, SetCookieL())
       
   407 					if(err != KErrNone)
       
   408 						{
       
   409 						iActiveConsole->Console().Printf(_L("Error %d\n"),err);
       
   410 						}
       
   411 					}
       
   412 				break;
       
   413 			case 'n':
       
   414 				con.Printf(_L("Finished with this device\n"));
       
   415 				iState = EDone;
       
   416 				break;
       
   417 			case 'f':
       
   418 				TRAPD(err,ChangeFriendlyNameL());
       
   419 				if (err)
       
   420 					{
       
   421 					iActiveConsole->Console().Printf(_L("Error %d\n"),err);
       
   422 					}
       
   423 				break;
       
   424 			
       
   425 			}
       
   426 			
       
   427 		}
       
   428 	else
       
   429 		{
       
   430 		con.Printf(_L("Not ready\n"));
       
   431 		}
       
   432 	
       
   433 	}
       
   434 
       
   435 
       
   436 TBool CActiveRegistryUser::PreProcessKey(TChar aKey)
       
   437 	{
       
   438 	TBool processed = EFalse;
       
   439 	
       
   440 	switch (aKey)
       
   441 		{
       
   442 		case 'x':
       
   443 		case EKeyEscape:
       
   444 			// ok in any state
       
   445 //			delete this;	// actually should tell app!
       
   446 			processed = ETrue;
       
   447 			break;
       
   448 		default:
       
   449 			break;
       
   450 		}
       
   451 	return processed;
       
   452 	}
       
   453 	
       
   454 void CActiveRegistryUser::Search(const TBTRegistrySearch& aSearch)
       
   455 	{
       
   456 	// keep copy as we go async
       
   457 	iSearch = aSearch;
       
   458 	iView.CreateView(iSearch, iStatus);
       
   459 	iState = ECreatingView;
       
   460 	SetActive();
       
   461 	}
       
   462 
       
   463 void CActiveRegistryUser::Unpair()
       
   464 	{
       
   465 	// unpair the single device in this view
       
   466 	if (!iResponseHandler->Results().Count())
       
   467 		{
       
   468 		iActiveConsole->Console().Printf(_L("whoops!\n"));
       
   469 		}
       
   470 	else
       
   471 		{
       
   472 		iView.UnpairDevice(iResponseHandler->Results()[0]->BDAddr(), iStatus);
       
   473 		iState = EUnpairing;
       
   474 		SetActive();
       
   475 		}
       
   476 	}
       
   477 void CActiveRegistryUser::UnpairAllDevicesInView()
       
   478 	{
       
   479 	// unpair all the devices in this view
       
   480 	if (!iResponseHandler->Results().Count())
       
   481 		{
       
   482 		iActiveConsole->Console().Printf(_L("whoops!\n"));
       
   483 		}
       
   484 	else
       
   485 		{
       
   486 		iView.UnpairAllInView(iStatus);
       
   487 		iState = EUnpairing;
       
   488 		SetActive();
       
   489 		}
       
   490 	}	
       
   491 void CActiveRegistryUser::ChangeFriendlyNameL()
       
   492 	{
       
   493 	if (!iResponseHandler->Results().Count())
       
   494 		{
       
   495 		iActiveConsole->Console().Printf(_L("whoops!\n"));
       
   496 		}
       
   497 	else
       
   498 		{
       
   499 		// havent got around to a full text entry box :o)
       
   500 		TBTDeviceName friendly = iApp.GetName();
       
   501 		iView.ModifyFriendlyDeviceNameL(iResponseHandler->Results()[0]->BDAddr(), friendly, iStatus);
       
   502 		iState = EChangingFriendlyName;
       
   503 		SetActive();
       
   504 		
       
   505 		}
       
   506 	}
       
   507 
       
   508 void CActiveRegistryUser::SetCookieL()
       
   509 	{
       
   510 	if (!iResponseHandler->Results().Count())
       
   511 		{
       
   512 		iActiveConsole->Console().Printf(_L("whoops!\n"));
       
   513 		}
       
   514 	else
       
   515 		{
       
   516 		iDevice = iResponseHandler->Results()[0]->AsNamelessDevice();
       
   517 		iDevice.SetUiCookie(0x00800001); // hardcoded for the moment :)
       
   518 		iState = ESettingCookie;
       
   519 		iView.ModifyDevice(iDevice, iStatus);
       
   520 		SetActive();
       
   521 		}
       
   522 	}
       
   523 		
       
   524 void CActiveRegistryUser::RequireAuthorisation(const TBool aAuthorisationRequired)
       
   525 	{
       
   526 	if (!iResponseHandler->Results().Count())
       
   527 		{
       
   528 		// there is no device listed..
       
   529 		iActiveConsole->Console().Printf(_L("whoops!\n"));
       
   530 		}
       
   531 	else
       
   532 		{
       
   533 		// set the authorisation requirement for the device in this view
       
   534 		TBTNamelessDevice device;
       
   535 		device = iResponseHandler->Results()[0]->AsNamelessDevice();
       
   536 		
       
   537 		TBTDeviceSecurity devSec = device.GlobalSecurity();
       
   538 		devSec.SetNoAuthorise(!aAuthorisationRequired);
       
   539 		device.SetGlobalSecurity(devSec);
       
   540 		
       
   541 		iView.ModifyDevice(device, iStatus);
       
   542 		
       
   543 		iState = EChangingAuthorisation;
       
   544 		SetActive();
       
   545 		}
       
   546 	}
       
   547 	
       
   548 
       
   549 void CActiveRegistryUser::Delete()
       
   550 	{
       
   551 	// choose device and create view with it in...let's do by address
       
   552 	// let user choose device from array
       
   553 	if (!iResponseHandler->Results().Count())
       
   554 		{
       
   555 		iActiveConsole->Console().Printf(_L("whoops!\n"));
       
   556 		}
       
   557 	else
       
   558 		{
       
   559 		iView.DeleteAllInView(iStatus);
       
   560 		iState = EDeleting;
       
   561 		SetActive();
       
   562 		}
       
   563 	}
       
   564 
       
   565 void CActiveRegistryUser::RunL()
       
   566 	{
       
   567 	switch (iState)
       
   568 		{
       
   569 		case ECreatingView:
       
   570 			{
       
   571 			// check for error or num devices
       
   572 			TInt result = iStatus.Int();
       
   573 			if (result == 0 || result == KErrNotFound)
       
   574 				{
       
   575 				iActiveConsole->Console().Printf(_L("No devices found by search"));
       
   576 				iState = EDone;
       
   577 				}
       
   578 			else if(result < 0)
       
   579 				{
       
   580 				iActiveConsole->Console().Printf(_L("Error searching for devices (%d)"), result);
       
   581 				iState = EDone;
       
   582 				}
       
   583 			else
       
   584 				{
       
   585 				if(result == 1)
       
   586 					{
       
   587 					iActiveConsole->Console().Printf(_L("*** 1 device found ***"));
       
   588 					}
       
   589 				else
       
   590 					{
       
   591 					iActiveConsole->Console().Printf(_L("*** %d devices found ***"), result);
       
   592 					}
       
   593 				iResponseHandler = CBTRegistryResponse::NewL(iView);
       
   594 				iResponseHandler->Start(iStatus);
       
   595 				SetActive();
       
   596 				iState = EUsingResponse;
       
   597 				}
       
   598 			break;
       
   599 			}
       
   600 			
       
   601 		case EUsingResponse:
       
   602 			{
       
   603 			RBTDeviceArray& array(iResponseHandler->Results());
       
   604 
       
   605 			// now display them
       
   606 			for (TInt i=0; i<array.Count(); i++)
       
   607 				{
       
   608 				DoDisplayDevice(i, *array[i]);
       
   609 				}
       
   610 			// depending on how many devices we may need to choose
       
   611 			if (array.Count() > 1)
       
   612 				{
       
   613 				ChooseDeviceL();
       
   614 				iState = ESelecting; // actually this stays here now
       
   615 				iViewObserver = CRegistryViewObserver::New(*this, iView);
       
   616 				}
       
   617 			else
       
   618 				{
       
   619 				// go and do stuff with that device
       
   620 				MenuL();
       
   621 				iState = EReady;
       
   622 				iViewObserver = CRegistryViewObserver::New(*this, iView);
       
   623 				}
       
   624 			break;
       
   625 			}			
       
   626 		case EUnpairing:
       
   627 			{
       
   628 			iActiveConsole->Console().Printf(_L("Unpaired\n"));
       
   629 			iState = EDone;
       
   630 			break;
       
   631 			}
       
   632 		case EDeleting:
       
   633 			{
       
   634 			iActiveConsole->Console().Printf(_L("Deleted\n"));
       
   635 			iState = EDone;
       
   636 			break;
       
   637 			}
       
   638 		case EChangingFriendlyName:
       
   639 			{
       
   640 			iActiveConsole->Console().Printf(_L("FriendlyName Changed\n"));
       
   641 			iState = EDone;			
       
   642 			break;
       
   643 			}
       
   644 		case EChangingAuthorisation:
       
   645 			{
       
   646 			iActiveConsole->Console().Printf(_L("Authorisation requirement changed\n"));
       
   647 			iState = EDone;			
       
   648 			break;
       
   649 			}
       
   650 		case ESettingCookie:
       
   651 			{
       
   652 			iActiveConsole->Console().Printf(_L("Set UI Cookie completed\n"));
       
   653 			iState = EDone;
       
   654 			break;
       
   655 			}
       
   656 			
       
   657 		}
       
   658 	}
       
   659 	
       
   660 void CActiveRegistryUser::ChooseDeviceL()
       
   661 	{
       
   662 	CConsoleBase& con = iActiveConsole->Console();
       
   663 	RBTDeviceArray& array = iResponseHandler->Results();
       
   664 	
       
   665 	con.Printf(_L("Enter U/D to Unpair/Delete all in view or Choose device 1-%d \n"), array.Count());
       
   666 	iActiveConsole->RequestKey();
       
   667 	}
       
   668 
       
   669 void CActiveRegistryUser::MenuL()
       
   670 	{
       
   671 	CConsoleBase& con = iActiveConsole->Console();
       
   672 	con.Printf(_L("\nu.\tUnpair\td.\tDelete\na.\tRequire Authorisation\tb.\tNo Authorisation(=Trust)\nf.\tchange Friendly name\tc.\tSet UI Cookie\nn.\tNothing\n"));
       
   673 	iActiveConsole->RequestKey();
       
   674 	}
       
   675 		
       
   676 TInt CActiveRegistryUser::RunError(TInt aError)
       
   677 	{
       
   678 	iActiveConsole->Console().Printf(_L("**RunL left with %d in state %d**\n"), aError, iState);
       
   679 	iApp.Error(aError);
       
   680 	return KErrNone;
       
   681 	}
       
   682 
       
   683 
       
   684 void CActiveRegistryUser::DoCancel()
       
   685 	{
       
   686 	iView.CancelRequest(iStatus);
       
   687 	iActiveConsole->Console().Printf(_L("Cancelled.\n"));
       
   688 	}
       
   689 
       
   690 
       
   691 void CActiveRegistryUser::DoDisplayDevice(TInt aIndex, const CBTDevice& aDevice)
       
   692 /**
       
   693 	Pretty print a device
       
   694 */
       
   695 	{
       
   696 	// if Friendly name then print it
       
   697 	// else if Bluetooth name print it
       
   698 	// else print address
       
   699 	// to emulate phone display only print 20 chars
       
   700 	const TInt KMaxLen = 20;
       
   701 	TBuf<KMaxLen> dispBuf; // could use Ptr (though makes rending BDAddr bit harder)
       
   702 
       
   703 	CConsoleBase& con = iActiveConsole->Console();
       
   704 	con.Printf(_L("\n"));
       
   705 
       
   706 	if (aDevice.IsValidFriendlyName())
       
   707 		dispBuf.Copy(aDevice.FriendlyName().Left(KMaxLen));
       
   708 		
       
   709 	else if (aDevice.IsValidDeviceName())
       
   710 		dispBuf.Copy(aDevice.DeviceName().Left(KMaxLen));
       
   711 	else
       
   712 		aDevice.BDAddr().GetReadable(dispBuf);
       
   713 	
       
   714 	con.Printf(_L("%d: %S "), aIndex+1, &dispBuf);
       
   715 	
       
   716 	// show if bonded
       
   717 	if (aDevice.IsValidPaired() && aDevice.IsPaired())
       
   718 		{
       
   719 		TBuf<40> linkKeyTypeString;
       
   720 		switch(aDevice.LinkKeyType())
       
   721 			{
       
   722 		case ELinkKeyCombination:
       
   723 			linkKeyTypeString = _L("Combination");
       
   724 			break;
       
   725 		case ELinkKeyUnauthenticatedUpgradable:
       
   726 			linkKeyTypeString = _L("Unauthenticated");
       
   727 			break;
       
   728 		case ELinkKeyUnauthenticatedNonUpgradable:
       
   729 			linkKeyTypeString = _L("Unauthenticated");
       
   730 			break;
       
   731 		case ELinkKeyAuthenticated:
       
   732 			linkKeyTypeString = _L("Authenticated");
       
   733 			break;
       
   734 		case ELinkKeyDebug:
       
   735 			linkKeyTypeString = _L("!!!DEBUG!!!");
       
   736 			break;
       
   737 		default:
       
   738 			linkKeyTypeString = _L("Unknown Type");
       
   739 			break;
       
   740 			}
       
   741 		con.Printf(_L("[Bonded: %S]"), &linkKeyTypeString);
       
   742 		}
       
   743 	
       
   744 	con.Printf(_L("\n"));
       
   745 	
       
   746 	if (aDevice.IsValidPassKey())
       
   747 		{	
       
   748 		TBTPinCode passKey = aDevice.PassKey();
       
   749 		con.Printf(_L(" - PassKey length: %d\n"), passKey().iLength);
       
   750 		}
       
   751 		
       
   752 	if (aDevice.IsValidUsed())
       
   753 		{
       
   754 		TBuf<40> dateString;
       
   755 		_LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%H%:1%T%:2%S%:3%");
       
   756 
       
   757 		TRAPD(err, aDevice.Used().FormatL(dateString, KDateString));
       
   758 		if(err == KErrNone)
       
   759 			{
       
   760 			con.Printf(_L(" - Last used: %S\n"), &dateString);
       
   761 			}
       
   762 		}
       
   763 	
       
   764 	if (aDevice.IsValidSeen())
       
   765 		{
       
   766 		TBuf<40> dateString;
       
   767 		_LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%H%:1%T%:2%S%:3%");
       
   768 
       
   769 		TRAPD(err, aDevice.Seen().FormatL(dateString, KDateString));
       
   770 		if(err == KErrNone)
       
   771 			{
       
   772 			con.Printf(_L(" - Last used: %S\n"), &dateString);
       
   773 			}
       
   774 		}
       
   775 		
       
   776 	if (aDevice.IsValidDeviceClass())
       
   777 		{
       
   778 		LogCoD(aDevice.DeviceClass().DeviceClass());
       
   779 		}
       
   780 	
       
   781 	if (aDevice.IsValidUiCookie())
       
   782 		{
       
   783 		con.Printf(_L(" - UI Cookie: 0x%08x\n"), aDevice.UiCookie());
       
   784 		}
       
   785 	}
       
   786 
       
   787 void CActiveRegistryUser::LogCoD(TUint aCoD)
       
   788 	{
       
   789 	class TCoDServiceClass
       
   790 		{
       
   791 		public:
       
   792 		TCoDServiceClass(TInt aBit, const TDesC& aName) {iBit = aBit, iName = aName;};
       
   793 		TBool Supported(TUint aBitMask)
       
   794 			{
       
   795 			TInt mask = 1 << iBit;
       
   796 			return (aBitMask & mask);
       
   797 			};
       
   798 		TInt iBit;
       
   799 		TBuf <32> iName;
       
   800 		};
       
   801 
       
   802 	TBuf<20> majordeviceclassname;
       
   803 	TBuf<30> minordeviceclassname;
       
   804 
       
   805 	TBTDeviceClass cod(aCoD);
       
   806 	
       
   807 	TUint8 majordeviceclass = cod.MajorDeviceClass();
       
   808 	TUint8 minordeviceclass = cod.MinorDeviceClass();
       
   809 
       
   810 	switch(majordeviceclass)
       
   811 		{
       
   812 		case 0:
       
   813 			majordeviceclassname=_L("Miscellaneous");
       
   814 			break;
       
   815 		case 1:
       
   816 			majordeviceclassname=_L("Computer");
       
   817 			switch(minordeviceclass)
       
   818 				{
       
   819 				case 0:
       
   820 					minordeviceclassname=_L("Unspecified");
       
   821 					break;
       
   822 				case 1:
       
   823 					minordeviceclassname=_L("Desktop workstation");
       
   824 					break;
       
   825 				case 2:
       
   826 					minordeviceclassname=_L("Server");
       
   827 					break;
       
   828 				case 3:
       
   829 					minordeviceclassname=_L("Laptop");
       
   830 					break;
       
   831 				case 4:
       
   832 					minordeviceclassname=_L("HPC/PDA (clamshell)");
       
   833 					break;
       
   834 				case 5:
       
   835 					minordeviceclassname=_L("Palm sized PC/PDA");
       
   836 					break;
       
   837 				default:
       
   838 					minordeviceclassname=_L("UNKNOWN");
       
   839 					break;
       
   840 				}
       
   841 			break;
       
   842 		case 2:
       
   843 			majordeviceclassname=_L("Phone");
       
   844 			switch(minordeviceclass)
       
   845 				{
       
   846 				case 0:
       
   847 					minordeviceclassname=_L("Unclassified");
       
   848 					break;
       
   849 				case 1:
       
   850 					minordeviceclassname=_L("Cellular");
       
   851 					break;
       
   852 				case 2:
       
   853 					minordeviceclassname=_L("Cordless");
       
   854 					break;
       
   855 				case 3:
       
   856 					minordeviceclassname=_L("Smartphone");
       
   857 					break;
       
   858 				case 4:
       
   859 					minordeviceclassname=_L("Wired modem/voice gateway");
       
   860 					break;
       
   861 				default:
       
   862 					minordeviceclassname=_L("UNKNOWN");
       
   863 					break;
       
   864 				}
       
   865 			break;
       
   866 		case 3:
       
   867 			majordeviceclassname=_L("LAN Access Point");
       
   868 			switch(minordeviceclass)
       
   869 				{
       
   870 				case 0:
       
   871 					minordeviceclassname=_L("Fully available");
       
   872 					break;
       
   873 				case 1:
       
   874 					minordeviceclassname=_L("1-17% utilised");
       
   875 					break;
       
   876 				case 2:
       
   877 					minordeviceclassname=_L("17-33% utilised");
       
   878 					break;
       
   879 				case 3:
       
   880 					minordeviceclassname=_L("33-50% utilised");
       
   881 					break;
       
   882 				case 4:
       
   883 					minordeviceclassname=_L("50-67% utilised");
       
   884 					break;
       
   885 				case 5:
       
   886 					minordeviceclassname=_L("67-83% utilised");
       
   887 					break;
       
   888 				case 6:
       
   889 					minordeviceclassname=_L("83-99% utilised");
       
   890 					break;
       
   891 				case 7:
       
   892 					minordeviceclassname=_L("No service available");
       
   893 					break;
       
   894 
       
   895 				}
       
   896 			break;
       
   897 		case 4:
       
   898 			majordeviceclassname=_L("Audio");
       
   899 			break;
       
   900 		case 5:
       
   901 			majordeviceclassname=_L("Peripheral");
       
   902 			break;
       
   903 		case 32:
       
   904 			majordeviceclassname=_L("Unspecified");
       
   905 			break;
       
   906 		default:
       
   907 			majordeviceclassname=_L("UNKNOWN");
       
   908 		};
       
   909 
       
   910 	TCoDServiceClass serviceclasses[]=
       
   911 		{
       
   912 		TCoDServiceClass(13, _L("LIAC Response")),
       
   913 		TCoDServiceClass(14, _L("-")),
       
   914 		TCoDServiceClass(15, _L("-")),
       
   915 		TCoDServiceClass(16, _L("-")),
       
   916 
       
   917 		TCoDServiceClass(17, _L("Networking")),
       
   918 		TCoDServiceClass(18, _L("Rendering")),
       
   919 		TCoDServiceClass(19, _L("Capturing")),
       
   920 		TCoDServiceClass(20, _L("ObjectTransfer")),
       
   921 		TCoDServiceClass(21, _L("Audio")),
       
   922 		TCoDServiceClass(22, _L("Telephony")),
       
   923 		TCoDServiceClass(23, _L("Information"))
       
   924 		};
       
   925 
       
   926 
       
   927 	TInt numserviceclasses = sizeof(serviceclasses)/sizeof(TCoDServiceClass);
       
   928 	
       
   929 	TBuf<100> output;
       
   930 
       
   931 /*	output.Format(_L("\t%S [0x%04x] | %S [0x%04x] (0x%08x)\n"),
       
   932 							&majordeviceclassname,
       
   933 							majordeviceclass,
       
   934 							&minordeviceclassname,
       
   935 							minordeviceclass,
       
   936 							aCoD);
       
   937 */
       
   938 	output.Format(_L(" - CoD: %S | %S\n"),
       
   939 					&majordeviceclassname,
       
   940 					&minordeviceclassname);
       
   941 
       
   942 	iActiveConsole->Console().Printf(output);
       
   943 	iActiveConsole->Console().Printf(_L(" - Services: "));
       
   944 	
       
   945 	for (TInt i = 0; i<numserviceclasses; i++)
       
   946 		{
       
   947 		if (serviceclasses[i].Supported(aCoD))
       
   948 			{
       
   949 			output.Format(_L("%S "), &serviceclasses[i].iName);
       
   950 			iActiveConsole->Console().Printf(output);
       
   951 			}
       
   952 		}
       
   953 	iActiveConsole->Console().Printf(_L("\n"));
       
   954 	}
       
   955 
       
   956 void CActiveRegistryUser::ViewChanged()
       
   957 	{
       
   958 	iActiveConsole->Console().Printf(_L("--Our view has changed!\n"));
       
   959 	}
       
   960 
       
   961 // ----------------------------------------------------------------------------
       
   962 //							Local Device User
       
   963 // ----------------------------------------------------------------------------
       
   964 
       
   965 CLocalDeviceUser::CLocalDeviceUser(CRegUIApp& aApp, RBTRegServ& aRegServ)
       
   966 : iRegistryServer(aRegServ), iApp(aApp)
       
   967 	{
       
   968 	}
       
   969 
       
   970 CLocalDeviceUser::~CLocalDeviceUser()
       
   971 	{
       
   972 	delete iActiveConsole;
       
   973 	iLocalDevice.Close();
       
   974 	}
       
   975 	
       
   976 CLocalDeviceUser* CLocalDeviceUser::NewL(CRegUIApp& aApp, RBTRegServ& aRegServ)
       
   977 	{
       
   978 	CLocalDeviceUser* u = new (ELeave) CLocalDeviceUser(aApp, aRegServ);
       
   979 	CleanupStack::PushL(u);
       
   980 	u->ConstructL();
       
   981 	CleanupStack::Pop(u);
       
   982 	return u;
       
   983 	}
       
   984 	
       
   985 void CLocalDeviceUser::ConstructL()
       
   986 	{
       
   987 	iActiveConsole = CActiveConsole::NewL(*this);
       
   988 	User::LeaveIfError(iLocalDevice.Open(iRegistryServer));
       
   989 
       
   990 	MenuL();
       
   991 	}
       
   992 	
       
   993 void CLocalDeviceUser::KeyPressedL(TChar aKey)
       
   994 	{
       
   995 	// non supported at moment
       
   996 	CConsoleBase& con = iActiveConsole->Console();
       
   997 	
       
   998 	switch (aKey)
       
   999 		{
       
  1000 		case 's':
       
  1001 			{
       
  1002 			TInt err = iLocalDevice.Get(iLocalDeviceSettings);
       
  1003 			if(!err)
       
  1004 				{
       
  1005 				con.Printf(_L("Scan Setting = 0x%.2x\n"), iLocalDeviceSettings.ScanEnable());
       
  1006 				}
       
  1007 			else
       
  1008 				{
       
  1009 				con.Printf(_L("Couldn't get settings\n"));
       
  1010 				}
       
  1011 			
       
  1012 			iActiveConsole->RequestKey();
       
  1013 			break;
       
  1014 			}
       
  1015 		case 'x':
       
  1016 		case EKeyEscape:
       
  1017 			{
       
  1018 			iApp.KindlyRemoveLocalDeviceUserPlease();
       
  1019 			break;
       
  1020 			}
       
  1021 		default:
       
  1022 			{
       
  1023 			con.Printf(_L("Not Supported\n"));
       
  1024 			iActiveConsole->RequestKey();
       
  1025 			break;
       
  1026 			}
       
  1027 		};
       
  1028 	}
       
  1029 
       
  1030 void CLocalDeviceUser::MenuL()
       
  1031 	{
       
  1032 	CConsoleBase& con = iActiveConsole->Console();
       
  1033 	con.Printf(_L("\ns.\tGet Scan Setting\td.\tDelete\n"));
       
  1034 	iActiveConsole->RequestKey();
       
  1035 	}
       
  1036 
       
  1037 CRegistryViewObserver* CRegistryViewObserver::New(MViewNotify& aNotifiee, RBTRegistry& aView)
       
  1038 	{
       
  1039 	CRegistryViewObserver* self = new CRegistryViewObserver(aNotifiee, aView);
       
  1040 	return self;
       
  1041 	}
       
  1042 
       
  1043 CRegistryViewObserver::CRegistryViewObserver(MViewNotify& aNotifiee, RBTRegistry& aView)
       
  1044 	: CActive(EPriorityStandard), iNotifiee(aNotifiee), iView(aView)
       
  1045 	{
       
  1046 	CActiveScheduler::Add(this);
       
  1047 	Queue();
       
  1048 	}
       
  1049 
       
  1050 CRegistryViewObserver::~CRegistryViewObserver()
       
  1051 	{
       
  1052 	Cancel();
       
  1053 	}
       
  1054 
       
  1055 void CRegistryViewObserver::RunL()
       
  1056 	{
       
  1057 	if (iStatus == KErrNone)
       
  1058 		{
       
  1059 		iNotifiee.ViewChanged();
       
  1060 		// Requeue request (view remains open)
       
  1061 		Queue();
       
  1062 		}
       
  1063 	else if (iStatus == KErrCompletion)
       
  1064 		{
       
  1065 		// The view is closing - count as a change but dont requeue request.
       
  1066 		iNotifiee.ViewChanged();
       
  1067 		}
       
  1068 	}
       
  1069 
       
  1070 void CRegistryViewObserver::Queue()
       
  1071 	{
       
  1072 	iView.NotifyViewChange(iStatus);
       
  1073 	SetActive();
       
  1074 	}
       
  1075 
       
  1076 void CRegistryViewObserver::DoCancel()
       
  1077 	{
       
  1078 	iView.CancelRequest(iStatus);
       
  1079 	}
       
  1080 
       
  1081 
       
  1082 // ----------------------------------------------------------------------------
       
  1083 //							Active Console
       
  1084 // ----------------------------------------------------------------------------
       
  1085 
       
  1086 
       
  1087 CActiveConsole::CActiveConsole(MActiveConsoleNotify& aNotify)
       
  1088 : CActive(EPriorityStandard), iNotify(aNotify)
       
  1089 	{
       
  1090 	CActiveScheduler::Add(this);
       
  1091 	}
       
  1092 
       
  1093 
       
  1094 CActiveConsole::~CActiveConsole()
       
  1095 	{
       
  1096 	Cancel();
       
  1097 	delete iConsole;
       
  1098 	}
       
  1099 
       
  1100 CActiveConsole* CActiveConsole::NewL(MActiveConsoleNotify& aNotify)
       
  1101 	{
       
  1102 	CActiveConsole* console = new (ELeave) CActiveConsole(aNotify);
       
  1103 	CleanupStack::PushL(console);
       
  1104 	console->ConstructL();
       
  1105 	CleanupStack::Pop();
       
  1106 	return console;
       
  1107 	}
       
  1108 
       
  1109 CConsoleBase* CActiveConsole::AutoSizeNewL(const TDesC& aTitle, TSize aSize)
       
  1110 	{
       
  1111 	// Try to create a console of our preferred size, otherwise we
       
  1112 	// guess the screen is too small and use its entirety.
       
  1113 	CConsoleBase* console = NULL;
       
  1114 	TRAPD(err, console = Console::NewL(aTitle, aSize));
       
  1115 	if (err != KErrNone)
       
  1116 		{
       
  1117 		// If we leave now it is not because of offscreen drawing.
       
  1118 		console = Console::NewL(aTitle, TSize(KConsFullScreen, KConsFullScreen));
       
  1119 		}
       
  1120 	return console;
       
  1121 	}
       
  1122 
       
  1123 void CActiveConsole::ConstructL()
       
  1124 	{
       
  1125 	iConsole = AutoSizeNewL(_L("RegUI Subsession"), gSmallConsole);
       
  1126 	}
       
  1127 
       
  1128 void CActiveConsole::DoCancel()
       
  1129 	{
       
  1130 	iConsole->ReadCancel();
       
  1131 	}
       
  1132 
       
  1133 void CActiveConsole::RequestKey()
       
  1134 	{
       
  1135 	DrawCursor();
       
  1136 	iConsole->Read(iStatus);
       
  1137 	SetActive();
       
  1138 	}
       
  1139 
       
  1140 void CActiveConsole::DrawCursor()
       
  1141 	{
       
  1142 	iConsole->SetPos(0);
       
  1143 	iConsole->Printf(_L(">>"));
       
  1144 	}
       
  1145 
       
  1146 void CActiveConsole::RunL()
       
  1147 	{
       
  1148 	// key has been pressed
       
  1149 	TChar ch = iConsole->KeyCode();
       
  1150 	iNotify.KeyPressedL(ch);
       
  1151 	}
       
  1152 
       
  1153 TInt CActiveConsole::RunError(TInt aError)
       
  1154 	{
       
  1155 	iConsole->Printf(_L("**Active console RunL left with %d**\n"), aError);
       
  1156 	return KErrNone;
       
  1157 	}
       
  1158 
       
  1159 
       
  1160 void StartL()
       
  1161 	{
       
  1162 	CRegUIApp* app = CRegUIApp::NewL();
       
  1163 	app->StartL();
       
  1164 
       
  1165 	delete app;
       
  1166 	//finished
       
  1167 	}
       
  1168 
       
  1169 TInt E32Main()
       
  1170 	{
       
  1171 	__UHEAP_MARK;
       
  1172 	CTrapCleanup* cleanupStack=CTrapCleanup::New();	// Get CleanupStack
       
  1173 
       
  1174 	CActiveScheduler* activescheduler=new CActiveScheduler;
       
  1175 	CActiveScheduler::Install(activescheduler);
       
  1176 
       
  1177 	TRAPD(err, StartL());
       
  1178 
       
  1179 	delete activescheduler;
       
  1180 	delete cleanupStack;
       
  1181 	__UHEAP_MARKEND;
       
  1182 
       
  1183 	return err;
       
  1184 	}
       
  1185 
       
  1186