Skip to content

Guard offset commands on module state - #273

Open
ctbjdm wants to merge 1 commit into
scottbez1:masterfrom
ctbjdm:fix-offset-state-guard
Open

ctbjdm wants to merge 1 commit into
scottbez1:masterfrom
ctbjdm:fix-offset-state-guard

Conversation

@ctbjdm

@ctbjdm ctbjdm commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Problem

IncreaseOffset() and SetOffset() in firmware/src/splitflap_module.h
mutate offset_steps before calling GoToTargetFlapIndex(), and neither
checks module state. GoToTargetFlapIndex() returns early unless the module
is NORMAL.

The result is that a module in SENSOR_ERROR accepts an offset change it
never physically applies. The stored offset and the hardware disagree, and
SaveAllOffsets() will then write that value to config.pb — so the
corruption survives a reboot.

SetOffset() is the more damaging of the two, since offset_steps = current_step adopts a step count that is only meaningful while the module
is actually tracking its position.

Reproduction

On a Chainlink chain with an unpopulated (or otherwise faulted) module:

  1. Confirm the module reports SENSOR_ERROR via request_state.
  2. Send INCREASE_OFFSET_TENTH to it. The module does not move — expected.
  3. Read state again: offset_steps has changed regardless.
  4. Send SAVE_ALL_OFFSETS. The bad value is now in config.pb.

Observed on an ESP32 Chainlink Buddy driving a six-module chain.

Change

Check state != NORMAL and return before mutating, in both functions.

This matches the guard GoToFlapIndex() already performs a few functions
above, which checks state before assigning target_flap_index. No call site
changes: the three callers in splitflap_task.cpp
(QCMD_INCR_OFFSET_TENTH, QCMD_INCR_OFFSET_HALF, QCMD_SET_OFFSET)
already ignore any result, and sit in the same switch as GoToFlapIndex(),
which guards internally.

I used strict NORMAL rather than also allowing LOOK_FOR_HOME the way
GoToFlapIndex() does, because homing is what establishes the reference
offset_steps is measured against — changing the offset partway through
seems like a race worth avoiding. Happy to relax it if you'd rather the two
be consistent.

Builds clean under chainlink_ota.

Known limitation

Rejection is silent, matching the existing style in this file. A user
nudging a faulted module gets no feedback — better than silent corruption,
but not good. Surfacing it in the protocol felt like a separate change and
a broader discussion, so I left it out.

IncreaseOffset() and SetOffset() mutate offset_steps before calling
GoToTargetFlapIndex(), which returns early unless the module is NORMAL.
A module in SENSOR_ERROR therefore accepts an offset change it never
physically applies, and a subsequent SaveAllOffsets() writes that value
to config.pb.

Check state before mutating, matching the existing guard in
GoToFlapIndex().

This branch has not been deployed

No deployments
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