/** * Protocol Handler - Implements Device-App Protocol for recording transfer */ import { Buffer } from 'buffer'; import type { TriggerDeviceUploadResponse } from '../ble/parsers'; import type { StorageInfo } from '../models/Device'; import type { DeviceRecording } from '../models/Recording'; /** * Transfer progress callback */ export type TransferProgressCallback = (bytesReceived: number, totalBytes?: number) => void; /** * Protocol Handler class */ export declare class ProtocolHandler { private bleManager; private activeTransfers; constructor(); /** * Get storage info from device */ getStorageInfo(deviceId: string): Promise; /** * List recordings on device */ listRecordings(deviceId: string): Promise; /** * Transfer a recording from device * Returns the audio data as a Buffer */ transferRecording(deviceId: string, recordingUuid: string, onProgress?: TransferProgressCallback): Promise<{ data: Buffer; e2eEncrypted: boolean; sha256?: string; }>; /** * Handle a transfer packet from device */ private handleTransferPacket; /** * Send an ACK packet to device */ private sendAck; /** * Assemble audio data from received packets. * * Plaintext path: concatenate chunks back-to-back. * * P10 encrypted path: prepend the streaming-AEAD header (ephemeral_pk[32] * + salt[4]); each accumulated chunk already includes its 2-byte * length prefix and 16-byte auth tag so the body matches the wire * format that `bleE2EService.decrypt()` parses server-side. */ private assembleAudioData; /** * Calculate CRC32 checksum */ private calculateCrc32; /** * Get CRC32 lookup table (lazy initialized) */ private crc32Table; private getCrc32Table; /** * Confirm sync to device (allows device to delete local copy) */ confirmSync(deviceId: string, recordingUuid: string): Promise; /** * Trigger device-side upload via WiFi/4G. * Sends opcode 0x03 to TRANSFER_CONTROL and waits for a 2-byte response * on TRANSFER_STATUS (byte[0]=0x03, byte[1]=status). * Returns null if firmware doesn't support this command (timeout). */ triggerDeviceUpload(deviceId: string): Promise; /** * Cancel an ongoing transfer */ cancelTransfer(deviceId: string, recordingUuid: string): Promise; /** * Check if a transfer is in progress */ isTransferInProgress(recordingUuid: string): boolean; /** * Stream a live recording transfer from device. * * Unlike transferRecording() which waits for EOF and returns a complete Buffer, * this method calls back on every DATA packet and handles PAUSED packets * (device caught up to recording write position, waiting for more audio). * * The transfer completes when an EOF packet is received (recording stopped * and all data has been sent). * * @param onData - called for each DATA packet with the audio chunk * @param onPaused - called when device sends PAUSED (caught up, more data coming) * @param onResumed - called when device resumes sending after PAUSED * @returns Promise that resolves with { totalBytes, checksum } on EOF */ streamTransfer(deviceId: string, recordingUuid: string, callbacks: { onData: (sequenceNumber: number, data: Buffer) => void; onPaused?: (bytesSent: number) => void; onResumed?: () => void; }): Promise<{ totalBytes: number; checksum: number; sha256?: string; }>; /** * Upload firmware binary to device via BLE. * Device writes the file to SD card as update.ufw, verifies CRC32, * then reboots to apply the update. * * Protocol: * 1. Send UPLOAD_START (0x08) with file size → wait for ready notification * 2. Send firmware data in chunks via RECORDING_TRANSFER (0x20 + seq + data) * 3. Send UPLOAD_VERIFY (0x09) with CRC32 → wait for verify notification * 4. Device reboots automatically on success */ uploadFirmware(deviceId: string, firmwareData: Buffer, onProgress?: (bytesSent: number, totalBytes: number) => void): Promise; /** * Clean up resources */ destroy(): void; } //# sourceMappingURL=ProtocolHandler.d.ts.map