twister: pytest: use runner_params from hardware map YAML file

Use 'runner_params' specified in hardware map YAML file. This allows to
configure custom params (like openocd's adapter configuration for
FT232-based probe) when used with pytest harness.

Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/device/hardware_adapter.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/device/hardware_adapter.py
index 5a5a845..ef25d58 100644
--- a/scripts/pylib/pytest-twister-harness/src/twister_harness/device/hardware_adapter.py
+++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/device/hardware_adapter.py
@@ -66,6 +66,8 @@
         extra_args: list[str] = []
         runner = self.device_config.runner
         base_args.extend(['--runner', runner])
+        for param in self.device_config.runner_params:
+            extra_args.append(param)
         if board_id := self.device_config.id:
             if runner == 'pyocd':
                 extra_args.append('--board-id')
diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py
index 0c5cf2a..34a09aa 100644
--- a/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py
+++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py
@@ -68,6 +68,11 @@
         help='Use the specified west runner (pyocd, nrfjprog, etc.).'
     )
     twister_harness_group.addoption(
+        '--runner-params',
+        action='append',
+        help='Use the specified west runner params.'
+    )
+    twister_harness_group.addoption(
         '--device-id',
         help='ID of connected hardware device (for example 000682459367).'
     )
diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py
index bcb8871..ece190c 100644
--- a/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py
+++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py
@@ -22,6 +22,7 @@
     serial: str = ''
     baud: int = 115200
     runner: str = ''
+    runner_params: list[str] = field(default_factory=list, repr=False)
     id: str = ''
     product: str = ''
     serial_pty: str = ''
@@ -46,6 +47,9 @@
         west_flash_extra_args: list[str] = []
         if config.option.west_flash_extra_args:
             west_flash_extra_args = [w.strip() for w in config.option.west_flash_extra_args.split(',')]
+        runner_params: list[str] = []
+        if config.option.runner_params:
+            runner_params = [w.strip() for w in config.option.runner_params]
         device_from_cli = DeviceConfig(
             type=config.option.device_type,
             build_dir=_cast_to_path(config.option.build_dir),
@@ -54,6 +58,7 @@
             serial=config.option.device_serial,
             baud=config.option.device_serial_baud,
             runner=config.option.runner,
+            runner_params=runner_params,
             id=config.option.device_id,
             product=config.option.device_product,
             serial_pty=config.option.device_serial_pty,
diff --git a/scripts/pylib/pytest-twister-harness/tests/device/hardware_adapter_test.py b/scripts/pylib/pytest-twister-harness/tests/device/hardware_adapter_test.py
index 5655c4f..214a608 100644
--- a/scripts/pylib/pytest-twister-harness/tests/device/hardware_adapter_test.py
+++ b/scripts/pylib/pytest-twister-harness/tests/device/hardware_adapter_test.py
@@ -129,6 +129,43 @@
 
 
 @mock.patch('shutil.which', return_value='west')
+def test_if_get_command_returns_proper_string_with_runner_params_1(patched_which, device: HardwareAdapter) -> None:
+    device.device_config.build_dir = Path('build')
+    device.device_config.runner_params = ['--runner-param1', 'runner-param2']
+    device.generate_command()
+    assert isinstance(device.command, list)
+    assert device.command == [
+        'west', 'flash', '--skip-rebuild', '--build-dir', 'build',
+        '--runner', 'runner', '--', '--runner-param1', 'runner-param2'
+    ]
+
+
+@mock.patch('shutil.which', return_value='west')
+def test_if_get_command_returns_proper_string_with_runner_params_2(patched_which, device: HardwareAdapter) -> None:
+    device.device_config.build_dir = Path('build')
+    device.device_config.runner = 'openocd'
+    device.device_config.runner_params = [
+        '--cmd-pre-init', 'adapter serial FT1LRSRD',
+        '--cmd-pre-init', 'source [find interface/ftdi/jtag-lock-pick_tiny_2.cfg]',
+        '--cmd-pre-init', 'transport select swd',
+        '--cmd-pre-init', 'source [find target/nrf52.cfg]',
+        '--cmd-pre-init', 'adapter speed 10000',
+    ]
+    device.device_config.product = 'JTAG-lock-pick Tiny 2'
+    device.generate_command()
+    assert isinstance(device.command, list)
+    assert device.command == [
+        'west', 'flash', '--skip-rebuild', '--build-dir', 'build',
+        '--runner', 'openocd', '--',
+        '--cmd-pre-init', 'adapter serial FT1LRSRD',
+        '--cmd-pre-init', 'source [find interface/ftdi/jtag-lock-pick_tiny_2.cfg]',
+        '--cmd-pre-init', 'transport select swd',
+        '--cmd-pre-init', 'source [find target/nrf52.cfg]',
+        '--cmd-pre-init', 'adapter speed 10000',
+    ]
+
+
+@mock.patch('shutil.which', return_value='west')
 def test_if_get_command_returns_proper_string_with_west_flash_extra_args(
     patched_which, device: HardwareAdapter
 ) -> None:
diff --git a/scripts/pylib/twister/twisterlib/harness.py b/scripts/pylib/twister/twisterlib/harness.py
index efba14c..b65fa7c 100644
--- a/scripts/pylib/twister/twisterlib/harness.py
+++ b/scripts/pylib/twister/twisterlib/harness.py
@@ -382,6 +382,9 @@
         if runner := hardware.runner or options.west_runner:
             command.append(f'--runner={runner}')
 
+        for param in hardware.runner_params:
+            command.append(f'--runner-params={param}')
+
         if options.west_flash and options.west_flash != []:
             command.append(f'--west-flash-extra-args={options.west_flash}')
 
diff --git a/scripts/tests/twister/test_harness.py b/scripts/tests/twister/test_harness.py
index f989870..3657163 100644
--- a/scripts/tests/twister/test_harness.py
+++ b/scripts/tests/twister/test_harness.py
@@ -316,6 +316,7 @@
     hardware.serial = 'serial'
     hardware.baud = 115200
     hardware.runner = "runner"
+    hardware.runner_params = ["--runner-param1", "runner-param2"]
 
     options = handler.options
     options.west_flash = "args"
@@ -349,6 +350,8 @@
             assert '--device-serial=serial' in command
             assert '--device-serial-baud=115200' in command
         assert '--runner=runner' in command
+        assert '--runner-params=--runner-param1' in command
+        assert '--runner-params=runner-param2' in command
         assert '--west-flash-extra-args=args' in command
         assert '--device-id=123' in command
         assert '--device-product=product' in command