18e745fdaSopenharmony_ci/*
28e745fdaSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License.
58e745fdaSopenharmony_ci * You may obtain a copy of the License at
68e745fdaSopenharmony_ci *
78e745fdaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88e745fdaSopenharmony_ci *
98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and
138e745fdaSopenharmony_ci * limitations under the License.
148e745fdaSopenharmony_ci */
158e745fdaSopenharmony_ci
168e745fdaSopenharmony_ciimport { AsyncCallback, Callback } from "./@ohos.base";
178e745fdaSopenharmony_ciimport connection from "./@ohos.net.connection";
188e745fdaSopenharmony_ciimport Context from "./application/Context";
198e745fdaSopenharmony_ci
208e745fdaSopenharmony_ci/**
218e745fdaSopenharmony_ci * Provides interfaces to discover DNS based services on a local network over Multicast DNS.
228e745fdaSopenharmony_ci * @namespace mdns
238e745fdaSopenharmony_ci * @syscap SystemCapability.Communication.NetManager.MDNS
248e745fdaSopenharmony_ci * @since 10
258e745fdaSopenharmony_ci */
268e745fdaSopenharmony_cideclare namespace mdns {
278e745fdaSopenharmony_ci  type NetAddress = connection.NetAddress;
288e745fdaSopenharmony_ci
298e745fdaSopenharmony_ci  /**
308e745fdaSopenharmony_ci   * Adds an mDNS service.
318e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
328e745fdaSopenharmony_ci   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
338e745fdaSopenharmony_ci   * @param { AsyncCallback<LocalServiceInfo> } callback Returns the callback of addLocalService.
348e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
358e745fdaSopenharmony_ci   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
368e745fdaSopenharmony_ci   * @throws { BusinessError } 2100003 - System internal error.
378e745fdaSopenharmony_ci   * @throws { BusinessError } 2204003 - Callback duplicated.
388e745fdaSopenharmony_ci   * @throws { BusinessError } 2204008 - Service instance duplicated.
398e745fdaSopenharmony_ci   * @throws { BusinessError } 2204010 - Send packet failed.
408e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
418e745fdaSopenharmony_ci   * @since 10
428e745fdaSopenharmony_ci   */
438e745fdaSopenharmony_ci  function addLocalService(context: Context, serviceInfo: LocalServiceInfo,
448e745fdaSopenharmony_ci                           callback: AsyncCallback<LocalServiceInfo>): void;
458e745fdaSopenharmony_ci
468e745fdaSopenharmony_ci  /**
478e745fdaSopenharmony_ci   * Adds an mDNS service.
488e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
498e745fdaSopenharmony_ci   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
508e745fdaSopenharmony_ci   * @returns { Promise<LocalServiceInfo> } The promise returned by the function.
518e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
528e745fdaSopenharmony_ci   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
538e745fdaSopenharmony_ci   * @throws { BusinessError } 2100003 - System internal error.
548e745fdaSopenharmony_ci   * @throws { BusinessError } 2204003 - Callback duplicated.
558e745fdaSopenharmony_ci   * @throws { BusinessError } 2204008 - Service instance duplicated.
568e745fdaSopenharmony_ci   * @throws { BusinessError } 2204010 - Send packet failed.
578e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
588e745fdaSopenharmony_ci   * @since 10
598e745fdaSopenharmony_ci   */
608e745fdaSopenharmony_ci  function addLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
618e745fdaSopenharmony_ci
628e745fdaSopenharmony_ci  /**
638e745fdaSopenharmony_ci   * Removes an mDNS service.
648e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
658e745fdaSopenharmony_ci   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
668e745fdaSopenharmony_ci   * @param { AsyncCallback<LocalServiceInfo> } callback Returns the callback of removeLocalService.
678e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
688e745fdaSopenharmony_ci   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
698e745fdaSopenharmony_ci   * @throws { BusinessError } 2100003 - System internal error.
708e745fdaSopenharmony_ci   * @throws { BusinessError } 2204002 - Callback not found.
718e745fdaSopenharmony_ci   * @throws { BusinessError } 2204008 - Service instance not found.
728e745fdaSopenharmony_ci   * @throws { BusinessError } 2204010 - Send packet failed.
738e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
748e745fdaSopenharmony_ci   * @since 10
758e745fdaSopenharmony_ci   */
768e745fdaSopenharmony_ci  function removeLocalService(context: Context, serviceInfo: LocalServiceInfo,
778e745fdaSopenharmony_ci                              callback: AsyncCallback<LocalServiceInfo>): void;
788e745fdaSopenharmony_ci
798e745fdaSopenharmony_ci  /**
808e745fdaSopenharmony_ci   * Removes an mDNS service.
818e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
828e745fdaSopenharmony_ci   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
838e745fdaSopenharmony_ci   * @returns { Promise<LocalServiceInfo> } The promise returned by the function.
848e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
858e745fdaSopenharmony_ci   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
868e745fdaSopenharmony_ci   * @throws { BusinessError } 2100003 - System internal error.
878e745fdaSopenharmony_ci   * @throws { BusinessError } 2204002 - Callback not found.
888e745fdaSopenharmony_ci   * @throws { BusinessError } 2204008 - Service instance not found.
898e745fdaSopenharmony_ci   * @throws { BusinessError } 2204010 - Send packet failed.
908e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
918e745fdaSopenharmony_ci   * @since 10
928e745fdaSopenharmony_ci   */
938e745fdaSopenharmony_ci  function removeLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
948e745fdaSopenharmony_ci
958e745fdaSopenharmony_ci  /**
968e745fdaSopenharmony_ci   * Create an mDNS based discovery service with context and serviceType.
978e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
988e745fdaSopenharmony_ci   * @param { string } serviceType The service type being discovered.
998e745fdaSopenharmony_ci   * @returns { DiscoveryService } the DiscoveryService of the createDiscoveryService.
1008e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
1018e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
1028e745fdaSopenharmony_ci   * @since 10
1038e745fdaSopenharmony_ci   */
1048e745fdaSopenharmony_ci  function createDiscoveryService(context: Context, serviceType: string): DiscoveryService;
1058e745fdaSopenharmony_ci
1068e745fdaSopenharmony_ci  /**
1078e745fdaSopenharmony_ci   * Resolves an mDNS service.
1088e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
1098e745fdaSopenharmony_ci   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
1108e745fdaSopenharmony_ci   * @param { AsyncCallback<LocalServiceInfo> } callback Returns the callback of resolveLocalService.
1118e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
1128e745fdaSopenharmony_ci   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
1138e745fdaSopenharmony_ci   * @throws { BusinessError } 2100003 - System internal error.
1148e745fdaSopenharmony_ci   * @throws { BusinessError } 2204003 - Callback duplicated.
1158e745fdaSopenharmony_ci   * @throws { BusinessError } 2204006 - Request timeout.
1168e745fdaSopenharmony_ci   * @throws { BusinessError } 2204010 - Send packet failed.
1178e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
1188e745fdaSopenharmony_ci   * @since 10
1198e745fdaSopenharmony_ci   */
1208e745fdaSopenharmony_ci  function resolveLocalService(context: Context, serviceInfo: LocalServiceInfo,
1218e745fdaSopenharmony_ci                               callback: AsyncCallback<LocalServiceInfo>): void;
1228e745fdaSopenharmony_ci
1238e745fdaSopenharmony_ci  /**
1248e745fdaSopenharmony_ci   * Resolves an mDNS service.
1258e745fdaSopenharmony_ci   * @param { Context } context Indicates the context of application or capability.
1268e745fdaSopenharmony_ci   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
1278e745fdaSopenharmony_ci   * @returns { Promise<LocalServiceInfo> } The promise returned by the function.
1288e745fdaSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error.
1298e745fdaSopenharmony_ci   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
1308e745fdaSopenharmony_ci   * @throws { BusinessError } 2100003 - System internal error.
1318e745fdaSopenharmony_ci   * @throws { BusinessError } 2204003 - Callback duplicated.
1328e745fdaSopenharmony_ci   * @throws { BusinessError } 2204006 - Request timeout.
1338e745fdaSopenharmony_ci   * @throws { BusinessError } 2204010 - Send packet failed.
1348e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
1358e745fdaSopenharmony_ci   * @since 10
1368e745fdaSopenharmony_ci   */
1378e745fdaSopenharmony_ci  function resolveLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
1388e745fdaSopenharmony_ci
1398e745fdaSopenharmony_ci  /**
1408e745fdaSopenharmony_ci   * Defines a DiscoveryService object for discovering mDNS services of the specified type.
1418e745fdaSopenharmony_ci   * @interface DiscoveryService
1428e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
1438e745fdaSopenharmony_ci   * @since 10
1448e745fdaSopenharmony_ci   */
1458e745fdaSopenharmony_ci  export interface DiscoveryService {
1468e745fdaSopenharmony_ci    /**
1478e745fdaSopenharmony_ci     * Enables listening for discoveryStart events of mDNS services.
1488e745fdaSopenharmony_ci     * @param { 'discoveryStart' } type Indicates Event name.
1498e745fdaSopenharmony_ci     * @param { Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }> } callback - the callback of on.
1508e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
1518e745fdaSopenharmony_ci     * @since 10
1528e745fdaSopenharmony_ci     */
1538e745fdaSopenharmony_ci    on(type: 'discoveryStart',
1548e745fdaSopenharmony_ci       callback: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void;
1558e745fdaSopenharmony_ci
1568e745fdaSopenharmony_ci    /**
1578e745fdaSopenharmony_ci     * Enables listening for discoveryStop events of mDNS services.
1588e745fdaSopenharmony_ci     * @param { 'discoveryStop' } type Indicates Event name.
1598e745fdaSopenharmony_ci     * @param { Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }> } callback - the callback of on.
1608e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
1618e745fdaSopenharmony_ci     * @since 10
1628e745fdaSopenharmony_ci     */
1638e745fdaSopenharmony_ci    on(type: 'discoveryStop',
1648e745fdaSopenharmony_ci       callback: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void;
1658e745fdaSopenharmony_ci
1668e745fdaSopenharmony_ci    /**
1678e745fdaSopenharmony_ci     * Enables listening for serviceFound events of mDNS services.
1688e745fdaSopenharmony_ci     * @param { 'serviceFound' } type Indicates Event name.
1698e745fdaSopenharmony_ci     * @param { Callback<LocalServiceInfo> } callback - the callback of on.
1708e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
1718e745fdaSopenharmony_ci     * @since 10
1728e745fdaSopenharmony_ci     */
1738e745fdaSopenharmony_ci    on(type: 'serviceFound', callback: Callback<LocalServiceInfo>): void;
1748e745fdaSopenharmony_ci
1758e745fdaSopenharmony_ci    /**
1768e745fdaSopenharmony_ci     * Enables listening for serviceLost events of mDNS services.
1778e745fdaSopenharmony_ci     * @param { 'serviceLost' } type Indicates Event name.
1788e745fdaSopenharmony_ci     * @param { Callback<LocalServiceInfo> } callback - the callback of on.
1798e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
1808e745fdaSopenharmony_ci     * @since 10
1818e745fdaSopenharmony_ci     */
1828e745fdaSopenharmony_ci    on(type: 'serviceLost', callback: Callback<LocalServiceInfo>): void;
1838e745fdaSopenharmony_ci
1848e745fdaSopenharmony_ci    /**
1858e745fdaSopenharmony_ci     * Starts searching for mDNS services on the LAN.
1868e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
1878e745fdaSopenharmony_ci     * @since 10
1888e745fdaSopenharmony_ci     */
1898e745fdaSopenharmony_ci    startSearchingMDNS(): void;
1908e745fdaSopenharmony_ci
1918e745fdaSopenharmony_ci    /**
1928e745fdaSopenharmony_ci     * Stops searching for mDNS services on the LAN.
1938e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
1948e745fdaSopenharmony_ci     * @since 10
1958e745fdaSopenharmony_ci     */
1968e745fdaSopenharmony_ci    stopSearchingMDNS(): void;
1978e745fdaSopenharmony_ci  }
1988e745fdaSopenharmony_ci
1998e745fdaSopenharmony_ci  /**
2008e745fdaSopenharmony_ci   * Defines the mDNS service information.
2018e745fdaSopenharmony_ci   * @interface LocalServiceInfo
2028e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
2038e745fdaSopenharmony_ci   * @since 10
2048e745fdaSopenharmony_ci   */
2058e745fdaSopenharmony_ci  export interface LocalServiceInfo {
2068e745fdaSopenharmony_ci    /**
2078e745fdaSopenharmony_ci     * Service type. Use an underscore (_) as the prefix, for example, _http._tcp.
2088e745fdaSopenharmony_ci     * @type {string}
2098e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2108e745fdaSopenharmony_ci     * @since 10
2118e745fdaSopenharmony_ci     */
2128e745fdaSopenharmony_ci    serviceType: string;
2138e745fdaSopenharmony_ci    /**
2148e745fdaSopenharmony_ci     * Service name.
2158e745fdaSopenharmony_ci     * @type {string}
2168e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2178e745fdaSopenharmony_ci     * @since 10
2188e745fdaSopenharmony_ci     */
2198e745fdaSopenharmony_ci    serviceName: string;
2208e745fdaSopenharmony_ci    /**
2218e745fdaSopenharmony_ci     * Port number.
2228e745fdaSopenharmony_ci     * @type {?number}
2238e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2248e745fdaSopenharmony_ci     * @since 10
2258e745fdaSopenharmony_ci     */
2268e745fdaSopenharmony_ci    port?: number;
2278e745fdaSopenharmony_ci    /**
2288e745fdaSopenharmony_ci     * IP address of the host.
2298e745fdaSopenharmony_ci     * @type {?NetAddress}
2308e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2318e745fdaSopenharmony_ci     * @since 10
2328e745fdaSopenharmony_ci     */
2338e745fdaSopenharmony_ci    host?: NetAddress;
2348e745fdaSopenharmony_ci    /**
2358e745fdaSopenharmony_ci     * DNS-SD TXT record pairs.
2368e745fdaSopenharmony_ci     * @type {?Array<ServiceAttribute>}
2378e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2388e745fdaSopenharmony_ci     * @since 10
2398e745fdaSopenharmony_ci     */
2408e745fdaSopenharmony_ci    serviceAttribute?: Array<ServiceAttribute>;
2418e745fdaSopenharmony_ci  }
2428e745fdaSopenharmony_ci
2438e745fdaSopenharmony_ci  /**
2448e745fdaSopenharmony_ci   * Defines the mDNS service attribute information.
2458e745fdaSopenharmony_ci   * @interface ServiceAttribute
2468e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
2478e745fdaSopenharmony_ci   * @since 10
2488e745fdaSopenharmony_ci   */
2498e745fdaSopenharmony_ci  export interface ServiceAttribute {
2508e745fdaSopenharmony_ci    /**
2518e745fdaSopenharmony_ci     * TXT record key.
2528e745fdaSopenharmony_ci     * @type {string}
2538e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2548e745fdaSopenharmony_ci     * @since 10
2558e745fdaSopenharmony_ci     */
2568e745fdaSopenharmony_ci    key: string;
2578e745fdaSopenharmony_ci    /**
2588e745fdaSopenharmony_ci     * TXT record value.
2598e745fdaSopenharmony_ci     * @type {Array<number>}
2608e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2618e745fdaSopenharmony_ci     * @since 10
2628e745fdaSopenharmony_ci     */
2638e745fdaSopenharmony_ci    value: Array<number>;
2648e745fdaSopenharmony_ci  }
2658e745fdaSopenharmony_ci
2668e745fdaSopenharmony_ci  /**
2678e745fdaSopenharmony_ci   * Defines the mDNS error information.
2688e745fdaSopenharmony_ci   * @enum {number}
2698e745fdaSopenharmony_ci   * @syscap SystemCapability.Communication.NetManager.MDNS
2708e745fdaSopenharmony_ci   * @since 10
2718e745fdaSopenharmony_ci   */
2728e745fdaSopenharmony_ci  export enum MdnsError {
2738e745fdaSopenharmony_ci    /**
2748e745fdaSopenharmony_ci     * Indicates that the operation failed due to internal error.
2758e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2768e745fdaSopenharmony_ci     * @since 10
2778e745fdaSopenharmony_ci     */
2788e745fdaSopenharmony_ci    INTERNAL_ERROR = 0,
2798e745fdaSopenharmony_ci
2808e745fdaSopenharmony_ci    /**
2818e745fdaSopenharmony_ci     * Indicates that the operation failed because it is already active.
2828e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2838e745fdaSopenharmony_ci     * @since 10
2848e745fdaSopenharmony_ci     */
2858e745fdaSopenharmony_ci    ALREADY_ACTIVE = 1,
2868e745fdaSopenharmony_ci
2878e745fdaSopenharmony_ci    /**
2888e745fdaSopenharmony_ci     * <p>Indicates that the operation failed because the maximum outstanding
2898e745fdaSopenharmony_ci     * requests from the applications have reached.</p>
2908e745fdaSopenharmony_ci     * @syscap SystemCapability.Communication.NetManager.MDNS
2918e745fdaSopenharmony_ci     * @since 10
2928e745fdaSopenharmony_ci     */
2938e745fdaSopenharmony_ci    MAX_LIMIT = 2
2948e745fdaSopenharmony_ci  }
2958e745fdaSopenharmony_ci}
2968e745fdaSopenharmony_ci
2978e745fdaSopenharmony_ci/**
2988e745fdaSopenharmony_ci * @since 10
2998e745fdaSopenharmony_ci */
3008e745fdaSopenharmony_ciexport default mdns;