[Tinyos-2-commits] CVS: tinyos-2.x/support/sdk/python/tinyos/message Makefile, NONE, 1.1 MoteIF.py, NONE, 1.1 SerialPacket.py, NONE, 1.1 Message.py, 1.2, 1.3 __init__.py, 1.2, 1.3

Chad Metcalf hiro at users.sourceforge.net
Fri Sep 14 11:11:48 PDT 2007


Update of /cvsroot/tinyos/tinyos-2.x/support/sdk/python/tinyos/message
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv629/message

Modified Files:
	Message.py __init__.py 
Added Files:
	Makefile MoteIF.py SerialPacket.py 
Log Message:
Initial commit of Harvards Python SDK. Tested to work with current release including TOSSIM.

--- NEW FILE: Makefile ---
SERIAL_H = $(TOSDIR)/lib/serial/Serial.h


SerialPacket.py: 
	mig -o $@ -python-classname=SerialPacket python $(SERIAL_H) serial_packet -I$(TOSDIR)/types

--- NEW FILE: MoteIF.py ---
#
# Copyright (c) 2005-2006
#      The President and Fellows of Harvard College.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Author: Geoffrey Mainland <mainland at eecs.harvard.edu>
# Tinyos-2: Stephen Dawson-Haggerty

import os
import re
import struct
import sys
import traceback
from tinyos.utils.Watcher import Watcher

from tinyos.packet.Serial import Serial
from tinyos.message.SerialPacket import SerialPacket
import tinyos.packet.PacketDispatcher
import tinyos.packet.PacketSource
import tinyos.packet.SFSource
try:
    import tinyos.packet.SerialSource
except:
    tinyos.packet.SerialSource = None

DEBUG = False

class MoteIFException(Exception):
    def __init__(self, *args):
        self.args = args

class MoteIF:
    def __init__(self):
        self.listeners = {}
        self.watcher = Watcher.getInstance()

    def addListener(self, listener, msgClass):
        if listener not in self.listeners:
            self.listeners[listener] = {}

        amTypes = self.listeners[listener]
        amTypes[msgClass.get_amType()] = msgClass

    def removeListener(self, listener):
        del self.listeners[listener]

    def dispatchPacket(self, source, packet):
        #try:
            #print "Packet length: ", len(packet)
            #            print "Dispatching from MoteIF"
            #             for i in packet:
            #                 print ord(i)," ",
            #             print
        try:
            # Message.py ignores base_offset, so we'll just chop off
            # the first byte (the SERIAL_AMTYPE) here.
            serial_pkt = SerialPacket(packet[1:],
                                      data_length=len(packet)-1)
        except:
            traceback.print_exc()


        try:
            data_start = serial_pkt.offset_data(0) + 1
            data_end = data_start + serial_pkt.get_header_length()
            data = packet[data_start:data_end]
            amType = serial_pkt.get_header_type()

        except Exception, x:
            print >>sys.stderr, x
            print >>sys.stderr, traceback.print_tb(sys.exc_info()[2])

        for l in self.listeners:
            amTypes = self.listeners[l]
            if amType in amTypes:
                try:
                    msgClass = amTypes[amType]
                    msg = msgClass(data=data,
                                   data_length = len(data),
                                   addr=serial_pkt.get_header_src(),
                                   gid=serial_pkt.get_header_group())
                    l.receive(source, msg)
                except Exception, x:
                    print >>sys.stderr, x
                    print >>sys.stderr, traceback.print_tb(sys.exc_info()[2])

    def sendMsg(self, dest, addr, amType, group, msg):
        try:
            payload = msg.dataGet()
            msg = SerialPacket(None)
            msg.set_header_dest(int(addr))
            msg.set_header_group(int(group))
            msg.set_header_type(int(amType))
            msg.set_header_length(len(payload))

            # from tinyos.packet.Serial
            data = chr(Serial.TOS_SERIAL_ACTIVE_MESSAGE_ID)
            data += msg.dataGet()[0:msg.offset_data(0)]
            data += payload

            dest.writePacket(data)
        except Exception, x:
            print >>sys.stderr, x
            print >>sys.stderr, traceback.print_tb(sys.exc_info()[2])

    def addSource(self, name=None):
        if name == None:
            name = os.environ.get("MOTECOM", "sf at localhost:9002")

        m = re.match(r'([^@]*)@(.*)', name)
        if m == None:
            raise MoteIFException("base source '%s'" % (name))

        (sourceType, args) = m.groups()

        if sourceType == "sf":
            source = tinyos.packet.SFSource.SFSource(self, args)
        elif sourceType == "serial" and tinyos.packet.SerialSource != None:
            source = tinyos.packet.SerialSource.SerialSource(self, args)
        else:
            raise MoteIFException("bad source")
        
        source.start()

        return source

    def finishAll(self):
        tinyos.packet.PacketSource.finishAll()

