-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdl_packets.py
193 lines (163 loc) · 5.09 KB
/
hdl_packets.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python
import crc16
import enum
from scapy.all import *
class HDLOperCode(enum.Enum):
SingleSwitchControlRequest = 0x0031
SingleSwitchControlResponse = 0x0032
ReadStatusOfChannelsRequest = 0x0033
ReadStatusOfChannelsResponse = 0x0034
ReadFloorHeatingStatusRequest = 0x1C5E
ReadFloorHeatingStatusResponse = 0x1C5F
ControlFloorHeatingStatusRequest = 0x1C5C
ControlFloorHeatingStatusResponse = 0x1C5D
ReadTemperatureRequest = 0xE3E7
ReadTemperatureResponse = 0xE3E8
class HDLSmartBus(Packet):
name = "HDLSmartBus"
fields_desc = [
XShortField("preamble0", 0xc0a8),
XShortField("preamble1", 0x0114),
XShortField("preamble2", 0x4844),
XShortField("preamble3", 0x4c4d),
XShortField("preamble4", 0x4952),
XShortField("preamble5", 0x4143),
XShortField("preamble6", 0x4c45),
XShortField("leading_code", 0xaaaa),
FieldLenField("length", None, length_of="content", fmt='B'),
XByteField("orig_subnet_id", 0xfc),
XByteField("orig_device_id", 0xfc),
XShortField("orig_device_type", 0xfffc),
XShortField("oper_code", None),
XByteField("target_subnet_id", None),
XByteField("target_device_id", None),
StrLenField("content", None, length_from=lambda pkt: pkt.length-11),
XShortField("crc", None)
]
preamble_len = 16
crc_len = 2
def post_build(self, p, pay):
if self.length is None:
tmp_len = len(p) - self.preamble_len + len(pay)
p = p[:16] + struct.pack("!B", tmp_len & 0xff) + p[17:]
if self.crc is None:
data = p[HDLSmartBus.preamble_len:-HDLSmartBus.crc_len]
crc = crc16.crc16xmodem(data)
p = p[:-2] + struct.pack("!H", crc)
return p + pay
class SingleSwitchControlRequest(Packet):
name = "SingleSwitchControlRequest"
operation_map = {
100: "ON",
0: "OFF"
}
fields_desc = [
ByteField("channel", None),
ByteEnumField("level", None, operation_map),
XShortField("padding", 0),
]
class SingleSwitchControlResponse(Packet):
name = "SingleSwitchControlResponse"
operation_map = {
100: "ON",
0: "OFF"
}
fields_desc = [
ByteField("channel", None),
ByteEnumField("return_code", None, operation_map),
ByteEnumField("level", None, operation_map),
XByteField("padding0", 0),
XByteField("padding1", 0),
XByteField("padding2", 0),
]
class ReadStatusOfChannelsResponse(Packet):
name = "ReadStatusOfChannelsResponse"
operation_map = {
100: "ON",
0: "OFF"
}
fields_desc = [
FieldLenField("length", None, length_of="content", fmt='B'),
StrLenField("channels", None, length_from=lambda pkt: pkt.length),
]
class ReadFloorHeatingStatusRequest(Packet):
name = "ReadFloorHeatingStatusRequest"
fields_desc = [
ByteField("channel", None)
]
class ReadFloorHeatingStatusResponse(Packet):
name = "ReadFloorHeatingStatusResponse"
status_map = {
0: "OFF",
1: "ON"
}
temp_unit_map = {
0: "CELS",
1: "FAHR"
}
mode_map = {
1: "NORMAL",
2: "DAY",
3: "NIGHT",
4: "AWAY",
5: "TIMER"
}
fields_desc = [
ByteField("channel", None),
ByteEnumField("status", None, status_map),
ByteEnumField("unit", None, temp_unit_map),
ByteEnumField("mode", None, mode_map),
ByteField("normal_temp", None),
ByteField("day_temp", None),
ByteField("night_temp", None),
ByteField("away_temp", None),
XShortField("timer", None),
XShortField("valve", None),
XShortField("pwd", None),
XShortField("watering", None),
XShortField("watering_time", None),
]
class ControlFloorHeatingStatusRequest(Packet):
name = "ControlFloorHeatingStatusRequest"
status_map = {
0: "OFF",
1: "ON"
}
temp_unit_map = {
0: "CELS",
1: "FAHR"
}
mode_map = {
1: "NORMAL",
2: "DAY",
3: "NIGHT",
4: "AWAY",
5: "TIMER"
}
fields_desc = [
ByteField("channel", None),
ByteEnumField("status", None, status_map),
ByteEnumField("unit", None, temp_unit_map),
ByteEnumField("mode", None, mode_map),
ByteField("normal_temp", None),
ByteField("day_temp", None),
ByteField("night_temp", None),
ByteField("away_temp", None),
XShortField("timer", None),
XShortField("valve", None),
XShortField("watering_time", None),
]
class ControlFloorHeatingStatusResponse(ControlFloorHeatingStatusRequest):
name = "ControlFloorHeatingStatusResponse"
class ReadTemperatureRequest(Packet):
name = "ReadTemperatureRequest"
fields_desc = [
ByteField("channel", None)
]
class ReadTemperatureResponse(Packet):
name = "ReadTemperatureResponse"
fields_desc = [
ByteField("channel", None),
BitField("sign", 0, 1),
BitField("temperature", 0, 7),
]