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
|
/*
* HID++ 1.0 protocol details.
*
* Copyright (C) 2014 Peter Wu <peter@lekensteyn.nl>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <string.h>
#include "hidpp10.h"
#include "hidpp.h"
#define READ_TIMEOUT 2000
static bool hidpp10_msg_filter(HidppMessage *msg, void *userdata)
{
bool ok;
struct {
hidpp_msg_cb_t *cb;
void *userdata;
HidppMessage *msg_out;
} *cd = userdata;
const HidppMessage *out = cd->msg_out;
const uint8_t out_addr = out->address;
ok = msg->device_index == out->device_index;
/* accept error responses matching the request, return as there are no
* notifications to check for. */
if (ok && msg->report_id == HIDPP_SHORT && msg->sub_id == SUB_ERROR_MSG
&& msg->sub_id == out->sub_id && msg->params[0] == out_addr) {
return true;
}
ok = ok && msg->sub_id == out->sub_id && msg->address == out_addr;
switch (out->report_id) {
case SUB_SET_REGISTER:
case SUB_GET_REGISTER:
case SUB_SET_LONG_REGISTER:
return ok && msg->report_id == HIDPP_SHORT;
case SUB_GET_LONG_REGISTER:
return ok && msg->report_id == HIDPP_LONG;
case NOTIF_DEV_DISCONNECT:
case NOTIF_DEV_CONNECT:
case NOTIF_RECV_LOCK_CHANGE:
if (cd->cb) {
/* notify of ... well... notifications */
cd->cb(msg, cd->userdata);
}
/* fall-through */
default:
/* no idea whether the report IDs must match or not */
return ok;
}
}
int hidpp10_request(int fd, HidppMessage *msg, hidpp_msg_cb_t *cb, void *userdata)
{
HidppMessage msg_in;
struct {
hidpp_msg_cb_t *cb;
void *userdata;
HidppMessage *msg_out;
} cd = { cb, userdata, msg };
if (!hidpp_write_report(fd, msg))
return -1;
/* find the HID++ response, passing notifications if needed */
if (!hidpp_read_msg(fd, READ_TIMEOUT, &msg_in, hidpp10_msg_filter, &cd))
return -1;
memcpy(msg, &msg_in, sizeof(msg_in));
return msg_in.sub_id == SUB_ERROR_MSG ? msg_in.params[1] : 0;
}
int hidpp10_enable_wireless_notifications(int fd, bool enabled)
{
int r;
HidppMessage msg = {
.report_id = HIDPP_SHORT,
.device_index = 0xFF,
.sub_id = SUB_GET_REGISTER,
.address = REG_ENABLED_NOTIFS
};
/* get notifications state */
r = hidpp10_request(fd, &msg, NULL, NULL);
if (r == 0 && !(msg.params[1] & 1) != !enabled) {
msg.params[1] |= enabled ? 1 : 0;
r = hidpp10_request(fd, &msg, NULL, NULL);
}
return r;
}
|