--- NEW FILE: SerialPacket.py ---
#
# This class is automatically generated by mig. DO NOT EDIT THIS FILE.
# This class implements a Python interface to the 'SerialPacket'
# message type.
#

import tinyos.message.Message

# The default size of this message type in bytes.
DEFAULT_MESSAGE_SIZE = 7

# The Active Message type associated with this message.
AM_TYPE = -1

class SerialPacket(tinyos.message.Message.Message):
    # Create a new SerialPacket of size 7.
    def __init__(self, data="", addr=None, gid=None, base_offset=0, data_length=7):
        tinyos.message.Message.Message.__init__(self, data, addr, gid, base_offset, data_length)
        self.amTypeSet(AM_TYPE)
    
    # Get AM_TYPE
    def get_amType(cls):
        return AM_TYPE
    
    get_amType = classmethod(get_amType)
    
    #
    # Return a String representation of this message. Includes the
    # message type name and the non-indexed field values.
    #
    def __str__(self):
        s = "Message <SerialPacket> \n"
        try:
            s += "  [header.dest=0x%x]\n" % (self.get_header_dest())
        except:
            pass
        try:
            s += "  [header.src=0x%x]\n" % (self.get_header_src())
        except:
            pass
        try:
            s += "  [header.length=0x%x]\n" % (self.get_header_length())
        except:
            pass
        try:
            s += "  [header.group=0x%x]\n" % (self.get_header_group())
        except:
            pass
        try:
            s += "  [header.type=0x%x]\n" % (self.get_header_type())
        except:
            pass
        try:
            pass
        except:
            pass
        return s

    # Message-type-specific access methods appear below.

    #
    # Accessor methods for field: header.dest
    #   Field type: int
    #   Offset (bits): 0
    #   Size (bits): 16
    #

    #
    # Return whether the field 'header.dest' is signed (False).
    #
    def isSigned_header_dest(self):
        return False
    
    #
    # Return whether the field 'header.dest' is an array (False).
    #
    def isArray_header_dest(self):
        return False
    
    #
    # Return the offset (in bytes) of the field 'header.dest'
    #
    def offset_header_dest(self):
        return (0 / 8)
    
    #
    # Return the offset (in bits) of the field 'header.dest'
    #
    def offsetBits_header_dest(self):
        return 0
    
    #
    # Return the value (as a int) of the field 'header.dest'
    #
    def get_header_dest(self):
        return self.getUIntElement(self.offsetBits_header_dest(), 16, 1)
    
    #
    # Set the value of the field 'header.dest'
    #
    def set_header_dest(self, value):
        self.setUIntElement(self.offsetBits_header_dest(), 16, value, 1)
    
    #
    # Return the size, in bytes, of the field 'header.dest'
    #
    def size_header_dest(self):
        return (16 / 8)
    
    #
    # Return the size, in bits, of the field 'header.dest'
    #
    def sizeBits_header_dest(self):
        return 16
    
    #
    # Accessor methods for field: header.src
    #   Field type: int
    #   Offset (bits): 16
    #   Size (bits): 16
    #

    #
    # Return whether the field 'header.src' is signed (False).
    #
    def isSigned_header_src(self):
        return False
    
    #
    # Return whether the field 'header.src' is an array (False).
    #
    def isArray_header_src(self):
        return False
    
    #
    # Return the offset (in bytes) of the field 'header.src'
    #
    def offset_header_src(self):
        return (16 / 8)
    
    #
    # Return the offset (in bits) of the field 'header.src'
    #
    def offsetBits_header_src(self):
        return 16
    
    #
    # Return the value (as a int) of the field 'header.src'
    #
    def get_header_src(self):
        return self.getUIntElement(self.offsetBits_header_src(), 16, 1)
    
    #
    # Set the value of the field 'header.src'
    #
    def set_header_src(self, value):
        self.setUIntElement(self.offsetBits_header_src(), 16, value, 1)
    
    #
    # Return the size, in bytes, of the field 'header.src'
    #
    def size_header_src(self):
        return (16 / 8)
    
    #
    # Return the size, in bits, of the field 'header.src'
    #
    def sizeBits_header_src(self):
        return 16
    
    #
    # Accessor methods for field: header.length
    #   Field type: short
    #   Offset (bits): 32
    #   Size (bits): 8
    #

    #
    # Return whether the field 'header.length' is signed (False).
    #
    def isSigned_header_length(self):
        return False
    
    #
    # Return whether the field 'header.length' is an array (False).
    #
    def isArray_header_length(self):
        return False
    
    #
    # Return the offset (in bytes) of the field 'header.length'
    #
    def offset_header_length(self):
        return (32 / 8)
    
    #
    # Return the offset (in bits) of the field 'header.length'
    #
    def offsetBits_header_length(self):
        return 32
    
    #
    # Return the value (as a short) of the field 'header.length'
    #
    def get_header_length(self):
        return self.getUIntElement(self.offsetBits_header_length(), 8, 1)
    
    #
    # Set the value of the field 'header.length'
    #
    def set_header_length(self, value):
        self.setUIntElement(self.offsetBits_header_length(), 8, value, 1)
    
    #
    # Return the size, in bytes, of the field 'header.length'
    #
    def size_header_length(self):
        return (8 / 8)
    
    #
    # Return the size, in bits, of the field 'header.length'
    #
    def sizeBits_header_length(self):
        return 8
    
    #
    # Accessor methods for field: header.group
    #   Field type: short
    #   Offset (bits): 40
    #   Size (bits): 8
    #

    #
    # Return whether the field 'header.group' is signed (False).
    #
    def isSigned_header_group(self):
        return False
    
    #
    # Return whether the field 'header.group' is an array (False).
    #
    def isArray_header_group(self):
        return False
    
    #
    # Return the offset (in bytes) of the field 'header.group'
    #
    def offset_header_group(self):
        return (40 / 8)
    
    #
    # Return the offset (in bits) of the field 'header.group'
    #
    def offsetBits_header_group(self):
        return 40
    
    #
    # Return the value (as a short) of the field 'header.group'
    #
    def get_header_group(self):
        return self.getUIntElement(self.offsetBits_header_group(), 8, 1)
    
    #
    # Set the value of the field 'header.group'
    #
    def set_header_group(self, value):
        self.setUIntElement(self.offsetBits_header_group(), 8, value, 1)
    
    #
    # Return the size, in bytes, of the field 'header.group'
    #
    def size_header_group(self):
        return (8 / 8)
    
    #
    # Return the size, in bits, of the field 'header.group'
    #
    def sizeBits_header_group(self):
        return 8
    
    #
    # Accessor methods for field: header.type
    #   Field type: short
    #   Offset (bits): 48
    #   Size (bits): 8
    #

    #
    # Return whether the field 'header.type' is signed (False).
    #
    def isSigned_header_type(self):
        return False
    
    #
    # Return whether the field 'header.type' is an array (False).
    #
    def isArray_header_type(self):
        return False
    
    #
    # Return the offset (in bytes) of the field 'header.type'
    #
    def offset_header_type(self):
        return (48 / 8)
    
    #
    # Return the offset (in bits) of the field 'header.type'
    #
    def offsetBits_header_type(self):
        return 48
    
    #
    # Return the value (as a short) of the field 'header.type'
    #
    def get_header_type(self):
        return self.getUIntElement(self.offsetBits_header_type(), 8, 1)
    
    #
    # Set the value of the field 'header.type'
    #
    def set_header_type(self, value):
        self.setUIntElement(self.offsetBits_header_type(), 8, value, 1)
    
    #
    # Return the size, in bytes, of the field 'header.type'
    #
    def size_header_type(self):
        return (8 / 8)
    
    #
    # Return the size, in bits, of the field 'header.type'
    #
    def sizeBits_header_type(self):
        return 8
    
    #
    # Accessor methods for field: data
    #   Field type: short[]
    #   Offset (bits): 56
    #   Size of each element (bits): 8
    #

    #
    # Return whether the field 'data' is signed (False).
    #
    def isSigned_data(self):
        return False
    
    #
    # Return whether the field 'data' is an array (True).
    #
    def isArray_data(self):
        return True
    
    #
    # Return the offset (in bytes) of the field 'data'
    #
    def offset_data(self, index1):
        offset = 56
        if index1 < 0:
            raise IndexError
        offset += 0 + index1 * 8
        return (offset / 8)
    
    #
    # Return the offset (in bits) of the field 'data'
    #
    def offsetBits_data(self, index1):
        offset = 56
        if index1 < 0:
            raise IndexError
        offset += 0 + index1 * 8
        return offset
    
    #
    # Return the entire array 'data' as a short[]
    #
    def get_data(self):
        raise IndexError
    
    #
    # Set the contents of the array 'data' from the given short[]
    #
    def set_data(self, value):
        for index0 in range(0, len(value)):
            self.setElement_data(index0, value[index0])

    #
    # Return an element (as a short) of the array 'data'
    #
    def getElement_data(self, index1):
        return self.getUIntElement(self.offsetBits_data(index1), 8, 1)
    
    #
    # Set an element of the array 'data'
    #
    def setElement_data(self, index1, value):
        self.setUIntElement(self.offsetBits_data(index1), 8, value, 1)
    
    #
    # Return the size, in bytes, of each element of the array 'data'
    #
    def elementSize_data(self):
        return (8 / 8)
    
    #
    # Return the size, in bits, of each element of the array 'data'
    #
    def elementSizeBits_data(self):
        return 8
    
    #
    # Return the number of dimensions in the array 'data'
    #
    def numDimensions_data(self):
        return 1
    
    #
    # Return the number of elements in the array 'data'
    # for the given dimension.
    #
    def numElements_data(self, dimension):
        array_dims = [ 0,  ]
        if dimension < 0 or dimension >= 1:
            raise IndexException
        if array_dims[dimension] == 0:
            raise IndexError
        return array_dims[dimension]
    
    #
    # Fill in the array 'data' with a String
    #
    def setString_data(self, s):
         l = len(s)
         for i in range(0, l):
             self.setElement_data(i, ord(s[i]));
         self.setElement_data(l, 0) #null terminate
    
    #
    # Read the array 'data' as a String
    #
    def getString_data(self):
        carr = "";
        for i in range(0, 4000):
            if self.getElement_data(i) == chr(0):
                break
            carr += self.getElement_data(i)
        return carr
    

Index: Message.py
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/python/tinyos/message/Message.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Message.py	12 Jul 2006 17:00:00 -0000	1.2
--- Message.py	14 Sep 2007 18:11:45 -0000	1.3
***************
*** 47,55 ****
              if data == None or len(data) != data_length:
                  self.data = chr(0) * data_length
          else:
              self.data_length = len(data)
  
              self.am_type = 0
! 
      def dataGet(self):
          return self.data
--- 47,56 ----
              if data == None or len(data) != data_length:
                  self.data = chr(0) * data_length
+ 
          else:
              self.data_length = len(data)
  
              self.am_type = 0
!             
      def dataGet(self):
          return self.data

Index: __init__.py
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/python/tinyos/message/__init__.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** __init__.py	12 Jul 2006 17:00:00 -0000	1.2
--- __init__.py	14 Sep 2007 18:11:46 -0000	1.3
***************
*** 29,31 ****
  # Author: Geoffrey Mainland <mainland at eecs.harvard.edu>
  #
! __all__ = ["Message"]
--- 29,31 ----
  # Author: Geoffrey Mainland <mainland at eecs.harvard.edu>
  #
! __all__ = ["Message", "MoteIF", "SerialPacket"]



More information about the Tinyos-2-commits mailing list