4 * cupsGetDevices implementation for CUPS.
6 * Copyright 2008-2010 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * cupsGetDevices() - Get available printer devices.
22 * Include necessary headers...
25 #include "cups-private.h"
29 * 'cupsGetDevices()' - Get available printer devices.
31 * This function sends a CUPS-Get-Devices request and streams the discovered
32 * devices to the specified callback function. The "timeout" parameter controls
33 * how long the request lasts, while the "include_schemes" and "exclude_schemes"
34 * parameters provide comma-delimited lists of backends to include or omit from
35 * the request respectively.
37 * @since CUPS 1.4/Mac OS X 10.6@
40 ipp_status_t
/* O - Request status - @code IPP_OK@ on success. */
42 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
43 int timeout
, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
44 const char *include_schemes
, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */
45 const char *exclude_schemes
, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
46 cups_device_cb_t callback
, /* I - Callback function */
47 void *user_data
) /* I - User data pointer */
49 ipp_t
*request
, /* CUPS-Get-Devices request */
50 *response
; /* CUPS-Get-Devices response */
51 ipp_attribute_t
*attr
; /* Current attribute */
52 const char *device_class
, /* device-class value */
53 *device_id
, /* device-id value */
54 *device_info
, /* device-info value */
55 *device_location
, /* device-location value */
56 *device_make_and_model
, /* device-make-and-model value */
57 *device_uri
; /* device-uri value */
58 int blocking
; /* Current blocking-IO mode */
59 cups_option_t option
; /* in/exclude-schemes option */
60 http_status_t status
; /* HTTP status of request */
61 ipp_state_t state
; /* IPP response state */
65 * Range check input...
68 DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", "
69 "exclude_schemes=\"%s\", callback=%p, user_data=%p)", http
,
70 timeout
, include_schemes
, exclude_schemes
, callback
,
74 return (IPP_INTERNAL_ERROR
);
77 http
= _cupsConnect();
80 return (IPP_SERVICE_UNAVAILABLE
);
83 * Create a CUPS-Get-Devices request...
86 request
= ippNewRequest(CUPS_GET_DEVICES
);
89 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "timeout",
94 option
.name
= "include-schemes";
95 option
.value
= (char *)include_schemes
;
97 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
102 option
.name
= "exclude-schemes";
103 option
.value
= (char *)exclude_schemes
;
105 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
109 * Send the request and do any necessary authentication...
114 DEBUG_puts("2cupsGetDevices: Sending request...");
115 status
= cupsSendRequest(http
, request
, "/", ippLength(request
));
117 DEBUG_puts("2cupsGetDevices: Waiting for response status...");
118 while (status
== HTTP_CONTINUE
)
119 status
= httpUpdate(http
);
121 if (status
!= HTTP_OK
)
125 if (status
== HTTP_UNAUTHORIZED
)
128 * See if we can do authentication...
131 DEBUG_puts("2cupsGetDevices: Need authorization...");
133 if (!cupsDoAuthentication(http
, "POST", "/"))
137 status
= HTTP_AUTHORIZATION_CANCELED
;
143 else if (status
== HTTP_UPGRADE_REQUIRED
)
146 * Force a reconnect with encryption...
149 DEBUG_puts("2cupsGetDevices: Need encryption...");
151 if (!httpReconnect(http
))
152 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
154 #endif /* HAVE_SSL */
157 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
159 DEBUG_printf(("2cupsGetDevices: status=%d", status
));
163 if (status
!= HTTP_OK
)
165 _cupsSetHTTPError(status
);
166 return (cupsLastError());
170 * Read the response in non-blocking mode...
173 blocking
= httpGetBlocking(http
);
174 httpBlocking(http
, 0);
180 device_location
= "";
181 device_make_and_model
= NULL
;
185 DEBUG_puts("2cupsGetDevices: Reading response...");
189 if ((state
= ippRead(http
, response
)) == IPP_ERROR
)
192 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state
,
195 if (!response
->attrs
)
198 while (attr
!= response
->last
)
201 attr
= response
->attrs
;
205 DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
206 attr
->name
, attr
->value_tag
));
210 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
212 (*callback
)(device_class
, device_id
, device_info
,
213 device_make_and_model
, device_uri
, device_location
,
219 device_location
= "";
220 device_make_and_model
= NULL
;
223 else if (!strcmp(attr
->name
, "device-class") &&
224 attr
->value_tag
== IPP_TAG_KEYWORD
)
225 device_class
= attr
->values
[0].string
.text
;
226 else if (!strcmp(attr
->name
, "device-id") &&
227 attr
->value_tag
== IPP_TAG_TEXT
)
228 device_id
= attr
->values
[0].string
.text
;
229 else if (!strcmp(attr
->name
, "device-info") &&
230 attr
->value_tag
== IPP_TAG_TEXT
)
231 device_info
= attr
->values
[0].string
.text
;
232 else if (!strcmp(attr
->name
, "device-location") &&
233 attr
->value_tag
== IPP_TAG_TEXT
)
234 device_location
= attr
->values
[0].string
.text
;
235 else if (!strcmp(attr
->name
, "device-make-and-model") &&
236 attr
->value_tag
== IPP_TAG_TEXT
)
237 device_make_and_model
= attr
->values
[0].string
.text
;
238 else if (!strcmp(attr
->name
, "device-uri") &&
239 attr
->value_tag
== IPP_TAG_URI
)
240 device_uri
= attr
->values
[0].string
.text
;
243 while (state
!= IPP_DATA
);
245 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state
,
248 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
250 (*callback
)(device_class
, device_id
, device_info
,
251 device_make_and_model
, device_uri
, device_location
, user_data
);
254 * Set the IPP status and return...
257 httpBlocking(http
, blocking
);
260 if (status
== HTTP_ERROR
)
261 _cupsSetError(IPP_ERROR
, NULL
, 0);
264 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
266 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
267 ippErrorString(response
->request
.status
.status_code
),
268 attr
? attr
->values
[0].string
.text
: ""));
270 _cupsSetError(response
->request
.status
.status_code
,
271 attr
? attr
->values
[0].string
.text
:
272 ippErrorString(response
->request
.status
.status_code
), 0);
277 return (cupsLastError());