{"version":3,"file":"index.mjs","names":[],"sources":["../../src/ota/ota.ts"],"sourcesContent":["/* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD */\n/* SPDX-License-Identifier: Apache-2.0 */\n\n/**\n * OTA service module for JS apps — wraps `brookesia_service_ota` RPC.\n *\n * @example\n * import ota from '@brookesia/service/ota';\n * if (ota.available()) {\n *   const result = ota.checkUpdate({ type: 'LocalManifest', uri: '/littlefs/ota.json' });\n *   brookesia.print(result.update_available);\n *   ota.close();\n * }\n */\n\nimport {\n  cancelUpdate,\n  checkUpdate,\n  closeOta,\n  confirmPendingBoot,\n  getAssetsState,\n  getOtaHistory,\n  getOtaProgress,\n  getOtaState,\n  getStorageConfig,\n  isOtaAvailable,\n  openOta,\n  resetOtaFailure,\n  setStorageConfig,\n  startUpdate,\n  type OtaClientOptions,\n  type OtaHandle,\n} from './ota-client.ts';\nimport type {\n  OtaArtifactInfo,\n  OtaArtifactType,\n  OtaAssetsState,\n  OtaCheckResult,\n  OtaErrorCode,\n  OtaHistoryEntry,\n  OtaManifestInfo,\n  OtaProgress,\n  OtaProgressOperation,\n  OtaProgressUnit,\n  OtaSource,\n  OtaSourceType,\n  OtaState,\n  OtaStorageConfig,\n  OtaTargetIdentity,\n  OtaTargetInfo,\n  OtaVersionDecision,\n} from './ota-types.ts';\n\nexport type {\n  OtaArtifactInfo,\n  OtaArtifactType,\n  OtaAssetsState,\n  OtaCheckResult,\n  OtaErrorCode,\n  OtaHistoryEntry,\n  OtaManifestInfo,\n  OtaProgress,\n  OtaProgressOperation,\n  OtaProgressUnit,\n  OtaSource,\n  OtaSourceType,\n  OtaState,\n  OtaStorageConfig,\n  OtaTargetIdentity,\n  OtaTargetInfo,\n  OtaVersionDecision,\n};\n\nexport type OtaModuleOptions = OtaClientOptions;\n\ntype State = {\n  handle: OtaHandle | null;\n  options: OtaModuleOptions;\n};\n\nconst state: State = { handle: null, options: {} };\n\nfunction handle(): OtaHandle {\n  if (state.handle !== null) {\n    return state.handle;\n  }\n  if (!isOtaAvailable(state.options)) {\n    throw new Error('OTA service is not available');\n  }\n  state.handle = openOta(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/ota`\n */\nexport function configure(options: OtaModuleOptions): void {\n  if (state.handle !== null) {\n    closeOta(state.handle, state.options);\n  }\n  state.handle = null;\n  state.options = options;\n}\n\nconst ota = {\n  /** @returns `true` when OTA service is registered (not linked in PC simulator by default) */\n  available(): boolean {\n    return isOtaAvailable(state.options);\n  },\n\n  /** Release the OTA service binding. */\n  close(): void {\n    if (state.handle === null) {\n      return;\n    }\n    closeOta(state.handle, state.options);\n    state.handle = null;\n  },\n\n  /**\n   * Check for available updates from a manifest source.\n   *\n   * @param source - `{ type: 'LocalManifest' | 'HttpManifest', uri }`\n   * @returns Decision with `update_available`, version info\n   */\n  checkUpdate(source: OtaSource): OtaCheckResult {\n    return checkUpdate(handle(), source, state.options);\n  },\n\n  /**\n   * Start downloading / applying an update.\n   *\n   * @param source - Same shape as {@link checkUpdate}\n   * @returns Initial progress snapshot\n   */\n  startUpdate(source: OtaSource): OtaProgress {\n    return startUpdate(handle(), source, state.options);\n  },\n\n  /** Cancel an in-progress update. */\n  cancelUpdate(): void {\n    cancelUpdate(handle(), state.options);\n  },\n\n  /** @returns Current OTA state and progress */\n  getState(): OtaProgress {\n    return getOtaState(handle(), state.options);\n  },\n\n  /** @returns Detailed progress (bytes, percent, operation) */\n  getProgress(): OtaProgress {\n    return getOtaProgress(handle(), state.options);\n  },\n\n  /** @returns Past OTA history entries */\n  getHistory(): OtaHistoryEntry[] {\n    return getOtaHistory(handle(), state.options);\n  },\n\n  /** Confirm boot into a pending update partition after reboot. */\n  confirmPendingBoot(): void {\n    confirmPendingBoot(handle(), state.options);\n  },\n\n  /** @returns Installed assets / resource bundle version state */\n  getAssetsState(): OtaAssetsState {\n    return getAssetsState(handle(), state.options);\n  },\n\n  /** @returns OTA download / staging storage configuration */\n  getStorageConfig(): OtaStorageConfig {\n    return getStorageConfig(handle(), state.options);\n  },\n\n  /**\n   * Update OTA storage paths / quotas.\n   *\n   * @param config - Storage layout for OTA artifacts\n   */\n  setStorageConfig(config: OtaStorageConfig): void {\n    setStorageConfig(handle(), config, state.options);\n  },\n\n  /** Clear failure state after a failed update attempt. */\n  resetFailure(): void {\n    resetOtaFailure(handle(), state.options);\n  },\n};\n\nexport default ota;\n"],"mappings":";;;;;;;;;;;;;AAgFA,MAAM,QAAe;CAAE,QAAQ;CAAM,SAAS,CAAC;AAAE;AAEjD,SAAS,SAAoB;CAC3B,IAAI,MAAM,WAAW,MACnB,OAAO,MAAM;CAEf,IAAI,CAAC,eAAe,MAAM,OAAO,GAC/B,MAAM,IAAI,MAAM,8BAA8B;CAEhD,MAAM,SAAS,QAAQ,MAAM,OAAO;CACpC,OAAO,MAAM;AACf;;;;;;AAOA,SAAgB,UAAU,SAAiC;CACzD,IAAI,MAAM,WAAW,MACnB,SAAS,MAAM,QAAQ,MAAM,OAAO;CAEtC,MAAM,SAAS;CACf,MAAM,UAAU;AAClB;AAEA,MAAM,MAAM;;CAEV,YAAqB;EACnB,OAAO,eAAe,MAAM,OAAO;CACrC;;CAGA,QAAc;EACZ,IAAI,MAAM,WAAW,MACnB;EAEF,SAAS,MAAM,QAAQ,MAAM,OAAO;EACpC,MAAM,SAAS;CACjB;;;;;;;CAQA,YAAY,QAAmC;EAC7C,OAAO,YAAY,OAAO,GAAG,QAAQ,MAAM,OAAO;CACpD;;;;;;;CAQA,YAAY,QAAgC;EAC1C,OAAO,YAAY,OAAO,GAAG,QAAQ,MAAM,OAAO;CACpD;;CAGA,eAAqB;EACnB,aAAa,OAAO,GAAG,MAAM,OAAO;CACtC;;CAGA,WAAwB;EACtB,OAAO,YAAY,OAAO,GAAG,MAAM,OAAO;CAC5C;;CAGA,cAA2B;EACzB,OAAO,eAAe,OAAO,GAAG,MAAM,OAAO;CAC/C;;CAGA,aAAgC;EAC9B,OAAO,cAAc,OAAO,GAAG,MAAM,OAAO;CAC9C;;CAGA,qBAA2B;EACzB,mBAAmB,OAAO,GAAG,MAAM,OAAO;CAC5C;;CAGA,iBAAiC;EAC/B,OAAO,eAAe,OAAO,GAAG,MAAM,OAAO;CAC/C;;CAGA,mBAAqC;EACnC,OAAO,iBAAiB,OAAO,GAAG,MAAM,OAAO;CACjD;;;;;;CAOA,iBAAiB,QAAgC;EAC/C,iBAAiB,OAAO,GAAG,QAAQ,MAAM,OAAO;CAClD;;CAGA,eAAqB;EACnB,gBAAgB,OAAO,GAAG,MAAM,OAAO;CACzC;AACF"}