Repository navigation
Expand file tree
/
Copy pathvalidator.py
More file actions
76 lines (56 loc) · 3.07 KB
/
Copy pathvalidator.py
File metadata and controls
76 lines (56 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Mechanical rule checks against decoded SMC readings.
Every check here is a rule against a documented sentinel or range (see
walkroad.md sections 5-6), never a diagnostic conclusion: "never claim
certainty". Sentinel/range checks only ever apply to readings that are
BOTH a temperature-shaped type AND a "T"-prefixed key (see
is_temperature_key below) -- independent of whether the key is curated in
dictionary.py, so an Unknown key still gets flagged the same way a known
one would, but a non-temperature reading (fan RPM, CPU current/power) never
does, even though Apple reuses the same fixed-point type codes for those.
"""
from dataclasses import dataclass
from typing import Optional
import decoder
# Raw 2-byte sentinel patterns AppleSMC uses for "no sensor here" (walkroad.md #5).
INVALID_RAW_SENTINELS = {b"\x80\x00", b"\x81\x00", b"\x82\x00"}
# Decoded-temperature sentinels (float-tolerant), same source.
INVALID_TEMPERATURE_VALUES = (-128.0, -127.0)
_TEMPERATURE_TOLERANCE = 0.01
# Range rules from walkroad.md section 6.
CPU_TEMPERATURE_RANGE = (20.0, 105.0)
FAN_RPM_RANGE = (1000, 6000)
REASON_SENTINEL = "sentinel_value"
REASON_NO_DATA = "no_data"
REASON_OUT_OF_RANGE = "out_of_range"
@dataclass(frozen=True)
class ValidationResult:
is_invalid: bool
is_out_of_range: bool
reason: Optional[str]
def _is_numeric(value) -> bool:
return isinstance(value, (int, float)) and not isinstance(value, bool)
def is_temperature_key(key: str) -> bool:
# Case-sensitive on purpose: the real dump has lowercase-first-letter
# keys ("tt11", "mSTC") that are NOT case variants of an uppercase
# temperature key -- e.g. "mSTC" is unrelated to the curated "MSTC" --
# so folding case here would sweep unrelated keys into "temperature".
return key.strip().startswith("T")
def validate(key: str, type_code: str, raw: bytes, decoded_value, category: str, name: str) -> ValidationResult:
type_code = type_code.strip().lower()
numeric = _is_numeric(decoded_value)
looks_like_temperature = decoder.is_temperature_shaped_type(type_code) and is_temperature_key(key)
if looks_like_temperature and raw in INVALID_RAW_SENTINELS:
return ValidationResult(is_invalid=True, is_out_of_range=False, reason=REASON_SENTINEL)
if looks_like_temperature and numeric:
for sentinel in INVALID_TEMPERATURE_VALUES:
if abs(decoded_value - sentinel) <= _TEMPERATURE_TOLERANCE:
return ValidationResult(is_invalid=True, is_out_of_range=False, reason=REASON_NO_DATA)
if category == "CPU" and numeric and looks_like_temperature:
low, high = CPU_TEMPERATURE_RANGE
if not (low <= decoded_value <= high):
return ValidationResult(is_invalid=False, is_out_of_range=True, reason=REASON_OUT_OF_RANGE)
if category == "Fan" and numeric and "speed" in name.lower():
low, high = FAN_RPM_RANGE
if not (low <= decoded_value <= high):
return ValidationResult(is_invalid=False, is_out_of_range=True, reason=REASON_OUT_OF_RANGE)
return ValidationResult(is_invalid=False, is_out_of_range=False, reason=None)