examples/ForumNokia/BluetoothPMPExample/src/Connector.cpp

00001 /*
00002  * Copyright © 2009 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
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     // disconnect and kill socket
00050     Disconnect();
00051     }
00052 
00053 
00054 void CConnector::DoCancel()
00055     {
00056     iSock.CancelAll();
00057     }
00058 
00059 
00060 // ----------------------------------------------------------------------------
00061 // CConnector::ConnectL(THostName aName, TBTDevAddr aAddr, TInt aPort)
00062 //
00063 // create a connection to given address on given port.  
00064 // ----------------------------------------------------------------------------
00065 TRequestStatus CConnector::ConnectL(THostName aName, TBTDevAddr aAddr, 
00066                                     TInt aPort)
00067     {
00068     iName=aName;
00069     iAddr=aAddr;
00070     iPort=aPort;
00071 
00072     // load protocol, RFCOMM
00073     TProtocolDesc pdesc;
00074     User::LeaveIfError(iSocketServ.FindProtocol(KRfComm(), pdesc));
00075 
00076     // open socket
00077     User::LeaveIfError(iSock.Open(iSocketServ, KRfComm));
00078     // set address and port
00079     TBTSockAddr addr;
00080     addr.SetBTAddr(iAddr);
00081     addr.SetPort(iPort);
00082     
00083     // connect socket
00084     TRequestStatus status;
00085     iSock.Connect(addr, status);
00086     User::WaitForRequest(status);
00087     if ( status!=KErrNone )
00088         {
00089         // error opening conn
00090         return status;
00091         }
00092         
00093     iState=EConnecting;
00094     WaitAndReceive();
00095     return status;
00096     }
00097 
00098 
00099 // ----------------------------------------------------------------------------
00100 // CConnector::Disconnect()
00101 //
00102 // disconnect from remote device, shutdown connected socket
00103 // ----------------------------------------------------------------------------
00104 void CConnector::Disconnect()
00105     {
00106     TRequestStatus status;
00107     // shutdown socket
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 // CConnector::SendData(const TDesC8& aData)
00120 //
00121 // send given data to remote device, write to connected socket
00122 // ----------------------------------------------------------------------------
00123 void CConnector::SendData(const TDesC8& aData)
00124     {
00125     // cancel any read requests on socket
00126     iSock.CancelRead();
00127     Cancel();
00128     // send message
00129     iState=ESending;
00130     iSock.Write(aData, iStatus);
00131     SetActive();
00132     }
00133 
00134 
00135 // ----------------------------------------------------------------------------
00136 // CConnector::WaitAndReceiveL()
00137 //
00138 // wait for and receive data from remote device, read connected socket
00139 // ----------------------------------------------------------------------------
00140 void CConnector::WaitAndReceive()
00141     {
00142     // cancel pending operations
00143     iSock.CancelRead();
00144     Cancel();
00145     // receive data from socket
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             // wait incoming data on socket
00165             WaitAndReceive();
00166             break;
00167             }
00168         case EWaiting:
00169             {
00170             // we got incoming data!
00171             HBufC* text = HBufC::NewLC(iBuffer.Length());
00172             text->Des().Copy(iBuffer);
00173             // observer will handle data
00174             HandleConnectorDataReceivedL(iName, *text);
00175             CleanupStack::PopAndDestroy(text);
00176 
00177             // start expecting new incoming data
00178             WaitAndReceive(); 
00179             break;
00180             }
00181         case ESending:
00182             {
00183             // tried to send a message
00184             if(iState!=KErrNone)
00185                 {
00186                 // Add error handling / socket re-read code
00187                 // here, not implemented in this example
00188                 }
00189 
00190             // start expecting new incoming data
00191             WaitAndReceive();
00192             break;
00193             }
00194         default:
00195             break;
00196         }
00197     }
00198 
00199 TInt CConnector::RunError(TInt /*aError*/)
00200     {
00201     // Add error handling here, not implemented in this example
00202     return KErrNone;
00203     }
00204 
00205 // ----------------------------------------------------------------------------
00206 // CConnector::HandleConnectorDataReceivedL(THostNama aName, TDesC& aData)
00207 //
00208 // a callback to observer indicating that connector has received data
00209 // ----------------------------------------------------------------------------
00210 void CConnector::HandleConnectorDataReceivedL(THostName aName, const TDesC& aData)
00211 {
00212     iObserver.HandleConnectorDataReceivedL(aName, aData);
00213 }
00214 

Generated by  doxygen 1.6.2