-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathairscan-escl.c
1275 lines (1100 loc) · 39.3 KB
/
airscan-escl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* ESCL protocol handler
*/
#include "airscan.h"
/******************** Protocol constants ********************/
/* If HTTP 503 reply is received, how many retry attempts
* to perform before giving up
*
* ESCL_RETRY_ATTEMPTS_LOAD - for NextDocument request
* ESCL_RETRY_ATTEMPTS - for other requests
*
* Note, some printers (namely, HP LaserJet MFP M28w) require
* a lot of retry attempts when loading next page at high res
*/
#define ESCL_RETRY_ATTEMPTS_LOAD 30
#define ESCL_RETRY_ATTEMPTS 10
/* And pause between retries, in milliseconds
*/
#define ESCL_RETRY_PAUSE 1000
/* Some devices (namely, Brother MFC-L2710DW) erroneously returns
* HTTP 404 Not Found when scanning from ADF, if next LOAD request
* send immediately after completion the previous one, and ScannerStatus
* returns ScannerAdfEmpty at this case, which leads to premature
* scan job termination with SANE_STATUS_NO_DOCS status
*
* Introducing a small delay between subsequent LOAD requests solves
* this problem.
*
* To avoid performance regression on a very fast scanners, this
* delay is limited to some fraction of the preceding LOAD
* query time
*
* ESCL_NEXT_LOAD_DELAY - delay between LOAD requests, milliseconds
* ESCL_NEXT_LOAD_DELAY_MAX - upper limit of this delay, as a fraction
* of a previous LOAD time
*/
#define ESCL_NEXT_LOAD_DELAY 1000
#define ESCL_NEXT_LOAD_DELAY_MAX 0.5
/* proto_handler_escl represents eSCL protocol handler
*/
typedef struct {
proto_handler proto; /* Base class */
/* Miscellaneous flags */
bool quirk_localhost; /* Set Host: localhost in ScanJobs rq */
bool quirk_canon_mf410_series; /* Canon MF410 Series */
bool quirk_port_in_host; /* Always set port in Host: header */
bool quirk_next_load_delay; /* Use ESCL_NEXT_LOAD_DELAY */
} proto_handler_escl;
/* XML namespace for XML writer
*/
static const xml_ns escl_xml_wr_ns[] = {
{"pwg", "http://www.pwg.org/schemas/2010/12/sm"},
{"scan", "http://schemas.hp.com/imaging/escl/2011/05/03"},
{NULL, NULL}
};
/******************** Miscellaneous types ********************/
/* escl_scanner_status represents decoded ScannerStatus response
*/
typedef struct {
SANE_Status device_status; /* <pwg:State>XXX</pwg:State> */
SANE_Status adf_status; /* <scan:AdfState>YYY</scan:AdfState> */
} escl_scanner_status;
/******************** Forward declarations ********************/
static error
escl_parse_scanner_status (const proto_ctx *ctx,
const char *xml_text, size_t xml_len, escl_scanner_status *out);
/******************** HTTP utility functions ********************/
/* Create HTTP query
*/
static http_query*
escl_http_query (const proto_ctx *ctx, const char *path,
const char *method, char *body)
{
proto_handler_escl *escl = (proto_handler_escl*) ctx->proto;
http_query *query = http_query_new_relative(ctx->http, ctx->base_uri, path,
method, body, "text/xml");
if (escl->quirk_port_in_host) {
http_query_force_port(query, true);
}
return query;
}
/* Create HTTP get query
*/
static http_query*
escl_http_get (const proto_ctx *ctx, const char *path)
{
return escl_http_query(ctx, path, "GET", NULL);
}
/******************** Device Capabilities ********************/
/* Parse color modes
*/
static error
escl_devcaps_source_parse_color_modes (xml_rd *xml, devcaps_source *src)
{
src->colormodes = 0;
xml_rd_enter(xml);
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
if(xml_rd_node_name_match(xml, "scan:ColorMode")) {
const char *v = xml_rd_node_value(xml);
if (!strcmp(v, "BlackAndWhite1")) {
src->colormodes |= 1 << ID_COLORMODE_BW1;
} else if (!strcmp(v, "Grayscale8")) {
src->colormodes |= 1 << ID_COLORMODE_GRAYSCALE;
} else if (!strcmp(v, "RGB24")) {
src->colormodes |= 1 << ID_COLORMODE_COLOR;
}
}
}
xml_rd_leave(xml);
return NULL;
}
/* Parse document formats
*/
static error
escl_devcaps_source_parse_document_formats (xml_rd *xml, devcaps_source *src)
{
xml_rd_enter(xml);
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
unsigned int flags = 0;
if(xml_rd_node_name_match(xml, "pwg:DocumentFormat")) {
flags |= DEVCAPS_SOURCE_PWG_DOCFMT;
}
if(xml_rd_node_name_match(xml, "scan:DocumentFormatExt")) {
flags |= DEVCAPS_SOURCE_SCAN_DOCFMT_EXT;
}
if (flags != 0) {
const char *v = xml_rd_node_value(xml);
ID_FORMAT fmt = id_format_by_mime_name(v);
if (fmt != ID_FORMAT_UNKNOWN) {
src->formats |= 1 << fmt;
src->flags |= flags;
}
}
}
xml_rd_leave(xml);
return NULL;
}
/* Parse discrete resolutions.
*/
static error
escl_devcaps_source_parse_discrete_resolutions (xml_rd *xml,
devcaps_source *src)
{
error err = NULL;
sane_word_array_reset(&src->resolutions);
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "scan:DiscreteResolution")) {
SANE_Word x = 0, y = 0;
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "scan:XResolution")) {
err = xml_rd_node_value_uint(xml, &x);
} else if (xml_rd_node_name_match(xml,
"scan:YResolution")) {
err = xml_rd_node_value_uint(xml, &y);
}
}
xml_rd_leave(xml);
if (x && y && x == y) {
src->resolutions = sane_word_array_append(src->resolutions, x);
}
}
}
xml_rd_leave(xml);
if (sane_word_array_len(src->resolutions) > 0) {
src->flags |= DEVCAPS_SOURCE_RES_DISCRETE;
sane_word_array_sort(src->resolutions);
}
return err;
}
/* Parse resolutions range
*/
static error
escl_devcaps_source_parse_resolutions_range (xml_rd *xml, devcaps_source *src)
{
error err = NULL;
SANE_Range range_x = {0, 0, 0}, range_y = {0, 0, 0};
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
SANE_Range *range = NULL;
if (xml_rd_node_name_match(xml, "scan:XResolution")) {
range = &range_x;
} else if (xml_rd_node_name_match(xml, "scan:XResolution")) {
range = &range_y;
}
if (range != NULL) {
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "scan:Min")) {
err = xml_rd_node_value_uint(xml, &range->min);
} else if (xml_rd_node_name_match(xml, "scan:Max")) {
err = xml_rd_node_value_uint(xml, &range->max);
} else if (xml_rd_node_name_match(xml, "scan:Step")) {
err = xml_rd_node_value_uint(xml, &range->quant);
}
}
xml_rd_leave(xml);
}
}
xml_rd_leave(xml);
if (range_x.min > range_x.max) {
err = ERROR("Invalid scan:XResolution range");
goto DONE;
}
if (range_y.min > range_y.max) {
err = ERROR("Invalid scan:YResolution range");
goto DONE;
}
/* If no quantization value, SANE uses 0, not 1
*/
if (range_x.quant == 1) {
range_x.quant = 0;
}
if (range_y.quant == 1) {
range_y.quant = 0;
}
/* Try to merge x/y ranges */
if (!math_range_merge(&src->res_range, &range_x, &range_y)) {
err = ERROR("Incompatible scan:XResolution and "
"scan:YResolution ranges");
goto DONE;
}
src->flags |= DEVCAPS_SOURCE_RES_RANGE;
DONE:
return err;
}
/* Parse supported resolutions.
*/
static error
escl_devcaps_source_parse_resolutions (xml_rd *xml, devcaps_source *src)
{
error err = NULL;
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "scan:DiscreteResolutions")) {
err = escl_devcaps_source_parse_discrete_resolutions(xml, src);
} else if (xml_rd_node_name_match(xml, "scan:ResolutionRange")) {
err = escl_devcaps_source_parse_resolutions_range(xml, src);
}
}
xml_rd_leave(xml);
/* Prefer discrete resolution, if both are provided */
if (src->flags & DEVCAPS_SOURCE_RES_DISCRETE) {
src->flags &= ~DEVCAPS_SOURCE_RES_RANGE;
}
return err;
}
/* Parse setting profiles (color modes, document formats etc).
*/
static error
escl_devcaps_source_parse_setting_profiles (xml_rd *xml, devcaps_source *src)
{
error err = NULL;
/* Parse setting profiles */
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "scan:SettingProfile")) {
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "scan:ColorModes")) {
err = escl_devcaps_source_parse_color_modes(xml, src);
} else if (xml_rd_node_name_match(xml,
"scan:DocumentFormats")) {
err = escl_devcaps_source_parse_document_formats(xml, src);
} else if (xml_rd_node_name_match(xml,
"scan:SupportedResolutions")) {
err = escl_devcaps_source_parse_resolutions(xml, src);
}
}
xml_rd_leave(xml);
}
}
xml_rd_leave(xml);
/* Validate results */
if (err == NULL) {
src->colormodes &= DEVCAPS_COLORMODES_SUPPORTED;
if (src->colormodes == 0) {
return ERROR("no color modes detected");
}
src->formats &= DEVCAPS_FORMATS_SUPPORTED;
if (src->formats == 0) {
return ERROR("no image formats detected");
}
if (!(src->flags & (DEVCAPS_SOURCE_RES_DISCRETE|
DEVCAPS_SOURCE_RES_RANGE))){
return ERROR("scan resolutions are not defined");
}
}
return err;
}
/* Parse supported intents (photo/document etc).
*/
static error
escl_devcaps_source_parse_supported_intents (xml_rd *xml, devcaps_source *src)
{
error err = NULL;
xml_rd_enter(xml);
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
if(xml_rd_node_name_match(xml, "scan:SupportedIntent")) {
const char *v = xml_rd_node_value(xml);
if (!strcmp(v, "Document")) {
src->scanintents |= 1 << ID_SCANINTENT_DOCUMENT;
} else if (!strcmp(v, "TextAndGraphic")) {
src->scanintents |= 1 << ID_SCANINTENT_TEXTANDGRAPHIC;
} else if (!strcmp(v, "Photo")) {
src->scanintents |= 1 << ID_SCANINTENT_PHOTO;
} else if (!strcmp(v, "Preview")) {
src->scanintents |= 1 << ID_SCANINTENT_PREVIEW;
} else if (!strcmp(v, "Object")) {
src->scanintents |= 1 << ID_SCANINTENT_OBJECT;
} else if (!strcmp(v, "BusinessCard")) {
src->scanintents |= 1 << ID_SCANINTENT_BUSINESSCARD;
} else {
log_debug(NULL, "unknown intent: %s", v);
}
}
}
xml_rd_leave(xml);
return err;
}
/* Parse ADF justification
*/
static void
escl_devcaps_parse_justification (xml_rd *xml,
ID_JUSTIFICATION *x, ID_JUSTIFICATION *y)
{
xml_rd_enter(xml);
*x = *y = ID_JUSTIFICATION_UNKNOWN;
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
if(xml_rd_node_name_match(xml, "pwg:XImagePosition")){
const char *v = xml_rd_node_value(xml);
if (!strcmp(v, "Right")){
*x = ID_JUSTIFICATION_RIGHT;
} else if (!strcmp(v, "Center")) {
*x = ID_JUSTIFICATION_CENTER;
} else if (!strcmp(v, "Left")) {
*x = ID_JUSTIFICATION_LEFT;
}
} else if(xml_rd_node_name_match(xml, "pwg:YImagePosition")){
const char *v = xml_rd_node_value(xml);
if (!strcmp(v, "Top")){
*y = ID_JUSTIFICATION_TOP;
} else if (!strcmp(v, "Center")) {
*y = ID_JUSTIFICATION_CENTER;
} else if (!strcmp(v, "Bottom")) {
*y = ID_JUSTIFICATION_BOTTOM;
}
}
}
xml_rd_leave(xml);
}
/* Parse source capabilities. Returns NULL on success, error string otherwise
*/
static error
escl_devcaps_source_parse (xml_rd *xml, devcaps_source **out)
{
devcaps_source *src = devcaps_source_new();
error err = NULL;
xml_rd_enter(xml);
for (; err == NULL && !xml_rd_end(xml); xml_rd_next(xml)) {
if(xml_rd_node_name_match(xml, "scan:MinWidth")) {
err = xml_rd_node_value_uint(xml, &src->min_wid_px);
} else if (xml_rd_node_name_match(xml, "scan:MaxWidth")) {
err = xml_rd_node_value_uint(xml, &src->max_wid_px);
} else if (xml_rd_node_name_match(xml, "scan:MinHeight")) {
err = xml_rd_node_value_uint(xml, &src->min_hei_px);
} else if (xml_rd_node_name_match(xml, "scan:MaxHeight")) {
err = xml_rd_node_value_uint(xml, &src->max_hei_px);
} else if (xml_rd_node_name_match(xml, "scan:SettingProfiles")) {
err = escl_devcaps_source_parse_setting_profiles(xml, src);
} else if (xml_rd_node_name_match(xml, "scan:SupportedIntents")) {
err = escl_devcaps_source_parse_supported_intents(xml, src);
}
}
xml_rd_leave(xml);
if (err != NULL) {
goto DONE;
}
if (src->max_wid_px != 0 && src->max_hei_px != 0)
{
/* Validate window size */
if (src->min_wid_px > src->max_wid_px)
{
err = ERROR("Invalid scan:MinWidth or scan:MaxWidth");
goto DONE;
}
if (src->min_hei_px > src->max_hei_px)
{
err = ERROR("Invalid scan:MinHeight or scan:MaxHeight");
goto DONE;
}
src->flags |= DEVCAPS_SOURCE_HAS_SIZE;
/* Set window ranges */
src->win_x_range_mm.min = src->win_y_range_mm.min = 0;
src->win_x_range_mm.max = math_px2mm_res(src->max_wid_px, 300);
src->win_y_range_mm.max = math_px2mm_res(src->max_hei_px, 300);
}
DONE:
if (err != NULL) {
devcaps_source_free(src);
} else {
if (*out == NULL) {
*out = src;
} else {
/* Duplicate detected. Ignored for now */
devcaps_source_free(src);
}
}
return err;
}
/* Parse compression factor parameters
*/
static error
escl_devcaps_compression_parse (xml_rd *xml, devcaps *caps)
{
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
error err = NULL;
if (xml_rd_node_name_match(xml, "scan:Min")) {
err = xml_rd_node_value_uint(xml, &caps->compression_range.min);
} else if (xml_rd_node_name_match(xml, "scan:Max")) {
err = xml_rd_node_value_uint(xml, &caps->compression_range.max);
} else if (xml_rd_node_name_match(xml, "scan:Step")) {
err = xml_rd_node_value_uint(xml, &caps->compression_range.quant);
} else if (xml_rd_node_name_match(xml, "scan:Normal")) {
err = xml_rd_node_value_uint(xml, &caps->compression_norm);
}
if (err != NULL) {
return err;
}
}
/* Validate obtained parameters.
*
* Note, errors are silently ignored starting from this point
*/
if (caps->compression_range.min > caps->compression_range.max) {
return NULL;
}
if (caps->compression_norm < caps->compression_range.min ||
caps->compression_norm > caps->compression_range.max) {
return NULL;
}
caps->compression_ok = true;
return NULL;
}
/* Parse device capabilities. devcaps structure must be initialized
* before calling this function.
*/
static error
escl_devcaps_parse (proto_handler_escl *escl,
devcaps *caps, const char *xml_text, size_t xml_len)
{
error err = NULL;
xml_rd *xml;
bool quirk_canon_iR2625_2630 = false;
ID_SOURCE id_src;
bool src_ok = false;
/* Fill "constant" part of device capabilities */
caps->units = 300;
caps->protocol = escl->proto.name;
caps->justification_x = caps->justification_y = ID_JUSTIFICATION_UNKNOWN;
/* Parse capabilities XML */
err = xml_rd_begin(&xml, xml_text, xml_len, NULL);
if (err != NULL) {
goto DONE;
}
if (!xml_rd_node_name_match(xml, "scan:ScannerCapabilities")) {
err = ERROR("XML: missed scan:ScannerCapabilities");
goto DONE;
}
xml_rd_enter(xml);
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "pwg:MakeAndModel")) {
const char *m = xml_rd_node_value(xml);
if (!strcmp(m, "Canon iR2625/2630")) {
quirk_canon_iR2625_2630 = true;
} else if (!strcmp(m, "HP LaserJet MFP M630")) {
escl->quirk_localhost = true;
} else if (!strcmp(m, "HP Color LaserJet FlowMFP M578")) {
escl->quirk_localhost = true;
} else if (!strcmp(m, "MF410 Series")) {
escl->quirk_canon_mf410_series = true;
} else if (!strncasecmp(m, "EPSON ", 6)) {
escl->quirk_port_in_host = true;
} else if (!strncasecmp(m, "Brother ", 8)) {
escl->quirk_next_load_delay = true;
}
} else if (xml_rd_node_name_match(xml, "scan:Manufacturer")) {
const char *m = xml_rd_node_value(xml);
if (!strcasecmp(m, "EPSON")) {
escl->quirk_port_in_host = true;
}
} else if (xml_rd_node_name_match(xml, "scan:Platen")) {
xml_rd_enter(xml);
if (xml_rd_node_name_match(xml, "scan:PlatenInputCaps")) {
err = escl_devcaps_source_parse(xml,
&caps->src[ID_SOURCE_PLATEN]);
}
xml_rd_leave(xml);
} else if (xml_rd_node_name_match(xml, "scan:Adf")) {
xml_rd_enter(xml);
while (!xml_rd_end(xml)) {
if (xml_rd_node_name_match(xml, "scan:AdfSimplexInputCaps")) {
err = escl_devcaps_source_parse(xml,
&caps->src[ID_SOURCE_ADF_SIMPLEX]);
} else if (xml_rd_node_name_match(xml,
"scan:AdfDuplexInputCaps")) {
err = escl_devcaps_source_parse(xml,
&caps->src[ID_SOURCE_ADF_DUPLEX]);
}
else if (xml_rd_node_name_match(xml, "scan:Justification")) {
escl_devcaps_parse_justification(xml,
&caps->justification_x, &caps->justification_y);
}
xml_rd_next(xml);
}
xml_rd_leave(xml);
} else if (xml_rd_node_name_match(xml, "scan:CompressionFactorSupport")) {
xml_rd_enter(xml);
err = escl_devcaps_compression_parse(xml, caps);
xml_rd_leave(xml);
}
if (err != NULL) {
goto DONE;
}
}
/* Check that we have at least one source */
for (id_src = (ID_SOURCE) 0; id_src < NUM_ID_SOURCE; id_src ++) {
if (caps->src[id_src] != NULL) {
src_ok = true;
}
}
if (!src_ok) {
err = ERROR("XML: neither platen nor ADF sources detected");
goto DONE;
}
/* Apply quirks, if any */
if (quirk_canon_iR2625_2630) {
/* This device announces resolutions up to 600 DPI,
* but actually doesn't support more that 300
*
* https://oip.manual.canon/USRMA-4209-zz-CSL-2600-enUV/contents/devu-apdx-sys_spec-send.html?search=600
*
* See #57 for details
*/
for (id_src = (ID_SOURCE) 0; id_src < NUM_ID_SOURCE; id_src ++) {
devcaps_source *src = caps->src[id_src];
if (src != NULL &&
/* paranoia: array won't be empty after quirk applied */
sane_word_array_len(src->resolutions) > 0 &&
src->resolutions[1] <= 300) {
sane_word_array_bound(src->resolutions, 0, 300);
}
}
}
DONE:
if (err != NULL) {
devcaps_reset(caps);
}
xml_rd_finish(&xml);
return err;
}
/* Query device capabilities
*/
static http_query*
escl_devcaps_query (const proto_ctx *ctx)
{
return escl_http_get(ctx, "ScannerCapabilities");
}
/* Decode device capabilities
*/
static error
escl_devcaps_decode (const proto_ctx *ctx, devcaps *caps)
{
proto_handler_escl *escl = (proto_handler_escl*) ctx->proto;
http_data *data = http_query_get_response_data(ctx->query);
const char *s;
/* Most of devices that have Server: HP_Compact_Server
* in their HTTP response header, require this quirk
* (see #116)
*/
s = http_query_get_response_header(ctx->query, "server");
if (s != NULL && !strcmp(s, "HP_Compact_Server")) {
escl->quirk_localhost = true;
}
return escl_devcaps_parse(escl, caps, data->bytes, data->size);
}
/* Create pre-scan check query
*/
static http_query*
escl_precheck_query (const proto_ctx *ctx)
{
return escl_http_get(ctx, "ScannerStatus");
}
/* Decode pre-scan check query results
*/
static proto_result
escl_precheck_decode (const proto_ctx *ctx)
{
proto_handler_escl *escl = (proto_handler_escl*) ctx->proto;
proto_result result = {0};
error err = NULL;
escl_scanner_status sts;
bool adf = ctx->params.src == ID_SOURCE_ADF_SIMPLEX ||
ctx->params.src == ID_SOURCE_ADF_DUPLEX;
/* Initialize result to something optimistic :-) */
result.status = SANE_STATUS_GOOD;
result.next = PROTO_OP_SCAN;
/* Decode status */
err = http_query_error(ctx->query);
if (err == NULL) {
http_data *data = http_query_get_response_data(ctx->query);
err = escl_parse_scanner_status(ctx, data->bytes, data->size, &sts);
}
if (err != NULL) {
result.err = err;
result.status = SANE_STATUS_IO_ERROR;
result.next = PROTO_OP_FINISH;
return result;
}
/* Note, the pre-check status is not always reliable, so normally
* we ignore it. However, with Canon MF410 Series attempt to
* scan from empty ADF causes ADF jam error (really, physical!),
* so we must take care
*/
if (escl->quirk_canon_mf410_series) {
if (adf) {
switch (sts.adf_status) {
case SANE_STATUS_JAMMED:
case SANE_STATUS_NO_DOCS:
result.status = sts.adf_status;
result.next = PROTO_OP_FINISH;
default:
break;
}
}
}
return result;
}
/* Fix Location: URL
*
* It replaces host part of uri with the host part from `orig_uri` and
* used for two purposes:
* 1) To fix URL in the Location: header. We don't trust the device
* and only allow it to specify path, not host (host is preserved
* from the `orig_uri` -- the URL used to initiate scanning
* 2) As redirection callback, together with the quirk_localhost.
* At this case, Host: is purposely invalid, which makes
* redirection unreliable (most likely, redirection will
* use "localhost" from the Host: header as the host part
* of URL).
*
* Can be directly used as http_query_onredir() callback
*/
static void
escl_scan_fix_location (void *p, http_uri *uri, const http_uri *orig_uri)
{
(void) p;
http_uri_fix_host(uri, orig_uri, "localhost");
}
/* Initiate scanning
*/
static http_query*
escl_scan_query (const proto_ctx *ctx)
{
proto_handler_escl *escl = (proto_handler_escl*) ctx->proto;
const proto_scan_params *params = &ctx->params;
const char *source = NULL;
const char *colormode = NULL;
const char *scanintent = NULL;
const char *mime = id_format_mime_name(ctx->params.format);
const devcaps_source *src = ctx->devcaps->src[params->src];
bool duplex = false;
http_query *query;
/* Prepare parameters */
switch (params->src) {
case ID_SOURCE_PLATEN: source = "Platen"; duplex = false; break;
case ID_SOURCE_ADF_SIMPLEX: source = "Feeder"; duplex = false; break;
case ID_SOURCE_ADF_DUPLEX: source = "Feeder"; duplex = true; break;
default:
log_internal_error(ctx->log);
}
switch (params->colormode) {
case ID_COLORMODE_COLOR: colormode = "RGB24"; break;
case ID_COLORMODE_GRAYSCALE: colormode = "Grayscale8"; break;
case ID_COLORMODE_BW1: colormode = "BlackAndWhite1"; break;
default:
log_internal_error(ctx->log);
}
switch (params->scanintent) {
case ID_SCANINTENT_UNSET: break;
case ID_SCANINTENT_DOCUMENT: scanintent = "Document"; break;
case ID_SCANINTENT_TEXTANDGRAPHIC: scanintent = "TextAndGraphic"; break;
case ID_SCANINTENT_PHOTO: scanintent = "Photo"; break;
case ID_SCANINTENT_PREVIEW: scanintent = "Preview"; break;
case ID_SCANINTENT_OBJECT: scanintent = "Object"; break;
case ID_SCANINTENT_BUSINESSCARD: scanintent = "BusinessCard"; break;
default:
log_internal_error(ctx->log);
}
/* Build scan request */
xml_wr *xml = xml_wr_begin("scan:ScanSettings", escl_xml_wr_ns);
xml_wr_add_text(xml, "pwg:Version", "2.0");
if (scanintent) {
xml_wr_add_text(xml, "scan:Intent", scanintent);
}
xml_wr_enter(xml, "pwg:ScanRegions");
xml_wr_enter(xml, "pwg:ScanRegion");
xml_wr_add_text(xml, "pwg:ContentRegionUnits",
"escl:ThreeHundredthsOfInches");
xml_wr_add_uint(xml, "pwg:XOffset", params->x_off);
xml_wr_add_uint(xml, "pwg:YOffset", params->y_off);
xml_wr_add_uint(xml, "pwg:Width", params->wid);
xml_wr_add_uint(xml, "pwg:Height", params->hei);
xml_wr_leave(xml); /* pwg:ScanRegion */
xml_wr_leave(xml); /* pwg:ScanRegions */
//xml_wr_add_text(xml, "scan:InputSource", source);
xml_wr_add_text(xml, "pwg:InputSource", source);
if (ctx->devcaps->compression_ok) {
xml_wr_add_uint(xml, "scan:CompressionFactor",
ctx->devcaps->compression_norm);
}
xml_wr_add_text(xml, "scan:ColorMode", colormode);
xml_wr_add_text(xml, "pwg:DocumentFormat", mime);
if ((src->flags & DEVCAPS_SOURCE_SCAN_DOCFMT_EXT) != 0) {
xml_wr_add_text(xml, "scan:DocumentFormatExt", mime);
}
xml_wr_add_uint(xml, "scan:XResolution", params->x_res);
xml_wr_add_uint(xml, "scan:YResolution", params->y_res);
if (params->src != ID_SOURCE_PLATEN) {
xml_wr_add_bool(xml, "scan:Duplex", duplex);
}
/* Send request to device */
query = escl_http_query(ctx, "ScanJobs", "POST",
xml_wr_finish_compact(xml));
/* Kyocera ECOSYS M6526cdn drops TLS connection after sending
* response HTTP headers, but before the body transfer is completed.
*
* As for this request we are only interested in the response
* headers, we can ignore this kind of error
*
* See here for details:
* https://github.com/alexpevzner/sane-airscan/issues/163
*/
http_query_no_need_response_body(query);
/* It's a dirty hack
*
* HP LaserJet MFP M630, HP Color LaserJet FlowMFP M578 and
* probably some other HP devices don't allow eSCL scan, unless
* Host is set to "localhost". It is probably bad and naive attempt
* to enforce some access security.
*
* So here we forcibly set Host to "localhost".
*
* Note, this hack doesn't work with some other printers
* see #92, #98 for details
*/
if (escl->quirk_localhost && !http_uri_is_loopback(ctx->base_uri)) {
http_query_set_request_header(query, "Host", "localhost");
http_query_onredir(query, escl_scan_fix_location);
}
return query;
}
/* Decode result of scan request
*/
static proto_result
escl_scan_decode (const proto_ctx *ctx)
{
proto_result result = {0};
error err = NULL;
const char *location;
http_uri *uri;
/* Check HTTP status */
if (http_query_status(ctx->query) != HTTP_STATUS_CREATED) {
err = eloop_eprintf("ScanJobs request: unexpected HTTP status %d",
http_query_status(ctx->query));
result.next = PROTO_OP_CHECK;
result.err = err;
return result;
}
/* Obtain location */
location = http_query_get_response_header(ctx->query, "Location");
if (location == NULL || *location == '\0') {
err = eloop_eprintf("ScanJobs request: empty location received");
goto ERROR;
}
/* Validate and save location */
uri = http_uri_new_relative(ctx->base_uri, location, true, false);
if (uri == NULL) {
err = eloop_eprintf("ScanJobs request: invalid location received");
goto ERROR;
}
/* Don't trust hostname in Location, replace it with hostname
* from the ctx->query (which represents scan request)
*
* The initial idea behind this approach is that scanner may
* not have a strong knowledge of its own host name so hostname
* supplied by device may be inaccurate.
*
* At least one device, HP Deskjet 3520 series, has demonstrated
* this behavior when scanning via IPP over USB. Instead of (expected)
* localhost host name, it returns CN26F178X605SZ.03f011b0.hpLedmUSB
*/
http_uri_fix_host(uri, http_query_uri(ctx->query), NULL);
result.data.location = str_dup(http_uri_str(uri));
http_uri_free(uri);
result.next = PROTO_OP_LOAD;
return result;
ERROR:
result.next = PROTO_OP_FINISH;
result.status = SANE_STATUS_IO_ERROR;
result.err = err;
return result;
}
/* Initiate image downloading
*/
static http_query*
escl_load_query (const proto_ctx *ctx)
{
char *url, *sep;
http_query *q;
sep = str_has_suffix(ctx->location, "/") ? "" : "/";
url = str_concat(ctx->location, sep, "NextDocument", NULL);
q = escl_http_get(ctx, url);
mem_free(url);
return q;
}
/* Decode result of image request
*/
static proto_result
escl_load_decode (const proto_ctx *ctx)
{
proto_handler_escl *escl = (proto_handler_escl*) ctx->proto;
proto_result result = {0};
error err = NULL;
timestamp t = 0;
/* Check HTTP status */
err = http_query_error(ctx->query);
if (err != NULL) {
if (ctx->params.src == ID_SOURCE_PLATEN && ctx->images_received > 0) {
result.next = PROTO_OP_CLEANUP;
} else {
result.next = PROTO_OP_CHECK;
result.err = eloop_eprintf("HTTP: %s", ESTRING(err));
}
return result;
}
/* Compute delay until next load */
if (escl->quirk_next_load_delay && ctx->params.src != ID_SOURCE_PLATEN) {
t = timestamp_now() - http_query_timestamp(ctx->query);
t *= ESCL_NEXT_LOAD_DELAY_MAX;
if (t > ESCL_NEXT_LOAD_DELAY) {
t = ESCL_NEXT_LOAD_DELAY;
}
}
/* Fill proto_result */
result.next = PROTO_OP_LOAD;
result.delay = (int) t;
result.data.image = http_data_ref(http_query_get_response_data(ctx->query));
return result;
}