// SPDX-License-Identifier: MIT /** * Connection types, errors, and events * Centralized type definitions for connection operations */ // ============================================ // Connection Events // ============================================ // Connection event constants export const EVENT_CONNECTION_STATE_CHANGED = 'EVENT_CONNECTION_STATE_CHANGED' // Connection event type export type ConnectionEventType = typeof EVENT_CONNECTION_STATE_CHANGED // Connection state type export type ConnectionState = 'connecting' | 'connected' | 'disconnected' // Event payload interfaces export interface ConnectionEventPayload { [EVENT_CONNECTION_STATE_CHANGED]: { address: string state: ConnectionState reason?: string } } // ============================================ // Connection Errors // ============================================ /** * Connection error codes - matches native error codes */ export enum ConnectionErrorCode { BLUETOOTH_NOT_ENABLED = 'BLUETOOTH_NOT_ENABLED', BLUETOOTH_NOT_SUPPORTED = 'BLUETOOTH_NOT_SUPPORTED', DEVICE_NOT_FOUND = 'DEVICE_NOT_FOUND', DEVICE_BUSY = 'DEVICE_BUSY', CONNECTION_FAILED = 'CONNECTION_FAILED', CONNECTION_LOST = 'CONNECTION_LOST', WRITE_FAILED = 'WRITE_FAILED', PERMISSION_DENIED = 'PERMISSION_DENIED', TIMEOUT = 'TIMEOUT', INVALID_ADDRESS = 'INVALID_ADDRESS', UNSUPPORTED_DEVICE = 'UNSUPPORTED_DEVICE', NETWORK_ERROR = 'NETWORK_ERROR', } /** * Connection error class */ export class ConnectionError extends Error { code: ConnectionErrorCode constructor(code: ConnectionErrorCode, message: string) { super(message) this.code = code this.name = 'ConnectionError' // Maintains proper stack trace for where error was thrown if (Error.captureStackTrace) { Error.captureStackTrace(this, ConnectionError) } } /** * Helper to create error from native error object */ static fromNative(error: any): ConnectionError { // Extract error code from native error const nativeCode = error.userInfo?.code || error.code || 'CONNECTION_FAILED' // Map to ConnectionErrorCode const code = Object.values(ConnectionErrorCode).includes(nativeCode as ConnectionErrorCode) ? (nativeCode as ConnectionErrorCode) : ConnectionErrorCode.CONNECTION_FAILED // Extract message const message = error.userInfo?.message || error.message || 'Connection failed' return new ConnectionError(code, message) } } // ============================================ // Connection Types // ============================================ /** * Test connection result */ export interface TestConnectionResult { success: boolean deviceName?: string deviceType?: string error?: { code: string message: string retryable?: boolean suggestion?: string } }