replx-agent 2.0.0__py3-none-any.whl
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.
Potentially problematic release.
This version of replx-agent might be problematic. Click here for more details.
- replx_agent/__init__.py +3 -0
- replx_agent/__main__.py +5 -0
- replx_agent/commands.py +131 -0
- replx_agent/helpers/__init__.py +3 -0
- replx_agent/helpers/scanner.py +308 -0
- replx_agent/pkg/__init__.py +11 -0
- replx_agent/pkg/compiler.py +153 -0
- replx_agent/pkg/registry.py +387 -0
- replx_agent/pkg/service.py +690 -0
- replx_agent/pkg/store.py +83 -0
- replx_agent/protocol/__init__.py +11 -0
- replx_agent/protocol/repl.py +1029 -0
- replx_agent/protocol/storage.py +1018 -0
- replx_agent/rpc/__init__.py +3 -0
- replx_agent/rpc/protocol.py +220 -0
- replx_agent/server/__init__.py +3 -0
- replx_agent/server/__main__.py +4 -0
- replx_agent/server/command_dispatcher.py +49 -0
- replx_agent/server/connection_manager.py +654 -0
- replx_agent/server/core.py +1221 -0
- replx_agent/server/handlers/__init__.py +24 -0
- replx_agent/server/handlers/exec.py +553 -0
- replx_agent/server/handlers/filesystem.py +383 -0
- replx_agent/server/handlers/firmware.py +118 -0
- replx_agent/server/handlers/i2c.py +23 -0
- replx_agent/server/handlers/package.py +182 -0
- replx_agent/server/handlers/repl.py +188 -0
- replx_agent/server/handlers/session.py +205 -0
- replx_agent/server/handlers/spi.py +23 -0
- replx_agent/server/handlers/transfer.py +600 -0
- replx_agent/server/handlers/uart.py +32 -0
- replx_agent/server/session_manager.py +391 -0
- replx_agent/server/tcp.py +207 -0
- replx_agent/terminal.py +566 -0
- replx_agent/transport/__init__.py +32 -0
- replx_agent/transport/serial.py +220 -0
- replx_agent/utils/__init__.py +95 -0
- replx_agent/utils/constants.py +78 -0
- replx_agent/utils/device_info.py +183 -0
- replx_agent/utils/exceptions.py +31 -0
- replx_agent-2.0.0.dist-info/METADATA +74 -0
- replx_agent-2.0.0.dist-info/RECORD +46 -0
- replx_agent-2.0.0.dist-info/WHEEL +5 -0
- replx_agent-2.0.0.dist-info/entry_points.txt +2 -0
- replx_agent-2.0.0.dist-info/licenses/LICENSE +21 -0
- replx_agent-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import logging
|
|
3
|
+
import posixpath
|
|
4
|
+
import threading
|
|
5
|
+
|
|
6
|
+
from replx_agent.rpc.protocol import AgentProtocol
|
|
7
|
+
from replx_agent.pkg import service as pkg_service
|
|
8
|
+
from replx_agent.utils.exceptions import PkgError
|
|
9
|
+
from ..command_dispatcher import CommandContext
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
# Raw bytes per pkg_typehints_get response; base64 of this fits MAX_PAYLOAD_SIZE.
|
|
14
|
+
_TYPEHINTS_CHUNK_SIZE = 32768
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class PackageCommandsMixin:
|
|
18
|
+
"""Agent-side package management: the package store (~/.replx), GitHub
|
|
19
|
+
downloads and mpy-cross compilation live on the agent host, so a remote
|
|
20
|
+
client only exchanges commands/progress events over the agent protocol."""
|
|
21
|
+
|
|
22
|
+
@staticmethod
|
|
23
|
+
def _pkg_require_context(ctx: CommandContext) -> None:
|
|
24
|
+
if not ctx.connection or not ctx.core:
|
|
25
|
+
raise RuntimeError("Not connected")
|
|
26
|
+
|
|
27
|
+
def _cmd_pkg_search(self, ctx: CommandContext, owner: str = "PlanXLab",
|
|
28
|
+
repo: str = "replx_libs", ref: str = "main",
|
|
29
|
+
query: str = None) -> dict:
|
|
30
|
+
self._pkg_require_context(ctx)
|
|
31
|
+
return pkg_service.compute_search_rows(
|
|
32
|
+
ctx.core, ctx.device, owner, repo, ref, query=query or None
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def _cmd_pkg_store_status(self, ctx: CommandContext) -> dict:
|
|
36
|
+
self._pkg_require_context(ctx)
|
|
37
|
+
return pkg_service.store_status(ctx.core, ctx.device)
|
|
38
|
+
|
|
39
|
+
def _cmd_pkg_clean(self, ctx: CommandContext) -> dict:
|
|
40
|
+
self._pkg_require_context(ctx)
|
|
41
|
+
return pkg_service.clean(ctx.core, ctx.device)
|
|
42
|
+
|
|
43
|
+
def _cmd_pkg_typehints_list(self, ctx: CommandContext) -> dict:
|
|
44
|
+
self._pkg_require_context(ctx)
|
|
45
|
+
return pkg_service.typehints_list(ctx.core, ctx.device)
|
|
46
|
+
|
|
47
|
+
def _cmd_pkg_typehints_get(self, ctx: CommandContext, scope: str,
|
|
48
|
+
relpath: str, offset: int = 0,
|
|
49
|
+
length: int = _TYPEHINTS_CHUNK_SIZE) -> dict:
|
|
50
|
+
self._pkg_require_context(ctx)
|
|
51
|
+
data = pkg_service.typehints_read(scope, relpath, ctx.core, ctx.device)
|
|
52
|
+
offset = max(0, int(offset))
|
|
53
|
+
length = min(max(1, int(length)), _TYPEHINTS_CHUNK_SIZE)
|
|
54
|
+
chunk = data[offset:offset + length]
|
|
55
|
+
return {
|
|
56
|
+
"scope": scope,
|
|
57
|
+
"relpath": relpath,
|
|
58
|
+
"offset": offset,
|
|
59
|
+
"size": len(data),
|
|
60
|
+
"eof": offset + len(chunk) >= len(data),
|
|
61
|
+
"content": base64.b64encode(chunk).decode("ascii"),
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
def _cmd_pkg_download(self, ctx: CommandContext, owner: str = "PlanXLab",
|
|
65
|
+
repo: str = "replx_libs", ref: str = "main",
|
|
66
|
+
scope_filter: str = None, file_filter: str = None) -> None:
|
|
67
|
+
conn = ctx.connection
|
|
68
|
+
if not conn or not ctx.core:
|
|
69
|
+
if conn:
|
|
70
|
+
conn.release()
|
|
71
|
+
raise RuntimeError("Not connected")
|
|
72
|
+
|
|
73
|
+
seq = ctx.seq
|
|
74
|
+
client_addr = ctx.client_addr
|
|
75
|
+
core = ctx.core
|
|
76
|
+
device = ctx.device
|
|
77
|
+
|
|
78
|
+
def emit(data: dict) -> None:
|
|
79
|
+
self._safe_send(AgentProtocol.encode_message(
|
|
80
|
+
AgentProtocol.create_progress_stream(seq, data)
|
|
81
|
+
), client_addr)
|
|
82
|
+
|
|
83
|
+
def run() -> None:
|
|
84
|
+
try:
|
|
85
|
+
result = pkg_service.download(
|
|
86
|
+
core, device, owner, repo, ref,
|
|
87
|
+
scope_filter=scope_filter,
|
|
88
|
+
file_filter=file_filter,
|
|
89
|
+
progress=emit,
|
|
90
|
+
)
|
|
91
|
+
result["success"] = True
|
|
92
|
+
response = AgentProtocol.create_response(seq, result=result)
|
|
93
|
+
except Exception as exc:
|
|
94
|
+
response = AgentProtocol.create_response(seq, error=str(exc))
|
|
95
|
+
finally:
|
|
96
|
+
conn.release()
|
|
97
|
+
self._send_stream_completion(response, client_addr)
|
|
98
|
+
|
|
99
|
+
threading.Thread(
|
|
100
|
+
target=run,
|
|
101
|
+
daemon=True,
|
|
102
|
+
name=f"replx-pkg-download-{seq}",
|
|
103
|
+
).start()
|
|
104
|
+
|
|
105
|
+
def _cmd_pkg_install(self, ctx: CommandContext, spec: str) -> None:
|
|
106
|
+
conn = ctx.connection
|
|
107
|
+
if not conn or not conn.file_system:
|
|
108
|
+
if conn:
|
|
109
|
+
conn.release()
|
|
110
|
+
raise RuntimeError("Not connected")
|
|
111
|
+
|
|
112
|
+
seq = ctx.seq
|
|
113
|
+
client_addr = ctx.client_addr
|
|
114
|
+
core = ctx.core
|
|
115
|
+
device = ctx.device
|
|
116
|
+
version = ctx.version if ctx.version not in (None, "?") else ""
|
|
117
|
+
|
|
118
|
+
def emit(data: dict) -> None:
|
|
119
|
+
self._safe_send(AgentProtocol.encode_message(
|
|
120
|
+
AgentProtocol.create_progress_stream(seq, data)
|
|
121
|
+
), client_addr)
|
|
122
|
+
|
|
123
|
+
def run() -> None:
|
|
124
|
+
try:
|
|
125
|
+
batch = pkg_service.build_install_batch(
|
|
126
|
+
spec, core, device, version, progress=emit
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
for path in batch["dirs"]:
|
|
130
|
+
real_path = self._to_real_path(path, conn)
|
|
131
|
+
try:
|
|
132
|
+
conn.file_system.mkdir(real_path)
|
|
133
|
+
except Exception:
|
|
134
|
+
pass
|
|
135
|
+
|
|
136
|
+
files_total = batch["files"]
|
|
137
|
+
total_bytes = batch["total_bytes"]
|
|
138
|
+
sent = 0
|
|
139
|
+
for idx, ((local_path, remote_path), size) in enumerate(
|
|
140
|
+
zip(batch["specs"], batch["sizes"])):
|
|
141
|
+
emit({
|
|
142
|
+
"phase": "install",
|
|
143
|
+
"file": posixpath.basename(remote_path),
|
|
144
|
+
"file_index": idx,
|
|
145
|
+
"files_total": files_total,
|
|
146
|
+
"file_bytes": size,
|
|
147
|
+
"current": sent,
|
|
148
|
+
"total": total_bytes,
|
|
149
|
+
})
|
|
150
|
+
real_path = self._to_real_path(remote_path, conn)
|
|
151
|
+
with conn.file_system.repl.session():
|
|
152
|
+
conn.file_system.put(local_path, real_path)
|
|
153
|
+
sent += size
|
|
154
|
+
|
|
155
|
+
emit({
|
|
156
|
+
"phase": "install",
|
|
157
|
+
"file": "",
|
|
158
|
+
"file_index": files_total,
|
|
159
|
+
"files_total": files_total,
|
|
160
|
+
"file_bytes": 0,
|
|
161
|
+
"current": sent,
|
|
162
|
+
"total": total_bytes,
|
|
163
|
+
})
|
|
164
|
+
response = AgentProtocol.create_response(seq, result={
|
|
165
|
+
"success": True,
|
|
166
|
+
"files": files_total,
|
|
167
|
+
"bytes": total_bytes,
|
|
168
|
+
})
|
|
169
|
+
except PkgError as exc:
|
|
170
|
+
response = AgentProtocol.create_response(seq, error=str(exc))
|
|
171
|
+
except Exception as exc:
|
|
172
|
+
response = AgentProtocol.create_response(
|
|
173
|
+
seq, error=f"pkg_install failed: {exc}")
|
|
174
|
+
finally:
|
|
175
|
+
conn.release()
|
|
176
|
+
self._send_stream_completion(response, client_addr)
|
|
177
|
+
|
|
178
|
+
threading.Thread(
|
|
179
|
+
target=run,
|
|
180
|
+
daemon=True,
|
|
181
|
+
name=f"replx-pkg-install-{seq}",
|
|
182
|
+
).start()
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
import threading
|
|
4
|
+
|
|
5
|
+
from replx_agent.utils.constants import REPL_PROMPT
|
|
6
|
+
from replx_agent.utils.exceptions import TransportError
|
|
7
|
+
from ..command_dispatcher import CommandContext
|
|
8
|
+
from ..connection_manager import BoardConnection
|
|
9
|
+
|
|
10
|
+
class ReplCommandsMixin:
|
|
11
|
+
@staticmethod
|
|
12
|
+
def _read_repl_chunk(repl) -> bytes:
|
|
13
|
+
count = repl.in_waiting()
|
|
14
|
+
if count > 0:
|
|
15
|
+
return repl.read_bytes(count)
|
|
16
|
+
|
|
17
|
+
transport = getattr(repl, 'transport', None)
|
|
18
|
+
if transport is None or not hasattr(transport, 'read_byte'):
|
|
19
|
+
return b''
|
|
20
|
+
|
|
21
|
+
first = transport.read_byte(timeout=0.02)
|
|
22
|
+
if not first:
|
|
23
|
+
return b''
|
|
24
|
+
|
|
25
|
+
extra = repl.in_waiting()
|
|
26
|
+
if extra > 0:
|
|
27
|
+
return first + repl.read_bytes(extra)
|
|
28
|
+
return first
|
|
29
|
+
|
|
30
|
+
async def _repl_reader_task(self, conn: BoardConnection) -> None:
|
|
31
|
+
loop = asyncio.get_running_loop()
|
|
32
|
+
repl = conn.repl_protocol
|
|
33
|
+
consecutive_transient = 0
|
|
34
|
+
try:
|
|
35
|
+
while conn.repl.active and conn.repl_protocol:
|
|
36
|
+
try:
|
|
37
|
+
data = await loop.run_in_executor(
|
|
38
|
+
self._slow_executor, self._read_repl_chunk, repl
|
|
39
|
+
)
|
|
40
|
+
if data:
|
|
41
|
+
conn.repl.append_output(data)
|
|
42
|
+
consecutive_transient = 0
|
|
43
|
+
else:
|
|
44
|
+
consecutive_transient = 0
|
|
45
|
+
await asyncio.sleep(0.01)
|
|
46
|
+
except TransportError:
|
|
47
|
+
consecutive_transient += 1
|
|
48
|
+
if consecutive_transient >= 20:
|
|
49
|
+
break
|
|
50
|
+
await asyncio.sleep(0.005)
|
|
51
|
+
except asyncio.CancelledError:
|
|
52
|
+
pass
|
|
53
|
+
except Exception:
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
def _repl_reader_loop(self, conn: BoardConnection):
|
|
57
|
+
if not conn or not conn.repl_protocol:
|
|
58
|
+
return
|
|
59
|
+
repl = conn.repl_protocol
|
|
60
|
+
consecutive_transient = 0
|
|
61
|
+
while conn.repl.active and conn.repl_protocol:
|
|
62
|
+
try:
|
|
63
|
+
data = self._read_repl_chunk(repl)
|
|
64
|
+
if data:
|
|
65
|
+
conn.repl.append_output(data)
|
|
66
|
+
consecutive_transient = 0
|
|
67
|
+
else:
|
|
68
|
+
consecutive_transient = 0
|
|
69
|
+
time.sleep(0.01)
|
|
70
|
+
except TransportError:
|
|
71
|
+
consecutive_transient += 1
|
|
72
|
+
if consecutive_transient >= 20:
|
|
73
|
+
break
|
|
74
|
+
time.sleep(0.005)
|
|
75
|
+
except Exception:
|
|
76
|
+
break
|
|
77
|
+
|
|
78
|
+
def _cmd_repl_enter(self, ctx: CommandContext) -> dict:
|
|
79
|
+
conn = ctx.connection
|
|
80
|
+
if not conn or not conn.repl_protocol:
|
|
81
|
+
raise RuntimeError("Not connected")
|
|
82
|
+
|
|
83
|
+
if conn.repl.active:
|
|
84
|
+
conn.repl.stop()
|
|
85
|
+
|
|
86
|
+
repl = conn.repl_protocol
|
|
87
|
+
|
|
88
|
+
if repl._in_raw_repl:
|
|
89
|
+
try:
|
|
90
|
+
repl._leave_repl()
|
|
91
|
+
except Exception:
|
|
92
|
+
pass
|
|
93
|
+
|
|
94
|
+
repl.exit_paste_mode()
|
|
95
|
+
time.sleep(0.05)
|
|
96
|
+
repl.interrupt()
|
|
97
|
+
time.sleep(0.1)
|
|
98
|
+
|
|
99
|
+
drain_start = time.time()
|
|
100
|
+
while time.time() - drain_start < 0.3:
|
|
101
|
+
if repl.in_waiting() > 0:
|
|
102
|
+
repl.read_bytes(repl.in_waiting())
|
|
103
|
+
time.sleep(0.02)
|
|
104
|
+
else:
|
|
105
|
+
break
|
|
106
|
+
|
|
107
|
+
repl.send_raw(b'\r')
|
|
108
|
+
time.sleep(0.15)
|
|
109
|
+
|
|
110
|
+
response = b""
|
|
111
|
+
read_start = time.time()
|
|
112
|
+
while time.time() - read_start < 0.5:
|
|
113
|
+
if repl.in_waiting() > 0:
|
|
114
|
+
response += repl.read_bytes(repl.in_waiting())
|
|
115
|
+
if REPL_PROMPT in response:
|
|
116
|
+
break
|
|
117
|
+
time.sleep(0.02)
|
|
118
|
+
|
|
119
|
+
response_str = response.decode('utf-8', errors='replace')
|
|
120
|
+
prompt_found = '>>>' in response_str
|
|
121
|
+
|
|
122
|
+
if prompt_found:
|
|
123
|
+
conn.repl.start(ctx.ppid)
|
|
124
|
+
loop = getattr(self, '_loop', None)
|
|
125
|
+
if loop is not None and not loop.is_closed():
|
|
126
|
+
conn.repl.reader_future = asyncio.run_coroutine_threadsafe(
|
|
127
|
+
self._repl_reader_task(conn), loop
|
|
128
|
+
)
|
|
129
|
+
else:
|
|
130
|
+
conn.repl.reader_thread = threading.Thread(
|
|
131
|
+
target=self._repl_reader_loop,
|
|
132
|
+
args=(conn,),
|
|
133
|
+
daemon=True,
|
|
134
|
+
name=f'REPL-Reader-{conn.port}'
|
|
135
|
+
)
|
|
136
|
+
conn.repl.reader_thread.start()
|
|
137
|
+
else:
|
|
138
|
+
conn.release()
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
"entered": prompt_found,
|
|
142
|
+
"output": response_str
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
def _cmd_repl_exit(self, ctx: CommandContext) -> dict:
|
|
146
|
+
conn = ctx.connection
|
|
147
|
+
if not conn:
|
|
148
|
+
return {"exited": False, "reason": "Not connected"}
|
|
149
|
+
|
|
150
|
+
if not conn.repl.is_owner(ctx.ppid):
|
|
151
|
+
return {"exited": False, "reason": "Not the REPL session owner"}
|
|
152
|
+
|
|
153
|
+
conn.repl.stop()
|
|
154
|
+
try:
|
|
155
|
+
conn.repl_protocol.reset()
|
|
156
|
+
except Exception:
|
|
157
|
+
pass
|
|
158
|
+
conn.release()
|
|
159
|
+
|
|
160
|
+
return {"exited": True}
|
|
161
|
+
|
|
162
|
+
def _cmd_repl_write(self, ctx: CommandContext, data: str) -> dict:
|
|
163
|
+
conn = ctx.connection
|
|
164
|
+
if not conn or not conn.repl_protocol:
|
|
165
|
+
raise RuntimeError("Not connected")
|
|
166
|
+
|
|
167
|
+
if not conn.repl.is_owner(ctx.ppid):
|
|
168
|
+
raise RuntimeError("Not the REPL session owner")
|
|
169
|
+
|
|
170
|
+
if isinstance(data, str):
|
|
171
|
+
data = data.encode('utf-8')
|
|
172
|
+
|
|
173
|
+
conn.repl_protocol.send_raw(data)
|
|
174
|
+
return {"written": len(data)}
|
|
175
|
+
|
|
176
|
+
def _cmd_repl_read(self, ctx: CommandContext) -> dict:
|
|
177
|
+
conn = ctx.connection
|
|
178
|
+
if not conn or not conn.repl_protocol:
|
|
179
|
+
raise RuntimeError("Not connected")
|
|
180
|
+
|
|
181
|
+
if not conn.repl.is_owner(ctx.ppid):
|
|
182
|
+
raise RuntimeError("Not the REPL session owner")
|
|
183
|
+
|
|
184
|
+
data = conn.repl.read_output()
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
"output": data.decode('utf-8', errors='replace')
|
|
188
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import threading
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from replx_agent.utils import canon_port as _canon_port
|
|
6
|
+
from ..command_dispatcher import CommandContext
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SessionCommandsMixin:
|
|
10
|
+
@staticmethod
|
|
11
|
+
def _canon_port(port: str) -> str:
|
|
12
|
+
return _canon_port(port)
|
|
13
|
+
|
|
14
|
+
def _cmd_session_setup(self, ctx: CommandContext, port: str = None,
|
|
15
|
+
core: str = "RP2350", device: str = None,
|
|
16
|
+
as_foreground: bool = True, set_default: bool = False,
|
|
17
|
+
local_default: str = None) -> dict:
|
|
18
|
+
ppid = ctx.ppid
|
|
19
|
+
if not ppid:
|
|
20
|
+
raise ValueError("Session PPID required")
|
|
21
|
+
|
|
22
|
+
if not port and ctx.explicit_port:
|
|
23
|
+
port = ctx.explicit_port
|
|
24
|
+
|
|
25
|
+
if not port:
|
|
26
|
+
raise ValueError("Port is required")
|
|
27
|
+
|
|
28
|
+
port = self._canon_port(port)
|
|
29
|
+
conn_key = port
|
|
30
|
+
|
|
31
|
+
session = self._get_or_create_session(ppid)
|
|
32
|
+
|
|
33
|
+
if local_default:
|
|
34
|
+
session.default_port = self._canon_port(local_default)
|
|
35
|
+
|
|
36
|
+
existing_conn = self._get_connection(conn_key)
|
|
37
|
+
|
|
38
|
+
if existing_conn:
|
|
39
|
+
self.session_manager.add_connection_to_session(
|
|
40
|
+
ppid, port,
|
|
41
|
+
as_foreground=as_foreground,
|
|
42
|
+
default_port=self._effective_default_port(session)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
if set_default:
|
|
46
|
+
self._set_default_port(conn_key)
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
"connected": True,
|
|
50
|
+
"device": existing_conn.device,
|
|
51
|
+
"core": existing_conn.core,
|
|
52
|
+
"version": existing_conn.version,
|
|
53
|
+
"manufacturer": existing_conn.manufacturer,
|
|
54
|
+
"device_root_fs": existing_conn.device_root_fs,
|
|
55
|
+
"port": port,
|
|
56
|
+
"is_foreground": session.foreground == conn_key if session.foreground else False,
|
|
57
|
+
"existing": True,
|
|
58
|
+
"switched_from": None
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
new_conn = self._create_board_connection(
|
|
62
|
+
port=port,
|
|
63
|
+
core=core,
|
|
64
|
+
device=device
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
self._add_connection(conn_key, new_conn)
|
|
68
|
+
|
|
69
|
+
self.session_manager.add_connection_to_session(
|
|
70
|
+
ppid, port,
|
|
71
|
+
as_foreground=as_foreground,
|
|
72
|
+
default_port=self._effective_default_port(session)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
if set_default:
|
|
76
|
+
self._set_default_port(conn_key)
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
"connected": True,
|
|
80
|
+
"device": new_conn.device,
|
|
81
|
+
"core": new_conn.core,
|
|
82
|
+
"version": new_conn.version,
|
|
83
|
+
"manufacturer": new_conn.manufacturer,
|
|
84
|
+
"device_root_fs": new_conn.device_root_fs,
|
|
85
|
+
"port": port,
|
|
86
|
+
"is_foreground": session.foreground == conn_key if session.foreground else False,
|
|
87
|
+
"existing": False,
|
|
88
|
+
"switched_from": None
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
def _cmd_session_disconnect(self, ctx: CommandContext, port: str = None, all_ports: bool = False) -> dict:
|
|
92
|
+
ppid = ctx.ppid
|
|
93
|
+
if not port and ctx.explicit_port:
|
|
94
|
+
port = ctx.explicit_port
|
|
95
|
+
port = self._canon_port(port) if port else None
|
|
96
|
+
|
|
97
|
+
ports_to_close = []
|
|
98
|
+
session = self._get_session(ppid) if ppid else None
|
|
99
|
+
old_foreground = session.foreground if session else None
|
|
100
|
+
|
|
101
|
+
if all_ports:
|
|
102
|
+
if not ppid:
|
|
103
|
+
raise ValueError("Session PPID required")
|
|
104
|
+
if not session:
|
|
105
|
+
raise ValueError("No session found for this terminal")
|
|
106
|
+
ports_to_close = list(session.get_all_connections())
|
|
107
|
+
freed_port = "all"
|
|
108
|
+
elif port:
|
|
109
|
+
ports_to_close = [port]
|
|
110
|
+
freed_port = port
|
|
111
|
+
else:
|
|
112
|
+
if not ppid:
|
|
113
|
+
raise ValueError("Session PPID required")
|
|
114
|
+
if not session:
|
|
115
|
+
raise ValueError("No session found for this terminal")
|
|
116
|
+
if not session.foreground:
|
|
117
|
+
raise ValueError("No foreground connection to free")
|
|
118
|
+
ports_to_close = [session.foreground]
|
|
119
|
+
freed_port = session.foreground
|
|
120
|
+
|
|
121
|
+
for conn_port in ports_to_close:
|
|
122
|
+
# A user-requested disconnect is global: close the physical port
|
|
123
|
+
# even if another session references it or the board is busy, then
|
|
124
|
+
# remove it from every session. Session.remove_connection promotes
|
|
125
|
+
# the most recent background to foreground for each affected
|
|
126
|
+
# session.
|
|
127
|
+
conn = self.connection_manager.get_connection(conn_port)
|
|
128
|
+
if conn:
|
|
129
|
+
self.connection_manager.disconnect(conn_port)
|
|
130
|
+
# Always clean up session references even if the connection was
|
|
131
|
+
# already removed (e.g., by a concurrent _handle_port_disconnect).
|
|
132
|
+
self.session_manager.remove_connection_from_all_sessions(conn_port)
|
|
133
|
+
|
|
134
|
+
self.session_manager.cleanup_empty_sessions()
|
|
135
|
+
|
|
136
|
+
session = self._get_session(ppid) if ppid else None
|
|
137
|
+
remaining_connections = len(session.get_all_connections()) if session and not session.is_empty() else 0
|
|
138
|
+
new_foreground = session.foreground if session and not session.is_empty() else None
|
|
139
|
+
fg_changed = old_foreground != new_foreground
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
"freed_port": freed_port,
|
|
143
|
+
"new_foreground": new_foreground,
|
|
144
|
+
"fg_changed": fg_changed,
|
|
145
|
+
"remaining_connections": remaining_connections
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
def _cmd_session_switch_fg(self, ctx: CommandContext) -> dict:
|
|
149
|
+
ppid = ctx.ppid
|
|
150
|
+
if not ppid:
|
|
151
|
+
return {"success": False, "error": "Session PPID required"}
|
|
152
|
+
|
|
153
|
+
port = self._canon_port(ctx.explicit_port)
|
|
154
|
+
if not port:
|
|
155
|
+
return {"success": False, "error": "Port required"}
|
|
156
|
+
|
|
157
|
+
conn = self.connection_manager.get_connection(port)
|
|
158
|
+
if not conn:
|
|
159
|
+
return {"success": False, "error": f"Connection {port} not found"}
|
|
160
|
+
|
|
161
|
+
session = self._get_session(ppid)
|
|
162
|
+
if not session:
|
|
163
|
+
session = self._get_or_create_session(ppid)
|
|
164
|
+
|
|
165
|
+
old_fg = session.foreground
|
|
166
|
+
|
|
167
|
+
session.add_connection(port, as_foreground=True)
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
"success": True,
|
|
171
|
+
"old_foreground": old_fg,
|
|
172
|
+
"new_foreground": port,
|
|
173
|
+
"session_created": old_fg is None
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
def _cmd_set_default(self, ctx: CommandContext, port: str = None, update_session: bool = False) -> dict:
|
|
177
|
+
port = port or ctx.explicit_port
|
|
178
|
+
if port:
|
|
179
|
+
port = self._canon_port(port)
|
|
180
|
+
self._set_default_port(port)
|
|
181
|
+
|
|
182
|
+
if update_session and ctx.ppid:
|
|
183
|
+
session = self.session_manager.add_connection_to_session(
|
|
184
|
+
ctx.ppid,
|
|
185
|
+
port,
|
|
186
|
+
as_foreground=True,
|
|
187
|
+
default_port=port,
|
|
188
|
+
)
|
|
189
|
+
session.default_port = port
|
|
190
|
+
|
|
191
|
+
return {"default": port, "set": True}
|
|
192
|
+
else:
|
|
193
|
+
self._default_port = None
|
|
194
|
+
return {"default": None, "set": False}
|
|
195
|
+
|
|
196
|
+
def _cmd_release(self, ctx: CommandContext) -> dict:
|
|
197
|
+
if ctx.ppid:
|
|
198
|
+
if ctx.explicit_port:
|
|
199
|
+
return self._cmd_session_disconnect(ctx, port=ctx.explicit_port)
|
|
200
|
+
else:
|
|
201
|
+
return self._cmd_session_disconnect(ctx, all_ports=False)
|
|
202
|
+
|
|
203
|
+
if ctx.explicit_port:
|
|
204
|
+
return self._cmd_disconnect_port(ctx, port=ctx.explicit_port)
|
|
205
|
+
return {"released": False, "port": ""}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from ..command_dispatcher import CommandContext
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SpiCommandsMixin:
|
|
5
|
+
def _spi_resolve_port(self, ctx: CommandContext) -> str:
|
|
6
|
+
port = self._resolve_connection_for_session(ctx.ppid, ctx.explicit_port)
|
|
7
|
+
if not port:
|
|
8
|
+
raise ValueError("No active connection. Connect to a board first.")
|
|
9
|
+
return port
|
|
10
|
+
|
|
11
|
+
def _cmd_spi_bus_set(self, ctx: CommandContext, **kwargs) -> dict:
|
|
12
|
+
port = self._spi_resolve_port(ctx)
|
|
13
|
+
self._spi_bus[port] = dict(kwargs)
|
|
14
|
+
return self._spi_bus[port]
|
|
15
|
+
|
|
16
|
+
def _cmd_spi_bus_get(self, ctx: CommandContext) -> dict:
|
|
17
|
+
port = self._spi_resolve_port(ctx)
|
|
18
|
+
return self._spi_bus.get(port) or {}
|
|
19
|
+
|
|
20
|
+
def _cmd_spi_bus_clear(self, ctx: CommandContext) -> dict:
|
|
21
|
+
port = self._spi_resolve_port(ctx)
|
|
22
|
+
self._spi_bus.pop(port, None)
|
|
23
|
+
return {}
|