示例#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)))
示例#2
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)))
示例#3
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)))
示例#4
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)))
示例#5
0
    def dehdlcify(self, inBuf):
        '''
        Parse an hdlc frame.
        
        :returns: the extracted frame, or -1 if wrong checksum
        '''
        assert inBuf[0] == self.HDLC_FLAG
        assert inBuf[-1] == self.HDLC_FLAG

        # make copy of input
        outBuf = inBuf[:]
        if log.isEnabledFor(logging.DEBUG):
            log.debug("got              {0}".format(u.formatStringBuf(outBuf)))

        # remove flags
        outBuf = outBuf[1:-1]
        if log.isEnabledFor(logging.DEBUG):
            log.debug("after flags:     {0}".format(u.formatStringBuf(outBuf)))

        # unstuff
        outBuf = outBuf.replace(self.HDLC_ESCAPE + self.HDLC_FLAG_ESCAPED,
                                self.HDLC_FLAG)
        outBuf = outBuf.replace(self.HDLC_ESCAPE + self.HDLC_ESCAPE_ESCAPED,
                                self.HDLC_ESCAPE)
        if log.isEnabledFor(logging.DEBUG):
            log.debug("after unstuff:   {0}".format(u.formatStringBuf(outBuf)))

        if len(outBuf) < 2:
            raise HdlcException('packet too short')

        # check CRC
        crc = self.HDLC_CRCINIT
        for b in outBuf:
            crc = self._crcIteration(crc, b)
        if crc != self.HDLC_CRCGOOD:
            raise HdlcException('wrong CRC')

        # remove CRC
        outBuf = outBuf[:-2]  # remove CRC
        if log.isEnabledFor(logging.DEBUG):
            log.debug("after CRC:       {0}".format(u.formatStringBuf(outBuf)))

        return outBuf
示例#6
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
示例#7
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
示例#8
0
 def dehdlcify(self,inBuf):
     '''
     Parse an hdlc frame.
     
     :returns: the extracted frame, or -1 if wrong checksum
     '''
     assert inBuf[ 0]==self.HDLC_FLAG
     assert inBuf[-1]==self.HDLC_FLAG
     
     # make copy of input
     outBuf     = inBuf[:]
     if log.isEnabledFor(logging.DEBUG):
         log.debug("got              {0}".format(u.formatStringBuf(outBuf)))
     
     # remove flags
     outBuf     = outBuf[1:-1]
     if log.isEnabledFor(logging.DEBUG):
         log.debug("after flags:     {0}".format(u.formatStringBuf(outBuf)))
     
     # unstuff
     outBuf     = outBuf.replace(self.HDLC_ESCAPE+self.HDLC_FLAG_ESCAPED,   self.HDLC_FLAG)
     outBuf     = outBuf.replace(self.HDLC_ESCAPE+self.HDLC_ESCAPE_ESCAPED, self.HDLC_ESCAPE)
     if log.isEnabledFor(logging.DEBUG):
         log.debug("after unstuff:   {0}".format(u.formatStringBuf(outBuf)))
     
     if len(outBuf)<2:
         raise HdlcException('packet too short')
     
     # check CRC
     crc        = self.HDLC_CRCINIT
     for b in outBuf:
         crc    = self._crcIteration(crc,b)
     if crc!=self.HDLC_CRCGOOD:
        raise HdlcException('wrong CRC')
     
     # remove CRC
     outBuf     = outBuf[:-2] # remove CRC
     if log.isEnabledFor(logging.DEBUG):
         log.debug("after CRC:       {0}".format(u.formatStringBuf(outBuf)))
     
     return outBuf
示例#9
0
 def run(self):
     try:
         # log
         log.info("start running")
     
         while self.goOn:     # open serial port
             
             # log 
             log.info("open port {0}".format(self.portname))
             
             if   self.mode==self.MODE_SERIAL:
                 self.serial = serial.Serial(self.serialport,self.baudrate)
             elif self.mode==self.MODE_EMULATED:
                 self.serial = self.emulatedMote.bspUart
             elif self.mode==self.MODE_IOTLAB:
                 self.serial = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                 self.serial.connect((self.iotlabmote,20000))
             else:
                 raise SystemError()
             
             while self.goOn: # read bytes from serial port
                 try:
                     if   self.mode==self.MODE_SERIAL:
                         rxBytes = self.serial.read(1)
                     elif self.mode==self.MODE_EMULATED:
                         rxBytes = self.serial.read()
                     elif self.mode==self.MODE_IOTLAB:
                         rxBytes = self.serial.recv(1024)
                     else:
                         raise SystemError()
                 except Exception as err:
                     print err
                     log.warning(err)
                     time.sleep(1)
                     break
                 else:
                     for rxByte in rxBytes:
                         if      (
                                     (not self.busyReceiving)             and 
                                     self.lastRxByte==self.hdlc.HDLC_FLAG and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # start of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: start of hdlc frame {1} {2}".format(self.name, u.formatStringBuf(self.hdlc.HDLC_FLAG), u.formatStringBuf(rxByte)))
                             self.busyReceiving       = True
                             self.inputBuf            = self.hdlc.HDLC_FLAG
                             self.inputBuf           += rxByte
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # middle of frame
                             
                             self.inputBuf           += rxByte
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte==self.hdlc.HDLC_FLAG
                                 ):
                             # end of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: end of hdlc frame {1} ".format(self.name, u.formatStringBuf(rxByte)))
                             self.busyReceiving       = False
                             self.inputBuf           += rxByte
                             
                             try:
                                 tempBuf = self.inputBuf
                                 self.inputBuf        = self.hdlc.dehdlcify(self.inputBuf)
                                 if log.isEnabledFor(logging.DEBUG):
                                     log.debug("{0}: {2} dehdlcized input: {1}".format(self.name, u.formatStringBuf(self.inputBuf), u.formatStringBuf(tempBuf)))
                             except OpenHdlc.HdlcException as err:
                                 log.warning('{0}: invalid serial frame: {2} {1}'.format(self.name, err, u.formatStringBuf(tempBuf)))
                             else:
                                 if self.inputBuf==chr(OpenParser.OpenParser.SERFRAME_MOTE2PC_REQUEST):
                                     with self.outputBufLock:
                                         if self.outputBuf:
                                             outputToWrite = self.outputBuf.pop(0)
                                             self.serial.write(outputToWrite)
                                 else:
                                     # dispatch
                                     dispatcher.send(
                                         sender        = self.name,
                                         signal        = 'fromMoteProbe@'+self.portname,
                                         data          = [ord(c) for c in self.inputBuf],
                                     )
                         
                         self.lastRxByte = rxByte
                     
                 if self.mode==self.MODE_EMULATED:
                     self.serial.doneReading()
     except Exception as err:
         errMsg=u.formatCrashMessage(self.name,err)
         print errMsg
         log.critical(errMsg)
         sys.exit(-1)
示例#10
0
 def run(self):
     try:
         # log
         log.info("start running")
     
         while self.goOn:     # open serial port
             
             # log 
             log.info("open port {0}".format(self.portname))
             
             if   self.mode==self.MODE_SERIAL:
                 self.serial = serial.Serial(self.serialport,self.baudrate)
                 self.serial.setDTR(0)
                 self.serial.setRTS(0)
             elif self.mode==self.MODE_EMULATED:
                 self.serial = self.emulatedMote.bspUart
             elif self.mode==self.MODE_IOTLAB:
                 self.serial = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                 self.serial.connect((self.iotlabmote,20000))
             else:
                 raise SystemError()
             
             while self.goOn: # read bytes from serial port
                 try:
                     if   self.mode==self.MODE_SERIAL:
                         rxBytes = self.serial.read(1)
                     elif self.mode==self.MODE_EMULATED:
                         rxBytes = self.serial.read()
                     elif self.mode==self.MODE_IOTLAB:
                         rxBytes = self.serial.recv(1024)
                     else:
                         raise SystemError()
                 except Exception as err:
                     print err
                     log.warning(err)
                     time.sleep(1)
                     break
                 else:
                     for rxByte in rxBytes:
                         if      (
                                     (not self.busyReceiving)             and 
                                     self.lastRxByte==self.hdlc.HDLC_FLAG and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # start of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: start of hdlc frame {1} {2}".format(self.name, u.formatStringBuf(self.hdlc.HDLC_FLAG), u.formatStringBuf(rxByte)))
                             self.busyReceiving       = True
                             self.inputBuf            = self.hdlc.HDLC_FLAG
                             self.inputBuf           += rxByte
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # middle of frame
                             
                             self.inputBuf           += rxByte
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte==self.hdlc.HDLC_FLAG
                                 ):
                             # end of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: end of hdlc frame {1} ".format(self.name, u.formatStringBuf(rxByte)))
                             self.busyReceiving       = False
                             self.inputBuf           += rxByte
                             
                             try:
                                 tempBuf = self.inputBuf
                                 self.inputBuf        = self.hdlc.dehdlcify(self.inputBuf)
                                 if log.isEnabledFor(logging.DEBUG):
                                     log.debug("{0}: {2} dehdlcized input: {1}".format(self.name, u.formatStringBuf(self.inputBuf), u.formatStringBuf(tempBuf)))
                             except OpenHdlc.HdlcException as err:
                                 log.warning('{0}: invalid serial frame: {2} {1}'.format(self.name, err, u.formatStringBuf(tempBuf)))
                             else:
                                 if self.inputBuf==chr(OpenParser.OpenParser.SERFRAME_MOTE2PC_REQUEST):
                                     with self.outputBufLock:
                                         if self.outputBuf:
                                             outputToWrite = self.outputBuf.pop(0)
                                             self.serial.write(outputToWrite)
                                 else:
                                     # dispatch
                                     dispatcher.send(
                                         sender        = self.name,
                                         signal        = 'fromMoteProbe@'+self.portname,
                                         data          = [ord(c) for c in self.inputBuf],
                                     )
                         
                         self.lastRxByte = rxByte
                     
                 if self.mode==self.MODE_EMULATED:
                     self.serial.doneReading()
     except Exception as err:
         errMsg=u.formatCrashMessage(self.name,err)
         print errMsg
         log.critical(errMsg)
         sys.exit(-1)
