bluetooth/btexample/testui/TBTNotifiers/TBTNotifiers.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 1997-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 // TBTNotifiers app
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32keys.h>
       
    19 #include <coemain.h>
       
    20 #include <eikenv.h>
       
    21 #include <eikdef.h>
       
    22 #include <tbtnotifiers.rsg>
       
    23 #include "TBTNotifiers.hrh"
       
    24 #include "TBTNotifiers.h"
       
    25 #include <btextnotifiers.h>
       
    26 #include <utf.h>
       
    27 #include <btdevice.h>
       
    28 
       
    29 void Panic(TInt aPanic)
       
    30 	{
       
    31 	User::Panic(_L("TBTNotifiers App"),aPanic);
       
    32 	}
       
    33 
       
    34 
       
    35 // Class CTBTNotifiersContainer
       
    36 
       
    37 void CTBTNotifiersContainer::ConstructL(const TRect& aRect)
       
    38 	{
       
    39 	CreateWindowL();
       
    40 	SetBlank();
       
    41 	SetRect(aRect);
       
    42 	ActivateL();
       
    43 	}
       
    44 
       
    45 CTBTNotifiersContainer::~CTBTNotifiersContainer()
       
    46 	{
       
    47 	}
       
    48 
       
    49 void CTBTNotifiersContainer::SizeChanged()
       
    50 	{
       
    51 	}
       
    52 
       
    53 TInt CTBTNotifiersContainer::CountComponentControls() const
       
    54 	{
       
    55 	return 0;
       
    56 	}
       
    57 
       
    58 CCoeControl* CTBTNotifiersContainer::ComponentControl(TInt /*aIndex*/) const
       
    59 	{
       
    60 	return NULL;
       
    61 	}
       
    62 
       
    63 void CTBTNotifiersContainer::Draw(const TRect& /*aRect*/) const
       
    64 	{
       
    65 	}
       
    66 
       
    67 
       
    68 //
       
    69 // CTBTNotifiersAppUi
       
    70 //
       
    71 CTBTNotifiersAppUi::~CTBTNotifiersAppUi()
       
    72 	{
       
    73 	RemoveFromStack(iAppView);
       
    74 	delete iAppView;
       
    75 	}
       
    76 
       
    77 void CTBTNotifiersAppUi::ConstructL()
       
    78 	{
       
    79 	BaseConstructL();
       
    80 	iAppView=new(ELeave) CTBTNotifiersContainer;
       
    81 	iAppView->ConstructL(ClientRect());
       
    82 	AddToStackL(iAppView);
       
    83 	}
       
    84 
       
    85 void CTBTNotifiersAppUi::HandleCommandL(TInt aCommand)
       
    86 	{
       
    87 	switch (aCommand)
       
    88 		{
       
    89 		case EEikCmdExit:
       
    90 			Exit();
       
    91 			break;
       
    92 		case ETBTNotifiersCmdTestPin:
       
    93 			iEikonEnv->InfoMsg(_L("testing pin..."));
       
    94 			TestPinL();
       
    95 			break;
       
    96 		case ETBTNotifiersCmdTestAuth:
       
    97 			iEikonEnv->InfoMsg(_L("testing authorisation..."));
       
    98 			TestAuthorisationL();
       
    99 			break;
       
   100 		case ETBTNotifiersCmdTestDeviceSearch:
       
   101 			iEikonEnv->InfoMsg(_L("testing device search..."));
       
   102 			TestDeviceSearchL();
       
   103 			break;
       
   104 		default:
       
   105 			Panic(0);
       
   106 			break;
       
   107 		}
       
   108 	}
       
   109 
       
   110 void CTBTNotifiersAppUi::TestAuthorisationL()
       
   111 	{
       
   112 	TBTAuthorisationParamsPckg pckg;
       
   113 	pckg().iBDAddr = TInt64(0x12345678);
       
   114 	pckg().iUid.iUid = 0xabcd1234;
       
   115 	TPckgBuf<TBool> resultPckg;
       
   116 
       
   117 	RNotifier notif;
       
   118 	User::LeaveIfError(notif.Connect());
       
   119 	TRequestStatus stat;
       
   120 	notif.StartNotifierAndGetResponse(stat, KBTManAuthNotifierUid, pckg, resultPckg);
       
   121 	User::After(2000000);
       
   122 	//now send device name
       
   123 	TBuf<16> name16 = _L("Tam's Dongle");
       
   124 	TBTNotifierUpdateParamsPckg updpckg;
       
   125 	updpckg().iName = name16;
       
   126 	updpckg().iResult = KErrNone;
       
   127 
       
   128 	TBuf8<1> answer;
       
   129 	TInt err = notif.UpdateNotifier(KBTManAuthNotifierUid, updpckg, answer);
       
   130 	if (err)
       
   131 		{
       
   132 		TBuf<4> error;
       
   133 		error.Zero();
       
   134 		error.AppendNum(err);
       
   135 		CEikonEnv::InfoWinL(_L("Error:"), error);
       
   136 		}
       
   137 	User::WaitForRequest(stat);
       
   138 	notif.CancelNotifier(KBTManAuthNotifierUid);
       
   139 	notif.Close();
       
   140 
       
   141 	TBuf<16> resultbuf;
       
   142 	resultbuf.Zero();
       
   143 	if (resultPckg())
       
   144 		resultbuf.Append(_L("Accepted"));
       
   145 	else
       
   146 		resultbuf.Append(_L("Denied"));
       
   147 	CEikonEnv::InfoWinL(_L("Authorisation Complete:"), resultbuf);
       
   148 	}
       
   149 
       
   150 void CTBTNotifiersAppUi::TestPinL()
       
   151 	{
       
   152 	TBTPinCode pin;
       
   153 	TBTDevAddr addr = TInt64(0x1234);
       
   154 	RNotifier notif;
       
   155 	User::LeaveIfError(notif.Connect());
       
   156 	TRequestStatus stat;
       
   157 	notif.StartNotifierAndGetResponse(stat, KBTManPinNotifierUid, TPckgBuf<TBTDevAddr>(addr), pin);
       
   158 	User::After(2000000);
       
   159 	//now send device name
       
   160 	TBuf<16> name16 = _L("Nick's Dongle");
       
   161 	TBTNotifierUpdateParamsPckg pckg;
       
   162 	pckg().iName = name16;
       
   163 	pckg().iResult = KErrNone;
       
   164 
       
   165 	TBuf8<1> answer;
       
   166 	TInt err = notif.UpdateNotifier(KBTManPinNotifierUid, pckg, answer);
       
   167 	if (err)
       
   168 		{
       
   169 		TBuf<4> error;
       
   170 		error.Zero();
       
   171 		error.AppendNum(err);
       
   172 		CEikonEnv::InfoWinL(_L("Error:"), error);
       
   173 		}
       
   174 	User::WaitForRequest(stat);
       
   175 	notif.CancelNotifier(KBTManPinNotifierUid);
       
   176 	notif.Close();
       
   177 
       
   178 	TBuf<16> buf16;
       
   179 	buf16.Zero();
       
   180 
       
   181 	if (stat.Int() == KErrNone)
       
   182 		{
       
   183 		TBuf8<16> buf8;
       
   184 		buf8.Zero();
       
   185 		for (TInt i=0; i<pin().iLength; i++)
       
   186 			{
       
   187 			buf8.Append(pin().iPIN[i]);
       
   188 			}
       
   189 		CnvUtfConverter::ConvertToUnicodeFromUtf8(buf16, buf8);
       
   190 		}
       
   191 	else
       
   192 		{
       
   193 		buf16.Append(_L("CANCELLED"));
       
   194 		}
       
   195 	CEikonEnv::InfoWinL(_L("Pin Input Complete:"), buf16);
       
   196 	}
       
   197 
       
   198 void CTBTNotifiersAppUi::TestDeviceSearchL()
       
   199 	{
       
   200 	TBTDevAddr addr = TInt64(0x1234);
       
   201 	TBTDeviceResponseParamsPckg pckg;
       
   202 	pckg().SetDeviceAddress(addr);
       
   203 	pckg().SetDeviceName(_L(""));
       
   204 	pckg().SetDeviceClass(TBTDeviceClass());
       
   205 
       
   206 	RNotifier notif;
       
   207 	User::LeaveIfError(notif.Connect());
       
   208 
       
   209 	TRequestStatus stat;
       
   210 	TBuf8<1> dummy;
       
   211 	notif.StartNotifierAndGetResponse(stat, KDeviceSelectionNotifierUid, dummy, pckg);
       
   212 	
       
   213 	User::WaitForRequest(stat);
       
   214 	notif.CancelNotifier(KBTManPinNotifierUid);
       
   215 	notif.Close();
       
   216 
       
   217 	if (stat.Int() == KErrNone)
       
   218 		{
       
   219 		TBuf<14> addressText;
       
   220 		pckg().BDAddr().GetReadable(addressText, _L("0x"), KNullDesC, KNullDesC);
       
   221 		CEikonEnv::InfoWinL(_L("Pin Input Complete:"),addressText);
       
   222 		}
       
   223 	else
       
   224 		{
       
   225 		CEikonEnv::InfoWinL(_L("Pin Input Complete:"),_L("CANCELLED"));
       
   226 		}
       
   227 	}
       
   228 
       
   229 //
       
   230 // CTBTNotifiersEikDocument
       
   231 //
       
   232 
       
   233 CEikAppUi* CTBTNotifiersEikDocument::CreateAppUiL()
       
   234 	{
       
   235 	return(new(ELeave) CTBTNotifiersAppUi);
       
   236 	}
       
   237 
       
   238 void CTBTNotifiersEikDocument::ConstructL()
       
   239 	{
       
   240 	}
       
   241 
       
   242 //
       
   243 // CTBTNotifiersApplication
       
   244 //
       
   245 
       
   246 TUid CTBTNotifiersApplication::AppDllUid() const
       
   247 	{
       
   248 	return KUidTBTNotifiersApp;
       
   249 	}
       
   250 
       
   251 CApaDocument* CTBTNotifiersApplication::CreateDocumentL()
       
   252 	{
       
   253 	CTBTNotifiersEikDocument* document=new(ELeave) CTBTNotifiersEikDocument(*this);
       
   254 	CleanupStack::PushL(document);
       
   255 	document->ConstructL();
       
   256 	CleanupStack::Pop();
       
   257 	return(document);
       
   258 	}
       
   259 
       
   260 CApaApplication* NewApplication()
       
   261 	{
       
   262 	return new CTBTNotifiersApplication;
       
   263 	}
       
   264 
       
   265 
       
   266 #include <eikstart.h>
       
   267 
       
   268 GLDEF_C TInt E32Main()
       
   269 	{
       
   270 	return EikStart::RunApplication(NewApplication);
       
   271 	}