fujistuff 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fujistuff/__init__.py +0 -0
- fujistuff/ble.py +367 -0
- fujistuff/blob.py +55 -0
- fujistuff/cameras/gen3.yaml +155 -0
- fujistuff/cameras/xt3-menu.yaml +1411 -0
- fujistuff/cameras/xt3-tether.yaml +132 -0
- fujistuff/cameras/xt3.yaml +764 -0
- fujistuff/cameras/xt30.yaml +193 -0
- fujistuff/catalogue.py +268 -0
- fujistuff/cli.py +131 -0
- fujistuff/decode.py +121 -0
- fujistuff/measure.py +62 -0
- fujistuff/menu.py +122 -0
- fujistuff/models.py +146 -0
- fujistuff/ptp.py +424 -0
- fujistuff/recipes.py +176 -0
- fujistuff/rounds.py +269 -0
- fujistuff/settings.py +94 -0
- fujistuff/store.py +104 -0
- fujistuff/sync.py +353 -0
- fujistuff/ui.py +13 -0
- fujistuff/walk.py +502 -0
- fujistuff/walkplan.py +191 -0
- fujistuff-0.1.0.dist-info/METADATA +116 -0
- fujistuff-0.1.0.dist-info/RECORD +27 -0
- fujistuff-0.1.0.dist-info/WHEEL +4 -0
- fujistuff-0.1.0.dist-info/entry_points.txt +3 -0
fujistuff/__init__.py
ADDED
|
File without changes
|
fujistuff/ble.py
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"""Bluetooth LE as a second way to the X-T3 settings backup. Writing is locked until `fuji test-write` passed.
|
|
2
|
+
|
|
3
|
+
Protocol after maam00/dialin-tools docs/FUJIFILM-PROTOCOL.md 3.4, 3.6 and 3.7 (MIT, tested there on an X100VI) and
|
|
4
|
+
the X-T3 GATT table efaafa of tiredboffin/fffw (MIT). Results: docs/research/2026-11-23-wireless-route.md
|
|
5
|
+
"""
|
|
6
|
+
import asyncio
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import threading
|
|
11
|
+
import zlib
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from . import blob
|
|
15
|
+
from .ptp import NotConnected, PtpError
|
|
16
|
+
|
|
17
|
+
COMPANY = 0x04D8
|
|
18
|
+
FILES = "af854c2e-b214-458e-97e2-912c4ecf2cb8" # service, also the XApp advert; Camera Remote has another key
|
|
19
|
+
CAMERA_REMOTE = "117c4142-edd4-4c77-8696-dd18eebb770a"
|
|
20
|
+
KEY = "aba356eb-9633-4e60-b73f-f52516dbd671"
|
|
21
|
+
NAME = "85b9163e-62d1-49ff-a6f5-054b4630d4a1"
|
|
22
|
+
SERIAL = "00002a25-0000-1000-8000-00805f9b34fb"
|
|
23
|
+
BACKUP_REQUEST = "f14e7a88-6f57-4872-a69b-b47ef3295c9b"
|
|
24
|
+
BACKUP_STATE = "11438c83-cfb0-4511-841b-759e0d2321c8"
|
|
25
|
+
CHUNK_SIZE = "7f3400fe-17e7-4b80-8a0e-81b0343c1b49"
|
|
26
|
+
FILE_INDEX = "051dd980-df9d-4472-a2e1-35811dd24ee1"
|
|
27
|
+
CHUNK = "ac0c799a-fa6c-4df5-bbc5-bb95cce7e6ea"
|
|
28
|
+
RESULT = "68052e8a-fb91-404f-8847-0eb4be24308c"
|
|
29
|
+
TRANSFER = "2e27ed9f-5506-41cd-ba48-dac06669ad95"
|
|
30
|
+
CLIENT = b"fujistuff\0"
|
|
31
|
+
RESTORE_REQUEST = "f1897eb9-a9aa-40f0-8ce6-2fa7915bc459"
|
|
32
|
+
RESTORE_STATE = "4b3a413c-230f-42bc-b3ed-b1db2eadee82"
|
|
33
|
+
FILE_INFO = "c922ac69-9480-4348-8f4b-9ee29bc30d1d"
|
|
34
|
+
# The only writes this module can make. The uploading ones are used by upload() alone.
|
|
35
|
+
WRITABLE = {KEY, NAME, BACKUP_REQUEST, CHUNK_SIZE, FILE_INDEX, RESULT}
|
|
36
|
+
UPLOADING = {RESTORE_REQUEST, FILE_INFO, CHUNK}
|
|
37
|
+
PART = 120 # data bytes per frame, both directions
|
|
38
|
+
MTU = 133 # the last frame has up to 130 bytes plus 3 for ATT
|
|
39
|
+
SETTLE = 3 # s after a restore: the camera's screen goes dark for a moment
|
|
40
|
+
LIMIT = 1 << 20
|
|
41
|
+
GAP = 1.5 # s between two reads: without it the X-T3 answers «not ready»
|
|
42
|
+
LOOP_TIMEOUT = 120 # s, above the sum of all waits of one read
|
|
43
|
+
READY = 10 # s to wait for «ready»; a «not ready» before that is not final
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class BleError(PtpError):
|
|
47
|
+
via = "Bluetooth"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class NotFound(BleError, NotConnected):
|
|
51
|
+
"""Camera off, asleep, out of reach, or connected to the phone."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def is_xt3(name: str | None) -> bool:
|
|
55
|
+
"""The X-T3 advertises <tag>X-T3-<tag> (seen on 5.11), fffw notes it without the hyphen; X-T30 must not match."""
|
|
56
|
+
return bool(re.fullmatch(r"(\w{4})X-T3-?\1", name or ""))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def pairing_key(name: str | None, uuids, manufacturer: dict[int, bytes]) -> bytes | None:
|
|
60
|
+
"""Key of an X-T3 in pairing mode for XApp-style clients. Adverts for Camera Remote carry a different key."""
|
|
61
|
+
data, uuids = manufacturer.get(COMPANY, b""), {u.lower() for u in uuids}
|
|
62
|
+
if is_xt3(name) and FILES in uuids and CAMERA_REMOTE not in uuids and len(data) == 5 and data[0] == 2:
|
|
63
|
+
return data[1:]
|
|
64
|
+
return None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Assembler:
|
|
68
|
+
"""Chunks are u16 sequence, u32 length, data. The last one (0xFFFF) ends with a u16 sum of all data bytes."""
|
|
69
|
+
|
|
70
|
+
def __init__(self):
|
|
71
|
+
self.data, self.next, self.done = b"", 0, False
|
|
72
|
+
|
|
73
|
+
def add(self, raw: bytes) -> None:
|
|
74
|
+
if self.done:
|
|
75
|
+
raise BleError("chunk after the last one")
|
|
76
|
+
seq, size, part = int.from_bytes(raw[:2], "little"), int.from_bytes(raw[2:6], "little"), raw[6:]
|
|
77
|
+
if len(raw) < 6 or size > len(part):
|
|
78
|
+
raise BleError(f"malformed chunk of {len(raw)} bytes")
|
|
79
|
+
part = part[:size]
|
|
80
|
+
if seq == 0xFFFF:
|
|
81
|
+
if size < 2:
|
|
82
|
+
raise BleError("last chunk without a sum")
|
|
83
|
+
part, want = part[:-2], int.from_bytes(part[-2:], "little")
|
|
84
|
+
self.done = True
|
|
85
|
+
elif seq != self.next:
|
|
86
|
+
raise BleError(f"chunk {seq} instead of {self.next}")
|
|
87
|
+
self.next += 1
|
|
88
|
+
self.data += part
|
|
89
|
+
if len(self.data) > LIMIT:
|
|
90
|
+
raise BleError("backup is too big")
|
|
91
|
+
if self.done and sum(self.data) & 0xFFFF != want:
|
|
92
|
+
raise BleError("sum does not match, the backup arrived damaged")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def unpack(data: bytes) -> bytes:
|
|
96
|
+
if data.startswith(blob.MAGIC):
|
|
97
|
+
return data
|
|
98
|
+
z = zlib.decompressobj()
|
|
99
|
+
try:
|
|
100
|
+
out = z.decompress(data, LIMIT)
|
|
101
|
+
except zlib.error as e:
|
|
102
|
+
raise BleError(f"neither a backup nor zlib: {e}") from e
|
|
103
|
+
if not z.eof:
|
|
104
|
+
raise BleError("compressed backup is incomplete")
|
|
105
|
+
return out
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def frames(wire: bytes) -> list[bytes]:
|
|
109
|
+
"""Frames are u16 sequence, u32 length, data, u16. The last one (0xFFFF) carries the sum in the data and behind it."""
|
|
110
|
+
total, parts = (sum(wire) & 0xFFFF).to_bytes(2, "little"), [wire[i:i + PART] for i in range(0, len(wire), PART)]
|
|
111
|
+
return [(0xFFFF).to_bytes(2, "little") + (len(p) + 2).to_bytes(4, "little") + p + total + total
|
|
112
|
+
if i == len(parts) - 1 else i.to_bytes(2, "little") + len(p).to_bytes(4, "little") + p + bytes(2)
|
|
113
|
+
for i, p in enumerate(parts)]
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async def write(client, uuid: str, data: bytes, allowed=WRITABLE) -> None:
|
|
117
|
+
if uuid not in allowed:
|
|
118
|
+
raise BleError(f"write to {uuid} is not allowed")
|
|
119
|
+
await asyncio.wait_for(client.write_gatt_char(uuid, data, response=True), 10)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
async def state(queues: dict[str, asyncio.Queue], uuid: str) -> int:
|
|
123
|
+
try:
|
|
124
|
+
return int.from_bytes((await asyncio.wait_for(queues[uuid].get(), 10))[:2], "little")
|
|
125
|
+
except TimeoutError:
|
|
126
|
+
raise BleError(f"no answer from the camera ({uuid[:8]})") from None
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async def ready(queues: dict[str, asyncio.Queue], uuid: str) -> None:
|
|
130
|
+
try:
|
|
131
|
+
async with asyncio.timeout(READY):
|
|
132
|
+
while int.from_bytes((await queues[uuid].get())[:2], "little") != 1:
|
|
133
|
+
pass # «not ready» before «ready» is not final
|
|
134
|
+
except TimeoutError:
|
|
135
|
+
raise BleError("the camera is busy (cable unplugged?)") from None
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async def upload(client, queues: dict[str, asyncio.Queue], wire: bytes) -> None:
|
|
139
|
+
"""Send a restore, one acknowledged frame at a time. Only «file received» after the last frame is a success."""
|
|
140
|
+
if client.mtu_size < MTU:
|
|
141
|
+
raise BleError(f"the link carries only {client.mtu_size} bytes per packet, {MTU} are needed")
|
|
142
|
+
allowed, result, fs = WRITABLE | UPLOADING, b"\2\0", frames(wire) # 2 = cancel
|
|
143
|
+
await write(client, RESTORE_REQUEST, b"\1\0", allowed)
|
|
144
|
+
try:
|
|
145
|
+
await ready(queues, RESTORE_STATE)
|
|
146
|
+
await write(client, CHUNK_SIZE, PART.to_bytes(4, "little"))
|
|
147
|
+
await write(client, FILE_INDEX, bytes(4))
|
|
148
|
+
await write(client, FILE_INFO, b"backup.dat".ljust(40, b"\0") + len(wire).to_bytes(4, "little"), allowed)
|
|
149
|
+
if (s := await state(queues, TRANSFER)) != 1:
|
|
150
|
+
raise BleError(f"the camera did not start the restore (state {s})")
|
|
151
|
+
for i, frame in enumerate(fs, 1):
|
|
152
|
+
await write(client, CHUNK, frame, allowed)
|
|
153
|
+
s = await state(queues, TRANSFER)
|
|
154
|
+
if s != (3 if i == len(fs) else 2):
|
|
155
|
+
if s == 0:
|
|
156
|
+
result = b"\0\0"
|
|
157
|
+
raise BleError(f"the camera refused the restore (state {s} after frame {i} of {len(fs)})")
|
|
158
|
+
result = b"\3\0"
|
|
159
|
+
finally:
|
|
160
|
+
try:
|
|
161
|
+
await write(client, RESULT, result)
|
|
162
|
+
except Exception:
|
|
163
|
+
if result == b"\3\0":
|
|
164
|
+
raise # whether it was taken is open: the caller reads back
|
|
165
|
+
await asyncio.sleep(SETTLE)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
async def download(client, queues: dict[str, asyncio.Queue]) -> bytes:
|
|
169
|
+
asm, ok = Assembler(), False
|
|
170
|
+
await write(client, BACKUP_REQUEST, b"\1\0")
|
|
171
|
+
try:
|
|
172
|
+
await ready(queues, BACKUP_STATE)
|
|
173
|
+
await write(client, CHUNK_SIZE, PART.to_bytes(4, "little"))
|
|
174
|
+
await write(client, FILE_INDEX, bytes(4))
|
|
175
|
+
while (s := await state(queues, TRANSFER)) in (1, 2):
|
|
176
|
+
asm.add(bytes(await asyncio.wait_for(client.read_gatt_char(CHUNK), 10)))
|
|
177
|
+
if s != 3 or not asm.done:
|
|
178
|
+
raise BleError(f"transfer ended in state {s}, last chunk seen: {asm.done}")
|
|
179
|
+
ok = True
|
|
180
|
+
return asm.data
|
|
181
|
+
finally:
|
|
182
|
+
try:
|
|
183
|
+
await write(client, RESULT, b"\3\0" if ok else b"\0\0") # tell the camera the transfer is over
|
|
184
|
+
except Exception:
|
|
185
|
+
pass # the link is gone or will be dropped; the first error counts
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
async def find(seconds: float, match):
|
|
189
|
+
from bleak import BleakScanner
|
|
190
|
+
|
|
191
|
+
seen = {}
|
|
192
|
+
async with BleakScanner() as scanner:
|
|
193
|
+
try:
|
|
194
|
+
async with asyncio.timeout(seconds):
|
|
195
|
+
async for dev, adv in scanner.advertisement_data():
|
|
196
|
+
name = adv.local_name or dev.name
|
|
197
|
+
if COMPANY in adv.manufacturer_data or is_xt3(name):
|
|
198
|
+
seen[dev.address] = f"{name} {adv.service_uuids}"
|
|
199
|
+
if (hit := match(dev, adv, name)) is not None:
|
|
200
|
+
return dev, hit
|
|
201
|
+
except TimeoutError:
|
|
202
|
+
pass
|
|
203
|
+
raise NotFound("Camera not found over Bluetooth. Fujifilm devices seen: " + ("; ".join(seen.values()) or "none"))
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
async def register(client, key: bytes) -> dict[str, asyncio.Queue]:
|
|
207
|
+
"""Present the key and subscribe. Returns a queue of state messages per characteristic."""
|
|
208
|
+
have = {c.uuid.lower() for s in client.services for c in s.characteristics}
|
|
209
|
+
if missing := (WRITABLE | UPLOADING | {TRANSFER, BACKUP_STATE, RESTORE_STATE}) - have:
|
|
210
|
+
raise BleError(f"not the expected GATT table, missing: {sorted(missing)}")
|
|
211
|
+
await asyncio.wait_for(client.read_gatt_char(SERIAL), 10)
|
|
212
|
+
await write(client, KEY, key)
|
|
213
|
+
await write(client, NAME, CLIENT)
|
|
214
|
+
queues = {u: asyncio.Queue() for u in (BACKUP_STATE, RESTORE_STATE, TRANSFER)}
|
|
215
|
+
for s in client.services:
|
|
216
|
+
for c in s.characteristics:
|
|
217
|
+
if not {"notify", "indicate"} & set(c.properties):
|
|
218
|
+
continue
|
|
219
|
+
uuid = c.uuid.lower()
|
|
220
|
+
handler = (lambda _, d, q=queues.get(uuid): q.put_nowait(bytes(d)) if q else None)
|
|
221
|
+
try:
|
|
222
|
+
await asyncio.wait_for(client.start_notify(c, handler), 10)
|
|
223
|
+
except Exception as e:
|
|
224
|
+
if uuid in queues:
|
|
225
|
+
raise BleError(f"cannot subscribe to {uuid}: {e!r}") from e
|
|
226
|
+
await asyncio.sleep(3) # dialin-tools waits before a request; whether the X-T3 needs it is unknown
|
|
227
|
+
if not client.is_connected:
|
|
228
|
+
raise BleError("the camera closed the connection")
|
|
229
|
+
return queues
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _save(path: Path, data: dict) -> None:
|
|
233
|
+
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
234
|
+
os.fchmod(fd, 0o600) # an existing file keeps its mode otherwise
|
|
235
|
+
with os.fdopen(fd, "w") as f:
|
|
236
|
+
json.dump(data, f)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class BleCameraLink:
|
|
240
|
+
"""Reads over one connection that stays open; reconnects by itself after an error."""
|
|
241
|
+
|
|
242
|
+
connect_hint = "Unplug the cable, switch the camera on, Bluetooth ON, phone app closed …"
|
|
243
|
+
|
|
244
|
+
def __init__(self, key_path: Path, find=find, make_client=None):
|
|
245
|
+
self._key_path, self._find, self._make_client = key_path, find, make_client
|
|
246
|
+
self._client = self._queues = self._loop = None
|
|
247
|
+
self._last = float("-inf") # loop time of the last read
|
|
248
|
+
self._read_data: bytes | None = None
|
|
249
|
+
self._written = False # the next read may meet a camera that is still busy with the restore
|
|
250
|
+
|
|
251
|
+
def _run(self, coro):
|
|
252
|
+
if self._loop is None: # own thread: notifications keep coming while Fabian is asked something
|
|
253
|
+
self._loop = asyncio.new_event_loop()
|
|
254
|
+
threading.Thread(target=self._loop.run_forever, daemon=True).start()
|
|
255
|
+
fut = asyncio.run_coroutine_threadsafe(asyncio.wait_for(coro, LOOP_TIMEOUT), self._loop)
|
|
256
|
+
try:
|
|
257
|
+
return fut.result()
|
|
258
|
+
except PtpError:
|
|
259
|
+
raise
|
|
260
|
+
except KeyboardInterrupt:
|
|
261
|
+
fut.cancel()
|
|
262
|
+
raise
|
|
263
|
+
except Exception as e: # bleak has its own error per platform
|
|
264
|
+
raise BleError(f"{type(e).__name__}: {e}") from e
|
|
265
|
+
|
|
266
|
+
async def _drop(self) -> None:
|
|
267
|
+
client, self._client, self._queues = self._client, None, None
|
|
268
|
+
if client is not None:
|
|
269
|
+
try:
|
|
270
|
+
await asyncio.wait_for(client.disconnect(), 10)
|
|
271
|
+
except Exception:
|
|
272
|
+
pass # already gone
|
|
273
|
+
|
|
274
|
+
async def _connect(self, dev, key: bytes) -> None:
|
|
275
|
+
await self._drop()
|
|
276
|
+
if self._make_client is None:
|
|
277
|
+
from bleak import BleakClient
|
|
278
|
+
|
|
279
|
+
self._make_client = lambda d: BleakClient(d, timeout=20)
|
|
280
|
+
self._client = self._make_client(dev)
|
|
281
|
+
await self._client.connect()
|
|
282
|
+
self._queues = await register(self._client, key)
|
|
283
|
+
|
|
284
|
+
async def _transfer(self, saved: dict, work) -> bytes | None:
|
|
285
|
+
try:
|
|
286
|
+
if self._client is None or not self._client.is_connected:
|
|
287
|
+
dev, _ = await self._find(30, lambda d, a, name: True if d.address == saved["address"] else None)
|
|
288
|
+
await self._connect(dev, bytes.fromhex(saved["key"]))
|
|
289
|
+
loop = asyncio.get_running_loop()
|
|
290
|
+
await asyncio.sleep(max(0, self._last + GAP - loop.time()))
|
|
291
|
+
for q in self._queues.values():
|
|
292
|
+
while not q.empty():
|
|
293
|
+
q.get_nowait()
|
|
294
|
+
try:
|
|
295
|
+
return await work(self._client, self._queues)
|
|
296
|
+
finally:
|
|
297
|
+
self._last = loop.time()
|
|
298
|
+
except BaseException: # also a cancelled read: the state of the transfer is unknown, start afresh
|
|
299
|
+
await self._drop()
|
|
300
|
+
raise
|
|
301
|
+
|
|
302
|
+
def _saved(self) -> dict:
|
|
303
|
+
return json.loads(self._key_path.read_text())
|
|
304
|
+
|
|
305
|
+
def read(self) -> bytes:
|
|
306
|
+
for tries in range(3 if self._written else 1, 0, -1):
|
|
307
|
+
try:
|
|
308
|
+
self._read_data = unpack(self._run(self._transfer(self._saved(), download)))
|
|
309
|
+
break
|
|
310
|
+
except PtpError:
|
|
311
|
+
if tries == 1:
|
|
312
|
+
raise
|
|
313
|
+
self._written = False
|
|
314
|
+
return self._read_data
|
|
315
|
+
|
|
316
|
+
@property
|
|
317
|
+
def can_write(self) -> bool:
|
|
318
|
+
"""Set by the write test. Pairing again takes it back."""
|
|
319
|
+
return self._saved().get("write_tested") is True
|
|
320
|
+
|
|
321
|
+
def allow_writes(self) -> None:
|
|
322
|
+
_save(self._key_path, {**self._saved(), "write_tested": True})
|
|
323
|
+
|
|
324
|
+
def write(self, data: bytes) -> None:
|
|
325
|
+
if not self.can_write:
|
|
326
|
+
raise BleError("Writing over Bluetooth is locked, run `fuji test-write` first or use the cable")
|
|
327
|
+
self._send(data)
|
|
328
|
+
|
|
329
|
+
def write_unchanged(self, data: bytes) -> None:
|
|
330
|
+
"""For the write test: sends nothing but the backup that was just read."""
|
|
331
|
+
if data != self._read_data:
|
|
332
|
+
raise BleError("not the backup that was just read")
|
|
333
|
+
self._send(data)
|
|
334
|
+
|
|
335
|
+
def _send(self, data: bytes) -> None:
|
|
336
|
+
blob.check_xt3(data)
|
|
337
|
+
if blob.with_checksum(data) != data:
|
|
338
|
+
raise BleError("wrong checksum, nothing sent")
|
|
339
|
+
if data[blob.HEADER_AT]:
|
|
340
|
+
raise BleError("header of a USB backup, nothing sent")
|
|
341
|
+
wire = zlib.compress(data, 6) # level 6 gives the camera's own bytes, checked on five X-T3 backups
|
|
342
|
+
self._written = True # also after an error: the state of the camera is unknown then
|
|
343
|
+
try:
|
|
344
|
+
self._run(self._transfer(self._saved(), lambda c, q: upload(c, q, wire)))
|
|
345
|
+
finally:
|
|
346
|
+
self.close()
|
|
347
|
+
|
|
348
|
+
def pair(self) -> str:
|
|
349
|
+
async def go():
|
|
350
|
+
try:
|
|
351
|
+
dev, key = await self._find(
|
|
352
|
+
60, lambda d, a, name: pairing_key(name, a.service_uuids, a.manufacturer_data))
|
|
353
|
+
await self._connect(dev, key)
|
|
354
|
+
return dev, key
|
|
355
|
+
finally:
|
|
356
|
+
await self._drop()
|
|
357
|
+
|
|
358
|
+
dev, key = self._run(go())
|
|
359
|
+
_save(self._key_path, {"address": dev.address, "key": key.hex()})
|
|
360
|
+
return dev.name
|
|
361
|
+
|
|
362
|
+
def close(self) -> None:
|
|
363
|
+
if self._loop is not None:
|
|
364
|
+
try:
|
|
365
|
+
self._run(self._drop())
|
|
366
|
+
except PtpError:
|
|
367
|
+
pass # nothing left to close
|
fujistuff/blob.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Pure-bytes core for the X-T3 settings-backup blob."""
|
|
2
|
+
|
|
3
|
+
SIZE = 33404
|
|
4
|
+
MAGIC = b"FUJIFILM"
|
|
5
|
+
CHECKSUM_AT = 176
|
|
6
|
+
HEADER_AT = 71 # last character of the identity string: missing in a backup read over Bluetooth
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BlobError(ValueError):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def checksum(blob: bytes) -> int:
|
|
14
|
+
# Formula after grawji: additive u16 over [0xA8, EOF) without the field itself and 0x7AF6-0x7AF9.
|
|
15
|
+
total = sum(blob[0xA8:]) - sum(blob[176:178]) - sum(blob[0x7AF6:0x7AFA])
|
|
16
|
+
return (total + 0xFF56) & 0xFFFF
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def with_checksum(blob: bytes) -> bytes:
|
|
20
|
+
return blob[:CHECKSUM_AT] + checksum(blob).to_bytes(2, "little") + blob[CHECKSUM_AT + 2:]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def with_header_of(blob: bytes, usb: bytes) -> bytes:
|
|
24
|
+
"""Give a backup read over Bluetooth the header byte of a USB backup (outside the checksum)."""
|
|
25
|
+
return blob[:HEADER_AT] + usb[HEADER_AT:HEADER_AT + 1] + blob[HEADER_AT + 1:]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def model(blob: bytes) -> str | None:
|
|
29
|
+
if not blob.startswith(MAGIC):
|
|
30
|
+
return None
|
|
31
|
+
return blob[0x14:0x24].split(b"\0", 1)[0].decode("ascii", "replace")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def check_xt3(blob: bytes) -> None:
|
|
35
|
+
if len(blob) != SIZE:
|
|
36
|
+
raise BlobError(f"Wrong size: {len(blob)} bytes instead of {SIZE}")
|
|
37
|
+
if not blob.startswith(MAGIC):
|
|
38
|
+
raise BlobError("Not a Fujifilm backup (FUJIFILM header missing)")
|
|
39
|
+
if model(blob) != "X-T3":
|
|
40
|
+
raise BlobError(f"Wrong camera model: {model(blob)!r}, expected 'X-T3'. "
|
|
41
|
+
"fujistuff writes only to the X-T3; other cameras: `fuji pull` and `fuji recipe import`")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def diff_offsets(a: bytes, b: bytes, ignore: frozenset[int]) -> list[int]:
|
|
45
|
+
return [i for i, (x, y) in enumerate(zip(a, b)) if x != y and i not in ignore]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def ranges(offsets: list[int]) -> list[tuple[int, int]]:
|
|
49
|
+
out: list[tuple[int, int]] = []
|
|
50
|
+
for o in offsets:
|
|
51
|
+
if out and out[-1][1] == o:
|
|
52
|
+
out[-1] = (out[-1][0], o + 1)
|
|
53
|
+
else:
|
|
54
|
+
out.append((o, o + 1))
|
|
55
|
+
return out
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
camera: X100F
|
|
2
|
+
blob_size: 5660
|
|
3
|
+
volatile: []
|
|
4
|
+
slots:
|
|
5
|
+
count: 7
|
|
6
|
+
base: 3909
|
|
7
|
+
stride: 256
|
|
8
|
+
fields:
|
|
9
|
+
- id: slot.wb_mode
|
|
10
|
+
menu: IMAGE QUALITY SETTING > WHITE BALANCE
|
|
11
|
+
offset: -33
|
|
12
|
+
enum:
|
|
13
|
+
Auto: 0
|
|
14
|
+
Daylight: 1
|
|
15
|
+
Shade: 2
|
|
16
|
+
Fluorescent1: 3
|
|
17
|
+
Fluorescent2: 4
|
|
18
|
+
Fluorescent3: 5
|
|
19
|
+
Incandescent: 6
|
|
20
|
+
Underwater: 7
|
|
21
|
+
Kelvin: 8
|
|
22
|
+
status: read
|
|
23
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
24
|
+
- id: slot.wb_kelvin
|
|
25
|
+
menu: IMAGE QUALITY SETTING > WHITE BALANCE > COLOR TEMPERATURE (K)
|
|
26
|
+
offset: -32
|
|
27
|
+
index:
|
|
28
|
+
- 10000
|
|
29
|
+
- 9100
|
|
30
|
+
- 8300
|
|
31
|
+
- 7700
|
|
32
|
+
- 7100
|
|
33
|
+
- 6700
|
|
34
|
+
- 6300
|
|
35
|
+
- 5900
|
|
36
|
+
- 5600
|
|
37
|
+
- 5300
|
|
38
|
+
- 5000
|
|
39
|
+
- 4800
|
|
40
|
+
- 4500
|
|
41
|
+
- 4300
|
|
42
|
+
- 4200
|
|
43
|
+
- 4000
|
|
44
|
+
- 3800
|
|
45
|
+
- 3700
|
|
46
|
+
- 3600
|
|
47
|
+
- 3400
|
|
48
|
+
- 3300
|
|
49
|
+
- 3200
|
|
50
|
+
- 3100
|
|
51
|
+
- 3000
|
|
52
|
+
- 2950
|
|
53
|
+
- 2850
|
|
54
|
+
- 2800
|
|
55
|
+
- 2700
|
|
56
|
+
- 2650
|
|
57
|
+
- 2550
|
|
58
|
+
- 2500
|
|
59
|
+
status: read
|
|
60
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
61
|
+
- id: slot.noise_reduction
|
|
62
|
+
menu: IMAGE QUALITY SETTING > NOISE REDUCTION
|
|
63
|
+
offset: -7
|
|
64
|
+
linear:
|
|
65
|
+
scale: 1
|
|
66
|
+
bias: 4
|
|
67
|
+
min: -4
|
|
68
|
+
max: 4
|
|
69
|
+
status: read
|
|
70
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
71
|
+
- id: slot.film_simulation
|
|
72
|
+
menu: IMAGE QUALITY SETTING > FILM SIMULATION
|
|
73
|
+
offset: 0
|
|
74
|
+
enum:
|
|
75
|
+
Provia: 0
|
|
76
|
+
Astia: 1
|
|
77
|
+
Velvia: 3
|
|
78
|
+
Sepia: 5
|
|
79
|
+
Monochrome: 7
|
|
80
|
+
Monochrome+R: 8
|
|
81
|
+
Monochrome+Ye: 9
|
|
82
|
+
Monochrome+G: 10
|
|
83
|
+
Pro Neg. Std: 11
|
|
84
|
+
Pro Neg. Hi: 12
|
|
85
|
+
Classic Chrome: 13
|
|
86
|
+
Acros: 14
|
|
87
|
+
Acros+R: 15
|
|
88
|
+
Acros+Ye: 16
|
|
89
|
+
Acros+G: 17
|
|
90
|
+
status: read
|
|
91
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
92
|
+
- id: slot.dynamic_range
|
|
93
|
+
menu: IMAGE QUALITY SETTING > DYNAMIC RANGE
|
|
94
|
+
offset: 3
|
|
95
|
+
enum:
|
|
96
|
+
Auto: 0
|
|
97
|
+
DR100: 1
|
|
98
|
+
DR200: 2
|
|
99
|
+
DR400: 3
|
|
100
|
+
status: read
|
|
101
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
102
|
+
- id: slot.color
|
|
103
|
+
menu: IMAGE QUALITY SETTING > COLOR
|
|
104
|
+
offset: 7
|
|
105
|
+
enum:
|
|
106
|
+
4: 3
|
|
107
|
+
3: 4
|
|
108
|
+
2: 5
|
|
109
|
+
1: 6
|
|
110
|
+
0: 0
|
|
111
|
+
-1: 8
|
|
112
|
+
-2: 7
|
|
113
|
+
-3: 9
|
|
114
|
+
-4: 10
|
|
115
|
+
status: read
|
|
116
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
117
|
+
- id: slot.sharpness
|
|
118
|
+
menu: IMAGE QUALITY SETTING > SHARPNESS
|
|
119
|
+
offset: 9
|
|
120
|
+
linear:
|
|
121
|
+
scale: -1
|
|
122
|
+
bias: 4
|
|
123
|
+
min: -4
|
|
124
|
+
max: 4
|
|
125
|
+
status: read
|
|
126
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
127
|
+
- id: slot.highlight
|
|
128
|
+
menu: IMAGE QUALITY SETTING > HIGHLIGHT TONE
|
|
129
|
+
offset: 10
|
|
130
|
+
linear:
|
|
131
|
+
scale: -1
|
|
132
|
+
bias: 4
|
|
133
|
+
min: -2
|
|
134
|
+
max: 4
|
|
135
|
+
status: read
|
|
136
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
137
|
+
- id: slot.shadow
|
|
138
|
+
menu: IMAGE QUALITY SETTING > SHADOW TONE
|
|
139
|
+
offset: 11
|
|
140
|
+
linear:
|
|
141
|
+
scale: -1
|
|
142
|
+
bias: 4
|
|
143
|
+
min: -2
|
|
144
|
+
max: 4
|
|
145
|
+
status: read
|
|
146
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|
|
147
|
+
- id: slot.grain
|
|
148
|
+
menu: IMAGE QUALITY SETTING > GRAIN EFFECT
|
|
149
|
+
offset: 12
|
|
150
|
+
enum:
|
|
151
|
+
Strong: 0
|
|
152
|
+
Weak: 1
|
|
153
|
+
'Off': 2
|
|
154
|
+
status: read
|
|
155
|
+
source: grawji backup_recipe.py _GEN3 (measured on an X100F)
|