# Generated by the protocol compiler version 1.3.8
# DO NOT EDIT THIS FILE DIRECTLY!

__doc__ = "Message serializer for the DPM protocol."

from itertools import chain, islice

__all__ = [
    "ProtocolError",
    "unmarshal_request",
    "unmarshal_reply",
    "RawSetting_struct",
    "ScaledSetting_struct",
    "TextSetting_struct",
    "SettingStatus_struct",
    "ServiceDiscovery_request",
    "OpenList_request",
    "AddToList_request",
    "Authenticate_request",
    "EnableSettings_request",
    "RemoveFromList_request",
    "StartList_request",
    "ClearList_request",
    "StopList_request",
    "ApplySettings_request",
    "ServiceDiscovery_reply",
    "OpenList_reply",
    "AddToList_reply",
    "RemoveFromList_reply",
    "StartList_reply",
    "ListStatus_reply",
    "Status_reply",
    "DeviceInfo_reply",
    "Scalar_reply",
    "ScalarArray_reply",
    "Raw_reply",
    "Text_reply",
    "TextArray_reply",
    "AnalogAlarm_reply",
    "DigitalAlarm_reply",
    "BasicStatus_reply",
    "TimedScalarArray_reply",
    "ApplySettings_reply",
    "Authenticate_reply",
]

import struct


class ProtocolError(Exception):
    """Exception class that gets raised when there's a problem marshaling
    or unmarshaling a message from the DPM protocol."""

    def __init__(self, reason):
        self.reason = reason

    def __str__(self):
        return repr(self.reason)


# -- Internal marshalling routines --


def emitRawInt(tag, val):
    def emitEach(buf, n):
        curr = (val >> (n * 8)) & 0xFF
        next = val >> ((n + 1) * 8)
        if (next == 0 and (curr & 0x80) != 0x80) or (next == -1 and (curr & 0x80) == 0x80):
            buf.append(tag + n + 1)
        else:
            emitEach(buf, n + 1)
        buf.append(curr)

    tmp = bytearray()
    emitEach(tmp, 0)
    return tmp


def marshal_bool(val):
    yield (0x71 if val else 0x70)


def marshal_int16(val):
    if isinstance(val, int):
        if val < 32768 and val > -32769:
            return emitRawInt(0x10, val)
        else:
            raise ProtocolError("value out of range for int16")
    else:
        raise ProtocolError("expected integer type")


def marshal_int32(val):
    if isinstance(val, int):
        if int(-2147483648) <= val <= int(2147483647):
            return emitRawInt(0x10, val)
        else:
            raise ProtocolError("value out of range for int32")
    else:
        raise ProtocolError("expected integer type")


def marshal_int64(val):
    if isinstance(val, int):
        if int(-9223372036854775808) <= val <= int(9223372036854775807):
            return emitRawInt(0x10, val)
        else:
            raise ProtocolError("value out of range for int64")
    else:
        raise ProtocolError("expected integer type")


def marshal_double(val):
    return chain(b"\x28", (ii for ii in struct.pack(">d", float(val))))


def marshal_string(val):
    if isinstance(val, str):
        return chain(emitRawInt(0x40, len(val)), (ord(ii) for ii in val))
    else:
        raise ProtocolError("expected string type")


def marshal_binary(val):
    if isinstance(val, (bytearray, bytes)):
        return chain(emitRawInt(0x30, len(val)), val)
    else:
        raise ProtocolError("expected bytearray or bytes type")


def marshal_array(fn, val):
    if isinstance(val, list):
        return chain(emitRawInt(0x50, len(val)), chain.from_iterable((fn(v) for v in val)))
    else:
        raise ProtocolError("expected list type")


class RawSetting_struct:
    def __init__(self):
        self.ref_id = int(0)
        self.data = b""

    def __eq__(self, other):
        return self.ref_id == other.ref_id and self.data == other.data

    def __ne__(self, other):
        return not self.__eq__(other)


def marshal_RawSetting_struct(val):
    return chain(b"\x51\x04\x12\x1e\xab", marshal_int64(val.ref_id), b"\x12\x7f\x38", marshal_binary(val.data))


class ScaledSetting_struct:
    def __init__(self):
        self.ref_id = int(0)
        self.data = []

    def __eq__(self, other):
        return self.ref_id == other.ref_id and self.data == other.data

    def __ne__(self, other):
        return not self.__eq__(other)


