-
Notifications
You must be signed in to change notification settings - Fork 1
/
airscan-wsd.c
1184 lines (970 loc) · 34.6 KB
/
airscan-wsd.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
*/
#define _GNU_SOURCE
#include <string.h>
#include "airscan.h"
#include <stdlib.h>
/* Protocol constants */
/* Miscellaneous strings, used by protocol
*/
#define WSD_ADDR_ANONYMOUS \
"http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"
#define WSD_ACTION_GET_SCANNER_ELEMENTS \
"http://schemas.microsoft.com/windows/2006/08/wdp/scan/GetScannerElements"
#define WSD_ACTION_CREATE_SCAN_JOB \
"http://schemas.microsoft.com/windows/2006/08/wdp/scan/CreateScanJob"
#define WSD_ACTION_RETRIEVE_IMAGE \
"http://schemas.microsoft.com/windows/2006/08/wdp/scan/RetrieveImage"
#define WSD_ACTION_CANCEL_JOB \
"http://schemas.microsoft.com/windows/2006/08/wdp/scan/CancelJob"
/* Retry parameters
*
* If CreateScanJobRequest is failed due to temporary reason (Calibrating,
* LampWarming), request is retries several times
*
* WSD_CREATE_SCAN_JOB_RETRY_PAUSE defines pause between retries,
* in milliseconds. WSD_CREATE_SCAN_JOB_RETRY_ATTEMPTS defines
* an attempt limit
*/
#define WSD_CREATE_SCAN_JOB_RETRY_PAUSE 1000
#define WSD_CREATE_SCAN_JOB_RETRY_ATTEMPTS 30
/* XML namespace translation for XML reader
*/
static const xml_ns wsd_ns_rd[] = {
{"s", "http*://schemas.xmlsoap.org/soap/envelope"}, /* SOAP 1.1 */
{"s", "http*://www.w3.org/2003/05/soap-envelope"}, /* SOAP 1.2 */
{"d", "http*://schemas.xmlsoap.org/ws/2005/04/discovery"},
{"a", "http*://schemas.xmlsoap.org/ws/2004/08/addressing"},
{"scan", "http*://schemas.microsoft.com/windows/2006/08/wdp/scan"},
{NULL, NULL}
};
/* XML namespace definitions for XML writer
*/
static const xml_ns wsd_ns_wr[] = {
{"soap", "http://www.w3.org/2003/05/soap-envelope"}, /* SOAP 1.2 */
{"wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing"},
{"sca", "http://schemas.microsoft.com/windows/2006/08/wdp/scan"},
{NULL, NULL}
};
/* proto_handler_wsd represents WSD protocol handler
*/
typedef struct {
proto_handler proto; /* Base class */
/* Error reasons decoding */
char fault_code[64];
/* Supported formats: JPEG variants */
bool exif;
bool jfif;
/* Supported formats: TIFF variabls */
bool tiff_single_uncompressed;
bool tiff_single_g4;
bool tiff_single_g3mh;
bool tiff_single_jpeg_tn2;
/* Other formats */
bool pdf_a;
bool png;
bool dib;
} proto_handler_wsd;
/* Forward declarations */
static http_query*
wsd_status_query (const proto_ctx *ctx);
/* Free WSD protocol handler
*/
static void
wsd_free (proto_handler *proto)
{
mem_free(proto);
}
/* Create a HTTP POST request
*/
static http_query*
wsd_http_post (const proto_ctx *ctx, char *body)
{
http_query *q;
q = http_query_new(ctx->http, http_uri_clone(ctx->base_uri),
"POST", body, "application/soap+xml");
http_query_set_request_header(q, "Cache-Control", "no-cache");
http_query_set_request_header(q, "Pragma", "no-cache");
http_query_set_request_header(q, "User-Agent", "WSDAPI");
return q;
}
/* Make SOAP header for outgoing request
*/
static void
wsd_make_request_header (const proto_ctx *ctx, xml_wr *xml, const char *action)
{
uuid u = uuid_rand();
xml_wr_enter(xml, "soap:Header");
xml_wr_add_text(xml, "wsa:MessageID", u.text);
//xml_wr_add_text(xml, "wsa:To", WSD_ADDR_ANONYMOUS);
xml_wr_add_text(xml, "wsa:To", http_uri_str(ctx->base_uri_nozone));
xml_wr_enter(xml, "wsa:ReplyTo");
xml_wr_add_text(xml, "wsa:Address", WSD_ADDR_ANONYMOUS);
xml_wr_leave(xml);
xml_wr_add_text(xml, "wsa:Action", action);
xml_wr_leave(xml);
}
/* Query device capabilities
*/
static http_query*
wsd_devcaps_query (const proto_ctx *ctx)
{
xml_wr *xml = xml_wr_begin("soap:Envelope", wsd_ns_wr);
wsd_make_request_header(ctx, xml, WSD_ACTION_GET_SCANNER_ELEMENTS);
xml_wr_enter(xml, "soap:Body");
xml_wr_enter(xml, "sca:GetScannerElementsRequest");
xml_wr_enter(xml, "sca:RequestedElements");
//xml_wr_add_text(xml, "sca:Name", "sca:ScannerDescription");
xml_wr_add_text(xml, "sca:Name", "sca:ScannerConfiguration");
//xml_wr_add_text(xml, "sca:Name", "sca:DefaultScanTicket");
//xml_wr_add_text(xml, "sca:Name", "sca:ScannerStatus");
xml_wr_leave(xml);
xml_wr_leave(xml);
xml_wr_leave(xml);
return wsd_http_post(ctx, xml_wr_finish_compact(xml));
}
/* Parse supported formats
*/
static error
wsd_devcaps_parse_formats (proto_handler_wsd *wsd,
devcaps *caps, xml_rd *xml, unsigned int *formats_out)
{
error err = NULL;
unsigned int level = xml_rd_depth(xml);
size_t prefixlen = strlen(xml_rd_node_path(xml));
unsigned int formats = 0;
(void) caps;
/* Decode supported formats */
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml) + prefixlen;
if (!strcmp(path, "/scan:FormatValue")) {
const char *v = xml_rd_node_value(xml);
if (!strcmp(v, "jfif")) {
wsd->jfif = true;
} else if (!strcmp(v, "exif")) {
wsd->exif = true;
} else if (!strcmp(v, "tiff-single-uncompressed")) {
wsd->tiff_single_uncompressed = true;
} else if (!strcmp(v, "tiff-single-g4")) {
wsd->tiff_single_g4 = true;
} else if (!strcmp(v, "tiff-single-g3mh")) {
wsd->tiff_single_g3mh = true;
} else if (!strcmp(v, "tiff-single-jpeg-tn2")) {
wsd->tiff_single_jpeg_tn2 = true;
} else if (!strcmp(v, "pdf-a")) {
wsd->pdf_a = true;
} else if (!strcmp(v, "png")) {
wsd->png = true;
} else if (!strcmp(v, "dib")) {
wsd->dib = true;
}
}
xml_rd_deep_next(xml, level);
}
/* Set formats bits */
if (wsd->jfif || wsd->exif) {
formats |= 1 << ID_FORMAT_JPEG;
}
if (wsd->pdf_a) {
formats |= 1 << ID_FORMAT_PDF;
}
if (wsd->png) {
formats |= 1 << ID_FORMAT_PNG;
}
if (wsd->tiff_single_g4 || wsd->tiff_single_g3mh) {
formats |= 1 << ID_FORMAT_TIFF;
}
if ((formats & DEVCAPS_FORMATS_SUPPORTED) == 0) {
/* These used as last resort */
if (wsd->tiff_single_jpeg_tn2 || wsd->tiff_single_uncompressed) {
formats |= 1 << ID_FORMAT_TIFF;
}
if (wsd->dib) {
formats |= 1 << ID_FORMAT_BMP;
}
}
*formats_out = formats;
if (((formats) & DEVCAPS_FORMATS_SUPPORTED) == 0) {
err = ERROR("no supported image formats");
}
return err;
}
/* Parse size
*/
static error
wsd_devcaps_parse_size (SANE_Word *out, xml_rd *xml)
{
SANE_Word val;
error err = xml_rd_node_value_uint(xml, &val);
if (err == NULL && *out < 0) {
*out = val;
}
return err;
}
/* Parse resolution and append it to array of resolutions
*/
static error
wsd_devcaps_parse_res (SANE_Word **res, xml_rd *xml)
{
SANE_Word val;
error err = xml_rd_node_value_uint(xml, &val);
if (err == NULL) {
*res = sane_word_array_append(*res, val);
}
return err;
}
/* Parse source configuration
*/
static error
wsd_devcaps_parse_source (devcaps *caps, xml_rd *xml, ID_SOURCE src_id)
{
error err = NULL;
unsigned int level = xml_rd_depth(xml);
size_t prefixlen = strlen(xml_rd_node_path(xml));
devcaps_source *src = devcaps_source_new();
SANE_Word *x_res = sane_word_array_new();
SANE_Word *y_res = sane_word_array_new();
SANE_Word min_wid = -1, max_wid = -1, min_hei = -1, max_hei = -1;
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml) + prefixlen;
if (!strcmp(path, "/scan:PlatenResolutions/scan:Widths/scan:Width") ||
!strcmp(path, "/scan:ADFResolutions/scan:Widths/scan:Width")) {
err = wsd_devcaps_parse_res(&x_res, xml);
} else if (!strcmp(path, "/scan:PlatenResolutions/scan:Heights/scan:Height") ||
!strcmp(path, "/scan:ADFResolutions/scan:Heights/scan:Height")) {
err = wsd_devcaps_parse_res(&y_res, xml);
} else if (!strcmp(path, "/scan:PlatenMinimumSize/scan:Width") ||
!strcmp(path, "/scan:ADFMinimumSize/scan:Width")) {
err = wsd_devcaps_parse_size(&min_wid, xml);
} else if (!strcmp(path, "/scan:PlatenMinimumSize/scan:Height") ||
!strcmp(path, "/scan:ADFMinimumSize/scan:Height")) {
err = wsd_devcaps_parse_size(&min_hei, xml);
} else if (!strcmp(path, "/scan:PlatenMaximumSize/scan:Width") ||
!strcmp(path, "/scan:ADFMaximumSize/scan:Width")) {
err = wsd_devcaps_parse_size(&max_wid, xml);
} else if (!strcmp(path, "/scan:PlatenMaximumSize/scan:Height") ||
!strcmp(path, "/scan:ADFMaximumSize/scan:Height")) {
err = wsd_devcaps_parse_size(&max_hei, xml);
} else if (!strcmp(path, "/scan:PlatenColor/scan:ColorEntry") ||
!strcmp(path, "/scan:ADFColor/scan:ColorEntry")) {
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;
}
}
if (err != NULL) {
break;
}
xml_rd_deep_next(xml, level);
}
/* Merge x/y resolutions */
if (err == NULL) {
sane_word_array_sort(x_res);
sane_word_array_sort(y_res);
sane_word_array_free(src->resolutions);
src->resolutions = sane_word_array_intersect_sorted(x_res, y_res);
if (sane_word_array_len(src->resolutions) > 0) {
src->flags |= DEVCAPS_SOURCE_RES_DISCRETE;
} else {
err = ERROR("no resolutions defined");
}
}
/* Check things */
src->colormodes &= DEVCAPS_COLORMODES_SUPPORTED;
if (err == NULL && src->colormodes == 0) {
err = ERROR("no color modes defined");
}
if (err == NULL && min_wid < 0) {
err = ERROR("minimum width not defined");
}
if (err == NULL && min_hei < 0) {
err = ERROR("minimum height not defined");
}
if (err == NULL && max_wid < 0) {
err = ERROR("maximum width not defined");
}
if (err == NULL && max_hei < 0) {
err = ERROR("maximum height not defined");
}
if (err == NULL && min_wid > max_wid) {
err = ERROR("minimum width > maximum width");
}
if (err == NULL && min_hei > max_hei) {
err = ERROR("minimum height > maximum height");
}
/* Fix things
*
* Note. Some scanners (namely, Kyocera ECOSYS M2040dn)
* return width and height swapped. As a workaround,
* we flip if back, if width is greater that heigh
*
* FIXME: more reliable detection of need to flip
* width and height is required
*/
if (max_wid > max_hei) {
SANE_Word tmp;
tmp = max_wid;
max_wid = max_hei;
max_hei = tmp;
tmp = min_wid;
min_wid = min_hei;
min_hei = tmp;
}
/* Workaround for yet another Kyocera bug. This device doesn't
* honor scan region settings. I.e., it understands it,
* properly mirrors in DocumentFinalParameters, but completely
* ignores when generating the image.
*
* So we can't rely on device's ability to clip the image and
* must implement clipping in software. It can be enforced
* in our backend by the following two lines:
*/
min_wid = max_wid;
min_hei = max_hei;
/* Save min/max width and height */
src->min_wid_px = min_wid;
src->max_wid_px = max_wid;
src->min_hei_px = min_hei;
src->max_hei_px = max_hei;
/* Save source */
if (err == NULL) {
if (caps->src[src_id] == NULL) {
caps->src[src_id] = src;
} else {
devcaps_source_free(src);
}
}
/* Cleanup and exit */
sane_word_array_free(x_res);
sane_word_array_free(y_res);
return err;
}
/* Parse scanner configuration
*/
static error
wsd_devcaps_parse_configuration (proto_handler_wsd *wsd,
devcaps *caps, xml_rd *xml)
{
error err = NULL;
unsigned int level = xml_rd_depth(xml);
size_t prefixlen = strlen(xml_rd_node_path(xml));
bool adf = false, duplex = false;
unsigned int formats = 0;
int i;
/* Parse configuration */
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml) + prefixlen;
if (!strcmp(path, "/scan:DeviceSettings/scan:FormatsSupported")) {
err = wsd_devcaps_parse_formats(wsd, caps, xml, &formats);
} else if (!strcmp(path, "/scan:Platen")) {
err = wsd_devcaps_parse_source(caps, xml, ID_SOURCE_PLATEN);
} else if (!strcmp(path, "/scan:ADF/scan:ADFFront")) {
adf = true;
err = wsd_devcaps_parse_source(caps, xml, ID_SOURCE_ADF_SIMPLEX);
} else if (!strcmp(path, "/scan:ADF/scan:ADFBack")) {
err = wsd_devcaps_parse_source(caps, xml, ID_SOURCE_ADF_DUPLEX);
} else if (!strcmp(path, "/scan:ADF/scan:ADFSupportsDuplex")) {
const char *v = xml_rd_node_value(xml);
duplex = !strcmp(v, "1") || !strcmp(v, "true");
} else {
//log_debug(NULL, "CONF: %s", path);
}
if (err != NULL) {
return err;
}
xml_rd_deep_next(xml, level);
}
/* Adjust sources */
for (i = 0; i < NUM_ID_SOURCE; i ++) {
devcaps_source *src = caps->src[i];
if (src != NULL) {
src->formats = formats;
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, 1000);
src->win_y_range_mm.max = math_px2mm_res(src->max_hei_px, 1000);
}
}
/* Note, WSD uses slightly unusual model: instead of providing
* source configurations for simplex and duplex modes, it provides
* source configuration for ADF front, which is required (when ADF
* is supported by device) and for ADF back, which is optional
*
* So we assume, that ADF front applies to both simplex and duplex
* modes, while ADF back applies only to duplex mode
*
* So if duplex is supported, we either merge front and back
* configurations, if both are present, or simply copy front
* to back, if back is missed
*/
if (adf && duplex) {
log_assert(NULL, caps->src[ID_SOURCE_ADF_SIMPLEX] != NULL);
if (caps->src[ID_SOURCE_ADF_DUPLEX] == NULL) {
caps->src[ID_SOURCE_ADF_DUPLEX] =
devcaps_source_clone(caps->src[ID_SOURCE_ADF_SIMPLEX]);
} else {
devcaps_source *src;
src = devcaps_source_merge(caps->src[ID_SOURCE_ADF_SIMPLEX],
caps->src[ID_SOURCE_ADF_DUPLEX]);
devcaps_source_free(caps->src[ID_SOURCE_ADF_DUPLEX]);
caps->src[ID_SOURCE_ADF_DUPLEX] = src;
}
} else if (caps->src[ID_SOURCE_ADF_DUPLEX] != NULL) {
devcaps_source_free(caps->src[ID_SOURCE_ADF_DUPLEX]);
caps->src[ID_SOURCE_ADF_DUPLEX] = NULL;
}
/* Check that we have at least one source */
ID_SOURCE id_src;
bool src_ok = false;
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) {
return ERROR("neither platen nor ADF sources detected");
}
return NULL;
}
/* Parse device capabilities
*/
static error
wsd_devcaps_parse (proto_handler_wsd *wsd,
devcaps *caps, const char *xml_text, size_t xml_len)
{
error err = NULL;
xml_rd *xml;
bool found_configuration = false;
/* Parse capabilities XML */
err = xml_rd_begin(&xml, xml_text, xml_len, wsd_ns_rd);
if (err != NULL) {
goto DONE;
}
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml);
if (!strcmp(path, "s:Envelope/s:Body"
"/scan:GetScannerElementsResponse/scan:ScannerElements/"
"scan:ElementData/scan:ScannerConfiguration")) {
found_configuration = true;
err = wsd_devcaps_parse_configuration(wsd, caps, xml);
}
if (err != NULL) {
goto DONE;
}
xml_rd_deep_next(xml, 0);
}
/* Check things */
if (!found_configuration) {
err = ERROR("ScannerConfiguration missed");
}
DONE:
if (err != NULL) {
devcaps_reset(caps);
}
xml_rd_finish(&xml);
return err;
}
/* Decode device capabilities
*/
static error
wsd_devcaps_decode (const proto_ctx *ctx, devcaps *caps)
{
proto_handler_wsd *wsd = (proto_handler_wsd*) ctx->proto;
http_data *data = http_query_get_response_data(ctx->query);
error err;
caps->units = 1000;
caps->protocol = ctx->proto->name;
caps->justification_x = caps->justification_y = ID_JUSTIFICATION_UNKNOWN;
err = wsd_devcaps_parse(wsd, caps, data->bytes, data->size);
return err;
}
/* Check if response is fault response without decoding it
*/
static bool
wsd_fault_check (const proto_ctx *ctx)
{
http_data *data;
static const char fault[] =
"//schemas.xmlsoap.org/ws/2004/08/addressing/fault";
/* If we have erroneous HTTP status, we expect to see fault message
* inside
*/
if (http_query_error(ctx->query) != NULL) {
return true;
}
/* Some devices (namely Lexmark MB2236adw and Xerox WorkCentre 3225)
* may use HTTP status 200 to return fault response, so check for
* the HTTP status code is not enough to distinguish between normal
* and fault response
*
* So we search the response body for the following string:
* "//schemas.xmlsoap.org/ws/2004/08/addressing/fault"
*
* If this string is found, this is probably a fault response
*
* Note, the scheme is stripped from this string, because some
* devices use "http://", why another may use "https://"
*
* Note, as optimization and to avoid searching this string
* across the image date, we assume that if we have got MIME
* multipart response, it is probably not fault.
*/
if (http_query_get_mp_response_count(ctx->query) != 0) {
return false;
}
data = http_query_get_response_data(ctx->query);
if (memmem(data->bytes, data->size, fault, sizeof(fault) - 1) != NULL) {
return true;
}
return false;
}
/* Decode fault response
*/
static proto_result
wsd_fault_decode (const proto_ctx *ctx)
{
proto_handler_wsd *wsd = (proto_handler_wsd*) ctx->proto;
proto_result result = {0};
http_data *data = http_query_get_response_data(ctx->query);
xml_rd *xml;
/* Parse XML */
result.err = xml_rd_begin(&xml, data->bytes, data->size, wsd_ns_rd);
if (result.err != NULL) {
result.next = PROTO_OP_FINISH;
result.status = SANE_STATUS_IO_ERROR;
return result;
}
/* Decode XML, save fault code */
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml);
if (!strcmp(path, "s:Envelope/s:Body/s:Fault/s:Code/s:Subcode/s:Value")) {
const char *fault = xml_rd_node_value(xml);
const char *s;
/* Skip namespace prefix */
s = strchr(fault, ':');
if (s != NULL) {
fault = s + 1;
}
/* Save the status */
log_debug(ctx->log, "fault code: %s", fault);
strncpy(wsd->fault_code, fault, sizeof(wsd->fault_code) - 1);
}
xml_rd_deep_next(xml, 0);
}
xml_rd_finish(&xml);
result.next = PROTO_OP_CHECK;
return result;
}
/* Create pre-scan check query
*/
static http_query*
wsd_precheck_query (const proto_ctx *ctx)
{
return wsd_status_query(ctx);
}
/* Decode pre-scan check query results
*/
static proto_result
wsd_precheck_decode (const proto_ctx *ctx)
{
proto_result result = {0};
(void) ctx;
result.next = PROTO_OP_SCAN;
result.status = SANE_STATUS_GOOD;
return result;
}
/* Initiate scanning
*/
static http_query*
wsd_scan_query (const proto_ctx *ctx)
{
proto_handler_wsd *wsd = (proto_handler_wsd*) ctx->proto;
const proto_scan_params *params = &ctx->params;
xml_wr *xml = xml_wr_begin("soap:Envelope", wsd_ns_wr);
const char *source = NULL;
const char *colormode = NULL;
const char *format = NULL;
static const char *sides_simplex[] = {"sca:MediaFront", NULL};
static const char *sides_duplex[] = {"sca:MediaFront", "sca:MediaBack", NULL};
const char **sides;
int i;
/* Prepare parameters */
switch (params->src) {
case ID_SOURCE_PLATEN: source = "Platen"; break;
case ID_SOURCE_ADF_SIMPLEX: source = "ADF"; break;
case ID_SOURCE_ADF_DUPLEX: source = "ADFDuplex"; break;
default:
log_internal_error(ctx->log);
}
sides = params->src == ID_SOURCE_ADF_DUPLEX ? sides_duplex : sides_simplex;
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);
}
/* Create scan request */
wsd_make_request_header(ctx, xml, WSD_ACTION_CREATE_SCAN_JOB);
xml_wr_enter(xml, "soap:Body");
xml_wr_enter(xml, "sca:CreateScanJobRequest");
xml_wr_enter(xml, "sca:ScanTicket");
xml_wr_enter(xml, "sca:JobDescription");
xml_wr_add_text(xml, "sca:JobName", "sane-airscan request");
xml_wr_add_text(xml, "sca:JobOriginatingUserName", "sane-airscan");
/* WS-Scan specification says that this parameter is optional,
* but without this parameter the Canon TR7500 rejects scan
* request with the InvalidArgs error
*/
xml_wr_add_text(xml, "sca:JobInformation", "sane-airscan");
xml_wr_leave(xml); // sca:JobDescription
xml_wr_enter(xml, "sca:DocumentParameters");
switch (ctx->params.format) {
case ID_FORMAT_JPEG:
if (wsd->jfif) {
format = "jfif";
} else if (wsd->exif) {
format = "exif";
}
break;
case ID_FORMAT_TIFF:
if (wsd->tiff_single_g4) {
format = "tiff-single-g4";
} else if (wsd->tiff_single_g3mh) {
format = "tiff-single-g3mh";
} else if (wsd->tiff_single_jpeg_tn2) {
format = "tiff-single-jpeg-tn2";
} else if (wsd->tiff_single_uncompressed) {
format = "tiff-single-uncompressed";
}
break;
case ID_FORMAT_PNG:
if (wsd->png) {
format = "png";
}
break;
case ID_FORMAT_PDF:
if (wsd->pdf_a) {
format = "pdf-a";
}
break;
case ID_FORMAT_BMP:
if (wsd->dib) {
format = "dib";
}
break;
case ID_FORMAT_UNKNOWN:
case NUM_ID_FORMAT:
break;
}
log_assert(ctx->log, format != NULL);
xml_wr_add_text(xml, "sca:Format", format);
xml_wr_add_text(xml, "sca:ImagesToTransfer", "0");
xml_wr_enter(xml, "sca:InputSize");
xml_wr_enter(xml, "sca:InputMediaSize");
xml_wr_add_uint(xml, "sca:Width", params->wid);
xml_wr_add_uint(xml, "sca:Height", params->hei);
xml_wr_leave(xml); // sca:InputMediaSize
xml_wr_leave(xml); // sca:InputSize
xml_wr_add_text(xml, "sca:InputSource", source);
xml_wr_enter(xml, "sca:MediaSides");
for (i = 0; sides[i] != NULL; i ++) {
xml_wr_enter(xml, sides[i]);
xml_wr_add_text(xml, "sca:ColorProcessing", colormode);
xml_wr_enter(xml, "sca:Resolution");
xml_wr_add_uint(xml, "sca:Width", params->x_res);
xml_wr_add_uint(xml, "sca:Height", params->y_res);
xml_wr_leave(xml); // sca:Resolution
xml_wr_enter(xml, "sca:ScanRegion");
xml_wr_add_uint(xml, "sca:ScanRegionXOffset", params->x_off);
xml_wr_add_uint(xml, "sca:ScanRegionYOffset", params->y_off);
xml_wr_add_uint(xml, "sca:ScanRegionWidth", params->wid);
xml_wr_add_uint(xml, "sca:ScanRegionHeight", params->hei);
xml_wr_leave(xml); // sca:ScanRegion
xml_wr_leave(xml);
}
xml_wr_leave(xml); // sca:MediaSides
xml_wr_leave(xml); // sca:DocumentParameters
xml_wr_leave(xml); // sca:ScanTicket
xml_wr_leave(xml); // sca:CreateScanJobRequest
xml_wr_leave(xml); // soap:Body
//log_debug(0, "%s", xml_wr_finish_compact(xml)); exit(0);
return wsd_http_post(ctx, xml_wr_finish_compact(xml));
}
/* Decode result of scan request
*/
static proto_result
wsd_scan_decode (const proto_ctx *ctx)
{
proto_result result = {0};
error err = NULL;
xml_rd *xml = NULL;
http_data *data;
SANE_Word job_id = -1;
char *job_token = NULL;
result.next = PROTO_OP_FINISH;
/* Decode error, if any */
if (wsd_fault_check(ctx)) {
return wsd_fault_decode(ctx);
}
/* Decode CreateScanJobResponse */
data = http_query_get_response_data(ctx->query);
err = xml_rd_begin(&xml, data->bytes, data->size, wsd_ns_rd);
if (err != NULL) {
err = eloop_eprintf("XML: %s", ESTRING(err));
goto DONE;
}
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml);
if (!strcmp(path, "s:Envelope/s:Body/scan:CreateScanJobResponse"
"/scan:JobId")) {
err = xml_rd_node_value_uint(xml, &job_id);
} else if (!strcmp(path, "s:Envelope/s:Body/scan:CreateScanJobResponse"
"/scan:JobToken")) {
mem_free(job_token);
job_token = str_dup(xml_rd_node_value(xml));
}
xml_rd_deep_next(xml, 0);
}
if (job_id == -1) {
err = ERROR("missed JobId");
goto DONE;
}
if (job_token == NULL) {
err = ERROR("missed JobToken");
goto DONE;
}
result.next = PROTO_OP_LOAD;
result.data.location = str_printf("%u:%s", job_id, job_token);
/* Cleanup and exit */
DONE:
xml_rd_finish(&xml);
mem_free(job_token);
if (err != NULL) {
result.err = eloop_eprintf("CreateScanJobResponse: %s", ESTRING(err));
}
if (result.next == PROTO_OP_FINISH) {
result.status = SANE_STATUS_IO_ERROR;
}
return result;
}
/* Initiate image downloading
*/
static http_query*
wsd_load_query (const proto_ctx *ctx)
{
xml_wr *xml = xml_wr_begin("soap:Envelope", wsd_ns_wr);
char *job_id, *job_token;
/* Split location into JobId and JobToken */
job_id = alloca(strlen(ctx->location) + 1);
strcpy(job_id, ctx->location);
job_token = strchr(job_id, ':');
*job_token ++ = '\0';
/* Build RetrieveImageRequest */
wsd_make_request_header(ctx, xml, WSD_ACTION_RETRIEVE_IMAGE);
xml_wr_enter(xml, "soap:Body");
xml_wr_enter(xml, "sca:RetrieveImageRequest");
xml_wr_enter(xml, "sca:DocumentDescription");
xml_wr_add_text(xml, "sca:DocumentName", "IMAGE000.JPG");
xml_wr_leave(xml);
xml_wr_add_text(xml, "sca:JobId", job_id);
xml_wr_add_text(xml, "sca:JobToken", job_token);
xml_wr_leave(xml);
xml_wr_leave(xml);
return wsd_http_post(ctx, xml_wr_finish_compact(xml));
}
/* Decode result of image request
*/
static proto_result
wsd_load_decode (const proto_ctx *ctx)
{
proto_result result = {0};
http_data *data;
/* Check HTTP status */
if (wsd_fault_check(ctx)) {
return wsd_fault_decode(ctx);
}
/* We expect multipart message with attached image */
data = http_query_get_mp_response_data(ctx->query, 1);
if (data == NULL) {
result.next = PROTO_OP_FINISH;
result.err = ERROR("RetrieveImageRequest: invalid response");
return result;
}
if (ctx->params.src == ID_SOURCE_PLATEN) {
result.next = PROTO_OP_FINISH;
} else {
result.next = PROTO_OP_LOAD;
}
result.data.image = http_data_ref(data);
return result;
}
/* Request device status
*/
static http_query*
wsd_status_query (const proto_ctx *ctx)
{
xml_wr *xml = xml_wr_begin("soap:Envelope", wsd_ns_wr);
wsd_make_request_header(ctx, xml, WSD_ACTION_GET_SCANNER_ELEMENTS);
xml_wr_enter(xml, "soap:Body");
xml_wr_enter(xml, "sca:GetScannerElementsRequest");
xml_wr_enter(xml, "sca:RequestedElements");
xml_wr_add_text(xml, "sca:Name", "sca:ScannerStatus");
xml_wr_leave(xml);
xml_wr_leave(xml);