Exemple #1
0
    def fill(self, byte):
        """
        fill: byte -> None

        Adds the given raw byte to this APIFrame. If this APIFrame is marked
        as escaped and this byte is an escape byte, the next byte in a call
        to fill() will be unescaped.
        """

        if self._unescape_next_byte:
            byte = intToByte(byteToInt(byte) ^ 0x20)
            self._unescape_next_byte = False
        elif self.escaped and byte == APIFrame.ESCAPE_BYTE:
            self._unescape_next_byte = True
            return

        self.raw_data += intToByte(byteToInt(byte))
Exemple #2
0
    def escape(data):
        """
        escape: byte string -> byte string

        When a 'special' byte is encountered in the given data string,
        it is preceded by an escape byte and XORed with 0x20.
        """

        escaped_data = b""
        for byte in data:
            if intToByte(byteToInt(byte)) in APIFrame.ESCAPE_BYTES:
                escaped_data += APIFrame.ESCAPE_BYTE
                escaped_data += intToByte(0x20 ^ byteToInt(byte))
            else:
                escaped_data += intToByte(byteToInt(byte))
                    
        return escaped_data
Exemple #3
0
 def checksum(self):
     """
     checksum: None -> single checksum byte
     
     checksum adds all bytes of the binary, unescaped data in the 
     frame, saves the last byte of the result, and subtracts it from 
     0xFF. The final result is the checksum
     """
     total = 0
     
     # Add together all bytes
     for byte in self.data:
         total += byteToInt(byte)
         
     # Only keep the last byte
     total = total & 0xFF
     
     return intToByte(0xFF - total)