class MyDynamicLengthDispatchMessage(Structure): type = DispatchField(UBInt8()) body = DispatchTarget(None, type, { 0x1F: MySimpleFixedPayload, 0xCC: MyPayloadMessage, }) eof = Magic(b'EOF')
class MyBasicDispatchMessage(Structure): type = DispatchField(UBInt8()) length = LengthField(UBInt16()) body = DispatchTarget( length, type, { 0x00: MyTargetMessage, 0x01: MyOtherTargetMessage, None: MyDefaultTargetMessage })
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 EthernetFrame(Structure): preamble_sof = Magic('\xAA' * 8) # 8 bytes of 10101010 mac_dest = UBInt8Sequence(6) mac_source = UBInt8Sequence(6) ethertype = DispatchField(UBInt16()) payload = DispatchTarget(None, ethertype, { 0x0800: IPV4Frame, # 0x85DD: IPV6Message, # 0x0806: ARPMessage, }) checksum = UBInt32()
class FieldAccessorMultiple(Structure): # FieldAccessors can be referenced multiple times, # and more than one attribute of a given field may be referenced. # TODO: Figure out how to support LengthField(bits.upper)? bits = BitField(8, upper=BitNum(4), lower=BitNum(4)) upper = bits.upper lower = bits.lower body = DispatchTarget( None, bits.lower, { 0b1010: Structure16, # 0x0A 0b0101: One, # 0x05 })
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 TypedArrayElement(Structure): type = TypeField(UBInt8(), size_mapping) value = DispatchTarget(type, type, dispatch_mapping)
class ErrorCaseSchema(Structure): type = DispatchField(UBInt8()) length = LengthField(UBInt8()) body = DispatchTarget(length, type, { 0x00: MagicSchema })
class FieldAccessorDispatch(Structure): bits = BitField(8, upper=BitNum(5), lower=BitNum(3)) body = DispatchTarget(None, bits.upper, { 0: Zero, 1: One, })
class Message(Structure): tag = DispatchField(UBInt16()) length = LengthField(UBInt16()) message = DispatchTarget(length, tag, MESSAGE_DISPATCH_MAPPING)