Skip to content

dafit/moyoung framing (pairs only, no decode) - #46

Merged
abdulsaheel merged 4 commits into
mainfrom
feat/dafit-moyoung-protocol
Sep 5, 2026
Merged

dafit/moyoung framing (pairs only, no decode)#46
abdulsaheel merged 4 commits into
mainfrom
feat/dafit-moyoung-protocol

Conversation

@abdulsaheel

@abdulsaheel abdulsaheel commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

adds the frame/ack/handshake bytes for the dafit/moyoung-v2 clone-watch family (m4/m6/lh716/sunset6/watch7/fit1900 style boards, sold under a pile of storefront names). pure dart, no crypto, no crc.

no signal decode here, just enough framing for edge to hold a session open and bank raw bytes.

Summary by Sourcery

Add experimental DaFit/MOYOUNG-V2 framing support to keep compatible watch sessions active while preserving raw protocol data.

New Features:

  • Add experimental DaFit/MOYOUNG-V2 protocol support for framing, acknowledgements, date/time packing, and session initialization across compatible watch families.

Enhancements:

  • Expose the DaFit protocol through the package's public library barrel.
  • Provide strict parsing and recognition for data and acknowledgement frames without decoding physiological signals.

Tests:

  • Add coverage for frame parsing, validation, acknowledgement handling, date/time packing, frame construction, and initialization sequences.

no signal decode yet, just the framing to hold a session open and bank raw bytes.
@sourcery-ai

sourcery-ai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces an experimental, publicly exported DaFit/MOYOUNG-V2 framing layer that can parse and construct data/ACK frames, pack handshake date-time data, and generate the required initialization sequence, with tests covering the wire-format behavior; it intentionally performs no signal decoding or transport integration.

Sequence diagram for the DaFit handshake and framed exchange

sequenceDiagram
    participant Edge
    participant Band

    Edge->>Band: buildDafitFrame Init1
    Edge->>Band: buildDafitFrame SetDateTime
    Edge->>Band: buildDafitFrame Init1
    Edge->>Band: buildDafitFrame Init2
    Edge->>Band: buildDafitFrame SetLanguage
    Edge->>Band: buildDafitFrame Init3
    Edge->>Band: buildDafitFrame GetHwInfo
    Edge->>Band: buildDafitFrame GetBandInfo
    Band-->>Edge: Data notification
    Edge->>Edge: parseDafitFrame
    Edge->>Band: buildDafitAck
    Band-->>Edge: ACK notification
    Edge->>Edge: isDafitAckFrame
Loading

File-Level Changes

Change Details Files
Adds a public pure-Dart DaFit/MOYOUNG-V2 framing API for session handshakes and raw byte exchange, explicitly excluding signal decoding, CRC, crypto, and transport concerns.
  • Exports the new protocol module through the package barrel.
  • Defines protocol constants for frame headers, groups, commands, and control values.
  • Parses data frames with big-endian length fields while rejecting ACK-headed and truncated inputs.
  • Builds outbound data frames and fixed-size ACK frames, preserving the received outer length for acknowledgements.
  • Packs date/time values into the documented bit layout and provides an eight-frame initialization sequence.
  • Documents the experimental, framing-only scope and expected Nordic UART transport behavior.
lib/openstrap_protocol.dart
lib/src/dafit.dart
Adds focused tests covering parsing, construction, ACK recognition, date/time packing, and handshake sequencing.
  • Verifies parsing of a captured date/time frame and rejection of ACK or truncated frames.
  • Checks data-frame round trips, empty payload framing, and ACK length derivation.
  • Validates packed date/time fields and the shape and contents of the eight-step initialization sequence.
test/dafit_test.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 51 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Team

Run ID: 00dca608-f8e4-430d-b9b8-331c91b706b3

📥 Commits

Reviewing files that changed from the base of the PR and between c78c176 and 06cb5f2.

📒 Files selected for processing (3)
  • lib/openstrap_protocol.dart
  • lib/src/dafit.dart
  • test/dafit_test.dart

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="lib/src/dafit.dart" line_range="129-130" />
<code_context>
+/// this family also uses band-to-app, interspersed with data frames during
+/// the same exchange. Not decoded further: nothing downstream needs more
+/// than "this notification was an ack, not data".
+bool isDafitAckFrame(List<int> value) =>
+    value.length == 8 && value[0] == kDafitAckHeader;
+
+/// Pack a local date/time into this family's bit-packed SET_DATE_TIME field:
</code_context>
<issue_to_address>
**issue (bug_risk):** isDafitAckFrame classifies any 8-byte packet beginning with 0xDC as a valid ACK, even when the fixed bytes at [1..2], [4], or [7] are wrong. A malformed or unrelated notification is therefore routed as an ACK instead of being rejected.

**Triggers:** When an 8-byte 0xDC-headed notification has invalid ACK fields.

**Suggested fix:** Validate the complete fixed ACK shape, including `value[1] == 0`, `value[2] == 0x05`, `value[4] == 0x01`, and `value[7] == 0x01`.

```suggestion
bool isDafitAckFrame(List<int> value) =>
    value.length == 8 &&
    value[0] == kDafitAckHeader &&
    value[1] == 0 &&
    value[2] == 0x05 &&
    value[4] == 0x01 &&
    value[7] == 0x01;
```
</issue_to_address>

### Comment 2
<location path="lib/src/dafit.dart" line_range="82" />
<code_context>
+/// delivered.
+DafitFrame? parseDafitFrame(List<int> value) {
+  if (value.length < 8 || value[0] != kDafitDataHeader) return null;
+  final outerLen = (value[1] << 8) | value[2];
+  final payloadLen = (value[6] << 8) | value[7];
+  if (value.length - 8 < payloadLen) return null;
+  return DafitFrame(
+    value[3],
</code_context>
<issue_to_address>
**issue (bug_risk):** parseDafitFrame reads and preserves outerLen but never verifies that it matches the declared payload length (`outerLen == 5 + payloadLen`). A malformed frame with inconsistent length fields is returned as valid, and buildDafitAck then echoes the invalid outer length and produces an incorrect ACK.

**Triggers:** When the band sends a data frame whose outer and payload length fields disagree.

**Suggested fix:** Reject the frame when `outerLen != 5 + payloadLen` before constructing DafitFrame.

```suggestion
  if (value.length - 8 < payloadLen || outerLen != 5 + payloadLen) return null;
```
</issue_to_address>

### Comment 3
<location path="lib/src/dafit.dart" line_range="136-141" />
<code_context>
+/// seconds, then (year-2000), month, day, hour, minute each shifted into
+/// their own bit range of one big-endian u32.
+int packDafitDateTime(DateTime t) {
+  return t.second |
+      ((t.year - 2000) << 26) |
+      (t.month << 22) |
+      (t.day << 17) |
+      (t.hour << 12) |
+      (t.minute << 6);
+}
+
</code_context>
<issue_to_address>
**nitpick (bug_risk):** packDafitDateTime does not constrain `t.year - 2000` to the documented six-bit field. Dates at or after 2064, or before 2000, spill outside the field and are truncated by the later 32-bit encoding, so the watch receives a different year.

**Triggers:** When the supplied DateTime is outside the protocol's representable year range 2000 through 2063.

**Suggested fix:** Reject unsupported years or mask and document an explicit wrapping policy before packing.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if the framing or ACK arithmetic is wrong, the watch may reject subsequent requests, stall the exchange, or drain its battery, and the initialization sequence can write an incorrect clock or language setting to the device. Reverting prevents future writes, while the device state and any battery drain already caused require a reconnect or corrective command to repair.

Blocking findings: lib/src/dafit.dart:130, lib/src/dafit.dart:82


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread lib/src/dafit.dart Outdated
Comment thread lib/src/dafit.dart Outdated
Comment thread lib/src/dafit.dart
length-mismatched frames were accepted and echoed back into acks; a
year outside 2000-2063 silently truncated in the clock write. sourcery

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@abdulsaheel
abdulsaheel merged commit 6423a4a into main Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant