Class ProtocolService

To open a protocol service:

service = "PHYS0";
cid     = new ProtocolService();

if (cid.open(service))
{
    /* Process */
}
else
{
    String text = "Failure opening service " + service +
                        ": " + cid.getLastErrorMessage();

    System.err.println(text);
}

When the protocol service is no longer needed:

if (!cid.close())
{
    String text = "Failure closing CID " + cid.getCid() +
                        ": " + cid.getLastErrorMessage();

    System.err.println(text);
}

To stack protocol services:

for (i = 1; i < cid.length; i++)
{
    if (!cid[i].push(cid[i-1]))
    {
        String text = "Failure pushing CID " +
                            cid[i].getCid() + " onto " +
                            cid[i-1].getCid() + ": " +
                            cid[i].getLastErrorMessage();

        System.err.println(text);
        break;
    }
}

To unstack protocol services:

for (i = cid.length - 1; i > 0; i--)
{
    if (!cid[i].pop())
    {
        String text = "Failure popping CID " +
                            cid[i].getCid() + " off of " +
                            cid[i-1].getCid() + ": " +
                            cid[i].getLastErrorMessage();

        System.err.println(text);
        break;
    }
}

To connect a protocol service:

if (cid.connectRequest())
{
    /* Check for events */
}
else
{
    String text = "Failure connecting on CID " +
                        cid.getCid() + ": " +
                        cid.getLastErrorMessage();

    System.err.println(text);
}

To disconnect a protocol service:

if (cid.disconnectRequest())
{
    /* Check for events */
}
else
{
    String text = "Failure disconnecting on CID " +
                        cid.getCid() + ": " +
                        cid.getLastErrorMessage();

    System.err.println(text);
}

To check for an event:

ProtocolEvent event = cid.event();

if (event != null)
{
    /* Switch on event.getType() */
}
else
{
    System.err.println("No event received.");
}

To read data:

try
{
    DataEvent data = cid.read();

    if (data != null)
    {
        /* Process data.getDataSize() bytes of data */
    }
}
catch (ProtocolEventException event)
{
    switch (event.getType())
    {
        case Laygo.EVENT_DATA_LENGTH_ERROR:
        case Laygo.EVENT_DATA_CRC_ERROR:
        case Laygo.EVENT_DATA_ABORTED_FRAME:

            /* Discard bad frame */
            event = null;
            break;

        default:

            throw event;
    }
}

To write data:

if (cid.write(buffer, size))
{
    /* size bytes of data written */
}
else
}
    String text = "Failure writing on CID " +
                        cid.getCid() + ": " +
                        cid.getLastErrorMessage();

    System.err.println(text);
}