File: libhidapi.cpp

package info (click to toggle)
concordance 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,700 kB
  • sloc: cpp: 7,203; sh: 4,621; ansic: 882; python: 707; perl: 171; makefile: 132; xml: 34
file content (156 lines) | stat: -rw-r--r-- 4,277 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * vim:tw=80:ai:tabstop=4:softtabstop=4:shiftwidth=4:expandtab
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (C) Copyright Kevin Timmerman 2007
 * (C) Copyright Phil Dibowitz 2007
 */

#include "lc_internal.h"
#include "libconcord.h"

#ifdef WANT_HIDAPI
#ifndef LC_LIBHIDAPI
#define LC_LIBHIDAPI

#include "hid.h"
#include <hidapi/hidapi.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

/*
 * Harmonies either fall under logitech's VendorID (0x046d), and logitech's
 * productID range for Harmonies (0xc110 - 0xc14f)...
 *
 * OR, they fall under 0x400/0xc359 (older 7-series, all 6-series).
 */
#define LOGITECH_VID 0x046D
#define LOGITECH_MIN_PID 0xc110
#define LOGITECH_MAX_PID 0xc14f
#define NATIONAL_VID 0x0400
#define NATIONAL_PID 0xc359

#define USB_PACKET_LENGTH 64

hid_device *h_dev;

int InitUSB()
{
    hid_init();
    /*
     * Note we do NOT call hid_exit() in ShutdownUSB, because you can
     * never reinitialize libhidapi - and on a reset, we need
     * to come back and do more stuff. So we set it up as an atexit()
     */
    atexit((void(*)())hid_exit);
    return 0;
}

void ShutdownUSB()
{
    if (h_dev) {
        hid_close(h_dev);
    }
}

bool is_harmony(struct hid_device_info *dev)
{
    /* IF vendor == logitech AND product is in range of harmony
     *   OR vendor == National Semiconductor and product is harmony
     */
    if ((dev->vendor_id == LOGITECH_VID
         && (dev->product_id >= LOGITECH_MIN_PID
             && dev->product_id <= LOGITECH_MAX_PID))
         || (dev->vendor_id == NATIONAL_VID
             && dev->product_id == NATIONAL_PID)) {
        return true;
    }
    return false;
}

/*
 * Find a HID device that is a Harmony
 */
int FindRemote(THIDINFO &hid_info)
{
    struct hid_device_info *devs, *cur_dev;
    bool found = false;
    devs = hid_enumerate(0x0, 0x0);
    cur_dev = devs;
    while (cur_dev) {
        debug("Testing: %04X, %04X", cur_dev->vendor_id, cur_dev->product_id);
        if (is_harmony(cur_dev)) {
            debug("Found a Harmony!");
            hid_info.vid = cur_dev->vendor_id;
            hid_info.pid = cur_dev->product_id;
            hid_info.ver = cur_dev->release_number;
            h_dev = hid_open(cur_dev->vendor_id, cur_dev->product_id, NULL);
            found = true;
            break;
        }
        cur_dev = cur_dev->next;
    }
    hid_free_enumeration(devs);
    if (!found || !h_dev) {
        debug("Failed to establish communication with remote");
        return LC_ERROR_CONNECT;
    }

    // Fill in hid_info
    const size_t buf_len = 128;
    wchar_t wide_s[buf_len];
    char s[buf_len];
    hid_get_manufacturer_string(h_dev, wide_s, buf_len);
    wcstombs(s, wide_s, buf_len);
    hid_info.mfg = s;
    hid_get_product_string(h_dev, wide_s, buf_len);
    wcstombs(s, wide_s, buf_len);
    hid_info.prod = s;

    return 0;
}

int HID_WriteReport(const uint8_t *data)
{
    uint8_t newdata[USB_PACKET_LENGTH+1];
    newdata[0] = 0x00;
    memcpy(&newdata[1], data, USB_PACKET_LENGTH);
    int err = hid_write(h_dev, newdata, USB_PACKET_LENGTH + 1);
    if (err < 0) {
        debug("Failed to write to device: %d (%ls)", err, hid_error(h_dev));
        return err;
    }

    return 0;
}

int HID_ReadReport(uint8_t *data, unsigned int timeout)
{
    int err = hid_read_timeout(h_dev, data, USB_PACKET_LENGTH, timeout);
    if (err < 0) {
        debug("Failed to read from device: %d (%ls)", err, hid_error(h_dev));
        return err;
    } else if (err == 0) {
        debug("USB read timed out");
        return 1;
    }

    return 0;
}

#endif
#endif