예제 #1
0
    def __init__(self):
        self.board_type = config.read_config('board_type')
        self.board_class = open_nodes.node_class(self.board_type)
        self.robot_type = config.read_config('robot', None)
        self.node_id = os.uname()[1]

        self.profile_from_dict = functools.partial(profile.Profile.from_dict,
                                                   self.board_class)
        self.default_profile = self.profile_from_dict(config.DEFAULT_PROFILE)
예제 #2
0
    def test_read_config(self):
        with mock.patch(utils.CFG_VAR_PATH, utils.test_cfg_dir('m3_no_robot')):
            self.assertEquals('m3', config.read_config('board_type'))
            self.assertEquals('m3', config.read_config('board_type', 'def'))

            self.assertRaises(IOError, config.read_config, 'robot')
            self.assertEquals(None, config.read_config('robot', None))

        with mock.patch(utils.CFG_VAR_PATH, utils.test_cfg_dir('m3_robot')):
            self.assertEquals('m3', config.read_config('board_type'))
            self.assertEquals('turtlebot2', config.read_config('robot'))
예제 #3
0
    def test_read_config(self):
        with mock.patch(utils.CFG_VAR_PATH, utils.test_cfg_dir('m3_no_robot')):
            self.assertEquals('m3', config.read_config('board_type'))
            self.assertEquals('m3', config.read_config('board_type', 'def'))

            self.assertRaises(IOError, config.read_config, 'robot')
            self.assertEquals(None, config.read_config('robot', None))

        with mock.patch(utils.CFG_VAR_PATH, utils.test_cfg_dir('m3_robot')):
            self.assertEquals('m3', config.read_config('board_type'))
            self.assertEquals('turtlebot2', config.read_config('robot'))
예제 #4
0
    def __init__(self):
        board_type = config.read_config('board_type')
        self.board_class = open_nodes.node_class(board_type)
        cn_type = config.read_config('control_node_type', 'iotlab')
        self.cn_class = config.control_node_class(cn_type)

        self.robot_type = config.read_config('robot', None)
        self.node_id = config.read_config('hostname')

        self.profile_from_dict = functools.partial(profile.Profile.from_dict,
                                                   self.board_class)
        self.default_profile = self.profile_from_dict(config.DEFAULT_PROFILE)
예제 #5
0
    def _check_debug(self, board_class):
        if (not hasattr(board_class, 'debug_stop') or  # no debug
                # Only openocd debug tested here (arm)
                not hasattr(board_class, 'OPENOCD_CFG_FILE')):
            return

        firmware = abspath(board_class.FW_AUTOTEST)
        hostname = config.read_config('ip', 'localhost')
        gdb_cmd = [
            'gdb',
            '-ex', 'set confirm off',
            '-ex', f'target remote {hostname}:3333',
            '-ex', 'monitor reset halt',
            '-ex', f'monitor flash write_image erase {firmware}',
            '-ex', 'monitor reset init',
            '-ex', 'monitor reset run',
            '-ex', 'quit',
        ]

        # Flash idle firmware
        ret = self._flash()
        self.assertEqual(0, ret.json['ret'])

        # idle firmware, there should be no reply
        self._check_node_echo(echo=False)

        # Start debugger
        ret = self.server.put('/open/debug/start')
        self.assertEqual(0, ret.json['ret'])

        # Flash autotest firmware
        ret = subprocess.call(gdb_cmd, stderr=subprocess.STDOUT)
        self.assertEqual(0, ret)
        time.sleep(1)

        # Autotest fw should be running
        self._check_node_echo(echo=True)

        # Flash idle firmware should fail
        ret = self._flash()
        self.assertNotEqual(0, ret.json['ret'])

        # No flash, Autotest fw should be still running
        self._check_node_echo(echo=True)

        # Stop debugger
        ret = self.server.put('/open/debug/stop')
        self.assertEqual(0, ret.json['ret'])

        # Make sure the node can be reflashed after debug session is done
        time.sleep(1)
        ret = self._flash()
        self.assertEqual(0, ret.json['ret'])
예제 #6
0
class OpenNodeConnection:
    """ Connects to serial port redirection and sends messages """
    HOST = config.read_config('ip', 'localhost')
    PORT = 20000

    def __init__(self, host=HOST, port=PORT, timeout=5.0):
        self.address = (host, port)
        self.timeout = timeout
        self.fd = None  # pylint:disable=invalid-name
        self.sock = None

    def start(self):
        """ Connect to the serial_redirection """
        try:
            self.sock = self.try_connect(self.address)
            self.sock.settimeout(self.timeout)  # pylint:disable=no-member
            self.fd = self.sock.makefile('rw')
            return 0
        except IOError:
            return 1

    def stop(self):
        """ Close the connection and wait until the connection is ready
        for reconnection """
        self.fd.close()
        self.fd = None
        self.sock = None

        # Wait redirection restarted
        # Should not wait on start because connection should work instantly
        # As it's the real user use case
        time.sleep(1.0)
        return 0

    @staticmethod
    def try_connect(address=(HOST, PORT), tries=10, step=0.5):
        """ Try connecting 'tries' time to address (host, port) tuple
        Sleep 'step' between each tries.
        If last trial fails, the IOError is raised

        The goal is to be resilient to the fact that serial_aggregator might be
        (re)starting.  """
        # Run 'tries -1' times with 'try except'
        for _ in range(0, tries - 1):
            try:
                return socket.create_connection(address)
            except IOError:
                time.sleep(step)

        # Last connection should run an exception
        return socket.create_connection(address)

    def send_command(self, command_list):
        """ Send command and wait for answer """
        assert isinstance(command_list, (list, tuple))
        packet = ' '.join(command_list)

        LOGGER.debug("Command send:   %r", packet)
        self._writeline(packet)
        answer = self._readline()
        LOGGER.debug("Command answer: %r", answer)
        return answer

    def _writeline(self, line):
        """ Write a line """
        self.fd.write(line + '\n')
        self.fd.flush()

    def _readline(self):
        """ Read a line """
        try:
            answer = self.fd.readline()
            if answer.endswith('\n'):
                return answer.strip().split(' ')
        except (socket.timeout, IOError) as err:
            LOGGER.warning("Read timeout: %s", err)
        return None

    @classmethod
    def send_one_command(cls, command_list, *args, **kwargs):
        """ Quick sending function.
        Connects, sends, reads answer and disconnects """
        with cls(*args, **kwargs) as conn:
            return conn.send_command(command_list)

    def __enter__(self):
        ret = self.start()
        if ret:
            raise RuntimeError("Error when starting serial, see logger")
        return self

    def __exit__(self, _type, _value, _traceback):
        self.stop()