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#ifndef BACKEND_GENESYS_USB_DEVICE_H
22#define BACKEND_GENESYS_USB_DEVICE_H
23
24#include "error.h"
25#include "../include/sane/sanei_usb.h"
26
27#include <cstdint>
28#include <string>
29
30namespace genesys {
31
32class IUsbDevice {
33public:
34    IUsbDevice() = default;
35
36    IUsbDevice(const IUsbDevice& other) = delete;
37    IUsbDevice& operator=(const IUsbDevice&) = delete;
38
39    virtual ~IUsbDevice();
40
41    virtual bool is_open() const = 0;
42
43    virtual const std::string& name() const = 0;
44
45    virtual void open(const char* dev_name) = 0;
46
47    virtual void clear_halt() = 0;
48    virtual void reset() = 0;
49    virtual void close() = 0;
50
51    virtual std::uint16_t get_vendor_id() = 0;
52    virtual std::uint16_t get_product_id() = 0;
53    virtual std::uint16_t get_bcd_device() = 0;
54
55    virtual void control_msg(int rtype, int reg, int value, int index, int length,
56                             std::uint8_t* data) = 0;
57    virtual void bulk_read(std::uint8_t* buffer, std::size_t* size) = 0;
58    virtual void bulk_write(const std::uint8_t* buffer, std::size_t* size) = 0;
59
60};
61
62class UsbDevice : public IUsbDevice {
63public:
64    UsbDevice() = default;
65
66    ~UsbDevice() override;
67
68    bool is_open() const override { return is_open_; }
69
70    const std::string& name() const override { return name_; }
71
72    void open(const char* dev_name) override;
73
74    void clear_halt() override;
75    void reset() override;
76    void close() override;
77
78    std::uint16_t get_vendor_id() override;
79    std::uint16_t get_product_id() override;
80    std::uint16_t get_bcd_device() override;
81
82    void control_msg(int rtype, int reg, int value, int index, int length,
83                     std::uint8_t* data) override;
84    void bulk_read(std::uint8_t* buffer, std::size_t* size) override;
85    void bulk_write(const std::uint8_t* buffer, std::size_t* size) override;
86
87private:
88
89    void assert_is_open() const;
90    void set_not_open();
91
92    std::string name_;
93    bool is_open_ = false;
94    int device_num_ = 0;
95};
96
97} // namespace genesys
98
99#endif // BACKEND_GENESYS_USB_DEVICE_H
100