examples/SFExamples/RockPaperScissorsGameSourceCode_S60/RPS/src/BluetoothSockConnector.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 
00030 
00031 // INCLUDE FILES
00032 
00033 #include "BluetoothSockConnector.h"
00034 #include "common.hrh"
00035 /*
00036 ============================================================================
00037 CBluetoothSockConnector's two stage constructor
00038 ============================================================================
00039 */
00040 CBluetoothSockConnector* CBluetoothSockConnector::NewL(RSocket& aBtSocket, MSocketConnectorObserver& aSockConnObs, RSocketServ& aSocketServer)
00041         {
00042     CBluetoothSockConnector* self = new (ELeave) CBluetoothSockConnector(aBtSocket, aSockConnObs, aSocketServer);
00043     CleanupStack::PushL(self);
00044     self->ConstructL();
00045     CleanupStack::Pop();
00046     return self;
00047         }
00048 
00049 /*
00050 ============================================================================
00051 CBluetoothSockConnector's costructor
00052 ============================================================================
00053 */
00054 CBluetoothSockConnector::CBluetoothSockConnector(RSocket& aBtSocket, MSocketConnectorObserver& aSockConnObs, RSocketServ& aSocketServer)
00055         :CActive(CActive::EPriorityStandard), iBtSocket(aBtSocket), iSockConnObs(aSockConnObs), iSocketServer(aSocketServer)
00056         {
00057         CActiveScheduler::Add(this);
00058         }
00059 
00060 /*
00061 ============================================================================
00062 CBluetoothSockConnector's second phase constructor 
00063 ============================================================================
00064 */
00065 void CBluetoothSockConnector::ConstructL()
00066         {       
00067         /*
00068         ============================================================================
00069         Using the Socket Server we need to load the RFCOMM communication protocol.
00070         First of all we need to see if BT RFCOMM is supported.
00071         ============================================================================
00072         */
00073         TProtocolDesc pdesc;
00074         User::LeaveIfError(iSocketServer.FindProtocol(KBTRFCOMM(), pdesc));
00075 
00076         /*
00077         ============================================================================
00078         Open the socket using BT RFCOMM
00079         ============================================================================
00080         */
00081         User::LeaveIfError(iBtSocket.Open(iSocketServer, KBTRFCOMM));
00082         }
00083 
00084 /*
00085 ============================================================================
00086 CBluetoothSockConnector's destructor
00087 ============================================================================
00088 */
00089 CBluetoothSockConnector::~CBluetoothSockConnector()
00090         {
00091         Cancel();
00092         }
00093 
00094 /*
00095 ============================================================================
00096 DoCancel is called as part of the active object's Cancel().
00097 Outstanding operations for a BT socket include: read, write, Ioctl, connect,
00098 accept, shutdown and the Baseband event notifier. All of these operations will
00099 be completed by this call
00100 ============================================================================
00101 */
00102 void CBluetoothSockConnector::DoCancel()
00103         {
00104         iBtSocket.CancelAll();
00105         }
00106 
00107 /*
00108 ============================================================================
00109 Handles the active object's socket connection completion event 
00110 ============================================================================
00111 */
00112 void CBluetoothSockConnector::RunL()
00113         {
00114         iSockConnObs.OnSockConnectionComplete(iStatus.Int());
00115         }
00116 
00117 /*
00118 ============================================================================
00119 Connect to the remote BT device on a given BT device address and on a given RFCOMM service's port
00120 ============================================================================
00121 */
00122 void CBluetoothSockConnector::ConnectToRemoteBtDeviceL(const TBTDevAddr& aDevAddr, TInt aPort)
00123         {
00124         /*
00125         ============================================================================
00126         Set the remote device address and service port using TBTSockAddr
00127         ============================================================================
00128         */
00129         iBtDevAddr.SetBTAddr(aDevAddr);
00130         iBtDevAddr.SetPort(aPort);
00131         
00132         /*
00133         ============================================================================
00134         Connect the BT socket to the remote device.
00135         ============================================================================
00136         */
00137         iBtSocket.Connect(iBtDevAddr, iStatus);
00138         SetActive();
00139         }
00140         
00141 /*
00142 ============================================================================
00143 Handles a leave occurring in the request completion event handler RunL().
00144 ============================================================================
00145 */
00146 TInt CBluetoothSockConnector::RunError(TInt aError)
00147         {
00148         iSockConnObs.OnSockConnectionComplete(aError);
00149         return KErrNone;
00150         }

Generated by  doxygen 1.6.2