ibm5250 0.1.0.dev1__tar.gz → 0.1.0.dev2__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
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: ibm5250
3
- Version: 0.1.0.dev1
3
+ Version: 0.1.0.dev2
4
4
  Summary: IBM 5250 terminal automation library
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.11
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ibm5250"
3
- version = "0.1.0.dev1"
3
+ version = "0.1.0.dev2"
4
4
  description = "IBM 5250 terminal automation library"
5
5
  requires-python = ">=3.11"
6
6
 
@@ -52,6 +52,19 @@ _VAR_HDR_OP_HI = 0x08 # TN5250E variable-header opcode byte 0
52
52
  _VAR_HDR_OP_LO = 0x2C # TN5250E variable-header opcode byte 1
53
53
  _SAVE_SCREEN_RESP_CC1 = 0x01 # CC1 value in the Save Screen response variable header
54
54
 
55
+ # TN5250E header byte 7 (request/response flags). Real emulators set bit 0x40
56
+ # here to signal the Attention (Attn) key -- a header-only INPUT record with no
57
+ # AID and no variable header. Observed on the wire when the PC "Esc" key (mapped
58
+ # to Attn) is pressed: 00 0a 12 a0 00 00 04 40 00 00.
59
+ _TN5250E_ATTN_FLAG = 0x40
60
+
61
+ # TN5250E header byte 9 operation code (RFC 2877 §4.3). After the workstation
62
+ # sends Attn, the host cancels its outstanding read invite by sending a
63
+ # header-only Cancel-Invite record (opcode 0x0A, no payload). The workstation
64
+ # must acknowledge it by echoing the same Cancel-Invite record back before the
65
+ # host will run its Attention program (e.g. the System Request / Assist menu).
66
+ _TN5250E_OP_CANCEL_INVITE = 0x0A
67
+
55
68
  _MAX_NEG_PASSES = 40 # Maximum Telnet option exchange iterations during negotiation
56
69
 
57
70
 
@@ -244,13 +257,61 @@ class TN5250Connection:
244
257
 
245
258
  frame += bytes([TelnetCmd.INTERPRET_AS_COMMAND, TelnetCmd.END_OF_RECORD])
246
259
 
260
+ self._send_frame(frame, description=f"→ {len(data)} payload bytes: {data.hex()}")
261
+
262
+ def _send_frame(self, frame: bytes, *, description: str) -> None:
263
+ """Send a fully-framed (IAC-EOR terminated) record under the send lock."""
264
+ if self._sock is None:
265
+ raise ConnectionError("Not connected")
266
+ log.debug(description)
247
267
  with self._lock:
248
268
  try:
249
269
  self._sock.sendall(frame)
250
270
  except OSError as exc:
251
271
  raise ConnectionError(f"Send failed: {exc}") from exc
252
272
 
253
- log.debug("→ %d payload bytes: %s", len(data), data.hex())
273
+ def send_attn(self) -> None:
274
+ """Send the Attention (Attn) signal to the host.
275
+
276
+ Attn is *not* an AID key. In TN5250E it is a header-only INPUT record
277
+ whose byte 7 carries the Attn flag (``0x40``) with no variable header
278
+ and no payload -- exactly what emulators emit when the PC "Esc" key
279
+ (mapped to Attn) is pressed. On the AS/400 this typically raises the
280
+ System Request / Assist menu. In basic (non-TN5250E) mode the Attn key
281
+ is signalled with a Telnet BREAK (RFC 1205).
282
+ """
283
+ if not self._tn5250e_active:
284
+ # Basic TN5250: the Attn key is signalled with a Telnet BREAK.
285
+ frame = bytes(
286
+ [
287
+ TelnetCmd.INTERPRET_AS_COMMAND,
288
+ TelnetCmd.BREAK,
289
+ TelnetCmd.INTERPRET_AS_COMMAND,
290
+ TelnetCmd.END_OF_RECORD,
291
+ ]
292
+ )
293
+ else:
294
+ # Header-only INPUT record: byte 7 carries the Attn flag.
295
+ frame = _build_tn5250e_frame(req_resp=_TN5250E_ATTN_FLAG, opcode=0x00)
296
+
297
+ self._send_frame(frame, description="Sending Attn signal")
298
+
299
+ def send_cancel_invite_response(self) -> None:
300
+ """Acknowledge a host Cancel-Invite by echoing it back.
301
+
302
+ After the workstation sends Attn, the host replies with a header-only
303
+ Cancel-Invite record (opcode ``0x0A``, no payload) to cancel its
304
+ outstanding read invite. The workstation must echo the identical record
305
+ back; only then does the host run its Attention program (which raises,
306
+ for example, the System Request / Assist menu). Without this ack the
307
+ host stalls and never sends the follow-up screen.
308
+ """
309
+ if not self._tn5250e_active:
310
+ return # cancel-invite handshake only occurs in TN5250E mode
311
+
312
+ # Header-only INPUT record: byte 9 opcode = Cancel-Invite, no payload.
313
+ frame = _build_tn5250e_frame(req_resp=0x00, opcode=_TN5250E_OP_CANCEL_INVITE)
314
+ self._send_frame(frame, description="Sending Cancel-Invite response")
254
315
 
255
316
  def send_save_screen_response(self) -> None:
256
317
  """Send the TN5250E-level Save Screen acknowledgment.
@@ -261,42 +322,16 @@ class TN5250Connection:
261
322
  send). This is a protocol-level exchange with no 5250 data stream
262
323
  payload.
263
324
  """
264
- if self._sock is None:
265
- raise ConnectionError("Not connected")
266
325
  if not self._tn5250e_active:
267
326
  return # save/restore handshake only occurs in TN5250E mode
268
327
 
269
- # 4-byte variable header: [ESC, RESTORE_SCREEN, CC1, CC2.RESET_MDT]
328
+ # 4-byte variable header: [ESC, RESTORE_SCREEN, CC1, CC2.RESET_MDT].
329
+ # byte 9 of the fixed header carries the variable-header length.
270
330
  var_hdr = bytes(
271
331
  [Command.ESC, Command.RESTORE_SCREEN, _SAVE_SCREEN_RESP_CC1, CC2.RESET_MDT]
272
332
  )
273
- total = _TN5250E_HEADER_LEN + len(var_hdr) # 14
274
- frame = (
275
- bytes(
276
- [
277
- (total >> 8) & 0xFF,
278
- total & 0xFF, # record length
279
- _GDS_RECORD_ID_HI,
280
- _GDS_RECORD_ID_LO, # GDS identifier
281
- 0x00,
282
- 0x00, # flags
283
- TN5250EDataType.INPUT, # data type (matches request)
284
- 0x00,
285
- 0x00, # seq / asap-exit
286
- len(var_hdr), # variable header length = 4
287
- ]
288
- )
289
- + var_hdr
290
- )
291
- frame = _iac_escape(frame) + bytes(
292
- [TelnetCmd.INTERPRET_AS_COMMAND, TelnetCmd.END_OF_RECORD]
293
- )
294
- log.debug("Sending Save Screen response")
295
- with self._lock:
296
- try:
297
- self._sock.sendall(frame)
298
- except OSError as exc:
299
- raise ConnectionError(f"Send failed: {exc}") from exc
333
+ frame = _build_tn5250e_frame(req_resp=0x00, opcode=len(var_hdr), var_hdr=var_hdr)
334
+ self._send_frame(frame, description="Sending Save Screen response")
300
335
 
301
336
  def recv_record(self) -> bytes:
302
337
  """Block until a complete record arrives; return the raw 5250 payload."""
@@ -369,6 +404,24 @@ class TN5250Connection:
369
404
  """True if TN5250E enhanced mode was successfully negotiated."""
370
405
  return self._tn5250e_active
371
406
 
407
+ @property
408
+ def last_recv_opcode(self) -> int:
409
+ """Return byte 9 (opcode/var_len) from the last received TN5250E frame.
410
+
411
+ Read-poll responses must echo this value back to the server.
412
+ """
413
+ return self._last_recv_opcode
414
+
415
+ @property
416
+ def last_recv_was_cancel_invite(self) -> bool:
417
+ """True if the last received TN5250E record was a Cancel-Invite.
418
+
419
+ The host sends a header-only Cancel-Invite (opcode ``0x0A``, no 5250
420
+ payload) after the workstation presses Attn. It must be acknowledged
421
+ via :meth:`send_cancel_invite_response`.
422
+ """
423
+ return self._last_recv_opcode == _TN5250E_OP_CANCEL_INVITE
424
+
372
425
  # ------------------------------------------------------------------
