]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/getdevices.c
Merge changes from CUPS 1.5svn-r9037.
[thirdparty/cups.git] / cups / getdevices.c
CommitLineData
ae71f5de
MS
1/*
2 * "$Id$"
3 *
4 * cupsGetDevices implementation for the Common UNIX Printing System (CUPS).
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
25#include "globals.h"
26#include "debug.h"
27
28
29/*
30 * 'cupsGetDevices()' - Get available printer devices.
31 *
32 * This function sends a CUPS-Get-Devices request and streams the discovered
33 * devices to the specified callback function. The "timeout" parameter controls
ed6e7faf
MS
34 * how long the request lasts, while the "include_schemes" and "exclude_schemes"
35 * parameters provide comma-delimited lists of backends to include or omit from
36 * the request respectively.
ae71f5de 37 *
178cb736 38 * @since CUPS 1.4/Mac OS X 10.6@
ae71f5de
MS
39 */
40
41ipp_status_t /* O - Request status - @code IPP_OK@ on success. */
42cupsGetDevices(
43 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
44 int timeout, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
ed6e7faf 45 const char *include_schemes, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */
ae71f5de
MS
46 const char *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
47 cups_device_cb_t callback, /* I - Callback function */
48 void *user_data) /* I - User data pointer */
49{
50 ipp_t *request, /* CUPS-Get-Devices request */
51 *response; /* CUPS-Get-Devices response */
52 ipp_attribute_t *attr; /* Current attribute */
53 const char *device_class, /* device-class value */
54 *device_id, /* device-id value */
55 *device_info, /* device-info value */
749b1e90 56 *device_location, /* device-location value */
ae71f5de
MS
57 *device_make_and_model, /* device-make-and-model value */
58 *device_uri; /* device-uri value */
59 int blocking; /* Current blocking-IO mode */
ed6e7faf 60 cups_option_t option; /* in/exclude-schemes option */
ae71f5de
MS
61 http_status_t status; /* HTTP status of request */
62 ipp_state_t state; /* IPP response state */
63
64
65 /*
66 * Range check input...
67 */
68
e07d4801
MS
69 DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", "
70 "exclude_schemes=\"%s\", callback=%p, user_data=%p)", http,
71 timeout, include_schemes, exclude_schemes, callback,
72 user_data));
73
ae71f5de
MS
74 if (!callback)
75 return (IPP_INTERNAL_ERROR);
76
77 if (!http)
78 http = _cupsConnect();
79
80 if (!http)
81 return (IPP_SERVICE_UNAVAILABLE);
82
83 /*
84 * Create a CUPS-Get-Devices request...
85 */
86
87 request = ippNewRequest(CUPS_GET_DEVICES);
88
89 if (timeout > 0)
90 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout",
91 timeout);
92
ed6e7faf
MS
93 if (include_schemes)
94 {
95 option.name = "include-schemes";
96 option.value = (char *)include_schemes;
97
98 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
99 }
100
ae71f5de
MS
101 if (exclude_schemes)
102 {
103 option.name = "exclude-schemes";
104 option.value = (char *)exclude_schemes;
105
106 cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
107 }
108
109 /*
ba55dc12 110 * Send the request...
ae71f5de
MS
111 */
112
ba55dc12
MS
113 DEBUG_puts("2cupsGetDevices: Sending request...");
114 status = cupsSendRequest(http, request, "/", ippLength(request));
ae71f5de 115
ba55dc12
MS
116 DEBUG_puts("2cupsGetDevices: Waiting for response status...");
117 while (status == HTTP_CONTINUE)
118 status = httpUpdate(http);
ae71f5de 119
e07d4801 120 DEBUG_printf(("2cupsGetDevices: status=%d", status));
ae71f5de
MS
121
122 ippDelete(request);
123
124 if (status != HTTP_OK)
125 {
126 _cupsSetHTTPError(status);
127 return (cupsLastError());
128 }
129
130 /*
131 * Read the response in non-blocking mode...
132 */
133
134 blocking = httpGetBlocking(http);
135 httpBlocking(http, 0);
136
137 response = ippNew();
138 device_class = NULL;
139 device_id = NULL;
140 device_info = NULL;
749b1e90 141 device_location = "";
ae71f5de
MS
142 device_make_and_model = NULL;
143 device_uri = NULL;
144 attr = NULL;
145
e07d4801 146 DEBUG_puts("2cupsGetDevices: Reading response...");
ae71f5de
MS
147
148 do
149 {
150 if ((state = ippRead(http, response)) == IPP_ERROR)
151 break;
152
e07d4801 153 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state,
ae71f5de
MS
154 response->last));
155
156 if (!response->attrs)
157 continue;
158
159 while (attr != response->last)
160 {
161 if (!attr)
162 attr = response->attrs;
163 else
164 attr = attr->next;
165
e07d4801
MS
166 DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
167 attr->name, attr->value_tag));
ae71f5de
MS
168
169 if (!attr->name)
170 {
171 if (device_class && device_id && device_info && device_make_and_model &&
172 device_uri)
173 (*callback)(device_class, device_id, device_info,
749b1e90
MS
174 device_make_and_model, device_uri, device_location,
175 user_data);
ae71f5de
MS
176
177 device_class = NULL;
178 device_id = NULL;
179 device_info = NULL;
749b1e90 180 device_location = "";
ae71f5de
MS
181 device_make_and_model = NULL;
182 device_uri = NULL;
183 }
184 else if (!strcmp(attr->name, "device-class") &&
185 attr->value_tag == IPP_TAG_KEYWORD)
186 device_class = attr->values[0].string.text;
187 else if (!strcmp(attr->name, "device-id") &&
188 attr->value_tag == IPP_TAG_TEXT)
189 device_id = attr->values[0].string.text;
190 else if (!strcmp(attr->name, "device-info") &&
191 attr->value_tag == IPP_TAG_TEXT)
192 device_info = attr->values[0].string.text;
749b1e90
MS
193 else if (!strcmp(attr->name, "device-location") &&
194 attr->value_tag == IPP_TAG_TEXT)
195 device_location = attr->values[0].string.text;
ae71f5de
MS
196 else if (!strcmp(attr->name, "device-make-and-model") &&
197 attr->value_tag == IPP_TAG_TEXT)
198 device_make_and_model = attr->values[0].string.text;
199 else if (!strcmp(attr->name, "device-uri") &&
200 attr->value_tag == IPP_TAG_URI)
201 device_uri = attr->values[0].string.text;
202 }
203 }
204 while (state != IPP_DATA);
205
e07d4801 206 DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state,
ae71f5de
MS
207 response->last));
208
209 if (device_class && device_id && device_info && device_make_and_model &&
210 device_uri)
211 (*callback)(device_class, device_id, device_info,
749b1e90 212 device_make_and_model, device_uri, device_location, user_data);
ae71f5de
MS
213
214 /*
215 * Set the IPP status and return...
216 */
217
218 httpBlocking(http, blocking);
8922323b 219 httpFlush(http);
ae71f5de 220
ba55dc12 221 if (status == HTTP_ERROR)
749b1e90 222 _cupsSetError(IPP_ERROR, NULL, 0);
ae71f5de
MS
223 else
224 {
225 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
226
e07d4801 227 DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
ae71f5de
MS
228 ippErrorString(response->request.status.status_code),
229 attr ? attr->values[0].string.text : ""));
230
231 _cupsSetError(response->request.status.status_code,
749b1e90
MS
232 attr ? attr->values[0].string.text :
233 ippErrorString(response->request.status.status_code), 0);
ae71f5de
MS
234 }
235
236 ippDelete(response);
237
238 return (cupsLastError());
239}
240
241
242/*
243 * End of "$Id$".
244 */