-
Notifications
You must be signed in to change notification settings - Fork 1
/
xoss_sync.py
357 lines (314 loc) · 15.7 KB
/
xoss_sync.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
#!/usr/bin/env python
#coding:utf-8
#
# (c) 2024 ekspla.
# MIT License. https://github.com/ekspla/xoss_sync
#
# A quick/preliminary version of code to fetch fit files from XOSS G+ cyclo-computer, inspired by f-xoss project
# (https://github.com/DCNick3/f-xoss).
#
# This code is a modified version of cycsync.py (https://github.com/Kaiserdragon2/CycSync) for Cycplus M2.
#
# The main differences from Cycsync are:
# 1. additions of crc8_xor and crc16_arc to check the data.
# 2. check successive block numbers in YMODEM protocol.
# 3. check size of the retrieved file.
# 4. use of slice assignment and memoryview in handling notification packets and blocks.
# 5. tested with XOSS G+ instead of Cycplus M2.
# 6. timings/delays were adjusted for my use case (XOSS G+, Win10 on Core-i5, TPLink UB400 BT dongle, py-3.8.6 and bleak-0.22.2).
#
# TODO:
# 1. handling of fit-file data more efficiently on memory.
import asyncio
from bleak import BleakScanner, BleakClient
import re
import os
import datetime
#TARGET_NAME = "XOSS G-040989"
TARGET_NAME = "XOSS"
#SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
CTL_CHARACTERISTIC_UUID = "6e400004-b5a3-f393-e0a9-e50e24dcca9e"
TX_CHARACTERISTIC_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
RX_CHARACTERISTIC_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
VALUE_IDLE = bytearray([0x04, 0x00, 0x04]) # r(ead)/w(rite)
FILE_FETCH = bytearray([0x05]) # w
OK_FILE_FETCH = bytearray([0x06]) # r
#FILE_SEND = bytearray([0x07]) # w
#OK_FILE_SEND = bytearray([0x08]) # r
VALUE_DISKSPACE = bytearray([0x09, 0x00, 0x09]) # w
OK_DISKSPACE = bytearray([0x0a]) # r
#FILE_DELETE = bytearray([0x0d]) # w
#OK_FILE_DELETE = bytearray([0x0e]) # r
#VALUE_STOP = bytearray([0x1f, 0x00, 0x1f]) # w
#VALUE_ERR_CMD = bytearray([0x11, 0x00, 0x11]) # r
#ERR_FILE_NA = bytearray([0x12]) # r
#VALUE_ERR_MEMORY = bytearray([0x13, 0x00, 0x13]) # r
#VALUE_ERR_NO_IDLE = bytearray([0x14, 0x00, 0x14]) # r
#ERR_FILE_PARSE = bytearray([0x15]) # r
TIME_SET = bytearray([0x54]) # w
OK_TIME_SET = bytearray([0x55]) # r
VALUE_STATUS = bytearray([0xff, 0x00, 0xff]) # w
VALUE_SOH = bytearray([0x01]) # SOH == 128-byte data
#VALUE_STX = bytearray([0x02]) # STX == 1024-byte data
VALUE_C = bytearray([0x43]) # 'C'
#VALUE_G = bytearray([0x47]) # 'G'
VALUE_ACK = bytearray([0x06]) # ACK
VALUE_NAK = bytearray([0x15]) # NAK
VALUE_EOT = bytearray([0x04]) # EOT
VALUE_CAN = bytearray([0x18]) # CAN
AWAIT_NEW_DATA = bytearray(b'AwaitNewData')
class BluetoothFileTransfer:
def __init__(self):
self.lock = asyncio.Lock()
# **Packet**
self.notification_data = bytearray()
# **Block**
self.is_block = False
self.block_buf = bytearray(3 + 128 + 2) # Header(SOH, num, ~num); data; CRC16
self.block_num = 0 # Block number(0-255).
self.idx_block_buf = 0 # Index in block_buf.
self.mv_block_buf = memoryview(self.block_buf)
self.block_data = self.mv_block_buf[3:-2]
self.block_crc = self.mv_block_buf[-2:]
self.block_error = False
# **File** A file is made of blocks; a block is made of packets.
self.data = bytearray()
self.data_size = 0
def create_notification_handler(self):
async def notification_handler(sender, data):
##print(data) # For test.
if data == VALUE_EOT: # Receive EOT.
self.is_block = False
self.notification_data = data
elif self.is_block: # Packets should be combined to make a block.
async with self.lock: # Use asyncio.Lock() for safety.
self.mv_block_buf[self.idx_block_buf:self.idx_block_buf + (len_data := len(data))] = data
self.idx_block_buf += len_data
else:
self.notification_data = data # Other messages/responses.
return notification_handler
async def discover_device(self, target_name):
print("Scanning for Bluetooth devices...")
retries = 3
while retries > 0:
devices = await BleakScanner.discover(timeout=30.0)
for device in devices:
print(f"Found device: {device.name} - {device.address}")
if device.name is not None and target_name in device.name:
print(f"Found target device: {device.name} - {device.address}")
return device
retries -= 1
print(f"Device with name {target_name} not found.")
return None
async def start_notify(self, client, uuid):
try:
await client.start_notify(uuid, self.create_notification_handler())
except Exception as e:
print(f"Failed to start notifications: {e}")
async def send_cmd(self, client, uuid, value, delay):
try:
await client.write_gatt_char(uuid, value, False)
except Exception as e:
print(f"Failed to write value to characteristic: {e}")
await asyncio.sleep(delay)
async def get_idle_status(self, client):
self.notification_data = AWAIT_NEW_DATA
self.is_block = False
await self.send_cmd(client, CTL_CHARACTERISTIC_UUID, VALUE_STATUS, 5.0) # Send STATUS (0xff, 0x00, 0xff)
await self.wait_until_data(client)
if self.notification_data == VALUE_IDLE: # Receive IDLE (0x04, 0x00, 0x04)
return True
if self.notification_data == AWAIT_NEW_DATA: # Timeout; No response
await self.send_cmd(client, CTL_CHARACTERISTIC_UUID, VALUE_IDLE, 0.1) # Send IDLE (0x04, 0x00, 0x04)
await self.wait_until_data(client)
if self.notification_data == VALUE_IDLE: # Receive IDLE (0x04, 0x00, 0x04)
return True
print(f'Error: {self.notification_data}')
return False
async def read_block_zero(self, client):
self.block_num = -1
self.idx_block_buf = 0
self.is_block = True
self.block_error = False
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_C, 0.1) # Send 'C'.
await self.read_block(client)
async def read_block(self, client):
async def check_block_buf():
while self.is_block and self.idx_block_buf < 133: # c.f. 1+1+1+128+2=133 bytes (one block)
await asyncio.sleep(0.01)
try:
await asyncio.wait_for(check_block_buf(), timeout=10)
if not self.is_block: return # The 1st EOT may arrive very late.
if int.from_bytes(self.block_crc, 'big') != self.crc16_arc(self.block_data):
self.block_error = True
else:
self.data.extend(self.block_data) # Blocks should be combined to make a file.
if self.block_buf[1] == (self.block_num + 1) % 256:
if self.block_error: print(f'Fixed error in block{self.block_buf[1]}.')
else:
print(f'Unexpected block: {self.block_num} -> {self.block_buf[1]}')
self.block_num = self.block_buf[1]
self.block_error = False
except asyncio.TimeoutError:
self.block_error = True
# Prepare for the next data block.
self.idx_block_buf = 0
async def end_of_transfer(self, client):
# The first EOT was received already.
await asyncio.sleep(0.1) # This avoids NAK to be sent too fast.
self.notification_data = AWAIT_NEW_DATA
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_NAK, 0.1) # Send NAK.
await self.wait_until_data(client) # Receive the second EOT.
await asyncio.sleep(0.1) # This avoids ACK to be sent too fast.
self.notification_data = AWAIT_NEW_DATA
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_ACK, 0.1) # Send ACK.
await self.wait_until_data(client) # Receive IDLE (0x04, 0x00, 0x04)
async def fetch_file(self, client, filename):
if self.notification_data != VALUE_IDLE:
if not await self.get_idle_status(client): return
# Request the File
self.notification_data = AWAIT_NEW_DATA
value_file_fetch = self.make_command(FILE_FETCH, filename)
await self.send_cmd(client, CTL_CHARACTERISTIC_UUID, value_file_fetch, 0.1) # Request starts with 0x05
await self.wait_until_data(client)
if self.notification_data == self.make_command(OK_FILE_FETCH, filename): # Response starts with 0x06
retries = 3
while retries > 0:
await self.read_block_zero(client) # Block 0 consists of name and size of the file.
if self.block_error:
retries -= 1
self.is_block = False # Wait 0.2 s for garbage.
await asyncio.sleep(0.2)
self.is_block = True
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_NAK, 0.1) # Send NAK on error.
else:
break
if retries == 0: # Too many errors in reading block zero; cancel transport.
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_CAN, 0.1) # Send CAN (cancel).
return
self.data_size = int(self.block_data.tobytes().rstrip(b'\x00').decode('utf-8').split()[1])
self.data = bytearray() # Where the file to be stored.
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_ACK, 0.1) # Send ACK.
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_C, 0.1) # Send 'C'.
# Blocks of num>=1 should be combined to obtain the file.
while self.is_block: # Receive EOT to exit this loop.
await self.read_block(client)
if not self.is_block: break # The 1st EOT may arrive very late.
if self.block_error:
self.is_block = False # Wait 0.2 s for garbage.
await asyncio.sleep(0.2)
self.is_block = True
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_NAK, 0.1) # Send NAK on error.
else:
await self.send_cmd(client, RX_CHARACTERISTIC_UUID, VALUE_ACK, 0.1) # Send ACK.
await self.end_of_transfer(client)
self.save_file_raw(filename)
async def wait_until_data(self, client):
i = 0
while self.notification_data == AWAIT_NEW_DATA:
await asyncio.sleep(0.01)
i = i + 1
if i >= 1000:
print(f"Something went wrong. No new notification data.")
break
async def read_diskspace(self, client):
# Read Diskspace; e.g. bytearray(b'\n556/8104\x1e')
self.notification_data = AWAIT_NEW_DATA
self.is_block = False
await self.send_cmd(client, CTL_CHARACTERISTIC_UUID, VALUE_DISKSPACE, 0.1) # Request starts with 0x09
await self.wait_until_data(client) # Response starts with 0x0a(b'\n')
if (self.crc8_xor(self.notification_data) == 0 and
self.notification_data.startswith(OK_DISKSPACE)):
diskspace = self.notification_data[1:-1].decode('utf-8')
print(f"Free Diskspace: {diskspace}kb")
async def time_set(self, client):
# Set RTC on the device (32-bit uint, UTC, and 1970/1/1 epoch)
self.notification_data = AWAIT_NEW_DATA
self.is_block = False
value_time_set = (TIME_SET
+ bytearray(int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()).to_bytes(4, 'little'))
+ bytearray([0x00]))
value_time_set[-1] = self.crc8_xor(value_time_set)
await self.send_cmd(client, CTL_CHARACTERISTIC_UUID, value_time_set, 0.1) # Request starts with 0x54
#await self.wait_until_data(client) # Response starts with 0x55
await asyncio.sleep(1) # Wait 1 sec because of no response from XOSS-G+ gen1.
async def run(self):
device = await self.discover_device(TARGET_NAME)
if not device:
return
async with BleakClient(device.address, timeout=60.0) as client:
if client.is_connected:
print(f"Connected to {device.name}")
##print(f"MTU {client.mtu_size}")
await self.start_notify(client, CTL_CHARACTERISTIC_UUID)
await self.start_notify(client, TX_CHARACTERISTIC_UUID)
print(f"Notifications started")
#await self.time_set(client)
await self.read_diskspace(client)
await self.fetch_file(client, 'filelist.txt')
fit_files = self.extract_fit_filenames('filelist.txt')
for fit_file in fit_files:
if os.path.exists(fit_file):
print(f'Skip: {fit_file}')
else:
print(f"Retrieving {fit_file}")
await self.fetch_file(client, fit_file)
await client.stop_notify(CTL_CHARACTERISTIC_UUID)
await client.stop_notify(TX_CHARACTERISTIC_UUID)
else:
print(f"Failed to connect to {device.name}")
def extract_fit_filenames(self, file_path):
fit_files = set()
pattern = re.compile(r'\d{14}\.fit')
try:
with open(file_path, 'r') as file:
lines = file.readlines()
for line in lines:
match = pattern.search(line)
if match:
fit_files.add(match.group(0))
except Exception as e:
print(f"Failed to read/parse file: {e}")
return fit_files
def save_file_raw(self, filename):
mv_file_data = memoryview(self.data)
i = -1
while self.data[i] == 0x00: # Remove padded zeros at the end.
i -= 1
with open(filename, "wb") as file:
size = file.write(mv_file_data[:i+1] if i < -1 else self.data)
if size != self.data_size:
print(f"Error: {size}(file size) != {self.data_size}(spec)")
else:
print(f"Successfully wrote combined data to {filename}")
def crc8_xor(self, data):
'''crc8/xor
See make_command() how to use.
'''
crc = 0
for x in data:
crc ^= x
return crc & 0xff
def crc16_arc(self, data):
'''crc16/arc
XOSS uses CRC16/ARC instead of CRC16/XMODEM.
'''
crc = 0
for x in data:
crc ^= x
for _ in range(8):
if (crc & 0x0001) > 0:
crc = (crc >> 1) ^ 0xa001
else:
crc = crc >> 1
return crc & 0xffff
def make_command(self, cmd, string=None):
byte_array = cmd + bytearray(string.encode('utf-8') if string is not None else b'\x00') + bytearray([0x00])
byte_array[-1] = self.crc8_xor(byte_array) # Replace the padded zero with crc8_xor.
return byte_array
if __name__ == "__main__":
transfer = BluetoothFileTransfer()
try:
asyncio.run(transfer.run())
finally:
asyncio.new_event_loop() # Clear retained state.