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 "./@ohos.base";
17import connection from "./@ohos.net.connection";
18import Context from "./application/Context";
19
20/**
21 * Provides interfaces to discover DNS based services on a local network over Multicast DNS.
22 * @namespace mdns
23 * @syscap SystemCapability.Communication.NetManager.MDNS
24 * @since 10
25 */
26declare namespace mdns {
27  type NetAddress = connection.NetAddress;
28
29  /**
30   * Adds an mDNS service.
31   * @param { Context } context Indicates the context of application or capability.
32   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
33   * @param { AsyncCallback<LocalServiceInfo> } callback Returns the callback of addLocalService.
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   * @syscap SystemCapability.Communication.NetManager.MDNS
41   * @since 10
42   */
43  function addLocalService(context: Context, serviceInfo: LocalServiceInfo,
44                           callback: AsyncCallback<LocalServiceInfo>): void;
45
46  /**
47   * Adds an mDNS service.
48   * @param { Context } context Indicates the context of application or capability.
49   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
50   * @returns { Promise<LocalServiceInfo> } The promise returned by the function.
51   * @throws { BusinessError } 401 - Parameter error.
52   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
53   * @throws { BusinessError } 2100003 - System internal error.
54   * @throws { BusinessError } 2204003 - Callback duplicated.
55   * @throws { BusinessError } 2204008 - Service instance duplicated.
56   * @throws { BusinessError } 2204010 - Send packet failed.
57   * @syscap SystemCapability.Communication.NetManager.MDNS
58   * @since 10
59   */
60  function addLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
61
62  /**
63   * Removes an mDNS service.
64   * @param { Context } context Indicates the context of application or capability.
65   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
66   * @param { AsyncCallback<LocalServiceInfo> } callback Returns the callback of removeLocalService.
67   * @throws { BusinessError } 401 - Parameter error.
68   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
69   * @throws { BusinessError } 2100003 - System internal error.
70   * @throws { BusinessError } 2204002 - Callback not found.
71   * @throws { BusinessError } 2204008 - Service instance not found.
72   * @throws { BusinessError } 2204010 - Send packet failed.
73   * @syscap SystemCapability.Communication.NetManager.MDNS
74   * @since 10
75   */
76  function removeLocalService(context: Context, serviceInfo: LocalServiceInfo,
77                              callback: AsyncCallback<LocalServiceInfo>): void;
78
79  /**
80   * Removes an mDNS service.
81   * @param { Context } context Indicates the context of application or capability.
82   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
83   * @returns { Promise<LocalServiceInfo> } The promise returned by the function.
84   * @throws { BusinessError } 401 - Parameter error.
85   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
86   * @throws { BusinessError } 2100003 - System internal error.
87   * @throws { BusinessError } 2204002 - Callback not found.
88   * @throws { BusinessError } 2204008 - Service instance not found.
89   * @throws { BusinessError } 2204010 - Send packet failed.
90   * @syscap SystemCapability.Communication.NetManager.MDNS
91   * @since 10
92   */
93  function removeLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
94
95  /**
96   * Create an mDNS based discovery service with context and serviceType.
97   * @param { Context } context Indicates the context of application or capability.
98   * @param { string } serviceType The service type being discovered.
99   * @returns { DiscoveryService } the DiscoveryService of the createDiscoveryService.
100   * @throws { BusinessError } 401 - Parameter error.
101   * @syscap SystemCapability.Communication.NetManager.MDNS
102   * @since 10
103   */
104  function createDiscoveryService(context: Context, serviceType: string): DiscoveryService;
105
106  /**
107   * Resolves an mDNS service.
108   * @param { Context } context Indicates the context of application or capability.
109   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
110   * @param { AsyncCallback<LocalServiceInfo> } callback Returns the callback of resolveLocalService.
111   * @throws { BusinessError } 401 - Parameter error.
112   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
113   * @throws { BusinessError } 2100003 - System internal error.
114   * @throws { BusinessError } 2204003 - Callback duplicated.
115   * @throws { BusinessError } 2204006 - Request timeout.
116   * @throws { BusinessError } 2204010 - Send packet failed.
117   * @syscap SystemCapability.Communication.NetManager.MDNS
118   * @since 10
119   */
120  function resolveLocalService(context: Context, serviceInfo: LocalServiceInfo,
121                               callback: AsyncCallback<LocalServiceInfo>): void;
122
123  /**
124   * Resolves an mDNS service.
125   * @param { Context } context Indicates the context of application or capability.
126   * @param { LocalServiceInfo } serviceInfo Information about the mDNS service. {@link LocalServiceInfo}
127   * @returns { Promise<LocalServiceInfo> } The promise returned by the function.
128   * @throws { BusinessError } 401 - Parameter error.
129   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
130   * @throws { BusinessError } 2100003 - System internal error.
131   * @throws { BusinessError } 2204003 - Callback duplicated.
132   * @throws { BusinessError } 2204006 - Request timeout.
133   * @throws { BusinessError } 2204010 - Send packet failed.
134   * @syscap SystemCapability.Communication.NetManager.MDNS
135   * @since 10
136   */
137  function resolveLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>;
138
139  /**
140   * Defines a DiscoveryService object for discovering mDNS services of the specified type.
141   * @interface DiscoveryService
142   * @syscap SystemCapability.Communication.NetManager.MDNS
143   * @since 10
144   */
145  export interface DiscoveryService {
146    /**
147     * Enables listening for discoveryStart events of mDNS services.
148     * @param { 'discoveryStart' } type Indicates Event name.
149     * @param { Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }> } callback - the callback of on.
150     * @syscap SystemCapability.Communication.NetManager.MDNS
151     * @since 10
152     */
153    on(type: 'discoveryStart',
154       callback: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void;
155
156    /**
157     * Enables listening for discoveryStop events of mDNS services.
158     * @param { 'discoveryStop' } type Indicates Event name.
159     * @param { Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }> } callback - the callback of on.
160     * @syscap SystemCapability.Communication.NetManager.MDNS
161     * @since 10
162     */
163    on(type: 'discoveryStop',
164       callback: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void;
165
166    /**
167     * Enables listening for serviceFound events of mDNS services.
168     * @param { 'serviceFound' } type Indicates Event name.
169     * @param { Callback<LocalServiceInfo> } callback - the callback of on.
170     * @syscap SystemCapability.Communication.NetManager.MDNS
171     * @since 10
172     */
173    on(type: 'serviceFound', callback: Callback<LocalServiceInfo>): void;
174
175    /**
176     * Enables listening for serviceLost events of mDNS services.
177     * @param { 'serviceLost' } type Indicates Event name.
178     * @param { Callback<LocalServiceInfo> } callback - the callback of on.
179     * @syscap SystemCapability.Communication.NetManager.MDNS
180     * @since 10
181     */
182    on(type: 'serviceLost', callback: Callback<LocalServiceInfo>): void;
183
184    /**
185     * Starts searching for mDNS services on the LAN.
186     * @syscap SystemCapability.Communication.NetManager.MDNS
187     * @since 10
188     */
189    startSearchingMDNS(): void;
190
191    /**
192     * Stops searching for mDNS services on the LAN.
193     * @syscap SystemCapability.Communication.NetManager.MDNS
194     * @since 10
195     */
196    stopSearchingMDNS(): void;
197  }
198
199  /**
200   * Defines the mDNS service information.
201   * @interface LocalServiceInfo
202   * @syscap SystemCapability.Communication.NetManager.MDNS
203   * @since 10
204   */
205  export interface LocalServiceInfo {
206    /**
207     * Service type. Use an underscore (_) as the prefix, for example, _http._tcp.
208     * @type {string}
209     * @syscap SystemCapability.Communication.NetManager.MDNS
210     * @since 10
211     */
212    serviceType: string;
213    /**
214     * Service name.
215     * @type {string}
216     * @syscap SystemCapability.Communication.NetManager.MDNS
217     * @since 10
218     */
219    serviceName: string;
220    /**
221     * Port number.
222     * @type {?number}
223     * @syscap SystemCapability.Communication.NetManager.MDNS
224     * @since 10
225     */
226    port?: number;
227    /**
228     * IP address of the host.
229     * @type {?NetAddress}
230     * @syscap SystemCapability.Communication.NetManager.MDNS
231     * @since 10
232     */
233    host?: NetAddress;
234    /**
235     * DNS-SD TXT record pairs.
236     * @type {?Array<ServiceAttribute>}
237     * @syscap SystemCapability.Communication.NetManager.MDNS
238     * @since 10
239     */
240    serviceAttribute?: Array<ServiceAttribute>;
241  }
242
243  /**
244   * Defines the mDNS service attribute information.
245   * @interface ServiceAttribute
246   * @syscap SystemCapability.Communication.NetManager.MDNS
247   * @since 10
248   */
249  export interface ServiceAttribute {
250    /**
251     * TXT record key.
252     * @type {string}
253     * @syscap SystemCapability.Communication.NetManager.MDNS
254     * @since 10
255     */
256    key: string;
257    /**
258     * TXT record value.
259     * @type {Array<number>}
260     * @syscap SystemCapability.Communication.NetManager.MDNS
261     * @since 10
262     */
263    value: Array<number>;
264  }
265
266  /**
267   * Defines the mDNS error information.
268   * @enum {number}
269   * @syscap SystemCapability.Communication.NetManager.MDNS
270   * @since 10
271   */
272  export enum MdnsError {
273    /**
274     * Indicates that the operation failed due to internal error.
275     * @syscap SystemCapability.Communication.NetManager.MDNS
276     * @since 10
277     */
278    INTERNAL_ERROR = 0,
279
280    /**
281     * Indicates that the operation failed because it is already active.
282     * @syscap SystemCapability.Communication.NetManager.MDNS
283     * @since 10
284     */
285    ALREADY_ACTIVE = 1,
286
287    /**
288     * <p>Indicates that the operation failed because the maximum outstanding
289     * requests from the applications have reached.</p>
290     * @syscap SystemCapability.Communication.NetManager.MDNS
291     * @since 10
292     */
293    MAX_LIMIT = 2
294  }
295}
296
297/**
298 * @since 10
299 */
300export default mdns;