Share via


Discovering Devices Using IrSock

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

Before an IrDA socket connection can be initiated, a device address must be obtained by calling the getsockopt function and specifying the socket option, IRLMP_ENUMDEVICES in the optname parameter. This option causes a single discovery to be run on each idle adapter. The list of discovered devices and cached devices (on active adapters) is returned immediately. For more information about the socket options supported for IrDA, see SOL_IRLMP.

The following example code shows how to call getsockopt to perform device discovery.

getsockopt(hIrdaSock, SOL_IRLMP, IRLMP_ENUMDEVICES, &DevList, &DevListLen);

This function returns a list of all available IrDA devices. A device address returned from the function call is copied into a SOCKADDR_IRDA structure, which is used by a subsequent call to the connect function to connect to the specified device.

The second approach to performing discovery of IrDA device addresses is to notify the application only when the device list changes from the last discovery run by the stack. This is performed by using the socket option, IRLMP_IAS_QUERY, which is used to retrieve a single attribute of a single class from a peer device's Information Access Service(IAS) database. IrDA provides this database to store IrDA-based information. Limited access to the IAS database is available through the Winsock interface, but such access is not typically used by applications and exists primarily to support connections to non-Windows devices that are not compliant with the Winsock IrDA conventions.

The application specifies the device and class to query and the attribute and attribute type.

Note

The device should have performed discovery in a previous call to getsockopt by using IRLMP_ENUMDEVICES, as described earlier. It is expected that the application allocates a buffer, of the necessary size, for the returned parameters.

The following code shows how to perform a device discovery by using getsockopt with the IRLMP_ENUMDEVICES option.

SOCKADDR_IRDA     DestSockAddr = { AF_IRDA, 0, 0, 0, 0, "SampleIrDAService" };
#define DEVICE_LIST_LEN    10
unsigned char    DevListBuff[sizeof(DEVICELIST) –
                 sizeof(IRDA_DEVICE_INFO) +
                 (sizeof(IRDA_DEVICE_INFO) * DEVICE_LIST_LEN)];
int              DevListLen    = sizeof(DevListBuff);
PDEVICELIST      pDevList      = (PDEVICELIST) &DevListBuff;
pDevList->numDevice = 0;
// Sock is not in connected state
if (getsockopt(Sock, SOL_IRLMP, IRLMP_ENUMDEVICES,
               (char *) pDevList, &DevListLen) == SOCKET_ERROR)
{
        // WSAGetLastError 
}
if (pDevList->numDevice == 0)
{
        // no devices discovered or cached
        // not a bad idea to run a couple of times
}
else
{
       // one per discovered device
       for (i = 0; i < (int) pDevList->numDevice; i++)
       {
            // typedef struct _IRDA_DEVICE_INFO
            // {
            //     u_char    irdaDeviceID[4];
            //     char      irdaDeviceName[22];
            //     u_char    irdaDeviceHints1;
            //     u_char    irdaDeviceHints2;
            //     u_char    irdaCharSet;
            // } _IRDA_DEVICE_INFO;
            // pDevList->Device[i]. see _IRDA_DEVICE_INFO for fields
            // display the device names and let the user select one
       }
}
// assume the user selected the first device [0]
memcpy(&DestSockAddr.irdaDeviceID[0], &pDevList->Device[0].irdaDeviceID[0], 4);
if (connect(Sock, (const struct sockaddr *) &DestSockAddr,
            sizeof(SOCKADDR_IRDA)) == SOCKET_ERROR)
{
       // WSAGetLastError
}

See Also

Concepts

IrSock Server Application
IrSock Client Application
Creating an Infrared Winsock Application
IrDA Application Development