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