|
| 1 | +// A Casio G-Shock / current-generation Casio smartwatch, speaking the 2C/2D |
| 2 | +// "all-features" GATT scheme, as a [BandAdapter]. |
| 3 | +// |
| 4 | +// NOTHING HERE HAS MET HARDWARE. Nobody on this project owns a Casio watch, |
| 5 | +// so every byte below is verified by the compiler and by the fixtures in |
| 6 | +// `test/adapters/casio_adapter_test.dart`, never by a real device. It ships |
| 7 | +// EXPERIMENTAL (ASSUMPTIONS R6): `signals` is `const {}` and `kAdapterSignals` |
| 8 | +// carries no entry for it beyond the empty map, exactly like `kOura` and |
| 9 | +// `kBleHrs`. |
| 10 | +// |
| 11 | +// THE WIRE SHAPE IS THE SIMPLEST ONE THIS SEAM HAS SEEN. One request |
| 12 | +// characteristic, one response characteristic, no envelope, no CRC, no |
| 13 | +// sequence counter, and no stored history to drain — closer to a plain notify |
| 14 | +// sensor than to anything WHOOP-shaped. The host writes a one-byte feature tag |
| 15 | +// and the watch answers on notify with `[featureTag, ...payload]`; there is |
| 16 | +// nothing to ACK and nothing to trim. |
| 17 | +// |
| 18 | +// WHAT THIS SESSION DOES AND DOES NOT DO. It writes a small, fixed set of |
| 19 | +// harmless, read-only feature tags — version info, app info, watch name, |
| 20 | +// module id, BLE features — and banks every reply verbatim. It never writes |
| 21 | +// the clock, an alarm, a reminder or any other setting: those are |
| 22 | +// control-plane writes with device-specific side effects, and a pairs-only |
| 23 | +// adapter has no business making them. No field of any reply is decoded; a |
| 24 | +// module-id BYTE COUNT is the one thing surfaced as a [BandNote], because it |
| 25 | +// is pure device metadata (not a measurement) and unambiguous without a |
| 26 | +// decoder. |
| 27 | + |
| 28 | +import 'dart:async'; |
| 29 | +import 'dart:typed_data'; |
| 30 | + |
| 31 | +import '_registry.dart'; |
| 32 | +import 'adapter.dart'; |
| 33 | +import 'signals.dart'; |
| 34 | + |
| 35 | +/// One Casio session. Const: it holds no state of its own — everything a |
| 36 | +/// session needs lives inside [run]. |
| 37 | +class CasioAdapter extends BandAdapter { |
| 38 | + /// How long to wait for a reply to one request before moving on. A watch |
| 39 | + /// that never answers a given tag costs one skipped probe, not a stalled |
| 40 | + /// session — there is no retry and no escalation here. |
| 41 | + final Duration replyTimeout; |
| 42 | + |
| 43 | + const CasioAdapter({this.replyTimeout = const Duration(seconds: 3)}); |
| 44 | + |
| 45 | + @override |
| 46 | + BandEntry get entry => kCasio; |
| 47 | + |
| 48 | + /// NOTHING, and that is the honest answer today rather than a placeholder. |
| 49 | + /// This adapter decodes no field of any reply — every response is banked |
| 50 | + /// raw and undecoded until someone has actually held one of these watches. |
| 51 | + @override |
| 52 | + Map<InputSignal, Duration> get signals => const {}; |
| 53 | + |
| 54 | + /// The harmless, read-only feature-request tags this session proves the |
| 55 | + /// link with. Every one is a plain info read: none sets the clock, an |
| 56 | + /// alarm, a reminder or any other watch state. |
| 57 | + static const List<int> kProbeTags = <int>[ |
| 58 | + 0x10, // BLE features |
| 59 | + 0x20, // version info |
| 60 | + 0x22, // app info |
| 61 | + 0x23, // watch name |
| 62 | + 0x26, // module id |
| 63 | + ]; |
| 64 | + |
| 65 | + /// The one probe tag whose reply length is worth naming — a byte count is |
| 66 | + /// pure device metadata, never a measurement. |
| 67 | + static const int _kModuleIdTag = 0x26; |
| 68 | + |
| 69 | + @override |
| 70 | + Stream<BandEvent> run(BandLink link) async* { |
| 71 | + final inbox = _Inbox(); |
| 72 | + final sub = link.notify(kCasioAllFeaturesChar).listen( |
| 73 | + (rec) => inbox.add(Uint8List.fromList(rec.$2)), |
| 74 | + onDone: inbox.close, |
| 75 | + onError: (Object _) => inbox.close(), |
| 76 | + ); |
| 77 | + final raw = <Uint8List>[]; |
| 78 | + try { |
| 79 | + for (final tag in kProbeTags) { |
| 80 | + if (!await link.write(kCasioReadRequestChar, <int>[tag])) { |
| 81 | + link.log( |
| 82 | + 'casio: request 0x${tag.toRadixString(16)} refused; skipping.'); |
| 83 | + continue; |
| 84 | + } |
| 85 | + // A frame whose first byte does not echo this tag is not this |
| 86 | + // request's answer — an unsolicited setting notification, or a stale |
| 87 | + // reply that missed a PREVIOUS tag's timeout window and landed here |
| 88 | + // instead. Banked anyway (every frame this wire sends is still |
| 89 | + // undecoded data worth keeping) but never claimed as tag's reply, so |
| 90 | + // it can never taint the module-id length note below. |
| 91 | + final deadline = DateTime.now().add(replyTimeout); |
| 92 | + Uint8List? resp; |
| 93 | + while (true) { |
| 94 | + final left = deadline.difference(DateTime.now()); |
| 95 | + if (left <= Duration.zero) break; |
| 96 | + final frame = await inbox.next(left); |
| 97 | + if (frame == null) break; |
| 98 | + if (frame.isNotEmpty && frame[0] == tag) { |
| 99 | + resp = frame; |
| 100 | + break; |
| 101 | + } |
| 102 | + if (frame.isNotEmpty) raw.add(frame); |
| 103 | + } |
| 104 | + if (resp == null) { |
| 105 | + link.log( |
| 106 | + 'casio: no reply to request 0x${tag.toRadixString(16)}.'); |
| 107 | + continue; |
| 108 | + } |
| 109 | + raw.add(resp); |
| 110 | + if (tag == _kModuleIdTag && resp.length > 1) { |
| 111 | + yield BandNote('casio_module_id_len', resp.length - 1); |
| 112 | + } |
| 113 | + } |
| 114 | + } finally { |
| 115 | + await sub.cancel(); |
| 116 | + } |
| 117 | + // No samples, ever — nothing is decoded. The frames are handed over so a |
| 118 | + // future decoder, written when someone owns one of these watches, has |
| 119 | + // something to run over. |
| 120 | + if (raw.isNotEmpty) yield SampleBatch(const [], raw: raw); |
| 121 | + // No OffloadCheckpoint: there is no stored history on this wire to trim. |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/// The single instance. Const, so it costs nothing to reference. |
| 126 | +const CasioAdapter kCasioAdapter = CasioAdapter(); |
| 127 | + |
| 128 | +/// Frames off the notify characteristic, buffered so a reply landing before |
| 129 | +/// anyone is waiting is not dropped. Same minimal shape as `oura.dart`'s own |
| 130 | +/// inbox — this wire has no auth handshake to search past, so there is no |
| 131 | +/// `firstWhere`, only "the next frame, or nothing". |
| 132 | +class _Inbox { |
| 133 | + final List<Uint8List> _buf = []; |
| 134 | + Completer<Uint8List?>? _waiter; |
| 135 | + bool _closed = false; |
| 136 | + |
| 137 | + void add(Uint8List v) { |
| 138 | + final w = _waiter; |
| 139 | + if (w != null && !w.isCompleted) { |
| 140 | + _waiter = null; |
| 141 | + w.complete(v); |
| 142 | + return; |
| 143 | + } |
| 144 | + _buf.add(v); |
| 145 | + } |
| 146 | + |
| 147 | + void close() { |
| 148 | + _closed = true; |
| 149 | + final w = _waiter; |
| 150 | + _waiter = null; |
| 151 | + if (w != null && !w.isCompleted) w.complete(null); |
| 152 | + } |
| 153 | + |
| 154 | + Future<Uint8List?> next(Duration timeout) { |
| 155 | + if (_buf.isNotEmpty) return Future.value(_buf.removeAt(0)); |
| 156 | + if (_closed) return Future.value(null); |
| 157 | + final w = Completer<Uint8List?>(); |
| 158 | + _waiter = w; |
| 159 | + return w.future.timeout(timeout, onTimeout: () { |
| 160 | + if (identical(_waiter, w)) _waiter = null; |
| 161 | + return null; |
| 162 | + }); |
| 163 | + } |
| 164 | +} |
0 commit comments