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
|
/* sane-airscan device capabilities parser test
*
* Copyright (C) 2019 and up by Alexander Pevzner (pzz@apevzner.com)
* See LICENSE for license terms and conditions
*/
#include "airscan.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Print error message and exit
*/
void __attribute__((noreturn))
die (const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
printf("\n");
va_end(ap);
exit(1);
}
/* The main function
*/
int
main (int argc, char **argv)
{
char *mode, *file;
FILE *fp;
void *data;
long size;
proto_handler *proto;
int rc;
error err;
devcaps caps;
/* Parse command-line arguments */
if (argc != 3) {
die(
"test-devcaps - decode and print device capabilities\n"
"usage: %s [-escl|-wsd] file.xml", argv[0]
);
}
mode = argv[1];
file = argv[2];
if (!strcmp(mode, "-escl")) {
proto = proto_handler_escl_new();
} else if (!strcmp(mode, "-wsd")) {
proto = proto_handler_wsd_new();
} else {
die("%s: unknown protocol", mode);
}
/* Load the file */
fp = fopen(file, "rb");
if (fp == NULL) {
die("%s: %s", file, strerror(errno));
}
rc = fseek(fp, 0, SEEK_END);
if (rc < 0) {
die("%s: %s", file, strerror(errno));
}
size = ftell(fp);
if (size < 0) {
die("%s: %s", file, strerror(errno));
}
rc = fseek(fp, 0, SEEK_SET);
if (rc < 0) {
die("%s: %s", file, strerror(errno));
}
data = mem_new(char, size);
if ((size_t) size != fread(data, 1, size, fp)) {
die("%s: read error", file);
}
fclose(fp);
/* Initialize logging */
conf.dbg_enabled = true;
log_init();
log_configure();
/* Decode device capabilities */
memset(&caps, 0, sizeof(caps));
devcaps_init(&caps);
err = proto->test_decode_devcaps(proto, data, size, &caps);
if (err != NULL) {
die("error: %s", ESTRING(err));
}
devcaps_dump(NULL, &caps, false);
/* Cleanup and exit */
proto_handler_free(proto);
mem_free(data);
return 0;
}
/* vim:ts=8:sw=4:et
*/
|