icom-wlan-node 0.3.0 → 0.4.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/README.md CHANGED
@@ -4,6 +4,7 @@ Icom WLAN (UDP) protocol implementation in Node.js + TypeScript, featuring:
4
4
 
5
5
  - Control channel handshake (AreYouThere/AreYouReady), login (0x80/0x60), token confirm/renew (0x40)
6
6
  - CI‑V over UDP encapsulation (open/close keep‑alive + CIV frame transport)
7
+ - Scope/spectrum data capture over CI‑V `0x27`, with automatic segment assembly into friendly frame events
7
8
  - Audio stream send/receive (LPCM 16‑bit mono @ 12 kHz; 20 ms frames)
8
9
  - Typed, event‑based API; designed for use as a dependency in other Node projects
9
10
 
@@ -63,6 +64,15 @@ rig.events.on('audio', (frame) => {
63
64
  // frame.pcm16 is raw 16‑bit PCM mono @ 12 kHz
64
65
  });
65
66
 
67
+ rig.events.on('scopeFrame', (frame) => {
68
+ console.log(
69
+ 'Scope:',
70
+ `${frame.startFreqHz}..${frame.endFreqHz} Hz`,
71
+ `pixels=${frame.pixels.length}`,
72
+ `mode=${frame.mode}`
73
+ );
74
+ });
75
+
66
76
  rig.events.on('error', (err) => console.error('UDP error', err));
67
77
 
