@slastra/tsplib 0.1.0 → 0.3.0

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.
package/FINDINGS.md CHANGED
@@ -1,4 +1,4 @@
1
- # Findings: Labeer Y43BT over BLE
1
+ # Findings: Labeer Y43BT and Core Innovations CTP800BD over BLE
2
2
 
3
3
  How the Y43BT's BLE interface was mapped, on 2026-11-03, with a Labeer Y43BT on the bench and [bleak](https://github.com/hbldh/bleak) on Linux (BlueZ). Every claim below was checked on paper or on the wire.
4
4
 
@@ -6,22 +6,22 @@ How the Y43BT's BLE interface was mapped, on 2026-11-03, with a Labeer Y43BT on
6
6
 
7
7
  The printer is dual-mode and shows up twice:
8
8
 
9
- | Name | Address | Transport |
10
- |---|---|---|
11
- | `Y43BT-69B2` | `28:D4:1E:FA:69:B2` | classic Bluetooth |
12
- | `Y43BT-69B2_LE` | `E8:D4:1E:FA:69:B2` (random) | BLE |
9
+ | Name | Address | Transport |
10
+ | --------------- | ---------------------------- | ----------------- |
11
+ | `Y43BT-69B2` | `28:D4:1E:FA:69:B2` | classic Bluetooth |
12
+ | `Y43BT-69B2_LE` | `E8:D4:1E:FA:69:B2` (random) | BLE |
13
13
 
14
14
  The LE advertisement lists service UUIDs `18f0` and `fee7` (the Tencent/WeChat service), and manufacturer data carrying the classic MAC.
15
15
 
16
16
  ## The GATT table
17
17
 
18
- | Service | Characteristics | What it turned out to be |
19
- |---|---|---|
20
- | `18f0` | `2af1` write, `2af0` notify | The usual home of TSPL on cheap BLE printers. **Accepts writes and does nothing.** No reply, no motor, no print. |
21
- | `ff00` | `ff02` write, `ff01` notify, `ff03` notify | **The printer.** TSPL on `ff02`, credit flow on `ff03`. |
22
- | `ff80` | `ff82` write, `ff81` notify | The BT module's AT command channel: anything else gets `\r\nERROR\r\n`. |
23
- | `49535343-fe7d-…` | ISSC transparent UART | Answers every write with `00 00 00 f7 01`, whatever was sent. |
24
- | `fff0`, `ff10`, `fee7` | assorted | Silent to everything tried. |
18
+ | Service | Characteristics | What it turned out to be |
19
+ | ---------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
20
+ | `18f0` | `2af1` write, `2af0` notify | The usual home of TSPL on cheap BLE printers. **Accepts writes and does nothing.** No reply, no motor, no print. |
21
+ | `ff00` | `ff02` write, `ff01` notify, `ff03` notify | **The printer.** TSPL on `ff02`, credit flow on `ff03`. |
22
+ | `ff80` | `ff82` write, `ff81` notify | The BT module's AT command channel: anything else gets `\r\nERROR\r\n`. |
23
+ | `49535343-fe7d-…` | ISSC transparent UART | Answers every write with `00 00 00 f7 01`, whatever was sent. |
24
+ | `fff0`, `ff10`, `fee7` | assorted | Silent to everything tried. |
25
25
 
26
26
  ## Finding the channel
27
27
 
@@ -54,10 +54,36 @@ A 112 mm ruler printed under that `SIZE` stopped right after its 101 mm tick. Th
54
54
 
55
55
  ## Media
56
56
 
57
- 4 × 6 in gap stock prints cleanly. 2 × 1 in stock printed the start of a label and then jammed twice on adhesive in the paper path; after cleaning, 4 × 6 has been fine. Small stock still needs a clean run.
57
+ 4 × 6 in gap stock prints cleanly. 2 × 1 in stock first printed the start of a label and then jammed, twice, on adhesive left in the paper path. Once that was cleaned out it printed without issue from printrow, placed correctly with the raster at `x = 0`, so the printer positions a narrow label from `SIZE` itself.
58
58
 
59
59
  ## Still open
60
60
 
61
61
  - The true head width, which needs a `SIZE` wider than 4 in.
62
62
  - Any status query this printer answers, if one exists. The vendor app (Flashlabel Pro) would show it in an HCI snoop.
63
- - Whether a narrow label is positioned by the printer from `SIZE` or needs an `x` offset to sit under the head.
63
+
64
+ ## Core Innovations CTP800BD
65
+
66
+ Probed on 2026-11-04, with raw LE ATT sockets on Linux (bluetoothd bypassed, for the reason below) and then from Chrome.
67
+
68
+ ### Same module, different name
69
+
70
+ It advertises as `Shipping Printer-B863` on a **public** address, `02:3F:05:B8:B8:63`, with service UUIDs `e7810a71-73ae-499d-8c15-faa9aef0c3f2` and the classic SPP `1101`. The GATT table matches the Y43BT's where it matters: `ff00` with `ff02` write, `ff01` and `ff03` notify, and `18f0`/`2af1` again. It also carries `e7810a71-…` (characteristic `bef8d6c9-…`) and the ISSC UART service, but no `ff80` AT channel.
71
+
72
+ TSPL on `ff02` prints: first a hand-written `TEXT`/`BOX`/`BAR` job, then a full 4 × 6 in raster from `buildLabel` (816 × 1216, 124,133 bytes) with an edge border, a millimetre ruler and 1-, 2- and 4-dot checkerboards. It came out complete and clean, and the ruler burned past its 100 mm tick. The product page gives the print width as 4.01 in. None of `ESC !?`, `~!T`, `~!I`, `~!F`, `~HS`, `ESC !S` or `~!@` got a reply on any channel.
73
+
74
+ ### It never returns credits
75
+
76
+ On subscribing, `ff03` notifies:
77
+
78
+ ```
79
+ 01 0a 10 credits
80
+ 02 b6 00 182: the largest write the module takes
81
+ ```
82
+
83
+ After that it never sent anything else on `ff03`: no credit back after any write, during a job or in the seconds after it. The full raster went out as 683 back-to-back 182-byte writes with no gating at all, in 1.6 s from the host's side, and printed without loss. Metering it by credits would stall on the eleventh write, so `connect` takes `credits: false` for it.
84
+
85
+ ### Linux: BlueZ connects over classic
86
+
87
+ The printer runs classic and LE on the same public address, and its advertising flags (`0x02`) do not mark BR/EDR as unsupported. For a device like that, BlueZ's `Device1.Connect` picks BR/EDR: a classic bond always wins, and otherwise the "last seen" tie between the two bearers breaks toward classic. A `btmon` capture of Chrome's `gatt.connect()` shows HCI `Create Connection` (the BR/EDR page, `0x01|0x0005`), then SDP on PSM 1, then a disconnect about five seconds later, because there is no GATT over classic. Chrome reports `NetworkError: Unknown error when connecting to the device`.
88
+
89
+ BlueZ 5.87 has a per-device `PreferredBearer` property, but only with `Experimental = true` in `/etc/bluetooth/main.conf`. With that on and the property set to `le` (and `Trusted` set to true), Chrome connected over LE and printed. A classic pairing from the desktop's Bluetooth settings also forces BR/EDR, so the printer should not be paired there.
package/README.md CHANGED
@@ -11,13 +11,21 @@ TSPL itself is an open, documented language, and plenty of code already speaks i
11
11
 
12
12
  ## Hardware
13
13
 
14
- Verified on a **Labeer Y43BT** over BLE, at 203 dpi (8 dots/mm), on 4 × 6 in stock: text, rasters, and a 1-dot checkerboard all print clean. A 60 KB full-width raster transfers in under 3 seconds. [printrow](https://printrow.lastra.us) drives it through this library, from Chrome.
14
+ Verified on a **Labeer Y43BT** over BLE, at 203 dpi (8 dots/mm), on 4 × 6 in and 2 × 1 in gap stock: text, rasters, and a 1-dot checkerboard all print clean. A 60 KB full-width raster transfers in under 3 seconds. [printrow](https://printrow.lastra.us) drives it through this library, from Chrome.
15
15
 
16
16
  The module advertises twice, as `Y43BT-XXXX` (classic Bluetooth) and `Y43BT-XXXX_LE`. Only the `_LE` one has the GATT services, so that is the one to pick in Chrome's chooser.
17
17
 
18
- The head prints at least the full 4 in (101.6 mm); its true width has not been measured past that. 2 × 1 in stock has printed but jammed on adhesive in the paper path, so small stock is not yet verified end to end.
18
+ The head prints at least the full 4 in (101.6 mm); its true width has not been measured past that. A narrow label needs no `x` offset: the printer places it under the head from `SIZE` alone.
19
19
 
20
- **How to tell if your printer is this one.** Connect over BLE and look for service `ff00` with characteristics `ff02` (write) and `ff03` (notify). If subscribing to `ff03` immediately notifies `01 07` and `02 f4 00`, it is this module, whatever the case says.
20
+ Also verified on a **Core Innovations CTP800BD**, the same module family under the name `Shipping Printer-XXXX`, on 4 × 6 in gap stock with a full-width 124 KB raster. Two things differ. It grants ten credits on subscribing and never returns another, so it is driven without credits. And it runs classic and LE on one public address, which on Linux makes BlueZ connect over classic and fail; [FINDINGS.md](./FINDINGS.md#core-innovations-ctp800bd) has the BlueZ setting that fixes it.
21
+
22
+ Both are in the exported `PRINTERS` table, so a bare `connect()` offers either one in Chrome's chooser and picks the credit mode from the name it gets back. `link.printer` says which it was. To insist on one:
23
+
24
+ ```ts
25
+ const link = await connect({ namePrefix: 'Shipping Printer', credits: false });
26
+ ```
27
+
28
+ **How to tell if your printer is this one.** Connect over BLE and look for service `ff00` with characteristics `ff02` (write) and `ff03` (notify). If subscribing to `ff03` immediately notifies a credit grant (`01 nn`) and a write limit (`02 lo hi`), it is this module, whatever the case says. If the credits never come back after writes, use `credits: false`.
21
29
 
22
30
  ## Install
23
31
 
@@ -45,17 +53,17 @@ await printJob(link, [async () => job], { onProgress: (done, total) => console.l
45
53
 
46
54
  `buildLabel` options:
47
55
 
48
- | Option | Default | |
49
- |---|---|---|
50
- | `widthMm`, `heightMm` | required | Label size. TSPL clips anything drawn past the width. |
51
- | `media` | `'gap'` | `'gap'`, `'mark'` (black mark, sent as `BLINE`) or `'continuous'`. |
52
- | `gapMm` | `3` | Gap or mark length. |
53
- | `density` | `8` | Burn darkness, 0 to 15. |
54
- | `copies` | `1` | Copies of this label, at no extra cost on the wire. |
55
- | `direction` | `0` | TSPL `DIRECTION`: `1` turns the label half a turn. |
56
- | `x`, `y` | `0` | Where the raster's top-left lands, in dots. |
56
+ | Option | Default | |
57
+ | --------------------- | -------- | ------------------------------------------------------------------ |
58
+ | `widthMm`, `heightMm` | required | Label size. TSPL clips anything drawn past the width. |
59
+ | `media` | `'gap'` | `'gap'`, `'mark'` (black mark, sent as `BLINE`) or `'continuous'`. |
60
+ | `gapMm` | `3` | Gap or mark length. |
61
+ | `density` | `8` | Burn darkness, 0 to 15. |
62
+ | `copies` | `1` | Copies of this label, at no extra cost on the wire. |
63
+ | `direction` | `0` | TSPL `DIRECTION`: `1` turns the label half a turn. |
64
+ | `x`, `y` | `0` | Where the raster's top-left lands, in dots. |
57
65
 
58
- `connect` options are `namePrefix` (default `'Y43BT'`), `maxChunk` (240), `creditTimeoutMs` (10000) and `onDisconnect`.
66
+ `connect` options are `namePrefix` (default: every printer in `PRINTERS`), `maxChunk` (240), `creditTimeoutMs` (10000), `credits` (default: the picked printer's entry in `PRINTERS`, else `true`; `false` sends without waiting for credits, for printers that never return them) and `onDisconnect`. The link reports `printer` (the `PRINTERS` entry, or `null`) and `credits`.
59
67
 
60
68
  Any transport with a `send(bytes)` that resolves once the bytes are accepted can drive `printJob`, so a USB `/dev/usb/lpN` writer works as well as Bluetooth.
61
69
 
@@ -66,7 +74,7 @@ Any transport with a `send(bytes)` that resolves once the bytes are accepted can
66
74
  - **Credits are the backpressure.** Every GATT write costs one credit and earns one back, so writes go out exactly as fast as the printer takes them, with no pacing timer to tune.
67
75
  - **The write size follows the printer.** It announces its limit (244 bytes) on the flow channel. If the browser's link never negotiated an MTU that big, the first large write fails, its credit is refunded, and the link drops to 20-byte writes for good.
68
76
  - **Cancelling stops between labels, never inside one.** A half-sent `BITMAP` leaves the printer reading the next job's commands as image data.
69
- - **There is no status.** The printer answered none of TSPL's or ESC/POS's status queries on any channel. A credit that never comes back (10 s by default) is the only fault signal, and it fails the send rather than hanging it.
77
+ - **There is no status.** Neither printer answered any of TSPL's or ESC/POS's status queries on any channel. A credit that never comes back (10 s by default) is the only fault signal, and it fails the send rather than hanging it. With `credits: false` there is no fault signal at all.
70
78
 
71
79
  ## Development
72
80
 
package/dist/index.d.ts CHANGED
@@ -10,4 +10,5 @@ export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel, t
10
10
  export { THRESHOLD, lumaOverWhite, imageDataToRows, type RasterRow, type PixelSource } from './raster.js';
11
11
  export { parseFlow, CreditGate, type FlowMessage } from './flow.js';
12
12
  export { printJob, type Link, type PrintJobOptions } from './transport.js';
13
+ export { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EACV,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EACf,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAE,KAAK,IAAI,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EACV,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EACf,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAE,KAAK,IAAI,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE3E,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel }
10
10
  export { THRESHOLD, lumaOverWhite, imageDataToRows } from './raster.js';
11
11
  export { parseFlow, CreditGate } from './flow.js';
12
12
  export { printJob } from './transport.js';
13
+ export { PRINTERS, knownPrinter } from './printers.js';
13
14
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EAGV,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EAGf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAmC,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EAGV,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EAGf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAmC,MAAM,gBAAgB,CAAC;AAE3E,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAqB,MAAM,eAAe,CAAC"}
@@ -0,0 +1,21 @@
1
+ /** A printer this library has been verified on, by what its BLE side advertises. */
2
+ export interface KnownPrinter {
3
+ /** What it is sold as. */
4
+ readonly name: string;
5
+ /** Its BLE advertised name, up to the per-unit suffix. */
6
+ readonly namePrefix: string;
7
+ /**
8
+ * Whether it hands a write credit back for every write. A printer that does
9
+ * is metered by them; one that does not has to be sent to back to back.
10
+ */
11
+ readonly credits: boolean;
12
+ }
13
+ /**
14
+ * Every printer verified so far. They share one BLE module family (TSPL on
15
+ * `ff00`, flow on `ff03`) and differ in name and in whether credits come back.
16
+ * See FINDINGS.md for each.
17
+ */
18
+ export declare const PRINTERS: readonly KnownPrinter[];
19
+ /** The known printer a BLE device name belongs to, if any. */
20
+ export declare function knownPrinter(deviceName: string): KnownPrinter | undefined;
21
+ //# sourceMappingURL=printers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"printers.d.ts","sourceRoot":"","sources":["../src/printers.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,MAAM,WAAW,YAAY;IAC5B,0BAA0B;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0DAA0D;IAC1D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC1B;AAED;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,SAAS,YAAY,EAK3C,CAAC;AAEF,8DAA8D;AAC9D,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAEzE"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Every printer verified so far. They share one BLE module family (TSPL on
3
+ * `ff00`, flow on `ff03`) and differ in name and in whether credits come back.
4
+ * See FINDINGS.md for each.
5
+ */
6
+ export const PRINTERS = [
7
+ // advertises `Y43BT-XXXX` (classic) and `Y43BT-XXXX_LE`; only `_LE` has GATT
8
+ { name: 'Labeer Y43BT', namePrefix: 'Y43BT', credits: true },
9
+ // grants ten credits on subscribing and never returns another
10
+ { name: 'Core Innovations CTP800BD', namePrefix: 'Shipping Printer', credits: false }
11
+ ];
12
+ /** The known printer a BLE device name belongs to, if any. */
13
+ export function knownPrinter(deviceName) {
14
+ return PRINTERS.find((p) => deviceName.startsWith(p.namePrefix));
15
+ }
16
+ //# sourceMappingURL=printers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"printers.js","sourceRoot":"","sources":["../src/printers.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAA4B;IAChD,6EAA6E;IAC7E,EAAE,IAAI,EAAE,cAAc,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE;IAC5D,8DAA8D;IAC9D,EAAE,IAAI,EAAE,2BAA2B,EAAE,UAAU,EAAE,kBAAkB,EAAE,OAAO,EAAE,KAAK,EAAE;CACrF,CAAC;AAEF,8DAA8D;AAC9D,MAAM,UAAU,YAAY,CAAC,UAAkB;IAC9C,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;AAClE,CAAC"}
@@ -1,9 +1,13 @@
1
+ import { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
1
2
  import type { Link } from './transport.js';
3
+ export { PRINTERS, knownPrinter, type KnownPrinter };
2
4
  export interface ConnectOptions {
3
5
  /**
4
- * Device name prefix to match. The module advertises twice, `Y43BT-XXXX`
5
- * over classic Bluetooth and `Y43BT-XXXX_LE` over BLE; pick the `_LE` one
6
- * in the chooser, since that is the one with the GATT services.
6
+ * Device name prefix to match. Left out, the chooser offers every printer in
7
+ * {@link PRINTERS}, so one Connect reaches whichever is switched on.
8
+ *
9
+ * The Y43BT advertises twice, `Y43BT-XXXX` over classic Bluetooth and
10
+ * `Y43BT-XXXX_LE` over BLE; the `_LE` one is the one with the GATT services.
7
11
  */
8
12
  namePrefix?: string;
9
13
  /**
@@ -15,12 +19,28 @@ export interface ConnectOptions {
15
19
  maxChunk?: number;
16
20
  /** How long to wait for the printer to return a credit before giving up. */
17
21
  creditTimeoutMs?: number;
22
+ /**
23
+ * Whether to meter writes by the credits on the flow channel. The Y43BT
24
+ * returns one credit per write; the Core Innovations CTP800BD grants ten
25
+ * on subscribing and never returns another, so metering it stalls on the
26
+ * eleventh write. With `false` the writes go out back to back, which that
27
+ * printer takes without loss, but a jammed or empty printer then no longer
28
+ * fails the send: there is no fault signal left.
29
+ *
30
+ * Left out, it follows the picked printer's entry in {@link PRINTERS}, and
31
+ * is `true` for a printer not in it.
32
+ */
33
+ credits?: boolean;
18
34
  /** Fires if the printer drops the GATT link. */
19
35
  onDisconnect?: () => void;
20
36
  }
21
37
  /** A connected printer. Extends {@link Link}, so it drives `printJob` directly. */
22
38
  export interface BluetoothLink extends Link {
23
39
  readonly deviceName: string;
40
+ /** The known printer the device turned out to be, or null for any other. */
41
+ readonly printer: KnownPrinter | null;
42
+ /** Whether writes are metered by credits on this link. */
43
+ readonly credits: boolean;
24
44
  /** The write size currently in use, in bytes. */
25
45
  readonly chunkSize: number;
26
46
  disconnect(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"web-bluetooth.d.ts","sourceRoot":"","sources":["../src/web-bluetooth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAc3C,MAAM,WAAW,cAAc;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,YAAY,CAAC,EAAE,MAAM,IAAI,CAAC;CAC1B;AAED,mFAAmF;AACnF,MAAM,WAAW,aAAc,SAAQ,IAAI;IAC1C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,iDAAiD;IACjD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,UAAU,IAAI,IAAI,CAAC;CACnB;AAED,yEAAyE;AACzE,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED,4EAA4E;AAC5E,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAED,6CAA6C;AAC7C,wBAAgB,WAAW,IAAI,OAAO,CAErC;AAED;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,aAAa,CAAC,CAoElF"}
1
+ {"version":3,"file":"web-bluetooth.d.ts","sourceRoot":"","sources":["../src/web-bluetooth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,CAAC;AAcrD,MAAM,WAAW,cAAc;IAC9B;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,gDAAgD;IAChD,YAAY,CAAC,EAAE,MAAM,IAAI,CAAC;CAC1B;AAED,mFAAmF;AACnF,MAAM,WAAW,aAAc,SAAQ,IAAI;IAC1C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,4EAA4E;IAC5E,QAAQ,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IACtC,0DAA0D;IAC1D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,iDAAiD;IACjD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,UAAU,IAAI,IAAI,CAAC;CACnB;AAED,yEAAyE;AACzE,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED,4EAA4E;AAC5E,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAED,6CAA6C;AAC7C,wBAAgB,WAAW,IAAI,OAAO,CAErC;AAED;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,aAAa,CAAC,CAyElF"}
@@ -1,4 +1,6 @@
1
1
  import { CreditGate, parseFlow } from './flow.js';
2
+ import { PRINTERS, knownPrinter } from './printers.js';
3
+ export { PRINTERS, knownPrinter };
2
4
  /**
3
5
  * The module advertises `0x18f0` (write `2af1`), the service most cheap BLE
4
6
  * printers carry TSPL on. On the Y43BT that one accepts writes and does
@@ -32,8 +34,11 @@ export function isSupported() {
32
34
  * Must be called from a user gesture, per the Web Bluetooth spec.
33
35
  */
34
36
  export async function connect(options = {}) {
35
- const { namePrefix = 'Y43BT', maxChunk = 240, creditTimeoutMs = 10000, onDisconnect } = options;
36
- const device = await pickDevice(namePrefix);
37
+ const { namePrefix, maxChunk = 240, creditTimeoutMs = 10000, onDisconnect } = options;
38
+ const prefixes = namePrefix === undefined ? PRINTERS.map((p) => p.namePrefix) : [namePrefix];
39
+ const device = await pickDevice(prefixes);
40
+ const printer = knownPrinter(device.name ?? '') ?? null;
41
+ const credits = options.credits ?? printer?.credits ?? true;
37
42
  if (!device.gatt)
38
43
  throw new Error('device has no GATT server');
39
44
  device.addEventListener('gattserverdisconnected', () => onDisconnect?.());
@@ -63,7 +68,8 @@ export async function connect(options = {}) {
63
68
  async function sendNow(bytes) {
64
69
  let at = 0;
65
70
  while (at < bytes.length) {
66
- await gate.take(creditTimeoutMs);
71
+ if (credits)
72
+ await gate.take(creditTimeoutMs);
67
73
  const size = chunk;
68
74
  try {
69
75
  await writeChar.writeValueWithoutResponse(bytes.slice(at, at + size));
@@ -71,7 +77,8 @@ export async function connect(options = {}) {
71
77
  catch (err) {
72
78
  // Nothing went out, so the credit is still ours. An oversized write
73
79
  // is the one failure worth retrying, smaller; anything else is real.
74
- gate.grant(1);
80
+ if (credits)
81
+ gate.grant(1);
75
82
  if (size <= SAFE_CHUNK)
76
83
  throw err;
77
84
  chunk = SAFE_CHUNK;
@@ -82,7 +89,9 @@ export async function connect(options = {}) {
82
89
  }
83
90
  }
84
91
  return {
85
- deviceName: device.name ?? namePrefix,
92
+ deviceName: device.name ?? prefixes[0],
93
+ printer,
94
+ credits,
86
95
  get chunkSize() {
87
96
  return chunk;
88
97
  },
@@ -96,11 +105,15 @@ export async function connect(options = {}) {
96
105
  }
97
106
  };
98
107
  }
99
- async function pickDevice(namePrefix) {
108
+ async function pickDevice(prefixes) {
109
+ const matching = (name = '') => prefixes.some((prefix) => name.startsWith(prefix));
100
110
  if (navigator.bluetooth.getDevices) {
101
111
  try {
112
+ // With more than one printer permitted, this takes the first, whether
113
+ // it is switched on or not. A caller that sees the connect fail can
114
+ // forget() it, and the next connect opens the chooser.
102
115
  const known = await navigator.bluetooth.getDevices();
103
- const matches = known.filter((d) => (d.name ?? '').startsWith(namePrefix));
116
+ const matches = known.filter((d) => matching(d.name));
104
117
  // a dual-mode printer can leave both of its names permitted; the LE one
105
118
  // is the one with the GATT services
106
119
  const hit = matches.find((d) => d.name?.endsWith('_LE')) ?? matches[0];
@@ -112,7 +125,7 @@ async function pickDevice(namePrefix) {
112
125
  }
113
126
  }
114
127
  return navigator.bluetooth.requestDevice({
115
- filters: [{ namePrefix }],
128
+ filters: prefixes.map((namePrefix) => ({ namePrefix })),
116
129
  optionalServices: [SERVICE]
117
130
  });
118
131
  }
@@ -1 +1 @@
1
- {"version":3,"file":"web-bluetooth.js","sourceRoot":"","sources":["../src/web-bluetooth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGlD;;;;GAIG;AACH,MAAM,OAAO,GAAG,MAAM,CAAC;AACvB,MAAM,UAAU,GAAG,MAAM,CAAC;AAC1B,MAAM,SAAS,GAAG,MAAM,CAAC;AAEzB,8EAA8E;AAC9E,MAAM,UAAU,GAAG,EAAE,CAAC;AA8BtB,yEAAyE;AACzE,MAAM,UAAU,YAAY;IAC3B,OAAO,OAAO,SAAS,KAAK,WAAW,IAAI,WAAW,IAAI,SAAS,CAAC;AACrE,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,eAAe;IAC9B,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,eAAe,CAAC;AAChE,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,WAAW;IAC1B,OAAO,YAAY,EAAE,IAAI,eAAe,EAAE,CAAC;AAC5C,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,UAA0B,EAAE;IACzD,MAAM,EAAE,UAAU,GAAG,OAAO,EAAE,QAAQ,GAAG,GAAG,EAAE,eAAe,GAAG,KAAK,EAAE,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AC3C,MAAM,QAAQ,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IACnF,IAAI,SAAS,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;QACpC,IAAI,CAAC;YACJ,sEAAsE;YACtE,oEAAoE;YACpE,uDAAuD;YACvD,MAAM,KAAK,GAAG,MAAM,SAAS,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;YACrD,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;YACtD,wEAAwE;YACxE,oCAAoC;YACpC,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC;YACvE,IAAI,GAAG;gBAAE,OAAO,GAAG,CAAC;QACrB,CAAC;QAAC,MAAM,CAAC;YACR,6DAA6D;QAC9D,CAAC;IACF,CAAC;IACD,OAAO,SAAS,CAAC,SAAS,CAAC,aAAa,CAAC;QACxC,OAAO,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;QACvD,gBAAgB,EAAE,CAAC,OAAO,CAAC;KAC3B,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@slastra/tsplib",
3
- "version": "0.1.0",
4
- "description": "TSPL raster label printing over BLE for printers that hide TSPL behind a credit-metered GATT channel (Labeer Y43BT and its white-label siblings). Job builder, 1-bit raster, and a Web Bluetooth transport.",
3
+ "version": "0.3.0",
4
+ "description": "TSPL raster label printing over BLE for printers that hide TSPL behind a credit-metered GATT channel (Labeer Y43BT, Core Innovations CTP800BD, and their white-label siblings). Job builder, 1-bit raster, and a Web Bluetooth transport.",
5
5
  "keywords": [
6
6
  "tspl",
7
7
  "labeer",
8
8
  "y43bt",
9
+ "ctp800bd",
10
+ "core-innovations",
9
11
  "thermal-printer",
10
12
  "label-printer",
11
13
  "shipping-label",
package/src/index.ts CHANGED
@@ -28,3 +28,5 @@ export {
28
28
  export { parseFlow, CreditGate, type FlowMessage } from './flow.js';
29
29
 
30
30
  export { printJob, type Link, type PrintJobOptions } from './transport.js';
31
+
32
+ export { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
@@ -0,0 +1,29 @@
1
+ /** A printer this library has been verified on, by what its BLE side advertises. */
2
+ export interface KnownPrinter {
3
+ /** What it is sold as. */
4
+ readonly name: string;
5
+ /** Its BLE advertised name, up to the per-unit suffix. */
6
+ readonly namePrefix: string;
7
+ /**
8
+ * Whether it hands a write credit back for every write. A printer that does
9
+ * is metered by them; one that does not has to be sent to back to back.
10
+ */
11
+ readonly credits: boolean;
12
+ }
13
+
14
+ /**
15
+ * Every printer verified so far. They share one BLE module family (TSPL on
16
+ * `ff00`, flow on `ff03`) and differ in name and in whether credits come back.
17
+ * See FINDINGS.md for each.
18
+ */
19
+ export const PRINTERS: readonly KnownPrinter[] = [
20
+ // advertises `Y43BT-XXXX` (classic) and `Y43BT-XXXX_LE`; only `_LE` has GATT
21
+ { name: 'Labeer Y43BT', namePrefix: 'Y43BT', credits: true },
22
+ // grants ten credits on subscribing and never returns another
23
+ { name: 'Core Innovations CTP800BD', namePrefix: 'Shipping Printer', credits: false }
24
+ ];
25
+
26
+ /** The known printer a BLE device name belongs to, if any. */
27
+ export function knownPrinter(deviceName: string): KnownPrinter | undefined {
28
+ return PRINTERS.find((p) => deviceName.startsWith(p.namePrefix));
29
+ }
@@ -1,6 +1,9 @@
1
1
  import { CreditGate, parseFlow } from './flow.js';
2
+ import { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
2
3
  import type { Link } from './transport.js';
3
4
 
5
+ export { PRINTERS, knownPrinter, type KnownPrinter };
6
+
4
7
  /**
5
8
  * The module advertises `0x18f0` (write `2af1`), the service most cheap BLE
6
9
  * printers carry TSPL on. On the Y43BT that one accepts writes and does
@@ -15,9 +18,11 @@ const SAFE_CHUNK = 20;
15
18
 
16
19
  export interface ConnectOptions {
17
20
  /**
18
- * Device name prefix to match. The module advertises twice, `Y43BT-XXXX`
19
- * over classic Bluetooth and `Y43BT-XXXX_LE` over BLE; pick the `_LE` one
20
- * in the chooser, since that is the one with the GATT services.
21
+ * Device name prefix to match. Left out, the chooser offers every printer in
22
+ * {@link PRINTERS}, so one Connect reaches whichever is switched on.
23
+ *
24
+ * The Y43BT advertises twice, `Y43BT-XXXX` over classic Bluetooth and
25
+ * `Y43BT-XXXX_LE` over BLE; the `_LE` one is the one with the GATT services.
21
26
  */
22
27
  namePrefix?: string;
23
28
  /**
@@ -29,6 +34,18 @@ export interface ConnectOptions {
29
34
  maxChunk?: number;
30
35
  /** How long to wait for the printer to return a credit before giving up. */
31
36
  creditTimeoutMs?: number;
37
+ /**
38
+ * Whether to meter writes by the credits on the flow channel. The Y43BT
39
+ * returns one credit per write; the Core Innovations CTP800BD grants ten
40
+ * on subscribing and never returns another, so metering it stalls on the
41
+ * eleventh write. With `false` the writes go out back to back, which that
42
+ * printer takes without loss, but a jammed or empty printer then no longer
43
+ * fails the send: there is no fault signal left.
44
+ *
45
+ * Left out, it follows the picked printer's entry in {@link PRINTERS}, and
46
+ * is `true` for a printer not in it.
47
+ */
48
+ credits?: boolean;
32
49
  /** Fires if the printer drops the GATT link. */
33
50
  onDisconnect?: () => void;
34
51
  }
@@ -36,6 +53,10 @@ export interface ConnectOptions {
36
53
  /** A connected printer. Extends {@link Link}, so it drives `printJob` directly. */
37
54
  export interface BluetoothLink extends Link {
38
55
  readonly deviceName: string;
56
+ /** The known printer the device turned out to be, or null for any other. */
57
+ readonly printer: KnownPrinter | null;
58
+ /** Whether writes are metered by credits on this link. */
59
+ readonly credits: boolean;
39
60
  /** The write size currently in use, in bytes. */
40
61
  readonly chunkSize: number;
41
62
  disconnect(): void;
@@ -67,9 +88,12 @@ export function isSupported(): boolean {
67
88
  * Must be called from a user gesture, per the Web Bluetooth spec.
68
89
  */
69
90
  export async function connect(options: ConnectOptions = {}): Promise<BluetoothLink> {
70
- const { namePrefix = 'Y43BT', maxChunk = 240, creditTimeoutMs = 10000, onDisconnect } = options;
91
+ const { namePrefix, maxChunk = 240, creditTimeoutMs = 10000, onDisconnect } = options;
92
+ const prefixes = namePrefix === undefined ? PRINTERS.map((p) => p.namePrefix) : [namePrefix];
71
93
 
72
- const device = await pickDevice(namePrefix);
94
+ const device = await pickDevice(prefixes);
95
+ const printer = knownPrinter(device.name ?? '') ?? null;
96
+ const credits = options.credits ?? printer?.credits ?? true;
73
97
  if (!device.gatt) throw new Error('device has no GATT server');
74
98
  device.addEventListener('gattserverdisconnected', () => onDisconnect?.());
75
99
 
@@ -101,7 +125,7 @@ export async function connect(options: ConnectOptions = {}): Promise<BluetoothLi
101
125
  async function sendNow(bytes: Uint8Array) {
102
126
  let at = 0;
103
127
  while (at < bytes.length) {
104
- await gate.take(creditTimeoutMs);
128
+ if (credits) await gate.take(creditTimeoutMs);
105
129
  const size = chunk;
106
130
  try {
107
131
  await writeChar.writeValueWithoutResponse(
@@ -110,7 +134,7 @@ export async function connect(options: ConnectOptions = {}): Promise<BluetoothLi
110
134
  } catch (err) {
111
135
  // Nothing went out, so the credit is still ours. An oversized write
112
136
  // is the one failure worth retrying, smaller; anything else is real.
113
- gate.grant(1);
137
+ if (credits) gate.grant(1);
114
138
  if (size <= SAFE_CHUNK) throw err;
115
139
  chunk = SAFE_CHUNK;
116
140
  shrunk = true;
@@ -121,7 +145,9 @@ export async function connect(options: ConnectOptions = {}): Promise<BluetoothLi
121
145
  }
122
146
 
123
147
  return {
124
- deviceName: device.name ?? namePrefix,
148
+ deviceName: device.name ?? prefixes[0],
149
+ printer,
150
+ credits,
125
151
  get chunkSize() {
126
152
  return chunk;
127
153
  },
@@ -136,11 +162,15 @@ export async function connect(options: ConnectOptions = {}): Promise<BluetoothLi
136
162
  };
137
163
  }
138
164
 
139
- async function pickDevice(namePrefix: string): Promise<BluetoothDevice> {
165
+ async function pickDevice(prefixes: string[]): Promise<BluetoothDevice> {
166
+ const matching = (name = '') => prefixes.some((prefix) => name.startsWith(prefix));
140
167
  if (navigator.bluetooth.getDevices) {
141
168
  try {
169
+ // With more than one printer permitted, this takes the first, whether
170
+ // it is switched on or not. A caller that sees the connect fail can
171
+ // forget() it, and the next connect opens the chooser.
142
172
  const known = await navigator.bluetooth.getDevices();
143
- const matches = known.filter((d) => (d.name ?? '').startsWith(namePrefix));
173
+ const matches = known.filter((d) => matching(d.name));
144
174
  // a dual-mode printer can leave both of its names permitted; the LE one
145
175
  // is the one with the GATT services
146
176
  const hit = matches.find((d) => d.name?.endsWith('_LE')) ?? matches[0];
@@ -150,7 +180,7 @@ async function pickDevice(namePrefix: string): Promise<BluetoothDevice> {
150
180
  }
151
181
  }
152
182
  return navigator.bluetooth.requestDevice({
153
- filters: [{ namePrefix }],
183
+ filters: prefixes.map((namePrefix) => ({ namePrefix })),
154
184
  optionalServices: [SERVICE]
155
185
  });
156
186
  }