def _unpack_words(self, fstring, handle): """ Un Packs Words based on the word order and byte order # ---------------------------------------------- # # Unpack in to network ordered unsigned integer # # Change Word order if little endian word order # # Pack values back based on correct byte order # # ---------------------------------------------- # :param handle: Value to be unpacked :return: """ handle = make_byte_string(handle) wc = WC.get(fstring.lower()) // 2 up = "!{}H".format(wc) handle = unpack(up, handle) if self._wordorder == Endian.Little: handle = list(reversed(handle)) # Repack as unsigned Integer pk = self._byteorder + 'H' handle = [pack(pk, p) for p in handle] handle = b''.join(handle) _logger.debug(unicode_string(handle)) return handle
def _unpack_words(self, fstring, handle): """ Un Packs Words based on the word order and byte order # ---------------------------------------------- # # Unpack in to network ordered unsigned integer # # Change Word order if little endian word order # # Pack values back based on correct byte order # # ---------------------------------------------- # :param handle: Value to be unpacked :return: """ handle = make_byte_string(handle) wc = WC.get(fstring.lower())//2 up = "!{}H".format(wc) handle = unpack(up, handle) if self._wordorder == Endian.Little: handle = list(reversed(handle)) # Repack as unsigned Integer pk = self._byteorder + 'H' handle = [pack(pk, p) for p in handle] handle = b''.join(handle) _logger.debug(handle) return handle
def decode_8bit_uint(self): """ Decodes a 8 bit unsigned int from the buffer """ self._pointer += 1 fstring = self._byteorder + 'B' handle = self._payload[self._pointer - 1:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def add_string(self, value): """ Adds a string to the buffer :param value: The value to add to the buffer """ value = make_byte_string(value) fstring = self._byteorder + str(len(value)) + 's' self._payload.append(pack(fstring, value))
def add_string(self, value): ''' Adds a string to the buffer :param value: The value to add to the buffer ''' value = make_byte_string(value) fstring = self._endian + str(len(value)) + 's' self._payload.append(pack(fstring, value))
def decode_64bit_float(self): ''' Decodes a 64 bit float(double) from the buffer ''' self._pointer += 8 fstring = self._endian + 'd' handle = self._payload[self._pointer - 8:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_32bit_float(self): ''' Decodes a 32 bit float from the buffer ''' self._pointer += 4 fstring = self._endian + 'f' handle = self._payload[self._pointer - 4:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def add_string(self, value): """ Adds a string to the buffer :param value: The value to add to the buffer """ value = make_byte_string(value) fstring = self._byteorder + str(len(value)) + 's' self._payload.append(pack(fstring, value))
def decode_64bit_uint(self): ''' Decodes a 64 bit unsigned int from the buffer ''' self._pointer += 8 fstring = self._endian + 'Q' handle = self._payload[self._pointer - 8:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def add_string(self, value): ''' Adds a string to the buffer :param value: The value to add to the buffer ''' value = make_byte_string(value) fstring = self._endian + str(len(value)) + 's' self._payload.append(pack(fstring, value))
def decode_64bit_float(self): ''' Decodes a 64 bit float(double) from the buffer ''' self._pointer += 8 fstring = self._endian + 'd' handle = self._payload[self._pointer - 8:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_32bit_float(self): ''' Decodes a 32 bit float from the buffer ''' self._pointer += 4 fstring = self._endian + 'f' handle = self._payload[self._pointer - 4:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_8bit_int(self): ''' Decodes a 8 bit signed int from the buffer ''' self._pointer += 1 fstring = self._endian + 'b' handle = self._payload[self._pointer - 1:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_64bit_uint(self): ''' Decodes a 64 bit unsigned int from the buffer ''' self._pointer += 8 fstring = self._endian + 'Q' handle = self._payload[self._pointer - 8:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_bits(self): """ Decodes a byte worth of bits from the buffer """ self._pointer += 1 # fstring = self._endian + 'B' handle = self._payload[self._pointer - 1:self._pointer] handle = make_byte_string(handle) return unpack_bitstring(handle)
def decode_16bit_int(self): """ Decodes a 16 bit signed int from the buffer """ self._pointer += 2 fstring = self._byteorder + 'h' handle = self._payload[self._pointer - 2:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_8bit_int(self): ''' Decodes a 8 bit signed int from the buffer ''' self._pointer += 1 fstring = self._endian + 'b' handle = self._payload[self._pointer - 1:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_16bit_int(self): """ Decodes a 16 bit signed int from the buffer """ self._pointer += 2 fstring = self._byteorder + 'h' handle = self._payload[self._pointer - 2:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_8bit_uint(self): """ Decodes a 8 bit unsigned int from the buffer """ self._pointer += 1 fstring = self._byteorder + 'B' handle = self._payload[self._pointer - 1:self._pointer] handle = make_byte_string(handle) return unpack(fstring, handle)[0]
def decode_bits(self): """ Decodes a byte worth of bits from the buffer """ self._pointer += 1 # fstring = self._endian + 'B' handle = self._payload[self._pointer - 1:self._pointer] handle = make_byte_string(handle) return unpack_bitstring(handle)
def append_crc(builder): crc = ut.computeCRC(ut.make_byte_string(builder.to_string())) #print(crc) hex_str = '%04x' % crc hex1 = int(hex_str[0] + hex_str[1], 16) hex2 = int(hex_str[2] + hex_str[3], 16) builder.add_8bit_uint(hex1) builder.add_8bit_uint(hex2)
def _unpack_words(self, fstring, handle): """ Packs Words based on the word order :param handle: Value to be unpacked :return: """ handle = make_byte_string(handle) if self._wordorder == Endian.Little: handle = [handle[i:i + 2] for i in range(0, len(handle), 2)] if self._byteorder == Endian.Big: handle = b''.join(list(reversed(handle))) else: handle = b''.join(handle) elif self._wordorder == Endian.Big and self._byteorder == Endian.Little: handle = [handle[i:i + 2] for i in range(0, len(handle), 2)] handle = b''.join(list(reversed(handle))) return handle