Skip to content

Fix hal_millis() wrap on Telink, config string overflow, steering of joined devices, first input read - #555

Open
gyimesibalazs wants to merge 4 commits into
romasku:mainfrom
gyimesibalazs:fix-timer-config-steering-input
Open

gyimesibalazs wants to merge 4 commits into
romasku:mainfrom
gyimesibalazs:fix-timer-config-steering-input

Conversation

@gyimesibalazs

Copy link
Copy Markdown

Four small fixes found while reading the code for #554. Each is its own commit, so they can be taken or dropped separately. Unlike #554 these come from code reading, not from a failure I reproduced; what was and was not tested is listed at the end.

1. Telink: hal_millis() wraps every 268 seconds

clock_time() is a 32-bit counter of 16 MHz ticks. hal_millis() divides it, so the millisecond value jumps back to 0 every 2^32 / 16 MHz = 268 s, and every now - then across the wrap is wrong:

  • button.c: a button held across the wrap gets hal_millis() - pressed_at_ms ≈ 4·10⁹, i.e. a long press at once. On the on-board button (B entry) a long press is hal_factory_reset().
  • button.c: a press at the same phase of a later 268 s cycle as the previous release passes changed_at - released_at_ms < multi_press_duration_ms and counts as a multi press.
  • poll_control_cluster.c: fast_poll_end_ms = hal_millis() + timeout can lie behind the wrap; hal_millis() never reaches it and the device stays in fast poll.
  • cover_cluster.c: elapsed movement time.

The chance per press is small (hold time / 268 s), but it is there for every device all the time. The fix accumulates the elapsed ticks; app_task() calls hal_millis() in every main loop round so no wrap is missed.

2. Device configuration string longer than the buffer

device_config_str.data is 128 bytes. The stack copies a written LONG_CHAR_STR into the attribute buffer without knowing its capacity, and the handler then writes data[size] = 0. A configuration of 128 characters or more overwrites what follows the buffer. Now the struct has room behind the stored part to take such a write, the handler rejects it and reloads the stored configuration. The NV item keeps its size (DEVICE_CONFIG_NV_SIZE), so existing devices keep their configuration.

3. Telink: steering on a device that is already on a network

app_task() calls hal_zigbee_start_network_steering() whenever the device is not joined. For a device that has network data and is only out of reach, bdb_networkSteerStart() takes the nodeIsOnANetwork branch (bdb_networkSteerNonFactoryNew()), which does not join anything: it broadcasts Mgmt_Permit_Joining_req for 180 s. The stack rejoins such a device by itself, so steering is now only started for a factory new device.

4. First read of an input

btn_init() reads the pin right after the pull resistor is switched on. If the line has not settled (long switch wire, restart), the wrong level becomes the initial state and the real one shows up as a change shortly after; in toggle mode that switches the relay. This would fit "relay toggles at a spontaneous restart, not every time" in #289, but I could not reproduce that issue, so treat it as a plausible cause only. The first read now waits until the level holds for 3 ms (at most 30 ms); the simulated build reads once as before.

Tested

Not tested

  • The wrap itself (waiting for a press to fall on it), the oversized configuration write, and the permit-join broadcast were not observed on a device.
  • Silicon Labs builds are not affected by 1 and 3 and were not built.

clock_time() is a 32-bit counter of 16 MHz ticks and wraps every 268 seconds.
hal_millis() divided it, so the millisecond value jumped back to 0 at each
wrap and every 'now - then' in the callers went wrong:
- a button held across the wrap was reported as a long press at once (on the
  on-board button a long press is a factory reset),
- a press at the same phase of a later 268 s cycle as the previous release
  counted as a multi press,
- on end devices a fast poll period whose end fell behind the wrap never ended.

Accumulate the elapsed ticks instead; app_task() calls hal_millis() in every
main loop round so that no wrap is missed.
The Zigbee stack copies a written long string into the attribute buffer
without knowing its capacity, and the write handler then terminated it at
data[size]. A configuration of 128 characters or more overwrote the variables
behind the 128-byte buffer.

The buffer gets room behind the stored part to take such a write, the handler
rejects it and restores the stored configuration. The stored item keeps its
size.
…a network

app_task() starts steering whenever the device is not joined. For a device
that has network data and is only out of reach (an end device that lost its
parent), BDB steering takes the 'node is on a network' branch: it does not
join anything, it broadcasts Mgmt_Permit_Joining_req and opens the whole
network for 180 seconds. The stack rejoins such a device by itself.
btn_init() read the pin right after the pull resistor was switched on. If the
line had not reached its level yet (long switch wire, restart), the wrong
value was taken as the initial state and the real level showed up as a change
a moment later; in toggle mode every change switches the relay.

The first read now waits until the level holds for 3 ms (at most 30 ms).
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