USB Libraries Help > USB Host Libraries > USB CDC Host Library > Using the Library > How the Library Works > Opening the CDC Host Client Driver
MPLAB Harmony USB Stack
Opening the CDC Host Client Driver

The application must open the CDC Host Client Driver to communicate and control the attached device. The device can be opened by using the USB_HOST_CDC_Open function and specifying the USB_HOST_CDC_OBJ object that was returned in the attached event handler. If the open function fails, it returns an invalid handle (USB_HOST_CDC_HANDLE_INVALID). Once opened successfully, a valid handle tracks the relationship between the client and the CDC Host Client Driver. This handle should be used with other CDC Host Client Driver functions to specify the instance of the CDC Host Client Driver being accessed. 

A CDC Host Client Driver instance can be opened multiple times by different application clients. In an ROTS based application each client could running its own thread. Multiple clients can read write data to the one CDC device. In such a case, the read and write requests are queued. The following code shows an example of how the CDC Driver is opened. 

Example:  

/* This code shows an example of the how to open the CDC Host Client
 * driver. The application state machine waits for a device attach and then
 * opens the CDC Host Client Driver. */

void APP_USBHostCDCAttachEventListener(USB_HOST_CDC_OBJ cdcObj, uintptr_t context)
{
    /* This function gets called when the CDC device is attached. Update the
     * application data structure to let the application know that this device
     * is attached */

    appData.deviceIsAttached = true;
    appData.cdcObj = cdcObj;
}

void APP_Tasks(void)
{
    switch (appData.state)
    {
        case APP_STATE_BUS_ENABLE:

            /* In this state the application enables the USB Host Bus. Note
             * how the CDC Attach event handler are registered before the bus
             * is enabled. */

            USB_HOST_EventHandlerSet(APP_USBHostEventHandler, (uintptr_t)0);
            USB_HOST_CDC_AttachEventHandlerSet(APP_USBHostCDCAttachEventListener, (uintptr_t) 0);
            USB_HOST_BusEnable(0);
            appData.state = APP_STATE_WAIT_FOR_BUS_ENABLE_COMPLETE;
            break;

        case APP_STATE_WAIT_FOR_BUS_ENABLE_COMPLETE:

            /* In this state we wait for the Bus enable to complete */
            if(USB_HOST_BusIsEnabled(0))
            {
                appData.state = APP_STATE_WAIT_FOR_DEVICE_ATTACH;
            }
            break;

        case APP_STATE_WAIT_FOR_DEVICE_ATTACH:

            /* In this state the application is waiting for the device to be
             * attached */
            if(appData.deviceIsAttached)
            {
                /* A device is attached. We can open this device */
                appData.state = APP_STATE_OPEN_DEVICE;
                appData.deviceIsAttached = false;
            }
            break;

        case APP_STATE_OPEN_DEVICE:

            /* In this state the application opens the attached device */
            appData.cdcHostHandle = USB_HOST_CDC_Open(appData.cdcObj);
            if(appData.cdcHostHandle != USB_HOST_CDC_HANDLE_INVALID)
            {
                /* The driver was opened successfully. Set the event handler
                 * and then go to the next state. */
                USB_HOST_CDC_EventHandlerSet(appData.cdcHostHandle,
                                             APP_USBHostCDCEventHandler,
                                            (uintptr_t)0);
                appData.state = APP_STATE_SET_LINE_CODING;
            }
            break;

        default:
            break;
    }
}