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: CBluetoothResponder has been implemented as a state machine that incapsulates all 00028 the BT stack parts that are responsible for advertising the BT service using the SDP server, 00029 setting BT security, listening for an incoming connection and transferring data to/from a connected 00030 remote device (Master). CBluetoothResponder acts as the Slave part in a multiplayer game. 00031 00032 The CBluetoothResponder's state machine includes the following operations: 00033 1) Finding an available RFCOMM's port 00034 2) Binding the BT socket to the RFCOMM's port 00035 3) Advertising the RPS server using the SDP server 00036 4) Setting BT security 00037 5) Listening for an incoming connection (Master connection) 00038 6) Both sending/receiving data to/from the remote device (Master) 00039 7) Callbacks to report to the App's engine the state machine error and completion of events such as listening 00040 for connection, connection completed, report data, sending data completed. 00041 00042 Note that if at any stage we receive an error then the state machine is reset to the EIdle 00043 state and all resources previously allocated are closed. 00044 00045 */ 00046 00047 00048 00049 #ifndef __BLUETOOTHRESPONDER_H__ 00050 #define __BLUETOOTHRESPONDER_H__ 00051 00052 // INCLUDES 00053 #include <bt_sock.h> 00054 #include <btmanclient.h> 00055 #include <btsdp.h> 00056 #include "BluetoothSocketWriterReader.h" 00057 #include "CommonInterfaces.h" 00058 00059 // FORWARD DECLARATIONS 00060 class CSocketReader; 00061 class CSocketWriter; 00062 00063 00064 class CBluetoothServiceAdvertiser; 00065 00066 00067 // CLASS DECLARATION 00072 class CBluetoothResponder : public CBluetoothConnectionBase, 00073 public MBluetoothServiceAdvertiserObserver, 00074 public MSocketWriterReaderObserver 00075 { 00076 public: 00081 enum TResponderState 00082 { 00083 EIdle, //State Machine's entry point 00084 EFindingAvailablePort, //Finding an available RFCOMM's port 00085 EBinding, //Binding the listening socket to the BT device address and RFCOMM's port 00086 EAdvertising, //Advertising the service into SDP's database 00087 ERegisteringService, //Setting the BT security 00088 EListening, //Listening for incoming connection 00089 EWaitingForData, //Waiting for incoming data 00090 ESendingData, //Sending data to the remote BT device 00091 }; 00092 00093 public: 00099 static CBluetoothResponder* NewL(MBluetoothObserver& aRespObs, RSocketServ& aSocketServer); 00100 00104 ~CBluetoothResponder(); 00105 00106 //From MBluetoothServiceAdvertiserObserver 00111 void ReportAdvertiserErr(TInt aError); 00112 00116 void AdvertiserComplete(); 00117 00118 //From MSocketWriterReaderObserver 00123 void WriteComplete(TInt aError); 00124 00130 void ReportData(const TDesC8& aData, TInt aError); 00131 00132 //From CBluetoothConnectionBase 00137 void SendData(const TDesC8& aData); 00138 00142 void StartL(); 00143 00144 private: 00150 CBluetoothResponder(MBluetoothObserver& aRespObs, RSocketServ& aSocketServer); 00151 00155 void ConstructL(); 00156 00157 // From CActive 00161 virtual void RunL(); 00162 00166 virtual void DoCancel(); 00167 00175 TInt RunError(TInt aError); 00176 00180 void SelfComplete(); 00181 00185 void FindAvailablePortL(); 00186 00190 void BindToPortL(); 00191 00195 void RegisterService(); 00196 00200 void ListenL(); 00201 00205 void WaitForData(); 00206 00207 private: 00211 MBluetoothObserver& iRespObs; 00212 00216 RSocketServ& iSocketServer; 00217 00221 TResponderState iState; 00222 00226 TInt iPort; 00227 00231 RSocket iListeningSocket; 00232 00236 RSocket iAcceptingSocket; 00237 00241 TBTSockAddr iBtSocketAddr; 00242 00246 CBluetoothServiceAdvertiser* iServiceAdvertiser; 00247 00252 CSocketReader* iBtSocketReader; 00253 00258 CSocketWriter* iBtSocketWriter; 00259 00263 TBTServiceSecurity iBtSecurity; 00264 }; 00265 00266 #endif // __BLUETOOTHRESPONDER_H__