]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/getdevices.c
Merge changes from CUPS 1.4svn-r7874.
[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 "exclude_schemes" parameter provides
35 * a comma-delimited list of backends to omit from the request.
36 *
37 * @since CUPS 1.4@
38 */
39
40 ipp_status_t /* O - Request status - @code IPP_OK@ on success. */
41 cupsGetDevices(
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 */
47 {
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 */
61
62
63 /*
64 * Range check input...
65 */
66
67 if (!callback)
68 return (IPP_INTERNAL_ERROR);
69
70 if (!http)
71 http = _cupsConnect();
72
73 if (!http)
74 return (IPP_SERVICE_UNAVAILABLE);
75
76 /*
77 * Create a CUPS-Get-Devices request...
78 */
79
80 request = ippNewRequest(CUPS_GET_DEVICES);
81
82 if (timeout > 0)
83 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout",
84 timeout);
85
86 if (exclude_schemes)
87 {
88 option.name = "exclude-schemes";
89 option.value = (char *)exclude_schemes;
90
91 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
92 }
93
94 /*
95 * Send the request and do any necessary authentication...
96 */
97
98 do
99 {
100 DEBUG_puts("cupsGetDevices: Sending request...");
101 status = cupsSendRequest(http, request, "/", ippLength(request));
102
103 DEBUG_puts("cupsGetDevices: Waiting for response status...");
104 while (status == HTTP_CONTINUE)
105 status = httpUpdate(http);
106
107 if (status != HTTP_OK)
108 {
109 httpFlush(http);
110
111 if (status == HTTP_UNAUTHORIZED)
112 {
113 /*
114 * See if we can do authentication...
115 */
116
117 int auth_result;
118
119 DEBUG_puts("cupsGetDevices: Need authorization...");
120
121 if ((auth_result = cupsDoAuthentication(http, "POST", "/")) == 0)
122 httpReconnect(http);
123 else if (auth_result < 0)
124 http->status = status = HTTP_FORBIDDEN;
125 }
126
127 #ifdef HAVE_SSL
128 else if (status == HTTP_UPGRADE_REQUIRED)
129 {
130 /*
131 * Force a reconnect with encryption...
132 */
133
134 DEBUG_puts("cupsGetDevices: Need encryption...");
135
136 if (!httpReconnect(http))
137 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
138 }
139 #endif /* HAVE_SSL */
140 }
141 }
142 while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
143
144 DEBUG_printf(("cupsGetDevices: status=%d\n", status));
145
146 ippDelete(request);
147
148 if (status != HTTP_OK)
149 {
150 _cupsSetHTTPError(status);
151 return (cupsLastError());
152 }
153
154 /*
155 * Read the response in non-blocking mode...
156 */
157
158 blocking = httpGetBlocking(http);
159 httpBlocking(http, 0);
160
161 response = ippNew();
162 device_class = NULL;
163 device_id = NULL;
164 device_info = NULL;
165 device_location = "";
166 device_make_and_model = NULL;
167 device_uri = NULL;
168 attr = NULL;
169
170 DEBUG_puts("cupsGetDevices: Reading response...");
171
172 do
173 {
174 if ((state = ippRead(http, response)) == IPP_ERROR)
175 break;
176
177 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state,
178 response->last));
179
180 if (!response->attrs)
181 continue;
182
183 while (attr != response->last)
184 {
185 if (!attr)
186 attr = response->attrs;
187 else
188 attr = attr->next;
189
190 DEBUG_printf(("cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d\n",
191 attr->name ? attr->name : "(null)", attr->value_tag));
192
193 if (!attr->name)
194 {
195 if (device_class && device_id && device_info && device_make_and_model &&
196 device_uri)
197 (*callback)(device_class, device_id, device_info,
198 device_make_and_model, device_uri, device_location,
199 user_data);
200
201 device_class = NULL;
202 device_id = NULL;
203 device_info = NULL;
204 device_location = "";
205 device_make_and_model = NULL;
206 device_uri = NULL;
207 }
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;
226 }
227 }
228 while (state != IPP_DATA);
229
230 DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state,
231 response->last));
232
233 if (device_class && device_id && device_info && device_make_and_model &&
234 device_uri)
235 (*callback)(device_class, device_id, device_info,
236 device_make_and_model, device_uri, device_location, user_data);
237
238 /*
239 * Set the IPP status and return...
240 */
241
242 httpBlocking(http, blocking);
243 httpFlush(http);
244
245 if (status == IPP_ERROR)
246 _cupsSetError(IPP_ERROR, NULL, 0);
247 else
248 {
249 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
250
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 : ""));
254
255 _cupsSetError(response->request.status.status_code,
256 attr ? attr->values[0].string.text :
257 ippErrorString(response->request.status.status_code), 0);
258 }
259
260 ippDelete(response);
261
262 return (cupsLastError());
263 }
264
265
266 /*
267 * End of "$Id$".
268 */