Skip to content

Commit

Permalink
test-devcaps: stub implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpevzner committed May 29, 2024
1 parent 929bfa8 commit 463127f
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ $(OBJDIR)%.o: %.c Makefile airscan.h

.PHONY: all clean install man

all: tags $(BACKEND) $(DISCOVER) test test-decode test-multipart test-zeroconf test-uri
all: tags $(BACKEND) $(DISCOVER) test test-decode test-devcaps test-multipart test-zeroconf test-uri

tags: $(SRC) airscan.h test.c test-decode.c test-multipart.c test-zeroconf.c test-uri.c
tags: $(SRC) airscan.h test.c test-decode.c test-devcaps.c test-multipart.c test-zeroconf.c test-uri.c
-ctags -R .

$(BACKEND): $(OBJDIR)airscan.o $(LIBAIRSCAN) airscan.sym
Expand Down Expand Up @@ -132,7 +132,7 @@ install: all
[ "$(COMPRESS)" = "" ] || $(COMPRESS) -f $(DESTDIR)/$(mandir)/man5/$(MAN_BACKEND)

clean:
rm -f test test-decode test-multipart test-zeroconf test-uri $(BACKEND) tags
rm -f test test-decode test-devcaps test-multipart test-zeroconf test-uri $(BACKEND) tags
rm -rf $(OBJDIR)

uninstall:
Expand Down Expand Up @@ -160,6 +160,9 @@ test: $(BACKEND) test.c
test-decode: test-decode.c $(LIBAIRSCAN)
$(CC) -o test-decode test-decode.c $(CPPFLAGS) $(common_CFLAGS) $(LIBAIRSCAN) $(tests_LDFLAGS)

test-devcaps: test-devcaps.c $(LIBAIRSCAN)
$(CC) -o test-devcaps test-devcaps.c $(CPPFLAGS) $(common_CFLAGS) $(LIBAIRSCAN) $(tests_LDFLAGS)

test-multipart: test-multipart.c $(LIBAIRSCAN)
$(CC) -o test-multipart test-multipart.c $(CPPFLAGS) $(common_CFLAGS) $(LIBAIRSCAN) $(tests_LDFLAGS)

Expand Down
18 changes: 18 additions & 0 deletions airscan-escl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,22 @@ escl_cancel_query (const proto_ctx *ctx)
return escl_http_query(ctx, ctx->location, "DELETE", NULL);
}

/******************** Test interfaces ********************/
/* Test interface: decode device capabilities
*/
static error
escl_test_decode_devcaps (proto_handler *proto,
const void *xml_text, size_t xms_size,
devcaps *caps)
{
(void) proto;
(void) xml_text;
(void) xms_size;
(void) caps;

return ERROR("not implemented");
}

/******************** Constructor/destructor ********************/
/* Free ESCL protocol handler
*/
Expand Down Expand Up @@ -1195,6 +1211,8 @@ proto_handler_escl_new (void)
escl->proto.cleanup_query = escl_cancel_query;
escl->proto.cancel_query = escl_cancel_query;

escl->proto.test_decode_devcaps = escl_test_decode_devcaps;

return &escl->proto;
}

Expand Down
17 changes: 17 additions & 0 deletions airscan-wsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,21 @@ wsd_cancel_query (const proto_ctx *ctx)
return wsd_http_post(ctx, xml_wr_finish_compact(xml));
}

/* Test interface: decode device capabilities
*/
static error
wsd_test_decode_devcaps (proto_handler *proto,
const void *xml_text, size_t xms_size,
devcaps *caps)
{
(void) proto;
(void) xml_text;
(void) xms_size;
(void) caps;

return ERROR("not implemented");
}

/* proto_handler_wsd_new creates new WSD protocol handler
*/
proto_handler*
Expand Down Expand Up @@ -1184,6 +1199,8 @@ proto_handler_wsd_new (void)

wsd->proto.cancel_query = wsd_cancel_query;

wsd->proto.test_decode_devcaps = wsd_test_decode_devcaps;

return &wsd->proto;
}

Expand Down
16 changes: 16 additions & 0 deletions airscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -3326,6 +3326,12 @@ struct proto_handler {
/* Cancel scan in progress
*/
http_query* (*cancel_query) (const proto_ctx *ctx);

/* Test interfaces. Not for regular use!
*/
error (*test_decode_devcaps) (proto_handler *proto,
const void *xml_text, size_t xms_size,
devcaps *caps);
};

/* proto_handler_escl_new creates new eSCL protocol handler
Expand Down Expand Up @@ -3353,6 +3359,16 @@ proto_handler_new (ID_PROTO proto)
}
}

/* proto_handler_free destroys protocol handler, previously
* created by proto_handler_new/proto_handler_escl_new/
* proto_handler_wsd_new functions
*/
static inline void
proto_handler_free (proto_handler *proto)
{
proto->free(proto);
}

/******************** Image decoding ********************/
/* The window withing the image
*
Expand Down
108 changes: 108 additions & 0 deletions test-devcaps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* sane-airscan device capabilities parser test
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* 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);

/* 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));
}

/* Cleanup and exit */
proto_handler_free(proto);
free(data);

return 0;
}

/* vim:ts=8:sw=4:et
*/

0 comments on commit 463127f

Please sign in to comment.