examples/QtQuick/bluetoothex/connector.cpp

00001 /*
00002  * Copyright (c) 2010 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:  
00028  */
00029 
00030 // INCLUDE FILES
00031 #include "connector.h"
00032 
00033 _LIT(KRfComm,"RFCOMM");
00034 
00035 CConnector* CConnector::NewL(MConnectorObserver& aObserver, 
00036                              RSocketServ& aSocketServ)
00037     {
00038     CConnector* self = CConnector::NewLC(aObserver, aSocketServ);
00039     CleanupStack::Pop(self);
00040     return self;
00041     }
00042 
00043 
00044 CConnector* CConnector::NewLC(MConnectorObserver& aObserver, 
00045                               RSocketServ& aSocketServ)
00046     {
00047     CConnector* self = new (ELeave) CConnector(aObserver, aSocketServ);
00048     CleanupStack::PushL(self);
00049     self->ConstructL();
00050     return self;
00051     }
00052 
00053 
00054 void CConnector::ConstructL()
00055     {
00056     }
00057 
00058 
00059 CConnector::CConnector(MConnectorObserver& aObserver, 
00060                        RSocketServ& aSocketServ):
00061     CActive(CActive::EPriorityStandard),
00062     iObserver(aObserver),
00063     iSocketServ(aSocketServ),
00064     iState(ENone)
00065     {
00066     CActiveScheduler::Add(this);
00067     }
00068 
00069 
00070 CConnector::~CConnector()
00071     {
00072     Cancel();
00073     // disconnect and kill socket
00074     Disconnect();
00075     }
00076 
00077 
00078 void CConnector::DoCancel()
00079     {
00080     iSock.CancelAll();
00081     }
00082 
00083 
00084 // ----------------------------------------------------------------------------
00085 // CConnector::ConnectL(THostName aName, TBTDevAddr aAddr, TInt aPort)
00086 //
00087 // create a connection to given address on given port.  
00088 // ----------------------------------------------------------------------------
00089 TRequestStatus CConnector::ConnectL(THostName aName, TBTDevAddr aAddr, 
00090                                     TInt aPort)
00091     {
00092     iName=aName;
00093     iAddr=aAddr;
00094     iPort=aPort;
00095 
00096     // load protocol, RFCOMM
00097     TProtocolDesc pdesc;
00098     User::LeaveIfError(iSocketServ.FindProtocol(KRfComm(), pdesc));
00099 
00100     // open socket
00101     User::LeaveIfError(iSock.Open(iSocketServ, KRfComm));
00102     // set address and port
00103     TBTSockAddr addr;
00104     addr.SetBTAddr(iAddr);
00105     addr.SetPort(iPort);
00106     
00107     // connect socket
00108     TRequestStatus status;
00109     iSock.Connect(addr, status);
00110     User::WaitForRequest(status);
00111     if ( status!=KErrNone )
00112         {
00113         // error opening conn
00114         return status;
00115         }
00116         
00117     iState=EConnecting;
00118     WaitAndReceive();
00119     return status;
00120     }
00121 
00122 
00123 // ----------------------------------------------------------------------------
00124 // CConnector::Disconnect()
00125 //
00126 // disconnect from remote device, shutdown connected socket
00127 // ----------------------------------------------------------------------------
00128 void CConnector::Disconnect()
00129     {
00130     TRequestStatus status;
00131     // shutdown socket
00132     if (iState == ENone)
00133         {
00134         return;
00135         }
00136     iSock.Shutdown(RSocket::ENormal, status);
00137     User::WaitForRequest(status);
00138     iSock.Close();
00139     }
00140 
00141 
00142 // ----------------------------------------------------------------------------
00143 // CConnector::SendData(const TDesC8& aData)
00144 //
00145 // send given data to remote device, write to connected socket
00146 // ----------------------------------------------------------------------------
00147 void CConnector::SendData(const TDesC8& aData)
00148     {
00149     // cancel any read requests on socket
00150     iSock.CancelRead();
00151     Cancel();
00152     // send message
00153     iState=ESending;
00154     iSock.Write(aData, iStatus);
00155     SetActive();
00156     }
00157 
00158 
00159 // ----------------------------------------------------------------------------
00160 // CConnector::WaitAndReceiveL()
00161 //
00162 // wait for and receive data from remote device, read connected socket
00163 // ----------------------------------------------------------------------------
00164 void CConnector::WaitAndReceive()
00165     {
00166     // cancel pending operations
00167     iSock.CancelRead();
00168     Cancel();
00169     // receive data from socket
00170     iState=EWaiting;
00171     iSock.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
00172     SetActive();
00173     }
00174 
00175 
00176 void CConnector::RunL()
00177     {
00178     if ( iStatus!=KErrNone )
00179         {
00180         iObserver.HandleConnectorErrorL(iName,iStatus.Int());
00181         return;
00182         }
00183 
00184     switch (iState)
00185         {
00186         case EConnecting:
00187             {
00188             // wait incoming data on socket
00189             WaitAndReceive();
00190             break;
00191             }
00192         case EWaiting:
00193             {
00194             // we got incoming data!
00195             HBufC* text = HBufC::NewLC(iBuffer.Length());
00196             text->Des().Copy(iBuffer);
00197             // observer will handle data
00198             HandleConnectorDataReceivedL(iName, *text);
00199             CleanupStack::PopAndDestroy(text);
00200 
00201             // start expecting new incoming data
00202             WaitAndReceive(); 
00203             break;
00204             }
00205         case ESending:
00206             {
00207             // tried to send a message
00208             if(iState!=KErrNone)
00209                 {
00210                 // Add error handling / socket re-read code
00211                 // here, not implemented in this example
00212                 }
00213 
00214             // start expecting new incoming data
00215             WaitAndReceive();
00216             break;
00217             }
00218         default:
00219             break;
00220         }
00221     }
00222 
00223 TInt CConnector::RunError(TInt /*aError*/)
00224     {
00225     // Add error handling here, not implemented in this example
00226     return KErrNone;
00227     }
00228 
00229 // ----------------------------------------------------------------------------
00230 // CConnector::HandleConnectorDataReceivedL(THostNama aName, TDesC& aData)
00231 //
00232 // a callback to observer indicating that connector has received data
00233 // ----------------------------------------------------------------------------
00234 void CConnector::HandleConnectorDataReceivedL(THostName aName, const TDesC& aData)
00235 {
00236     iObserver.HandleConnectorDataReceivedL(aName, aData);
00237 }
00238 

Generated by  doxygen 1.6.2