Esempio n. 1
0
def test_dehdlcifyToZero():

    log.debug("\n---------- test_dehdlcifyToZero")

    hdlc = OpenHdlc.OpenHdlc()

    # frame
    frame = [0x53, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa]
    frame = ''.join([chr(b) for b in frame])
    log.debug("frame:      {0}".format(u.formatStringBuf(frame)))

    # hdlcify
    frame = hdlc.hdlcify(frame)
    log.debug("hdlcify: {0}".format(u.formatStringBuf(frame)))

    # remove flags
    frame = frame[1:-1]
    log.debug("no flags:   {0}".format(u.formatStringBuf(frame)))

    # calculate CRC
    crcini = 0xffff
    crc = crcini
    for c in frame:
        tmp = crc ^ (ord(c))
        crc = (crc >> 8) ^ hdlc.FCS16TAB[(tmp & 0xff)]
        log.debug("after {0}, crc={1}".format(hex(ord(c)), hex(crc)))
Esempio n. 2
0
    def parseOneFile(self, filename):
        self.hdlc = OpenHdlc.OpenHdlc()
        (hdlcFrames, _) = self.hdlc.dehdlcify(filename)

        parsedFrames = []
        for f in hdlcFrames:
            # first byte is the type of frame
            if f[0] == ord('D'):
                pf = self.parse_DATA(f[1:])
            elif f[0] == ord('S'):
                pf = self.parse_STATUS(f[1:])
            elif f[0] == ord('I'):
                pf = self.parse_INFO(f[1:])
            elif f[0] == ord('E'):
                pf = self.parse_ERROR(f[1:])
            elif f[0] == ord('C'):
                pf = self.parse_CRITICAL(f[1:])
            elif f[0] == ord('R'):
                pf = self.parse_REQUEST(f[1:])
            else:
                print 'TODO: parse frame of type {0}'.format(chr(f[0]))
            if pf:
                parsedFrames += [pf]

        with open(filename + '.parsed', 'w') as f:
            f.write('\n'.join([str(pf) for pf in parsedFrames]))

        return parsedFrames
Esempio n. 3
0
    def __init__(self, serialportName, serialportBaudrate):

        # log
        log.debug("create instance")

        # store params
        self.serialportName = serialportName
        self.serialportBaudrate = serialportBaudrate

        # local variables
        self.hdlc = OpenHdlc.OpenHdlc()
        self.lastRxByte = self.hdlc.HDLC_FLAG
        self.busyReceiving = False
        self.inputBuf = ''
        self.outputBuf = []
        self.outputBufLock = threading.RLock()

        # initialize the parent class
        threading.Thread.__init__(self)

        # give this thread a name
        self.name = 'moteProbeSerialThread@' + self.serialportName

        # connect to dispatcher
        dispatcher.connect(
            self.send,
            signal='bytesFromTcpPort' + self.serialportName,
        )
def oneFileParser(logfilePath):

    hdlc = OpenHdlc.OpenHdlc()
    (hdlcFrames, _) = hdlc.dehdlcify(logfilePath)

    parsedFrames = []
    for f in hdlcFrames:
        if not f:
            continue
        # first byte is the type of frame
        if f[0] == ord('D'):
            pf = parse_DATA(f[1:])
        elif f[0] == ord('S'):
            pf = parse_STATUS(f[1:])
        elif f[0] == ord('I'):
            pf = parse_INFO(f[1:])
        elif f[0] == ord('E'):
            pf = parse_ERROR(f[1:])
        elif f[0] == ord('C'):
            pf = parse_CRITICAL(f[1:])
        elif f[0] == ord('R'):
            pf = parse_REQUEST(f[1:])
        else:
            print 'TODO: parse frame of type {0}'.format(chr(f[0]))
        if pf:
            parsedFrames += [pf]

    with open(logfilePath + '.parsed', 'w') as f:
        f.write('\n'.join([str(pf) for pf in parsedFrames]))

    print 'Parsing {0}...Done!'.format(logfilePath)
Esempio n. 5
0
def test_buildRequestFrame():

    log.debug("\n---------- test_buildRequestFrame")

    hdlc = OpenHdlc.OpenHdlc()

    # hdlcify
    frameHdlcified = hdlc.hdlcify('\x53')
    log.debug("request frame: {0}".format(u.formatStringBuf(frameHdlcified)))
Esempio n. 6
0
 def __init__(self):
     self.fileLock = threading.RLock()
     self.hdlc = OpenHdlc.OpenHdlc()
     self.hrParser = HrParser.HrParser()
     self.api = IpMgrDefinition.IpMgrDefinition()
     self.connSerial = IpMgrConnectorSerial.IpMgrConnectorSerial()
     self.oapLock = threading.RLock()
     self.oap = OAPDispatcher.OAPDispatcher()
     self.oap.register_notif_handler(self._handle_oap_notif)
Esempio n. 7
0
def test_randdomBackAndForth(randomFrame):

    log.debug("\n---------- test_randdomBackAndForth")

    hdlc = OpenHdlc.OpenHdlc()

    log.debug("randomFrame:    {0}".format(u.formatBuf(randomFrame)))

    # hdlcify
    frameHdlcified = hdlc.hdlcify(randomFrame)
    log.debug("hdlcified:   {0}".format(u.formatBuf(frameHdlcified)))

    # dehdlcify
    frameDehdlcified = hdlc.dehdlcify(frameHdlcified)
    log.debug("dehdlcified:    {0}".format(u.formatBuf(frameDehdlcified)))

    assert frameDehdlcified == randomFrame
 def __init__(self,serialport=None):
     
     # verify params
     if   serialport:
         self.mode = self.MODE_SERIAL
     else:
         raise SystemError()
     
     # store params
     if   self.mode==self.MODE_SERIAL:
         self.serialport       = serialport[0]
         self.baudrate         = serialport[1]
         self.portname         = self.serialport
     else:
         raise SystemError()
     
     # log
     print "creating moteProbe attaching to {0}".format(
             self.portname,
         )
     
     # local variables
     self.hdlc                 = OpenHdlc.OpenHdlc()
     self.lastRxByte           = self.hdlc.HDLC_FLAG
     self.busyReceiving        = False
     self.inputBuf             = ''
     self.outputBuf            = []
     self.outputBufLock        = threading.RLock()
     self.dataLock             = threading.Lock()
     # flag to permit exit from read loop
     self.goOn                 = True
     
     # initialize the parent class
     threading.Thread.__init__(self)
     
     # give this thread a name
     self.name                 = 'moteProbe@'+self.portname
     
     if self.mode in [self.MODE_EMULATED,self.MODE_IOTLAB]:
         # Non-daemonized moteProbe does not consistently die on close(),
         # so ensure moteProbe does not persist.
         self.daemon           = True
 
     # start myself
     self.start()
Esempio n. 9
0
def test_randdomBackAndForth(randomFrame):
    
    randomFrame = json.loads(randomFrame)
    randomFrame = ''.join([chr(b) for b in randomFrame])
    
    log.debug("\n---------- test_randdomBackAndForth")
    
    hdlc = OpenHdlc.OpenHdlc()
    
    log.debug("randomFrame:    {0}".format(u.formatStringBuf(randomFrame)))
    
    # hdlcify
    frameHdlcified = hdlc.hdlcify(randomFrame)
    log.debug("hdlcified:   {0}".format(u.formatStringBuf(frameHdlcified)))
    
    # dehdlcify
    frameDehdlcified = hdlc.dehdlcify(frameHdlcified)
    log.debug("dehdlcified:    {0}".format(u.formatStringBuf(frameDehdlcified)))
    
    assert frameDehdlcified==randomFrame
Esempio n. 10
0
 def __init__(self,serialport=None,emulatedMote=None,iotlabmote=None):
     
     # verify params
     if   serialport:
         assert not emulatedMote
         assert not iotlabmote
         self.mode             = self.MODE_SERIAL
     elif emulatedMote:
         assert not serialport
         assert not iotlabmote
         self.mode             = self.MODE_EMULATED
     elif iotlabmote:
         assert not serialport
         assert not emulatedMote
         self.mode             = self.MODE_IOTLAB
     else:
         raise SystemError()
     
     # store params
     if   self.mode==self.MODE_SERIAL:
         self.serialport       = serialport[0]
         self.baudrate         = serialport[1]
         self.portname         = self.serialport
     elif self.mode==self.MODE_EMULATED:
         self.emulatedMote     = emulatedMote
         self.portname         = 'emulated{0}'.format(self.emulatedMote.getId())
     elif self.mode==self.MODE_IOTLAB:
         self.iotlabmote       = iotlabmote
         self.portname         = 'IoT-LAB{0}'.format(iotlabmote)
     else:
         raise SystemError()
     
     # log
     log.info("creating moteProbe attaching to {0}".format(
             self.portname,
         )
     )
     
     # local variables
     self.hdlc                 = OpenHdlc.OpenHdlc()
     self.lastRxByte           = self.hdlc.HDLC_FLAG
     self.busyReceiving        = False
     self.inputBuf             = ''
     self.outputBuf            = []
     self.outputBufLock        = threading.RLock()
     self.dataLock             = threading.Lock()
     # flag to permit exit from read loop
     self.goOn                 = True
     
     # initialize the parent class
     threading.Thread.__init__(self)
     
     # give this thread a name
     self.name                 = 'moteProbe@'+self.portname
     
     if self.mode in [self.MODE_EMULATED,self.MODE_IOTLAB]:
         # Non-daemonized moteProbe does not consistently die on close(),
         # so ensure moteProbe does not persist.
         self.daemon           = True
     
     # connect to dispatcher
     dispatcher.connect(
         self._bufferDataToSend,
         signal = 'fromMoteConnector@'+self.portname,
     )
 
     # start myself
     self.start()
