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.
21 * Include necessary headers...
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 "exclude_schemes" parameter provides
34 * a comma-delimited list of backends to omit from the request.
39 ipp_status_t
/* O - Request status - @code IPP_OK@ on success. */
41 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
42 int timeout
, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
43 const char *exclude_schemes
, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
44 cups_device_cb_t callback
, /* I - Callback function */
45 void *user_data
) /* I - User data pointer */
47 ipp_t
*request
, /* CUPS-Get-Devices request */
48 *response
; /* CUPS-Get-Devices response */
49 ipp_attribute_t
*attr
; /* Current attribute */
50 const char *device_class
, /* device-class value */
51 *device_id
, /* device-id value */
52 *device_info
, /* device-info value */
53 *device_make_and_model
, /* device-make-and-model value */
54 *device_uri
; /* device-uri value */
55 int blocking
; /* Current blocking-IO mode */
56 cups_option_t option
; /* exclude-schemes option */
57 http_status_t status
; /* HTTP status of request */
58 ipp_state_t state
; /* IPP response state */
62 * Range check input...
66 return (IPP_INTERNAL_ERROR
);
69 http
= _cupsConnect();
72 return (IPP_SERVICE_UNAVAILABLE
);
75 * Create a CUPS-Get-Devices request...
78 request
= ippNewRequest(CUPS_GET_DEVICES
);
81 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "timeout",
86 option
.name
= "exclude-schemes";
87 option
.value
= (char *)exclude_schemes
;
89 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
93 * Send the request and do any necessary authentication...
98 DEBUG_puts("cupsGetDevices: Sending request...");
99 status
= cupsSendRequest(http
, request
, "/", ippLength(request
));
101 DEBUG_puts("cupsGetDevices: Waiting for response status...");
102 while (status
== HTTP_CONTINUE
)
103 status
= httpUpdate(http
);
105 if (status
!= HTTP_OK
)
109 if (status
== HTTP_UNAUTHORIZED
)
112 * See if we can do authentication...
117 DEBUG_puts("cupsGetDevices: Need authorization...");
119 if ((auth_result
= cupsDoAuthentication(http
, "POST", "/")) == 0)
121 else if (auth_result
< 0)
122 http
->status
= status
= HTTP_FORBIDDEN
;
126 else if (status
== HTTP_UPGRADE_REQUIRED
)
129 * Force a reconnect with encryption...
132 DEBUG_puts("cupsGetDevices: Need encryption...");
134 if (!httpReconnect(http
))
135 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
137 #endif /* HAVE_SSL */
140 while (status
== HTTP_UNAUTHORIZED
|| status
== HTTP_UPGRADE_REQUIRED
);
142 DEBUG_printf(("cupsGetDevices: status=%d\n", status
));
146 if (status
!= HTTP_OK
)
148 _cupsSetHTTPError(status
);
149 return (cupsLastError());
153 * Read the response in non-blocking mode...
156 blocking
= httpGetBlocking(http
);
157 httpBlocking(http
, 0);
163 device_make_and_model
= NULL
;
167 DEBUG_puts("cupsGetDevices: Reading response...");
171 if ((state
= ippRead(http
, response
)) == IPP_ERROR
)
174 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state
,
177 if (!response
->attrs
)
180 while (attr
!= response
->last
)
183 attr
= response
->attrs
;
187 DEBUG_printf(("cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d\n",
188 attr
->name
? attr
->name
: "(null)", attr
->value_tag
));
192 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
194 (*callback
)(device_class
, device_id
, device_info
,
195 device_make_and_model
, device_uri
, user_data
);
200 device_make_and_model
= NULL
;
203 else if (!strcmp(attr
->name
, "device-class") &&
204 attr
->value_tag
== IPP_TAG_KEYWORD
)
205 device_class
= attr
->values
[0].string
.text
;
206 else if (!strcmp(attr
->name
, "device-id") &&
207 attr
->value_tag
== IPP_TAG_TEXT
)
208 device_id
= attr
->values
[0].string
.text
;
209 else if (!strcmp(attr
->name
, "device-info") &&
210 attr
->value_tag
== IPP_TAG_TEXT
)
211 device_info
= attr
->values
[0].string
.text
;
212 else if (!strcmp(attr
->name
, "device-make-and-model") &&
213 attr
->value_tag
== IPP_TAG_TEXT
)
214 device_make_and_model
= attr
->values
[0].string
.text
;
215 else if (!strcmp(attr
->name
, "device-uri") &&
216 attr
->value_tag
== IPP_TAG_URI
)
217 device_uri
= attr
->values
[0].string
.text
;
220 while (state
!= IPP_DATA
);
222 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state
,
225 if (device_class
&& device_id
&& device_info
&& device_make_and_model
&&
227 (*callback
)(device_class
, device_id
, device_info
,
228 device_make_and_model
, device_uri
, user_data
);
231 * Set the IPP status and return...
234 httpBlocking(http
, blocking
);
236 if (status
== IPP_ERROR
)
237 _cupsSetError(IPP_ERROR
, NULL
);
240 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
242 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"\n",
243 ippErrorString(response
->request
.status
.status_code
),
244 attr
? attr
->values
[0].string
.text
: ""));
246 _cupsSetError(response
->request
.status
.status_code
,
247 attr
? attr
->values
[0].string
.text
:
248 ippErrorString(response
->request
.status
.status_code
));
253 return (cupsLastError());