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 #ifndef BACKEND_GENESYS_USB_DEVICE_H 43 #define BACKEND_GENESYS_USB_DEVICE_H 44 45 #include "error.h" 46 #include "../include/sane/sanei_usb.h" 47 48 #include <cstdint> 49 #include <string> 50 51 namespace genesys { 52 53 class IUsbDevice { 54 public: 55 IUsbDevice() = default; 56 57 IUsbDevice(const IUsbDevice& other) = delete; 58 IUsbDevice& operator=(const IUsbDevice&) = delete; 59 60 virtual ~IUsbDevice(); 61 62 virtual bool is_open() const = 0; 63 64 virtual const std::string& name() const = 0; 65 66 virtual void open(const char* dev_name) = 0; 67 68 virtual void clear_halt() = 0; 69 virtual void reset() = 0; 70 virtual void close() = 0; 71 72 virtual std::uint16_t get_vendor_id() = 0; 73 virtual std::uint16_t get_product_id() = 0; 74 virtual std::uint16_t get_bcd_device() = 0; 75 76 virtual void control_msg(int rtype, int reg, int value, int index, int length, 77 std::uint8_t* data) = 0; 78 virtual void bulk_read(std::uint8_t* buffer, std::size_t* size) = 0; 79 virtual void bulk_write(const std::uint8_t* buffer, std::size_t* size) = 0; 80 81 }; 82 83 class UsbDevice : public IUsbDevice { 84 public: 85 UsbDevice() = default; 86 87 ~UsbDevice() override; 88 is_open()89 bool is_open() const override { return is_open_; } 90 name()91 const std::string& name() const override { return name_; } 92 93 void open(const char* dev_name) override; 94 95 void clear_halt() override; 96 void reset() override; 97 void close() override; 98 99 std::uint16_t get_vendor_id() override; 100 std::uint16_t get_product_id() override; 101 std::uint16_t get_bcd_device() override; 102 103 void control_msg(int rtype, int reg, int value, int index, int length, 104 std::uint8_t* data) override; 105 void bulk_read(std::uint8_t* buffer, std::size_t* size) override; 106 void bulk_write(const std::uint8_t* buffer, std::size_t* size) override; 107 108 private: 109 110 void assert_is_open() const; 111 void set_not_open(); 112 113 std::string name_; 114 bool is_open_ = false; 115 int device_num_ = 0; 116 }; 117 118 } // namespace genesys 119 120 #endif // BACKEND_GENESYS_USB_DEVICE_H 121