-
Notifications
You must be signed in to change notification settings - Fork 1
/
airscan-device.c
1904 lines (1595 loc) · 53.9 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>
#include <unistd.h>
/******************** Constants ********************/
/* HTTP timeouts, by operation, in milliseconds
*/
#define DEVICE_HTTP_TIMEOUT_DEVCAPS 5000
#define DEVICE_HTTP_TIMEOUT_PRECHECK 5000
#define DEVICE_HTTP_TIMEOUT_SCAN 30000
#define DEVICE_HTTP_TIMEOUT_LOAD -1
#define DEVICE_HTTP_TIMEOUT_CHECK 5000
#define DEVICE_HTTP_TIMEOUT_CLEANUP 30000
#define DEVICE_HTTP_TIMEOUT_CANCEL 30000
/* HTTP timeout for operation that was pending
* in a moment of cancel (if any)
*/
#define DEVICE_HTTP_TIMEOUT_CANCELED_OP 10000
/******************** Device management ********************/
/* Device flags
*/
enum {
DEVICE_SCANNING = (1 << 0), /* We are between sane_start() and
final sane_read() */
DEVICE_READING = (1 << 1) /* sane_read() can be called */
};
/* Device state diagram
*
* OPENED
* |
* V
* PROBING->PROBING_FAILED--------------------------------
* | |
* V |
* -->IDLE |
* | | submit PROTO_OP_SCAN |
* | V |
* | SCANNING---------- |
* | | | async cancel request received, |
* | | | dev->stm_cancel_event signalled |
* | | V |
* | | CANCEL_REQ |
* | | | dev->stm_cancel_event callback |
* | | | |
* | | +------ |
* | | reached | | PROTO_OP_SCAN still pending |
* | | CLEANUP | V |
* | |<----------------CANCEL_DELAYED |
* | | | | | PROTO_OP_SCAN failed |
* | | V V ----------------- |
* | | CANCEL_SENT | |
* | | job | | cancel request | |
* | | finished | | finished | |
* | | V V | |
* | | CANCEL_JOB_DONE CANCEL_REQ_DONE | |
* | | cancel req | | job | |
* | | finished | | finished | |
* | V | | | |
* | CLEANUP | | | |
* | | | | | |
* | V V V V |
* ---DONE<-------------------------------------- |
* | |
* V |
* CLOSED<------------------------------------------------
*/
typedef enum {
DEVICE_STM_OPENED,
DEVICE_STM_PROBING,
DEVICE_STM_PROBING_FAILED,
DEVICE_STM_IDLE,
DEVICE_STM_SCANNING,
DEVICE_STM_CANCEL_REQ,
DEVICE_STM_CANCEL_DELAYED,
DEVICE_STM_CANCEL_SENT,
DEVICE_STM_CANCEL_JOB_DONE,
DEVICE_STM_CANCEL_REQ_DONE,
DEVICE_STM_CLEANUP,
DEVICE_STM_DONE,
DEVICE_STM_CLOSED
} DEVICE_STM_STATE;
/* Device descriptor
*/
struct device {
/* Common part */
zeroconf_devinfo *devinfo; /* Device info */
log_ctx *log; /* Logging context */
unsigned int flags; /* Device flags */
devopt opt; /* Device options */
int checking_http_status; /* HTTP status before CHECK_STATUS */
/* State machinery */
DEVICE_STM_STATE stm_state; /* Device state */
pthread_cond_t stm_cond; /* Signalled when state changes */
eloop_event *stm_cancel_event; /* Signalled to initiate cancel */
http_query *stm_cancel_query; /* CANCEL query */
bool stm_cancel_sent; /* Cancel was sent to device */
eloop_timer *stm_timer; /* Delay timer */
struct timespec stm_last_fail_time;/* Last failed sane_start() time */
/* Protocol handling */
proto_ctx proto_ctx; /* Protocol handler context */
/* I/O handling (AVAHI and HTTP) */
zeroconf_endpoint *endpoint_current; /* Current endpoint to probe */
/* Job status */
SANE_Status job_status; /* Job completion status */
SANE_Word job_skip_x; /* How much pixels to skip, */
SANE_Word job_skip_y; /* from left and top */
/* Image decoders */
image_decoder *decoders[NUM_ID_FORMAT]; /* Decoders by format */
/* Read machinery */
SANE_Bool read_non_blocking; /* Non-blocking I/O mode */
pollable *read_pollable; /* Signalled when read won't
block */
http_data_queue *read_queue; /* Queue of received images */
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_real_wid; /* Real line width */
SANE_Int read_line_off; /* Current offset in the line */
SANE_Int read_skip_bytes; /* How many bytes to skip at line
beginning */
bool read_24_to_8; /* Resample 24 to 8 bits */
filter *read_filters; /* Chain of image filters */
};
/* Static variables
*/
static device **device_table;
/* Forward declarations
*/
static device*
device_find_by_ident (const char *ident);
static void
device_http_cancel (device *dev);
static void
device_http_onerror (void *ptr, error err);
static void
device_proto_set (device *dev, ID_PROTO proto);
static void
device_scanner_capabilities_callback (void *ptr, http_query *q);
static void
device_probe_endpoint (device *dev, zeroconf_endpoint *endpoint);
static void
device_job_set_status (device *dev, SANE_Status status);
static inline DEVICE_STM_STATE
device_stm_state_get (device *dev);
static void
device_stm_state_set (device *dev, DEVICE_STM_STATE state);
static bool
device_stm_cancel_perform (device *dev, SANE_Status status);
static void
device_stm_op_callback (void *ptr, http_query *q);
static void
device_stm_cancel_event_callback (void *data);
static void
device_read_filters_setup (device *dev);
static void
device_read_filters_cleanup (device *dev);
static void
device_management_start_stop (bool start);
/******************** Device table management ********************/
/* Create a device.
*
* May fail. At this case, NULL will be returned and status will be set
*/
static device*
device_new (zeroconf_devinfo *devinfo)
{
device *dev;
/* Create device */
dev = mem_new(device, 1);
dev->devinfo = devinfo;
dev->log = log_ctx_new(dev->devinfo->name, NULL);
log_debug(dev->log, "device created");
dev->proto_ctx.log = dev->log;
dev->proto_ctx.devcaps = &dev->opt.caps;
devopt_init(&dev->opt);
dev->proto_ctx.http = http_client_new(dev->log, dev);
pthread_cond_init(&dev->stm_cond, NULL);
dev->read_pollable = pollable_new();
dev->read_queue = http_data_queue_new();
/* Add to the table */
device_table = ptr_array_append(device_table, dev);
return dev;
}
/* Destroy a device
*/
static void
device_free (device *dev, const char *log_msg)
{
int i;
/* Remove device from table */
log_debug(dev->log, "removed from device table");
ptr_array_del(device_table, ptr_array_find(device_table, dev));
/* Stop all pending I/O activity */
device_http_cancel(dev);
if (dev->stm_cancel_event != NULL) {
eloop_event_free(dev->stm_cancel_event);
}
if (dev->stm_timer != NULL) {
eloop_timer_cancel(dev->stm_timer);
}
/* Release all memory */
device_proto_set(dev, ID_PROTO_UNKNOWN);
devopt_cleanup(&dev->opt);
http_client_free(dev->proto_ctx.http);
http_uri_free(dev->proto_ctx.base_uri);
http_uri_free(dev->proto_ctx.base_uri_nozone);
mem_free((char*) dev->proto_ctx.location);
pthread_cond_destroy(&dev->stm_cond);
for (i = 0; i < NUM_ID_FORMAT; i ++) {
image_decoder *decoder = dev->decoders[i];
if (decoder != NULL) {
image_decoder_free(decoder);
log_debug(dev->log, "closed decoder: %s", id_format_short_name(i));
}
}
http_data_queue_free(dev->read_queue);
pollable_free(dev->read_pollable);
device_read_filters_cleanup(dev);
log_debug(dev->log, "device destroyed");
if (log_msg != NULL) {
log_debug(dev->log, "%s", log_msg);
}
log_ctx_free(dev->log);
zeroconf_devinfo_free(dev->devinfo);
mem_free(dev);
}
/* Start probing. Called via eloop_call
*/
static void
device_start_probing (void *data)
{
device *dev = data;
device_probe_endpoint(dev, dev->devinfo->endpoints);
}
/* Start device I/O.
*/
static SANE_Status
device_io_start (device *dev)
{
dev->stm_cancel_event = eloop_event_new(device_stm_cancel_event_callback, dev);
if (dev->stm_cancel_event == NULL) {
return SANE_STATUS_NO_MEM;
}
device_stm_state_set(dev, DEVICE_STM_PROBING);
eloop_call(device_start_probing, dev);
return SANE_STATUS_GOOD;
}
/* Find device by ident
*/
static device*
device_find_by_ident (const char *ident)
{
size_t i, len = mem_len(device_table);
for (i = 0; i < len; i ++) {
device *dev = device_table[i];
if (!strcmp(dev->devinfo->ident, ident)) {
return dev;
}
}
return NULL;
}
/* Purge device_table
*/
static void
device_table_purge (void)
{
while (mem_len(device_table) > 0) {
device_free(device_table[0], NULL);
}
}
/******************** Underlying protocol operations ********************/
/* Set protocol handler
*/
static void
device_proto_set (device *dev, ID_PROTO proto)
{
if (dev->proto_ctx.proto != NULL) {
log_debug(dev->log, "closed protocol \"%s\"",
dev->proto_ctx.proto->name);
dev->proto_ctx.proto->free(dev->proto_ctx.proto);
dev->proto_ctx.proto = NULL;
}
if (proto != ID_PROTO_UNKNOWN) {
dev->proto_ctx.proto = proto_handler_new(proto);
log_assert(dev->log, dev->proto_ctx.proto != NULL);
log_debug(dev->log, "using protocol \"%s\"",
dev->proto_ctx.proto->name);
}
}
/* Set base URI. `uri' ownership is taken by this function
*/
static void
device_proto_set_base_uri (device *dev, http_uri *uri)
{
http_uri_free(dev->proto_ctx.base_uri);
dev->proto_ctx.base_uri = uri;
http_uri_free(dev->proto_ctx.base_uri_nozone);
dev->proto_ctx.base_uri_nozone = http_uri_clone(uri);
http_uri_strip_zone_suffux(dev->proto_ctx.base_uri_nozone);
}
/* Query device capabilities
*/
static void
device_proto_devcaps_submit (device *dev, void (*callback) (void*, http_query*))
{
http_query *q;
q = dev->proto_ctx.proto->devcaps_query(&dev->proto_ctx);
http_query_timeout(q, DEVICE_HTTP_TIMEOUT_DEVCAPS);
http_query_submit(q, callback);
dev->proto_ctx.query = q;
}
/* Decode device capabilities
*/
static error
device_proto_devcaps_decode (device *dev, devcaps *caps)
{
return dev->proto_ctx.proto->devcaps_decode(&dev->proto_ctx, caps);
}
/* http_query_onrxhdr() callback
*/
static void
device_proto_op_onrxhdr (void *p, http_query *q)
{
device *dev = p;
if (dev->proto_ctx.op == PROTO_OP_LOAD && !dev->stm_cancel_sent) {
http_query_timeout(q, -1);
}
}
/* Submit operation request
*/
static void
device_proto_op_submit (device *dev, PROTO_OP op,
void (*callback) (void*, http_query*))
{
http_query *(*func) (const proto_ctx *ctx) = NULL;
int timeout = -1;
http_query *q;
switch (op) {
case PROTO_OP_NONE: log_internal_error(dev->log); break;
case PROTO_OP_FINISH: log_internal_error(dev->log); break;
case PROTO_OP_PRECHECK:
func = dev->proto_ctx.proto->precheck_query;
timeout = DEVICE_HTTP_TIMEOUT_PRECHECK;
break;
case PROTO_OP_SCAN:
func = dev->proto_ctx.proto->scan_query;
timeout = DEVICE_HTTP_TIMEOUT_SCAN;
break;
case PROTO_OP_LOAD:
func = dev->proto_ctx.proto->load_query;
timeout = DEVICE_HTTP_TIMEOUT_LOAD;
break;
case PROTO_OP_CHECK:
func = dev->proto_ctx.proto->status_query;
timeout = DEVICE_HTTP_TIMEOUT_CHECK;
break;
case PROTO_OP_CLEANUP:
func = dev->proto_ctx.proto->cleanup_query;
timeout = DEVICE_HTTP_TIMEOUT_CLEANUP;
break;
}
log_assert(dev->log, func != NULL);
log_debug(dev->log, "%s: submitting: attempt=%d",
proto_op_name(op), dev->proto_ctx.failed_attempt);
dev->proto_ctx.op = op;
q = func(&dev->proto_ctx);
http_query_timeout(q, timeout);
if (op == PROTO_OP_LOAD) {
http_query_onrxhdr(q, device_proto_op_onrxhdr);
}
http_query_submit(q, callback);
dev->proto_ctx.query = q;
}
/* Dummy decode for PROTO_OP_CANCEL and PROTO_OP_CLEANUP
*/
static proto_result
device_proto_dummy_decode (const proto_ctx *ctx)
{
proto_result result = {0};
(void) ctx;
result.next = PROTO_OP_FINISH;
return result;
}
/* Decode operation response
*/
static proto_result
device_proto_op_decode (device *dev, PROTO_OP op)
{
proto_result (*func) (const proto_ctx *ctx) = NULL;
proto_result result;
switch (op) {
case PROTO_OP_NONE: log_internal_error(dev->log); break;
case PROTO_OP_PRECHECK:func = dev->proto_ctx.proto->precheck_decode; break;
case PROTO_OP_SCAN: func = dev->proto_ctx.proto->scan_decode; break;
case PROTO_OP_LOAD: func = dev->proto_ctx.proto->load_decode; break;
case PROTO_OP_CHECK: func = dev->proto_ctx.proto->status_decode; break;
case PROTO_OP_CLEANUP: func = device_proto_dummy_decode; break;
case PROTO_OP_FINISH: log_internal_error(dev->log); break;
}
log_assert(dev->log, func != NULL);
log_debug(dev->log, "%s: decoding", proto_op_name(op));
result = func(&dev->proto_ctx);
log_debug(dev->log, "%s: decoded: status=\"%s\" next=%s delay=%d",
proto_op_name(op),
sane_strstatus(result.status),
proto_op_name(result.next),
result.delay);
if (result.next == PROTO_OP_CHECK) {
int http_status = http_query_status(dev->proto_ctx.query);
dev->proto_ctx.failed_op = op;
dev->proto_ctx.failed_http_status = http_status;
}
if (op == PROTO_OP_CHECK) {
dev->proto_ctx.failed_attempt ++;
}
return result;
}
/******************** HTTP operations ********************/
/* Cancel pending HTTP request, if any
*/
static void
device_http_cancel (device *dev)
{
http_client_cancel(dev->proto_ctx.http);
if (dev->stm_timer != NULL) {
eloop_timer_cancel(dev->stm_timer);
dev->stm_timer = NULL;
}
}
/* http_client onerror callback
*/
static void
device_http_onerror (void *ptr, error err)
{
device *dev = ptr;
SANE_Status status;
status = err == ERROR_ENOMEM ? SANE_STATUS_NO_MEM : SANE_STATUS_IO_ERROR;
log_debug(dev->log, "cancelling job due to error: %s", ESTRING(err));
if (!device_stm_cancel_perform(dev, status)) {
device_stm_state_set(dev, DEVICE_STM_DONE);
} else {
/* Scan job known to be done, now waiting for cancel
* completion
*/
device_stm_state_set(dev, DEVICE_STM_CANCEL_JOB_DONE);
}
}
/******************** Protocol initialization ********************/
/* Probe next device address
*/
static void
device_probe_endpoint (device *dev, zeroconf_endpoint *endpoint)
{
/* Switch endpoint */
log_assert(dev->log, endpoint->proto != ID_PROTO_UNKNOWN);
if (dev->endpoint_current == NULL ||
dev->endpoint_current->proto != endpoint->proto) {
device_proto_set(dev, endpoint->proto);
}
dev->endpoint_current = endpoint;
device_proto_set_base_uri(dev, http_uri_clone(endpoint->uri));
/* Fetch device capabilities */
device_proto_devcaps_submit (dev, device_scanner_capabilities_callback);
}
/* Scanner capabilities fetch callback
*/
static void
device_scanner_capabilities_callback (void *ptr, http_query *q)
{
error err = NULL;
device *dev = ptr;
int i;
unsigned int formats;
/* Check request status */
err = http_query_error(q);
if (err != NULL) {
err = eloop_eprintf("scanner capabilities query: %s", ESTRING(err));
goto DONE;
}
/* Parse XML response */
err = device_proto_devcaps_decode (dev, &dev->opt.caps);
if (err != NULL) {
err = eloop_eprintf("scanner capabilities: %s", err);
goto DONE;
}
devcaps_dump(dev->log, &dev->opt.caps);
devopt_set_defaults(&dev->opt);
/* Setup decoders */
formats = 0;
for (i = 0; i < NUM_ID_SOURCE; i ++) {
devcaps_source *src = dev->opt.caps.src[i];
if (src != NULL) {
formats |= src->formats;
}
}
formats &= DEVCAPS_FORMATS_SUPPORTED;
for (i = 0; i < NUM_ID_FORMAT; i ++) {
if ((formats & (1 << i)) != 0) {
switch (i) {
case ID_FORMAT_JPEG:
dev->decoders[i] = image_decoder_jpeg_new();
break;
case ID_FORMAT_PNG:
dev->decoders[i] = image_decoder_png_new();
break;
case ID_FORMAT_BMP:
dev->decoders[i] = image_decoder_bmp_new();
break;
default:
log_internal_error(dev->log);
}
log_debug(dev->log, "new decoder: %s", id_format_short_name(i));
}
}
/* Update endpoint address in case of HTTP redirection */
if (!http_uri_equal(http_query_uri(q), http_query_real_uri(q))) {
const char *uri_str = http_uri_str(http_query_uri(q));
const char *real_uri_str = http_uri_str(http_query_real_uri(q));
const char *base_str = http_uri_str(dev->proto_ctx.base_uri);
if (str_has_prefix(uri_str, base_str)) {
const char *tail = uri_str + strlen(base_str);
if (str_has_suffix(real_uri_str, tail)) {
size_t l = strlen(real_uri_str) - strlen(tail);
char *new_uri_str = alloca(l + 1);
http_uri *new_uri;
memcpy(new_uri_str, real_uri_str, l);
new_uri_str[l] = '\0';
log_debug(dev->log, "endpoint URI changed due to redirection:");
log_debug(dev->log, " old URL: %s", base_str);
log_debug(dev->log, " new URL: %s", new_uri_str);
new_uri = http_uri_new(new_uri_str, true);
log_assert(dev->log, new_uri != NULL);
device_proto_set_base_uri(dev, new_uri);
}
}
}
/* Cleanup and exit */
DONE:
if (err != NULL) {
log_debug(dev->log, ESTRING(err));
if (dev->endpoint_current != NULL &&
dev->endpoint_current->next != NULL) {
device_probe_endpoint(dev, dev->endpoint_current->next);
} else {
device_stm_state_set(dev, DEVICE_STM_PROBING_FAILED);
}
} else {
device_stm_state_set(dev, DEVICE_STM_IDLE);
http_client_onerror(dev->proto_ctx.http, device_http_onerror);
}
}
/******************** Scan state machinery ********************/
/* Get state name, for debugging
*/
static const char*
device_stm_state_name (DEVICE_STM_STATE state)
{
switch (state) {
case DEVICE_STM_OPENED: return "DEVICE_STM_OPENED";
case DEVICE_STM_PROBING: return "DEVICE_STM_PROBING";
case DEVICE_STM_PROBING_FAILED: return "DEVICE_STM_PROBING_FAILED";
case DEVICE_STM_IDLE: return "DEVICE_STM_IDLE";
case DEVICE_STM_SCANNING: return "DEVICE_STM_SCANNING";
case DEVICE_STM_CANCEL_REQ: return "DEVICE_STM_CANCEL_REQ";
case DEVICE_STM_CANCEL_DELAYED: return "DEVICE_STM_CANCEL_DELAYED";
case DEVICE_STM_CANCEL_SENT: return "DEVICE_STM_CANCEL_SENT";
case DEVICE_STM_CANCEL_JOB_DONE: return "DEVICE_STM_CANCEL_JOB_DONE";
case DEVICE_STM_CANCEL_REQ_DONE: return "DEVICE_STM_CANCEL_REQ_DONE";
case DEVICE_STM_CLEANUP: return "DEVICE_STM_CLEANUP";
case DEVICE_STM_DONE: return "DEVICE_STM_DONE";
case DEVICE_STM_CLOSED: return "DEVICE_STM_CLOSED";
}
return NULL;
}
/* Get state
*/
static inline DEVICE_STM_STATE
device_stm_state_get (device *dev)
{
return __atomic_load_n(&dev->stm_state, __ATOMIC_SEQ_CST);
}
/* Check if device is in working state
*/
static bool
device_stm_state_working (device *dev)
{
DEVICE_STM_STATE state = device_stm_state_get(dev);
return state > DEVICE_STM_IDLE && state < DEVICE_STM_DONE;
}
/* Set state
*/
static void
device_stm_state_set (device *dev, DEVICE_STM_STATE state)
{
DEVICE_STM_STATE old_state = device_stm_state_get(dev);
if (old_state != state) {
log_debug(dev->log, "%s->%s",
device_stm_state_name(old_state), device_stm_state_name(state));
__atomic_store_n(&dev->stm_state, state, __ATOMIC_SEQ_CST);
pthread_cond_broadcast(&dev->stm_cond);
if (!device_stm_state_working(dev)) {
pollable_signal(dev->read_pollable);
}
}
}
/* cancel_query() callback
*/
static void
device_stm_cancel_callback (void *ptr, http_query *q)
{
device *dev = ptr;
(void) q;
dev->stm_cancel_query = NULL;
if (device_stm_state_get(dev) == DEVICE_STM_CANCEL_JOB_DONE) {
device_stm_state_set(dev, DEVICE_STM_DONE);
} else {
device_stm_state_set(dev, DEVICE_STM_CANCEL_REQ_DONE);
}
}
/* Perform cancel, if possible
*/
static bool
device_stm_cancel_perform (device *dev, SANE_Status status)
{
proto_ctx *ctx = &dev->proto_ctx;
device_job_set_status(dev, status);
if (ctx->location != NULL && !dev->stm_cancel_sent) {
if (ctx->params.src == ID_SOURCE_PLATEN &&
ctx->images_received > 0) {
/* If we are not expecting more images, skip cancel
* and simple wait until job is done
*
* Otherwise Xerox VersaLink B405 remains busy for
* a quite long time without any need
*/
log_debug(dev->log, "cancel skipped as job is almost done");
return false;
} else {
/* Otherwise, perform a normal cancel operation
*/
device_stm_state_set(dev, DEVICE_STM_CANCEL_SENT);
log_assert(dev->log, dev->stm_cancel_query == NULL);
dev->stm_cancel_query = ctx->proto->cancel_query(ctx);
http_query_onerror(dev->stm_cancel_query, NULL);
http_query_timeout(dev->stm_cancel_query,
DEVICE_HTTP_TIMEOUT_CANCEL);
http_client_timeout(dev->proto_ctx.http,
DEVICE_HTTP_TIMEOUT_CANCELED_OP);
http_query_submit(dev->stm_cancel_query, device_stm_cancel_callback);
dev->stm_cancel_sent = true;
}
return true;
}
return false;
}
/* stm_cancel_event callback
*/
static void
device_stm_cancel_event_callback (void *data)
{
device *dev = data;
log_debug(dev->log, "cancel processing started");
if (!device_stm_cancel_perform(dev, SANE_STATUS_CANCELLED)) {
device_stm_state_set(dev, DEVICE_STM_CANCEL_DELAYED);
}
}
/* Request cancel.
*
* Note, reason must be NULL, if cancel requested from the signal handler
*/
static void
device_stm_cancel_req (device *dev, const char *reason)
{
DEVICE_STM_STATE expected = DEVICE_STM_SCANNING;
bool ok = __atomic_compare_exchange_n(&dev->stm_state, &expected,
DEVICE_STM_CANCEL_REQ, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
if (ok) {
if (reason != NULL) {
log_debug(dev->log, "cancel requested: %s", reason);
}
eloop_event_trigger(dev->stm_cancel_event);
}
}
/* stm_timer callback
*/
static void
device_stm_timer_callback (void *data)
{
device *dev = data;
dev->stm_timer = NULL;
device_proto_op_submit(dev, dev->proto_ctx.op, device_stm_op_callback);
}
/* Operation callback
*/
static void
device_stm_op_callback (void *ptr, http_query *q)
{
device *dev = ptr;
proto_result result = device_proto_op_decode(dev, dev->proto_ctx.op);
(void) q;
if (result.err != NULL) {
log_debug(dev->log, "%s", ESTRING(result.err));
}
/* Save useful result, if any */
if (dev->proto_ctx.op == PROTO_OP_SCAN) {
if (result.data.location != NULL) {
mem_free((char*) dev->proto_ctx.location); /* Just in case */
dev->proto_ctx.location = result.data.location;
dev->proto_ctx.failed_attempt = 0;
pthread_cond_broadcast(&dev->stm_cond);
}
} else if (dev->proto_ctx.op == PROTO_OP_LOAD) {
if (result.data.image != NULL) {
http_data_queue_push(dev->read_queue, result.data.image);
dev->proto_ctx.images_received ++;
pollable_signal(dev->read_pollable);
dev->proto_ctx.failed_attempt = 0;
pthread_cond_broadcast(&dev->stm_cond);
}
}
/* Update job status */
device_job_set_status(dev, result.status);
/* If CANCEL was sent, and next operation is CLEANUP or
* current operation is CHECK, FINISH the job
*/
if (dev->stm_cancel_sent) {
if (result.next == PROTO_OP_CLEANUP ||
dev->proto_ctx.op == PROTO_OP_CHECK) {
result.next = PROTO_OP_FINISH;
}
}
/* Check for FINISH */
if (result.next == PROTO_OP_FINISH) {
if (dev->proto_ctx.images_received == 0) {
/* If no images received, and no error status
* yet set, use SANE_STATUS_IO_ERROR as default
* error code
*/
device_job_set_status(dev, SANE_STATUS_IO_ERROR);
}
if (device_stm_state_get(dev) == DEVICE_STM_CANCEL_SENT) {
device_stm_state_set(dev, DEVICE_STM_CANCEL_JOB_DONE);
} else {
device_stm_state_set(dev, DEVICE_STM_DONE);
}
return;
}
/* Handle switch to PROTO_OP_CLEANUP state */
if (result.next == PROTO_OP_CLEANUP) {
device_stm_state_set(dev, DEVICE_STM_CLEANUP);
}
/* Handle delayed cancellation */
if (device_stm_state_get(dev) == DEVICE_STM_CANCEL_DELAYED) {
if (!device_stm_cancel_perform(dev, SANE_STATUS_CANCELLED)) {
/* Finish the job, if we has not yet reached cancellable
* state
*/
device_stm_state_set(dev, DEVICE_STM_DONE);
return;
}
}
/* Handle delay */
if (result.delay != 0) {
log_assert(dev->log, dev->stm_timer == NULL);
dev->stm_timer = eloop_timer_new(result.delay,
device_stm_timer_callback, dev);
dev->proto_ctx.op = result.next;
return;
}
/* Submit next operation */
device_proto_op_submit(dev, result.next, device_stm_op_callback);
}
/* 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
* protocol-specific DPI (defined by devcaps::units)
* 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, SANE_Word units)
{
device_geom geom;
geom.off = math_mm2px_res(tl, units);
geom.len = math_mm2px_res(br - tl, units);
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;