]>
Commit | Line | Data |
---|---|---|
ae71f5de MS |
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 | */ | |
19 | ||
20 | /* | |
21 | * Include necessary headers... | |
22 | */ | |
23 | ||
24 | #include "globals.h" | |
25 | #include "debug.h" | |
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 | |
33 | * how long the request lasts, while the "exclude_schemes" parameter provides | |
34 | * a comma-delimited list of backends to omit from the request. | |
35 | * | |
36 | * @since CUPS 1.4@ | |
37 | */ | |
38 | ||
39 | ipp_status_t /* O - Request status - @code IPP_OK@ on success. */ | |
40 | cupsGetDevices( | |
41 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
42 | int timeout, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */ | |
43 | const char *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */ | |
44 | cups_device_cb_t callback, /* I - Callback function */ | |
45 | void *user_data) /* I - User data pointer */ | |
46 | { | |
47 | ipp_t *request, /* CUPS-Get-Devices request */ | |
48 | *response; /* CUPS-Get-Devices response */ | |
49 | ipp_attribute_t *attr; /* Current attribute */ | |
50 | const char *device_class, /* device-class value */ | |
51 | *device_id, /* device-id value */ | |
52 | *device_info, /* device-info value */ | |
53 | *device_make_and_model, /* device-make-and-model value */ | |
54 | *device_uri; /* device-uri value */ | |
55 | int blocking; /* Current blocking-IO mode */ | |
56 | cups_option_t option; /* exclude-schemes option */ | |
57 | http_status_t status; /* HTTP status of request */ | |
58 | ipp_state_t state; /* IPP response state */ | |
59 | ||
60 | ||
61 | /* | |
62 | * Range check input... | |
63 | */ | |
64 | ||
65 | if (!callback) | |
66 | return (IPP_INTERNAL_ERROR); | |
67 | ||
68 | if (!http) | |
69 | http = _cupsConnect(); | |
70 | ||
71 | if (!http) | |
72 | return (IPP_SERVICE_UNAVAILABLE); | |
73 | ||
74 | /* | |
75 | * Create a CUPS-Get-Devices request... | |
76 | */ | |
77 | ||
78 | request = ippNewRequest(CUPS_GET_DEVICES); | |
79 | ||
80 | if (timeout > 0) | |
81 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout", | |
82 | timeout); | |
83 | ||
84 | if (exclude_schemes) | |
85 | { | |
86 | option.name = "exclude-schemes"; | |
87 | option.value = (char *)exclude_schemes; | |
88 | ||
89 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
90 | } | |
91 | ||
92 | /* | |
93 | * Send the request and do any necessary authentication... | |
94 | */ | |
95 | ||
96 | do | |
97 | { | |
98 | DEBUG_puts("cupsGetDevices: Sending request..."); | |
99 | status = cupsSendRequest(http, request, "/", ippLength(request)); | |
100 | ||
101 | DEBUG_puts("cupsGetDevices: Waiting for response status..."); | |
102 | while (status == HTTP_CONTINUE) | |
103 | status = httpUpdate(http); | |
104 | ||
105 | if (status != HTTP_OK) | |
106 | { | |
107 | httpFlush(http); | |
108 | ||
109 | if (status == HTTP_UNAUTHORIZED) | |
110 | { | |
111 | /* | |
112 | * See if we can do authentication... | |
113 | */ | |
114 | ||
115 | int auth_result; | |
116 | ||
117 | DEBUG_puts("cupsGetDevices: Need authorization..."); | |
118 | ||
119 | if ((auth_result = cupsDoAuthentication(http, "POST", "/")) == 0) | |
120 | httpReconnect(http); | |
121 | else if (auth_result < 0) | |
122 | http->status = status = HTTP_FORBIDDEN; | |
123 | } | |
124 | ||
125 | #ifdef HAVE_SSL | |
126 | else if (status == HTTP_UPGRADE_REQUIRED) | |
127 | { | |
128 | /* | |
129 | * Force a reconnect with encryption... | |
130 | */ | |
131 | ||
132 | DEBUG_puts("cupsGetDevices: Need encryption..."); | |
133 | ||
134 | if (!httpReconnect(http)) | |
135 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); | |
136 | } | |
137 | #endif /* HAVE_SSL */ | |
138 | } | |
139 | } | |
140 | while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED); | |
141 | ||
142 | DEBUG_printf(("cupsGetDevices: status=%d\n", status)); | |
143 | ||
144 | ippDelete(request); | |
145 | ||
146 | if (status != HTTP_OK) | |
147 | { | |
148 | _cupsSetHTTPError(status); | |
149 | return (cupsLastError()); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Read the response in non-blocking mode... | |
154 | */ | |
155 | ||
156 | blocking = httpGetBlocking(http); | |
157 | httpBlocking(http, 0); | |
158 | ||
159 | response = ippNew(); | |
160 | device_class = NULL; | |
161 | device_id = NULL; | |
162 | device_info = NULL; | |
163 | device_make_and_model = NULL; | |
164 | device_uri = NULL; | |
165 | attr = NULL; | |
166 | ||
167 | DEBUG_puts("cupsGetDevices: Reading response..."); | |
168 | ||
169 | do | |
170 | { | |
171 | if ((state = ippRead(http, response)) == IPP_ERROR) | |
172 | break; | |
173 | ||
174 | DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state, | |
175 | response->last)); | |
176 | ||
177 | if (!response->attrs) | |
178 | continue; | |
179 | ||
180 | while (attr != response->last) | |
181 | { | |
182 | if (!attr) | |
183 | attr = response->attrs; | |
184 | else | |
185 | attr = attr->next; | |
186 | ||
187 | DEBUG_printf(("cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d\n", | |
188 | attr->name ? attr->name : "(null)", attr->value_tag)); | |
189 | ||
190 | if (!attr->name) | |
191 | { | |
192 | if (device_class && device_id && device_info && device_make_and_model && | |
193 | device_uri) | |
194 | (*callback)(device_class, device_id, device_info, | |
195 | device_make_and_model, device_uri, user_data); | |
196 | ||
197 | device_class = NULL; | |
198 | device_id = NULL; | |
199 | device_info = NULL; | |
200 | device_make_and_model = NULL; | |
201 | device_uri = NULL; | |
202 | } | |
203 | else if (!strcmp(attr->name, "device-class") && | |
204 | attr->value_tag == IPP_TAG_KEYWORD) | |
205 | device_class = attr->values[0].string.text; | |
206 | else if (!strcmp(attr->name, "device-id") && | |
207 | attr->value_tag == IPP_TAG_TEXT) | |
208 | device_id = attr->values[0].string.text; | |
209 | else if (!strcmp(attr->name, "device-info") && | |
210 | attr->value_tag == IPP_TAG_TEXT) | |
211 | device_info = attr->values[0].string.text; | |
212 | else if (!strcmp(attr->name, "device-make-and-model") && | |
213 | attr->value_tag == IPP_TAG_TEXT) | |
214 | device_make_and_model = attr->values[0].string.text; | |
215 | else if (!strcmp(attr->name, "device-uri") && | |
216 | attr->value_tag == IPP_TAG_URI) | |
217 | device_uri = attr->values[0].string.text; | |
218 | } | |
219 | } | |
220 | while (state != IPP_DATA); | |
221 | ||
222 | DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state, | |
223 | response->last)); | |
224 | ||
225 | if (device_class && device_id && device_info && device_make_and_model && | |
226 | device_uri) | |
227 | (*callback)(device_class, device_id, device_info, | |
228 | device_make_and_model, device_uri, user_data); | |
229 | ||
230 | /* | |
231 | * Set the IPP status and return... | |
232 | */ | |
233 | ||
234 | httpBlocking(http, blocking); | |
235 | ||
236 | if (status == IPP_ERROR) | |
237 | _cupsSetError(IPP_ERROR, NULL); | |
238 | else | |
239 | { | |
240 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
241 | ||
242 | DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"\n", | |
243 | ippErrorString(response->request.status.status_code), | |
244 | attr ? attr->values[0].string.text : "")); | |
245 | ||
246 | _cupsSetError(response->request.status.status_code, | |
247 | attr ? attr->values[0].string.text : | |
248 | ippErrorString(response->request.status.status_code)); | |
249 | } | |
250 | ||
251 | ippDelete(response); | |
252 | ||
253 | return (cupsLastError()); | |
254 | } | |
255 | ||
256 | ||
257 | /* | |
258 | * End of "$Id$". | |
259 | */ |