]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/getdevices.c
Merge changes from CUPS 1.4svn-r8162.
[thirdparty/cups.git] / cups / getdevices.c
1 /*
2 * "$Id$"
3 *
4 * cupsGetDevices implementation for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2008 by Apple Inc.
7 *
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/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * cupsGetDevices() - Get available printer devices.
19 */
20
21 /*
22 * Include necessary headers...
23 */
24
25 #include "globals.h"
26 #include "debug.h"
27
28
29 /*
30 * 'cupsGetDevices()' - Get available printer devices.
31 *
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.
37 *
38 * @since CUPS 1.4@
39 */
40
41 ipp_status_t /* O - Request status - @code IPP_OK@ on success. */
42 cupsGetDevices(
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 */
49 {
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 */
63
64
65 /*
66 * Range check input...
67 */
68
69 if (!callback)
70 return (IPP_INTERNAL_ERROR);
71
72 if (!http)
73 http = _cupsConnect();
74
75 if (!http)
76 return (IPP_SERVICE_UNAVAILABLE);
77
78 /*
79 * Create a CUPS-Get-Devices request...
80 */
81
82 request = ippNewRequest(CUPS_GET_DEVICES);
83
84 if (timeout > 0)
85 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout",
86 timeout);
87
88 if (include_schemes)
89 {
90 option.name = "include-schemes";
91 option.value = (char *)include_schemes;
92
93 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
94 }
95
96 if (exclude_schemes)
97 {
98 option.name = "exclude-schemes";
99 option.value = (char *)exclude_schemes;
100
101 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
102 }
103
104 /*
105 * Send the request and do any necessary authentication...
106 */
107
108 do
109 {
110 DEBUG_puts("cupsGetDevices: Sending request...");
111 status = cupsSendRequest(http, request, "/", ippLength(request));
112
113 DEBUG_puts("cupsGetDevices: Waiting for response status...");
114 while (status == HTTP_CONTINUE)
115 status = httpUpdate(http);
116
117 if (status != HTTP_OK)
118 {
119 httpFlush(http);
120
121 if (status == HTTP_UNAUTHORIZED)
122 {
123 /*
124 * See if we can do authentication...
125 */
126
127 int auth_result;
128
129 DEBUG_puts("cupsGetDevices: Need authorization...");
130
131 if ((auth_result = cupsDoAuthentication(http, "POST", "/")) == 0)
132 httpReconnect(http);
133 else if (auth_result < 0)
134 http->status = status = HTTP_FORBIDDEN;
135 }
136
137 #ifdef HAVE_SSL
138 else if (status == HTTP_UPGRADE_REQUIRED)
139 {
140 /*
141 * Force a reconnect with encryption...
142 */
143
144 DEBUG_puts("cupsGetDevices: Need encryption...");
145
146 if (!httpReconnect(http))
147 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
148 }
149 #endif /* HAVE_SSL */
150 }
151 }
152 while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
153
154 DEBUG_printf(("cupsGetDevices: status=%d\n", status));
155
156 ippDelete(request);
157
158 if (status != HTTP_OK)
159 {
160 _cupsSetHTTPError(status);
161 return (cupsLastError());
162 }
163
164 /*
165 * Read the response in non-blocking mode...
166 */
167
168 blocking = httpGetBlocking(http);
169 httpBlocking(http, 0);
170
171 response = ippNew();
172 device_class = NULL;
173 device_id = NULL;
174 device_info = NULL;
175 device_location = "";
176 device_make_and_model = NULL;
177 device_uri = NULL;
178 attr = NULL;
179
180 DEBUG_puts("cupsGetDevices: Reading response...");
181
182 do
183 {
184 if ((state = ippRead(http, response)) == IPP_ERROR)
185 break;
186
187 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state,
188 response->last));
189
190 if (!response->attrs)
191 continue;
192
193 while (attr != response->last)
194 {
195 if (!attr)
196 attr = response->attrs;
197 else
198 attr = attr->next;
199
200 DEBUG_printf(("cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d\n",
201 attr->name ? attr->name : "(null)", attr->value_tag));
202
203 if (!attr->name)
204 {
205 if (device_class && device_id && device_info && device_make_and_model &&
206 device_uri)
207 (*callback)(device_class, device_id, device_info,
208 device_make_and_model, device_uri, device_location,
209 user_data);
210
211 device_class = NULL;
212 device_id = NULL;
213 device_info = NULL;
214 device_location = "";
215 device_make_and_model = NULL;
216 device_uri = NULL;
217 }
218 else if (!strcmp(attr->name, "device-class") &&
219 attr->value_tag == IPP_TAG_KEYWORD)
220 device_class = attr->values[0].string.text;
221 else if (!strcmp(attr->name, "device-id") &&
222 attr->value_tag == IPP_TAG_TEXT)
223 device_id = attr->values[0].string.text;
224 else if (!strcmp(attr->name, "device-info") &&
225 attr->value_tag == IPP_TAG_TEXT)
226 device_info = attr->values[0].string.text;
227 else if (!strcmp(attr->name, "device-location") &&
228 attr->value_tag == IPP_TAG_TEXT)
229 device_location = attr->values[0].string.text;
230 else if (!strcmp(attr->name, "device-make-and-model") &&
231 attr->value_tag == IPP_TAG_TEXT)
232 device_make_and_model = attr->values[0].string.text;
233 else if (!strcmp(attr->name, "device-uri") &&
234 attr->value_tag == IPP_TAG_URI)
235 device_uri = attr->values[0].string.text;
236 }
237 }
238 while (state != IPP_DATA);
239
240 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state,
241 response->last));
242
243 if (device_class && device_id && device_info && device_make_and_model &&
244 device_uri)
245 (*callback)(device_class, device_id, device_info,
246 device_make_and_model, device_uri, device_location, user_data);
247
248 /*
249 * Set the IPP status and return...
250 */
251
252 httpBlocking(http, blocking);
253 httpFlush(http);
254
255 if (status == IPP_ERROR)
256 _cupsSetError(IPP_ERROR, NULL, 0);
257 else
258 {
259 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
260
261 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"\n",
262 ippErrorString(response->request.status.status_code),
263 attr ? attr->values[0].string.text : ""));
264
265 _cupsSetError(response->request.status.status_code,
266 attr ? attr->values[0].string.text :
267 ippErrorString(response->request.status.status_code), 0);
268 }
269
270 ippDelete(response);
271
272 return (cupsLastError());
273 }
274
275
276 /*
277 * End of "$Id$".
278 */