4 * cupsGetDevices implementation for the Common UNIX Printing System (CUPS).
6 * Copyright 2008-2009 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...
30 * 'cupsGetDevices()' - Get available printer devices.
32 * This function sends a CUPS-Get-Devices request and streams the discovered
33 * devices to the specified callback function. The "timeout" parameter controls
34 * how long the request lasts, while the "include_schemes" and "exclude_schemes"
35 * parameters provide comma-delimited lists of backends to include or omit from
36 * the request respectively.
41 ipp_status_t
/* O - Request status - @code IPP_OK@ on success. */
43 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
44 int timeout
, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
45 const char *include_schemes
, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */
46 const char *exclude_schemes
, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
47 cups_device_cb_t callback
, /* I - Callback function */
48 void *user_data
) /* I - User data pointer */
50 ipp_t
*request
, /* CUPS-Get-Devices request */
51 *response
; /* CUPS-Get-Devices response */
52 ipp_attribute_t
*attr
; /* Current attribute */
53 const char *device_class
, /* device-class value */
54 *device_id
, /* device-id value */
55 *device_info
, /* device-info value */
56 *device_location
, /* device-location value */
57 *device_make_and_model
, /* device-make-and-model value */
58 *device_uri
; /* device-uri value */
59 int blocking
; /* Current blocking-IO mode */
60 cups_option_t option
; /* in/exclude-schemes option */
61 http_status_t status
; /* HTTP status of request */
62 ipp_state_t state
; /* IPP response state */
66 * Range check input...
69 DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", "
70 "exclude_schemes=\"%s\", callback=%p, user_data=%p)", http
,
71 timeout
, include_schemes
, exclude_schemes
, callback
,
75 return (IPP_INTERNAL_ERROR
);
78 http
= _cupsConnect();
81 return (IPP_SERVICE_UNAVAILABLE
);
84 * Create a CUPS-Get-Devices request...
87 request
= ippNewRequest(CUPS_GET_DEVICES
);
90 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "timeout",
95 option
.name
= "include-schemes";
96 option
.value
= (char *)include_schemes
;
98 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
103 option
.name
= "exclude-schemes";
104 option
.value
= (char *)exclude_schemes
;
106 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
110 * Send the request and do any necessary authentication...
115 DEBUG_puts("2cupsGetDevices: Sending request...");
116 status
= cupsSendRequest(http
, request
, "/", ippLength(request
));
118 DEBUG_puts("2cupsGetDevices: Waiting for response status...");
119 while (status
== HTTP_CONTINUE
)
120 status
= httpUpdate(http
);
122 if (status
!= HTTP_OK
)
126 if (status
== HTTP_UNAUTHORIZED
)
129 * See if we can do authentication...
132 DEBUG_puts("2cupsGetDevices: Need authorization...");
134 if (!cupsDoAuthentication(http
, "POST", "/"))
141 else if (status
== HTTP_UPGRADE_REQUIRED
)
144 * Force a reconnect with encryption...
147 DEBUG_puts("2cupsGetDevices: Need encryption...");
149 if (!httpReconnect(http
))
150 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
152 #endif /* HAVE_SSL */
155 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
157 DEBUG_printf(("2cupsGetDevices: status=%d", status
));
161 if (status
!= HTTP_OK
)
163 _cupsSetHTTPError(status
);
164 return (cupsLastError());
168 * Read the response in non-blocking mode...
171 blocking
= httpGetBlocking(http
);
172 httpBlocking(http
, 0);
178 device_location
= "";
179 device_make_and_model
= NULL
;
183 DEBUG_puts("2cupsGetDevices: Reading response...");
187 if ((state
= ippRead(http
, response
)) == IPP_ERROR
)
190 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state
,
193 if (!response
->attrs
)
196 while (attr
!= response
->last
)
199 attr
= response
->attrs
;
203 DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
204 attr
->name
, attr
->value_tag
));
208 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
210 (*callback
)(device_class
, device_id
, device_info
,
211 device_make_and_model
, device_uri
, device_location
,
217 device_location
= "";
218 device_make_and_model
= NULL
;
221 else if (!strcmp(attr
->name
, "device-class") &&
222 attr
->value_tag
== IPP_TAG_KEYWORD
)
223 device_class
= attr
->values
[0].string
.text
;
224 else if (!strcmp(attr
->name
, "device-id") &&
225 attr
->value_tag
== IPP_TAG_TEXT
)
226 device_id
= attr
->values
[0].string
.text
;
227 else if (!strcmp(attr
->name
, "device-info") &&
228 attr
->value_tag
== IPP_TAG_TEXT
)
229 device_info
= attr
->values
[0].string
.text
;
230 else if (!strcmp(attr
->name
, "device-location") &&
231 attr
->value_tag
== IPP_TAG_TEXT
)
232 device_location
= attr
->values
[0].string
.text
;
233 else if (!strcmp(attr
->name
, "device-make-and-model") &&
234 attr
->value_tag
== IPP_TAG_TEXT
)
235 device_make_and_model
= attr
->values
[0].string
.text
;
236 else if (!strcmp(attr
->name
, "device-uri") &&
237 attr
->value_tag
== IPP_TAG_URI
)
238 device_uri
= attr
->values
[0].string
.text
;
241 while (state
!= IPP_DATA
);
243 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state
,
246 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
248 (*callback
)(device_class
, device_id
, device_info
,
249 device_make_and_model
, device_uri
, device_location
, user_data
);
252 * Set the IPP status and return...
255 httpBlocking(http
, blocking
);
258 if (status
== IPP_ERROR
)
259 _cupsSetError(IPP_ERROR
, NULL
, 0);
262 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
264 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
265 ippErrorString(response
->request
.status
.status_code
),
266 attr
? attr
->values
[0].string
.text
: ""));
268 _cupsSetError(response
->request
.status
.status_code
,
269 attr
? attr
->values
[0].string
.text
:
270 ippErrorString(response
->request
.status
.status_code
), 0);
275 return (cupsLastError());