1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4
5 This file is part of the SANE package.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 As a special exception, the authors of SANE give permission for
21 additional uses of the libraries contained in this release of SANE.
22
23 The exception is that, if you link a SANE library with other files
24 to produce an executable, this does not by itself cause the
25 resulting executable to be covered by the GNU General Public
26 License. Your use of that executable is in no way restricted on
27 account of linking the SANE library code into it.
28
29 This exception does not, however, invalidate any other reasons why
30 the executable file might be covered by the GNU General Public
31 License.
32
33 If you submit changes to SANE to the maintainers to be included in
34 a subsequent release, you agree by submitting the changes that
35 those changes may be distributed with this exception intact.
36
37 If you write modifications of your own for SANE, it is your choice
38 whether to permit this exception to apply to your modifications.
39 If you do not wish that, delete this exception notice.
40 */
41
42 #define DEBUG_DECLARE_ONLY
43
44 #include "test_usb_device.h"
45 #include "low.h"
46
47 namespace genesys {
48
TestUsbDevice(std::uint16_t vendor,std::uint16_t product,std::uint16_t bcd_device)49 TestUsbDevice::TestUsbDevice(std::uint16_t vendor, std::uint16_t product,
50 std::uint16_t bcd_device) :
51 vendor_{vendor},
52 product_{product},
53 bcd_device_{bcd_device}
54 {
55 }
56
~TestUsbDevice()57 TestUsbDevice::~TestUsbDevice()
58 {
59 if (is_open()) {
60 DBG(DBG_error, "TestUsbDevice not closed; closing automatically");
61 close();
62 }
63 }
64
open(const char * dev_name)65 void TestUsbDevice::open(const char* dev_name)
66 {
67 DBG_HELPER(dbg);
68
69 if (is_open()) {
70 throw SaneException("device already open");
71 }
72 name_ = dev_name;
73 is_open_ = true;
74 }
75
clear_halt()76 void TestUsbDevice::clear_halt()
77 {
78 DBG_HELPER(dbg);
79 assert_is_open();
80 }
81
reset()82 void TestUsbDevice::reset()
83 {
84 DBG_HELPER(dbg);
85 assert_is_open();
86 }
87
close()88 void TestUsbDevice::close()
89 {
90 DBG_HELPER(dbg);
91 assert_is_open();
92
93 is_open_ = false;
94 name_ = "";
95 }
96
get_vendor_id()97 std::uint16_t TestUsbDevice::get_vendor_id()
98 {
99 DBG_HELPER(dbg);
100 assert_is_open();
101 return vendor_;
102 }
103
get_product_id()104 std::uint16_t TestUsbDevice::get_product_id()
105 {
106 DBG_HELPER(dbg);
107 assert_is_open();
108 return product_;
109 }
110
get_bcd_device()111 std::uint16_t TestUsbDevice::get_bcd_device()
112 {
113 DBG_HELPER(dbg);
114 assert_is_open();
115 return bcd_device_;
116 }
117
control_msg(int rtype,int reg,int value,int index,int length,std::uint8_t * data)118 void TestUsbDevice::control_msg(int rtype, int reg, int value, int index, int length,
119 std::uint8_t* data)
120 {
121 (void) reg;
122 (void) value;
123 (void) index;
124 DBG_HELPER(dbg);
125 assert_is_open();
126 if (rtype == REQUEST_TYPE_IN) {
127 std::memset(data, 0, length);
128 }
129 }
130
bulk_read(std::uint8_t * buffer,std::size_t * size)131 void TestUsbDevice::bulk_read(std::uint8_t* buffer, std::size_t* size)
132 {
133
134 DBG_HELPER(dbg);
135 assert_is_open();
136 std::memset(buffer, 0, *size);
137 }
138
bulk_write(const std::uint8_t * buffer,std::size_t * size)139 void TestUsbDevice::bulk_write(const std::uint8_t* buffer, std::size_t* size)
140 {
141 (void) buffer;
142 (void) size;
143 DBG_HELPER(dbg);
144 assert_is_open();
145 }
146
assert_is_open() const147 void TestUsbDevice::assert_is_open() const
148 {
149 if (!is_open()) {
150 throw SaneException("device not open");
151 }
152 }
153
154 } // namespace genesys
155