bluetooth/btexample/testui/TBTNotifiersText/TBTNotifiersText.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2008-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 "TBTNotifiersText.h"
       
    17 #include <e32base.h>
       
    18 #include <e32std.h>
       
    19 #include <e32cons.h>			// Console
       
    20 
       
    21 #include <btextnotifiers.h>
       
    22 
       
    23 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    24 #include <btextnotifierspartner.h>
       
    25 #endif
       
    26 
       
    27 #include <utf.h>
       
    28 #include <btdevice.h>
       
    29 
       
    30 //  Constants
       
    31 
       
    32 _LIT(KTextConsoleTitle, "Notifiers Test");
       
    33 
       
    34 LOCAL_C void DoStartL ()
       
    35 	{
       
    36 	// Create active scheduler (to run active objects)
       
    37 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
    38 	CleanupStack::PushL (scheduler);
       
    39 	CActiveScheduler::Install (scheduler);
       
    40 	
       
    41 	CBTNotifierConsole* console = CBTNotifierConsole::NewLC();
       
    42 	
       
    43 	CActiveScheduler::Current()->Start();
       
    44 	CleanupStack::PopAndDestroy (console);
       
    45 	
       
    46 	// Delete active scheduler
       
    47 	CleanupStack::PopAndDestroy (scheduler);
       
    48 	}
       
    49 
       
    50 //  Global Functions
       
    51 
       
    52 GLDEF_C TInt E32Main()
       
    53 	{
       
    54 	// Create cleanup stack
       
    55 	__UHEAP_MARK;
       
    56 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    57 
       
    58 	// Run application code inside TRAP harness, wait keypress when terminated
       
    59 	TRAPD(mainError, DoStartL());
       
    60 
       
    61 	delete cleanup;
       
    62 	__UHEAP_MARKEND;
       
    63 	return mainError;
       
    64 	}
       
    65 
       
    66 CBTNotifierConsole* CBTNotifierConsole::NewL()
       
    67 	{
       
    68 	CBTNotifierConsole* self = CBTNotifierConsole::NewLC();
       
    69 	CleanupStack::Pop(self);
       
    70 	return self;
       
    71 	}
       
    72 
       
    73 CBTNotifierConsole* CBTNotifierConsole::NewLC()
       
    74 	{
       
    75 	CBTNotifierConsole* self = new(ELeave) CBTNotifierConsole;
       
    76 	CleanupStack::PushL(self);
       
    77 	self->ConstructL();
       
    78 	return self;
       
    79 	}
       
    80 
       
    81 CBTNotifierConsole::CBTNotifierConsole(): CActive(EPriorityStandard)
       
    82 	{
       
    83 	}
       
    84 
       
    85 CBTNotifierConsole::~CBTNotifierConsole()
       
    86 	{
       
    87 	Cancel();
       
    88 	delete iConsole;
       
    89 	}
       
    90 
       
    91 void CBTNotifierConsole::ConstructL()
       
    92 	{
       
    93 	iConsole = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen));
       
    94 	CActiveScheduler::Add(this);
       
    95 	DrawMenu();
       
    96 	RequestKey();
       
    97 	}
       
    98 
       
    99 
       
   100 void CBTNotifierConsole::RunL()
       
   101 	{
       
   102 	switch (iConsole->KeyCode())
       
   103 		{
       
   104 		case 'a':
       
   105 			TestAuthorisationL();
       
   106 			break;
       
   107 		case 'p':
       
   108 			TestPinL();
       
   109 			break;
       
   110 		case 's':
       
   111 			TestDeviceSearchL();
       
   112 			break;
       
   113 		case 'n':
       
   114 			TestNumericComparisonL();
       
   115 			break;
       
   116 		case 'e':
       
   117 			TestPasskeyL();
       
   118 			break;
       
   119 		case 'l':
       
   120 			TestLegacyPinL();
       
   121 			break;
       
   122 			
       
   123 		case 'x':
       
   124 			CActiveScheduler::Current()->Stop();
       
   125 			break;
       
   126 		default:
       
   127 			break;
       
   128 		}
       
   129 	DrawMenu();
       
   130 	RequestKey();
       
   131 	}
       
   132 
       
   133 void CBTNotifierConsole::DoCancel()
       
   134 	{
       
   135 	iConsole->ReadCancel();
       
   136 	}
       
   137 
       
   138 void CBTNotifierConsole::RequestKey()
       
   139 	{
       
   140 	iConsole->Read(iStatus);
       
   141 	SetActive();
       
   142 	}
       
   143 
       
   144 void CBTNotifierConsole::DrawMenu()
       
   145 	{
       
   146 	iConsole->ClearScreen();
       
   147 	iConsole->Write(_L("Test BT Notifiers\n\n"));
       
   148 	
       
   149 	iConsole->Write(_L("p\tPIN Entry\n"));
       
   150 	iConsole->Write(_L("l\tLegacy PIN Entry\n"));
       
   151 	iConsole->Write(_L("s\tDevice Search\n"));
       
   152 	iConsole->Write(_L("a\tAuthorisation\n"));
       
   153 	iConsole->Write(_L("n\tNumeric Comparison\n"));
       
   154 	iConsole->Write(_L("e\tPasskey Entry\n"));
       
   155 
       
   156 	iConsole->Write(_L("\nx\tExit\n\n"));
       
   157 	}
       
   158 
       
   159 void CBTNotifierConsole::TestAuthorisationL()
       
   160 	{
       
   161 	TBTAuthorisationParamsPckg pckg;
       
   162 	pckg().iBDAddr = TInt64(0x12345678);
       
   163 	pckg().iUid.iUid = 0xabcd1234;
       
   164 	TPckgBuf<TBool> resultPckg;
       
   165 
       
   166 	RNotifier notif;
       
   167 	User::LeaveIfError(notif.Connect());
       
   168 	TRequestStatus stat;
       
   169 	notif.StartNotifierAndGetResponse(stat, KBTManAuthNotifierUid, pckg, resultPckg);
       
   170 	User::After(2000000);
       
   171 	//now send device name
       
   172 	TBuf<16> name16 = _L("Tam's Dongle");
       
   173 	TBTNotifierUpdateParamsPckg updpckg;
       
   174 	updpckg().iName = name16;
       
   175 	updpckg().iResult = KErrNone;
       
   176 
       
   177 	TBuf8<1> answer;
       
   178 	TInt err = notif.UpdateNotifier(KBTManAuthNotifierUid, updpckg, answer);
       
   179 	if (err)
       
   180 		{
       
   181 		TBuf<4> error;
       
   182 		error.Zero();
       
   183 		error.AppendNum(err);
       
   184 		iConsole->Write(_L("Error: "));
       
   185 		iConsole->Write(error);
       
   186 		iConsole->Write(_L("\n"));
       
   187 		}
       
   188 	User::WaitForRequest(stat);
       
   189 	notif.CancelNotifier(KBTManAuthNotifierUid);
       
   190 	notif.Close();
       
   191 	if (resultPckg())
       
   192 		{
       
   193 		iConsole->Write(_L("Authorisation Accepted"));
       
   194 		}
       
   195 	else
       
   196 		{
       
   197 		iConsole->Write(_L("Authorisation Denied"));
       
   198 		}
       
   199 	User::After(2000000);
       
   200 	}
       
   201 
       
   202 void CBTNotifierConsole::TestLegacyPinL()
       
   203 	{
       
   204 	TBTDevAddr devAddr(TInt64(0x1234));
       
   205 	TBTPasskeyNotifierParamsPckg pckg;
       
   206 	pckg().iPasskeyMinLength = 4;
       
   207 	pckg().iLocallyInitiated = ETrue;
       
   208 	
       
   209 	TBTPinCode pin;
       
   210 	
       
   211 	RNotifier notif;
       
   212 	User::LeaveIfError(notif.Connect());
       
   213 	TRequestStatus stat;
       
   214 	notif.StartNotifierAndGetResponse(stat, KBTManPinNotifierUid, pckg, pin);
       
   215 	User::After(2000000);
       
   216 	//now send device name
       
   217 	TBuf<16> name16 = _L("Nick's Dongle");
       
   218 	TBTNotifierUpdateParamsPckg updatePckg;
       
   219 	updatePckg().iName = name16;
       
   220 	updatePckg().iResult = KErrNone;
       
   221 
       
   222 	TBuf8<1> answer;
       
   223 	TInt err = notif.UpdateNotifier(KBTManPinNotifierUid, updatePckg, answer);
       
   224 	if (err)
       
   225 		{
       
   226 		TBuf<4> error;
       
   227 		error.Zero();
       
   228 		error.AppendNum(err);
       
   229 		//CEikonEnv::InfoWinL(_L("Error:"), error);
       
   230 		}
       
   231 	User::WaitForRequest(stat);
       
   232 	notif.CancelNotifier(KBTManPinNotifierUid);
       
   233 	notif.Close();
       
   234 
       
   235 	TBuf<16> buf16;
       
   236 	buf16.Zero();
       
   237 
       
   238 	if (stat.Int() == KErrNone)
       
   239 		{
       
   240 		TBuf8<16> buf8;
       
   241 		buf8.Zero();
       
   242 		for (TInt i=0; i<pin().iLength; i++)
       
   243 			{
       
   244 			buf8.Append(pin().iPIN[i]);
       
   245 			}
       
   246 		CnvUtfConverter::ConvertToUnicodeFromUtf8(buf16, buf8);
       
   247 		}
       
   248 	else
       
   249 		{
       
   250 		buf16.AppendFormat(_L("Error: %d"), stat.Int());
       
   251 		}
       
   252 	iConsole->Write(_L("Pin Input Complete: "));
       
   253 	iConsole->Write(buf16);
       
   254 	User::After(2000000);
       
   255 	}
       
   256 
       
   257 void CBTNotifierConsole::TestDeviceSearchL()
       
   258 	{
       
   259 	TBTDevAddr addr = TInt64(0x1234);
       
   260 	TBTDeviceResponseParamsPckg pckg;
       
   261 	pckg().SetDeviceAddress(addr);
       
   262 	pckg().SetDeviceName(_L(""));
       
   263 	pckg().SetDeviceClass(TBTDeviceClass());
       
   264 
       
   265 	RNotifier notif;
       
   266 	User::LeaveIfError(notif.Connect());
       
   267 
       
   268 	TRequestStatus stat;
       
   269 	TBuf8<1> dummy;
       
   270 	notif.StartNotifierAndGetResponse(stat, KDeviceSelectionNotifierUid, dummy, pckg);
       
   271 	
       
   272 	User::WaitForRequest(stat);
       
   273 	notif.CancelNotifier(KDeviceSelectionNotifierUid);
       
   274 	notif.Close();
       
   275 
       
   276 	if (stat.Int() == KErrNone)
       
   277 		{
       
   278 		TBuf<14> addressText;
       
   279 		pckg().BDAddr().GetReadable(addressText, _L("0x"), KNullDesC, KNullDesC);
       
   280 		iConsole->Write(_L("Device Search Complete: "));
       
   281 		iConsole->Write(addressText);
       
   282 		}
       
   283 	else
       
   284 		{
       
   285 		iConsole->Write(_L("Device search cancelled"));
       
   286 		}
       
   287 	User::After(2000000);
       
   288 	}
       
   289 
       
   290 void CBTNotifierConsole::TestNumericComparisonL()
       
   291 	{
       
   292 	TBTDevAddr devAddr(TInt64(0x12345678));
       
   293 	TBTNumericComparisonParamsPckg pckg;
       
   294 	pckg() = TBTNumericComparisonParams(devAddr, _L("Test Device"), 0x12345678, TBTNumericComparisonParams::ERemoteCanConfirm, ETrue);
       
   295 
       
   296 	TPckgBuf<TBool> resultPckg;
       
   297 	
       
   298 	RNotifier notif;
       
   299 	User::LeaveIfError(notif.Connect());
       
   300 
       
   301 	TRequestStatus stat;
       
   302 	notif.StartNotifierAndGetResponse(stat, KBTNumericComparisonNotifierUid, pckg, resultPckg);
       
   303 	User::WaitForRequest(stat);
       
   304 	notif.CancelNotifier(KBTNumericComparisonNotifierUid);
       
   305 
       
   306 	notif.Close();
       
   307 
       
   308 	if (resultPckg())
       
   309 		{
       
   310 		iConsole->Write(_L("Numeric comparison succeeded"));
       
   311 		}
       
   312 	else
       
   313 		{
       
   314 		iConsole->Write(_L("Numeric comparison failed"));
       
   315 		}
       
   316 	
       
   317 	User::After(2000000);
       
   318 	}
       
   319 
       
   320 void CBTNotifierConsole::TestPasskeyL()
       
   321 	{
       
   322 	TBTDevAddr devAddr(TInt64(0x12345678));
       
   323 	TBTPasskeyDisplayParamsPckg pckg;
       
   324 	pckg() = TBTPasskeyDisplayParams(devAddr, _L("Test Device"), 0x12345678, ETrue);
       
   325 
       
   326 	TPckgBuf<TBool> resultPckg;
       
   327 	
       
   328 	RNotifier notif;
       
   329 	User::LeaveIfError(notif.Connect());
       
   330 
       
   331 	TRequestStatus stat;
       
   332 	notif.StartNotifierAndGetResponse(stat, KBTPasskeyDisplayNotifierUid, pckg, resultPckg);
       
   333 
       
   334 	TBTPasskeyDisplayUpdateParamsPckg updatePckg;
       
   335 	TBuf8<1> answer;
       
   336 
       
   337 	User::After(500000);
       
   338 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyEntryStarted);
       
   339 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   340 
       
   341 	User::After(500000);
       
   342 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   343 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   344 
       
   345 	User::After(500000);
       
   346 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   347 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   348 
       
   349 	User::After(500000);
       
   350 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   351 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   352 
       
   353 	User::After(500000);
       
   354 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitDeleted);
       
   355 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   356 
       
   357 	User::After(500000);
       
   358 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitDeleted);
       
   359 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   360 
       
   361 	User::After(500000);
       
   362 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   363 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   364 
       
   365 	User::After(500000);
       
   366 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyCleared);
       
   367 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   368 	
       
   369 	User::After(500000);
       
   370 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   371 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   372 
       
   373 	User::After(500000);
       
   374 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   375 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   376 
       
   377 	User::After(500000);
       
   378 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyDigitEntered);
       
   379 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   380 
       
   381 	
       
   382 	User::After(500000);
       
   383 	updatePckg() = TBTPasskeyDisplayUpdateParams(EPasskeyEntryCompleted);
       
   384 	notif.UpdateNotifier(KBTPasskeyDisplayNotifierUid, updatePckg, answer);
       
   385 
       
   386 
       
   387 	User::WaitForRequest(stat);
       
   388 	notif.CancelNotifier(KBTPasskeyDisplayNotifierUid);
       
   389 	notif.Close();
       
   390 	
       
   391 	iConsole->Printf(_L("Notifier completed with error %d"), stat.Int());
       
   392 	
       
   393 	User::After(2000000);
       
   394 	}
       
   395 	
       
   396 	
       
   397 void CBTNotifierConsole::TestPinL()
       
   398 	{
       
   399 	TBTPinCode pin;
       
   400 	TBTDevAddr devAddr(TInt64(0x12345678));
       
   401 	
       
   402 	TBTPinCodeEntryNotifierParamsPckg pckg;
       
   403 	pckg() = TBTPinCodeEntryNotifierParams(devAddr, _L("Test Device"), 4, ETrue, ETrue, 4);
       
   404 
       
   405 	RNotifier notif;
       
   406 	User::LeaveIfError(notif.Connect());
       
   407 	CleanupClosePushL(notif);
       
   408 	TRequestStatus stat;
       
   409 	notif.StartNotifierAndGetResponse(stat, KBTPinCodeEntryNotifierUid, pckg, pin);
       
   410 	User::After(2000000);
       
   411 
       
   412 	//now send device name
       
   413 	TBTDeviceNameUpdateParamsPckg updatePckg;
       
   414 	updatePckg() = TBTDeviceNameUpdateParams(_L("New Device Name"), KErrNone);
       
   415 	TBuf8<1> answer;
       
   416 	User::LeaveIfError(notif.UpdateNotifier(KBTPinCodeEntryNotifierUid, updatePckg, answer));
       
   417 	User::WaitForRequest(stat);
       
   418 
       
   419 	notif.CancelNotifier(KBTPinCodeEntryNotifierUid);
       
   420 	CleanupStack::PopAndDestroy(&notif);
       
   421 
       
   422 	TBuf<16> buf16;
       
   423 	buf16.Zero();
       
   424 
       
   425 	if (stat.Int() == KErrNone)
       
   426 		{
       
   427 		TBuf8<16> buf8;
       
   428 		buf8.Zero();
       
   429 		for (TInt i=0; i<pin().iLength; i++)
       
   430 			{
       
   431 			buf8.Append(pin().iPIN[i]);
       
   432 			}
       
   433 		CnvUtfConverter::ConvertToUnicodeFromUtf8(buf16, buf8);
       
   434 		}
       
   435 	else
       
   436 		{
       
   437 		buf16.AppendFormat(_L("Error: %d"), stat.Int());
       
   438 		}
       
   439 	iConsole->Write(_L("Pin Input Complete: "));
       
   440 	iConsole->Write(buf16);
       
   441 	User::After(2000000);
       
   442 	}