- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# ieee802154_base.py

Go to the documentation of this file.

    00001 #   Copyright (c) 2008 Axel Wachtler
    00002 #   All rights reserved.
    00003 #
    00004 #   Redistribution and use in source and binary forms, with or without
    00005 #   modification, are permitted provided that the following conditions
    00006 #   are met:
    00007 #
    00008 #   * Redistributions of source code must retain the above copyright
    00009 #     notice, this list of conditions and the following disclaimer.
    00010 #   * Redistributions in binary form must reproduce the above copyright
    00011 #     notice, this list of conditions and the following disclaimer in the
    00012 #     documentation and/or other materials provided with the distribution.
    00013 #   * Neither the name of the authors nor the names of its contributors
    00014 #     may be used to endorse or promote products derived from this software
    00015 #     without specific prior written permission.
    00016 #
    00017 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00018 #   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00019 #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00020 #   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00021 #   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    00022 #   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    00023 #   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    00024 #   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    00025 #   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    00026 #   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    00027 #   POSSIBILITY OF SUCH DAMAGE.
    00028
    00029 # $Id: ieee802154_base.py,v 1.5 2009/02/23 22:06:03 awachtler Exp $
    00030 ##
    00031 # @file
    00032 # @ingroup grpContribCapture
    00033 # @brief basic communication class
    00034 #
    00035 # === import ==================================================================
    00036 import sys, traceback, struct
    00037
    00038 # === globals =================================================================
    00039
    00040 # === functions ===============================================================
    00041
    00042 # === classes =================================================================
    00043 ##
    00044 # @brief programm context storage class
    00045 class ProgramContext:
    00046     ## name of the file socket, overwritten by -i option
    00047     SOCKNAME='ieee802154'
    00048     ## name of the capture file, overwritten by -r option
    00049     CAPFILE=None
    00050     ## name of the capture port, overwritten by -p option
    00051     PORT=None
    00052     ## Channel number for sniffing.
    00053     CHANNEL = None
    00054     ## Verbosity Level.
    00055     VERBOSE = 0
    00056     ## Maximum Packets to be captured.
    00057     MAXPACKETS = 0
    00058     ## Start control GUI.
    00059     GUI = False
    00060     # Data Rate
    00061     RATE = None
    00062
    00063 ##
    00064 # Generic reporting and exception handler class
    00065 class UtilBase:
    00066     TMO = 1
    00067     VERBOSE = 0
    00068     ## reporting an error to stderr.
    00069     def error(self,msg,*args):
    00070         sys.stderr.write("ERROR:"+msg%args+"\n")
    00071         sys.stderr.flush()
    00072
    00073     ## regular message
    00074     def message(self,lvl,msg,*args):
    00075         if self.VERBOSE >= lvl:
    00076             prompt = "+"*(lvl)
    00077             msg = msg%args
    00078             msg = msg.replace("\n","\n> ")
    00079             print prompt,msg
    00080             sys.stdout.flush()
    00081
    00082     ## configure verbosity level
    00083     def setverbose(self,verbose):
    00084         self.VERBOSE = verbose
    00085
    00086     ## custom exception printer
    00087     def exc_handler(self, diag):
    00088         print sys.exc_info()
    00089         self.message(0,diag)
    00090         if self.VERBOSE>0:
    00091             traceback.print_exc()
    00092
    00093
    00094 ##
    00095 # @brief Base class for @ref PcapFile and @ref PcapPort
    00096 class PcapBase:
    00097     ## Cstruct format string for pcap packet header
    00098     PCAP_PACKET="QLL"
    00099     ## Cstruct format string for pcap file header
    00100     # for details see http://wiki.wireshark.org/Development/LibpcapFileFormat
    00101     #
    00102     # typedef struct pcap_hdr_s {
    00103     #   guint32 magic_number;   /* magic number */
    00104     #   guint16 version_major;  /* major version number */
    00105     #   guint16 version_minor;  /* minor version number */
    00106     #   gint32  thiszone;       /* GMT to local correction */
    00107     #   guint32 sigfigs;        /* accuracy of timestamps */
    00108     #   guint32 snaplen;        /* max length of captured packets, in octets */
    00109     #   guint32 network;        /* data link type */
    00110     # } pcap_hdr_t;
    00111     #
    00112     PCAP_HEADER="LHHLLLL"
    00113     ##
    00114     # create pcap header file
    00115     def pcap_get_header(self):
    00116         magic_number = 0xa1b2c3d4 #<1> L
    00117         version_major = 2         #<2> H
    00118         version_minor = 4         #<3> H
    00119         thiszone = 0              #<4> L
    00120         sigfigs = 0               #<5> L
    00121         snaplen = 0x80            #<6> L maximum length of 802.15.4 packet incl. PHR (length field)
    00122         network = 0xc3            #<7> L
    00123         ret = struct.pack(self.PCAP_HEADER,
    00124                      magic_number, version_major, version_minor,
    00125                      thiszone, sigfigs,snaplen, network)
    00126         return ret
    00127
    00128     ## parse the entire data of a capture file.
    00129     # @retval hdr   the header structure of the file
    00130     # @retval ret   a list containing the packets as raw strings
    00131     def pcap_parse_data(self,data):
    00132         ret = []
    00133         hsz = struct.calcsize(self.PCAP_HEADER)
    00134         psz = struct.calcsize(self.PCAP_PACKET)
    00135         o = hsz
    00136         hdr, data = data[:o], data[o:]
    00137         while(len(data) > psz):
    00138             o = psz
    00139             ph, data = data[:o], data[o:]
    00140             ts,cl,fc = struct.unpack(self.PCAP_PACKET , ph)
    00141             if cl != fc:
    00142                 self.message(1,"MISMATCH cl=%x fc=%x", cl, fc)
    00143             o = cl
    00144             frm, data = data[:o], data[o:]
    00145             l = len(frm)
    00146             #frm = chr(1) + frm + chr(4)
    00147             ret.append(ph+frm)
    00148         return hdr,ret
    00149
    00150
    00151 # === init ====================================================================