"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSerialRaw = setSerialRaw;
const child_process_1 = require("child_process");
const log_1 = require("../log");
// Put a /dev/cu.* | /dev/tty* | COM serial port into RAW mode before we read
// it. Without this, macOS (and Linux) open the TTY in canonical/"cooked"
// mode: the kernel only hands bytes to the reader when it sees a line-feed
// (\n) and echoes input back. Bluetooth barcode / NFC readers almost always
// terminate a scan with CR (\r) — or nothing — so in cooked mode the bytes
// sit in the line-discipline buffer forever and our fs.ReadStream never gets
// a 'data' event. That is the "port is open (lsof shows the fd) but zero
// scans are ever parsed" failure.
//
// `raw` flips off canonical mode, echo, signal chars and input mapping, and
// sets VMIN=1/VTIME=0 so every byte is delivered as it arrives. We do NOT
// touch the baud rate: for USB-CDC and Bluetooth-SPP the link speed is
// nominal, and forcing a wrong baud would break a real FTDI/CP210x USB
// scanner. Best-effort — a failure here is logged, not fatal, so a platform
// without `stty` still attaches (it just may not deliver CR-only data).
function setSerialRaw(devPath) {
    // Windows COM ports aren't stty-configurable here; skip silently.
    if (/^COM\d+$/i.test(devPath))
        return;
    // macOS uses `stty -f <dev>`, Linux/BSD use `stty -F <dev>`.
    const flag = process.platform === 'darwin' ? '-f' : '-F';
    try {
        (0, child_process_1.execSync)(`stty ${flag} ${devPath} raw -echo -echoe -echok -echoctl -echoke -ixon -ixoff 2>/dev/null`, {
            timeout: 3000,
        });
        log_1.log.info('serial: set raw mode', { path: devPath });
    }
    catch (e) {
        log_1.log.warn('serial: stty raw failed (continuing — CR-only data may not flush)', {
            path: devPath,
            msg: e?.message,
        });
    }
}
//# sourceMappingURL=serial-tty.js.map