#!/usr/bin/env python3 from datetime import datetime import time import sys color_base = {'grey':'\033[90m%s\033[0m' , 'yellow':'\033[33m%s\033[0m' , 'red':'\033[31m%s\033[0m' , 'lred':'\033[91m%s\033[0m' , 'green':'\033[32m%s\033[0m' , 'lgreen':'\033[92m%s\033[0m' , 'blue':'\033[94m%s\033[0m' , 'purple':'\033[95m%s\033[0m' , 'lblue':'\033[96m%s\033[0m' } def color(string, color = 'grey'): if color in color_base: return color_base[color] % string else: return color_base['grey'] % string def make_byte(s): #byt = int(s[0])<<7 | int(s[1])<<6 | int(s[2])<<5 | int(s[3])<<4 | int(s[4])<<3| int(s[5])<<2 | int(s[6])<<1 | int(s[7]) byt = s[0]<<7 | s[1]<<6 | s[2]<<5 | s[3]<<4 | s[4]<<3| s[5]<<2 | s[6]<<1 | s[7] return byt ####### crc8 ### def crc8_add(c): global crc8 crc8 ^= c<<7 crc8 <<= 1 if (crc8 > 0xff): crc8 &= 0xff crc8 ^= 0x07 ####### crc16 ### def crc2_add(c): global crc2 crc2 ^= c<<15 crc2 <<= 1 if (crc2 > 0xffff): crc2 &= 0xffff crc2 ^= 0x1021 ######## crc16 ### def parse(hex_data, addrfilter): if (addrfilter): s_buff="01010101"+''.join(format(x, '08b') for x in addrfilter)+''.join(format(x, '08b') for x in hex_data)+ "00000000000000000000000000000000000000000000000000000000000000000" else: s_buff="01010101"+''.join(format(x, '08b') for x in hex_data)+ "00000000000000000000000000000000000000000000000000000000000000000" global crc8 global crc2 p_crc=0 p_crc8=0 p_crc_calc=1 p_crc8_calc=1 p_buff=[] for c in s_buff: p_buff.append(int(c)) for r in range(2,3): #(2,3) fixes performance issue in raspi2 by only checking 5 byte address logic. change to range(0,3) do inispect ALL allowed lengths. buf3 = p_buff[8:329] if (addrfilter): addr_len=len(addrfilter) goal=32 else: addr_len=r+3 goal=32-addr_len #p_addr = hex_data[0:addr_len] #bytearray #print "## START mit Adresslaenge "+str(addr_len)+": "+''.join("%02x " % b for b in p_addr) +" ##" #print "Range: "+str(addr_len) p_addr = '' for k in range(addr_len): p_addr = p_addr + chr(make_byte(buf3[k*8:k*8+8])) ## pcf p5_len = make_byte([0,0]+buf3[addr_len*8:addr_len*8+6]) p5_id = make_byte([0,0,0,0,0,0]+buf3[addr_len*8+6:(addr_len+1)*8]) p5_ack = make_byte([0,0,0,0,0,0,0]+buf3[(addr_len+1)*8:(addr_len+1)*8+1]) #print "PCF: "+str(p5_len)+", "+str(p5_id)+", "+str(p5_ack) ## data last bit 0 (guess it with 1 as well) for bit in range(0,2): buf3[329-8-1]=bit if (p5_len>32) & (bit==1): continue ## ignore guess=1 on invalid payload-length if (p5_len>goal) & (bit==1): continue ## ignore guess=1 length we did not fully capture. for hh in range(0,goal): if (p5_len<=goal) & (hh==p5_len): enhanced=True else: enhanced=False if (enhanced == False) and (bit == 1): continue if (enhanced == True) and (bit == 1) and (p5_len>1)) hexline=bytearray.fromhex(n[24:99]) binline=(''.join("%s" % '{:08b}'.format(b) for b in hexstart)) + str(pcf[2]) + (''.join("%s" % '{:08b}'.format(b) for b in hexline)) string='' for i in range(int(len(binline)/8)): #print(binline[0+8*i : 8+8*i] + " ist in hex: "+ format( int(binline[0+8*i : 8+8*i], 2),'02x')) string +=format( int(binline[0+8*i : 8+8*i], 2),'02x') string +=' ff' parse(bytearray.fromhex(string), False) sys.stdout.flush() except: print('Line='+line) print('pfchex='+str(pfchex) + ' format>>1=' + '{:02x}'.format(pfchex>>1)) print('conv for hexstart=' + n[:15] + '{:02x}'.format(pfchex>>1) ) print('ERROR in mainloop: '+str(sys.exc_info())) sys.stdout.flush()