Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



How to make a primary connection

Once a device has been discovered, an IrDA connect may be initiated.

An RSocket object is required to achieve this. In terms of the IrDA specification, a successful return from connect will put the two machines into the link state (or NRM) after having completed both the IrLAP and IrLMP connect procedures.

Use the functions RSocket::Open(), RSocket::Bind(), and RSocket::Connect() to achieve this connection.

The following example code illustrates the attempted opening of two IrDA sockets on <homeport,remoteport> tuple pairs <2,1> and <2,3>.

TInt ret;
RSocketServ ss;
...
//
// MAKING SOCKET 1
//
TProtocolDesc pInfo;
RSocket       sock1;
ret=sock1.Open(ss,pInfo.iAddrFamily,pInfo.iSockType,pInfo.iProtocol);

//
// BIND SOCKET 1 - PORT 2
//
TUint8         home   = 0x02;
TUint8         remote = 0x01;
TSockAddr      addr;
TRequestStatus stat1;

addr.SetPort(home);
sock1.Bind(addr);                  // Binding to home port: home

addr.SetPort(remote);              // Trying to connect to sockt 1,
sock1.Connect(addr,stat1);         // remote port: remote
User::WaitForRequest(stat1);

switch (stat1.Int())
    {
case KErrNone:
    // Successfully completed a MUX connect
    break;

case KErrNotFound:
    // #### No devices were discovered ####
    break;

case KErrTimedOut:
    // #### LAP connect has failed ####
    break;

case KErrCouldNotConnect:
    // #### MUX connect has failed ####
    break;

case KErrDisconnected:
    // #### General disconnect indication ####
    break;

default:
    // #### Unknown connnect error ####
    break;
    }

//
// MAKING SOCKET 2
//
RSocket sock2;
ret=sock2.Open(ss,pInfo.iAddrFamily,pInfo.iSockType,pInfo.iProtocol);

//
// BIND TO SOCKET 2
//
home   = 0x02;
remote = 0x03;

addr.SetPort(home);
sock2.Bind(addr);                  // Binding to home port: home

addr.SetPort(remote);              // Trying to connect to socket 2
test.Next(_L("Connect to socket"));// remote port: remote
sock2.Connect(addr,stat1);  
User::WaitForRequest(stat1);       // stat1 should return KErrNone

Note that it is not necessary to bind to a specific home port. If desired, an autobinding mechanism may be invoked by omitting the bind code. In the case illustrated below, after opening the socket, an attempt is made to connect to a specified remote port only. The LocalName() service is subsequently invoked in order to determine the value of the home port generated during autobinding. The autobinding code in the IrDA protocol module always sets the home port to the lowest unused home port value. The code fragment below also illustrates how to append connect data by means of an overloaded RSocket connect call. The fPsion IRLink 1.00 string is necessary to initiate a file transfer with a version 1 EPOC16 IrDA client (Siena or S3c). The output string is the corresponding return from the remote SIBO machines MUX connect confirm frame. A successful return from this connect thus corresponds to a MUX connection to an EPOC16 IrDA stack.

...
TUint8 remote = 0x01; // Must set remote port to 1 to talk to SIBO.
TUint8 home   = 0xff;
TRequestStatus stat1;

//
// CONNECTING TO SOCKET
//
_LIT8(KConnData,"fPsion IRLink 1.00"); // Up to 64 bytes connect data.
TBuf<64> output;                      // For connect confirm data.

addr.SetPort(remote);                     // Remote port: remote
sock1.Connect(addr,connData,output,stat1);// If no bind, then AutoBind
User::WaitForRequest(stat1);              // invoked.

switch (stat1.Int())
 {
case KErrNone:
    // Successfully completed a MUX connect
    // Returned data in descriptor buffer: output
    break;

case KErrNotFound:
    // ### No devices were discovered ####
    break;

case KErrTimedOut:
    // #### LAP connect has failed ####
    break;

case KErrCouldNotConnect:
    // #### MUX connect has failed ####
    break;

case KErrDisconnected:
    // #### General disconnect indication ####
    break;

default:
    // #### Unknown connnect error ####
    break;
    }

//
// LOCAL AND REMOTE NAMES
//
sock1.LocalName(addr,stat1);
User::WaitForRequest(stat1);
if (stat1.Int()==KErrNone)
 {
 home=TUint8(addr.Port());   // Bound to home port: home
 }
else
 {
 // stat1.Int() returned from LocalName() request
 }

sock1.RemoteName(addr,stat1);
User::WaitForRequest(stat1);
if (stat1.Int()==KErrNone)
 {
 remote=TUint8(addr.Port()); // Bound to remote port: remote
 }
else
 {
 // stat1.Int()returned from RemoteName() request
 }