Example #1
0
    def _recv(self):
        """
            Called repeatedly by RxServer.server_forever
            May return None or one or more messages
        """
        #Do once per call
        msgs=[]
        
        # Read from serial device
        if self.ser:
            s = self.ser.read(1000)
            try:
                n = len(s)
            except TypeError:
                n = 0       
        else:
            logger.error("No serial device")
            self._connect()
            time.sleep(5)
            n = 0                
        
        #logger.debug("Received %d bytes" % n)

        # Process received data          
        if n > 0:
            lst = []
            
            w = s.split(chr(0xc0))
            n = len(w)
            
            # No 0xc0 in frame
            if n == 1:
                self.buf += w[0]
        
            # Single 0xc0 in frame
            elif n == 2:
                # Closing 0xc0 found
                if not w[0] == '':
                    # Partial frame continued, otherwise drop
                    lst.append(self.buf + w[0])
                    self.buf = ''
        
                # Opening 0xc0 found
                else:
                    lst.append(self.buf)
                    self.buf = w[1]
        
            # At least one complete frane received
            elif n >= 3:                   
                for i in range(0, n - 1):
                    st = self.buf + w[i]       
                    if not st == '':
                        lst.append(st)
                        self.buf = ''         
                if not w[n - 1] == '':
                    self.buf = w[n - 1]                

            # Loop through received frames
            for p in lst:
                if len(p) == 0:
                    continue                       
                if ord(p[0]) == 0:  #received a data frame
                    #hex_str = " ".join([ "{:02x}".format(ord(c)) for c in p[1:] ])
                    #logger.debug("MSG RAW (HEX): "+hex_str)
                    raw_str = ax25.kiss2raw(p[1:])
                    msg_tuple = ax25.parse_raw_msg(raw_str)
                    if msg_tuple:               
                        strfrom, strto, msg_data, digis = msg_tuple         
                        #logger.debug(msg_data)
                        if len(msg_data) < MESSAGE_HEADER_LEN:
                            logger.error("Did not receive enough "
                                          "data to be a NodeMessage.")
                            #logger.error(`msg_data`)
                            continue
                	    #Convert to Node Message
                        node_msg = NodeMessage(msg_data)
                        if node_msg is not None:      
                            msgs.append((node_msg, strfrom))
                                             
        #return a list of (message, addr) tuples
        if msgs:              
            return msgs
        else:
            return None
Example #2
0
    def serve_forever(self):
        """
            This method will loop forever and appropriately handle all of our 
            received messages, either processing them or forwarding them.
        """

        buf = ''
        # Main program loop    
        while True:
            # Read serial line
            if self.ser <> None:
                s = self.ser.read(1000)
                try:
                    n = len(s)
                except TypeError:
                    n = 0       
            else:
                n = 0                
            
            # Process received data          
            if n <> 0:
                lst = []
                
                w = s.split(chr(0xc0))
                n = len(w)
                
                # No 0xc0 in frame
                if n == 1:
                    buf += w[0]
            
                # Single 0xc0 in frame
                elif n == 2:
                    # Closing 0xc0 found
                    if w[0] <> '':
                        # Partial frame continued, otherwise drop
                        lst.append(buf + w[0])
                        buf = ''
            
                    # Opening 0xc0 found
                    else:
                        lst.append(buf)
                        buf = w[1]
            
                # At least one complete frane received
                elif n >= 3:                   
                    for i in range(0, n - 1):
                        st = buf + w[i]       
                        if st <> '':
                            lst.append(st)
                            buf = ''         
                    if w[n - 1] <> '':
                        buf = w[n - 1]                

                # Loop through received frames
                for p in lst:
                    if len(p) == 0:
                        continue                       
                    if ord(p[0]) == 0:
                        hex_str = " ".join([ "{:02x}".format(ord(c)) for c in p[1:] ])
                        #logger.debug("MSG RAW (HEX): "+hex_str)
                        raw_str = ax25.kiss2raw(p[1:])
                        msg_tuple = ax25.parse_raw_msg(raw_str)
                        if msg_tuple:               
                            strfrom, strto, msg_data, digis = msg_tuple         
                            #logger.debug(msg_data)
                            #if len(msg_data) < MESSAGE_HEADER_LEN:
                            #    logger.error("Did not receive enough \
                            #                  data to be a NodeMessage.")
                            #    #logger.error(`msg_data`)
                            #    continue
                            
                    	    #Convert to Node Message
                            #node_msg = NodeMessage(msg_data)
                            print msg_data

                            #if node_msg is not None:    
                                #Add to queue  
                                # Filter messages from our self.  (Pretty likely with RF)
                                #if node_msg.source == self.my_node_id:
                                #    logger.debug("Got message from myself, ignoring.")
                                #    continue
                                # Add to our RX queue
                                #RX_QUEUE.put((node_msg, strfrom))
                    # Control frame received
                    else:
                        pass