⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectexchangeserver.cpp

📁 This C++ code example provides a method for transferring objects or chunks of data from one device
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    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 );
    iLog.LogL( *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 );

    // Bind and start listeing the port with security setted,
    User::LeaveIfError(socket.Bind(sockaddr));
    User::LeaveIfError(socket.Listen(KSimultainousSocketsOpen ) );

    // setup channel security
    TRequestStatus status;
    RBTMan secManager;

    RBTSecuritySettings secDatabase;
    TBTServiceSecurity secSettings;
    // connect to security manager
    User::LeaveIfError(secManager.Connect());
    CleanupClosePushL(secManager);
    User::LeaveIfError(secDatabase.Open(secManager));
    CleanupClosePushL(secDatabase);
    // setup security
    TUid settingsUID;
    settingsUID.iUid = KServiceClass;
    secSettings.SetUid(settingsUID);
    secSettings.SetChannelID(channel);
    secSettings.SetProtocolID(KSolBtRFCOMM);
    secSettings.SetAuthentication(aAuthentication);
    secSettings.SetAuthorisation(aAuthorisation);
    secSettings.SetEncryption(aEncryption);
    secSettings.SetDenied(aDenied);
    // register settings with security
    secDatabase.RegisterService(secSettings, status);
    User::WaitForRequest(status);
    CleanupStack::PopAndDestroy(2,&secManager);//SecDatabase, secManager

    // 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -