forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairscan-device.c
1774 lines (1505 loc) · 48.6 KB
/
airscan-device.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
*
* Device management
*/
#include "airscan.h"
#include <stdlib.h>
#include <string.h>
/******************** Constants *********************/
/* Max time to wait until device table is ready, in seconds
*/
#define DEVICE_TABLE_READY_TIMEOUT 5
/* If HTTP 503 reply is received, how many retry attempts
* to perform before giving up
*/
#define DEVICE_HTTP_RETRY_ATTEMPTS 10
/* And pause between retries, in seconds
*/
#define DEVICE_HTTP_RETRY_PAUSE 1
/******************** Device management ********************/
/* Device flags
*/
enum {
DEVICE_LISTED = (1 << 0), /* Device listed in device_table */
DEVICE_READY = (1 << 1), /* Device is ready */
DEVICE_HALTED = (1 << 2), /* Device is halted */
DEVICE_INIT_WAIT = (1 << 3), /* Device was found during initial
scan and not ready yet */
DEVICE_OPENED = (1 << 4), /* Device currently opened */
DEVICE_SCANNING = (1 << 5), /* We are between sane_start() and
final sane_read() */
DEVICE_CLOSING = (1 << 6), /* Close in progress */
DEVICE_ALL_FLAGS = 0xffffffff
};
/* Device states
*/
typedef enum {
DEVICE_SCAN_IDLE,
DEVICE_SCAN_STARTED,
DEVICE_SCAN_REQUESTING,
DEVICE_SCAN_LOADING,
DEVICE_SCAN_LOAD_RETRY,
DEVICE_SCAN_CHECK_STATUS,
DEVICE_SCAN_CLEANING_UP,
DEVICE_SCAN_DONE
} DEVICE_STATE;
/* Device descriptor
*/
struct device {
/* Common part */
volatile gint refcnt; /* Reference counter */
const char *name; /* Device name */
unsigned int flags; /* Device flags */
devopt opt; /* Device options */
DEVICE_STATE state; /* Device state */
DEVICE_STATE checking_state; /* State before CHECK_STATUS */
int checking_http_status; /* HTTP status before CHECK_STATUS */
GCond state_cond; /* Signaled when state changes */
/* I/O handling (AVAHI and HTTP) */
zeroconf_addrinfo *addresses; /* Device addresses, NULL if
device was statically added */
zeroconf_addrinfo *addr_current; /* Current address to probe */
http_uri *uri_escl; /* eSCL base URI */
http_client *http_client; /* HTTP client */
eloop_timer *http_timer; /* HTTP retry timer */
int http_retry; /* HTTP retry count */
trace *trace; /* Protocol trace */
/* Scanning state machinery */
SANE_Status job_status; /* Job completion status */
GString *job_location; /* Scanned page location */
bool job_has_location; /* Location is valid */
eloop_event *job_cancel_event; /* Cancel event */
bool job_cancel_rq; /* Cancel requested */
GPtrArray *job_images; /* Array of SoupBuffer* */
unsigned int job_images_received; /* How many images received */
SANE_Word job_skip_x; /* How much pixels to skip, */
SANE_Word job_skip_y; /* from left and top */
/* Read machinery */
SANE_Bool read_non_blocking; /* Non-blocking I/O mode */
image_decoder *read_decoder_jpeg; /* JPEG decoder */
pollable *read_pollable; /* Signalled when read won't
block */
http_data *read_image; /* Current image */
SANE_Byte *read_line_buf; /* Single-line buffer */
SANE_Int read_line_num; /* Current image line 0-based */
SANE_Int read_line_end; /* If read_line_num>read_line_end
no more lines left in image */
SANE_Int read_line_off; /* Current offset in the line */
SANE_Int read_skip_lines; /* How many lines to skip */
SANE_Int read_skip_bytes; /* How many bytes to skip at line
beginning */
};
/* Static variables
*/
static GPtrArray *device_table;
static GCond device_table_cond;
/* Forward declarations
*/
static device*
device_find (const char *name);
static void
device_http_cancel (device *dev);
static void
device_http_onerror (device *dev, error err);
static void
device_scanner_capabilities_callback (device *dev, http_query *q);
static void
device_probe_address (device *dev, zeroconf_addrinfo *addrinfo);
static void
device_job_set_status (device *dev, SANE_Status status);
static void
device_job_abort (device *dev, SANE_Status status);
static void
device_escl_cleanup (device *dev);
static void
device_escl_load_retry (device *dev);
static void
device_escl_load_page (device *dev);
static void
device_read_queue_purge (device *dev);
static bool
device_read_push (device *dev);
static void
device_management_start_stop (bool start);
/******************** Device table management ********************/
/* Add device to the table
*/
static void
device_add (const char *name, zeroconf_addrinfo *addresses,
bool init_scan, bool statically)
{
device *dev;
/* Issue log message */
log_debug(NULL, "%s adding: \"%s\"",
statically ? "statically" : "dynamically", name);
/* Don't allow duplicate devices */
dev = device_find(name);
if (dev != NULL) {
log_debug(dev, "device already exist");
return;
}
/* Create device */
dev = g_new0(device, 1);
dev->refcnt = 1;
dev->name = g_strdup(name);
dev->flags = DEVICE_LISTED | DEVICE_INIT_WAIT;
if (init_scan) {
dev->flags |= DEVICE_INIT_WAIT;
}
devopt_init(&dev->opt);
dev->http_client = http_client_new(dev);
dev->trace = trace_open(name);
dev->job_location = g_string_new(NULL);
g_cond_init(&dev->state_cond);
dev->job_images = g_ptr_array_new();
dev->read_decoder_jpeg = image_decoder_jpeg_new();
dev->read_pollable = pollable_new();
log_debug(dev, "device created");
/* Add to the table */
g_ptr_array_add(device_table, dev);
/* Initialize device I/O */
dev->addresses = zeroconf_addrinfo_list_copy(addresses);
device_probe_address(dev, dev->addresses);
return;
}
/* Ref the device
*/
static inline device*
device_ref (device *dev)
{
g_atomic_int_inc(&dev->refcnt);
return dev;
}
/* Unref the device
*/
static inline void
device_unref (device *dev)
{
if (g_atomic_int_dec_and_test(&dev->refcnt)) {
log_debug(dev, "device destroyed");
log_assert(dev, (dev->flags & DEVICE_LISTED) == 0);
log_assert(dev, (dev->flags & DEVICE_HALTED) != 0);
log_assert(dev, (dev->flags & DEVICE_OPENED) == 0);
/* Release all memory */
g_free((void*) dev->name);
devopt_cleanup(&dev->opt);
zeroconf_addrinfo_list_free(dev->addresses);
http_uri_free(dev->uri_escl);
http_client_free(dev->http_client);
g_string_free(dev->job_location, TRUE);
g_cond_clear(&dev->state_cond);
device_read_queue_purge(dev);
g_ptr_array_free(dev->job_images, TRUE);
image_decoder_free(dev->read_decoder_jpeg);
pollable_free(dev->read_pollable);
g_free(dev);
}
}
/* Del device from the table. It implicitly halts all
* pending I/O activity
*
* Note, reference to the device may still exist (device
* may be opened), so memory can be freed later, when
* device is not used anymore
*/
static void
device_del (device *dev)
{
/* Remove device from table */
log_debug(dev, "removed from device table");
log_assert(dev, (dev->flags & DEVICE_LISTED) != 0);
dev->flags &= ~DEVICE_LISTED;
g_ptr_array_remove(device_table, dev);
/* Stop all pending I/O activity */
device_http_cancel(dev);
trace_close(dev->trace);
dev->trace = NULL;
dev->flags |= DEVICE_HALTED;
dev->flags &= ~DEVICE_READY;
/* Unref the device */
device_unref(dev);
}
/* Find device in a table
*/
static device*
device_find (const char *name)
{
unsigned int i;
for (i = 0; i < device_table->len; i ++) {
device *dev = g_ptr_array_index(device_table, i);
if (!strcmp(dev->name, name)) {
return dev;
}
}
return NULL;
}
/* Collect devices matching the flags. Return count of
* collected devices. If caller is only interested in
* the count, it is safe to call with out == NULL
*
* It's a caller responsibility to provide big enough
* output buffer (use device_table_size() to make a guess)
*
* Caller must own glib_main_loop lock
*/
static unsigned int
device_table_collect (unsigned int flags, device *out[])
{
unsigned int x, y = 0;
for (x = 0; x < device_table->len; x ++) {
device *dev = g_ptr_array_index(device_table, x);
if ((dev->flags & flags) != 0) {
if (out != NULL) {
out[y] = dev;
}
y ++;
}
}
return y;
}
/* Get current device_table size
*/
static unsigned int
device_table_size (void)
{
log_assert(NULL, device_table);
return device_table->len;
}
/* Purge device_table
*/
static void
device_table_purge (void)
{
size_t sz = device_table_size(), i;
device **devices = g_newa(device*, sz);
sz = device_table_collect(DEVICE_ALL_FLAGS, devices);
for (i = 0; i < sz; i ++) {
device_del(devices[i]);
}
}
/* Check if device table is ready, i.e., there is no DEVICE_INIT_WAIT
* devices
*/
static bool
device_table_ready (void)
{
return device_table_collect(DEVICE_INIT_WAIT, NULL) == 0;
}
/******************** Device state management ********************/
/* Get device state name, for debugging
*/
static inline const char*
device_state_name (DEVICE_STATE state)
{
switch (state) {
case DEVICE_SCAN_IDLE: return "SCAN_IDLE";
case DEVICE_SCAN_STARTED: return "SCAN_STARTED";
case DEVICE_SCAN_REQUESTING: return "SCAN_REQUESTING";
case DEVICE_SCAN_LOADING: return "SCAN_LOADING";
case DEVICE_SCAN_LOAD_RETRY: return "SCAN_LOAD_RETRY";
case DEVICE_SCAN_CHECK_STATUS: return "SCAN_CHECK_STATUS";
case DEVICE_SCAN_CLEANING_UP: return "SCAN_CLEANING_UP";
case DEVICE_SCAN_DONE: return "SCAN_DONE";
}
return "UNKNOWN";
}
/* Set device state
*/
static void
device_state_set (device *dev, DEVICE_STATE state)
{
if (dev->state != state) {
log_debug(dev, "state=%s; has_location=%s; retry=%d",
device_state_name(state),
dev->job_has_location ? "true" : "false",
dev->http_retry);
dev->state = state;
g_cond_broadcast(&dev->state_cond);
if (dev->state == DEVICE_SCAN_DONE) {
if ((dev->flags & DEVICE_SCANNING) != 0) {
pollable_signal(dev->read_pollable);
}
}
}
}
/******************** HTTP operations ********************/
/* Initiate HTTP request
*
* If request != NULL, it becomes a request message body. The
* memory ownership will be taken by this function, assuming
* request body needs to be released with g_free() after use
*
* Content type of the outgoing requests assumed to be "text/xml"
*/
static void
device_http_perform (device *dev, const char *path,
const char *method, char *body,
void (*callback)(device*, http_query *q))
{
http_uri *uri = http_uri_new_relative(dev->uri_escl, path, true, false);
http_query_new(dev->http_client, uri, method, body, "text/xml", callback);
}
/* Initiate HTTP GET request
*/
static void
device_http_get (device *dev, const char *path,
void (*callback)(device*, http_query *q))
{
device_http_perform(dev, path, "GET", NULL, callback);
}
/* Cancel pending HTTP request, if any
*/
static void
device_http_cancel (device *dev)
{
http_client_cancel(dev->http_client);
if (dev->http_timer != NULL) {
eloop_timer_cancel(dev->http_timer);
dev->http_timer = NULL;
}
}
/* http_client onerror callback
*/
static void
device_http_onerror (device *dev, error err) {
log_debug(dev, ESTRING(err));
device_job_set_status(dev, SANE_STATUS_IO_ERROR);
if (dev->job_has_location) {
device_escl_cleanup(dev);
} else {
device_state_set(dev, DEVICE_SCAN_DONE);
}
}
/******************** ESCL initialization ********************/
/* Probe next device address
*/
static void
device_probe_address (device *dev, zeroconf_addrinfo *addrinfo)
{
/* Cleanup after previous probe */
dev->addr_current = addrinfo;
if (dev->uri_escl != NULL) {
http_uri_free(dev->uri_escl);
}
/* Parse device URI */
dev->uri_escl = http_uri_new(addrinfo->uri, true);
log_assert(dev, dev->uri_escl != NULL);
/* Make sure eSCL URI's path ends with '/' character */
const char *path = http_uri_get_path(dev->uri_escl);
if (!g_str_has_suffix(path, "/")) {
size_t len = strlen(path);
char *path2 = g_alloca(len + 2);
memcpy(path2, path, len);
path2[len] = '/';
path2[len+1] = '\0';
http_uri_set_path(dev->uri_escl, path2);
}
/* Fetch device capabilities */
device_http_get(dev, "ScannerCapabilities",
device_scanner_capabilities_callback);
}
/* ScannerCapabilities fetch callback
*/
static void
device_scanner_capabilities_callback (device *dev, http_query *q)
{
error err = NULL;
/* Check request status */
err = http_query_error(q);
if (err != NULL) {
err = eloop_eprintf("ScannerCapabilities query: %s", ESTRING(err));
goto DONE;
}
/* Parse XML response */
http_data *data = http_query_get_response_data(q);
err = devopt_import_caps(&dev->opt, data->bytes, data->size);
if (err != NULL) {
err = eloop_eprintf("ScannerCapabilities: %s", err);
goto DONE;
}
devcaps_dump(dev->trace, &dev->opt.caps);
/* Cleanup and exit */
DONE:
if (err != NULL) {
log_debug(dev, ESTRING(err));
trace_error(dev->trace, err);
if (dev->addr_current != NULL && dev->addr_current->next != NULL) {
device_probe_address(dev, dev->addr_current->next);
} else {
device_del(dev);
}
} else {
dev->flags |= DEVICE_READY;
dev->flags &= ~DEVICE_INIT_WAIT;
http_client_onerror(dev->http_client, device_http_onerror);
}
g_cond_broadcast(&device_table_cond);
}
/******************** ESCL scanning ********************/
/* HTTP DELETE ${dev->job_location} callback
*/
static void
device_escl_cleanup_callback (device *dev, http_query *q)
{
(void) q;
device_state_set(dev, DEVICE_SCAN_DONE);
}
/* ESCL: cleanup after scan (delete current job)
*
* HTTP DELETE ${dev->job_location}
*/
static void
device_escl_cleanup (device *dev)
{
device_state_set(dev, DEVICE_SCAN_CLEANING_UP);
device_http_perform(dev, dev->job_location->str, "DELETE", NULL,
device_escl_cleanup_callback);
}
/* Parse ScannerStatus response.
*/
static SANE_Status
device_escl_decode_scanner_status (device *dev, const char *xml_text, size_t xml_len)
{
error err = NULL;
xml_rd *xml;
SANE_Status device_status = SANE_STATUS_UNSUPPORTED;
SANE_Status adf_status = SANE_STATUS_UNSUPPORTED;
SANE_Status status;
/* Decode XML */
err = xml_rd_begin(&xml, xml_text, xml_len);
if (err != NULL) {
goto DONE;
}
if (!xml_rd_node_name_match(xml, "scan:ScannerStatus")) {
err = ERROR("XML: missed scan:ScannerStatus");
goto DONE;
}
xml_rd_enter(xml);
for (; !xml_rd_end(xml); xml_rd_next(xml)) {
if (xml_rd_node_name_match(xml, "pwg:State")) {
const char *state = xml_rd_node_value(xml);
if (!strcmp(state, "Idle")) {
device_status = SANE_STATUS_GOOD;
} else if (!strcmp(state, "Processing")) {
device_status = SANE_STATUS_DEVICE_BUSY;
} else {
device_status = SANE_STATUS_UNSUPPORTED;
}
} else if (xml_rd_node_name_match(xml, "scan:AdfState")) {
const char *state = xml_rd_node_value(xml);
if (!strcmp(state, "ScannerAdfLoaded")) {
adf_status = SANE_STATUS_GOOD;
} else if (!strcmp(state, "ScannerAdfJam")) {
adf_status = SANE_STATUS_JAMMED;
} else if (!strcmp(state, "ScannerAdfDoorOpen")) {
adf_status = SANE_STATUS_COVER_OPEN;
} else if (!strcmp(state, "ScannerAdfProcessing")) {
/* Kyocera version */
adf_status = SANE_STATUS_NO_DOCS;
} else if (!strcmp(state, "ScannerAdfEmpty")) {
/* Cannon TR4500, EPSON XP-7100 */
adf_status = SANE_STATUS_NO_DOCS;
} else {
adf_status = SANE_STATUS_UNSUPPORTED;
}
}
}
/* Decode Job status */
if (device_status != SANE_STATUS_GOOD &&
device_status != SANE_STATUS_UNSUPPORTED) {
status = device_status;
} else if (dev->opt.src == OPT_SOURCE_PLATEN) {
status = device_status;
} else {
status = adf_status;
}
DONE:
xml_rd_finish(&xml);
trace_printf(dev->trace, "-----");
if (err != NULL) {
trace_printf(dev->trace, "Error: %s", ESTRING(err));
status = SANE_STATUS_IO_ERROR;
} else {
trace_printf(dev->trace, "Device status: %s",
sane_strstatus(device_status));
trace_printf(dev->trace, "ADF status: %s",
sane_strstatus(adf_status));
trace_printf(dev->trace, "Job status: %s",
sane_strstatus(status));
trace_printf(dev->trace, "");
}
return status;
}
/* HTTP GET ${dev->uri_escl}/ScannerStatus callback
*/
static void
device_escl_check_status_callback (device *dev, http_query *q)
{
error err = NULL;
SANE_Status status;
/* Decode status */
err = http_query_error(q);
if (err != NULL) {
trace_printf(dev->trace, "ScannerStatus query: %s", ESTRING(err));
status = SANE_STATUS_IO_ERROR;
} else {
http_data *data = http_query_get_response_data(q);
status = device_escl_decode_scanner_status(dev, data->bytes, data->size);
}
/* Now it't time to take a decision */
if (dev->checking_state == DEVICE_SCAN_LOADING &&
dev->checking_http_status == HTTP_STATUS_SERVICE_UNAVAILABLE ) {
/* Note, some devices may return HTTP_STATUS_SERVICE_UNAVAILABLE
* on attempt to load page immediately after job is created
*
* So if status doesn't cleanly indicate any error, lets retry
* several times
*/
switch (status) {
case SANE_STATUS_GOOD:
case SANE_STATUS_UNSUPPORTED:
case SANE_STATUS_DEVICE_BUSY:
if ( dev->http_retry + 1 < DEVICE_HTTP_RETRY_ATTEMPTS) {
dev->http_retry ++;
device_escl_load_retry(dev);
}
return;
default:
break;
}
}
if (status == SANE_STATUS_GOOD || status == SANE_STATUS_UNSUPPORTED) {
status = SANE_STATUS_IO_ERROR;
}
/* Set job status */
if (dev->job_images_received == 0) {
/* If we have received at least one image, ignore the
* error and finish the job with successful status.
* User will get the error upon attempt to request
* a next image
*/
device_job_set_status(dev, status);
}
/* Finish the job */
if (dev->job_has_location) {
device_escl_cleanup(dev);
} else {
device_state_set(dev, DEVICE_SCAN_DONE);
}
}
/* ESCL: check scanner status (used after failed scan request to
* clarify reasons)
*
* HTTP GET ${dev->uri_escl}/ScannerStatus
*/
static void
device_escl_check_status (device *dev, int http_status)
{
dev->checking_state = dev->state;
dev->checking_http_status = http_status;
device_state_set(dev, DEVICE_SCAN_CHECK_STATUS);
device_http_get(dev, "ScannerStatus",
device_escl_check_status_callback);
}
/* LOAD_RETRY timer callback
*/
static void
device_escl_load_retry_callback (void *data) {
device *dev = data;
dev->http_timer = NULL;
device_escl_load_page(dev);
}
/* Retry HTTP GET ${dev->job_location}/NextDocument
* after some delay
*/
static void
device_escl_load_retry (device *dev) {
device_state_set(dev, DEVICE_SCAN_LOAD_RETRY);
dev->http_timer = eloop_timer_new(DEVICE_HTTP_RETRY_PAUSE * 1000,
device_escl_load_retry_callback, dev);
}
/* HTTP GET ${dev->job_location}/NextDocument callback
*/
static void
device_escl_load_page_callback (device *dev, http_query *q)
{
error err;
/* Try to fetch next page until previous page fetched successfully */
err = http_query_error(q);
if (err == NULL) {
http_data *data = http_query_get_response_data(q);
g_ptr_array_add(dev->job_images, http_data_ref(data));
dev->job_images_received ++;
dev->http_retry = 0;
if (dev->job_images_received == 1) {
if (!device_read_push(dev)) {
device_job_abort(dev, SANE_STATUS_IO_ERROR);
return;
}
}
if (dev->opt.src == OPT_SOURCE_PLATEN) {
device_escl_cleanup(dev);
} else {
device_escl_load_page(dev);
}
} else {
device_escl_check_status(dev, http_query_status(q));
}
}
/* ESCL: load next page
*
* HTTP GET ${dev->job_location}/NextDocument request
*/
static void
device_escl_load_page (device *dev)
{
size_t sz = dev->job_location->len;
if (sz == 0 || dev->job_location->str[sz-1] != '/') {
g_string_append_c(dev->job_location, '/');
}
g_string_append(dev->job_location, "NextDocument");
device_state_set(dev, DEVICE_SCAN_LOADING);
device_http_get(dev, dev->job_location->str, device_escl_load_page_callback);
g_string_truncate(dev->job_location, sz);
}
/* HTTP POST ${dev->uri_escl}/ScanJobs callback
*/
static void
device_escl_start_scan_callback (device *dev, http_query *q)
{
error err = NULL;
const char *location;
SANE_Status status = SANE_STATUS_GOOD;
http_uri *uri;
/* Check HTTP status */
if (http_query_status(q) != HTTP_STATUS_CREATED) {
err = eloop_eprintf("ScanJobs request: unexpected HTTP status %d",
http_query_status(q));
goto DONE;
}
/* Obtain location */
location = http_query_get_response_header(q, "Location");
if (location == NULL || *location == '\0') {
err = eloop_eprintf("ScanJobs request: empty location received");
status = SANE_STATUS_IO_ERROR;
goto DONE;
}
/* Validate and save location */
uri = http_uri_new_relative(dev->uri_escl, location, true, true);
if (uri == NULL) {
err = eloop_eprintf("ScanJobs request: invalid location received");
status = SANE_STATUS_IO_ERROR;
goto DONE;
}
g_string_assign(dev->job_location, http_uri_get_path(uri));
dev->job_has_location = true;
http_uri_free(uri);
/* Check for pending cancellation */
if (dev->job_cancel_rq) {
device_job_set_status(dev, SANE_STATUS_CANCELLED);
device_escl_cleanup(dev);
return;
}
/* Start loading pages */
device_escl_load_page(dev);
return;
/* Cleanup and exit */
DONE:
log_debug(dev, ESTRING(err));
trace_error(dev->trace, err);
if (status == SANE_STATUS_GOOD) {
device_escl_check_status(dev, http_query_status(q));
} else {
device_job_set_status(dev, status);
device_state_set(dev, DEVICE_SCAN_DONE);
}
}
/* Geometrical scan parameters
*/
typedef struct {
SANE_Word off; /* Requested X/Y offset, in pixels assuming 300 DPI */
SANE_Word len; /* Requested width/height, in pixels, assuming 300 DPI */
SANE_Word skip; /* Pixels to skip in returned image, in pixels assuming
actual resolution */
}
device_geom;
/*
* Computing geometrical scan parameters
*
* Input:
* tl, br - top-left, bottom-rights X/Y, in mm
* minlen, maxlen - device-defined min and max width or height,
* in pixels, assuming 300 dpi
* res - scan resolution
*
* Output:
* Filled device_geom structure
*
* Problem description.
*
* First of all, we use 3 different units to deal with geometrical
* parameters:
* 1) we communicate with frontend in millimeters
* 2) we communicate with scanner in pixels, assuming 300 DPI
* 3) when we deal with image, sizes are in pixels in real
* resolution
*
* Second, scanner returns minimal and maximal window size, but
* to simplify frontend's life, we pretend there is no such thing,
* as a minimal width or height, otherwise TL and BR ranges become
* dependent from each other. Instead, we always request image from
* scanner not smaller that scanner's minimum, and clip excessive
* image parts, if required.
*
* This all makes things non-trivial. This function handles
* this complexity
*/
static device_geom
device_geom_compute (SANE_Fixed tl, SANE_Fixed br,
SANE_Word minlen, SANE_Word maxlen, SANE_Word res)
{
device_geom geom;
geom.off = math_mm2px(tl);
geom.len = math_mm2px(br - tl);
geom.skip = 0;
minlen = math_max(minlen, 1);
geom.len = math_bound(geom.len, minlen, maxlen);
if (geom.off + geom.len > maxlen) {
geom.skip = geom.off + geom.len - maxlen;
geom.off -= geom.skip;
geom.skip = math_muldiv(geom.skip, res, 300);
}
return geom;
}
/* ESCL: start scanning
*
* HTTP POST ${dev->uri_escl}/ScanJobs
*/
static void
device_escl_start_scan (device *dev)
{
const char *source = NULL;
const char *colormode = NULL;
bool duplex = false;
const char *mime = "image/jpeg";
//const char *mime = "application/pdf";
SANE_Word x_resolution = dev->opt.resolution;
SANE_Word y_resolution = dev->opt.resolution;
devcaps_source *src = dev->opt.caps.src[dev->opt.src];
device_geom geom_x, geom_y;
char buf[64];
/* Prepare window parameters */
geom_x = device_geom_compute(dev->opt.tl_x, dev->opt.br_x,
src->min_wid_px, src->max_wid_px, x_resolution);
geom_y = device_geom_compute(dev->opt.tl_y, dev->opt.br_y,
src->min_hei_px, src->max_hei_px, y_resolution);
dev->job_skip_x = geom_x.skip;
dev->job_skip_y = geom_y.skip;
/* Prepare other parameters */
switch (dev->opt.src) {
case OPT_SOURCE_PLATEN: source = "Platen"; duplex = false; break;
case OPT_SOURCE_ADF_SIMPLEX: source = "Feeder"; duplex = false; break;
case OPT_SOURCE_ADF_DUPLEX: source = "Feeder"; duplex = true; break;
default:
log_internal_error(dev);
}
switch (dev->opt.colormode) {
case OPT_COLORMODE_COLOR: colormode = "RGB24"; break;
case OPT_COLORMODE_GRAYSCALE: colormode = "Grayscale8"; break;
case OPT_COLORMODE_LINEART: colormode = "BlackAndWhite1"; break;
default:
log_internal_error(dev);
}
/* Dump parameters */
trace_printf(dev->trace, "==============================");
trace_printf(dev->trace, "Starting scan, using the following parameters:");
trace_printf(dev->trace, " source: %s", source);
trace_printf(dev->trace, " colormode: %s", colormode);
trace_printf(dev->trace, " tl_x: %s mm",
math_fmt_mm(dev->opt.tl_x, buf));
trace_printf(dev->trace, " tl_y: %s mm",
math_fmt_mm(dev->opt.tl_y, buf));
trace_printf(dev->trace, " br_x: %s mm",
math_fmt_mm(dev->opt.br_x, buf));
trace_printf(dev->trace, " br_y: %s mm",
math_fmt_mm(dev->opt.br_y, buf));
trace_printf(dev->trace, " image size: %dx%d", geom_x.len, geom_y.len);
trace_printf(dev->trace, " image X offset: %d", geom_x.off);
trace_printf(dev->trace, " image Y offset: %d", geom_y.off);
trace_printf(dev->trace, " x_resolution: %d", x_resolution);
trace_printf(dev->trace, " y_resolution: %d", y_resolution);
trace_printf(dev->trace, " image format: %s", mime);
trace_printf(dev->trace, " duplex: %s", duplex ? "true" : "false");
trace_printf(dev->trace, "");
/* Build scan request */
xml_wr *xml = xml_wr_begin("scan:ScanSettings");
xml_wr_add_text(xml, "pwg:Version", "2.0");
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", geom_x.off);
xml_wr_add_uint(xml, "pwg:YOffset", geom_y.off);
xml_wr_add_uint(xml, "pwg:Width", geom_x.len);
xml_wr_add_uint(xml, "pwg:Height", geom_y.len);
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);
xml_wr_add_text(xml, "scan:ColorMode", colormode);