2 * cupsGetDevices implementation for CUPS.
4 * Copyright 2008-2016 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include "cups-private.h"
14 #include "debug-internal.h"
15 #include "adminutil.h"
19 * 'cupsGetDevices()' - Get available printer devices.
21 * This function sends a CUPS-Get-Devices request and streams the discovered
22 * devices to the specified callback function. The "timeout" parameter controls
23 * how long the request lasts, while the "include_schemes" and "exclude_schemes"
24 * parameters provide comma-delimited lists of backends to include or omit from
25 * the request respectively.
27 * This function is deprecated with the IPP printer discovery functionality
28 * being provided by the @link cupsEnumDests@ and @cupsGetDests@ functions.
33 ipp_status_t /* O - Request status - @code IPP_OK@ on success. */
35 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
36 int timeout, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
37 const char *include_schemes, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */
38 const char *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
39 cups_device_cb_t callback, /* I - Callback function */
40 void *user_data) /* I - User data pointer */
42 ipp_t *request, /* CUPS-Get-Devices request */
43 *response; /* CUPS-Get-Devices response */
44 ipp_attribute_t *attr; /* Current attribute */
45 const char *device_class, /* device-class value */
46 *device_id, /* device-id value */
47 *device_info, /* device-info value */
48 *device_location, /* device-location value */
49 *device_make_and_model, /* device-make-and-model value */
50 *device_uri; /* device-uri value */
51 int blocking; /* Current blocking-IO mode */
52 cups_option_t option; /* in/exclude-schemes option */
53 http_status_t status; /* HTTP status of request */
54 ipp_state_t state; /* IPP response state */
58 * Range check input...
61 DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", exclude_schemes=\"%s\", callback=%p, user_data=%p)", (void *)http, timeout, include_schemes, exclude_schemes, (void *)callback, user_data));
64 return (IPP_STATUS_ERROR_INTERNAL);
67 http = _cupsConnect();
70 return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE);
73 * Create a CUPS-Get-Devices request...
76 request = ippNewRequest(IPP_OP_CUPS_GET_DEVICES);
79 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout",
84 option.name = "include-schemes";
85 option.value = (char *)include_schemes;
87 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
92 option.name = "exclude-schemes";
93 option.value = (char *)exclude_schemes;
95 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
99 * Send the request and do any necessary authentication...
104 DEBUG_puts("2cupsGetDevices: Sending request...");
105 status = cupsSendRequest(http, request, "/", ippLength(request));
107 DEBUG_puts("2cupsGetDevices: Waiting for response status...");
108 while (status == HTTP_STATUS_CONTINUE)
109 status = httpUpdate(http);
111 if (status != HTTP_STATUS_OK)
115 if (status == HTTP_STATUS_UNAUTHORIZED)
118 * See if we can do authentication...
121 DEBUG_puts("2cupsGetDevices: Need authorization...");
123 if (!cupsDoAuthentication(http, "POST", "/"))
124 httpReconnect2(http, 30000, NULL);
127 status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
133 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
136 * Force a reconnect with encryption...
139 DEBUG_puts("2cupsGetDevices: Need encryption...");
141 if (!httpReconnect2(http, 30000, NULL))
142 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
144 #endif /* HAVE_SSL */
147 while (status == HTTP_STATUS_UNAUTHORIZED ||
148 status == HTTP_STATUS_UPGRADE_REQUIRED);
150 DEBUG_printf(("2cupsGetDevices: status=%d", status));
154 if (status != HTTP_STATUS_OK)
156 _cupsSetHTTPError(status);
157 return (cupsLastError());
161 * Read the response in non-blocking mode...
164 blocking = httpGetBlocking(http);
165 httpBlocking(http, 0);
171 device_location = "";
172 device_make_and_model = NULL;
176 DEBUG_puts("2cupsGetDevices: Reading response...");
180 if ((state = ippRead(http, response)) == IPP_STATE_ERROR)
183 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, (void *)response->last));
185 if (!response->attrs)
188 while (attr != response->last)
191 attr = response->attrs;
195 DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
196 attr->name, attr->value_tag));
200 if (device_class && device_id && device_info && device_make_and_model &&
202 (*callback)(device_class, device_id, device_info,
203 device_make_and_model, device_uri, device_location,
209 device_location = "";
210 device_make_and_model = NULL;
213 else if (!strcmp(attr->name, "device-class") &&
214 attr->value_tag == IPP_TAG_KEYWORD)
215 device_class = attr->values[0].string.text;
216 else if (!strcmp(attr->name, "device-id") &&
217 attr->value_tag == IPP_TAG_TEXT)
218 device_id = attr->values[0].string.text;
219 else if (!strcmp(attr->name, "device-info") &&
220 attr->value_tag == IPP_TAG_TEXT)
221 device_info = attr->values[0].string.text;
222 else if (!strcmp(attr->name, "device-location") &&
223 attr->value_tag == IPP_TAG_TEXT)
224 device_location = attr->values[0].string.text;
225 else if (!strcmp(attr->name, "device-make-and-model") &&
226 attr->value_tag == IPP_TAG_TEXT)
227 device_make_and_model = attr->values[0].string.text;
228 else if (!strcmp(attr->name, "device-uri") &&
229 attr->value_tag == IPP_TAG_URI)
230 device_uri = attr->values[0].string.text;
233 while (state != IPP_STATE_DATA);
235 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, (void *)response->last));
237 if (device_class && device_id && device_info && device_make_and_model &&
239 (*callback)(device_class, device_id, device_info,
240 device_make_and_model, device_uri, device_location, user_data);
243 * Set the IPP status and return...
246 httpBlocking(http, blocking);
249 if (status == HTTP_STATUS_ERROR)
250 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0);
253 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
255 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
256 ippErrorString(response->request.status.status_code),
257 attr ? attr->values[0].string.text : ""));
259 _cupsSetError(response->request.status.status_code,
260 attr ? attr->values[0].string.text :
261 ippErrorString(response->request.status.status_code), 0);
266 return (cupsLastError());