-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathannepro.py
283 lines (229 loc) · 15.2 KB
/
annepro.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
from threading import Thread
from gatt import DeviceManager, Device, Service, Characteristic
import time
import re
import math
lights = list()
# lights.append([0x02, 0x01, 0x01])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x00, 0x00, 0x00, 0x40, 0xD5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x06, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x07, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x08, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0xFF, 0xFF, 0xFF, 0x00, 0x00])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x09, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0A, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0C, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
lights.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
lights.append([0x09, 0xD7, 0x03, 0x06, 0x10, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
sync = []
sync.append([0x02, 0x01, 0x01])
keys0 = []
# keys0.append([0x02, 0x01, 0x01])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x00, 0x00, 0x00, 0xDA, 0xEF, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x01, 0x27, 0x27, 0x27, 0x27, 0x27, 0x14, 0x1A, 0x08, 0x15, 0x17, 0x1C, 0x18, 0x0C, 0x12])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x02, 0x13, 0x2F, 0x27, 0x27, 0x39, 0x07, 0x16, 0x07, 0x09, 0x0A, 0x0B, 0x0D, 0x11, 0x0F])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x03, 0x33, 0x34, 0x00, 0x28, 0xE1, 0x1D, 0x1B, 0x06, 0x19, 0x05, 0x11, 0x10, 0x36, 0x37])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x04, 0x38, 0x00, 0x00, 0xE5, 0xE0, 0xE3, 0xE2, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x05, 0xE6, 0xFE, 0xFA, 0xE4, 0x35, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x06, 0x43, 0x44, 0x45, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x47])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x07, 0x48, 0x4A, 0x4D, 0x46, 0x00, 0x50, 0x51, 0x4F, 0x00, 0x00, 0x00, 0x50, 0x51, 0x4F])
keys0.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x08, 0x4B, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49])
all = []
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x00, 0x00, 0x00, 0x25, 0xBC, 0xFF, 0xFF, 0xFF, 0x7F, 0x85, 0x94, 0x7F, 0x85, 0x94, 0x7F])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x01, 0x85, 0x94, 0x7F, 0x85, 0x94, 0x7F, 0x85, 0x94, 0x7F, 0x85, 0x94, 0x7F, 0x85, 0x94])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x02, 0x7F, 0x85, 0x94, 0x7F, 0x85, 0x94, 0x7F, 0x85, 0x94, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x03, 0xFF, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0xFF, 0xFF, 0xFF, 0x2E, 0x4C, 0x8F, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x06, 0xFF, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x07, 0x4C, 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x09, 0x00, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0A, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0C, 0x00, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x00])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
all.append([0x09, 0xD7, 0x03, 0x10, 0x10, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C])
all.append([0x09, 0xD7, 0x03, 0x06, 0x10, 0x0F, 0x8F, 0x2E, 0x4C, 0x8F, 0x00, 0x00, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C, 0x8F, 0x2E, 0x4C])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x00, 0x00, 0x00, 0x10, 0xB1, 0x27, 0x27, 0x20, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x01, 0x27, 0x27, 0x27, 0x27, 0x27, 0x14, 0x1A, 0x08, 0x15, 0x17, 0x1C, 0x18, 0x0C, 0x12])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x02, 0x13, 0x2F, 0x27, 0x27, 0x39, 0x07, 0x16, 0x07, 0x09, 0x0A, 0x0B, 0x0D, 0x11, 0x0F])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x03, 0x33, 0x34, 0x00, 0x28, 0xE1, 0x1D, 0x1B, 0x06, 0x19, 0x05, 0x11, 0x10, 0x36, 0x37])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x04, 0x38, 0x00, 0x00, 0xE5, 0xE0, 0xE3, 0xE2, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x05, 0xE6, 0xFE, 0xFA, 0xE4, 0x35, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x06, 0x43, 0x44, 0x45, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x47])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x07, 0x48, 0x4A, 0x4D, 0x46, 0x00, 0x50, 0x51, 0x4F, 0x00, 0x00, 0x00, 0x50, 0x51, 0x4F])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x08, 0x4B, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49])
all.append([0x07, 0x91, 0x02, 0x10, 0x0B, 0x09, 0x4C, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
all.append([0x07, 0x91, 0x02, 0x06, 0x0B, 0x0A, 0x00, 0xFE, 0xFA, 0x00, 0x00, 0xE3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
class pauser:
paused = False
class AnneManager(DeviceManager):
def __init__(self, adapter_name):
print("-- anne pro manager created using bluetooth adapter: " + adapter_name + " --")
super().__init__(adapter_name)
def update_devices(self):
print("BT Adapter: " + self.adapter_name + " | updating devices")
super().update_devices()
def make_device(self, mac_address):
device = Device(mac_address=mac_address, manager=self, managed=False)
if "anne" in device.alias().lower():
print("BT Adapter: " + self.adapter_name + " | Anne Pro Keyboard found (" + device.mac_address + ")")
return AnnePro(mac_address=mac_address, manager=self)
class AnnePro(Device):
def __init__(self, mac_address, manager):
super().__init__(mac_address, manager)
print("-- Discovered: Anne Pro Keyboard:")
print("\t{}: {}".format("MAC", self.mac_address))
print("\t{}: {}".format("Alias", self.alias()))
print("\t{}: {}".format("Adapter", self.manager.adapter_name))
def connect(self):
print("BT Adapter: " + self.manager.adapter_name + " | establishing connection with " + self.alias() + " (" + self.mac_address + ")...")
super().connect()
def connect_succeeded(self):
super().connect_succeeded()
print("BT Adapter: " + self.manager.adapter_name + " | successful connection with " + self.alias() + " (" + self.mac_address + ")!")
def connect_failed(self, error):
super().connect_failed(error)
print("BT Adapter: " + self.manager.adapter_name + " | failed connection with " + self.alias() + " (" + self.mac_address + ")!")
print(error)
def services_resolved(self):
self._disconnect_service_signals()
self.services = list()
services_regex = re.compile(self._device_path + '/service[0-9abcdef]{4}$')
managed_services = [
service for service in self._object_manager.GetManagedObjects().items()
if services_regex.match(service[0])]
for service in managed_services:
if service[1]['org.bluez.GattService1']['UUID'] == 'f000ffc0-0451-4000-b000-000000000000':
self.services.append(OverAirDownloadService(device=self, path=service[0]))
print("BT Device: " + self.mac_address + " | " + str(len(self.services)) + " resolved services saved")
def characteristic_write_value_succeeded(self, characteristic):
super().characteristic_write_value_succeeded(characteristic)
print("Characteristic: " + characteristic.uuid + " | successful write to device " + self.mac_address)
def characteristic_write_value_failed(self, characteristic, error):
super().characteristic_write_value_failed(characteristic, error)
print("Characteristic: " + characteristic.uuid + " | failed write to device " + str(self.mac_address))
class OverAirDownloadService(Service):
def __init__(self, device, path):
DATA_BLOCK_SIZE = 14
DATA_BUFFER_SIZE = 20
super().__init__(device, path, 'f000ffc0-0451-4000-b000-000000000000')
print("-- Discovered: Over Air Download Service")
print("\t{}: {}".format("UUID", self.uuid))
print("\t{}: {}".format("Device", self.device.mac_address))
print("\t{}: {}".format("Adapter", self.device.manager.adapter_name))
def characteristics_resolved(self):
"""
Called when all service's characteristics got resolved.
"""
self._disconnect_characteristic_signals()
characteristics_regex = re.compile(self._path + '/char[0-9abcdef]{4}$')
managed_characteristics = [
char for char in self._object_manager.GetManagedObjects().items()
if characteristics_regex.match(char[0])]
self.characteristics = list()
for characteristic in managed_characteristics:
if characteristic[1]['org.bluez.GattCharacteristic1']['UUID'] == 'f000ffc2-0451-4000-b000-000000000000':
self.characteristics.append(OADWriteCharacteristic(service=self,
path=characteristic[0],
uuid=characteristic[1]['org.bluez.GattCharacteristic1']['UUID']))
elif characteristic[1]['org.bluez.GattCharacteristic1']['UUID'] == 'f000ffc1-0451-4000-b000-000000000000':
self.characteristics.append(OADReadCharacteristic(service=self,
path=characteristic[0],
uuid=characteristic[1]['org.bluez.GattCharacteristic1']['UUID']))
self._connect_characteristic_signals()
class OADWriteCharacteristic(Characteristic):
def __init__(self, service, path, uuid):
self.write_buffer = []
super().__init__(service, path, uuid)
print("-- Discovered: Over Air Download Service - Write Characteristic")
print("\t{}: {}".format("UUID", self.uuid))
print("\t{}: {}".format("Service UUID", self.service.uuid))
print("\t{}: {}".format("Device", self.service.device.mac_address))
print("\t{}: {}".format("Adapter", self.service.device.manager.adapter_name))
def write_value(self, value, offset=0):
# self.write_buffer.append(OADDataBuffer(value))1212
test = 0
for item in value:
super().write_value(item)
test += len(item)
# if test:
# test = False
# time.sleep(3)121asd
print(item)
time.sleep(.4)
print(test)
def _write_value_succeeded(self):
if self.write_buffer:
self.write_buffer[0].blocks_sent += 1
self.write_buffer[0].is_sending = False
self.continue_write()
super()._write_value_succeeded()
def continue_write(self):
if self.write_buffer:
if not self.write_buffer[0].is_sending:
next = next(self.write_buffer[0].next_buffered)
if next is not None:
self.write_buffer[0].is_sending = True
super().write_value(self.write_buffer[0].meta, next)
else:
self.write_buffer.pop()
def init_write(self):
while self.write_buffer is not None:
self.continue_write()
class OADDataBuffer:
BLOCK_SIZE = 14
BUFFER_SIZE = 20
def __init__(self, metadata, value, offset=0):
self.blocks_max = len(metadata) if value is None else math.ceil(len(value) / self.service.DATA_BOCK_SIZE)
self.blocks_sent = 0
self.bytes_sent = 0
self.buffer = []
self.meta = metadata
self.data = value
def next_buffered(self):
pass
# def write_bytes(self):
class OADReadCharacteristic(Characteristic):
def __init__(self, service, path, uuid):
super().__init__(service, path, uuid)
print("-- Discovered: Over Air Download Service - Read Characteristic")
print("\t{}: {}".format("UUID", self.uuid))
print("\t{}: {}".format("Service UUID", self.service.uuid))
print("\t{}: {}".format("Device", self.service.device.mac_address))
print("\t{}: {}".format("Adapter", self.service.device.manager.adapter_name))
def write_value(self, value, offset=0):
print("Characteristic: " + self.uuid + " | writing data with offset " + offset)
print(value)
super().write_value(value, offset)
# class Test(object):
# mac_address = '60:64:05:b3:c4:53'
adaptor = AnneManager("hci0")
theAnne = None
anne = adaptor.devices()
for item in anne:
theAnne = item
# theAnne.connect()
thread = Thread(target = adaptor.run)
theAnne.connect()
thread.start()
print("Started Thread.")
theAnne.services[0].characteristics[0].write_value(all)
# theAnne.services[0].characteristics[0].write_value(sync)
# theAnne.services[0].characteristics[0].write_value(lights)
# theAnne.services[0].characteristics[0].write_value(keys0)
try:
while True:
pass
except KeyboardInterrupt:
print('KeyboardInterrupt!')
theAnne.disconnect()
adaptor.stop()
thread.join()