1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 * Copyright (C) 2016 Mopria Alliance, Inc.
4 * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "lib_wprint.h"
20 #include "cups.h"
21 #include "http-private.h"
22 #include "ipphelper.h"
23 #include "wprint_debug.h"
24
25 #include "ipp_print.h"
26 #include "../plugins/media.h"
27
28 #define TAG "ipphelper"
29
30 /*
31 * Get the IPP version of the given printer
32 */
33 static status_t determine_ipp_version(char *, http_t *);
34
35 /*
36 * Tests IPP versions and sets it to the latest working version
37 */
38 static status_t test_and_set_ipp_version(char *, http_t *, int, int);
39
40 /*
41 * Parses supported IPP versions from the IPP response and copies them into ippVersions
42 */
43 static void parse_IPPVersions(ipp_t *response, ipp_version_supported_t *ippVersions);
44
45 /*
46 * Parses printer URIs from the IPP response and copies them into capabilities
47 */
48 static void parse_printerUris(ipp_t *response, printer_capabilities_t *capabilities);
49
50 /*
51 * Known media sizes.
52 *
53 * A note on rounding: In some cases the Android-specified width (in mils) is rounded down.
54 * This causes artifacts in libjpeg-turbo when rendering to the correct width, so in these
55 * cases we override with a rounded-up value.
56 */
57 struct MediaSizeTableElement SupportedMediaSizes[SUPPORTED_MEDIA_SIZE_COUNT] = {
58 { US_LETTER, "LETTER", 8500, 11000, UNKNOWN_VALUE, UNKNOWN_VALUE, "na_letter_8.5x11in" },
59 { US_LEGAL, "LEGAL", 8500, 14000, UNKNOWN_VALUE, UNKNOWN_VALUE, "na_legal_8.5x14in" },
60 { LEDGER, "LEDGER", 11000, 17000, UNKNOWN_VALUE, UNKNOWN_VALUE, "na_ledger_11x17in" },
61 { INDEX_CARD_5X7, "5X7", 5000, 7000, UNKNOWN_VALUE, UNKNOWN_VALUE, "na_5x7_5x7in" },
62
63 // Android system uses width of 11690
64 { ISO_A3, "A3", 11694, 16540, 297, 420, "iso_a3_297x420mm" },
65
66 // Android system uses width of 8267
67 { ISO_A4, "A4", 8268, 11692, 210, 297, "iso_a4_210x297mm" },
68 { ISO_A5, "A5", 5830, 8270, 148, 210, "iso_a5_148x210mm" },
69
70 // Android system uses width of 10118
71 { JIS_B4, "JIS B4", 10119, 14331, 257, 364, "jis_b4_257x364mm" },
72
73 // Android system uses width of 7165
74 { JIS_B5, "JIS B5", 7167, 10118, 182, 257, "jis_b5_182x257mm" },
75 { US_GOVERNMENT_LETTER, "8x10", 8000, 10000, UNKNOWN_VALUE, UNKNOWN_VALUE,
76 "na_govt-letter_8x10in" },
77 { INDEX_CARD_4X6, "4x6", 4000, 6000, UNKNOWN_VALUE, UNKNOWN_VALUE, "na_index-4x6_4x6in" },
78 { JPN_HAGAKI_PC, "JPOST", 3940, 5830, 100, 148, "jpn_hagaki_100x148mm" },
79 { PHOTO_89X119, "89X119", 3504, 4685, 89, 119, "om_dsc-photo_89x119mm" },
80 { CARD_54X86, "54X86", 2126, 3386, 54, 86, "om_card_54x86mm" },
81 { OE_PHOTO_L, "L", 3500, 5000, UNKNOWN_VALUE, UNKNOWN_VALUE, "oe_photo-l_3.5x5in" }
82 };
83
84 typedef struct {
85 double Lower;
86 double Upper;
87 } media_dimension_mm_t;
88
89 static const char *__request_ipp_version[] = {"ipp-versions-supported"};
90
91 static int __ipp_version_major = 2;
92 static int __ipp_version_minor = 0;
93
set_ipp_version(ipp_t * op_to_set,char * printer_uri,http_t * http,ipp_version_state use_existing_version)94 status_t set_ipp_version(ipp_t *op_to_set, char *printer_uri, http_t *http,
95 ipp_version_state use_existing_version) {
96 LOGD("set_ipp_version(): Enter %d", use_existing_version);
97 if (op_to_set == NULL) {
98 return ERROR;
99 }
100 switch (use_existing_version) {
101 case NEW_REQUEST_SEQUENCE:
102 __ipp_version_major = 2;
103 __ipp_version_minor = 0;
104 break;
105 case IPP_VERSION_RESOLVED:
106 break;
107 case IPP_VERSION_UNSUPPORTED:
108 if (determine_ipp_version(printer_uri, http) != 0) {
109 return ERROR;
110 }
111 break;
112 }
113 ippSetVersion(op_to_set, __ipp_version_major, __ipp_version_minor);
114 LOGD("set_ipp_version(): Done");
115 return OK;
116 }
117
determine_ipp_version(char * printer_uri,http_t * http)118 static status_t determine_ipp_version(char *printer_uri, http_t *http) {
119 LOGD("determine_ipp_version(): Enter printer_uri = %s", printer_uri);
120
121 if (http == NULL) {
122 LOGE("determine_ipp_version(): http is NULL cannot continue");
123 return ERROR;
124 }
125 if ((test_and_set_ipp_version(printer_uri, http, 1, 1) == OK)
126 || (test_and_set_ipp_version(printer_uri, http, 1, 0) == OK)
127 || (test_and_set_ipp_version(printer_uri, http, 2, 0) == OK)) {
128 LOGD("successfully set ipp version.");
129 } else {
130 LOGD("could not get ipp version using any known ipp version.");
131 return ERROR;
132 }
133 return OK;
134 }
135
test_and_set_ipp_version(char * printer_uri,http_t * http,int major,int minor)136 static status_t test_and_set_ipp_version(char *printer_uri, http_t *http, int major, int minor) {
137 status_t return_value = ERROR;
138 int service_unavailable_retry_count = 0;
139 int bad_request_retry_count = 0;
140 ipp_t *request = NULL;
141 ipp_t *response;
142 ipp_version_supported_t ippVersions;
143 char http_resource[1024];
144 int op = IPP_GET_PRINTER_ATTRIBUTES;
145
146 LOGD("test_and_set_ipp_version(): Enter %d - %d", major, minor);
147 memset(&ippVersions, 0, sizeof(ipp_version_supported_t));
148 getResourceFromURI(printer_uri, http_resource, 1024);
149 do {
150 request = ippNewRequest(op);
151 ippSetVersion(request, major, minor);
152 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer_uri);
153 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes",
154 sizeof(__request_ipp_version) / sizeof(__request_ipp_version[0]),
155 NULL, __request_ipp_version);
156 if ((response = cupsDoRequest(http, request, http_resource)) == NULL) {
157 ipp_status_t ipp_status = cupsLastError();
158 LOGD("test_and_set_ipp_version: response is null: ipp_status %d %s",
159 ipp_status, ippErrorString(ipp_status));
160 if (ipp_status == IPP_INTERNAL_ERROR) {
161 LOGE("test_and_set_ipp_version: 1280 received, bailing...");
162 break;
163 } else if ((ipp_status == IPP_SERVICE_UNAVAILABLE) &&
164 (service_unavailable_retry_count < IPP_SERVICE_ERROR_MAX_RETRIES)) {
165 LOGE("test_and_set_ipp_version: 1282 received, retrying %d of %d",
166 service_unavailable_retry_count, IPP_SERVICE_ERROR_MAX_RETRIES);
167 service_unavailable_retry_count++;
168 continue;
169 } else if (ipp_status == IPP_BAD_REQUEST) {
170 LOGE("test_and_set_ipp_version: IPP_Status of IPP_BAD_REQUEST "
171 "received. retry (%d) of (%d)", bad_request_retry_count,
172 IPP_BAD_REQUEST_MAX_RETRIES);
173 if (bad_request_retry_count > IPP_BAD_REQUEST_MAX_RETRIES) {
174 break;
175 }
176 bad_request_retry_count++;
177 continue;
178 } else if (ipp_status == IPP_NOT_FOUND) {
179 LOGE("test_and_set_ipp_version: IPP_Status of IPP_NOT_FOUND received");
180 break;
181 }
182 return_value = ERROR;
183 } else {
184 ipp_status_t ipp_status = cupsLastError();
185 LOGD("ipp CUPS last ERROR: %d, %s", ipp_status, ippErrorString(ipp_status));
186 if (ipp_status == IPP_BAD_REQUEST) {
187 LOGD("IPP_Status of IPP_BAD_REQUEST received. retry (%d) of (%d)",
188 bad_request_retry_count, IPP_BAD_REQUEST_MAX_RETRIES);
189 if (bad_request_retry_count > IPP_BAD_REQUEST_MAX_RETRIES) {
190 break;
191 }
192 bad_request_retry_count++;
193 ippDelete(response);
194 continue;
195 }
196
197 parse_IPPVersions(response, &ippVersions);
198 if (ippVersions.supportsIpp20) {
199 __ipp_version_major = 2;
200 __ipp_version_minor = 0;
201 return_value = OK;
202 LOGD("test_and_set_ipp_version(): ipp version set to %d,%d",
203 __ipp_version_major, __ipp_version_minor);
204 } else if (ippVersions.supportsIpp11) {
205 __ipp_version_major = 1;
206 __ipp_version_minor = 1;
207 return_value = OK;
208 LOGD("test_and_set_ipp_version(): ipp version set to %d,%d",
209 __ipp_version_major, __ipp_version_minor);
210 } else if (ippVersions.supportsIpp10) {
211 __ipp_version_major = 1;
212 __ipp_version_minor = 0;
213 return_value = OK;
214 LOGD("test_and_set_ipp_version(): ipp version set to %d,%d",
215 __ipp_version_major, __ipp_version_minor);
216 } else {
217 LOGD("test_and_set_ipp_version: ipp version not found");
218 return_value = ERROR;
219 }
220 }
221 if (response != NULL) ippDelete(response);
222 break;
223 } while (1);
224 return return_value;
225 }
226
get_PrinterState(http_t * http,char * printer_uri,printer_state_dyn_t * printer_state_dyn,ipp_pstate_t * printer_state)227 ipp_status_t get_PrinterState(http_t *http, char *printer_uri,
228 printer_state_dyn_t *printer_state_dyn, ipp_pstate_t *printer_state) {
229 LOGD("get_PrinterState(): Enter");
230
231 // Requested printer attributes
232 static const char *pattrs[] = {"printer-make-and-model", "printer-state",
233 "printer-state-message", "printer-state-reasons"};
234
235 ipp_t *request = NULL;
236 ipp_t *response = NULL;
237 ipp_status_t ipp_status = IPP_OK;
238 int op = IPP_GET_PRINTER_ATTRIBUTES;
239 char http_resource[1024];
240 getResourceFromURI(printer_uri, http_resource, 1024);
241
242 if (printer_state_dyn == NULL) {
243 LOGE("get_PrinterState(): printer_state_dyn is null");
244 return ipp_status;
245 }
246
247 if (printer_state) {
248 *printer_state = IPP_PRINTER_STOPPED;
249 } else {
250 LOGE("get_PrinterState(): printer_state is null");
251 }
252 request = ippNewRequest(op);
253
254 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer_uri);
255
256 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes",
257 sizeof(pattrs) / sizeof(pattrs[0]), NULL, pattrs);
258
259 if ((response = ipp_doCupsRequest(http, request, http_resource, printer_uri)) == NULL) {
260 ipp_status = cupsLastError();
261 LOGE("get_PrinterState(): response is null: ipp_status %d", ipp_status);
262 printer_state_dyn->printer_status = PRINT_STATUS_UNABLE_TO_CONNECT;
263 printer_state_dyn->printer_reasons[0] = PRINT_STATUS_UNABLE_TO_CONNECT;
264 } else {
265 ipp_status = cupsLastError();
266 LOGD("ipp CUPS last ERROR: %d, %s", ipp_status, ippErrorString(ipp_status));
267 get_PrinterStateReason(response, printer_state, printer_state_dyn);
268 LOGD("get_PrinterState(): printer_state_dyn->printer_status: %d",
269 printer_state_dyn->printer_status);
270 }
271 LOGD("get_PrinterState(): exit http->fd %d, ipp_status %d, printer_state %d", http->fd,
272 ipp_status, printer_state_dyn->printer_status);
273
274 ippDelete(request);
275 ippDelete(response);
276 return ipp_status;
277 }
278
get_PrinterStateReason(ipp_t * response,ipp_pstate_t * printer_state,printer_state_dyn_t * printer_state_dyn)279 void get_PrinterStateReason(ipp_t *response, ipp_pstate_t *printer_state,
280 printer_state_dyn_t *printer_state_dyn) {
281 LOGD("get_PrinterStateReason(): Enter");
282 ipp_attribute_t *attrptr;
283 int reason_idx = 0;
284 int idx = 0;
285 ipp_pstate_t printer_ippstate = IPP_PRINTER_IDLE;
286
287 if ((attrptr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) == NULL) {
288 LOGE("get_PrinterStateReason printer-state null");
289 printer_state_dyn->printer_status = PRINT_STATUS_UNABLE_TO_CONNECT;
290 printer_state_dyn->printer_reasons[0] = PRINT_STATUS_UNABLE_TO_CONNECT;
291 } else {
292 printer_ippstate = (ipp_pstate_t) ippGetInteger(attrptr, 0);
293 *printer_state = printer_ippstate;
294
295 LOGD("get_PrinterStateReason printer-state: %d", printer_ippstate);
296 // set the printer_status; they may be modified based on the status reasons below.
297 switch (printer_ippstate) {
298 case IPP_PRINTER_IDLE:
299 printer_state_dyn->printer_status = PRINT_STATUS_IDLE;
300 break;
301 case IPP_PRINTER_PROCESSING:
302 printer_state_dyn->printer_status = PRINT_STATUS_PRINTING;
303 break;
304 case IPP_PRINTER_STOPPED:
305 printer_state_dyn->printer_status = PRINT_STATUS_SVC_REQUEST;
306 break;
307 }
308 }
309
310 if ((attrptr = ippFindAttribute(response, "printer-state-reasons", IPP_TAG_KEYWORD)) == NULL) {
311 LOGE(" get_PrinterStateReason printer-state reason null");
312 printer_state_dyn->printer_status = PRINT_STATUS_UNABLE_TO_CONNECT;
313 printer_state_dyn->printer_reasons[0] = PRINT_STATUS_UNABLE_TO_CONNECT;
314 } else {
315 for (idx = 0; idx < ippGetCount(attrptr); idx++) {
316 // Per RFC2911 any of these can have -error, -warning, or -report appended to end
317 LOGD("get_PrinterStateReason printer-state-reason: %s",
318 ippGetString(attrptr, idx, NULL));
319 if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_NONE,
320 strlen(IPP_PRNT_STATE_NONE)) == 0) {
321 switch (printer_ippstate) {
322 case IPP_PRINTER_IDLE:
323 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_IDLE;
324 break;
325 case IPP_PRINTER_PROCESSING:
326 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_PRINTING;
327 break;
328 case IPP_PRINTER_STOPPED:
329 // should this be PRINT_STATUS_SVC_REQUEST
330 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_UNKNOWN;
331 break;
332 }
333 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_SPOOL_FULL,
334 strlen(IPP_PRNT_STATE_SPOOL_FULL)) == 0) {
335 switch (printer_ippstate) {
336 case IPP_PRINTER_IDLE:
337 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_UNKNOWN;
338 break;
339 case IPP_PRINTER_PROCESSING:
340 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_PRINTING;
341 break;
342 case IPP_PRINTER_STOPPED:
343 // should this be PRINT_STATUS_SVC_REQUEST
344 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_UNKNOWN;
345 break;
346 }
347 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_MARKER_SUPPLY_LOW,
348 strlen(IPP_PRNT_STATE_MARKER_SUPPLY_LOW)) == 0) {
349 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_LOW_ON_INK;
350 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_TONER_LOW,
351 strlen(IPP_PRNT_STATE_TONER_LOW)) == 0) {
352 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_LOW_ON_TONER;
353 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_OTHER_WARN,
354 strlen(IPP_PRNT_STATE_OTHER_WARN)) == 0) {
355 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_UNKNOWN;
356 } else {
357 // check blocking cases
358 if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_MEDIA_NEEDED,
359 strlen(IPP_PRNT_STATE_MEDIA_NEEDED)) == 0) {
360 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_OUT_OF_PAPER;
361 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_MEDIA_EMPTY,
362 strlen(IPP_PRNT_STATE_MEDIA_EMPTY)) == 0) {
363 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_OUT_OF_PAPER;
364 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_TONER_EMPTY,
365 strlen(IPP_PRNT_STATE_TONER_EMPTY)) == 0) {
366 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_OUT_OF_TONER;
367 } else if (strncmp(ippGetString(attrptr, idx, NULL),
368 IPP_PRNT_STATE_MARKER_SUPPLY_EMPTY,
369 strlen(IPP_PRNT_STATE_MARKER_SUPPLY_EMPTY)) == 0) {
370 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_OUT_OF_INK;
371 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_DOOR_OPEN,
372 strlen(IPP_PRNT_STATE_DOOR_OPEN)) == 0) {
373 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_DOOR_OPEN;
374 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_COVER_OPEN,
375 strlen(IPP_PRNT_STATE_COVER_OPEN)) == 0) {
376 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_DOOR_OPEN;
377 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_MEDIA_JAM,
378 strlen(IPP_PRNT_STATE_MEDIA_JAM)) == 0) {
379 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_JAMMED;
380 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_SHUTDOWN,
381 strlen(IPP_PRNT_SHUTDOWN)) == 0) {
382 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_SHUTTING_DOWN;
383 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_STATE_OTHER_ERR,
384 strlen(IPP_PRNT_STATE_OTHER_ERR)) == 0) {
385 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_SVC_REQUEST;
386 } else if (strncmp(ippGetString(attrptr, idx, NULL), IPP_PRNT_PAUSED,
387 strlen(IPP_PRNT_PAUSED)) == 0) {
388 printer_state_dyn->printer_reasons[reason_idx++] = PRINT_STATUS_UNKNOWN;
389 }
390 }
391 } // end of reasons loop
392 }
393 }
394
print_col(ipp_t * col)395 static void print_col(ipp_t *col) {
396 int i;
397 ipp_attribute_t *attr;
398
399 LOGD("{");
400 for (attr = ippFirstAttribute(col); attr; attr = ippNextAttribute(col)) {
401 switch (ippGetValueTag(attr)) {
402 case IPP_TAG_INTEGER:
403 case IPP_TAG_ENUM:
404 for (i = 0; i < ippGetCount(attr); i++) {
405 LOGD(" %s(%s%s)= %d ", ippGetName(attr),
406 ippGetCount(attr) > 1 ? "1setOf " : "",
407 ippTagString(ippGetValueTag(attr)), ippGetInteger(attr, i));
408 }
409 break;
410 case IPP_TAG_BOOLEAN:
411 for (i = 0; i < ippGetCount(attr); i++) {
412 if (ippGetBoolean(attr, i)) {
413 LOGD(" %s(%s%s)= true ", ippGetName(attr),
414 ippGetCount(attr) > 1 ? "1setOf " : "",
415 ippTagString(ippGetValueTag(attr)));
416 } else {
417 LOGD(" %s(%s%s)= false ", ippGetName(attr),
418 ippGetCount(attr) > 1 ? "1setOf " : "",
419 ippTagString(ippGetValueTag(attr)));
420 }
421 }
422 break;
423 case IPP_TAG_NOVALUE:
424 LOGD(" %s(%s%s)= novalue", ippGetName(attr),
425 ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)));
426 break;
427 case IPP_TAG_RANGE:
428 for (i = 0; i < ippGetCount(attr); i++) {
429 int lower, upper;
430 lower = ippGetRange(attr, i, &upper);
431 LOGD(" %s(%s%s)= %d-%d ", ippGetName(attr),
432 ippGetCount(attr) > 1 ? "1setOf " : "",
433 ippTagString(ippGetValueTag(attr)), lower, upper);
434 }
435 break;
436 case IPP_TAG_RESOLUTION:
437 for (i = 0; i < ippGetCount(attr); i++) {
438 ipp_res_t units;
439 int xres, yres;
440 xres = ippGetResolution(attr, i, &yres, &units);
441 LOGD(" %s(%s%s)= %dx%d%s ", ippGetName(attr),
442 ippGetCount(attr) > 1 ? "1setOf " : "",
443 ippTagString(ippGetValueTag(attr)), xres, yres,
444 units == IPP_RES_PER_INCH ? "dpi" : "dpc");
445 }
446 break;
447 case IPP_TAG_STRING:
448 case IPP_TAG_TEXT:
449 case IPP_TAG_NAME:
450 case IPP_TAG_KEYWORD:
451 case IPP_TAG_CHARSET:
452 case IPP_TAG_URI:
453 case IPP_TAG_MIMETYPE:
454 case IPP_TAG_LANGUAGE:
455 for (i = 0; i < ippGetCount(attr); i++) {
456 LOGD(" %s(%s%s)= \"%s\" ", ippGetName(attr),
457 ippGetCount(attr) > 1 ? "1setOf " : "",
458 ippTagString(ippGetValueTag(attr)), ippGetString(attr, i, NULL));
459 }
460 break;
461 case IPP_TAG_TEXTLANG:
462 case IPP_TAG_NAMELANG:
463 for (i = 0; i < ippGetCount(attr); i++) {
464 const char *charset;
465 const char *text;
466 text = ippGetString(attr, i, &charset);
467 LOGD(" %s(%s%s)= \"%s\",%s ", ippGetName(attr),
468 ippGetCount(attr) > 1 ? "1setOf " : "",
469 ippTagString(ippGetValueTag(attr)), text, charset);
470 }
471 break;
472 case IPP_TAG_BEGIN_COLLECTION:
473 for (i = 0; i < ippGetCount(attr); i++) {
474 print_col(ippGetCollection(attr, i));
475 }
476 break;
477 default:
478 break;
479 }
480 }
481 LOGD("}");
482 }
483
print_attr(ipp_attribute_t * attr)484 void print_attr(ipp_attribute_t *attr) {
485 int i;
486
487 if (ippGetName(attr) == NULL) {
488 return;
489 }
490
491 switch (ippGetValueTag(attr)) {
492 case IPP_TAG_INTEGER:
493 case IPP_TAG_ENUM:
494 for (i = 0; i < ippGetCount(attr); i++) {
495 LOGD("%s (%s%s) = %d ", ippGetName(attr), ippGetCount(attr) > 1 ? "1setOf " : "",
496 ippTagString(ippGetValueTag(attr)), ippGetInteger(attr, i));
497 }
498 break;
499 case IPP_TAG_BOOLEAN:
500 for (i = 0; i < ippGetCount(attr); i++) {
501 if (ippGetBoolean(attr, i)) {
502 LOGD("%s (%s%s) = true ", ippGetName(attr),
503 ippGetCount(attr) > 1 ? "1setOf " : "",
504 ippTagString(ippGetValueTag(attr)));
505 } else {
506 LOGD("%s (%s%s) = false ", ippGetName(attr),
507 ippGetCount(attr) > 1 ? "1setOf " : "",
508 ippTagString(ippGetValueTag(attr)));
509 }
510 }
511 break;
512 case IPP_TAG_NOVALUE:
513 LOGD("%s (%s%s) = novalue", ippGetName(attr),
514 ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)));
515 break;
516 case IPP_TAG_RANGE:
517 for (i = 0; i < ippGetCount(attr); i++) {
518 int lower, upper;
519 lower = ippGetRange(attr, i, &upper);
520 LOGD("%s (%s%s) = %d-%d ", ippGetName(attr), ippGetCount(attr) > 1 ? "1setOf " : "",
521 ippTagString(ippGetValueTag(attr)), lower, upper);
522 }
523 break;
524 case IPP_TAG_RESOLUTION:
525 for (i = 0; i < ippGetCount(attr); i++) {
526 ipp_res_t units;
527 int xres, yres;
528 xres = ippGetResolution(attr, i, &yres, &units);
529 LOGD("%s (%s%s) = %dx%d%s ", ippGetName(attr),
530 ippGetCount(attr) > 1 ? "1setOf " : "",
531 ippTagString(ippGetValueTag(attr)), xres, yres,
532 units == IPP_RES_PER_INCH ? "dpi" : "dpc");
533 }
534 break;
535 case IPP_TAG_STRING:
536 case IPP_TAG_TEXT:
537 case IPP_TAG_NAME:
538 case IPP_TAG_KEYWORD:
539 case IPP_TAG_CHARSET:
540 case IPP_TAG_URI:
541 case IPP_TAG_MIMETYPE:
542 case IPP_TAG_LANGUAGE:
543 for (i = 0; i < ippGetCount(attr); i++) {
544 LOGD("%s (%s%s) = \"%s\" ", ippGetName(attr),
545 ippGetCount(attr) > 1 ? "1setOf " : "",
546 ippTagString(ippGetValueTag(attr)), ippGetString(attr, i, NULL));
547 }
548 break;
549 case IPP_TAG_TEXTLANG:
550 case IPP_TAG_NAMELANG:
551 for (i = 0; i < ippGetCount(attr); i++) {
552 const char *charset;
553 const char *text;
554 text = ippGetString(attr, i, &charset);
555 LOGD("%s (%s%s) = \"%s\",%s ", ippGetName(attr),
556 ippGetCount(attr) > 1 ? "1setOf " : "",
557 ippTagString(ippGetValueTag(attr)), text, charset);
558 }
559 break;
560
561 case IPP_TAG_BEGIN_COLLECTION:
562 for (i = 0; i < ippGetCount(attr); i++) {
563 LOGD("%s (%s%s): IPP_TAG_BEGIN_COLLECTION", ippGetName(attr),
564 ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)));
565 print_col(ippGetCollection(attr, i));
566 }
567 LOGD("%s (%s%s): IPP_TAG_END_COLLECTION", ippGetName(attr),
568 ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)));
569 break;
570
571 default:
572 break;
573 }
574 }
575
parse_IPPVersions(ipp_t * response,ipp_version_supported_t * ippVersions)576 void parse_IPPVersions(ipp_t *response, ipp_version_supported_t *ippVersions) {
577 int i;
578 ipp_attribute_t *attrptr;
579 char ipp10[] = "1.0";
580 char ipp11[] = "1.1";
581 char ipp20[] = "2.0";
582 LOGD(" Entered IPPVersions");
583 if (ippVersions != NULL) {
584 memset(ippVersions, 0, sizeof(ipp_version_supported_t));
585 LOGD(" in get_supportedIPPVersions");
586 attrptr = ippFindAttribute(response, "ipp-versions-supported", IPP_TAG_KEYWORD);
587 if (attrptr != NULL) {
588 LOGD(" in get_supportedIPPVersions: %d", ippGetCount(attrptr));
589 for (i = 0; i < ippGetCount(attrptr); i++) {
590 if (strcmp(ipp10, ippGetString(attrptr, i, NULL)) == 0) {
591 ippVersions->supportsIpp10 = 1;
592 } else if (strcmp(ipp11, ippGetString(attrptr, i, NULL)) == 0) {
593 ippVersions->supportsIpp11 = 1;
594 } else if (strcmp(ipp20, ippGetString(attrptr, i, NULL)) == 0) {
595 ippVersions->supportsIpp20 = 1;
596 } else {
597 LOGD("found another ipp version. %s", ippGetString(attrptr, i, NULL));
598 }
599 }
600 }
601 }
602 }
603
mapDFMediaToIPPKeyword(media_size_t media_size)604 const char *mapDFMediaToIPPKeyword(media_size_t media_size) {
605 int i;
606 for (i = 0; i < SUPPORTED_MEDIA_SIZE_COUNT; i++) {
607 if (SupportedMediaSizes[i].media_size == (media_size_t) media_size) {
608 return (SupportedMediaSizes[i].PWGName);
609 }
610 }
611 return (SupportedMediaSizes[0].PWGName);
612 }
613
ipp_find_media_size(const char * ipp_media_keyword,media_size_t * media_size)614 int ipp_find_media_size(const char *ipp_media_keyword, media_size_t *media_size) {
615 int i;
616 LOGD("ipp_find_media_size entry is %s", ipp_media_keyword);
617 for (i = 0; i < SUPPORTED_MEDIA_SIZE_COUNT; i++) {
618 if (strcmp(SupportedMediaSizes[i].PWGName, ipp_media_keyword) == 0) {
619 LOGD(" mediaArraySize: match string %s PT_size: %d",
620 SupportedMediaSizes[i].PWGName, SupportedMediaSizes[i].media_size);
621 break;
622 }
623 }
624 if (i < SUPPORTED_MEDIA_SIZE_COUNT) {
625 *media_size = SupportedMediaSizes[i].media_size;
626 return i;
627 } else {
628 return -1;
629 }
630 return -1;
631 }
632
parse_getMediaSupported(ipp_t * response,media_supported_t * media_supported)633 void parse_getMediaSupported(ipp_t *response, media_supported_t *media_supported) {
634 int i;
635 ipp_attribute_t *attrptr;
636 int sizes_idx = 0;
637 LOGD(" Entered getMediaSupported");
638
639 media_size_t media_sizeTemp;
640 int idx = 0;
641
642 if ((attrptr = ippFindAttribute(response, "media-supported", IPP_TAG_KEYWORD)) != NULL) {
643 LOGD("media-supported found; number of values %d", ippGetCount(attrptr));
644 for (i = 0; i < ippGetCount(attrptr); i++) {
645 idx = ipp_find_media_size(ippGetString(attrptr, i, NULL), &media_sizeTemp);
646
647 // Modified since anytime the find media size returned 0 it could either mean
648 // NOT found or na_letter.
649 if (idx >= 0) {
650 media_supported->media_size[sizes_idx] = media_sizeTemp;
651 media_supported->idxKeywordTranTable[sizes_idx] = idx;
652 sizes_idx++;
653 }
654 }
655 } else {
656 LOGD("media-supported not found");
657 }
658 }
659
get_supportedPrinterResolutions(ipp_attribute_t * attrptr,printer_capabilities_t * capabilities)660 static void get_supportedPrinterResolutions(ipp_attribute_t *attrptr,
661 printer_capabilities_t *capabilities) {
662 int idx = 0;
663 int i;
664 for (i = 0; i < ippGetCount(attrptr); i++) {
665 ipp_res_t units;
666 int xres, yres;
667 xres = ippGetResolution(attrptr, i, &yres, &units);
668 if (units == IPP_RES_PER_INCH) {
669 if ((idx < MAX_RESOLUTIONS_SUPPORTED) && (xres == yres)) {
670 capabilities->supportedResolutions[idx] = xres;
671 idx++;
672 }
673 }
674 }
675 capabilities->numSupportedResolutions = idx;
676 }
677
getResourceFromURI(const char * uri,char * resource,int resourcelen)678 void getResourceFromURI(const char *uri, char *resource, int resourcelen) {
679 char scheme[1024];
680 char username[1024];
681 char host[1024];
682 int port;
683 httpSeparateURI(0, uri, scheme, 1024, username, 1024, host, 1024, &port, resource, resourcelen);
684 }
685
686 /*
687 * Add a new media type to a printer's collection of supported media types
688 */
addMediaType(printer_capabilities_t * capabilities,media_type_t mediaType)689 static void addMediaType(printer_capabilities_t *capabilities, media_type_t mediaType) {
690 int index;
691 for (index = 0; index < capabilities->numSupportedMediaTypes; index++) {
692 // Skip if already present
693 if (capabilities->supportedMediaTypes[index] == mediaType) return;
694 }
695
696 // Add if not found and not too many
697 if (capabilities->numSupportedMediaTypes < MAX_MEDIA_TYPES_SUPPORTED) {
698 capabilities->supportedMediaTypes[capabilities->numSupportedMediaTypes++] = mediaType;
699 } else {
700 LOGI("Hit MAX_MEDIA_TYPES_SUPPORTED while adding %d", mediaType);
701 }
702 }
703
parse_printerAttributes(ipp_t * response,printer_capabilities_t * capabilities)704 void parse_printerAttributes(ipp_t *response, printer_capabilities_t *capabilities) {
705 int i, j;
706 ipp_attribute_t *attrptr;
707
708 LOGD("Entered parse_printerAttributes");
709
710 media_supported_t media_supported;
711 for (i = 0; i <= PAGE_STATUS_MAX - 1; i++) {
712 media_supported.media_size[i] = 0;
713 }
714 parse_getMediaSupported(response, &media_supported);
715
716 parse_printerUris(response, capabilities);
717
718 LOGD("Media Supported: ");
719 int idx = 0;
720 capabilities->numSupportedMediaTypes = 0;
721 for (i = 0; i <= PAGE_STATUS_MAX - 1; i++) {
722 if (media_supported.media_size[i] != 0) {
723 capabilities->supportedMediaSizes[capabilities->numSupportedMediaSizes++] =
724 media_supported.media_size[i];
725 idx = media_supported.idxKeywordTranTable[i];
726 LOGD(" i %d, \tPT_Size: %d \tidx %d \tKeyword: %s", i, media_supported.media_size[i],
727 idx, SupportedMediaSizes[idx].PWGName);
728 }
729 }
730
731 if ((attrptr = ippFindAttribute(response, "printer-dns-sd-name", IPP_TAG_NAME)) != NULL) {
732 strlcpy(capabilities->name, ippGetString(attrptr, 0, NULL), sizeof(capabilities->name));
733 }
734
735 if (!capabilities->name[0]) {
736 if ((attrptr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL) {
737 strlcpy(capabilities->name, ippGetString(attrptr, 0, NULL), sizeof(capabilities->name));
738 }
739 }
740
741 if (!capabilities->name[0]) {
742 if ((attrptr = ippFindAttribute(response, "printer-name", IPP_TAG_TEXT)) != NULL) {
743 strlcpy(capabilities->name, ippGetString(attrptr, 0, NULL), sizeof(capabilities->name));
744 }
745 }
746
747 if ((attrptr = ippFindAttribute(response, "printer-make-and-model", IPP_TAG_TEXT)) != NULL) {
748 strlcpy(capabilities->make, ippGetString(attrptr, 0, NULL), sizeof(capabilities->make));
749 }
750
751 if ((attrptr = ippFindAttribute(response, "printer-uuid", IPP_TAG_URI)) != NULL) {
752 strlcpy(capabilities->uuid, ippGetString(attrptr, 0, NULL), sizeof(capabilities->uuid));
753 }
754
755 if ((attrptr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL) {
756 strlcpy(capabilities->location, ippGetString(attrptr, 0, NULL),
757 sizeof(capabilities->location));
758 }
759
760 if ((attrptr = ippFindAttribute(response, "media-default", IPP_TAG_KEYWORD)) != NULL) {
761 strlcpy(capabilities->mediaDefault, ippGetString(attrptr, 0, NULL),
762 sizeof(capabilities->mediaDefault));
763 }
764
765 if ((attrptr = ippFindAttribute(response, "color-supported", IPP_TAG_BOOLEAN)) != NULL) {
766 if (ippGetBoolean(attrptr, 0)) {
767 capabilities->color = 1;
768 }
769 }
770 if ((attrptr = ippFindAttribute(response, "copies-supported", IPP_TAG_RANGE)) != NULL) {
771 int upper = 0;
772 for (i = 0; i < ippGetCount(attrptr); i++) {
773 ippGetRange(attrptr, i, &upper);
774 }
775 if (upper > 1) {
776 capabilities->canCopy = 1;
777 }
778 }
779 if ((attrptr = ippFindAttribute(response, "print-color-mode-supported", IPP_TAG_KEYWORD)) !=
780 NULL) {
781 for (i = 0; i < ippGetCount(attrptr); i++) {
782 if (strcmp("color", ippGetString(attrptr, i, NULL)) == 0) {
783 capabilities->color = 1;
784 }
785 }
786 }
787 if ((attrptr = ippFindAttribute(response, "print-quality-supported", IPP_TAG_ENUM)) !=
788 NULL) {
789 for (i = 0; i < ippGetCount(attrptr) && capabilities->numSupportedQuality
790 < MAX_QUALITY_SUPPORTED; i++) {
791 LOGD("print-quality-supported: %d", ippGetInteger(attrptr, i));
792 capabilities->supportedQuality[capabilities->numSupportedQuality++] =
793 ippGetInteger(attrptr, i);
794 }
795 }
796
797 char imagePCLm[] = "application/PCLm";
798 char imagePWG[] = "image/pwg-raster";
799 char imagePDF[] = "image/pdf";
800 char applicationPDF[] = "application/pdf";
801
802 if ((attrptr = ippFindAttribute(response, "document-format-supported", IPP_TAG_MIMETYPE))
803 != NULL) {
804 for (i = 0; i < ippGetCount(attrptr); i++) {
805 if (strcmp(imagePDF, ippGetString(attrptr, i, NULL)) == 0) {
806 capabilities->canPrintPDF = 1;
807 } else if (strcmp(applicationPDF, ippGetString(attrptr, i, NULL)) == 0) {
808 capabilities->canPrintPDF = 1;
809 } else if (strcmp(imagePCLm, ippGetString(attrptr, i, NULL)) == 0) {
810 capabilities->canPrintPCLm = 1;
811 } else if (strcmp(applicationPDF, ippGetString(attrptr, i, NULL)) == 0) {
812 capabilities->canPrintPDF = 1;
813 } else if (strcmp(imagePWG, ippGetString(attrptr, i, NULL)) == 0) {
814 capabilities->canPrintPWG = 1;
815 }
816 }
817 }
818
819 if ((attrptr = ippFindAttribute(response, "sides-supported", IPP_TAG_KEYWORD)) != NULL) {
820 for (i = 0; i < ippGetCount(attrptr); i++) {
821 if (strcmp(IPP_SIDES_TWO_SIDED_SHORT_EDGE, ippGetString(attrptr, i, NULL)) == 0) {
822 capabilities->duplex = 1;
823 } else if (strcmp(IPP_SIDES_TWO_SIDED_LONG_EDGE, ippGetString(attrptr, i, NULL)) == 0) {
824 capabilities->duplex = 1;
825 }
826 }
827 }
828
829 // Look up supported media types
830 capabilities->numSupportedMediaTypes = 0;
831 if (((attrptr = ippFindAttribute(response, "media-type-supported", IPP_TAG_KEYWORD)) != NULL)
832 || ((attrptr = ippFindAttribute(response, "media-type-supported", IPP_TAG_NAME))
833 != NULL)) {
834 for (i = 0; i < ippGetCount(attrptr); i++) {
835 if (strcasestr(ippGetString(attrptr, i, NULL), "photographic-glossy")) {
836 addMediaType(capabilities, MEDIA_PHOTO_GLOSSY);
837 } else if (strcasestr(ippGetString(attrptr, i, NULL), "photo")) {
838 addMediaType(capabilities, MEDIA_PHOTO);
839 } else if (strcasestr(ippGetString(attrptr, i, NULL), "stationery")) {
840 addMediaType(capabilities, MEDIA_PLAIN);
841 }
842 }
843 }
844
845 if (capabilities->numSupportedMediaTypes == 0) {
846 // If no recognized media types were found, fall back to all 3 just in case
847 addMediaType(capabilities, MEDIA_PLAIN);
848 addMediaType(capabilities, MEDIA_PHOTO);
849 addMediaType(capabilities, MEDIA_PHOTO_GLOSSY);
850 }
851
852 capabilities->numSupportedResolutions = 0;
853 // only appears that SMM supports the pclm-source-resolution-supported attribute
854 // if that is not present, use the printer-resolution-supported attribute to determine
855 // if 300DPI is supported
856 if ((attrptr = ippFindAttribute(response, "pclm-source-resolution-supported",
857 IPP_TAG_RESOLUTION)) != NULL) {
858 get_supportedPrinterResolutions(attrptr, capabilities);
859 } else if ((attrptr = ippFindAttribute(response, "printer-resolution-supported",
860 IPP_TAG_RESOLUTION)) != NULL) {
861 get_supportedPrinterResolutions(attrptr, capabilities);
862 }
863
864 char ipp10[] = "1.0";
865 char ipp11[] = "1.1";
866 char ipp20[] = "2.0";
867
868 if ((attrptr = ippFindAttribute(response, "ipp-versions-supported", IPP_TAG_KEYWORD)) != NULL) {
869 unsigned char supportsIpp20 = 0;
870 unsigned char supportsIpp11 = 0;
871 unsigned char supportsIpp10 = 0;
872
873 for (i = 0; i < ippGetCount(attrptr); i++) {
874 if (strcmp(ipp10, ippGetString(attrptr, i, NULL)) == 0) {
875 supportsIpp10 = 1;
876 } else if (strcmp(ipp11, ippGetString(attrptr, i, NULL)) == 0) {
877 supportsIpp11 = 1;
878 } else if (strcmp(ipp20, ippGetString(attrptr, i, NULL)) == 0) {
879 supportsIpp20 = 1;
880 } else {
881 LOGD("found another ipp version. %s", ippGetString(attrptr, i, NULL));
882 }
883 if (supportsIpp20) {
884 capabilities->ippVersionMajor = 2;
885 capabilities->ippVersionMinor = 0;
886 } else if (supportsIpp11) {
887 capabilities->ippVersionMajor = 1;
888 capabilities->ippVersionMinor = 1;
889 } else if (supportsIpp10) {
890 capabilities->ippVersionMajor = 1;
891 capabilities->ippVersionMinor = 0;
892 } else {
893 // default to 1.0
894 capabilities->ippVersionMajor = 1;
895 capabilities->ippVersionMinor = 0;
896 }
897 }
898 }
899
900 char epcl10[] = "1.0";
901 if ((attrptr = ippFindAttribute(response, "epcl-version-supported", IPP_TAG_KEYWORD)) != NULL) {
902 for (i = 0; i < ippGetCount(attrptr); i++) {
903 LOGD("setting epcl_ipp_version (KEYWORD) %s", ippGetString(attrptr, i, NULL));
904
905 // substring match because different devices implemented spec differently
906 if (strstr(ippGetString(attrptr, i, NULL), epcl10) != NULL) {
907 LOGD("setting epcl_ipp_version = 1");
908 capabilities->ePclIppVersion = 1;
909 }
910 }
911 }
912
913 if ((attrptr = ippFindAttribute(response, "epcl-version-supported", IPP_TAG_TEXT)) != NULL) {
914 for (i = 0; i < ippGetCount(attrptr); i++) {
915 LOGD("setting epcl_ipp_verion (TEXT) %s", ippGetString(attrptr, i, NULL));
916
917 // substring match because different devices implemented spec differently
918 if (strstr(ippGetString(attrptr, i, NULL), epcl10) != NULL) {
919 LOGD("setting epcl_ipp_verion = 1");
920 capabilities->ePclIppVersion = 1;
921 }
922 }
923 }
924
925 if ((attrptr = ippFindAttribute(response, "media-col-default", IPP_TAG_BEGIN_COLLECTION)) !=
926 NULL) {
927 for (i = 0; i < ippGetCount(attrptr); i++) {
928 LOGD("Gathering margins supported");
929
930 ipp_t *collection = ippGetCollection(attrptr, i);
931
932 for (j = 0, attrptr = ippFirstAttribute(collection);
933 (j < 4) && (attrptr != NULL); attrptr = ippNextAttribute(collection)) {
934 if (strcmp("media-top-margin", ippGetName(attrptr)) == 0) {
935 capabilities->printerTopMargin = ippGetInteger(attrptr, 0);
936 } else if (strcmp("media-bottom-margin", ippGetName(attrptr)) == 0) {
937 capabilities->printerBottomMargin = ippGetInteger(attrptr, 0);
938 } else if (strcmp("media-left-margin", ippGetName(attrptr)) == 0) {
939 capabilities->printerLeftMargin = ippGetInteger(attrptr, 0);
940 } else if (strcmp("media-right-margin", ippGetName(attrptr)) == 0) {
941 capabilities->printerRightMargin = ippGetInteger(attrptr, 0);
942 }
943 }
944 }
945 }
946
947 if ((attrptr = ippFindAttribute(response, "media-size-name", IPP_TAG_KEYWORD)) != NULL) {
948 capabilities->isMediaSizeNameSupported = true;
949 } else {
950 capabilities->isMediaSizeNameSupported = false;
951 }
952
953 // is strip length supported? if so, stored in capabilities
954 if ((attrptr = ippFindAttribute(response, "pclm-strip-height-preferred",
955 IPP_TAG_INTEGER)) != NULL) {
956 LOGD("pclm-strip-height-preferred=%d", ippGetInteger(attrptr, 0));
957
958 // if the strip height is 0, the device wants us to send the entire page in one band
959 // (according to ePCL spec). Since our code doesn't currently support generating an entire
960 // page in one band, set the strip height to the default value every device *should* support
961 // also, for some reason our code crashes when it attempts to generate strips at 512 or
962 // above. Therefore, limiting the upper bound strip height to 256
963 if (ippGetInteger(attrptr, 0) == 0 || ippGetInteger(attrptr, 0) > 256) {
964 capabilities->stripHeight = STRIPE_HEIGHT;
965 } else {
966 capabilities->stripHeight = ippGetInteger(attrptr, 0);
967 }
968 } else {
969 capabilities->stripHeight = STRIPE_HEIGHT;
970 }
971
972 // what is the preferred compression method - jpeg, flate, rle
973 if ((attrptr = ippFindAttribute(response, "pclm-compression-method-preferred",
974 IPP_TAG_KEYWORD)) != NULL) {
975 LOGD("pclm-compression-method-preferred=%s", ippGetString(attrptr, 0, NULL));
976 }
977
978 // is device able to rotate back page for duplex jobs?
979 if ((attrptr = ippFindAttribute(response, "pclm-raster-back-side", IPP_TAG_KEYWORD)) != NULL) {
980 LOGD("pclm-raster-back-side=%s", ippGetString(attrptr, 0, NULL));
981 if (strcmp(ippGetString(attrptr, 0, NULL), "rotated") == 0) {
982 capabilities->canRotateDuplexBackPage = 0;
983 LOGD("Device cannot rotate back page for duplex jobs.");
984 } else {
985 capabilities->canRotateDuplexBackPage = 1;
986 }
987 }
988
989 // look for full-bleed supported by looking for 0 on all margins
990 bool topsupported = false, bottomsupported = false, rightsupported = false,
991 leftsupported = false;
992 if ((attrptr = ippFindAttribute(response, "media-top-margin-supported", IPP_TAG_INTEGER)) !=
993 NULL) {
994 for (i = 0; i < ippGetCount(attrptr); i++) {
995 if (ippGetInteger(attrptr, i) == 0) {
996 LOGD("Top Margin Supported");
997 topsupported = true;
998 break;
999 }
1000 }
1001 }
1002 if ((attrptr = ippFindAttribute(response, "media-bottom-margin-supported", IPP_TAG_INTEGER)) !=
1003 NULL) {
1004 for (i = 0; i < ippGetCount(attrptr); i++) {
1005 if (ippGetInteger(attrptr, i) == 0) {
1006 LOGD("Bottom Margin Supported");
1007 bottomsupported = true;
1008 break;
1009 }
1010 }
1011 }
1012 if ((attrptr = ippFindAttribute(response, "media-right-margin-supported", IPP_TAG_INTEGER)) !=
1013 NULL) {
1014 for (i = 0; i < ippGetCount(attrptr); i++) {
1015 if (ippGetInteger(attrptr, i) == 0) {
1016 LOGD("Right Margin Supported");
1017 rightsupported = true;
1018 break;
1019 }
1020 }
1021 }
1022 if ((attrptr = ippFindAttribute(response, "media-left-margin-supported", IPP_TAG_INTEGER)) !=
1023 NULL) {
1024 for (i = 0; i < ippGetCount(attrptr); i++) {
1025 if (ippGetInteger(attrptr, i) == 0) {
1026 LOGD("Left Margin Supported");
1027 leftsupported = true;
1028 break;
1029 }
1030 }
1031 }
1032
1033 if (topsupported && bottomsupported && rightsupported && leftsupported) {
1034 LOGD("full-bleed is supported");
1035 capabilities->borderless = 1;
1036 } else {
1037 LOGD("full-bleed is NOT supported");
1038 }
1039
1040 if ((attrptr = ippFindAttribute(response, "printer-device-id", IPP_TAG_TEXT)) != NULL) {
1041 if (strstr(ippGetString(attrptr, 0, NULL), "PCL3GUI") != NULL) {
1042 capabilities->inkjet = 1;
1043 }
1044 } else if (capabilities->borderless == 1) {
1045 capabilities->inkjet = 1;
1046 }
1047
1048 // determine if device prints pages face-down
1049 capabilities->faceDownTray = 1;
1050 if ((attrptr = ippFindAttribute(response, "output-bin-supported", IPP_TAG_KEYWORD)) != NULL) {
1051 if (strstr(ippGetString(attrptr, 0, NULL), "face-up") != NULL) {
1052 capabilities->faceDownTray = 0;
1053 }
1054 }
1055
1056 // Determine supported document format details
1057 if ((attrptr = ippFindAttribute(response, "document-format-details-supported",
1058 IPP_TAG_KEYWORD)) != NULL) {
1059 for (i = 0; i < ippGetCount(attrptr); i++) {
1060 if (strcmp("document-source-application-name", ippGetString(attrptr, i, NULL)) == 0) {
1061 capabilities->docSourceAppName = 1;
1062 } else if (
1063 strcmp("document-source-application-version", ippGetString(attrptr, i, NULL)) ==
1064 0) {
1065 capabilities->docSourceAppVersion = 1;
1066 } else if (strcmp("document-source-os-name", ippGetString(attrptr, i, NULL)) == 0) {
1067 capabilities->docSourceOsName = 1;
1068 } else if (strcmp("document-source-os-version", ippGetString(attrptr, i, NULL)) == 0) {
1069 capabilities->docSourceOsVersion = 1;
1070 }
1071 }
1072 }
1073 debuglist_printerCapabilities(capabilities);
1074 }
1075
1076 // Used in parse_printerUris
1077 #define MAX_URIS 10
1078 typedef struct {
1079 const char *uri;
1080 int valid;
1081 } parsed_uri_t;
1082
parse_printerUris(ipp_t * response,printer_capabilities_t * capabilities)1083 static void parse_printerUris(ipp_t *response, printer_capabilities_t *capabilities) {
1084 ipp_attribute_t *attrptr;
1085 int i;
1086 parsed_uri_t uris[MAX_URIS] = {0};
1087
1088 if ((attrptr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) {
1089 for (i = 0; i < MIN(ippGetCount(attrptr), MAX_URIS); i++) {
1090 uris[i].uri = ippGetString(attrptr, i, NULL);
1091 uris[i].valid = true;
1092 }
1093 }
1094
1095 // If security or authentication is required (non-"none") at any URI, mark it invalid
1096
1097 if ((attrptr = ippFindAttribute(response, "uri-security-supported", IPP_TAG_KEYWORD)) != NULL) {
1098 for (i = 0; i < MIN(ippGetCount(attrptr), MAX_URIS); i++) {
1099 if (strcmp("none", ippGetString(attrptr, i, NULL)) != 0) {
1100 LOGD("parse_printerUris %s invalid because sec=%s", uris[i].uri,
1101 ippGetString(attrptr, i, NULL));
1102 uris[i].valid = false;
1103 }
1104 }
1105 }
1106
1107 if ((attrptr = ippFindAttribute(response, "uri-authentication-supported", IPP_TAG_KEYWORD))
1108 != NULL) {
1109 for (i = 0; i < MIN(ippGetCount(attrptr), MAX_URIS); i++) {
1110 // Allow "none" and "requesting-user-name" only
1111 if (strcmp("none", ippGetString(attrptr, i, NULL)) != 0 &&
1112 strcmp("requesting-user-name", ippGetString(attrptr, i, NULL)) != 0) {
1113 LOGD("parse_printerUris %s invalid because auth=%s", uris[i].uri,
1114 ippGetString(attrptr, i, NULL));
1115 uris[i].valid = false;
1116 }
1117 }
1118 }
1119
1120 // Find a valid URI and copy it into place.
1121 for (i = 0; i < MAX_URIS; i++) {
1122 if (uris[i].valid) {
1123 LOGD("parse_printerUris found %s", uris[i].uri);
1124 strlcpy(capabilities->printerUri, uris[i].uri, sizeof(capabilities->printerUri));
1125 break;
1126 }
1127 }
1128 }
1129
debuglist_printerCapabilities(printer_capabilities_t * capabilities)1130 void debuglist_printerCapabilities(printer_capabilities_t *capabilities) {
1131 LOGD("printer make: %s", capabilities->make);
1132 LOGD("printer default media: %s", capabilities->mediaDefault);
1133 LOGD("canPrintPDF: %d", capabilities->canPrintPDF);
1134 LOGD("duplex: %d", capabilities->duplex);
1135 LOGD("canRotateDuplexBackPage: %d", capabilities->canRotateDuplexBackPage);
1136 LOGD("color: %d", capabilities->color);
1137 LOGD("canCopy: %d", capabilities->canCopy);
1138 LOGD("ippVersionMajor: %d", capabilities->ippVersionMajor);
1139 LOGD("ippVersionMinor: %d", capabilities->ippVersionMinor);
1140 LOGD("strip height: %d", capabilities->stripHeight);
1141 LOGD("faceDownTray: %d", capabilities->faceDownTray);
1142 }
1143
debuglist_printerStatus(printer_state_dyn_t * printer_state_dyn)1144 void debuglist_printerStatus(printer_state_dyn_t *printer_state_dyn) {
1145 const char *decoded = "unknown";
1146 if (printer_state_dyn->printer_status == PRINT_STATUS_INITIALIZING) {
1147 decoded = "Initializing";
1148 } else if (printer_state_dyn->printer_status == PRINT_STATUS_SHUTTING_DOWN) {
1149 decoded = "Shutting Down";
1150 } else if (printer_state_dyn->printer_status == PRINT_STATUS_UNABLE_TO_CONNECT) {
1151 decoded = "Unable To Connect";
1152 } else if (printer_state_dyn->printer_status == PRINT_STATUS_UNKNOWN) {
1153 decoded = "Unknown";
1154 } else if (printer_state_dyn->printer_status == PRINT_STATUS_OFFLINE) {
1155 decoded = "Offline";
1156 } else if (printer_state_dyn->printer_status == PRINT_STATUS_IDLE) {
1157 decoded = "Idle";
1158 } else if (printer_state_dyn->printer_status == PRINT_STATUS_PRINTING) {
1159 decoded = "Printing";
1160 } else if (printer_state_dyn->printer_status == PRINT_STATUS_OUT_OF_PAPER) {
1161 decoded = "Out Of Paper";
1162 } else if (printer_state_dyn->printer_status == PRINT_STATUS_OUT_OF_INK) {
1163 decoded = "Out Of Ink";
1164 } else if (printer_state_dyn->printer_status == PRINT_STATUS_JAMMED) {
1165 decoded = "Jammed";
1166 } else if (printer_state_dyn->printer_status == PRINT_STATUS_DOOR_OPEN) {
1167 decoded = "Door Open";
1168 } else if (printer_state_dyn->printer_status == PRINT_STATUS_SVC_REQUEST) {
1169 decoded = "Service Request";
1170 }
1171 LOGD("printer status: %d (%s)", printer_state_dyn->printer_status, decoded);
1172
1173 int idx = 0;
1174 for (idx = 0; idx < (PRINT_STATUS_MAX_STATE + 1); idx++) {
1175 if (PRINT_STATUS_MAX_STATE != printer_state_dyn->printer_reasons[idx]) {
1176 LOGD("printer_reasons (%d): %d", idx, printer_state_dyn->printer_reasons[idx]);
1177 }
1178 }
1179 }
1180
1181 /*
1182 * Handle server certificate information.
1183 */
ipp_server_cert_cb(http_t * http,void * tls,cups_array_t * certs,void * user_data)1184 static int ipp_server_cert_cb(http_t *http, void *tls, cups_array_t *certs, void *user_data) {
1185 wprint_connect_info_t *connect_info = (wprint_connect_info_t *)user_data;
1186 int error = 0;
1187 if (connect_info->validate_certificate) {
1188 http_credential_t *credential = cupsArrayFirst(certs);
1189 if (credential) {
1190 LOGD("ipp_server_cert_cb: validate_certificate (len=%d)", credential->datalen);
1191 error = connect_info->validate_certificate(connect_info, credential->data, credential->datalen);
1192 }
1193 }
1194 return error;
1195 }
1196
ipp_cups_connect(const wprint_connect_info_t * connect_info,char * printer_uri,unsigned int uriLength)1197 http_t *ipp_cups_connect(const wprint_connect_info_t *connect_info, char *printer_uri,
1198 unsigned int uriLength) {
1199 const char *uri_path;
1200 http_t *curl_http = NULL;
1201
1202 cupsSetServerCertCB(ipp_server_cert_cb, (void *)connect_info);
1203
1204 if ((connect_info->uri_path == NULL) || (strlen(connect_info->uri_path) == 0)) {
1205 uri_path = DEFAULT_IPP_URI_RESOURCE;
1206 } else {
1207 uri_path = connect_info->uri_path;
1208 }
1209
1210 int ippPortNumber = ((connect_info->port_num == IPP_PORT) ? ippPort() : connect_info->port_num);
1211
1212 if (strstr(connect_info->uri_scheme,IPPS_PREFIX) != NULL) {
1213 curl_http = httpConnectEncrypt(connect_info->printer_addr, ippPortNumber, HTTP_ENCRYPTION_ALWAYS);
1214
1215 // If ALWAYS doesn't work, fall back to REQUIRED
1216 if (curl_http == NULL) {
1217 curl_http = httpConnectEncrypt(connect_info->printer_addr, ippPortNumber, HTTP_ENCRYPT_REQUIRED);
1218 }
1219 } else {
1220 curl_http = httpConnectEncrypt(connect_info->printer_addr, ippPortNumber, HTTP_ENCRYPTION_IF_REQUESTED);
1221 }
1222
1223 httpSetTimeout(curl_http, (double)connect_info->timeout / 1000, NULL, 0);
1224 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, uriLength, connect_info->uri_scheme, NULL,
1225 connect_info->printer_addr, ippPortNumber, uri_path);
1226
1227 if (curl_http == NULL) {
1228 LOGD("ipp_cups_connect failed addr=%s port=%d", connect_info->printer_addr, ippPortNumber);
1229 }
1230
1231 cupsSetServerCertCB(NULL, NULL);
1232 return curl_http;
1233 }
1234
1235 /*
1236 * Send a request using cupsSendRequest(). Loop if we get NULL or CONTINUE. Does not delete
1237 * the request.
1238 */
ippSendRequest(http_t * http,ipp_t * request,char * resource)1239 static ipp_t *ippSendRequest(http_t *http, ipp_t *request, char *resource) {
1240 ipp_t *response = NULL;
1241 http_status_t result;
1242 bool retry;
1243
1244 do {
1245 retry = false;
1246 result = cupsSendRequest(http, request, resource, ippLength(request));
1247 if (result != HTTP_ERROR) {
1248 response = cupsGetResponse(http, resource);
1249 result = httpGetStatus(http);
1250 }
1251
1252 if (result == HTTP_CONTINUE && response == NULL) {
1253 // We need to retry when this happens.
1254 LOGD("ippSendRequest: (Continue with NULL response) Retry");
1255 retry = true;
1256 } else if (result == HTTP_ERROR || result >= HTTP_BAD_REQUEST) {
1257 _cupsSetHTTPError(result);
1258 break;
1259 }
1260
1261 if (http->state != HTTP_WAITING) {
1262 httpFlush(http);
1263 }
1264 } while (retry);
1265
1266 return response;
1267 }
1268
1269 /*
1270 * Call ippDoCupsIORequest, repeating if a failure occurs based on failure conditions, and
1271 * returning the response (or NULL if it failed).
1272 *
1273 * Does not free the request, and the caller must call ippDelete to free any valid response.
1274 */
ipp_doCupsRequest(http_t * http,ipp_t * request,char * http_resource,char * printer_uri)1275 ipp_t *ipp_doCupsRequest(http_t *http, ipp_t *request, char *http_resource, char *printer_uri) {
1276 ipp_status_t ipp_status;
1277 ipp_t *response = NULL;
1278 int service_unavailable_retry_count = 0;
1279 int bad_request_retry_count = 0;
1280 int internal_error_retry_count = 0;
1281 ipp_version_state ipp_version_supported = IPP_VERSION_RESOLVED;
1282
1283 // Fail if any of these parameters are NULL
1284 if (http == NULL || request == NULL || http_resource == NULL || printer_uri == NULL) {
1285 return NULL;
1286 }
1287
1288 do {
1289 // Give up immediately if wprint is done.
1290 if (!wprintIsRunning()) return NULL;
1291
1292 // This is a no-op until we hit the error IPP_VERSION_NOT_SUPPORTED and retry.
1293 if (set_ipp_version(request, printer_uri, http, ipp_version_supported) != 0) {
1294 // We tried to find the correct IPP version by doing a series of get attribute
1295 // requests but they all failed... we give up.
1296 LOGE("ipp_doCupsRequest: set_ipp_version!=0, version not set");
1297 break;
1298 }
1299
1300 response = ippSendRequest(http, request, http_resource);
1301 if (response == NULL) {
1302 ipp_status = cupsLastError();
1303 if (ipp_status == IPP_INTERNAL_ERROR || ipp_status == HTTP_ERROR) {
1304 internal_error_retry_count++;
1305 if (internal_error_retry_count > IPP_INTERNAL_ERROR_MAX_RETRIES) {
1306 break;
1307 }
1308
1309 LOGE("ipp_doCupsRequest: %s %d received, retry %d of %d",
1310 printer_uri, ipp_status, internal_error_retry_count,
1311 IPP_INTERNAL_ERROR_MAX_RETRIES);
1312 continue;
1313 } else if (ipp_status == IPP_SERVICE_UNAVAILABLE) {
1314 service_unavailable_retry_count++;
1315 if (service_unavailable_retry_count > IPP_SERVICE_ERROR_MAX_RETRIES) {
1316 break;
1317 }
1318
1319 LOGE("ipp_doCupsRequest: %s IPP_SERVICE_UNAVAILABLE received, retrying %d of %d",
1320 printer_uri, service_unavailable_retry_count,
1321 IPP_SERVICE_ERROR_MAX_RETRIES);
1322 continue;
1323 } else if (ipp_status == IPP_BAD_REQUEST) {
1324 bad_request_retry_count++;
1325 if (bad_request_retry_count > IPP_BAD_REQUEST_MAX_RETRIES) {
1326 break;
1327 }
1328
1329 LOGD("ipp_doCupsRequest: %s IPP_BAD_REQUEST received. retry (%d) of (%d)",
1330 printer_uri, bad_request_retry_count, IPP_BAD_REQUEST_MAX_RETRIES);
1331 continue;
1332 } else if (ipp_status == IPP_NOT_FOUND) {
1333 LOGE("ipp_doCupsRequest: %s IPP_NOT_FOUND received.", printer_uri);
1334 break;
1335 }
1336 } else {
1337 ipp_status = cupsLastError();
1338 if (ipp_status == IPP_BAD_REQUEST) {
1339 bad_request_retry_count++;
1340 LOGE("ipp_doCupsRequest: %s IPP_BAD_REQUEST received. retry (%d) of (%d)",
1341 printer_uri, bad_request_retry_count, IPP_BAD_REQUEST_MAX_RETRIES);
1342 if (bad_request_retry_count > IPP_BAD_REQUEST_MAX_RETRIES) {
1343 break;
1344 }
1345
1346 ippDelete(response);
1347 response = NULL;
1348 continue;
1349 } else if (ipp_status == IPP_VERSION_NOT_SUPPORTED) {
1350 ipp_version_supported = IPP_VERSION_UNSUPPORTED;
1351 ippDelete(response);
1352 response = NULL;
1353 continue;
1354 }
1355 }
1356 break;
1357 } while (1);
1358
1359 return response;
1360 }