-
Notifications
You must be signed in to change notification settings - Fork 1
/
airscan.c
441 lines (343 loc) · 10.7 KB
/
airscan.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
/* 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
*
* SANE API
*/
#include "airscan.h"
/* Static variables
*/
static const SANE_Device **sane_device_list;
/* Initialize the backend
*/
SANE_Status
sane_init (SANE_Int *version_code, SANE_Auth_Callback authorize)
{
SANE_Status status;
if (version_code != NULL) {
*version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR,
SANE_CURRENT_MINOR, 0);
}
(void) authorize;
status = airscan_init(0, "API: sane_init(): called");
if (status == SANE_STATUS_GOOD) {
status = device_management_init();
}
if (status != SANE_STATUS_GOOD) {
log_debug(NULL, "API: sane_init(): %s", sane_strstatus(status));
}
return status;
}
/* Exit the backend
*/
void
sane_exit (void)
{
log_debug(NULL, "API: sane_exit(): called");
eloop_thread_stop();
device_management_cleanup();
airscan_cleanup("API: sane_exit(): OK");
}
/* Get list of devices
*/
SANE_Status
sane_get_devices (const SANE_Device ***device_list, SANE_Bool local_only)
{
log_debug(NULL, "API: sane_get_devices(): called");
if (local_only) {
/* Note, all our devices are non-local */
static const SANE_Device *empty_devlist[1] = {0};
*device_list = empty_devlist;
} else {
eloop_mutex_lock();
zeroconf_device_list_free(sane_device_list);
sane_device_list = zeroconf_device_list_get();
*device_list = sane_device_list;
eloop_mutex_unlock();
}
log_debug(NULL, "API: sane_get_devices(): done");
return SANE_STATUS_GOOD;
}
/* Open the device
*/
SANE_Status
sane_open (SANE_String_Const name, SANE_Handle *handle)
{
SANE_Status status;
device *dev;
const SANE_Device **dev_list = NULL;
log_debug(NULL, "API: sane_open(\"%s\"): called", name ? name : "");
eloop_mutex_lock();
/* If name is not set, open the first device
*/
if (name == NULL || *name == '\0') {
dev_list = zeroconf_device_list_get();
if (dev_list[0] != NULL) {
name = dev_list[0]->name;
}
}
dev = device_open(name, &status);
eloop_mutex_unlock();
if (dev != NULL) {
*handle = (SANE_Handle) dev;
}
log_debug(device_log_ctx(dev), "API: sane_open(\"%s\"): %s",
name ? name : "", sane_strstatus(status));
zeroconf_device_list_free(dev_list);
return status;
}
/* Close the device
*/
void
sane_close (SANE_Handle handle)
{
device *dev = (device*) handle;
log_debug(device_log_ctx(dev), "API: sane_close(): called");
eloop_mutex_lock();
device_close((device*) handle, "API: sane_close(): done");
eloop_mutex_unlock();
}
/* Get option descriptor
*/
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
device *dev = (device*) handle;
log_ctx *log = device_log_ctx(dev);
const SANE_Option_Descriptor *desc;
log_debug(log, "API: device_get_option_descriptor(): called");
eloop_mutex_lock();
desc = device_get_option_descriptor(dev, option);
eloop_mutex_unlock();
log_debug(log, "API: device_get_option_descriptor(): done");
return desc;
}
/* Write sane_control_option operation to the log
*/
static void
sane_control_option_log (log_ctx *log, const SANE_Option_Descriptor *desc,
SANE_Int option, SANE_Action action, void *value, SANE_Int info)
{
char vbuf[128];
char ibuf[128] = "";
bool get;
switch (action) {
case SANE_ACTION_GET_VALUE:
get = true;
break;
case SANE_ACTION_SET_VALUE:
get = false;
break;
case SANE_ACTION_SET_AUTO:
default:
/* Not supported here */
return;
}
switch (desc->type) {
case SANE_TYPE_BOOL:
strcpy(vbuf, *((SANE_Word*) value) ? "true" : "false");
break;
case SANE_TYPE_INT:
sprintf(vbuf, "%d", *((SANE_Word*) value));
break;
case SANE_TYPE_FIXED:
sprintf(vbuf, "%g", SANE_UNFIX(*((SANE_Fixed*) value)));
break;
case SANE_TYPE_STRING:
snprintf(vbuf, sizeof(vbuf), "\"%s\"", (char*) value);
break;
case SANE_TYPE_BUTTON:
case SANE_TYPE_GROUP:
default:
/* Not supported here */
return;
}
if (action == SANE_ACTION_SET_VALUE && info != 0) {
strcat(ibuf, " info: ");
if ((info & SANE_INFO_INEXACT) != 0) {
strcat(ibuf, "inexact");
info &= ~SANE_INFO_INEXACT;
if (info != 0) {
strcat(ibuf, ", ");
}
}
if ((info & (SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS)) != 0) {
strcat(ibuf, "reload:");
if ((info & SANE_INFO_RELOAD_OPTIONS) != 0) {
strcat(ibuf, " options");
}
if ((info & SANE_INFO_RELOAD_PARAMS) != 0) {
strcat(ibuf, " params");
}
}
}
log_debug(log, "API: %s %s: %s %s",
get ? "get" : "set", option ? desc->name : "(0)", vbuf, ibuf);
}
/* Get or set option value
*/
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option, SANE_Action action,
void *value, SANE_Int *info)
{
SANE_Status status = SANE_STATUS_INVAL;
device *dev = (device*) handle;
const SANE_Option_Descriptor *desc;
log_ctx *log = device_log_ctx(dev);
eloop_mutex_lock();
/* Roughly validate arguments */
if (dev == NULL || value == NULL) {
goto DONE;
}
desc = device_get_option_descriptor(dev, option);
if (desc == NULL) {
goto DONE;
}
if (action == SANE_ACTION_SET_VALUE && !SANE_OPTION_IS_SETTABLE(desc->cap)){
goto DONE;
}
/* Get/set the option */
if (action == SANE_ACTION_GET_VALUE) {
status = device_get_option(dev, option, value);
} else {
status = device_set_option(dev, option, value, info);
}
DONE:
eloop_mutex_unlock();
if (status == SANE_STATUS_GOOD) {
sane_control_option_log(log, desc, option, action, value,
info ? *info : 0);
}
return status;
}
/* Get current scan parameters
*/
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters *params)
{
SANE_Status status = SANE_STATUS_GOOD;
device *dev = (device*) handle;
log_ctx *log = device_log_ctx(dev);
log_debug(log, "API: sane_get_params(): called");
if (params != NULL) {
eloop_mutex_lock();
status = device_get_parameters(dev, params);
eloop_mutex_unlock();
}
log_debug(log, "API: sane_get_params(): done");
return status;
}
/* Start scanning operation
*/
SANE_Status
sane_start (SANE_Handle handle)
{
SANE_Status status;
device *dev = (device*) handle;
log_ctx *log = device_log_ctx(dev);
log_debug(log, "API: sane_start(): called");
eloop_mutex_lock();
status = device_start(dev);
eloop_mutex_unlock();
log_debug(log, "API: sane_start(): %s", sane_strstatus(status));
return status;
}
/* Read scanned image
*/
SANE_Status
sane_read (SANE_Handle handle, SANE_Byte *data, SANE_Int max_len, SANE_Int *len)
{
SANE_Status status;
device *dev = (device*) handle;
log_ctx *log = device_log_ctx(dev);
eloop_mutex_lock();
status = device_read(dev, data, max_len, len);
eloop_mutex_unlock();
/* Note, as a special exception, we don't log every successful
* call of sane_read(), because during loading of image there
* are a lot of them
*/
if (status != SANE_STATUS_GOOD) {
log_debug(log, "API: sane_read(): %s", sane_strstatus(status));
}
return status;
}
/* Cancel scanning operation
*/
void
sane_cancel (SANE_Handle handle)
{
device *dev = handle;
/* Note, no mutex lock here and no logging. We can be called from
* signal handler. device_cancel() properly handles it
*/
device_cancel(dev);
}
/* Set I/O mode
*/
SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
SANE_Status status;
device *dev = (device*) handle;
log_ctx *log = device_log_ctx(dev);
const char *mode = non_blocking ? "true" : "false";
log_debug(log, "API: sane_set_io_mode(%s): called", mode);
eloop_mutex_lock();
status = device_set_io_mode(dev, non_blocking);
eloop_mutex_unlock();
log_debug(log, "API: sane_set_io_mode(%s): %s", mode,
sane_strstatus(status));
return status;
}
/* Get select file descriptor
*/
SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int *fd)
{
SANE_Status status;
device *dev = (device*) handle;
log_ctx *log = device_log_ctx(dev);
log_debug(log, "API: sane_get_select_fd(): called");
eloop_mutex_lock();
status = device_get_select_fd(dev, fd);
eloop_mutex_unlock();
if (status == SANE_STATUS_GOOD) {
log_debug(log, "API: sane_get_select_fd(): fd = %d", *fd);
} else {
log_debug(log, "API: sane_get_select_fd(): %s", sane_strstatus(status));
}
return status;
}
/******************** API aliases for libsane-dll ********************/
SANE_Status __attribute__ ((alias ("sane_init")))
sane_airscan_init (SANE_Int *version_code, SANE_Auth_Callback authorize);
void __attribute__ ((alias ("sane_exit")))
sane_airscan_exit (void);
SANE_Status __attribute__ ((alias ("sane_get_devices")))
sane_airscan_get_devices (const SANE_Device ***device_list, SANE_Bool local_only);
SANE_Status __attribute__ ((alias ("sane_open")))
sane_airscan_open (SANE_String_Const devicename, SANE_Handle *handle);
void __attribute__ ((alias ("sane_close")))
sane_airscan_close (SANE_Handle handle);
const SANE_Option_Descriptor * __attribute__ ((alias ("sane_get_option_descriptor")))
sane_airscan_get_option_descriptor (SANE_Handle handle, SANE_Int option);
SANE_Status __attribute__ ((alias ("sane_control_option")))
sane_airscan_control_option (SANE_Handle handle, SANE_Int option,
SANE_Action action, void *value, SANE_Int *info);
SANE_Status __attribute__ ((alias ("sane_get_parameters")))
sane_airscan_get_parameters (SANE_Handle handle, SANE_Parameters *params);
SANE_Status __attribute__ ((alias ("sane_start")))
sane_airscan_start (SANE_Handle handle);
SANE_Status __attribute__ ((alias ("sane_read")))
sane_airscan_read (SANE_Handle handle, SANE_Byte *data,
SANE_Int max_length, SANE_Int *length);
void __attribute__ ((alias ("sane_cancel")))
sane_airscan_cancel (SANE_Handle handle);
SANE_Status __attribute__ ((alias ("sane_set_io_mode")))
sane_airscan_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking);
SANE_Status __attribute__ ((alias ("sane_get_select_fd")))
sane_airscan_get_select_fd (SANE_Handle handle, SANE_Int * fd);
/* vim:ts=8:sw=4:et
*/