⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tossimapp.py

📁 tinyos-2.x.rar
💻 PY
📖 第 1 页 / 共 2 页
字号:
# "Copyright (c) 2000-2003 The Regents of the University of California.  
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose, without fee, and without written agreement
# is hereby granted, provided that the above copyright notice, the following
# two paragraphs and the author appear in all copies of this software.
# 
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY
# OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
#
#                                                                      
# "Copyright (c) 2005 Stanford University. All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose, without fee, and without written
# agreement is hereby granted, provided that the above copyright
# notice, the following two paragraphs and the author appear in all
# copies of this software.
#
# IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
# IF STANFORD UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
# STANFORD UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE
# PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND STANFORD UNIVERSITY
# HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS."
#
# @author Kamin Whitehouse 
# @author Philip Levis

from tinyos.tossim.TossimNescDecls import *

class NescVariables(object) :
  def __init__( self, applicationName="Unknown App", xmlFilename=None ) :
    self.applicationName = applicationName
    self._varNames = []
    self._vars = []

    dom = minidom.parse(xmlFilename)
    variableList = [node for node in dom.getElementsByTagName("variables")]
    while len(variableList) > 0:
      variables = variableList.pop(0).getElementsByTagName("variable")
      while len(variables) > 0:
        cVariable = 0
        isArray = 0

        variable = variables.pop(0)
        name = variable.getAttribute("name")
        component = variable.getElementsByTagName("component-ref")

        if (len(component) > 0):
          name = component[0].getAttribute("qname") + "." + name
        else:  # It's in a C file
          cVariable = 1
          fileName = variable.getAttribute("loc")
          index = fileName.rfind("/") # First check for a UNIX path
          if (index == -1):
            index = fileName.rfind("\\") # Then a windows path
            if (index == -1):
              index = fileName.rfind(":") # Then if it's in the local dir

          if (index != -1):
            fileName = fileName[index+1:]
            index = fileName.rfind(".")
            if (index != -1):
              fileName = fileName[0:index]
              name = fileName + "." + name

        varType = "unknown"
        varTypes = variable.getElementsByTagName("type-float")
        if (len(varTypes) == 0):
          varTypes = variable.getElementsByTagName("type-int")

        if (len(variable.getElementsByTagName("type-array")) > 0):
          isArray = 1
          
        if (len(varTypes) > 0):
          varTypeEntry = varTypes[0]
          varType = varTypeEntry.getAttribute("cname")

        if (cVariable == 0):
          self._varNames.append(str(name))
          self._vars.append(str(name))
          if (isArray):
            self._vars.append("array")
          else:
            self._vars.append("simple")
          self._vars.append(str(varType))
             
  def __str__(self) :
    """ Print all available variables."""
    string = "\n"
    name = 1
    for val in self._varNames :
      if (name):
        string += "\t" + val
        name = 0
      else:
        string += ": " + val + "\n"
        name = 1
        
    return string
     
  def variables(self):
    return self._vars
  