Esempio n. 11
0
 def __init__(self,serialport=None,emulatedMote=None):
     assert bool(serialport) != bool(emulatedMote)
     
     if serialport:
         assert not emulatedMote
         self.realserial       = True
     else:
         assert not serialport
         self.realserial       = False
     
     # store params
     if self.realserial:
         # import pyserial module (needs to be installed)
         import serial
         
         # store params
         self.serialport       = serialport[0]
         self.baudrate         = serialport[1]
         
         # log
         log.info("creating moteProbe attaching to serialport {0}@{1}".format(
                 self.serialport,
                 self.baudrate,
             )
         )
     else:
         # store params
         self.emulatedMote     = emulatedMote
         self.serialport       = 'emulated{0}'.format(self.emulatedMote.getId())
         
         # log
         log.info("creating moteProbe attaching to emulated mote {0}".format(
                 self.serialport,
             )
         )
     
     # local variables
     self.hdlc                 = OpenHdlc.OpenHdlc()
     self.lastRxByte           = self.hdlc.HDLC_FLAG
     self.busyReceiving        = False
     self.inputBuf             = ''
     self.outputBuf            = []
     self.outputBufLock        = threading.RLock()
     self.dataLock             = threading.Lock()
     # flag to permit exit from read loop
     self.goOn                 = True
     
     # initialize the parent class
     threading.Thread.__init__(self)
     
     # give this thread a name
     self.name                 = 'moteProbe@'+self.serialport
     
     if not self.realserial:
         # Non-daemonized moteProbe does not consistently die on close(),
         # so ensure moteProbe does not persist.
         self.daemon           = True
     
     # connect to dispatcher
     dispatcher.connect(
         self._bufferDataToSend,
         signal = 'fromMoteConnector@'+self.serialport,
     )
 
     # start myself
     self.start()
Esempio n. 12
0
 def __init__(self,serialport=None,emulatedMote=None,iotlabmote=None,testbedmote_eui64=None):
     
     # verify params
     if   serialport:
         assert not emulatedMote
         assert not iotlabmote
         assert not testbedmote_eui64
         self.mode             = self.MODE_SERIAL
     elif emulatedMote:
         assert not serialport
         assert not iotlabmote
         assert not testbedmote_eui64
         self.mode             = self.MODE_EMULATED
     elif iotlabmote:
         assert not serialport
         assert not emulatedMote
         assert not testbedmote_eui64
         self.mode             = self.MODE_IOTLAB
     elif testbedmote_eui64:
         assert not serialport
         assert not emulatedMote
         assert not iotlabmote
         self.mode             = self.MODE_TESTBED
     else:
         raise SystemError()
     
     # store params
     if   self.mode==self.MODE_SERIAL:
         self.serialport         = serialport[0]
         self.baudrate           = serialport[1]
         self.portname           = self.serialport
     elif self.mode==self.MODE_EMULATED:
         self.emulatedMote       = emulatedMote
         self.portname           = 'emulated{0}'.format(self.emulatedMote.getId())
     elif self.mode==self.MODE_IOTLAB:
         self.iotlabmote         = iotlabmote
         self.portname           = 'IoT-LAB{0}'.format(iotlabmote)
     elif self.mode==self.MODE_TESTBED:
         self.testbedmote_eui64  = testbedmote_eui64
         self.portname           = 'opentestbed_{0}'.format(testbedmote_eui64)
     else:
         raise SystemError()
     
     # log
     log.info("creating moteProbe attaching to {0}".format(
             self.portname,
         )
     )
     
     # local variables
     self.hdlc                 = OpenHdlc.OpenHdlc()
     self.lastRxByte           = self.hdlc.HDLC_FLAG
     self.busyReceiving        = False
     self.inputBuf             = ''
     self.outputBuf            = []
     self.outputBufLock        = threading.RLock()
     self.dataLock             = threading.Lock()
     # flag to permit exit from read loop
     self.goOn                 = True
     
     self.sendToParser         = None # to be assigned
     
     if self.mode == self.MODE_TESTBED:
         # initialize variable for testbedmote
         self.serialbytes_queue       = Queue.Queue(maxsize=10) # create queue for receiving serialbytes messages
         
         # mqtt client
         self.mqttclient                = mqtt.Client()
         self.mqttclient.on_connect     = self._on_mqtt_connect
         self.mqttclient.on_message     = self._on_mqtt_message
         self.mqttclient.connect(OPENTESTBED_BROKER_ADDRESS)
         self.mqttclient.loop_start()
     
     # initialize the parent class
     threading.Thread.__init__(self)
     
     # give this thread a name
     self.name                 = 'moteProbe@'+self.portname
     
     if self.mode in [self.MODE_EMULATED,self.MODE_IOTLAB]:
         # Non-daemonized moteProbe does not consistently die on close(),
         # so ensure moteProbe does not persist.
         self.daemon           = True
     
     # connect to dispatcher
     dispatcher.connect(
         self._sendData,
         signal = 'fromMoteConnector@'+self.portname,
     )
 
     # start myself
     self.start()