-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathbluetooth_hid.cpp
More file actions
174 lines (140 loc) · 5.79 KB
/
bluetooth_hid.cpp
File metadata and controls
174 lines (140 loc) · 5.79 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2020-2021 ndeadly
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 "bluetooth_hid.hpp"
#include "../btdrv_mitm_flags.hpp"
#include "../../controllers/controller_management.hpp"
#include <mutex>
#include <cstring>
namespace ams::bluetooth::hid {
namespace {
os::Mutex g_event_info_lock(false);
bluetooth::HidEventInfo g_event_info;
bluetooth::HidEventType g_current_event_type;
os::SystemEvent g_system_event;
os::SystemEvent g_system_event_fwd(os::EventClearMode_AutoClear, true);
os::SystemEvent g_system_event_user_fwd(os::EventClearMode_AutoClear, true);
os::Event g_init_event(os::EventClearMode_ManualClear);
os::Event g_data_read_event(os::EventClearMode_AutoClear);
}
bool IsInitialized() {
return g_init_event.TryWait();
}
void SignalInitialized(void) {
g_init_event.Signal();
}
void WaitInitialized(void) {
g_init_event.Wait();
}
os::SystemEvent *GetSystemEvent(void) {
return &g_system_event;
}
os::SystemEvent *GetForwardEvent(void) {
return &g_system_event_fwd;
}
os::SystemEvent *GetUserForwardEvent(void) {
return &g_system_event_user_fwd;
}
void SignalFakeEvent(bluetooth::HidEventType type, const void *data, size_t size) {
g_current_event_type = type;
std::memcpy(&g_event_info, data, size);
g_system_event_fwd.Signal();
}
Result VirtualReconnect(const bluetooth::Address *address) {
if (hos::GetVersion() >= hos::Version_12_0_0) {
struct ConnectionState {
BtdrvHidConnectionStatus status;
BtdrvAddress address;
};
// Signal fake disconnection event
ConnectionState disconnected = { BtdrvHidConnectionStatus_Closed, *address };
SignalFakeEvent(BtdrvHidEventType_Connection, &disconnected, sizeof(disconnected));
g_data_read_event.Wait();
// If we don't wait a bit the console disconnects the controller for real
svcSleepThread(100'000'000ULL);
// Signal fake connection event
ConnectionState connected = { BtdrvHidConnectionStatus_Opened, *address };
SignalFakeEvent(BtdrvHidEventType_Connection, &connected, sizeof(connected));
g_data_read_event.Wait();
}
else {
struct ConnectionState {
BtdrvAddress address;
uint8_t pad[2];
BtdrvHidConnectionStatus status;
};
// Signal fake disconnection event
ConnectionState disconnected = { *address, {0}, BtdrvHidConnectionStatusOld_Closed };
SignalFakeEvent(BtdrvHidEventTypeOld_Connection, &disconnected, sizeof(disconnected));
g_data_read_event.Wait();
// If we don't wait a bit the console disconnects the controller for real
svcSleepThread(100'000'000ULL);
// Signal fake connection event
ConnectionState connected = { *address, {0}, BtdrvHidConnectionStatusOld_Opened };
SignalFakeEvent(BtdrvHidEventTypeOld_Connection, &connected, sizeof(connected));
g_data_read_event.Wait();
}
return ams::ResultSuccess();
}
Result GetEventInfo(bluetooth::HidEventType *type, void *buffer, size_t size) {
std::scoped_lock lk(g_event_info_lock);
*type = g_current_event_type;
std::memcpy(buffer, &g_event_info, size);
g_data_read_event.Signal();
return ams::ResultSuccess();
}
inline void HandleConnectionStateEventV1(bluetooth::HidEventInfo *event_info) {
switch (event_info->connection.v1.status) {
case BtdrvHidConnectionStatusOld_Opened:
controller::AttachHandler(&event_info->connection.v1.addr);
break;
case BtdrvHidConnectionStatusOld_Closed:
controller::RemoveHandler(&event_info->connection.v1.addr);
break;
default:
break;
}
}
inline void HandleConnectionStateEventV12(bluetooth::HidEventInfo *event_info) {
switch (event_info->connection.v12.status) {
case BtdrvHidConnectionStatus_Opened:
controller::AttachHandler(&event_info->connection.v12.addr);
break;
case BtdrvHidConnectionStatus_Closed:
controller::RemoveHandler(&event_info->connection.v12.addr);
break;
default:
break;
}
}
void HandleEvent(void) {
{
std::scoped_lock lk(g_event_info_lock);
R_ABORT_UNLESS(btdrvGetHidEventInfo(&g_event_info, sizeof(bluetooth::HidEventInfo), &g_current_event_type));
}
switch (g_current_event_type) {
case BtdrvHidEventType_Connection:
hos::GetVersion() < hos::Version_12_0_0 ? HandleConnectionStateEventV1(&g_event_info) : HandleConnectionStateEventV12(&g_event_info);
break;
default:
break;
}
g_system_event_fwd.Signal();
g_data_read_event.Wait();
if (g_system_event_user_fwd.GetBase()->state) {
g_system_event_user_fwd.Signal();
}
}
}