src/HID.h
branchv_0
changeset 5 6799cec5c2f8
parent 4 405aa9de65d2
child 6 975f38eb1e12
equal deleted inserted replaced
4:405aa9de65d2 5:6799cec5c2f8
       
     1 #pragma once
       
     2 
       
     3 #include <array>
       
     4 #include <vector>
       
     5 #include <memory>
       
     6 #include <unistd.h>
       
     7 #include <cassert>
       
     8 
       
     9 #include <hidapi/hidapi.h>
       
    10 
       
    11 using Packet = std::vector<uint8_t>;
       
    12 static_assert(sizeof (uint8_t) == sizeof (unsigned char)); // unsigned char is used in the HID API library
       
    13 
       
    14 class HIDException {
       
    15 private:
       
    16 	std::wstring message;
       
    17 public:
       
    18 
       
    19 	HIDException(std::wstring message) : message(message) {
       
    20 	}
       
    21 
       
    22 	virtual ~HIDException() {
       
    23 	}
       
    24 
       
    25 	const std::wstring getMessage() const {
       
    26 		return message;
       
    27 	}
       
    28 
       
    29 };
       
    30 
       
    31 class HIDDevice {
       
    32 private:
       
    33 	std::shared_ptr<hid_device> handle;
       
    34 
       
    35 public:
       
    36 
       
    37 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
       
    38 		int initError = hid_init(); // TODO: move to HIDContext class?
       
    39 		if (initError) throw HIDException(L"Unable to init HID API.");
       
    40 		handle.reset(hid_open(vendorId, productId, serialNumber), hid_close);
       
    41 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is the device present?");
       
    42 	}
       
    43 
       
    44 	virtual ~HIDDevice() {
       
    45 		hid_exit(); // TODO: move to HIDContext class?
       
    46 	}
       
    47 
       
    48 	const std::wstring getManufacturerName() const {
       
    49 		std::array<wchar_t, 200 > buffer;
       
    50 		int error = hid_get_manufacturer_string(handle.get(), buffer.data(), buffer.size());
       
    51 		if (error) throw HIDException(L"Unable to get manufacturer name.");
       
    52 		return buffer.data();
       
    53 	}
       
    54 
       
    55 	const std::wstring getProductName() const {
       
    56 		std::array<wchar_t, 200 > buffer;
       
    57 		int error = hid_get_product_string(handle.get(), buffer.data(), buffer.size());
       
    58 		if (error) throw HIDException(L"Unable to get product name.");
       
    59 		return buffer.data();
       
    60 	}
       
    61 
       
    62 	const std::wstring getSerialNumber() const {
       
    63 		std::array<wchar_t, 200 > buffer;
       
    64 		int error = hid_get_serial_number_string(handle.get(), buffer.data(), buffer.size());
       
    65 		if (error) throw HIDException(L"Unable to get serial number.");
       
    66 		return buffer.data();
       
    67 	}
       
    68 
       
    69 	void sendFeatureReport(Packet data) {
       
    70 		int written = hid_send_feature_report(handle.get(), data.data(), data.size());
       
    71 		if (written < 0) throw HIDException(L"Unable to send feature report.");
       
    72 	}
       
    73 
       
    74 };