-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHighLevelAnalyzer.py
175 lines (128 loc) · 4.6 KB
/
HighLevelAnalyzer.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# SPDX-FileCopyrightText: 2021 Diego Elio Pettenò
#
# SPDX-License-Identifier: MIT
import functools
import operator
from typing import Any, Dict, List, Optional
from saleae.analyzers import HighLevelAnalyzer, AnalyzerFrame
from saleae.data import GraphTime, GraphTimeDelta
_PACKETS_DISTANCE = GraphTimeDelta(1)
def _checksum(packet):
return (functools.reduce(operator.add, packet) & 0xFF) ^ 0x55
_FAN_SPEED = {
0x00: "low",
0x10: "medium",
0x20: "high",
0x40: "power",
}
_MODE = {
0x00: "Cool",
0x10: "dH",
0x20: "Fan",
0x40: "Heat",
}
def annotate_hvac_packet(packet_data: bytes, packet_attributes: Dict[str, Any]) -> None:
packet_attributes["source"] = "HVAC"
if not (packet_data[1] & 0x80):
packet_attributes["room_temperature"] = (packet_data[1] / 2) + 10
packet_attributes["unknown_data"] = bytes(
(
packet_data[0],
0 if "room_temperature" in packet_attributes else packet_data[1],
packet_data[2],
packet_data[3],
packet_data[4],
)
)
def annotate_panel_packet(
packet_data: bytes, packet_attributes: Dict[str, Any]
) -> None:
packet_attributes["source"] = "Panel"
if packet_data[0] & 0x90 == 0x90:
packet_attributes["features_inquiry"] = True
if packet_data[1:4] != b"\x00\x00\x00\x00":
packet_attributes["unknown_data"] = b"\x00" + packet_data[1:4]
else:
mode_val = packet_data[0] & 0x70
packet_attributes["mode"] = _MODE.get(mode_val, hex(mode_val))
packet_attributes["resistor?"] = bool(packet_data[0] & 0x08)
packet_attributes["running"] = bool(packet_data[0] & 0x04)
packet_attributes["settings_changed"] = bool(packet_data[0] & 0x01)
packet_attributes["room_temperature"] = (packet_data[1] / 2) + 10
packet_attributes["plasma"] = bool(packet_data[2] & 0x80)
packet_attributes["fan_speed"] = _FAN_SPEED[packet_data[2] & 0x70]
packet_attributes["set_temperature"] = str((packet_data[2] & 0x0F) + 16)
packet_attributes["swivel"] = bool(packet_data[3] & 0x20)
packet_attributes["swirl"] = bool(packet_data[4] & 0x01)
packet_attributes["unknown_data"] = bytes(
(
packet_data[0] & 0x02,
0,
0,
packet_data[3] & ~0x20,
packet_data[4] & ~0x01,
)
)
def recompose_packet(
packet_frames: List[AnalyzerFrame], last_end: Optional[GraphTime]
) -> AnalyzerFrame:
packet_data = b""
for frame in packet_frames:
packet_data += frame.data["data"]
received_checksum = packet_data[-1]
calculated_checksum = _checksum(packet_data[:-1])
if received_checksum != calculated_checksum:
return AnalyzerFrame(
"invalid_checksum",
packet_frames[0].start_time,
packet_frames[-1].end_time,
{"data": packet_data.hex()},
)
packet_attributes = {
"data": packet_data[:-1].hex(),
"checksum": received_checksum,
}
packet_distance = (
(packet_frames[0].start_time - last_end)
if last_end
else GraphTimeDelta(second=30)
)
if packet_distance < GraphTimeDelta(millisecond=300):
# This was a response from the Controller, so source is HVAC.
annotate_hvac_packet(packet_data, packet_attributes)
else:
annotate_panel_packet(packet_data, packet_attributes)
return AnalyzerFrame(
"valid_packet",
packet_frames[0].start_time,
packet_frames[-1].end_time,
packet_attributes,
)
# High level analyzers must subclass the HighLevelAnalyzer class.
class Hla(HighLevelAnalyzer):
result_types = {
"invalid_checksum": {"format": "Invalid Checksum: {{data.packet}}"},
"valid_packet": {"format": "{{data.source}} {{data.data}}"},
}
def __init__(self):
"""
Initialize HLA.
Settings can be accessed using the same name used above.
"""
self._last_start = None
self._last_end = None
self._packet = []
def decode(self, frame: AnalyzerFrame):
if frame.type != "data":
return
if (
self._packet
and (frame.end_time - self._packet[-1].end_time) > _PACKETS_DISTANCE
):
self._packet = []
self._packet.append(frame)
if len(self._packet) == 6:
analyzed_frame = recompose_packet(self._packet, self._last_end)
self._packet = []
self._last_end = analyzed_frame.end_time
return analyzed_frame