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