]>
Commit | Line | Data |
---|---|---|
ae71f5de MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
71e16022 | 4 | * cupsGetDevices implementation for CUPS. |
ae71f5de | 5 | * |
cb7f98ee | 6 | * Copyright 2008-2013 by Apple Inc. |
ae71f5de MS |
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 | * | |
749b1e90 | 18 | * cupsGetDevices() - Get available printer devices. |
ae71f5de MS |
19 | */ |
20 | ||
21 | /* | |
22 | * Include necessary headers... | |
23 | */ | |
24 | ||
71e16022 | 25 | #include "cups-private.h" |
ae71f5de MS |
26 | |
27 | ||
28 | /* | |
29 | * 'cupsGetDevices()' - Get available printer devices. | |
30 | * | |
31 | * This function sends a CUPS-Get-Devices request and streams the discovered | |
32 | * devices to the specified callback function. The "timeout" parameter controls | |
ed6e7faf MS |
33 | * how long the request lasts, while the "include_schemes" and "exclude_schemes" |
34 | * parameters provide comma-delimited lists of backends to include or omit from | |
35 | * the request respectively. | |
ae71f5de | 36 | * |
f3c17241 | 37 | * @since CUPS 1.4/OS X 10.6@ |
ae71f5de MS |
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@ */ | |
ed6e7faf | 44 | const char *include_schemes, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */ |
ae71f5de MS |
45 | const char *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */ |
46 | cups_device_cb_t callback, /* I - Callback function */ | |
47 | void *user_data) /* I - User data pointer */ | |
48 | { | |
49 | ipp_t *request, /* CUPS-Get-Devices request */ | |
50 | *response; /* CUPS-Get-Devices response */ | |
51 | ipp_attribute_t *attr; /* Current attribute */ | |
52 | const char *device_class, /* device-class value */ | |
53 | *device_id, /* device-id value */ | |
54 | *device_info, /* device-info value */ | |
749b1e90 | 55 | *device_location, /* device-location value */ |
ae71f5de MS |
56 | *device_make_and_model, /* device-make-and-model value */ |
57 | *device_uri; /* device-uri value */ | |
58 | int blocking; /* Current blocking-IO mode */ | |
ed6e7faf | 59 | cups_option_t option; /* in/exclude-schemes option */ |
ae71f5de MS |
60 | http_status_t status; /* HTTP status of request */ |
61 | ipp_state_t state; /* IPP response state */ | |
62 | ||
63 | ||
64 | /* | |
65 | * Range check input... | |
66 | */ | |
67 | ||
807315e6 | 68 | 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)); |
e07d4801 | 69 | |
ae71f5de | 70 | if (!callback) |
cb7f98ee | 71 | return (IPP_STATUS_ERROR_INTERNAL); |
ae71f5de MS |
72 | |
73 | if (!http) | |
74 | http = _cupsConnect(); | |
75 | ||
76 | if (!http) | |
cb7f98ee | 77 | return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE); |
ae71f5de MS |
78 | |
79 | /* | |
80 | * Create a CUPS-Get-Devices request... | |
81 | */ | |
82 | ||
cb7f98ee | 83 | request = ippNewRequest(IPP_OP_CUPS_GET_DEVICES); |
ae71f5de MS |
84 | |
85 | if (timeout > 0) | |
86 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout", | |
87 | timeout); | |
88 | ||
ed6e7faf MS |
89 | if (include_schemes) |
90 | { | |
91 | option.name = "include-schemes"; | |
92 | option.value = (char *)include_schemes; | |
93 | ||
94 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
95 | } | |
96 | ||
ae71f5de MS |
97 | if (exclude_schemes) |
98 | { | |
99 | option.name = "exclude-schemes"; | |
100 | option.value = (char *)exclude_schemes; | |
101 | ||
102 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
103 | } | |
104 | ||
105 | /* | |
5180a04c | 106 | * Send the request and do any necessary authentication... |
ae71f5de MS |
107 | */ |
108 | ||
5180a04c MS |
109 | do |
110 | { | |
111 | DEBUG_puts("2cupsGetDevices: Sending request..."); | |
112 | status = cupsSendRequest(http, request, "/", ippLength(request)); | |
113 | ||
114 | DEBUG_puts("2cupsGetDevices: Waiting for response status..."); | |
cb7f98ee | 115 | while (status == HTTP_STATUS_CONTINUE) |
5180a04c MS |
116 | status = httpUpdate(http); |
117 | ||
cb7f98ee | 118 | if (status != HTTP_STATUS_OK) |
5180a04c MS |
119 | { |
120 | httpFlush(http); | |
121 | ||
cb7f98ee | 122 | if (status == HTTP_STATUS_UNAUTHORIZED) |
5180a04c MS |
123 | { |
124 | /* | |
125 | * See if we can do authentication... | |
126 | */ | |
127 | ||
128 | DEBUG_puts("2cupsGetDevices: Need authorization..."); | |
129 | ||
130 | if (!cupsDoAuthentication(http, "POST", "/")) | |
cb7f98ee | 131 | httpReconnect2(http, 30000, NULL); |
5180a04c MS |
132 | else |
133 | { | |
cb7f98ee | 134 | status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
5180a04c MS |
135 | break; |
136 | } | |
137 | } | |
138 | ||
139 | #ifdef HAVE_SSL | |
cb7f98ee | 140 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) |
5180a04c MS |
141 | { |
142 | /* | |
143 | * Force a reconnect with encryption... | |
144 | */ | |
145 | ||
146 | DEBUG_puts("2cupsGetDevices: Need encryption..."); | |
147 | ||
cb7f98ee MS |
148 | if (!httpReconnect2(http, 30000, NULL)) |
149 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
5180a04c MS |
150 | } |
151 | #endif /* HAVE_SSL */ | |
152 | } | |
153 | } | |
cb7f98ee MS |
154 | while (status == HTTP_STATUS_UNAUTHORIZED || |
155 | status == HTTP_STATUS_UPGRADE_REQUIRED); | |
ae71f5de | 156 | |
e07d4801 | 157 | DEBUG_printf(("2cupsGetDevices: status=%d", status)); |
ae71f5de MS |
158 | |
159 | ippDelete(request); | |
160 | ||
cb7f98ee | 161 | if (status != HTTP_STATUS_OK) |
ae71f5de MS |
162 | { |
163 | _cupsSetHTTPError(status); | |
164 | return (cupsLastError()); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Read the response in non-blocking mode... | |
169 | */ | |
170 | ||
171 | blocking = httpGetBlocking(http); | |
172 | httpBlocking(http, 0); | |
173 | ||
174 | response = ippNew(); | |
175 | device_class = NULL; | |
176 | device_id = NULL; | |
177 | device_info = NULL; | |
749b1e90 | 178 | device_location = ""; |
ae71f5de MS |
179 | device_make_and_model = NULL; |
180 | device_uri = NULL; | |
181 | attr = NULL; | |
182 | ||
e07d4801 | 183 | DEBUG_puts("2cupsGetDevices: Reading response..."); |
ae71f5de MS |
184 | |
185 | do | |
186 | { | |
cb7f98ee | 187 | if ((state = ippRead(http, response)) == IPP_STATE_ERROR) |
ae71f5de MS |
188 | break; |
189 | ||
807315e6 | 190 | DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, (void *)response->last)); |
ae71f5de MS |
191 | |
192 | if (!response->attrs) | |
193 | continue; | |
194 | ||
195 | while (attr != response->last) | |
196 | { | |
197 | if (!attr) | |
198 | attr = response->attrs; | |
199 | else | |
200 | attr = attr->next; | |
201 | ||
e07d4801 MS |
202 | DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d", |
203 | attr->name, attr->value_tag)); | |
ae71f5de MS |
204 | |
205 | if (!attr->name) | |
206 | { | |
207 | if (device_class && device_id && device_info && device_make_and_model && | |
208 | device_uri) | |
209 | (*callback)(device_class, device_id, device_info, | |
749b1e90 MS |
210 | device_make_and_model, device_uri, device_location, |
211 | user_data); | |
ae71f5de MS |
212 | |
213 | device_class = NULL; | |
214 | device_id = NULL; | |
215 | device_info = NULL; | |
749b1e90 | 216 | device_location = ""; |
ae71f5de MS |
217 | device_make_and_model = NULL; |
218 | device_uri = NULL; | |
219 | } | |
220 | else if (!strcmp(attr->name, "device-class") && | |
221 | attr->value_tag == IPP_TAG_KEYWORD) | |
222 | device_class = attr->values[0].string.text; | |
223 | else if (!strcmp(attr->name, "device-id") && | |
224 | attr->value_tag == IPP_TAG_TEXT) | |
225 | device_id = attr->values[0].string.text; | |
226 | else if (!strcmp(attr->name, "device-info") && | |
227 | attr->value_tag == IPP_TAG_TEXT) | |
228 | device_info = attr->values[0].string.text; | |
749b1e90 MS |
229 | else if (!strcmp(attr->name, "device-location") && |
230 | attr->value_tag == IPP_TAG_TEXT) | |
231 | device_location = attr->values[0].string.text; | |
ae71f5de MS |
232 | else if (!strcmp(attr->name, "device-make-and-model") && |
233 | attr->value_tag == IPP_TAG_TEXT) | |
234 | device_make_and_model = attr->values[0].string.text; | |
235 | else if (!strcmp(attr->name, "device-uri") && | |
236 | attr->value_tag == IPP_TAG_URI) | |
237 | device_uri = attr->values[0].string.text; | |
238 | } | |
239 | } | |
cb7f98ee | 240 | while (state != IPP_STATE_DATA); |
ae71f5de | 241 | |
807315e6 | 242 | DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, (void *)response->last)); |
ae71f5de MS |
243 | |
244 | if (device_class && device_id && device_info && device_make_and_model && | |
245 | device_uri) | |
246 | (*callback)(device_class, device_id, device_info, | |
749b1e90 | 247 | device_make_and_model, device_uri, device_location, user_data); |
ae71f5de MS |
248 | |
249 | /* | |
250 | * Set the IPP status and return... | |
251 | */ | |
252 | ||
253 | httpBlocking(http, blocking); | |
8922323b | 254 | httpFlush(http); |
ae71f5de | 255 | |
cb7f98ee MS |
256 | if (status == HTTP_STATUS_ERROR) |
257 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0); | |
ae71f5de MS |
258 | else |
259 | { | |
260 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
261 | ||
e07d4801 | 262 | DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"", |
ae71f5de MS |
263 | ippErrorString(response->request.status.status_code), |
264 | attr ? attr->values[0].string.text : "")); | |
265 | ||
266 | _cupsSetError(response->request.status.status_code, | |
749b1e90 MS |
267 | attr ? attr->values[0].string.text : |
268 | ippErrorString(response->request.status.status_code), 0); | |
ae71f5de MS |
269 | } |
270 | ||
271 | ippDelete(response); | |
272 | ||
273 | return (cupsLastError()); | |
274 | } | |
275 | ||
276 | ||
277 | /* | |
278 | * End of "$Id$". | |
279 | */ |