class NescTypes( object ) :
  """A class that holds all types defined in a specific nesc application.

  usage:
  myTypes = NescTypes('/path/to/nescDecls.xml')
  print myTypes
  var = myTypes.typeName
  """
  def __init__( self, applicationName="Unknown App", xmlFilename = None) :
    self.applicationName = applicationName
    self._typeNames = []
    self._types = {}
    #figure out the sizes of all the basic types for this platform (by scanning the xml file)
    platformTypes = {}
    typeRE = re.compile('cname=\"([\w\s]+?)\" size=\"I:(\d+?)\"')
    infile = open(xmlFilename, 'r')
    for line in infile :
      match = typeRE.search(line)
      if match != None:
        platformTypes[match.groups()[0]] = int(match.groups()[1])      
    #define all the basic types
    self.addType(
      nescType("uint8_t", "unsigned char", "int", "type-int", "B",1,0))
    self.addType(
      nescType("int8_t", "signed char", "int", "type-int", "b", 1, 0))
    if (platformTypes.has_key("int") and platformTypes["int"] == 4) or \
       (platformTypes.has_key("unsigned int") and platformTypes["unsigned int"] == 4) :
      self.addType(
        nescType("uint16_t", "unsigned short", "int", "type-int", "H", 2, 0))
      self.addType(
        nescType("int16_t", "short", "int", "type-int", "h", 2, 0))
      self.addType(
        nescType("uint32_t", "unsigned int", "int", "type-int", "L",4,0))
      self.addType(
        nescType("int32_t", "int", "int", "type-int", "L", 4, 0))
      self.addType(
        nescType("unsigned long", "unsigned long", "int", "type-int", "L",4,0))
      self.addType(
        nescType("long", "long", "int", "type-int", "l", 4, 0))
    else : #int is 2 bytes long (the default)
      self.addType(
        nescType("unsigned short", "unsigned short", "int", "type-int", "H", 2, 0))
      self.addType(
        nescType("short", "short", "int", "type-int", "h", 2, 0))
      self.addType(
        nescType("uint16_t", "unsigned int", "int", "type-int", "H", 2, 0))
      self.addType(
        nescType("int16_t", "int", "int", "type-int", "h", 2, 0))
      self.addType(
        nescType("uint32_t", "unsigned long", "int", "type-int", "L",4,0))
      self.addType(
        nescType("int32_t", "long", "int", "type-int", "l", 4, 0))
    self.addType(
      nescType("int64_t", "long long", "long", "type-int", "q", 8, 0))
    self.addType(
      nescType("uint64_t", "unsigned long long", "long", "type-int", "Q", 8, 0))
    self.addType(
      nescType("float", "float", "float", "type-float", "f", 4, 0))
    if platformTypes.has_key("double") and platformTypes["double"] == 8 :
      self.addType(
        nescType("double", "double", "float", "type-float", "d", 8, 0))
    else : #double is 4 bytes (the default)
      self.addType(
        nescType("double", "double", "float", "type-float", "f", 4, 0))
    self.addType(
      nescType("char", "char", "str", "type-int", "c", 1, '\x00'))
    self.addType(
      nescType("void", "void", "", "type-void", "", 0, ''))

    #some arrays for error reporting:
    self.unknownStructs = []
    self.anonymousStructs = []
    self.anonymousRefStructs = []
    self.undefinedTypes = []
    self.createTypesFromXml(xmlFilename)
    self._typeNames.sort()
    #self.printSkippedTypes()
  
  def addType(self, value) :
    if not value.nescType in self._typeNames :
      self._typeNames.append(value.nescType)
    self._types[value.nescType] = value #XXX: why does this have to be unconditional??
    if not self._types.has_key(value.cType):
      self._types[value.cType] = value
      self._typeNames.append(value.cType)
    
  def __getattr__(self, name) :
    if name in self._typeNames :
      return deepcopy(self._types[name])
    else:
      raise AttributeError("No type \"%s\" defined" % name)
  
  def __getitem__(self, key) :
    if key in self._typeNames :
      return deepcopy(self._types[key])
    else:
      raise AttributeError("No type \"%s\" defined" % key)

  def __repr__(self) :
    return "%s object at %s:\n\n\t%s" % (self.__class__, hex(id(self)), str(self))
    
  def __str__(self) :
    """ Print all available types."""
    string = "\n"
    for t in self._typeNames :
      string += "\t%s\n" % t
    return string
    
  def createTypesFromXml(self, xmlFilename) :
    """Go through the struct and typedef elements in the nescDecls.xml file"""
    
    dom = minidom.parse(xmlFilename)
    typeDefs = [node for node in dom.getElementsByTagName("struct")]
    for node in dom.getElementsByTagName("typedef") :
      typeDefs.append(node)
    
    numSkipped = 0

    #keep going through the queue until it is empty
    while len(typeDefs) > 0:
      typeDef = typeDefs.pop(0)

      #if this is a typedef, see if the value is there
      if typeDef.tagName == "typedef" :
        value = typeDef.getAttribute("value")
        name = typeDef.getAttribute("name")
        #if the real value exists and typedef doesn't already exist, copy and rename original
        if self._types.has_key(value) :
          newType = deepcopy(self._types[value])
          newType.nescType = name
          self.addType(newType)
          numSkipped=0
        else :
          #try again later
          typeDefs.append(typeDef)
          numSkipped += 1
          
      else :

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -