src/HID.h
branchv_0
changeset 8 465572518625
parent 7 148f8dd077e8
child 9 d0fc5f337ea2
equal deleted inserted replaced
7:148f8dd077e8 8:465572518625
     1 /**
       
     2  * cadMousePro
       
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <array>
       
    21 #include <vector>
       
    22 #include <memory>
       
    23 #include <unistd.h>
       
    24 #include <cassert>
       
    25 
       
    26 #include <hidapi/hidapi.h>
       
    27 
       
    28 using Packet = std::vector<uint8_t>;
       
    29 static_assert(sizeof (uint8_t) == sizeof (unsigned char)); // unsigned char is used in the HID API library
       
    30 
       
    31 class HIDException {
       
    32 private:
       
    33 	std::wstring message;
       
    34 public:
       
    35 
       
    36 	HIDException(std::wstring message) : message(message) {
       
    37 	}
       
    38 
       
    39 	virtual ~HIDException() {
       
    40 	}
       
    41 
       
    42 	const std::wstring getMessage() const {
       
    43 		return message;
       
    44 	}
       
    45 
       
    46 };
       
    47 
       
    48 class HIDDevice {
       
    49 private:
       
    50 	std::shared_ptr<hid_device> handle;
       
    51 
       
    52 public:
       
    53 
       
    54 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
       
    55 		int initError = hid_init(); // TODO: move to HIDContext class?
       
    56 		if (initError) throw HIDException(L"Unable to init HID API.");
       
    57 		handle.reset(hid_open(vendorId, productId, serialNumber), hid_close);
       
    58 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is the device present?");
       
    59 	}
       
    60 
       
    61 	virtual ~HIDDevice() {
       
    62 		hid_exit(); // TODO: move to HIDContext class?
       
    63 	}
       
    64 
       
    65 	const std::wstring getManufacturerName() const {
       
    66 		std::array<wchar_t, 200 > buffer;
       
    67 		int error = hid_get_manufacturer_string(handle.get(), buffer.data(), buffer.size());
       
    68 		if (error) throw HIDException(L"Unable to get manufacturer name.");
       
    69 		return buffer.data();
       
    70 	}
       
    71 
       
    72 	const std::wstring getProductName() const {
       
    73 		std::array<wchar_t, 200 > buffer;
       
    74 		int error = hid_get_product_string(handle.get(), buffer.data(), buffer.size());
       
    75 		if (error) throw HIDException(L"Unable to get product name.");
       
    76 		return buffer.data();
       
    77 	}
       
    78 
       
    79 	const std::wstring getSerialNumber() const {
       
    80 		std::array<wchar_t, 200 > buffer;
       
    81 		int error = hid_get_serial_number_string(handle.get(), buffer.data(), buffer.size());
       
    82 		if (error) throw HIDException(L"Unable to get serial number.");
       
    83 		return buffer.data();
       
    84 	}
       
    85 
       
    86 	void sendFeatureReport(Packet data) {
       
    87 		int written = hid_send_feature_report(handle.get(), data.data(), data.size());
       
    88 		if (written < 0) throw HIDException(L"Unable to send feature report.");
       
    89 	}
       
    90 
       
    91 };