Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/decode_watchdog.py
Views: 1798
#!/usr/bin/env python1'''2decode an watchdog message34/Tools/scripts/decode_watchdog.py "WDOG, 2641424, -3, 0, 0, 0, 0, 0, 0, 122, 3, 0, 181, 4196355, 135203219, SPI1"56AP_FLAKE8_CLEAN7'''89import re10import sys11import optparse12from collections import OrderedDict1314import decode_ICSR151617class DecodeWatchDog(object):1819class Component(object):20def __init__(self, value):21self.value = value2223def prefix(self):24m = re.match(".*Component([A-Z]+)", str(type(self)))25return m.group(1)2627def decode(self):28return "?????"2930def string_value(self):31return str(self.value)3233def print_decoded(self):34print("%5s %25s: %12s: %s" % (35self.prefix(),36self.expansion(),37self.string_value(),38self.decode()39))4041class ComponentT(Component):4243def expansion(self):44return "Scheduler Task"4546def decode(self):47if int(self.value) == -3:48return "Waiting for sample"49if int(self.value) == -1:50return "Pre-loop"51if int(self.value) == -2:52return "Fast loop"53return self.value5455class ComponentSL(Component):5657def expansion(self):58return "Semaphore Line"5960def decode(self):61if int(self.value) == 0:62return "Not waiting on semaphore"63return self.value6465class ComponentFL(Component):66def expansion(self):67return "Fault Line"6869class ComponentFT(Component):7071def expansion(self):72return "Fault Type"7374def decode(self):75x = int(self.value)76# this list taken from AP_HAL_ChibiOS/system.cpp77fault_types = {781: "Reset",792: "NMI",803: "HardFault",814: "MemManage",825: "BusFault",836: "UsageFault",84}85if x in fault_types:86return fault_types[x]87return super(DecodeWatchDog.ComponentFT, self).decode()8889class ComponentFA(Component):9091def expansion(self):92return "Fault Address"9394def string_value(self):95return hex(int(self.value, 16))9697class ComponentFTP(Component):9899def expansion(self):100return "Fault Thread Priority"101102class ComponentFLR(Component):103104def expansion(self):105return "Fault Long Return Address" # ?? FIXME: is this really what LR stands for?106107def string_value(self):108return "0x" + self.value109110class ComponentFICSR(Component):111112def expansion(self):113return "Fault ICS Register" # ?? FIXME: expand further114115def string_value(self):116return hex(int(self.value, 16))117118def decode(self):119return "[Below]"120121def print_decoded(self):122super(DecodeWatchDog.ComponentFICSR, self).print_decoded()123decoder = decode_ICSR.DecodeICSR()124text = decoder.string(int(self.value))125sys.stdout.write(re.sub("^", " ", text, flags=re.M))126127class ComponentMM(Component):128129def expansion(self):130return "MAVLink Message"131132def decode(self):133if int(self.value) == 0:134return "[None]"135return super(DecodeWatchDog.ComponentMM, self).decode()136137class ComponentMC(Component):138139def expansion(self):140return "MAVLink Command"141142def decode(self):143if int(self.value) == 0:144return "[None]"145return super(DecodeWatchDog.ComponentMC, self).decode()146147class ComponentIE(Component):148149def expansion(self):150return "Internal Error Mask"151152class ComponentIEHex(ComponentIE):153154def expansion(self):155return "Internal Error Mask"156157def string_value(self):158return hex(int(self.value, 16))159160class ComponentIEC(Component):161162def expansion(self):163return "Internal Error Count"164165def decode(self):166return self.value167168class ComponentIEL(Component):169170def expansion(self):171return "Internal Error Line"172173def decode(self):174return self.value175176class ComponentTN(Component):177178def expansion(self):179return "Thread name"180181def __init__(self):182self.components = OrderedDict()183self.components["T"] = DecodeWatchDog.ComponentT184self.components["SL"] = DecodeWatchDog.ComponentSL185self.components["FL"] = DecodeWatchDog.ComponentFL186self.components["FT"] = DecodeWatchDog.ComponentFT187self.components["FA"] = DecodeWatchDog.ComponentFA188self.components["FTP"] = DecodeWatchDog.ComponentFTP189self.components["FLR"] = DecodeWatchDog.ComponentFLR190self.components["FICSR"] = DecodeWatchDog.ComponentFICSR191self.components["MM"] = DecodeWatchDog.ComponentMM192self.components["MC"] = DecodeWatchDog.ComponentMC193self.components["IE"] = DecodeWatchDog.ComponentIEHex194self.components["IEC"] = DecodeWatchDog.ComponentIEC195self.components["TN"] = DecodeWatchDog.ComponentTN196197self.df_components = {}198self.df_components["Task"] = DecodeWatchDog.ComponentT199self.df_components["Tsk"] = DecodeWatchDog.ComponentT200self.df_components["IErr"] = DecodeWatchDog.ComponentIE201self.df_components["IE"] = DecodeWatchDog.ComponentIE202self.df_components["IEC"] = DecodeWatchDog.ComponentIEC203self.df_components["IEL"] = DecodeWatchDog.ComponentIEL204self.df_components["MavMsg"] = DecodeWatchDog.ComponentMM205self.df_components["MvMsg"] = DecodeWatchDog.ComponentMM206self.df_components["MvCmd"] = DecodeWatchDog.ComponentMC207self.df_components["SemLine"] = DecodeWatchDog.ComponentSL208self.df_components["SmLn"] = DecodeWatchDog.ComponentSL209self.df_components["FL"] = DecodeWatchDog.ComponentFL210self.df_components["FT"] = DecodeWatchDog.ComponentFT211self.df_components["FA"] = DecodeWatchDog.ComponentFA212self.df_components["FP"] = DecodeWatchDog.ComponentFTP213self.df_components["LR"] = DecodeWatchDog.ComponentFLR214self.df_components["ICSR"] = DecodeWatchDog.ComponentFICSR215self.df_components["TN"] = DecodeWatchDog.ComponentTN216217def run(self, text):218219# see if the supplied string is a statustext message:220re_string = "(?:APM: )?WDG:"221for component in self.components.keys():222re_string += " %s(?P<%s>[^ ]+)" % (component, component)223224# print("string: %s" % text)225# print("re_string: %s" % re_string)226227wdg_re = re.compile(re_string)228229m = wdg_re.match(text)230if m is not None:231comp = []232for group in m.groupdict():233comp.append(self.components[group](m.group(group)))234for c in comp:235c.print_decoded()236return237238# not a statustext message; see if it a WDOG dataflash message239df_re = re.compile("WDOG {(.*)}")240m = df_re.match(text)241if m is not None:242pairs = m.group(1).split(",")243for pair in pairs:244(name, value) = pair.split(":")245name = name.strip()246if name == "TimeUS":247continue248value = value.strip()249# print("(%s)=(%s)" % (name, value))250if name in ["LR", "FICSR", "FA"]:251value = int(value, 10)252value = hex(value)253value = value[2:]254if name not in self.df_components:255raise KeyError(name)256self.df_components[name](value).print_decoded()257return258259# not a statustext message and not a mavlogdump dump of a WDOG260# dataflash message. See if it is a .log-style CSV line261log_re = re.compile(r"WDOG, (\d+), ([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), "262r"([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), ([-\d]+), (\w+)")263column_names = "TimeUS,Tsk,IE,IEC,IEL,MvMsg,MvCmd,SmLn,FL,FT,FA,FP,ICSR,LR,TN"264cols = column_names.split(",")265m = log_re.match(text)266if m is not None:267for i in range(0, len(cols)):268name = cols[i]269if name == 'TimeUS':270continue271value = m.group(i+1)272# convert some things from base10 to hex:273if name in ["LR", "FICSR", "FA"]:274value = int(value, 10)275value = hex(value)276value = value[2:]277if name not in self.df_components:278raise KeyError(name)279self.df_components[name](value).print_decoded()280return281282raise ValueError("Text not recognised")283284# 2020-06-10 17:20:08.45: WDOG {TimeUS : 949568, Task : -2, IErr : 0, IErrCnt : 0, MavMsg : 0, MavCmd : 0, SemLine : 0, FL : 100, FT : 3, FA : 404947019, FP : 183, ICSR : 4196355} # NOQA285286# APM: WDG: T-3 SL0 FL122 FT3 FA0 FTP177 FLR80CBB35 FICSR4196355 MM0 MC0 IE67108864 IEC12353 TN:rcin287288# FMT, 254, 47, WDOG, QbIHHHHHHHIBIIn, TimeUS,Tsk,IE,IEC,IEL,MvMsg,MvCmd,SmLn,FL,FT,FA,FP,ICSR,LR,TN289# WDOG, 2641424, -3, 0, 0, 0, 0, 0, 0, 122, 3, 0, 181, 4196355, 135203219, SPI1290291292if __name__ == '__main__':293294parser = optparse.OptionParser(__file__)295296opts, args = parser.parse_args()297298if len(args) == 0:299print("Usage: %s" % parser.usage)300sys.exit(0)301302text = args[0]303304decoder = DecodeWatchDog()305decoder.run(text)306307308