44from typing import TYPE_CHECKING , AnyStr
55
66import pexpect
7+ from pytest_embedded .dut import Dut
8+ from pytest_embedded_qemu .dut import QemuDut
9+ from pytest_embedded_qemu .qemu import Qemu
710from pytest_embedded_serial .dut import SerialDut
811
912if TYPE_CHECKING :
1013 from .app import NuttxApp
1114
1215
13- class NuttxDut (SerialDut ):
16+ class NuttxDut (Dut ):
1417 """
1518 Generic DUT class for use with NuttX RTOS.
1619 """
1720
1821 PROMPT_NSH = 'nsh>'
1922 PROMPT_TIMEOUT_S = 30
2023
21- def __init__ (
22- self ,
23- ** kwargs ,
24- ) -> None :
25- super ().__init__ (** kwargs )
26-
2724 def write (self , data : str ) -> None :
2825 """
2926 Write to NuttShell and sleep for a few hundred milliseconds to
@@ -45,11 +42,14 @@ def return_code(self, timeout: int = PROMPT_TIMEOUT_S) -> int:
4542 Matches the 'echo $?' response and extracts the integer value
4643 corresponding to the last program return code.
4744
45+ The first regex option on expect is for serial interface,
46+ while the second will match QEMU.
47+
4848 Returns:
4949 int: return code.
5050 """
5151 self .write ('echo $?' )
52- echo_match = self .expect (r'echo \$\?\r\n(\d+)' , timeout = timeout )
52+ echo_match = self .expect ([ r'echo \$\?\r\n(\d+)' , r'echo \$\?\n*(\d+)\n' ] , timeout = timeout )
5353 ret_code = re .findall (r'\d+' , echo_match .group ().decode ())
5454
5555 if not ret_code :
@@ -75,8 +75,78 @@ def write_and_return(self, data: str, timeout: int = 2) -> AnyStr:
7575 ans = self .expect (pexpect .TIMEOUT , timeout = timeout )
7676 return ans .rstrip ().decode ()
7777
78+ def reset_to_nsh (self , ready_prompt : str = PROMPT_NSH ) -> None :
79+ """
80+ Resets the board and waits until the Nuttshell prompt appears.
81+ Defaults to 'nsh>'.
82+
83+ Args:
84+ ready_prompt (str): string on prompt that signals completion.
85+
86+ Returns:
87+ None
88+ """
89+ if self .reset :
90+ logging .info ('Resetting board' )
91+ self .reset ()
92+ else :
93+ logging .error ('Resetting method not available' )
94+ self .expect (ready_prompt , timeout = self .PROMPT_TIMEOUT_S )
95+
96+
97+ class NuttxSerialDut (SerialDut , NuttxDut ):
98+ """
99+ DUT class for serial ports connected to generic boards running NuttX
100+ with NuttX RTOS.
101+ """
102+
103+ def __init__ (
104+ self ,
105+ ** kwargs ,
106+ ) -> None :
107+ super ().__init__ (** kwargs )
108+
109+ def reset (self ) -> None :
110+ """Reset the DUT by toggling the DTR line.
111+
112+ Args:
113+ None.
114+
115+ Returns:
116+ None.
117+ """
118+ self .serial .proc .dtr = False
119+ sleep (0.2 )
120+ self .serial .proc .dtr = True
121+
122+
123+ class NuttxQemuDut (QemuDut , NuttxDut ):
124+ """
125+ DUT class for QEMU usage of the NuttX RTOS.
126+ """
127+
128+ def __init__ (
129+ self ,
130+ qemu : Qemu ,
131+ ** kwargs ,
132+ ) -> None :
133+ self .qemu = qemu
134+
135+ super ().__init__ (qemu = qemu , ** kwargs )
78136
79- class NuttxEspDut (NuttxDut ):
137+ def reset (self ) -> None :
138+ """Hard reset the DUT.
139+
140+ Args:
141+ None.
142+
143+ Returns:
144+ None.
145+ """
146+ self .hard_reset ()
147+
148+
149+ class NuttxEspDut (NuttxSerialDut ):
80150 """
81151 DUT class for serial ports connected to Espressif boards which are
82152 flashed with NuttX RTOS.
@@ -91,16 +161,14 @@ def __init__(
91161
92162 super ().__init__ (app = app , ** kwargs )
93163
94- def reset_to_nsh (self , ready_prompt : str = NuttxDut . PROMPT_NSH ) -> None :
164+ def reset (self ) -> None :
95165 """
96- Resets the board and waits until the Nuttshell prompt appears.
97- Defaults to 'nsh>'.
166+ Resets the board.
98167
99168 Args:
100- ready_prompt (str): string on prompt that signals completion .
169+ None .
101170
102171 Returns:
103- None
172+ None.
104173 """
105174 self .serial .hard_reset ()
106- self .expect (ready_prompt , timeout = NuttxDut .PROMPT_TIMEOUT_S )
0 commit comments