Skip to content

Commit

Permalink
feat: add method to return Zniffer capture as Buffer (#6836)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominic Griesel <[email protected]>
  • Loading branch information
raman325 and AlCalzone authored May 15, 2024
1 parent 2246fda commit 43c4299
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/api/zniffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ When done, make sure to destroy the Zniffer instance to release the serial port
await zniffer.destroy();
```

Captured frames can also be returned as a `Buffer` in the `.zlf` format using the `getCaptureAsZLFBuffer` method:

```ts
await zniffer.getCaptureAsZLFBuffer();
```

## Frequency selection

The configured frequency of the Zniffer has to match the frequency of the Z-Wave network it is capturing. Zniffers based on 700/800 series firmware support frequencies that match the `ZnifferRegion` enum:
Expand Down
16 changes: 10 additions & 6 deletions packages/zwave-js/src/lib/zniffer/Zniffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,17 +851,21 @@ export class Zniffer extends TypedEventEmitter<ZnifferEventCallbacks> {
this._capturedFrames = [];
}

/** Saves the captured frames in a `.zlf` file that can be read by the official Zniffer application. */
public async saveCaptureToFile(filePath: string): Promise<void> {
/** Get the captured frames in the official Zniffer application format. */
public getCaptureAsZLFBuffer(): Buffer {
// Mimics the current Zniffer software, without using features like sessions and comments
const header = Buffer.alloc(2048, 0);
header[0] = 0x68; // zniffer version
header.writeUInt16BE(0x2312, 0x07fe); // checksum
return Buffer.concat([
header,
...this._capturedFrames.map(captureToZLFEntry),
]);
}

await fs.writeFile(filePath, header);
for (const frame of this._capturedFrames) {
await fs.appendFile(filePath, captureToZLFEntry(frame));
}
/** Saves the captured frames in a `.zlf` file that can be read by the official Zniffer application. */
public async saveCaptureToFile(filePath: string): Promise<void> {
await fs.writeFile(filePath, this.getCaptureAsZLFBuffer());
}

/**
Expand Down

0 comments on commit 43c4299

Please sign in to comment.