2 * cupsGetDevices implementation for CUPS.
4 * Copyright 2008-2013 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * file is missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
19 #include "cups-private.h"
23 * 'cupsGetDevices()' - Get available printer devices.
25 * This function sends a CUPS-Get-Devices request and streams the discovered
26 * devices to the specified callback function. The "timeout" parameter controls
27 * how long the request lasts, while the "include_schemes" and "exclude_schemes"
28 * parameters provide comma-delimited lists of backends to include or omit from
29 * the request respectively.
31 * @since CUPS 1.4/OS X 10.6@
34 ipp_status_t
/* O - Request status - @code IPP_OK@ on success. */
36 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
37 int timeout
, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
38 const char *include_schemes
, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */
39 const char *exclude_schemes
, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
40 cups_device_cb_t callback
, /* I - Callback function */
41 void *user_data
) /* I - User data pointer */
43 ipp_t
*request
, /* CUPS-Get-Devices request */
44 *response
; /* CUPS-Get-Devices response */
45 ipp_attribute_t
*attr
; /* Current attribute */
46 const char *device_class
, /* device-class value */
47 *device_id
, /* device-id value */
48 *device_info
, /* device-info value */
49 *device_location
, /* device-location value */
50 *device_make_and_model
, /* device-make-and-model value */
51 *device_uri
; /* device-uri value */
52 int blocking
; /* Current blocking-IO mode */
53 cups_option_t option
; /* in/exclude-schemes option */
54 http_status_t status
; /* HTTP status of request */
55 ipp_state_t state
; /* IPP response state */
59 * Range check input...
62 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
));
65 return (IPP_STATUS_ERROR_INTERNAL
);
68 http
= _cupsConnect();
71 return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
);
74 * Create a CUPS-Get-Devices request...
77 request
= ippNewRequest(IPP_OP_CUPS_GET_DEVICES
);
80 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "timeout",
85 option
.name
= "include-schemes";
86 option
.value
= (char *)include_schemes
;
88 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
93 option
.name
= "exclude-schemes";
94 option
.value
= (char *)exclude_schemes
;
96 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
100 * Send the request and do any necessary authentication...
105 DEBUG_puts("2cupsGetDevices: Sending request...");
106 status
= cupsSendRequest(http
, request
, "/", ippLength(request
));
108 DEBUG_puts("2cupsGetDevices: Waiting for response status...");
109 while (status
== HTTP_STATUS_CONTINUE
)
110 status
= httpUpdate(http
);
112 if (status
!= HTTP_STATUS_OK
)
116 if (status
== HTTP_STATUS_UNAUTHORIZED
)
119 * See if we can do authentication...
122 DEBUG_puts("2cupsGetDevices: Need authorization...");
124 if (!cupsDoAuthentication(http
, "POST", "/"))
125 httpReconnect2(http
, 30000, NULL
);
128 status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
134 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
137 * Force a reconnect with encryption...
140 DEBUG_puts("2cupsGetDevices: Need encryption...");
142 if (!httpReconnect2(http
, 30000, NULL
))
143 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
145 #endif /* HAVE_SSL */
148 while (status
== HTTP_STATUS_UNAUTHORIZED
||
149 status
== HTTP_STATUS_UPGRADE_REQUIRED
);
151 DEBUG_printf(("2cupsGetDevices: status=%d", status
));
155 if (status
!= HTTP_STATUS_OK
)
157 _cupsSetHTTPError(status
);
158 return (cupsLastError());
162 * Read the response in non-blocking mode...
165 blocking
= httpGetBlocking(http
);
166 httpBlocking(http
, 0);
172 device_location
= "";
173 device_make_and_model
= NULL
;
177 DEBUG_puts("2cupsGetDevices: Reading response...");
181 if ((state
= ippRead(http
, response
)) == IPP_STATE_ERROR
)
184 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state
, (void *)response
->last
));
186 if (!response
->attrs
)
189 while (attr
!= response
->last
)
192 attr
= response
->attrs
;
196 DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
197 attr
->name
, attr
->value_tag
));
201 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
203 (*callback
)(device_class
, device_id
, device_info
,
204 device_make_and_model
, device_uri
, device_location
,
210 device_location
= "";
211 device_make_and_model
= NULL
;
214 else if (!strcmp(attr
->name
, "device-class") &&
215 attr
->value_tag
== IPP_TAG_KEYWORD
)
216 device_class
= attr
->values
[0].string
.text
;
217 else if (!strcmp(attr
->name
, "device-id") &&
218 attr
->value_tag
== IPP_TAG_TEXT
)
219 device_id
= attr
->values
[0].string
.text
;
220 else if (!strcmp(attr
->name
, "device-info") &&
221 attr
->value_tag
== IPP_TAG_TEXT
)
222 device_info
= attr
->values
[0].string
.text
;
223 else if (!strcmp(attr
->name
, "device-location") &&
224 attr
->value_tag
== IPP_TAG_TEXT
)
225 device_location
= attr
->values
[0].string
.text
;
226 else if (!strcmp(attr
->name
, "device-make-and-model") &&
227 attr
->value_tag
== IPP_TAG_TEXT
)
228 device_make_and_model
= attr
->values
[0].string
.text
;
229 else if (!strcmp(attr
->name
, "device-uri") &&
230 attr
->value_tag
== IPP_TAG_URI
)
231 device_uri
= attr
->values
[0].string
.text
;
234 while (state
!= IPP_STATE_DATA
);
236 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state
, (void *)response
->last
));
238 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
240 (*callback
)(device_class
, device_id
, device_info
,
241 device_make_and_model
, device_uri
, device_location
, user_data
);
244 * Set the IPP status and return...
247 httpBlocking(http
, blocking
);
250 if (status
== HTTP_STATUS_ERROR
)
251 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(http
->error
), 0);
254 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
256 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
257 ippErrorString(response
->request
.status
.status_code
),
258 attr
? attr
->values
[0].string
.text
: ""));
260 _cupsSetError(response
->request
.status
.status_code
,
261 attr
? attr
->values
[0].string
.text
:
262 ippErrorString(response
->request
.status
.status_code
), 0);
267 return (cupsLastError());