| 1 | // Copyright (c) 2017-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | |
| 30 | #if defined(HAVE_HIDAPI) |
| 31 | |
| 32 | #include <boost/optional/optional.hpp> |
| 33 | #include <hidapi/hidapi.h> |
| 34 | #include "device_io.hpp" |
| 35 | |
| 36 | #pragma once |
| 37 | |
| 38 | namespace hw { |
| 39 | namespace io { |
| 40 | |
| 41 | |
| 42 | |
| 43 | /** HID class base. Commands are formated as follow: |
| 44 | * |
| 45 | * |----------------------------------------------------------| |
| 46 | * | 2 bytes | 1 byte | 2 bytes | 2 bytes | len bytes | |
| 47 | * |-----------|----------|-----------|----------|------------| |
| 48 | * | channel | tag | sequence | len | payload | |
| 49 | * |----------------------------------------------------------| |
| 50 | */ |
| 51 | |
| 52 | |
| 53 | struct hid_conn_params { |
| 54 | unsigned int vid; |
| 55 | unsigned int pid; |
| 56 | int interface_number; |
| 57 | unsigned short usage_page; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | class device_io_hid: device_io { |
| 62 | |
| 63 | |
| 64 | private: |
| 65 | |
| 66 | |
| 67 | unsigned short channel; |
| 68 | unsigned char tag; |
| 69 | unsigned int packet_size; |
| 70 | unsigned int timeout; |
| 71 | |
| 72 | unsigned int usb_vid; |
| 73 | unsigned int usb_pid; |
| 74 | hid_device *usb_device; |
| 75 | |
| 76 | void io_hid_log(int read, unsigned char* buf, int buf_len); |
| 77 | void io_hid_init(); |
| 78 | void io_hid_exit() ; |
| 79 | void io_hid_open(int vid, int pid, int mode); |
| 80 | void io_hid_close (void); |
| 81 | |
| 82 | unsigned int wrapCommand(const unsigned char *command, size_t command_len, unsigned char *out, size_t out_len); |
| 83 | unsigned int unwrapReponse(const unsigned char *data, size_t data_len, unsigned char *out, size_t out_len); |
| 84 | |
| 85 | hid_device_info *find_device(hid_device_info *devices_list, boost::optional<int> interface_number, boost::optional<unsigned short> usage_page); |
| 86 | |
| 87 | public: |
| 88 | bool hid_verbose = false; |
| 89 | |
| 90 | static const unsigned short DEFAULT_CHANNEL = 0x0001; |
| 91 | static const unsigned char DEFAULT_TAG = 0x01; |
| 92 | static const unsigned int DEFAULT_PACKET_SIZE = 64; |
| 93 | static const unsigned int DEFAULT_TIMEOUT = 120000; |
| 94 | |
| 95 | device_io_hid(unsigned short channel, unsigned char tag, unsigned int packet_zize, unsigned int timeout); |
| 96 | device_io_hid(); |
| 97 | ~device_io_hid() {}; |
| 98 | |
| 99 | void init(); |
| 100 | void connect(void *params); |
| 101 | void connect(const std::vector<hid_conn_params> &conn); |
| 102 | hid_device *connect(unsigned int vid, unsigned int pid, boost::optional<int> interface_number, boost::optional<unsigned short> usage_page); |
| 103 | bool connected() const; |
| 104 | int exchange(unsigned char *command, unsigned int cmd_len, unsigned char *response, unsigned int max_resp_len, bool user_input); |
| 105 | void disconnect(); |
| 106 | void release(); |
| 107 | }; |
| 108 | }; |
| 109 | }; |
| 110 | |
| 111 | #endif //#if defined(HAVE_HIDAPI) |