Repository navigation
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
Open
gyimesibalazs wants to merge 4 commits into
gyimesibalazs wants to merge 4 commits into
Conversation
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).
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 secondsclock_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 everynow - thenacross the wrap is wrong:button.c: a button held across the wrap getshal_millis() - pressed_at_ms≈ 4·10⁹, i.e. a long press at once. On the on-board button (Bentry) a long press ishal_factory_reset().button.c: a press at the same phase of a later 268 s cycle as the previous release passeschanged_at - released_at_ms < multi_press_duration_msand counts as a multi press.poll_control_cluster.c:fast_poll_end_ms = hal_millis() + timeoutcan 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()callshal_millis()in every main loop round so no wrap is missed.2. Device configuration string longer than the buffer
device_config_str.datais 128 bytes. The stack copies a writtenLONG_CHAR_STRinto the attribute buffer without knowing its capacity, and the handler then writesdata[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()callshal_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 thenodeIsOnANetworkbranch (bdb_networkSteerNonFactoryNew()), which does not join anything: it broadcastsMgmt_Permit_Joining_reqfor 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
pytest tests: same result asmainhere (215 passed; the remaining failures are the missingstub_end_devicebinary in my environment, identical onmain).Not tested