sdev 0.4.1__tar.gz → 0.4.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdev
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: 串口控制器工具包
5
5
  Home-page: https://github.com/klrc/sdev
6
6
  Author: klrc
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sdev"
7
- version = "0.4.1"
7
+ version = "0.4.2"
8
8
  description = "串口控制器工具包"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -14,7 +14,7 @@ from typing import Literal
14
14
  from .core import SerialDevice
15
15
  from .class_wrapper import Demoboard, Relay
16
16
 
17
- __version__ = "0.4.1"
17
+ __version__ = "0.4.2"
18
18
  __author__ = "klrc"
19
19
  __email__ = "1440698245@qq.com"
20
20
 
@@ -21,6 +21,10 @@ class Demoboard(SerialDevice):
21
21
  - 提供 shell() / check_alive() 等高层方法。
22
22
  """
23
23
 
24
+ def __init__(self, *args, **kwargs):
25
+ super().__init__(*args, **kwargs)
26
+ self.check_alive()
27
+
24
28
  def shell(
25
29
  self,
26
30
  cmd: str,
@@ -35,20 +39,14 @@ class Demoboard(SerialDevice):
35
39
  self,
36
40
  *,
37
41
  uboot_flag: str = "uboot#",
38
- awaken_flag: str = "Process",
39
42
  prompt_flag: str = " #",
40
- ctrl_c_timeout: float = 5.0,
41
- reboot_timeout: float = 120.0,
42
- prompt_timeout: float = 60.0,
43
+ response_timeout: float = 3,
43
44
  ) -> None:
44
45
  _check_alive_func(
45
46
  self,
46
47
  uboot_flag=uboot_flag,
47
- awaken_flag=awaken_flag,
48
48
  prompt_flag=prompt_flag,
49
- ctrl_c_timeout=ctrl_c_timeout,
50
- reboot_timeout=reboot_timeout,
51
- prompt_timeout=prompt_timeout,
49
+ response_timeout=response_timeout,
52
50
  )
53
51
 
54
52
 
@@ -64,3 +62,4 @@ class Relay(SerialDevice):
64
62
 
65
63
 
66
64
  __all__ = ["Demoboard", "Relay"]
65
+
@@ -85,13 +85,9 @@ def shell(
85
85
 
86
86
  def check_alive(
87
87
  device: SerialDevice,
88
- *,
89
88
  uboot_flag: str = "uboot#",
90
- awaken_flag: str = "Process",
91
89
  prompt_flag: str = " #",
92
- ctrl_c_timeout: float = 5.0,
93
- reboot_timeout: float = 120.0,
94
- prompt_timeout: float = 60.0,
90
+ response_timeout: float = 3,
95
91
  ) -> None:
96
92
  """
97
93
  检查 demoboard 是否「醒着」且有 shell 提示符。
@@ -102,33 +98,41 @@ def check_alive(
102
98
  2. 若在 U-Boot:
103
99
  - 发送 "reboot";
104
100
  - 等待 awaken_flag(如 "Process ... done" 中的 "Process")出现,超时则抛 TimeoutError;
105
- 3. 最后,无论是否经过 reboot,统一等待 shell 提示符 prompt_flag(如 " #"),超时抛 TimeoutError。
101
+ 3. 最后,无论是否经过 reboot,统一等待 shell 提示符 prompt_flag(如 "#"),超时抛 TimeoutError。
106
102
  """
107
- # 1. 发送 Ctrl-C 探测状态
108
- try:
109
- # 尝试通过 Ctrl-C 获取响应
110
- lines = shell(device, CTRL_C, prompt_flag=prompt_flag, timeout=ctrl_c_timeout)
111
- except TimeoutError:
112
- # 如果超时,说明可能正在启动或者完全没响应,尝试盲发 Ctrl-C 并等待
113
- device.send(CTRL_C)
114
- lines = list(device.scan(end_flag=None, timeout=ctrl_c_timeout))
115
-
116
- # 2. 检查是否在 U-Boot
117
- is_uboot = any(uboot_flag in line for line in lines)
118
- if is_uboot:
119
- logger.warning(f"Device is in U-Boot (matched {uboot_flag!r}), rebooting...")
120
- device.send("reboot")
121
- # 等待系统开始启动(出现 awaken_flag)
122
- list(device.scan(end_flag=awaken_flag, timeout=reboot_timeout))
123
- logger.info("Reboot signal detected, waiting for system to boot...")
124
-
125
- # 3. 统一等待最终提示符
103
+
104
+ # 1. Ctrl-C,判定控制台是否响应
105
+ under_reboot = False
126
106
  try:
127
- list(device.scan(end_flag=prompt_flag, timeout=prompt_timeout))
128
- logger.info(f"Device is alive (matched prompt {prompt_flag!r})")
107
+ lines = shell(device, CTRL_C, prompt_flag, timeout=response_timeout)
129
108
  except TimeoutError:
130
- logger.error(f"Timed out waiting for prompt {prompt_flag!r}")
131
- raise
109
+ wait_iter = 0
110
+ while True:
111
+ device.send(CTRL_C)
112
+ lines = shell(device, None, None, timeout=response_timeout, clear=False)
113
+ if len(lines) > 0:
114
+ under_reboot = True
115
+ break
116
+ wait_iter += 1
117
+ if wait_iter > 5:
118
+ logger.warning("CTRL-C test failed, please check the connection")
119
+ break
120
+
121
+ # 2. 检查是否被中止进入uboot
122
+ for line in lines:
123
+ if uboot_flag in line:
124
+ logger.warning("uboot flag found, rebooting")
125
+ device.send("reboot")
126
+ under_reboot = True
127
+ break
128
+
129
+ # 3. 进入启动流程
130
+ if under_reboot:
131
+ try:
132
+ lines = shell(device, None, None, timeout=response_timeout)
133
+ except TimeoutError:
134
+ pass # 等待启动彻底完成
135
+ return check_alive(device, uboot_flag, prompt_flag, response_timeout)
132
136
 
133
137
 
134
138
  if __name__ == "__main__":
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdev
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: 串口控制器工具包
5
5
  Home-page: https://github.com/klrc/sdev
6
6
  Author: klrc
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="sdev",
8
- version="0.4.1",
8
+ version="0.4.2",
9
9
  author="klrc",
10
10
  author_email="144069824@qq.com",
11
11
  description="串口控制器工具包",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes