-
Notifications
You must be signed in to change notification settings - Fork 5
/
zbdecode
executable file
·61 lines (46 loc) · 1.46 KB
/
zbdecode
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
#!/usr/bin/env python3
# Decode hex dumps of ZigBee packets on stdin
import sys
from binascii import unhexlify
from ZbPy import AES
from ZbPy import IEEE802154
from ZbPy import ZigbeeNetwork
from ZbPy import ZigbeeApplication
from ZbPy import ZigbeeCluster
from ZbPy import Parser
# This is the "well known" zigbee2mqtt key.
# The Ikea gateway uses a different key that has to be learned
# by joining the network (not yet implemented)
nwk_key = unhexlify("01030507090b0d0f00020406080a0c0d")
aes = AES.AES(nwk_key)
def process_packet(data, verbose=False):
#print(data)
ieee = IEEE802154.IEEE802154(data=data)
if verbose: print("IEEE:", ieee)
if ieee.frame_type != IEEE802154.FRAME_TYPE_DATA or len(ieee.payload) == 0:
return ieee
nwk = ZigbeeNetwork.ZigbeeNetwork(data=ieee.payload, aes=aes)
if verbose: print("NWK:", nwk)
ieee.payload = nwk
if nwk.frame_type != ZigbeeNetwork.FRAME_TYPE_DATA:
return ieee
aps = ZigbeeApplication.ZigbeeApplication(data=nwk.payload)
nwk.payload = aps
if verbose: print("APS:", aps)
if aps.frame_type != ZigbeeApplication.FRAME_TYPE_DATA:
return ieee
# join requests are "special" for now
if aps.cluster == 0x36:
print("JOIN:", ieee)
return ieee
zcl = ZigbeeCluster.ZigbeeCluster(data=aps.payload)
aps.payload = zcl
if verbose: print("ZCL:", ieee)
return ieee
while True:
line = sys.stdin.readline()
if not line:
break
data = bytearray(unhexlify(line.rstrip()))
ieee = process_packet(data, verbose=True)
print(ieee)