• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16/**
17 * @file
18 * @kit DistributedServiceKit
19 */
20
21import { Callback } from './@ohos.base';
22
23/**
24 * Provides methods to establish enhance link.
25 *
26 * @namespace linkEnhance
27 * @syscap SystemCapability.DistributedSched.AppCollaboration
28 * @since 20
29 */
30declare namespace linkEnhance {
31
32  /**
33   * Describes the result of connect operation.
34   *
35   * @typedef ConnectResult
36   * @syscap SystemCapability.DistributedSched.AppCollaboration
37   * @since 20
38   */
39  interface ConnectResult {
40    /**
41     * Indicates the peer device id.
42     *
43     * @type { string }
44     * @syscap SystemCapability.DistributedSched.AppCollaboration
45     * @since 20
46     */
47    deviceId: string;
48
49    /**
50     * Indicates success or not, true if success, false if failure.
51     *
52     * @type { boolean }
53     * @syscap SystemCapability.DistributedSched.AppCollaboration
54     * @since 20
55     */
56    success: boolean;
57
58    /**
59     * Indicates the reason if failure, it is always 0 if success.
60     * value below:
61     * - 32390200 - Connect timeout.
62     * - 32390201 - Peer server is not started.
63     * - 32390300 - Internal error.
64     *
65     * @type { number }
66     * @syscap SystemCapability.DistributedSched.AppCollaboration
67     * @since 20
68     */
69    reason: number
70  }
71
72
73  /**
74   * Manages server.
75   * Before calling any server methods, you must call {@link createServer} first.
76   *
77   * @typedef Server
78   * @syscap SystemCapability.DistributedSched.AppCollaboration
79   * @since 20
80   */
81  interface Server {
82    /**
83     * Start server which can be connected by others.
84     *
85     * @permission ohos.permission.DISTRIBUTED_DATASYNC
86     * @throws { BusinessError } 201 - Permission denied.
87     * @throws { BusinessError } 32390202 - The number of servers exceeds the limit.
88     * @throws { BusinessError } 32390300 - Internal error.
89     * @syscap SystemCapability.DistributedSched.AppCollaboration
90     * @since 20
91     */
92    start(): void;
93
94    /**
95     * Stop server which can not be connected ever.
96     *
97     * @permission ohos.permission.DISTRIBUTED_DATASYNC
98     * @throws { BusinessError } 201 - Permission denied.
99     * @syscap SystemCapability.DistributedSched.AppCollaboration
100     * @since 20
101     */
102    stop(): void;
103
104    /**
105     * Closes this {@code Server} object and unregisters its callbacks.
106     *
107     * @permission ohos.permission.DISTRIBUTED_DATASYNC
108     * @throws { BusinessError } 201 - Permission denied.
109     * @syscap SystemCapability.DistributedSched.AppCollaboration
110     * @since 20
111     */
112    close(): void;
113
114    /**
115     * Subscribe server is connected event.
116     *
117     * @permission ohos.permission.DISTRIBUTED_DATASYNC
118     * @param { 'connectionAccepted' } type - Type of the server is connected event to listen for.
119     * @param { Callback<Connection> } callback - Callback used to listen for the server is connected event.
120     * @throws { BusinessError } 201 - Permission denied.
121     * @throws { BusinessError } 32390206 - Parameter invalid.
122     * @syscap SystemCapability.DistributedSched.AppCollaboration
123     * @since 20
124     */
125    on(type: 'connectionAccepted', callback: Callback<Connection>): void;
126
127    /**
128     * Unsubscribe server is connected event.
129     *
130     * @permission ohos.permission.DISTRIBUTED_DATASYNC
131     * @param { 'connectionAccepted' } type - Type of the server is connected event to listen for.
132     * @param { Callback<Connection> } callback - Callback used to listen for the server is connected event.
133     * @throws { BusinessError } 201 -  Permission denied.
134     * @throws { BusinessError } 32390206 - Parameter invalid.
135     * @syscap SystemCapability.DistributedSched.AppCollaboration
136     * @since 20
137     */
138    off(type: 'connectionAccepted', callback?: Callback<Connection>): void;
139
140    /**
141     * Subscribe server stop event, it should always rebuild the server after being called.
142     *
143     * @permission ohos.permission.DISTRIBUTED_DATASYNC
144     * @param { 'serverStopped' } type - Type of the server state change event to listen for.
145     * @param { Callback<number> } callback - Callback used to listen for the server stop event.
146     * @throws { BusinessError } 201 - Permission denied.
147     * @throws { BusinessError } 32390206 - Parameter invalid.
148     * @syscap SystemCapability.DistributedSched.AppCollaboration
149     * @since 20
150     */
151    on(type: 'serverStopped', callback: Callback<number>): void;
152
153    /**
154     * Unsubscribe server stop event.
155     *
156     * @permission ohos.permission.DISTRIBUTED_DATASYNC
157     * @param { 'serverStopped' } type - Type of the server state change event to listen for.
158     * @param { Callback<number> } callback - Callback used to listen for the server state change event.
159     * @throws { BusinessError } 201 - Permission denied.
160     * @throws { BusinessError } 32390206 - Parameter invalid.
161     * @syscap SystemCapability.DistributedSched.AppCollaboration
162     * @since 20
163     */
164    off(type: 'serverStopped', callback?: Callback<number>): void;
165
166  }
167
168  /**
169   * Create an server instance.
170   *
171   * @permission ohos.permission.DISTRIBUTED_DATASYNC
172   * @param { string } name - Name of the server.
173   * @returns { Server } Returns a server instance {@code Server}.
174   * @throws { BusinessError } 201 - Permission denied.
175   * @throws { BusinessError } 32390206 - Invalid parameter.
176   * @throws { BusinessError } 32390203 - Duplicate server name.
177   * @syscap SystemCapability.DistributedSched.AppCollaboration
178   * @since 20
179   */
180  function createServer(name: string): Server;
181
182  /**
183   * Manages connection.
184   * Before calling any connection methods, you must use {@link createServer} to create an instance,
185   * or get a connection from {@link acceptConnect} of server.
186   *
187   * @typedef Connection
188   * @syscap SystemCapability.DistributedSched.AppCollaboration
189   * @since 20
190   */
191  interface Connection {
192    /**
193     * Connects to the peer server, which must be in connectable advertising state.
194     * <p>The 'ConnectResult' event should be subscribed to get the result of this operation.
195     *
196     * @permission ohos.permission.DISTRIBUTED_DATASYNC
197     * @throws { BusinessError } 201 - Permission denied.
198     * @throws { BusinessError } 32390204 - The number of connection exceeds the limit.
199     * @throws { BusinessError } 32390300 - Internal error.
200     * @syscap SystemCapability.DistributedSched.AppCollaboration
201     * @since 20
202     */
203    connect(): void;
204
205    /**
206     * Disconnects from or stops an ongoing connection to the peer server.
207     *
208     * @permission ohos.permission.DISTRIBUTED_DATASYNC
209     * @throws { BusinessError } 201 - Permission denied.
210     * @syscap SystemCapability.DistributedSched.AppCollaboration
211     * @since 20
212     */
213    disconnect(): void;
214
215    /**
216     * Disables connection.
217     * <p> This method unregisters the device and clears all registered callbacks and handles.
218     *
219     * @permission ohos.permission.DISTRIBUTED_DATASYNC
220     * @throws { BusinessError } 201 - Permission denied.
221     * @syscap SystemCapability.DistributedSched.AppCollaboration
222     * @since 20
223     */
224    close(): void;
225
226    /**
227     * Gets the peer device id of connection
228     *
229     * @permission ohos.permission.DISTRIBUTED_DATASYNC
230     * @returns { string } Returns the peer device id, return "" if operation failed.
231     * @throws { BusinessError } 201 - Permission denied.
232     * @syscap SystemCapability.DistributedSched.AppCollaboration
233     * @since 20
234     */
235    getPeerDeviceId(): string;
236
237    /**
238     * Send data to the peer.
239     *
240     * @permission ohos.permission.DISTRIBUTED_DATASYNC
241     * @param { ArrayBuffer } data - Indicates data to be sent.
242     * @throws { BusinessError } 201 - Permission denied.
243     * @throws { BusinessError } 32390206 - Invalid parameter.
244     * @throws { BusinessError } 32390205 - Connection is not ready.
245     * @throws { BusinessError } 32390300 - Internal error.
246     * @syscap SystemCapability.DistributedSched.AppCollaboration
247     * @since 20
248     */
249    sendData(data: ArrayBuffer): void;
250
251    /**
252     * Subscribe connect result event.
253     *
254     * @permission ohos.permission.DISTRIBUTED_DATASYNC
255     * @param { 'connectResult' } type - Type of result event to listen for.
256     * @param { Callback<ConnectResult> } callback - Callback used to listen for result  event.
257     * @throws { BusinessError } 201 - Permission denied.
258     * @throws { BusinessError } 32390206 - Invalid parameter.
259     * @syscap SystemCapability.DistributedSched.AppCollaboration
260     * @since 20
261     */
262    on(type: 'connectResult', callback: Callback<ConnectResult>): void;
263
264    /**
265     * Unsubscribe connect result event.
266     *
267     * @permission ohos.permission.DISTRIBUTED_DATASYNC
268     * @param { 'connectResult' } type - Type of result event to listen for.
269     * @param { Callback<ConnectResult> } callback - Callback used to listen for result  event.
270     * @throws { BusinessError } 201 - Permission denied.
271     * @throws { BusinessError } 32390206 - Invalid parameter.
272     * @syscap SystemCapability.DistributedSched.AppCollaboration
273     * @since 20
274     */
275    off(type: 'connectResult', callback?: Callback<ConnectResult>): void;
276
277    /**
278     * Subscribe connection disconnected event.
279     *
280     * @permission ohos.permission.DISTRIBUTED_DATASYNC
281     * @param { 'disconnected' } type - Type of connection disconnected event to listen for.
282     * @param { Callback<number> } callback - Callback used to listen for the connection disconnected event.
283     * @throws { BusinessError } 201 - Permission denied.
284     * @throws { BusinessError } 32390206 - Invalid parameter.
285     * @syscap SystemCapability.DistributedSched.AppCollaboration
286     * @since 20
287     */
288    on(type: 'disconnected', callback: Callback<number>): void;
289
290    /**
291     * Unsubscribe connection disconnected event.
292     *
293     * @permission ohos.permission.DISTRIBUTED_DATASYNC
294     * @param { 'disconnected' } type - Type of connection disconnected event to listen for.
295     * @param { Callback<number> } callback - Callback used to listen for the connection disconnected event.
296     * @throws { BusinessError } 201 - Permission denied.
297     * @throws { BusinessError } 32390206 - Invalid parameter.
298     * @syscap SystemCapability.DistributedSched.AppCollaboration
299     * @since 20
300     */
301    off(type: 'disconnected', callback?: Callback<number>): void;
302
303    /**
304     * Subscribe connection data received event.
305     *
306     * @permission ohos.permission.DISTRIBUTED_DATASYNC
307     * @param { 'dataReceived' } type - Type of the connection data received event to listen for.
308     * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the connection data received event.
309     * @throws { BusinessError } 201 - Permission denied.
310     * @throws { BusinessError } 32390206 - Invalid parameter.
311     * @syscap SystemCapability.DistributedSched.AppCollaboration
312     * @since 20
313     */
314    on(type: 'dataReceived', callback: Callback<ArrayBuffer>): void;
315
316    /**
317     * Unsubscribe connection data received event.
318     *
319     * @permission ohos.permission.DISTRIBUTED_DATASYNC
320     * @param { 'dataReceived' } type - Type of the connection data received event to listen for.
321     * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the connection data received event.
322     * @throws { BusinessError } 201 - Permission denied.
323     * @throws { BusinessError } 32390206 - Invalid parameter.
324     * @syscap SystemCapability.DistributedSched.AppCollaboration
325     * @since 20
326     */
327    off(type: 'dataReceived', callback?: Callback<ArrayBuffer>): void;
328  }
329
330  /**
331   * Create a connection instance.
332   *
333   * @permission ohos.permission.DISTRIBUTED_DATASYNC
334   * @param { string } deviceId - Indicates device id.
335   * @param { string } name - Indicates server name to be connected.
336   * @returns { Connection } Returns a connection instance {@code Connection}.
337   * @throws { BusinessError } 201 - Permission denied.
338   * @throws { BusinessError } 32390206 - Invalid parameter.
339   * @syscap SystemCapability.DistributedSched.AppCollaboration
340   * @since 20
341   */
342  function createConnection(deviceId: string, name: string): Connection;
343}
344
345export default linkEnhance;
346