mqtt_server.py 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. """MQTT broker for virtual printer.
  2. Implements an MQTT broker that accepts connections from slicers,
  3. authenticates with the configured access code, and logs print commands.
  4. """
  5. import asyncio
  6. import copy
  7. import hmac
  8. import json
  9. import logging
  10. import socket
  11. import ssl
  12. import time
  13. from collections.abc import Callable
  14. from pathlib import Path
  15. from typing import TYPE_CHECKING
  16. from backend.app.services.virtual_printer._debug import append_event, dump_wire
  17. if TYPE_CHECKING:
  18. from backend.app.services.virtual_printer.mqtt_bridge import MQTTBridge
  19. logger = logging.getLogger(__name__)
  20. # Default MQTT port for Bambu printers (MQTT over TLS)
  21. MQTT_PORT = 8883
  22. # Per-IP MQTT auth rate-limit. 5 failures within 60 s blocks further attempts
  23. # for the remainder of the window. Bambu printers themselves don't rate-limit,
  24. # but they're not exposed past the LAN edge; Bambuddy's VPs sometimes are
  25. # (Tailscale, port-forwarded), so an 8-char access code without any
  26. # brute-force friction is too weak. The window auto-recovers — no manual
  27. # unblock — so a legitimate user who fat-fingered their access code 5 times
  28. # only waits up to 60 s.
  29. _AUTH_RATE_LIMIT_MAX_ATTEMPTS = 5
  30. _AUTH_RATE_LIMIT_WINDOW_SECONDS = 60.0
  31. # Pending-request map bound. Each entry maps a slicer command's
  32. # sequence_id to its originating client_id so the bridge response can be
  33. # routed back to just that client. Bounded so a slicer that issues
  34. # commands without ever consuming responses can't leak memory.
  35. _PENDING_REQUEST_MAX_ENTRIES = 256
  36. # Target-printer gcode_states for which the VP mirrors live print progress to
  37. # the slicer (#1887). BambuStudio and OrcaSlicer gate BOTH the Device-tab
  38. # progress panel and the Send button on the same predicate —
  39. # `MachineObject::is_in_printing()`, i.e. gcode_state in
  40. # {RUNNING, PAUSE, SLICING, PREPARE} — so reporting the real state verbatim
  41. # would show progress at the cost of blocking Send for as long as the printer
  42. # prints. That is exactly the #1558 regression. `FINISH` is the one state that
  43. # renders the progress panel (`is_in_printing() || print_status == "FINISH"` in
  44. # StatusPanel::update_subtask) while leaving Send enabled, so the mirror keeps
  45. # reporting FINISH and only fills in the numbers underneath it.
  46. _MIRRORED_PRINT_STATES = frozenset({"RUNNING", "PAUSE"})
  47. # How long after the last upload-state transition the VP keeps echoing the
  48. # slicer's own filename back at it before switching the report over to whatever
  49. # the target printer is really printing. The slicer releases its in-flight-job
  50. # lock when it sees gcode_state=FINISH carrying the subtask_name it uploaded
  51. # (#1280 / #1658); swapping in the printer's filename while that handshake is
  52. # still in flight wedges the send modal at "Downloading". 5 s covers the 1.5 s
  53. # `_schedule_finish_release` timer plus several 1 Hz pushes.
  54. _UPLOAD_SETTLE_SECONDS = 5.0
  55. # Model code → product_name for version response (must match what slicer expects)
  56. MODEL_PRODUCT_NAMES = {
  57. "BL-P001": "X1 Carbon",
  58. "BL-P002": "X1",
  59. "C13": "X1E",
  60. "N6": "X2D",
  61. "N9": "A2L",
  62. "C11": "P1P",
  63. "C12": "P1S",
  64. "N7": "P2S",
  65. "N2S": "A1",
  66. "N1": "A1 mini",
  67. "O1D": "H2D",
  68. "O1C": "H2C",
  69. "O1C2": "H2C",
  70. "O1S": "H2S",
  71. }
  72. class VirtualPrinterMQTTServer:
  73. """MQTT broker that accepts connections from slicers.
  74. This is a minimal MQTT broker implementation that:
  75. - Accepts TLS connections on port 8883
  76. - Authenticates with username 'bblp' and the configured access code
  77. - Receives print commands on device/{serial}/request
  78. - Can publish status on device/{serial}/report
  79. """
  80. def __init__(
  81. self,
  82. serial: str,
  83. access_code: str,
  84. cert_path: Path,
  85. key_path: Path,
  86. port: int = MQTT_PORT,
  87. on_print_command: Callable[[str, dict], None] | None = None,
  88. ):
  89. """Initialize the MQTT server.
  90. Args:
  91. serial: Virtual printer serial number
  92. access_code: Password for authentication
  93. cert_path: Path to TLS certificate
  94. key_path: Path to TLS private key
  95. port: Port to listen on (default 8883)
  96. on_print_command: Callback when print command received (filename, data)
  97. """
  98. self.serial = serial
  99. self.access_code = access_code
  100. self.cert_path = cert_path
  101. self.key_path = key_path
  102. self.port = port
  103. self.on_print_command = on_print_command
  104. self._running = False
  105. self._broker = None
  106. self._broker_task = None
  107. async def start(self) -> None:
  108. """Start the MQTT broker."""
  109. if self._running:
  110. return
  111. # Try to import amqtt
  112. try:
  113. from amqtt.broker import Broker
  114. except ImportError:
  115. logger.error("amqtt not installed. Run: pip install amqtt")
  116. return
  117. logger.info("Starting virtual printer MQTT broker on port %s", self.port)
  118. # Build broker configuration
  119. config = {
  120. "listeners": {
  121. "default": {
  122. "type": "tcp",
  123. "bind": f"0.0.0.0:{self.port}",
  124. "ssl": "on",
  125. "certfile": str(self.cert_path),
  126. "keyfile": str(self.key_path),
  127. },
  128. },
  129. "auth": {
  130. "allow-anonymous": False,
  131. "plugins": ["auth_custom"],
  132. },
  133. "topic-check": {
  134. "enabled": False, # Allow any topic
  135. },
  136. }
  137. try:
  138. self._running = True
  139. # Create and start broker
  140. self._broker = Broker(config)
  141. # Register custom auth plugin
  142. self._broker.plugins_manager.plugins_handlers["auth_custom"] = self._authenticate
  143. # Start the broker
  144. await self._broker.start()
  145. logger.info("MQTT broker started on port %s", self.port)
  146. # Keep running
  147. while self._running:
  148. await asyncio.sleep(1)
  149. except OSError as e:
  150. if e.errno == 98: # Address already in use
  151. logger.error("MQTT port %s is already in use", self.port)
  152. else:
  153. logger.error("MQTT broker error: %s", e)
  154. except asyncio.CancelledError:
  155. logger.debug("MQTT broker task cancelled")
  156. except Exception as e:
  157. logger.error("MQTT broker error: %s", e)
  158. finally:
  159. await self.stop()
  160. async def _authenticate(self, session) -> bool:
  161. """Authenticate MQTT connection.
  162. Args:
  163. session: MQTT session with username/password
  164. Returns:
  165. True if authentication successful
  166. """
  167. username = getattr(session, "username", None)
  168. password = getattr(session, "password", None)
  169. # Bambu slicers use 'bblp' as username and access code as password
  170. if username == "bblp" and password == self.access_code:
  171. logger.debug("MQTT client authenticated from %s", session.remote_address)
  172. return True
  173. logger.warning("MQTT auth failed for user '%s' from %s", username, session.remote_address)
  174. return False
  175. async def stop(self) -> None:
  176. """Stop the MQTT broker."""
  177. logger.info("Stopping MQTT broker")
  178. self._running = False
  179. if self._broker:
  180. try:
  181. await self._broker.shutdown()
  182. except OSError as e:
  183. logger.debug("Error shutting down MQTT broker: %s", e)
  184. self._broker = None
  185. class SimpleMQTTServer:
  186. """Simplified MQTT server using raw sockets.
  187. This is a fallback implementation that handles basic MQTT protocol
  188. without requiring the amqtt library. It's less feature-complete but
  189. more lightweight.
  190. """
  191. def __init__(
  192. self,
  193. serial: str,
  194. access_code: str,
  195. cert_path: Path,
  196. key_path: Path,
  197. port: int = MQTT_PORT,
  198. on_print_command: Callable[[str, dict], None] | None = None,
  199. model: str = "",
  200. bind_address: str = "0.0.0.0", # nosec B104
  201. vp_name: str = "",
  202. ):
  203. self.serial = serial
  204. self.access_code = access_code
  205. self.model = model
  206. self.cert_path = cert_path
  207. self.key_path = key_path
  208. self.port = port
  209. self.on_print_command = on_print_command
  210. self.bind_address = bind_address
  211. self.vp_name = vp_name
  212. self._log_prefix = f"[{vp_name}] " if vp_name else ""
  213. self._running = False
  214. # Set after the socket is bound — see ftp_server.py for rationale.
  215. self.ready = asyncio.Event()
  216. self._server = None
  217. self._clients: dict[str, asyncio.StreamWriter] = {}
  218. # Per-client "effective serial" — the serial the slicer actually uses in
  219. # device/{serial}/report|request topics. Populated from the first
  220. # SUBSCRIBE/PUBLISH we see on a connection. This lets the VP respond on
  221. # the topic the slicer is listening on even when it disagrees with
  222. # self.serial (e.g. a stale Orca config that was bound to an older VP
  223. # serial, or a printer entry that was re-pointed at the VP IP without
  224. # updating the serial).
  225. self._client_serials: dict[str, str] = {}
  226. self._status_push_task: asyncio.Task | None = None
  227. self._sequence_id = 0
  228. # Dynamic state for status reports
  229. self._gcode_state = "IDLE"
  230. self._current_file = ""
  231. self._prepare_percent = "0"
  232. # Monotonic timestamp of the last upload-state transition, so the live-
  233. # progress mirror can tell whether the slicer is still waiting on its
  234. # own upload handshake. Starts at -inf: a VP that has never seen an
  235. # upload has no handshake to protect and can mirror immediately.
  236. self._state_changed_at = float("-inf")
  237. # MQTT bridge for non-proxy modes — set by VirtualPrinterInstance after start().
  238. # When the bridge is_active, real printer pushes are fanned out to slicers and
  239. # the synthetic 1s push is suspended. When the target printer goes offline the
  240. # synthetic fallback resumes automatically.
  241. self._bridge: MQTTBridge | None = None
  242. # Per-source-IP failed-auth tracker for rate-limiting / lockout.
  243. # Maps IP → list[monotonic timestamp] of recent failures within the
  244. # window. Pruned on every check so it doesn't grow unbounded.
  245. self._auth_failures: dict[str, list[float]] = {}
  246. # Maps sequence_id → originating client_id for slicer-initiated
  247. # commands forwarded to the real printer. Used in
  248. # ``push_raw_to_clients`` to route the printer's response only
  249. # back to the requesting slicer instead of fanning out to all
  250. # connected clients (which leaks slicer A's responses to slicer
  251. # B in multi-slicer setups). FIFO-bounded; if a response never
  252. # arrives the entry ages out instead of leaking.
  253. self._pending_requests: dict[str, str] = {}
  254. async def start(self) -> None:
  255. """Start the MQTT server."""
  256. if self._running:
  257. return
  258. logger.info("Starting simple MQTT server on port %s", self.port)
  259. # Create SSL context with Bambu-compatible settings
  260. ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  261. ssl_context.load_cert_chain(str(self.cert_path), str(self.key_path))
  262. # Match Bambu printer behavior - accept any client
  263. ssl_context.verify_mode = ssl.CERT_NONE
  264. # Allow TLS 1.2 for broader compatibility (some slicers may not support 1.3)
  265. ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
  266. # Match real Bambu printer cipher behaviour: include the plain-RSA
  267. # AES-GCM suites the slicer expects. On hardened distros
  268. # (Fedora / RHEL with `update-crypto-policies`, hardened Alpine builds)
  269. # OpenSSL's `DEFAULT` list strips these suites, leaving no overlap
  270. # with the slicer's MQTT-over-TLS ClientHello — handshake fails
  271. # immediately and the slicer reports a connect error before any MQTT
  272. # CONNECT can be sent (#1610 audit). Same shape as the #620 fix.
  273. ssl_context.set_ciphers("DEFAULT:AES256-GCM-SHA384:AES128-GCM-SHA256")
  274. # Disable hostname checking
  275. ssl_context.check_hostname = False
  276. # Log certificate info
  277. import subprocess
  278. try:
  279. result = subprocess.run(
  280. ["openssl", "x509", "-in", str(self.cert_path), "-noout", "-subject", "-issuer"],
  281. capture_output=True,
  282. text=True,
  283. timeout=5,
  284. )
  285. logger.info("MQTT SSL cert info: %s", result.stdout.strip())
  286. except (OSError, subprocess.SubprocessError):
  287. pass # Certificate info is for debug logging only; not critical
  288. logger.info("MQTT SSL context: TLS 1.2+, cert=%s", self.cert_path)
  289. try:
  290. self._running = True
  291. # Wrapper to log ALL connection attempts including SSL errors
  292. async def connection_handler(reader, writer):
  293. try:
  294. addr = writer.get_extra_info("peername")
  295. ssl_obj = writer.get_extra_info("ssl_object")
  296. if ssl_obj:
  297. logger.info(
  298. f"{self._log_prefix}MQTT TLS connection from {addr} - cipher={ssl_obj.cipher()}, version={ssl_obj.version()}"
  299. )
  300. else:
  301. logger.info("%sMQTT connection from %s (no TLS?)", self._log_prefix, addr)
  302. await self._handle_client(reader, writer)
  303. except ssl.SSLError as e:
  304. logger.error("MQTT SSL error: %s", e)
  305. except Exception as e:
  306. logger.error("MQTT connection handler error: %s", e)
  307. self._server = await asyncio.start_server(
  308. connection_handler,
  309. self.bind_address,
  310. self.port,
  311. ssl=ssl_context,
  312. )
  313. self.ready.set()
  314. logger.info("Simple MQTT server listening on port %s", self.port)
  315. # Start periodic status push task
  316. self._status_push_task = asyncio.create_task(self._periodic_status_push())
  317. async with self._server:
  318. await self._server.serve_forever()
  319. except OSError as e:
  320. if e.errno == 98: # Address already in use
  321. logger.error("MQTT port %s is already in use", self.port)
  322. else:
  323. logger.error("MQTT server error: %s", e)
  324. except asyncio.CancelledError:
  325. logger.debug("MQTT server task cancelled")
  326. except Exception as e:
  327. logger.error("MQTT server error: %s", e)
  328. finally:
  329. await self.stop()
  330. async def stop(self) -> None:
  331. """Stop the MQTT server."""
  332. logger.info("Stopping simple MQTT server")
  333. self._running = False
  334. self.ready.clear()
  335. # Stop periodic status push
  336. if self._status_push_task:
  337. self._status_push_task.cancel()
  338. try:
  339. await self._status_push_task
  340. except asyncio.CancelledError:
  341. pass # Expected when stopping the periodic status push task
  342. self._status_push_task = None
  343. # Close all client connections (iterate over copy to avoid modification during iteration)
  344. for _client_id, writer in list(self._clients.items()):
  345. try:
  346. writer.close()
  347. await writer.wait_closed()
  348. except OSError:
  349. pass # Best-effort client connection cleanup; client may have disconnected
  350. self._clients.clear()
  351. self._client_serials.clear()
  352. if self._server:
  353. try:
  354. self._server.close()
  355. await self._server.wait_closed()
  356. except OSError:
  357. pass # Best-effort server shutdown; port may already be released
  358. self._server = None
  359. @staticmethod
  360. def _extract_serial_from_topic(topic: str) -> str | None:
  361. """Pull the serial out of a `device/{serial}/report|request` topic.
  362. Returns None if the topic doesn't match that shape — callers fall back
  363. to self.serial in that case.
  364. """
  365. if not topic.startswith("device/"):
  366. return None
  367. rest = topic[len("device/") :]
  368. # Expect "{serial}/report" or "{serial}/request" (possibly with suffixes).
  369. slash = rest.find("/")
  370. if slash <= 0:
  371. return None
  372. return rest[:slash]
  373. def set_bridge(self, bridge: "MQTTBridge | None") -> None:
  374. """Attach (or detach) the MQTT bridge that mirrors the target printer."""
  375. self._bridge = bridge
  376. async def _periodic_status_push(self) -> None:
  377. """Send periodic status updates to all connected clients (1 Hz, exact pre-bridge behaviour)."""
  378. logger.info("Starting periodic status push task")
  379. # Per-client push counters reset every 60 ticks. Lets us confirm from
  380. # logs whether the 1Hz push is actually reaching a specific slicer
  381. # connection (#1548 keepalive follow-up: keepalive parser shipped but
  382. # OrcaSlicer still disconnects on idle, and the periodic push is
  383. # otherwise silent at INFO level so it can't be observed in the
  384. # support bundle). One log line per minute per active connection —
  385. # nothing when no slicer is attached.
  386. push_counts: dict[str, int] = {}
  387. ticks_since_summary = 0
  388. while self._running:
  389. try:
  390. await asyncio.sleep(1) # Push every 1 second like real printers
  391. ticks_since_summary += 1
  392. disconnected = []
  393. for client_id, writer in list(self._clients.items()):
  394. try:
  395. if writer.is_closing():
  396. disconnected.append(client_id)
  397. continue
  398. serial = self._client_serials.get(client_id, self.serial)
  399. # log_event=False: the 1Hz cached push is already
  400. # captured by ``dump_wire`` snapshot mode (see
  401. # _debug.py); appending it to the cmd.jsonl would
  402. # flood the file ~60 lines/min per VP.
  403. await self._send_status_report(writer, serial=serial, log_event=False)
  404. push_counts[client_id] = push_counts.get(client_id, 0) + 1
  405. except OSError as e:
  406. logger.debug("Failed to push status to %s: %s", client_id, e)
  407. disconnected.append(client_id)
  408. # Remove disconnected clients
  409. for client_id in disconnected:
  410. self._clients.pop(client_id, None)
  411. self._client_serials.pop(client_id, None)
  412. push_counts.pop(client_id, None)
  413. if ticks_since_summary >= 60:
  414. for cid, count in push_counts.items():
  415. logger.info(
  416. "%s1Hz status push: %d pushes/min to %s",
  417. self._log_prefix,
  418. count,
  419. cid,
  420. )
  421. push_counts.clear()
  422. ticks_since_summary = 0
  423. except asyncio.CancelledError:
  424. break
  425. except Exception as e:
  426. logger.error("Periodic status push error: %s", e)
  427. logger.info("Periodic status push task stopped")
  428. async def push_raw_to_clients(self, topic: str, payload: bytes) -> None:
  429. """Publish a pre-serialized MQTT payload on `topic` to connected slicers.
  430. Called by MQTTBridge from the asyncio loop (scheduled via
  431. run_coroutine_threadsafe from paho's network thread).
  432. Routes the response only back to the originating slicer if the
  433. payload's sequence_id was previously recorded via
  434. ``_record_pending_request``. Falls back to fan-out for
  435. printer-initiated pushes (push_status etc.) and for sequence_ids
  436. we never saw (covers a slicer that subscribes mid-flight to a
  437. topic for which an earlier request is still in flight).
  438. """
  439. topic_bytes = topic.encode("utf-8")
  440. # MQTT remaining-length: 2-byte topic length prefix + topic + message body.
  441. remaining = 2 + len(topic_bytes) + len(payload)
  442. packet = bytearray([0x30]) # PUBLISH, QoS 0
  443. while True:
  444. byte = remaining % 128
  445. remaining //= 128
  446. if remaining > 0:
  447. byte |= 0x80
  448. packet.append(byte)
  449. if remaining == 0:
  450. break
  451. packet.extend([len(topic_bytes) >> 8, len(topic_bytes) & 0xFF])
  452. packet.extend(topic_bytes)
  453. packet.extend(payload)
  454. frame = bytes(packet)
  455. target_client_id = self._lookup_pending_request_client(payload)
  456. disconnected = []
  457. for client_id, writer in list(self._clients.items()):
  458. if target_client_id is not None and client_id != target_client_id:
  459. continue
  460. try:
  461. if writer.is_closing():
  462. disconnected.append(client_id)
  463. continue
  464. writer.write(frame)
  465. try:
  466. await asyncio.wait_for(writer.drain(), timeout=5)
  467. except TimeoutError:
  468. logger.debug("MQTT drain timeout pushing bridge frame to %s", client_id)
  469. except OSError as e:
  470. logger.debug("Failed to push bridge frame to %s: %s", client_id, e)
  471. disconnected.append(client_id)
  472. for client_id in disconnected:
  473. self._clients.pop(client_id, None)
  474. self._client_serials.pop(client_id, None)
  475. async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
  476. """Handle an MQTT client connection."""
  477. addr = writer.get_extra_info("peername")
  478. client_id = f"{addr[0]}:{addr[1]}" if addr else "unknown"
  479. logger.info("%sMQTT client connected: %s", self._log_prefix, client_id)
  480. authenticated = False
  481. # Per-packet read timeout. Before CONNECT we default to 60 s so a
  482. # client that opens TCP but never sends anything still gets reaped.
  483. # After CONNECT we drop the application-level read timeout entirely
  484. # and rely on TCP keepalive (SO_KEEPALIVE) to detect dead connections
  485. # — this matches real Bambu firmware, which does not enforce MQTT
  486. # spec §4.4's 1.5× idle disconnect (#1548 round 2). OrcaSlicer's
  487. # MQTT client on some platforms does not emit PINGREQ at all on idle
  488. # connections; the same install that stays connected to a real P1S
  489. # indefinitely was disconnecting from us at keepalive×1.5.
  490. read_timeout: float | None = 60.0
  491. try:
  492. while self._running:
  493. # Read MQTT fixed header
  494. try:
  495. header = await asyncio.wait_for(reader.read(1), timeout=read_timeout)
  496. except TimeoutError:
  497. break
  498. if not header:
  499. break
  500. packet_type = (header[0] & 0xF0) >> 4
  501. # Read remaining length
  502. remaining_length = await self._read_remaining_length(reader)
  503. if remaining_length is None:
  504. break
  505. # Read payload
  506. payload = await reader.read(remaining_length) if remaining_length > 0 else b""
  507. # Handle packet types
  508. if packet_type == 1: # CONNECT
  509. source_ip = addr[0] if addr else "unknown"
  510. if self._is_auth_rate_limited(source_ip):
  511. logger.warning(
  512. "%sMQTT auth rate-limited from %s (>=%d failures in %ds)",
  513. self._log_prefix,
  514. source_ip,
  515. _AUTH_RATE_LIMIT_MAX_ATTEMPTS,
  516. int(_AUTH_RATE_LIMIT_WINDOW_SECONDS),
  517. )
  518. writer.write(bytes([0x20, 0x02, 0x00, 0x05])) # Not authorized
  519. await writer.drain()
  520. break
  521. authenticated, keep_alive = await self._handle_connect(payload, writer)
  522. if not authenticated:
  523. self._record_auth_failure(source_ip)
  524. break
  525. self._clear_auth_failures(source_ip)
  526. # Drop the application-level read timeout; rely on
  527. # SO_KEEPALIVE below for dead-connection detection.
  528. # Real Bambu firmware does the same — accept any
  529. # negotiated keepalive but never enforce §4.4's 1.5×
  530. # disconnect on the otherwise-idle MQTT session
  531. # (#1548 round 2). keep_alive is logged for support
  532. # bundles but no longer drives a disconnect.
  533. read_timeout = None
  534. logger.info(
  535. "%sMQTT client %s authenticated (negotiated keepalive=%ds, idle disconnect disabled)",
  536. self._log_prefix,
  537. client_id,
  538. keep_alive,
  539. )
  540. # Enable TCP keepalive so a hard network drop is detected
  541. # by the OS within a few minutes rather than waiting for
  542. # the next outbound write to ECONNRESET.
  543. #
  544. # Also tighten the Linux keepalive schedule. Defaults are
  545. # tcp_keepalive_time=7200 s (2 h before first probe),
  546. # tcp_keepalive_intvl=75, tcp_keepalive_probes=9 — so a
  547. # macOS client that goes to sleep silently is only
  548. # detected as dead ~2 h 11 min later, and until then the
  549. # push loop keeps stalling on drain-timeouts to the
  550. # zombie socket. #1872: a P1S sleep/wake left the pre-
  551. # sleep session in _clients for 5+ min with no eviction
  552. # signal. New settings (idle=60 s, interval=15 s,
  553. # count=4) detect a dead peer in ~2 min. `getattr` guards
  554. # keep this cross-platform — macOS has TCP_KEEPINTVL but
  555. # not TCP_KEEPIDLE (uses TCP_KEEPALIVE); other platforms
  556. # silently skip.
  557. sock = writer.get_extra_info("socket")
  558. if sock is not None:
  559. try:
  560. sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
  561. except OSError as e:
  562. logger.debug("%sFailed to set SO_KEEPALIVE on %s: %s", self._log_prefix, client_id, e)
  563. for opt_name, opt_value in (
  564. ("TCP_KEEPIDLE", 60),
  565. ("TCP_KEEPINTVL", 15),
  566. ("TCP_KEEPCNT", 4),
  567. ):
  568. opt = getattr(socket, opt_name, None)
  569. if opt is None:
  570. continue
  571. try:
  572. sock.setsockopt(socket.IPPROTO_TCP, opt, opt_value)
  573. except OSError as e:
  574. logger.debug(
  575. "%sFailed to set %s=%s on %s: %s",
  576. self._log_prefix,
  577. opt_name,
  578. opt_value,
  579. client_id,
  580. e,
  581. )
  582. # Register client for periodic status pushes; start with
  583. # self.serial as the fallback until we learn the slicer's
  584. # preferred serial from the first SUBSCRIBE/PUBLISH.
  585. self._clients[client_id] = writer
  586. self._client_serials[client_id] = self.serial
  587. elif packet_type == 3: # PUBLISH
  588. if authenticated:
  589. await self._handle_publish(header[0], payload, writer, client_id)
  590. elif packet_type == 8: # SUBSCRIBE
  591. if authenticated:
  592. await self._handle_subscribe(payload, writer, client_id)
  593. elif packet_type == 12: # PINGREQ
  594. # Send PINGRESP
  595. writer.write(bytes([0xD0, 0x00]))
  596. await writer.drain()
  597. elif packet_type == 14: # DISCONNECT
  598. break
  599. except asyncio.CancelledError:
  600. pass # Expected when server is shutting down and cancels client tasks
  601. except Exception as e:
  602. # Outer handler — inner handlers already absorb expected parser
  603. # / IO failures at debug. Anything reaching here is unexpected
  604. # and would otherwise silently drop the slicer connection with
  605. # no actionable signal in production logs (defaults are INFO+).
  606. logger.warning("%sMQTT client session error from %s: %s", self._log_prefix, client_id, e)
  607. finally:
  608. logger.debug("MQTT client disconnected: %s", client_id)
  609. self._clients.pop(client_id, None)
  610. self._client_serials.pop(client_id, None)
  611. try:
  612. writer.close()
  613. await writer.wait_closed()
  614. except OSError:
  615. pass # Best-effort socket cleanup on client disconnect
  616. async def _read_remaining_length(self, reader: asyncio.StreamReader) -> int | None:
  617. """Read MQTT remaining length (variable byte integer)."""
  618. multiplier = 1
  619. value = 0
  620. for _ in range(4):
  621. try:
  622. byte = await reader.read(1)
  623. if not byte:
  624. return None
  625. encoded = byte[0]
  626. value += (encoded & 127) * multiplier
  627. if (encoded & 128) == 0:
  628. return value
  629. multiplier *= 128
  630. except OSError:
  631. return None
  632. return None
  633. def _record_pending_request(self, data: dict, client_id: str) -> None:
  634. """Stash sequence_id → client_id for any nested block with a sequence_id.
  635. Slicer commands typically wrap their seq id in ``{"print": {...}}`` or
  636. ``{"info": {...}}`` / ``{"system": {...}}`` etc. Walks top-level dict
  637. values once to find the seq id; if absent (some commands omit it) we
  638. skip — the response will fall through to broadcast which is fine for
  639. unsolicited pushes.
  640. """
  641. for block in data.values():
  642. if isinstance(block, dict):
  643. seq = block.get("sequence_id")
  644. if seq is not None:
  645. key = str(seq)
  646. # Evict oldest entry when over the cap. Python dicts
  647. # preserve insertion order so iter(self._pending_requests)
  648. # yields the oldest key first.
  649. while len(self._pending_requests) >= _PENDING_REQUEST_MAX_ENTRIES:
  650. oldest = next(iter(self._pending_requests))
  651. self._pending_requests.pop(oldest, None)
  652. self._pending_requests[key] = client_id
  653. return
  654. def _lookup_pending_request_client(self, payload: bytes) -> str | None:
  655. """Parse a bridge-forwarded MQTT payload and return the originating
  656. client_id if its sequence_id was recorded.
  657. Returns ``None`` for printer-initiated pushes (no recorded seq id) so
  658. push_raw_to_clients falls back to broadcast — required for push_status
  659. and the other unsolicited pushes that every connected slicer expects.
  660. """
  661. try:
  662. parsed = json.loads(payload)
  663. except (ValueError, TypeError):
  664. return None
  665. if not isinstance(parsed, dict):
  666. return None
  667. for block in parsed.values():
  668. if isinstance(block, dict):
  669. seq = block.get("sequence_id")
  670. if seq is not None:
  671. return self._pending_requests.pop(str(seq), None)
  672. return None
  673. def _is_auth_rate_limited(self, source_ip: str) -> bool:
  674. """Return True if ``source_ip`` has hit the per-IP failure cap.
  675. Prunes timestamps older than the window so the dict doesn't grow
  676. unbounded. Uses ``time.monotonic()`` for a wall-clock-jump-immune
  677. clock that's safe to call from any context (sync or async).
  678. """
  679. import time as _time
  680. now = _time.monotonic()
  681. window_start = now - _AUTH_RATE_LIMIT_WINDOW_SECONDS
  682. recent = [t for t in self._auth_failures.get(source_ip, []) if t >= window_start]
  683. if recent:
  684. self._auth_failures[source_ip] = recent
  685. else:
  686. self._auth_failures.pop(source_ip, None)
  687. return len(recent) >= _AUTH_RATE_LIMIT_MAX_ATTEMPTS
  688. def _record_auth_failure(self, source_ip: str) -> None:
  689. """Append a timestamp for ``source_ip``'s failed auth attempt."""
  690. import time as _time
  691. now = _time.monotonic()
  692. self._auth_failures.setdefault(source_ip, []).append(now)
  693. def _clear_auth_failures(self, source_ip: str) -> None:
  694. """Reset ``source_ip``'s failure history after a successful auth."""
  695. self._auth_failures.pop(source_ip, None)
  696. async def _handle_connect(self, payload: bytes, writer: asyncio.StreamWriter) -> tuple[bool, int]:
  697. """Handle MQTT CONNECT packet.
  698. Returns ``(authenticated, keep_alive_seconds)`` — the second element
  699. is the value the client advertised in CONNECT, so the caller's
  700. read-loop can honour it instead of the hardcoded default. ``0``
  701. means the client opted out of keepalive (#1548).
  702. """
  703. try:
  704. # Parse CONNECT packet
  705. # Skip protocol name length and name
  706. idx = 0
  707. proto_len = (payload[idx] << 8) | payload[idx + 1]
  708. idx += 2 + proto_len
  709. # Skip protocol level and connect flags
  710. # connect_flags = payload[idx + 1]
  711. idx += 2
  712. # Keepalive (2-byte big-endian, seconds). Honoured by the read
  713. # loop in `_handle_client` per MQTT spec §3.1.2.10 / §4.4 —
  714. # before #1548 we ignored this and used a hardcoded 60 s, which
  715. # closed OrcaSlicer's idle connection at exactly the negotiated
  716. # keepalive boundary instead of the spec-mandated 1.5×.
  717. keep_alive = (payload[idx] << 8) | payload[idx + 1]
  718. idx += 2
  719. # Read client ID
  720. client_id_len = (payload[idx] << 8) | payload[idx + 1]
  721. idx += 2
  722. # client_id = payload[idx : idx + client_id_len].decode("utf-8")
  723. idx += client_id_len
  724. # Read username
  725. username_len = (payload[idx] << 8) | payload[idx + 1]
  726. idx += 2
  727. username = payload[idx : idx + username_len].decode("utf-8")
  728. idx += username_len
  729. # Read password
  730. password_len = (payload[idx] << 8) | payload[idx + 1]
  731. idx += 2
  732. password = payload[idx : idx + password_len].decode("utf-8")
  733. # Authenticate. ``hmac.compare_digest`` is constant-time to keep
  734. # the auth check from leaking the access code via response timing
  735. # under network jitter — LAN-only threat is marginal, but it's
  736. # the standard fix and costs nothing.
  737. if username == "bblp" and hmac.compare_digest(password, self.access_code):
  738. # Send CONNACK with success
  739. writer.write(bytes([0x20, 0x02, 0x00, 0x00]))
  740. await writer.drain()
  741. logger.info("%sMQTT client authenticated successfully", self._log_prefix)
  742. # Send immediate status report after auth - slicer expects this
  743. await self._send_status_report(writer)
  744. return True, keep_alive
  745. else:
  746. # Send CONNACK with auth failure
  747. writer.write(bytes([0x20, 0x02, 0x00, 0x05])) # Not authorized
  748. await writer.drain()
  749. logger.warning("%sMQTT auth failed for user '%s' (access code mismatch)", self._log_prefix, username)
  750. return False, 0
  751. except (IndexError, ValueError) as e:
  752. logger.debug("MQTT CONNECT parse error: %s", e)
  753. # Send CONNACK with error
  754. writer.write(bytes([0x20, 0x02, 0x00, 0x02])) # Protocol error
  755. await writer.drain()
  756. return False, 0
  757. async def _handle_subscribe(self, payload: bytes, writer: asyncio.StreamWriter, client_id: str) -> None:
  758. """Handle MQTT SUBSCRIBE packet."""
  759. try:
  760. # Parse packet ID
  761. packet_id = (payload[0] << 8) | payload[1]
  762. # Parse topic filters (just acknowledge them)
  763. idx = 2
  764. granted_qos = []
  765. learned_serial: str | None = None
  766. while idx < len(payload):
  767. topic_len = (payload[idx] << 8) | payload[idx + 1]
  768. idx += 2
  769. topic = payload[idx : idx + topic_len].decode("utf-8")
  770. idx += topic_len
  771. requested_qos = payload[idx]
  772. idx += 1
  773. logger.info("%sMQTT subscribe: %s QoS=%s", self._log_prefix, topic, requested_qos)
  774. granted_qos.append(min(requested_qos, 1)) # Grant up to QoS 1
  775. # Remember the serial the slicer is listening on so status/version
  776. # responses go to a topic it actually subscribed to.
  777. if learned_serial is None:
  778. extracted = self._extract_serial_from_topic(topic)
  779. if extracted:
  780. learned_serial = extracted
  781. if learned_serial and learned_serial != self._client_serials.get(client_id):
  782. if learned_serial != self.serial:
  783. logger.info(
  784. "%sMQTT client subscribed with serial %s (VP serial is %s) — adapting responses",
  785. self._log_prefix,
  786. learned_serial,
  787. self.serial,
  788. )
  789. self._client_serials[client_id] = learned_serial
  790. # Send SUBACK
  791. suback = bytes([0x90, 2 + len(granted_qos), packet_id >> 8, packet_id & 0xFF])
  792. suback += bytes(granted_qos)
  793. writer.write(suback)
  794. await writer.drain()
  795. # Send initial status report after subscribe on the client's subscribed topic
  796. await self._send_status_report(writer, serial=self._client_serials.get(client_id, self.serial))
  797. except (IndexError, ValueError, OSError) as e:
  798. logger.debug("MQTT SUBSCRIBE error: %s", e)
  799. async def _send_status_report(
  800. self, writer: asyncio.StreamWriter, serial: str | None = None, log_event: bool = True
  801. ) -> None:
  802. """Send a status report to the slicer after connection.
  803. When a bridge is active and has cached the real printer's latest
  804. push_status, send a copy of the real push with only the upload-state-
  805. machine fields we own (gcode_state, gcode_file, prepare_percent,
  806. subtask_name) overridden. BambuStudio's Send pre-flight checks the
  807. push_status shape against what it expects from the printer model, and
  808. the synthetic stub introduced fields the real H2D doesn't have (storage,
  809. the wrong chamber_temper shape, etc.) which trip the check.
  810. While the target printer is actually printing and the VP has no upload
  811. handshake of its own in flight, the live-progress fields are mirrored
  812. through instead of zeroed, under a forced gcode_state=FINISH — see
  813. ``_mirroring_live_progress`` for why that specific state (#1887).
  814. """
  815. try:
  816. self._sequence_id += 1
  817. cached = self._bridge.get_latest_print_state() if self._bridge is not None else None
  818. if isinstance(cached, dict):
  819. # Real-printer-shaped response. Copy the cache, then replace the
  820. # protocol / upload-state fields with values under our control.
  821. # Deep copy — current mutations are top-level only, but a future
  822. # override that writes into a nested dict (e.g. ``online``,
  823. # ``upgrade_state``, ``ipcam``) would otherwise corrupt the
  824. # bridge cache and be read by every subsequent subscriber until
  825. # the next real-printer push lands. Cost is one allocation per
  826. # status report; the cached dict is already short-lived.
  827. print_block = copy.deepcopy(cached)
  828. print_block["sequence_id"] = str(self._sequence_id)
  829. print_block["command"] = "push_status"
  830. print_block["msg"] = 0
  831. mirroring = self._mirroring_live_progress(cached)
  832. if mirroring:
  833. # gcode_file / subtask_name / the progress fields stay as the
  834. # printer reported them — the slicer renders what is really
  835. # on the bed. FINISH keeps the Send button enabled.
  836. print_block["gcode_state"] = "FINISH"
  837. print_block["gcode_file_prepare_percent"] = "100"
  838. print_block.setdefault("subtask_name", "")
  839. else:
  840. print_block["gcode_state"] = self._gcode_state
  841. print_block["gcode_file"] = self._current_file
  842. print_block["gcode_file_prepare_percent"] = self._prepare_percent
  843. if self._current_file:
  844. print_block["subtask_name"] = self._current_file.replace(".3mf", "")
  845. else:
  846. # Don't override real subtask_name with empty if no upload pending.
  847. print_block.setdefault("subtask_name", "")
  848. # Storage-availability indicators the slicer's "Send" pre-flight reads
  849. # (#1228). P1S/A1-class firmware doesn't always include these in
  850. # push_status (no SD card inserted, older field shapes), and BambuStudio
  851. # rejects the send pre-flight with the generic "storage needs to be
  852. # inserted before send to printer" error before even attempting FTP.
  853. # For VP usage the slicer uploads via FTPS to Bambuddy's filesystem —
  854. # the printer's actual SD/storage state is irrelevant on that path.
  855. # Force "available" indicators so the pre-flight passes regardless of
  856. # what the real printer reports. Restores the 0.2.3.2 synthetic-stub
  857. # behaviour for these fields without losing the live AMS / k-profile /
  858. # camera mirror cached-as-base provides.
  859. print_block["home_flag"] = print_block.get("home_flag", 0) | 0x100 # bit 8 = HAS_SDCARD_NORMAL
  860. print_block["sdcard"] = True
  861. print_block.setdefault("storage", {"free": 1_000_000_000, "total": 32_000_000_000})
  862. # Live-progress fields the slicer's Send pre-flight reads
  863. # (#1558). When the VP reports itself idle, these have to read
  864. # idle too: the cached push_status carries the printer's real
  865. # values, and a report that says gcode_state=IDLE while
  866. # mc_percent>0 / stg_cur>0 is internally contradictory — the
  867. # slicer takes it as busy and blocks Send. Same shape as #1228
  868. # (storage). When the mirror is on, gcode_state=FINISH agrees
  869. # with a non-zero progress set, so they pass through instead.
  870. if not mirroring:
  871. print_block["mc_print_stage"] = ""
  872. print_block["mc_percent"] = 0
  873. print_block["mc_remaining_time"] = 0
  874. print_block["stg"] = []
  875. print_block["stg_cur"] = 0
  876. print_block["layer_num"] = 0
  877. print_block["total_layer_num"] = 0
  878. # print_error is never mirrored: StatusPanel raises a modal error
  879. # dialog for a non-zero code, and the VP is not the machine that
  880. # threw it — the user's own printer card in Bambuddy reports the
  881. # fault. Zero it in both branches.
  882. print_block["print_error"] = 0
  883. status = {"print": print_block}
  884. dump_wire(self.vp_name, "out", status)
  885. await self._publish_to_report(writer, status, serial or self.serial, log_event=log_event)
  886. return
  887. # No bridge / no cache yet — fall back to the synthetic stub.
  888. status = {
  889. "print": {
  890. "sequence_id": str(self._sequence_id),
  891. "command": "push_status",
  892. "msg": 0,
  893. "gcode_state": self._gcode_state,
  894. "gcode_file": self._current_file,
  895. "gcode_file_prepare_percent": self._prepare_percent,
  896. "subtask_name": self._current_file.replace(".3mf", "") if self._current_file else "",
  897. "mc_print_stage": "",
  898. "mc_percent": 0,
  899. "mc_remaining_time": 0,
  900. "wifi_signal": "-44dBm",
  901. "print_error": 0,
  902. "print_type": "",
  903. "bed_temper": 25.0,
  904. "bed_target_temper": 0.0,
  905. "nozzle_temper": 25.0,
  906. "nozzle_target_temper": 0.0,
  907. "chamber_temper": 25.0,
  908. "cooling_fan_speed": "0",
  909. "big_fan1_speed": "0",
  910. "big_fan2_speed": "0",
  911. "heatbreak_fan_speed": "0",
  912. "spd_lvl": 1,
  913. "spd_mag": 100,
  914. "stg": [],
  915. "stg_cur": 0,
  916. "layer_num": 0,
  917. "total_layer_num": 0,
  918. "home_flag": 256, # Bit 8 = SD card present (HAS_SDCARD_NORMAL)
  919. "hw_switch_state": 0,
  920. "online": {"ahb": False, "rfid": False, "version": 7},
  921. "ams_status": 0,
  922. "sdcard": True,
  923. "storage": {"free": 1000000000, "total": 32000000000},
  924. "upgrade_state": {
  925. "sequence_id": 0,
  926. "progress": "",
  927. "status": "",
  928. "consistency_request": False,
  929. "dis_state": 0,
  930. "err_code": 0,
  931. "force_upgrade": False,
  932. "message": "",
  933. "module": "",
  934. "new_version_state": 2,
  935. "new_ver_list": [],
  936. "ota_new_version_number": "",
  937. "ahb_new_version_number": "",
  938. },
  939. "ipcam": {
  940. "ipcam_dev": "1",
  941. "ipcam_record": "enable",
  942. "timelapse": "disable",
  943. "resolution": "1080p",
  944. "mode_bits": 0,
  945. },
  946. "xcam": {
  947. "allow_skip_parts": False,
  948. "buildplate_marker_detector": True,
  949. "first_layer_inspector": True,
  950. "halt_print_sensitivity": "medium",
  951. "print_halt": True,
  952. "printing_monitor": True,
  953. "spaghetti_detector": True,
  954. },
  955. "lights_report": [{"node": "chamber_light", "mode": "on"}],
  956. "nozzle_diameter": "0.4",
  957. "nozzle_type": "hardened_steel",
  958. }
  959. }
  960. await self._publish_to_report(writer, status, serial or self.serial, log_event=log_event)
  961. except OSError as e:
  962. logger.error("Failed to send status report: %s", e)
  963. async def _send_version_response(
  964. self, writer: asyncio.StreamWriter, sequence_id: str, serial: str | None = None
  965. ) -> None:
  966. """Send version info response to the slicer."""
  967. try:
  968. product_name = MODEL_PRODUCT_NAMES.get(self.model, self.model or "X1 Carbon")
  969. # The serial is embedded inside the module[].sn fields *and* used as
  970. # the report topic. Use the client's effective serial so the slicer
  971. # sees internal/topic consistency even when it differs from self.serial.
  972. serial = serial or self.serial
  973. # Build version response matching OrcaSlicer expectations
  974. # Required fields per module: name, product_name, sw_ver, sw_new_ver, sn, hw_ver, flag
  975. version_info = {
  976. "info": {
  977. "command": "get_version",
  978. "sequence_id": sequence_id,
  979. "module": [
  980. {
  981. "name": "ota",
  982. "product_name": product_name,
  983. "sw_ver": "01.07.00.00",
  984. "sw_new_ver": "",
  985. "hw_ver": "OTA",
  986. "sn": serial,
  987. "flag": 0,
  988. },
  989. {
  990. "name": "esp32",
  991. "product_name": product_name,
  992. "sw_ver": "01.07.22.25",
  993. "sw_new_ver": "",
  994. "hw_ver": "AP05",
  995. "sn": serial,
  996. "flag": 0,
  997. },
  998. {
  999. "name": "rv1126",
  1000. "product_name": product_name,
  1001. "sw_ver": "00.00.27.38",
  1002. "sw_new_ver": "",
  1003. "hw_ver": "AP05",
  1004. "sn": serial,
  1005. "flag": 0,
  1006. },
  1007. {
  1008. "name": "th",
  1009. "product_name": product_name,
  1010. "sw_ver": "00.00.04.00",
  1011. "sw_new_ver": "",
  1012. "hw_ver": "TH07",
  1013. "sn": serial,
  1014. "flag": 0,
  1015. },
  1016. {
  1017. "name": "mc",
  1018. "product_name": product_name,
  1019. "sw_ver": "00.00.10.00",
  1020. "sw_new_ver": "",
  1021. "hw_ver": "MC07",
  1022. "sn": serial,
  1023. "flag": 0,
  1024. },
  1025. ],
  1026. }
  1027. }
  1028. # Overlay real version modules from the bridge cache when available
  1029. # (specifically the AMS modules ams/0, n3f/0, n3s/128 etc. that
  1030. # BambuStudio's Prepare tab uses to identify AMS hardware — without
  1031. # them every AMS unit shows as "unknown" in the Prepare panel).
  1032. if self._bridge is not None:
  1033. cached_modules = self._bridge.get_latest_version_modules()
  1034. if isinstance(cached_modules, list) and cached_modules:
  1035. version_info["info"]["module"] = cached_modules
  1036. await self._publish_to_report(writer, version_info, serial)
  1037. logger.info("Sent version response (product_name=%s)", product_name)
  1038. except OSError as e:
  1039. logger.error("Failed to send version response: %s", e)
  1040. def set_gcode_state(self, state: str, filename: str = "", prepare_percent: str = "0") -> None:
  1041. """Update the gcode state reported to connected slicers.
  1042. Called by the manager to reflect FTP upload progress/completion.
  1043. """
  1044. self._gcode_state = state
  1045. self._current_file = filename
  1046. self._prepare_percent = prepare_percent
  1047. self._state_changed_at = time.monotonic()
  1048. def _mirroring_live_progress(self, cached: dict) -> bool:
  1049. """True when the report should carry the target printer's live progress.
  1050. The slicers gate the Device-tab progress panel and the Send button on
  1051. the same predicate (``MachineObject::is_in_printing()``), so the VP
  1052. cannot report the printer's real gcode_state without also telling the
  1053. slicer it is too busy to accept a job — which is the whole point of a
  1054. non-proxy VP, and was the #1558 regression. Reporting FINISH instead
  1055. renders the panel (StatusPanel checks ``is_in_printing() ||
  1056. print_status == "FINISH"``) and leaves Send enabled, so the mirror is
  1057. FINISH plus the printer's real numbers.
  1058. Two things suppress it:
  1059. * The VP's own upload state machine owns the report while a job is
  1060. being handed over (PREPARE), and for a short settle window after —
  1061. the slicer only releases its in-flight-job lock once it sees FINISH
  1062. carrying the ``subtask_name`` it just uploaded (#1280 / #1658), and
  1063. swapping in the printer's filename mid-handshake wedges the send
  1064. modal at "Downloading".
  1065. * The printer isn't printing, in which case there is no progress to
  1066. show and the VP's own state is the honest thing to report.
  1067. """
  1068. if self._gcode_state == "PREPARE":
  1069. return False
  1070. if time.monotonic() - self._state_changed_at < _UPLOAD_SETTLE_SECONDS:
  1071. return False
  1072. return str(cached.get("gcode_state") or "").upper() in _MIRRORED_PRINT_STATES
  1073. async def _publish_to_report(
  1074. self, writer: asyncio.StreamWriter, payload: dict, serial: str = "", log_event: bool = True
  1075. ) -> None:
  1076. """Publish a message on the device report topic.
  1077. Real Bambu printers wire-format push_status JSON with 4-space indentation
  1078. (32254 bytes for an idle H2D push vs 14268 bytes compact). BambuStudio's
  1079. Send pre-flight rejects compact JSON — without matching the on-wire
  1080. format the slicer never proceeds to FTP upload.
  1081. ``log_event=True`` records the publish in ``vp_wire/<vp>_cmd.jsonl``
  1082. under the ``bridge_to_slicer`` direction so #1622-style triages can
  1083. diff the bridge's own outbound replies (info.get_version answer,
  1084. project_file ack, on-demand pushall response) against the real
  1085. printer's ``printer_to_slicer`` forwards. The 1Hz periodic push
  1086. sets ``log_event=False`` because dump_wire's overwrite-snapshot
  1087. already covers cache shape and a per-second JSONL line would dwarf
  1088. the actual command events.
  1089. """
  1090. topic = f"device/{serial or self.serial}/report"
  1091. message = json.dumps(payload, indent=4)
  1092. topic_bytes = topic.encode("utf-8")
  1093. message_bytes = message.encode("utf-8")
  1094. remaining = 2 + len(topic_bytes) + len(message_bytes)
  1095. packet = bytes([0x30]) # PUBLISH, QoS 0
  1096. while remaining > 0:
  1097. byte = remaining % 128
  1098. remaining //= 128
  1099. if remaining > 0:
  1100. byte |= 0x80
  1101. packet += bytes([byte])
  1102. packet += bytes([len(topic_bytes) >> 8, len(topic_bytes) & 0xFF])
  1103. packet += topic_bytes
  1104. packet += message_bytes
  1105. if log_event:
  1106. # Env-flagged command trace (#1622): captures bridge-synthesised
  1107. # replies (info.get_version, project_file ack, on-demand pushall
  1108. # response) AFTER the payload is finalised but before it hits
  1109. # the wire — so the cmd.jsonl reflects exactly what the slicer
  1110. # parses. Pair with the slicer_to_bridge events from
  1111. # _handle_publish and the printer_to_slicer fan-outs from
  1112. # mqtt_bridge.
  1113. append_event(self.vp_name, "bridge_to_slicer", topic, payload)
  1114. writer.write(packet)
  1115. # Timeout the drain to prevent blocking the event loop if the
  1116. # MQTT client stops reading (e.g. slicer busy with FTP upload,
  1117. # macOS suspends the client mid-session — #1872).
  1118. #
  1119. # On timeout, close the writer and raise BrokenPipeError so the
  1120. # push-loop's ``except OSError`` at ``_periodic_status_push``
  1121. # evicts the client from ``self._clients`` on this same tick.
  1122. # Before this, timeouts logged at DEBUG and returned silently,
  1123. # so the zombie writer sat in ``self._clients`` until SO_KEEPALIVE
  1124. # detected the dead peer (~2 h on Linux defaults). That kept the
  1125. # push loop spending 5 s per iteration on the stalled client and
  1126. # left the slicer's UI unaware the session was gone.
  1127. try:
  1128. await asyncio.wait_for(writer.drain(), timeout=5)
  1129. except TimeoutError as e:
  1130. logger.info(
  1131. "%sMQTT drain timeout for %s — closing stalled writer",
  1132. self._log_prefix,
  1133. topic,
  1134. )
  1135. try:
  1136. writer.close()
  1137. except Exception:
  1138. pass # best-effort — writer may already be broken
  1139. raise BrokenPipeError(f"drain timeout on {topic}") from e
  1140. async def _send_print_response(
  1141. self, writer: asyncio.StreamWriter, sequence_id: str, filename: str, serial: str | None = None
  1142. ) -> None:
  1143. """Send project_file acknowledgment matching real Bambu printer behavior."""
  1144. # Update state so periodic status pushes reflect preparation. Goes
  1145. # through set_gcode_state so the live-progress mirror sees the
  1146. # transition and holds off until the upload handshake has settled.
  1147. self.set_gcode_state("PREPARE", filename=filename, prepare_percent="0")
  1148. try:
  1149. # Send command acknowledgment — slicer expects to see
  1150. # command: "project_file" echoed back before starting FTP upload
  1151. subtask_name = filename.replace(".3mf", "") if filename else ""
  1152. response = {
  1153. "print": {
  1154. "command": "project_file",
  1155. "sequence_id": sequence_id,
  1156. "param": "Metadata/plate_1.gcode",
  1157. "subtask_name": subtask_name,
  1158. "gcode_state": "PREPARE",
  1159. "gcode_file": filename,
  1160. "gcode_file_prepare_percent": "0",
  1161. "result": "SUCCESS",
  1162. "msg": 0,
  1163. }
  1164. }
  1165. await self._publish_to_report(writer, response, serial or self.serial)
  1166. logger.info("Sent project_file acknowledgment for %s", filename)
  1167. except OSError as e:
  1168. logger.error("Failed to send print response: %s", e)
  1169. async def _handle_publish(self, header: int, payload: bytes, writer: asyncio.StreamWriter, client_id: str) -> None:
  1170. """Handle MQTT PUBLISH packet."""
  1171. try:
  1172. # Parse topic
  1173. idx = 0
  1174. topic_len = (payload[idx] << 8) | payload[idx + 1]
  1175. idx += 2
  1176. topic = payload[idx : idx + topic_len].decode("utf-8")
  1177. idx += topic_len
  1178. # Check for packet ID (QoS > 0)
  1179. qos = (header & 0x06) >> 1
  1180. if qos > 0:
  1181. # packet_id = (payload[idx] << 8) | payload[idx + 1]
  1182. idx += 2
  1183. # Parse message
  1184. message = payload[idx:].decode("utf-8")
  1185. logger.info("MQTT publish to %s: %s...", topic, message[:100])
  1186. # Only handle publishes on *some* device/.../request topic. The
  1187. # serial is taken from the topic rather than compared against
  1188. # self.serial: the client is already authenticated via the access
  1189. # code, and Orca/BambuStudio may have a cached serial that differs
  1190. # from the VP's computed self.serial (#927). Use the topic's serial
  1191. # for all responses so they land on the topic the slicer subscribed
  1192. # to.
  1193. if not topic.startswith("device/") or "/request" not in topic:
  1194. return
  1195. client_serial = self._extract_serial_from_topic(topic) or self.serial
  1196. if client_serial and client_serial != self._client_serials.get(client_id):
  1197. if client_serial != self.serial:
  1198. logger.info(
  1199. "%sMQTT client publishing with serial %s (VP serial is %s) — adapting responses",
  1200. self._log_prefix,
  1201. client_serial,
  1202. self.serial,
  1203. )
  1204. self._client_serials[client_id] = client_serial
  1205. try:
  1206. # Some slicer builds (observed with OrcaSlicer on Linux, #927)
  1207. # include the C-string null terminator in the MQTT payload
  1208. # length, so the decoded message ends with \x00. Real brokers
  1209. # pass the bytes through; strict json.loads raises "Extra data"
  1210. # and every pushall/get_version/project_file silently dropped.
  1211. data = json.loads(message.rstrip("\x00 \r\n\t"))
  1212. except json.JSONDecodeError as e:
  1213. logger.debug(
  1214. "MQTT publish JSON decode failed: %s (payload=%r)",
  1215. e,
  1216. message[:200],
  1217. )
  1218. return
  1219. # Env-flagged command trace (#1622): every slicer-originated publish
  1220. # gets a line in vp_wire/<vp>_cmd.jsonl alongside the printer-side
  1221. # responses captured in mqtt_bridge. Off by default.
  1222. append_event(self.vp_name, "slicer_to_bridge", topic, data)
  1223. # The synthetic flow below is the original (pre-bridge) behaviour and is
  1224. # what the proven-working FTP "Send" depends on. Do NOT replace any
  1225. # synthetic response with a forward — only ADD forwarding alongside,
  1226. # at the bottom, for commands the synthetic flow doesn't handle
  1227. # (AMS write / xcam / system / etc., which need to actually reach
  1228. # the real printer).
  1229. handled_locally = False
  1230. # Handle pushing command (status request)
  1231. if "pushing" in data:
  1232. pushing_data = data["pushing"]
  1233. command = pushing_data.get("command", "")
  1234. logger.info("MQTT pushing command: %s", command)
  1235. if command == "pushall":
  1236. logger.info("Sending status report in response to pushall")
  1237. await self._send_status_report(writer, serial=client_serial)
  1238. handled_locally = True
  1239. elif command == "start":
  1240. logger.info("Starting status push stream")
  1241. await self._send_status_report(writer, serial=client_serial)
  1242. handled_locally = True
  1243. # Handle info commands (get_version, etc.)
  1244. if "info" in data:
  1245. info_data = data["info"]
  1246. command = info_data.get("command", "")
  1247. sequence_id = info_data.get("sequence_id", "0")
  1248. logger.info("MQTT info command: %s", command)
  1249. if command == "get_version":
  1250. await self._send_version_response(writer, sequence_id, serial=client_serial)
  1251. handled_locally = True
  1252. # Handle print commands
  1253. if "print" in data:
  1254. print_data = data["print"]
  1255. command = print_data.get("command", "")
  1256. filename = print_data.get("subtask_name", "")
  1257. sequence_id = print_data.get("sequence_id", "0")
  1258. logger.info("MQTT print command: %s for %s", command, filename)
  1259. if command in ("project_file", "gcode_file"):
  1260. # File lives on Bambuddy, not the printer — synthetic only.
  1261. file_3mf = print_data.get("file", filename)
  1262. await self._send_print_response(writer, sequence_id, file_3mf, serial=client_serial)
  1263. if self.on_print_command:
  1264. # `filename` is the slicer's `subtask_name` (bare model
  1265. # name, no extension). Pass it through verbatim — the
  1266. # `_schedule_finish_release` chain echoes it back as
  1267. # gcode_file + subtask_name in push_status, and the
  1268. # slicer matches against its own subtask_name there.
  1269. # The FTP filename (with extension) is in print_data
  1270. # under "file" for the queue-stash side to use as its
  1271. # own key matching `_add_to_print_queue`'s lookup.
  1272. await self._notify_print_command(filename, print_data)
  1273. handled_locally = True
  1274. # Forward anything the synthetic flow didn't handle to the real
  1275. # printer. AMS load / dry / xcam / system / extrusion_cali_get etc.
  1276. if not handled_locally and self._bridge is not None and self._bridge.is_active:
  1277. # Remember which client originated this command so the
  1278. # printer's response goes back only to them (not fanned
  1279. # out to every connected slicer).
  1280. self._record_pending_request(data, client_id)
  1281. self._bridge.forward_to_printer(data)
  1282. except (IndexError, ValueError, OSError) as e:
  1283. logger.debug("MQTT PUBLISH error: %s", e)
  1284. async def _notify_print_command(self, filename: str, data: dict) -> None:
  1285. """Notify callback of print command."""
  1286. if self.on_print_command:
  1287. try:
  1288. result = self.on_print_command(filename, data)
  1289. if asyncio.iscoroutine(result):
  1290. await result
  1291. except Exception as e:
  1292. logger.error("Print command callback error: %s", e)