#include "cups-private.h"
ipp_status_t
cupsGetDevices(
http_t *http,
int timeout,
const char *include_schemes,
const char *exclude_schemes,
cups_device_cb_t callback,
void *user_data)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *device_class,
*device_id,
*device_info,
*device_location,
*device_make_and_model,
*device_uri;
int blocking;
cups_option_t option;
http_status_t status;
ipp_state_t state;
DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", "
"exclude_schemes=\"%s\", callback=%p, user_data=%p)", http,
timeout, include_schemes, exclude_schemes, callback,
user_data));
if (!callback)
return (IPP_INTERNAL_ERROR);
if (!http)
http = _cupsConnect();
if (!http)
return (IPP_SERVICE_UNAVAILABLE);
request = ippNewRequest(CUPS_GET_DEVICES);
if (timeout > 0)
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout",
timeout);
if (include_schemes)
{
option.name = "include-schemes";
option.value = (char *)include_schemes;
cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
}
if (exclude_schemes)
{
option.name = "exclude-schemes";
option.value = (char *)exclude_schemes;
cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
}
do
{
DEBUG_puts("2cupsGetDevices: Sending request...");
status = cupsSendRequest(http, request, "/", ippLength(request));
DEBUG_puts("2cupsGetDevices: Waiting for response status...");
while (status == HTTP_CONTINUE)
status = httpUpdate(http);
if (status != HTTP_OK)
{
httpFlush(http);
if (status == HTTP_UNAUTHORIZED)
{
DEBUG_puts("2cupsGetDevices: Need authorization...");
if (!cupsDoAuthentication(http, "POST", "/"))
httpReconnect(http);
else
{
status = HTTP_AUTHORIZATION_CANCELED;
break;
}
}
#ifdef HAVE_SSL
else if (status == HTTP_UPGRADE_REQUIRED)
{
DEBUG_puts("2cupsGetDevices: Need encryption...");
if (!httpReconnect(http))
httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
}
#endif
}
}
while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
DEBUG_printf(("2cupsGetDevices: status=%d", status));
ippDelete(request);
if (status != HTTP_OK)
{
_cupsSetHTTPError(status);
return (cupsLastError());
}
blocking = httpGetBlocking(http);
httpBlocking(http, 0);
response = ippNew();
device_class = NULL;
device_id = NULL;
device_info = NULL;
device_location = "";
device_make_and_model = NULL;
device_uri = NULL;
attr = NULL;
DEBUG_puts("2cupsGetDevices: Reading response...");
do
{
if ((state = ippRead(http, response)) == IPP_ERROR)
break;
DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state,
response->last));
if (!response->attrs)
continue;
while (attr != response->last)
{
if (!attr)
attr = response->attrs;
else
attr = attr->next;
DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
attr->name, attr->value_tag));
if (!attr->name)
{
if (device_class && device_id && device_info && device_make_and_model &&
device_uri)
(*callback)(device_class, device_id, device_info,
device_make_and_model, device_uri, device_location,
user_data);
device_class = NULL;
device_id = NULL;
device_info = NULL;
device_location = "";
device_make_and_model = NULL;
device_uri = NULL;
}
else if (!strcmp(attr->name, "device-class") &&
attr->value_tag == IPP_TAG_KEYWORD)
device_class = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-id") &&
attr->value_tag == IPP_TAG_TEXT)
device_id = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-info") &&
attr->value_tag == IPP_TAG_TEXT)
device_info = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-location") &&
attr->value_tag == IPP_TAG_TEXT)
device_location = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-make-and-model") &&
attr->value_tag == IPP_TAG_TEXT)
device_make_and_model = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-uri") &&
attr->value_tag == IPP_TAG_URI)
device_uri = attr->values[0].string.text;
}
}
while (state != IPP_DATA);
DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state,
response->last));
if (device_class && device_id && device_info && device_make_and_model &&
device_uri)
(*callback)(device_class, device_id, device_info,
device_make_and_model, device_uri, device_location, user_data);
httpBlocking(http, blocking);
httpFlush(http);
if (status == HTTP_ERROR)
_cupsSetError(IPP_INTERNAL_ERROR, strerror(http->error), 0);
else
{
attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
ippErrorString(response->request.status.status_code),
attr ? attr->values[0].string.text : ""));
_cupsSetError(response->request.status.status_code,
attr ? attr->values[0].string.text :
ippErrorString(response->request.status.status_code), 0);
}
ippDelete(response);
return (cupsLastError());
}