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
|
/* ****************************************************************************
* stream/builtin/libusb/read.c -- read from a libusb stream.
* Copyright (C) 2016-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 "libusb.h"
#ifndef LIBCASIO_DISABLED_LIBUSB
/**
* casio_libusb_read:
* Read using libusb cookie.
*
* @arg vcookie the cookie (voided)
* @arg data the data pointer.
* @arg size the data size.
* @return the error code (0 if ok).
*/
# define ENDPOINT_IN (LIBUSB_ENDPOINT_IN | LIBUSB_TRANSFER_TYPE_BULK)
int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie,
unsigned char *dest, size_t size)
{
int libusberr;
size_t tocopy;
/* Transmit what's already in the buffer. */
if (cookie->_start <= cookie->_end) {
tocopy = cookie->_end - cookie->_start + 1;
if (tocopy > size) tocopy = size;
memcpy(dest, &cookie->_buffer[cookie->_start], tocopy);
cookie->_start += tocopy;
dest += tocopy;
size -= tocopy;
}
/* Main receiving loop. */
while (size) {
int recv;
/* Make the transfer. */
libusberr = libusb_bulk_transfer(cookie->_handle, ENDPOINT_IN,
cookie->_buffer, BUFSIZE, &recv, cookie->tmread);
switch (libusberr) {
case 0:
break;
case LIBUSB_ERROR_PIPE:
case LIBUSB_ERROR_NO_DEVICE:
case LIBUSB_ERROR_IO:
msg((ll_error, "The calculator is not here anymore :("));
return (casio_error_nocalc);
case LIBUSB_ERROR_TIMEOUT:
return (casio_error_timeout);
default:
msg((ll_fatal, "libusb error was %d: %s", libusberr,
libusb_strerror(libusberr)));
return (casio_error_unknown);
}
/* Get the current size to copy. */
tocopy = (size_t)recv;
if (tocopy > size) tocopy = size;
/* Copy to destination. */
memcpy(dest, cookie->_buffer, tocopy);
dest += tocopy;
size -= tocopy;
/* Correct start and end points. */
cookie->_start = tocopy;
cookie->_end = (size_t)recv - 1;
}
return (0);
}
#endif
|