示例#11
0
 def run(self):
     try:
         # log
         log.info("start running")
     
         while self.goOn:     # open serial port
             
             # log 
             log.info("open port {0}".format(self.portname))
             
             if   self.mode==self.MODE_SERIAL:
                 self.serial = serial.Serial(self.serialport,self.baudrate,timeout=1,xonxoff=True,rtscts=False,dsrdtr=False)
             elif self.mode==self.MODE_EMULATED:
                 self.serial = self.emulatedMote.bspUart
             elif self.mode==self.MODE_IOTLAB:
                 self.serial = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                 self.serial.connect((self.iotlabmote,20000))
             elif self.mode==self.MODE_TESTBED:
                 # subscribe to topic: opentestbed/deviceType/mote/deviceId/00-12-4b-00-14-b5-b6-49/notif/frommoteserialbytes
                 self.mqtt_seriaqueue = self.serialbytes_queue
             else:
                 raise SystemError()
             
             while self.goOn: # read bytes from serial port
                 try:
                     if   self.mode==self.MODE_SERIAL:
                         rxBytes = self.serial.read(1)
                         if rxBytes == 0: # timeout
                             continue
                     elif self.mode==self.MODE_EMULATED:
                         rxBytes = self.serial.read()
                     elif self.mode==self.MODE_IOTLAB:
                         rxBytes = self.serial.recv(1024)
                     elif self.mode==self.MODE_TESTBED:
                         rxBytes = self.mqtt_seriaqueue.get()
                         rxBytes = [chr(i) for i in rxBytes]
                     else:
                         raise SystemError()
                 except Exception as err:
                     print err
                     log.warning(err)
                     time.sleep(1)
                     break
                 else:
                     for rxByte in rxBytes:
                         if      (
                                     (not self.busyReceiving)             and 
                                     self.lastRxByte==self.hdlc.HDLC_FLAG and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # start of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: start of hdlc frame {1} {2}".format(self.name, u.formatStringBuf(self.hdlc.HDLC_FLAG), u.formatStringBuf(rxByte)))
                             self.busyReceiving       = True
                             self.xonxoffEscaping     = False
                             self.inputBuf            = self.hdlc.HDLC_FLAG
                             self._addToInputBuf(rxByte)
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # middle of frame
                             
                             self._addToInputBuf(rxByte)
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte==self.hdlc.HDLC_FLAG
                                 ):
                             # end of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: end of hdlc frame {1} ".format(self.name, u.formatStringBuf(rxByte)))
                             self.busyReceiving       = False
                             self._addToInputBuf(rxByte)
                             
                             try:
                                 tempBuf = self.inputBuf
                                 self.inputBuf        = self.hdlc.dehdlcify(self.inputBuf)
                                 if log.isEnabledFor(logging.DEBUG):
                                     log.debug("{0}: {2} dehdlcized input: {1}".format(self.name, u.formatStringBuf(self.inputBuf), u.formatStringBuf(tempBuf)))
                             except OpenHdlc.HdlcException as err:
                                 log.warning('{0}: invalid serial frame: {2} {1}'.format(self.name, err, u.formatStringBuf(tempBuf)))
                             else:
                                 if self.sendToParser:
                                     self.sendToParser([ord(c) for c in self.inputBuf])
                         
                         self.lastRxByte = rxByte
                     
                 if self.mode==self.MODE_EMULATED:
                     self.serial.doneReading()
     except Exception as err:
         errMsg=u.formatCrashMessage(self.name,err)
         print errMsg
         log.critical(errMsg)
         sys.exit(-1)
     finally:
         if self.mode==self.MODE_SERIAL and self.serial is not None:
             self.serial.close()
示例#12
0
 def run(self):
     try:
         # log
         log.info("start running")
     
         while self.goOn:     # open serial port
             
             # log 
             log.info("open port {0}".format(self.portname))
             
             if   self.mode==self.MODE_SERIAL:
                 self.serial = serial.Serial(self.serialport,self.baudrate,timeout=1,rtscts=False,dsrdtr=False)
             elif self.mode==self.MODE_EMULATED:
                 self.serial = self.emulatedMote.bspUart
             elif self.mode==self.MODE_IOTLAB:
                 self.serial = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                 self.serial.connect((self.iotlabmote,20000))
             elif self.mode==self.MODE_TESTBED:
                 # subscribe to topic: opentestbed/deviceType/mote/deviceId/00-12-4b-00-14-b5-b6-49/notif/frommoteserialbytes
                 self.mqtt_seriaqueue = self.serialbytes_queue
             else:
                 raise SystemError()
             
             while self.goOn: # read bytes from serial port
                 try:
                     if   self.mode==self.MODE_SERIAL:
                         rxBytes = self.serial.read(1)
                         if rxBytes == 0: # timeout
                             continue
                     elif self.mode==self.MODE_EMULATED:
                         rxBytes = self.serial.read()
                     elif self.mode==self.MODE_IOTLAB:
                         rxBytes = self.serial.recv(1024)
                     elif self.mode==self.MODE_TESTBED:
                         rxBytes = self.mqtt_seriaqueue.get()
                         rxBytes = [chr(i) for i in rxBytes]
                     else:
                         raise SystemError()
                 except Exception as err:
                     print err
                     log.warning(err)
                     time.sleep(1)
                     break
                 else:
                     for rxByte in rxBytes:
                         if      (
                                     (not self.busyReceiving)             and 
                                     self.lastRxByte==self.hdlc.HDLC_FLAG and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # start of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: start of hdlc frame {1} {2}".format(self.name, u.formatStringBuf(self.hdlc.HDLC_FLAG), u.formatStringBuf(rxByte)))
                             self.busyReceiving       = True
                             self.inputBuf            = self.hdlc.HDLC_FLAG
                             self.inputBuf           += rxByte
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte!=self.hdlc.HDLC_FLAG
                                 ):
                             # middle of frame
                             
                             self.inputBuf           += rxByte
                         elif    (
                                     self.busyReceiving                   and
                                     rxByte==self.hdlc.HDLC_FLAG
                                 ):
                             # end of frame
                             if log.isEnabledFor(logging.DEBUG):
                                 log.debug("{0}: end of hdlc frame {1} ".format(self.name, u.formatStringBuf(rxByte)))
                             self.busyReceiving       = False
                             self.inputBuf           += rxByte
                             
                             try:
                                 tempBuf = self.inputBuf
                                 with open(socket.gethostname()+'.log','a') as f:
                                     f.write(self.inputBuf)
                                 self.inputBuf        = self.hdlc.dehdlcify(self.inputBuf)
                                 if log.isEnabledFor(logging.DEBUG):
                                     log.debug("{0}: {2} dehdlcized input: {1}".format(self.name, u.formatStringBuf(self.inputBuf), u.formatStringBuf(tempBuf)))
                             except OpenHdlc.HdlcException as err:
                                 log.warning('{0}: invalid serial frame: {2} {1}'.format(self.name, err, u.formatStringBuf(tempBuf)))
                             else:
                                 if self.inputBuf==chr(OpenParser.OpenParser.SERFRAME_MOTE2PC_REQUEST):
                                     with self.outputBufLock:
                                         if self.outputBuf:
                                             outputToWrite = self.outputBuf.pop(0)
                                             self.serial.write(outputToWrite)
                                 else:
                                     # dispatch
                                     dispatcher.send(
                                         sender        = self.name,
                                         signal        = 'fromMoteProbe@'+self.portname,
                                         data          = [ord(c) for c in self.inputBuf],
                                     )
                         
                         self.lastRxByte = rxByte
                     
                 if self.mode==self.MODE_EMULATED:
                     self.serial.doneReading()
     except Exception as err:
         errMsg=u.formatCrashMessage(self.name,err)
         print errMsg
         log.critical(errMsg)
         sys.exit(-1)
     finally:
         if self.mode==self.MODE_SERIAL and self.serial is not None:
             self.serial.close()