4 * cupsGetDevices implementation for the Common UNIX Printing System (CUPS).
6 * Copyright 2008 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 "exclude_schemes" parameter provides
35 * a comma-delimited list of backends to omit from the request.
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 *exclude_schemes
, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
45 cups_device_cb_t callback
, /* I - Callback function */
46 void *user_data
) /* I - User data pointer */
48 ipp_t
*request
, /* CUPS-Get-Devices request */
49 *response
; /* CUPS-Get-Devices response */
50 ipp_attribute_t
*attr
; /* Current attribute */
51 const char *device_class
, /* device-class value */
52 *device_id
, /* device-id value */
53 *device_info
, /* device-info value */
54 *device_location
, /* device-location value */
55 *device_make_and_model
, /* device-make-and-model value */
56 *device_uri
; /* device-uri value */
57 int blocking
; /* Current blocking-IO mode */
58 cups_option_t option
; /* exclude-schemes option */
59 http_status_t status
; /* HTTP status of request */
60 ipp_state_t state
; /* IPP response state */
64 * Range check input...
68 return (IPP_INTERNAL_ERROR
);
71 http
= _cupsConnect();
74 return (IPP_SERVICE_UNAVAILABLE
);
77 * Create a CUPS-Get-Devices request...
80 request
= ippNewRequest(CUPS_GET_DEVICES
);
83 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "timeout",
88 option
.name
= "exclude-schemes";
89 option
.value
= (char *)exclude_schemes
;
91 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
95 * Send the request and do any necessary authentication...
100 DEBUG_puts("cupsGetDevices: Sending request...");
101 status
= cupsSendRequest(http
, request
, "/", ippLength(request
));
103 DEBUG_puts("cupsGetDevices: Waiting for response status...");
104 while (status
== HTTP_CONTINUE
)
105 status
= httpUpdate(http
);
107 if (status
!= HTTP_OK
)
111 if (status
== HTTP_UNAUTHORIZED
)
114 * See if we can do authentication...
119 DEBUG_puts("cupsGetDevices: Need authorization...");
121 if ((auth_result
= cupsDoAuthentication(http
, "POST", "/")) == 0)
123 else if (auth_result
< 0)
124 http
->status
= status
= HTTP_FORBIDDEN
;
128 else if (status
== HTTP_UPGRADE_REQUIRED
)
131 * Force a reconnect with encryption...
134 DEBUG_puts("cupsGetDevices: Need encryption...");
136 if (!httpReconnect(http
))
137 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
139 #endif /* HAVE_SSL */
142 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
144 DEBUG_printf(("cupsGetDevices: status=%d\n", status
));
148 if (status
!= HTTP_OK
)
150 _cupsSetHTTPError(status
);
151 return (cupsLastError());
155 * Read the response in non-blocking mode...
158 blocking
= httpGetBlocking(http
);
159 httpBlocking(http
, 0);
165 device_location
= "";
166 device_make_and_model
= NULL
;
170 DEBUG_puts("cupsGetDevices: Reading response...");
174 if ((state
= ippRead(http
, response
)) == IPP_ERROR
)
177 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state
,
180 if (!response
->attrs
)
183 while (attr
!= response
->last
)
186 attr
= response
->attrs
;
190 DEBUG_printf(("cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d\n",
191 attr
->name
? attr
->name
: "(null)", attr
->value_tag
));
195 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
197 (*callback
)(device_class
, device_id
, device_info
,
198 device_make_and_model
, device_uri
, device_location
,
204 device_location
= "";
205 device_make_and_model
= NULL
;
208 else if (!strcmp(attr
->name
, "device-class") &&
209 attr
->value_tag
== IPP_TAG_KEYWORD
)
210 device_class
= attr
->values
[0].string
.text
;
211 else if (!strcmp(attr
->name
, "device-id") &&
212 attr
->value_tag
== IPP_TAG_TEXT
)
213 device_id
= attr
->values
[0].string
.text
;
214 else if (!strcmp(attr
->name
, "device-info") &&
215 attr
->value_tag
== IPP_TAG_TEXT
)
216 device_info
= attr
->values
[0].string
.text
;
217 else if (!strcmp(attr
->name
, "device-location") &&
218 attr
->value_tag
== IPP_TAG_TEXT
)
219 device_location
= attr
->values
[0].string
.text
;
220 else if (!strcmp(attr
->name
, "device-make-and-model") &&
221 attr
->value_tag
== IPP_TAG_TEXT
)
222 device_make_and_model
= attr
->values
[0].string
.text
;
223 else if (!strcmp(attr
->name
, "device-uri") &&
224 attr
->value_tag
== IPP_TAG_URI
)
225 device_uri
= attr
->values
[0].string
.text
;
228 while (state
!= IPP_DATA
);
230 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state
,
233 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
235 (*callback
)(device_class
, device_id
, device_info
,
236 device_make_and_model
, device_uri
, device_location
, user_data
);
239 * Set the IPP status and return...
242 httpBlocking(http
, blocking
);
245 if (status
== IPP_ERROR
)
246 _cupsSetError(IPP_ERROR
, NULL
, 0);
249 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
251 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"\n",
252 ippErrorString(response
->request
.status
.status_code
),
253 attr
? attr
->values
[0].string
.text
: ""));
255 _cupsSetError(response
->request
.status
.status_code
,
256 attr
? attr
->values
[0].string
.text
:
257 ippErrorString(response
->request
.status
.status_code
), 0);
262 return (cupsLastError());