00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "Connector.h"
00032 #include "BluetoothPMPExampleApp.h"
00033
00034 _LIT(KRfComm,"RFCOMM");
00035
00036 CConnector* CConnector::NewL(MConnectorObserver& aObserver,
00037 RSocketServ& aSocketServ)
00038 {
00039 CConnector* self = CConnector::NewLC(aObserver, aSocketServ);
00040 CleanupStack::Pop(self);
00041 return self;
00042 }
00043
00044
00045 CConnector* CConnector::NewLC(MConnectorObserver& aObserver,
00046 RSocketServ& aSocketServ)
00047 {
00048 CConnector* self = new (ELeave) CConnector(aObserver, aSocketServ);
00049 CleanupStack::PushL(self);
00050 self->ConstructL();
00051 return self;
00052 }
00053
00054
00055 void CConnector::ConstructL()
00056 {
00057 }
00058
00059
00060 CConnector::CConnector(MConnectorObserver& aObserver,
00061 RSocketServ& aSocketServ):
00062 CActive(CActive::EPriorityStandard),
00063 iObserver(aObserver),
00064 iSocketServ(aSocketServ),
00065 iState(ENone)
00066 {
00067 CActiveScheduler::Add(this);
00068 }
00069
00070
00071 CConnector::~CConnector()
00072 {
00073 Cancel();
00074
00075 Disconnect();
00076 }
00077
00078
00079 void CConnector::DoCancel()
00080 {
00081 iSock.CancelAll();
00082 }
00083
00084
00085
00086
00087
00088
00089
00090 TRequestStatus CConnector::ConnectL(THostName aName, TBTDevAddr aAddr,
00091 TInt aPort)
00092 {
00093 iName=aName;
00094 iAddr=aAddr;
00095 iPort=aPort;
00096
00097
00098 TProtocolDesc pdesc;
00099 User::LeaveIfError(iSocketServ.FindProtocol(KRfComm(), pdesc));
00100
00101
00102 User::LeaveIfError(iSock.Open(iSocketServ, KRfComm));
00103
00104 TBTSockAddr addr;
00105 addr.SetBTAddr(iAddr);
00106 addr.SetPort(iPort);
00107
00108
00109 TRequestStatus status;
00110 iSock.Connect(addr, status);
00111 User::WaitForRequest(status);
00112 if ( status!=KErrNone )
00113 {
00114
00115 return status;
00116 }
00117
00118 iState=EConnecting;
00119 WaitAndReceive();
00120 return status;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 void CConnector::Disconnect()
00130 {
00131 TRequestStatus status;
00132
00133 if (iState == ENone)
00134 {
00135 return;
00136 }
00137 iSock.Shutdown(RSocket::ENormal, status);
00138 User::WaitForRequest(status);
00139 iSock.Close();
00140 }
00141
00142
00143
00144
00145
00146
00147
00148 void CConnector::SendData(const TDesC8& aData)
00149 {
00150
00151 iSock.CancelRead();
00152 Cancel();
00153
00154 iState=ESending;
00155 iSock.Write(aData, iStatus);
00156 SetActive();
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 void CConnector::WaitAndReceive()
00166 {
00167
00168 iSock.CancelRead();
00169 Cancel();
00170
00171 iState=EWaiting;
00172 iSock.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
00173 SetActive();
00174 }
00175
00176
00177 void CConnector::RunL()
00178 {
00179 if ( iStatus!=KErrNone )
00180 {
00181 iObserver.HandleConnectorErrorL(iName,iStatus.Int());
00182 return;
00183 }
00184
00185 switch (iState)
00186 {
00187 case EConnecting:
00188 {
00189
00190 WaitAndReceive();
00191 break;
00192 }
00193 case EWaiting:
00194 {
00195
00196 HBufC* text = HBufC::NewLC(iBuffer.Length());
00197 text->Des().Copy(iBuffer);
00198
00199 HandleConnectorDataReceivedL(iName, *text);
00200 CleanupStack::PopAndDestroy(text);
00201
00202
00203 WaitAndReceive();
00204 break;
00205 }
00206 case ESending:
00207 {
00208
00209 if(iState!=KErrNone)
00210 {
00211
00212
00213 }
00214
00215
00216 WaitAndReceive();
00217 break;
00218 }
00219 default:
00220 break;
00221 }
00222 }
00223
00224 TInt CConnector::RunError(TInt )
00225 {
00226
00227 return KErrNone;
00228 }
00229
00230
00231
00232
00233
00234
00235 void CConnector::HandleConnectorDataReceivedL(THostName aName, const TDesC& aData)
00236 {
00237 iObserver.HandleConnectorDataReceivedL(aName, aData);
00238 }
00239