# Copyright (c) 2017-2019 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import struct
from spinnman.model import RouterDiagnostics
from .abstract_multi_connection_process import AbstractMultiConnectionProcess

_N_REGISTERS = 16
_ONE_WORD = struct.Struct("<I")

""" A process for reading the diagnostic data block from a SpiNNaker\
router.
"""
__slots__ = [
"_control_register",
"_error_status",
"_register_values"]

def __init__(self, connection_selector):
"""
:param connection_selector:
:type connection_selector:
AbstractMultiConnectionProcessConnectionSelector
"""
super().__init__(connection_selector)
self._control_register = None
self._error_status = None
self._register_values = [0] * _N_REGISTERS

def __handle_control_register_response(self, response):
self._control_register = _ONE_WORD.unpack_from(
response.data, response.offset)[0]

def __handle_error_status_response(self, response):
self._error_status = _ONE_WORD.unpack_from(
response.data, response.offset)[0]

def __handle_register_response(self, response):
for register in range(_N_REGISTERS):
self._register_values[register] = _ONE_WORD.unpack_from(
response.data, response.offset + (register * 4))[0]

[docs]    def get_router_diagnostics(self, x, y):
"""
:param int x:
:param int y:
:rtype: RouterDiagnostics
"""