bluetooth/btexample/example/btsocket/tsrc/tclient.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 <e32test.h>
       
    17 
       
    18 #ifdef __EPOC32__
       
    19 #include <c32comm.h>
       
    20 #endif
       
    21 
       
    22 #include "cbtclient.h"
       
    23 
       
    24 class CMyClient: public CBase, MConnectionObserver, MBluetoothSocketNotifier
       
    25 	{
       
    26 	public:
       
    27 		void ConstructL();
       
    28 		~CMyClient();
       
    29 		virtual void HandleNewConnection(CBluetoothSocket* aConnection);
       
    30 		virtual void HandleConnectFailed(TInt aError);
       
    31 		//Virtual functions from MBluetoothSocketNotifier
       
    32 		virtual void HandleConnectCompleteL(TInt aErr);
       
    33 		virtual void HandleAcceptCompleteL(TInt aErr);
       
    34 		virtual void HandleShutdownCompleteL(TInt aErr);
       
    35 		virtual void HandleSendCompleteL(TInt aErr);
       
    36 		virtual void HandleReceiveCompleteL(TInt aErr);
       
    37 		virtual void HandleIoctlCompleteL(TInt aErr);
       
    38 		virtual void HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification);
       
    39 		CBtClient* iBtClient;
       
    40 		RSdp iSdpSession;
       
    41 		RSocketServ iSocketServ;
       
    42 		CBluetoothSocket* iConnection;
       
    43 	};
       
    44 
       
    45 RTest test(_L("tClient"));
       
    46 CMyClient *TheClient;
       
    47 
       
    48 void CMyClient::ConstructL()
       
    49 	{
       
    50 	User::LeaveIfError(iSocketServ.Connect());
       
    51 	User::LeaveIfError(iSdpSession.Connect());
       
    52 	TBTServiceSecurity sec;
       
    53 	sec.SetAuthentication(EMitmNotRequired);
       
    54 	sec.SetAuthorisation(EFalse);
       
    55 	sec.SetEncryption(EFalse);
       
    56 	sec.SetDenied(EFalse);
       
    57 	iBtClient = CBtClient::NewL(TUUID(KSerialPortUUID), //register as a serial port so we can be easily seen
       
    58 								iSocketServ,
       
    59 								*this,
       
    60 								&sec);
       
    61 	iBtClient->ConnectToRemoteDeviceL(*this);
       
    62 	}
       
    63 
       
    64 CMyClient::~CMyClient()
       
    65 	{
       
    66 	delete iBtClient;
       
    67 	delete iConnection;
       
    68 	iSdpSession.Close();
       
    69 	iSocketServ.Close();
       
    70 	}
       
    71 
       
    72 void CMyClient::HandleNewConnection(CBluetoothSocket* aConnection)
       
    73 	{
       
    74 	test(iConnection==NULL);
       
    75 	iConnection = aConnection;
       
    76 	}
       
    77 
       
    78 void CMyClient::HandleConnectFailed(TInt /*aErr*/)
       
    79 	{
       
    80 	test(0);
       
    81 	}
       
    82 
       
    83 _LIT8(KTestData, "tclient 12345678");
       
    84 void CMyClient::HandleConnectCompleteL(TInt aErr)
       
    85 	{
       
    86 	test(aErr == KErrNone);
       
    87 	iConnection->Send(KTestData, 0);
       
    88 	}
       
    89 
       
    90 void CMyClient::HandleAcceptCompleteL(TInt /*aErr*/)
       
    91 	{
       
    92 	test(0);
       
    93 	}
       
    94 
       
    95 void CMyClient::HandleShutdownCompleteL(TInt /*aErr*/)
       
    96 	{
       
    97 	delete iConnection;
       
    98 	iConnection = NULL;
       
    99 	User::After(10000000);
       
   100 	CActiveScheduler::Stop();
       
   101 	}
       
   102 
       
   103 void CMyClient::HandleSendCompleteL(TInt aErr)
       
   104 	{
       
   105 	test(aErr == KErrNone);
       
   106 	iConnection->Shutdown(RSocket::EImmediate);
       
   107 	}
       
   108 
       
   109 void CMyClient::HandleReceiveCompleteL(TInt /*aErr*/)
       
   110 	{
       
   111 	test(0);
       
   112 	}
       
   113 
       
   114 void CMyClient::HandleIoctlCompleteL(TInt /*aErr*/)
       
   115 	{
       
   116 	test(0);
       
   117 	}
       
   118 
       
   119 void CMyClient::HandleActivateBasebandEventNotifierCompleteL(TInt /*aErr*/, TBTBasebandEventNotification& /*aEventNotification*/)
       
   120 	{
       
   121 	test(0);
       
   122 	}
       
   123 
       
   124 void RunTestL()
       
   125 	{
       
   126 	CActiveScheduler *sched = new(ELeave)CActiveScheduler;
       
   127 	CleanupStack::PushL(sched);
       
   128 	CActiveScheduler::Install(sched);
       
   129 	TheClient = new(ELeave) CMyClient;
       
   130 	CleanupStack::PushL(TheClient);
       
   131 	TheClient->ConstructL();
       
   132 	CActiveScheduler::Start();
       
   133 	CleanupStack::PopAndDestroy(TheClient);
       
   134 	CleanupStack::PopAndDestroy(sched);
       
   135 	}
       
   136 
       
   137 #if defined (__WINS__)
       
   138 #define PDD_NAME _L("ECDRV")
       
   139 #define LDD_NAME _L("ECOMM")
       
   140 #else  // __GCC32__
       
   141 #define PDD_NAME _L("EUART1")
       
   142 #define LDD_NAME _L("ECOMM")
       
   143 #endif
       
   144 
       
   145 //this is not needed with a UI, only text shell
       
   146 void LoadLDD_PDD()
       
   147 	{
       
   148 	TInt r;
       
   149 #ifdef __EPOC32__
       
   150 	r=StartC32();
       
   151 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   152 		{
       
   153 		test.Printf(_L("Failed %d!\n\r"),r);
       
   154 		test(r==KErrNone);
       
   155 		}
       
   156 	else
       
   157 		test.Printf(_L("Started C32\n"));
       
   158 #endif
       
   159 	test.Printf(_L("Loading PDD\n"));
       
   160 	r=User::LoadPhysicalDevice(PDD_NAME);
       
   161 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   162 		{
       
   163 		test.Printf(_L("Failed %d!\n\r"),r);
       
   164 		test(r==KErrNone);
       
   165 		}
       
   166 	else 
       
   167 		test.Printf(_L("Loaded LDD\n"));
       
   168 	test.Printf(_L("Loading LDD\n"));
       
   169 	r=User::LoadLogicalDevice(LDD_NAME);
       
   170 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   171 		{
       
   172 		test.Printf(_L("Failed %d!\n\r"),r);
       
   173 		test(r==KErrNone);
       
   174 		}
       
   175 	else
       
   176 		test.Printf(_L("Loaded PDD\n"));
       
   177 	}
       
   178 
       
   179 
       
   180 TInt E32Main()
       
   181 	{
       
   182 	User::SetJustInTime(ETrue);
       
   183 	__UHEAP_MARK;
       
   184 	CTrapCleanup* cleanupStack=CTrapCleanup::New();
       
   185 
       
   186 	LoadLDD_PDD();
       
   187 	TRAPD(err,RunTestL());	//	Ignore err
       
   188 
       
   189 	if (err != KErrNone)
       
   190 		{
       
   191 		test.Printf(_L("Error %d"), err);
       
   192 		test.Getch();
       
   193 		}
       
   194 
       
   195 	test.Close();
       
   196 	delete cleanupStack;
       
   197 	__UHEAP_MARKEND;
       
   198    	
       
   199 	return err;
       
   200 	}
       
   201