www.pudn.com > S60_Platform_Bluetooth_OBEX_Example.rar > objectexchangeserver_26.cpp


/* Copyright (c) 2004, Nokia. All rights reserved */ 
 
 
// INCLUDE FILES 
#include  
#include  
#include  
 
#include "ObjectExchangeServer.h" 
#include "ObjectExchangeServiceAdvertiser.h" 
#include "ObjectExchangeProtocolConstants.h" 
#include "Log.h" 
#include "BTObjectExchange.pan" 
 
#include  
#include  // New for 8.0 
 
#include  
#include  
 
// ============================ MEMBER FUNCTIONS ============================== 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::NewL() 
// Two-phased constructor. 
// ---------------------------------------------------------------------------- 
// 
CObjectExchangeServer* CObjectExchangeServer::NewL( MLog& aLog ) 
    { 
    CObjectExchangeServer* self = CObjectExchangeServer::NewLC( aLog ); 
    CleanupStack::Pop( self ); 
    return self; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::NewLC() 
// Two-phased constructor. 
// ---------------------------------------------------------------------------- 
// 
CObjectExchangeServer* CObjectExchangeServer::NewLC( MLog& aLog ) 
    { 
    CObjectExchangeServer* self = new ( ELeave ) CObjectExchangeServer( aLog ); 
    CleanupStack::PushL( self ); 
    self->ConstructL(); 
    return self; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::CObjectExchangeServer() 
// Constructor. 
// ---------------------------------------------------------------------------- 
// 
CObjectExchangeServer::CObjectExchangeServer( MLog& aLog ) 
:  iLog( aLog ) 
    { 
    } 
 
// ----------------------------------------------------------------------------- 
// CObjectExchangeServer::ConstructL() 
// Symbian 2nd phase constructor can leave. 
// ----------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::ConstructL() 
    { 
    iAdvertiser = CObjectExchangeServiceAdvertiser::NewL(); 
    User::LeaveIfError(iFs.Connect()); 
    iTempFile = KTempFile().AllocL(); 
 
    //Make sure the file directory exists 
    TFileName file = KTempFile(); 
    TParsePtr parsePtr ( file ); 
    TFileName filename = parsePtr.DriveAndPath(); 
    BaflUtils::EnsurePathExistsL( CEikonEnv::Static()->FsSession(), filename); 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::~CObjectExchangeServer() 
// Destructor. 
// ---------------------------------------------------------------------------- 
// 
CObjectExchangeServer::~CObjectExchangeServer() 
    { 
    if ( iObexServer && iObexServer->IsStarted() ) 
        { 
        iObexServer->Stop(); 
        } 
 
    delete iObexServer; 
    iObexServer = NULL; 
 
    //This is used for temp purposes, see TransportDownIndication 
    delete iObexBufData; 
    iObexBufData = NULL; 
 
    delete iObexBufObject; 
    iObexBufObject = NULL; 
 
    delete iAdvertiser; 
    iAdvertiser = NULL; 
 
    delete iTempFile; 
    iTempFile=NULL; 
 
    iFs.Delete( *iTempFile ); //Destroy temp file 
    iFs.Close(); 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::ErrorIndication() 
// Receive error indication. 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::ErrorIndication( TInt aError ) 
    { 
    HBufC* strErrorPrefix = NULL; 
    //ignore possible error 
    TRAPD(ignore, 
        strErrorPrefix = StringLoader::LoadL ( R_BTOB_ERROR_PREFIX ); 
        ); 
    Log(*strErrorPrefix, aError ); 
    delete strErrorPrefix; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::TransportUpIndication() 
// Called when the underlying socket transport connection is made from 
// a remote client to the server 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::TransportUpIndication() 
    { 
    HBufC* strConnected = NULL; 
    //ignore possible error 
    TRAPD(ignore, 
        strConnected = StringLoader::LoadL( R_BTOB_CONNECTED ); 
        ); 
    Log(*strConnected); 
    delete strConnected; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::TransportDownIndication() 
// Transport connection is dropped. 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::TransportDownIndication() 
    { 
    HBufC* strDisconnecting = NULL; 
    //ignore possible error 
    TRAPD(ignore, 
        strDisconnecting = StringLoader::LoadL( R_BTOB_DISCONNECTING ); 
        ); 
    Log(*strDisconnecting ); 
 
    if ( iObexBufObject ) 
        { 
        //transfer cancelled, set temporary 
        //buffer so obexobject doesn't create the file again 
        delete iObexBufData; 
        iObexBufData = NULL; 
        TRAPD( err, ( iObexBufData = CBufFlat::NewL( KBufferGranularity ) ) ); 
        if ( err == KErrNone ) 
            { 
            TRAP( err, ( iObexBufObject->SetDataBufL( iObexBufData ) ) ); 
            } 
        } 
 
    if ( iTempFile ) 
        { 
        iFs.Delete( *iTempFile ); 
        } 
 
 
    delete strDisconnecting; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::ObexConnectIndication() 
// Invoked when an OBEX connection is made from a remote client. 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::ObexConnectIndication( 
    const TObexConnectInfo&, const TDesC8& ) 
    { 
    return KErrNone; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::ObexDisconnectIndication() 
// OBEX server has been disconnected. 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::ObexDisconnectIndication( const TDesC8& /*aInfo*/ ) 
    { 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::PutRequestIndication() 
// ---------------------------------------------------------------------------- 
// 
CObexBufObject* CObjectExchangeServer::PutRequestIndication() 
    { 
    delete iObexBufObject; 
    iObexBufObject = NULL; 
    iFs.Delete( *iTempFile ); 
 
    //Can't leave here 
    TRAPD(err, 
        { 
        iObexBufObject = CObexBufObject::NewL( NULL ); 
        //The file is created if it doesn't exist 
        iObexBufObject->SetDataBufL( *iTempFile ); 
        } 
    ); 
 
    if(err) 
        { 
        Log(KPutRequestIndication, err); 
        delete iObexBufObject; 
        iObexBufObject=NULL; 
        return NULL; 
        } 
 
    return iObexBufObject; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::PutPacketIndication() 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::PutPacketIndication() 
    { 
    return KErrNone; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::PutCompleteIndication() 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::PutCompleteIndication() 
    { 
    Log(iObexBufObject->Name()); 
    TFileName file = KTempFile(); 
    TParsePtr parsePtr ( file ); 
    TFileName filename = parsePtr.DriveAndPath(); 
    filename.Append(iObexBufObject->Name()); 
 
    delete iObexBufObject; 
    iObexBufObject = NULL; 
 
    TInt err2 = BaflUtils::RenameFile(iFs, KTempFile, filename, CFileMan::EOverWrite); 
    if( err2 ) 
        { 
        Log(KCopyFileError, err2 ); 
        } 
 
    return KErrNone; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::GetRequestIndication() 
// Called when a full get request has been received from the client. 
// ---------------------------------------------------------------------------- 
// 
CObexBufObject* CObjectExchangeServer 
::GetRequestIndication( CObexBaseObject* /*aRequiredObject*/ ) 
    { 
    return NULL; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::GetPacketIndication() 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::GetPacketIndication() 
    { 
    return KErrNone; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::GetCompleteIndication() 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::GetCompleteIndication() 
    { 
    return KErrNone; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::SetPathIndication() 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::SetPathIndication( const CObex 
                                              ::TSetPathInfo& /*aPathInfo*/, 
                                              const TDesC8& /*aInfo*/ ) 
    { 
    return KErrNone; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::AbortIndication() 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::AbortIndication() 
    { 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::DisconnectL() 
// Disconnects the server. 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::DisconnectL() 
    { 
    if ( iObexServer && iObexServer->IsStarted() ) 
        { 
        iObexServer->Stop(); 
        } 
 
    delete iObexServer; 
    iObexServer = NULL; 
 
    if ( iAdvertiser->IsAdvertising() ) 
        { 
        iAdvertiser->StopAdvertisingL(); 
        } 
    HBufC* strServerStopped = StringLoader::LoadLC ( R_BTOB_SERVER_STOPPED ); 
    Log( *strServerStopped ); 
    CleanupStack::PopAndDestroy ( strServerStopped ); 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::StartL() 
// Starts the server. 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::StartL() 
    { 
    TRAPD( err,InitialiseServerL() ); 
 
    if ( err != KErrNone ) 
        { 
        HBufC* strFailed = StringLoader::LoadLC ( R_BTOB_STR_FAILED ); 
        iLog.LogL( *strFailed, err ); 
        CleanupStack::PopAndDestroy ( strFailed ); 
        DisconnectL(); 
       } 
    else 
        { 
        TParsePtrC parsePtr ( iTempFile->Des() ); 
        //Log current working directory 
        iLog.LogL( KCurrentWorkingDirectory ); 
        iLog.LogL(parsePtr.DriveAndPath()); 
        } 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::InitialiseServerL() 
// Initialises the server. 
// ---------------------------------------------------------------------------- 
// 
void CObjectExchangeServer::InitialiseServerL() 
    { 
    if ( iObexServer ) 
        { 
        ASSERT( IsConnected() ); // server already running 
        return; 
        } 
 
    // Set the Socket's security with parameters, 
    // Authentication, Encryption, Authorisation and Denied 
    // Method also return the channel available to listen to. 
    TInt channel ( SetSecurityWithChannelL( EFalse, EFalse, ETrue, EFalse ) ); 
 
    // start the OBEX server 
    TObexBluetoothProtocolInfo obexProtocolInfo; 
    obexProtocolInfo.iTransport.Copy( KServerTransportName ); 
    obexProtocolInfo.iAddr.SetPort( channel ); 
 
    iObexServer = CObexServer::NewL( obexProtocolInfo ); 
    iObexServer->Start( this ); 
 
    // advertise this service 
    iAdvertiser->StartAdvertisingL( channel ); 
    iAdvertiser->UpdateAvailabilityL( ETrue ); 
 
    HBufC* strServerStarted = StringLoader::LoadLC ( R_BTOB_SERVER_STARTED ); 
    iLog.LogL( *strServerStarted ); 
    CleanupStack::PopAndDestroy ( strServerStarted ); 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::SetSecurityWithChannelL() 
// Sets the security on the channel port and returns the available port. 
// ---------------------------------------------------------------------------- 
// 
TInt CObjectExchangeServer::SetSecurityWithChannelL( TBool aAuthentication, 
                                                  TBool aEncryption, 
                                                  TBool aAuthorisation, 
                                                  TBool aDenied ) 
 
    { 
 
    // Local variable to channel to listen to. 
    TInt channel; 
 
    RSocketServ socketServer; 
 
    // Connect to SocetServer 
    User::LeaveIfError( socketServer.Connect() ); 
    CleanupClosePushL( socketServer ); 
 
    RSocket socket; 
    // Open the Socket connection 
    User::LeaveIfError( socket.Open( socketServer, KStrRFCOMM ) );  // "RFCOMM" 
    CleanupClosePushL( socket ); 
 
    // Retreive to one channel that is available. 
    User::LeaveIfError( socket.GetOpt( KRFCOMMGetAvailableServerChannel,KSolBtRFCOMM, channel ) ); 
 
    // Set the Socket's Port. 
    TBTSockAddr sockaddr; 
    sockaddr.SetPort( channel ); 
 
    // Set the security according to. 
    TBTServiceSecurity serviceSecurity; 
    serviceSecurity.SetUid ( KUidBTObjectExchangeApp ); 
    serviceSecurity.SetAuthentication ( aAuthentication ); 
    serviceSecurity.SetEncryption ( aEncryption ); 
    serviceSecurity.SetAuthorisation ( aAuthorisation ); 
    serviceSecurity.SetDenied( aDenied ); 
 
    // Attach the security settings. 
    sockaddr.SetSecurity(serviceSecurity); 
 
    // Bind and start listeing the port with security setted, 
    User::LeaveIfError(socket.Bind(sockaddr)); 
    User::LeaveIfError(socket.Listen(KSimultainousSocketsOpen ) ); 
 
    // now close the socket and the socket server 
    CleanupStack::PopAndDestroy();  //  socket 
    CleanupStack::PopAndDestroy();  //  socketServer 
 
    return channel; 
    } 
 
// ---------------------------------------------------------------------------- 
// CObjectExchangeServer::IsConnected() 
// Results true if the server is connected. 
// ---------------------------------------------------------------------------- 
// 
TBool CObjectExchangeServer::IsConnected() 
    { 
    return iObexServer != NULL; 
    } 
 
void CObjectExchangeServer::Log(const TDesC& aText, TInt KErrCode) 
    { 
    //Ignore possible logging errors 
    if( KErrCode == KErrNone ) 
        { 
        TRAPD(ignore, iLog.LogL(aText) ); 
        } 
    else 
        { 
        TRAPD(ignore, iLog.LogL(aText, KErrCode) ); 
        } 
    } 
 
// End of File