Receiving Data from the Remote Device

Context

When the MLlcpConnOrientedListener::RemoteConnectRequest() callback is called, the remote device sends a connect request to the local device. The local device can accept the connect request by using the MLlcpConnOrientedTransporter::AcceptConnectRequest() method or decline the connect request by using the MLlcpConnOrientedTransporter::DeclineAcceptRequest() method.

In this "Hello World!" example, the remote device's connect request is accepted if the request is the first request. All other requests are declined. When the first connect request is accepted, CMyOwnLlcpApplication waits for incoming data packets from the remote device.

Steps

  1. Call the MLlcpConnOrientedListener::RemoteConnectRequest() method and store the remote connection, as shown in the following code snippet:

    // CMyOwnLlcpApplication::RemoteConnectRequest()
    // From MLlcpConnOrientedListener
    // -----------------------------------------------------------------------------
    //
    void CMyOwnLlcpApplication::RemoteConnectRequest( MLlcpConnOrientedTransporter* aConnection )
        {
        TInt error = KErrNone;
        
        // Only accept one incoming remote connection
        if ( !iRemoteConnection )
            {
            aConnection->AcceptConnectRequest();
            
            // Create a wrapper for the connection. 
            TRAP( error, iRemoteConnection = COwnLlcpConnection::NewL( aConnection ) );
            if ( error == KErrNone )
                {
                // Start receiving the data
                iRemoteConnection->StartReceive( *this );
                }
            else
                {
                delete aConnection;
                }
            }
        else
            {
            // Decline the connection request and delete the connection object
            aConnection->DeclineConnectRequest();
            delete aConnection;
            }
        }
    // -----------------------------------------------------------------------------
    // End of CMyOwnLlcpApplication::RemoteConnectRequest()

    Note: When a connect request is accepted, the MLlcpConnOrientedTransporter object is automatically moved to the Connected status. This indicates that there is no need to issue the MLlcpConnOrientedTransporter::Connect() method before transferring or receiving the data since the MLlcpConnOrientedTransporter object is already connected.

  2. Implement the COwnLlcpConnection::StartReceive() method as shown in the following code snippet:

    // COwnLlcpConnection::StartReceive()
    // -----------------------------------------------------------------------------
    //
    TInt COwnLlcpConnection::StartReceive( MLlcpReceiveCb& aLlcpReceiveCb )
        {
        TInt error = KErrNone;
        
        if ( iConnState == EConnected )
            {
            if ( iActionState == EIdle )
                {
                TInt length = 0;
                length = iConnection->SupportedDataLength();
                iLlcpReceiveCb = &aLlcpReceiveCb
                
                if ( length > 0 )
                    {
                    iReceiveBuf.Zero();
                    error = iReceiveBuf.ReAlloc( length );
                    
                    if ( error == KErrNone )
                        {
                        iConnection->Receive( iStatus, iReceiveBuf );
                        SetActive();
                        iActionState = EReceiving;
                        }
                    }
                else
                    {
                    // if the length is 0 or negative, the LLCP link is destroyed.
                    error = KErrNotReady;
                    }
                }
            else
                {
                // The connection is already connecting or transfering data, 
                // and cannot start receiving.
                error = KErrInUse;
                }
            }
        else
            {
            error = KErrNotReady;
            }
        
        return error;
        }
    // -----------------------------------------------------------------------------
    // End of COwnLlcpConnection::StartReceive()
    
    

    Note:

    • The COwnLlcpConnection::StartReceive() method cannot start receiving data if there is a pending transfer or receive request. In such a scenario, KErrInUse is returned.

    • Data cannot be received from a remote device if a connection is not established. In such a scenario, KErrNotReady is returned.

    • The LLCP link between the local and remote can be removed anytime. Therefore, the MLlcpConnOrientedTransporter::SupportedDataLength() method can return negative values and they must be handled correctly.

    The following diagram illustrates the sequence diagram of data receiving: