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
21 #define DEBUG_DECLARE_ONLY
22
23 #include "test_usb_device.h"
24 #include "low.h"
25
26 namespace genesys {
27
TestUsbDevice(std::uint16_t vendor, std::uint16_t product, std::uint16_t bcd_device)28 TestUsbDevice::TestUsbDevice(std::uint16_t vendor, std::uint16_t product,
29 std::uint16_t bcd_device) :
30 vendor_{vendor},
31 product_{product},
32 bcd_device_{bcd_device}
33 {
34 }
35
~TestUsbDevice()36 TestUsbDevice::~TestUsbDevice()
37 {
38 if (is_open()) {
39 DBG(DBG_error, "TestUsbDevice not closed; closing automatically");
40 close();
41 }
42 }
43
open(const char* dev_name)44 void TestUsbDevice::open(const char* dev_name)
45 {
46 DBG_HELPER(dbg);
47
48 if (is_open()) {
49 throw SaneException("device already open");
50 }
51 name_ = dev_name;
52 is_open_ = true;
53 }
54
clear_halt()55 void TestUsbDevice::clear_halt()
56 {
57 DBG_HELPER(dbg);
58 assert_is_open();
59 }
60
reset()61 void TestUsbDevice::reset()
62 {
63 DBG_HELPER(dbg);
64 assert_is_open();
65 }
66
close()67 void TestUsbDevice::close()
68 {
69 DBG_HELPER(dbg);
70 assert_is_open();
71
72 is_open_ = false;
73 name_ = "";
74 }
75
get_vendor_id()76 std::uint16_t TestUsbDevice::get_vendor_id()
77 {
78 DBG_HELPER(dbg);
79 assert_is_open();
80 return vendor_;
81 }
82
get_product_id()83 std::uint16_t TestUsbDevice::get_product_id()
84 {
85 DBG_HELPER(dbg);
86 assert_is_open();
87 return product_;
88 }
89
get_bcd_device()90 std::uint16_t TestUsbDevice::get_bcd_device()
91 {
92 DBG_HELPER(dbg);
93 assert_is_open();
94 return bcd_device_;
95 }
96
control_msg(int rtype, int reg, int value, int index, int length, std::uint8_t* data)97 void TestUsbDevice::control_msg(int rtype, int reg, int value, int index, int length,
98 std::uint8_t* data)
99 {
100 (void) reg;
101 (void) value;
102 (void) index;
103 DBG_HELPER(dbg);
104 assert_is_open();
105 if (rtype == REQUEST_TYPE_IN) {
106 std::memset(data, 0, length);
107 }
108 }
109
bulk_read(std::uint8_t* buffer, std::size_t* size)110 void TestUsbDevice::bulk_read(std::uint8_t* buffer, std::size_t* size)
111 {
112
113 DBG_HELPER(dbg);
114 assert_is_open();
115 std::memset(buffer, 0, *size);
116 }
117
bulk_write(const std::uint8_t* buffer, std::size_t* size)118 void TestUsbDevice::bulk_write(const std::uint8_t* buffer, std::size_t* size)
119 {
120 (void) buffer;
121 (void) size;
122 DBG_HELPER(dbg);
123 assert_is_open();
124 }
125
assert_is_open() const126 void TestUsbDevice::assert_is_open() const
127 {
128 if (!is_open()) {
129 throw SaneException("device not open");
130 }
131 }
132
133 } // namespace genesys
134