bluetooth/btexample/example/btsocket/tsrc/tservice.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 "cbtservice.h"
       
    23 
       
    24 class CMySCOService;
       
    25 class CMyService: public CBase, MConnectionObserver, MBluetoothSocketNotifier
       
    26 	{
       
    27 	public:
       
    28 		void ConstructL();
       
    29 		~CMyService();
       
    30 		virtual void HandleNewConnection(CBluetoothSocket* aConnection);
       
    31 		virtual void HandleConnectFailed(TInt aError);
       
    32 		//Virtual functions from MBluetoothSocketNotifier
       
    33 		virtual void HandleConnectCompleteL(TInt aErr);
       
    34 		virtual void HandleAcceptCompleteL(TInt aErr);
       
    35 		virtual void HandleShutdownCompleteL(TInt aErr);
       
    36 		virtual void HandleSendCompleteL(TInt aErr);
       
    37 		virtual void HandleReceiveCompleteL(TInt aErr);
       
    38 		virtual void HandleIoctlCompleteL(TInt aErr);
       
    39 		virtual void HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification);
       
    40 		CBtService* iBtService;
       
    41 		RSdp iSdpSession;
       
    42 		RSocketServ iSocketServ;
       
    43 		CBluetoothSocket* iConnection;
       
    44 		CMySCOService* iSCOConnection;
       
    45 	};
       
    46 
       
    47 class CMySCOService: MBluetoothSynchronousLinkNotifier
       
    48 	{
       
    49 	public:
       
    50 		void ConstructL();
       
    51 		CMySCOService(RSocketServ& aSocketServ);
       
    52 		~CMySCOService();
       
    53 		//virtual functions from MBluetoothSynchronousLinkNotifier
       
    54 		virtual void HandleSetupConnectionCompleteL(TInt aErr);
       
    55 		virtual void HandleDisconnectionCompleteL(TInt aErr);
       
    56 		virtual void HandleAcceptConnectionCompleteL(TInt aErr);
       
    57 		virtual void HandleSendCompleteL(TInt aErr);
       
    58 		virtual void HandleReceiveCompleteL(TInt aErr);
       
    59 		CBluetoothSynchronousLink *iConnection;
       
    60 		RSocketServ& iSocketServ;
       
    61 	};
       
    62 
       
    63 RTest test(_L("tservice"));
       
    64 CMyService *TheService;
       
    65 
       
    66 void CMyService::ConstructL()
       
    67 	{
       
    68 	User::LeaveIfError(iSocketServ.Connect());
       
    69 	iSCOConnection = new(ELeave) CMySCOService(iSocketServ);
       
    70 	iSCOConnection->ConstructL();
       
    71 	User::LeaveIfError(iSdpSession.Connect());
       
    72 	TBTServiceSecurity sec;
       
    73 	sec.SetAuthentication(EMitmNotRequired);
       
    74 	sec.SetAuthorisation(EFalse);
       
    75 	sec.SetEncryption(EFalse);
       
    76 	sec.SetDenied(EFalse);
       
    77 	iBtService = CBtService::NewL(TUUID(KSerialPortUUID), //register as a serial port so we can be easily seen
       
    78 								iSdpSession,
       
    79 								iSocketServ,
       
    80 								*this,
       
    81 								KRFCOMM,
       
    82 								&sec);
       
    83 	iConnection = CBluetoothSocket::NewL(*this, iSocketServ);
       
    84 	iBtService->AcceptConnection(*iConnection);
       
    85 	}
       
    86 
       
    87 CMyService::~CMyService()
       
    88 	{
       
    89 	delete iBtService;
       
    90 	delete iConnection;
       
    91 	delete iSCOConnection;
       
    92 	iSdpSession.Close();
       
    93 	iSocketServ.Close();
       
    94 	}
       
    95 
       
    96 void CMyService::HandleNewConnection(CBluetoothSocket* aConnection)
       
    97 	{
       
    98 	test(aConnection==iConnection);
       
    99 	delete iConnection;
       
   100 	iConnection = NULL;
       
   101 	CActiveScheduler::Stop();
       
   102 	}
       
   103 
       
   104 void CMyService::HandleConnectFailed(TInt /*aErr*/)
       
   105 	{
       
   106 	test(0);
       
   107 	}
       
   108 
       
   109 void CMyService::HandleConnectCompleteL(TInt /*aErr*/)
       
   110 	{
       
   111 	test(0);
       
   112 	}
       
   113 
       
   114 void CMyService::HandleAcceptCompleteL(TInt /*aErr*/)
       
   115 	{
       
   116 	test(0);
       
   117 	}
       
   118 
       
   119 void CMyService::HandleShutdownCompleteL(TInt /*aErr*/)
       
   120 	{
       
   121 	test(0);
       
   122 	}
       
   123 
       
   124 void CMyService::HandleSendCompleteL(TInt /*aErr*/)
       
   125 	{
       
   126 	test(0);
       
   127 	}
       
   128 
       
   129 void CMyService::HandleReceiveCompleteL(TInt /*aErr*/)
       
   130 	{
       
   131 	test(0);
       
   132 	}
       
   133 
       
   134 void CMyService::HandleIoctlCompleteL(TInt /*aErr*/)
       
   135 	{
       
   136 	test(0);
       
   137 	}
       
   138 
       
   139 void CMyService::HandleActivateBasebandEventNotifierCompleteL(TInt /*aErr*/, TBTBasebandEventNotification& /*aEventNotification*/)
       
   140 	{
       
   141 	test(0);
       
   142 	}
       
   143 
       
   144 CMySCOService::CMySCOService(RSocketServ& aSS) :
       
   145 	iSocketServ(aSS)
       
   146 	{
       
   147 	}
       
   148 
       
   149 CMySCOService::~CMySCOService()
       
   150 	{
       
   151 	delete iConnection;
       
   152 	}
       
   153 
       
   154 void CMySCOService::ConstructL()
       
   155 	{
       
   156 	iConnection = CBluetoothSynchronousLink::NewL(*this, iSocketServ);
       
   157 	iConnection->AcceptConnection();
       
   158 	}
       
   159 
       
   160 void CMySCOService::HandleSetupConnectionCompleteL(TInt /*aErr*/)
       
   161 	{
       
   162 	test(0);
       
   163 	}
       
   164 void CMySCOService::HandleDisconnectionCompleteL(TInt /*aErr*/)
       
   165 	{
       
   166 	}
       
   167 void CMySCOService::HandleAcceptConnectionCompleteL(TInt aErr)
       
   168 	{
       
   169 	test(aErr == KErrNone);
       
   170 	}
       
   171 void CMySCOService::HandleSendCompleteL(TInt /*aErr*/)
       
   172 	{
       
   173 	test(0);
       
   174 	}
       
   175 void CMySCOService::HandleReceiveCompleteL(TInt /*aErr*/)
       
   176 	{
       
   177 	test(0);
       
   178 	}
       
   179 
       
   180 
       
   181 void RunTestL()
       
   182 	{
       
   183 	CActiveScheduler *sched = new(ELeave)CActiveScheduler;
       
   184 	CleanupStack::PushL(sched);
       
   185 	CActiveScheduler::Install(sched);
       
   186 	TheService = new(ELeave) CMyService;
       
   187 	CleanupStack::PushL(TheService);
       
   188 	TheService->ConstructL();
       
   189 	CActiveScheduler::Start();
       
   190 	CleanupStack::PopAndDestroy(TheService);
       
   191 	CleanupStack::PopAndDestroy(sched);
       
   192 	}
       
   193 
       
   194 #if defined (__WINS__)
       
   195 #define PDD_NAME _L("ECDRV")
       
   196 #define LDD_NAME _L("ECOMM")
       
   197 #else  // __GCC32__
       
   198 #define PDD_NAME _L("EUART1")
       
   199 #define LDD_NAME _L("ECOMM")
       
   200 // #define ETNA_PDD_NAME _L("EUART2") // for debugging over com2
       
   201 #endif
       
   202 
       
   203 //this is not needed with a UI, only text shell
       
   204 void LoadLDD_PDD()
       
   205 	{
       
   206 	TInt r;
       
   207 #ifdef __EPOC32__
       
   208 	r=StartC32();
       
   209 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   210 		{
       
   211 		test.Printf(_L("Failed %d!\n\r"),r);
       
   212 		test(r==KErrNone);
       
   213 		}
       
   214 	else
       
   215 		test.Printf(_L("Started C32\n"));
       
   216 #endif
       
   217 	test.Printf(_L("Loading PDD\n"));
       
   218 	r=User::LoadPhysicalDevice(PDD_NAME);
       
   219 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   220 		{
       
   221 		test.Printf(_L("Failed %d!\n\r"),r);
       
   222 		test(r==KErrNone);
       
   223 		}
       
   224 	else 
       
   225 		test.Printf(_L("Loaded LDD\n"));
       
   226 	test.Printf(_L("Loading LDD\n"));
       
   227 	r=User::LoadLogicalDevice(LDD_NAME);
       
   228 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   229 		{
       
   230 		test.Printf(_L("Failed %d!\n\r"),r);
       
   231 		test(r==KErrNone);
       
   232 		}
       
   233 	else
       
   234 		test.Printf(_L("Loaded PDD\n"));
       
   235 	}
       
   236 
       
   237 
       
   238 TInt E32Main()
       
   239 	{
       
   240 	User::SetJustInTime(ETrue);
       
   241 	__UHEAP_MARK;
       
   242 	CTrapCleanup* cleanupStack=CTrapCleanup::New();
       
   243 
       
   244 	LoadLDD_PDD();
       
   245 	TRAPD(err,RunTestL());	//	Ignore err
       
   246 
       
   247 	if (err != KErrNone)
       
   248 		{
       
   249 		test.Printf(_L("Error %d"), err);
       
   250 		test.Getch();
       
   251 		}
       
   252 
       
   253 	test.Close();
       
   254 	delete cleanupStack;
       
   255 	__UHEAP_MARKEND;
       
   256    	
       
   257 	return err;
       
   258 	}
       
   259