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