373
426
  # Internal — Telnet negotiation
374
427
  # ------------------------------------------------------------------
@@ -698,3 +751,40 @@ class TN5250Connection:
698
751
  def _iac_escape(data: bytes) -> bytes:
699
752
  """Double any IAC (0xFF) bytes in *data* for safe Telnet transmission."""
700
753
  return data.replace(b"\xff", b"\xff\xff")
754
+
755
+
756
+ def _build_tn5250e_frame(*, req_resp: int, opcode: int, var_hdr: bytes = b"") -> bytes:
757
+ """Build an IAC-EOR-terminated TN5250E frame with no 5250 payload.
758
+
759
+ Produces the fixed 10-byte header (RFC 2877 §3.3) followed by an optional
760
+ variable header, IAC-escapes it, and appends the IAC-EOR record terminator.
761
+ Used for the header-only control records: Attn, Cancel-Invite response and
762
+ Save Screen response.
763
+
764
+ Parameters
765
+ ----------
766
+ req_resp:
767
+ Byte 7 (request/response flags) — e.g. the Attn flag.
768
+ opcode:
769
+ Byte 9 (operation code / variable-header length).
770
+ var_hdr:
771
+ Optional variable-header bytes appended after the fixed header.
772
+ """
773
+ total = _TN5250E_HEADER_LEN + len(var_hdr)
774
+ header = bytes(
775
+ [
776
+ (total >> 8) & 0xFF,
777
+ total & 0xFF, # 0-1: logical record length
778
+ _GDS_RECORD_ID_HI,
779
+ _GDS_RECORD_ID_LO, # 2-3: GDS identifier
780
+ 0x00,
781
+ 0x00, # 4-5: reserved
782
+ TN5250EDataType.INPUT, # 6: data type (workstation → host)
783
+ req_resp, # 7: request/response flags
784
+ 0x00, # 8: error recovery
785
+ opcode, # 9: opcode / variable-header length
786
+ ]
787
+ )
788
+ return _iac_escape(header + var_hdr) + bytes(
789
+ [TelnetCmd.INTERPRET_AS_COMMAND, TelnetCmd.END_OF_RECORD]
790
+ )
@@ -217,6 +217,20 @@ class Terminal:
217
217
  """Send an AID key by name, e.g. ``"Enter"``, ``"F3"``, ``"Clear"``."""
218
218
  self.send_key(AID.from_name(name), wait=wait)
219
219
 
220
+ def send_attn(self, *, wait: bool = True) -> None:
221
+ """Send the Attention (Attn) signal to the host.
222
+
223
+ Attn is a protocol-level attention indication -- *not* an AID key -- and
224
+ is what 5250 emulators send for the PC "Esc" key. On the AS/400 it
225
+ typically raises the System Request / Assist menu. By default this
226
+ blocks until the host responds; pass ``wait=False`` to return
227
+ immediately after sending.
228
+ """
229
+ self._conn.send_attn()
230
+ self._screen._keyboard_locked = True
231
+ if wait:
232
+ self._recv_and_apply()
233
+
220
234
  def type_into(
221
235
  self,
222
236
  field: Field | FieldProxy,
@@ -517,6 +531,14 @@ class Terminal:
517
531
 
518
532
  def _recv_and_apply(self) -> None:
519
533
  received_payload = self._conn.recv_record()
534
+ # A header-only Cancel-Invite (no 5250 payload) is the host's reply to
535
+ # our Attn: it cancels the outstanding read invite. We must echo it back
536
+ # to let the host run its Attention program (e.g. the Assist menu);
537
+ # otherwise the host stalls and never sends the follow-up screen.
538
+ if not received_payload and self._conn.last_recv_was_cancel_invite:
539
+ log.debug("Received Cancel-Invite; echoing acknowledgment")
540
+ self._conn.send_cancel_invite_response()
541
+ return
520
542
  parsed_events = list(self._parser.parse(received_payload))
521
543
  # If this record has screen content, the pending save is no longer back-to-back
522
544
  for ev in parsed_events:
@@ -13,7 +13,7 @@ wheels = [
13
13
 
14
14
  [[package]]
15
15
  name = "ibm5250"
16
- version = "0.1.0.dev1"
16
+ version = "0.1.0.dev2"
17
17
  source = { editable = "." }
18
18
 
19
19
  [package.dev-dependencies]
File without changes
File without changes
File without changes