bluetooth/btexample/testui/BTTextNotifiers/src/pbapTextNotifiers.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2006-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 <bttypes.h>
       
    17 #include <e32cons.h>
       
    18 #include "BTTextNotifiers.h"
       
    19 
       
    20 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
    21 //define password always returned by auth notifier
       
    22 _LIT(KAutoPassword, "1234");
       
    23 #endif
       
    24 
       
    25 //
       
    26 // PbapTextNotifiersConsole
       
    27 //
       
    28 CConsoleBase* PbapTextNotifiersConsole::AutoSizeNewL(const TDesC& aTitle, TSize aSize)
       
    29 	{
       
    30 	//try to create a console of our preferred size, otherwise we
       
    31 	//guess the screen is too small and use its entirety.
       
    32 	CConsoleBase* console = NULL;
       
    33 	TRAPD(err, console = Console::NewL(aTitle, aSize));
       
    34 	if (err != KErrNone)
       
    35 		{
       
    36 		//if we leave now it is not because of offscreen drawing.
       
    37 		console = Console::NewL(aTitle, TSize(KConsFullScreen, KConsFullScreen));
       
    38 		}
       
    39 	return console;
       
    40 	}
       
    41 
       
    42 //
       
    43 // CPbapAuthNotifier
       
    44 //
       
    45 CPbapAuthNotifier* CPbapAuthNotifier::NewLC()
       
    46 	{
       
    47 	CPbapAuthNotifier* self = new (ELeave) CPbapAuthNotifier();
       
    48 	CleanupStack::PushL(self);
       
    49 	self->ConstructL();
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 CPbapAuthNotifier::CPbapAuthNotifier()
       
    54 	{
       
    55 	}
       
    56 
       
    57 void CPbapAuthNotifier::ConstructL()
       
    58 	{
       
    59 	iEngine = CPbapAuthNotifierEngine::NewL(*this);
       
    60 	}
       
    61 
       
    62 void CPbapAuthNotifier::Release()
       
    63 	{
       
    64 	Cancel();
       
    65 	delete this;
       
    66 	}
       
    67 
       
    68 void CPbapAuthNotifier::Cancel()
       
    69 	{
       
    70 	iEngine->Stop();
       
    71 
       
    72 	if (iNeedToCompleteMessage)
       
    73 		{
       
    74 		iMessage.Complete(KErrCancel);
       
    75 		iNeedToCompleteMessage = EFalse;
       
    76 		}
       
    77 	}
       
    78 
       
    79 CPbapAuthNotifier::~CPbapAuthNotifier()
       
    80 	{
       
    81 	delete iEngine;
       
    82 	}
       
    83 
       
    84 CPbapAuthNotifier::TNotifierInfo CPbapAuthNotifier::RegisterL()
       
    85 	{
       
    86 	iInfo.iUid=KPbapAuthNotifierUid;
       
    87 	iInfo.iChannel=KScreenOutputChannel;
       
    88 	iInfo.iPriority=ENotifierPriorityVHigh;
       
    89 	return iInfo;
       
    90 	}
       
    91 
       
    92 CPbapAuthNotifier::TNotifierInfo CPbapAuthNotifier::Info() const
       
    93 	{
       
    94 	return iInfo;
       
    95 	}
       
    96 
       
    97 TPtrC8 CPbapAuthNotifier::StartL(const TDesC8& /*aBuffer*/)
       
    98 	{
       
    99 	return KNullDesC8();
       
   100 	}
       
   101 
       
   102 void CPbapAuthNotifier::StartL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   103 	{
       
   104 	iReplySlot = aReplySlot;
       
   105 	iMessage = RMessage2(aMessage);
       
   106 	iNeedToCompleteMessage = ETrue;
       
   107 
       
   108 	TRAPD(err, iEngine->StartPasswordEntryL(aBuffer));
       
   109 	if (err)
       
   110 		{
       
   111 		aMessage.Complete(err);
       
   112 		iNeedToCompleteMessage = EFalse;
       
   113 		User::Leave(err);
       
   114 		}
       
   115 	}
       
   116 
       
   117 TPtrC8 CPbapAuthNotifier::UpdateL(const TDesC8& /*aBuffer*/)
       
   118 	{
       
   119 	return KNullDesC8();
       
   120 	}
       
   121 
       
   122 void CPbapAuthNotifier::PasswordInputComplete(const TDesC& aPassword, TInt aReason)
       
   123 	{
       
   124 	if (aReason == KErrNone)
       
   125 		{
       
   126 		TPbapAuthNotifierResponsePckg pckg;
       
   127 		pckg().SetPassword(aPassword);		
       
   128 		TInt err = iMessage.Write(iReplySlot, pckg);
       
   129 		iMessage.Complete(err);
       
   130 		}
       
   131 	else
       
   132 		{
       
   133 		iMessage.Complete(aReason);
       
   134 		}
       
   135 	iNeedToCompleteMessage = EFalse;
       
   136 	}
       
   137 
       
   138 
       
   139 //
       
   140 // CPbapAuthNotifierEngine
       
   141 //
       
   142 CPbapAuthNotifierEngine* CPbapAuthNotifierEngine::NewL(CPbapAuthNotifier& aParent)
       
   143 	{
       
   144 	CPbapAuthNotifierEngine* self = new (ELeave) CPbapAuthNotifierEngine(aParent);
       
   145 	CleanupStack::PushL(self);
       
   146 	self->ConstructL();
       
   147 	CleanupStack::Pop(self);
       
   148 	return self;
       
   149 	}
       
   150 
       
   151 CPbapAuthNotifierEngine::CPbapAuthNotifierEngine(CPbapAuthNotifier& aParent)
       
   152 : CActive(EPriorityStandard), iParent(aParent)
       
   153 	{
       
   154 	CActiveScheduler::Add(this);
       
   155 	}
       
   156 
       
   157 void CPbapAuthNotifierEngine::ConstructL()
       
   158 	{
       
   159 	}
       
   160 
       
   161 void CPbapAuthNotifierEngine::StartPasswordEntryL(const TDesC8& aBuffer)
       
   162 	{
       
   163 	iParamsPckg.Copy(aBuffer);
       
   164 
       
   165 	delete iDevice;
       
   166 	iDevice = NULL;
       
   167 	iDevice = CBTDevice::NewL(iParamsPckg().RemoteAddr());
       
   168 
       
   169 	// get the device name from the registry
       
   170 	User::LeaveIfError(iRegistry.Connect());
       
   171 	User::LeaveIfError(iRegistryView.Open(iRegistry));
       
   172 	iRegistrySearch.FindAddress(iParamsPckg().RemoteAddr());
       
   173 	iRegistryView.CreateView(iRegistrySearch, iStatus);
       
   174 	iRegistryState = EFinding;
       
   175 	SetActive();
       
   176 	}
       
   177 
       
   178 void CPbapAuthNotifierEngine::RunL()
       
   179 	{
       
   180 	if (iStatus == KErrNotFound)
       
   181 		{
       
   182 		// name not found, need to store device address so it can be displayed in UI 
       
   183 		iDevice->SetDeviceAddress(iParamsPckg().RemoteAddr());
       
   184 		iAuthConsole = CPbapAuthNotifierConsole::NewL(this, *iDevice, iParamsPckg().Realm(), iParamsPckg().RealmTruncated());
       
   185 		iAuthConsole->GetPasswordL();
       
   186 		}
       
   187 	else if (iStatus >= KErrNone)
       
   188 		{
       
   189 		switch (iRegistryState)
       
   190 			{
       
   191 			case EFinding:
       
   192 				{
       
   193 				iResponse = CBTRegistryResponse::NewL(iRegistryView);
       
   194 				iResponse->Start(iStatus);
       
   195 				SetActive();
       
   196 				iRegistryState = EGetting;
       
   197 				}
       
   198 				break;
       
   199 			case EGetting:
       
   200 				{
       
   201 				iAuthConsole = CPbapAuthNotifierConsole::NewL(this, *(iResponse->Results()[0]), iParamsPckg().Realm(), iParamsPckg().RealmTruncated());
       
   202 				iAuthConsole->GetPasswordL();
       
   203 				}
       
   204 				break;
       
   205 			}
       
   206 		}
       
   207 	}
       
   208 
       
   209 void CPbapAuthNotifierEngine::DoCancel()
       
   210 	{
       
   211 	iRegistryView.CancelRequest(iStatus);
       
   212 	if (iResponse)
       
   213 		{
       
   214 		iResponse->Cancel();
       
   215 		}
       
   216 	if (iAuthConsole)
       
   217 		{
       
   218 		iAuthConsole->Cancel();
       
   219 		}
       
   220 	}
       
   221 
       
   222 CPbapAuthNotifierEngine::~CPbapAuthNotifierEngine()
       
   223 	{
       
   224 	Cancel();
       
   225 
       
   226 	delete iAuthConsole;
       
   227 	delete iDevice;
       
   228 
       
   229 	iRegistryView.Close();
       
   230 	iRegistry.Close();
       
   231 	}
       
   232 
       
   233 void CPbapAuthNotifierEngine::Stop()
       
   234 	{
       
   235 	Cancel();
       
   236 
       
   237 	if (iAuthConsole)
       
   238 		{
       
   239 		iAuthConsole->Cancel();
       
   240 		delete iAuthConsole;
       
   241 		iAuthConsole = NULL;
       
   242 		}
       
   243 
       
   244 	//clean up device
       
   245 	delete iDevice;
       
   246 	iDevice = NULL;
       
   247 	}
       
   248 
       
   249 void CPbapAuthNotifierEngine::PasswordInputComplete(const TDesC& aPassword, TInt aReason)
       
   250 	{
       
   251 	iParent.PasswordInputComplete(aPassword, aReason);
       
   252 	delete iDevice;
       
   253 	iDevice = NULL;
       
   254 	delete iAuthConsole;
       
   255 	iAuthConsole = NULL;
       
   256 	}
       
   257 
       
   258 
       
   259 //
       
   260 // CPbapAuthNotifierConsole
       
   261 //
       
   262 CPbapAuthNotifierConsole* CPbapAuthNotifierConsole::NewL(CPbapAuthNotifierEngine* aParent, const CBTDevice& aDevice, const TDesC& aRealm, TBool aRealmTruncated)
       
   263 	{
       
   264 	CPbapAuthNotifierConsole* self = new(ELeave) CPbapAuthNotifierConsole(aParent, aRealm, aRealmTruncated);
       
   265 	CleanupStack::PushL(self);
       
   266 	self->ConstructL(aDevice);
       
   267 	CleanupStack::Pop(self);
       
   268 	return self;
       
   269 	}
       
   270 
       
   271 CPbapAuthNotifierConsole::CPbapAuthNotifierConsole(CPbapAuthNotifierEngine* aParent, const TDesC& aRealm, TBool aRealmTruncated)
       
   272 	: CActive(EPriorityStandard), iParent(aParent), iRealm(aRealm), iRealmTruncated(aRealmTruncated)
       
   273 	{
       
   274 	CActiveScheduler::Add(this);
       
   275 	}
       
   276 
       
   277 void CPbapAuthNotifierConsole::ConstructL(const CBTDevice& aDevice)
       
   278 	{
       
   279 	iConsole = PbapTextNotifiersConsole::AutoSizeNewL(_L("PBAP Password input"), TSize(KConsFullScreen,KConsFullScreen));
       
   280 	iDevice = &aDevice;
       
   281 
       
   282 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
   283 	TCallBack autoNotifierCB(AutoNotifierCallBack, this);
       
   284 	iAutoNotifierCallback = new (ELeave)CAsyncCallBack(autoNotifierCB, EPriorityStandard);
       
   285 #endif // __PBAP_TEXT_NOTIFIERS_AUTO__
       
   286 	}
       
   287 
       
   288 CPbapAuthNotifierConsole::~CPbapAuthNotifierConsole()
       
   289 	{
       
   290 	Cancel();
       
   291 	delete iConsole;
       
   292 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
   293 	delete iAutoNotifierCallback;
       
   294 #endif
       
   295 	}
       
   296 
       
   297 void CPbapAuthNotifierConsole::RunL()
       
   298 	{
       
   299 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
   300 	iParent->PasswordInputComplete(KAutoPassword(), KErrNone);
       
   301 #else // __PBAP_TEXT_NOTIFIERS_AUTO__
       
   302 
       
   303 	TKeyCode key = iConsole->KeyCode();
       
   304 	if (key == EKeyEnter)
       
   305 		{
       
   306 		iParent->PasswordInputComplete(iPassword, KErrNone);
       
   307 		}
       
   308 	else if (key == EKeyEscape)
       
   309 		{
       
   310 		iParent->PasswordInputComplete(iPassword, KErrCancel);
       
   311 		}
       
   312 	else
       
   313 		{
       
   314 		if (iPassword.Length() < KPbapAuthPasswordLength)
       
   315 			{
       
   316 			TBuf<1> b;
       
   317 			b.Append(key);
       
   318 			iPassword.Append(b);
       
   319 			iConsole->Printf(b);
       
   320 			IssueRequestL();
       
   321 			}
       
   322 		else
       
   323 			{
       
   324 			iConsole->Printf(_L("\nToo many characters...\nPress any key to continue\n"));
       
   325 			TRequestStatus stat;
       
   326 			iConsole->Read(stat);
       
   327 			User::WaitForRequest(stat);
       
   328 			GetPasswordL();
       
   329 			}
       
   330 		}
       
   331 #endif // __PBAP_TEXT_NOTIFIERS_AUTO__
       
   332 	}
       
   333 
       
   334 void CPbapAuthNotifierConsole::DoCancel()
       
   335 	{
       
   336 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
   337 	iConsole->ReadCancel();
       
   338 #endif
       
   339 	iConsole->Printf(_L("\nPin cancelled.\nPress any key to continue...\n"));
       
   340 	iPassword.Zero();
       
   341 	}
       
   342 
       
   343 void CPbapAuthNotifierConsole::GetPasswordL()
       
   344 	{
       
   345 	iPassword.Zero();
       
   346 	iConsole->ClearScreen();
       
   347 	iConsole->Printf(_L("Please enter password for device:\nAddress: "));
       
   348 	TBTDevAddr a = iDevice->BDAddr();
       
   349 	iConsole->Printf(_L("0x%02x%02x%02x%02x%02x%02x"), a[0], a[1], a[2], a[3], a[4], a[5]);
       
   350 	if (iDevice->IsValidDeviceName())
       
   351 		{
       
   352 		iConsole->Printf(_L("\nName: "));
       
   353 		TBuf<KMaxBluetoothNameLen> dispBuf;	 // BT allows device names upto 248 characters
       
   354 		dispBuf.Copy(iDevice->DeviceName());
       
   355 		iConsole->Printf(dispBuf);
       
   356 		}
       
   357 	
       
   358 	iConsole->Printf(_L("\nRealm: "));
       
   359 	iConsole->Printf(iRealm);
       
   360 	if (iRealmTruncated)
       
   361 		{
       
   362 		iConsole->Printf(_L(">>"));
       
   363 		}
       
   364 		
       
   365 	iConsole->Printf(_L("\nand press [return] to accept or [esc] to cancel\n"));
       
   366 	IssueRequestL();
       
   367 	}
       
   368 
       
   369 void CPbapAuthNotifierConsole::IssueRequestL()
       
   370 	{
       
   371 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
   372 	iAutoNotifierCallback->CallBack();
       
   373 	iStatus = KRequestPending;
       
   374 #else //__PBAP_TEXT_NOTIFIERS_AUTO__
       
   375 	iConsole->Read(iStatus);
       
   376 #endif // __PBAP_TEXT_NOTIFIERS_AUTO__
       
   377 
       
   378 	SetActive();
       
   379 	}
       
   380 
       
   381 #ifdef __PBAP_TEXT_NOTIFIERS_AUTO__
       
   382 /*static*/ TInt CPbapAuthNotifierConsole::AutoNotifierCallBack(TAny *aAuthConsole)
       
   383 	{
       
   384 	CPbapAuthNotifierConsole* authConsole = static_cast<CPbapAuthNotifierConsole*>(aAuthConsole);
       
   385 	if(authConsole->IsActive())
       
   386 		{
       
   387 		TRequestStatus* stat = &(authConsole->iStatus);
       
   388 		User::RequestComplete(stat, KErrNone);
       
   389 		}
       
   390 	return EFalse;
       
   391 	}
       
   392 #endif // __PBAP_TEXT_NOTIFIERS_AUTO__