68
78
  (async () => {
@@ -93,6 +103,37 @@ rig.sendAudioFloat32(tone, true);
93
103
  await rig.setPtt(false);
94
104
  ```
95
105
 
106
+ ### Scope / Spectrum
107
+
108
+ ```ts
109
+ await rig.connect();
110
+
111
+ rig.events.on('scopeSegment', (segment) => {
112
+ console.log(`scope segment ${segment.sequence}/${segment.sequenceMax}`);
113
+ });
114
+
115
+ rig.events.on('scopeFrame', (frame) => {
116
+ console.log('scope frame ready', {
117
+ startFreqHz: frame.startFreqHz,
118
+ endFreqHz: frame.endFreqHz,
119
+ pixelCount: frame.pixels.length,
120
+ outOfRange: frame.outOfRange
121
+ });
122
+ });
123
+
124
+ // Enable basic scope output
125
+ await rig.enableScope();
126
+
127
+ // Wait for one complete frame
128
+ const frame = await rig.waitForScopeFrame({ timeout: 3000 });
129
+ if (frame) {
130
+ console.log(frame.pixels[0], frame.pixels[1]);
131
+ }
132
+
133
+ // Disable scope output when finished
134
+ await rig.disableScope();
135
+ ```
136
+
96
137
  ## API Overview
97
138
 
98
139
  - `new IcomControl(options)`
@@ -104,6 +145,8 @@ await rig.setPtt(false);
104
145
  - `capabilities(CapabilitiesInfo)` — civ address, audio name (0xA8)
105
146
  - `civ(Buffer)` — raw CI‑V payload bytes as transported over UDP
106
147
  - `civFrame(Buffer)` — one complete CI‑V frame (FE FE ... FD)
148
+ - `scopeSegment(IcomScopeSegmentInfo)` — one parsed `0x27` scope segment
149
+ - `scopeFrame(IcomScopeFrame)` — one assembled spectrum/waterfall frame
107
150
  - `audio({ pcm16: Buffer })` — audio frames
108
151
  - `error(Error)` — UDP errors
109
152
  - `connectionLost(ConnectionLostInfo)` — session timeout detected
@@ -114,6 +157,7 @@ await rig.setPtt(false);
114
157
  - **Connection**: `connect()` / `disconnect(options?)` — connects control + CIV + audio sub‑sessions; resolves when all ready
115
158
  - `disconnect()` accepts optional `DisconnectOptions` or `DisconnectReason` for better error handling
116
159
  - **Raw CI‑V**: `sendCiv(buf: Buffer)` — send a raw CI‑V frame
160
+ - **Scope / Spectrum**: `scope`, `enableScope()`, `disableScope()`, `waitForScopeFrame()`
117
161
  - **Audio TX**: `setPtt(on: boolean)`, `sendAudioFloat32()`, `sendAudioPcm16()`
118
162
  - **Rig Control**: `setFrequency()`, `setMode()`, `setConnectorDataMode()`, `setConnectorWLanLevel()`
119
163
  - **Rig Query**: `readOperatingFrequency()`, `readOperatingMode()`, `readTransmitFrequency()`, `readTransceiverState()`, `readBandEdges()`
@@ -257,6 +301,45 @@ The library exposes common CI‑V operations as friendly methods. Addresses are
257
301
  - `readTransceiverState(options?: QueryOptions) => Promise<'TX' | 'RX' | 'UNKNOWN' | null>`
258
302
  - `readBandEdges(options?: QueryOptions) => Promise<Buffer|null>`
259
303
 
304
+ #### Scope / Spectrum
305
+
306
+ - `scope: IcomScopeService` — Standalone scope service object that can be reused with other CI‑V transport paths in the future
307
+ - `enableScope() => Promise<void>` — Send the minimal command sequence to enable basic scope output
308
+ - `disableScope() => Promise<void>` — Send the minimal command sequence to disable scope output
309
+ - `waitForScopeFrame(options?: QueryOptions) => Promise<IcomScopeFrame | null>` — Wait for the next complete scope frame
310
+
311
+ `IcomScopeFrame` shape:
312
+
313
+ ```ts
314
+ interface IcomScopeFrame {
315
+ valid: boolean;
316
+ receiver: 0 | 1;
317
+ sequence: number;
318
+ sequenceMax: number;
319
+ mode: 0 | 1 | 2 | 3;
320
+ outOfRange: boolean;
321
+ startFreqHz: number;
322
+ endFreqHz: number;
323
+ pixels: Uint8Array;
324
+ rawCivPayloads: Buffer[];
325
+ transport: 'lan-civ' | 'serial';
326
+ }
327
+ ```
328
+
329
+ Current implementation notes:
330
+
331
+ - Currently implements only the basic on/off controls and `0x27 00 00` scope data capture
332
+ - The parsing layer is decoupled from the UDP session layer and only depends on complete CI‑V frames
333
+ - Frequency fields are currently parsed with `freqLen=5` by default
334
+ - LAN aggregate waterfall payload splitting is not implemented yet; standard segment input is supported
335
+ - The `scope` logic is designed to be reusable for future serial CI‑V or Hamlib CI‑V integration
336
+
337
+ #### Antenna Tuner (ATU)
338
+
339
+ - `readTunerStatus(options?: QueryOptions) => Promise<{ raw: number; state: 'OFF'|'ON'|'TUNING' } | null>` — Read tuner status (CI‑V 0x1A/0x00)
340
+ - `setTunerEnabled(enabled: boolean) => Promise<void>` — Enable/disable internal tuner (CI‑V 0x1A/0x01)
341
+ - `startManualTune() => Promise<void>` — Trigger one manual tune cycle (CI‑V 0x1A/0x02/0x00)
342
+
260
343
  #### Meters & Levels
261
344
 
262
345
  **Reception Meters** (available anytime):
@@ -383,6 +466,23 @@ await rig.setConnectorDataMode('WLAN');
383
466
  // Or numeric: await rig.setConnectorDataMode(0x03);
384
467
 
385
468
  await rig.setConnectorWLanLevel(120); // Set WLAN audio level
469
+
470
+ // Scope capture
471
+ await rig.enableScope();
472
+ const scope = await rig.waitForScopeFrame({ timeout: 3000 });
473
+ if (scope) {
474
+ console.log(`Scope ${scope.startFreqHz}..${scope.endFreqHz}, ${scope.pixels.length} pixels`);
475
+ }
476
+ await rig.disableScope();
477
+
478
+ // Antenna tuner
479
+ const atu = await rig.readTunerStatus({ timeout: 2000 });
480
+ if (atu) {
481
+ console.log('ATU:', atu.state);
482
+ }
483
+
484
+ await rig.setTunerEnabled(true);
485
+ await rig.startManualTune();
386
486
  ```
387
487
 
388
488
  ## Design Notes
@@ -393,6 +493,7 @@ await rig.setConnectorWLanLevel(120); // Set WLAN audio level
393
493
  - Credentials use the same simple substitution cipher as FT8CN’s Android client (`passCode`).
394
494
  - The 0x90/0x50 handshake strictly follows FT8CN’s timing and endianness. We pre‑open local CIV/Audio sockets, reply with local ports on first 0x90, then set remote ports upon 0x50.
395
495
  - CIV/audio sub‑sessions each run their own Ping/Idle and (for CIV) OpenClose keep‑alive.
496
+ - Scope data is treated as CI‑V business payload, not as a separate UDP stream. `IcomControl` only bridges CI‑V frames into the reusable `IcomScopeService`.
396
497
 
397
498
  ### Endianness and parsing tips
398
499
 
@@ -418,33 +519,10 @@ ICOM_IP=192.168.31.253 ICOM_PORT=50001 ICOM_USER=icom ICOM_PASS=icomicom npm tes
418
519
  - Full token renewal loop and advanced status flag parsing simplified.
419
520
  - Audio receive/playback is library‑only; playback is up to the integrator.
420
521
  - Robust retransmit/multi‑retransmit handling can be extended.
522
+ - Scope support is currently limited to basic on/off commands plus standard `0x27 00 00` segment parsing.
523
+ - LAN aggregate waterfall payload splitting is not implemented yet.
524
+ - Scope control subcommands beyond basic enable/disable are not implemented yet.
421
525
 
422
526
  ## License
423
527
 
424
528
  MIT
425
- #### Antenna Tuner (ATU)
426
-
427
- - `readTunerStatus(options?: QueryOptions) => Promise<{ raw: number; state: 'OFF'|'ON'|'TUNING' } | null>` — 读取天调状态(CI‑V 0x1A/0x00)
428
- - `setTunerEnabled(enabled: boolean) => Promise<void>` — 开启/关闭内置天调(CI‑V 0x1A/0x01 00/01)
429
- - `startManualTune() => Promise<void>` — 触发一次手动调谐(相当于 [TUNE] 键,CI‑V 0x1A/0x02/0x00)
430
-
431
- 示例:
432
-
433
- ```ts
434
- // 读取天调状态
435
- const atu = await rig.readTunerStatus({ timeout: 2000 });
436
- if (atu) console.log('ATU:', atu.state); // OFF / ON / TUNING
437
-
438
- // 启用内置天调
439
- await rig.setTunerEnabled(true);
440
-
441
- // 触发一次手动调谐
442
- await rig.startManualTune();
443
-
444
- // 可选:轮询状态直到结束
445
- let status;
446
- do {
447
- await new Promise(r => setTimeout(r, 300));
448
- status = await rig.readTunerStatus({ timeout: 1000 });
449
- } while (status && status.state === 'TUNING');
450
- ```
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './types';
2
2
  export { IcomControl } from './rig/IcomControl';
3
+ export { IcomScopeService } from './scope/IcomScopeService';
4
+ export { IcomScopeCommands } from './scope/IcomScopeCommands';
3
5
  export { MODE_MAP, CONNECTOR_MODE_MAP, DEFAULT_CONTROLLER_ADDR, METER_THRESHOLDS, METER_CALIBRATION, getModeCode, getConnectorModeCode, getModeString, getConnectorModeString, getFilterString, rawToPowerPercent, rawToVoltage, rawToCurrent } from './rig/IcomConstants';
4
6
  export { parseTwoByteBcd, intToTwoByteBcd } from './utils/bcd';
5
7
  export { IcomRigCommands } from './rig/IcomRigCommands';
package/dist/index.js CHANGED
@@ -14,12 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.getDisconnectMessage = exports.ConnectionAbortedError = exports.setupBasicErrorProtection = exports.setupGlobalErrorHandlers = exports.AUDIO_RATE = exports.IcomRigCommands = exports.intToTwoByteBcd = exports.parseTwoByteBcd = exports.rawToCurrent = exports.rawToVoltage = exports.rawToPowerPercent = exports.getFilterString = exports.getConnectorModeString = exports.getModeString = exports.getConnectorModeCode = exports.getModeCode = exports.METER_CALIBRATION = exports.METER_THRESHOLDS = exports.DEFAULT_CONTROLLER_ADDR = exports.CONNECTOR_MODE_MAP = exports.MODE_MAP = exports.IcomControl = void 0;
17
+ exports.getDisconnectMessage = exports.ConnectionAbortedError = exports.setupBasicErrorProtection = exports.setupGlobalErrorHandlers = exports.AUDIO_RATE = exports.IcomRigCommands = exports.intToTwoByteBcd = exports.parseTwoByteBcd = exports.rawToCurrent = exports.rawToVoltage = exports.rawToPowerPercent = exports.getFilterString = exports.getConnectorModeString = exports.getModeString = exports.getConnectorModeCode = exports.getModeCode = exports.METER_CALIBRATION = exports.METER_THRESHOLDS = exports.DEFAULT_CONTROLLER_ADDR = exports.CONNECTOR_MODE_MAP = exports.MODE_MAP = exports.IcomScopeCommands = exports.IcomScopeService = exports.IcomControl = void 0;
18
18
  // Export types (includes ConnectionPhase, ConnectionMetrics, etc.)
19
19
  __exportStar(require("./types"), exports);
20
20
  // Export main class
21
21
  var IcomControl_1 = require("./rig/IcomControl");
22
22
  Object.defineProperty(exports, "IcomControl", { enumerable: true, get: function () { return IcomControl_1.IcomControl; } });
23
+ var IcomScopeService_1 = require("./scope/IcomScopeService");
24
+ Object.defineProperty(exports, "IcomScopeService", { enumerable: true, get: function () { return IcomScopeService_1.IcomScopeService; } });
25
+ var IcomScopeCommands_1 = require("./scope/IcomScopeCommands");
26
+ Object.defineProperty(exports, "IcomScopeCommands", { enumerable: true, get: function () { return IcomScopeCommands_1.IcomScopeCommands; } });
23
27
  // Export constants and enums
24
28
  var IcomConstants_1 = require("./rig/IcomConstants");
25
29
  Object.defineProperty(exports, "MODE_MAP", { enumerable: true, get: function () { return IcomConstants_1.MODE_MAP; } });
@@ -1,6 +1,7 @@
1
1
  import { IcomRigOptions, RigEventEmitter, IcomMode, ConnectorDataMode, SetModeOptions, QueryOptions, SwrReading, AlcReading, WlanLevelReading, LevelMeterReading, SquelchStatusReading, AudioSquelchReading, OvfStatusReading, PowerLevelReading, CompLevelReading, VoltageReading, CurrentReading, ConnectionState, ConnectionMonitorConfig, ConnectionPhase, ConnectionMetrics, DisconnectReason, DisconnectOptions, TunerStatusReading, LevelReading } from '../types';
2
2
  import { IcomCiv } from './IcomCiv';
3
3
  import { IcomAudio } from './IcomAudio';
4
+ import { IcomScopeService } from '../scope/IcomScopeService';
4
5
  export declare class IcomControl {
5
6
  private ev;
6
7
  private sess;
@@ -8,6 +9,7 @@ export declare class IcomControl {
8
9
  private audioSess;
9
10
  civ: IcomCiv;
10
11
  audio: IcomAudio;
12
+ scope: IcomScopeService;
11
13
  private options;
12
14
  private rigName;
13
15
  private macAddress;
@@ -73,6 +75,9 @@ export declare class IcomControl {
73
75
  */
74
76
  disconnect(options?: DisconnectOptions | DisconnectReason): Promise<void>;
75
77
  sendCiv(data: Buffer): void;
78
+ enableScope(): Promise<void>;
79
+ disableScope(): Promise<void>;
80
+ waitForScopeFrame(options?: QueryOptions): Promise<import("../types").IcomScopeFrame | null>;
76
81
  /**
77
82
  * Set PTT (Push-To-Talk) state
78
83
  * @param on - true to key transmitter, false to unkey
@@ -46,6 +46,8 @@ const IcomConstants_1 = require("./IcomConstants");
46
46
  const bcd_1 = require("../utils/bcd");
47
47
  const errors_1 = require("../utils/errors");
48
48
  const smeter_1 = require("../utils/smeter");
49
+ const IcomScopeCommands_1 = require("../scope/IcomScopeCommands");
50
+ const IcomScopeService_1 = require("../scope/IcomScopeService");
49
51
  class IcomControl {
50
52
  constructor(options) {
51
53
  this.ev = new events_1.EventEmitter();
@@ -92,6 +94,9 @@ class IcomControl {
92
94
  this.audioSess.open();
93
95
  this.civ = new IcomCiv_1.IcomCiv(this.civSess);
94
96
  this.audio = new IcomAudio_1.IcomAudio(this.audioSess);
97
+ this.scope = new IcomScopeService_1.IcomScopeService();
98
+ this.scope.on('scopeSegment', (segment) => this.ev.emit('scopeSegment', segment));
99
+ this.scope.on('scopeFrame', (frame) => this.ev.emit('scopeFrame', frame));
95
100
  }
96
101
  get events() { return this.ev; }
97
102
  // ============================================================================
@@ -510,6 +515,22 @@ class IcomControl {
510
515
  }
511
516
  }
512
517
  sendCiv(data) { this.civ.sendCivData(data); }
518
+ async enableScope() {
519
+ const ctrAddr = IcomConstants_1.DEFAULT_CONTROLLER_ADDR;
520
+ const rigAddr = this.civ.civAddress & 0xff;
521
+ this.sendCiv(IcomScopeCommands_1.IcomScopeCommands.setScopeDisplay(ctrAddr, rigAddr, true));
522
+ this.sendCiv(IcomScopeCommands_1.IcomScopeCommands.setScopeDataOutput(ctrAddr, rigAddr, true));
523
+ }
524
+ async disableScope() {
525
+ const ctrAddr = IcomConstants_1.DEFAULT_CONTROLLER_ADDR;
526
+ const rigAddr = this.civ.civAddress & 0xff;
527
+ this.sendCiv(IcomScopeCommands_1.IcomScopeCommands.setScopeDataOutput(ctrAddr, rigAddr, false));
528
+ this.sendCiv(IcomScopeCommands_1.IcomScopeCommands.setScopeDisplay(ctrAddr, rigAddr, false));
529
+ }
530
+ async waitForScopeFrame(options) {
531
+ const timeoutMs = options?.timeout ?? 3000;
532
+ return this.scope.waitForScopeFrame(timeoutMs);
533
+ }
513
534
  /**
514
535
  * Set PTT (Push-To-Talk) state
515
536
  * @param on - true to key transmitter, false to unkey
@@ -1514,6 +1535,7 @@ class IcomControl {
1514
1535
  this.civAssembleBuf = this.civAssembleBuf.subarray(end + 1);
1515
1536
  // Emit event
1516
1537
  this.ev.emit('civFrame', frame);
1538
+ this.scope.handleCivFrame(frame, 'lan-civ');
1517
1539
  // Continue loop in case multiple frames are in buffer
1518
1540
  }
1519
1541
  }
@@ -0,0 +1,4 @@
1
+ export declare const IcomScopeCommands: {
2
+ setScopeDataOutput(ctrAddr: number, rigAddr: number, enabled: boolean): Buffer;
3
+ setScopeDisplay(ctrAddr: number, rigAddr: number, enabled: boolean): Buffer;
4
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IcomScopeCommands = void 0;
4
+ exports.IcomScopeCommands = {
5
+ setScopeDataOutput(ctrAddr, rigAddr, enabled) {
6
+ return Buffer.from([
7
+ 0xfe, 0xfe, rigAddr & 0xff, ctrAddr & 0xff, 0x27, 0x11, enabled ? 0x01 : 0x00, 0xfd
8
+ ]);
9
+ },
10
+ setScopeDisplay(ctrAddr, rigAddr, enabled) {
11
+ return Buffer.from([
12
+ 0xfe, 0xfe, rigAddr & 0xff, ctrAddr & 0xff, 0x27, 0x10, enabled ? 0x01 : 0x00, 0xfd
13
+ ]);
14
+ }
15
+ };
@@ -0,0 +1,5 @@
1
+ import { IcomScopeSegmentInfo } from '../types';
2
+ export declare function isScopeFrame(frame: Buffer): boolean;
3
+ export declare function bcdByteToInt(v: number): number;
4
+ export declare function parseIcomBcdFreqLE(bytes: Buffer): number;
5
+ export declare function parseScopeSegment(frame: Buffer, transport: 'lan-civ' | 'serial', freqLen?: number): IcomScopeSegmentInfo | null;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isScopeFrame = isScopeFrame;
4
+ exports.bcdByteToInt = bcdByteToInt;
5
+ exports.parseIcomBcdFreqLE = parseIcomBcdFreqLE;
6
+ exports.parseScopeSegment = parseScopeSegment;
7
+ const SCOPE_PREFIX = [0x27, 0x00, 0x00];
8
+ function isScopeFrame(frame) {
9
+ if (frame.length < 9)
10
+ return false;
11
+ if (frame[0] !== 0xfe || frame[1] !== 0xfe)
12
+ return false;
13
+ if (frame[frame.length - 1] !== 0xfd)
14
+ return false;
15
+ return frame[4] === SCOPE_PREFIX[0] && frame[5] === SCOPE_PREFIX[1] && frame[6] === SCOPE_PREFIX[2];
16
+ }
17
+ function bcdByteToInt(v) {
18
+ return (v & 0x0f) + (((v >> 4) & 0x0f) * 10);
19
+ }
20
+ function parseIcomBcdFreqLE(bytes) {
21
+ let hz = 0;
22
+ let multiplier = 1;
23
+ for (const byte of bytes) {
24
+ hz += (byte & 0x0f) * multiplier;
25
+ multiplier *= 10;
26
+ hz += ((byte >> 4) & 0x0f) * multiplier;
27
+ multiplier *= 10;
28
+ }
29
+ return hz;
30
+ }
31
+ function parseScopeSegment(frame, transport, freqLen = 5) {
32
+ if (!isScopeFrame(frame))
33
+ return null;
34
+ const payload = frame.subarray(4, frame.length - 1);
35
+ if (payload.length < 5)
36
+ return null;
37
+ const sequence = bcdByteToInt(payload[3]);
38
+ const sequenceMax = bcdByteToInt(payload[4]);
39
+ if (sequence <= 0 || sequenceMax <= 0 || sequence > sequenceMax)
40
+ return null;
41
+ const segment = {
42
+ receiver: 0,
43
+ sequence,
44
+ sequenceMax,
45
+ rawCivPayload: Buffer.from(payload),
46
+ transport
47
+ };
48
+ if (sequence === 1) {
49
+ const minimumHeaderLength = 3 + 2 + (freqLen * 2) + 1;
50
+ if (payload.length < minimumHeaderLength)
51
+ return null;
52
+ const mode = payload[5];
53
+ const primaryFreq = parseIcomBcdFreqLE(payload.subarray(6, 6 + freqLen));
54
+ const secondaryFreq = parseIcomBcdFreqLE(payload.subarray(6 + freqLen, 6 + (freqLen * 2)));
55
+ const outOfRange = payload[6 + (freqLen * 2)] !== 0x00;
56
+ let startFreqHz = primaryFreq;
57
+ let endFreqHz = secondaryFreq;
58
+ if (mode === 0) {
59
+ startFreqHz = Math.max(0, primaryFreq - secondaryFreq);
60
+ endFreqHz = primaryFreq + secondaryFreq;
61
+ }
62
+ segment.mode = mode;
63
+ segment.outOfRange = outOfRange;
64
+ segment.startFreqHz = startFreqHz;
65
+ segment.endFreqHz = endFreqHz;
66
+ const pixelOffset = 7 + (freqLen * 2);
67
+ segment.pixels = new Uint8Array(payload.subarray(pixelOffset));
68
+ return segment;
69
+ }
70
+ segment.pixels = new Uint8Array(payload.subarray(5));
71
+ return segment;
72
+ }
@@ -0,0 +1,17 @@
1
+ import { EventEmitter } from 'events';
2
+ import { IcomScopeFrame, IcomScopeSegmentInfo, IcomScopeTransport } from '../types';
3
+ export interface IcomScopeServiceOptions {
4
+ assemblyTimeoutMs?: number;
5
+ freqLen?: number;
6
+ }
7
+ export declare class IcomScopeService extends EventEmitter {
8
+ private readonly assemblyTimeoutMs;
9
+ private readonly freqLen;
10
+ private readonly assemblies;
11
+ constructor(options?: IcomScopeServiceOptions);
12
+ handleCivFrame(frame: Buffer, transport: IcomScopeTransport): IcomScopeFrame | null;
13
+ handleScopeSegment(segment: IcomScopeSegmentInfo): IcomScopeFrame | null;
14
+ waitForScopeFrame(timeoutMs?: number): Promise<IcomScopeFrame | null>;
15
+ private cleanupExpiredAssemblies;
16
+ private concatChunks;
17
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IcomScopeService = void 0;
4
+ const events_1 = require("events");
5
+ const IcomScopeParser_1 = require("./IcomScopeParser");
6
+ class IcomScopeService extends events_1.EventEmitter {
7
+ constructor(options) {
8
+ super();
9
+ this.assemblies = new Map();
10
+ this.assemblyTimeoutMs = options?.assemblyTimeoutMs ?? 500;
11
+ this.freqLen = options?.freqLen ?? 5;
12
+ }
13
+ handleCivFrame(frame, transport) {
14
+ const segment = (0, IcomScopeParser_1.parseScopeSegment)(frame, transport, this.freqLen);
15
+ if (!segment)
16
+ return null;
17
+ return this.handleScopeSegment(segment);
18
+ }
19
+ handleScopeSegment(segment) {
20
+ this.cleanupExpiredAssemblies();
21
+ this.emit('scopeSegment', segment);
22
+ const key = segment.receiver;
23
+ const now = Date.now();
24
+ if (segment.sequence === 1) {
25
+ this.assemblies.set(key, {
26
+ receiver: segment.receiver,
27
+ expectedMax: segment.sequenceMax,
28
+ mode: segment.mode,
29
+ startFreqHz: segment.startFreqHz,
30
+ endFreqHz: segment.endFreqHz,
31
+ outOfRange: segment.outOfRange,
32
+ chunks: segment.pixels ? [segment.pixels] : [],
33
+ rawCivPayloads: [Buffer.from(segment.rawCivPayload)],
34
+ updatedAt: now
35
+ });
36
+ }
37
+ else {
38
+ const state = this.assemblies.get(key);
39
+ if (!state)
40
+ return null;
41
+ if (state.expectedMax !== segment.sequenceMax) {
42
+ this.assemblies.delete(key);
43
+ return null;
44
+ }
45
+ state.chunks.push(segment.pixels ?? new Uint8Array());
46
+ state.rawCivPayloads.push(Buffer.from(segment.rawCivPayload));
47
+ state.updatedAt = now;
48
+ }
49
+ const state = this.assemblies.get(key);
50
+ if (!state)
51
+ return null;
52
+ if (segment.sequence !== state.expectedMax)
53
+ return null;
54
+ if (state.mode === undefined || state.startFreqHz === undefined || state.endFreqHz === undefined || state.outOfRange === undefined) {
55
+ this.assemblies.delete(key);
56
+ return null;
57
+ }
58
+ const pixels = this.concatChunks(state.chunks);
59
+ const frame = {
60
+ valid: true,
61
+ receiver: state.receiver,
62
+ sequence: segment.sequence,
63
+ sequenceMax: state.expectedMax,
64
+ mode: state.mode,
65
+ outOfRange: state.outOfRange,
66
+ startFreqHz: state.startFreqHz,
67
+ endFreqHz: state.endFreqHz,
68
+ pixels,
69
+ rawCivPayloads: state.rawCivPayloads.map((payload) => Buffer.from(payload)),
70
+ transport: segment.transport
71
+ };
72
+ this.assemblies.delete(key);
73
+ this.emit('scopeFrame', frame);
74
+ return frame;
75
+ }
76
+ async waitForScopeFrame(timeoutMs = 3000) {
77
+ return new Promise((resolve) => {
78
+ let done = false;
79
+ const onFrame = (frame) => {
80
+ done = true;
81
+ this.off('scopeFrame', onFrame);
82
+ resolve(frame);
83
+ };
84
+ this.on('scopeFrame', onFrame);
85
+ setTimeout(() => {
86
+ if (done)
87
+ return;
88
+ this.off('scopeFrame', onFrame);
89
+ resolve(null);
90
+ }, timeoutMs);
91
+ });
92
+ }
93
+ cleanupExpiredAssemblies() {
94
+ const now = Date.now();
95
+ for (const [key, state] of this.assemblies.entries()) {
96
+ if (now - state.updatedAt > this.assemblyTimeoutMs) {
97
+ this.assemblies.delete(key);
98
+ }
99
+ }
100
+ }
101
+ concatChunks(chunks) {
102
+ const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
103
+ const out = new Uint8Array(total);
104
+ let offset = 0;
105
+ for (const chunk of chunks) {
106
+ out.set(chunk, offset);
107
+ offset += chunk.length;
108
+ }
109
+ return out;
110
+ }
111
+ }
112
+ exports.IcomScopeService = IcomScopeService;
package/dist/types.d.ts CHANGED
@@ -39,6 +39,8 @@ export interface IcomRigEvents {
39
39
  capabilities: (c: CapabilitiesInfo) => void;
40
40
  civ: (data: Buffer) => void;
41
41
  civFrame: (frame: Buffer) => void;
42
+ scopeSegment: (segment: IcomScopeSegmentInfo) => void;
43
+ scopeFrame: (frame: IcomScopeFrame) => void;
42
44
  audio: (frame: AudioFrame) => void;
43
45
  error: (err: Error) => void;
44
46
  connectionLost: (info: ConnectionLostInfo) => void;
@@ -82,6 +84,32 @@ export interface QueryOptions {
82
84
  */
83
85
  timeout?: number;
84
86
  }
87
+ export type IcomScopeTransport = 'lan-civ' | 'serial';
88
+ export interface IcomScopeSegmentInfo {
89
+ receiver: 0 | 1;
90
+ sequence: number;
91
+ sequenceMax: number;
92
+ mode?: 0 | 1 | 2 | 3;
93
+ outOfRange?: boolean;
94
+ startFreqHz?: number;
95
+ endFreqHz?: number;
96
+ pixels?: Uint8Array;
97
+ rawCivPayload: Buffer;
98
+ transport: IcomScopeTransport;
99
+ }
100
+ export interface IcomScopeFrame {
101
+ valid: boolean;
102
+ receiver: 0 | 1;
103
+ sequence: number;
104
+ sequenceMax: number;
105
+ mode: 0 | 1 | 2 | 3;
106
+ outOfRange: boolean;
107
+ startFreqHz: number;
108
+ endFreqHz: number;
109
+ pixels: Uint8Array;
110
+ rawCivPayloads: Buffer[];
111
+ transport: IcomScopeTransport;
112
+ }
85
113
  /**
86
114
  * Result of a meter reading operation (SWR, ALC, etc.)
87
115
  * @deprecated Use specific types like SwrReading, AlcReading instead
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "icom-wlan-node",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Icom WLAN (CI‑V, audio) protocol implementation for Node.js/TypeScript.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,16 +10,27 @@
10
10
  "test": "jest",
11
11
  "prepublishOnly": "npm run build",
12
12
  "lint": "eslint .",
13
- "diagnose": "tsx scripts/diagnose.ts"
13
+ "diagnose": "tsx scripts/diagnose.ts",
14
+ "test:scope:real": "tsx scripts/test-scope-real.ts"
14
15
  },
15
- "keywords": ["icom", "civ", "cat", "udp", "hamradio", "wlan", "audio"],
16
+ "keywords": [
17
+ "icom",
18
+ "civ",
19
+ "cat",
20
+ "udp",
21
+ "hamradio",
22
+ "wlan",
23
+ "audio"
24
+ ],
16
25
  "author": "boybook",
17
26
  "license": "MIT",
18
27
  "repository": {
19
28
  "type": "git",
20
29
  "url": "https://github.com/boybook/icom-wlan-node.git"
21
30
  },
22
- "files": ["dist"],
31
+ "files": [
32
+ "dist"
33
+ ],
23
34
  "devDependencies": {
24
35
  "@types/jest": "^29.5.12",
25
36
  "@types/node": "^20.10.6",