114 lines
2.6 KiB
Python
114 lines
2.6 KiB
Python
import os
|
|
import sys
|
|
import struct
|
|
import dataclasses
|
|
from bin.log import myprint
|
|
from bin.log import mywrite
|
|
|
|
STR_YELLOW="\033[0;33m"
|
|
STR_END="\033[0m"
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class cli_msg_hdr_t:
|
|
src_mac:bytearray
|
|
target_mac:bytearray
|
|
module_id:int
|
|
cli_crc:int
|
|
msg_id:int
|
|
auto_ack:int
|
|
reserved2:int
|
|
msg_len:int
|
|
sn:int
|
|
@classmethod
|
|
def __init__(self, data:bytearray):
|
|
self.src_mac=data[0:6]
|
|
self.target_mac=data[6:12]
|
|
self.module_id=struct.unpack("<H", data[12:14])[0]
|
|
self.cli_crc=struct.unpack("<H", data[14:16])[0]
|
|
self.msg_id=struct.unpack("<H", data[16:18])[0]
|
|
self.auto_ack=struct.unpack("<B", data[18:19])[0]&0x01
|
|
self.reserved2=struct.unpack("<B", data[19:20])[0]
|
|
self.msg_len=struct.unpack("<H", data[20:22])[0]
|
|
self.sn=struct.unpack("<H", data[22:24])[0]
|
|
|
|
@dataclasses.dataclass
|
|
class payload_t:
|
|
fw_version:int
|
|
time_stamp:int
|
|
seq:int
|
|
mod_msg:int
|
|
payload_len:int
|
|
@classmethod
|
|
def __init__(self, data:bytearray):
|
|
self.fw_version=struct.unpack("<I", data[0:4])[0]
|
|
self.time_stamp=struct.unpack("<I", data[4:8])[0]
|
|
self.seq=struct.unpack("<I", data[8:12])[0]
|
|
self.mod_msg=struct.unpack("<I", data[12:16])[0]
|
|
self.payload_len=struct.unpack("<I", data[16:20])[0]>>16
|
|
|
|
|
|
|
|
|
|
def get_log(log_file:str):
|
|
with open(log_file, 'rb') as f:
|
|
data=f.read()
|
|
save=data[0x143000:0x144580]
|
|
with open("save.bin", 'wb+') as f:
|
|
f.write(save)
|
|
|
|
def find_log(data):
|
|
for i in range(0, len(data), 0x20):
|
|
if data[i:i+2]==b'$$':
|
|
myprint(f"log offset={hex(i)}")
|
|
return i
|
|
return -1
|
|
|
|
|
|
def print_log(log_file:str):
|
|
with open(log_file, 'rb') as f:
|
|
data=f.read()
|
|
off=0
|
|
# addr表示相对于bin文件的地址
|
|
addr=off
|
|
if(off<0):
|
|
myprint("not found log")
|
|
return
|
|
data=data[off:]
|
|
while len(data)>0:
|
|
off=data.find(b'$$')
|
|
if(off<0):
|
|
break
|
|
addr+=off
|
|
myprint(f"off={hex(addr)}")
|
|
data=data[off+2:]
|
|
addr+=2
|
|
end=data.find(b'&&')
|
|
if(end<0):
|
|
break
|
|
a=cli_msg_hdr_t(data[0:24])
|
|
# msg_len减去的是payload的长度
|
|
myprint(f"msg_len={a.msg_len-20}")
|
|
if(a.msg_len!=end-24):
|
|
myprint(f"msg_len error")
|
|
continue
|
|
# a=payload_t(data[24:44])
|
|
# print(a)
|
|
pstr=data[44:end]
|
|
for i in range(len(pstr)):
|
|
if(pstr[i]&0x80):
|
|
pstr=pstr[:i]
|
|
break
|
|
try:
|
|
mywrite(f"{pstr.decode('utf-8')}")
|
|
except:
|
|
mywrite(f"{pstr}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python get_log.py <log_file>")
|
|
sys.exit(1)
|
|
log_file = sys.argv[1]
|
|
print_log(log_file)
|