construct.Const(b'\x03\x21'), 'record_id' / construct.Int16ul, ) _MEAL_FLAG = { common.Meal.NONE: 0x00, common.Meal.BEFORE: 0x01, common.Meal.AFTER: 0x02, } _READING_RESPONSE = construct.Struct( _COMMAND_SUCCESS, 'timestamp' / lifescan_binary_protocol.VERIO_TIMESTAMP, 'value' / construct.Int16ul, 'control_test' / construct.Flag, 'meal' / construct.Mapping(construct.Byte, _MEAL_FLAG), construct.Padding(2), # unknown ) class Device(serial.SerialDevice): BAUDRATE = 38400 DEFAULT_CABLE_ID = '10c4:85a7' # Specific ID for embedded cp210x TIMEOUT = 0.5 def __init__(self, device): super(Device, self).__init__(device) self.buffered_reader_ = construct.Rebuffered(_PACKET, tailcutoff=1024) def connect(self): pass
In = 0xa5 Out = 0xa3 def byte_checksum(data): return functools.reduce(operator.add, data) & 0xFF _PACKET = construct.Struct( 'data' / construct.RawCopy( construct.Struct( construct.Const(b'\x51'), 'command' / construct.Byte, 'message' / construct.Bytes(4), 'direction' / construct.Mapping(construct.Byte, {e: e.value for e in Direction}), ), ), 'checksum' / construct.Checksum(construct.Byte, byte_checksum, construct.this.data.data), ) _EMPTY_MESSAGE = 0 _CONNECT_REQUEST = 0x22 _VALID_CONNECT_RESPONSE = {0x22, 0x24, 0x54} _GET_DATETIME = 0x23 _SET_DATETIME = 0x33 _GET_MODEL = 0x24
command_prefix_construct = construct.Const(command_prefix, construct.Byte) return construct.Struct( 'data' / construct.RawCopy( construct.Struct( construct.Const(b'\x02'), # stx 'length' / construct.Rebuild( construct.Byte, lambda this: len(this.message) + 7), 'link_control' / link_control_construct, 'command_prefix' / command_prefix_construct, 'message' / construct.Bytes(lambda this: this.length - 7), construct.Const(b'\x03'), # etx ), ), 'checksum' / construct.Checksum(construct.Int16ul, lifescan.crc_ccitt, construct.this.data.data), ) COMMAND_SUCCESS = construct.Const(b'\x06') VERIO_TIMESTAMP = construct_extras.Timestamp( construct.Int32ul, epoch=946684800) # 2000-01-01 (not 2010) _GLUCOSE_UNIT_MAPPING_TABLE = { common.Unit.MG_DL: 0x00, common.Unit.MMOL_L: 0x01, } GLUCOSE_UNIT = construct.Mapping(construct.Byte, _GLUCOSE_UNIT_MAPPING_TABLE)
def __init__(self, websocket, mode): self._websocket = websocket self._mode = mode self._inbound_packet = construct.Struct( "type" / construct.Enum( construct.Int8ul, setup=0, killed=1, kill=2, remove=3, sync=4, club_collision=5, wall_collision=6, set_leaderboard=7, set_target_dim=8 ), "payload" / construct.Switch( construct.this.type, { "setup": construct.Struct( "server_version" / construct.CString(encoding="utf8"), construct.Check(lambda ctx: ctx.server_version == CLIENT_VERSION), "syncer_value" / construct.Int32ul, "game_mode" / construct.Mapping( construct.Int8ul, { 0: Mode.ffa, 1: Mode.tdm }, dict() # This packet is only received ), "setup_value" / construct.Int32ul ), "killed": construct.Pass, "kill": construct.Struct("stamp" / construct.Int32ul), "remove": construct.Struct( "enqueue_param" / construct.Int32ul, # TODO: Figure out purpose "player_id" / construct.Int32ul ), "sync": construct.Struct( "timestamp" / construct.Int32ul, "remove_count" / construct.Int32ul, "removal_array" / construct.Array( construct.this.remove_count, construct.Int32ul ), "sync_count" / construct.Int32ul, "sync_array" / construct.Array( construct.this.sync_count, construct.Struct( "player_id" / construct.Int32ul, "player_state" / physical_state, "mace_state" / physical_state, "mace_radius" / construct.Float32l ) ) ), "club_collision": construct.Struct( "enqueue_param" / construct.Int32ul, # TODO: Figure out purpose "p" / vector2d, # TODO: Figure out purpose "i" / construct.Float32l, # TODO: Figure out purpose "first_id" / construct.Int32ul, "first_state" / physical_state, "second_id" / construct.Int32ul, "second_state" / physical_state ), "wall_collision": construct.Struct( "enqueue_param" / construct.Int32ul, # TODO: Figure out purpose "p" / vector2d, # TODO: Figure out purpose "i" / construct.Float32l, # TODO: Figure out purpose "player_id" / construct.Int32ul, "player_state" / physical_state, "mace_radius" / construct.Float32l ), "set_leaderboard": construct.Struct( "player_count" / construct.Int32ul, "total" / construct.Int32ul, # TODO: Figure out purpose, construct.IfThenElse( lambda ctx: self._mode == Mode.ffa, construct.Struct( # FFA "count" / construct.Int8ul, "first_entry_id" / construct.Int32ul, "leaderboard" / construct.Array( construct.this.count, construct.Struct( "name" / construct.CString(encoding="utf8"), "score" / construct.Int32ul, ) ), "king_name" / construct.CString(encoding="utf8"), "king_score" / construct.Int32ul, "place" / construct.Int32ul, "score" / construct.Int32ul ), construct.Array( 3, construct.Struct( "id" / construct.Int8ul, "score" / construct.Int32ul, "count" / construct.Int32ul ) ) ) ), "set_target_dim": construct.Struct("target_dim" / vector2d) } ) ) self._outbound_packet = construct.Struct( "type" / construct.Enum( construct.Int8ul, play=0, direction=1, move_up=2, move_down=3, move_left=4, move_right=5, stop_move_up=6, stop_move_down=7, stop_move_left=8, stop_move_right=9 ), "payload" / construct.Switch( construct.this.type, { "play": construct.Pass # TODO: Implement the rest of the packets } ) )