]>
Commit | Line | Data |
---|---|---|
ae71f5de MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
71e16022 | 4 | * cupsGetDevices implementation for CUPS. |
ae71f5de | 5 | * |
ba55dc12 | 6 | * Copyright 2008-2010 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 | * |
178cb736 | 37 | * @since CUPS 1.4/Mac 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 | ||
e07d4801 MS |
68 | DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", " |
69 | "exclude_schemes=\"%s\", callback=%p, user_data=%p)", http, | |
70 | timeout, include_schemes, exclude_schemes, callback, | |
71 | user_data)); | |
72 | ||
ae71f5de MS |
73 | if (!callback) |
74 | return (IPP_INTERNAL_ERROR); | |
75 | ||
76 | if (!http) | |
77 | http = _cupsConnect(); | |
78 | ||
79 | if (!http) | |
80 | return (IPP_SERVICE_UNAVAILABLE); | |
81 | ||
82 | /* | |
83 | * Create a CUPS-Get-Devices request... | |
84 | */ | |
85 | ||
86 | request = ippNewRequest(CUPS_GET_DEVICES); | |
87 | ||
88 | if (timeout > 0) | |
89 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout", | |
90 | timeout); | |
91 | ||
ed6e7faf MS |
92 | if (include_schemes) |
93 | { | |
94 | option.name = "include-schemes"; | |
95 | option.value = (char *)include_schemes; | |
96 | ||
97 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
98 | } | |
99 | ||
ae71f5de MS |
100 | if (exclude_schemes) |
101 | { | |
102 | option.name = "exclude-schemes"; | |
103 | option.value = (char *)exclude_schemes; | |
104 | ||
105 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
106 | } | |
107 | ||
108 | /* | |
ba55dc12 | 109 | * Send the request... |
ae71f5de MS |
110 | */ |
111 | ||
ba55dc12 MS |
112 | DEBUG_puts("2cupsGetDevices: Sending request..."); |
113 | status = cupsSendRequest(http, request, "/", ippLength(request)); | |
ae71f5de | 114 | |
ba55dc12 MS |
115 | DEBUG_puts("2cupsGetDevices: Waiting for response status..."); |
116 | while (status == HTTP_CONTINUE) | |
117 | status = httpUpdate(http); | |
ae71f5de | 118 | |
e07d4801 | 119 | DEBUG_printf(("2cupsGetDevices: status=%d", status)); |
ae71f5de MS |
120 | |
121 | ippDelete(request); | |
122 | ||
123 | if (status != HTTP_OK) | |
124 | { | |
125 | _cupsSetHTTPError(status); | |
126 | return (cupsLastError()); | |
127 | } | |
128 | ||
129 | /* | |
130 | * Read the response in non-blocking mode... | |
131 | */ | |
132 | ||
133 | blocking = httpGetBlocking(http); | |
134 | httpBlocking(http, 0); | |
135 | ||
136 | response = ippNew(); | |
137 | device_class = NULL; | |
138 | device_id = NULL; | |
139 | device_info = NULL; | |
749b1e90 | 140 | device_location = ""; |
ae71f5de MS |
141 | device_make_and_model = NULL; |
142 | device_uri = NULL; | |
143 | attr = NULL; | |
144 | ||
e07d4801 | 145 | DEBUG_puts("2cupsGetDevices: Reading response..."); |
ae71f5de MS |
146 | |
147 | do | |
148 | { | |
149 | if ((state = ippRead(http, response)) == IPP_ERROR) | |
150 | break; | |
151 | ||
e07d4801 | 152 | DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, |
ae71f5de MS |
153 | response->last)); |
154 | ||
155 | if (!response->attrs) | |
156 | continue; | |
157 | ||
158 | while (attr != response->last) | |
159 | { | |
160 | if (!attr) | |
161 | attr = response->attrs; | |
162 | else | |
163 | attr = attr->next; | |
164 | ||
e07d4801 MS |
165 | DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d", |
166 | attr->name, attr->value_tag)); | |
ae71f5de MS |
167 | |
168 | if (!attr->name) | |
169 | { | |
170 | if (device_class && device_id && device_info && device_make_and_model && | |
171 | device_uri) | |
172 | (*callback)(device_class, device_id, device_info, | |
749b1e90 MS |
173 | device_make_and_model, device_uri, device_location, |
174 | user_data); | |
ae71f5de MS |
175 | |
176 | device_class = NULL; | |
177 | device_id = NULL; | |
178 | device_info = NULL; | |
749b1e90 | 179 | device_location = ""; |
ae71f5de MS |
180 | device_make_and_model = NULL; |
181 | device_uri = NULL; | |
182 | } | |
183 | else if (!strcmp(attr->name, "device-class") && | |
184 | attr->value_tag == IPP_TAG_KEYWORD) | |
185 | device_class = attr->values[0].string.text; | |
186 | else if (!strcmp(attr->name, "device-id") && | |
187 | attr->value_tag == IPP_TAG_TEXT) | |
188 | device_id = attr->values[0].string.text; | |
189 | else if (!strcmp(attr->name, "device-info") && | |
190 | attr->value_tag == IPP_TAG_TEXT) | |
191 | device_info = attr->values[0].string.text; | |
749b1e90 MS |
192 | else if (!strcmp(attr->name, "device-location") && |
193 | attr->value_tag == IPP_TAG_TEXT) | |
194 | device_location = attr->values[0].string.text; | |
ae71f5de MS |
195 | else if (!strcmp(attr->name, "device-make-and-model") && |
196 | attr->value_tag == IPP_TAG_TEXT) | |
197 | device_make_and_model = attr->values[0].string.text; | |
198 | else if (!strcmp(attr->name, "device-uri") && | |
199 | attr->value_tag == IPP_TAG_URI) | |
200 | device_uri = attr->values[0].string.text; | |
201 | } | |
202 | } | |
203 | while (state != IPP_DATA); | |
204 | ||
e07d4801 | 205 | DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, |
ae71f5de MS |
206 | response->last)); |
207 | ||
208 | if (device_class && device_id && device_info && device_make_and_model && | |
209 | device_uri) | |
210 | (*callback)(device_class, device_id, device_info, | |
749b1e90 | 211 | device_make_and_model, device_uri, device_location, user_data); |
ae71f5de MS |
212 | |
213 | /* | |
214 | * Set the IPP status and return... | |
215 | */ | |
216 | ||
217 | httpBlocking(http, blocking); | |
8922323b | 218 | httpFlush(http); |
ae71f5de | 219 | |
ba55dc12 | 220 | if (status == HTTP_ERROR) |
749b1e90 | 221 | _cupsSetError(IPP_ERROR, NULL, 0); |
ae71f5de MS |
222 | else |
223 | { | |
224 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
225 | ||
e07d4801 | 226 | DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"", |
ae71f5de MS |
227 | ippErrorString(response->request.status.status_code), |
228 | attr ? attr->values[0].string.text : "")); | |
229 | ||
230 | _cupsSetError(response->request.status.status_code, | |
749b1e90 MS |
231 | attr ? attr->values[0].string.text : |
232 | ippErrorString(response->request.status.status_code), 0); | |
ae71f5de MS |
233 | } |
234 | ||
235 | ippDelete(response); | |
236 | ||
237 | return (cupsLastError()); | |
238 | } | |
239 | ||
240 | ||
241 | /* | |
242 | * End of "$Id$". | |
243 | */ |