{"version":3,"file":"index.mjs","names":[],"sources":["../../src/device/device.ts"],"sourcesContent":["/* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD */\n/* SPDX-License-Identifier: Apache-2.0 */\n\n/**\n * Device service module for JS apps — wraps `brookesia_service_device` RPC.\n *\n * @example\n * import device from '@brookesia/service/device';\n * if (device.available()) {\n *   brookesia.print(device.getBoardInfo());\n *   brookesia.print(device.getPowerBatteryState());\n *   device.close();\n * }\n */\n\nimport {\n  closeDevice,\n  getBoardInfo,\n  getCameraDeviceInfos,\n  getCapabilities,\n  getNetworkConnectivityInfo,\n  getPowerBatteryChargeConfig,\n  getPowerBatteryInfo,\n  getPowerBatteryState,\n  isDeviceAvailable,\n  openDevice,\n  setPowerBatteryChargeConfig,\n  setPowerBatteryChargingEnabled,\n  type DeviceClientOptions,\n  type DeviceHandle,\n} from './device-client.ts';\nimport type {\n  DeviceBoardInfo,\n  DeviceCameraInfo,\n  DeviceCapability,\n  DeviceNetworkConnectivityInfo,\n  DevicePowerBatteryChargeConfig,\n  DevicePowerBatteryInfo,\n  DevicePowerBatteryState,\n} from './device-types.ts';\n\nexport type {\n  DeviceBoardInfo,\n  DeviceCameraInfo,\n  DeviceCapability,\n  DeviceNetworkConnectivityInfo,\n  DevicePowerBatteryChargeConfig,\n  DevicePowerBatteryInfo,\n  DevicePowerBatteryState,\n};\n\nexport type DeviceModuleOptions = DeviceClientOptions;\n\ntype State = {\n  handle: DeviceHandle | null;\n  options: DeviceModuleOptions;\n};\n\nconst state: State = { handle: null, options: {} };\n\nfunction handle(): DeviceHandle {\n  if (state.handle !== null) {\n    return state.handle;\n  }\n  if (!isDeviceAvailable(state.options)) {\n    throw new Error('Device service is not available');\n  }\n  state.handle = openDevice(state.options);\n  return state.handle;\n}\n\n/**\n * Inject a mock RPC host and reset the cached service handle (vitest / Node only).\n *\n * @param options - Client options, typically `{ host }` from `@brookesia/service/hosts/device`\n */\nexport function configure(options: DeviceModuleOptions): void {\n  if (state.handle !== null) {\n    closeDevice(state.handle, state.options);\n  }\n  state.handle = null;\n  state.options = options;\n}\n\nconst device = {\n  /** @returns `true` when the Device service is registered on the device */\n  available(): boolean {\n    return isDeviceAvailable(state.options);\n  },\n\n  /** Release the Device service binding. */\n  close(): void {\n    if (state.handle === null) {\n      return;\n    }\n    closeDevice(state.handle, state.options);\n    state.handle = null;\n  },\n\n  /** @returns HAL capability flags (audio, display, camera, …) */\n  getCapabilities(): DeviceCapability[] {\n    return getCapabilities(handle(), state.options);\n  },\n\n  /** @returns Board name, chip model, firmware version, etc. */\n  getBoardInfo(): DeviceBoardInfo {\n    return getBoardInfo(handle(), state.options);\n  },\n\n  /**\n   * @returns Camera device list\n   * @throws When no camera HAL is present on the board\n   */\n  getCameraDeviceInfos(): DeviceCameraInfo[] {\n    return getCameraDeviceInfos(handle(), state.options);\n  },\n\n  /** @returns Per-interface network connectivity status */\n  getNetworkConnectivityInfo(): DeviceNetworkConnectivityInfo[] {\n    return getNetworkConnectivityInfo(handle(), state.options);\n  },\n\n  /** @returns Static battery metadata (capacity, chemistry, …) */\n  getPowerBatteryInfo(): DevicePowerBatteryInfo {\n    return getPowerBatteryInfo(handle(), state.options);\n  },\n\n  /** @returns Live battery level, voltage, charging state */\n  getPowerBatteryState(): DevicePowerBatteryState {\n    return getPowerBatteryState(handle(), state.options);\n  },\n\n  /** @returns Charge current / voltage limits */\n  getPowerBatteryChargeConfig(): DevicePowerBatteryChargeConfig {\n    return getPowerBatteryChargeConfig(handle(), state.options);\n  },\n\n  /**\n   * Update charge configuration.\n   *\n   * @param config - Charge limits from {@link getPowerBatteryChargeConfig}\n   */\n  setPowerBatteryChargeConfig(config: DevicePowerBatteryChargeConfig): void {\n    setPowerBatteryChargeConfig(handle(), config, state.options);\n  },\n\n  /**\n   * Enable or disable battery charging.\n   *\n   * @param enabled - `true` to allow charging\n   */\n  setPowerBatteryChargingEnabled(enabled: boolean): void {\n    setPowerBatteryChargingEnabled(handle(), enabled, state.options);\n  },\n};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