# Path: pyipmi/msgs/registry.py
# def create_request_by_name(self, name, *args, **kwargs):
#     return self.registry[name + "Req"](*args, **kwargs)
# 
# Path: pyipmi/utils.py
# def check_completion_code(cc):
#     if cc != constants.CC_OK:
#         raise CompletionCodeError(cc)
# 
# Path: pyipmi/state.py
# class State(DefaultProperties, ResponseDecoder):
#     """This is a container that represents a state.
# 
#     The state can have default
#     properties that are created by init.
#     """
# 
#     def __init__(self, rsp=None):
#         DefaultProperties.__init__(self)
#         ResponseDecoder.__init__(self, rsp)
# 
# Path: pyipmi/fields.py
# class VersionField(object):
#     """This class represent the Version fields defines by IPMI.
# 
#     Introduced with HPM the version field can hold additional auxiliary bytes.
#     """
# 
#     VERSION_FIELD_LEN = 2
#     VERSION_WITH_AUX_FIELD_LEN = 6
# 
#     def __init__(self, data=None):
#         self.major = None
#         self.minor = None
#         if data:
#             self._from_data(data)
# 
#     def _from_data(self, data):
#         if isinstance(data, str):
#             data = [ord(c) for c in data]
# 
#         data = array.array('B', data)
#         self.version = self._decode_data(data[0:2])
#         if len(data) == self.VERSION_WITH_AUX_FIELD_LEN:
#             self.auxiliary = data[2:6]
# 
#     def __str__(self):
#         return self.version_to_string()
# 
#     def _decode_data(self, data):
#         """`data` is array.array."""
#         self.major = data[0]
# 
#         if data[1] == 0xff:
#             self.minor = data[1]
#         elif data[1] <= 0x99:
#             self.minor = int(py3_array_tobytes(data[1:2]).decode('bcd+'))
#         else:
#             raise DecodingError()
# 
#     def version_to_string(self):
#         return ''.join("%s.%s" % (self.major, self.minor))


from .msgs import create_request_by_name
from .utils import check_completion_code
from .state import State
from .fields import VersionField

class DeviceId(State):

    def __str__(self):
        string = 'Device ID: %d' % self.device_id
        string += ' revision: %d' % self.revision
        string += ' available: %d' % self.available
        string += ' fw version: %s' % (self.fw_revision)
        string += ' ipmi: %s' % self.ipmi_version
        string += ' manufacturer: %d' % self.manufacturer_id
        string += ' product: %d' % self.product_id
        return string

    def supports_function(self, name):
        """Return if a function is supported.

        `name` is one of 'SENSOR', 'SDR_REPOSITORY', 'SEL', 'FRU_INVENTORY',
        'IPMB_EVENT_RECEIVER', 'IPMB_EVENT_GENERATOR', 'BRIDGE', 'CHASSIS'.
        """
        return name.lower() in self.supported_functions

    def _from_response(self, rsp):
        self.device_id = rsp.device_id
        self.revision = rsp.device_revision.device_revision
        self.provides_sdrs = bool(rsp.device_revision.provides_device_sdrs)
        self.available = bool(rsp.firmware_revision.device_available)

        self.fw_revision = VersionField(
            (rsp.firmware_revision.major, rsp.firmware_revision.minor))


# Path: pyipmi/msgs/registry.py
# def create_request_by_name(self, name, *args, **kwargs):
#     return self.registry[name + "Req"](*args, **kwargs)
# 
# Path: pyipmi/utils.py
# def check_completion_code(cc):
#     if cc != constants.CC_OK:
#         raise CompletionCodeError(cc)
# 
# Path: pyipmi/state.py
# class State(DefaultProperties, ResponseDecoder):
#     """This is a container that represents a state.
# 
#     The state can have default
#     properties that are created by init.
#     """
# 
#     def __init__(self, rsp=None):
#         DefaultProperties.__init__(self)
#         ResponseDecoder.__init__(self, rsp)
# 
# Path: pyipmi/fields.py
# class VersionField(object):
#     """This class represent the Version fields defines by IPMI.
# 
#     Introduced with HPM the version field can hold additional auxiliary bytes.
#     """
# 
#     VERSION_FIELD_LEN = 2
#     VERSION_WITH_AUX_FIELD_LEN = 6
# 
#     def __init__(self, data=None):
#         self.major = None
#         self.minor = None
#         if data:
#             self._from_data(data)
# 
#     def _from_data(self, data):
#         if isinstance(data, str):
#             data = [ord(c) for c in data]
# 
#         data = array.array('B', data)
#         self.version = self._decode_data(data[0:2])
#         if len(data) == self.VERSION_WITH_AUX_FIELD_LEN:
#             self.auxiliary = data[2:6]
# 
#     def __str__(self):
#         return self.version_to_string()
# 
#     def _decode_data(self, data):
#         """`data` is array.array."""
#         self.major = data[0]
# 
#         if data[1] == 0xff:
#             self.minor = data[1]
#         elif data[1] <= 0x99:
#             self.minor = int(py3_array_tobytes(data[1:2]).decode('bcd+'))
#         else:
#             raise DecodingError()
# 
#     def version_to_string(self):
#         return ''.join("%s.%s" % (self.major, self.minor))


# Path: pyipmi/bmc.py
from .msgs import create_request_by_name
from .utils import check_completion_code
from .state import State
from .fields import VersionField


class DeviceId(State):

    def __str__(self):
        string = 'Device ID: %d' % self.device_id
        string += ' revision: %d' % self.revision
        string += ' available: %d' % self.available
        string += ' fw version: %s' % (self.fw_revision)
        string += ' ipmi: %s' % self.ipmi_version
        string += ' manufacturer: %d' % self.manufacturer_id
        string += ' product: %d' % self.product_id
        return string

    def supports_function(self, name):
        """Return if a function is supported.

        `name` is one of 'SENSOR', 'SDR_REPOSITORY', 'SEL', 'FRU_INVENTORY',
        'IPMB_EVENT_RECEIVER', 'IPMB_EVENT_GENERATOR', 'BRIDGE', 'CHASSIS'.
        """
        return name.lower() in self.supported_functions

    def _from_response(self, rsp):
        self.device_id = rsp.device_id
        self.revision = rsp.device_revision.device_revision
        self.provides_sdrs = bool(rsp.device_revision.provides_device_sdrs)
        self.available = bool(rsp.firmware_revision.device_available)

        self.fw_revision = VersionField(
            (rsp.firmware_revision.major, rsp.firmware_revision.minor))


        self.ipmi_version = VersionField(