class VLANIDMessage(MessageStructure): value = Payload() def get_conf(payload): if payload == None or len(payload) == 0: return None vlanid = struct.unpack('!H', payload[0:2])[0] pp = ord(payload[2]) ports = [] for p in range(0, 8): if pp & (1 << (7 - p)): ports.append(p + 1) return (vlanid, ports) def set_conf(conf): if conf == None or conf[0] == None: return '\x00\x00\x00' vlanid = conf[0] ports = conf[1] pp = 0 for p in range(0, 8): if p + 1 in ports: pp = pp | (1 << (7 - p)) return struct.pack('!HB', vlanid, pp) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class PortMirrorMessage(MessageStructure): value = Payload() def get_conf(payload): if len(payload) == 0: return None sp = ord(payload[2]) source_ports = [] for p in range(0, 8): if sp & (1 << (7 - p)): source_ports.append(p + 1) return (ord(payload[0]), source_ports) def set_conf(conf): if conf == None or conf[0] == None: return '\x00\x00\x00' destination_port = conf[0] source_ports = conf[1] sp = 0 for p in range(0, 8): if p + 1 in source_ports: sp = sp + (1 << (7 - p)) return chr(destination_port) + '\x00' + chr(sp) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class Robot2DriverStationPacket(Structure): control_byte = BitField(8, reset=BitBool(), not_estop=BitBool(), enabled=BitBool(), autonmous=BitBool(), fms_attacted=BitBool(), resync=BitBool(), test=BitBool(), fpga_checksum=BitBool()) battery_voltage = UBInt16() ds_digital_in = UBInt8() unknown_1 = UBInt32() team_number = UBInt16() crio_mac_address = UBInt48() version = UBInt64() unknown_2b = UBInt40() reported_state = UBInt8() packet_index = UBInt16() unknown_4 = Payload(988) crc_checksum = CRCField(UBInt32(), crc32, 0, 1024) def reported_state_str(self): if self.reported_state == 0: return c.NO_CODE elif self.reported_state == 1: return c.DISABLED elif self.reported_state == 4: return c.TELEOP else: return "Unknown: " + str(self.reported_state)
class IGMPSnoopingStatusMessage(MessageStructure): value = Payload() vlanid = FieldProperty(value, onget=lambda v: None if len(v) == 0 else struct.unpack('!H', v[2:4])[0] if v[0:2] == '\x00\x01' else 0, onset=lambda v: struct.pack('!HH', 1 if v > 0 else 0, v))
class BoxedGreedy(Structure): sof = Magic(b'\xAA') a = LengthField(UBInt8()) b = UBInt8() payload = Payload() c = UBInt16() d = VariableRawPayload(a) eof = Magic(b'\xbb')
class TestCableResultMessage(MessageStructure): value = Payload() port_result_meters = FieldProperty( value, onget=lambda v: None if len(v) == 0 else (ord(v[0]), None, None) if len(v) == 1 else struct.unpack('!BII', v), onset=lambda v: chr(v[0]) if v[1] == None or v[2] == None else struct.pack( '!BII', v[0], v[1], v[2]))
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 BroadcastBandwidthMessage(MessageStructure): value = Payload() def get_conf(payload): if payload == None or len(payload) != 5: return None return struct.unpack('!BI', payload) def set_conf(conf): port = conf[0] bandwidth = conf[1] return struct.pack('!BI', port, bandwidth) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class VLANID802Message(MessageStructure): value = Payload() def get_conf(payload): if payload == None or len(payload) == 0: return None vlanid = struct.unpack('!H', payload[0:2])[0] if len(payload) == 2: return (vlanid, None) mp = ord(payload[2]) & (~ord(payload[3])) untag_ports = [] for p in range(0, 8): if mp & (1 << (7 - p)): untag_ports.append(p + 1) mp = ord(payload[3]) tag_ports = [] for p in range(0, 8): if mp & (1 << (7 - p)): tag_ports.append(p + 1) return (vlanid, tag_ports, untag_ports) def set_conf(conf): if conf == None or conf[0] == None: return '\x00\x00\x00\x00' # TODO vlanid = conf[0] tag_ports = conf[1] untag_ports = conf[2] if tag_ports == None or untag_ports == None: return struct.pack('!H', vlanid) tp = 0 for p in range(0, 8): if p + 1 in tag_ports: tp = tp | (1 << (7 - p)) up = tp for p in range(0, 8): if p + 1 in untag_ports: up = up | (1 << (7 - p)) return struct.pack('!HBB', vlanid, up, tp) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class DNSMessage(Structure): identification = UBInt16() fields = BitField( 16, is_reply=BitBool(), # QR opcode=BitNum(4), truncated=BitBool(), recursion_desired=BitBool(), ra=BitBool(), z=BitBool(), non_authenticated_data_acceptable=BitBool(), cd=BitBool(), rcode=BitNum(4), ) total_questions = UBInt16() total_answers_rrs = UBInt16() total_authority_rrs = UBInt16() total_additional_rrs = UBInt16() data = Payload() # greedy
class TypeFieldWrongSize(Structure): type = TypeField(UBInt8(), size_mapping) greedy = Payload(type)
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 ConditionalArrayGreedyAfter(Structure): length = LengthField(UBInt8()) array = FieldArray(ConditionalArrayElement, length) greedy = Payload()
class PortbasedQOSMessage(MessageStructure): value = Payload() config = FieldProperty(value, onget=lambda v: None if len(v) == 0 else struct.unpack('!BB', v), onset=lambda v: struct.pack('!BB', v[0], v[1]))
class MyPayloadMessage(Structure): payload = Payload()
class PascalString16(Structure): length = LengthField(UBInt16()) value = Payload(length)
class NameStructureGreedyAfter(Structure): first = SubstructureField(PascalString16) last = SubstructureField(PascalString16) greedy = Payload()
class NumberOfPortsMessage(MessageStructure): value = Payload() ports = FieldProperty(value, onget=lambda v: None if len(v) == 0 else ord(v[0]), onset=lambda v: chr(v))
class LoopDetectionMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class QOSMessage(MessageStructure): value = Payload() type = FieldProperty(value, onget=lambda v: None if len(v) == 0 else ord(v), onset=lambda v: chr(v))
class BasicGreedy(Structure): a = UBInt8() b = UBInt8() payload = Payload()
class CRCGreedyTail(Structure): payload = Payload() magic = Magic(b'~~') crc = CRCField(UBInt32(), crc32, 0, -1) # all (checksum zeroed)
class UnknownMessage0017(MessageStructure): value = Payload()
class BroadcastFilteringMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x03', onset=lambda v: '\x03' if v else '\x00')
class IGMPHeaderValidationMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class BlockUnknownMulticastsMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class UnknownMessage9400(MessageStructure): value = Payload()
class DriverStation2RobotPacket(Structure): packet_index = UBInt16() control_byte = BitField(8, reset=BitBool(), not_estop=BitBool(), enabled=BitBool(), autonmous=BitBool(), fms_attached=BitBool(), resync=BitBool(), test=BitBool(), fpga_checksum=BitBool()) digital_input = BitField( 8, input_8=BitBool(), input_7=BitBool(), input_6=BitBool(), input_5=BitBool(), input_4=BitBool(), input_3=BitBool(), input_2=BitBool(), input_1=BitBool(), ) team_number = UBInt16() alliance = UBInt8() #ASCII of B or R position = UBInt8() #ASCII of 1,2,3 joystick_1_axis_1 = SBInt8() joystick_1_axis_2 = SBInt8() joystick_1_axis_3 = SBInt8() joystick_1_axis_4 = SBInt8() joystick_1_axis_5 = SBInt8() joystick_1_axis_6 = SBInt8() joystick_1_buttons = BitField(16, button_1=BitBool(), button_2=BitBool(), button_3=BitBool(), button_4=BitBool(), button_5=BitBool(), button_6=BitBool(), button_7=BitBool(), button_8=BitBool(), button_9=BitBool(), button_10=BitBool(), button_11=BitBool(), button_12=BitBool(), button_13=BitBool(), button_14=BitBool(), button_15=BitBool(), button_16=BitBool()) joystick_2_axis_1 = SBInt8() joystick_2_axis_2 = SBInt8() joystick_2_axis_3 = SBInt8() joystick_2_axis_4 = SBInt8() joystick_2_axis_5 = SBInt8() joystick_2_axis_6 = SBInt8() joystick_2_buttons = BitField(16, button_1=BitBool(), button_2=BitBool(), button_3=BitBool(), button_4=BitBool(), button_5=BitBool(), button_6=BitBool(), button_7=BitBool(), button_8=BitBool(), button_9=BitBool(), button_10=BitBool(), button_11=BitBool(), button_12=BitBool(), button_13=BitBool(), button_14=BitBool(), button_15=BitBool(), button_16=BitBool()) joystick_3_axis_1 = SBInt8() joystick_3_axis_2 = SBInt8() joystick_3_axis_3 = SBInt8() joystick_3_axis_4 = SBInt8() joystick_3_axis_5 = SBInt8() joystick_3_axis_6 = SBInt8() joystick_3_buttons = BitField(16, button_1=BitBool(), button_2=BitBool(), button_3=BitBool(), button_4=BitBool(), button_5=BitBool(), button_6=BitBool(), button_7=BitBool(), button_8=BitBool(), button_9=BitBool(), button_10=BitBool(), button_11=BitBool(), button_12=BitBool(), button_13=BitBool(), button_14=BitBool(), button_15=BitBool(), button_16=BitBool()) joystick_4_axis_1 = SBInt8() joystick_4_axis_2 = SBInt8() joystick_4_axis_3 = SBInt8() joystick_4_axis_4 = SBInt8() joystick_4_axis_5 = SBInt8() joystick_4_axis_6 = SBInt8() joystick_4_buttons = BitField(16, button_1=BitBool(), button_2=BitBool(), button_3=BitBool(), button_4=BitBool(), button_5=BitBool(), button_6=BitBool(), button_7=BitBool(), button_8=BitBool(), button_9=BitBool(), button_10=BitBool(), button_11=BitBool(), button_12=BitBool(), button_13=BitBool(), button_14=BitBool(), button_15=BitBool(), button_16=BitBool()) analog_value_1 = UBInt16() analog_value_2 = UBInt16() analog_value_3 = UBInt16() analog_value_4 = UBInt16() crio_checksum = UBInt64() fpga_checksum_1 = UBInt32() fpga_checksum_2 = UBInt32() fpga_checksum_3 = UBInt32() fpga_checksum_4 = UBInt32() driver_station_version = UBInt64() #ASCII string such as '02121300' unknown = Payload(939) crc_checksum = CRCField(UBInt32(), crc32, 0, 1024) @staticmethod def make_packet(index, team_number): """ I Don't like this """ ret = DriverStation2RobotPacket() ret.packet_index = index ret.control_byte.reset = False ret.control_byte.not_estop = True ret.control_byte.enabled = False ret.control_byte.autonmous = False ret.control_byte.fms_attached = False ret.control_byte.resync = False ret.control_byte.fpga_checksum = False ret.digital_input.input_8 = False ret.digital_input.input_7 = False ret.digital_input.input_6 = False ret.digital_input.input_5 = False ret.digital_input.input_4 = False ret.digital_input.input_3 = False ret.digital_input.input_2 = False ret.digital_input.input_1 = False ret.team_number = team_number ret.alliance = ord('R') ret.position = ord('1') ret.joystick_1_axis_1 = 0 ret.joystick_1_axis_2 = 0 ret.joystick_1_axis_3 = 0 ret.joystick_1_axis_4 = 0 ret.joystick_1_axis_5 = 0 ret.joystick_1_axis_6 = 0 ret.joystick_1_buttons.button_1 = False ret.joystick_1_buttons.button_2 = False ret.joystick_1_buttons.button_3 = False ret.joystick_1_buttons.button_4 = False ret.joystick_1_buttons.button_5 = False ret.joystick_1_buttons.button_6 = False ret.joystick_1_buttons.button_7 = False ret.joystick_1_buttons.button_8 = False ret.joystick_1_buttons.button_9 = False ret.joystick_1_buttons.button_10 = False ret.joystick_1_buttons.button_11 = False ret.joystick_1_buttons.button_12 = False ret.joystick_1_buttons.button_13 = False ret.joystick_1_buttons.button_14 = False ret.joystick_1_buttons.button_15 = False ret.joystick_1_buttons.button_16 = False ret.joystick_2_axis_1 = 0 ret.joystick_2_axis_2 = 0 ret.joystick_2_axis_3 = 0 ret.joystick_2_axis_4 = 0 ret.joystick_2_axis_5 = 0 ret.joystick_2_axis_6 = 0 ret.joystick_2_buttons.button_1 = False ret.joystick_2_buttons.button_2 = False ret.joystick_2_buttons.button_3 = False ret.joystick_2_buttons.button_4 = False ret.joystick_2_buttons.button_5 = False ret.joystick_2_buttons.button_6 = False ret.joystick_2_buttons.button_7 = False ret.joystick_2_buttons.button_8 = False ret.joystick_2_buttons.button_9 = False ret.joystick_2_buttons.button_10 = False ret.joystick_2_buttons.button_11 = False ret.joystick_2_buttons.button_12 = False ret.joystick_2_buttons.button_13 = False ret.joystick_2_buttons.button_14 = False ret.joystick_2_buttons.button_15 = False ret.joystick_2_buttons.button_16 = False ret.joystick_3_axis_1 = 0 ret.joystick_3_axis_2 = 0 ret.joystick_3_axis_3 = 0 ret.joystick_3_axis_4 = 0 ret.joystick_3_axis_5 = 0 ret.joystick_3_axis_6 = 0 ret.joystick_3_buttons.button_1 = False ret.joystick_3_buttons.button_2 = False ret.joystick_3_buttons.button_3 = False ret.joystick_3_buttons.button_4 = False ret.joystick_3_buttons.button_5 = False ret.joystick_3_buttons.button_6 = False ret.joystick_3_buttons.button_7 = False ret.joystick_3_buttons.button_8 = False ret.joystick_3_buttons.button_9 = False ret.joystick_3_buttons.button_10 = False ret.joystick_3_buttons.button_11 = False ret.joystick_3_buttons.button_12 = False ret.joystick_3_buttons.button_13 = False ret.joystick_3_buttons.button_14 = False ret.joystick_3_buttons.button_15 = False ret.joystick_3_buttons.button_16 = False ret.joystick_4_axis_1 = 0 ret.joystick_4_axis_2 = 0 ret.joystick_4_axis_3 = 0 ret.joystick_4_axis_4 = 0 ret.joystick_4_axis_5 = 0 ret.joystick_4_axis_6 = 0 ret.joystick_4_buttons.button_1 = False ret.joystick_4_buttons.button_2 = False ret.joystick_4_buttons.button_3 = False ret.joystick_4_buttons.button_4 = False ret.joystick_4_buttons.button_5 = False ret.joystick_4_buttons.button_6 = False ret.joystick_4_buttons.button_7 = False ret.joystick_4_buttons.button_8 = False ret.joystick_4_buttons.button_9 = False ret.joystick_4_buttons.button_10 = False ret.joystick_4_buttons.button_11 = False ret.joystick_4_buttons.button_12 = False ret.joystick_4_buttons.button_13 = False ret.joystick_4_buttons.button_14 = False ret.joystick_4_buttons.button_15 = False ret.joystick_4_buttons.button_16 = False ret.analog_value_1 = 0 ret.analog_value_2 = 0 ret.analog_value_3 = 0 ret.analog_value_4 = 0 ret.crio_checksum = 0 ret.fpga_checksum_1 = 0 ret.fpga_checksum_2 = 0 ret.fpga_checksum_3 = 0 ret.fpga_checksum_4 = 0 # This was reported by the FRC 2016 DS in 2014 mode ret.driver_station_version = 3546356223709687856 payload = "" for i in xrange(0, 940): payload += "\x00" ret.unknown = payload return ret
class EchoProtocolFrame(Structure): frame_type = UBInt8() payload_length = LengthField(UBInt16()) payload = Payload(payload_length)
class Zero(Structure): body = Payload()