def marshal_ScaledSetting_struct(val):
    return chain(
        b"\x51\x04\x12\x1e\xab", marshal_int64(val.ref_id), b"\x12\x7f\x38", marshal_array(marshal_double, val.data)
    )


class TextSetting_struct:
    def __init__(self):
        self.ref_id = int(0)
        self.data = []

    def __eq__(self, other):
        return self.ref_id == other.ref_id and self.data == other.data

    def __ne__(self, other):
        return not self.__eq__(other)


def marshal_TextSetting_struct(val):
    return chain(
        b"\x51\x04\x12\x1e\xab", marshal_int64(val.ref_id), b"\x12\x7f\x38", marshal_array(marshal_string, val.data)
    )


class SettingStatus_struct:
    def __init__(self):
        self.ref_id = int(0)
        self.status = int(0)

    def __eq__(self, other):
        return self.ref_id == other.ref_id and self.status == other.status

    def __ne__(self, other):
        return not self.__eq__(other)


def marshal_SettingStatus_struct(val):
    return chain(b"\x51\x04\x12\x1e\xab", marshal_int64(val.ref_id), b"\x12\x44\x54", marshal_int16(val.status))


class ServiceDiscovery_request:
    def __eq__(self, other):
        return True

    def __ne__(self, other):
        return False

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ServiceDiscovery_request."""
        return b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xdf\xda\x51\x00"


class OpenList_request:
    def __eq__(self, other):
        return (not hasattr(self, "location") and not hasattr(other, "location")) or (
            hasattr(self, "location") and hasattr(other, "location") and self.location == other.location
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of OpenList_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x08\x1c",
            emitRawInt(0x50, 0 + (2 if hasattr(self, "location") else 0)),
            chain(b"\x12\x9d\xe0", marshal_string(self.location)) if hasattr(self, "location") else b"",
        )


class AddToList_request:
    def __init__(self):
        self.list_id = int(0)
        self.ref_id = int(0)
        self.drf_request = ""

    def __eq__(self, other):
        return self.list_id == other.list_id and self.ref_id == other.ref_id and self.drf_request == other.drf_request

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of AddToList_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x11\x61\x51\x06\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\x63\x24",
            marshal_string(self.drf_request),
        )


class Authenticate_request:
    def __init__(self):
        self.list_id = int(0)
        self.token = b""

    def __eq__(self, other):
        return self.list_id == other.list_id and self.token == other.token

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of Authenticate_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xc4\x53\x51\x04\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x8d\xa1",
            marshal_binary(self.token),
        )


class EnableSettings_request:
    def __init__(self):
        self.list_id = int(0)
        self.MIC = b""
        self.message = b""

    def __eq__(self, other):
        return self.list_id == other.list_id and self.MIC == other.MIC and self.message == other.message

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of EnableSettings_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x53\x62\x51\x06\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x1f\xcd",
            marshal_binary(self.MIC),
            b"\x12\x31\x02",
            marshal_binary(self.message),
        )


class RemoveFromList_request:
    def __init__(self):
        self.list_id = int(0)
        self.ref_id = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id and self.ref_id == other.ref_id

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of RemoveFromList_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xf1\x21\x51\x04\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x1e\xab",
            marshal_int64(self.ref_id),
        )


class StartList_request:
    def __init__(self):
        self.list_id = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id and (
            (not hasattr(self, "model") and not hasattr(other, "model"))
            or (hasattr(self, "model") and hasattr(other, "model") and self.model == other.model)
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of StartList_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xdd\xb5",
            emitRawInt(0x50, 2 + (2 if hasattr(self, "model") else 0)),
            b"\x12\xe8\x20",
            marshal_int32(self.list_id),
            chain(b"\x12\x5e\x63", marshal_string(self.model)) if hasattr(self, "model") else b"",
        )


class ClearList_request:
    def __init__(self):
        self.list_id = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ClearList_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xc3\x09\x51\x02\x12\xe8\x20", marshal_int32(self.list_id)
        )


class StopList_request:
    def __init__(self):
        self.list_id = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of StopList_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x53\xa9\x51\x02\x12\xe8\x20", marshal_int32(self.list_id)
        )


class ApplySettings_request:
    def __init__(self):
        self.user_name = ""
        self.list_id = int(0)

    def __eq__(self, other):
        return (
            self.user_name == other.user_name
            and self.list_id == other.list_id
            and (
                (not hasattr(self, "raw_array") and not hasattr(other, "raw_array"))
                or (hasattr(self, "raw_array") and hasattr(other, "raw_array") and self.raw_array == other.raw_array)
            )
            and (
                (not hasattr(self, "scaled_array") and not hasattr(other, "scaled_array"))
                or (
                    hasattr(self, "scaled_array")
                    and hasattr(other, "scaled_array")
                    and self.scaled_array == other.scaled_array
                )
            )
            and (
                (not hasattr(self, "text_array") and not hasattr(other, "text_array"))
                or (
                    hasattr(self, "text_array") and hasattr(other, "text_array") and self.text_array == other.text_array
                )
            )
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ApplySettings_request."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xc6\x78",
            emitRawInt(
                0x50,
                4
                + (2 if hasattr(self, "raw_array") else 0)
                + (2 if hasattr(self, "scaled_array") else 0)
                + (2 if hasattr(self, "text_array") else 0),
            ),
            b"\x12\x56\x48",
            marshal_string(self.user_name),
            b"\x12\xe8\x20",
            marshal_int32(self.list_id),
            chain(b"\x12\x03\x01", marshal_array(marshal_RawSetting_struct, self.raw_array))
            if hasattr(self, "raw_array")
            else b"",
            chain(b"\x12\x63\xce", marshal_array(marshal_ScaledSetting_struct, self.scaled_array))
            if hasattr(self, "scaled_array")
            else b"",
            chain(b"\x12\xce\x8f", marshal_array(marshal_TextSetting_struct, self.text_array))
            if hasattr(self, "text_array")
            else b"",
        )


class ServiceDiscovery_reply:
    def __init__(self):
        self.load = int(0)
        self.serviceLocation = ""

    def __eq__(self, other):
        return self.load == other.load and self.serviceLocation == other.serviceLocation

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ServiceDiscovery_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xcd\x7e\x51\x04\x12\x1e\xb3",
            marshal_int16(self.load),
            b"\x12\x11\xaf",
            marshal_string(self.serviceLocation),
        )


class OpenList_reply:
    def __init__(self):
        self.list_id = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of OpenList_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x34\x9e\x51\x02\x12\xe8\x20", marshal_int32(self.list_id)
        )


class AddToList_reply:
    def __init__(self):
        self.list_id = int(0)
        self.ref_id = int(0)
        self.status = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id and self.ref_id == other.ref_id and self.status == other.status

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of AddToList_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x8b\xac\x51\x06\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\x44\x54",
            marshal_int16(self.status),
        )


class RemoveFromList_reply:
    def __init__(self):
        self.list_id = int(0)
        self.ref_id = int(0)
        self.status = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id and self.ref_id == other.ref_id and self.status == other.status

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of RemoveFromList_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xf4\x1a\x51\x06\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\x44\x54",
            marshal_int16(self.status),
        )


class StartList_reply:
    def __init__(self):
        self.list_id = int(0)
        self.status = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id and self.status == other.status

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of StartList_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x96\x00\x51\x04\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x44\x54",
            marshal_int16(self.status),
        )


class ListStatus_reply:
    def __init__(self):
        self.list_id = int(0)
        self.status = int(0)

    def __eq__(self, other):
        return self.list_id == other.list_id and self.status == other.status

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ListStatus_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x73\x70\x51\x04\x12\xe8\x20",
            marshal_int32(self.list_id),
            b"\x12\x44\x54",
            marshal_int16(self.status),
        )


class Status_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of Status_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xd6\xad\x51\x08\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
        )


class DeviceInfo_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.di = int(0)
        self.name = ""
        self.description = ""

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.di == other.di
            and self.name == other.name
            and self.description == other.description
            and (
                (not hasattr(self, "units") and not hasattr(other, "units"))
                or (hasattr(self, "units") and hasattr(other, "units") and self.units == other.units)
            )
            and (
                (not hasattr(self, "format_hint") and not hasattr(other, "format_hint"))
                or (
                    hasattr(self, "format_hint")
                    and hasattr(other, "format_hint")
                    and self.format_hint == other.format_hint
                )
            )
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of DeviceInfo_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x6f\xed",
            emitRawInt(0x50, 8 + (2 if hasattr(self, "units") else 0) + (2 if hasattr(self, "format_hint") else 0)),
            b"\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\x82\xdd",
            marshal_int32(self.di),
            b"\x12\x93\x1c",
            marshal_string(self.name),
            b"\x12\xf9\x2c",
            marshal_string(self.description),
            chain(b"\x12\x3d\xfb", marshal_string(self.units)) if hasattr(self, "units") else b"",
            chain(b"\x12\x7e\xc2", marshal_int16(self.format_hint)) if hasattr(self, "format_hint") else b"",
        )


class Scalar_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)
        self.data = float(0.0)

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
            and self.data == other.data
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of Scalar_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xd6\xab\x51\x0a\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
            b"\x12\x7f\x38",
            marshal_double(self.data),
        )


class ScalarArray_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)
        self.data = []

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
            and self.data == other.data
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ScalarArray_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x59\xfc\x51\x0a\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
            b"\x12\x7f\x38",
            marshal_array(marshal_double, self.data),
        )


class Raw_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)
        self.data = b""

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
            and self.data == other.data
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of Raw_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xcf\x5e\x51\x0a\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
            b"\x12\x7f\x38",
            marshal_binary(self.data),
        )


class Text_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)
        self.data = ""

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
            and self.data == other.data
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of Text_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x8f\x32\x51\x0a\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
            b"\x12\x7f\x38",
            marshal_string(self.data),
        )


class TextArray_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)
        self.data = []

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
            and self.data == other.data
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of TextArray_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x32\x7b\x51\x0a\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
            b"\x12\x7f\x38",
            marshal_array(marshal_string, self.data),
        )


class AnalogAlarm_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.minimum = float(0.0)
        self.maximum = float(0.0)
        self.alarm_enable = bool(False)
        self.alarm_status = bool(False)
        self.abort = bool(False)
        self.abort_inhibit = bool(False)
        self.tries_needed = int(0)
        self.tries_now = int(0)

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.minimum == other.minimum
            and self.maximum == other.maximum
            and self.alarm_enable == other.alarm_enable
            and self.alarm_status == other.alarm_status
            and self.abort == other.abort
            and self.abort_inhibit == other.abort_inhibit
            and self.tries_needed == other.tries_needed
            and self.tries_now == other.tries_now
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of AnalogAlarm_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x75\x88\x51\x16\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x23\x4c",
            marshal_double(self.minimum),
            b"\x12\xe2\x88",
            marshal_double(self.maximum),
            b"\x12\x8f\x20",
            marshal_bool(self.alarm_enable),
            b"\x12\x60\x36",
            marshal_bool(self.alarm_status),
            b"\x12\x4a\x1c",
            marshal_bool(self.abort),
            b"\x12\x81\x72",
            marshal_bool(self.abort_inhibit),
            b"\x12\x01\x32",
            marshal_int32(self.tries_needed),
            b"\x12\x2b\x3e",
            marshal_int32(self.tries_now),
        )


class DigitalAlarm_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.nominal = int(0)
        self.mask = int(0)
        self.alarm_enable = bool(False)
        self.alarm_status = bool(False)
        self.abort = bool(False)
        self.abort_inhibit = bool(False)
        self.tries_needed = int(0)
        self.tries_now = int(0)

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.nominal == other.nominal
            and self.mask == other.mask
            and self.alarm_enable == other.alarm_enable
            and self.alarm_status == other.alarm_status
            and self.abort == other.abort
            and self.abort_inhibit == other.abort_inhibit
            and self.tries_needed == other.tries_needed
            and self.tries_now == other.tries_now
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of DigitalAlarm_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xc7\x9b\x51\x16\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x4f\x1d",
            marshal_int32(self.nominal),
            b"\x12\x11\xeb",
            marshal_int32(self.mask),
            b"\x12\x8f\x20",
            marshal_bool(self.alarm_enable),
            b"\x12\x60\x36",
            marshal_bool(self.alarm_status),
            b"\x12\x4a\x1c",
            marshal_bool(self.abort),
            b"\x12\x81\x72",
            marshal_bool(self.abort_inhibit),
            b"\x12\x01\x32",
            marshal_int32(self.tries_needed),
            b"\x12\x2b\x3e",
            marshal_int32(self.tries_now),
        )


class BasicStatus_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and (
                (not hasattr(self, "on") and not hasattr(other, "on"))
                or (hasattr(self, "on") and hasattr(other, "on") and self.on == other.on)
            )
            and (
                (not hasattr(self, "ready") and not hasattr(other, "ready"))
                or (hasattr(self, "ready") and hasattr(other, "ready") and self.ready == other.ready)
            )
            and (
                (not hasattr(self, "remote") and not hasattr(other, "remote"))
                or (hasattr(self, "remote") and hasattr(other, "remote") and self.remote == other.remote)
            )
            and (
                (not hasattr(self, "positive") and not hasattr(other, "positive"))
                or (hasattr(self, "positive") and hasattr(other, "positive") and self.positive == other.positive)
            )
            and (
                (not hasattr(self, "ramp") and not hasattr(other, "ramp"))
                or (hasattr(self, "ramp") and hasattr(other, "ramp") and self.ramp == other.ramp)
            )
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of BasicStatus_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\xf2\xf9",
            emitRawInt(
                0x50,
                6
                + (2 if hasattr(self, "on") else 0)
                + (2 if hasattr(self, "ready") else 0)
                + (2 if hasattr(self, "remote") else 0)
                + (2 if hasattr(self, "positive") else 0)
                + (2 if hasattr(self, "ramp") else 0),
            ),
            b"\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            chain(b"\x12\x5c\x01", marshal_bool(self.on)) if hasattr(self, "on") else b"",
            chain(b"\x12\xab\x23", marshal_bool(self.ready)) if hasattr(self, "ready") else b"",
            chain(b"\x12\xe4\x86", marshal_bool(self.remote)) if hasattr(self, "remote") else b"",
            chain(b"\x12\x27\x16", marshal_bool(self.positive)) if hasattr(self, "positive") else b"",
            chain(b"\x12\x92\x38", marshal_bool(self.ramp)) if hasattr(self, "ramp") else b"",
        )


class TimedScalarArray_reply:
    def __init__(self):
        self.ref_id = int(0)
        self.timestamp = int(0)
        self.cycle = int(0)
        self.status = int(0)
        self.data = []
        self.micros = []

    def __eq__(self, other):
        return (
            self.ref_id == other.ref_id
            and self.timestamp == other.timestamp
            and self.cycle == other.cycle
            and self.status == other.status
            and self.data == other.data
            and self.micros == other.micros
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of TimedScalarArray_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x7a\x32\x51\x0c\x12\x1e\xab",
            marshal_int64(self.ref_id),
            b"\x12\xd5\x5b",
            marshal_int64(self.timestamp),
            b"\x12\x07\x40",
            marshal_int64(self.cycle),
            b"\x12\x44\x54",
            marshal_int16(self.status),
            b"\x12\x7f\x38",
            marshal_array(marshal_double, self.data),
            b"\x12\x34\x2d",
            marshal_array(marshal_int64, self.micros),
        )


class ApplySettings_reply:
    def __init__(self):
        self.status = []

    def __eq__(self, other):
        return self.status == other.status

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of ApplySettings_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x43\xc0\x51\x02\x12\x44\x54",
            marshal_array(marshal_SettingStatus_struct, self.status),
        )


class Authenticate_reply:
    def __init__(self):
        self.token = None
        self.serviceName = ""

    def __eq__(self, other):
        return (
            (not hasattr(self, "serviceName") and not hasattr(other, "serviceName"))
            or (
                hasattr(self, "serviceName") and hasattr(other, "serviceName") and self.serviceName == other.serviceName
            )
        ) and (
            (not hasattr(self, "token") and not hasattr(other, "token"))
            or (hasattr(self, "token") and hasattr(other, "token") and self.token == other.token)
        )

    def __ne__(self, other):
        return not self.__eq__(other)

    def marshal(self):
        """Returns a generator that emits a character stream representing
        the marshaled contents of Authenticate_reply."""
        return chain(
            b"SDD\x02\x51\x03\x14\xb1\x3a\x70\x3a\x12\x1c\x76",
            emitRawInt(0x50, 0 + (2 if hasattr(self, "serviceName") else 0) + (2 if hasattr(self, "token") else 0)),
            chain(b"\x12\x63\x38", marshal_string(self.serviceName)) if hasattr(self, "serviceName") else b"",
            chain(b"\x12\x8d\xa1", marshal_binary(self.token)) if hasattr(self, "token") else b"",
        )


def marshal_request(val):
    return val.marshal()


def marshal_reply(val):
    return val.marshal()


# -- Internal unmarshalling routines --


def consumeRawInt(it, tag):
    itTag = it.__next__()
    itLen = itTag & 0xF
    if (itTag & 0xF0) == tag and itLen > 0 and itLen <= 8:
        it = islice(it, itLen)
        retVal = it.__next__()
        if (0x80 & retVal) != 0:
            retVal |= -256
        for xx in it:
            retVal = (retVal << 8) + xx
        return int(retVal)
    else:
        raise ProtocolError("bad tag or length")


def unmarshal_bool(ii):
    val = ii.__next__()
    if val == 112:
        return False
    elif val == 113:
        return True
    else:
        raise ProtocolError("expected boolean value")


def unmarshal_int16(ii):
    val = consumeRawInt(ii, 0x10)
    if val >= -0x8000 and val < 0x8000:
        return int(val)
    else:
        raise ProtocolError("value out of range for int16")


def unmarshal_int32(ii):
    val = consumeRawInt(ii, 0x10)
    if int(-2147483648) <= val <= int(2147483647):
        return int(val)
    else:
        raise ProtocolError("value out of range for int32")


def unmarshal_int64(ii):
    val = consumeRawInt(ii, 0x10)
    if int(-9223372036854775808) <= val <= int(9223372036854775807):
        return val
    else:
        raise ProtocolError("value out of range for int64")


_unpack_float = struct.Struct(">xd").unpack


def unmarshal_double(it):
    raw = bytes(islice(it, 9))
    if raw[0] == 40:
        (v,) = _unpack_float(raw)
        return v
    else:
        raise ProtocolError("expected tag for double")


def unmarshal_string(ii):
    return bytes(islice(ii, consumeRawInt(ii, 0x40))).decode("utf-8")


def unmarshal_binary(ii):
    return bytes(islice(ii, consumeRawInt(ii, 0x30)))


def unmarshal_array(ii, fn):
    return [fn(ii) for x in range(consumeRawInt(ii, 0x50))]


def unmarshal_header(ii):
    if (
        ii.__next__() != 83
        or ii.__next__() != 68
        or ii.__next__() != 68
        or ii.__next__() != 2
        or consumeRawInt(ii, 0x50) != 3
    ):
        raise ProtocolError("invalid header")
    elif consumeRawInt(ii, 0x10) != -1321570246:
        raise ProtocolError("incorrect protocol specified")


def unmarshal_RawSetting_struct(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = RawSetting_struct()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 32568:
                tmp.data = unmarshal_binary(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ScaledSetting_struct(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ScaledSetting_struct()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 32568:
                tmp.data = unmarshal_array(ii, unmarshal_double)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_TextSetting_struct(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = TextSetting_struct()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 32568:
                tmp.data = unmarshal_array(ii, unmarshal_string)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_SettingStatus_struct(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = SettingStatus_struct()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ServiceDiscovery_request(ii):
    if consumeRawInt(ii, 0x50) != 0:
        raise ProtocolError("incorrect number of fields")
    else:
        return ServiceDiscovery_request()


def unmarshal_OpenList_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if (nFlds % 2) != 0 or nFlds < 0 or nFlds > 2:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = OpenList_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -25120:
                tmp.location = unmarshal_string(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_AddToList_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 6:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = AddToList_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 25380:
                tmp.drf_request = unmarshal_string(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_Authenticate_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = Authenticate_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == -29279:
                tmp.token = unmarshal_binary(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_EnableSettings_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 6:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = EnableSettings_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 8141:
                tmp.MIC = unmarshal_binary(ii)
            elif fld == 12546:
                tmp.message = unmarshal_binary(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_RemoveFromList_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = RemoveFromList_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_StartList_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if (nFlds % 2) != 0 or nFlds < 2 or nFlds > 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = StartList_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 24163:
                tmp.model = unmarshal_string(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ClearList_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 2:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ClearList_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_StopList_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 2:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = StopList_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ApplySettings_request(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if (nFlds % 2) != 0 or nFlds < 4 or nFlds > 10:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ApplySettings_request()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 22088:
                tmp.user_name = unmarshal_string(ii)
            elif fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 769:
                tmp.raw_array = unmarshal_array(ii, unmarshal_RawSetting_struct)
            elif fld == 25550:
                tmp.scaled_array = unmarshal_array(ii, unmarshal_ScaledSetting_struct)
            elif fld == -12657:
                tmp.text_array = unmarshal_array(ii, unmarshal_TextSetting_struct)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ServiceDiscovery_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ServiceDiscovery_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7859:
                tmp.load = unmarshal_int16(ii)
            elif fld == 4527:
                tmp.serviceLocation = unmarshal_string(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_OpenList_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 2:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = OpenList_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_AddToList_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 6:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = AddToList_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_RemoveFromList_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 6:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = RemoveFromList_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_StartList_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = StartList_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ListStatus_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ListStatus_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == -6112:
                tmp.list_id = unmarshal_int32(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_Status_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 8:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = Status_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_DeviceInfo_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if (nFlds % 2) != 0 or nFlds < 8 or nFlds > 12:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = DeviceInfo_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -32035:
                tmp.di = unmarshal_int32(ii)
            elif fld == -27876:
                tmp.name = unmarshal_string(ii)
            elif fld == -1748:
                tmp.description = unmarshal_string(ii)
            elif fld == 15867:
                tmp.units = unmarshal_string(ii)
            elif fld == 32450:
                tmp.format_hint = unmarshal_int16(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_Scalar_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 10:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = Scalar_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            elif fld == 32568:
                tmp.data = unmarshal_double(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ScalarArray_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 10:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ScalarArray_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            elif fld == 32568:
                tmp.data = unmarshal_array(ii, unmarshal_double)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_Raw_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 10:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = Raw_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            elif fld == 32568:
                tmp.data = unmarshal_binary(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_Text_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 10:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = Text_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            elif fld == 32568:
                tmp.data = unmarshal_string(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_TextArray_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 10:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = TextArray_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            elif fld == 32568:
                tmp.data = unmarshal_array(ii, unmarshal_string)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_AnalogAlarm_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 22:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = AnalogAlarm_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 9036:
                tmp.minimum = unmarshal_double(ii)
            elif fld == -7544:
                tmp.maximum = unmarshal_double(ii)
            elif fld == -28896:
                tmp.alarm_enable = unmarshal_bool(ii)
            elif fld == 24630:
                tmp.alarm_status = unmarshal_bool(ii)
            elif fld == 18972:
                tmp.abort = unmarshal_bool(ii)
            elif fld == -32398:
                tmp.abort_inhibit = unmarshal_bool(ii)
            elif fld == 306:
                tmp.tries_needed = unmarshal_int32(ii)
            elif fld == 11070:
                tmp.tries_now = unmarshal_int32(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_DigitalAlarm_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 22:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = DigitalAlarm_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 20253:
                tmp.nominal = unmarshal_int32(ii)
            elif fld == 4587:
                tmp.mask = unmarshal_int32(ii)
            elif fld == -28896:
                tmp.alarm_enable = unmarshal_bool(ii)
            elif fld == 24630:
                tmp.alarm_status = unmarshal_bool(ii)
            elif fld == 18972:
                tmp.abort = unmarshal_bool(ii)
            elif fld == -32398:
                tmp.abort_inhibit = unmarshal_bool(ii)
            elif fld == 306:
                tmp.tries_needed = unmarshal_int32(ii)
            elif fld == 11070:
                tmp.tries_now = unmarshal_int32(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_BasicStatus_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if (nFlds % 2) != 0 or nFlds < 6 or nFlds > 16:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = BasicStatus_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 23553:
                tmp.on = unmarshal_bool(ii)
            elif fld == -21725:
                tmp.ready = unmarshal_bool(ii)
            elif fld == -7034:
                tmp.remote = unmarshal_bool(ii)
            elif fld == 10006:
                tmp.positive = unmarshal_bool(ii)
            elif fld == -28104:
                tmp.ramp = unmarshal_bool(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_TimedScalarArray_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 12:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = TimedScalarArray_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 7851:
                tmp.ref_id = unmarshal_int64(ii)
            elif fld == -10917:
                tmp.timestamp = unmarshal_int64(ii)
            elif fld == 1856:
                tmp.cycle = unmarshal_int64(ii)
            elif fld == 17492:
                tmp.status = unmarshal_int16(ii)
            elif fld == 32568:
                tmp.data = unmarshal_array(ii, unmarshal_double)
            elif fld == 13357:
                tmp.micros = unmarshal_array(ii, unmarshal_int64)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_ApplySettings_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if nFlds != 2:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = ApplySettings_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 17492:
                tmp.status = unmarshal_array(ii, unmarshal_SettingStatus_struct)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_Authenticate_reply(ii):
    nFlds = consumeRawInt(ii, 0x50)
    if (nFlds % 2) != 0 or nFlds < 0 or nFlds > 4:
        raise ProtocolError("incorrect number of fields")
    else:
        tmp = Authenticate_reply()
        for xx in range(nFlds // 2):
            fld = consumeRawInt(ii, 0x10)
            if fld == 25400:
                tmp.serviceName = unmarshal_string(ii)
            elif fld == -29279:
                tmp.token = unmarshal_binary(ii)
            else:
                raise ProtocolError("unknown field found")
        return tmp


def unmarshal_request(ii):
    """Attempts to unmarshal a request message from the specified
    generator, ii. If an error occurs, the ProtocolError exception
    will be raised."""
    try:
        unmarshal_header(ii)
        msg = consumeRawInt(ii, 0x10)
        if msg == -8230:
            return unmarshal_ServiceDiscovery_request(ii)
        elif msg == 2076:
            return unmarshal_OpenList_request(ii)
        elif msg == 97:
            return unmarshal_AddToList_request(ii)
        elif msg == -15277:
            return unmarshal_Authenticate_request(ii)
        elif msg == 21346:
            return unmarshal_EnableSettings_request(ii)
        elif msg == -3807:
            return unmarshal_RemoveFromList_request(ii)
        elif msg == -8779:
            return unmarshal_StartList_request(ii)
        elif msg == -15607:
            return unmarshal_ClearList_request(ii)
        elif msg == 21417:
            return unmarshal_StopList_request(ii)
        elif msg == -14728:
            return unmarshal_ApplySettings_request(ii)
        else:
            raise ProtocolError("unknown request type")
    except StopIteration:
        raise ProtocolError("unexpected end of input")


def unmarshal_reply(ii):
    """Attempts to unmarshal a reply message from the specified
    generator, ii. If an error occurs, the ProtocolError exception
    will be raised."""
    try:
        unmarshal_header(ii)
        msg = consumeRawInt(ii, 0x10)
        if msg == -12930:
            return unmarshal_ServiceDiscovery_reply(ii)
        elif msg == 13470:
            return unmarshal_OpenList_reply(ii)
        elif msg == -29780:
            return unmarshal_AddToList_reply(ii)
        elif msg == -3046:
            return unmarshal_RemoveFromList_reply(ii)
        elif msg == -27136:
            return unmarshal_StartList_reply(ii)
        elif msg == 29552:
            return unmarshal_ListStatus_reply(ii)
        elif msg == -10579:
            return unmarshal_Status_reply(ii)
        elif msg == 28653:
            return unmarshal_DeviceInfo_reply(ii)
        elif msg == -10581:
            return unmarshal_Scalar_reply(ii)
        elif msg == 23036:
            return unmarshal_ScalarArray_reply(ii)
        elif msg == -12450:
            return unmarshal_Raw_reply(ii)
        elif msg == -28878:
            return unmarshal_Text_reply(ii)
        elif msg == 12923:
            return unmarshal_TextArray_reply(ii)
        elif msg == 30088:
            return unmarshal_AnalogAlarm_reply(ii)
        elif msg == -14437:
            return unmarshal_DigitalAlarm_reply(ii)
        elif msg == -3335:
            return unmarshal_BasicStatus_reply(ii)
        elif msg == 31282:
            return unmarshal_TimedScalarArray_reply(ii)
        elif msg == 17344:
            return unmarshal_ApplySettings_reply(ii)
        elif msg == 7286:
            return unmarshal_Authenticate_reply(ii)
        else:
            raise ProtocolError("unknown reply type")
    except StopIteration:
        raise ProtocolError("unexpected end of input")
