Ejemplo n.º 1
0
def make_packet(payload, samples_per_symbol, bits_per_symbol,
		preamble, access_code,
                pad_for_usrp=True, do_whitening=False, add_crc=False):
	"""
	Build a packet

	Args:
		payload: packet payload, len [0, 4096]
		samples_per_symbol: samples per symbol (needed for padding calculation) (int)
		bits_per_symbol: (needed for padding calculation) (int)
		preamble: (eg: 10101010...)
		access_code: the sync word
		pad_for_usrp:
		do_whitening: ccxxxx whitening version
		add_crc: add CRC16 (2 bytes) checksum
		
	Packet will have access code at the beginning, followed by length (1 byte), payload
	and finally CRC-16.
	"""

	if not packet_utils.is_1_0_string(preamble):
		raise ValueError, "preamble must be a string containing only 0's and 1's (%r)" % (preamble,)

	if not packet_utils.is_1_0_string(access_code):
		raise ValueError, "access_code must be a string containing only 0's and 1's (%r)" % (access_code,)

	(packed_access_code, padded) = packet_utils.conv_1_0_string_to_packed_binary_string(access_code)
	(packed_preamble, ignore) = packet_utils.conv_1_0_string_to_packed_binary_string(preamble)

	# len
	payload_length = len(payload)
	# header (1 byte)
	pkt_hd = make_header(payload_length)
	# data
	pkt_dt = payload
	# final message
	final_message = ''.join((pkt_hd, pkt_dt))

	# CRC ?
	if add_crc:
		crc = crc16(final_message)
		#print "crc: %02x %02x" % (ord(crc[0:1]), ord(crc[1:2]))
		final_message = ''.join((final_message, crc))

	# Whitening ?
	pkt = ''
	if do_whitening:
		pkt = ''.join((packed_preamble, packed_access_code,  whiten(final_message), '\x55'))
	else:
        	pkt = ''.join((packed_preamble, packed_access_code, final_message, '\x55'))

	# Padding ?
	if pad_for_usrp:
        	usrp_packing = packet_utils._npadding_bytes(len(pkt), samples_per_symbol, bits_per_symbol) * '\x55'
        	pkt = pkt + usrp_packing
	
	return pkt
Ejemplo n.º 2
0
def make_packet(payload, samples_per_symbol, bits_per_symbol, pad_for_usrp=True):
	"""
	Build a packet

	Args:
		payload: packet payload, len [0, 4096]
		samples_per_symbol: samples per symbol (needed for padding calculation) (int)
		bits_per_symbol: (needed for padding calculation) (int)
		pad_for_usrp:

	Packet will have the preamble and access code at the beginning, followed by
	the encoded payload and an 8-bit CRC.
	"""

	# CRC
	crc = crc8(payload)
	raw_message = ''.join((payload, crc))

	# 4b/6b encoding
	encoded_nibbles = []
	for element in raw_message:
		for shift in [4, 0]:
			encoded_nibble = ENCODER_TABLE[(ord(element) >> shift) & 0xf]
			encoded_nibbles.append(encoded_nibble)

	# Nibble to bit conversion
	bits = []
	for element in encoded_nibbles:
		for bit_index in range(0, 6)[::-1]:
			bit = (element >> bit_index) & 0x01
			bits.append(bit)

	# Bit padding
	if len(bits) % 8:
		padding = [0, 1] * ((len(bits) % 8) / 2)
		bits.extend(padding)

	# Convert the bits to bytes
	encoded_message = str(bits_to_bytes(bits))

	# Prepend the preamble and sync words/access code to the message
	packet = ''.join((PREAMBLE, ACCESS_CODE, encoded_message, PADDING))

	# Padding (optional)
	if pad_for_usrp:
		usrp_packing = packet_utils._npadding_bytes(len(packet), samples_per_symbol, bits_per_symbol) * '\x00'
		packet = packet + usrp_packing

	return packet