• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {AsyncCallback, Callback} from "./basic";
17import connection from "./@ohos.net.connection";
18import Context from "./application/BaseContext";
19
20/**
21 * Provides interfaces to discover DNS based services on a local network over Multicast DNS.
22 *
23 * @since 9
24 * @syscap SystemCapability.Communication.NetManager.MDNS
25 */
26declare namespace mdns {
27  type NetAddress = connection.NetAddress;
28   bundleName = Context.getBundleName()
29  /**
30   * Adds an mDNS service.
31   *
32   * @param context Indicates the context of application or capability.
33   * @param serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
34   * @throws {BusinessError} 401 - Parameter error.
35   * @throws {BusinessError} 2100002 - Operation failed. Cannot connect to service.
36   * @throws {BusinessError} 2100003 - System internal error.
37   * @throws {BusinessError} 2204003 - Callback duplicated.
38   * @throws {BusinessError} 2204008 - Service instance duplicated.
39   * @throws {BusinessError} 2204010 - Send packet failed.
40  */
41  function addLocalService(context: bundleName, serviceInfo: LocalServiceInfo,
42    callback: AsyncCallback<LocalServiceInfo>): void;
43  function addLocalService(context: bundleName, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
44
45  /**
46   * Removes an mDNS service.
47   *
48   * @param context Indicates the context of application or capability.
49   * @param serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
50   * @throws {BusinessError} 401 - Parameter error.
51   * @throws {BusinessError} 2100002 - Operation failed. Cannot connect to service.
52   * @throws {BusinessError} 2100003 - System internal error.
53   * @throws {BusinessError} 2204002 - Callback not found.
54   * @throws {BusinessError} 2204008 - Service instance not found.
55   * @throws {BusinessError} 2204010 - Send packet failed.
56  */
57  function removeLocalService(context: bundleName, serviceInfo: LocalServiceInfo,
58    callback: AsyncCallback<LocalServiceInfo>): void;
59  function removeLocalService(context: bundleName, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
60
61  /**
62   * Create an mDNS based discovery service with context and serviceType.
63   *
64   * @param serviceType The service type being discovered.
65   * @throws {BusinessError} 401 - Parameter error.
66  */
67  function createDiscoveryService(context: bundleName, serviceType: string): DiscoveryService;
68
69  /**
70   * Resolves an mDNS service.
71   *
72   * @param context Indicates the context of application or capability.
73   * @param serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
74   * @throws {BusinessError} 401 - Parameter error.
75   * @throws {BusinessError} 2100002 - Operation failed. Cannot connect to service.
76   * @throws {BusinessError} 2100003 - System internal error.
77   * @throws {BusinessError} 2204003 - Callback duplicated.
78   * @throws {BusinessError} 2204006 - Request timeout.
79   * @throws {BusinessError} 2204010 - Send packet failed.
80  */
81  function resolveLocalService(context: bundleName, serviceInfo: LocalServiceInfo,
82    callback: AsyncCallback<LocalServiceInfo>): void;
83  function resolveLocalService(context: bundleName, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
84
85  export interface DiscoveryService {
86    /**
87     * Enables listening for discoveryStart events of mDNS services.
88     */
89    on(type: 'discoveryStart',
90      callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MDNS_ERR}>): void;
91
92    /**
93     * Enables listening for discoveryStop events of mDNS services.
94     */
95    on(type: 'discoveryStop',
96      callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MDNS_ERR}>): void;
97
98    /**
99     * Enables listening for serviceFound events of mDNS services.
100     */
101    on(type: 'serviceFound', callback: Callback<LocalServiceInfo>): void;
102
103    /**
104     * Enables listening for serviceLost events of mDNS services.
105     */
106    on(type: 'serviceLost', callback: Callback<LocalServiceInfo>): void;
107
108    /**
109     * Starts searching for mDNS services on the LAN.
110     */
111    startSearchingMDNS(): void;
112
113    /**
114     * Stops searching for mDNS services on the LAN.
115     */
116    stopSearchingMDNS(): void;
117  }
118
119  export interface LocalServiceInfo {
120    /**
121     * Service type. Use an underscore (_) as the prefix, for example, _http._tcp.
122     */
123    serviceType: string;
124    /**
125     * Service name.
126     */
127    serviceName: string;
128    /**
129     * Port number.
130     */
131    port?: number;
132    /**
133     * IP address of the host.
134     */
135    host?: NetAddress;
136    /**
137     * DNS-SD TXT record pairs.
138     */
139    serviceAttribute?: Array<ServiceAttribute>;
140  }
141
142  export interface ServiceAttribute {
143    /**
144     * TXT record key.
145     */
146    key: string;
147    /**
148     * TXT record value.
149     */
150    value: Array<number>;
151  }
152
153  export enum MDNS_ERR {
154    /**
155     * Indicates that the operation failed due to   internal error.
156     */
157    INTERNAL_ERROR = 0,
158
159    /**
160     * Indicates that the operation failed because it is already active.
161     */
162    ALREADY_ACTIVE = 1,
163
164    /**
165     * Indicates that the operation failed because the maximum outstanding
166     * requests from the applications have reached.
167     */
168    MAX_LIMIT = 2
169  }
170}
171
172export default mdns;