LCATtlmDb.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2005
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "Wrapper class for LCAT based telemetry"
00012 __author__   = "Selim Tuvi <stuvi@slac.stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "2005/09/13 00:08:27"
00014 __updated__  = "$Date: 2006/03/09 15:28:29 $"
00015 __version__  = "$Revision: 1.18 $"
00016 __release__  = "$Name: HEAD $"
00017 __credits__  = "SLAC"
00018 
00019 import LICOS.copyright_SLAC
00020 
00021 import logging as log
00022 import threading
00023 
00024 # import generated Python classes from the LCAT package
00025 import LCAT_tlm_db
00026 import LICOS.lib.cmdTlmDb.VSC_tlm_db as VSC_tlm_db    # Generated and copied
00027 
00028 from   LCAT_decoder import ccsdsTlmHdr
00029 from   LCAT_msg_db import LCAT_Msg_Db
00030 
00031 from LICOS.lib.LATconstants import FSW_UNIT_NAMES
00032 
00033 
00034 from cmdTlmDb import TlmDb
00035 
00036 class LCATtlmDb(TlmDb):
00037   """!\brief Database wrapper class for LCAT based telemetry.
00038 
00039   The purpose of this class is to provide helper methods for
00040   translating telemetry mnemonic to apid and vice versa.
00041   """
00042 
00043   def __init__(self):
00044     """!\brief LCATtlmDb constructor.
00045 
00046     """
00047     TlmDb.__init__(self)
00048     self.__dbModule = LCAT_tlm_db
00049     self.__dbModule2 = VSC_tlm_db
00050     self.__tlmNameToApid = {}
00051     self.__apidToTlmName = {}
00052     self.__populateDicts()
00053     self.MSG = LCAT_Msg_Db()
00054 
00055   def __populateDicts(self):
00056     """!\brief Fill in the dictionaries used by
00057               getApidFromName and getNameFromApid.
00058     """
00059     tlmApidDict = self.__dbModule.TlmApidDict()
00060     for (apid, mnem) in tlmApidDict.apids.items():
00061       self.__tlmNameToApid[mnem] = apid
00062       self.__apidToTlmName[apid] = mnem
00063     td2 = self.__dbModule2.TlmApidDict()
00064     for (apid, mnem) in td2.apids.items():
00065       self.__tlmNameToApid[mnem] = apid
00066       self.__apidToTlmName[apid] = mnem
00067 
00068 
00069 
00070   def getNames(self):
00071     """!\brief Retrieve a list of all telemetry mnemonic names.
00072 
00073     \return A list containing all mnemonic names
00074     """
00075     return self.__tlmNameToApid.keys()
00076 
00077   def getApids(self):
00078     """!\brief Retrieve a list of all telemetry apids.
00079 
00080     \return A list containing all mnemonic apids
00081     """
00082     return self.__apidToTlmName.keys()
00083 
00084   def getApidFromName(self, name):
00085     """!\brief Retrieve the apid from the telemetry mnemonic name.
00086 
00087     \param name Telemetry mnemonic name
00088 
00089     \return Telemetry apid
00090     """
00091     fn = "getApidFromName"
00092     if name in self.__tlmNameToApid:
00093       return self.__tlmNameToApid[name]
00094     else:
00095       log.warn("%s: Telemetry mnemonic %s not found in the database" % (fn, name))
00096 
00097   def getApidsFromNames(self, nameList):
00098     """!\brief Retrieve the apid list corresponding to the
00099               given telemetry mnemonic name list.
00100 
00101     \param nameList Telemetry mnemonic name list
00102 
00103     \return Telemetry apid list
00104     """
00105     fn = "getApidsFromNames"
00106     apidList = []
00107     for name in nameList:
00108       if name in self.__tlmNameToApid:
00109         apidList.append(self.__tlmNameToApid[name])
00110       else:
00111         log.warn("%s: Telemetry mnemonic %s not found in the database" % (fn, name))
00112     return apidList
00113 
00114   def getNameFromApid(self, apid):
00115     """!\brief Retrieve the name from the telemetry apid.
00116 
00117     \param apid Telemetry apid
00118 
00119     \return Telemetry mnemonic name.
00120     """
00121     if apid in self.__apidToTlmName:
00122       return self.__apidToTlmName[apid]
00123 
00124   def getDbModule(self):
00125     """!\brief Return the generated database module.
00126 
00127     \return Database module
00128     """
00129     return self.__dbModule
00130 
00131   def msgByName(self, name):
00132     """!\brief Return the message by name.
00133 
00134     \param name Message name
00135 
00136     \return Message text
00137     """
00138     return self.MSG.msg_by_name(name)
00139 
00140   def msgByValue(self, value):
00141     """!\brief Return the message by value.
00142 
00143     \param value Message value
00144 
00145     \return Message tuple containing message code and description
00146     """
00147     msgTuple = self.MSG.msg_by_value(~(1 << 31) & value)
00148     if msgTuple is None:
00149       msgTuple = ('UNDEFINED', 'Undefined message 0x%08x' % (~(1 << 31) & value))
00150     return msgTuple
00151 
00152   def msgIsSuccess(self, value):
00153     """!\brief Check if the message status is success.
00154 
00155     \param value Message value
00156 
00157     \return True if the value is success, False otherwise
00158     """
00159     return (value & 1) == 0
00160 
00161   def decodeTelemetry(self, telemetry, dump=False):
00162     """!\brief Decode a telemetry packet and optionally dump the payloads.
00163 
00164     \param telemetry Raw CCSDS packet
00165     \param dump      Boolean indicating whether to dump the
00166                      telemetry payloads
00167 
00168     \return Decoded packet
00169     """
00170     pkt = LICOS_VscTlmPacketFactory(telemetry)
00171     pkt.decode_payload()
00172     if dump:
00173       for (name, value) in self.getPayloadData(pkt):
00174         log.debug("  %20s: 0x%x" % (name, value))
00175     return pkt
00176 
00177   def getPayloadData(self, pkt):
00178     """!\brief Retrieve payload name and values.
00179 
00180     \param pkt Decoded packet
00181 
00182     \return A list containing payload (name, value) tuples
00183     """
00184     return [(m[0], pkt.get_payload(m[0])) for m in pkt.mnemList]
00185 
00186   def msgOutToText(self, pkt):
00187     """!\Form a text string out of the message tuple
00188 
00189     \param pkt Decoded packet
00190 
00191     \return String containing a text version of the message tuple
00192     """
00193     msgTuple = self.interpretMsgOut(pkt)
00194     return "%s: " % FSW_UNIT_NAMES[msgTuple[0]] + "%s %s %s %s %s" % msgTuple[1:]
00195 
00196   def interpretMsgOut(self, pkt):
00197     """!\brief Interprets the LCMMSGOUTC telemetry packets
00198               and converts them to a tuple.
00199 
00200     \param pkt Decoded packet
00201 
00202     \return Tuple containing (source, task, func, facil, name, text)
00203     """
00204     source = pkt.get_payload("LCMMSGNODE")
00205     task = ""
00206     for i in range(8):
00207       task+=chr(pkt.get_payload("LCMMSGTASK%d" % i))
00208     func = ""
00209     for i in range(32):
00210       func+=chr(pkt.get_payload("LCMMSGFUNC%d" % i))
00211     facil = ""
00212     for i in range(4):
00213       facil+=chr(pkt.get_payload("LCMMSGFACIL%d" % i))
00214     name = ""
00215     for i in range(8):
00216       name+=chr(pkt.get_payload("LCMMSGNAME%d" % i))
00217     text = ""
00218     for i in range(128):
00219       text+=chr(pkt.get_payload("LCMMSGTEXT%d" % i))
00220     return  (source, task.rstrip(), func.rstrip(), facil.rstrip(),
00221              name.rstrip(), text.rstrip())
00222 
00223 def LICOS_TlmPacketFactory(packet):
00224   """!\brief Take a binary string and create a packet from it
00225 
00226   \param packet   A binary representation of a CCSDS string
00227   \return A decodable packet object
00228   """
00229   import struct
00230   theShort = struct.unpack('!H', packet[0:2])
00231   apid = theShort[0] & 0x7ff
00232   try:
00233     pkt = ApidTlmClassMap[apid](packet)
00234     return pkt
00235   except:
00236     return None
00237 
00238 def LICOS_VscTlmPacketFactory(packet):
00239   """!\brief Take a VSC telemetry object and create a packet from it
00240 
00241   \param packet   A VSC telemetry object
00242   \return A decodable packet object
00243   """
00244   pkt = None
00245   apid = packet.apid()
00246   try:
00247     pkt = ApidTlmClassMap[apid](packet)
00248   except:
00249     return None
00250   try:
00251     pkt.decode_vsc(packet)
00252   except Exception,e:
00253     print "LICOS tlm pkt apid", hex(apid), "decode error", e
00254     return None
00255   return pkt
00256 
00257 # Generate a map of apid to telemetry classes to be used with the
00258 # factory method above.
00259 ApidTlmClassMap = {}
00260 for i in LCAT_tlm_db.TlmApidDict().apids:
00261   ApidTlmClassMap[i] = getattr(LCAT_tlm_db, "LCAT_TlmPacket%03x" % i)
00262 
00263 # JHP By hand, add the VSC telemetry info to the proper objects:
00264 # 1: dispatch map
00265 for i in VSC_tlm_db.TlmApidDict().apids:
00266   ApidTlmClassMap[i] = getattr(VSC_tlm_db, "LCAT_TlmPacket%03x" % i)
00267 
00268 # 2: Mnemonic dicts
00269 # print dir(LCAT_tlm_db.TlmMnemonicDict)
00270 # print dir(VSC_tlm_db.TlmMnemonicDict)
00271 # LCAT_tlm_db.TlmMnemonicDict.mnemonics["VSC"] = VSC_tlm_db.TlmMnemonicDict.mnemonics["VSC"]
00272 # LCAT_tlm_db.TlmMnemonicDict.desc += VSC_tlm_db.TlmMnemonicDict.desc
00273 
00274 # 3: bitfield definitions
00275 #for i in VSC_tlm_db.LCAT_tlmPacket.bitDict:
00276 #  LCAT_tlm_db.LCAT_tlmPacket.bitDict[i] = VSC_tlm_db.LCAT_tlmPacket.bitDict[i]
00277 

Generated on Thu Apr 27 20:52:42 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO