aboutsummaryrefslogtreecommitdiff
path: root/lib/stream/open/usb.c
blob: f9089dc060188558a6566aaf104304d7c3a61403 (plain)
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
/* ****************************************************************************
 * stream/open/usb.c -- open a USB stream.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcasio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libcasio; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include "../stream.h"
#define NUM 5

/* Correspondance type. */
struct corresp {
	int                    _valid;
	casio_openusbstream_t *_openusb;
};

/* List of streams. */
CASIO_LOCAL struct corresp openusbs[NUM + 1] = {
#if   !defined(LIBCASIO_DISABLED_LIBUSB)
	{1, casio_openusb_libusb},
#endif
#if   !defined(LIBCASIO_DISABLED_WINDOWS)
	{1, casio_openusb_windows},
#endif
	{0, NULL}
};

/**
 *	casio_add_default_usb_stream:
 *	Add a USB stream opener.
 *
 *	@arg	function	the function pointer to add.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_add_default_usb_stream(casio_openusbstream_t *function)
{
	struct corresp *c; int num;

	for (c = openusbs, num = NUM; c->_valid && num; c++, num--);
	if (!num)
		return (casio_error_op);

	c->_valid = 1;
	c->_openusb = function;
	return (0);
}

/**
 *	casio_open_usb_stream:
 *	Open the USB stream.
 *
 *	@arg	stream		the stream to make.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_open_usb_stream(casio_stream_t **stream)
{
	int err; struct corresp *c = openusbs;

	if (!c->_valid)
		return (casio_error_op);
	for (; c->_valid; c++) {
		err = (*c->_openusb)(stream);

		if (!err)
			return (0);
		if (err != casio_error_nocalc)
			return (err);
	}

	return (casio_error_nocalc);
}