Repository navigation
Fix Kamstrup timestamp timezone decode (#1191) + latent hour-array OOB guard - #1227
Merged
Merged
Conversation
setHourImport/getHourImport/setHourExport/getHourExport guarded with `hour > 24`, allowing hour == 24 to index day.hImport[24] / hExport[24] — one past the 24-element arrays, whose neighbour is lastMeterReadTime. No current caller passes 24 (the clearing loops use 24 as an exclusive bound and breakTime only yields 0..23), so this is latent, but a single refactor could activate it. Use `hour >= 24`. The month-day accessors are already correct: they index dImport[day-1] with a `day > 31` guard, so day 31 maps to index 30. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…d -1h Kamstrup meters leave the DLMS clock `deviation` (UTC offset) field unspecified (0x8000) — verified across every timestamped Kamstrup fixture in the corpus. The decoder compensated with a hardcoded `ts -= 3600`, which is only correct for CET. On a Finnish meter (EET, issue #1191) the meter timestamp came out an hour+ off, and it was wrong for any non-CET zone in general. Ground truth (a Norwegian capture with a known-good NTP clock: meter 21:14 CET vs true 20:14 UTC, in summer) confirms Kamstrup reports its clock in local STANDARD time year-round — it does not spring forward for DST. So convert deterministically: treat the wall-clock fields as the configured zone's standard local time and subtract that zone's standard UTC offset (probed from `tz` at a mid-winter date). The DST status bit is intentionally ignored — the clock value is standard time regardless. Aidon's handling is left unchanged. This keeps CET correct in both seasons (and removes the previous libc-mktime dependence) while fixing EET and other zones. Adds test_kamstrup_timezone covering CET winter, CET summer and EET (Jonas's frame from #1191 as fixture gh1191-1.hex), plus the native Timezone stub gains a standard offset and the harness can decode with a given timezone. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Three small robustness fixes from Arne's investigation notes on #1191: 1. configTime() daylight offset: the 2nd argument is the incremental DST shift, but the code passed the full summer offset (toLocal(JULY1970)-JULY1970), double-counting the base offset during DST. Subtract the standard offset so it's the ~3600 s delta. Currently inert (nothing calls localtime(); the only mktime() forces isdst=0), but correct and future-proof. 2. Meter-clock seed is now one-shot. The boot-time fallback that seeds the system clock from the meter timestamp (when time() < BuildEpoch) could re-fire if the clock ever regressed mid-uptime, permanently overwriting a good clock with a meter time that some meters decode with an offset error. Guard it so it only runs once per boot. 3. thresholds[] out-of-bounds read: the tariff-step loop tested thresholds[idx] before the idx<10 bound (short-circuit order), reading thresholds[10] on a 10-element array once the index reached 10. Check the bound first, and clamp getCurrentThreshold() to 0..9. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Member
Author
|
Added a third commit with three small robustness fixes from Arne's #1191 investigation notes (verified against the code):
Build note
|
🔧 PR Build ArtifactsVersion: All environments built successfully. Download the zip files:
|
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.
Two decoder fixes found while investigating #1191.
1. Kamstrup meter-timestamp timezone decode (the #1191 "1 hour off")
Kamstrup meters leave the DLMS clock
deviation(UTC offset) field unspecified (0x8000) — confirmed across every timestamped Kamstrup fixture in the corpus. The decoder compensated with a hardcodedts -= 3600inadjustForKnownIssues(), which is only correct for CET. On a Finnish meter (EET) the timestamp came out 1–2 h off, and it was wrong for any non-CET zone generally.Ground truth (a Norwegian capture with a healthy NTP clock — meter
21:14CET vs true20:14UTC, in summer) confirms Kamstrup reports its clock in local standard time year-round — it does not spring forward for DST, and the DST status bit (when set) is only informational.Fix: treat the wall-clock fields as the configured zone's standard local time and subtract that zone's standard UTC offset (probed from
tzat a mid-winter date). Deterministic (no more libc-mktimedependence), CET stays correct in both seasons, EET/other zones are now correct. Aidon's path is unchanged.Caveat / open item
We do not have a Finnish capture with a reliable clock to 100% confirm Finnish Kamstrup also reports standard time year-round (the #1191 reporter's device clock was itself days off). This fix is a strict improvement regardless — it's exact for CET/CEST and never worse than the old behaviour for EET. If Finnish meters turn out to spring forward, only the summer offset would need revisiting.
Tests
test_kamstrup_timezonepins the conversion across CET winter, CET/CEST summer, and EET:wall − 1hgh1191-1.hex) →wall − 2hThe native
Timezonestub gained a standard offset, and the harness can now decode with a supplied timezone. The golden master is unaffected (it doesn't capture meter timestamps).gh1191-1.hexis documented in the payload README and loaded directly by the test; I deliberately did not regeneratemanifest.json/fixtures_generated.h/the golden here, because doing so also pulls in pre-existing, unrelated drift (an untracked elgama fixture and an order-dependent stale value ingh175-2's golden row) — best handled separately.2. Latent out-of-bounds guard in hourly accessors
setHourImport/getHourImport/setHourExport/getHourExportguarded withhour > 24, allowinghour == 24to index the 24-elementhImport[24]/hExport[24]arrays one past the end (neighbour:lastMeterReadTime). No current caller passes 24, so it's latent — changed tohour >= 24. (The month-day accessors are already correct — they indexdImport[day-1].)Testing
pio test -e native→ 16/16 pass (incl. newtest_kamstrup_timezone, golden unchanged)pio run -e esp8266devand-e esp32dev→ build SUCCESSRelated: #1191. Companion to the NTP-freshness diagnostic in #1221.
🤖 Generated with Claude Code