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: CBluetoothConnector has been implemented as a state machine that encapsulates all the BT stack 00028 parts that are responsible for managing the connection between a Master and a Slave. 00029 00030 The CBluetoothConnector's state machine includes the following operations: 00031 1) Searching on the remote device (Slave) for available services 00032 2) Connecting to the remote device (Slave) 00033 3) Both sending and receiving data to/from the remote device (Slave) 00034 4) Callbacks to report to the App's engine the state machine error 00035 and completion of events such as connection completed, report data, sending data completed. 00036 00037 00038 Note that if at any stage we receive an error then the state machine is reset to the EIdle 00039 state and all resources previously allocated are closed/cancelled 00040 */ 00041 00042 00043 00044 #ifndef __BLUETOOTHCONNECTOR_H__ 00045 #define __BLUETOOTHCONNECTOR_H__ 00046 00047 // INCLUDES 00048 #include <bt_sock.h> 00049 #include <btmanclient.h> 00050 #include <btsdp.h> 00051 #include "BluetoothSocketWriterReader.h" 00052 #include "CommonInterfaces.h" 00053 00054 class CBluetoothDeviceDiscoverer; 00055 class CBluetoothServiceSearcher; 00056 class CBluetoothSockConnector; 00057 class CSocketReader; 00058 class CSocketWriter; 00059 00064 class CBluetoothConnector : public CBluetoothConnectionBase, 00065 public MBluetoothServiceSearcherObserver, 00066 public MSocketConnectorObserver, 00067 public MSocketWriterReaderObserver 00068 { 00069 public: 00074 enum TConnectorState 00075 { 00076 EIdle, //State machine entry point 00077 ESearchingService, //Searching for a service on the remote BT device 00078 EConnecting, //Connecting to the remote BT device 00079 EWaitingForData, //Listening for incoming data 00080 ESendingData, //Sending data to the remote BT device 00081 }; 00082 public: 00089 static CBluetoothConnector* NewL(MBluetoothObserver& aConnObs, RSocketServ& aSocketServer, const TBTDevAddr& aDevAddr); 00090 00094 ~CBluetoothConnector(); 00095 00096 public: //From CBluetoothConnectionBase 00101 void SendData(const TDesC8& aData); 00102 00106 void StartL(); 00107 00108 private: 00115 CBluetoothConnector(MBluetoothObserver& aConnObs, RSocketServ& aSocketServer, const TBTDevAddr& aDevAddr); 00116 00120 void ConstructL(); 00121 00122 private: // From CActive 00126 virtual void RunL(); 00127 00131 virtual void DoCancel(); 00132 00140 virtual TInt RunError(TInt aError); 00141 00142 //From MBluetoothServiceSearcherObserver 00148 void OnServiceSearchComplete(TInt aPort, TInt aError); 00149 00150 //From MSocketConnectorObserver 00155 void OnSockConnectionComplete(TInt aError); 00156 00157 private: //From MSocketWriterReaderObserver 00162 void WriteComplete(TInt aError); 00163 00169 void ReportData(const TDesC8& aData, TInt aError); 00170 00174 void SearchServiceL(); 00175 00179 void ConnectDeviceL(); 00180 00184 void WaitForData(); 00185 00189 void CancelSocket(); 00190 00194 void SelfComplete(); 00195 00196 private: 00200 MBluetoothObserver& iConnObs; 00201 00205 RSocketServ& iSocketServer; 00206 00210 TBTDevAddr iBtDevAddr; 00211 00215 RSocket iBtSocket; 00216 00220 TConnectorState iState; 00221 00226 CBluetoothServiceSearcher* iServiceSearcher; 00227 00231 CBluetoothSockConnector* iSocketConnector; 00232 00236 TInt iPort; 00237 00242 CSocketReader* iBtSocketReader; 00243 00248 CSocketWriter* iBtSocketWriter; 00249 00253 TInt iMaxIterConnection; 00254 00258 TBool iConnected; 00259 }; 00260 00261 #endif