Skip to content

Instantly share code, notes, and snippets.

@gnuheidix
Created June 8, 2021 20:48
Show Gist options
  • Save gnuheidix/4e63a3d2a8c38048a7c319d258f06134 to your computer and use it in GitHub Desktop.
Save gnuheidix/4e63a3d2a8c38048a7c319d258f06134 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import sys, fcntl, time
def decrypt(key, data):
cstate = [0x48, 0x74, 0x65, 0x6D, 0x70, 0x39, 0x39, 0x65]
shuffle = [2, 4, 0, 7, 1, 6, 5, 3]
phase1 = [0] * 8
for i, o in enumerate(shuffle):
phase1[o] = data[i]
phase2 = [0] * 8
for i in range(8):
phase2[i] = phase1[i] ^ key[i]
phase3 = [0] * 8
for i in range(8):
phase3[i] = ( (phase2[i] >> 3) | (phase2[ (i-1+8)%8 ] << 5) ) & 0xff
ctmp = [0] * 8
for i in range(8):
ctmp[i] = ( (cstate[i] >> 4) | (cstate[i]<<4) ) & 0xff
out = [0] * 8
for i in range(8):
out[i] = (0x100 + phase3[i] - ctmp[i]) & 0xff
return out
def hd(d):
return " ".join("%02X" % e for e in d)
if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == "config":
print("co2mini.label CO2 in ppm")
print("graph_vlabel ppm")
print("graph_title CO2-Niveau")
print("graph_category wohnung")
sys.exit(0)
fp = open("/dev/co2mini0", "a+b", 0)
HIDIOCSFEATURE_9 = 0xC0094806
# Key retrieved from /dev/random, guaranteed to be random ;)
key = [0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96]
set_report = "\x00" + "".join(chr(e) for e in key)
fcntl.ioctl(fp, HIDIOCSFEATURE_9, set_report)
values = {}
while True:
data = list(ord(e) for e in fp.read(8))
decrypted = decrypt(key, data)
if decrypted[4] != 0x0d or (sum(decrypted[:3]) & 0xff) != decrypted[3]:
print(hd(data), " => ", hd(decrypted), "Checksum error")
else:
op = decrypted[0]
val = decrypted[1] << 8 | decrypted[2]
values[op] = val
## From http://co2meters.com/Documentation/AppNotes/AN146-RAD-0401-serial-communication.pdf
if 0x50 in values:
print("co2mini.value %i" % values[0x50])
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment