-
Notifications
You must be signed in to change notification settings - Fork 10
/
blehid.py
367 lines (317 loc) · 10.5 KB
/
blehid.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import operator
import logging
import time
from functools import reduce
from serial_wrappers import BLESerialWrapper
import asyncio
# keys source http://www.freebsddiary.org/APC/usb_hid_usages.php
keymap = dict([
("BACKSPACE", 0x2a),
("ENTER", 0x28),
("TAB", 0x2b),
("PAUSE", 0x48),
("ESCAPE", 0x29),
("SPACE", 0x2c),
("QUOTE", 0x34),
("COMMA", 0x36),
("MINUS", 0x2d),
("PERIOD", 0x37),
("SLASH", 0x38),
("0", 0x27),
("1", 0x1e),
("2", 0x1f),
("3", 0x20),
("4", 0x21),
("5", 0x22),
("6", 0x23),
("7", 0x24),
("8", 0x25),
("9", 0x26),
("SEMICOLON", 0x33),
("EQUALS", 0x2e),
("LEFTBRACKET", 0x2f),
("BACKSLASH", 0x31),
("RIGHTBRACKET", 0x30),
("BACKQUOTE", 0x35),
("A", 0x04),
("B", 0x05),
("C", 0x06),
("D", 0x07),
("E", 0x08),
("F", 0x09),
("G", 0x0a),
("H", 0x0b),
("I", 0x0c),
("J", 0x0d),
("K", 0x0e),
("L", 0x0f),
("M", 0x10),
("N", 0x11),
("O", 0x12),
("P", 0x13),
("Q", 0x14),
("R", 0x15),
("S", 0x16),
("T", 0x17),
("U", 0x18),
("V", 0x19),
("W", 0x1a),
("X", 0x1b),
("Y", 0x1c),
("Z", 0x1d),
("DELETE", 0x4c),
("KP0", 0x62),
("KP1", 0x59),
("KP2", 0x5a),
("KP3", 0x5b),
("KP4", 0x5c),
("KP5", 0x5d),
("KP6", 0x5e),
("KP7", 0x5f),
("KP8", 0x60),
("KP9", 0x61),
("KP_PERIOD", 0x63),
("KP_DIVIDE", 0x54),
("KP_MULTIPLY", 0x55),
("KP_MINUS", 0x56),
("KP_PLUS", 0x57),
("KP_ENTER", 0x58),
("KP_EQUAL", 0x67), # Keypad =
("KP_COMMA", 0x85), # Keypad Comma
("KP_EQSIGN", 0x86), # Keypad Equal Sign
("UP", 0x52),
("DOWN", 0x51),
("RIGHT", 0x4f),
("LEFT", 0x50),
("INSERT", 0x49),
("HOME", 0x4a),
("END", 0x4d),
("PAGEUP", 0x4b),
("PAGEDOWN", 0x4e),
("F1", 0x3a),
("F2", 0x3b),
("F3", 0x3c),
("F4", 0x3d),
("F5", 0x3e),
("F6", 0x3f),
("F7", 0x40),
("F8", 0x41),
("F9", 0x42),
("F10", 0x43),
("F11", 0x44),
("F12", 0x45),
("NUMLOCK", 0x53),
("CAPSLOCK", 0x39),
("SCROLLOCK", 0x47),
("RIGHTARROW", 0x4F),
("LEFTARROW", 0x50),
("DOWNARROW", 0x51),
("UPARROW", 0x52),
("APP", 0x65), # Keyboard Application
("LGUI", 0xE3), # Keyboard Left GUI
("RGUI", 0xE7), # Keyboard Right GUI
("CUSTOM~", 0x32), # Keyboard Non-US # and ~
("NONUSHASH", 0x32), # Alias
("NONUSTILDE", 0x32), # Alias
("NON-US-BACKSLASH", 0x64), # Non US backslash
("PRINTSCREEN", 0x46), # Keyboard PrintScreen
("POWER", 0x66), # Keyboard Power
("EXECUTE", 0x74), # Keyboard Execute
("HELP", 0x75), # Keyboard Help
("MENU", 0x76), # Keyboard Menu
("SELECT", 0x77), # Keyboard Select
("STOP", 0x78), # Keyboard Stop
("AGAIN", 0x79), # Keyboard Again
("UNDO", 0x7A), # Keyboard Undo
("CUT", 0x7B), # Keyboard Cut
("COPY", 0x7C), # Keyboard Copy
("PASTE", 0x7D), # Keyboard Paste
("FIND", 0x7E), # Keyboard Find
("MUTE", 0x7F), # Keyboard Mute
("VOLUP", 0x80), # Keyboard Volume Up
("VOLDOWN", 0x81), # Keyboard Volume Down
("LOCKING_CAPSLOCK", 0x82), # Keyboard Locking Caps Lock
("LOCKING_NUMLOCK", 0x83), # Keyboard Locking Num Lock
("LOCKING_SCROLLOCK", 0x84), # Keyboard Locking Scroll Lock
("ALTERASE", 0x99), # Keyboard Alternate Erase
("ATTENTION", 0x9A), # Keyboard SysReq/Attention
("CANCEL", 0x9B), # Keyboard Cancel
("CLEAR", 0x9C), # Keyboard Clear
("PRIOR", 0x9D), # Keyboard Prior
("RETURN", 0x9E), # Keyboard Return
("SEPARATOR", 0x9F), # Keyboard Separator
("OUT", 0xA0), # Keyboard Out
("OPER", 0xA1), # Keyboard Oper
])
async def _write_atcmd(ser, msg):
if not isinstance(msg, bytes):
msg = msg.encode()
logging.debug("request: {}".format(
msg if isinstance(msg, str) else str(msg, "utf8")))
ser.flushInput()
if isinstance(ser, BLESerialWrapper):
await ser.write(msg + b"\r\n")
else:
await asyncio.gather(asyncio.to_thread(ser.write, (msg + b"\r\n")))
async def _read_response(ser, n=1):
v = b""
while n > 0:
if isinstance(ser, BLESerialWrapper):
v += await ser.readline()
else:
response = await asyncio.gather(asyncio.to_thread(ser.readline))
v += response[0]
n -= 1
msg = str(v, "utf8").strip()
logging.debug("response: {}".format(msg))
return msg
async def blehid_init_serial(ser):
await _write_atcmd(ser, "AT")
await _read_response(ser)
# _write_atcmd(ser, "ATI")
# _read_response(ser, n=8)
await _write_atcmd(ser, "ATE=0")
await _read_response(ser)
await _write_atcmd(ser, "AT+BLEHIDEN=1")
response = await _read_response(ser)
if response.upper() == "ERROR":
# running older framework < 0.6.6, fallback to enable keyboard
await _write_atcmd(ser, "AT+BLEKEYBOARDEN=1")
await _read_response(ser)
await _write_atcmd(ser, "ATZ")
await _read_response(ser)
MOUSE_MAX_MOVE = 2500
async def blehid_send_movemouse(ser, right, down, wheely, wheelx):
right = max(-1 * MOUSE_MAX_MOVE,
right) if right < 0 else min(MOUSE_MAX_MOVE, right)
down = max(-1 * MOUSE_MAX_MOVE,
down) if down < 0 else min(MOUSE_MAX_MOVE, down)
wheely = max(-1 * MOUSE_MAX_MOVE,
wheely) if wheely < 0 else min(MOUSE_MAX_MOVE, wheely)
wheelx = max(-1 * MOUSE_MAX_MOVE,
wheelx) if wheelx < 0 else min(MOUSE_MAX_MOVE, wheelx)
while right != 0 or down != 0 or wheelx != 0 or wheely != 0:
rmove = max(-128, min(right, 127))
dmove = max(-128, min(down, 127))
wymove = max(-128, min(wheely, 127))
wxmove = max(-128, min(wheelx, 127))
atcmd = "AT+BLEHIDMOUSEMOVE={},{},{},{}".format(
rmove, dmove, wymove, wxmove)
await _write_atcmd(ser, atcmd)
await _read_response(ser)
right -= rmove
down -= dmove
wheely -= wymove
wheelx -= wxmove
async def blehid_send_mousebutton(ser, btn, behavior=None):
atcmd = "AT+BLEHIDMOUSEBUTTON={}{}".format(
btn, "" if behavior is None else "," + behavior)
await _write_atcmd(ser, atcmd)
await _read_response(ser)
async def blehid_send_keyboardcode(ser, key, modifiers, down, keys):
if key in ["LCTRL", "LSHIFT", "LALT", "LMETA", "RCTRL", "RSHIFT", "RALT", "RMETA"]:
modifiers.append(key)
key = None
logging.debug('key:'+str(key)+' modifiers:'+str(modifiers)+' action:'+str(down))
# modifier handling
hidmod = reduce(operator.or_, map(
lambda a: a[1],
filter(lambda a: a[0] in modifiers,
[("LCTRL", 0x01), ("LSHIFT", 0x02), ("LALT", 0x04),
("LMETA", 0x08), ("RCTRL", 0x10), ("RSHIFT", 0x20),
("RALT", 0x40), ("RMETA", 0x80)])), 0)
if down == True:
keys[0] = keys[0] | hidmod
else:
keys[0] = keys[0] & (~hidmod)
# key handling
keycode = keymap.get(key, 0)
# if OS == 'ios' and keycode == 13:
# hidcode = 0x58
if key != 0:
for i in range(2, 8):
if keys[i] == 0:
if down == True:
keys[i] = keycode
break
elif keys[i] == keycode:
if down == False:
keys[i] = 0
else:
break
# preparing command
logging.debug("keycode: {:02x}, mod: {:02x}".format(keycode, keys[0]))
atcmd = "AT+BLEKEYBOARDCODE={:02x}-00".format(keys[0])
zerocmd = ""
for i in range(2, 8):
if keys[i] != 0:
atcmd += "-{:02x}".format(keys[i])
else:
zerocmd += "-00"
atcmd += zerocmd
await _write_atcmd(ser, atcmd)
await _read_response(ser)
async def blehid_send_devicecommand(ser, devicecommand):
logging.debug('device command:'+str(devicecommand))
if devicecommand == 'drop-bonded-device':
await _write_atcmd(ser, "AT+GAPDISCONNECT")
await _read_response(ser)
await _write_atcmd(ser, "AT+GAPDELBONDS")
await _read_response(ser)
logging.debug('Reset BT device')
async def blehid_send_switch_command(ser, devicecommand):
logging.debug('device command:'+str(devicecommand))
if devicecommand == 'switch':
await _write_atcmd(ser, "AT+SWITCHCONN")
await _read_response(ser)
elif devicecommand.startswith("switch="):
await _write_atcmd(ser, "AT+SWITCHCONN=\"{}\"".format(devicecommand.split("=")[1]))
await _read_response(ser)
async def blehid_send_get_device_name(ser, devicecommand):
logging.debug('device command:'+str(devicecommand))
if devicecommand == 'devname':
await _write_atcmd(ser, "AT+BLECURRENTDEVICENAME")
resp = await _read_response(ser, n=2)
resp = resp.split('\n') #split('\r\n')
try:
return resp[1]
except:
pass
return 'NONE'
async def blehid_send_add_device(ser, devicecommand):
logging.debug('device command:'+str(devicecommand))
await _write_atcmd(ser, "AT+BLEADDNEWDEVICE")
async def blehid_send_clear_device_list(ser, devicecommand):
logging.debug('device command:'+str(devicecommand))
await _write_atcmd(ser, "AT+RESETDEVLIST")
async def blehid_send_remove_device(ser, devicecommand):
logging.debug('device command:'+"at+bleremovedevice\"" + devicecommand.split("=")[1] + "\"")
await _write_atcmd(ser, "AT+BLEREMOVEDEVICE=\"" + devicecommand.split("=")[1] + "\"")
async def blehid_get_device_list(ser, devicecommand):
logging.debug('device command:'+str(devicecommand))
ser.flushInput()
ser.flushOutput()
await asyncio.sleep(0.1) #time.sleep(0.1)
await _write_atcmd(ser, "AT+PRINTDEVLIST")
"""
timeout = time.time() + 1
while True:
if time.time() > timeout:
break
"""
await asyncio.sleep(1)
data = ser.read_all()
data = str(data, "utf8").strip().split('\n') #split('\r\n')
logging.debug("response: {}".format(data))
return data[1:]
async def blehid_get_at_response(ser):
logging.debug('Sending AT command to dongle')
await _write_atcmd(ser, "AT")
return await _read_response(ser)
async def blehid_get_mode(ser):
logging.debug('Get dongle mode')
await _write_atcmd(ser, "AT+GETMODE")
return await _read_response(ser)
async def blehid_switch_mode(ser):
logging.debug('Switch dongle mode')
await _write_atcmd(ser, "AT+SWITCHMODE")