class BoxedGreedy(Structure): sof = Magic(b'\xAA') a = LengthField(UBInt8()) b = UBInt8() payload = Payload() c = UBInt16() d = VariableRawPayload(a) eof = Magic(b'\xbb')
class ConditionalDispatch(Structure): f1 = DispatchField(UBInt8()) f2 = UBInt8() # separate from f1 length = LengthField(UBInt8()) f3 = ConditionalField(DispatchTarget(length, f1, { 0: EmptyStructure, 1: BasicMessage }), condition=lambda m: m.f2 == 255)
class MyBasicDispatchMessage(Structure): type = DispatchField(UBInt8()) length = LengthField(UBInt16()) body = DispatchTarget( length, type, { 0x00: MyTargetMessage, 0x01: MyOtherTargetMessage, None: MyDefaultTargetMessage })
class BGAPIFrame(Structure): head = LengthField(BitField(16, is_event=BitBool(), technology_type=BitNum(4), length=BitNum(11)), get_length=lambda f: f.length, set_length=lambda f, l: setattr(f, 'length', l)) class_id = UBInt8() command_id = UBInt8() payload = Payload(head)
class MyTargetMessage(Structure): # inherited from the parent message _length = LengthField(DependentField('length')) payload = VariableRawPayload(_length)
class MyVarSeqMessage(Structure): type = UBInt8() length = LengthField(UBInt8()) seq = UBInt8Sequence(length)
class SuperChild(Structure): options = DependentField('options') ubseq = DependentField('ubseq') length = LengthField(DependentField('submessage_length')) remaining = VariableRawPayload(length)
class MyLengthyMessage(Structure): length = LengthField(UBInt16()) payload = VariableRawPayload(length)
class ErrorCaseSchema(Structure): type = DispatchField(UBInt8()) length = LengthField(UBInt8()) body = DispatchTarget(length, type, { 0x00: MagicSchema })
class ConditionalLength(Structure): f1 = UBInt8() f2 = ConditionalField(LengthField(UBInt8()), lambda m: m.f1 == 255) f3 = ConditionalField(Payload(length_provider=f2), lambda m: m.f1 == 255)
class PascalString16(Structure): length = LengthField(UBInt16()) value = Payload(length)
class BasicMessageArrayNumElements(Structure): count = LengthField(UBInt8()) array = FieldArray(BasicMessage, num_elements_provider=count)
class BasicMessageArrayAfter(Structure): count = LengthField(UBInt8(), multiplier=2) array = FieldArray(BasicMessage, count) after = UBInt8()
class UDPFrame(Structure): source_port = UBInt16() destination_port = UBInt16() length = LengthField(UBInt16()) checksum = UBInt16() data = VariableRawPayload(length)
class MyOtherTargetMessage(Structure): #: Inherited from the parent message _length = LengthField(DependentField('length')) sequence = UBInt8Sequence(_length)
class MyDefaultTargetMessage(Structure): _length = LengthField(DependentField('length')) sequence = UBInt8Sequence(_length)
class ConditionalArrayGreedyAfter(Structure): length = LengthField(UBInt8()) array = FieldArray(ConditionalArrayElement, length) greedy = Payload()
class SuperMessage(Structure): magic = Magic(b'\xAA\xAA') # bitfield options = BitField(8, b1=BitBool(), b2=BitBool(), rest=BitNum(6)) # unsigned big endian ubint8 = UBInt8() ubint16 = UBInt16() ubint24 = UBInt24() ubint32 = UBInt32() ubint64 = UBInt64() # signed big endian sbint8 = SBInt8() sbint16 = SBInt16() sbint32 = SBInt32() sbint64 = SBInt64() # unsigned little endian ulint8 = ULInt8() ulint16 = ULInt16() ulint32 = ULInt32() ulint64 = ULInt64() # signed little endian slint8 = SLInt8() slint16 = SLInt16() slint32 = SLInt32() slint64 = SLInt64() # optional optional_one = ConditionalField(UBInt8(), lambda m: m.options.b1) optional_two = ConditionalField(UBInt8(), lambda m: m.options.b2) # sequences with variable lengths ubseql = LengthField(UBInt8()) ubseq = UBInt8Sequence(ubseql) sbseql = LengthField(UBInt8()) sbseq = SBInt8Sequence(sbseql) # sequences with fixed lengths ubseqf = UBInt8Sequence(5) sbseqf = SBInt8Sequence(5) # don't change anything... for test coverage ulint16_value = FieldProperty(ulint16) ulint16_byte_string = FieldProperty( ulint16, onget=lambda v: str(v), onset=lambda v: struct.unpack(">H", v)[0]) message_type = DispatchField(UBInt8()) submessage_length = LengthField(UBInt16()) submessage = DispatchTarget(submessage_length, message_type, {0xEF: SuperChild}) # checksum starts after beginning magic, ends before # the checksum crc = CRCField(UBInt16(), crc16_ccitt, 2, -3) eof = Magic(b'~')
class MyMuiltipliedLengthMessage(Structure): length = LengthField(UBInt8(), multiplier=8) payload = VariableRawPayload(length)
class EchoProtocolFrame(Structure): frame_type = UBInt8() payload_length = LengthField(UBInt16()) payload = Payload(payload_length)
class Message(Structure): tag = DispatchField(UBInt16()) length = LengthField(UBInt16()) message = DispatchTarget(length, tag, MESSAGE_DISPATCH_MAPPING)